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