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