]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
e905a9ed | 2 | * Linux NET3: IP/IP protocol decoder. |
1da177e4 LT |
3 | * |
4 | * Version: $Id: ipip.c,v 1.50 2001/10/02 02:22:36 davem Exp $ | |
5 | * | |
6 | * Authors: | |
7 | * Sam Lantinga (slouken@cs.ucdavis.edu) 02/01/95 | |
8 | * | |
9 | * Fixes: | |
10 | * Alan Cox : Merged and made usable non modular (its so tiny its silly as | |
11 | * a module taking up 2 pages). | |
12 | * Alan Cox : Fixed bug with 1.3.18 and IPIP not working (now needs to set skb->h.iph) | |
13 | * to keep ip_forward happy. | |
14 | * Alan Cox : More fixes for 1.3.21, and firewall fix. Maybe this will work soon 8). | |
15 | * Kai Schulte : Fixed #defines for IP_FIREWALL->FIREWALL | |
16 | * David Woodhouse : Perform some basic ICMP handling. | |
17 | * IPIP Routing without decapsulation. | |
18 | * Carlos Picoto : GRE over IP support | |
19 | * Alexey Kuznetsov: Reworked. Really, now it is truncated version of ipv4/ip_gre.c. | |
20 | * I do not want to merge them together. | |
21 | * | |
22 | * This program is free software; you can redistribute it and/or | |
23 | * modify it under the terms of the GNU General Public License | |
24 | * as published by the Free Software Foundation; either version | |
25 | * 2 of the License, or (at your option) any later version. | |
26 | * | |
27 | */ | |
28 | ||
29 | /* tunnel.c: an IP tunnel driver | |
30 | ||
31 | The purpose of this driver is to provide an IP tunnel through | |
32 | which you can tunnel network traffic transparently across subnets. | |
33 | ||
34 | This was written by looking at Nick Holloway's dummy driver | |
35 | Thanks for the great code! | |
36 | ||
37 | -Sam Lantinga (slouken@cs.ucdavis.edu) 02/01/95 | |
e905a9ed | 38 | |
1da177e4 LT |
39 | Minor tweaks: |
40 | Cleaned up the code a little and added some pre-1.3.0 tweaks. | |
41 | dev->hard_header/hard_header_len changed to use no headers. | |
42 | Comments/bracketing tweaked. | |
43 | Made the tunnels use dev->name not tunnel: when error reporting. | |
44 | Added tx_dropped stat | |
e905a9ed | 45 | |
1da177e4 LT |
46 | -Alan Cox (Alan.Cox@linux.org) 21 March 95 |
47 | ||
48 | Reworked: | |
49 | Changed to tunnel to destination gateway in addition to the | |
50 | tunnel's pointopoint address | |
51 | Almost completely rewritten | |
52 | Note: There is currently no firewall or ICMP handling done. | |
53 | ||
54 | -Sam Lantinga (slouken@cs.ucdavis.edu) 02/13/96 | |
e905a9ed | 55 | |
1da177e4 LT |
56 | */ |
57 | ||
58 | /* Things I wish I had known when writing the tunnel driver: | |
59 | ||
60 | When the tunnel_xmit() function is called, the skb contains the | |
61 | packet to be sent (plus a great deal of extra info), and dev | |
62 | contains the tunnel device that _we_ are. | |
63 | ||
64 | When we are passed a packet, we are expected to fill in the | |
65 | source address with our source IP address. | |
66 | ||
67 | What is the proper way to allocate, copy and free a buffer? | |
68 | After you allocate it, it is a "0 length" chunk of memory | |
69 | starting at zero. If you want to add headers to the buffer | |
70 | later, you'll have to call "skb_reserve(skb, amount)" with | |
71 | the amount of memory you want reserved. Then, you call | |
72 | "skb_put(skb, amount)" with the amount of space you want in | |
73 | the buffer. skb_put() returns a pointer to the top (#0) of | |
74 | that buffer. skb->len is set to the amount of space you have | |
75 | "allocated" with skb_put(). You can then write up to skb->len | |
76 | bytes to that buffer. If you need more, you can call skb_put() | |
77 | again with the additional amount of space you need. You can | |
e905a9ed | 78 | find out how much more space you can allocate by calling |
1da177e4 LT |
79 | "skb_tailroom(skb)". |
80 | Now, to add header space, call "skb_push(skb, header_len)". | |
81 | This creates space at the beginning of the buffer and returns | |
82 | a pointer to this new space. If later you need to strip a | |
83 | header from a buffer, call "skb_pull(skb, header_len)". | |
84 | skb_headroom() will return how much space is left at the top | |
85 | of the buffer (before the main data). Remember, this headroom | |
86 | space must be reserved before the skb_put() function is called. | |
87 | */ | |
88 | ||
89 | /* | |
90 | This version of net/ipv4/ipip.c is cloned of net/ipv4/ip_gre.c | |
91 | ||
92 | For comments look at net/ipv4/ip_gre.c --ANK | |
93 | */ | |
94 | ||
e905a9ed | 95 | |
4fc268d2 | 96 | #include <linux/capability.h> |
1da177e4 LT |
97 | #include <linux/module.h> |
98 | #include <linux/types.h> | |
1da177e4 LT |
99 | #include <linux/kernel.h> |
100 | #include <asm/uaccess.h> | |
101 | #include <linux/skbuff.h> | |
102 | #include <linux/netdevice.h> | |
103 | #include <linux/in.h> | |
104 | #include <linux/tcp.h> | |
105 | #include <linux/udp.h> | |
106 | #include <linux/if_arp.h> | |
107 | #include <linux/mroute.h> | |
108 | #include <linux/init.h> | |
109 | #include <linux/netfilter_ipv4.h> | |
46f25dff | 110 | #include <linux/if_ether.h> |
1da177e4 LT |
111 | |
112 | #include <net/sock.h> | |
113 | #include <net/ip.h> | |
114 | #include <net/icmp.h> | |
1da177e4 LT |
115 | #include <net/ipip.h> |
116 | #include <net/inet_ecn.h> | |
117 | #include <net/xfrm.h> | |
10dc4c7b PE |
118 | #include <net/net_namespace.h> |
119 | #include <net/netns/generic.h> | |
1da177e4 LT |
120 | |
121 | #define HASH_SIZE 16 | |
d5a0a1e3 | 122 | #define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&0xF) |
1da177e4 | 123 | |
10dc4c7b PE |
124 | static int ipip_net_id; |
125 | struct ipip_net { | |
44d3c299 PE |
126 | struct ip_tunnel *tunnels_r_l[HASH_SIZE]; |
127 | struct ip_tunnel *tunnels_r[HASH_SIZE]; | |
128 | struct ip_tunnel *tunnels_l[HASH_SIZE]; | |
129 | struct ip_tunnel *tunnels_wc[1]; | |
130 | struct ip_tunnel **tunnels[4]; | |
131 | ||
b9855c54 | 132 | struct net_device *fb_tunnel_dev; |
10dc4c7b PE |
133 | }; |
134 | ||
1da177e4 LT |
135 | static int ipip_fb_tunnel_init(struct net_device *dev); |
136 | static int ipip_tunnel_init(struct net_device *dev); | |
137 | static void ipip_tunnel_setup(struct net_device *dev); | |
138 | ||
1da177e4 LT |
139 | static DEFINE_RWLOCK(ipip_lock); |
140 | ||
b9fae5c9 PE |
141 | static struct ip_tunnel * ipip_tunnel_lookup(struct net *net, |
142 | __be32 remote, __be32 local) | |
1da177e4 LT |
143 | { |
144 | unsigned h0 = HASH(remote); | |
145 | unsigned h1 = HASH(local); | |
146 | struct ip_tunnel *t; | |
44d3c299 | 147 | struct ipip_net *ipn = net_generic(net, ipip_net_id); |
1da177e4 | 148 | |
44d3c299 | 149 | for (t = ipn->tunnels_r_l[h0^h1]; t; t = t->next) { |
1da177e4 LT |
150 | if (local == t->parms.iph.saddr && |
151 | remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP)) | |
152 | return t; | |
153 | } | |
44d3c299 | 154 | for (t = ipn->tunnels_r[h0]; t; t = t->next) { |
1da177e4 LT |
155 | if (remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP)) |
156 | return t; | |
157 | } | |
44d3c299 | 158 | for (t = ipn->tunnels_l[h1]; t; t = t->next) { |
1da177e4 LT |
159 | if (local == t->parms.iph.saddr && (t->dev->flags&IFF_UP)) |
160 | return t; | |
161 | } | |
44d3c299 | 162 | if ((t = ipn->tunnels_wc[0]) != NULL && (t->dev->flags&IFF_UP)) |
1da177e4 LT |
163 | return t; |
164 | return NULL; | |
165 | } | |
166 | ||
b9fae5c9 PE |
167 | static struct ip_tunnel **__ipip_bucket(struct ipip_net *ipn, |
168 | struct ip_tunnel_parm *parms) | |
1da177e4 | 169 | { |
87d1a164 YH |
170 | __be32 remote = parms->iph.daddr; |
171 | __be32 local = parms->iph.saddr; | |
1da177e4 LT |
172 | unsigned h = 0; |
173 | int prio = 0; | |
174 | ||
175 | if (remote) { | |
176 | prio |= 2; | |
177 | h ^= HASH(remote); | |
178 | } | |
179 | if (local) { | |
180 | prio |= 1; | |
181 | h ^= HASH(local); | |
182 | } | |
44d3c299 | 183 | return &ipn->tunnels[prio][h]; |
1da177e4 LT |
184 | } |
185 | ||
b9fae5c9 PE |
186 | static inline struct ip_tunnel **ipip_bucket(struct ipip_net *ipn, |
187 | struct ip_tunnel *t) | |
87d1a164 | 188 | { |
b9fae5c9 | 189 | return __ipip_bucket(ipn, &t->parms); |
87d1a164 | 190 | } |
1da177e4 | 191 | |
b9fae5c9 | 192 | static void ipip_tunnel_unlink(struct ipip_net *ipn, struct ip_tunnel *t) |
1da177e4 LT |
193 | { |
194 | struct ip_tunnel **tp; | |
195 | ||
b9fae5c9 | 196 | for (tp = ipip_bucket(ipn, t); *tp; tp = &(*tp)->next) { |
1da177e4 LT |
197 | if (t == *tp) { |
198 | write_lock_bh(&ipip_lock); | |
199 | *tp = t->next; | |
200 | write_unlock_bh(&ipip_lock); | |
201 | break; | |
202 | } | |
203 | } | |
204 | } | |
205 | ||
b9fae5c9 | 206 | static void ipip_tunnel_link(struct ipip_net *ipn, struct ip_tunnel *t) |
1da177e4 | 207 | { |
b9fae5c9 | 208 | struct ip_tunnel **tp = ipip_bucket(ipn, t); |
1da177e4 LT |
209 | |
210 | t->next = *tp; | |
211 | write_lock_bh(&ipip_lock); | |
212 | *tp = t; | |
213 | write_unlock_bh(&ipip_lock); | |
214 | } | |
215 | ||
b9fae5c9 PE |
216 | static struct ip_tunnel * ipip_tunnel_locate(struct net *net, |
217 | struct ip_tunnel_parm *parms, int create) | |
1da177e4 | 218 | { |
d5a0a1e3 AV |
219 | __be32 remote = parms->iph.daddr; |
220 | __be32 local = parms->iph.saddr; | |
1da177e4 LT |
221 | struct ip_tunnel *t, **tp, *nt; |
222 | struct net_device *dev; | |
1da177e4 | 223 | char name[IFNAMSIZ]; |
b9fae5c9 | 224 | struct ipip_net *ipn = net_generic(net, ipip_net_id); |
1da177e4 | 225 | |
b9fae5c9 | 226 | for (tp = __ipip_bucket(ipn, parms); (t = *tp) != NULL; tp = &t->next) { |
1da177e4 LT |
227 | if (local == t->parms.iph.saddr && remote == t->parms.iph.daddr) |
228 | return t; | |
229 | } | |
230 | if (!create) | |
231 | return NULL; | |
232 | ||
233 | if (parms->name[0]) | |
234 | strlcpy(name, parms->name, IFNAMSIZ); | |
34cc7ba6 PE |
235 | else |
236 | sprintf(name, "tunl%%d"); | |
1da177e4 LT |
237 | |
238 | dev = alloc_netdev(sizeof(*t), name, ipip_tunnel_setup); | |
239 | if (dev == NULL) | |
240 | return NULL; | |
241 | ||
b37d428b PE |
242 | if (strchr(name, '%')) { |
243 | if (dev_alloc_name(dev, name) < 0) | |
244 | goto failed_free; | |
245 | } | |
246 | ||
2941a486 | 247 | nt = netdev_priv(dev); |
1da177e4 LT |
248 | dev->init = ipip_tunnel_init; |
249 | nt->parms = *parms; | |
250 | ||
b37d428b PE |
251 | if (register_netdevice(dev) < 0) |
252 | goto failed_free; | |
1da177e4 LT |
253 | |
254 | dev_hold(dev); | |
b9fae5c9 | 255 | ipip_tunnel_link(ipn, nt); |
1da177e4 LT |
256 | return nt; |
257 | ||
b37d428b PE |
258 | failed_free: |
259 | free_netdev(dev); | |
1da177e4 LT |
260 | return NULL; |
261 | } | |
262 | ||
263 | static void ipip_tunnel_uninit(struct net_device *dev) | |
264 | { | |
b9855c54 PE |
265 | struct net *net = dev_net(dev); |
266 | struct ipip_net *ipn = net_generic(net, ipip_net_id); | |
267 | ||
268 | if (dev == ipn->fb_tunnel_dev) { | |
1da177e4 | 269 | write_lock_bh(&ipip_lock); |
44d3c299 | 270 | ipn->tunnels_wc[0] = NULL; |
1da177e4 LT |
271 | write_unlock_bh(&ipip_lock); |
272 | } else | |
b9fae5c9 | 273 | ipip_tunnel_unlink(ipn, netdev_priv(dev)); |
1da177e4 LT |
274 | dev_put(dev); |
275 | } | |
276 | ||
d2acc347 | 277 | static int ipip_err(struct sk_buff *skb, u32 info) |
1da177e4 LT |
278 | { |
279 | #ifndef I_WISH_WORLD_WERE_PERFECT | |
280 | ||
281 | /* It is not :-( All the routers (except for Linux) return only | |
282 | 8 bytes of packet payload. It means, that precise relaying of | |
283 | ICMP in the real Internet is absolutely infeasible. | |
284 | */ | |
285 | struct iphdr *iph = (struct iphdr*)skb->data; | |
88c7664f ACM |
286 | const int type = icmp_hdr(skb)->type; |
287 | const int code = icmp_hdr(skb)->code; | |
1da177e4 | 288 | struct ip_tunnel *t; |
d2acc347 | 289 | int err; |
1da177e4 LT |
290 | |
291 | switch (type) { | |
292 | default: | |
293 | case ICMP_PARAMETERPROB: | |
d2acc347 | 294 | return 0; |
1da177e4 LT |
295 | |
296 | case ICMP_DEST_UNREACH: | |
297 | switch (code) { | |
298 | case ICMP_SR_FAILED: | |
299 | case ICMP_PORT_UNREACH: | |
300 | /* Impossible event. */ | |
d2acc347 | 301 | return 0; |
1da177e4 LT |
302 | case ICMP_FRAG_NEEDED: |
303 | /* Soft state for pmtu is maintained by IP core. */ | |
d2acc347 | 304 | return 0; |
1da177e4 LT |
305 | default: |
306 | /* All others are translated to HOST_UNREACH. | |
307 | rfc2003 contains "deep thoughts" about NET_UNREACH, | |
308 | I believe they are just ether pollution. --ANK | |
309 | */ | |
310 | break; | |
311 | } | |
312 | break; | |
313 | case ICMP_TIME_EXCEEDED: | |
314 | if (code != ICMP_EXC_TTL) | |
d2acc347 | 315 | return 0; |
1da177e4 LT |
316 | break; |
317 | } | |
318 | ||
d2acc347 HX |
319 | err = -ENOENT; |
320 | ||
1da177e4 | 321 | read_lock(&ipip_lock); |
cec3ffae | 322 | t = ipip_tunnel_lookup(dev_net(skb->dev), iph->daddr, iph->saddr); |
1da177e4 LT |
323 | if (t == NULL || t->parms.iph.daddr == 0) |
324 | goto out; | |
d2acc347 HX |
325 | |
326 | err = 0; | |
1da177e4 LT |
327 | if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED) |
328 | goto out; | |
329 | ||
330 | if (jiffies - t->err_time < IPTUNNEL_ERR_TIMEO) | |
331 | t->err_count++; | |
332 | else | |
333 | t->err_count = 1; | |
334 | t->err_time = jiffies; | |
335 | out: | |
336 | read_unlock(&ipip_lock); | |
d2acc347 | 337 | return err; |
1da177e4 LT |
338 | #else |
339 | struct iphdr *iph = (struct iphdr*)dp; | |
340 | int hlen = iph->ihl<<2; | |
341 | struct iphdr *eiph; | |
88c7664f ACM |
342 | const int type = icmp_hdr(skb)->type; |
343 | const int code = icmp_hdr(skb)->code; | |
1da177e4 LT |
344 | int rel_type = 0; |
345 | int rel_code = 0; | |
c55e2f49 AV |
346 | __be32 rel_info = 0; |
347 | __u32 n = 0; | |
1da177e4 LT |
348 | struct sk_buff *skb2; |
349 | struct flowi fl; | |
350 | struct rtable *rt; | |
351 | ||
352 | if (len < hlen + sizeof(struct iphdr)) | |
d2acc347 | 353 | return 0; |
1da177e4 LT |
354 | eiph = (struct iphdr*)(dp + hlen); |
355 | ||
356 | switch (type) { | |
357 | default: | |
d2acc347 | 358 | return 0; |
1da177e4 | 359 | case ICMP_PARAMETERPROB: |
88c7664f | 360 | n = ntohl(icmp_hdr(skb)->un.gateway) >> 24; |
c55e2f49 | 361 | if (n < hlen) |
d2acc347 | 362 | return 0; |
1da177e4 LT |
363 | |
364 | /* So... This guy found something strange INSIDE encapsulated | |
365 | packet. Well, he is fool, but what can we do ? | |
366 | */ | |
367 | rel_type = ICMP_PARAMETERPROB; | |
c55e2f49 | 368 | rel_info = htonl((n - hlen) << 24); |
1da177e4 LT |
369 | break; |
370 | ||
371 | case ICMP_DEST_UNREACH: | |
372 | switch (code) { | |
373 | case ICMP_SR_FAILED: | |
374 | case ICMP_PORT_UNREACH: | |
375 | /* Impossible event. */ | |
d2acc347 | 376 | return 0; |
1da177e4 LT |
377 | case ICMP_FRAG_NEEDED: |
378 | /* And it is the only really necessary thing :-) */ | |
88c7664f | 379 | n = ntohs(icmp_hdr(skb)->un.frag.mtu); |
c55e2f49 | 380 | if (n < hlen+68) |
d2acc347 | 381 | return 0; |
c55e2f49 | 382 | n -= hlen; |
1da177e4 | 383 | /* BSD 4.2 MORE DOES NOT EXIST IN NATURE. */ |
c55e2f49 | 384 | if (n > ntohs(eiph->tot_len)) |
d2acc347 | 385 | return 0; |
c55e2f49 | 386 | rel_info = htonl(n); |
1da177e4 LT |
387 | break; |
388 | default: | |
389 | /* All others are translated to HOST_UNREACH. | |
390 | rfc2003 contains "deep thoughts" about NET_UNREACH, | |
391 | I believe, it is just ether pollution. --ANK | |
392 | */ | |
393 | rel_type = ICMP_DEST_UNREACH; | |
394 | rel_code = ICMP_HOST_UNREACH; | |
395 | break; | |
396 | } | |
397 | break; | |
398 | case ICMP_TIME_EXCEEDED: | |
399 | if (code != ICMP_EXC_TTL) | |
d2acc347 | 400 | return 0; |
1da177e4 LT |
401 | break; |
402 | } | |
403 | ||
404 | /* Prepare fake skb to feed it to icmp_send */ | |
405 | skb2 = skb_clone(skb, GFP_ATOMIC); | |
406 | if (skb2 == NULL) | |
d2acc347 | 407 | return 0; |
1da177e4 LT |
408 | dst_release(skb2->dst); |
409 | skb2->dst = NULL; | |
410 | skb_pull(skb2, skb->data - (u8*)eiph); | |
c1d2bbe1 | 411 | skb_reset_network_header(skb2); |
1da177e4 LT |
412 | |
413 | /* Try to guess incoming interface */ | |
414 | memset(&fl, 0, sizeof(fl)); | |
415 | fl.fl4_daddr = eiph->saddr; | |
416 | fl.fl4_tos = RT_TOS(eiph->tos); | |
417 | fl.proto = IPPROTO_IPIP; | |
b99f0152 | 418 | if (ip_route_output_key(dev_net(skb->dev), &rt, &key)) { |
1da177e4 | 419 | kfree_skb(skb2); |
d2acc347 | 420 | return 0; |
1da177e4 LT |
421 | } |
422 | skb2->dev = rt->u.dst.dev; | |
423 | ||
424 | /* route "incoming" packet */ | |
425 | if (rt->rt_flags&RTCF_LOCAL) { | |
426 | ip_rt_put(rt); | |
427 | rt = NULL; | |
428 | fl.fl4_daddr = eiph->daddr; | |
429 | fl.fl4_src = eiph->saddr; | |
430 | fl.fl4_tos = eiph->tos; | |
b99f0152 | 431 | if (ip_route_output_key(dev_net(skb->dev), &rt, &fl) || |
1da177e4 LT |
432 | rt->u.dst.dev->type != ARPHRD_TUNNEL) { |
433 | ip_rt_put(rt); | |
434 | kfree_skb(skb2); | |
d2acc347 | 435 | return 0; |
1da177e4 LT |
436 | } |
437 | } else { | |
438 | ip_rt_put(rt); | |
439 | if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos, skb2->dev) || | |
440 | skb2->dst->dev->type != ARPHRD_TUNNEL) { | |
441 | kfree_skb(skb2); | |
d2acc347 | 442 | return 0; |
1da177e4 LT |
443 | } |
444 | } | |
445 | ||
446 | /* change mtu on this route */ | |
447 | if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) { | |
c55e2f49 | 448 | if (n > dst_mtu(skb2->dst)) { |
1da177e4 | 449 | kfree_skb(skb2); |
d2acc347 | 450 | return 0; |
1da177e4 | 451 | } |
c55e2f49 | 452 | skb2->dst->ops->update_pmtu(skb2->dst, n); |
1da177e4 | 453 | } else if (type == ICMP_TIME_EXCEEDED) { |
2941a486 | 454 | struct ip_tunnel *t = netdev_priv(skb2->dev); |
1da177e4 LT |
455 | if (t->parms.iph.ttl) { |
456 | rel_type = ICMP_DEST_UNREACH; | |
457 | rel_code = ICMP_HOST_UNREACH; | |
458 | } | |
459 | } | |
460 | ||
461 | icmp_send(skb2, rel_type, rel_code, rel_info); | |
462 | kfree_skb(skb2); | |
d2acc347 | 463 | return 0; |
1da177e4 LT |
464 | #endif |
465 | } | |
466 | ||
eddc9ec5 ACM |
467 | static inline void ipip_ecn_decapsulate(const struct iphdr *outer_iph, |
468 | struct sk_buff *skb) | |
1da177e4 | 469 | { |
eddc9ec5 | 470 | struct iphdr *inner_iph = ip_hdr(skb); |
1da177e4 LT |
471 | |
472 | if (INET_ECN_is_ce(outer_iph->tos)) | |
473 | IP_ECN_set_ce(inner_iph); | |
474 | } | |
475 | ||
476 | static int ipip_rcv(struct sk_buff *skb) | |
477 | { | |
1da177e4 | 478 | struct ip_tunnel *tunnel; |
eddc9ec5 | 479 | const struct iphdr *iph = ip_hdr(skb); |
1da177e4 LT |
480 | |
481 | read_lock(&ipip_lock); | |
cec3ffae | 482 | if ((tunnel = ipip_tunnel_lookup(dev_net(skb->dev), |
b9fae5c9 | 483 | iph->saddr, iph->daddr)) != NULL) { |
1da177e4 LT |
484 | if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) { |
485 | read_unlock(&ipip_lock); | |
486 | kfree_skb(skb); | |
487 | return 0; | |
488 | } | |
489 | ||
490 | secpath_reset(skb); | |
491 | ||
b0e380b1 | 492 | skb->mac_header = skb->network_header; |
c1d2bbe1 | 493 | skb_reset_network_header(skb); |
1da177e4 LT |
494 | skb->protocol = htons(ETH_P_IP); |
495 | skb->pkt_type = PACKET_HOST; | |
496 | ||
497 | tunnel->stat.rx_packets++; | |
498 | tunnel->stat.rx_bytes += skb->len; | |
499 | skb->dev = tunnel->dev; | |
500 | dst_release(skb->dst); | |
501 | skb->dst = NULL; | |
502 | nf_reset(skb); | |
503 | ipip_ecn_decapsulate(iph, skb); | |
504 | netif_rx(skb); | |
505 | read_unlock(&ipip_lock); | |
506 | return 0; | |
507 | } | |
508 | read_unlock(&ipip_lock); | |
509 | ||
1da177e4 LT |
510 | return -1; |
511 | } | |
512 | ||
513 | /* | |
514 | * This function assumes it is being called from dev_queue_xmit() | |
515 | * and that skb is filled properly by that function. | |
516 | */ | |
517 | ||
518 | static int ipip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev) | |
519 | { | |
2941a486 | 520 | struct ip_tunnel *tunnel = netdev_priv(dev); |
1da177e4 LT |
521 | struct net_device_stats *stats = &tunnel->stat; |
522 | struct iphdr *tiph = &tunnel->parms.iph; | |
523 | u8 tos = tunnel->parms.iph.tos; | |
d5a0a1e3 | 524 | __be16 df = tiph->frag_off; |
1da177e4 LT |
525 | struct rtable *rt; /* Route to the other host */ |
526 | struct net_device *tdev; /* Device to other host */ | |
eddc9ec5 | 527 | struct iphdr *old_iph = ip_hdr(skb); |
1da177e4 | 528 | struct iphdr *iph; /* Our new IP header */ |
c2636b4d | 529 | unsigned int max_headroom; /* The extra header space needed */ |
d5a0a1e3 | 530 | __be32 dst = tiph->daddr; |
1da177e4 LT |
531 | int mtu; |
532 | ||
533 | if (tunnel->recursion++) { | |
534 | tunnel->stat.collisions++; | |
535 | goto tx_error; | |
536 | } | |
537 | ||
538 | if (skb->protocol != htons(ETH_P_IP)) | |
539 | goto tx_error; | |
540 | ||
541 | if (tos&1) | |
542 | tos = old_iph->tos; | |
543 | ||
544 | if (!dst) { | |
545 | /* NBMA tunnel */ | |
ee6b9673 | 546 | if ((rt = skb->rtable) == NULL) { |
1da177e4 LT |
547 | tunnel->stat.tx_fifo_errors++; |
548 | goto tx_error; | |
549 | } | |
550 | if ((dst = rt->rt_gateway) == 0) | |
551 | goto tx_error_icmp; | |
552 | } | |
553 | ||
554 | { | |
555 | struct flowi fl = { .oif = tunnel->parms.link, | |
556 | .nl_u = { .ip4_u = | |
557 | { .daddr = dst, | |
558 | .saddr = tiph->saddr, | |
559 | .tos = RT_TOS(tos) } }, | |
560 | .proto = IPPROTO_IPIP }; | |
b99f0152 | 561 | if (ip_route_output_key(dev_net(dev), &rt, &fl)) { |
1da177e4 LT |
562 | tunnel->stat.tx_carrier_errors++; |
563 | goto tx_error_icmp; | |
564 | } | |
565 | } | |
566 | tdev = rt->u.dst.dev; | |
567 | ||
568 | if (tdev == dev) { | |
569 | ip_rt_put(rt); | |
570 | tunnel->stat.collisions++; | |
571 | goto tx_error; | |
572 | } | |
573 | ||
574 | if (tiph->frag_off) | |
575 | mtu = dst_mtu(&rt->u.dst) - sizeof(struct iphdr); | |
576 | else | |
577 | mtu = skb->dst ? dst_mtu(skb->dst) : dev->mtu; | |
578 | ||
579 | if (mtu < 68) { | |
580 | tunnel->stat.collisions++; | |
581 | ip_rt_put(rt); | |
582 | goto tx_error; | |
583 | } | |
584 | if (skb->dst) | |
585 | skb->dst->ops->update_pmtu(skb->dst, mtu); | |
586 | ||
587 | df |= (old_iph->frag_off&htons(IP_DF)); | |
588 | ||
589 | if ((old_iph->frag_off&htons(IP_DF)) && mtu < ntohs(old_iph->tot_len)) { | |
590 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu)); | |
591 | ip_rt_put(rt); | |
592 | goto tx_error; | |
593 | } | |
594 | ||
595 | if (tunnel->err_count > 0) { | |
596 | if (jiffies - tunnel->err_time < IPTUNNEL_ERR_TIMEO) { | |
597 | tunnel->err_count--; | |
598 | dst_link_failure(skb); | |
599 | } else | |
600 | tunnel->err_count = 0; | |
601 | } | |
602 | ||
603 | /* | |
604 | * Okay, now see if we can stuff it in the buffer as-is. | |
605 | */ | |
606 | max_headroom = (LL_RESERVED_SPACE(tdev)+sizeof(struct iphdr)); | |
607 | ||
cfbba49d PM |
608 | if (skb_headroom(skb) < max_headroom || skb_shared(skb) || |
609 | (skb_cloned(skb) && !skb_clone_writable(skb, 0))) { | |
1da177e4 LT |
610 | struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom); |
611 | if (!new_skb) { | |
612 | ip_rt_put(rt); | |
e905a9ed | 613 | stats->tx_dropped++; |
1da177e4 LT |
614 | dev_kfree_skb(skb); |
615 | tunnel->recursion--; | |
616 | return 0; | |
617 | } | |
618 | if (skb->sk) | |
619 | skb_set_owner_w(new_skb, skb->sk); | |
620 | dev_kfree_skb(skb); | |
621 | skb = new_skb; | |
eddc9ec5 | 622 | old_iph = ip_hdr(skb); |
1da177e4 LT |
623 | } |
624 | ||
b0e380b1 | 625 | skb->transport_header = skb->network_header; |
e2d1bca7 ACM |
626 | skb_push(skb, sizeof(struct iphdr)); |
627 | skb_reset_network_header(skb); | |
1da177e4 | 628 | memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); |
48d5cad8 PM |
629 | IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED | |
630 | IPSKB_REROUTED); | |
1da177e4 LT |
631 | dst_release(skb->dst); |
632 | skb->dst = &rt->u.dst; | |
633 | ||
634 | /* | |
635 | * Push down and install the IPIP header. | |
636 | */ | |
637 | ||
eddc9ec5 | 638 | iph = ip_hdr(skb); |
1da177e4 LT |
639 | iph->version = 4; |
640 | iph->ihl = sizeof(struct iphdr)>>2; | |
641 | iph->frag_off = df; | |
642 | iph->protocol = IPPROTO_IPIP; | |
643 | iph->tos = INET_ECN_encapsulate(tos, old_iph->tos); | |
644 | iph->daddr = rt->rt_dst; | |
645 | iph->saddr = rt->rt_src; | |
646 | ||
647 | if ((iph->ttl = tiph->ttl) == 0) | |
648 | iph->ttl = old_iph->ttl; | |
649 | ||
650 | nf_reset(skb); | |
651 | ||
652 | IPTUNNEL_XMIT(); | |
653 | tunnel->recursion--; | |
654 | return 0; | |
655 | ||
656 | tx_error_icmp: | |
657 | dst_link_failure(skb); | |
658 | tx_error: | |
659 | stats->tx_errors++; | |
660 | dev_kfree_skb(skb); | |
661 | tunnel->recursion--; | |
662 | return 0; | |
663 | } | |
664 | ||
5533995b MS |
665 | static void ipip_tunnel_bind_dev(struct net_device *dev) |
666 | { | |
667 | struct net_device *tdev = NULL; | |
668 | struct ip_tunnel *tunnel; | |
669 | struct iphdr *iph; | |
670 | ||
671 | tunnel = netdev_priv(dev); | |
672 | iph = &tunnel->parms.iph; | |
673 | ||
674 | if (iph->daddr) { | |
675 | struct flowi fl = { .oif = tunnel->parms.link, | |
676 | .nl_u = { .ip4_u = | |
677 | { .daddr = iph->daddr, | |
678 | .saddr = iph->saddr, | |
679 | .tos = RT_TOS(iph->tos) } }, | |
680 | .proto = IPPROTO_IPIP }; | |
681 | struct rtable *rt; | |
b99f0152 | 682 | if (!ip_route_output_key(dev_net(dev), &rt, &fl)) { |
5533995b MS |
683 | tdev = rt->u.dst.dev; |
684 | ip_rt_put(rt); | |
685 | } | |
686 | dev->flags |= IFF_POINTOPOINT; | |
687 | } | |
688 | ||
689 | if (!tdev && tunnel->parms.link) | |
b99f0152 | 690 | tdev = __dev_get_by_index(dev_net(dev), tunnel->parms.link); |
5533995b MS |
691 | |
692 | if (tdev) { | |
693 | dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr); | |
694 | dev->mtu = tdev->mtu - sizeof(struct iphdr); | |
695 | } | |
696 | dev->iflink = tunnel->parms.link; | |
697 | } | |
698 | ||
1da177e4 LT |
699 | static int |
700 | ipip_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd) | |
701 | { | |
702 | int err = 0; | |
703 | struct ip_tunnel_parm p; | |
704 | struct ip_tunnel *t; | |
b9855c54 PE |
705 | struct net *net = dev_net(dev); |
706 | struct ipip_net *ipn = net_generic(net, ipip_net_id); | |
1da177e4 LT |
707 | |
708 | switch (cmd) { | |
709 | case SIOCGETTUNNEL: | |
710 | t = NULL; | |
b9855c54 | 711 | if (dev == ipn->fb_tunnel_dev) { |
1da177e4 LT |
712 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) { |
713 | err = -EFAULT; | |
714 | break; | |
715 | } | |
b9fae5c9 | 716 | t = ipip_tunnel_locate(net, &p, 0); |
1da177e4 LT |
717 | } |
718 | if (t == NULL) | |
2941a486 | 719 | t = netdev_priv(dev); |
1da177e4 LT |
720 | memcpy(&p, &t->parms, sizeof(p)); |
721 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) | |
722 | err = -EFAULT; | |
723 | break; | |
724 | ||
725 | case SIOCADDTUNNEL: | |
726 | case SIOCCHGTUNNEL: | |
727 | err = -EPERM; | |
728 | if (!capable(CAP_NET_ADMIN)) | |
729 | goto done; | |
730 | ||
731 | err = -EFAULT; | |
732 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) | |
733 | goto done; | |
734 | ||
735 | err = -EINVAL; | |
736 | if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPIP || | |
737 | p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF))) | |
738 | goto done; | |
739 | if (p.iph.ttl) | |
740 | p.iph.frag_off |= htons(IP_DF); | |
741 | ||
b9fae5c9 | 742 | t = ipip_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL); |
1da177e4 | 743 | |
b9855c54 | 744 | if (dev != ipn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) { |
1da177e4 LT |
745 | if (t != NULL) { |
746 | if (t->dev != dev) { | |
747 | err = -EEXIST; | |
748 | break; | |
749 | } | |
750 | } else { | |
751 | if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) || | |
752 | (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) { | |
753 | err = -EINVAL; | |
754 | break; | |
755 | } | |
2941a486 | 756 | t = netdev_priv(dev); |
b9fae5c9 | 757 | ipip_tunnel_unlink(ipn, t); |
1da177e4 LT |
758 | t->parms.iph.saddr = p.iph.saddr; |
759 | t->parms.iph.daddr = p.iph.daddr; | |
760 | memcpy(dev->dev_addr, &p.iph.saddr, 4); | |
761 | memcpy(dev->broadcast, &p.iph.daddr, 4); | |
b9fae5c9 | 762 | ipip_tunnel_link(ipn, t); |
1da177e4 LT |
763 | netdev_state_change(dev); |
764 | } | |
765 | } | |
766 | ||
767 | if (t) { | |
768 | err = 0; | |
769 | if (cmd == SIOCCHGTUNNEL) { | |
770 | t->parms.iph.ttl = p.iph.ttl; | |
771 | t->parms.iph.tos = p.iph.tos; | |
772 | t->parms.iph.frag_off = p.iph.frag_off; | |
5533995b MS |
773 | if (t->parms.link != p.link) { |
774 | t->parms.link = p.link; | |
775 | ipip_tunnel_bind_dev(dev); | |
776 | netdev_state_change(dev); | |
777 | } | |
1da177e4 LT |
778 | } |
779 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p))) | |
780 | err = -EFAULT; | |
781 | } else | |
782 | err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT); | |
783 | break; | |
784 | ||
785 | case SIOCDELTUNNEL: | |
786 | err = -EPERM; | |
787 | if (!capable(CAP_NET_ADMIN)) | |
788 | goto done; | |
789 | ||
b9855c54 | 790 | if (dev == ipn->fb_tunnel_dev) { |
1da177e4 LT |
791 | err = -EFAULT; |
792 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) | |
793 | goto done; | |
794 | err = -ENOENT; | |
b9fae5c9 | 795 | if ((t = ipip_tunnel_locate(net, &p, 0)) == NULL) |
1da177e4 LT |
796 | goto done; |
797 | err = -EPERM; | |
b9855c54 | 798 | if (t->dev == ipn->fb_tunnel_dev) |
1da177e4 LT |
799 | goto done; |
800 | dev = t->dev; | |
801 | } | |
22f8cde5 SH |
802 | unregister_netdevice(dev); |
803 | err = 0; | |
1da177e4 LT |
804 | break; |
805 | ||
806 | default: | |
807 | err = -EINVAL; | |
808 | } | |
809 | ||
810 | done: | |
811 | return err; | |
812 | } | |
813 | ||
814 | static struct net_device_stats *ipip_tunnel_get_stats(struct net_device *dev) | |
815 | { | |
2941a486 | 816 | return &(((struct ip_tunnel*)netdev_priv(dev))->stat); |
1da177e4 LT |
817 | } |
818 | ||
819 | static int ipip_tunnel_change_mtu(struct net_device *dev, int new_mtu) | |
820 | { | |
821 | if (new_mtu < 68 || new_mtu > 0xFFF8 - sizeof(struct iphdr)) | |
822 | return -EINVAL; | |
823 | dev->mtu = new_mtu; | |
824 | return 0; | |
825 | } | |
826 | ||
827 | static void ipip_tunnel_setup(struct net_device *dev) | |
828 | { | |
1da177e4 LT |
829 | dev->uninit = ipip_tunnel_uninit; |
830 | dev->hard_start_xmit = ipip_tunnel_xmit; | |
831 | dev->get_stats = ipip_tunnel_get_stats; | |
832 | dev->do_ioctl = ipip_tunnel_ioctl; | |
833 | dev->change_mtu = ipip_tunnel_change_mtu; | |
834 | dev->destructor = free_netdev; | |
835 | ||
836 | dev->type = ARPHRD_TUNNEL; | |
837 | dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr); | |
46f25dff | 838 | dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr); |
1da177e4 LT |
839 | dev->flags = IFF_NOARP; |
840 | dev->iflink = 0; | |
841 | dev->addr_len = 4; | |
842 | } | |
843 | ||
844 | static int ipip_tunnel_init(struct net_device *dev) | |
845 | { | |
1da177e4 | 846 | struct ip_tunnel *tunnel; |
1da177e4 | 847 | |
2941a486 | 848 | tunnel = netdev_priv(dev); |
1da177e4 LT |
849 | |
850 | tunnel->dev = dev; | |
851 | strcpy(tunnel->parms.name, dev->name); | |
852 | ||
853 | memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4); | |
854 | memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4); | |
855 | ||
5533995b | 856 | ipip_tunnel_bind_dev(dev); |
1da177e4 LT |
857 | |
858 | return 0; | |
859 | } | |
860 | ||
b9855c54 | 861 | static int ipip_fb_tunnel_init(struct net_device *dev) |
1da177e4 | 862 | { |
2941a486 | 863 | struct ip_tunnel *tunnel = netdev_priv(dev); |
1da177e4 | 864 | struct iphdr *iph = &tunnel->parms.iph; |
44d3c299 | 865 | struct ipip_net *ipn = net_generic(dev_net(dev), ipip_net_id); |
1da177e4 LT |
866 | |
867 | tunnel->dev = dev; | |
868 | strcpy(tunnel->parms.name, dev->name); | |
869 | ||
870 | iph->version = 4; | |
871 | iph->protocol = IPPROTO_IPIP; | |
872 | iph->ihl = 5; | |
873 | ||
874 | dev_hold(dev); | |
44d3c299 | 875 | ipn->tunnels_wc[0] = tunnel; |
1da177e4 LT |
876 | return 0; |
877 | } | |
878 | ||
879 | static struct xfrm_tunnel ipip_handler = { | |
880 | .handler = ipip_rcv, | |
881 | .err_handler = ipip_err, | |
d2acc347 | 882 | .priority = 1, |
1da177e4 LT |
883 | }; |
884 | ||
885 | static char banner[] __initdata = | |
886 | KERN_INFO "IPv4 over IPv4 tunneling driver\n"; | |
887 | ||
44d3c299 PE |
888 | static void ipip_destroy_tunnels(struct ipip_net *ipn) |
889 | { | |
890 | int prio; | |
891 | ||
892 | for (prio = 1; prio < 4; prio++) { | |
893 | int h; | |
894 | for (h = 0; h < HASH_SIZE; h++) { | |
895 | struct ip_tunnel *t; | |
896 | while ((t = ipn->tunnels[prio][h]) != NULL) | |
897 | unregister_netdevice(t->dev); | |
898 | } | |
899 | } | |
900 | } | |
901 | ||
10dc4c7b PE |
902 | static int ipip_init_net(struct net *net) |
903 | { | |
904 | int err; | |
905 | struct ipip_net *ipn; | |
906 | ||
907 | err = -ENOMEM; | |
44d3c299 | 908 | ipn = kzalloc(sizeof(struct ipip_net), GFP_KERNEL); |
10dc4c7b PE |
909 | if (ipn == NULL) |
910 | goto err_alloc; | |
911 | ||
912 | err = net_assign_generic(net, ipip_net_id, ipn); | |
913 | if (err < 0) | |
914 | goto err_assign; | |
915 | ||
44d3c299 PE |
916 | ipn->tunnels[0] = ipn->tunnels_wc; |
917 | ipn->tunnels[1] = ipn->tunnels_l; | |
918 | ipn->tunnels[2] = ipn->tunnels_r; | |
919 | ipn->tunnels[3] = ipn->tunnels_r_l; | |
920 | ||
b9855c54 PE |
921 | ipn->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), |
922 | "tunl0", | |
923 | ipip_tunnel_setup); | |
924 | if (!ipn->fb_tunnel_dev) { | |
925 | err = -ENOMEM; | |
926 | goto err_alloc_dev; | |
927 | } | |
928 | ||
929 | ipn->fb_tunnel_dev->init = ipip_fb_tunnel_init; | |
930 | dev_net_set(ipn->fb_tunnel_dev, net); | |
931 | ||
932 | if ((err = register_netdev(ipn->fb_tunnel_dev))) | |
933 | goto err_reg_dev; | |
934 | ||
10dc4c7b PE |
935 | return 0; |
936 | ||
b9855c54 PE |
937 | err_reg_dev: |
938 | free_netdev(ipn->fb_tunnel_dev); | |
939 | err_alloc_dev: | |
940 | /* nothing */ | |
10dc4c7b PE |
941 | err_assign: |
942 | kfree(ipn); | |
943 | err_alloc: | |
944 | return err; | |
945 | } | |
946 | ||
947 | static void ipip_exit_net(struct net *net) | |
948 | { | |
949 | struct ipip_net *ipn; | |
950 | ||
951 | ipn = net_generic(net, ipip_net_id); | |
b9855c54 | 952 | rtnl_lock(); |
44d3c299 | 953 | ipip_destroy_tunnels(ipn); |
b9855c54 PE |
954 | unregister_netdevice(ipn->fb_tunnel_dev); |
955 | rtnl_unlock(); | |
10dc4c7b PE |
956 | kfree(ipn); |
957 | } | |
958 | ||
959 | static struct pernet_operations ipip_net_ops = { | |
960 | .init = ipip_init_net, | |
961 | .exit = ipip_exit_net, | |
962 | }; | |
963 | ||
1da177e4 LT |
964 | static int __init ipip_init(void) |
965 | { | |
966 | int err; | |
967 | ||
968 | printk(banner); | |
969 | ||
c0d56408 | 970 | if (xfrm4_tunnel_register(&ipip_handler, AF_INET)) { |
1da177e4 LT |
971 | printk(KERN_INFO "ipip init: can't register tunnel\n"); |
972 | return -EAGAIN; | |
973 | } | |
974 | ||
10dc4c7b PE |
975 | err = register_pernet_gen_device(&ipip_net_id, &ipip_net_ops); |
976 | if (err) | |
b9855c54 PE |
977 | xfrm4_tunnel_deregister(&ipip_handler, AF_INET); |
978 | ||
1da177e4 | 979 | return err; |
1da177e4 LT |
980 | } |
981 | ||
982 | static void __exit ipip_fini(void) | |
983 | { | |
c0d56408 | 984 | if (xfrm4_tunnel_deregister(&ipip_handler, AF_INET)) |
1da177e4 LT |
985 | printk(KERN_INFO "ipip close: can't deregister tunnel\n"); |
986 | ||
10dc4c7b | 987 | unregister_pernet_gen_device(ipip_net_id, &ipip_net_ops); |
1da177e4 LT |
988 | } |
989 | ||
990 | module_init(ipip_init); | |
991 | module_exit(ipip_fini); | |
992 | MODULE_LICENSE("GPL"); |