blob: c2f300c314be8f37699d220fbb6ab5e62039990a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C)2002 USAGI/WIDE Project
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090013 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Authors
19 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090020 * Mitsuru KANDA @USAGI : IPv6 Support
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 * Kazunori MIYAZAWA @USAGI :
22 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090023 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * This file is derived from net/ipv4/ah.c.
25 */
26
Steffen Klassert8631e9b2009-10-07 22:49:01 +000027#include <crypto/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/module.h>
29#include <net/ip.h>
30#include <net/ah.h>
31#include <linux/crypto.h>
32#include <linux/pfkeyv2.h>
33#include <linux/string.h>
Steffen Klassert8631e9b2009-10-07 22:49:01 +000034#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <net/icmp.h>
36#include <net/ipv6.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020037#include <net/protocol.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <net/xfrm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Steffen Klassert8631e9b2009-10-07 22:49:01 +000040#define IPV6HDR_BASELEN 8
41
42struct tmp_ext {
43#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
44 struct in6_addr saddr;
45#endif
46 struct in6_addr daddr;
47 char hdrs[0];
48};
49
50struct ah_skb_cb {
51 struct xfrm_skb_cb xfrm;
52 void *tmp;
53};
54
55#define AH_SKB_CB(__skb) ((struct ah_skb_cb *)&((__skb)->cb[0]))
56
57static void *ah_alloc_tmp(struct crypto_ahash *ahash, int nfrags,
58 unsigned int size)
59{
60 unsigned int len;
61
62 len = size + crypto_ahash_digestsize(ahash) +
63 (crypto_ahash_alignmask(ahash) &
64 ~(crypto_tfm_ctx_alignment() - 1));
65
66 len = ALIGN(len, crypto_tfm_ctx_alignment());
67
68 len += sizeof(struct ahash_request) + crypto_ahash_reqsize(ahash);
69 len = ALIGN(len, __alignof__(struct scatterlist));
70
71 len += sizeof(struct scatterlist) * nfrags;
72
73 return kmalloc(len, GFP_ATOMIC);
74}
75
76static inline struct tmp_ext *ah_tmp_ext(void *base)
77{
78 return base + IPV6HDR_BASELEN;
79}
80
81static inline u8 *ah_tmp_auth(u8 *tmp, unsigned int offset)
82{
83 return tmp + offset;
84}
85
86static inline u8 *ah_tmp_icv(struct crypto_ahash *ahash, void *tmp,
87 unsigned int offset)
88{
89 return PTR_ALIGN((u8 *)tmp + offset, crypto_ahash_alignmask(ahash) + 1);
90}
91
92static inline struct ahash_request *ah_tmp_req(struct crypto_ahash *ahash,
93 u8 *icv)
94{
95 struct ahash_request *req;
96
97 req = (void *)PTR_ALIGN(icv + crypto_ahash_digestsize(ahash),
98 crypto_tfm_ctx_alignment());
99
100 ahash_request_set_tfm(req, ahash);
101
102 return req;
103}
104
105static inline struct scatterlist *ah_req_sg(struct crypto_ahash *ahash,
106 struct ahash_request *req)
107{
108 return (void *)ALIGN((unsigned long)(req + 1) +
109 crypto_ahash_reqsize(ahash),
110 __alignof__(struct scatterlist));
111}
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113static int zero_out_mutable_opts(struct ipv6_opt_hdr *opthdr)
114{
115 u8 *opt = (u8 *)opthdr;
116 int len = ipv6_optlen(opthdr);
117 int off = 0;
118 int optlen = 0;
119
120 off += 2;
121 len -= 2;
122
123 while (len > 0) {
124
125 switch (opt[off]) {
126
127 case IPV6_TLV_PAD0:
128 optlen = 1;
129 break;
130 default:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900131 if (len < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 goto bad;
133 optlen = opt[off+1]+2;
134 if (len < optlen)
135 goto bad;
136 if (opt[off] & 0x20)
137 memset(&opt[off+2], 0, opt[off+1]);
138 break;
139 }
140
141 off += optlen;
142 len -= optlen;
143 }
144 if (len == 0)
145 return 1;
146
147bad:
148 return 0;
149}
150
Masahide NAKAMURA59fbb3a62007-06-26 23:56:32 -0700151#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Masahide NAKAMURA27637df2006-08-23 19:29:47 -0700152/**
153 * ipv6_rearrange_destopt - rearrange IPv6 destination options header
154 * @iph: IPv6 header
155 * @destopt: destionation options header
156 */
157static void ipv6_rearrange_destopt(struct ipv6hdr *iph, struct ipv6_opt_hdr *destopt)
158{
159 u8 *opt = (u8 *)destopt;
160 int len = ipv6_optlen(destopt);
161 int off = 0;
162 int optlen = 0;
163
164 off += 2;
165 len -= 2;
166
167 while (len > 0) {
168
169 switch (opt[off]) {
170
171 case IPV6_TLV_PAD0:
172 optlen = 1;
173 break;
174 default:
175 if (len < 2)
176 goto bad;
177 optlen = opt[off+1]+2;
178 if (len < optlen)
179 goto bad;
180
181 /* Rearrange the source address in @iph and the
182 * addresses in home address option for final source.
183 * See 11.3.2 of RFC 3775 for details.
184 */
185 if (opt[off] == IPV6_TLV_HAO) {
186 struct in6_addr final_addr;
187 struct ipv6_destopt_hao *hao;
188
189 hao = (struct ipv6_destopt_hao *)&opt[off];
190 if (hao->length != sizeof(hao->addr)) {
191 if (net_ratelimit())
192 printk(KERN_WARNING "destopt hao: invalid header length: %u\n", hao->length);
193 goto bad;
194 }
195 ipv6_addr_copy(&final_addr, &hao->addr);
196 ipv6_addr_copy(&hao->addr, &iph->saddr);
197 ipv6_addr_copy(&iph->saddr, &final_addr);
198 }
199 break;
200 }
201
202 off += optlen;
203 len -= optlen;
204 }
YOSHIFUJI Hideakie731c242006-08-24 23:18:12 +0900205 /* Note: ok if len == 0 */
Masahide NAKAMURA27637df2006-08-23 19:29:47 -0700206bad:
207 return;
208}
Masahide NAKAMURA136ebf02007-06-26 23:51:41 -0700209#else
210static void ipv6_rearrange_destopt(struct ipv6hdr *iph, struct ipv6_opt_hdr *destopt) {}
Masahide NAKAMURA27637df2006-08-23 19:29:47 -0700211#endif
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213/**
214 * ipv6_rearrange_rthdr - rearrange IPv6 routing header
215 * @iph: IPv6 header
216 * @rthdr: routing header
217 *
218 * Rearrange the destination address in @iph and the addresses in @rthdr
219 * so that they appear in the order they will at the final destination.
220 * See Appendix A2 of RFC 2402 for details.
221 */
222static void ipv6_rearrange_rthdr(struct ipv6hdr *iph, struct ipv6_rt_hdr *rthdr)
223{
224 int segments, segments_left;
225 struct in6_addr *addrs;
226 struct in6_addr final_addr;
227
228 segments_left = rthdr->segments_left;
229 if (segments_left == 0)
230 return;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900231 rthdr->segments_left = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 /* The value of rthdr->hdrlen has been verified either by the system
234 * call if it is locally generated, or by ipv6_rthdr_rcv() for incoming
235 * packets. So we can assume that it is even and that segments is
236 * greater than or equal to segments_left.
237 *
238 * For the same reason we can assume that this option is of type 0.
239 */
240 segments = rthdr->hdrlen >> 1;
241
242 addrs = ((struct rt0_hdr *)rthdr)->addr;
243 ipv6_addr_copy(&final_addr, addrs + segments - 1);
244
245 addrs += segments - segments_left;
246 memmove(addrs + 1, addrs, (segments_left - 1) * sizeof(*addrs));
247
248 ipv6_addr_copy(addrs, &iph->daddr);
249 ipv6_addr_copy(&iph->daddr, &final_addr);
250}
251
Masahide NAKAMURA27637df2006-08-23 19:29:47 -0700252static int ipv6_clear_mutable_options(struct ipv6hdr *iph, int len, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 union {
255 struct ipv6hdr *iph;
256 struct ipv6_opt_hdr *opth;
257 struct ipv6_rt_hdr *rth;
258 char *raw;
259 } exthdr = { .iph = iph };
260 char *end = exthdr.raw + len;
261 int nexthdr = iph->nexthdr;
262
263 exthdr.iph++;
264
265 while (exthdr.raw < end) {
266 switch (nexthdr) {
Masahide NAKAMURA27637df2006-08-23 19:29:47 -0700267 case NEXTHDR_DEST:
268 if (dir == XFRM_POLICY_OUT)
269 ipv6_rearrange_destopt(iph, exthdr.opth);
YOSHIFUJI Hideakie731c242006-08-24 23:18:12 +0900270 case NEXTHDR_HOP:
271 if (!zero_out_mutable_opts(exthdr.opth)) {
272 LIMIT_NETDEBUG(
273 KERN_WARNING "overrun %sopts\n",
274 nexthdr == NEXTHDR_HOP ?
275 "hop" : "dest");
276 return -EINVAL;
277 }
278 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 case NEXTHDR_ROUTING:
281 ipv6_rearrange_rthdr(iph, exthdr.rth);
282 break;
283
284 default :
285 return 0;
286 }
287
288 nexthdr = exthdr.opth->nexthdr;
289 exthdr.raw += ipv6_optlen(exthdr.opth);
290 }
291
292 return 0;
293}
294
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000295static void ah6_output_done(struct crypto_async_request *base, int err)
296{
297 int extlen;
298 u8 *iph_base;
299 u8 *icv;
300 struct sk_buff *skb = base->data;
301 struct xfrm_state *x = skb_dst(skb)->xfrm;
302 struct ah_data *ahp = x->data;
303 struct ipv6hdr *top_iph = ipv6_hdr(skb);
304 struct ip_auth_hdr *ah = ip_auth_hdr(skb);
305 struct tmp_ext *iph_ext;
306
307 extlen = skb_network_header_len(skb) - sizeof(struct ipv6hdr);
308 if (extlen)
309 extlen += sizeof(*iph_ext);
310
311 iph_base = AH_SKB_CB(skb)->tmp;
312 iph_ext = ah_tmp_ext(iph_base);
313 icv = ah_tmp_icv(ahp->ahash, iph_ext, extlen);
314
315 memcpy(ah->auth_data, icv, ahp->icv_trunc_len);
316 memcpy(top_iph, iph_base, IPV6HDR_BASELEN);
317
318 if (extlen) {
319#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
320 memcpy(&top_iph->saddr, iph_ext, extlen);
321#else
322 memcpy(&top_iph->daddr, iph_ext, extlen);
323#endif
324 }
325
326 err = ah->nexthdr;
327
328 kfree(AH_SKB_CB(skb)->tmp);
329 xfrm_output_resume(skb, err);
330}
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332static int ah6_output(struct xfrm_state *x, struct sk_buff *skb)
333{
334 int err;
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000335 int nfrags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 int extlen;
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000337 u8 *iph_base;
338 u8 *icv;
339 u8 nexthdr;
340 struct sk_buff *trailer;
341 struct crypto_ahash *ahash;
342 struct ahash_request *req;
343 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 struct ipv6hdr *top_iph;
345 struct ip_auth_hdr *ah;
346 struct ah_data *ahp;
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000347 struct tmp_ext *iph_ext;
348
349 ahp = x->data;
350 ahash = ahp->ahash;
351
352 if ((err = skb_cow_data(skb, 0, &trailer)) < 0)
353 goto out;
354 nfrags = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Herbert Xu7b277b12007-10-10 15:44:06 -0700356 skb_push(skb, -skb_network_offset(skb));
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000357 extlen = skb_network_header_len(skb) - sizeof(struct ipv6hdr);
358 if (extlen)
359 extlen += sizeof(*iph_ext);
360
361 err = -ENOMEM;
362 iph_base = ah_alloc_tmp(ahash, nfrags, IPV6HDR_BASELEN + extlen);
363 if (!iph_base)
364 goto out;
365
366 iph_ext = ah_tmp_ext(iph_base);
367 icv = ah_tmp_icv(ahash, iph_ext, extlen);
368 req = ah_tmp_req(ahash, icv);
369 sg = ah_req_sg(ahash, req);
370
371 ah = ip_auth_hdr(skb);
372 memset(ah->auth_data, 0, ahp->icv_trunc_len);
373
Herbert Xu007f0212007-10-09 13:25:59 -0700374 top_iph = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 top_iph->payload_len = htons(skb->len - sizeof(*top_iph));
376
Herbert Xu007f0212007-10-09 13:25:59 -0700377 nexthdr = *skb_mac_header(skb);
378 *skb_mac_header(skb) = IPPROTO_AH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 /* When there are no extension headers, we only need to save the first
381 * 8 bytes of the base IP header.
382 */
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000383 memcpy(iph_base, top_iph, IPV6HDR_BASELEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (extlen) {
Masahide NAKAMURA59fbb3a62007-06-26 23:56:32 -0700386#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000387 memcpy(iph_ext, &top_iph->saddr, extlen);
YOSHIFUJI Hideakie731c242006-08-24 23:18:12 +0900388#else
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000389 memcpy(iph_ext, &top_iph->daddr, extlen);
YOSHIFUJI Hideakie731c242006-08-24 23:18:12 +0900390#endif
Masahide NAKAMURA27637df2006-08-23 19:29:47 -0700391 err = ipv6_clear_mutable_options(top_iph,
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000392 extlen - sizeof(*iph_ext) +
Masahide NAKAMURA27637df2006-08-23 19:29:47 -0700393 sizeof(*top_iph),
394 XFRM_POLICY_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (err)
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000396 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 ah->nexthdr = nexthdr;
400
401 top_iph->priority = 0;
402 top_iph->flow_lbl[0] = 0;
403 top_iph->flow_lbl[1] = 0;
404 top_iph->flow_lbl[2] = 0;
405 top_iph->hop_limit = 0;
406
Herbert Xu87bdc482007-10-10 15:45:25 -0700407 ah->hdrlen = (XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len) >> 2) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 ah->reserved = 0;
410 ah->spi = x->id.spi;
Herbert Xub318e0e2008-02-12 22:50:35 -0800411 ah->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output);
Herbert Xub7c65382007-10-09 13:33:35 -0700412
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000413 sg_init_table(sg, nfrags);
414 skb_to_sgvec(skb, sg, 0, skb->len);
Herbert Xub7c65382007-10-09 13:33:35 -0700415
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000416 ahash_request_set_crypt(req, sg, icv, skb->len);
417 ahash_request_set_callback(req, 0, ah6_output_done, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000419 AH_SKB_CB(skb)->tmp = iph_base;
420
421 err = crypto_ahash_digest(req);
422 if (err) {
423 if (err == -EINPROGRESS)
424 goto out;
425
426 if (err == -EBUSY)
427 err = NET_XMIT_DROP;
428 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000431 memcpy(ah->auth_data, icv, ahp->icv_trunc_len);
432 memcpy(top_iph, iph_base, IPV6HDR_BASELEN);
433
434 if (extlen) {
435#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
436 memcpy(&top_iph->saddr, iph_ext, extlen);
437#else
438 memcpy(&top_iph->daddr, iph_ext, extlen);
439#endif
440 }
441
442out_free:
443 kfree(iph_base);
444out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return err;
446}
447
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000448static void ah6_input_done(struct crypto_async_request *base, int err)
449{
450 u8 *auth_data;
451 u8 *icv;
452 u8 *work_iph;
453 struct sk_buff *skb = base->data;
454 struct xfrm_state *x = xfrm_input_state(skb);
455 struct ah_data *ahp = x->data;
456 struct ip_auth_hdr *ah = ip_auth_hdr(skb);
457 int hdr_len = skb_network_header_len(skb);
458 int ah_hlen = (ah->hdrlen + 2) << 2;
459
460 work_iph = AH_SKB_CB(skb)->tmp;
461 auth_data = ah_tmp_auth(work_iph, hdr_len);
462 icv = ah_tmp_icv(ahp->ahash, auth_data, ahp->icv_trunc_len);
463
464 err = memcmp(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG: 0;
465 if (err)
466 goto out;
467
468 skb->network_header += ah_hlen;
469 memcpy(skb_network_header(skb), work_iph, hdr_len);
470 __skb_pull(skb, ah_hlen + hdr_len);
471 skb_set_transport_header(skb, -hdr_len);
472
473 err = ah->nexthdr;
474out:
475 kfree(AH_SKB_CB(skb)->tmp);
476 xfrm_input_resume(skb, err);
477}
478
479
480
Herbert Xue6956332006-04-01 00:52:46 -0800481static int ah6_input(struct xfrm_state *x, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
483 /*
484 * Before process AH
485 * [IPv6][Ext1][Ext2][AH][Dest][Payload]
486 * |<-------------->| hdr_len
487 *
488 * To erase AH:
489 * Keeping copy of cleared headers. After AH processing,
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700490 * Moving the pointer of skb->network_header by using skb_pull as long
491 * as AH header length. Then copy back the copy as long as hdr_len
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 * If destination header following AH exists, copy it into after [Ext2].
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900493 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 * |<>|[IPv6][Ext1][Ext2][Dest][Payload]
495 * There is offset of AH before IPv6 header after the process.
496 */
497
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000498 u8 *auth_data;
499 u8 *icv;
500 u8 *work_iph;
501 struct sk_buff *trailer;
502 struct crypto_ahash *ahash;
503 struct ahash_request *req;
504 struct scatterlist *sg;
Herbert Xu87bdc482007-10-10 15:45:25 -0700505 struct ip_auth_hdr *ah;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700506 struct ipv6hdr *ip6h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 struct ah_data *ahp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 u16 hdr_len;
509 u16 ah_hlen;
510 int nexthdr;
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000511 int nfrags;
512 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 if (!pskb_may_pull(skb, sizeof(struct ip_auth_hdr)))
515 goto out;
516
517 /* We are going to _remove_ AH header to keep sockets happy,
518 * so... Later this can change. */
519 if (skb_cloned(skb) &&
520 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
521 goto out;
522
Herbert Xu7aa68cb2007-10-17 21:30:07 -0700523 skb->ip_summed = CHECKSUM_NONE;
524
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000525 hdr_len = skb_network_header_len(skb);
Herbert Xu87bdc482007-10-10 15:45:25 -0700526 ah = (struct ip_auth_hdr *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 ahp = x->data;
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000528 ahash = ahp->ahash;
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 nexthdr = ah->nexthdr;
531 ah_hlen = (ah->hdrlen + 2) << 2;
532
Herbert Xu87bdc482007-10-10 15:45:25 -0700533 if (ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_full_len) &&
534 ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len))
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900535 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 if (!pskb_may_pull(skb, ah_hlen))
538 goto out;
539
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700540 ip6h = ipv6_hdr(skb);
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000541
542 skb_push(skb, hdr_len);
543
544 if ((err = skb_cow_data(skb, 0, &trailer)) < 0)
545 goto out;
546 nfrags = err;
547
548 work_iph = ah_alloc_tmp(ahash, nfrags, hdr_len + ahp->icv_trunc_len);
549 if (!work_iph)
550 goto out;
551
552 auth_data = ah_tmp_auth(work_iph, hdr_len);
553 icv = ah_tmp_icv(ahash, auth_data, ahp->icv_trunc_len);
554 req = ah_tmp_req(ahash, icv);
555 sg = ah_req_sg(ahash, req);
556
557 memcpy(work_iph, ip6h, hdr_len);
558 memcpy(auth_data, ah->auth_data, ahp->icv_trunc_len);
559 memset(ah->auth_data, 0, ahp->icv_trunc_len);
560
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700561 if (ipv6_clear_mutable_options(ip6h, hdr_len, XFRM_POLICY_IN))
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000562 goto out_free;
563
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700564 ip6h->priority = 0;
565 ip6h->flow_lbl[0] = 0;
566 ip6h->flow_lbl[1] = 0;
567 ip6h->flow_lbl[2] = 0;
568 ip6h->hop_limit = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000570 sg_init_table(sg, nfrags);
571 skb_to_sgvec(skb, sg, 0, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000573 ahash_request_set_crypt(req, sg, icv, skb->len);
574 ahash_request_set_callback(req, 0, ah6_input_done, skb);
575
576 AH_SKB_CB(skb)->tmp = work_iph;
577
578 err = crypto_ahash_digest(req);
579 if (err) {
580 if (err == -EINPROGRESS)
581 goto out;
582
583 if (err == -EBUSY)
584 err = NET_XMIT_DROP;
585 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
Herbert Xu0ebea8e2007-11-13 21:45:58 -0800587
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000588 err = memcmp(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG: 0;
Herbert Xu0ebea8e2007-11-13 21:45:58 -0800589 if (err)
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000590 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700592 skb->network_header += ah_hlen;
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000593 memcpy(skb_network_header(skb), work_iph, hdr_len);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700594 skb->transport_header = skb->network_header;
Herbert Xu31a4ab92006-05-27 23:06:13 -0700595 __skb_pull(skb, ah_hlen + hdr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000597 err = nexthdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000599out_free:
600 kfree(work_iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601out:
Herbert Xu07d4ee52006-08-20 14:24:50 +1000602 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900605static void ah6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700606 u8 type, u8 code, int offset, __be32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Alexey Dobriyan4fb236b2008-11-25 17:59:27 -0800608 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 struct ipv6hdr *iph = (struct ipv6hdr*)skb->data;
610 struct ip_auth_hdr *ah = (struct ip_auth_hdr*)(skb->data+offset);
611 struct xfrm_state *x;
612
613 if (type != ICMPV6_DEST_UNREACH &&
614 type != ICMPV6_PKT_TOOBIG)
615 return;
616
Alexey Dobriyan4fb236b2008-11-25 17:59:27 -0800617 x = xfrm_state_lookup(net, (xfrm_address_t *)&iph->daddr, ah->spi, IPPROTO_AH, AF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (!x)
619 return;
620
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700621 NETDEBUG(KERN_DEBUG "pmtu discovery on SA AH/%08x/%pI6\n",
Harvey Harrison0c6ce782008-10-28 16:09:23 -0700622 ntohl(ah->spi), &iph->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 xfrm_state_put(x);
625}
626
Herbert Xu72cb6962005-06-20 13:18:08 -0700627static int ah6_init_state(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
629 struct ah_data *ahp = NULL;
630 struct xfrm_algo_desc *aalg_desc;
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000631 struct crypto_ahash *ahash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 if (!x->aalg)
634 goto error;
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (x->encap)
637 goto error;
638
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800639 ahp = kzalloc(sizeof(*ahp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (ahp == NULL)
641 return -ENOMEM;
642
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000643 ahash = crypto_alloc_ahash(x->aalg->alg_name, 0, 0);
644 if (IS_ERR(ahash))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 goto error;
Herbert Xu07d4ee52006-08-20 14:24:50 +1000646
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000647 ahp->ahash = ahash;
648 if (crypto_ahash_setkey(ahash, x->aalg->alg_key,
Herbert Xubc31d3b2007-10-08 17:14:34 -0700649 (x->aalg->alg_key_len + 7) / 8))
Herbert Xu07d4ee52006-08-20 14:24:50 +1000650 goto error;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 /*
653 * Lookup the algorithm description maintained by xfrm_algo,
654 * verify crypto transform properties, and store information
655 * we need for AH processing. This lookup cannot fail here
Herbert Xu07d4ee52006-08-20 14:24:50 +1000656 * after a successful crypto_alloc_hash().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 */
658 aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
659 BUG_ON(!aalg_desc);
660
661 if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000662 crypto_ahash_digestsize(ahash)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 printk(KERN_INFO "AH: %s digestsize %u != %hu\n",
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000664 x->aalg->alg_name, crypto_ahash_digestsize(ahash),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 aalg_desc->uinfo.auth.icv_fullbits/8);
666 goto error;
667 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 ahp->icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
Martin Willi8f8a0882009-11-25 00:29:53 +0000670 ahp->icv_trunc_len = x->aalg->alg_trunc_len/8;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 BUG_ON(ahp->icv_trunc_len > MAX_AH_AUTH_LEN);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900673
Herbert Xu87bdc482007-10-10 15:45:25 -0700674 x->props.header_len = XFRM_ALIGN8(sizeof(struct ip_auth_hdr) +
675 ahp->icv_trunc_len);
Herbert Xuca681452007-10-17 21:35:15 -0700676 switch (x->props.mode) {
677 case XFRM_MODE_BEET:
678 case XFRM_MODE_TRANSPORT:
679 break;
680 case XFRM_MODE_TUNNEL:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 x->props.header_len += sizeof(struct ipv6hdr);
Masahide NAKAMURAea2c47b2007-10-22 02:30:15 -0700682 break;
Herbert Xuca681452007-10-17 21:35:15 -0700683 default:
684 goto error;
685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 x->data = ahp;
687
688 return 0;
689
690error:
691 if (ahp) {
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000692 crypto_free_ahash(ahp->ahash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 kfree(ahp);
694 }
695 return -EINVAL;
696}
697
698static void ah6_destroy(struct xfrm_state *x)
699{
700 struct ah_data *ahp = x->data;
701
702 if (!ahp)
703 return;
704
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000705 crypto_free_ahash(ahp->ahash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 kfree(ahp);
707}
708
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800709static const struct xfrm_type ah6_type =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
711 .description = "AH6",
712 .owner = THIS_MODULE,
713 .proto = IPPROTO_AH,
Herbert Xu436a0a42007-10-08 17:25:53 -0700714 .flags = XFRM_TYPE_REPLAY_PROT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 .init_state = ah6_init_state,
716 .destructor = ah6_destroy,
717 .input = ah6_input,
Masahide NAKAMURAaee5adb2006-08-23 17:57:28 -0700718 .output = ah6_output,
719 .hdr_offset = xfrm6_find_1stfragopt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720};
721
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000722static const struct inet6_protocol ah6_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 .handler = xfrm6_rcv,
724 .err_handler = ah6_err,
725 .flags = INET6_PROTO_NOPOLICY,
726};
727
728static int __init ah6_init(void)
729{
730 if (xfrm_register_type(&ah6_type, AF_INET6) < 0) {
731 printk(KERN_INFO "ipv6 ah init: can't add xfrm type\n");
732 return -EAGAIN;
733 }
734
735 if (inet6_add_protocol(&ah6_protocol, IPPROTO_AH) < 0) {
736 printk(KERN_INFO "ipv6 ah init: can't add protocol\n");
737 xfrm_unregister_type(&ah6_type, AF_INET6);
738 return -EAGAIN;
739 }
740
741 return 0;
742}
743
744static void __exit ah6_fini(void)
745{
746 if (inet6_del_protocol(&ah6_protocol, IPPROTO_AH) < 0)
747 printk(KERN_INFO "ipv6 ah close: can't remove protocol\n");
748
749 if (xfrm_unregister_type(&ah6_type, AF_INET6) < 0)
750 printk(KERN_INFO "ipv6 ah close: can't remove xfrm type\n");
751
752}
753
754module_init(ah6_init);
755module_exit(ah6_fini);
756
757MODULE_LICENSE("GPL");
Masahide NAKAMURAd3d6dd32007-06-26 23:57:49 -0700758MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_AH);