xref: /linux/net/ipv4/ip_gre.c (revision 6c734fb8592f6768170e48e7102cb2f0a1bb9759)
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>
40c5441932SPravin B Shelar #include <net/ip_tunnels.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    Alexey Kuznetsov.
1121da177e4SLinus Torvalds  */
1131da177e4SLinus Torvalds 
114eccc1bb8Sstephen hemminger static bool log_ecn_error = true;
115eccc1bb8Sstephen hemminger module_param(log_ecn_error, bool, 0644);
116eccc1bb8Sstephen hemminger MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
117eccc1bb8Sstephen hemminger 
118c19e654dSHerbert Xu static struct rtnl_link_ops ipgre_link_ops __read_mostly;
1191da177e4SLinus Torvalds static int ipgre_tunnel_init(struct net_device *dev);
120eb8ce741SPavel Emelyanov 
121f99189b1SEric Dumazet static int ipgre_net_id __read_mostly;
122c5441932SPravin B Shelar static int gre_tap_net_id __read_mostly;
123eb8ce741SPavel Emelyanov 
124bda7bb46SPravin B Shelar static int ipgre_err(struct sk_buff *skb, u32 info,
125bda7bb46SPravin B Shelar 		     const struct tnl_ptk_info *tpi)
1261da177e4SLinus Torvalds {
1271da177e4SLinus Torvalds 
128071f92d0SRami Rosen 	/* All the routers (except for Linux) return only
1291da177e4SLinus Torvalds 	   8 bytes of packet payload. It means, that precise relaying of
1301da177e4SLinus Torvalds 	   ICMP in the real Internet is absolutely infeasible.
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 	   Moreover, Cisco "wise men" put GRE key to the third word
133c5441932SPravin B Shelar 	   in GRE header. It makes impossible maintaining even soft
134c5441932SPravin B Shelar 	   state for keyed GRE tunnels with enabled checksum. Tell
135c5441932SPravin B Shelar 	   them "thank you".
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds 	   Well, I wonder, rfc1812 was written by Cisco employee,
138bff52857Sstephen hemminger 	   what the hell these idiots break standards established
139bff52857Sstephen hemminger 	   by themselves???
1401da177e4SLinus Torvalds 	   */
141c5441932SPravin B Shelar 	struct net *net = dev_net(skb->dev);
142c5441932SPravin B Shelar 	struct ip_tunnel_net *itn;
14396f5a846SEric Dumazet 	const struct iphdr *iph;
14488c7664fSArnaldo Carvalho de Melo 	const int type = icmp_hdr(skb)->type;
14588c7664fSArnaldo Carvalho de Melo 	const int code = icmp_hdr(skb)->code;
1461da177e4SLinus Torvalds 	struct ip_tunnel *t;
147d2083287Sstephen hemminger 
1481da177e4SLinus Torvalds 	switch (type) {
1491da177e4SLinus Torvalds 	default:
1501da177e4SLinus Torvalds 	case ICMP_PARAMETERPROB:
151bda7bb46SPravin B Shelar 		return PACKET_RCVD;
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds 	case ICMP_DEST_UNREACH:
1541da177e4SLinus Torvalds 		switch (code) {
1551da177e4SLinus Torvalds 		case ICMP_SR_FAILED:
1561da177e4SLinus Torvalds 		case ICMP_PORT_UNREACH:
1571da177e4SLinus Torvalds 			/* Impossible event. */
158bda7bb46SPravin B Shelar 			return PACKET_RCVD;
1591da177e4SLinus Torvalds 		default:
1601da177e4SLinus Torvalds 			/* All others are translated to HOST_UNREACH.
1611da177e4SLinus Torvalds 			   rfc2003 contains "deep thoughts" about NET_UNREACH,
1621da177e4SLinus Torvalds 			   I believe they are just ether pollution. --ANK
1631da177e4SLinus Torvalds 			 */
1641da177e4SLinus Torvalds 			break;
1651da177e4SLinus Torvalds 		}
1661da177e4SLinus Torvalds 		break;
1671da177e4SLinus Torvalds 	case ICMP_TIME_EXCEEDED:
1681da177e4SLinus Torvalds 		if (code != ICMP_EXC_TTL)
169bda7bb46SPravin B Shelar 			return PACKET_RCVD;
1701da177e4SLinus Torvalds 		break;
17155be7a9cSDavid S. Miller 
17255be7a9cSDavid S. Miller 	case ICMP_REDIRECT:
17355be7a9cSDavid S. Miller 		break;
1741da177e4SLinus Torvalds 	}
1751da177e4SLinus Torvalds 
176bda7bb46SPravin B Shelar 	if (tpi->proto == htons(ETH_P_TEB))
177c5441932SPravin B Shelar 		itn = net_generic(net, gre_tap_net_id);
178c5441932SPravin B Shelar 	else
179c5441932SPravin B Shelar 		itn = net_generic(net, ipgre_net_id);
180c5441932SPravin B Shelar 
18196f5a846SEric Dumazet 	iph = (const struct iphdr *)skb->data;
182bda7bb46SPravin B Shelar 	t = ip_tunnel_lookup(itn, skb->dev->ifindex, tpi->flags,
183bda7bb46SPravin B Shelar 			     iph->daddr, iph->saddr, tpi->key);
184d2083287Sstephen hemminger 
18536393395SDavid S. Miller 	if (t == NULL)
186bda7bb46SPravin B Shelar 		return PACKET_REJECT;
18736393395SDavid S. Miller 
18836393395SDavid S. Miller 	if (t->parms.iph.daddr == 0 ||
189f97c1e0cSJoe Perches 	    ipv4_is_multicast(t->parms.iph.daddr))
190bda7bb46SPravin B Shelar 		return PACKET_RCVD;
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
193bda7bb46SPravin B Shelar 		return PACKET_RCVD;
1941da177e4SLinus Torvalds 
195da6185d8SWei Yongjun 	if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
1961da177e4SLinus Torvalds 		t->err_count++;
1971da177e4SLinus Torvalds 	else
1981da177e4SLinus Torvalds 		t->err_count = 1;
1991da177e4SLinus Torvalds 	t->err_time = jiffies;
200bda7bb46SPravin B Shelar 	return PACKET_RCVD;
2011da177e4SLinus Torvalds }
2021da177e4SLinus Torvalds 
203bda7bb46SPravin B Shelar static int ipgre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi)
2041da177e4SLinus Torvalds {
205c5441932SPravin B Shelar 	struct net *net = dev_net(skb->dev);
206c5441932SPravin B Shelar 	struct ip_tunnel_net *itn;
207b71d1d42SEric Dumazet 	const struct iphdr *iph;
2081da177e4SLinus Torvalds 	struct ip_tunnel *tunnel;
2091da177e4SLinus Torvalds 
210bda7bb46SPravin B Shelar 	if (tpi->proto == htons(ETH_P_TEB))
211c5441932SPravin B Shelar 		itn = net_generic(net, gre_tap_net_id);
212c5441932SPravin B Shelar 	else
213c5441932SPravin B Shelar 		itn = net_generic(net, ipgre_net_id);
214c5441932SPravin B Shelar 
215eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
216bda7bb46SPravin B Shelar 	tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, tpi->flags,
217bda7bb46SPravin B Shelar 				  iph->saddr, iph->daddr, tpi->key);
2181da177e4SLinus Torvalds 
219d2083287Sstephen hemminger 	if (tunnel) {
220bda7bb46SPravin B Shelar 		ip_tunnel_rcv(tunnel, skb, tpi, log_ecn_error);
221bda7bb46SPravin B Shelar 		return PACKET_RCVD;
2221da177e4SLinus Torvalds 	}
223bda7bb46SPravin B Shelar 	return PACKET_REJECT;
2241da177e4SLinus Torvalds }
2251da177e4SLinus Torvalds 
226c5441932SPravin B Shelar static void __gre_xmit(struct sk_buff *skb, struct net_device *dev,
227c5441932SPravin B Shelar 		       const struct iphdr *tnl_params,
228c5441932SPravin B Shelar 		       __be16 proto)
229c5441932SPravin B Shelar {
230c5441932SPravin B Shelar 	struct ip_tunnel *tunnel = netdev_priv(dev);
231c5441932SPravin B Shelar 	struct tnl_ptk_info tpi;
232c5441932SPravin B Shelar 
233c5441932SPravin B Shelar 	tpi.flags = tunnel->parms.o_flags;
234c5441932SPravin B Shelar 	tpi.proto = proto;
235c5441932SPravin B Shelar 	tpi.key = tunnel->parms.o_key;
236c5441932SPravin B Shelar 	if (tunnel->parms.o_flags & TUNNEL_SEQ)
237c5441932SPravin B Shelar 		tunnel->o_seqno++;
238c5441932SPravin B Shelar 	tpi.seq = htonl(tunnel->o_seqno);
239cef401deSEric Dumazet 
240c5441932SPravin B Shelar 	/* Push GRE header. */
241752f36daSPravin B Shelar 	gre_build_header(skb, &tpi, tunnel->hlen);
2421da177e4SLinus Torvalds 
243bf3d6a8fSNicolas Dichtel 	ip_tunnel_xmit(skb, dev, tnl_params, tnl_params->protocol);
2441da177e4SLinus Torvalds }
2451da177e4SLinus Torvalds 
246c5441932SPravin B Shelar static netdev_tx_t ipgre_xmit(struct sk_buff *skb,
247c5441932SPravin B Shelar 			      struct net_device *dev)
248ee34c1ebSMichal Schmidt {
249c5441932SPravin B Shelar 	struct ip_tunnel *tunnel = netdev_priv(dev);
250c5441932SPravin B Shelar 	const struct iphdr *tnl_params;
251ee34c1ebSMichal Schmidt 
25245f2e997SPravin B Shelar 	skb = gre_handle_offloads(skb, !!(tunnel->parms.o_flags&TUNNEL_CSUM));
253c5441932SPravin B Shelar 	if (IS_ERR(skb))
254c5441932SPravin B Shelar 		goto out;
255ee34c1ebSMichal Schmidt 
256c5441932SPravin B Shelar 	if (dev->header_ops) {
257c5441932SPravin B Shelar 		/* Need space for new headers */
258c5441932SPravin B Shelar 		if (skb_cow_head(skb, dev->needed_headroom -
2592bac7cb3SChen Gang 				      (tunnel->hlen + sizeof(struct iphdr))))
260c5441932SPravin B Shelar 			goto free_skb;
261ee34c1ebSMichal Schmidt 
262c5441932SPravin B Shelar 		tnl_params = (const struct iphdr *)skb->data;
263cbb1e85fSDavid S. Miller 
264c5441932SPravin B Shelar 		/* Pull skb since ip_tunnel_xmit() needs skb->data pointing
265c5441932SPravin B Shelar 		 * to gre header.
266c5441932SPravin B Shelar 		 */
267c5441932SPravin B Shelar 		skb_pull(skb, tunnel->hlen + sizeof(struct iphdr));
268c5441932SPravin B Shelar 	} else {
269c5441932SPravin B Shelar 		if (skb_cow_head(skb, dev->needed_headroom))
270c5441932SPravin B Shelar 			goto free_skb;
271c5441932SPravin B Shelar 
272c5441932SPravin B Shelar 		tnl_params = &tunnel->parms.iph;
273ee34c1ebSMichal Schmidt 	}
274e1a80002SHerbert Xu 
275c5441932SPravin B Shelar 	__gre_xmit(skb, dev, tnl_params, skb->protocol);
276c5441932SPravin B Shelar 
277c5441932SPravin B Shelar 	return NETDEV_TX_OK;
278c5441932SPravin B Shelar 
279c5441932SPravin B Shelar free_skb:
280c5441932SPravin B Shelar 	dev_kfree_skb(skb);
281c5441932SPravin B Shelar out:
282c5441932SPravin B Shelar 	dev->stats.tx_dropped++;
283c5441932SPravin B Shelar 	return NETDEV_TX_OK;
284ee34c1ebSMichal Schmidt }
285ee34c1ebSMichal Schmidt 
286c5441932SPravin B Shelar static netdev_tx_t gre_tap_xmit(struct sk_buff *skb,
287c5441932SPravin B Shelar 				struct net_device *dev)
288c5441932SPravin B Shelar {
289c5441932SPravin B Shelar 	struct ip_tunnel *tunnel = netdev_priv(dev);
290ee34c1ebSMichal Schmidt 
29145f2e997SPravin B Shelar 	skb = gre_handle_offloads(skb, !!(tunnel->parms.o_flags&TUNNEL_CSUM));
292c5441932SPravin B Shelar 	if (IS_ERR(skb))
293c5441932SPravin B Shelar 		goto out;
294ee34c1ebSMichal Schmidt 
295c5441932SPravin B Shelar 	if (skb_cow_head(skb, dev->needed_headroom))
296c5441932SPravin B Shelar 		goto free_skb;
29742aa9162SHerbert Xu 
298c5441932SPravin B Shelar 	__gre_xmit(skb, dev, &tunnel->parms.iph, htons(ETH_P_TEB));
29942aa9162SHerbert Xu 
300c5441932SPravin B Shelar 	return NETDEV_TX_OK;
301c5441932SPravin B Shelar 
302c5441932SPravin B Shelar free_skb:
303c5441932SPravin B Shelar 	dev_kfree_skb(skb);
304c5441932SPravin B Shelar out:
305c5441932SPravin B Shelar 	dev->stats.tx_dropped++;
306c5441932SPravin B Shelar 	return NETDEV_TX_OK;
30768c33163SPravin B Shelar }
308ee34c1ebSMichal Schmidt 
309c5441932SPravin B Shelar static int ipgre_tunnel_ioctl(struct net_device *dev,
310c5441932SPravin B Shelar 			      struct ifreq *ifr, int cmd)
3111da177e4SLinus Torvalds {
3121da177e4SLinus Torvalds 	int err = 0;
3131da177e4SLinus Torvalds 	struct ip_tunnel_parm p;
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 	if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
316c5441932SPravin B Shelar 		return -EFAULT;
317*6c734fb8SCong Wang 	if (cmd == SIOCADDTUNNEL || cmd == SIOCCHGTUNNEL) {
3181da177e4SLinus Torvalds 		if (p.iph.version != 4 || p.iph.protocol != IPPROTO_GRE ||
3191da177e4SLinus Torvalds 		    p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)) ||
320*6c734fb8SCong Wang 		    ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING)))
3211da177e4SLinus Torvalds 			return -EINVAL;
322c5441932SPravin B Shelar 	}
323c5441932SPravin B Shelar 	p.i_flags = gre_flags_to_tnl_flags(p.i_flags);
324c5441932SPravin B Shelar 	p.o_flags = gre_flags_to_tnl_flags(p.o_flags);
325c5441932SPravin B Shelar 
326c5441932SPravin B Shelar 	err = ip_tunnel_ioctl(dev, &p, cmd);
327c5441932SPravin B Shelar 	if (err)
328c5441932SPravin B Shelar 		return err;
329c5441932SPravin B Shelar 
330c5441932SPravin B Shelar 	p.i_flags = tnl_flags_to_gre_flags(p.i_flags);
331c5441932SPravin B Shelar 	p.o_flags = tnl_flags_to_gre_flags(p.o_flags);
332c5441932SPravin B Shelar 
333c5441932SPravin B Shelar 	if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
334c5441932SPravin B Shelar 		return -EFAULT;
3351da177e4SLinus Torvalds 	return 0;
3361da177e4SLinus Torvalds }
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds /* Nice toy. Unfortunately, useless in real life :-)
3391da177e4SLinus Torvalds    It allows to construct virtual multiprotocol broadcast "LAN"
3401da177e4SLinus Torvalds    over the Internet, provided multicast routing is tuned.
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds    I have no idea was this bicycle invented before me,
3441da177e4SLinus Torvalds    so that I had to set ARPHRD_IPGRE to a random value.
3451da177e4SLinus Torvalds    I have an impression, that Cisco could make something similar,
3461da177e4SLinus Torvalds    but this feature is apparently missing in IOS<=11.2(8).
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds    I set up 10.66.66/24 and fec0:6666:6666::0/96 as virtual networks
3491da177e4SLinus Torvalds    with broadcast 224.66.66.66. If you have access to mbone, play with me :-)
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds    ping -t 255 224.66.66.66
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds    If nobody answers, mbone does not work.
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds    ip tunnel add Universe mode gre remote 224.66.66.66 local <Your_real_addr> ttl 255
3561da177e4SLinus Torvalds    ip addr add 10.66.66.<somewhat>/24 dev Universe
3571da177e4SLinus Torvalds    ifconfig Universe up
3581da177e4SLinus Torvalds    ifconfig Universe add fe80::<Your_real_addr>/10
3591da177e4SLinus Torvalds    ifconfig Universe add fec0:6666:6666::<Your_real_addr>/96
3601da177e4SLinus Torvalds    ftp 10.66.66.66
3611da177e4SLinus Torvalds    ...
3621da177e4SLinus Torvalds    ftp fec0:6666:6666::193.233.7.65
3631da177e4SLinus Torvalds    ...
3641da177e4SLinus Torvalds  */
3653b04dddeSStephen Hemminger static int ipgre_header(struct sk_buff *skb, struct net_device *dev,
3663b04dddeSStephen Hemminger 			unsigned short type,
3671507850bSEric Dumazet 			const void *daddr, const void *saddr, unsigned int len)
3681da177e4SLinus Torvalds {
3692941a486SPatrick McHardy 	struct ip_tunnel *t = netdev_priv(dev);
370c5441932SPravin B Shelar 	struct iphdr *iph;
371c5441932SPravin B Shelar 	struct gre_base_hdr *greh;
372c5441932SPravin B Shelar 
373c5441932SPravin B Shelar 	iph = (struct iphdr *)skb_push(skb, t->hlen + sizeof(*iph));
374c5441932SPravin B Shelar 	greh = (struct gre_base_hdr *)(iph+1);
375c5441932SPravin B Shelar 	greh->flags = tnl_flags_to_gre_flags(t->parms.o_flags);
376c5441932SPravin B Shelar 	greh->protocol = htons(type);
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds 	memcpy(iph, &t->parms.iph, sizeof(struct iphdr));
3791da177e4SLinus Torvalds 
380c5441932SPravin B Shelar 	/* Set the source hardware address. */
3811da177e4SLinus Torvalds 	if (saddr)
3821da177e4SLinus Torvalds 		memcpy(&iph->saddr, saddr, 4);
3836d55cb91STimo Teräs 	if (daddr)
3841da177e4SLinus Torvalds 		memcpy(&iph->daddr, daddr, 4);
3856d55cb91STimo Teräs 	if (iph->daddr)
3861da177e4SLinus Torvalds 		return t->hlen;
3871da177e4SLinus Torvalds 
388c5441932SPravin B Shelar 	return -(t->hlen + sizeof(*iph));
3891da177e4SLinus Torvalds }
3901da177e4SLinus Torvalds 
3916a5f44d7STimo Teras static int ipgre_header_parse(const struct sk_buff *skb, unsigned char *haddr)
3926a5f44d7STimo Teras {
393b71d1d42SEric Dumazet 	const struct iphdr *iph = (const struct iphdr *) skb_mac_header(skb);
3946a5f44d7STimo Teras 	memcpy(haddr, &iph->saddr, 4);
3956a5f44d7STimo Teras 	return 4;
3966a5f44d7STimo Teras }
3976a5f44d7STimo Teras 
3983b04dddeSStephen Hemminger static const struct header_ops ipgre_header_ops = {
3993b04dddeSStephen Hemminger 	.create	= ipgre_header,
4006a5f44d7STimo Teras 	.parse	= ipgre_header_parse,
4013b04dddeSStephen Hemminger };
4023b04dddeSStephen Hemminger 
4036a5f44d7STimo Teras #ifdef CONFIG_NET_IPGRE_BROADCAST
4041da177e4SLinus Torvalds static int ipgre_open(struct net_device *dev)
4051da177e4SLinus Torvalds {
4062941a486SPatrick McHardy 	struct ip_tunnel *t = netdev_priv(dev);
4071da177e4SLinus Torvalds 
408f97c1e0cSJoe Perches 	if (ipv4_is_multicast(t->parms.iph.daddr)) {
409cbb1e85fSDavid S. Miller 		struct flowi4 fl4;
410cbb1e85fSDavid S. Miller 		struct rtable *rt;
411cbb1e85fSDavid S. Miller 
412cbb1e85fSDavid S. Miller 		rt = ip_route_output_gre(dev_net(dev), &fl4,
41378fbfd8aSDavid S. Miller 					 t->parms.iph.daddr,
41478fbfd8aSDavid S. Miller 					 t->parms.iph.saddr,
41578fbfd8aSDavid S. Miller 					 t->parms.o_key,
41678fbfd8aSDavid S. Miller 					 RT_TOS(t->parms.iph.tos),
41778fbfd8aSDavid S. Miller 					 t->parms.link);
418b23dd4feSDavid S. Miller 		if (IS_ERR(rt))
4191da177e4SLinus Torvalds 			return -EADDRNOTAVAIL;
420d8d1f30bSChangli Gao 		dev = rt->dst.dev;
4211da177e4SLinus Torvalds 		ip_rt_put(rt);
422e5ed6399SHerbert Xu 		if (__in_dev_get_rtnl(dev) == NULL)
4231da177e4SLinus Torvalds 			return -EADDRNOTAVAIL;
4241da177e4SLinus Torvalds 		t->mlink = dev->ifindex;
425e5ed6399SHerbert Xu 		ip_mc_inc_group(__in_dev_get_rtnl(dev), t->parms.iph.daddr);
4261da177e4SLinus Torvalds 	}
4271da177e4SLinus Torvalds 	return 0;
4281da177e4SLinus Torvalds }
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds static int ipgre_close(struct net_device *dev)
4311da177e4SLinus Torvalds {
4322941a486SPatrick McHardy 	struct ip_tunnel *t = netdev_priv(dev);
433b8c26a33SStephen Hemminger 
434f97c1e0cSJoe Perches 	if (ipv4_is_multicast(t->parms.iph.daddr) && t->mlink) {
4357fee0ca2SDenis V. Lunev 		struct in_device *in_dev;
436c346dca1SYOSHIFUJI Hideaki 		in_dev = inetdev_by_index(dev_net(dev), t->mlink);
4378723e1b4SEric Dumazet 		if (in_dev)
4381da177e4SLinus Torvalds 			ip_mc_dec_group(in_dev, t->parms.iph.daddr);
4391da177e4SLinus Torvalds 	}
4401da177e4SLinus Torvalds 	return 0;
4411da177e4SLinus Torvalds }
4421da177e4SLinus Torvalds #endif
4431da177e4SLinus Torvalds 
444b8c26a33SStephen Hemminger static const struct net_device_ops ipgre_netdev_ops = {
445b8c26a33SStephen Hemminger 	.ndo_init		= ipgre_tunnel_init,
446c5441932SPravin B Shelar 	.ndo_uninit		= ip_tunnel_uninit,
447b8c26a33SStephen Hemminger #ifdef CONFIG_NET_IPGRE_BROADCAST
448b8c26a33SStephen Hemminger 	.ndo_open		= ipgre_open,
449b8c26a33SStephen Hemminger 	.ndo_stop		= ipgre_close,
450b8c26a33SStephen Hemminger #endif
451c5441932SPravin B Shelar 	.ndo_start_xmit		= ipgre_xmit,
452b8c26a33SStephen Hemminger 	.ndo_do_ioctl		= ipgre_tunnel_ioctl,
453c5441932SPravin B Shelar 	.ndo_change_mtu		= ip_tunnel_change_mtu,
454c5441932SPravin B Shelar 	.ndo_get_stats64	= ip_tunnel_get_stats64,
455b8c26a33SStephen Hemminger };
456b8c26a33SStephen Hemminger 
4576b78f16eSEric Dumazet #define GRE_FEATURES (NETIF_F_SG |		\
4586b78f16eSEric Dumazet 		      NETIF_F_FRAGLIST |	\
4596b78f16eSEric Dumazet 		      NETIF_F_HIGHDMA |		\
4606b78f16eSEric Dumazet 		      NETIF_F_HW_CSUM)
4616b78f16eSEric Dumazet 
4621da177e4SLinus Torvalds static void ipgre_tunnel_setup(struct net_device *dev)
4631da177e4SLinus Torvalds {
464b8c26a33SStephen Hemminger 	dev->netdev_ops		= &ipgre_netdev_ops;
465c5441932SPravin B Shelar 	ip_tunnel_setup(dev, ipgre_net_id);
466c5441932SPravin B Shelar }
4671da177e4SLinus Torvalds 
468c5441932SPravin B Shelar static void __gre_tunnel_init(struct net_device *dev)
469c5441932SPravin B Shelar {
470c5441932SPravin B Shelar 	struct ip_tunnel *tunnel;
471c5441932SPravin B Shelar 
472c5441932SPravin B Shelar 	tunnel = netdev_priv(dev);
473c5441932SPravin B Shelar 	tunnel->hlen = ip_gre_calc_hlen(tunnel->parms.o_flags);
474c5441932SPravin B Shelar 	tunnel->parms.iph.protocol = IPPROTO_GRE;
475c5441932SPravin B Shelar 
476c95b819aSHerbert Xu 	dev->needed_headroom	= LL_MAX_HEADER + sizeof(struct iphdr) + 4;
47746f25dffSKris Katterjohn 	dev->mtu		= ETH_DATA_LEN - sizeof(struct iphdr) - 4;
4786b78f16eSEric Dumazet 
479c5441932SPravin B Shelar 	dev->features		|= NETIF_F_NETNS_LOCAL | GRE_FEATURES;
4806b78f16eSEric Dumazet 	dev->hw_features	|= GRE_FEATURES;
481c5441932SPravin B Shelar 
482c5441932SPravin B Shelar 	if (!(tunnel->parms.o_flags & TUNNEL_SEQ)) {
483c5441932SPravin B Shelar 		/* TCP offload with GRE SEQ is not supported. */
484c5441932SPravin B Shelar 		dev->features    |= NETIF_F_GSO_SOFTWARE;
485c5441932SPravin B Shelar 		dev->hw_features |= NETIF_F_GSO_SOFTWARE;
486c5441932SPravin B Shelar 		/* Can use a lockless transmit, unless we generate
487c5441932SPravin B Shelar 		 * output sequences
488c5441932SPravin B Shelar 		 */
489c5441932SPravin B Shelar 		dev->features |= NETIF_F_LLTX;
490c5441932SPravin B Shelar 	}
4911da177e4SLinus Torvalds }
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds static int ipgre_tunnel_init(struct net_device *dev)
4941da177e4SLinus Torvalds {
495c5441932SPravin B Shelar 	struct ip_tunnel *tunnel = netdev_priv(dev);
496c5441932SPravin B Shelar 	struct iphdr *iph = &tunnel->parms.iph;
4971da177e4SLinus Torvalds 
498c5441932SPravin B Shelar 	__gre_tunnel_init(dev);
4991da177e4SLinus Torvalds 
500c5441932SPravin B Shelar 	memcpy(dev->dev_addr, &iph->saddr, 4);
501c5441932SPravin B Shelar 	memcpy(dev->broadcast, &iph->daddr, 4);
5021da177e4SLinus Torvalds 
503c5441932SPravin B Shelar 	dev->type		= ARPHRD_IPGRE;
504c5441932SPravin B Shelar 	dev->flags		= IFF_NOARP;
505c5441932SPravin B Shelar 	dev->priv_flags		&= ~IFF_XMIT_DST_RELEASE;
506c5441932SPravin B Shelar 	dev->addr_len		= 4;
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds 	if (iph->daddr) {
5091da177e4SLinus Torvalds #ifdef CONFIG_NET_IPGRE_BROADCAST
510f97c1e0cSJoe Perches 		if (ipv4_is_multicast(iph->daddr)) {
5111da177e4SLinus Torvalds 			if (!iph->saddr)
5121da177e4SLinus Torvalds 				return -EINVAL;
5131da177e4SLinus Torvalds 			dev->flags = IFF_BROADCAST;
5143b04dddeSStephen Hemminger 			dev->header_ops = &ipgre_header_ops;
5151da177e4SLinus Torvalds 		}
5161da177e4SLinus Torvalds #endif
517ee34c1ebSMichal Schmidt 	} else
5186a5f44d7STimo Teras 		dev->header_ops = &ipgre_header_ops;
5191da177e4SLinus Torvalds 
520c5441932SPravin B Shelar 	return ip_tunnel_init(dev);
52160769a5dSEric Dumazet }
52260769a5dSEric Dumazet 
523bda7bb46SPravin B Shelar static struct gre_cisco_protocol ipgre_protocol = {
5241da177e4SLinus Torvalds 	.handler        = ipgre_rcv,
5251da177e4SLinus Torvalds 	.err_handler    = ipgre_err,
526bda7bb46SPravin B Shelar 	.priority       = 0,
5271da177e4SLinus Torvalds };
5281da177e4SLinus Torvalds 
5292c8c1e72SAlexey Dobriyan static int __net_init ipgre_init_net(struct net *net)
53059a4c759SPavel Emelyanov {
531c5441932SPravin B Shelar 	return ip_tunnel_init_net(net, ipgre_net_id, &ipgre_link_ops, NULL);
53259a4c759SPavel Emelyanov }
53359a4c759SPavel Emelyanov 
5342c8c1e72SAlexey Dobriyan static void __net_exit ipgre_exit_net(struct net *net)
53559a4c759SPavel Emelyanov {
536c5441932SPravin B Shelar 	struct ip_tunnel_net *itn = net_generic(net, ipgre_net_id);
537c5441932SPravin B Shelar 	ip_tunnel_delete_net(itn);
53859a4c759SPavel Emelyanov }
53959a4c759SPavel Emelyanov 
54059a4c759SPavel Emelyanov static struct pernet_operations ipgre_net_ops = {
54159a4c759SPavel Emelyanov 	.init = ipgre_init_net,
54259a4c759SPavel Emelyanov 	.exit = ipgre_exit_net,
543cfb8fbf2SEric W. Biederman 	.id   = &ipgre_net_id,
544c5441932SPravin B Shelar 	.size = sizeof(struct ip_tunnel_net),
54559a4c759SPavel Emelyanov };
5461da177e4SLinus Torvalds 
547c19e654dSHerbert Xu static int ipgre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[])
548c19e654dSHerbert Xu {
549c19e654dSHerbert Xu 	__be16 flags;
550c19e654dSHerbert Xu 
551c19e654dSHerbert Xu 	if (!data)
552c19e654dSHerbert Xu 		return 0;
553c19e654dSHerbert Xu 
554c19e654dSHerbert Xu 	flags = 0;
555c19e654dSHerbert Xu 	if (data[IFLA_GRE_IFLAGS])
556c19e654dSHerbert Xu 		flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
557c19e654dSHerbert Xu 	if (data[IFLA_GRE_OFLAGS])
558c19e654dSHerbert Xu 		flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
559c19e654dSHerbert Xu 	if (flags & (GRE_VERSION|GRE_ROUTING))
560c19e654dSHerbert Xu 		return -EINVAL;
561c19e654dSHerbert Xu 
562c19e654dSHerbert Xu 	return 0;
563c19e654dSHerbert Xu }
564c19e654dSHerbert Xu 
565e1a80002SHerbert Xu static int ipgre_tap_validate(struct nlattr *tb[], struct nlattr *data[])
566e1a80002SHerbert Xu {
567e1a80002SHerbert Xu 	__be32 daddr;
568e1a80002SHerbert Xu 
569e1a80002SHerbert Xu 	if (tb[IFLA_ADDRESS]) {
570e1a80002SHerbert Xu 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
571e1a80002SHerbert Xu 			return -EINVAL;
572e1a80002SHerbert Xu 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
573e1a80002SHerbert Xu 			return -EADDRNOTAVAIL;
574e1a80002SHerbert Xu 	}
575e1a80002SHerbert Xu 
576e1a80002SHerbert Xu 	if (!data)
577e1a80002SHerbert Xu 		goto out;
578e1a80002SHerbert Xu 
579e1a80002SHerbert Xu 	if (data[IFLA_GRE_REMOTE]) {
580e1a80002SHerbert Xu 		memcpy(&daddr, nla_data(data[IFLA_GRE_REMOTE]), 4);
581e1a80002SHerbert Xu 		if (!daddr)
582e1a80002SHerbert Xu 			return -EINVAL;
583e1a80002SHerbert Xu 	}
584e1a80002SHerbert Xu 
585e1a80002SHerbert Xu out:
586e1a80002SHerbert Xu 	return ipgre_tunnel_validate(tb, data);
587e1a80002SHerbert Xu }
588e1a80002SHerbert Xu 
589c5441932SPravin B Shelar static void ipgre_netlink_parms(struct nlattr *data[], struct nlattr *tb[],
590c19e654dSHerbert Xu 			       struct ip_tunnel_parm *parms)
591c19e654dSHerbert Xu {
5927bb82d92SHerbert Xu 	memset(parms, 0, sizeof(*parms));
593c19e654dSHerbert Xu 
594c19e654dSHerbert Xu 	parms->iph.protocol = IPPROTO_GRE;
595c19e654dSHerbert Xu 
596c19e654dSHerbert Xu 	if (!data)
597c19e654dSHerbert Xu 		return;
598c19e654dSHerbert Xu 
599c19e654dSHerbert Xu 	if (data[IFLA_GRE_LINK])
600c19e654dSHerbert Xu 		parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
601c19e654dSHerbert Xu 
602c19e654dSHerbert Xu 	if (data[IFLA_GRE_IFLAGS])
603c5441932SPravin B Shelar 		parms->i_flags = gre_flags_to_tnl_flags(nla_get_be16(data[IFLA_GRE_IFLAGS]));
604c19e654dSHerbert Xu 
605c19e654dSHerbert Xu 	if (data[IFLA_GRE_OFLAGS])
606c5441932SPravin B Shelar 		parms->o_flags = gre_flags_to_tnl_flags(nla_get_be16(data[IFLA_GRE_OFLAGS]));
607c19e654dSHerbert Xu 
608c19e654dSHerbert Xu 	if (data[IFLA_GRE_IKEY])
609c19e654dSHerbert Xu 		parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
610c19e654dSHerbert Xu 
611c19e654dSHerbert Xu 	if (data[IFLA_GRE_OKEY])
612c19e654dSHerbert Xu 		parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
613c19e654dSHerbert Xu 
614c19e654dSHerbert Xu 	if (data[IFLA_GRE_LOCAL])
6154d74f8baSPatrick McHardy 		parms->iph.saddr = nla_get_be32(data[IFLA_GRE_LOCAL]);
616c19e654dSHerbert Xu 
617c19e654dSHerbert Xu 	if (data[IFLA_GRE_REMOTE])
6184d74f8baSPatrick McHardy 		parms->iph.daddr = nla_get_be32(data[IFLA_GRE_REMOTE]);
619c19e654dSHerbert Xu 
620c19e654dSHerbert Xu 	if (data[IFLA_GRE_TTL])
621c19e654dSHerbert Xu 		parms->iph.ttl = nla_get_u8(data[IFLA_GRE_TTL]);
622c19e654dSHerbert Xu 
623c19e654dSHerbert Xu 	if (data[IFLA_GRE_TOS])
624c19e654dSHerbert Xu 		parms->iph.tos = nla_get_u8(data[IFLA_GRE_TOS]);
625c19e654dSHerbert Xu 
626c19e654dSHerbert Xu 	if (!data[IFLA_GRE_PMTUDISC] || nla_get_u8(data[IFLA_GRE_PMTUDISC]))
627c19e654dSHerbert Xu 		parms->iph.frag_off = htons(IP_DF);
628c19e654dSHerbert Xu }
629c19e654dSHerbert Xu 
630c5441932SPravin B Shelar static int gre_tap_init(struct net_device *dev)
631e1a80002SHerbert Xu {
632c5441932SPravin B Shelar 	__gre_tunnel_init(dev);
633e1a80002SHerbert Xu 
634c5441932SPravin B Shelar 	return ip_tunnel_init(dev);
635e1a80002SHerbert Xu }
636e1a80002SHerbert Xu 
637c5441932SPravin B Shelar static const struct net_device_ops gre_tap_netdev_ops = {
638c5441932SPravin B Shelar 	.ndo_init		= gre_tap_init,
639c5441932SPravin B Shelar 	.ndo_uninit		= ip_tunnel_uninit,
640c5441932SPravin B Shelar 	.ndo_start_xmit		= gre_tap_xmit,
641b8c26a33SStephen Hemminger 	.ndo_set_mac_address 	= eth_mac_addr,
642b8c26a33SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
643c5441932SPravin B Shelar 	.ndo_change_mtu		= ip_tunnel_change_mtu,
644c5441932SPravin B Shelar 	.ndo_get_stats64	= ip_tunnel_get_stats64,
645b8c26a33SStephen Hemminger };
646b8c26a33SStephen Hemminger 
647e1a80002SHerbert Xu static void ipgre_tap_setup(struct net_device *dev)
648e1a80002SHerbert Xu {
649e1a80002SHerbert Xu 	ether_setup(dev);
650c5441932SPravin B Shelar 	dev->netdev_ops		= &gre_tap_netdev_ops;
651c5441932SPravin B Shelar 	ip_tunnel_setup(dev, gre_tap_net_id);
652e1a80002SHerbert Xu }
653e1a80002SHerbert Xu 
654c5441932SPravin B Shelar static int ipgre_newlink(struct net *src_net, struct net_device *dev,
655c5441932SPravin B Shelar 			 struct nlattr *tb[], struct nlattr *data[])
656c19e654dSHerbert Xu {
657c5441932SPravin B Shelar 	struct ip_tunnel_parm p;
658c19e654dSHerbert Xu 
659c5441932SPravin B Shelar 	ipgre_netlink_parms(data, tb, &p);
660c5441932SPravin B Shelar 	return ip_tunnel_newlink(dev, tb, &p);
661c19e654dSHerbert Xu }
662c19e654dSHerbert Xu 
663c19e654dSHerbert Xu static int ipgre_changelink(struct net_device *dev, struct nlattr *tb[],
664c19e654dSHerbert Xu 			    struct nlattr *data[])
665c19e654dSHerbert Xu {
666c19e654dSHerbert Xu 	struct ip_tunnel_parm p;
667c19e654dSHerbert Xu 
668c5441932SPravin B Shelar 	ipgre_netlink_parms(data, tb, &p);
669c5441932SPravin B Shelar 	return ip_tunnel_changelink(dev, tb, &p);
670c19e654dSHerbert Xu }
671c19e654dSHerbert Xu 
672c19e654dSHerbert Xu static size_t ipgre_get_size(const struct net_device *dev)
673c19e654dSHerbert Xu {
674c19e654dSHerbert Xu 	return
675c19e654dSHerbert Xu 		/* IFLA_GRE_LINK */
676c19e654dSHerbert Xu 		nla_total_size(4) +
677c19e654dSHerbert Xu 		/* IFLA_GRE_IFLAGS */
678c19e654dSHerbert Xu 		nla_total_size(2) +
679c19e654dSHerbert Xu 		/* IFLA_GRE_OFLAGS */
680c19e654dSHerbert Xu 		nla_total_size(2) +
681c19e654dSHerbert Xu 		/* IFLA_GRE_IKEY */
682c19e654dSHerbert Xu 		nla_total_size(4) +
683c19e654dSHerbert Xu 		/* IFLA_GRE_OKEY */
684c19e654dSHerbert Xu 		nla_total_size(4) +
685c19e654dSHerbert Xu 		/* IFLA_GRE_LOCAL */
686c19e654dSHerbert Xu 		nla_total_size(4) +
687c19e654dSHerbert Xu 		/* IFLA_GRE_REMOTE */
688c19e654dSHerbert Xu 		nla_total_size(4) +
689c19e654dSHerbert Xu 		/* IFLA_GRE_TTL */
690c19e654dSHerbert Xu 		nla_total_size(1) +
691c19e654dSHerbert Xu 		/* IFLA_GRE_TOS */
692c19e654dSHerbert Xu 		nla_total_size(1) +
693c19e654dSHerbert Xu 		/* IFLA_GRE_PMTUDISC */
694c19e654dSHerbert Xu 		nla_total_size(1) +
695c19e654dSHerbert Xu 		0;
696c19e654dSHerbert Xu }
697c19e654dSHerbert Xu 
698c19e654dSHerbert Xu static int ipgre_fill_info(struct sk_buff *skb, const struct net_device *dev)
699c19e654dSHerbert Xu {
700c19e654dSHerbert Xu 	struct ip_tunnel *t = netdev_priv(dev);
701c19e654dSHerbert Xu 	struct ip_tunnel_parm *p = &t->parms;
702c19e654dSHerbert Xu 
703f3756b79SDavid S. Miller 	if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
704c5441932SPravin B Shelar 	    nla_put_be16(skb, IFLA_GRE_IFLAGS, tnl_flags_to_gre_flags(p->i_flags)) ||
705c5441932SPravin B Shelar 	    nla_put_be16(skb, IFLA_GRE_OFLAGS, tnl_flags_to_gre_flags(p->o_flags)) ||
706f3756b79SDavid S. Miller 	    nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
707f3756b79SDavid S. Miller 	    nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
708f3756b79SDavid S. Miller 	    nla_put_be32(skb, IFLA_GRE_LOCAL, p->iph.saddr) ||
709f3756b79SDavid S. Miller 	    nla_put_be32(skb, IFLA_GRE_REMOTE, p->iph.daddr) ||
710f3756b79SDavid S. Miller 	    nla_put_u8(skb, IFLA_GRE_TTL, p->iph.ttl) ||
711f3756b79SDavid S. Miller 	    nla_put_u8(skb, IFLA_GRE_TOS, p->iph.tos) ||
712f3756b79SDavid S. Miller 	    nla_put_u8(skb, IFLA_GRE_PMTUDISC,
713f3756b79SDavid S. Miller 		       !!(p->iph.frag_off & htons(IP_DF))))
714f3756b79SDavid S. Miller 		goto nla_put_failure;
715c19e654dSHerbert Xu 	return 0;
716c19e654dSHerbert Xu 
717c19e654dSHerbert Xu nla_put_failure:
718c19e654dSHerbert Xu 	return -EMSGSIZE;
719c19e654dSHerbert Xu }
720c19e654dSHerbert Xu 
721c19e654dSHerbert Xu static const struct nla_policy ipgre_policy[IFLA_GRE_MAX + 1] = {
722c19e654dSHerbert Xu 	[IFLA_GRE_LINK]		= { .type = NLA_U32 },
723c19e654dSHerbert Xu 	[IFLA_GRE_IFLAGS]	= { .type = NLA_U16 },
724c19e654dSHerbert Xu 	[IFLA_GRE_OFLAGS]	= { .type = NLA_U16 },
725c19e654dSHerbert Xu 	[IFLA_GRE_IKEY]		= { .type = NLA_U32 },
726c19e654dSHerbert Xu 	[IFLA_GRE_OKEY]		= { .type = NLA_U32 },
7274d74f8baSPatrick McHardy 	[IFLA_GRE_LOCAL]	= { .len = FIELD_SIZEOF(struct iphdr, saddr) },
7284d74f8baSPatrick McHardy 	[IFLA_GRE_REMOTE]	= { .len = FIELD_SIZEOF(struct iphdr, daddr) },
729c19e654dSHerbert Xu 	[IFLA_GRE_TTL]		= { .type = NLA_U8 },
730c19e654dSHerbert Xu 	[IFLA_GRE_TOS]		= { .type = NLA_U8 },
731c19e654dSHerbert Xu 	[IFLA_GRE_PMTUDISC]	= { .type = NLA_U8 },
732c19e654dSHerbert Xu };
733c19e654dSHerbert Xu 
734c19e654dSHerbert Xu static struct rtnl_link_ops ipgre_link_ops __read_mostly = {
735c19e654dSHerbert Xu 	.kind		= "gre",
736c19e654dSHerbert Xu 	.maxtype	= IFLA_GRE_MAX,
737c19e654dSHerbert Xu 	.policy		= ipgre_policy,
738c19e654dSHerbert Xu 	.priv_size	= sizeof(struct ip_tunnel),
739c19e654dSHerbert Xu 	.setup		= ipgre_tunnel_setup,
740c19e654dSHerbert Xu 	.validate	= ipgre_tunnel_validate,
741c19e654dSHerbert Xu 	.newlink	= ipgre_newlink,
742c19e654dSHerbert Xu 	.changelink	= ipgre_changelink,
743c5441932SPravin B Shelar 	.dellink	= ip_tunnel_dellink,
744c19e654dSHerbert Xu 	.get_size	= ipgre_get_size,
745c19e654dSHerbert Xu 	.fill_info	= ipgre_fill_info,
746c19e654dSHerbert Xu };
747c19e654dSHerbert Xu 
748e1a80002SHerbert Xu static struct rtnl_link_ops ipgre_tap_ops __read_mostly = {
749e1a80002SHerbert Xu 	.kind		= "gretap",
750e1a80002SHerbert Xu 	.maxtype	= IFLA_GRE_MAX,
751e1a80002SHerbert Xu 	.policy		= ipgre_policy,
752e1a80002SHerbert Xu 	.priv_size	= sizeof(struct ip_tunnel),
753e1a80002SHerbert Xu 	.setup		= ipgre_tap_setup,
754e1a80002SHerbert Xu 	.validate	= ipgre_tap_validate,
755e1a80002SHerbert Xu 	.newlink	= ipgre_newlink,
756e1a80002SHerbert Xu 	.changelink	= ipgre_changelink,
757c5441932SPravin B Shelar 	.dellink	= ip_tunnel_dellink,
758e1a80002SHerbert Xu 	.get_size	= ipgre_get_size,
759e1a80002SHerbert Xu 	.fill_info	= ipgre_fill_info,
760e1a80002SHerbert Xu };
761e1a80002SHerbert Xu 
762c5441932SPravin B Shelar static int __net_init ipgre_tap_init_net(struct net *net)
763c5441932SPravin B Shelar {
764c5441932SPravin B Shelar 	return ip_tunnel_init_net(net, gre_tap_net_id, &ipgre_tap_ops, NULL);
765c5441932SPravin B Shelar }
766c5441932SPravin B Shelar 
767c5441932SPravin B Shelar static void __net_exit ipgre_tap_exit_net(struct net *net)
768c5441932SPravin B Shelar {
769c5441932SPravin B Shelar 	struct ip_tunnel_net *itn = net_generic(net, gre_tap_net_id);
770c5441932SPravin B Shelar 	ip_tunnel_delete_net(itn);
771c5441932SPravin B Shelar }
772c5441932SPravin B Shelar 
773c5441932SPravin B Shelar static struct pernet_operations ipgre_tap_net_ops = {
774c5441932SPravin B Shelar 	.init = ipgre_tap_init_net,
775c5441932SPravin B Shelar 	.exit = ipgre_tap_exit_net,
776c5441932SPravin B Shelar 	.id   = &gre_tap_net_id,
777c5441932SPravin B Shelar 	.size = sizeof(struct ip_tunnel_net),
778c5441932SPravin B Shelar };
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds static int __init ipgre_init(void)
7811da177e4SLinus Torvalds {
7821da177e4SLinus Torvalds 	int err;
7831da177e4SLinus Torvalds 
784058bd4d2SJoe Perches 	pr_info("GRE over IPv4 tunneling driver\n");
7851da177e4SLinus Torvalds 
786cfb8fbf2SEric W. Biederman 	err = register_pernet_device(&ipgre_net_ops);
78759a4c759SPavel Emelyanov 	if (err < 0)
788c2892f02SAlexey Dobriyan 		return err;
789c2892f02SAlexey Dobriyan 
790c5441932SPravin B Shelar 	err = register_pernet_device(&ipgre_tap_net_ops);
791c5441932SPravin B Shelar 	if (err < 0)
792c5441932SPravin B Shelar 		goto pnet_tap_faied;
793c5441932SPravin B Shelar 
794bda7bb46SPravin B Shelar 	err = gre_cisco_register(&ipgre_protocol);
795c2892f02SAlexey Dobriyan 	if (err < 0) {
796058bd4d2SJoe Perches 		pr_info("%s: can't add protocol\n", __func__);
797c2892f02SAlexey Dobriyan 		goto add_proto_failed;
798c2892f02SAlexey Dobriyan 	}
7997daa0004SPavel Emelyanov 
800c19e654dSHerbert Xu 	err = rtnl_link_register(&ipgre_link_ops);
801c19e654dSHerbert Xu 	if (err < 0)
802c19e654dSHerbert Xu 		goto rtnl_link_failed;
803c19e654dSHerbert Xu 
804e1a80002SHerbert Xu 	err = rtnl_link_register(&ipgre_tap_ops);
805e1a80002SHerbert Xu 	if (err < 0)
806e1a80002SHerbert Xu 		goto tap_ops_failed;
807e1a80002SHerbert Xu 
808c5441932SPravin B Shelar 	return 0;
809c19e654dSHerbert Xu 
810e1a80002SHerbert Xu tap_ops_failed:
811e1a80002SHerbert Xu 	rtnl_link_unregister(&ipgre_link_ops);
812c19e654dSHerbert Xu rtnl_link_failed:
813bda7bb46SPravin B Shelar 	gre_cisco_unregister(&ipgre_protocol);
814c2892f02SAlexey Dobriyan add_proto_failed:
815c5441932SPravin B Shelar 	unregister_pernet_device(&ipgre_tap_net_ops);
816c5441932SPravin B Shelar pnet_tap_faied:
817c2892f02SAlexey Dobriyan 	unregister_pernet_device(&ipgre_net_ops);
818c5441932SPravin B Shelar 	return err;
8191da177e4SLinus Torvalds }
8201da177e4SLinus Torvalds 
821db44575fSAlexey Kuznetsov static void __exit ipgre_fini(void)
8221da177e4SLinus Torvalds {
823e1a80002SHerbert Xu 	rtnl_link_unregister(&ipgre_tap_ops);
824c19e654dSHerbert Xu 	rtnl_link_unregister(&ipgre_link_ops);
825bda7bb46SPravin B Shelar 	gre_cisco_unregister(&ipgre_protocol);
826c5441932SPravin B Shelar 	unregister_pernet_device(&ipgre_tap_net_ops);
827c2892f02SAlexey Dobriyan 	unregister_pernet_device(&ipgre_net_ops);
8281da177e4SLinus Torvalds }
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds module_init(ipgre_init);
8311da177e4SLinus Torvalds module_exit(ipgre_fini);
8321da177e4SLinus Torvalds MODULE_LICENSE("GPL");
8334d74f8baSPatrick McHardy MODULE_ALIAS_RTNL_LINK("gre");
8344d74f8baSPatrick McHardy MODULE_ALIAS_RTNL_LINK("gretap");
8358909c9adSVasiliy Kulikov MODULE_ALIAS_NETDEV("gre0");
836c5441932SPravin B Shelar MODULE_ALIAS_NETDEV("gretap0");
837