xref: /linux/net/ipv4/ip_gre.c (revision f8c1b7ce00254a5bb75d5b5e5ef1601326a0e08e)
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 
181c0c0c50fSDuan Jiong 	iph = (const struct iphdr *)(icmp_hdr(skb) + 1);
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) {
2200e3da5bbSTimo Teräs 		skb_pop_mac_header(skb);
221bda7bb46SPravin B Shelar 		ip_tunnel_rcv(tunnel, skb, tpi, log_ecn_error);
222bda7bb46SPravin B Shelar 		return PACKET_RCVD;
2231da177e4SLinus Torvalds 	}
224bda7bb46SPravin B Shelar 	return PACKET_REJECT;
2251da177e4SLinus Torvalds }
2261da177e4SLinus Torvalds 
227c5441932SPravin B Shelar static void __gre_xmit(struct sk_buff *skb, struct net_device *dev,
228c5441932SPravin B Shelar 		       const struct iphdr *tnl_params,
229c5441932SPravin B Shelar 		       __be16 proto)
230c5441932SPravin B Shelar {
231c5441932SPravin B Shelar 	struct ip_tunnel *tunnel = netdev_priv(dev);
232c5441932SPravin B Shelar 	struct tnl_ptk_info tpi;
233c5441932SPravin B Shelar 
234c5441932SPravin B Shelar 	tpi.flags = tunnel->parms.o_flags;
235c5441932SPravin B Shelar 	tpi.proto = proto;
236c5441932SPravin B Shelar 	tpi.key = tunnel->parms.o_key;
237c5441932SPravin B Shelar 	if (tunnel->parms.o_flags & TUNNEL_SEQ)
238c5441932SPravin B Shelar 		tunnel->o_seqno++;
239c5441932SPravin B Shelar 	tpi.seq = htonl(tunnel->o_seqno);
240cef401deSEric Dumazet 
241c5441932SPravin B Shelar 	/* Push GRE header. */
242752f36daSPravin B Shelar 	gre_build_header(skb, &tpi, tunnel->hlen);
2431da177e4SLinus Torvalds 
244bf3d6a8fSNicolas Dichtel 	ip_tunnel_xmit(skb, dev, tnl_params, tnl_params->protocol);
2451da177e4SLinus Torvalds }
2461da177e4SLinus Torvalds 
247c5441932SPravin B Shelar static netdev_tx_t ipgre_xmit(struct sk_buff *skb,
248c5441932SPravin B Shelar 			      struct net_device *dev)
249ee34c1ebSMichal Schmidt {
250c5441932SPravin B Shelar 	struct ip_tunnel *tunnel = netdev_priv(dev);
251c5441932SPravin B Shelar 	const struct iphdr *tnl_params;
252ee34c1ebSMichal Schmidt 
25345f2e997SPravin B Shelar 	skb = gre_handle_offloads(skb, !!(tunnel->parms.o_flags&TUNNEL_CSUM));
254c5441932SPravin B Shelar 	if (IS_ERR(skb))
255c5441932SPravin B Shelar 		goto out;
256ee34c1ebSMichal Schmidt 
257c5441932SPravin B Shelar 	if (dev->header_ops) {
258c5441932SPravin B Shelar 		/* Need space for new headers */
259c5441932SPravin B Shelar 		if (skb_cow_head(skb, dev->needed_headroom -
2602bac7cb3SChen Gang 				      (tunnel->hlen + sizeof(struct iphdr))))
261c5441932SPravin B Shelar 			goto free_skb;
262ee34c1ebSMichal Schmidt 
263c5441932SPravin B Shelar 		tnl_params = (const struct iphdr *)skb->data;
264cbb1e85fSDavid S. Miller 
265c5441932SPravin B Shelar 		/* Pull skb since ip_tunnel_xmit() needs skb->data pointing
266c5441932SPravin B Shelar 		 * to gre header.
267c5441932SPravin B Shelar 		 */
268c5441932SPravin B Shelar 		skb_pull(skb, tunnel->hlen + sizeof(struct iphdr));
269c5441932SPravin B Shelar 	} else {
270c5441932SPravin B Shelar 		if (skb_cow_head(skb, dev->needed_headroom))
271c5441932SPravin B Shelar 			goto free_skb;
272c5441932SPravin B Shelar 
273c5441932SPravin B Shelar 		tnl_params = &tunnel->parms.iph;
274ee34c1ebSMichal Schmidt 	}
275e1a80002SHerbert Xu 
276c5441932SPravin B Shelar 	__gre_xmit(skb, dev, tnl_params, skb->protocol);
277c5441932SPravin B Shelar 
278c5441932SPravin B Shelar 	return NETDEV_TX_OK;
279c5441932SPravin B Shelar 
280c5441932SPravin B Shelar free_skb:
2813acfa1e7SEric Dumazet 	kfree_skb(skb);
282c5441932SPravin B Shelar out:
283c5441932SPravin B Shelar 	dev->stats.tx_dropped++;
284c5441932SPravin B Shelar 	return NETDEV_TX_OK;
285ee34c1ebSMichal Schmidt }
286ee34c1ebSMichal Schmidt 
287c5441932SPravin B Shelar static netdev_tx_t gre_tap_xmit(struct sk_buff *skb,
288c5441932SPravin B Shelar 				struct net_device *dev)
289c5441932SPravin B Shelar {
290c5441932SPravin B Shelar 	struct ip_tunnel *tunnel = netdev_priv(dev);
291ee34c1ebSMichal Schmidt 
29245f2e997SPravin B Shelar 	skb = gre_handle_offloads(skb, !!(tunnel->parms.o_flags&TUNNEL_CSUM));
293c5441932SPravin B Shelar 	if (IS_ERR(skb))
294c5441932SPravin B Shelar 		goto out;
295ee34c1ebSMichal Schmidt 
296c5441932SPravin B Shelar 	if (skb_cow_head(skb, dev->needed_headroom))
297c5441932SPravin B Shelar 		goto free_skb;
29842aa9162SHerbert Xu 
299c5441932SPravin B Shelar 	__gre_xmit(skb, dev, &tunnel->parms.iph, htons(ETH_P_TEB));
30042aa9162SHerbert Xu 
301c5441932SPravin B Shelar 	return NETDEV_TX_OK;
302c5441932SPravin B Shelar 
303c5441932SPravin B Shelar free_skb:
3043acfa1e7SEric Dumazet 	kfree_skb(skb);
305c5441932SPravin B Shelar out:
306c5441932SPravin B Shelar 	dev->stats.tx_dropped++;
307c5441932SPravin B Shelar 	return NETDEV_TX_OK;
30868c33163SPravin B Shelar }
309ee34c1ebSMichal Schmidt 
310c5441932SPravin B Shelar static int ipgre_tunnel_ioctl(struct net_device *dev,
311c5441932SPravin B Shelar 			      struct ifreq *ifr, int cmd)
3121da177e4SLinus Torvalds {
3131da177e4SLinus Torvalds 	int err = 0;
3141da177e4SLinus Torvalds 	struct ip_tunnel_parm p;
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds 	if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
317c5441932SPravin B Shelar 		return -EFAULT;
3186c734fb8SCong Wang 	if (cmd == SIOCADDTUNNEL || cmd == SIOCCHGTUNNEL) {
3191da177e4SLinus Torvalds 		if (p.iph.version != 4 || p.iph.protocol != IPPROTO_GRE ||
3201da177e4SLinus Torvalds 		    p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)) ||
3216c734fb8SCong Wang 		    ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING)))
3221da177e4SLinus Torvalds 			return -EINVAL;
323c5441932SPravin B Shelar 	}
324c5441932SPravin B Shelar 	p.i_flags = gre_flags_to_tnl_flags(p.i_flags);
325c5441932SPravin B Shelar 	p.o_flags = gre_flags_to_tnl_flags(p.o_flags);
326c5441932SPravin B Shelar 
327c5441932SPravin B Shelar 	err = ip_tunnel_ioctl(dev, &p, cmd);
328c5441932SPravin B Shelar 	if (err)
329c5441932SPravin B Shelar 		return err;
330c5441932SPravin B Shelar 
331c5441932SPravin B Shelar 	p.i_flags = tnl_flags_to_gre_flags(p.i_flags);
332c5441932SPravin B Shelar 	p.o_flags = tnl_flags_to_gre_flags(p.o_flags);
333c5441932SPravin B Shelar 
334c5441932SPravin B Shelar 	if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
335c5441932SPravin B Shelar 		return -EFAULT;
3361da177e4SLinus Torvalds 	return 0;
3371da177e4SLinus Torvalds }
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds /* Nice toy. Unfortunately, useless in real life :-)
3401da177e4SLinus Torvalds    It allows to construct virtual multiprotocol broadcast "LAN"
3411da177e4SLinus Torvalds    over the Internet, provided multicast routing is tuned.
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds    I have no idea was this bicycle invented before me,
3451da177e4SLinus Torvalds    so that I had to set ARPHRD_IPGRE to a random value.
3461da177e4SLinus Torvalds    I have an impression, that Cisco could make something similar,
3471da177e4SLinus Torvalds    but this feature is apparently missing in IOS<=11.2(8).
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds    I set up 10.66.66/24 and fec0:6666:6666::0/96 as virtual networks
3501da177e4SLinus Torvalds    with broadcast 224.66.66.66. If you have access to mbone, play with me :-)
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds    ping -t 255 224.66.66.66
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds    If nobody answers, mbone does not work.
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds    ip tunnel add Universe mode gre remote 224.66.66.66 local <Your_real_addr> ttl 255
3571da177e4SLinus Torvalds    ip addr add 10.66.66.<somewhat>/24 dev Universe
3581da177e4SLinus Torvalds    ifconfig Universe up
3591da177e4SLinus Torvalds    ifconfig Universe add fe80::<Your_real_addr>/10
3601da177e4SLinus Torvalds    ifconfig Universe add fec0:6666:6666::<Your_real_addr>/96
3611da177e4SLinus Torvalds    ftp 10.66.66.66
3621da177e4SLinus Torvalds    ...
3631da177e4SLinus Torvalds    ftp fec0:6666:6666::193.233.7.65
3641da177e4SLinus Torvalds    ...
3651da177e4SLinus Torvalds  */
3663b04dddeSStephen Hemminger static int ipgre_header(struct sk_buff *skb, struct net_device *dev,
3673b04dddeSStephen Hemminger 			unsigned short type,
3681507850bSEric Dumazet 			const void *daddr, const void *saddr, unsigned int len)
3691da177e4SLinus Torvalds {
3702941a486SPatrick McHardy 	struct ip_tunnel *t = netdev_priv(dev);
371c5441932SPravin B Shelar 	struct iphdr *iph;
372c5441932SPravin B Shelar 	struct gre_base_hdr *greh;
373c5441932SPravin B Shelar 
374c5441932SPravin B Shelar 	iph = (struct iphdr *)skb_push(skb, t->hlen + sizeof(*iph));
375c5441932SPravin B Shelar 	greh = (struct gre_base_hdr *)(iph+1);
376c5441932SPravin B Shelar 	greh->flags = tnl_flags_to_gre_flags(t->parms.o_flags);
377c5441932SPravin B Shelar 	greh->protocol = htons(type);
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 	memcpy(iph, &t->parms.iph, sizeof(struct iphdr));
3801da177e4SLinus Torvalds 
381c5441932SPravin B Shelar 	/* Set the source hardware address. */
3821da177e4SLinus Torvalds 	if (saddr)
3831da177e4SLinus Torvalds 		memcpy(&iph->saddr, saddr, 4);
3846d55cb91STimo Teräs 	if (daddr)
3851da177e4SLinus Torvalds 		memcpy(&iph->daddr, daddr, 4);
3866d55cb91STimo Teräs 	if (iph->daddr)
38777a482bdSTimo Teräs 		return t->hlen + sizeof(*iph);
3881da177e4SLinus Torvalds 
389c5441932SPravin B Shelar 	return -(t->hlen + sizeof(*iph));
3901da177e4SLinus Torvalds }
3911da177e4SLinus Torvalds 
3926a5f44d7STimo Teras static int ipgre_header_parse(const struct sk_buff *skb, unsigned char *haddr)
3936a5f44d7STimo Teras {
394b71d1d42SEric Dumazet 	const struct iphdr *iph = (const struct iphdr *) skb_mac_header(skb);
3956a5f44d7STimo Teras 	memcpy(haddr, &iph->saddr, 4);
3966a5f44d7STimo Teras 	return 4;
3976a5f44d7STimo Teras }
3986a5f44d7STimo Teras 
3993b04dddeSStephen Hemminger static const struct header_ops ipgre_header_ops = {
4003b04dddeSStephen Hemminger 	.create	= ipgre_header,
4016a5f44d7STimo Teras 	.parse	= ipgre_header_parse,
4023b04dddeSStephen Hemminger };
4033b04dddeSStephen Hemminger 
4046a5f44d7STimo Teras #ifdef CONFIG_NET_IPGRE_BROADCAST
4051da177e4SLinus Torvalds static int ipgre_open(struct net_device *dev)
4061da177e4SLinus Torvalds {
4072941a486SPatrick McHardy 	struct ip_tunnel *t = netdev_priv(dev);
4081da177e4SLinus Torvalds 
409f97c1e0cSJoe Perches 	if (ipv4_is_multicast(t->parms.iph.daddr)) {
410cbb1e85fSDavid S. Miller 		struct flowi4 fl4;
411cbb1e85fSDavid S. Miller 		struct rtable *rt;
412cbb1e85fSDavid S. Miller 
413b57708adSNicolas Dichtel 		rt = ip_route_output_gre(t->net, &fl4,
41478fbfd8aSDavid S. Miller 					 t->parms.iph.daddr,
41578fbfd8aSDavid S. Miller 					 t->parms.iph.saddr,
41678fbfd8aSDavid S. Miller 					 t->parms.o_key,
41778fbfd8aSDavid S. Miller 					 RT_TOS(t->parms.iph.tos),
41878fbfd8aSDavid S. Miller 					 t->parms.link);
419b23dd4feSDavid S. Miller 		if (IS_ERR(rt))
4201da177e4SLinus Torvalds 			return -EADDRNOTAVAIL;
421d8d1f30bSChangli Gao 		dev = rt->dst.dev;
4221da177e4SLinus Torvalds 		ip_rt_put(rt);
423e5ed6399SHerbert Xu 		if (__in_dev_get_rtnl(dev) == NULL)
4241da177e4SLinus Torvalds 			return -EADDRNOTAVAIL;
4251da177e4SLinus Torvalds 		t->mlink = dev->ifindex;
426e5ed6399SHerbert Xu 		ip_mc_inc_group(__in_dev_get_rtnl(dev), t->parms.iph.daddr);
4271da177e4SLinus Torvalds 	}
4281da177e4SLinus Torvalds 	return 0;
4291da177e4SLinus Torvalds }
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds static int ipgre_close(struct net_device *dev)
4321da177e4SLinus Torvalds {
4332941a486SPatrick McHardy 	struct ip_tunnel *t = netdev_priv(dev);
434b8c26a33SStephen Hemminger 
435f97c1e0cSJoe Perches 	if (ipv4_is_multicast(t->parms.iph.daddr) && t->mlink) {
4367fee0ca2SDenis V. Lunev 		struct in_device *in_dev;
437b57708adSNicolas Dichtel 		in_dev = inetdev_by_index(t->net, t->mlink);
4388723e1b4SEric Dumazet 		if (in_dev)
4391da177e4SLinus Torvalds 			ip_mc_dec_group(in_dev, t->parms.iph.daddr);
4401da177e4SLinus Torvalds 	}
4411da177e4SLinus Torvalds 	return 0;
4421da177e4SLinus Torvalds }
4431da177e4SLinus Torvalds #endif
4441da177e4SLinus Torvalds 
445b8c26a33SStephen Hemminger static const struct net_device_ops ipgre_netdev_ops = {
446b8c26a33SStephen Hemminger 	.ndo_init		= ipgre_tunnel_init,
447c5441932SPravin B Shelar 	.ndo_uninit		= ip_tunnel_uninit,
448b8c26a33SStephen Hemminger #ifdef CONFIG_NET_IPGRE_BROADCAST
449b8c26a33SStephen Hemminger 	.ndo_open		= ipgre_open,
450b8c26a33SStephen Hemminger 	.ndo_stop		= ipgre_close,
451b8c26a33SStephen Hemminger #endif
452c5441932SPravin B Shelar 	.ndo_start_xmit		= ipgre_xmit,
453b8c26a33SStephen Hemminger 	.ndo_do_ioctl		= ipgre_tunnel_ioctl,
454c5441932SPravin B Shelar 	.ndo_change_mtu		= ip_tunnel_change_mtu,
455c5441932SPravin B Shelar 	.ndo_get_stats64	= ip_tunnel_get_stats64,
456b8c26a33SStephen Hemminger };
457b8c26a33SStephen Hemminger 
4586b78f16eSEric Dumazet #define GRE_FEATURES (NETIF_F_SG |		\
4596b78f16eSEric Dumazet 		      NETIF_F_FRAGLIST |	\
4606b78f16eSEric Dumazet 		      NETIF_F_HIGHDMA |		\
4616b78f16eSEric Dumazet 		      NETIF_F_HW_CSUM)
4626b78f16eSEric Dumazet 
4631da177e4SLinus Torvalds static void ipgre_tunnel_setup(struct net_device *dev)
4641da177e4SLinus Torvalds {
465b8c26a33SStephen Hemminger 	dev->netdev_ops		= &ipgre_netdev_ops;
4665a455275SNicolas Dichtel 	dev->type		= ARPHRD_IPGRE;
467c5441932SPravin B Shelar 	ip_tunnel_setup(dev, ipgre_net_id);
468c5441932SPravin B Shelar }
4691da177e4SLinus Torvalds 
470c5441932SPravin B Shelar static void __gre_tunnel_init(struct net_device *dev)
471c5441932SPravin B Shelar {
472c5441932SPravin B Shelar 	struct ip_tunnel *tunnel;
473c5441932SPravin B Shelar 
474c5441932SPravin B Shelar 	tunnel = netdev_priv(dev);
475c5441932SPravin B Shelar 	tunnel->hlen = ip_gre_calc_hlen(tunnel->parms.o_flags);
476c5441932SPravin B Shelar 	tunnel->parms.iph.protocol = IPPROTO_GRE;
477c5441932SPravin B Shelar 
478c95b819aSHerbert Xu 	dev->needed_headroom	= LL_MAX_HEADER + sizeof(struct iphdr) + 4;
47946f25dffSKris Katterjohn 	dev->mtu		= ETH_DATA_LEN - sizeof(struct iphdr) - 4;
4806b78f16eSEric Dumazet 
481b57708adSNicolas Dichtel 	dev->features		|= GRE_FEATURES;
4826b78f16eSEric Dumazet 	dev->hw_features	|= GRE_FEATURES;
483c5441932SPravin B Shelar 
484c5441932SPravin B Shelar 	if (!(tunnel->parms.o_flags & TUNNEL_SEQ)) {
485c5441932SPravin B Shelar 		/* TCP offload with GRE SEQ is not supported. */
486c5441932SPravin B Shelar 		dev->features    |= NETIF_F_GSO_SOFTWARE;
487c5441932SPravin B Shelar 		dev->hw_features |= NETIF_F_GSO_SOFTWARE;
488c5441932SPravin B Shelar 		/* Can use a lockless transmit, unless we generate
489c5441932SPravin B Shelar 		 * output sequences
490c5441932SPravin B Shelar 		 */
491c5441932SPravin B Shelar 		dev->features |= NETIF_F_LLTX;
492c5441932SPravin B Shelar 	}
4931da177e4SLinus Torvalds }
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds static int ipgre_tunnel_init(struct net_device *dev)
4961da177e4SLinus Torvalds {
497c5441932SPravin B Shelar 	struct ip_tunnel *tunnel = netdev_priv(dev);
498c5441932SPravin B Shelar 	struct iphdr *iph = &tunnel->parms.iph;
4991da177e4SLinus Torvalds 
500c5441932SPravin B Shelar 	__gre_tunnel_init(dev);
5011da177e4SLinus Torvalds 
502c5441932SPravin B Shelar 	memcpy(dev->dev_addr, &iph->saddr, 4);
503c5441932SPravin B Shelar 	memcpy(dev->broadcast, &iph->daddr, 4);
5041da177e4SLinus Torvalds 
505c5441932SPravin B Shelar 	dev->flags		= IFF_NOARP;
506c5441932SPravin B Shelar 	dev->priv_flags		&= ~IFF_XMIT_DST_RELEASE;
507c5441932SPravin B Shelar 	dev->addr_len		= 4;
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds 	if (iph->daddr) {
5101da177e4SLinus Torvalds #ifdef CONFIG_NET_IPGRE_BROADCAST
511f97c1e0cSJoe Perches 		if (ipv4_is_multicast(iph->daddr)) {
5121da177e4SLinus Torvalds 			if (!iph->saddr)
5131da177e4SLinus Torvalds 				return -EINVAL;
5141da177e4SLinus Torvalds 			dev->flags = IFF_BROADCAST;
5153b04dddeSStephen Hemminger 			dev->header_ops = &ipgre_header_ops;
5161da177e4SLinus Torvalds 		}
5171da177e4SLinus Torvalds #endif
518ee34c1ebSMichal Schmidt 	} else
5196a5f44d7STimo Teras 		dev->header_ops = &ipgre_header_ops;
5201da177e4SLinus Torvalds 
521c5441932SPravin B Shelar 	return ip_tunnel_init(dev);
52260769a5dSEric Dumazet }
52360769a5dSEric Dumazet 
524bda7bb46SPravin B Shelar static struct gre_cisco_protocol ipgre_protocol = {
5251da177e4SLinus Torvalds 	.handler        = ipgre_rcv,
5261da177e4SLinus Torvalds 	.err_handler    = ipgre_err,
527bda7bb46SPravin B Shelar 	.priority       = 0,
5281da177e4SLinus Torvalds };
5291da177e4SLinus Torvalds 
5302c8c1e72SAlexey Dobriyan static int __net_init ipgre_init_net(struct net *net)
53159a4c759SPavel Emelyanov {
532c5441932SPravin B Shelar 	return ip_tunnel_init_net(net, ipgre_net_id, &ipgre_link_ops, NULL);
53359a4c759SPavel Emelyanov }
53459a4c759SPavel Emelyanov 
5352c8c1e72SAlexey Dobriyan static void __net_exit ipgre_exit_net(struct net *net)
53659a4c759SPavel Emelyanov {
537c5441932SPravin B Shelar 	struct ip_tunnel_net *itn = net_generic(net, ipgre_net_id);
5386c742e71SNicolas Dichtel 	ip_tunnel_delete_net(itn, &ipgre_link_ops);
53959a4c759SPavel Emelyanov }
54059a4c759SPavel Emelyanov 
54159a4c759SPavel Emelyanov static struct pernet_operations ipgre_net_ops = {
54259a4c759SPavel Emelyanov 	.init = ipgre_init_net,
54359a4c759SPavel Emelyanov 	.exit = ipgre_exit_net,
544cfb8fbf2SEric W. Biederman 	.id   = &ipgre_net_id,
545c5441932SPravin B Shelar 	.size = sizeof(struct ip_tunnel_net),
54659a4c759SPavel Emelyanov };
5471da177e4SLinus Torvalds 
548c19e654dSHerbert Xu static int ipgre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[])
549c19e654dSHerbert Xu {
550c19e654dSHerbert Xu 	__be16 flags;
551c19e654dSHerbert Xu 
552c19e654dSHerbert Xu 	if (!data)
553c19e654dSHerbert Xu 		return 0;
554c19e654dSHerbert Xu 
555c19e654dSHerbert Xu 	flags = 0;
556c19e654dSHerbert Xu 	if (data[IFLA_GRE_IFLAGS])
557c19e654dSHerbert Xu 		flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
558c19e654dSHerbert Xu 	if (data[IFLA_GRE_OFLAGS])
559c19e654dSHerbert Xu 		flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
560c19e654dSHerbert Xu 	if (flags & (GRE_VERSION|GRE_ROUTING))
561c19e654dSHerbert Xu 		return -EINVAL;
562c19e654dSHerbert Xu 
563c19e654dSHerbert Xu 	return 0;
564c19e654dSHerbert Xu }
565c19e654dSHerbert Xu 
566e1a80002SHerbert Xu static int ipgre_tap_validate(struct nlattr *tb[], struct nlattr *data[])
567e1a80002SHerbert Xu {
568e1a80002SHerbert Xu 	__be32 daddr;
569e1a80002SHerbert Xu 
570e1a80002SHerbert Xu 	if (tb[IFLA_ADDRESS]) {
571e1a80002SHerbert Xu 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
572e1a80002SHerbert Xu 			return -EINVAL;
573e1a80002SHerbert Xu 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
574e1a80002SHerbert Xu 			return -EADDRNOTAVAIL;
575e1a80002SHerbert Xu 	}
576e1a80002SHerbert Xu 
577e1a80002SHerbert Xu 	if (!data)
578e1a80002SHerbert Xu 		goto out;
579e1a80002SHerbert Xu 
580e1a80002SHerbert Xu 	if (data[IFLA_GRE_REMOTE]) {
581e1a80002SHerbert Xu 		memcpy(&daddr, nla_data(data[IFLA_GRE_REMOTE]), 4);
582e1a80002SHerbert Xu 		if (!daddr)
583e1a80002SHerbert Xu 			return -EINVAL;
584e1a80002SHerbert Xu 	}
585e1a80002SHerbert Xu 
586e1a80002SHerbert Xu out:
587e1a80002SHerbert Xu 	return ipgre_tunnel_validate(tb, data);
588e1a80002SHerbert Xu }
589e1a80002SHerbert Xu 
590c5441932SPravin B Shelar static void ipgre_netlink_parms(struct nlattr *data[], struct nlattr *tb[],
591c19e654dSHerbert Xu 			       struct ip_tunnel_parm *parms)
592c19e654dSHerbert Xu {
5937bb82d92SHerbert Xu 	memset(parms, 0, sizeof(*parms));
594c19e654dSHerbert Xu 
595c19e654dSHerbert Xu 	parms->iph.protocol = IPPROTO_GRE;
596c19e654dSHerbert Xu 
597c19e654dSHerbert Xu 	if (!data)
598c19e654dSHerbert Xu 		return;
599c19e654dSHerbert Xu 
600c19e654dSHerbert Xu 	if (data[IFLA_GRE_LINK])
601c19e654dSHerbert Xu 		parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
602c19e654dSHerbert Xu 
603c19e654dSHerbert Xu 	if (data[IFLA_GRE_IFLAGS])
604c5441932SPravin B Shelar 		parms->i_flags = gre_flags_to_tnl_flags(nla_get_be16(data[IFLA_GRE_IFLAGS]));
605c19e654dSHerbert Xu 
606c19e654dSHerbert Xu 	if (data[IFLA_GRE_OFLAGS])
607c5441932SPravin B Shelar 		parms->o_flags = gre_flags_to_tnl_flags(nla_get_be16(data[IFLA_GRE_OFLAGS]));
608c19e654dSHerbert Xu 
609c19e654dSHerbert Xu 	if (data[IFLA_GRE_IKEY])
610c19e654dSHerbert Xu 		parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
611c19e654dSHerbert Xu 
612c19e654dSHerbert Xu 	if (data[IFLA_GRE_OKEY])
613c19e654dSHerbert Xu 		parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
614c19e654dSHerbert Xu 
615c19e654dSHerbert Xu 	if (data[IFLA_GRE_LOCAL])
6164d74f8baSPatrick McHardy 		parms->iph.saddr = nla_get_be32(data[IFLA_GRE_LOCAL]);
617c19e654dSHerbert Xu 
618c19e654dSHerbert Xu 	if (data[IFLA_GRE_REMOTE])
6194d74f8baSPatrick McHardy 		parms->iph.daddr = nla_get_be32(data[IFLA_GRE_REMOTE]);
620c19e654dSHerbert Xu 
621c19e654dSHerbert Xu 	if (data[IFLA_GRE_TTL])
622c19e654dSHerbert Xu 		parms->iph.ttl = nla_get_u8(data[IFLA_GRE_TTL]);
623c19e654dSHerbert Xu 
624c19e654dSHerbert Xu 	if (data[IFLA_GRE_TOS])
625c19e654dSHerbert Xu 		parms->iph.tos = nla_get_u8(data[IFLA_GRE_TOS]);
626c19e654dSHerbert Xu 
627c19e654dSHerbert Xu 	if (!data[IFLA_GRE_PMTUDISC] || nla_get_u8(data[IFLA_GRE_PMTUDISC]))
628c19e654dSHerbert Xu 		parms->iph.frag_off = htons(IP_DF);
629c19e654dSHerbert Xu }
630c19e654dSHerbert Xu 
631c5441932SPravin B Shelar static int gre_tap_init(struct net_device *dev)
632e1a80002SHerbert Xu {
633c5441932SPravin B Shelar 	__gre_tunnel_init(dev);
634e1a80002SHerbert Xu 
635c5441932SPravin B Shelar 	return ip_tunnel_init(dev);
636e1a80002SHerbert Xu }
637e1a80002SHerbert Xu 
638c5441932SPravin B Shelar static const struct net_device_ops gre_tap_netdev_ops = {
639c5441932SPravin B Shelar 	.ndo_init		= gre_tap_init,
640c5441932SPravin B Shelar 	.ndo_uninit		= ip_tunnel_uninit,
641c5441932SPravin B Shelar 	.ndo_start_xmit		= gre_tap_xmit,
642b8c26a33SStephen Hemminger 	.ndo_set_mac_address 	= eth_mac_addr,
643b8c26a33SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
644c5441932SPravin B Shelar 	.ndo_change_mtu		= ip_tunnel_change_mtu,
645c5441932SPravin B Shelar 	.ndo_get_stats64	= ip_tunnel_get_stats64,
646b8c26a33SStephen Hemminger };
647b8c26a33SStephen Hemminger 
648e1a80002SHerbert Xu static void ipgre_tap_setup(struct net_device *dev)
649e1a80002SHerbert Xu {
650e1a80002SHerbert Xu 	ether_setup(dev);
651c5441932SPravin B Shelar 	dev->netdev_ops		= &gre_tap_netdev_ops;
652*f8c1b7ceSstephen hemminger 	dev->priv_flags 	|= IFF_LIVE_ADDR_CHANGE;
653c5441932SPravin B Shelar 	ip_tunnel_setup(dev, gre_tap_net_id);
654e1a80002SHerbert Xu }
655e1a80002SHerbert Xu 
656c5441932SPravin B Shelar static int ipgre_newlink(struct net *src_net, struct net_device *dev,
657c5441932SPravin B Shelar 			 struct nlattr *tb[], struct nlattr *data[])
658c19e654dSHerbert Xu {
659c5441932SPravin B Shelar 	struct ip_tunnel_parm p;
660c19e654dSHerbert Xu 
661c5441932SPravin B Shelar 	ipgre_netlink_parms(data, tb, &p);
662c5441932SPravin B Shelar 	return ip_tunnel_newlink(dev, tb, &p);
663c19e654dSHerbert Xu }
664c19e654dSHerbert Xu 
665c19e654dSHerbert Xu static int ipgre_changelink(struct net_device *dev, struct nlattr *tb[],
666c19e654dSHerbert Xu 			    struct nlattr *data[])
667c19e654dSHerbert Xu {
668c19e654dSHerbert Xu 	struct ip_tunnel_parm p;
669c19e654dSHerbert Xu 
670c5441932SPravin B Shelar 	ipgre_netlink_parms(data, tb, &p);
671c5441932SPravin B Shelar 	return ip_tunnel_changelink(dev, tb, &p);
672c19e654dSHerbert Xu }
673c19e654dSHerbert Xu 
674c19e654dSHerbert Xu static size_t ipgre_get_size(const struct net_device *dev)
675c19e654dSHerbert Xu {
676c19e654dSHerbert Xu 	return
677c19e654dSHerbert Xu 		/* IFLA_GRE_LINK */
678c19e654dSHerbert Xu 		nla_total_size(4) +
679c19e654dSHerbert Xu 		/* IFLA_GRE_IFLAGS */
680c19e654dSHerbert Xu 		nla_total_size(2) +
681c19e654dSHerbert Xu 		/* IFLA_GRE_OFLAGS */
682c19e654dSHerbert Xu 		nla_total_size(2) +
683c19e654dSHerbert Xu 		/* IFLA_GRE_IKEY */
684c19e654dSHerbert Xu 		nla_total_size(4) +
685c19e654dSHerbert Xu 		/* IFLA_GRE_OKEY */
686c19e654dSHerbert Xu 		nla_total_size(4) +
687c19e654dSHerbert Xu 		/* IFLA_GRE_LOCAL */
688c19e654dSHerbert Xu 		nla_total_size(4) +
689c19e654dSHerbert Xu 		/* IFLA_GRE_REMOTE */
690c19e654dSHerbert Xu 		nla_total_size(4) +
691c19e654dSHerbert Xu 		/* IFLA_GRE_TTL */
692c19e654dSHerbert Xu 		nla_total_size(1) +
693c19e654dSHerbert Xu 		/* IFLA_GRE_TOS */
694c19e654dSHerbert Xu 		nla_total_size(1) +
695c19e654dSHerbert Xu 		/* IFLA_GRE_PMTUDISC */
696c19e654dSHerbert Xu 		nla_total_size(1) +
697c19e654dSHerbert Xu 		0;
698c19e654dSHerbert Xu }
699c19e654dSHerbert Xu 
700c19e654dSHerbert Xu static int ipgre_fill_info(struct sk_buff *skb, const struct net_device *dev)
701c19e654dSHerbert Xu {
702c19e654dSHerbert Xu 	struct ip_tunnel *t = netdev_priv(dev);
703c19e654dSHerbert Xu 	struct ip_tunnel_parm *p = &t->parms;
704c19e654dSHerbert Xu 
705f3756b79SDavid S. Miller 	if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
706c5441932SPravin B Shelar 	    nla_put_be16(skb, IFLA_GRE_IFLAGS, tnl_flags_to_gre_flags(p->i_flags)) ||
707c5441932SPravin B Shelar 	    nla_put_be16(skb, IFLA_GRE_OFLAGS, tnl_flags_to_gre_flags(p->o_flags)) ||
708f3756b79SDavid S. Miller 	    nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
709f3756b79SDavid S. Miller 	    nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
710f3756b79SDavid S. Miller 	    nla_put_be32(skb, IFLA_GRE_LOCAL, p->iph.saddr) ||
711f3756b79SDavid S. Miller 	    nla_put_be32(skb, IFLA_GRE_REMOTE, p->iph.daddr) ||
712f3756b79SDavid S. Miller 	    nla_put_u8(skb, IFLA_GRE_TTL, p->iph.ttl) ||
713f3756b79SDavid S. Miller 	    nla_put_u8(skb, IFLA_GRE_TOS, p->iph.tos) ||
714f3756b79SDavid S. Miller 	    nla_put_u8(skb, IFLA_GRE_PMTUDISC,
715f3756b79SDavid S. Miller 		       !!(p->iph.frag_off & htons(IP_DF))))
716f3756b79SDavid S. Miller 		goto nla_put_failure;
717c19e654dSHerbert Xu 	return 0;
718c19e654dSHerbert Xu 
719c19e654dSHerbert Xu nla_put_failure:
720c19e654dSHerbert Xu 	return -EMSGSIZE;
721c19e654dSHerbert Xu }
722c19e654dSHerbert Xu 
723c19e654dSHerbert Xu static const struct nla_policy ipgre_policy[IFLA_GRE_MAX + 1] = {
724c19e654dSHerbert Xu 	[IFLA_GRE_LINK]		= { .type = NLA_U32 },
725c19e654dSHerbert Xu 	[IFLA_GRE_IFLAGS]	= { .type = NLA_U16 },
726c19e654dSHerbert Xu 	[IFLA_GRE_OFLAGS]	= { .type = NLA_U16 },
727c19e654dSHerbert Xu 	[IFLA_GRE_IKEY]		= { .type = NLA_U32 },
728c19e654dSHerbert Xu 	[IFLA_GRE_OKEY]		= { .type = NLA_U32 },
7294d74f8baSPatrick McHardy 	[IFLA_GRE_LOCAL]	= { .len = FIELD_SIZEOF(struct iphdr, saddr) },
7304d74f8baSPatrick McHardy 	[IFLA_GRE_REMOTE]	= { .len = FIELD_SIZEOF(struct iphdr, daddr) },
731c19e654dSHerbert Xu 	[IFLA_GRE_TTL]		= { .type = NLA_U8 },
732c19e654dSHerbert Xu 	[IFLA_GRE_TOS]		= { .type = NLA_U8 },
733c19e654dSHerbert Xu 	[IFLA_GRE_PMTUDISC]	= { .type = NLA_U8 },
734c19e654dSHerbert Xu };
735c19e654dSHerbert Xu 
736c19e654dSHerbert Xu static struct rtnl_link_ops ipgre_link_ops __read_mostly = {
737c19e654dSHerbert Xu 	.kind		= "gre",
738c19e654dSHerbert Xu 	.maxtype	= IFLA_GRE_MAX,
739c19e654dSHerbert Xu 	.policy		= ipgre_policy,
740c19e654dSHerbert Xu 	.priv_size	= sizeof(struct ip_tunnel),
741c19e654dSHerbert Xu 	.setup		= ipgre_tunnel_setup,
742c19e654dSHerbert Xu 	.validate	= ipgre_tunnel_validate,
743c19e654dSHerbert Xu 	.newlink	= ipgre_newlink,
744c19e654dSHerbert Xu 	.changelink	= ipgre_changelink,
745c5441932SPravin B Shelar 	.dellink	= ip_tunnel_dellink,
746c19e654dSHerbert Xu 	.get_size	= ipgre_get_size,
747c19e654dSHerbert Xu 	.fill_info	= ipgre_fill_info,
748c19e654dSHerbert Xu };
749c19e654dSHerbert Xu 
750e1a80002SHerbert Xu static struct rtnl_link_ops ipgre_tap_ops __read_mostly = {
751e1a80002SHerbert Xu 	.kind		= "gretap",
752e1a80002SHerbert Xu 	.maxtype	= IFLA_GRE_MAX,
753e1a80002SHerbert Xu 	.policy		= ipgre_policy,
754e1a80002SHerbert Xu 	.priv_size	= sizeof(struct ip_tunnel),
755e1a80002SHerbert Xu 	.setup		= ipgre_tap_setup,
756e1a80002SHerbert Xu 	.validate	= ipgre_tap_validate,
757e1a80002SHerbert Xu 	.newlink	= ipgre_newlink,
758e1a80002SHerbert Xu 	.changelink	= ipgre_changelink,
759c5441932SPravin B Shelar 	.dellink	= ip_tunnel_dellink,
760e1a80002SHerbert Xu 	.get_size	= ipgre_get_size,
761e1a80002SHerbert Xu 	.fill_info	= ipgre_fill_info,
762e1a80002SHerbert Xu };
763e1a80002SHerbert Xu 
764c5441932SPravin B Shelar static int __net_init ipgre_tap_init_net(struct net *net)
765c5441932SPravin B Shelar {
766c5441932SPravin B Shelar 	return ip_tunnel_init_net(net, gre_tap_net_id, &ipgre_tap_ops, NULL);
767c5441932SPravin B Shelar }
768c5441932SPravin B Shelar 
769c5441932SPravin B Shelar static void __net_exit ipgre_tap_exit_net(struct net *net)
770c5441932SPravin B Shelar {
771c5441932SPravin B Shelar 	struct ip_tunnel_net *itn = net_generic(net, gre_tap_net_id);
7726c742e71SNicolas Dichtel 	ip_tunnel_delete_net(itn, &ipgre_tap_ops);
773c5441932SPravin B Shelar }
774c5441932SPravin B Shelar 
775c5441932SPravin B Shelar static struct pernet_operations ipgre_tap_net_ops = {
776c5441932SPravin B Shelar 	.init = ipgre_tap_init_net,
777c5441932SPravin B Shelar 	.exit = ipgre_tap_exit_net,
778c5441932SPravin B Shelar 	.id   = &gre_tap_net_id,
779c5441932SPravin B Shelar 	.size = sizeof(struct ip_tunnel_net),
780c5441932SPravin B Shelar };
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds static int __init ipgre_init(void)
7831da177e4SLinus Torvalds {
7841da177e4SLinus Torvalds 	int err;
7851da177e4SLinus Torvalds 
786058bd4d2SJoe Perches 	pr_info("GRE over IPv4 tunneling driver\n");
7871da177e4SLinus Torvalds 
788cfb8fbf2SEric W. Biederman 	err = register_pernet_device(&ipgre_net_ops);
78959a4c759SPavel Emelyanov 	if (err < 0)
790c2892f02SAlexey Dobriyan 		return err;
791c2892f02SAlexey Dobriyan 
792c5441932SPravin B Shelar 	err = register_pernet_device(&ipgre_tap_net_ops);
793c5441932SPravin B Shelar 	if (err < 0)
794c5441932SPravin B Shelar 		goto pnet_tap_faied;
795c5441932SPravin B Shelar 
796bda7bb46SPravin B Shelar 	err = gre_cisco_register(&ipgre_protocol);
797c2892f02SAlexey Dobriyan 	if (err < 0) {
798058bd4d2SJoe Perches 		pr_info("%s: can't add protocol\n", __func__);
799c2892f02SAlexey Dobriyan 		goto add_proto_failed;
800c2892f02SAlexey Dobriyan 	}
8017daa0004SPavel Emelyanov 
802c19e654dSHerbert Xu 	err = rtnl_link_register(&ipgre_link_ops);
803c19e654dSHerbert Xu 	if (err < 0)
804c19e654dSHerbert Xu 		goto rtnl_link_failed;
805c19e654dSHerbert Xu 
806e1a80002SHerbert Xu 	err = rtnl_link_register(&ipgre_tap_ops);
807e1a80002SHerbert Xu 	if (err < 0)
808e1a80002SHerbert Xu 		goto tap_ops_failed;
809e1a80002SHerbert Xu 
810c5441932SPravin B Shelar 	return 0;
811c19e654dSHerbert Xu 
812e1a80002SHerbert Xu tap_ops_failed:
813e1a80002SHerbert Xu 	rtnl_link_unregister(&ipgre_link_ops);
814c19e654dSHerbert Xu rtnl_link_failed:
815bda7bb46SPravin B Shelar 	gre_cisco_unregister(&ipgre_protocol);
816c2892f02SAlexey Dobriyan add_proto_failed:
817c5441932SPravin B Shelar 	unregister_pernet_device(&ipgre_tap_net_ops);
818c5441932SPravin B Shelar pnet_tap_faied:
819c2892f02SAlexey Dobriyan 	unregister_pernet_device(&ipgre_net_ops);
820c5441932SPravin B Shelar 	return err;
8211da177e4SLinus Torvalds }
8221da177e4SLinus Torvalds 
823db44575fSAlexey Kuznetsov static void __exit ipgre_fini(void)
8241da177e4SLinus Torvalds {
825e1a80002SHerbert Xu 	rtnl_link_unregister(&ipgre_tap_ops);
826c19e654dSHerbert Xu 	rtnl_link_unregister(&ipgre_link_ops);
827bda7bb46SPravin B Shelar 	gre_cisco_unregister(&ipgre_protocol);
828c5441932SPravin B Shelar 	unregister_pernet_device(&ipgre_tap_net_ops);
829c2892f02SAlexey Dobriyan 	unregister_pernet_device(&ipgre_net_ops);
8301da177e4SLinus Torvalds }
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds module_init(ipgre_init);
8331da177e4SLinus Torvalds module_exit(ipgre_fini);
8341da177e4SLinus Torvalds MODULE_LICENSE("GPL");
8354d74f8baSPatrick McHardy MODULE_ALIAS_RTNL_LINK("gre");
8364d74f8baSPatrick McHardy MODULE_ALIAS_RTNL_LINK("gretap");
8378909c9adSVasiliy Kulikov MODULE_ALIAS_NETDEV("gre0");
838c5441932SPravin B Shelar MODULE_ALIAS_NETDEV("gretap0");
839