blob: e9bbfde19ac32420a6dfd77b090e8b22fd97edc6 [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
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09009 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
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
Al Viro6067b2b2006-09-27 18:47:59 -070019static int xfrm4_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070020{
21 switch (nexthdr) {
22 case IPPROTO_IPIP:
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -080023 case IPPROTO_IPV6:
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070024 *spi = ip_hdr(skb)->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 *seq = 0;
26 return 0;
27 }
28
29 return xfrm_parse_spi(skb, nexthdr, spi, seq);
30}
31
Patrick McHardyb05e1062006-01-06 23:03:34 -080032#ifdef CONFIG_NETFILTER
33static inline int xfrm4_rcv_encap_finish(struct sk_buff *skb)
34{
Patrick McHardyb05e1062006-01-06 23:03:34 -080035 if (skb->dst == NULL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070036 const struct iphdr *iph = ip_hdr(skb);
37
Patrick McHardyb05e1062006-01-06 23:03:34 -080038 if (ip_route_input(skb, iph->daddr, iph->saddr, iph->tos,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090039 skb->dev))
Patrick McHardyb05e1062006-01-06 23:03:34 -080040 goto drop;
41 }
42 return dst_input(skb);
43drop:
44 kfree_skb(skb);
45 return NET_RX_DROP;
46}
47#endif
48
James Chapman067b2072007-07-05 17:08:05 -070049static int xfrm4_rcv_encap(struct sk_buff *skb, __u16 encap_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Al Viro6067b2b2006-09-27 18:47:59 -070051 __be32 spi, seq;
Herbert Xudbe5b4a2006-04-01 00:54:16 -080052 struct xfrm_state *xfrm_vec[XFRM_MAX_DEPTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 struct xfrm_state *x;
54 int xfrm_nr = 0;
55 int decaps = 0;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070056 int err = xfrm4_parse_spi(skb, ip_hdr(skb)->protocol, &spi, &seq);
Herbert Xu631a66982007-10-10 15:46:21 -070057 unsigned int nhoff = offsetof(struct iphdr, protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070059 if (err != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 goto drop;
61
62 do {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070063 const struct iphdr *iph = ip_hdr(skb);
Herbert Xu631a66982007-10-10 15:46:21 -070064 int nexthdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 if (xfrm_nr == XFRM_MAX_DEPTH)
67 goto drop;
68
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -080069 x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, spi,
70 iph->protocol != IPPROTO_IPV6 ? iph->protocol : IPPROTO_IPIP, AF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 if (x == NULL)
72 goto drop;
73
74 spin_lock(&x->lock);
75 if (unlikely(x->km.state != XFRM_STATE_VALID))
76 goto drop_unlock;
77
Herbert Xu8bf4b8a2006-04-04 12:51:05 -070078 if ((x->encap ? x->encap->encap_type : 0) != encap_type)
Herbert Xue6956332006-04-01 00:52:46 -080079 goto drop_unlock;
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 if (x->props.replay_window && xfrm_replay_check(x, seq))
82 goto drop_unlock;
83
84 if (xfrm_state_check_expire(x))
85 goto drop_unlock;
86
Herbert Xu631a66982007-10-10 15:46:21 -070087 nexthdr = x->type->input(x, skb);
88 if (nexthdr <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 goto drop_unlock;
90
Herbert Xu631a66982007-10-10 15:46:21 -070091 skb_network_header(skb)[nhoff] = nexthdr;
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 /* 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
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700114 err = xfrm_parse_spi(skb, ip_hdr(skb)->protocol, &spi, &seq);
115 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 goto drop;
117 } while (!err);
118
119 /* Allocate new secpath or COW existing one. */
120
121 if (!skb->sp || atomic_read(&skb->sp->refcnt) != 1) {
122 struct sec_path *sp;
123 sp = secpath_dup(skb->sp);
124 if (!sp)
125 goto drop;
126 if (skb->sp)
127 secpath_put(skb->sp);
128 skb->sp = sp;
129 }
130 if (xfrm_nr + skb->sp->len > XFRM_MAX_DEPTH)
131 goto drop;
132
Herbert Xudbe5b4a2006-04-01 00:54:16 -0800133 memcpy(skb->sp->xvec + skb->sp->len, xfrm_vec,
134 xfrm_nr * sizeof(xfrm_vec[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 skb->sp->len += xfrm_nr;
136
Patrick McHardyb05e1062006-01-06 23:03:34 -0800137 nf_reset(skb);
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 if (decaps) {
Kazunori MIYAZAWAf282d452007-05-29 13:03:17 -0700140 dst_release(skb->dst);
141 skb->dst = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 netif_rx(skb);
143 return 0;
144 } else {
Patrick McHardyb05e1062006-01-06 23:03:34 -0800145#ifdef CONFIG_NETFILTER
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700146 __skb_push(skb, skb->data - skb_network_header(skb));
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700147 ip_hdr(skb)->tot_len = htons(skb->len);
148 ip_send_check(ip_hdr(skb));
Patrick McHardyb05e1062006-01-06 23:03:34 -0800149
150 NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, skb->dev, NULL,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900151 xfrm4_rcv_encap_finish);
Patrick McHardyb05e1062006-01-06 23:03:34 -0800152 return 0;
153#else
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700154 return -ip_hdr(skb)->protocol;
Patrick McHardyb05e1062006-01-06 23:03:34 -0800155#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 }
157
158drop_unlock:
159 spin_unlock(&x->lock);
160 xfrm_state_put(x);
161drop:
162 while (--xfrm_nr >= 0)
Herbert Xudbe5b4a2006-04-01 00:54:16 -0800163 xfrm_state_put(xfrm_vec[xfrm_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 kfree_skb(skb);
166 return 0;
167}
James Chapman067b2072007-07-05 17:08:05 -0700168
169/* If it's a keepalive packet, then just eat it.
170 * If it's an encapsulated packet, then pass it to the
171 * IPsec xfrm input.
172 * Returns 0 if skb passed to xfrm or was dropped.
173 * Returns >0 if skb should be passed to UDP.
174 * Returns <0 if skb should be resubmitted (-ret is protocol)
175 */
176int xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb)
177{
178 struct udp_sock *up = udp_sk(sk);
179 struct udphdr *uh;
180 struct iphdr *iph;
181 int iphlen, len;
182 int ret;
183
184 __u8 *udpdata;
185 __be32 *udpdata32;
186 __u16 encap_type = up->encap_type;
187
188 /* if this is not encapsulated socket, then just return now */
189 if (!encap_type)
190 return 1;
191
192 /* If this is a paged skb, make sure we pull up
193 * whatever data we need to look at. */
194 len = skb->len - sizeof(struct udphdr);
195 if (!pskb_may_pull(skb, sizeof(struct udphdr) + min(len, 8)))
196 return 1;
197
198 /* Now we can get the pointers */
199 uh = udp_hdr(skb);
200 udpdata = (__u8 *)uh + sizeof(struct udphdr);
201 udpdata32 = (__be32 *)udpdata;
202
203 switch (encap_type) {
204 default:
205 case UDP_ENCAP_ESPINUDP:
206 /* Check if this is a keepalive packet. If so, eat it. */
207 if (len == 1 && udpdata[0] == 0xff) {
208 goto drop;
209 } else if (len > sizeof(struct ip_esp_hdr) && udpdata32[0] != 0) {
210 /* ESP Packet without Non-ESP header */
211 len = sizeof(struct udphdr);
212 } else
213 /* Must be an IKE packet.. pass it through */
214 return 1;
215 break;
216 case UDP_ENCAP_ESPINUDP_NON_IKE:
217 /* Check if this is a keepalive packet. If so, eat it. */
218 if (len == 1 && udpdata[0] == 0xff) {
219 goto drop;
220 } else if (len > 2 * sizeof(u32) + sizeof(struct ip_esp_hdr) &&
221 udpdata32[0] == 0 && udpdata32[1] == 0) {
222
223 /* ESP Packet with Non-IKE marker */
224 len = sizeof(struct udphdr) + 2 * sizeof(u32);
225 } else
226 /* Must be an IKE packet.. pass it through */
227 return 1;
228 break;
229 }
230
231 /* At this point we are sure that this is an ESPinUDP packet,
232 * so we need to remove 'len' bytes from the packet (the UDP
233 * header and optional ESP marker bytes) and then modify the
234 * protocol to ESP, and then call into the transform receiver.
235 */
236 if (skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
237 goto drop;
238
239 /* Now we can update and verify the packet length... */
240 iph = ip_hdr(skb);
241 iphlen = iph->ihl << 2;
242 iph->tot_len = htons(ntohs(iph->tot_len) - len);
243 if (skb->len < iphlen + len) {
244 /* packet is too small!?! */
245 goto drop;
246 }
247
248 /* pull the data buffer up to the ESP header and set the
249 * transport header to point to ESP. Keep UDP on the stack
250 * for later.
251 */
252 __skb_pull(skb, len);
253 skb_reset_transport_header(skb);
254
255 /* modify the protocol (it's ESP!) */
256 iph->protocol = IPPROTO_ESP;
257
258 /* process ESP */
259 ret = xfrm4_rcv_encap(skb, encap_type);
260 return ret;
261
262drop:
263 kfree_skb(skb);
264 return 0;
265}
266
267int xfrm4_rcv(struct sk_buff *skb)
268{
269 return xfrm4_rcv_encap(skb, 0);
270}
271
272EXPORT_SYMBOL(xfrm4_rcv);