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