4 * Copyright(c) 2016-2017 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <sys/types.h>
38 #include <netinet/in.h>
39 #include <netinet/ip.h>
40 #include <netinet/ip6.h>
44 #include <rte_common.h>
45 #include <rte_crypto.h>
46 #include <rte_cryptodev.h>
47 #include <rte_random.h>
53 #define IV_OFFSET (sizeof(struct rte_crypto_op) + \
54 sizeof(struct rte_crypto_sym_op))
57 esp_inbound(struct rte_mbuf *m, struct ipsec_sa *sa,
58 struct rte_crypto_op *cop)
61 struct rte_crypto_sym_op *sym_cop;
62 int32_t payload_len, ip_hdr_len;
64 RTE_ASSERT(m != NULL);
65 RTE_ASSERT(sa != NULL);
66 RTE_ASSERT(cop != NULL);
68 ip4 = rte_pktmbuf_mtod(m, struct ip *);
69 if (likely(ip4->ip_v == IPVERSION))
70 ip_hdr_len = ip4->ip_hl * 4;
71 else if (ip4->ip_v == IP6_VERSION)
72 /* XXX No option headers supported */
73 ip_hdr_len = sizeof(struct ip6_hdr);
75 RTE_LOG(ERR, IPSEC_ESP, "invalid IP packet type %d\n",
80 payload_len = rte_pktmbuf_pkt_len(m) - ip_hdr_len -
81 sizeof(struct esp_hdr) - sa->iv_len - sa->digest_len;
83 if ((payload_len & (sa->block_size - 1)) || (payload_len <= 0)) {
84 RTE_LOG_DP(DEBUG, IPSEC_ESP, "payload %d not multiple of %u\n",
85 payload_len, sa->block_size);
89 sym_cop = get_sym_cop(cop);
92 sym_cop->cipher.data.offset = ip_hdr_len + sizeof(struct esp_hdr) +
94 sym_cop->cipher.data.length = payload_len;
98 uint8_t *iv = RTE_PTR_ADD(ip4, ip_hdr_len + sizeof(struct esp_hdr));
99 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(cop,
100 uint8_t *, IV_OFFSET);
102 switch (sa->cipher_algo) {
103 case RTE_CRYPTO_CIPHER_NULL:
104 case RTE_CRYPTO_CIPHER_AES_CBC:
105 /* Copy IV at the end of crypto operation */
106 rte_memcpy(iv_ptr, iv, sa->iv_len);
107 sym_cop->cipher.iv.data = iv_ptr;
108 sym_cop->cipher.iv.phys_addr =
109 rte_crypto_op_ctophys_offset(cop, IV_OFFSET);
110 sym_cop->cipher.iv.length = sa->iv_len;
112 case RTE_CRYPTO_CIPHER_AES_CTR:
113 case RTE_CRYPTO_CIPHER_AES_GCM:
114 icb = get_cnt_blk(m);
115 icb->salt = sa->salt;
116 memcpy(&icb->iv, iv, 8);
117 icb->cnt = rte_cpu_to_be_32(1);
118 sym_cop->cipher.iv.data = iv_ptr;
119 sym_cop->cipher.iv.phys_addr =
120 rte_crypto_op_ctophys_offset(cop, IV_OFFSET);
121 sym_cop->cipher.iv.length = 16;
124 RTE_LOG(ERR, IPSEC_ESP, "unsupported cipher algorithm %u\n",
129 switch (sa->auth_algo) {
130 case RTE_CRYPTO_AUTH_NULL:
131 case RTE_CRYPTO_AUTH_SHA1_HMAC:
132 case RTE_CRYPTO_AUTH_SHA256_HMAC:
133 sym_cop->auth.data.offset = ip_hdr_len;
134 sym_cop->auth.data.length = sizeof(struct esp_hdr) +
135 sa->iv_len + payload_len;
137 case RTE_CRYPTO_AUTH_AES_GCM:
139 memcpy(aad, iv - sizeof(struct esp_hdr), 8);
140 sym_cop->auth.aad.data = aad;
141 sym_cop->auth.aad.phys_addr = rte_pktmbuf_mtophys_offset(m,
142 aad - rte_pktmbuf_mtod(m, uint8_t *));
143 sym_cop->auth.aad.length = 8;
146 RTE_LOG(ERR, IPSEC_ESP, "unsupported auth algorithm %u\n",
151 sym_cop->auth.digest.data = rte_pktmbuf_mtod_offset(m, void*,
152 rte_pktmbuf_pkt_len(m) - sa->digest_len);
153 sym_cop->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
154 rte_pktmbuf_pkt_len(m) - sa->digest_len);
155 sym_cop->auth.digest.length = sa->digest_len;
161 esp_inbound_post(struct rte_mbuf *m, struct ipsec_sa *sa,
162 struct rte_crypto_op *cop)
166 uint8_t *nexthdr, *pad_len;
170 RTE_ASSERT(m != NULL);
171 RTE_ASSERT(sa != NULL);
172 RTE_ASSERT(cop != NULL);
174 if (cop->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
175 RTE_LOG(ERR, IPSEC_ESP, "failed crypto op\n");
179 nexthdr = rte_pktmbuf_mtod_offset(m, uint8_t*,
180 rte_pktmbuf_pkt_len(m) - sa->digest_len - 1);
181 pad_len = nexthdr - 1;
183 padding = pad_len - *pad_len;
184 for (i = 0; i < *pad_len; i++) {
185 if (padding[i] != i + 1) {
186 RTE_LOG(ERR, IPSEC_ESP, "invalid padding\n");
191 if (rte_pktmbuf_trim(m, *pad_len + 2 + sa->digest_len)) {
192 RTE_LOG(ERR, IPSEC_ESP,
193 "failed to remove pad_len + digest\n");
197 if (unlikely(sa->flags == TRANSPORT)) {
198 ip = rte_pktmbuf_mtod(m, struct ip *);
199 ip4 = (struct ip *)rte_pktmbuf_adj(m,
200 sizeof(struct esp_hdr) + sa->iv_len);
201 if (likely(ip->ip_v == IPVERSION)) {
202 memmove(ip4, ip, ip->ip_hl * 4);
203 ip4->ip_p = *nexthdr;
204 ip4->ip_len = htons(rte_pktmbuf_data_len(m));
206 ip6 = (struct ip6_hdr *)ip4;
207 /* XXX No option headers supported */
208 memmove(ip6, ip, sizeof(struct ip6_hdr));
209 ip6->ip6_nxt = *nexthdr;
210 ip6->ip6_plen = htons(rte_pktmbuf_data_len(m));
213 ipip_inbound(m, sizeof(struct esp_hdr) + sa->iv_len);
219 esp_outbound(struct rte_mbuf *m, struct ipsec_sa *sa,
220 struct rte_crypto_op *cop)
224 struct esp_hdr *esp = NULL;
225 uint8_t *padding, *new_ip, nlp;
226 struct rte_crypto_sym_op *sym_cop;
228 uint16_t pad_payload_len, pad_len, ip_hdr_len;
230 RTE_ASSERT(m != NULL);
231 RTE_ASSERT(sa != NULL);
232 RTE_ASSERT(cop != NULL);
236 ip4 = rte_pktmbuf_mtod(m, struct ip *);
237 if (likely(ip4->ip_v == IPVERSION)) {
238 if (unlikely(sa->flags == TRANSPORT)) {
239 ip_hdr_len = ip4->ip_hl * 4;
243 } else if (ip4->ip_v == IP6_VERSION) {
244 if (unlikely(sa->flags == TRANSPORT)) {
245 /* XXX No option headers supported */
246 ip_hdr_len = sizeof(struct ip6_hdr);
247 ip6 = (struct ip6_hdr *)ip4;
252 RTE_LOG(ERR, IPSEC_ESP, "invalid IP packet type %d\n",
257 /* Padded payload length */
258 pad_payload_len = RTE_ALIGN_CEIL(rte_pktmbuf_pkt_len(m) -
259 ip_hdr_len + 2, sa->block_size);
260 pad_len = pad_payload_len + ip_hdr_len - rte_pktmbuf_pkt_len(m);
262 RTE_ASSERT(sa->flags == IP4_TUNNEL || sa->flags == IP6_TUNNEL ||
263 sa->flags == TRANSPORT);
265 if (likely(sa->flags == IP4_TUNNEL))
266 ip_hdr_len = sizeof(struct ip);
267 else if (sa->flags == IP6_TUNNEL)
268 ip_hdr_len = sizeof(struct ip6_hdr);
269 else if (sa->flags != TRANSPORT) {
270 RTE_LOG(ERR, IPSEC_ESP, "Unsupported SA flags: 0x%x\n",
275 /* Check maximum packet size */
276 if (unlikely(ip_hdr_len + sizeof(struct esp_hdr) + sa->iv_len +
277 pad_payload_len + sa->digest_len > IP_MAXPACKET)) {
278 RTE_LOG(ERR, IPSEC_ESP, "ipsec packet is too big\n");
282 padding = (uint8_t *)rte_pktmbuf_append(m, pad_len + sa->digest_len);
283 if (unlikely(padding == NULL)) {
284 RTE_LOG(ERR, IPSEC_ESP, "not enough mbuf trailing space\n");
287 rte_prefetch0(padding);
291 ip4 = ip4ip_outbound(m, sizeof(struct esp_hdr) + sa->iv_len,
293 esp = (struct esp_hdr *)(ip4 + 1);
296 ip6 = ip6ip_outbound(m, sizeof(struct esp_hdr) + sa->iv_len,
298 esp = (struct esp_hdr *)(ip6 + 1);
301 new_ip = (uint8_t *)rte_pktmbuf_prepend(m,
302 sizeof(struct esp_hdr) + sa->iv_len);
303 memmove(new_ip, ip4, ip_hdr_len);
304 esp = (struct esp_hdr *)(new_ip + ip_hdr_len);
305 if (likely(ip4->ip_v == IPVERSION)) {
306 ip4 = (struct ip *)new_ip;
307 ip4->ip_p = IPPROTO_ESP;
308 ip4->ip_len = htons(rte_pktmbuf_data_len(m));
310 ip6 = (struct ip6_hdr *)new_ip;
311 ip6->ip6_nxt = IPPROTO_ESP;
312 ip6->ip6_plen = htons(rte_pktmbuf_data_len(m));
317 esp->spi = rte_cpu_to_be_32(sa->spi);
318 esp->seq = rte_cpu_to_be_32((uint32_t)sa->seq);
320 uint64_t *iv = (uint64_t *)(esp + 1);
322 sym_cop = get_sym_cop(cop);
324 switch (sa->cipher_algo) {
325 case RTE_CRYPTO_CIPHER_NULL:
326 case RTE_CRYPTO_CIPHER_AES_CBC:
327 memset(iv, 0, sa->iv_len);
328 sym_cop->cipher.data.offset = ip_hdr_len +
329 sizeof(struct esp_hdr);
330 sym_cop->cipher.data.length = pad_payload_len + sa->iv_len;
332 case RTE_CRYPTO_CIPHER_AES_CTR:
333 case RTE_CRYPTO_CIPHER_AES_GCM:
335 sym_cop->cipher.data.offset = ip_hdr_len +
336 sizeof(struct esp_hdr) + sa->iv_len;
337 sym_cop->cipher.data.length = pad_payload_len;
340 RTE_LOG(ERR, IPSEC_ESP, "unsupported cipher algorithm %u\n",
345 /* Fill pad_len using default sequential scheme */
346 for (i = 0; i < pad_len - 2; i++)
348 padding[pad_len - 2] = pad_len - 2;
349 padding[pad_len - 1] = nlp;
351 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(cop,
352 uint8_t *, IV_OFFSET);
353 struct cnt_blk *icb = get_cnt_blk(m);
354 icb->salt = sa->salt;
356 icb->cnt = rte_cpu_to_be_32(1);
357 sym_cop->cipher.iv.data = iv_ptr;
358 sym_cop->cipher.iv.phys_addr =
359 rte_crypto_op_ctophys_offset(cop, IV_OFFSET);
360 sym_cop->cipher.iv.length = 16;
364 switch (sa->auth_algo) {
365 case RTE_CRYPTO_AUTH_NULL:
366 case RTE_CRYPTO_AUTH_SHA1_HMAC:
367 case RTE_CRYPTO_AUTH_SHA256_HMAC:
368 sym_cop->auth.data.offset = ip_hdr_len;
369 sym_cop->auth.data.length = sizeof(struct esp_hdr) +
370 sa->iv_len + pad_payload_len;
372 case RTE_CRYPTO_AUTH_AES_GCM:
375 sym_cop->auth.aad.data = aad;
376 sym_cop->auth.aad.phys_addr = rte_pktmbuf_mtophys_offset(m,
377 aad - rte_pktmbuf_mtod(m, uint8_t *));
378 sym_cop->auth.aad.length = 8;
381 RTE_LOG(ERR, IPSEC_ESP, "unsupported auth algorithm %u\n",
386 sym_cop->auth.digest.data = rte_pktmbuf_mtod_offset(m, uint8_t *,
387 rte_pktmbuf_pkt_len(m) - sa->digest_len);
388 sym_cop->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
389 rte_pktmbuf_pkt_len(m) - sa->digest_len);
390 sym_cop->auth.digest.length = sa->digest_len;
396 esp_outbound_post(struct rte_mbuf *m __rte_unused,
397 struct ipsec_sa *sa __rte_unused,
398 struct rte_crypto_op *cop)
400 RTE_ASSERT(m != NULL);
401 RTE_ASSERT(sa != NULL);
402 RTE_ASSERT(cop != NULL);
404 if (cop->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
405 RTE_LOG(ERR, IPSEC_ESP, "Failed crypto op\n");