blob: 1bbd0930063eeca7a75aab193a53fdae5f9e973b [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) ||
William Tu3b04caa2018-03-09 07:34:40 -0800129 gre_proto == htons(ETH_P_ERSPAN) ||
130 gre_proto == htons(ETH_P_ERSPAN2)) ?
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000131 ARPHRD_ETHER : ARPHRD_IP6GRE;
132 int score, cand_score = 4;
133
Amerigo Wange086cad2012-11-11 21:52:34 +0000134 for_each_ip_tunnel_rcu(t, ign->tunnels_r_l[h0 ^ h1]) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000135 if (!ipv6_addr_equal(local, &t->parms.laddr) ||
136 !ipv6_addr_equal(remote, &t->parms.raddr) ||
137 key != t->parms.i_key ||
138 !(t->dev->flags & IFF_UP))
139 continue;
140
141 if (t->dev->type != ARPHRD_IP6GRE &&
142 t->dev->type != dev_type)
143 continue;
144
145 score = 0;
146 if (t->parms.link != link)
147 score |= 1;
148 if (t->dev->type != dev_type)
149 score |= 2;
150 if (score == 0)
151 return t;
152
153 if (score < cand_score) {
154 cand = t;
155 cand_score = score;
156 }
157 }
158
Amerigo Wange086cad2012-11-11 21:52:34 +0000159 for_each_ip_tunnel_rcu(t, ign->tunnels_r[h0 ^ h1]) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000160 if (!ipv6_addr_equal(remote, &t->parms.raddr) ||
161 key != t->parms.i_key ||
162 !(t->dev->flags & IFF_UP))
163 continue;
164
165 if (t->dev->type != ARPHRD_IP6GRE &&
166 t->dev->type != dev_type)
167 continue;
168
169 score = 0;
170 if (t->parms.link != link)
171 score |= 1;
172 if (t->dev->type != dev_type)
173 score |= 2;
174 if (score == 0)
175 return t;
176
177 if (score < cand_score) {
178 cand = t;
179 cand_score = score;
180 }
181 }
182
Amerigo Wange086cad2012-11-11 21:52:34 +0000183 for_each_ip_tunnel_rcu(t, ign->tunnels_l[h1]) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000184 if ((!ipv6_addr_equal(local, &t->parms.laddr) &&
185 (!ipv6_addr_equal(local, &t->parms.raddr) ||
186 !ipv6_addr_is_multicast(local))) ||
187 key != t->parms.i_key ||
188 !(t->dev->flags & IFF_UP))
189 continue;
190
191 if (t->dev->type != ARPHRD_IP6GRE &&
192 t->dev->type != dev_type)
193 continue;
194
195 score = 0;
196 if (t->parms.link != link)
197 score |= 1;
198 if (t->dev->type != dev_type)
199 score |= 2;
200 if (score == 0)
201 return t;
202
203 if (score < cand_score) {
204 cand = t;
205 cand_score = score;
206 }
207 }
208
Amerigo Wange086cad2012-11-11 21:52:34 +0000209 for_each_ip_tunnel_rcu(t, ign->tunnels_wc[h1]) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000210 if (t->parms.i_key != key ||
211 !(t->dev->flags & IFF_UP))
212 continue;
213
214 if (t->dev->type != ARPHRD_IP6GRE &&
215 t->dev->type != dev_type)
216 continue;
217
218 score = 0;
219 if (t->parms.link != link)
220 score |= 1;
221 if (t->dev->type != dev_type)
222 score |= 2;
223 if (score == 0)
224 return t;
225
226 if (score < cand_score) {
227 cand = t;
228 cand_score = score;
229 }
230 }
231
Ian Morris53b24b82015-03-29 14:00:05 +0100232 if (cand)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000233 return cand;
234
William Tu6712abc2017-12-01 15:26:08 -0800235 t = rcu_dereference(ign->collect_md_tun);
236 if (t && t->dev->flags & IFF_UP)
237 return t;
238
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000239 dev = ign->fb_tunnel_dev;
240 if (dev->flags & IFF_UP)
241 return netdev_priv(dev);
242
243 return NULL;
244}
245
246static struct ip6_tnl __rcu **__ip6gre_bucket(struct ip6gre_net *ign,
247 const struct __ip6_tnl_parm *p)
248{
249 const struct in6_addr *remote = &p->raddr;
250 const struct in6_addr *local = &p->laddr;
251 unsigned int h = HASH_KEY(p->i_key);
252 int prio = 0;
253
254 if (!ipv6_addr_any(local))
255 prio |= 1;
256 if (!ipv6_addr_any(remote) && !ipv6_addr_is_multicast(remote)) {
257 prio |= 2;
258 h ^= HASH_ADDR(remote);
259 }
260
261 return &ign->tunnels[prio][h];
262}
263
264static inline struct ip6_tnl __rcu **ip6gre_bucket(struct ip6gre_net *ign,
265 const struct ip6_tnl *t)
266{
267 return __ip6gre_bucket(ign, &t->parms);
268}
269
270static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t)
271{
272 struct ip6_tnl __rcu **tp = ip6gre_bucket(ign, t);
273
William Tu6712abc2017-12-01 15:26:08 -0800274 if (t->parms.collect_md)
275 rcu_assign_pointer(ign->collect_md_tun, t);
276
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000277 rcu_assign_pointer(t->next, rtnl_dereference(*tp));
278 rcu_assign_pointer(*tp, t);
279}
280
281static void ip6gre_tunnel_unlink(struct ip6gre_net *ign, struct ip6_tnl *t)
282{
283 struct ip6_tnl __rcu **tp;
284 struct ip6_tnl *iter;
285
William Tu6712abc2017-12-01 15:26:08 -0800286 if (t->parms.collect_md)
287 rcu_assign_pointer(ign->collect_md_tun, NULL);
288
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000289 for (tp = ip6gre_bucket(ign, t);
290 (iter = rtnl_dereference(*tp)) != NULL;
291 tp = &iter->next) {
292 if (t == iter) {
293 rcu_assign_pointer(*tp, t->next);
294 break;
295 }
296 }
297}
298
299static struct ip6_tnl *ip6gre_tunnel_find(struct net *net,
300 const struct __ip6_tnl_parm *parms,
301 int type)
302{
303 const struct in6_addr *remote = &parms->raddr;
304 const struct in6_addr *local = &parms->laddr;
305 __be32 key = parms->i_key;
306 int link = parms->link;
307 struct ip6_tnl *t;
308 struct ip6_tnl __rcu **tp;
309 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
310
311 for (tp = __ip6gre_bucket(ign, parms);
312 (t = rtnl_dereference(*tp)) != NULL;
313 tp = &t->next)
314 if (ipv6_addr_equal(local, &t->parms.laddr) &&
315 ipv6_addr_equal(remote, &t->parms.raddr) &&
316 key == t->parms.i_key &&
317 link == t->parms.link &&
318 type == t->dev->type)
319 break;
320
321 return t;
322}
323
324static struct ip6_tnl *ip6gre_tunnel_locate(struct net *net,
325 const struct __ip6_tnl_parm *parms, int create)
326{
327 struct ip6_tnl *t, *nt;
328 struct net_device *dev;
329 char name[IFNAMSIZ];
330 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
331
332 t = ip6gre_tunnel_find(net, parms, ARPHRD_IP6GRE);
Steffen Klassertcd0a0bd2014-09-22 10:07:26 +0200333 if (t && create)
334 return NULL;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000335 if (t || !create)
336 return t;
337
338 if (parms->name[0])
339 strlcpy(name, parms->name, IFNAMSIZ);
340 else
341 strcpy(name, "ip6gre%d");
342
Tom Gundersenc835a672014-07-14 16:37:24 +0200343 dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
344 ip6gre_tunnel_setup);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000345 if (!dev)
346 return NULL;
347
348 dev_net_set(dev, net);
349
350 nt = netdev_priv(dev);
351 nt->parms = *parms;
352 dev->rtnl_link_ops = &ip6gre_link_ops;
353
354 nt->dev = dev;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +0200355 nt->net = dev_net(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000356
357 if (register_netdevice(dev) < 0)
358 goto failed_free;
359
Alexey Kodanev128bb972018-01-18 20:51:12 +0300360 ip6gre_tnl_link_config(nt, 1);
361
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000362 /* Can use a lockless transmit, unless we generate output sequences */
Tom Herbertb45bd1d2016-05-09 17:12:12 -0700363 if (!(nt->parms.o_flags & TUNNEL_SEQ))
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000364 dev->features |= NETIF_F_LLTX;
365
366 dev_hold(dev);
367 ip6gre_tunnel_link(ign, nt);
368 return nt;
369
370failed_free:
371 free_netdev(dev);
372 return NULL;
373}
374
375static void ip6gre_tunnel_uninit(struct net_device *dev)
376{
Nicolas Dichtel22f08062014-04-22 10:15:24 +0200377 struct ip6_tnl *t = netdev_priv(dev);
378 struct ip6gre_net *ign = net_generic(t->net, ip6gre_net_id);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000379
Nicolas Dichtel22f08062014-04-22 10:15:24 +0200380 ip6gre_tunnel_unlink(ign, t);
Paolo Abeni607f7252016-02-12 15:43:54 +0100381 dst_cache_reset(&t->dst_cache);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000382 dev_put(dev);
383}
384
385
386static void ip6gre_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Eric Dumazet78920322017-02-04 23:18:55 -0800387 u8 type, u8 code, int offset, __be32 info)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000388{
Xin Long929fc032017-11-11 19:06:49 +0800389 struct net *net = dev_net(skb->dev);
Eric Dumazet78920322017-02-04 23:18:55 -0800390 const struct gre_base_hdr *greh;
391 const struct ipv6hdr *ipv6h;
392 int grehlen = sizeof(*greh);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000393 struct ip6_tnl *t;
Eric Dumazet78920322017-02-04 23:18:55 -0800394 int key_off = 0;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000395 __be16 flags;
Eric Dumazet78920322017-02-04 23:18:55 -0800396 __be32 key;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000397
Eric Dumazet78920322017-02-04 23:18:55 -0800398 if (!pskb_may_pull(skb, offset + grehlen))
399 return;
400 greh = (const struct gre_base_hdr *)(skb->data + offset);
401 flags = greh->flags;
402 if (flags & (GRE_VERSION | GRE_ROUTING))
403 return;
404 if (flags & GRE_CSUM)
405 grehlen += 4;
406 if (flags & GRE_KEY) {
407 key_off = grehlen + offset;
408 grehlen += 4;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000409 }
410
Eric Dumazet78920322017-02-04 23:18:55 -0800411 if (!pskb_may_pull(skb, offset + grehlen))
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000412 return;
Eric Dumazetb87fb392012-08-19 03:47:30 +0000413 ipv6h = (const struct ipv6hdr *)skb->data;
Eric Dumazet78920322017-02-04 23:18:55 -0800414 greh = (const struct gre_base_hdr *)(skb->data + offset);
415 key = key_off ? *(__be32 *)(skb->data + key_off) : 0;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000416
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000417 t = ip6gre_tunnel_lookup(skb->dev, &ipv6h->daddr, &ipv6h->saddr,
Eric Dumazet78920322017-02-04 23:18:55 -0800418 key, greh->protocol);
Ian Morris63159f22015-03-29 14:00:04 +0100419 if (!t)
stephen hemminger0c5794a2012-09-24 18:12:24 +0000420 return;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000421
422 switch (type) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000423 struct ipv6_tlv_tnl_enc_lim *tel;
Xin Longfe1a4ca2017-11-11 19:06:50 +0800424 __u32 teli;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000425 case ICMPV6_DEST_UNREACH:
Matt Bennetta46496c2015-09-23 16:58:31 +1200426 net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n",
427 t->parms.name);
Xin Longf8d20b42017-10-26 19:23:27 +0800428 if (code != ICMPV6_PORT_UNREACH)
429 break;
430 return;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000431 case ICMPV6_TIME_EXCEED:
432 if (code == ICMPV6_EXC_HOPLIMIT) {
Matt Bennetta46496c2015-09-23 16:58:31 +1200433 net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
434 t->parms.name);
Xin Longf8d20b42017-10-26 19:23:27 +0800435 break;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000436 }
Xin Longf8d20b42017-10-26 19:23:27 +0800437 return;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000438 case ICMPV6_PARAMPROB:
439 teli = 0;
440 if (code == ICMPV6_HDR_FIELD)
441 teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
442
Sabrina Dubrocad1e158e2015-02-04 15:25:09 +0100443 if (teli && teli == be32_to_cpu(info) - 2) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000444 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
445 if (tel->encap_limit == 0) {
Matt Bennetta46496c2015-09-23 16:58:31 +1200446 net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
447 t->parms.name);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000448 }
449 } else {
Matt Bennetta46496c2015-09-23 16:58:31 +1200450 net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
451 t->parms.name);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000452 }
Xin Longf8d20b42017-10-26 19:23:27 +0800453 return;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000454 case ICMPV6_PKT_TOOBIG:
Xin Longfe1a4ca2017-11-11 19:06:50 +0800455 ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
Xin Longf8d20b42017-10-26 19:23:27 +0800456 return;
Xin Long929fc032017-11-11 19:06:49 +0800457 case NDISC_REDIRECT:
458 ip6_redirect(skb, net, skb->dev->ifindex, 0,
459 sock_net_uid(net, NULL));
460 return;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000461 }
462
463 if (time_before(jiffies, t->err_time + IP6TUNNEL_ERR_TIMEO))
464 t->err_count++;
465 else
466 t->err_count = 1;
467 t->err_time = jiffies;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000468}
469
Tom Herbert308edfdf2016-04-29 17:12:17 -0700470static int ip6gre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000471{
472 const struct ipv6hdr *ipv6h;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000473 struct ip6_tnl *tunnel;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000474
475 ipv6h = ipv6_hdr(skb);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000476 tunnel = ip6gre_tunnel_lookup(skb->dev,
Tom Herbert308edfdf2016-04-29 17:12:17 -0700477 &ipv6h->saddr, &ipv6h->daddr, tpi->key,
478 tpi->proto);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000479 if (tunnel) {
William Tu6712abc2017-12-01 15:26:08 -0800480 if (tunnel->parms.collect_md) {
481 struct metadata_dst *tun_dst;
482 __be64 tun_id;
483 __be16 flags;
484
485 flags = tpi->flags;
486 tun_id = key32_to_tunnel_id(tpi->key);
487
488 tun_dst = ipv6_tun_rx_dst(skb, flags, tun_id, 0);
489 if (!tun_dst)
490 return PACKET_REJECT;
491
492 ip6_tnl_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
493 } else {
494 ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error);
495 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000496
Tom Herbert308edfdf2016-04-29 17:12:17 -0700497 return PACKET_RCVD;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000498 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000499
Tom Herbert308edfdf2016-04-29 17:12:17 -0700500 return PACKET_REJECT;
501}
502
William Tu5a963eb2017-11-30 11:51:29 -0800503static int ip6erspan_rcv(struct sk_buff *skb, int gre_hdr_len,
504 struct tnl_ptk_info *tpi)
505{
William Tu1d7e2ed2017-12-13 16:38:55 -0800506 struct erspan_base_hdr *ershdr;
507 struct erspan_metadata *pkt_md;
William Tu5a963eb2017-11-30 11:51:29 -0800508 const struct ipv6hdr *ipv6h;
William Tu3df192832018-02-05 13:35:34 -0800509 struct erspan_md2 *md2;
William Tu5a963eb2017-11-30 11:51:29 -0800510 struct ip6_tnl *tunnel;
William Tu1d7e2ed2017-12-13 16:38:55 -0800511 u8 ver;
William Tu5a963eb2017-11-30 11:51:29 -0800512
William Tu5a963eb2017-11-30 11:51:29 -0800513 if (unlikely(!pskb_may_pull(skb, sizeof(*ershdr))))
514 return PACKET_REJECT;
515
Haishuang Yan293a1992017-12-20 09:53:19 +0800516 ipv6h = ipv6_hdr(skb);
517 ershdr = (struct erspan_base_hdr *)skb->data;
William Tuc69de582018-01-25 13:20:09 -0800518 ver = ershdr->ver;
519 tpi->key = cpu_to_be32(get_session_id(ershdr));
William Tu5a963eb2017-11-30 11:51:29 -0800520
521 tunnel = ip6gre_tunnel_lookup(skb->dev,
522 &ipv6h->saddr, &ipv6h->daddr, tpi->key,
523 tpi->proto);
524 if (tunnel) {
William Tu1d7e2ed2017-12-13 16:38:55 -0800525 int len = erspan_hdr_len(ver);
526
527 if (unlikely(!pskb_may_pull(skb, len)))
William Tuae3e1332017-12-15 14:27:43 -0800528 return PACKET_REJECT;
William Tu1d7e2ed2017-12-13 16:38:55 -0800529
William Tud91e8db52017-12-15 14:27:44 -0800530 ershdr = (struct erspan_base_hdr *)skb->data;
531 pkt_md = (struct erspan_metadata *)(ershdr + 1);
532
William Tu1d7e2ed2017-12-13 16:38:55 -0800533 if (__iptunnel_pull_header(skb, len,
William Tu5a963eb2017-11-30 11:51:29 -0800534 htons(ETH_P_TEB),
535 false, false) < 0)
536 return PACKET_REJECT;
537
William Tuef7baf52017-12-05 15:15:44 -0800538 if (tunnel->parms.collect_md) {
539 struct metadata_dst *tun_dst;
540 struct ip_tunnel_info *info;
541 struct erspan_metadata *md;
542 __be64 tun_id;
543 __be16 flags;
544
545 tpi->flags |= TUNNEL_KEY;
546 flags = tpi->flags;
547 tun_id = key32_to_tunnel_id(tpi->key);
548
549 tun_dst = ipv6_tun_rx_dst(skb, flags, tun_id,
550 sizeof(*md));
551 if (!tun_dst)
552 return PACKET_REJECT;
553
554 info = &tun_dst->u.tun_info;
555 md = ip_tunnel_info_opts(info);
William Tu94d7d8f2017-12-13 16:38:57 -0800556 md->version = ver;
William Tu3df192832018-02-05 13:35:34 -0800557 md2 = &md->u.md2;
558 memcpy(md2, pkt_md, ver == 1 ? ERSPAN_V1_MDSIZE :
559 ERSPAN_V2_MDSIZE);
William Tuef7baf52017-12-05 15:15:44 -0800560 info->key.tun_flags |= TUNNEL_ERSPAN_OPT;
561 info->options_len = sizeof(*md);
562
563 ip6_tnl_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
564
565 } else {
William Tuef7baf52017-12-05 15:15:44 -0800566 ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error);
567 }
William Tu5a963eb2017-11-30 11:51:29 -0800568
569 return PACKET_RCVD;
570 }
571
572 return PACKET_REJECT;
573}
574
Tom Herbert308edfdf2016-04-29 17:12:17 -0700575static int gre_rcv(struct sk_buff *skb)
576{
577 struct tnl_ptk_info tpi;
578 bool csum_err = false;
579 int hdr_len;
580
Eric Dumazete582615ad2016-06-15 06:24:00 -0700581 hdr_len = gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IPV6), 0);
Jiri Bencf132ae72016-05-03 15:00:21 +0200582 if (hdr_len < 0)
Tom Herbert308edfdf2016-04-29 17:12:17 -0700583 goto drop;
584
585 if (iptunnel_pull_header(skb, hdr_len, tpi.proto, false))
586 goto drop;
587
William Tu94d7d8f2017-12-13 16:38:57 -0800588 if (unlikely(tpi.proto == htons(ETH_P_ERSPAN) ||
589 tpi.proto == htons(ETH_P_ERSPAN2))) {
William Tu5a963eb2017-11-30 11:51:29 -0800590 if (ip6erspan_rcv(skb, hdr_len, &tpi) == PACKET_RCVD)
591 return 0;
Haishuang Yana7343212017-12-20 10:21:47 +0800592 goto out;
William Tu5a963eb2017-11-30 11:51:29 -0800593 }
594
Tom Herbert308edfdf2016-04-29 17:12:17 -0700595 if (ip6gre_rcv(skb, &tpi) == PACKET_RCVD)
596 return 0;
597
Haishuang Yana7343212017-12-20 10:21:47 +0800598out:
Tom Herbert308edfdf2016-04-29 17:12:17 -0700599 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000600drop:
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000601 kfree_skb(skb);
602 return 0;
603}
604
Tom Herbertb05229f2016-04-29 17:12:21 -0700605static int gre_handle_offloads(struct sk_buff *skb, bool csum)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000606{
Tom Herbertb05229f2016-04-29 17:12:21 -0700607 return iptunnel_handle_offloads(skb,
608 csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000609}
610
William Tu898b29792017-11-30 11:51:28 -0800611static void prepare_ip6gre_xmit_ipv4(struct sk_buff *skb,
612 struct net_device *dev,
613 struct flowi6 *fl6, __u8 *dsfield,
614 int *encap_limit)
615{
616 const struct iphdr *iph = ip_hdr(skb);
617 struct ip6_tnl *t = netdev_priv(dev);
618
619 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
620 *encap_limit = t->parms.encap_limit;
621
622 memcpy(fl6, &t->fl.u.ip6, sizeof(*fl6));
623
624 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
625 *dsfield = ipv4_get_dsfield(iph);
626 else
627 *dsfield = ip6_tclass(t->parms.flowinfo);
628
629 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
630 fl6->flowi6_mark = skb->mark;
631 else
632 fl6->flowi6_mark = t->parms.fwmark;
633
634 fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
635}
636
637static int prepare_ip6gre_xmit_ipv6(struct sk_buff *skb,
638 struct net_device *dev,
639 struct flowi6 *fl6, __u8 *dsfield,
640 int *encap_limit)
641{
642 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
643 struct ip6_tnl *t = netdev_priv(dev);
644 __u16 offset;
645
646 offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
647 /* ip6_tnl_parse_tlv_enc_lim() might have reallocated skb->head */
648
649 if (offset > 0) {
650 struct ipv6_tlv_tnl_enc_lim *tel;
651
652 tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
653 if (tel->encap_limit == 0) {
654 icmpv6_send(skb, ICMPV6_PARAMPROB,
655 ICMPV6_HDR_FIELD, offset + 2);
656 return -1;
657 }
658 *encap_limit = tel->encap_limit - 1;
659 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) {
660 *encap_limit = t->parms.encap_limit;
661 }
662
663 memcpy(fl6, &t->fl.u.ip6, sizeof(*fl6));
664
665 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
666 *dsfield = ipv6_get_dsfield(ipv6h);
667 else
668 *dsfield = ip6_tclass(t->parms.flowinfo);
669
670 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
671 fl6->flowlabel |= ip6_flowlabel(ipv6h);
672
673 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
674 fl6->flowi6_mark = skb->mark;
675 else
676 fl6->flowi6_mark = t->parms.fwmark;
677
678 fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
679
680 return 0;
681}
682
Tom Herbertb05229f2016-04-29 17:12:21 -0700683static netdev_tx_t __gre6_xmit(struct sk_buff *skb,
684 struct net_device *dev, __u8 dsfield,
685 struct flowi6 *fl6, int encap_limit,
686 __u32 *pmtu, __be16 proto)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000687{
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000688 struct ip6_tnl *tunnel = netdev_priv(dev);
Xin Long8aec4952017-10-26 19:27:17 +0800689 __be16 protocol;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000690
691 if (dev->type == ARPHRD_ETHER)
692 IPCB(skb)->flags = 0;
693
Tom Herbertb05229f2016-04-29 17:12:21 -0700694 if (dev->header_ops && dev->type == ARPHRD_IP6GRE)
695 fl6->daddr = ((struct ipv6hdr *)skb->data)->daddr;
696 else
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000697 fl6->daddr = tunnel->parms.raddr;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000698
Tom Herbertb05229f2016-04-29 17:12:21 -0700699 if (tunnel->parms.o_flags & TUNNEL_SEQ)
700 tunnel->o_seqno++;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000701
Tom Herbertb05229f2016-04-29 17:12:21 -0700702 /* Push GRE header. */
Xin Long8aec4952017-10-26 19:27:17 +0800703 protocol = (dev->type == ARPHRD_ETHER) ? htons(ETH_P_TEB) : proto;
William Tu6712abc2017-12-01 15:26:08 -0800704
705 if (tunnel->parms.collect_md) {
706 struct ip_tunnel_info *tun_info;
707 const struct ip_tunnel_key *key;
708 __be16 flags;
709
710 tun_info = skb_tunnel_info(skb);
711 if (unlikely(!tun_info ||
712 !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
713 ip_tunnel_info_af(tun_info) != AF_INET6))
714 return -EINVAL;
715
716 key = &tun_info->key;
717 memset(fl6, 0, sizeof(*fl6));
718 fl6->flowi6_proto = IPPROTO_GRE;
719 fl6->daddr = key->u.ipv6.dst;
720 fl6->flowlabel = key->label;
721 fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
722
723 dsfield = key->tos;
724 flags = key->tun_flags & (TUNNEL_CSUM | TUNNEL_KEY);
725 tunnel->tun_hlen = gre_calc_hlen(flags);
726
727 gre_build_header(skb, tunnel->tun_hlen,
728 flags, protocol,
729 tunnel_id_to_key32(tun_info->key.tun_id), 0);
730
731 } else {
732 gre_build_header(skb, tunnel->tun_hlen, tunnel->parms.o_flags,
733 protocol, tunnel->parms.o_key,
734 htonl(tunnel->o_seqno));
735 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000736
Tom Herbertb05229f2016-04-29 17:12:21 -0700737 return ip6_tnl_xmit(skb, dev, dsfield, fl6, encap_limit, pmtu,
738 NEXTHDR_GRE);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000739}
740
741static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev)
742{
743 struct ip6_tnl *t = netdev_priv(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000744 int encap_limit = -1;
745 struct flowi6 fl6;
William Tu6712abc2017-12-01 15:26:08 -0800746 __u8 dsfield = 0;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000747 __u32 mtu;
748 int err;
749
Bernie Harris5146d1f2016-02-22 12:58:05 +1300750 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
751
William Tu6712abc2017-12-01 15:26:08 -0800752 if (!t->parms.collect_md)
753 prepare_ip6gre_xmit_ipv4(skb, dev, &fl6,
754 &dsfield, &encap_limit);
Lorenzo Colittie2d118a2016-11-04 02:23:43 +0900755
Tom Herbertb05229f2016-04-29 17:12:21 -0700756 err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
757 if (err)
758 return -1;
759
760 err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
761 skb->protocol);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000762 if (err != 0) {
763 /* XXX: send ICMP error even if DF is not set. */
764 if (err == -EMSGSIZE)
765 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
766 htonl(mtu));
767 return -1;
768 }
769
770 return 0;
771}
772
773static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev)
774{
775 struct ip6_tnl *t = netdev_priv(dev);
776 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
777 int encap_limit = -1;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000778 struct flowi6 fl6;
William Tu6712abc2017-12-01 15:26:08 -0800779 __u8 dsfield = 0;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000780 __u32 mtu;
781 int err;
782
783 if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
784 return -1;
785
William Tu6712abc2017-12-01 15:26:08 -0800786 if (!t->parms.collect_md &&
787 prepare_ip6gre_xmit_ipv6(skb, dev, &fl6, &dsfield, &encap_limit))
William Tu898b29792017-11-30 11:51:28 -0800788 return -1;
Lorenzo Colittie2d118a2016-11-04 02:23:43 +0900789
Tom Herbertb05229f2016-04-29 17:12:21 -0700790 if (gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM)))
791 return -1;
792
793 err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit,
794 &mtu, skb->protocol);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000795 if (err != 0) {
796 if (err == -EMSGSIZE)
797 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
798 return -1;
799 }
800
801 return 0;
802}
803
804/**
805 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
806 * @t: the outgoing tunnel device
807 * @hdr: IPv6 header from the incoming packet
808 *
809 * Description:
810 * Avoid trivial tunneling loop by checking that tunnel exit-point
811 * doesn't match source of incoming packet.
812 *
813 * Return:
814 * 1 if conflict,
815 * 0 else
816 **/
817
818static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl *t,
819 const struct ipv6hdr *hdr)
820{
821 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
822}
823
824static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev)
825{
826 struct ip6_tnl *t = netdev_priv(dev);
827 int encap_limit = -1;
828 struct flowi6 fl6;
829 __u32 mtu;
830 int err;
831
832 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
833 encap_limit = t->parms.encap_limit;
834
William Tu6712abc2017-12-01 15:26:08 -0800835 if (!t->parms.collect_md)
836 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000837
Tom Herbertb05229f2016-04-29 17:12:21 -0700838 err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
839 if (err)
840 return err;
841
842 err = __gre6_xmit(skb, dev, 0, &fl6, encap_limit, &mtu, skb->protocol);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000843
844 return err;
845}
846
847static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb,
848 struct net_device *dev)
849{
850 struct ip6_tnl *t = netdev_priv(dev);
851 struct net_device_stats *stats = &t->dev->stats;
852 int ret;
853
Steffen Klassertd5005142014-11-05 08:02:48 +0100854 if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
Tommi Rantala41ab3e32013-02-06 03:24:02 +0000855 goto tx_err;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000856
857 switch (skb->protocol) {
858 case htons(ETH_P_IP):
859 ret = ip6gre_xmit_ipv4(skb, dev);
860 break;
861 case htons(ETH_P_IPV6):
862 ret = ip6gre_xmit_ipv6(skb, dev);
863 break;
864 default:
865 ret = ip6gre_xmit_other(skb, dev);
866 break;
867 }
868
869 if (ret < 0)
870 goto tx_err;
871
872 return NETDEV_TX_OK;
873
874tx_err:
875 stats->tx_errors++;
876 stats->tx_dropped++;
877 kfree_skb(skb);
878 return NETDEV_TX_OK;
879}
880
William Tu5a963eb2017-11-30 11:51:29 -0800881static netdev_tx_t ip6erspan_tunnel_xmit(struct sk_buff *skb,
882 struct net_device *dev)
883{
884 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
885 struct ip6_tnl *t = netdev_priv(dev);
886 struct dst_entry *dst = skb_dst(skb);
887 struct net_device_stats *stats;
888 bool truncate = false;
889 int encap_limit = -1;
890 __u8 dsfield = false;
891 struct flowi6 fl6;
892 int err = -EINVAL;
893 __u32 mtu;
894
895 if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
896 goto tx_err;
897
898 if (gre_handle_offloads(skb, false))
899 goto tx_err;
900
William Tu5a963eb2017-11-30 11:51:29 -0800901 if (skb->len > dev->mtu + dev->hard_header_len) {
902 pskb_trim(skb, dev->mtu + dev->hard_header_len);
903 truncate = true;
904 }
905
William Tue41c7c62018-03-09 07:34:42 -0800906 if (skb_cow_head(skb, dev->needed_headroom))
907 goto tx_err;
908
William Tu5a963eb2017-11-30 11:51:29 -0800909 t->parms.o_flags &= ~TUNNEL_KEY;
William Tu5a963eb2017-11-30 11:51:29 -0800910 IPCB(skb)->flags = 0;
William Tuef7baf52017-12-05 15:15:44 -0800911
912 /* For collect_md mode, derive fl6 from the tunnel key,
913 * for native mode, call prepare_ip6gre_xmit_{ipv4,ipv6}.
914 */
915 if (t->parms.collect_md) {
916 struct ip_tunnel_info *tun_info;
917 const struct ip_tunnel_key *key;
918 struct erspan_metadata *md;
William Tuc69de582018-01-25 13:20:09 -0800919 __be32 tun_id;
William Tuef7baf52017-12-05 15:15:44 -0800920
921 tun_info = skb_tunnel_info(skb);
922 if (unlikely(!tun_info ||
923 !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
924 ip_tunnel_info_af(tun_info) != AF_INET6))
925 return -EINVAL;
926
927 key = &tun_info->key;
928 memset(&fl6, 0, sizeof(fl6));
929 fl6.flowi6_proto = IPPROTO_GRE;
930 fl6.daddr = key->u.ipv6.dst;
931 fl6.flowlabel = key->label;
932 fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
933
934 dsfield = key->tos;
935 md = ip_tunnel_info_opts(tun_info);
936 if (!md)
937 goto tx_err;
938
William Tuc69de582018-01-25 13:20:09 -0800939 tun_id = tunnel_id_to_key32(key->tun_id);
William Tu94d7d8f2017-12-13 16:38:57 -0800940 if (md->version == 1) {
941 erspan_build_header(skb,
William Tuc69de582018-01-25 13:20:09 -0800942 ntohl(tun_id),
William Tu94d7d8f2017-12-13 16:38:57 -0800943 ntohl(md->u.index), truncate,
944 false);
945 } else if (md->version == 2) {
William Tu94d7d8f2017-12-13 16:38:57 -0800946 erspan_build_header_v2(skb,
William Tuc69de582018-01-25 13:20:09 -0800947 ntohl(tun_id),
948 md->u.md2.dir,
949 get_hwid(&md->u.md2),
950 truncate, false);
William Tud6aa7112018-03-09 07:34:41 -0800951 } else {
952 goto tx_err;
William Tu94d7d8f2017-12-13 16:38:57 -0800953 }
William Tuef7baf52017-12-05 15:15:44 -0800954 } else {
955 switch (skb->protocol) {
956 case htons(ETH_P_IP):
957 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
958 prepare_ip6gre_xmit_ipv4(skb, dev, &fl6,
959 &dsfield, &encap_limit);
960 break;
961 case htons(ETH_P_IPV6):
962 if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
963 goto tx_err;
964 if (prepare_ip6gre_xmit_ipv6(skb, dev, &fl6,
965 &dsfield, &encap_limit))
966 goto tx_err;
967 break;
968 default:
969 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
970 break;
971 }
972
William Tu94d7d8f2017-12-13 16:38:57 -0800973 if (t->parms.erspan_ver == 1)
William Tuc69de582018-01-25 13:20:09 -0800974 erspan_build_header(skb, ntohl(t->parms.o_key),
William Tu94d7d8f2017-12-13 16:38:57 -0800975 t->parms.index,
976 truncate, false);
977 else
William Tuc69de582018-01-25 13:20:09 -0800978 erspan_build_header_v2(skb, ntohl(t->parms.o_key),
William Tu94d7d8f2017-12-13 16:38:57 -0800979 t->parms.dir,
980 t->parms.hwid,
981 truncate, false);
William Tuef7baf52017-12-05 15:15:44 -0800982 fl6.daddr = t->parms.raddr;
983 }
William Tu5a963eb2017-11-30 11:51:29 -0800984
985 /* Push GRE header. */
986 gre_build_header(skb, 8, TUNNEL_SEQ,
987 htons(ETH_P_ERSPAN), 0, htonl(t->o_seqno++));
988
989 /* TooBig packet may have updated dst->dev's mtu */
William Tuef7baf52017-12-05 15:15:44 -0800990 if (!t->parms.collect_md && dst && dst_mtu(dst) > dst->dev->mtu)
William Tu5a963eb2017-11-30 11:51:29 -0800991 dst->ops->update_pmtu(dst, NULL, skb, dst->dev->mtu);
992
993 err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
994 NEXTHDR_GRE);
995 if (err != 0) {
996 /* XXX: send ICMP error even if DF is not set. */
997 if (err == -EMSGSIZE) {
998 if (skb->protocol == htons(ETH_P_IP))
999 icmp_send(skb, ICMP_DEST_UNREACH,
1000 ICMP_FRAG_NEEDED, htonl(mtu));
1001 else
1002 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1003 }
1004
1005 goto tx_err;
1006 }
1007 return NETDEV_TX_OK;
1008
1009tx_err:
1010 stats = &t->dev->stats;
1011 stats->tx_errors++;
1012 stats->tx_dropped++;
1013 kfree_skb(skb);
1014 return NETDEV_TX_OK;
1015}
1016
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001017static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu)
1018{
1019 struct net_device *dev = t->dev;
1020 struct __ip6_tnl_parm *p = &t->parms;
1021 struct flowi6 *fl6 = &t->fl.u.ip6;
Tom Herbertdb2ec952016-05-09 17:12:08 -07001022 int t_hlen;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001023
1024 if (dev->type != ARPHRD_ETHER) {
1025 memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
1026 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
1027 }
1028
1029 /* Set up flowi template */
1030 fl6->saddr = p->laddr;
1031 fl6->daddr = p->raddr;
1032 fl6->flowi6_oif = p->link;
1033 fl6->flowlabel = 0;
Haishuang Yan252f3f52016-05-21 18:17:35 +08001034 fl6->flowi6_proto = IPPROTO_GRE;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001035
1036 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
1037 fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
1038 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
1039 fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
1040
1041 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
1042 p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
1043
1044 if (p->flags&IP6_TNL_F_CAP_XMIT &&
1045 p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER)
1046 dev->flags |= IFF_POINTOPOINT;
1047 else
1048 dev->flags &= ~IFF_POINTOPOINT;
1049
Tom Herbertdb2ec952016-05-09 17:12:08 -07001050 t->tun_hlen = gre_calc_hlen(t->parms.o_flags);
1051
Tom Herbert1faf3d92016-05-18 09:06:19 -07001052 t->hlen = t->encap_hlen + t->tun_hlen;
Tom Herbertdb2ec952016-05-09 17:12:08 -07001053
1054 t_hlen = t->hlen + sizeof(struct ipv6hdr);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001055
1056 if (p->flags & IP6_TNL_F_CAP_XMIT) {
1057 int strict = (ipv6_addr_type(&p->raddr) &
1058 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
1059
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001060 struct rt6_info *rt = rt6_lookup(t->net,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001061 &p->raddr, &p->laddr,
1062 p->link, strict);
1063
Ian Morris63159f22015-03-29 14:00:04 +01001064 if (!rt)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001065 return;
1066
1067 if (rt->dst.dev) {
Tom Herbertdb2ec952016-05-09 17:12:08 -07001068 dev->hard_header_len = rt->dst.dev->hard_header_len +
1069 t_hlen;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001070
1071 if (set_mtu) {
Tom Herbertdb2ec952016-05-09 17:12:08 -07001072 dev->mtu = rt->dst.dev->mtu - t_hlen;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001073 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1074 dev->mtu -= 8;
Alexander Duycka9e242c2016-04-14 15:33:45 -04001075 if (dev->type == ARPHRD_ETHER)
1076 dev->mtu -= ETH_HLEN;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001077
1078 if (dev->mtu < IPV6_MIN_MTU)
1079 dev->mtu = IPV6_MIN_MTU;
1080 }
1081 }
Amerigo Wang94e187c2012-10-29 00:13:19 +00001082 ip6_rt_put(rt);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001083 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001084}
1085
1086static int ip6gre_tnl_change(struct ip6_tnl *t,
1087 const struct __ip6_tnl_parm *p, int set_mtu)
1088{
1089 t->parms.laddr = p->laddr;
1090 t->parms.raddr = p->raddr;
1091 t->parms.flags = p->flags;
1092 t->parms.hop_limit = p->hop_limit;
1093 t->parms.encap_limit = p->encap_limit;
1094 t->parms.flowinfo = p->flowinfo;
1095 t->parms.link = p->link;
1096 t->parms.proto = p->proto;
1097 t->parms.i_key = p->i_key;
1098 t->parms.o_key = p->o_key;
1099 t->parms.i_flags = p->i_flags;
1100 t->parms.o_flags = p->o_flags;
Craig Gallek0a473b82017-04-19 12:30:53 -04001101 t->parms.fwmark = p->fwmark;
Paolo Abeni607f7252016-02-12 15:43:54 +01001102 dst_cache_reset(&t->dst_cache);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001103 ip6gre_tnl_link_config(t, set_mtu);
1104 return 0;
1105}
1106
1107static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p,
1108 const struct ip6_tnl_parm2 *u)
1109{
1110 p->laddr = u->laddr;
1111 p->raddr = u->raddr;
1112 p->flags = u->flags;
1113 p->hop_limit = u->hop_limit;
1114 p->encap_limit = u->encap_limit;
1115 p->flowinfo = u->flowinfo;
1116 p->link = u->link;
1117 p->i_key = u->i_key;
1118 p->o_key = u->o_key;
Tom Herbertf41fe3c2016-05-09 17:12:09 -07001119 p->i_flags = gre_flags_to_tnl_flags(u->i_flags);
1120 p->o_flags = gre_flags_to_tnl_flags(u->o_flags);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001121 memcpy(p->name, u->name, sizeof(u->name));
1122}
1123
1124static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u,
1125 const struct __ip6_tnl_parm *p)
1126{
1127 u->proto = IPPROTO_GRE;
1128 u->laddr = p->laddr;
1129 u->raddr = p->raddr;
1130 u->flags = p->flags;
1131 u->hop_limit = p->hop_limit;
1132 u->encap_limit = p->encap_limit;
1133 u->flowinfo = p->flowinfo;
1134 u->link = p->link;
1135 u->i_key = p->i_key;
1136 u->o_key = p->o_key;
Tom Herbertf41fe3c2016-05-09 17:12:09 -07001137 u->i_flags = gre_tnl_flags_to_gre_flags(p->i_flags);
1138 u->o_flags = gre_tnl_flags_to_gre_flags(p->o_flags);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001139 memcpy(u->name, p->name, sizeof(u->name));
1140}
1141
1142static int ip6gre_tunnel_ioctl(struct net_device *dev,
1143 struct ifreq *ifr, int cmd)
1144{
1145 int err = 0;
1146 struct ip6_tnl_parm2 p;
1147 struct __ip6_tnl_parm p1;
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001148 struct ip6_tnl *t = netdev_priv(dev);
1149 struct net *net = t->net;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001150 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1151
Tom Herbert308edfdf2016-04-29 17:12:17 -07001152 memset(&p1, 0, sizeof(p1));
1153
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001154 switch (cmd) {
1155 case SIOCGETTUNNEL:
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001156 if (dev == ign->fb_tunnel_dev) {
1157 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
1158 err = -EFAULT;
1159 break;
1160 }
1161 ip6gre_tnl_parm_from_user(&p1, &p);
1162 t = ip6gre_tunnel_locate(net, &p1, 0);
Ian Morris63159f22015-03-29 14:00:04 +01001163 if (!t)
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001164 t = netdev_priv(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001165 }
Amerigo Wang5dbd5062013-05-09 21:56:37 +00001166 memset(&p, 0, sizeof(p));
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001167 ip6gre_tnl_parm_to_user(&p, &t->parms);
1168 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
1169 err = -EFAULT;
1170 break;
1171
1172 case SIOCADDTUNNEL:
1173 case SIOCCHGTUNNEL:
1174 err = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00001175 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001176 goto done;
1177
1178 err = -EFAULT;
1179 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1180 goto done;
1181
1182 err = -EINVAL;
1183 if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))
1184 goto done;
1185
1186 if (!(p.i_flags&GRE_KEY))
1187 p.i_key = 0;
1188 if (!(p.o_flags&GRE_KEY))
1189 p.o_key = 0;
1190
1191 ip6gre_tnl_parm_from_user(&p1, &p);
1192 t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL);
1193
1194 if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
Ian Morris53b24b82015-03-29 14:00:05 +01001195 if (t) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001196 if (t->dev != dev) {
1197 err = -EEXIST;
1198 break;
1199 }
1200 } else {
1201 t = netdev_priv(dev);
1202
1203 ip6gre_tunnel_unlink(ign, t);
1204 synchronize_net();
1205 ip6gre_tnl_change(t, &p1, 1);
1206 ip6gre_tunnel_link(ign, t);
1207 netdev_state_change(dev);
1208 }
1209 }
1210
1211 if (t) {
1212 err = 0;
1213
Amerigo Wang5dbd5062013-05-09 21:56:37 +00001214 memset(&p, 0, sizeof(p));
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001215 ip6gre_tnl_parm_to_user(&p, &t->parms);
1216 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
1217 err = -EFAULT;
1218 } else
1219 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
1220 break;
1221
1222 case SIOCDELTUNNEL:
1223 err = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00001224 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001225 goto done;
1226
1227 if (dev == ign->fb_tunnel_dev) {
1228 err = -EFAULT;
1229 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1230 goto done;
1231 err = -ENOENT;
1232 ip6gre_tnl_parm_from_user(&p1, &p);
1233 t = ip6gre_tunnel_locate(net, &p1, 0);
Ian Morris63159f22015-03-29 14:00:04 +01001234 if (!t)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001235 goto done;
1236 err = -EPERM;
1237 if (t == netdev_priv(ign->fb_tunnel_dev))
1238 goto done;
1239 dev = t->dev;
1240 }
1241 unregister_netdevice(dev);
1242 err = 0;
1243 break;
1244
1245 default:
1246 err = -EINVAL;
1247 }
1248
1249done:
1250 return err;
1251}
1252
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001253static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
Xin Long76cc0d32017-09-15 12:00:07 +08001254 unsigned short type, const void *daddr,
1255 const void *saddr, unsigned int len)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001256{
1257 struct ip6_tnl *t = netdev_priv(dev);
Xin Long76cc0d32017-09-15 12:00:07 +08001258 struct ipv6hdr *ipv6h;
1259 __be16 *p;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001260
Xin Long76cc0d32017-09-15 12:00:07 +08001261 ipv6h = skb_push(skb, t->hlen + sizeof(*ipv6h));
1262 ip6_flow_hdr(ipv6h, 0, ip6_make_flowlabel(dev_net(dev), skb,
1263 t->fl.u.ip6.flowlabel,
1264 true, &t->fl.u.ip6));
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001265 ipv6h->hop_limit = t->parms.hop_limit;
1266 ipv6h->nexthdr = NEXTHDR_GRE;
1267 ipv6h->saddr = t->parms.laddr;
1268 ipv6h->daddr = t->parms.raddr;
1269
Xin Long76cc0d32017-09-15 12:00:07 +08001270 p = (__be16 *)(ipv6h + 1);
1271 p[0] = t->parms.o_flags;
1272 p[1] = htons(type);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001273
1274 /*
1275 * Set the source hardware address.
1276 */
1277
1278 if (saddr)
1279 memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr));
1280 if (daddr)
1281 memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
1282 if (!ipv6_addr_any(&ipv6h->daddr))
1283 return t->hlen;
1284
1285 return -t->hlen;
1286}
1287
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001288static const struct header_ops ip6gre_header_ops = {
1289 .create = ip6gre_header,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001290};
1291
1292static const struct net_device_ops ip6gre_netdev_ops = {
1293 .ndo_init = ip6gre_tunnel_init,
1294 .ndo_uninit = ip6gre_tunnel_uninit,
1295 .ndo_start_xmit = ip6gre_tunnel_xmit,
1296 .ndo_do_ioctl = ip6gre_tunnel_ioctl,
Tom Herbertb05229f2016-04-29 17:12:21 -07001297 .ndo_change_mtu = ip6_tnl_change_mtu,
Pravin B Shelarf61dd382013-03-25 14:50:00 +00001298 .ndo_get_stats64 = ip_tunnel_get_stats64,
Nicolas Dichtelecf2c062015-04-02 17:07:01 +02001299 .ndo_get_iflink = ip6_tnl_get_iflink,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001300};
1301
1302static void ip6gre_dev_free(struct net_device *dev)
1303{
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001304 struct ip6_tnl *t = netdev_priv(dev);
1305
Paolo Abeni607f7252016-02-12 15:43:54 +01001306 dst_cache_destroy(&t->dst_cache);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001307 free_percpu(dev->tstats);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001308}
1309
1310static void ip6gre_tunnel_setup(struct net_device *dev)
1311{
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001312 dev->netdev_ops = &ip6gre_netdev_ops;
David S. Millercf124db2017-05-08 12:52:56 -04001313 dev->needs_free_netdev = true;
1314 dev->priv_destructor = ip6gre_dev_free;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001315
1316 dev->type = ARPHRD_IP6GRE;
Tom Herbertb05229f2016-04-29 17:12:21 -07001317
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001318 dev->flags |= IFF_NOARP;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001319 dev->addr_len = sizeof(struct in6_addr);
Eric Dumazet02875872014-10-05 18:38:35 -07001320 netif_keep_dst(dev);
Felix Jia45ce0fd2017-01-26 16:59:18 +13001321 /* This perm addr will be used as interface identifier by IPv6 */
1322 dev->addr_assign_type = NET_ADDR_RANDOM;
1323 eth_random_addr(dev->perm_addr);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001324}
1325
Alexey Kodaneve5a93362017-12-20 19:36:03 +03001326#define GRE6_FEATURES (NETIF_F_SG | \
1327 NETIF_F_FRAGLIST | \
1328 NETIF_F_HIGHDMA | \
1329 NETIF_F_HW_CSUM)
1330
1331static void ip6gre_tnl_init_features(struct net_device *dev)
1332{
1333 struct ip6_tnl *nt = netdev_priv(dev);
1334
1335 dev->features |= GRE6_FEATURES;
1336 dev->hw_features |= GRE6_FEATURES;
1337
1338 if (!(nt->parms.o_flags & TUNNEL_SEQ)) {
1339 /* TCP offload with GRE SEQ is not supported, nor
1340 * can we support 2 levels of outer headers requiring
1341 * an update.
1342 */
1343 if (!(nt->parms.o_flags & TUNNEL_CSUM) ||
1344 nt->encap.type == TUNNEL_ENCAP_NONE) {
1345 dev->features |= NETIF_F_GSO_SOFTWARE;
1346 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
1347 }
1348
1349 /* Can use a lockless transmit, unless we generate
1350 * output sequences
1351 */
1352 dev->features |= NETIF_F_LLTX;
1353 }
1354}
1355
Martin KaFai Laua3c119d2015-09-15 14:30:05 -07001356static int ip6gre_tunnel_init_common(struct net_device *dev)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001357{
1358 struct ip6_tnl *tunnel;
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001359 int ret;
Tom Herbertb05229f2016-04-29 17:12:21 -07001360 int t_hlen;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001361
1362 tunnel = netdev_priv(dev);
1363
1364 tunnel->dev = dev;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001365 tunnel->net = dev_net(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001366 strcpy(tunnel->parms.name, dev->name);
1367
Martin KaFai Laua3c119d2015-09-15 14:30:05 -07001368 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1369 if (!dev->tstats)
1370 return -ENOMEM;
1371
Paolo Abeni607f7252016-02-12 15:43:54 +01001372 ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001373 if (ret) {
1374 free_percpu(dev->tstats);
1375 dev->tstats = NULL;
1376 return ret;
1377 }
1378
Tom Herbertb05229f2016-04-29 17:12:21 -07001379 tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
Tom Herbert1faf3d92016-05-18 09:06:19 -07001380 tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen;
Tom Herbertb05229f2016-04-29 17:12:21 -07001381 t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
1382
Tom Herbertdb2ec952016-05-09 17:12:08 -07001383 dev->hard_header_len = LL_MAX_HEADER + t_hlen;
1384 dev->mtu = ETH_DATA_LEN - t_hlen;
Haishuang Yan1b227e52016-05-21 18:17:34 +08001385 if (dev->type == ARPHRD_ETHER)
1386 dev->mtu -= ETH_HLEN;
Tom Herbertb05229f2016-04-29 17:12:21 -07001387 if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1388 dev->mtu -= 8;
1389
William Tu6712abc2017-12-01 15:26:08 -08001390 if (tunnel->parms.collect_md) {
1391 dev->features |= NETIF_F_NETNS_LOCAL;
1392 netif_keep_dst(dev);
1393 }
Alexey Kodaneve5a93362017-12-20 19:36:03 +03001394 ip6gre_tnl_init_features(dev);
William Tu6712abc2017-12-01 15:26:08 -08001395
Martin KaFai Laua3c119d2015-09-15 14:30:05 -07001396 return 0;
1397}
1398
1399static int ip6gre_tunnel_init(struct net_device *dev)
1400{
1401 struct ip6_tnl *tunnel;
1402 int ret;
1403
1404 ret = ip6gre_tunnel_init_common(dev);
1405 if (ret)
1406 return ret;
1407
1408 tunnel = netdev_priv(dev);
1409
William Tu6712abc2017-12-01 15:26:08 -08001410 if (tunnel->parms.collect_md)
1411 return 0;
1412
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001413 memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr));
1414 memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr));
1415
1416 if (ipv6_addr_any(&tunnel->parms.raddr))
1417 dev->header_ops = &ip6gre_header_ops;
1418
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001419 return 0;
1420}
1421
1422static void ip6gre_fb_tunnel_init(struct net_device *dev)
1423{
1424 struct ip6_tnl *tunnel = netdev_priv(dev);
1425
1426 tunnel->dev = dev;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001427 tunnel->net = dev_net(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001428 strcpy(tunnel->parms.name, dev->name);
1429
1430 tunnel->hlen = sizeof(struct ipv6hdr) + 4;
1431
1432 dev_hold(dev);
1433}
1434
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001435static struct inet6_protocol ip6gre_protocol __read_mostly = {
Tom Herbert308edfdf2016-04-29 17:12:17 -07001436 .handler = gre_rcv,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001437 .err_handler = ip6gre_err,
1438 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1439};
1440
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001441static void ip6gre_destroy_tunnels(struct net *net, struct list_head *head)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001442{
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001443 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1444 struct net_device *dev, *aux;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001445 int prio;
1446
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001447 for_each_netdev_safe(net, dev, aux)
1448 if (dev->rtnl_link_ops == &ip6gre_link_ops ||
William Tu5a963eb2017-11-30 11:51:29 -08001449 dev->rtnl_link_ops == &ip6gre_tap_ops ||
1450 dev->rtnl_link_ops == &ip6erspan_tap_ops)
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001451 unregister_netdevice_queue(dev, head);
1452
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001453 for (prio = 0; prio < 4; prio++) {
1454 int h;
Jiri Kosinae87a8f22016-08-10 11:03:35 +02001455 for (h = 0; h < IP6_GRE_HASH_SIZE; h++) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001456 struct ip6_tnl *t;
1457
1458 t = rtnl_dereference(ign->tunnels[prio][h]);
1459
Ian Morris53b24b82015-03-29 14:00:05 +01001460 while (t) {
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001461 /* If dev is in the same netns, it has already
1462 * been added to the list by the previous loop.
1463 */
1464 if (!net_eq(dev_net(t->dev), net))
1465 unregister_netdevice_queue(t->dev,
1466 head);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001467 t = rtnl_dereference(t->next);
1468 }
1469 }
1470 }
1471}
1472
1473static int __net_init ip6gre_init_net(struct net *net)
1474{
1475 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1476 int err;
1477
1478 ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0",
Tom Gundersenc835a672014-07-14 16:37:24 +02001479 NET_NAME_UNKNOWN,
1480 ip6gre_tunnel_setup);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001481 if (!ign->fb_tunnel_dev) {
1482 err = -ENOMEM;
1483 goto err_alloc_dev;
1484 }
1485 dev_net_set(ign->fb_tunnel_dev, net);
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001486 /* FB netdevice is special: we have one, and only one per netns.
1487 * Allowing to move it to another netns is clearly unsafe.
1488 */
1489 ign->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL;
1490
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001491
1492 ip6gre_fb_tunnel_init(ign->fb_tunnel_dev);
1493 ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops;
1494
1495 err = register_netdev(ign->fb_tunnel_dev);
1496 if (err)
1497 goto err_reg_dev;
1498
1499 rcu_assign_pointer(ign->tunnels_wc[0],
1500 netdev_priv(ign->fb_tunnel_dev));
1501 return 0;
1502
1503err_reg_dev:
David S. Millercf124db2017-05-08 12:52:56 -04001504 free_netdev(ign->fb_tunnel_dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001505err_alloc_dev:
1506 return err;
1507}
1508
Eric Dumazetbb401ca2017-09-19 16:27:08 -07001509static void __net_exit ip6gre_exit_batch_net(struct list_head *net_list)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001510{
Eric Dumazetbb401ca2017-09-19 16:27:08 -07001511 struct net *net;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001512 LIST_HEAD(list);
1513
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001514 rtnl_lock();
Eric Dumazetbb401ca2017-09-19 16:27:08 -07001515 list_for_each_entry(net, net_list, exit_list)
1516 ip6gre_destroy_tunnels(net, &list);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001517 unregister_netdevice_many(&list);
1518 rtnl_unlock();
1519}
1520
1521static struct pernet_operations ip6gre_net_ops = {
1522 .init = ip6gre_init_net,
Eric Dumazetbb401ca2017-09-19 16:27:08 -07001523 .exit_batch = ip6gre_exit_batch_net,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001524 .id = &ip6gre_net_id,
1525 .size = sizeof(struct ip6gre_net),
1526};
1527
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001528static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[],
1529 struct netlink_ext_ack *extack)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001530{
1531 __be16 flags;
1532
1533 if (!data)
1534 return 0;
1535
1536 flags = 0;
1537 if (data[IFLA_GRE_IFLAGS])
1538 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1539 if (data[IFLA_GRE_OFLAGS])
1540 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1541 if (flags & (GRE_VERSION|GRE_ROUTING))
1542 return -EINVAL;
1543
1544 return 0;
1545}
1546
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001547static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[],
1548 struct netlink_ext_ack *extack)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001549{
1550 struct in6_addr daddr;
1551
1552 if (tb[IFLA_ADDRESS]) {
1553 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1554 return -EINVAL;
1555 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1556 return -EADDRNOTAVAIL;
1557 }
1558
1559 if (!data)
1560 goto out;
1561
1562 if (data[IFLA_GRE_REMOTE]) {
Jiri Benc67b61f62015-03-29 16:59:26 +02001563 daddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001564 if (ipv6_addr_any(&daddr))
1565 return -EINVAL;
1566 }
1567
1568out:
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001569 return ip6gre_tunnel_validate(tb, data, extack);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001570}
1571
William Tu5a963eb2017-11-30 11:51:29 -08001572static int ip6erspan_tap_validate(struct nlattr *tb[], struct nlattr *data[],
1573 struct netlink_ext_ack *extack)
1574{
1575 __be16 flags = 0;
William Tu94d7d8f2017-12-13 16:38:57 -08001576 int ret, ver = 0;
William Tu5a963eb2017-11-30 11:51:29 -08001577
1578 if (!data)
1579 return 0;
1580
1581 ret = ip6gre_tap_validate(tb, data, extack);
1582 if (ret)
1583 return ret;
1584
1585 /* ERSPAN should only have GRE sequence and key flag */
1586 if (data[IFLA_GRE_OFLAGS])
1587 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1588 if (data[IFLA_GRE_IFLAGS])
1589 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1590 if (!data[IFLA_GRE_COLLECT_METADATA] &&
1591 flags != (GRE_SEQ | GRE_KEY))
1592 return -EINVAL;
1593
1594 /* ERSPAN Session ID only has 10-bit. Since we reuse
1595 * 32-bit key field as ID, check it's range.
1596 */
1597 if (data[IFLA_GRE_IKEY] &&
1598 (ntohl(nla_get_be32(data[IFLA_GRE_IKEY])) & ~ID_MASK))
1599 return -EINVAL;
1600
1601 if (data[IFLA_GRE_OKEY] &&
1602 (ntohl(nla_get_be32(data[IFLA_GRE_OKEY])) & ~ID_MASK))
1603 return -EINVAL;
1604
William Tu94d7d8f2017-12-13 16:38:57 -08001605 if (data[IFLA_GRE_ERSPAN_VER]) {
1606 ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]);
1607 if (ver != 1 && ver != 2)
William Tu5a963eb2017-11-30 11:51:29 -08001608 return -EINVAL;
1609 }
William Tu94d7d8f2017-12-13 16:38:57 -08001610
1611 if (ver == 1) {
1612 if (data[IFLA_GRE_ERSPAN_INDEX]) {
1613 u32 index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
1614
1615 if (index & ~INDEX_MASK)
1616 return -EINVAL;
1617 }
1618 } else if (ver == 2) {
1619 if (data[IFLA_GRE_ERSPAN_DIR]) {
1620 u16 dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]);
1621
1622 if (dir & ~(DIR_MASK >> DIR_OFFSET))
1623 return -EINVAL;
1624 }
1625
1626 if (data[IFLA_GRE_ERSPAN_HWID]) {
1627 u16 hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]);
1628
1629 if (hwid & ~(HWID_MASK >> HWID_OFFSET))
1630 return -EINVAL;
1631 }
1632 }
1633
William Tu5a963eb2017-11-30 11:51:29 -08001634 return 0;
1635}
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001636
1637static void ip6gre_netlink_parms(struct nlattr *data[],
1638 struct __ip6_tnl_parm *parms)
1639{
1640 memset(parms, 0, sizeof(*parms));
1641
1642 if (!data)
1643 return;
1644
1645 if (data[IFLA_GRE_LINK])
1646 parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
1647
1648 if (data[IFLA_GRE_IFLAGS])
Tom Herbertf41fe3c2016-05-09 17:12:09 -07001649 parms->i_flags = gre_flags_to_tnl_flags(
1650 nla_get_be16(data[IFLA_GRE_IFLAGS]));
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001651
1652 if (data[IFLA_GRE_OFLAGS])
Tom Herbertf41fe3c2016-05-09 17:12:09 -07001653 parms->o_flags = gre_flags_to_tnl_flags(
1654 nla_get_be16(data[IFLA_GRE_OFLAGS]));
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001655
1656 if (data[IFLA_GRE_IKEY])
1657 parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
1658
1659 if (data[IFLA_GRE_OKEY])
1660 parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
1661
1662 if (data[IFLA_GRE_LOCAL])
Jiri Benc67b61f62015-03-29 16:59:26 +02001663 parms->laddr = nla_get_in6_addr(data[IFLA_GRE_LOCAL]);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001664
1665 if (data[IFLA_GRE_REMOTE])
Jiri Benc67b61f62015-03-29 16:59:26 +02001666 parms->raddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001667
1668 if (data[IFLA_GRE_TTL])
1669 parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]);
1670
1671 if (data[IFLA_GRE_ENCAP_LIMIT])
1672 parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]);
1673
1674 if (data[IFLA_GRE_FLOWINFO])
Lance Richardsonc2675de2016-09-24 14:01:04 -04001675 parms->flowinfo = nla_get_be32(data[IFLA_GRE_FLOWINFO]);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001676
1677 if (data[IFLA_GRE_FLAGS])
1678 parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]);
Craig Gallek0a473b82017-04-19 12:30:53 -04001679
1680 if (data[IFLA_GRE_FWMARK])
1681 parms->fwmark = nla_get_u32(data[IFLA_GRE_FWMARK]);
William Tu5a963eb2017-11-30 11:51:29 -08001682
William Tu6712abc2017-12-01 15:26:08 -08001683 if (data[IFLA_GRE_COLLECT_METADATA])
1684 parms->collect_md = true;
William Tu94d7d8f2017-12-13 16:38:57 -08001685
1686 if (data[IFLA_GRE_ERSPAN_VER])
1687 parms->erspan_ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]);
1688
1689 if (parms->erspan_ver == 1) {
1690 if (data[IFLA_GRE_ERSPAN_INDEX])
1691 parms->index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
1692 } else if (parms->erspan_ver == 2) {
1693 if (data[IFLA_GRE_ERSPAN_DIR])
1694 parms->dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]);
1695 if (data[IFLA_GRE_ERSPAN_HWID])
1696 parms->hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]);
1697 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001698}
1699
1700static int ip6gre_tap_init(struct net_device *dev)
1701{
Martin KaFai Laua3c119d2015-09-15 14:30:05 -07001702 int ret;
1703
1704 ret = ip6gre_tunnel_init_common(dev);
1705 if (ret)
1706 return ret;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001707
Shweta Choudaha0a46baa2016-06-08 20:15:43 +01001708 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1709
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001710 return 0;
1711}
1712
1713static const struct net_device_ops ip6gre_tap_netdev_ops = {
1714 .ndo_init = ip6gre_tap_init,
1715 .ndo_uninit = ip6gre_tunnel_uninit,
1716 .ndo_start_xmit = ip6gre_tunnel_xmit,
1717 .ndo_set_mac_address = eth_mac_addr,
1718 .ndo_validate_addr = eth_validate_addr,
Tom Herbertb05229f2016-04-29 17:12:21 -07001719 .ndo_change_mtu = ip6_tnl_change_mtu,
Pravin B Shelarf61dd382013-03-25 14:50:00 +00001720 .ndo_get_stats64 = ip_tunnel_get_stats64,
Nicolas Dichtelecf2c062015-04-02 17:07:01 +02001721 .ndo_get_iflink = ip6_tnl_get_iflink,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001722};
1723
William Tu5a963eb2017-11-30 11:51:29 -08001724static int ip6erspan_tap_init(struct net_device *dev)
1725{
1726 struct ip6_tnl *tunnel;
1727 int t_hlen;
1728 int ret;
1729
1730 tunnel = netdev_priv(dev);
1731
1732 tunnel->dev = dev;
1733 tunnel->net = dev_net(dev);
1734 strcpy(tunnel->parms.name, dev->name);
1735
1736 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1737 if (!dev->tstats)
1738 return -ENOMEM;
1739
1740 ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
1741 if (ret) {
1742 free_percpu(dev->tstats);
1743 dev->tstats = NULL;
1744 return ret;
1745 }
1746
1747 tunnel->tun_hlen = 8;
1748 tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen +
William Tu94d7d8f2017-12-13 16:38:57 -08001749 erspan_hdr_len(tunnel->parms.erspan_ver);
William Tu5a963eb2017-11-30 11:51:29 -08001750 t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
1751
1752 dev->hard_header_len = LL_MAX_HEADER + t_hlen;
1753 dev->mtu = ETH_DATA_LEN - t_hlen;
1754 if (dev->type == ARPHRD_ETHER)
1755 dev->mtu -= ETH_HLEN;
1756 if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1757 dev->mtu -= 8;
1758
1759 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1760 tunnel = netdev_priv(dev);
1761 ip6gre_tnl_link_config(tunnel, 1);
1762
1763 return 0;
1764}
1765
1766static const struct net_device_ops ip6erspan_netdev_ops = {
1767 .ndo_init = ip6erspan_tap_init,
1768 .ndo_uninit = ip6gre_tunnel_uninit,
1769 .ndo_start_xmit = ip6erspan_tunnel_xmit,
1770 .ndo_set_mac_address = eth_mac_addr,
1771 .ndo_validate_addr = eth_validate_addr,
1772 .ndo_change_mtu = ip6_tnl_change_mtu,
1773 .ndo_get_stats64 = ip_tunnel_get_stats64,
1774 .ndo_get_iflink = ip6_tnl_get_iflink,
1775};
1776
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001777static void ip6gre_tap_setup(struct net_device *dev)
1778{
1779
1780 ether_setup(dev);
1781
Xin Long2c521292017-12-18 14:25:09 +08001782 dev->max_mtu = 0;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001783 dev->netdev_ops = &ip6gre_tap_netdev_ops;
David S. Millercf124db2017-05-08 12:52:56 -04001784 dev->needs_free_netdev = true;
1785 dev->priv_destructor = ip6gre_dev_free;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001786
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001787 dev->features |= NETIF_F_NETNS_LOCAL;
Jiri Bencd13b1612016-02-17 15:32:53 +01001788 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
Shweta Choudaha0a46baa2016-06-08 20:15:43 +01001789 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Xin Long2d405572017-09-28 13:23:50 +08001790 netif_keep_dst(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001791}
1792
Tom Herbert1faf3d92016-05-18 09:06:19 -07001793static bool ip6gre_netlink_encap_parms(struct nlattr *data[],
1794 struct ip_tunnel_encap *ipencap)
1795{
1796 bool ret = false;
1797
1798 memset(ipencap, 0, sizeof(*ipencap));
1799
1800 if (!data)
1801 return ret;
1802
1803 if (data[IFLA_GRE_ENCAP_TYPE]) {
1804 ret = true;
1805 ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]);
1806 }
1807
1808 if (data[IFLA_GRE_ENCAP_FLAGS]) {
1809 ret = true;
1810 ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]);
1811 }
1812
1813 if (data[IFLA_GRE_ENCAP_SPORT]) {
1814 ret = true;
1815 ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]);
1816 }
1817
1818 if (data[IFLA_GRE_ENCAP_DPORT]) {
1819 ret = true;
1820 ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]);
1821 }
1822
1823 return ret;
1824}
1825
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001826static int ip6gre_newlink(struct net *src_net, struct net_device *dev,
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02001827 struct nlattr *tb[], struct nlattr *data[],
1828 struct netlink_ext_ack *extack)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001829{
1830 struct ip6_tnl *nt;
1831 struct net *net = dev_net(dev);
1832 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
Tom Herbert1faf3d92016-05-18 09:06:19 -07001833 struct ip_tunnel_encap ipencap;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001834 int err;
1835
1836 nt = netdev_priv(dev);
Tom Herbert1faf3d92016-05-18 09:06:19 -07001837
1838 if (ip6gre_netlink_encap_parms(data, &ipencap)) {
1839 int err = ip6_tnl_encap_setup(nt, &ipencap);
1840
1841 if (err < 0)
1842 return err;
1843 }
1844
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001845 ip6gre_netlink_parms(data, &nt->parms);
1846
William Tu6712abc2017-12-01 15:26:08 -08001847 if (nt->parms.collect_md) {
1848 if (rtnl_dereference(ign->collect_md_tun))
1849 return -EEXIST;
1850 } else {
1851 if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
1852 return -EEXIST;
1853 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001854
1855 if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
1856 eth_hw_addr_random(dev);
1857
1858 nt->dev = dev;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001859 nt->net = dev_net(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001860
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001861 err = register_netdevice(dev);
1862 if (err)
1863 goto out;
1864
Alexey Kodanev128bb972018-01-18 20:51:12 +03001865 ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]);
1866
1867 if (tb[IFLA_MTU])
1868 ip6_tnl_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1869
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001870 dev_hold(dev);
1871 ip6gre_tunnel_link(ign, nt);
1872
1873out:
1874 return err;
1875}
1876
1877static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],
Matthias Schifferad744b22017-06-25 23:56:00 +02001878 struct nlattr *data[],
1879 struct netlink_ext_ack *extack)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001880{
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001881 struct ip6_tnl *t, *nt = netdev_priv(dev);
1882 struct net *net = nt->net;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001883 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1884 struct __ip6_tnl_parm p;
Tom Herbert1faf3d92016-05-18 09:06:19 -07001885 struct ip_tunnel_encap ipencap;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001886
1887 if (dev == ign->fb_tunnel_dev)
1888 return -EINVAL;
1889
Tom Herbert1faf3d92016-05-18 09:06:19 -07001890 if (ip6gre_netlink_encap_parms(data, &ipencap)) {
1891 int err = ip6_tnl_encap_setup(nt, &ipencap);
1892
1893 if (err < 0)
1894 return err;
1895 }
1896
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001897 ip6gre_netlink_parms(data, &p);
1898
1899 t = ip6gre_tunnel_locate(net, &p, 0);
1900
1901 if (t) {
1902 if (t->dev != dev)
1903 return -EEXIST;
1904 } else {
1905 t = nt;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001906 }
1907
Nicolas Dichtel6a61d4d2015-12-03 17:21:50 +01001908 ip6gre_tunnel_unlink(ign, t);
1909 ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]);
1910 ip6gre_tunnel_link(ign, t);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001911 return 0;
1912}
1913
Nicolas Dichtel54d63f782014-04-14 17:11:38 +02001914static void ip6gre_dellink(struct net_device *dev, struct list_head *head)
1915{
1916 struct net *net = dev_net(dev);
1917 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1918
1919 if (dev != ign->fb_tunnel_dev)
1920 unregister_netdevice_queue(dev, head);
1921}
1922
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001923static size_t ip6gre_get_size(const struct net_device *dev)
1924{
1925 return
1926 /* IFLA_GRE_LINK */
1927 nla_total_size(4) +
1928 /* IFLA_GRE_IFLAGS */
1929 nla_total_size(2) +
1930 /* IFLA_GRE_OFLAGS */
1931 nla_total_size(2) +
1932 /* IFLA_GRE_IKEY */
1933 nla_total_size(4) +
1934 /* IFLA_GRE_OKEY */
1935 nla_total_size(4) +
1936 /* IFLA_GRE_LOCAL */
Nicolas Dichtela3754132012-11-09 05:34:56 +00001937 nla_total_size(sizeof(struct in6_addr)) +
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001938 /* IFLA_GRE_REMOTE */
Nicolas Dichtela3754132012-11-09 05:34:56 +00001939 nla_total_size(sizeof(struct in6_addr)) +
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001940 /* IFLA_GRE_TTL */
1941 nla_total_size(1) +
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001942 /* IFLA_GRE_ENCAP_LIMIT */
1943 nla_total_size(1) +
1944 /* IFLA_GRE_FLOWINFO */
1945 nla_total_size(4) +
1946 /* IFLA_GRE_FLAGS */
1947 nla_total_size(4) +
Tom Herbert1faf3d92016-05-18 09:06:19 -07001948 /* IFLA_GRE_ENCAP_TYPE */
1949 nla_total_size(2) +
1950 /* IFLA_GRE_ENCAP_FLAGS */
1951 nla_total_size(2) +
1952 /* IFLA_GRE_ENCAP_SPORT */
1953 nla_total_size(2) +
1954 /* IFLA_GRE_ENCAP_DPORT */
1955 nla_total_size(2) +
William Tu6712abc2017-12-01 15:26:08 -08001956 /* IFLA_GRE_COLLECT_METADATA */
1957 nla_total_size(0) +
Craig Gallek0a473b82017-04-19 12:30:53 -04001958 /* IFLA_GRE_FWMARK */
1959 nla_total_size(4) +
William Tu5a963eb2017-11-30 11:51:29 -08001960 /* IFLA_GRE_ERSPAN_INDEX */
1961 nla_total_size(4) +
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001962 0;
1963}
1964
1965static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev)
1966{
1967 struct ip6_tnl *t = netdev_priv(dev);
1968 struct __ip6_tnl_parm *p = &t->parms;
1969
1970 if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
Tom Herbertf41fe3c2016-05-09 17:12:09 -07001971 nla_put_be16(skb, IFLA_GRE_IFLAGS,
1972 gre_tnl_flags_to_gre_flags(p->i_flags)) ||
1973 nla_put_be16(skb, IFLA_GRE_OFLAGS,
1974 gre_tnl_flags_to_gre_flags(p->o_flags)) ||
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001975 nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
1976 nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
Jiri Benc930345e2015-03-29 16:59:25 +02001977 nla_put_in6_addr(skb, IFLA_GRE_LOCAL, &p->laddr) ||
1978 nla_put_in6_addr(skb, IFLA_GRE_REMOTE, &p->raddr) ||
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001979 nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) ||
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001980 nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) ||
1981 nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) ||
Craig Gallek0a473b82017-04-19 12:30:53 -04001982 nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags) ||
William Tu5a963eb2017-11-30 11:51:29 -08001983 nla_put_u32(skb, IFLA_GRE_FWMARK, p->fwmark) ||
1984 nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, p->index))
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001985 goto nla_put_failure;
Tom Herbert1faf3d92016-05-18 09:06:19 -07001986
1987 if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE,
1988 t->encap.type) ||
1989 nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT,
1990 t->encap.sport) ||
1991 nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT,
1992 t->encap.dport) ||
1993 nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS,
1994 t->encap.flags))
1995 goto nla_put_failure;
1996
William Tu6712abc2017-12-01 15:26:08 -08001997 if (p->collect_md) {
1998 if (nla_put_flag(skb, IFLA_GRE_COLLECT_METADATA))
1999 goto nla_put_failure;
2000 }
2001
William Tu94d7d8f2017-12-13 16:38:57 -08002002 if (nla_put_u8(skb, IFLA_GRE_ERSPAN_VER, p->erspan_ver))
2003 goto nla_put_failure;
2004
2005 if (p->erspan_ver == 1) {
2006 if (nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, p->index))
2007 goto nla_put_failure;
2008 } else if (p->erspan_ver == 2) {
2009 if (nla_put_u8(skb, IFLA_GRE_ERSPAN_DIR, p->dir))
2010 goto nla_put_failure;
2011 if (nla_put_u16(skb, IFLA_GRE_ERSPAN_HWID, p->hwid))
2012 goto nla_put_failure;
2013 }
2014
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002015 return 0;
2016
2017nla_put_failure:
2018 return -EMSGSIZE;
2019}
2020
2021static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = {
2022 [IFLA_GRE_LINK] = { .type = NLA_U32 },
2023 [IFLA_GRE_IFLAGS] = { .type = NLA_U16 },
2024 [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
2025 [IFLA_GRE_IKEY] = { .type = NLA_U32 },
2026 [IFLA_GRE_OKEY] = { .type = NLA_U32 },
2027 [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct ipv6hdr, saddr) },
2028 [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct ipv6hdr, daddr) },
2029 [IFLA_GRE_TTL] = { .type = NLA_U8 },
2030 [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 },
2031 [IFLA_GRE_FLOWINFO] = { .type = NLA_U32 },
2032 [IFLA_GRE_FLAGS] = { .type = NLA_U32 },
Tom Herbert1faf3d92016-05-18 09:06:19 -07002033 [IFLA_GRE_ENCAP_TYPE] = { .type = NLA_U16 },
2034 [IFLA_GRE_ENCAP_FLAGS] = { .type = NLA_U16 },
2035 [IFLA_GRE_ENCAP_SPORT] = { .type = NLA_U16 },
2036 [IFLA_GRE_ENCAP_DPORT] = { .type = NLA_U16 },
William Tu6712abc2017-12-01 15:26:08 -08002037 [IFLA_GRE_COLLECT_METADATA] = { .type = NLA_FLAG },
Craig Gallek0a473b82017-04-19 12:30:53 -04002038 [IFLA_GRE_FWMARK] = { .type = NLA_U32 },
William Tu5a963eb2017-11-30 11:51:29 -08002039 [IFLA_GRE_ERSPAN_INDEX] = { .type = NLA_U32 },
William Tu94d7d8f2017-12-13 16:38:57 -08002040 [IFLA_GRE_ERSPAN_VER] = { .type = NLA_U8 },
2041 [IFLA_GRE_ERSPAN_DIR] = { .type = NLA_U8 },
2042 [IFLA_GRE_ERSPAN_HWID] = { .type = NLA_U16 },
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002043};
2044
William Tu5a963eb2017-11-30 11:51:29 -08002045static void ip6erspan_tap_setup(struct net_device *dev)
2046{
2047 ether_setup(dev);
2048
2049 dev->netdev_ops = &ip6erspan_netdev_ops;
2050 dev->needs_free_netdev = true;
2051 dev->priv_destructor = ip6gre_dev_free;
2052
2053 dev->features |= NETIF_F_NETNS_LOCAL;
2054 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
2055 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2056 netif_keep_dst(dev);
2057}
2058
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002059static struct rtnl_link_ops ip6gre_link_ops __read_mostly = {
2060 .kind = "ip6gre",
2061 .maxtype = IFLA_GRE_MAX,
2062 .policy = ip6gre_policy,
2063 .priv_size = sizeof(struct ip6_tnl),
2064 .setup = ip6gre_tunnel_setup,
2065 .validate = ip6gre_tunnel_validate,
2066 .newlink = ip6gre_newlink,
2067 .changelink = ip6gre_changelink,
Nicolas Dichtel54d63f782014-04-14 17:11:38 +02002068 .dellink = ip6gre_dellink,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002069 .get_size = ip6gre_get_size,
2070 .fill_info = ip6gre_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01002071 .get_link_net = ip6_tnl_get_link_net,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002072};
2073
2074static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = {
2075 .kind = "ip6gretap",
2076 .maxtype = IFLA_GRE_MAX,
2077 .policy = ip6gre_policy,
2078 .priv_size = sizeof(struct ip6_tnl),
2079 .setup = ip6gre_tap_setup,
2080 .validate = ip6gre_tap_validate,
2081 .newlink = ip6gre_newlink,
2082 .changelink = ip6gre_changelink,
2083 .get_size = ip6gre_get_size,
2084 .fill_info = ip6gre_fill_info,
Nicolas Dichtel3390e392015-01-20 15:15:43 +01002085 .get_link_net = ip6_tnl_get_link_net,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002086};
2087
William Tu5a963eb2017-11-30 11:51:29 -08002088static struct rtnl_link_ops ip6erspan_tap_ops __read_mostly = {
2089 .kind = "ip6erspan",
2090 .maxtype = IFLA_GRE_MAX,
2091 .policy = ip6gre_policy,
2092 .priv_size = sizeof(struct ip6_tnl),
2093 .setup = ip6erspan_tap_setup,
2094 .validate = ip6erspan_tap_validate,
2095 .newlink = ip6gre_newlink,
2096 .changelink = ip6gre_changelink,
2097 .get_size = ip6gre_get_size,
2098 .fill_info = ip6gre_fill_info,
2099 .get_link_net = ip6_tnl_get_link_net,
2100};
2101
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002102/*
2103 * And now the modules code and kernel interface.
2104 */
2105
2106static int __init ip6gre_init(void)
2107{
2108 int err;
2109
2110 pr_info("GRE over IPv6 tunneling driver\n");
2111
2112 err = register_pernet_device(&ip6gre_net_ops);
2113 if (err < 0)
2114 return err;
2115
2116 err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE);
2117 if (err < 0) {
2118 pr_info("%s: can't add protocol\n", __func__);
2119 goto add_proto_failed;
2120 }
2121
2122 err = rtnl_link_register(&ip6gre_link_ops);
2123 if (err < 0)
2124 goto rtnl_link_failed;
2125
2126 err = rtnl_link_register(&ip6gre_tap_ops);
2127 if (err < 0)
2128 goto tap_ops_failed;
2129
William Tu5a963eb2017-11-30 11:51:29 -08002130 err = rtnl_link_register(&ip6erspan_tap_ops);
2131 if (err < 0)
2132 goto erspan_link_failed;
2133
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002134out:
2135 return err;
2136
William Tu5a963eb2017-11-30 11:51:29 -08002137erspan_link_failed:
2138 rtnl_link_unregister(&ip6gre_tap_ops);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002139tap_ops_failed:
2140 rtnl_link_unregister(&ip6gre_link_ops);
2141rtnl_link_failed:
2142 inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
2143add_proto_failed:
2144 unregister_pernet_device(&ip6gre_net_ops);
2145 goto out;
2146}
2147
2148static void __exit ip6gre_fini(void)
2149{
2150 rtnl_link_unregister(&ip6gre_tap_ops);
2151 rtnl_link_unregister(&ip6gre_link_ops);
William Tu5a963eb2017-11-30 11:51:29 -08002152 rtnl_link_unregister(&ip6erspan_tap_ops);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002153 inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
2154 unregister_pernet_device(&ip6gre_net_ops);
2155}
2156
2157module_init(ip6gre_init);
2158module_exit(ip6gre_fini);
2159MODULE_LICENSE("GPL");
2160MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
2161MODULE_DESCRIPTION("GRE over IPv6 tunneling device");
2162MODULE_ALIAS_RTNL_LINK("ip6gre");
Nicolas Dichtel5a4ee9a2014-09-24 11:03:00 +02002163MODULE_ALIAS_RTNL_LINK("ip6gretap");
William Tu94d7d8f2017-12-13 16:38:57 -08002164MODULE_ALIAS_RTNL_LINK("ip6erspan");
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002165MODULE_ALIAS_NETDEV("ip6gre0");