xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * GRE over IPv6 protocol decoder. |
| 3 | * |
| 4 | * Authors: Dmitry Kozlov (xeb@mail.ru) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 14 | |
| 15 | #include <linux/capability.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/types.h> |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/uaccess.h> |
| 21 | #include <linux/skbuff.h> |
| 22 | #include <linux/netdevice.h> |
| 23 | #include <linux/in.h> |
| 24 | #include <linux/tcp.h> |
| 25 | #include <linux/udp.h> |
| 26 | #include <linux/if_arp.h> |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 27 | #include <linux/init.h> |
| 28 | #include <linux/in6.h> |
| 29 | #include <linux/inetdevice.h> |
| 30 | #include <linux/igmp.h> |
| 31 | #include <linux/netfilter_ipv4.h> |
| 32 | #include <linux/etherdevice.h> |
| 33 | #include <linux/if_ether.h> |
| 34 | #include <linux/hash.h> |
| 35 | #include <linux/if_tunnel.h> |
| 36 | #include <linux/ip6_tunnel.h> |
| 37 | |
| 38 | #include <net/sock.h> |
| 39 | #include <net/ip.h> |
Pravin B Shelar | c544193 | 2013-03-25 14:49:35 +0000 | [diff] [blame] | 40 | #include <net/ip_tunnels.h> |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 41 | #include <net/icmp.h> |
| 42 | #include <net/protocol.h> |
| 43 | #include <net/addrconf.h> |
| 44 | #include <net/arp.h> |
| 45 | #include <net/checksum.h> |
| 46 | #include <net/dsfield.h> |
| 47 | #include <net/inet_ecn.h> |
| 48 | #include <net/xfrm.h> |
| 49 | #include <net/net_namespace.h> |
| 50 | #include <net/netns/generic.h> |
| 51 | #include <net/rtnetlink.h> |
| 52 | |
| 53 | #include <net/ipv6.h> |
| 54 | #include <net/ip6_fib.h> |
| 55 | #include <net/ip6_route.h> |
| 56 | #include <net/ip6_tunnel.h> |
Tom Herbert | 308edfdf | 2016-04-29 17:12:17 -0700 | [diff] [blame] | 57 | #include <net/gre.h> |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 58 | #include <net/erspan.h> |
William Tu | 6712abc | 2017-12-01 15:26:08 -0800 | [diff] [blame] | 59 | #include <net/dst_metadata.h> |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 60 | |
| 61 | |
stephen hemminger | eccc1bb | 2012-09-25 11:02:48 +0000 | [diff] [blame] | 62 | static bool log_ecn_error = true; |
| 63 | module_param(log_ecn_error, bool, 0644); |
| 64 | MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN"); |
| 65 | |
Jiri Kosina | e87a8f2 | 2016-08-10 11:03:35 +0200 | [diff] [blame] | 66 | #define IP6_GRE_HASH_SIZE_SHIFT 5 |
| 67 | #define IP6_GRE_HASH_SIZE (1 << IP6_GRE_HASH_SIZE_SHIFT) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 68 | |
Alexey Dobriyan | c7d03a0 | 2016-11-17 04:58:21 +0300 | [diff] [blame] | 69 | static unsigned int ip6gre_net_id __read_mostly; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 70 | struct ip6gre_net { |
Jiri Kosina | e87a8f2 | 2016-08-10 11:03:35 +0200 | [diff] [blame] | 71 | struct ip6_tnl __rcu *tunnels[4][IP6_GRE_HASH_SIZE]; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 72 | |
William Tu | 6712abc | 2017-12-01 15:26:08 -0800 | [diff] [blame] | 73 | struct ip6_tnl __rcu *collect_md_tun; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 74 | struct net_device *fb_tunnel_dev; |
| 75 | }; |
| 76 | |
| 77 | static struct rtnl_link_ops ip6gre_link_ops __read_mostly; |
Nicolas Dichtel | 22f0806 | 2014-04-22 10:15:24 +0200 | [diff] [blame] | 78 | static struct rtnl_link_ops ip6gre_tap_ops __read_mostly; |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 79 | static struct rtnl_link_ops ip6erspan_tap_ops __read_mostly; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 80 | static int ip6gre_tunnel_init(struct net_device *dev); |
| 81 | static void ip6gre_tunnel_setup(struct net_device *dev); |
| 82 | static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t); |
| 83 | static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu); |
| 84 | |
| 85 | /* Tunnel hash table */ |
| 86 | |
| 87 | /* |
| 88 | 4 hash tables: |
| 89 | |
| 90 | 3: (remote,local) |
| 91 | 2: (remote,*) |
| 92 | 1: (*,local) |
| 93 | 0: (*,*) |
| 94 | |
| 95 | We require exact key match i.e. if a key is present in packet |
| 96 | it will match only tunnel with the same key; if it is not present, |
| 97 | it will match only keyless tunnel. |
| 98 | |
| 99 | All keysless packets, if not matched configured keyless tunnels |
| 100 | will match fallback tunnel. |
| 101 | */ |
| 102 | |
Jiri Kosina | e87a8f2 | 2016-08-10 11:03:35 +0200 | [diff] [blame] | 103 | #define HASH_KEY(key) (((__force u32)key^((__force u32)key>>4))&(IP6_GRE_HASH_SIZE - 1)) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 104 | static u32 HASH_ADDR(const struct in6_addr *addr) |
| 105 | { |
| 106 | u32 hash = ipv6_addr_hash(addr); |
| 107 | |
Jiri Kosina | e87a8f2 | 2016-08-10 11:03:35 +0200 | [diff] [blame] | 108 | return hash_32(hash, IP6_GRE_HASH_SIZE_SHIFT); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | #define tunnels_r_l tunnels[3] |
| 112 | #define tunnels_r tunnels[2] |
| 113 | #define tunnels_l tunnels[1] |
| 114 | #define tunnels_wc tunnels[0] |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 115 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 116 | /* Given src, dst and key, find appropriate for input tunnel. */ |
| 117 | |
| 118 | static struct ip6_tnl *ip6gre_tunnel_lookup(struct net_device *dev, |
| 119 | const struct in6_addr *remote, const struct in6_addr *local, |
| 120 | __be32 key, __be16 gre_proto) |
| 121 | { |
| 122 | struct net *net = dev_net(dev); |
| 123 | int link = dev->ifindex; |
| 124 | unsigned int h0 = HASH_ADDR(remote); |
| 125 | unsigned int h1 = HASH_KEY(key); |
| 126 | struct ip6_tnl *t, *cand = NULL; |
| 127 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 128 | int dev_type = (gre_proto == htons(ETH_P_TEB) || |
| 129 | gre_proto == htons(ETH_P_ERSPAN)) ? |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 130 | ARPHRD_ETHER : ARPHRD_IP6GRE; |
| 131 | int score, cand_score = 4; |
| 132 | |
Amerigo Wang | e086cad | 2012-11-11 21:52:34 +0000 | [diff] [blame] | 133 | for_each_ip_tunnel_rcu(t, ign->tunnels_r_l[h0 ^ h1]) { |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 134 | if (!ipv6_addr_equal(local, &t->parms.laddr) || |
| 135 | !ipv6_addr_equal(remote, &t->parms.raddr) || |
| 136 | key != t->parms.i_key || |
| 137 | !(t->dev->flags & IFF_UP)) |
| 138 | continue; |
| 139 | |
| 140 | if (t->dev->type != ARPHRD_IP6GRE && |
| 141 | t->dev->type != dev_type) |
| 142 | continue; |
| 143 | |
| 144 | score = 0; |
| 145 | if (t->parms.link != link) |
| 146 | score |= 1; |
| 147 | if (t->dev->type != dev_type) |
| 148 | score |= 2; |
| 149 | if (score == 0) |
| 150 | return t; |
| 151 | |
| 152 | if (score < cand_score) { |
| 153 | cand = t; |
| 154 | cand_score = score; |
| 155 | } |
| 156 | } |
| 157 | |
Amerigo Wang | e086cad | 2012-11-11 21:52:34 +0000 | [diff] [blame] | 158 | for_each_ip_tunnel_rcu(t, ign->tunnels_r[h0 ^ h1]) { |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 159 | if (!ipv6_addr_equal(remote, &t->parms.raddr) || |
| 160 | key != t->parms.i_key || |
| 161 | !(t->dev->flags & IFF_UP)) |
| 162 | continue; |
| 163 | |
| 164 | if (t->dev->type != ARPHRD_IP6GRE && |
| 165 | t->dev->type != dev_type) |
| 166 | continue; |
| 167 | |
| 168 | score = 0; |
| 169 | if (t->parms.link != link) |
| 170 | score |= 1; |
| 171 | if (t->dev->type != dev_type) |
| 172 | score |= 2; |
| 173 | if (score == 0) |
| 174 | return t; |
| 175 | |
| 176 | if (score < cand_score) { |
| 177 | cand = t; |
| 178 | cand_score = score; |
| 179 | } |
| 180 | } |
| 181 | |
Amerigo Wang | e086cad | 2012-11-11 21:52:34 +0000 | [diff] [blame] | 182 | for_each_ip_tunnel_rcu(t, ign->tunnels_l[h1]) { |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 183 | if ((!ipv6_addr_equal(local, &t->parms.laddr) && |
| 184 | (!ipv6_addr_equal(local, &t->parms.raddr) || |
| 185 | !ipv6_addr_is_multicast(local))) || |
| 186 | key != t->parms.i_key || |
| 187 | !(t->dev->flags & IFF_UP)) |
| 188 | continue; |
| 189 | |
| 190 | if (t->dev->type != ARPHRD_IP6GRE && |
| 191 | t->dev->type != dev_type) |
| 192 | continue; |
| 193 | |
| 194 | score = 0; |
| 195 | if (t->parms.link != link) |
| 196 | score |= 1; |
| 197 | if (t->dev->type != dev_type) |
| 198 | score |= 2; |
| 199 | if (score == 0) |
| 200 | return t; |
| 201 | |
| 202 | if (score < cand_score) { |
| 203 | cand = t; |
| 204 | cand_score = score; |
| 205 | } |
| 206 | } |
| 207 | |
Amerigo Wang | e086cad | 2012-11-11 21:52:34 +0000 | [diff] [blame] | 208 | for_each_ip_tunnel_rcu(t, ign->tunnels_wc[h1]) { |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 209 | if (t->parms.i_key != key || |
| 210 | !(t->dev->flags & IFF_UP)) |
| 211 | continue; |
| 212 | |
| 213 | if (t->dev->type != ARPHRD_IP6GRE && |
| 214 | t->dev->type != dev_type) |
| 215 | continue; |
| 216 | |
| 217 | score = 0; |
| 218 | if (t->parms.link != link) |
| 219 | score |= 1; |
| 220 | if (t->dev->type != dev_type) |
| 221 | score |= 2; |
| 222 | if (score == 0) |
| 223 | return t; |
| 224 | |
| 225 | if (score < cand_score) { |
| 226 | cand = t; |
| 227 | cand_score = score; |
| 228 | } |
| 229 | } |
| 230 | |
Ian Morris | 53b24b8 | 2015-03-29 14:00:05 +0100 | [diff] [blame] | 231 | if (cand) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 232 | return cand; |
| 233 | |
William Tu | 6712abc | 2017-12-01 15:26:08 -0800 | [diff] [blame] | 234 | t = rcu_dereference(ign->collect_md_tun); |
| 235 | if (t && t->dev->flags & IFF_UP) |
| 236 | return t; |
| 237 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 238 | dev = ign->fb_tunnel_dev; |
Eric Dumazet | 79134e6 | 2018-03-08 12:51:41 -0800 | [diff] [blame] | 239 | if (dev && dev->flags & IFF_UP) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 240 | return netdev_priv(dev); |
| 241 | |
| 242 | return NULL; |
| 243 | } |
| 244 | |
| 245 | static struct ip6_tnl __rcu **__ip6gre_bucket(struct ip6gre_net *ign, |
| 246 | const struct __ip6_tnl_parm *p) |
| 247 | { |
| 248 | const struct in6_addr *remote = &p->raddr; |
| 249 | const struct in6_addr *local = &p->laddr; |
| 250 | unsigned int h = HASH_KEY(p->i_key); |
| 251 | int prio = 0; |
| 252 | |
| 253 | if (!ipv6_addr_any(local)) |
| 254 | prio |= 1; |
| 255 | if (!ipv6_addr_any(remote) && !ipv6_addr_is_multicast(remote)) { |
| 256 | prio |= 2; |
| 257 | h ^= HASH_ADDR(remote); |
| 258 | } |
| 259 | |
| 260 | return &ign->tunnels[prio][h]; |
| 261 | } |
| 262 | |
| 263 | static inline struct ip6_tnl __rcu **ip6gre_bucket(struct ip6gre_net *ign, |
| 264 | const struct ip6_tnl *t) |
| 265 | { |
| 266 | return __ip6gre_bucket(ign, &t->parms); |
| 267 | } |
| 268 | |
| 269 | static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t) |
| 270 | { |
| 271 | struct ip6_tnl __rcu **tp = ip6gre_bucket(ign, t); |
| 272 | |
William Tu | 6712abc | 2017-12-01 15:26:08 -0800 | [diff] [blame] | 273 | if (t->parms.collect_md) |
| 274 | rcu_assign_pointer(ign->collect_md_tun, t); |
| 275 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 276 | rcu_assign_pointer(t->next, rtnl_dereference(*tp)); |
| 277 | rcu_assign_pointer(*tp, t); |
| 278 | } |
| 279 | |
| 280 | static void ip6gre_tunnel_unlink(struct ip6gre_net *ign, struct ip6_tnl *t) |
| 281 | { |
| 282 | struct ip6_tnl __rcu **tp; |
| 283 | struct ip6_tnl *iter; |
| 284 | |
William Tu | 6712abc | 2017-12-01 15:26:08 -0800 | [diff] [blame] | 285 | if (t->parms.collect_md) |
| 286 | rcu_assign_pointer(ign->collect_md_tun, NULL); |
| 287 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 288 | for (tp = ip6gre_bucket(ign, t); |
| 289 | (iter = rtnl_dereference(*tp)) != NULL; |
| 290 | tp = &iter->next) { |
| 291 | if (t == iter) { |
| 292 | rcu_assign_pointer(*tp, t->next); |
| 293 | break; |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | static struct ip6_tnl *ip6gre_tunnel_find(struct net *net, |
| 299 | const struct __ip6_tnl_parm *parms, |
| 300 | int type) |
| 301 | { |
| 302 | const struct in6_addr *remote = &parms->raddr; |
| 303 | const struct in6_addr *local = &parms->laddr; |
| 304 | __be32 key = parms->i_key; |
| 305 | int link = parms->link; |
| 306 | struct ip6_tnl *t; |
| 307 | struct ip6_tnl __rcu **tp; |
| 308 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); |
| 309 | |
| 310 | for (tp = __ip6gre_bucket(ign, parms); |
| 311 | (t = rtnl_dereference(*tp)) != NULL; |
| 312 | tp = &t->next) |
| 313 | if (ipv6_addr_equal(local, &t->parms.laddr) && |
| 314 | ipv6_addr_equal(remote, &t->parms.raddr) && |
| 315 | key == t->parms.i_key && |
| 316 | link == t->parms.link && |
| 317 | type == t->dev->type) |
| 318 | break; |
| 319 | |
| 320 | return t; |
| 321 | } |
| 322 | |
| 323 | static struct ip6_tnl *ip6gre_tunnel_locate(struct net *net, |
| 324 | const struct __ip6_tnl_parm *parms, int create) |
| 325 | { |
| 326 | struct ip6_tnl *t, *nt; |
| 327 | struct net_device *dev; |
| 328 | char name[IFNAMSIZ]; |
| 329 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); |
| 330 | |
| 331 | t = ip6gre_tunnel_find(net, parms, ARPHRD_IP6GRE); |
Steffen Klassert | cd0a0bd | 2014-09-22 10:07:26 +0200 | [diff] [blame] | 332 | if (t && create) |
| 333 | return NULL; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 334 | if (t || !create) |
| 335 | return t; |
| 336 | |
| 337 | if (parms->name[0]) |
| 338 | strlcpy(name, parms->name, IFNAMSIZ); |
| 339 | else |
| 340 | strcpy(name, "ip6gre%d"); |
| 341 | |
Tom Gundersen | c835a67 | 2014-07-14 16:37:24 +0200 | [diff] [blame] | 342 | dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN, |
| 343 | ip6gre_tunnel_setup); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 344 | if (!dev) |
| 345 | return NULL; |
| 346 | |
| 347 | dev_net_set(dev, net); |
| 348 | |
| 349 | nt = netdev_priv(dev); |
| 350 | nt->parms = *parms; |
| 351 | dev->rtnl_link_ops = &ip6gre_link_ops; |
| 352 | |
| 353 | nt->dev = dev; |
Nicolas Dichtel | 0bd87628 | 2013-08-13 17:51:12 +0200 | [diff] [blame] | 354 | nt->net = dev_net(dev); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 355 | |
| 356 | if (register_netdevice(dev) < 0) |
| 357 | goto failed_free; |
| 358 | |
Alexey Kodanev | 128bb97 | 2018-01-18 20:51:12 +0300 | [diff] [blame] | 359 | ip6gre_tnl_link_config(nt, 1); |
| 360 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 361 | /* Can use a lockless transmit, unless we generate output sequences */ |
Tom Herbert | b45bd1d | 2016-05-09 17:12:12 -0700 | [diff] [blame] | 362 | if (!(nt->parms.o_flags & TUNNEL_SEQ)) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 363 | dev->features |= NETIF_F_LLTX; |
| 364 | |
| 365 | dev_hold(dev); |
| 366 | ip6gre_tunnel_link(ign, nt); |
| 367 | return nt; |
| 368 | |
| 369 | failed_free: |
| 370 | free_netdev(dev); |
| 371 | return NULL; |
| 372 | } |
| 373 | |
| 374 | static void ip6gre_tunnel_uninit(struct net_device *dev) |
| 375 | { |
Nicolas Dichtel | 22f0806 | 2014-04-22 10:15:24 +0200 | [diff] [blame] | 376 | struct ip6_tnl *t = netdev_priv(dev); |
| 377 | struct ip6gre_net *ign = net_generic(t->net, ip6gre_net_id); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 378 | |
Nicolas Dichtel | 22f0806 | 2014-04-22 10:15:24 +0200 | [diff] [blame] | 379 | ip6gre_tunnel_unlink(ign, t); |
Paolo Abeni | 607f725 | 2016-02-12 15:43:54 +0100 | [diff] [blame] | 380 | dst_cache_reset(&t->dst_cache); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 381 | dev_put(dev); |
| 382 | } |
| 383 | |
| 384 | |
| 385 | static void ip6gre_err(struct sk_buff *skb, struct inet6_skb_parm *opt, |
Eric Dumazet | 7892032 | 2017-02-04 23:18:55 -0800 | [diff] [blame] | 386 | u8 type, u8 code, int offset, __be32 info) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 387 | { |
Xin Long | 929fc03 | 2017-11-11 19:06:49 +0800 | [diff] [blame] | 388 | struct net *net = dev_net(skb->dev); |
Eric Dumazet | 7892032 | 2017-02-04 23:18:55 -0800 | [diff] [blame] | 389 | const struct gre_base_hdr *greh; |
| 390 | const struct ipv6hdr *ipv6h; |
| 391 | int grehlen = sizeof(*greh); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 392 | struct ip6_tnl *t; |
Eric Dumazet | 7892032 | 2017-02-04 23:18:55 -0800 | [diff] [blame] | 393 | int key_off = 0; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 394 | __be16 flags; |
Eric Dumazet | 7892032 | 2017-02-04 23:18:55 -0800 | [diff] [blame] | 395 | __be32 key; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 396 | |
Eric Dumazet | 7892032 | 2017-02-04 23:18:55 -0800 | [diff] [blame] | 397 | if (!pskb_may_pull(skb, offset + grehlen)) |
| 398 | return; |
| 399 | greh = (const struct gre_base_hdr *)(skb->data + offset); |
| 400 | flags = greh->flags; |
| 401 | if (flags & (GRE_VERSION | GRE_ROUTING)) |
| 402 | return; |
| 403 | if (flags & GRE_CSUM) |
| 404 | grehlen += 4; |
| 405 | if (flags & GRE_KEY) { |
| 406 | key_off = grehlen + offset; |
| 407 | grehlen += 4; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Eric Dumazet | 7892032 | 2017-02-04 23:18:55 -0800 | [diff] [blame] | 410 | if (!pskb_may_pull(skb, offset + grehlen)) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 411 | return; |
Eric Dumazet | b87fb39 | 2012-08-19 03:47:30 +0000 | [diff] [blame] | 412 | ipv6h = (const struct ipv6hdr *)skb->data; |
Eric Dumazet | 7892032 | 2017-02-04 23:18:55 -0800 | [diff] [blame] | 413 | greh = (const struct gre_base_hdr *)(skb->data + offset); |
| 414 | key = key_off ? *(__be32 *)(skb->data + key_off) : 0; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 415 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 416 | t = ip6gre_tunnel_lookup(skb->dev, &ipv6h->daddr, &ipv6h->saddr, |
Eric Dumazet | 7892032 | 2017-02-04 23:18:55 -0800 | [diff] [blame] | 417 | key, greh->protocol); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 418 | if (!t) |
stephen hemminger | 0c5794a | 2012-09-24 18:12:24 +0000 | [diff] [blame] | 419 | return; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 420 | |
| 421 | switch (type) { |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 422 | struct ipv6_tlv_tnl_enc_lim *tel; |
Xin Long | fe1a4ca | 2017-11-11 19:06:50 +0800 | [diff] [blame] | 423 | __u32 teli; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 424 | case ICMPV6_DEST_UNREACH: |
Matt Bennett | a46496c | 2015-09-23 16:58:31 +1200 | [diff] [blame] | 425 | net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n", |
| 426 | t->parms.name); |
Xin Long | f8d20b4 | 2017-10-26 19:23:27 +0800 | [diff] [blame] | 427 | if (code != ICMPV6_PORT_UNREACH) |
| 428 | break; |
| 429 | return; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 430 | case ICMPV6_TIME_EXCEED: |
| 431 | if (code == ICMPV6_EXC_HOPLIMIT) { |
Matt Bennett | a46496c | 2015-09-23 16:58:31 +1200 | [diff] [blame] | 432 | net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n", |
| 433 | t->parms.name); |
Xin Long | f8d20b4 | 2017-10-26 19:23:27 +0800 | [diff] [blame] | 434 | break; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 435 | } |
Xin Long | f8d20b4 | 2017-10-26 19:23:27 +0800 | [diff] [blame] | 436 | return; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 437 | case ICMPV6_PARAMPROB: |
| 438 | teli = 0; |
| 439 | if (code == ICMPV6_HDR_FIELD) |
| 440 | teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data); |
| 441 | |
Sabrina Dubroca | d1e158e | 2015-02-04 15:25:09 +0100 | [diff] [blame] | 442 | if (teli && teli == be32_to_cpu(info) - 2) { |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 443 | tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli]; |
| 444 | if (tel->encap_limit == 0) { |
Matt Bennett | a46496c | 2015-09-23 16:58:31 +1200 | [diff] [blame] | 445 | net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n", |
| 446 | t->parms.name); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 447 | } |
| 448 | } else { |
Matt Bennett | a46496c | 2015-09-23 16:58:31 +1200 | [diff] [blame] | 449 | net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n", |
| 450 | t->parms.name); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 451 | } |
Xin Long | f8d20b4 | 2017-10-26 19:23:27 +0800 | [diff] [blame] | 452 | return; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 453 | case ICMPV6_PKT_TOOBIG: |
Xin Long | fe1a4ca | 2017-11-11 19:06:50 +0800 | [diff] [blame] | 454 | ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL)); |
Xin Long | f8d20b4 | 2017-10-26 19:23:27 +0800 | [diff] [blame] | 455 | return; |
Xin Long | 929fc03 | 2017-11-11 19:06:49 +0800 | [diff] [blame] | 456 | case NDISC_REDIRECT: |
| 457 | ip6_redirect(skb, net, skb->dev->ifindex, 0, |
| 458 | sock_net_uid(net, NULL)); |
| 459 | return; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | if (time_before(jiffies, t->err_time + IP6TUNNEL_ERR_TIMEO)) |
| 463 | t->err_count++; |
| 464 | else |
| 465 | t->err_count = 1; |
| 466 | t->err_time = jiffies; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Tom Herbert | 308edfdf | 2016-04-29 17:12:17 -0700 | [diff] [blame] | 469 | static int ip6gre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 470 | { |
| 471 | const struct ipv6hdr *ipv6h; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 472 | struct ip6_tnl *tunnel; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 473 | |
| 474 | ipv6h = ipv6_hdr(skb); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 475 | tunnel = ip6gre_tunnel_lookup(skb->dev, |
Tom Herbert | 308edfdf | 2016-04-29 17:12:17 -0700 | [diff] [blame] | 476 | &ipv6h->saddr, &ipv6h->daddr, tpi->key, |
| 477 | tpi->proto); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 478 | if (tunnel) { |
William Tu | 6712abc | 2017-12-01 15:26:08 -0800 | [diff] [blame] | 479 | if (tunnel->parms.collect_md) { |
| 480 | struct metadata_dst *tun_dst; |
| 481 | __be64 tun_id; |
| 482 | __be16 flags; |
| 483 | |
| 484 | flags = tpi->flags; |
| 485 | tun_id = key32_to_tunnel_id(tpi->key); |
| 486 | |
| 487 | tun_dst = ipv6_tun_rx_dst(skb, flags, tun_id, 0); |
| 488 | if (!tun_dst) |
| 489 | return PACKET_REJECT; |
| 490 | |
| 491 | ip6_tnl_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error); |
| 492 | } else { |
| 493 | ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error); |
| 494 | } |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 495 | |
Tom Herbert | 308edfdf | 2016-04-29 17:12:17 -0700 | [diff] [blame] | 496 | return PACKET_RCVD; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 497 | } |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 498 | |
Tom Herbert | 308edfdf | 2016-04-29 17:12:17 -0700 | [diff] [blame] | 499 | return PACKET_REJECT; |
| 500 | } |
| 501 | |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 502 | static int ip6erspan_rcv(struct sk_buff *skb, int gre_hdr_len, |
| 503 | struct tnl_ptk_info *tpi) |
| 504 | { |
William Tu | 1d7e2ed | 2017-12-13 16:38:55 -0800 | [diff] [blame] | 505 | struct erspan_base_hdr *ershdr; |
| 506 | struct erspan_metadata *pkt_md; |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 507 | const struct ipv6hdr *ipv6h; |
William Tu | 3df19283 | 2018-02-05 13:35:34 -0800 | [diff] [blame] | 508 | struct erspan_md2 *md2; |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 509 | struct ip6_tnl *tunnel; |
William Tu | 1d7e2ed | 2017-12-13 16:38:55 -0800 | [diff] [blame] | 510 | u8 ver; |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 511 | |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 512 | if (unlikely(!pskb_may_pull(skb, sizeof(*ershdr)))) |
| 513 | return PACKET_REJECT; |
| 514 | |
Haishuang Yan | 293a199 | 2017-12-20 09:53:19 +0800 | [diff] [blame] | 515 | ipv6h = ipv6_hdr(skb); |
| 516 | ershdr = (struct erspan_base_hdr *)skb->data; |
William Tu | c69de58 | 2018-01-25 13:20:09 -0800 | [diff] [blame] | 517 | ver = ershdr->ver; |
| 518 | tpi->key = cpu_to_be32(get_session_id(ershdr)); |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 519 | |
| 520 | tunnel = ip6gre_tunnel_lookup(skb->dev, |
| 521 | &ipv6h->saddr, &ipv6h->daddr, tpi->key, |
| 522 | tpi->proto); |
| 523 | if (tunnel) { |
William Tu | 1d7e2ed | 2017-12-13 16:38:55 -0800 | [diff] [blame] | 524 | int len = erspan_hdr_len(ver); |
| 525 | |
| 526 | if (unlikely(!pskb_may_pull(skb, len))) |
William Tu | ae3e133 | 2017-12-15 14:27:43 -0800 | [diff] [blame] | 527 | return PACKET_REJECT; |
William Tu | 1d7e2ed | 2017-12-13 16:38:55 -0800 | [diff] [blame] | 528 | |
William Tu | d91e8db5 | 2017-12-15 14:27:44 -0800 | [diff] [blame] | 529 | ershdr = (struct erspan_base_hdr *)skb->data; |
| 530 | pkt_md = (struct erspan_metadata *)(ershdr + 1); |
| 531 | |
William Tu | 1d7e2ed | 2017-12-13 16:38:55 -0800 | [diff] [blame] | 532 | if (__iptunnel_pull_header(skb, len, |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 533 | htons(ETH_P_TEB), |
| 534 | false, false) < 0) |
| 535 | return PACKET_REJECT; |
| 536 | |
William Tu | ef7baf5 | 2017-12-05 15:15:44 -0800 | [diff] [blame] | 537 | if (tunnel->parms.collect_md) { |
| 538 | struct metadata_dst *tun_dst; |
| 539 | struct ip_tunnel_info *info; |
| 540 | struct erspan_metadata *md; |
| 541 | __be64 tun_id; |
| 542 | __be16 flags; |
| 543 | |
| 544 | tpi->flags |= TUNNEL_KEY; |
| 545 | flags = tpi->flags; |
| 546 | tun_id = key32_to_tunnel_id(tpi->key); |
| 547 | |
| 548 | tun_dst = ipv6_tun_rx_dst(skb, flags, tun_id, |
| 549 | sizeof(*md)); |
| 550 | if (!tun_dst) |
| 551 | return PACKET_REJECT; |
| 552 | |
| 553 | info = &tun_dst->u.tun_info; |
| 554 | md = ip_tunnel_info_opts(info); |
William Tu | 94d7d8f | 2017-12-13 16:38:57 -0800 | [diff] [blame] | 555 | md->version = ver; |
William Tu | 3df19283 | 2018-02-05 13:35:34 -0800 | [diff] [blame] | 556 | md2 = &md->u.md2; |
| 557 | memcpy(md2, pkt_md, ver == 1 ? ERSPAN_V1_MDSIZE : |
| 558 | ERSPAN_V2_MDSIZE); |
William Tu | ef7baf5 | 2017-12-05 15:15:44 -0800 | [diff] [blame] | 559 | info->key.tun_flags |= TUNNEL_ERSPAN_OPT; |
| 560 | info->options_len = sizeof(*md); |
| 561 | |
| 562 | ip6_tnl_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error); |
| 563 | |
| 564 | } else { |
William Tu | ef7baf5 | 2017-12-05 15:15:44 -0800 | [diff] [blame] | 565 | ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error); |
| 566 | } |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 567 | |
| 568 | return PACKET_RCVD; |
| 569 | } |
| 570 | |
| 571 | return PACKET_REJECT; |
| 572 | } |
| 573 | |
Tom Herbert | 308edfdf | 2016-04-29 17:12:17 -0700 | [diff] [blame] | 574 | static int gre_rcv(struct sk_buff *skb) |
| 575 | { |
| 576 | struct tnl_ptk_info tpi; |
| 577 | bool csum_err = false; |
| 578 | int hdr_len; |
| 579 | |
Eric Dumazet | e582615ad | 2016-06-15 06:24:00 -0700 | [diff] [blame] | 580 | hdr_len = gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IPV6), 0); |
Jiri Benc | f132ae7 | 2016-05-03 15:00:21 +0200 | [diff] [blame] | 581 | if (hdr_len < 0) |
Tom Herbert | 308edfdf | 2016-04-29 17:12:17 -0700 | [diff] [blame] | 582 | goto drop; |
| 583 | |
| 584 | if (iptunnel_pull_header(skb, hdr_len, tpi.proto, false)) |
| 585 | goto drop; |
| 586 | |
William Tu | 94d7d8f | 2017-12-13 16:38:57 -0800 | [diff] [blame] | 587 | if (unlikely(tpi.proto == htons(ETH_P_ERSPAN) || |
| 588 | tpi.proto == htons(ETH_P_ERSPAN2))) { |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 589 | if (ip6erspan_rcv(skb, hdr_len, &tpi) == PACKET_RCVD) |
| 590 | return 0; |
Haishuang Yan | a734321 | 2017-12-20 10:21:47 +0800 | [diff] [blame] | 591 | goto out; |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 592 | } |
| 593 | |
Tom Herbert | 308edfdf | 2016-04-29 17:12:17 -0700 | [diff] [blame] | 594 | if (ip6gre_rcv(skb, &tpi) == PACKET_RCVD) |
| 595 | return 0; |
| 596 | |
Haishuang Yan | a734321 | 2017-12-20 10:21:47 +0800 | [diff] [blame] | 597 | out: |
Tom Herbert | 308edfdf | 2016-04-29 17:12:17 -0700 | [diff] [blame] | 598 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 599 | drop: |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 600 | kfree_skb(skb); |
| 601 | return 0; |
| 602 | } |
| 603 | |
Tom Herbert | b05229f | 2016-04-29 17:12:21 -0700 | [diff] [blame] | 604 | static int gre_handle_offloads(struct sk_buff *skb, bool csum) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 605 | { |
Tom Herbert | b05229f | 2016-04-29 17:12:21 -0700 | [diff] [blame] | 606 | return iptunnel_handle_offloads(skb, |
| 607 | csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 608 | } |
| 609 | |
William Tu | 898b2979 | 2017-11-30 11:51:28 -0800 | [diff] [blame] | 610 | static void prepare_ip6gre_xmit_ipv4(struct sk_buff *skb, |
| 611 | struct net_device *dev, |
| 612 | struct flowi6 *fl6, __u8 *dsfield, |
| 613 | int *encap_limit) |
| 614 | { |
| 615 | const struct iphdr *iph = ip_hdr(skb); |
| 616 | struct ip6_tnl *t = netdev_priv(dev); |
| 617 | |
| 618 | if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) |
| 619 | *encap_limit = t->parms.encap_limit; |
| 620 | |
| 621 | memcpy(fl6, &t->fl.u.ip6, sizeof(*fl6)); |
| 622 | |
| 623 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS) |
| 624 | *dsfield = ipv4_get_dsfield(iph); |
| 625 | else |
| 626 | *dsfield = ip6_tclass(t->parms.flowinfo); |
| 627 | |
| 628 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK) |
| 629 | fl6->flowi6_mark = skb->mark; |
| 630 | else |
| 631 | fl6->flowi6_mark = t->parms.fwmark; |
| 632 | |
| 633 | fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL); |
| 634 | } |
| 635 | |
| 636 | static int prepare_ip6gre_xmit_ipv6(struct sk_buff *skb, |
| 637 | struct net_device *dev, |
| 638 | struct flowi6 *fl6, __u8 *dsfield, |
| 639 | int *encap_limit) |
| 640 | { |
| 641 | struct ipv6hdr *ipv6h = ipv6_hdr(skb); |
| 642 | struct ip6_tnl *t = netdev_priv(dev); |
| 643 | __u16 offset; |
| 644 | |
| 645 | offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb)); |
| 646 | /* ip6_tnl_parse_tlv_enc_lim() might have reallocated skb->head */ |
| 647 | |
| 648 | if (offset > 0) { |
| 649 | struct ipv6_tlv_tnl_enc_lim *tel; |
| 650 | |
| 651 | tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset]; |
| 652 | if (tel->encap_limit == 0) { |
| 653 | icmpv6_send(skb, ICMPV6_PARAMPROB, |
| 654 | ICMPV6_HDR_FIELD, offset + 2); |
| 655 | return -1; |
| 656 | } |
| 657 | *encap_limit = tel->encap_limit - 1; |
| 658 | } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) { |
| 659 | *encap_limit = t->parms.encap_limit; |
| 660 | } |
| 661 | |
| 662 | memcpy(fl6, &t->fl.u.ip6, sizeof(*fl6)); |
| 663 | |
| 664 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS) |
| 665 | *dsfield = ipv6_get_dsfield(ipv6h); |
| 666 | else |
| 667 | *dsfield = ip6_tclass(t->parms.flowinfo); |
| 668 | |
| 669 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL) |
| 670 | fl6->flowlabel |= ip6_flowlabel(ipv6h); |
| 671 | |
| 672 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK) |
| 673 | fl6->flowi6_mark = skb->mark; |
| 674 | else |
| 675 | fl6->flowi6_mark = t->parms.fwmark; |
| 676 | |
| 677 | fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL); |
| 678 | |
| 679 | return 0; |
| 680 | } |
| 681 | |
Tom Herbert | b05229f | 2016-04-29 17:12:21 -0700 | [diff] [blame] | 682 | static netdev_tx_t __gre6_xmit(struct sk_buff *skb, |
| 683 | struct net_device *dev, __u8 dsfield, |
| 684 | struct flowi6 *fl6, int encap_limit, |
| 685 | __u32 *pmtu, __be16 proto) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 686 | { |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 687 | struct ip6_tnl *tunnel = netdev_priv(dev); |
Xin Long | 8aec495 | 2017-10-26 19:27:17 +0800 | [diff] [blame] | 688 | __be16 protocol; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 689 | |
| 690 | if (dev->type == ARPHRD_ETHER) |
| 691 | IPCB(skb)->flags = 0; |
| 692 | |
Tom Herbert | b05229f | 2016-04-29 17:12:21 -0700 | [diff] [blame] | 693 | if (dev->header_ops && dev->type == ARPHRD_IP6GRE) |
| 694 | fl6->daddr = ((struct ipv6hdr *)skb->data)->daddr; |
| 695 | else |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 696 | fl6->daddr = tunnel->parms.raddr; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 697 | |
Tom Herbert | b05229f | 2016-04-29 17:12:21 -0700 | [diff] [blame] | 698 | /* Push GRE header. */ |
Xin Long | 8aec495 | 2017-10-26 19:27:17 +0800 | [diff] [blame] | 699 | protocol = (dev->type == ARPHRD_ETHER) ? htons(ETH_P_TEB) : proto; |
William Tu | 6712abc | 2017-12-01 15:26:08 -0800 | [diff] [blame] | 700 | |
| 701 | if (tunnel->parms.collect_md) { |
| 702 | struct ip_tunnel_info *tun_info; |
| 703 | const struct ip_tunnel_key *key; |
| 704 | __be16 flags; |
| 705 | |
| 706 | tun_info = skb_tunnel_info(skb); |
| 707 | if (unlikely(!tun_info || |
| 708 | !(tun_info->mode & IP_TUNNEL_INFO_TX) || |
| 709 | ip_tunnel_info_af(tun_info) != AF_INET6)) |
| 710 | return -EINVAL; |
| 711 | |
| 712 | key = &tun_info->key; |
| 713 | memset(fl6, 0, sizeof(*fl6)); |
| 714 | fl6->flowi6_proto = IPPROTO_GRE; |
| 715 | fl6->daddr = key->u.ipv6.dst; |
| 716 | fl6->flowlabel = key->label; |
| 717 | fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL); |
| 718 | |
| 719 | dsfield = key->tos; |
William Tu | 77a5196 | 2018-03-01 13:49:57 -0800 | [diff] [blame] | 720 | flags = key->tun_flags & |
| 721 | (TUNNEL_CSUM | TUNNEL_KEY | TUNNEL_SEQ); |
William Tu | 6712abc | 2017-12-01 15:26:08 -0800 | [diff] [blame] | 722 | tunnel->tun_hlen = gre_calc_hlen(flags); |
| 723 | |
| 724 | gre_build_header(skb, tunnel->tun_hlen, |
| 725 | flags, protocol, |
William Tu | 77a5196 | 2018-03-01 13:49:57 -0800 | [diff] [blame] | 726 | tunnel_id_to_key32(tun_info->key.tun_id), |
Colin Ian King | 1574639 | 2018-03-21 19:34:58 +0000 | [diff] [blame^] | 727 | (flags & TUNNEL_SEQ) ? htonl(tunnel->o_seqno++) |
William Tu | 77a5196 | 2018-03-01 13:49:57 -0800 | [diff] [blame] | 728 | : 0); |
William Tu | 6712abc | 2017-12-01 15:26:08 -0800 | [diff] [blame] | 729 | |
| 730 | } else { |
William Tu | 77a5196 | 2018-03-01 13:49:57 -0800 | [diff] [blame] | 731 | if (tunnel->parms.o_flags & TUNNEL_SEQ) |
| 732 | tunnel->o_seqno++; |
| 733 | |
William Tu | 6712abc | 2017-12-01 15:26:08 -0800 | [diff] [blame] | 734 | gre_build_header(skb, tunnel->tun_hlen, tunnel->parms.o_flags, |
| 735 | protocol, tunnel->parms.o_key, |
| 736 | htonl(tunnel->o_seqno)); |
| 737 | } |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 738 | |
Tom Herbert | b05229f | 2016-04-29 17:12:21 -0700 | [diff] [blame] | 739 | return ip6_tnl_xmit(skb, dev, dsfield, fl6, encap_limit, pmtu, |
| 740 | NEXTHDR_GRE); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev) |
| 744 | { |
| 745 | struct ip6_tnl *t = netdev_priv(dev); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 746 | int encap_limit = -1; |
| 747 | struct flowi6 fl6; |
William Tu | 6712abc | 2017-12-01 15:26:08 -0800 | [diff] [blame] | 748 | __u8 dsfield = 0; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 749 | __u32 mtu; |
| 750 | int err; |
| 751 | |
Bernie Harris | 5146d1f | 2016-02-22 12:58:05 +1300 | [diff] [blame] | 752 | memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); |
| 753 | |
William Tu | 6712abc | 2017-12-01 15:26:08 -0800 | [diff] [blame] | 754 | if (!t->parms.collect_md) |
| 755 | prepare_ip6gre_xmit_ipv4(skb, dev, &fl6, |
| 756 | &dsfield, &encap_limit); |
Lorenzo Colitti | e2d118a | 2016-11-04 02:23:43 +0900 | [diff] [blame] | 757 | |
Tom Herbert | b05229f | 2016-04-29 17:12:21 -0700 | [diff] [blame] | 758 | err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM)); |
| 759 | if (err) |
| 760 | return -1; |
| 761 | |
| 762 | err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu, |
| 763 | skb->protocol); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 764 | if (err != 0) { |
| 765 | /* XXX: send ICMP error even if DF is not set. */ |
| 766 | if (err == -EMSGSIZE) |
| 767 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, |
| 768 | htonl(mtu)); |
| 769 | return -1; |
| 770 | } |
| 771 | |
| 772 | return 0; |
| 773 | } |
| 774 | |
| 775 | static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev) |
| 776 | { |
| 777 | struct ip6_tnl *t = netdev_priv(dev); |
| 778 | struct ipv6hdr *ipv6h = ipv6_hdr(skb); |
| 779 | int encap_limit = -1; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 780 | struct flowi6 fl6; |
William Tu | 6712abc | 2017-12-01 15:26:08 -0800 | [diff] [blame] | 781 | __u8 dsfield = 0; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 782 | __u32 mtu; |
| 783 | int err; |
| 784 | |
| 785 | if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr)) |
| 786 | return -1; |
| 787 | |
William Tu | 6712abc | 2017-12-01 15:26:08 -0800 | [diff] [blame] | 788 | if (!t->parms.collect_md && |
| 789 | prepare_ip6gre_xmit_ipv6(skb, dev, &fl6, &dsfield, &encap_limit)) |
William Tu | 898b2979 | 2017-11-30 11:51:28 -0800 | [diff] [blame] | 790 | return -1; |
Lorenzo Colitti | e2d118a | 2016-11-04 02:23:43 +0900 | [diff] [blame] | 791 | |
Tom Herbert | b05229f | 2016-04-29 17:12:21 -0700 | [diff] [blame] | 792 | if (gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM))) |
| 793 | return -1; |
| 794 | |
| 795 | err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit, |
| 796 | &mtu, skb->protocol); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 797 | if (err != 0) { |
| 798 | if (err == -EMSGSIZE) |
| 799 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); |
| 800 | return -1; |
| 801 | } |
| 802 | |
| 803 | return 0; |
| 804 | } |
| 805 | |
| 806 | /** |
| 807 | * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own |
| 808 | * @t: the outgoing tunnel device |
| 809 | * @hdr: IPv6 header from the incoming packet |
| 810 | * |
| 811 | * Description: |
| 812 | * Avoid trivial tunneling loop by checking that tunnel exit-point |
| 813 | * doesn't match source of incoming packet. |
| 814 | * |
| 815 | * Return: |
| 816 | * 1 if conflict, |
| 817 | * 0 else |
| 818 | **/ |
| 819 | |
| 820 | static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl *t, |
| 821 | const struct ipv6hdr *hdr) |
| 822 | { |
| 823 | return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr); |
| 824 | } |
| 825 | |
| 826 | static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev) |
| 827 | { |
| 828 | struct ip6_tnl *t = netdev_priv(dev); |
| 829 | int encap_limit = -1; |
| 830 | struct flowi6 fl6; |
| 831 | __u32 mtu; |
| 832 | int err; |
| 833 | |
| 834 | if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) |
| 835 | encap_limit = t->parms.encap_limit; |
| 836 | |
William Tu | 6712abc | 2017-12-01 15:26:08 -0800 | [diff] [blame] | 837 | if (!t->parms.collect_md) |
| 838 | memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6)); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 839 | |
Tom Herbert | b05229f | 2016-04-29 17:12:21 -0700 | [diff] [blame] | 840 | err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM)); |
| 841 | if (err) |
| 842 | return err; |
| 843 | |
| 844 | err = __gre6_xmit(skb, dev, 0, &fl6, encap_limit, &mtu, skb->protocol); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 845 | |
| 846 | return err; |
| 847 | } |
| 848 | |
| 849 | static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb, |
| 850 | struct net_device *dev) |
| 851 | { |
| 852 | struct ip6_tnl *t = netdev_priv(dev); |
| 853 | struct net_device_stats *stats = &t->dev->stats; |
| 854 | int ret; |
| 855 | |
Steffen Klassert | d500514 | 2014-11-05 08:02:48 +0100 | [diff] [blame] | 856 | if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr)) |
Tommi Rantala | 41ab3e3 | 2013-02-06 03:24:02 +0000 | [diff] [blame] | 857 | goto tx_err; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 858 | |
| 859 | switch (skb->protocol) { |
| 860 | case htons(ETH_P_IP): |
| 861 | ret = ip6gre_xmit_ipv4(skb, dev); |
| 862 | break; |
| 863 | case htons(ETH_P_IPV6): |
| 864 | ret = ip6gre_xmit_ipv6(skb, dev); |
| 865 | break; |
| 866 | default: |
| 867 | ret = ip6gre_xmit_other(skb, dev); |
| 868 | break; |
| 869 | } |
| 870 | |
| 871 | if (ret < 0) |
| 872 | goto tx_err; |
| 873 | |
| 874 | return NETDEV_TX_OK; |
| 875 | |
| 876 | tx_err: |
| 877 | stats->tx_errors++; |
| 878 | stats->tx_dropped++; |
| 879 | kfree_skb(skb); |
| 880 | return NETDEV_TX_OK; |
| 881 | } |
| 882 | |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 883 | static netdev_tx_t ip6erspan_tunnel_xmit(struct sk_buff *skb, |
| 884 | struct net_device *dev) |
| 885 | { |
| 886 | struct ipv6hdr *ipv6h = ipv6_hdr(skb); |
| 887 | struct ip6_tnl *t = netdev_priv(dev); |
| 888 | struct dst_entry *dst = skb_dst(skb); |
| 889 | struct net_device_stats *stats; |
| 890 | bool truncate = false; |
| 891 | int encap_limit = -1; |
| 892 | __u8 dsfield = false; |
| 893 | struct flowi6 fl6; |
| 894 | int err = -EINVAL; |
| 895 | __u32 mtu; |
| 896 | |
| 897 | if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr)) |
| 898 | goto tx_err; |
| 899 | |
| 900 | if (gre_handle_offloads(skb, false)) |
| 901 | goto tx_err; |
| 902 | |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 903 | if (skb->len > dev->mtu + dev->hard_header_len) { |
| 904 | pskb_trim(skb, dev->mtu + dev->hard_header_len); |
| 905 | truncate = true; |
| 906 | } |
| 907 | |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 908 | t->parms.o_flags &= ~TUNNEL_KEY; |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 909 | IPCB(skb)->flags = 0; |
William Tu | ef7baf5 | 2017-12-05 15:15:44 -0800 | [diff] [blame] | 910 | |
| 911 | /* For collect_md mode, derive fl6 from the tunnel key, |
| 912 | * for native mode, call prepare_ip6gre_xmit_{ipv4,ipv6}. |
| 913 | */ |
| 914 | if (t->parms.collect_md) { |
| 915 | struct ip_tunnel_info *tun_info; |
| 916 | const struct ip_tunnel_key *key; |
| 917 | struct erspan_metadata *md; |
William Tu | c69de58 | 2018-01-25 13:20:09 -0800 | [diff] [blame] | 918 | __be32 tun_id; |
William Tu | ef7baf5 | 2017-12-05 15:15:44 -0800 | [diff] [blame] | 919 | |
| 920 | tun_info = skb_tunnel_info(skb); |
| 921 | if (unlikely(!tun_info || |
| 922 | !(tun_info->mode & IP_TUNNEL_INFO_TX) || |
| 923 | ip_tunnel_info_af(tun_info) != AF_INET6)) |
| 924 | return -EINVAL; |
| 925 | |
| 926 | key = &tun_info->key; |
| 927 | memset(&fl6, 0, sizeof(fl6)); |
| 928 | fl6.flowi6_proto = IPPROTO_GRE; |
| 929 | fl6.daddr = key->u.ipv6.dst; |
| 930 | fl6.flowlabel = key->label; |
| 931 | fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL); |
| 932 | |
| 933 | dsfield = key->tos; |
| 934 | md = ip_tunnel_info_opts(tun_info); |
| 935 | if (!md) |
| 936 | goto tx_err; |
| 937 | |
William Tu | c69de58 | 2018-01-25 13:20:09 -0800 | [diff] [blame] | 938 | tun_id = tunnel_id_to_key32(key->tun_id); |
William Tu | 94d7d8f | 2017-12-13 16:38:57 -0800 | [diff] [blame] | 939 | if (md->version == 1) { |
| 940 | erspan_build_header(skb, |
William Tu | c69de58 | 2018-01-25 13:20:09 -0800 | [diff] [blame] | 941 | ntohl(tun_id), |
William Tu | 94d7d8f | 2017-12-13 16:38:57 -0800 | [diff] [blame] | 942 | ntohl(md->u.index), truncate, |
| 943 | false); |
| 944 | } else if (md->version == 2) { |
William Tu | 94d7d8f | 2017-12-13 16:38:57 -0800 | [diff] [blame] | 945 | erspan_build_header_v2(skb, |
William Tu | c69de58 | 2018-01-25 13:20:09 -0800 | [diff] [blame] | 946 | ntohl(tun_id), |
| 947 | md->u.md2.dir, |
| 948 | get_hwid(&md->u.md2), |
| 949 | truncate, false); |
William Tu | 94d7d8f | 2017-12-13 16:38:57 -0800 | [diff] [blame] | 950 | } |
William Tu | ef7baf5 | 2017-12-05 15:15:44 -0800 | [diff] [blame] | 951 | } else { |
| 952 | switch (skb->protocol) { |
| 953 | case htons(ETH_P_IP): |
| 954 | memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); |
| 955 | prepare_ip6gre_xmit_ipv4(skb, dev, &fl6, |
| 956 | &dsfield, &encap_limit); |
| 957 | break; |
| 958 | case htons(ETH_P_IPV6): |
| 959 | if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr)) |
| 960 | goto tx_err; |
| 961 | if (prepare_ip6gre_xmit_ipv6(skb, dev, &fl6, |
| 962 | &dsfield, &encap_limit)) |
| 963 | goto tx_err; |
| 964 | break; |
| 965 | default: |
| 966 | memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6)); |
| 967 | break; |
| 968 | } |
| 969 | |
William Tu | 94d7d8f | 2017-12-13 16:38:57 -0800 | [diff] [blame] | 970 | if (t->parms.erspan_ver == 1) |
William Tu | c69de58 | 2018-01-25 13:20:09 -0800 | [diff] [blame] | 971 | erspan_build_header(skb, ntohl(t->parms.o_key), |
William Tu | 94d7d8f | 2017-12-13 16:38:57 -0800 | [diff] [blame] | 972 | t->parms.index, |
| 973 | truncate, false); |
| 974 | else |
William Tu | c69de58 | 2018-01-25 13:20:09 -0800 | [diff] [blame] | 975 | erspan_build_header_v2(skb, ntohl(t->parms.o_key), |
William Tu | 94d7d8f | 2017-12-13 16:38:57 -0800 | [diff] [blame] | 976 | t->parms.dir, |
| 977 | t->parms.hwid, |
| 978 | truncate, false); |
William Tu | ef7baf5 | 2017-12-05 15:15:44 -0800 | [diff] [blame] | 979 | fl6.daddr = t->parms.raddr; |
| 980 | } |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 981 | |
| 982 | /* Push GRE header. */ |
| 983 | gre_build_header(skb, 8, TUNNEL_SEQ, |
| 984 | htons(ETH_P_ERSPAN), 0, htonl(t->o_seqno++)); |
| 985 | |
| 986 | /* TooBig packet may have updated dst->dev's mtu */ |
William Tu | ef7baf5 | 2017-12-05 15:15:44 -0800 | [diff] [blame] | 987 | if (!t->parms.collect_md && dst && dst_mtu(dst) > dst->dev->mtu) |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 988 | dst->ops->update_pmtu(dst, NULL, skb, dst->dev->mtu); |
| 989 | |
| 990 | err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu, |
| 991 | NEXTHDR_GRE); |
| 992 | if (err != 0) { |
| 993 | /* XXX: send ICMP error even if DF is not set. */ |
| 994 | if (err == -EMSGSIZE) { |
| 995 | if (skb->protocol == htons(ETH_P_IP)) |
| 996 | icmp_send(skb, ICMP_DEST_UNREACH, |
| 997 | ICMP_FRAG_NEEDED, htonl(mtu)); |
| 998 | else |
| 999 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); |
| 1000 | } |
| 1001 | |
| 1002 | goto tx_err; |
| 1003 | } |
| 1004 | return NETDEV_TX_OK; |
| 1005 | |
| 1006 | tx_err: |
| 1007 | stats = &t->dev->stats; |
| 1008 | stats->tx_errors++; |
| 1009 | stats->tx_dropped++; |
| 1010 | kfree_skb(skb); |
| 1011 | return NETDEV_TX_OK; |
| 1012 | } |
| 1013 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1014 | static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu) |
| 1015 | { |
| 1016 | struct net_device *dev = t->dev; |
| 1017 | struct __ip6_tnl_parm *p = &t->parms; |
| 1018 | struct flowi6 *fl6 = &t->fl.u.ip6; |
Tom Herbert | db2ec95 | 2016-05-09 17:12:08 -0700 | [diff] [blame] | 1019 | int t_hlen; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1020 | |
| 1021 | if (dev->type != ARPHRD_ETHER) { |
| 1022 | memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr)); |
| 1023 | memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr)); |
| 1024 | } |
| 1025 | |
| 1026 | /* Set up flowi template */ |
| 1027 | fl6->saddr = p->laddr; |
| 1028 | fl6->daddr = p->raddr; |
| 1029 | fl6->flowi6_oif = p->link; |
| 1030 | fl6->flowlabel = 0; |
Haishuang Yan | 252f3f5 | 2016-05-21 18:17:35 +0800 | [diff] [blame] | 1031 | fl6->flowi6_proto = IPPROTO_GRE; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1032 | |
| 1033 | if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS)) |
| 1034 | fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo; |
| 1035 | if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL)) |
| 1036 | fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo; |
| 1037 | |
| 1038 | p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET); |
| 1039 | p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr); |
| 1040 | |
| 1041 | if (p->flags&IP6_TNL_F_CAP_XMIT && |
| 1042 | p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER) |
| 1043 | dev->flags |= IFF_POINTOPOINT; |
| 1044 | else |
| 1045 | dev->flags &= ~IFF_POINTOPOINT; |
| 1046 | |
Tom Herbert | db2ec95 | 2016-05-09 17:12:08 -0700 | [diff] [blame] | 1047 | t->tun_hlen = gre_calc_hlen(t->parms.o_flags); |
| 1048 | |
Tom Herbert | 1faf3d9 | 2016-05-18 09:06:19 -0700 | [diff] [blame] | 1049 | t->hlen = t->encap_hlen + t->tun_hlen; |
Tom Herbert | db2ec95 | 2016-05-09 17:12:08 -0700 | [diff] [blame] | 1050 | |
| 1051 | t_hlen = t->hlen + sizeof(struct ipv6hdr); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1052 | |
| 1053 | if (p->flags & IP6_TNL_F_CAP_XMIT) { |
| 1054 | int strict = (ipv6_addr_type(&p->raddr) & |
| 1055 | (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)); |
| 1056 | |
Nicolas Dichtel | 22f0806 | 2014-04-22 10:15:24 +0200 | [diff] [blame] | 1057 | struct rt6_info *rt = rt6_lookup(t->net, |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1058 | &p->raddr, &p->laddr, |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1059 | p->link, NULL, strict); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1060 | |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 1061 | if (!rt) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1062 | return; |
| 1063 | |
| 1064 | if (rt->dst.dev) { |
Tom Herbert | db2ec95 | 2016-05-09 17:12:08 -0700 | [diff] [blame] | 1065 | dev->hard_header_len = rt->dst.dev->hard_header_len + |
| 1066 | t_hlen; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1067 | |
| 1068 | if (set_mtu) { |
Tom Herbert | db2ec95 | 2016-05-09 17:12:08 -0700 | [diff] [blame] | 1069 | dev->mtu = rt->dst.dev->mtu - t_hlen; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1070 | if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) |
| 1071 | dev->mtu -= 8; |
Alexander Duyck | a9e242c | 2016-04-14 15:33:45 -0400 | [diff] [blame] | 1072 | if (dev->type == ARPHRD_ETHER) |
| 1073 | dev->mtu -= ETH_HLEN; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1074 | |
| 1075 | if (dev->mtu < IPV6_MIN_MTU) |
| 1076 | dev->mtu = IPV6_MIN_MTU; |
| 1077 | } |
| 1078 | } |
Amerigo Wang | 94e187c | 2012-10-29 00:13:19 +0000 | [diff] [blame] | 1079 | ip6_rt_put(rt); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1080 | } |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | static int ip6gre_tnl_change(struct ip6_tnl *t, |
| 1084 | const struct __ip6_tnl_parm *p, int set_mtu) |
| 1085 | { |
| 1086 | t->parms.laddr = p->laddr; |
| 1087 | t->parms.raddr = p->raddr; |
| 1088 | t->parms.flags = p->flags; |
| 1089 | t->parms.hop_limit = p->hop_limit; |
| 1090 | t->parms.encap_limit = p->encap_limit; |
| 1091 | t->parms.flowinfo = p->flowinfo; |
| 1092 | t->parms.link = p->link; |
| 1093 | t->parms.proto = p->proto; |
| 1094 | t->parms.i_key = p->i_key; |
| 1095 | t->parms.o_key = p->o_key; |
| 1096 | t->parms.i_flags = p->i_flags; |
| 1097 | t->parms.o_flags = p->o_flags; |
Craig Gallek | 0a473b8 | 2017-04-19 12:30:53 -0400 | [diff] [blame] | 1098 | t->parms.fwmark = p->fwmark; |
Paolo Abeni | 607f725 | 2016-02-12 15:43:54 +0100 | [diff] [blame] | 1099 | dst_cache_reset(&t->dst_cache); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1100 | ip6gre_tnl_link_config(t, set_mtu); |
| 1101 | return 0; |
| 1102 | } |
| 1103 | |
| 1104 | static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p, |
| 1105 | const struct ip6_tnl_parm2 *u) |
| 1106 | { |
| 1107 | p->laddr = u->laddr; |
| 1108 | p->raddr = u->raddr; |
| 1109 | p->flags = u->flags; |
| 1110 | p->hop_limit = u->hop_limit; |
| 1111 | p->encap_limit = u->encap_limit; |
| 1112 | p->flowinfo = u->flowinfo; |
| 1113 | p->link = u->link; |
| 1114 | p->i_key = u->i_key; |
| 1115 | p->o_key = u->o_key; |
Tom Herbert | f41fe3c | 2016-05-09 17:12:09 -0700 | [diff] [blame] | 1116 | p->i_flags = gre_flags_to_tnl_flags(u->i_flags); |
| 1117 | p->o_flags = gre_flags_to_tnl_flags(u->o_flags); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1118 | memcpy(p->name, u->name, sizeof(u->name)); |
| 1119 | } |
| 1120 | |
| 1121 | static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u, |
| 1122 | const struct __ip6_tnl_parm *p) |
| 1123 | { |
| 1124 | u->proto = IPPROTO_GRE; |
| 1125 | u->laddr = p->laddr; |
| 1126 | u->raddr = p->raddr; |
| 1127 | u->flags = p->flags; |
| 1128 | u->hop_limit = p->hop_limit; |
| 1129 | u->encap_limit = p->encap_limit; |
| 1130 | u->flowinfo = p->flowinfo; |
| 1131 | u->link = p->link; |
| 1132 | u->i_key = p->i_key; |
| 1133 | u->o_key = p->o_key; |
Tom Herbert | f41fe3c | 2016-05-09 17:12:09 -0700 | [diff] [blame] | 1134 | u->i_flags = gre_tnl_flags_to_gre_flags(p->i_flags); |
| 1135 | u->o_flags = gre_tnl_flags_to_gre_flags(p->o_flags); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1136 | memcpy(u->name, p->name, sizeof(u->name)); |
| 1137 | } |
| 1138 | |
| 1139 | static int ip6gre_tunnel_ioctl(struct net_device *dev, |
| 1140 | struct ifreq *ifr, int cmd) |
| 1141 | { |
| 1142 | int err = 0; |
| 1143 | struct ip6_tnl_parm2 p; |
| 1144 | struct __ip6_tnl_parm p1; |
Nicolas Dichtel | 22f0806 | 2014-04-22 10:15:24 +0200 | [diff] [blame] | 1145 | struct ip6_tnl *t = netdev_priv(dev); |
| 1146 | struct net *net = t->net; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1147 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); |
| 1148 | |
Tom Herbert | 308edfdf | 2016-04-29 17:12:17 -0700 | [diff] [blame] | 1149 | memset(&p1, 0, sizeof(p1)); |
| 1150 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1151 | switch (cmd) { |
| 1152 | case SIOCGETTUNNEL: |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1153 | if (dev == ign->fb_tunnel_dev) { |
| 1154 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) { |
| 1155 | err = -EFAULT; |
| 1156 | break; |
| 1157 | } |
| 1158 | ip6gre_tnl_parm_from_user(&p1, &p); |
| 1159 | t = ip6gre_tunnel_locate(net, &p1, 0); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 1160 | if (!t) |
Nicolas Dichtel | 22f0806 | 2014-04-22 10:15:24 +0200 | [diff] [blame] | 1161 | t = netdev_priv(dev); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1162 | } |
Amerigo Wang | 5dbd506 | 2013-05-09 21:56:37 +0000 | [diff] [blame] | 1163 | memset(&p, 0, sizeof(p)); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1164 | ip6gre_tnl_parm_to_user(&p, &t->parms); |
| 1165 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) |
| 1166 | err = -EFAULT; |
| 1167 | break; |
| 1168 | |
| 1169 | case SIOCADDTUNNEL: |
| 1170 | case SIOCCHGTUNNEL: |
| 1171 | err = -EPERM; |
Eric W. Biederman | af31f41 | 2012-11-16 03:03:06 +0000 | [diff] [blame] | 1172 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1173 | goto done; |
| 1174 | |
| 1175 | err = -EFAULT; |
| 1176 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) |
| 1177 | goto done; |
| 1178 | |
| 1179 | err = -EINVAL; |
| 1180 | if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING)) |
| 1181 | goto done; |
| 1182 | |
| 1183 | if (!(p.i_flags&GRE_KEY)) |
| 1184 | p.i_key = 0; |
| 1185 | if (!(p.o_flags&GRE_KEY)) |
| 1186 | p.o_key = 0; |
| 1187 | |
| 1188 | ip6gre_tnl_parm_from_user(&p1, &p); |
| 1189 | t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL); |
| 1190 | |
| 1191 | if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) { |
Ian Morris | 53b24b8 | 2015-03-29 14:00:05 +0100 | [diff] [blame] | 1192 | if (t) { |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1193 | if (t->dev != dev) { |
| 1194 | err = -EEXIST; |
| 1195 | break; |
| 1196 | } |
| 1197 | } else { |
| 1198 | t = netdev_priv(dev); |
| 1199 | |
| 1200 | ip6gre_tunnel_unlink(ign, t); |
| 1201 | synchronize_net(); |
| 1202 | ip6gre_tnl_change(t, &p1, 1); |
| 1203 | ip6gre_tunnel_link(ign, t); |
| 1204 | netdev_state_change(dev); |
| 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | if (t) { |
| 1209 | err = 0; |
| 1210 | |
Amerigo Wang | 5dbd506 | 2013-05-09 21:56:37 +0000 | [diff] [blame] | 1211 | memset(&p, 0, sizeof(p)); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1212 | ip6gre_tnl_parm_to_user(&p, &t->parms); |
| 1213 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) |
| 1214 | err = -EFAULT; |
| 1215 | } else |
| 1216 | err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT); |
| 1217 | break; |
| 1218 | |
| 1219 | case SIOCDELTUNNEL: |
| 1220 | err = -EPERM; |
Eric W. Biederman | af31f41 | 2012-11-16 03:03:06 +0000 | [diff] [blame] | 1221 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1222 | goto done; |
| 1223 | |
| 1224 | if (dev == ign->fb_tunnel_dev) { |
| 1225 | err = -EFAULT; |
| 1226 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) |
| 1227 | goto done; |
| 1228 | err = -ENOENT; |
| 1229 | ip6gre_tnl_parm_from_user(&p1, &p); |
| 1230 | t = ip6gre_tunnel_locate(net, &p1, 0); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 1231 | if (!t) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1232 | goto done; |
| 1233 | err = -EPERM; |
| 1234 | if (t == netdev_priv(ign->fb_tunnel_dev)) |
| 1235 | goto done; |
| 1236 | dev = t->dev; |
| 1237 | } |
| 1238 | unregister_netdevice(dev); |
| 1239 | err = 0; |
| 1240 | break; |
| 1241 | |
| 1242 | default: |
| 1243 | err = -EINVAL; |
| 1244 | } |
| 1245 | |
| 1246 | done: |
| 1247 | return err; |
| 1248 | } |
| 1249 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1250 | static int ip6gre_header(struct sk_buff *skb, struct net_device *dev, |
Xin Long | 76cc0d3 | 2017-09-15 12:00:07 +0800 | [diff] [blame] | 1251 | unsigned short type, const void *daddr, |
| 1252 | const void *saddr, unsigned int len) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1253 | { |
| 1254 | struct ip6_tnl *t = netdev_priv(dev); |
Xin Long | 76cc0d3 | 2017-09-15 12:00:07 +0800 | [diff] [blame] | 1255 | struct ipv6hdr *ipv6h; |
| 1256 | __be16 *p; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1257 | |
Xin Long | 76cc0d3 | 2017-09-15 12:00:07 +0800 | [diff] [blame] | 1258 | ipv6h = skb_push(skb, t->hlen + sizeof(*ipv6h)); |
| 1259 | ip6_flow_hdr(ipv6h, 0, ip6_make_flowlabel(dev_net(dev), skb, |
| 1260 | t->fl.u.ip6.flowlabel, |
| 1261 | true, &t->fl.u.ip6)); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1262 | ipv6h->hop_limit = t->parms.hop_limit; |
| 1263 | ipv6h->nexthdr = NEXTHDR_GRE; |
| 1264 | ipv6h->saddr = t->parms.laddr; |
| 1265 | ipv6h->daddr = t->parms.raddr; |
| 1266 | |
Xin Long | 76cc0d3 | 2017-09-15 12:00:07 +0800 | [diff] [blame] | 1267 | p = (__be16 *)(ipv6h + 1); |
| 1268 | p[0] = t->parms.o_flags; |
| 1269 | p[1] = htons(type); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1270 | |
| 1271 | /* |
| 1272 | * Set the source hardware address. |
| 1273 | */ |
| 1274 | |
| 1275 | if (saddr) |
| 1276 | memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr)); |
| 1277 | if (daddr) |
| 1278 | memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr)); |
| 1279 | if (!ipv6_addr_any(&ipv6h->daddr)) |
| 1280 | return t->hlen; |
| 1281 | |
| 1282 | return -t->hlen; |
| 1283 | } |
| 1284 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1285 | static const struct header_ops ip6gre_header_ops = { |
| 1286 | .create = ip6gre_header, |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1287 | }; |
| 1288 | |
| 1289 | static const struct net_device_ops ip6gre_netdev_ops = { |
| 1290 | .ndo_init = ip6gre_tunnel_init, |
| 1291 | .ndo_uninit = ip6gre_tunnel_uninit, |
| 1292 | .ndo_start_xmit = ip6gre_tunnel_xmit, |
| 1293 | .ndo_do_ioctl = ip6gre_tunnel_ioctl, |
Tom Herbert | b05229f | 2016-04-29 17:12:21 -0700 | [diff] [blame] | 1294 | .ndo_change_mtu = ip6_tnl_change_mtu, |
Pravin B Shelar | f61dd38 | 2013-03-25 14:50:00 +0000 | [diff] [blame] | 1295 | .ndo_get_stats64 = ip_tunnel_get_stats64, |
Nicolas Dichtel | ecf2c06 | 2015-04-02 17:07:01 +0200 | [diff] [blame] | 1296 | .ndo_get_iflink = ip6_tnl_get_iflink, |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1297 | }; |
| 1298 | |
| 1299 | static void ip6gre_dev_free(struct net_device *dev) |
| 1300 | { |
Martin KaFai Lau | cdf3464 | 2015-09-15 14:30:07 -0700 | [diff] [blame] | 1301 | struct ip6_tnl *t = netdev_priv(dev); |
| 1302 | |
Paolo Abeni | 607f725 | 2016-02-12 15:43:54 +0100 | [diff] [blame] | 1303 | dst_cache_destroy(&t->dst_cache); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1304 | free_percpu(dev->tstats); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1305 | } |
| 1306 | |
| 1307 | static void ip6gre_tunnel_setup(struct net_device *dev) |
| 1308 | { |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1309 | dev->netdev_ops = &ip6gre_netdev_ops; |
David S. Miller | cf124db | 2017-05-08 12:52:56 -0400 | [diff] [blame] | 1310 | dev->needs_free_netdev = true; |
| 1311 | dev->priv_destructor = ip6gre_dev_free; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1312 | |
| 1313 | dev->type = ARPHRD_IP6GRE; |
Tom Herbert | b05229f | 2016-04-29 17:12:21 -0700 | [diff] [blame] | 1314 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1315 | dev->flags |= IFF_NOARP; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1316 | dev->addr_len = sizeof(struct in6_addr); |
Eric Dumazet | 0287587 | 2014-10-05 18:38:35 -0700 | [diff] [blame] | 1317 | netif_keep_dst(dev); |
Felix Jia | 45ce0fd | 2017-01-26 16:59:18 +1300 | [diff] [blame] | 1318 | /* This perm addr will be used as interface identifier by IPv6 */ |
| 1319 | dev->addr_assign_type = NET_ADDR_RANDOM; |
| 1320 | eth_random_addr(dev->perm_addr); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1321 | } |
| 1322 | |
Alexey Kodanev | e5a9336 | 2017-12-20 19:36:03 +0300 | [diff] [blame] | 1323 | #define GRE6_FEATURES (NETIF_F_SG | \ |
| 1324 | NETIF_F_FRAGLIST | \ |
| 1325 | NETIF_F_HIGHDMA | \ |
| 1326 | NETIF_F_HW_CSUM) |
| 1327 | |
| 1328 | static void ip6gre_tnl_init_features(struct net_device *dev) |
| 1329 | { |
| 1330 | struct ip6_tnl *nt = netdev_priv(dev); |
| 1331 | |
| 1332 | dev->features |= GRE6_FEATURES; |
| 1333 | dev->hw_features |= GRE6_FEATURES; |
| 1334 | |
| 1335 | if (!(nt->parms.o_flags & TUNNEL_SEQ)) { |
| 1336 | /* TCP offload with GRE SEQ is not supported, nor |
| 1337 | * can we support 2 levels of outer headers requiring |
| 1338 | * an update. |
| 1339 | */ |
| 1340 | if (!(nt->parms.o_flags & TUNNEL_CSUM) || |
| 1341 | nt->encap.type == TUNNEL_ENCAP_NONE) { |
| 1342 | dev->features |= NETIF_F_GSO_SOFTWARE; |
| 1343 | dev->hw_features |= NETIF_F_GSO_SOFTWARE; |
| 1344 | } |
| 1345 | |
| 1346 | /* Can use a lockless transmit, unless we generate |
| 1347 | * output sequences |
| 1348 | */ |
| 1349 | dev->features |= NETIF_F_LLTX; |
| 1350 | } |
| 1351 | } |
| 1352 | |
Martin KaFai Lau | a3c119d | 2015-09-15 14:30:05 -0700 | [diff] [blame] | 1353 | static int ip6gre_tunnel_init_common(struct net_device *dev) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1354 | { |
| 1355 | struct ip6_tnl *tunnel; |
Martin KaFai Lau | cdf3464 | 2015-09-15 14:30:07 -0700 | [diff] [blame] | 1356 | int ret; |
Tom Herbert | b05229f | 2016-04-29 17:12:21 -0700 | [diff] [blame] | 1357 | int t_hlen; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1358 | |
| 1359 | tunnel = netdev_priv(dev); |
| 1360 | |
| 1361 | tunnel->dev = dev; |
Nicolas Dichtel | 0bd87628 | 2013-08-13 17:51:12 +0200 | [diff] [blame] | 1362 | tunnel->net = dev_net(dev); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1363 | strcpy(tunnel->parms.name, dev->name); |
| 1364 | |
Martin KaFai Lau | a3c119d | 2015-09-15 14:30:05 -0700 | [diff] [blame] | 1365 | dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); |
| 1366 | if (!dev->tstats) |
| 1367 | return -ENOMEM; |
| 1368 | |
Paolo Abeni | 607f725 | 2016-02-12 15:43:54 +0100 | [diff] [blame] | 1369 | ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL); |
Martin KaFai Lau | cdf3464 | 2015-09-15 14:30:07 -0700 | [diff] [blame] | 1370 | if (ret) { |
| 1371 | free_percpu(dev->tstats); |
| 1372 | dev->tstats = NULL; |
| 1373 | return ret; |
| 1374 | } |
| 1375 | |
Tom Herbert | b05229f | 2016-04-29 17:12:21 -0700 | [diff] [blame] | 1376 | tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags); |
Tom Herbert | 1faf3d9 | 2016-05-18 09:06:19 -0700 | [diff] [blame] | 1377 | tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen; |
Tom Herbert | b05229f | 2016-04-29 17:12:21 -0700 | [diff] [blame] | 1378 | t_hlen = tunnel->hlen + sizeof(struct ipv6hdr); |
| 1379 | |
Tom Herbert | db2ec95 | 2016-05-09 17:12:08 -0700 | [diff] [blame] | 1380 | dev->hard_header_len = LL_MAX_HEADER + t_hlen; |
| 1381 | dev->mtu = ETH_DATA_LEN - t_hlen; |
Haishuang Yan | 1b227e5 | 2016-05-21 18:17:34 +0800 | [diff] [blame] | 1382 | if (dev->type == ARPHRD_ETHER) |
| 1383 | dev->mtu -= ETH_HLEN; |
Tom Herbert | b05229f | 2016-04-29 17:12:21 -0700 | [diff] [blame] | 1384 | if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) |
| 1385 | dev->mtu -= 8; |
| 1386 | |
William Tu | 6712abc | 2017-12-01 15:26:08 -0800 | [diff] [blame] | 1387 | if (tunnel->parms.collect_md) { |
| 1388 | dev->features |= NETIF_F_NETNS_LOCAL; |
| 1389 | netif_keep_dst(dev); |
| 1390 | } |
Alexey Kodanev | e5a9336 | 2017-12-20 19:36:03 +0300 | [diff] [blame] | 1391 | ip6gre_tnl_init_features(dev); |
William Tu | 6712abc | 2017-12-01 15:26:08 -0800 | [diff] [blame] | 1392 | |
Martin KaFai Lau | a3c119d | 2015-09-15 14:30:05 -0700 | [diff] [blame] | 1393 | return 0; |
| 1394 | } |
| 1395 | |
| 1396 | static int ip6gre_tunnel_init(struct net_device *dev) |
| 1397 | { |
| 1398 | struct ip6_tnl *tunnel; |
| 1399 | int ret; |
| 1400 | |
| 1401 | ret = ip6gre_tunnel_init_common(dev); |
| 1402 | if (ret) |
| 1403 | return ret; |
| 1404 | |
| 1405 | tunnel = netdev_priv(dev); |
| 1406 | |
William Tu | 6712abc | 2017-12-01 15:26:08 -0800 | [diff] [blame] | 1407 | if (tunnel->parms.collect_md) |
| 1408 | return 0; |
| 1409 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1410 | memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr)); |
| 1411 | memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr)); |
| 1412 | |
| 1413 | if (ipv6_addr_any(&tunnel->parms.raddr)) |
| 1414 | dev->header_ops = &ip6gre_header_ops; |
| 1415 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1416 | return 0; |
| 1417 | } |
| 1418 | |
| 1419 | static void ip6gre_fb_tunnel_init(struct net_device *dev) |
| 1420 | { |
| 1421 | struct ip6_tnl *tunnel = netdev_priv(dev); |
| 1422 | |
| 1423 | tunnel->dev = dev; |
Nicolas Dichtel | 0bd87628 | 2013-08-13 17:51:12 +0200 | [diff] [blame] | 1424 | tunnel->net = dev_net(dev); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1425 | strcpy(tunnel->parms.name, dev->name); |
| 1426 | |
| 1427 | tunnel->hlen = sizeof(struct ipv6hdr) + 4; |
| 1428 | |
| 1429 | dev_hold(dev); |
| 1430 | } |
| 1431 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1432 | static struct inet6_protocol ip6gre_protocol __read_mostly = { |
Tom Herbert | 308edfdf | 2016-04-29 17:12:17 -0700 | [diff] [blame] | 1433 | .handler = gre_rcv, |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1434 | .err_handler = ip6gre_err, |
| 1435 | .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL, |
| 1436 | }; |
| 1437 | |
Nicolas Dichtel | 22f0806 | 2014-04-22 10:15:24 +0200 | [diff] [blame] | 1438 | static void ip6gre_destroy_tunnels(struct net *net, struct list_head *head) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1439 | { |
Nicolas Dichtel | 22f0806 | 2014-04-22 10:15:24 +0200 | [diff] [blame] | 1440 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); |
| 1441 | struct net_device *dev, *aux; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1442 | int prio; |
| 1443 | |
Nicolas Dichtel | 22f0806 | 2014-04-22 10:15:24 +0200 | [diff] [blame] | 1444 | for_each_netdev_safe(net, dev, aux) |
| 1445 | if (dev->rtnl_link_ops == &ip6gre_link_ops || |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 1446 | dev->rtnl_link_ops == &ip6gre_tap_ops || |
| 1447 | dev->rtnl_link_ops == &ip6erspan_tap_ops) |
Nicolas Dichtel | 22f0806 | 2014-04-22 10:15:24 +0200 | [diff] [blame] | 1448 | unregister_netdevice_queue(dev, head); |
| 1449 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1450 | for (prio = 0; prio < 4; prio++) { |
| 1451 | int h; |
Jiri Kosina | e87a8f2 | 2016-08-10 11:03:35 +0200 | [diff] [blame] | 1452 | for (h = 0; h < IP6_GRE_HASH_SIZE; h++) { |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1453 | struct ip6_tnl *t; |
| 1454 | |
| 1455 | t = rtnl_dereference(ign->tunnels[prio][h]); |
| 1456 | |
Ian Morris | 53b24b8 | 2015-03-29 14:00:05 +0100 | [diff] [blame] | 1457 | while (t) { |
Nicolas Dichtel | 22f0806 | 2014-04-22 10:15:24 +0200 | [diff] [blame] | 1458 | /* If dev is in the same netns, it has already |
| 1459 | * been added to the list by the previous loop. |
| 1460 | */ |
| 1461 | if (!net_eq(dev_net(t->dev), net)) |
| 1462 | unregister_netdevice_queue(t->dev, |
| 1463 | head); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1464 | t = rtnl_dereference(t->next); |
| 1465 | } |
| 1466 | } |
| 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | static int __net_init ip6gre_init_net(struct net *net) |
| 1471 | { |
| 1472 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); |
| 1473 | int err; |
| 1474 | |
Eric Dumazet | 79134e6 | 2018-03-08 12:51:41 -0800 | [diff] [blame] | 1475 | if (!net_has_fallback_tunnels(net)) |
| 1476 | return 0; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1477 | ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0", |
Tom Gundersen | c835a67 | 2014-07-14 16:37:24 +0200 | [diff] [blame] | 1478 | NET_NAME_UNKNOWN, |
| 1479 | ip6gre_tunnel_setup); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1480 | if (!ign->fb_tunnel_dev) { |
| 1481 | err = -ENOMEM; |
| 1482 | goto err_alloc_dev; |
| 1483 | } |
| 1484 | dev_net_set(ign->fb_tunnel_dev, net); |
Nicolas Dichtel | 22f0806 | 2014-04-22 10:15:24 +0200 | [diff] [blame] | 1485 | /* FB netdevice is special: we have one, and only one per netns. |
| 1486 | * Allowing to move it to another netns is clearly unsafe. |
| 1487 | */ |
| 1488 | ign->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL; |
| 1489 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1490 | |
| 1491 | ip6gre_fb_tunnel_init(ign->fb_tunnel_dev); |
| 1492 | ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops; |
| 1493 | |
| 1494 | err = register_netdev(ign->fb_tunnel_dev); |
| 1495 | if (err) |
| 1496 | goto err_reg_dev; |
| 1497 | |
| 1498 | rcu_assign_pointer(ign->tunnels_wc[0], |
| 1499 | netdev_priv(ign->fb_tunnel_dev)); |
| 1500 | return 0; |
| 1501 | |
| 1502 | err_reg_dev: |
David S. Miller | cf124db | 2017-05-08 12:52:56 -0400 | [diff] [blame] | 1503 | free_netdev(ign->fb_tunnel_dev); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1504 | err_alloc_dev: |
| 1505 | return err; |
| 1506 | } |
| 1507 | |
Eric Dumazet | bb401ca | 2017-09-19 16:27:08 -0700 | [diff] [blame] | 1508 | static void __net_exit ip6gre_exit_batch_net(struct list_head *net_list) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1509 | { |
Eric Dumazet | bb401ca | 2017-09-19 16:27:08 -0700 | [diff] [blame] | 1510 | struct net *net; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1511 | LIST_HEAD(list); |
| 1512 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1513 | rtnl_lock(); |
Eric Dumazet | bb401ca | 2017-09-19 16:27:08 -0700 | [diff] [blame] | 1514 | list_for_each_entry(net, net_list, exit_list) |
| 1515 | ip6gre_destroy_tunnels(net, &list); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1516 | unregister_netdevice_many(&list); |
| 1517 | rtnl_unlock(); |
| 1518 | } |
| 1519 | |
| 1520 | static struct pernet_operations ip6gre_net_ops = { |
| 1521 | .init = ip6gre_init_net, |
Eric Dumazet | bb401ca | 2017-09-19 16:27:08 -0700 | [diff] [blame] | 1522 | .exit_batch = ip6gre_exit_batch_net, |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1523 | .id = &ip6gre_net_id, |
| 1524 | .size = sizeof(struct ip6gre_net), |
Kirill Tkhai | 5c155c5 | 2018-02-26 16:02:03 +0300 | [diff] [blame] | 1525 | .async = true, |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1526 | }; |
| 1527 | |
Matthias Schiffer | a8b8a889 | 2017-06-25 23:56:01 +0200 | [diff] [blame] | 1528 | static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[], |
| 1529 | struct netlink_ext_ack *extack) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1530 | { |
| 1531 | __be16 flags; |
| 1532 | |
| 1533 | if (!data) |
| 1534 | return 0; |
| 1535 | |
| 1536 | flags = 0; |
| 1537 | if (data[IFLA_GRE_IFLAGS]) |
| 1538 | flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]); |
| 1539 | if (data[IFLA_GRE_OFLAGS]) |
| 1540 | flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]); |
| 1541 | if (flags & (GRE_VERSION|GRE_ROUTING)) |
| 1542 | return -EINVAL; |
| 1543 | |
| 1544 | return 0; |
| 1545 | } |
| 1546 | |
Matthias Schiffer | a8b8a889 | 2017-06-25 23:56:01 +0200 | [diff] [blame] | 1547 | static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[], |
| 1548 | struct netlink_ext_ack *extack) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1549 | { |
| 1550 | struct in6_addr daddr; |
| 1551 | |
| 1552 | if (tb[IFLA_ADDRESS]) { |
| 1553 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) |
| 1554 | return -EINVAL; |
| 1555 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) |
| 1556 | return -EADDRNOTAVAIL; |
| 1557 | } |
| 1558 | |
| 1559 | if (!data) |
| 1560 | goto out; |
| 1561 | |
| 1562 | if (data[IFLA_GRE_REMOTE]) { |
Jiri Benc | 67b61f6 | 2015-03-29 16:59:26 +0200 | [diff] [blame] | 1563 | daddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1564 | if (ipv6_addr_any(&daddr)) |
| 1565 | return -EINVAL; |
| 1566 | } |
| 1567 | |
| 1568 | out: |
Matthias Schiffer | a8b8a889 | 2017-06-25 23:56:01 +0200 | [diff] [blame] | 1569 | return ip6gre_tunnel_validate(tb, data, extack); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 1572 | static int ip6erspan_tap_validate(struct nlattr *tb[], struct nlattr *data[], |
| 1573 | struct netlink_ext_ack *extack) |
| 1574 | { |
| 1575 | __be16 flags = 0; |
William Tu | 94d7d8f | 2017-12-13 16:38:57 -0800 | [diff] [blame] | 1576 | int ret, ver = 0; |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 1577 | |
| 1578 | if (!data) |
| 1579 | return 0; |
| 1580 | |
| 1581 | ret = ip6gre_tap_validate(tb, data, extack); |
| 1582 | if (ret) |
| 1583 | return ret; |
| 1584 | |
| 1585 | /* ERSPAN should only have GRE sequence and key flag */ |
| 1586 | if (data[IFLA_GRE_OFLAGS]) |
| 1587 | flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]); |
| 1588 | if (data[IFLA_GRE_IFLAGS]) |
| 1589 | flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]); |
| 1590 | if (!data[IFLA_GRE_COLLECT_METADATA] && |
| 1591 | flags != (GRE_SEQ | GRE_KEY)) |
| 1592 | return -EINVAL; |
| 1593 | |
| 1594 | /* ERSPAN Session ID only has 10-bit. Since we reuse |
| 1595 | * 32-bit key field as ID, check it's range. |
| 1596 | */ |
| 1597 | if (data[IFLA_GRE_IKEY] && |
| 1598 | (ntohl(nla_get_be32(data[IFLA_GRE_IKEY])) & ~ID_MASK)) |
| 1599 | return -EINVAL; |
| 1600 | |
| 1601 | if (data[IFLA_GRE_OKEY] && |
| 1602 | (ntohl(nla_get_be32(data[IFLA_GRE_OKEY])) & ~ID_MASK)) |
| 1603 | return -EINVAL; |
| 1604 | |
William Tu | 94d7d8f | 2017-12-13 16:38:57 -0800 | [diff] [blame] | 1605 | if (data[IFLA_GRE_ERSPAN_VER]) { |
| 1606 | ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]); |
| 1607 | if (ver != 1 && ver != 2) |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 1608 | return -EINVAL; |
| 1609 | } |
William Tu | 94d7d8f | 2017-12-13 16:38:57 -0800 | [diff] [blame] | 1610 | |
| 1611 | if (ver == 1) { |
| 1612 | if (data[IFLA_GRE_ERSPAN_INDEX]) { |
| 1613 | u32 index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]); |
| 1614 | |
| 1615 | if (index & ~INDEX_MASK) |
| 1616 | return -EINVAL; |
| 1617 | } |
| 1618 | } else if (ver == 2) { |
| 1619 | if (data[IFLA_GRE_ERSPAN_DIR]) { |
| 1620 | u16 dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]); |
| 1621 | |
| 1622 | if (dir & ~(DIR_MASK >> DIR_OFFSET)) |
| 1623 | return -EINVAL; |
| 1624 | } |
| 1625 | |
| 1626 | if (data[IFLA_GRE_ERSPAN_HWID]) { |
| 1627 | u16 hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]); |
| 1628 | |
| 1629 | if (hwid & ~(HWID_MASK >> HWID_OFFSET)) |
| 1630 | return -EINVAL; |
| 1631 | } |
| 1632 | } |
| 1633 | |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 1634 | return 0; |
| 1635 | } |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1636 | |
| 1637 | static void ip6gre_netlink_parms(struct nlattr *data[], |
| 1638 | struct __ip6_tnl_parm *parms) |
| 1639 | { |
| 1640 | memset(parms, 0, sizeof(*parms)); |
| 1641 | |
| 1642 | if (!data) |
| 1643 | return; |
| 1644 | |
| 1645 | if (data[IFLA_GRE_LINK]) |
| 1646 | parms->link = nla_get_u32(data[IFLA_GRE_LINK]); |
| 1647 | |
| 1648 | if (data[IFLA_GRE_IFLAGS]) |
Tom Herbert | f41fe3c | 2016-05-09 17:12:09 -0700 | [diff] [blame] | 1649 | parms->i_flags = gre_flags_to_tnl_flags( |
| 1650 | nla_get_be16(data[IFLA_GRE_IFLAGS])); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1651 | |
| 1652 | if (data[IFLA_GRE_OFLAGS]) |
Tom Herbert | f41fe3c | 2016-05-09 17:12:09 -0700 | [diff] [blame] | 1653 | parms->o_flags = gre_flags_to_tnl_flags( |
| 1654 | nla_get_be16(data[IFLA_GRE_OFLAGS])); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1655 | |
| 1656 | if (data[IFLA_GRE_IKEY]) |
| 1657 | parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]); |
| 1658 | |
| 1659 | if (data[IFLA_GRE_OKEY]) |
| 1660 | parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]); |
| 1661 | |
| 1662 | if (data[IFLA_GRE_LOCAL]) |
Jiri Benc | 67b61f6 | 2015-03-29 16:59:26 +0200 | [diff] [blame] | 1663 | parms->laddr = nla_get_in6_addr(data[IFLA_GRE_LOCAL]); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1664 | |
| 1665 | if (data[IFLA_GRE_REMOTE]) |
Jiri Benc | 67b61f6 | 2015-03-29 16:59:26 +0200 | [diff] [blame] | 1666 | parms->raddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1667 | |
| 1668 | if (data[IFLA_GRE_TTL]) |
| 1669 | parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]); |
| 1670 | |
| 1671 | if (data[IFLA_GRE_ENCAP_LIMIT]) |
| 1672 | parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]); |
| 1673 | |
| 1674 | if (data[IFLA_GRE_FLOWINFO]) |
Lance Richardson | c2675de | 2016-09-24 14:01:04 -0400 | [diff] [blame] | 1675 | parms->flowinfo = nla_get_be32(data[IFLA_GRE_FLOWINFO]); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1676 | |
| 1677 | if (data[IFLA_GRE_FLAGS]) |
| 1678 | parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]); |
Craig Gallek | 0a473b8 | 2017-04-19 12:30:53 -0400 | [diff] [blame] | 1679 | |
| 1680 | if (data[IFLA_GRE_FWMARK]) |
| 1681 | parms->fwmark = nla_get_u32(data[IFLA_GRE_FWMARK]); |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 1682 | |
William Tu | 6712abc | 2017-12-01 15:26:08 -0800 | [diff] [blame] | 1683 | if (data[IFLA_GRE_COLLECT_METADATA]) |
| 1684 | parms->collect_md = true; |
William Tu | 94d7d8f | 2017-12-13 16:38:57 -0800 | [diff] [blame] | 1685 | |
| 1686 | if (data[IFLA_GRE_ERSPAN_VER]) |
| 1687 | parms->erspan_ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]); |
| 1688 | |
| 1689 | if (parms->erspan_ver == 1) { |
| 1690 | if (data[IFLA_GRE_ERSPAN_INDEX]) |
| 1691 | parms->index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]); |
| 1692 | } else if (parms->erspan_ver == 2) { |
| 1693 | if (data[IFLA_GRE_ERSPAN_DIR]) |
| 1694 | parms->dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]); |
| 1695 | if (data[IFLA_GRE_ERSPAN_HWID]) |
| 1696 | parms->hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]); |
| 1697 | } |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1698 | } |
| 1699 | |
| 1700 | static int ip6gre_tap_init(struct net_device *dev) |
| 1701 | { |
Martin KaFai Lau | a3c119d | 2015-09-15 14:30:05 -0700 | [diff] [blame] | 1702 | int ret; |
| 1703 | |
| 1704 | ret = ip6gre_tunnel_init_common(dev); |
| 1705 | if (ret) |
| 1706 | return ret; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1707 | |
Shweta Choudaha | 0a46baa | 2016-06-08 20:15:43 +0100 | [diff] [blame] | 1708 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; |
| 1709 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1710 | return 0; |
| 1711 | } |
| 1712 | |
| 1713 | static const struct net_device_ops ip6gre_tap_netdev_ops = { |
| 1714 | .ndo_init = ip6gre_tap_init, |
| 1715 | .ndo_uninit = ip6gre_tunnel_uninit, |
| 1716 | .ndo_start_xmit = ip6gre_tunnel_xmit, |
| 1717 | .ndo_set_mac_address = eth_mac_addr, |
| 1718 | .ndo_validate_addr = eth_validate_addr, |
Tom Herbert | b05229f | 2016-04-29 17:12:21 -0700 | [diff] [blame] | 1719 | .ndo_change_mtu = ip6_tnl_change_mtu, |
Pravin B Shelar | f61dd38 | 2013-03-25 14:50:00 +0000 | [diff] [blame] | 1720 | .ndo_get_stats64 = ip_tunnel_get_stats64, |
Nicolas Dichtel | ecf2c06 | 2015-04-02 17:07:01 +0200 | [diff] [blame] | 1721 | .ndo_get_iflink = ip6_tnl_get_iflink, |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1722 | }; |
| 1723 | |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 1724 | static int ip6erspan_tap_init(struct net_device *dev) |
| 1725 | { |
| 1726 | struct ip6_tnl *tunnel; |
| 1727 | int t_hlen; |
| 1728 | int ret; |
| 1729 | |
| 1730 | tunnel = netdev_priv(dev); |
| 1731 | |
| 1732 | tunnel->dev = dev; |
| 1733 | tunnel->net = dev_net(dev); |
| 1734 | strcpy(tunnel->parms.name, dev->name); |
| 1735 | |
| 1736 | dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); |
| 1737 | if (!dev->tstats) |
| 1738 | return -ENOMEM; |
| 1739 | |
| 1740 | ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL); |
| 1741 | if (ret) { |
| 1742 | free_percpu(dev->tstats); |
| 1743 | dev->tstats = NULL; |
| 1744 | return ret; |
| 1745 | } |
| 1746 | |
| 1747 | tunnel->tun_hlen = 8; |
| 1748 | tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen + |
William Tu | 94d7d8f | 2017-12-13 16:38:57 -0800 | [diff] [blame] | 1749 | erspan_hdr_len(tunnel->parms.erspan_ver); |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 1750 | t_hlen = tunnel->hlen + sizeof(struct ipv6hdr); |
| 1751 | |
| 1752 | dev->hard_header_len = LL_MAX_HEADER + t_hlen; |
| 1753 | dev->mtu = ETH_DATA_LEN - t_hlen; |
| 1754 | if (dev->type == ARPHRD_ETHER) |
| 1755 | dev->mtu -= ETH_HLEN; |
| 1756 | if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) |
| 1757 | dev->mtu -= 8; |
| 1758 | |
| 1759 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; |
| 1760 | tunnel = netdev_priv(dev); |
| 1761 | ip6gre_tnl_link_config(tunnel, 1); |
| 1762 | |
| 1763 | return 0; |
| 1764 | } |
| 1765 | |
| 1766 | static const struct net_device_ops ip6erspan_netdev_ops = { |
| 1767 | .ndo_init = ip6erspan_tap_init, |
| 1768 | .ndo_uninit = ip6gre_tunnel_uninit, |
| 1769 | .ndo_start_xmit = ip6erspan_tunnel_xmit, |
| 1770 | .ndo_set_mac_address = eth_mac_addr, |
| 1771 | .ndo_validate_addr = eth_validate_addr, |
| 1772 | .ndo_change_mtu = ip6_tnl_change_mtu, |
| 1773 | .ndo_get_stats64 = ip_tunnel_get_stats64, |
| 1774 | .ndo_get_iflink = ip6_tnl_get_iflink, |
| 1775 | }; |
| 1776 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1777 | static void ip6gre_tap_setup(struct net_device *dev) |
| 1778 | { |
| 1779 | |
| 1780 | ether_setup(dev); |
| 1781 | |
Xin Long | 2c52129 | 2017-12-18 14:25:09 +0800 | [diff] [blame] | 1782 | dev->max_mtu = 0; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1783 | dev->netdev_ops = &ip6gre_tap_netdev_ops; |
David S. Miller | cf124db | 2017-05-08 12:52:56 -0400 | [diff] [blame] | 1784 | dev->needs_free_netdev = true; |
| 1785 | dev->priv_destructor = ip6gre_dev_free; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1786 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1787 | dev->features |= NETIF_F_NETNS_LOCAL; |
Jiri Benc | d13b161 | 2016-02-17 15:32:53 +0100 | [diff] [blame] | 1788 | dev->priv_flags &= ~IFF_TX_SKB_SHARING; |
Shweta Choudaha | 0a46baa | 2016-06-08 20:15:43 +0100 | [diff] [blame] | 1789 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; |
Xin Long | 2d40557 | 2017-09-28 13:23:50 +0800 | [diff] [blame] | 1790 | netif_keep_dst(dev); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1791 | } |
| 1792 | |
Petr Machata | d1b2a6c | 2018-02-27 14:53:37 +0100 | [diff] [blame] | 1793 | bool is_ip6gretap_dev(const struct net_device *dev) |
| 1794 | { |
| 1795 | return dev->netdev_ops == &ip6gre_tap_netdev_ops; |
| 1796 | } |
| 1797 | EXPORT_SYMBOL_GPL(is_ip6gretap_dev); |
| 1798 | |
Tom Herbert | 1faf3d9 | 2016-05-18 09:06:19 -0700 | [diff] [blame] | 1799 | static bool ip6gre_netlink_encap_parms(struct nlattr *data[], |
| 1800 | struct ip_tunnel_encap *ipencap) |
| 1801 | { |
| 1802 | bool ret = false; |
| 1803 | |
| 1804 | memset(ipencap, 0, sizeof(*ipencap)); |
| 1805 | |
| 1806 | if (!data) |
| 1807 | return ret; |
| 1808 | |
| 1809 | if (data[IFLA_GRE_ENCAP_TYPE]) { |
| 1810 | ret = true; |
| 1811 | ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]); |
| 1812 | } |
| 1813 | |
| 1814 | if (data[IFLA_GRE_ENCAP_FLAGS]) { |
| 1815 | ret = true; |
| 1816 | ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]); |
| 1817 | } |
| 1818 | |
| 1819 | if (data[IFLA_GRE_ENCAP_SPORT]) { |
| 1820 | ret = true; |
| 1821 | ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]); |
| 1822 | } |
| 1823 | |
| 1824 | if (data[IFLA_GRE_ENCAP_DPORT]) { |
| 1825 | ret = true; |
| 1826 | ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]); |
| 1827 | } |
| 1828 | |
| 1829 | return ret; |
| 1830 | } |
| 1831 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1832 | static int ip6gre_newlink(struct net *src_net, struct net_device *dev, |
Matthias Schiffer | 7a3f4a1 | 2017-06-25 23:55:59 +0200 | [diff] [blame] | 1833 | struct nlattr *tb[], struct nlattr *data[], |
| 1834 | struct netlink_ext_ack *extack) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1835 | { |
| 1836 | struct ip6_tnl *nt; |
| 1837 | struct net *net = dev_net(dev); |
| 1838 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); |
Tom Herbert | 1faf3d9 | 2016-05-18 09:06:19 -0700 | [diff] [blame] | 1839 | struct ip_tunnel_encap ipencap; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1840 | int err; |
| 1841 | |
| 1842 | nt = netdev_priv(dev); |
Tom Herbert | 1faf3d9 | 2016-05-18 09:06:19 -0700 | [diff] [blame] | 1843 | |
| 1844 | if (ip6gre_netlink_encap_parms(data, &ipencap)) { |
| 1845 | int err = ip6_tnl_encap_setup(nt, &ipencap); |
| 1846 | |
| 1847 | if (err < 0) |
| 1848 | return err; |
| 1849 | } |
| 1850 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1851 | ip6gre_netlink_parms(data, &nt->parms); |
| 1852 | |
William Tu | 6712abc | 2017-12-01 15:26:08 -0800 | [diff] [blame] | 1853 | if (nt->parms.collect_md) { |
| 1854 | if (rtnl_dereference(ign->collect_md_tun)) |
| 1855 | return -EEXIST; |
| 1856 | } else { |
| 1857 | if (ip6gre_tunnel_find(net, &nt->parms, dev->type)) |
| 1858 | return -EEXIST; |
| 1859 | } |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1860 | |
| 1861 | if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS]) |
| 1862 | eth_hw_addr_random(dev); |
| 1863 | |
| 1864 | nt->dev = dev; |
Nicolas Dichtel | 0bd87628 | 2013-08-13 17:51:12 +0200 | [diff] [blame] | 1865 | nt->net = dev_net(dev); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1866 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1867 | err = register_netdevice(dev); |
| 1868 | if (err) |
| 1869 | goto out; |
| 1870 | |
Alexey Kodanev | 128bb97 | 2018-01-18 20:51:12 +0300 | [diff] [blame] | 1871 | ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]); |
| 1872 | |
| 1873 | if (tb[IFLA_MTU]) |
| 1874 | ip6_tnl_change_mtu(dev, nla_get_u32(tb[IFLA_MTU])); |
| 1875 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1876 | dev_hold(dev); |
| 1877 | ip6gre_tunnel_link(ign, nt); |
| 1878 | |
| 1879 | out: |
| 1880 | return err; |
| 1881 | } |
| 1882 | |
| 1883 | static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[], |
Matthias Schiffer | ad744b2 | 2017-06-25 23:56:00 +0200 | [diff] [blame] | 1884 | struct nlattr *data[], |
| 1885 | struct netlink_ext_ack *extack) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1886 | { |
Nicolas Dichtel | 22f0806 | 2014-04-22 10:15:24 +0200 | [diff] [blame] | 1887 | struct ip6_tnl *t, *nt = netdev_priv(dev); |
| 1888 | struct net *net = nt->net; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1889 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); |
| 1890 | struct __ip6_tnl_parm p; |
Tom Herbert | 1faf3d9 | 2016-05-18 09:06:19 -0700 | [diff] [blame] | 1891 | struct ip_tunnel_encap ipencap; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1892 | |
| 1893 | if (dev == ign->fb_tunnel_dev) |
| 1894 | return -EINVAL; |
| 1895 | |
Tom Herbert | 1faf3d9 | 2016-05-18 09:06:19 -0700 | [diff] [blame] | 1896 | if (ip6gre_netlink_encap_parms(data, &ipencap)) { |
| 1897 | int err = ip6_tnl_encap_setup(nt, &ipencap); |
| 1898 | |
| 1899 | if (err < 0) |
| 1900 | return err; |
| 1901 | } |
| 1902 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1903 | ip6gre_netlink_parms(data, &p); |
| 1904 | |
| 1905 | t = ip6gre_tunnel_locate(net, &p, 0); |
| 1906 | |
| 1907 | if (t) { |
| 1908 | if (t->dev != dev) |
| 1909 | return -EEXIST; |
| 1910 | } else { |
| 1911 | t = nt; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1912 | } |
| 1913 | |
Nicolas Dichtel | 6a61d4d | 2015-12-03 17:21:50 +0100 | [diff] [blame] | 1914 | ip6gre_tunnel_unlink(ign, t); |
| 1915 | ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]); |
| 1916 | ip6gre_tunnel_link(ign, t); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1917 | return 0; |
| 1918 | } |
| 1919 | |
Nicolas Dichtel | 54d63f78 | 2014-04-14 17:11:38 +0200 | [diff] [blame] | 1920 | static void ip6gre_dellink(struct net_device *dev, struct list_head *head) |
| 1921 | { |
| 1922 | struct net *net = dev_net(dev); |
| 1923 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); |
| 1924 | |
| 1925 | if (dev != ign->fb_tunnel_dev) |
| 1926 | unregister_netdevice_queue(dev, head); |
| 1927 | } |
| 1928 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1929 | static size_t ip6gre_get_size(const struct net_device *dev) |
| 1930 | { |
| 1931 | return |
| 1932 | /* IFLA_GRE_LINK */ |
| 1933 | nla_total_size(4) + |
| 1934 | /* IFLA_GRE_IFLAGS */ |
| 1935 | nla_total_size(2) + |
| 1936 | /* IFLA_GRE_OFLAGS */ |
| 1937 | nla_total_size(2) + |
| 1938 | /* IFLA_GRE_IKEY */ |
| 1939 | nla_total_size(4) + |
| 1940 | /* IFLA_GRE_OKEY */ |
| 1941 | nla_total_size(4) + |
| 1942 | /* IFLA_GRE_LOCAL */ |
Nicolas Dichtel | a375413 | 2012-11-09 05:34:56 +0000 | [diff] [blame] | 1943 | nla_total_size(sizeof(struct in6_addr)) + |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1944 | /* IFLA_GRE_REMOTE */ |
Nicolas Dichtel | a375413 | 2012-11-09 05:34:56 +0000 | [diff] [blame] | 1945 | nla_total_size(sizeof(struct in6_addr)) + |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1946 | /* IFLA_GRE_TTL */ |
| 1947 | nla_total_size(1) + |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1948 | /* IFLA_GRE_ENCAP_LIMIT */ |
| 1949 | nla_total_size(1) + |
| 1950 | /* IFLA_GRE_FLOWINFO */ |
| 1951 | nla_total_size(4) + |
| 1952 | /* IFLA_GRE_FLAGS */ |
| 1953 | nla_total_size(4) + |
Tom Herbert | 1faf3d9 | 2016-05-18 09:06:19 -0700 | [diff] [blame] | 1954 | /* IFLA_GRE_ENCAP_TYPE */ |
| 1955 | nla_total_size(2) + |
| 1956 | /* IFLA_GRE_ENCAP_FLAGS */ |
| 1957 | nla_total_size(2) + |
| 1958 | /* IFLA_GRE_ENCAP_SPORT */ |
| 1959 | nla_total_size(2) + |
| 1960 | /* IFLA_GRE_ENCAP_DPORT */ |
| 1961 | nla_total_size(2) + |
William Tu | 6712abc | 2017-12-01 15:26:08 -0800 | [diff] [blame] | 1962 | /* IFLA_GRE_COLLECT_METADATA */ |
| 1963 | nla_total_size(0) + |
Craig Gallek | 0a473b8 | 2017-04-19 12:30:53 -0400 | [diff] [blame] | 1964 | /* IFLA_GRE_FWMARK */ |
| 1965 | nla_total_size(4) + |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 1966 | /* IFLA_GRE_ERSPAN_INDEX */ |
| 1967 | nla_total_size(4) + |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1968 | 0; |
| 1969 | } |
| 1970 | |
| 1971 | static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev) |
| 1972 | { |
| 1973 | struct ip6_tnl *t = netdev_priv(dev); |
| 1974 | struct __ip6_tnl_parm *p = &t->parms; |
| 1975 | |
| 1976 | if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) || |
Tom Herbert | f41fe3c | 2016-05-09 17:12:09 -0700 | [diff] [blame] | 1977 | nla_put_be16(skb, IFLA_GRE_IFLAGS, |
| 1978 | gre_tnl_flags_to_gre_flags(p->i_flags)) || |
| 1979 | nla_put_be16(skb, IFLA_GRE_OFLAGS, |
| 1980 | gre_tnl_flags_to_gre_flags(p->o_flags)) || |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1981 | nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) || |
| 1982 | nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) || |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 1983 | nla_put_in6_addr(skb, IFLA_GRE_LOCAL, &p->laddr) || |
| 1984 | nla_put_in6_addr(skb, IFLA_GRE_REMOTE, &p->raddr) || |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1985 | nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) || |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1986 | nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) || |
| 1987 | nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) || |
Craig Gallek | 0a473b8 | 2017-04-19 12:30:53 -0400 | [diff] [blame] | 1988 | nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags) || |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 1989 | nla_put_u32(skb, IFLA_GRE_FWMARK, p->fwmark) || |
| 1990 | nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, p->index)) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1991 | goto nla_put_failure; |
Tom Herbert | 1faf3d9 | 2016-05-18 09:06:19 -0700 | [diff] [blame] | 1992 | |
| 1993 | if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE, |
| 1994 | t->encap.type) || |
| 1995 | nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT, |
| 1996 | t->encap.sport) || |
| 1997 | nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT, |
| 1998 | t->encap.dport) || |
| 1999 | nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS, |
| 2000 | t->encap.flags)) |
| 2001 | goto nla_put_failure; |
| 2002 | |
William Tu | 6712abc | 2017-12-01 15:26:08 -0800 | [diff] [blame] | 2003 | if (p->collect_md) { |
| 2004 | if (nla_put_flag(skb, IFLA_GRE_COLLECT_METADATA)) |
| 2005 | goto nla_put_failure; |
| 2006 | } |
| 2007 | |
William Tu | 94d7d8f | 2017-12-13 16:38:57 -0800 | [diff] [blame] | 2008 | if (nla_put_u8(skb, IFLA_GRE_ERSPAN_VER, p->erspan_ver)) |
| 2009 | goto nla_put_failure; |
| 2010 | |
| 2011 | if (p->erspan_ver == 1) { |
| 2012 | if (nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, p->index)) |
| 2013 | goto nla_put_failure; |
| 2014 | } else if (p->erspan_ver == 2) { |
| 2015 | if (nla_put_u8(skb, IFLA_GRE_ERSPAN_DIR, p->dir)) |
| 2016 | goto nla_put_failure; |
| 2017 | if (nla_put_u16(skb, IFLA_GRE_ERSPAN_HWID, p->hwid)) |
| 2018 | goto nla_put_failure; |
| 2019 | } |
| 2020 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 2021 | return 0; |
| 2022 | |
| 2023 | nla_put_failure: |
| 2024 | return -EMSGSIZE; |
| 2025 | } |
| 2026 | |
| 2027 | static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = { |
| 2028 | [IFLA_GRE_LINK] = { .type = NLA_U32 }, |
| 2029 | [IFLA_GRE_IFLAGS] = { .type = NLA_U16 }, |
| 2030 | [IFLA_GRE_OFLAGS] = { .type = NLA_U16 }, |
| 2031 | [IFLA_GRE_IKEY] = { .type = NLA_U32 }, |
| 2032 | [IFLA_GRE_OKEY] = { .type = NLA_U32 }, |
| 2033 | [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct ipv6hdr, saddr) }, |
| 2034 | [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct ipv6hdr, daddr) }, |
| 2035 | [IFLA_GRE_TTL] = { .type = NLA_U8 }, |
| 2036 | [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 }, |
| 2037 | [IFLA_GRE_FLOWINFO] = { .type = NLA_U32 }, |
| 2038 | [IFLA_GRE_FLAGS] = { .type = NLA_U32 }, |
Tom Herbert | 1faf3d9 | 2016-05-18 09:06:19 -0700 | [diff] [blame] | 2039 | [IFLA_GRE_ENCAP_TYPE] = { .type = NLA_U16 }, |
| 2040 | [IFLA_GRE_ENCAP_FLAGS] = { .type = NLA_U16 }, |
| 2041 | [IFLA_GRE_ENCAP_SPORT] = { .type = NLA_U16 }, |
| 2042 | [IFLA_GRE_ENCAP_DPORT] = { .type = NLA_U16 }, |
William Tu | 6712abc | 2017-12-01 15:26:08 -0800 | [diff] [blame] | 2043 | [IFLA_GRE_COLLECT_METADATA] = { .type = NLA_FLAG }, |
Craig Gallek | 0a473b8 | 2017-04-19 12:30:53 -0400 | [diff] [blame] | 2044 | [IFLA_GRE_FWMARK] = { .type = NLA_U32 }, |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 2045 | [IFLA_GRE_ERSPAN_INDEX] = { .type = NLA_U32 }, |
William Tu | 94d7d8f | 2017-12-13 16:38:57 -0800 | [diff] [blame] | 2046 | [IFLA_GRE_ERSPAN_VER] = { .type = NLA_U8 }, |
| 2047 | [IFLA_GRE_ERSPAN_DIR] = { .type = NLA_U8 }, |
| 2048 | [IFLA_GRE_ERSPAN_HWID] = { .type = NLA_U16 }, |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 2049 | }; |
| 2050 | |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 2051 | static void ip6erspan_tap_setup(struct net_device *dev) |
| 2052 | { |
| 2053 | ether_setup(dev); |
| 2054 | |
| 2055 | dev->netdev_ops = &ip6erspan_netdev_ops; |
| 2056 | dev->needs_free_netdev = true; |
| 2057 | dev->priv_destructor = ip6gre_dev_free; |
| 2058 | |
| 2059 | dev->features |= NETIF_F_NETNS_LOCAL; |
| 2060 | dev->priv_flags &= ~IFF_TX_SKB_SHARING; |
| 2061 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; |
| 2062 | netif_keep_dst(dev); |
| 2063 | } |
| 2064 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 2065 | static struct rtnl_link_ops ip6gre_link_ops __read_mostly = { |
| 2066 | .kind = "ip6gre", |
| 2067 | .maxtype = IFLA_GRE_MAX, |
| 2068 | .policy = ip6gre_policy, |
| 2069 | .priv_size = sizeof(struct ip6_tnl), |
| 2070 | .setup = ip6gre_tunnel_setup, |
| 2071 | .validate = ip6gre_tunnel_validate, |
| 2072 | .newlink = ip6gre_newlink, |
| 2073 | .changelink = ip6gre_changelink, |
Nicolas Dichtel | 54d63f78 | 2014-04-14 17:11:38 +0200 | [diff] [blame] | 2074 | .dellink = ip6gre_dellink, |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 2075 | .get_size = ip6gre_get_size, |
| 2076 | .fill_info = ip6gre_fill_info, |
Nicolas Dichtel | 1728d4f | 2015-01-15 15:11:17 +0100 | [diff] [blame] | 2077 | .get_link_net = ip6_tnl_get_link_net, |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 2078 | }; |
| 2079 | |
| 2080 | static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = { |
| 2081 | .kind = "ip6gretap", |
| 2082 | .maxtype = IFLA_GRE_MAX, |
| 2083 | .policy = ip6gre_policy, |
| 2084 | .priv_size = sizeof(struct ip6_tnl), |
| 2085 | .setup = ip6gre_tap_setup, |
| 2086 | .validate = ip6gre_tap_validate, |
| 2087 | .newlink = ip6gre_newlink, |
| 2088 | .changelink = ip6gre_changelink, |
| 2089 | .get_size = ip6gre_get_size, |
| 2090 | .fill_info = ip6gre_fill_info, |
Nicolas Dichtel | 3390e39 | 2015-01-20 15:15:43 +0100 | [diff] [blame] | 2091 | .get_link_net = ip6_tnl_get_link_net, |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 2092 | }; |
| 2093 | |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 2094 | static struct rtnl_link_ops ip6erspan_tap_ops __read_mostly = { |
| 2095 | .kind = "ip6erspan", |
| 2096 | .maxtype = IFLA_GRE_MAX, |
| 2097 | .policy = ip6gre_policy, |
| 2098 | .priv_size = sizeof(struct ip6_tnl), |
| 2099 | .setup = ip6erspan_tap_setup, |
| 2100 | .validate = ip6erspan_tap_validate, |
| 2101 | .newlink = ip6gre_newlink, |
| 2102 | .changelink = ip6gre_changelink, |
| 2103 | .get_size = ip6gre_get_size, |
| 2104 | .fill_info = ip6gre_fill_info, |
| 2105 | .get_link_net = ip6_tnl_get_link_net, |
| 2106 | }; |
| 2107 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 2108 | /* |
| 2109 | * And now the modules code and kernel interface. |
| 2110 | */ |
| 2111 | |
| 2112 | static int __init ip6gre_init(void) |
| 2113 | { |
| 2114 | int err; |
| 2115 | |
| 2116 | pr_info("GRE over IPv6 tunneling driver\n"); |
| 2117 | |
| 2118 | err = register_pernet_device(&ip6gre_net_ops); |
| 2119 | if (err < 0) |
| 2120 | return err; |
| 2121 | |
| 2122 | err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE); |
| 2123 | if (err < 0) { |
| 2124 | pr_info("%s: can't add protocol\n", __func__); |
| 2125 | goto add_proto_failed; |
| 2126 | } |
| 2127 | |
| 2128 | err = rtnl_link_register(&ip6gre_link_ops); |
| 2129 | if (err < 0) |
| 2130 | goto rtnl_link_failed; |
| 2131 | |
| 2132 | err = rtnl_link_register(&ip6gre_tap_ops); |
| 2133 | if (err < 0) |
| 2134 | goto tap_ops_failed; |
| 2135 | |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 2136 | err = rtnl_link_register(&ip6erspan_tap_ops); |
| 2137 | if (err < 0) |
| 2138 | goto erspan_link_failed; |
| 2139 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 2140 | out: |
| 2141 | return err; |
| 2142 | |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 2143 | erspan_link_failed: |
| 2144 | rtnl_link_unregister(&ip6gre_tap_ops); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 2145 | tap_ops_failed: |
| 2146 | rtnl_link_unregister(&ip6gre_link_ops); |
| 2147 | rtnl_link_failed: |
| 2148 | inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE); |
| 2149 | add_proto_failed: |
| 2150 | unregister_pernet_device(&ip6gre_net_ops); |
| 2151 | goto out; |
| 2152 | } |
| 2153 | |
| 2154 | static void __exit ip6gre_fini(void) |
| 2155 | { |
| 2156 | rtnl_link_unregister(&ip6gre_tap_ops); |
| 2157 | rtnl_link_unregister(&ip6gre_link_ops); |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 2158 | rtnl_link_unregister(&ip6erspan_tap_ops); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 2159 | inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE); |
| 2160 | unregister_pernet_device(&ip6gre_net_ops); |
| 2161 | } |
| 2162 | |
| 2163 | module_init(ip6gre_init); |
| 2164 | module_exit(ip6gre_fini); |
| 2165 | MODULE_LICENSE("GPL"); |
| 2166 | MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)"); |
| 2167 | MODULE_DESCRIPTION("GRE over IPv6 tunneling device"); |
| 2168 | MODULE_ALIAS_RTNL_LINK("ip6gre"); |
Nicolas Dichtel | 5a4ee9a | 2014-09-24 11:03:00 +0200 | [diff] [blame] | 2169 | MODULE_ALIAS_RTNL_LINK("ip6gretap"); |
William Tu | 94d7d8f | 2017-12-13 16:38:57 -0800 | [diff] [blame] | 2170 | MODULE_ALIAS_RTNL_LINK("ip6erspan"); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 2171 | MODULE_ALIAS_NETDEV("ip6gre0"); |