4 * Copyright(c) 2016 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));
97 switch (sa->cipher_algo) {
98 case RTE_CRYPTO_CIPHER_NULL:
99 case RTE_CRYPTO_CIPHER_AES_CBC:
100 sym_cop->cipher.iv.data = iv;
101 sym_cop->cipher.iv.phys_addr = rte_pktmbuf_mtophys_offset(m,
102 ip_hdr_len + sizeof(struct esp_hdr));
103 sym_cop->cipher.iv.length = 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);
111 sym_cop->cipher.iv.data = (uint8_t *)icb;
112 sym_cop->cipher.iv.phys_addr = rte_pktmbuf_mtophys_offset(m,
113 (uint8_t *)icb - rte_pktmbuf_mtod(m, uint8_t *));
114 sym_cop->cipher.iv.length = 16;
117 RTE_LOG(ERR, IPSEC_ESP, "unsupported cipher algorithm %u\n",
122 switch (sa->auth_algo) {
123 case RTE_CRYPTO_AUTH_NULL:
124 case RTE_CRYPTO_AUTH_SHA1_HMAC:
125 case RTE_CRYPTO_AUTH_SHA256_HMAC:
126 sym_cop->auth.data.offset = ip_hdr_len;
127 sym_cop->auth.data.length = sizeof(struct esp_hdr) +
128 sa->iv_len + payload_len;
130 case RTE_CRYPTO_AUTH_AES_GCM:
132 memcpy(aad, iv - sizeof(struct esp_hdr), 8);
133 sym_cop->auth.aad.data = aad;
134 sym_cop->auth.aad.phys_addr = rte_pktmbuf_mtophys_offset(m,
135 aad - rte_pktmbuf_mtod(m, uint8_t *));
136 sym_cop->auth.aad.length = 8;
139 RTE_LOG(ERR, IPSEC_ESP, "unsupported auth algorithm %u\n",
144 sym_cop->auth.digest.data = rte_pktmbuf_mtod_offset(m, void*,
145 rte_pktmbuf_pkt_len(m) - sa->digest_len);
146 sym_cop->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
147 rte_pktmbuf_pkt_len(m) - sa->digest_len);
148 sym_cop->auth.digest.length = sa->digest_len;
154 esp_inbound_post(struct rte_mbuf *m, struct ipsec_sa *sa,
155 struct rte_crypto_op *cop)
159 uint8_t *nexthdr, *pad_len;
163 RTE_ASSERT(m != NULL);
164 RTE_ASSERT(sa != NULL);
165 RTE_ASSERT(cop != NULL);
167 if (cop->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
168 RTE_LOG(ERR, IPSEC_ESP, "failed crypto op\n");
172 nexthdr = rte_pktmbuf_mtod_offset(m, uint8_t*,
173 rte_pktmbuf_pkt_len(m) - sa->digest_len - 1);
174 pad_len = nexthdr - 1;
176 padding = pad_len - *pad_len;
177 for (i = 0; i < *pad_len; i++) {
178 if (padding[i] != i + 1) {
179 RTE_LOG(ERR, IPSEC_ESP, "invalid padding\n");
184 if (rte_pktmbuf_trim(m, *pad_len + 2 + sa->digest_len)) {
185 RTE_LOG(ERR, IPSEC_ESP,
186 "failed to remove pad_len + digest\n");
190 if (unlikely(sa->flags == TRANSPORT)) {
191 ip = rte_pktmbuf_mtod(m, struct ip *);
192 ip4 = (struct ip *)rte_pktmbuf_adj(m,
193 sizeof(struct esp_hdr) + sa->iv_len);
194 if (likely(ip->ip_v == IPVERSION)) {
195 memmove(ip4, ip, ip->ip_hl * 4);
196 ip4->ip_p = *nexthdr;
197 ip4->ip_len = htons(rte_pktmbuf_data_len(m));
199 ip6 = (struct ip6_hdr *)ip4;
200 /* XXX No option headers supported */
201 memmove(ip6, ip, sizeof(struct ip6_hdr));
202 ip6->ip6_nxt = *nexthdr;
203 ip6->ip6_plen = htons(rte_pktmbuf_data_len(m));
206 ipip_inbound(m, sizeof(struct esp_hdr) + sa->iv_len);
212 esp_outbound(struct rte_mbuf *m, struct ipsec_sa *sa,
213 struct rte_crypto_op *cop)
217 struct esp_hdr *esp = NULL;
218 uint8_t *padding, *new_ip, nlp;
219 struct rte_crypto_sym_op *sym_cop;
221 uint16_t pad_payload_len, pad_len, ip_hdr_len;
223 RTE_ASSERT(m != NULL);
224 RTE_ASSERT(sa != NULL);
225 RTE_ASSERT(cop != NULL);
229 ip4 = rte_pktmbuf_mtod(m, struct ip *);
230 if (likely(ip4->ip_v == IPVERSION)) {
231 if (unlikely(sa->flags == TRANSPORT)) {
232 ip_hdr_len = ip4->ip_hl * 4;
236 } else if (ip4->ip_v == IP6_VERSION) {
237 if (unlikely(sa->flags == TRANSPORT)) {
238 /* XXX No option headers supported */
239 ip_hdr_len = sizeof(struct ip6_hdr);
240 ip6 = (struct ip6_hdr *)ip4;
245 RTE_LOG(ERR, IPSEC_ESP, "invalid IP packet type %d\n",
250 /* Padded payload length */
251 pad_payload_len = RTE_ALIGN_CEIL(rte_pktmbuf_pkt_len(m) -
252 ip_hdr_len + 2, sa->block_size);
253 pad_len = pad_payload_len + ip_hdr_len - rte_pktmbuf_pkt_len(m);
255 RTE_ASSERT(sa->flags == IP4_TUNNEL || sa->flags == IP6_TUNNEL ||
256 sa->flags == TRANSPORT);
258 if (likely(sa->flags == IP4_TUNNEL))
259 ip_hdr_len = sizeof(struct ip);
260 else if (sa->flags == IP6_TUNNEL)
261 ip_hdr_len = sizeof(struct ip6_hdr);
262 else if (sa->flags != TRANSPORT) {
263 RTE_LOG(ERR, IPSEC_ESP, "Unsupported SA flags: 0x%x\n",
268 /* Check maximum packet size */
269 if (unlikely(ip_hdr_len + sizeof(struct esp_hdr) + sa->iv_len +
270 pad_payload_len + sa->digest_len > IP_MAXPACKET)) {
271 RTE_LOG(ERR, IPSEC_ESP, "ipsec packet is too big\n");
275 padding = (uint8_t *)rte_pktmbuf_append(m, pad_len + sa->digest_len);
276 if (unlikely(padding == NULL)) {
277 RTE_LOG(ERR, IPSEC_ESP, "not enough mbuf trailing space\n");
280 rte_prefetch0(padding);
284 ip4 = ip4ip_outbound(m, sizeof(struct esp_hdr) + sa->iv_len,
286 esp = (struct esp_hdr *)(ip4 + 1);
289 ip6 = ip6ip_outbound(m, sizeof(struct esp_hdr) + sa->iv_len,
291 esp = (struct esp_hdr *)(ip6 + 1);
294 new_ip = (uint8_t *)rte_pktmbuf_prepend(m,
295 sizeof(struct esp_hdr) + sa->iv_len);
296 memmove(new_ip, ip4, ip_hdr_len);
297 esp = (struct esp_hdr *)(new_ip + ip_hdr_len);
298 if (likely(ip4->ip_v == IPVERSION)) {
299 ip4 = (struct ip *)new_ip;
300 ip4->ip_p = IPPROTO_ESP;
301 ip4->ip_len = htons(rte_pktmbuf_data_len(m));
303 ip6 = (struct ip6_hdr *)new_ip;
304 ip6->ip6_nxt = IPPROTO_ESP;
305 ip6->ip6_plen = htons(rte_pktmbuf_data_len(m));
310 esp->spi = rte_cpu_to_be_32(sa->spi);
311 esp->seq = rte_cpu_to_be_32((uint32_t)sa->seq);
313 uint64_t *iv = (uint64_t *)(esp + 1);
315 sym_cop = get_sym_cop(cop);
317 switch (sa->cipher_algo) {
318 case RTE_CRYPTO_CIPHER_NULL:
319 case RTE_CRYPTO_CIPHER_AES_CBC:
320 memset(iv, 0, sa->iv_len);
321 sym_cop->cipher.data.offset = ip_hdr_len +
322 sizeof(struct esp_hdr);
323 sym_cop->cipher.data.length = pad_payload_len + sa->iv_len;
325 case RTE_CRYPTO_CIPHER_AES_CTR:
326 case RTE_CRYPTO_CIPHER_AES_GCM:
328 sym_cop->cipher.data.offset = ip_hdr_len +
329 sizeof(struct esp_hdr) + sa->iv_len;
330 sym_cop->cipher.data.length = pad_payload_len;
333 RTE_LOG(ERR, IPSEC_ESP, "unsupported cipher algorithm %u\n",
338 /* Fill pad_len using default sequential scheme */
339 for (i = 0; i < pad_len - 2; i++)
341 padding[pad_len - 2] = pad_len - 2;
342 padding[pad_len - 1] = nlp;
344 struct cnt_blk *icb = get_cnt_blk(m);
345 icb->salt = sa->salt;
347 icb->cnt = rte_cpu_to_be_32(1);
348 sym_cop->cipher.iv.data = (uint8_t *)icb;
349 sym_cop->cipher.iv.phys_addr = rte_pktmbuf_mtophys_offset(m,
350 (uint8_t *)icb - rte_pktmbuf_mtod(m, uint8_t *));
351 sym_cop->cipher.iv.length = 16;
355 switch (sa->auth_algo) {
356 case RTE_CRYPTO_AUTH_NULL:
357 case RTE_CRYPTO_AUTH_SHA1_HMAC:
358 case RTE_CRYPTO_AUTH_SHA256_HMAC:
359 sym_cop->auth.data.offset = ip_hdr_len;
360 sym_cop->auth.data.length = sizeof(struct esp_hdr) +
361 sa->iv_len + pad_payload_len;
363 case RTE_CRYPTO_AUTH_AES_GCM:
366 sym_cop->auth.aad.data = aad;
367 sym_cop->auth.aad.phys_addr = rte_pktmbuf_mtophys_offset(m,
368 aad - rte_pktmbuf_mtod(m, uint8_t *));
369 sym_cop->auth.aad.length = 8;
372 RTE_LOG(ERR, IPSEC_ESP, "unsupported auth algorithm %u\n",
377 sym_cop->auth.digest.data = rte_pktmbuf_mtod_offset(m, uint8_t *,
378 rte_pktmbuf_pkt_len(m) - sa->digest_len);
379 sym_cop->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
380 rte_pktmbuf_pkt_len(m) - sa->digest_len);
381 sym_cop->auth.digest.length = sa->digest_len;
387 esp_outbound_post(struct rte_mbuf *m __rte_unused,
388 struct ipsec_sa *sa __rte_unused,
389 struct rte_crypto_op *cop)
391 RTE_ASSERT(m != NULL);
392 RTE_ASSERT(sa != NULL);
393 RTE_ASSERT(cop != NULL);
395 if (cop->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
396 RTE_LOG(ERR, IPSEC_ESP, "Failed crypto op\n");