xref: /openbmc/linux/net/ipv4/esp4.c (revision 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2)
1 #include <linux/config.h>
2 #include <linux/module.h>
3 #include <net/ip.h>
4 #include <net/xfrm.h>
5 #include <net/esp.h>
6 #include <asm/scatterlist.h>
7 #include <linux/crypto.h>
8 #include <linux/pfkeyv2.h>
9 #include <linux/random.h>
10 #include <net/icmp.h>
11 #include <net/udp.h>
12 
13 /* decapsulation data for use when post-processing */
14 struct esp_decap_data {
15 	xfrm_address_t	saddr;
16 	__u16		sport;
17 	__u8		proto;
18 };
19 
20 static int esp_output(struct xfrm_state *x, struct sk_buff *skb)
21 {
22 	int err;
23 	struct iphdr *top_iph;
24 	struct ip_esp_hdr *esph;
25 	struct crypto_tfm *tfm;
26 	struct esp_data *esp;
27 	struct sk_buff *trailer;
28 	int blksize;
29 	int clen;
30 	int alen;
31 	int nfrags;
32 
33 	/* Strip IP+ESP header. */
34 	__skb_pull(skb, skb->h.raw - skb->data);
35 	/* Now skb is pure payload to encrypt */
36 
37 	err = -ENOMEM;
38 
39 	/* Round to block size */
40 	clen = skb->len;
41 
42 	esp = x->data;
43 	alen = esp->auth.icv_trunc_len;
44 	tfm = esp->conf.tfm;
45 	blksize = (crypto_tfm_alg_blocksize(tfm) + 3) & ~3;
46 	clen = (clen + 2 + blksize-1)&~(blksize-1);
47 	if (esp->conf.padlen)
48 		clen = (clen + esp->conf.padlen-1)&~(esp->conf.padlen-1);
49 
50 	if ((nfrags = skb_cow_data(skb, clen-skb->len+alen, &trailer)) < 0)
51 		goto error;
52 
53 	/* Fill padding... */
54 	do {
55 		int i;
56 		for (i=0; i<clen-skb->len - 2; i++)
57 			*(u8*)(trailer->tail + i) = i+1;
58 	} while (0);
59 	*(u8*)(trailer->tail + clen-skb->len - 2) = (clen - skb->len)-2;
60 	pskb_put(skb, trailer, clen - skb->len);
61 
62 	__skb_push(skb, skb->data - skb->nh.raw);
63 	top_iph = skb->nh.iph;
64 	esph = (struct ip_esp_hdr *)(skb->nh.raw + top_iph->ihl*4);
65 	top_iph->tot_len = htons(skb->len + alen);
66 	*(u8*)(trailer->tail - 1) = top_iph->protocol;
67 
68 	/* this is non-NULL only with UDP Encapsulation */
69 	if (x->encap) {
70 		struct xfrm_encap_tmpl *encap = x->encap;
71 		struct udphdr *uh;
72 		u32 *udpdata32;
73 
74 		uh = (struct udphdr *)esph;
75 		uh->source = encap->encap_sport;
76 		uh->dest = encap->encap_dport;
77 		uh->len = htons(skb->len + alen - top_iph->ihl*4);
78 		uh->check = 0;
79 
80 		switch (encap->encap_type) {
81 		default:
82 		case UDP_ENCAP_ESPINUDP:
83 			esph = (struct ip_esp_hdr *)(uh + 1);
84 			break;
85 		case UDP_ENCAP_ESPINUDP_NON_IKE:
86 			udpdata32 = (u32 *)(uh + 1);
87 			udpdata32[0] = udpdata32[1] = 0;
88 			esph = (struct ip_esp_hdr *)(udpdata32 + 2);
89 			break;
90 		}
91 
92 		top_iph->protocol = IPPROTO_UDP;
93 	} else
94 		top_iph->protocol = IPPROTO_ESP;
95 
96 	esph->spi = x->id.spi;
97 	esph->seq_no = htonl(++x->replay.oseq);
98 
99 	if (esp->conf.ivlen)
100 		crypto_cipher_set_iv(tfm, esp->conf.ivec, crypto_tfm_alg_ivsize(tfm));
101 
102 	do {
103 		struct scatterlist *sg = &esp->sgbuf[0];
104 
105 		if (unlikely(nfrags > ESP_NUM_FAST_SG)) {
106 			sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
107 			if (!sg)
108 				goto error;
109 		}
110 		skb_to_sgvec(skb, sg, esph->enc_data+esp->conf.ivlen-skb->data, clen);
111 		crypto_cipher_encrypt(tfm, sg, sg, clen);
112 		if (unlikely(sg != &esp->sgbuf[0]))
113 			kfree(sg);
114 	} while (0);
115 
116 	if (esp->conf.ivlen) {
117 		memcpy(esph->enc_data, esp->conf.ivec, crypto_tfm_alg_ivsize(tfm));
118 		crypto_cipher_get_iv(tfm, esp->conf.ivec, crypto_tfm_alg_ivsize(tfm));
119 	}
120 
121 	if (esp->auth.icv_full_len) {
122 		esp->auth.icv(esp, skb, (u8*)esph-skb->data,
123 		              sizeof(struct ip_esp_hdr) + esp->conf.ivlen+clen, trailer->tail);
124 		pskb_put(skb, trailer, alen);
125 	}
126 
127 	ip_send_check(top_iph);
128 
129 	err = 0;
130 
131 error:
132 	return err;
133 }
134 
135 /*
136  * Note: detecting truncated vs. non-truncated authentication data is very
137  * expensive, so we only support truncated data, which is the recommended
138  * and common case.
139  */
140 static int esp_input(struct xfrm_state *x, struct xfrm_decap_state *decap, struct sk_buff *skb)
141 {
142 	struct iphdr *iph;
143 	struct ip_esp_hdr *esph;
144 	struct esp_data *esp = x->data;
145 	struct sk_buff *trailer;
146 	int blksize = crypto_tfm_alg_blocksize(esp->conf.tfm);
147 	int alen = esp->auth.icv_trunc_len;
148 	int elen = skb->len - sizeof(struct ip_esp_hdr) - esp->conf.ivlen - alen;
149 	int nfrags;
150 	int encap_len = 0;
151 
152 	if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr)))
153 		goto out;
154 
155 	if (elen <= 0 || (elen & (blksize-1)))
156 		goto out;
157 
158 	/* If integrity check is required, do this. */
159 	if (esp->auth.icv_full_len) {
160 		u8 sum[esp->auth.icv_full_len];
161 		u8 sum1[alen];
162 
163 		esp->auth.icv(esp, skb, 0, skb->len-alen, sum);
164 
165 		if (skb_copy_bits(skb, skb->len-alen, sum1, alen))
166 			BUG();
167 
168 		if (unlikely(memcmp(sum, sum1, alen))) {
169 			x->stats.integrity_failed++;
170 			goto out;
171 		}
172 	}
173 
174 	if ((nfrags = skb_cow_data(skb, 0, &trailer)) < 0)
175 		goto out;
176 
177 	skb->ip_summed = CHECKSUM_NONE;
178 
179 	esph = (struct ip_esp_hdr*)skb->data;
180 	iph = skb->nh.iph;
181 
182 	/* Get ivec. This can be wrong, check against another impls. */
183 	if (esp->conf.ivlen)
184 		crypto_cipher_set_iv(esp->conf.tfm, esph->enc_data, crypto_tfm_alg_ivsize(esp->conf.tfm));
185 
186         {
187 		u8 nexthdr[2];
188 		struct scatterlist *sg = &esp->sgbuf[0];
189 		u8 workbuf[60];
190 		int padlen;
191 
192 		if (unlikely(nfrags > ESP_NUM_FAST_SG)) {
193 			sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
194 			if (!sg)
195 				goto out;
196 		}
197 		skb_to_sgvec(skb, sg, sizeof(struct ip_esp_hdr) + esp->conf.ivlen, elen);
198 		crypto_cipher_decrypt(esp->conf.tfm, sg, sg, elen);
199 		if (unlikely(sg != &esp->sgbuf[0]))
200 			kfree(sg);
201 
202 		if (skb_copy_bits(skb, skb->len-alen-2, nexthdr, 2))
203 			BUG();
204 
205 		padlen = nexthdr[0];
206 		if (padlen+2 >= elen)
207 			goto out;
208 
209 		/* ... check padding bits here. Silly. :-) */
210 
211 		if (x->encap && decap && decap->decap_type) {
212 			struct esp_decap_data *encap_data;
213 			struct udphdr *uh = (struct udphdr *) (iph+1);
214 
215 			encap_data = (struct esp_decap_data *) (decap->decap_data);
216 			encap_data->proto = 0;
217 
218 			switch (decap->decap_type) {
219 			case UDP_ENCAP_ESPINUDP:
220 			case UDP_ENCAP_ESPINUDP_NON_IKE:
221 				encap_data->proto = AF_INET;
222 				encap_data->saddr.a4 = iph->saddr;
223 				encap_data->sport = uh->source;
224 				encap_len = (void*)esph - (void*)uh;
225 				break;
226 
227 			default:
228 				goto out;
229 			}
230 		}
231 
232 		iph->protocol = nexthdr[1];
233 		pskb_trim(skb, skb->len - alen - padlen - 2);
234 		memcpy(workbuf, skb->nh.raw, iph->ihl*4);
235 		skb->h.raw = skb_pull(skb, sizeof(struct ip_esp_hdr) + esp->conf.ivlen);
236 		skb->nh.raw += encap_len + sizeof(struct ip_esp_hdr) + esp->conf.ivlen;
237 		memcpy(skb->nh.raw, workbuf, iph->ihl*4);
238 		skb->nh.iph->tot_len = htons(skb->len);
239 	}
240 
241 	return 0;
242 
243 out:
244 	return -EINVAL;
245 }
246 
247 static int esp_post_input(struct xfrm_state *x, struct xfrm_decap_state *decap, struct sk_buff *skb)
248 {
249 
250 	if (x->encap) {
251 		struct xfrm_encap_tmpl *encap;
252 		struct esp_decap_data *decap_data;
253 
254 		encap = x->encap;
255 		decap_data = (struct esp_decap_data *)(decap->decap_data);
256 
257 		/* first, make sure that the decap type == the encap type */
258 		if (encap->encap_type != decap->decap_type)
259 			return -EINVAL;
260 
261 		switch (encap->encap_type) {
262 		default:
263 		case UDP_ENCAP_ESPINUDP:
264 		case UDP_ENCAP_ESPINUDP_NON_IKE:
265 			/*
266 			 * 1) if the NAT-T peer's IP or port changed then
267 			 *    advertize the change to the keying daemon.
268 			 *    This is an inbound SA, so just compare
269 			 *    SRC ports.
270 			 */
271 			if (decap_data->proto == AF_INET &&
272 			    (decap_data->saddr.a4 != x->props.saddr.a4 ||
273 			     decap_data->sport != encap->encap_sport)) {
274 				xfrm_address_t ipaddr;
275 
276 				ipaddr.a4 = decap_data->saddr.a4;
277 				km_new_mapping(x, &ipaddr, decap_data->sport);
278 
279 				/* XXX: perhaps add an extra
280 				 * policy check here, to see
281 				 * if we should allow or
282 				 * reject a packet from a
283 				 * different source
284 				 * address/port.
285 				 */
286 			}
287 
288 			/*
289 			 * 2) ignore UDP/TCP checksums in case
290 			 *    of NAT-T in Transport Mode, or
291 			 *    perform other post-processing fixes
292 			 *    as per * draft-ietf-ipsec-udp-encaps-06,
293 			 *    section 3.1.2
294 			 */
295 			if (!x->props.mode)
296 				skb->ip_summed = CHECKSUM_UNNECESSARY;
297 
298 			break;
299 		}
300 	}
301 	return 0;
302 }
303 
304 static u32 esp4_get_max_size(struct xfrm_state *x, int mtu)
305 {
306 	struct esp_data *esp = x->data;
307 	u32 blksize = crypto_tfm_alg_blocksize(esp->conf.tfm);
308 
309 	if (x->props.mode) {
310 		mtu = (mtu + 2 + blksize-1)&~(blksize-1);
311 	} else {
312 		/* The worst case. */
313 		mtu += 2 + blksize;
314 	}
315 	if (esp->conf.padlen)
316 		mtu = (mtu + esp->conf.padlen-1)&~(esp->conf.padlen-1);
317 
318 	return mtu + x->props.header_len + esp->auth.icv_trunc_len;
319 }
320 
321 static void esp4_err(struct sk_buff *skb, u32 info)
322 {
323 	struct iphdr *iph = (struct iphdr*)skb->data;
324 	struct ip_esp_hdr *esph = (struct ip_esp_hdr*)(skb->data+(iph->ihl<<2));
325 	struct xfrm_state *x;
326 
327 	if (skb->h.icmph->type != ICMP_DEST_UNREACH ||
328 	    skb->h.icmph->code != ICMP_FRAG_NEEDED)
329 		return;
330 
331 	x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, esph->spi, IPPROTO_ESP, AF_INET);
332 	if (!x)
333 		return;
334 	NETDEBUG(printk(KERN_DEBUG "pmtu discovery on SA ESP/%08x/%08x\n",
335 			ntohl(esph->spi), ntohl(iph->daddr)));
336 	xfrm_state_put(x);
337 }
338 
339 static void esp_destroy(struct xfrm_state *x)
340 {
341 	struct esp_data *esp = x->data;
342 
343 	if (!esp)
344 		return;
345 
346 	if (esp->conf.tfm) {
347 		crypto_free_tfm(esp->conf.tfm);
348 		esp->conf.tfm = NULL;
349 	}
350 	if (esp->conf.ivec) {
351 		kfree(esp->conf.ivec);
352 		esp->conf.ivec = NULL;
353 	}
354 	if (esp->auth.tfm) {
355 		crypto_free_tfm(esp->auth.tfm);
356 		esp->auth.tfm = NULL;
357 	}
358 	if (esp->auth.work_icv) {
359 		kfree(esp->auth.work_icv);
360 		esp->auth.work_icv = NULL;
361 	}
362 	kfree(esp);
363 }
364 
365 static int esp_init_state(struct xfrm_state *x, void *args)
366 {
367 	struct esp_data *esp = NULL;
368 
369 	/* null auth and encryption can have zero length keys */
370 	if (x->aalg) {
371 		if (x->aalg->alg_key_len > 512)
372 			goto error;
373 	}
374 	if (x->ealg == NULL)
375 		goto error;
376 
377 	esp = kmalloc(sizeof(*esp), GFP_KERNEL);
378 	if (esp == NULL)
379 		return -ENOMEM;
380 
381 	memset(esp, 0, sizeof(*esp));
382 
383 	if (x->aalg) {
384 		struct xfrm_algo_desc *aalg_desc;
385 
386 		esp->auth.key = x->aalg->alg_key;
387 		esp->auth.key_len = (x->aalg->alg_key_len+7)/8;
388 		esp->auth.tfm = crypto_alloc_tfm(x->aalg->alg_name, 0);
389 		if (esp->auth.tfm == NULL)
390 			goto error;
391 		esp->auth.icv = esp_hmac_digest;
392 
393 		aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
394 		BUG_ON(!aalg_desc);
395 
396 		if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
397 		    crypto_tfm_alg_digestsize(esp->auth.tfm)) {
398 			NETDEBUG(printk(KERN_INFO "ESP: %s digestsize %u != %hu\n",
399 			       x->aalg->alg_name,
400 			       crypto_tfm_alg_digestsize(esp->auth.tfm),
401 			       aalg_desc->uinfo.auth.icv_fullbits/8));
402 			goto error;
403 		}
404 
405 		esp->auth.icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
406 		esp->auth.icv_trunc_len = aalg_desc->uinfo.auth.icv_truncbits/8;
407 
408 		esp->auth.work_icv = kmalloc(esp->auth.icv_full_len, GFP_KERNEL);
409 		if (!esp->auth.work_icv)
410 			goto error;
411 	}
412 	esp->conf.key = x->ealg->alg_key;
413 	esp->conf.key_len = (x->ealg->alg_key_len+7)/8;
414 	if (x->props.ealgo == SADB_EALG_NULL)
415 		esp->conf.tfm = crypto_alloc_tfm(x->ealg->alg_name, CRYPTO_TFM_MODE_ECB);
416 	else
417 		esp->conf.tfm = crypto_alloc_tfm(x->ealg->alg_name, CRYPTO_TFM_MODE_CBC);
418 	if (esp->conf.tfm == NULL)
419 		goto error;
420 	esp->conf.ivlen = crypto_tfm_alg_ivsize(esp->conf.tfm);
421 	esp->conf.padlen = 0;
422 	if (esp->conf.ivlen) {
423 		esp->conf.ivec = kmalloc(esp->conf.ivlen, GFP_KERNEL);
424 		if (unlikely(esp->conf.ivec == NULL))
425 			goto error;
426 		get_random_bytes(esp->conf.ivec, esp->conf.ivlen);
427 	}
428 	if (crypto_cipher_setkey(esp->conf.tfm, esp->conf.key, esp->conf.key_len))
429 		goto error;
430 	x->props.header_len = sizeof(struct ip_esp_hdr) + esp->conf.ivlen;
431 	if (x->props.mode)
432 		x->props.header_len += sizeof(struct iphdr);
433 	if (x->encap) {
434 		struct xfrm_encap_tmpl *encap = x->encap;
435 
436 		switch (encap->encap_type) {
437 		default:
438 			goto error;
439 		case UDP_ENCAP_ESPINUDP:
440 			x->props.header_len += sizeof(struct udphdr);
441 			break;
442 		case UDP_ENCAP_ESPINUDP_NON_IKE:
443 			x->props.header_len += sizeof(struct udphdr) + 2 * sizeof(u32);
444 			break;
445 		}
446 	}
447 	x->data = esp;
448 	x->props.trailer_len = esp4_get_max_size(x, 0) - x->props.header_len;
449 	return 0;
450 
451 error:
452 	x->data = esp;
453 	esp_destroy(x);
454 	x->data = NULL;
455 	return -EINVAL;
456 }
457 
458 static struct xfrm_type esp_type =
459 {
460 	.description	= "ESP4",
461 	.owner		= THIS_MODULE,
462 	.proto	     	= IPPROTO_ESP,
463 	.init_state	= esp_init_state,
464 	.destructor	= esp_destroy,
465 	.get_max_size	= esp4_get_max_size,
466 	.input		= esp_input,
467 	.post_input	= esp_post_input,
468 	.output		= esp_output
469 };
470 
471 static struct net_protocol esp4_protocol = {
472 	.handler	=	xfrm4_rcv,
473 	.err_handler	=	esp4_err,
474 	.no_policy	=	1,
475 };
476 
477 static int __init esp4_init(void)
478 {
479 	struct xfrm_decap_state decap;
480 
481 	if (sizeof(struct esp_decap_data)  <
482 	    sizeof(decap.decap_data)) {
483 		extern void decap_data_too_small(void);
484 
485 		decap_data_too_small();
486 	}
487 
488 	if (xfrm_register_type(&esp_type, AF_INET) < 0) {
489 		printk(KERN_INFO "ip esp init: can't add xfrm type\n");
490 		return -EAGAIN;
491 	}
492 	if (inet_add_protocol(&esp4_protocol, IPPROTO_ESP) < 0) {
493 		printk(KERN_INFO "ip esp init: can't add protocol\n");
494 		xfrm_unregister_type(&esp_type, AF_INET);
495 		return -EAGAIN;
496 	}
497 	return 0;
498 }
499 
500 static void __exit esp4_fini(void)
501 {
502 	if (inet_del_protocol(&esp4_protocol, IPPROTO_ESP) < 0)
503 		printk(KERN_INFO "ip esp close: can't remove protocol\n");
504 	if (xfrm_unregister_type(&esp_type, AF_INET) < 0)
505 		printk(KERN_INFO "ip esp close: can't remove xfrm type\n");
506 }
507 
508 module_init(esp4_init);
509 module_exit(esp4_fini);
510 MODULE_LICENSE("GPL");
511