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