xref: /linux/net/ipv4/ip_gre.c (revision 301bd140ed0b24f0da660874c7e8a47dad8c8222)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *	Linux NET3:	GRE over IP protocol decoder.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *	Authors: Alexey Kuznetsov (kuznet@ms2.inr.ac.ru)
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
8afd46503SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9afd46503SJoe Perches 
104fc268d2SRandy Dunlap #include <linux/capability.h>
111da177e4SLinus Torvalds #include <linux/module.h>
121da177e4SLinus Torvalds #include <linux/types.h>
131da177e4SLinus Torvalds #include <linux/kernel.h>
145a0e3ad6STejun Heo #include <linux/slab.h>
157c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
161da177e4SLinus Torvalds #include <linux/skbuff.h>
171da177e4SLinus Torvalds #include <linux/netdevice.h>
181da177e4SLinus Torvalds #include <linux/in.h>
191da177e4SLinus Torvalds #include <linux/tcp.h>
201da177e4SLinus Torvalds #include <linux/udp.h>
211da177e4SLinus Torvalds #include <linux/if_arp.h>
222e15ea39SPravin B Shelar #include <linux/if_vlan.h>
231da177e4SLinus Torvalds #include <linux/init.h>
241da177e4SLinus Torvalds #include <linux/in6.h>
251da177e4SLinus Torvalds #include <linux/inetdevice.h>
261da177e4SLinus Torvalds #include <linux/igmp.h>
271da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
28e1a80002SHerbert Xu #include <linux/etherdevice.h>
2946f25dffSKris Katterjohn #include <linux/if_ether.h>
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds #include <net/sock.h>
321da177e4SLinus Torvalds #include <net/ip.h>
331da177e4SLinus Torvalds #include <net/icmp.h>
341da177e4SLinus Torvalds #include <net/protocol.h>
35c5441932SPravin B Shelar #include <net/ip_tunnels.h>
361da177e4SLinus Torvalds #include <net/arp.h>
371da177e4SLinus Torvalds #include <net/checksum.h>
381da177e4SLinus Torvalds #include <net/dsfield.h>
391da177e4SLinus Torvalds #include <net/inet_ecn.h>
401da177e4SLinus Torvalds #include <net/xfrm.h>
4159a4c759SPavel Emelyanov #include <net/net_namespace.h>
4259a4c759SPavel Emelyanov #include <net/netns/generic.h>
43c19e654dSHerbert Xu #include <net/rtnetlink.h>
4400959adeSDmitry Kozlov #include <net/gre.h>
452e15ea39SPravin B Shelar #include <net/dst_metadata.h>
4684e54fe0SWilliam Tu #include <net/erspan.h>
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds /*
491da177e4SLinus Torvalds    Problems & solutions
501da177e4SLinus Torvalds    --------------------
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds    1. The most important issue is detecting local dead loops.
531da177e4SLinus Torvalds    They would cause complete host lockup in transmit, which
541da177e4SLinus Torvalds    would be "resolved" by stack overflow or, if queueing is enabled,
551da177e4SLinus Torvalds    with infinite looping in net_bh.
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds    We cannot track such dead loops during route installation,
581da177e4SLinus Torvalds    it is infeasible task. The most general solutions would be
591da177e4SLinus Torvalds    to keep skb->encapsulation counter (sort of local ttl),
606d0722a2SEric Dumazet    and silently drop packet when it expires. It is a good
61bff52857Sstephen hemminger    solution, but it supposes maintaining new variable in ALL
621da177e4SLinus Torvalds    skb, even if no tunneling is used.
631da177e4SLinus Torvalds 
646d0722a2SEric Dumazet    Current solution: xmit_recursion breaks dead loops. This is a percpu
656d0722a2SEric Dumazet    counter, since when we enter the first ndo_xmit(), cpu migration is
666d0722a2SEric Dumazet    forbidden. We force an exit if this counter reaches RECURSION_LIMIT
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds    2. Networking dead loops would not kill routers, but would really
691da177e4SLinus Torvalds    kill network. IP hop limit plays role of "t->recursion" in this case,
701da177e4SLinus Torvalds    if we copy it from packet being encapsulated to upper header.
711da177e4SLinus Torvalds    It is very good solution, but it introduces two problems:
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds    - Routing protocols, using packets with ttl=1 (OSPF, RIP2),
741da177e4SLinus Torvalds      do not work over tunnels.
751da177e4SLinus Torvalds    - traceroute does not work. I planned to relay ICMP from tunnel,
761da177e4SLinus Torvalds      so that this problem would be solved and traceroute output
771da177e4SLinus Torvalds      would even more informative. This idea appeared to be wrong:
781da177e4SLinus Torvalds      only Linux complies to rfc1812 now (yes, guys, Linux is the only
791da177e4SLinus Torvalds      true router now :-)), all routers (at least, in neighbourhood of mine)
801da177e4SLinus Torvalds      return only 8 bytes of payload. It is the end.
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds    Hence, if we want that OSPF worked or traceroute said something reasonable,
831da177e4SLinus Torvalds    we should search for another solution.
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds    One of them is to parse packet trying to detect inner encapsulation
861da177e4SLinus Torvalds    made by our node. It is difficult or even impossible, especially,
87bff52857Sstephen hemminger    taking into account fragmentation. TO be short, ttl is not solution at all.
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds    Current solution: The solution was UNEXPECTEDLY SIMPLE.
901da177e4SLinus Torvalds    We force DF flag on tunnels with preconfigured hop limit,
911da177e4SLinus Torvalds    that is ALL. :-) Well, it does not remove the problem completely,
921da177e4SLinus Torvalds    but exponential growth of network traffic is changed to linear
931da177e4SLinus Torvalds    (branches, that exceed pmtu are pruned) and tunnel mtu
94bff52857Sstephen hemminger    rapidly degrades to value <68, where looping stops.
951da177e4SLinus Torvalds    Yes, it is not good if there exists a router in the loop,
961da177e4SLinus Torvalds    which does not force DF, even when encapsulating packets have DF set.
971da177e4SLinus Torvalds    But it is not our problem! Nobody could accuse us, we made
981da177e4SLinus Torvalds    all that we could make. Even if it is your gated who injected
991da177e4SLinus Torvalds    fatal route to network, even if it were you who configured
1001da177e4SLinus Torvalds    fatal static route: you are innocent. :-)
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds    Alexey Kuznetsov.
1031da177e4SLinus Torvalds  */
1041da177e4SLinus Torvalds 
105eccc1bb8Sstephen hemminger static bool log_ecn_error = true;
106eccc1bb8Sstephen hemminger module_param(log_ecn_error, bool, 0644);
107eccc1bb8Sstephen hemminger MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
108eccc1bb8Sstephen hemminger 
109c19e654dSHerbert Xu static struct rtnl_link_ops ipgre_link_ops __read_mostly;
110aab1e898SGuillaume Nault static const struct header_ops ipgre_header_ops;
111aab1e898SGuillaume Nault 
1121da177e4SLinus Torvalds static int ipgre_tunnel_init(struct net_device *dev);
1131a66a836SWilliam Tu static void erspan_build_header(struct sk_buff *skb,
114c69de58bSWilliam Tu 				u32 id, u32 index,
115a3222dc9SWilliam Tu 				bool truncate, bool is_ipv4);
116eb8ce741SPavel Emelyanov 
117c7d03a00SAlexey Dobriyan static unsigned int ipgre_net_id __read_mostly;
118c7d03a00SAlexey Dobriyan static unsigned int gre_tap_net_id __read_mostly;
11984e54fe0SWilliam Tu static unsigned int erspan_net_id __read_mostly;
120eb8ce741SPavel Emelyanov 
12132bbd879SStefano Brivio static int ipgre_err(struct sk_buff *skb, u32 info,
122bda7bb46SPravin B Shelar 		     const struct tnl_ptk_info *tpi)
1231da177e4SLinus Torvalds {
1241da177e4SLinus Torvalds 
125071f92d0SRami Rosen 	/* All the routers (except for Linux) return only
1261da177e4SLinus Torvalds 	   8 bytes of packet payload. It means, that precise relaying of
1271da177e4SLinus Torvalds 	   ICMP in the real Internet is absolutely infeasible.
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	   Moreover, Cisco "wise men" put GRE key to the third word
130c5441932SPravin B Shelar 	   in GRE header. It makes impossible maintaining even soft
131c5441932SPravin B Shelar 	   state for keyed GRE tunnels with enabled checksum. Tell
132c5441932SPravin B Shelar 	   them "thank you".
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds 	   Well, I wonder, rfc1812 was written by Cisco employee,
135bff52857Sstephen hemminger 	   what the hell these idiots break standards established
136bff52857Sstephen hemminger 	   by themselves???
1371da177e4SLinus Torvalds 	   */
138c5441932SPravin B Shelar 	struct net *net = dev_net(skb->dev);
139c5441932SPravin B Shelar 	struct ip_tunnel_net *itn;
14096f5a846SEric Dumazet 	const struct iphdr *iph;
14188c7664fSArnaldo Carvalho de Melo 	const int type = icmp_hdr(skb)->type;
14288c7664fSArnaldo Carvalho de Melo 	const int code = icmp_hdr(skb)->code;
14320e1954fSEric Dumazet 	unsigned int data_len = 0;
1441da177e4SLinus Torvalds 	struct ip_tunnel *t;
145d2083287Sstephen hemminger 
146bda7bb46SPravin B Shelar 	if (tpi->proto == htons(ETH_P_TEB))
147c5441932SPravin B Shelar 		itn = net_generic(net, gre_tap_net_id);
14851dc63e3SHaishuang Yan 	else if (tpi->proto == htons(ETH_P_ERSPAN) ||
14951dc63e3SHaishuang Yan 		 tpi->proto == htons(ETH_P_ERSPAN2))
15051dc63e3SHaishuang Yan 		itn = net_generic(net, erspan_net_id);
151c5441932SPravin B Shelar 	else
152c5441932SPravin B Shelar 		itn = net_generic(net, ipgre_net_id);
153c5441932SPravin B Shelar 
154c0c0c50fSDuan Jiong 	iph = (const struct iphdr *)(icmp_hdr(skb) + 1);
155bda7bb46SPravin B Shelar 	t = ip_tunnel_lookup(itn, skb->dev->ifindex, tpi->flags,
156bda7bb46SPravin B Shelar 			     iph->daddr, iph->saddr, tpi->key);
157d2083287Sstephen hemminger 
15851456b29SIan Morris 	if (!t)
15932bbd879SStefano Brivio 		return -ENOENT;
16032bbd879SStefano Brivio 
16132bbd879SStefano Brivio 	switch (type) {
16232bbd879SStefano Brivio 	default:
16332bbd879SStefano Brivio 	case ICMP_PARAMETERPROB:
16432bbd879SStefano Brivio 		return 0;
16532bbd879SStefano Brivio 
16632bbd879SStefano Brivio 	case ICMP_DEST_UNREACH:
16732bbd879SStefano Brivio 		switch (code) {
16832bbd879SStefano Brivio 		case ICMP_SR_FAILED:
16932bbd879SStefano Brivio 		case ICMP_PORT_UNREACH:
17032bbd879SStefano Brivio 			/* Impossible event. */
17132bbd879SStefano Brivio 			return 0;
17232bbd879SStefano Brivio 		default:
17332bbd879SStefano Brivio 			/* All others are translated to HOST_UNREACH.
17432bbd879SStefano Brivio 			   rfc2003 contains "deep thoughts" about NET_UNREACH,
17532bbd879SStefano Brivio 			   I believe they are just ether pollution. --ANK
17632bbd879SStefano Brivio 			 */
17732bbd879SStefano Brivio 			break;
17832bbd879SStefano Brivio 		}
17932bbd879SStefano Brivio 		break;
18032bbd879SStefano Brivio 
18132bbd879SStefano Brivio 	case ICMP_TIME_EXCEEDED:
18232bbd879SStefano Brivio 		if (code != ICMP_EXC_TTL)
18332bbd879SStefano Brivio 			return 0;
18432bbd879SStefano Brivio 		data_len = icmp_hdr(skb)->un.reserved[1] * 4; /* RFC 4884 4.1 */
18532bbd879SStefano Brivio 		break;
18632bbd879SStefano Brivio 
18732bbd879SStefano Brivio 	case ICMP_REDIRECT:
18832bbd879SStefano Brivio 		break;
18932bbd879SStefano Brivio 	}
19036393395SDavid S. Miller 
1919b8c6d7bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1929b8c6d7bSEric Dumazet        if (tpi->proto == htons(ETH_P_IPV6) &&
19320e1954fSEric Dumazet            !ip6_err_gen_icmpv6_unreach(skb, iph->ihl * 4 + tpi->hdr_len,
19420e1954fSEric Dumazet 				       type, data_len))
19532bbd879SStefano Brivio                return 0;
1969b8c6d7bSEric Dumazet #endif
1979b8c6d7bSEric Dumazet 
19836393395SDavid S. Miller 	if (t->parms.iph.daddr == 0 ||
199f97c1e0cSJoe Perches 	    ipv4_is_multicast(t->parms.iph.daddr))
20032bbd879SStefano Brivio 		return 0;
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 	if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
20332bbd879SStefano Brivio 		return 0;
2041da177e4SLinus Torvalds 
205da6185d8SWei Yongjun 	if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
2061da177e4SLinus Torvalds 		t->err_count++;
2071da177e4SLinus Torvalds 	else
2081da177e4SLinus Torvalds 		t->err_count = 1;
2091da177e4SLinus Torvalds 	t->err_time = jiffies;
21032bbd879SStefano Brivio 
21132bbd879SStefano Brivio 	return 0;
2129f57c67cSPravin B Shelar }
2139f57c67cSPravin B Shelar 
2149f57c67cSPravin B Shelar static void gre_err(struct sk_buff *skb, u32 info)
2159f57c67cSPravin B Shelar {
2169f57c67cSPravin B Shelar 	/* All the routers (except for Linux) return only
2179f57c67cSPravin B Shelar 	 * 8 bytes of packet payload. It means, that precise relaying of
2189f57c67cSPravin B Shelar 	 * ICMP in the real Internet is absolutely infeasible.
2199f57c67cSPravin B Shelar 	 *
2209f57c67cSPravin B Shelar 	 * Moreover, Cisco "wise men" put GRE key to the third word
2219f57c67cSPravin B Shelar 	 * in GRE header. It makes impossible maintaining even soft
2229f57c67cSPravin B Shelar 	 * state for keyed
2239f57c67cSPravin B Shelar 	 * GRE tunnels with enabled checksum. Tell them "thank you".
2249f57c67cSPravin B Shelar 	 *
2259f57c67cSPravin B Shelar 	 * Well, I wonder, rfc1812 was written by Cisco employee,
2269f57c67cSPravin B Shelar 	 * what the hell these idiots break standards established
2279f57c67cSPravin B Shelar 	 * by themselves???
2289f57c67cSPravin B Shelar 	 */
2299f57c67cSPravin B Shelar 
230e582615aSEric Dumazet 	const struct iphdr *iph = (struct iphdr *)skb->data;
2319f57c67cSPravin B Shelar 	const int type = icmp_hdr(skb)->type;
2329f57c67cSPravin B Shelar 	const int code = icmp_hdr(skb)->code;
2339f57c67cSPravin B Shelar 	struct tnl_ptk_info tpi;
2349f57c67cSPravin B Shelar 
235b0350d51SHaishuang Yan 	if (gre_parse_header(skb, &tpi, NULL, htons(ETH_P_IP),
236b0350d51SHaishuang Yan 			     iph->ihl * 4) < 0)
2379f57c67cSPravin B Shelar 		return;
2389f57c67cSPravin B Shelar 
2399f57c67cSPravin B Shelar 	if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
2409f57c67cSPravin B Shelar 		ipv4_update_pmtu(skb, dev_net(skb->dev), info,
241d888f396SMaciej Żenczykowski 				 skb->dev->ifindex, IPPROTO_GRE);
2429f57c67cSPravin B Shelar 		return;
2439f57c67cSPravin B Shelar 	}
2449f57c67cSPravin B Shelar 	if (type == ICMP_REDIRECT) {
2451042caa7SMaciej Żenczykowski 		ipv4_redirect(skb, dev_net(skb->dev), skb->dev->ifindex,
2461042caa7SMaciej Żenczykowski 			      IPPROTO_GRE);
2479f57c67cSPravin B Shelar 		return;
2489f57c67cSPravin B Shelar 	}
2499f57c67cSPravin B Shelar 
2509f57c67cSPravin B Shelar 	ipgre_err(skb, info, &tpi);
2511da177e4SLinus Torvalds }
2521da177e4SLinus Torvalds 
253f989d546SWilliam Tu static bool is_erspan_type1(int gre_hdr_len)
254f989d546SWilliam Tu {
255f989d546SWilliam Tu 	/* Both ERSPAN type I (version 0) and type II (version 1) use
256f989d546SWilliam Tu 	 * protocol 0x88BE, but the type I has only 4-byte GRE header,
257f989d546SWilliam Tu 	 * while type II has 8-byte.
258f989d546SWilliam Tu 	 */
259f989d546SWilliam Tu 	return gre_hdr_len == 4;
260f989d546SWilliam Tu }
261f989d546SWilliam Tu 
26284e54fe0SWilliam Tu static int erspan_rcv(struct sk_buff *skb, struct tnl_ptk_info *tpi,
26384e54fe0SWilliam Tu 		      int gre_hdr_len)
26484e54fe0SWilliam Tu {
26584e54fe0SWilliam Tu 	struct net *net = dev_net(skb->dev);
26684e54fe0SWilliam Tu 	struct metadata_dst *tun_dst = NULL;
2671d7e2ed2SWilliam Tu 	struct erspan_base_hdr *ershdr;
26884e54fe0SWilliam Tu 	struct ip_tunnel_net *itn;
26984e54fe0SWilliam Tu 	struct ip_tunnel *tunnel;
27084e54fe0SWilliam Tu 	const struct iphdr *iph;
2713df19283SWilliam Tu 	struct erspan_md2 *md2;
2721d7e2ed2SWilliam Tu 	int ver;
27384e54fe0SWilliam Tu 	int len;
27484e54fe0SWilliam Tu 
27584e54fe0SWilliam Tu 	itn = net_generic(net, erspan_net_id);
27684e54fe0SWilliam Tu 	iph = ip_hdr(skb);
277f989d546SWilliam Tu 	if (is_erspan_type1(gre_hdr_len)) {
278f989d546SWilliam Tu 		ver = 0;
279f989d546SWilliam Tu 		tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex,
280f989d546SWilliam Tu 					  tpi->flags | TUNNEL_NO_KEY,
281f989d546SWilliam Tu 					  iph->saddr, iph->daddr, 0);
282f989d546SWilliam Tu 	} else {
2831d7e2ed2SWilliam Tu 		ershdr = (struct erspan_base_hdr *)(skb->data + gre_hdr_len);
284c69de58bSWilliam Tu 		ver = ershdr->ver;
28584e54fe0SWilliam Tu 		tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex,
28684e54fe0SWilliam Tu 					  tpi->flags | TUNNEL_KEY,
28784e54fe0SWilliam Tu 					  iph->saddr, iph->daddr, tpi->key);
288f989d546SWilliam Tu 	}
28984e54fe0SWilliam Tu 
29084e54fe0SWilliam Tu 	if (tunnel) {
291f989d546SWilliam Tu 		if (is_erspan_type1(gre_hdr_len))
292f989d546SWilliam Tu 			len = gre_hdr_len;
293f989d546SWilliam Tu 		else
2941d7e2ed2SWilliam Tu 			len = gre_hdr_len + erspan_hdr_len(ver);
295f989d546SWilliam Tu 
2961d7e2ed2SWilliam Tu 		if (unlikely(!pskb_may_pull(skb, len)))
297ae3e1337SWilliam Tu 			return PACKET_REJECT;
2981d7e2ed2SWilliam Tu 
29984e54fe0SWilliam Tu 		if (__iptunnel_pull_header(skb,
3001d7e2ed2SWilliam Tu 					   len,
30184e54fe0SWilliam Tu 					   htons(ETH_P_TEB),
30284e54fe0SWilliam Tu 					   false, false) < 0)
30384e54fe0SWilliam Tu 			goto drop;
30484e54fe0SWilliam Tu 
3051a66a836SWilliam Tu 		if (tunnel->collect_md) {
306492b67e2SLorenzo Bianconi 			struct erspan_metadata *pkt_md, *md;
3071a66a836SWilliam Tu 			struct ip_tunnel_info *info;
308492b67e2SLorenzo Bianconi 			unsigned char *gh;
3091a66a836SWilliam Tu 			__be64 tun_id;
3101a66a836SWilliam Tu 			__be16 flags;
3111a66a836SWilliam Tu 
3121a66a836SWilliam Tu 			tpi->flags |= TUNNEL_KEY;
3131a66a836SWilliam Tu 			flags = tpi->flags;
3141a66a836SWilliam Tu 			tun_id = key32_to_tunnel_id(tpi->key);
3151a66a836SWilliam Tu 
3161a66a836SWilliam Tu 			tun_dst = ip_tun_rx_dst(skb, flags,
3171a66a836SWilliam Tu 						tun_id, sizeof(*md));
3181a66a836SWilliam Tu 			if (!tun_dst)
3191a66a836SWilliam Tu 				return PACKET_REJECT;
3201a66a836SWilliam Tu 
321492b67e2SLorenzo Bianconi 			/* skb can be uncloned in __iptunnel_pull_header, so
322492b67e2SLorenzo Bianconi 			 * old pkt_md is no longer valid and we need to reset
323492b67e2SLorenzo Bianconi 			 * it
324492b67e2SLorenzo Bianconi 			 */
325492b67e2SLorenzo Bianconi 			gh = skb_network_header(skb) +
326492b67e2SLorenzo Bianconi 			     skb_network_header_len(skb);
327492b67e2SLorenzo Bianconi 			pkt_md = (struct erspan_metadata *)(gh + gre_hdr_len +
328492b67e2SLorenzo Bianconi 							    sizeof(*ershdr));
3291a66a836SWilliam Tu 			md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
330f551c91dSWilliam Tu 			md->version = ver;
3313df19283SWilliam Tu 			md2 = &md->u.md2;
3323df19283SWilliam Tu 			memcpy(md2, pkt_md, ver == 1 ? ERSPAN_V1_MDSIZE :
3333df19283SWilliam Tu 						       ERSPAN_V2_MDSIZE);
334f551c91dSWilliam Tu 
3351a66a836SWilliam Tu 			info = &tun_dst->u.tun_info;
3361a66a836SWilliam Tu 			info->key.tun_flags |= TUNNEL_ERSPAN_OPT;
3371a66a836SWilliam Tu 			info->options_len = sizeof(*md);
3381a66a836SWilliam Tu 		}
3391a66a836SWilliam Tu 
34084e54fe0SWilliam Tu 		skb_reset_mac_header(skb);
34184e54fe0SWilliam Tu 		ip_tunnel_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
34284e54fe0SWilliam Tu 		return PACKET_RCVD;
34384e54fe0SWilliam Tu 	}
3445a64506bSHaishuang Yan 	return PACKET_REJECT;
3455a64506bSHaishuang Yan 
34684e54fe0SWilliam Tu drop:
34784e54fe0SWilliam Tu 	kfree_skb(skb);
34884e54fe0SWilliam Tu 	return PACKET_RCVD;
34984e54fe0SWilliam Tu }
35084e54fe0SWilliam Tu 
351125372faSJiri Benc static int __ipgre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi,
352125372faSJiri Benc 		       struct ip_tunnel_net *itn, int hdr_len, bool raw_proto)
3531da177e4SLinus Torvalds {
3542e15ea39SPravin B Shelar 	struct metadata_dst *tun_dst = NULL;
355b71d1d42SEric Dumazet 	const struct iphdr *iph;
3561da177e4SLinus Torvalds 	struct ip_tunnel *tunnel;
3571da177e4SLinus Torvalds 
358eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
359bda7bb46SPravin B Shelar 	tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, tpi->flags,
360bda7bb46SPravin B Shelar 				  iph->saddr, iph->daddr, tpi->key);
3611da177e4SLinus Torvalds 
362d2083287Sstephen hemminger 	if (tunnel) {
363c0d59da7Swenxu 		const struct iphdr *tnl_params;
364c0d59da7Swenxu 
365125372faSJiri Benc 		if (__iptunnel_pull_header(skb, hdr_len, tpi->proto,
366125372faSJiri Benc 					   raw_proto, false) < 0)
367244a797bSJiri Benc 			goto drop;
368244a797bSJiri Benc 
369aab1e898SGuillaume Nault 		/* Special case for ipgre_header_parse(), which expects the
370aab1e898SGuillaume Nault 		 * mac_header to point to the outer IP header.
371aab1e898SGuillaume Nault 		 */
372aab1e898SGuillaume Nault 		if (tunnel->dev->header_ops == &ipgre_header_ops)
3730e3da5bbSTimo Teräs 			skb_pop_mac_header(skb);
374e271c7b4SJiri Benc 		else
375e271c7b4SJiri Benc 			skb_reset_mac_header(skb);
376c0d59da7Swenxu 
377c0d59da7Swenxu 		tnl_params = &tunnel->parms.iph;
378c0d59da7Swenxu 		if (tunnel->collect_md || tnl_params->daddr == 0) {
379c29a70d2SPravin B Shelar 			__be16 flags;
380c29a70d2SPravin B Shelar 			__be64 tun_id;
3812e15ea39SPravin B Shelar 
382c29a70d2SPravin B Shelar 			flags = tpi->flags & (TUNNEL_CSUM | TUNNEL_KEY);
383d817f432SAmir Vadai 			tun_id = key32_to_tunnel_id(tpi->key);
384c29a70d2SPravin B Shelar 			tun_dst = ip_tun_rx_dst(skb, flags, tun_id, 0);
3852e15ea39SPravin B Shelar 			if (!tun_dst)
3862e15ea39SPravin B Shelar 				return PACKET_REJECT;
3872e15ea39SPravin B Shelar 		}
3882e15ea39SPravin B Shelar 
3892e15ea39SPravin B Shelar 		ip_tunnel_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
390bda7bb46SPravin B Shelar 		return PACKET_RCVD;
3911da177e4SLinus Torvalds 	}
392125372faSJiri Benc 	return PACKET_NEXT;
393244a797bSJiri Benc 
394244a797bSJiri Benc drop:
395244a797bSJiri Benc 	kfree_skb(skb);
396244a797bSJiri Benc 	return PACKET_RCVD;
3971da177e4SLinus Torvalds }
3981da177e4SLinus Torvalds 
399125372faSJiri Benc static int ipgre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi,
400125372faSJiri Benc 		     int hdr_len)
401125372faSJiri Benc {
402125372faSJiri Benc 	struct net *net = dev_net(skb->dev);
403125372faSJiri Benc 	struct ip_tunnel_net *itn;
404125372faSJiri Benc 	int res;
405125372faSJiri Benc 
406125372faSJiri Benc 	if (tpi->proto == htons(ETH_P_TEB))
407125372faSJiri Benc 		itn = net_generic(net, gre_tap_net_id);
408125372faSJiri Benc 	else
409125372faSJiri Benc 		itn = net_generic(net, ipgre_net_id);
410125372faSJiri Benc 
411125372faSJiri Benc 	res = __ipgre_rcv(skb, tpi, itn, hdr_len, false);
412125372faSJiri Benc 	if (res == PACKET_NEXT && tpi->proto == htons(ETH_P_TEB)) {
413125372faSJiri Benc 		/* ipgre tunnels in collect metadata mode should receive
414125372faSJiri Benc 		 * also ETH_P_TEB traffic.
415125372faSJiri Benc 		 */
416125372faSJiri Benc 		itn = net_generic(net, ipgre_net_id);
417125372faSJiri Benc 		res = __ipgre_rcv(skb, tpi, itn, hdr_len, true);
418125372faSJiri Benc 	}
419125372faSJiri Benc 	return res;
420125372faSJiri Benc }
421125372faSJiri Benc 
4229f57c67cSPravin B Shelar static int gre_rcv(struct sk_buff *skb)
4239f57c67cSPravin B Shelar {
4249f57c67cSPravin B Shelar 	struct tnl_ptk_info tpi;
4259f57c67cSPravin B Shelar 	bool csum_err = false;
42695f5c64cSTom Herbert 	int hdr_len;
4279f57c67cSPravin B Shelar 
4289f57c67cSPravin B Shelar #ifdef CONFIG_NET_IPGRE_BROADCAST
4299f57c67cSPravin B Shelar 	if (ipv4_is_multicast(ip_hdr(skb)->daddr)) {
4309f57c67cSPravin B Shelar 		/* Looped back packet, drop it! */
4319f57c67cSPravin B Shelar 		if (rt_is_output_route(skb_rtable(skb)))
4329f57c67cSPravin B Shelar 			goto drop;
4339f57c67cSPravin B Shelar 	}
4349f57c67cSPravin B Shelar #endif
4359f57c67cSPravin B Shelar 
436e582615aSEric Dumazet 	hdr_len = gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IP), 0);
437f132ae7cSJiri Benc 	if (hdr_len < 0)
43895f5c64cSTom Herbert 		goto drop;
43995f5c64cSTom Herbert 
440f551c91dSWilliam Tu 	if (unlikely(tpi.proto == htons(ETH_P_ERSPAN) ||
441f551c91dSWilliam Tu 		     tpi.proto == htons(ETH_P_ERSPAN2))) {
44284e54fe0SWilliam Tu 		if (erspan_rcv(skb, &tpi, hdr_len) == PACKET_RCVD)
44384e54fe0SWilliam Tu 			return 0;
444dd8d5b8cSHaishuang Yan 		goto out;
44584e54fe0SWilliam Tu 	}
44684e54fe0SWilliam Tu 
447244a797bSJiri Benc 	if (ipgre_rcv(skb, &tpi, hdr_len) == PACKET_RCVD)
4489f57c67cSPravin B Shelar 		return 0;
4499f57c67cSPravin B Shelar 
450dd8d5b8cSHaishuang Yan out:
4519f57c67cSPravin B Shelar 	icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
4529f57c67cSPravin B Shelar drop:
4539f57c67cSPravin B Shelar 	kfree_skb(skb);
4549f57c67cSPravin B Shelar 	return 0;
4559f57c67cSPravin B Shelar }
4569f57c67cSPravin B Shelar 
457c5441932SPravin B Shelar static void __gre_xmit(struct sk_buff *skb, struct net_device *dev,
458c5441932SPravin B Shelar 		       const struct iphdr *tnl_params,
459c5441932SPravin B Shelar 		       __be16 proto)
460c5441932SPravin B Shelar {
461c5441932SPravin B Shelar 	struct ip_tunnel *tunnel = netdev_priv(dev);
462ff827bebSPeilin Ye 	__be16 flags = tunnel->parms.o_flags;
463cef401deSEric Dumazet 
464c5441932SPravin B Shelar 	/* Push GRE header. */
465182a352dSTom Herbert 	gre_build_header(skb, tunnel->tun_hlen,
466ff827bebSPeilin Ye 			 flags, proto, tunnel->parms.o_key,
46731c417c9SPeilin Ye 			 (flags & TUNNEL_SEQ) ? htonl(atomic_fetch_inc(&tunnel->o_seqno)) : 0);
4681da177e4SLinus Torvalds 
469bf3d6a8fSNicolas Dichtel 	ip_tunnel_xmit(skb, dev, tnl_params, tnl_params->protocol);
4701da177e4SLinus Torvalds }
4711da177e4SLinus Torvalds 
472aed069dfSAlexander Duyck static int gre_handle_offloads(struct sk_buff *skb, bool csum)
473b2acd1dcSPravin B Shelar {
4746fa79666SEdward Cree 	return iptunnel_handle_offloads(skb, csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE);
475b2acd1dcSPravin B Shelar }
476b2acd1dcSPravin B Shelar 
477862a03c3SWilliam Tu static void gre_fb_xmit(struct sk_buff *skb, struct net_device *dev,
478862a03c3SWilliam Tu 			__be16 proto)
479862a03c3SWilliam Tu {
48077a5196aSWilliam Tu 	struct ip_tunnel *tunnel = netdev_priv(dev);
481862a03c3SWilliam Tu 	struct ip_tunnel_info *tun_info;
482862a03c3SWilliam Tu 	const struct ip_tunnel_key *key;
483862a03c3SWilliam Tu 	int tunnel_hlen;
484962924faSwenxu 	__be16 flags;
485862a03c3SWilliam Tu 
486862a03c3SWilliam Tu 	tun_info = skb_tunnel_info(skb);
487862a03c3SWilliam Tu 	if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
488862a03c3SWilliam Tu 		     ip_tunnel_info_af(tun_info) != AF_INET))
489862a03c3SWilliam Tu 		goto err_free_skb;
490862a03c3SWilliam Tu 
491862a03c3SWilliam Tu 	key = &tun_info->key;
492862a03c3SWilliam Tu 	tunnel_hlen = gre_calc_hlen(key->tun_flags);
493862a03c3SWilliam Tu 
494962924faSwenxu 	if (skb_cow_head(skb, dev->needed_headroom))
495962924faSwenxu 		goto err_free_skb;
4962e15ea39SPravin B Shelar 
4972e15ea39SPravin B Shelar 	/* Push Tunnel header. */
498aed069dfSAlexander Duyck 	if (gre_handle_offloads(skb, !!(tun_info->key.tun_flags & TUNNEL_CSUM)))
499962924faSwenxu 		goto err_free_skb;
5002e15ea39SPravin B Shelar 
50177a5196aSWilliam Tu 	flags = tun_info->key.tun_flags &
50277a5196aSWilliam Tu 		(TUNNEL_CSUM | TUNNEL_KEY | TUNNEL_SEQ);
503cba65321SDavid S. Miller 	gre_build_header(skb, tunnel_hlen, flags, proto,
50477a5196aSWilliam Tu 			 tunnel_id_to_key32(tun_info->key.tun_id),
50531c417c9SPeilin Ye 			 (flags & TUNNEL_SEQ) ? htonl(atomic_fetch_inc(&tunnel->o_seqno)) : 0);
5062e15ea39SPravin B Shelar 
507962924faSwenxu 	ip_md_tunnel_xmit(skb, dev, IPPROTO_GRE, tunnel_hlen);
508039f5062SPravin B Shelar 
5092e15ea39SPravin B Shelar 	return;
5102e15ea39SPravin B Shelar 
5112e15ea39SPravin B Shelar err_free_skb:
5122e15ea39SPravin B Shelar 	kfree_skb(skb);
5132e15ea39SPravin B Shelar 	dev->stats.tx_dropped++;
5142e15ea39SPravin B Shelar }
5152e15ea39SPravin B Shelar 
51620704bd1SXin Long static void erspan_fb_xmit(struct sk_buff *skb, struct net_device *dev)
5171a66a836SWilliam Tu {
5181a66a836SWilliam Tu 	struct ip_tunnel *tunnel = netdev_priv(dev);
5191a66a836SWilliam Tu 	struct ip_tunnel_info *tun_info;
5201a66a836SWilliam Tu 	const struct ip_tunnel_key *key;
5211a66a836SWilliam Tu 	struct erspan_metadata *md;
5221a66a836SWilliam Tu 	bool truncate = false;
523962924faSwenxu 	__be16 proto;
5241a66a836SWilliam Tu 	int tunnel_hlen;
525f551c91dSWilliam Tu 	int version;
5261baf5ebfSWilliam Tu 	int nhoff;
5271a66a836SWilliam Tu 
5281a66a836SWilliam Tu 	tun_info = skb_tunnel_info(skb);
5291a66a836SWilliam Tu 	if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
5301a66a836SWilliam Tu 		     ip_tunnel_info_af(tun_info) != AF_INET))
5311a66a836SWilliam Tu 		goto err_free_skb;
5321a66a836SWilliam Tu 
5331a66a836SWilliam Tu 	key = &tun_info->key;
534256c87c1SPieter Jansen van Vuuren 	if (!(tun_info->key.tun_flags & TUNNEL_ERSPAN_OPT))
535962924faSwenxu 		goto err_free_skb;
5362eb8d6d2SXin Long 	if (tun_info->options_len < sizeof(*md))
537962924faSwenxu 		goto err_free_skb;
5382eb8d6d2SXin Long 	md = ip_tunnel_info_opts(tun_info);
5391a66a836SWilliam Tu 
5401a66a836SWilliam Tu 	/* ERSPAN has fixed 8 byte GRE header */
541f551c91dSWilliam Tu 	version = md->version;
542f551c91dSWilliam Tu 	tunnel_hlen = 8 + erspan_hdr_len(version);
5431a66a836SWilliam Tu 
544962924faSwenxu 	if (skb_cow_head(skb, dev->needed_headroom))
545962924faSwenxu 		goto err_free_skb;
5461a66a836SWilliam Tu 
5471a66a836SWilliam Tu 	if (gre_handle_offloads(skb, false))
548962924faSwenxu 		goto err_free_skb;
5491a66a836SWilliam Tu 
550f192970dSWilliam Tu 	if (skb->len > dev->mtu + dev->hard_header_len) {
551f192970dSWilliam Tu 		pskb_trim(skb, dev->mtu + dev->hard_header_len);
5521a66a836SWilliam Tu 		truncate = true;
5531a66a836SWilliam Tu 	}
5541a66a836SWilliam Tu 
5551baf5ebfSWilliam Tu 	nhoff = skb_network_header(skb) - skb_mac_header(skb);
5561baf5ebfSWilliam Tu 	if (skb->protocol == htons(ETH_P_IP) &&
5571baf5ebfSWilliam Tu 	    (ntohs(ip_hdr(skb)->tot_len) > skb->len - nhoff))
5581baf5ebfSWilliam Tu 		truncate = true;
5591baf5ebfSWilliam Tu 
560*301bd140SEric Dumazet 	if (skb->protocol == htons(ETH_P_IPV6)) {
561*301bd140SEric Dumazet 		int thoff;
562*301bd140SEric Dumazet 
563*301bd140SEric Dumazet 		if (skb_transport_header_was_set(skb))
564d5db21a3SWilliam Tu 			thoff = skb_transport_header(skb) - skb_mac_header(skb);
565*301bd140SEric Dumazet 		else
566*301bd140SEric Dumazet 			thoff = nhoff + sizeof(struct ipv6hdr);
567*301bd140SEric Dumazet 		if (ntohs(ipv6_hdr(skb)->payload_len) > skb->len - thoff)
568d5db21a3SWilliam Tu 			truncate = true;
569*301bd140SEric Dumazet 	}
570d5db21a3SWilliam Tu 
571f551c91dSWilliam Tu 	if (version == 1) {
572c69de58bSWilliam Tu 		erspan_build_header(skb, ntohl(tunnel_id_to_key32(key->tun_id)),
5731d7e2ed2SWilliam Tu 				    ntohl(md->u.index), truncate, true);
57420704bd1SXin Long 		proto = htons(ETH_P_ERSPAN);
575f551c91dSWilliam Tu 	} else if (version == 2) {
576c69de58bSWilliam Tu 		erspan_build_header_v2(skb,
577c69de58bSWilliam Tu 				       ntohl(tunnel_id_to_key32(key->tun_id)),
578c69de58bSWilliam Tu 				       md->u.md2.dir,
579c69de58bSWilliam Tu 				       get_hwid(&md->u.md2),
580c69de58bSWilliam Tu 				       truncate, true);
58120704bd1SXin Long 		proto = htons(ETH_P_ERSPAN2);
582f551c91dSWilliam Tu 	} else {
583962924faSwenxu 		goto err_free_skb;
584f551c91dSWilliam Tu 	}
5851a66a836SWilliam Tu 
5861a66a836SWilliam Tu 	gre_build_header(skb, 8, TUNNEL_SEQ,
58731c417c9SPeilin Ye 			 proto, 0, htonl(atomic_fetch_inc(&tunnel->o_seqno)));
5881a66a836SWilliam Tu 
589962924faSwenxu 	ip_md_tunnel_xmit(skb, dev, IPPROTO_GRE, tunnel_hlen);
5901a66a836SWilliam Tu 
5911a66a836SWilliam Tu 	return;
5921a66a836SWilliam Tu 
5931a66a836SWilliam Tu err_free_skb:
5941a66a836SWilliam Tu 	kfree_skb(skb);
5951a66a836SWilliam Tu 	dev->stats.tx_dropped++;
5961a66a836SWilliam Tu }
5971a66a836SWilliam Tu 
598fc4099f1SPravin B Shelar static int gre_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
599fc4099f1SPravin B Shelar {
600fc4099f1SPravin B Shelar 	struct ip_tunnel_info *info = skb_tunnel_info(skb);
601962924faSwenxu 	const struct ip_tunnel_key *key;
602fc4099f1SPravin B Shelar 	struct rtable *rt;
603fc4099f1SPravin B Shelar 	struct flowi4 fl4;
604fc4099f1SPravin B Shelar 
605fc4099f1SPravin B Shelar 	if (ip_tunnel_info_af(info) != AF_INET)
606fc4099f1SPravin B Shelar 		return -EINVAL;
607fc4099f1SPravin B Shelar 
608962924faSwenxu 	key = &info->key;
609962924faSwenxu 	ip_tunnel_init_flow(&fl4, IPPROTO_GRE, key->u.ipv4.dst, key->u.ipv4.src,
610f7716b31SGuillaume Nault 			    tunnel_id_to_key32(key->tun_id),
611db53cd3dSDavid Ahern 			    key->tos & ~INET_ECN_MASK, dev_net(dev), 0,
612db53cd3dSDavid Ahern 			    skb->mark, skb_get_hash(skb));
613962924faSwenxu 	rt = ip_route_output_key(dev_net(dev), &fl4);
614fc4099f1SPravin B Shelar 	if (IS_ERR(rt))
615fc4099f1SPravin B Shelar 		return PTR_ERR(rt);
616fc4099f1SPravin B Shelar 
617fc4099f1SPravin B Shelar 	ip_rt_put(rt);
618fc4099f1SPravin B Shelar 	info->key.u.ipv4.src = fl4.saddr;
619fc4099f1SPravin B Shelar 	return 0;
620fc4099f1SPravin B Shelar }
621fc4099f1SPravin B Shelar 
622c5441932SPravin B Shelar static netdev_tx_t ipgre_xmit(struct sk_buff *skb,
623c5441932SPravin B Shelar 			      struct net_device *dev)
624ee34c1ebSMichal Schmidt {
625c5441932SPravin B Shelar 	struct ip_tunnel *tunnel = netdev_priv(dev);
626c5441932SPravin B Shelar 	const struct iphdr *tnl_params;
627ee34c1ebSMichal Schmidt 
628cb9f1b78SWillem de Bruijn 	if (!pskb_inet_may_pull(skb))
629cb9f1b78SWillem de Bruijn 		goto free_skb;
630cb9f1b78SWillem de Bruijn 
6312e15ea39SPravin B Shelar 	if (tunnel->collect_md) {
6322090714eSJiri Benc 		gre_fb_xmit(skb, dev, skb->protocol);
6332e15ea39SPravin B Shelar 		return NETDEV_TX_OK;
6342e15ea39SPravin B Shelar 	}
6352e15ea39SPravin B Shelar 
636c5441932SPravin B Shelar 	if (dev->header_ops) {
637fdafed45SCong Wang 		if (skb_cow_head(skb, 0))
638c5441932SPravin B Shelar 			goto free_skb;
639ee34c1ebSMichal Schmidt 
640c5441932SPravin B Shelar 		tnl_params = (const struct iphdr *)skb->data;
641cbb1e85fSDavid S. Miller 
642c5441932SPravin B Shelar 		/* Pull skb since ip_tunnel_xmit() needs skb->data pointing
643c5441932SPravin B Shelar 		 * to gre header.
644c5441932SPravin B Shelar 		 */
6458d21e996SWillem de Bruijn 		skb_pull(skb, tunnel->hlen + sizeof(struct iphdr));
6468a0033a9STimo Teräs 		skb_reset_mac_header(skb);
6478d21e996SWillem de Bruijn 
6488d21e996SWillem de Bruijn 		if (skb->ip_summed == CHECKSUM_PARTIAL &&
6498d21e996SWillem de Bruijn 		    skb_checksum_start(skb) < skb->data)
6508d21e996SWillem de Bruijn 			goto free_skb;
651c5441932SPravin B Shelar 	} else {
652c5441932SPravin B Shelar 		if (skb_cow_head(skb, dev->needed_headroom))
653c5441932SPravin B Shelar 			goto free_skb;
654c5441932SPravin B Shelar 
655c5441932SPravin B Shelar 		tnl_params = &tunnel->parms.iph;
656ee34c1ebSMichal Schmidt 	}
657e1a80002SHerbert Xu 
658aed069dfSAlexander Duyck 	if (gre_handle_offloads(skb, !!(tunnel->parms.o_flags & TUNNEL_CSUM)))
659aed069dfSAlexander Duyck 		goto free_skb;
6608a0033a9STimo Teräs 
661c5441932SPravin B Shelar 	__gre_xmit(skb, dev, tnl_params, skb->protocol);
662c5441932SPravin B Shelar 	return NETDEV_TX_OK;
663c5441932SPravin B Shelar 
664c5441932SPravin B Shelar free_skb:
6653acfa1e7SEric Dumazet 	kfree_skb(skb);
666c5441932SPravin B Shelar 	dev->stats.tx_dropped++;
667c5441932SPravin B Shelar 	return NETDEV_TX_OK;
668ee34c1ebSMichal Schmidt }
669ee34c1ebSMichal Schmidt 
67084e54fe0SWilliam Tu static netdev_tx_t erspan_xmit(struct sk_buff *skb,
67184e54fe0SWilliam Tu 			       struct net_device *dev)
67284e54fe0SWilliam Tu {
67384e54fe0SWilliam Tu 	struct ip_tunnel *tunnel = netdev_priv(dev);
67484e54fe0SWilliam Tu 	bool truncate = false;
67520704bd1SXin Long 	__be16 proto;
67684e54fe0SWilliam Tu 
677cb9f1b78SWillem de Bruijn 	if (!pskb_inet_may_pull(skb))
678cb9f1b78SWillem de Bruijn 		goto free_skb;
679cb9f1b78SWillem de Bruijn 
6801a66a836SWilliam Tu 	if (tunnel->collect_md) {
68120704bd1SXin Long 		erspan_fb_xmit(skb, dev);
6821a66a836SWilliam Tu 		return NETDEV_TX_OK;
6831a66a836SWilliam Tu 	}
6841a66a836SWilliam Tu 
68584e54fe0SWilliam Tu 	if (gre_handle_offloads(skb, false))
68684e54fe0SWilliam Tu 		goto free_skb;
68784e54fe0SWilliam Tu 
68884e54fe0SWilliam Tu 	if (skb_cow_head(skb, dev->needed_headroom))
68984e54fe0SWilliam Tu 		goto free_skb;
69084e54fe0SWilliam Tu 
691f192970dSWilliam Tu 	if (skb->len > dev->mtu + dev->hard_header_len) {
692f192970dSWilliam Tu 		pskb_trim(skb, dev->mtu + dev->hard_header_len);
69384e54fe0SWilliam Tu 		truncate = true;
69484e54fe0SWilliam Tu 	}
69584e54fe0SWilliam Tu 
69684e54fe0SWilliam Tu 	/* Push ERSPAN header */
697f989d546SWilliam Tu 	if (tunnel->erspan_ver == 0) {
698f989d546SWilliam Tu 		proto = htons(ETH_P_ERSPAN);
699f989d546SWilliam Tu 		tunnel->parms.o_flags &= ~TUNNEL_SEQ;
700f989d546SWilliam Tu 	} else if (tunnel->erspan_ver == 1) {
701c69de58bSWilliam Tu 		erspan_build_header(skb, ntohl(tunnel->parms.o_key),
702c69de58bSWilliam Tu 				    tunnel->index,
703a3222dc9SWilliam Tu 				    truncate, true);
70420704bd1SXin Long 		proto = htons(ETH_P_ERSPAN);
70520704bd1SXin Long 	} else if (tunnel->erspan_ver == 2) {
706c69de58bSWilliam Tu 		erspan_build_header_v2(skb, ntohl(tunnel->parms.o_key),
707f551c91dSWilliam Tu 				       tunnel->dir, tunnel->hwid,
708f551c91dSWilliam Tu 				       truncate, true);
70920704bd1SXin Long 		proto = htons(ETH_P_ERSPAN2);
71020704bd1SXin Long 	} else {
71102f99df1SWilliam Tu 		goto free_skb;
71220704bd1SXin Long 	}
713f551c91dSWilliam Tu 
71484e54fe0SWilliam Tu 	tunnel->parms.o_flags &= ~TUNNEL_KEY;
71520704bd1SXin Long 	__gre_xmit(skb, dev, &tunnel->parms.iph, proto);
71684e54fe0SWilliam Tu 	return NETDEV_TX_OK;
71784e54fe0SWilliam Tu 
71884e54fe0SWilliam Tu free_skb:
71984e54fe0SWilliam Tu 	kfree_skb(skb);
72084e54fe0SWilliam Tu 	dev->stats.tx_dropped++;
72184e54fe0SWilliam Tu 	return NETDEV_TX_OK;
72284e54fe0SWilliam Tu }
72384e54fe0SWilliam Tu 
724c5441932SPravin B Shelar static netdev_tx_t gre_tap_xmit(struct sk_buff *skb,
725c5441932SPravin B Shelar 				struct net_device *dev)
726c5441932SPravin B Shelar {
727c5441932SPravin B Shelar 	struct ip_tunnel *tunnel = netdev_priv(dev);
728ee34c1ebSMichal Schmidt 
729cb9f1b78SWillem de Bruijn 	if (!pskb_inet_may_pull(skb))
730cb9f1b78SWillem de Bruijn 		goto free_skb;
731cb9f1b78SWillem de Bruijn 
7322e15ea39SPravin B Shelar 	if (tunnel->collect_md) {
7332090714eSJiri Benc 		gre_fb_xmit(skb, dev, htons(ETH_P_TEB));
7342e15ea39SPravin B Shelar 		return NETDEV_TX_OK;
7352e15ea39SPravin B Shelar 	}
7362e15ea39SPravin B Shelar 
737aed069dfSAlexander Duyck 	if (gre_handle_offloads(skb, !!(tunnel->parms.o_flags & TUNNEL_CSUM)))
738aed069dfSAlexander Duyck 		goto free_skb;
739ee34c1ebSMichal Schmidt 
740c5441932SPravin B Shelar 	if (skb_cow_head(skb, dev->needed_headroom))
741c5441932SPravin B Shelar 		goto free_skb;
74242aa9162SHerbert Xu 
743c5441932SPravin B Shelar 	__gre_xmit(skb, dev, &tunnel->parms.iph, htons(ETH_P_TEB));
744c5441932SPravin B Shelar 	return NETDEV_TX_OK;
745c5441932SPravin B Shelar 
746c5441932SPravin B Shelar free_skb:
7473acfa1e7SEric Dumazet 	kfree_skb(skb);
748c5441932SPravin B Shelar 	dev->stats.tx_dropped++;
749c5441932SPravin B Shelar 	return NETDEV_TX_OK;
75068c33163SPravin B Shelar }
751ee34c1ebSMichal Schmidt 
752dd9d598cSXin Long static void ipgre_link_update(struct net_device *dev, bool set_mtu)
753dd9d598cSXin Long {
754dd9d598cSXin Long 	struct ip_tunnel *tunnel = netdev_priv(dev);
755020e8f60SPeilin Ye 	__be16 flags;
756dd9d598cSXin Long 	int len;
757dd9d598cSXin Long 
758dd9d598cSXin Long 	len = tunnel->tun_hlen;
759dd9d598cSXin Long 	tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
760dd9d598cSXin Long 	len = tunnel->tun_hlen - len;
761dd9d598cSXin Long 	tunnel->hlen = tunnel->hlen + len;
762dd9d598cSXin Long 
763fdafed45SCong Wang 	if (dev->header_ops)
764fdafed45SCong Wang 		dev->hard_header_len += len;
765fdafed45SCong Wang 	else
766fdafed45SCong Wang 		dev->needed_headroom += len;
767fdafed45SCong Wang 
768dd9d598cSXin Long 	if (set_mtu)
769dd9d598cSXin Long 		dev->mtu = max_t(int, dev->mtu - len, 68);
770dd9d598cSXin Long 
771020e8f60SPeilin Ye 	flags = tunnel->parms.o_flags;
772020e8f60SPeilin Ye 
773020e8f60SPeilin Ye 	if (flags & TUNNEL_SEQ ||
774020e8f60SPeilin Ye 	    (flags & TUNNEL_CSUM && tunnel->encap.type != TUNNEL_ENCAP_NONE)) {
7751cc5954fSSabrina Dubroca 		dev->features &= ~NETIF_F_GSO_SOFTWARE;
7761cc5954fSSabrina Dubroca 		dev->hw_features &= ~NETIF_F_GSO_SOFTWARE;
7771cc5954fSSabrina Dubroca 	} else {
778020e8f60SPeilin Ye 		dev->features |= NETIF_F_GSO_SOFTWARE;
779020e8f60SPeilin Ye 		dev->hw_features |= NETIF_F_GSO_SOFTWARE;
780dd9d598cSXin Long 	}
781dd9d598cSXin Long }
782dd9d598cSXin Long 
783607259a6SChristoph Hellwig static int ipgre_tunnel_ctl(struct net_device *dev, struct ip_tunnel_parm *p,
784607259a6SChristoph Hellwig 			    int cmd)
7851da177e4SLinus Torvalds {
786a0efab67SXin Long 	int err;
7871da177e4SLinus Torvalds 
7886c734fb8SCong Wang 	if (cmd == SIOCADDTUNNEL || cmd == SIOCCHGTUNNEL) {
789607259a6SChristoph Hellwig 		if (p->iph.version != 4 || p->iph.protocol != IPPROTO_GRE ||
790607259a6SChristoph Hellwig 		    p->iph.ihl != 5 || (p->iph.frag_off & htons(~IP_DF)) ||
791607259a6SChristoph Hellwig 		    ((p->i_flags | p->o_flags) & (GRE_VERSION | GRE_ROUTING)))
7921da177e4SLinus Torvalds 			return -EINVAL;
793c5441932SPravin B Shelar 	}
794a0efab67SXin Long 
795607259a6SChristoph Hellwig 	p->i_flags = gre_flags_to_tnl_flags(p->i_flags);
796607259a6SChristoph Hellwig 	p->o_flags = gre_flags_to_tnl_flags(p->o_flags);
797c5441932SPravin B Shelar 
798607259a6SChristoph Hellwig 	err = ip_tunnel_ctl(dev, p, cmd);
799c5441932SPravin B Shelar 	if (err)
800c5441932SPravin B Shelar 		return err;
801c5441932SPravin B Shelar 
802a0efab67SXin Long 	if (cmd == SIOCCHGTUNNEL) {
803a0efab67SXin Long 		struct ip_tunnel *t = netdev_priv(dev);
804a0efab67SXin Long 
805607259a6SChristoph Hellwig 		t->parms.i_flags = p->i_flags;
806607259a6SChristoph Hellwig 		t->parms.o_flags = p->o_flags;
807a0efab67SXin Long 
808a0efab67SXin Long 		if (strcmp(dev->rtnl_link_ops->kind, "erspan"))
809a0efab67SXin Long 			ipgre_link_update(dev, true);
810a0efab67SXin Long 	}
811a0efab67SXin Long 
812607259a6SChristoph Hellwig 	p->i_flags = gre_tnl_flags_to_gre_flags(p->i_flags);
813607259a6SChristoph Hellwig 	p->o_flags = gre_tnl_flags_to_gre_flags(p->o_flags);
8141da177e4SLinus Torvalds 	return 0;
8151da177e4SLinus Torvalds }
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds /* Nice toy. Unfortunately, useless in real life :-)
8181da177e4SLinus Torvalds    It allows to construct virtual multiprotocol broadcast "LAN"
8191da177e4SLinus Torvalds    over the Internet, provided multicast routing is tuned.
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 
8221da177e4SLinus Torvalds    I have no idea was this bicycle invented before me,
8231da177e4SLinus Torvalds    so that I had to set ARPHRD_IPGRE to a random value.
8241da177e4SLinus Torvalds    I have an impression, that Cisco could make something similar,
8251da177e4SLinus Torvalds    but this feature is apparently missing in IOS<=11.2(8).
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds    I set up 10.66.66/24 and fec0:6666:6666::0/96 as virtual networks
8281da177e4SLinus Torvalds    with broadcast 224.66.66.66. If you have access to mbone, play with me :-)
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds    ping -t 255 224.66.66.66
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds    If nobody answers, mbone does not work.
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds    ip tunnel add Universe mode gre remote 224.66.66.66 local <Your_real_addr> ttl 255
8351da177e4SLinus Torvalds    ip addr add 10.66.66.<somewhat>/24 dev Universe
8361da177e4SLinus Torvalds    ifconfig Universe up
8371da177e4SLinus Torvalds    ifconfig Universe add fe80::<Your_real_addr>/10
8381da177e4SLinus Torvalds    ifconfig Universe add fec0:6666:6666::<Your_real_addr>/96
8391da177e4SLinus Torvalds    ftp 10.66.66.66
8401da177e4SLinus Torvalds    ...
8411da177e4SLinus Torvalds    ftp fec0:6666:6666::193.233.7.65
8421da177e4SLinus Torvalds    ...
8431da177e4SLinus Torvalds  */
8443b04dddeSStephen Hemminger static int ipgre_header(struct sk_buff *skb, struct net_device *dev,
8453b04dddeSStephen Hemminger 			unsigned short type,
8461507850bSEric Dumazet 			const void *daddr, const void *saddr, unsigned int len)
8471da177e4SLinus Torvalds {
8482941a486SPatrick McHardy 	struct ip_tunnel *t = netdev_priv(dev);
849c5441932SPravin B Shelar 	struct iphdr *iph;
850c5441932SPravin B Shelar 	struct gre_base_hdr *greh;
851c5441932SPravin B Shelar 
852d58ff351SJohannes Berg 	iph = skb_push(skb, t->hlen + sizeof(*iph));
853c5441932SPravin B Shelar 	greh = (struct gre_base_hdr *)(iph+1);
85495f5c64cSTom Herbert 	greh->flags = gre_tnl_flags_to_gre_flags(t->parms.o_flags);
855c5441932SPravin B Shelar 	greh->protocol = htons(type);
8561da177e4SLinus Torvalds 
8571da177e4SLinus Torvalds 	memcpy(iph, &t->parms.iph, sizeof(struct iphdr));
8581da177e4SLinus Torvalds 
859c5441932SPravin B Shelar 	/* Set the source hardware address. */
8601da177e4SLinus Torvalds 	if (saddr)
8611da177e4SLinus Torvalds 		memcpy(&iph->saddr, saddr, 4);
8626d55cb91STimo Teräs 	if (daddr)
8631da177e4SLinus Torvalds 		memcpy(&iph->daddr, daddr, 4);
8646d55cb91STimo Teräs 	if (iph->daddr)
86577a482bdSTimo Teräs 		return t->hlen + sizeof(*iph);
8661da177e4SLinus Torvalds 
867c5441932SPravin B Shelar 	return -(t->hlen + sizeof(*iph));
8681da177e4SLinus Torvalds }
8691da177e4SLinus Torvalds 
8706a5f44d7STimo Teras static int ipgre_header_parse(const struct sk_buff *skb, unsigned char *haddr)
8716a5f44d7STimo Teras {
872b71d1d42SEric Dumazet 	const struct iphdr *iph = (const struct iphdr *) skb_mac_header(skb);
8736a5f44d7STimo Teras 	memcpy(haddr, &iph->saddr, 4);
8746a5f44d7STimo Teras 	return 4;
8756a5f44d7STimo Teras }
8766a5f44d7STimo Teras 
8773b04dddeSStephen Hemminger static const struct header_ops ipgre_header_ops = {
8783b04dddeSStephen Hemminger 	.create	= ipgre_header,
8796a5f44d7STimo Teras 	.parse	= ipgre_header_parse,
8803b04dddeSStephen Hemminger };
8813b04dddeSStephen Hemminger 
8826a5f44d7STimo Teras #ifdef CONFIG_NET_IPGRE_BROADCAST
8831da177e4SLinus Torvalds static int ipgre_open(struct net_device *dev)
8841da177e4SLinus Torvalds {
8852941a486SPatrick McHardy 	struct ip_tunnel *t = netdev_priv(dev);
8861da177e4SLinus Torvalds 
887f97c1e0cSJoe Perches 	if (ipv4_is_multicast(t->parms.iph.daddr)) {
888cbb1e85fSDavid S. Miller 		struct flowi4 fl4;
889cbb1e85fSDavid S. Miller 		struct rtable *rt;
890cbb1e85fSDavid S. Miller 
891b57708adSNicolas Dichtel 		rt = ip_route_output_gre(t->net, &fl4,
89278fbfd8aSDavid S. Miller 					 t->parms.iph.daddr,
89378fbfd8aSDavid S. Miller 					 t->parms.iph.saddr,
89478fbfd8aSDavid S. Miller 					 t->parms.o_key,
89578fbfd8aSDavid S. Miller 					 RT_TOS(t->parms.iph.tos),
89678fbfd8aSDavid S. Miller 					 t->parms.link);
897b23dd4feSDavid S. Miller 		if (IS_ERR(rt))
8981da177e4SLinus Torvalds 			return -EADDRNOTAVAIL;
899d8d1f30bSChangli Gao 		dev = rt->dst.dev;
9001da177e4SLinus Torvalds 		ip_rt_put(rt);
90151456b29SIan Morris 		if (!__in_dev_get_rtnl(dev))
9021da177e4SLinus Torvalds 			return -EADDRNOTAVAIL;
9031da177e4SLinus Torvalds 		t->mlink = dev->ifindex;
904e5ed6399SHerbert Xu 		ip_mc_inc_group(__in_dev_get_rtnl(dev), t->parms.iph.daddr);
9051da177e4SLinus Torvalds 	}
9061da177e4SLinus Torvalds 	return 0;
9071da177e4SLinus Torvalds }
9081da177e4SLinus Torvalds 
9091da177e4SLinus Torvalds static int ipgre_close(struct net_device *dev)
9101da177e4SLinus Torvalds {
9112941a486SPatrick McHardy 	struct ip_tunnel *t = netdev_priv(dev);
912b8c26a33SStephen Hemminger 
913f97c1e0cSJoe Perches 	if (ipv4_is_multicast(t->parms.iph.daddr) && t->mlink) {
9147fee0ca2SDenis V. Lunev 		struct in_device *in_dev;
915b57708adSNicolas Dichtel 		in_dev = inetdev_by_index(t->net, t->mlink);
9168723e1b4SEric Dumazet 		if (in_dev)
9171da177e4SLinus Torvalds 			ip_mc_dec_group(in_dev, t->parms.iph.daddr);
9181da177e4SLinus Torvalds 	}
9191da177e4SLinus Torvalds 	return 0;
9201da177e4SLinus Torvalds }
9211da177e4SLinus Torvalds #endif
9221da177e4SLinus Torvalds 
923b8c26a33SStephen Hemminger static const struct net_device_ops ipgre_netdev_ops = {
924b8c26a33SStephen Hemminger 	.ndo_init		= ipgre_tunnel_init,
925c5441932SPravin B Shelar 	.ndo_uninit		= ip_tunnel_uninit,
926b8c26a33SStephen Hemminger #ifdef CONFIG_NET_IPGRE_BROADCAST
927b8c26a33SStephen Hemminger 	.ndo_open		= ipgre_open,
928b8c26a33SStephen Hemminger 	.ndo_stop		= ipgre_close,
929b8c26a33SStephen Hemminger #endif
930c5441932SPravin B Shelar 	.ndo_start_xmit		= ipgre_xmit,
9313e7a1c7cSArnd Bergmann 	.ndo_siocdevprivate	= ip_tunnel_siocdevprivate,
932c5441932SPravin B Shelar 	.ndo_change_mtu		= ip_tunnel_change_mtu,
93398d7fc46SHeiner Kallweit 	.ndo_get_stats64	= dev_get_tstats64,
9341e99584bSNicolas Dichtel 	.ndo_get_iflink		= ip_tunnel_get_iflink,
935607259a6SChristoph Hellwig 	.ndo_tunnel_ctl		= ipgre_tunnel_ctl,
936b8c26a33SStephen Hemminger };
937b8c26a33SStephen Hemminger 
9386b78f16eSEric Dumazet #define GRE_FEATURES (NETIF_F_SG |		\
9396b78f16eSEric Dumazet 		      NETIF_F_FRAGLIST |	\
9406b78f16eSEric Dumazet 		      NETIF_F_HIGHDMA |		\
9416b78f16eSEric Dumazet 		      NETIF_F_HW_CSUM)
9426b78f16eSEric Dumazet 
9431da177e4SLinus Torvalds static void ipgre_tunnel_setup(struct net_device *dev)
9441da177e4SLinus Torvalds {
945b8c26a33SStephen Hemminger 	dev->netdev_ops		= &ipgre_netdev_ops;
9465a455275SNicolas Dichtel 	dev->type		= ARPHRD_IPGRE;
947c5441932SPravin B Shelar 	ip_tunnel_setup(dev, ipgre_net_id);
948c5441932SPravin B Shelar }
9491da177e4SLinus Torvalds 
950c5441932SPravin B Shelar static void __gre_tunnel_init(struct net_device *dev)
951c5441932SPravin B Shelar {
952c5441932SPravin B Shelar 	struct ip_tunnel *tunnel;
953020e8f60SPeilin Ye 	__be16 flags;
954c5441932SPravin B Shelar 
955c5441932SPravin B Shelar 	tunnel = netdev_priv(dev);
95695f5c64cSTom Herbert 	tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
957c5441932SPravin B Shelar 	tunnel->parms.iph.protocol = IPPROTO_GRE;
958c5441932SPravin B Shelar 
9594565e991STom Herbert 	tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen;
960fdafed45SCong Wang 	dev->needed_headroom = tunnel->hlen + sizeof(tunnel->parms.iph);
9614565e991STom Herbert 
962020e8f60SPeilin Ye 	dev->features		|= GRE_FEATURES | NETIF_F_LLTX;
9636b78f16eSEric Dumazet 	dev->hw_features	|= GRE_FEATURES;
964c5441932SPravin B Shelar 
965020e8f60SPeilin Ye 	flags = tunnel->parms.o_flags;
966020e8f60SPeilin Ye 
967020e8f60SPeilin Ye 	/* TCP offload with GRE SEQ is not supported, nor can we support 2
968020e8f60SPeilin Ye 	 * levels of outer headers requiring an update.
969a0ca153fSAlexander Duyck 	 */
970020e8f60SPeilin Ye 	if (flags & TUNNEL_SEQ)
971020e8f60SPeilin Ye 		return;
972020e8f60SPeilin Ye 	if (flags & TUNNEL_CSUM && tunnel->encap.type != TUNNEL_ENCAP_NONE)
973020e8f60SPeilin Ye 		return;
974020e8f60SPeilin Ye 
975c5441932SPravin B Shelar 	dev->features |= NETIF_F_GSO_SOFTWARE;
976c5441932SPravin B Shelar 	dev->hw_features |= NETIF_F_GSO_SOFTWARE;
977a0ca153fSAlexander Duyck }
978a0ca153fSAlexander Duyck 
9791da177e4SLinus Torvalds static int ipgre_tunnel_init(struct net_device *dev)
9801da177e4SLinus Torvalds {
981c5441932SPravin B Shelar 	struct ip_tunnel *tunnel = netdev_priv(dev);
982c5441932SPravin B Shelar 	struct iphdr *iph = &tunnel->parms.iph;
9831da177e4SLinus Torvalds 
984c5441932SPravin B Shelar 	__gre_tunnel_init(dev);
9851da177e4SLinus Torvalds 
9865a1b7e1aSJakub Kicinski 	__dev_addr_set(dev, &iph->saddr, 4);
987c5441932SPravin B Shelar 	memcpy(dev->broadcast, &iph->daddr, 4);
9881da177e4SLinus Torvalds 
989c5441932SPravin B Shelar 	dev->flags		= IFF_NOARP;
99002875878SEric Dumazet 	netif_keep_dst(dev);
991c5441932SPravin B Shelar 	dev->addr_len		= 4;
9921da177e4SLinus Torvalds 
993a64b04d8SJiri Benc 	if (iph->daddr && !tunnel->collect_md) {
9941da177e4SLinus Torvalds #ifdef CONFIG_NET_IPGRE_BROADCAST
995f97c1e0cSJoe Perches 		if (ipv4_is_multicast(iph->daddr)) {
9961da177e4SLinus Torvalds 			if (!iph->saddr)
9971da177e4SLinus Torvalds 				return -EINVAL;
9981da177e4SLinus Torvalds 			dev->flags = IFF_BROADCAST;
9993b04dddeSStephen Hemminger 			dev->header_ops = &ipgre_header_ops;
1000fdafed45SCong Wang 			dev->hard_header_len = tunnel->hlen + sizeof(*iph);
1001fdafed45SCong Wang 			dev->needed_headroom = 0;
10021da177e4SLinus Torvalds 		}
10031da177e4SLinus Torvalds #endif
1004a64b04d8SJiri Benc 	} else if (!tunnel->collect_md) {
10056a5f44d7STimo Teras 		dev->header_ops = &ipgre_header_ops;
1006fdafed45SCong Wang 		dev->hard_header_len = tunnel->hlen + sizeof(*iph);
1007fdafed45SCong Wang 		dev->needed_headroom = 0;
1008a64b04d8SJiri Benc 	}
10091da177e4SLinus Torvalds 
1010c5441932SPravin B Shelar 	return ip_tunnel_init(dev);
101160769a5dSEric Dumazet }
101260769a5dSEric Dumazet 
10139f57c67cSPravin B Shelar static const struct gre_protocol ipgre_protocol = {
10149f57c67cSPravin B Shelar 	.handler     = gre_rcv,
10159f57c67cSPravin B Shelar 	.err_handler = gre_err,
10161da177e4SLinus Torvalds };
10171da177e4SLinus Torvalds 
10182c8c1e72SAlexey Dobriyan static int __net_init ipgre_init_net(struct net *net)
101959a4c759SPavel Emelyanov {
1020c5441932SPravin B Shelar 	return ip_tunnel_init_net(net, ipgre_net_id, &ipgre_link_ops, NULL);
102159a4c759SPavel Emelyanov }
102259a4c759SPavel Emelyanov 
102364bc1781SEric Dumazet static void __net_exit ipgre_exit_batch_net(struct list_head *list_net)
102459a4c759SPavel Emelyanov {
102564bc1781SEric Dumazet 	ip_tunnel_delete_nets(list_net, ipgre_net_id, &ipgre_link_ops);
102659a4c759SPavel Emelyanov }
102759a4c759SPavel Emelyanov 
102859a4c759SPavel Emelyanov static struct pernet_operations ipgre_net_ops = {
102959a4c759SPavel Emelyanov 	.init = ipgre_init_net,
103064bc1781SEric Dumazet 	.exit_batch = ipgre_exit_batch_net,
1031cfb8fbf2SEric W. Biederman 	.id   = &ipgre_net_id,
1032c5441932SPravin B Shelar 	.size = sizeof(struct ip_tunnel_net),
103359a4c759SPavel Emelyanov };
10341da177e4SLinus Torvalds 
1035a8b8a889SMatthias Schiffer static int ipgre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[],
1036a8b8a889SMatthias Schiffer 				 struct netlink_ext_ack *extack)
1037c19e654dSHerbert Xu {
1038c19e654dSHerbert Xu 	__be16 flags;
1039c19e654dSHerbert Xu 
1040c19e654dSHerbert Xu 	if (!data)
1041c19e654dSHerbert Xu 		return 0;
1042c19e654dSHerbert Xu 
1043c19e654dSHerbert Xu 	flags = 0;
1044c19e654dSHerbert Xu 	if (data[IFLA_GRE_IFLAGS])
1045c19e654dSHerbert Xu 		flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1046c19e654dSHerbert Xu 	if (data[IFLA_GRE_OFLAGS])
1047c19e654dSHerbert Xu 		flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1048c19e654dSHerbert Xu 	if (flags & (GRE_VERSION|GRE_ROUTING))
1049c19e654dSHerbert Xu 		return -EINVAL;
1050c19e654dSHerbert Xu 
1051946b636fSJiri Benc 	if (data[IFLA_GRE_COLLECT_METADATA] &&
1052946b636fSJiri Benc 	    data[IFLA_GRE_ENCAP_TYPE] &&
1053946b636fSJiri Benc 	    nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]) != TUNNEL_ENCAP_NONE)
1054946b636fSJiri Benc 		return -EINVAL;
1055946b636fSJiri Benc 
1056c19e654dSHerbert Xu 	return 0;
1057c19e654dSHerbert Xu }
1058c19e654dSHerbert Xu 
1059a8b8a889SMatthias Schiffer static int ipgre_tap_validate(struct nlattr *tb[], struct nlattr *data[],
1060a8b8a889SMatthias Schiffer 			      struct netlink_ext_ack *extack)
1061e1a80002SHerbert Xu {
1062e1a80002SHerbert Xu 	__be32 daddr;
1063e1a80002SHerbert Xu 
1064e1a80002SHerbert Xu 	if (tb[IFLA_ADDRESS]) {
1065e1a80002SHerbert Xu 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1066e1a80002SHerbert Xu 			return -EINVAL;
1067e1a80002SHerbert Xu 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1068e1a80002SHerbert Xu 			return -EADDRNOTAVAIL;
1069e1a80002SHerbert Xu 	}
1070e1a80002SHerbert Xu 
1071e1a80002SHerbert Xu 	if (!data)
1072e1a80002SHerbert Xu 		goto out;
1073e1a80002SHerbert Xu 
1074e1a80002SHerbert Xu 	if (data[IFLA_GRE_REMOTE]) {
1075e1a80002SHerbert Xu 		memcpy(&daddr, nla_data(data[IFLA_GRE_REMOTE]), 4);
1076e1a80002SHerbert Xu 		if (!daddr)
1077e1a80002SHerbert Xu 			return -EINVAL;
1078e1a80002SHerbert Xu 	}
1079e1a80002SHerbert Xu 
1080e1a80002SHerbert Xu out:
1081a8b8a889SMatthias Schiffer 	return ipgre_tunnel_validate(tb, data, extack);
1082e1a80002SHerbert Xu }
1083e1a80002SHerbert Xu 
108484e54fe0SWilliam Tu static int erspan_validate(struct nlattr *tb[], struct nlattr *data[],
108584e54fe0SWilliam Tu 			   struct netlink_ext_ack *extack)
108684e54fe0SWilliam Tu {
108784e54fe0SWilliam Tu 	__be16 flags = 0;
108884e54fe0SWilliam Tu 	int ret;
108984e54fe0SWilliam Tu 
109084e54fe0SWilliam Tu 	if (!data)
109184e54fe0SWilliam Tu 		return 0;
109284e54fe0SWilliam Tu 
109384e54fe0SWilliam Tu 	ret = ipgre_tap_validate(tb, data, extack);
109484e54fe0SWilliam Tu 	if (ret)
109584e54fe0SWilliam Tu 		return ret;
109684e54fe0SWilliam Tu 
109751fa960dSWilliam Tu 	if (data[IFLA_GRE_ERSPAN_VER] &&
109851fa960dSWilliam Tu 	    nla_get_u8(data[IFLA_GRE_ERSPAN_VER]) == 0)
1099f989d546SWilliam Tu 		return 0;
1100f989d546SWilliam Tu 
1101f989d546SWilliam Tu 	/* ERSPAN type II/III should only have GRE sequence and key flag */
11021a66a836SWilliam Tu 	if (data[IFLA_GRE_OFLAGS])
110384e54fe0SWilliam Tu 		flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
11041a66a836SWilliam Tu 	if (data[IFLA_GRE_IFLAGS])
110584e54fe0SWilliam Tu 		flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
11061a66a836SWilliam Tu 	if (!data[IFLA_GRE_COLLECT_METADATA] &&
11071a66a836SWilliam Tu 	    flags != (GRE_SEQ | GRE_KEY))
110884e54fe0SWilliam Tu 		return -EINVAL;
110984e54fe0SWilliam Tu 
111084e54fe0SWilliam Tu 	/* ERSPAN Session ID only has 10-bit. Since we reuse
111184e54fe0SWilliam Tu 	 * 32-bit key field as ID, check it's range.
111284e54fe0SWilliam Tu 	 */
111384e54fe0SWilliam Tu 	if (data[IFLA_GRE_IKEY] &&
111484e54fe0SWilliam Tu 	    (ntohl(nla_get_be32(data[IFLA_GRE_IKEY])) & ~ID_MASK))
111584e54fe0SWilliam Tu 		return -EINVAL;
111684e54fe0SWilliam Tu 
111784e54fe0SWilliam Tu 	if (data[IFLA_GRE_OKEY] &&
111884e54fe0SWilliam Tu 	    (ntohl(nla_get_be32(data[IFLA_GRE_OKEY])) & ~ID_MASK))
111984e54fe0SWilliam Tu 		return -EINVAL;
112084e54fe0SWilliam Tu 
112184e54fe0SWilliam Tu 	return 0;
112284e54fe0SWilliam Tu }
112384e54fe0SWilliam Tu 
112422a59be8SPhilip Prindeville static int ipgre_netlink_parms(struct net_device *dev,
11252e15ea39SPravin B Shelar 				struct nlattr *data[],
11262e15ea39SPravin B Shelar 				struct nlattr *tb[],
11279830ad4cSCraig Gallek 				struct ip_tunnel_parm *parms,
11289830ad4cSCraig Gallek 				__u32 *fwmark)
1129c19e654dSHerbert Xu {
113022a59be8SPhilip Prindeville 	struct ip_tunnel *t = netdev_priv(dev);
113122a59be8SPhilip Prindeville 
11327bb82d92SHerbert Xu 	memset(parms, 0, sizeof(*parms));
1133c19e654dSHerbert Xu 
1134c19e654dSHerbert Xu 	parms->iph.protocol = IPPROTO_GRE;
1135c19e654dSHerbert Xu 
1136c19e654dSHerbert Xu 	if (!data)
113722a59be8SPhilip Prindeville 		return 0;
1138c19e654dSHerbert Xu 
1139c19e654dSHerbert Xu 	if (data[IFLA_GRE_LINK])
1140c19e654dSHerbert Xu 		parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
1141c19e654dSHerbert Xu 
1142c19e654dSHerbert Xu 	if (data[IFLA_GRE_IFLAGS])
1143c5441932SPravin B Shelar 		parms->i_flags = gre_flags_to_tnl_flags(nla_get_be16(data[IFLA_GRE_IFLAGS]));
1144c19e654dSHerbert Xu 
1145c19e654dSHerbert Xu 	if (data[IFLA_GRE_OFLAGS])
1146c5441932SPravin B Shelar 		parms->o_flags = gre_flags_to_tnl_flags(nla_get_be16(data[IFLA_GRE_OFLAGS]));
1147c19e654dSHerbert Xu 
1148c19e654dSHerbert Xu 	if (data[IFLA_GRE_IKEY])
1149c19e654dSHerbert Xu 		parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
1150c19e654dSHerbert Xu 
1151c19e654dSHerbert Xu 	if (data[IFLA_GRE_OKEY])
1152c19e654dSHerbert Xu 		parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
1153c19e654dSHerbert Xu 
1154c19e654dSHerbert Xu 	if (data[IFLA_GRE_LOCAL])
115567b61f6cSJiri Benc 		parms->iph.saddr = nla_get_in_addr(data[IFLA_GRE_LOCAL]);
1156c19e654dSHerbert Xu 
1157c19e654dSHerbert Xu 	if (data[IFLA_GRE_REMOTE])
115867b61f6cSJiri Benc 		parms->iph.daddr = nla_get_in_addr(data[IFLA_GRE_REMOTE]);
1159c19e654dSHerbert Xu 
1160c19e654dSHerbert Xu 	if (data[IFLA_GRE_TTL])
1161c19e654dSHerbert Xu 		parms->iph.ttl = nla_get_u8(data[IFLA_GRE_TTL]);
1162c19e654dSHerbert Xu 
1163c19e654dSHerbert Xu 	if (data[IFLA_GRE_TOS])
1164c19e654dSHerbert Xu 		parms->iph.tos = nla_get_u8(data[IFLA_GRE_TOS]);
1165c19e654dSHerbert Xu 
116622a59be8SPhilip Prindeville 	if (!data[IFLA_GRE_PMTUDISC] || nla_get_u8(data[IFLA_GRE_PMTUDISC])) {
116722a59be8SPhilip Prindeville 		if (t->ignore_df)
116822a59be8SPhilip Prindeville 			return -EINVAL;
1169c19e654dSHerbert Xu 		parms->iph.frag_off = htons(IP_DF);
117022a59be8SPhilip Prindeville 	}
11712e15ea39SPravin B Shelar 
11722e15ea39SPravin B Shelar 	if (data[IFLA_GRE_COLLECT_METADATA]) {
11732e15ea39SPravin B Shelar 		t->collect_md = true;
1174e271c7b4SJiri Benc 		if (dev->type == ARPHRD_IPGRE)
1175e271c7b4SJiri Benc 			dev->type = ARPHRD_NONE;
11762e15ea39SPravin B Shelar 	}
117722a59be8SPhilip Prindeville 
117822a59be8SPhilip Prindeville 	if (data[IFLA_GRE_IGNORE_DF]) {
117922a59be8SPhilip Prindeville 		if (nla_get_u8(data[IFLA_GRE_IGNORE_DF])
118022a59be8SPhilip Prindeville 		  && (parms->iph.frag_off & htons(IP_DF)))
118122a59be8SPhilip Prindeville 			return -EINVAL;
118222a59be8SPhilip Prindeville 		t->ignore_df = !!nla_get_u8(data[IFLA_GRE_IGNORE_DF]);
118322a59be8SPhilip Prindeville 	}
118422a59be8SPhilip Prindeville 
11859830ad4cSCraig Gallek 	if (data[IFLA_GRE_FWMARK])
11869830ad4cSCraig Gallek 		*fwmark = nla_get_u32(data[IFLA_GRE_FWMARK]);
11879830ad4cSCraig Gallek 
1188e1f8f78fSPetr Machata 	return 0;
1189e1f8f78fSPetr Machata }
1190e1f8f78fSPetr Machata 
1191e1f8f78fSPetr Machata static int erspan_netlink_parms(struct net_device *dev,
1192e1f8f78fSPetr Machata 				struct nlattr *data[],
1193e1f8f78fSPetr Machata 				struct nlattr *tb[],
1194e1f8f78fSPetr Machata 				struct ip_tunnel_parm *parms,
1195e1f8f78fSPetr Machata 				__u32 *fwmark)
1196e1f8f78fSPetr Machata {
1197e1f8f78fSPetr Machata 	struct ip_tunnel *t = netdev_priv(dev);
1198e1f8f78fSPetr Machata 	int err;
1199e1f8f78fSPetr Machata 
1200e1f8f78fSPetr Machata 	err = ipgre_netlink_parms(dev, data, tb, parms, fwmark);
1201e1f8f78fSPetr Machata 	if (err)
1202e1f8f78fSPetr Machata 		return err;
120332ca98feSPetr Machata 	if (!data)
120432ca98feSPetr Machata 		return 0;
1205e1f8f78fSPetr Machata 
1206f551c91dSWilliam Tu 	if (data[IFLA_GRE_ERSPAN_VER]) {
1207f551c91dSWilliam Tu 		t->erspan_ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]);
1208f551c91dSWilliam Tu 
1209f989d546SWilliam Tu 		if (t->erspan_ver > 2)
1210f551c91dSWilliam Tu 			return -EINVAL;
1211f551c91dSWilliam Tu 	}
1212f551c91dSWilliam Tu 
1213f551c91dSWilliam Tu 	if (t->erspan_ver == 1) {
121484e54fe0SWilliam Tu 		if (data[IFLA_GRE_ERSPAN_INDEX]) {
121584e54fe0SWilliam Tu 			t->index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
121684e54fe0SWilliam Tu 			if (t->index & ~INDEX_MASK)
121784e54fe0SWilliam Tu 				return -EINVAL;
121884e54fe0SWilliam Tu 		}
1219f551c91dSWilliam Tu 	} else if (t->erspan_ver == 2) {
1220f551c91dSWilliam Tu 		if (data[IFLA_GRE_ERSPAN_DIR]) {
1221f551c91dSWilliam Tu 			t->dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]);
1222f551c91dSWilliam Tu 			if (t->dir & ~(DIR_MASK >> DIR_OFFSET))
1223f551c91dSWilliam Tu 				return -EINVAL;
1224f551c91dSWilliam Tu 		}
1225f551c91dSWilliam Tu 		if (data[IFLA_GRE_ERSPAN_HWID]) {
1226f551c91dSWilliam Tu 			t->hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]);
1227f551c91dSWilliam Tu 			if (t->hwid & ~(HWID_MASK >> HWID_OFFSET))
1228f551c91dSWilliam Tu 				return -EINVAL;
1229f551c91dSWilliam Tu 		}
1230f551c91dSWilliam Tu 	}
123184e54fe0SWilliam Tu 
123222a59be8SPhilip Prindeville 	return 0;
1233c19e654dSHerbert Xu }
1234c19e654dSHerbert Xu 
12354565e991STom Herbert /* This function returns true when ENCAP attributes are present in the nl msg */
12364565e991STom Herbert static bool ipgre_netlink_encap_parms(struct nlattr *data[],
12374565e991STom Herbert 				      struct ip_tunnel_encap *ipencap)
12384565e991STom Herbert {
12394565e991STom Herbert 	bool ret = false;
12404565e991STom Herbert 
12414565e991STom Herbert 	memset(ipencap, 0, sizeof(*ipencap));
12424565e991STom Herbert 
12434565e991STom Herbert 	if (!data)
12444565e991STom Herbert 		return ret;
12454565e991STom Herbert 
12464565e991STom Herbert 	if (data[IFLA_GRE_ENCAP_TYPE]) {
12474565e991STom Herbert 		ret = true;
12484565e991STom Herbert 		ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]);
12494565e991STom Herbert 	}
12504565e991STom Herbert 
12514565e991STom Herbert 	if (data[IFLA_GRE_ENCAP_FLAGS]) {
12524565e991STom Herbert 		ret = true;
12534565e991STom Herbert 		ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]);
12544565e991STom Herbert 	}
12554565e991STom Herbert 
12564565e991STom Herbert 	if (data[IFLA_GRE_ENCAP_SPORT]) {
12574565e991STom Herbert 		ret = true;
12583e97fa70SSabrina Dubroca 		ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]);
12594565e991STom Herbert 	}
12604565e991STom Herbert 
12614565e991STom Herbert 	if (data[IFLA_GRE_ENCAP_DPORT]) {
12624565e991STom Herbert 		ret = true;
12633e97fa70SSabrina Dubroca 		ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]);
12644565e991STom Herbert 	}
12654565e991STom Herbert 
12664565e991STom Herbert 	return ret;
12674565e991STom Herbert }
12684565e991STom Herbert 
1269c5441932SPravin B Shelar static int gre_tap_init(struct net_device *dev)
1270e1a80002SHerbert Xu {
1271c5441932SPravin B Shelar 	__gre_tunnel_init(dev);
1272bec94d43Sstephen hemminger 	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1273d51711c0SXin Long 	netif_keep_dst(dev);
1274e1a80002SHerbert Xu 
1275c5441932SPravin B Shelar 	return ip_tunnel_init(dev);
1276e1a80002SHerbert Xu }
1277e1a80002SHerbert Xu 
1278c5441932SPravin B Shelar static const struct net_device_ops gre_tap_netdev_ops = {
1279c5441932SPravin B Shelar 	.ndo_init		= gre_tap_init,
1280c5441932SPravin B Shelar 	.ndo_uninit		= ip_tunnel_uninit,
1281c5441932SPravin B Shelar 	.ndo_start_xmit		= gre_tap_xmit,
1282b8c26a33SStephen Hemminger 	.ndo_set_mac_address 	= eth_mac_addr,
1283b8c26a33SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
1284c5441932SPravin B Shelar 	.ndo_change_mtu		= ip_tunnel_change_mtu,
128598d7fc46SHeiner Kallweit 	.ndo_get_stats64	= dev_get_tstats64,
12861e99584bSNicolas Dichtel 	.ndo_get_iflink		= ip_tunnel_get_iflink,
1287fc4099f1SPravin B Shelar 	.ndo_fill_metadata_dst	= gre_fill_metadata_dst,
1288b8c26a33SStephen Hemminger };
1289b8c26a33SStephen Hemminger 
129084e54fe0SWilliam Tu static int erspan_tunnel_init(struct net_device *dev)
129184e54fe0SWilliam Tu {
129284e54fe0SWilliam Tu 	struct ip_tunnel *tunnel = netdev_priv(dev);
129384e54fe0SWilliam Tu 
1294f989d546SWilliam Tu 	if (tunnel->erspan_ver == 0)
1295f989d546SWilliam Tu 		tunnel->tun_hlen = 4; /* 4-byte GRE hdr. */
1296f989d546SWilliam Tu 	else
1297f989d546SWilliam Tu 		tunnel->tun_hlen = 8; /* 8-byte GRE hdr. */
1298f989d546SWilliam Tu 
129984e54fe0SWilliam Tu 	tunnel->parms.iph.protocol = IPPROTO_GRE;
1300c122fda2SXin Long 	tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen +
1301f551c91dSWilliam Tu 		       erspan_hdr_len(tunnel->erspan_ver);
130284e54fe0SWilliam Tu 
130384e54fe0SWilliam Tu 	dev->features		|= GRE_FEATURES;
130484e54fe0SWilliam Tu 	dev->hw_features	|= GRE_FEATURES;
130584e54fe0SWilliam Tu 	dev->priv_flags		|= IFF_LIVE_ADDR_CHANGE;
1306c84bed44SXin Long 	netif_keep_dst(dev);
130784e54fe0SWilliam Tu 
130884e54fe0SWilliam Tu 	return ip_tunnel_init(dev);
130984e54fe0SWilliam Tu }
131084e54fe0SWilliam Tu 
131184e54fe0SWilliam Tu static const struct net_device_ops erspan_netdev_ops = {
131284e54fe0SWilliam Tu 	.ndo_init		= erspan_tunnel_init,
131384e54fe0SWilliam Tu 	.ndo_uninit		= ip_tunnel_uninit,
131484e54fe0SWilliam Tu 	.ndo_start_xmit		= erspan_xmit,
131584e54fe0SWilliam Tu 	.ndo_set_mac_address	= eth_mac_addr,
131684e54fe0SWilliam Tu 	.ndo_validate_addr	= eth_validate_addr,
131784e54fe0SWilliam Tu 	.ndo_change_mtu		= ip_tunnel_change_mtu,
131898d7fc46SHeiner Kallweit 	.ndo_get_stats64	= dev_get_tstats64,
131984e54fe0SWilliam Tu 	.ndo_get_iflink		= ip_tunnel_get_iflink,
132084e54fe0SWilliam Tu 	.ndo_fill_metadata_dst	= gre_fill_metadata_dst,
132184e54fe0SWilliam Tu };
132284e54fe0SWilliam Tu 
1323e1a80002SHerbert Xu static void ipgre_tap_setup(struct net_device *dev)
1324e1a80002SHerbert Xu {
1325e1a80002SHerbert Xu 	ether_setup(dev);
1326cfddd4c3SXin Long 	dev->max_mtu = 0;
1327c5441932SPravin B Shelar 	dev->netdev_ops	= &gre_tap_netdev_ops;
1328d13b161cSJiri Benc 	dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1329f8c1b7ceSstephen hemminger 	dev->priv_flags	|= IFF_LIVE_ADDR_CHANGE;
1330c5441932SPravin B Shelar 	ip_tunnel_setup(dev, gre_tap_net_id);
1331e1a80002SHerbert Xu }
1332e1a80002SHerbert Xu 
1333e1f8f78fSPetr Machata static int
1334e1f8f78fSPetr Machata ipgre_newlink_encap_setup(struct net_device *dev, struct nlattr *data[])
1335c19e654dSHerbert Xu {
13364565e991STom Herbert 	struct ip_tunnel_encap ipencap;
13374565e991STom Herbert 
13384565e991STom Herbert 	if (ipgre_netlink_encap_parms(data, &ipencap)) {
13394565e991STom Herbert 		struct ip_tunnel *t = netdev_priv(dev);
1340e1f8f78fSPetr Machata 		int err = ip_tunnel_encap_setup(t, &ipencap);
13414565e991STom Herbert 
13424565e991STom Herbert 		if (err < 0)
13434565e991STom Herbert 			return err;
13444565e991STom Herbert 	}
1345c19e654dSHerbert Xu 
1346e1f8f78fSPetr Machata 	return 0;
1347e1f8f78fSPetr Machata }
1348e1f8f78fSPetr Machata 
1349e1f8f78fSPetr Machata static int ipgre_newlink(struct net *src_net, struct net_device *dev,
1350e1f8f78fSPetr Machata 			 struct nlattr *tb[], struct nlattr *data[],
1351e1f8f78fSPetr Machata 			 struct netlink_ext_ack *extack)
1352e1f8f78fSPetr Machata {
1353e1f8f78fSPetr Machata 	struct ip_tunnel_parm p;
1354e1f8f78fSPetr Machata 	__u32 fwmark = 0;
1355e1f8f78fSPetr Machata 	int err;
1356e1f8f78fSPetr Machata 
1357e1f8f78fSPetr Machata 	err = ipgre_newlink_encap_setup(dev, data);
1358e1f8f78fSPetr Machata 	if (err)
1359e1f8f78fSPetr Machata 		return err;
1360e1f8f78fSPetr Machata 
13619830ad4cSCraig Gallek 	err = ipgre_netlink_parms(dev, data, tb, &p, &fwmark);
136222a59be8SPhilip Prindeville 	if (err < 0)
136322a59be8SPhilip Prindeville 		return err;
13649830ad4cSCraig Gallek 	return ip_tunnel_newlink(dev, tb, &p, fwmark);
1365c19e654dSHerbert Xu }
1366c19e654dSHerbert Xu 
1367e1f8f78fSPetr Machata static int erspan_newlink(struct net *src_net, struct net_device *dev,
1368e1f8f78fSPetr Machata 			  struct nlattr *tb[], struct nlattr *data[],
1369e1f8f78fSPetr Machata 			  struct netlink_ext_ack *extack)
1370e1f8f78fSPetr Machata {
1371e1f8f78fSPetr Machata 	struct ip_tunnel_parm p;
1372e1f8f78fSPetr Machata 	__u32 fwmark = 0;
1373e1f8f78fSPetr Machata 	int err;
1374e1f8f78fSPetr Machata 
1375e1f8f78fSPetr Machata 	err = ipgre_newlink_encap_setup(dev, data);
1376e1f8f78fSPetr Machata 	if (err)
1377e1f8f78fSPetr Machata 		return err;
1378e1f8f78fSPetr Machata 
1379e1f8f78fSPetr Machata 	err = erspan_netlink_parms(dev, data, tb, &p, &fwmark);
1380e1f8f78fSPetr Machata 	if (err)
1381e1f8f78fSPetr Machata 		return err;
1382e1f8f78fSPetr Machata 	return ip_tunnel_newlink(dev, tb, &p, fwmark);
1383e1f8f78fSPetr Machata }
1384e1f8f78fSPetr Machata 
1385c19e654dSHerbert Xu static int ipgre_changelink(struct net_device *dev, struct nlattr *tb[],
1386ad744b22SMatthias Schiffer 			    struct nlattr *data[],
1387ad744b22SMatthias Schiffer 			    struct netlink_ext_ack *extack)
1388c19e654dSHerbert Xu {
13899830ad4cSCraig Gallek 	struct ip_tunnel *t = netdev_priv(dev);
13909830ad4cSCraig Gallek 	__u32 fwmark = t->fwmark;
1391dd9d598cSXin Long 	struct ip_tunnel_parm p;
139222a59be8SPhilip Prindeville 	int err;
13934565e991STom Herbert 
1394e1f8f78fSPetr Machata 	err = ipgre_newlink_encap_setup(dev, data);
1395e1f8f78fSPetr Machata 	if (err)
13964565e991STom Herbert 		return err;
1397c19e654dSHerbert Xu 
13989830ad4cSCraig Gallek 	err = ipgre_netlink_parms(dev, data, tb, &p, &fwmark);
139922a59be8SPhilip Prindeville 	if (err < 0)
140022a59be8SPhilip Prindeville 		return err;
1401dd9d598cSXin Long 
1402dd9d598cSXin Long 	err = ip_tunnel_changelink(dev, tb, &p, fwmark);
1403dd9d598cSXin Long 	if (err < 0)
1404dd9d598cSXin Long 		return err;
1405dd9d598cSXin Long 
1406dd9d598cSXin Long 	t->parms.i_flags = p.i_flags;
1407dd9d598cSXin Long 	t->parms.o_flags = p.o_flags;
1408dd9d598cSXin Long 
1409dd9d598cSXin Long 	ipgre_link_update(dev, !tb[IFLA_MTU]);
1410dd9d598cSXin Long 
1411dd9d598cSXin Long 	return 0;
1412c19e654dSHerbert Xu }
1413c19e654dSHerbert Xu 
1414e1f8f78fSPetr Machata static int erspan_changelink(struct net_device *dev, struct nlattr *tb[],
1415e1f8f78fSPetr Machata 			     struct nlattr *data[],
1416e1f8f78fSPetr Machata 			     struct netlink_ext_ack *extack)
1417e1f8f78fSPetr Machata {
1418e1f8f78fSPetr Machata 	struct ip_tunnel *t = netdev_priv(dev);
1419e1f8f78fSPetr Machata 	__u32 fwmark = t->fwmark;
1420e1f8f78fSPetr Machata 	struct ip_tunnel_parm p;
1421e1f8f78fSPetr Machata 	int err;
1422e1f8f78fSPetr Machata 
1423e1f8f78fSPetr Machata 	err = ipgre_newlink_encap_setup(dev, data);
1424e1f8f78fSPetr Machata 	if (err)
1425e1f8f78fSPetr Machata 		return err;
1426e1f8f78fSPetr Machata 
1427e1f8f78fSPetr Machata 	err = erspan_netlink_parms(dev, data, tb, &p, &fwmark);
1428e1f8f78fSPetr Machata 	if (err < 0)
1429e1f8f78fSPetr Machata 		return err;
1430e1f8f78fSPetr Machata 
1431e1f8f78fSPetr Machata 	err = ip_tunnel_changelink(dev, tb, &p, fwmark);
1432e1f8f78fSPetr Machata 	if (err < 0)
1433e1f8f78fSPetr Machata 		return err;
1434e1f8f78fSPetr Machata 
1435e1f8f78fSPetr Machata 	t->parms.i_flags = p.i_flags;
1436e1f8f78fSPetr Machata 	t->parms.o_flags = p.o_flags;
1437e1f8f78fSPetr Machata 
1438e1f8f78fSPetr Machata 	return 0;
1439e1f8f78fSPetr Machata }
1440e1f8f78fSPetr Machata 
1441c19e654dSHerbert Xu static size_t ipgre_get_size(const struct net_device *dev)
1442c19e654dSHerbert Xu {
1443c19e654dSHerbert Xu 	return
1444c19e654dSHerbert Xu 		/* IFLA_GRE_LINK */
1445c19e654dSHerbert Xu 		nla_total_size(4) +
1446c19e654dSHerbert Xu 		/* IFLA_GRE_IFLAGS */
1447c19e654dSHerbert Xu 		nla_total_size(2) +
1448c19e654dSHerbert Xu 		/* IFLA_GRE_OFLAGS */
1449c19e654dSHerbert Xu 		nla_total_size(2) +
1450c19e654dSHerbert Xu 		/* IFLA_GRE_IKEY */
1451c19e654dSHerbert Xu 		nla_total_size(4) +
1452c19e654dSHerbert Xu 		/* IFLA_GRE_OKEY */
1453c19e654dSHerbert Xu 		nla_total_size(4) +
1454c19e654dSHerbert Xu 		/* IFLA_GRE_LOCAL */
1455c19e654dSHerbert Xu 		nla_total_size(4) +
1456c19e654dSHerbert Xu 		/* IFLA_GRE_REMOTE */
1457c19e654dSHerbert Xu 		nla_total_size(4) +
1458c19e654dSHerbert Xu 		/* IFLA_GRE_TTL */
1459c19e654dSHerbert Xu 		nla_total_size(1) +
1460c19e654dSHerbert Xu 		/* IFLA_GRE_TOS */
1461c19e654dSHerbert Xu 		nla_total_size(1) +
1462c19e654dSHerbert Xu 		/* IFLA_GRE_PMTUDISC */
1463c19e654dSHerbert Xu 		nla_total_size(1) +
14644565e991STom Herbert 		/* IFLA_GRE_ENCAP_TYPE */
14654565e991STom Herbert 		nla_total_size(2) +
14664565e991STom Herbert 		/* IFLA_GRE_ENCAP_FLAGS */
14674565e991STom Herbert 		nla_total_size(2) +
14684565e991STom Herbert 		/* IFLA_GRE_ENCAP_SPORT */
14694565e991STom Herbert 		nla_total_size(2) +
14704565e991STom Herbert 		/* IFLA_GRE_ENCAP_DPORT */
14714565e991STom Herbert 		nla_total_size(2) +
14722e15ea39SPravin B Shelar 		/* IFLA_GRE_COLLECT_METADATA */
14732e15ea39SPravin B Shelar 		nla_total_size(0) +
147422a59be8SPhilip Prindeville 		/* IFLA_GRE_IGNORE_DF */
147522a59be8SPhilip Prindeville 		nla_total_size(1) +
14769830ad4cSCraig Gallek 		/* IFLA_GRE_FWMARK */
14779830ad4cSCraig Gallek 		nla_total_size(4) +
147884e54fe0SWilliam Tu 		/* IFLA_GRE_ERSPAN_INDEX */
147984e54fe0SWilliam Tu 		nla_total_size(4) +
1480f551c91dSWilliam Tu 		/* IFLA_GRE_ERSPAN_VER */
1481f551c91dSWilliam Tu 		nla_total_size(1) +
1482f551c91dSWilliam Tu 		/* IFLA_GRE_ERSPAN_DIR */
1483f551c91dSWilliam Tu 		nla_total_size(1) +
1484f551c91dSWilliam Tu 		/* IFLA_GRE_ERSPAN_HWID */
1485f551c91dSWilliam Tu 		nla_total_size(2) +
1486c19e654dSHerbert Xu 		0;
1487c19e654dSHerbert Xu }
1488c19e654dSHerbert Xu 
1489c19e654dSHerbert Xu static int ipgre_fill_info(struct sk_buff *skb, const struct net_device *dev)
1490c19e654dSHerbert Xu {
1491c19e654dSHerbert Xu 	struct ip_tunnel *t = netdev_priv(dev);
1492c19e654dSHerbert Xu 	struct ip_tunnel_parm *p = &t->parms;
1493feaf5c79SLorenzo Bianconi 	__be16 o_flags = p->o_flags;
1494feaf5c79SLorenzo Bianconi 
1495f989d546SWilliam Tu 	if (t->erspan_ver <= 2) {
1496f989d546SWilliam Tu 		if (t->erspan_ver != 0 && !t->collect_md)
1497feaf5c79SLorenzo Bianconi 			o_flags |= TUNNEL_KEY;
1498c19e654dSHerbert Xu 
14992bdf700eSLorenzo Bianconi 		if (nla_put_u8(skb, IFLA_GRE_ERSPAN_VER, t->erspan_ver))
15002bdf700eSLorenzo Bianconi 			goto nla_put_failure;
15012bdf700eSLorenzo Bianconi 
15022bdf700eSLorenzo Bianconi 		if (t->erspan_ver == 1) {
15032bdf700eSLorenzo Bianconi 			if (nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, t->index))
15042bdf700eSLorenzo Bianconi 				goto nla_put_failure;
1505f989d546SWilliam Tu 		} else if (t->erspan_ver == 2) {
15062bdf700eSLorenzo Bianconi 			if (nla_put_u8(skb, IFLA_GRE_ERSPAN_DIR, t->dir))
15072bdf700eSLorenzo Bianconi 				goto nla_put_failure;
15082bdf700eSLorenzo Bianconi 			if (nla_put_u16(skb, IFLA_GRE_ERSPAN_HWID, t->hwid))
15092bdf700eSLorenzo Bianconi 				goto nla_put_failure;
15102bdf700eSLorenzo Bianconi 		}
15112bdf700eSLorenzo Bianconi 	}
15122bdf700eSLorenzo Bianconi 
1513f3756b79SDavid S. Miller 	if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
151495f5c64cSTom Herbert 	    nla_put_be16(skb, IFLA_GRE_IFLAGS,
151595f5c64cSTom Herbert 			 gre_tnl_flags_to_gre_flags(p->i_flags)) ||
151695f5c64cSTom Herbert 	    nla_put_be16(skb, IFLA_GRE_OFLAGS,
1517feaf5c79SLorenzo Bianconi 			 gre_tnl_flags_to_gre_flags(o_flags)) ||
1518f3756b79SDavid S. Miller 	    nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
1519f3756b79SDavid S. Miller 	    nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
1520930345eaSJiri Benc 	    nla_put_in_addr(skb, IFLA_GRE_LOCAL, p->iph.saddr) ||
1521930345eaSJiri Benc 	    nla_put_in_addr(skb, IFLA_GRE_REMOTE, p->iph.daddr) ||
1522f3756b79SDavid S. Miller 	    nla_put_u8(skb, IFLA_GRE_TTL, p->iph.ttl) ||
1523f3756b79SDavid S. Miller 	    nla_put_u8(skb, IFLA_GRE_TOS, p->iph.tos) ||
1524f3756b79SDavid S. Miller 	    nla_put_u8(skb, IFLA_GRE_PMTUDISC,
15259830ad4cSCraig Gallek 		       !!(p->iph.frag_off & htons(IP_DF))) ||
15269830ad4cSCraig Gallek 	    nla_put_u32(skb, IFLA_GRE_FWMARK, t->fwmark))
1527f3756b79SDavid S. Miller 		goto nla_put_failure;
15284565e991STom Herbert 
15294565e991STom Herbert 	if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE,
15304565e991STom Herbert 			t->encap.type) ||
15313e97fa70SSabrina Dubroca 	    nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT,
15324565e991STom Herbert 			 t->encap.sport) ||
15333e97fa70SSabrina Dubroca 	    nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT,
15344565e991STom Herbert 			 t->encap.dport) ||
15354565e991STom Herbert 	    nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS,
1536e1b2cb65STom Herbert 			t->encap.flags))
15374565e991STom Herbert 		goto nla_put_failure;
15384565e991STom Herbert 
153922a59be8SPhilip Prindeville 	if (nla_put_u8(skb, IFLA_GRE_IGNORE_DF, t->ignore_df))
154022a59be8SPhilip Prindeville 		goto nla_put_failure;
154122a59be8SPhilip Prindeville 
15422e15ea39SPravin B Shelar 	if (t->collect_md) {
15432e15ea39SPravin B Shelar 		if (nla_put_flag(skb, IFLA_GRE_COLLECT_METADATA))
15442e15ea39SPravin B Shelar 			goto nla_put_failure;
15452e15ea39SPravin B Shelar 	}
15462e15ea39SPravin B Shelar 
1547c19e654dSHerbert Xu 	return 0;
1548c19e654dSHerbert Xu 
1549c19e654dSHerbert Xu nla_put_failure:
1550c19e654dSHerbert Xu 	return -EMSGSIZE;
1551c19e654dSHerbert Xu }
1552c19e654dSHerbert Xu 
155384e54fe0SWilliam Tu static void erspan_setup(struct net_device *dev)
155484e54fe0SWilliam Tu {
155584581bdaSXin Long 	struct ip_tunnel *t = netdev_priv(dev);
155684581bdaSXin Long 
155784e54fe0SWilliam Tu 	ether_setup(dev);
15580e141f75SHaishuang Yan 	dev->max_mtu = 0;
155984e54fe0SWilliam Tu 	dev->netdev_ops = &erspan_netdev_ops;
156084e54fe0SWilliam Tu 	dev->priv_flags &= ~IFF_TX_SKB_SHARING;
156184e54fe0SWilliam Tu 	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
156284e54fe0SWilliam Tu 	ip_tunnel_setup(dev, erspan_net_id);
156384581bdaSXin Long 	t->erspan_ver = 1;
156484e54fe0SWilliam Tu }
156584e54fe0SWilliam Tu 
1566c19e654dSHerbert Xu static const struct nla_policy ipgre_policy[IFLA_GRE_MAX + 1] = {
1567c19e654dSHerbert Xu 	[IFLA_GRE_LINK]		= { .type = NLA_U32 },
1568c19e654dSHerbert Xu 	[IFLA_GRE_IFLAGS]	= { .type = NLA_U16 },
1569c19e654dSHerbert Xu 	[IFLA_GRE_OFLAGS]	= { .type = NLA_U16 },
1570c19e654dSHerbert Xu 	[IFLA_GRE_IKEY]		= { .type = NLA_U32 },
1571c19e654dSHerbert Xu 	[IFLA_GRE_OKEY]		= { .type = NLA_U32 },
1572c593642cSPankaj Bharadiya 	[IFLA_GRE_LOCAL]	= { .len = sizeof_field(struct iphdr, saddr) },
1573c593642cSPankaj Bharadiya 	[IFLA_GRE_REMOTE]	= { .len = sizeof_field(struct iphdr, daddr) },
1574c19e654dSHerbert Xu 	[IFLA_GRE_TTL]		= { .type = NLA_U8 },
1575c19e654dSHerbert Xu 	[IFLA_GRE_TOS]		= { .type = NLA_U8 },
1576c19e654dSHerbert Xu 	[IFLA_GRE_PMTUDISC]	= { .type = NLA_U8 },
15774565e991STom Herbert 	[IFLA_GRE_ENCAP_TYPE]	= { .type = NLA_U16 },
15784565e991STom Herbert 	[IFLA_GRE_ENCAP_FLAGS]	= { .type = NLA_U16 },
15794565e991STom Herbert 	[IFLA_GRE_ENCAP_SPORT]	= { .type = NLA_U16 },
15804565e991STom Herbert 	[IFLA_GRE_ENCAP_DPORT]	= { .type = NLA_U16 },
15812e15ea39SPravin B Shelar 	[IFLA_GRE_COLLECT_METADATA]	= { .type = NLA_FLAG },
158222a59be8SPhilip Prindeville 	[IFLA_GRE_IGNORE_DF]	= { .type = NLA_U8 },
15839830ad4cSCraig Gallek 	[IFLA_GRE_FWMARK]	= { .type = NLA_U32 },
158484e54fe0SWilliam Tu 	[IFLA_GRE_ERSPAN_INDEX]	= { .type = NLA_U32 },
1585f551c91dSWilliam Tu 	[IFLA_GRE_ERSPAN_VER]	= { .type = NLA_U8 },
1586f551c91dSWilliam Tu 	[IFLA_GRE_ERSPAN_DIR]	= { .type = NLA_U8 },
1587f551c91dSWilliam Tu 	[IFLA_GRE_ERSPAN_HWID]	= { .type = NLA_U16 },
1588c19e654dSHerbert Xu };
1589c19e654dSHerbert Xu 
1590c19e654dSHerbert Xu static struct rtnl_link_ops ipgre_link_ops __read_mostly = {
1591c19e654dSHerbert Xu 	.kind		= "gre",
1592c19e654dSHerbert Xu 	.maxtype	= IFLA_GRE_MAX,
1593c19e654dSHerbert Xu 	.policy		= ipgre_policy,
1594c19e654dSHerbert Xu 	.priv_size	= sizeof(struct ip_tunnel),
1595c19e654dSHerbert Xu 	.setup		= ipgre_tunnel_setup,
1596c19e654dSHerbert Xu 	.validate	= ipgre_tunnel_validate,
1597c19e654dSHerbert Xu 	.newlink	= ipgre_newlink,
1598c19e654dSHerbert Xu 	.changelink	= ipgre_changelink,
1599c5441932SPravin B Shelar 	.dellink	= ip_tunnel_dellink,
1600c19e654dSHerbert Xu 	.get_size	= ipgre_get_size,
1601c19e654dSHerbert Xu 	.fill_info	= ipgre_fill_info,
16021728d4faSNicolas Dichtel 	.get_link_net	= ip_tunnel_get_link_net,
1603c19e654dSHerbert Xu };
1604c19e654dSHerbert Xu 
1605e1a80002SHerbert Xu static struct rtnl_link_ops ipgre_tap_ops __read_mostly = {
1606e1a80002SHerbert Xu 	.kind		= "gretap",
1607e1a80002SHerbert Xu 	.maxtype	= IFLA_GRE_MAX,
1608e1a80002SHerbert Xu 	.policy		= ipgre_policy,
1609e1a80002SHerbert Xu 	.priv_size	= sizeof(struct ip_tunnel),
1610e1a80002SHerbert Xu 	.setup		= ipgre_tap_setup,
1611e1a80002SHerbert Xu 	.validate	= ipgre_tap_validate,
1612e1a80002SHerbert Xu 	.newlink	= ipgre_newlink,
1613e1a80002SHerbert Xu 	.changelink	= ipgre_changelink,
1614c5441932SPravin B Shelar 	.dellink	= ip_tunnel_dellink,
1615e1a80002SHerbert Xu 	.get_size	= ipgre_get_size,
1616e1a80002SHerbert Xu 	.fill_info	= ipgre_fill_info,
16171728d4faSNicolas Dichtel 	.get_link_net	= ip_tunnel_get_link_net,
1618e1a80002SHerbert Xu };
1619e1a80002SHerbert Xu 
162084e54fe0SWilliam Tu static struct rtnl_link_ops erspan_link_ops __read_mostly = {
162184e54fe0SWilliam Tu 	.kind		= "erspan",
162284e54fe0SWilliam Tu 	.maxtype	= IFLA_GRE_MAX,
162384e54fe0SWilliam Tu 	.policy		= ipgre_policy,
162484e54fe0SWilliam Tu 	.priv_size	= sizeof(struct ip_tunnel),
162584e54fe0SWilliam Tu 	.setup		= erspan_setup,
162684e54fe0SWilliam Tu 	.validate	= erspan_validate,
1627e1f8f78fSPetr Machata 	.newlink	= erspan_newlink,
1628e1f8f78fSPetr Machata 	.changelink	= erspan_changelink,
162984e54fe0SWilliam Tu 	.dellink	= ip_tunnel_dellink,
163084e54fe0SWilliam Tu 	.get_size	= ipgre_get_size,
163184e54fe0SWilliam Tu 	.fill_info	= ipgre_fill_info,
163284e54fe0SWilliam Tu 	.get_link_net	= ip_tunnel_get_link_net,
163384e54fe0SWilliam Tu };
163484e54fe0SWilliam Tu 
1635b2acd1dcSPravin B Shelar struct net_device *gretap_fb_dev_create(struct net *net, const char *name,
1636b2acd1dcSPravin B Shelar 					u8 name_assign_type)
1637b2acd1dcSPravin B Shelar {
1638b2acd1dcSPravin B Shelar 	struct nlattr *tb[IFLA_MAX + 1];
1639b2acd1dcSPravin B Shelar 	struct net_device *dev;
1640106da663SNicolas Dichtel 	LIST_HEAD(list_kill);
1641b2acd1dcSPravin B Shelar 	struct ip_tunnel *t;
1642b2acd1dcSPravin B Shelar 	int err;
1643b2acd1dcSPravin B Shelar 
1644b2acd1dcSPravin B Shelar 	memset(&tb, 0, sizeof(tb));
1645b2acd1dcSPravin B Shelar 
1646b2acd1dcSPravin B Shelar 	dev = rtnl_create_link(net, name, name_assign_type,
1647d0522f1cSDavid Ahern 			       &ipgre_tap_ops, tb, NULL);
1648b2acd1dcSPravin B Shelar 	if (IS_ERR(dev))
1649b2acd1dcSPravin B Shelar 		return dev;
1650b2acd1dcSPravin B Shelar 
1651b2acd1dcSPravin B Shelar 	/* Configure flow based GRE device. */
1652b2acd1dcSPravin B Shelar 	t = netdev_priv(dev);
1653b2acd1dcSPravin B Shelar 	t->collect_md = true;
1654b2acd1dcSPravin B Shelar 
16557a3f4a18SMatthias Schiffer 	err = ipgre_newlink(net, dev, tb, NULL, NULL);
1656106da663SNicolas Dichtel 	if (err < 0) {
1657106da663SNicolas Dichtel 		free_netdev(dev);
1658106da663SNicolas Dichtel 		return ERR_PTR(err);
1659106da663SNicolas Dichtel 	}
16607e059158SDavid Wragg 
16617e059158SDavid Wragg 	/* openvswitch users expect packet sizes to be unrestricted,
16627e059158SDavid Wragg 	 * so set the largest MTU we can.
16637e059158SDavid Wragg 	 */
16647e059158SDavid Wragg 	err = __ip_tunnel_change_mtu(dev, IP_MAX_MTU, false);
16657e059158SDavid Wragg 	if (err)
16667e059158SDavid Wragg 		goto out;
16677e059158SDavid Wragg 
1668da6f1da8SNicolas Dichtel 	err = rtnl_configure_link(dev, NULL);
1669da6f1da8SNicolas Dichtel 	if (err < 0)
1670da6f1da8SNicolas Dichtel 		goto out;
1671da6f1da8SNicolas Dichtel 
1672b2acd1dcSPravin B Shelar 	return dev;
1673b2acd1dcSPravin B Shelar out:
1674106da663SNicolas Dichtel 	ip_tunnel_dellink(dev, &list_kill);
1675106da663SNicolas Dichtel 	unregister_netdevice_many(&list_kill);
1676b2acd1dcSPravin B Shelar 	return ERR_PTR(err);
1677b2acd1dcSPravin B Shelar }
1678b2acd1dcSPravin B Shelar EXPORT_SYMBOL_GPL(gretap_fb_dev_create);
1679b2acd1dcSPravin B Shelar 
1680c5441932SPravin B Shelar static int __net_init ipgre_tap_init_net(struct net *net)
1681c5441932SPravin B Shelar {
16822e15ea39SPravin B Shelar 	return ip_tunnel_init_net(net, gre_tap_net_id, &ipgre_tap_ops, "gretap0");
1683c5441932SPravin B Shelar }
1684c5441932SPravin B Shelar 
168564bc1781SEric Dumazet static void __net_exit ipgre_tap_exit_batch_net(struct list_head *list_net)
1686c5441932SPravin B Shelar {
168764bc1781SEric Dumazet 	ip_tunnel_delete_nets(list_net, gre_tap_net_id, &ipgre_tap_ops);
1688c5441932SPravin B Shelar }
1689c5441932SPravin B Shelar 
1690c5441932SPravin B Shelar static struct pernet_operations ipgre_tap_net_ops = {
1691c5441932SPravin B Shelar 	.init = ipgre_tap_init_net,
169264bc1781SEric Dumazet 	.exit_batch = ipgre_tap_exit_batch_net,
1693c5441932SPravin B Shelar 	.id   = &gre_tap_net_id,
1694c5441932SPravin B Shelar 	.size = sizeof(struct ip_tunnel_net),
1695c5441932SPravin B Shelar };
16961da177e4SLinus Torvalds 
169784e54fe0SWilliam Tu static int __net_init erspan_init_net(struct net *net)
169884e54fe0SWilliam Tu {
169984e54fe0SWilliam Tu 	return ip_tunnel_init_net(net, erspan_net_id,
170084e54fe0SWilliam Tu 				  &erspan_link_ops, "erspan0");
170184e54fe0SWilliam Tu }
170284e54fe0SWilliam Tu 
170364bc1781SEric Dumazet static void __net_exit erspan_exit_batch_net(struct list_head *net_list)
170484e54fe0SWilliam Tu {
170564bc1781SEric Dumazet 	ip_tunnel_delete_nets(net_list, erspan_net_id, &erspan_link_ops);
170684e54fe0SWilliam Tu }
170784e54fe0SWilliam Tu 
170884e54fe0SWilliam Tu static struct pernet_operations erspan_net_ops = {
170984e54fe0SWilliam Tu 	.init = erspan_init_net,
171064bc1781SEric Dumazet 	.exit_batch = erspan_exit_batch_net,
171184e54fe0SWilliam Tu 	.id   = &erspan_net_id,
171284e54fe0SWilliam Tu 	.size = sizeof(struct ip_tunnel_net),
171384e54fe0SWilliam Tu };
171484e54fe0SWilliam Tu 
17151da177e4SLinus Torvalds static int __init ipgre_init(void)
17161da177e4SLinus Torvalds {
17171da177e4SLinus Torvalds 	int err;
17181da177e4SLinus Torvalds 
1719058bd4d2SJoe Perches 	pr_info("GRE over IPv4 tunneling driver\n");
17201da177e4SLinus Torvalds 
1721cfb8fbf2SEric W. Biederman 	err = register_pernet_device(&ipgre_net_ops);
172259a4c759SPavel Emelyanov 	if (err < 0)
1723c2892f02SAlexey Dobriyan 		return err;
1724c2892f02SAlexey Dobriyan 
1725c5441932SPravin B Shelar 	err = register_pernet_device(&ipgre_tap_net_ops);
1726c5441932SPravin B Shelar 	if (err < 0)
1727e3d0328cSWilliam Tu 		goto pnet_tap_failed;
1728c5441932SPravin B Shelar 
172984e54fe0SWilliam Tu 	err = register_pernet_device(&erspan_net_ops);
173084e54fe0SWilliam Tu 	if (err < 0)
173184e54fe0SWilliam Tu 		goto pnet_erspan_failed;
173284e54fe0SWilliam Tu 
17339f57c67cSPravin B Shelar 	err = gre_add_protocol(&ipgre_protocol, GREPROTO_CISCO);
1734c2892f02SAlexey Dobriyan 	if (err < 0) {
1735058bd4d2SJoe Perches 		pr_info("%s: can't add protocol\n", __func__);
1736c2892f02SAlexey Dobriyan 		goto add_proto_failed;
1737c2892f02SAlexey Dobriyan 	}
17387daa0004SPavel Emelyanov 
1739c19e654dSHerbert Xu 	err = rtnl_link_register(&ipgre_link_ops);
1740c19e654dSHerbert Xu 	if (err < 0)
1741c19e654dSHerbert Xu 		goto rtnl_link_failed;
1742c19e654dSHerbert Xu 
1743e1a80002SHerbert Xu 	err = rtnl_link_register(&ipgre_tap_ops);
1744e1a80002SHerbert Xu 	if (err < 0)
1745e1a80002SHerbert Xu 		goto tap_ops_failed;
1746e1a80002SHerbert Xu 
174784e54fe0SWilliam Tu 	err = rtnl_link_register(&erspan_link_ops);
174884e54fe0SWilliam Tu 	if (err < 0)
174984e54fe0SWilliam Tu 		goto erspan_link_failed;
175084e54fe0SWilliam Tu 
1751c5441932SPravin B Shelar 	return 0;
1752c19e654dSHerbert Xu 
175384e54fe0SWilliam Tu erspan_link_failed:
175484e54fe0SWilliam Tu 	rtnl_link_unregister(&ipgre_tap_ops);
1755e1a80002SHerbert Xu tap_ops_failed:
1756e1a80002SHerbert Xu 	rtnl_link_unregister(&ipgre_link_ops);
1757c19e654dSHerbert Xu rtnl_link_failed:
17589f57c67cSPravin B Shelar 	gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO);
1759c2892f02SAlexey Dobriyan add_proto_failed:
176084e54fe0SWilliam Tu 	unregister_pernet_device(&erspan_net_ops);
176184e54fe0SWilliam Tu pnet_erspan_failed:
1762c5441932SPravin B Shelar 	unregister_pernet_device(&ipgre_tap_net_ops);
1763e3d0328cSWilliam Tu pnet_tap_failed:
1764c2892f02SAlexey Dobriyan 	unregister_pernet_device(&ipgre_net_ops);
1765c5441932SPravin B Shelar 	return err;
17661da177e4SLinus Torvalds }
17671da177e4SLinus Torvalds 
1768db44575fSAlexey Kuznetsov static void __exit ipgre_fini(void)
17691da177e4SLinus Torvalds {
1770e1a80002SHerbert Xu 	rtnl_link_unregister(&ipgre_tap_ops);
1771c19e654dSHerbert Xu 	rtnl_link_unregister(&ipgre_link_ops);
177284e54fe0SWilliam Tu 	rtnl_link_unregister(&erspan_link_ops);
17739f57c67cSPravin B Shelar 	gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO);
1774c5441932SPravin B Shelar 	unregister_pernet_device(&ipgre_tap_net_ops);
1775c2892f02SAlexey Dobriyan 	unregister_pernet_device(&ipgre_net_ops);
177684e54fe0SWilliam Tu 	unregister_pernet_device(&erspan_net_ops);
17771da177e4SLinus Torvalds }
17781da177e4SLinus Torvalds 
17791da177e4SLinus Torvalds module_init(ipgre_init);
17801da177e4SLinus Torvalds module_exit(ipgre_fini);
17811da177e4SLinus Torvalds MODULE_LICENSE("GPL");
17824d74f8baSPatrick McHardy MODULE_ALIAS_RTNL_LINK("gre");
17834d74f8baSPatrick McHardy MODULE_ALIAS_RTNL_LINK("gretap");
178484e54fe0SWilliam Tu MODULE_ALIAS_RTNL_LINK("erspan");
17858909c9adSVasiliy Kulikov MODULE_ALIAS_NETDEV("gre0");
1786c5441932SPravin B Shelar MODULE_ALIAS_NETDEV("gretap0");
178784e54fe0SWilliam Tu MODULE_ALIAS_NETDEV("erspan0");
1788