blob: 1de39e1dc4324d2b094bf166c5f461482c82d950 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002 * Linux NET3: IP/IP protocol decoder.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090038
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 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
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 -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
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056*/
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
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090078 find out how much more space you can allocate by calling
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 "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
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090095
Randy Dunlap4fc268d2006-01-11 12:17:47 -080096#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#include <linux/module.h>
98#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#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>
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800110#include <linux/if_ether.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112#include <net/sock.h>
113#include <net/ip.h>
114#include <net/icmp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115#include <net/ipip.h>
116#include <net/inet_ecn.h>
117#include <net/xfrm.h>
Pavel Emelyanov10dc4c72008-04-16 01:03:13 -0700118#include <net/net_namespace.h>
119#include <net/netns/generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121#define HASH_SIZE 16
Al Virod5a0a1e2006-11-08 00:23:14 -0800122#define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&0xF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Pavel Emelyanov10dc4c72008-04-16 01:03:13 -0700124static int ipip_net_id;
125struct ipip_net {
Pavel Emelyanov44d3c292008-04-16 01:05:32 -0700126 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
Pavel Emelyanovb9855c52008-04-16 01:04:13 -0700132 struct net_device *fb_tunnel_dev;
Pavel Emelyanov10dc4c72008-04-16 01:03:13 -0700133};
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135static int ipip_fb_tunnel_init(struct net_device *dev);
136static int ipip_tunnel_init(struct net_device *dev);
137static void ipip_tunnel_setup(struct net_device *dev);
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139static DEFINE_RWLOCK(ipip_lock);
140
Pavel Emelyanovb9fae5c2008-04-16 01:04:35 -0700141static struct ip_tunnel * ipip_tunnel_lookup(struct net *net,
142 __be32 remote, __be32 local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 unsigned h0 = HASH(remote);
145 unsigned h1 = HASH(local);
146 struct ip_tunnel *t;
Pavel Emelyanov44d3c292008-04-16 01:05:32 -0700147 struct ipip_net *ipn = net_generic(net, ipip_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Pavel Emelyanov44d3c292008-04-16 01:05:32 -0700149 for (t = ipn->tunnels_r_l[h0^h1]; t; t = t->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (local == t->parms.iph.saddr &&
151 remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP))
152 return t;
153 }
Pavel Emelyanov44d3c292008-04-16 01:05:32 -0700154 for (t = ipn->tunnels_r[h0]; t; t = t->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 if (remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP))
156 return t;
157 }
Pavel Emelyanov44d3c292008-04-16 01:05:32 -0700158 for (t = ipn->tunnels_l[h1]; t; t = t->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (local == t->parms.iph.saddr && (t->dev->flags&IFF_UP))
160 return t;
161 }
Pavel Emelyanov44d3c292008-04-16 01:05:32 -0700162 if ((t = ipn->tunnels_wc[0]) != NULL && (t->dev->flags&IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 return t;
164 return NULL;
165}
166
Pavel Emelyanovb9fae5c2008-04-16 01:04:35 -0700167static struct ip_tunnel **__ipip_bucket(struct ipip_net *ipn,
168 struct ip_tunnel_parm *parms)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
YOSHIFUJI Hideaki87d1a162007-04-24 20:44:47 +0900170 __be32 remote = parms->iph.daddr;
171 __be32 local = parms->iph.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 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 }
Pavel Emelyanov44d3c292008-04-16 01:05:32 -0700183 return &ipn->tunnels[prio][h];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
Pavel Emelyanovb9fae5c2008-04-16 01:04:35 -0700186static inline struct ip_tunnel **ipip_bucket(struct ipip_net *ipn,
187 struct ip_tunnel *t)
YOSHIFUJI Hideaki87d1a162007-04-24 20:44:47 +0900188{
Pavel Emelyanovb9fae5c2008-04-16 01:04:35 -0700189 return __ipip_bucket(ipn, &t->parms);
YOSHIFUJI Hideaki87d1a162007-04-24 20:44:47 +0900190}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Pavel Emelyanovb9fae5c2008-04-16 01:04:35 -0700192static void ipip_tunnel_unlink(struct ipip_net *ipn, struct ip_tunnel *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 struct ip_tunnel **tp;
195
Pavel Emelyanovb9fae5c2008-04-16 01:04:35 -0700196 for (tp = ipip_bucket(ipn, t); *tp; tp = &(*tp)->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 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
Pavel Emelyanovb9fae5c2008-04-16 01:04:35 -0700206static void ipip_tunnel_link(struct ipip_net *ipn, struct ip_tunnel *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Pavel Emelyanovb9fae5c2008-04-16 01:04:35 -0700208 struct ip_tunnel **tp = ipip_bucket(ipn, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 t->next = *tp;
211 write_lock_bh(&ipip_lock);
212 *tp = t;
213 write_unlock_bh(&ipip_lock);
214}
215
Pavel Emelyanovb9fae5c2008-04-16 01:04:35 -0700216static struct ip_tunnel * ipip_tunnel_locate(struct net *net,
217 struct ip_tunnel_parm *parms, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Al Virod5a0a1e2006-11-08 00:23:14 -0800219 __be32 remote = parms->iph.daddr;
220 __be32 local = parms->iph.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 struct ip_tunnel *t, **tp, *nt;
222 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 char name[IFNAMSIZ];
Pavel Emelyanovb9fae5c2008-04-16 01:04:35 -0700224 struct ipip_net *ipn = net_generic(net, ipip_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Pavel Emelyanovb9fae5c2008-04-16 01:04:35 -0700226 for (tp = __ipip_bucket(ipn, parms); (t = *tp) != NULL; tp = &t->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 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);
Pavel Emelyanov34cc7ba2008-02-23 20:19:20 -0800235 else
236 sprintf(name, "tunl%%d");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 dev = alloc_netdev(sizeof(*t), name, ipip_tunnel_setup);
239 if (dev == NULL)
240 return NULL;
241
Pavel Emelyanovb37d4282008-02-26 23:51:04 -0800242 if (strchr(name, '%')) {
243 if (dev_alloc_name(dev, name) < 0)
244 goto failed_free;
245 }
246
Patrick McHardy2941a482006-01-08 22:05:26 -0800247 nt = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 dev->init = ipip_tunnel_init;
249 nt->parms = *parms;
250
Pavel Emelyanovb37d4282008-02-26 23:51:04 -0800251 if (register_netdevice(dev) < 0)
252 goto failed_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 dev_hold(dev);
Pavel Emelyanovb9fae5c2008-04-16 01:04:35 -0700255 ipip_tunnel_link(ipn, nt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return nt;
257
Pavel Emelyanovb37d4282008-02-26 23:51:04 -0800258failed_free:
259 free_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return NULL;
261}
262
263static void ipip_tunnel_uninit(struct net_device *dev)
264{
Pavel Emelyanovb9855c52008-04-16 01:04:13 -0700265 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) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 write_lock_bh(&ipip_lock);
Pavel Emelyanov44d3c292008-04-16 01:05:32 -0700270 ipn->tunnels_wc[0] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 write_unlock_bh(&ipip_lock);
272 } else
Pavel Emelyanovb9fae5c2008-04-16 01:04:35 -0700273 ipip_tunnel_unlink(ipn, netdev_priv(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 dev_put(dev);
275}
276
Herbert Xud2acc342006-03-28 01:12:13 -0800277static int ipip_err(struct sk_buff *skb, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
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;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300286 const int type = icmp_hdr(skb)->type;
287 const int code = icmp_hdr(skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 struct ip_tunnel *t;
Herbert Xud2acc342006-03-28 01:12:13 -0800289 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 switch (type) {
292 default:
293 case ICMP_PARAMETERPROB:
Herbert Xud2acc342006-03-28 01:12:13 -0800294 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 case ICMP_DEST_UNREACH:
297 switch (code) {
298 case ICMP_SR_FAILED:
299 case ICMP_PORT_UNREACH:
300 /* Impossible event. */
Herbert Xud2acc342006-03-28 01:12:13 -0800301 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 case ICMP_FRAG_NEEDED:
303 /* Soft state for pmtu is maintained by IP core. */
Herbert Xud2acc342006-03-28 01:12:13 -0800304 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 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)
Herbert Xud2acc342006-03-28 01:12:13 -0800315 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 break;
317 }
318
Herbert Xud2acc342006-03-28 01:12:13 -0800319 err = -ENOENT;
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 read_lock(&ipip_lock);
Pavel Emelyanovcec3ffa2008-04-16 01:05:03 -0700322 t = ipip_tunnel_lookup(dev_net(skb->dev), iph->daddr, iph->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 if (t == NULL || t->parms.iph.daddr == 0)
324 goto out;
Herbert Xud2acc342006-03-28 01:12:13 -0800325
326 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 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;
335out:
336 read_unlock(&ipip_lock);
Herbert Xud2acc342006-03-28 01:12:13 -0800337 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338#else
339 struct iphdr *iph = (struct iphdr*)dp;
340 int hlen = iph->ihl<<2;
341 struct iphdr *eiph;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300342 const int type = icmp_hdr(skb)->type;
343 const int code = icmp_hdr(skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 int rel_type = 0;
345 int rel_code = 0;
Al Viroc55e2f42006-09-19 13:23:19 -0700346 __be32 rel_info = 0;
347 __u32 n = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 struct sk_buff *skb2;
349 struct flowi fl;
350 struct rtable *rt;
351
352 if (len < hlen + sizeof(struct iphdr))
Herbert Xud2acc342006-03-28 01:12:13 -0800353 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 eiph = (struct iphdr*)(dp + hlen);
355
356 switch (type) {
357 default:
Herbert Xud2acc342006-03-28 01:12:13 -0800358 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 case ICMP_PARAMETERPROB:
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300360 n = ntohl(icmp_hdr(skb)->un.gateway) >> 24;
Al Viroc55e2f42006-09-19 13:23:19 -0700361 if (n < hlen)
Herbert Xud2acc342006-03-28 01:12:13 -0800362 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
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;
Al Viroc55e2f42006-09-19 13:23:19 -0700368 rel_info = htonl((n - hlen) << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 break;
370
371 case ICMP_DEST_UNREACH:
372 switch (code) {
373 case ICMP_SR_FAILED:
374 case ICMP_PORT_UNREACH:
375 /* Impossible event. */
Herbert Xud2acc342006-03-28 01:12:13 -0800376 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 case ICMP_FRAG_NEEDED:
378 /* And it is the only really necessary thing :-) */
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300379 n = ntohs(icmp_hdr(skb)->un.frag.mtu);
Al Viroc55e2f42006-09-19 13:23:19 -0700380 if (n < hlen+68)
Herbert Xud2acc342006-03-28 01:12:13 -0800381 return 0;
Al Viroc55e2f42006-09-19 13:23:19 -0700382 n -= hlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 /* BSD 4.2 MORE DOES NOT EXIST IN NATURE. */
Al Viroc55e2f42006-09-19 13:23:19 -0700384 if (n > ntohs(eiph->tot_len))
Herbert Xud2acc342006-03-28 01:12:13 -0800385 return 0;
Al Viroc55e2f42006-09-19 13:23:19 -0700386 rel_info = htonl(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 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)
Herbert Xud2acc342006-03-28 01:12:13 -0800400 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 break;
402 }
403
404 /* Prepare fake skb to feed it to icmp_send */
405 skb2 = skb_clone(skb, GFP_ATOMIC);
406 if (skb2 == NULL)
Herbert Xud2acc342006-03-28 01:12:13 -0800407 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 dst_release(skb2->dst);
409 skb2->dst = NULL;
410 skb_pull(skb2, skb->data - (u8*)eiph);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700411 skb_reset_network_header(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
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;
Pavel Emelyanovb99f0152008-04-16 01:05:57 -0700418 if (ip_route_output_key(dev_net(skb->dev), &rt, &key)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 kfree_skb(skb2);
Herbert Xud2acc342006-03-28 01:12:13 -0800420 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
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;
Pavel Emelyanovb99f0152008-04-16 01:05:57 -0700431 if (ip_route_output_key(dev_net(skb->dev), &rt, &fl) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 rt->u.dst.dev->type != ARPHRD_TUNNEL) {
433 ip_rt_put(rt);
434 kfree_skb(skb2);
Herbert Xud2acc342006-03-28 01:12:13 -0800435 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
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);
Herbert Xud2acc342006-03-28 01:12:13 -0800442 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444 }
445
446 /* change mtu on this route */
447 if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
Al Viroc55e2f42006-09-19 13:23:19 -0700448 if (n > dst_mtu(skb2->dst)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 kfree_skb(skb2);
Herbert Xud2acc342006-03-28 01:12:13 -0800450 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
Al Viroc55e2f42006-09-19 13:23:19 -0700452 skb2->dst->ops->update_pmtu(skb2->dst, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 } else if (type == ICMP_TIME_EXCEEDED) {
Patrick McHardy2941a482006-01-08 22:05:26 -0800454 struct ip_tunnel *t = netdev_priv(skb2->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 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);
Herbert Xud2acc342006-03-28 01:12:13 -0800463 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464#endif
465}
466
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700467static inline void ipip_ecn_decapsulate(const struct iphdr *outer_iph,
468 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700470 struct iphdr *inner_iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 if (INET_ECN_is_ce(outer_iph->tos))
473 IP_ECN_set_ce(inner_iph);
474}
475
476static int ipip_rcv(struct sk_buff *skb)
477{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 struct ip_tunnel *tunnel;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700479 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 read_lock(&ipip_lock);
Pavel Emelyanovcec3ffa2008-04-16 01:05:03 -0700482 if ((tunnel = ipip_tunnel_lookup(dev_net(skb->dev),
Pavel Emelyanovb9fae5c2008-04-16 01:04:35 -0700483 iph->saddr, iph->daddr)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 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
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700492 skb->mac_header = skb->network_header;
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700493 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 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
518static int ipip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
519{
Patrick McHardy2941a482006-01-08 22:05:26 -0800520 struct ip_tunnel *tunnel = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 struct net_device_stats *stats = &tunnel->stat;
522 struct iphdr *tiph = &tunnel->parms.iph;
523 u8 tos = tunnel->parms.iph.tos;
Al Virod5a0a1e2006-11-08 00:23:14 -0800524 __be16 df = tiph->frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 struct rtable *rt; /* Route to the other host */
526 struct net_device *tdev; /* Device to other host */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700527 struct iphdr *old_iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 struct iphdr *iph; /* Our new IP header */
Chuck Leverc2636b42007-10-23 21:07:32 -0700529 unsigned int max_headroom; /* The extra header space needed */
Al Virod5a0a1e2006-11-08 00:23:14 -0800530 __be32 dst = tiph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 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 */
Eric Dumazetee6b9672008-03-05 18:30:47 -0800546 if ((rt = skb->rtable) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 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 };
Pavel Emelyanovb99f0152008-04-16 01:05:57 -0700561 if (ip_route_output_key(dev_net(dev), &rt, &fl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 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
Patrick McHardycfbba492007-07-09 15:33:40 -0700608 if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
609 (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
611 if (!new_skb) {
612 ip_rt_put(rt);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900613 stats->tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 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;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700622 old_iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
624
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700625 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -0700626 skb_push(skb, sizeof(struct iphdr));
627 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800629 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
630 IPSKB_REROUTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 dst_release(skb->dst);
632 skb->dst = &rt->u.dst;
633
634 /*
635 * Push down and install the IPIP header.
636 */
637
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700638 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 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
656tx_error_icmp:
657 dst_link_failure(skb);
658tx_error:
659 stats->tx_errors++;
660 dev_kfree_skb(skb);
661 tunnel->recursion--;
662 return 0;
663}
664
Michal Schmidt55339952007-12-12 11:01:43 -0800665static 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;
Pavel Emelyanovb99f0152008-04-16 01:05:57 -0700682 if (!ip_route_output_key(dev_net(dev), &rt, &fl)) {
Michal Schmidt55339952007-12-12 11:01:43 -0800683 tdev = rt->u.dst.dev;
684 ip_rt_put(rt);
685 }
686 dev->flags |= IFF_POINTOPOINT;
687 }
688
689 if (!tdev && tunnel->parms.link)
Pavel Emelyanovb99f0152008-04-16 01:05:57 -0700690 tdev = __dev_get_by_index(dev_net(dev), tunnel->parms.link);
Michal Schmidt55339952007-12-12 11:01:43 -0800691
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699static int
700ipip_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;
Pavel Emelyanovb9855c52008-04-16 01:04:13 -0700705 struct net *net = dev_net(dev);
706 struct ipip_net *ipn = net_generic(net, ipip_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 switch (cmd) {
709 case SIOCGETTUNNEL:
710 t = NULL;
Pavel Emelyanovb9855c52008-04-16 01:04:13 -0700711 if (dev == ipn->fb_tunnel_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
713 err = -EFAULT;
714 break;
715 }
Pavel Emelyanovb9fae5c2008-04-16 01:04:35 -0700716 t = ipip_tunnel_locate(net, &p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
718 if (t == NULL)
Patrick McHardy2941a482006-01-08 22:05:26 -0800719 t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 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
Pavel Emelyanovb9fae5c2008-04-16 01:04:35 -0700742 t = ipip_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Pavel Emelyanovb9855c52008-04-16 01:04:13 -0700744 if (dev != ipn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 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 }
Patrick McHardy2941a482006-01-08 22:05:26 -0800756 t = netdev_priv(dev);
Pavel Emelyanovb9fae5c2008-04-16 01:04:35 -0700757 ipip_tunnel_unlink(ipn, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 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);
Pavel Emelyanovb9fae5c2008-04-16 01:04:35 -0700762 ipip_tunnel_link(ipn, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 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;
Michal Schmidt55339952007-12-12 11:01:43 -0800773 if (t->parms.link != p.link) {
774 t->parms.link = p.link;
775 ipip_tunnel_bind_dev(dev);
776 netdev_state_change(dev);
777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
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
Pavel Emelyanovb9855c52008-04-16 01:04:13 -0700790 if (dev == ipn->fb_tunnel_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 err = -EFAULT;
792 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
793 goto done;
794 err = -ENOENT;
Pavel Emelyanovb9fae5c2008-04-16 01:04:35 -0700795 if ((t = ipip_tunnel_locate(net, &p, 0)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 goto done;
797 err = -EPERM;
Pavel Emelyanovb9855c52008-04-16 01:04:13 -0700798 if (t->dev == ipn->fb_tunnel_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 goto done;
800 dev = t->dev;
801 }
Stephen Hemminger22f8cde2007-02-07 00:09:58 -0800802 unregister_netdevice(dev);
803 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 break;
805
806 default:
807 err = -EINVAL;
808 }
809
810done:
811 return err;
812}
813
814static struct net_device_stats *ipip_tunnel_get_stats(struct net_device *dev)
815{
Patrick McHardy2941a482006-01-08 22:05:26 -0800816 return &(((struct ip_tunnel*)netdev_priv(dev))->stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
818
819static 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
827static void ipip_tunnel_setup(struct net_device *dev)
828{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 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);
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800838 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 dev->flags = IFF_NOARP;
840 dev->iflink = 0;
841 dev->addr_len = 4;
842}
843
844static int ipip_tunnel_init(struct net_device *dev)
845{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 struct ip_tunnel *tunnel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Patrick McHardy2941a482006-01-08 22:05:26 -0800848 tunnel = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
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
Michal Schmidt55339952007-12-12 11:01:43 -0800856 ipip_tunnel_bind_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 return 0;
859}
860
Pavel Emelyanovb9855c52008-04-16 01:04:13 -0700861static int ipip_fb_tunnel_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
Patrick McHardy2941a482006-01-08 22:05:26 -0800863 struct ip_tunnel *tunnel = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 struct iphdr *iph = &tunnel->parms.iph;
Pavel Emelyanov44d3c292008-04-16 01:05:32 -0700865 struct ipip_net *ipn = net_generic(dev_net(dev), ipip_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
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);
Pavel Emelyanov44d3c292008-04-16 01:05:32 -0700875 ipn->tunnels_wc[0] = tunnel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return 0;
877}
878
879static struct xfrm_tunnel ipip_handler = {
880 .handler = ipip_rcv,
881 .err_handler = ipip_err,
Herbert Xud2acc342006-03-28 01:12:13 -0800882 .priority = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883};
884
885static char banner[] __initdata =
886 KERN_INFO "IPv4 over IPv4 tunneling driver\n";
887
Pavel Emelyanov44d3c292008-04-16 01:05:32 -0700888static 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
Pavel Emelyanov10dc4c72008-04-16 01:03:13 -0700902static int ipip_init_net(struct net *net)
903{
904 int err;
905 struct ipip_net *ipn;
906
907 err = -ENOMEM;
Pavel Emelyanov44d3c292008-04-16 01:05:32 -0700908 ipn = kzalloc(sizeof(struct ipip_net), GFP_KERNEL);
Pavel Emelyanov10dc4c72008-04-16 01:03:13 -0700909 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
Pavel Emelyanov44d3c292008-04-16 01:05:32 -0700916 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
Pavel Emelyanovb9855c52008-04-16 01:04:13 -0700921 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
Pavel Emelyanov10dc4c72008-04-16 01:03:13 -0700935 return 0;
936
Pavel Emelyanovb9855c52008-04-16 01:04:13 -0700937err_reg_dev:
938 free_netdev(ipn->fb_tunnel_dev);
939err_alloc_dev:
940 /* nothing */
Pavel Emelyanov10dc4c72008-04-16 01:03:13 -0700941err_assign:
942 kfree(ipn);
943err_alloc:
944 return err;
945}
946
947static void ipip_exit_net(struct net *net)
948{
949 struct ipip_net *ipn;
950
951 ipn = net_generic(net, ipip_net_id);
Pavel Emelyanovb9855c52008-04-16 01:04:13 -0700952 rtnl_lock();
Pavel Emelyanov44d3c292008-04-16 01:05:32 -0700953 ipip_destroy_tunnels(ipn);
Pavel Emelyanovb9855c52008-04-16 01:04:13 -0700954 unregister_netdevice(ipn->fb_tunnel_dev);
955 rtnl_unlock();
Pavel Emelyanov10dc4c72008-04-16 01:03:13 -0700956 kfree(ipn);
957}
958
959static struct pernet_operations ipip_net_ops = {
960 .init = ipip_init_net,
961 .exit = ipip_exit_net,
962};
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964static int __init ipip_init(void)
965{
966 int err;
967
968 printk(banner);
969
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800970 if (xfrm4_tunnel_register(&ipip_handler, AF_INET)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 printk(KERN_INFO "ipip init: can't register tunnel\n");
972 return -EAGAIN;
973 }
974
Pavel Emelyanov10dc4c72008-04-16 01:03:13 -0700975 err = register_pernet_gen_device(&ipip_net_id, &ipip_net_ops);
976 if (err)
Pavel Emelyanovb9855c52008-04-16 01:04:13 -0700977 xfrm4_tunnel_deregister(&ipip_handler, AF_INET);
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
981
982static void __exit ipip_fini(void)
983{
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800984 if (xfrm4_tunnel_deregister(&ipip_handler, AF_INET))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 printk(KERN_INFO "ipip close: can't deregister tunnel\n");
986
Pavel Emelyanov10dc4c72008-04-16 01:03:13 -0700987 unregister_pernet_gen_device(ipip_net_id, &ipip_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
989
990module_init(ipip_init);
991module_exit(ipip_fini);
992MODULE_LICENSE("GPL");