examples/ipsec-secgw: support IPv6
[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_memcpy.h>
46 #include <rte_crypto.h>
47 #include <rte_cryptodev.h>
48 #include <rte_random.h>
49
50 #include "ipsec.h"
51 #include "esp.h"
52 #include "ipip.h"
53
54 static inline void
55 random_iv_u64(uint64_t *buf, uint16_t n)
56 {
57         uint32_t left = n & 0x7;
58         uint32_t i;
59
60         RTE_ASSERT((n & 0x3) == 0);
61
62         for (i = 0; i < (n >> 3); i++)
63                 buf[i] = rte_rand();
64
65         if (left)
66                 *((uint32_t *)&buf[i]) = (uint32_t)lrand48();
67 }
68
69 int
70 esp_inbound(struct rte_mbuf *m, struct ipsec_sa *sa,
71                 struct rte_crypto_op *cop)
72 {
73         int32_t payload_len, ip_hdr_len;
74         struct rte_crypto_sym_op *sym_cop;
75
76         RTE_ASSERT(m != NULL);
77         RTE_ASSERT(sa != NULL);
78         RTE_ASSERT(cop != NULL);
79
80         ip_hdr_len = 0;
81         switch (sa->flags) {
82         case IP4_TUNNEL:
83                 ip_hdr_len = sizeof(struct ip);
84                 break;
85         case IP6_TUNNEL:
86                 ip_hdr_len = sizeof(struct ip6_hdr);
87                 break;
88         }
89
90         payload_len = rte_pktmbuf_pkt_len(m) - ip_hdr_len -
91                 sizeof(struct esp_hdr) - sa->iv_len - sa->digest_len;
92
93         if ((payload_len & (sa->block_size - 1)) || (payload_len <= 0)) {
94                 RTE_LOG(DEBUG, IPSEC_ESP, "payload %d not multiple of %u\n",
95                                 payload_len, sa->block_size);
96                 return -EINVAL;
97         }
98
99         sym_cop = (struct rte_crypto_sym_op *)(cop + 1);
100
101         sym_cop->m_src = m;
102         sym_cop->cipher.data.offset =  ip_hdr_len + sizeof(struct esp_hdr) +
103                 sa->iv_len;
104         sym_cop->cipher.data.length = payload_len;
105
106         sym_cop->cipher.iv.data = rte_pktmbuf_mtod_offset(m, void*,
107                          ip_hdr_len + sizeof(struct esp_hdr));
108         sym_cop->cipher.iv.phys_addr = rte_pktmbuf_mtophys_offset(m,
109                          ip_hdr_len + sizeof(struct esp_hdr));
110         sym_cop->cipher.iv.length = sa->iv_len;
111
112         sym_cop->auth.data.offset = ip_hdr_len;
113         sym_cop->auth.data.length = sizeof(struct esp_hdr) +
114                 sa->iv_len + payload_len;
115
116         sym_cop->auth.digest.data = rte_pktmbuf_mtod_offset(m, void*,
117                         rte_pktmbuf_pkt_len(m) - sa->digest_len);
118         sym_cop->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
119                         rte_pktmbuf_pkt_len(m) - sa->digest_len);
120         sym_cop->auth.digest.length = sa->digest_len;
121
122         return 0;
123 }
124
125 int
126 esp_inbound_post(struct rte_mbuf *m, struct ipsec_sa *sa,
127                 struct rte_crypto_op *cop)
128 {
129         uint8_t *nexthdr, *pad_len;
130         uint8_t *padding;
131         uint16_t i;
132
133         RTE_ASSERT(m != NULL);
134         RTE_ASSERT(sa != NULL);
135         RTE_ASSERT(cop != NULL);
136
137         if (cop->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
138                 RTE_LOG(ERR, IPSEC_ESP, "Failed crypto op\n");
139                 return -1;
140         }
141
142         nexthdr = rte_pktmbuf_mtod_offset(m, uint8_t*,
143                         rte_pktmbuf_pkt_len(m) - sa->digest_len - 1);
144         pad_len = nexthdr - 1;
145
146         padding = pad_len - *pad_len;
147         for (i = 0; i < *pad_len; i++) {
148                 if (padding[i] != i + 1) {
149                         RTE_LOG(ERR, IPSEC_ESP, "invalid pad_len field\n");
150                         return -EINVAL;
151                 }
152         }
153
154         if (rte_pktmbuf_trim(m, *pad_len + 2 + sa->digest_len)) {
155                 RTE_LOG(ERR, IPSEC_ESP,
156                                 "failed to remove pad_len + digest\n");
157                 return -EINVAL;
158         }
159
160         ipip_inbound(m, sizeof(struct esp_hdr) + sa->iv_len);
161
162         return 0;
163 }
164
165 int
166 esp_outbound(struct rte_mbuf *m, struct ipsec_sa *sa,
167                 struct rte_crypto_op *cop)
168 {
169         uint16_t pad_payload_len, pad_len, ip_hdr_len;
170         struct ip *ip4;
171         struct ip6_hdr *ip6;
172         struct esp_hdr *esp;
173         int32_t i;
174         char *padding;
175         struct rte_crypto_sym_op *sym_cop;
176
177         RTE_ASSERT(m != NULL);
178         RTE_ASSERT(sa != NULL);
179         RTE_ASSERT(cop != NULL);
180
181         /* Payload length */
182         pad_payload_len = RTE_ALIGN_CEIL(rte_pktmbuf_pkt_len(m) + 2,
183                         sa->block_size);
184         pad_len = pad_payload_len - rte_pktmbuf_pkt_len(m);
185
186         ip_hdr_len = 0;
187         switch (sa->flags) {
188         case IP4_TUNNEL:
189                 ip_hdr_len = sizeof(struct ip);
190                 break;
191         case IP6_TUNNEL:
192                 ip_hdr_len = sizeof(struct ip6_hdr);
193                 break;
194         }
195
196         /* Check maximum packet size */
197         if (unlikely(ip_hdr_len + sizeof(struct esp_hdr) + sa->iv_len +
198                         pad_payload_len + sa->digest_len > IP_MAXPACKET)) {
199                 RTE_LOG(ERR, IPSEC_ESP, "ipsec packet is too big\n");
200                 return -EINVAL;
201         }
202
203         padding = rte_pktmbuf_append(m, pad_len + sa->digest_len);
204         if (unlikely(padding == NULL)) {
205                 RTE_LOG(ERR, IPSEC_ESP, "not enough mbuf trailing space\n");
206                 return -ENOSPC;
207         }
208         rte_prefetch0(padding);
209
210         switch (sa->flags) {
211         case IP4_TUNNEL:
212                 ip4 = ip4ip_outbound(m, sizeof(struct esp_hdr) + sa->iv_len,
213                                 &sa->src, &sa->dst);
214                 esp = (struct esp_hdr *)(ip4 + 1);
215                 break;
216         case IP6_TUNNEL:
217                 ip6 = ip6ip_outbound(m, sizeof(struct esp_hdr) + sa->iv_len,
218                                 &sa->src, &sa->dst);
219                 esp = (struct esp_hdr *)(ip6 + 1);
220                 break;
221         default:
222                 RTE_LOG(ERR, IPSEC_ESP, "Unsupported SA flags: 0x%x\n",
223                                 sa->flags);
224                 return -EINVAL;
225         }
226
227         sa->seq++;
228         esp->spi = rte_cpu_to_be_32(sa->spi);
229         esp->seq = rte_cpu_to_be_32(sa->seq);
230
231         if (sa->cipher_algo == RTE_CRYPTO_CIPHER_AES_CBC)
232                 random_iv_u64((uint64_t *)(esp + 1), sa->iv_len);
233
234         /* Fill pad_len using default sequential scheme */
235         for (i = 0; i < pad_len - 2; i++)
236                 padding[i] = i + 1;
237         padding[pad_len - 2] = pad_len - 2;
238
239         if (RTE_ETH_IS_IPV4_HDR(m->packet_type))
240                 padding[pad_len - 1] = IPPROTO_IPIP;
241         else
242                 padding[pad_len - 1] = IPPROTO_IPV6;
243
244         sym_cop = (struct rte_crypto_sym_op *)(cop + 1);
245
246         sym_cop->m_src = m;
247         sym_cop->cipher.data.offset = ip_hdr_len + sizeof(struct esp_hdr) +
248                         sa->iv_len;
249         sym_cop->cipher.data.length = pad_payload_len;
250
251         sym_cop->cipher.iv.data = rte_pktmbuf_mtod_offset(m, uint8_t *,
252                          ip_hdr_len + sizeof(struct esp_hdr));
253         sym_cop->cipher.iv.phys_addr = rte_pktmbuf_mtophys_offset(m,
254                          ip_hdr_len + sizeof(struct esp_hdr));
255         sym_cop->cipher.iv.length = sa->iv_len;
256
257         sym_cop->auth.data.offset = ip_hdr_len;
258         sym_cop->auth.data.length = sizeof(struct esp_hdr) + sa->iv_len +
259                 pad_payload_len;
260
261         sym_cop->auth.digest.data = rte_pktmbuf_mtod_offset(m, uint8_t *,
262                         rte_pktmbuf_pkt_len(m) - sa->digest_len);
263         sym_cop->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
264                         rte_pktmbuf_pkt_len(m) - sa->digest_len);
265         sym_cop->auth.digest.length = sa->digest_len;
266
267         return 0;
268 }
269
270 int
271 esp_outbound_post(struct rte_mbuf *m __rte_unused,
272                 struct ipsec_sa *sa __rte_unused,
273                 struct rte_crypto_op *cop)
274 {
275         RTE_ASSERT(m != NULL);
276         RTE_ASSERT(sa != NULL);
277         RTE_ASSERT(cop != NULL);
278
279         if (cop->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
280                 RTE_LOG(ERR, IPSEC_ESP, "Failed crypto op\n");
281                 return -1;
282         }
283
284         return 0;
285 }