Merge branch 'drm-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/airlied...
[linux-2.6-block.git] / net / ipv4 / ah4.c
CommitLineData
dff3bb06 1#include <crypto/hash.h>
07d4ee58 2#include <linux/err.h>
1da177e4
LT
3#include <linux/module.h>
4#include <net/ip.h>
5#include <net/xfrm.h>
6#include <net/ah.h>
7#include <linux/crypto.h>
8#include <linux/pfkeyv2.h>
dff3bb06 9#include <linux/scatterlist.h>
1da177e4 10#include <net/icmp.h>
14c85021 11#include <net/protocol.h>
1da177e4 12
dff3bb06
SK
13struct ah_skb_cb {
14 struct xfrm_skb_cb xfrm;
15 void *tmp;
16};
17
18#define AH_SKB_CB(__skb) ((struct ah_skb_cb *)&((__skb)->cb[0]))
19
20static void *ah_alloc_tmp(struct crypto_ahash *ahash, int nfrags,
21 unsigned int size)
22{
23 unsigned int len;
24
25 len = size + crypto_ahash_digestsize(ahash) +
26 (crypto_ahash_alignmask(ahash) &
27 ~(crypto_tfm_ctx_alignment() - 1));
28
29 len = ALIGN(len, crypto_tfm_ctx_alignment());
30
31 len += sizeof(struct ahash_request) + crypto_ahash_reqsize(ahash);
32 len = ALIGN(len, __alignof__(struct scatterlist));
33
34 len += sizeof(struct scatterlist) * nfrags;
35
36 return kmalloc(len, GFP_ATOMIC);
37}
38
39static inline u8 *ah_tmp_auth(void *tmp, unsigned int offset)
40{
41 return tmp + offset;
42}
43
44static inline u8 *ah_tmp_icv(struct crypto_ahash *ahash, void *tmp,
45 unsigned int offset)
46{
47 return PTR_ALIGN((u8 *)tmp + offset, crypto_ahash_alignmask(ahash) + 1);
48}
49
50static inline struct ahash_request *ah_tmp_req(struct crypto_ahash *ahash,
51 u8 *icv)
52{
53 struct ahash_request *req;
54
55 req = (void *)PTR_ALIGN(icv + crypto_ahash_digestsize(ahash),
56 crypto_tfm_ctx_alignment());
57
58 ahash_request_set_tfm(req, ahash);
59
60 return req;
61}
62
63static inline struct scatterlist *ah_req_sg(struct crypto_ahash *ahash,
64 struct ahash_request *req)
65{
66 return (void *)ALIGN((unsigned long)(req + 1) +
67 crypto_ahash_reqsize(ahash),
68 __alignof__(struct scatterlist));
69}
1da177e4
LT
70
71/* Clear mutable options and find final destination to substitute
72 * into IP header for icv calculation. Options are already checked
73 * for validity, so paranoia is not required. */
74
d5a0a1e3 75static int ip_clear_mutable_options(struct iphdr *iph, __be32 *daddr)
1da177e4
LT
76{
77 unsigned char * optptr = (unsigned char*)(iph+1);
78 int l = iph->ihl*4 - sizeof(struct iphdr);
79 int optlen;
80
81 while (l > 0) {
82 switch (*optptr) {
83 case IPOPT_END:
84 return 0;
85 case IPOPT_NOOP:
86 l--;
87 optptr++;
88 continue;
89 }
90 optlen = optptr[1];
91 if (optlen<2 || optlen>l)
92 return -EINVAL;
93 switch (*optptr) {
94 case IPOPT_SEC:
95 case 0x85: /* Some "Extended Security" crap. */
11a03f78 96 case IPOPT_CIPSO:
1da177e4
LT
97 case IPOPT_RA:
98 case 0x80|21: /* RFC1770 */
99 break;
100 case IPOPT_LSRR:
101 case IPOPT_SSRR:
102 if (optlen < 6)
103 return -EINVAL;
104 memcpy(daddr, optptr+optlen-4, 4);
105 /* Fall through */
106 default:
96fe1c02 107 memset(optptr, 0, optlen);
1da177e4
LT
108 }
109 l -= optlen;
110 optptr += optlen;
111 }
112 return 0;
113}
114
dff3bb06
SK
115static void ah_output_done(struct crypto_async_request *base, int err)
116{
117 u8 *icv;
118 struct iphdr *iph;
119 struct sk_buff *skb = base->data;
120 struct xfrm_state *x = skb_dst(skb)->xfrm;
121 struct ah_data *ahp = x->data;
122 struct iphdr *top_iph = ip_hdr(skb);
123 struct ip_auth_hdr *ah = ip_auth_hdr(skb);
124 int ihl = ip_hdrlen(skb);
125
126 iph = AH_SKB_CB(skb)->tmp;
127 icv = ah_tmp_icv(ahp->ahash, iph, ihl);
128 memcpy(ah->auth_data, icv, ahp->icv_trunc_len);
129
130 top_iph->tos = iph->tos;
131 top_iph->ttl = iph->ttl;
132 top_iph->frag_off = iph->frag_off;
133 if (top_iph->ihl != 5) {
134 top_iph->daddr = iph->daddr;
135 memcpy(top_iph+1, iph+1, top_iph->ihl*4 - sizeof(struct iphdr));
136 }
137
138 err = ah->nexthdr;
139
140 kfree(AH_SKB_CB(skb)->tmp);
141 xfrm_output_resume(skb, err);
142}
143
1da177e4
LT
144static int ah_output(struct xfrm_state *x, struct sk_buff *skb)
145{
146 int err;
dff3bb06
SK
147 int nfrags;
148 int ihl;
149 u8 *icv;
150 struct sk_buff *trailer;
151 struct crypto_ahash *ahash;
152 struct ahash_request *req;
153 struct scatterlist *sg;
1da177e4
LT
154 struct iphdr *iph, *top_iph;
155 struct ip_auth_hdr *ah;
156 struct ah_data *ahp;
dff3bb06
SK
157
158 ahp = x->data;
159 ahash = ahp->ahash;
160
161 if ((err = skb_cow_data(skb, 0, &trailer)) < 0)
162 goto out;
163 nfrags = err;
1da177e4 164
7b277b1a 165 skb_push(skb, -skb_network_offset(skb));
dff3bb06
SK
166 ah = ip_auth_hdr(skb);
167 ihl = ip_hdrlen(skb);
168
169 err = -ENOMEM;
170 iph = ah_alloc_tmp(ahash, nfrags, ihl);
171 if (!iph)
172 goto out;
173
174 icv = ah_tmp_icv(ahash, iph, ihl);
175 req = ah_tmp_req(ahash, icv);
176 sg = ah_req_sg(ahash, req);
177
178 memset(ah->auth_data, 0, ahp->icv_trunc_len);
179
eddc9ec5 180 top_iph = ip_hdr(skb);
1da177e4
LT
181
182 iph->tos = top_iph->tos;
183 iph->ttl = top_iph->ttl;
184 iph->frag_off = top_iph->frag_off;
185
186 if (top_iph->ihl != 5) {
187 iph->daddr = top_iph->daddr;
188 memcpy(iph+1, top_iph+1, top_iph->ihl*4 - sizeof(struct iphdr));
189 err = ip_clear_mutable_options(top_iph, &top_iph->daddr);
190 if (err)
dff3bb06 191 goto out_free;
1da177e4
LT
192 }
193
37fedd3a
HX
194 ah->nexthdr = *skb_mac_header(skb);
195 *skb_mac_header(skb) = IPPROTO_AH;
1da177e4
LT
196
197 top_iph->tos = 0;
198 top_iph->tot_len = htons(skb->len);
199 top_iph->frag_off = 0;
200 top_iph->ttl = 0;
1da177e4
LT
201 top_iph->check = 0;
202
87bdc48d 203 ah->hdrlen = (XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len) >> 2) - 2;
1da177e4
LT
204
205 ah->reserved = 0;
206 ah->spi = x->id.spi;
b318e0e4 207 ah->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output);
b7c6538c 208
dff3bb06
SK
209 sg_init_table(sg, nfrags);
210 skb_to_sgvec(skb, sg, 0, skb->len);
b7c6538c 211
dff3bb06
SK
212 ahash_request_set_crypt(req, sg, icv, skb->len);
213 ahash_request_set_callback(req, 0, ah_output_done, skb);
214
215 AH_SKB_CB(skb)->tmp = iph;
216
217 err = crypto_ahash_digest(req);
218 if (err) {
219 if (err == -EINPROGRESS)
220 goto out;
221
222 if (err == -EBUSY)
223 err = NET_XMIT_DROP;
224 goto out_free;
225 }
226
227 memcpy(ah->auth_data, icv, ahp->icv_trunc_len);
1da177e4
LT
228
229 top_iph->tos = iph->tos;
230 top_iph->ttl = iph->ttl;
231 top_iph->frag_off = iph->frag_off;
232 if (top_iph->ihl != 5) {
233 top_iph->daddr = iph->daddr;
234 memcpy(top_iph+1, iph+1, top_iph->ihl*4 - sizeof(struct iphdr));
235 }
236
dff3bb06
SK
237out_free:
238 kfree(iph);
239out:
1da177e4
LT
240 return err;
241}
242
dff3bb06
SK
243static void ah_input_done(struct crypto_async_request *base, int err)
244{
245 u8 *auth_data;
246 u8 *icv;
247 struct iphdr *work_iph;
248 struct sk_buff *skb = base->data;
249 struct xfrm_state *x = xfrm_input_state(skb);
250 struct ah_data *ahp = x->data;
251 struct ip_auth_hdr *ah = ip_auth_hdr(skb);
252 int ihl = ip_hdrlen(skb);
253 int ah_hlen = (ah->hdrlen + 2) << 2;
254
255 work_iph = AH_SKB_CB(skb)->tmp;
256 auth_data = ah_tmp_auth(work_iph, ihl);
257 icv = ah_tmp_icv(ahp->ahash, auth_data, ahp->icv_trunc_len);
258
259 err = memcmp(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG: 0;
260 if (err)
261 goto out;
262
263 skb->network_header += ah_hlen;
264 memcpy(skb_network_header(skb), work_iph, ihl);
265 __skb_pull(skb, ah_hlen + ihl);
266 skb_set_transport_header(skb, -ihl);
267
268 err = ah->nexthdr;
269out:
270 kfree(AH_SKB_CB(skb)->tmp);
271 xfrm_input_resume(skb, err);
272}
273
e695633e 274static int ah_input(struct xfrm_state *x, struct sk_buff *skb)
1da177e4
LT
275{
276 int ah_hlen;
31a4ab93 277 int ihl;
631a6698 278 int nexthdr;
dff3bb06
SK
279 int nfrags;
280 u8 *auth_data;
281 u8 *icv;
282 struct sk_buff *trailer;
283 struct crypto_ahash *ahash;
284 struct ahash_request *req;
285 struct scatterlist *sg;
286 struct iphdr *iph, *work_iph;
1da177e4
LT
287 struct ip_auth_hdr *ah;
288 struct ah_data *ahp;
dff3bb06 289 int err = -ENOMEM;
1da177e4 290
87bdc48d 291 if (!pskb_may_pull(skb, sizeof(*ah)))
1da177e4
LT
292 goto out;
293
87bdc48d 294 ah = (struct ip_auth_hdr *)skb->data;
1da177e4 295 ahp = x->data;
dff3bb06
SK
296 ahash = ahp->ahash;
297
631a6698 298 nexthdr = ah->nexthdr;
1da177e4 299 ah_hlen = (ah->hdrlen + 2) << 2;
e905a9ed 300
87bdc48d
HX
301 if (ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_full_len) &&
302 ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len))
1da177e4
LT
303 goto out;
304
305 if (!pskb_may_pull(skb, ah_hlen))
306 goto out;
307
308 /* We are going to _remove_ AH header to keep sockets happy,
309 * so... Later this can change. */
310 if (skb_cloned(skb) &&
311 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
312 goto out;
313
314 skb->ip_summed = CHECKSUM_NONE;
315
87bdc48d 316 ah = (struct ip_auth_hdr *)skb->data;
eddc9ec5 317 iph = ip_hdr(skb);
dff3bb06
SK
318 ihl = ip_hdrlen(skb);
319
320 if ((err = skb_cow_data(skb, 0, &trailer)) < 0)
321 goto out;
322 nfrags = err;
323
324 work_iph = ah_alloc_tmp(ahash, nfrags, ihl + ahp->icv_trunc_len);
325 if (!work_iph)
326 goto out;
327
328 auth_data = ah_tmp_auth(work_iph, ihl);
329 icv = ah_tmp_icv(ahash, auth_data, ahp->icv_trunc_len);
330 req = ah_tmp_req(ahash, icv);
331 sg = ah_req_sg(ahash, req);
1da177e4 332
dff3bb06
SK
333 memcpy(work_iph, iph, ihl);
334 memcpy(auth_data, ah->auth_data, ahp->icv_trunc_len);
335 memset(ah->auth_data, 0, ahp->icv_trunc_len);
1da177e4
LT
336
337 iph->ttl = 0;
338 iph->tos = 0;
339 iph->frag_off = 0;
340 iph->check = 0;
31a4ab93 341 if (ihl > sizeof(*iph)) {
d5a0a1e3 342 __be32 dummy;
dff3bb06
SK
343 err = ip_clear_mutable_options(iph, &dummy);
344 if (err)
345 goto out_free;
1da177e4 346 }
0ebea8ef 347
dff3bb06 348 skb_push(skb, ihl);
e905a9ed 349
dff3bb06
SK
350 sg_init_table(sg, nfrags);
351 skb_to_sgvec(skb, sg, 0, skb->len);
352
353 ahash_request_set_crypt(req, sg, icv, skb->len);
354 ahash_request_set_callback(req, 0, ah_input_done, skb);
355
356 AH_SKB_CB(skb)->tmp = work_iph;
357
358 err = crypto_ahash_digest(req);
359 if (err) {
360 if (err == -EINPROGRESS)
361 goto out;
362
363 if (err == -EBUSY)
364 err = NET_XMIT_DROP;
365 goto out_free;
1da177e4 366 }
0ebea8ef 367
dff3bb06 368 err = memcmp(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG: 0;
0ebea8ef 369 if (err)
dff3bb06 370 goto out_free;
0ebea8ef 371
b0e380b1 372 skb->network_header += ah_hlen;
dff3bb06 373 memcpy(skb_network_header(skb), work_iph, ihl);
31a4ab93 374 __skb_pull(skb, ah_hlen + ihl);
dff3bb06 375 skb_set_transport_header(skb, -ihl);
1da177e4 376
dff3bb06 377 err = nexthdr;
1da177e4 378
dff3bb06
SK
379out_free:
380 kfree (work_iph);
1da177e4 381out:
07d4ee58 382 return err;
1da177e4
LT
383}
384
385static void ah4_err(struct sk_buff *skb, u32 info)
386{
4fb236ba 387 struct net *net = dev_net(skb->dev);
d9319100
JK
388 struct iphdr *iph = (struct iphdr *)skb->data;
389 struct ip_auth_hdr *ah = (struct ip_auth_hdr *)(skb->data+(iph->ihl<<2));
1da177e4
LT
390 struct xfrm_state *x;
391
88c7664f
ACM
392 if (icmp_hdr(skb)->type != ICMP_DEST_UNREACH ||
393 icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
1da177e4
LT
394 return;
395
4fb236ba 396 x = xfrm_state_lookup(net, (xfrm_address_t *)&iph->daddr, ah->spi, IPPROTO_AH, AF_INET);
1da177e4
LT
397 if (!x)
398 return;
399 printk(KERN_DEBUG "pmtu discovery on SA AH/%08x/%08x\n",
400 ntohl(ah->spi), ntohl(iph->daddr));
401 xfrm_state_put(x);
402}
403
72cb6962 404static int ah_init_state(struct xfrm_state *x)
1da177e4
LT
405{
406 struct ah_data *ahp = NULL;
407 struct xfrm_algo_desc *aalg_desc;
dff3bb06 408 struct crypto_ahash *ahash;
1da177e4
LT
409
410 if (!x->aalg)
411 goto error;
412
1da177e4
LT
413 if (x->encap)
414 goto error;
415
0da974f4 416 ahp = kzalloc(sizeof(*ahp), GFP_KERNEL);
dff3bb06 417 if (!ahp)
1da177e4
LT
418 return -ENOMEM;
419
dff3bb06
SK
420 ahash = crypto_alloc_ahash(x->aalg->alg_name, 0, 0);
421 if (IS_ERR(ahash))
07d4ee58
HX
422 goto error;
423
dff3bb06
SK
424 ahp->ahash = ahash;
425 if (crypto_ahash_setkey(ahash, x->aalg->alg_key,
426 (x->aalg->alg_key_len + 7) / 8))
1da177e4 427 goto error;
e905a9ed 428
1da177e4
LT
429 /*
430 * Lookup the algorithm description maintained by xfrm_algo,
431 * verify crypto transform properties, and store information
432 * we need for AH processing. This lookup cannot fail here
dff3bb06 433 * after a successful crypto_alloc_ahash().
1da177e4
LT
434 */
435 aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
436 BUG_ON(!aalg_desc);
437
438 if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
dff3bb06 439 crypto_ahash_digestsize(ahash)) {
1da177e4 440 printk(KERN_INFO "AH: %s digestsize %u != %hu\n",
dff3bb06 441 x->aalg->alg_name, crypto_ahash_digestsize(ahash),
1da177e4
LT
442 aalg_desc->uinfo.auth.icv_fullbits/8);
443 goto error;
444 }
e905a9ed 445
1da177e4 446 ahp->icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
8f8a088c 447 ahp->icv_trunc_len = x->aalg->alg_trunc_len/8;
e905a9ed 448
1da177e4 449 BUG_ON(ahp->icv_trunc_len > MAX_AH_AUTH_LEN);
e905a9ed 450
87bdc48d
HX
451 x->props.header_len = XFRM_ALIGN8(sizeof(struct ip_auth_hdr) +
452 ahp->icv_trunc_len);
7e49e6de 453 if (x->props.mode == XFRM_MODE_TUNNEL)
1da177e4
LT
454 x->props.header_len += sizeof(struct iphdr);
455 x->data = ahp;
456
457 return 0;
458
459error:
460 if (ahp) {
dff3bb06 461 crypto_free_ahash(ahp->ahash);
1da177e4
LT
462 kfree(ahp);
463 }
464 return -EINVAL;
465}
466
467static void ah_destroy(struct xfrm_state *x)
468{
469 struct ah_data *ahp = x->data;
470
471 if (!ahp)
472 return;
473
dff3bb06 474 crypto_free_ahash(ahp->ahash);
1da177e4
LT
475 kfree(ahp);
476}
477
478
533cb5b0 479static const struct xfrm_type ah_type =
1da177e4
LT
480{
481 .description = "AH4",
482 .owner = THIS_MODULE,
483 .proto = IPPROTO_AH,
436a0a40 484 .flags = XFRM_TYPE_REPLAY_PROT,
1da177e4
LT
485 .init_state = ah_init_state,
486 .destructor = ah_destroy,
487 .input = ah_input,
488 .output = ah_output
489};
490
32613090 491static const struct net_protocol ah4_protocol = {
1da177e4
LT
492 .handler = xfrm4_rcv,
493 .err_handler = ah4_err,
494 .no_policy = 1,
4fb236ba 495 .netns_ok = 1,
1da177e4
LT
496};
497
498static int __init ah4_init(void)
499{
500 if (xfrm_register_type(&ah_type, AF_INET) < 0) {
501 printk(KERN_INFO "ip ah init: can't add xfrm type\n");
502 return -EAGAIN;
503 }
504 if (inet_add_protocol(&ah4_protocol, IPPROTO_AH) < 0) {
505 printk(KERN_INFO "ip ah init: can't add protocol\n");
506 xfrm_unregister_type(&ah_type, AF_INET);
507 return -EAGAIN;
508 }
509 return 0;
510}
511
512static void __exit ah4_fini(void)
513{
514 if (inet_del_protocol(&ah4_protocol, IPPROTO_AH) < 0)
515 printk(KERN_INFO "ip ah close: can't remove protocol\n");
516 if (xfrm_unregister_type(&ah_type, AF_INET) < 0)
517 printk(KERN_INFO "ip ah close: can't remove xfrm type\n");
518}
519
520module_init(ah4_init);
521module_exit(ah4_fini);
522MODULE_LICENSE("GPL");
d3d6dd3a 523MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_AH);