xref: /linux/net/ipv4/ip_gre.c (revision cef401de7be8c4e155c6746bfccf721a4fa5fab9)
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 
123eccc1bb8Sstephen hemminger static bool log_ecn_error = true;
124eccc1bb8Sstephen hemminger module_param(log_ecn_error, bool, 0644);
125eccc1bb8Sstephen hemminger MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
126eccc1bb8Sstephen hemminger 
127c19e654dSHerbert Xu static struct rtnl_link_ops ipgre_link_ops __read_mostly;
1281da177e4SLinus Torvalds static int ipgre_tunnel_init(struct net_device *dev);
1291da177e4SLinus Torvalds static void ipgre_tunnel_setup(struct net_device *dev);
13042aa9162SHerbert Xu static int ipgre_tunnel_bind_dev(struct net_device *dev);
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds /* Fallback tunnel: no source, no destination, no key, no options */
1331da177e4SLinus Torvalds 
134eb8ce741SPavel Emelyanov #define HASH_SIZE  16
135eb8ce741SPavel Emelyanov 
136f99189b1SEric Dumazet static int ipgre_net_id __read_mostly;
13759a4c759SPavel Emelyanov struct ipgre_net {
1381507850bSEric Dumazet 	struct ip_tunnel __rcu *tunnels[4][HASH_SIZE];
139eb8ce741SPavel Emelyanov 
1407daa0004SPavel Emelyanov 	struct net_device *fb_tunnel_dev;
14159a4c759SPavel Emelyanov };
14259a4c759SPavel Emelyanov 
1431da177e4SLinus Torvalds /* Tunnel hash table */
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds /*
1461da177e4SLinus Torvalds    4 hash tables:
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds    3: (remote,local)
1491da177e4SLinus Torvalds    2: (remote,*)
1501da177e4SLinus Torvalds    1: (*,local)
1511da177e4SLinus Torvalds    0: (*,*)
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds    We require exact key match i.e. if a key is present in packet
1541da177e4SLinus Torvalds    it will match only tunnel with the same key; if it is not present,
1551da177e4SLinus Torvalds    it will match only keyless tunnel.
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds    All keysless packets, if not matched configured keyless tunnels
1581da177e4SLinus Torvalds    will match fallback tunnel.
1591da177e4SLinus Torvalds  */
1601da177e4SLinus Torvalds 
161d5a0a1e3SAl Viro #define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&0xF)
1621da177e4SLinus Torvalds 
163eb8ce741SPavel Emelyanov #define tunnels_r_l	tunnels[3]
164eb8ce741SPavel Emelyanov #define tunnels_r	tunnels[2]
165eb8ce741SPavel Emelyanov #define tunnels_l	tunnels[1]
166eb8ce741SPavel Emelyanov #define tunnels_wc	tunnels[0]
1671da177e4SLinus Torvalds 
16887b6d218Sstephen hemminger static struct rtnl_link_stats64 *ipgre_get_stats64(struct net_device *dev,
16987b6d218Sstephen hemminger 						   struct rtnl_link_stats64 *tot)
170e985aad7SEric Dumazet {
171e985aad7SEric Dumazet 	int i;
172e985aad7SEric Dumazet 
173e985aad7SEric Dumazet 	for_each_possible_cpu(i) {
174e985aad7SEric Dumazet 		const struct pcpu_tstats *tstats = per_cpu_ptr(dev->tstats, i);
17587b6d218Sstephen hemminger 		u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
17687b6d218Sstephen hemminger 		unsigned int start;
177e985aad7SEric Dumazet 
17887b6d218Sstephen hemminger 		do {
17987b6d218Sstephen hemminger 			start = u64_stats_fetch_begin_bh(&tstats->syncp);
18087b6d218Sstephen hemminger 			rx_packets = tstats->rx_packets;
18187b6d218Sstephen hemminger 			tx_packets = tstats->tx_packets;
18287b6d218Sstephen hemminger 			rx_bytes = tstats->rx_bytes;
18387b6d218Sstephen hemminger 			tx_bytes = tstats->tx_bytes;
18487b6d218Sstephen hemminger 		} while (u64_stats_fetch_retry_bh(&tstats->syncp, start));
18587b6d218Sstephen hemminger 
18687b6d218Sstephen hemminger 		tot->rx_packets += rx_packets;
18787b6d218Sstephen hemminger 		tot->tx_packets += tx_packets;
18887b6d218Sstephen hemminger 		tot->rx_bytes   += rx_bytes;
18987b6d218Sstephen hemminger 		tot->tx_bytes   += tx_bytes;
190e985aad7SEric Dumazet 	}
19187b6d218Sstephen hemminger 
19287b6d218Sstephen hemminger 	tot->multicast = dev->stats.multicast;
19387b6d218Sstephen hemminger 	tot->rx_crc_errors = dev->stats.rx_crc_errors;
19487b6d218Sstephen hemminger 	tot->rx_fifo_errors = dev->stats.rx_fifo_errors;
19587b6d218Sstephen hemminger 	tot->rx_length_errors = dev->stats.rx_length_errors;
196eccc1bb8Sstephen hemminger 	tot->rx_frame_errors = dev->stats.rx_frame_errors;
19787b6d218Sstephen hemminger 	tot->rx_errors = dev->stats.rx_errors;
198eccc1bb8Sstephen hemminger 
19987b6d218Sstephen hemminger 	tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
20087b6d218Sstephen hemminger 	tot->tx_carrier_errors = dev->stats.tx_carrier_errors;
20187b6d218Sstephen hemminger 	tot->tx_dropped = dev->stats.tx_dropped;
20287b6d218Sstephen hemminger 	tot->tx_aborted_errors = dev->stats.tx_aborted_errors;
20387b6d218Sstephen hemminger 	tot->tx_errors = dev->stats.tx_errors;
20487b6d218Sstephen hemminger 
20587b6d218Sstephen hemminger 	return tot;
206e985aad7SEric Dumazet }
207e985aad7SEric Dumazet 
208d2083287Sstephen hemminger /* Does key in tunnel parameters match packet */
209d2083287Sstephen hemminger static bool ipgre_key_match(const struct ip_tunnel_parm *p,
2109fbef059Sstephen hemminger 			    __be16 flags, __be32 key)
211d2083287Sstephen hemminger {
212d2083287Sstephen hemminger 	if (p->i_flags & GRE_KEY) {
213d2083287Sstephen hemminger 		if (flags & GRE_KEY)
214d2083287Sstephen hemminger 			return key == p->i_key;
215d2083287Sstephen hemminger 		else
216d2083287Sstephen hemminger 			return false;	/* key expected, none present */
217d2083287Sstephen hemminger 	} else
218d2083287Sstephen hemminger 		return !(flags & GRE_KEY);
219d2083287Sstephen hemminger }
220d2083287Sstephen hemminger 
2211da177e4SLinus Torvalds /* Given src, dst and key, find appropriate for input tunnel. */
2221da177e4SLinus Torvalds 
223749c10f9STimo Teras static struct ip_tunnel *ipgre_tunnel_lookup(struct net_device *dev,
224e1a80002SHerbert Xu 					     __be32 remote, __be32 local,
2259fbef059Sstephen hemminger 					     __be16 flags, __be32 key,
226d2083287Sstephen hemminger 					     __be16 gre_proto)
2271da177e4SLinus Torvalds {
228749c10f9STimo Teras 	struct net *net = dev_net(dev);
229749c10f9STimo Teras 	int link = dev->ifindex;
2301507850bSEric Dumazet 	unsigned int h0 = HASH(remote);
2311507850bSEric Dumazet 	unsigned int h1 = HASH(key);
232afcf1242STimo Teras 	struct ip_tunnel *t, *cand = NULL;
2337daa0004SPavel Emelyanov 	struct ipgre_net *ign = net_generic(net, ipgre_net_id);
234e1a80002SHerbert Xu 	int dev_type = (gre_proto == htons(ETH_P_TEB)) ?
235e1a80002SHerbert Xu 		       ARPHRD_ETHER : ARPHRD_IPGRE;
236afcf1242STimo Teras 	int score, cand_score = 4;
2371da177e4SLinus Torvalds 
238e086cadcSAmerigo Wang 	for_each_ip_tunnel_rcu(t, ign->tunnels_r_l[h0 ^ h1]) {
239749c10f9STimo Teras 		if (local != t->parms.iph.saddr ||
240749c10f9STimo Teras 		    remote != t->parms.iph.daddr ||
241749c10f9STimo Teras 		    !(t->dev->flags & IFF_UP))
242749c10f9STimo Teras 			continue;
243749c10f9STimo Teras 
244d2083287Sstephen hemminger 		if (!ipgre_key_match(&t->parms, flags, key))
245d2083287Sstephen hemminger 			continue;
246d2083287Sstephen hemminger 
247749c10f9STimo Teras 		if (t->dev->type != ARPHRD_IPGRE &&
248749c10f9STimo Teras 		    t->dev->type != dev_type)
249749c10f9STimo Teras 			continue;
250749c10f9STimo Teras 
251afcf1242STimo Teras 		score = 0;
252749c10f9STimo Teras 		if (t->parms.link != link)
253afcf1242STimo Teras 			score |= 1;
254749c10f9STimo Teras 		if (t->dev->type != dev_type)
255afcf1242STimo Teras 			score |= 2;
256afcf1242STimo Teras 		if (score == 0)
2571da177e4SLinus Torvalds 			return t;
258afcf1242STimo Teras 
259afcf1242STimo Teras 		if (score < cand_score) {
260afcf1242STimo Teras 			cand = t;
261afcf1242STimo Teras 			cand_score = score;
262afcf1242STimo Teras 		}
263e1a80002SHerbert Xu 	}
264e1a80002SHerbert Xu 
265e086cadcSAmerigo Wang 	for_each_ip_tunnel_rcu(t, ign->tunnels_r[h0 ^ h1]) {
266749c10f9STimo Teras 		if (remote != t->parms.iph.daddr ||
267749c10f9STimo Teras 		    !(t->dev->flags & IFF_UP))
268749c10f9STimo Teras 			continue;
269749c10f9STimo Teras 
270d2083287Sstephen hemminger 		if (!ipgre_key_match(&t->parms, flags, key))
271d2083287Sstephen hemminger 			continue;
272d2083287Sstephen hemminger 
273749c10f9STimo Teras 		if (t->dev->type != ARPHRD_IPGRE &&
274749c10f9STimo Teras 		    t->dev->type != dev_type)
275749c10f9STimo Teras 			continue;
276749c10f9STimo Teras 
277afcf1242STimo Teras 		score = 0;
278749c10f9STimo Teras 		if (t->parms.link != link)
279afcf1242STimo Teras 			score |= 1;
280749c10f9STimo Teras 		if (t->dev->type != dev_type)
281afcf1242STimo Teras 			score |= 2;
282afcf1242STimo Teras 		if (score == 0)
2831da177e4SLinus Torvalds 			return t;
284afcf1242STimo Teras 
285afcf1242STimo Teras 		if (score < cand_score) {
286afcf1242STimo Teras 			cand = t;
287afcf1242STimo Teras 			cand_score = score;
288afcf1242STimo Teras 		}
289e1a80002SHerbert Xu 	}
290e1a80002SHerbert Xu 
291e086cadcSAmerigo Wang 	for_each_ip_tunnel_rcu(t, ign->tunnels_l[h1]) {
292749c10f9STimo Teras 		if ((local != t->parms.iph.saddr &&
293749c10f9STimo Teras 		     (local != t->parms.iph.daddr ||
294749c10f9STimo Teras 		      !ipv4_is_multicast(local))) ||
295749c10f9STimo Teras 		    !(t->dev->flags & IFF_UP))
296749c10f9STimo Teras 			continue;
297749c10f9STimo Teras 
298d2083287Sstephen hemminger 		if (!ipgre_key_match(&t->parms, flags, key))
299d2083287Sstephen hemminger 			continue;
300d2083287Sstephen hemminger 
301749c10f9STimo Teras 		if (t->dev->type != ARPHRD_IPGRE &&
302749c10f9STimo Teras 		    t->dev->type != dev_type)
303749c10f9STimo Teras 			continue;
304749c10f9STimo Teras 
305afcf1242STimo Teras 		score = 0;
306749c10f9STimo Teras 		if (t->parms.link != link)
307afcf1242STimo Teras 			score |= 1;
308749c10f9STimo Teras 		if (t->dev->type != dev_type)
309afcf1242STimo Teras 			score |= 2;
310afcf1242STimo Teras 		if (score == 0)
3111da177e4SLinus Torvalds 			return t;
312afcf1242STimo Teras 
313afcf1242STimo Teras 		if (score < cand_score) {
314afcf1242STimo Teras 			cand = t;
315afcf1242STimo Teras 			cand_score = score;
316afcf1242STimo Teras 		}
317e1a80002SHerbert Xu 	}
318e1a80002SHerbert Xu 
319e086cadcSAmerigo Wang 	for_each_ip_tunnel_rcu(t, ign->tunnels_wc[h1]) {
320749c10f9STimo Teras 		if (t->parms.i_key != key ||
321749c10f9STimo Teras 		    !(t->dev->flags & IFF_UP))
322749c10f9STimo Teras 			continue;
323749c10f9STimo Teras 
324749c10f9STimo Teras 		if (t->dev->type != ARPHRD_IPGRE &&
325749c10f9STimo Teras 		    t->dev->type != dev_type)
326749c10f9STimo Teras 			continue;
327749c10f9STimo Teras 
328afcf1242STimo Teras 		score = 0;
329749c10f9STimo Teras 		if (t->parms.link != link)
330afcf1242STimo Teras 			score |= 1;
331749c10f9STimo Teras 		if (t->dev->type != dev_type)
332afcf1242STimo Teras 			score |= 2;
333afcf1242STimo Teras 		if (score == 0)
3341da177e4SLinus Torvalds 			return t;
335afcf1242STimo Teras 
336afcf1242STimo Teras 		if (score < cand_score) {
337afcf1242STimo Teras 			cand = t;
338afcf1242STimo Teras 			cand_score = score;
339afcf1242STimo Teras 		}
340e1a80002SHerbert Xu 	}
341e1a80002SHerbert Xu 
342afcf1242STimo Teras 	if (cand != NULL)
343afcf1242STimo Teras 		return cand;
3441da177e4SLinus Torvalds 
3458d5b2c08SEric Dumazet 	dev = ign->fb_tunnel_dev;
3468d5b2c08SEric Dumazet 	if (dev->flags & IFF_UP)
3478d5b2c08SEric Dumazet 		return netdev_priv(dev);
348749c10f9STimo Teras 
3491da177e4SLinus Torvalds 	return NULL;
3501da177e4SLinus Torvalds }
3511da177e4SLinus Torvalds 
3521507850bSEric Dumazet static struct ip_tunnel __rcu **__ipgre_bucket(struct ipgre_net *ign,
353f57e7d5aSPavel Emelyanov 		struct ip_tunnel_parm *parms)
3541da177e4SLinus Torvalds {
3555056a1efSYOSHIFUJI Hideaki 	__be32 remote = parms->iph.daddr;
3565056a1efSYOSHIFUJI Hideaki 	__be32 local = parms->iph.saddr;
3575056a1efSYOSHIFUJI Hideaki 	__be32 key = parms->i_key;
3581507850bSEric Dumazet 	unsigned int h = HASH(key);
3591da177e4SLinus Torvalds 	int prio = 0;
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 	if (local)
3621da177e4SLinus Torvalds 		prio |= 1;
363f97c1e0cSJoe Perches 	if (remote && !ipv4_is_multicast(remote)) {
3641da177e4SLinus Torvalds 		prio |= 2;
3651da177e4SLinus Torvalds 		h ^= HASH(remote);
3661da177e4SLinus Torvalds 	}
3671da177e4SLinus Torvalds 
368eb8ce741SPavel Emelyanov 	return &ign->tunnels[prio][h];
3691da177e4SLinus Torvalds }
3701da177e4SLinus Torvalds 
3711507850bSEric Dumazet static inline struct ip_tunnel __rcu **ipgre_bucket(struct ipgre_net *ign,
372f57e7d5aSPavel Emelyanov 		struct ip_tunnel *t)
3735056a1efSYOSHIFUJI Hideaki {
374f57e7d5aSPavel Emelyanov 	return __ipgre_bucket(ign, &t->parms);
3755056a1efSYOSHIFUJI Hideaki }
3765056a1efSYOSHIFUJI Hideaki 
377f57e7d5aSPavel Emelyanov static void ipgre_tunnel_link(struct ipgre_net *ign, struct ip_tunnel *t)
3781da177e4SLinus Torvalds {
3791507850bSEric Dumazet 	struct ip_tunnel __rcu **tp = ipgre_bucket(ign, t);
3801da177e4SLinus Torvalds 
3811507850bSEric Dumazet 	rcu_assign_pointer(t->next, rtnl_dereference(*tp));
3828d5b2c08SEric Dumazet 	rcu_assign_pointer(*tp, t);
3831da177e4SLinus Torvalds }
3841da177e4SLinus Torvalds 
385f57e7d5aSPavel Emelyanov static void ipgre_tunnel_unlink(struct ipgre_net *ign, struct ip_tunnel *t)
3861da177e4SLinus Torvalds {
3871507850bSEric Dumazet 	struct ip_tunnel __rcu **tp;
3881507850bSEric Dumazet 	struct ip_tunnel *iter;
3891da177e4SLinus Torvalds 
3901507850bSEric Dumazet 	for (tp = ipgre_bucket(ign, t);
3911507850bSEric Dumazet 	     (iter = rtnl_dereference(*tp)) != NULL;
3921507850bSEric Dumazet 	     tp = &iter->next) {
3931507850bSEric Dumazet 		if (t == iter) {
3941507850bSEric Dumazet 			rcu_assign_pointer(*tp, t->next);
3951da177e4SLinus Torvalds 			break;
3961da177e4SLinus Torvalds 		}
3971da177e4SLinus Torvalds 	}
3981da177e4SLinus Torvalds }
3991da177e4SLinus Torvalds 
400e1a80002SHerbert Xu static struct ip_tunnel *ipgre_tunnel_find(struct net *net,
401e1a80002SHerbert Xu 					   struct ip_tunnel_parm *parms,
402e1a80002SHerbert Xu 					   int type)
4031da177e4SLinus Torvalds {
404d5a0a1e3SAl Viro 	__be32 remote = parms->iph.daddr;
405d5a0a1e3SAl Viro 	__be32 local = parms->iph.saddr;
406d5a0a1e3SAl Viro 	__be32 key = parms->i_key;
407749c10f9STimo Teras 	int link = parms->link;
4081507850bSEric Dumazet 	struct ip_tunnel *t;
4091507850bSEric Dumazet 	struct ip_tunnel __rcu **tp;
410e1a80002SHerbert Xu 	struct ipgre_net *ign = net_generic(net, ipgre_net_id);
411e1a80002SHerbert Xu 
4121507850bSEric Dumazet 	for (tp = __ipgre_bucket(ign, parms);
4131507850bSEric Dumazet 	     (t = rtnl_dereference(*tp)) != NULL;
4141507850bSEric Dumazet 	     tp = &t->next)
415e1a80002SHerbert Xu 		if (local == t->parms.iph.saddr &&
416e1a80002SHerbert Xu 		    remote == t->parms.iph.daddr &&
417e1a80002SHerbert Xu 		    key == t->parms.i_key &&
418749c10f9STimo Teras 		    link == t->parms.link &&
419e1a80002SHerbert Xu 		    type == t->dev->type)
420e1a80002SHerbert Xu 			break;
421e1a80002SHerbert Xu 
422e1a80002SHerbert Xu 	return t;
423e1a80002SHerbert Xu }
424e1a80002SHerbert Xu 
425e1a80002SHerbert Xu static struct ip_tunnel *ipgre_tunnel_locate(struct net *net,
426e1a80002SHerbert Xu 		struct ip_tunnel_parm *parms, int create)
427e1a80002SHerbert Xu {
428e1a80002SHerbert Xu 	struct ip_tunnel *t, *nt;
4291da177e4SLinus Torvalds 	struct net_device *dev;
4301da177e4SLinus Torvalds 	char name[IFNAMSIZ];
431f57e7d5aSPavel Emelyanov 	struct ipgre_net *ign = net_generic(net, ipgre_net_id);
4321da177e4SLinus Torvalds 
433e1a80002SHerbert Xu 	t = ipgre_tunnel_find(net, parms, ARPHRD_IPGRE);
434e1a80002SHerbert Xu 	if (t || !create)
4351da177e4SLinus Torvalds 		return t;
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds 	if (parms->name[0])
4381da177e4SLinus Torvalds 		strlcpy(name, parms->name, IFNAMSIZ);
43934cc7ba6SPavel Emelyanov 	else
440407d6fcbSstephen hemminger 		strcpy(name, "gre%d");
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds 	dev = alloc_netdev(sizeof(*t), name, ipgre_tunnel_setup);
4431da177e4SLinus Torvalds 	if (!dev)
4441da177e4SLinus Torvalds 		return NULL;
4451da177e4SLinus Torvalds 
4460b67ecebSPavel Emelyanov 	dev_net_set(dev, net);
4470b67ecebSPavel Emelyanov 
4482941a486SPatrick McHardy 	nt = netdev_priv(dev);
4491da177e4SLinus Torvalds 	nt->parms = *parms;
450c19e654dSHerbert Xu 	dev->rtnl_link_ops = &ipgre_link_ops;
4511da177e4SLinus Torvalds 
45242aa9162SHerbert Xu 	dev->mtu = ipgre_tunnel_bind_dev(dev);
45342aa9162SHerbert Xu 
454b37d428bSPavel Emelyanov 	if (register_netdevice(dev) < 0)
455b37d428bSPavel Emelyanov 		goto failed_free;
4561da177e4SLinus Torvalds 
457f2b3ee9eSWillem de Bruijn 	/* Can use a lockless transmit, unless we generate output sequences */
458f2b3ee9eSWillem de Bruijn 	if (!(nt->parms.o_flags & GRE_SEQ))
459f2b3ee9eSWillem de Bruijn 		dev->features |= NETIF_F_LLTX;
460f2b3ee9eSWillem de Bruijn 
4611da177e4SLinus Torvalds 	dev_hold(dev);
462f57e7d5aSPavel Emelyanov 	ipgre_tunnel_link(ign, nt);
4631da177e4SLinus Torvalds 	return nt;
4641da177e4SLinus Torvalds 
465b37d428bSPavel Emelyanov failed_free:
466b37d428bSPavel Emelyanov 	free_netdev(dev);
4671da177e4SLinus Torvalds 	return NULL;
4681da177e4SLinus Torvalds }
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds static void ipgre_tunnel_uninit(struct net_device *dev)
4711da177e4SLinus Torvalds {
472f57e7d5aSPavel Emelyanov 	struct net *net = dev_net(dev);
473f57e7d5aSPavel Emelyanov 	struct ipgre_net *ign = net_generic(net, ipgre_net_id);
474f57e7d5aSPavel Emelyanov 
475f57e7d5aSPavel Emelyanov 	ipgre_tunnel_unlink(ign, netdev_priv(dev));
4761da177e4SLinus Torvalds 	dev_put(dev);
4771da177e4SLinus Torvalds }
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds static void ipgre_err(struct sk_buff *skb, u32 info)
4811da177e4SLinus Torvalds {
4821da177e4SLinus Torvalds 
483071f92d0SRami Rosen /* All the routers (except for Linux) return only
4841da177e4SLinus Torvalds    8 bytes of packet payload. It means, that precise relaying of
4851da177e4SLinus Torvalds    ICMP in the real Internet is absolutely infeasible.
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds    Moreover, Cisco "wise men" put GRE key to the third word
4881da177e4SLinus Torvalds    in GRE header. It makes impossible maintaining even soft state for keyed
4891da177e4SLinus Torvalds    GRE tunnels with enabled checksum. Tell them "thank you".
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds    Well, I wonder, rfc1812 was written by Cisco employee,
492bff52857Sstephen hemminger    what the hell these idiots break standards established
493bff52857Sstephen hemminger    by themselves???
4941da177e4SLinus Torvalds  */
4951da177e4SLinus Torvalds 
496b71d1d42SEric Dumazet 	const struct iphdr *iph = (const struct iphdr *)skb->data;
497d5a0a1e3SAl Viro 	__be16	     *p = (__be16 *)(skb->data+(iph->ihl<<2));
4981da177e4SLinus Torvalds 	int grehlen = (iph->ihl<<2) + 4;
49988c7664fSArnaldo Carvalho de Melo 	const int type = icmp_hdr(skb)->type;
50088c7664fSArnaldo Carvalho de Melo 	const int code = icmp_hdr(skb)->code;
5011da177e4SLinus Torvalds 	struct ip_tunnel *t;
502d5a0a1e3SAl Viro 	__be16 flags;
503d2083287Sstephen hemminger 	__be32 key = 0;
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds 	flags = p[0];
5061da177e4SLinus Torvalds 	if (flags&(GRE_CSUM|GRE_KEY|GRE_SEQ|GRE_ROUTING|GRE_VERSION)) {
5071da177e4SLinus Torvalds 		if (flags&(GRE_VERSION|GRE_ROUTING))
5081da177e4SLinus Torvalds 			return;
5091da177e4SLinus Torvalds 		if (flags&GRE_KEY) {
5101da177e4SLinus Torvalds 			grehlen += 4;
5111da177e4SLinus Torvalds 			if (flags&GRE_CSUM)
5121da177e4SLinus Torvalds 				grehlen += 4;
5131da177e4SLinus Torvalds 		}
5141da177e4SLinus Torvalds 	}
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 	/* If only 8 bytes returned, keyed message will be dropped here */
5171da177e4SLinus Torvalds 	if (skb_headlen(skb) < grehlen)
5181da177e4SLinus Torvalds 		return;
5191da177e4SLinus Torvalds 
520d2083287Sstephen hemminger 	if (flags & GRE_KEY)
521d2083287Sstephen hemminger 		key = *(((__be32 *)p) + (grehlen / 4) - 1);
522d2083287Sstephen hemminger 
5231da177e4SLinus Torvalds 	switch (type) {
5241da177e4SLinus Torvalds 	default:
5251da177e4SLinus Torvalds 	case ICMP_PARAMETERPROB:
5261da177e4SLinus Torvalds 		return;
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds 	case ICMP_DEST_UNREACH:
5291da177e4SLinus Torvalds 		switch (code) {
5301da177e4SLinus Torvalds 		case ICMP_SR_FAILED:
5311da177e4SLinus Torvalds 		case ICMP_PORT_UNREACH:
5321da177e4SLinus Torvalds 			/* Impossible event. */
5331da177e4SLinus Torvalds 			return;
5341da177e4SLinus Torvalds 		default:
5351da177e4SLinus Torvalds 			/* All others are translated to HOST_UNREACH.
5361da177e4SLinus Torvalds 			   rfc2003 contains "deep thoughts" about NET_UNREACH,
5371da177e4SLinus Torvalds 			   I believe they are just ether pollution. --ANK
5381da177e4SLinus Torvalds 			 */
5391da177e4SLinus Torvalds 			break;
5401da177e4SLinus Torvalds 		}
5411da177e4SLinus Torvalds 		break;
5421da177e4SLinus Torvalds 	case ICMP_TIME_EXCEEDED:
5431da177e4SLinus Torvalds 		if (code != ICMP_EXC_TTL)
5441da177e4SLinus Torvalds 			return;
5451da177e4SLinus Torvalds 		break;
54655be7a9cSDavid S. Miller 
54755be7a9cSDavid S. Miller 	case ICMP_REDIRECT:
54855be7a9cSDavid S. Miller 		break;
5491da177e4SLinus Torvalds 	}
5501da177e4SLinus Torvalds 
551749c10f9STimo Teras 	t = ipgre_tunnel_lookup(skb->dev, iph->daddr, iph->saddr,
552d2083287Sstephen hemminger 				flags, key, p[1]);
553d2083287Sstephen hemminger 
55436393395SDavid S. Miller 	if (t == NULL)
5550c5794a6Sstephen hemminger 		return;
55636393395SDavid S. Miller 
55736393395SDavid S. Miller 	if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
55836393395SDavid S. Miller 		ipv4_update_pmtu(skb, dev_net(skb->dev), info,
55936393395SDavid S. Miller 				 t->parms.link, 0, IPPROTO_GRE, 0);
5600c5794a6Sstephen hemminger 		return;
56136393395SDavid S. Miller 	}
56255be7a9cSDavid S. Miller 	if (type == ICMP_REDIRECT) {
56355be7a9cSDavid S. Miller 		ipv4_redirect(skb, dev_net(skb->dev), t->parms.link, 0,
56455be7a9cSDavid S. Miller 			      IPPROTO_GRE, 0);
5650c5794a6Sstephen hemminger 		return;
56655be7a9cSDavid S. Miller 	}
56736393395SDavid S. Miller 	if (t->parms.iph.daddr == 0 ||
568f97c1e0cSJoe Perches 	    ipv4_is_multicast(t->parms.iph.daddr))
5690c5794a6Sstephen hemminger 		return;
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds 	if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
5720c5794a6Sstephen hemminger 		return;
5731da177e4SLinus Torvalds 
574da6185d8SWei Yongjun 	if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
5751da177e4SLinus Torvalds 		t->err_count++;
5761da177e4SLinus Torvalds 	else
5771da177e4SLinus Torvalds 		t->err_count = 1;
5781da177e4SLinus Torvalds 	t->err_time = jiffies;
5791da177e4SLinus Torvalds }
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds static inline u8
582b71d1d42SEric Dumazet ipgre_ecn_encapsulate(u8 tos, const struct iphdr *old_iph, struct sk_buff *skb)
5831da177e4SLinus Torvalds {
5841da177e4SLinus Torvalds 	u8 inner = 0;
5851da177e4SLinus Torvalds 	if (skb->protocol == htons(ETH_P_IP))
5861da177e4SLinus Torvalds 		inner = old_iph->tos;
5871da177e4SLinus Torvalds 	else if (skb->protocol == htons(ETH_P_IPV6))
588b71d1d42SEric Dumazet 		inner = ipv6_get_dsfield((const struct ipv6hdr *)old_iph);
5891da177e4SLinus Torvalds 	return INET_ECN_encapsulate(tos, inner);
5901da177e4SLinus Torvalds }
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds static int ipgre_rcv(struct sk_buff *skb)
5931da177e4SLinus Torvalds {
594b71d1d42SEric Dumazet 	const struct iphdr *iph;
5951da177e4SLinus Torvalds 	u8     *h;
596d5a0a1e3SAl Viro 	__be16    flags;
597d3bc23e7SAl Viro 	__sum16   csum = 0;
598d5a0a1e3SAl Viro 	__be32 key = 0;
5991da177e4SLinus Torvalds 	u32    seqno = 0;
6001da177e4SLinus Torvalds 	struct ip_tunnel *tunnel;
6011da177e4SLinus Torvalds 	int    offset = 4;
602e1a80002SHerbert Xu 	__be16 gre_proto;
603eccc1bb8Sstephen hemminger 	int    err;
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds 	if (!pskb_may_pull(skb, 16))
6060c5794a6Sstephen hemminger 		goto drop;
6071da177e4SLinus Torvalds 
608eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
6091da177e4SLinus Torvalds 	h = skb->data;
610d5a0a1e3SAl Viro 	flags = *(__be16 *)h;
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds 	if (flags&(GRE_CSUM|GRE_KEY|GRE_ROUTING|GRE_SEQ|GRE_VERSION)) {
6131da177e4SLinus Torvalds 		/* - Version must be 0.
6141da177e4SLinus Torvalds 		   - We do not support routing headers.
6151da177e4SLinus Torvalds 		 */
6161da177e4SLinus Torvalds 		if (flags&(GRE_VERSION|GRE_ROUTING))
6170c5794a6Sstephen hemminger 			goto drop;
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 		if (flags&GRE_CSUM) {
620fb286bb2SHerbert Xu 			switch (skb->ip_summed) {
62184fa7933SPatrick McHardy 			case CHECKSUM_COMPLETE:
622d3bc23e7SAl Viro 				csum = csum_fold(skb->csum);
623fb286bb2SHerbert Xu 				if (!csum)
624fb286bb2SHerbert Xu 					break;
625fb286bb2SHerbert Xu 				/* fall through */
626fb286bb2SHerbert Xu 			case CHECKSUM_NONE:
627fb286bb2SHerbert Xu 				skb->csum = 0;
628fb286bb2SHerbert Xu 				csum = __skb_checksum_complete(skb);
62984fa7933SPatrick McHardy 				skb->ip_summed = CHECKSUM_COMPLETE;
6301da177e4SLinus Torvalds 			}
6311da177e4SLinus Torvalds 			offset += 4;
6321da177e4SLinus Torvalds 		}
6331da177e4SLinus Torvalds 		if (flags&GRE_KEY) {
634d5a0a1e3SAl Viro 			key = *(__be32 *)(h + offset);
6351da177e4SLinus Torvalds 			offset += 4;
6361da177e4SLinus Torvalds 		}
6371da177e4SLinus Torvalds 		if (flags&GRE_SEQ) {
638d5a0a1e3SAl Viro 			seqno = ntohl(*(__be32 *)(h + offset));
6391da177e4SLinus Torvalds 			offset += 4;
6401da177e4SLinus Torvalds 		}
6411da177e4SLinus Torvalds 	}
6421da177e4SLinus Torvalds 
643e1a80002SHerbert Xu 	gre_proto = *(__be16 *)(h + 2);
644e1a80002SHerbert Xu 
645d2083287Sstephen hemminger 	tunnel = ipgre_tunnel_lookup(skb->dev,
646d2083287Sstephen hemminger 				     iph->saddr, iph->daddr, flags, key,
647d2083287Sstephen hemminger 				     gre_proto);
648d2083287Sstephen hemminger 	if (tunnel) {
649e985aad7SEric Dumazet 		struct pcpu_tstats *tstats;
650addd68ebSPavel Emelyanov 
6511da177e4SLinus Torvalds 		secpath_reset(skb);
6521da177e4SLinus Torvalds 
653e1a80002SHerbert Xu 		skb->protocol = gre_proto;
6541da177e4SLinus Torvalds 		/* WCCP version 1 and 2 protocol decoding.
6551da177e4SLinus Torvalds 		 * - Change protocol to IP
6561da177e4SLinus Torvalds 		 * - When dealing with WCCPv2, Skip extra 4 bytes in GRE header
6571da177e4SLinus Torvalds 		 */
658e1a80002SHerbert Xu 		if (flags == 0 && gre_proto == htons(ETH_P_WCCP)) {
659496c98dfSYOSHIFUJI Hideaki 			skb->protocol = htons(ETH_P_IP);
6601da177e4SLinus Torvalds 			if ((*(h + offset) & 0xF0) != 0x40)
6611da177e4SLinus Torvalds 				offset += 4;
6621da177e4SLinus Torvalds 		}
6631da177e4SLinus Torvalds 
6641d069167STimo Teras 		skb->mac_header = skb->network_header;
6654209fb60SArnaldo Carvalho de Melo 		__pskb_pull(skb, offset);
6669c70220bSArnaldo Carvalho de Melo 		skb_postpull_rcsum(skb, skb_transport_header(skb), offset);
6671da177e4SLinus Torvalds 		skb->pkt_type = PACKET_HOST;
6681da177e4SLinus Torvalds #ifdef CONFIG_NET_IPGRE_BROADCAST
669f97c1e0cSJoe Perches 		if (ipv4_is_multicast(iph->daddr)) {
6701da177e4SLinus Torvalds 			/* Looped back packet, drop it! */
671c7537967SDavid S. Miller 			if (rt_is_output_route(skb_rtable(skb)))
6721da177e4SLinus Torvalds 				goto drop;
673e985aad7SEric Dumazet 			tunnel->dev->stats.multicast++;
6741da177e4SLinus Torvalds 			skb->pkt_type = PACKET_BROADCAST;
6751da177e4SLinus Torvalds 		}
6761da177e4SLinus Torvalds #endif
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds 		if (((flags&GRE_CSUM) && csum) ||
6791da177e4SLinus Torvalds 		    (!(flags&GRE_CSUM) && tunnel->parms.i_flags&GRE_CSUM)) {
680e985aad7SEric Dumazet 			tunnel->dev->stats.rx_crc_errors++;
681e985aad7SEric Dumazet 			tunnel->dev->stats.rx_errors++;
6821da177e4SLinus Torvalds 			goto drop;
6831da177e4SLinus Torvalds 		}
6841da177e4SLinus Torvalds 		if (tunnel->parms.i_flags&GRE_SEQ) {
6851da177e4SLinus Torvalds 			if (!(flags&GRE_SEQ) ||
6861da177e4SLinus Torvalds 			    (tunnel->i_seqno && (s32)(seqno - tunnel->i_seqno) < 0)) {
687e985aad7SEric Dumazet 				tunnel->dev->stats.rx_fifo_errors++;
688e985aad7SEric Dumazet 				tunnel->dev->stats.rx_errors++;
6891da177e4SLinus Torvalds 				goto drop;
6901da177e4SLinus Torvalds 			}
6911da177e4SLinus Torvalds 			tunnel->i_seqno = seqno + 1;
6921da177e4SLinus Torvalds 		}
693e1a80002SHerbert Xu 
694e1a80002SHerbert Xu 		/* Warning: All skb pointers will be invalidated! */
695e1a80002SHerbert Xu 		if (tunnel->dev->type == ARPHRD_ETHER) {
696e1a80002SHerbert Xu 			if (!pskb_may_pull(skb, ETH_HLEN)) {
697e985aad7SEric Dumazet 				tunnel->dev->stats.rx_length_errors++;
698e985aad7SEric Dumazet 				tunnel->dev->stats.rx_errors++;
699e1a80002SHerbert Xu 				goto drop;
700e1a80002SHerbert Xu 			}
701e1a80002SHerbert Xu 
702e1a80002SHerbert Xu 			iph = ip_hdr(skb);
703e1a80002SHerbert Xu 			skb->protocol = eth_type_trans(skb, tunnel->dev);
704e1a80002SHerbert Xu 			skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
705e1a80002SHerbert Xu 		}
706e1a80002SHerbert Xu 
707eccc1bb8Sstephen hemminger 		__skb_tunnel_rx(skb, tunnel->dev);
708eccc1bb8Sstephen hemminger 
709eccc1bb8Sstephen hemminger 		skb_reset_network_header(skb);
710eccc1bb8Sstephen hemminger 		err = IP_ECN_decapsulate(iph, skb);
711eccc1bb8Sstephen hemminger 		if (unlikely(err)) {
712eccc1bb8Sstephen hemminger 			if (log_ecn_error)
713eccc1bb8Sstephen hemminger 				net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
714eccc1bb8Sstephen hemminger 						     &iph->saddr, iph->tos);
715eccc1bb8Sstephen hemminger 			if (err > 1) {
716eccc1bb8Sstephen hemminger 				++tunnel->dev->stats.rx_frame_errors;
717eccc1bb8Sstephen hemminger 				++tunnel->dev->stats.rx_errors;
718eccc1bb8Sstephen hemminger 				goto drop;
719eccc1bb8Sstephen hemminger 			}
720eccc1bb8Sstephen hemminger 		}
721eccc1bb8Sstephen hemminger 
722e985aad7SEric Dumazet 		tstats = this_cpu_ptr(tunnel->dev->tstats);
72387b6d218Sstephen hemminger 		u64_stats_update_begin(&tstats->syncp);
724e985aad7SEric Dumazet 		tstats->rx_packets++;
725e985aad7SEric Dumazet 		tstats->rx_bytes += skb->len;
72687b6d218Sstephen hemminger 		u64_stats_update_end(&tstats->syncp);
727e985aad7SEric Dumazet 
72860769a5dSEric Dumazet 		gro_cells_receive(&tunnel->gro_cells, skb);
7298990f468SEric Dumazet 		return 0;
7301da177e4SLinus Torvalds 	}
73145af08beSHerbert Xu 	icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds drop:
7341da177e4SLinus Torvalds 	kfree_skb(skb);
735a02cec21SEric Dumazet 	return 0;
7361da177e4SLinus Torvalds }
7371da177e4SLinus Torvalds 
7386fef4c0cSStephen Hemminger static netdev_tx_t ipgre_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
7391da177e4SLinus Torvalds {
7402941a486SPatrick McHardy 	struct ip_tunnel *tunnel = netdev_priv(dev);
741*cef401deSEric Dumazet 	const struct iphdr  *old_iph;
742b71d1d42SEric Dumazet 	const struct iphdr  *tiph;
743cbb1e85fSDavid S. Miller 	struct flowi4 fl4;
7441da177e4SLinus Torvalds 	u8     tos;
745d5a0a1e3SAl Viro 	__be16 df;
7461da177e4SLinus Torvalds 	struct rtable *rt;     			/* Route to the other host */
7471da177e4SLinus Torvalds 	struct net_device *tdev;		/* Device to other host */
7481da177e4SLinus Torvalds 	struct iphdr  *iph;			/* Our new IP header */
749c2636b4dSChuck Lever 	unsigned int max_headroom;		/* The extra header space needed */
7501da177e4SLinus Torvalds 	int    gre_hlen;
751d5a0a1e3SAl Viro 	__be32 dst;
7521da177e4SLinus Torvalds 	int    mtu;
753f7e75ba1SEric Dumazet 	u8     ttl;
7541da177e4SLinus Torvalds 
7556b78f16eSEric Dumazet 	if (skb->ip_summed == CHECKSUM_PARTIAL &&
7566b78f16eSEric Dumazet 	    skb_checksum_help(skb))
7576b78f16eSEric Dumazet 		goto tx_error;
7586b78f16eSEric Dumazet 
759*cef401deSEric Dumazet 	old_iph = ip_hdr(skb);
760*cef401deSEric Dumazet 
761e1a80002SHerbert Xu 	if (dev->type == ARPHRD_ETHER)
762e1a80002SHerbert Xu 		IPCB(skb)->flags = 0;
763e1a80002SHerbert Xu 
764e1a80002SHerbert Xu 	if (dev->header_ops && dev->type == ARPHRD_IPGRE) {
7651da177e4SLinus Torvalds 		gre_hlen = 0;
766412ed947SIsaku Yamahata 		if (skb->protocol == htons(ETH_P_IP))
767b71d1d42SEric Dumazet 			tiph = (const struct iphdr *)skb->data;
768412ed947SIsaku Yamahata 		else
769412ed947SIsaku Yamahata 			tiph = &tunnel->parms.iph;
7701da177e4SLinus Torvalds 	} else {
7711da177e4SLinus Torvalds 		gre_hlen = tunnel->hlen;
7721da177e4SLinus Torvalds 		tiph = &tunnel->parms.iph;
7731da177e4SLinus Torvalds 	}
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds 	if ((dst = tiph->daddr) == 0) {
7761da177e4SLinus Torvalds 		/* NBMA tunnel */
7771da177e4SLinus Torvalds 
778adf30907SEric Dumazet 		if (skb_dst(skb) == NULL) {
779e985aad7SEric Dumazet 			dev->stats.tx_fifo_errors++;
7801da177e4SLinus Torvalds 			goto tx_error;
7811da177e4SLinus Torvalds 		}
7821da177e4SLinus Torvalds 
78361d57f87SDavid S. Miller 		if (skb->protocol == htons(ETH_P_IP)) {
784511c3f92SEric Dumazet 			rt = skb_rtable(skb);
785f8126f1dSDavid S. Miller 			dst = rt_nexthop(rt, old_iph->daddr);
78661d57f87SDavid S. Miller 		}
787dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
7881da177e4SLinus Torvalds 		else if (skb->protocol == htons(ETH_P_IPV6)) {
789b71d1d42SEric Dumazet 			const struct in6_addr *addr6;
7900ec88662SDavid S. Miller 			struct neighbour *neigh;
7910ec88662SDavid S. Miller 			bool do_tx_error_icmp;
7921da177e4SLinus Torvalds 			int addr_type;
7931da177e4SLinus Torvalds 
7940ec88662SDavid S. Miller 			neigh = dst_neigh_lookup(skb_dst(skb), &ipv6_hdr(skb)->daddr);
7951da177e4SLinus Torvalds 			if (neigh == NULL)
7961da177e4SLinus Torvalds 				goto tx_error;
7971da177e4SLinus Torvalds 
798b71d1d42SEric Dumazet 			addr6 = (const struct in6_addr *)&neigh->primary_key;
7991da177e4SLinus Torvalds 			addr_type = ipv6_addr_type(addr6);
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds 			if (addr_type == IPV6_ADDR_ANY) {
8020660e03fSArnaldo Carvalho de Melo 				addr6 = &ipv6_hdr(skb)->daddr;
8031da177e4SLinus Torvalds 				addr_type = ipv6_addr_type(addr6);
8041da177e4SLinus Torvalds 			}
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds 			if ((addr_type & IPV6_ADDR_COMPATv4) == 0)
8070ec88662SDavid S. Miller 				do_tx_error_icmp = true;
8080ec88662SDavid S. Miller 			else {
8090ec88662SDavid S. Miller 				do_tx_error_icmp = false;
8101da177e4SLinus Torvalds 				dst = addr6->s6_addr32[3];
8111da177e4SLinus Torvalds 			}
8120ec88662SDavid S. Miller 			neigh_release(neigh);
8130ec88662SDavid S. Miller 			if (do_tx_error_icmp)
8140ec88662SDavid S. Miller 				goto tx_error_icmp;
8150ec88662SDavid S. Miller 		}
8161da177e4SLinus Torvalds #endif
8171da177e4SLinus Torvalds 		else
8181da177e4SLinus Torvalds 			goto tx_error;
8191da177e4SLinus Torvalds 	}
8201da177e4SLinus Torvalds 
821f7e75ba1SEric Dumazet 	ttl = tiph->ttl;
8221da177e4SLinus Torvalds 	tos = tiph->tos;
823ee686ca9SAndreas Jaggi 	if (tos == 1) {
824ee686ca9SAndreas Jaggi 		tos = 0;
8251da177e4SLinus Torvalds 		if (skb->protocol == htons(ETH_P_IP))
8261da177e4SLinus Torvalds 			tos = old_iph->tos;
827dd4ba83dSStephen Hemminger 		else if (skb->protocol == htons(ETH_P_IPV6))
828b71d1d42SEric Dumazet 			tos = ipv6_get_dsfield((const struct ipv6hdr *)old_iph);
8291da177e4SLinus Torvalds 	}
8301da177e4SLinus Torvalds 
831cbb1e85fSDavid S. Miller 	rt = ip_route_output_gre(dev_net(dev), &fl4, dst, tiph->saddr,
83278fbfd8aSDavid S. Miller 				 tunnel->parms.o_key, RT_TOS(tos),
83378fbfd8aSDavid S. Miller 				 tunnel->parms.link);
834b23dd4feSDavid S. Miller 	if (IS_ERR(rt)) {
835e985aad7SEric Dumazet 		dev->stats.tx_carrier_errors++;
8361da177e4SLinus Torvalds 		goto tx_error;
8371da177e4SLinus Torvalds 	}
838d8d1f30bSChangli Gao 	tdev = rt->dst.dev;
8391da177e4SLinus Torvalds 
8401da177e4SLinus Torvalds 	if (tdev == dev) {
8411da177e4SLinus Torvalds 		ip_rt_put(rt);
842e985aad7SEric Dumazet 		dev->stats.collisions++;
8431da177e4SLinus Torvalds 		goto tx_error;
8441da177e4SLinus Torvalds 	}
8451da177e4SLinus Torvalds 
8461da177e4SLinus Torvalds 	df = tiph->frag_off;
8471da177e4SLinus Torvalds 	if (df)
848d8d1f30bSChangli Gao 		mtu = dst_mtu(&rt->dst) - dev->hard_header_len - tunnel->hlen;
8491da177e4SLinus Torvalds 	else
850adf30907SEric Dumazet 		mtu = skb_dst(skb) ? dst_mtu(skb_dst(skb)) : dev->mtu;
8511da177e4SLinus Torvalds 
852adf30907SEric Dumazet 	if (skb_dst(skb))
8536700c270SDavid S. Miller 		skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds 	if (skb->protocol == htons(ETH_P_IP)) {
8561da177e4SLinus Torvalds 		df |= (old_iph->frag_off&htons(IP_DF));
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds 		if ((old_iph->frag_off&htons(IP_DF)) &&
8591da177e4SLinus Torvalds 		    mtu < ntohs(old_iph->tot_len)) {
8601da177e4SLinus Torvalds 			icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
8611da177e4SLinus Torvalds 			ip_rt_put(rt);
8621da177e4SLinus Torvalds 			goto tx_error;
8631da177e4SLinus Torvalds 		}
8641da177e4SLinus Torvalds 	}
865dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
8661da177e4SLinus Torvalds 	else if (skb->protocol == htons(ETH_P_IPV6)) {
867adf30907SEric Dumazet 		struct rt6_info *rt6 = (struct rt6_info *)skb_dst(skb);
8681da177e4SLinus Torvalds 
869adf30907SEric Dumazet 		if (rt6 && mtu < dst_mtu(skb_dst(skb)) && mtu >= IPV6_MIN_MTU) {
870f97c1e0cSJoe Perches 			if ((tunnel->parms.iph.daddr &&
871f97c1e0cSJoe Perches 			     !ipv4_is_multicast(tunnel->parms.iph.daddr)) ||
8721da177e4SLinus Torvalds 			    rt6->rt6i_dst.plen == 128) {
8731da177e4SLinus Torvalds 				rt6->rt6i_flags |= RTF_MODIFIED;
874defb3519SDavid S. Miller 				dst_metric_set(skb_dst(skb), RTAX_MTU, mtu);
8751da177e4SLinus Torvalds 			}
8761da177e4SLinus Torvalds 		}
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds 		if (mtu >= IPV6_MIN_MTU && mtu < skb->len - tunnel->hlen + gre_hlen) {
8793ffe533cSAlexey Dobriyan 			icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
8801da177e4SLinus Torvalds 			ip_rt_put(rt);
8811da177e4SLinus Torvalds 			goto tx_error;
8821da177e4SLinus Torvalds 		}
8831da177e4SLinus Torvalds 	}
8841da177e4SLinus Torvalds #endif
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds 	if (tunnel->err_count > 0) {
887da6185d8SWei Yongjun 		if (time_before(jiffies,
888da6185d8SWei Yongjun 				tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
8891da177e4SLinus Torvalds 			tunnel->err_count--;
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 			dst_link_failure(skb);
8921da177e4SLinus Torvalds 		} else
8931da177e4SLinus Torvalds 			tunnel->err_count = 0;
8941da177e4SLinus Torvalds 	}
8951da177e4SLinus Torvalds 
896d8d1f30bSChangli Gao 	max_headroom = LL_RESERVED_SPACE(tdev) + gre_hlen + rt->dst.header_len;
8971da177e4SLinus Torvalds 
898cfbba49dSPatrick McHardy 	if (skb_headroom(skb) < max_headroom || skb_shared(skb)||
899cfbba49dSPatrick McHardy 	    (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
9001da177e4SLinus Torvalds 		struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
901805dc1d6SHerbert Xu 		if (max_headroom > dev->needed_headroom)
902805dc1d6SHerbert Xu 			dev->needed_headroom = max_headroom;
9031da177e4SLinus Torvalds 		if (!new_skb) {
9041da177e4SLinus Torvalds 			ip_rt_put(rt);
905e985aad7SEric Dumazet 			dev->stats.tx_dropped++;
9061da177e4SLinus Torvalds 			dev_kfree_skb(skb);
9076ed10654SPatrick McHardy 			return NETDEV_TX_OK;
9081da177e4SLinus Torvalds 		}
9091da177e4SLinus Torvalds 		if (skb->sk)
9101da177e4SLinus Torvalds 			skb_set_owner_w(new_skb, skb->sk);
9111da177e4SLinus Torvalds 		dev_kfree_skb(skb);
9121da177e4SLinus Torvalds 		skb = new_skb;
913eddc9ec5SArnaldo Carvalho de Melo 		old_iph = ip_hdr(skb);
914f7e75ba1SEric Dumazet 		/* Warning : tiph value might point to freed memory */
9151da177e4SLinus Torvalds 	}
9161da177e4SLinus Torvalds 
917e2d1bca7SArnaldo Carvalho de Melo 	skb_push(skb, gre_hlen);
918e2d1bca7SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
919861aa6d5SIsaku Yamahata 	skb_set_transport_header(skb, sizeof(*iph));
9201da177e4SLinus Torvalds 	memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
92148d5cad8SPatrick McHardy 	IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
92248d5cad8SPatrick McHardy 			      IPSKB_REROUTED);
923adf30907SEric Dumazet 	skb_dst_drop(skb);
924d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds 	/*
9271da177e4SLinus Torvalds 	 *	Push down and install the IPIP header.
9281da177e4SLinus Torvalds 	 */
9291da177e4SLinus Torvalds 
930eddc9ec5SArnaldo Carvalho de Melo 	iph 			=	ip_hdr(skb);
9311da177e4SLinus Torvalds 	iph->version		=	4;
9321da177e4SLinus Torvalds 	iph->ihl		=	sizeof(struct iphdr) >> 2;
9331da177e4SLinus Torvalds 	iph->frag_off		=	df;
9341da177e4SLinus Torvalds 	iph->protocol		=	IPPROTO_GRE;
9351da177e4SLinus Torvalds 	iph->tos		=	ipgre_ecn_encapsulate(tos, old_iph, skb);
936cbb1e85fSDavid S. Miller 	iph->daddr		=	fl4.daddr;
937cbb1e85fSDavid S. Miller 	iph->saddr		=	fl4.saddr;
938f7e75ba1SEric Dumazet 	iph->ttl		=	ttl;
9391da177e4SLinus Torvalds 
940f7e75ba1SEric Dumazet 	if (ttl == 0) {
9411da177e4SLinus Torvalds 		if (skb->protocol == htons(ETH_P_IP))
9421da177e4SLinus Torvalds 			iph->ttl = old_iph->ttl;
943dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
9441da177e4SLinus Torvalds 		else if (skb->protocol == htons(ETH_P_IPV6))
945b71d1d42SEric Dumazet 			iph->ttl = ((const struct ipv6hdr *)old_iph)->hop_limit;
9461da177e4SLinus Torvalds #endif
9471da177e4SLinus Torvalds 		else
948323e126fSDavid S. Miller 			iph->ttl = ip4_dst_hoplimit(&rt->dst);
9491da177e4SLinus Torvalds 	}
9501da177e4SLinus Torvalds 
951d5a0a1e3SAl Viro 	((__be16 *)(iph + 1))[0] = tunnel->parms.o_flags;
952e1a80002SHerbert Xu 	((__be16 *)(iph + 1))[1] = (dev->type == ARPHRD_ETHER) ?
953e1a80002SHerbert Xu 				   htons(ETH_P_TEB) : skb->protocol;
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds 	if (tunnel->parms.o_flags&(GRE_KEY|GRE_CSUM|GRE_SEQ)) {
956d5a0a1e3SAl Viro 		__be32 *ptr = (__be32 *)(((u8 *)iph) + tunnel->hlen - 4);
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds 		if (tunnel->parms.o_flags&GRE_SEQ) {
9591da177e4SLinus Torvalds 			++tunnel->o_seqno;
9601da177e4SLinus Torvalds 			*ptr = htonl(tunnel->o_seqno);
9611da177e4SLinus Torvalds 			ptr--;
9621da177e4SLinus Torvalds 		}
9631da177e4SLinus Torvalds 		if (tunnel->parms.o_flags&GRE_KEY) {
9641da177e4SLinus Torvalds 			*ptr = tunnel->parms.o_key;
9651da177e4SLinus Torvalds 			ptr--;
9661da177e4SLinus Torvalds 		}
9671da177e4SLinus Torvalds 		if (tunnel->parms.o_flags&GRE_CSUM) {
9681da177e4SLinus Torvalds 			*ptr = 0;
9695f92a738SAl Viro 			*(__sum16 *)ptr = ip_compute_csum((void *)(iph+1), skb->len - sizeof(struct iphdr));
9701da177e4SLinus Torvalds 		}
9711da177e4SLinus Torvalds 	}
9721da177e4SLinus Torvalds 
973aa0010f8SAmerigo Wang 	iptunnel_xmit(skb, dev);
9746ed10654SPatrick McHardy 	return NETDEV_TX_OK;
9751da177e4SLinus Torvalds 
976496053f4SDavid S. Miller #if IS_ENABLED(CONFIG_IPV6)
9771da177e4SLinus Torvalds tx_error_icmp:
9781da177e4SLinus Torvalds 	dst_link_failure(skb);
979496053f4SDavid S. Miller #endif
9801da177e4SLinus Torvalds tx_error:
981e985aad7SEric Dumazet 	dev->stats.tx_errors++;
9821da177e4SLinus Torvalds 	dev_kfree_skb(skb);
9836ed10654SPatrick McHardy 	return NETDEV_TX_OK;
9841da177e4SLinus Torvalds }
9851da177e4SLinus Torvalds 
98642aa9162SHerbert Xu static int ipgre_tunnel_bind_dev(struct net_device *dev)
987ee34c1ebSMichal Schmidt {
988ee34c1ebSMichal Schmidt 	struct net_device *tdev = NULL;
989ee34c1ebSMichal Schmidt 	struct ip_tunnel *tunnel;
990b71d1d42SEric Dumazet 	const struct iphdr *iph;
991ee34c1ebSMichal Schmidt 	int hlen = LL_MAX_HEADER;
992ee34c1ebSMichal Schmidt 	int mtu = ETH_DATA_LEN;
993ee34c1ebSMichal Schmidt 	int addend = sizeof(struct iphdr) + 4;
994ee34c1ebSMichal Schmidt 
995ee34c1ebSMichal Schmidt 	tunnel = netdev_priv(dev);
996ee34c1ebSMichal Schmidt 	iph = &tunnel->parms.iph;
997ee34c1ebSMichal Schmidt 
998c95b819aSHerbert Xu 	/* Guess output device to choose reasonable mtu and needed_headroom */
999ee34c1ebSMichal Schmidt 
1000ee34c1ebSMichal Schmidt 	if (iph->daddr) {
1001cbb1e85fSDavid S. Miller 		struct flowi4 fl4;
1002cbb1e85fSDavid S. Miller 		struct rtable *rt;
1003cbb1e85fSDavid S. Miller 
1004cbb1e85fSDavid S. Miller 		rt = ip_route_output_gre(dev_net(dev), &fl4,
100578fbfd8aSDavid S. Miller 					 iph->daddr, iph->saddr,
100678fbfd8aSDavid S. Miller 					 tunnel->parms.o_key,
100778fbfd8aSDavid S. Miller 					 RT_TOS(iph->tos),
100878fbfd8aSDavid S. Miller 					 tunnel->parms.link);
1009b23dd4feSDavid S. Miller 		if (!IS_ERR(rt)) {
1010d8d1f30bSChangli Gao 			tdev = rt->dst.dev;
1011ee34c1ebSMichal Schmidt 			ip_rt_put(rt);
1012ee34c1ebSMichal Schmidt 		}
1013e1a80002SHerbert Xu 
1014e1a80002SHerbert Xu 		if (dev->type != ARPHRD_ETHER)
1015ee34c1ebSMichal Schmidt 			dev->flags |= IFF_POINTOPOINT;
1016ee34c1ebSMichal Schmidt 	}
1017ee34c1ebSMichal Schmidt 
1018ee34c1ebSMichal Schmidt 	if (!tdev && tunnel->parms.link)
101996635522SPavel Emelyanov 		tdev = __dev_get_by_index(dev_net(dev), tunnel->parms.link);
1020ee34c1ebSMichal Schmidt 
1021ee34c1ebSMichal Schmidt 	if (tdev) {
1022c95b819aSHerbert Xu 		hlen = tdev->hard_header_len + tdev->needed_headroom;
1023ee34c1ebSMichal Schmidt 		mtu = tdev->mtu;
1024ee34c1ebSMichal Schmidt 	}
1025ee34c1ebSMichal Schmidt 	dev->iflink = tunnel->parms.link;
1026ee34c1ebSMichal Schmidt 
1027ee34c1ebSMichal Schmidt 	/* Precalculate GRE options length */
1028ee34c1ebSMichal Schmidt 	if (tunnel->parms.o_flags&(GRE_CSUM|GRE_KEY|GRE_SEQ)) {
1029ee34c1ebSMichal Schmidt 		if (tunnel->parms.o_flags&GRE_CSUM)
1030ee34c1ebSMichal Schmidt 			addend += 4;
1031ee34c1ebSMichal Schmidt 		if (tunnel->parms.o_flags&GRE_KEY)
1032ee34c1ebSMichal Schmidt 			addend += 4;
1033ee34c1ebSMichal Schmidt 		if (tunnel->parms.o_flags&GRE_SEQ)
1034ee34c1ebSMichal Schmidt 			addend += 4;
1035ee34c1ebSMichal Schmidt 	}
1036c95b819aSHerbert Xu 	dev->needed_headroom = addend + hlen;
10378cdb0456STom Goff 	mtu -= dev->hard_header_len + addend;
103842aa9162SHerbert Xu 
103942aa9162SHerbert Xu 	if (mtu < 68)
104042aa9162SHerbert Xu 		mtu = 68;
104142aa9162SHerbert Xu 
1042ee34c1ebSMichal Schmidt 	tunnel->hlen = addend;
1043ee34c1ebSMichal Schmidt 
104442aa9162SHerbert Xu 	return mtu;
1045ee34c1ebSMichal Schmidt }
1046ee34c1ebSMichal Schmidt 
10471da177e4SLinus Torvalds static int
10481da177e4SLinus Torvalds ipgre_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
10491da177e4SLinus Torvalds {
10501da177e4SLinus Torvalds 	int err = 0;
10511da177e4SLinus Torvalds 	struct ip_tunnel_parm p;
10521da177e4SLinus Torvalds 	struct ip_tunnel *t;
1053f57e7d5aSPavel Emelyanov 	struct net *net = dev_net(dev);
1054f57e7d5aSPavel Emelyanov 	struct ipgre_net *ign = net_generic(net, ipgre_net_id);
10551da177e4SLinus Torvalds 
10561da177e4SLinus Torvalds 	switch (cmd) {
10571da177e4SLinus Torvalds 	case SIOCGETTUNNEL:
10581da177e4SLinus Torvalds 		t = NULL;
10597daa0004SPavel Emelyanov 		if (dev == ign->fb_tunnel_dev) {
10601da177e4SLinus Torvalds 			if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
10611da177e4SLinus Torvalds 				err = -EFAULT;
10621da177e4SLinus Torvalds 				break;
10631da177e4SLinus Torvalds 			}
1064f57e7d5aSPavel Emelyanov 			t = ipgre_tunnel_locate(net, &p, 0);
10651da177e4SLinus Torvalds 		}
10661da177e4SLinus Torvalds 		if (t == NULL)
10672941a486SPatrick McHardy 			t = netdev_priv(dev);
10681da177e4SLinus Torvalds 		memcpy(&p, &t->parms, sizeof(p));
10691da177e4SLinus Torvalds 		if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
10701da177e4SLinus Torvalds 			err = -EFAULT;
10711da177e4SLinus Torvalds 		break;
10721da177e4SLinus Torvalds 
10731da177e4SLinus Torvalds 	case SIOCADDTUNNEL:
10741da177e4SLinus Torvalds 	case SIOCCHGTUNNEL:
10751da177e4SLinus Torvalds 		err = -EPERM;
107652e804c6SEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
10771da177e4SLinus Torvalds 			goto done;
10781da177e4SLinus Torvalds 
10791da177e4SLinus Torvalds 		err = -EFAULT;
10801da177e4SLinus Torvalds 		if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
10811da177e4SLinus Torvalds 			goto done;
10821da177e4SLinus Torvalds 
10831da177e4SLinus Torvalds 		err = -EINVAL;
10841da177e4SLinus Torvalds 		if (p.iph.version != 4 || p.iph.protocol != IPPROTO_GRE ||
10851da177e4SLinus Torvalds 		    p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)) ||
10861da177e4SLinus Torvalds 		    ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING)))
10871da177e4SLinus Torvalds 			goto done;
10881da177e4SLinus Torvalds 		if (p.iph.ttl)
10891da177e4SLinus Torvalds 			p.iph.frag_off |= htons(IP_DF);
10901da177e4SLinus Torvalds 
10911da177e4SLinus Torvalds 		if (!(p.i_flags&GRE_KEY))
10921da177e4SLinus Torvalds 			p.i_key = 0;
10931da177e4SLinus Torvalds 		if (!(p.o_flags&GRE_KEY))
10941da177e4SLinus Torvalds 			p.o_key = 0;
10951da177e4SLinus Torvalds 
1096f57e7d5aSPavel Emelyanov 		t = ipgre_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL);
10971da177e4SLinus Torvalds 
10987daa0004SPavel Emelyanov 		if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
10991da177e4SLinus Torvalds 			if (t != NULL) {
11001da177e4SLinus Torvalds 				if (t->dev != dev) {
11011da177e4SLinus Torvalds 					err = -EEXIST;
11021da177e4SLinus Torvalds 					break;
11031da177e4SLinus Torvalds 				}
11041da177e4SLinus Torvalds 			} else {
11051507850bSEric Dumazet 				unsigned int nflags = 0;
11061da177e4SLinus Torvalds 
11072941a486SPatrick McHardy 				t = netdev_priv(dev);
11081da177e4SLinus Torvalds 
1109f97c1e0cSJoe Perches 				if (ipv4_is_multicast(p.iph.daddr))
11101da177e4SLinus Torvalds 					nflags = IFF_BROADCAST;
11111da177e4SLinus Torvalds 				else if (p.iph.daddr)
11121da177e4SLinus Torvalds 					nflags = IFF_POINTOPOINT;
11131da177e4SLinus Torvalds 
11141da177e4SLinus Torvalds 				if ((dev->flags^nflags)&(IFF_POINTOPOINT|IFF_BROADCAST)) {
11151da177e4SLinus Torvalds 					err = -EINVAL;
11161da177e4SLinus Torvalds 					break;
11171da177e4SLinus Torvalds 				}
1118f57e7d5aSPavel Emelyanov 				ipgre_tunnel_unlink(ign, t);
111974b0b85bSPavel Emelyanov 				synchronize_net();
11201da177e4SLinus Torvalds 				t->parms.iph.saddr = p.iph.saddr;
11211da177e4SLinus Torvalds 				t->parms.iph.daddr = p.iph.daddr;
11221da177e4SLinus Torvalds 				t->parms.i_key = p.i_key;
11231da177e4SLinus Torvalds 				t->parms.o_key = p.o_key;
11241da177e4SLinus Torvalds 				memcpy(dev->dev_addr, &p.iph.saddr, 4);
11251da177e4SLinus Torvalds 				memcpy(dev->broadcast, &p.iph.daddr, 4);
1126f57e7d5aSPavel Emelyanov 				ipgre_tunnel_link(ign, t);
11271da177e4SLinus Torvalds 				netdev_state_change(dev);
11281da177e4SLinus Torvalds 			}
11291da177e4SLinus Torvalds 		}
11301da177e4SLinus Torvalds 
11311da177e4SLinus Torvalds 		if (t) {
11321da177e4SLinus Torvalds 			err = 0;
11331da177e4SLinus Torvalds 			if (cmd == SIOCCHGTUNNEL) {
11341da177e4SLinus Torvalds 				t->parms.iph.ttl = p.iph.ttl;
11351da177e4SLinus Torvalds 				t->parms.iph.tos = p.iph.tos;
11361da177e4SLinus Torvalds 				t->parms.iph.frag_off = p.iph.frag_off;
1137ee34c1ebSMichal Schmidt 				if (t->parms.link != p.link) {
1138ee34c1ebSMichal Schmidt 					t->parms.link = p.link;
113942aa9162SHerbert Xu 					dev->mtu = ipgre_tunnel_bind_dev(dev);
1140ee34c1ebSMichal Schmidt 					netdev_state_change(dev);
1141ee34c1ebSMichal Schmidt 				}
11421da177e4SLinus Torvalds 			}
11431da177e4SLinus Torvalds 			if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
11441da177e4SLinus Torvalds 				err = -EFAULT;
11451da177e4SLinus Torvalds 		} else
11461da177e4SLinus Torvalds 			err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
11471da177e4SLinus Torvalds 		break;
11481da177e4SLinus Torvalds 
11491da177e4SLinus Torvalds 	case SIOCDELTUNNEL:
11501da177e4SLinus Torvalds 		err = -EPERM;
115152e804c6SEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
11521da177e4SLinus Torvalds 			goto done;
11531da177e4SLinus Torvalds 
11547daa0004SPavel Emelyanov 		if (dev == ign->fb_tunnel_dev) {
11551da177e4SLinus Torvalds 			err = -EFAULT;
11561da177e4SLinus Torvalds 			if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
11571da177e4SLinus Torvalds 				goto done;
11581da177e4SLinus Torvalds 			err = -ENOENT;
1159f57e7d5aSPavel Emelyanov 			if ((t = ipgre_tunnel_locate(net, &p, 0)) == NULL)
11601da177e4SLinus Torvalds 				goto done;
11611da177e4SLinus Torvalds 			err = -EPERM;
11627daa0004SPavel Emelyanov 			if (t == netdev_priv(ign->fb_tunnel_dev))
11631da177e4SLinus Torvalds 				goto done;
11641da177e4SLinus Torvalds 			dev = t->dev;
11651da177e4SLinus Torvalds 		}
116622f8cde5SStephen Hemminger 		unregister_netdevice(dev);
116722f8cde5SStephen Hemminger 		err = 0;
11681da177e4SLinus Torvalds 		break;
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds 	default:
11711da177e4SLinus Torvalds 		err = -EINVAL;
11721da177e4SLinus Torvalds 	}
11731da177e4SLinus Torvalds 
11741da177e4SLinus Torvalds done:
11751da177e4SLinus Torvalds 	return err;
11761da177e4SLinus Torvalds }
11771da177e4SLinus Torvalds 
11781da177e4SLinus Torvalds static int ipgre_tunnel_change_mtu(struct net_device *dev, int new_mtu)
11791da177e4SLinus Torvalds {
11802941a486SPatrick McHardy 	struct ip_tunnel *tunnel = netdev_priv(dev);
1181c95b819aSHerbert Xu 	if (new_mtu < 68 ||
1182c95b819aSHerbert Xu 	    new_mtu > 0xFFF8 - dev->hard_header_len - tunnel->hlen)
11831da177e4SLinus Torvalds 		return -EINVAL;
11841da177e4SLinus Torvalds 	dev->mtu = new_mtu;
11851da177e4SLinus Torvalds 	return 0;
11861da177e4SLinus Torvalds }
11871da177e4SLinus Torvalds 
11881da177e4SLinus Torvalds /* Nice toy. Unfortunately, useless in real life :-)
11891da177e4SLinus Torvalds    It allows to construct virtual multiprotocol broadcast "LAN"
11901da177e4SLinus Torvalds    over the Internet, provided multicast routing is tuned.
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds 
11931da177e4SLinus Torvalds    I have no idea was this bicycle invented before me,
11941da177e4SLinus Torvalds    so that I had to set ARPHRD_IPGRE to a random value.
11951da177e4SLinus Torvalds    I have an impression, that Cisco could make something similar,
11961da177e4SLinus Torvalds    but this feature is apparently missing in IOS<=11.2(8).
11971da177e4SLinus Torvalds 
11981da177e4SLinus Torvalds    I set up 10.66.66/24 and fec0:6666:6666::0/96 as virtual networks
11991da177e4SLinus Torvalds    with broadcast 224.66.66.66. If you have access to mbone, play with me :-)
12001da177e4SLinus Torvalds 
12011da177e4SLinus Torvalds    ping -t 255 224.66.66.66
12021da177e4SLinus Torvalds 
12031da177e4SLinus Torvalds    If nobody answers, mbone does not work.
12041da177e4SLinus Torvalds 
12051da177e4SLinus Torvalds    ip tunnel add Universe mode gre remote 224.66.66.66 local <Your_real_addr> ttl 255
12061da177e4SLinus Torvalds    ip addr add 10.66.66.<somewhat>/24 dev Universe
12071da177e4SLinus Torvalds    ifconfig Universe up
12081da177e4SLinus Torvalds    ifconfig Universe add fe80::<Your_real_addr>/10
12091da177e4SLinus Torvalds    ifconfig Universe add fec0:6666:6666::<Your_real_addr>/96
12101da177e4SLinus Torvalds    ftp 10.66.66.66
12111da177e4SLinus Torvalds    ...
12121da177e4SLinus Torvalds    ftp fec0:6666:6666::193.233.7.65
12131da177e4SLinus Torvalds    ...
12141da177e4SLinus Torvalds 
12151da177e4SLinus Torvalds  */
12161da177e4SLinus Torvalds 
12173b04dddeSStephen Hemminger static int ipgre_header(struct sk_buff *skb, struct net_device *dev,
12183b04dddeSStephen Hemminger 			unsigned short type,
12191507850bSEric Dumazet 			const void *daddr, const void *saddr, unsigned int len)
12201da177e4SLinus Torvalds {
12212941a486SPatrick McHardy 	struct ip_tunnel *t = netdev_priv(dev);
12221da177e4SLinus Torvalds 	struct iphdr *iph = (struct iphdr *)skb_push(skb, t->hlen);
1223d5a0a1e3SAl Viro 	__be16 *p = (__be16 *)(iph+1);
12241da177e4SLinus Torvalds 
12251da177e4SLinus Torvalds 	memcpy(iph, &t->parms.iph, sizeof(struct iphdr));
12261da177e4SLinus Torvalds 	p[0]		= t->parms.o_flags;
12271da177e4SLinus Torvalds 	p[1]		= htons(type);
12281da177e4SLinus Torvalds 
12291da177e4SLinus Torvalds 	/*
12301da177e4SLinus Torvalds 	 *	Set the source hardware address.
12311da177e4SLinus Torvalds 	 */
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds 	if (saddr)
12341da177e4SLinus Torvalds 		memcpy(&iph->saddr, saddr, 4);
12356d55cb91STimo Teräs 	if (daddr)
12361da177e4SLinus Torvalds 		memcpy(&iph->daddr, daddr, 4);
12376d55cb91STimo Teräs 	if (iph->daddr)
12381da177e4SLinus Torvalds 		return t->hlen;
12391da177e4SLinus Torvalds 
12401da177e4SLinus Torvalds 	return -t->hlen;
12411da177e4SLinus Torvalds }
12421da177e4SLinus Torvalds 
12436a5f44d7STimo Teras static int ipgre_header_parse(const struct sk_buff *skb, unsigned char *haddr)
12446a5f44d7STimo Teras {
1245b71d1d42SEric Dumazet 	const struct iphdr *iph = (const struct iphdr *) skb_mac_header(skb);
12466a5f44d7STimo Teras 	memcpy(haddr, &iph->saddr, 4);
12476a5f44d7STimo Teras 	return 4;
12486a5f44d7STimo Teras }
12496a5f44d7STimo Teras 
12503b04dddeSStephen Hemminger static const struct header_ops ipgre_header_ops = {
12513b04dddeSStephen Hemminger 	.create	= ipgre_header,
12526a5f44d7STimo Teras 	.parse	= ipgre_header_parse,
12533b04dddeSStephen Hemminger };
12543b04dddeSStephen Hemminger 
12556a5f44d7STimo Teras #ifdef CONFIG_NET_IPGRE_BROADCAST
12561da177e4SLinus Torvalds static int ipgre_open(struct net_device *dev)
12571da177e4SLinus Torvalds {
12582941a486SPatrick McHardy 	struct ip_tunnel *t = netdev_priv(dev);
12591da177e4SLinus Torvalds 
1260f97c1e0cSJoe Perches 	if (ipv4_is_multicast(t->parms.iph.daddr)) {
1261cbb1e85fSDavid S. Miller 		struct flowi4 fl4;
1262cbb1e85fSDavid S. Miller 		struct rtable *rt;
1263cbb1e85fSDavid S. Miller 
1264cbb1e85fSDavid S. Miller 		rt = ip_route_output_gre(dev_net(dev), &fl4,
126578fbfd8aSDavid S. Miller 					 t->parms.iph.daddr,
126678fbfd8aSDavid S. Miller 					 t->parms.iph.saddr,
126778fbfd8aSDavid S. Miller 					 t->parms.o_key,
126878fbfd8aSDavid S. Miller 					 RT_TOS(t->parms.iph.tos),
126978fbfd8aSDavid S. Miller 					 t->parms.link);
1270b23dd4feSDavid S. Miller 		if (IS_ERR(rt))
12711da177e4SLinus Torvalds 			return -EADDRNOTAVAIL;
1272d8d1f30bSChangli Gao 		dev = rt->dst.dev;
12731da177e4SLinus Torvalds 		ip_rt_put(rt);
1274e5ed6399SHerbert Xu 		if (__in_dev_get_rtnl(dev) == NULL)
12751da177e4SLinus Torvalds 			return -EADDRNOTAVAIL;
12761da177e4SLinus Torvalds 		t->mlink = dev->ifindex;
1277e5ed6399SHerbert Xu 		ip_mc_inc_group(__in_dev_get_rtnl(dev), t->parms.iph.daddr);
12781da177e4SLinus Torvalds 	}
12791da177e4SLinus Torvalds 	return 0;
12801da177e4SLinus Torvalds }
12811da177e4SLinus Torvalds 
12821da177e4SLinus Torvalds static int ipgre_close(struct net_device *dev)
12831da177e4SLinus Torvalds {
12842941a486SPatrick McHardy 	struct ip_tunnel *t = netdev_priv(dev);
1285b8c26a33SStephen Hemminger 
1286f97c1e0cSJoe Perches 	if (ipv4_is_multicast(t->parms.iph.daddr) && t->mlink) {
12877fee0ca2SDenis V. Lunev 		struct in_device *in_dev;
1288c346dca1SYOSHIFUJI Hideaki 		in_dev = inetdev_by_index(dev_net(dev), t->mlink);
12898723e1b4SEric Dumazet 		if (in_dev)
12901da177e4SLinus Torvalds 			ip_mc_dec_group(in_dev, t->parms.iph.daddr);
12911da177e4SLinus Torvalds 	}
12921da177e4SLinus Torvalds 	return 0;
12931da177e4SLinus Torvalds }
12941da177e4SLinus Torvalds 
12951da177e4SLinus Torvalds #endif
12961da177e4SLinus Torvalds 
1297b8c26a33SStephen Hemminger static const struct net_device_ops ipgre_netdev_ops = {
1298b8c26a33SStephen Hemminger 	.ndo_init		= ipgre_tunnel_init,
1299b8c26a33SStephen Hemminger 	.ndo_uninit		= ipgre_tunnel_uninit,
1300b8c26a33SStephen Hemminger #ifdef CONFIG_NET_IPGRE_BROADCAST
1301b8c26a33SStephen Hemminger 	.ndo_open		= ipgre_open,
1302b8c26a33SStephen Hemminger 	.ndo_stop		= ipgre_close,
1303b8c26a33SStephen Hemminger #endif
1304b8c26a33SStephen Hemminger 	.ndo_start_xmit		= ipgre_tunnel_xmit,
1305b8c26a33SStephen Hemminger 	.ndo_do_ioctl		= ipgre_tunnel_ioctl,
1306b8c26a33SStephen Hemminger 	.ndo_change_mtu		= ipgre_tunnel_change_mtu,
130787b6d218Sstephen hemminger 	.ndo_get_stats64	= ipgre_get_stats64,
1308b8c26a33SStephen Hemminger };
1309b8c26a33SStephen Hemminger 
1310e985aad7SEric Dumazet static void ipgre_dev_free(struct net_device *dev)
1311e985aad7SEric Dumazet {
131260769a5dSEric Dumazet 	struct ip_tunnel *tunnel = netdev_priv(dev);
131360769a5dSEric Dumazet 
131460769a5dSEric Dumazet 	gro_cells_destroy(&tunnel->gro_cells);
1315e985aad7SEric Dumazet 	free_percpu(dev->tstats);
1316e985aad7SEric Dumazet 	free_netdev(dev);
1317e985aad7SEric Dumazet }
1318e985aad7SEric Dumazet 
13196b78f16eSEric Dumazet #define GRE_FEATURES (NETIF_F_SG |		\
13206b78f16eSEric Dumazet 		      NETIF_F_FRAGLIST |	\
13216b78f16eSEric Dumazet 		      NETIF_F_HIGHDMA |		\
13226b78f16eSEric Dumazet 		      NETIF_F_HW_CSUM)
13236b78f16eSEric Dumazet 
13241da177e4SLinus Torvalds static void ipgre_tunnel_setup(struct net_device *dev)
13251da177e4SLinus Torvalds {
1326b8c26a33SStephen Hemminger 	dev->netdev_ops		= &ipgre_netdev_ops;
1327e985aad7SEric Dumazet 	dev->destructor 	= ipgre_dev_free;
13281da177e4SLinus Torvalds 
13291da177e4SLinus Torvalds 	dev->type		= ARPHRD_IPGRE;
1330c95b819aSHerbert Xu 	dev->needed_headroom 	= LL_MAX_HEADER + sizeof(struct iphdr) + 4;
133146f25dffSKris Katterjohn 	dev->mtu		= ETH_DATA_LEN - sizeof(struct iphdr) - 4;
13321da177e4SLinus Torvalds 	dev->flags		= IFF_NOARP;
13331da177e4SLinus Torvalds 	dev->iflink		= 0;
13341da177e4SLinus Torvalds 	dev->addr_len		= 4;
13350b67ecebSPavel Emelyanov 	dev->features		|= NETIF_F_NETNS_LOCAL;
1336108bfa89SEric Dumazet 	dev->priv_flags		&= ~IFF_XMIT_DST_RELEASE;
13376b78f16eSEric Dumazet 
13386b78f16eSEric Dumazet 	dev->features		|= GRE_FEATURES;
13396b78f16eSEric Dumazet 	dev->hw_features	|= GRE_FEATURES;
13401da177e4SLinus Torvalds }
13411da177e4SLinus Torvalds 
13421da177e4SLinus Torvalds static int ipgre_tunnel_init(struct net_device *dev)
13431da177e4SLinus Torvalds {
13441da177e4SLinus Torvalds 	struct ip_tunnel *tunnel;
13451da177e4SLinus Torvalds 	struct iphdr *iph;
134660769a5dSEric Dumazet 	int err;
13471da177e4SLinus Torvalds 
13482941a486SPatrick McHardy 	tunnel = netdev_priv(dev);
13491da177e4SLinus Torvalds 	iph = &tunnel->parms.iph;
13501da177e4SLinus Torvalds 
13511da177e4SLinus Torvalds 	tunnel->dev = dev;
13521da177e4SLinus Torvalds 	strcpy(tunnel->parms.name, dev->name);
13531da177e4SLinus Torvalds 
13541da177e4SLinus Torvalds 	memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
13551da177e4SLinus Torvalds 	memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
13561da177e4SLinus Torvalds 
13571da177e4SLinus Torvalds 	if (iph->daddr) {
13581da177e4SLinus Torvalds #ifdef CONFIG_NET_IPGRE_BROADCAST
1359f97c1e0cSJoe Perches 		if (ipv4_is_multicast(iph->daddr)) {
13601da177e4SLinus Torvalds 			if (!iph->saddr)
13611da177e4SLinus Torvalds 				return -EINVAL;
13621da177e4SLinus Torvalds 			dev->flags = IFF_BROADCAST;
13633b04dddeSStephen Hemminger 			dev->header_ops = &ipgre_header_ops;
13641da177e4SLinus Torvalds 		}
13651da177e4SLinus Torvalds #endif
1366ee34c1ebSMichal Schmidt 	} else
13676a5f44d7STimo Teras 		dev->header_ops = &ipgre_header_ops;
13681da177e4SLinus Torvalds 
1369e985aad7SEric Dumazet 	dev->tstats = alloc_percpu(struct pcpu_tstats);
1370e985aad7SEric Dumazet 	if (!dev->tstats)
1371e985aad7SEric Dumazet 		return -ENOMEM;
1372e985aad7SEric Dumazet 
137360769a5dSEric Dumazet 	err = gro_cells_init(&tunnel->gro_cells, dev);
137460769a5dSEric Dumazet 	if (err) {
137560769a5dSEric Dumazet 		free_percpu(dev->tstats);
137660769a5dSEric Dumazet 		return err;
137760769a5dSEric Dumazet 	}
137860769a5dSEric Dumazet 
13791da177e4SLinus Torvalds 	return 0;
13801da177e4SLinus Torvalds }
13811da177e4SLinus Torvalds 
1382b8c26a33SStephen Hemminger static void ipgre_fb_tunnel_init(struct net_device *dev)
13831da177e4SLinus Torvalds {
13842941a486SPatrick McHardy 	struct ip_tunnel *tunnel = netdev_priv(dev);
13851da177e4SLinus Torvalds 	struct iphdr *iph = &tunnel->parms.iph;
13861da177e4SLinus Torvalds 
13871da177e4SLinus Torvalds 	tunnel->dev = dev;
13881da177e4SLinus Torvalds 	strcpy(tunnel->parms.name, dev->name);
13891da177e4SLinus Torvalds 
13901da177e4SLinus Torvalds 	iph->version		= 4;
13911da177e4SLinus Torvalds 	iph->protocol		= IPPROTO_GRE;
13921da177e4SLinus Torvalds 	iph->ihl		= 5;
13931da177e4SLinus Torvalds 	tunnel->hlen		= sizeof(struct iphdr) + 4;
13941da177e4SLinus Torvalds 
13951da177e4SLinus Torvalds 	dev_hold(dev);
13961da177e4SLinus Torvalds }
13971da177e4SLinus Torvalds 
13981da177e4SLinus Torvalds 
139900959adeSDmitry Kozlov static const struct gre_protocol ipgre_protocol = {
14001da177e4SLinus Torvalds 	.handler     = ipgre_rcv,
14011da177e4SLinus Torvalds 	.err_handler = ipgre_err,
14021da177e4SLinus Torvalds };
14031da177e4SLinus Torvalds 
1404eef6dd65SEric Dumazet static void ipgre_destroy_tunnels(struct ipgre_net *ign, struct list_head *head)
1405eb8ce741SPavel Emelyanov {
1406eb8ce741SPavel Emelyanov 	int prio;
1407eb8ce741SPavel Emelyanov 
1408eb8ce741SPavel Emelyanov 	for (prio = 0; prio < 4; prio++) {
1409eb8ce741SPavel Emelyanov 		int h;
1410eb8ce741SPavel Emelyanov 		for (h = 0; h < HASH_SIZE; h++) {
14111507850bSEric Dumazet 			struct ip_tunnel *t;
14121507850bSEric Dumazet 
14131507850bSEric Dumazet 			t = rtnl_dereference(ign->tunnels[prio][h]);
1414eef6dd65SEric Dumazet 
1415eef6dd65SEric Dumazet 			while (t != NULL) {
1416eef6dd65SEric Dumazet 				unregister_netdevice_queue(t->dev, head);
14171507850bSEric Dumazet 				t = rtnl_dereference(t->next);
1418eef6dd65SEric Dumazet 			}
1419eb8ce741SPavel Emelyanov 		}
1420eb8ce741SPavel Emelyanov 	}
1421eb8ce741SPavel Emelyanov }
1422eb8ce741SPavel Emelyanov 
14232c8c1e72SAlexey Dobriyan static int __net_init ipgre_init_net(struct net *net)
142459a4c759SPavel Emelyanov {
1425cfb8fbf2SEric W. Biederman 	struct ipgre_net *ign = net_generic(net, ipgre_net_id);
142659a4c759SPavel Emelyanov 	int err;
142759a4c759SPavel Emelyanov 
14287daa0004SPavel Emelyanov 	ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "gre0",
14297daa0004SPavel Emelyanov 					   ipgre_tunnel_setup);
14307daa0004SPavel Emelyanov 	if (!ign->fb_tunnel_dev) {
14317daa0004SPavel Emelyanov 		err = -ENOMEM;
14327daa0004SPavel Emelyanov 		goto err_alloc_dev;
14337daa0004SPavel Emelyanov 	}
1434be77e593SAlexey Dobriyan 	dev_net_set(ign->fb_tunnel_dev, net);
14357daa0004SPavel Emelyanov 
1436b8c26a33SStephen Hemminger 	ipgre_fb_tunnel_init(ign->fb_tunnel_dev);
1437c19e654dSHerbert Xu 	ign->fb_tunnel_dev->rtnl_link_ops = &ipgre_link_ops;
14387daa0004SPavel Emelyanov 
14397daa0004SPavel Emelyanov 	if ((err = register_netdev(ign->fb_tunnel_dev)))
14407daa0004SPavel Emelyanov 		goto err_reg_dev;
14417daa0004SPavel Emelyanov 
14423285ee3bSEric Dumazet 	rcu_assign_pointer(ign->tunnels_wc[0],
14433285ee3bSEric Dumazet 			   netdev_priv(ign->fb_tunnel_dev));
144459a4c759SPavel Emelyanov 	return 0;
144559a4c759SPavel Emelyanov 
14467daa0004SPavel Emelyanov err_reg_dev:
14473285ee3bSEric Dumazet 	ipgre_dev_free(ign->fb_tunnel_dev);
14487daa0004SPavel Emelyanov err_alloc_dev:
144959a4c759SPavel Emelyanov 	return err;
145059a4c759SPavel Emelyanov }
145159a4c759SPavel Emelyanov 
14522c8c1e72SAlexey Dobriyan static void __net_exit ipgre_exit_net(struct net *net)
145359a4c759SPavel Emelyanov {
145459a4c759SPavel Emelyanov 	struct ipgre_net *ign;
1455eef6dd65SEric Dumazet 	LIST_HEAD(list);
145659a4c759SPavel Emelyanov 
145759a4c759SPavel Emelyanov 	ign = net_generic(net, ipgre_net_id);
14587daa0004SPavel Emelyanov 	rtnl_lock();
1459eef6dd65SEric Dumazet 	ipgre_destroy_tunnels(ign, &list);
1460eef6dd65SEric Dumazet 	unregister_netdevice_many(&list);
14617daa0004SPavel Emelyanov 	rtnl_unlock();
146259a4c759SPavel Emelyanov }
146359a4c759SPavel Emelyanov 
146459a4c759SPavel Emelyanov static struct pernet_operations ipgre_net_ops = {
146559a4c759SPavel Emelyanov 	.init = ipgre_init_net,
146659a4c759SPavel Emelyanov 	.exit = ipgre_exit_net,
1467cfb8fbf2SEric W. Biederman 	.id   = &ipgre_net_id,
1468cfb8fbf2SEric W. Biederman 	.size = sizeof(struct ipgre_net),
146959a4c759SPavel Emelyanov };
14701da177e4SLinus Torvalds 
1471c19e654dSHerbert Xu static int ipgre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[])
1472c19e654dSHerbert Xu {
1473c19e654dSHerbert Xu 	__be16 flags;
1474c19e654dSHerbert Xu 
1475c19e654dSHerbert Xu 	if (!data)
1476c19e654dSHerbert Xu 		return 0;
1477c19e654dSHerbert Xu 
1478c19e654dSHerbert Xu 	flags = 0;
1479c19e654dSHerbert Xu 	if (data[IFLA_GRE_IFLAGS])
1480c19e654dSHerbert Xu 		flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1481c19e654dSHerbert Xu 	if (data[IFLA_GRE_OFLAGS])
1482c19e654dSHerbert Xu 		flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1483c19e654dSHerbert Xu 	if (flags & (GRE_VERSION|GRE_ROUTING))
1484c19e654dSHerbert Xu 		return -EINVAL;
1485c19e654dSHerbert Xu 
1486c19e654dSHerbert Xu 	return 0;
1487c19e654dSHerbert Xu }
1488c19e654dSHerbert Xu 
1489e1a80002SHerbert Xu static int ipgre_tap_validate(struct nlattr *tb[], struct nlattr *data[])
1490e1a80002SHerbert Xu {
1491e1a80002SHerbert Xu 	__be32 daddr;
1492e1a80002SHerbert Xu 
1493e1a80002SHerbert Xu 	if (tb[IFLA_ADDRESS]) {
1494e1a80002SHerbert Xu 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1495e1a80002SHerbert Xu 			return -EINVAL;
1496e1a80002SHerbert Xu 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1497e1a80002SHerbert Xu 			return -EADDRNOTAVAIL;
1498e1a80002SHerbert Xu 	}
1499e1a80002SHerbert Xu 
1500e1a80002SHerbert Xu 	if (!data)
1501e1a80002SHerbert Xu 		goto out;
1502e1a80002SHerbert Xu 
1503e1a80002SHerbert Xu 	if (data[IFLA_GRE_REMOTE]) {
1504e1a80002SHerbert Xu 		memcpy(&daddr, nla_data(data[IFLA_GRE_REMOTE]), 4);
1505e1a80002SHerbert Xu 		if (!daddr)
1506e1a80002SHerbert Xu 			return -EINVAL;
1507e1a80002SHerbert Xu 	}
1508e1a80002SHerbert Xu 
1509e1a80002SHerbert Xu out:
1510e1a80002SHerbert Xu 	return ipgre_tunnel_validate(tb, data);
1511e1a80002SHerbert Xu }
1512e1a80002SHerbert Xu 
1513c19e654dSHerbert Xu static void ipgre_netlink_parms(struct nlattr *data[],
1514c19e654dSHerbert Xu 				struct ip_tunnel_parm *parms)
1515c19e654dSHerbert Xu {
15167bb82d92SHerbert Xu 	memset(parms, 0, sizeof(*parms));
1517c19e654dSHerbert Xu 
1518c19e654dSHerbert Xu 	parms->iph.protocol = IPPROTO_GRE;
1519c19e654dSHerbert Xu 
1520c19e654dSHerbert Xu 	if (!data)
1521c19e654dSHerbert Xu 		return;
1522c19e654dSHerbert Xu 
1523c19e654dSHerbert Xu 	if (data[IFLA_GRE_LINK])
1524c19e654dSHerbert Xu 		parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
1525c19e654dSHerbert Xu 
1526c19e654dSHerbert Xu 	if (data[IFLA_GRE_IFLAGS])
1527c19e654dSHerbert Xu 		parms->i_flags = nla_get_be16(data[IFLA_GRE_IFLAGS]);
1528c19e654dSHerbert Xu 
1529c19e654dSHerbert Xu 	if (data[IFLA_GRE_OFLAGS])
1530c19e654dSHerbert Xu 		parms->o_flags = nla_get_be16(data[IFLA_GRE_OFLAGS]);
1531c19e654dSHerbert Xu 
1532c19e654dSHerbert Xu 	if (data[IFLA_GRE_IKEY])
1533c19e654dSHerbert Xu 		parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
1534c19e654dSHerbert Xu 
1535c19e654dSHerbert Xu 	if (data[IFLA_GRE_OKEY])
1536c19e654dSHerbert Xu 		parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
1537c19e654dSHerbert Xu 
1538c19e654dSHerbert Xu 	if (data[IFLA_GRE_LOCAL])
15394d74f8baSPatrick McHardy 		parms->iph.saddr = nla_get_be32(data[IFLA_GRE_LOCAL]);
1540c19e654dSHerbert Xu 
1541c19e654dSHerbert Xu 	if (data[IFLA_GRE_REMOTE])
15424d74f8baSPatrick McHardy 		parms->iph.daddr = nla_get_be32(data[IFLA_GRE_REMOTE]);
1543c19e654dSHerbert Xu 
1544c19e654dSHerbert Xu 	if (data[IFLA_GRE_TTL])
1545c19e654dSHerbert Xu 		parms->iph.ttl = nla_get_u8(data[IFLA_GRE_TTL]);
1546c19e654dSHerbert Xu 
1547c19e654dSHerbert Xu 	if (data[IFLA_GRE_TOS])
1548c19e654dSHerbert Xu 		parms->iph.tos = nla_get_u8(data[IFLA_GRE_TOS]);
1549c19e654dSHerbert Xu 
1550c19e654dSHerbert Xu 	if (!data[IFLA_GRE_PMTUDISC] || nla_get_u8(data[IFLA_GRE_PMTUDISC]))
1551c19e654dSHerbert Xu 		parms->iph.frag_off = htons(IP_DF);
1552c19e654dSHerbert Xu }
1553c19e654dSHerbert Xu 
1554e1a80002SHerbert Xu static int ipgre_tap_init(struct net_device *dev)
1555e1a80002SHerbert Xu {
1556e1a80002SHerbert Xu 	struct ip_tunnel *tunnel;
1557e1a80002SHerbert Xu 
1558e1a80002SHerbert Xu 	tunnel = netdev_priv(dev);
1559e1a80002SHerbert Xu 
1560e1a80002SHerbert Xu 	tunnel->dev = dev;
1561e1a80002SHerbert Xu 	strcpy(tunnel->parms.name, dev->name);
1562e1a80002SHerbert Xu 
1563e1a80002SHerbert Xu 	ipgre_tunnel_bind_dev(dev);
1564e1a80002SHerbert Xu 
1565e985aad7SEric Dumazet 	dev->tstats = alloc_percpu(struct pcpu_tstats);
1566e985aad7SEric Dumazet 	if (!dev->tstats)
1567e985aad7SEric Dumazet 		return -ENOMEM;
1568e985aad7SEric Dumazet 
1569e1a80002SHerbert Xu 	return 0;
1570e1a80002SHerbert Xu }
1571e1a80002SHerbert Xu 
1572b8c26a33SStephen Hemminger static const struct net_device_ops ipgre_tap_netdev_ops = {
1573b8c26a33SStephen Hemminger 	.ndo_init		= ipgre_tap_init,
1574b8c26a33SStephen Hemminger 	.ndo_uninit		= ipgre_tunnel_uninit,
1575b8c26a33SStephen Hemminger 	.ndo_start_xmit		= ipgre_tunnel_xmit,
1576b8c26a33SStephen Hemminger 	.ndo_set_mac_address 	= eth_mac_addr,
1577b8c26a33SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
1578b8c26a33SStephen Hemminger 	.ndo_change_mtu		= ipgre_tunnel_change_mtu,
157987b6d218Sstephen hemminger 	.ndo_get_stats64	= ipgre_get_stats64,
1580b8c26a33SStephen Hemminger };
1581b8c26a33SStephen Hemminger 
1582e1a80002SHerbert Xu static void ipgre_tap_setup(struct net_device *dev)
1583e1a80002SHerbert Xu {
1584e1a80002SHerbert Xu 
1585e1a80002SHerbert Xu 	ether_setup(dev);
1586e1a80002SHerbert Xu 
15872e9526b3SHerbert Xu 	dev->netdev_ops		= &ipgre_tap_netdev_ops;
1588e985aad7SEric Dumazet 	dev->destructor 	= ipgre_dev_free;
1589e1a80002SHerbert Xu 
1590e1a80002SHerbert Xu 	dev->iflink		= 0;
1591e1a80002SHerbert Xu 	dev->features		|= NETIF_F_NETNS_LOCAL;
1592e1a80002SHerbert Xu }
1593e1a80002SHerbert Xu 
159481adee47SEric W. Biederman static int ipgre_newlink(struct net *src_net, struct net_device *dev, struct nlattr *tb[],
1595c19e654dSHerbert Xu 			 struct nlattr *data[])
1596c19e654dSHerbert Xu {
1597c19e654dSHerbert Xu 	struct ip_tunnel *nt;
1598c19e654dSHerbert Xu 	struct net *net = dev_net(dev);
1599c19e654dSHerbert Xu 	struct ipgre_net *ign = net_generic(net, ipgre_net_id);
1600c19e654dSHerbert Xu 	int mtu;
1601c19e654dSHerbert Xu 	int err;
1602c19e654dSHerbert Xu 
1603c19e654dSHerbert Xu 	nt = netdev_priv(dev);
1604c19e654dSHerbert Xu 	ipgre_netlink_parms(data, &nt->parms);
1605c19e654dSHerbert Xu 
1606e1a80002SHerbert Xu 	if (ipgre_tunnel_find(net, &nt->parms, dev->type))
1607c19e654dSHerbert Xu 		return -EEXIST;
1608c19e654dSHerbert Xu 
1609e1a80002SHerbert Xu 	if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
1610f2cedb63SDanny Kukawka 		eth_hw_addr_random(dev);
1611e1a80002SHerbert Xu 
1612c19e654dSHerbert Xu 	mtu = ipgre_tunnel_bind_dev(dev);
1613c19e654dSHerbert Xu 	if (!tb[IFLA_MTU])
1614c19e654dSHerbert Xu 		dev->mtu = mtu;
1615c19e654dSHerbert Xu 
1616b790e01aSEric Dumazet 	/* Can use a lockless transmit, unless we generate output sequences */
1617b790e01aSEric Dumazet 	if (!(nt->parms.o_flags & GRE_SEQ))
1618b790e01aSEric Dumazet 		dev->features |= NETIF_F_LLTX;
1619b790e01aSEric Dumazet 
1620c19e654dSHerbert Xu 	err = register_netdevice(dev);
1621c19e654dSHerbert Xu 	if (err)
1622c19e654dSHerbert Xu 		goto out;
1623c19e654dSHerbert Xu 
1624c19e654dSHerbert Xu 	dev_hold(dev);
1625c19e654dSHerbert Xu 	ipgre_tunnel_link(ign, nt);
1626c19e654dSHerbert Xu 
1627c19e654dSHerbert Xu out:
1628c19e654dSHerbert Xu 	return err;
1629c19e654dSHerbert Xu }
1630c19e654dSHerbert Xu 
1631c19e654dSHerbert Xu static int ipgre_changelink(struct net_device *dev, struct nlattr *tb[],
1632c19e654dSHerbert Xu 			    struct nlattr *data[])
1633c19e654dSHerbert Xu {
1634c19e654dSHerbert Xu 	struct ip_tunnel *t, *nt;
1635c19e654dSHerbert Xu 	struct net *net = dev_net(dev);
1636c19e654dSHerbert Xu 	struct ipgre_net *ign = net_generic(net, ipgre_net_id);
1637c19e654dSHerbert Xu 	struct ip_tunnel_parm p;
1638c19e654dSHerbert Xu 	int mtu;
1639c19e654dSHerbert Xu 
1640c19e654dSHerbert Xu 	if (dev == ign->fb_tunnel_dev)
1641c19e654dSHerbert Xu 		return -EINVAL;
1642c19e654dSHerbert Xu 
1643c19e654dSHerbert Xu 	nt = netdev_priv(dev);
1644c19e654dSHerbert Xu 	ipgre_netlink_parms(data, &p);
1645c19e654dSHerbert Xu 
1646c19e654dSHerbert Xu 	t = ipgre_tunnel_locate(net, &p, 0);
1647c19e654dSHerbert Xu 
1648c19e654dSHerbert Xu 	if (t) {
1649c19e654dSHerbert Xu 		if (t->dev != dev)
1650c19e654dSHerbert Xu 			return -EEXIST;
1651c19e654dSHerbert Xu 	} else {
1652c19e654dSHerbert Xu 		t = nt;
1653c19e654dSHerbert Xu 
16542e9526b3SHerbert Xu 		if (dev->type != ARPHRD_ETHER) {
16551507850bSEric Dumazet 			unsigned int nflags = 0;
16562e9526b3SHerbert Xu 
1657c19e654dSHerbert Xu 			if (ipv4_is_multicast(p.iph.daddr))
1658c19e654dSHerbert Xu 				nflags = IFF_BROADCAST;
1659c19e654dSHerbert Xu 			else if (p.iph.daddr)
1660c19e654dSHerbert Xu 				nflags = IFF_POINTOPOINT;
1661c19e654dSHerbert Xu 
1662c19e654dSHerbert Xu 			if ((dev->flags ^ nflags) &
1663c19e654dSHerbert Xu 			    (IFF_POINTOPOINT | IFF_BROADCAST))
1664c19e654dSHerbert Xu 				return -EINVAL;
16652e9526b3SHerbert Xu 		}
1666c19e654dSHerbert Xu 
1667c19e654dSHerbert Xu 		ipgre_tunnel_unlink(ign, t);
1668c19e654dSHerbert Xu 		t->parms.iph.saddr = p.iph.saddr;
1669c19e654dSHerbert Xu 		t->parms.iph.daddr = p.iph.daddr;
1670c19e654dSHerbert Xu 		t->parms.i_key = p.i_key;
16712e9526b3SHerbert Xu 		if (dev->type != ARPHRD_ETHER) {
1672c19e654dSHerbert Xu 			memcpy(dev->dev_addr, &p.iph.saddr, 4);
1673c19e654dSHerbert Xu 			memcpy(dev->broadcast, &p.iph.daddr, 4);
16742e9526b3SHerbert Xu 		}
1675c19e654dSHerbert Xu 		ipgre_tunnel_link(ign, t);
1676c19e654dSHerbert Xu 		netdev_state_change(dev);
1677c19e654dSHerbert Xu 	}
1678c19e654dSHerbert Xu 
1679c19e654dSHerbert Xu 	t->parms.o_key = p.o_key;
1680c19e654dSHerbert Xu 	t->parms.iph.ttl = p.iph.ttl;
1681c19e654dSHerbert Xu 	t->parms.iph.tos = p.iph.tos;
1682c19e654dSHerbert Xu 	t->parms.iph.frag_off = p.iph.frag_off;
1683c19e654dSHerbert Xu 
1684c19e654dSHerbert Xu 	if (t->parms.link != p.link) {
1685c19e654dSHerbert Xu 		t->parms.link = p.link;
1686c19e654dSHerbert Xu 		mtu = ipgre_tunnel_bind_dev(dev);
1687c19e654dSHerbert Xu 		if (!tb[IFLA_MTU])
1688c19e654dSHerbert Xu 			dev->mtu = mtu;
1689c19e654dSHerbert Xu 		netdev_state_change(dev);
1690c19e654dSHerbert Xu 	}
1691c19e654dSHerbert Xu 
1692c19e654dSHerbert Xu 	return 0;
1693c19e654dSHerbert Xu }
1694c19e654dSHerbert Xu 
1695c19e654dSHerbert Xu static size_t ipgre_get_size(const struct net_device *dev)
1696c19e654dSHerbert Xu {
1697c19e654dSHerbert Xu 	return
1698c19e654dSHerbert Xu 		/* IFLA_GRE_LINK */
1699c19e654dSHerbert Xu 		nla_total_size(4) +
1700c19e654dSHerbert Xu 		/* IFLA_GRE_IFLAGS */
1701c19e654dSHerbert Xu 		nla_total_size(2) +
1702c19e654dSHerbert Xu 		/* IFLA_GRE_OFLAGS */
1703c19e654dSHerbert Xu 		nla_total_size(2) +
1704c19e654dSHerbert Xu 		/* IFLA_GRE_IKEY */
1705c19e654dSHerbert Xu 		nla_total_size(4) +
1706c19e654dSHerbert Xu 		/* IFLA_GRE_OKEY */
1707c19e654dSHerbert Xu 		nla_total_size(4) +
1708c19e654dSHerbert Xu 		/* IFLA_GRE_LOCAL */
1709c19e654dSHerbert Xu 		nla_total_size(4) +
1710c19e654dSHerbert Xu 		/* IFLA_GRE_REMOTE */
1711c19e654dSHerbert Xu 		nla_total_size(4) +
1712c19e654dSHerbert Xu 		/* IFLA_GRE_TTL */
1713c19e654dSHerbert Xu 		nla_total_size(1) +
1714c19e654dSHerbert Xu 		/* IFLA_GRE_TOS */
1715c19e654dSHerbert Xu 		nla_total_size(1) +
1716c19e654dSHerbert Xu 		/* IFLA_GRE_PMTUDISC */
1717c19e654dSHerbert Xu 		nla_total_size(1) +
1718c19e654dSHerbert Xu 		0;
1719c19e654dSHerbert Xu }
1720c19e654dSHerbert Xu 
1721c19e654dSHerbert Xu static int ipgre_fill_info(struct sk_buff *skb, const struct net_device *dev)
1722c19e654dSHerbert Xu {
1723c19e654dSHerbert Xu 	struct ip_tunnel *t = netdev_priv(dev);
1724c19e654dSHerbert Xu 	struct ip_tunnel_parm *p = &t->parms;
1725c19e654dSHerbert Xu 
1726f3756b79SDavid S. Miller 	if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
1727f3756b79SDavid S. Miller 	    nla_put_be16(skb, IFLA_GRE_IFLAGS, p->i_flags) ||
1728f3756b79SDavid S. Miller 	    nla_put_be16(skb, IFLA_GRE_OFLAGS, p->o_flags) ||
1729f3756b79SDavid S. Miller 	    nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
1730f3756b79SDavid S. Miller 	    nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
1731f3756b79SDavid S. Miller 	    nla_put_be32(skb, IFLA_GRE_LOCAL, p->iph.saddr) ||
1732f3756b79SDavid S. Miller 	    nla_put_be32(skb, IFLA_GRE_REMOTE, p->iph.daddr) ||
1733f3756b79SDavid S. Miller 	    nla_put_u8(skb, IFLA_GRE_TTL, p->iph.ttl) ||
1734f3756b79SDavid S. Miller 	    nla_put_u8(skb, IFLA_GRE_TOS, p->iph.tos) ||
1735f3756b79SDavid S. Miller 	    nla_put_u8(skb, IFLA_GRE_PMTUDISC,
1736f3756b79SDavid S. Miller 		       !!(p->iph.frag_off & htons(IP_DF))))
1737f3756b79SDavid S. Miller 		goto nla_put_failure;
1738c19e654dSHerbert Xu 	return 0;
1739c19e654dSHerbert Xu 
1740c19e654dSHerbert Xu nla_put_failure:
1741c19e654dSHerbert Xu 	return -EMSGSIZE;
1742c19e654dSHerbert Xu }
1743c19e654dSHerbert Xu 
1744c19e654dSHerbert Xu static const struct nla_policy ipgre_policy[IFLA_GRE_MAX + 1] = {
1745c19e654dSHerbert Xu 	[IFLA_GRE_LINK]		= { .type = NLA_U32 },
1746c19e654dSHerbert Xu 	[IFLA_GRE_IFLAGS]	= { .type = NLA_U16 },
1747c19e654dSHerbert Xu 	[IFLA_GRE_OFLAGS]	= { .type = NLA_U16 },
1748c19e654dSHerbert Xu 	[IFLA_GRE_IKEY]		= { .type = NLA_U32 },
1749c19e654dSHerbert Xu 	[IFLA_GRE_OKEY]		= { .type = NLA_U32 },
17504d74f8baSPatrick McHardy 	[IFLA_GRE_LOCAL]	= { .len = FIELD_SIZEOF(struct iphdr, saddr) },
17514d74f8baSPatrick McHardy 	[IFLA_GRE_REMOTE]	= { .len = FIELD_SIZEOF(struct iphdr, daddr) },
1752c19e654dSHerbert Xu 	[IFLA_GRE_TTL]		= { .type = NLA_U8 },
1753c19e654dSHerbert Xu 	[IFLA_GRE_TOS]		= { .type = NLA_U8 },
1754c19e654dSHerbert Xu 	[IFLA_GRE_PMTUDISC]	= { .type = NLA_U8 },
1755c19e654dSHerbert Xu };
1756c19e654dSHerbert Xu 
1757c19e654dSHerbert Xu static struct rtnl_link_ops ipgre_link_ops __read_mostly = {
1758c19e654dSHerbert Xu 	.kind		= "gre",
1759c19e654dSHerbert Xu 	.maxtype	= IFLA_GRE_MAX,
1760c19e654dSHerbert Xu 	.policy		= ipgre_policy,
1761c19e654dSHerbert Xu 	.priv_size	= sizeof(struct ip_tunnel),
1762c19e654dSHerbert Xu 	.setup		= ipgre_tunnel_setup,
1763c19e654dSHerbert Xu 	.validate	= ipgre_tunnel_validate,
1764c19e654dSHerbert Xu 	.newlink	= ipgre_newlink,
1765c19e654dSHerbert Xu 	.changelink	= ipgre_changelink,
1766c19e654dSHerbert Xu 	.get_size	= ipgre_get_size,
1767c19e654dSHerbert Xu 	.fill_info	= ipgre_fill_info,
1768c19e654dSHerbert Xu };
1769c19e654dSHerbert Xu 
1770e1a80002SHerbert Xu static struct rtnl_link_ops ipgre_tap_ops __read_mostly = {
1771e1a80002SHerbert Xu 	.kind		= "gretap",
1772e1a80002SHerbert Xu 	.maxtype	= IFLA_GRE_MAX,
1773e1a80002SHerbert Xu 	.policy		= ipgre_policy,
1774e1a80002SHerbert Xu 	.priv_size	= sizeof(struct ip_tunnel),
1775e1a80002SHerbert Xu 	.setup		= ipgre_tap_setup,
1776e1a80002SHerbert Xu 	.validate	= ipgre_tap_validate,
1777e1a80002SHerbert Xu 	.newlink	= ipgre_newlink,
1778e1a80002SHerbert Xu 	.changelink	= ipgre_changelink,
1779e1a80002SHerbert Xu 	.get_size	= ipgre_get_size,
1780e1a80002SHerbert Xu 	.fill_info	= ipgre_fill_info,
1781e1a80002SHerbert Xu };
1782e1a80002SHerbert Xu 
17831da177e4SLinus Torvalds /*
17841da177e4SLinus Torvalds  *	And now the modules code and kernel interface.
17851da177e4SLinus Torvalds  */
17861da177e4SLinus Torvalds 
17871da177e4SLinus Torvalds static int __init ipgre_init(void)
17881da177e4SLinus Torvalds {
17891da177e4SLinus Torvalds 	int err;
17901da177e4SLinus Torvalds 
1791058bd4d2SJoe Perches 	pr_info("GRE over IPv4 tunneling driver\n");
17921da177e4SLinus Torvalds 
1793cfb8fbf2SEric W. Biederman 	err = register_pernet_device(&ipgre_net_ops);
179459a4c759SPavel Emelyanov 	if (err < 0)
1795c2892f02SAlexey Dobriyan 		return err;
1796c2892f02SAlexey Dobriyan 
179700959adeSDmitry Kozlov 	err = gre_add_protocol(&ipgre_protocol, GREPROTO_CISCO);
1798c2892f02SAlexey Dobriyan 	if (err < 0) {
1799058bd4d2SJoe Perches 		pr_info("%s: can't add protocol\n", __func__);
1800c2892f02SAlexey Dobriyan 		goto add_proto_failed;
1801c2892f02SAlexey Dobriyan 	}
18027daa0004SPavel Emelyanov 
1803c19e654dSHerbert Xu 	err = rtnl_link_register(&ipgre_link_ops);
1804c19e654dSHerbert Xu 	if (err < 0)
1805c19e654dSHerbert Xu 		goto rtnl_link_failed;
1806c19e654dSHerbert Xu 
1807e1a80002SHerbert Xu 	err = rtnl_link_register(&ipgre_tap_ops);
1808e1a80002SHerbert Xu 	if (err < 0)
1809e1a80002SHerbert Xu 		goto tap_ops_failed;
1810e1a80002SHerbert Xu 
1811c19e654dSHerbert Xu out:
18127daa0004SPavel Emelyanov 	return err;
1813c19e654dSHerbert Xu 
1814e1a80002SHerbert Xu tap_ops_failed:
1815e1a80002SHerbert Xu 	rtnl_link_unregister(&ipgre_link_ops);
1816c19e654dSHerbert Xu rtnl_link_failed:
181700959adeSDmitry Kozlov 	gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO);
1818c2892f02SAlexey Dobriyan add_proto_failed:
1819c2892f02SAlexey Dobriyan 	unregister_pernet_device(&ipgre_net_ops);
1820c19e654dSHerbert Xu 	goto out;
18211da177e4SLinus Torvalds }
18221da177e4SLinus Torvalds 
1823db44575fSAlexey Kuznetsov static void __exit ipgre_fini(void)
18241da177e4SLinus Torvalds {
1825e1a80002SHerbert Xu 	rtnl_link_unregister(&ipgre_tap_ops);
1826c19e654dSHerbert Xu 	rtnl_link_unregister(&ipgre_link_ops);
182700959adeSDmitry Kozlov 	if (gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO) < 0)
1828058bd4d2SJoe Perches 		pr_info("%s: can't remove protocol\n", __func__);
1829c2892f02SAlexey Dobriyan 	unregister_pernet_device(&ipgre_net_ops);
18301da177e4SLinus Torvalds }
18311da177e4SLinus Torvalds 
18321da177e4SLinus Torvalds module_init(ipgre_init);
18331da177e4SLinus Torvalds module_exit(ipgre_fini);
18341da177e4SLinus Torvalds MODULE_LICENSE("GPL");
18354d74f8baSPatrick McHardy MODULE_ALIAS_RTNL_LINK("gre");
18364d74f8baSPatrick McHardy MODULE_ALIAS_RTNL_LINK("gretap");
18378909c9adSVasiliy Kulikov MODULE_ALIAS_NETDEV("gre0");
1838