blob: b7a79ddcf8516bc725220f073127e9b9f58acfbc [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 ip6gre_tnl_link_config(nt, 1);
356
357 if (register_netdevice(dev) < 0)
358 goto failed_free;
359
360 /* Can use a lockless transmit, unless we generate output sequences */
Tom Herbertb45bd1d2016-05-09 17:12:12 -0700361 if (!(nt->parms.o_flags & TUNNEL_SEQ))
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000362 dev->features |= NETIF_F_LLTX;
363
364 dev_hold(dev);
365 ip6gre_tunnel_link(ign, nt);
366 return nt;
367
368failed_free:
369 free_netdev(dev);
370 return NULL;
371}
372
373static void ip6gre_tunnel_uninit(struct net_device *dev)
374{
Nicolas Dichtel22f08062014-04-22 10:15:24 +0200375 struct ip6_tnl *t = netdev_priv(dev);
376 struct ip6gre_net *ign = net_generic(t->net, ip6gre_net_id);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000377
Nicolas Dichtel22f08062014-04-22 10:15:24 +0200378 ip6gre_tunnel_unlink(ign, t);
Paolo Abeni607f7252016-02-12 15:43:54 +0100379 dst_cache_reset(&t->dst_cache);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000380 dev_put(dev);
381}
382
383
384static void ip6gre_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Eric Dumazet78920322017-02-04 23:18:55 -0800385 u8 type, u8 code, int offset, __be32 info)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000386{
Xin Long929fc032017-11-11 19:06:49 +0800387 struct net *net = dev_net(skb->dev);
Eric Dumazet78920322017-02-04 23:18:55 -0800388 const struct gre_base_hdr *greh;
389 const struct ipv6hdr *ipv6h;
390 int grehlen = sizeof(*greh);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000391 struct ip6_tnl *t;
Eric Dumazet78920322017-02-04 23:18:55 -0800392 int key_off = 0;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000393 __be16 flags;
Eric Dumazet78920322017-02-04 23:18:55 -0800394 __be32 key;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000395
Eric Dumazet78920322017-02-04 23:18:55 -0800396 if (!pskb_may_pull(skb, offset + grehlen))
397 return;
398 greh = (const struct gre_base_hdr *)(skb->data + offset);
399 flags = greh->flags;
400 if (flags & (GRE_VERSION | GRE_ROUTING))
401 return;
402 if (flags & GRE_CSUM)
403 grehlen += 4;
404 if (flags & GRE_KEY) {
405 key_off = grehlen + offset;
406 grehlen += 4;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000407 }
408
Eric Dumazet78920322017-02-04 23:18:55 -0800409 if (!pskb_may_pull(skb, offset + grehlen))
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000410 return;
Eric Dumazetb87fb392012-08-19 03:47:30 +0000411 ipv6h = (const struct ipv6hdr *)skb->data;
Eric Dumazet78920322017-02-04 23:18:55 -0800412 greh = (const struct gre_base_hdr *)(skb->data + offset);
413 key = key_off ? *(__be32 *)(skb->data + key_off) : 0;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000414
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000415 t = ip6gre_tunnel_lookup(skb->dev, &ipv6h->daddr, &ipv6h->saddr,
Eric Dumazet78920322017-02-04 23:18:55 -0800416 key, greh->protocol);
Ian Morris63159f22015-03-29 14:00:04 +0100417 if (!t)
stephen hemminger0c5794a2012-09-24 18:12:24 +0000418 return;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000419
420 switch (type) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000421 struct ipv6_tlv_tnl_enc_lim *tel;
Xin Longfe1a4ca2017-11-11 19:06:50 +0800422 __u32 teli;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000423 case ICMPV6_DEST_UNREACH:
Matt Bennetta46496c2015-09-23 16:58:31 +1200424 net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n",
425 t->parms.name);
Xin Longf8d20b42017-10-26 19:23:27 +0800426 if (code != ICMPV6_PORT_UNREACH)
427 break;
428 return;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000429 case ICMPV6_TIME_EXCEED:
430 if (code == ICMPV6_EXC_HOPLIMIT) {
Matt Bennetta46496c2015-09-23 16:58:31 +1200431 net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
432 t->parms.name);
Xin Longf8d20b42017-10-26 19:23:27 +0800433 break;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000434 }
Xin Longf8d20b42017-10-26 19:23:27 +0800435 return;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000436 case ICMPV6_PARAMPROB:
437 teli = 0;
438 if (code == ICMPV6_HDR_FIELD)
439 teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
440
Sabrina Dubrocad1e158e2015-02-04 15:25:09 +0100441 if (teli && teli == be32_to_cpu(info) - 2) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000442 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
443 if (tel->encap_limit == 0) {
Matt Bennetta46496c2015-09-23 16:58:31 +1200444 net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
445 t->parms.name);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000446 }
447 } else {
Matt Bennetta46496c2015-09-23 16:58:31 +1200448 net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
449 t->parms.name);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000450 }
Xin Longf8d20b42017-10-26 19:23:27 +0800451 return;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000452 case ICMPV6_PKT_TOOBIG:
Xin Longfe1a4ca2017-11-11 19:06:50 +0800453 ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
Xin Longf8d20b42017-10-26 19:23:27 +0800454 return;
Xin Long929fc032017-11-11 19:06:49 +0800455 case NDISC_REDIRECT:
456 ip6_redirect(skb, net, skb->dev->ifindex, 0,
457 sock_net_uid(net, NULL));
458 return;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000459 }
460
461 if (time_before(jiffies, t->err_time + IP6TUNNEL_ERR_TIMEO))
462 t->err_count++;
463 else
464 t->err_count = 1;
465 t->err_time = jiffies;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000466}
467
Tom Herbert308edfdf2016-04-29 17:12:17 -0700468static int ip6gre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000469{
470 const struct ipv6hdr *ipv6h;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000471 struct ip6_tnl *tunnel;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000472
473 ipv6h = ipv6_hdr(skb);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000474 tunnel = ip6gre_tunnel_lookup(skb->dev,
Tom Herbert308edfdf2016-04-29 17:12:17 -0700475 &ipv6h->saddr, &ipv6h->daddr, tpi->key,
476 tpi->proto);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000477 if (tunnel) {
William Tu6712abc2017-12-01 15:26:08 -0800478 if (tunnel->parms.collect_md) {
479 struct metadata_dst *tun_dst;
480 __be64 tun_id;
481 __be16 flags;
482
483 flags = tpi->flags;
484 tun_id = key32_to_tunnel_id(tpi->key);
485
486 tun_dst = ipv6_tun_rx_dst(skb, flags, tun_id, 0);
487 if (!tun_dst)
488 return PACKET_REJECT;
489
490 ip6_tnl_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
491 } else {
492 ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error);
493 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000494
Tom Herbert308edfdf2016-04-29 17:12:17 -0700495 return PACKET_RCVD;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000496 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000497
Tom Herbert308edfdf2016-04-29 17:12:17 -0700498 return PACKET_REJECT;
499}
500
William Tu5a963eb2017-11-30 11:51:29 -0800501static int ip6erspan_rcv(struct sk_buff *skb, int gre_hdr_len,
502 struct tnl_ptk_info *tpi)
503{
William Tu1d7e2ed2017-12-13 16:38:55 -0800504 struct erspan_base_hdr *ershdr;
505 struct erspan_metadata *pkt_md;
William Tu5a963eb2017-11-30 11:51:29 -0800506 const struct ipv6hdr *ipv6h;
William Tu5a963eb2017-11-30 11:51:29 -0800507 struct ip6_tnl *tunnel;
William Tu1d7e2ed2017-12-13 16:38:55 -0800508 u8 ver;
William Tu5a963eb2017-11-30 11:51:29 -0800509
William Tu5a963eb2017-11-30 11:51:29 -0800510 if (unlikely(!pskb_may_pull(skb, sizeof(*ershdr))))
511 return PACKET_REJECT;
512
Haishuang Yan293a1992017-12-20 09:53:19 +0800513 ipv6h = ipv6_hdr(skb);
514 ershdr = (struct erspan_base_hdr *)skb->data;
William Tu1d7e2ed2017-12-13 16:38:55 -0800515 ver = (ntohs(ershdr->ver_vlan) & VER_MASK) >> VER_OFFSET;
William Tu5a963eb2017-11-30 11:51:29 -0800516 tpi->key = cpu_to_be32(ntohs(ershdr->session_id) & ID_MASK);
William Tu5a963eb2017-11-30 11:51:29 -0800517
518 tunnel = ip6gre_tunnel_lookup(skb->dev,
519 &ipv6h->saddr, &ipv6h->daddr, tpi->key,
520 tpi->proto);
521 if (tunnel) {
William Tu1d7e2ed2017-12-13 16:38:55 -0800522 int len = erspan_hdr_len(ver);
523
524 if (unlikely(!pskb_may_pull(skb, len)))
William Tuae3e1332017-12-15 14:27:43 -0800525 return PACKET_REJECT;
William Tu1d7e2ed2017-12-13 16:38:55 -0800526
William Tud91e8db52017-12-15 14:27:44 -0800527 ershdr = (struct erspan_base_hdr *)skb->data;
528 pkt_md = (struct erspan_metadata *)(ershdr + 1);
529
William Tu1d7e2ed2017-12-13 16:38:55 -0800530 if (__iptunnel_pull_header(skb, len,
William Tu5a963eb2017-11-30 11:51:29 -0800531 htons(ETH_P_TEB),
532 false, false) < 0)
533 return PACKET_REJECT;
534
William Tuef7baf52017-12-05 15:15:44 -0800535 if (tunnel->parms.collect_md) {
536 struct metadata_dst *tun_dst;
537 struct ip_tunnel_info *info;
538 struct erspan_metadata *md;
539 __be64 tun_id;
540 __be16 flags;
541
542 tpi->flags |= TUNNEL_KEY;
543 flags = tpi->flags;
544 tun_id = key32_to_tunnel_id(tpi->key);
545
546 tun_dst = ipv6_tun_rx_dst(skb, flags, tun_id,
547 sizeof(*md));
548 if (!tun_dst)
549 return PACKET_REJECT;
550
551 info = &tun_dst->u.tun_info;
552 md = ip_tunnel_info_opts(info);
553 if (!md)
554 return PACKET_REJECT;
555
William Tu1d7e2ed2017-12-13 16:38:55 -0800556 memcpy(md, pkt_md, sizeof(*md));
William Tu94d7d8f2017-12-13 16:38:57 -0800557 md->version = ver;
William Tuef7baf52017-12-05 15:15:44 -0800558 info->key.tun_flags |= TUNNEL_ERSPAN_OPT;
559 info->options_len = sizeof(*md);
560
561 ip6_tnl_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
562
563 } else {
William Tu94d7d8f2017-12-13 16:38:57 -0800564 tunnel->parms.erspan_ver = ver;
565
566 if (ver == 1) {
567 tunnel->parms.index = ntohl(pkt_md->u.index);
568 } else {
569 u16 md2_flags;
570 u16 dir, hwid;
571
572 md2_flags = ntohs(pkt_md->u.md2.flags);
573 dir = (md2_flags & DIR_MASK) >> DIR_OFFSET;
574 hwid = (md2_flags & HWID_MASK) >> HWID_OFFSET;
575 tunnel->parms.dir = dir;
576 tunnel->parms.hwid = hwid;
577 }
578
William Tuef7baf52017-12-05 15:15:44 -0800579 ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error);
580 }
William Tu5a963eb2017-11-30 11:51:29 -0800581
582 return PACKET_RCVD;
583 }
584
585 return PACKET_REJECT;
586}
587
Tom Herbert308edfdf2016-04-29 17:12:17 -0700588static int gre_rcv(struct sk_buff *skb)
589{
590 struct tnl_ptk_info tpi;
591 bool csum_err = false;
592 int hdr_len;
593
Eric Dumazete5826152016-06-15 06:24:00 -0700594 hdr_len = gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IPV6), 0);
Jiri Bencf132ae72016-05-03 15:00:21 +0200595 if (hdr_len < 0)
Tom Herbert308edfdf2016-04-29 17:12:17 -0700596 goto drop;
597
598 if (iptunnel_pull_header(skb, hdr_len, tpi.proto, false))
599 goto drop;
600
William Tu94d7d8f2017-12-13 16:38:57 -0800601 if (unlikely(tpi.proto == htons(ETH_P_ERSPAN) ||
602 tpi.proto == htons(ETH_P_ERSPAN2))) {
William Tu5a963eb2017-11-30 11:51:29 -0800603 if (ip6erspan_rcv(skb, hdr_len, &tpi) == PACKET_RCVD)
604 return 0;
Haishuang Yana7343212017-12-20 10:21:47 +0800605 goto out;
William Tu5a963eb2017-11-30 11:51:29 -0800606 }
607
Tom Herbert308edfdf2016-04-29 17:12:17 -0700608 if (ip6gre_rcv(skb, &tpi) == PACKET_RCVD)
609 return 0;
610
Haishuang Yana7343212017-12-20 10:21:47 +0800611out:
Tom Herbert308edfdf2016-04-29 17:12:17 -0700612 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000613drop:
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000614 kfree_skb(skb);
615 return 0;
616}
617
Tom Herbertb05229f2016-04-29 17:12:21 -0700618static int gre_handle_offloads(struct sk_buff *skb, bool csum)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000619{
Tom Herbertb05229f2016-04-29 17:12:21 -0700620 return iptunnel_handle_offloads(skb,
621 csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000622}
623
William Tu898b29792017-11-30 11:51:28 -0800624static void prepare_ip6gre_xmit_ipv4(struct sk_buff *skb,
625 struct net_device *dev,
626 struct flowi6 *fl6, __u8 *dsfield,
627 int *encap_limit)
628{
629 const struct iphdr *iph = ip_hdr(skb);
630 struct ip6_tnl *t = netdev_priv(dev);
631
632 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
633 *encap_limit = t->parms.encap_limit;
634
635 memcpy(fl6, &t->fl.u.ip6, sizeof(*fl6));
636
637 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
638 *dsfield = ipv4_get_dsfield(iph);
639 else
640 *dsfield = ip6_tclass(t->parms.flowinfo);
641
642 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
643 fl6->flowi6_mark = skb->mark;
644 else
645 fl6->flowi6_mark = t->parms.fwmark;
646
647 fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
648}
649
650static int prepare_ip6gre_xmit_ipv6(struct sk_buff *skb,
651 struct net_device *dev,
652 struct flowi6 *fl6, __u8 *dsfield,
653 int *encap_limit)
654{
655 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
656 struct ip6_tnl *t = netdev_priv(dev);
657 __u16 offset;
658
659 offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
660 /* ip6_tnl_parse_tlv_enc_lim() might have reallocated skb->head */
661
662 if (offset > 0) {
663 struct ipv6_tlv_tnl_enc_lim *tel;
664
665 tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
666 if (tel->encap_limit == 0) {
667 icmpv6_send(skb, ICMPV6_PARAMPROB,
668 ICMPV6_HDR_FIELD, offset + 2);
669 return -1;
670 }
671 *encap_limit = tel->encap_limit - 1;
672 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) {
673 *encap_limit = t->parms.encap_limit;
674 }
675
676 memcpy(fl6, &t->fl.u.ip6, sizeof(*fl6));
677
678 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
679 *dsfield = ipv6_get_dsfield(ipv6h);
680 else
681 *dsfield = ip6_tclass(t->parms.flowinfo);
682
683 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
684 fl6->flowlabel |= ip6_flowlabel(ipv6h);
685
686 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
687 fl6->flowi6_mark = skb->mark;
688 else
689 fl6->flowi6_mark = t->parms.fwmark;
690
691 fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
692
693 return 0;
694}
695
Tom Herbertb05229f2016-04-29 17:12:21 -0700696static netdev_tx_t __gre6_xmit(struct sk_buff *skb,
697 struct net_device *dev, __u8 dsfield,
698 struct flowi6 *fl6, int encap_limit,
699 __u32 *pmtu, __be16 proto)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000700{
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000701 struct ip6_tnl *tunnel = netdev_priv(dev);
Xin Long8aec4952017-10-26 19:27:17 +0800702 __be16 protocol;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000703
704 if (dev->type == ARPHRD_ETHER)
705 IPCB(skb)->flags = 0;
706
Tom Herbertb05229f2016-04-29 17:12:21 -0700707 if (dev->header_ops && dev->type == ARPHRD_IP6GRE)
708 fl6->daddr = ((struct ipv6hdr *)skb->data)->daddr;
709 else
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000710 fl6->daddr = tunnel->parms.raddr;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000711
Tom Herbertb05229f2016-04-29 17:12:21 -0700712 if (tunnel->parms.o_flags & TUNNEL_SEQ)
713 tunnel->o_seqno++;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000714
Tom Herbertb05229f2016-04-29 17:12:21 -0700715 /* Push GRE header. */
Xin Long8aec4952017-10-26 19:27:17 +0800716 protocol = (dev->type == ARPHRD_ETHER) ? htons(ETH_P_TEB) : proto;
William Tu6712abc2017-12-01 15:26:08 -0800717
718 if (tunnel->parms.collect_md) {
719 struct ip_tunnel_info *tun_info;
720 const struct ip_tunnel_key *key;
721 __be16 flags;
722
723 tun_info = skb_tunnel_info(skb);
724 if (unlikely(!tun_info ||
725 !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
726 ip_tunnel_info_af(tun_info) != AF_INET6))
727 return -EINVAL;
728
729 key = &tun_info->key;
730 memset(fl6, 0, sizeof(*fl6));
731 fl6->flowi6_proto = IPPROTO_GRE;
732 fl6->daddr = key->u.ipv6.dst;
733 fl6->flowlabel = key->label;
734 fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
735
736 dsfield = key->tos;
737 flags = key->tun_flags & (TUNNEL_CSUM | TUNNEL_KEY);
738 tunnel->tun_hlen = gre_calc_hlen(flags);
739
740 gre_build_header(skb, tunnel->tun_hlen,
741 flags, protocol,
742 tunnel_id_to_key32(tun_info->key.tun_id), 0);
743
744 } else {
745 gre_build_header(skb, tunnel->tun_hlen, tunnel->parms.o_flags,
746 protocol, tunnel->parms.o_key,
747 htonl(tunnel->o_seqno));
748 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000749
Tom Herbertb05229f2016-04-29 17:12:21 -0700750 return ip6_tnl_xmit(skb, dev, dsfield, fl6, encap_limit, pmtu,
751 NEXTHDR_GRE);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000752}
753
754static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev)
755{
756 struct ip6_tnl *t = netdev_priv(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000757 int encap_limit = -1;
758 struct flowi6 fl6;
William Tu6712abc2017-12-01 15:26:08 -0800759 __u8 dsfield = 0;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000760 __u32 mtu;
761 int err;
762
Bernie Harris5146d1f2016-02-22 12:58:05 +1300763 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
764
William Tu6712abc2017-12-01 15:26:08 -0800765 if (!t->parms.collect_md)
766 prepare_ip6gre_xmit_ipv4(skb, dev, &fl6,
767 &dsfield, &encap_limit);
Lorenzo Colittie2d118a2016-11-04 02:23:43 +0900768
Tom Herbertb05229f2016-04-29 17:12:21 -0700769 err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
770 if (err)
771 return -1;
772
773 err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
774 skb->protocol);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000775 if (err != 0) {
776 /* XXX: send ICMP error even if DF is not set. */
777 if (err == -EMSGSIZE)
778 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
779 htonl(mtu));
780 return -1;
781 }
782
783 return 0;
784}
785
786static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev)
787{
788 struct ip6_tnl *t = netdev_priv(dev);
789 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
790 int encap_limit = -1;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000791 struct flowi6 fl6;
William Tu6712abc2017-12-01 15:26:08 -0800792 __u8 dsfield = 0;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000793 __u32 mtu;
794 int err;
795
796 if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
797 return -1;
798
William Tu6712abc2017-12-01 15:26:08 -0800799 if (!t->parms.collect_md &&
800 prepare_ip6gre_xmit_ipv6(skb, dev, &fl6, &dsfield, &encap_limit))
William Tu898b29792017-11-30 11:51:28 -0800801 return -1;
Lorenzo Colittie2d118a2016-11-04 02:23:43 +0900802
Tom Herbertb05229f2016-04-29 17:12:21 -0700803 if (gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM)))
804 return -1;
805
806 err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit,
807 &mtu, skb->protocol);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000808 if (err != 0) {
809 if (err == -EMSGSIZE)
810 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
811 return -1;
812 }
813
814 return 0;
815}
816
817/**
818 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
819 * @t: the outgoing tunnel device
820 * @hdr: IPv6 header from the incoming packet
821 *
822 * Description:
823 * Avoid trivial tunneling loop by checking that tunnel exit-point
824 * doesn't match source of incoming packet.
825 *
826 * Return:
827 * 1 if conflict,
828 * 0 else
829 **/
830
831static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl *t,
832 const struct ipv6hdr *hdr)
833{
834 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
835}
836
837static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev)
838{
839 struct ip6_tnl *t = netdev_priv(dev);
840 int encap_limit = -1;
841 struct flowi6 fl6;
842 __u32 mtu;
843 int err;
844
845 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
846 encap_limit = t->parms.encap_limit;
847
William Tu6712abc2017-12-01 15:26:08 -0800848 if (!t->parms.collect_md)
849 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000850
Tom Herbertb05229f2016-04-29 17:12:21 -0700851 err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
852 if (err)
853 return err;
854
855 err = __gre6_xmit(skb, dev, 0, &fl6, encap_limit, &mtu, skb->protocol);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000856
857 return err;
858}
859
860static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb,
861 struct net_device *dev)
862{
863 struct ip6_tnl *t = netdev_priv(dev);
864 struct net_device_stats *stats = &t->dev->stats;
865 int ret;
866
Steffen Klassertd5005142014-11-05 08:02:48 +0100867 if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
Tommi Rantala41ab3e32013-02-06 03:24:02 +0000868 goto tx_err;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000869
870 switch (skb->protocol) {
871 case htons(ETH_P_IP):
872 ret = ip6gre_xmit_ipv4(skb, dev);
873 break;
874 case htons(ETH_P_IPV6):
875 ret = ip6gre_xmit_ipv6(skb, dev);
876 break;
877 default:
878 ret = ip6gre_xmit_other(skb, dev);
879 break;
880 }
881
882 if (ret < 0)
883 goto tx_err;
884
885 return NETDEV_TX_OK;
886
887tx_err:
888 stats->tx_errors++;
889 stats->tx_dropped++;
890 kfree_skb(skb);
891 return NETDEV_TX_OK;
892}
893
William Tu5a963eb2017-11-30 11:51:29 -0800894static netdev_tx_t ip6erspan_tunnel_xmit(struct sk_buff *skb,
895 struct net_device *dev)
896{
897 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
898 struct ip6_tnl *t = netdev_priv(dev);
899 struct dst_entry *dst = skb_dst(skb);
900 struct net_device_stats *stats;
901 bool truncate = false;
902 int encap_limit = -1;
903 __u8 dsfield = false;
904 struct flowi6 fl6;
905 int err = -EINVAL;
906 __u32 mtu;
907
908 if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
909 goto tx_err;
910
911 if (gre_handle_offloads(skb, false))
912 goto tx_err;
913
William Tu5a963eb2017-11-30 11:51:29 -0800914 if (skb->len > dev->mtu + dev->hard_header_len) {
915 pskb_trim(skb, dev->mtu + dev->hard_header_len);
916 truncate = true;
917 }
918
William Tu5a963eb2017-11-30 11:51:29 -0800919 t->parms.o_flags &= ~TUNNEL_KEY;
William Tu5a963eb2017-11-30 11:51:29 -0800920 IPCB(skb)->flags = 0;
William Tuef7baf52017-12-05 15:15:44 -0800921
922 /* For collect_md mode, derive fl6 from the tunnel key,
923 * for native mode, call prepare_ip6gre_xmit_{ipv4,ipv6}.
924 */
925 if (t->parms.collect_md) {
926 struct ip_tunnel_info *tun_info;
927 const struct ip_tunnel_key *key;
928 struct erspan_metadata *md;
929
930 tun_info = skb_tunnel_info(skb);
931 if (unlikely(!tun_info ||
932 !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
933 ip_tunnel_info_af(tun_info) != AF_INET6))
934 return -EINVAL;
935
936 key = &tun_info->key;
937 memset(&fl6, 0, sizeof(fl6));
938 fl6.flowi6_proto = IPPROTO_GRE;
939 fl6.daddr = key->u.ipv6.dst;
940 fl6.flowlabel = key->label;
941 fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
942
943 dsfield = key->tos;
944 md = ip_tunnel_info_opts(tun_info);
945 if (!md)
946 goto tx_err;
947
William Tu94d7d8f2017-12-13 16:38:57 -0800948 if (md->version == 1) {
949 erspan_build_header(skb,
950 tunnel_id_to_key32(key->tun_id),
951 ntohl(md->u.index), truncate,
952 false);
953 } else if (md->version == 2) {
954 u16 md2_flags;
955 u16 dir, hwid;
William Tuef7baf52017-12-05 15:15:44 -0800956
William Tu94d7d8f2017-12-13 16:38:57 -0800957 md2_flags = ntohs(md->u.md2.flags);
958 dir = (md2_flags & DIR_MASK) >> DIR_OFFSET;
959 hwid = (md2_flags & HWID_MASK) >> HWID_OFFSET;
960
961 erspan_build_header_v2(skb,
962 tunnel_id_to_key32(key->tun_id),
963 dir, hwid, truncate,
964 false);
965 }
William Tuef7baf52017-12-05 15:15:44 -0800966 } else {
967 switch (skb->protocol) {
968 case htons(ETH_P_IP):
969 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
970 prepare_ip6gre_xmit_ipv4(skb, dev, &fl6,
971 &dsfield, &encap_limit);
972 break;
973 case htons(ETH_P_IPV6):
974 if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
975 goto tx_err;
976 if (prepare_ip6gre_xmit_ipv6(skb, dev, &fl6,
977 &dsfield, &encap_limit))
978 goto tx_err;
979 break;
980 default:
981 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
982 break;
983 }
984
William Tu94d7d8f2017-12-13 16:38:57 -0800985 if (t->parms.erspan_ver == 1)
986 erspan_build_header(skb, t->parms.o_key,
987 t->parms.index,
988 truncate, false);
989 else
990 erspan_build_header_v2(skb, t->parms.o_key,
991 t->parms.dir,
992 t->parms.hwid,
993 truncate, false);
William Tuef7baf52017-12-05 15:15:44 -0800994 fl6.daddr = t->parms.raddr;
995 }
William Tu5a963eb2017-11-30 11:51:29 -0800996
997 /* Push GRE header. */
998 gre_build_header(skb, 8, TUNNEL_SEQ,
999 htons(ETH_P_ERSPAN), 0, htonl(t->o_seqno++));
1000
1001 /* TooBig packet may have updated dst->dev's mtu */
William Tuef7baf52017-12-05 15:15:44 -08001002 if (!t->parms.collect_md && dst && dst_mtu(dst) > dst->dev->mtu)
William Tu5a963eb2017-11-30 11:51:29 -08001003 dst->ops->update_pmtu(dst, NULL, skb, dst->dev->mtu);
1004
1005 err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
1006 NEXTHDR_GRE);
1007 if (err != 0) {
1008 /* XXX: send ICMP error even if DF is not set. */
1009 if (err == -EMSGSIZE) {
1010 if (skb->protocol == htons(ETH_P_IP))
1011 icmp_send(skb, ICMP_DEST_UNREACH,
1012 ICMP_FRAG_NEEDED, htonl(mtu));
1013 else
1014 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1015 }
1016
1017 goto tx_err;
1018 }
1019 return NETDEV_TX_OK;
1020
1021tx_err:
1022 stats = &t->dev->stats;
1023 stats->tx_errors++;
1024 stats->tx_dropped++;
1025 kfree_skb(skb);
1026 return NETDEV_TX_OK;
1027}
1028
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001029static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu)
1030{
1031 struct net_device *dev = t->dev;
1032 struct __ip6_tnl_parm *p = &t->parms;
1033 struct flowi6 *fl6 = &t->fl.u.ip6;
Tom Herbertdb2ec952016-05-09 17:12:08 -07001034 int t_hlen;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001035
1036 if (dev->type != ARPHRD_ETHER) {
1037 memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
1038 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
1039 }
1040
1041 /* Set up flowi template */
1042 fl6->saddr = p->laddr;
1043 fl6->daddr = p->raddr;
1044 fl6->flowi6_oif = p->link;
1045 fl6->flowlabel = 0;
Haishuang Yan252f3f52016-05-21 18:17:35 +08001046 fl6->flowi6_proto = IPPROTO_GRE;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001047
1048 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
1049 fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
1050 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
1051 fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
1052
1053 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
1054 p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
1055
1056 if (p->flags&IP6_TNL_F_CAP_XMIT &&
1057 p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER)
1058 dev->flags |= IFF_POINTOPOINT;
1059 else
1060 dev->flags &= ~IFF_POINTOPOINT;
1061
Tom Herbertdb2ec952016-05-09 17:12:08 -07001062 t->tun_hlen = gre_calc_hlen(t->parms.o_flags);
1063
Tom Herbert1faf3d92016-05-18 09:06:19 -07001064 t->hlen = t->encap_hlen + t->tun_hlen;
Tom Herbertdb2ec952016-05-09 17:12:08 -07001065
1066 t_hlen = t->hlen + sizeof(struct ipv6hdr);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001067
1068 if (p->flags & IP6_TNL_F_CAP_XMIT) {
1069 int strict = (ipv6_addr_type(&p->raddr) &
1070 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
1071
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001072 struct rt6_info *rt = rt6_lookup(t->net,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001073 &p->raddr, &p->laddr,
1074 p->link, strict);
1075
Ian Morris63159f22015-03-29 14:00:04 +01001076 if (!rt)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001077 return;
1078
1079 if (rt->dst.dev) {
Tom Herbertdb2ec952016-05-09 17:12:08 -07001080 dev->hard_header_len = rt->dst.dev->hard_header_len +
1081 t_hlen;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001082
1083 if (set_mtu) {
Tom Herbertdb2ec952016-05-09 17:12:08 -07001084 dev->mtu = rt->dst.dev->mtu - t_hlen;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001085 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1086 dev->mtu -= 8;
Alexander Duycka9e242c2016-04-14 15:33:45 -04001087 if (dev->type == ARPHRD_ETHER)
1088 dev->mtu -= ETH_HLEN;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001089
1090 if (dev->mtu < IPV6_MIN_MTU)
1091 dev->mtu = IPV6_MIN_MTU;
1092 }
1093 }
Amerigo Wang94e187c2012-10-29 00:13:19 +00001094 ip6_rt_put(rt);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001095 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001096}
1097
1098static int ip6gre_tnl_change(struct ip6_tnl *t,
1099 const struct __ip6_tnl_parm *p, int set_mtu)
1100{
1101 t->parms.laddr = p->laddr;
1102 t->parms.raddr = p->raddr;
1103 t->parms.flags = p->flags;
1104 t->parms.hop_limit = p->hop_limit;
1105 t->parms.encap_limit = p->encap_limit;
1106 t->parms.flowinfo = p->flowinfo;
1107 t->parms.link = p->link;
1108 t->parms.proto = p->proto;
1109 t->parms.i_key = p->i_key;
1110 t->parms.o_key = p->o_key;
1111 t->parms.i_flags = p->i_flags;
1112 t->parms.o_flags = p->o_flags;
Craig Gallek0a473b82017-04-19 12:30:53 -04001113 t->parms.fwmark = p->fwmark;
Paolo Abeni607f7252016-02-12 15:43:54 +01001114 dst_cache_reset(&t->dst_cache);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001115 ip6gre_tnl_link_config(t, set_mtu);
1116 return 0;
1117}
1118
1119static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p,
1120 const struct ip6_tnl_parm2 *u)
1121{
1122 p->laddr = u->laddr;
1123 p->raddr = u->raddr;
1124 p->flags = u->flags;
1125 p->hop_limit = u->hop_limit;
1126 p->encap_limit = u->encap_limit;
1127 p->flowinfo = u->flowinfo;
1128 p->link = u->link;
1129 p->i_key = u->i_key;
1130 p->o_key = u->o_key;
Tom Herbertf41fe3c2016-05-09 17:12:09 -07001131 p->i_flags = gre_flags_to_tnl_flags(u->i_flags);
1132 p->o_flags = gre_flags_to_tnl_flags(u->o_flags);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001133 memcpy(p->name, u->name, sizeof(u->name));
1134}
1135
1136static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u,
1137 const struct __ip6_tnl_parm *p)
1138{
1139 u->proto = IPPROTO_GRE;
1140 u->laddr = p->laddr;
1141 u->raddr = p->raddr;
1142 u->flags = p->flags;
1143 u->hop_limit = p->hop_limit;
1144 u->encap_limit = p->encap_limit;
1145 u->flowinfo = p->flowinfo;
1146 u->link = p->link;
1147 u->i_key = p->i_key;
1148 u->o_key = p->o_key;
Tom Herbertf41fe3c2016-05-09 17:12:09 -07001149 u->i_flags = gre_tnl_flags_to_gre_flags(p->i_flags);
1150 u->o_flags = gre_tnl_flags_to_gre_flags(p->o_flags);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001151 memcpy(u->name, p->name, sizeof(u->name));
1152}
1153
1154static int ip6gre_tunnel_ioctl(struct net_device *dev,
1155 struct ifreq *ifr, int cmd)
1156{
1157 int err = 0;
1158 struct ip6_tnl_parm2 p;
1159 struct __ip6_tnl_parm p1;
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001160 struct ip6_tnl *t = netdev_priv(dev);
1161 struct net *net = t->net;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001162 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1163
Tom Herbert308edfdf2016-04-29 17:12:17 -07001164 memset(&p1, 0, sizeof(p1));
1165
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001166 switch (cmd) {
1167 case SIOCGETTUNNEL:
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001168 if (dev == ign->fb_tunnel_dev) {
1169 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
1170 err = -EFAULT;
1171 break;
1172 }
1173 ip6gre_tnl_parm_from_user(&p1, &p);
1174 t = ip6gre_tunnel_locate(net, &p1, 0);
Ian Morris63159f22015-03-29 14:00:04 +01001175 if (!t)
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001176 t = netdev_priv(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001177 }
Amerigo Wang5dbd5062013-05-09 21:56:37 +00001178 memset(&p, 0, sizeof(p));
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001179 ip6gre_tnl_parm_to_user(&p, &t->parms);
1180 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
1181 err = -EFAULT;
1182 break;
1183
1184 case SIOCADDTUNNEL:
1185 case SIOCCHGTUNNEL:
1186 err = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00001187 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001188 goto done;
1189
1190 err = -EFAULT;
1191 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1192 goto done;
1193
1194 err = -EINVAL;
1195 if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))
1196 goto done;
1197
1198 if (!(p.i_flags&GRE_KEY))
1199 p.i_key = 0;
1200 if (!(p.o_flags&GRE_KEY))
1201 p.o_key = 0;
1202
1203 ip6gre_tnl_parm_from_user(&p1, &p);
1204 t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL);
1205
1206 if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
Ian Morris53b24b82015-03-29 14:00:05 +01001207 if (t) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001208 if (t->dev != dev) {
1209 err = -EEXIST;
1210 break;
1211 }
1212 } else {
1213 t = netdev_priv(dev);
1214
1215 ip6gre_tunnel_unlink(ign, t);
1216 synchronize_net();
1217 ip6gre_tnl_change(t, &p1, 1);
1218 ip6gre_tunnel_link(ign, t);
1219 netdev_state_change(dev);
1220 }
1221 }
1222
1223 if (t) {
1224 err = 0;
1225
Amerigo Wang5dbd5062013-05-09 21:56:37 +00001226 memset(&p, 0, sizeof(p));
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001227 ip6gre_tnl_parm_to_user(&p, &t->parms);
1228 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
1229 err = -EFAULT;
1230 } else
1231 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
1232 break;
1233
1234 case SIOCDELTUNNEL:
1235 err = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00001236 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001237 goto done;
1238
1239 if (dev == ign->fb_tunnel_dev) {
1240 err = -EFAULT;
1241 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1242 goto done;
1243 err = -ENOENT;
1244 ip6gre_tnl_parm_from_user(&p1, &p);
1245 t = ip6gre_tunnel_locate(net, &p1, 0);
Ian Morris63159f22015-03-29 14:00:04 +01001246 if (!t)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001247 goto done;
1248 err = -EPERM;
1249 if (t == netdev_priv(ign->fb_tunnel_dev))
1250 goto done;
1251 dev = t->dev;
1252 }
1253 unregister_netdevice(dev);
1254 err = 0;
1255 break;
1256
1257 default:
1258 err = -EINVAL;
1259 }
1260
1261done:
1262 return err;
1263}
1264
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001265static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
Xin Long76cc0d32017-09-15 12:00:07 +08001266 unsigned short type, const void *daddr,
1267 const void *saddr, unsigned int len)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001268{
1269 struct ip6_tnl *t = netdev_priv(dev);
Xin Long76cc0d32017-09-15 12:00:07 +08001270 struct ipv6hdr *ipv6h;
1271 __be16 *p;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001272
Xin Long76cc0d32017-09-15 12:00:07 +08001273 ipv6h = skb_push(skb, t->hlen + sizeof(*ipv6h));
1274 ip6_flow_hdr(ipv6h, 0, ip6_make_flowlabel(dev_net(dev), skb,
1275 t->fl.u.ip6.flowlabel,
1276 true, &t->fl.u.ip6));
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001277 ipv6h->hop_limit = t->parms.hop_limit;
1278 ipv6h->nexthdr = NEXTHDR_GRE;
1279 ipv6h->saddr = t->parms.laddr;
1280 ipv6h->daddr = t->parms.raddr;
1281
Xin Long76cc0d32017-09-15 12:00:07 +08001282 p = (__be16 *)(ipv6h + 1);
1283 p[0] = t->parms.o_flags;
1284 p[1] = htons(type);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001285
1286 /*
1287 * Set the source hardware address.
1288 */
1289
1290 if (saddr)
1291 memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr));
1292 if (daddr)
1293 memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
1294 if (!ipv6_addr_any(&ipv6h->daddr))
1295 return t->hlen;
1296
1297 return -t->hlen;
1298}
1299
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001300static const struct header_ops ip6gre_header_ops = {
1301 .create = ip6gre_header,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001302};
1303
1304static const struct net_device_ops ip6gre_netdev_ops = {
1305 .ndo_init = ip6gre_tunnel_init,
1306 .ndo_uninit = ip6gre_tunnel_uninit,
1307 .ndo_start_xmit = ip6gre_tunnel_xmit,
1308 .ndo_do_ioctl = ip6gre_tunnel_ioctl,
Tom Herbertb05229f2016-04-29 17:12:21 -07001309 .ndo_change_mtu = ip6_tnl_change_mtu,
Pravin B Shelarf61dd382013-03-25 14:50:00 +00001310 .ndo_get_stats64 = ip_tunnel_get_stats64,
Nicolas Dichtelecf2c062015-04-02 17:07:01 +02001311 .ndo_get_iflink = ip6_tnl_get_iflink,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001312};
1313
1314static void ip6gre_dev_free(struct net_device *dev)
1315{
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001316 struct ip6_tnl *t = netdev_priv(dev);
1317
Paolo Abeni607f7252016-02-12 15:43:54 +01001318 dst_cache_destroy(&t->dst_cache);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001319 free_percpu(dev->tstats);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001320}
1321
1322static void ip6gre_tunnel_setup(struct net_device *dev)
1323{
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001324 dev->netdev_ops = &ip6gre_netdev_ops;
David S. Millercf124db2017-05-08 12:52:56 -04001325 dev->needs_free_netdev = true;
1326 dev->priv_destructor = ip6gre_dev_free;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001327
1328 dev->type = ARPHRD_IP6GRE;
Tom Herbertb05229f2016-04-29 17:12:21 -07001329
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001330 dev->flags |= IFF_NOARP;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001331 dev->addr_len = sizeof(struct in6_addr);
Eric Dumazet02875872014-10-05 18:38:35 -07001332 netif_keep_dst(dev);
Felix Jia45ce0fd2017-01-26 16:59:18 +13001333 /* This perm addr will be used as interface identifier by IPv6 */
1334 dev->addr_assign_type = NET_ADDR_RANDOM;
1335 eth_random_addr(dev->perm_addr);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001336}
1337
Martin KaFai Laua3c119d2015-09-15 14:30:05 -07001338static int ip6gre_tunnel_init_common(struct net_device *dev)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001339{
1340 struct ip6_tnl *tunnel;
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001341 int ret;
Tom Herbertb05229f2016-04-29 17:12:21 -07001342 int t_hlen;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001343
1344 tunnel = netdev_priv(dev);
1345
1346 tunnel->dev = dev;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001347 tunnel->net = dev_net(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001348 strcpy(tunnel->parms.name, dev->name);
1349
Martin KaFai Laua3c119d2015-09-15 14:30:05 -07001350 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1351 if (!dev->tstats)
1352 return -ENOMEM;
1353
Paolo Abeni607f7252016-02-12 15:43:54 +01001354 ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001355 if (ret) {
1356 free_percpu(dev->tstats);
1357 dev->tstats = NULL;
1358 return ret;
1359 }
1360
Tom Herbertb05229f2016-04-29 17:12:21 -07001361 tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
Tom Herbert1faf3d92016-05-18 09:06:19 -07001362 tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen;
Tom Herbertb05229f2016-04-29 17:12:21 -07001363 t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
1364
Tom Herbertdb2ec952016-05-09 17:12:08 -07001365 dev->hard_header_len = LL_MAX_HEADER + t_hlen;
1366 dev->mtu = ETH_DATA_LEN - t_hlen;
Haishuang Yan1b227e52016-05-21 18:17:34 +08001367 if (dev->type == ARPHRD_ETHER)
1368 dev->mtu -= ETH_HLEN;
Tom Herbertb05229f2016-04-29 17:12:21 -07001369 if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1370 dev->mtu -= 8;
1371
William Tu6712abc2017-12-01 15:26:08 -08001372 if (tunnel->parms.collect_md) {
1373 dev->features |= NETIF_F_NETNS_LOCAL;
1374 netif_keep_dst(dev);
1375 }
1376
Martin KaFai Laua3c119d2015-09-15 14:30:05 -07001377 return 0;
1378}
1379
1380static int ip6gre_tunnel_init(struct net_device *dev)
1381{
1382 struct ip6_tnl *tunnel;
1383 int ret;
1384
1385 ret = ip6gre_tunnel_init_common(dev);
1386 if (ret)
1387 return ret;
1388
1389 tunnel = netdev_priv(dev);
1390
William Tu6712abc2017-12-01 15:26:08 -08001391 if (tunnel->parms.collect_md)
1392 return 0;
1393
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001394 memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr));
1395 memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr));
1396
1397 if (ipv6_addr_any(&tunnel->parms.raddr))
1398 dev->header_ops = &ip6gre_header_ops;
1399
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001400 return 0;
1401}
1402
1403static void ip6gre_fb_tunnel_init(struct net_device *dev)
1404{
1405 struct ip6_tnl *tunnel = netdev_priv(dev);
1406
1407 tunnel->dev = dev;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001408 tunnel->net = dev_net(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001409 strcpy(tunnel->parms.name, dev->name);
1410
1411 tunnel->hlen = sizeof(struct ipv6hdr) + 4;
1412
1413 dev_hold(dev);
1414}
1415
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001416static struct inet6_protocol ip6gre_protocol __read_mostly = {
Tom Herbert308edfdf2016-04-29 17:12:17 -07001417 .handler = gre_rcv,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001418 .err_handler = ip6gre_err,
1419 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1420};
1421
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001422static void ip6gre_destroy_tunnels(struct net *net, struct list_head *head)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001423{
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001424 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1425 struct net_device *dev, *aux;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001426 int prio;
1427
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001428 for_each_netdev_safe(net, dev, aux)
1429 if (dev->rtnl_link_ops == &ip6gre_link_ops ||
William Tu5a963eb2017-11-30 11:51:29 -08001430 dev->rtnl_link_ops == &ip6gre_tap_ops ||
1431 dev->rtnl_link_ops == &ip6erspan_tap_ops)
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001432 unregister_netdevice_queue(dev, head);
1433
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001434 for (prio = 0; prio < 4; prio++) {
1435 int h;
Jiri Kosinae87a8f22016-08-10 11:03:35 +02001436 for (h = 0; h < IP6_GRE_HASH_SIZE; h++) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001437 struct ip6_tnl *t;
1438
1439 t = rtnl_dereference(ign->tunnels[prio][h]);
1440
Ian Morris53b24b82015-03-29 14:00:05 +01001441 while (t) {
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001442 /* If dev is in the same netns, it has already
1443 * been added to the list by the previous loop.
1444 */
1445 if (!net_eq(dev_net(t->dev), net))
1446 unregister_netdevice_queue(t->dev,
1447 head);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001448 t = rtnl_dereference(t->next);
1449 }
1450 }
1451 }
1452}
1453
1454static int __net_init ip6gre_init_net(struct net *net)
1455{
1456 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1457 int err;
1458
1459 ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0",
Tom Gundersenc835a672014-07-14 16:37:24 +02001460 NET_NAME_UNKNOWN,
1461 ip6gre_tunnel_setup);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001462 if (!ign->fb_tunnel_dev) {
1463 err = -ENOMEM;
1464 goto err_alloc_dev;
1465 }
1466 dev_net_set(ign->fb_tunnel_dev, net);
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001467 /* FB netdevice is special: we have one, and only one per netns.
1468 * Allowing to move it to another netns is clearly unsafe.
1469 */
1470 ign->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL;
1471
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001472
1473 ip6gre_fb_tunnel_init(ign->fb_tunnel_dev);
1474 ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops;
1475
1476 err = register_netdev(ign->fb_tunnel_dev);
1477 if (err)
1478 goto err_reg_dev;
1479
1480 rcu_assign_pointer(ign->tunnels_wc[0],
1481 netdev_priv(ign->fb_tunnel_dev));
1482 return 0;
1483
1484err_reg_dev:
David S. Millercf124db2017-05-08 12:52:56 -04001485 free_netdev(ign->fb_tunnel_dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001486err_alloc_dev:
1487 return err;
1488}
1489
Eric Dumazetbb401ca2017-09-19 16:27:08 -07001490static void __net_exit ip6gre_exit_batch_net(struct list_head *net_list)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001491{
Eric Dumazetbb401ca2017-09-19 16:27:08 -07001492 struct net *net;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001493 LIST_HEAD(list);
1494
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001495 rtnl_lock();
Eric Dumazetbb401ca2017-09-19 16:27:08 -07001496 list_for_each_entry(net, net_list, exit_list)
1497 ip6gre_destroy_tunnels(net, &list);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001498 unregister_netdevice_many(&list);
1499 rtnl_unlock();
1500}
1501
1502static struct pernet_operations ip6gre_net_ops = {
1503 .init = ip6gre_init_net,
Eric Dumazetbb401ca2017-09-19 16:27:08 -07001504 .exit_batch = ip6gre_exit_batch_net,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001505 .id = &ip6gre_net_id,
1506 .size = sizeof(struct ip6gre_net),
1507};
1508
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001509static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[],
1510 struct netlink_ext_ack *extack)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001511{
1512 __be16 flags;
1513
1514 if (!data)
1515 return 0;
1516
1517 flags = 0;
1518 if (data[IFLA_GRE_IFLAGS])
1519 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1520 if (data[IFLA_GRE_OFLAGS])
1521 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1522 if (flags & (GRE_VERSION|GRE_ROUTING))
1523 return -EINVAL;
1524
1525 return 0;
1526}
1527
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001528static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[],
1529 struct netlink_ext_ack *extack)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001530{
1531 struct in6_addr daddr;
1532
1533 if (tb[IFLA_ADDRESS]) {
1534 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1535 return -EINVAL;
1536 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1537 return -EADDRNOTAVAIL;
1538 }
1539
1540 if (!data)
1541 goto out;
1542
1543 if (data[IFLA_GRE_REMOTE]) {
Jiri Benc67b61f62015-03-29 16:59:26 +02001544 daddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001545 if (ipv6_addr_any(&daddr))
1546 return -EINVAL;
1547 }
1548
1549out:
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001550 return ip6gre_tunnel_validate(tb, data, extack);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001551}
1552
William Tu5a963eb2017-11-30 11:51:29 -08001553static int ip6erspan_tap_validate(struct nlattr *tb[], struct nlattr *data[],
1554 struct netlink_ext_ack *extack)
1555{
1556 __be16 flags = 0;
William Tu94d7d8f2017-12-13 16:38:57 -08001557 int ret, ver = 0;
William Tu5a963eb2017-11-30 11:51:29 -08001558
1559 if (!data)
1560 return 0;
1561
1562 ret = ip6gre_tap_validate(tb, data, extack);
1563 if (ret)
1564 return ret;
1565
1566 /* ERSPAN should only have GRE sequence and key flag */
1567 if (data[IFLA_GRE_OFLAGS])
1568 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1569 if (data[IFLA_GRE_IFLAGS])
1570 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1571 if (!data[IFLA_GRE_COLLECT_METADATA] &&
1572 flags != (GRE_SEQ | GRE_KEY))
1573 return -EINVAL;
1574
1575 /* ERSPAN Session ID only has 10-bit. Since we reuse
1576 * 32-bit key field as ID, check it's range.
1577 */
1578 if (data[IFLA_GRE_IKEY] &&
1579 (ntohl(nla_get_be32(data[IFLA_GRE_IKEY])) & ~ID_MASK))
1580 return -EINVAL;
1581
1582 if (data[IFLA_GRE_OKEY] &&
1583 (ntohl(nla_get_be32(data[IFLA_GRE_OKEY])) & ~ID_MASK))
1584 return -EINVAL;
1585
William Tu94d7d8f2017-12-13 16:38:57 -08001586 if (data[IFLA_GRE_ERSPAN_VER]) {
1587 ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]);
1588 if (ver != 1 && ver != 2)
William Tu5a963eb2017-11-30 11:51:29 -08001589 return -EINVAL;
1590 }
William Tu94d7d8f2017-12-13 16:38:57 -08001591
1592 if (ver == 1) {
1593 if (data[IFLA_GRE_ERSPAN_INDEX]) {
1594 u32 index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
1595
1596 if (index & ~INDEX_MASK)
1597 return -EINVAL;
1598 }
1599 } else if (ver == 2) {
1600 if (data[IFLA_GRE_ERSPAN_DIR]) {
1601 u16 dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]);
1602
1603 if (dir & ~(DIR_MASK >> DIR_OFFSET))
1604 return -EINVAL;
1605 }
1606
1607 if (data[IFLA_GRE_ERSPAN_HWID]) {
1608 u16 hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]);
1609
1610 if (hwid & ~(HWID_MASK >> HWID_OFFSET))
1611 return -EINVAL;
1612 }
1613 }
1614
William Tu5a963eb2017-11-30 11:51:29 -08001615 return 0;
1616}
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001617
1618static void ip6gre_netlink_parms(struct nlattr *data[],
1619 struct __ip6_tnl_parm *parms)
1620{
1621 memset(parms, 0, sizeof(*parms));
1622
1623 if (!data)
1624 return;
1625
1626 if (data[IFLA_GRE_LINK])
1627 parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
1628
1629 if (data[IFLA_GRE_IFLAGS])
Tom Herbertf41fe3c2016-05-09 17:12:09 -07001630 parms->i_flags = gre_flags_to_tnl_flags(
1631 nla_get_be16(data[IFLA_GRE_IFLAGS]));
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001632
1633 if (data[IFLA_GRE_OFLAGS])
Tom Herbertf41fe3c2016-05-09 17:12:09 -07001634 parms->o_flags = gre_flags_to_tnl_flags(
1635 nla_get_be16(data[IFLA_GRE_OFLAGS]));
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001636
1637 if (data[IFLA_GRE_IKEY])
1638 parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
1639
1640 if (data[IFLA_GRE_OKEY])
1641 parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
1642
1643 if (data[IFLA_GRE_LOCAL])
Jiri Benc67b61f62015-03-29 16:59:26 +02001644 parms->laddr = nla_get_in6_addr(data[IFLA_GRE_LOCAL]);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001645
1646 if (data[IFLA_GRE_REMOTE])
Jiri Benc67b61f62015-03-29 16:59:26 +02001647 parms->raddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001648
1649 if (data[IFLA_GRE_TTL])
1650 parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]);
1651
1652 if (data[IFLA_GRE_ENCAP_LIMIT])
1653 parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]);
1654
1655 if (data[IFLA_GRE_FLOWINFO])
Lance Richardsonc2675de2016-09-24 14:01:04 -04001656 parms->flowinfo = nla_get_be32(data[IFLA_GRE_FLOWINFO]);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001657
1658 if (data[IFLA_GRE_FLAGS])
1659 parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]);
Craig Gallek0a473b82017-04-19 12:30:53 -04001660
1661 if (data[IFLA_GRE_FWMARK])
1662 parms->fwmark = nla_get_u32(data[IFLA_GRE_FWMARK]);
William Tu5a963eb2017-11-30 11:51:29 -08001663
William Tu6712abc2017-12-01 15:26:08 -08001664 if (data[IFLA_GRE_COLLECT_METADATA])
1665 parms->collect_md = true;
William Tu94d7d8f2017-12-13 16:38:57 -08001666
1667 if (data[IFLA_GRE_ERSPAN_VER])
1668 parms->erspan_ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]);
1669
1670 if (parms->erspan_ver == 1) {
1671 if (data[IFLA_GRE_ERSPAN_INDEX])
1672 parms->index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
1673 } else if (parms->erspan_ver == 2) {
1674 if (data[IFLA_GRE_ERSPAN_DIR])
1675 parms->dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]);
1676 if (data[IFLA_GRE_ERSPAN_HWID])
1677 parms->hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]);
1678 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001679}
1680
1681static int ip6gre_tap_init(struct net_device *dev)
1682{
1683 struct ip6_tnl *tunnel;
Martin KaFai Laua3c119d2015-09-15 14:30:05 -07001684 int ret;
1685
1686 ret = ip6gre_tunnel_init_common(dev);
1687 if (ret)
1688 return ret;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001689
Shweta Choudaha0a46baa2016-06-08 20:15:43 +01001690 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1691
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001692 tunnel = netdev_priv(dev);
1693
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001694 ip6gre_tnl_link_config(tunnel, 1);
1695
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001696 return 0;
1697}
1698
1699static const struct net_device_ops ip6gre_tap_netdev_ops = {
1700 .ndo_init = ip6gre_tap_init,
1701 .ndo_uninit = ip6gre_tunnel_uninit,
1702 .ndo_start_xmit = ip6gre_tunnel_xmit,
1703 .ndo_set_mac_address = eth_mac_addr,
1704 .ndo_validate_addr = eth_validate_addr,
Tom Herbertb05229f2016-04-29 17:12:21 -07001705 .ndo_change_mtu = ip6_tnl_change_mtu,
Pravin B Shelarf61dd382013-03-25 14:50:00 +00001706 .ndo_get_stats64 = ip_tunnel_get_stats64,
Nicolas Dichtelecf2c062015-04-02 17:07:01 +02001707 .ndo_get_iflink = ip6_tnl_get_iflink,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001708};
1709
Alexander Duyckac4eb002016-04-14 15:33:51 -04001710#define GRE6_FEATURES (NETIF_F_SG | \
1711 NETIF_F_FRAGLIST | \
1712 NETIF_F_HIGHDMA | \
1713 NETIF_F_HW_CSUM)
1714
William Tu5a963eb2017-11-30 11:51:29 -08001715static int ip6erspan_tap_init(struct net_device *dev)
1716{
1717 struct ip6_tnl *tunnel;
1718 int t_hlen;
1719 int ret;
1720
1721 tunnel = netdev_priv(dev);
1722
1723 tunnel->dev = dev;
1724 tunnel->net = dev_net(dev);
1725 strcpy(tunnel->parms.name, dev->name);
1726
1727 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1728 if (!dev->tstats)
1729 return -ENOMEM;
1730
1731 ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
1732 if (ret) {
1733 free_percpu(dev->tstats);
1734 dev->tstats = NULL;
1735 return ret;
1736 }
1737
1738 tunnel->tun_hlen = 8;
1739 tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen +
William Tu94d7d8f2017-12-13 16:38:57 -08001740 erspan_hdr_len(tunnel->parms.erspan_ver);
William Tu5a963eb2017-11-30 11:51:29 -08001741 t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
1742
1743 dev->hard_header_len = LL_MAX_HEADER + t_hlen;
1744 dev->mtu = ETH_DATA_LEN - t_hlen;
1745 if (dev->type == ARPHRD_ETHER)
1746 dev->mtu -= ETH_HLEN;
1747 if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1748 dev->mtu -= 8;
1749
1750 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1751 tunnel = netdev_priv(dev);
1752 ip6gre_tnl_link_config(tunnel, 1);
1753
1754 return 0;
1755}
1756
1757static const struct net_device_ops ip6erspan_netdev_ops = {
1758 .ndo_init = ip6erspan_tap_init,
1759 .ndo_uninit = ip6gre_tunnel_uninit,
1760 .ndo_start_xmit = ip6erspan_tunnel_xmit,
1761 .ndo_set_mac_address = eth_mac_addr,
1762 .ndo_validate_addr = eth_validate_addr,
1763 .ndo_change_mtu = ip6_tnl_change_mtu,
1764 .ndo_get_stats64 = ip_tunnel_get_stats64,
1765 .ndo_get_iflink = ip6_tnl_get_iflink,
1766};
1767
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001768static void ip6gre_tap_setup(struct net_device *dev)
1769{
1770
1771 ether_setup(dev);
1772
1773 dev->netdev_ops = &ip6gre_tap_netdev_ops;
David S. Millercf124db2017-05-08 12:52:56 -04001774 dev->needs_free_netdev = true;
1775 dev->priv_destructor = ip6gre_dev_free;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001776
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001777 dev->features |= NETIF_F_NETNS_LOCAL;
Jiri Bencd13b1612016-02-17 15:32:53 +01001778 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
Shweta Choudaha0a46baa2016-06-08 20:15:43 +01001779 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Xin Long2d405572017-09-28 13:23:50 +08001780 netif_keep_dst(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001781}
1782
Tom Herbert1faf3d92016-05-18 09:06:19 -07001783static bool ip6gre_netlink_encap_parms(struct nlattr *data[],
1784 struct ip_tunnel_encap *ipencap)
1785{
1786 bool ret = false;
1787
1788 memset(ipencap, 0, sizeof(*ipencap));
1789
1790 if (!data)
1791 return ret;
1792
1793 if (data[IFLA_GRE_ENCAP_TYPE]) {
1794 ret = true;
1795 ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]);
1796 }
1797
1798 if (data[IFLA_GRE_ENCAP_FLAGS]) {
1799 ret = true;
1800 ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]);
1801 }
1802
1803 if (data[IFLA_GRE_ENCAP_SPORT]) {
1804 ret = true;
1805 ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]);
1806 }
1807
1808 if (data[IFLA_GRE_ENCAP_DPORT]) {
1809 ret = true;
1810 ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]);
1811 }
1812
1813 return ret;
1814}
1815
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001816static int ip6gre_newlink(struct net *src_net, struct net_device *dev,
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02001817 struct nlattr *tb[], struct nlattr *data[],
1818 struct netlink_ext_ack *extack)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001819{
1820 struct ip6_tnl *nt;
1821 struct net *net = dev_net(dev);
1822 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
Tom Herbert1faf3d92016-05-18 09:06:19 -07001823 struct ip_tunnel_encap ipencap;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001824 int err;
1825
1826 nt = netdev_priv(dev);
Tom Herbert1faf3d92016-05-18 09:06:19 -07001827
1828 if (ip6gre_netlink_encap_parms(data, &ipencap)) {
1829 int err = ip6_tnl_encap_setup(nt, &ipencap);
1830
1831 if (err < 0)
1832 return err;
1833 }
1834
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001835 ip6gre_netlink_parms(data, &nt->parms);
1836
William Tu6712abc2017-12-01 15:26:08 -08001837 if (nt->parms.collect_md) {
1838 if (rtnl_dereference(ign->collect_md_tun))
1839 return -EEXIST;
1840 } else {
1841 if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
1842 return -EEXIST;
1843 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001844
1845 if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
1846 eth_hw_addr_random(dev);
1847
1848 nt->dev = dev;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001849 nt->net = dev_net(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001850 ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]);
1851
Alexander Duyckac4eb002016-04-14 15:33:51 -04001852 dev->features |= GRE6_FEATURES;
1853 dev->hw_features |= GRE6_FEATURES;
1854
Tom Herbertb45bd1d2016-05-09 17:12:12 -07001855 if (!(nt->parms.o_flags & TUNNEL_SEQ)) {
Alexander Duyck6a553682016-05-18 10:44:47 -07001856 /* TCP offload with GRE SEQ is not supported, nor
1857 * can we support 2 levels of outer headers requiring
1858 * an update.
Alexander Duyck3a80e1f2016-04-14 15:34:04 -04001859 */
Alexander Duyck6a553682016-05-18 10:44:47 -07001860 if (!(nt->parms.o_flags & TUNNEL_CSUM) ||
1861 (nt->encap.type == TUNNEL_ENCAP_NONE)) {
1862 dev->features |= NETIF_F_GSO_SOFTWARE;
1863 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
1864 }
Alexander Duyck3a80e1f2016-04-14 15:34:04 -04001865
1866 /* Can use a lockless transmit, unless we generate
1867 * output sequences
1868 */
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001869 dev->features |= NETIF_F_LLTX;
Alexander Duyck3a80e1f2016-04-14 15:34:04 -04001870 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001871
1872 err = register_netdevice(dev);
1873 if (err)
1874 goto out;
1875
1876 dev_hold(dev);
1877 ip6gre_tunnel_link(ign, nt);
1878
1879out:
1880 return err;
1881}
1882
1883static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],
Matthias Schifferad744b22017-06-25 23:56:00 +02001884 struct nlattr *data[],
1885 struct netlink_ext_ack *extack)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001886{
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001887 struct ip6_tnl *t, *nt = netdev_priv(dev);
1888 struct net *net = nt->net;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001889 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1890 struct __ip6_tnl_parm p;
Tom Herbert1faf3d92016-05-18 09:06:19 -07001891 struct ip_tunnel_encap ipencap;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001892
1893 if (dev == ign->fb_tunnel_dev)
1894 return -EINVAL;
1895
Tom Herbert1faf3d92016-05-18 09:06:19 -07001896 if (ip6gre_netlink_encap_parms(data, &ipencap)) {
1897 int err = ip6_tnl_encap_setup(nt, &ipencap);
1898
1899 if (err < 0)
1900 return err;
1901 }
1902
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001903 ip6gre_netlink_parms(data, &p);
1904
1905 t = ip6gre_tunnel_locate(net, &p, 0);
1906
1907 if (t) {
1908 if (t->dev != dev)
1909 return -EEXIST;
1910 } else {
1911 t = nt;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001912 }
1913
Nicolas Dichtel6a61d4d2015-12-03 17:21:50 +01001914 ip6gre_tunnel_unlink(ign, t);
1915 ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]);
1916 ip6gre_tunnel_link(ign, t);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001917 return 0;
1918}
1919
Nicolas Dichtel54d63f782014-04-14 17:11:38 +02001920static void ip6gre_dellink(struct net_device *dev, struct list_head *head)
1921{
1922 struct net *net = dev_net(dev);
1923 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1924
1925 if (dev != ign->fb_tunnel_dev)
1926 unregister_netdevice_queue(dev, head);
1927}
1928
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001929static size_t ip6gre_get_size(const struct net_device *dev)
1930{
1931 return
1932 /* IFLA_GRE_LINK */
1933 nla_total_size(4) +
1934 /* IFLA_GRE_IFLAGS */
1935 nla_total_size(2) +
1936 /* IFLA_GRE_OFLAGS */
1937 nla_total_size(2) +
1938 /* IFLA_GRE_IKEY */
1939 nla_total_size(4) +
1940 /* IFLA_GRE_OKEY */
1941 nla_total_size(4) +
1942 /* IFLA_GRE_LOCAL */
Nicolas Dichtela3754132012-11-09 05:34:56 +00001943 nla_total_size(sizeof(struct in6_addr)) +
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001944 /* IFLA_GRE_REMOTE */
Nicolas Dichtela3754132012-11-09 05:34:56 +00001945 nla_total_size(sizeof(struct in6_addr)) +
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001946 /* IFLA_GRE_TTL */
1947 nla_total_size(1) +
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001948 /* IFLA_GRE_ENCAP_LIMIT */
1949 nla_total_size(1) +
1950 /* IFLA_GRE_FLOWINFO */
1951 nla_total_size(4) +
1952 /* IFLA_GRE_FLAGS */
1953 nla_total_size(4) +
Tom Herbert1faf3d92016-05-18 09:06:19 -07001954 /* IFLA_GRE_ENCAP_TYPE */
1955 nla_total_size(2) +
1956 /* IFLA_GRE_ENCAP_FLAGS */
1957 nla_total_size(2) +
1958 /* IFLA_GRE_ENCAP_SPORT */
1959 nla_total_size(2) +
1960 /* IFLA_GRE_ENCAP_DPORT */
1961 nla_total_size(2) +
William Tu6712abc2017-12-01 15:26:08 -08001962 /* IFLA_GRE_COLLECT_METADATA */
1963 nla_total_size(0) +
Craig Gallek0a473b82017-04-19 12:30:53 -04001964 /* IFLA_GRE_FWMARK */
1965 nla_total_size(4) +
William Tu5a963eb2017-11-30 11:51:29 -08001966 /* IFLA_GRE_ERSPAN_INDEX */
1967 nla_total_size(4) +
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001968 0;
1969}
1970
1971static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev)
1972{
1973 struct ip6_tnl *t = netdev_priv(dev);
1974 struct __ip6_tnl_parm *p = &t->parms;
1975
1976 if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
Tom Herbertf41fe3c2016-05-09 17:12:09 -07001977 nla_put_be16(skb, IFLA_GRE_IFLAGS,
1978 gre_tnl_flags_to_gre_flags(p->i_flags)) ||
1979 nla_put_be16(skb, IFLA_GRE_OFLAGS,
1980 gre_tnl_flags_to_gre_flags(p->o_flags)) ||
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001981 nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
1982 nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
Jiri Benc930345e2015-03-29 16:59:25 +02001983 nla_put_in6_addr(skb, IFLA_GRE_LOCAL, &p->laddr) ||
1984 nla_put_in6_addr(skb, IFLA_GRE_REMOTE, &p->raddr) ||
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001985 nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) ||
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001986 nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) ||
1987 nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) ||
Craig Gallek0a473b82017-04-19 12:30:53 -04001988 nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags) ||
William Tu5a963eb2017-11-30 11:51:29 -08001989 nla_put_u32(skb, IFLA_GRE_FWMARK, p->fwmark) ||
1990 nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, p->index))
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001991 goto nla_put_failure;
Tom Herbert1faf3d92016-05-18 09:06:19 -07001992
1993 if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE,
1994 t->encap.type) ||
1995 nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT,
1996 t->encap.sport) ||
1997 nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT,
1998 t->encap.dport) ||
1999 nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS,
2000 t->encap.flags))
2001 goto nla_put_failure;
2002
William Tu6712abc2017-12-01 15:26:08 -08002003 if (p->collect_md) {
2004 if (nla_put_flag(skb, IFLA_GRE_COLLECT_METADATA))
2005 goto nla_put_failure;
2006 }
2007
William Tu94d7d8f2017-12-13 16:38:57 -08002008 if (nla_put_u8(skb, IFLA_GRE_ERSPAN_VER, p->erspan_ver))
2009 goto nla_put_failure;
2010
2011 if (p->erspan_ver == 1) {
2012 if (nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, p->index))
2013 goto nla_put_failure;
2014 } else if (p->erspan_ver == 2) {
2015 if (nla_put_u8(skb, IFLA_GRE_ERSPAN_DIR, p->dir))
2016 goto nla_put_failure;
2017 if (nla_put_u16(skb, IFLA_GRE_ERSPAN_HWID, p->hwid))
2018 goto nla_put_failure;
2019 }
2020
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002021 return 0;
2022
2023nla_put_failure:
2024 return -EMSGSIZE;
2025}
2026
2027static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = {
2028 [IFLA_GRE_LINK] = { .type = NLA_U32 },
2029 [IFLA_GRE_IFLAGS] = { .type = NLA_U16 },
2030 [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
2031 [IFLA_GRE_IKEY] = { .type = NLA_U32 },
2032 [IFLA_GRE_OKEY] = { .type = NLA_U32 },
2033 [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct ipv6hdr, saddr) },
2034 [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct ipv6hdr, daddr) },
2035 [IFLA_GRE_TTL] = { .type = NLA_U8 },
2036 [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 },
2037 [IFLA_GRE_FLOWINFO] = { .type = NLA_U32 },
2038 [IFLA_GRE_FLAGS] = { .type = NLA_U32 },
Tom Herbert1faf3d92016-05-18 09:06:19 -07002039 [IFLA_GRE_ENCAP_TYPE] = { .type = NLA_U16 },
2040 [IFLA_GRE_ENCAP_FLAGS] = { .type = NLA_U16 },
2041 [IFLA_GRE_ENCAP_SPORT] = { .type = NLA_U16 },
2042 [IFLA_GRE_ENCAP_DPORT] = { .type = NLA_U16 },
William Tu6712abc2017-12-01 15:26:08 -08002043 [IFLA_GRE_COLLECT_METADATA] = { .type = NLA_FLAG },
Craig Gallek0a473b82017-04-19 12:30:53 -04002044 [IFLA_GRE_FWMARK] = { .type = NLA_U32 },
William Tu5a963eb2017-11-30 11:51:29 -08002045 [IFLA_GRE_ERSPAN_INDEX] = { .type = NLA_U32 },
William Tu94d7d8f2017-12-13 16:38:57 -08002046 [IFLA_GRE_ERSPAN_VER] = { .type = NLA_U8 },
2047 [IFLA_GRE_ERSPAN_DIR] = { .type = NLA_U8 },
2048 [IFLA_GRE_ERSPAN_HWID] = { .type = NLA_U16 },
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002049};
2050
William Tu5a963eb2017-11-30 11:51:29 -08002051static void ip6erspan_tap_setup(struct net_device *dev)
2052{
2053 ether_setup(dev);
2054
2055 dev->netdev_ops = &ip6erspan_netdev_ops;
2056 dev->needs_free_netdev = true;
2057 dev->priv_destructor = ip6gre_dev_free;
2058
2059 dev->features |= NETIF_F_NETNS_LOCAL;
2060 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
2061 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2062 netif_keep_dst(dev);
2063}
2064
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002065static struct rtnl_link_ops ip6gre_link_ops __read_mostly = {
2066 .kind = "ip6gre",
2067 .maxtype = IFLA_GRE_MAX,
2068 .policy = ip6gre_policy,
2069 .priv_size = sizeof(struct ip6_tnl),
2070 .setup = ip6gre_tunnel_setup,
2071 .validate = ip6gre_tunnel_validate,
2072 .newlink = ip6gre_newlink,
2073 .changelink = ip6gre_changelink,
Nicolas Dichtel54d63f782014-04-14 17:11:38 +02002074 .dellink = ip6gre_dellink,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002075 .get_size = ip6gre_get_size,
2076 .fill_info = ip6gre_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01002077 .get_link_net = ip6_tnl_get_link_net,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002078};
2079
2080static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = {
2081 .kind = "ip6gretap",
2082 .maxtype = IFLA_GRE_MAX,
2083 .policy = ip6gre_policy,
2084 .priv_size = sizeof(struct ip6_tnl),
2085 .setup = ip6gre_tap_setup,
2086 .validate = ip6gre_tap_validate,
2087 .newlink = ip6gre_newlink,
2088 .changelink = ip6gre_changelink,
2089 .get_size = ip6gre_get_size,
2090 .fill_info = ip6gre_fill_info,
Nicolas Dichtel3390e392015-01-20 15:15:43 +01002091 .get_link_net = ip6_tnl_get_link_net,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002092};
2093
William Tu5a963eb2017-11-30 11:51:29 -08002094static struct rtnl_link_ops ip6erspan_tap_ops __read_mostly = {
2095 .kind = "ip6erspan",
2096 .maxtype = IFLA_GRE_MAX,
2097 .policy = ip6gre_policy,
2098 .priv_size = sizeof(struct ip6_tnl),
2099 .setup = ip6erspan_tap_setup,
2100 .validate = ip6erspan_tap_validate,
2101 .newlink = ip6gre_newlink,
2102 .changelink = ip6gre_changelink,
2103 .get_size = ip6gre_get_size,
2104 .fill_info = ip6gre_fill_info,
2105 .get_link_net = ip6_tnl_get_link_net,
2106};
2107
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002108/*
2109 * And now the modules code and kernel interface.
2110 */
2111
2112static int __init ip6gre_init(void)
2113{
2114 int err;
2115
2116 pr_info("GRE over IPv6 tunneling driver\n");
2117
2118 err = register_pernet_device(&ip6gre_net_ops);
2119 if (err < 0)
2120 return err;
2121
2122 err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE);
2123 if (err < 0) {
2124 pr_info("%s: can't add protocol\n", __func__);
2125 goto add_proto_failed;
2126 }
2127
2128 err = rtnl_link_register(&ip6gre_link_ops);
2129 if (err < 0)
2130 goto rtnl_link_failed;
2131
2132 err = rtnl_link_register(&ip6gre_tap_ops);
2133 if (err < 0)
2134 goto tap_ops_failed;
2135
William Tu5a963eb2017-11-30 11:51:29 -08002136 err = rtnl_link_register(&ip6erspan_tap_ops);
2137 if (err < 0)
2138 goto erspan_link_failed;
2139
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002140out:
2141 return err;
2142
William Tu5a963eb2017-11-30 11:51:29 -08002143erspan_link_failed:
2144 rtnl_link_unregister(&ip6gre_tap_ops);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002145tap_ops_failed:
2146 rtnl_link_unregister(&ip6gre_link_ops);
2147rtnl_link_failed:
2148 inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
2149add_proto_failed:
2150 unregister_pernet_device(&ip6gre_net_ops);
2151 goto out;
2152}
2153
2154static void __exit ip6gre_fini(void)
2155{
2156 rtnl_link_unregister(&ip6gre_tap_ops);
2157 rtnl_link_unregister(&ip6gre_link_ops);
William Tu5a963eb2017-11-30 11:51:29 -08002158 rtnl_link_unregister(&ip6erspan_tap_ops);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002159 inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
2160 unregister_pernet_device(&ip6gre_net_ops);
2161}
2162
2163module_init(ip6gre_init);
2164module_exit(ip6gre_fini);
2165MODULE_LICENSE("GPL");
2166MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
2167MODULE_DESCRIPTION("GRE over IPv6 tunneling device");
2168MODULE_ALIAS_RTNL_LINK("ip6gre");
Nicolas Dichtel5a4ee9a2014-09-24 11:03:00 +02002169MODULE_ALIAS_RTNL_LINK("ip6gretap");
William Tu94d7d8f2017-12-13 16:38:57 -08002170MODULE_ALIAS_RTNL_LINK("ip6erspan");
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002171MODULE_ALIAS_NETDEV("ip6gre0");