xref: /linux/net/ipv4/ip_gre.c (revision f3756b79e8f76cb92830383c215deba146fe0a26)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	Linux NET3:	GRE over IP protocol decoder.
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *	Authors: Alexey Kuznetsov (kuznet@ms2.inr.ac.ru)
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
71da177e4SLinus Torvalds  *	modify it under the terms of the GNU General Public License
81da177e4SLinus Torvalds  *	as published by the Free Software Foundation; either version
91da177e4SLinus Torvalds  *	2 of the License, or (at your option) any later version.
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  */
121da177e4SLinus Torvalds 
13afd46503SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14afd46503SJoe Perches 
154fc268d2SRandy Dunlap #include <linux/capability.h>
161da177e4SLinus Torvalds #include <linux/module.h>
171da177e4SLinus Torvalds #include <linux/types.h>
181da177e4SLinus Torvalds #include <linux/kernel.h>
195a0e3ad6STejun Heo #include <linux/slab.h>
201da177e4SLinus Torvalds #include <asm/uaccess.h>
211da177e4SLinus Torvalds #include <linux/skbuff.h>
221da177e4SLinus Torvalds #include <linux/netdevice.h>
231da177e4SLinus Torvalds #include <linux/in.h>
241da177e4SLinus Torvalds #include <linux/tcp.h>
251da177e4SLinus Torvalds #include <linux/udp.h>
261da177e4SLinus Torvalds #include <linux/if_arp.h>
271da177e4SLinus Torvalds #include <linux/mroute.h>
281da177e4SLinus Torvalds #include <linux/init.h>
291da177e4SLinus Torvalds #include <linux/in6.h>
301da177e4SLinus Torvalds #include <linux/inetdevice.h>
311da177e4SLinus Torvalds #include <linux/igmp.h>
321da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
33e1a80002SHerbert Xu #include <linux/etherdevice.h>
3446f25dffSKris Katterjohn #include <linux/if_ether.h>
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds #include <net/sock.h>
371da177e4SLinus Torvalds #include <net/ip.h>
381da177e4SLinus Torvalds #include <net/icmp.h>
391da177e4SLinus Torvalds #include <net/protocol.h>
401da177e4SLinus Torvalds #include <net/ipip.h>
411da177e4SLinus Torvalds #include <net/arp.h>
421da177e4SLinus Torvalds #include <net/checksum.h>
431da177e4SLinus Torvalds #include <net/dsfield.h>
441da177e4SLinus Torvalds #include <net/inet_ecn.h>
451da177e4SLinus Torvalds #include <net/xfrm.h>
4659a4c759SPavel Emelyanov #include <net/net_namespace.h>
4759a4c759SPavel Emelyanov #include <net/netns/generic.h>
48c19e654dSHerbert Xu #include <net/rtnetlink.h>
4900959adeSDmitry Kozlov #include <net/gre.h>
501da177e4SLinus Torvalds 
51dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
521da177e4SLinus Torvalds #include <net/ipv6.h>
531da177e4SLinus Torvalds #include <net/ip6_fib.h>
541da177e4SLinus Torvalds #include <net/ip6_route.h>
551da177e4SLinus Torvalds #endif
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds /*
581da177e4SLinus Torvalds    Problems & solutions
591da177e4SLinus Torvalds    --------------------
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds    1. The most important issue is detecting local dead loops.
621da177e4SLinus Torvalds    They would cause complete host lockup in transmit, which
631da177e4SLinus Torvalds    would be "resolved" by stack overflow or, if queueing is enabled,
641da177e4SLinus Torvalds    with infinite looping in net_bh.
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds    We cannot track such dead loops during route installation,
671da177e4SLinus Torvalds    it is infeasible task. The most general solutions would be
681da177e4SLinus Torvalds    to keep skb->encapsulation counter (sort of local ttl),
696d0722a2SEric Dumazet    and silently drop packet when it expires. It is a good
70bff52857Sstephen hemminger    solution, but it supposes maintaining new variable in ALL
711da177e4SLinus Torvalds    skb, even if no tunneling is used.
721da177e4SLinus Torvalds 
736d0722a2SEric Dumazet    Current solution: xmit_recursion breaks dead loops. This is a percpu
746d0722a2SEric Dumazet    counter, since when we enter the first ndo_xmit(), cpu migration is
756d0722a2SEric Dumazet    forbidden. We force an exit if this counter reaches RECURSION_LIMIT
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds    2. Networking dead loops would not kill routers, but would really
781da177e4SLinus Torvalds    kill network. IP hop limit plays role of "t->recursion" in this case,
791da177e4SLinus Torvalds    if we copy it from packet being encapsulated to upper header.
801da177e4SLinus Torvalds    It is very good solution, but it introduces two problems:
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds    - Routing protocols, using packets with ttl=1 (OSPF, RIP2),
831da177e4SLinus Torvalds      do not work over tunnels.
841da177e4SLinus Torvalds    - traceroute does not work. I planned to relay ICMP from tunnel,
851da177e4SLinus Torvalds      so that this problem would be solved and traceroute output
861da177e4SLinus Torvalds      would even more informative. This idea appeared to be wrong:
871da177e4SLinus Torvalds      only Linux complies to rfc1812 now (yes, guys, Linux is the only
881da177e4SLinus Torvalds      true router now :-)), all routers (at least, in neighbourhood of mine)
891da177e4SLinus Torvalds      return only 8 bytes of payload. It is the end.
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds    Hence, if we want that OSPF worked or traceroute said something reasonable,
921da177e4SLinus Torvalds    we should search for another solution.
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds    One of them is to parse packet trying to detect inner encapsulation
951da177e4SLinus Torvalds    made by our node. It is difficult or even impossible, especially,
96bff52857Sstephen hemminger    taking into account fragmentation. TO be short, ttl is not solution at all.
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds    Current solution: The solution was UNEXPECTEDLY SIMPLE.
991da177e4SLinus Torvalds    We force DF flag on tunnels with preconfigured hop limit,
1001da177e4SLinus Torvalds    that is ALL. :-) Well, it does not remove the problem completely,
1011da177e4SLinus Torvalds    but exponential growth of network traffic is changed to linear
1021da177e4SLinus Torvalds    (branches, that exceed pmtu are pruned) and tunnel mtu
103bff52857Sstephen hemminger    rapidly degrades to value <68, where looping stops.
1041da177e4SLinus Torvalds    Yes, it is not good if there exists a router in the loop,
1051da177e4SLinus Torvalds    which does not force DF, even when encapsulating packets have DF set.
1061da177e4SLinus Torvalds    But it is not our problem! Nobody could accuse us, we made
1071da177e4SLinus Torvalds    all that we could make. Even if it is your gated who injected
1081da177e4SLinus Torvalds    fatal route to network, even if it were you who configured
1091da177e4SLinus Torvalds    fatal static route: you are innocent. :-)
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds    3. Really, ipv4/ipip.c, ipv4/ip_gre.c and ipv6/sit.c contain
1141da177e4SLinus Torvalds    practically identical code. It would be good to glue them
1151da177e4SLinus Torvalds    together, but it is not very evident, how to make them modular.
1161da177e4SLinus Torvalds    sit is integral part of IPv6, ipip and gre are naturally modular.
1171da177e4SLinus Torvalds    We could extract common parts (hash table, ioctl etc)
1181da177e4SLinus Torvalds    to a separate module (ip_tunnel.c).
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds    Alexey Kuznetsov.
1211da177e4SLinus Torvalds  */
1221da177e4SLinus Torvalds 
123c19e654dSHerbert Xu static struct rtnl_link_ops ipgre_link_ops __read_mostly;
1241da177e4SLinus Torvalds static int ipgre_tunnel_init(struct net_device *dev);
1251da177e4SLinus Torvalds static void ipgre_tunnel_setup(struct net_device *dev);
12642aa9162SHerbert Xu static int ipgre_tunnel_bind_dev(struct net_device *dev);
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds /* Fallback tunnel: no source, no destination, no key, no options */
1291da177e4SLinus Torvalds 
130eb8ce741SPavel Emelyanov #define HASH_SIZE  16
131eb8ce741SPavel Emelyanov 
132f99189b1SEric Dumazet static int ipgre_net_id __read_mostly;
13359a4c759SPavel Emelyanov struct ipgre_net {
1341507850bSEric Dumazet 	struct ip_tunnel __rcu *tunnels[4][HASH_SIZE];
135eb8ce741SPavel Emelyanov 
1367daa0004SPavel Emelyanov 	struct net_device *fb_tunnel_dev;
13759a4c759SPavel Emelyanov };
13859a4c759SPavel Emelyanov 
1391da177e4SLinus Torvalds /* Tunnel hash table */
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds /*
1421da177e4SLinus Torvalds    4 hash tables:
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds    3: (remote,local)
1451da177e4SLinus Torvalds    2: (remote,*)
1461da177e4SLinus Torvalds    1: (*,local)
1471da177e4SLinus Torvalds    0: (*,*)
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds    We require exact key match i.e. if a key is present in packet
1501da177e4SLinus Torvalds    it will match only tunnel with the same key; if it is not present,
1511da177e4SLinus Torvalds    it will match only keyless tunnel.
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds    All keysless packets, if not matched configured keyless tunnels
1541da177e4SLinus Torvalds    will match fallback tunnel.
1551da177e4SLinus Torvalds  */
1561da177e4SLinus Torvalds 
157d5a0a1e3SAl Viro #define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&0xF)
1581da177e4SLinus Torvalds 
159eb8ce741SPavel Emelyanov #define tunnels_r_l	tunnels[3]
160eb8ce741SPavel Emelyanov #define tunnels_r	tunnels[2]
161eb8ce741SPavel Emelyanov #define tunnels_l	tunnels[1]
162eb8ce741SPavel Emelyanov #define tunnels_wc	tunnels[0]
1638d5b2c08SEric Dumazet /*
1641507850bSEric Dumazet  * Locking : hash tables are protected by RCU and RTNL
1658d5b2c08SEric Dumazet  */
1661da177e4SLinus Torvalds 
1678d5b2c08SEric Dumazet #define for_each_ip_tunnel_rcu(start) \
1688d5b2c08SEric Dumazet 	for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
1691da177e4SLinus Torvalds 
170e985aad7SEric Dumazet /* often modified stats are per cpu, other are shared (netdev->stats) */
171e985aad7SEric Dumazet struct pcpu_tstats {
172e985aad7SEric Dumazet 	unsigned long	rx_packets;
173e985aad7SEric Dumazet 	unsigned long	rx_bytes;
174e985aad7SEric Dumazet 	unsigned long	tx_packets;
175e985aad7SEric Dumazet 	unsigned long	tx_bytes;
1768ce120f1SEric Dumazet } __attribute__((aligned(4*sizeof(unsigned long))));
177e985aad7SEric Dumazet 
178e985aad7SEric Dumazet static struct net_device_stats *ipgre_get_stats(struct net_device *dev)
179e985aad7SEric Dumazet {
180e985aad7SEric Dumazet 	struct pcpu_tstats sum = { 0 };
181e985aad7SEric Dumazet 	int i;
182e985aad7SEric Dumazet 
183e985aad7SEric Dumazet 	for_each_possible_cpu(i) {
184e985aad7SEric Dumazet 		const struct pcpu_tstats *tstats = per_cpu_ptr(dev->tstats, i);
185e985aad7SEric Dumazet 
186e985aad7SEric Dumazet 		sum.rx_packets += tstats->rx_packets;
187e985aad7SEric Dumazet 		sum.rx_bytes   += tstats->rx_bytes;
188e985aad7SEric Dumazet 		sum.tx_packets += tstats->tx_packets;
189e985aad7SEric Dumazet 		sum.tx_bytes   += tstats->tx_bytes;
190e985aad7SEric Dumazet 	}
191e985aad7SEric Dumazet 	dev->stats.rx_packets = sum.rx_packets;
192e985aad7SEric Dumazet 	dev->stats.rx_bytes   = sum.rx_bytes;
193e985aad7SEric Dumazet 	dev->stats.tx_packets = sum.tx_packets;
194e985aad7SEric Dumazet 	dev->stats.tx_bytes   = sum.tx_bytes;
195e985aad7SEric Dumazet 	return &dev->stats;
196e985aad7SEric Dumazet }
197e985aad7SEric Dumazet 
1981da177e4SLinus Torvalds /* Given src, dst and key, find appropriate for input tunnel. */
1991da177e4SLinus Torvalds 
200749c10f9STimo Teras static struct ip_tunnel * ipgre_tunnel_lookup(struct net_device *dev,
201e1a80002SHerbert Xu 					      __be32 remote, __be32 local,
202e1a80002SHerbert Xu 					      __be32 key, __be16 gre_proto)
2031da177e4SLinus Torvalds {
204749c10f9STimo Teras 	struct net *net = dev_net(dev);
205749c10f9STimo Teras 	int link = dev->ifindex;
2061507850bSEric Dumazet 	unsigned int h0 = HASH(remote);
2071507850bSEric Dumazet 	unsigned int h1 = HASH(key);
208afcf1242STimo Teras 	struct ip_tunnel *t, *cand = NULL;
2097daa0004SPavel Emelyanov 	struct ipgre_net *ign = net_generic(net, ipgre_net_id);
210e1a80002SHerbert Xu 	int dev_type = (gre_proto == htons(ETH_P_TEB)) ?
211e1a80002SHerbert Xu 		       ARPHRD_ETHER : ARPHRD_IPGRE;
212afcf1242STimo Teras 	int score, cand_score = 4;
2131da177e4SLinus Torvalds 
2148d5b2c08SEric Dumazet 	for_each_ip_tunnel_rcu(ign->tunnels_r_l[h0 ^ h1]) {
215749c10f9STimo Teras 		if (local != t->parms.iph.saddr ||
216749c10f9STimo Teras 		    remote != t->parms.iph.daddr ||
217749c10f9STimo Teras 		    key != t->parms.i_key ||
218749c10f9STimo Teras 		    !(t->dev->flags & IFF_UP))
219749c10f9STimo Teras 			continue;
220749c10f9STimo Teras 
221749c10f9STimo Teras 		if (t->dev->type != ARPHRD_IPGRE &&
222749c10f9STimo Teras 		    t->dev->type != dev_type)
223749c10f9STimo Teras 			continue;
224749c10f9STimo Teras 
225afcf1242STimo Teras 		score = 0;
226749c10f9STimo Teras 		if (t->parms.link != link)
227afcf1242STimo Teras 			score |= 1;
228749c10f9STimo Teras 		if (t->dev->type != dev_type)
229afcf1242STimo Teras 			score |= 2;
230afcf1242STimo Teras 		if (score == 0)
2311da177e4SLinus Torvalds 			return t;
232afcf1242STimo Teras 
233afcf1242STimo Teras 		if (score < cand_score) {
234afcf1242STimo Teras 			cand = t;
235afcf1242STimo Teras 			cand_score = score;
236afcf1242STimo Teras 		}
237e1a80002SHerbert Xu 	}
238e1a80002SHerbert Xu 
2398d5b2c08SEric Dumazet 	for_each_ip_tunnel_rcu(ign->tunnels_r[h0 ^ h1]) {
240749c10f9STimo Teras 		if (remote != t->parms.iph.daddr ||
241749c10f9STimo Teras 		    key != t->parms.i_key ||
242749c10f9STimo Teras 		    !(t->dev->flags & IFF_UP))
243749c10f9STimo Teras 			continue;
244749c10f9STimo Teras 
245749c10f9STimo Teras 		if (t->dev->type != ARPHRD_IPGRE &&
246749c10f9STimo Teras 		    t->dev->type != dev_type)
247749c10f9STimo Teras 			continue;
248749c10f9STimo Teras 
249afcf1242STimo Teras 		score = 0;
250749c10f9STimo Teras 		if (t->parms.link != link)
251afcf1242STimo Teras 			score |= 1;
252749c10f9STimo Teras 		if (t->dev->type != dev_type)
253afcf1242STimo Teras 			score |= 2;
254afcf1242STimo Teras 		if (score == 0)
2551da177e4SLinus Torvalds 			return t;
256afcf1242STimo Teras 
257afcf1242STimo Teras 		if (score < cand_score) {
258afcf1242STimo Teras 			cand = t;
259afcf1242STimo Teras 			cand_score = score;
260afcf1242STimo Teras 		}
261e1a80002SHerbert Xu 	}
262e1a80002SHerbert Xu 
2638d5b2c08SEric Dumazet 	for_each_ip_tunnel_rcu(ign->tunnels_l[h1]) {
264749c10f9STimo Teras 		if ((local != t->parms.iph.saddr &&
265749c10f9STimo Teras 		     (local != t->parms.iph.daddr ||
266749c10f9STimo Teras 		      !ipv4_is_multicast(local))) ||
267749c10f9STimo Teras 		    key != t->parms.i_key ||
268749c10f9STimo Teras 		    !(t->dev->flags & IFF_UP))
269749c10f9STimo Teras 			continue;
270749c10f9STimo Teras 
271749c10f9STimo Teras 		if (t->dev->type != ARPHRD_IPGRE &&
272749c10f9STimo Teras 		    t->dev->type != dev_type)
273749c10f9STimo Teras 			continue;
274749c10f9STimo Teras 
275afcf1242STimo Teras 		score = 0;
276749c10f9STimo Teras 		if (t->parms.link != link)
277afcf1242STimo Teras 			score |= 1;
278749c10f9STimo Teras 		if (t->dev->type != dev_type)
279afcf1242STimo Teras 			score |= 2;
280afcf1242STimo Teras 		if (score == 0)
2811da177e4SLinus Torvalds 			return t;
282afcf1242STimo Teras 
283afcf1242STimo Teras 		if (score < cand_score) {
284afcf1242STimo Teras 			cand = t;
285afcf1242STimo Teras 			cand_score = score;
286afcf1242STimo Teras 		}
287e1a80002SHerbert Xu 	}
288e1a80002SHerbert Xu 
2898d5b2c08SEric Dumazet 	for_each_ip_tunnel_rcu(ign->tunnels_wc[h1]) {
290749c10f9STimo Teras 		if (t->parms.i_key != key ||
291749c10f9STimo Teras 		    !(t->dev->flags & IFF_UP))
292749c10f9STimo Teras 			continue;
293749c10f9STimo Teras 
294749c10f9STimo Teras 		if (t->dev->type != ARPHRD_IPGRE &&
295749c10f9STimo Teras 		    t->dev->type != dev_type)
296749c10f9STimo Teras 			continue;
297749c10f9STimo Teras 
298afcf1242STimo Teras 		score = 0;
299749c10f9STimo Teras 		if (t->parms.link != link)
300afcf1242STimo Teras 			score |= 1;
301749c10f9STimo Teras 		if (t->dev->type != dev_type)
302afcf1242STimo Teras 			score |= 2;
303afcf1242STimo Teras 		if (score == 0)
3041da177e4SLinus Torvalds 			return t;
305afcf1242STimo Teras 
306afcf1242STimo Teras 		if (score < cand_score) {
307afcf1242STimo Teras 			cand = t;
308afcf1242STimo Teras 			cand_score = score;
309afcf1242STimo Teras 		}
310e1a80002SHerbert Xu 	}
311e1a80002SHerbert Xu 
312afcf1242STimo Teras 	if (cand != NULL)
313afcf1242STimo Teras 		return cand;
3141da177e4SLinus Torvalds 
3158d5b2c08SEric Dumazet 	dev = ign->fb_tunnel_dev;
3168d5b2c08SEric Dumazet 	if (dev->flags & IFF_UP)
3178d5b2c08SEric Dumazet 		return netdev_priv(dev);
318749c10f9STimo Teras 
3191da177e4SLinus Torvalds 	return NULL;
3201da177e4SLinus Torvalds }
3211da177e4SLinus Torvalds 
3221507850bSEric Dumazet static struct ip_tunnel __rcu **__ipgre_bucket(struct ipgre_net *ign,
323f57e7d5aSPavel Emelyanov 		struct ip_tunnel_parm *parms)
3241da177e4SLinus Torvalds {
3255056a1efSYOSHIFUJI Hideaki 	__be32 remote = parms->iph.daddr;
3265056a1efSYOSHIFUJI Hideaki 	__be32 local = parms->iph.saddr;
3275056a1efSYOSHIFUJI Hideaki 	__be32 key = parms->i_key;
3281507850bSEric Dumazet 	unsigned int h = HASH(key);
3291da177e4SLinus Torvalds 	int prio = 0;
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 	if (local)
3321da177e4SLinus Torvalds 		prio |= 1;
333f97c1e0cSJoe Perches 	if (remote && !ipv4_is_multicast(remote)) {
3341da177e4SLinus Torvalds 		prio |= 2;
3351da177e4SLinus Torvalds 		h ^= HASH(remote);
3361da177e4SLinus Torvalds 	}
3371da177e4SLinus Torvalds 
338eb8ce741SPavel Emelyanov 	return &ign->tunnels[prio][h];
3391da177e4SLinus Torvalds }
3401da177e4SLinus Torvalds 
3411507850bSEric Dumazet static inline struct ip_tunnel __rcu **ipgre_bucket(struct ipgre_net *ign,
342f57e7d5aSPavel Emelyanov 		struct ip_tunnel *t)
3435056a1efSYOSHIFUJI Hideaki {
344f57e7d5aSPavel Emelyanov 	return __ipgre_bucket(ign, &t->parms);
3455056a1efSYOSHIFUJI Hideaki }
3465056a1efSYOSHIFUJI Hideaki 
347f57e7d5aSPavel Emelyanov static void ipgre_tunnel_link(struct ipgre_net *ign, struct ip_tunnel *t)
3481da177e4SLinus Torvalds {
3491507850bSEric Dumazet 	struct ip_tunnel __rcu **tp = ipgre_bucket(ign, t);
3501da177e4SLinus Torvalds 
3511507850bSEric Dumazet 	rcu_assign_pointer(t->next, rtnl_dereference(*tp));
3528d5b2c08SEric Dumazet 	rcu_assign_pointer(*tp, t);
3531da177e4SLinus Torvalds }
3541da177e4SLinus Torvalds 
355f57e7d5aSPavel Emelyanov static void ipgre_tunnel_unlink(struct ipgre_net *ign, struct ip_tunnel *t)
3561da177e4SLinus Torvalds {
3571507850bSEric Dumazet 	struct ip_tunnel __rcu **tp;
3581507850bSEric Dumazet 	struct ip_tunnel *iter;
3591da177e4SLinus Torvalds 
3601507850bSEric Dumazet 	for (tp = ipgre_bucket(ign, t);
3611507850bSEric Dumazet 	     (iter = rtnl_dereference(*tp)) != NULL;
3621507850bSEric Dumazet 	     tp = &iter->next) {
3631507850bSEric Dumazet 		if (t == iter) {
3641507850bSEric Dumazet 			rcu_assign_pointer(*tp, t->next);
3651da177e4SLinus Torvalds 			break;
3661da177e4SLinus Torvalds 		}
3671da177e4SLinus Torvalds 	}
3681da177e4SLinus Torvalds }
3691da177e4SLinus Torvalds 
370e1a80002SHerbert Xu static struct ip_tunnel *ipgre_tunnel_find(struct net *net,
371e1a80002SHerbert Xu 					   struct ip_tunnel_parm *parms,
372e1a80002SHerbert Xu 					   int type)
3731da177e4SLinus Torvalds {
374d5a0a1e3SAl Viro 	__be32 remote = parms->iph.daddr;
375d5a0a1e3SAl Viro 	__be32 local = parms->iph.saddr;
376d5a0a1e3SAl Viro 	__be32 key = parms->i_key;
377749c10f9STimo Teras 	int link = parms->link;
3781507850bSEric Dumazet 	struct ip_tunnel *t;
3791507850bSEric Dumazet 	struct ip_tunnel __rcu **tp;
380e1a80002SHerbert Xu 	struct ipgre_net *ign = net_generic(net, ipgre_net_id);
381e1a80002SHerbert Xu 
3821507850bSEric Dumazet 	for (tp = __ipgre_bucket(ign, parms);
3831507850bSEric Dumazet 	     (t = rtnl_dereference(*tp)) != NULL;
3841507850bSEric Dumazet 	     tp = &t->next)
385e1a80002SHerbert Xu 		if (local == t->parms.iph.saddr &&
386e1a80002SHerbert Xu 		    remote == t->parms.iph.daddr &&
387e1a80002SHerbert Xu 		    key == t->parms.i_key &&
388749c10f9STimo Teras 		    link == t->parms.link &&
389e1a80002SHerbert Xu 		    type == t->dev->type)
390e1a80002SHerbert Xu 			break;
391e1a80002SHerbert Xu 
392e1a80002SHerbert Xu 	return t;
393e1a80002SHerbert Xu }
394e1a80002SHerbert Xu 
395e1a80002SHerbert Xu static struct ip_tunnel *ipgre_tunnel_locate(struct net *net,
396e1a80002SHerbert Xu 		struct ip_tunnel_parm *parms, int create)
397e1a80002SHerbert Xu {
398e1a80002SHerbert Xu 	struct ip_tunnel *t, *nt;
3991da177e4SLinus Torvalds 	struct net_device *dev;
4001da177e4SLinus Torvalds 	char name[IFNAMSIZ];
401f57e7d5aSPavel Emelyanov 	struct ipgre_net *ign = net_generic(net, ipgre_net_id);
4021da177e4SLinus Torvalds 
403e1a80002SHerbert Xu 	t = ipgre_tunnel_find(net, parms, ARPHRD_IPGRE);
404e1a80002SHerbert Xu 	if (t || !create)
4051da177e4SLinus Torvalds 		return t;
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 	if (parms->name[0])
4081da177e4SLinus Torvalds 		strlcpy(name, parms->name, IFNAMSIZ);
40934cc7ba6SPavel Emelyanov 	else
410407d6fcbSstephen hemminger 		strcpy(name, "gre%d");
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds 	dev = alloc_netdev(sizeof(*t), name, ipgre_tunnel_setup);
4131da177e4SLinus Torvalds 	if (!dev)
4141da177e4SLinus Torvalds 		return NULL;
4151da177e4SLinus Torvalds 
4160b67ecebSPavel Emelyanov 	dev_net_set(dev, net);
4170b67ecebSPavel Emelyanov 
4182941a486SPatrick McHardy 	nt = netdev_priv(dev);
4191da177e4SLinus Torvalds 	nt->parms = *parms;
420c19e654dSHerbert Xu 	dev->rtnl_link_ops = &ipgre_link_ops;
4211da177e4SLinus Torvalds 
42242aa9162SHerbert Xu 	dev->mtu = ipgre_tunnel_bind_dev(dev);
42342aa9162SHerbert Xu 
424b37d428bSPavel Emelyanov 	if (register_netdevice(dev) < 0)
425b37d428bSPavel Emelyanov 		goto failed_free;
4261da177e4SLinus Torvalds 
427f2b3ee9eSWillem de Bruijn 	/* Can use a lockless transmit, unless we generate output sequences */
428f2b3ee9eSWillem de Bruijn 	if (!(nt->parms.o_flags & GRE_SEQ))
429f2b3ee9eSWillem de Bruijn 		dev->features |= NETIF_F_LLTX;
430f2b3ee9eSWillem de Bruijn 
4311da177e4SLinus Torvalds 	dev_hold(dev);
432f57e7d5aSPavel Emelyanov 	ipgre_tunnel_link(ign, nt);
4331da177e4SLinus Torvalds 	return nt;
4341da177e4SLinus Torvalds 
435b37d428bSPavel Emelyanov failed_free:
436b37d428bSPavel Emelyanov 	free_netdev(dev);
4371da177e4SLinus Torvalds 	return NULL;
4381da177e4SLinus Torvalds }
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds static void ipgre_tunnel_uninit(struct net_device *dev)
4411da177e4SLinus Torvalds {
442f57e7d5aSPavel Emelyanov 	struct net *net = dev_net(dev);
443f57e7d5aSPavel Emelyanov 	struct ipgre_net *ign = net_generic(net, ipgre_net_id);
444f57e7d5aSPavel Emelyanov 
445f57e7d5aSPavel Emelyanov 	ipgre_tunnel_unlink(ign, netdev_priv(dev));
4461da177e4SLinus Torvalds 	dev_put(dev);
4471da177e4SLinus Torvalds }
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds static void ipgre_err(struct sk_buff *skb, u32 info)
4511da177e4SLinus Torvalds {
4521da177e4SLinus Torvalds 
453071f92d0SRami Rosen /* All the routers (except for Linux) return only
4541da177e4SLinus Torvalds    8 bytes of packet payload. It means, that precise relaying of
4551da177e4SLinus Torvalds    ICMP in the real Internet is absolutely infeasible.
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds    Moreover, Cisco "wise men" put GRE key to the third word
4581da177e4SLinus Torvalds    in GRE header. It makes impossible maintaining even soft state for keyed
4591da177e4SLinus Torvalds    GRE tunnels with enabled checksum. Tell them "thank you".
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds    Well, I wonder, rfc1812 was written by Cisco employee,
462bff52857Sstephen hemminger    what the hell these idiots break standards established
463bff52857Sstephen hemminger    by themselves???
4641da177e4SLinus Torvalds  */
4651da177e4SLinus Torvalds 
466b71d1d42SEric Dumazet 	const struct iphdr *iph = (const struct iphdr *)skb->data;
467d5a0a1e3SAl Viro 	__be16	     *p = (__be16*)(skb->data+(iph->ihl<<2));
4681da177e4SLinus Torvalds 	int grehlen = (iph->ihl<<2) + 4;
46988c7664fSArnaldo Carvalho de Melo 	const int type = icmp_hdr(skb)->type;
47088c7664fSArnaldo Carvalho de Melo 	const int code = icmp_hdr(skb)->code;
4711da177e4SLinus Torvalds 	struct ip_tunnel *t;
472d5a0a1e3SAl Viro 	__be16 flags;
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds 	flags = p[0];
4751da177e4SLinus Torvalds 	if (flags&(GRE_CSUM|GRE_KEY|GRE_SEQ|GRE_ROUTING|GRE_VERSION)) {
4761da177e4SLinus Torvalds 		if (flags&(GRE_VERSION|GRE_ROUTING))
4771da177e4SLinus Torvalds 			return;
4781da177e4SLinus Torvalds 		if (flags&GRE_KEY) {
4791da177e4SLinus Torvalds 			grehlen += 4;
4801da177e4SLinus Torvalds 			if (flags&GRE_CSUM)
4811da177e4SLinus Torvalds 				grehlen += 4;
4821da177e4SLinus Torvalds 		}
4831da177e4SLinus Torvalds 	}
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds 	/* If only 8 bytes returned, keyed message will be dropped here */
4861da177e4SLinus Torvalds 	if (skb_headlen(skb) < grehlen)
4871da177e4SLinus Torvalds 		return;
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds 	switch (type) {
4901da177e4SLinus Torvalds 	default:
4911da177e4SLinus Torvalds 	case ICMP_PARAMETERPROB:
4921da177e4SLinus Torvalds 		return;
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds 	case ICMP_DEST_UNREACH:
4951da177e4SLinus Torvalds 		switch (code) {
4961da177e4SLinus Torvalds 		case ICMP_SR_FAILED:
4971da177e4SLinus Torvalds 		case ICMP_PORT_UNREACH:
4981da177e4SLinus Torvalds 			/* Impossible event. */
4991da177e4SLinus Torvalds 			return;
5001da177e4SLinus Torvalds 		case ICMP_FRAG_NEEDED:
5011da177e4SLinus Torvalds 			/* Soft state for pmtu is maintained by IP core. */
5021da177e4SLinus Torvalds 			return;
5031da177e4SLinus Torvalds 		default:
5041da177e4SLinus Torvalds 			/* All others are translated to HOST_UNREACH.
5051da177e4SLinus Torvalds 			   rfc2003 contains "deep thoughts" about NET_UNREACH,
5061da177e4SLinus Torvalds 			   I believe they are just ether pollution. --ANK
5071da177e4SLinus Torvalds 			 */
5081da177e4SLinus Torvalds 			break;
5091da177e4SLinus Torvalds 		}
5101da177e4SLinus Torvalds 		break;
5111da177e4SLinus Torvalds 	case ICMP_TIME_EXCEEDED:
5121da177e4SLinus Torvalds 		if (code != ICMP_EXC_TTL)
5131da177e4SLinus Torvalds 			return;
5141da177e4SLinus Torvalds 		break;
5151da177e4SLinus Torvalds 	}
5161da177e4SLinus Torvalds 
5178d5b2c08SEric Dumazet 	rcu_read_lock();
518749c10f9STimo Teras 	t = ipgre_tunnel_lookup(skb->dev, iph->daddr, iph->saddr,
519e1a80002SHerbert Xu 				flags & GRE_KEY ?
520e1a80002SHerbert Xu 				*(((__be32 *)p) + (grehlen / 4) - 1) : 0,
521e1a80002SHerbert Xu 				p[1]);
522f97c1e0cSJoe Perches 	if (t == NULL || t->parms.iph.daddr == 0 ||
523f97c1e0cSJoe Perches 	    ipv4_is_multicast(t->parms.iph.daddr))
5241da177e4SLinus Torvalds 		goto out;
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds 	if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
5271da177e4SLinus Torvalds 		goto out;
5281da177e4SLinus Torvalds 
529da6185d8SWei Yongjun 	if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
5301da177e4SLinus Torvalds 		t->err_count++;
5311da177e4SLinus Torvalds 	else
5321da177e4SLinus Torvalds 		t->err_count = 1;
5331da177e4SLinus Torvalds 	t->err_time = jiffies;
5341da177e4SLinus Torvalds out:
5358d5b2c08SEric Dumazet 	rcu_read_unlock();
5361da177e4SLinus Torvalds }
5371da177e4SLinus Torvalds 
538b71d1d42SEric Dumazet static inline void ipgre_ecn_decapsulate(const struct iphdr *iph, struct sk_buff *skb)
5391da177e4SLinus Torvalds {
5401da177e4SLinus Torvalds 	if (INET_ECN_is_ce(iph->tos)) {
5411da177e4SLinus Torvalds 		if (skb->protocol == htons(ETH_P_IP)) {
542eddc9ec5SArnaldo Carvalho de Melo 			IP_ECN_set_ce(ip_hdr(skb));
5431da177e4SLinus Torvalds 		} else if (skb->protocol == htons(ETH_P_IPV6)) {
5440660e03fSArnaldo Carvalho de Melo 			IP6_ECN_set_ce(ipv6_hdr(skb));
5451da177e4SLinus Torvalds 		}
5461da177e4SLinus Torvalds 	}
5471da177e4SLinus Torvalds }
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds static inline u8
550b71d1d42SEric Dumazet ipgre_ecn_encapsulate(u8 tos, const struct iphdr *old_iph, struct sk_buff *skb)
5511da177e4SLinus Torvalds {
5521da177e4SLinus Torvalds 	u8 inner = 0;
5531da177e4SLinus Torvalds 	if (skb->protocol == htons(ETH_P_IP))
5541da177e4SLinus Torvalds 		inner = old_iph->tos;
5551da177e4SLinus Torvalds 	else if (skb->protocol == htons(ETH_P_IPV6))
556b71d1d42SEric Dumazet 		inner = ipv6_get_dsfield((const struct ipv6hdr *)old_iph);
5571da177e4SLinus Torvalds 	return INET_ECN_encapsulate(tos, inner);
5581da177e4SLinus Torvalds }
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds static int ipgre_rcv(struct sk_buff *skb)
5611da177e4SLinus Torvalds {
562b71d1d42SEric Dumazet 	const struct iphdr *iph;
5631da177e4SLinus Torvalds 	u8     *h;
564d5a0a1e3SAl Viro 	__be16    flags;
565d3bc23e7SAl Viro 	__sum16   csum = 0;
566d5a0a1e3SAl Viro 	__be32 key = 0;
5671da177e4SLinus Torvalds 	u32    seqno = 0;
5681da177e4SLinus Torvalds 	struct ip_tunnel *tunnel;
5691da177e4SLinus Torvalds 	int    offset = 4;
570e1a80002SHerbert Xu 	__be16 gre_proto;
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	if (!pskb_may_pull(skb, 16))
5731da177e4SLinus Torvalds 		goto drop_nolock;
5741da177e4SLinus Torvalds 
575eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
5761da177e4SLinus Torvalds 	h = skb->data;
577d5a0a1e3SAl Viro 	flags = *(__be16*)h;
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds 	if (flags&(GRE_CSUM|GRE_KEY|GRE_ROUTING|GRE_SEQ|GRE_VERSION)) {
5801da177e4SLinus Torvalds 		/* - Version must be 0.
5811da177e4SLinus Torvalds 		   - We do not support routing headers.
5821da177e4SLinus Torvalds 		 */
5831da177e4SLinus Torvalds 		if (flags&(GRE_VERSION|GRE_ROUTING))
5841da177e4SLinus Torvalds 			goto drop_nolock;
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds 		if (flags&GRE_CSUM) {
587fb286bb2SHerbert Xu 			switch (skb->ip_summed) {
58884fa7933SPatrick McHardy 			case CHECKSUM_COMPLETE:
589d3bc23e7SAl Viro 				csum = csum_fold(skb->csum);
590fb286bb2SHerbert Xu 				if (!csum)
591fb286bb2SHerbert Xu 					break;
592fb286bb2SHerbert Xu 				/* fall through */
593fb286bb2SHerbert Xu 			case CHECKSUM_NONE:
594fb286bb2SHerbert Xu 				skb->csum = 0;
595fb286bb2SHerbert Xu 				csum = __skb_checksum_complete(skb);
59684fa7933SPatrick McHardy 				skb->ip_summed = CHECKSUM_COMPLETE;
5971da177e4SLinus Torvalds 			}
5981da177e4SLinus Torvalds 			offset += 4;
5991da177e4SLinus Torvalds 		}
6001da177e4SLinus Torvalds 		if (flags&GRE_KEY) {
601d5a0a1e3SAl Viro 			key = *(__be32*)(h + offset);
6021da177e4SLinus Torvalds 			offset += 4;
6031da177e4SLinus Torvalds 		}
6041da177e4SLinus Torvalds 		if (flags&GRE_SEQ) {
605d5a0a1e3SAl Viro 			seqno = ntohl(*(__be32*)(h + offset));
6061da177e4SLinus Torvalds 			offset += 4;
6071da177e4SLinus Torvalds 		}
6081da177e4SLinus Torvalds 	}
6091da177e4SLinus Torvalds 
610e1a80002SHerbert Xu 	gre_proto = *(__be16 *)(h + 2);
611e1a80002SHerbert Xu 
6128d5b2c08SEric Dumazet 	rcu_read_lock();
613749c10f9STimo Teras 	if ((tunnel = ipgre_tunnel_lookup(skb->dev,
614e1a80002SHerbert Xu 					  iph->saddr, iph->daddr, key,
615e1a80002SHerbert Xu 					  gre_proto))) {
616e985aad7SEric Dumazet 		struct pcpu_tstats *tstats;
617addd68ebSPavel Emelyanov 
6181da177e4SLinus Torvalds 		secpath_reset(skb);
6191da177e4SLinus Torvalds 
620e1a80002SHerbert Xu 		skb->protocol = gre_proto;
6211da177e4SLinus Torvalds 		/* WCCP version 1 and 2 protocol decoding.
6221da177e4SLinus Torvalds 		 * - Change protocol to IP
6231da177e4SLinus Torvalds 		 * - When dealing with WCCPv2, Skip extra 4 bytes in GRE header
6241da177e4SLinus Torvalds 		 */
625e1a80002SHerbert Xu 		if (flags == 0 && gre_proto == htons(ETH_P_WCCP)) {
626496c98dfSYOSHIFUJI Hideaki 			skb->protocol = htons(ETH_P_IP);
6271da177e4SLinus Torvalds 			if ((*(h + offset) & 0xF0) != 0x40)
6281da177e4SLinus Torvalds 				offset += 4;
6291da177e4SLinus Torvalds 		}
6301da177e4SLinus Torvalds 
6311d069167STimo Teras 		skb->mac_header = skb->network_header;
6324209fb60SArnaldo Carvalho de Melo 		__pskb_pull(skb, offset);
6339c70220bSArnaldo Carvalho de Melo 		skb_postpull_rcsum(skb, skb_transport_header(skb), offset);
6341da177e4SLinus Torvalds 		skb->pkt_type = PACKET_HOST;
6351da177e4SLinus Torvalds #ifdef CONFIG_NET_IPGRE_BROADCAST
636f97c1e0cSJoe Perches 		if (ipv4_is_multicast(iph->daddr)) {
6371da177e4SLinus Torvalds 			/* Looped back packet, drop it! */
638c7537967SDavid S. Miller 			if (rt_is_output_route(skb_rtable(skb)))
6391da177e4SLinus Torvalds 				goto drop;
640e985aad7SEric Dumazet 			tunnel->dev->stats.multicast++;
6411da177e4SLinus Torvalds 			skb->pkt_type = PACKET_BROADCAST;
6421da177e4SLinus Torvalds 		}
6431da177e4SLinus Torvalds #endif
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds 		if (((flags&GRE_CSUM) && csum) ||
6461da177e4SLinus Torvalds 		    (!(flags&GRE_CSUM) && tunnel->parms.i_flags&GRE_CSUM)) {
647e985aad7SEric Dumazet 			tunnel->dev->stats.rx_crc_errors++;
648e985aad7SEric Dumazet 			tunnel->dev->stats.rx_errors++;
6491da177e4SLinus Torvalds 			goto drop;
6501da177e4SLinus Torvalds 		}
6511da177e4SLinus Torvalds 		if (tunnel->parms.i_flags&GRE_SEQ) {
6521da177e4SLinus Torvalds 			if (!(flags&GRE_SEQ) ||
6531da177e4SLinus Torvalds 			    (tunnel->i_seqno && (s32)(seqno - tunnel->i_seqno) < 0)) {
654e985aad7SEric Dumazet 				tunnel->dev->stats.rx_fifo_errors++;
655e985aad7SEric Dumazet 				tunnel->dev->stats.rx_errors++;
6561da177e4SLinus Torvalds 				goto drop;
6571da177e4SLinus Torvalds 			}
6581da177e4SLinus Torvalds 			tunnel->i_seqno = seqno + 1;
6591da177e4SLinus Torvalds 		}
660e1a80002SHerbert Xu 
661e1a80002SHerbert Xu 		/* Warning: All skb pointers will be invalidated! */
662e1a80002SHerbert Xu 		if (tunnel->dev->type == ARPHRD_ETHER) {
663e1a80002SHerbert Xu 			if (!pskb_may_pull(skb, ETH_HLEN)) {
664e985aad7SEric Dumazet 				tunnel->dev->stats.rx_length_errors++;
665e985aad7SEric Dumazet 				tunnel->dev->stats.rx_errors++;
666e1a80002SHerbert Xu 				goto drop;
667e1a80002SHerbert Xu 			}
668e1a80002SHerbert Xu 
669e1a80002SHerbert Xu 			iph = ip_hdr(skb);
670e1a80002SHerbert Xu 			skb->protocol = eth_type_trans(skb, tunnel->dev);
671e1a80002SHerbert Xu 			skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
672e1a80002SHerbert Xu 		}
673e1a80002SHerbert Xu 
674e985aad7SEric Dumazet 		tstats = this_cpu_ptr(tunnel->dev->tstats);
675e985aad7SEric Dumazet 		tstats->rx_packets++;
676e985aad7SEric Dumazet 		tstats->rx_bytes += skb->len;
677e985aad7SEric Dumazet 
678e985aad7SEric Dumazet 		__skb_tunnel_rx(skb, tunnel->dev);
679e1a80002SHerbert Xu 
680e1a80002SHerbert Xu 		skb_reset_network_header(skb);
6811da177e4SLinus Torvalds 		ipgre_ecn_decapsulate(iph, skb);
682e1a80002SHerbert Xu 
683caf586e5SEric Dumazet 		netif_rx(skb);
6848990f468SEric Dumazet 
6858d5b2c08SEric Dumazet 		rcu_read_unlock();
6868990f468SEric Dumazet 		return 0;
6871da177e4SLinus Torvalds 	}
68845af08beSHerbert Xu 	icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
6891da177e4SLinus Torvalds 
6901da177e4SLinus Torvalds drop:
6918d5b2c08SEric Dumazet 	rcu_read_unlock();
6921da177e4SLinus Torvalds drop_nolock:
6931da177e4SLinus Torvalds 	kfree_skb(skb);
694a02cec21SEric Dumazet 	return 0;
6951da177e4SLinus Torvalds }
6961da177e4SLinus Torvalds 
6976fef4c0cSStephen Hemminger static netdev_tx_t ipgre_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
6981da177e4SLinus Torvalds {
6992941a486SPatrick McHardy 	struct ip_tunnel *tunnel = netdev_priv(dev);
700e985aad7SEric Dumazet 	struct pcpu_tstats *tstats;
701b71d1d42SEric Dumazet 	const struct iphdr  *old_iph = ip_hdr(skb);
702b71d1d42SEric Dumazet 	const struct iphdr  *tiph;
703cbb1e85fSDavid S. Miller 	struct flowi4 fl4;
7041da177e4SLinus Torvalds 	u8     tos;
705d5a0a1e3SAl Viro 	__be16 df;
7061da177e4SLinus Torvalds 	struct rtable *rt;     			/* Route to the other host */
7071da177e4SLinus Torvalds 	struct net_device *tdev;		/* Device to other host */
7081da177e4SLinus Torvalds 	struct iphdr  *iph;			/* Our new IP header */
709c2636b4dSChuck Lever 	unsigned int max_headroom;		/* The extra header space needed */
7101da177e4SLinus Torvalds 	int    gre_hlen;
711d5a0a1e3SAl Viro 	__be32 dst;
7121da177e4SLinus Torvalds 	int    mtu;
7131da177e4SLinus Torvalds 
714e1a80002SHerbert Xu 	if (dev->type == ARPHRD_ETHER)
715e1a80002SHerbert Xu 		IPCB(skb)->flags = 0;
716e1a80002SHerbert Xu 
717e1a80002SHerbert Xu 	if (dev->header_ops && dev->type == ARPHRD_IPGRE) {
7181da177e4SLinus Torvalds 		gre_hlen = 0;
719b71d1d42SEric Dumazet 		tiph = (const struct iphdr *)skb->data;
7201da177e4SLinus Torvalds 	} else {
7211da177e4SLinus Torvalds 		gre_hlen = tunnel->hlen;
7221da177e4SLinus Torvalds 		tiph = &tunnel->parms.iph;
7231da177e4SLinus Torvalds 	}
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds 	if ((dst = tiph->daddr) == 0) {
7261da177e4SLinus Torvalds 		/* NBMA tunnel */
7271da177e4SLinus Torvalds 
728adf30907SEric Dumazet 		if (skb_dst(skb) == NULL) {
729e985aad7SEric Dumazet 			dev->stats.tx_fifo_errors++;
7301da177e4SLinus Torvalds 			goto tx_error;
7311da177e4SLinus Torvalds 		}
7321da177e4SLinus Torvalds 
73361d57f87SDavid S. Miller 		if (skb->protocol == htons(ETH_P_IP)) {
734511c3f92SEric Dumazet 			rt = skb_rtable(skb);
73561d57f87SDavid S. Miller 			dst = rt->rt_gateway;
73661d57f87SDavid S. Miller 		}
737dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
7381da177e4SLinus Torvalds 		else if (skb->protocol == htons(ETH_P_IPV6)) {
739b71d1d42SEric Dumazet 			const struct in6_addr *addr6;
7400ec88662SDavid S. Miller 			struct neighbour *neigh;
7410ec88662SDavid S. Miller 			bool do_tx_error_icmp;
7421da177e4SLinus Torvalds 			int addr_type;
7431da177e4SLinus Torvalds 
7440ec88662SDavid S. Miller 			neigh = dst_neigh_lookup(skb_dst(skb), &ipv6_hdr(skb)->daddr);
7451da177e4SLinus Torvalds 			if (neigh == NULL)
7461da177e4SLinus Torvalds 				goto tx_error;
7471da177e4SLinus Torvalds 
748b71d1d42SEric Dumazet 			addr6 = (const struct in6_addr *)&neigh->primary_key;
7491da177e4SLinus Torvalds 			addr_type = ipv6_addr_type(addr6);
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds 			if (addr_type == IPV6_ADDR_ANY) {
7520660e03fSArnaldo Carvalho de Melo 				addr6 = &ipv6_hdr(skb)->daddr;
7531da177e4SLinus Torvalds 				addr_type = ipv6_addr_type(addr6);
7541da177e4SLinus Torvalds 			}
7551da177e4SLinus Torvalds 
7561da177e4SLinus Torvalds 			if ((addr_type & IPV6_ADDR_COMPATv4) == 0)
7570ec88662SDavid S. Miller 				do_tx_error_icmp = true;
7580ec88662SDavid S. Miller 			else {
7590ec88662SDavid S. Miller 				do_tx_error_icmp = false;
7601da177e4SLinus Torvalds 				dst = addr6->s6_addr32[3];
7611da177e4SLinus Torvalds 			}
7620ec88662SDavid S. Miller 			neigh_release(neigh);
7630ec88662SDavid S. Miller 			if (do_tx_error_icmp)
7640ec88662SDavid S. Miller 				goto tx_error_icmp;
7650ec88662SDavid S. Miller 		}
7661da177e4SLinus Torvalds #endif
7671da177e4SLinus Torvalds 		else
7681da177e4SLinus Torvalds 			goto tx_error;
7691da177e4SLinus Torvalds 	}
7701da177e4SLinus Torvalds 
7711da177e4SLinus Torvalds 	tos = tiph->tos;
772ee686ca9SAndreas Jaggi 	if (tos == 1) {
773ee686ca9SAndreas Jaggi 		tos = 0;
7741da177e4SLinus Torvalds 		if (skb->protocol == htons(ETH_P_IP))
7751da177e4SLinus Torvalds 			tos = old_iph->tos;
776dd4ba83dSStephen Hemminger 		else if (skb->protocol == htons(ETH_P_IPV6))
777b71d1d42SEric Dumazet 			tos = ipv6_get_dsfield((const struct ipv6hdr *)old_iph);
7781da177e4SLinus Torvalds 	}
7791da177e4SLinus Torvalds 
780cbb1e85fSDavid S. Miller 	rt = ip_route_output_gre(dev_net(dev), &fl4, dst, tiph->saddr,
78178fbfd8aSDavid S. Miller 				 tunnel->parms.o_key, RT_TOS(tos),
78278fbfd8aSDavid S. Miller 				 tunnel->parms.link);
783b23dd4feSDavid S. Miller 	if (IS_ERR(rt)) {
784e985aad7SEric Dumazet 		dev->stats.tx_carrier_errors++;
7851da177e4SLinus Torvalds 		goto tx_error;
7861da177e4SLinus Torvalds 	}
787d8d1f30bSChangli Gao 	tdev = rt->dst.dev;
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds 	if (tdev == dev) {
7901da177e4SLinus Torvalds 		ip_rt_put(rt);
791e985aad7SEric Dumazet 		dev->stats.collisions++;
7921da177e4SLinus Torvalds 		goto tx_error;
7931da177e4SLinus Torvalds 	}
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds 	df = tiph->frag_off;
7961da177e4SLinus Torvalds 	if (df)
797d8d1f30bSChangli Gao 		mtu = dst_mtu(&rt->dst) - dev->hard_header_len - tunnel->hlen;
7981da177e4SLinus Torvalds 	else
799adf30907SEric Dumazet 		mtu = skb_dst(skb) ? dst_mtu(skb_dst(skb)) : dev->mtu;
8001da177e4SLinus Torvalds 
801adf30907SEric Dumazet 	if (skb_dst(skb))
802adf30907SEric Dumazet 		skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu);
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds 	if (skb->protocol == htons(ETH_P_IP)) {
8051da177e4SLinus Torvalds 		df |= (old_iph->frag_off&htons(IP_DF));
8061da177e4SLinus Torvalds 
8071da177e4SLinus Torvalds 		if ((old_iph->frag_off&htons(IP_DF)) &&
8081da177e4SLinus Torvalds 		    mtu < ntohs(old_iph->tot_len)) {
8091da177e4SLinus Torvalds 			icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
8101da177e4SLinus Torvalds 			ip_rt_put(rt);
8111da177e4SLinus Torvalds 			goto tx_error;
8121da177e4SLinus Torvalds 		}
8131da177e4SLinus Torvalds 	}
814dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
8151da177e4SLinus Torvalds 	else if (skb->protocol == htons(ETH_P_IPV6)) {
816adf30907SEric Dumazet 		struct rt6_info *rt6 = (struct rt6_info *)skb_dst(skb);
8171da177e4SLinus Torvalds 
818adf30907SEric Dumazet 		if (rt6 && mtu < dst_mtu(skb_dst(skb)) && mtu >= IPV6_MIN_MTU) {
819f97c1e0cSJoe Perches 			if ((tunnel->parms.iph.daddr &&
820f97c1e0cSJoe Perches 			     !ipv4_is_multicast(tunnel->parms.iph.daddr)) ||
8211da177e4SLinus Torvalds 			    rt6->rt6i_dst.plen == 128) {
8221da177e4SLinus Torvalds 				rt6->rt6i_flags |= RTF_MODIFIED;
823defb3519SDavid S. Miller 				dst_metric_set(skb_dst(skb), RTAX_MTU, mtu);
8241da177e4SLinus Torvalds 			}
8251da177e4SLinus Torvalds 		}
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds 		if (mtu >= IPV6_MIN_MTU && mtu < skb->len - tunnel->hlen + gre_hlen) {
8283ffe533cSAlexey Dobriyan 			icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
8291da177e4SLinus Torvalds 			ip_rt_put(rt);
8301da177e4SLinus Torvalds 			goto tx_error;
8311da177e4SLinus Torvalds 		}
8321da177e4SLinus Torvalds 	}
8331da177e4SLinus Torvalds #endif
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds 	if (tunnel->err_count > 0) {
836da6185d8SWei Yongjun 		if (time_before(jiffies,
837da6185d8SWei Yongjun 				tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
8381da177e4SLinus Torvalds 			tunnel->err_count--;
8391da177e4SLinus Torvalds 
8401da177e4SLinus Torvalds 			dst_link_failure(skb);
8411da177e4SLinus Torvalds 		} else
8421da177e4SLinus Torvalds 			tunnel->err_count = 0;
8431da177e4SLinus Torvalds 	}
8441da177e4SLinus Torvalds 
845d8d1f30bSChangli Gao 	max_headroom = LL_RESERVED_SPACE(tdev) + gre_hlen + rt->dst.header_len;
8461da177e4SLinus Torvalds 
847cfbba49dSPatrick McHardy 	if (skb_headroom(skb) < max_headroom || skb_shared(skb)||
848cfbba49dSPatrick McHardy 	    (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
8491da177e4SLinus Torvalds 		struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
850805dc1d6SHerbert Xu 		if (max_headroom > dev->needed_headroom)
851805dc1d6SHerbert Xu 			dev->needed_headroom = max_headroom;
8521da177e4SLinus Torvalds 		if (!new_skb) {
8531da177e4SLinus Torvalds 			ip_rt_put(rt);
854e985aad7SEric Dumazet 			dev->stats.tx_dropped++;
8551da177e4SLinus Torvalds 			dev_kfree_skb(skb);
8566ed10654SPatrick McHardy 			return NETDEV_TX_OK;
8571da177e4SLinus Torvalds 		}
8581da177e4SLinus Torvalds 		if (skb->sk)
8591da177e4SLinus Torvalds 			skb_set_owner_w(new_skb, skb->sk);
8601da177e4SLinus Torvalds 		dev_kfree_skb(skb);
8611da177e4SLinus Torvalds 		skb = new_skb;
862eddc9ec5SArnaldo Carvalho de Melo 		old_iph = ip_hdr(skb);
8631da177e4SLinus Torvalds 	}
8641da177e4SLinus Torvalds 
86564194c31SHerbert Xu 	skb_reset_transport_header(skb);
866e2d1bca7SArnaldo Carvalho de Melo 	skb_push(skb, gre_hlen);
867e2d1bca7SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
8681da177e4SLinus Torvalds 	memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
86948d5cad8SPatrick McHardy 	IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
87048d5cad8SPatrick McHardy 			      IPSKB_REROUTED);
871adf30907SEric Dumazet 	skb_dst_drop(skb);
872d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds 	/*
8751da177e4SLinus Torvalds 	 *	Push down and install the IPIP header.
8761da177e4SLinus Torvalds 	 */
8771da177e4SLinus Torvalds 
878eddc9ec5SArnaldo Carvalho de Melo 	iph 			=	ip_hdr(skb);
8791da177e4SLinus Torvalds 	iph->version		=	4;
8801da177e4SLinus Torvalds 	iph->ihl		=	sizeof(struct iphdr) >> 2;
8811da177e4SLinus Torvalds 	iph->frag_off		=	df;
8821da177e4SLinus Torvalds 	iph->protocol		=	IPPROTO_GRE;
8831da177e4SLinus Torvalds 	iph->tos		=	ipgre_ecn_encapsulate(tos, old_iph, skb);
884cbb1e85fSDavid S. Miller 	iph->daddr		=	fl4.daddr;
885cbb1e85fSDavid S. Miller 	iph->saddr		=	fl4.saddr;
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds 	if ((iph->ttl = tiph->ttl) == 0) {
8881da177e4SLinus Torvalds 		if (skb->protocol == htons(ETH_P_IP))
8891da177e4SLinus Torvalds 			iph->ttl = old_iph->ttl;
890dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
8911da177e4SLinus Torvalds 		else if (skb->protocol == htons(ETH_P_IPV6))
892b71d1d42SEric Dumazet 			iph->ttl = ((const struct ipv6hdr *)old_iph)->hop_limit;
8931da177e4SLinus Torvalds #endif
8941da177e4SLinus Torvalds 		else
895323e126fSDavid S. Miller 			iph->ttl = ip4_dst_hoplimit(&rt->dst);
8961da177e4SLinus Torvalds 	}
8971da177e4SLinus Torvalds 
898d5a0a1e3SAl Viro 	((__be16 *)(iph + 1))[0] = tunnel->parms.o_flags;
899e1a80002SHerbert Xu 	((__be16 *)(iph + 1))[1] = (dev->type == ARPHRD_ETHER) ?
900e1a80002SHerbert Xu 				   htons(ETH_P_TEB) : skb->protocol;
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds 	if (tunnel->parms.o_flags&(GRE_KEY|GRE_CSUM|GRE_SEQ)) {
903d5a0a1e3SAl Viro 		__be32 *ptr = (__be32*)(((u8*)iph) + tunnel->hlen - 4);
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 		if (tunnel->parms.o_flags&GRE_SEQ) {
9061da177e4SLinus Torvalds 			++tunnel->o_seqno;
9071da177e4SLinus Torvalds 			*ptr = htonl(tunnel->o_seqno);
9081da177e4SLinus Torvalds 			ptr--;
9091da177e4SLinus Torvalds 		}
9101da177e4SLinus Torvalds 		if (tunnel->parms.o_flags&GRE_KEY) {
9111da177e4SLinus Torvalds 			*ptr = tunnel->parms.o_key;
9121da177e4SLinus Torvalds 			ptr--;
9131da177e4SLinus Torvalds 		}
9141da177e4SLinus Torvalds 		if (tunnel->parms.o_flags&GRE_CSUM) {
9151da177e4SLinus Torvalds 			*ptr = 0;
9165f92a738SAl Viro 			*(__sum16*)ptr = ip_compute_csum((void*)(iph+1), skb->len - sizeof(struct iphdr));
9171da177e4SLinus Torvalds 		}
9181da177e4SLinus Torvalds 	}
9191da177e4SLinus Torvalds 
9201da177e4SLinus Torvalds 	nf_reset(skb);
921e985aad7SEric Dumazet 	tstats = this_cpu_ptr(dev->tstats);
922e985aad7SEric Dumazet 	__IPTUNNEL_XMIT(tstats, &dev->stats);
9236ed10654SPatrick McHardy 	return NETDEV_TX_OK;
9241da177e4SLinus Torvalds 
925496053f4SDavid S. Miller #if IS_ENABLED(CONFIG_IPV6)
9261da177e4SLinus Torvalds tx_error_icmp:
9271da177e4SLinus Torvalds 	dst_link_failure(skb);
928496053f4SDavid S. Miller #endif
9291da177e4SLinus Torvalds tx_error:
930e985aad7SEric Dumazet 	dev->stats.tx_errors++;
9311da177e4SLinus Torvalds 	dev_kfree_skb(skb);
9326ed10654SPatrick McHardy 	return NETDEV_TX_OK;
9331da177e4SLinus Torvalds }
9341da177e4SLinus Torvalds 
93542aa9162SHerbert Xu static int ipgre_tunnel_bind_dev(struct net_device *dev)
936ee34c1ebSMichal Schmidt {
937ee34c1ebSMichal Schmidt 	struct net_device *tdev = NULL;
938ee34c1ebSMichal Schmidt 	struct ip_tunnel *tunnel;
939b71d1d42SEric Dumazet 	const struct iphdr *iph;
940ee34c1ebSMichal Schmidt 	int hlen = LL_MAX_HEADER;
941ee34c1ebSMichal Schmidt 	int mtu = ETH_DATA_LEN;
942ee34c1ebSMichal Schmidt 	int addend = sizeof(struct iphdr) + 4;
943ee34c1ebSMichal Schmidt 
944ee34c1ebSMichal Schmidt 	tunnel = netdev_priv(dev);
945ee34c1ebSMichal Schmidt 	iph = &tunnel->parms.iph;
946ee34c1ebSMichal Schmidt 
947c95b819aSHerbert Xu 	/* Guess output device to choose reasonable mtu and needed_headroom */
948ee34c1ebSMichal Schmidt 
949ee34c1ebSMichal Schmidt 	if (iph->daddr) {
950cbb1e85fSDavid S. Miller 		struct flowi4 fl4;
951cbb1e85fSDavid S. Miller 		struct rtable *rt;
952cbb1e85fSDavid S. Miller 
953cbb1e85fSDavid S. Miller 		rt = ip_route_output_gre(dev_net(dev), &fl4,
95478fbfd8aSDavid S. Miller 					 iph->daddr, iph->saddr,
95578fbfd8aSDavid S. Miller 					 tunnel->parms.o_key,
95678fbfd8aSDavid S. Miller 					 RT_TOS(iph->tos),
95778fbfd8aSDavid S. Miller 					 tunnel->parms.link);
958b23dd4feSDavid S. Miller 		if (!IS_ERR(rt)) {
959d8d1f30bSChangli Gao 			tdev = rt->dst.dev;
960ee34c1ebSMichal Schmidt 			ip_rt_put(rt);
961ee34c1ebSMichal Schmidt 		}
962e1a80002SHerbert Xu 
963e1a80002SHerbert Xu 		if (dev->type != ARPHRD_ETHER)
964ee34c1ebSMichal Schmidt 			dev->flags |= IFF_POINTOPOINT;
965ee34c1ebSMichal Schmidt 	}
966ee34c1ebSMichal Schmidt 
967ee34c1ebSMichal Schmidt 	if (!tdev && tunnel->parms.link)
96896635522SPavel Emelyanov 		tdev = __dev_get_by_index(dev_net(dev), tunnel->parms.link);
969ee34c1ebSMichal Schmidt 
970ee34c1ebSMichal Schmidt 	if (tdev) {
971c95b819aSHerbert Xu 		hlen = tdev->hard_header_len + tdev->needed_headroom;
972ee34c1ebSMichal Schmidt 		mtu = tdev->mtu;
973ee34c1ebSMichal Schmidt 	}
974ee34c1ebSMichal Schmidt 	dev->iflink = tunnel->parms.link;
975ee34c1ebSMichal Schmidt 
976ee34c1ebSMichal Schmidt 	/* Precalculate GRE options length */
977ee34c1ebSMichal Schmidt 	if (tunnel->parms.o_flags&(GRE_CSUM|GRE_KEY|GRE_SEQ)) {
978ee34c1ebSMichal Schmidt 		if (tunnel->parms.o_flags&GRE_CSUM)
979ee34c1ebSMichal Schmidt 			addend += 4;
980ee34c1ebSMichal Schmidt 		if (tunnel->parms.o_flags&GRE_KEY)
981ee34c1ebSMichal Schmidt 			addend += 4;
982ee34c1ebSMichal Schmidt 		if (tunnel->parms.o_flags&GRE_SEQ)
983ee34c1ebSMichal Schmidt 			addend += 4;
984ee34c1ebSMichal Schmidt 	}
985c95b819aSHerbert Xu 	dev->needed_headroom = addend + hlen;
9868cdb0456STom Goff 	mtu -= dev->hard_header_len + addend;
98742aa9162SHerbert Xu 
98842aa9162SHerbert Xu 	if (mtu < 68)
98942aa9162SHerbert Xu 		mtu = 68;
99042aa9162SHerbert Xu 
991ee34c1ebSMichal Schmidt 	tunnel->hlen = addend;
992ee34c1ebSMichal Schmidt 
99342aa9162SHerbert Xu 	return mtu;
994ee34c1ebSMichal Schmidt }
995ee34c1ebSMichal Schmidt 
9961da177e4SLinus Torvalds static int
9971da177e4SLinus Torvalds ipgre_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
9981da177e4SLinus Torvalds {
9991da177e4SLinus Torvalds 	int err = 0;
10001da177e4SLinus Torvalds 	struct ip_tunnel_parm p;
10011da177e4SLinus Torvalds 	struct ip_tunnel *t;
1002f57e7d5aSPavel Emelyanov 	struct net *net = dev_net(dev);
1003f57e7d5aSPavel Emelyanov 	struct ipgre_net *ign = net_generic(net, ipgre_net_id);
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds 	switch (cmd) {
10061da177e4SLinus Torvalds 	case SIOCGETTUNNEL:
10071da177e4SLinus Torvalds 		t = NULL;
10087daa0004SPavel Emelyanov 		if (dev == ign->fb_tunnel_dev) {
10091da177e4SLinus Torvalds 			if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
10101da177e4SLinus Torvalds 				err = -EFAULT;
10111da177e4SLinus Torvalds 				break;
10121da177e4SLinus Torvalds 			}
1013f57e7d5aSPavel Emelyanov 			t = ipgre_tunnel_locate(net, &p, 0);
10141da177e4SLinus Torvalds 		}
10151da177e4SLinus Torvalds 		if (t == NULL)
10162941a486SPatrick McHardy 			t = netdev_priv(dev);
10171da177e4SLinus Torvalds 		memcpy(&p, &t->parms, sizeof(p));
10181da177e4SLinus Torvalds 		if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
10191da177e4SLinus Torvalds 			err = -EFAULT;
10201da177e4SLinus Torvalds 		break;
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds 	case SIOCADDTUNNEL:
10231da177e4SLinus Torvalds 	case SIOCCHGTUNNEL:
10241da177e4SLinus Torvalds 		err = -EPERM;
10251da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
10261da177e4SLinus Torvalds 			goto done;
10271da177e4SLinus Torvalds 
10281da177e4SLinus Torvalds 		err = -EFAULT;
10291da177e4SLinus Torvalds 		if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
10301da177e4SLinus Torvalds 			goto done;
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds 		err = -EINVAL;
10331da177e4SLinus Torvalds 		if (p.iph.version != 4 || p.iph.protocol != IPPROTO_GRE ||
10341da177e4SLinus Torvalds 		    p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)) ||
10351da177e4SLinus Torvalds 		    ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING)))
10361da177e4SLinus Torvalds 			goto done;
10371da177e4SLinus Torvalds 		if (p.iph.ttl)
10381da177e4SLinus Torvalds 			p.iph.frag_off |= htons(IP_DF);
10391da177e4SLinus Torvalds 
10401da177e4SLinus Torvalds 		if (!(p.i_flags&GRE_KEY))
10411da177e4SLinus Torvalds 			p.i_key = 0;
10421da177e4SLinus Torvalds 		if (!(p.o_flags&GRE_KEY))
10431da177e4SLinus Torvalds 			p.o_key = 0;
10441da177e4SLinus Torvalds 
1045f57e7d5aSPavel Emelyanov 		t = ipgre_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL);
10461da177e4SLinus Torvalds 
10477daa0004SPavel Emelyanov 		if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
10481da177e4SLinus Torvalds 			if (t != NULL) {
10491da177e4SLinus Torvalds 				if (t->dev != dev) {
10501da177e4SLinus Torvalds 					err = -EEXIST;
10511da177e4SLinus Torvalds 					break;
10521da177e4SLinus Torvalds 				}
10531da177e4SLinus Torvalds 			} else {
10541507850bSEric Dumazet 				unsigned int nflags = 0;
10551da177e4SLinus Torvalds 
10562941a486SPatrick McHardy 				t = netdev_priv(dev);
10571da177e4SLinus Torvalds 
1058f97c1e0cSJoe Perches 				if (ipv4_is_multicast(p.iph.daddr))
10591da177e4SLinus Torvalds 					nflags = IFF_BROADCAST;
10601da177e4SLinus Torvalds 				else if (p.iph.daddr)
10611da177e4SLinus Torvalds 					nflags = IFF_POINTOPOINT;
10621da177e4SLinus Torvalds 
10631da177e4SLinus Torvalds 				if ((dev->flags^nflags)&(IFF_POINTOPOINT|IFF_BROADCAST)) {
10641da177e4SLinus Torvalds 					err = -EINVAL;
10651da177e4SLinus Torvalds 					break;
10661da177e4SLinus Torvalds 				}
1067f57e7d5aSPavel Emelyanov 				ipgre_tunnel_unlink(ign, t);
106874b0b85bSPavel Emelyanov 				synchronize_net();
10691da177e4SLinus Torvalds 				t->parms.iph.saddr = p.iph.saddr;
10701da177e4SLinus Torvalds 				t->parms.iph.daddr = p.iph.daddr;
10711da177e4SLinus Torvalds 				t->parms.i_key = p.i_key;
10721da177e4SLinus Torvalds 				t->parms.o_key = p.o_key;
10731da177e4SLinus Torvalds 				memcpy(dev->dev_addr, &p.iph.saddr, 4);
10741da177e4SLinus Torvalds 				memcpy(dev->broadcast, &p.iph.daddr, 4);
1075f57e7d5aSPavel Emelyanov 				ipgre_tunnel_link(ign, t);
10761da177e4SLinus Torvalds 				netdev_state_change(dev);
10771da177e4SLinus Torvalds 			}
10781da177e4SLinus Torvalds 		}
10791da177e4SLinus Torvalds 
10801da177e4SLinus Torvalds 		if (t) {
10811da177e4SLinus Torvalds 			err = 0;
10821da177e4SLinus Torvalds 			if (cmd == SIOCCHGTUNNEL) {
10831da177e4SLinus Torvalds 				t->parms.iph.ttl = p.iph.ttl;
10841da177e4SLinus Torvalds 				t->parms.iph.tos = p.iph.tos;
10851da177e4SLinus Torvalds 				t->parms.iph.frag_off = p.iph.frag_off;
1086ee34c1ebSMichal Schmidt 				if (t->parms.link != p.link) {
1087ee34c1ebSMichal Schmidt 					t->parms.link = p.link;
108842aa9162SHerbert Xu 					dev->mtu = ipgre_tunnel_bind_dev(dev);
1089ee34c1ebSMichal Schmidt 					netdev_state_change(dev);
1090ee34c1ebSMichal Schmidt 				}
10911da177e4SLinus Torvalds 			}
10921da177e4SLinus Torvalds 			if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
10931da177e4SLinus Torvalds 				err = -EFAULT;
10941da177e4SLinus Torvalds 		} else
10951da177e4SLinus Torvalds 			err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
10961da177e4SLinus Torvalds 		break;
10971da177e4SLinus Torvalds 
10981da177e4SLinus Torvalds 	case SIOCDELTUNNEL:
10991da177e4SLinus Torvalds 		err = -EPERM;
11001da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
11011da177e4SLinus Torvalds 			goto done;
11021da177e4SLinus Torvalds 
11037daa0004SPavel Emelyanov 		if (dev == ign->fb_tunnel_dev) {
11041da177e4SLinus Torvalds 			err = -EFAULT;
11051da177e4SLinus Torvalds 			if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
11061da177e4SLinus Torvalds 				goto done;
11071da177e4SLinus Torvalds 			err = -ENOENT;
1108f57e7d5aSPavel Emelyanov 			if ((t = ipgre_tunnel_locate(net, &p, 0)) == NULL)
11091da177e4SLinus Torvalds 				goto done;
11101da177e4SLinus Torvalds 			err = -EPERM;
11117daa0004SPavel Emelyanov 			if (t == netdev_priv(ign->fb_tunnel_dev))
11121da177e4SLinus Torvalds 				goto done;
11131da177e4SLinus Torvalds 			dev = t->dev;
11141da177e4SLinus Torvalds 		}
111522f8cde5SStephen Hemminger 		unregister_netdevice(dev);
111622f8cde5SStephen Hemminger 		err = 0;
11171da177e4SLinus Torvalds 		break;
11181da177e4SLinus Torvalds 
11191da177e4SLinus Torvalds 	default:
11201da177e4SLinus Torvalds 		err = -EINVAL;
11211da177e4SLinus Torvalds 	}
11221da177e4SLinus Torvalds 
11231da177e4SLinus Torvalds done:
11241da177e4SLinus Torvalds 	return err;
11251da177e4SLinus Torvalds }
11261da177e4SLinus Torvalds 
11271da177e4SLinus Torvalds static int ipgre_tunnel_change_mtu(struct net_device *dev, int new_mtu)
11281da177e4SLinus Torvalds {
11292941a486SPatrick McHardy 	struct ip_tunnel *tunnel = netdev_priv(dev);
1130c95b819aSHerbert Xu 	if (new_mtu < 68 ||
1131c95b819aSHerbert Xu 	    new_mtu > 0xFFF8 - dev->hard_header_len - tunnel->hlen)
11321da177e4SLinus Torvalds 		return -EINVAL;
11331da177e4SLinus Torvalds 	dev->mtu = new_mtu;
11341da177e4SLinus Torvalds 	return 0;
11351da177e4SLinus Torvalds }
11361da177e4SLinus Torvalds 
11371da177e4SLinus Torvalds /* Nice toy. Unfortunately, useless in real life :-)
11381da177e4SLinus Torvalds    It allows to construct virtual multiprotocol broadcast "LAN"
11391da177e4SLinus Torvalds    over the Internet, provided multicast routing is tuned.
11401da177e4SLinus Torvalds 
11411da177e4SLinus Torvalds 
11421da177e4SLinus Torvalds    I have no idea was this bicycle invented before me,
11431da177e4SLinus Torvalds    so that I had to set ARPHRD_IPGRE to a random value.
11441da177e4SLinus Torvalds    I have an impression, that Cisco could make something similar,
11451da177e4SLinus Torvalds    but this feature is apparently missing in IOS<=11.2(8).
11461da177e4SLinus Torvalds 
11471da177e4SLinus Torvalds    I set up 10.66.66/24 and fec0:6666:6666::0/96 as virtual networks
11481da177e4SLinus Torvalds    with broadcast 224.66.66.66. If you have access to mbone, play with me :-)
11491da177e4SLinus Torvalds 
11501da177e4SLinus Torvalds    ping -t 255 224.66.66.66
11511da177e4SLinus Torvalds 
11521da177e4SLinus Torvalds    If nobody answers, mbone does not work.
11531da177e4SLinus Torvalds 
11541da177e4SLinus Torvalds    ip tunnel add Universe mode gre remote 224.66.66.66 local <Your_real_addr> ttl 255
11551da177e4SLinus Torvalds    ip addr add 10.66.66.<somewhat>/24 dev Universe
11561da177e4SLinus Torvalds    ifconfig Universe up
11571da177e4SLinus Torvalds    ifconfig Universe add fe80::<Your_real_addr>/10
11581da177e4SLinus Torvalds    ifconfig Universe add fec0:6666:6666::<Your_real_addr>/96
11591da177e4SLinus Torvalds    ftp 10.66.66.66
11601da177e4SLinus Torvalds    ...
11611da177e4SLinus Torvalds    ftp fec0:6666:6666::193.233.7.65
11621da177e4SLinus Torvalds    ...
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds  */
11651da177e4SLinus Torvalds 
11663b04dddeSStephen Hemminger static int ipgre_header(struct sk_buff *skb, struct net_device *dev,
11673b04dddeSStephen Hemminger 			unsigned short type,
11681507850bSEric Dumazet 			const void *daddr, const void *saddr, unsigned int len)
11691da177e4SLinus Torvalds {
11702941a486SPatrick McHardy 	struct ip_tunnel *t = netdev_priv(dev);
11711da177e4SLinus Torvalds 	struct iphdr *iph = (struct iphdr *)skb_push(skb, t->hlen);
1172d5a0a1e3SAl Viro 	__be16 *p = (__be16*)(iph+1);
11731da177e4SLinus Torvalds 
11741da177e4SLinus Torvalds 	memcpy(iph, &t->parms.iph, sizeof(struct iphdr));
11751da177e4SLinus Torvalds 	p[0]		= t->parms.o_flags;
11761da177e4SLinus Torvalds 	p[1]		= htons(type);
11771da177e4SLinus Torvalds 
11781da177e4SLinus Torvalds 	/*
11791da177e4SLinus Torvalds 	 *	Set the source hardware address.
11801da177e4SLinus Torvalds 	 */
11811da177e4SLinus Torvalds 
11821da177e4SLinus Torvalds 	if (saddr)
11831da177e4SLinus Torvalds 		memcpy(&iph->saddr, saddr, 4);
11846d55cb91STimo Teräs 	if (daddr)
11851da177e4SLinus Torvalds 		memcpy(&iph->daddr, daddr, 4);
11866d55cb91STimo Teräs 	if (iph->daddr)
11871da177e4SLinus Torvalds 		return t->hlen;
11881da177e4SLinus Torvalds 
11891da177e4SLinus Torvalds 	return -t->hlen;
11901da177e4SLinus Torvalds }
11911da177e4SLinus Torvalds 
11926a5f44d7STimo Teras static int ipgre_header_parse(const struct sk_buff *skb, unsigned char *haddr)
11936a5f44d7STimo Teras {
1194b71d1d42SEric Dumazet 	const struct iphdr *iph = (const struct iphdr *) skb_mac_header(skb);
11956a5f44d7STimo Teras 	memcpy(haddr, &iph->saddr, 4);
11966a5f44d7STimo Teras 	return 4;
11976a5f44d7STimo Teras }
11986a5f44d7STimo Teras 
11993b04dddeSStephen Hemminger static const struct header_ops ipgre_header_ops = {
12003b04dddeSStephen Hemminger 	.create	= ipgre_header,
12016a5f44d7STimo Teras 	.parse	= ipgre_header_parse,
12023b04dddeSStephen Hemminger };
12033b04dddeSStephen Hemminger 
12046a5f44d7STimo Teras #ifdef CONFIG_NET_IPGRE_BROADCAST
12051da177e4SLinus Torvalds static int ipgre_open(struct net_device *dev)
12061da177e4SLinus Torvalds {
12072941a486SPatrick McHardy 	struct ip_tunnel *t = netdev_priv(dev);
12081da177e4SLinus Torvalds 
1209f97c1e0cSJoe Perches 	if (ipv4_is_multicast(t->parms.iph.daddr)) {
1210cbb1e85fSDavid S. Miller 		struct flowi4 fl4;
1211cbb1e85fSDavid S. Miller 		struct rtable *rt;
1212cbb1e85fSDavid S. Miller 
1213cbb1e85fSDavid S. Miller 		rt = ip_route_output_gre(dev_net(dev), &fl4,
121478fbfd8aSDavid S. Miller 					 t->parms.iph.daddr,
121578fbfd8aSDavid S. Miller 					 t->parms.iph.saddr,
121678fbfd8aSDavid S. Miller 					 t->parms.o_key,
121778fbfd8aSDavid S. Miller 					 RT_TOS(t->parms.iph.tos),
121878fbfd8aSDavid S. Miller 					 t->parms.link);
1219b23dd4feSDavid S. Miller 		if (IS_ERR(rt))
12201da177e4SLinus Torvalds 			return -EADDRNOTAVAIL;
1221d8d1f30bSChangli Gao 		dev = rt->dst.dev;
12221da177e4SLinus Torvalds 		ip_rt_put(rt);
1223e5ed6399SHerbert Xu 		if (__in_dev_get_rtnl(dev) == NULL)
12241da177e4SLinus Torvalds 			return -EADDRNOTAVAIL;
12251da177e4SLinus Torvalds 		t->mlink = dev->ifindex;
1226e5ed6399SHerbert Xu 		ip_mc_inc_group(__in_dev_get_rtnl(dev), t->parms.iph.daddr);
12271da177e4SLinus Torvalds 	}
12281da177e4SLinus Torvalds 	return 0;
12291da177e4SLinus Torvalds }
12301da177e4SLinus Torvalds 
12311da177e4SLinus Torvalds static int ipgre_close(struct net_device *dev)
12321da177e4SLinus Torvalds {
12332941a486SPatrick McHardy 	struct ip_tunnel *t = netdev_priv(dev);
1234b8c26a33SStephen Hemminger 
1235f97c1e0cSJoe Perches 	if (ipv4_is_multicast(t->parms.iph.daddr) && t->mlink) {
12367fee0ca2SDenis V. Lunev 		struct in_device *in_dev;
1237c346dca1SYOSHIFUJI Hideaki 		in_dev = inetdev_by_index(dev_net(dev), t->mlink);
12388723e1b4SEric Dumazet 		if (in_dev)
12391da177e4SLinus Torvalds 			ip_mc_dec_group(in_dev, t->parms.iph.daddr);
12401da177e4SLinus Torvalds 	}
12411da177e4SLinus Torvalds 	return 0;
12421da177e4SLinus Torvalds }
12431da177e4SLinus Torvalds 
12441da177e4SLinus Torvalds #endif
12451da177e4SLinus Torvalds 
1246b8c26a33SStephen Hemminger static const struct net_device_ops ipgre_netdev_ops = {
1247b8c26a33SStephen Hemminger 	.ndo_init		= ipgre_tunnel_init,
1248b8c26a33SStephen Hemminger 	.ndo_uninit		= ipgre_tunnel_uninit,
1249b8c26a33SStephen Hemminger #ifdef CONFIG_NET_IPGRE_BROADCAST
1250b8c26a33SStephen Hemminger 	.ndo_open		= ipgre_open,
1251b8c26a33SStephen Hemminger 	.ndo_stop		= ipgre_close,
1252b8c26a33SStephen Hemminger #endif
1253b8c26a33SStephen Hemminger 	.ndo_start_xmit		= ipgre_tunnel_xmit,
1254b8c26a33SStephen Hemminger 	.ndo_do_ioctl		= ipgre_tunnel_ioctl,
1255b8c26a33SStephen Hemminger 	.ndo_change_mtu		= ipgre_tunnel_change_mtu,
1256e985aad7SEric Dumazet 	.ndo_get_stats		= ipgre_get_stats,
1257b8c26a33SStephen Hemminger };
1258b8c26a33SStephen Hemminger 
1259e985aad7SEric Dumazet static void ipgre_dev_free(struct net_device *dev)
1260e985aad7SEric Dumazet {
1261e985aad7SEric Dumazet 	free_percpu(dev->tstats);
1262e985aad7SEric Dumazet 	free_netdev(dev);
1263e985aad7SEric Dumazet }
1264e985aad7SEric Dumazet 
12651da177e4SLinus Torvalds static void ipgre_tunnel_setup(struct net_device *dev)
12661da177e4SLinus Torvalds {
1267b8c26a33SStephen Hemminger 	dev->netdev_ops		= &ipgre_netdev_ops;
1268e985aad7SEric Dumazet 	dev->destructor 	= ipgre_dev_free;
12691da177e4SLinus Torvalds 
12701da177e4SLinus Torvalds 	dev->type		= ARPHRD_IPGRE;
1271c95b819aSHerbert Xu 	dev->needed_headroom 	= LL_MAX_HEADER + sizeof(struct iphdr) + 4;
127246f25dffSKris Katterjohn 	dev->mtu		= ETH_DATA_LEN - sizeof(struct iphdr) - 4;
12731da177e4SLinus Torvalds 	dev->flags		= IFF_NOARP;
12741da177e4SLinus Torvalds 	dev->iflink		= 0;
12751da177e4SLinus Torvalds 	dev->addr_len		= 4;
12760b67ecebSPavel Emelyanov 	dev->features		|= NETIF_F_NETNS_LOCAL;
1277108bfa89SEric Dumazet 	dev->priv_flags		&= ~IFF_XMIT_DST_RELEASE;
12781da177e4SLinus Torvalds }
12791da177e4SLinus Torvalds 
12801da177e4SLinus Torvalds static int ipgre_tunnel_init(struct net_device *dev)
12811da177e4SLinus Torvalds {
12821da177e4SLinus Torvalds 	struct ip_tunnel *tunnel;
12831da177e4SLinus Torvalds 	struct iphdr *iph;
12841da177e4SLinus Torvalds 
12852941a486SPatrick McHardy 	tunnel = netdev_priv(dev);
12861da177e4SLinus Torvalds 	iph = &tunnel->parms.iph;
12871da177e4SLinus Torvalds 
12881da177e4SLinus Torvalds 	tunnel->dev = dev;
12891da177e4SLinus Torvalds 	strcpy(tunnel->parms.name, dev->name);
12901da177e4SLinus Torvalds 
12911da177e4SLinus Torvalds 	memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
12921da177e4SLinus Torvalds 	memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
12931da177e4SLinus Torvalds 
12941da177e4SLinus Torvalds 	if (iph->daddr) {
12951da177e4SLinus Torvalds #ifdef CONFIG_NET_IPGRE_BROADCAST
1296f97c1e0cSJoe Perches 		if (ipv4_is_multicast(iph->daddr)) {
12971da177e4SLinus Torvalds 			if (!iph->saddr)
12981da177e4SLinus Torvalds 				return -EINVAL;
12991da177e4SLinus Torvalds 			dev->flags = IFF_BROADCAST;
13003b04dddeSStephen Hemminger 			dev->header_ops = &ipgre_header_ops;
13011da177e4SLinus Torvalds 		}
13021da177e4SLinus Torvalds #endif
1303ee34c1ebSMichal Schmidt 	} else
13046a5f44d7STimo Teras 		dev->header_ops = &ipgre_header_ops;
13051da177e4SLinus Torvalds 
1306e985aad7SEric Dumazet 	dev->tstats = alloc_percpu(struct pcpu_tstats);
1307e985aad7SEric Dumazet 	if (!dev->tstats)
1308e985aad7SEric Dumazet 		return -ENOMEM;
1309e985aad7SEric Dumazet 
13101da177e4SLinus Torvalds 	return 0;
13111da177e4SLinus Torvalds }
13121da177e4SLinus Torvalds 
1313b8c26a33SStephen Hemminger static void ipgre_fb_tunnel_init(struct net_device *dev)
13141da177e4SLinus Torvalds {
13152941a486SPatrick McHardy 	struct ip_tunnel *tunnel = netdev_priv(dev);
13161da177e4SLinus Torvalds 	struct iphdr *iph = &tunnel->parms.iph;
13171da177e4SLinus Torvalds 
13181da177e4SLinus Torvalds 	tunnel->dev = dev;
13191da177e4SLinus Torvalds 	strcpy(tunnel->parms.name, dev->name);
13201da177e4SLinus Torvalds 
13211da177e4SLinus Torvalds 	iph->version		= 4;
13221da177e4SLinus Torvalds 	iph->protocol		= IPPROTO_GRE;
13231da177e4SLinus Torvalds 	iph->ihl		= 5;
13241da177e4SLinus Torvalds 	tunnel->hlen		= sizeof(struct iphdr) + 4;
13251da177e4SLinus Torvalds 
13261da177e4SLinus Torvalds 	dev_hold(dev);
13271da177e4SLinus Torvalds }
13281da177e4SLinus Torvalds 
13291da177e4SLinus Torvalds 
133000959adeSDmitry Kozlov static const struct gre_protocol ipgre_protocol = {
13311da177e4SLinus Torvalds 	.handler     = ipgre_rcv,
13321da177e4SLinus Torvalds 	.err_handler = ipgre_err,
13331da177e4SLinus Torvalds };
13341da177e4SLinus Torvalds 
1335eef6dd65SEric Dumazet static void ipgre_destroy_tunnels(struct ipgre_net *ign, struct list_head *head)
1336eb8ce741SPavel Emelyanov {
1337eb8ce741SPavel Emelyanov 	int prio;
1338eb8ce741SPavel Emelyanov 
1339eb8ce741SPavel Emelyanov 	for (prio = 0; prio < 4; prio++) {
1340eb8ce741SPavel Emelyanov 		int h;
1341eb8ce741SPavel Emelyanov 		for (h = 0; h < HASH_SIZE; h++) {
13421507850bSEric Dumazet 			struct ip_tunnel *t;
13431507850bSEric Dumazet 
13441507850bSEric Dumazet 			t = rtnl_dereference(ign->tunnels[prio][h]);
1345eef6dd65SEric Dumazet 
1346eef6dd65SEric Dumazet 			while (t != NULL) {
1347eef6dd65SEric Dumazet 				unregister_netdevice_queue(t->dev, head);
13481507850bSEric Dumazet 				t = rtnl_dereference(t->next);
1349eef6dd65SEric Dumazet 			}
1350eb8ce741SPavel Emelyanov 		}
1351eb8ce741SPavel Emelyanov 	}
1352eb8ce741SPavel Emelyanov }
1353eb8ce741SPavel Emelyanov 
13542c8c1e72SAlexey Dobriyan static int __net_init ipgre_init_net(struct net *net)
135559a4c759SPavel Emelyanov {
1356cfb8fbf2SEric W. Biederman 	struct ipgre_net *ign = net_generic(net, ipgre_net_id);
135759a4c759SPavel Emelyanov 	int err;
135859a4c759SPavel Emelyanov 
13597daa0004SPavel Emelyanov 	ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "gre0",
13607daa0004SPavel Emelyanov 					   ipgre_tunnel_setup);
13617daa0004SPavel Emelyanov 	if (!ign->fb_tunnel_dev) {
13627daa0004SPavel Emelyanov 		err = -ENOMEM;
13637daa0004SPavel Emelyanov 		goto err_alloc_dev;
13647daa0004SPavel Emelyanov 	}
1365be77e593SAlexey Dobriyan 	dev_net_set(ign->fb_tunnel_dev, net);
13667daa0004SPavel Emelyanov 
1367b8c26a33SStephen Hemminger 	ipgre_fb_tunnel_init(ign->fb_tunnel_dev);
1368c19e654dSHerbert Xu 	ign->fb_tunnel_dev->rtnl_link_ops = &ipgre_link_ops;
13697daa0004SPavel Emelyanov 
13707daa0004SPavel Emelyanov 	if ((err = register_netdev(ign->fb_tunnel_dev)))
13717daa0004SPavel Emelyanov 		goto err_reg_dev;
13727daa0004SPavel Emelyanov 
13733285ee3bSEric Dumazet 	rcu_assign_pointer(ign->tunnels_wc[0],
13743285ee3bSEric Dumazet 			   netdev_priv(ign->fb_tunnel_dev));
137559a4c759SPavel Emelyanov 	return 0;
137659a4c759SPavel Emelyanov 
13777daa0004SPavel Emelyanov err_reg_dev:
13783285ee3bSEric Dumazet 	ipgre_dev_free(ign->fb_tunnel_dev);
13797daa0004SPavel Emelyanov err_alloc_dev:
138059a4c759SPavel Emelyanov 	return err;
138159a4c759SPavel Emelyanov }
138259a4c759SPavel Emelyanov 
13832c8c1e72SAlexey Dobriyan static void __net_exit ipgre_exit_net(struct net *net)
138459a4c759SPavel Emelyanov {
138559a4c759SPavel Emelyanov 	struct ipgre_net *ign;
1386eef6dd65SEric Dumazet 	LIST_HEAD(list);
138759a4c759SPavel Emelyanov 
138859a4c759SPavel Emelyanov 	ign = net_generic(net, ipgre_net_id);
13897daa0004SPavel Emelyanov 	rtnl_lock();
1390eef6dd65SEric Dumazet 	ipgre_destroy_tunnels(ign, &list);
1391eef6dd65SEric Dumazet 	unregister_netdevice_many(&list);
13927daa0004SPavel Emelyanov 	rtnl_unlock();
139359a4c759SPavel Emelyanov }
139459a4c759SPavel Emelyanov 
139559a4c759SPavel Emelyanov static struct pernet_operations ipgre_net_ops = {
139659a4c759SPavel Emelyanov 	.init = ipgre_init_net,
139759a4c759SPavel Emelyanov 	.exit = ipgre_exit_net,
1398cfb8fbf2SEric W. Biederman 	.id   = &ipgre_net_id,
1399cfb8fbf2SEric W. Biederman 	.size = sizeof(struct ipgre_net),
140059a4c759SPavel Emelyanov };
14011da177e4SLinus Torvalds 
1402c19e654dSHerbert Xu static int ipgre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[])
1403c19e654dSHerbert Xu {
1404c19e654dSHerbert Xu 	__be16 flags;
1405c19e654dSHerbert Xu 
1406c19e654dSHerbert Xu 	if (!data)
1407c19e654dSHerbert Xu 		return 0;
1408c19e654dSHerbert Xu 
1409c19e654dSHerbert Xu 	flags = 0;
1410c19e654dSHerbert Xu 	if (data[IFLA_GRE_IFLAGS])
1411c19e654dSHerbert Xu 		flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1412c19e654dSHerbert Xu 	if (data[IFLA_GRE_OFLAGS])
1413c19e654dSHerbert Xu 		flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1414c19e654dSHerbert Xu 	if (flags & (GRE_VERSION|GRE_ROUTING))
1415c19e654dSHerbert Xu 		return -EINVAL;
1416c19e654dSHerbert Xu 
1417c19e654dSHerbert Xu 	return 0;
1418c19e654dSHerbert Xu }
1419c19e654dSHerbert Xu 
1420e1a80002SHerbert Xu static int ipgre_tap_validate(struct nlattr *tb[], struct nlattr *data[])
1421e1a80002SHerbert Xu {
1422e1a80002SHerbert Xu 	__be32 daddr;
1423e1a80002SHerbert Xu 
1424e1a80002SHerbert Xu 	if (tb[IFLA_ADDRESS]) {
1425e1a80002SHerbert Xu 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1426e1a80002SHerbert Xu 			return -EINVAL;
1427e1a80002SHerbert Xu 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1428e1a80002SHerbert Xu 			return -EADDRNOTAVAIL;
1429e1a80002SHerbert Xu 	}
1430e1a80002SHerbert Xu 
1431e1a80002SHerbert Xu 	if (!data)
1432e1a80002SHerbert Xu 		goto out;
1433e1a80002SHerbert Xu 
1434e1a80002SHerbert Xu 	if (data[IFLA_GRE_REMOTE]) {
1435e1a80002SHerbert Xu 		memcpy(&daddr, nla_data(data[IFLA_GRE_REMOTE]), 4);
1436e1a80002SHerbert Xu 		if (!daddr)
1437e1a80002SHerbert Xu 			return -EINVAL;
1438e1a80002SHerbert Xu 	}
1439e1a80002SHerbert Xu 
1440e1a80002SHerbert Xu out:
1441e1a80002SHerbert Xu 	return ipgre_tunnel_validate(tb, data);
1442e1a80002SHerbert Xu }
1443e1a80002SHerbert Xu 
1444c19e654dSHerbert Xu static void ipgre_netlink_parms(struct nlattr *data[],
1445c19e654dSHerbert Xu 				struct ip_tunnel_parm *parms)
1446c19e654dSHerbert Xu {
14477bb82d92SHerbert Xu 	memset(parms, 0, sizeof(*parms));
1448c19e654dSHerbert Xu 
1449c19e654dSHerbert Xu 	parms->iph.protocol = IPPROTO_GRE;
1450c19e654dSHerbert Xu 
1451c19e654dSHerbert Xu 	if (!data)
1452c19e654dSHerbert Xu 		return;
1453c19e654dSHerbert Xu 
1454c19e654dSHerbert Xu 	if (data[IFLA_GRE_LINK])
1455c19e654dSHerbert Xu 		parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
1456c19e654dSHerbert Xu 
1457c19e654dSHerbert Xu 	if (data[IFLA_GRE_IFLAGS])
1458c19e654dSHerbert Xu 		parms->i_flags = nla_get_be16(data[IFLA_GRE_IFLAGS]);
1459c19e654dSHerbert Xu 
1460c19e654dSHerbert Xu 	if (data[IFLA_GRE_OFLAGS])
1461c19e654dSHerbert Xu 		parms->o_flags = nla_get_be16(data[IFLA_GRE_OFLAGS]);
1462c19e654dSHerbert Xu 
1463c19e654dSHerbert Xu 	if (data[IFLA_GRE_IKEY])
1464c19e654dSHerbert Xu 		parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
1465c19e654dSHerbert Xu 
1466c19e654dSHerbert Xu 	if (data[IFLA_GRE_OKEY])
1467c19e654dSHerbert Xu 		parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
1468c19e654dSHerbert Xu 
1469c19e654dSHerbert Xu 	if (data[IFLA_GRE_LOCAL])
14704d74f8baSPatrick McHardy 		parms->iph.saddr = nla_get_be32(data[IFLA_GRE_LOCAL]);
1471c19e654dSHerbert Xu 
1472c19e654dSHerbert Xu 	if (data[IFLA_GRE_REMOTE])
14734d74f8baSPatrick McHardy 		parms->iph.daddr = nla_get_be32(data[IFLA_GRE_REMOTE]);
1474c19e654dSHerbert Xu 
1475c19e654dSHerbert Xu 	if (data[IFLA_GRE_TTL])
1476c19e654dSHerbert Xu 		parms->iph.ttl = nla_get_u8(data[IFLA_GRE_TTL]);
1477c19e654dSHerbert Xu 
1478c19e654dSHerbert Xu 	if (data[IFLA_GRE_TOS])
1479c19e654dSHerbert Xu 		parms->iph.tos = nla_get_u8(data[IFLA_GRE_TOS]);
1480c19e654dSHerbert Xu 
1481c19e654dSHerbert Xu 	if (!data[IFLA_GRE_PMTUDISC] || nla_get_u8(data[IFLA_GRE_PMTUDISC]))
1482c19e654dSHerbert Xu 		parms->iph.frag_off = htons(IP_DF);
1483c19e654dSHerbert Xu }
1484c19e654dSHerbert Xu 
1485e1a80002SHerbert Xu static int ipgre_tap_init(struct net_device *dev)
1486e1a80002SHerbert Xu {
1487e1a80002SHerbert Xu 	struct ip_tunnel *tunnel;
1488e1a80002SHerbert Xu 
1489e1a80002SHerbert Xu 	tunnel = netdev_priv(dev);
1490e1a80002SHerbert Xu 
1491e1a80002SHerbert Xu 	tunnel->dev = dev;
1492e1a80002SHerbert Xu 	strcpy(tunnel->parms.name, dev->name);
1493e1a80002SHerbert Xu 
1494e1a80002SHerbert Xu 	ipgre_tunnel_bind_dev(dev);
1495e1a80002SHerbert Xu 
1496e985aad7SEric Dumazet 	dev->tstats = alloc_percpu(struct pcpu_tstats);
1497e985aad7SEric Dumazet 	if (!dev->tstats)
1498e985aad7SEric Dumazet 		return -ENOMEM;
1499e985aad7SEric Dumazet 
1500e1a80002SHerbert Xu 	return 0;
1501e1a80002SHerbert Xu }
1502e1a80002SHerbert Xu 
1503b8c26a33SStephen Hemminger static const struct net_device_ops ipgre_tap_netdev_ops = {
1504b8c26a33SStephen Hemminger 	.ndo_init		= ipgre_tap_init,
1505b8c26a33SStephen Hemminger 	.ndo_uninit		= ipgre_tunnel_uninit,
1506b8c26a33SStephen Hemminger 	.ndo_start_xmit		= ipgre_tunnel_xmit,
1507b8c26a33SStephen Hemminger 	.ndo_set_mac_address 	= eth_mac_addr,
1508b8c26a33SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
1509b8c26a33SStephen Hemminger 	.ndo_change_mtu		= ipgre_tunnel_change_mtu,
1510e985aad7SEric Dumazet 	.ndo_get_stats		= ipgre_get_stats,
1511b8c26a33SStephen Hemminger };
1512b8c26a33SStephen Hemminger 
1513e1a80002SHerbert Xu static void ipgre_tap_setup(struct net_device *dev)
1514e1a80002SHerbert Xu {
1515e1a80002SHerbert Xu 
1516e1a80002SHerbert Xu 	ether_setup(dev);
1517e1a80002SHerbert Xu 
15182e9526b3SHerbert Xu 	dev->netdev_ops		= &ipgre_tap_netdev_ops;
1519e985aad7SEric Dumazet 	dev->destructor 	= ipgre_dev_free;
1520e1a80002SHerbert Xu 
1521e1a80002SHerbert Xu 	dev->iflink		= 0;
1522e1a80002SHerbert Xu 	dev->features		|= NETIF_F_NETNS_LOCAL;
1523e1a80002SHerbert Xu }
1524e1a80002SHerbert Xu 
152581adee47SEric W. Biederman static int ipgre_newlink(struct net *src_net, struct net_device *dev, struct nlattr *tb[],
1526c19e654dSHerbert Xu 			 struct nlattr *data[])
1527c19e654dSHerbert Xu {
1528c19e654dSHerbert Xu 	struct ip_tunnel *nt;
1529c19e654dSHerbert Xu 	struct net *net = dev_net(dev);
1530c19e654dSHerbert Xu 	struct ipgre_net *ign = net_generic(net, ipgre_net_id);
1531c19e654dSHerbert Xu 	int mtu;
1532c19e654dSHerbert Xu 	int err;
1533c19e654dSHerbert Xu 
1534c19e654dSHerbert Xu 	nt = netdev_priv(dev);
1535c19e654dSHerbert Xu 	ipgre_netlink_parms(data, &nt->parms);
1536c19e654dSHerbert Xu 
1537e1a80002SHerbert Xu 	if (ipgre_tunnel_find(net, &nt->parms, dev->type))
1538c19e654dSHerbert Xu 		return -EEXIST;
1539c19e654dSHerbert Xu 
1540e1a80002SHerbert Xu 	if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
1541f2cedb63SDanny Kukawka 		eth_hw_addr_random(dev);
1542e1a80002SHerbert Xu 
1543c19e654dSHerbert Xu 	mtu = ipgre_tunnel_bind_dev(dev);
1544c19e654dSHerbert Xu 	if (!tb[IFLA_MTU])
1545c19e654dSHerbert Xu 		dev->mtu = mtu;
1546c19e654dSHerbert Xu 
1547b790e01aSEric Dumazet 	/* Can use a lockless transmit, unless we generate output sequences */
1548b790e01aSEric Dumazet 	if (!(nt->parms.o_flags & GRE_SEQ))
1549b790e01aSEric Dumazet 		dev->features |= NETIF_F_LLTX;
1550b790e01aSEric Dumazet 
1551c19e654dSHerbert Xu 	err = register_netdevice(dev);
1552c19e654dSHerbert Xu 	if (err)
1553c19e654dSHerbert Xu 		goto out;
1554c19e654dSHerbert Xu 
1555c19e654dSHerbert Xu 	dev_hold(dev);
1556c19e654dSHerbert Xu 	ipgre_tunnel_link(ign, nt);
1557c19e654dSHerbert Xu 
1558c19e654dSHerbert Xu out:
1559c19e654dSHerbert Xu 	return err;
1560c19e654dSHerbert Xu }
1561c19e654dSHerbert Xu 
1562c19e654dSHerbert Xu static int ipgre_changelink(struct net_device *dev, struct nlattr *tb[],
1563c19e654dSHerbert Xu 			    struct nlattr *data[])
1564c19e654dSHerbert Xu {
1565c19e654dSHerbert Xu 	struct ip_tunnel *t, *nt;
1566c19e654dSHerbert Xu 	struct net *net = dev_net(dev);
1567c19e654dSHerbert Xu 	struct ipgre_net *ign = net_generic(net, ipgre_net_id);
1568c19e654dSHerbert Xu 	struct ip_tunnel_parm p;
1569c19e654dSHerbert Xu 	int mtu;
1570c19e654dSHerbert Xu 
1571c19e654dSHerbert Xu 	if (dev == ign->fb_tunnel_dev)
1572c19e654dSHerbert Xu 		return -EINVAL;
1573c19e654dSHerbert Xu 
1574c19e654dSHerbert Xu 	nt = netdev_priv(dev);
1575c19e654dSHerbert Xu 	ipgre_netlink_parms(data, &p);
1576c19e654dSHerbert Xu 
1577c19e654dSHerbert Xu 	t = ipgre_tunnel_locate(net, &p, 0);
1578c19e654dSHerbert Xu 
1579c19e654dSHerbert Xu 	if (t) {
1580c19e654dSHerbert Xu 		if (t->dev != dev)
1581c19e654dSHerbert Xu 			return -EEXIST;
1582c19e654dSHerbert Xu 	} else {
1583c19e654dSHerbert Xu 		t = nt;
1584c19e654dSHerbert Xu 
15852e9526b3SHerbert Xu 		if (dev->type != ARPHRD_ETHER) {
15861507850bSEric Dumazet 			unsigned int nflags = 0;
15872e9526b3SHerbert Xu 
1588c19e654dSHerbert Xu 			if (ipv4_is_multicast(p.iph.daddr))
1589c19e654dSHerbert Xu 				nflags = IFF_BROADCAST;
1590c19e654dSHerbert Xu 			else if (p.iph.daddr)
1591c19e654dSHerbert Xu 				nflags = IFF_POINTOPOINT;
1592c19e654dSHerbert Xu 
1593c19e654dSHerbert Xu 			if ((dev->flags ^ nflags) &
1594c19e654dSHerbert Xu 			    (IFF_POINTOPOINT | IFF_BROADCAST))
1595c19e654dSHerbert Xu 				return -EINVAL;
15962e9526b3SHerbert Xu 		}
1597c19e654dSHerbert Xu 
1598c19e654dSHerbert Xu 		ipgre_tunnel_unlink(ign, t);
1599c19e654dSHerbert Xu 		t->parms.iph.saddr = p.iph.saddr;
1600c19e654dSHerbert Xu 		t->parms.iph.daddr = p.iph.daddr;
1601c19e654dSHerbert Xu 		t->parms.i_key = p.i_key;
16022e9526b3SHerbert Xu 		if (dev->type != ARPHRD_ETHER) {
1603c19e654dSHerbert Xu 			memcpy(dev->dev_addr, &p.iph.saddr, 4);
1604c19e654dSHerbert Xu 			memcpy(dev->broadcast, &p.iph.daddr, 4);
16052e9526b3SHerbert Xu 		}
1606c19e654dSHerbert Xu 		ipgre_tunnel_link(ign, t);
1607c19e654dSHerbert Xu 		netdev_state_change(dev);
1608c19e654dSHerbert Xu 	}
1609c19e654dSHerbert Xu 
1610c19e654dSHerbert Xu 	t->parms.o_key = p.o_key;
1611c19e654dSHerbert Xu 	t->parms.iph.ttl = p.iph.ttl;
1612c19e654dSHerbert Xu 	t->parms.iph.tos = p.iph.tos;
1613c19e654dSHerbert Xu 	t->parms.iph.frag_off = p.iph.frag_off;
1614c19e654dSHerbert Xu 
1615c19e654dSHerbert Xu 	if (t->parms.link != p.link) {
1616c19e654dSHerbert Xu 		t->parms.link = p.link;
1617c19e654dSHerbert Xu 		mtu = ipgre_tunnel_bind_dev(dev);
1618c19e654dSHerbert Xu 		if (!tb[IFLA_MTU])
1619c19e654dSHerbert Xu 			dev->mtu = mtu;
1620c19e654dSHerbert Xu 		netdev_state_change(dev);
1621c19e654dSHerbert Xu 	}
1622c19e654dSHerbert Xu 
1623c19e654dSHerbert Xu 	return 0;
1624c19e654dSHerbert Xu }
1625c19e654dSHerbert Xu 
1626c19e654dSHerbert Xu static size_t ipgre_get_size(const struct net_device *dev)
1627c19e654dSHerbert Xu {
1628c19e654dSHerbert Xu 	return
1629c19e654dSHerbert Xu 		/* IFLA_GRE_LINK */
1630c19e654dSHerbert Xu 		nla_total_size(4) +
1631c19e654dSHerbert Xu 		/* IFLA_GRE_IFLAGS */
1632c19e654dSHerbert Xu 		nla_total_size(2) +
1633c19e654dSHerbert Xu 		/* IFLA_GRE_OFLAGS */
1634c19e654dSHerbert Xu 		nla_total_size(2) +
1635c19e654dSHerbert Xu 		/* IFLA_GRE_IKEY */
1636c19e654dSHerbert Xu 		nla_total_size(4) +
1637c19e654dSHerbert Xu 		/* IFLA_GRE_OKEY */
1638c19e654dSHerbert Xu 		nla_total_size(4) +
1639c19e654dSHerbert Xu 		/* IFLA_GRE_LOCAL */
1640c19e654dSHerbert Xu 		nla_total_size(4) +
1641c19e654dSHerbert Xu 		/* IFLA_GRE_REMOTE */
1642c19e654dSHerbert Xu 		nla_total_size(4) +
1643c19e654dSHerbert Xu 		/* IFLA_GRE_TTL */
1644c19e654dSHerbert Xu 		nla_total_size(1) +
1645c19e654dSHerbert Xu 		/* IFLA_GRE_TOS */
1646c19e654dSHerbert Xu 		nla_total_size(1) +
1647c19e654dSHerbert Xu 		/* IFLA_GRE_PMTUDISC */
1648c19e654dSHerbert Xu 		nla_total_size(1) +
1649c19e654dSHerbert Xu 		0;
1650c19e654dSHerbert Xu }
1651c19e654dSHerbert Xu 
1652c19e654dSHerbert Xu static int ipgre_fill_info(struct sk_buff *skb, const struct net_device *dev)
1653c19e654dSHerbert Xu {
1654c19e654dSHerbert Xu 	struct ip_tunnel *t = netdev_priv(dev);
1655c19e654dSHerbert Xu 	struct ip_tunnel_parm *p = &t->parms;
1656c19e654dSHerbert Xu 
1657*f3756b79SDavid S. Miller 	if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
1658*f3756b79SDavid S. Miller 	    nla_put_be16(skb, IFLA_GRE_IFLAGS, p->i_flags) ||
1659*f3756b79SDavid S. Miller 	    nla_put_be16(skb, IFLA_GRE_OFLAGS, p->o_flags) ||
1660*f3756b79SDavid S. Miller 	    nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
1661*f3756b79SDavid S. Miller 	    nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
1662*f3756b79SDavid S. Miller 	    nla_put_be32(skb, IFLA_GRE_LOCAL, p->iph.saddr) ||
1663*f3756b79SDavid S. Miller 	    nla_put_be32(skb, IFLA_GRE_REMOTE, p->iph.daddr) ||
1664*f3756b79SDavid S. Miller 	    nla_put_u8(skb, IFLA_GRE_TTL, p->iph.ttl) ||
1665*f3756b79SDavid S. Miller 	    nla_put_u8(skb, IFLA_GRE_TOS, p->iph.tos) ||
1666*f3756b79SDavid S. Miller 	    nla_put_u8(skb, IFLA_GRE_PMTUDISC,
1667*f3756b79SDavid S. Miller 		       !!(p->iph.frag_off & htons(IP_DF))))
1668*f3756b79SDavid S. Miller 		goto nla_put_failure;
1669c19e654dSHerbert Xu 	return 0;
1670c19e654dSHerbert Xu 
1671c19e654dSHerbert Xu nla_put_failure:
1672c19e654dSHerbert Xu 	return -EMSGSIZE;
1673c19e654dSHerbert Xu }
1674c19e654dSHerbert Xu 
1675c19e654dSHerbert Xu static const struct nla_policy ipgre_policy[IFLA_GRE_MAX + 1] = {
1676c19e654dSHerbert Xu 	[IFLA_GRE_LINK]		= { .type = NLA_U32 },
1677c19e654dSHerbert Xu 	[IFLA_GRE_IFLAGS]	= { .type = NLA_U16 },
1678c19e654dSHerbert Xu 	[IFLA_GRE_OFLAGS]	= { .type = NLA_U16 },
1679c19e654dSHerbert Xu 	[IFLA_GRE_IKEY]		= { .type = NLA_U32 },
1680c19e654dSHerbert Xu 	[IFLA_GRE_OKEY]		= { .type = NLA_U32 },
16814d74f8baSPatrick McHardy 	[IFLA_GRE_LOCAL]	= { .len = FIELD_SIZEOF(struct iphdr, saddr) },
16824d74f8baSPatrick McHardy 	[IFLA_GRE_REMOTE]	= { .len = FIELD_SIZEOF(struct iphdr, daddr) },
1683c19e654dSHerbert Xu 	[IFLA_GRE_TTL]		= { .type = NLA_U8 },
1684c19e654dSHerbert Xu 	[IFLA_GRE_TOS]		= { .type = NLA_U8 },
1685c19e654dSHerbert Xu 	[IFLA_GRE_PMTUDISC]	= { .type = NLA_U8 },
1686c19e654dSHerbert Xu };
1687c19e654dSHerbert Xu 
1688c19e654dSHerbert Xu static struct rtnl_link_ops ipgre_link_ops __read_mostly = {
1689c19e654dSHerbert Xu 	.kind		= "gre",
1690c19e654dSHerbert Xu 	.maxtype	= IFLA_GRE_MAX,
1691c19e654dSHerbert Xu 	.policy		= ipgre_policy,
1692c19e654dSHerbert Xu 	.priv_size	= sizeof(struct ip_tunnel),
1693c19e654dSHerbert Xu 	.setup		= ipgre_tunnel_setup,
1694c19e654dSHerbert Xu 	.validate	= ipgre_tunnel_validate,
1695c19e654dSHerbert Xu 	.newlink	= ipgre_newlink,
1696c19e654dSHerbert Xu 	.changelink	= ipgre_changelink,
1697c19e654dSHerbert Xu 	.get_size	= ipgre_get_size,
1698c19e654dSHerbert Xu 	.fill_info	= ipgre_fill_info,
1699c19e654dSHerbert Xu };
1700c19e654dSHerbert Xu 
1701e1a80002SHerbert Xu static struct rtnl_link_ops ipgre_tap_ops __read_mostly = {
1702e1a80002SHerbert Xu 	.kind		= "gretap",
1703e1a80002SHerbert Xu 	.maxtype	= IFLA_GRE_MAX,
1704e1a80002SHerbert Xu 	.policy		= ipgre_policy,
1705e1a80002SHerbert Xu 	.priv_size	= sizeof(struct ip_tunnel),
1706e1a80002SHerbert Xu 	.setup		= ipgre_tap_setup,
1707e1a80002SHerbert Xu 	.validate	= ipgre_tap_validate,
1708e1a80002SHerbert Xu 	.newlink	= ipgre_newlink,
1709e1a80002SHerbert Xu 	.changelink	= ipgre_changelink,
1710e1a80002SHerbert Xu 	.get_size	= ipgre_get_size,
1711e1a80002SHerbert Xu 	.fill_info	= ipgre_fill_info,
1712e1a80002SHerbert Xu };
1713e1a80002SHerbert Xu 
17141da177e4SLinus Torvalds /*
17151da177e4SLinus Torvalds  *	And now the modules code and kernel interface.
17161da177e4SLinus Torvalds  */
17171da177e4SLinus Torvalds 
17181da177e4SLinus Torvalds static int __init ipgre_init(void)
17191da177e4SLinus Torvalds {
17201da177e4SLinus Torvalds 	int err;
17211da177e4SLinus Torvalds 
1722058bd4d2SJoe Perches 	pr_info("GRE over IPv4 tunneling driver\n");
17231da177e4SLinus Torvalds 
1724cfb8fbf2SEric W. Biederman 	err = register_pernet_device(&ipgre_net_ops);
172559a4c759SPavel Emelyanov 	if (err < 0)
1726c2892f02SAlexey Dobriyan 		return err;
1727c2892f02SAlexey Dobriyan 
172800959adeSDmitry Kozlov 	err = gre_add_protocol(&ipgre_protocol, GREPROTO_CISCO);
1729c2892f02SAlexey Dobriyan 	if (err < 0) {
1730058bd4d2SJoe Perches 		pr_info("%s: can't add protocol\n", __func__);
1731c2892f02SAlexey Dobriyan 		goto add_proto_failed;
1732c2892f02SAlexey Dobriyan 	}
17337daa0004SPavel Emelyanov 
1734c19e654dSHerbert Xu 	err = rtnl_link_register(&ipgre_link_ops);
1735c19e654dSHerbert Xu 	if (err < 0)
1736c19e654dSHerbert Xu 		goto rtnl_link_failed;
1737c19e654dSHerbert Xu 
1738e1a80002SHerbert Xu 	err = rtnl_link_register(&ipgre_tap_ops);
1739e1a80002SHerbert Xu 	if (err < 0)
1740e1a80002SHerbert Xu 		goto tap_ops_failed;
1741e1a80002SHerbert Xu 
1742c19e654dSHerbert Xu out:
17437daa0004SPavel Emelyanov 	return err;
1744c19e654dSHerbert Xu 
1745e1a80002SHerbert Xu tap_ops_failed:
1746e1a80002SHerbert Xu 	rtnl_link_unregister(&ipgre_link_ops);
1747c19e654dSHerbert Xu rtnl_link_failed:
174800959adeSDmitry Kozlov 	gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO);
1749c2892f02SAlexey Dobriyan add_proto_failed:
1750c2892f02SAlexey Dobriyan 	unregister_pernet_device(&ipgre_net_ops);
1751c19e654dSHerbert Xu 	goto out;
17521da177e4SLinus Torvalds }
17531da177e4SLinus Torvalds 
1754db44575fSAlexey Kuznetsov static void __exit ipgre_fini(void)
17551da177e4SLinus Torvalds {
1756e1a80002SHerbert Xu 	rtnl_link_unregister(&ipgre_tap_ops);
1757c19e654dSHerbert Xu 	rtnl_link_unregister(&ipgre_link_ops);
175800959adeSDmitry Kozlov 	if (gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO) < 0)
1759058bd4d2SJoe Perches 		pr_info("%s: can't remove protocol\n", __func__);
1760c2892f02SAlexey Dobriyan 	unregister_pernet_device(&ipgre_net_ops);
17611da177e4SLinus Torvalds }
17621da177e4SLinus Torvalds 
17631da177e4SLinus Torvalds module_init(ipgre_init);
17641da177e4SLinus Torvalds module_exit(ipgre_fini);
17651da177e4SLinus Torvalds MODULE_LICENSE("GPL");
17664d74f8baSPatrick McHardy MODULE_ALIAS_RTNL_LINK("gre");
17674d74f8baSPatrick McHardy MODULE_ALIAS_RTNL_LINK("gretap");
17688909c9adSVasiliy Kulikov MODULE_ALIAS_NETDEV("gre0");
1769