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);
148 esp_inbound_post(struct rte_mbuf *m, struct ipsec_sa *sa,
149 struct rte_crypto_op *cop)
153 uint8_t *nexthdr, *pad_len;
157 RTE_ASSERT(m != NULL);
158 RTE_ASSERT(sa != NULL);
159 RTE_ASSERT(cop != NULL);
161 if (cop->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
162 RTE_LOG(ERR, IPSEC_ESP, "failed crypto op\n");
166 nexthdr = rte_pktmbuf_mtod_offset(m, uint8_t*,
167 rte_pktmbuf_pkt_len(m) - sa->digest_len - 1);
168 pad_len = nexthdr - 1;
170 padding = pad_len - *pad_len;
171 for (i = 0; i < *pad_len; i++) {
172 if (padding[i] != i + 1) {
173 RTE_LOG(ERR, IPSEC_ESP, "invalid padding\n");
178 if (rte_pktmbuf_trim(m, *pad_len + 2 + sa->digest_len)) {
179 RTE_LOG(ERR, IPSEC_ESP,
180 "failed to remove pad_len + digest\n");
184 if (unlikely(sa->flags == TRANSPORT)) {
185 ip = rte_pktmbuf_mtod(m, struct ip *);
186 ip4 = (struct ip *)rte_pktmbuf_adj(m,
187 sizeof(struct esp_hdr) + sa->iv_len);
188 if (likely(ip->ip_v == IPVERSION)) {
189 memmove(ip4, ip, ip->ip_hl * 4);
190 ip4->ip_p = *nexthdr;
191 ip4->ip_len = htons(rte_pktmbuf_data_len(m));
193 ip6 = (struct ip6_hdr *)ip4;
194 /* XXX No option headers supported */
195 memmove(ip6, ip, sizeof(struct ip6_hdr));
196 ip6->ip6_nxt = *nexthdr;
197 ip6->ip6_plen = htons(rte_pktmbuf_data_len(m));
200 ipip_inbound(m, sizeof(struct esp_hdr) + sa->iv_len);
206 esp_outbound(struct rte_mbuf *m, struct ipsec_sa *sa,
207 struct rte_crypto_op *cop)
211 struct esp_hdr *esp = NULL;
212 uint8_t *padding, *new_ip, nlp;
213 struct rte_crypto_sym_op *sym_cop;
215 uint16_t pad_payload_len, pad_len, ip_hdr_len;
217 RTE_ASSERT(m != NULL);
218 RTE_ASSERT(sa != NULL);
219 RTE_ASSERT(cop != NULL);
223 ip4 = rte_pktmbuf_mtod(m, struct ip *);
224 if (likely(ip4->ip_v == IPVERSION)) {
225 if (unlikely(sa->flags == TRANSPORT)) {
226 ip_hdr_len = ip4->ip_hl * 4;
230 } else if (ip4->ip_v == IP6_VERSION) {
231 if (unlikely(sa->flags == TRANSPORT)) {
232 /* XXX No option headers supported */
233 ip_hdr_len = sizeof(struct ip6_hdr);
234 ip6 = (struct ip6_hdr *)ip4;
239 RTE_LOG(ERR, IPSEC_ESP, "invalid IP packet type %d\n",
244 /* Padded payload length */
245 pad_payload_len = RTE_ALIGN_CEIL(rte_pktmbuf_pkt_len(m) -
246 ip_hdr_len + 2, sa->block_size);
247 pad_len = pad_payload_len + ip_hdr_len - rte_pktmbuf_pkt_len(m);
249 RTE_ASSERT(sa->flags == IP4_TUNNEL || sa->flags == IP6_TUNNEL ||
250 sa->flags == TRANSPORT);
252 if (likely(sa->flags == IP4_TUNNEL))
253 ip_hdr_len = sizeof(struct ip);
254 else if (sa->flags == IP6_TUNNEL)
255 ip_hdr_len = sizeof(struct ip6_hdr);
256 else if (sa->flags != TRANSPORT) {
257 RTE_LOG(ERR, IPSEC_ESP, "Unsupported SA flags: 0x%x\n",
262 /* Check maximum packet size */
263 if (unlikely(ip_hdr_len + sizeof(struct esp_hdr) + sa->iv_len +
264 pad_payload_len + sa->digest_len > IP_MAXPACKET)) {
265 RTE_LOG(ERR, IPSEC_ESP, "ipsec packet is too big\n");
269 padding = (uint8_t *)rte_pktmbuf_append(m, pad_len + sa->digest_len);
270 if (unlikely(padding == NULL)) {
271 RTE_LOG(ERR, IPSEC_ESP, "not enough mbuf trailing space\n");
274 rte_prefetch0(padding);
278 ip4 = ip4ip_outbound(m, sizeof(struct esp_hdr) + sa->iv_len,
280 esp = (struct esp_hdr *)(ip4 + 1);
283 ip6 = ip6ip_outbound(m, sizeof(struct esp_hdr) + sa->iv_len,
285 esp = (struct esp_hdr *)(ip6 + 1);
288 new_ip = (uint8_t *)rte_pktmbuf_prepend(m,
289 sizeof(struct esp_hdr) + sa->iv_len);
290 memmove(new_ip, ip4, ip_hdr_len);
291 esp = (struct esp_hdr *)(new_ip + ip_hdr_len);
292 if (likely(ip4->ip_v == IPVERSION)) {
293 ip4 = (struct ip *)new_ip;
294 ip4->ip_p = IPPROTO_ESP;
295 ip4->ip_len = htons(rte_pktmbuf_data_len(m));
297 ip6 = (struct ip6_hdr *)new_ip;
298 ip6->ip6_nxt = IPPROTO_ESP;
299 ip6->ip6_plen = htons(rte_pktmbuf_data_len(m));
304 esp->spi = rte_cpu_to_be_32(sa->spi);
305 esp->seq = rte_cpu_to_be_32((uint32_t)sa->seq);
307 uint64_t *iv = (uint64_t *)(esp + 1);
309 sym_cop = get_sym_cop(cop);
311 switch (sa->cipher_algo) {
312 case RTE_CRYPTO_CIPHER_NULL:
313 case RTE_CRYPTO_CIPHER_AES_CBC:
314 memset(iv, 0, sa->iv_len);
315 sym_cop->cipher.data.offset = ip_hdr_len +
316 sizeof(struct esp_hdr);
317 sym_cop->cipher.data.length = pad_payload_len + sa->iv_len;
319 case RTE_CRYPTO_CIPHER_AES_CTR:
320 case RTE_CRYPTO_CIPHER_AES_GCM:
322 sym_cop->cipher.data.offset = ip_hdr_len +
323 sizeof(struct esp_hdr) + sa->iv_len;
324 sym_cop->cipher.data.length = pad_payload_len;
327 RTE_LOG(ERR, IPSEC_ESP, "unsupported cipher algorithm %u\n",
332 /* Fill pad_len using default sequential scheme */
333 for (i = 0; i < pad_len - 2; i++)
335 padding[pad_len - 2] = pad_len - 2;
336 padding[pad_len - 1] = nlp;
338 struct cnt_blk *icb = get_cnt_blk(m);
339 icb->salt = sa->salt;
341 icb->cnt = rte_cpu_to_be_32(1);
345 switch (sa->auth_algo) {
346 case RTE_CRYPTO_AUTH_NULL:
347 case RTE_CRYPTO_AUTH_SHA1_HMAC:
348 case RTE_CRYPTO_AUTH_SHA256_HMAC:
349 sym_cop->auth.data.offset = ip_hdr_len;
350 sym_cop->auth.data.length = sizeof(struct esp_hdr) +
351 sa->iv_len + pad_payload_len;
353 case RTE_CRYPTO_AUTH_AES_GCM:
356 sym_cop->auth.aad.data = aad;
357 sym_cop->auth.aad.phys_addr = rte_pktmbuf_mtophys_offset(m,
358 aad - rte_pktmbuf_mtod(m, uint8_t *));
361 RTE_LOG(ERR, IPSEC_ESP, "unsupported auth algorithm %u\n",
366 sym_cop->auth.digest.data = rte_pktmbuf_mtod_offset(m, uint8_t *,
367 rte_pktmbuf_pkt_len(m) - sa->digest_len);
368 sym_cop->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
369 rte_pktmbuf_pkt_len(m) - sa->digest_len);
375 esp_outbound_post(struct rte_mbuf *m __rte_unused,
376 struct ipsec_sa *sa __rte_unused,
377 struct rte_crypto_op *cop)
379 RTE_ASSERT(m != NULL);
380 RTE_ASSERT(sa != NULL);
381 RTE_ASSERT(cop != NULL);
383 if (cop->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
384 RTE_LOG(ERR, IPSEC_ESP, "Failed crypto op\n");