blob: 8655d038364cba364880f8cb9595e94708fe14db [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * xfrm4_input.c
3 *
4 * Changes:
5 * YOSHIFUJI Hideaki @USAGI
6 * Split up af-specific portion
7 * Derek Atkins <derek@ihtfp.com>
8 * Add Encapsulation support
9 *
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_ipv4.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <net/ip.h>
17#include <net/xfrm.h>
18
19int xfrm4_rcv(struct sk_buff *skb)
20{
21 return xfrm4_rcv_encap(skb, 0);
22}
23
24EXPORT_SYMBOL(xfrm4_rcv);
25
Al Viro6067b2b2006-09-27 18:47:59 -070026static int xfrm4_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
28 switch (nexthdr) {
29 case IPPROTO_IPIP:
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 *spi = skb->nh.iph->saddr;
31 *seq = 0;
32 return 0;
33 }
34
35 return xfrm_parse_spi(skb, nexthdr, spi, seq);
36}
37
Patrick McHardyb05e1062006-01-06 23:03:34 -080038#ifdef CONFIG_NETFILTER
39static inline int xfrm4_rcv_encap_finish(struct sk_buff *skb)
40{
41 struct iphdr *iph = skb->nh.iph;
42
43 if (skb->dst == NULL) {
44 if (ip_route_input(skb, iph->daddr, iph->saddr, iph->tos,
45 skb->dev))
46 goto drop;
47 }
48 return dst_input(skb);
49drop:
50 kfree_skb(skb);
51 return NET_RX_DROP;
52}
53#endif
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055int xfrm4_rcv_encap(struct sk_buff *skb, __u16 encap_type)
56{
57 int err;
Al Viro6067b2b2006-09-27 18:47:59 -070058 __be32 spi, seq;
Herbert Xudbe5b4a2006-04-01 00:54:16 -080059 struct xfrm_state *xfrm_vec[XFRM_MAX_DEPTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 struct xfrm_state *x;
61 int xfrm_nr = 0;
62 int decaps = 0;
63
64 if ((err = xfrm4_parse_spi(skb, skb->nh.iph->protocol, &spi, &seq)) != 0)
65 goto drop;
66
67 do {
68 struct iphdr *iph = skb->nh.iph;
69
70 if (xfrm_nr == XFRM_MAX_DEPTH)
71 goto drop;
72
73 x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, spi, iph->protocol, AF_INET);
74 if (x == NULL)
75 goto drop;
76
77 spin_lock(&x->lock);
78 if (unlikely(x->km.state != XFRM_STATE_VALID))
79 goto drop_unlock;
80
Herbert Xu8bf4b8a2006-04-04 12:51:05 -070081 if ((x->encap ? x->encap->encap_type : 0) != encap_type)
Herbert Xue6956332006-04-01 00:52:46 -080082 goto drop_unlock;
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if (x->props.replay_window && xfrm_replay_check(x, seq))
85 goto drop_unlock;
86
87 if (xfrm_state_check_expire(x))
88 goto drop_unlock;
89
Herbert Xue6956332006-04-01 00:52:46 -080090 if (x->type->input(x, skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 goto drop_unlock;
92
93 /* only the first xfrm gets the encap type */
94 encap_type = 0;
95
96 if (x->props.replay_window)
97 xfrm_replay_advance(x, seq);
98
99 x->curlft.bytes += skb->len;
100 x->curlft.packets++;
101
102 spin_unlock(&x->lock);
103
Herbert Xudbe5b4a2006-04-01 00:54:16 -0800104 xfrm_vec[xfrm_nr++] = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Herbert Xub59f45d2006-05-27 23:05:54 -0700106 if (x->mode->input(x, skb))
107 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700109 if (x->props.mode == XFRM_MODE_TUNNEL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 decaps = 1;
111 break;
112 }
113
114 if ((err = xfrm_parse_spi(skb, skb->nh.iph->protocol, &spi, &seq)) < 0)
115 goto drop;
116 } while (!err);
117
118 /* Allocate new secpath or COW existing one. */
119
120 if (!skb->sp || atomic_read(&skb->sp->refcnt) != 1) {
121 struct sec_path *sp;
122 sp = secpath_dup(skb->sp);
123 if (!sp)
124 goto drop;
125 if (skb->sp)
126 secpath_put(skb->sp);
127 skb->sp = sp;
128 }
129 if (xfrm_nr + skb->sp->len > XFRM_MAX_DEPTH)
130 goto drop;
131
Herbert Xudbe5b4a2006-04-01 00:54:16 -0800132 memcpy(skb->sp->xvec + skb->sp->len, xfrm_vec,
133 xfrm_nr * sizeof(xfrm_vec[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 skb->sp->len += xfrm_nr;
135
Patrick McHardyb05e1062006-01-06 23:03:34 -0800136 nf_reset(skb);
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 if (decaps) {
139 if (!(skb->dev->flags&IFF_LOOPBACK)) {
140 dst_release(skb->dst);
141 skb->dst = NULL;
142 }
143 netif_rx(skb);
144 return 0;
145 } else {
Patrick McHardyb05e1062006-01-06 23:03:34 -0800146#ifdef CONFIG_NETFILTER
147 __skb_push(skb, skb->data - skb->nh.raw);
148 skb->nh.iph->tot_len = htons(skb->len);
149 ip_send_check(skb->nh.iph);
150
151 NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, skb->dev, NULL,
152 xfrm4_rcv_encap_finish);
153 return 0;
154#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return -skb->nh.iph->protocol;
Patrick McHardyb05e1062006-01-06 23:03:34 -0800156#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 }
158
159drop_unlock:
160 spin_unlock(&x->lock);
161 xfrm_state_put(x);
162drop:
163 while (--xfrm_nr >= 0)
Herbert Xudbe5b4a2006-04-01 00:54:16 -0800164 xfrm_state_put(xfrm_vec[xfrm_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 kfree_skb(skb);
167 return 0;
168}