1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2017 Intel Corporation
9 #include <netinet/in.h>
10 #include <netinet/ip.h>
11 #include <netinet/ip6.h>
15 #include <rte_common.h>
16 #include <rte_crypto.h>
17 #include <rte_cryptodev.h>
18 #include <rte_random.h>
25 esp_inbound(struct rte_mbuf *m, struct ipsec_sa *sa,
26 struct rte_crypto_op *cop)
29 struct rte_crypto_sym_op *sym_cop;
30 int32_t payload_len, ip_hdr_len;
32 RTE_ASSERT(sa != NULL);
33 if (sa->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO)
36 RTE_ASSERT(m != NULL);
37 RTE_ASSERT(cop != NULL);
39 ip4 = rte_pktmbuf_mtod(m, struct ip *);
40 if (likely(ip4->ip_v == IPVERSION))
41 ip_hdr_len = ip4->ip_hl * 4;
42 else if (ip4->ip_v == IP6_VERSION)
43 /* XXX No option headers supported */
44 ip_hdr_len = sizeof(struct ip6_hdr);
46 RTE_LOG(ERR, IPSEC_ESP, "invalid IP packet type %d\n",
51 payload_len = rte_pktmbuf_pkt_len(m) - ip_hdr_len -
52 sizeof(struct esp_hdr) - sa->iv_len - sa->digest_len;
54 if ((payload_len & (sa->block_size - 1)) || (payload_len <= 0)) {
55 RTE_LOG_DP(DEBUG, IPSEC_ESP, "payload %d not multiple of %u\n",
56 payload_len, sa->block_size);
60 sym_cop = get_sym_cop(cop);
63 if (sa->aead_algo == RTE_CRYPTO_AEAD_AES_GCM) {
64 sym_cop->aead.data.offset = ip_hdr_len + sizeof(struct esp_hdr) +
66 sym_cop->aead.data.length = payload_len;
70 uint8_t *iv = RTE_PTR_ADD(ip4, ip_hdr_len + sizeof(struct esp_hdr));
74 memcpy(&icb->iv, iv, 8);
75 icb->cnt = rte_cpu_to_be_32(1);
78 memcpy(aad, iv - sizeof(struct esp_hdr), 8);
79 sym_cop->aead.aad.data = aad;
80 sym_cop->aead.aad.phys_addr = rte_pktmbuf_iova_offset(m,
81 aad - rte_pktmbuf_mtod(m, uint8_t *));
83 sym_cop->aead.digest.data = rte_pktmbuf_mtod_offset(m, void*,
84 rte_pktmbuf_pkt_len(m) - sa->digest_len);
85 sym_cop->aead.digest.phys_addr = rte_pktmbuf_iova_offset(m,
86 rte_pktmbuf_pkt_len(m) - sa->digest_len);
88 sym_cop->cipher.data.offset = ip_hdr_len + sizeof(struct esp_hdr) +
90 sym_cop->cipher.data.length = payload_len;
93 uint8_t *iv = RTE_PTR_ADD(ip4, ip_hdr_len + sizeof(struct esp_hdr));
94 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(cop,
95 uint8_t *, IV_OFFSET);
97 switch (sa->cipher_algo) {
98 case RTE_CRYPTO_CIPHER_NULL:
99 case RTE_CRYPTO_CIPHER_AES_CBC:
100 /* Copy IV at the end of crypto operation */
101 rte_memcpy(iv_ptr, iv, sa->iv_len);
103 case RTE_CRYPTO_CIPHER_AES_CTR:
104 icb = get_cnt_blk(m);
105 icb->salt = sa->salt;
106 memcpy(&icb->iv, iv, 8);
107 icb->cnt = rte_cpu_to_be_32(1);
110 RTE_LOG(ERR, IPSEC_ESP, "unsupported cipher algorithm %u\n",
115 switch (sa->auth_algo) {
116 case RTE_CRYPTO_AUTH_NULL:
117 case RTE_CRYPTO_AUTH_SHA1_HMAC:
118 case RTE_CRYPTO_AUTH_SHA256_HMAC:
119 sym_cop->auth.data.offset = ip_hdr_len;
120 sym_cop->auth.data.length = sizeof(struct esp_hdr) +
121 sa->iv_len + payload_len;
124 RTE_LOG(ERR, IPSEC_ESP, "unsupported auth algorithm %u\n",
129 sym_cop->auth.digest.data = rte_pktmbuf_mtod_offset(m, void*,
130 rte_pktmbuf_pkt_len(m) - sa->digest_len);
131 sym_cop->auth.digest.phys_addr = rte_pktmbuf_iova_offset(m,
132 rte_pktmbuf_pkt_len(m) - sa->digest_len);
139 esp_inbound_post(struct rte_mbuf *m, struct ipsec_sa *sa,
140 struct rte_crypto_op *cop)
144 uint8_t *nexthdr, *pad_len;
148 RTE_ASSERT(m != NULL);
149 RTE_ASSERT(sa != NULL);
150 RTE_ASSERT(cop != NULL);
152 if (sa->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
153 if (m->ol_flags & PKT_RX_SEC_OFFLOAD) {
154 if (m->ol_flags & PKT_RX_SEC_OFFLOAD_FAILED)
155 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
157 cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
159 cop->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
162 if (cop->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
163 RTE_LOG(ERR, IPSEC_ESP, "failed crypto op\n");
167 if (sa->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO &&
168 sa->ol_flags & RTE_SECURITY_RX_HW_TRAILER_OFFLOAD) {
169 nexthdr = &m->inner_esp_next_proto;
171 nexthdr = rte_pktmbuf_mtod_offset(m, uint8_t*,
172 rte_pktmbuf_pkt_len(m) - sa->digest_len - 1);
173 pad_len = nexthdr - 1;
175 padding = pad_len - *pad_len;
176 for (i = 0; i < *pad_len; i++) {
177 if (padding[i] != i + 1) {
178 RTE_LOG(ERR, IPSEC_ESP, "invalid padding\n");
183 if (rte_pktmbuf_trim(m, *pad_len + 2 + sa->digest_len)) {
184 RTE_LOG(ERR, IPSEC_ESP,
185 "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) -
204 sizeof(struct ip6_hdr));
207 ipip_inbound(m, sizeof(struct esp_hdr) + sa->iv_len);
213 esp_outbound(struct rte_mbuf *m, struct ipsec_sa *sa,
214 struct rte_crypto_op *cop)
218 struct esp_hdr *esp = NULL;
219 uint8_t *padding = NULL, *new_ip, nlp;
220 struct rte_crypto_sym_op *sym_cop;
222 uint16_t pad_payload_len, pad_len, ip_hdr_len;
224 RTE_ASSERT(m != NULL);
225 RTE_ASSERT(sa != 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 /* Add trailer padding if it is not constructed by HW */
276 if (sa->type != RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO ||
277 (sa->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO &&
278 !(sa->ol_flags & RTE_SECURITY_TX_HW_TRAILER_OFFLOAD))) {
279 padding = (uint8_t *)rte_pktmbuf_append(m, pad_len +
281 if (unlikely(padding == NULL)) {
282 RTE_LOG(ERR, IPSEC_ESP,
283 "not enough mbuf trailing space\n");
286 rte_prefetch0(padding);
291 ip4 = ip4ip_outbound(m, sizeof(struct esp_hdr) + sa->iv_len,
293 esp = (struct esp_hdr *)(ip4 + 1);
296 ip6 = ip6ip_outbound(m, sizeof(struct esp_hdr) + sa->iv_len,
298 esp = (struct esp_hdr *)(ip6 + 1);
301 new_ip = (uint8_t *)rte_pktmbuf_prepend(m,
302 sizeof(struct esp_hdr) + sa->iv_len);
303 memmove(new_ip, ip4, ip_hdr_len);
304 esp = (struct esp_hdr *)(new_ip + ip_hdr_len);
305 ip4 = (struct ip *)new_ip;
306 if (likely(ip4->ip_v == IPVERSION)) {
307 ip4->ip_p = IPPROTO_ESP;
308 ip4->ip_len = htons(rte_pktmbuf_data_len(m));
310 ip6 = (struct ip6_hdr *)new_ip;
311 ip6->ip6_nxt = IPPROTO_ESP;
312 ip6->ip6_plen = htons(rte_pktmbuf_data_len(m) -
313 sizeof(struct ip6_hdr));
318 esp->spi = rte_cpu_to_be_32(sa->spi);
319 esp->seq = rte_cpu_to_be_32((uint32_t)sa->seq);
322 uint64_t *iv = (uint64_t *)(esp + 1);
323 if (sa->aead_algo == RTE_CRYPTO_AEAD_AES_GCM) {
324 *iv = rte_cpu_to_be_64(sa->seq);
326 switch (sa->cipher_algo) {
327 case RTE_CRYPTO_CIPHER_NULL:
328 case RTE_CRYPTO_CIPHER_AES_CBC:
329 memset(iv, 0, sa->iv_len);
331 case RTE_CRYPTO_CIPHER_AES_CTR:
332 *iv = rte_cpu_to_be_64(sa->seq);
335 RTE_LOG(ERR, IPSEC_ESP,
336 "unsupported cipher algorithm %u\n",
342 if (sa->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
343 if (sa->ol_flags & RTE_SECURITY_TX_HW_TRAILER_OFFLOAD) {
344 /* Set the inner esp next protocol for HW trailer */
345 m->inner_esp_next_proto = nlp;
346 m->packet_type |= RTE_PTYPE_TUNNEL_ESP;
348 padding[pad_len - 2] = pad_len - 2;
349 padding[pad_len - 1] = nlp;
354 RTE_ASSERT(cop != NULL);
355 sym_cop = get_sym_cop(cop);
358 if (sa->aead_algo == RTE_CRYPTO_AEAD_AES_GCM) {
361 sym_cop->aead.data.offset = ip_hdr_len +
362 sizeof(struct esp_hdr) + sa->iv_len;
363 sym_cop->aead.data.length = pad_payload_len;
365 /* Fill pad_len using default sequential scheme */
366 for (i = 0; i < pad_len - 2; i++)
368 padding[pad_len - 2] = pad_len - 2;
369 padding[pad_len - 1] = nlp;
371 struct cnt_blk *icb = get_cnt_blk(m);
372 icb->salt = sa->salt;
373 icb->iv = rte_cpu_to_be_64(sa->seq);
374 icb->cnt = rte_cpu_to_be_32(1);
378 sym_cop->aead.aad.data = aad;
379 sym_cop->aead.aad.phys_addr = rte_pktmbuf_iova_offset(m,
380 aad - rte_pktmbuf_mtod(m, uint8_t *));
382 sym_cop->aead.digest.data = rte_pktmbuf_mtod_offset(m, uint8_t *,
383 rte_pktmbuf_pkt_len(m) - sa->digest_len);
384 sym_cop->aead.digest.phys_addr = rte_pktmbuf_iova_offset(m,
385 rte_pktmbuf_pkt_len(m) - sa->digest_len);
387 switch (sa->cipher_algo) {
388 case RTE_CRYPTO_CIPHER_NULL:
389 case RTE_CRYPTO_CIPHER_AES_CBC:
390 sym_cop->cipher.data.offset = ip_hdr_len +
391 sizeof(struct esp_hdr);
392 sym_cop->cipher.data.length = pad_payload_len + sa->iv_len;
394 case RTE_CRYPTO_CIPHER_AES_CTR:
395 sym_cop->cipher.data.offset = ip_hdr_len +
396 sizeof(struct esp_hdr) + sa->iv_len;
397 sym_cop->cipher.data.length = pad_payload_len;
400 RTE_LOG(ERR, IPSEC_ESP, "unsupported cipher algorithm %u\n",
405 /* Fill pad_len using default sequential scheme */
406 for (i = 0; i < pad_len - 2; i++)
408 padding[pad_len - 2] = pad_len - 2;
409 padding[pad_len - 1] = nlp;
411 struct cnt_blk *icb = get_cnt_blk(m);
412 icb->salt = sa->salt;
413 icb->iv = rte_cpu_to_be_64(sa->seq);
414 icb->cnt = rte_cpu_to_be_32(1);
416 switch (sa->auth_algo) {
417 case RTE_CRYPTO_AUTH_NULL:
418 case RTE_CRYPTO_AUTH_SHA1_HMAC:
419 case RTE_CRYPTO_AUTH_SHA256_HMAC:
420 sym_cop->auth.data.offset = ip_hdr_len;
421 sym_cop->auth.data.length = sizeof(struct esp_hdr) +
422 sa->iv_len + pad_payload_len;
425 RTE_LOG(ERR, IPSEC_ESP, "unsupported auth algorithm %u\n",
430 sym_cop->auth.digest.data = rte_pktmbuf_mtod_offset(m, uint8_t *,
431 rte_pktmbuf_pkt_len(m) - sa->digest_len);
432 sym_cop->auth.digest.phys_addr = rte_pktmbuf_iova_offset(m,
433 rte_pktmbuf_pkt_len(m) - sa->digest_len);
441 esp_outbound_post(struct rte_mbuf *m,
443 struct rte_crypto_op *cop)
445 RTE_ASSERT(m != NULL);
446 RTE_ASSERT(sa != NULL);
448 if (sa->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
449 m->ol_flags |= PKT_TX_SEC_OFFLOAD;
451 RTE_ASSERT(cop != NULL);
452 if (cop->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
453 RTE_LOG(ERR, IPSEC_ESP, "Failed crypto op\n");