blob: 50913dbd0612e985164b0017de2ddbfa45697dfe [file] [log] [blame]
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001/*
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.ruc12b3952012-08-10 00:51:50 +000027#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 Shelarc5441932013-03-25 14:49:35 +000040#include <net/ip_tunnels.h>
xeb@mail.ruc12b3952012-08-10 00:51:50 +000041#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 Herbert308edfdf2016-04-29 17:12:17 -070057#include <net/gre.h>
William Tu5a963eb2017-11-30 11:51:29 -080058#include <net/erspan.h>
William Tu6712abc2017-12-01 15:26:08 -080059#include <net/dst_metadata.h>
xeb@mail.ruc12b3952012-08-10 00:51:50 +000060
61
stephen hemmingereccc1bb2012-09-25 11:02:48 +000062static bool log_ecn_error = true;
63module_param(log_ecn_error, bool, 0644);
64MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
65
Jiri Kosinae87a8f22016-08-10 11:03:35 +020066#define IP6_GRE_HASH_SIZE_SHIFT 5
67#define IP6_GRE_HASH_SIZE (1 << IP6_GRE_HASH_SIZE_SHIFT)
xeb@mail.ruc12b3952012-08-10 00:51:50 +000068
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030069static unsigned int ip6gre_net_id __read_mostly;
xeb@mail.ruc12b3952012-08-10 00:51:50 +000070struct ip6gre_net {
Jiri Kosinae87a8f22016-08-10 11:03:35 +020071 struct ip6_tnl __rcu *tunnels[4][IP6_GRE_HASH_SIZE];
xeb@mail.ruc12b3952012-08-10 00:51:50 +000072
William Tu6712abc2017-12-01 15:26:08 -080073 struct ip6_tnl __rcu *collect_md_tun;
xeb@mail.ruc12b3952012-08-10 00:51:50 +000074 struct net_device *fb_tunnel_dev;
75};
76
77static struct rtnl_link_ops ip6gre_link_ops __read_mostly;
Nicolas Dichtel22f08062014-04-22 10:15:24 +020078static struct rtnl_link_ops ip6gre_tap_ops __read_mostly;
William Tu5a963eb2017-11-30 11:51:29 -080079static struct rtnl_link_ops ip6erspan_tap_ops __read_mostly;
xeb@mail.ruc12b3952012-08-10 00:51:50 +000080static int ip6gre_tunnel_init(struct net_device *dev);
81static void ip6gre_tunnel_setup(struct net_device *dev);
82static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t);
83static 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 Kosinae87a8f22016-08-10 11:03:35 +0200103#define HASH_KEY(key) (((__force u32)key^((__force u32)key>>4))&(IP6_GRE_HASH_SIZE - 1))
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000104static u32 HASH_ADDR(const struct in6_addr *addr)
105{
106 u32 hash = ipv6_addr_hash(addr);
107
Jiri Kosinae87a8f22016-08-10 11:03:35 +0200108 return hash_32(hash, IP6_GRE_HASH_SIZE_SHIFT);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000109}
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.ruc12b3952012-08-10 00:51:50 +0000115
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000116/* Given src, dst and key, find appropriate for input tunnel. */
117
118static 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 Tu5a963eb2017-11-30 11:51:29 -0800128 int dev_type = (gre_proto == htons(ETH_P_TEB) ||
129 gre_proto == htons(ETH_P_ERSPAN)) ?
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000130 ARPHRD_ETHER : ARPHRD_IP6GRE;
131 int score, cand_score = 4;
132
Amerigo Wange086cad2012-11-11 21:52:34 +0000133 for_each_ip_tunnel_rcu(t, ign->tunnels_r_l[h0 ^ h1]) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000134 if (!ipv6_addr_equal(local, &t->parms.laddr) ||
135 !ipv6_addr_equal(remote, &t->parms.raddr) ||
136 key != t->parms.i_key ||
137 !(t->dev->flags & IFF_UP))
138 continue;
139
140 if (t->dev->type != ARPHRD_IP6GRE &&
141 t->dev->type != dev_type)
142 continue;
143
144 score = 0;
145 if (t->parms.link != link)
146 score |= 1;
147 if (t->dev->type != dev_type)
148 score |= 2;
149 if (score == 0)
150 return t;
151
152 if (score < cand_score) {
153 cand = t;
154 cand_score = score;
155 }
156 }
157
Amerigo Wange086cad2012-11-11 21:52:34 +0000158 for_each_ip_tunnel_rcu(t, ign->tunnels_r[h0 ^ h1]) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000159 if (!ipv6_addr_equal(remote, &t->parms.raddr) ||
160 key != t->parms.i_key ||
161 !(t->dev->flags & IFF_UP))
162 continue;
163
164 if (t->dev->type != ARPHRD_IP6GRE &&
165 t->dev->type != dev_type)
166 continue;
167
168 score = 0;
169 if (t->parms.link != link)
170 score |= 1;
171 if (t->dev->type != dev_type)
172 score |= 2;
173 if (score == 0)
174 return t;
175
176 if (score < cand_score) {
177 cand = t;
178 cand_score = score;
179 }
180 }
181
Amerigo Wange086cad2012-11-11 21:52:34 +0000182 for_each_ip_tunnel_rcu(t, ign->tunnels_l[h1]) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000183 if ((!ipv6_addr_equal(local, &t->parms.laddr) &&
184 (!ipv6_addr_equal(local, &t->parms.raddr) ||
185 !ipv6_addr_is_multicast(local))) ||
186 key != t->parms.i_key ||
187 !(t->dev->flags & IFF_UP))
188 continue;
189
190 if (t->dev->type != ARPHRD_IP6GRE &&
191 t->dev->type != dev_type)
192 continue;
193
194 score = 0;
195 if (t->parms.link != link)
196 score |= 1;
197 if (t->dev->type != dev_type)
198 score |= 2;
199 if (score == 0)
200 return t;
201
202 if (score < cand_score) {
203 cand = t;
204 cand_score = score;
205 }
206 }
207
Amerigo Wange086cad2012-11-11 21:52:34 +0000208 for_each_ip_tunnel_rcu(t, ign->tunnels_wc[h1]) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000209 if (t->parms.i_key != key ||
210 !(t->dev->flags & IFF_UP))
211 continue;
212
213 if (t->dev->type != ARPHRD_IP6GRE &&
214 t->dev->type != dev_type)
215 continue;
216
217 score = 0;
218 if (t->parms.link != link)
219 score |= 1;
220 if (t->dev->type != dev_type)
221 score |= 2;
222 if (score == 0)
223 return t;
224
225 if (score < cand_score) {
226 cand = t;
227 cand_score = score;
228 }
229 }
230
Ian Morris53b24b82015-03-29 14:00:05 +0100231 if (cand)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000232 return cand;
233
William Tu6712abc2017-12-01 15:26:08 -0800234 t = rcu_dereference(ign->collect_md_tun);
235 if (t && t->dev->flags & IFF_UP)
236 return t;
237
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000238 dev = ign->fb_tunnel_dev;
239 if (dev->flags & IFF_UP)
240 return netdev_priv(dev);
241
242 return NULL;
243}
244
245static struct ip6_tnl __rcu **__ip6gre_bucket(struct ip6gre_net *ign,
246 const struct __ip6_tnl_parm *p)
247{
248 const struct in6_addr *remote = &p->raddr;
249 const struct in6_addr *local = &p->laddr;
250 unsigned int h = HASH_KEY(p->i_key);
251 int prio = 0;
252
253 if (!ipv6_addr_any(local))
254 prio |= 1;
255 if (!ipv6_addr_any(remote) && !ipv6_addr_is_multicast(remote)) {
256 prio |= 2;
257 h ^= HASH_ADDR(remote);
258 }
259
260 return &ign->tunnels[prio][h];
261}
262
263static inline struct ip6_tnl __rcu **ip6gre_bucket(struct ip6gre_net *ign,
264 const struct ip6_tnl *t)
265{
266 return __ip6gre_bucket(ign, &t->parms);
267}
268
269static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t)
270{
271 struct ip6_tnl __rcu **tp = ip6gre_bucket(ign, t);
272
William Tu6712abc2017-12-01 15:26:08 -0800273 if (t->parms.collect_md)
274 rcu_assign_pointer(ign->collect_md_tun, t);
275
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000276 rcu_assign_pointer(t->next, rtnl_dereference(*tp));
277 rcu_assign_pointer(*tp, t);
278}
279
280static void ip6gre_tunnel_unlink(struct ip6gre_net *ign, struct ip6_tnl *t)
281{
282 struct ip6_tnl __rcu **tp;
283 struct ip6_tnl *iter;
284
William Tu6712abc2017-12-01 15:26:08 -0800285 if (t->parms.collect_md)
286 rcu_assign_pointer(ign->collect_md_tun, NULL);
287
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000288 for (tp = ip6gre_bucket(ign, t);
289 (iter = rtnl_dereference(*tp)) != NULL;
290 tp = &iter->next) {
291 if (t == iter) {
292 rcu_assign_pointer(*tp, t->next);
293 break;
294 }
295 }
296}
297
298static struct ip6_tnl *ip6gre_tunnel_find(struct net *net,
299 const struct __ip6_tnl_parm *parms,
300 int type)
301{
302 const struct in6_addr *remote = &parms->raddr;
303 const struct in6_addr *local = &parms->laddr;
304 __be32 key = parms->i_key;
305 int link = parms->link;
306 struct ip6_tnl *t;
307 struct ip6_tnl __rcu **tp;
308 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
309
310 for (tp = __ip6gre_bucket(ign, parms);
311 (t = rtnl_dereference(*tp)) != NULL;
312 tp = &t->next)
313 if (ipv6_addr_equal(local, &t->parms.laddr) &&
314 ipv6_addr_equal(remote, &t->parms.raddr) &&
315 key == t->parms.i_key &&
316 link == t->parms.link &&
317 type == t->dev->type)
318 break;
319
320 return t;
321}
322
323static struct ip6_tnl *ip6gre_tunnel_locate(struct net *net,
324 const struct __ip6_tnl_parm *parms, int create)
325{
326 struct ip6_tnl *t, *nt;
327 struct net_device *dev;
328 char name[IFNAMSIZ];
329 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
330
331 t = ip6gre_tunnel_find(net, parms, ARPHRD_IP6GRE);
Steffen Klassertcd0a0bd2014-09-22 10:07:26 +0200332 if (t && create)
333 return NULL;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000334 if (t || !create)
335 return t;
336
337 if (parms->name[0])
338 strlcpy(name, parms->name, IFNAMSIZ);
339 else
340 strcpy(name, "ip6gre%d");
341
Tom Gundersenc835a672014-07-14 16:37:24 +0200342 dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
343 ip6gre_tunnel_setup);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000344 if (!dev)
345 return NULL;
346
347 dev_net_set(dev, net);
348
349 nt = netdev_priv(dev);
350 nt->parms = *parms;
351 dev->rtnl_link_ops = &ip6gre_link_ops;
352
353 nt->dev = dev;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +0200354 nt->net = dev_net(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000355
356 if (register_netdevice(dev) < 0)
357 goto failed_free;
358
Alexey Kodanev128bb972018-01-18 20:51:12 +0300359 ip6gre_tnl_link_config(nt, 1);
360
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000361 /* Can use a lockless transmit, unless we generate output sequences */
Tom Herbertb45bd1d2016-05-09 17:12:12 -0700362 if (!(nt->parms.o_flags & TUNNEL_SEQ))
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000363 dev->features |= NETIF_F_LLTX;
364
365 dev_hold(dev);
366 ip6gre_tunnel_link(ign, nt);
367 return nt;
368
369failed_free:
370 free_netdev(dev);
371 return NULL;
372}
373
374static void ip6gre_tunnel_uninit(struct net_device *dev)
375{
Nicolas Dichtel22f08062014-04-22 10:15:24 +0200376 struct ip6_tnl *t = netdev_priv(dev);
377 struct ip6gre_net *ign = net_generic(t->net, ip6gre_net_id);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000378
Nicolas Dichtel22f08062014-04-22 10:15:24 +0200379 ip6gre_tunnel_unlink(ign, t);
Paolo Abeni607f7252016-02-12 15:43:54 +0100380 dst_cache_reset(&t->dst_cache);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000381 dev_put(dev);
382}
383
384
385static void ip6gre_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Eric Dumazet78920322017-02-04 23:18:55 -0800386 u8 type, u8 code, int offset, __be32 info)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000387{
Xin Long929fc032017-11-11 19:06:49 +0800388 struct net *net = dev_net(skb->dev);
Eric Dumazet78920322017-02-04 23:18:55 -0800389 const struct gre_base_hdr *greh;
390 const struct ipv6hdr *ipv6h;
391 int grehlen = sizeof(*greh);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000392 struct ip6_tnl *t;
Eric Dumazet78920322017-02-04 23:18:55 -0800393 int key_off = 0;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000394 __be16 flags;
Eric Dumazet78920322017-02-04 23:18:55 -0800395 __be32 key;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000396
Eric Dumazet78920322017-02-04 23:18:55 -0800397 if (!pskb_may_pull(skb, offset + grehlen))
398 return;
399 greh = (const struct gre_base_hdr *)(skb->data + offset);
400 flags = greh->flags;
401 if (flags & (GRE_VERSION | GRE_ROUTING))
402 return;
403 if (flags & GRE_CSUM)
404 grehlen += 4;
405 if (flags & GRE_KEY) {
406 key_off = grehlen + offset;
407 grehlen += 4;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000408 }
409
Eric Dumazet78920322017-02-04 23:18:55 -0800410 if (!pskb_may_pull(skb, offset + grehlen))
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000411 return;
Eric Dumazetb87fb392012-08-19 03:47:30 +0000412 ipv6h = (const struct ipv6hdr *)skb->data;
Eric Dumazet78920322017-02-04 23:18:55 -0800413 greh = (const struct gre_base_hdr *)(skb->data + offset);
414 key = key_off ? *(__be32 *)(skb->data + key_off) : 0;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000415
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000416 t = ip6gre_tunnel_lookup(skb->dev, &ipv6h->daddr, &ipv6h->saddr,
Eric Dumazet78920322017-02-04 23:18:55 -0800417 key, greh->protocol);
Ian Morris63159f22015-03-29 14:00:04 +0100418 if (!t)
stephen hemminger0c5794a2012-09-24 18:12:24 +0000419 return;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000420
421 switch (type) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000422 struct ipv6_tlv_tnl_enc_lim *tel;
Xin Longfe1a4ca2017-11-11 19:06:50 +0800423 __u32 teli;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000424 case ICMPV6_DEST_UNREACH:
Matt Bennetta46496c2015-09-23 16:58:31 +1200425 net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n",
426 t->parms.name);
Xin Longf8d20b42017-10-26 19:23:27 +0800427 if (code != ICMPV6_PORT_UNREACH)
428 break;
429 return;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000430 case ICMPV6_TIME_EXCEED:
431 if (code == ICMPV6_EXC_HOPLIMIT) {
Matt Bennetta46496c2015-09-23 16:58:31 +1200432 net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
433 t->parms.name);
Xin Longf8d20b42017-10-26 19:23:27 +0800434 break;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000435 }
Xin Longf8d20b42017-10-26 19:23:27 +0800436 return;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000437 case ICMPV6_PARAMPROB:
438 teli = 0;
439 if (code == ICMPV6_HDR_FIELD)
440 teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
441
Sabrina Dubrocad1e158e2015-02-04 15:25:09 +0100442 if (teli && teli == be32_to_cpu(info) - 2) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000443 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
444 if (tel->encap_limit == 0) {
Matt Bennetta46496c2015-09-23 16:58:31 +1200445 net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
446 t->parms.name);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000447 }
448 } else {
Matt Bennetta46496c2015-09-23 16:58:31 +1200449 net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
450 t->parms.name);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000451 }
Xin Longf8d20b42017-10-26 19:23:27 +0800452 return;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000453 case ICMPV6_PKT_TOOBIG:
Xin Longfe1a4ca2017-11-11 19:06:50 +0800454 ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
Xin Longf8d20b42017-10-26 19:23:27 +0800455 return;
Xin Long929fc032017-11-11 19:06:49 +0800456 case NDISC_REDIRECT:
457 ip6_redirect(skb, net, skb->dev->ifindex, 0,
458 sock_net_uid(net, NULL));
459 return;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000460 }
461
462 if (time_before(jiffies, t->err_time + IP6TUNNEL_ERR_TIMEO))
463 t->err_count++;
464 else
465 t->err_count = 1;
466 t->err_time = jiffies;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000467}
468
Tom Herbert308edfdf2016-04-29 17:12:17 -0700469static int ip6gre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000470{
471 const struct ipv6hdr *ipv6h;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000472 struct ip6_tnl *tunnel;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000473
474 ipv6h = ipv6_hdr(skb);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000475 tunnel = ip6gre_tunnel_lookup(skb->dev,
Tom Herbert308edfdf2016-04-29 17:12:17 -0700476 &ipv6h->saddr, &ipv6h->daddr, tpi->key,
477 tpi->proto);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000478 if (tunnel) {
William Tu6712abc2017-12-01 15:26:08 -0800479 if (tunnel->parms.collect_md) {
480 struct metadata_dst *tun_dst;
481 __be64 tun_id;
482 __be16 flags;
483
484 flags = tpi->flags;
485 tun_id = key32_to_tunnel_id(tpi->key);
486
487 tun_dst = ipv6_tun_rx_dst(skb, flags, tun_id, 0);
488 if (!tun_dst)
489 return PACKET_REJECT;
490
491 ip6_tnl_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
492 } else {
493 ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error);
494 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000495
Tom Herbert308edfdf2016-04-29 17:12:17 -0700496 return PACKET_RCVD;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000497 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000498
Tom Herbert308edfdf2016-04-29 17:12:17 -0700499 return PACKET_REJECT;
500}
501
William Tu5a963eb2017-11-30 11:51:29 -0800502static int ip6erspan_rcv(struct sk_buff *skb, int gre_hdr_len,
503 struct tnl_ptk_info *tpi)
504{
William Tu1d7e2ed2017-12-13 16:38:55 -0800505 struct erspan_base_hdr *ershdr;
506 struct erspan_metadata *pkt_md;
William Tu5a963eb2017-11-30 11:51:29 -0800507 const struct ipv6hdr *ipv6h;
William Tu3df192832018-02-05 13:35:34 -0800508 struct erspan_md2 *md2;
William Tu5a963eb2017-11-30 11:51:29 -0800509 struct ip6_tnl *tunnel;
William Tu1d7e2ed2017-12-13 16:38:55 -0800510 u8 ver;
William Tu5a963eb2017-11-30 11:51:29 -0800511
William Tu5a963eb2017-11-30 11:51:29 -0800512 if (unlikely(!pskb_may_pull(skb, sizeof(*ershdr))))
513 return PACKET_REJECT;
514
Haishuang Yan293a1992017-12-20 09:53:19 +0800515 ipv6h = ipv6_hdr(skb);
516 ershdr = (struct erspan_base_hdr *)skb->data;
William Tuc69de582018-01-25 13:20:09 -0800517 ver = ershdr->ver;
518 tpi->key = cpu_to_be32(get_session_id(ershdr));
William Tu5a963eb2017-11-30 11:51:29 -0800519
520 tunnel = ip6gre_tunnel_lookup(skb->dev,
521 &ipv6h->saddr, &ipv6h->daddr, tpi->key,
522 tpi->proto);
523 if (tunnel) {
William Tu1d7e2ed2017-12-13 16:38:55 -0800524 int len = erspan_hdr_len(ver);
525
526 if (unlikely(!pskb_may_pull(skb, len)))
William Tuae3e1332017-12-15 14:27:43 -0800527 return PACKET_REJECT;
William Tu1d7e2ed2017-12-13 16:38:55 -0800528
William Tud91e8db52017-12-15 14:27:44 -0800529 ershdr = (struct erspan_base_hdr *)skb->data;
530 pkt_md = (struct erspan_metadata *)(ershdr + 1);
531
William Tu1d7e2ed2017-12-13 16:38:55 -0800532 if (__iptunnel_pull_header(skb, len,
William Tu5a963eb2017-11-30 11:51:29 -0800533 htons(ETH_P_TEB),
534 false, false) < 0)
535 return PACKET_REJECT;
536
William Tuef7baf52017-12-05 15:15:44 -0800537 if (tunnel->parms.collect_md) {
538 struct metadata_dst *tun_dst;
539 struct ip_tunnel_info *info;
540 struct erspan_metadata *md;
541 __be64 tun_id;
542 __be16 flags;
543
544 tpi->flags |= TUNNEL_KEY;
545 flags = tpi->flags;
546 tun_id = key32_to_tunnel_id(tpi->key);
547
548 tun_dst = ipv6_tun_rx_dst(skb, flags, tun_id,
549 sizeof(*md));
550 if (!tun_dst)
551 return PACKET_REJECT;
552
553 info = &tun_dst->u.tun_info;
554 md = ip_tunnel_info_opts(info);
William Tu94d7d8f2017-12-13 16:38:57 -0800555 md->version = ver;
William Tu3df192832018-02-05 13:35:34 -0800556 md2 = &md->u.md2;
557 memcpy(md2, pkt_md, ver == 1 ? ERSPAN_V1_MDSIZE :
558 ERSPAN_V2_MDSIZE);
William Tuef7baf52017-12-05 15:15:44 -0800559 info->key.tun_flags |= TUNNEL_ERSPAN_OPT;
560 info->options_len = sizeof(*md);
561
562 ip6_tnl_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
563
564 } else {
William Tu94d7d8f2017-12-13 16:38:57 -0800565 tunnel->parms.erspan_ver = ver;
566
567 if (ver == 1) {
568 tunnel->parms.index = ntohl(pkt_md->u.index);
569 } else {
William Tuc69de582018-01-25 13:20:09 -0800570 tunnel->parms.dir = pkt_md->u.md2.dir;
571 tunnel->parms.hwid = get_hwid(&pkt_md->u.md2);
William Tu94d7d8f2017-12-13 16:38:57 -0800572 }
573
William Tuef7baf52017-12-05 15:15:44 -0800574 ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error);
575 }
William Tu5a963eb2017-11-30 11:51:29 -0800576
577 return PACKET_RCVD;
578 }
579
580 return PACKET_REJECT;
581}
582
Tom Herbert308edfdf2016-04-29 17:12:17 -0700583static int gre_rcv(struct sk_buff *skb)
584{
585 struct tnl_ptk_info tpi;
586 bool csum_err = false;
587 int hdr_len;
588
Eric Dumazete582615ad2016-06-15 06:24:00 -0700589 hdr_len = gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IPV6), 0);
Jiri Bencf132ae72016-05-03 15:00:21 +0200590 if (hdr_len < 0)
Tom Herbert308edfdf2016-04-29 17:12:17 -0700591 goto drop;
592
593 if (iptunnel_pull_header(skb, hdr_len, tpi.proto, false))
594 goto drop;
595
William Tu94d7d8f2017-12-13 16:38:57 -0800596 if (unlikely(tpi.proto == htons(ETH_P_ERSPAN) ||
597 tpi.proto == htons(ETH_P_ERSPAN2))) {
William Tu5a963eb2017-11-30 11:51:29 -0800598 if (ip6erspan_rcv(skb, hdr_len, &tpi) == PACKET_RCVD)
599 return 0;
Haishuang Yana7343212017-12-20 10:21:47 +0800600 goto out;
William Tu5a963eb2017-11-30 11:51:29 -0800601 }
602
Tom Herbert308edfdf2016-04-29 17:12:17 -0700603 if (ip6gre_rcv(skb, &tpi) == PACKET_RCVD)
604 return 0;
605
Haishuang Yana7343212017-12-20 10:21:47 +0800606out:
Tom Herbert308edfdf2016-04-29 17:12:17 -0700607 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000608drop:
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000609 kfree_skb(skb);
610 return 0;
611}
612
Tom Herbertb05229f2016-04-29 17:12:21 -0700613static int gre_handle_offloads(struct sk_buff *skb, bool csum)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000614{
Tom Herbertb05229f2016-04-29 17:12:21 -0700615 return iptunnel_handle_offloads(skb,
616 csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000617}
618
William Tu898b29792017-11-30 11:51:28 -0800619static void prepare_ip6gre_xmit_ipv4(struct sk_buff *skb,
620 struct net_device *dev,
621 struct flowi6 *fl6, __u8 *dsfield,
622 int *encap_limit)
623{
624 const struct iphdr *iph = ip_hdr(skb);
625 struct ip6_tnl *t = netdev_priv(dev);
626
627 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
628 *encap_limit = t->parms.encap_limit;
629
630 memcpy(fl6, &t->fl.u.ip6, sizeof(*fl6));
631
632 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
633 *dsfield = ipv4_get_dsfield(iph);
634 else
635 *dsfield = ip6_tclass(t->parms.flowinfo);
636
637 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
638 fl6->flowi6_mark = skb->mark;
639 else
640 fl6->flowi6_mark = t->parms.fwmark;
641
642 fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
643}
644
645static int prepare_ip6gre_xmit_ipv6(struct sk_buff *skb,
646 struct net_device *dev,
647 struct flowi6 *fl6, __u8 *dsfield,
648 int *encap_limit)
649{
650 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
651 struct ip6_tnl *t = netdev_priv(dev);
652 __u16 offset;
653
654 offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
655 /* ip6_tnl_parse_tlv_enc_lim() might have reallocated skb->head */
656
657 if (offset > 0) {
658 struct ipv6_tlv_tnl_enc_lim *tel;
659
660 tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
661 if (tel->encap_limit == 0) {
662 icmpv6_send(skb, ICMPV6_PARAMPROB,
663 ICMPV6_HDR_FIELD, offset + 2);
664 return -1;
665 }
666 *encap_limit = tel->encap_limit - 1;
667 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) {
668 *encap_limit = t->parms.encap_limit;
669 }
670
671 memcpy(fl6, &t->fl.u.ip6, sizeof(*fl6));
672
673 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
674 *dsfield = ipv6_get_dsfield(ipv6h);
675 else
676 *dsfield = ip6_tclass(t->parms.flowinfo);
677
678 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
679 fl6->flowlabel |= ip6_flowlabel(ipv6h);
680
681 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
682 fl6->flowi6_mark = skb->mark;
683 else
684 fl6->flowi6_mark = t->parms.fwmark;
685
686 fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
687
688 return 0;
689}
690
Tom Herbertb05229f2016-04-29 17:12:21 -0700691static netdev_tx_t __gre6_xmit(struct sk_buff *skb,
692 struct net_device *dev, __u8 dsfield,
693 struct flowi6 *fl6, int encap_limit,
694 __u32 *pmtu, __be16 proto)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000695{
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000696 struct ip6_tnl *tunnel = netdev_priv(dev);
Xin Long8aec4952017-10-26 19:27:17 +0800697 __be16 protocol;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000698
699 if (dev->type == ARPHRD_ETHER)
700 IPCB(skb)->flags = 0;
701
Tom Herbertb05229f2016-04-29 17:12:21 -0700702 if (dev->header_ops && dev->type == ARPHRD_IP6GRE)
703 fl6->daddr = ((struct ipv6hdr *)skb->data)->daddr;
704 else
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000705 fl6->daddr = tunnel->parms.raddr;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000706
Tom Herbertb05229f2016-04-29 17:12:21 -0700707 if (tunnel->parms.o_flags & TUNNEL_SEQ)
708 tunnel->o_seqno++;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000709
Tom Herbertb05229f2016-04-29 17:12:21 -0700710 /* Push GRE header. */
Xin Long8aec4952017-10-26 19:27:17 +0800711 protocol = (dev->type == ARPHRD_ETHER) ? htons(ETH_P_TEB) : proto;
William Tu6712abc2017-12-01 15:26:08 -0800712
713 if (tunnel->parms.collect_md) {
714 struct ip_tunnel_info *tun_info;
715 const struct ip_tunnel_key *key;
716 __be16 flags;
717
718 tun_info = skb_tunnel_info(skb);
719 if (unlikely(!tun_info ||
720 !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
721 ip_tunnel_info_af(tun_info) != AF_INET6))
722 return -EINVAL;
723
724 key = &tun_info->key;
725 memset(fl6, 0, sizeof(*fl6));
726 fl6->flowi6_proto = IPPROTO_GRE;
727 fl6->daddr = key->u.ipv6.dst;
728 fl6->flowlabel = key->label;
729 fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
730
731 dsfield = key->tos;
732 flags = key->tun_flags & (TUNNEL_CSUM | TUNNEL_KEY);
733 tunnel->tun_hlen = gre_calc_hlen(flags);
734
735 gre_build_header(skb, tunnel->tun_hlen,
736 flags, protocol,
737 tunnel_id_to_key32(tun_info->key.tun_id), 0);
738
739 } else {
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.ruc12b3952012-08-10 00:51:50 +0000744
Tom Herbertb05229f2016-04-29 17:12:21 -0700745 return ip6_tnl_xmit(skb, dev, dsfield, fl6, encap_limit, pmtu,
746 NEXTHDR_GRE);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000747}
748
749static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev)
750{
751 struct ip6_tnl *t = netdev_priv(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000752 int encap_limit = -1;
753 struct flowi6 fl6;
William Tu6712abc2017-12-01 15:26:08 -0800754 __u8 dsfield = 0;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000755 __u32 mtu;
756 int err;
757
Bernie Harris5146d1f2016-02-22 12:58:05 +1300758 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
759
William Tu6712abc2017-12-01 15:26:08 -0800760 if (!t->parms.collect_md)
761 prepare_ip6gre_xmit_ipv4(skb, dev, &fl6,
762 &dsfield, &encap_limit);
Lorenzo Colittie2d118a2016-11-04 02:23:43 +0900763
Tom Herbertb05229f2016-04-29 17:12:21 -0700764 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.ruc12b3952012-08-10 00:51:50 +0000770 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
781static 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.ruc12b3952012-08-10 00:51:50 +0000786 struct flowi6 fl6;
William Tu6712abc2017-12-01 15:26:08 -0800787 __u8 dsfield = 0;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000788 __u32 mtu;
789 int err;
790
791 if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
792 return -1;
793
William Tu6712abc2017-12-01 15:26:08 -0800794 if (!t->parms.collect_md &&
795 prepare_ip6gre_xmit_ipv6(skb, dev, &fl6, &dsfield, &encap_limit))
William Tu898b29792017-11-30 11:51:28 -0800796 return -1;
Lorenzo Colittie2d118a2016-11-04 02:23:43 +0900797
Tom Herbertb05229f2016-04-29 17:12:21 -0700798 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.ruc12b3952012-08-10 00:51:50 +0000803 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
826static 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
832static 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 Tu6712abc2017-12-01 15:26:08 -0800843 if (!t->parms.collect_md)
844 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000845
Tom Herbertb05229f2016-04-29 17:12:21 -0700846 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.ruc12b3952012-08-10 00:51:50 +0000851
852 return err;
853}
854
855static 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 Klassertd5005142014-11-05 08:02:48 +0100862 if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
Tommi Rantala41ab3e32013-02-06 03:24:02 +0000863 goto tx_err;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000864
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
882tx_err:
883 stats->tx_errors++;
884 stats->tx_dropped++;
885 kfree_skb(skb);
886 return NETDEV_TX_OK;
887}
888
William Tu5a963eb2017-11-30 11:51:29 -0800889static 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 Tu5a963eb2017-11-30 11:51:29 -0800909 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 Tu5a963eb2017-11-30 11:51:29 -0800914 t->parms.o_flags &= ~TUNNEL_KEY;
William Tu5a963eb2017-11-30 11:51:29 -0800915 IPCB(skb)->flags = 0;
William Tuef7baf52017-12-05 15:15:44 -0800916
917 /* For collect_md mode, derive fl6 from the tunnel key,
918 * for native mode, call prepare_ip6gre_xmit_{ipv4,ipv6}.
919 */
920 if (t->parms.collect_md) {
921 struct ip_tunnel_info *tun_info;
922 const struct ip_tunnel_key *key;
923 struct erspan_metadata *md;
William Tuc69de582018-01-25 13:20:09 -0800924 __be32 tun_id;
William Tuef7baf52017-12-05 15:15:44 -0800925
926 tun_info = skb_tunnel_info(skb);
927 if (unlikely(!tun_info ||
928 !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
929 ip_tunnel_info_af(tun_info) != AF_INET6))
930 return -EINVAL;
931
932 key = &tun_info->key;
933 memset(&fl6, 0, sizeof(fl6));
934 fl6.flowi6_proto = IPPROTO_GRE;
935 fl6.daddr = key->u.ipv6.dst;
936 fl6.flowlabel = key->label;
937 fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
938
939 dsfield = key->tos;
940 md = ip_tunnel_info_opts(tun_info);
941 if (!md)
942 goto tx_err;
943
William Tuc69de582018-01-25 13:20:09 -0800944 tun_id = tunnel_id_to_key32(key->tun_id);
William Tu94d7d8f2017-12-13 16:38:57 -0800945 if (md->version == 1) {
946 erspan_build_header(skb,
William Tuc69de582018-01-25 13:20:09 -0800947 ntohl(tun_id),
William Tu94d7d8f2017-12-13 16:38:57 -0800948 ntohl(md->u.index), truncate,
949 false);
950 } else if (md->version == 2) {
William Tu94d7d8f2017-12-13 16:38:57 -0800951 erspan_build_header_v2(skb,
William Tuc69de582018-01-25 13:20:09 -0800952 ntohl(tun_id),
953 md->u.md2.dir,
954 get_hwid(&md->u.md2),
955 truncate, false);
William Tu94d7d8f2017-12-13 16:38:57 -0800956 }
William Tuef7baf52017-12-05 15:15:44 -0800957 } else {
958 switch (skb->protocol) {
959 case htons(ETH_P_IP):
960 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
961 prepare_ip6gre_xmit_ipv4(skb, dev, &fl6,
962 &dsfield, &encap_limit);
963 break;
964 case htons(ETH_P_IPV6):
965 if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
966 goto tx_err;
967 if (prepare_ip6gre_xmit_ipv6(skb, dev, &fl6,
968 &dsfield, &encap_limit))
969 goto tx_err;
970 break;
971 default:
972 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
973 break;
974 }
975
William Tu94d7d8f2017-12-13 16:38:57 -0800976 if (t->parms.erspan_ver == 1)
William Tuc69de582018-01-25 13:20:09 -0800977 erspan_build_header(skb, ntohl(t->parms.o_key),
William Tu94d7d8f2017-12-13 16:38:57 -0800978 t->parms.index,
979 truncate, false);
980 else
William Tuc69de582018-01-25 13:20:09 -0800981 erspan_build_header_v2(skb, ntohl(t->parms.o_key),
William Tu94d7d8f2017-12-13 16:38:57 -0800982 t->parms.dir,
983 t->parms.hwid,
984 truncate, false);
William Tuef7baf52017-12-05 15:15:44 -0800985 fl6.daddr = t->parms.raddr;
986 }
William Tu5a963eb2017-11-30 11:51:29 -0800987
988 /* Push GRE header. */
989 gre_build_header(skb, 8, TUNNEL_SEQ,
990 htons(ETH_P_ERSPAN), 0, htonl(t->o_seqno++));
991
992 /* TooBig packet may have updated dst->dev's mtu */
William Tuef7baf52017-12-05 15:15:44 -0800993 if (!t->parms.collect_md && dst && dst_mtu(dst) > dst->dev->mtu)
William Tu5a963eb2017-11-30 11:51:29 -0800994 dst->ops->update_pmtu(dst, NULL, skb, dst->dev->mtu);
995
996 err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
997 NEXTHDR_GRE);
998 if (err != 0) {
999 /* XXX: send ICMP error even if DF is not set. */
1000 if (err == -EMSGSIZE) {
1001 if (skb->protocol == htons(ETH_P_IP))
1002 icmp_send(skb, ICMP_DEST_UNREACH,
1003 ICMP_FRAG_NEEDED, htonl(mtu));
1004 else
1005 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1006 }
1007
1008 goto tx_err;
1009 }
1010 return NETDEV_TX_OK;
1011
1012tx_err:
1013 stats = &t->dev->stats;
1014 stats->tx_errors++;
1015 stats->tx_dropped++;
1016 kfree_skb(skb);
1017 return NETDEV_TX_OK;
1018}
1019
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001020static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu)
1021{
1022 struct net_device *dev = t->dev;
1023 struct __ip6_tnl_parm *p = &t->parms;
1024 struct flowi6 *fl6 = &t->fl.u.ip6;
Tom Herbertdb2ec952016-05-09 17:12:08 -07001025 int t_hlen;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001026
1027 if (dev->type != ARPHRD_ETHER) {
1028 memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
1029 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
1030 }
1031
1032 /* Set up flowi template */
1033 fl6->saddr = p->laddr;
1034 fl6->daddr = p->raddr;
1035 fl6->flowi6_oif = p->link;
1036 fl6->flowlabel = 0;
Haishuang Yan252f3f52016-05-21 18:17:35 +08001037 fl6->flowi6_proto = IPPROTO_GRE;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001038
1039 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
1040 fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
1041 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
1042 fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
1043
1044 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
1045 p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
1046
1047 if (p->flags&IP6_TNL_F_CAP_XMIT &&
1048 p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER)
1049 dev->flags |= IFF_POINTOPOINT;
1050 else
1051 dev->flags &= ~IFF_POINTOPOINT;
1052
Tom Herbertdb2ec952016-05-09 17:12:08 -07001053 t->tun_hlen = gre_calc_hlen(t->parms.o_flags);
1054
Tom Herbert1faf3d92016-05-18 09:06:19 -07001055 t->hlen = t->encap_hlen + t->tun_hlen;
Tom Herbertdb2ec952016-05-09 17:12:08 -07001056
1057 t_hlen = t->hlen + sizeof(struct ipv6hdr);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001058
1059 if (p->flags & IP6_TNL_F_CAP_XMIT) {
1060 int strict = (ipv6_addr_type(&p->raddr) &
1061 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
1062
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001063 struct rt6_info *rt = rt6_lookup(t->net,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001064 &p->raddr, &p->laddr,
1065 p->link, strict);
1066
Ian Morris63159f22015-03-29 14:00:04 +01001067 if (!rt)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001068 return;
1069
1070 if (rt->dst.dev) {
Tom Herbertdb2ec952016-05-09 17:12:08 -07001071 dev->hard_header_len = rt->dst.dev->hard_header_len +
1072 t_hlen;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001073
1074 if (set_mtu) {
Tom Herbertdb2ec952016-05-09 17:12:08 -07001075 dev->mtu = rt->dst.dev->mtu - t_hlen;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001076 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1077 dev->mtu -= 8;
Alexander Duycka9e242c2016-04-14 15:33:45 -04001078 if (dev->type == ARPHRD_ETHER)
1079 dev->mtu -= ETH_HLEN;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001080
1081 if (dev->mtu < IPV6_MIN_MTU)
1082 dev->mtu = IPV6_MIN_MTU;
1083 }
1084 }
Amerigo Wang94e187c2012-10-29 00:13:19 +00001085 ip6_rt_put(rt);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001086 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001087}
1088
1089static int ip6gre_tnl_change(struct ip6_tnl *t,
1090 const struct __ip6_tnl_parm *p, int set_mtu)
1091{
1092 t->parms.laddr = p->laddr;
1093 t->parms.raddr = p->raddr;
1094 t->parms.flags = p->flags;
1095 t->parms.hop_limit = p->hop_limit;
1096 t->parms.encap_limit = p->encap_limit;
1097 t->parms.flowinfo = p->flowinfo;
1098 t->parms.link = p->link;
1099 t->parms.proto = p->proto;
1100 t->parms.i_key = p->i_key;
1101 t->parms.o_key = p->o_key;
1102 t->parms.i_flags = p->i_flags;
1103 t->parms.o_flags = p->o_flags;
Craig Gallek0a473b82017-04-19 12:30:53 -04001104 t->parms.fwmark = p->fwmark;
Paolo Abeni607f7252016-02-12 15:43:54 +01001105 dst_cache_reset(&t->dst_cache);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001106 ip6gre_tnl_link_config(t, set_mtu);
1107 return 0;
1108}
1109
1110static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p,
1111 const struct ip6_tnl_parm2 *u)
1112{
1113 p->laddr = u->laddr;
1114 p->raddr = u->raddr;
1115 p->flags = u->flags;
1116 p->hop_limit = u->hop_limit;
1117 p->encap_limit = u->encap_limit;
1118 p->flowinfo = u->flowinfo;
1119 p->link = u->link;
1120 p->i_key = u->i_key;
1121 p->o_key = u->o_key;
Tom Herbertf41fe3c2016-05-09 17:12:09 -07001122 p->i_flags = gre_flags_to_tnl_flags(u->i_flags);
1123 p->o_flags = gre_flags_to_tnl_flags(u->o_flags);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001124 memcpy(p->name, u->name, sizeof(u->name));
1125}
1126
1127static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u,
1128 const struct __ip6_tnl_parm *p)
1129{
1130 u->proto = IPPROTO_GRE;
1131 u->laddr = p->laddr;
1132 u->raddr = p->raddr;
1133 u->flags = p->flags;
1134 u->hop_limit = p->hop_limit;
1135 u->encap_limit = p->encap_limit;
1136 u->flowinfo = p->flowinfo;
1137 u->link = p->link;
1138 u->i_key = p->i_key;
1139 u->o_key = p->o_key;
Tom Herbertf41fe3c2016-05-09 17:12:09 -07001140 u->i_flags = gre_tnl_flags_to_gre_flags(p->i_flags);
1141 u->o_flags = gre_tnl_flags_to_gre_flags(p->o_flags);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001142 memcpy(u->name, p->name, sizeof(u->name));
1143}
1144
1145static int ip6gre_tunnel_ioctl(struct net_device *dev,
1146 struct ifreq *ifr, int cmd)
1147{
1148 int err = 0;
1149 struct ip6_tnl_parm2 p;
1150 struct __ip6_tnl_parm p1;
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001151 struct ip6_tnl *t = netdev_priv(dev);
1152 struct net *net = t->net;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001153 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1154
Tom Herbert308edfdf2016-04-29 17:12:17 -07001155 memset(&p1, 0, sizeof(p1));
1156
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001157 switch (cmd) {
1158 case SIOCGETTUNNEL:
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001159 if (dev == ign->fb_tunnel_dev) {
1160 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
1161 err = -EFAULT;
1162 break;
1163 }
1164 ip6gre_tnl_parm_from_user(&p1, &p);
1165 t = ip6gre_tunnel_locate(net, &p1, 0);
Ian Morris63159f22015-03-29 14:00:04 +01001166 if (!t)
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001167 t = netdev_priv(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001168 }
Amerigo Wang5dbd5062013-05-09 21:56:37 +00001169 memset(&p, 0, sizeof(p));
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001170 ip6gre_tnl_parm_to_user(&p, &t->parms);
1171 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
1172 err = -EFAULT;
1173 break;
1174
1175 case SIOCADDTUNNEL:
1176 case SIOCCHGTUNNEL:
1177 err = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00001178 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001179 goto done;
1180
1181 err = -EFAULT;
1182 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1183 goto done;
1184
1185 err = -EINVAL;
1186 if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))
1187 goto done;
1188
1189 if (!(p.i_flags&GRE_KEY))
1190 p.i_key = 0;
1191 if (!(p.o_flags&GRE_KEY))
1192 p.o_key = 0;
1193
1194 ip6gre_tnl_parm_from_user(&p1, &p);
1195 t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL);
1196
1197 if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
Ian Morris53b24b82015-03-29 14:00:05 +01001198 if (t) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001199 if (t->dev != dev) {
1200 err = -EEXIST;
1201 break;
1202 }
1203 } else {
1204 t = netdev_priv(dev);
1205
1206 ip6gre_tunnel_unlink(ign, t);
1207 synchronize_net();
1208 ip6gre_tnl_change(t, &p1, 1);
1209 ip6gre_tunnel_link(ign, t);
1210 netdev_state_change(dev);
1211 }
1212 }
1213
1214 if (t) {
1215 err = 0;
1216
Amerigo Wang5dbd5062013-05-09 21:56:37 +00001217 memset(&p, 0, sizeof(p));
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001218 ip6gre_tnl_parm_to_user(&p, &t->parms);
1219 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
1220 err = -EFAULT;
1221 } else
1222 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
1223 break;
1224
1225 case SIOCDELTUNNEL:
1226 err = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00001227 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001228 goto done;
1229
1230 if (dev == ign->fb_tunnel_dev) {
1231 err = -EFAULT;
1232 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1233 goto done;
1234 err = -ENOENT;
1235 ip6gre_tnl_parm_from_user(&p1, &p);
1236 t = ip6gre_tunnel_locate(net, &p1, 0);
Ian Morris63159f22015-03-29 14:00:04 +01001237 if (!t)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001238 goto done;
1239 err = -EPERM;
1240 if (t == netdev_priv(ign->fb_tunnel_dev))
1241 goto done;
1242 dev = t->dev;
1243 }
1244 unregister_netdevice(dev);
1245 err = 0;
1246 break;
1247
1248 default:
1249 err = -EINVAL;
1250 }
1251
1252done:
1253 return err;
1254}
1255
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001256static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
Xin Long76cc0d32017-09-15 12:00:07 +08001257 unsigned short type, const void *daddr,
1258 const void *saddr, unsigned int len)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001259{
1260 struct ip6_tnl *t = netdev_priv(dev);
Xin Long76cc0d32017-09-15 12:00:07 +08001261 struct ipv6hdr *ipv6h;
1262 __be16 *p;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001263
Xin Long76cc0d32017-09-15 12:00:07 +08001264 ipv6h = skb_push(skb, t->hlen + sizeof(*ipv6h));
1265 ip6_flow_hdr(ipv6h, 0, ip6_make_flowlabel(dev_net(dev), skb,
1266 t->fl.u.ip6.flowlabel,
1267 true, &t->fl.u.ip6));
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001268 ipv6h->hop_limit = t->parms.hop_limit;
1269 ipv6h->nexthdr = NEXTHDR_GRE;
1270 ipv6h->saddr = t->parms.laddr;
1271 ipv6h->daddr = t->parms.raddr;
1272
Xin Long76cc0d32017-09-15 12:00:07 +08001273 p = (__be16 *)(ipv6h + 1);
1274 p[0] = t->parms.o_flags;
1275 p[1] = htons(type);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001276
1277 /*
1278 * Set the source hardware address.
1279 */
1280
1281 if (saddr)
1282 memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr));
1283 if (daddr)
1284 memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
1285 if (!ipv6_addr_any(&ipv6h->daddr))
1286 return t->hlen;
1287
1288 return -t->hlen;
1289}
1290
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001291static const struct header_ops ip6gre_header_ops = {
1292 .create = ip6gre_header,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001293};
1294
1295static const struct net_device_ops ip6gre_netdev_ops = {
1296 .ndo_init = ip6gre_tunnel_init,
1297 .ndo_uninit = ip6gre_tunnel_uninit,
1298 .ndo_start_xmit = ip6gre_tunnel_xmit,
1299 .ndo_do_ioctl = ip6gre_tunnel_ioctl,
Tom Herbertb05229f2016-04-29 17:12:21 -07001300 .ndo_change_mtu = ip6_tnl_change_mtu,
Pravin B Shelarf61dd382013-03-25 14:50:00 +00001301 .ndo_get_stats64 = ip_tunnel_get_stats64,
Nicolas Dichtelecf2c062015-04-02 17:07:01 +02001302 .ndo_get_iflink = ip6_tnl_get_iflink,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001303};
1304
1305static void ip6gre_dev_free(struct net_device *dev)
1306{
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001307 struct ip6_tnl *t = netdev_priv(dev);
1308
Paolo Abeni607f7252016-02-12 15:43:54 +01001309 dst_cache_destroy(&t->dst_cache);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001310 free_percpu(dev->tstats);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001311}
1312
1313static void ip6gre_tunnel_setup(struct net_device *dev)
1314{
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001315 dev->netdev_ops = &ip6gre_netdev_ops;
David S. Millercf124db2017-05-08 12:52:56 -04001316 dev->needs_free_netdev = true;
1317 dev->priv_destructor = ip6gre_dev_free;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001318
1319 dev->type = ARPHRD_IP6GRE;
Tom Herbertb05229f2016-04-29 17:12:21 -07001320
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001321 dev->flags |= IFF_NOARP;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001322 dev->addr_len = sizeof(struct in6_addr);
Eric Dumazet02875872014-10-05 18:38:35 -07001323 netif_keep_dst(dev);
Felix Jia45ce0fd2017-01-26 16:59:18 +13001324 /* This perm addr will be used as interface identifier by IPv6 */
1325 dev->addr_assign_type = NET_ADDR_RANDOM;
1326 eth_random_addr(dev->perm_addr);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001327}
1328
Alexey Kodaneve5a93362017-12-20 19:36:03 +03001329#define GRE6_FEATURES (NETIF_F_SG | \
1330 NETIF_F_FRAGLIST | \
1331 NETIF_F_HIGHDMA | \
1332 NETIF_F_HW_CSUM)
1333
1334static void ip6gre_tnl_init_features(struct net_device *dev)
1335{
1336 struct ip6_tnl *nt = netdev_priv(dev);
1337
1338 dev->features |= GRE6_FEATURES;
1339 dev->hw_features |= GRE6_FEATURES;
1340
1341 if (!(nt->parms.o_flags & TUNNEL_SEQ)) {
1342 /* TCP offload with GRE SEQ is not supported, nor
1343 * can we support 2 levels of outer headers requiring
1344 * an update.
1345 */
1346 if (!(nt->parms.o_flags & TUNNEL_CSUM) ||
1347 nt->encap.type == TUNNEL_ENCAP_NONE) {
1348 dev->features |= NETIF_F_GSO_SOFTWARE;
1349 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
1350 }
1351
1352 /* Can use a lockless transmit, unless we generate
1353 * output sequences
1354 */
1355 dev->features |= NETIF_F_LLTX;
1356 }
1357}
1358
Martin KaFai Laua3c119d2015-09-15 14:30:05 -07001359static int ip6gre_tunnel_init_common(struct net_device *dev)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001360{
1361 struct ip6_tnl *tunnel;
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001362 int ret;
Tom Herbertb05229f2016-04-29 17:12:21 -07001363 int t_hlen;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001364
1365 tunnel = netdev_priv(dev);
1366
1367 tunnel->dev = dev;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001368 tunnel->net = dev_net(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001369 strcpy(tunnel->parms.name, dev->name);
1370
Martin KaFai Laua3c119d2015-09-15 14:30:05 -07001371 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1372 if (!dev->tstats)
1373 return -ENOMEM;
1374
Paolo Abeni607f7252016-02-12 15:43:54 +01001375 ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001376 if (ret) {
1377 free_percpu(dev->tstats);
1378 dev->tstats = NULL;
1379 return ret;
1380 }
1381
Tom Herbertb05229f2016-04-29 17:12:21 -07001382 tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
Tom Herbert1faf3d92016-05-18 09:06:19 -07001383 tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen;
Tom Herbertb05229f2016-04-29 17:12:21 -07001384 t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
1385
Tom Herbertdb2ec952016-05-09 17:12:08 -07001386 dev->hard_header_len = LL_MAX_HEADER + t_hlen;
1387 dev->mtu = ETH_DATA_LEN - t_hlen;
Haishuang Yan1b227e52016-05-21 18:17:34 +08001388 if (dev->type == ARPHRD_ETHER)
1389 dev->mtu -= ETH_HLEN;
Tom Herbertb05229f2016-04-29 17:12:21 -07001390 if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1391 dev->mtu -= 8;
1392
William Tu6712abc2017-12-01 15:26:08 -08001393 if (tunnel->parms.collect_md) {
1394 dev->features |= NETIF_F_NETNS_LOCAL;
1395 netif_keep_dst(dev);
1396 }
Alexey Kodaneve5a93362017-12-20 19:36:03 +03001397 ip6gre_tnl_init_features(dev);
William Tu6712abc2017-12-01 15:26:08 -08001398
Martin KaFai Laua3c119d2015-09-15 14:30:05 -07001399 return 0;
1400}
1401
1402static int ip6gre_tunnel_init(struct net_device *dev)
1403{
1404 struct ip6_tnl *tunnel;
1405 int ret;
1406
1407 ret = ip6gre_tunnel_init_common(dev);
1408 if (ret)
1409 return ret;
1410
1411 tunnel = netdev_priv(dev);
1412
William Tu6712abc2017-12-01 15:26:08 -08001413 if (tunnel->parms.collect_md)
1414 return 0;
1415
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001416 memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr));
1417 memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr));
1418
1419 if (ipv6_addr_any(&tunnel->parms.raddr))
1420 dev->header_ops = &ip6gre_header_ops;
1421
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001422 return 0;
1423}
1424
1425static void ip6gre_fb_tunnel_init(struct net_device *dev)
1426{
1427 struct ip6_tnl *tunnel = netdev_priv(dev);
1428
1429 tunnel->dev = dev;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001430 tunnel->net = dev_net(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001431 strcpy(tunnel->parms.name, dev->name);
1432
1433 tunnel->hlen = sizeof(struct ipv6hdr) + 4;
1434
1435 dev_hold(dev);
1436}
1437
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001438static struct inet6_protocol ip6gre_protocol __read_mostly = {
Tom Herbert308edfdf2016-04-29 17:12:17 -07001439 .handler = gre_rcv,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001440 .err_handler = ip6gre_err,
1441 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1442};
1443
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001444static void ip6gre_destroy_tunnels(struct net *net, struct list_head *head)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001445{
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001446 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1447 struct net_device *dev, *aux;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001448 int prio;
1449
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001450 for_each_netdev_safe(net, dev, aux)
1451 if (dev->rtnl_link_ops == &ip6gre_link_ops ||
William Tu5a963eb2017-11-30 11:51:29 -08001452 dev->rtnl_link_ops == &ip6gre_tap_ops ||
1453 dev->rtnl_link_ops == &ip6erspan_tap_ops)
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001454 unregister_netdevice_queue(dev, head);
1455
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001456 for (prio = 0; prio < 4; prio++) {
1457 int h;
Jiri Kosinae87a8f22016-08-10 11:03:35 +02001458 for (h = 0; h < IP6_GRE_HASH_SIZE; h++) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001459 struct ip6_tnl *t;
1460
1461 t = rtnl_dereference(ign->tunnels[prio][h]);
1462
Ian Morris53b24b82015-03-29 14:00:05 +01001463 while (t) {
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001464 /* If dev is in the same netns, it has already
1465 * been added to the list by the previous loop.
1466 */
1467 if (!net_eq(dev_net(t->dev), net))
1468 unregister_netdevice_queue(t->dev,
1469 head);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001470 t = rtnl_dereference(t->next);
1471 }
1472 }
1473 }
1474}
1475
1476static int __net_init ip6gre_init_net(struct net *net)
1477{
1478 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1479 int err;
1480
1481 ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0",
Tom Gundersenc835a672014-07-14 16:37:24 +02001482 NET_NAME_UNKNOWN,
1483 ip6gre_tunnel_setup);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001484 if (!ign->fb_tunnel_dev) {
1485 err = -ENOMEM;
1486 goto err_alloc_dev;
1487 }
1488 dev_net_set(ign->fb_tunnel_dev, net);
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001489 /* FB netdevice is special: we have one, and only one per netns.
1490 * Allowing to move it to another netns is clearly unsafe.
1491 */
1492 ign->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL;
1493
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001494
1495 ip6gre_fb_tunnel_init(ign->fb_tunnel_dev);
1496 ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops;
1497
1498 err = register_netdev(ign->fb_tunnel_dev);
1499 if (err)
1500 goto err_reg_dev;
1501
1502 rcu_assign_pointer(ign->tunnels_wc[0],
1503 netdev_priv(ign->fb_tunnel_dev));
1504 return 0;
1505
1506err_reg_dev:
David S. Millercf124db2017-05-08 12:52:56 -04001507 free_netdev(ign->fb_tunnel_dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001508err_alloc_dev:
1509 return err;
1510}
1511
Eric Dumazetbb401ca2017-09-19 16:27:08 -07001512static void __net_exit ip6gre_exit_batch_net(struct list_head *net_list)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001513{
Eric Dumazetbb401ca2017-09-19 16:27:08 -07001514 struct net *net;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001515 LIST_HEAD(list);
1516
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001517 rtnl_lock();
Eric Dumazetbb401ca2017-09-19 16:27:08 -07001518 list_for_each_entry(net, net_list, exit_list)
1519 ip6gre_destroy_tunnels(net, &list);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001520 unregister_netdevice_many(&list);
1521 rtnl_unlock();
1522}
1523
1524static struct pernet_operations ip6gre_net_ops = {
1525 .init = ip6gre_init_net,
Eric Dumazetbb401ca2017-09-19 16:27:08 -07001526 .exit_batch = ip6gre_exit_batch_net,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001527 .id = &ip6gre_net_id,
1528 .size = sizeof(struct ip6gre_net),
1529};
1530
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001531static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[],
1532 struct netlink_ext_ack *extack)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001533{
1534 __be16 flags;
1535
1536 if (!data)
1537 return 0;
1538
1539 flags = 0;
1540 if (data[IFLA_GRE_IFLAGS])
1541 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1542 if (data[IFLA_GRE_OFLAGS])
1543 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1544 if (flags & (GRE_VERSION|GRE_ROUTING))
1545 return -EINVAL;
1546
1547 return 0;
1548}
1549
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001550static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[],
1551 struct netlink_ext_ack *extack)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001552{
1553 struct in6_addr daddr;
1554
1555 if (tb[IFLA_ADDRESS]) {
1556 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1557 return -EINVAL;
1558 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1559 return -EADDRNOTAVAIL;
1560 }
1561
1562 if (!data)
1563 goto out;
1564
1565 if (data[IFLA_GRE_REMOTE]) {
Jiri Benc67b61f62015-03-29 16:59:26 +02001566 daddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001567 if (ipv6_addr_any(&daddr))
1568 return -EINVAL;
1569 }
1570
1571out:
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001572 return ip6gre_tunnel_validate(tb, data, extack);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001573}
1574
William Tu5a963eb2017-11-30 11:51:29 -08001575static int ip6erspan_tap_validate(struct nlattr *tb[], struct nlattr *data[],
1576 struct netlink_ext_ack *extack)
1577{
1578 __be16 flags = 0;
William Tu94d7d8f2017-12-13 16:38:57 -08001579 int ret, ver = 0;
William Tu5a963eb2017-11-30 11:51:29 -08001580
1581 if (!data)
1582 return 0;
1583
1584 ret = ip6gre_tap_validate(tb, data, extack);
1585 if (ret)
1586 return ret;
1587
1588 /* ERSPAN should only have GRE sequence and key flag */
1589 if (data[IFLA_GRE_OFLAGS])
1590 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1591 if (data[IFLA_GRE_IFLAGS])
1592 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1593 if (!data[IFLA_GRE_COLLECT_METADATA] &&
1594 flags != (GRE_SEQ | GRE_KEY))
1595 return -EINVAL;
1596
1597 /* ERSPAN Session ID only has 10-bit. Since we reuse
1598 * 32-bit key field as ID, check it's range.
1599 */
1600 if (data[IFLA_GRE_IKEY] &&
1601 (ntohl(nla_get_be32(data[IFLA_GRE_IKEY])) & ~ID_MASK))
1602 return -EINVAL;
1603
1604 if (data[IFLA_GRE_OKEY] &&
1605 (ntohl(nla_get_be32(data[IFLA_GRE_OKEY])) & ~ID_MASK))
1606 return -EINVAL;
1607
William Tu94d7d8f2017-12-13 16:38:57 -08001608 if (data[IFLA_GRE_ERSPAN_VER]) {
1609 ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]);
1610 if (ver != 1 && ver != 2)
William Tu5a963eb2017-11-30 11:51:29 -08001611 return -EINVAL;
1612 }
William Tu94d7d8f2017-12-13 16:38:57 -08001613
1614 if (ver == 1) {
1615 if (data[IFLA_GRE_ERSPAN_INDEX]) {
1616 u32 index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
1617
1618 if (index & ~INDEX_MASK)
1619 return -EINVAL;
1620 }
1621 } else if (ver == 2) {
1622 if (data[IFLA_GRE_ERSPAN_DIR]) {
1623 u16 dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]);
1624
1625 if (dir & ~(DIR_MASK >> DIR_OFFSET))
1626 return -EINVAL;
1627 }
1628
1629 if (data[IFLA_GRE_ERSPAN_HWID]) {
1630 u16 hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]);
1631
1632 if (hwid & ~(HWID_MASK >> HWID_OFFSET))
1633 return -EINVAL;
1634 }
1635 }
1636
William Tu5a963eb2017-11-30 11:51:29 -08001637 return 0;
1638}
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001639
1640static void ip6gre_netlink_parms(struct nlattr *data[],
1641 struct __ip6_tnl_parm *parms)
1642{
1643 memset(parms, 0, sizeof(*parms));
1644
1645 if (!data)
1646 return;
1647
1648 if (data[IFLA_GRE_LINK])
1649 parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
1650
1651 if (data[IFLA_GRE_IFLAGS])
Tom Herbertf41fe3c2016-05-09 17:12:09 -07001652 parms->i_flags = gre_flags_to_tnl_flags(
1653 nla_get_be16(data[IFLA_GRE_IFLAGS]));
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001654
1655 if (data[IFLA_GRE_OFLAGS])
Tom Herbertf41fe3c2016-05-09 17:12:09 -07001656 parms->o_flags = gre_flags_to_tnl_flags(
1657 nla_get_be16(data[IFLA_GRE_OFLAGS]));
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001658
1659 if (data[IFLA_GRE_IKEY])
1660 parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
1661
1662 if (data[IFLA_GRE_OKEY])
1663 parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
1664
1665 if (data[IFLA_GRE_LOCAL])
Jiri Benc67b61f62015-03-29 16:59:26 +02001666 parms->laddr = nla_get_in6_addr(data[IFLA_GRE_LOCAL]);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001667
1668 if (data[IFLA_GRE_REMOTE])
Jiri Benc67b61f62015-03-29 16:59:26 +02001669 parms->raddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001670
1671 if (data[IFLA_GRE_TTL])
1672 parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]);
1673
1674 if (data[IFLA_GRE_ENCAP_LIMIT])
1675 parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]);
1676
1677 if (data[IFLA_GRE_FLOWINFO])
Lance Richardsonc2675de2016-09-24 14:01:04 -04001678 parms->flowinfo = nla_get_be32(data[IFLA_GRE_FLOWINFO]);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001679
1680 if (data[IFLA_GRE_FLAGS])
1681 parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]);
Craig Gallek0a473b82017-04-19 12:30:53 -04001682
1683 if (data[IFLA_GRE_FWMARK])
1684 parms->fwmark = nla_get_u32(data[IFLA_GRE_FWMARK]);
William Tu5a963eb2017-11-30 11:51:29 -08001685
William Tu6712abc2017-12-01 15:26:08 -08001686 if (data[IFLA_GRE_COLLECT_METADATA])
1687 parms->collect_md = true;
William Tu94d7d8f2017-12-13 16:38:57 -08001688
1689 if (data[IFLA_GRE_ERSPAN_VER])
1690 parms->erspan_ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]);
1691
1692 if (parms->erspan_ver == 1) {
1693 if (data[IFLA_GRE_ERSPAN_INDEX])
1694 parms->index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
1695 } else if (parms->erspan_ver == 2) {
1696 if (data[IFLA_GRE_ERSPAN_DIR])
1697 parms->dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]);
1698 if (data[IFLA_GRE_ERSPAN_HWID])
1699 parms->hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]);
1700 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001701}
1702
1703static int ip6gre_tap_init(struct net_device *dev)
1704{
Martin KaFai Laua3c119d2015-09-15 14:30:05 -07001705 int ret;
1706
1707 ret = ip6gre_tunnel_init_common(dev);
1708 if (ret)
1709 return ret;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001710
Shweta Choudaha0a46baa2016-06-08 20:15:43 +01001711 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1712
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001713 return 0;
1714}
1715
1716static const struct net_device_ops ip6gre_tap_netdev_ops = {
1717 .ndo_init = ip6gre_tap_init,
1718 .ndo_uninit = ip6gre_tunnel_uninit,
1719 .ndo_start_xmit = ip6gre_tunnel_xmit,
1720 .ndo_set_mac_address = eth_mac_addr,
1721 .ndo_validate_addr = eth_validate_addr,
Tom Herbertb05229f2016-04-29 17:12:21 -07001722 .ndo_change_mtu = ip6_tnl_change_mtu,
Pravin B Shelarf61dd382013-03-25 14:50:00 +00001723 .ndo_get_stats64 = ip_tunnel_get_stats64,
Nicolas Dichtelecf2c062015-04-02 17:07:01 +02001724 .ndo_get_iflink = ip6_tnl_get_iflink,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001725};
1726
William Tu5a963eb2017-11-30 11:51:29 -08001727static int ip6erspan_tap_init(struct net_device *dev)
1728{
1729 struct ip6_tnl *tunnel;
1730 int t_hlen;
1731 int ret;
1732
1733 tunnel = netdev_priv(dev);
1734
1735 tunnel->dev = dev;
1736 tunnel->net = dev_net(dev);
1737 strcpy(tunnel->parms.name, dev->name);
1738
1739 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1740 if (!dev->tstats)
1741 return -ENOMEM;
1742
1743 ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
1744 if (ret) {
1745 free_percpu(dev->tstats);
1746 dev->tstats = NULL;
1747 return ret;
1748 }
1749
1750 tunnel->tun_hlen = 8;
1751 tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen +
William Tu94d7d8f2017-12-13 16:38:57 -08001752 erspan_hdr_len(tunnel->parms.erspan_ver);
William Tu5a963eb2017-11-30 11:51:29 -08001753 t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
1754
1755 dev->hard_header_len = LL_MAX_HEADER + t_hlen;
1756 dev->mtu = ETH_DATA_LEN - t_hlen;
1757 if (dev->type == ARPHRD_ETHER)
1758 dev->mtu -= ETH_HLEN;
1759 if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1760 dev->mtu -= 8;
1761
1762 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1763 tunnel = netdev_priv(dev);
1764 ip6gre_tnl_link_config(tunnel, 1);
1765
1766 return 0;
1767}
1768
1769static const struct net_device_ops ip6erspan_netdev_ops = {
1770 .ndo_init = ip6erspan_tap_init,
1771 .ndo_uninit = ip6gre_tunnel_uninit,
1772 .ndo_start_xmit = ip6erspan_tunnel_xmit,
1773 .ndo_set_mac_address = eth_mac_addr,
1774 .ndo_validate_addr = eth_validate_addr,
1775 .ndo_change_mtu = ip6_tnl_change_mtu,
1776 .ndo_get_stats64 = ip_tunnel_get_stats64,
1777 .ndo_get_iflink = ip6_tnl_get_iflink,
1778};
1779
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001780static void ip6gre_tap_setup(struct net_device *dev)
1781{
1782
1783 ether_setup(dev);
1784
Xin Long2c521292017-12-18 14:25:09 +08001785 dev->max_mtu = 0;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001786 dev->netdev_ops = &ip6gre_tap_netdev_ops;
David S. Millercf124db2017-05-08 12:52:56 -04001787 dev->needs_free_netdev = true;
1788 dev->priv_destructor = ip6gre_dev_free;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001789
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001790 dev->features |= NETIF_F_NETNS_LOCAL;
Jiri Bencd13b1612016-02-17 15:32:53 +01001791 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
Shweta Choudaha0a46baa2016-06-08 20:15:43 +01001792 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Xin Long2d405572017-09-28 13:23:50 +08001793 netif_keep_dst(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001794}
1795
Tom Herbert1faf3d92016-05-18 09:06:19 -07001796static bool ip6gre_netlink_encap_parms(struct nlattr *data[],
1797 struct ip_tunnel_encap *ipencap)
1798{
1799 bool ret = false;
1800
1801 memset(ipencap, 0, sizeof(*ipencap));
1802
1803 if (!data)
1804 return ret;
1805
1806 if (data[IFLA_GRE_ENCAP_TYPE]) {
1807 ret = true;
1808 ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]);
1809 }
1810
1811 if (data[IFLA_GRE_ENCAP_FLAGS]) {
1812 ret = true;
1813 ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]);
1814 }
1815
1816 if (data[IFLA_GRE_ENCAP_SPORT]) {
1817 ret = true;
1818 ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]);
1819 }
1820
1821 if (data[IFLA_GRE_ENCAP_DPORT]) {
1822 ret = true;
1823 ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]);
1824 }
1825
1826 return ret;
1827}
1828
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001829static int ip6gre_newlink(struct net *src_net, struct net_device *dev,
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02001830 struct nlattr *tb[], struct nlattr *data[],
1831 struct netlink_ext_ack *extack)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001832{
1833 struct ip6_tnl *nt;
1834 struct net *net = dev_net(dev);
1835 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
Tom Herbert1faf3d92016-05-18 09:06:19 -07001836 struct ip_tunnel_encap ipencap;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001837 int err;
1838
1839 nt = netdev_priv(dev);
Tom Herbert1faf3d92016-05-18 09:06:19 -07001840
1841 if (ip6gre_netlink_encap_parms(data, &ipencap)) {
1842 int err = ip6_tnl_encap_setup(nt, &ipencap);
1843
1844 if (err < 0)
1845 return err;
1846 }
1847
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001848 ip6gre_netlink_parms(data, &nt->parms);
1849
William Tu6712abc2017-12-01 15:26:08 -08001850 if (nt->parms.collect_md) {
1851 if (rtnl_dereference(ign->collect_md_tun))
1852 return -EEXIST;
1853 } else {
1854 if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
1855 return -EEXIST;
1856 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001857
1858 if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
1859 eth_hw_addr_random(dev);
1860
1861 nt->dev = dev;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001862 nt->net = dev_net(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001863
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001864 err = register_netdevice(dev);
1865 if (err)
1866 goto out;
1867
Alexey Kodanev128bb972018-01-18 20:51:12 +03001868 ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]);
1869
1870 if (tb[IFLA_MTU])
1871 ip6_tnl_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1872
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001873 dev_hold(dev);
1874 ip6gre_tunnel_link(ign, nt);
1875
1876out:
1877 return err;
1878}
1879
1880static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],
Matthias Schifferad744b22017-06-25 23:56:00 +02001881 struct nlattr *data[],
1882 struct netlink_ext_ack *extack)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001883{
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001884 struct ip6_tnl *t, *nt = netdev_priv(dev);
1885 struct net *net = nt->net;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001886 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1887 struct __ip6_tnl_parm p;
Tom Herbert1faf3d92016-05-18 09:06:19 -07001888 struct ip_tunnel_encap ipencap;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001889
1890 if (dev == ign->fb_tunnel_dev)
1891 return -EINVAL;
1892
Tom Herbert1faf3d92016-05-18 09:06:19 -07001893 if (ip6gre_netlink_encap_parms(data, &ipencap)) {
1894 int err = ip6_tnl_encap_setup(nt, &ipencap);
1895
1896 if (err < 0)
1897 return err;
1898 }
1899
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001900 ip6gre_netlink_parms(data, &p);
1901
1902 t = ip6gre_tunnel_locate(net, &p, 0);
1903
1904 if (t) {
1905 if (t->dev != dev)
1906 return -EEXIST;
1907 } else {
1908 t = nt;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001909 }
1910
Nicolas Dichtel6a61d4d2015-12-03 17:21:50 +01001911 ip6gre_tunnel_unlink(ign, t);
1912 ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]);
1913 ip6gre_tunnel_link(ign, t);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001914 return 0;
1915}
1916
Nicolas Dichtel54d63f782014-04-14 17:11:38 +02001917static void ip6gre_dellink(struct net_device *dev, struct list_head *head)
1918{
1919 struct net *net = dev_net(dev);
1920 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1921
1922 if (dev != ign->fb_tunnel_dev)
1923 unregister_netdevice_queue(dev, head);
1924}
1925
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001926static size_t ip6gre_get_size(const struct net_device *dev)
1927{
1928 return
1929 /* IFLA_GRE_LINK */
1930 nla_total_size(4) +
1931 /* IFLA_GRE_IFLAGS */
1932 nla_total_size(2) +
1933 /* IFLA_GRE_OFLAGS */
1934 nla_total_size(2) +
1935 /* IFLA_GRE_IKEY */
1936 nla_total_size(4) +
1937 /* IFLA_GRE_OKEY */
1938 nla_total_size(4) +
1939 /* IFLA_GRE_LOCAL */
Nicolas Dichtela3754132012-11-09 05:34:56 +00001940 nla_total_size(sizeof(struct in6_addr)) +
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001941 /* IFLA_GRE_REMOTE */
Nicolas Dichtela3754132012-11-09 05:34:56 +00001942 nla_total_size(sizeof(struct in6_addr)) +
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001943 /* IFLA_GRE_TTL */
1944 nla_total_size(1) +
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001945 /* IFLA_GRE_ENCAP_LIMIT */
1946 nla_total_size(1) +
1947 /* IFLA_GRE_FLOWINFO */
1948 nla_total_size(4) +
1949 /* IFLA_GRE_FLAGS */
1950 nla_total_size(4) +
Tom Herbert1faf3d92016-05-18 09:06:19 -07001951 /* IFLA_GRE_ENCAP_TYPE */
1952 nla_total_size(2) +
1953 /* IFLA_GRE_ENCAP_FLAGS */
1954 nla_total_size(2) +
1955 /* IFLA_GRE_ENCAP_SPORT */
1956 nla_total_size(2) +
1957 /* IFLA_GRE_ENCAP_DPORT */
1958 nla_total_size(2) +
William Tu6712abc2017-12-01 15:26:08 -08001959 /* IFLA_GRE_COLLECT_METADATA */
1960 nla_total_size(0) +
Craig Gallek0a473b82017-04-19 12:30:53 -04001961 /* IFLA_GRE_FWMARK */
1962 nla_total_size(4) +
William Tu5a963eb2017-11-30 11:51:29 -08001963 /* IFLA_GRE_ERSPAN_INDEX */
1964 nla_total_size(4) +
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001965 0;
1966}
1967
1968static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev)
1969{
1970 struct ip6_tnl *t = netdev_priv(dev);
1971 struct __ip6_tnl_parm *p = &t->parms;
1972
1973 if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
Tom Herbertf41fe3c2016-05-09 17:12:09 -07001974 nla_put_be16(skb, IFLA_GRE_IFLAGS,
1975 gre_tnl_flags_to_gre_flags(p->i_flags)) ||
1976 nla_put_be16(skb, IFLA_GRE_OFLAGS,
1977 gre_tnl_flags_to_gre_flags(p->o_flags)) ||
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001978 nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
1979 nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
Jiri Benc930345e2015-03-29 16:59:25 +02001980 nla_put_in6_addr(skb, IFLA_GRE_LOCAL, &p->laddr) ||
1981 nla_put_in6_addr(skb, IFLA_GRE_REMOTE, &p->raddr) ||
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001982 nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) ||
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001983 nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) ||
1984 nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) ||
Craig Gallek0a473b82017-04-19 12:30:53 -04001985 nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags) ||
William Tu5a963eb2017-11-30 11:51:29 -08001986 nla_put_u32(skb, IFLA_GRE_FWMARK, p->fwmark) ||
1987 nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, p->index))
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001988 goto nla_put_failure;
Tom Herbert1faf3d92016-05-18 09:06:19 -07001989
1990 if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE,
1991 t->encap.type) ||
1992 nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT,
1993 t->encap.sport) ||
1994 nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT,
1995 t->encap.dport) ||
1996 nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS,
1997 t->encap.flags))
1998 goto nla_put_failure;
1999
William Tu6712abc2017-12-01 15:26:08 -08002000 if (p->collect_md) {
2001 if (nla_put_flag(skb, IFLA_GRE_COLLECT_METADATA))
2002 goto nla_put_failure;
2003 }
2004
William Tu94d7d8f2017-12-13 16:38:57 -08002005 if (nla_put_u8(skb, IFLA_GRE_ERSPAN_VER, p->erspan_ver))
2006 goto nla_put_failure;
2007
2008 if (p->erspan_ver == 1) {
2009 if (nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, p->index))
2010 goto nla_put_failure;
2011 } else if (p->erspan_ver == 2) {
2012 if (nla_put_u8(skb, IFLA_GRE_ERSPAN_DIR, p->dir))
2013 goto nla_put_failure;
2014 if (nla_put_u16(skb, IFLA_GRE_ERSPAN_HWID, p->hwid))
2015 goto nla_put_failure;
2016 }
2017
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002018 return 0;
2019
2020nla_put_failure:
2021 return -EMSGSIZE;
2022}
2023
2024static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = {
2025 [IFLA_GRE_LINK] = { .type = NLA_U32 },
2026 [IFLA_GRE_IFLAGS] = { .type = NLA_U16 },
2027 [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
2028 [IFLA_GRE_IKEY] = { .type = NLA_U32 },
2029 [IFLA_GRE_OKEY] = { .type = NLA_U32 },
2030 [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct ipv6hdr, saddr) },
2031 [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct ipv6hdr, daddr) },
2032 [IFLA_GRE_TTL] = { .type = NLA_U8 },
2033 [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 },
2034 [IFLA_GRE_FLOWINFO] = { .type = NLA_U32 },
2035 [IFLA_GRE_FLAGS] = { .type = NLA_U32 },
Tom Herbert1faf3d92016-05-18 09:06:19 -07002036 [IFLA_GRE_ENCAP_TYPE] = { .type = NLA_U16 },
2037 [IFLA_GRE_ENCAP_FLAGS] = { .type = NLA_U16 },
2038 [IFLA_GRE_ENCAP_SPORT] = { .type = NLA_U16 },
2039 [IFLA_GRE_ENCAP_DPORT] = { .type = NLA_U16 },
William Tu6712abc2017-12-01 15:26:08 -08002040 [IFLA_GRE_COLLECT_METADATA] = { .type = NLA_FLAG },
Craig Gallek0a473b82017-04-19 12:30:53 -04002041 [IFLA_GRE_FWMARK] = { .type = NLA_U32 },
William Tu5a963eb2017-11-30 11:51:29 -08002042 [IFLA_GRE_ERSPAN_INDEX] = { .type = NLA_U32 },
William Tu94d7d8f2017-12-13 16:38:57 -08002043 [IFLA_GRE_ERSPAN_VER] = { .type = NLA_U8 },
2044 [IFLA_GRE_ERSPAN_DIR] = { .type = NLA_U8 },
2045 [IFLA_GRE_ERSPAN_HWID] = { .type = NLA_U16 },
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002046};
2047
William Tu5a963eb2017-11-30 11:51:29 -08002048static void ip6erspan_tap_setup(struct net_device *dev)
2049{
2050 ether_setup(dev);
2051
2052 dev->netdev_ops = &ip6erspan_netdev_ops;
2053 dev->needs_free_netdev = true;
2054 dev->priv_destructor = ip6gre_dev_free;
2055
2056 dev->features |= NETIF_F_NETNS_LOCAL;
2057 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
2058 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2059 netif_keep_dst(dev);
2060}
2061
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002062static struct rtnl_link_ops ip6gre_link_ops __read_mostly = {
2063 .kind = "ip6gre",
2064 .maxtype = IFLA_GRE_MAX,
2065 .policy = ip6gre_policy,
2066 .priv_size = sizeof(struct ip6_tnl),
2067 .setup = ip6gre_tunnel_setup,
2068 .validate = ip6gre_tunnel_validate,
2069 .newlink = ip6gre_newlink,
2070 .changelink = ip6gre_changelink,
Nicolas Dichtel54d63f782014-04-14 17:11:38 +02002071 .dellink = ip6gre_dellink,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002072 .get_size = ip6gre_get_size,
2073 .fill_info = ip6gre_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01002074 .get_link_net = ip6_tnl_get_link_net,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002075};
2076
2077static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = {
2078 .kind = "ip6gretap",
2079 .maxtype = IFLA_GRE_MAX,
2080 .policy = ip6gre_policy,
2081 .priv_size = sizeof(struct ip6_tnl),
2082 .setup = ip6gre_tap_setup,
2083 .validate = ip6gre_tap_validate,
2084 .newlink = ip6gre_newlink,
2085 .changelink = ip6gre_changelink,
2086 .get_size = ip6gre_get_size,
2087 .fill_info = ip6gre_fill_info,
Nicolas Dichtel3390e392015-01-20 15:15:43 +01002088 .get_link_net = ip6_tnl_get_link_net,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002089};
2090
William Tu5a963eb2017-11-30 11:51:29 -08002091static struct rtnl_link_ops ip6erspan_tap_ops __read_mostly = {
2092 .kind = "ip6erspan",
2093 .maxtype = IFLA_GRE_MAX,
2094 .policy = ip6gre_policy,
2095 .priv_size = sizeof(struct ip6_tnl),
2096 .setup = ip6erspan_tap_setup,
2097 .validate = ip6erspan_tap_validate,
2098 .newlink = ip6gre_newlink,
2099 .changelink = ip6gre_changelink,
2100 .get_size = ip6gre_get_size,
2101 .fill_info = ip6gre_fill_info,
2102 .get_link_net = ip6_tnl_get_link_net,
2103};
2104
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002105/*
2106 * And now the modules code and kernel interface.
2107 */
2108
2109static int __init ip6gre_init(void)
2110{
2111 int err;
2112
2113 pr_info("GRE over IPv6 tunneling driver\n");
2114
2115 err = register_pernet_device(&ip6gre_net_ops);
2116 if (err < 0)
2117 return err;
2118
2119 err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE);
2120 if (err < 0) {
2121 pr_info("%s: can't add protocol\n", __func__);
2122 goto add_proto_failed;
2123 }
2124
2125 err = rtnl_link_register(&ip6gre_link_ops);
2126 if (err < 0)
2127 goto rtnl_link_failed;
2128
2129 err = rtnl_link_register(&ip6gre_tap_ops);
2130 if (err < 0)
2131 goto tap_ops_failed;
2132
William Tu5a963eb2017-11-30 11:51:29 -08002133 err = rtnl_link_register(&ip6erspan_tap_ops);
2134 if (err < 0)
2135 goto erspan_link_failed;
2136
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002137out:
2138 return err;
2139
William Tu5a963eb2017-11-30 11:51:29 -08002140erspan_link_failed:
2141 rtnl_link_unregister(&ip6gre_tap_ops);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002142tap_ops_failed:
2143 rtnl_link_unregister(&ip6gre_link_ops);
2144rtnl_link_failed:
2145 inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
2146add_proto_failed:
2147 unregister_pernet_device(&ip6gre_net_ops);
2148 goto out;
2149}
2150
2151static void __exit ip6gre_fini(void)
2152{
2153 rtnl_link_unregister(&ip6gre_tap_ops);
2154 rtnl_link_unregister(&ip6gre_link_ops);
William Tu5a963eb2017-11-30 11:51:29 -08002155 rtnl_link_unregister(&ip6erspan_tap_ops);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002156 inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
2157 unregister_pernet_device(&ip6gre_net_ops);
2158}
2159
2160module_init(ip6gre_init);
2161module_exit(ip6gre_fini);
2162MODULE_LICENSE("GPL");
2163MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
2164MODULE_DESCRIPTION("GRE over IPv6 tunneling device");
2165MODULE_ALIAS_RTNL_LINK("ip6gre");
Nicolas Dichtel5a4ee9a2014-09-24 11:03:00 +02002166MODULE_ALIAS_RTNL_LINK("ip6gretap");
William Tu94d7d8f2017-12-13 16:38:57 -08002167MODULE_ALIAS_RTNL_LINK("ip6erspan");
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002168MODULE_ALIAS_NETDEV("ip6gre0");