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