]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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> | |
b05e1066 PM |
14 | #include <linux/netfilter.h> |
15 | #include <linux/netfilter_ipv4.h> | |
1da177e4 LT |
16 | #include <net/inet_ecn.h> |
17 | #include <net/ip.h> | |
18 | #include <net/xfrm.h> | |
19 | ||
20 | int xfrm4_rcv(struct sk_buff *skb) | |
21 | { | |
22 | return xfrm4_rcv_encap(skb, 0); | |
23 | } | |
24 | ||
25 | EXPORT_SYMBOL(xfrm4_rcv); | |
26 | ||
27 | static inline void ipip_ecn_decapsulate(struct sk_buff *skb) | |
28 | { | |
29 | struct iphdr *outer_iph = skb->nh.iph; | |
30 | struct iphdr *inner_iph = skb->h.ipiph; | |
31 | ||
32 | if (INET_ECN_is_ce(outer_iph->tos)) | |
33 | IP_ECN_set_ce(inner_iph); | |
34 | } | |
35 | ||
36 | static int xfrm4_parse_spi(struct sk_buff *skb, u8 nexthdr, u32 *spi, u32 *seq) | |
37 | { | |
38 | switch (nexthdr) { | |
39 | case IPPROTO_IPIP: | |
40 | if (!pskb_may_pull(skb, sizeof(struct iphdr))) | |
41 | return -EINVAL; | |
42 | *spi = skb->nh.iph->saddr; | |
43 | *seq = 0; | |
44 | return 0; | |
45 | } | |
46 | ||
47 | return xfrm_parse_spi(skb, nexthdr, spi, seq); | |
48 | } | |
49 | ||
b05e1066 PM |
50 | #ifdef CONFIG_NETFILTER |
51 | static inline int xfrm4_rcv_encap_finish(struct sk_buff *skb) | |
52 | { | |
53 | struct iphdr *iph = skb->nh.iph; | |
54 | ||
55 | if (skb->dst == NULL) { | |
56 | if (ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, | |
57 | skb->dev)) | |
58 | goto drop; | |
59 | } | |
60 | return dst_input(skb); | |
61 | drop: | |
62 | kfree_skb(skb); | |
63 | return NET_RX_DROP; | |
64 | } | |
65 | #endif | |
66 | ||
1da177e4 LT |
67 | int xfrm4_rcv_encap(struct sk_buff *skb, __u16 encap_type) |
68 | { | |
69 | int err; | |
70 | u32 spi, seq; | |
71 | struct sec_decap_state xfrm_vec[XFRM_MAX_DEPTH]; | |
72 | struct xfrm_state *x; | |
73 | int xfrm_nr = 0; | |
74 | int decaps = 0; | |
75 | ||
76 | if ((err = xfrm4_parse_spi(skb, skb->nh.iph->protocol, &spi, &seq)) != 0) | |
77 | goto drop; | |
78 | ||
79 | do { | |
80 | struct iphdr *iph = skb->nh.iph; | |
81 | ||
82 | if (xfrm_nr == XFRM_MAX_DEPTH) | |
83 | goto drop; | |
84 | ||
85 | x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, spi, iph->protocol, AF_INET); | |
86 | if (x == NULL) | |
87 | goto drop; | |
88 | ||
89 | spin_lock(&x->lock); | |
90 | if (unlikely(x->km.state != XFRM_STATE_VALID)) | |
91 | goto drop_unlock; | |
92 | ||
e695633e HX |
93 | if (x->encap->encap_type != encap_type) |
94 | goto drop_unlock; | |
95 | ||
1da177e4 LT |
96 | if (x->props.replay_window && xfrm_replay_check(x, seq)) |
97 | goto drop_unlock; | |
98 | ||
99 | if (xfrm_state_check_expire(x)) | |
100 | goto drop_unlock; | |
101 | ||
102 | xfrm_vec[xfrm_nr].decap.decap_type = encap_type; | |
e695633e | 103 | if (x->type->input(x, skb)) |
1da177e4 LT |
104 | goto drop_unlock; |
105 | ||
106 | /* only the first xfrm gets the encap type */ | |
107 | encap_type = 0; | |
108 | ||
109 | if (x->props.replay_window) | |
110 | xfrm_replay_advance(x, seq); | |
111 | ||
112 | x->curlft.bytes += skb->len; | |
113 | x->curlft.packets++; | |
114 | ||
115 | spin_unlock(&x->lock); | |
116 | ||
117 | xfrm_vec[xfrm_nr++].xvec = x; | |
118 | ||
119 | iph = skb->nh.iph; | |
120 | ||
121 | if (x->props.mode) { | |
122 | if (iph->protocol != IPPROTO_IPIP) | |
123 | goto drop; | |
124 | if (!pskb_may_pull(skb, sizeof(struct iphdr))) | |
125 | goto drop; | |
126 | if (skb_cloned(skb) && | |
127 | pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) | |
128 | goto drop; | |
129 | if (x->props.flags & XFRM_STATE_DECAP_DSCP) | |
130 | ipv4_copy_dscp(iph, skb->h.ipiph); | |
131 | if (!(x->props.flags & XFRM_STATE_NOECN)) | |
132 | ipip_ecn_decapsulate(skb); | |
133 | skb->mac.raw = memmove(skb->data - skb->mac_len, | |
134 | skb->mac.raw, skb->mac_len); | |
135 | skb->nh.raw = skb->data; | |
136 | memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options)); | |
137 | decaps = 1; | |
138 | break; | |
139 | } | |
140 | ||
141 | if ((err = xfrm_parse_spi(skb, skb->nh.iph->protocol, &spi, &seq)) < 0) | |
142 | goto drop; | |
143 | } while (!err); | |
144 | ||
145 | /* Allocate new secpath or COW existing one. */ | |
146 | ||
147 | if (!skb->sp || atomic_read(&skb->sp->refcnt) != 1) { | |
148 | struct sec_path *sp; | |
149 | sp = secpath_dup(skb->sp); | |
150 | if (!sp) | |
151 | goto drop; | |
152 | if (skb->sp) | |
153 | secpath_put(skb->sp); | |
154 | skb->sp = sp; | |
155 | } | |
156 | if (xfrm_nr + skb->sp->len > XFRM_MAX_DEPTH) | |
157 | goto drop; | |
158 | ||
159 | memcpy(skb->sp->x+skb->sp->len, xfrm_vec, xfrm_nr*sizeof(struct sec_decap_state)); | |
160 | skb->sp->len += xfrm_nr; | |
161 | ||
b05e1066 PM |
162 | nf_reset(skb); |
163 | ||
1da177e4 LT |
164 | if (decaps) { |
165 | if (!(skb->dev->flags&IFF_LOOPBACK)) { | |
166 | dst_release(skb->dst); | |
167 | skb->dst = NULL; | |
168 | } | |
169 | netif_rx(skb); | |
170 | return 0; | |
171 | } else { | |
b05e1066 PM |
172 | #ifdef CONFIG_NETFILTER |
173 | __skb_push(skb, skb->data - skb->nh.raw); | |
174 | skb->nh.iph->tot_len = htons(skb->len); | |
175 | ip_send_check(skb->nh.iph); | |
176 | ||
177 | NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, skb->dev, NULL, | |
178 | xfrm4_rcv_encap_finish); | |
179 | return 0; | |
180 | #else | |
1da177e4 | 181 | return -skb->nh.iph->protocol; |
b05e1066 | 182 | #endif |
1da177e4 LT |
183 | } |
184 | ||
185 | drop_unlock: | |
186 | spin_unlock(&x->lock); | |
187 | xfrm_state_put(x); | |
188 | drop: | |
189 | while (--xfrm_nr >= 0) | |
190 | xfrm_state_put(xfrm_vec[xfrm_nr].xvec); | |
191 | ||
192 | kfree_skb(skb); | |
193 | return 0; | |
194 | } |