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));
217 ipip_inbound(m, sizeof(struct esp_hdr) + sa->iv_len);
223 esp_outbound(struct rte_mbuf *m, struct ipsec_sa *sa,
224 struct rte_crypto_op *cop)
228 struct esp_hdr *esp = NULL;
229 uint8_t *padding, *new_ip, nlp;
230 struct rte_crypto_sym_op *sym_cop;
232 uint16_t pad_payload_len, pad_len, ip_hdr_len;
234 RTE_ASSERT(m != NULL);
235 RTE_ASSERT(sa != NULL);
236 RTE_ASSERT(cop != NULL);
240 ip4 = rte_pktmbuf_mtod(m, struct ip *);
241 if (likely(ip4->ip_v == IPVERSION)) {
242 if (unlikely(sa->flags == TRANSPORT)) {
243 ip_hdr_len = ip4->ip_hl * 4;
247 } else if (ip4->ip_v == IP6_VERSION) {
248 if (unlikely(sa->flags == TRANSPORT)) {
249 /* XXX No option headers supported */
250 ip_hdr_len = sizeof(struct ip6_hdr);
251 ip6 = (struct ip6_hdr *)ip4;
256 RTE_LOG(ERR, IPSEC_ESP, "invalid IP packet type %d\n",
261 /* Padded payload length */
262 pad_payload_len = RTE_ALIGN_CEIL(rte_pktmbuf_pkt_len(m) -
263 ip_hdr_len + 2, sa->block_size);
264 pad_len = pad_payload_len + ip_hdr_len - rte_pktmbuf_pkt_len(m);
266 RTE_ASSERT(sa->flags == IP4_TUNNEL || sa->flags == IP6_TUNNEL ||
267 sa->flags == TRANSPORT);
269 if (likely(sa->flags == IP4_TUNNEL))
270 ip_hdr_len = sizeof(struct ip);
271 else if (sa->flags == IP6_TUNNEL)
272 ip_hdr_len = sizeof(struct ip6_hdr);
273 else if (sa->flags != TRANSPORT) {
274 RTE_LOG(ERR, IPSEC_ESP, "Unsupported SA flags: 0x%x\n",
279 /* Check maximum packet size */
280 if (unlikely(ip_hdr_len + sizeof(struct esp_hdr) + sa->iv_len +
281 pad_payload_len + sa->digest_len > IP_MAXPACKET)) {
282 RTE_LOG(ERR, IPSEC_ESP, "ipsec packet is too big\n");
286 padding = (uint8_t *)rte_pktmbuf_append(m, pad_len + sa->digest_len);
287 if (unlikely(padding == NULL)) {
288 RTE_LOG(ERR, IPSEC_ESP, "not enough mbuf trailing space\n");
291 rte_prefetch0(padding);
295 ip4 = ip4ip_outbound(m, sizeof(struct esp_hdr) + sa->iv_len,
297 esp = (struct esp_hdr *)(ip4 + 1);
300 ip6 = ip6ip_outbound(m, sizeof(struct esp_hdr) + sa->iv_len,
302 esp = (struct esp_hdr *)(ip6 + 1);
305 new_ip = (uint8_t *)rte_pktmbuf_prepend(m,
306 sizeof(struct esp_hdr) + sa->iv_len);
307 memmove(new_ip, ip4, ip_hdr_len);
308 esp = (struct esp_hdr *)(new_ip + ip_hdr_len);
309 if (likely(ip4->ip_v == IPVERSION)) {
310 ip4 = (struct ip *)new_ip;
311 ip4->ip_p = IPPROTO_ESP;
312 ip4->ip_len = htons(rte_pktmbuf_data_len(m));
314 ip6 = (struct ip6_hdr *)new_ip;
315 ip6->ip6_nxt = IPPROTO_ESP;
316 ip6->ip6_plen = htons(rte_pktmbuf_data_len(m));
321 esp->spi = rte_cpu_to_be_32(sa->spi);
322 esp->seq = rte_cpu_to_be_32((uint32_t)sa->seq);
324 uint64_t *iv = (uint64_t *)(esp + 1);
326 sym_cop = get_sym_cop(cop);
329 if (sa->aead_algo == RTE_CRYPTO_AEAD_AES_GCM) {
333 sym_cop->aead.data.offset = ip_hdr_len +
334 sizeof(struct esp_hdr) + sa->iv_len;
335 sym_cop->aead.data.length = pad_payload_len;
337 /* Fill pad_len using default sequential scheme */
338 for (i = 0; i < pad_len - 2; i++)
340 padding[pad_len - 2] = pad_len - 2;
341 padding[pad_len - 1] = nlp;
343 struct cnt_blk *icb = get_cnt_blk(m);
344 icb->salt = sa->salt;
346 icb->cnt = rte_cpu_to_be_32(1);
350 sym_cop->aead.aad.data = aad;
351 sym_cop->aead.aad.phys_addr = rte_pktmbuf_mtophys_offset(m,
352 aad - rte_pktmbuf_mtod(m, uint8_t *));
354 sym_cop->aead.digest.data = rte_pktmbuf_mtod_offset(m, uint8_t *,
355 rte_pktmbuf_pkt_len(m) - sa->digest_len);
356 sym_cop->aead.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
357 rte_pktmbuf_pkt_len(m) - sa->digest_len);
359 switch (sa->cipher_algo) {
360 case RTE_CRYPTO_CIPHER_NULL:
361 case RTE_CRYPTO_CIPHER_AES_CBC:
362 memset(iv, 0, sa->iv_len);
363 sym_cop->cipher.data.offset = ip_hdr_len +
364 sizeof(struct esp_hdr);
365 sym_cop->cipher.data.length = pad_payload_len + sa->iv_len;
367 case RTE_CRYPTO_CIPHER_AES_CTR:
369 sym_cop->cipher.data.offset = ip_hdr_len +
370 sizeof(struct esp_hdr) + sa->iv_len;
371 sym_cop->cipher.data.length = pad_payload_len;
374 RTE_LOG(ERR, IPSEC_ESP, "unsupported cipher algorithm %u\n",
379 /* Fill pad_len using default sequential scheme */
380 for (i = 0; i < pad_len - 2; i++)
382 padding[pad_len - 2] = pad_len - 2;
383 padding[pad_len - 1] = nlp;
385 struct cnt_blk *icb = get_cnt_blk(m);
386 icb->salt = sa->salt;
388 icb->cnt = rte_cpu_to_be_32(1);
390 switch (sa->auth_algo) {
391 case RTE_CRYPTO_AUTH_NULL:
392 case RTE_CRYPTO_AUTH_SHA1_HMAC:
393 case RTE_CRYPTO_AUTH_SHA256_HMAC:
394 sym_cop->auth.data.offset = ip_hdr_len;
395 sym_cop->auth.data.length = sizeof(struct esp_hdr) +
396 sa->iv_len + pad_payload_len;
399 RTE_LOG(ERR, IPSEC_ESP, "unsupported auth algorithm %u\n",
404 sym_cop->auth.digest.data = rte_pktmbuf_mtod_offset(m, uint8_t *,
405 rte_pktmbuf_pkt_len(m) - sa->digest_len);
406 sym_cop->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
407 rte_pktmbuf_pkt_len(m) - sa->digest_len);
414 esp_outbound_post(struct rte_mbuf *m __rte_unused,
415 struct ipsec_sa *sa __rte_unused,
416 struct rte_crypto_op *cop)
418 RTE_ASSERT(m != NULL);
419 RTE_ASSERT(sa != NULL);
420 RTE_ASSERT(cop != NULL);
422 if (cop->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
423 RTE_LOG(ERR, IPSEC_ESP, "Failed crypto op\n");