examples: use SPDX tag for Intel copyright files
[dpdk.git] / examples / ipsec-secgw / esp.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016-2017 Intel Corporation
3  */
4
5 #include <stdint.h>
6 #include <stdlib.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <netinet/in.h>
10 #include <netinet/ip.h>
11 #include <netinet/ip6.h>
12 #include <fcntl.h>
13 #include <unistd.h>
14
15 #include <rte_common.h>
16 #include <rte_crypto.h>
17 #include <rte_cryptodev.h>
18 #include <rte_random.h>
19
20 #include "ipsec.h"
21 #include "esp.h"
22 #include "ipip.h"
23
24 int
25 esp_inbound(struct rte_mbuf *m, struct ipsec_sa *sa,
26                 struct rte_crypto_op *cop)
27 {
28         struct ip *ip4;
29         struct rte_crypto_sym_op *sym_cop;
30         int32_t payload_len, ip_hdr_len;
31
32         RTE_ASSERT(sa != NULL);
33         if (sa->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO)
34                 return 0;
35
36         RTE_ASSERT(m != NULL);
37         RTE_ASSERT(cop != NULL);
38
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);
45         else {
46                 RTE_LOG(ERR, IPSEC_ESP, "invalid IP packet type %d\n",
47                                 ip4->ip_v);
48                 return -EINVAL;
49         }
50
51         payload_len = rte_pktmbuf_pkt_len(m) - ip_hdr_len -
52                 sizeof(struct esp_hdr) - sa->iv_len - sa->digest_len;
53
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);
57                 return -EINVAL;
58         }
59
60         sym_cop = get_sym_cop(cop);
61         sym_cop->m_src = m;
62
63         if (sa->aead_algo == RTE_CRYPTO_AEAD_AES_GCM) {
64                 sym_cop->aead.data.offset =  ip_hdr_len + sizeof(struct esp_hdr) +
65                         sa->iv_len;
66                 sym_cop->aead.data.length = payload_len;
67
68                 struct cnt_blk *icb;
69                 uint8_t *aad;
70                 uint8_t *iv = RTE_PTR_ADD(ip4, ip_hdr_len + sizeof(struct esp_hdr));
71
72                 icb = get_cnt_blk(m);
73                 icb->salt = sa->salt;
74                 memcpy(&icb->iv, iv, 8);
75                 icb->cnt = rte_cpu_to_be_32(1);
76
77                 aad = get_aad(m);
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 *));
82
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);
87         } else {
88                 sym_cop->cipher.data.offset =  ip_hdr_len + sizeof(struct esp_hdr) +
89                         sa->iv_len;
90                 sym_cop->cipher.data.length = payload_len;
91
92                 struct cnt_blk *icb;
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);
96
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);
102                         break;
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);
108                         break;
109                 default:
110                         RTE_LOG(ERR, IPSEC_ESP, "unsupported cipher algorithm %u\n",
111                                         sa->cipher_algo);
112                         return -EINVAL;
113                 }
114
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;
122                         break;
123                 default:
124                         RTE_LOG(ERR, IPSEC_ESP, "unsupported auth algorithm %u\n",
125                                         sa->auth_algo);
126                         return -EINVAL;
127                 }
128
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);
133         }
134
135         return 0;
136 }
137
138 int
139 esp_inbound_post(struct rte_mbuf *m, struct ipsec_sa *sa,
140                 struct rte_crypto_op *cop)
141 {
142         struct ip *ip4, *ip;
143         struct ip6_hdr *ip6;
144         uint8_t *nexthdr, *pad_len;
145         uint8_t *padding;
146         uint16_t i;
147
148         RTE_ASSERT(m != NULL);
149         RTE_ASSERT(sa != NULL);
150         RTE_ASSERT(cop != NULL);
151
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;
156                         else
157                                 cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
158                 } else
159                         cop->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
160         }
161
162         if (cop->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
163                 RTE_LOG(ERR, IPSEC_ESP, "failed crypto op\n");
164                 return -1;
165         }
166
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;
170         } else {
171                 nexthdr = rte_pktmbuf_mtod_offset(m, uint8_t*,
172                                 rte_pktmbuf_pkt_len(m) - sa->digest_len - 1);
173                 pad_len = nexthdr - 1;
174
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");
179                                 return -EINVAL;
180                         }
181                 }
182
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");
186                         return -EINVAL;
187                 }
188         }
189
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));
198                 } else {
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));
205                 }
206         } else
207                 ipip_inbound(m, sizeof(struct esp_hdr) + sa->iv_len);
208
209         return 0;
210 }
211
212 int
213 esp_outbound(struct rte_mbuf *m, struct ipsec_sa *sa,
214                 struct rte_crypto_op *cop)
215 {
216         struct ip *ip4;
217         struct ip6_hdr *ip6;
218         struct esp_hdr *esp = NULL;
219         uint8_t *padding = NULL, *new_ip, nlp;
220         struct rte_crypto_sym_op *sym_cop;
221         int32_t i;
222         uint16_t pad_payload_len, pad_len, ip_hdr_len;
223
224         RTE_ASSERT(m != NULL);
225         RTE_ASSERT(sa != NULL);
226
227         ip_hdr_len = 0;
228
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;
233                         nlp = ip4->ip_p;
234                 } else
235                         nlp = IPPROTO_IPIP;
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;
241                         nlp = ip6->ip6_nxt;
242                 } else
243                         nlp = IPPROTO_IPV6;
244         } else {
245                 RTE_LOG(ERR, IPSEC_ESP, "invalid IP packet type %d\n",
246                                 ip4->ip_v);
247                 return -EINVAL;
248         }
249
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);
254
255         RTE_ASSERT(sa->flags == IP4_TUNNEL || sa->flags == IP6_TUNNEL ||
256                         sa->flags == TRANSPORT);
257
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",
264                                 sa->flags);
265                 return -EINVAL;
266         }
267
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");
272                 return -EINVAL;
273         }
274
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 +
280                                                         sa->digest_len);
281                 if (unlikely(padding == NULL)) {
282                         RTE_LOG(ERR, IPSEC_ESP,
283                                         "not enough mbuf trailing space\n");
284                         return -ENOSPC;
285                 }
286                 rte_prefetch0(padding);
287         }
288
289         switch (sa->flags) {
290         case IP4_TUNNEL:
291                 ip4 = ip4ip_outbound(m, sizeof(struct esp_hdr) + sa->iv_len,
292                                 &sa->src, &sa->dst);
293                 esp = (struct esp_hdr *)(ip4 + 1);
294                 break;
295         case IP6_TUNNEL:
296                 ip6 = ip6ip_outbound(m, sizeof(struct esp_hdr) + sa->iv_len,
297                                 &sa->src, &sa->dst);
298                 esp = (struct esp_hdr *)(ip6 + 1);
299                 break;
300         case TRANSPORT:
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));
309                 } else {
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));
314                 }
315         }
316
317         sa->seq++;
318         esp->spi = rte_cpu_to_be_32(sa->spi);
319         esp->seq = rte_cpu_to_be_32((uint32_t)sa->seq);
320
321         /* set iv */
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);
325         } else {
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);
330                         break;
331                 case RTE_CRYPTO_CIPHER_AES_CTR:
332                         *iv = rte_cpu_to_be_64(sa->seq);
333                         break;
334                 default:
335                         RTE_LOG(ERR, IPSEC_ESP,
336                                 "unsupported cipher algorithm %u\n",
337                                 sa->cipher_algo);
338                         return -EINVAL;
339                 }
340         }
341
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;
347                 } else {
348                         padding[pad_len - 2] = pad_len - 2;
349                         padding[pad_len - 1] = nlp;
350                 }
351                 goto done;
352         }
353
354         RTE_ASSERT(cop != NULL);
355         sym_cop = get_sym_cop(cop);
356         sym_cop->m_src = m;
357
358         if (sa->aead_algo == RTE_CRYPTO_AEAD_AES_GCM) {
359                 uint8_t *aad;
360
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;
364
365                 /* Fill pad_len using default sequential scheme */
366                 for (i = 0; i < pad_len - 2; i++)
367                         padding[i] = i + 1;
368                 padding[pad_len - 2] = pad_len - 2;
369                 padding[pad_len - 1] = nlp;
370
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);
375
376                 aad = get_aad(m);
377                 memcpy(aad, esp, 8);
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 *));
381
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);
386         } else {
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;
393                         break;
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;
398                         break;
399                 default:
400                         RTE_LOG(ERR, IPSEC_ESP, "unsupported cipher algorithm %u\n",
401                                         sa->cipher_algo);
402                         return -EINVAL;
403                 }
404
405                 /* Fill pad_len using default sequential scheme */
406                 for (i = 0; i < pad_len - 2; i++)
407                         padding[i] = i + 1;
408                 padding[pad_len - 2] = pad_len - 2;
409                 padding[pad_len - 1] = nlp;
410
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);
415
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;
423                         break;
424                 default:
425                         RTE_LOG(ERR, IPSEC_ESP, "unsupported auth algorithm %u\n",
426                                         sa->auth_algo);
427                         return -EINVAL;
428                 }
429
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);
434         }
435
436 done:
437         return 0;
438 }
439
440 int
441 esp_outbound_post(struct rte_mbuf *m,
442                   struct ipsec_sa *sa,
443                   struct rte_crypto_op *cop)
444 {
445         RTE_ASSERT(m != NULL);
446         RTE_ASSERT(sa != NULL);
447
448         if (sa->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
449                 m->ol_flags |= PKT_TX_SEC_OFFLOAD;
450         } else {
451                 RTE_ASSERT(cop != NULL);
452                 if (cop->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
453                         RTE_LOG(ERR, IPSEC_ESP, "Failed crypto op\n");
454                         return -1;
455                 }
456         }
457
458         return 0;
459 }