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>
54 esp_inbound(struct rte_mbuf *m, struct ipsec_sa *sa,
55 struct rte_crypto_op *cop)
58 struct rte_crypto_sym_op *sym_cop;
59 int32_t payload_len, ip_hdr_len;
61 RTE_ASSERT(m != NULL);
62 RTE_ASSERT(sa != NULL);
63 RTE_ASSERT(cop != NULL);
65 ip4 = rte_pktmbuf_mtod(m, struct ip *);
66 if (likely(ip4->ip_v == IPVERSION))
67 ip_hdr_len = ip4->ip_hl * 4;
68 else if (ip4->ip_v == IP6_VERSION)
69 /* XXX No option headers supported */
70 ip_hdr_len = sizeof(struct ip6_hdr);
72 RTE_LOG(ERR, IPSEC_ESP, "invalid IP packet type %d\n",
77 payload_len = rte_pktmbuf_pkt_len(m) - ip_hdr_len -
78 sizeof(struct esp_hdr) - sa->iv_len - sa->digest_len;
80 if ((payload_len & (sa->block_size - 1)) || (payload_len <= 0)) {
81 RTE_LOG_DP(DEBUG, IPSEC_ESP, "payload %d not multiple of %u\n",
82 payload_len, sa->block_size);
86 sym_cop = get_sym_cop(cop);
89 if (sa->aead_algo == RTE_CRYPTO_AEAD_AES_GCM) {
90 sym_cop->aead.data.offset = ip_hdr_len + sizeof(struct esp_hdr) +
92 sym_cop->aead.data.length = payload_len;
96 uint8_t *iv = RTE_PTR_ADD(ip4, ip_hdr_len + sizeof(struct esp_hdr));
100 memcpy(&icb->iv, iv, 8);
101 icb->cnt = rte_cpu_to_be_32(1);
104 memcpy(aad, iv - sizeof(struct esp_hdr), 8);
105 sym_cop->aead.aad.data = aad;
106 sym_cop->aead.aad.phys_addr = rte_pktmbuf_mtophys_offset(m,
107 aad - rte_pktmbuf_mtod(m, uint8_t *));
109 sym_cop->aead.digest.data = rte_pktmbuf_mtod_offset(m, void*,
110 rte_pktmbuf_pkt_len(m) - sa->digest_len);
111 sym_cop->aead.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
112 rte_pktmbuf_pkt_len(m) - sa->digest_len);
114 sym_cop->cipher.data.offset = ip_hdr_len + sizeof(struct esp_hdr) +
116 sym_cop->cipher.data.length = payload_len;
119 uint8_t *iv = RTE_PTR_ADD(ip4, ip_hdr_len + sizeof(struct esp_hdr));
120 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(cop,
121 uint8_t *, IV_OFFSET);
123 switch (sa->cipher_algo) {
124 case RTE_CRYPTO_CIPHER_NULL:
125 case RTE_CRYPTO_CIPHER_AES_CBC:
126 /* Copy IV at the end of crypto operation */
127 rte_memcpy(iv_ptr, iv, sa->iv_len);
129 case RTE_CRYPTO_CIPHER_AES_CTR:
130 icb = get_cnt_blk(m);
131 icb->salt = sa->salt;
132 memcpy(&icb->iv, iv, 8);
133 icb->cnt = rte_cpu_to_be_32(1);
136 RTE_LOG(ERR, IPSEC_ESP, "unsupported cipher algorithm %u\n",
141 switch (sa->auth_algo) {
142 case RTE_CRYPTO_AUTH_NULL:
143 case RTE_CRYPTO_AUTH_SHA1_HMAC:
144 case RTE_CRYPTO_AUTH_SHA256_HMAC:
145 sym_cop->auth.data.offset = ip_hdr_len;
146 sym_cop->auth.data.length = sizeof(struct esp_hdr) +
147 sa->iv_len + payload_len;
150 RTE_LOG(ERR, IPSEC_ESP, "unsupported auth algorithm %u\n",
155 sym_cop->auth.digest.data = rte_pktmbuf_mtod_offset(m, void*,
156 rte_pktmbuf_pkt_len(m) - sa->digest_len);
157 sym_cop->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
158 rte_pktmbuf_pkt_len(m) - sa->digest_len);
165 esp_inbound_post(struct rte_mbuf *m, struct ipsec_sa *sa,
166 struct rte_crypto_op *cop)
170 uint8_t *nexthdr, *pad_len;
174 RTE_ASSERT(m != NULL);
175 RTE_ASSERT(sa != NULL);
176 RTE_ASSERT(cop != NULL);
178 if (cop->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
179 RTE_LOG(ERR, IPSEC_ESP, "failed crypto op\n");
183 nexthdr = rte_pktmbuf_mtod_offset(m, uint8_t*,
184 rte_pktmbuf_pkt_len(m) - sa->digest_len - 1);
185 pad_len = nexthdr - 1;
187 padding = pad_len - *pad_len;
188 for (i = 0; i < *pad_len; i++) {
189 if (padding[i] != i + 1) {
190 RTE_LOG(ERR, IPSEC_ESP, "invalid padding\n");
195 if (rte_pktmbuf_trim(m, *pad_len + 2 + sa->digest_len)) {
196 RTE_LOG(ERR, IPSEC_ESP,
197 "failed to remove pad_len + digest\n");
201 if (unlikely(sa->flags == TRANSPORT)) {
202 ip = rte_pktmbuf_mtod(m, struct ip *);
203 ip4 = (struct ip *)rte_pktmbuf_adj(m,
204 sizeof(struct esp_hdr) + sa->iv_len);
205 if (likely(ip->ip_v == IPVERSION)) {
206 memmove(ip4, ip, ip->ip_hl * 4);
207 ip4->ip_p = *nexthdr;
208 ip4->ip_len = htons(rte_pktmbuf_data_len(m));
210 ip6 = (struct ip6_hdr *)ip4;
211 /* XXX No option headers supported */
212 memmove(ip6, ip, sizeof(struct ip6_hdr));
213 ip6->ip6_nxt = *nexthdr;
214 ip6->ip6_plen = htons(rte_pktmbuf_data_len(m) -
215 sizeof(struct ip6_hdr));
218 ipip_inbound(m, sizeof(struct esp_hdr) + sa->iv_len);
224 esp_outbound(struct rte_mbuf *m, struct ipsec_sa *sa,
225 struct rte_crypto_op *cop)
229 struct esp_hdr *esp = NULL;
230 uint8_t *padding, *new_ip, nlp;
231 struct rte_crypto_sym_op *sym_cop;
233 uint16_t pad_payload_len, pad_len, ip_hdr_len;
235 RTE_ASSERT(m != NULL);
236 RTE_ASSERT(sa != NULL);
237 RTE_ASSERT(cop != NULL);
241 ip4 = rte_pktmbuf_mtod(m, struct ip *);
242 if (likely(ip4->ip_v == IPVERSION)) {
243 if (unlikely(sa->flags == TRANSPORT)) {
244 ip_hdr_len = ip4->ip_hl * 4;
248 } else if (ip4->ip_v == IP6_VERSION) {
249 if (unlikely(sa->flags == TRANSPORT)) {
250 /* XXX No option headers supported */
251 ip_hdr_len = sizeof(struct ip6_hdr);
252 ip6 = (struct ip6_hdr *)ip4;
257 RTE_LOG(ERR, IPSEC_ESP, "invalid IP packet type %d\n",
262 /* Padded payload length */
263 pad_payload_len = RTE_ALIGN_CEIL(rte_pktmbuf_pkt_len(m) -
264 ip_hdr_len + 2, sa->block_size);
265 pad_len = pad_payload_len + ip_hdr_len - rte_pktmbuf_pkt_len(m);
267 RTE_ASSERT(sa->flags == IP4_TUNNEL || sa->flags == IP6_TUNNEL ||
268 sa->flags == TRANSPORT);
270 if (likely(sa->flags == IP4_TUNNEL))
271 ip_hdr_len = sizeof(struct ip);
272 else if (sa->flags == IP6_TUNNEL)
273 ip_hdr_len = sizeof(struct ip6_hdr);
274 else if (sa->flags != TRANSPORT) {
275 RTE_LOG(ERR, IPSEC_ESP, "Unsupported SA flags: 0x%x\n",
280 /* Check maximum packet size */
281 if (unlikely(ip_hdr_len + sizeof(struct esp_hdr) + sa->iv_len +
282 pad_payload_len + sa->digest_len > IP_MAXPACKET)) {
283 RTE_LOG(ERR, IPSEC_ESP, "ipsec packet is too big\n");
287 padding = (uint8_t *)rte_pktmbuf_append(m, pad_len + sa->digest_len);
288 if (unlikely(padding == NULL)) {
289 RTE_LOG(ERR, IPSEC_ESP, "not enough mbuf trailing space\n");
292 rte_prefetch0(padding);
296 ip4 = ip4ip_outbound(m, sizeof(struct esp_hdr) + sa->iv_len,
298 esp = (struct esp_hdr *)(ip4 + 1);
301 ip6 = ip6ip_outbound(m, sizeof(struct esp_hdr) + sa->iv_len,
303 esp = (struct esp_hdr *)(ip6 + 1);
306 new_ip = (uint8_t *)rte_pktmbuf_prepend(m,
307 sizeof(struct esp_hdr) + sa->iv_len);
308 memmove(new_ip, ip4, ip_hdr_len);
309 esp = (struct esp_hdr *)(new_ip + ip_hdr_len);
310 ip4 = (struct ip *)new_ip;
311 if (likely(ip4->ip_v == IPVERSION)) {
312 ip4->ip_p = IPPROTO_ESP;
313 ip4->ip_len = htons(rte_pktmbuf_data_len(m));
315 ip6 = (struct ip6_hdr *)new_ip;
316 ip6->ip6_nxt = IPPROTO_ESP;
317 ip6->ip6_plen = htons(rte_pktmbuf_data_len(m) -
318 sizeof(struct ip6_hdr));
323 esp->spi = rte_cpu_to_be_32(sa->spi);
324 esp->seq = rte_cpu_to_be_32((uint32_t)sa->seq);
326 uint64_t *iv = (uint64_t *)(esp + 1);
328 sym_cop = get_sym_cop(cop);
331 if (sa->aead_algo == RTE_CRYPTO_AEAD_AES_GCM) {
335 sym_cop->aead.data.offset = ip_hdr_len +
336 sizeof(struct esp_hdr) + sa->iv_len;
337 sym_cop->aead.data.length = pad_payload_len;
339 /* Fill pad_len using default sequential scheme */
340 for (i = 0; i < pad_len - 2; i++)
342 padding[pad_len - 2] = pad_len - 2;
343 padding[pad_len - 1] = nlp;
345 struct cnt_blk *icb = get_cnt_blk(m);
346 icb->salt = sa->salt;
348 icb->cnt = rte_cpu_to_be_32(1);
352 sym_cop->aead.aad.data = aad;
353 sym_cop->aead.aad.phys_addr = rte_pktmbuf_mtophys_offset(m,
354 aad - rte_pktmbuf_mtod(m, uint8_t *));
356 sym_cop->aead.digest.data = rte_pktmbuf_mtod_offset(m, uint8_t *,
357 rte_pktmbuf_pkt_len(m) - sa->digest_len);
358 sym_cop->aead.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
359 rte_pktmbuf_pkt_len(m) - sa->digest_len);
361 switch (sa->cipher_algo) {
362 case RTE_CRYPTO_CIPHER_NULL:
363 case RTE_CRYPTO_CIPHER_AES_CBC:
364 memset(iv, 0, sa->iv_len);
365 sym_cop->cipher.data.offset = ip_hdr_len +
366 sizeof(struct esp_hdr);
367 sym_cop->cipher.data.length = pad_payload_len + sa->iv_len;
369 case RTE_CRYPTO_CIPHER_AES_CTR:
371 sym_cop->cipher.data.offset = ip_hdr_len +
372 sizeof(struct esp_hdr) + sa->iv_len;
373 sym_cop->cipher.data.length = pad_payload_len;
376 RTE_LOG(ERR, IPSEC_ESP, "unsupported cipher algorithm %u\n",
381 /* Fill pad_len using default sequential scheme */
382 for (i = 0; i < pad_len - 2; i++)
384 padding[pad_len - 2] = pad_len - 2;
385 padding[pad_len - 1] = nlp;
387 struct cnt_blk *icb = get_cnt_blk(m);
388 icb->salt = sa->salt;
390 icb->cnt = rte_cpu_to_be_32(1);
392 switch (sa->auth_algo) {
393 case RTE_CRYPTO_AUTH_NULL:
394 case RTE_CRYPTO_AUTH_SHA1_HMAC:
395 case RTE_CRYPTO_AUTH_SHA256_HMAC:
396 sym_cop->auth.data.offset = ip_hdr_len;
397 sym_cop->auth.data.length = sizeof(struct esp_hdr) +
398 sa->iv_len + pad_payload_len;
401 RTE_LOG(ERR, IPSEC_ESP, "unsupported auth algorithm %u\n",
406 sym_cop->auth.digest.data = rte_pktmbuf_mtod_offset(m, uint8_t *,
407 rte_pktmbuf_pkt_len(m) - sa->digest_len);
408 sym_cop->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
409 rte_pktmbuf_pkt_len(m) - sa->digest_len);
416 esp_outbound_post(struct rte_mbuf *m __rte_unused,
417 struct ipsec_sa *sa __rte_unused,
418 struct rte_crypto_op *cop)
420 RTE_ASSERT(m != NULL);
421 RTE_ASSERT(sa != NULL);
422 RTE_ASSERT(cop != NULL);
424 if (cop->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
425 RTE_LOG(ERR, IPSEC_ESP, "Failed crypto op\n");