blob: 1510ce9a4e4eb3730ba323d00296aa0cc904e864 [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{
504 const struct ipv6hdr *ipv6h;
505 struct erspanhdr *ershdr;
506 struct ip6_tnl *tunnel;
507 __be32 index;
508
509 ipv6h = ipv6_hdr(skb);
510 ershdr = (struct erspanhdr *)skb->data;
511
512 if (unlikely(!pskb_may_pull(skb, sizeof(*ershdr))))
513 return PACKET_REJECT;
514
515 tpi->key = cpu_to_be32(ntohs(ershdr->session_id) & ID_MASK);
516 index = ershdr->md.index;
517
518 tunnel = ip6gre_tunnel_lookup(skb->dev,
519 &ipv6h->saddr, &ipv6h->daddr, tpi->key,
520 tpi->proto);
521 if (tunnel) {
522 if (__iptunnel_pull_header(skb, sizeof(*ershdr),
523 htons(ETH_P_TEB),
524 false, false) < 0)
525 return PACKET_REJECT;
526
527 tunnel->parms.index = ntohl(index);
528 ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error);
529
530 return PACKET_RCVD;
531 }
532
533 return PACKET_REJECT;
534}
535
Tom Herbert308edfdf2016-04-29 17:12:17 -0700536static int gre_rcv(struct sk_buff *skb)
537{
538 struct tnl_ptk_info tpi;
539 bool csum_err = false;
540 int hdr_len;
541
Eric Dumazete582615ad2016-06-15 06:24:00 -0700542 hdr_len = gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IPV6), 0);
Jiri Bencf132ae72016-05-03 15:00:21 +0200543 if (hdr_len < 0)
Tom Herbert308edfdf2016-04-29 17:12:17 -0700544 goto drop;
545
546 if (iptunnel_pull_header(skb, hdr_len, tpi.proto, false))
547 goto drop;
548
William Tu5a963eb2017-11-30 11:51:29 -0800549 if (unlikely(tpi.proto == htons(ETH_P_ERSPAN))) {
550 if (ip6erspan_rcv(skb, hdr_len, &tpi) == PACKET_RCVD)
551 return 0;
552 goto drop;
553 }
554
Tom Herbert308edfdf2016-04-29 17:12:17 -0700555 if (ip6gre_rcv(skb, &tpi) == PACKET_RCVD)
556 return 0;
557
558 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000559drop:
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000560 kfree_skb(skb);
561 return 0;
562}
563
Tom Herbertb05229f2016-04-29 17:12:21 -0700564static int gre_handle_offloads(struct sk_buff *skb, bool csum)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000565{
Tom Herbertb05229f2016-04-29 17:12:21 -0700566 return iptunnel_handle_offloads(skb,
567 csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000568}
569
William Tu898b29792017-11-30 11:51:28 -0800570static void prepare_ip6gre_xmit_ipv4(struct sk_buff *skb,
571 struct net_device *dev,
572 struct flowi6 *fl6, __u8 *dsfield,
573 int *encap_limit)
574{
575 const struct iphdr *iph = ip_hdr(skb);
576 struct ip6_tnl *t = netdev_priv(dev);
577
578 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
579 *encap_limit = t->parms.encap_limit;
580
581 memcpy(fl6, &t->fl.u.ip6, sizeof(*fl6));
582
583 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
584 *dsfield = ipv4_get_dsfield(iph);
585 else
586 *dsfield = ip6_tclass(t->parms.flowinfo);
587
588 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
589 fl6->flowi6_mark = skb->mark;
590 else
591 fl6->flowi6_mark = t->parms.fwmark;
592
593 fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
594}
595
596static int prepare_ip6gre_xmit_ipv6(struct sk_buff *skb,
597 struct net_device *dev,
598 struct flowi6 *fl6, __u8 *dsfield,
599 int *encap_limit)
600{
601 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
602 struct ip6_tnl *t = netdev_priv(dev);
603 __u16 offset;
604
605 offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
606 /* ip6_tnl_parse_tlv_enc_lim() might have reallocated skb->head */
607
608 if (offset > 0) {
609 struct ipv6_tlv_tnl_enc_lim *tel;
610
611 tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
612 if (tel->encap_limit == 0) {
613 icmpv6_send(skb, ICMPV6_PARAMPROB,
614 ICMPV6_HDR_FIELD, offset + 2);
615 return -1;
616 }
617 *encap_limit = tel->encap_limit - 1;
618 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) {
619 *encap_limit = t->parms.encap_limit;
620 }
621
622 memcpy(fl6, &t->fl.u.ip6, sizeof(*fl6));
623
624 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
625 *dsfield = ipv6_get_dsfield(ipv6h);
626 else
627 *dsfield = ip6_tclass(t->parms.flowinfo);
628
629 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
630 fl6->flowlabel |= ip6_flowlabel(ipv6h);
631
632 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
633 fl6->flowi6_mark = skb->mark;
634 else
635 fl6->flowi6_mark = t->parms.fwmark;
636
637 fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
638
639 return 0;
640}
641
Tom Herbertb05229f2016-04-29 17:12:21 -0700642static netdev_tx_t __gre6_xmit(struct sk_buff *skb,
643 struct net_device *dev, __u8 dsfield,
644 struct flowi6 *fl6, int encap_limit,
645 __u32 *pmtu, __be16 proto)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000646{
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000647 struct ip6_tnl *tunnel = netdev_priv(dev);
Xin Long8aec4952017-10-26 19:27:17 +0800648 __be16 protocol;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000649
650 if (dev->type == ARPHRD_ETHER)
651 IPCB(skb)->flags = 0;
652
Tom Herbertb05229f2016-04-29 17:12:21 -0700653 if (dev->header_ops && dev->type == ARPHRD_IP6GRE)
654 fl6->daddr = ((struct ipv6hdr *)skb->data)->daddr;
655 else
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000656 fl6->daddr = tunnel->parms.raddr;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000657
Tom Herbertb05229f2016-04-29 17:12:21 -0700658 if (tunnel->parms.o_flags & TUNNEL_SEQ)
659 tunnel->o_seqno++;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000660
Tom Herbertb05229f2016-04-29 17:12:21 -0700661 /* Push GRE header. */
Xin Long8aec4952017-10-26 19:27:17 +0800662 protocol = (dev->type == ARPHRD_ETHER) ? htons(ETH_P_TEB) : proto;
William Tu6712abc2017-12-01 15:26:08 -0800663
664 if (tunnel->parms.collect_md) {
665 struct ip_tunnel_info *tun_info;
666 const struct ip_tunnel_key *key;
667 __be16 flags;
668
669 tun_info = skb_tunnel_info(skb);
670 if (unlikely(!tun_info ||
671 !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
672 ip_tunnel_info_af(tun_info) != AF_INET6))
673 return -EINVAL;
674
675 key = &tun_info->key;
676 memset(fl6, 0, sizeof(*fl6));
677 fl6->flowi6_proto = IPPROTO_GRE;
678 fl6->daddr = key->u.ipv6.dst;
679 fl6->flowlabel = key->label;
680 fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
681
682 dsfield = key->tos;
683 flags = key->tun_flags & (TUNNEL_CSUM | TUNNEL_KEY);
684 tunnel->tun_hlen = gre_calc_hlen(flags);
685
686 gre_build_header(skb, tunnel->tun_hlen,
687 flags, protocol,
688 tunnel_id_to_key32(tun_info->key.tun_id), 0);
689
690 } else {
691 gre_build_header(skb, tunnel->tun_hlen, tunnel->parms.o_flags,
692 protocol, tunnel->parms.o_key,
693 htonl(tunnel->o_seqno));
694 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000695
Tom Herbertb05229f2016-04-29 17:12:21 -0700696 return ip6_tnl_xmit(skb, dev, dsfield, fl6, encap_limit, pmtu,
697 NEXTHDR_GRE);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000698}
699
700static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev)
701{
702 struct ip6_tnl *t = netdev_priv(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000703 int encap_limit = -1;
704 struct flowi6 fl6;
William Tu6712abc2017-12-01 15:26:08 -0800705 __u8 dsfield = 0;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000706 __u32 mtu;
707 int err;
708
Bernie Harris5146d1f2016-02-22 12:58:05 +1300709 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
710
William Tu6712abc2017-12-01 15:26:08 -0800711 if (!t->parms.collect_md)
712 prepare_ip6gre_xmit_ipv4(skb, dev, &fl6,
713 &dsfield, &encap_limit);
Lorenzo Colittie2d118a2016-11-04 02:23:43 +0900714
Tom Herbertb05229f2016-04-29 17:12:21 -0700715 err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
716 if (err)
717 return -1;
718
719 err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
720 skb->protocol);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000721 if (err != 0) {
722 /* XXX: send ICMP error even if DF is not set. */
723 if (err == -EMSGSIZE)
724 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
725 htonl(mtu));
726 return -1;
727 }
728
729 return 0;
730}
731
732static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev)
733{
734 struct ip6_tnl *t = netdev_priv(dev);
735 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
736 int encap_limit = -1;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000737 struct flowi6 fl6;
William Tu6712abc2017-12-01 15:26:08 -0800738 __u8 dsfield = 0;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000739 __u32 mtu;
740 int err;
741
742 if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
743 return -1;
744
William Tu6712abc2017-12-01 15:26:08 -0800745 if (!t->parms.collect_md &&
746 prepare_ip6gre_xmit_ipv6(skb, dev, &fl6, &dsfield, &encap_limit))
William Tu898b29792017-11-30 11:51:28 -0800747 return -1;
Lorenzo Colittie2d118a2016-11-04 02:23:43 +0900748
Tom Herbertb05229f2016-04-29 17:12:21 -0700749 if (gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM)))
750 return -1;
751
752 err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit,
753 &mtu, skb->protocol);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000754 if (err != 0) {
755 if (err == -EMSGSIZE)
756 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
757 return -1;
758 }
759
760 return 0;
761}
762
763/**
764 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
765 * @t: the outgoing tunnel device
766 * @hdr: IPv6 header from the incoming packet
767 *
768 * Description:
769 * Avoid trivial tunneling loop by checking that tunnel exit-point
770 * doesn't match source of incoming packet.
771 *
772 * Return:
773 * 1 if conflict,
774 * 0 else
775 **/
776
777static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl *t,
778 const struct ipv6hdr *hdr)
779{
780 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
781}
782
783static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev)
784{
785 struct ip6_tnl *t = netdev_priv(dev);
786 int encap_limit = -1;
787 struct flowi6 fl6;
788 __u32 mtu;
789 int err;
790
791 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
792 encap_limit = t->parms.encap_limit;
793
William Tu6712abc2017-12-01 15:26:08 -0800794 if (!t->parms.collect_md)
795 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000796
Tom Herbertb05229f2016-04-29 17:12:21 -0700797 err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
798 if (err)
799 return err;
800
801 err = __gre6_xmit(skb, dev, 0, &fl6, encap_limit, &mtu, skb->protocol);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000802
803 return err;
804}
805
806static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb,
807 struct net_device *dev)
808{
809 struct ip6_tnl *t = netdev_priv(dev);
810 struct net_device_stats *stats = &t->dev->stats;
811 int ret;
812
Steffen Klassertd5005142014-11-05 08:02:48 +0100813 if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
Tommi Rantala41ab3e32013-02-06 03:24:02 +0000814 goto tx_err;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000815
816 switch (skb->protocol) {
817 case htons(ETH_P_IP):
818 ret = ip6gre_xmit_ipv4(skb, dev);
819 break;
820 case htons(ETH_P_IPV6):
821 ret = ip6gre_xmit_ipv6(skb, dev);
822 break;
823 default:
824 ret = ip6gre_xmit_other(skb, dev);
825 break;
826 }
827
828 if (ret < 0)
829 goto tx_err;
830
831 return NETDEV_TX_OK;
832
833tx_err:
834 stats->tx_errors++;
835 stats->tx_dropped++;
836 kfree_skb(skb);
837 return NETDEV_TX_OK;
838}
839
William Tu5a963eb2017-11-30 11:51:29 -0800840static netdev_tx_t ip6erspan_tunnel_xmit(struct sk_buff *skb,
841 struct net_device *dev)
842{
843 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
844 struct ip6_tnl *t = netdev_priv(dev);
845 struct dst_entry *dst = skb_dst(skb);
846 struct net_device_stats *stats;
847 bool truncate = false;
848 int encap_limit = -1;
849 __u8 dsfield = false;
850 struct flowi6 fl6;
851 int err = -EINVAL;
852 __u32 mtu;
853
854 if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
855 goto tx_err;
856
857 if (gre_handle_offloads(skb, false))
858 goto tx_err;
859
860 switch (skb->protocol) {
861 case htons(ETH_P_IP):
862 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
863 prepare_ip6gre_xmit_ipv4(skb, dev, &fl6,
864 &dsfield, &encap_limit);
865 break;
866 case htons(ETH_P_IPV6):
867 if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
868 goto tx_err;
869 if (prepare_ip6gre_xmit_ipv6(skb, dev, &fl6,
870 &dsfield, &encap_limit))
871 goto tx_err;
872 break;
873 default:
874 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
875 break;
876 }
877
878 if (skb->len > dev->mtu + dev->hard_header_len) {
879 pskb_trim(skb, dev->mtu + dev->hard_header_len);
880 truncate = true;
881 }
882
883 erspan_build_header(skb, t->parms.o_key, t->parms.index,
884 truncate, false);
885 t->parms.o_flags &= ~TUNNEL_KEY;
886
887 IPCB(skb)->flags = 0;
888 fl6.daddr = t->parms.raddr;
889
890 /* Push GRE header. */
891 gre_build_header(skb, 8, TUNNEL_SEQ,
892 htons(ETH_P_ERSPAN), 0, htonl(t->o_seqno++));
893
894 /* TooBig packet may have updated dst->dev's mtu */
895 if (dst && dst_mtu(dst) > dst->dev->mtu)
896 dst->ops->update_pmtu(dst, NULL, skb, dst->dev->mtu);
897
898 err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
899 NEXTHDR_GRE);
900 if (err != 0) {
901 /* XXX: send ICMP error even if DF is not set. */
902 if (err == -EMSGSIZE) {
903 if (skb->protocol == htons(ETH_P_IP))
904 icmp_send(skb, ICMP_DEST_UNREACH,
905 ICMP_FRAG_NEEDED, htonl(mtu));
906 else
907 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
908 }
909
910 goto tx_err;
911 }
912 return NETDEV_TX_OK;
913
914tx_err:
915 stats = &t->dev->stats;
916 stats->tx_errors++;
917 stats->tx_dropped++;
918 kfree_skb(skb);
919 return NETDEV_TX_OK;
920}
921
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000922static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu)
923{
924 struct net_device *dev = t->dev;
925 struct __ip6_tnl_parm *p = &t->parms;
926 struct flowi6 *fl6 = &t->fl.u.ip6;
Tom Herbertdb2ec952016-05-09 17:12:08 -0700927 int t_hlen;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000928
929 if (dev->type != ARPHRD_ETHER) {
930 memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
931 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
932 }
933
934 /* Set up flowi template */
935 fl6->saddr = p->laddr;
936 fl6->daddr = p->raddr;
937 fl6->flowi6_oif = p->link;
938 fl6->flowlabel = 0;
Haishuang Yan252f3f52016-05-21 18:17:35 +0800939 fl6->flowi6_proto = IPPROTO_GRE;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000940
941 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
942 fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
943 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
944 fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
945
946 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
947 p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
948
949 if (p->flags&IP6_TNL_F_CAP_XMIT &&
950 p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER)
951 dev->flags |= IFF_POINTOPOINT;
952 else
953 dev->flags &= ~IFF_POINTOPOINT;
954
Tom Herbertdb2ec952016-05-09 17:12:08 -0700955 t->tun_hlen = gre_calc_hlen(t->parms.o_flags);
956
Tom Herbert1faf3d92016-05-18 09:06:19 -0700957 t->hlen = t->encap_hlen + t->tun_hlen;
Tom Herbertdb2ec952016-05-09 17:12:08 -0700958
959 t_hlen = t->hlen + sizeof(struct ipv6hdr);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000960
961 if (p->flags & IP6_TNL_F_CAP_XMIT) {
962 int strict = (ipv6_addr_type(&p->raddr) &
963 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
964
Nicolas Dichtel22f08062014-04-22 10:15:24 +0200965 struct rt6_info *rt = rt6_lookup(t->net,
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000966 &p->raddr, &p->laddr,
967 p->link, strict);
968
Ian Morris63159f22015-03-29 14:00:04 +0100969 if (!rt)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000970 return;
971
972 if (rt->dst.dev) {
Tom Herbertdb2ec952016-05-09 17:12:08 -0700973 dev->hard_header_len = rt->dst.dev->hard_header_len +
974 t_hlen;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000975
976 if (set_mtu) {
Tom Herbertdb2ec952016-05-09 17:12:08 -0700977 dev->mtu = rt->dst.dev->mtu - t_hlen;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000978 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
979 dev->mtu -= 8;
Alexander Duycka9e242c2016-04-14 15:33:45 -0400980 if (dev->type == ARPHRD_ETHER)
981 dev->mtu -= ETH_HLEN;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000982
983 if (dev->mtu < IPV6_MIN_MTU)
984 dev->mtu = IPV6_MIN_MTU;
985 }
986 }
Amerigo Wang94e187c2012-10-29 00:13:19 +0000987 ip6_rt_put(rt);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000988 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000989}
990
991static int ip6gre_tnl_change(struct ip6_tnl *t,
992 const struct __ip6_tnl_parm *p, int set_mtu)
993{
994 t->parms.laddr = p->laddr;
995 t->parms.raddr = p->raddr;
996 t->parms.flags = p->flags;
997 t->parms.hop_limit = p->hop_limit;
998 t->parms.encap_limit = p->encap_limit;
999 t->parms.flowinfo = p->flowinfo;
1000 t->parms.link = p->link;
1001 t->parms.proto = p->proto;
1002 t->parms.i_key = p->i_key;
1003 t->parms.o_key = p->o_key;
1004 t->parms.i_flags = p->i_flags;
1005 t->parms.o_flags = p->o_flags;
Craig Gallek0a473b82017-04-19 12:30:53 -04001006 t->parms.fwmark = p->fwmark;
Paolo Abeni607f7252016-02-12 15:43:54 +01001007 dst_cache_reset(&t->dst_cache);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001008 ip6gre_tnl_link_config(t, set_mtu);
1009 return 0;
1010}
1011
1012static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p,
1013 const struct ip6_tnl_parm2 *u)
1014{
1015 p->laddr = u->laddr;
1016 p->raddr = u->raddr;
1017 p->flags = u->flags;
1018 p->hop_limit = u->hop_limit;
1019 p->encap_limit = u->encap_limit;
1020 p->flowinfo = u->flowinfo;
1021 p->link = u->link;
1022 p->i_key = u->i_key;
1023 p->o_key = u->o_key;
Tom Herbertf41fe3c2016-05-09 17:12:09 -07001024 p->i_flags = gre_flags_to_tnl_flags(u->i_flags);
1025 p->o_flags = gre_flags_to_tnl_flags(u->o_flags);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001026 memcpy(p->name, u->name, sizeof(u->name));
1027}
1028
1029static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u,
1030 const struct __ip6_tnl_parm *p)
1031{
1032 u->proto = IPPROTO_GRE;
1033 u->laddr = p->laddr;
1034 u->raddr = p->raddr;
1035 u->flags = p->flags;
1036 u->hop_limit = p->hop_limit;
1037 u->encap_limit = p->encap_limit;
1038 u->flowinfo = p->flowinfo;
1039 u->link = p->link;
1040 u->i_key = p->i_key;
1041 u->o_key = p->o_key;
Tom Herbertf41fe3c2016-05-09 17:12:09 -07001042 u->i_flags = gre_tnl_flags_to_gre_flags(p->i_flags);
1043 u->o_flags = gre_tnl_flags_to_gre_flags(p->o_flags);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001044 memcpy(u->name, p->name, sizeof(u->name));
1045}
1046
1047static int ip6gre_tunnel_ioctl(struct net_device *dev,
1048 struct ifreq *ifr, int cmd)
1049{
1050 int err = 0;
1051 struct ip6_tnl_parm2 p;
1052 struct __ip6_tnl_parm p1;
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001053 struct ip6_tnl *t = netdev_priv(dev);
1054 struct net *net = t->net;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001055 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1056
Tom Herbert308edfdf2016-04-29 17:12:17 -07001057 memset(&p1, 0, sizeof(p1));
1058
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001059 switch (cmd) {
1060 case SIOCGETTUNNEL:
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001061 if (dev == ign->fb_tunnel_dev) {
1062 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
1063 err = -EFAULT;
1064 break;
1065 }
1066 ip6gre_tnl_parm_from_user(&p1, &p);
1067 t = ip6gre_tunnel_locate(net, &p1, 0);
Ian Morris63159f22015-03-29 14:00:04 +01001068 if (!t)
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001069 t = netdev_priv(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001070 }
Amerigo Wang5dbd5062013-05-09 21:56:37 +00001071 memset(&p, 0, sizeof(p));
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001072 ip6gre_tnl_parm_to_user(&p, &t->parms);
1073 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
1074 err = -EFAULT;
1075 break;
1076
1077 case SIOCADDTUNNEL:
1078 case SIOCCHGTUNNEL:
1079 err = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00001080 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001081 goto done;
1082
1083 err = -EFAULT;
1084 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1085 goto done;
1086
1087 err = -EINVAL;
1088 if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))
1089 goto done;
1090
1091 if (!(p.i_flags&GRE_KEY))
1092 p.i_key = 0;
1093 if (!(p.o_flags&GRE_KEY))
1094 p.o_key = 0;
1095
1096 ip6gre_tnl_parm_from_user(&p1, &p);
1097 t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL);
1098
1099 if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
Ian Morris53b24b82015-03-29 14:00:05 +01001100 if (t) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001101 if (t->dev != dev) {
1102 err = -EEXIST;
1103 break;
1104 }
1105 } else {
1106 t = netdev_priv(dev);
1107
1108 ip6gre_tunnel_unlink(ign, t);
1109 synchronize_net();
1110 ip6gre_tnl_change(t, &p1, 1);
1111 ip6gre_tunnel_link(ign, t);
1112 netdev_state_change(dev);
1113 }
1114 }
1115
1116 if (t) {
1117 err = 0;
1118
Amerigo Wang5dbd5062013-05-09 21:56:37 +00001119 memset(&p, 0, sizeof(p));
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001120 ip6gre_tnl_parm_to_user(&p, &t->parms);
1121 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
1122 err = -EFAULT;
1123 } else
1124 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
1125 break;
1126
1127 case SIOCDELTUNNEL:
1128 err = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00001129 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001130 goto done;
1131
1132 if (dev == ign->fb_tunnel_dev) {
1133 err = -EFAULT;
1134 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1135 goto done;
1136 err = -ENOENT;
1137 ip6gre_tnl_parm_from_user(&p1, &p);
1138 t = ip6gre_tunnel_locate(net, &p1, 0);
Ian Morris63159f22015-03-29 14:00:04 +01001139 if (!t)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001140 goto done;
1141 err = -EPERM;
1142 if (t == netdev_priv(ign->fb_tunnel_dev))
1143 goto done;
1144 dev = t->dev;
1145 }
1146 unregister_netdevice(dev);
1147 err = 0;
1148 break;
1149
1150 default:
1151 err = -EINVAL;
1152 }
1153
1154done:
1155 return err;
1156}
1157
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001158static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
Xin Long76cc0d32017-09-15 12:00:07 +08001159 unsigned short type, const void *daddr,
1160 const void *saddr, unsigned int len)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001161{
1162 struct ip6_tnl *t = netdev_priv(dev);
Xin Long76cc0d32017-09-15 12:00:07 +08001163 struct ipv6hdr *ipv6h;
1164 __be16 *p;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001165
Xin Long76cc0d32017-09-15 12:00:07 +08001166 ipv6h = skb_push(skb, t->hlen + sizeof(*ipv6h));
1167 ip6_flow_hdr(ipv6h, 0, ip6_make_flowlabel(dev_net(dev), skb,
1168 t->fl.u.ip6.flowlabel,
1169 true, &t->fl.u.ip6));
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001170 ipv6h->hop_limit = t->parms.hop_limit;
1171 ipv6h->nexthdr = NEXTHDR_GRE;
1172 ipv6h->saddr = t->parms.laddr;
1173 ipv6h->daddr = t->parms.raddr;
1174
Xin Long76cc0d32017-09-15 12:00:07 +08001175 p = (__be16 *)(ipv6h + 1);
1176 p[0] = t->parms.o_flags;
1177 p[1] = htons(type);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001178
1179 /*
1180 * Set the source hardware address.
1181 */
1182
1183 if (saddr)
1184 memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr));
1185 if (daddr)
1186 memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
1187 if (!ipv6_addr_any(&ipv6h->daddr))
1188 return t->hlen;
1189
1190 return -t->hlen;
1191}
1192
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001193static const struct header_ops ip6gre_header_ops = {
1194 .create = ip6gre_header,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001195};
1196
1197static const struct net_device_ops ip6gre_netdev_ops = {
1198 .ndo_init = ip6gre_tunnel_init,
1199 .ndo_uninit = ip6gre_tunnel_uninit,
1200 .ndo_start_xmit = ip6gre_tunnel_xmit,
1201 .ndo_do_ioctl = ip6gre_tunnel_ioctl,
Tom Herbertb05229f2016-04-29 17:12:21 -07001202 .ndo_change_mtu = ip6_tnl_change_mtu,
Pravin B Shelarf61dd382013-03-25 14:50:00 +00001203 .ndo_get_stats64 = ip_tunnel_get_stats64,
Nicolas Dichtelecf2c062015-04-02 17:07:01 +02001204 .ndo_get_iflink = ip6_tnl_get_iflink,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001205};
1206
1207static void ip6gre_dev_free(struct net_device *dev)
1208{
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001209 struct ip6_tnl *t = netdev_priv(dev);
1210
Paolo Abeni607f7252016-02-12 15:43:54 +01001211 dst_cache_destroy(&t->dst_cache);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001212 free_percpu(dev->tstats);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001213}
1214
1215static void ip6gre_tunnel_setup(struct net_device *dev)
1216{
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001217 dev->netdev_ops = &ip6gre_netdev_ops;
David S. Millercf124db2017-05-08 12:52:56 -04001218 dev->needs_free_netdev = true;
1219 dev->priv_destructor = ip6gre_dev_free;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001220
1221 dev->type = ARPHRD_IP6GRE;
Tom Herbertb05229f2016-04-29 17:12:21 -07001222
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001223 dev->flags |= IFF_NOARP;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001224 dev->addr_len = sizeof(struct in6_addr);
Eric Dumazet02875872014-10-05 18:38:35 -07001225 netif_keep_dst(dev);
Felix Jia45ce0fd2017-01-26 16:59:18 +13001226 /* This perm addr will be used as interface identifier by IPv6 */
1227 dev->addr_assign_type = NET_ADDR_RANDOM;
1228 eth_random_addr(dev->perm_addr);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001229}
1230
Martin KaFai Laua3c119d2015-09-15 14:30:05 -07001231static int ip6gre_tunnel_init_common(struct net_device *dev)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001232{
1233 struct ip6_tnl *tunnel;
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001234 int ret;
Tom Herbertb05229f2016-04-29 17:12:21 -07001235 int t_hlen;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001236
1237 tunnel = netdev_priv(dev);
1238
1239 tunnel->dev = dev;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001240 tunnel->net = dev_net(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001241 strcpy(tunnel->parms.name, dev->name);
1242
Martin KaFai Laua3c119d2015-09-15 14:30:05 -07001243 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1244 if (!dev->tstats)
1245 return -ENOMEM;
1246
Paolo Abeni607f7252016-02-12 15:43:54 +01001247 ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001248 if (ret) {
1249 free_percpu(dev->tstats);
1250 dev->tstats = NULL;
1251 return ret;
1252 }
1253
Tom Herbertb05229f2016-04-29 17:12:21 -07001254 tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
Tom Herbert1faf3d92016-05-18 09:06:19 -07001255 tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen;
Tom Herbertb05229f2016-04-29 17:12:21 -07001256 t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
1257
Tom Herbertdb2ec952016-05-09 17:12:08 -07001258 dev->hard_header_len = LL_MAX_HEADER + t_hlen;
1259 dev->mtu = ETH_DATA_LEN - t_hlen;
Haishuang Yan1b227e52016-05-21 18:17:34 +08001260 if (dev->type == ARPHRD_ETHER)
1261 dev->mtu -= ETH_HLEN;
Tom Herbertb05229f2016-04-29 17:12:21 -07001262 if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1263 dev->mtu -= 8;
1264
William Tu6712abc2017-12-01 15:26:08 -08001265 if (tunnel->parms.collect_md) {
1266 dev->features |= NETIF_F_NETNS_LOCAL;
1267 netif_keep_dst(dev);
1268 }
1269
Martin KaFai Laua3c119d2015-09-15 14:30:05 -07001270 return 0;
1271}
1272
1273static int ip6gre_tunnel_init(struct net_device *dev)
1274{
1275 struct ip6_tnl *tunnel;
1276 int ret;
1277
1278 ret = ip6gre_tunnel_init_common(dev);
1279 if (ret)
1280 return ret;
1281
1282 tunnel = netdev_priv(dev);
1283
William Tu6712abc2017-12-01 15:26:08 -08001284 if (tunnel->parms.collect_md)
1285 return 0;
1286
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001287 memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr));
1288 memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr));
1289
1290 if (ipv6_addr_any(&tunnel->parms.raddr))
1291 dev->header_ops = &ip6gre_header_ops;
1292
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001293 return 0;
1294}
1295
1296static void ip6gre_fb_tunnel_init(struct net_device *dev)
1297{
1298 struct ip6_tnl *tunnel = netdev_priv(dev);
1299
1300 tunnel->dev = dev;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001301 tunnel->net = dev_net(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001302 strcpy(tunnel->parms.name, dev->name);
1303
1304 tunnel->hlen = sizeof(struct ipv6hdr) + 4;
1305
1306 dev_hold(dev);
1307}
1308
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001309static struct inet6_protocol ip6gre_protocol __read_mostly = {
Tom Herbert308edfdf2016-04-29 17:12:17 -07001310 .handler = gre_rcv,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001311 .err_handler = ip6gre_err,
1312 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1313};
1314
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001315static void ip6gre_destroy_tunnels(struct net *net, struct list_head *head)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001316{
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001317 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1318 struct net_device *dev, *aux;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001319 int prio;
1320
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001321 for_each_netdev_safe(net, dev, aux)
1322 if (dev->rtnl_link_ops == &ip6gre_link_ops ||
William Tu5a963eb2017-11-30 11:51:29 -08001323 dev->rtnl_link_ops == &ip6gre_tap_ops ||
1324 dev->rtnl_link_ops == &ip6erspan_tap_ops)
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001325 unregister_netdevice_queue(dev, head);
1326
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001327 for (prio = 0; prio < 4; prio++) {
1328 int h;
Jiri Kosinae87a8f22016-08-10 11:03:35 +02001329 for (h = 0; h < IP6_GRE_HASH_SIZE; h++) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001330 struct ip6_tnl *t;
1331
1332 t = rtnl_dereference(ign->tunnels[prio][h]);
1333
Ian Morris53b24b82015-03-29 14:00:05 +01001334 while (t) {
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001335 /* If dev is in the same netns, it has already
1336 * been added to the list by the previous loop.
1337 */
1338 if (!net_eq(dev_net(t->dev), net))
1339 unregister_netdevice_queue(t->dev,
1340 head);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001341 t = rtnl_dereference(t->next);
1342 }
1343 }
1344 }
1345}
1346
1347static int __net_init ip6gre_init_net(struct net *net)
1348{
1349 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1350 int err;
1351
1352 ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0",
Tom Gundersenc835a672014-07-14 16:37:24 +02001353 NET_NAME_UNKNOWN,
1354 ip6gre_tunnel_setup);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001355 if (!ign->fb_tunnel_dev) {
1356 err = -ENOMEM;
1357 goto err_alloc_dev;
1358 }
1359 dev_net_set(ign->fb_tunnel_dev, net);
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001360 /* FB netdevice is special: we have one, and only one per netns.
1361 * Allowing to move it to another netns is clearly unsafe.
1362 */
1363 ign->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL;
1364
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001365
1366 ip6gre_fb_tunnel_init(ign->fb_tunnel_dev);
1367 ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops;
1368
1369 err = register_netdev(ign->fb_tunnel_dev);
1370 if (err)
1371 goto err_reg_dev;
1372
1373 rcu_assign_pointer(ign->tunnels_wc[0],
1374 netdev_priv(ign->fb_tunnel_dev));
1375 return 0;
1376
1377err_reg_dev:
David S. Millercf124db2017-05-08 12:52:56 -04001378 free_netdev(ign->fb_tunnel_dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001379err_alloc_dev:
1380 return err;
1381}
1382
Eric Dumazetbb401ca2017-09-19 16:27:08 -07001383static void __net_exit ip6gre_exit_batch_net(struct list_head *net_list)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001384{
Eric Dumazetbb401ca2017-09-19 16:27:08 -07001385 struct net *net;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001386 LIST_HEAD(list);
1387
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001388 rtnl_lock();
Eric Dumazetbb401ca2017-09-19 16:27:08 -07001389 list_for_each_entry(net, net_list, exit_list)
1390 ip6gre_destroy_tunnels(net, &list);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001391 unregister_netdevice_many(&list);
1392 rtnl_unlock();
1393}
1394
1395static struct pernet_operations ip6gre_net_ops = {
1396 .init = ip6gre_init_net,
Eric Dumazetbb401ca2017-09-19 16:27:08 -07001397 .exit_batch = ip6gre_exit_batch_net,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001398 .id = &ip6gre_net_id,
1399 .size = sizeof(struct ip6gre_net),
1400};
1401
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001402static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[],
1403 struct netlink_ext_ack *extack)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001404{
1405 __be16 flags;
1406
1407 if (!data)
1408 return 0;
1409
1410 flags = 0;
1411 if (data[IFLA_GRE_IFLAGS])
1412 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1413 if (data[IFLA_GRE_OFLAGS])
1414 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1415 if (flags & (GRE_VERSION|GRE_ROUTING))
1416 return -EINVAL;
1417
1418 return 0;
1419}
1420
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001421static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[],
1422 struct netlink_ext_ack *extack)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001423{
1424 struct in6_addr daddr;
1425
1426 if (tb[IFLA_ADDRESS]) {
1427 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1428 return -EINVAL;
1429 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1430 return -EADDRNOTAVAIL;
1431 }
1432
1433 if (!data)
1434 goto out;
1435
1436 if (data[IFLA_GRE_REMOTE]) {
Jiri Benc67b61f62015-03-29 16:59:26 +02001437 daddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001438 if (ipv6_addr_any(&daddr))
1439 return -EINVAL;
1440 }
1441
1442out:
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001443 return ip6gre_tunnel_validate(tb, data, extack);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001444}
1445
William Tu5a963eb2017-11-30 11:51:29 -08001446static int ip6erspan_tap_validate(struct nlattr *tb[], struct nlattr *data[],
1447 struct netlink_ext_ack *extack)
1448{
1449 __be16 flags = 0;
1450 int ret;
1451
1452 if (!data)
1453 return 0;
1454
1455 ret = ip6gre_tap_validate(tb, data, extack);
1456 if (ret)
1457 return ret;
1458
1459 /* ERSPAN should only have GRE sequence and key flag */
1460 if (data[IFLA_GRE_OFLAGS])
1461 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1462 if (data[IFLA_GRE_IFLAGS])
1463 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1464 if (!data[IFLA_GRE_COLLECT_METADATA] &&
1465 flags != (GRE_SEQ | GRE_KEY))
1466 return -EINVAL;
1467
1468 /* ERSPAN Session ID only has 10-bit. Since we reuse
1469 * 32-bit key field as ID, check it's range.
1470 */
1471 if (data[IFLA_GRE_IKEY] &&
1472 (ntohl(nla_get_be32(data[IFLA_GRE_IKEY])) & ~ID_MASK))
1473 return -EINVAL;
1474
1475 if (data[IFLA_GRE_OKEY] &&
1476 (ntohl(nla_get_be32(data[IFLA_GRE_OKEY])) & ~ID_MASK))
1477 return -EINVAL;
1478
1479 if (data[IFLA_GRE_ERSPAN_INDEX]) {
1480 u32 index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
1481
1482 if (index & ~INDEX_MASK)
1483 return -EINVAL;
1484 }
1485 return 0;
1486}
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001487
1488static void ip6gre_netlink_parms(struct nlattr *data[],
1489 struct __ip6_tnl_parm *parms)
1490{
1491 memset(parms, 0, sizeof(*parms));
1492
1493 if (!data)
1494 return;
1495
1496 if (data[IFLA_GRE_LINK])
1497 parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
1498
1499 if (data[IFLA_GRE_IFLAGS])
Tom Herbertf41fe3c2016-05-09 17:12:09 -07001500 parms->i_flags = gre_flags_to_tnl_flags(
1501 nla_get_be16(data[IFLA_GRE_IFLAGS]));
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001502
1503 if (data[IFLA_GRE_OFLAGS])
Tom Herbertf41fe3c2016-05-09 17:12:09 -07001504 parms->o_flags = gre_flags_to_tnl_flags(
1505 nla_get_be16(data[IFLA_GRE_OFLAGS]));
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001506
1507 if (data[IFLA_GRE_IKEY])
1508 parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
1509
1510 if (data[IFLA_GRE_OKEY])
1511 parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
1512
1513 if (data[IFLA_GRE_LOCAL])
Jiri Benc67b61f62015-03-29 16:59:26 +02001514 parms->laddr = nla_get_in6_addr(data[IFLA_GRE_LOCAL]);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001515
1516 if (data[IFLA_GRE_REMOTE])
Jiri Benc67b61f62015-03-29 16:59:26 +02001517 parms->raddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001518
1519 if (data[IFLA_GRE_TTL])
1520 parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]);
1521
1522 if (data[IFLA_GRE_ENCAP_LIMIT])
1523 parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]);
1524
1525 if (data[IFLA_GRE_FLOWINFO])
Lance Richardsonc2675de2016-09-24 14:01:04 -04001526 parms->flowinfo = nla_get_be32(data[IFLA_GRE_FLOWINFO]);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001527
1528 if (data[IFLA_GRE_FLAGS])
1529 parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]);
Craig Gallek0a473b82017-04-19 12:30:53 -04001530
1531 if (data[IFLA_GRE_FWMARK])
1532 parms->fwmark = nla_get_u32(data[IFLA_GRE_FWMARK]);
William Tu5a963eb2017-11-30 11:51:29 -08001533
1534 if (data[IFLA_GRE_ERSPAN_INDEX])
1535 parms->index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
William Tu6712abc2017-12-01 15:26:08 -08001536
1537 if (data[IFLA_GRE_COLLECT_METADATA])
1538 parms->collect_md = true;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001539}
1540
1541static int ip6gre_tap_init(struct net_device *dev)
1542{
1543 struct ip6_tnl *tunnel;
Martin KaFai Laua3c119d2015-09-15 14:30:05 -07001544 int ret;
1545
1546 ret = ip6gre_tunnel_init_common(dev);
1547 if (ret)
1548 return ret;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001549
Shweta Choudaha0a46baa2016-06-08 20:15:43 +01001550 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1551
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001552 tunnel = netdev_priv(dev);
1553
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001554 ip6gre_tnl_link_config(tunnel, 1);
1555
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001556 return 0;
1557}
1558
1559static const struct net_device_ops ip6gre_tap_netdev_ops = {
1560 .ndo_init = ip6gre_tap_init,
1561 .ndo_uninit = ip6gre_tunnel_uninit,
1562 .ndo_start_xmit = ip6gre_tunnel_xmit,
1563 .ndo_set_mac_address = eth_mac_addr,
1564 .ndo_validate_addr = eth_validate_addr,
Tom Herbertb05229f2016-04-29 17:12:21 -07001565 .ndo_change_mtu = ip6_tnl_change_mtu,
Pravin B Shelarf61dd382013-03-25 14:50:00 +00001566 .ndo_get_stats64 = ip_tunnel_get_stats64,
Nicolas Dichtelecf2c062015-04-02 17:07:01 +02001567 .ndo_get_iflink = ip6_tnl_get_iflink,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001568};
1569
Alexander Duyckac4eb002016-04-14 15:33:51 -04001570#define GRE6_FEATURES (NETIF_F_SG | \
1571 NETIF_F_FRAGLIST | \
1572 NETIF_F_HIGHDMA | \
1573 NETIF_F_HW_CSUM)
1574
William Tu5a963eb2017-11-30 11:51:29 -08001575static int ip6erspan_tap_init(struct net_device *dev)
1576{
1577 struct ip6_tnl *tunnel;
1578 int t_hlen;
1579 int ret;
1580
1581 tunnel = netdev_priv(dev);
1582
1583 tunnel->dev = dev;
1584 tunnel->net = dev_net(dev);
1585 strcpy(tunnel->parms.name, dev->name);
1586
1587 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1588 if (!dev->tstats)
1589 return -ENOMEM;
1590
1591 ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
1592 if (ret) {
1593 free_percpu(dev->tstats);
1594 dev->tstats = NULL;
1595 return ret;
1596 }
1597
1598 tunnel->tun_hlen = 8;
1599 tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen +
1600 sizeof(struct erspanhdr);
1601 t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
1602
1603 dev->hard_header_len = LL_MAX_HEADER + t_hlen;
1604 dev->mtu = ETH_DATA_LEN - t_hlen;
1605 if (dev->type == ARPHRD_ETHER)
1606 dev->mtu -= ETH_HLEN;
1607 if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1608 dev->mtu -= 8;
1609
1610 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1611 tunnel = netdev_priv(dev);
1612 ip6gre_tnl_link_config(tunnel, 1);
1613
1614 return 0;
1615}
1616
1617static const struct net_device_ops ip6erspan_netdev_ops = {
1618 .ndo_init = ip6erspan_tap_init,
1619 .ndo_uninit = ip6gre_tunnel_uninit,
1620 .ndo_start_xmit = ip6erspan_tunnel_xmit,
1621 .ndo_set_mac_address = eth_mac_addr,
1622 .ndo_validate_addr = eth_validate_addr,
1623 .ndo_change_mtu = ip6_tnl_change_mtu,
1624 .ndo_get_stats64 = ip_tunnel_get_stats64,
1625 .ndo_get_iflink = ip6_tnl_get_iflink,
1626};
1627
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001628static void ip6gre_tap_setup(struct net_device *dev)
1629{
1630
1631 ether_setup(dev);
1632
1633 dev->netdev_ops = &ip6gre_tap_netdev_ops;
David S. Millercf124db2017-05-08 12:52:56 -04001634 dev->needs_free_netdev = true;
1635 dev->priv_destructor = ip6gre_dev_free;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001636
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001637 dev->features |= NETIF_F_NETNS_LOCAL;
Jiri Bencd13b1612016-02-17 15:32:53 +01001638 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
Shweta Choudaha0a46baa2016-06-08 20:15:43 +01001639 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Xin Long2d405572017-09-28 13:23:50 +08001640 netif_keep_dst(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001641}
1642
Tom Herbert1faf3d92016-05-18 09:06:19 -07001643static bool ip6gre_netlink_encap_parms(struct nlattr *data[],
1644 struct ip_tunnel_encap *ipencap)
1645{
1646 bool ret = false;
1647
1648 memset(ipencap, 0, sizeof(*ipencap));
1649
1650 if (!data)
1651 return ret;
1652
1653 if (data[IFLA_GRE_ENCAP_TYPE]) {
1654 ret = true;
1655 ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]);
1656 }
1657
1658 if (data[IFLA_GRE_ENCAP_FLAGS]) {
1659 ret = true;
1660 ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]);
1661 }
1662
1663 if (data[IFLA_GRE_ENCAP_SPORT]) {
1664 ret = true;
1665 ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]);
1666 }
1667
1668 if (data[IFLA_GRE_ENCAP_DPORT]) {
1669 ret = true;
1670 ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]);
1671 }
1672
1673 return ret;
1674}
1675
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001676static int ip6gre_newlink(struct net *src_net, struct net_device *dev,
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02001677 struct nlattr *tb[], struct nlattr *data[],
1678 struct netlink_ext_ack *extack)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001679{
1680 struct ip6_tnl *nt;
1681 struct net *net = dev_net(dev);
1682 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
Tom Herbert1faf3d92016-05-18 09:06:19 -07001683 struct ip_tunnel_encap ipencap;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001684 int err;
1685
1686 nt = netdev_priv(dev);
Tom Herbert1faf3d92016-05-18 09:06:19 -07001687
1688 if (ip6gre_netlink_encap_parms(data, &ipencap)) {
1689 int err = ip6_tnl_encap_setup(nt, &ipencap);
1690
1691 if (err < 0)
1692 return err;
1693 }
1694
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001695 ip6gre_netlink_parms(data, &nt->parms);
1696
William Tu6712abc2017-12-01 15:26:08 -08001697 if (nt->parms.collect_md) {
1698 if (rtnl_dereference(ign->collect_md_tun))
1699 return -EEXIST;
1700 } else {
1701 if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
1702 return -EEXIST;
1703 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001704
1705 if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
1706 eth_hw_addr_random(dev);
1707
1708 nt->dev = dev;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001709 nt->net = dev_net(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001710 ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]);
1711
Alexander Duyckac4eb002016-04-14 15:33:51 -04001712 dev->features |= GRE6_FEATURES;
1713 dev->hw_features |= GRE6_FEATURES;
1714
Tom Herbertb45bd1d2016-05-09 17:12:12 -07001715 if (!(nt->parms.o_flags & TUNNEL_SEQ)) {
Alexander Duyck6a553682016-05-18 10:44:47 -07001716 /* TCP offload with GRE SEQ is not supported, nor
1717 * can we support 2 levels of outer headers requiring
1718 * an update.
Alexander Duyck3a80e1f2016-04-14 15:34:04 -04001719 */
Alexander Duyck6a553682016-05-18 10:44:47 -07001720 if (!(nt->parms.o_flags & TUNNEL_CSUM) ||
1721 (nt->encap.type == TUNNEL_ENCAP_NONE)) {
1722 dev->features |= NETIF_F_GSO_SOFTWARE;
1723 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
1724 }
Alexander Duyck3a80e1f2016-04-14 15:34:04 -04001725
1726 /* Can use a lockless transmit, unless we generate
1727 * output sequences
1728 */
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001729 dev->features |= NETIF_F_LLTX;
Alexander Duyck3a80e1f2016-04-14 15:34:04 -04001730 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001731
1732 err = register_netdevice(dev);
1733 if (err)
1734 goto out;
1735
1736 dev_hold(dev);
1737 ip6gre_tunnel_link(ign, nt);
1738
1739out:
1740 return err;
1741}
1742
1743static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],
Matthias Schifferad744b22017-06-25 23:56:00 +02001744 struct nlattr *data[],
1745 struct netlink_ext_ack *extack)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001746{
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001747 struct ip6_tnl *t, *nt = netdev_priv(dev);
1748 struct net *net = nt->net;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001749 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1750 struct __ip6_tnl_parm p;
Tom Herbert1faf3d92016-05-18 09:06:19 -07001751 struct ip_tunnel_encap ipencap;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001752
1753 if (dev == ign->fb_tunnel_dev)
1754 return -EINVAL;
1755
Tom Herbert1faf3d92016-05-18 09:06:19 -07001756 if (ip6gre_netlink_encap_parms(data, &ipencap)) {
1757 int err = ip6_tnl_encap_setup(nt, &ipencap);
1758
1759 if (err < 0)
1760 return err;
1761 }
1762
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001763 ip6gre_netlink_parms(data, &p);
1764
1765 t = ip6gre_tunnel_locate(net, &p, 0);
1766
1767 if (t) {
1768 if (t->dev != dev)
1769 return -EEXIST;
1770 } else {
1771 t = nt;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001772 }
1773
Nicolas Dichtel6a61d4d2015-12-03 17:21:50 +01001774 ip6gre_tunnel_unlink(ign, t);
1775 ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]);
1776 ip6gre_tunnel_link(ign, t);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001777 return 0;
1778}
1779
Nicolas Dichtel54d63f782014-04-14 17:11:38 +02001780static void ip6gre_dellink(struct net_device *dev, struct list_head *head)
1781{
1782 struct net *net = dev_net(dev);
1783 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1784
1785 if (dev != ign->fb_tunnel_dev)
1786 unregister_netdevice_queue(dev, head);
1787}
1788
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001789static size_t ip6gre_get_size(const struct net_device *dev)
1790{
1791 return
1792 /* IFLA_GRE_LINK */
1793 nla_total_size(4) +
1794 /* IFLA_GRE_IFLAGS */
1795 nla_total_size(2) +
1796 /* IFLA_GRE_OFLAGS */
1797 nla_total_size(2) +
1798 /* IFLA_GRE_IKEY */
1799 nla_total_size(4) +
1800 /* IFLA_GRE_OKEY */
1801 nla_total_size(4) +
1802 /* IFLA_GRE_LOCAL */
Nicolas Dichtela3754132012-11-09 05:34:56 +00001803 nla_total_size(sizeof(struct in6_addr)) +
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001804 /* IFLA_GRE_REMOTE */
Nicolas Dichtela3754132012-11-09 05:34:56 +00001805 nla_total_size(sizeof(struct in6_addr)) +
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001806 /* IFLA_GRE_TTL */
1807 nla_total_size(1) +
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001808 /* IFLA_GRE_ENCAP_LIMIT */
1809 nla_total_size(1) +
1810 /* IFLA_GRE_FLOWINFO */
1811 nla_total_size(4) +
1812 /* IFLA_GRE_FLAGS */
1813 nla_total_size(4) +
Tom Herbert1faf3d92016-05-18 09:06:19 -07001814 /* IFLA_GRE_ENCAP_TYPE */
1815 nla_total_size(2) +
1816 /* IFLA_GRE_ENCAP_FLAGS */
1817 nla_total_size(2) +
1818 /* IFLA_GRE_ENCAP_SPORT */
1819 nla_total_size(2) +
1820 /* IFLA_GRE_ENCAP_DPORT */
1821 nla_total_size(2) +
William Tu6712abc2017-12-01 15:26:08 -08001822 /* IFLA_GRE_COLLECT_METADATA */
1823 nla_total_size(0) +
Craig Gallek0a473b82017-04-19 12:30:53 -04001824 /* IFLA_GRE_FWMARK */
1825 nla_total_size(4) +
William Tu5a963eb2017-11-30 11:51:29 -08001826 /* IFLA_GRE_ERSPAN_INDEX */
1827 nla_total_size(4) +
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001828 0;
1829}
1830
1831static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev)
1832{
1833 struct ip6_tnl *t = netdev_priv(dev);
1834 struct __ip6_tnl_parm *p = &t->parms;
1835
1836 if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
Tom Herbertf41fe3c2016-05-09 17:12:09 -07001837 nla_put_be16(skb, IFLA_GRE_IFLAGS,
1838 gre_tnl_flags_to_gre_flags(p->i_flags)) ||
1839 nla_put_be16(skb, IFLA_GRE_OFLAGS,
1840 gre_tnl_flags_to_gre_flags(p->o_flags)) ||
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001841 nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
1842 nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
Jiri Benc930345e2015-03-29 16:59:25 +02001843 nla_put_in6_addr(skb, IFLA_GRE_LOCAL, &p->laddr) ||
1844 nla_put_in6_addr(skb, IFLA_GRE_REMOTE, &p->raddr) ||
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001845 nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) ||
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001846 nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) ||
1847 nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) ||
Craig Gallek0a473b82017-04-19 12:30:53 -04001848 nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags) ||
William Tu5a963eb2017-11-30 11:51:29 -08001849 nla_put_u32(skb, IFLA_GRE_FWMARK, p->fwmark) ||
1850 nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, p->index))
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001851 goto nla_put_failure;
Tom Herbert1faf3d92016-05-18 09:06:19 -07001852
1853 if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE,
1854 t->encap.type) ||
1855 nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT,
1856 t->encap.sport) ||
1857 nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT,
1858 t->encap.dport) ||
1859 nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS,
1860 t->encap.flags))
1861 goto nla_put_failure;
1862
William Tu6712abc2017-12-01 15:26:08 -08001863 if (p->collect_md) {
1864 if (nla_put_flag(skb, IFLA_GRE_COLLECT_METADATA))
1865 goto nla_put_failure;
1866 }
1867
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001868 return 0;
1869
1870nla_put_failure:
1871 return -EMSGSIZE;
1872}
1873
1874static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = {
1875 [IFLA_GRE_LINK] = { .type = NLA_U32 },
1876 [IFLA_GRE_IFLAGS] = { .type = NLA_U16 },
1877 [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
1878 [IFLA_GRE_IKEY] = { .type = NLA_U32 },
1879 [IFLA_GRE_OKEY] = { .type = NLA_U32 },
1880 [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct ipv6hdr, saddr) },
1881 [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct ipv6hdr, daddr) },
1882 [IFLA_GRE_TTL] = { .type = NLA_U8 },
1883 [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 },
1884 [IFLA_GRE_FLOWINFO] = { .type = NLA_U32 },
1885 [IFLA_GRE_FLAGS] = { .type = NLA_U32 },
Tom Herbert1faf3d92016-05-18 09:06:19 -07001886 [IFLA_GRE_ENCAP_TYPE] = { .type = NLA_U16 },
1887 [IFLA_GRE_ENCAP_FLAGS] = { .type = NLA_U16 },
1888 [IFLA_GRE_ENCAP_SPORT] = { .type = NLA_U16 },
1889 [IFLA_GRE_ENCAP_DPORT] = { .type = NLA_U16 },
William Tu6712abc2017-12-01 15:26:08 -08001890 [IFLA_GRE_COLLECT_METADATA] = { .type = NLA_FLAG },
Craig Gallek0a473b82017-04-19 12:30:53 -04001891 [IFLA_GRE_FWMARK] = { .type = NLA_U32 },
William Tu5a963eb2017-11-30 11:51:29 -08001892 [IFLA_GRE_ERSPAN_INDEX] = { .type = NLA_U32 },
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001893};
1894
William Tu5a963eb2017-11-30 11:51:29 -08001895static void ip6erspan_tap_setup(struct net_device *dev)
1896{
1897 ether_setup(dev);
1898
1899 dev->netdev_ops = &ip6erspan_netdev_ops;
1900 dev->needs_free_netdev = true;
1901 dev->priv_destructor = ip6gre_dev_free;
1902
1903 dev->features |= NETIF_F_NETNS_LOCAL;
1904 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1905 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1906 netif_keep_dst(dev);
1907}
1908
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001909static struct rtnl_link_ops ip6gre_link_ops __read_mostly = {
1910 .kind = "ip6gre",
1911 .maxtype = IFLA_GRE_MAX,
1912 .policy = ip6gre_policy,
1913 .priv_size = sizeof(struct ip6_tnl),
1914 .setup = ip6gre_tunnel_setup,
1915 .validate = ip6gre_tunnel_validate,
1916 .newlink = ip6gre_newlink,
1917 .changelink = ip6gre_changelink,
Nicolas Dichtel54d63f782014-04-14 17:11:38 +02001918 .dellink = ip6gre_dellink,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001919 .get_size = ip6gre_get_size,
1920 .fill_info = ip6gre_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01001921 .get_link_net = ip6_tnl_get_link_net,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001922};
1923
1924static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = {
1925 .kind = "ip6gretap",
1926 .maxtype = IFLA_GRE_MAX,
1927 .policy = ip6gre_policy,
1928 .priv_size = sizeof(struct ip6_tnl),
1929 .setup = ip6gre_tap_setup,
1930 .validate = ip6gre_tap_validate,
1931 .newlink = ip6gre_newlink,
1932 .changelink = ip6gre_changelink,
1933 .get_size = ip6gre_get_size,
1934 .fill_info = ip6gre_fill_info,
Nicolas Dichtel3390e392015-01-20 15:15:43 +01001935 .get_link_net = ip6_tnl_get_link_net,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001936};
1937
William Tu5a963eb2017-11-30 11:51:29 -08001938static struct rtnl_link_ops ip6erspan_tap_ops __read_mostly = {
1939 .kind = "ip6erspan",
1940 .maxtype = IFLA_GRE_MAX,
1941 .policy = ip6gre_policy,
1942 .priv_size = sizeof(struct ip6_tnl),
1943 .setup = ip6erspan_tap_setup,
1944 .validate = ip6erspan_tap_validate,
1945 .newlink = ip6gre_newlink,
1946 .changelink = ip6gre_changelink,
1947 .get_size = ip6gre_get_size,
1948 .fill_info = ip6gre_fill_info,
1949 .get_link_net = ip6_tnl_get_link_net,
1950};
1951
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001952/*
1953 * And now the modules code and kernel interface.
1954 */
1955
1956static int __init ip6gre_init(void)
1957{
1958 int err;
1959
1960 pr_info("GRE over IPv6 tunneling driver\n");
1961
1962 err = register_pernet_device(&ip6gre_net_ops);
1963 if (err < 0)
1964 return err;
1965
1966 err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE);
1967 if (err < 0) {
1968 pr_info("%s: can't add protocol\n", __func__);
1969 goto add_proto_failed;
1970 }
1971
1972 err = rtnl_link_register(&ip6gre_link_ops);
1973 if (err < 0)
1974 goto rtnl_link_failed;
1975
1976 err = rtnl_link_register(&ip6gre_tap_ops);
1977 if (err < 0)
1978 goto tap_ops_failed;
1979
William Tu5a963eb2017-11-30 11:51:29 -08001980 err = rtnl_link_register(&ip6erspan_tap_ops);
1981 if (err < 0)
1982 goto erspan_link_failed;
1983
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001984out:
1985 return err;
1986
William Tu5a963eb2017-11-30 11:51:29 -08001987erspan_link_failed:
1988 rtnl_link_unregister(&ip6gre_tap_ops);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001989tap_ops_failed:
1990 rtnl_link_unregister(&ip6gre_link_ops);
1991rtnl_link_failed:
1992 inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
1993add_proto_failed:
1994 unregister_pernet_device(&ip6gre_net_ops);
1995 goto out;
1996}
1997
1998static void __exit ip6gre_fini(void)
1999{
2000 rtnl_link_unregister(&ip6gre_tap_ops);
2001 rtnl_link_unregister(&ip6gre_link_ops);
William Tu5a963eb2017-11-30 11:51:29 -08002002 rtnl_link_unregister(&ip6erspan_tap_ops);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002003 inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
2004 unregister_pernet_device(&ip6gre_net_ops);
2005}
2006
2007module_init(ip6gre_init);
2008module_exit(ip6gre_fini);
2009MODULE_LICENSE("GPL");
2010MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
2011MODULE_DESCRIPTION("GRE over IPv6 tunneling device");
2012MODULE_ALIAS_RTNL_LINK("ip6gre");
Nicolas Dichtel5a4ee9a2014-09-24 11:03:00 +02002013MODULE_ALIAS_RTNL_LINK("ip6gretap");
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002014MODULE_ALIAS_NETDEV("ip6gre0");