70bb81f75a79816c2615025b00fb8c4b3705fc38
[deb_dpdk.git] / examples / ipsec-secgw / esp.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016-2017 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include <stdint.h>
35 #include <stdlib.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <netinet/in.h>
39 #include <netinet/ip.h>
40 #include <netinet/ip6.h>
41 #include <fcntl.h>
42 #include <unistd.h>
43
44 #include <rte_common.h>
45 #include <rte_crypto.h>
46 #include <rte_cryptodev.h>
47 #include <rte_random.h>
48
49 #include "ipsec.h"
50 #include "esp.h"
51 #include "ipip.h"
52
53 int
54 esp_inbound(struct rte_mbuf *m, struct ipsec_sa *sa,
55                 struct rte_crypto_op *cop)
56 {
57         struct ip *ip4;
58         struct rte_crypto_sym_op *sym_cop;
59         int32_t payload_len, ip_hdr_len;
60
61         RTE_ASSERT(m != NULL);
62         RTE_ASSERT(sa != NULL);
63         RTE_ASSERT(cop != NULL);
64
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);
71         else {
72                 RTE_LOG(ERR, IPSEC_ESP, "invalid IP packet type %d\n",
73                                 ip4->ip_v);
74                 return -EINVAL;
75         }
76
77         payload_len = rte_pktmbuf_pkt_len(m) - ip_hdr_len -
78                 sizeof(struct esp_hdr) - sa->iv_len - sa->digest_len;
79
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);
83                 return -EINVAL;
84         }
85
86         sym_cop = get_sym_cop(cop);
87         sym_cop->m_src = m;
88
89         if (sa->aead_algo == RTE_CRYPTO_AEAD_AES_GCM) {
90                 sym_cop->aead.data.offset =  ip_hdr_len + sizeof(struct esp_hdr) +
91                         sa->iv_len;
92                 sym_cop->aead.data.length = payload_len;
93
94                 struct cnt_blk *icb;
95                 uint8_t *aad;
96                 uint8_t *iv = RTE_PTR_ADD(ip4, ip_hdr_len + sizeof(struct esp_hdr));
97
98                 icb = get_cnt_blk(m);
99                 icb->salt = sa->salt;
100                 memcpy(&icb->iv, iv, 8);
101                 icb->cnt = rte_cpu_to_be_32(1);
102
103                 aad = get_aad(m);
104                 memcpy(aad, iv - sizeof(struct esp_hdr), 8);
105                 sym_cop->aead.aad.data = aad;
106                 sym_cop->aead.aad.phys_addr = rte_pktmbuf_mtophys_offset(m,
107                                 aad - rte_pktmbuf_mtod(m, uint8_t *));
108
109                 sym_cop->aead.digest.data = rte_pktmbuf_mtod_offset(m, void*,
110                                 rte_pktmbuf_pkt_len(m) - sa->digest_len);
111                 sym_cop->aead.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
112                                 rte_pktmbuf_pkt_len(m) - sa->digest_len);
113         } else {
114                 sym_cop->cipher.data.offset =  ip_hdr_len + sizeof(struct esp_hdr) +
115                         sa->iv_len;
116                 sym_cop->cipher.data.length = payload_len;
117
118                 struct cnt_blk *icb;
119                 uint8_t *iv = RTE_PTR_ADD(ip4, ip_hdr_len + sizeof(struct esp_hdr));
120                 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(cop,
121                                         uint8_t *, IV_OFFSET);
122
123                 switch (sa->cipher_algo) {
124                 case RTE_CRYPTO_CIPHER_NULL:
125                 case RTE_CRYPTO_CIPHER_AES_CBC:
126                         /* Copy IV at the end of crypto operation */
127                         rte_memcpy(iv_ptr, iv, sa->iv_len);
128                         break;
129                 case RTE_CRYPTO_CIPHER_AES_CTR:
130                         icb = get_cnt_blk(m);
131                         icb->salt = sa->salt;
132                         memcpy(&icb->iv, iv, 8);
133                         icb->cnt = rte_cpu_to_be_32(1);
134                         break;
135                 default:
136                         RTE_LOG(ERR, IPSEC_ESP, "unsupported cipher algorithm %u\n",
137                                         sa->cipher_algo);
138                         return -EINVAL;
139                 }
140
141                 switch (sa->auth_algo) {
142                 case RTE_CRYPTO_AUTH_NULL:
143                 case RTE_CRYPTO_AUTH_SHA1_HMAC:
144                 case RTE_CRYPTO_AUTH_SHA256_HMAC:
145                         sym_cop->auth.data.offset = ip_hdr_len;
146                         sym_cop->auth.data.length = sizeof(struct esp_hdr) +
147                                 sa->iv_len + payload_len;
148                         break;
149                 default:
150                         RTE_LOG(ERR, IPSEC_ESP, "unsupported auth algorithm %u\n",
151                                         sa->auth_algo);
152                         return -EINVAL;
153                 }
154
155                 sym_cop->auth.digest.data = rte_pktmbuf_mtod_offset(m, void*,
156                                 rte_pktmbuf_pkt_len(m) - sa->digest_len);
157                 sym_cop->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
158                                 rte_pktmbuf_pkt_len(m) - sa->digest_len);
159         }
160
161         return 0;
162 }
163
164 int
165 esp_inbound_post(struct rte_mbuf *m, struct ipsec_sa *sa,
166                 struct rte_crypto_op *cop)
167 {
168         struct ip *ip4, *ip;
169         struct ip6_hdr *ip6;
170         uint8_t *nexthdr, *pad_len;
171         uint8_t *padding;
172         uint16_t i;
173
174         RTE_ASSERT(m != NULL);
175         RTE_ASSERT(sa != NULL);
176         RTE_ASSERT(cop != NULL);
177
178         if (cop->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
179                 RTE_LOG(ERR, IPSEC_ESP, "failed crypto op\n");
180                 return -1;
181         }
182
183         nexthdr = rte_pktmbuf_mtod_offset(m, uint8_t*,
184                         rte_pktmbuf_pkt_len(m) - sa->digest_len - 1);
185         pad_len = nexthdr - 1;
186
187         padding = pad_len - *pad_len;
188         for (i = 0; i < *pad_len; i++) {
189                 if (padding[i] != i + 1) {
190                         RTE_LOG(ERR, IPSEC_ESP, "invalid padding\n");
191                         return -EINVAL;
192                 }
193         }
194
195         if (rte_pktmbuf_trim(m, *pad_len + 2 + sa->digest_len)) {
196                 RTE_LOG(ERR, IPSEC_ESP,
197                                 "failed to remove pad_len + digest\n");
198                 return -EINVAL;
199         }
200
201         if (unlikely(sa->flags == TRANSPORT)) {
202                 ip = rte_pktmbuf_mtod(m, struct ip *);
203                 ip4 = (struct ip *)rte_pktmbuf_adj(m,
204                                 sizeof(struct esp_hdr) + sa->iv_len);
205                 if (likely(ip->ip_v == IPVERSION)) {
206                         memmove(ip4, ip, ip->ip_hl * 4);
207                         ip4->ip_p = *nexthdr;
208                         ip4->ip_len = htons(rte_pktmbuf_data_len(m));
209                 } else {
210                         ip6 = (struct ip6_hdr *)ip4;
211                         /* XXX No option headers supported */
212                         memmove(ip6, ip, sizeof(struct ip6_hdr));
213                         ip6->ip6_nxt = *nexthdr;
214                         ip6->ip6_plen = htons(rte_pktmbuf_data_len(m));
215                 }
216         } else
217                 ipip_inbound(m, sizeof(struct esp_hdr) + sa->iv_len);
218
219         return 0;
220 }
221
222 int
223 esp_outbound(struct rte_mbuf *m, struct ipsec_sa *sa,
224                 struct rte_crypto_op *cop)
225 {
226         struct ip *ip4;
227         struct ip6_hdr *ip6;
228         struct esp_hdr *esp = NULL;
229         uint8_t *padding, *new_ip, nlp;
230         struct rte_crypto_sym_op *sym_cop;
231         int32_t i;
232         uint16_t pad_payload_len, pad_len, ip_hdr_len;
233
234         RTE_ASSERT(m != NULL);
235         RTE_ASSERT(sa != NULL);
236         RTE_ASSERT(cop != NULL);
237
238         ip_hdr_len = 0;
239
240         ip4 = rte_pktmbuf_mtod(m, struct ip *);
241         if (likely(ip4->ip_v == IPVERSION)) {
242                 if (unlikely(sa->flags == TRANSPORT)) {
243                         ip_hdr_len = ip4->ip_hl * 4;
244                         nlp = ip4->ip_p;
245                 } else
246                         nlp = IPPROTO_IPIP;
247         } else if (ip4->ip_v == IP6_VERSION) {
248                 if (unlikely(sa->flags == TRANSPORT)) {
249                         /* XXX No option headers supported */
250                         ip_hdr_len = sizeof(struct ip6_hdr);
251                         ip6 = (struct ip6_hdr *)ip4;
252                         nlp = ip6->ip6_nxt;
253                 } else
254                         nlp = IPPROTO_IPV6;
255         } else {
256                 RTE_LOG(ERR, IPSEC_ESP, "invalid IP packet type %d\n",
257                                 ip4->ip_v);
258                 return -EINVAL;
259         }
260
261         /* Padded payload length */
262         pad_payload_len = RTE_ALIGN_CEIL(rte_pktmbuf_pkt_len(m) -
263                         ip_hdr_len + 2, sa->block_size);
264         pad_len = pad_payload_len + ip_hdr_len - rte_pktmbuf_pkt_len(m);
265
266         RTE_ASSERT(sa->flags == IP4_TUNNEL || sa->flags == IP6_TUNNEL ||
267                         sa->flags == TRANSPORT);
268
269         if (likely(sa->flags == IP4_TUNNEL))
270                 ip_hdr_len = sizeof(struct ip);
271         else if (sa->flags == IP6_TUNNEL)
272                 ip_hdr_len = sizeof(struct ip6_hdr);
273         else if (sa->flags != TRANSPORT) {
274                 RTE_LOG(ERR, IPSEC_ESP, "Unsupported SA flags: 0x%x\n",
275                                 sa->flags);
276                 return -EINVAL;
277         }
278
279         /* Check maximum packet size */
280         if (unlikely(ip_hdr_len + sizeof(struct esp_hdr) + sa->iv_len +
281                         pad_payload_len + sa->digest_len > IP_MAXPACKET)) {
282                 RTE_LOG(ERR, IPSEC_ESP, "ipsec packet is too big\n");
283                 return -EINVAL;
284         }
285
286         padding = (uint8_t *)rte_pktmbuf_append(m, pad_len + sa->digest_len);
287         if (unlikely(padding == NULL)) {
288                 RTE_LOG(ERR, IPSEC_ESP, "not enough mbuf trailing space\n");
289                 return -ENOSPC;
290         }
291         rte_prefetch0(padding);
292
293         switch (sa->flags) {
294         case IP4_TUNNEL:
295                 ip4 = ip4ip_outbound(m, sizeof(struct esp_hdr) + sa->iv_len,
296                                 &sa->src, &sa->dst);
297                 esp = (struct esp_hdr *)(ip4 + 1);
298                 break;
299         case IP6_TUNNEL:
300                 ip6 = ip6ip_outbound(m, sizeof(struct esp_hdr) + sa->iv_len,
301                                 &sa->src, &sa->dst);
302                 esp = (struct esp_hdr *)(ip6 + 1);
303                 break;
304         case TRANSPORT:
305                 new_ip = (uint8_t *)rte_pktmbuf_prepend(m,
306                                 sizeof(struct esp_hdr) + sa->iv_len);
307                 memmove(new_ip, ip4, ip_hdr_len);
308                 esp = (struct esp_hdr *)(new_ip + ip_hdr_len);
309                 if (likely(ip4->ip_v == IPVERSION)) {
310                         ip4 = (struct ip *)new_ip;
311                         ip4->ip_p = IPPROTO_ESP;
312                         ip4->ip_len = htons(rte_pktmbuf_data_len(m));
313                 } else {
314                         ip6 = (struct ip6_hdr *)new_ip;
315                         ip6->ip6_nxt = IPPROTO_ESP;
316                         ip6->ip6_plen = htons(rte_pktmbuf_data_len(m));
317                 }
318         }
319
320         sa->seq++;
321         esp->spi = rte_cpu_to_be_32(sa->spi);
322         esp->seq = rte_cpu_to_be_32((uint32_t)sa->seq);
323
324         uint64_t *iv = (uint64_t *)(esp + 1);
325
326         sym_cop = get_sym_cop(cop);
327         sym_cop->m_src = m;
328
329         if (sa->aead_algo == RTE_CRYPTO_AEAD_AES_GCM) {
330                 uint8_t *aad;
331
332                 *iv = sa->seq;
333                 sym_cop->aead.data.offset = ip_hdr_len +
334                         sizeof(struct esp_hdr) + sa->iv_len;
335                 sym_cop->aead.data.length = pad_payload_len;
336
337                 /* Fill pad_len using default sequential scheme */
338                 for (i = 0; i < pad_len - 2; i++)
339                         padding[i] = i + 1;
340                 padding[pad_len - 2] = pad_len - 2;
341                 padding[pad_len - 1] = nlp;
342
343                 struct cnt_blk *icb = get_cnt_blk(m);
344                 icb->salt = sa->salt;
345                 icb->iv = sa->seq;
346                 icb->cnt = rte_cpu_to_be_32(1);
347
348                 aad = get_aad(m);
349                 memcpy(aad, esp, 8);
350                 sym_cop->aead.aad.data = aad;
351                 sym_cop->aead.aad.phys_addr = rte_pktmbuf_mtophys_offset(m,
352                                 aad - rte_pktmbuf_mtod(m, uint8_t *));
353
354                 sym_cop->aead.digest.data = rte_pktmbuf_mtod_offset(m, uint8_t *,
355                         rte_pktmbuf_pkt_len(m) - sa->digest_len);
356                 sym_cop->aead.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
357                         rte_pktmbuf_pkt_len(m) - sa->digest_len);
358         } else {
359                 switch (sa->cipher_algo) {
360                 case RTE_CRYPTO_CIPHER_NULL:
361                 case RTE_CRYPTO_CIPHER_AES_CBC:
362                         memset(iv, 0, sa->iv_len);
363                         sym_cop->cipher.data.offset = ip_hdr_len +
364                                 sizeof(struct esp_hdr);
365                         sym_cop->cipher.data.length = pad_payload_len + sa->iv_len;
366                         break;
367                 case RTE_CRYPTO_CIPHER_AES_CTR:
368                         *iv = sa->seq;
369                         sym_cop->cipher.data.offset = ip_hdr_len +
370                                 sizeof(struct esp_hdr) + sa->iv_len;
371                         sym_cop->cipher.data.length = pad_payload_len;
372                         break;
373                 default:
374                         RTE_LOG(ERR, IPSEC_ESP, "unsupported cipher algorithm %u\n",
375                                         sa->cipher_algo);
376                         return -EINVAL;
377                 }
378
379                 /* Fill pad_len using default sequential scheme */
380                 for (i = 0; i < pad_len - 2; i++)
381                         padding[i] = i + 1;
382                 padding[pad_len - 2] = pad_len - 2;
383                 padding[pad_len - 1] = nlp;
384
385                 struct cnt_blk *icb = get_cnt_blk(m);
386                 icb->salt = sa->salt;
387                 icb->iv = sa->seq;
388                 icb->cnt = rte_cpu_to_be_32(1);
389
390                 switch (sa->auth_algo) {
391                 case RTE_CRYPTO_AUTH_NULL:
392                 case RTE_CRYPTO_AUTH_SHA1_HMAC:
393                 case RTE_CRYPTO_AUTH_SHA256_HMAC:
394                         sym_cop->auth.data.offset = ip_hdr_len;
395                         sym_cop->auth.data.length = sizeof(struct esp_hdr) +
396                                 sa->iv_len + pad_payload_len;
397                         break;
398                 default:
399                         RTE_LOG(ERR, IPSEC_ESP, "unsupported auth algorithm %u\n",
400                                         sa->auth_algo);
401                         return -EINVAL;
402                 }
403
404                 sym_cop->auth.digest.data = rte_pktmbuf_mtod_offset(m, uint8_t *,
405                                 rte_pktmbuf_pkt_len(m) - sa->digest_len);
406                 sym_cop->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
407                                 rte_pktmbuf_pkt_len(m) - sa->digest_len);
408         }
409
410         return 0;
411 }
412
413 int
414 esp_outbound_post(struct rte_mbuf *m __rte_unused,
415                 struct ipsec_sa *sa __rte_unused,
416                 struct rte_crypto_op *cop)
417 {
418         RTE_ASSERT(m != NULL);
419         RTE_ASSERT(sa != NULL);
420         RTE_ASSERT(cop != NULL);
421
422         if (cop->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
423                 RTE_LOG(ERR, IPSEC_ESP, "Failed crypto op\n");
424                 return -1;
425         }
426
427         return 0;
428 }