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