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