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; |
| 239 | if (dev->flags & IFF_UP) |
| 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 | ip6gre_tnl_link_config(nt, 1); |
| 356 | |
| 357 | if (register_netdevice(dev) < 0) |
| 358 | goto failed_free; |
| 359 | |
| 360 | /* Can use a lockless transmit, unless we generate output sequences */ |
Tom Herbert | b45bd1d | 2016-05-09 17:12:12 -0700 | [diff] [blame] | 361 | if (!(nt->parms.o_flags & TUNNEL_SEQ)) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 362 | dev->features |= NETIF_F_LLTX; |
| 363 | |
| 364 | dev_hold(dev); |
| 365 | ip6gre_tunnel_link(ign, nt); |
| 366 | return nt; |
| 367 | |
| 368 | failed_free: |
| 369 | free_netdev(dev); |
| 370 | return NULL; |
| 371 | } |
| 372 | |
| 373 | static void ip6gre_tunnel_uninit(struct net_device *dev) |
| 374 | { |
Nicolas Dichtel | 22f0806 | 2014-04-22 10:15:24 +0200 | [diff] [blame] | 375 | struct ip6_tnl *t = netdev_priv(dev); |
| 376 | struct ip6gre_net *ign = net_generic(t->net, ip6gre_net_id); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 377 | |
Nicolas Dichtel | 22f0806 | 2014-04-22 10:15:24 +0200 | [diff] [blame] | 378 | ip6gre_tunnel_unlink(ign, t); |
Paolo Abeni | 607f725 | 2016-02-12 15:43:54 +0100 | [diff] [blame] | 379 | dst_cache_reset(&t->dst_cache); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 380 | dev_put(dev); |
| 381 | } |
| 382 | |
| 383 | |
| 384 | 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] | 385 | u8 type, u8 code, int offset, __be32 info) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 386 | { |
Xin Long | 929fc03 | 2017-11-11 19:06:49 +0800 | [diff] [blame] | 387 | struct net *net = dev_net(skb->dev); |
Eric Dumazet | 7892032 | 2017-02-04 23:18:55 -0800 | [diff] [blame] | 388 | const struct gre_base_hdr *greh; |
| 389 | const struct ipv6hdr *ipv6h; |
| 390 | int grehlen = sizeof(*greh); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 391 | struct ip6_tnl *t; |
Eric Dumazet | 7892032 | 2017-02-04 23:18:55 -0800 | [diff] [blame] | 392 | int key_off = 0; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 393 | __be16 flags; |
Eric Dumazet | 7892032 | 2017-02-04 23:18:55 -0800 | [diff] [blame] | 394 | __be32 key; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 395 | |
Eric Dumazet | 7892032 | 2017-02-04 23:18:55 -0800 | [diff] [blame] | 396 | if (!pskb_may_pull(skb, offset + grehlen)) |
| 397 | return; |
| 398 | greh = (const struct gre_base_hdr *)(skb->data + offset); |
| 399 | flags = greh->flags; |
| 400 | if (flags & (GRE_VERSION | GRE_ROUTING)) |
| 401 | return; |
| 402 | if (flags & GRE_CSUM) |
| 403 | grehlen += 4; |
| 404 | if (flags & GRE_KEY) { |
| 405 | key_off = grehlen + offset; |
| 406 | grehlen += 4; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Eric Dumazet | 7892032 | 2017-02-04 23:18:55 -0800 | [diff] [blame] | 409 | if (!pskb_may_pull(skb, offset + grehlen)) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 410 | return; |
Eric Dumazet | b87fb39 | 2012-08-19 03:47:30 +0000 | [diff] [blame] | 411 | ipv6h = (const struct ipv6hdr *)skb->data; |
Eric Dumazet | 7892032 | 2017-02-04 23:18:55 -0800 | [diff] [blame] | 412 | greh = (const struct gre_base_hdr *)(skb->data + offset); |
| 413 | key = key_off ? *(__be32 *)(skb->data + key_off) : 0; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 414 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 415 | t = ip6gre_tunnel_lookup(skb->dev, &ipv6h->daddr, &ipv6h->saddr, |
Eric Dumazet | 7892032 | 2017-02-04 23:18:55 -0800 | [diff] [blame] | 416 | key, greh->protocol); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 417 | if (!t) |
stephen hemminger | 0c5794a | 2012-09-24 18:12:24 +0000 | [diff] [blame] | 418 | return; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 419 | |
| 420 | switch (type) { |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 421 | struct ipv6_tlv_tnl_enc_lim *tel; |
Xin Long | fe1a4ca | 2017-11-11 19:06:50 +0800 | [diff] [blame] | 422 | __u32 teli; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 423 | case ICMPV6_DEST_UNREACH: |
Matt Bennett | a46496c | 2015-09-23 16:58:31 +1200 | [diff] [blame] | 424 | net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n", |
| 425 | t->parms.name); |
Xin Long | f8d20b4 | 2017-10-26 19:23:27 +0800 | [diff] [blame] | 426 | if (code != ICMPV6_PORT_UNREACH) |
| 427 | break; |
| 428 | return; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 429 | case ICMPV6_TIME_EXCEED: |
| 430 | if (code == ICMPV6_EXC_HOPLIMIT) { |
Matt Bennett | a46496c | 2015-09-23 16:58:31 +1200 | [diff] [blame] | 431 | net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n", |
| 432 | t->parms.name); |
Xin Long | f8d20b4 | 2017-10-26 19:23:27 +0800 | [diff] [blame] | 433 | break; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 434 | } |
Xin Long | f8d20b4 | 2017-10-26 19:23:27 +0800 | [diff] [blame] | 435 | return; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 436 | case ICMPV6_PARAMPROB: |
| 437 | teli = 0; |
| 438 | if (code == ICMPV6_HDR_FIELD) |
| 439 | teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data); |
| 440 | |
Sabrina Dubroca | d1e158e | 2015-02-04 15:25:09 +0100 | [diff] [blame] | 441 | if (teli && teli == be32_to_cpu(info) - 2) { |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 442 | tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli]; |
| 443 | if (tel->encap_limit == 0) { |
Matt Bennett | a46496c | 2015-09-23 16:58:31 +1200 | [diff] [blame] | 444 | net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n", |
| 445 | t->parms.name); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 446 | } |
| 447 | } else { |
Matt Bennett | a46496c | 2015-09-23 16:58:31 +1200 | [diff] [blame] | 448 | net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n", |
| 449 | t->parms.name); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 450 | } |
Xin Long | f8d20b4 | 2017-10-26 19:23:27 +0800 | [diff] [blame] | 451 | return; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 452 | case ICMPV6_PKT_TOOBIG: |
Xin Long | fe1a4ca | 2017-11-11 19:06:50 +0800 | [diff] [blame] | 453 | 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] | 454 | return; |
Xin Long | 929fc03 | 2017-11-11 19:06:49 +0800 | [diff] [blame] | 455 | case NDISC_REDIRECT: |
| 456 | ip6_redirect(skb, net, skb->dev->ifindex, 0, |
| 457 | sock_net_uid(net, NULL)); |
| 458 | return; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | if (time_before(jiffies, t->err_time + IP6TUNNEL_ERR_TIMEO)) |
| 462 | t->err_count++; |
| 463 | else |
| 464 | t->err_count = 1; |
| 465 | t->err_time = jiffies; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Tom Herbert | 308edfdf | 2016-04-29 17:12:17 -0700 | [diff] [blame] | 468 | 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] | 469 | { |
| 470 | const struct ipv6hdr *ipv6h; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 471 | struct ip6_tnl *tunnel; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 472 | |
| 473 | ipv6h = ipv6_hdr(skb); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 474 | tunnel = ip6gre_tunnel_lookup(skb->dev, |
Tom Herbert | 308edfdf | 2016-04-29 17:12:17 -0700 | [diff] [blame] | 475 | &ipv6h->saddr, &ipv6h->daddr, tpi->key, |
| 476 | tpi->proto); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 477 | if (tunnel) { |
William Tu | 6712abc | 2017-12-01 15:26:08 -0800 | [diff] [blame^] | 478 | if (tunnel->parms.collect_md) { |
| 479 | struct metadata_dst *tun_dst; |
| 480 | __be64 tun_id; |
| 481 | __be16 flags; |
| 482 | |
| 483 | flags = tpi->flags; |
| 484 | tun_id = key32_to_tunnel_id(tpi->key); |
| 485 | |
| 486 | tun_dst = ipv6_tun_rx_dst(skb, flags, tun_id, 0); |
| 487 | if (!tun_dst) |
| 488 | return PACKET_REJECT; |
| 489 | |
| 490 | ip6_tnl_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error); |
| 491 | } else { |
| 492 | ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error); |
| 493 | } |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 494 | |
Tom Herbert | 308edfdf | 2016-04-29 17:12:17 -0700 | [diff] [blame] | 495 | return PACKET_RCVD; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 496 | } |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 497 | |
Tom Herbert | 308edfdf | 2016-04-29 17:12:17 -0700 | [diff] [blame] | 498 | return PACKET_REJECT; |
| 499 | } |
| 500 | |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 501 | static int ip6erspan_rcv(struct sk_buff *skb, int gre_hdr_len, |
| 502 | struct tnl_ptk_info *tpi) |
| 503 | { |
| 504 | const struct ipv6hdr *ipv6h; |
| 505 | struct erspanhdr *ershdr; |
| 506 | struct ip6_tnl *tunnel; |
| 507 | __be32 index; |
| 508 | |
| 509 | ipv6h = ipv6_hdr(skb); |
| 510 | ershdr = (struct erspanhdr *)skb->data; |
| 511 | |
| 512 | if (unlikely(!pskb_may_pull(skb, sizeof(*ershdr)))) |
| 513 | return PACKET_REJECT; |
| 514 | |
| 515 | tpi->key = cpu_to_be32(ntohs(ershdr->session_id) & ID_MASK); |
| 516 | index = ershdr->md.index; |
| 517 | |
| 518 | tunnel = ip6gre_tunnel_lookup(skb->dev, |
| 519 | &ipv6h->saddr, &ipv6h->daddr, tpi->key, |
| 520 | tpi->proto); |
| 521 | if (tunnel) { |
| 522 | if (__iptunnel_pull_header(skb, sizeof(*ershdr), |
| 523 | htons(ETH_P_TEB), |
| 524 | false, false) < 0) |
| 525 | return PACKET_REJECT; |
| 526 | |
| 527 | tunnel->parms.index = ntohl(index); |
| 528 | ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error); |
| 529 | |
| 530 | return PACKET_RCVD; |
| 531 | } |
| 532 | |
| 533 | return PACKET_REJECT; |
| 534 | } |
| 535 | |
Tom Herbert | 308edfdf | 2016-04-29 17:12:17 -0700 | [diff] [blame] | 536 | static int gre_rcv(struct sk_buff *skb) |
| 537 | { |
| 538 | struct tnl_ptk_info tpi; |
| 539 | bool csum_err = false; |
| 540 | int hdr_len; |
| 541 | |
Eric Dumazet | e582615ad | 2016-06-15 06:24:00 -0700 | [diff] [blame] | 542 | 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] | 543 | if (hdr_len < 0) |
Tom Herbert | 308edfdf | 2016-04-29 17:12:17 -0700 | [diff] [blame] | 544 | goto drop; |
| 545 | |
| 546 | if (iptunnel_pull_header(skb, hdr_len, tpi.proto, false)) |
| 547 | goto drop; |
| 548 | |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 549 | if (unlikely(tpi.proto == htons(ETH_P_ERSPAN))) { |
| 550 | if (ip6erspan_rcv(skb, hdr_len, &tpi) == PACKET_RCVD) |
| 551 | return 0; |
| 552 | goto drop; |
| 553 | } |
| 554 | |
Tom Herbert | 308edfdf | 2016-04-29 17:12:17 -0700 | [diff] [blame] | 555 | if (ip6gre_rcv(skb, &tpi) == PACKET_RCVD) |
| 556 | return 0; |
| 557 | |
| 558 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 559 | drop: |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 560 | kfree_skb(skb); |
| 561 | return 0; |
| 562 | } |
| 563 | |
Tom Herbert | b05229f | 2016-04-29 17:12:21 -0700 | [diff] [blame] | 564 | static int gre_handle_offloads(struct sk_buff *skb, bool csum) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 565 | { |
Tom Herbert | b05229f | 2016-04-29 17:12:21 -0700 | [diff] [blame] | 566 | return iptunnel_handle_offloads(skb, |
| 567 | csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 568 | } |
| 569 | |
William Tu | 898b2979 | 2017-11-30 11:51:28 -0800 | [diff] [blame] | 570 | static void prepare_ip6gre_xmit_ipv4(struct sk_buff *skb, |
| 571 | struct net_device *dev, |
| 572 | struct flowi6 *fl6, __u8 *dsfield, |
| 573 | int *encap_limit) |
| 574 | { |
| 575 | const struct iphdr *iph = ip_hdr(skb); |
| 576 | struct ip6_tnl *t = netdev_priv(dev); |
| 577 | |
| 578 | if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) |
| 579 | *encap_limit = t->parms.encap_limit; |
| 580 | |
| 581 | memcpy(fl6, &t->fl.u.ip6, sizeof(*fl6)); |
| 582 | |
| 583 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS) |
| 584 | *dsfield = ipv4_get_dsfield(iph); |
| 585 | else |
| 586 | *dsfield = ip6_tclass(t->parms.flowinfo); |
| 587 | |
| 588 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK) |
| 589 | fl6->flowi6_mark = skb->mark; |
| 590 | else |
| 591 | fl6->flowi6_mark = t->parms.fwmark; |
| 592 | |
| 593 | fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL); |
| 594 | } |
| 595 | |
| 596 | static int prepare_ip6gre_xmit_ipv6(struct sk_buff *skb, |
| 597 | struct net_device *dev, |
| 598 | struct flowi6 *fl6, __u8 *dsfield, |
| 599 | int *encap_limit) |
| 600 | { |
| 601 | struct ipv6hdr *ipv6h = ipv6_hdr(skb); |
| 602 | struct ip6_tnl *t = netdev_priv(dev); |
| 603 | __u16 offset; |
| 604 | |
| 605 | offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb)); |
| 606 | /* ip6_tnl_parse_tlv_enc_lim() might have reallocated skb->head */ |
| 607 | |
| 608 | if (offset > 0) { |
| 609 | struct ipv6_tlv_tnl_enc_lim *tel; |
| 610 | |
| 611 | tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset]; |
| 612 | if (tel->encap_limit == 0) { |
| 613 | icmpv6_send(skb, ICMPV6_PARAMPROB, |
| 614 | ICMPV6_HDR_FIELD, offset + 2); |
| 615 | return -1; |
| 616 | } |
| 617 | *encap_limit = tel->encap_limit - 1; |
| 618 | } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) { |
| 619 | *encap_limit = t->parms.encap_limit; |
| 620 | } |
| 621 | |
| 622 | memcpy(fl6, &t->fl.u.ip6, sizeof(*fl6)); |
| 623 | |
| 624 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS) |
| 625 | *dsfield = ipv6_get_dsfield(ipv6h); |
| 626 | else |
| 627 | *dsfield = ip6_tclass(t->parms.flowinfo); |
| 628 | |
| 629 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL) |
| 630 | fl6->flowlabel |= ip6_flowlabel(ipv6h); |
| 631 | |
| 632 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK) |
| 633 | fl6->flowi6_mark = skb->mark; |
| 634 | else |
| 635 | fl6->flowi6_mark = t->parms.fwmark; |
| 636 | |
| 637 | fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL); |
| 638 | |
| 639 | return 0; |
| 640 | } |
| 641 | |
Tom Herbert | b05229f | 2016-04-29 17:12:21 -0700 | [diff] [blame] | 642 | static netdev_tx_t __gre6_xmit(struct sk_buff *skb, |
| 643 | struct net_device *dev, __u8 dsfield, |
| 644 | struct flowi6 *fl6, int encap_limit, |
| 645 | __u32 *pmtu, __be16 proto) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 646 | { |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 647 | struct ip6_tnl *tunnel = netdev_priv(dev); |
Xin Long | 8aec495 | 2017-10-26 19:27:17 +0800 | [diff] [blame] | 648 | __be16 protocol; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 649 | |
| 650 | if (dev->type == ARPHRD_ETHER) |
| 651 | IPCB(skb)->flags = 0; |
| 652 | |
Tom Herbert | b05229f | 2016-04-29 17:12:21 -0700 | [diff] [blame] | 653 | if (dev->header_ops && dev->type == ARPHRD_IP6GRE) |
| 654 | fl6->daddr = ((struct ipv6hdr *)skb->data)->daddr; |
| 655 | else |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 656 | fl6->daddr = tunnel->parms.raddr; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 657 | |
Tom Herbert | b05229f | 2016-04-29 17:12:21 -0700 | [diff] [blame] | 658 | if (tunnel->parms.o_flags & TUNNEL_SEQ) |
| 659 | tunnel->o_seqno++; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 660 | |
Tom Herbert | b05229f | 2016-04-29 17:12:21 -0700 | [diff] [blame] | 661 | /* Push GRE header. */ |
Xin Long | 8aec495 | 2017-10-26 19:27:17 +0800 | [diff] [blame] | 662 | protocol = (dev->type == ARPHRD_ETHER) ? htons(ETH_P_TEB) : proto; |
William Tu | 6712abc | 2017-12-01 15:26:08 -0800 | [diff] [blame^] | 663 | |
| 664 | if (tunnel->parms.collect_md) { |
| 665 | struct ip_tunnel_info *tun_info; |
| 666 | const struct ip_tunnel_key *key; |
| 667 | __be16 flags; |
| 668 | |
| 669 | tun_info = skb_tunnel_info(skb); |
| 670 | if (unlikely(!tun_info || |
| 671 | !(tun_info->mode & IP_TUNNEL_INFO_TX) || |
| 672 | ip_tunnel_info_af(tun_info) != AF_INET6)) |
| 673 | return -EINVAL; |
| 674 | |
| 675 | key = &tun_info->key; |
| 676 | memset(fl6, 0, sizeof(*fl6)); |
| 677 | fl6->flowi6_proto = IPPROTO_GRE; |
| 678 | fl6->daddr = key->u.ipv6.dst; |
| 679 | fl6->flowlabel = key->label; |
| 680 | fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL); |
| 681 | |
| 682 | dsfield = key->tos; |
| 683 | flags = key->tun_flags & (TUNNEL_CSUM | TUNNEL_KEY); |
| 684 | tunnel->tun_hlen = gre_calc_hlen(flags); |
| 685 | |
| 686 | gre_build_header(skb, tunnel->tun_hlen, |
| 687 | flags, protocol, |
| 688 | tunnel_id_to_key32(tun_info->key.tun_id), 0); |
| 689 | |
| 690 | } else { |
| 691 | gre_build_header(skb, tunnel->tun_hlen, tunnel->parms.o_flags, |
| 692 | protocol, tunnel->parms.o_key, |
| 693 | htonl(tunnel->o_seqno)); |
| 694 | } |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 695 | |
Tom Herbert | b05229f | 2016-04-29 17:12:21 -0700 | [diff] [blame] | 696 | return ip6_tnl_xmit(skb, dev, dsfield, fl6, encap_limit, pmtu, |
| 697 | NEXTHDR_GRE); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev) |
| 701 | { |
| 702 | struct ip6_tnl *t = netdev_priv(dev); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 703 | int encap_limit = -1; |
| 704 | struct flowi6 fl6; |
William Tu | 6712abc | 2017-12-01 15:26:08 -0800 | [diff] [blame^] | 705 | __u8 dsfield = 0; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 706 | __u32 mtu; |
| 707 | int err; |
| 708 | |
Bernie Harris | 5146d1f | 2016-02-22 12:58:05 +1300 | [diff] [blame] | 709 | memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); |
| 710 | |
William Tu | 6712abc | 2017-12-01 15:26:08 -0800 | [diff] [blame^] | 711 | if (!t->parms.collect_md) |
| 712 | prepare_ip6gre_xmit_ipv4(skb, dev, &fl6, |
| 713 | &dsfield, &encap_limit); |
Lorenzo Colitti | e2d118a | 2016-11-04 02:23:43 +0900 | [diff] [blame] | 714 | |
Tom Herbert | b05229f | 2016-04-29 17:12:21 -0700 | [diff] [blame] | 715 | err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM)); |
| 716 | if (err) |
| 717 | return -1; |
| 718 | |
| 719 | err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu, |
| 720 | skb->protocol); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 721 | if (err != 0) { |
| 722 | /* XXX: send ICMP error even if DF is not set. */ |
| 723 | if (err == -EMSGSIZE) |
| 724 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, |
| 725 | htonl(mtu)); |
| 726 | return -1; |
| 727 | } |
| 728 | |
| 729 | return 0; |
| 730 | } |
| 731 | |
| 732 | static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev) |
| 733 | { |
| 734 | struct ip6_tnl *t = netdev_priv(dev); |
| 735 | struct ipv6hdr *ipv6h = ipv6_hdr(skb); |
| 736 | int encap_limit = -1; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 737 | struct flowi6 fl6; |
William Tu | 6712abc | 2017-12-01 15:26:08 -0800 | [diff] [blame^] | 738 | __u8 dsfield = 0; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 739 | __u32 mtu; |
| 740 | int err; |
| 741 | |
| 742 | if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr)) |
| 743 | return -1; |
| 744 | |
William Tu | 6712abc | 2017-12-01 15:26:08 -0800 | [diff] [blame^] | 745 | if (!t->parms.collect_md && |
| 746 | prepare_ip6gre_xmit_ipv6(skb, dev, &fl6, &dsfield, &encap_limit)) |
William Tu | 898b2979 | 2017-11-30 11:51:28 -0800 | [diff] [blame] | 747 | return -1; |
Lorenzo Colitti | e2d118a | 2016-11-04 02:23:43 +0900 | [diff] [blame] | 748 | |
Tom Herbert | b05229f | 2016-04-29 17:12:21 -0700 | [diff] [blame] | 749 | if (gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM))) |
| 750 | return -1; |
| 751 | |
| 752 | err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit, |
| 753 | &mtu, skb->protocol); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 754 | if (err != 0) { |
| 755 | if (err == -EMSGSIZE) |
| 756 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); |
| 757 | return -1; |
| 758 | } |
| 759 | |
| 760 | return 0; |
| 761 | } |
| 762 | |
| 763 | /** |
| 764 | * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own |
| 765 | * @t: the outgoing tunnel device |
| 766 | * @hdr: IPv6 header from the incoming packet |
| 767 | * |
| 768 | * Description: |
| 769 | * Avoid trivial tunneling loop by checking that tunnel exit-point |
| 770 | * doesn't match source of incoming packet. |
| 771 | * |
| 772 | * Return: |
| 773 | * 1 if conflict, |
| 774 | * 0 else |
| 775 | **/ |
| 776 | |
| 777 | static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl *t, |
| 778 | const struct ipv6hdr *hdr) |
| 779 | { |
| 780 | return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr); |
| 781 | } |
| 782 | |
| 783 | static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev) |
| 784 | { |
| 785 | struct ip6_tnl *t = netdev_priv(dev); |
| 786 | int encap_limit = -1; |
| 787 | struct flowi6 fl6; |
| 788 | __u32 mtu; |
| 789 | int err; |
| 790 | |
| 791 | if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) |
| 792 | encap_limit = t->parms.encap_limit; |
| 793 | |
William Tu | 6712abc | 2017-12-01 15:26:08 -0800 | [diff] [blame^] | 794 | if (!t->parms.collect_md) |
| 795 | memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6)); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 796 | |
Tom Herbert | b05229f | 2016-04-29 17:12:21 -0700 | [diff] [blame] | 797 | err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM)); |
| 798 | if (err) |
| 799 | return err; |
| 800 | |
| 801 | 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] | 802 | |
| 803 | return err; |
| 804 | } |
| 805 | |
| 806 | static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb, |
| 807 | struct net_device *dev) |
| 808 | { |
| 809 | struct ip6_tnl *t = netdev_priv(dev); |
| 810 | struct net_device_stats *stats = &t->dev->stats; |
| 811 | int ret; |
| 812 | |
Steffen Klassert | d500514 | 2014-11-05 08:02:48 +0100 | [diff] [blame] | 813 | if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr)) |
Tommi Rantala | 41ab3e3 | 2013-02-06 03:24:02 +0000 | [diff] [blame] | 814 | goto tx_err; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 815 | |
| 816 | switch (skb->protocol) { |
| 817 | case htons(ETH_P_IP): |
| 818 | ret = ip6gre_xmit_ipv4(skb, dev); |
| 819 | break; |
| 820 | case htons(ETH_P_IPV6): |
| 821 | ret = ip6gre_xmit_ipv6(skb, dev); |
| 822 | break; |
| 823 | default: |
| 824 | ret = ip6gre_xmit_other(skb, dev); |
| 825 | break; |
| 826 | } |
| 827 | |
| 828 | if (ret < 0) |
| 829 | goto tx_err; |
| 830 | |
| 831 | return NETDEV_TX_OK; |
| 832 | |
| 833 | tx_err: |
| 834 | stats->tx_errors++; |
| 835 | stats->tx_dropped++; |
| 836 | kfree_skb(skb); |
| 837 | return NETDEV_TX_OK; |
| 838 | } |
| 839 | |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 840 | static netdev_tx_t ip6erspan_tunnel_xmit(struct sk_buff *skb, |
| 841 | struct net_device *dev) |
| 842 | { |
| 843 | struct ipv6hdr *ipv6h = ipv6_hdr(skb); |
| 844 | struct ip6_tnl *t = netdev_priv(dev); |
| 845 | struct dst_entry *dst = skb_dst(skb); |
| 846 | struct net_device_stats *stats; |
| 847 | bool truncate = false; |
| 848 | int encap_limit = -1; |
| 849 | __u8 dsfield = false; |
| 850 | struct flowi6 fl6; |
| 851 | int err = -EINVAL; |
| 852 | __u32 mtu; |
| 853 | |
| 854 | if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr)) |
| 855 | goto tx_err; |
| 856 | |
| 857 | if (gre_handle_offloads(skb, false)) |
| 858 | goto tx_err; |
| 859 | |
| 860 | switch (skb->protocol) { |
| 861 | case htons(ETH_P_IP): |
| 862 | memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); |
| 863 | prepare_ip6gre_xmit_ipv4(skb, dev, &fl6, |
| 864 | &dsfield, &encap_limit); |
| 865 | break; |
| 866 | case htons(ETH_P_IPV6): |
| 867 | if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr)) |
| 868 | goto tx_err; |
| 869 | if (prepare_ip6gre_xmit_ipv6(skb, dev, &fl6, |
| 870 | &dsfield, &encap_limit)) |
| 871 | goto tx_err; |
| 872 | break; |
| 873 | default: |
| 874 | memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6)); |
| 875 | break; |
| 876 | } |
| 877 | |
| 878 | if (skb->len > dev->mtu + dev->hard_header_len) { |
| 879 | pskb_trim(skb, dev->mtu + dev->hard_header_len); |
| 880 | truncate = true; |
| 881 | } |
| 882 | |
| 883 | erspan_build_header(skb, t->parms.o_key, t->parms.index, |
| 884 | truncate, false); |
| 885 | t->parms.o_flags &= ~TUNNEL_KEY; |
| 886 | |
| 887 | IPCB(skb)->flags = 0; |
| 888 | fl6.daddr = t->parms.raddr; |
| 889 | |
| 890 | /* Push GRE header. */ |
| 891 | gre_build_header(skb, 8, TUNNEL_SEQ, |
| 892 | htons(ETH_P_ERSPAN), 0, htonl(t->o_seqno++)); |
| 893 | |
| 894 | /* TooBig packet may have updated dst->dev's mtu */ |
| 895 | if (dst && dst_mtu(dst) > dst->dev->mtu) |
| 896 | dst->ops->update_pmtu(dst, NULL, skb, dst->dev->mtu); |
| 897 | |
| 898 | err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu, |
| 899 | NEXTHDR_GRE); |
| 900 | if (err != 0) { |
| 901 | /* XXX: send ICMP error even if DF is not set. */ |
| 902 | if (err == -EMSGSIZE) { |
| 903 | if (skb->protocol == htons(ETH_P_IP)) |
| 904 | icmp_send(skb, ICMP_DEST_UNREACH, |
| 905 | ICMP_FRAG_NEEDED, htonl(mtu)); |
| 906 | else |
| 907 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); |
| 908 | } |
| 909 | |
| 910 | goto tx_err; |
| 911 | } |
| 912 | return NETDEV_TX_OK; |
| 913 | |
| 914 | tx_err: |
| 915 | stats = &t->dev->stats; |
| 916 | stats->tx_errors++; |
| 917 | stats->tx_dropped++; |
| 918 | kfree_skb(skb); |
| 919 | return NETDEV_TX_OK; |
| 920 | } |
| 921 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 922 | static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu) |
| 923 | { |
| 924 | struct net_device *dev = t->dev; |
| 925 | struct __ip6_tnl_parm *p = &t->parms; |
| 926 | struct flowi6 *fl6 = &t->fl.u.ip6; |
Tom Herbert | db2ec95 | 2016-05-09 17:12:08 -0700 | [diff] [blame] | 927 | int t_hlen; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 928 | |
| 929 | if (dev->type != ARPHRD_ETHER) { |
| 930 | memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr)); |
| 931 | memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr)); |
| 932 | } |
| 933 | |
| 934 | /* Set up flowi template */ |
| 935 | fl6->saddr = p->laddr; |
| 936 | fl6->daddr = p->raddr; |
| 937 | fl6->flowi6_oif = p->link; |
| 938 | fl6->flowlabel = 0; |
Haishuang Yan | 252f3f5 | 2016-05-21 18:17:35 +0800 | [diff] [blame] | 939 | fl6->flowi6_proto = IPPROTO_GRE; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 940 | |
| 941 | if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS)) |
| 942 | fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo; |
| 943 | if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL)) |
| 944 | fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo; |
| 945 | |
| 946 | p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET); |
| 947 | p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr); |
| 948 | |
| 949 | if (p->flags&IP6_TNL_F_CAP_XMIT && |
| 950 | p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER) |
| 951 | dev->flags |= IFF_POINTOPOINT; |
| 952 | else |
| 953 | dev->flags &= ~IFF_POINTOPOINT; |
| 954 | |
Tom Herbert | db2ec95 | 2016-05-09 17:12:08 -0700 | [diff] [blame] | 955 | t->tun_hlen = gre_calc_hlen(t->parms.o_flags); |
| 956 | |
Tom Herbert | 1faf3d9 | 2016-05-18 09:06:19 -0700 | [diff] [blame] | 957 | t->hlen = t->encap_hlen + t->tun_hlen; |
Tom Herbert | db2ec95 | 2016-05-09 17:12:08 -0700 | [diff] [blame] | 958 | |
| 959 | t_hlen = t->hlen + sizeof(struct ipv6hdr); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 960 | |
| 961 | if (p->flags & IP6_TNL_F_CAP_XMIT) { |
| 962 | int strict = (ipv6_addr_type(&p->raddr) & |
| 963 | (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)); |
| 964 | |
Nicolas Dichtel | 22f0806 | 2014-04-22 10:15:24 +0200 | [diff] [blame] | 965 | struct rt6_info *rt = rt6_lookup(t->net, |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 966 | &p->raddr, &p->laddr, |
| 967 | p->link, strict); |
| 968 | |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 969 | if (!rt) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 970 | return; |
| 971 | |
| 972 | if (rt->dst.dev) { |
Tom Herbert | db2ec95 | 2016-05-09 17:12:08 -0700 | [diff] [blame] | 973 | dev->hard_header_len = rt->dst.dev->hard_header_len + |
| 974 | t_hlen; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 975 | |
| 976 | if (set_mtu) { |
Tom Herbert | db2ec95 | 2016-05-09 17:12:08 -0700 | [diff] [blame] | 977 | dev->mtu = rt->dst.dev->mtu - t_hlen; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 978 | if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) |
| 979 | dev->mtu -= 8; |
Alexander Duyck | a9e242c | 2016-04-14 15:33:45 -0400 | [diff] [blame] | 980 | if (dev->type == ARPHRD_ETHER) |
| 981 | dev->mtu -= ETH_HLEN; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 982 | |
| 983 | if (dev->mtu < IPV6_MIN_MTU) |
| 984 | dev->mtu = IPV6_MIN_MTU; |
| 985 | } |
| 986 | } |
Amerigo Wang | 94e187c | 2012-10-29 00:13:19 +0000 | [diff] [blame] | 987 | ip6_rt_put(rt); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 988 | } |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | static int ip6gre_tnl_change(struct ip6_tnl *t, |
| 992 | const struct __ip6_tnl_parm *p, int set_mtu) |
| 993 | { |
| 994 | t->parms.laddr = p->laddr; |
| 995 | t->parms.raddr = p->raddr; |
| 996 | t->parms.flags = p->flags; |
| 997 | t->parms.hop_limit = p->hop_limit; |
| 998 | t->parms.encap_limit = p->encap_limit; |
| 999 | t->parms.flowinfo = p->flowinfo; |
| 1000 | t->parms.link = p->link; |
| 1001 | t->parms.proto = p->proto; |
| 1002 | t->parms.i_key = p->i_key; |
| 1003 | t->parms.o_key = p->o_key; |
| 1004 | t->parms.i_flags = p->i_flags; |
| 1005 | t->parms.o_flags = p->o_flags; |
Craig Gallek | 0a473b8 | 2017-04-19 12:30:53 -0400 | [diff] [blame] | 1006 | t->parms.fwmark = p->fwmark; |
Paolo Abeni | 607f725 | 2016-02-12 15:43:54 +0100 | [diff] [blame] | 1007 | dst_cache_reset(&t->dst_cache); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1008 | ip6gre_tnl_link_config(t, set_mtu); |
| 1009 | return 0; |
| 1010 | } |
| 1011 | |
| 1012 | static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p, |
| 1013 | const struct ip6_tnl_parm2 *u) |
| 1014 | { |
| 1015 | p->laddr = u->laddr; |
| 1016 | p->raddr = u->raddr; |
| 1017 | p->flags = u->flags; |
| 1018 | p->hop_limit = u->hop_limit; |
| 1019 | p->encap_limit = u->encap_limit; |
| 1020 | p->flowinfo = u->flowinfo; |
| 1021 | p->link = u->link; |
| 1022 | p->i_key = u->i_key; |
| 1023 | p->o_key = u->o_key; |
Tom Herbert | f41fe3c | 2016-05-09 17:12:09 -0700 | [diff] [blame] | 1024 | p->i_flags = gre_flags_to_tnl_flags(u->i_flags); |
| 1025 | p->o_flags = gre_flags_to_tnl_flags(u->o_flags); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1026 | memcpy(p->name, u->name, sizeof(u->name)); |
| 1027 | } |
| 1028 | |
| 1029 | static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u, |
| 1030 | const struct __ip6_tnl_parm *p) |
| 1031 | { |
| 1032 | u->proto = IPPROTO_GRE; |
| 1033 | u->laddr = p->laddr; |
| 1034 | u->raddr = p->raddr; |
| 1035 | u->flags = p->flags; |
| 1036 | u->hop_limit = p->hop_limit; |
| 1037 | u->encap_limit = p->encap_limit; |
| 1038 | u->flowinfo = p->flowinfo; |
| 1039 | u->link = p->link; |
| 1040 | u->i_key = p->i_key; |
| 1041 | u->o_key = p->o_key; |
Tom Herbert | f41fe3c | 2016-05-09 17:12:09 -0700 | [diff] [blame] | 1042 | u->i_flags = gre_tnl_flags_to_gre_flags(p->i_flags); |
| 1043 | 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] | 1044 | memcpy(u->name, p->name, sizeof(u->name)); |
| 1045 | } |
| 1046 | |
| 1047 | static int ip6gre_tunnel_ioctl(struct net_device *dev, |
| 1048 | struct ifreq *ifr, int cmd) |
| 1049 | { |
| 1050 | int err = 0; |
| 1051 | struct ip6_tnl_parm2 p; |
| 1052 | struct __ip6_tnl_parm p1; |
Nicolas Dichtel | 22f0806 | 2014-04-22 10:15:24 +0200 | [diff] [blame] | 1053 | struct ip6_tnl *t = netdev_priv(dev); |
| 1054 | struct net *net = t->net; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1055 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); |
| 1056 | |
Tom Herbert | 308edfdf | 2016-04-29 17:12:17 -0700 | [diff] [blame] | 1057 | memset(&p1, 0, sizeof(p1)); |
| 1058 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1059 | switch (cmd) { |
| 1060 | case SIOCGETTUNNEL: |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1061 | if (dev == ign->fb_tunnel_dev) { |
| 1062 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) { |
| 1063 | err = -EFAULT; |
| 1064 | break; |
| 1065 | } |
| 1066 | ip6gre_tnl_parm_from_user(&p1, &p); |
| 1067 | t = ip6gre_tunnel_locate(net, &p1, 0); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 1068 | if (!t) |
Nicolas Dichtel | 22f0806 | 2014-04-22 10:15:24 +0200 | [diff] [blame] | 1069 | t = netdev_priv(dev); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1070 | } |
Amerigo Wang | 5dbd506 | 2013-05-09 21:56:37 +0000 | [diff] [blame] | 1071 | memset(&p, 0, sizeof(p)); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1072 | ip6gre_tnl_parm_to_user(&p, &t->parms); |
| 1073 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) |
| 1074 | err = -EFAULT; |
| 1075 | break; |
| 1076 | |
| 1077 | case SIOCADDTUNNEL: |
| 1078 | case SIOCCHGTUNNEL: |
| 1079 | err = -EPERM; |
Eric W. Biederman | af31f41 | 2012-11-16 03:03:06 +0000 | [diff] [blame] | 1080 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1081 | goto done; |
| 1082 | |
| 1083 | err = -EFAULT; |
| 1084 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) |
| 1085 | goto done; |
| 1086 | |
| 1087 | err = -EINVAL; |
| 1088 | if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING)) |
| 1089 | goto done; |
| 1090 | |
| 1091 | if (!(p.i_flags&GRE_KEY)) |
| 1092 | p.i_key = 0; |
| 1093 | if (!(p.o_flags&GRE_KEY)) |
| 1094 | p.o_key = 0; |
| 1095 | |
| 1096 | ip6gre_tnl_parm_from_user(&p1, &p); |
| 1097 | t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL); |
| 1098 | |
| 1099 | if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) { |
Ian Morris | 53b24b8 | 2015-03-29 14:00:05 +0100 | [diff] [blame] | 1100 | if (t) { |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1101 | if (t->dev != dev) { |
| 1102 | err = -EEXIST; |
| 1103 | break; |
| 1104 | } |
| 1105 | } else { |
| 1106 | t = netdev_priv(dev); |
| 1107 | |
| 1108 | ip6gre_tunnel_unlink(ign, t); |
| 1109 | synchronize_net(); |
| 1110 | ip6gre_tnl_change(t, &p1, 1); |
| 1111 | ip6gre_tunnel_link(ign, t); |
| 1112 | netdev_state_change(dev); |
| 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | if (t) { |
| 1117 | err = 0; |
| 1118 | |
Amerigo Wang | 5dbd506 | 2013-05-09 21:56:37 +0000 | [diff] [blame] | 1119 | memset(&p, 0, sizeof(p)); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1120 | ip6gre_tnl_parm_to_user(&p, &t->parms); |
| 1121 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) |
| 1122 | err = -EFAULT; |
| 1123 | } else |
| 1124 | err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT); |
| 1125 | break; |
| 1126 | |
| 1127 | case SIOCDELTUNNEL: |
| 1128 | err = -EPERM; |
Eric W. Biederman | af31f41 | 2012-11-16 03:03:06 +0000 | [diff] [blame] | 1129 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1130 | goto done; |
| 1131 | |
| 1132 | if (dev == ign->fb_tunnel_dev) { |
| 1133 | err = -EFAULT; |
| 1134 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) |
| 1135 | goto done; |
| 1136 | err = -ENOENT; |
| 1137 | ip6gre_tnl_parm_from_user(&p1, &p); |
| 1138 | t = ip6gre_tunnel_locate(net, &p1, 0); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 1139 | if (!t) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1140 | goto done; |
| 1141 | err = -EPERM; |
| 1142 | if (t == netdev_priv(ign->fb_tunnel_dev)) |
| 1143 | goto done; |
| 1144 | dev = t->dev; |
| 1145 | } |
| 1146 | unregister_netdevice(dev); |
| 1147 | err = 0; |
| 1148 | break; |
| 1149 | |
| 1150 | default: |
| 1151 | err = -EINVAL; |
| 1152 | } |
| 1153 | |
| 1154 | done: |
| 1155 | return err; |
| 1156 | } |
| 1157 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1158 | static int ip6gre_header(struct sk_buff *skb, struct net_device *dev, |
Xin Long | 76cc0d3 | 2017-09-15 12:00:07 +0800 | [diff] [blame] | 1159 | unsigned short type, const void *daddr, |
| 1160 | const void *saddr, unsigned int len) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1161 | { |
| 1162 | struct ip6_tnl *t = netdev_priv(dev); |
Xin Long | 76cc0d3 | 2017-09-15 12:00:07 +0800 | [diff] [blame] | 1163 | struct ipv6hdr *ipv6h; |
| 1164 | __be16 *p; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1165 | |
Xin Long | 76cc0d3 | 2017-09-15 12:00:07 +0800 | [diff] [blame] | 1166 | ipv6h = skb_push(skb, t->hlen + sizeof(*ipv6h)); |
| 1167 | ip6_flow_hdr(ipv6h, 0, ip6_make_flowlabel(dev_net(dev), skb, |
| 1168 | t->fl.u.ip6.flowlabel, |
| 1169 | true, &t->fl.u.ip6)); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1170 | ipv6h->hop_limit = t->parms.hop_limit; |
| 1171 | ipv6h->nexthdr = NEXTHDR_GRE; |
| 1172 | ipv6h->saddr = t->parms.laddr; |
| 1173 | ipv6h->daddr = t->parms.raddr; |
| 1174 | |
Xin Long | 76cc0d3 | 2017-09-15 12:00:07 +0800 | [diff] [blame] | 1175 | p = (__be16 *)(ipv6h + 1); |
| 1176 | p[0] = t->parms.o_flags; |
| 1177 | p[1] = htons(type); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1178 | |
| 1179 | /* |
| 1180 | * Set the source hardware address. |
| 1181 | */ |
| 1182 | |
| 1183 | if (saddr) |
| 1184 | memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr)); |
| 1185 | if (daddr) |
| 1186 | memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr)); |
| 1187 | if (!ipv6_addr_any(&ipv6h->daddr)) |
| 1188 | return t->hlen; |
| 1189 | |
| 1190 | return -t->hlen; |
| 1191 | } |
| 1192 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1193 | static const struct header_ops ip6gre_header_ops = { |
| 1194 | .create = ip6gre_header, |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1195 | }; |
| 1196 | |
| 1197 | static const struct net_device_ops ip6gre_netdev_ops = { |
| 1198 | .ndo_init = ip6gre_tunnel_init, |
| 1199 | .ndo_uninit = ip6gre_tunnel_uninit, |
| 1200 | .ndo_start_xmit = ip6gre_tunnel_xmit, |
| 1201 | .ndo_do_ioctl = ip6gre_tunnel_ioctl, |
Tom Herbert | b05229f | 2016-04-29 17:12:21 -0700 | [diff] [blame] | 1202 | .ndo_change_mtu = ip6_tnl_change_mtu, |
Pravin B Shelar | f61dd38 | 2013-03-25 14:50:00 +0000 | [diff] [blame] | 1203 | .ndo_get_stats64 = ip_tunnel_get_stats64, |
Nicolas Dichtel | ecf2c06 | 2015-04-02 17:07:01 +0200 | [diff] [blame] | 1204 | .ndo_get_iflink = ip6_tnl_get_iflink, |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1205 | }; |
| 1206 | |
| 1207 | static void ip6gre_dev_free(struct net_device *dev) |
| 1208 | { |
Martin KaFai Lau | cdf3464 | 2015-09-15 14:30:07 -0700 | [diff] [blame] | 1209 | struct ip6_tnl *t = netdev_priv(dev); |
| 1210 | |
Paolo Abeni | 607f725 | 2016-02-12 15:43:54 +0100 | [diff] [blame] | 1211 | dst_cache_destroy(&t->dst_cache); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1212 | free_percpu(dev->tstats); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | static void ip6gre_tunnel_setup(struct net_device *dev) |
| 1216 | { |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1217 | dev->netdev_ops = &ip6gre_netdev_ops; |
David S. Miller | cf124db | 2017-05-08 12:52:56 -0400 | [diff] [blame] | 1218 | dev->needs_free_netdev = true; |
| 1219 | dev->priv_destructor = ip6gre_dev_free; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1220 | |
| 1221 | dev->type = ARPHRD_IP6GRE; |
Tom Herbert | b05229f | 2016-04-29 17:12:21 -0700 | [diff] [blame] | 1222 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1223 | dev->flags |= IFF_NOARP; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1224 | dev->addr_len = sizeof(struct in6_addr); |
Eric Dumazet | 0287587 | 2014-10-05 18:38:35 -0700 | [diff] [blame] | 1225 | netif_keep_dst(dev); |
Felix Jia | 45ce0fd | 2017-01-26 16:59:18 +1300 | [diff] [blame] | 1226 | /* This perm addr will be used as interface identifier by IPv6 */ |
| 1227 | dev->addr_assign_type = NET_ADDR_RANDOM; |
| 1228 | eth_random_addr(dev->perm_addr); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
Martin KaFai Lau | a3c119d | 2015-09-15 14:30:05 -0700 | [diff] [blame] | 1231 | static int ip6gre_tunnel_init_common(struct net_device *dev) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1232 | { |
| 1233 | struct ip6_tnl *tunnel; |
Martin KaFai Lau | cdf3464 | 2015-09-15 14:30:07 -0700 | [diff] [blame] | 1234 | int ret; |
Tom Herbert | b05229f | 2016-04-29 17:12:21 -0700 | [diff] [blame] | 1235 | int t_hlen; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1236 | |
| 1237 | tunnel = netdev_priv(dev); |
| 1238 | |
| 1239 | tunnel->dev = dev; |
Nicolas Dichtel | 0bd87628 | 2013-08-13 17:51:12 +0200 | [diff] [blame] | 1240 | tunnel->net = dev_net(dev); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1241 | strcpy(tunnel->parms.name, dev->name); |
| 1242 | |
Martin KaFai Lau | a3c119d | 2015-09-15 14:30:05 -0700 | [diff] [blame] | 1243 | dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); |
| 1244 | if (!dev->tstats) |
| 1245 | return -ENOMEM; |
| 1246 | |
Paolo Abeni | 607f725 | 2016-02-12 15:43:54 +0100 | [diff] [blame] | 1247 | ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL); |
Martin KaFai Lau | cdf3464 | 2015-09-15 14:30:07 -0700 | [diff] [blame] | 1248 | if (ret) { |
| 1249 | free_percpu(dev->tstats); |
| 1250 | dev->tstats = NULL; |
| 1251 | return ret; |
| 1252 | } |
| 1253 | |
Tom Herbert | b05229f | 2016-04-29 17:12:21 -0700 | [diff] [blame] | 1254 | tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags); |
Tom Herbert | 1faf3d9 | 2016-05-18 09:06:19 -0700 | [diff] [blame] | 1255 | tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen; |
Tom Herbert | b05229f | 2016-04-29 17:12:21 -0700 | [diff] [blame] | 1256 | t_hlen = tunnel->hlen + sizeof(struct ipv6hdr); |
| 1257 | |
Tom Herbert | db2ec95 | 2016-05-09 17:12:08 -0700 | [diff] [blame] | 1258 | dev->hard_header_len = LL_MAX_HEADER + t_hlen; |
| 1259 | dev->mtu = ETH_DATA_LEN - t_hlen; |
Haishuang Yan | 1b227e5 | 2016-05-21 18:17:34 +0800 | [diff] [blame] | 1260 | if (dev->type == ARPHRD_ETHER) |
| 1261 | dev->mtu -= ETH_HLEN; |
Tom Herbert | b05229f | 2016-04-29 17:12:21 -0700 | [diff] [blame] | 1262 | if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) |
| 1263 | dev->mtu -= 8; |
| 1264 | |
William Tu | 6712abc | 2017-12-01 15:26:08 -0800 | [diff] [blame^] | 1265 | if (tunnel->parms.collect_md) { |
| 1266 | dev->features |= NETIF_F_NETNS_LOCAL; |
| 1267 | netif_keep_dst(dev); |
| 1268 | } |
| 1269 | |
Martin KaFai Lau | a3c119d | 2015-09-15 14:30:05 -0700 | [diff] [blame] | 1270 | return 0; |
| 1271 | } |
| 1272 | |
| 1273 | static int ip6gre_tunnel_init(struct net_device *dev) |
| 1274 | { |
| 1275 | struct ip6_tnl *tunnel; |
| 1276 | int ret; |
| 1277 | |
| 1278 | ret = ip6gre_tunnel_init_common(dev); |
| 1279 | if (ret) |
| 1280 | return ret; |
| 1281 | |
| 1282 | tunnel = netdev_priv(dev); |
| 1283 | |
William Tu | 6712abc | 2017-12-01 15:26:08 -0800 | [diff] [blame^] | 1284 | if (tunnel->parms.collect_md) |
| 1285 | return 0; |
| 1286 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1287 | memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr)); |
| 1288 | memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr)); |
| 1289 | |
| 1290 | if (ipv6_addr_any(&tunnel->parms.raddr)) |
| 1291 | dev->header_ops = &ip6gre_header_ops; |
| 1292 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1293 | return 0; |
| 1294 | } |
| 1295 | |
| 1296 | static void ip6gre_fb_tunnel_init(struct net_device *dev) |
| 1297 | { |
| 1298 | struct ip6_tnl *tunnel = netdev_priv(dev); |
| 1299 | |
| 1300 | tunnel->dev = dev; |
Nicolas Dichtel | 0bd87628 | 2013-08-13 17:51:12 +0200 | [diff] [blame] | 1301 | tunnel->net = dev_net(dev); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1302 | strcpy(tunnel->parms.name, dev->name); |
| 1303 | |
| 1304 | tunnel->hlen = sizeof(struct ipv6hdr) + 4; |
| 1305 | |
| 1306 | dev_hold(dev); |
| 1307 | } |
| 1308 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1309 | static struct inet6_protocol ip6gre_protocol __read_mostly = { |
Tom Herbert | 308edfdf | 2016-04-29 17:12:17 -0700 | [diff] [blame] | 1310 | .handler = gre_rcv, |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1311 | .err_handler = ip6gre_err, |
| 1312 | .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL, |
| 1313 | }; |
| 1314 | |
Nicolas Dichtel | 22f0806 | 2014-04-22 10:15:24 +0200 | [diff] [blame] | 1315 | 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] | 1316 | { |
Nicolas Dichtel | 22f0806 | 2014-04-22 10:15:24 +0200 | [diff] [blame] | 1317 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); |
| 1318 | struct net_device *dev, *aux; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1319 | int prio; |
| 1320 | |
Nicolas Dichtel | 22f0806 | 2014-04-22 10:15:24 +0200 | [diff] [blame] | 1321 | for_each_netdev_safe(net, dev, aux) |
| 1322 | if (dev->rtnl_link_ops == &ip6gre_link_ops || |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 1323 | dev->rtnl_link_ops == &ip6gre_tap_ops || |
| 1324 | dev->rtnl_link_ops == &ip6erspan_tap_ops) |
Nicolas Dichtel | 22f0806 | 2014-04-22 10:15:24 +0200 | [diff] [blame] | 1325 | unregister_netdevice_queue(dev, head); |
| 1326 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1327 | for (prio = 0; prio < 4; prio++) { |
| 1328 | int h; |
Jiri Kosina | e87a8f2 | 2016-08-10 11:03:35 +0200 | [diff] [blame] | 1329 | for (h = 0; h < IP6_GRE_HASH_SIZE; h++) { |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1330 | struct ip6_tnl *t; |
| 1331 | |
| 1332 | t = rtnl_dereference(ign->tunnels[prio][h]); |
| 1333 | |
Ian Morris | 53b24b8 | 2015-03-29 14:00:05 +0100 | [diff] [blame] | 1334 | while (t) { |
Nicolas Dichtel | 22f0806 | 2014-04-22 10:15:24 +0200 | [diff] [blame] | 1335 | /* If dev is in the same netns, it has already |
| 1336 | * been added to the list by the previous loop. |
| 1337 | */ |
| 1338 | if (!net_eq(dev_net(t->dev), net)) |
| 1339 | unregister_netdevice_queue(t->dev, |
| 1340 | head); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1341 | t = rtnl_dereference(t->next); |
| 1342 | } |
| 1343 | } |
| 1344 | } |
| 1345 | } |
| 1346 | |
| 1347 | static int __net_init ip6gre_init_net(struct net *net) |
| 1348 | { |
| 1349 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); |
| 1350 | int err; |
| 1351 | |
| 1352 | ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0", |
Tom Gundersen | c835a67 | 2014-07-14 16:37:24 +0200 | [diff] [blame] | 1353 | NET_NAME_UNKNOWN, |
| 1354 | ip6gre_tunnel_setup); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1355 | if (!ign->fb_tunnel_dev) { |
| 1356 | err = -ENOMEM; |
| 1357 | goto err_alloc_dev; |
| 1358 | } |
| 1359 | dev_net_set(ign->fb_tunnel_dev, net); |
Nicolas Dichtel | 22f0806 | 2014-04-22 10:15:24 +0200 | [diff] [blame] | 1360 | /* FB netdevice is special: we have one, and only one per netns. |
| 1361 | * Allowing to move it to another netns is clearly unsafe. |
| 1362 | */ |
| 1363 | ign->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL; |
| 1364 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1365 | |
| 1366 | ip6gre_fb_tunnel_init(ign->fb_tunnel_dev); |
| 1367 | ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops; |
| 1368 | |
| 1369 | err = register_netdev(ign->fb_tunnel_dev); |
| 1370 | if (err) |
| 1371 | goto err_reg_dev; |
| 1372 | |
| 1373 | rcu_assign_pointer(ign->tunnels_wc[0], |
| 1374 | netdev_priv(ign->fb_tunnel_dev)); |
| 1375 | return 0; |
| 1376 | |
| 1377 | err_reg_dev: |
David S. Miller | cf124db | 2017-05-08 12:52:56 -0400 | [diff] [blame] | 1378 | free_netdev(ign->fb_tunnel_dev); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1379 | err_alloc_dev: |
| 1380 | return err; |
| 1381 | } |
| 1382 | |
Eric Dumazet | bb401ca | 2017-09-19 16:27:08 -0700 | [diff] [blame] | 1383 | 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] | 1384 | { |
Eric Dumazet | bb401ca | 2017-09-19 16:27:08 -0700 | [diff] [blame] | 1385 | struct net *net; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1386 | LIST_HEAD(list); |
| 1387 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1388 | rtnl_lock(); |
Eric Dumazet | bb401ca | 2017-09-19 16:27:08 -0700 | [diff] [blame] | 1389 | list_for_each_entry(net, net_list, exit_list) |
| 1390 | ip6gre_destroy_tunnels(net, &list); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1391 | unregister_netdevice_many(&list); |
| 1392 | rtnl_unlock(); |
| 1393 | } |
| 1394 | |
| 1395 | static struct pernet_operations ip6gre_net_ops = { |
| 1396 | .init = ip6gre_init_net, |
Eric Dumazet | bb401ca | 2017-09-19 16:27:08 -0700 | [diff] [blame] | 1397 | .exit_batch = ip6gre_exit_batch_net, |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1398 | .id = &ip6gre_net_id, |
| 1399 | .size = sizeof(struct ip6gre_net), |
| 1400 | }; |
| 1401 | |
Matthias Schiffer | a8b8a889 | 2017-06-25 23:56:01 +0200 | [diff] [blame] | 1402 | static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[], |
| 1403 | struct netlink_ext_ack *extack) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1404 | { |
| 1405 | __be16 flags; |
| 1406 | |
| 1407 | if (!data) |
| 1408 | return 0; |
| 1409 | |
| 1410 | flags = 0; |
| 1411 | if (data[IFLA_GRE_IFLAGS]) |
| 1412 | flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]); |
| 1413 | if (data[IFLA_GRE_OFLAGS]) |
| 1414 | flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]); |
| 1415 | if (flags & (GRE_VERSION|GRE_ROUTING)) |
| 1416 | return -EINVAL; |
| 1417 | |
| 1418 | return 0; |
| 1419 | } |
| 1420 | |
Matthias Schiffer | a8b8a889 | 2017-06-25 23:56:01 +0200 | [diff] [blame] | 1421 | static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[], |
| 1422 | struct netlink_ext_ack *extack) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1423 | { |
| 1424 | struct in6_addr daddr; |
| 1425 | |
| 1426 | if (tb[IFLA_ADDRESS]) { |
| 1427 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) |
| 1428 | return -EINVAL; |
| 1429 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) |
| 1430 | return -EADDRNOTAVAIL; |
| 1431 | } |
| 1432 | |
| 1433 | if (!data) |
| 1434 | goto out; |
| 1435 | |
| 1436 | if (data[IFLA_GRE_REMOTE]) { |
Jiri Benc | 67b61f6 | 2015-03-29 16:59:26 +0200 | [diff] [blame] | 1437 | daddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1438 | if (ipv6_addr_any(&daddr)) |
| 1439 | return -EINVAL; |
| 1440 | } |
| 1441 | |
| 1442 | out: |
Matthias Schiffer | a8b8a889 | 2017-06-25 23:56:01 +0200 | [diff] [blame] | 1443 | return ip6gre_tunnel_validate(tb, data, extack); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 1446 | static int ip6erspan_tap_validate(struct nlattr *tb[], struct nlattr *data[], |
| 1447 | struct netlink_ext_ack *extack) |
| 1448 | { |
| 1449 | __be16 flags = 0; |
| 1450 | int ret; |
| 1451 | |
| 1452 | if (!data) |
| 1453 | return 0; |
| 1454 | |
| 1455 | ret = ip6gre_tap_validate(tb, data, extack); |
| 1456 | if (ret) |
| 1457 | return ret; |
| 1458 | |
| 1459 | /* ERSPAN should only have GRE sequence and key flag */ |
| 1460 | if (data[IFLA_GRE_OFLAGS]) |
| 1461 | flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]); |
| 1462 | if (data[IFLA_GRE_IFLAGS]) |
| 1463 | flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]); |
| 1464 | if (!data[IFLA_GRE_COLLECT_METADATA] && |
| 1465 | flags != (GRE_SEQ | GRE_KEY)) |
| 1466 | return -EINVAL; |
| 1467 | |
| 1468 | /* ERSPAN Session ID only has 10-bit. Since we reuse |
| 1469 | * 32-bit key field as ID, check it's range. |
| 1470 | */ |
| 1471 | if (data[IFLA_GRE_IKEY] && |
| 1472 | (ntohl(nla_get_be32(data[IFLA_GRE_IKEY])) & ~ID_MASK)) |
| 1473 | return -EINVAL; |
| 1474 | |
| 1475 | if (data[IFLA_GRE_OKEY] && |
| 1476 | (ntohl(nla_get_be32(data[IFLA_GRE_OKEY])) & ~ID_MASK)) |
| 1477 | return -EINVAL; |
| 1478 | |
| 1479 | if (data[IFLA_GRE_ERSPAN_INDEX]) { |
| 1480 | u32 index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]); |
| 1481 | |
| 1482 | if (index & ~INDEX_MASK) |
| 1483 | return -EINVAL; |
| 1484 | } |
| 1485 | return 0; |
| 1486 | } |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1487 | |
| 1488 | static void ip6gre_netlink_parms(struct nlattr *data[], |
| 1489 | struct __ip6_tnl_parm *parms) |
| 1490 | { |
| 1491 | memset(parms, 0, sizeof(*parms)); |
| 1492 | |
| 1493 | if (!data) |
| 1494 | return; |
| 1495 | |
| 1496 | if (data[IFLA_GRE_LINK]) |
| 1497 | parms->link = nla_get_u32(data[IFLA_GRE_LINK]); |
| 1498 | |
| 1499 | if (data[IFLA_GRE_IFLAGS]) |
Tom Herbert | f41fe3c | 2016-05-09 17:12:09 -0700 | [diff] [blame] | 1500 | parms->i_flags = gre_flags_to_tnl_flags( |
| 1501 | nla_get_be16(data[IFLA_GRE_IFLAGS])); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1502 | |
| 1503 | if (data[IFLA_GRE_OFLAGS]) |
Tom Herbert | f41fe3c | 2016-05-09 17:12:09 -0700 | [diff] [blame] | 1504 | parms->o_flags = gre_flags_to_tnl_flags( |
| 1505 | nla_get_be16(data[IFLA_GRE_OFLAGS])); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1506 | |
| 1507 | if (data[IFLA_GRE_IKEY]) |
| 1508 | parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]); |
| 1509 | |
| 1510 | if (data[IFLA_GRE_OKEY]) |
| 1511 | parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]); |
| 1512 | |
| 1513 | if (data[IFLA_GRE_LOCAL]) |
Jiri Benc | 67b61f6 | 2015-03-29 16:59:26 +0200 | [diff] [blame] | 1514 | parms->laddr = nla_get_in6_addr(data[IFLA_GRE_LOCAL]); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1515 | |
| 1516 | if (data[IFLA_GRE_REMOTE]) |
Jiri Benc | 67b61f6 | 2015-03-29 16:59:26 +0200 | [diff] [blame] | 1517 | parms->raddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1518 | |
| 1519 | if (data[IFLA_GRE_TTL]) |
| 1520 | parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]); |
| 1521 | |
| 1522 | if (data[IFLA_GRE_ENCAP_LIMIT]) |
| 1523 | parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]); |
| 1524 | |
| 1525 | if (data[IFLA_GRE_FLOWINFO]) |
Lance Richardson | c2675de | 2016-09-24 14:01:04 -0400 | [diff] [blame] | 1526 | parms->flowinfo = nla_get_be32(data[IFLA_GRE_FLOWINFO]); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1527 | |
| 1528 | if (data[IFLA_GRE_FLAGS]) |
| 1529 | parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]); |
Craig Gallek | 0a473b8 | 2017-04-19 12:30:53 -0400 | [diff] [blame] | 1530 | |
| 1531 | if (data[IFLA_GRE_FWMARK]) |
| 1532 | parms->fwmark = nla_get_u32(data[IFLA_GRE_FWMARK]); |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 1533 | |
| 1534 | if (data[IFLA_GRE_ERSPAN_INDEX]) |
| 1535 | parms->index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]); |
William Tu | 6712abc | 2017-12-01 15:26:08 -0800 | [diff] [blame^] | 1536 | |
| 1537 | if (data[IFLA_GRE_COLLECT_METADATA]) |
| 1538 | parms->collect_md = true; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
| 1541 | static int ip6gre_tap_init(struct net_device *dev) |
| 1542 | { |
| 1543 | struct ip6_tnl *tunnel; |
Martin KaFai Lau | a3c119d | 2015-09-15 14:30:05 -0700 | [diff] [blame] | 1544 | int ret; |
| 1545 | |
| 1546 | ret = ip6gre_tunnel_init_common(dev); |
| 1547 | if (ret) |
| 1548 | return ret; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1549 | |
Shweta Choudaha | 0a46baa | 2016-06-08 20:15:43 +0100 | [diff] [blame] | 1550 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; |
| 1551 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1552 | tunnel = netdev_priv(dev); |
| 1553 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1554 | ip6gre_tnl_link_config(tunnel, 1); |
| 1555 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1556 | return 0; |
| 1557 | } |
| 1558 | |
| 1559 | static const struct net_device_ops ip6gre_tap_netdev_ops = { |
| 1560 | .ndo_init = ip6gre_tap_init, |
| 1561 | .ndo_uninit = ip6gre_tunnel_uninit, |
| 1562 | .ndo_start_xmit = ip6gre_tunnel_xmit, |
| 1563 | .ndo_set_mac_address = eth_mac_addr, |
| 1564 | .ndo_validate_addr = eth_validate_addr, |
Tom Herbert | b05229f | 2016-04-29 17:12:21 -0700 | [diff] [blame] | 1565 | .ndo_change_mtu = ip6_tnl_change_mtu, |
Pravin B Shelar | f61dd38 | 2013-03-25 14:50:00 +0000 | [diff] [blame] | 1566 | .ndo_get_stats64 = ip_tunnel_get_stats64, |
Nicolas Dichtel | ecf2c06 | 2015-04-02 17:07:01 +0200 | [diff] [blame] | 1567 | .ndo_get_iflink = ip6_tnl_get_iflink, |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1568 | }; |
| 1569 | |
Alexander Duyck | ac4eb00 | 2016-04-14 15:33:51 -0400 | [diff] [blame] | 1570 | #define GRE6_FEATURES (NETIF_F_SG | \ |
| 1571 | NETIF_F_FRAGLIST | \ |
| 1572 | NETIF_F_HIGHDMA | \ |
| 1573 | NETIF_F_HW_CSUM) |
| 1574 | |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 1575 | static int ip6erspan_tap_init(struct net_device *dev) |
| 1576 | { |
| 1577 | struct ip6_tnl *tunnel; |
| 1578 | int t_hlen; |
| 1579 | int ret; |
| 1580 | |
| 1581 | tunnel = netdev_priv(dev); |
| 1582 | |
| 1583 | tunnel->dev = dev; |
| 1584 | tunnel->net = dev_net(dev); |
| 1585 | strcpy(tunnel->parms.name, dev->name); |
| 1586 | |
| 1587 | dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); |
| 1588 | if (!dev->tstats) |
| 1589 | return -ENOMEM; |
| 1590 | |
| 1591 | ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL); |
| 1592 | if (ret) { |
| 1593 | free_percpu(dev->tstats); |
| 1594 | dev->tstats = NULL; |
| 1595 | return ret; |
| 1596 | } |
| 1597 | |
| 1598 | tunnel->tun_hlen = 8; |
| 1599 | tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen + |
| 1600 | sizeof(struct erspanhdr); |
| 1601 | t_hlen = tunnel->hlen + sizeof(struct ipv6hdr); |
| 1602 | |
| 1603 | dev->hard_header_len = LL_MAX_HEADER + t_hlen; |
| 1604 | dev->mtu = ETH_DATA_LEN - t_hlen; |
| 1605 | if (dev->type == ARPHRD_ETHER) |
| 1606 | dev->mtu -= ETH_HLEN; |
| 1607 | if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) |
| 1608 | dev->mtu -= 8; |
| 1609 | |
| 1610 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; |
| 1611 | tunnel = netdev_priv(dev); |
| 1612 | ip6gre_tnl_link_config(tunnel, 1); |
| 1613 | |
| 1614 | return 0; |
| 1615 | } |
| 1616 | |
| 1617 | static const struct net_device_ops ip6erspan_netdev_ops = { |
| 1618 | .ndo_init = ip6erspan_tap_init, |
| 1619 | .ndo_uninit = ip6gre_tunnel_uninit, |
| 1620 | .ndo_start_xmit = ip6erspan_tunnel_xmit, |
| 1621 | .ndo_set_mac_address = eth_mac_addr, |
| 1622 | .ndo_validate_addr = eth_validate_addr, |
| 1623 | .ndo_change_mtu = ip6_tnl_change_mtu, |
| 1624 | .ndo_get_stats64 = ip_tunnel_get_stats64, |
| 1625 | .ndo_get_iflink = ip6_tnl_get_iflink, |
| 1626 | }; |
| 1627 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1628 | static void ip6gre_tap_setup(struct net_device *dev) |
| 1629 | { |
| 1630 | |
| 1631 | ether_setup(dev); |
| 1632 | |
| 1633 | dev->netdev_ops = &ip6gre_tap_netdev_ops; |
David S. Miller | cf124db | 2017-05-08 12:52:56 -0400 | [diff] [blame] | 1634 | dev->needs_free_netdev = true; |
| 1635 | dev->priv_destructor = ip6gre_dev_free; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1636 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1637 | dev->features |= NETIF_F_NETNS_LOCAL; |
Jiri Benc | d13b161 | 2016-02-17 15:32:53 +0100 | [diff] [blame] | 1638 | dev->priv_flags &= ~IFF_TX_SKB_SHARING; |
Shweta Choudaha | 0a46baa | 2016-06-08 20:15:43 +0100 | [diff] [blame] | 1639 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; |
Xin Long | 2d40557 | 2017-09-28 13:23:50 +0800 | [diff] [blame] | 1640 | netif_keep_dst(dev); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1641 | } |
| 1642 | |
Tom Herbert | 1faf3d9 | 2016-05-18 09:06:19 -0700 | [diff] [blame] | 1643 | static bool ip6gre_netlink_encap_parms(struct nlattr *data[], |
| 1644 | struct ip_tunnel_encap *ipencap) |
| 1645 | { |
| 1646 | bool ret = false; |
| 1647 | |
| 1648 | memset(ipencap, 0, sizeof(*ipencap)); |
| 1649 | |
| 1650 | if (!data) |
| 1651 | return ret; |
| 1652 | |
| 1653 | if (data[IFLA_GRE_ENCAP_TYPE]) { |
| 1654 | ret = true; |
| 1655 | ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]); |
| 1656 | } |
| 1657 | |
| 1658 | if (data[IFLA_GRE_ENCAP_FLAGS]) { |
| 1659 | ret = true; |
| 1660 | ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]); |
| 1661 | } |
| 1662 | |
| 1663 | if (data[IFLA_GRE_ENCAP_SPORT]) { |
| 1664 | ret = true; |
| 1665 | ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]); |
| 1666 | } |
| 1667 | |
| 1668 | if (data[IFLA_GRE_ENCAP_DPORT]) { |
| 1669 | ret = true; |
| 1670 | ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]); |
| 1671 | } |
| 1672 | |
| 1673 | return ret; |
| 1674 | } |
| 1675 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1676 | static int ip6gre_newlink(struct net *src_net, struct net_device *dev, |
Matthias Schiffer | 7a3f4a1 | 2017-06-25 23:55:59 +0200 | [diff] [blame] | 1677 | struct nlattr *tb[], struct nlattr *data[], |
| 1678 | struct netlink_ext_ack *extack) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1679 | { |
| 1680 | struct ip6_tnl *nt; |
| 1681 | struct net *net = dev_net(dev); |
| 1682 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); |
Tom Herbert | 1faf3d9 | 2016-05-18 09:06:19 -0700 | [diff] [blame] | 1683 | struct ip_tunnel_encap ipencap; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1684 | int err; |
| 1685 | |
| 1686 | nt = netdev_priv(dev); |
Tom Herbert | 1faf3d9 | 2016-05-18 09:06:19 -0700 | [diff] [blame] | 1687 | |
| 1688 | if (ip6gre_netlink_encap_parms(data, &ipencap)) { |
| 1689 | int err = ip6_tnl_encap_setup(nt, &ipencap); |
| 1690 | |
| 1691 | if (err < 0) |
| 1692 | return err; |
| 1693 | } |
| 1694 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1695 | ip6gre_netlink_parms(data, &nt->parms); |
| 1696 | |
William Tu | 6712abc | 2017-12-01 15:26:08 -0800 | [diff] [blame^] | 1697 | if (nt->parms.collect_md) { |
| 1698 | if (rtnl_dereference(ign->collect_md_tun)) |
| 1699 | return -EEXIST; |
| 1700 | } else { |
| 1701 | if (ip6gre_tunnel_find(net, &nt->parms, dev->type)) |
| 1702 | return -EEXIST; |
| 1703 | } |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1704 | |
| 1705 | if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS]) |
| 1706 | eth_hw_addr_random(dev); |
| 1707 | |
| 1708 | nt->dev = dev; |
Nicolas Dichtel | 0bd87628 | 2013-08-13 17:51:12 +0200 | [diff] [blame] | 1709 | nt->net = dev_net(dev); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1710 | ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]); |
| 1711 | |
Alexander Duyck | ac4eb00 | 2016-04-14 15:33:51 -0400 | [diff] [blame] | 1712 | dev->features |= GRE6_FEATURES; |
| 1713 | dev->hw_features |= GRE6_FEATURES; |
| 1714 | |
Tom Herbert | b45bd1d | 2016-05-09 17:12:12 -0700 | [diff] [blame] | 1715 | if (!(nt->parms.o_flags & TUNNEL_SEQ)) { |
Alexander Duyck | 6a55368 | 2016-05-18 10:44:47 -0700 | [diff] [blame] | 1716 | /* TCP offload with GRE SEQ is not supported, nor |
| 1717 | * can we support 2 levels of outer headers requiring |
| 1718 | * an update. |
Alexander Duyck | 3a80e1f | 2016-04-14 15:34:04 -0400 | [diff] [blame] | 1719 | */ |
Alexander Duyck | 6a55368 | 2016-05-18 10:44:47 -0700 | [diff] [blame] | 1720 | if (!(nt->parms.o_flags & TUNNEL_CSUM) || |
| 1721 | (nt->encap.type == TUNNEL_ENCAP_NONE)) { |
| 1722 | dev->features |= NETIF_F_GSO_SOFTWARE; |
| 1723 | dev->hw_features |= NETIF_F_GSO_SOFTWARE; |
| 1724 | } |
Alexander Duyck | 3a80e1f | 2016-04-14 15:34:04 -0400 | [diff] [blame] | 1725 | |
| 1726 | /* Can use a lockless transmit, unless we generate |
| 1727 | * output sequences |
| 1728 | */ |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1729 | dev->features |= NETIF_F_LLTX; |
Alexander Duyck | 3a80e1f | 2016-04-14 15:34:04 -0400 | [diff] [blame] | 1730 | } |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1731 | |
| 1732 | err = register_netdevice(dev); |
| 1733 | if (err) |
| 1734 | goto out; |
| 1735 | |
| 1736 | dev_hold(dev); |
| 1737 | ip6gre_tunnel_link(ign, nt); |
| 1738 | |
| 1739 | out: |
| 1740 | return err; |
| 1741 | } |
| 1742 | |
| 1743 | static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[], |
Matthias Schiffer | ad744b2 | 2017-06-25 23:56:00 +0200 | [diff] [blame] | 1744 | struct nlattr *data[], |
| 1745 | struct netlink_ext_ack *extack) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1746 | { |
Nicolas Dichtel | 22f0806 | 2014-04-22 10:15:24 +0200 | [diff] [blame] | 1747 | struct ip6_tnl *t, *nt = netdev_priv(dev); |
| 1748 | struct net *net = nt->net; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1749 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); |
| 1750 | struct __ip6_tnl_parm p; |
Tom Herbert | 1faf3d9 | 2016-05-18 09:06:19 -0700 | [diff] [blame] | 1751 | struct ip_tunnel_encap ipencap; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1752 | |
| 1753 | if (dev == ign->fb_tunnel_dev) |
| 1754 | return -EINVAL; |
| 1755 | |
Tom Herbert | 1faf3d9 | 2016-05-18 09:06:19 -0700 | [diff] [blame] | 1756 | if (ip6gre_netlink_encap_parms(data, &ipencap)) { |
| 1757 | int err = ip6_tnl_encap_setup(nt, &ipencap); |
| 1758 | |
| 1759 | if (err < 0) |
| 1760 | return err; |
| 1761 | } |
| 1762 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1763 | ip6gre_netlink_parms(data, &p); |
| 1764 | |
| 1765 | t = ip6gre_tunnel_locate(net, &p, 0); |
| 1766 | |
| 1767 | if (t) { |
| 1768 | if (t->dev != dev) |
| 1769 | return -EEXIST; |
| 1770 | } else { |
| 1771 | t = nt; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
Nicolas Dichtel | 6a61d4d | 2015-12-03 17:21:50 +0100 | [diff] [blame] | 1774 | ip6gre_tunnel_unlink(ign, t); |
| 1775 | ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]); |
| 1776 | ip6gre_tunnel_link(ign, t); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1777 | return 0; |
| 1778 | } |
| 1779 | |
Nicolas Dichtel | 54d63f78 | 2014-04-14 17:11:38 +0200 | [diff] [blame] | 1780 | static void ip6gre_dellink(struct net_device *dev, struct list_head *head) |
| 1781 | { |
| 1782 | struct net *net = dev_net(dev); |
| 1783 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); |
| 1784 | |
| 1785 | if (dev != ign->fb_tunnel_dev) |
| 1786 | unregister_netdevice_queue(dev, head); |
| 1787 | } |
| 1788 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1789 | static size_t ip6gre_get_size(const struct net_device *dev) |
| 1790 | { |
| 1791 | return |
| 1792 | /* IFLA_GRE_LINK */ |
| 1793 | nla_total_size(4) + |
| 1794 | /* IFLA_GRE_IFLAGS */ |
| 1795 | nla_total_size(2) + |
| 1796 | /* IFLA_GRE_OFLAGS */ |
| 1797 | nla_total_size(2) + |
| 1798 | /* IFLA_GRE_IKEY */ |
| 1799 | nla_total_size(4) + |
| 1800 | /* IFLA_GRE_OKEY */ |
| 1801 | nla_total_size(4) + |
| 1802 | /* IFLA_GRE_LOCAL */ |
Nicolas Dichtel | a375413 | 2012-11-09 05:34:56 +0000 | [diff] [blame] | 1803 | nla_total_size(sizeof(struct in6_addr)) + |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1804 | /* IFLA_GRE_REMOTE */ |
Nicolas Dichtel | a375413 | 2012-11-09 05:34:56 +0000 | [diff] [blame] | 1805 | nla_total_size(sizeof(struct in6_addr)) + |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1806 | /* IFLA_GRE_TTL */ |
| 1807 | nla_total_size(1) + |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1808 | /* IFLA_GRE_ENCAP_LIMIT */ |
| 1809 | nla_total_size(1) + |
| 1810 | /* IFLA_GRE_FLOWINFO */ |
| 1811 | nla_total_size(4) + |
| 1812 | /* IFLA_GRE_FLAGS */ |
| 1813 | nla_total_size(4) + |
Tom Herbert | 1faf3d9 | 2016-05-18 09:06:19 -0700 | [diff] [blame] | 1814 | /* IFLA_GRE_ENCAP_TYPE */ |
| 1815 | nla_total_size(2) + |
| 1816 | /* IFLA_GRE_ENCAP_FLAGS */ |
| 1817 | nla_total_size(2) + |
| 1818 | /* IFLA_GRE_ENCAP_SPORT */ |
| 1819 | nla_total_size(2) + |
| 1820 | /* IFLA_GRE_ENCAP_DPORT */ |
| 1821 | nla_total_size(2) + |
William Tu | 6712abc | 2017-12-01 15:26:08 -0800 | [diff] [blame^] | 1822 | /* IFLA_GRE_COLLECT_METADATA */ |
| 1823 | nla_total_size(0) + |
Craig Gallek | 0a473b8 | 2017-04-19 12:30:53 -0400 | [diff] [blame] | 1824 | /* IFLA_GRE_FWMARK */ |
| 1825 | nla_total_size(4) + |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 1826 | /* IFLA_GRE_ERSPAN_INDEX */ |
| 1827 | nla_total_size(4) + |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1828 | 0; |
| 1829 | } |
| 1830 | |
| 1831 | static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev) |
| 1832 | { |
| 1833 | struct ip6_tnl *t = netdev_priv(dev); |
| 1834 | struct __ip6_tnl_parm *p = &t->parms; |
| 1835 | |
| 1836 | if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) || |
Tom Herbert | f41fe3c | 2016-05-09 17:12:09 -0700 | [diff] [blame] | 1837 | nla_put_be16(skb, IFLA_GRE_IFLAGS, |
| 1838 | gre_tnl_flags_to_gre_flags(p->i_flags)) || |
| 1839 | nla_put_be16(skb, IFLA_GRE_OFLAGS, |
| 1840 | gre_tnl_flags_to_gre_flags(p->o_flags)) || |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1841 | nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) || |
| 1842 | nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) || |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 1843 | nla_put_in6_addr(skb, IFLA_GRE_LOCAL, &p->laddr) || |
| 1844 | nla_put_in6_addr(skb, IFLA_GRE_REMOTE, &p->raddr) || |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1845 | nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) || |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1846 | nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) || |
| 1847 | nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) || |
Craig Gallek | 0a473b8 | 2017-04-19 12:30:53 -0400 | [diff] [blame] | 1848 | nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags) || |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 1849 | nla_put_u32(skb, IFLA_GRE_FWMARK, p->fwmark) || |
| 1850 | nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, p->index)) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1851 | goto nla_put_failure; |
Tom Herbert | 1faf3d9 | 2016-05-18 09:06:19 -0700 | [diff] [blame] | 1852 | |
| 1853 | if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE, |
| 1854 | t->encap.type) || |
| 1855 | nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT, |
| 1856 | t->encap.sport) || |
| 1857 | nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT, |
| 1858 | t->encap.dport) || |
| 1859 | nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS, |
| 1860 | t->encap.flags)) |
| 1861 | goto nla_put_failure; |
| 1862 | |
William Tu | 6712abc | 2017-12-01 15:26:08 -0800 | [diff] [blame^] | 1863 | if (p->collect_md) { |
| 1864 | if (nla_put_flag(skb, IFLA_GRE_COLLECT_METADATA)) |
| 1865 | goto nla_put_failure; |
| 1866 | } |
| 1867 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1868 | return 0; |
| 1869 | |
| 1870 | nla_put_failure: |
| 1871 | return -EMSGSIZE; |
| 1872 | } |
| 1873 | |
| 1874 | static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = { |
| 1875 | [IFLA_GRE_LINK] = { .type = NLA_U32 }, |
| 1876 | [IFLA_GRE_IFLAGS] = { .type = NLA_U16 }, |
| 1877 | [IFLA_GRE_OFLAGS] = { .type = NLA_U16 }, |
| 1878 | [IFLA_GRE_IKEY] = { .type = NLA_U32 }, |
| 1879 | [IFLA_GRE_OKEY] = { .type = NLA_U32 }, |
| 1880 | [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct ipv6hdr, saddr) }, |
| 1881 | [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct ipv6hdr, daddr) }, |
| 1882 | [IFLA_GRE_TTL] = { .type = NLA_U8 }, |
| 1883 | [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 }, |
| 1884 | [IFLA_GRE_FLOWINFO] = { .type = NLA_U32 }, |
| 1885 | [IFLA_GRE_FLAGS] = { .type = NLA_U32 }, |
Tom Herbert | 1faf3d9 | 2016-05-18 09:06:19 -0700 | [diff] [blame] | 1886 | [IFLA_GRE_ENCAP_TYPE] = { .type = NLA_U16 }, |
| 1887 | [IFLA_GRE_ENCAP_FLAGS] = { .type = NLA_U16 }, |
| 1888 | [IFLA_GRE_ENCAP_SPORT] = { .type = NLA_U16 }, |
| 1889 | [IFLA_GRE_ENCAP_DPORT] = { .type = NLA_U16 }, |
William Tu | 6712abc | 2017-12-01 15:26:08 -0800 | [diff] [blame^] | 1890 | [IFLA_GRE_COLLECT_METADATA] = { .type = NLA_FLAG }, |
Craig Gallek | 0a473b8 | 2017-04-19 12:30:53 -0400 | [diff] [blame] | 1891 | [IFLA_GRE_FWMARK] = { .type = NLA_U32 }, |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 1892 | [IFLA_GRE_ERSPAN_INDEX] = { .type = NLA_U32 }, |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1893 | }; |
| 1894 | |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 1895 | static void ip6erspan_tap_setup(struct net_device *dev) |
| 1896 | { |
| 1897 | ether_setup(dev); |
| 1898 | |
| 1899 | dev->netdev_ops = &ip6erspan_netdev_ops; |
| 1900 | dev->needs_free_netdev = true; |
| 1901 | dev->priv_destructor = ip6gre_dev_free; |
| 1902 | |
| 1903 | dev->features |= NETIF_F_NETNS_LOCAL; |
| 1904 | dev->priv_flags &= ~IFF_TX_SKB_SHARING; |
| 1905 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; |
| 1906 | netif_keep_dst(dev); |
| 1907 | } |
| 1908 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1909 | static struct rtnl_link_ops ip6gre_link_ops __read_mostly = { |
| 1910 | .kind = "ip6gre", |
| 1911 | .maxtype = IFLA_GRE_MAX, |
| 1912 | .policy = ip6gre_policy, |
| 1913 | .priv_size = sizeof(struct ip6_tnl), |
| 1914 | .setup = ip6gre_tunnel_setup, |
| 1915 | .validate = ip6gre_tunnel_validate, |
| 1916 | .newlink = ip6gre_newlink, |
| 1917 | .changelink = ip6gre_changelink, |
Nicolas Dichtel | 54d63f78 | 2014-04-14 17:11:38 +0200 | [diff] [blame] | 1918 | .dellink = ip6gre_dellink, |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1919 | .get_size = ip6gre_get_size, |
| 1920 | .fill_info = ip6gre_fill_info, |
Nicolas Dichtel | 1728d4f | 2015-01-15 15:11:17 +0100 | [diff] [blame] | 1921 | .get_link_net = ip6_tnl_get_link_net, |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1922 | }; |
| 1923 | |
| 1924 | static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = { |
| 1925 | .kind = "ip6gretap", |
| 1926 | .maxtype = IFLA_GRE_MAX, |
| 1927 | .policy = ip6gre_policy, |
| 1928 | .priv_size = sizeof(struct ip6_tnl), |
| 1929 | .setup = ip6gre_tap_setup, |
| 1930 | .validate = ip6gre_tap_validate, |
| 1931 | .newlink = ip6gre_newlink, |
| 1932 | .changelink = ip6gre_changelink, |
| 1933 | .get_size = ip6gre_get_size, |
| 1934 | .fill_info = ip6gre_fill_info, |
Nicolas Dichtel | 3390e39 | 2015-01-20 15:15:43 +0100 | [diff] [blame] | 1935 | .get_link_net = ip6_tnl_get_link_net, |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1936 | }; |
| 1937 | |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 1938 | static struct rtnl_link_ops ip6erspan_tap_ops __read_mostly = { |
| 1939 | .kind = "ip6erspan", |
| 1940 | .maxtype = IFLA_GRE_MAX, |
| 1941 | .policy = ip6gre_policy, |
| 1942 | .priv_size = sizeof(struct ip6_tnl), |
| 1943 | .setup = ip6erspan_tap_setup, |
| 1944 | .validate = ip6erspan_tap_validate, |
| 1945 | .newlink = ip6gre_newlink, |
| 1946 | .changelink = ip6gre_changelink, |
| 1947 | .get_size = ip6gre_get_size, |
| 1948 | .fill_info = ip6gre_fill_info, |
| 1949 | .get_link_net = ip6_tnl_get_link_net, |
| 1950 | }; |
| 1951 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1952 | /* |
| 1953 | * And now the modules code and kernel interface. |
| 1954 | */ |
| 1955 | |
| 1956 | static int __init ip6gre_init(void) |
| 1957 | { |
| 1958 | int err; |
| 1959 | |
| 1960 | pr_info("GRE over IPv6 tunneling driver\n"); |
| 1961 | |
| 1962 | err = register_pernet_device(&ip6gre_net_ops); |
| 1963 | if (err < 0) |
| 1964 | return err; |
| 1965 | |
| 1966 | err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE); |
| 1967 | if (err < 0) { |
| 1968 | pr_info("%s: can't add protocol\n", __func__); |
| 1969 | goto add_proto_failed; |
| 1970 | } |
| 1971 | |
| 1972 | err = rtnl_link_register(&ip6gre_link_ops); |
| 1973 | if (err < 0) |
| 1974 | goto rtnl_link_failed; |
| 1975 | |
| 1976 | err = rtnl_link_register(&ip6gre_tap_ops); |
| 1977 | if (err < 0) |
| 1978 | goto tap_ops_failed; |
| 1979 | |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 1980 | err = rtnl_link_register(&ip6erspan_tap_ops); |
| 1981 | if (err < 0) |
| 1982 | goto erspan_link_failed; |
| 1983 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1984 | out: |
| 1985 | return err; |
| 1986 | |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 1987 | erspan_link_failed: |
| 1988 | rtnl_link_unregister(&ip6gre_tap_ops); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1989 | tap_ops_failed: |
| 1990 | rtnl_link_unregister(&ip6gre_link_ops); |
| 1991 | rtnl_link_failed: |
| 1992 | inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE); |
| 1993 | add_proto_failed: |
| 1994 | unregister_pernet_device(&ip6gre_net_ops); |
| 1995 | goto out; |
| 1996 | } |
| 1997 | |
| 1998 | static void __exit ip6gre_fini(void) |
| 1999 | { |
| 2000 | rtnl_link_unregister(&ip6gre_tap_ops); |
| 2001 | rtnl_link_unregister(&ip6gre_link_ops); |
William Tu | 5a963eb | 2017-11-30 11:51:29 -0800 | [diff] [blame] | 2002 | rtnl_link_unregister(&ip6erspan_tap_ops); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 2003 | inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE); |
| 2004 | unregister_pernet_device(&ip6gre_net_ops); |
| 2005 | } |
| 2006 | |
| 2007 | module_init(ip6gre_init); |
| 2008 | module_exit(ip6gre_fini); |
| 2009 | MODULE_LICENSE("GPL"); |
| 2010 | MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)"); |
| 2011 | MODULE_DESCRIPTION("GRE over IPv6 tunneling device"); |
| 2012 | MODULE_ALIAS_RTNL_LINK("ip6gre"); |
Nicolas Dichtel | 5a4ee9a | 2014-09-24 11:03:00 +0200 | [diff] [blame] | 2013 | MODULE_ALIAS_RTNL_LINK("ip6gretap"); |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 2014 | MODULE_ALIAS_NETDEV("ip6gre0"); |