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 134fc268d2SRandy Dunlap #include <linux/capability.h> 141da177e4SLinus Torvalds #include <linux/module.h> 151da177e4SLinus Torvalds #include <linux/types.h> 161da177e4SLinus Torvalds #include <linux/kernel.h> 175a0e3ad6STejun Heo #include <linux/slab.h> 181da177e4SLinus Torvalds #include <asm/uaccess.h> 191da177e4SLinus Torvalds #include <linux/skbuff.h> 201da177e4SLinus Torvalds #include <linux/netdevice.h> 211da177e4SLinus Torvalds #include <linux/in.h> 221da177e4SLinus Torvalds #include <linux/tcp.h> 231da177e4SLinus Torvalds #include <linux/udp.h> 241da177e4SLinus Torvalds #include <linux/if_arp.h> 251da177e4SLinus Torvalds #include <linux/mroute.h> 261da177e4SLinus Torvalds #include <linux/init.h> 271da177e4SLinus Torvalds #include <linux/in6.h> 281da177e4SLinus Torvalds #include <linux/inetdevice.h> 291da177e4SLinus Torvalds #include <linux/igmp.h> 301da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 31e1a80002SHerbert Xu #include <linux/etherdevice.h> 3246f25dffSKris Katterjohn #include <linux/if_ether.h> 331da177e4SLinus Torvalds 341da177e4SLinus Torvalds #include <net/sock.h> 351da177e4SLinus Torvalds #include <net/ip.h> 361da177e4SLinus Torvalds #include <net/icmp.h> 371da177e4SLinus Torvalds #include <net/protocol.h> 381da177e4SLinus Torvalds #include <net/ipip.h> 391da177e4SLinus Torvalds #include <net/arp.h> 401da177e4SLinus Torvalds #include <net/checksum.h> 411da177e4SLinus Torvalds #include <net/dsfield.h> 421da177e4SLinus Torvalds #include <net/inet_ecn.h> 431da177e4SLinus Torvalds #include <net/xfrm.h> 4459a4c759SPavel Emelyanov #include <net/net_namespace.h> 4559a4c759SPavel Emelyanov #include <net/netns/generic.h> 46c19e654dSHerbert Xu #include <net/rtnetlink.h> 4700959adeSDmitry Kozlov #include <net/gre.h> 481da177e4SLinus Torvalds 49842c74bfSEric Dumazet #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 501da177e4SLinus Torvalds #include <net/ipv6.h> 511da177e4SLinus Torvalds #include <net/ip6_fib.h> 521da177e4SLinus Torvalds #include <net/ip6_route.h> 531da177e4SLinus Torvalds #endif 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds /* 561da177e4SLinus Torvalds Problems & solutions 571da177e4SLinus Torvalds -------------------- 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds 1. The most important issue is detecting local dead loops. 601da177e4SLinus Torvalds They would cause complete host lockup in transmit, which 611da177e4SLinus Torvalds would be "resolved" by stack overflow or, if queueing is enabled, 621da177e4SLinus Torvalds with infinite looping in net_bh. 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds We cannot track such dead loops during route installation, 651da177e4SLinus Torvalds it is infeasible task. The most general solutions would be 661da177e4SLinus Torvalds to keep skb->encapsulation counter (sort of local ttl), 676d0722a2SEric Dumazet and silently drop packet when it expires. It is a good 681da177e4SLinus Torvalds solution, but it supposes maintaing new variable in ALL 691da177e4SLinus Torvalds skb, even if no tunneling is used. 701da177e4SLinus Torvalds 716d0722a2SEric Dumazet Current solution: xmit_recursion breaks dead loops. This is a percpu 726d0722a2SEric Dumazet counter, since when we enter the first ndo_xmit(), cpu migration is 736d0722a2SEric Dumazet forbidden. We force an exit if this counter reaches RECURSION_LIMIT 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds 2. Networking dead loops would not kill routers, but would really 761da177e4SLinus Torvalds kill network. IP hop limit plays role of "t->recursion" in this case, 771da177e4SLinus Torvalds if we copy it from packet being encapsulated to upper header. 781da177e4SLinus Torvalds It is very good solution, but it introduces two problems: 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds - Routing protocols, using packets with ttl=1 (OSPF, RIP2), 811da177e4SLinus Torvalds do not work over tunnels. 821da177e4SLinus Torvalds - traceroute does not work. I planned to relay ICMP from tunnel, 831da177e4SLinus Torvalds so that this problem would be solved and traceroute output 841da177e4SLinus Torvalds would even more informative. This idea appeared to be wrong: 851da177e4SLinus Torvalds only Linux complies to rfc1812 now (yes, guys, Linux is the only 861da177e4SLinus Torvalds true router now :-)), all routers (at least, in neighbourhood of mine) 871da177e4SLinus Torvalds return only 8 bytes of payload. It is the end. 881da177e4SLinus Torvalds 891da177e4SLinus Torvalds Hence, if we want that OSPF worked or traceroute said something reasonable, 901da177e4SLinus Torvalds we should search for another solution. 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds One of them is to parse packet trying to detect inner encapsulation 931da177e4SLinus Torvalds made by our node. It is difficult or even impossible, especially, 941da177e4SLinus Torvalds taking into account fragmentation. TO be short, tt is not solution at all. 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds Current solution: The solution was UNEXPECTEDLY SIMPLE. 971da177e4SLinus Torvalds We force DF flag on tunnels with preconfigured hop limit, 981da177e4SLinus Torvalds that is ALL. :-) Well, it does not remove the problem completely, 991da177e4SLinus Torvalds but exponential growth of network traffic is changed to linear 1001da177e4SLinus Torvalds (branches, that exceed pmtu are pruned) and tunnel mtu 1011da177e4SLinus Torvalds fastly degrades to value <68, where looping stops. 1021da177e4SLinus Torvalds Yes, it is not good if there exists a router in the loop, 1031da177e4SLinus Torvalds which does not force DF, even when encapsulating packets have DF set. 1041da177e4SLinus Torvalds But it is not our problem! Nobody could accuse us, we made 1051da177e4SLinus Torvalds all that we could make. Even if it is your gated who injected 1061da177e4SLinus Torvalds fatal route to network, even if it were you who configured 1071da177e4SLinus Torvalds fatal static route: you are innocent. :-) 1081da177e4SLinus Torvalds 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds 3. Really, ipv4/ipip.c, ipv4/ip_gre.c and ipv6/sit.c contain 1121da177e4SLinus Torvalds practically identical code. It would be good to glue them 1131da177e4SLinus Torvalds together, but it is not very evident, how to make them modular. 1141da177e4SLinus Torvalds sit is integral part of IPv6, ipip and gre are naturally modular. 1151da177e4SLinus Torvalds We could extract common parts (hash table, ioctl etc) 1161da177e4SLinus Torvalds to a separate module (ip_tunnel.c). 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds Alexey Kuznetsov. 1191da177e4SLinus Torvalds */ 1201da177e4SLinus Torvalds 121c19e654dSHerbert Xu static struct rtnl_link_ops ipgre_link_ops __read_mostly; 1221da177e4SLinus Torvalds static int ipgre_tunnel_init(struct net_device *dev); 1231da177e4SLinus Torvalds static void ipgre_tunnel_setup(struct net_device *dev); 12442aa9162SHerbert Xu static int ipgre_tunnel_bind_dev(struct net_device *dev); 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds /* Fallback tunnel: no source, no destination, no key, no options */ 1271da177e4SLinus Torvalds 128eb8ce741SPavel Emelyanov #define HASH_SIZE 16 129eb8ce741SPavel Emelyanov 130f99189b1SEric Dumazet static int ipgre_net_id __read_mostly; 13159a4c759SPavel Emelyanov struct ipgre_net { 1321507850bSEric Dumazet struct ip_tunnel __rcu *tunnels[4][HASH_SIZE]; 133eb8ce741SPavel Emelyanov 1347daa0004SPavel Emelyanov struct net_device *fb_tunnel_dev; 13559a4c759SPavel Emelyanov }; 13659a4c759SPavel Emelyanov 1371da177e4SLinus Torvalds /* Tunnel hash table */ 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds /* 1401da177e4SLinus Torvalds 4 hash tables: 1411da177e4SLinus Torvalds 1421da177e4SLinus Torvalds 3: (remote,local) 1431da177e4SLinus Torvalds 2: (remote,*) 1441da177e4SLinus Torvalds 1: (*,local) 1451da177e4SLinus Torvalds 0: (*,*) 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds We require exact key match i.e. if a key is present in packet 1481da177e4SLinus Torvalds it will match only tunnel with the same key; if it is not present, 1491da177e4SLinus Torvalds it will match only keyless tunnel. 1501da177e4SLinus Torvalds 1511da177e4SLinus Torvalds All keysless packets, if not matched configured keyless tunnels 1521da177e4SLinus Torvalds will match fallback tunnel. 1531da177e4SLinus Torvalds */ 1541da177e4SLinus Torvalds 155d5a0a1e3SAl Viro #define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&0xF) 1561da177e4SLinus Torvalds 157eb8ce741SPavel Emelyanov #define tunnels_r_l tunnels[3] 158eb8ce741SPavel Emelyanov #define tunnels_r tunnels[2] 159eb8ce741SPavel Emelyanov #define tunnels_l tunnels[1] 160eb8ce741SPavel Emelyanov #define tunnels_wc tunnels[0] 1618d5b2c08SEric Dumazet /* 1621507850bSEric Dumazet * Locking : hash tables are protected by RCU and RTNL 1638d5b2c08SEric Dumazet */ 1641da177e4SLinus Torvalds 1658d5b2c08SEric Dumazet #define for_each_ip_tunnel_rcu(start) \ 1668d5b2c08SEric Dumazet for (t = rcu_dereference(start); t; t = rcu_dereference(t->next)) 1671da177e4SLinus Torvalds 168e985aad7SEric Dumazet /* often modified stats are per cpu, other are shared (netdev->stats) */ 169e985aad7SEric Dumazet struct pcpu_tstats { 170e985aad7SEric Dumazet unsigned long rx_packets; 171e985aad7SEric Dumazet unsigned long rx_bytes; 172e985aad7SEric Dumazet unsigned long tx_packets; 173e985aad7SEric Dumazet unsigned long tx_bytes; 174e985aad7SEric Dumazet }; 175e985aad7SEric Dumazet 176e985aad7SEric Dumazet static struct net_device_stats *ipgre_get_stats(struct net_device *dev) 177e985aad7SEric Dumazet { 178e985aad7SEric Dumazet struct pcpu_tstats sum = { 0 }; 179e985aad7SEric Dumazet int i; 180e985aad7SEric Dumazet 181e985aad7SEric Dumazet for_each_possible_cpu(i) { 182e985aad7SEric Dumazet const struct pcpu_tstats *tstats = per_cpu_ptr(dev->tstats, i); 183e985aad7SEric Dumazet 184e985aad7SEric Dumazet sum.rx_packets += tstats->rx_packets; 185e985aad7SEric Dumazet sum.rx_bytes += tstats->rx_bytes; 186e985aad7SEric Dumazet sum.tx_packets += tstats->tx_packets; 187e985aad7SEric Dumazet sum.tx_bytes += tstats->tx_bytes; 188e985aad7SEric Dumazet } 189e985aad7SEric Dumazet dev->stats.rx_packets = sum.rx_packets; 190e985aad7SEric Dumazet dev->stats.rx_bytes = sum.rx_bytes; 191e985aad7SEric Dumazet dev->stats.tx_packets = sum.tx_packets; 192e985aad7SEric Dumazet dev->stats.tx_bytes = sum.tx_bytes; 193e985aad7SEric Dumazet return &dev->stats; 194e985aad7SEric Dumazet } 195e985aad7SEric Dumazet 1961da177e4SLinus Torvalds /* Given src, dst and key, find appropriate for input tunnel. */ 1971da177e4SLinus Torvalds 198749c10f9STimo Teras static struct ip_tunnel * ipgre_tunnel_lookup(struct net_device *dev, 199e1a80002SHerbert Xu __be32 remote, __be32 local, 200e1a80002SHerbert Xu __be32 key, __be16 gre_proto) 2011da177e4SLinus Torvalds { 202749c10f9STimo Teras struct net *net = dev_net(dev); 203749c10f9STimo Teras int link = dev->ifindex; 2041507850bSEric Dumazet unsigned int h0 = HASH(remote); 2051507850bSEric Dumazet unsigned int h1 = HASH(key); 206afcf1242STimo Teras struct ip_tunnel *t, *cand = NULL; 2077daa0004SPavel Emelyanov struct ipgre_net *ign = net_generic(net, ipgre_net_id); 208e1a80002SHerbert Xu int dev_type = (gre_proto == htons(ETH_P_TEB)) ? 209e1a80002SHerbert Xu ARPHRD_ETHER : ARPHRD_IPGRE; 210afcf1242STimo Teras int score, cand_score = 4; 2111da177e4SLinus Torvalds 2128d5b2c08SEric Dumazet for_each_ip_tunnel_rcu(ign->tunnels_r_l[h0 ^ h1]) { 213749c10f9STimo Teras if (local != t->parms.iph.saddr || 214749c10f9STimo Teras remote != t->parms.iph.daddr || 215749c10f9STimo Teras key != t->parms.i_key || 216749c10f9STimo Teras !(t->dev->flags & IFF_UP)) 217749c10f9STimo Teras continue; 218749c10f9STimo Teras 219749c10f9STimo Teras if (t->dev->type != ARPHRD_IPGRE && 220749c10f9STimo Teras t->dev->type != dev_type) 221749c10f9STimo Teras continue; 222749c10f9STimo Teras 223afcf1242STimo Teras score = 0; 224749c10f9STimo Teras if (t->parms.link != link) 225afcf1242STimo Teras score |= 1; 226749c10f9STimo Teras if (t->dev->type != dev_type) 227afcf1242STimo Teras score |= 2; 228afcf1242STimo Teras if (score == 0) 2291da177e4SLinus Torvalds return t; 230afcf1242STimo Teras 231afcf1242STimo Teras if (score < cand_score) { 232afcf1242STimo Teras cand = t; 233afcf1242STimo Teras cand_score = score; 234afcf1242STimo Teras } 235e1a80002SHerbert Xu } 236e1a80002SHerbert Xu 2378d5b2c08SEric Dumazet for_each_ip_tunnel_rcu(ign->tunnels_r[h0 ^ h1]) { 238749c10f9STimo Teras if (remote != t->parms.iph.daddr || 239749c10f9STimo Teras key != t->parms.i_key || 240749c10f9STimo Teras !(t->dev->flags & IFF_UP)) 241749c10f9STimo Teras continue; 242749c10f9STimo Teras 243749c10f9STimo Teras if (t->dev->type != ARPHRD_IPGRE && 244749c10f9STimo Teras t->dev->type != dev_type) 245749c10f9STimo Teras continue; 246749c10f9STimo Teras 247afcf1242STimo Teras score = 0; 248749c10f9STimo Teras if (t->parms.link != link) 249afcf1242STimo Teras score |= 1; 250749c10f9STimo Teras if (t->dev->type != dev_type) 251afcf1242STimo Teras score |= 2; 252afcf1242STimo Teras if (score == 0) 2531da177e4SLinus Torvalds return t; 254afcf1242STimo Teras 255afcf1242STimo Teras if (score < cand_score) { 256afcf1242STimo Teras cand = t; 257afcf1242STimo Teras cand_score = score; 258afcf1242STimo Teras } 259e1a80002SHerbert Xu } 260e1a80002SHerbert Xu 2618d5b2c08SEric Dumazet for_each_ip_tunnel_rcu(ign->tunnels_l[h1]) { 262749c10f9STimo Teras if ((local != t->parms.iph.saddr && 263749c10f9STimo Teras (local != t->parms.iph.daddr || 264749c10f9STimo Teras !ipv4_is_multicast(local))) || 265749c10f9STimo Teras key != t->parms.i_key || 266749c10f9STimo Teras !(t->dev->flags & IFF_UP)) 267749c10f9STimo Teras continue; 268749c10f9STimo Teras 269749c10f9STimo Teras if (t->dev->type != ARPHRD_IPGRE && 270749c10f9STimo Teras t->dev->type != dev_type) 271749c10f9STimo Teras continue; 272749c10f9STimo Teras 273afcf1242STimo Teras score = 0; 274749c10f9STimo Teras if (t->parms.link != link) 275afcf1242STimo Teras score |= 1; 276749c10f9STimo Teras if (t->dev->type != dev_type) 277afcf1242STimo Teras score |= 2; 278afcf1242STimo Teras if (score == 0) 2791da177e4SLinus Torvalds return t; 280afcf1242STimo Teras 281afcf1242STimo Teras if (score < cand_score) { 282afcf1242STimo Teras cand = t; 283afcf1242STimo Teras cand_score = score; 284afcf1242STimo Teras } 285e1a80002SHerbert Xu } 286e1a80002SHerbert Xu 2878d5b2c08SEric Dumazet for_each_ip_tunnel_rcu(ign->tunnels_wc[h1]) { 288749c10f9STimo Teras if (t->parms.i_key != key || 289749c10f9STimo Teras !(t->dev->flags & IFF_UP)) 290749c10f9STimo Teras continue; 291749c10f9STimo Teras 292749c10f9STimo Teras if (t->dev->type != ARPHRD_IPGRE && 293749c10f9STimo Teras t->dev->type != dev_type) 294749c10f9STimo Teras continue; 295749c10f9STimo Teras 296afcf1242STimo Teras score = 0; 297749c10f9STimo Teras if (t->parms.link != link) 298afcf1242STimo Teras score |= 1; 299749c10f9STimo Teras if (t->dev->type != dev_type) 300afcf1242STimo Teras score |= 2; 301afcf1242STimo Teras if (score == 0) 3021da177e4SLinus Torvalds return t; 303afcf1242STimo Teras 304afcf1242STimo Teras if (score < cand_score) { 305afcf1242STimo Teras cand = t; 306afcf1242STimo Teras cand_score = score; 307afcf1242STimo Teras } 308e1a80002SHerbert Xu } 309e1a80002SHerbert Xu 310afcf1242STimo Teras if (cand != NULL) 311afcf1242STimo Teras return cand; 3121da177e4SLinus Torvalds 3138d5b2c08SEric Dumazet dev = ign->fb_tunnel_dev; 3148d5b2c08SEric Dumazet if (dev->flags & IFF_UP) 3158d5b2c08SEric Dumazet return netdev_priv(dev); 316749c10f9STimo Teras 3171da177e4SLinus Torvalds return NULL; 3181da177e4SLinus Torvalds } 3191da177e4SLinus Torvalds 3201507850bSEric Dumazet static struct ip_tunnel __rcu **__ipgre_bucket(struct ipgre_net *ign, 321f57e7d5aSPavel Emelyanov struct ip_tunnel_parm *parms) 3221da177e4SLinus Torvalds { 3235056a1efSYOSHIFUJI Hideaki __be32 remote = parms->iph.daddr; 3245056a1efSYOSHIFUJI Hideaki __be32 local = parms->iph.saddr; 3255056a1efSYOSHIFUJI Hideaki __be32 key = parms->i_key; 3261507850bSEric Dumazet unsigned int h = HASH(key); 3271da177e4SLinus Torvalds int prio = 0; 3281da177e4SLinus Torvalds 3291da177e4SLinus Torvalds if (local) 3301da177e4SLinus Torvalds prio |= 1; 331f97c1e0cSJoe Perches if (remote && !ipv4_is_multicast(remote)) { 3321da177e4SLinus Torvalds prio |= 2; 3331da177e4SLinus Torvalds h ^= HASH(remote); 3341da177e4SLinus Torvalds } 3351da177e4SLinus Torvalds 336eb8ce741SPavel Emelyanov return &ign->tunnels[prio][h]; 3371da177e4SLinus Torvalds } 3381da177e4SLinus Torvalds 3391507850bSEric Dumazet static inline struct ip_tunnel __rcu **ipgre_bucket(struct ipgre_net *ign, 340f57e7d5aSPavel Emelyanov struct ip_tunnel *t) 3415056a1efSYOSHIFUJI Hideaki { 342f57e7d5aSPavel Emelyanov return __ipgre_bucket(ign, &t->parms); 3435056a1efSYOSHIFUJI Hideaki } 3445056a1efSYOSHIFUJI Hideaki 345f57e7d5aSPavel Emelyanov static void ipgre_tunnel_link(struct ipgre_net *ign, struct ip_tunnel *t) 3461da177e4SLinus Torvalds { 3471507850bSEric Dumazet struct ip_tunnel __rcu **tp = ipgre_bucket(ign, t); 3481da177e4SLinus Torvalds 3491507850bSEric Dumazet rcu_assign_pointer(t->next, rtnl_dereference(*tp)); 3508d5b2c08SEric Dumazet rcu_assign_pointer(*tp, t); 3511da177e4SLinus Torvalds } 3521da177e4SLinus Torvalds 353f57e7d5aSPavel Emelyanov static void ipgre_tunnel_unlink(struct ipgre_net *ign, struct ip_tunnel *t) 3541da177e4SLinus Torvalds { 3551507850bSEric Dumazet struct ip_tunnel __rcu **tp; 3561507850bSEric Dumazet struct ip_tunnel *iter; 3571da177e4SLinus Torvalds 3581507850bSEric Dumazet for (tp = ipgre_bucket(ign, t); 3591507850bSEric Dumazet (iter = rtnl_dereference(*tp)) != NULL; 3601507850bSEric Dumazet tp = &iter->next) { 3611507850bSEric Dumazet if (t == iter) { 3621507850bSEric Dumazet rcu_assign_pointer(*tp, t->next); 3631da177e4SLinus Torvalds break; 3641da177e4SLinus Torvalds } 3651da177e4SLinus Torvalds } 3661da177e4SLinus Torvalds } 3671da177e4SLinus Torvalds 368e1a80002SHerbert Xu static struct ip_tunnel *ipgre_tunnel_find(struct net *net, 369e1a80002SHerbert Xu struct ip_tunnel_parm *parms, 370e1a80002SHerbert Xu int type) 3711da177e4SLinus Torvalds { 372d5a0a1e3SAl Viro __be32 remote = parms->iph.daddr; 373d5a0a1e3SAl Viro __be32 local = parms->iph.saddr; 374d5a0a1e3SAl Viro __be32 key = parms->i_key; 375749c10f9STimo Teras int link = parms->link; 3761507850bSEric Dumazet struct ip_tunnel *t; 3771507850bSEric Dumazet struct ip_tunnel __rcu **tp; 378e1a80002SHerbert Xu struct ipgre_net *ign = net_generic(net, ipgre_net_id); 379e1a80002SHerbert Xu 3801507850bSEric Dumazet for (tp = __ipgre_bucket(ign, parms); 3811507850bSEric Dumazet (t = rtnl_dereference(*tp)) != NULL; 3821507850bSEric Dumazet tp = &t->next) 383e1a80002SHerbert Xu if (local == t->parms.iph.saddr && 384e1a80002SHerbert Xu remote == t->parms.iph.daddr && 385e1a80002SHerbert Xu key == t->parms.i_key && 386749c10f9STimo Teras link == t->parms.link && 387e1a80002SHerbert Xu type == t->dev->type) 388e1a80002SHerbert Xu break; 389e1a80002SHerbert Xu 390e1a80002SHerbert Xu return t; 391e1a80002SHerbert Xu } 392e1a80002SHerbert Xu 393e1a80002SHerbert Xu static struct ip_tunnel *ipgre_tunnel_locate(struct net *net, 394e1a80002SHerbert Xu struct ip_tunnel_parm *parms, int create) 395e1a80002SHerbert Xu { 396e1a80002SHerbert Xu struct ip_tunnel *t, *nt; 3971da177e4SLinus Torvalds struct net_device *dev; 3981da177e4SLinus Torvalds char name[IFNAMSIZ]; 399f57e7d5aSPavel Emelyanov struct ipgre_net *ign = net_generic(net, ipgre_net_id); 4001da177e4SLinus Torvalds 401e1a80002SHerbert Xu t = ipgre_tunnel_find(net, parms, ARPHRD_IPGRE); 402e1a80002SHerbert Xu if (t || !create) 4031da177e4SLinus Torvalds return t; 4041da177e4SLinus Torvalds 4051da177e4SLinus Torvalds if (parms->name[0]) 4061da177e4SLinus Torvalds strlcpy(name, parms->name, IFNAMSIZ); 40734cc7ba6SPavel Emelyanov else 40834cc7ba6SPavel Emelyanov sprintf(name, "gre%%d"); 4091da177e4SLinus Torvalds 4101da177e4SLinus Torvalds dev = alloc_netdev(sizeof(*t), name, ipgre_tunnel_setup); 4111da177e4SLinus Torvalds if (!dev) 4121da177e4SLinus Torvalds return NULL; 4131da177e4SLinus Torvalds 4140b67ecebSPavel Emelyanov dev_net_set(dev, net); 4150b67ecebSPavel Emelyanov 416b37d428bSPavel Emelyanov if (strchr(name, '%')) { 417b37d428bSPavel Emelyanov if (dev_alloc_name(dev, name) < 0) 418b37d428bSPavel Emelyanov goto failed_free; 419b37d428bSPavel Emelyanov } 420b37d428bSPavel Emelyanov 4212941a486SPatrick McHardy nt = netdev_priv(dev); 4221da177e4SLinus Torvalds nt->parms = *parms; 423c19e654dSHerbert Xu dev->rtnl_link_ops = &ipgre_link_ops; 4241da177e4SLinus Torvalds 42542aa9162SHerbert Xu dev->mtu = ipgre_tunnel_bind_dev(dev); 42642aa9162SHerbert Xu 427b37d428bSPavel Emelyanov if (register_netdevice(dev) < 0) 428b37d428bSPavel Emelyanov goto failed_free; 4291da177e4SLinus Torvalds 4301da177e4SLinus Torvalds dev_hold(dev); 431f57e7d5aSPavel Emelyanov ipgre_tunnel_link(ign, nt); 4321da177e4SLinus Torvalds return nt; 4331da177e4SLinus Torvalds 434b37d428bSPavel Emelyanov failed_free: 435b37d428bSPavel Emelyanov free_netdev(dev); 4361da177e4SLinus Torvalds return NULL; 4371da177e4SLinus Torvalds } 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds static void ipgre_tunnel_uninit(struct net_device *dev) 4401da177e4SLinus Torvalds { 441f57e7d5aSPavel Emelyanov struct net *net = dev_net(dev); 442f57e7d5aSPavel Emelyanov struct ipgre_net *ign = net_generic(net, ipgre_net_id); 443f57e7d5aSPavel Emelyanov 444f57e7d5aSPavel Emelyanov ipgre_tunnel_unlink(ign, netdev_priv(dev)); 4451da177e4SLinus Torvalds dev_put(dev); 4461da177e4SLinus Torvalds } 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds 4491da177e4SLinus Torvalds static void ipgre_err(struct sk_buff *skb, u32 info) 4501da177e4SLinus Torvalds { 4511da177e4SLinus Torvalds 452071f92d0SRami Rosen /* All the routers (except for Linux) return only 4531da177e4SLinus Torvalds 8 bytes of packet payload. It means, that precise relaying of 4541da177e4SLinus Torvalds ICMP in the real Internet is absolutely infeasible. 4551da177e4SLinus Torvalds 4561da177e4SLinus Torvalds Moreover, Cisco "wise men" put GRE key to the third word 4571da177e4SLinus Torvalds in GRE header. It makes impossible maintaining even soft state for keyed 4581da177e4SLinus Torvalds GRE tunnels with enabled checksum. Tell them "thank you". 4591da177e4SLinus Torvalds 4601da177e4SLinus Torvalds Well, I wonder, rfc1812 was written by Cisco employee, 4611da177e4SLinus Torvalds what the hell these idiots break standrads established 4621da177e4SLinus Torvalds by themself??? 4631da177e4SLinus Torvalds */ 4641da177e4SLinus Torvalds 4651da177e4SLinus Torvalds struct iphdr *iph = (struct iphdr *)skb->data; 466d5a0a1e3SAl Viro __be16 *p = (__be16*)(skb->data+(iph->ihl<<2)); 4671da177e4SLinus Torvalds int grehlen = (iph->ihl<<2) + 4; 46888c7664fSArnaldo Carvalho de Melo const int type = icmp_hdr(skb)->type; 46988c7664fSArnaldo Carvalho de Melo const int code = icmp_hdr(skb)->code; 4701da177e4SLinus Torvalds struct ip_tunnel *t; 471d5a0a1e3SAl Viro __be16 flags; 4721da177e4SLinus Torvalds 4731da177e4SLinus Torvalds flags = p[0]; 4741da177e4SLinus Torvalds if (flags&(GRE_CSUM|GRE_KEY|GRE_SEQ|GRE_ROUTING|GRE_VERSION)) { 4751da177e4SLinus Torvalds if (flags&(GRE_VERSION|GRE_ROUTING)) 4761da177e4SLinus Torvalds return; 4771da177e4SLinus Torvalds if (flags&GRE_KEY) { 4781da177e4SLinus Torvalds grehlen += 4; 4791da177e4SLinus Torvalds if (flags&GRE_CSUM) 4801da177e4SLinus Torvalds grehlen += 4; 4811da177e4SLinus Torvalds } 4821da177e4SLinus Torvalds } 4831da177e4SLinus Torvalds 4841da177e4SLinus Torvalds /* If only 8 bytes returned, keyed message will be dropped here */ 4851da177e4SLinus Torvalds if (skb_headlen(skb) < grehlen) 4861da177e4SLinus Torvalds return; 4871da177e4SLinus Torvalds 4881da177e4SLinus Torvalds switch (type) { 4891da177e4SLinus Torvalds default: 4901da177e4SLinus Torvalds case ICMP_PARAMETERPROB: 4911da177e4SLinus Torvalds return; 4921da177e4SLinus Torvalds 4931da177e4SLinus Torvalds case ICMP_DEST_UNREACH: 4941da177e4SLinus Torvalds switch (code) { 4951da177e4SLinus Torvalds case ICMP_SR_FAILED: 4961da177e4SLinus Torvalds case ICMP_PORT_UNREACH: 4971da177e4SLinus Torvalds /* Impossible event. */ 4981da177e4SLinus Torvalds return; 4991da177e4SLinus Torvalds case ICMP_FRAG_NEEDED: 5001da177e4SLinus Torvalds /* Soft state for pmtu is maintained by IP core. */ 5011da177e4SLinus Torvalds return; 5021da177e4SLinus Torvalds default: 5031da177e4SLinus Torvalds /* All others are translated to HOST_UNREACH. 5041da177e4SLinus Torvalds rfc2003 contains "deep thoughts" about NET_UNREACH, 5051da177e4SLinus Torvalds I believe they are just ether pollution. --ANK 5061da177e4SLinus Torvalds */ 5071da177e4SLinus Torvalds break; 5081da177e4SLinus Torvalds } 5091da177e4SLinus Torvalds break; 5101da177e4SLinus Torvalds case ICMP_TIME_EXCEEDED: 5111da177e4SLinus Torvalds if (code != ICMP_EXC_TTL) 5121da177e4SLinus Torvalds return; 5131da177e4SLinus Torvalds break; 5141da177e4SLinus Torvalds } 5151da177e4SLinus Torvalds 5168d5b2c08SEric Dumazet rcu_read_lock(); 517749c10f9STimo Teras t = ipgre_tunnel_lookup(skb->dev, iph->daddr, iph->saddr, 518e1a80002SHerbert Xu flags & GRE_KEY ? 519e1a80002SHerbert Xu *(((__be32 *)p) + (grehlen / 4) - 1) : 0, 520e1a80002SHerbert Xu p[1]); 521f97c1e0cSJoe Perches if (t == NULL || t->parms.iph.daddr == 0 || 522f97c1e0cSJoe Perches ipv4_is_multicast(t->parms.iph.daddr)) 5231da177e4SLinus Torvalds goto out; 5241da177e4SLinus Torvalds 5251da177e4SLinus Torvalds if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED) 5261da177e4SLinus Torvalds goto out; 5271da177e4SLinus Torvalds 528da6185d8SWei Yongjun if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO)) 5291da177e4SLinus Torvalds t->err_count++; 5301da177e4SLinus Torvalds else 5311da177e4SLinus Torvalds t->err_count = 1; 5321da177e4SLinus Torvalds t->err_time = jiffies; 5331da177e4SLinus Torvalds out: 5348d5b2c08SEric Dumazet rcu_read_unlock(); 5351da177e4SLinus Torvalds } 5361da177e4SLinus Torvalds 5371da177e4SLinus Torvalds static inline void ipgre_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb) 5381da177e4SLinus Torvalds { 5391da177e4SLinus Torvalds if (INET_ECN_is_ce(iph->tos)) { 5401da177e4SLinus Torvalds if (skb->protocol == htons(ETH_P_IP)) { 541eddc9ec5SArnaldo Carvalho de Melo IP_ECN_set_ce(ip_hdr(skb)); 5421da177e4SLinus Torvalds } else if (skb->protocol == htons(ETH_P_IPV6)) { 5430660e03fSArnaldo Carvalho de Melo IP6_ECN_set_ce(ipv6_hdr(skb)); 5441da177e4SLinus Torvalds } 5451da177e4SLinus Torvalds } 5461da177e4SLinus Torvalds } 5471da177e4SLinus Torvalds 5481da177e4SLinus Torvalds static inline u8 5491da177e4SLinus Torvalds ipgre_ecn_encapsulate(u8 tos, struct iphdr *old_iph, struct sk_buff *skb) 5501da177e4SLinus Torvalds { 5511da177e4SLinus Torvalds u8 inner = 0; 5521da177e4SLinus Torvalds if (skb->protocol == htons(ETH_P_IP)) 5531da177e4SLinus Torvalds inner = old_iph->tos; 5541da177e4SLinus Torvalds else if (skb->protocol == htons(ETH_P_IPV6)) 5551da177e4SLinus Torvalds inner = ipv6_get_dsfield((struct ipv6hdr *)old_iph); 5561da177e4SLinus Torvalds return INET_ECN_encapsulate(tos, inner); 5571da177e4SLinus Torvalds } 5581da177e4SLinus Torvalds 5591da177e4SLinus Torvalds static int ipgre_rcv(struct sk_buff *skb) 5601da177e4SLinus Torvalds { 5611da177e4SLinus Torvalds struct iphdr *iph; 5621da177e4SLinus Torvalds u8 *h; 563d5a0a1e3SAl Viro __be16 flags; 564d3bc23e7SAl Viro __sum16 csum = 0; 565d5a0a1e3SAl Viro __be32 key = 0; 5661da177e4SLinus Torvalds u32 seqno = 0; 5671da177e4SLinus Torvalds struct ip_tunnel *tunnel; 5681da177e4SLinus Torvalds int offset = 4; 569e1a80002SHerbert Xu __be16 gre_proto; 5701da177e4SLinus Torvalds 5711da177e4SLinus Torvalds if (!pskb_may_pull(skb, 16)) 5721da177e4SLinus Torvalds goto drop_nolock; 5731da177e4SLinus Torvalds 574eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 5751da177e4SLinus Torvalds h = skb->data; 576d5a0a1e3SAl Viro flags = *(__be16*)h; 5771da177e4SLinus Torvalds 5781da177e4SLinus Torvalds if (flags&(GRE_CSUM|GRE_KEY|GRE_ROUTING|GRE_SEQ|GRE_VERSION)) { 5791da177e4SLinus Torvalds /* - Version must be 0. 5801da177e4SLinus Torvalds - We do not support routing headers. 5811da177e4SLinus Torvalds */ 5821da177e4SLinus Torvalds if (flags&(GRE_VERSION|GRE_ROUTING)) 5831da177e4SLinus Torvalds goto drop_nolock; 5841da177e4SLinus Torvalds 5851da177e4SLinus Torvalds if (flags&GRE_CSUM) { 586fb286bb2SHerbert Xu switch (skb->ip_summed) { 58784fa7933SPatrick McHardy case CHECKSUM_COMPLETE: 588d3bc23e7SAl Viro csum = csum_fold(skb->csum); 589fb286bb2SHerbert Xu if (!csum) 590fb286bb2SHerbert Xu break; 591fb286bb2SHerbert Xu /* fall through */ 592fb286bb2SHerbert Xu case CHECKSUM_NONE: 593fb286bb2SHerbert Xu skb->csum = 0; 594fb286bb2SHerbert Xu csum = __skb_checksum_complete(skb); 59584fa7933SPatrick McHardy skb->ip_summed = CHECKSUM_COMPLETE; 5961da177e4SLinus Torvalds } 5971da177e4SLinus Torvalds offset += 4; 5981da177e4SLinus Torvalds } 5991da177e4SLinus Torvalds if (flags&GRE_KEY) { 600d5a0a1e3SAl Viro key = *(__be32*)(h + offset); 6011da177e4SLinus Torvalds offset += 4; 6021da177e4SLinus Torvalds } 6031da177e4SLinus Torvalds if (flags&GRE_SEQ) { 604d5a0a1e3SAl Viro seqno = ntohl(*(__be32*)(h + offset)); 6051da177e4SLinus Torvalds offset += 4; 6061da177e4SLinus Torvalds } 6071da177e4SLinus Torvalds } 6081da177e4SLinus Torvalds 609e1a80002SHerbert Xu gre_proto = *(__be16 *)(h + 2); 610e1a80002SHerbert Xu 6118d5b2c08SEric Dumazet rcu_read_lock(); 612749c10f9STimo Teras if ((tunnel = ipgre_tunnel_lookup(skb->dev, 613e1a80002SHerbert Xu iph->saddr, iph->daddr, key, 614e1a80002SHerbert Xu gre_proto))) { 615e985aad7SEric Dumazet struct pcpu_tstats *tstats; 616addd68ebSPavel Emelyanov 6171da177e4SLinus Torvalds secpath_reset(skb); 6181da177e4SLinus Torvalds 619e1a80002SHerbert Xu skb->protocol = gre_proto; 6201da177e4SLinus Torvalds /* WCCP version 1 and 2 protocol decoding. 6211da177e4SLinus Torvalds * - Change protocol to IP 6221da177e4SLinus Torvalds * - When dealing with WCCPv2, Skip extra 4 bytes in GRE header 6231da177e4SLinus Torvalds */ 624e1a80002SHerbert Xu if (flags == 0 && gre_proto == htons(ETH_P_WCCP)) { 625496c98dfSYOSHIFUJI Hideaki skb->protocol = htons(ETH_P_IP); 6261da177e4SLinus Torvalds if ((*(h + offset) & 0xF0) != 0x40) 6271da177e4SLinus Torvalds offset += 4; 6281da177e4SLinus Torvalds } 6291da177e4SLinus Torvalds 6301d069167STimo Teras skb->mac_header = skb->network_header; 6314209fb60SArnaldo Carvalho de Melo __pskb_pull(skb, offset); 6329c70220bSArnaldo Carvalho de Melo skb_postpull_rcsum(skb, skb_transport_header(skb), offset); 6331da177e4SLinus Torvalds skb->pkt_type = PACKET_HOST; 6341da177e4SLinus Torvalds #ifdef CONFIG_NET_IPGRE_BROADCAST 635f97c1e0cSJoe Perches if (ipv4_is_multicast(iph->daddr)) { 6361da177e4SLinus Torvalds /* Looped back packet, drop it! */ 637511c3f92SEric Dumazet if (skb_rtable(skb)->fl.iif == 0) 6381da177e4SLinus Torvalds goto drop; 639e985aad7SEric Dumazet tunnel->dev->stats.multicast++; 6401da177e4SLinus Torvalds skb->pkt_type = PACKET_BROADCAST; 6411da177e4SLinus Torvalds } 6421da177e4SLinus Torvalds #endif 6431da177e4SLinus Torvalds 6441da177e4SLinus Torvalds if (((flags&GRE_CSUM) && csum) || 6451da177e4SLinus Torvalds (!(flags&GRE_CSUM) && tunnel->parms.i_flags&GRE_CSUM)) { 646e985aad7SEric Dumazet tunnel->dev->stats.rx_crc_errors++; 647e985aad7SEric Dumazet tunnel->dev->stats.rx_errors++; 6481da177e4SLinus Torvalds goto drop; 6491da177e4SLinus Torvalds } 6501da177e4SLinus Torvalds if (tunnel->parms.i_flags&GRE_SEQ) { 6511da177e4SLinus Torvalds if (!(flags&GRE_SEQ) || 6521da177e4SLinus Torvalds (tunnel->i_seqno && (s32)(seqno - tunnel->i_seqno) < 0)) { 653e985aad7SEric Dumazet tunnel->dev->stats.rx_fifo_errors++; 654e985aad7SEric Dumazet tunnel->dev->stats.rx_errors++; 6551da177e4SLinus Torvalds goto drop; 6561da177e4SLinus Torvalds } 6571da177e4SLinus Torvalds tunnel->i_seqno = seqno + 1; 6581da177e4SLinus Torvalds } 659e1a80002SHerbert Xu 660e1a80002SHerbert Xu /* Warning: All skb pointers will be invalidated! */ 661e1a80002SHerbert Xu if (tunnel->dev->type == ARPHRD_ETHER) { 662e1a80002SHerbert Xu if (!pskb_may_pull(skb, ETH_HLEN)) { 663e985aad7SEric Dumazet tunnel->dev->stats.rx_length_errors++; 664e985aad7SEric Dumazet tunnel->dev->stats.rx_errors++; 665e1a80002SHerbert Xu goto drop; 666e1a80002SHerbert Xu } 667e1a80002SHerbert Xu 668e1a80002SHerbert Xu iph = ip_hdr(skb); 669e1a80002SHerbert Xu skb->protocol = eth_type_trans(skb, tunnel->dev); 670e1a80002SHerbert Xu skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN); 671e1a80002SHerbert Xu } 672e1a80002SHerbert Xu 673e985aad7SEric Dumazet tstats = this_cpu_ptr(tunnel->dev->tstats); 674e985aad7SEric Dumazet tstats->rx_packets++; 675e985aad7SEric Dumazet tstats->rx_bytes += skb->len; 676e985aad7SEric Dumazet 677e985aad7SEric Dumazet __skb_tunnel_rx(skb, tunnel->dev); 678e1a80002SHerbert Xu 679e1a80002SHerbert Xu skb_reset_network_header(skb); 6801da177e4SLinus Torvalds ipgre_ecn_decapsulate(iph, skb); 681e1a80002SHerbert Xu 682caf586e5SEric Dumazet netif_rx(skb); 6838990f468SEric Dumazet 6848d5b2c08SEric Dumazet rcu_read_unlock(); 6858990f468SEric Dumazet return 0; 6861da177e4SLinus Torvalds } 68745af08beSHerbert Xu icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); 6881da177e4SLinus Torvalds 6891da177e4SLinus Torvalds drop: 6908d5b2c08SEric Dumazet rcu_read_unlock(); 6911da177e4SLinus Torvalds drop_nolock: 6921da177e4SLinus Torvalds kfree_skb(skb); 693a02cec21SEric Dumazet return 0; 6941da177e4SLinus Torvalds } 6951da177e4SLinus Torvalds 6966fef4c0cSStephen Hemminger static netdev_tx_t ipgre_tunnel_xmit(struct sk_buff *skb, struct net_device *dev) 6971da177e4SLinus Torvalds { 6982941a486SPatrick McHardy struct ip_tunnel *tunnel = netdev_priv(dev); 699e985aad7SEric Dumazet struct pcpu_tstats *tstats; 700eddc9ec5SArnaldo Carvalho de Melo struct iphdr *old_iph = ip_hdr(skb); 7011da177e4SLinus Torvalds struct iphdr *tiph; 7021da177e4SLinus Torvalds u8 tos; 703d5a0a1e3SAl Viro __be16 df; 7041da177e4SLinus Torvalds struct rtable *rt; /* Route to the other host */ 7051da177e4SLinus Torvalds struct net_device *tdev; /* Device to other host */ 7061da177e4SLinus Torvalds struct iphdr *iph; /* Our new IP header */ 707c2636b4dSChuck Lever unsigned int max_headroom; /* The extra header space needed */ 7081da177e4SLinus Torvalds int gre_hlen; 709d5a0a1e3SAl Viro __be32 dst; 7101da177e4SLinus Torvalds int mtu; 7111da177e4SLinus Torvalds 712e1a80002SHerbert Xu if (dev->type == ARPHRD_ETHER) 713e1a80002SHerbert Xu IPCB(skb)->flags = 0; 714e1a80002SHerbert Xu 715e1a80002SHerbert Xu if (dev->header_ops && dev->type == ARPHRD_IPGRE) { 7161da177e4SLinus Torvalds gre_hlen = 0; 7171da177e4SLinus Torvalds tiph = (struct iphdr *)skb->data; 7181da177e4SLinus Torvalds } else { 7191da177e4SLinus Torvalds gre_hlen = tunnel->hlen; 7201da177e4SLinus Torvalds tiph = &tunnel->parms.iph; 7211da177e4SLinus Torvalds } 7221da177e4SLinus Torvalds 7231da177e4SLinus Torvalds if ((dst = tiph->daddr) == 0) { 7241da177e4SLinus Torvalds /* NBMA tunnel */ 7251da177e4SLinus Torvalds 726adf30907SEric Dumazet if (skb_dst(skb) == NULL) { 727e985aad7SEric Dumazet dev->stats.tx_fifo_errors++; 7281da177e4SLinus Torvalds goto tx_error; 7291da177e4SLinus Torvalds } 7301da177e4SLinus Torvalds 7311da177e4SLinus Torvalds if (skb->protocol == htons(ETH_P_IP)) { 732511c3f92SEric Dumazet rt = skb_rtable(skb); 7331da177e4SLinus Torvalds if ((dst = rt->rt_gateway) == 0) 7341da177e4SLinus Torvalds goto tx_error_icmp; 7351da177e4SLinus Torvalds } 736842c74bfSEric Dumazet #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 7371da177e4SLinus Torvalds else if (skb->protocol == htons(ETH_P_IPV6)) { 7381da177e4SLinus Torvalds struct in6_addr *addr6; 7391da177e4SLinus Torvalds int addr_type; 740adf30907SEric Dumazet struct neighbour *neigh = skb_dst(skb)->neighbour; 7411da177e4SLinus Torvalds 7421da177e4SLinus Torvalds if (neigh == NULL) 7431da177e4SLinus Torvalds goto tx_error; 7441da177e4SLinus Torvalds 7451da177e4SLinus Torvalds addr6 = (struct in6_addr *)&neigh->primary_key; 7461da177e4SLinus Torvalds addr_type = ipv6_addr_type(addr6); 7471da177e4SLinus Torvalds 7481da177e4SLinus Torvalds if (addr_type == IPV6_ADDR_ANY) { 7490660e03fSArnaldo Carvalho de Melo addr6 = &ipv6_hdr(skb)->daddr; 7501da177e4SLinus Torvalds addr_type = ipv6_addr_type(addr6); 7511da177e4SLinus Torvalds } 7521da177e4SLinus Torvalds 7531da177e4SLinus Torvalds if ((addr_type & IPV6_ADDR_COMPATv4) == 0) 7541da177e4SLinus Torvalds goto tx_error_icmp; 7551da177e4SLinus Torvalds 7561da177e4SLinus Torvalds dst = addr6->s6_addr32[3]; 7571da177e4SLinus Torvalds } 7581da177e4SLinus Torvalds #endif 7591da177e4SLinus Torvalds else 7601da177e4SLinus Torvalds goto tx_error; 7611da177e4SLinus Torvalds } 7621da177e4SLinus Torvalds 7631da177e4SLinus Torvalds tos = tiph->tos; 764ee686ca9SAndreas Jaggi if (tos == 1) { 765ee686ca9SAndreas Jaggi tos = 0; 7661da177e4SLinus Torvalds if (skb->protocol == htons(ETH_P_IP)) 7671da177e4SLinus Torvalds tos = old_iph->tos; 768dd4ba83dSStephen Hemminger else if (skb->protocol == htons(ETH_P_IPV6)) 769dd4ba83dSStephen Hemminger tos = ipv6_get_dsfield((struct ipv6hdr *)old_iph); 7701da177e4SLinus Torvalds } 7711da177e4SLinus Torvalds 7721da177e4SLinus Torvalds { 773e985aad7SEric Dumazet struct flowi fl = { 774e985aad7SEric Dumazet .oif = tunnel->parms.link, 775e985aad7SEric Dumazet .nl_u = { 776e985aad7SEric Dumazet .ip4_u = { 777e985aad7SEric Dumazet .daddr = dst, 7781da177e4SLinus Torvalds .saddr = tiph->saddr, 779e985aad7SEric Dumazet .tos = RT_TOS(tos) 780e985aad7SEric Dumazet } 781e985aad7SEric Dumazet }, 782e985aad7SEric Dumazet .proto = IPPROTO_GRE 783e985aad7SEric Dumazet } 784e985aad7SEric Dumazet ; 78596635522SPavel Emelyanov if (ip_route_output_key(dev_net(dev), &rt, &fl)) { 786e985aad7SEric Dumazet dev->stats.tx_carrier_errors++; 7871da177e4SLinus Torvalds goto tx_error; 7881da177e4SLinus Torvalds } 7891da177e4SLinus Torvalds } 790d8d1f30bSChangli Gao tdev = rt->dst.dev; 7911da177e4SLinus Torvalds 7921da177e4SLinus Torvalds if (tdev == dev) { 7931da177e4SLinus Torvalds ip_rt_put(rt); 794e985aad7SEric Dumazet dev->stats.collisions++; 7951da177e4SLinus Torvalds goto tx_error; 7961da177e4SLinus Torvalds } 7971da177e4SLinus Torvalds 7981da177e4SLinus Torvalds df = tiph->frag_off; 7991da177e4SLinus Torvalds if (df) 800d8d1f30bSChangli Gao mtu = dst_mtu(&rt->dst) - dev->hard_header_len - tunnel->hlen; 8011da177e4SLinus Torvalds else 802adf30907SEric Dumazet mtu = skb_dst(skb) ? dst_mtu(skb_dst(skb)) : dev->mtu; 8031da177e4SLinus Torvalds 804adf30907SEric Dumazet if (skb_dst(skb)) 805adf30907SEric Dumazet skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu); 8061da177e4SLinus Torvalds 8071da177e4SLinus Torvalds if (skb->protocol == htons(ETH_P_IP)) { 8081da177e4SLinus Torvalds df |= (old_iph->frag_off&htons(IP_DF)); 8091da177e4SLinus Torvalds 8101da177e4SLinus Torvalds if ((old_iph->frag_off&htons(IP_DF)) && 8111da177e4SLinus Torvalds mtu < ntohs(old_iph->tot_len)) { 8121da177e4SLinus Torvalds icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu)); 8131da177e4SLinus Torvalds ip_rt_put(rt); 8141da177e4SLinus Torvalds goto tx_error; 8151da177e4SLinus Torvalds } 8161da177e4SLinus Torvalds } 817842c74bfSEric Dumazet #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 8181da177e4SLinus Torvalds else if (skb->protocol == htons(ETH_P_IPV6)) { 819adf30907SEric Dumazet struct rt6_info *rt6 = (struct rt6_info *)skb_dst(skb); 8201da177e4SLinus Torvalds 821adf30907SEric Dumazet if (rt6 && mtu < dst_mtu(skb_dst(skb)) && mtu >= IPV6_MIN_MTU) { 822f97c1e0cSJoe Perches if ((tunnel->parms.iph.daddr && 823f97c1e0cSJoe Perches !ipv4_is_multicast(tunnel->parms.iph.daddr)) || 8241da177e4SLinus Torvalds rt6->rt6i_dst.plen == 128) { 8251da177e4SLinus Torvalds rt6->rt6i_flags |= RTF_MODIFIED; 826adf30907SEric Dumazet skb_dst(skb)->metrics[RTAX_MTU-1] = mtu; 8271da177e4SLinus Torvalds } 8281da177e4SLinus Torvalds } 8291da177e4SLinus Torvalds 8301da177e4SLinus Torvalds if (mtu >= IPV6_MIN_MTU && mtu < skb->len - tunnel->hlen + gre_hlen) { 8313ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); 8321da177e4SLinus Torvalds ip_rt_put(rt); 8331da177e4SLinus Torvalds goto tx_error; 8341da177e4SLinus Torvalds } 8351da177e4SLinus Torvalds } 8361da177e4SLinus Torvalds #endif 8371da177e4SLinus Torvalds 8381da177e4SLinus Torvalds if (tunnel->err_count > 0) { 839da6185d8SWei Yongjun if (time_before(jiffies, 840da6185d8SWei Yongjun tunnel->err_time + IPTUNNEL_ERR_TIMEO)) { 8411da177e4SLinus Torvalds tunnel->err_count--; 8421da177e4SLinus Torvalds 8431da177e4SLinus Torvalds dst_link_failure(skb); 8441da177e4SLinus Torvalds } else 8451da177e4SLinus Torvalds tunnel->err_count = 0; 8461da177e4SLinus Torvalds } 8471da177e4SLinus Torvalds 848d8d1f30bSChangli Gao max_headroom = LL_RESERVED_SPACE(tdev) + gre_hlen + rt->dst.header_len; 8491da177e4SLinus Torvalds 850cfbba49dSPatrick McHardy if (skb_headroom(skb) < max_headroom || skb_shared(skb)|| 851cfbba49dSPatrick McHardy (skb_cloned(skb) && !skb_clone_writable(skb, 0))) { 8521da177e4SLinus Torvalds struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom); 853243aad83STimo Teräs if (max_headroom > dev->needed_headroom) 854243aad83STimo Teräs dev->needed_headroom = max_headroom; 8551da177e4SLinus Torvalds if (!new_skb) { 8561da177e4SLinus Torvalds ip_rt_put(rt); 857e985aad7SEric Dumazet dev->stats.tx_dropped++; 8581da177e4SLinus Torvalds dev_kfree_skb(skb); 8596ed10654SPatrick McHardy return NETDEV_TX_OK; 8601da177e4SLinus Torvalds } 8611da177e4SLinus Torvalds if (skb->sk) 8621da177e4SLinus Torvalds skb_set_owner_w(new_skb, skb->sk); 8631da177e4SLinus Torvalds dev_kfree_skb(skb); 8641da177e4SLinus Torvalds skb = new_skb; 865eddc9ec5SArnaldo Carvalho de Melo old_iph = ip_hdr(skb); 8661da177e4SLinus Torvalds } 8671da177e4SLinus Torvalds 86864194c31SHerbert Xu skb_reset_transport_header(skb); 869e2d1bca7SArnaldo Carvalho de Melo skb_push(skb, gre_hlen); 870e2d1bca7SArnaldo Carvalho de Melo skb_reset_network_header(skb); 8711da177e4SLinus Torvalds memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); 87248d5cad8SPatrick McHardy IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED | 87348d5cad8SPatrick McHardy IPSKB_REROUTED); 874adf30907SEric Dumazet skb_dst_drop(skb); 875d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 8761da177e4SLinus Torvalds 8771da177e4SLinus Torvalds /* 8781da177e4SLinus Torvalds * Push down and install the IPIP header. 8791da177e4SLinus Torvalds */ 8801da177e4SLinus Torvalds 881eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 8821da177e4SLinus Torvalds iph->version = 4; 8831da177e4SLinus Torvalds iph->ihl = sizeof(struct iphdr) >> 2; 8841da177e4SLinus Torvalds iph->frag_off = df; 8851da177e4SLinus Torvalds iph->protocol = IPPROTO_GRE; 8861da177e4SLinus Torvalds iph->tos = ipgre_ecn_encapsulate(tos, old_iph, skb); 8871da177e4SLinus Torvalds iph->daddr = rt->rt_dst; 8881da177e4SLinus Torvalds iph->saddr = rt->rt_src; 8891da177e4SLinus Torvalds 8901da177e4SLinus Torvalds if ((iph->ttl = tiph->ttl) == 0) { 8911da177e4SLinus Torvalds if (skb->protocol == htons(ETH_P_IP)) 8921da177e4SLinus Torvalds iph->ttl = old_iph->ttl; 893842c74bfSEric Dumazet #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 8941da177e4SLinus Torvalds else if (skb->protocol == htons(ETH_P_IPV6)) 8951da177e4SLinus Torvalds iph->ttl = ((struct ipv6hdr *)old_iph)->hop_limit; 8961da177e4SLinus Torvalds #endif 8971da177e4SLinus Torvalds else 898d8d1f30bSChangli Gao iph->ttl = dst_metric(&rt->dst, RTAX_HOPLIMIT); 8991da177e4SLinus Torvalds } 9001da177e4SLinus Torvalds 901d5a0a1e3SAl Viro ((__be16 *)(iph + 1))[0] = tunnel->parms.o_flags; 902e1a80002SHerbert Xu ((__be16 *)(iph + 1))[1] = (dev->type == ARPHRD_ETHER) ? 903e1a80002SHerbert Xu htons(ETH_P_TEB) : skb->protocol; 9041da177e4SLinus Torvalds 9051da177e4SLinus Torvalds if (tunnel->parms.o_flags&(GRE_KEY|GRE_CSUM|GRE_SEQ)) { 906d5a0a1e3SAl Viro __be32 *ptr = (__be32*)(((u8*)iph) + tunnel->hlen - 4); 9071da177e4SLinus Torvalds 9081da177e4SLinus Torvalds if (tunnel->parms.o_flags&GRE_SEQ) { 9091da177e4SLinus Torvalds ++tunnel->o_seqno; 9101da177e4SLinus Torvalds *ptr = htonl(tunnel->o_seqno); 9111da177e4SLinus Torvalds ptr--; 9121da177e4SLinus Torvalds } 9131da177e4SLinus Torvalds if (tunnel->parms.o_flags&GRE_KEY) { 9141da177e4SLinus Torvalds *ptr = tunnel->parms.o_key; 9151da177e4SLinus Torvalds ptr--; 9161da177e4SLinus Torvalds } 9171da177e4SLinus Torvalds if (tunnel->parms.o_flags&GRE_CSUM) { 9181da177e4SLinus Torvalds *ptr = 0; 9195f92a738SAl Viro *(__sum16*)ptr = ip_compute_csum((void*)(iph+1), skb->len - sizeof(struct iphdr)); 9201da177e4SLinus Torvalds } 9211da177e4SLinus Torvalds } 9221da177e4SLinus Torvalds 9231da177e4SLinus Torvalds nf_reset(skb); 924e985aad7SEric Dumazet tstats = this_cpu_ptr(dev->tstats); 925e985aad7SEric Dumazet __IPTUNNEL_XMIT(tstats, &dev->stats); 9266ed10654SPatrick McHardy return NETDEV_TX_OK; 9271da177e4SLinus Torvalds 9281da177e4SLinus Torvalds tx_error_icmp: 9291da177e4SLinus Torvalds dst_link_failure(skb); 9301da177e4SLinus Torvalds 9311da177e4SLinus Torvalds tx_error: 932e985aad7SEric Dumazet dev->stats.tx_errors++; 9331da177e4SLinus Torvalds dev_kfree_skb(skb); 9346ed10654SPatrick McHardy return NETDEV_TX_OK; 9351da177e4SLinus Torvalds } 9361da177e4SLinus Torvalds 93742aa9162SHerbert Xu static int ipgre_tunnel_bind_dev(struct net_device *dev) 938ee34c1ebSMichal Schmidt { 939ee34c1ebSMichal Schmidt struct net_device *tdev = NULL; 940ee34c1ebSMichal Schmidt struct ip_tunnel *tunnel; 941ee34c1ebSMichal Schmidt struct iphdr *iph; 942ee34c1ebSMichal Schmidt int hlen = LL_MAX_HEADER; 943ee34c1ebSMichal Schmidt int mtu = ETH_DATA_LEN; 944ee34c1ebSMichal Schmidt int addend = sizeof(struct iphdr) + 4; 945ee34c1ebSMichal Schmidt 946ee34c1ebSMichal Schmidt tunnel = netdev_priv(dev); 947ee34c1ebSMichal Schmidt iph = &tunnel->parms.iph; 948ee34c1ebSMichal Schmidt 949c95b819aSHerbert Xu /* Guess output device to choose reasonable mtu and needed_headroom */ 950ee34c1ebSMichal Schmidt 951ee34c1ebSMichal Schmidt if (iph->daddr) { 952e985aad7SEric Dumazet struct flowi fl = { 953e985aad7SEric Dumazet .oif = tunnel->parms.link, 954e985aad7SEric Dumazet .nl_u = { 955e985aad7SEric Dumazet .ip4_u = { 956e985aad7SEric Dumazet .daddr = iph->daddr, 957ee34c1ebSMichal Schmidt .saddr = iph->saddr, 958e985aad7SEric Dumazet .tos = RT_TOS(iph->tos) 959e985aad7SEric Dumazet } 960e985aad7SEric Dumazet }, 961e985aad7SEric Dumazet .proto = IPPROTO_GRE 962e985aad7SEric Dumazet }; 963ee34c1ebSMichal Schmidt struct rtable *rt; 964e985aad7SEric Dumazet 96596635522SPavel Emelyanov if (!ip_route_output_key(dev_net(dev), &rt, &fl)) { 966d8d1f30bSChangli Gao tdev = rt->dst.dev; 967ee34c1ebSMichal Schmidt ip_rt_put(rt); 968ee34c1ebSMichal Schmidt } 969e1a80002SHerbert Xu 970e1a80002SHerbert Xu if (dev->type != ARPHRD_ETHER) 971ee34c1ebSMichal Schmidt dev->flags |= IFF_POINTOPOINT; 972ee34c1ebSMichal Schmidt } 973ee34c1ebSMichal Schmidt 974ee34c1ebSMichal Schmidt if (!tdev && tunnel->parms.link) 97596635522SPavel Emelyanov tdev = __dev_get_by_index(dev_net(dev), tunnel->parms.link); 976ee34c1ebSMichal Schmidt 977ee34c1ebSMichal Schmidt if (tdev) { 978c95b819aSHerbert Xu hlen = tdev->hard_header_len + tdev->needed_headroom; 979ee34c1ebSMichal Schmidt mtu = tdev->mtu; 980ee34c1ebSMichal Schmidt } 981ee34c1ebSMichal Schmidt dev->iflink = tunnel->parms.link; 982ee34c1ebSMichal Schmidt 983ee34c1ebSMichal Schmidt /* Precalculate GRE options length */ 984ee34c1ebSMichal Schmidt if (tunnel->parms.o_flags&(GRE_CSUM|GRE_KEY|GRE_SEQ)) { 985ee34c1ebSMichal Schmidt if (tunnel->parms.o_flags&GRE_CSUM) 986ee34c1ebSMichal Schmidt addend += 4; 987ee34c1ebSMichal Schmidt if (tunnel->parms.o_flags&GRE_KEY) 988ee34c1ebSMichal Schmidt addend += 4; 989ee34c1ebSMichal Schmidt if (tunnel->parms.o_flags&GRE_SEQ) 990ee34c1ebSMichal Schmidt addend += 4; 991ee34c1ebSMichal Schmidt } 992c95b819aSHerbert Xu dev->needed_headroom = addend + hlen; 9938cdb0456STom Goff mtu -= dev->hard_header_len + addend; 99442aa9162SHerbert Xu 99542aa9162SHerbert Xu if (mtu < 68) 99642aa9162SHerbert Xu mtu = 68; 99742aa9162SHerbert Xu 998ee34c1ebSMichal Schmidt tunnel->hlen = addend; 999ee34c1ebSMichal Schmidt 100042aa9162SHerbert Xu return mtu; 1001ee34c1ebSMichal Schmidt } 1002ee34c1ebSMichal Schmidt 10031da177e4SLinus Torvalds static int 10041da177e4SLinus Torvalds ipgre_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd) 10051da177e4SLinus Torvalds { 10061da177e4SLinus Torvalds int err = 0; 10071da177e4SLinus Torvalds struct ip_tunnel_parm p; 10081da177e4SLinus Torvalds struct ip_tunnel *t; 1009f57e7d5aSPavel Emelyanov struct net *net = dev_net(dev); 1010f57e7d5aSPavel Emelyanov struct ipgre_net *ign = net_generic(net, ipgre_net_id); 10111da177e4SLinus Torvalds 10121da177e4SLinus Torvalds switch (cmd) { 10131da177e4SLinus Torvalds case SIOCGETTUNNEL: 10141da177e4SLinus Torvalds t = NULL; 10157daa0004SPavel Emelyanov if (dev == ign->fb_tunnel_dev) { 10161da177e4SLinus Torvalds if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) { 10171da177e4SLinus Torvalds err = -EFAULT; 10181da177e4SLinus Torvalds break; 10191da177e4SLinus Torvalds } 1020f57e7d5aSPavel Emelyanov t = ipgre_tunnel_locate(net, &p, 0); 10211da177e4SLinus Torvalds } 10221da177e4SLinus Torvalds if (t == NULL) 10232941a486SPatrick McHardy t = netdev_priv(dev); 10241da177e4SLinus Torvalds memcpy(&p, &t->parms, sizeof(p)); 10251da177e4SLinus Torvalds if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) 10261da177e4SLinus Torvalds err = -EFAULT; 10271da177e4SLinus Torvalds break; 10281da177e4SLinus Torvalds 10291da177e4SLinus Torvalds case SIOCADDTUNNEL: 10301da177e4SLinus Torvalds case SIOCCHGTUNNEL: 10311da177e4SLinus Torvalds err = -EPERM; 10321da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 10331da177e4SLinus Torvalds goto done; 10341da177e4SLinus Torvalds 10351da177e4SLinus Torvalds err = -EFAULT; 10361da177e4SLinus Torvalds if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) 10371da177e4SLinus Torvalds goto done; 10381da177e4SLinus Torvalds 10391da177e4SLinus Torvalds err = -EINVAL; 10401da177e4SLinus Torvalds if (p.iph.version != 4 || p.iph.protocol != IPPROTO_GRE || 10411da177e4SLinus Torvalds p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)) || 10421da177e4SLinus Torvalds ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))) 10431da177e4SLinus Torvalds goto done; 10441da177e4SLinus Torvalds if (p.iph.ttl) 10451da177e4SLinus Torvalds p.iph.frag_off |= htons(IP_DF); 10461da177e4SLinus Torvalds 10471da177e4SLinus Torvalds if (!(p.i_flags&GRE_KEY)) 10481da177e4SLinus Torvalds p.i_key = 0; 10491da177e4SLinus Torvalds if (!(p.o_flags&GRE_KEY)) 10501da177e4SLinus Torvalds p.o_key = 0; 10511da177e4SLinus Torvalds 1052f57e7d5aSPavel Emelyanov t = ipgre_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL); 10531da177e4SLinus Torvalds 10547daa0004SPavel Emelyanov if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) { 10551da177e4SLinus Torvalds if (t != NULL) { 10561da177e4SLinus Torvalds if (t->dev != dev) { 10571da177e4SLinus Torvalds err = -EEXIST; 10581da177e4SLinus Torvalds break; 10591da177e4SLinus Torvalds } 10601da177e4SLinus Torvalds } else { 10611507850bSEric Dumazet unsigned int nflags = 0; 10621da177e4SLinus Torvalds 10632941a486SPatrick McHardy t = netdev_priv(dev); 10641da177e4SLinus Torvalds 1065f97c1e0cSJoe Perches if (ipv4_is_multicast(p.iph.daddr)) 10661da177e4SLinus Torvalds nflags = IFF_BROADCAST; 10671da177e4SLinus Torvalds else if (p.iph.daddr) 10681da177e4SLinus Torvalds nflags = IFF_POINTOPOINT; 10691da177e4SLinus Torvalds 10701da177e4SLinus Torvalds if ((dev->flags^nflags)&(IFF_POINTOPOINT|IFF_BROADCAST)) { 10711da177e4SLinus Torvalds err = -EINVAL; 10721da177e4SLinus Torvalds break; 10731da177e4SLinus Torvalds } 1074f57e7d5aSPavel Emelyanov ipgre_tunnel_unlink(ign, t); 10751da177e4SLinus Torvalds t->parms.iph.saddr = p.iph.saddr; 10761da177e4SLinus Torvalds t->parms.iph.daddr = p.iph.daddr; 10771da177e4SLinus Torvalds t->parms.i_key = p.i_key; 10781da177e4SLinus Torvalds t->parms.o_key = p.o_key; 10791da177e4SLinus Torvalds memcpy(dev->dev_addr, &p.iph.saddr, 4); 10801da177e4SLinus Torvalds memcpy(dev->broadcast, &p.iph.daddr, 4); 1081f57e7d5aSPavel Emelyanov ipgre_tunnel_link(ign, t); 10821da177e4SLinus Torvalds netdev_state_change(dev); 10831da177e4SLinus Torvalds } 10841da177e4SLinus Torvalds } 10851da177e4SLinus Torvalds 10861da177e4SLinus Torvalds if (t) { 10871da177e4SLinus Torvalds err = 0; 10881da177e4SLinus Torvalds if (cmd == SIOCCHGTUNNEL) { 10891da177e4SLinus Torvalds t->parms.iph.ttl = p.iph.ttl; 10901da177e4SLinus Torvalds t->parms.iph.tos = p.iph.tos; 10911da177e4SLinus Torvalds t->parms.iph.frag_off = p.iph.frag_off; 1092ee34c1ebSMichal Schmidt if (t->parms.link != p.link) { 1093ee34c1ebSMichal Schmidt t->parms.link = p.link; 109442aa9162SHerbert Xu dev->mtu = ipgre_tunnel_bind_dev(dev); 1095ee34c1ebSMichal Schmidt netdev_state_change(dev); 1096ee34c1ebSMichal Schmidt } 10971da177e4SLinus Torvalds } 10981da177e4SLinus Torvalds if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p))) 10991da177e4SLinus Torvalds err = -EFAULT; 11001da177e4SLinus Torvalds } else 11011da177e4SLinus Torvalds err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT); 11021da177e4SLinus Torvalds break; 11031da177e4SLinus Torvalds 11041da177e4SLinus Torvalds case SIOCDELTUNNEL: 11051da177e4SLinus Torvalds err = -EPERM; 11061da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 11071da177e4SLinus Torvalds goto done; 11081da177e4SLinus Torvalds 11097daa0004SPavel Emelyanov if (dev == ign->fb_tunnel_dev) { 11101da177e4SLinus Torvalds err = -EFAULT; 11111da177e4SLinus Torvalds if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) 11121da177e4SLinus Torvalds goto done; 11131da177e4SLinus Torvalds err = -ENOENT; 1114f57e7d5aSPavel Emelyanov if ((t = ipgre_tunnel_locate(net, &p, 0)) == NULL) 11151da177e4SLinus Torvalds goto done; 11161da177e4SLinus Torvalds err = -EPERM; 11177daa0004SPavel Emelyanov if (t == netdev_priv(ign->fb_tunnel_dev)) 11181da177e4SLinus Torvalds goto done; 11191da177e4SLinus Torvalds dev = t->dev; 11201da177e4SLinus Torvalds } 112122f8cde5SStephen Hemminger unregister_netdevice(dev); 112222f8cde5SStephen Hemminger err = 0; 11231da177e4SLinus Torvalds break; 11241da177e4SLinus Torvalds 11251da177e4SLinus Torvalds default: 11261da177e4SLinus Torvalds err = -EINVAL; 11271da177e4SLinus Torvalds } 11281da177e4SLinus Torvalds 11291da177e4SLinus Torvalds done: 11301da177e4SLinus Torvalds return err; 11311da177e4SLinus Torvalds } 11321da177e4SLinus Torvalds 11331da177e4SLinus Torvalds static int ipgre_tunnel_change_mtu(struct net_device *dev, int new_mtu) 11341da177e4SLinus Torvalds { 11352941a486SPatrick McHardy struct ip_tunnel *tunnel = netdev_priv(dev); 1136c95b819aSHerbert Xu if (new_mtu < 68 || 1137c95b819aSHerbert Xu new_mtu > 0xFFF8 - dev->hard_header_len - tunnel->hlen) 11381da177e4SLinus Torvalds return -EINVAL; 11391da177e4SLinus Torvalds dev->mtu = new_mtu; 11401da177e4SLinus Torvalds return 0; 11411da177e4SLinus Torvalds } 11421da177e4SLinus Torvalds 11431da177e4SLinus Torvalds /* Nice toy. Unfortunately, useless in real life :-) 11441da177e4SLinus Torvalds It allows to construct virtual multiprotocol broadcast "LAN" 11451da177e4SLinus Torvalds over the Internet, provided multicast routing is tuned. 11461da177e4SLinus Torvalds 11471da177e4SLinus Torvalds 11481da177e4SLinus Torvalds I have no idea was this bicycle invented before me, 11491da177e4SLinus Torvalds so that I had to set ARPHRD_IPGRE to a random value. 11501da177e4SLinus Torvalds I have an impression, that Cisco could make something similar, 11511da177e4SLinus Torvalds but this feature is apparently missing in IOS<=11.2(8). 11521da177e4SLinus Torvalds 11531da177e4SLinus Torvalds I set up 10.66.66/24 and fec0:6666:6666::0/96 as virtual networks 11541da177e4SLinus Torvalds with broadcast 224.66.66.66. If you have access to mbone, play with me :-) 11551da177e4SLinus Torvalds 11561da177e4SLinus Torvalds ping -t 255 224.66.66.66 11571da177e4SLinus Torvalds 11581da177e4SLinus Torvalds If nobody answers, mbone does not work. 11591da177e4SLinus Torvalds 11601da177e4SLinus Torvalds ip tunnel add Universe mode gre remote 224.66.66.66 local <Your_real_addr> ttl 255 11611da177e4SLinus Torvalds ip addr add 10.66.66.<somewhat>/24 dev Universe 11621da177e4SLinus Torvalds ifconfig Universe up 11631da177e4SLinus Torvalds ifconfig Universe add fe80::<Your_real_addr>/10 11641da177e4SLinus Torvalds ifconfig Universe add fec0:6666:6666::<Your_real_addr>/96 11651da177e4SLinus Torvalds ftp 10.66.66.66 11661da177e4SLinus Torvalds ... 11671da177e4SLinus Torvalds ftp fec0:6666:6666::193.233.7.65 11681da177e4SLinus Torvalds ... 11691da177e4SLinus Torvalds 11701da177e4SLinus Torvalds */ 11711da177e4SLinus Torvalds 11723b04dddeSStephen Hemminger static int ipgre_header(struct sk_buff *skb, struct net_device *dev, 11733b04dddeSStephen Hemminger unsigned short type, 11741507850bSEric Dumazet const void *daddr, const void *saddr, unsigned int len) 11751da177e4SLinus Torvalds { 11762941a486SPatrick McHardy struct ip_tunnel *t = netdev_priv(dev); 11771da177e4SLinus Torvalds struct iphdr *iph = (struct iphdr *)skb_push(skb, t->hlen); 1178d5a0a1e3SAl Viro __be16 *p = (__be16*)(iph+1); 11791da177e4SLinus Torvalds 11801da177e4SLinus Torvalds memcpy(iph, &t->parms.iph, sizeof(struct iphdr)); 11811da177e4SLinus Torvalds p[0] = t->parms.o_flags; 11821da177e4SLinus Torvalds p[1] = htons(type); 11831da177e4SLinus Torvalds 11841da177e4SLinus Torvalds /* 11851da177e4SLinus Torvalds * Set the source hardware address. 11861da177e4SLinus Torvalds */ 11871da177e4SLinus Torvalds 11881da177e4SLinus Torvalds if (saddr) 11891da177e4SLinus Torvalds memcpy(&iph->saddr, saddr, 4); 11906d55cb91STimo Teräs if (daddr) 11911da177e4SLinus Torvalds memcpy(&iph->daddr, daddr, 4); 11926d55cb91STimo Teräs if (iph->daddr) 11931da177e4SLinus Torvalds return t->hlen; 11941da177e4SLinus Torvalds 11951da177e4SLinus Torvalds return -t->hlen; 11961da177e4SLinus Torvalds } 11971da177e4SLinus Torvalds 11986a5f44d7STimo Teras static int ipgre_header_parse(const struct sk_buff *skb, unsigned char *haddr) 11996a5f44d7STimo Teras { 12006a5f44d7STimo Teras struct iphdr *iph = (struct iphdr *) skb_mac_header(skb); 12016a5f44d7STimo Teras memcpy(haddr, &iph->saddr, 4); 12026a5f44d7STimo Teras return 4; 12036a5f44d7STimo Teras } 12046a5f44d7STimo Teras 12053b04dddeSStephen Hemminger static const struct header_ops ipgre_header_ops = { 12063b04dddeSStephen Hemminger .create = ipgre_header, 12076a5f44d7STimo Teras .parse = ipgre_header_parse, 12083b04dddeSStephen Hemminger }; 12093b04dddeSStephen Hemminger 12106a5f44d7STimo Teras #ifdef CONFIG_NET_IPGRE_BROADCAST 12111da177e4SLinus Torvalds static int ipgre_open(struct net_device *dev) 12121da177e4SLinus Torvalds { 12132941a486SPatrick McHardy struct ip_tunnel *t = netdev_priv(dev); 12141da177e4SLinus Torvalds 1215f97c1e0cSJoe Perches if (ipv4_is_multicast(t->parms.iph.daddr)) { 1216e985aad7SEric Dumazet struct flowi fl = { 1217e985aad7SEric Dumazet .oif = t->parms.link, 1218e985aad7SEric Dumazet .nl_u = { 1219e985aad7SEric Dumazet .ip4_u = { 1220e985aad7SEric Dumazet .daddr = t->parms.iph.daddr, 12211da177e4SLinus Torvalds .saddr = t->parms.iph.saddr, 1222e985aad7SEric Dumazet .tos = RT_TOS(t->parms.iph.tos) 1223e985aad7SEric Dumazet } 1224e985aad7SEric Dumazet }, 1225e985aad7SEric Dumazet .proto = IPPROTO_GRE 1226e985aad7SEric Dumazet }; 12271da177e4SLinus Torvalds struct rtable *rt; 1228e985aad7SEric Dumazet 122996635522SPavel Emelyanov if (ip_route_output_key(dev_net(dev), &rt, &fl)) 12301da177e4SLinus Torvalds return -EADDRNOTAVAIL; 1231d8d1f30bSChangli Gao dev = rt->dst.dev; 12321da177e4SLinus Torvalds ip_rt_put(rt); 1233e5ed6399SHerbert Xu if (__in_dev_get_rtnl(dev) == NULL) 12341da177e4SLinus Torvalds return -EADDRNOTAVAIL; 12351da177e4SLinus Torvalds t->mlink = dev->ifindex; 1236e5ed6399SHerbert Xu ip_mc_inc_group(__in_dev_get_rtnl(dev), t->parms.iph.daddr); 12371da177e4SLinus Torvalds } 12381da177e4SLinus Torvalds return 0; 12391da177e4SLinus Torvalds } 12401da177e4SLinus Torvalds 12411da177e4SLinus Torvalds static int ipgre_close(struct net_device *dev) 12421da177e4SLinus Torvalds { 12432941a486SPatrick McHardy struct ip_tunnel *t = netdev_priv(dev); 1244b8c26a33SStephen Hemminger 1245f97c1e0cSJoe Perches if (ipv4_is_multicast(t->parms.iph.daddr) && t->mlink) { 12467fee0ca2SDenis V. Lunev struct in_device *in_dev; 1247c346dca1SYOSHIFUJI Hideaki in_dev = inetdev_by_index(dev_net(dev), t->mlink); 1248*8723e1b4SEric Dumazet if (in_dev) 12491da177e4SLinus Torvalds ip_mc_dec_group(in_dev, t->parms.iph.daddr); 12501da177e4SLinus Torvalds } 12511da177e4SLinus Torvalds return 0; 12521da177e4SLinus Torvalds } 12531da177e4SLinus Torvalds 12541da177e4SLinus Torvalds #endif 12551da177e4SLinus Torvalds 1256b8c26a33SStephen Hemminger static const struct net_device_ops ipgre_netdev_ops = { 1257b8c26a33SStephen Hemminger .ndo_init = ipgre_tunnel_init, 1258b8c26a33SStephen Hemminger .ndo_uninit = ipgre_tunnel_uninit, 1259b8c26a33SStephen Hemminger #ifdef CONFIG_NET_IPGRE_BROADCAST 1260b8c26a33SStephen Hemminger .ndo_open = ipgre_open, 1261b8c26a33SStephen Hemminger .ndo_stop = ipgre_close, 1262b8c26a33SStephen Hemminger #endif 1263b8c26a33SStephen Hemminger .ndo_start_xmit = ipgre_tunnel_xmit, 1264b8c26a33SStephen Hemminger .ndo_do_ioctl = ipgre_tunnel_ioctl, 1265b8c26a33SStephen Hemminger .ndo_change_mtu = ipgre_tunnel_change_mtu, 1266e985aad7SEric Dumazet .ndo_get_stats = ipgre_get_stats, 1267b8c26a33SStephen Hemminger }; 1268b8c26a33SStephen Hemminger 1269e985aad7SEric Dumazet static void ipgre_dev_free(struct net_device *dev) 1270e985aad7SEric Dumazet { 1271e985aad7SEric Dumazet free_percpu(dev->tstats); 1272e985aad7SEric Dumazet free_netdev(dev); 1273e985aad7SEric Dumazet } 1274e985aad7SEric Dumazet 12751da177e4SLinus Torvalds static void ipgre_tunnel_setup(struct net_device *dev) 12761da177e4SLinus Torvalds { 1277b8c26a33SStephen Hemminger dev->netdev_ops = &ipgre_netdev_ops; 1278e985aad7SEric Dumazet dev->destructor = ipgre_dev_free; 12791da177e4SLinus Torvalds 12801da177e4SLinus Torvalds dev->type = ARPHRD_IPGRE; 1281c95b819aSHerbert Xu dev->needed_headroom = LL_MAX_HEADER + sizeof(struct iphdr) + 4; 128246f25dffSKris Katterjohn dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 4; 12831da177e4SLinus Torvalds dev->flags = IFF_NOARP; 12841da177e4SLinus Torvalds dev->iflink = 0; 12851da177e4SLinus Torvalds dev->addr_len = 4; 12860b67ecebSPavel Emelyanov dev->features |= NETIF_F_NETNS_LOCAL; 1287108bfa89SEric Dumazet dev->priv_flags &= ~IFF_XMIT_DST_RELEASE; 12881da177e4SLinus Torvalds } 12891da177e4SLinus Torvalds 12901da177e4SLinus Torvalds static int ipgre_tunnel_init(struct net_device *dev) 12911da177e4SLinus Torvalds { 12921da177e4SLinus Torvalds struct ip_tunnel *tunnel; 12931da177e4SLinus Torvalds struct iphdr *iph; 12941da177e4SLinus Torvalds 12952941a486SPatrick McHardy tunnel = netdev_priv(dev); 12961da177e4SLinus Torvalds iph = &tunnel->parms.iph; 12971da177e4SLinus Torvalds 12981da177e4SLinus Torvalds tunnel->dev = dev; 12991da177e4SLinus Torvalds strcpy(tunnel->parms.name, dev->name); 13001da177e4SLinus Torvalds 13011da177e4SLinus Torvalds memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4); 13021da177e4SLinus Torvalds memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4); 13031da177e4SLinus Torvalds 13041da177e4SLinus Torvalds if (iph->daddr) { 13051da177e4SLinus Torvalds #ifdef CONFIG_NET_IPGRE_BROADCAST 1306f97c1e0cSJoe Perches if (ipv4_is_multicast(iph->daddr)) { 13071da177e4SLinus Torvalds if (!iph->saddr) 13081da177e4SLinus Torvalds return -EINVAL; 13091da177e4SLinus Torvalds dev->flags = IFF_BROADCAST; 13103b04dddeSStephen Hemminger dev->header_ops = &ipgre_header_ops; 13111da177e4SLinus Torvalds } 13121da177e4SLinus Torvalds #endif 1313ee34c1ebSMichal Schmidt } else 13146a5f44d7STimo Teras dev->header_ops = &ipgre_header_ops; 13151da177e4SLinus Torvalds 1316e985aad7SEric Dumazet dev->tstats = alloc_percpu(struct pcpu_tstats); 1317e985aad7SEric Dumazet if (!dev->tstats) 1318e985aad7SEric Dumazet return -ENOMEM; 1319e985aad7SEric Dumazet 13201da177e4SLinus Torvalds return 0; 13211da177e4SLinus Torvalds } 13221da177e4SLinus Torvalds 1323b8c26a33SStephen Hemminger static void ipgre_fb_tunnel_init(struct net_device *dev) 13241da177e4SLinus Torvalds { 13252941a486SPatrick McHardy struct ip_tunnel *tunnel = netdev_priv(dev); 13261da177e4SLinus Torvalds struct iphdr *iph = &tunnel->parms.iph; 1327eb8ce741SPavel Emelyanov struct ipgre_net *ign = net_generic(dev_net(dev), ipgre_net_id); 13281da177e4SLinus Torvalds 13291da177e4SLinus Torvalds tunnel->dev = dev; 13301da177e4SLinus Torvalds strcpy(tunnel->parms.name, dev->name); 13311da177e4SLinus Torvalds 13321da177e4SLinus Torvalds iph->version = 4; 13331da177e4SLinus Torvalds iph->protocol = IPPROTO_GRE; 13341da177e4SLinus Torvalds iph->ihl = 5; 13351da177e4SLinus Torvalds tunnel->hlen = sizeof(struct iphdr) + 4; 13361da177e4SLinus Torvalds 13371da177e4SLinus Torvalds dev_hold(dev); 13381507850bSEric Dumazet rcu_assign_pointer(ign->tunnels_wc[0], tunnel); 13391da177e4SLinus Torvalds } 13401da177e4SLinus Torvalds 13411da177e4SLinus Torvalds 134200959adeSDmitry Kozlov static const struct gre_protocol ipgre_protocol = { 13431da177e4SLinus Torvalds .handler = ipgre_rcv, 13441da177e4SLinus Torvalds .err_handler = ipgre_err, 13451da177e4SLinus Torvalds }; 13461da177e4SLinus Torvalds 1347eef6dd65SEric Dumazet static void ipgre_destroy_tunnels(struct ipgre_net *ign, struct list_head *head) 1348eb8ce741SPavel Emelyanov { 1349eb8ce741SPavel Emelyanov int prio; 1350eb8ce741SPavel Emelyanov 1351eb8ce741SPavel Emelyanov for (prio = 0; prio < 4; prio++) { 1352eb8ce741SPavel Emelyanov int h; 1353eb8ce741SPavel Emelyanov for (h = 0; h < HASH_SIZE; h++) { 13541507850bSEric Dumazet struct ip_tunnel *t; 13551507850bSEric Dumazet 13561507850bSEric Dumazet t = rtnl_dereference(ign->tunnels[prio][h]); 1357eef6dd65SEric Dumazet 1358eef6dd65SEric Dumazet while (t != NULL) { 1359eef6dd65SEric Dumazet unregister_netdevice_queue(t->dev, head); 13601507850bSEric Dumazet t = rtnl_dereference(t->next); 1361eef6dd65SEric Dumazet } 1362eb8ce741SPavel Emelyanov } 1363eb8ce741SPavel Emelyanov } 1364eb8ce741SPavel Emelyanov } 1365eb8ce741SPavel Emelyanov 13662c8c1e72SAlexey Dobriyan static int __net_init ipgre_init_net(struct net *net) 136759a4c759SPavel Emelyanov { 1368cfb8fbf2SEric W. Biederman struct ipgre_net *ign = net_generic(net, ipgre_net_id); 136959a4c759SPavel Emelyanov int err; 137059a4c759SPavel Emelyanov 13717daa0004SPavel Emelyanov ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "gre0", 13727daa0004SPavel Emelyanov ipgre_tunnel_setup); 13737daa0004SPavel Emelyanov if (!ign->fb_tunnel_dev) { 13747daa0004SPavel Emelyanov err = -ENOMEM; 13757daa0004SPavel Emelyanov goto err_alloc_dev; 13767daa0004SPavel Emelyanov } 1377be77e593SAlexey Dobriyan dev_net_set(ign->fb_tunnel_dev, net); 13787daa0004SPavel Emelyanov 1379b8c26a33SStephen Hemminger ipgre_fb_tunnel_init(ign->fb_tunnel_dev); 1380c19e654dSHerbert Xu ign->fb_tunnel_dev->rtnl_link_ops = &ipgre_link_ops; 13817daa0004SPavel Emelyanov 13827daa0004SPavel Emelyanov if ((err = register_netdev(ign->fb_tunnel_dev))) 13837daa0004SPavel Emelyanov goto err_reg_dev; 13847daa0004SPavel Emelyanov 138559a4c759SPavel Emelyanov return 0; 138659a4c759SPavel Emelyanov 13877daa0004SPavel Emelyanov err_reg_dev: 13887daa0004SPavel Emelyanov free_netdev(ign->fb_tunnel_dev); 13897daa0004SPavel Emelyanov err_alloc_dev: 139059a4c759SPavel Emelyanov return err; 139159a4c759SPavel Emelyanov } 139259a4c759SPavel Emelyanov 13932c8c1e72SAlexey Dobriyan static void __net_exit ipgre_exit_net(struct net *net) 139459a4c759SPavel Emelyanov { 139559a4c759SPavel Emelyanov struct ipgre_net *ign; 1396eef6dd65SEric Dumazet LIST_HEAD(list); 139759a4c759SPavel Emelyanov 139859a4c759SPavel Emelyanov ign = net_generic(net, ipgre_net_id); 13997daa0004SPavel Emelyanov rtnl_lock(); 1400eef6dd65SEric Dumazet ipgre_destroy_tunnels(ign, &list); 1401eef6dd65SEric Dumazet unregister_netdevice_many(&list); 14027daa0004SPavel Emelyanov rtnl_unlock(); 140359a4c759SPavel Emelyanov } 140459a4c759SPavel Emelyanov 140559a4c759SPavel Emelyanov static struct pernet_operations ipgre_net_ops = { 140659a4c759SPavel Emelyanov .init = ipgre_init_net, 140759a4c759SPavel Emelyanov .exit = ipgre_exit_net, 1408cfb8fbf2SEric W. Biederman .id = &ipgre_net_id, 1409cfb8fbf2SEric W. Biederman .size = sizeof(struct ipgre_net), 141059a4c759SPavel Emelyanov }; 14111da177e4SLinus Torvalds 1412c19e654dSHerbert Xu static int ipgre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[]) 1413c19e654dSHerbert Xu { 1414c19e654dSHerbert Xu __be16 flags; 1415c19e654dSHerbert Xu 1416c19e654dSHerbert Xu if (!data) 1417c19e654dSHerbert Xu return 0; 1418c19e654dSHerbert Xu 1419c19e654dSHerbert Xu flags = 0; 1420c19e654dSHerbert Xu if (data[IFLA_GRE_IFLAGS]) 1421c19e654dSHerbert Xu flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]); 1422c19e654dSHerbert Xu if (data[IFLA_GRE_OFLAGS]) 1423c19e654dSHerbert Xu flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]); 1424c19e654dSHerbert Xu if (flags & (GRE_VERSION|GRE_ROUTING)) 1425c19e654dSHerbert Xu return -EINVAL; 1426c19e654dSHerbert Xu 1427c19e654dSHerbert Xu return 0; 1428c19e654dSHerbert Xu } 1429c19e654dSHerbert Xu 1430e1a80002SHerbert Xu static int ipgre_tap_validate(struct nlattr *tb[], struct nlattr *data[]) 1431e1a80002SHerbert Xu { 1432e1a80002SHerbert Xu __be32 daddr; 1433e1a80002SHerbert Xu 1434e1a80002SHerbert Xu if (tb[IFLA_ADDRESS]) { 1435e1a80002SHerbert Xu if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) 1436e1a80002SHerbert Xu return -EINVAL; 1437e1a80002SHerbert Xu if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) 1438e1a80002SHerbert Xu return -EADDRNOTAVAIL; 1439e1a80002SHerbert Xu } 1440e1a80002SHerbert Xu 1441e1a80002SHerbert Xu if (!data) 1442e1a80002SHerbert Xu goto out; 1443e1a80002SHerbert Xu 1444e1a80002SHerbert Xu if (data[IFLA_GRE_REMOTE]) { 1445e1a80002SHerbert Xu memcpy(&daddr, nla_data(data[IFLA_GRE_REMOTE]), 4); 1446e1a80002SHerbert Xu if (!daddr) 1447e1a80002SHerbert Xu return -EINVAL; 1448e1a80002SHerbert Xu } 1449e1a80002SHerbert Xu 1450e1a80002SHerbert Xu out: 1451e1a80002SHerbert Xu return ipgre_tunnel_validate(tb, data); 1452e1a80002SHerbert Xu } 1453e1a80002SHerbert Xu 1454c19e654dSHerbert Xu static void ipgre_netlink_parms(struct nlattr *data[], 1455c19e654dSHerbert Xu struct ip_tunnel_parm *parms) 1456c19e654dSHerbert Xu { 14577bb82d92SHerbert Xu memset(parms, 0, sizeof(*parms)); 1458c19e654dSHerbert Xu 1459c19e654dSHerbert Xu parms->iph.protocol = IPPROTO_GRE; 1460c19e654dSHerbert Xu 1461c19e654dSHerbert Xu if (!data) 1462c19e654dSHerbert Xu return; 1463c19e654dSHerbert Xu 1464c19e654dSHerbert Xu if (data[IFLA_GRE_LINK]) 1465c19e654dSHerbert Xu parms->link = nla_get_u32(data[IFLA_GRE_LINK]); 1466c19e654dSHerbert Xu 1467c19e654dSHerbert Xu if (data[IFLA_GRE_IFLAGS]) 1468c19e654dSHerbert Xu parms->i_flags = nla_get_be16(data[IFLA_GRE_IFLAGS]); 1469c19e654dSHerbert Xu 1470c19e654dSHerbert Xu if (data[IFLA_GRE_OFLAGS]) 1471c19e654dSHerbert Xu parms->o_flags = nla_get_be16(data[IFLA_GRE_OFLAGS]); 1472c19e654dSHerbert Xu 1473c19e654dSHerbert Xu if (data[IFLA_GRE_IKEY]) 1474c19e654dSHerbert Xu parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]); 1475c19e654dSHerbert Xu 1476c19e654dSHerbert Xu if (data[IFLA_GRE_OKEY]) 1477c19e654dSHerbert Xu parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]); 1478c19e654dSHerbert Xu 1479c19e654dSHerbert Xu if (data[IFLA_GRE_LOCAL]) 14804d74f8baSPatrick McHardy parms->iph.saddr = nla_get_be32(data[IFLA_GRE_LOCAL]); 1481c19e654dSHerbert Xu 1482c19e654dSHerbert Xu if (data[IFLA_GRE_REMOTE]) 14834d74f8baSPatrick McHardy parms->iph.daddr = nla_get_be32(data[IFLA_GRE_REMOTE]); 1484c19e654dSHerbert Xu 1485c19e654dSHerbert Xu if (data[IFLA_GRE_TTL]) 1486c19e654dSHerbert Xu parms->iph.ttl = nla_get_u8(data[IFLA_GRE_TTL]); 1487c19e654dSHerbert Xu 1488c19e654dSHerbert Xu if (data[IFLA_GRE_TOS]) 1489c19e654dSHerbert Xu parms->iph.tos = nla_get_u8(data[IFLA_GRE_TOS]); 1490c19e654dSHerbert Xu 1491c19e654dSHerbert Xu if (!data[IFLA_GRE_PMTUDISC] || nla_get_u8(data[IFLA_GRE_PMTUDISC])) 1492c19e654dSHerbert Xu parms->iph.frag_off = htons(IP_DF); 1493c19e654dSHerbert Xu } 1494c19e654dSHerbert Xu 1495e1a80002SHerbert Xu static int ipgre_tap_init(struct net_device *dev) 1496e1a80002SHerbert Xu { 1497e1a80002SHerbert Xu struct ip_tunnel *tunnel; 1498e1a80002SHerbert Xu 1499e1a80002SHerbert Xu tunnel = netdev_priv(dev); 1500e1a80002SHerbert Xu 1501e1a80002SHerbert Xu tunnel->dev = dev; 1502e1a80002SHerbert Xu strcpy(tunnel->parms.name, dev->name); 1503e1a80002SHerbert Xu 1504e1a80002SHerbert Xu ipgre_tunnel_bind_dev(dev); 1505e1a80002SHerbert Xu 1506e985aad7SEric Dumazet dev->tstats = alloc_percpu(struct pcpu_tstats); 1507e985aad7SEric Dumazet if (!dev->tstats) 1508e985aad7SEric Dumazet return -ENOMEM; 1509e985aad7SEric Dumazet 1510e1a80002SHerbert Xu return 0; 1511e1a80002SHerbert Xu } 1512e1a80002SHerbert Xu 1513b8c26a33SStephen Hemminger static const struct net_device_ops ipgre_tap_netdev_ops = { 1514b8c26a33SStephen Hemminger .ndo_init = ipgre_tap_init, 1515b8c26a33SStephen Hemminger .ndo_uninit = ipgre_tunnel_uninit, 1516b8c26a33SStephen Hemminger .ndo_start_xmit = ipgre_tunnel_xmit, 1517b8c26a33SStephen Hemminger .ndo_set_mac_address = eth_mac_addr, 1518b8c26a33SStephen Hemminger .ndo_validate_addr = eth_validate_addr, 1519b8c26a33SStephen Hemminger .ndo_change_mtu = ipgre_tunnel_change_mtu, 1520e985aad7SEric Dumazet .ndo_get_stats = ipgre_get_stats, 1521b8c26a33SStephen Hemminger }; 1522b8c26a33SStephen Hemminger 1523e1a80002SHerbert Xu static void ipgre_tap_setup(struct net_device *dev) 1524e1a80002SHerbert Xu { 1525e1a80002SHerbert Xu 1526e1a80002SHerbert Xu ether_setup(dev); 1527e1a80002SHerbert Xu 15282e9526b3SHerbert Xu dev->netdev_ops = &ipgre_tap_netdev_ops; 1529e985aad7SEric Dumazet dev->destructor = ipgre_dev_free; 1530e1a80002SHerbert Xu 1531e1a80002SHerbert Xu dev->iflink = 0; 1532e1a80002SHerbert Xu dev->features |= NETIF_F_NETNS_LOCAL; 1533e1a80002SHerbert Xu } 1534e1a80002SHerbert Xu 153581adee47SEric W. Biederman static int ipgre_newlink(struct net *src_net, struct net_device *dev, struct nlattr *tb[], 1536c19e654dSHerbert Xu struct nlattr *data[]) 1537c19e654dSHerbert Xu { 1538c19e654dSHerbert Xu struct ip_tunnel *nt; 1539c19e654dSHerbert Xu struct net *net = dev_net(dev); 1540c19e654dSHerbert Xu struct ipgre_net *ign = net_generic(net, ipgre_net_id); 1541c19e654dSHerbert Xu int mtu; 1542c19e654dSHerbert Xu int err; 1543c19e654dSHerbert Xu 1544c19e654dSHerbert Xu nt = netdev_priv(dev); 1545c19e654dSHerbert Xu ipgre_netlink_parms(data, &nt->parms); 1546c19e654dSHerbert Xu 1547e1a80002SHerbert Xu if (ipgre_tunnel_find(net, &nt->parms, dev->type)) 1548c19e654dSHerbert Xu return -EEXIST; 1549c19e654dSHerbert Xu 1550e1a80002SHerbert Xu if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS]) 1551e1a80002SHerbert Xu random_ether_addr(dev->dev_addr); 1552e1a80002SHerbert Xu 1553c19e654dSHerbert Xu mtu = ipgre_tunnel_bind_dev(dev); 1554c19e654dSHerbert Xu if (!tb[IFLA_MTU]) 1555c19e654dSHerbert Xu dev->mtu = mtu; 1556c19e654dSHerbert Xu 1557b790e01aSEric Dumazet /* Can use a lockless transmit, unless we generate output sequences */ 1558b790e01aSEric Dumazet if (!(nt->parms.o_flags & GRE_SEQ)) 1559b790e01aSEric Dumazet dev->features |= NETIF_F_LLTX; 1560b790e01aSEric Dumazet 1561c19e654dSHerbert Xu err = register_netdevice(dev); 1562c19e654dSHerbert Xu if (err) 1563c19e654dSHerbert Xu goto out; 1564c19e654dSHerbert Xu 1565c19e654dSHerbert Xu dev_hold(dev); 1566c19e654dSHerbert Xu ipgre_tunnel_link(ign, nt); 1567c19e654dSHerbert Xu 1568c19e654dSHerbert Xu out: 1569c19e654dSHerbert Xu return err; 1570c19e654dSHerbert Xu } 1571c19e654dSHerbert Xu 1572c19e654dSHerbert Xu static int ipgre_changelink(struct net_device *dev, struct nlattr *tb[], 1573c19e654dSHerbert Xu struct nlattr *data[]) 1574c19e654dSHerbert Xu { 1575c19e654dSHerbert Xu struct ip_tunnel *t, *nt; 1576c19e654dSHerbert Xu struct net *net = dev_net(dev); 1577c19e654dSHerbert Xu struct ipgre_net *ign = net_generic(net, ipgre_net_id); 1578c19e654dSHerbert Xu struct ip_tunnel_parm p; 1579c19e654dSHerbert Xu int mtu; 1580c19e654dSHerbert Xu 1581c19e654dSHerbert Xu if (dev == ign->fb_tunnel_dev) 1582c19e654dSHerbert Xu return -EINVAL; 1583c19e654dSHerbert Xu 1584c19e654dSHerbert Xu nt = netdev_priv(dev); 1585c19e654dSHerbert Xu ipgre_netlink_parms(data, &p); 1586c19e654dSHerbert Xu 1587c19e654dSHerbert Xu t = ipgre_tunnel_locate(net, &p, 0); 1588c19e654dSHerbert Xu 1589c19e654dSHerbert Xu if (t) { 1590c19e654dSHerbert Xu if (t->dev != dev) 1591c19e654dSHerbert Xu return -EEXIST; 1592c19e654dSHerbert Xu } else { 1593c19e654dSHerbert Xu t = nt; 1594c19e654dSHerbert Xu 15952e9526b3SHerbert Xu if (dev->type != ARPHRD_ETHER) { 15961507850bSEric Dumazet unsigned int nflags = 0; 15972e9526b3SHerbert Xu 1598c19e654dSHerbert Xu if (ipv4_is_multicast(p.iph.daddr)) 1599c19e654dSHerbert Xu nflags = IFF_BROADCAST; 1600c19e654dSHerbert Xu else if (p.iph.daddr) 1601c19e654dSHerbert Xu nflags = IFF_POINTOPOINT; 1602c19e654dSHerbert Xu 1603c19e654dSHerbert Xu if ((dev->flags ^ nflags) & 1604c19e654dSHerbert Xu (IFF_POINTOPOINT | IFF_BROADCAST)) 1605c19e654dSHerbert Xu return -EINVAL; 16062e9526b3SHerbert Xu } 1607c19e654dSHerbert Xu 1608c19e654dSHerbert Xu ipgre_tunnel_unlink(ign, t); 1609c19e654dSHerbert Xu t->parms.iph.saddr = p.iph.saddr; 1610c19e654dSHerbert Xu t->parms.iph.daddr = p.iph.daddr; 1611c19e654dSHerbert Xu t->parms.i_key = p.i_key; 16122e9526b3SHerbert Xu if (dev->type != ARPHRD_ETHER) { 1613c19e654dSHerbert Xu memcpy(dev->dev_addr, &p.iph.saddr, 4); 1614c19e654dSHerbert Xu memcpy(dev->broadcast, &p.iph.daddr, 4); 16152e9526b3SHerbert Xu } 1616c19e654dSHerbert Xu ipgre_tunnel_link(ign, t); 1617c19e654dSHerbert Xu netdev_state_change(dev); 1618c19e654dSHerbert Xu } 1619c19e654dSHerbert Xu 1620c19e654dSHerbert Xu t->parms.o_key = p.o_key; 1621c19e654dSHerbert Xu t->parms.iph.ttl = p.iph.ttl; 1622c19e654dSHerbert Xu t->parms.iph.tos = p.iph.tos; 1623c19e654dSHerbert Xu t->parms.iph.frag_off = p.iph.frag_off; 1624c19e654dSHerbert Xu 1625c19e654dSHerbert Xu if (t->parms.link != p.link) { 1626c19e654dSHerbert Xu t->parms.link = p.link; 1627c19e654dSHerbert Xu mtu = ipgre_tunnel_bind_dev(dev); 1628c19e654dSHerbert Xu if (!tb[IFLA_MTU]) 1629c19e654dSHerbert Xu dev->mtu = mtu; 1630c19e654dSHerbert Xu netdev_state_change(dev); 1631c19e654dSHerbert Xu } 1632c19e654dSHerbert Xu 1633c19e654dSHerbert Xu return 0; 1634c19e654dSHerbert Xu } 1635c19e654dSHerbert Xu 1636c19e654dSHerbert Xu static size_t ipgre_get_size(const struct net_device *dev) 1637c19e654dSHerbert Xu { 1638c19e654dSHerbert Xu return 1639c19e654dSHerbert Xu /* IFLA_GRE_LINK */ 1640c19e654dSHerbert Xu nla_total_size(4) + 1641c19e654dSHerbert Xu /* IFLA_GRE_IFLAGS */ 1642c19e654dSHerbert Xu nla_total_size(2) + 1643c19e654dSHerbert Xu /* IFLA_GRE_OFLAGS */ 1644c19e654dSHerbert Xu nla_total_size(2) + 1645c19e654dSHerbert Xu /* IFLA_GRE_IKEY */ 1646c19e654dSHerbert Xu nla_total_size(4) + 1647c19e654dSHerbert Xu /* IFLA_GRE_OKEY */ 1648c19e654dSHerbert Xu nla_total_size(4) + 1649c19e654dSHerbert Xu /* IFLA_GRE_LOCAL */ 1650c19e654dSHerbert Xu nla_total_size(4) + 1651c19e654dSHerbert Xu /* IFLA_GRE_REMOTE */ 1652c19e654dSHerbert Xu nla_total_size(4) + 1653c19e654dSHerbert Xu /* IFLA_GRE_TTL */ 1654c19e654dSHerbert Xu nla_total_size(1) + 1655c19e654dSHerbert Xu /* IFLA_GRE_TOS */ 1656c19e654dSHerbert Xu nla_total_size(1) + 1657c19e654dSHerbert Xu /* IFLA_GRE_PMTUDISC */ 1658c19e654dSHerbert Xu nla_total_size(1) + 1659c19e654dSHerbert Xu 0; 1660c19e654dSHerbert Xu } 1661c19e654dSHerbert Xu 1662c19e654dSHerbert Xu static int ipgre_fill_info(struct sk_buff *skb, const struct net_device *dev) 1663c19e654dSHerbert Xu { 1664c19e654dSHerbert Xu struct ip_tunnel *t = netdev_priv(dev); 1665c19e654dSHerbert Xu struct ip_tunnel_parm *p = &t->parms; 1666c19e654dSHerbert Xu 1667c19e654dSHerbert Xu NLA_PUT_U32(skb, IFLA_GRE_LINK, p->link); 1668c19e654dSHerbert Xu NLA_PUT_BE16(skb, IFLA_GRE_IFLAGS, p->i_flags); 1669c19e654dSHerbert Xu NLA_PUT_BE16(skb, IFLA_GRE_OFLAGS, p->o_flags); 1670ba9e64b1SPatrick McHardy NLA_PUT_BE32(skb, IFLA_GRE_IKEY, p->i_key); 1671ba9e64b1SPatrick McHardy NLA_PUT_BE32(skb, IFLA_GRE_OKEY, p->o_key); 16724d74f8baSPatrick McHardy NLA_PUT_BE32(skb, IFLA_GRE_LOCAL, p->iph.saddr); 16734d74f8baSPatrick McHardy NLA_PUT_BE32(skb, IFLA_GRE_REMOTE, p->iph.daddr); 1674c19e654dSHerbert Xu NLA_PUT_U8(skb, IFLA_GRE_TTL, p->iph.ttl); 1675c19e654dSHerbert Xu NLA_PUT_U8(skb, IFLA_GRE_TOS, p->iph.tos); 1676c19e654dSHerbert Xu NLA_PUT_U8(skb, IFLA_GRE_PMTUDISC, !!(p->iph.frag_off & htons(IP_DF))); 1677c19e654dSHerbert Xu 1678c19e654dSHerbert Xu return 0; 1679c19e654dSHerbert Xu 1680c19e654dSHerbert Xu nla_put_failure: 1681c19e654dSHerbert Xu return -EMSGSIZE; 1682c19e654dSHerbert Xu } 1683c19e654dSHerbert Xu 1684c19e654dSHerbert Xu static const struct nla_policy ipgre_policy[IFLA_GRE_MAX + 1] = { 1685c19e654dSHerbert Xu [IFLA_GRE_LINK] = { .type = NLA_U32 }, 1686c19e654dSHerbert Xu [IFLA_GRE_IFLAGS] = { .type = NLA_U16 }, 1687c19e654dSHerbert Xu [IFLA_GRE_OFLAGS] = { .type = NLA_U16 }, 1688c19e654dSHerbert Xu [IFLA_GRE_IKEY] = { .type = NLA_U32 }, 1689c19e654dSHerbert Xu [IFLA_GRE_OKEY] = { .type = NLA_U32 }, 16904d74f8baSPatrick McHardy [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) }, 16914d74f8baSPatrick McHardy [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) }, 1692c19e654dSHerbert Xu [IFLA_GRE_TTL] = { .type = NLA_U8 }, 1693c19e654dSHerbert Xu [IFLA_GRE_TOS] = { .type = NLA_U8 }, 1694c19e654dSHerbert Xu [IFLA_GRE_PMTUDISC] = { .type = NLA_U8 }, 1695c19e654dSHerbert Xu }; 1696c19e654dSHerbert Xu 1697c19e654dSHerbert Xu static struct rtnl_link_ops ipgre_link_ops __read_mostly = { 1698c19e654dSHerbert Xu .kind = "gre", 1699c19e654dSHerbert Xu .maxtype = IFLA_GRE_MAX, 1700c19e654dSHerbert Xu .policy = ipgre_policy, 1701c19e654dSHerbert Xu .priv_size = sizeof(struct ip_tunnel), 1702c19e654dSHerbert Xu .setup = ipgre_tunnel_setup, 1703c19e654dSHerbert Xu .validate = ipgre_tunnel_validate, 1704c19e654dSHerbert Xu .newlink = ipgre_newlink, 1705c19e654dSHerbert Xu .changelink = ipgre_changelink, 1706c19e654dSHerbert Xu .get_size = ipgre_get_size, 1707c19e654dSHerbert Xu .fill_info = ipgre_fill_info, 1708c19e654dSHerbert Xu }; 1709c19e654dSHerbert Xu 1710e1a80002SHerbert Xu static struct rtnl_link_ops ipgre_tap_ops __read_mostly = { 1711e1a80002SHerbert Xu .kind = "gretap", 1712e1a80002SHerbert Xu .maxtype = IFLA_GRE_MAX, 1713e1a80002SHerbert Xu .policy = ipgre_policy, 1714e1a80002SHerbert Xu .priv_size = sizeof(struct ip_tunnel), 1715e1a80002SHerbert Xu .setup = ipgre_tap_setup, 1716e1a80002SHerbert Xu .validate = ipgre_tap_validate, 1717e1a80002SHerbert Xu .newlink = ipgre_newlink, 1718e1a80002SHerbert Xu .changelink = ipgre_changelink, 1719e1a80002SHerbert Xu .get_size = ipgre_get_size, 1720e1a80002SHerbert Xu .fill_info = ipgre_fill_info, 1721e1a80002SHerbert Xu }; 1722e1a80002SHerbert Xu 17231da177e4SLinus Torvalds /* 17241da177e4SLinus Torvalds * And now the modules code and kernel interface. 17251da177e4SLinus Torvalds */ 17261da177e4SLinus Torvalds 17271da177e4SLinus Torvalds static int __init ipgre_init(void) 17281da177e4SLinus Torvalds { 17291da177e4SLinus Torvalds int err; 17301da177e4SLinus Torvalds 17311da177e4SLinus Torvalds printk(KERN_INFO "GRE over IPv4 tunneling driver\n"); 17321da177e4SLinus Torvalds 1733cfb8fbf2SEric W. Biederman err = register_pernet_device(&ipgre_net_ops); 173459a4c759SPavel Emelyanov if (err < 0) 1735c2892f02SAlexey Dobriyan return err; 1736c2892f02SAlexey Dobriyan 173700959adeSDmitry Kozlov err = gre_add_protocol(&ipgre_protocol, GREPROTO_CISCO); 1738c2892f02SAlexey Dobriyan if (err < 0) { 1739c2892f02SAlexey Dobriyan printk(KERN_INFO "ipgre init: can't add protocol\n"); 1740c2892f02SAlexey Dobriyan goto add_proto_failed; 1741c2892f02SAlexey Dobriyan } 17427daa0004SPavel Emelyanov 1743c19e654dSHerbert Xu err = rtnl_link_register(&ipgre_link_ops); 1744c19e654dSHerbert Xu if (err < 0) 1745c19e654dSHerbert Xu goto rtnl_link_failed; 1746c19e654dSHerbert Xu 1747e1a80002SHerbert Xu err = rtnl_link_register(&ipgre_tap_ops); 1748e1a80002SHerbert Xu if (err < 0) 1749e1a80002SHerbert Xu goto tap_ops_failed; 1750e1a80002SHerbert Xu 1751c19e654dSHerbert Xu out: 17527daa0004SPavel Emelyanov return err; 1753c19e654dSHerbert Xu 1754e1a80002SHerbert Xu tap_ops_failed: 1755e1a80002SHerbert Xu rtnl_link_unregister(&ipgre_link_ops); 1756c19e654dSHerbert Xu rtnl_link_failed: 175700959adeSDmitry Kozlov gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO); 1758c2892f02SAlexey Dobriyan add_proto_failed: 1759c2892f02SAlexey Dobriyan unregister_pernet_device(&ipgre_net_ops); 1760c19e654dSHerbert Xu goto out; 17611da177e4SLinus Torvalds } 17621da177e4SLinus Torvalds 1763db44575fSAlexey Kuznetsov static void __exit ipgre_fini(void) 17641da177e4SLinus Torvalds { 1765e1a80002SHerbert Xu rtnl_link_unregister(&ipgre_tap_ops); 1766c19e654dSHerbert Xu rtnl_link_unregister(&ipgre_link_ops); 176700959adeSDmitry Kozlov if (gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO) < 0) 17681da177e4SLinus Torvalds printk(KERN_INFO "ipgre close: can't remove protocol\n"); 1769c2892f02SAlexey Dobriyan unregister_pernet_device(&ipgre_net_ops); 17701da177e4SLinus Torvalds } 17711da177e4SLinus Torvalds 17721da177e4SLinus Torvalds module_init(ipgre_init); 17731da177e4SLinus Torvalds module_exit(ipgre_fini); 17741da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 17754d74f8baSPatrick McHardy MODULE_ALIAS_RTNL_LINK("gre"); 17764d74f8baSPatrick McHardy MODULE_ALIAS_RTNL_LINK("gretap"); 1777