blob: 9084582d236bc97f7da39e6f1090f954cb63e94f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * xfrm6_input.c: based on net/ipv4/xfrm4_input.c
3 *
4 * Authors:
5 * Mitsuru KANDA @USAGI
6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
8 * YOSHIFUJI Hideaki @USAGI
9 * IPv6 support
10 */
11
12#include <linux/module.h>
13#include <linux/string.h>
Patrick McHardyb05e1062006-01-06 23:03:34 -080014#include <linux/netfilter.h>
15#include <linux/netfilter_ipv6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <net/ipv6.h>
17#include <net/xfrm.h>
18
Herbert Xu227620e2007-11-13 21:41:28 -080019int xfrm6_extract_input(struct xfrm_state *x, struct sk_buff *skb)
20{
21 return xfrm6_extract_header(skb);
22}
23
Herbert Xu33b5ecb2007-10-17 21:29:25 -070024int xfrm6_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
Herbert Xu2fcb45b2007-12-03 22:54:12 -080026 XFRM_SPI_SKB_CB(skb)->family = AF_INET6;
Herbert Xu716062f2007-11-13 21:44:23 -080027 XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct ipv6hdr, daddr);
28 return xfrm_input(skb, nexthdr, spi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029}
Linus Torvalds1da177e2005-04-16 15:20:36 -070030EXPORT_SYMBOL(xfrm6_rcv_spi);
31
Herbert Xu716062f2007-11-13 21:44:23 -080032int xfrm6_transport_finish(struct sk_buff *skb, int async)
33{
Herbert Xu60d5fcf2007-11-19 18:47:58 -080034 skb_network_header(skb)[IP6CB(skb)->nhoff] =
35 XFRM_MODE_SKB_CB(skb)->protocol;
36
Herbert Xu0883ae02007-12-30 21:10:14 -080037#ifndef CONFIG_NETFILTER
38 if (!async)
39 return 1;
40#endif
41
Herbert Xu716062f2007-11-13 21:44:23 -080042 ipv6_hdr(skb)->payload_len = htons(skb->len);
43 __skb_push(skb, skb->data - skb_network_header(skb));
44
Patrick McHardy6e23ae22007-11-19 18:53:30 -080045 NF_HOOK(PF_INET6, NF_INET_PRE_ROUTING, skb, skb->dev, NULL,
Herbert Xu716062f2007-11-13 21:44:23 -080046 ip6_rcv_finish);
47 return -1;
Herbert Xu716062f2007-11-13 21:44:23 -080048}
49
Herbert Xue5bbef22007-10-15 12:50:28 -070050int xfrm6_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Herbert Xu33b5ecb2007-10-17 21:29:25 -070052 return xfrm6_rcv_spi(skb, skb_network_header(skb)[IP6CB(skb)->nhoff],
53 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -070055
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +090056EXPORT_SYMBOL(xfrm6_rcv);
57
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -070058int xfrm6_input_addr(struct sk_buff *skb, xfrm_address_t *daddr,
59 xfrm_address_t *saddr, u8 proto)
60{
Alexey Dobriyan59c99402008-11-25 17:59:52 -080061 struct net *net = dev_net(skb->dev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090062 struct xfrm_state *x = NULL;
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -070063 int i = 0;
64
Masahide NAKAMURA9473e1f2007-12-20 20:41:57 -080065 /* Allocate new secpath or COW existing one. */
66 if (!skb->sp || atomic_read(&skb->sp->refcnt) != 1) {
67 struct sec_path *sp;
68
69 sp = secpath_dup(skb->sp);
70 if (!sp) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -080071 XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR);
Masahide NAKAMURA9473e1f2007-12-20 20:41:57 -080072 goto drop;
73 }
74 if (skb->sp)
75 secpath_put(skb->sp);
76 skb->sp = sp;
77 }
78
79 if (1 + skb->sp->len == XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -080080 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA9473e1f2007-12-20 20:41:57 -080081 goto drop;
82 }
83
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -070084 for (i = 0; i < 3; i++) {
85 xfrm_address_t *dst, *src;
YOSHIFUJI Hideakia002c6f2008-02-19 17:24:33 +090086
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -070087 switch (i) {
88 case 0:
89 dst = daddr;
90 src = saddr;
91 break;
92 case 1:
93 /* lookup state with wild-card source address */
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -070094 dst = daddr;
YOSHIFUJI Hideakia002c6f2008-02-19 17:24:33 +090095 src = (xfrm_address_t *)&in6addr_any;
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -070096 break;
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -070097 default:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090098 /* lookup state with wild-card addresses */
YOSHIFUJI Hideakia002c6f2008-02-19 17:24:33 +090099 dst = (xfrm_address_t *)&in6addr_any;
100 src = (xfrm_address_t *)&in6addr_any;
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700101 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900102 }
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700103
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800104 x = xfrm_state_lookup_byaddr(net, dst, src, proto, AF_INET6);
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700105 if (!x)
106 continue;
107
108 spin_lock(&x->lock);
109
YOSHIFUJI Hideakia002c6f2008-02-19 17:24:33 +0900110 if ((!i || (x->props.flags & XFRM_STATE_WILDRECV)) &&
111 likely(x->km.state == XFRM_STATE_VALID) &&
112 !xfrm_state_check_expire(x)) {
113 spin_unlock(&x->lock);
114 if (x->type->input(x, skb) > 0) {
115 /* found a valid state */
116 break;
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700117 }
YOSHIFUJI Hideakia002c6f2008-02-19 17:24:33 +0900118 } else
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700119 spin_unlock(&x->lock);
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700120
YOSHIFUJI Hideakia002c6f2008-02-19 17:24:33 +0900121 xfrm_state_put(x);
122 x = NULL;
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700123 }
124
Masahide NAKAMURA9473e1f2007-12-20 20:41:57 -0800125 if (!x) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800126 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES);
Paul Mooreafeb14b2007-12-21 14:58:11 -0800127 xfrm_audit_state_notfound_simple(skb, AF_INET6);
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700128 goto drop;
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700129 }
130
Masahide NAKAMURA9473e1f2007-12-20 20:41:57 -0800131 skb->sp->xvec[skb->sp->len++] = x;
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700132
Masahide NAKAMURA9473e1f2007-12-20 20:41:57 -0800133 spin_lock(&x->lock);
134
135 x->curlft.bytes += skb->len;
136 x->curlft.packets++;
137
138 spin_unlock(&x->lock);
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700139
140 return 1;
Masahide NAKAMURA9473e1f2007-12-20 20:41:57 -0800141
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700142drop:
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700143 return -1;
144}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900145
146EXPORT_SYMBOL(xfrm6_input_addr);