11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Linux NET3: GRE over IP protocol decoder. 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Authors: Alexey Kuznetsov (kuznet@ms2.inr.ac.ru) 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 71da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 81da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 91da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds */ 121da177e4SLinus Torvalds 13afd46503SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 14afd46503SJoe Perches 154fc268d2SRandy Dunlap #include <linux/capability.h> 161da177e4SLinus Torvalds #include <linux/module.h> 171da177e4SLinus Torvalds #include <linux/types.h> 181da177e4SLinus Torvalds #include <linux/kernel.h> 195a0e3ad6STejun Heo #include <linux/slab.h> 207c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 211da177e4SLinus Torvalds #include <linux/skbuff.h> 221da177e4SLinus Torvalds #include <linux/netdevice.h> 231da177e4SLinus Torvalds #include <linux/in.h> 241da177e4SLinus Torvalds #include <linux/tcp.h> 251da177e4SLinus Torvalds #include <linux/udp.h> 261da177e4SLinus Torvalds #include <linux/if_arp.h> 272e15ea39SPravin B Shelar #include <linux/if_vlan.h> 281da177e4SLinus Torvalds #include <linux/init.h> 291da177e4SLinus Torvalds #include <linux/in6.h> 301da177e4SLinus Torvalds #include <linux/inetdevice.h> 311da177e4SLinus Torvalds #include <linux/igmp.h> 321da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 33e1a80002SHerbert Xu #include <linux/etherdevice.h> 3446f25dffSKris Katterjohn #include <linux/if_ether.h> 351da177e4SLinus Torvalds 361da177e4SLinus Torvalds #include <net/sock.h> 371da177e4SLinus Torvalds #include <net/ip.h> 381da177e4SLinus Torvalds #include <net/icmp.h> 391da177e4SLinus Torvalds #include <net/protocol.h> 40c5441932SPravin B Shelar #include <net/ip_tunnels.h> 411da177e4SLinus Torvalds #include <net/arp.h> 421da177e4SLinus Torvalds #include <net/checksum.h> 431da177e4SLinus Torvalds #include <net/dsfield.h> 441da177e4SLinus Torvalds #include <net/inet_ecn.h> 451da177e4SLinus Torvalds #include <net/xfrm.h> 4659a4c759SPavel Emelyanov #include <net/net_namespace.h> 4759a4c759SPavel Emelyanov #include <net/netns/generic.h> 48c19e654dSHerbert Xu #include <net/rtnetlink.h> 4900959adeSDmitry Kozlov #include <net/gre.h> 502e15ea39SPravin B Shelar #include <net/dst_metadata.h> 5184e54fe0SWilliam Tu #include <net/erspan.h> 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds /* 541da177e4SLinus Torvalds Problems & solutions 551da177e4SLinus Torvalds -------------------- 561da177e4SLinus Torvalds 571da177e4SLinus Torvalds 1. The most important issue is detecting local dead loops. 581da177e4SLinus Torvalds They would cause complete host lockup in transmit, which 591da177e4SLinus Torvalds would be "resolved" by stack overflow or, if queueing is enabled, 601da177e4SLinus Torvalds with infinite looping in net_bh. 611da177e4SLinus Torvalds 621da177e4SLinus Torvalds We cannot track such dead loops during route installation, 631da177e4SLinus Torvalds it is infeasible task. The most general solutions would be 641da177e4SLinus Torvalds to keep skb->encapsulation counter (sort of local ttl), 656d0722a2SEric Dumazet and silently drop packet when it expires. It is a good 66bff52857Sstephen hemminger solution, but it supposes maintaining new variable in ALL 671da177e4SLinus Torvalds skb, even if no tunneling is used. 681da177e4SLinus Torvalds 696d0722a2SEric Dumazet Current solution: xmit_recursion breaks dead loops. This is a percpu 706d0722a2SEric Dumazet counter, since when we enter the first ndo_xmit(), cpu migration is 716d0722a2SEric Dumazet forbidden. We force an exit if this counter reaches RECURSION_LIMIT 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds 2. Networking dead loops would not kill routers, but would really 741da177e4SLinus Torvalds kill network. IP hop limit plays role of "t->recursion" in this case, 751da177e4SLinus Torvalds if we copy it from packet being encapsulated to upper header. 761da177e4SLinus Torvalds It is very good solution, but it introduces two problems: 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds - Routing protocols, using packets with ttl=1 (OSPF, RIP2), 791da177e4SLinus Torvalds do not work over tunnels. 801da177e4SLinus Torvalds - traceroute does not work. I planned to relay ICMP from tunnel, 811da177e4SLinus Torvalds so that this problem would be solved and traceroute output 821da177e4SLinus Torvalds would even more informative. This idea appeared to be wrong: 831da177e4SLinus Torvalds only Linux complies to rfc1812 now (yes, guys, Linux is the only 841da177e4SLinus Torvalds true router now :-)), all routers (at least, in neighbourhood of mine) 851da177e4SLinus Torvalds return only 8 bytes of payload. It is the end. 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds Hence, if we want that OSPF worked or traceroute said something reasonable, 881da177e4SLinus Torvalds we should search for another solution. 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds One of them is to parse packet trying to detect inner encapsulation 911da177e4SLinus Torvalds made by our node. It is difficult or even impossible, especially, 92bff52857Sstephen hemminger taking into account fragmentation. TO be short, ttl is not solution at all. 931da177e4SLinus Torvalds 941da177e4SLinus Torvalds Current solution: The solution was UNEXPECTEDLY SIMPLE. 951da177e4SLinus Torvalds We force DF flag on tunnels with preconfigured hop limit, 961da177e4SLinus Torvalds that is ALL. :-) Well, it does not remove the problem completely, 971da177e4SLinus Torvalds but exponential growth of network traffic is changed to linear 981da177e4SLinus Torvalds (branches, that exceed pmtu are pruned) and tunnel mtu 99bff52857Sstephen hemminger rapidly degrades to value <68, where looping stops. 1001da177e4SLinus Torvalds Yes, it is not good if there exists a router in the loop, 1011da177e4SLinus Torvalds which does not force DF, even when encapsulating packets have DF set. 1021da177e4SLinus Torvalds But it is not our problem! Nobody could accuse us, we made 1031da177e4SLinus Torvalds all that we could make. Even if it is your gated who injected 1041da177e4SLinus Torvalds fatal route to network, even if it were you who configured 1051da177e4SLinus Torvalds fatal static route: you are innocent. :-) 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds Alexey Kuznetsov. 1081da177e4SLinus Torvalds */ 1091da177e4SLinus Torvalds 110eccc1bb8Sstephen hemminger static bool log_ecn_error = true; 111eccc1bb8Sstephen hemminger module_param(log_ecn_error, bool, 0644); 112eccc1bb8Sstephen hemminger MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN"); 113eccc1bb8Sstephen hemminger 114c19e654dSHerbert Xu static struct rtnl_link_ops ipgre_link_ops __read_mostly; 1151da177e4SLinus Torvalds static int ipgre_tunnel_init(struct net_device *dev); 1161a66a836SWilliam Tu static void erspan_build_header(struct sk_buff *skb, 117c69de58bSWilliam Tu u32 id, u32 index, 118a3222dc9SWilliam Tu bool truncate, bool is_ipv4); 119eb8ce741SPavel Emelyanov 120c7d03a00SAlexey Dobriyan static unsigned int ipgre_net_id __read_mostly; 121c7d03a00SAlexey Dobriyan static unsigned int gre_tap_net_id __read_mostly; 12284e54fe0SWilliam Tu static unsigned int erspan_net_id __read_mostly; 123eb8ce741SPavel Emelyanov 1249f57c67cSPravin B Shelar static void ipgre_err(struct sk_buff *skb, u32 info, 125bda7bb46SPravin B Shelar const struct tnl_ptk_info *tpi) 1261da177e4SLinus Torvalds { 1271da177e4SLinus Torvalds 128071f92d0SRami Rosen /* All the routers (except for Linux) return only 1291da177e4SLinus Torvalds 8 bytes of packet payload. It means, that precise relaying of 1301da177e4SLinus Torvalds ICMP in the real Internet is absolutely infeasible. 1311da177e4SLinus Torvalds 1321da177e4SLinus Torvalds Moreover, Cisco "wise men" put GRE key to the third word 133c5441932SPravin B Shelar in GRE header. It makes impossible maintaining even soft 134c5441932SPravin B Shelar state for keyed GRE tunnels with enabled checksum. Tell 135c5441932SPravin B Shelar them "thank you". 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds Well, I wonder, rfc1812 was written by Cisco employee, 138bff52857Sstephen hemminger what the hell these idiots break standards established 139bff52857Sstephen hemminger by themselves??? 1401da177e4SLinus Torvalds */ 141c5441932SPravin B Shelar struct net *net = dev_net(skb->dev); 142c5441932SPravin B Shelar struct ip_tunnel_net *itn; 14396f5a846SEric Dumazet const struct iphdr *iph; 14488c7664fSArnaldo Carvalho de Melo const int type = icmp_hdr(skb)->type; 14588c7664fSArnaldo Carvalho de Melo const int code = icmp_hdr(skb)->code; 14620e1954fSEric Dumazet unsigned int data_len = 0; 1471da177e4SLinus Torvalds struct ip_tunnel *t; 148d2083287Sstephen hemminger 1491da177e4SLinus Torvalds switch (type) { 1501da177e4SLinus Torvalds default: 1511da177e4SLinus Torvalds case ICMP_PARAMETERPROB: 1529f57c67cSPravin B Shelar return; 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds case ICMP_DEST_UNREACH: 1551da177e4SLinus Torvalds switch (code) { 1561da177e4SLinus Torvalds case ICMP_SR_FAILED: 1571da177e4SLinus Torvalds case ICMP_PORT_UNREACH: 1581da177e4SLinus Torvalds /* Impossible event. */ 1599f57c67cSPravin B Shelar return; 1601da177e4SLinus Torvalds default: 1611da177e4SLinus Torvalds /* All others are translated to HOST_UNREACH. 1621da177e4SLinus Torvalds rfc2003 contains "deep thoughts" about NET_UNREACH, 1631da177e4SLinus Torvalds I believe they are just ether pollution. --ANK 1641da177e4SLinus Torvalds */ 1651da177e4SLinus Torvalds break; 1661da177e4SLinus Torvalds } 1671da177e4SLinus Torvalds break; 1689f57c67cSPravin B Shelar 1691da177e4SLinus Torvalds case ICMP_TIME_EXCEEDED: 1701da177e4SLinus Torvalds if (code != ICMP_EXC_TTL) 1719f57c67cSPravin B Shelar return; 17220e1954fSEric Dumazet data_len = icmp_hdr(skb)->un.reserved[1] * 4; /* RFC 4884 4.1 */ 1731da177e4SLinus Torvalds break; 17455be7a9cSDavid S. Miller 17555be7a9cSDavid S. Miller case ICMP_REDIRECT: 17655be7a9cSDavid S. Miller break; 1771da177e4SLinus Torvalds } 1781da177e4SLinus Torvalds 179bda7bb46SPravin B Shelar if (tpi->proto == htons(ETH_P_TEB)) 180c5441932SPravin B Shelar itn = net_generic(net, gre_tap_net_id); 181c5441932SPravin B Shelar else 182c5441932SPravin B Shelar itn = net_generic(net, ipgre_net_id); 183c5441932SPravin B Shelar 184c0c0c50fSDuan Jiong iph = (const struct iphdr *)(icmp_hdr(skb) + 1); 185bda7bb46SPravin B Shelar t = ip_tunnel_lookup(itn, skb->dev->ifindex, tpi->flags, 186bda7bb46SPravin B Shelar iph->daddr, iph->saddr, tpi->key); 187d2083287Sstephen hemminger 18851456b29SIan Morris if (!t) 1899f57c67cSPravin B Shelar return; 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)) 1959b8c6d7bSEric Dumazet return; 1969b8c6d7bSEric Dumazet #endif 1979b8c6d7bSEric Dumazet 19836393395SDavid S. Miller if (t->parms.iph.daddr == 0 || 199f97c1e0cSJoe Perches ipv4_is_multicast(t->parms.iph.daddr)) 2009f57c67cSPravin B Shelar return; 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED) 2039f57c67cSPravin B Shelar return; 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; 2109f57c67cSPravin B Shelar } 2119f57c67cSPravin B Shelar 2129f57c67cSPravin B Shelar static void gre_err(struct sk_buff *skb, u32 info) 2139f57c67cSPravin B Shelar { 2149f57c67cSPravin B Shelar /* All the routers (except for Linux) return only 2159f57c67cSPravin B Shelar * 8 bytes of packet payload. It means, that precise relaying of 2169f57c67cSPravin B Shelar * ICMP in the real Internet is absolutely infeasible. 2179f57c67cSPravin B Shelar * 2189f57c67cSPravin B Shelar * Moreover, Cisco "wise men" put GRE key to the third word 2199f57c67cSPravin B Shelar * in GRE header. It makes impossible maintaining even soft 2209f57c67cSPravin B Shelar * state for keyed 2219f57c67cSPravin B Shelar * GRE tunnels with enabled checksum. Tell them "thank you". 2229f57c67cSPravin B Shelar * 2239f57c67cSPravin B Shelar * Well, I wonder, rfc1812 was written by Cisco employee, 2249f57c67cSPravin B Shelar * what the hell these idiots break standards established 2259f57c67cSPravin B Shelar * by themselves??? 2269f57c67cSPravin B Shelar */ 2279f57c67cSPravin B Shelar 228e582615aSEric Dumazet const struct iphdr *iph = (struct iphdr *)skb->data; 2299f57c67cSPravin B Shelar const int type = icmp_hdr(skb)->type; 2309f57c67cSPravin B Shelar const int code = icmp_hdr(skb)->code; 2319f57c67cSPravin B Shelar struct tnl_ptk_info tpi; 2329f57c67cSPravin B Shelar bool csum_err = false; 2339f57c67cSPravin B Shelar 234e582615aSEric Dumazet if (gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IP), 235e582615aSEric Dumazet iph->ihl * 4) < 0) { 2369f57c67cSPravin B Shelar if (!csum_err) /* ignore csum errors. */ 2379f57c67cSPravin B Shelar return; 2389f57c67cSPravin B Shelar } 2399f57c67cSPravin B Shelar 2409f57c67cSPravin B Shelar if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) { 2419f57c67cSPravin B Shelar ipv4_update_pmtu(skb, dev_net(skb->dev), info, 2429f57c67cSPravin B Shelar skb->dev->ifindex, 0, IPPROTO_GRE, 0); 2439f57c67cSPravin B Shelar return; 2449f57c67cSPravin B Shelar } 2459f57c67cSPravin B Shelar if (type == ICMP_REDIRECT) { 2469f57c67cSPravin B Shelar ipv4_redirect(skb, dev_net(skb->dev), skb->dev->ifindex, 0, 2479f57c67cSPravin B Shelar IPPROTO_GRE, 0); 2489f57c67cSPravin B Shelar return; 2499f57c67cSPravin B Shelar } 2509f57c67cSPravin B Shelar 2519f57c67cSPravin B Shelar ipgre_err(skb, info, &tpi); 2521da177e4SLinus Torvalds } 2531da177e4SLinus Torvalds 25484e54fe0SWilliam Tu static int erspan_rcv(struct sk_buff *skb, struct tnl_ptk_info *tpi, 25584e54fe0SWilliam Tu int gre_hdr_len) 25684e54fe0SWilliam Tu { 25784e54fe0SWilliam Tu struct net *net = dev_net(skb->dev); 25884e54fe0SWilliam Tu struct metadata_dst *tun_dst = NULL; 2591d7e2ed2SWilliam Tu struct erspan_base_hdr *ershdr; 2601d7e2ed2SWilliam Tu struct erspan_metadata *pkt_md; 26184e54fe0SWilliam Tu struct ip_tunnel_net *itn; 26284e54fe0SWilliam Tu struct ip_tunnel *tunnel; 26384e54fe0SWilliam Tu const struct iphdr *iph; 2643df19283SWilliam Tu struct erspan_md2 *md2; 2651d7e2ed2SWilliam Tu int ver; 26684e54fe0SWilliam Tu int len; 26784e54fe0SWilliam Tu 26884e54fe0SWilliam Tu itn = net_generic(net, erspan_net_id); 26984e54fe0SWilliam Tu len = gre_hdr_len + sizeof(*ershdr); 27084e54fe0SWilliam Tu 2711d7e2ed2SWilliam Tu /* Check based hdr len */ 27284e54fe0SWilliam Tu if (unlikely(!pskb_may_pull(skb, len))) 273c05fad57SHaishuang Yan return PACKET_REJECT; 27484e54fe0SWilliam Tu 27584e54fe0SWilliam Tu iph = ip_hdr(skb); 2761d7e2ed2SWilliam Tu ershdr = (struct erspan_base_hdr *)(skb->data + gre_hdr_len); 277c69de58bSWilliam Tu ver = ershdr->ver; 27884e54fe0SWilliam Tu 27984e54fe0SWilliam Tu /* The original GRE header does not have key field, 28084e54fe0SWilliam Tu * Use ERSPAN 10-bit session ID as key. 28184e54fe0SWilliam Tu */ 282c69de58bSWilliam Tu tpi->key = cpu_to_be32(get_session_id(ershdr)); 28384e54fe0SWilliam Tu tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, 28484e54fe0SWilliam Tu tpi->flags | TUNNEL_KEY, 28584e54fe0SWilliam Tu iph->saddr, iph->daddr, tpi->key); 28684e54fe0SWilliam Tu 28784e54fe0SWilliam Tu if (tunnel) { 2881d7e2ed2SWilliam Tu len = gre_hdr_len + erspan_hdr_len(ver); 2891d7e2ed2SWilliam Tu if (unlikely(!pskb_may_pull(skb, len))) 290ae3e1337SWilliam Tu return PACKET_REJECT; 2911d7e2ed2SWilliam Tu 292d91e8db5SWilliam Tu ershdr = (struct erspan_base_hdr *)(skb->data + gre_hdr_len); 293d91e8db5SWilliam Tu pkt_md = (struct erspan_metadata *)(ershdr + 1); 294d91e8db5SWilliam Tu 29584e54fe0SWilliam Tu if (__iptunnel_pull_header(skb, 2961d7e2ed2SWilliam Tu len, 29784e54fe0SWilliam Tu htons(ETH_P_TEB), 29884e54fe0SWilliam Tu false, false) < 0) 29984e54fe0SWilliam Tu goto drop; 30084e54fe0SWilliam Tu 3011a66a836SWilliam Tu if (tunnel->collect_md) { 3021a66a836SWilliam Tu struct ip_tunnel_info *info; 3031a66a836SWilliam Tu struct erspan_metadata *md; 3041a66a836SWilliam Tu __be64 tun_id; 3051a66a836SWilliam Tu __be16 flags; 3061a66a836SWilliam Tu 3071a66a836SWilliam Tu tpi->flags |= TUNNEL_KEY; 3081a66a836SWilliam Tu flags = tpi->flags; 3091a66a836SWilliam Tu tun_id = key32_to_tunnel_id(tpi->key); 3101a66a836SWilliam Tu 3111a66a836SWilliam Tu tun_dst = ip_tun_rx_dst(skb, flags, 3121a66a836SWilliam Tu tun_id, sizeof(*md)); 3131a66a836SWilliam Tu if (!tun_dst) 3141a66a836SWilliam Tu return PACKET_REJECT; 3151a66a836SWilliam Tu 3161a66a836SWilliam Tu md = ip_tunnel_info_opts(&tun_dst->u.tun_info); 317f551c91dSWilliam Tu md->version = ver; 3183df19283SWilliam Tu md2 = &md->u.md2; 3193df19283SWilliam Tu memcpy(md2, pkt_md, ver == 1 ? ERSPAN_V1_MDSIZE : 3203df19283SWilliam Tu ERSPAN_V2_MDSIZE); 321f551c91dSWilliam Tu 3221a66a836SWilliam Tu info = &tun_dst->u.tun_info; 3231a66a836SWilliam Tu info->key.tun_flags |= TUNNEL_ERSPAN_OPT; 3241a66a836SWilliam Tu info->options_len = sizeof(*md); 3251a66a836SWilliam Tu } 3261a66a836SWilliam Tu 32784e54fe0SWilliam Tu skb_reset_mac_header(skb); 32884e54fe0SWilliam Tu ip_tunnel_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error); 32984e54fe0SWilliam Tu return PACKET_RCVD; 33084e54fe0SWilliam Tu } 33184e54fe0SWilliam Tu drop: 33284e54fe0SWilliam Tu kfree_skb(skb); 33384e54fe0SWilliam Tu return PACKET_RCVD; 33484e54fe0SWilliam Tu } 33584e54fe0SWilliam Tu 336125372faSJiri Benc static int __ipgre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi, 337125372faSJiri Benc struct ip_tunnel_net *itn, int hdr_len, bool raw_proto) 3381da177e4SLinus Torvalds { 3392e15ea39SPravin B Shelar struct metadata_dst *tun_dst = NULL; 340b71d1d42SEric Dumazet const struct iphdr *iph; 3411da177e4SLinus Torvalds struct ip_tunnel *tunnel; 3421da177e4SLinus Torvalds 343eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 344bda7bb46SPravin B Shelar tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, tpi->flags, 345bda7bb46SPravin B Shelar iph->saddr, iph->daddr, tpi->key); 3461da177e4SLinus Torvalds 347d2083287Sstephen hemminger if (tunnel) { 348125372faSJiri Benc if (__iptunnel_pull_header(skb, hdr_len, tpi->proto, 349125372faSJiri Benc raw_proto, false) < 0) 350244a797bSJiri Benc goto drop; 351244a797bSJiri Benc 352e271c7b4SJiri Benc if (tunnel->dev->type != ARPHRD_NONE) 3530e3da5bbSTimo Teräs skb_pop_mac_header(skb); 354e271c7b4SJiri Benc else 355e271c7b4SJiri Benc skb_reset_mac_header(skb); 3562e15ea39SPravin B Shelar if (tunnel->collect_md) { 357c29a70d2SPravin B Shelar __be16 flags; 358c29a70d2SPravin B Shelar __be64 tun_id; 3592e15ea39SPravin B Shelar 360c29a70d2SPravin B Shelar flags = tpi->flags & (TUNNEL_CSUM | TUNNEL_KEY); 361d817f432SAmir Vadai tun_id = key32_to_tunnel_id(tpi->key); 362c29a70d2SPravin B Shelar tun_dst = ip_tun_rx_dst(skb, flags, tun_id, 0); 3632e15ea39SPravin B Shelar if (!tun_dst) 3642e15ea39SPravin B Shelar return PACKET_REJECT; 3652e15ea39SPravin B Shelar } 3662e15ea39SPravin B Shelar 3672e15ea39SPravin B Shelar ip_tunnel_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error); 368bda7bb46SPravin B Shelar return PACKET_RCVD; 3691da177e4SLinus Torvalds } 370125372faSJiri Benc return PACKET_NEXT; 371244a797bSJiri Benc 372244a797bSJiri Benc drop: 373244a797bSJiri Benc kfree_skb(skb); 374244a797bSJiri Benc return PACKET_RCVD; 3751da177e4SLinus Torvalds } 3761da177e4SLinus Torvalds 377125372faSJiri Benc static int ipgre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi, 378125372faSJiri Benc int hdr_len) 379125372faSJiri Benc { 380125372faSJiri Benc struct net *net = dev_net(skb->dev); 381125372faSJiri Benc struct ip_tunnel_net *itn; 382125372faSJiri Benc int res; 383125372faSJiri Benc 384125372faSJiri Benc if (tpi->proto == htons(ETH_P_TEB)) 385125372faSJiri Benc itn = net_generic(net, gre_tap_net_id); 386125372faSJiri Benc else 387125372faSJiri Benc itn = net_generic(net, ipgre_net_id); 388125372faSJiri Benc 389125372faSJiri Benc res = __ipgre_rcv(skb, tpi, itn, hdr_len, false); 390125372faSJiri Benc if (res == PACKET_NEXT && tpi->proto == htons(ETH_P_TEB)) { 391125372faSJiri Benc /* ipgre tunnels in collect metadata mode should receive 392125372faSJiri Benc * also ETH_P_TEB traffic. 393125372faSJiri Benc */ 394125372faSJiri Benc itn = net_generic(net, ipgre_net_id); 395125372faSJiri Benc res = __ipgre_rcv(skb, tpi, itn, hdr_len, true); 396125372faSJiri Benc } 397125372faSJiri Benc return res; 398125372faSJiri Benc } 399125372faSJiri Benc 4009f57c67cSPravin B Shelar static int gre_rcv(struct sk_buff *skb) 4019f57c67cSPravin B Shelar { 4029f57c67cSPravin B Shelar struct tnl_ptk_info tpi; 4039f57c67cSPravin B Shelar bool csum_err = false; 40495f5c64cSTom Herbert int hdr_len; 4059f57c67cSPravin B Shelar 4069f57c67cSPravin B Shelar #ifdef CONFIG_NET_IPGRE_BROADCAST 4079f57c67cSPravin B Shelar if (ipv4_is_multicast(ip_hdr(skb)->daddr)) { 4089f57c67cSPravin B Shelar /* Looped back packet, drop it! */ 4099f57c67cSPravin B Shelar if (rt_is_output_route(skb_rtable(skb))) 4109f57c67cSPravin B Shelar goto drop; 4119f57c67cSPravin B Shelar } 4129f57c67cSPravin B Shelar #endif 4139f57c67cSPravin B Shelar 414e582615aSEric Dumazet hdr_len = gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IP), 0); 415f132ae7cSJiri Benc if (hdr_len < 0) 41695f5c64cSTom Herbert goto drop; 41795f5c64cSTom Herbert 418f551c91dSWilliam Tu if (unlikely(tpi.proto == htons(ETH_P_ERSPAN) || 419f551c91dSWilliam Tu tpi.proto == htons(ETH_P_ERSPAN2))) { 42084e54fe0SWilliam Tu if (erspan_rcv(skb, &tpi, hdr_len) == PACKET_RCVD) 42184e54fe0SWilliam Tu return 0; 422dd8d5b8cSHaishuang Yan goto out; 42384e54fe0SWilliam Tu } 42484e54fe0SWilliam Tu 425244a797bSJiri Benc if (ipgre_rcv(skb, &tpi, hdr_len) == PACKET_RCVD) 4269f57c67cSPravin B Shelar return 0; 4279f57c67cSPravin B Shelar 428dd8d5b8cSHaishuang Yan out: 4299f57c67cSPravin B Shelar icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); 4309f57c67cSPravin B Shelar drop: 4319f57c67cSPravin B Shelar kfree_skb(skb); 4329f57c67cSPravin B Shelar return 0; 4339f57c67cSPravin B Shelar } 4349f57c67cSPravin B Shelar 435c5441932SPravin B Shelar static void __gre_xmit(struct sk_buff *skb, struct net_device *dev, 436c5441932SPravin B Shelar const struct iphdr *tnl_params, 437c5441932SPravin B Shelar __be16 proto) 438c5441932SPravin B Shelar { 439c5441932SPravin B Shelar struct ip_tunnel *tunnel = netdev_priv(dev); 440c5441932SPravin B Shelar 441c5441932SPravin B Shelar if (tunnel->parms.o_flags & TUNNEL_SEQ) 442c5441932SPravin B Shelar tunnel->o_seqno++; 443cef401deSEric Dumazet 444c5441932SPravin B Shelar /* Push GRE header. */ 445182a352dSTom Herbert gre_build_header(skb, tunnel->tun_hlen, 446182a352dSTom Herbert tunnel->parms.o_flags, proto, tunnel->parms.o_key, 447182a352dSTom Herbert htonl(tunnel->o_seqno)); 4481da177e4SLinus Torvalds 449bf3d6a8fSNicolas Dichtel ip_tunnel_xmit(skb, dev, tnl_params, tnl_params->protocol); 4501da177e4SLinus Torvalds } 4511da177e4SLinus Torvalds 452aed069dfSAlexander Duyck static int gre_handle_offloads(struct sk_buff *skb, bool csum) 453b2acd1dcSPravin B Shelar { 4546fa79666SEdward Cree return iptunnel_handle_offloads(skb, csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE); 455b2acd1dcSPravin B Shelar } 456b2acd1dcSPravin B Shelar 457fc4099f1SPravin B Shelar static struct rtable *gre_get_rt(struct sk_buff *skb, 458fc4099f1SPravin B Shelar struct net_device *dev, 459fc4099f1SPravin B Shelar struct flowi4 *fl, 460fc4099f1SPravin B Shelar const struct ip_tunnel_key *key) 461fc4099f1SPravin B Shelar { 462fc4099f1SPravin B Shelar struct net *net = dev_net(dev); 463fc4099f1SPravin B Shelar 464fc4099f1SPravin B Shelar memset(fl, 0, sizeof(*fl)); 465fc4099f1SPravin B Shelar fl->daddr = key->u.ipv4.dst; 466fc4099f1SPravin B Shelar fl->saddr = key->u.ipv4.src; 467fc4099f1SPravin B Shelar fl->flowi4_tos = RT_TOS(key->tos); 468fc4099f1SPravin B Shelar fl->flowi4_mark = skb->mark; 469fc4099f1SPravin B Shelar fl->flowi4_proto = IPPROTO_GRE; 470fc4099f1SPravin B Shelar 471fc4099f1SPravin B Shelar return ip_route_output_key(net, fl); 472fc4099f1SPravin B Shelar } 473fc4099f1SPravin B Shelar 474862a03c3SWilliam Tu static struct rtable *prepare_fb_xmit(struct sk_buff *skb, 475862a03c3SWilliam Tu struct net_device *dev, 476862a03c3SWilliam Tu struct flowi4 *fl, 477862a03c3SWilliam Tu int tunnel_hlen) 4782e15ea39SPravin B Shelar { 4792e15ea39SPravin B Shelar struct ip_tunnel_info *tun_info; 4802e15ea39SPravin B Shelar const struct ip_tunnel_key *key; 481db3c6139SDaniel Borkmann struct rtable *rt = NULL; 4822e15ea39SPravin B Shelar int min_headroom; 483db3c6139SDaniel Borkmann bool use_cache; 4842e15ea39SPravin B Shelar int err; 4852e15ea39SPravin B Shelar 48661adedf3SJiri Benc tun_info = skb_tunnel_info(skb); 4872e15ea39SPravin B Shelar key = &tun_info->key; 488db3c6139SDaniel Borkmann use_cache = ip_tunnel_dst_cache_usable(skb, tun_info); 489862a03c3SWilliam Tu 490db3c6139SDaniel Borkmann if (use_cache) 491862a03c3SWilliam Tu rt = dst_cache_get_ip4(&tun_info->dst_cache, &fl->saddr); 4923c1cb4d2SPaolo Abeni if (!rt) { 493862a03c3SWilliam Tu rt = gre_get_rt(skb, dev, fl, key); 4942e15ea39SPravin B Shelar if (IS_ERR(rt)) 4952e15ea39SPravin B Shelar goto err_free_skb; 496db3c6139SDaniel Borkmann if (use_cache) 4973c1cb4d2SPaolo Abeni dst_cache_set_ip4(&tun_info->dst_cache, &rt->dst, 498862a03c3SWilliam Tu fl->saddr); 4993c1cb4d2SPaolo Abeni } 5002e15ea39SPravin B Shelar 5012e15ea39SPravin B Shelar min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len 5022e15ea39SPravin B Shelar + tunnel_hlen + sizeof(struct iphdr); 5032e15ea39SPravin B Shelar if (skb_headroom(skb) < min_headroom || skb_header_cloned(skb)) { 5042e15ea39SPravin B Shelar int head_delta = SKB_DATA_ALIGN(min_headroom - 5052e15ea39SPravin B Shelar skb_headroom(skb) + 5062e15ea39SPravin B Shelar 16); 5072e15ea39SPravin B Shelar err = pskb_expand_head(skb, max_t(int, head_delta, 0), 5082e15ea39SPravin B Shelar 0, GFP_ATOMIC); 5092e15ea39SPravin B Shelar if (unlikely(err)) 5102e15ea39SPravin B Shelar goto err_free_rt; 5112e15ea39SPravin B Shelar } 512862a03c3SWilliam Tu return rt; 513862a03c3SWilliam Tu 514862a03c3SWilliam Tu err_free_rt: 515862a03c3SWilliam Tu ip_rt_put(rt); 516862a03c3SWilliam Tu err_free_skb: 517862a03c3SWilliam Tu kfree_skb(skb); 518862a03c3SWilliam Tu dev->stats.tx_dropped++; 519862a03c3SWilliam Tu return NULL; 520862a03c3SWilliam Tu } 521862a03c3SWilliam Tu 522862a03c3SWilliam Tu static void gre_fb_xmit(struct sk_buff *skb, struct net_device *dev, 523862a03c3SWilliam Tu __be16 proto) 524862a03c3SWilliam Tu { 525*77a5196aSWilliam Tu struct ip_tunnel *tunnel = netdev_priv(dev); 526862a03c3SWilliam Tu struct ip_tunnel_info *tun_info; 527862a03c3SWilliam Tu const struct ip_tunnel_key *key; 528862a03c3SWilliam Tu struct rtable *rt = NULL; 529862a03c3SWilliam Tu struct flowi4 fl; 530862a03c3SWilliam Tu int tunnel_hlen; 531862a03c3SWilliam Tu __be16 df, flags; 532862a03c3SWilliam Tu 533862a03c3SWilliam Tu tun_info = skb_tunnel_info(skb); 534862a03c3SWilliam Tu if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) || 535862a03c3SWilliam Tu ip_tunnel_info_af(tun_info) != AF_INET)) 536862a03c3SWilliam Tu goto err_free_skb; 537862a03c3SWilliam Tu 538862a03c3SWilliam Tu key = &tun_info->key; 539862a03c3SWilliam Tu tunnel_hlen = gre_calc_hlen(key->tun_flags); 540862a03c3SWilliam Tu 541862a03c3SWilliam Tu rt = prepare_fb_xmit(skb, dev, &fl, tunnel_hlen); 542862a03c3SWilliam Tu if (!rt) 543862a03c3SWilliam Tu return; 5442e15ea39SPravin B Shelar 5452e15ea39SPravin B Shelar /* Push Tunnel header. */ 546aed069dfSAlexander Duyck if (gre_handle_offloads(skb, !!(tun_info->key.tun_flags & TUNNEL_CSUM))) 5472e15ea39SPravin B Shelar goto err_free_rt; 5482e15ea39SPravin B Shelar 549*77a5196aSWilliam Tu flags = tun_info->key.tun_flags & 550*77a5196aSWilliam Tu (TUNNEL_CSUM | TUNNEL_KEY | TUNNEL_SEQ); 551cba65321SDavid S. Miller gre_build_header(skb, tunnel_hlen, flags, proto, 552*77a5196aSWilliam Tu tunnel_id_to_key32(tun_info->key.tun_id), 553*77a5196aSWilliam Tu (flags | TUNNEL_SEQ) ? htonl(tunnel->o_seqno++) : 0); 5542e15ea39SPravin B Shelar 5552e15ea39SPravin B Shelar df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0; 556039f5062SPravin B Shelar 557039f5062SPravin B Shelar iptunnel_xmit(skb->sk, rt, skb, fl.saddr, key->u.ipv4.dst, IPPROTO_GRE, 5587c383fb2SJiri Benc key->tos, key->ttl, df, false); 5592e15ea39SPravin B Shelar return; 5602e15ea39SPravin B Shelar 5612e15ea39SPravin B Shelar err_free_rt: 5622e15ea39SPravin B Shelar ip_rt_put(rt); 5632e15ea39SPravin B Shelar err_free_skb: 5642e15ea39SPravin B Shelar kfree_skb(skb); 5652e15ea39SPravin B Shelar dev->stats.tx_dropped++; 5662e15ea39SPravin B Shelar } 5672e15ea39SPravin B Shelar 5681a66a836SWilliam Tu static void erspan_fb_xmit(struct sk_buff *skb, struct net_device *dev, 5691a66a836SWilliam Tu __be16 proto) 5701a66a836SWilliam Tu { 5711a66a836SWilliam Tu struct ip_tunnel *tunnel = netdev_priv(dev); 5721a66a836SWilliam Tu struct ip_tunnel_info *tun_info; 5731a66a836SWilliam Tu const struct ip_tunnel_key *key; 5741a66a836SWilliam Tu struct erspan_metadata *md; 5751a66a836SWilliam Tu struct rtable *rt = NULL; 5761a66a836SWilliam Tu bool truncate = false; 5771a66a836SWilliam Tu struct flowi4 fl; 5781a66a836SWilliam Tu int tunnel_hlen; 579f551c91dSWilliam Tu int version; 5801a66a836SWilliam Tu __be16 df; 5811a66a836SWilliam Tu 5821a66a836SWilliam Tu tun_info = skb_tunnel_info(skb); 5831a66a836SWilliam Tu if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) || 5841a66a836SWilliam Tu ip_tunnel_info_af(tun_info) != AF_INET)) 5851a66a836SWilliam Tu goto err_free_skb; 5861a66a836SWilliam Tu 5871a66a836SWilliam Tu key = &tun_info->key; 588f551c91dSWilliam Tu md = ip_tunnel_info_opts(tun_info); 589f551c91dSWilliam Tu if (!md) 590f551c91dSWilliam Tu goto err_free_rt; 5911a66a836SWilliam Tu 5921a66a836SWilliam Tu /* ERSPAN has fixed 8 byte GRE header */ 593f551c91dSWilliam Tu version = md->version; 594f551c91dSWilliam Tu tunnel_hlen = 8 + erspan_hdr_len(version); 5951a66a836SWilliam Tu 5961a66a836SWilliam Tu rt = prepare_fb_xmit(skb, dev, &fl, tunnel_hlen); 5971a66a836SWilliam Tu if (!rt) 5981a66a836SWilliam Tu return; 5991a66a836SWilliam Tu 6001a66a836SWilliam Tu if (gre_handle_offloads(skb, false)) 6011a66a836SWilliam Tu goto err_free_rt; 6021a66a836SWilliam Tu 603f192970dSWilliam Tu if (skb->len > dev->mtu + dev->hard_header_len) { 604f192970dSWilliam Tu pskb_trim(skb, dev->mtu + dev->hard_header_len); 6051a66a836SWilliam Tu truncate = true; 6061a66a836SWilliam Tu } 6071a66a836SWilliam Tu 608f551c91dSWilliam Tu if (version == 1) { 609c69de58bSWilliam Tu erspan_build_header(skb, ntohl(tunnel_id_to_key32(key->tun_id)), 6101d7e2ed2SWilliam Tu ntohl(md->u.index), truncate, true); 611f551c91dSWilliam Tu } else if (version == 2) { 612c69de58bSWilliam Tu erspan_build_header_v2(skb, 613c69de58bSWilliam Tu ntohl(tunnel_id_to_key32(key->tun_id)), 614c69de58bSWilliam Tu md->u.md2.dir, 615c69de58bSWilliam Tu get_hwid(&md->u.md2), 616c69de58bSWilliam Tu truncate, true); 617f551c91dSWilliam Tu } else { 618f551c91dSWilliam Tu goto err_free_rt; 619f551c91dSWilliam Tu } 6201a66a836SWilliam Tu 6211a66a836SWilliam Tu gre_build_header(skb, 8, TUNNEL_SEQ, 6221a66a836SWilliam Tu htons(ETH_P_ERSPAN), 0, htonl(tunnel->o_seqno++)); 6231a66a836SWilliam Tu 6241a66a836SWilliam Tu df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0; 6251a66a836SWilliam Tu 6261a66a836SWilliam Tu iptunnel_xmit(skb->sk, rt, skb, fl.saddr, key->u.ipv4.dst, IPPROTO_GRE, 6271a66a836SWilliam Tu key->tos, key->ttl, df, false); 6281a66a836SWilliam Tu return; 6291a66a836SWilliam Tu 6301a66a836SWilliam Tu err_free_rt: 6311a66a836SWilliam Tu ip_rt_put(rt); 6321a66a836SWilliam Tu err_free_skb: 6331a66a836SWilliam Tu kfree_skb(skb); 6341a66a836SWilliam Tu dev->stats.tx_dropped++; 6351a66a836SWilliam Tu } 6361a66a836SWilliam Tu 637fc4099f1SPravin B Shelar static int gre_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb) 638fc4099f1SPravin B Shelar { 639fc4099f1SPravin B Shelar struct ip_tunnel_info *info = skb_tunnel_info(skb); 640fc4099f1SPravin B Shelar struct rtable *rt; 641fc4099f1SPravin B Shelar struct flowi4 fl4; 642fc4099f1SPravin B Shelar 643fc4099f1SPravin B Shelar if (ip_tunnel_info_af(info) != AF_INET) 644fc4099f1SPravin B Shelar return -EINVAL; 645fc4099f1SPravin B Shelar 646fc4099f1SPravin B Shelar rt = gre_get_rt(skb, dev, &fl4, &info->key); 647fc4099f1SPravin B Shelar if (IS_ERR(rt)) 648fc4099f1SPravin B Shelar return PTR_ERR(rt); 649fc4099f1SPravin B Shelar 650fc4099f1SPravin B Shelar ip_rt_put(rt); 651fc4099f1SPravin B Shelar info->key.u.ipv4.src = fl4.saddr; 652fc4099f1SPravin B Shelar return 0; 653fc4099f1SPravin B Shelar } 654fc4099f1SPravin B Shelar 655c5441932SPravin B Shelar static netdev_tx_t ipgre_xmit(struct sk_buff *skb, 656c5441932SPravin B Shelar struct net_device *dev) 657ee34c1ebSMichal Schmidt { 658c5441932SPravin B Shelar struct ip_tunnel *tunnel = netdev_priv(dev); 659c5441932SPravin B Shelar const struct iphdr *tnl_params; 660ee34c1ebSMichal Schmidt 6612e15ea39SPravin B Shelar if (tunnel->collect_md) { 6622090714eSJiri Benc gre_fb_xmit(skb, dev, skb->protocol); 6632e15ea39SPravin B Shelar return NETDEV_TX_OK; 6642e15ea39SPravin B Shelar } 6652e15ea39SPravin B Shelar 666c5441932SPravin B Shelar if (dev->header_ops) { 667c5441932SPravin B Shelar /* Need space for new headers */ 668c5441932SPravin B Shelar if (skb_cow_head(skb, dev->needed_headroom - 6692bac7cb3SChen Gang (tunnel->hlen + sizeof(struct iphdr)))) 670c5441932SPravin B Shelar goto free_skb; 671ee34c1ebSMichal Schmidt 672c5441932SPravin B Shelar tnl_params = (const struct iphdr *)skb->data; 673cbb1e85fSDavid S. Miller 674c5441932SPravin B Shelar /* Pull skb since ip_tunnel_xmit() needs skb->data pointing 675c5441932SPravin B Shelar * to gre header. 676c5441932SPravin B Shelar */ 677c5441932SPravin B Shelar skb_pull(skb, tunnel->hlen + sizeof(struct iphdr)); 6788a0033a9STimo Teräs skb_reset_mac_header(skb); 679c5441932SPravin B Shelar } else { 680c5441932SPravin B Shelar if (skb_cow_head(skb, dev->needed_headroom)) 681c5441932SPravin B Shelar goto free_skb; 682c5441932SPravin B Shelar 683c5441932SPravin B Shelar tnl_params = &tunnel->parms.iph; 684ee34c1ebSMichal Schmidt } 685e1a80002SHerbert Xu 686aed069dfSAlexander Duyck if (gre_handle_offloads(skb, !!(tunnel->parms.o_flags & TUNNEL_CSUM))) 687aed069dfSAlexander Duyck goto free_skb; 6888a0033a9STimo Teräs 689c5441932SPravin B Shelar __gre_xmit(skb, dev, tnl_params, skb->protocol); 690c5441932SPravin B Shelar return NETDEV_TX_OK; 691c5441932SPravin B Shelar 692c5441932SPravin B Shelar free_skb: 6933acfa1e7SEric Dumazet kfree_skb(skb); 694c5441932SPravin B Shelar dev->stats.tx_dropped++; 695c5441932SPravin B Shelar return NETDEV_TX_OK; 696ee34c1ebSMichal Schmidt } 697ee34c1ebSMichal Schmidt 69884e54fe0SWilliam Tu static netdev_tx_t erspan_xmit(struct sk_buff *skb, 69984e54fe0SWilliam Tu struct net_device *dev) 70084e54fe0SWilliam Tu { 70184e54fe0SWilliam Tu struct ip_tunnel *tunnel = netdev_priv(dev); 70284e54fe0SWilliam Tu bool truncate = false; 70384e54fe0SWilliam Tu 7041a66a836SWilliam Tu if (tunnel->collect_md) { 7051a66a836SWilliam Tu erspan_fb_xmit(skb, dev, skb->protocol); 7061a66a836SWilliam Tu return NETDEV_TX_OK; 7071a66a836SWilliam Tu } 7081a66a836SWilliam Tu 70984e54fe0SWilliam Tu if (gre_handle_offloads(skb, false)) 71084e54fe0SWilliam Tu goto free_skb; 71184e54fe0SWilliam Tu 71284e54fe0SWilliam Tu if (skb_cow_head(skb, dev->needed_headroom)) 71384e54fe0SWilliam Tu goto free_skb; 71484e54fe0SWilliam Tu 715f192970dSWilliam Tu if (skb->len > dev->mtu + dev->hard_header_len) { 716f192970dSWilliam Tu pskb_trim(skb, dev->mtu + dev->hard_header_len); 71784e54fe0SWilliam Tu truncate = true; 71884e54fe0SWilliam Tu } 71984e54fe0SWilliam Tu 72084e54fe0SWilliam Tu /* Push ERSPAN header */ 721f551c91dSWilliam Tu if (tunnel->erspan_ver == 1) 722c69de58bSWilliam Tu erspan_build_header(skb, ntohl(tunnel->parms.o_key), 723c69de58bSWilliam Tu tunnel->index, 724a3222dc9SWilliam Tu truncate, true); 725f551c91dSWilliam Tu else 726c69de58bSWilliam Tu erspan_build_header_v2(skb, ntohl(tunnel->parms.o_key), 727f551c91dSWilliam Tu tunnel->dir, tunnel->hwid, 728f551c91dSWilliam Tu truncate, true); 729f551c91dSWilliam Tu 73084e54fe0SWilliam Tu tunnel->parms.o_flags &= ~TUNNEL_KEY; 73184e54fe0SWilliam Tu __gre_xmit(skb, dev, &tunnel->parms.iph, htons(ETH_P_ERSPAN)); 73284e54fe0SWilliam Tu return NETDEV_TX_OK; 73384e54fe0SWilliam Tu 73484e54fe0SWilliam Tu free_skb: 73584e54fe0SWilliam Tu kfree_skb(skb); 73684e54fe0SWilliam Tu dev->stats.tx_dropped++; 73784e54fe0SWilliam Tu return NETDEV_TX_OK; 73884e54fe0SWilliam Tu } 73984e54fe0SWilliam Tu 740c5441932SPravin B Shelar static netdev_tx_t gre_tap_xmit(struct sk_buff *skb, 741c5441932SPravin B Shelar struct net_device *dev) 742c5441932SPravin B Shelar { 743c5441932SPravin B Shelar struct ip_tunnel *tunnel = netdev_priv(dev); 744ee34c1ebSMichal Schmidt 7452e15ea39SPravin B Shelar if (tunnel->collect_md) { 7462090714eSJiri Benc gre_fb_xmit(skb, dev, htons(ETH_P_TEB)); 7472e15ea39SPravin B Shelar return NETDEV_TX_OK; 7482e15ea39SPravin B Shelar } 7492e15ea39SPravin B Shelar 750aed069dfSAlexander Duyck if (gre_handle_offloads(skb, !!(tunnel->parms.o_flags & TUNNEL_CSUM))) 751aed069dfSAlexander Duyck goto free_skb; 752ee34c1ebSMichal Schmidt 753c5441932SPravin B Shelar if (skb_cow_head(skb, dev->needed_headroom)) 754c5441932SPravin B Shelar goto free_skb; 75542aa9162SHerbert Xu 756c5441932SPravin B Shelar __gre_xmit(skb, dev, &tunnel->parms.iph, htons(ETH_P_TEB)); 757c5441932SPravin B Shelar return NETDEV_TX_OK; 758c5441932SPravin B Shelar 759c5441932SPravin B Shelar free_skb: 7603acfa1e7SEric Dumazet kfree_skb(skb); 761c5441932SPravin B Shelar dev->stats.tx_dropped++; 762c5441932SPravin B Shelar return NETDEV_TX_OK; 76368c33163SPravin B Shelar } 764ee34c1ebSMichal Schmidt 765dd9d598cSXin Long static void ipgre_link_update(struct net_device *dev, bool set_mtu) 766dd9d598cSXin Long { 767dd9d598cSXin Long struct ip_tunnel *tunnel = netdev_priv(dev); 768dd9d598cSXin Long int len; 769dd9d598cSXin Long 770dd9d598cSXin Long len = tunnel->tun_hlen; 771dd9d598cSXin Long tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags); 772dd9d598cSXin Long len = tunnel->tun_hlen - len; 773dd9d598cSXin Long tunnel->hlen = tunnel->hlen + len; 774dd9d598cSXin Long 775dd9d598cSXin Long dev->needed_headroom = dev->needed_headroom + len; 776dd9d598cSXin Long if (set_mtu) 777dd9d598cSXin Long dev->mtu = max_t(int, dev->mtu - len, 68); 778dd9d598cSXin Long 779dd9d598cSXin Long if (!(tunnel->parms.o_flags & TUNNEL_SEQ)) { 780dd9d598cSXin Long if (!(tunnel->parms.o_flags & TUNNEL_CSUM) || 781dd9d598cSXin Long tunnel->encap.type == TUNNEL_ENCAP_NONE) { 782dd9d598cSXin Long dev->features |= NETIF_F_GSO_SOFTWARE; 783dd9d598cSXin Long dev->hw_features |= NETIF_F_GSO_SOFTWARE; 784dd9d598cSXin Long } 785dd9d598cSXin Long dev->features |= NETIF_F_LLTX; 786dd9d598cSXin Long } 787dd9d598cSXin Long } 788dd9d598cSXin Long 789c5441932SPravin B Shelar static int ipgre_tunnel_ioctl(struct net_device *dev, 790c5441932SPravin B Shelar struct ifreq *ifr, int cmd) 7911da177e4SLinus Torvalds { 7921da177e4SLinus Torvalds struct ip_tunnel_parm p; 793a0efab67SXin Long int err; 7941da177e4SLinus Torvalds 7951da177e4SLinus Torvalds if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) 796c5441932SPravin B Shelar return -EFAULT; 797a0efab67SXin Long 7986c734fb8SCong Wang if (cmd == SIOCADDTUNNEL || cmd == SIOCCHGTUNNEL) { 7991da177e4SLinus Torvalds if (p.iph.version != 4 || p.iph.protocol != IPPROTO_GRE || 8001da177e4SLinus Torvalds p.iph.ihl != 5 || (p.iph.frag_off & htons(~IP_DF)) || 8016c734fb8SCong Wang ((p.i_flags | p.o_flags) & (GRE_VERSION | GRE_ROUTING))) 8021da177e4SLinus Torvalds return -EINVAL; 803c5441932SPravin B Shelar } 804a0efab67SXin Long 805c5441932SPravin B Shelar p.i_flags = gre_flags_to_tnl_flags(p.i_flags); 806c5441932SPravin B Shelar p.o_flags = gre_flags_to_tnl_flags(p.o_flags); 807c5441932SPravin B Shelar 808c5441932SPravin B Shelar err = ip_tunnel_ioctl(dev, &p, cmd); 809c5441932SPravin B Shelar if (err) 810c5441932SPravin B Shelar return err; 811c5441932SPravin B Shelar 812a0efab67SXin Long if (cmd == SIOCCHGTUNNEL) { 813a0efab67SXin Long struct ip_tunnel *t = netdev_priv(dev); 814a0efab67SXin Long 815a0efab67SXin Long t->parms.i_flags = p.i_flags; 816a0efab67SXin Long t->parms.o_flags = p.o_flags; 817a0efab67SXin Long 818a0efab67SXin Long if (strcmp(dev->rtnl_link_ops->kind, "erspan")) 819a0efab67SXin Long ipgre_link_update(dev, true); 820a0efab67SXin Long } 821a0efab67SXin Long 82295f5c64cSTom Herbert p.i_flags = gre_tnl_flags_to_gre_flags(p.i_flags); 82395f5c64cSTom Herbert p.o_flags = gre_tnl_flags_to_gre_flags(p.o_flags); 824c5441932SPravin B Shelar 825c5441932SPravin B Shelar if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) 826c5441932SPravin B Shelar return -EFAULT; 827a0efab67SXin Long 8281da177e4SLinus Torvalds return 0; 8291da177e4SLinus Torvalds } 8301da177e4SLinus Torvalds 8311da177e4SLinus Torvalds /* Nice toy. Unfortunately, useless in real life :-) 8321da177e4SLinus Torvalds It allows to construct virtual multiprotocol broadcast "LAN" 8331da177e4SLinus Torvalds over the Internet, provided multicast routing is tuned. 8341da177e4SLinus Torvalds 8351da177e4SLinus Torvalds 8361da177e4SLinus Torvalds I have no idea was this bicycle invented before me, 8371da177e4SLinus Torvalds so that I had to set ARPHRD_IPGRE to a random value. 8381da177e4SLinus Torvalds I have an impression, that Cisco could make something similar, 8391da177e4SLinus Torvalds but this feature is apparently missing in IOS<=11.2(8). 8401da177e4SLinus Torvalds 8411da177e4SLinus Torvalds I set up 10.66.66/24 and fec0:6666:6666::0/96 as virtual networks 8421da177e4SLinus Torvalds with broadcast 224.66.66.66. If you have access to mbone, play with me :-) 8431da177e4SLinus Torvalds 8441da177e4SLinus Torvalds ping -t 255 224.66.66.66 8451da177e4SLinus Torvalds 8461da177e4SLinus Torvalds If nobody answers, mbone does not work. 8471da177e4SLinus Torvalds 8481da177e4SLinus Torvalds ip tunnel add Universe mode gre remote 224.66.66.66 local <Your_real_addr> ttl 255 8491da177e4SLinus Torvalds ip addr add 10.66.66.<somewhat>/24 dev Universe 8501da177e4SLinus Torvalds ifconfig Universe up 8511da177e4SLinus Torvalds ifconfig Universe add fe80::<Your_real_addr>/10 8521da177e4SLinus Torvalds ifconfig Universe add fec0:6666:6666::<Your_real_addr>/96 8531da177e4SLinus Torvalds ftp 10.66.66.66 8541da177e4SLinus Torvalds ... 8551da177e4SLinus Torvalds ftp fec0:6666:6666::193.233.7.65 8561da177e4SLinus Torvalds ... 8571da177e4SLinus Torvalds */ 8583b04dddeSStephen Hemminger static int ipgre_header(struct sk_buff *skb, struct net_device *dev, 8593b04dddeSStephen Hemminger unsigned short type, 8601507850bSEric Dumazet const void *daddr, const void *saddr, unsigned int len) 8611da177e4SLinus Torvalds { 8622941a486SPatrick McHardy struct ip_tunnel *t = netdev_priv(dev); 863c5441932SPravin B Shelar struct iphdr *iph; 864c5441932SPravin B Shelar struct gre_base_hdr *greh; 865c5441932SPravin B Shelar 866d58ff351SJohannes Berg iph = skb_push(skb, t->hlen + sizeof(*iph)); 867c5441932SPravin B Shelar greh = (struct gre_base_hdr *)(iph+1); 86895f5c64cSTom Herbert greh->flags = gre_tnl_flags_to_gre_flags(t->parms.o_flags); 869c5441932SPravin B Shelar greh->protocol = htons(type); 8701da177e4SLinus Torvalds 8711da177e4SLinus Torvalds memcpy(iph, &t->parms.iph, sizeof(struct iphdr)); 8721da177e4SLinus Torvalds 873c5441932SPravin B Shelar /* Set the source hardware address. */ 8741da177e4SLinus Torvalds if (saddr) 8751da177e4SLinus Torvalds memcpy(&iph->saddr, saddr, 4); 8766d55cb91STimo Teräs if (daddr) 8771da177e4SLinus Torvalds memcpy(&iph->daddr, daddr, 4); 8786d55cb91STimo Teräs if (iph->daddr) 87977a482bdSTimo Teräs return t->hlen + sizeof(*iph); 8801da177e4SLinus Torvalds 881c5441932SPravin B Shelar return -(t->hlen + sizeof(*iph)); 8821da177e4SLinus Torvalds } 8831da177e4SLinus Torvalds 8846a5f44d7STimo Teras static int ipgre_header_parse(const struct sk_buff *skb, unsigned char *haddr) 8856a5f44d7STimo Teras { 886b71d1d42SEric Dumazet const struct iphdr *iph = (const struct iphdr *) skb_mac_header(skb); 8876a5f44d7STimo Teras memcpy(haddr, &iph->saddr, 4); 8886a5f44d7STimo Teras return 4; 8896a5f44d7STimo Teras } 8906a5f44d7STimo Teras 8913b04dddeSStephen Hemminger static const struct header_ops ipgre_header_ops = { 8923b04dddeSStephen Hemminger .create = ipgre_header, 8936a5f44d7STimo Teras .parse = ipgre_header_parse, 8943b04dddeSStephen Hemminger }; 8953b04dddeSStephen Hemminger 8966a5f44d7STimo Teras #ifdef CONFIG_NET_IPGRE_BROADCAST 8971da177e4SLinus Torvalds static int ipgre_open(struct net_device *dev) 8981da177e4SLinus Torvalds { 8992941a486SPatrick McHardy struct ip_tunnel *t = netdev_priv(dev); 9001da177e4SLinus Torvalds 901f97c1e0cSJoe Perches if (ipv4_is_multicast(t->parms.iph.daddr)) { 902cbb1e85fSDavid S. Miller struct flowi4 fl4; 903cbb1e85fSDavid S. Miller struct rtable *rt; 904cbb1e85fSDavid S. Miller 905b57708adSNicolas Dichtel rt = ip_route_output_gre(t->net, &fl4, 90678fbfd8aSDavid S. Miller t->parms.iph.daddr, 90778fbfd8aSDavid S. Miller t->parms.iph.saddr, 90878fbfd8aSDavid S. Miller t->parms.o_key, 90978fbfd8aSDavid S. Miller RT_TOS(t->parms.iph.tos), 91078fbfd8aSDavid S. Miller t->parms.link); 911b23dd4feSDavid S. Miller if (IS_ERR(rt)) 9121da177e4SLinus Torvalds return -EADDRNOTAVAIL; 913d8d1f30bSChangli Gao dev = rt->dst.dev; 9141da177e4SLinus Torvalds ip_rt_put(rt); 91551456b29SIan Morris if (!__in_dev_get_rtnl(dev)) 9161da177e4SLinus Torvalds return -EADDRNOTAVAIL; 9171da177e4SLinus Torvalds t->mlink = dev->ifindex; 918e5ed6399SHerbert Xu ip_mc_inc_group(__in_dev_get_rtnl(dev), t->parms.iph.daddr); 9191da177e4SLinus Torvalds } 9201da177e4SLinus Torvalds return 0; 9211da177e4SLinus Torvalds } 9221da177e4SLinus Torvalds 9231da177e4SLinus Torvalds static int ipgre_close(struct net_device *dev) 9241da177e4SLinus Torvalds { 9252941a486SPatrick McHardy struct ip_tunnel *t = netdev_priv(dev); 926b8c26a33SStephen Hemminger 927f97c1e0cSJoe Perches if (ipv4_is_multicast(t->parms.iph.daddr) && t->mlink) { 9287fee0ca2SDenis V. Lunev struct in_device *in_dev; 929b57708adSNicolas Dichtel in_dev = inetdev_by_index(t->net, t->mlink); 9308723e1b4SEric Dumazet if (in_dev) 9311da177e4SLinus Torvalds ip_mc_dec_group(in_dev, t->parms.iph.daddr); 9321da177e4SLinus Torvalds } 9331da177e4SLinus Torvalds return 0; 9341da177e4SLinus Torvalds } 9351da177e4SLinus Torvalds #endif 9361da177e4SLinus Torvalds 937b8c26a33SStephen Hemminger static const struct net_device_ops ipgre_netdev_ops = { 938b8c26a33SStephen Hemminger .ndo_init = ipgre_tunnel_init, 939c5441932SPravin B Shelar .ndo_uninit = ip_tunnel_uninit, 940b8c26a33SStephen Hemminger #ifdef CONFIG_NET_IPGRE_BROADCAST 941b8c26a33SStephen Hemminger .ndo_open = ipgre_open, 942b8c26a33SStephen Hemminger .ndo_stop = ipgre_close, 943b8c26a33SStephen Hemminger #endif 944c5441932SPravin B Shelar .ndo_start_xmit = ipgre_xmit, 945b8c26a33SStephen Hemminger .ndo_do_ioctl = ipgre_tunnel_ioctl, 946c5441932SPravin B Shelar .ndo_change_mtu = ip_tunnel_change_mtu, 947c5441932SPravin B Shelar .ndo_get_stats64 = ip_tunnel_get_stats64, 9481e99584bSNicolas Dichtel .ndo_get_iflink = ip_tunnel_get_iflink, 949b8c26a33SStephen Hemminger }; 950b8c26a33SStephen Hemminger 9516b78f16eSEric Dumazet #define GRE_FEATURES (NETIF_F_SG | \ 9526b78f16eSEric Dumazet NETIF_F_FRAGLIST | \ 9536b78f16eSEric Dumazet NETIF_F_HIGHDMA | \ 9546b78f16eSEric Dumazet NETIF_F_HW_CSUM) 9556b78f16eSEric Dumazet 9561da177e4SLinus Torvalds static void ipgre_tunnel_setup(struct net_device *dev) 9571da177e4SLinus Torvalds { 958b8c26a33SStephen Hemminger dev->netdev_ops = &ipgre_netdev_ops; 9595a455275SNicolas Dichtel dev->type = ARPHRD_IPGRE; 960c5441932SPravin B Shelar ip_tunnel_setup(dev, ipgre_net_id); 961c5441932SPravin B Shelar } 9621da177e4SLinus Torvalds 963c5441932SPravin B Shelar static void __gre_tunnel_init(struct net_device *dev) 964c5441932SPravin B Shelar { 965c5441932SPravin B Shelar struct ip_tunnel *tunnel; 9664565e991STom Herbert int t_hlen; 967c5441932SPravin B Shelar 968c5441932SPravin B Shelar tunnel = netdev_priv(dev); 96995f5c64cSTom Herbert tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags); 970c5441932SPravin B Shelar tunnel->parms.iph.protocol = IPPROTO_GRE; 971c5441932SPravin B Shelar 9724565e991STom Herbert tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen; 9734565e991STom Herbert 9744565e991STom Herbert t_hlen = tunnel->hlen + sizeof(struct iphdr); 9754565e991STom Herbert 9764565e991STom Herbert dev->needed_headroom = LL_MAX_HEADER + t_hlen + 4; 9774565e991STom Herbert dev->mtu = ETH_DATA_LEN - t_hlen - 4; 9786b78f16eSEric Dumazet 979b57708adSNicolas Dichtel dev->features |= GRE_FEATURES; 9806b78f16eSEric Dumazet dev->hw_features |= GRE_FEATURES; 981c5441932SPravin B Shelar 982c5441932SPravin B Shelar if (!(tunnel->parms.o_flags & TUNNEL_SEQ)) { 983a0ca153fSAlexander Duyck /* TCP offload with GRE SEQ is not supported, nor 984a0ca153fSAlexander Duyck * can we support 2 levels of outer headers requiring 985a0ca153fSAlexander Duyck * an update. 986a0ca153fSAlexander Duyck */ 987a0ca153fSAlexander Duyck if (!(tunnel->parms.o_flags & TUNNEL_CSUM) || 988a0ca153fSAlexander Duyck (tunnel->encap.type == TUNNEL_ENCAP_NONE)) { 989c5441932SPravin B Shelar dev->features |= NETIF_F_GSO_SOFTWARE; 990c5441932SPravin B Shelar dev->hw_features |= NETIF_F_GSO_SOFTWARE; 991a0ca153fSAlexander Duyck } 992a0ca153fSAlexander Duyck 993c5441932SPravin B Shelar /* Can use a lockless transmit, unless we generate 994c5441932SPravin B Shelar * output sequences 995c5441932SPravin B Shelar */ 996c5441932SPravin B Shelar dev->features |= NETIF_F_LLTX; 997c5441932SPravin B Shelar } 9981da177e4SLinus Torvalds } 9991da177e4SLinus Torvalds 10001da177e4SLinus Torvalds static int ipgre_tunnel_init(struct net_device *dev) 10011da177e4SLinus Torvalds { 1002c5441932SPravin B Shelar struct ip_tunnel *tunnel = netdev_priv(dev); 1003c5441932SPravin B Shelar struct iphdr *iph = &tunnel->parms.iph; 10041da177e4SLinus Torvalds 1005c5441932SPravin B Shelar __gre_tunnel_init(dev); 10061da177e4SLinus Torvalds 1007c5441932SPravin B Shelar memcpy(dev->dev_addr, &iph->saddr, 4); 1008c5441932SPravin B Shelar memcpy(dev->broadcast, &iph->daddr, 4); 10091da177e4SLinus Torvalds 1010c5441932SPravin B Shelar dev->flags = IFF_NOARP; 101102875878SEric Dumazet netif_keep_dst(dev); 1012c5441932SPravin B Shelar dev->addr_len = 4; 10131da177e4SLinus Torvalds 1014a64b04d8SJiri Benc if (iph->daddr && !tunnel->collect_md) { 10151da177e4SLinus Torvalds #ifdef CONFIG_NET_IPGRE_BROADCAST 1016f97c1e0cSJoe Perches if (ipv4_is_multicast(iph->daddr)) { 10171da177e4SLinus Torvalds if (!iph->saddr) 10181da177e4SLinus Torvalds return -EINVAL; 10191da177e4SLinus Torvalds dev->flags = IFF_BROADCAST; 10203b04dddeSStephen Hemminger dev->header_ops = &ipgre_header_ops; 10211da177e4SLinus Torvalds } 10221da177e4SLinus Torvalds #endif 1023a64b04d8SJiri Benc } else if (!tunnel->collect_md) { 10246a5f44d7STimo Teras dev->header_ops = &ipgre_header_ops; 1025a64b04d8SJiri Benc } 10261da177e4SLinus Torvalds 1027c5441932SPravin B Shelar return ip_tunnel_init(dev); 102860769a5dSEric Dumazet } 102960769a5dSEric Dumazet 10309f57c67cSPravin B Shelar static const struct gre_protocol ipgre_protocol = { 10319f57c67cSPravin B Shelar .handler = gre_rcv, 10329f57c67cSPravin B Shelar .err_handler = gre_err, 10331da177e4SLinus Torvalds }; 10341da177e4SLinus Torvalds 10352c8c1e72SAlexey Dobriyan static int __net_init ipgre_init_net(struct net *net) 103659a4c759SPavel Emelyanov { 1037c5441932SPravin B Shelar return ip_tunnel_init_net(net, ipgre_net_id, &ipgre_link_ops, NULL); 103859a4c759SPavel Emelyanov } 103959a4c759SPavel Emelyanov 104064bc1781SEric Dumazet static void __net_exit ipgre_exit_batch_net(struct list_head *list_net) 104159a4c759SPavel Emelyanov { 104264bc1781SEric Dumazet ip_tunnel_delete_nets(list_net, ipgre_net_id, &ipgre_link_ops); 104359a4c759SPavel Emelyanov } 104459a4c759SPavel Emelyanov 104559a4c759SPavel Emelyanov static struct pernet_operations ipgre_net_ops = { 104659a4c759SPavel Emelyanov .init = ipgre_init_net, 104764bc1781SEric Dumazet .exit_batch = ipgre_exit_batch_net, 1048cfb8fbf2SEric W. Biederman .id = &ipgre_net_id, 1049c5441932SPravin B Shelar .size = sizeof(struct ip_tunnel_net), 105031502104SKirill Tkhai .async = true, 105159a4c759SPavel Emelyanov }; 10521da177e4SLinus Torvalds 1053a8b8a889SMatthias Schiffer static int ipgre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[], 1054a8b8a889SMatthias Schiffer struct netlink_ext_ack *extack) 1055c19e654dSHerbert Xu { 1056c19e654dSHerbert Xu __be16 flags; 1057c19e654dSHerbert Xu 1058c19e654dSHerbert Xu if (!data) 1059c19e654dSHerbert Xu return 0; 1060c19e654dSHerbert Xu 1061c19e654dSHerbert Xu flags = 0; 1062c19e654dSHerbert Xu if (data[IFLA_GRE_IFLAGS]) 1063c19e654dSHerbert Xu flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]); 1064c19e654dSHerbert Xu if (data[IFLA_GRE_OFLAGS]) 1065c19e654dSHerbert Xu flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]); 1066c19e654dSHerbert Xu if (flags & (GRE_VERSION|GRE_ROUTING)) 1067c19e654dSHerbert Xu return -EINVAL; 1068c19e654dSHerbert Xu 1069946b636fSJiri Benc if (data[IFLA_GRE_COLLECT_METADATA] && 1070946b636fSJiri Benc data[IFLA_GRE_ENCAP_TYPE] && 1071946b636fSJiri Benc nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]) != TUNNEL_ENCAP_NONE) 1072946b636fSJiri Benc return -EINVAL; 1073946b636fSJiri Benc 1074c19e654dSHerbert Xu return 0; 1075c19e654dSHerbert Xu } 1076c19e654dSHerbert Xu 1077a8b8a889SMatthias Schiffer static int ipgre_tap_validate(struct nlattr *tb[], struct nlattr *data[], 1078a8b8a889SMatthias Schiffer struct netlink_ext_ack *extack) 1079e1a80002SHerbert Xu { 1080e1a80002SHerbert Xu __be32 daddr; 1081e1a80002SHerbert Xu 1082e1a80002SHerbert Xu if (tb[IFLA_ADDRESS]) { 1083e1a80002SHerbert Xu if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) 1084e1a80002SHerbert Xu return -EINVAL; 1085e1a80002SHerbert Xu if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) 1086e1a80002SHerbert Xu return -EADDRNOTAVAIL; 1087e1a80002SHerbert Xu } 1088e1a80002SHerbert Xu 1089e1a80002SHerbert Xu if (!data) 1090e1a80002SHerbert Xu goto out; 1091e1a80002SHerbert Xu 1092e1a80002SHerbert Xu if (data[IFLA_GRE_REMOTE]) { 1093e1a80002SHerbert Xu memcpy(&daddr, nla_data(data[IFLA_GRE_REMOTE]), 4); 1094e1a80002SHerbert Xu if (!daddr) 1095e1a80002SHerbert Xu return -EINVAL; 1096e1a80002SHerbert Xu } 1097e1a80002SHerbert Xu 1098e1a80002SHerbert Xu out: 1099a8b8a889SMatthias Schiffer return ipgre_tunnel_validate(tb, data, extack); 1100e1a80002SHerbert Xu } 1101e1a80002SHerbert Xu 110284e54fe0SWilliam Tu static int erspan_validate(struct nlattr *tb[], struct nlattr *data[], 110384e54fe0SWilliam Tu struct netlink_ext_ack *extack) 110484e54fe0SWilliam Tu { 110584e54fe0SWilliam Tu __be16 flags = 0; 110684e54fe0SWilliam Tu int ret; 110784e54fe0SWilliam Tu 110884e54fe0SWilliam Tu if (!data) 110984e54fe0SWilliam Tu return 0; 111084e54fe0SWilliam Tu 111184e54fe0SWilliam Tu ret = ipgre_tap_validate(tb, data, extack); 111284e54fe0SWilliam Tu if (ret) 111384e54fe0SWilliam Tu return ret; 111484e54fe0SWilliam Tu 111584e54fe0SWilliam Tu /* ERSPAN should only have GRE sequence and key flag */ 11161a66a836SWilliam Tu if (data[IFLA_GRE_OFLAGS]) 111784e54fe0SWilliam Tu flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]); 11181a66a836SWilliam Tu if (data[IFLA_GRE_IFLAGS]) 111984e54fe0SWilliam Tu flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]); 11201a66a836SWilliam Tu if (!data[IFLA_GRE_COLLECT_METADATA] && 11211a66a836SWilliam Tu flags != (GRE_SEQ | GRE_KEY)) 112284e54fe0SWilliam Tu return -EINVAL; 112384e54fe0SWilliam Tu 112484e54fe0SWilliam Tu /* ERSPAN Session ID only has 10-bit. Since we reuse 112584e54fe0SWilliam Tu * 32-bit key field as ID, check it's range. 112684e54fe0SWilliam Tu */ 112784e54fe0SWilliam Tu if (data[IFLA_GRE_IKEY] && 112884e54fe0SWilliam Tu (ntohl(nla_get_be32(data[IFLA_GRE_IKEY])) & ~ID_MASK)) 112984e54fe0SWilliam Tu return -EINVAL; 113084e54fe0SWilliam Tu 113184e54fe0SWilliam Tu if (data[IFLA_GRE_OKEY] && 113284e54fe0SWilliam Tu (ntohl(nla_get_be32(data[IFLA_GRE_OKEY])) & ~ID_MASK)) 113384e54fe0SWilliam Tu return -EINVAL; 113484e54fe0SWilliam Tu 113584e54fe0SWilliam Tu return 0; 113684e54fe0SWilliam Tu } 113784e54fe0SWilliam Tu 113822a59be8SPhilip Prindeville static int ipgre_netlink_parms(struct net_device *dev, 11392e15ea39SPravin B Shelar struct nlattr *data[], 11402e15ea39SPravin B Shelar struct nlattr *tb[], 11419830ad4cSCraig Gallek struct ip_tunnel_parm *parms, 11429830ad4cSCraig Gallek __u32 *fwmark) 1143c19e654dSHerbert Xu { 114422a59be8SPhilip Prindeville struct ip_tunnel *t = netdev_priv(dev); 114522a59be8SPhilip Prindeville 11467bb82d92SHerbert Xu memset(parms, 0, sizeof(*parms)); 1147c19e654dSHerbert Xu 1148c19e654dSHerbert Xu parms->iph.protocol = IPPROTO_GRE; 1149c19e654dSHerbert Xu 1150c19e654dSHerbert Xu if (!data) 115122a59be8SPhilip Prindeville return 0; 1152c19e654dSHerbert Xu 1153c19e654dSHerbert Xu if (data[IFLA_GRE_LINK]) 1154c19e654dSHerbert Xu parms->link = nla_get_u32(data[IFLA_GRE_LINK]); 1155c19e654dSHerbert Xu 1156c19e654dSHerbert Xu if (data[IFLA_GRE_IFLAGS]) 1157c5441932SPravin B Shelar parms->i_flags = gre_flags_to_tnl_flags(nla_get_be16(data[IFLA_GRE_IFLAGS])); 1158c19e654dSHerbert Xu 1159c19e654dSHerbert Xu if (data[IFLA_GRE_OFLAGS]) 1160c5441932SPravin B Shelar parms->o_flags = gre_flags_to_tnl_flags(nla_get_be16(data[IFLA_GRE_OFLAGS])); 1161c19e654dSHerbert Xu 1162c19e654dSHerbert Xu if (data[IFLA_GRE_IKEY]) 1163c19e654dSHerbert Xu parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]); 1164c19e654dSHerbert Xu 1165c19e654dSHerbert Xu if (data[IFLA_GRE_OKEY]) 1166c19e654dSHerbert Xu parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]); 1167c19e654dSHerbert Xu 1168c19e654dSHerbert Xu if (data[IFLA_GRE_LOCAL]) 116967b61f6cSJiri Benc parms->iph.saddr = nla_get_in_addr(data[IFLA_GRE_LOCAL]); 1170c19e654dSHerbert Xu 1171c19e654dSHerbert Xu if (data[IFLA_GRE_REMOTE]) 117267b61f6cSJiri Benc parms->iph.daddr = nla_get_in_addr(data[IFLA_GRE_REMOTE]); 1173c19e654dSHerbert Xu 1174c19e654dSHerbert Xu if (data[IFLA_GRE_TTL]) 1175c19e654dSHerbert Xu parms->iph.ttl = nla_get_u8(data[IFLA_GRE_TTL]); 1176c19e654dSHerbert Xu 1177c19e654dSHerbert Xu if (data[IFLA_GRE_TOS]) 1178c19e654dSHerbert Xu parms->iph.tos = nla_get_u8(data[IFLA_GRE_TOS]); 1179c19e654dSHerbert Xu 118022a59be8SPhilip Prindeville if (!data[IFLA_GRE_PMTUDISC] || nla_get_u8(data[IFLA_GRE_PMTUDISC])) { 118122a59be8SPhilip Prindeville if (t->ignore_df) 118222a59be8SPhilip Prindeville return -EINVAL; 1183c19e654dSHerbert Xu parms->iph.frag_off = htons(IP_DF); 118422a59be8SPhilip Prindeville } 11852e15ea39SPravin B Shelar 11862e15ea39SPravin B Shelar if (data[IFLA_GRE_COLLECT_METADATA]) { 11872e15ea39SPravin B Shelar t->collect_md = true; 1188e271c7b4SJiri Benc if (dev->type == ARPHRD_IPGRE) 1189e271c7b4SJiri Benc dev->type = ARPHRD_NONE; 11902e15ea39SPravin B Shelar } 119122a59be8SPhilip Prindeville 119222a59be8SPhilip Prindeville if (data[IFLA_GRE_IGNORE_DF]) { 119322a59be8SPhilip Prindeville if (nla_get_u8(data[IFLA_GRE_IGNORE_DF]) 119422a59be8SPhilip Prindeville && (parms->iph.frag_off & htons(IP_DF))) 119522a59be8SPhilip Prindeville return -EINVAL; 119622a59be8SPhilip Prindeville t->ignore_df = !!nla_get_u8(data[IFLA_GRE_IGNORE_DF]); 119722a59be8SPhilip Prindeville } 119822a59be8SPhilip Prindeville 11999830ad4cSCraig Gallek if (data[IFLA_GRE_FWMARK]) 12009830ad4cSCraig Gallek *fwmark = nla_get_u32(data[IFLA_GRE_FWMARK]); 12019830ad4cSCraig Gallek 1202f551c91dSWilliam Tu if (data[IFLA_GRE_ERSPAN_VER]) { 1203f551c91dSWilliam Tu t->erspan_ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]); 1204f551c91dSWilliam Tu 1205f551c91dSWilliam Tu if (t->erspan_ver != 1 && t->erspan_ver != 2) 1206f551c91dSWilliam Tu return -EINVAL; 1207f551c91dSWilliam Tu } 1208f551c91dSWilliam Tu 1209f551c91dSWilliam Tu if (t->erspan_ver == 1) { 121084e54fe0SWilliam Tu if (data[IFLA_GRE_ERSPAN_INDEX]) { 121184e54fe0SWilliam Tu t->index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]); 121284e54fe0SWilliam Tu if (t->index & ~INDEX_MASK) 121384e54fe0SWilliam Tu return -EINVAL; 121484e54fe0SWilliam Tu } 1215f551c91dSWilliam Tu } else if (t->erspan_ver == 2) { 1216f551c91dSWilliam Tu if (data[IFLA_GRE_ERSPAN_DIR]) { 1217f551c91dSWilliam Tu t->dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]); 1218f551c91dSWilliam Tu if (t->dir & ~(DIR_MASK >> DIR_OFFSET)) 1219f551c91dSWilliam Tu return -EINVAL; 1220f551c91dSWilliam Tu } 1221f551c91dSWilliam Tu if (data[IFLA_GRE_ERSPAN_HWID]) { 1222f551c91dSWilliam Tu t->hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]); 1223f551c91dSWilliam Tu if (t->hwid & ~(HWID_MASK >> HWID_OFFSET)) 1224f551c91dSWilliam Tu return -EINVAL; 1225f551c91dSWilliam Tu } 1226f551c91dSWilliam Tu } 122784e54fe0SWilliam Tu 122822a59be8SPhilip Prindeville return 0; 1229c19e654dSHerbert Xu } 1230c19e654dSHerbert Xu 12314565e991STom Herbert /* This function returns true when ENCAP attributes are present in the nl msg */ 12324565e991STom Herbert static bool ipgre_netlink_encap_parms(struct nlattr *data[], 12334565e991STom Herbert struct ip_tunnel_encap *ipencap) 12344565e991STom Herbert { 12354565e991STom Herbert bool ret = false; 12364565e991STom Herbert 12374565e991STom Herbert memset(ipencap, 0, sizeof(*ipencap)); 12384565e991STom Herbert 12394565e991STom Herbert if (!data) 12404565e991STom Herbert return ret; 12414565e991STom Herbert 12424565e991STom Herbert if (data[IFLA_GRE_ENCAP_TYPE]) { 12434565e991STom Herbert ret = true; 12444565e991STom Herbert ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]); 12454565e991STom Herbert } 12464565e991STom Herbert 12474565e991STom Herbert if (data[IFLA_GRE_ENCAP_FLAGS]) { 12484565e991STom Herbert ret = true; 12494565e991STom Herbert ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]); 12504565e991STom Herbert } 12514565e991STom Herbert 12524565e991STom Herbert if (data[IFLA_GRE_ENCAP_SPORT]) { 12534565e991STom Herbert ret = true; 12543e97fa70SSabrina Dubroca ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]); 12554565e991STom Herbert } 12564565e991STom Herbert 12574565e991STom Herbert if (data[IFLA_GRE_ENCAP_DPORT]) { 12584565e991STom Herbert ret = true; 12593e97fa70SSabrina Dubroca ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]); 12604565e991STom Herbert } 12614565e991STom Herbert 12624565e991STom Herbert return ret; 12634565e991STom Herbert } 12644565e991STom Herbert 1265c5441932SPravin B Shelar static int gre_tap_init(struct net_device *dev) 1266e1a80002SHerbert Xu { 1267c5441932SPravin B Shelar __gre_tunnel_init(dev); 1268bec94d43Sstephen hemminger dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; 1269d51711c0SXin Long netif_keep_dst(dev); 1270e1a80002SHerbert Xu 1271c5441932SPravin B Shelar return ip_tunnel_init(dev); 1272e1a80002SHerbert Xu } 1273e1a80002SHerbert Xu 1274c5441932SPravin B Shelar static const struct net_device_ops gre_tap_netdev_ops = { 1275c5441932SPravin B Shelar .ndo_init = gre_tap_init, 1276c5441932SPravin B Shelar .ndo_uninit = ip_tunnel_uninit, 1277c5441932SPravin B Shelar .ndo_start_xmit = gre_tap_xmit, 1278b8c26a33SStephen Hemminger .ndo_set_mac_address = eth_mac_addr, 1279b8c26a33SStephen Hemminger .ndo_validate_addr = eth_validate_addr, 1280c5441932SPravin B Shelar .ndo_change_mtu = ip_tunnel_change_mtu, 1281c5441932SPravin B Shelar .ndo_get_stats64 = ip_tunnel_get_stats64, 12821e99584bSNicolas Dichtel .ndo_get_iflink = ip_tunnel_get_iflink, 1283fc4099f1SPravin B Shelar .ndo_fill_metadata_dst = gre_fill_metadata_dst, 1284b8c26a33SStephen Hemminger }; 1285b8c26a33SStephen Hemminger 128684e54fe0SWilliam Tu static int erspan_tunnel_init(struct net_device *dev) 128784e54fe0SWilliam Tu { 128884e54fe0SWilliam Tu struct ip_tunnel *tunnel = netdev_priv(dev); 128984e54fe0SWilliam Tu int t_hlen; 129084e54fe0SWilliam Tu 129184e54fe0SWilliam Tu tunnel->tun_hlen = 8; 129284e54fe0SWilliam Tu tunnel->parms.iph.protocol = IPPROTO_GRE; 1293c122fda2SXin Long tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen + 1294f551c91dSWilliam Tu erspan_hdr_len(tunnel->erspan_ver); 1295c122fda2SXin Long t_hlen = tunnel->hlen + sizeof(struct iphdr); 129684e54fe0SWilliam Tu 129784e54fe0SWilliam Tu dev->needed_headroom = LL_MAX_HEADER + t_hlen + 4; 129884e54fe0SWilliam Tu dev->mtu = ETH_DATA_LEN - t_hlen - 4; 129984e54fe0SWilliam Tu dev->features |= GRE_FEATURES; 130084e54fe0SWilliam Tu dev->hw_features |= GRE_FEATURES; 130184e54fe0SWilliam Tu dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; 1302c84bed44SXin Long netif_keep_dst(dev); 130384e54fe0SWilliam Tu 130484e54fe0SWilliam Tu return ip_tunnel_init(dev); 130584e54fe0SWilliam Tu } 130684e54fe0SWilliam Tu 130784e54fe0SWilliam Tu static const struct net_device_ops erspan_netdev_ops = { 130884e54fe0SWilliam Tu .ndo_init = erspan_tunnel_init, 130984e54fe0SWilliam Tu .ndo_uninit = ip_tunnel_uninit, 131084e54fe0SWilliam Tu .ndo_start_xmit = erspan_xmit, 131184e54fe0SWilliam Tu .ndo_set_mac_address = eth_mac_addr, 131284e54fe0SWilliam Tu .ndo_validate_addr = eth_validate_addr, 131384e54fe0SWilliam Tu .ndo_change_mtu = ip_tunnel_change_mtu, 131484e54fe0SWilliam Tu .ndo_get_stats64 = ip_tunnel_get_stats64, 131584e54fe0SWilliam Tu .ndo_get_iflink = ip_tunnel_get_iflink, 131684e54fe0SWilliam Tu .ndo_fill_metadata_dst = gre_fill_metadata_dst, 131784e54fe0SWilliam Tu }; 131884e54fe0SWilliam Tu 1319e1a80002SHerbert Xu static void ipgre_tap_setup(struct net_device *dev) 1320e1a80002SHerbert Xu { 1321e1a80002SHerbert Xu ether_setup(dev); 1322cfddd4c3SXin Long dev->max_mtu = 0; 1323c5441932SPravin B Shelar dev->netdev_ops = &gre_tap_netdev_ops; 1324d13b161cSJiri Benc dev->priv_flags &= ~IFF_TX_SKB_SHARING; 1325f8c1b7ceSstephen hemminger dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; 1326c5441932SPravin B Shelar ip_tunnel_setup(dev, gre_tap_net_id); 1327e1a80002SHerbert Xu } 1328e1a80002SHerbert Xu 1329d1b2a6c4SPetr Machata bool is_gretap_dev(const struct net_device *dev) 1330d1b2a6c4SPetr Machata { 1331d1b2a6c4SPetr Machata return dev->netdev_ops == &gre_tap_netdev_ops; 1332d1b2a6c4SPetr Machata } 1333d1b2a6c4SPetr Machata EXPORT_SYMBOL_GPL(is_gretap_dev); 1334d1b2a6c4SPetr Machata 1335c5441932SPravin B Shelar static int ipgre_newlink(struct net *src_net, struct net_device *dev, 13367a3f4a18SMatthias Schiffer struct nlattr *tb[], struct nlattr *data[], 13377a3f4a18SMatthias Schiffer struct netlink_ext_ack *extack) 1338c19e654dSHerbert Xu { 1339c5441932SPravin B Shelar struct ip_tunnel_parm p; 13404565e991STom Herbert struct ip_tunnel_encap ipencap; 13419830ad4cSCraig Gallek __u32 fwmark = 0; 134222a59be8SPhilip Prindeville int err; 13434565e991STom Herbert 13444565e991STom Herbert if (ipgre_netlink_encap_parms(data, &ipencap)) { 13454565e991STom Herbert struct ip_tunnel *t = netdev_priv(dev); 134622a59be8SPhilip Prindeville err = ip_tunnel_encap_setup(t, &ipencap); 13474565e991STom Herbert 13484565e991STom Herbert if (err < 0) 13494565e991STom Herbert return err; 13504565e991STom Herbert } 1351c19e654dSHerbert Xu 13529830ad4cSCraig Gallek err = ipgre_netlink_parms(dev, data, tb, &p, &fwmark); 135322a59be8SPhilip Prindeville if (err < 0) 135422a59be8SPhilip Prindeville return err; 13559830ad4cSCraig Gallek return ip_tunnel_newlink(dev, tb, &p, fwmark); 1356c19e654dSHerbert Xu } 1357c19e654dSHerbert Xu 1358c19e654dSHerbert Xu static int ipgre_changelink(struct net_device *dev, struct nlattr *tb[], 1359ad744b22SMatthias Schiffer struct nlattr *data[], 1360ad744b22SMatthias Schiffer struct netlink_ext_ack *extack) 1361c19e654dSHerbert Xu { 13629830ad4cSCraig Gallek struct ip_tunnel *t = netdev_priv(dev); 13634565e991STom Herbert struct ip_tunnel_encap ipencap; 13649830ad4cSCraig Gallek __u32 fwmark = t->fwmark; 1365dd9d598cSXin Long struct ip_tunnel_parm p; 136622a59be8SPhilip Prindeville int err; 13674565e991STom Herbert 13684565e991STom Herbert if (ipgre_netlink_encap_parms(data, &ipencap)) { 136922a59be8SPhilip Prindeville err = ip_tunnel_encap_setup(t, &ipencap); 13704565e991STom Herbert 13714565e991STom Herbert if (err < 0) 13724565e991STom Herbert return err; 13734565e991STom Herbert } 1374c19e654dSHerbert Xu 13759830ad4cSCraig Gallek err = ipgre_netlink_parms(dev, data, tb, &p, &fwmark); 137622a59be8SPhilip Prindeville if (err < 0) 137722a59be8SPhilip Prindeville return err; 1378dd9d598cSXin Long 1379dd9d598cSXin Long err = ip_tunnel_changelink(dev, tb, &p, fwmark); 1380dd9d598cSXin Long if (err < 0) 1381dd9d598cSXin Long return err; 1382dd9d598cSXin Long 1383dd9d598cSXin Long t->parms.i_flags = p.i_flags; 1384dd9d598cSXin Long t->parms.o_flags = p.o_flags; 1385dd9d598cSXin Long 1386dd9d598cSXin Long if (strcmp(dev->rtnl_link_ops->kind, "erspan")) 1387dd9d598cSXin Long ipgre_link_update(dev, !tb[IFLA_MTU]); 1388dd9d598cSXin Long 1389dd9d598cSXin Long return 0; 1390c19e654dSHerbert Xu } 1391c19e654dSHerbert Xu 1392c19e654dSHerbert Xu static size_t ipgre_get_size(const struct net_device *dev) 1393c19e654dSHerbert Xu { 1394c19e654dSHerbert Xu return 1395c19e654dSHerbert Xu /* IFLA_GRE_LINK */ 1396c19e654dSHerbert Xu nla_total_size(4) + 1397c19e654dSHerbert Xu /* IFLA_GRE_IFLAGS */ 1398c19e654dSHerbert Xu nla_total_size(2) + 1399c19e654dSHerbert Xu /* IFLA_GRE_OFLAGS */ 1400c19e654dSHerbert Xu nla_total_size(2) + 1401c19e654dSHerbert Xu /* IFLA_GRE_IKEY */ 1402c19e654dSHerbert Xu nla_total_size(4) + 1403c19e654dSHerbert Xu /* IFLA_GRE_OKEY */ 1404c19e654dSHerbert Xu nla_total_size(4) + 1405c19e654dSHerbert Xu /* IFLA_GRE_LOCAL */ 1406c19e654dSHerbert Xu nla_total_size(4) + 1407c19e654dSHerbert Xu /* IFLA_GRE_REMOTE */ 1408c19e654dSHerbert Xu nla_total_size(4) + 1409c19e654dSHerbert Xu /* IFLA_GRE_TTL */ 1410c19e654dSHerbert Xu nla_total_size(1) + 1411c19e654dSHerbert Xu /* IFLA_GRE_TOS */ 1412c19e654dSHerbert Xu nla_total_size(1) + 1413c19e654dSHerbert Xu /* IFLA_GRE_PMTUDISC */ 1414c19e654dSHerbert Xu nla_total_size(1) + 14154565e991STom Herbert /* IFLA_GRE_ENCAP_TYPE */ 14164565e991STom Herbert nla_total_size(2) + 14174565e991STom Herbert /* IFLA_GRE_ENCAP_FLAGS */ 14184565e991STom Herbert nla_total_size(2) + 14194565e991STom Herbert /* IFLA_GRE_ENCAP_SPORT */ 14204565e991STom Herbert nla_total_size(2) + 14214565e991STom Herbert /* IFLA_GRE_ENCAP_DPORT */ 14224565e991STom Herbert nla_total_size(2) + 14232e15ea39SPravin B Shelar /* IFLA_GRE_COLLECT_METADATA */ 14242e15ea39SPravin B Shelar nla_total_size(0) + 142522a59be8SPhilip Prindeville /* IFLA_GRE_IGNORE_DF */ 142622a59be8SPhilip Prindeville nla_total_size(1) + 14279830ad4cSCraig Gallek /* IFLA_GRE_FWMARK */ 14289830ad4cSCraig Gallek nla_total_size(4) + 142984e54fe0SWilliam Tu /* IFLA_GRE_ERSPAN_INDEX */ 143084e54fe0SWilliam Tu nla_total_size(4) + 1431f551c91dSWilliam Tu /* IFLA_GRE_ERSPAN_VER */ 1432f551c91dSWilliam Tu nla_total_size(1) + 1433f551c91dSWilliam Tu /* IFLA_GRE_ERSPAN_DIR */ 1434f551c91dSWilliam Tu nla_total_size(1) + 1435f551c91dSWilliam Tu /* IFLA_GRE_ERSPAN_HWID */ 1436f551c91dSWilliam Tu nla_total_size(2) + 1437c19e654dSHerbert Xu 0; 1438c19e654dSHerbert Xu } 1439c19e654dSHerbert Xu 1440c19e654dSHerbert Xu static int ipgre_fill_info(struct sk_buff *skb, const struct net_device *dev) 1441c19e654dSHerbert Xu { 1442c19e654dSHerbert Xu struct ip_tunnel *t = netdev_priv(dev); 1443c19e654dSHerbert Xu struct ip_tunnel_parm *p = &t->parms; 1444c19e654dSHerbert Xu 1445f3756b79SDavid S. Miller if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) || 144695f5c64cSTom Herbert nla_put_be16(skb, IFLA_GRE_IFLAGS, 144795f5c64cSTom Herbert gre_tnl_flags_to_gre_flags(p->i_flags)) || 144895f5c64cSTom Herbert nla_put_be16(skb, IFLA_GRE_OFLAGS, 144995f5c64cSTom Herbert gre_tnl_flags_to_gre_flags(p->o_flags)) || 1450f3756b79SDavid S. Miller nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) || 1451f3756b79SDavid S. Miller nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) || 1452930345eaSJiri Benc nla_put_in_addr(skb, IFLA_GRE_LOCAL, p->iph.saddr) || 1453930345eaSJiri Benc nla_put_in_addr(skb, IFLA_GRE_REMOTE, p->iph.daddr) || 1454f3756b79SDavid S. Miller nla_put_u8(skb, IFLA_GRE_TTL, p->iph.ttl) || 1455f3756b79SDavid S. Miller nla_put_u8(skb, IFLA_GRE_TOS, p->iph.tos) || 1456f3756b79SDavid S. Miller nla_put_u8(skb, IFLA_GRE_PMTUDISC, 14579830ad4cSCraig Gallek !!(p->iph.frag_off & htons(IP_DF))) || 14589830ad4cSCraig Gallek nla_put_u32(skb, IFLA_GRE_FWMARK, t->fwmark)) 1459f3756b79SDavid S. Miller goto nla_put_failure; 14604565e991STom Herbert 14614565e991STom Herbert if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE, 14624565e991STom Herbert t->encap.type) || 14633e97fa70SSabrina Dubroca nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT, 14644565e991STom Herbert t->encap.sport) || 14653e97fa70SSabrina Dubroca nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT, 14664565e991STom Herbert t->encap.dport) || 14674565e991STom Herbert nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS, 1468e1b2cb65STom Herbert t->encap.flags)) 14694565e991STom Herbert goto nla_put_failure; 14704565e991STom Herbert 147122a59be8SPhilip Prindeville if (nla_put_u8(skb, IFLA_GRE_IGNORE_DF, t->ignore_df)) 147222a59be8SPhilip Prindeville goto nla_put_failure; 147322a59be8SPhilip Prindeville 14742e15ea39SPravin B Shelar if (t->collect_md) { 14752e15ea39SPravin B Shelar if (nla_put_flag(skb, IFLA_GRE_COLLECT_METADATA)) 14762e15ea39SPravin B Shelar goto nla_put_failure; 14772e15ea39SPravin B Shelar } 14782e15ea39SPravin B Shelar 1479f551c91dSWilliam Tu if (nla_put_u8(skb, IFLA_GRE_ERSPAN_VER, t->erspan_ver)) 1480f551c91dSWilliam Tu goto nla_put_failure; 1481f551c91dSWilliam Tu 1482f551c91dSWilliam Tu if (t->erspan_ver == 1) { 148384e54fe0SWilliam Tu if (nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, t->index)) 148484e54fe0SWilliam Tu goto nla_put_failure; 1485f551c91dSWilliam Tu } else if (t->erspan_ver == 2) { 1486f551c91dSWilliam Tu if (nla_put_u8(skb, IFLA_GRE_ERSPAN_DIR, t->dir)) 1487f551c91dSWilliam Tu goto nla_put_failure; 1488f551c91dSWilliam Tu if (nla_put_u16(skb, IFLA_GRE_ERSPAN_HWID, t->hwid)) 1489f551c91dSWilliam Tu goto nla_put_failure; 1490f551c91dSWilliam Tu } 149184e54fe0SWilliam Tu 1492c19e654dSHerbert Xu return 0; 1493c19e654dSHerbert Xu 1494c19e654dSHerbert Xu nla_put_failure: 1495c19e654dSHerbert Xu return -EMSGSIZE; 1496c19e654dSHerbert Xu } 1497c19e654dSHerbert Xu 149884e54fe0SWilliam Tu static void erspan_setup(struct net_device *dev) 149984e54fe0SWilliam Tu { 150084e54fe0SWilliam Tu ether_setup(dev); 150184e54fe0SWilliam Tu dev->netdev_ops = &erspan_netdev_ops; 150284e54fe0SWilliam Tu dev->priv_flags &= ~IFF_TX_SKB_SHARING; 150384e54fe0SWilliam Tu dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; 150484e54fe0SWilliam Tu ip_tunnel_setup(dev, erspan_net_id); 150584e54fe0SWilliam Tu } 150684e54fe0SWilliam Tu 1507c19e654dSHerbert Xu static const struct nla_policy ipgre_policy[IFLA_GRE_MAX + 1] = { 1508c19e654dSHerbert Xu [IFLA_GRE_LINK] = { .type = NLA_U32 }, 1509c19e654dSHerbert Xu [IFLA_GRE_IFLAGS] = { .type = NLA_U16 }, 1510c19e654dSHerbert Xu [IFLA_GRE_OFLAGS] = { .type = NLA_U16 }, 1511c19e654dSHerbert Xu [IFLA_GRE_IKEY] = { .type = NLA_U32 }, 1512c19e654dSHerbert Xu [IFLA_GRE_OKEY] = { .type = NLA_U32 }, 15134d74f8baSPatrick McHardy [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) }, 15144d74f8baSPatrick McHardy [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) }, 1515c19e654dSHerbert Xu [IFLA_GRE_TTL] = { .type = NLA_U8 }, 1516c19e654dSHerbert Xu [IFLA_GRE_TOS] = { .type = NLA_U8 }, 1517c19e654dSHerbert Xu [IFLA_GRE_PMTUDISC] = { .type = NLA_U8 }, 15184565e991STom Herbert [IFLA_GRE_ENCAP_TYPE] = { .type = NLA_U16 }, 15194565e991STom Herbert [IFLA_GRE_ENCAP_FLAGS] = { .type = NLA_U16 }, 15204565e991STom Herbert [IFLA_GRE_ENCAP_SPORT] = { .type = NLA_U16 }, 15214565e991STom Herbert [IFLA_GRE_ENCAP_DPORT] = { .type = NLA_U16 }, 15222e15ea39SPravin B Shelar [IFLA_GRE_COLLECT_METADATA] = { .type = NLA_FLAG }, 152322a59be8SPhilip Prindeville [IFLA_GRE_IGNORE_DF] = { .type = NLA_U8 }, 15249830ad4cSCraig Gallek [IFLA_GRE_FWMARK] = { .type = NLA_U32 }, 152584e54fe0SWilliam Tu [IFLA_GRE_ERSPAN_INDEX] = { .type = NLA_U32 }, 1526f551c91dSWilliam Tu [IFLA_GRE_ERSPAN_VER] = { .type = NLA_U8 }, 1527f551c91dSWilliam Tu [IFLA_GRE_ERSPAN_DIR] = { .type = NLA_U8 }, 1528f551c91dSWilliam Tu [IFLA_GRE_ERSPAN_HWID] = { .type = NLA_U16 }, 1529c19e654dSHerbert Xu }; 1530c19e654dSHerbert Xu 1531c19e654dSHerbert Xu static struct rtnl_link_ops ipgre_link_ops __read_mostly = { 1532c19e654dSHerbert Xu .kind = "gre", 1533c19e654dSHerbert Xu .maxtype = IFLA_GRE_MAX, 1534c19e654dSHerbert Xu .policy = ipgre_policy, 1535c19e654dSHerbert Xu .priv_size = sizeof(struct ip_tunnel), 1536c19e654dSHerbert Xu .setup = ipgre_tunnel_setup, 1537c19e654dSHerbert Xu .validate = ipgre_tunnel_validate, 1538c19e654dSHerbert Xu .newlink = ipgre_newlink, 1539c19e654dSHerbert Xu .changelink = ipgre_changelink, 1540c5441932SPravin B Shelar .dellink = ip_tunnel_dellink, 1541c19e654dSHerbert Xu .get_size = ipgre_get_size, 1542c19e654dSHerbert Xu .fill_info = ipgre_fill_info, 15431728d4faSNicolas Dichtel .get_link_net = ip_tunnel_get_link_net, 1544c19e654dSHerbert Xu }; 1545c19e654dSHerbert Xu 1546e1a80002SHerbert Xu static struct rtnl_link_ops ipgre_tap_ops __read_mostly = { 1547e1a80002SHerbert Xu .kind = "gretap", 1548e1a80002SHerbert Xu .maxtype = IFLA_GRE_MAX, 1549e1a80002SHerbert Xu .policy = ipgre_policy, 1550e1a80002SHerbert Xu .priv_size = sizeof(struct ip_tunnel), 1551e1a80002SHerbert Xu .setup = ipgre_tap_setup, 1552e1a80002SHerbert Xu .validate = ipgre_tap_validate, 1553e1a80002SHerbert Xu .newlink = ipgre_newlink, 1554e1a80002SHerbert Xu .changelink = ipgre_changelink, 1555c5441932SPravin B Shelar .dellink = ip_tunnel_dellink, 1556e1a80002SHerbert Xu .get_size = ipgre_get_size, 1557e1a80002SHerbert Xu .fill_info = ipgre_fill_info, 15581728d4faSNicolas Dichtel .get_link_net = ip_tunnel_get_link_net, 1559e1a80002SHerbert Xu }; 1560e1a80002SHerbert Xu 156184e54fe0SWilliam Tu static struct rtnl_link_ops erspan_link_ops __read_mostly = { 156284e54fe0SWilliam Tu .kind = "erspan", 156384e54fe0SWilliam Tu .maxtype = IFLA_GRE_MAX, 156484e54fe0SWilliam Tu .policy = ipgre_policy, 156584e54fe0SWilliam Tu .priv_size = sizeof(struct ip_tunnel), 156684e54fe0SWilliam Tu .setup = erspan_setup, 156784e54fe0SWilliam Tu .validate = erspan_validate, 156884e54fe0SWilliam Tu .newlink = ipgre_newlink, 156984e54fe0SWilliam Tu .changelink = ipgre_changelink, 157084e54fe0SWilliam Tu .dellink = ip_tunnel_dellink, 157184e54fe0SWilliam Tu .get_size = ipgre_get_size, 157284e54fe0SWilliam Tu .fill_info = ipgre_fill_info, 157384e54fe0SWilliam Tu .get_link_net = ip_tunnel_get_link_net, 157484e54fe0SWilliam Tu }; 157584e54fe0SWilliam Tu 1576b2acd1dcSPravin B Shelar struct net_device *gretap_fb_dev_create(struct net *net, const char *name, 1577b2acd1dcSPravin B Shelar u8 name_assign_type) 1578b2acd1dcSPravin B Shelar { 1579b2acd1dcSPravin B Shelar struct nlattr *tb[IFLA_MAX + 1]; 1580b2acd1dcSPravin B Shelar struct net_device *dev; 1581106da663SNicolas Dichtel LIST_HEAD(list_kill); 1582b2acd1dcSPravin B Shelar struct ip_tunnel *t; 1583b2acd1dcSPravin B Shelar int err; 1584b2acd1dcSPravin B Shelar 1585b2acd1dcSPravin B Shelar memset(&tb, 0, sizeof(tb)); 1586b2acd1dcSPravin B Shelar 1587b2acd1dcSPravin B Shelar dev = rtnl_create_link(net, name, name_assign_type, 1588b2acd1dcSPravin B Shelar &ipgre_tap_ops, tb); 1589b2acd1dcSPravin B Shelar if (IS_ERR(dev)) 1590b2acd1dcSPravin B Shelar return dev; 1591b2acd1dcSPravin B Shelar 1592b2acd1dcSPravin B Shelar /* Configure flow based GRE device. */ 1593b2acd1dcSPravin B Shelar t = netdev_priv(dev); 1594b2acd1dcSPravin B Shelar t->collect_md = true; 1595b2acd1dcSPravin B Shelar 15967a3f4a18SMatthias Schiffer err = ipgre_newlink(net, dev, tb, NULL, NULL); 1597106da663SNicolas Dichtel if (err < 0) { 1598106da663SNicolas Dichtel free_netdev(dev); 1599106da663SNicolas Dichtel return ERR_PTR(err); 1600106da663SNicolas Dichtel } 16017e059158SDavid Wragg 16027e059158SDavid Wragg /* openvswitch users expect packet sizes to be unrestricted, 16037e059158SDavid Wragg * so set the largest MTU we can. 16047e059158SDavid Wragg */ 16057e059158SDavid Wragg err = __ip_tunnel_change_mtu(dev, IP_MAX_MTU, false); 16067e059158SDavid Wragg if (err) 16077e059158SDavid Wragg goto out; 16087e059158SDavid Wragg 1609da6f1da8SNicolas Dichtel err = rtnl_configure_link(dev, NULL); 1610da6f1da8SNicolas Dichtel if (err < 0) 1611da6f1da8SNicolas Dichtel goto out; 1612da6f1da8SNicolas Dichtel 1613b2acd1dcSPravin B Shelar return dev; 1614b2acd1dcSPravin B Shelar out: 1615106da663SNicolas Dichtel ip_tunnel_dellink(dev, &list_kill); 1616106da663SNicolas Dichtel unregister_netdevice_many(&list_kill); 1617b2acd1dcSPravin B Shelar return ERR_PTR(err); 1618b2acd1dcSPravin B Shelar } 1619b2acd1dcSPravin B Shelar EXPORT_SYMBOL_GPL(gretap_fb_dev_create); 1620b2acd1dcSPravin B Shelar 1621c5441932SPravin B Shelar static int __net_init ipgre_tap_init_net(struct net *net) 1622c5441932SPravin B Shelar { 16232e15ea39SPravin B Shelar return ip_tunnel_init_net(net, gre_tap_net_id, &ipgre_tap_ops, "gretap0"); 1624c5441932SPravin B Shelar } 1625c5441932SPravin B Shelar 162664bc1781SEric Dumazet static void __net_exit ipgre_tap_exit_batch_net(struct list_head *list_net) 1627c5441932SPravin B Shelar { 162864bc1781SEric Dumazet ip_tunnel_delete_nets(list_net, gre_tap_net_id, &ipgre_tap_ops); 1629c5441932SPravin B Shelar } 1630c5441932SPravin B Shelar 1631c5441932SPravin B Shelar static struct pernet_operations ipgre_tap_net_ops = { 1632c5441932SPravin B Shelar .init = ipgre_tap_init_net, 163364bc1781SEric Dumazet .exit_batch = ipgre_tap_exit_batch_net, 1634c5441932SPravin B Shelar .id = &gre_tap_net_id, 1635c5441932SPravin B Shelar .size = sizeof(struct ip_tunnel_net), 163631502104SKirill Tkhai .async = true, 1637c5441932SPravin B Shelar }; 16381da177e4SLinus Torvalds 163984e54fe0SWilliam Tu static int __net_init erspan_init_net(struct net *net) 164084e54fe0SWilliam Tu { 164184e54fe0SWilliam Tu return ip_tunnel_init_net(net, erspan_net_id, 164284e54fe0SWilliam Tu &erspan_link_ops, "erspan0"); 164384e54fe0SWilliam Tu } 164484e54fe0SWilliam Tu 164564bc1781SEric Dumazet static void __net_exit erspan_exit_batch_net(struct list_head *net_list) 164684e54fe0SWilliam Tu { 164764bc1781SEric Dumazet ip_tunnel_delete_nets(net_list, erspan_net_id, &erspan_link_ops); 164884e54fe0SWilliam Tu } 164984e54fe0SWilliam Tu 165084e54fe0SWilliam Tu static struct pernet_operations erspan_net_ops = { 165184e54fe0SWilliam Tu .init = erspan_init_net, 165264bc1781SEric Dumazet .exit_batch = erspan_exit_batch_net, 165384e54fe0SWilliam Tu .id = &erspan_net_id, 165484e54fe0SWilliam Tu .size = sizeof(struct ip_tunnel_net), 165531502104SKirill Tkhai .async = true, 165684e54fe0SWilliam Tu }; 165784e54fe0SWilliam Tu 16581da177e4SLinus Torvalds static int __init ipgre_init(void) 16591da177e4SLinus Torvalds { 16601da177e4SLinus Torvalds int err; 16611da177e4SLinus Torvalds 1662058bd4d2SJoe Perches pr_info("GRE over IPv4 tunneling driver\n"); 16631da177e4SLinus Torvalds 1664cfb8fbf2SEric W. Biederman err = register_pernet_device(&ipgre_net_ops); 166559a4c759SPavel Emelyanov if (err < 0) 1666c2892f02SAlexey Dobriyan return err; 1667c2892f02SAlexey Dobriyan 1668c5441932SPravin B Shelar err = register_pernet_device(&ipgre_tap_net_ops); 1669c5441932SPravin B Shelar if (err < 0) 1670e3d0328cSWilliam Tu goto pnet_tap_failed; 1671c5441932SPravin B Shelar 167284e54fe0SWilliam Tu err = register_pernet_device(&erspan_net_ops); 167384e54fe0SWilliam Tu if (err < 0) 167484e54fe0SWilliam Tu goto pnet_erspan_failed; 167584e54fe0SWilliam Tu 16769f57c67cSPravin B Shelar err = gre_add_protocol(&ipgre_protocol, GREPROTO_CISCO); 1677c2892f02SAlexey Dobriyan if (err < 0) { 1678058bd4d2SJoe Perches pr_info("%s: can't add protocol\n", __func__); 1679c2892f02SAlexey Dobriyan goto add_proto_failed; 1680c2892f02SAlexey Dobriyan } 16817daa0004SPavel Emelyanov 1682c19e654dSHerbert Xu err = rtnl_link_register(&ipgre_link_ops); 1683c19e654dSHerbert Xu if (err < 0) 1684c19e654dSHerbert Xu goto rtnl_link_failed; 1685c19e654dSHerbert Xu 1686e1a80002SHerbert Xu err = rtnl_link_register(&ipgre_tap_ops); 1687e1a80002SHerbert Xu if (err < 0) 1688e1a80002SHerbert Xu goto tap_ops_failed; 1689e1a80002SHerbert Xu 169084e54fe0SWilliam Tu err = rtnl_link_register(&erspan_link_ops); 169184e54fe0SWilliam Tu if (err < 0) 169284e54fe0SWilliam Tu goto erspan_link_failed; 169384e54fe0SWilliam Tu 1694c5441932SPravin B Shelar return 0; 1695c19e654dSHerbert Xu 169684e54fe0SWilliam Tu erspan_link_failed: 169784e54fe0SWilliam Tu rtnl_link_unregister(&ipgre_tap_ops); 1698e1a80002SHerbert Xu tap_ops_failed: 1699e1a80002SHerbert Xu rtnl_link_unregister(&ipgre_link_ops); 1700c19e654dSHerbert Xu rtnl_link_failed: 17019f57c67cSPravin B Shelar gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO); 1702c2892f02SAlexey Dobriyan add_proto_failed: 170384e54fe0SWilliam Tu unregister_pernet_device(&erspan_net_ops); 170484e54fe0SWilliam Tu pnet_erspan_failed: 1705c5441932SPravin B Shelar unregister_pernet_device(&ipgre_tap_net_ops); 1706e3d0328cSWilliam Tu pnet_tap_failed: 1707c2892f02SAlexey Dobriyan unregister_pernet_device(&ipgre_net_ops); 1708c5441932SPravin B Shelar return err; 17091da177e4SLinus Torvalds } 17101da177e4SLinus Torvalds 1711db44575fSAlexey Kuznetsov static void __exit ipgre_fini(void) 17121da177e4SLinus Torvalds { 1713e1a80002SHerbert Xu rtnl_link_unregister(&ipgre_tap_ops); 1714c19e654dSHerbert Xu rtnl_link_unregister(&ipgre_link_ops); 171584e54fe0SWilliam Tu rtnl_link_unregister(&erspan_link_ops); 17169f57c67cSPravin B Shelar gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO); 1717c5441932SPravin B Shelar unregister_pernet_device(&ipgre_tap_net_ops); 1718c2892f02SAlexey Dobriyan unregister_pernet_device(&ipgre_net_ops); 171984e54fe0SWilliam Tu unregister_pernet_device(&erspan_net_ops); 17201da177e4SLinus Torvalds } 17211da177e4SLinus Torvalds 17221da177e4SLinus Torvalds module_init(ipgre_init); 17231da177e4SLinus Torvalds module_exit(ipgre_fini); 17241da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 17254d74f8baSPatrick McHardy MODULE_ALIAS_RTNL_LINK("gre"); 17264d74f8baSPatrick McHardy MODULE_ALIAS_RTNL_LINK("gretap"); 172784e54fe0SWilliam Tu MODULE_ALIAS_RTNL_LINK("erspan"); 17288909c9adSVasiliy Kulikov MODULE_ALIAS_NETDEV("gre0"); 1729c5441932SPravin B Shelar MODULE_ALIAS_NETDEV("gretap0"); 173084e54fe0SWilliam Tu MODULE_ALIAS_NETDEV("erspan0"); 1731