2 * Copyright (C)2002 USAGI/WIDE Project
4 * 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.
9 * 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.
14 * 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
20 * Mitsuru KANDA @USAGI : IPv6 Support
21 * Kazunori MIYAZAWA @USAGI :
22 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
24 * This file is derived from net/ipv4/ah.c.
27 #include <linux/config.h>
28 #include <linux/module.h>
29 #include <net/inet_ecn.h>
32 #include <linux/crypto.h>
33 #include <linux/pfkeyv2.h>
37 #include <asm/scatterlist.h>
39 static int zero_out_mutable_opts(struct ipv6_opt_hdr *opthdr)
41 u8 *opt = (u8 *)opthdr;
42 int len = ipv6_optlen(opthdr);
59 optlen = opt[off+1]+2;
63 memset(&opt[off+2], 0, opt[off+1]);
77 static int ipv6_clear_mutable_options(struct sk_buff *skb, u16 *nh_offset, int dir)
79 u16 offset = sizeof(struct ipv6hdr);
80 struct ipv6_opt_hdr *exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
81 unsigned int packet_len = skb->tail - skb->nh.raw;
82 u8 nexthdr = skb->nh.ipv6h->nexthdr;
85 *nh_offset = ((unsigned char *)&skb->nh.ipv6h->nexthdr) - skb->nh.raw;
87 while (offset + 1 <= packet_len) {
93 offset += ipv6_optlen(exthdr);
94 if (!zero_out_mutable_opts(exthdr)) {
96 printk(KERN_WARNING "overrun hopopts\n"));
99 nexthdr = exthdr->nexthdr;
100 exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
103 case NEXTHDR_ROUTING:
105 offset += ipv6_optlen(exthdr);
106 ((struct ipv6_rt_hdr*)exthdr)->segments_left = 0;
107 nexthdr = exthdr->nexthdr;
108 exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
113 offset += ipv6_optlen(exthdr);
114 if (!zero_out_mutable_opts(exthdr)) {
116 printk(KERN_WARNING "overrun destopt\n"));
119 nexthdr = exthdr->nexthdr;
120 exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
124 if (dir == XFRM_POLICY_OUT) {
125 memset(((struct ipv6_auth_hdr*)exthdr)->auth_data, 0,
126 (((struct ipv6_auth_hdr*)exthdr)->hdrlen - 1) << 2);
128 if (exthdr->nexthdr == NEXTHDR_DEST) {
129 offset += (((struct ipv6_auth_hdr*)exthdr)->hdrlen + 2) << 2;
130 exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
131 nextnexthdr = exthdr->nexthdr;
132 if (!zero_out_mutable_opts(exthdr)) {
134 printk(KERN_WARNING "overrun destopt\n"));
147 int ah6_output(struct sk_buff *skb)
150 int hdr_len = sizeof(struct ipv6hdr);
151 struct dst_entry *dst = skb->dst;
152 struct xfrm_state *x = dst->xfrm;
153 struct ipv6hdr *iph = NULL;
154 struct ip_auth_hdr *ah;
159 if (skb->ip_summed == CHECKSUM_HW && skb_checksum_help(skb) == NULL) {
164 spin_lock_bh(&x->lock);
165 err = xfrm_check_output(x, skb, AF_INET6);
171 skb->nh.ipv6h = (struct ipv6hdr*)skb_push(skb, x->props.header_len);
172 skb->nh.ipv6h->version = 6;
173 skb->nh.ipv6h->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
174 skb->nh.ipv6h->nexthdr = IPPROTO_AH;
175 ipv6_addr_copy(&skb->nh.ipv6h->saddr,
176 (struct in6_addr *) &x->props.saddr);
177 ipv6_addr_copy(&skb->nh.ipv6h->daddr,
178 (struct in6_addr *) &x->id.daddr);
179 ah = (struct ip_auth_hdr*)(skb->nh.ipv6h+1);
180 ah->nexthdr = IPPROTO_IPV6;
182 hdr_len = skb->h.raw - skb->nh.raw;
183 iph = kmalloc(hdr_len, GFP_ATOMIC);
188 memcpy(iph, skb->data, hdr_len);
189 skb->nh.ipv6h = (struct ipv6hdr*)skb_push(skb, x->props.header_len);
190 memcpy(skb->nh.ipv6h, iph, hdr_len);
191 nexthdr = ipv6_clear_mutable_options(skb, &nh_offset, XFRM_POLICY_OUT);
195 skb->nh.raw[nh_offset] = IPPROTO_AH;
196 skb->nh.ipv6h->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
197 ah = (struct ip_auth_hdr*)(skb->nh.raw+hdr_len);
198 skb->h.raw = (unsigned char*) ah;
199 ah->nexthdr = nexthdr;
202 skb->nh.ipv6h->priority = 0;
203 skb->nh.ipv6h->flow_lbl[0] = 0;
204 skb->nh.ipv6h->flow_lbl[1] = 0;
205 skb->nh.ipv6h->flow_lbl[2] = 0;
206 skb->nh.ipv6h->hop_limit = 0;
209 ah->hdrlen = (XFRM_ALIGN8(sizeof(struct ipv6_auth_hdr) +
210 ahp->icv_trunc_len) >> 2) - 2;
214 ah->seq_no = htonl(++x->replay.oseq);
215 ahp->icv(ahp, skb, ah->auth_data);
218 skb->nh.ipv6h->hop_limit = iph->hop_limit;
219 skb->nh.ipv6h->priority = iph->priority;
220 skb->nh.ipv6h->flow_lbl[0] = iph->flow_lbl[0];
221 skb->nh.ipv6h->flow_lbl[1] = iph->flow_lbl[1];
222 skb->nh.ipv6h->flow_lbl[2] = iph->flow_lbl[2];
223 if (x->props.flags & XFRM_STATE_NOECN)
224 IP6_ECN_clear(skb->nh.ipv6h);
226 memcpy(skb->nh.ipv6h, iph, hdr_len);
227 skb->nh.raw[nh_offset] = IPPROTO_AH;
228 skb->nh.ipv6h->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
232 skb->nh.raw = skb->data;
234 x->curlft.bytes += skb->len;
236 spin_unlock_bh(&x->lock);
237 if ((skb->dst = dst_pop(dst)) == NULL) {
241 return NET_XMIT_BYPASS;
245 spin_unlock_bh(&x->lock);
251 int ah6_input(struct xfrm_state *x, struct xfrm_decap_state *decap, struct sk_buff *skb)
255 * [IPv6][Ext1][Ext2][AH][Dest][Payload]
256 * |<-------------->| hdr_len
257 * |<------------------------>| cleared_hlen
260 * Keeping copy of cleared headers. After AH processing,
261 * Moving the pointer of skb->nh.raw by using skb_pull as long as AH
262 * header length. Then copy back the copy as long as hdr_len
263 * If destination header following AH exists, copy it into after [Ext2].
265 * |<>|[IPv6][Ext1][Ext2][Dest][Payload]
266 * There is offset of AH before IPv6 header after the process.
269 struct ipv6_auth_hdr *ah;
271 unsigned char *tmp_hdr = NULL;
279 if (!pskb_may_pull(skb, sizeof(struct ip_auth_hdr)))
282 /* We are going to _remove_ AH header to keep sockets happy,
283 * so... Later this can change. */
284 if (skb_cloned(skb) &&
285 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
288 hdr_len = skb->data - skb->nh.raw;
289 cleared_hlen = hdr_len;
290 ah = (struct ipv6_auth_hdr*)skb->data;
292 nexthdr = ah->nexthdr;
293 ah_hlen = (ah->hdrlen + 2) << 2;
294 cleared_hlen += ah_hlen;
296 if (nexthdr == NEXTHDR_DEST) {
297 struct ipv6_opt_hdr *dsthdr = (struct ipv6_opt_hdr*)(skb->data + ah_hlen);
298 cleared_hlen += ipv6_optlen(dsthdr);
301 if (ah_hlen != XFRM_ALIGN8(sizeof(struct ipv6_auth_hdr) + ahp->icv_full_len) &&
302 ah_hlen != XFRM_ALIGN8(sizeof(struct ipv6_auth_hdr) + ahp->icv_trunc_len))
305 if (!pskb_may_pull(skb, ah_hlen))
308 tmp_hdr = kmalloc(cleared_hlen, GFP_ATOMIC);
311 memcpy(tmp_hdr, skb->nh.raw, cleared_hlen);
312 ipv6_clear_mutable_options(skb, &nh_offset, XFRM_POLICY_IN);
313 skb->nh.ipv6h->priority = 0;
314 skb->nh.ipv6h->flow_lbl[0] = 0;
315 skb->nh.ipv6h->flow_lbl[1] = 0;
316 skb->nh.ipv6h->flow_lbl[2] = 0;
317 skb->nh.ipv6h->hop_limit = 0;
320 u8 auth_data[MAX_AH_AUTH_LEN];
322 memcpy(auth_data, ah->auth_data, ahp->icv_trunc_len);
323 memset(ah->auth_data, 0, ahp->icv_trunc_len);
324 skb_push(skb, skb->data - skb->nh.raw);
325 ahp->icv(ahp, skb, ah->auth_data);
326 if (memcmp(ah->auth_data, auth_data, ahp->icv_trunc_len)) {
328 printk(KERN_WARNING "ipsec ah authentication error\n"));
329 x->stats.integrity_failed++;
334 skb->nh.raw = skb_pull(skb, ah_hlen);
335 memcpy(skb->nh.raw, tmp_hdr, hdr_len);
336 if (nexthdr == NEXTHDR_DEST) {
337 memcpy(skb->nh.raw + hdr_len,
338 tmp_hdr + hdr_len + ah_hlen,
339 cleared_hlen - hdr_len - ah_hlen);
341 prevhdr = (u8*)(skb->nh.raw + nh_offset);
343 skb->nh.ipv6h->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
344 skb_pull(skb, hdr_len);
345 skb->h.raw = skb->data;
358 void ah6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
359 int type, int code, int offset, __u32 info)
361 struct ipv6hdr *iph = (struct ipv6hdr*)skb->data;
362 struct ip_auth_hdr *ah = (struct ip_auth_hdr*)(skb->data+offset);
363 struct xfrm_state *x;
365 if (type != ICMPV6_DEST_UNREACH ||
366 type != ICMPV6_PKT_TOOBIG)
369 x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, ah->spi, IPPROTO_AH, AF_INET6);
373 NETDEBUG(printk(KERN_DEBUG "pmtu discovery on SA AH/%08x/"
374 "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
375 ntohl(ah->spi), NIP6(iph->daddr)));
380 static int ah6_init_state(struct xfrm_state *x, void *args)
382 struct ah_data *ahp = NULL;
383 struct xfrm_algo_desc *aalg_desc;
388 /* null auth can use a zero length key */
389 if (x->aalg->alg_key_len > 512)
392 ahp = kmalloc(sizeof(*ahp), GFP_KERNEL);
396 memset(ahp, 0, sizeof(*ahp));
398 ahp->key = x->aalg->alg_key;
399 ahp->key_len = (x->aalg->alg_key_len+7)/8;
400 ahp->tfm = crypto_alloc_tfm(x->aalg->alg_name, 0);
403 ahp->icv = ah_hmac_digest;
406 * Lookup the algorithm description maintained by xfrm_algo,
407 * verify crypto transform properties, and store information
408 * we need for AH processing. This lookup cannot fail here
409 * after a successful crypto_alloc_tfm().
411 aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name);
414 if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
415 crypto_tfm_alg_digestsize(ahp->tfm)) {
416 printk(KERN_INFO "AH: %s digestsize %u != %hu\n",
417 x->aalg->alg_name, crypto_tfm_alg_digestsize(ahp->tfm),
418 aalg_desc->uinfo.auth.icv_fullbits/8);
422 ahp->icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
423 ahp->icv_trunc_len = aalg_desc->uinfo.auth.icv_truncbits/8;
425 BUG_ON(ahp->icv_trunc_len > MAX_AH_AUTH_LEN);
427 ahp->work_icv = kmalloc(ahp->icv_full_len, GFP_KERNEL);
431 x->props.header_len = XFRM_ALIGN8(sizeof(struct ipv6_auth_hdr) + ahp->icv_trunc_len);
433 x->props.header_len += sizeof(struct ipv6hdr);
441 kfree(ahp->work_icv);
443 crypto_free_tfm(ahp->tfm);
449 static void ah6_destroy(struct xfrm_state *x)
451 struct ah_data *ahp = x->data;
457 kfree(ahp->work_icv);
458 ahp->work_icv = NULL;
461 crypto_free_tfm(ahp->tfm);
467 static struct xfrm_type ah6_type =
469 .description = "AH6",
470 .owner = THIS_MODULE,
472 .init_state = ah6_init_state,
473 .destructor = ah6_destroy,
478 static struct inet6_protocol ah6_protocol = {
479 .handler = xfrm6_rcv,
480 .err_handler = ah6_err,
481 .flags = INET6_PROTO_NOPOLICY,
484 int __init ah6_init(void)
486 if (xfrm_register_type(&ah6_type, AF_INET6) < 0) {
487 printk(KERN_INFO "ipv6 ah init: can't add xfrm type\n");
491 if (inet6_add_protocol(&ah6_protocol, IPPROTO_AH) < 0) {
492 printk(KERN_INFO "ipv6 ah init: can't add protocol\n");
493 xfrm_unregister_type(&ah6_type, AF_INET6);
500 static void __exit ah6_fini(void)
502 if (inet6_del_protocol(&ah6_protocol, IPPROTO_AH) < 0)
503 printk(KERN_INFO "ipv6 ah close: can't remove protocol\n");
505 if (xfrm_unregister_type(&ah6_type, AF_INET6) < 0)
506 printk(KERN_INFO "ipv6 ah close: can't remove xfrm type\n");
510 module_init(ah6_init);
511 module_exit(ah6_fini);
513 MODULE_LICENSE("GPL");