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 sym_cop->cipher.data.offset = ip_hdr_len + sizeof(struct esp_hdr) +
91 sym_cop->cipher.data.length = payload_len;
95 uint8_t *iv = RTE_PTR_ADD(ip4, ip_hdr_len + sizeof(struct esp_hdr));
96 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(cop,
97 uint8_t *, IV_OFFSET);
99 switch (sa->cipher_algo) {
100 case RTE_CRYPTO_CIPHER_NULL:
101 case RTE_CRYPTO_CIPHER_AES_CBC:
102 /* Copy IV at the end of crypto operation */
103 rte_memcpy(iv_ptr, iv, sa->iv_len);
105 case RTE_CRYPTO_CIPHER_AES_CTR:
106 case RTE_CRYPTO_CIPHER_AES_GCM:
107 icb = get_cnt_blk(m);
108 icb->salt = sa->salt;
109 memcpy(&icb->iv, iv, 8);
110 icb->cnt = rte_cpu_to_be_32(1);
113 RTE_LOG(ERR, IPSEC_ESP, "unsupported cipher algorithm %u\n",
118 switch (sa->auth_algo) {
119 case RTE_CRYPTO_AUTH_NULL:
120 case RTE_CRYPTO_AUTH_SHA1_HMAC:
121 case RTE_CRYPTO_AUTH_SHA256_HMAC:
122 sym_cop->auth.data.offset = ip_hdr_len;
123 sym_cop->auth.data.length = sizeof(struct esp_hdr) +
124 sa->iv_len + payload_len;
126 case RTE_CRYPTO_AUTH_AES_GCM:
128 memcpy(aad, iv - sizeof(struct esp_hdr), 8);
129 sym_cop->auth.aad.data = aad;
130 sym_cop->auth.aad.phys_addr = rte_pktmbuf_mtophys_offset(m,
131 aad - rte_pktmbuf_mtod(m, uint8_t *));
134 RTE_LOG(ERR, IPSEC_ESP, "unsupported auth algorithm %u\n",
139 sym_cop->auth.digest.data = rte_pktmbuf_mtod_offset(m, void*,
140 rte_pktmbuf_pkt_len(m) - sa->digest_len);
141 sym_cop->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
142 rte_pktmbuf_pkt_len(m) - sa->digest_len);
143 sym_cop->auth.digest.length = sa->digest_len;
149 esp_inbound_post(struct rte_mbuf *m, struct ipsec_sa *sa,
150 struct rte_crypto_op *cop)
154 uint8_t *nexthdr, *pad_len;
158 RTE_ASSERT(m != NULL);
159 RTE_ASSERT(sa != NULL);
160 RTE_ASSERT(cop != NULL);
162 if (cop->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
163 RTE_LOG(ERR, IPSEC_ESP, "failed crypto op\n");
167 nexthdr = rte_pktmbuf_mtod_offset(m, uint8_t*,
168 rte_pktmbuf_pkt_len(m) - sa->digest_len - 1);
169 pad_len = nexthdr - 1;
171 padding = pad_len - *pad_len;
172 for (i = 0; i < *pad_len; i++) {
173 if (padding[i] != i + 1) {
174 RTE_LOG(ERR, IPSEC_ESP, "invalid padding\n");
179 if (rte_pktmbuf_trim(m, *pad_len + 2 + sa->digest_len)) {
180 RTE_LOG(ERR, IPSEC_ESP,
181 "failed to remove pad_len + digest\n");
185 if (unlikely(sa->flags == TRANSPORT)) {
186 ip = rte_pktmbuf_mtod(m, struct ip *);
187 ip4 = (struct ip *)rte_pktmbuf_adj(m,
188 sizeof(struct esp_hdr) + sa->iv_len);
189 if (likely(ip->ip_v == IPVERSION)) {
190 memmove(ip4, ip, ip->ip_hl * 4);
191 ip4->ip_p = *nexthdr;
192 ip4->ip_len = htons(rte_pktmbuf_data_len(m));
194 ip6 = (struct ip6_hdr *)ip4;
195 /* XXX No option headers supported */
196 memmove(ip6, ip, sizeof(struct ip6_hdr));
197 ip6->ip6_nxt = *nexthdr;
198 ip6->ip6_plen = htons(rte_pktmbuf_data_len(m));
201 ipip_inbound(m, sizeof(struct esp_hdr) + sa->iv_len);
207 esp_outbound(struct rte_mbuf *m, struct ipsec_sa *sa,
208 struct rte_crypto_op *cop)
212 struct esp_hdr *esp = NULL;
213 uint8_t *padding, *new_ip, nlp;
214 struct rte_crypto_sym_op *sym_cop;
216 uint16_t pad_payload_len, pad_len, ip_hdr_len;
218 RTE_ASSERT(m != NULL);
219 RTE_ASSERT(sa != NULL);
220 RTE_ASSERT(cop != NULL);
224 ip4 = rte_pktmbuf_mtod(m, struct ip *);
225 if (likely(ip4->ip_v == IPVERSION)) {
226 if (unlikely(sa->flags == TRANSPORT)) {
227 ip_hdr_len = ip4->ip_hl * 4;
231 } else if (ip4->ip_v == IP6_VERSION) {
232 if (unlikely(sa->flags == TRANSPORT)) {
233 /* XXX No option headers supported */
234 ip_hdr_len = sizeof(struct ip6_hdr);
235 ip6 = (struct ip6_hdr *)ip4;
240 RTE_LOG(ERR, IPSEC_ESP, "invalid IP packet type %d\n",
245 /* Padded payload length */
246 pad_payload_len = RTE_ALIGN_CEIL(rte_pktmbuf_pkt_len(m) -
247 ip_hdr_len + 2, sa->block_size);
248 pad_len = pad_payload_len + ip_hdr_len - rte_pktmbuf_pkt_len(m);
250 RTE_ASSERT(sa->flags == IP4_TUNNEL || sa->flags == IP6_TUNNEL ||
251 sa->flags == TRANSPORT);
253 if (likely(sa->flags == IP4_TUNNEL))
254 ip_hdr_len = sizeof(struct ip);
255 else if (sa->flags == IP6_TUNNEL)
256 ip_hdr_len = sizeof(struct ip6_hdr);
257 else if (sa->flags != TRANSPORT) {
258 RTE_LOG(ERR, IPSEC_ESP, "Unsupported SA flags: 0x%x\n",
263 /* Check maximum packet size */
264 if (unlikely(ip_hdr_len + sizeof(struct esp_hdr) + sa->iv_len +
265 pad_payload_len + sa->digest_len > IP_MAXPACKET)) {
266 RTE_LOG(ERR, IPSEC_ESP, "ipsec packet is too big\n");
270 padding = (uint8_t *)rte_pktmbuf_append(m, pad_len + sa->digest_len);
271 if (unlikely(padding == NULL)) {
272 RTE_LOG(ERR, IPSEC_ESP, "not enough mbuf trailing space\n");
275 rte_prefetch0(padding);
279 ip4 = ip4ip_outbound(m, sizeof(struct esp_hdr) + sa->iv_len,
281 esp = (struct esp_hdr *)(ip4 + 1);
284 ip6 = ip6ip_outbound(m, sizeof(struct esp_hdr) + sa->iv_len,
286 esp = (struct esp_hdr *)(ip6 + 1);
289 new_ip = (uint8_t *)rte_pktmbuf_prepend(m,
290 sizeof(struct esp_hdr) + sa->iv_len);
291 memmove(new_ip, ip4, ip_hdr_len);
292 esp = (struct esp_hdr *)(new_ip + ip_hdr_len);
293 if (likely(ip4->ip_v == IPVERSION)) {
294 ip4 = (struct ip *)new_ip;
295 ip4->ip_p = IPPROTO_ESP;
296 ip4->ip_len = htons(rte_pktmbuf_data_len(m));
298 ip6 = (struct ip6_hdr *)new_ip;
299 ip6->ip6_nxt = IPPROTO_ESP;
300 ip6->ip6_plen = htons(rte_pktmbuf_data_len(m));
305 esp->spi = rte_cpu_to_be_32(sa->spi);
306 esp->seq = rte_cpu_to_be_32((uint32_t)sa->seq);
308 uint64_t *iv = (uint64_t *)(esp + 1);
310 sym_cop = get_sym_cop(cop);
312 switch (sa->cipher_algo) {
313 case RTE_CRYPTO_CIPHER_NULL:
314 case RTE_CRYPTO_CIPHER_AES_CBC:
315 memset(iv, 0, sa->iv_len);
316 sym_cop->cipher.data.offset = ip_hdr_len +
317 sizeof(struct esp_hdr);
318 sym_cop->cipher.data.length = pad_payload_len + sa->iv_len;
320 case RTE_CRYPTO_CIPHER_AES_CTR:
321 case RTE_CRYPTO_CIPHER_AES_GCM:
323 sym_cop->cipher.data.offset = ip_hdr_len +
324 sizeof(struct esp_hdr) + sa->iv_len;
325 sym_cop->cipher.data.length = pad_payload_len;
328 RTE_LOG(ERR, IPSEC_ESP, "unsupported cipher algorithm %u\n",
333 /* Fill pad_len using default sequential scheme */
334 for (i = 0; i < pad_len - 2; i++)
336 padding[pad_len - 2] = pad_len - 2;
337 padding[pad_len - 1] = nlp;
339 struct cnt_blk *icb = get_cnt_blk(m);
340 icb->salt = sa->salt;
342 icb->cnt = rte_cpu_to_be_32(1);
346 switch (sa->auth_algo) {
347 case RTE_CRYPTO_AUTH_NULL:
348 case RTE_CRYPTO_AUTH_SHA1_HMAC:
349 case RTE_CRYPTO_AUTH_SHA256_HMAC:
350 sym_cop->auth.data.offset = ip_hdr_len;
351 sym_cop->auth.data.length = sizeof(struct esp_hdr) +
352 sa->iv_len + pad_payload_len;
354 case RTE_CRYPTO_AUTH_AES_GCM:
357 sym_cop->auth.aad.data = aad;
358 sym_cop->auth.aad.phys_addr = rte_pktmbuf_mtophys_offset(m,
359 aad - rte_pktmbuf_mtod(m, uint8_t *));
362 RTE_LOG(ERR, IPSEC_ESP, "unsupported auth algorithm %u\n",
367 sym_cop->auth.digest.data = rte_pktmbuf_mtod_offset(m, uint8_t *,
368 rte_pktmbuf_pkt_len(m) - sa->digest_len);
369 sym_cop->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
370 rte_pktmbuf_pkt_len(m) - sa->digest_len);
371 sym_cop->auth.digest.length = sa->digest_len;
377 esp_outbound_post(struct rte_mbuf *m __rte_unused,
378 struct ipsec_sa *sa __rte_unused,
379 struct rte_crypto_op *cop)
381 RTE_ASSERT(m != NULL);
382 RTE_ASSERT(sa != NULL);
383 RTE_ASSERT(cop != NULL);
385 if (cop->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
386 RTE_LOG(ERR, IPSEC_ESP, "Failed crypto op\n");