examples/ipsec-secgw: convert IV to big endian
[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                                               sizeof(struct ip6_hdr));
216                 }
217         } else
218                 ipip_inbound(m, sizeof(struct esp_hdr) + sa->iv_len);
219
220         return 0;
221 }
222
223 int
224 esp_outbound(struct rte_mbuf *m, struct ipsec_sa *sa,
225                 struct rte_crypto_op *cop)
226 {
227         struct ip *ip4;
228         struct ip6_hdr *ip6;
229         struct esp_hdr *esp = NULL;
230         uint8_t *padding, *new_ip, nlp;
231         struct rte_crypto_sym_op *sym_cop;
232         int32_t i;
233         uint16_t pad_payload_len, pad_len, ip_hdr_len;
234
235         RTE_ASSERT(m != NULL);
236         RTE_ASSERT(sa != NULL);
237         RTE_ASSERT(cop != NULL);
238
239         ip_hdr_len = 0;
240
241         ip4 = rte_pktmbuf_mtod(m, struct ip *);
242         if (likely(ip4->ip_v == IPVERSION)) {
243                 if (unlikely(sa->flags == TRANSPORT)) {
244                         ip_hdr_len = ip4->ip_hl * 4;
245                         nlp = ip4->ip_p;
246                 } else
247                         nlp = IPPROTO_IPIP;
248         } else if (ip4->ip_v == IP6_VERSION) {
249                 if (unlikely(sa->flags == TRANSPORT)) {
250                         /* XXX No option headers supported */
251                         ip_hdr_len = sizeof(struct ip6_hdr);
252                         ip6 = (struct ip6_hdr *)ip4;
253                         nlp = ip6->ip6_nxt;
254                 } else
255                         nlp = IPPROTO_IPV6;
256         } else {
257                 RTE_LOG(ERR, IPSEC_ESP, "invalid IP packet type %d\n",
258                                 ip4->ip_v);
259                 return -EINVAL;
260         }
261
262         /* Padded payload length */
263         pad_payload_len = RTE_ALIGN_CEIL(rte_pktmbuf_pkt_len(m) -
264                         ip_hdr_len + 2, sa->block_size);
265         pad_len = pad_payload_len + ip_hdr_len - rte_pktmbuf_pkt_len(m);
266
267         RTE_ASSERT(sa->flags == IP4_TUNNEL || sa->flags == IP6_TUNNEL ||
268                         sa->flags == TRANSPORT);
269
270         if (likely(sa->flags == IP4_TUNNEL))
271                 ip_hdr_len = sizeof(struct ip);
272         else if (sa->flags == IP6_TUNNEL)
273                 ip_hdr_len = sizeof(struct ip6_hdr);
274         else if (sa->flags != TRANSPORT) {
275                 RTE_LOG(ERR, IPSEC_ESP, "Unsupported SA flags: 0x%x\n",
276                                 sa->flags);
277                 return -EINVAL;
278         }
279
280         /* Check maximum packet size */
281         if (unlikely(ip_hdr_len + sizeof(struct esp_hdr) + sa->iv_len +
282                         pad_payload_len + sa->digest_len > IP_MAXPACKET)) {
283                 RTE_LOG(ERR, IPSEC_ESP, "ipsec packet is too big\n");
284                 return -EINVAL;
285         }
286
287         padding = (uint8_t *)rte_pktmbuf_append(m, pad_len + sa->digest_len);
288         if (unlikely(padding == NULL)) {
289                 RTE_LOG(ERR, IPSEC_ESP, "not enough mbuf trailing space\n");
290                 return -ENOSPC;
291         }
292         rte_prefetch0(padding);
293
294         switch (sa->flags) {
295         case IP4_TUNNEL:
296                 ip4 = ip4ip_outbound(m, sizeof(struct esp_hdr) + sa->iv_len,
297                                 &sa->src, &sa->dst);
298                 esp = (struct esp_hdr *)(ip4 + 1);
299                 break;
300         case IP6_TUNNEL:
301                 ip6 = ip6ip_outbound(m, sizeof(struct esp_hdr) + sa->iv_len,
302                                 &sa->src, &sa->dst);
303                 esp = (struct esp_hdr *)(ip6 + 1);
304                 break;
305         case TRANSPORT:
306                 new_ip = (uint8_t *)rte_pktmbuf_prepend(m,
307                                 sizeof(struct esp_hdr) + sa->iv_len);
308                 memmove(new_ip, ip4, ip_hdr_len);
309                 esp = (struct esp_hdr *)(new_ip + ip_hdr_len);
310                 ip4 = (struct ip *)new_ip;
311                 if (likely(ip4->ip_v == IPVERSION)) {
312                         ip4->ip_p = IPPROTO_ESP;
313                         ip4->ip_len = htons(rte_pktmbuf_data_len(m));
314                 } else {
315                         ip6 = (struct ip6_hdr *)new_ip;
316                         ip6->ip6_nxt = IPPROTO_ESP;
317                         ip6->ip6_plen = htons(rte_pktmbuf_data_len(m) -
318                                               sizeof(struct ip6_hdr));
319                 }
320         }
321
322         sa->seq++;
323         esp->spi = rte_cpu_to_be_32(sa->spi);
324         esp->seq = rte_cpu_to_be_32((uint32_t)sa->seq);
325
326         uint64_t *iv = (uint64_t *)(esp + 1);
327
328         sym_cop = get_sym_cop(cop);
329         sym_cop->m_src = m;
330
331         if (sa->aead_algo == RTE_CRYPTO_AEAD_AES_GCM) {
332                 uint8_t *aad;
333
334                 *iv = rte_cpu_to_be_64(sa->seq);
335                 sym_cop->aead.data.offset = ip_hdr_len +
336                         sizeof(struct esp_hdr) + sa->iv_len;
337                 sym_cop->aead.data.length = pad_payload_len;
338
339                 /* Fill pad_len using default sequential scheme */
340                 for (i = 0; i < pad_len - 2; i++)
341                         padding[i] = i + 1;
342                 padding[pad_len - 2] = pad_len - 2;
343                 padding[pad_len - 1] = nlp;
344
345                 struct cnt_blk *icb = get_cnt_blk(m);
346                 icb->salt = sa->salt;
347                 icb->iv = rte_cpu_to_be_64(sa->seq);
348                 icb->cnt = rte_cpu_to_be_32(1);
349
350                 aad = get_aad(m);
351                 memcpy(aad, esp, 8);
352                 sym_cop->aead.aad.data = aad;
353                 sym_cop->aead.aad.phys_addr = rte_pktmbuf_mtophys_offset(m,
354                                 aad - rte_pktmbuf_mtod(m, uint8_t *));
355
356                 sym_cop->aead.digest.data = rte_pktmbuf_mtod_offset(m, uint8_t *,
357                         rte_pktmbuf_pkt_len(m) - sa->digest_len);
358                 sym_cop->aead.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
359                         rte_pktmbuf_pkt_len(m) - sa->digest_len);
360         } else {
361                 switch (sa->cipher_algo) {
362                 case RTE_CRYPTO_CIPHER_NULL:
363                 case RTE_CRYPTO_CIPHER_AES_CBC:
364                         memset(iv, 0, sa->iv_len);
365                         sym_cop->cipher.data.offset = ip_hdr_len +
366                                 sizeof(struct esp_hdr);
367                         sym_cop->cipher.data.length = pad_payload_len + sa->iv_len;
368                         break;
369                 case RTE_CRYPTO_CIPHER_AES_CTR:
370                         *iv = rte_cpu_to_be_64(sa->seq);
371                         sym_cop->cipher.data.offset = ip_hdr_len +
372                                 sizeof(struct esp_hdr) + sa->iv_len;
373                         sym_cop->cipher.data.length = pad_payload_len;
374                         break;
375                 default:
376                         RTE_LOG(ERR, IPSEC_ESP, "unsupported cipher algorithm %u\n",
377                                         sa->cipher_algo);
378                         return -EINVAL;
379                 }
380
381                 /* Fill pad_len using default sequential scheme */
382                 for (i = 0; i < pad_len - 2; i++)
383                         padding[i] = i + 1;
384                 padding[pad_len - 2] = pad_len - 2;
385                 padding[pad_len - 1] = nlp;
386
387                 struct cnt_blk *icb = get_cnt_blk(m);
388                 icb->salt = sa->salt;
389                 icb->iv = rte_cpu_to_be_64(sa->seq);
390                 icb->cnt = rte_cpu_to_be_32(1);
391
392                 switch (sa->auth_algo) {
393                 case RTE_CRYPTO_AUTH_NULL:
394                 case RTE_CRYPTO_AUTH_SHA1_HMAC:
395                 case RTE_CRYPTO_AUTH_SHA256_HMAC:
396                         sym_cop->auth.data.offset = ip_hdr_len;
397                         sym_cop->auth.data.length = sizeof(struct esp_hdr) +
398                                 sa->iv_len + pad_payload_len;
399                         break;
400                 default:
401                         RTE_LOG(ERR, IPSEC_ESP, "unsupported auth algorithm %u\n",
402                                         sa->auth_algo);
403                         return -EINVAL;
404                 }
405
406                 sym_cop->auth.digest.data = rte_pktmbuf_mtod_offset(m, uint8_t *,
407                                 rte_pktmbuf_pkt_len(m) - sa->digest_len);
408                 sym_cop->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
409                                 rte_pktmbuf_pkt_len(m) - sa->digest_len);
410         }
411
412         return 0;
413 }
414
415 int
416 esp_outbound_post(struct rte_mbuf *m __rte_unused,
417                 struct ipsec_sa *sa __rte_unused,
418                 struct rte_crypto_op *cop)
419 {
420         RTE_ASSERT(m != NULL);
421         RTE_ASSERT(sa != NULL);
422         RTE_ASSERT(cop != NULL);
423
424         if (cop->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
425                 RTE_LOG(ERR, IPSEC_ESP, "Failed crypto op\n");
426                 return -1;
427         }
428
429         return 0;
430 }