blob: 720855e41100476945afda37c59c622572267dd8 [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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Authors:
5 * Sam Lantinga (slouken@cs.ucdavis.edu) 02/01/95
6 *
7 * Fixes:
8 * Alan Cox : Merged and made usable non modular (its so tiny its silly as
9 * a module taking up 2 pages).
10 * Alan Cox : Fixed bug with 1.3.18 and IPIP not working (now needs to set skb->h.iph)
11 * to keep ip_forward happy.
12 * Alan Cox : More fixes for 1.3.21, and firewall fix. Maybe this will work soon 8).
13 * Kai Schulte : Fixed #defines for IP_FIREWALL->FIREWALL
14 * David Woodhouse : Perform some basic ICMP handling.
15 * IPIP Routing without decapsulation.
16 * Carlos Picoto : GRE over IP support
17 * Alexey Kuznetsov: Reworked. Really, now it is truncated version of ipv4/ip_gre.c.
18 * I do not want to merge them together.
19 *
20 * This program is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU General Public License
22 * as published by the Free Software Foundation; either version
23 * 2 of the License, or (at your option) any later version.
24 *
25 */
26
27/* tunnel.c: an IP tunnel driver
28
29 The purpose of this driver is to provide an IP tunnel through
30 which you can tunnel network traffic transparently across subnets.
31
32 This was written by looking at Nick Holloway's dummy driver
33 Thanks for the great code!
34
35 -Sam Lantinga (slouken@cs.ucdavis.edu) 02/01/95
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 Minor tweaks:
38 Cleaned up the code a little and added some pre-1.3.0 tweaks.
39 dev->hard_header/hard_header_len changed to use no headers.
40 Comments/bracketing tweaked.
41 Made the tunnels use dev->name not tunnel: when error reporting.
42 Added tx_dropped stat
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090043
Alan Cox113aa832008-10-13 19:01:08 -070044 -Alan Cox (alan@lxorguk.ukuu.org.uk) 21 March 95
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46 Reworked:
47 Changed to tunnel to destination gateway in addition to the
48 tunnel's pointopoint address
49 Almost completely rewritten
50 Note: There is currently no firewall or ICMP handling done.
51
52 -Sam Lantinga (slouken@cs.ucdavis.edu) 02/13/96
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054*/
55
56/* Things I wish I had known when writing the tunnel driver:
57
58 When the tunnel_xmit() function is called, the skb contains the
59 packet to be sent (plus a great deal of extra info), and dev
60 contains the tunnel device that _we_ are.
61
62 When we are passed a packet, we are expected to fill in the
63 source address with our source IP address.
64
65 What is the proper way to allocate, copy and free a buffer?
66 After you allocate it, it is a "0 length" chunk of memory
67 starting at zero. If you want to add headers to the buffer
68 later, you'll have to call "skb_reserve(skb, amount)" with
69 the amount of memory you want reserved. Then, you call
70 "skb_put(skb, amount)" with the amount of space you want in
71 the buffer. skb_put() returns a pointer to the top (#0) of
72 that buffer. skb->len is set to the amount of space you have
73 "allocated" with skb_put(). You can then write up to skb->len
74 bytes to that buffer. If you need more, you can call skb_put()
75 again with the additional amount of space you need. You can
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090076 find out how much more space you can allocate by calling
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 "skb_tailroom(skb)".
78 Now, to add header space, call "skb_push(skb, header_len)".
79 This creates space at the beginning of the buffer and returns
80 a pointer to this new space. If later you need to strip a
81 header from a buffer, call "skb_pull(skb, header_len)".
82 skb_headroom() will return how much space is left at the top
83 of the buffer (before the main data). Remember, this headroom
84 space must be reserved before the skb_put() function is called.
85 */
86
87/*
88 This version of net/ipv4/ipip.c is cloned of net/ipv4/ip_gre.c
89
90 For comments look at net/ipv4/ip_gre.c --ANK
91 */
92
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090093
Randy Dunlap4fc268d2006-01-11 12:17:47 -080094#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#include <linux/module.h>
96#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090098#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#include <asm/uaccess.h>
100#include <linux/skbuff.h>
101#include <linux/netdevice.h>
102#include <linux/in.h>
103#include <linux/tcp.h>
104#include <linux/udp.h>
105#include <linux/if_arp.h>
106#include <linux/mroute.h>
107#include <linux/init.h>
108#include <linux/netfilter_ipv4.h>
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800109#include <linux/if_ether.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111#include <net/sock.h>
112#include <net/ip.h>
113#include <net/icmp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#include <net/ipip.h>
115#include <net/inet_ecn.h>
116#include <net/xfrm.h>
Pavel Emelyanov10dc4c72008-04-16 01:03:13 -0700117#include <net/net_namespace.h>
118#include <net/netns/generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120#define HASH_SIZE 16
Al Virod5a0a1e2006-11-08 00:23:14 -0800121#define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&0xF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
stephen hemmingereccc1bb2012-09-25 11:02:48 +0000123static bool log_ecn_error = true;
124module_param(log_ecn_error, bool, 0644);
125MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
126
Eric Dumazetf99189b2009-11-17 10:42:49 +0000127static int ipip_net_id __read_mostly;
Pavel Emelyanov10dc4c72008-04-16 01:03:13 -0700128struct ipip_net {
Eric Dumazetb7285b72010-09-15 11:07:24 +0000129 struct ip_tunnel __rcu *tunnels_r_l[HASH_SIZE];
130 struct ip_tunnel __rcu *tunnels_r[HASH_SIZE];
131 struct ip_tunnel __rcu *tunnels_l[HASH_SIZE];
132 struct ip_tunnel __rcu *tunnels_wc[1];
133 struct ip_tunnel __rcu **tunnels[4];
Pavel Emelyanov44d3c292008-04-16 01:05:32 -0700134
Pavel Emelyanovb9855c52008-04-16 01:04:13 -0700135 struct net_device *fb_tunnel_dev;
Pavel Emelyanov10dc4c72008-04-16 01:03:13 -0700136};
137
Eric Dumazet3c97af92010-09-27 00:35:50 +0000138static int ipip_tunnel_init(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139static void ipip_tunnel_setup(struct net_device *dev);
Eric Dumazet3c97af92010-09-27 00:35:50 +0000140static void ipip_dev_free(struct net_device *dev);
Nicolas Dichtel09746582012-11-09 06:09:59 +0000141static struct rtnl_link_ops ipip_link_ops __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Eric Dumazet8f95dd62009-10-23 05:42:02 +0000143/*
Eric Dumazetb7285b72010-09-15 11:07:24 +0000144 * Locking : hash tables are protected by RCU and RTNL
Eric Dumazet8f95dd62009-10-23 05:42:02 +0000145 */
Eric Dumazet8f95dd62009-10-23 05:42:02 +0000146
147#define for_each_ip_tunnel_rcu(start) \
148 for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Eric Dumazet3c97af92010-09-27 00:35:50 +0000150/* often modified stats are per cpu, other are shared (netdev->stats) */
151struct pcpu_tstats {
stephen hemminger87b6d212012-04-12 06:31:16 +0000152 u64 rx_packets;
153 u64 rx_bytes;
154 u64 tx_packets;
155 u64 tx_bytes;
156 struct u64_stats_sync syncp;
157};
Eric Dumazet3c97af92010-09-27 00:35:50 +0000158
stephen hemminger87b6d212012-04-12 06:31:16 +0000159static struct rtnl_link_stats64 *ipip_get_stats64(struct net_device *dev,
160 struct rtnl_link_stats64 *tot)
Eric Dumazet3c97af92010-09-27 00:35:50 +0000161{
Eric Dumazet3c97af92010-09-27 00:35:50 +0000162 int i;
163
164 for_each_possible_cpu(i) {
165 const struct pcpu_tstats *tstats = per_cpu_ptr(dev->tstats, i);
stephen hemminger87b6d212012-04-12 06:31:16 +0000166 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
167 unsigned int start;
Eric Dumazet3c97af92010-09-27 00:35:50 +0000168
stephen hemminger87b6d212012-04-12 06:31:16 +0000169 do {
170 start = u64_stats_fetch_begin_bh(&tstats->syncp);
171 rx_packets = tstats->rx_packets;
172 tx_packets = tstats->tx_packets;
173 rx_bytes = tstats->rx_bytes;
174 tx_bytes = tstats->tx_bytes;
175 } while (u64_stats_fetch_retry_bh(&tstats->syncp, start));
176
177 tot->rx_packets += rx_packets;
178 tot->tx_packets += tx_packets;
179 tot->rx_bytes += rx_bytes;
180 tot->tx_bytes += tx_bytes;
Eric Dumazet3c97af92010-09-27 00:35:50 +0000181 }
stephen hemminger87b6d212012-04-12 06:31:16 +0000182
183 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
184 tot->tx_carrier_errors = dev->stats.tx_carrier_errors;
185 tot->tx_dropped = dev->stats.tx_dropped;
186 tot->tx_aborted_errors = dev->stats.tx_aborted_errors;
187 tot->tx_errors = dev->stats.tx_errors;
188 tot->collisions = dev->stats.collisions;
189
190 return tot;
Eric Dumazet3c97af92010-09-27 00:35:50 +0000191}
192
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000193static struct ip_tunnel *ipip_tunnel_lookup(struct net *net,
Pavel Emelyanovb9fae5c2008-04-16 01:04:35 -0700194 __be32 remote, __be32 local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Eric Dumazetb7285b72010-09-15 11:07:24 +0000196 unsigned int h0 = HASH(remote);
197 unsigned int h1 = HASH(local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 struct ip_tunnel *t;
Pavel Emelyanov44d3c292008-04-16 01:05:32 -0700199 struct ipip_net *ipn = net_generic(net, ipip_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Eric Dumazet8f95dd62009-10-23 05:42:02 +0000201 for_each_ip_tunnel_rcu(ipn->tunnels_r_l[h0 ^ h1])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (local == t->parms.iph.saddr &&
203 remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP))
204 return t;
Eric Dumazet8f95dd62009-10-23 05:42:02 +0000205
206 for_each_ip_tunnel_rcu(ipn->tunnels_r[h0])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP))
208 return t;
Eric Dumazet8f95dd62009-10-23 05:42:02 +0000209
210 for_each_ip_tunnel_rcu(ipn->tunnels_l[h1])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (local == t->parms.iph.saddr && (t->dev->flags&IFF_UP))
212 return t;
Eric Dumazet8f95dd62009-10-23 05:42:02 +0000213
214 t = rcu_dereference(ipn->tunnels_wc[0]);
215 if (t && (t->dev->flags&IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return t;
217 return NULL;
218}
219
Eric Dumazetb7285b72010-09-15 11:07:24 +0000220static struct ip_tunnel __rcu **__ipip_bucket(struct ipip_net *ipn,
Pavel Emelyanovb9fae5c2008-04-16 01:04:35 -0700221 struct ip_tunnel_parm *parms)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
YOSHIFUJI Hideaki87d1a162007-04-24 20:44:47 +0900223 __be32 remote = parms->iph.daddr;
224 __be32 local = parms->iph.saddr;
Eric Dumazetb7285b72010-09-15 11:07:24 +0000225 unsigned int h = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 int prio = 0;
227
228 if (remote) {
229 prio |= 2;
230 h ^= HASH(remote);
231 }
232 if (local) {
233 prio |= 1;
234 h ^= HASH(local);
235 }
Pavel Emelyanov44d3c292008-04-16 01:05:32 -0700236 return &ipn->tunnels[prio][h];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
Eric Dumazetb7285b72010-09-15 11:07:24 +0000239static inline struct ip_tunnel __rcu **ipip_bucket(struct ipip_net *ipn,
Pavel Emelyanovb9fae5c2008-04-16 01:04:35 -0700240 struct ip_tunnel *t)
YOSHIFUJI Hideaki87d1a162007-04-24 20:44:47 +0900241{
Pavel Emelyanovb9fae5c2008-04-16 01:04:35 -0700242 return __ipip_bucket(ipn, &t->parms);
YOSHIFUJI Hideaki87d1a162007-04-24 20:44:47 +0900243}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Pavel Emelyanovb9fae5c2008-04-16 01:04:35 -0700245static void ipip_tunnel_unlink(struct ipip_net *ipn, struct ip_tunnel *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
Eric Dumazetb7285b72010-09-15 11:07:24 +0000247 struct ip_tunnel __rcu **tp;
248 struct ip_tunnel *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Eric Dumazetb7285b72010-09-15 11:07:24 +0000250 for (tp = ipip_bucket(ipn, t);
251 (iter = rtnl_dereference(*tp)) != NULL;
252 tp = &iter->next) {
253 if (t == iter) {
Eric Dumazetcf778b02012-01-12 04:41:32 +0000254 rcu_assign_pointer(*tp, t->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 break;
256 }
257 }
258}
259
Pavel Emelyanovb9fae5c2008-04-16 01:04:35 -0700260static void ipip_tunnel_link(struct ipip_net *ipn, struct ip_tunnel *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Eric Dumazetb7285b72010-09-15 11:07:24 +0000262 struct ip_tunnel __rcu **tp = ipip_bucket(ipn, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Eric Dumazetcf778b02012-01-12 04:41:32 +0000264 rcu_assign_pointer(t->next, rtnl_dereference(*tp));
265 rcu_assign_pointer(*tp, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000268static struct ip_tunnel *ipip_tunnel_locate(struct net *net,
Pavel Emelyanovb9fae5c2008-04-16 01:04:35 -0700269 struct ip_tunnel_parm *parms, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Al Virod5a0a1e2006-11-08 00:23:14 -0800271 __be32 remote = parms->iph.daddr;
272 __be32 local = parms->iph.saddr;
Eric Dumazetb7285b72010-09-15 11:07:24 +0000273 struct ip_tunnel *t, *nt;
274 struct ip_tunnel __rcu **tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 char name[IFNAMSIZ];
Pavel Emelyanovb9fae5c2008-04-16 01:04:35 -0700277 struct ipip_net *ipn = net_generic(net, ipip_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Eric Dumazetb7285b72010-09-15 11:07:24 +0000279 for (tp = __ipip_bucket(ipn, parms);
280 (t = rtnl_dereference(*tp)) != NULL;
281 tp = &t->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (local == t->parms.iph.saddr && remote == t->parms.iph.daddr)
283 return t;
284 }
285 if (!create)
286 return NULL;
287
288 if (parms->name[0])
289 strlcpy(name, parms->name, IFNAMSIZ);
Pavel Emelyanov34cc7ba2008-02-23 20:19:20 -0800290 else
Eric Dumazet3c97af92010-09-27 00:35:50 +0000291 strcpy(name, "tunl%d");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 dev = alloc_netdev(sizeof(*t), name, ipip_tunnel_setup);
294 if (dev == NULL)
295 return NULL;
296
Pavel Emelyanov0a826402008-04-16 01:06:18 -0700297 dev_net_set(dev, net);
298
Patrick McHardy2941a482006-01-08 22:05:26 -0800299 nt = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 nt->parms = *parms;
301
Eric Dumazet3c97af92010-09-27 00:35:50 +0000302 if (ipip_tunnel_init(dev) < 0)
303 goto failed_free;
Stephen Hemminger23a12b12008-11-20 20:33:21 -0800304
Pavel Emelyanovb37d428b2008-02-26 23:51:04 -0800305 if (register_netdevice(dev) < 0)
306 goto failed_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Ted Feng72b36012011-12-08 00:46:21 +0000308 strcpy(nt->parms.name, dev->name);
Nicolas Dichtel09746582012-11-09 06:09:59 +0000309 dev->rtnl_link_ops = &ipip_link_ops;
Ted Feng72b36012011-12-08 00:46:21 +0000310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 dev_hold(dev);
Pavel Emelyanovb9fae5c2008-04-16 01:04:35 -0700312 ipip_tunnel_link(ipn, nt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 return nt;
314
Pavel Emelyanovb37d428b2008-02-26 23:51:04 -0800315failed_free:
Eric Dumazet3c97af92010-09-27 00:35:50 +0000316 ipip_dev_free(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return NULL;
318}
319
Eric Dumazetb7285b72010-09-15 11:07:24 +0000320/* called with RTNL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321static void ipip_tunnel_uninit(struct net_device *dev)
322{
Pavel Emelyanovb9855c52008-04-16 01:04:13 -0700323 struct net *net = dev_net(dev);
324 struct ipip_net *ipn = net_generic(net, ipip_net_id);
325
Eric Dumazetb7285b72010-09-15 11:07:24 +0000326 if (dev == ipn->fb_tunnel_dev)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000327 RCU_INIT_POINTER(ipn->tunnels_wc[0], NULL);
Eric Dumazetb7285b72010-09-15 11:07:24 +0000328 else
Pavel Emelyanovb9fae5c2008-04-16 01:04:35 -0700329 ipip_tunnel_unlink(ipn, netdev_priv(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 dev_put(dev);
331}
332
Herbert Xud2acc342006-03-28 01:12:13 -0800333static int ipip_err(struct sk_buff *skb, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Rami Rosen071f92d2008-05-21 17:47:54 -0700336/* All the routers (except for Linux) return only
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 8 bytes of packet payload. It means, that precise relaying of
338 ICMP in the real Internet is absolutely infeasible.
339 */
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000340 const struct iphdr *iph = (const struct iphdr *)skb->data;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300341 const int type = icmp_hdr(skb)->type;
342 const int code = icmp_hdr(skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 struct ip_tunnel *t;
Herbert Xud2acc342006-03-28 01:12:13 -0800344 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 switch (type) {
347 default:
348 case ICMP_PARAMETERPROB:
Herbert Xud2acc342006-03-28 01:12:13 -0800349 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 case ICMP_DEST_UNREACH:
352 switch (code) {
353 case ICMP_SR_FAILED:
354 case ICMP_PORT_UNREACH:
355 /* Impossible event. */
Herbert Xud2acc342006-03-28 01:12:13 -0800356 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 default:
358 /* All others are translated to HOST_UNREACH.
359 rfc2003 contains "deep thoughts" about NET_UNREACH,
360 I believe they are just ether pollution. --ANK
361 */
362 break;
363 }
364 break;
365 case ICMP_TIME_EXCEEDED:
366 if (code != ICMP_EXC_TTL)
Herbert Xud2acc342006-03-28 01:12:13 -0800367 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 break;
David S. Miller55be7a92012-07-11 21:27:49 -0700369 case ICMP_REDIRECT:
370 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372
Herbert Xud2acc342006-03-28 01:12:13 -0800373 err = -ENOENT;
Pavel Emelyanovcec3ffa2008-04-16 01:05:03 -0700374 t = ipip_tunnel_lookup(dev_net(skb->dev), iph->daddr, iph->saddr);
David S. Miller36393392012-06-14 22:21:46 -0700375 if (t == NULL)
376 goto out;
377
378 if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
379 ipv4_update_pmtu(skb, dev_net(skb->dev), info,
380 t->dev->ifindex, 0, IPPROTO_IPIP, 0);
381 err = 0;
382 goto out;
383 }
384
David S. Miller55be7a92012-07-11 21:27:49 -0700385 if (type == ICMP_REDIRECT) {
386 ipv4_redirect(skb, dev_net(skb->dev), t->dev->ifindex, 0,
387 IPPROTO_IPIP, 0);
388 err = 0;
389 goto out;
390 }
391
David S. Miller36393392012-06-14 22:21:46 -0700392 if (t->parms.iph.daddr == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 goto out;
Herbert Xud2acc342006-03-28 01:12:13 -0800394
395 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
397 goto out;
398
Wei Yongjun26d94b42009-02-24 23:36:47 -0800399 if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 t->err_count++;
401 else
402 t->err_count = 1;
403 t->err_time = jiffies;
404out:
stephen hemmingerb0558ef2012-09-24 18:12:25 +0000405
Herbert Xud2acc342006-03-28 01:12:13 -0800406 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409static int ipip_rcv(struct sk_buff *skb)
410{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 struct ip_tunnel *tunnel;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700412 const struct iphdr *iph = ip_hdr(skb);
stephen hemmingereccc1bb2012-09-25 11:02:48 +0000413 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Eric Dumazet3c97af92010-09-27 00:35:50 +0000415 tunnel = ipip_tunnel_lookup(dev_net(skb->dev), iph->saddr, iph->daddr);
416 if (tunnel != NULL) {
417 struct pcpu_tstats *tstats;
418
stephen hemmingereccc1bb2012-09-25 11:02:48 +0000419 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
420 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 secpath_reset(skb);
423
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700424 skb->mac_header = skb->network_header;
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700425 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 skb->protocol = htons(ETH_P_IP);
427 skb->pkt_type = PACKET_HOST;
428
stephen hemmingereccc1bb2012-09-25 11:02:48 +0000429 __skb_tunnel_rx(skb, tunnel->dev);
430
431 err = IP_ECN_decapsulate(iph, skb);
432 if (unlikely(err)) {
433 if (log_ecn_error)
434 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
435 &iph->saddr, iph->tos);
436 if (err > 1) {
437 ++tunnel->dev->stats.rx_frame_errors;
438 ++tunnel->dev->stats.rx_errors;
439 goto drop;
440 }
441 }
442
Eric Dumazet3c97af92010-09-27 00:35:50 +0000443 tstats = this_cpu_ptr(tunnel->dev->tstats);
stephen hemminger87b6d212012-04-12 06:31:16 +0000444 u64_stats_update_begin(&tstats->syncp);
Eric Dumazet3c97af92010-09-27 00:35:50 +0000445 tstats->rx_packets++;
446 tstats->rx_bytes += skb->len;
stephen hemminger87b6d212012-04-12 06:31:16 +0000447 u64_stats_update_end(&tstats->syncp);
Eric Dumazet3c97af92010-09-27 00:35:50 +0000448
Eric Dumazetcaf586e2010-09-30 21:06:55 +0000449 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return 0;
451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return -1;
stephen hemmingereccc1bb2012-09-25 11:02:48 +0000454
455drop:
456 kfree_skb(skb);
457 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
460/*
461 * This function assumes it is being called from dev_queue_xmit()
462 * and that skb is filled properly by that function.
463 */
464
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000465static netdev_tx_t ipip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Patrick McHardy2941a482006-01-08 22:05:26 -0800467 struct ip_tunnel *tunnel = netdev_priv(dev);
Eric Dumazet3c97af92010-09-27 00:35:50 +0000468 struct pcpu_tstats *tstats;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000469 const struct iphdr *tiph = &tunnel->parms.iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 u8 tos = tunnel->parms.iph.tos;
Al Virod5a0a1e2006-11-08 00:23:14 -0800471 __be16 df = tiph->frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 struct rtable *rt; /* Route to the other host */
Eric Dumazet3c97af92010-09-27 00:35:50 +0000473 struct net_device *tdev; /* Device to other host */
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000474 const struct iphdr *old_iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 struct iphdr *iph; /* Our new IP header */
Chuck Leverc2636b42007-10-23 21:07:32 -0700476 unsigned int max_headroom; /* The extra header space needed */
Al Virod5a0a1e2006-11-08 00:23:14 -0800477 __be32 dst = tiph->daddr;
David S. Miller31e45432011-05-03 20:25:42 -0700478 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 int mtu;
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (skb->protocol != htons(ETH_P_IP))
482 goto tx_error;
483
Eric Dumazetc3b89fb2012-11-08 09:59:52 +0000484 if (skb->ip_summed == CHECKSUM_PARTIAL &&
485 skb_checksum_help(skb))
486 goto tx_error;
487
Eric Dumazet3c97af92010-09-27 00:35:50 +0000488 if (tos & 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 tos = old_iph->tos;
490
491 if (!dst) {
492 /* NBMA tunnel */
Eric Dumazet511c3f92009-06-02 05:14:27 +0000493 if ((rt = skb_rtable(skb)) == NULL) {
Eric Dumazet3c97af92010-09-27 00:35:50 +0000494 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 goto tx_error;
496 }
David S. Millerf8126f12012-07-13 05:03:45 -0700497 dst = rt_nexthop(rt, old_iph->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
499
David S. Miller31e45432011-05-03 20:25:42 -0700500 rt = ip_route_output_ports(dev_net(dev), &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -0500501 dst, tiph->saddr,
502 0, 0,
503 IPPROTO_IPIP, RT_TOS(tos),
504 tunnel->parms.link);
505 if (IS_ERR(rt)) {
506 dev->stats.tx_carrier_errors++;
507 goto tx_error_icmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700509 tdev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 if (tdev == dev) {
512 ip_rt_put(rt);
Eric Dumazet3c97af92010-09-27 00:35:50 +0000513 dev->stats.collisions++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 goto tx_error;
515 }
516
Herbert Xu23ca0c92009-11-06 10:37:41 +0000517 df |= old_iph->frag_off & htons(IP_DF);
518
519 if (df) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700520 mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Herbert Xu23ca0c92009-11-06 10:37:41 +0000522 if (mtu < 68) {
Eric Dumazet3c97af92010-09-27 00:35:50 +0000523 dev->stats.collisions++;
Herbert Xu23ca0c92009-11-06 10:37:41 +0000524 ip_rt_put(rt);
525 goto tx_error;
526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Herbert Xu23ca0c92009-11-06 10:37:41 +0000528 if (skb_dst(skb))
David S. Miller6700c272012-07-17 03:29:28 -0700529 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Herbert Xu23ca0c92009-11-06 10:37:41 +0000531 if ((old_iph->frag_off & htons(IP_DF)) &&
532 mtu < ntohs(old_iph->tot_len)) {
533 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
534 htonl(mtu));
535 ip_rt_put(rt);
536 goto tx_error;
537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
539
540 if (tunnel->err_count > 0) {
Wei Yongjun26d94b42009-02-24 23:36:47 -0800541 if (time_before(jiffies,
542 tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 tunnel->err_count--;
544 dst_link_failure(skb);
545 } else
546 tunnel->err_count = 0;
547 }
548
549 /*
550 * Okay, now see if we can stuff it in the buffer as-is.
551 */
552 max_headroom = (LL_RESERVED_SPACE(tdev)+sizeof(struct iphdr));
553
Patrick McHardycfbba492007-07-09 15:33:40 -0700554 if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
555 (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
557 if (!new_skb) {
558 ip_rt_put(rt);
Eric Dumazet3c97af92010-09-27 00:35:50 +0000559 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 dev_kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000561 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563 if (skb->sk)
564 skb_set_owner_w(new_skb, skb->sk);
565 dev_kfree_skb(skb);
566 skb = new_skb;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700567 old_iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
569
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700570 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -0700571 skb_push(skb, sizeof(struct iphdr));
572 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800574 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
575 IPSKB_REROUTED);
Eric Dumazetadf30902009-06-02 05:19:30 +0000576 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -0700577 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 /*
580 * Push down and install the IPIP header.
581 */
582
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700583 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 iph->version = 4;
585 iph->ihl = sizeof(struct iphdr)>>2;
586 iph->frag_off = df;
587 iph->protocol = IPPROTO_IPIP;
588 iph->tos = INET_ECN_encapsulate(tos, old_iph->tos);
David S. Miller69458cb2011-05-04 11:10:28 -0700589 iph->daddr = fl4.daddr;
590 iph->saddr = fl4.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 if ((iph->ttl = tiph->ttl) == 0)
593 iph->ttl = old_iph->ttl;
594
595 nf_reset(skb);
Eric Dumazet3c97af92010-09-27 00:35:50 +0000596 tstats = this_cpu_ptr(dev->tstats);
597 __IPTUNNEL_XMIT(tstats, &dev->stats);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000598 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600tx_error_icmp:
601 dst_link_failure(skb);
602tx_error:
Eric Dumazet3c97af92010-09-27 00:35:50 +0000603 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 dev_kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000605 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
Michal Schmidt55339952007-12-12 11:01:43 -0800608static void ipip_tunnel_bind_dev(struct net_device *dev)
609{
610 struct net_device *tdev = NULL;
611 struct ip_tunnel *tunnel;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000612 const struct iphdr *iph;
Michal Schmidt55339952007-12-12 11:01:43 -0800613
614 tunnel = netdev_priv(dev);
615 iph = &tunnel->parms.iph;
616
617 if (iph->daddr) {
David S. Miller31e45432011-05-03 20:25:42 -0700618 struct rtable *rt;
619 struct flowi4 fl4;
Eric Dumazet3c97af92010-09-27 00:35:50 +0000620
David S. Miller31e45432011-05-03 20:25:42 -0700621 rt = ip_route_output_ports(dev_net(dev), &fl4, NULL,
622 iph->daddr, iph->saddr,
623 0, 0,
624 IPPROTO_IPIP,
625 RT_TOS(iph->tos),
626 tunnel->parms.link);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800627 if (!IS_ERR(rt)) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700628 tdev = rt->dst.dev;
Michal Schmidt55339952007-12-12 11:01:43 -0800629 ip_rt_put(rt);
630 }
631 dev->flags |= IFF_POINTOPOINT;
632 }
633
634 if (!tdev && tunnel->parms.link)
Pavel Emelyanovb99f0152008-04-16 01:05:57 -0700635 tdev = __dev_get_by_index(dev_net(dev), tunnel->parms.link);
Michal Schmidt55339952007-12-12 11:01:43 -0800636
637 if (tdev) {
638 dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
639 dev->mtu = tdev->mtu - sizeof(struct iphdr);
640 }
641 dev->iflink = tunnel->parms.link;
642}
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644static int
645ipip_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
646{
647 int err = 0;
648 struct ip_tunnel_parm p;
649 struct ip_tunnel *t;
Pavel Emelyanovb9855c52008-04-16 01:04:13 -0700650 struct net *net = dev_net(dev);
651 struct ipip_net *ipn = net_generic(net, ipip_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 switch (cmd) {
654 case SIOCGETTUNNEL:
655 t = NULL;
Pavel Emelyanovb9855c52008-04-16 01:04:13 -0700656 if (dev == ipn->fb_tunnel_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
658 err = -EFAULT;
659 break;
660 }
Pavel Emelyanovb9fae5c2008-04-16 01:04:35 -0700661 t = ipip_tunnel_locate(net, &p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
663 if (t == NULL)
Patrick McHardy2941a482006-01-08 22:05:26 -0800664 t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 memcpy(&p, &t->parms, sizeof(p));
666 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
667 err = -EFAULT;
668 break;
669
670 case SIOCADDTUNNEL:
671 case SIOCCHGTUNNEL:
672 err = -EPERM;
673 if (!capable(CAP_NET_ADMIN))
674 goto done;
675
676 err = -EFAULT;
677 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
678 goto done;
679
680 err = -EINVAL;
681 if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPIP ||
682 p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
683 goto done;
684 if (p.iph.ttl)
685 p.iph.frag_off |= htons(IP_DF);
686
Pavel Emelyanovb9fae5c2008-04-16 01:04:35 -0700687 t = ipip_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Pavel Emelyanovb9855c52008-04-16 01:04:13 -0700689 if (dev != ipn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (t != NULL) {
691 if (t->dev != dev) {
692 err = -EEXIST;
693 break;
694 }
695 } else {
696 if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
697 (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
698 err = -EINVAL;
699 break;
700 }
Patrick McHardy2941a482006-01-08 22:05:26 -0800701 t = netdev_priv(dev);
Pavel Emelyanovb9fae5c2008-04-16 01:04:35 -0700702 ipip_tunnel_unlink(ipn, t);
Pavel Emelyanov74b0b852010-10-27 05:43:53 +0000703 synchronize_net();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 t->parms.iph.saddr = p.iph.saddr;
705 t->parms.iph.daddr = p.iph.daddr;
706 memcpy(dev->dev_addr, &p.iph.saddr, 4);
707 memcpy(dev->broadcast, &p.iph.daddr, 4);
Pavel Emelyanovb9fae5c2008-04-16 01:04:35 -0700708 ipip_tunnel_link(ipn, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 netdev_state_change(dev);
710 }
711 }
712
713 if (t) {
714 err = 0;
715 if (cmd == SIOCCHGTUNNEL) {
716 t->parms.iph.ttl = p.iph.ttl;
717 t->parms.iph.tos = p.iph.tos;
718 t->parms.iph.frag_off = p.iph.frag_off;
Michal Schmidt55339952007-12-12 11:01:43 -0800719 if (t->parms.link != p.link) {
720 t->parms.link = p.link;
721 ipip_tunnel_bind_dev(dev);
722 netdev_state_change(dev);
723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
725 if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
726 err = -EFAULT;
727 } else
728 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
729 break;
730
731 case SIOCDELTUNNEL:
732 err = -EPERM;
733 if (!capable(CAP_NET_ADMIN))
734 goto done;
735
Pavel Emelyanovb9855c52008-04-16 01:04:13 -0700736 if (dev == ipn->fb_tunnel_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 err = -EFAULT;
738 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
739 goto done;
740 err = -ENOENT;
Pavel Emelyanovb9fae5c2008-04-16 01:04:35 -0700741 if ((t = ipip_tunnel_locate(net, &p, 0)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 goto done;
743 err = -EPERM;
Pavel Emelyanovb9855c52008-04-16 01:04:13 -0700744 if (t->dev == ipn->fb_tunnel_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 goto done;
746 dev = t->dev;
747 }
Stephen Hemminger22f8cde2007-02-07 00:09:58 -0800748 unregister_netdevice(dev);
749 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 break;
751
752 default:
753 err = -EINVAL;
754 }
755
756done:
757 return err;
758}
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760static int ipip_tunnel_change_mtu(struct net_device *dev, int new_mtu)
761{
762 if (new_mtu < 68 || new_mtu > 0xFFF8 - sizeof(struct iphdr))
763 return -EINVAL;
764 dev->mtu = new_mtu;
765 return 0;
766}
767
Stephen Hemminger23a12b12008-11-20 20:33:21 -0800768static const struct net_device_ops ipip_netdev_ops = {
769 .ndo_uninit = ipip_tunnel_uninit,
770 .ndo_start_xmit = ipip_tunnel_xmit,
771 .ndo_do_ioctl = ipip_tunnel_ioctl,
772 .ndo_change_mtu = ipip_tunnel_change_mtu,
stephen hemminger87b6d212012-04-12 06:31:16 +0000773 .ndo_get_stats64 = ipip_get_stats64,
Stephen Hemminger23a12b12008-11-20 20:33:21 -0800774};
775
Eric Dumazet3c97af92010-09-27 00:35:50 +0000776static void ipip_dev_free(struct net_device *dev)
777{
778 free_percpu(dev->tstats);
779 free_netdev(dev);
780}
781
Eric Dumazetc3b89fb2012-11-08 09:59:52 +0000782#define IPIP_FEATURES (NETIF_F_SG | \
783 NETIF_F_FRAGLIST | \
784 NETIF_F_HIGHDMA | \
785 NETIF_F_HW_CSUM)
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787static void ipip_tunnel_setup(struct net_device *dev)
788{
Stephen Hemminger23a12b12008-11-20 20:33:21 -0800789 dev->netdev_ops = &ipip_netdev_ops;
Eric Dumazet3c97af92010-09-27 00:35:50 +0000790 dev->destructor = ipip_dev_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 dev->type = ARPHRD_TUNNEL;
793 dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800794 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 dev->flags = IFF_NOARP;
796 dev->iflink = 0;
797 dev->addr_len = 4;
Pavel Emelyanov0a826402008-04-16 01:06:18 -0700798 dev->features |= NETIF_F_NETNS_LOCAL;
Eric Dumazet153f0942010-09-28 00:17:17 +0000799 dev->features |= NETIF_F_LLTX;
Eric Dumazet28e72212009-05-28 10:44:30 +0000800 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
Eric Dumazetc3b89fb2012-11-08 09:59:52 +0000801
802 dev->features |= IPIP_FEATURES;
803 dev->hw_features |= IPIP_FEATURES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
805
Eric Dumazet3c97af92010-09-27 00:35:50 +0000806static int ipip_tunnel_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Stephen Hemminger23a12b12008-11-20 20:33:21 -0800808 struct ip_tunnel *tunnel = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 tunnel->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
813 memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
814
Michal Schmidt55339952007-12-12 11:01:43 -0800815 ipip_tunnel_bind_dev(dev);
Eric Dumazet3c97af92010-09-27 00:35:50 +0000816
817 dev->tstats = alloc_percpu(struct pcpu_tstats);
818 if (!dev->tstats)
819 return -ENOMEM;
820
821 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
823
Eric Dumazetfada5632010-09-27 23:56:46 +0000824static int __net_init ipip_fb_tunnel_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Patrick McHardy2941a482006-01-08 22:05:26 -0800826 struct ip_tunnel *tunnel = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 struct iphdr *iph = &tunnel->parms.iph;
Pavel Emelyanov44d3c292008-04-16 01:05:32 -0700828 struct ipip_net *ipn = net_generic(dev_net(dev), ipip_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 tunnel->dev = dev;
831 strcpy(tunnel->parms.name, dev->name);
832
833 iph->version = 4;
834 iph->protocol = IPPROTO_IPIP;
835 iph->ihl = 5;
836
Eric Dumazetfada5632010-09-27 23:56:46 +0000837 dev->tstats = alloc_percpu(struct pcpu_tstats);
838 if (!dev->tstats)
839 return -ENOMEM;
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 dev_hold(dev);
Eric Dumazetcf778b02012-01-12 04:41:32 +0000842 rcu_assign_pointer(ipn->tunnels_wc[0], tunnel);
Eric Dumazetfada5632010-09-27 23:56:46 +0000843 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
Nicolas Dichtel09746582012-11-09 06:09:59 +0000846static size_t ipip_get_size(const struct net_device *dev)
847{
848 return
849 /* IFLA_IPTUN_LINK */
850 nla_total_size(4) +
851 /* IFLA_IPTUN_LOCAL */
852 nla_total_size(4) +
853 /* IFLA_IPTUN_REMOTE */
854 nla_total_size(4) +
855 /* IFLA_IPTUN_TTL */
856 nla_total_size(1) +
857 /* IFLA_IPTUN_TOS */
858 nla_total_size(1) +
859 0;
860}
861
862static int ipip_fill_info(struct sk_buff *skb, const struct net_device *dev)
863{
864 struct ip_tunnel *tunnel = netdev_priv(dev);
865 struct ip_tunnel_parm *parm = &tunnel->parms;
866
867 if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
868 nla_put_be32(skb, IFLA_IPTUN_LOCAL, parm->iph.saddr) ||
869 nla_put_be32(skb, IFLA_IPTUN_REMOTE, parm->iph.daddr) ||
870 nla_put_u8(skb, IFLA_IPTUN_TTL, parm->iph.ttl) ||
871 nla_put_u8(skb, IFLA_IPTUN_TOS, parm->iph.tos))
872 goto nla_put_failure;
873 return 0;
874
875nla_put_failure:
876 return -EMSGSIZE;
877}
878
879static struct rtnl_link_ops ipip_link_ops __read_mostly = {
880 .kind = "ipip",
881 .maxtype = IFLA_IPTUN_MAX,
882 .priv_size = sizeof(struct ip_tunnel),
883 .get_size = ipip_get_size,
884 .fill_info = ipip_fill_info,
885};
886
Eric Dumazet6dcd8142010-08-30 07:04:14 +0000887static struct xfrm_tunnel ipip_handler __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 .handler = ipip_rcv,
889 .err_handler = ipip_err,
Herbert Xud2acc342006-03-28 01:12:13 -0800890 .priority = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891};
892
Stephen Hemminger5747a1a2009-02-22 00:02:08 -0800893static const char banner[] __initconst =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 KERN_INFO "IPv4 over IPv4 tunneling driver\n";
895
Eric Dumazet0694c4c2009-10-27 07:06:59 +0000896static void ipip_destroy_tunnels(struct ipip_net *ipn, struct list_head *head)
Pavel Emelyanov44d3c292008-04-16 01:05:32 -0700897{
898 int prio;
899
900 for (prio = 1; prio < 4; prio++) {
901 int h;
902 for (h = 0; h < HASH_SIZE; h++) {
Eric Dumazetb7285b72010-09-15 11:07:24 +0000903 struct ip_tunnel *t;
Eric Dumazet0694c4c2009-10-27 07:06:59 +0000904
Eric Dumazetb7285b72010-09-15 11:07:24 +0000905 t = rtnl_dereference(ipn->tunnels[prio][h]);
Eric Dumazet0694c4c2009-10-27 07:06:59 +0000906 while (t != NULL) {
907 unregister_netdevice_queue(t->dev, head);
Eric Dumazetb7285b72010-09-15 11:07:24 +0000908 t = rtnl_dereference(t->next);
Eric Dumazet0694c4c2009-10-27 07:06:59 +0000909 }
Pavel Emelyanov44d3c292008-04-16 01:05:32 -0700910 }
911 }
912}
913
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000914static int __net_init ipip_init_net(struct net *net)
Pavel Emelyanov10dc4c72008-04-16 01:03:13 -0700915{
Eric W. Biederman86de8a62009-11-29 15:46:14 +0000916 struct ipip_net *ipn = net_generic(net, ipip_net_id);
Ted Feng72b36012011-12-08 00:46:21 +0000917 struct ip_tunnel *t;
Pavel Emelyanov10dc4c72008-04-16 01:03:13 -0700918 int err;
Pavel Emelyanov10dc4c72008-04-16 01:03:13 -0700919
Pavel Emelyanov44d3c292008-04-16 01:05:32 -0700920 ipn->tunnels[0] = ipn->tunnels_wc;
921 ipn->tunnels[1] = ipn->tunnels_l;
922 ipn->tunnels[2] = ipn->tunnels_r;
923 ipn->tunnels[3] = ipn->tunnels_r_l;
924
Pavel Emelyanovb9855c52008-04-16 01:04:13 -0700925 ipn->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel),
926 "tunl0",
927 ipip_tunnel_setup);
928 if (!ipn->fb_tunnel_dev) {
929 err = -ENOMEM;
930 goto err_alloc_dev;
931 }
Alexey Dobriyanbe77e592008-11-23 17:26:26 -0800932 dev_net_set(ipn->fb_tunnel_dev, net);
Pavel Emelyanovb9855c52008-04-16 01:04:13 -0700933
Eric Dumazetfada5632010-09-27 23:56:46 +0000934 err = ipip_fb_tunnel_init(ipn->fb_tunnel_dev);
935 if (err)
936 goto err_reg_dev;
Pavel Emelyanovb9855c52008-04-16 01:04:13 -0700937
938 if ((err = register_netdev(ipn->fb_tunnel_dev)))
939 goto err_reg_dev;
940
Ted Feng72b36012011-12-08 00:46:21 +0000941 t = netdev_priv(ipn->fb_tunnel_dev);
942
943 strcpy(t->parms.name, ipn->fb_tunnel_dev->name);
Pavel Emelyanov10dc4c72008-04-16 01:03:13 -0700944 return 0;
945
Pavel Emelyanovb9855c52008-04-16 01:04:13 -0700946err_reg_dev:
Eric Dumazetfada5632010-09-27 23:56:46 +0000947 ipip_dev_free(ipn->fb_tunnel_dev);
Pavel Emelyanovb9855c52008-04-16 01:04:13 -0700948err_alloc_dev:
949 /* nothing */
Pavel Emelyanov10dc4c72008-04-16 01:03:13 -0700950 return err;
951}
952
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000953static void __net_exit ipip_exit_net(struct net *net)
Pavel Emelyanov10dc4c72008-04-16 01:03:13 -0700954{
Eric W. Biederman86de8a62009-11-29 15:46:14 +0000955 struct ipip_net *ipn = net_generic(net, ipip_net_id);
Eric Dumazet0694c4c2009-10-27 07:06:59 +0000956 LIST_HEAD(list);
Pavel Emelyanov10dc4c72008-04-16 01:03:13 -0700957
Pavel Emelyanovb9855c52008-04-16 01:04:13 -0700958 rtnl_lock();
Eric Dumazet0694c4c2009-10-27 07:06:59 +0000959 ipip_destroy_tunnels(ipn, &list);
960 unregister_netdevice_queue(ipn->fb_tunnel_dev, &list);
961 unregister_netdevice_many(&list);
Pavel Emelyanovb9855c52008-04-16 01:04:13 -0700962 rtnl_unlock();
Pavel Emelyanov10dc4c72008-04-16 01:03:13 -0700963}
964
965static struct pernet_operations ipip_net_ops = {
966 .init = ipip_init_net,
967 .exit = ipip_exit_net,
Eric W. Biederman86de8a62009-11-29 15:46:14 +0000968 .id = &ipip_net_id,
969 .size = sizeof(struct ipip_net),
Pavel Emelyanov10dc4c72008-04-16 01:03:13 -0700970};
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972static int __init ipip_init(void)
973{
974 int err;
975
976 printk(banner);
977
Eric W. Biederman86de8a62009-11-29 15:46:14 +0000978 err = register_pernet_device(&ipip_net_ops);
Alexey Dobriyand5aa4072010-02-16 09:05:04 +0000979 if (err < 0)
980 return err;
981 err = xfrm4_tunnel_register(&ipip_handler, AF_INET);
982 if (err < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +0000983 pr_info("%s: can't register tunnel\n", __func__);
Nicolas Dichtel09746582012-11-09 06:09:59 +0000984 goto xfrm_tunnel_failed;
Alexey Dobriyand5aa4072010-02-16 09:05:04 +0000985 }
Nicolas Dichtel09746582012-11-09 06:09:59 +0000986 err = rtnl_link_register(&ipip_link_ops);
987 if (err < 0)
988 goto rtnl_link_failed;
989
990out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 return err;
Nicolas Dichtel09746582012-11-09 06:09:59 +0000992
993rtnl_link_failed:
994 xfrm4_tunnel_deregister(&ipip_handler, AF_INET);
995xfrm_tunnel_failed:
996 unregister_pernet_device(&ipip_net_ops);
997 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998}
999
1000static void __exit ipip_fini(void)
1001{
Nicolas Dichtel09746582012-11-09 06:09:59 +00001002 rtnl_link_unregister(&ipip_link_ops);
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -08001003 if (xfrm4_tunnel_deregister(&ipip_handler, AF_INET))
Joe Perches058bd4d2012-03-11 18:36:11 +00001004 pr_info("%s: can't deregister tunnel\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Eric W. Biederman86de8a62009-11-29 15:46:14 +00001006 unregister_pernet_device(&ipip_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
1008
1009module_init(ipip_init);
1010module_exit(ipip_fini);
1011MODULE_LICENSE("GPL");
Vasiliy Kulikov8909c9a2011-03-02 00:33:13 +03001012MODULE_ALIAS_NETDEV("tunl0");