blob: 5162ecc45c202a88d3781e9617126551c13e60d3 [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);
Petr Machata2d665032018-05-17 16:36:51 +020084static void ip6erspan_tnl_link_config(struct ip6_tnl *t, int set_mtu);
xeb@mail.ruc12b3952012-08-10 00:51:50 +000085
86/* Tunnel hash table */
87
88/*
89 4 hash tables:
90
91 3: (remote,local)
92 2: (remote,*)
93 1: (*,local)
94 0: (*,*)
95
96 We require exact key match i.e. if a key is present in packet
97 it will match only tunnel with the same key; if it is not present,
98 it will match only keyless tunnel.
99
100 All keysless packets, if not matched configured keyless tunnels
101 will match fallback tunnel.
102 */
103
Jiri Kosinae87a8f22016-08-10 11:03:35 +0200104#define HASH_KEY(key) (((__force u32)key^((__force u32)key>>4))&(IP6_GRE_HASH_SIZE - 1))
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000105static u32 HASH_ADDR(const struct in6_addr *addr)
106{
107 u32 hash = ipv6_addr_hash(addr);
108
Jiri Kosinae87a8f22016-08-10 11:03:35 +0200109 return hash_32(hash, IP6_GRE_HASH_SIZE_SHIFT);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000110}
111
112#define tunnels_r_l tunnels[3]
113#define tunnels_r tunnels[2]
114#define tunnels_l tunnels[1]
115#define tunnels_wc tunnels[0]
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000116
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000117/* Given src, dst and key, find appropriate for input tunnel. */
118
119static struct ip6_tnl *ip6gre_tunnel_lookup(struct net_device *dev,
120 const struct in6_addr *remote, const struct in6_addr *local,
121 __be32 key, __be16 gre_proto)
122{
123 struct net *net = dev_net(dev);
124 int link = dev->ifindex;
125 unsigned int h0 = HASH_ADDR(remote);
126 unsigned int h1 = HASH_KEY(key);
127 struct ip6_tnl *t, *cand = NULL;
128 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
William Tu5a963eb2017-11-30 11:51:29 -0800129 int dev_type = (gre_proto == htons(ETH_P_TEB) ||
William Tu3b04caa2018-03-09 07:34:40 -0800130 gre_proto == htons(ETH_P_ERSPAN) ||
131 gre_proto == htons(ETH_P_ERSPAN2)) ?
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000132 ARPHRD_ETHER : ARPHRD_IP6GRE;
133 int score, cand_score = 4;
134
Amerigo Wange086cad2012-11-11 21:52:34 +0000135 for_each_ip_tunnel_rcu(t, ign->tunnels_r_l[h0 ^ h1]) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000136 if (!ipv6_addr_equal(local, &t->parms.laddr) ||
137 !ipv6_addr_equal(remote, &t->parms.raddr) ||
138 key != t->parms.i_key ||
139 !(t->dev->flags & IFF_UP))
140 continue;
141
142 if (t->dev->type != ARPHRD_IP6GRE &&
143 t->dev->type != dev_type)
144 continue;
145
146 score = 0;
147 if (t->parms.link != link)
148 score |= 1;
149 if (t->dev->type != dev_type)
150 score |= 2;
151 if (score == 0)
152 return t;
153
154 if (score < cand_score) {
155 cand = t;
156 cand_score = score;
157 }
158 }
159
Amerigo Wange086cad2012-11-11 21:52:34 +0000160 for_each_ip_tunnel_rcu(t, ign->tunnels_r[h0 ^ h1]) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000161 if (!ipv6_addr_equal(remote, &t->parms.raddr) ||
162 key != t->parms.i_key ||
163 !(t->dev->flags & IFF_UP))
164 continue;
165
166 if (t->dev->type != ARPHRD_IP6GRE &&
167 t->dev->type != dev_type)
168 continue;
169
170 score = 0;
171 if (t->parms.link != link)
172 score |= 1;
173 if (t->dev->type != dev_type)
174 score |= 2;
175 if (score == 0)
176 return t;
177
178 if (score < cand_score) {
179 cand = t;
180 cand_score = score;
181 }
182 }
183
Amerigo Wange086cad2012-11-11 21:52:34 +0000184 for_each_ip_tunnel_rcu(t, ign->tunnels_l[h1]) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000185 if ((!ipv6_addr_equal(local, &t->parms.laddr) &&
186 (!ipv6_addr_equal(local, &t->parms.raddr) ||
187 !ipv6_addr_is_multicast(local))) ||
188 key != t->parms.i_key ||
189 !(t->dev->flags & IFF_UP))
190 continue;
191
192 if (t->dev->type != ARPHRD_IP6GRE &&
193 t->dev->type != dev_type)
194 continue;
195
196 score = 0;
197 if (t->parms.link != link)
198 score |= 1;
199 if (t->dev->type != dev_type)
200 score |= 2;
201 if (score == 0)
202 return t;
203
204 if (score < cand_score) {
205 cand = t;
206 cand_score = score;
207 }
208 }
209
Amerigo Wange086cad2012-11-11 21:52:34 +0000210 for_each_ip_tunnel_rcu(t, ign->tunnels_wc[h1]) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000211 if (t->parms.i_key != key ||
212 !(t->dev->flags & IFF_UP))
213 continue;
214
215 if (t->dev->type != ARPHRD_IP6GRE &&
216 t->dev->type != dev_type)
217 continue;
218
219 score = 0;
220 if (t->parms.link != link)
221 score |= 1;
222 if (t->dev->type != dev_type)
223 score |= 2;
224 if (score == 0)
225 return t;
226
227 if (score < cand_score) {
228 cand = t;
229 cand_score = score;
230 }
231 }
232
Ian Morris53b24b82015-03-29 14:00:05 +0100233 if (cand)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000234 return cand;
235
William Tu6712abc2017-12-01 15:26:08 -0800236 t = rcu_dereference(ign->collect_md_tun);
237 if (t && t->dev->flags & IFF_UP)
238 return t;
239
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000240 dev = ign->fb_tunnel_dev;
Eric Dumazet79134e62018-03-08 12:51:41 -0800241 if (dev && dev->flags & IFF_UP)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000242 return netdev_priv(dev);
243
244 return NULL;
245}
246
247static struct ip6_tnl __rcu **__ip6gre_bucket(struct ip6gre_net *ign,
248 const struct __ip6_tnl_parm *p)
249{
250 const struct in6_addr *remote = &p->raddr;
251 const struct in6_addr *local = &p->laddr;
252 unsigned int h = HASH_KEY(p->i_key);
253 int prio = 0;
254
255 if (!ipv6_addr_any(local))
256 prio |= 1;
257 if (!ipv6_addr_any(remote) && !ipv6_addr_is_multicast(remote)) {
258 prio |= 2;
259 h ^= HASH_ADDR(remote);
260 }
261
262 return &ign->tunnels[prio][h];
263}
264
265static inline struct ip6_tnl __rcu **ip6gre_bucket(struct ip6gre_net *ign,
266 const struct ip6_tnl *t)
267{
268 return __ip6gre_bucket(ign, &t->parms);
269}
270
271static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t)
272{
273 struct ip6_tnl __rcu **tp = ip6gre_bucket(ign, t);
274
William Tu6712abc2017-12-01 15:26:08 -0800275 if (t->parms.collect_md)
276 rcu_assign_pointer(ign->collect_md_tun, t);
277
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000278 rcu_assign_pointer(t->next, rtnl_dereference(*tp));
279 rcu_assign_pointer(*tp, t);
280}
281
282static void ip6gre_tunnel_unlink(struct ip6gre_net *ign, struct ip6_tnl *t)
283{
284 struct ip6_tnl __rcu **tp;
285 struct ip6_tnl *iter;
286
William Tu6712abc2017-12-01 15:26:08 -0800287 if (t->parms.collect_md)
288 rcu_assign_pointer(ign->collect_md_tun, NULL);
289
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000290 for (tp = ip6gre_bucket(ign, t);
291 (iter = rtnl_dereference(*tp)) != NULL;
292 tp = &iter->next) {
293 if (t == iter) {
294 rcu_assign_pointer(*tp, t->next);
295 break;
296 }
297 }
298}
299
300static struct ip6_tnl *ip6gre_tunnel_find(struct net *net,
301 const struct __ip6_tnl_parm *parms,
302 int type)
303{
304 const struct in6_addr *remote = &parms->raddr;
305 const struct in6_addr *local = &parms->laddr;
306 __be32 key = parms->i_key;
307 int link = parms->link;
308 struct ip6_tnl *t;
309 struct ip6_tnl __rcu **tp;
310 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
311
312 for (tp = __ip6gre_bucket(ign, parms);
313 (t = rtnl_dereference(*tp)) != NULL;
314 tp = &t->next)
315 if (ipv6_addr_equal(local, &t->parms.laddr) &&
316 ipv6_addr_equal(remote, &t->parms.raddr) &&
317 key == t->parms.i_key &&
318 link == t->parms.link &&
319 type == t->dev->type)
320 break;
321
322 return t;
323}
324
325static struct ip6_tnl *ip6gre_tunnel_locate(struct net *net,
326 const struct __ip6_tnl_parm *parms, int create)
327{
328 struct ip6_tnl *t, *nt;
329 struct net_device *dev;
330 char name[IFNAMSIZ];
331 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
332
333 t = ip6gre_tunnel_find(net, parms, ARPHRD_IP6GRE);
Steffen Klassertcd0a0bd2014-09-22 10:07:26 +0200334 if (t && create)
335 return NULL;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000336 if (t || !create)
337 return t;
338
Eric Dumazet5f42df02018-04-05 06:39:29 -0700339 if (parms->name[0]) {
340 if (!dev_valid_name(parms->name))
341 return NULL;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000342 strlcpy(name, parms->name, IFNAMSIZ);
Eric Dumazet5f42df02018-04-05 06:39:29 -0700343 } else {
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000344 strcpy(name, "ip6gre%d");
Eric Dumazet5f42df02018-04-05 06:39:29 -0700345 }
Tom Gundersenc835a672014-07-14 16:37:24 +0200346 dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
347 ip6gre_tunnel_setup);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000348 if (!dev)
349 return NULL;
350
351 dev_net_set(dev, net);
352
353 nt = netdev_priv(dev);
354 nt->parms = *parms;
355 dev->rtnl_link_ops = &ip6gre_link_ops;
356
357 nt->dev = dev;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +0200358 nt->net = dev_net(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000359
360 if (register_netdevice(dev) < 0)
361 goto failed_free;
362
Alexey Kodanev128bb972018-01-18 20:51:12 +0300363 ip6gre_tnl_link_config(nt, 1);
364
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000365 /* Can use a lockless transmit, unless we generate output sequences */
Tom Herbertb45bd1d2016-05-09 17:12:12 -0700366 if (!(nt->parms.o_flags & TUNNEL_SEQ))
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000367 dev->features |= NETIF_F_LLTX;
368
369 dev_hold(dev);
370 ip6gre_tunnel_link(ign, nt);
371 return nt;
372
373failed_free:
374 free_netdev(dev);
375 return NULL;
376}
377
378static void ip6gre_tunnel_uninit(struct net_device *dev)
379{
Nicolas Dichtel22f08062014-04-22 10:15:24 +0200380 struct ip6_tnl *t = netdev_priv(dev);
381 struct ip6gre_net *ign = net_generic(t->net, ip6gre_net_id);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000382
Nicolas Dichtel22f08062014-04-22 10:15:24 +0200383 ip6gre_tunnel_unlink(ign, t);
Paolo Abeni607f7252016-02-12 15:43:54 +0100384 dst_cache_reset(&t->dst_cache);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000385 dev_put(dev);
386}
387
388
389static void ip6gre_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Eric Dumazet78920322017-02-04 23:18:55 -0800390 u8 type, u8 code, int offset, __be32 info)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000391{
Xin Long929fc032017-11-11 19:06:49 +0800392 struct net *net = dev_net(skb->dev);
Eric Dumazet78920322017-02-04 23:18:55 -0800393 const struct gre_base_hdr *greh;
394 const struct ipv6hdr *ipv6h;
395 int grehlen = sizeof(*greh);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000396 struct ip6_tnl *t;
Eric Dumazet78920322017-02-04 23:18:55 -0800397 int key_off = 0;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000398 __be16 flags;
Eric Dumazet78920322017-02-04 23:18:55 -0800399 __be32 key;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000400
Eric Dumazet78920322017-02-04 23:18:55 -0800401 if (!pskb_may_pull(skb, offset + grehlen))
402 return;
403 greh = (const struct gre_base_hdr *)(skb->data + offset);
404 flags = greh->flags;
405 if (flags & (GRE_VERSION | GRE_ROUTING))
406 return;
407 if (flags & GRE_CSUM)
408 grehlen += 4;
409 if (flags & GRE_KEY) {
410 key_off = grehlen + offset;
411 grehlen += 4;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000412 }
413
Eric Dumazet78920322017-02-04 23:18:55 -0800414 if (!pskb_may_pull(skb, offset + grehlen))
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000415 return;
Eric Dumazetb87fb392012-08-19 03:47:30 +0000416 ipv6h = (const struct ipv6hdr *)skb->data;
Eric Dumazet78920322017-02-04 23:18:55 -0800417 greh = (const struct gre_base_hdr *)(skb->data + offset);
418 key = key_off ? *(__be32 *)(skb->data + key_off) : 0;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000419
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000420 t = ip6gre_tunnel_lookup(skb->dev, &ipv6h->daddr, &ipv6h->saddr,
Eric Dumazet78920322017-02-04 23:18:55 -0800421 key, greh->protocol);
Ian Morris63159f22015-03-29 14:00:04 +0100422 if (!t)
stephen hemminger0c5794a2012-09-24 18:12:24 +0000423 return;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000424
425 switch (type) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000426 struct ipv6_tlv_tnl_enc_lim *tel;
Xin Longfe1a4ca2017-11-11 19:06:50 +0800427 __u32 teli;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000428 case ICMPV6_DEST_UNREACH:
Matt Bennetta46496c2015-09-23 16:58:31 +1200429 net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n",
430 t->parms.name);
Xin Longf8d20b42017-10-26 19:23:27 +0800431 if (code != ICMPV6_PORT_UNREACH)
432 break;
433 return;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000434 case ICMPV6_TIME_EXCEED:
435 if (code == ICMPV6_EXC_HOPLIMIT) {
Matt Bennetta46496c2015-09-23 16:58:31 +1200436 net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
437 t->parms.name);
Xin Longf8d20b42017-10-26 19:23:27 +0800438 break;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000439 }
Xin Longf8d20b42017-10-26 19:23:27 +0800440 return;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000441 case ICMPV6_PARAMPROB:
442 teli = 0;
443 if (code == ICMPV6_HDR_FIELD)
444 teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
445
Sabrina Dubrocad1e158e2015-02-04 15:25:09 +0100446 if (teli && teli == be32_to_cpu(info) - 2) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000447 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
448 if (tel->encap_limit == 0) {
Matt Bennetta46496c2015-09-23 16:58:31 +1200449 net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
450 t->parms.name);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000451 }
452 } else {
Matt Bennetta46496c2015-09-23 16:58:31 +1200453 net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
454 t->parms.name);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000455 }
Xin Longf8d20b42017-10-26 19:23:27 +0800456 return;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000457 case ICMPV6_PKT_TOOBIG:
Xin Longfe1a4ca2017-11-11 19:06:50 +0800458 ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
Xin Longf8d20b42017-10-26 19:23:27 +0800459 return;
Xin Long929fc032017-11-11 19:06:49 +0800460 case NDISC_REDIRECT:
461 ip6_redirect(skb, net, skb->dev->ifindex, 0,
462 sock_net_uid(net, NULL));
463 return;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000464 }
465
466 if (time_before(jiffies, t->err_time + IP6TUNNEL_ERR_TIMEO))
467 t->err_count++;
468 else
469 t->err_count = 1;
470 t->err_time = jiffies;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000471}
472
Tom Herbert308edfdf2016-04-29 17:12:17 -0700473static int ip6gre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000474{
475 const struct ipv6hdr *ipv6h;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000476 struct ip6_tnl *tunnel;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000477
478 ipv6h = ipv6_hdr(skb);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000479 tunnel = ip6gre_tunnel_lookup(skb->dev,
Tom Herbert308edfdf2016-04-29 17:12:17 -0700480 &ipv6h->saddr, &ipv6h->daddr, tpi->key,
481 tpi->proto);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000482 if (tunnel) {
William Tu6712abc2017-12-01 15:26:08 -0800483 if (tunnel->parms.collect_md) {
484 struct metadata_dst *tun_dst;
485 __be64 tun_id;
486 __be16 flags;
487
488 flags = tpi->flags;
489 tun_id = key32_to_tunnel_id(tpi->key);
490
491 tun_dst = ipv6_tun_rx_dst(skb, flags, tun_id, 0);
492 if (!tun_dst)
493 return PACKET_REJECT;
494
495 ip6_tnl_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
496 } else {
497 ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error);
498 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000499
Tom Herbert308edfdf2016-04-29 17:12:17 -0700500 return PACKET_RCVD;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000501 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000502
Tom Herbert308edfdf2016-04-29 17:12:17 -0700503 return PACKET_REJECT;
504}
505
William Tu5a963eb2017-11-30 11:51:29 -0800506static int ip6erspan_rcv(struct sk_buff *skb, int gre_hdr_len,
507 struct tnl_ptk_info *tpi)
508{
William Tu1d7e2ed2017-12-13 16:38:55 -0800509 struct erspan_base_hdr *ershdr;
510 struct erspan_metadata *pkt_md;
William Tu5a963eb2017-11-30 11:51:29 -0800511 const struct ipv6hdr *ipv6h;
William Tu3df192832018-02-05 13:35:34 -0800512 struct erspan_md2 *md2;
William Tu5a963eb2017-11-30 11:51:29 -0800513 struct ip6_tnl *tunnel;
William Tu1d7e2ed2017-12-13 16:38:55 -0800514 u8 ver;
William Tu5a963eb2017-11-30 11:51:29 -0800515
William Tu5a963eb2017-11-30 11:51:29 -0800516 if (unlikely(!pskb_may_pull(skb, sizeof(*ershdr))))
517 return PACKET_REJECT;
518
Haishuang Yan293a1992017-12-20 09:53:19 +0800519 ipv6h = ipv6_hdr(skb);
520 ershdr = (struct erspan_base_hdr *)skb->data;
William Tuc69de582018-01-25 13:20:09 -0800521 ver = ershdr->ver;
522 tpi->key = cpu_to_be32(get_session_id(ershdr));
William Tu5a963eb2017-11-30 11:51:29 -0800523
524 tunnel = ip6gre_tunnel_lookup(skb->dev,
525 &ipv6h->saddr, &ipv6h->daddr, tpi->key,
526 tpi->proto);
527 if (tunnel) {
William Tu1d7e2ed2017-12-13 16:38:55 -0800528 int len = erspan_hdr_len(ver);
529
530 if (unlikely(!pskb_may_pull(skb, len)))
William Tuae3e1332017-12-15 14:27:43 -0800531 return PACKET_REJECT;
William Tu1d7e2ed2017-12-13 16:38:55 -0800532
William Tud91e8db52017-12-15 14:27:44 -0800533 ershdr = (struct erspan_base_hdr *)skb->data;
534 pkt_md = (struct erspan_metadata *)(ershdr + 1);
535
William Tu1d7e2ed2017-12-13 16:38:55 -0800536 if (__iptunnel_pull_header(skb, len,
William Tu5a963eb2017-11-30 11:51:29 -0800537 htons(ETH_P_TEB),
538 false, false) < 0)
539 return PACKET_REJECT;
540
William Tuef7baf52017-12-05 15:15:44 -0800541 if (tunnel->parms.collect_md) {
542 struct metadata_dst *tun_dst;
543 struct ip_tunnel_info *info;
544 struct erspan_metadata *md;
545 __be64 tun_id;
546 __be16 flags;
547
548 tpi->flags |= TUNNEL_KEY;
549 flags = tpi->flags;
550 tun_id = key32_to_tunnel_id(tpi->key);
551
552 tun_dst = ipv6_tun_rx_dst(skb, flags, tun_id,
553 sizeof(*md));
554 if (!tun_dst)
555 return PACKET_REJECT;
556
557 info = &tun_dst->u.tun_info;
558 md = ip_tunnel_info_opts(info);
William Tu94d7d8f2017-12-13 16:38:57 -0800559 md->version = ver;
William Tu3df192832018-02-05 13:35:34 -0800560 md2 = &md->u.md2;
561 memcpy(md2, pkt_md, ver == 1 ? ERSPAN_V1_MDSIZE :
562 ERSPAN_V2_MDSIZE);
William Tuef7baf52017-12-05 15:15:44 -0800563 info->key.tun_flags |= TUNNEL_ERSPAN_OPT;
564 info->options_len = sizeof(*md);
565
566 ip6_tnl_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
567
568 } else {
William Tuef7baf52017-12-05 15:15:44 -0800569 ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error);
570 }
William Tu5a963eb2017-11-30 11:51:29 -0800571
572 return PACKET_RCVD;
573 }
574
575 return PACKET_REJECT;
576}
577
Tom Herbert308edfdf2016-04-29 17:12:17 -0700578static int gre_rcv(struct sk_buff *skb)
579{
580 struct tnl_ptk_info tpi;
581 bool csum_err = false;
582 int hdr_len;
583
Eric Dumazete5826152016-06-15 06:24:00 -0700584 hdr_len = gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IPV6), 0);
Jiri Bencf132ae72016-05-03 15:00:21 +0200585 if (hdr_len < 0)
Tom Herbert308edfdf2016-04-29 17:12:17 -0700586 goto drop;
587
588 if (iptunnel_pull_header(skb, hdr_len, tpi.proto, false))
589 goto drop;
590
William Tu94d7d8f2017-12-13 16:38:57 -0800591 if (unlikely(tpi.proto == htons(ETH_P_ERSPAN) ||
592 tpi.proto == htons(ETH_P_ERSPAN2))) {
William Tu5a963eb2017-11-30 11:51:29 -0800593 if (ip6erspan_rcv(skb, hdr_len, &tpi) == PACKET_RCVD)
594 return 0;
Haishuang Yana7343212017-12-20 10:21:47 +0800595 goto out;
William Tu5a963eb2017-11-30 11:51:29 -0800596 }
597
Tom Herbert308edfdf2016-04-29 17:12:17 -0700598 if (ip6gre_rcv(skb, &tpi) == PACKET_RCVD)
599 return 0;
600
Haishuang Yana7343212017-12-20 10:21:47 +0800601out:
Tom Herbert308edfdf2016-04-29 17:12:17 -0700602 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000603drop:
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000604 kfree_skb(skb);
605 return 0;
606}
607
Tom Herbertb05229f2016-04-29 17:12:21 -0700608static int gre_handle_offloads(struct sk_buff *skb, bool csum)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000609{
Tom Herbertb05229f2016-04-29 17:12:21 -0700610 return iptunnel_handle_offloads(skb,
611 csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000612}
613
William Tu898b29792017-11-30 11:51:28 -0800614static void prepare_ip6gre_xmit_ipv4(struct sk_buff *skb,
615 struct net_device *dev,
616 struct flowi6 *fl6, __u8 *dsfield,
617 int *encap_limit)
618{
619 const struct iphdr *iph = ip_hdr(skb);
620 struct ip6_tnl *t = netdev_priv(dev);
621
622 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
623 *encap_limit = t->parms.encap_limit;
624
625 memcpy(fl6, &t->fl.u.ip6, sizeof(*fl6));
626
627 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
628 *dsfield = ipv4_get_dsfield(iph);
629 else
630 *dsfield = ip6_tclass(t->parms.flowinfo);
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
640static int prepare_ip6gre_xmit_ipv6(struct sk_buff *skb,
641 struct net_device *dev,
642 struct flowi6 *fl6, __u8 *dsfield,
643 int *encap_limit)
644{
645 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
646 struct ip6_tnl *t = netdev_priv(dev);
647 __u16 offset;
648
649 offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
650 /* ip6_tnl_parse_tlv_enc_lim() might have reallocated skb->head */
651
652 if (offset > 0) {
653 struct ipv6_tlv_tnl_enc_lim *tel;
654
655 tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
656 if (tel->encap_limit == 0) {
657 icmpv6_send(skb, ICMPV6_PARAMPROB,
658 ICMPV6_HDR_FIELD, offset + 2);
659 return -1;
660 }
661 *encap_limit = tel->encap_limit - 1;
662 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) {
663 *encap_limit = t->parms.encap_limit;
664 }
665
666 memcpy(fl6, &t->fl.u.ip6, sizeof(*fl6));
667
668 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
669 *dsfield = ipv6_get_dsfield(ipv6h);
670 else
671 *dsfield = ip6_tclass(t->parms.flowinfo);
672
673 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
674 fl6->flowlabel |= ip6_flowlabel(ipv6h);
675
676 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
677 fl6->flowi6_mark = skb->mark;
678 else
679 fl6->flowi6_mark = t->parms.fwmark;
680
681 fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
682
683 return 0;
684}
685
Tom Herbertb05229f2016-04-29 17:12:21 -0700686static netdev_tx_t __gre6_xmit(struct sk_buff *skb,
687 struct net_device *dev, __u8 dsfield,
688 struct flowi6 *fl6, int encap_limit,
689 __u32 *pmtu, __be16 proto)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000690{
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000691 struct ip6_tnl *tunnel = netdev_priv(dev);
Xin Long8aec4952017-10-26 19:27:17 +0800692 __be16 protocol;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000693
694 if (dev->type == ARPHRD_ETHER)
695 IPCB(skb)->flags = 0;
696
Tom Herbertb05229f2016-04-29 17:12:21 -0700697 if (dev->header_ops && dev->type == ARPHRD_IP6GRE)
698 fl6->daddr = ((struct ipv6hdr *)skb->data)->daddr;
699 else
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000700 fl6->daddr = tunnel->parms.raddr;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000701
Petr Machata01b8d062018-05-17 16:36:10 +0200702 if (skb_cow_head(skb, dev->needed_headroom ?: tunnel->hlen))
703 return -ENOMEM;
704
Tom Herbertb05229f2016-04-29 17:12:21 -0700705 /* Push GRE header. */
Xin Long8aec4952017-10-26 19:27:17 +0800706 protocol = (dev->type == ARPHRD_ETHER) ? htons(ETH_P_TEB) : proto;
William Tu6712abc2017-12-01 15:26:08 -0800707
708 if (tunnel->parms.collect_md) {
709 struct ip_tunnel_info *tun_info;
710 const struct ip_tunnel_key *key;
711 __be16 flags;
712
713 tun_info = skb_tunnel_info(skb);
714 if (unlikely(!tun_info ||
715 !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
716 ip_tunnel_info_af(tun_info) != AF_INET6))
717 return -EINVAL;
718
719 key = &tun_info->key;
720 memset(fl6, 0, sizeof(*fl6));
721 fl6->flowi6_proto = IPPROTO_GRE;
722 fl6->daddr = key->u.ipv6.dst;
723 fl6->flowlabel = key->label;
724 fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
725
726 dsfield = key->tos;
William Tu77a51962018-03-01 13:49:57 -0800727 flags = key->tun_flags &
728 (TUNNEL_CSUM | TUNNEL_KEY | TUNNEL_SEQ);
William Tu6712abc2017-12-01 15:26:08 -0800729 tunnel->tun_hlen = gre_calc_hlen(flags);
730
731 gre_build_header(skb, tunnel->tun_hlen,
732 flags, protocol,
William Tu77a51962018-03-01 13:49:57 -0800733 tunnel_id_to_key32(tun_info->key.tun_id),
Colin Ian King15746392018-03-21 19:34:58 +0000734 (flags & TUNNEL_SEQ) ? htonl(tunnel->o_seqno++)
William Tu77a51962018-03-01 13:49:57 -0800735 : 0);
William Tu6712abc2017-12-01 15:26:08 -0800736
737 } else {
William Tu77a51962018-03-01 13:49:57 -0800738 if (tunnel->parms.o_flags & TUNNEL_SEQ)
739 tunnel->o_seqno++;
740
William Tu6712abc2017-12-01 15:26:08 -0800741 gre_build_header(skb, tunnel->tun_hlen, tunnel->parms.o_flags,
742 protocol, tunnel->parms.o_key,
743 htonl(tunnel->o_seqno));
744 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000745
Tom Herbertb05229f2016-04-29 17:12:21 -0700746 return ip6_tnl_xmit(skb, dev, dsfield, fl6, encap_limit, pmtu,
747 NEXTHDR_GRE);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000748}
749
750static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev)
751{
752 struct ip6_tnl *t = netdev_priv(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000753 int encap_limit = -1;
754 struct flowi6 fl6;
William Tu6712abc2017-12-01 15:26:08 -0800755 __u8 dsfield = 0;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000756 __u32 mtu;
757 int err;
758
Bernie Harris5146d1f2016-02-22 12:58:05 +1300759 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
760
William Tu6712abc2017-12-01 15:26:08 -0800761 if (!t->parms.collect_md)
762 prepare_ip6gre_xmit_ipv4(skb, dev, &fl6,
763 &dsfield, &encap_limit);
Lorenzo Colittie2d118a2016-11-04 02:23:43 +0900764
Tom Herbertb05229f2016-04-29 17:12:21 -0700765 err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
766 if (err)
767 return -1;
768
769 err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
770 skb->protocol);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000771 if (err != 0) {
772 /* XXX: send ICMP error even if DF is not set. */
773 if (err == -EMSGSIZE)
774 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
775 htonl(mtu));
776 return -1;
777 }
778
779 return 0;
780}
781
782static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev)
783{
784 struct ip6_tnl *t = netdev_priv(dev);
785 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
786 int encap_limit = -1;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000787 struct flowi6 fl6;
William Tu6712abc2017-12-01 15:26:08 -0800788 __u8 dsfield = 0;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000789 __u32 mtu;
790 int err;
791
792 if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
793 return -1;
794
William Tu6712abc2017-12-01 15:26:08 -0800795 if (!t->parms.collect_md &&
796 prepare_ip6gre_xmit_ipv6(skb, dev, &fl6, &dsfield, &encap_limit))
William Tu898b29792017-11-30 11:51:28 -0800797 return -1;
Lorenzo Colittie2d118a2016-11-04 02:23:43 +0900798
Tom Herbertb05229f2016-04-29 17:12:21 -0700799 if (gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM)))
800 return -1;
801
802 err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit,
803 &mtu, skb->protocol);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000804 if (err != 0) {
805 if (err == -EMSGSIZE)
806 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
807 return -1;
808 }
809
810 return 0;
811}
812
813/**
814 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
815 * @t: the outgoing tunnel device
816 * @hdr: IPv6 header from the incoming packet
817 *
818 * Description:
819 * Avoid trivial tunneling loop by checking that tunnel exit-point
820 * doesn't match source of incoming packet.
821 *
822 * Return:
823 * 1 if conflict,
824 * 0 else
825 **/
826
827static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl *t,
828 const struct ipv6hdr *hdr)
829{
830 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
831}
832
833static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev)
834{
835 struct ip6_tnl *t = netdev_priv(dev);
836 int encap_limit = -1;
837 struct flowi6 fl6;
838 __u32 mtu;
839 int err;
840
841 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
842 encap_limit = t->parms.encap_limit;
843
William Tu6712abc2017-12-01 15:26:08 -0800844 if (!t->parms.collect_md)
845 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000846
Tom Herbertb05229f2016-04-29 17:12:21 -0700847 err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
848 if (err)
849 return err;
850
851 err = __gre6_xmit(skb, dev, 0, &fl6, encap_limit, &mtu, skb->protocol);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000852
853 return err;
854}
855
856static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb,
857 struct net_device *dev)
858{
859 struct ip6_tnl *t = netdev_priv(dev);
860 struct net_device_stats *stats = &t->dev->stats;
861 int ret;
862
Steffen Klassertd5005142014-11-05 08:02:48 +0100863 if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
Tommi Rantala41ab3e32013-02-06 03:24:02 +0000864 goto tx_err;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000865
866 switch (skb->protocol) {
867 case htons(ETH_P_IP):
868 ret = ip6gre_xmit_ipv4(skb, dev);
869 break;
870 case htons(ETH_P_IPV6):
871 ret = ip6gre_xmit_ipv6(skb, dev);
872 break;
873 default:
874 ret = ip6gre_xmit_other(skb, dev);
875 break;
876 }
877
878 if (ret < 0)
879 goto tx_err;
880
881 return NETDEV_TX_OK;
882
883tx_err:
884 stats->tx_errors++;
885 stats->tx_dropped++;
886 kfree_skb(skb);
887 return NETDEV_TX_OK;
888}
889
William Tu5a963eb2017-11-30 11:51:29 -0800890static netdev_tx_t ip6erspan_tunnel_xmit(struct sk_buff *skb,
891 struct net_device *dev)
892{
893 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
894 struct ip6_tnl *t = netdev_priv(dev);
895 struct dst_entry *dst = skb_dst(skb);
896 struct net_device_stats *stats;
897 bool truncate = false;
898 int encap_limit = -1;
899 __u8 dsfield = false;
900 struct flowi6 fl6;
901 int err = -EINVAL;
902 __u32 mtu;
903
904 if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
905 goto tx_err;
906
907 if (gre_handle_offloads(skb, false))
908 goto tx_err;
909
William Tu5a963eb2017-11-30 11:51:29 -0800910 if (skb->len > dev->mtu + dev->hard_header_len) {
911 pskb_trim(skb, dev->mtu + dev->hard_header_len);
912 truncate = true;
913 }
914
Petr Machata56914842018-05-17 16:36:15 +0200915 if (skb_cow_head(skb, dev->needed_headroom ?: t->hlen))
William Tue41c7c62018-03-09 07:34:42 -0800916 goto tx_err;
917
William Tu5a963eb2017-11-30 11:51:29 -0800918 t->parms.o_flags &= ~TUNNEL_KEY;
William Tu5a963eb2017-11-30 11:51:29 -0800919 IPCB(skb)->flags = 0;
William Tuef7baf52017-12-05 15:15:44 -0800920
921 /* For collect_md mode, derive fl6 from the tunnel key,
922 * for native mode, call prepare_ip6gre_xmit_{ipv4,ipv6}.
923 */
924 if (t->parms.collect_md) {
925 struct ip_tunnel_info *tun_info;
926 const struct ip_tunnel_key *key;
927 struct erspan_metadata *md;
William Tuc69de582018-01-25 13:20:09 -0800928 __be32 tun_id;
William Tuef7baf52017-12-05 15:15:44 -0800929
930 tun_info = skb_tunnel_info(skb);
931 if (unlikely(!tun_info ||
932 !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
933 ip_tunnel_info_af(tun_info) != AF_INET6))
934 return -EINVAL;
935
936 key = &tun_info->key;
937 memset(&fl6, 0, sizeof(fl6));
938 fl6.flowi6_proto = IPPROTO_GRE;
939 fl6.daddr = key->u.ipv6.dst;
940 fl6.flowlabel = key->label;
941 fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
942
943 dsfield = key->tos;
944 md = ip_tunnel_info_opts(tun_info);
945 if (!md)
946 goto tx_err;
947
William Tuc69de582018-01-25 13:20:09 -0800948 tun_id = tunnel_id_to_key32(key->tun_id);
William Tu94d7d8f2017-12-13 16:38:57 -0800949 if (md->version == 1) {
950 erspan_build_header(skb,
William Tuc69de582018-01-25 13:20:09 -0800951 ntohl(tun_id),
William Tu94d7d8f2017-12-13 16:38:57 -0800952 ntohl(md->u.index), truncate,
953 false);
954 } else if (md->version == 2) {
William Tu94d7d8f2017-12-13 16:38:57 -0800955 erspan_build_header_v2(skb,
William Tuc69de582018-01-25 13:20:09 -0800956 ntohl(tun_id),
957 md->u.md2.dir,
958 get_hwid(&md->u.md2),
959 truncate, false);
William Tud6aa7112018-03-09 07:34:41 -0800960 } else {
961 goto tx_err;
William Tu94d7d8f2017-12-13 16:38:57 -0800962 }
William Tuef7baf52017-12-05 15:15:44 -0800963 } else {
964 switch (skb->protocol) {
965 case htons(ETH_P_IP):
966 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
967 prepare_ip6gre_xmit_ipv4(skb, dev, &fl6,
968 &dsfield, &encap_limit);
969 break;
970 case htons(ETH_P_IPV6):
971 if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
972 goto tx_err;
973 if (prepare_ip6gre_xmit_ipv6(skb, dev, &fl6,
974 &dsfield, &encap_limit))
975 goto tx_err;
976 break;
977 default:
978 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
979 break;
980 }
981
William Tu94d7d8f2017-12-13 16:38:57 -0800982 if (t->parms.erspan_ver == 1)
William Tuc69de582018-01-25 13:20:09 -0800983 erspan_build_header(skb, ntohl(t->parms.o_key),
William Tu94d7d8f2017-12-13 16:38:57 -0800984 t->parms.index,
985 truncate, false);
William Tu02f99df2018-05-16 17:24:32 -0700986 else if (t->parms.erspan_ver == 2)
William Tuc69de582018-01-25 13:20:09 -0800987 erspan_build_header_v2(skb, ntohl(t->parms.o_key),
William Tu94d7d8f2017-12-13 16:38:57 -0800988 t->parms.dir,
989 t->parms.hwid,
990 truncate, false);
William Tu02f99df2018-05-16 17:24:32 -0700991 else
992 goto tx_err;
993
William Tuef7baf52017-12-05 15:15:44 -0800994 fl6.daddr = t->parms.raddr;
995 }
William Tu5a963eb2017-11-30 11:51:29 -0800996
997 /* Push GRE header. */
998 gre_build_header(skb, 8, TUNNEL_SEQ,
999 htons(ETH_P_ERSPAN), 0, htonl(t->o_seqno++));
1000
1001 /* TooBig packet may have updated dst->dev's mtu */
William Tuef7baf52017-12-05 15:15:44 -08001002 if (!t->parms.collect_md && dst && dst_mtu(dst) > dst->dev->mtu)
William Tu5a963eb2017-11-30 11:51:29 -08001003 dst->ops->update_pmtu(dst, NULL, skb, dst->dev->mtu);
1004
1005 err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
1006 NEXTHDR_GRE);
1007 if (err != 0) {
1008 /* XXX: send ICMP error even if DF is not set. */
1009 if (err == -EMSGSIZE) {
1010 if (skb->protocol == htons(ETH_P_IP))
1011 icmp_send(skb, ICMP_DEST_UNREACH,
1012 ICMP_FRAG_NEEDED, htonl(mtu));
1013 else
1014 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1015 }
1016
1017 goto tx_err;
1018 }
1019 return NETDEV_TX_OK;
1020
1021tx_err:
1022 stats = &t->dev->stats;
1023 stats->tx_errors++;
1024 stats->tx_dropped++;
1025 kfree_skb(skb);
1026 return NETDEV_TX_OK;
1027}
1028
Petr Machataa4833732018-05-17 16:36:27 +02001029static void ip6gre_tnl_link_config_common(struct ip6_tnl *t)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001030{
1031 struct net_device *dev = t->dev;
1032 struct __ip6_tnl_parm *p = &t->parms;
1033 struct flowi6 *fl6 = &t->fl.u.ip6;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001034
1035 if (dev->type != ARPHRD_ETHER) {
1036 memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
1037 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
1038 }
1039
1040 /* Set up flowi template */
1041 fl6->saddr = p->laddr;
1042 fl6->daddr = p->raddr;
1043 fl6->flowi6_oif = p->link;
1044 fl6->flowlabel = 0;
Haishuang Yan252f3f52016-05-21 18:17:35 +08001045 fl6->flowi6_proto = IPPROTO_GRE;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001046
1047 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
1048 fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
1049 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
1050 fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
1051
1052 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
1053 p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
1054
1055 if (p->flags&IP6_TNL_F_CAP_XMIT &&
1056 p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER)
1057 dev->flags |= IFF_POINTOPOINT;
1058 else
1059 dev->flags &= ~IFF_POINTOPOINT;
Petr Machataa4833732018-05-17 16:36:27 +02001060}
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001061
Petr Machataa4833732018-05-17 16:36:27 +02001062static void ip6gre_tnl_link_config_route(struct ip6_tnl *t, int set_mtu,
1063 int t_hlen)
1064{
1065 const struct __ip6_tnl_parm *p = &t->parms;
1066 struct net_device *dev = t->dev;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001067
1068 if (p->flags & IP6_TNL_F_CAP_XMIT) {
1069 int strict = (ipv6_addr_type(&p->raddr) &
1070 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
1071
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001072 struct rt6_info *rt = rt6_lookup(t->net,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001073 &p->raddr, &p->laddr,
David Ahernb75cc8f2018-03-02 08:32:17 -08001074 p->link, NULL, strict);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001075
Ian Morris63159f22015-03-29 14:00:04 +01001076 if (!rt)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001077 return;
1078
1079 if (rt->dst.dev) {
Tom Herbertdb2ec952016-05-09 17:12:08 -07001080 dev->hard_header_len = rt->dst.dev->hard_header_len +
1081 t_hlen;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001082
1083 if (set_mtu) {
Tom Herbertdb2ec952016-05-09 17:12:08 -07001084 dev->mtu = rt->dst.dev->mtu - t_hlen;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001085 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1086 dev->mtu -= 8;
Alexander Duycka9e242c2016-04-14 15:33:45 -04001087 if (dev->type == ARPHRD_ETHER)
1088 dev->mtu -= ETH_HLEN;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001089
1090 if (dev->mtu < IPV6_MIN_MTU)
1091 dev->mtu = IPV6_MIN_MTU;
1092 }
1093 }
Amerigo Wang94e187c2012-10-29 00:13:19 +00001094 ip6_rt_put(rt);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001095 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001096}
1097
Petr Machataa4833732018-05-17 16:36:27 +02001098static int ip6gre_calc_hlen(struct ip6_tnl *tunnel)
1099{
1100 int t_hlen;
1101
1102 tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
1103 tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen;
1104
1105 t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
1106 tunnel->dev->hard_header_len = LL_MAX_HEADER + t_hlen;
1107 return t_hlen;
1108}
1109
1110static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu)
1111{
1112 ip6gre_tnl_link_config_common(t);
1113 ip6gre_tnl_link_config_route(t, set_mtu, ip6gre_calc_hlen(t));
1114}
1115
Petr Machataa6465352018-05-17 16:36:33 +02001116static void ip6gre_tnl_copy_tnl_parm(struct ip6_tnl *t,
1117 const struct __ip6_tnl_parm *p)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001118{
1119 t->parms.laddr = p->laddr;
1120 t->parms.raddr = p->raddr;
1121 t->parms.flags = p->flags;
1122 t->parms.hop_limit = p->hop_limit;
1123 t->parms.encap_limit = p->encap_limit;
1124 t->parms.flowinfo = p->flowinfo;
1125 t->parms.link = p->link;
1126 t->parms.proto = p->proto;
1127 t->parms.i_key = p->i_key;
1128 t->parms.o_key = p->o_key;
1129 t->parms.i_flags = p->i_flags;
1130 t->parms.o_flags = p->o_flags;
Craig Gallek0a473b82017-04-19 12:30:53 -04001131 t->parms.fwmark = p->fwmark;
Paolo Abeni607f7252016-02-12 15:43:54 +01001132 dst_cache_reset(&t->dst_cache);
Petr Machataa6465352018-05-17 16:36:33 +02001133}
1134
1135static int ip6gre_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p,
1136 int set_mtu)
1137{
1138 ip6gre_tnl_copy_tnl_parm(t, p);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001139 ip6gre_tnl_link_config(t, set_mtu);
1140 return 0;
1141}
1142
1143static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p,
1144 const struct ip6_tnl_parm2 *u)
1145{
1146 p->laddr = u->laddr;
1147 p->raddr = u->raddr;
1148 p->flags = u->flags;
1149 p->hop_limit = u->hop_limit;
1150 p->encap_limit = u->encap_limit;
1151 p->flowinfo = u->flowinfo;
1152 p->link = u->link;
1153 p->i_key = u->i_key;
1154 p->o_key = u->o_key;
Tom Herbertf41fe3c2016-05-09 17:12:09 -07001155 p->i_flags = gre_flags_to_tnl_flags(u->i_flags);
1156 p->o_flags = gre_flags_to_tnl_flags(u->o_flags);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001157 memcpy(p->name, u->name, sizeof(u->name));
1158}
1159
1160static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u,
1161 const struct __ip6_tnl_parm *p)
1162{
1163 u->proto = IPPROTO_GRE;
1164 u->laddr = p->laddr;
1165 u->raddr = p->raddr;
1166 u->flags = p->flags;
1167 u->hop_limit = p->hop_limit;
1168 u->encap_limit = p->encap_limit;
1169 u->flowinfo = p->flowinfo;
1170 u->link = p->link;
1171 u->i_key = p->i_key;
1172 u->o_key = p->o_key;
Tom Herbertf41fe3c2016-05-09 17:12:09 -07001173 u->i_flags = gre_tnl_flags_to_gre_flags(p->i_flags);
1174 u->o_flags = gre_tnl_flags_to_gre_flags(p->o_flags);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001175 memcpy(u->name, p->name, sizeof(u->name));
1176}
1177
1178static int ip6gre_tunnel_ioctl(struct net_device *dev,
1179 struct ifreq *ifr, int cmd)
1180{
1181 int err = 0;
1182 struct ip6_tnl_parm2 p;
1183 struct __ip6_tnl_parm p1;
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001184 struct ip6_tnl *t = netdev_priv(dev);
1185 struct net *net = t->net;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001186 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1187
Tom Herbert308edfdf2016-04-29 17:12:17 -07001188 memset(&p1, 0, sizeof(p1));
1189
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001190 switch (cmd) {
1191 case SIOCGETTUNNEL:
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001192 if (dev == ign->fb_tunnel_dev) {
1193 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
1194 err = -EFAULT;
1195 break;
1196 }
1197 ip6gre_tnl_parm_from_user(&p1, &p);
1198 t = ip6gre_tunnel_locate(net, &p1, 0);
Ian Morris63159f22015-03-29 14:00:04 +01001199 if (!t)
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001200 t = netdev_priv(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001201 }
Amerigo Wang5dbd5062013-05-09 21:56:37 +00001202 memset(&p, 0, sizeof(p));
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001203 ip6gre_tnl_parm_to_user(&p, &t->parms);
1204 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
1205 err = -EFAULT;
1206 break;
1207
1208 case SIOCADDTUNNEL:
1209 case SIOCCHGTUNNEL:
1210 err = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00001211 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001212 goto done;
1213
1214 err = -EFAULT;
1215 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1216 goto done;
1217
1218 err = -EINVAL;
1219 if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))
1220 goto done;
1221
1222 if (!(p.i_flags&GRE_KEY))
1223 p.i_key = 0;
1224 if (!(p.o_flags&GRE_KEY))
1225 p.o_key = 0;
1226
1227 ip6gre_tnl_parm_from_user(&p1, &p);
1228 t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL);
1229
1230 if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
Ian Morris53b24b82015-03-29 14:00:05 +01001231 if (t) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001232 if (t->dev != dev) {
1233 err = -EEXIST;
1234 break;
1235 }
1236 } else {
1237 t = netdev_priv(dev);
1238
1239 ip6gre_tunnel_unlink(ign, t);
1240 synchronize_net();
1241 ip6gre_tnl_change(t, &p1, 1);
1242 ip6gre_tunnel_link(ign, t);
1243 netdev_state_change(dev);
1244 }
1245 }
1246
1247 if (t) {
1248 err = 0;
1249
Amerigo Wang5dbd5062013-05-09 21:56:37 +00001250 memset(&p, 0, sizeof(p));
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001251 ip6gre_tnl_parm_to_user(&p, &t->parms);
1252 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
1253 err = -EFAULT;
1254 } else
1255 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
1256 break;
1257
1258 case SIOCDELTUNNEL:
1259 err = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00001260 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001261 goto done;
1262
1263 if (dev == ign->fb_tunnel_dev) {
1264 err = -EFAULT;
1265 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1266 goto done;
1267 err = -ENOENT;
1268 ip6gre_tnl_parm_from_user(&p1, &p);
1269 t = ip6gre_tunnel_locate(net, &p1, 0);
Ian Morris63159f22015-03-29 14:00:04 +01001270 if (!t)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001271 goto done;
1272 err = -EPERM;
1273 if (t == netdev_priv(ign->fb_tunnel_dev))
1274 goto done;
1275 dev = t->dev;
1276 }
1277 unregister_netdevice(dev);
1278 err = 0;
1279 break;
1280
1281 default:
1282 err = -EINVAL;
1283 }
1284
1285done:
1286 return err;
1287}
1288
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001289static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
Xin Long76cc0d32017-09-15 12:00:07 +08001290 unsigned short type, const void *daddr,
1291 const void *saddr, unsigned int len)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001292{
1293 struct ip6_tnl *t = netdev_priv(dev);
Xin Long76cc0d32017-09-15 12:00:07 +08001294 struct ipv6hdr *ipv6h;
1295 __be16 *p;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001296
Xin Long76cc0d32017-09-15 12:00:07 +08001297 ipv6h = skb_push(skb, t->hlen + sizeof(*ipv6h));
1298 ip6_flow_hdr(ipv6h, 0, ip6_make_flowlabel(dev_net(dev), skb,
1299 t->fl.u.ip6.flowlabel,
1300 true, &t->fl.u.ip6));
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001301 ipv6h->hop_limit = t->parms.hop_limit;
1302 ipv6h->nexthdr = NEXTHDR_GRE;
1303 ipv6h->saddr = t->parms.laddr;
1304 ipv6h->daddr = t->parms.raddr;
1305
Xin Long76cc0d32017-09-15 12:00:07 +08001306 p = (__be16 *)(ipv6h + 1);
1307 p[0] = t->parms.o_flags;
1308 p[1] = htons(type);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001309
1310 /*
1311 * Set the source hardware address.
1312 */
1313
1314 if (saddr)
1315 memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr));
1316 if (daddr)
1317 memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
1318 if (!ipv6_addr_any(&ipv6h->daddr))
1319 return t->hlen;
1320
1321 return -t->hlen;
1322}
1323
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001324static const struct header_ops ip6gre_header_ops = {
1325 .create = ip6gre_header,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001326};
1327
1328static const struct net_device_ops ip6gre_netdev_ops = {
1329 .ndo_init = ip6gre_tunnel_init,
1330 .ndo_uninit = ip6gre_tunnel_uninit,
1331 .ndo_start_xmit = ip6gre_tunnel_xmit,
1332 .ndo_do_ioctl = ip6gre_tunnel_ioctl,
Tom Herbertb05229f2016-04-29 17:12:21 -07001333 .ndo_change_mtu = ip6_tnl_change_mtu,
Pravin B Shelarf61dd382013-03-25 14:50:00 +00001334 .ndo_get_stats64 = ip_tunnel_get_stats64,
Nicolas Dichtelecf2c062015-04-02 17:07:01 +02001335 .ndo_get_iflink = ip6_tnl_get_iflink,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001336};
1337
1338static void ip6gre_dev_free(struct net_device *dev)
1339{
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001340 struct ip6_tnl *t = netdev_priv(dev);
1341
Paolo Abeni607f7252016-02-12 15:43:54 +01001342 dst_cache_destroy(&t->dst_cache);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001343 free_percpu(dev->tstats);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001344}
1345
1346static void ip6gre_tunnel_setup(struct net_device *dev)
1347{
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001348 dev->netdev_ops = &ip6gre_netdev_ops;
David S. Millercf124db2017-05-08 12:52:56 -04001349 dev->needs_free_netdev = true;
1350 dev->priv_destructor = ip6gre_dev_free;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001351
1352 dev->type = ARPHRD_IP6GRE;
Tom Herbertb05229f2016-04-29 17:12:21 -07001353
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001354 dev->flags |= IFF_NOARP;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001355 dev->addr_len = sizeof(struct in6_addr);
Eric Dumazet02875872014-10-05 18:38:35 -07001356 netif_keep_dst(dev);
Felix Jia45ce0fd2017-01-26 16:59:18 +13001357 /* This perm addr will be used as interface identifier by IPv6 */
1358 dev->addr_assign_type = NET_ADDR_RANDOM;
1359 eth_random_addr(dev->perm_addr);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001360}
1361
Alexey Kodaneve5a93362017-12-20 19:36:03 +03001362#define GRE6_FEATURES (NETIF_F_SG | \
1363 NETIF_F_FRAGLIST | \
1364 NETIF_F_HIGHDMA | \
1365 NETIF_F_HW_CSUM)
1366
1367static void ip6gre_tnl_init_features(struct net_device *dev)
1368{
1369 struct ip6_tnl *nt = netdev_priv(dev);
1370
1371 dev->features |= GRE6_FEATURES;
1372 dev->hw_features |= GRE6_FEATURES;
1373
1374 if (!(nt->parms.o_flags & TUNNEL_SEQ)) {
1375 /* TCP offload with GRE SEQ is not supported, nor
1376 * can we support 2 levels of outer headers requiring
1377 * an update.
1378 */
1379 if (!(nt->parms.o_flags & TUNNEL_CSUM) ||
1380 nt->encap.type == TUNNEL_ENCAP_NONE) {
1381 dev->features |= NETIF_F_GSO_SOFTWARE;
1382 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
1383 }
1384
1385 /* Can use a lockless transmit, unless we generate
1386 * output sequences
1387 */
1388 dev->features |= NETIF_F_LLTX;
1389 }
1390}
1391
Martin KaFai Laua3c119d2015-09-15 14:30:05 -07001392static int ip6gre_tunnel_init_common(struct net_device *dev)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001393{
1394 struct ip6_tnl *tunnel;
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001395 int ret;
Tom Herbertb05229f2016-04-29 17:12:21 -07001396 int t_hlen;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001397
1398 tunnel = netdev_priv(dev);
1399
1400 tunnel->dev = dev;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001401 tunnel->net = dev_net(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001402 strcpy(tunnel->parms.name, dev->name);
1403
Martin KaFai Laua3c119d2015-09-15 14:30:05 -07001404 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1405 if (!dev->tstats)
1406 return -ENOMEM;
1407
Paolo Abeni607f7252016-02-12 15:43:54 +01001408 ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001409 if (ret) {
1410 free_percpu(dev->tstats);
1411 dev->tstats = NULL;
1412 return ret;
1413 }
1414
Petr Machataa4833732018-05-17 16:36:27 +02001415 t_hlen = ip6gre_calc_hlen(tunnel);
Tom Herbertdb2ec952016-05-09 17:12:08 -07001416 dev->mtu = ETH_DATA_LEN - t_hlen;
Haishuang Yan1b227e52016-05-21 18:17:34 +08001417 if (dev->type == ARPHRD_ETHER)
1418 dev->mtu -= ETH_HLEN;
Tom Herbertb05229f2016-04-29 17:12:21 -07001419 if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1420 dev->mtu -= 8;
1421
William Tu6712abc2017-12-01 15:26:08 -08001422 if (tunnel->parms.collect_md) {
1423 dev->features |= NETIF_F_NETNS_LOCAL;
1424 netif_keep_dst(dev);
1425 }
Alexey Kodaneve5a93362017-12-20 19:36:03 +03001426 ip6gre_tnl_init_features(dev);
William Tu6712abc2017-12-01 15:26:08 -08001427
Martin KaFai Laua3c119d2015-09-15 14:30:05 -07001428 return 0;
1429}
1430
1431static int ip6gre_tunnel_init(struct net_device *dev)
1432{
1433 struct ip6_tnl *tunnel;
1434 int ret;
1435
1436 ret = ip6gre_tunnel_init_common(dev);
1437 if (ret)
1438 return ret;
1439
1440 tunnel = netdev_priv(dev);
1441
William Tu6712abc2017-12-01 15:26:08 -08001442 if (tunnel->parms.collect_md)
1443 return 0;
1444
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001445 memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr));
1446 memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr));
1447
1448 if (ipv6_addr_any(&tunnel->parms.raddr))
1449 dev->header_ops = &ip6gre_header_ops;
1450
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001451 return 0;
1452}
1453
1454static void ip6gre_fb_tunnel_init(struct net_device *dev)
1455{
1456 struct ip6_tnl *tunnel = netdev_priv(dev);
1457
1458 tunnel->dev = dev;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001459 tunnel->net = dev_net(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001460 strcpy(tunnel->parms.name, dev->name);
1461
1462 tunnel->hlen = sizeof(struct ipv6hdr) + 4;
1463
1464 dev_hold(dev);
1465}
1466
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001467static struct inet6_protocol ip6gre_protocol __read_mostly = {
Tom Herbert308edfdf2016-04-29 17:12:17 -07001468 .handler = gre_rcv,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001469 .err_handler = ip6gre_err,
1470 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1471};
1472
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001473static void ip6gre_destroy_tunnels(struct net *net, struct list_head *head)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001474{
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001475 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1476 struct net_device *dev, *aux;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001477 int prio;
1478
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001479 for_each_netdev_safe(net, dev, aux)
1480 if (dev->rtnl_link_ops == &ip6gre_link_ops ||
William Tu5a963eb2017-11-30 11:51:29 -08001481 dev->rtnl_link_ops == &ip6gre_tap_ops ||
1482 dev->rtnl_link_ops == &ip6erspan_tap_ops)
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001483 unregister_netdevice_queue(dev, head);
1484
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001485 for (prio = 0; prio < 4; prio++) {
1486 int h;
Jiri Kosinae87a8f22016-08-10 11:03:35 +02001487 for (h = 0; h < IP6_GRE_HASH_SIZE; h++) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001488 struct ip6_tnl *t;
1489
1490 t = rtnl_dereference(ign->tunnels[prio][h]);
1491
Ian Morris53b24b82015-03-29 14:00:05 +01001492 while (t) {
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001493 /* If dev is in the same netns, it has already
1494 * been added to the list by the previous loop.
1495 */
1496 if (!net_eq(dev_net(t->dev), net))
1497 unregister_netdevice_queue(t->dev,
1498 head);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001499 t = rtnl_dereference(t->next);
1500 }
1501 }
1502 }
1503}
1504
1505static int __net_init ip6gre_init_net(struct net *net)
1506{
1507 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1508 int err;
1509
Eric Dumazet79134e62018-03-08 12:51:41 -08001510 if (!net_has_fallback_tunnels(net))
1511 return 0;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001512 ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0",
Tom Gundersenc835a672014-07-14 16:37:24 +02001513 NET_NAME_UNKNOWN,
1514 ip6gre_tunnel_setup);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001515 if (!ign->fb_tunnel_dev) {
1516 err = -ENOMEM;
1517 goto err_alloc_dev;
1518 }
1519 dev_net_set(ign->fb_tunnel_dev, net);
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001520 /* FB netdevice is special: we have one, and only one per netns.
1521 * Allowing to move it to another netns is clearly unsafe.
1522 */
1523 ign->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL;
1524
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001525
1526 ip6gre_fb_tunnel_init(ign->fb_tunnel_dev);
1527 ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops;
1528
1529 err = register_netdev(ign->fb_tunnel_dev);
1530 if (err)
1531 goto err_reg_dev;
1532
1533 rcu_assign_pointer(ign->tunnels_wc[0],
1534 netdev_priv(ign->fb_tunnel_dev));
1535 return 0;
1536
1537err_reg_dev:
David S. Millercf124db2017-05-08 12:52:56 -04001538 free_netdev(ign->fb_tunnel_dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001539err_alloc_dev:
1540 return err;
1541}
1542
Eric Dumazetbb401ca2017-09-19 16:27:08 -07001543static void __net_exit ip6gre_exit_batch_net(struct list_head *net_list)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001544{
Eric Dumazetbb401ca2017-09-19 16:27:08 -07001545 struct net *net;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001546 LIST_HEAD(list);
1547
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001548 rtnl_lock();
Eric Dumazetbb401ca2017-09-19 16:27:08 -07001549 list_for_each_entry(net, net_list, exit_list)
1550 ip6gre_destroy_tunnels(net, &list);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001551 unregister_netdevice_many(&list);
1552 rtnl_unlock();
1553}
1554
1555static struct pernet_operations ip6gre_net_ops = {
1556 .init = ip6gre_init_net,
Eric Dumazetbb401ca2017-09-19 16:27:08 -07001557 .exit_batch = ip6gre_exit_batch_net,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001558 .id = &ip6gre_net_id,
1559 .size = sizeof(struct ip6gre_net),
1560};
1561
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001562static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[],
1563 struct netlink_ext_ack *extack)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001564{
1565 __be16 flags;
1566
1567 if (!data)
1568 return 0;
1569
1570 flags = 0;
1571 if (data[IFLA_GRE_IFLAGS])
1572 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1573 if (data[IFLA_GRE_OFLAGS])
1574 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1575 if (flags & (GRE_VERSION|GRE_ROUTING))
1576 return -EINVAL;
1577
1578 return 0;
1579}
1580
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001581static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[],
1582 struct netlink_ext_ack *extack)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001583{
1584 struct in6_addr daddr;
1585
1586 if (tb[IFLA_ADDRESS]) {
1587 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1588 return -EINVAL;
1589 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1590 return -EADDRNOTAVAIL;
1591 }
1592
1593 if (!data)
1594 goto out;
1595
1596 if (data[IFLA_GRE_REMOTE]) {
Jiri Benc67b61f62015-03-29 16:59:26 +02001597 daddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001598 if (ipv6_addr_any(&daddr))
1599 return -EINVAL;
1600 }
1601
1602out:
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001603 return ip6gre_tunnel_validate(tb, data, extack);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001604}
1605
William Tu5a963eb2017-11-30 11:51:29 -08001606static int ip6erspan_tap_validate(struct nlattr *tb[], struct nlattr *data[],
1607 struct netlink_ext_ack *extack)
1608{
1609 __be16 flags = 0;
William Tu94d7d8f2017-12-13 16:38:57 -08001610 int ret, ver = 0;
William Tu5a963eb2017-11-30 11:51:29 -08001611
1612 if (!data)
1613 return 0;
1614
1615 ret = ip6gre_tap_validate(tb, data, extack);
1616 if (ret)
1617 return ret;
1618
1619 /* ERSPAN should only have GRE sequence and key flag */
1620 if (data[IFLA_GRE_OFLAGS])
1621 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1622 if (data[IFLA_GRE_IFLAGS])
1623 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1624 if (!data[IFLA_GRE_COLLECT_METADATA] &&
1625 flags != (GRE_SEQ | GRE_KEY))
1626 return -EINVAL;
1627
1628 /* ERSPAN Session ID only has 10-bit. Since we reuse
1629 * 32-bit key field as ID, check it's range.
1630 */
1631 if (data[IFLA_GRE_IKEY] &&
1632 (ntohl(nla_get_be32(data[IFLA_GRE_IKEY])) & ~ID_MASK))
1633 return -EINVAL;
1634
1635 if (data[IFLA_GRE_OKEY] &&
1636 (ntohl(nla_get_be32(data[IFLA_GRE_OKEY])) & ~ID_MASK))
1637 return -EINVAL;
1638
William Tu94d7d8f2017-12-13 16:38:57 -08001639 if (data[IFLA_GRE_ERSPAN_VER]) {
1640 ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]);
1641 if (ver != 1 && ver != 2)
William Tu5a963eb2017-11-30 11:51:29 -08001642 return -EINVAL;
1643 }
William Tu94d7d8f2017-12-13 16:38:57 -08001644
1645 if (ver == 1) {
1646 if (data[IFLA_GRE_ERSPAN_INDEX]) {
1647 u32 index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
1648
1649 if (index & ~INDEX_MASK)
1650 return -EINVAL;
1651 }
1652 } else if (ver == 2) {
1653 if (data[IFLA_GRE_ERSPAN_DIR]) {
1654 u16 dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]);
1655
1656 if (dir & ~(DIR_MASK >> DIR_OFFSET))
1657 return -EINVAL;
1658 }
1659
1660 if (data[IFLA_GRE_ERSPAN_HWID]) {
1661 u16 hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]);
1662
1663 if (hwid & ~(HWID_MASK >> HWID_OFFSET))
1664 return -EINVAL;
1665 }
1666 }
1667
William Tu5a963eb2017-11-30 11:51:29 -08001668 return 0;
1669}
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001670
1671static void ip6gre_netlink_parms(struct nlattr *data[],
1672 struct __ip6_tnl_parm *parms)
1673{
1674 memset(parms, 0, sizeof(*parms));
1675
1676 if (!data)
1677 return;
1678
1679 if (data[IFLA_GRE_LINK])
1680 parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
1681
1682 if (data[IFLA_GRE_IFLAGS])
Tom Herbertf41fe3c2016-05-09 17:12:09 -07001683 parms->i_flags = gre_flags_to_tnl_flags(
1684 nla_get_be16(data[IFLA_GRE_IFLAGS]));
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001685
1686 if (data[IFLA_GRE_OFLAGS])
Tom Herbertf41fe3c2016-05-09 17:12:09 -07001687 parms->o_flags = gre_flags_to_tnl_flags(
1688 nla_get_be16(data[IFLA_GRE_OFLAGS]));
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001689
1690 if (data[IFLA_GRE_IKEY])
1691 parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
1692
1693 if (data[IFLA_GRE_OKEY])
1694 parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
1695
1696 if (data[IFLA_GRE_LOCAL])
Jiri Benc67b61f62015-03-29 16:59:26 +02001697 parms->laddr = nla_get_in6_addr(data[IFLA_GRE_LOCAL]);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001698
1699 if (data[IFLA_GRE_REMOTE])
Jiri Benc67b61f62015-03-29 16:59:26 +02001700 parms->raddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001701
1702 if (data[IFLA_GRE_TTL])
1703 parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]);
1704
1705 if (data[IFLA_GRE_ENCAP_LIMIT])
1706 parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]);
1707
1708 if (data[IFLA_GRE_FLOWINFO])
Lance Richardsonc2675de2016-09-24 14:01:04 -04001709 parms->flowinfo = nla_get_be32(data[IFLA_GRE_FLOWINFO]);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001710
1711 if (data[IFLA_GRE_FLAGS])
1712 parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]);
Craig Gallek0a473b82017-04-19 12:30:53 -04001713
1714 if (data[IFLA_GRE_FWMARK])
1715 parms->fwmark = nla_get_u32(data[IFLA_GRE_FWMARK]);
William Tu5a963eb2017-11-30 11:51:29 -08001716
William Tu6712abc2017-12-01 15:26:08 -08001717 if (data[IFLA_GRE_COLLECT_METADATA])
1718 parms->collect_md = true;
William Tu94d7d8f2017-12-13 16:38:57 -08001719
1720 if (data[IFLA_GRE_ERSPAN_VER])
1721 parms->erspan_ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]);
1722
1723 if (parms->erspan_ver == 1) {
1724 if (data[IFLA_GRE_ERSPAN_INDEX])
1725 parms->index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
1726 } else if (parms->erspan_ver == 2) {
1727 if (data[IFLA_GRE_ERSPAN_DIR])
1728 parms->dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]);
1729 if (data[IFLA_GRE_ERSPAN_HWID])
1730 parms->hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]);
1731 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001732}
1733
1734static int ip6gre_tap_init(struct net_device *dev)
1735{
Martin KaFai Laua3c119d2015-09-15 14:30:05 -07001736 int ret;
1737
1738 ret = ip6gre_tunnel_init_common(dev);
1739 if (ret)
1740 return ret;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001741
Shweta Choudaha0a46baa2016-06-08 20:15:43 +01001742 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1743
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001744 return 0;
1745}
1746
1747static const struct net_device_ops ip6gre_tap_netdev_ops = {
1748 .ndo_init = ip6gre_tap_init,
1749 .ndo_uninit = ip6gre_tunnel_uninit,
1750 .ndo_start_xmit = ip6gre_tunnel_xmit,
1751 .ndo_set_mac_address = eth_mac_addr,
1752 .ndo_validate_addr = eth_validate_addr,
Tom Herbertb05229f2016-04-29 17:12:21 -07001753 .ndo_change_mtu = ip6_tnl_change_mtu,
Pravin B Shelarf61dd382013-03-25 14:50:00 +00001754 .ndo_get_stats64 = ip_tunnel_get_stats64,
Nicolas Dichtelecf2c062015-04-02 17:07:01 +02001755 .ndo_get_iflink = ip6_tnl_get_iflink,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001756};
1757
Petr Machata2d665032018-05-17 16:36:51 +02001758static int ip6erspan_calc_hlen(struct ip6_tnl *tunnel)
1759{
1760 int t_hlen;
1761
1762 tunnel->tun_hlen = 8;
1763 tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen +
1764 erspan_hdr_len(tunnel->parms.erspan_ver);
1765
1766 t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
1767 tunnel->dev->hard_header_len = LL_MAX_HEADER + t_hlen;
1768 return t_hlen;
1769}
1770
William Tu5a963eb2017-11-30 11:51:29 -08001771static int ip6erspan_tap_init(struct net_device *dev)
1772{
1773 struct ip6_tnl *tunnel;
1774 int t_hlen;
1775 int ret;
1776
1777 tunnel = netdev_priv(dev);
1778
1779 tunnel->dev = dev;
1780 tunnel->net = dev_net(dev);
1781 strcpy(tunnel->parms.name, dev->name);
1782
1783 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1784 if (!dev->tstats)
1785 return -ENOMEM;
1786
1787 ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
1788 if (ret) {
1789 free_percpu(dev->tstats);
1790 dev->tstats = NULL;
1791 return ret;
1792 }
1793
Petr Machata2d665032018-05-17 16:36:51 +02001794 t_hlen = ip6erspan_calc_hlen(tunnel);
William Tu5a963eb2017-11-30 11:51:29 -08001795 dev->mtu = ETH_DATA_LEN - t_hlen;
1796 if (dev->type == ARPHRD_ETHER)
1797 dev->mtu -= ETH_HLEN;
1798 if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1799 dev->mtu -= 8;
1800
1801 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Petr Machata2d665032018-05-17 16:36:51 +02001802 ip6erspan_tnl_link_config(tunnel, 1);
William Tu5a963eb2017-11-30 11:51:29 -08001803
1804 return 0;
1805}
1806
1807static const struct net_device_ops ip6erspan_netdev_ops = {
1808 .ndo_init = ip6erspan_tap_init,
1809 .ndo_uninit = ip6gre_tunnel_uninit,
1810 .ndo_start_xmit = ip6erspan_tunnel_xmit,
1811 .ndo_set_mac_address = eth_mac_addr,
1812 .ndo_validate_addr = eth_validate_addr,
1813 .ndo_change_mtu = ip6_tnl_change_mtu,
1814 .ndo_get_stats64 = ip_tunnel_get_stats64,
1815 .ndo_get_iflink = ip6_tnl_get_iflink,
1816};
1817
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001818static void ip6gre_tap_setup(struct net_device *dev)
1819{
1820
1821 ether_setup(dev);
1822
Xin Long2c521292017-12-18 14:25:09 +08001823 dev->max_mtu = 0;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001824 dev->netdev_ops = &ip6gre_tap_netdev_ops;
David S. Millercf124db2017-05-08 12:52:56 -04001825 dev->needs_free_netdev = true;
1826 dev->priv_destructor = ip6gre_dev_free;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001827
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001828 dev->features |= NETIF_F_NETNS_LOCAL;
Jiri Bencd13b1612016-02-17 15:32:53 +01001829 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
Shweta Choudaha0a46baa2016-06-08 20:15:43 +01001830 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Xin Long2d405572017-09-28 13:23:50 +08001831 netif_keep_dst(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001832}
1833
Petr Machatad1b2a6c2018-02-27 14:53:37 +01001834bool is_ip6gretap_dev(const struct net_device *dev)
1835{
1836 return dev->netdev_ops == &ip6gre_tap_netdev_ops;
1837}
1838EXPORT_SYMBOL_GPL(is_ip6gretap_dev);
1839
Tom Herbert1faf3d92016-05-18 09:06:19 -07001840static bool ip6gre_netlink_encap_parms(struct nlattr *data[],
1841 struct ip_tunnel_encap *ipencap)
1842{
1843 bool ret = false;
1844
1845 memset(ipencap, 0, sizeof(*ipencap));
1846
1847 if (!data)
1848 return ret;
1849
1850 if (data[IFLA_GRE_ENCAP_TYPE]) {
1851 ret = true;
1852 ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]);
1853 }
1854
1855 if (data[IFLA_GRE_ENCAP_FLAGS]) {
1856 ret = true;
1857 ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]);
1858 }
1859
1860 if (data[IFLA_GRE_ENCAP_SPORT]) {
1861 ret = true;
1862 ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]);
1863 }
1864
1865 if (data[IFLA_GRE_ENCAP_DPORT]) {
1866 ret = true;
1867 ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]);
1868 }
1869
1870 return ret;
1871}
1872
Petr Machata7fa38a72018-05-17 16:36:39 +02001873static int ip6gre_newlink_common(struct net *src_net, struct net_device *dev,
1874 struct nlattr *tb[], struct nlattr *data[],
1875 struct netlink_ext_ack *extack)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001876{
1877 struct ip6_tnl *nt;
1878 struct net *net = dev_net(dev);
1879 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
Tom Herbert1faf3d92016-05-18 09:06:19 -07001880 struct ip_tunnel_encap ipencap;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001881 int err;
1882
1883 nt = netdev_priv(dev);
Tom Herbert1faf3d92016-05-18 09:06:19 -07001884
1885 if (ip6gre_netlink_encap_parms(data, &ipencap)) {
1886 int err = ip6_tnl_encap_setup(nt, &ipencap);
1887
1888 if (err < 0)
1889 return err;
1890 }
1891
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001892 ip6gre_netlink_parms(data, &nt->parms);
1893
William Tu6712abc2017-12-01 15:26:08 -08001894 if (nt->parms.collect_md) {
1895 if (rtnl_dereference(ign->collect_md_tun))
1896 return -EEXIST;
1897 } else {
1898 if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
1899 return -EEXIST;
1900 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001901
1902 if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
1903 eth_hw_addr_random(dev);
1904
1905 nt->dev = dev;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001906 nt->net = dev_net(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001907
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001908 err = register_netdevice(dev);
1909 if (err)
1910 goto out;
1911
Alexey Kodanev128bb972018-01-18 20:51:12 +03001912 if (tb[IFLA_MTU])
1913 ip6_tnl_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1914
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001915 dev_hold(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001916
1917out:
1918 return err;
1919}
1920
Petr Machata7fa38a72018-05-17 16:36:39 +02001921static int ip6gre_newlink(struct net *src_net, struct net_device *dev,
1922 struct nlattr *tb[], struct nlattr *data[],
1923 struct netlink_ext_ack *extack)
1924{
1925 int err = ip6gre_newlink_common(src_net, dev, tb, data, extack);
1926 struct ip6_tnl *nt = netdev_priv(dev);
1927 struct net *net = dev_net(dev);
1928
1929 if (!err) {
1930 ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]);
1931 ip6gre_tunnel_link(net_generic(net, ip6gre_net_id), nt);
1932 }
1933 return err;
1934}
1935
Petr Machatac8632fc2018-05-17 16:36:45 +02001936static struct ip6_tnl *
1937ip6gre_changelink_common(struct net_device *dev, struct nlattr *tb[],
1938 struct nlattr *data[], struct __ip6_tnl_parm *p_p,
1939 struct netlink_ext_ack *extack)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001940{
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001941 struct ip6_tnl *t, *nt = netdev_priv(dev);
1942 struct net *net = nt->net;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001943 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
Tom Herbert1faf3d92016-05-18 09:06:19 -07001944 struct ip_tunnel_encap ipencap;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001945
1946 if (dev == ign->fb_tunnel_dev)
Petr Machatac8632fc2018-05-17 16:36:45 +02001947 return ERR_PTR(-EINVAL);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001948
Tom Herbert1faf3d92016-05-18 09:06:19 -07001949 if (ip6gre_netlink_encap_parms(data, &ipencap)) {
1950 int err = ip6_tnl_encap_setup(nt, &ipencap);
1951
1952 if (err < 0)
Petr Machatac8632fc2018-05-17 16:36:45 +02001953 return ERR_PTR(err);
Tom Herbert1faf3d92016-05-18 09:06:19 -07001954 }
1955
Petr Machatac8632fc2018-05-17 16:36:45 +02001956 ip6gre_netlink_parms(data, p_p);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001957
Petr Machatac8632fc2018-05-17 16:36:45 +02001958 t = ip6gre_tunnel_locate(net, p_p, 0);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001959
1960 if (t) {
1961 if (t->dev != dev)
Petr Machatac8632fc2018-05-17 16:36:45 +02001962 return ERR_PTR(-EEXIST);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001963 } else {
1964 t = nt;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001965 }
1966
Petr Machatac8632fc2018-05-17 16:36:45 +02001967 return t;
1968}
1969
1970static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],
1971 struct nlattr *data[],
1972 struct netlink_ext_ack *extack)
1973{
1974 struct ip6gre_net *ign = net_generic(dev_net(dev), ip6gre_net_id);
1975 struct __ip6_tnl_parm p;
1976 struct ip6_tnl *t;
1977
1978 t = ip6gre_changelink_common(dev, tb, data, &p, extack);
1979 if (IS_ERR(t))
1980 return PTR_ERR(t);
1981
Nicolas Dichtel6a61d4d2015-12-03 17:21:50 +01001982 ip6gre_tunnel_unlink(ign, t);
1983 ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]);
1984 ip6gre_tunnel_link(ign, t);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001985 return 0;
1986}
1987
Nicolas Dichtel54d63f782014-04-14 17:11:38 +02001988static void ip6gre_dellink(struct net_device *dev, struct list_head *head)
1989{
1990 struct net *net = dev_net(dev);
1991 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1992
1993 if (dev != ign->fb_tunnel_dev)
1994 unregister_netdevice_queue(dev, head);
1995}
1996
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001997static size_t ip6gre_get_size(const struct net_device *dev)
1998{
1999 return
2000 /* IFLA_GRE_LINK */
2001 nla_total_size(4) +
2002 /* IFLA_GRE_IFLAGS */
2003 nla_total_size(2) +
2004 /* IFLA_GRE_OFLAGS */
2005 nla_total_size(2) +
2006 /* IFLA_GRE_IKEY */
2007 nla_total_size(4) +
2008 /* IFLA_GRE_OKEY */
2009 nla_total_size(4) +
2010 /* IFLA_GRE_LOCAL */
Nicolas Dichtela3754132012-11-09 05:34:56 +00002011 nla_total_size(sizeof(struct in6_addr)) +
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002012 /* IFLA_GRE_REMOTE */
Nicolas Dichtela3754132012-11-09 05:34:56 +00002013 nla_total_size(sizeof(struct in6_addr)) +
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002014 /* IFLA_GRE_TTL */
2015 nla_total_size(1) +
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002016 /* IFLA_GRE_ENCAP_LIMIT */
2017 nla_total_size(1) +
2018 /* IFLA_GRE_FLOWINFO */
2019 nla_total_size(4) +
2020 /* IFLA_GRE_FLAGS */
2021 nla_total_size(4) +
Tom Herbert1faf3d92016-05-18 09:06:19 -07002022 /* IFLA_GRE_ENCAP_TYPE */
2023 nla_total_size(2) +
2024 /* IFLA_GRE_ENCAP_FLAGS */
2025 nla_total_size(2) +
2026 /* IFLA_GRE_ENCAP_SPORT */
2027 nla_total_size(2) +
2028 /* IFLA_GRE_ENCAP_DPORT */
2029 nla_total_size(2) +
William Tu6712abc2017-12-01 15:26:08 -08002030 /* IFLA_GRE_COLLECT_METADATA */
2031 nla_total_size(0) +
Craig Gallek0a473b82017-04-19 12:30:53 -04002032 /* IFLA_GRE_FWMARK */
2033 nla_total_size(4) +
William Tu5a963eb2017-11-30 11:51:29 -08002034 /* IFLA_GRE_ERSPAN_INDEX */
2035 nla_total_size(4) +
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002036 0;
2037}
2038
2039static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev)
2040{
2041 struct ip6_tnl *t = netdev_priv(dev);
2042 struct __ip6_tnl_parm *p = &t->parms;
2043
2044 if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
Tom Herbertf41fe3c2016-05-09 17:12:09 -07002045 nla_put_be16(skb, IFLA_GRE_IFLAGS,
2046 gre_tnl_flags_to_gre_flags(p->i_flags)) ||
2047 nla_put_be16(skb, IFLA_GRE_OFLAGS,
2048 gre_tnl_flags_to_gre_flags(p->o_flags)) ||
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002049 nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
2050 nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
Jiri Benc930345e2015-03-29 16:59:25 +02002051 nla_put_in6_addr(skb, IFLA_GRE_LOCAL, &p->laddr) ||
2052 nla_put_in6_addr(skb, IFLA_GRE_REMOTE, &p->raddr) ||
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002053 nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) ||
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002054 nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) ||
2055 nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) ||
Craig Gallek0a473b82017-04-19 12:30:53 -04002056 nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags) ||
William Tu5a963eb2017-11-30 11:51:29 -08002057 nla_put_u32(skb, IFLA_GRE_FWMARK, p->fwmark) ||
2058 nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, p->index))
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002059 goto nla_put_failure;
Tom Herbert1faf3d92016-05-18 09:06:19 -07002060
2061 if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE,
2062 t->encap.type) ||
2063 nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT,
2064 t->encap.sport) ||
2065 nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT,
2066 t->encap.dport) ||
2067 nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS,
2068 t->encap.flags))
2069 goto nla_put_failure;
2070
William Tu6712abc2017-12-01 15:26:08 -08002071 if (p->collect_md) {
2072 if (nla_put_flag(skb, IFLA_GRE_COLLECT_METADATA))
2073 goto nla_put_failure;
2074 }
2075
William Tu94d7d8f2017-12-13 16:38:57 -08002076 if (nla_put_u8(skb, IFLA_GRE_ERSPAN_VER, p->erspan_ver))
2077 goto nla_put_failure;
2078
2079 if (p->erspan_ver == 1) {
2080 if (nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, p->index))
2081 goto nla_put_failure;
2082 } else if (p->erspan_ver == 2) {
2083 if (nla_put_u8(skb, IFLA_GRE_ERSPAN_DIR, p->dir))
2084 goto nla_put_failure;
2085 if (nla_put_u16(skb, IFLA_GRE_ERSPAN_HWID, p->hwid))
2086 goto nla_put_failure;
2087 }
2088
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002089 return 0;
2090
2091nla_put_failure:
2092 return -EMSGSIZE;
2093}
2094
2095static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = {
2096 [IFLA_GRE_LINK] = { .type = NLA_U32 },
2097 [IFLA_GRE_IFLAGS] = { .type = NLA_U16 },
2098 [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
2099 [IFLA_GRE_IKEY] = { .type = NLA_U32 },
2100 [IFLA_GRE_OKEY] = { .type = NLA_U32 },
2101 [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct ipv6hdr, saddr) },
2102 [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct ipv6hdr, daddr) },
2103 [IFLA_GRE_TTL] = { .type = NLA_U8 },
2104 [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 },
2105 [IFLA_GRE_FLOWINFO] = { .type = NLA_U32 },
2106 [IFLA_GRE_FLAGS] = { .type = NLA_U32 },
Tom Herbert1faf3d92016-05-18 09:06:19 -07002107 [IFLA_GRE_ENCAP_TYPE] = { .type = NLA_U16 },
2108 [IFLA_GRE_ENCAP_FLAGS] = { .type = NLA_U16 },
2109 [IFLA_GRE_ENCAP_SPORT] = { .type = NLA_U16 },
2110 [IFLA_GRE_ENCAP_DPORT] = { .type = NLA_U16 },
William Tu6712abc2017-12-01 15:26:08 -08002111 [IFLA_GRE_COLLECT_METADATA] = { .type = NLA_FLAG },
Craig Gallek0a473b82017-04-19 12:30:53 -04002112 [IFLA_GRE_FWMARK] = { .type = NLA_U32 },
William Tu5a963eb2017-11-30 11:51:29 -08002113 [IFLA_GRE_ERSPAN_INDEX] = { .type = NLA_U32 },
William Tu94d7d8f2017-12-13 16:38:57 -08002114 [IFLA_GRE_ERSPAN_VER] = { .type = NLA_U8 },
2115 [IFLA_GRE_ERSPAN_DIR] = { .type = NLA_U8 },
2116 [IFLA_GRE_ERSPAN_HWID] = { .type = NLA_U16 },
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002117};
2118
William Tu5a963eb2017-11-30 11:51:29 -08002119static void ip6erspan_tap_setup(struct net_device *dev)
2120{
2121 ether_setup(dev);
2122
2123 dev->netdev_ops = &ip6erspan_netdev_ops;
2124 dev->needs_free_netdev = true;
2125 dev->priv_destructor = ip6gre_dev_free;
2126
2127 dev->features |= NETIF_F_NETNS_LOCAL;
2128 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
2129 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2130 netif_keep_dst(dev);
2131}
2132
Petr Machata2d665032018-05-17 16:36:51 +02002133static int ip6erspan_newlink(struct net *src_net, struct net_device *dev,
2134 struct nlattr *tb[], struct nlattr *data[],
2135 struct netlink_ext_ack *extack)
2136{
2137 int err = ip6gre_newlink_common(src_net, dev, tb, data, extack);
2138 struct ip6_tnl *nt = netdev_priv(dev);
2139 struct net *net = dev_net(dev);
2140
2141 if (!err) {
2142 ip6erspan_tnl_link_config(nt, !tb[IFLA_MTU]);
2143 ip6gre_tunnel_link(net_generic(net, ip6gre_net_id), nt);
2144 }
2145 return err;
2146}
2147
2148static void ip6erspan_tnl_link_config(struct ip6_tnl *t, int set_mtu)
2149{
2150 ip6gre_tnl_link_config_common(t);
2151 ip6gre_tnl_link_config_route(t, set_mtu, ip6erspan_calc_hlen(t));
2152}
2153
2154static int ip6erspan_tnl_change(struct ip6_tnl *t,
2155 const struct __ip6_tnl_parm *p, int set_mtu)
2156{
2157 ip6gre_tnl_copy_tnl_parm(t, p);
2158 ip6erspan_tnl_link_config(t, set_mtu);
2159 return 0;
2160}
2161
2162static int ip6erspan_changelink(struct net_device *dev, struct nlattr *tb[],
2163 struct nlattr *data[],
2164 struct netlink_ext_ack *extack)
2165{
2166 struct ip6gre_net *ign = net_generic(dev_net(dev), ip6gre_net_id);
2167 struct __ip6_tnl_parm p;
2168 struct ip6_tnl *t;
2169
2170 t = ip6gre_changelink_common(dev, tb, data, &p, extack);
2171 if (IS_ERR(t))
2172 return PTR_ERR(t);
2173
2174 ip6gre_tunnel_unlink(ign, t);
2175 ip6erspan_tnl_change(t, &p, !tb[IFLA_MTU]);
2176 ip6gre_tunnel_link(ign, t);
2177 return 0;
2178}
2179
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002180static struct rtnl_link_ops ip6gre_link_ops __read_mostly = {
2181 .kind = "ip6gre",
2182 .maxtype = IFLA_GRE_MAX,
2183 .policy = ip6gre_policy,
2184 .priv_size = sizeof(struct ip6_tnl),
2185 .setup = ip6gre_tunnel_setup,
2186 .validate = ip6gre_tunnel_validate,
2187 .newlink = ip6gre_newlink,
2188 .changelink = ip6gre_changelink,
Nicolas Dichtel54d63f782014-04-14 17:11:38 +02002189 .dellink = ip6gre_dellink,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002190 .get_size = ip6gre_get_size,
2191 .fill_info = ip6gre_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01002192 .get_link_net = ip6_tnl_get_link_net,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002193};
2194
2195static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = {
2196 .kind = "ip6gretap",
2197 .maxtype = IFLA_GRE_MAX,
2198 .policy = ip6gre_policy,
2199 .priv_size = sizeof(struct ip6_tnl),
2200 .setup = ip6gre_tap_setup,
2201 .validate = ip6gre_tap_validate,
2202 .newlink = ip6gre_newlink,
2203 .changelink = ip6gre_changelink,
2204 .get_size = ip6gre_get_size,
2205 .fill_info = ip6gre_fill_info,
Nicolas Dichtel3390e392015-01-20 15:15:43 +01002206 .get_link_net = ip6_tnl_get_link_net,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002207};
2208
William Tu5a963eb2017-11-30 11:51:29 -08002209static struct rtnl_link_ops ip6erspan_tap_ops __read_mostly = {
2210 .kind = "ip6erspan",
2211 .maxtype = IFLA_GRE_MAX,
2212 .policy = ip6gre_policy,
2213 .priv_size = sizeof(struct ip6_tnl),
2214 .setup = ip6erspan_tap_setup,
2215 .validate = ip6erspan_tap_validate,
Petr Machata2d665032018-05-17 16:36:51 +02002216 .newlink = ip6erspan_newlink,
2217 .changelink = ip6erspan_changelink,
William Tu5a963eb2017-11-30 11:51:29 -08002218 .get_size = ip6gre_get_size,
2219 .fill_info = ip6gre_fill_info,
2220 .get_link_net = ip6_tnl_get_link_net,
2221};
2222
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002223/*
2224 * And now the modules code and kernel interface.
2225 */
2226
2227static int __init ip6gre_init(void)
2228{
2229 int err;
2230
2231 pr_info("GRE over IPv6 tunneling driver\n");
2232
2233 err = register_pernet_device(&ip6gre_net_ops);
2234 if (err < 0)
2235 return err;
2236
2237 err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE);
2238 if (err < 0) {
2239 pr_info("%s: can't add protocol\n", __func__);
2240 goto add_proto_failed;
2241 }
2242
2243 err = rtnl_link_register(&ip6gre_link_ops);
2244 if (err < 0)
2245 goto rtnl_link_failed;
2246
2247 err = rtnl_link_register(&ip6gre_tap_ops);
2248 if (err < 0)
2249 goto tap_ops_failed;
2250
William Tu5a963eb2017-11-30 11:51:29 -08002251 err = rtnl_link_register(&ip6erspan_tap_ops);
2252 if (err < 0)
2253 goto erspan_link_failed;
2254
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002255out:
2256 return err;
2257
William Tu5a963eb2017-11-30 11:51:29 -08002258erspan_link_failed:
2259 rtnl_link_unregister(&ip6gre_tap_ops);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002260tap_ops_failed:
2261 rtnl_link_unregister(&ip6gre_link_ops);
2262rtnl_link_failed:
2263 inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
2264add_proto_failed:
2265 unregister_pernet_device(&ip6gre_net_ops);
2266 goto out;
2267}
2268
2269static void __exit ip6gre_fini(void)
2270{
2271 rtnl_link_unregister(&ip6gre_tap_ops);
2272 rtnl_link_unregister(&ip6gre_link_ops);
William Tu5a963eb2017-11-30 11:51:29 -08002273 rtnl_link_unregister(&ip6erspan_tap_ops);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002274 inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
2275 unregister_pernet_device(&ip6gre_net_ops);
2276}
2277
2278module_init(ip6gre_init);
2279module_exit(ip6gre_fini);
2280MODULE_LICENSE("GPL");
2281MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
2282MODULE_DESCRIPTION("GRE over IPv6 tunneling device");
2283MODULE_ALIAS_RTNL_LINK("ip6gre");
Nicolas Dichtel5a4ee9a2014-09-24 11:03:00 +02002284MODULE_ALIAS_RTNL_LINK("ip6gretap");
William Tu94d7d8f2017-12-13 16:38:57 -08002285MODULE_ALIAS_RTNL_LINK("ip6erspan");
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002286MODULE_ALIAS_NETDEV("ip6gre0");