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