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