examples/ipsec-secgw: add AES-GCM
[dpdk.git] / examples / ipsec-secgw / esp.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016 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(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
88         sym_cop->m_src = m;
89         sym_cop->cipher.data.offset =  ip_hdr_len + sizeof(struct esp_hdr) +
90                 sa->iv_len;
91         sym_cop->cipher.data.length = payload_len;
92
93         struct cnt_blk *icb;
94         uint8_t *aad;
95         uint8_t *iv = RTE_PTR_ADD(ip4, ip_hdr_len + sizeof(struct esp_hdr));
96
97         switch (sa->cipher_algo) {
98         case RTE_CRYPTO_CIPHER_NULL:
99         case RTE_CRYPTO_CIPHER_AES_CBC:
100                 sym_cop->cipher.iv.data = iv;
101                 sym_cop->cipher.iv.phys_addr = rte_pktmbuf_mtophys_offset(m,
102                                  ip_hdr_len + sizeof(struct esp_hdr));
103                 sym_cop->cipher.iv.length = sa->iv_len;
104                 break;
105         case RTE_CRYPTO_CIPHER_AES_GCM:
106                 icb = get_cnt_blk(m);
107                 icb->salt = sa->salt;
108                 memcpy(&icb->iv, iv, 8);
109                 icb->cnt = rte_cpu_to_be_32(1);
110                 sym_cop->cipher.iv.data = (uint8_t *)icb;
111                 sym_cop->cipher.iv.phys_addr = rte_pktmbuf_mtophys_offset(m,
112                          (uint8_t *)icb - rte_pktmbuf_mtod(m, uint8_t *));
113                 sym_cop->cipher.iv.length = 16;
114                 break;
115         default:
116                 RTE_LOG(ERR, IPSEC_ESP, "unsupported cipher algorithm %u\n",
117                                 sa->cipher_algo);
118                 return -EINVAL;
119         }
120
121         switch (sa->auth_algo) {
122         case RTE_CRYPTO_AUTH_NULL:
123         case RTE_CRYPTO_AUTH_SHA1_HMAC:
124                 sym_cop->auth.data.offset = ip_hdr_len;
125                 sym_cop->auth.data.length = sizeof(struct esp_hdr) +
126                         sa->iv_len + payload_len;
127                 break;
128         case RTE_CRYPTO_AUTH_AES_GCM:
129                 aad = get_aad(m);
130                 memcpy(aad, iv - sizeof(struct esp_hdr), 8);
131                 sym_cop->auth.aad.data = aad;
132                 sym_cop->auth.aad.phys_addr = rte_pktmbuf_mtophys_offset(m,
133                                 aad - rte_pktmbuf_mtod(m, uint8_t *));
134                 sym_cop->auth.aad.length = 8;
135                 break;
136         default:
137                 RTE_LOG(ERR, IPSEC_ESP, "unsupported auth algorithm %u\n",
138                                 sa->auth_algo);
139                 return -EINVAL;
140         }
141
142         sym_cop->auth.digest.data = rte_pktmbuf_mtod_offset(m, void*,
143                         rte_pktmbuf_pkt_len(m) - sa->digest_len);
144         sym_cop->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
145                         rte_pktmbuf_pkt_len(m) - sa->digest_len);
146         sym_cop->auth.digest.length = sa->digest_len;
147
148         return 0;
149 }
150
151 int
152 esp_inbound_post(struct rte_mbuf *m, struct ipsec_sa *sa,
153                 struct rte_crypto_op *cop)
154 {
155         struct ip *ip4, *ip;
156         struct ip6_hdr *ip6;
157         uint8_t *nexthdr, *pad_len;
158         uint8_t *padding;
159         uint16_t i;
160
161         RTE_ASSERT(m != NULL);
162         RTE_ASSERT(sa != NULL);
163         RTE_ASSERT(cop != NULL);
164
165         if (cop->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
166                 RTE_LOG(ERR, IPSEC_ESP, "failed crypto op\n");
167                 return -1;
168         }
169
170         nexthdr = rte_pktmbuf_mtod_offset(m, uint8_t*,
171                         rte_pktmbuf_pkt_len(m) - sa->digest_len - 1);
172         pad_len = nexthdr - 1;
173
174         padding = pad_len - *pad_len;
175         for (i = 0; i < *pad_len; i++) {
176                 if (padding[i] != i + 1) {
177                         RTE_LOG(ERR, IPSEC_ESP, "invalid padding\n");
178                         return -EINVAL;
179                 }
180         }
181
182         if (rte_pktmbuf_trim(m, *pad_len + 2 + sa->digest_len)) {
183                 RTE_LOG(ERR, IPSEC_ESP,
184                                 "failed to remove pad_len + digest\n");
185                 return -EINVAL;
186         }
187
188         if (unlikely(sa->flags == TRANSPORT)) {
189                 ip = rte_pktmbuf_mtod(m, struct ip *);
190                 ip4 = (struct ip *)rte_pktmbuf_adj(m,
191                                 sizeof(struct esp_hdr) + sa->iv_len);
192                 if (likely(ip->ip_v == IPVERSION)) {
193                         memmove(ip4, ip, ip->ip_hl * 4);
194                         ip4->ip_p = *nexthdr;
195                         ip4->ip_len = htons(rte_pktmbuf_data_len(m));
196                 } else {
197                         ip6 = (struct ip6_hdr *)ip4;
198                         /* XXX No option headers supported */
199                         memmove(ip6, ip, sizeof(struct ip6_hdr));
200                         ip6->ip6_nxt = *nexthdr;
201                         ip6->ip6_plen = htons(rte_pktmbuf_data_len(m));
202                 }
203         } else
204                 ipip_inbound(m, sizeof(struct esp_hdr) + sa->iv_len);
205
206         return 0;
207 }
208
209 int
210 esp_outbound(struct rte_mbuf *m, struct ipsec_sa *sa,
211                 struct rte_crypto_op *cop)
212 {
213         struct ip *ip4;
214         struct ip6_hdr *ip6;
215         struct esp_hdr *esp = NULL;
216         uint8_t *padding, *new_ip, nlp;
217         struct rte_crypto_sym_op *sym_cop;
218         int32_t i;
219         uint16_t pad_payload_len, pad_len, ip_hdr_len;
220
221         RTE_ASSERT(m != NULL);
222         RTE_ASSERT(sa != NULL);
223         RTE_ASSERT(cop != NULL);
224
225         ip_hdr_len = 0;
226
227         ip4 = rte_pktmbuf_mtod(m, struct ip *);
228         if (likely(ip4->ip_v == IPVERSION)) {
229                 if (unlikely(sa->flags == TRANSPORT)) {
230                         ip_hdr_len = ip4->ip_hl * 4;
231                         nlp = ip4->ip_p;
232                 } else
233                         nlp = IPPROTO_IPIP;
234         } else if (ip4->ip_v == IP6_VERSION) {
235                 if (unlikely(sa->flags == TRANSPORT)) {
236                         /* XXX No option headers supported */
237                         ip_hdr_len = sizeof(struct ip6_hdr);
238                         ip6 = (struct ip6_hdr *)ip4;
239                         nlp = ip6->ip6_nxt;
240                 } else
241                         nlp = IPPROTO_IPV6;
242         } else {
243                 RTE_LOG(ERR, IPSEC_ESP, "invalid IP packet type %d\n",
244                                 ip4->ip_v);
245                 return -EINVAL;
246         }
247
248         /* Padded payload length */
249         pad_payload_len = RTE_ALIGN_CEIL(rte_pktmbuf_pkt_len(m) -
250                         ip_hdr_len + 2, sa->block_size);
251         pad_len = pad_payload_len + ip_hdr_len - rte_pktmbuf_pkt_len(m);
252
253         RTE_ASSERT(sa->flags == IP4_TUNNEL || sa->flags == IP6_TUNNEL ||
254                         sa->flags == TRANSPORT);
255
256         if (likely(sa->flags == IP4_TUNNEL))
257                 ip_hdr_len = sizeof(struct ip);
258         else if (sa->flags == IP6_TUNNEL)
259                 ip_hdr_len = sizeof(struct ip6_hdr);
260         else if (sa->flags != TRANSPORT) {
261                 RTE_LOG(ERR, IPSEC_ESP, "Unsupported SA flags: 0x%x\n",
262                                 sa->flags);
263                 return -EINVAL;
264         }
265
266         /* Check maximum packet size */
267         if (unlikely(ip_hdr_len + sizeof(struct esp_hdr) + sa->iv_len +
268                         pad_payload_len + sa->digest_len > IP_MAXPACKET)) {
269                 RTE_LOG(ERR, IPSEC_ESP, "ipsec packet is too big\n");
270                 return -EINVAL;
271         }
272
273         padding = (uint8_t *)rte_pktmbuf_append(m, pad_len + sa->digest_len);
274         if (unlikely(padding == NULL)) {
275                 RTE_LOG(ERR, IPSEC_ESP, "not enough mbuf trailing space\n");
276                 return -ENOSPC;
277         }
278         rte_prefetch0(padding);
279
280         switch (sa->flags) {
281         case IP4_TUNNEL:
282                 ip4 = ip4ip_outbound(m, sizeof(struct esp_hdr) + sa->iv_len,
283                                 &sa->src, &sa->dst);
284                 esp = (struct esp_hdr *)(ip4 + 1);
285                 break;
286         case IP6_TUNNEL:
287                 ip6 = ip6ip_outbound(m, sizeof(struct esp_hdr) + sa->iv_len,
288                                 &sa->src, &sa->dst);
289                 esp = (struct esp_hdr *)(ip6 + 1);
290                 break;
291         case TRANSPORT:
292                 new_ip = (uint8_t *)rte_pktmbuf_prepend(m,
293                                 sizeof(struct esp_hdr) + sa->iv_len);
294                 memmove(new_ip, ip4, ip_hdr_len);
295                 esp = (struct esp_hdr *)(new_ip + ip_hdr_len);
296                 if (likely(ip4->ip_v == IPVERSION)) {
297                         ip4 = (struct ip *)new_ip;
298                         ip4->ip_p = IPPROTO_ESP;
299                         ip4->ip_len = htons(rte_pktmbuf_data_len(m));
300                 } else {
301                         ip6 = (struct ip6_hdr *)new_ip;
302                         ip6->ip6_nxt = IPPROTO_ESP;
303                         ip6->ip6_plen = htons(rte_pktmbuf_data_len(m));
304                 }
305         }
306
307         sa->seq++;
308         esp->spi = rte_cpu_to_be_32(sa->spi);
309         esp->seq = rte_cpu_to_be_32((uint32_t)sa->seq);
310
311         uint64_t *iv = (uint64_t *)(esp + 1);
312
313         sym_cop = get_sym_cop(cop);
314         sym_cop->m_src = m;
315         switch (sa->cipher_algo) {
316         case RTE_CRYPTO_CIPHER_NULL:
317         case RTE_CRYPTO_CIPHER_AES_CBC:
318                 memset(iv, 0, sa->iv_len);
319                 sym_cop->cipher.data.offset = ip_hdr_len +
320                         sizeof(struct esp_hdr);
321                 sym_cop->cipher.data.length = pad_payload_len + sa->iv_len;
322                 break;
323         case RTE_CRYPTO_CIPHER_AES_GCM:
324                 *iv = sa->seq;
325                 sym_cop->cipher.data.offset = ip_hdr_len +
326                         sizeof(struct esp_hdr) + sa->iv_len;
327                 sym_cop->cipher.data.length = pad_payload_len;
328                 break;
329         default:
330                 RTE_LOG(ERR, IPSEC_ESP, "unsupported cipher algorithm %u\n",
331                                 sa->cipher_algo);
332                 return -EINVAL;
333         }
334
335         /* Fill pad_len using default sequential scheme */
336         for (i = 0; i < pad_len - 2; i++)
337                 padding[i] = i + 1;
338         padding[pad_len - 2] = pad_len - 2;
339         padding[pad_len - 1] = nlp;
340
341         struct cnt_blk *icb = get_cnt_blk(m);
342         icb->salt = sa->salt;
343         icb->iv = sa->seq;
344         icb->cnt = rte_cpu_to_be_32(1);
345         sym_cop->cipher.iv.data = (uint8_t *)icb;
346         sym_cop->cipher.iv.phys_addr = rte_pktmbuf_mtophys_offset(m,
347                          (uint8_t *)icb - rte_pktmbuf_mtod(m, uint8_t *));
348         sym_cop->cipher.iv.length = 16;
349
350         uint8_t *aad;
351
352         switch (sa->auth_algo) {
353         case RTE_CRYPTO_AUTH_NULL:
354         case RTE_CRYPTO_AUTH_SHA1_HMAC:
355                 sym_cop->auth.data.offset = ip_hdr_len;
356                 sym_cop->auth.data.length = sizeof(struct esp_hdr) +
357                         sa->iv_len + pad_payload_len;
358                 break;
359         case RTE_CRYPTO_AUTH_AES_GCM:
360                 aad = get_aad(m);
361                 memcpy(aad, esp, 8);
362                 sym_cop->auth.aad.data = aad;
363                 sym_cop->auth.aad.phys_addr = rte_pktmbuf_mtophys_offset(m,
364                                 aad - rte_pktmbuf_mtod(m, uint8_t *));
365                 sym_cop->auth.aad.length = 8;
366                 break;
367         default:
368                 RTE_LOG(ERR, IPSEC_ESP, "unsupported auth algorithm %u\n",
369                                 sa->auth_algo);
370                 return -EINVAL;
371         }
372
373         sym_cop->auth.digest.data = rte_pktmbuf_mtod_offset(m, uint8_t *,
374                         rte_pktmbuf_pkt_len(m) - sa->digest_len);
375         sym_cop->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
376                         rte_pktmbuf_pkt_len(m) - sa->digest_len);
377         sym_cop->auth.digest.length = sa->digest_len;
378
379         return 0;
380 }
381
382 int
383 esp_outbound_post(struct rte_mbuf *m __rte_unused,
384                 struct ipsec_sa *sa __rte_unused,
385                 struct rte_crypto_op *cop)
386 {
387         RTE_ASSERT(m != NULL);
388         RTE_ASSERT(sa != NULL);
389         RTE_ASSERT(cop != NULL);
390
391         if (cop->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
392                 RTE_LOG(ERR, IPSEC_ESP, "Failed crypto op\n");
393                 return -1;
394         }
395
396         return 0;
397 }