blob: a3e1dec96a7482e6a6c27cd62426a3f4e0128d1b [file] [log] [blame]
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001/*
2 * GRE over IPv6 protocol decoder.
3 *
4 * Authors: Dmitry Kozlov (xeb@mail.ru)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
15#include <linux/capability.h>
16#include <linux/module.h>
17#include <linux/types.h>
18#include <linux/kernel.h>
19#include <linux/slab.h>
20#include <linux/uaccess.h>
21#include <linux/skbuff.h>
22#include <linux/netdevice.h>
23#include <linux/in.h>
24#include <linux/tcp.h>
25#include <linux/udp.h>
26#include <linux/if_arp.h>
xeb@mail.ruc12b3952012-08-10 00:51:50 +000027#include <linux/init.h>
28#include <linux/in6.h>
29#include <linux/inetdevice.h>
30#include <linux/igmp.h>
31#include <linux/netfilter_ipv4.h>
32#include <linux/etherdevice.h>
33#include <linux/if_ether.h>
34#include <linux/hash.h>
35#include <linux/if_tunnel.h>
36#include <linux/ip6_tunnel.h>
37
38#include <net/sock.h>
39#include <net/ip.h>
Pravin B Shelarc5441932013-03-25 14:49:35 +000040#include <net/ip_tunnels.h>
xeb@mail.ruc12b3952012-08-10 00:51:50 +000041#include <net/icmp.h>
42#include <net/protocol.h>
43#include <net/addrconf.h>
44#include <net/arp.h>
45#include <net/checksum.h>
46#include <net/dsfield.h>
47#include <net/inet_ecn.h>
48#include <net/xfrm.h>
49#include <net/net_namespace.h>
50#include <net/netns/generic.h>
51#include <net/rtnetlink.h>
52
53#include <net/ipv6.h>
54#include <net/ip6_fib.h>
55#include <net/ip6_route.h>
56#include <net/ip6_tunnel.h>
Tom Herbert308edfdf2016-04-29 17:12:17 -070057#include <net/gre.h>
William Tu5a963eb2017-11-30 11:51:29 -080058#include <net/erspan.h>
William Tu6712abc2017-12-01 15:26:08 -080059#include <net/dst_metadata.h>
xeb@mail.ruc12b3952012-08-10 00:51:50 +000060
61
stephen hemmingereccc1bb2012-09-25 11:02:48 +000062static bool log_ecn_error = true;
63module_param(log_ecn_error, bool, 0644);
64MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
65
Jiri Kosinae87a8f22016-08-10 11:03:35 +020066#define IP6_GRE_HASH_SIZE_SHIFT 5
67#define IP6_GRE_HASH_SIZE (1 << IP6_GRE_HASH_SIZE_SHIFT)
xeb@mail.ruc12b3952012-08-10 00:51:50 +000068
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030069static unsigned int ip6gre_net_id __read_mostly;
xeb@mail.ruc12b3952012-08-10 00:51:50 +000070struct ip6gre_net {
Jiri Kosinae87a8f22016-08-10 11:03:35 +020071 struct ip6_tnl __rcu *tunnels[4][IP6_GRE_HASH_SIZE];
xeb@mail.ruc12b3952012-08-10 00:51:50 +000072
William Tu6712abc2017-12-01 15:26:08 -080073 struct ip6_tnl __rcu *collect_md_tun;
xeb@mail.ruc12b3952012-08-10 00:51:50 +000074 struct net_device *fb_tunnel_dev;
75};
76
77static struct rtnl_link_ops ip6gre_link_ops __read_mostly;
Nicolas Dichtel22f08062014-04-22 10:15:24 +020078static struct rtnl_link_ops ip6gre_tap_ops __read_mostly;
William Tu5a963eb2017-11-30 11:51:29 -080079static struct rtnl_link_ops ip6erspan_tap_ops __read_mostly;
xeb@mail.ruc12b3952012-08-10 00:51:50 +000080static int ip6gre_tunnel_init(struct net_device *dev);
81static void ip6gre_tunnel_setup(struct net_device *dev);
82static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t);
83static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu);
84
85/* Tunnel hash table */
86
87/*
88 4 hash tables:
89
90 3: (remote,local)
91 2: (remote,*)
92 1: (*,local)
93 0: (*,*)
94
95 We require exact key match i.e. if a key is present in packet
96 it will match only tunnel with the same key; if it is not present,
97 it will match only keyless tunnel.
98
99 All keysless packets, if not matched configured keyless tunnels
100 will match fallback tunnel.
101 */
102
Jiri Kosinae87a8f22016-08-10 11:03:35 +0200103#define HASH_KEY(key) (((__force u32)key^((__force u32)key>>4))&(IP6_GRE_HASH_SIZE - 1))
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000104static u32 HASH_ADDR(const struct in6_addr *addr)
105{
106 u32 hash = ipv6_addr_hash(addr);
107
Jiri Kosinae87a8f22016-08-10 11:03:35 +0200108 return hash_32(hash, IP6_GRE_HASH_SIZE_SHIFT);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000109}
110
111#define tunnels_r_l tunnels[3]
112#define tunnels_r tunnels[2]
113#define tunnels_l tunnels[1]
114#define tunnels_wc tunnels[0]
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000115
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000116/* Given src, dst and key, find appropriate for input tunnel. */
117
118static struct ip6_tnl *ip6gre_tunnel_lookup(struct net_device *dev,
119 const struct in6_addr *remote, const struct in6_addr *local,
120 __be32 key, __be16 gre_proto)
121{
122 struct net *net = dev_net(dev);
123 int link = dev->ifindex;
124 unsigned int h0 = HASH_ADDR(remote);
125 unsigned int h1 = HASH_KEY(key);
126 struct ip6_tnl *t, *cand = NULL;
127 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
William Tu5a963eb2017-11-30 11:51:29 -0800128 int dev_type = (gre_proto == htons(ETH_P_TEB) ||
William Tu3b04caa2018-03-09 07:34:40 -0800129 gre_proto == htons(ETH_P_ERSPAN) ||
130 gre_proto == htons(ETH_P_ERSPAN2)) ?
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000131 ARPHRD_ETHER : ARPHRD_IP6GRE;
132 int score, cand_score = 4;
133
Amerigo Wange086cad2012-11-11 21:52:34 +0000134 for_each_ip_tunnel_rcu(t, ign->tunnels_r_l[h0 ^ h1]) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000135 if (!ipv6_addr_equal(local, &t->parms.laddr) ||
136 !ipv6_addr_equal(remote, &t->parms.raddr) ||
137 key != t->parms.i_key ||
138 !(t->dev->flags & IFF_UP))
139 continue;
140
141 if (t->dev->type != ARPHRD_IP6GRE &&
142 t->dev->type != dev_type)
143 continue;
144
145 score = 0;
146 if (t->parms.link != link)
147 score |= 1;
148 if (t->dev->type != dev_type)
149 score |= 2;
150 if (score == 0)
151 return t;
152
153 if (score < cand_score) {
154 cand = t;
155 cand_score = score;
156 }
157 }
158
Amerigo Wange086cad2012-11-11 21:52:34 +0000159 for_each_ip_tunnel_rcu(t, ign->tunnels_r[h0 ^ h1]) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000160 if (!ipv6_addr_equal(remote, &t->parms.raddr) ||
161 key != t->parms.i_key ||
162 !(t->dev->flags & IFF_UP))
163 continue;
164
165 if (t->dev->type != ARPHRD_IP6GRE &&
166 t->dev->type != dev_type)
167 continue;
168
169 score = 0;
170 if (t->parms.link != link)
171 score |= 1;
172 if (t->dev->type != dev_type)
173 score |= 2;
174 if (score == 0)
175 return t;
176
177 if (score < cand_score) {
178 cand = t;
179 cand_score = score;
180 }
181 }
182
Amerigo Wange086cad2012-11-11 21:52:34 +0000183 for_each_ip_tunnel_rcu(t, ign->tunnels_l[h1]) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000184 if ((!ipv6_addr_equal(local, &t->parms.laddr) &&
185 (!ipv6_addr_equal(local, &t->parms.raddr) ||
186 !ipv6_addr_is_multicast(local))) ||
187 key != t->parms.i_key ||
188 !(t->dev->flags & IFF_UP))
189 continue;
190
191 if (t->dev->type != ARPHRD_IP6GRE &&
192 t->dev->type != dev_type)
193 continue;
194
195 score = 0;
196 if (t->parms.link != link)
197 score |= 1;
198 if (t->dev->type != dev_type)
199 score |= 2;
200 if (score == 0)
201 return t;
202
203 if (score < cand_score) {
204 cand = t;
205 cand_score = score;
206 }
207 }
208
Amerigo Wange086cad2012-11-11 21:52:34 +0000209 for_each_ip_tunnel_rcu(t, ign->tunnels_wc[h1]) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000210 if (t->parms.i_key != key ||
211 !(t->dev->flags & IFF_UP))
212 continue;
213
214 if (t->dev->type != ARPHRD_IP6GRE &&
215 t->dev->type != dev_type)
216 continue;
217
218 score = 0;
219 if (t->parms.link != link)
220 score |= 1;
221 if (t->dev->type != dev_type)
222 score |= 2;
223 if (score == 0)
224 return t;
225
226 if (score < cand_score) {
227 cand = t;
228 cand_score = score;
229 }
230 }
231
Ian Morris53b24b82015-03-29 14:00:05 +0100232 if (cand)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000233 return cand;
234
William Tu6712abc2017-12-01 15:26:08 -0800235 t = rcu_dereference(ign->collect_md_tun);
236 if (t && t->dev->flags & IFF_UP)
237 return t;
238
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000239 dev = ign->fb_tunnel_dev;
Eric Dumazet79134e62018-03-08 12:51:41 -0800240 if (dev && dev->flags & IFF_UP)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000241 return netdev_priv(dev);
242
243 return NULL;
244}
245
246static struct ip6_tnl __rcu **__ip6gre_bucket(struct ip6gre_net *ign,
247 const struct __ip6_tnl_parm *p)
248{
249 const struct in6_addr *remote = &p->raddr;
250 const struct in6_addr *local = &p->laddr;
251 unsigned int h = HASH_KEY(p->i_key);
252 int prio = 0;
253
254 if (!ipv6_addr_any(local))
255 prio |= 1;
256 if (!ipv6_addr_any(remote) && !ipv6_addr_is_multicast(remote)) {
257 prio |= 2;
258 h ^= HASH_ADDR(remote);
259 }
260
261 return &ign->tunnels[prio][h];
262}
263
264static inline struct ip6_tnl __rcu **ip6gre_bucket(struct ip6gre_net *ign,
265 const struct ip6_tnl *t)
266{
267 return __ip6gre_bucket(ign, &t->parms);
268}
269
270static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t)
271{
272 struct ip6_tnl __rcu **tp = ip6gre_bucket(ign, t);
273
William Tu6712abc2017-12-01 15:26:08 -0800274 if (t->parms.collect_md)
275 rcu_assign_pointer(ign->collect_md_tun, t);
276
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000277 rcu_assign_pointer(t->next, rtnl_dereference(*tp));
278 rcu_assign_pointer(*tp, t);
279}
280
281static void ip6gre_tunnel_unlink(struct ip6gre_net *ign, struct ip6_tnl *t)
282{
283 struct ip6_tnl __rcu **tp;
284 struct ip6_tnl *iter;
285
William Tu6712abc2017-12-01 15:26:08 -0800286 if (t->parms.collect_md)
287 rcu_assign_pointer(ign->collect_md_tun, NULL);
288
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000289 for (tp = ip6gre_bucket(ign, t);
290 (iter = rtnl_dereference(*tp)) != NULL;
291 tp = &iter->next) {
292 if (t == iter) {
293 rcu_assign_pointer(*tp, t->next);
294 break;
295 }
296 }
297}
298
299static struct ip6_tnl *ip6gre_tunnel_find(struct net *net,
300 const struct __ip6_tnl_parm *parms,
301 int type)
302{
303 const struct in6_addr *remote = &parms->raddr;
304 const struct in6_addr *local = &parms->laddr;
305 __be32 key = parms->i_key;
306 int link = parms->link;
307 struct ip6_tnl *t;
308 struct ip6_tnl __rcu **tp;
309 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
310
311 for (tp = __ip6gre_bucket(ign, parms);
312 (t = rtnl_dereference(*tp)) != NULL;
313 tp = &t->next)
314 if (ipv6_addr_equal(local, &t->parms.laddr) &&
315 ipv6_addr_equal(remote, &t->parms.raddr) &&
316 key == t->parms.i_key &&
317 link == t->parms.link &&
318 type == t->dev->type)
319 break;
320
321 return t;
322}
323
324static struct ip6_tnl *ip6gre_tunnel_locate(struct net *net,
325 const struct __ip6_tnl_parm *parms, int create)
326{
327 struct ip6_tnl *t, *nt;
328 struct net_device *dev;
329 char name[IFNAMSIZ];
330 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
331
332 t = ip6gre_tunnel_find(net, parms, ARPHRD_IP6GRE);
Steffen Klassertcd0a0bd2014-09-22 10:07:26 +0200333 if (t && create)
334 return NULL;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000335 if (t || !create)
336 return t;
337
Eric Dumazet5f42df02018-04-05 06:39:29 -0700338 if (parms->name[0]) {
339 if (!dev_valid_name(parms->name))
340 return NULL;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000341 strlcpy(name, parms->name, IFNAMSIZ);
Eric Dumazet5f42df02018-04-05 06:39:29 -0700342 } else {
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000343 strcpy(name, "ip6gre%d");
Eric Dumazet5f42df02018-04-05 06:39:29 -0700344 }
Tom Gundersenc835a672014-07-14 16:37:24 +0200345 dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
346 ip6gre_tunnel_setup);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000347 if (!dev)
348 return NULL;
349
350 dev_net_set(dev, net);
351
352 nt = netdev_priv(dev);
353 nt->parms = *parms;
354 dev->rtnl_link_ops = &ip6gre_link_ops;
355
356 nt->dev = dev;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +0200357 nt->net = dev_net(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000358
359 if (register_netdevice(dev) < 0)
360 goto failed_free;
361
Alexey Kodanev128bb972018-01-18 20:51:12 +0300362 ip6gre_tnl_link_config(nt, 1);
363
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000364 /* Can use a lockless transmit, unless we generate output sequences */
Tom Herbertb45bd1d2016-05-09 17:12:12 -0700365 if (!(nt->parms.o_flags & TUNNEL_SEQ))
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000366 dev->features |= NETIF_F_LLTX;
367
368 dev_hold(dev);
369 ip6gre_tunnel_link(ign, nt);
370 return nt;
371
372failed_free:
373 free_netdev(dev);
374 return NULL;
375}
376
377static void ip6gre_tunnel_uninit(struct net_device *dev)
378{
Nicolas Dichtel22f08062014-04-22 10:15:24 +0200379 struct ip6_tnl *t = netdev_priv(dev);
380 struct ip6gre_net *ign = net_generic(t->net, ip6gre_net_id);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000381
Nicolas Dichtel22f08062014-04-22 10:15:24 +0200382 ip6gre_tunnel_unlink(ign, t);
Paolo Abeni607f7252016-02-12 15:43:54 +0100383 dst_cache_reset(&t->dst_cache);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000384 dev_put(dev);
385}
386
387
388static void ip6gre_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Eric Dumazet78920322017-02-04 23:18:55 -0800389 u8 type, u8 code, int offset, __be32 info)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000390{
Xin Long929fc032017-11-11 19:06:49 +0800391 struct net *net = dev_net(skb->dev);
Eric Dumazet78920322017-02-04 23:18:55 -0800392 const struct gre_base_hdr *greh;
393 const struct ipv6hdr *ipv6h;
394 int grehlen = sizeof(*greh);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000395 struct ip6_tnl *t;
Eric Dumazet78920322017-02-04 23:18:55 -0800396 int key_off = 0;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000397 __be16 flags;
Eric Dumazet78920322017-02-04 23:18:55 -0800398 __be32 key;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000399
Eric Dumazet78920322017-02-04 23:18:55 -0800400 if (!pskb_may_pull(skb, offset + grehlen))
401 return;
402 greh = (const struct gre_base_hdr *)(skb->data + offset);
403 flags = greh->flags;
404 if (flags & (GRE_VERSION | GRE_ROUTING))
405 return;
406 if (flags & GRE_CSUM)
407 grehlen += 4;
408 if (flags & GRE_KEY) {
409 key_off = grehlen + offset;
410 grehlen += 4;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000411 }
412
Eric Dumazet78920322017-02-04 23:18:55 -0800413 if (!pskb_may_pull(skb, offset + grehlen))
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000414 return;
Eric Dumazetb87fb392012-08-19 03:47:30 +0000415 ipv6h = (const struct ipv6hdr *)skb->data;
Eric Dumazet78920322017-02-04 23:18:55 -0800416 greh = (const struct gre_base_hdr *)(skb->data + offset);
417 key = key_off ? *(__be32 *)(skb->data + key_off) : 0;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000418
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000419 t = ip6gre_tunnel_lookup(skb->dev, &ipv6h->daddr, &ipv6h->saddr,
Eric Dumazet78920322017-02-04 23:18:55 -0800420 key, greh->protocol);
Ian Morris63159f22015-03-29 14:00:04 +0100421 if (!t)
stephen hemminger0c5794a2012-09-24 18:12:24 +0000422 return;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000423
424 switch (type) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000425 struct ipv6_tlv_tnl_enc_lim *tel;
Xin Longfe1a4ca2017-11-11 19:06:50 +0800426 __u32 teli;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000427 case ICMPV6_DEST_UNREACH:
Matt Bennetta46496c2015-09-23 16:58:31 +1200428 net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n",
429 t->parms.name);
Xin Longf8d20b42017-10-26 19:23:27 +0800430 if (code != ICMPV6_PORT_UNREACH)
431 break;
432 return;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000433 case ICMPV6_TIME_EXCEED:
434 if (code == ICMPV6_EXC_HOPLIMIT) {
Matt Bennetta46496c2015-09-23 16:58:31 +1200435 net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
436 t->parms.name);
Xin Longf8d20b42017-10-26 19:23:27 +0800437 break;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000438 }
Xin Longf8d20b42017-10-26 19:23:27 +0800439 return;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000440 case ICMPV6_PARAMPROB:
441 teli = 0;
442 if (code == ICMPV6_HDR_FIELD)
443 teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
444
Sabrina Dubrocad1e158e2015-02-04 15:25:09 +0100445 if (teli && teli == be32_to_cpu(info) - 2) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000446 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
447 if (tel->encap_limit == 0) {
Matt Bennetta46496c2015-09-23 16:58:31 +1200448 net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
449 t->parms.name);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000450 }
451 } else {
Matt Bennetta46496c2015-09-23 16:58:31 +1200452 net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
453 t->parms.name);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000454 }
Xin Longf8d20b42017-10-26 19:23:27 +0800455 return;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000456 case ICMPV6_PKT_TOOBIG:
Xin Longfe1a4ca2017-11-11 19:06:50 +0800457 ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
Xin Longf8d20b42017-10-26 19:23:27 +0800458 return;
Xin Long929fc032017-11-11 19:06:49 +0800459 case NDISC_REDIRECT:
460 ip6_redirect(skb, net, skb->dev->ifindex, 0,
461 sock_net_uid(net, NULL));
462 return;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000463 }
464
465 if (time_before(jiffies, t->err_time + IP6TUNNEL_ERR_TIMEO))
466 t->err_count++;
467 else
468 t->err_count = 1;
469 t->err_time = jiffies;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000470}
471
Tom Herbert308edfdf2016-04-29 17:12:17 -0700472static int ip6gre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000473{
474 const struct ipv6hdr *ipv6h;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000475 struct ip6_tnl *tunnel;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000476
477 ipv6h = ipv6_hdr(skb);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000478 tunnel = ip6gre_tunnel_lookup(skb->dev,
Tom Herbert308edfdf2016-04-29 17:12:17 -0700479 &ipv6h->saddr, &ipv6h->daddr, tpi->key,
480 tpi->proto);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000481 if (tunnel) {
William Tu6712abc2017-12-01 15:26:08 -0800482 if (tunnel->parms.collect_md) {
483 struct metadata_dst *tun_dst;
484 __be64 tun_id;
485 __be16 flags;
486
487 flags = tpi->flags;
488 tun_id = key32_to_tunnel_id(tpi->key);
489
490 tun_dst = ipv6_tun_rx_dst(skb, flags, tun_id, 0);
491 if (!tun_dst)
492 return PACKET_REJECT;
493
494 ip6_tnl_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
495 } else {
496 ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error);
497 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000498
Tom Herbert308edfdf2016-04-29 17:12:17 -0700499 return PACKET_RCVD;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000500 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000501
Tom Herbert308edfdf2016-04-29 17:12:17 -0700502 return PACKET_REJECT;
503}
504
William Tu5a963eb2017-11-30 11:51:29 -0800505static int ip6erspan_rcv(struct sk_buff *skb, int gre_hdr_len,
506 struct tnl_ptk_info *tpi)
507{
William Tu1d7e2ed2017-12-13 16:38:55 -0800508 struct erspan_base_hdr *ershdr;
509 struct erspan_metadata *pkt_md;
William Tu5a963eb2017-11-30 11:51:29 -0800510 const struct ipv6hdr *ipv6h;
William Tu3df192832018-02-05 13:35:34 -0800511 struct erspan_md2 *md2;
William Tu5a963eb2017-11-30 11:51:29 -0800512 struct ip6_tnl *tunnel;
William Tu1d7e2ed2017-12-13 16:38:55 -0800513 u8 ver;
William Tu5a963eb2017-11-30 11:51:29 -0800514
William Tu5a963eb2017-11-30 11:51:29 -0800515 if (unlikely(!pskb_may_pull(skb, sizeof(*ershdr))))
516 return PACKET_REJECT;
517
Haishuang Yan293a1992017-12-20 09:53:19 +0800518 ipv6h = ipv6_hdr(skb);
519 ershdr = (struct erspan_base_hdr *)skb->data;
William Tuc69de582018-01-25 13:20:09 -0800520 ver = ershdr->ver;
521 tpi->key = cpu_to_be32(get_session_id(ershdr));
William Tu5a963eb2017-11-30 11:51:29 -0800522
523 tunnel = ip6gre_tunnel_lookup(skb->dev,
524 &ipv6h->saddr, &ipv6h->daddr, tpi->key,
525 tpi->proto);
526 if (tunnel) {
William Tu1d7e2ed2017-12-13 16:38:55 -0800527 int len = erspan_hdr_len(ver);
528
529 if (unlikely(!pskb_may_pull(skb, len)))
William Tuae3e1332017-12-15 14:27:43 -0800530 return PACKET_REJECT;
William Tu1d7e2ed2017-12-13 16:38:55 -0800531
William Tud91e8db52017-12-15 14:27:44 -0800532 ershdr = (struct erspan_base_hdr *)skb->data;
533 pkt_md = (struct erspan_metadata *)(ershdr + 1);
534
William Tu1d7e2ed2017-12-13 16:38:55 -0800535 if (__iptunnel_pull_header(skb, len,
William Tu5a963eb2017-11-30 11:51:29 -0800536 htons(ETH_P_TEB),
537 false, false) < 0)
538 return PACKET_REJECT;
539
William Tuef7baf52017-12-05 15:15:44 -0800540 if (tunnel->parms.collect_md) {
541 struct metadata_dst *tun_dst;
542 struct ip_tunnel_info *info;
543 struct erspan_metadata *md;
544 __be64 tun_id;
545 __be16 flags;
546
547 tpi->flags |= TUNNEL_KEY;
548 flags = tpi->flags;
549 tun_id = key32_to_tunnel_id(tpi->key);
550
551 tun_dst = ipv6_tun_rx_dst(skb, flags, tun_id,
552 sizeof(*md));
553 if (!tun_dst)
554 return PACKET_REJECT;
555
556 info = &tun_dst->u.tun_info;
557 md = ip_tunnel_info_opts(info);
William Tu94d7d8f2017-12-13 16:38:57 -0800558 md->version = ver;
William Tu3df192832018-02-05 13:35:34 -0800559 md2 = &md->u.md2;
560 memcpy(md2, pkt_md, ver == 1 ? ERSPAN_V1_MDSIZE :
561 ERSPAN_V2_MDSIZE);
William Tuef7baf52017-12-05 15:15:44 -0800562 info->key.tun_flags |= TUNNEL_ERSPAN_OPT;
563 info->options_len = sizeof(*md);
564
565 ip6_tnl_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
566
567 } else {
William Tuef7baf52017-12-05 15:15:44 -0800568 ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error);
569 }
William Tu5a963eb2017-11-30 11:51:29 -0800570
571 return PACKET_RCVD;
572 }
573
574 return PACKET_REJECT;
575}
576
Tom Herbert308edfdf2016-04-29 17:12:17 -0700577static int gre_rcv(struct sk_buff *skb)
578{
579 struct tnl_ptk_info tpi;
580 bool csum_err = false;
581 int hdr_len;
582
Eric Dumazete582615ad2016-06-15 06:24:00 -0700583 hdr_len = gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IPV6), 0);
Jiri Bencf132ae72016-05-03 15:00:21 +0200584 if (hdr_len < 0)
Tom Herbert308edfdf2016-04-29 17:12:17 -0700585 goto drop;
586
587 if (iptunnel_pull_header(skb, hdr_len, tpi.proto, false))
588 goto drop;
589
William Tu94d7d8f2017-12-13 16:38:57 -0800590 if (unlikely(tpi.proto == htons(ETH_P_ERSPAN) ||
591 tpi.proto == htons(ETH_P_ERSPAN2))) {
William Tu5a963eb2017-11-30 11:51:29 -0800592 if (ip6erspan_rcv(skb, hdr_len, &tpi) == PACKET_RCVD)
593 return 0;
Haishuang Yana7343212017-12-20 10:21:47 +0800594 goto out;
William Tu5a963eb2017-11-30 11:51:29 -0800595 }
596
Tom Herbert308edfdf2016-04-29 17:12:17 -0700597 if (ip6gre_rcv(skb, &tpi) == PACKET_RCVD)
598 return 0;
599
Haishuang Yana7343212017-12-20 10:21:47 +0800600out:
Tom Herbert308edfdf2016-04-29 17:12:17 -0700601 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000602drop:
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000603 kfree_skb(skb);
604 return 0;
605}
606
Tom Herbertb05229f2016-04-29 17:12:21 -0700607static int gre_handle_offloads(struct sk_buff *skb, bool csum)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000608{
Tom Herbertb05229f2016-04-29 17:12:21 -0700609 return iptunnel_handle_offloads(skb,
610 csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000611}
612
William Tu898b29792017-11-30 11:51:28 -0800613static void prepare_ip6gre_xmit_ipv4(struct sk_buff *skb,
614 struct net_device *dev,
615 struct flowi6 *fl6, __u8 *dsfield,
616 int *encap_limit)
617{
618 const struct iphdr *iph = ip_hdr(skb);
619 struct ip6_tnl *t = netdev_priv(dev);
620
621 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
622 *encap_limit = t->parms.encap_limit;
623
624 memcpy(fl6, &t->fl.u.ip6, sizeof(*fl6));
625
626 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
627 *dsfield = ipv4_get_dsfield(iph);
628 else
629 *dsfield = ip6_tclass(t->parms.flowinfo);
630
631 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
632 fl6->flowi6_mark = skb->mark;
633 else
634 fl6->flowi6_mark = t->parms.fwmark;
635
636 fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
637}
638
639static int prepare_ip6gre_xmit_ipv6(struct sk_buff *skb,
640 struct net_device *dev,
641 struct flowi6 *fl6, __u8 *dsfield,
642 int *encap_limit)
643{
644 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
645 struct ip6_tnl *t = netdev_priv(dev);
646 __u16 offset;
647
648 offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
649 /* ip6_tnl_parse_tlv_enc_lim() might have reallocated skb->head */
650
651 if (offset > 0) {
652 struct ipv6_tlv_tnl_enc_lim *tel;
653
654 tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
655 if (tel->encap_limit == 0) {
656 icmpv6_send(skb, ICMPV6_PARAMPROB,
657 ICMPV6_HDR_FIELD, offset + 2);
658 return -1;
659 }
660 *encap_limit = tel->encap_limit - 1;
661 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) {
662 *encap_limit = t->parms.encap_limit;
663 }
664
665 memcpy(fl6, &t->fl.u.ip6, sizeof(*fl6));
666
667 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
668 *dsfield = ipv6_get_dsfield(ipv6h);
669 else
670 *dsfield = ip6_tclass(t->parms.flowinfo);
671
672 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
673 fl6->flowlabel |= ip6_flowlabel(ipv6h);
674
675 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
676 fl6->flowi6_mark = skb->mark;
677 else
678 fl6->flowi6_mark = t->parms.fwmark;
679
680 fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
681
682 return 0;
683}
684
Tom Herbertb05229f2016-04-29 17:12:21 -0700685static netdev_tx_t __gre6_xmit(struct sk_buff *skb,
686 struct net_device *dev, __u8 dsfield,
687 struct flowi6 *fl6, int encap_limit,
688 __u32 *pmtu, __be16 proto)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000689{
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000690 struct ip6_tnl *tunnel = netdev_priv(dev);
Xin Long8aec4952017-10-26 19:27:17 +0800691 __be16 protocol;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000692
693 if (dev->type == ARPHRD_ETHER)
694 IPCB(skb)->flags = 0;
695
Tom Herbertb05229f2016-04-29 17:12:21 -0700696 if (dev->header_ops && dev->type == ARPHRD_IP6GRE)
697 fl6->daddr = ((struct ipv6hdr *)skb->data)->daddr;
698 else
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000699 fl6->daddr = tunnel->parms.raddr;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000700
Tom Herbertb05229f2016-04-29 17:12:21 -0700701 /* Push GRE header. */
Xin Long8aec4952017-10-26 19:27:17 +0800702 protocol = (dev->type == ARPHRD_ETHER) ? htons(ETH_P_TEB) : proto;
William Tu6712abc2017-12-01 15:26:08 -0800703
704 if (tunnel->parms.collect_md) {
705 struct ip_tunnel_info *tun_info;
706 const struct ip_tunnel_key *key;
707 __be16 flags;
708
709 tun_info = skb_tunnel_info(skb);
710 if (unlikely(!tun_info ||
711 !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
712 ip_tunnel_info_af(tun_info) != AF_INET6))
713 return -EINVAL;
714
715 key = &tun_info->key;
716 memset(fl6, 0, sizeof(*fl6));
717 fl6->flowi6_proto = IPPROTO_GRE;
718 fl6->daddr = key->u.ipv6.dst;
719 fl6->flowlabel = key->label;
720 fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
721
722 dsfield = key->tos;
William Tu77a51962018-03-01 13:49:57 -0800723 flags = key->tun_flags &
724 (TUNNEL_CSUM | TUNNEL_KEY | TUNNEL_SEQ);
William Tu6712abc2017-12-01 15:26:08 -0800725 tunnel->tun_hlen = gre_calc_hlen(flags);
726
727 gre_build_header(skb, tunnel->tun_hlen,
728 flags, protocol,
William Tu77a51962018-03-01 13:49:57 -0800729 tunnel_id_to_key32(tun_info->key.tun_id),
Colin Ian King15746392018-03-21 19:34:58 +0000730 (flags & TUNNEL_SEQ) ? htonl(tunnel->o_seqno++)
William Tu77a51962018-03-01 13:49:57 -0800731 : 0);
William Tu6712abc2017-12-01 15:26:08 -0800732
733 } else {
William Tu77a51962018-03-01 13:49:57 -0800734 if (tunnel->parms.o_flags & TUNNEL_SEQ)
735 tunnel->o_seqno++;
736
William Tu6712abc2017-12-01 15:26:08 -0800737 gre_build_header(skb, tunnel->tun_hlen, tunnel->parms.o_flags,
738 protocol, tunnel->parms.o_key,
739 htonl(tunnel->o_seqno));
740 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000741
Tom Herbertb05229f2016-04-29 17:12:21 -0700742 return ip6_tnl_xmit(skb, dev, dsfield, fl6, encap_limit, pmtu,
743 NEXTHDR_GRE);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000744}
745
746static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev)
747{
748 struct ip6_tnl *t = netdev_priv(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000749 int encap_limit = -1;
750 struct flowi6 fl6;
William Tu6712abc2017-12-01 15:26:08 -0800751 __u8 dsfield = 0;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000752 __u32 mtu;
753 int err;
754
Bernie Harris5146d1f2016-02-22 12:58:05 +1300755 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
756
William Tu6712abc2017-12-01 15:26:08 -0800757 if (!t->parms.collect_md)
758 prepare_ip6gre_xmit_ipv4(skb, dev, &fl6,
759 &dsfield, &encap_limit);
Lorenzo Colittie2d118a2016-11-04 02:23:43 +0900760
Tom Herbertb05229f2016-04-29 17:12:21 -0700761 err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
762 if (err)
763 return -1;
764
765 err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
766 skb->protocol);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000767 if (err != 0) {
768 /* XXX: send ICMP error even if DF is not set. */
769 if (err == -EMSGSIZE)
770 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
771 htonl(mtu));
772 return -1;
773 }
774
775 return 0;
776}
777
778static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev)
779{
780 struct ip6_tnl *t = netdev_priv(dev);
781 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
782 int encap_limit = -1;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000783 struct flowi6 fl6;
William Tu6712abc2017-12-01 15:26:08 -0800784 __u8 dsfield = 0;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000785 __u32 mtu;
786 int err;
787
788 if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
789 return -1;
790
William Tu6712abc2017-12-01 15:26:08 -0800791 if (!t->parms.collect_md &&
792 prepare_ip6gre_xmit_ipv6(skb, dev, &fl6, &dsfield, &encap_limit))
William Tu898b29792017-11-30 11:51:28 -0800793 return -1;
Lorenzo Colittie2d118a2016-11-04 02:23:43 +0900794
Tom Herbertb05229f2016-04-29 17:12:21 -0700795 if (gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM)))
796 return -1;
797
798 err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit,
799 &mtu, skb->protocol);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000800 if (err != 0) {
801 if (err == -EMSGSIZE)
802 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
803 return -1;
804 }
805
806 return 0;
807}
808
809/**
810 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
811 * @t: the outgoing tunnel device
812 * @hdr: IPv6 header from the incoming packet
813 *
814 * Description:
815 * Avoid trivial tunneling loop by checking that tunnel exit-point
816 * doesn't match source of incoming packet.
817 *
818 * Return:
819 * 1 if conflict,
820 * 0 else
821 **/
822
823static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl *t,
824 const struct ipv6hdr *hdr)
825{
826 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
827}
828
829static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev)
830{
831 struct ip6_tnl *t = netdev_priv(dev);
832 int encap_limit = -1;
833 struct flowi6 fl6;
834 __u32 mtu;
835 int err;
836
837 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
838 encap_limit = t->parms.encap_limit;
839
William Tu6712abc2017-12-01 15:26:08 -0800840 if (!t->parms.collect_md)
841 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000842
Tom Herbertb05229f2016-04-29 17:12:21 -0700843 err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
844 if (err)
845 return err;
846
847 err = __gre6_xmit(skb, dev, 0, &fl6, encap_limit, &mtu, skb->protocol);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000848
849 return err;
850}
851
852static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb,
853 struct net_device *dev)
854{
855 struct ip6_tnl *t = netdev_priv(dev);
856 struct net_device_stats *stats = &t->dev->stats;
857 int ret;
858
Steffen Klassertd5005142014-11-05 08:02:48 +0100859 if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
Tommi Rantala41ab3e32013-02-06 03:24:02 +0000860 goto tx_err;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000861
862 switch (skb->protocol) {
863 case htons(ETH_P_IP):
864 ret = ip6gre_xmit_ipv4(skb, dev);
865 break;
866 case htons(ETH_P_IPV6):
867 ret = ip6gre_xmit_ipv6(skb, dev);
868 break;
869 default:
870 ret = ip6gre_xmit_other(skb, dev);
871 break;
872 }
873
874 if (ret < 0)
875 goto tx_err;
876
877 return NETDEV_TX_OK;
878
879tx_err:
880 stats->tx_errors++;
881 stats->tx_dropped++;
882 kfree_skb(skb);
883 return NETDEV_TX_OK;
884}
885
William Tu5a963eb2017-11-30 11:51:29 -0800886static netdev_tx_t ip6erspan_tunnel_xmit(struct sk_buff *skb,
887 struct net_device *dev)
888{
889 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
890 struct ip6_tnl *t = netdev_priv(dev);
891 struct dst_entry *dst = skb_dst(skb);
892 struct net_device_stats *stats;
893 bool truncate = false;
894 int encap_limit = -1;
895 __u8 dsfield = false;
896 struct flowi6 fl6;
897 int err = -EINVAL;
898 __u32 mtu;
899
900 if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
901 goto tx_err;
902
903 if (gre_handle_offloads(skb, false))
904 goto tx_err;
905
William Tu5a963eb2017-11-30 11:51:29 -0800906 if (skb->len > dev->mtu + dev->hard_header_len) {
907 pskb_trim(skb, dev->mtu + dev->hard_header_len);
908 truncate = true;
909 }
910
William Tue41c7c62018-03-09 07:34:42 -0800911 if (skb_cow_head(skb, dev->needed_headroom))
912 goto tx_err;
913
William Tu5a963eb2017-11-30 11:51:29 -0800914 t->parms.o_flags &= ~TUNNEL_KEY;
William Tu5a963eb2017-11-30 11:51:29 -0800915 IPCB(skb)->flags = 0;
William Tuef7baf52017-12-05 15:15:44 -0800916
917 /* For collect_md mode, derive fl6 from the tunnel key,
918 * for native mode, call prepare_ip6gre_xmit_{ipv4,ipv6}.
919 */
920 if (t->parms.collect_md) {
921 struct ip_tunnel_info *tun_info;
922 const struct ip_tunnel_key *key;
923 struct erspan_metadata *md;
William Tuc69de582018-01-25 13:20:09 -0800924 __be32 tun_id;
William Tuef7baf52017-12-05 15:15:44 -0800925
926 tun_info = skb_tunnel_info(skb);
927 if (unlikely(!tun_info ||
928 !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
929 ip_tunnel_info_af(tun_info) != AF_INET6))
930 return -EINVAL;
931
932 key = &tun_info->key;
933 memset(&fl6, 0, sizeof(fl6));
934 fl6.flowi6_proto = IPPROTO_GRE;
935 fl6.daddr = key->u.ipv6.dst;
936 fl6.flowlabel = key->label;
937 fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
938
939 dsfield = key->tos;
940 md = ip_tunnel_info_opts(tun_info);
941 if (!md)
942 goto tx_err;
943
William Tuc69de582018-01-25 13:20:09 -0800944 tun_id = tunnel_id_to_key32(key->tun_id);
William Tu94d7d8f2017-12-13 16:38:57 -0800945 if (md->version == 1) {
946 erspan_build_header(skb,
William Tuc69de582018-01-25 13:20:09 -0800947 ntohl(tun_id),
William Tu94d7d8f2017-12-13 16:38:57 -0800948 ntohl(md->u.index), truncate,
949 false);
950 } else if (md->version == 2) {
William Tu94d7d8f2017-12-13 16:38:57 -0800951 erspan_build_header_v2(skb,
William Tuc69de582018-01-25 13:20:09 -0800952 ntohl(tun_id),
953 md->u.md2.dir,
954 get_hwid(&md->u.md2),
955 truncate, false);
William Tud6aa7112018-03-09 07:34:41 -0800956 } else {
957 goto tx_err;
William Tu94d7d8f2017-12-13 16:38:57 -0800958 }
William Tuef7baf52017-12-05 15:15:44 -0800959 } else {
960 switch (skb->protocol) {
961 case htons(ETH_P_IP):
962 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
963 prepare_ip6gre_xmit_ipv4(skb, dev, &fl6,
964 &dsfield, &encap_limit);
965 break;
966 case htons(ETH_P_IPV6):
967 if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
968 goto tx_err;
969 if (prepare_ip6gre_xmit_ipv6(skb, dev, &fl6,
970 &dsfield, &encap_limit))
971 goto tx_err;
972 break;
973 default:
974 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
975 break;
976 }
977
William Tu94d7d8f2017-12-13 16:38:57 -0800978 if (t->parms.erspan_ver == 1)
William Tuc69de582018-01-25 13:20:09 -0800979 erspan_build_header(skb, ntohl(t->parms.o_key),
William Tu94d7d8f2017-12-13 16:38:57 -0800980 t->parms.index,
981 truncate, false);
William Tu02f99df2018-05-16 17:24:32 -0700982 else if (t->parms.erspan_ver == 2)
William Tuc69de582018-01-25 13:20:09 -0800983 erspan_build_header_v2(skb, ntohl(t->parms.o_key),
William Tu94d7d8f2017-12-13 16:38:57 -0800984 t->parms.dir,
985 t->parms.hwid,
986 truncate, false);
William Tu02f99df2018-05-16 17:24:32 -0700987 else
988 goto tx_err;
989
William Tuef7baf52017-12-05 15:15:44 -0800990 fl6.daddr = t->parms.raddr;
991 }
William Tu5a963eb2017-11-30 11:51:29 -0800992
993 /* Push GRE header. */
994 gre_build_header(skb, 8, TUNNEL_SEQ,
995 htons(ETH_P_ERSPAN), 0, htonl(t->o_seqno++));
996
997 /* TooBig packet may have updated dst->dev's mtu */
William Tuef7baf52017-12-05 15:15:44 -0800998 if (!t->parms.collect_md && dst && dst_mtu(dst) > dst->dev->mtu)
William Tu5a963eb2017-11-30 11:51:29 -0800999 dst->ops->update_pmtu(dst, NULL, skb, dst->dev->mtu);
1000
1001 err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
1002 NEXTHDR_GRE);
1003 if (err != 0) {
1004 /* XXX: send ICMP error even if DF is not set. */
1005 if (err == -EMSGSIZE) {
1006 if (skb->protocol == htons(ETH_P_IP))
1007 icmp_send(skb, ICMP_DEST_UNREACH,
1008 ICMP_FRAG_NEEDED, htonl(mtu));
1009 else
1010 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1011 }
1012
1013 goto tx_err;
1014 }
1015 return NETDEV_TX_OK;
1016
1017tx_err:
1018 stats = &t->dev->stats;
1019 stats->tx_errors++;
1020 stats->tx_dropped++;
1021 kfree_skb(skb);
1022 return NETDEV_TX_OK;
1023}
1024
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001025static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu)
1026{
1027 struct net_device *dev = t->dev;
1028 struct __ip6_tnl_parm *p = &t->parms;
1029 struct flowi6 *fl6 = &t->fl.u.ip6;
Tom Herbertdb2ec952016-05-09 17:12:08 -07001030 int t_hlen;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001031
1032 if (dev->type != ARPHRD_ETHER) {
1033 memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
1034 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
1035 }
1036
1037 /* Set up flowi template */
1038 fl6->saddr = p->laddr;
1039 fl6->daddr = p->raddr;
1040 fl6->flowi6_oif = p->link;
1041 fl6->flowlabel = 0;
Haishuang Yan252f3f52016-05-21 18:17:35 +08001042 fl6->flowi6_proto = IPPROTO_GRE;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001043
1044 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
1045 fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
1046 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
1047 fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
1048
1049 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
1050 p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
1051
1052 if (p->flags&IP6_TNL_F_CAP_XMIT &&
1053 p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER)
1054 dev->flags |= IFF_POINTOPOINT;
1055 else
1056 dev->flags &= ~IFF_POINTOPOINT;
1057
Tom Herbertdb2ec952016-05-09 17:12:08 -07001058 t->tun_hlen = gre_calc_hlen(t->parms.o_flags);
1059
Tom Herbert1faf3d92016-05-18 09:06:19 -07001060 t->hlen = t->encap_hlen + t->tun_hlen;
Tom Herbertdb2ec952016-05-09 17:12:08 -07001061
1062 t_hlen = t->hlen + sizeof(struct ipv6hdr);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001063
1064 if (p->flags & IP6_TNL_F_CAP_XMIT) {
1065 int strict = (ipv6_addr_type(&p->raddr) &
1066 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
1067
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001068 struct rt6_info *rt = rt6_lookup(t->net,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001069 &p->raddr, &p->laddr,
David Ahernb75cc8f2018-03-02 08:32:17 -08001070 p->link, NULL, strict);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001071
Ian Morris63159f22015-03-29 14:00:04 +01001072 if (!rt)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001073 return;
1074
1075 if (rt->dst.dev) {
Tom Herbertdb2ec952016-05-09 17:12:08 -07001076 dev->hard_header_len = rt->dst.dev->hard_header_len +
1077 t_hlen;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001078
1079 if (set_mtu) {
Tom Herbertdb2ec952016-05-09 17:12:08 -07001080 dev->mtu = rt->dst.dev->mtu - t_hlen;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001081 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1082 dev->mtu -= 8;
Alexander Duycka9e242c2016-04-14 15:33:45 -04001083 if (dev->type == ARPHRD_ETHER)
1084 dev->mtu -= ETH_HLEN;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001085
1086 if (dev->mtu < IPV6_MIN_MTU)
1087 dev->mtu = IPV6_MIN_MTU;
1088 }
1089 }
Amerigo Wang94e187c2012-10-29 00:13:19 +00001090 ip6_rt_put(rt);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001091 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001092}
1093
1094static int ip6gre_tnl_change(struct ip6_tnl *t,
1095 const struct __ip6_tnl_parm *p, int set_mtu)
1096{
1097 t->parms.laddr = p->laddr;
1098 t->parms.raddr = p->raddr;
1099 t->parms.flags = p->flags;
1100 t->parms.hop_limit = p->hop_limit;
1101 t->parms.encap_limit = p->encap_limit;
1102 t->parms.flowinfo = p->flowinfo;
1103 t->parms.link = p->link;
1104 t->parms.proto = p->proto;
1105 t->parms.i_key = p->i_key;
1106 t->parms.o_key = p->o_key;
1107 t->parms.i_flags = p->i_flags;
1108 t->parms.o_flags = p->o_flags;
Craig Gallek0a473b82017-04-19 12:30:53 -04001109 t->parms.fwmark = p->fwmark;
Paolo Abeni607f7252016-02-12 15:43:54 +01001110 dst_cache_reset(&t->dst_cache);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001111 ip6gre_tnl_link_config(t, set_mtu);
1112 return 0;
1113}
1114
1115static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p,
1116 const struct ip6_tnl_parm2 *u)
1117{
1118 p->laddr = u->laddr;
1119 p->raddr = u->raddr;
1120 p->flags = u->flags;
1121 p->hop_limit = u->hop_limit;
1122 p->encap_limit = u->encap_limit;
1123 p->flowinfo = u->flowinfo;
1124 p->link = u->link;
1125 p->i_key = u->i_key;
1126 p->o_key = u->o_key;
Tom Herbertf41fe3c2016-05-09 17:12:09 -07001127 p->i_flags = gre_flags_to_tnl_flags(u->i_flags);
1128 p->o_flags = gre_flags_to_tnl_flags(u->o_flags);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001129 memcpy(p->name, u->name, sizeof(u->name));
1130}
1131
1132static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u,
1133 const struct __ip6_tnl_parm *p)
1134{
1135 u->proto = IPPROTO_GRE;
1136 u->laddr = p->laddr;
1137 u->raddr = p->raddr;
1138 u->flags = p->flags;
1139 u->hop_limit = p->hop_limit;
1140 u->encap_limit = p->encap_limit;
1141 u->flowinfo = p->flowinfo;
1142 u->link = p->link;
1143 u->i_key = p->i_key;
1144 u->o_key = p->o_key;
Tom Herbertf41fe3c2016-05-09 17:12:09 -07001145 u->i_flags = gre_tnl_flags_to_gre_flags(p->i_flags);
1146 u->o_flags = gre_tnl_flags_to_gre_flags(p->o_flags);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001147 memcpy(u->name, p->name, sizeof(u->name));
1148}
1149
1150static int ip6gre_tunnel_ioctl(struct net_device *dev,
1151 struct ifreq *ifr, int cmd)
1152{
1153 int err = 0;
1154 struct ip6_tnl_parm2 p;
1155 struct __ip6_tnl_parm p1;
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001156 struct ip6_tnl *t = netdev_priv(dev);
1157 struct net *net = t->net;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001158 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1159
Tom Herbert308edfdf2016-04-29 17:12:17 -07001160 memset(&p1, 0, sizeof(p1));
1161
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001162 switch (cmd) {
1163 case SIOCGETTUNNEL:
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001164 if (dev == ign->fb_tunnel_dev) {
1165 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
1166 err = -EFAULT;
1167 break;
1168 }
1169 ip6gre_tnl_parm_from_user(&p1, &p);
1170 t = ip6gre_tunnel_locate(net, &p1, 0);
Ian Morris63159f22015-03-29 14:00:04 +01001171 if (!t)
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001172 t = netdev_priv(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001173 }
Amerigo Wang5dbd5062013-05-09 21:56:37 +00001174 memset(&p, 0, sizeof(p));
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001175 ip6gre_tnl_parm_to_user(&p, &t->parms);
1176 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
1177 err = -EFAULT;
1178 break;
1179
1180 case SIOCADDTUNNEL:
1181 case SIOCCHGTUNNEL:
1182 err = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00001183 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001184 goto done;
1185
1186 err = -EFAULT;
1187 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1188 goto done;
1189
1190 err = -EINVAL;
1191 if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))
1192 goto done;
1193
1194 if (!(p.i_flags&GRE_KEY))
1195 p.i_key = 0;
1196 if (!(p.o_flags&GRE_KEY))
1197 p.o_key = 0;
1198
1199 ip6gre_tnl_parm_from_user(&p1, &p);
1200 t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL);
1201
1202 if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
Ian Morris53b24b82015-03-29 14:00:05 +01001203 if (t) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001204 if (t->dev != dev) {
1205 err = -EEXIST;
1206 break;
1207 }
1208 } else {
1209 t = netdev_priv(dev);
1210
1211 ip6gre_tunnel_unlink(ign, t);
1212 synchronize_net();
1213 ip6gre_tnl_change(t, &p1, 1);
1214 ip6gre_tunnel_link(ign, t);
1215 netdev_state_change(dev);
1216 }
1217 }
1218
1219 if (t) {
1220 err = 0;
1221
Amerigo Wang5dbd5062013-05-09 21:56:37 +00001222 memset(&p, 0, sizeof(p));
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001223 ip6gre_tnl_parm_to_user(&p, &t->parms);
1224 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
1225 err = -EFAULT;
1226 } else
1227 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
1228 break;
1229
1230 case SIOCDELTUNNEL:
1231 err = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00001232 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001233 goto done;
1234
1235 if (dev == ign->fb_tunnel_dev) {
1236 err = -EFAULT;
1237 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1238 goto done;
1239 err = -ENOENT;
1240 ip6gre_tnl_parm_from_user(&p1, &p);
1241 t = ip6gre_tunnel_locate(net, &p1, 0);
Ian Morris63159f22015-03-29 14:00:04 +01001242 if (!t)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001243 goto done;
1244 err = -EPERM;
1245 if (t == netdev_priv(ign->fb_tunnel_dev))
1246 goto done;
1247 dev = t->dev;
1248 }
1249 unregister_netdevice(dev);
1250 err = 0;
1251 break;
1252
1253 default:
1254 err = -EINVAL;
1255 }
1256
1257done:
1258 return err;
1259}
1260
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001261static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
Xin Long76cc0d32017-09-15 12:00:07 +08001262 unsigned short type, const void *daddr,
1263 const void *saddr, unsigned int len)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001264{
1265 struct ip6_tnl *t = netdev_priv(dev);
Xin Long76cc0d32017-09-15 12:00:07 +08001266 struct ipv6hdr *ipv6h;
1267 __be16 *p;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001268
Xin Long76cc0d32017-09-15 12:00:07 +08001269 ipv6h = skb_push(skb, t->hlen + sizeof(*ipv6h));
1270 ip6_flow_hdr(ipv6h, 0, ip6_make_flowlabel(dev_net(dev), skb,
1271 t->fl.u.ip6.flowlabel,
1272 true, &t->fl.u.ip6));
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001273 ipv6h->hop_limit = t->parms.hop_limit;
1274 ipv6h->nexthdr = NEXTHDR_GRE;
1275 ipv6h->saddr = t->parms.laddr;
1276 ipv6h->daddr = t->parms.raddr;
1277
Xin Long76cc0d32017-09-15 12:00:07 +08001278 p = (__be16 *)(ipv6h + 1);
1279 p[0] = t->parms.o_flags;
1280 p[1] = htons(type);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001281
1282 /*
1283 * Set the source hardware address.
1284 */
1285
1286 if (saddr)
1287 memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr));
1288 if (daddr)
1289 memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
1290 if (!ipv6_addr_any(&ipv6h->daddr))
1291 return t->hlen;
1292
1293 return -t->hlen;
1294}
1295
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001296static const struct header_ops ip6gre_header_ops = {
1297 .create = ip6gre_header,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001298};
1299
1300static const struct net_device_ops ip6gre_netdev_ops = {
1301 .ndo_init = ip6gre_tunnel_init,
1302 .ndo_uninit = ip6gre_tunnel_uninit,
1303 .ndo_start_xmit = ip6gre_tunnel_xmit,
1304 .ndo_do_ioctl = ip6gre_tunnel_ioctl,
Tom Herbertb05229f2016-04-29 17:12:21 -07001305 .ndo_change_mtu = ip6_tnl_change_mtu,
Pravin B Shelarf61dd382013-03-25 14:50:00 +00001306 .ndo_get_stats64 = ip_tunnel_get_stats64,
Nicolas Dichtelecf2c062015-04-02 17:07:01 +02001307 .ndo_get_iflink = ip6_tnl_get_iflink,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001308};
1309
1310static void ip6gre_dev_free(struct net_device *dev)
1311{
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001312 struct ip6_tnl *t = netdev_priv(dev);
1313
Paolo Abeni607f7252016-02-12 15:43:54 +01001314 dst_cache_destroy(&t->dst_cache);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001315 free_percpu(dev->tstats);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001316}
1317
1318static void ip6gre_tunnel_setup(struct net_device *dev)
1319{
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001320 dev->netdev_ops = &ip6gre_netdev_ops;
David S. Millercf124db2017-05-08 12:52:56 -04001321 dev->needs_free_netdev = true;
1322 dev->priv_destructor = ip6gre_dev_free;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001323
1324 dev->type = ARPHRD_IP6GRE;
Tom Herbertb05229f2016-04-29 17:12:21 -07001325
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001326 dev->flags |= IFF_NOARP;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001327 dev->addr_len = sizeof(struct in6_addr);
Eric Dumazet02875872014-10-05 18:38:35 -07001328 netif_keep_dst(dev);
Felix Jia45ce0fd2017-01-26 16:59:18 +13001329 /* This perm addr will be used as interface identifier by IPv6 */
1330 dev->addr_assign_type = NET_ADDR_RANDOM;
1331 eth_random_addr(dev->perm_addr);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001332}
1333
Alexey Kodaneve5a93362017-12-20 19:36:03 +03001334#define GRE6_FEATURES (NETIF_F_SG | \
1335 NETIF_F_FRAGLIST | \
1336 NETIF_F_HIGHDMA | \
1337 NETIF_F_HW_CSUM)
1338
1339static void ip6gre_tnl_init_features(struct net_device *dev)
1340{
1341 struct ip6_tnl *nt = netdev_priv(dev);
1342
1343 dev->features |= GRE6_FEATURES;
1344 dev->hw_features |= GRE6_FEATURES;
1345
1346 if (!(nt->parms.o_flags & TUNNEL_SEQ)) {
1347 /* TCP offload with GRE SEQ is not supported, nor
1348 * can we support 2 levels of outer headers requiring
1349 * an update.
1350 */
1351 if (!(nt->parms.o_flags & TUNNEL_CSUM) ||
1352 nt->encap.type == TUNNEL_ENCAP_NONE) {
1353 dev->features |= NETIF_F_GSO_SOFTWARE;
1354 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
1355 }
1356
1357 /* Can use a lockless transmit, unless we generate
1358 * output sequences
1359 */
1360 dev->features |= NETIF_F_LLTX;
1361 }
1362}
1363
Martin KaFai Laua3c119d2015-09-15 14:30:05 -07001364static int ip6gre_tunnel_init_common(struct net_device *dev)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001365{
1366 struct ip6_tnl *tunnel;
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001367 int ret;
Tom Herbertb05229f2016-04-29 17:12:21 -07001368 int t_hlen;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001369
1370 tunnel = netdev_priv(dev);
1371
1372 tunnel->dev = dev;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001373 tunnel->net = dev_net(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001374 strcpy(tunnel->parms.name, dev->name);
1375
Martin KaFai Laua3c119d2015-09-15 14:30:05 -07001376 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1377 if (!dev->tstats)
1378 return -ENOMEM;
1379
Paolo Abeni607f7252016-02-12 15:43:54 +01001380 ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001381 if (ret) {
1382 free_percpu(dev->tstats);
1383 dev->tstats = NULL;
1384 return ret;
1385 }
1386
Tom Herbertb05229f2016-04-29 17:12:21 -07001387 tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
Tom Herbert1faf3d92016-05-18 09:06:19 -07001388 tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen;
Tom Herbertb05229f2016-04-29 17:12:21 -07001389 t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
1390
Tom Herbertdb2ec952016-05-09 17:12:08 -07001391 dev->hard_header_len = LL_MAX_HEADER + t_hlen;
1392 dev->mtu = ETH_DATA_LEN - t_hlen;
Haishuang Yan1b227e52016-05-21 18:17:34 +08001393 if (dev->type == ARPHRD_ETHER)
1394 dev->mtu -= ETH_HLEN;
Tom Herbertb05229f2016-04-29 17:12:21 -07001395 if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1396 dev->mtu -= 8;
1397
William Tu6712abc2017-12-01 15:26:08 -08001398 if (tunnel->parms.collect_md) {
1399 dev->features |= NETIF_F_NETNS_LOCAL;
1400 netif_keep_dst(dev);
1401 }
Alexey Kodaneve5a93362017-12-20 19:36:03 +03001402 ip6gre_tnl_init_features(dev);
William Tu6712abc2017-12-01 15:26:08 -08001403
Martin KaFai Laua3c119d2015-09-15 14:30:05 -07001404 return 0;
1405}
1406
1407static int ip6gre_tunnel_init(struct net_device *dev)
1408{
1409 struct ip6_tnl *tunnel;
1410 int ret;
1411
1412 ret = ip6gre_tunnel_init_common(dev);
1413 if (ret)
1414 return ret;
1415
1416 tunnel = netdev_priv(dev);
1417
William Tu6712abc2017-12-01 15:26:08 -08001418 if (tunnel->parms.collect_md)
1419 return 0;
1420
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001421 memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr));
1422 memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr));
1423
1424 if (ipv6_addr_any(&tunnel->parms.raddr))
1425 dev->header_ops = &ip6gre_header_ops;
1426
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001427 return 0;
1428}
1429
1430static void ip6gre_fb_tunnel_init(struct net_device *dev)
1431{
1432 struct ip6_tnl *tunnel = netdev_priv(dev);
1433
1434 tunnel->dev = dev;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001435 tunnel->net = dev_net(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001436 strcpy(tunnel->parms.name, dev->name);
1437
1438 tunnel->hlen = sizeof(struct ipv6hdr) + 4;
1439
1440 dev_hold(dev);
1441}
1442
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001443static struct inet6_protocol ip6gre_protocol __read_mostly = {
Tom Herbert308edfdf2016-04-29 17:12:17 -07001444 .handler = gre_rcv,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001445 .err_handler = ip6gre_err,
1446 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1447};
1448
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001449static void ip6gre_destroy_tunnels(struct net *net, struct list_head *head)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001450{
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001451 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1452 struct net_device *dev, *aux;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001453 int prio;
1454
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001455 for_each_netdev_safe(net, dev, aux)
1456 if (dev->rtnl_link_ops == &ip6gre_link_ops ||
William Tu5a963eb2017-11-30 11:51:29 -08001457 dev->rtnl_link_ops == &ip6gre_tap_ops ||
1458 dev->rtnl_link_ops == &ip6erspan_tap_ops)
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001459 unregister_netdevice_queue(dev, head);
1460
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001461 for (prio = 0; prio < 4; prio++) {
1462 int h;
Jiri Kosinae87a8f22016-08-10 11:03:35 +02001463 for (h = 0; h < IP6_GRE_HASH_SIZE; h++) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001464 struct ip6_tnl *t;
1465
1466 t = rtnl_dereference(ign->tunnels[prio][h]);
1467
Ian Morris53b24b82015-03-29 14:00:05 +01001468 while (t) {
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001469 /* If dev is in the same netns, it has already
1470 * been added to the list by the previous loop.
1471 */
1472 if (!net_eq(dev_net(t->dev), net))
1473 unregister_netdevice_queue(t->dev,
1474 head);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001475 t = rtnl_dereference(t->next);
1476 }
1477 }
1478 }
1479}
1480
1481static int __net_init ip6gre_init_net(struct net *net)
1482{
1483 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1484 int err;
1485
Eric Dumazet79134e62018-03-08 12:51:41 -08001486 if (!net_has_fallback_tunnels(net))
1487 return 0;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001488 ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0",
Tom Gundersenc835a672014-07-14 16:37:24 +02001489 NET_NAME_UNKNOWN,
1490 ip6gre_tunnel_setup);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001491 if (!ign->fb_tunnel_dev) {
1492 err = -ENOMEM;
1493 goto err_alloc_dev;
1494 }
1495 dev_net_set(ign->fb_tunnel_dev, net);
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001496 /* FB netdevice is special: we have one, and only one per netns.
1497 * Allowing to move it to another netns is clearly unsafe.
1498 */
1499 ign->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL;
1500
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001501
1502 ip6gre_fb_tunnel_init(ign->fb_tunnel_dev);
1503 ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops;
1504
1505 err = register_netdev(ign->fb_tunnel_dev);
1506 if (err)
1507 goto err_reg_dev;
1508
1509 rcu_assign_pointer(ign->tunnels_wc[0],
1510 netdev_priv(ign->fb_tunnel_dev));
1511 return 0;
1512
1513err_reg_dev:
David S. Millercf124db2017-05-08 12:52:56 -04001514 free_netdev(ign->fb_tunnel_dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001515err_alloc_dev:
1516 return err;
1517}
1518
Eric Dumazetbb401ca2017-09-19 16:27:08 -07001519static void __net_exit ip6gre_exit_batch_net(struct list_head *net_list)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001520{
Eric Dumazetbb401ca2017-09-19 16:27:08 -07001521 struct net *net;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001522 LIST_HEAD(list);
1523
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001524 rtnl_lock();
Eric Dumazetbb401ca2017-09-19 16:27:08 -07001525 list_for_each_entry(net, net_list, exit_list)
1526 ip6gre_destroy_tunnels(net, &list);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001527 unregister_netdevice_many(&list);
1528 rtnl_unlock();
1529}
1530
1531static struct pernet_operations ip6gre_net_ops = {
1532 .init = ip6gre_init_net,
Eric Dumazetbb401ca2017-09-19 16:27:08 -07001533 .exit_batch = ip6gre_exit_batch_net,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001534 .id = &ip6gre_net_id,
1535 .size = sizeof(struct ip6gre_net),
1536};
1537
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001538static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[],
1539 struct netlink_ext_ack *extack)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001540{
1541 __be16 flags;
1542
1543 if (!data)
1544 return 0;
1545
1546 flags = 0;
1547 if (data[IFLA_GRE_IFLAGS])
1548 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1549 if (data[IFLA_GRE_OFLAGS])
1550 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1551 if (flags & (GRE_VERSION|GRE_ROUTING))
1552 return -EINVAL;
1553
1554 return 0;
1555}
1556
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001557static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[],
1558 struct netlink_ext_ack *extack)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001559{
1560 struct in6_addr daddr;
1561
1562 if (tb[IFLA_ADDRESS]) {
1563 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1564 return -EINVAL;
1565 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1566 return -EADDRNOTAVAIL;
1567 }
1568
1569 if (!data)
1570 goto out;
1571
1572 if (data[IFLA_GRE_REMOTE]) {
Jiri Benc67b61f62015-03-29 16:59:26 +02001573 daddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001574 if (ipv6_addr_any(&daddr))
1575 return -EINVAL;
1576 }
1577
1578out:
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001579 return ip6gre_tunnel_validate(tb, data, extack);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001580}
1581
William Tu5a963eb2017-11-30 11:51:29 -08001582static int ip6erspan_tap_validate(struct nlattr *tb[], struct nlattr *data[],
1583 struct netlink_ext_ack *extack)
1584{
1585 __be16 flags = 0;
William Tu94d7d8f2017-12-13 16:38:57 -08001586 int ret, ver = 0;
William Tu5a963eb2017-11-30 11:51:29 -08001587
1588 if (!data)
1589 return 0;
1590
1591 ret = ip6gre_tap_validate(tb, data, extack);
1592 if (ret)
1593 return ret;
1594
1595 /* ERSPAN should only have GRE sequence and key flag */
1596 if (data[IFLA_GRE_OFLAGS])
1597 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1598 if (data[IFLA_GRE_IFLAGS])
1599 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1600 if (!data[IFLA_GRE_COLLECT_METADATA] &&
1601 flags != (GRE_SEQ | GRE_KEY))
1602 return -EINVAL;
1603
1604 /* ERSPAN Session ID only has 10-bit. Since we reuse
1605 * 32-bit key field as ID, check it's range.
1606 */
1607 if (data[IFLA_GRE_IKEY] &&
1608 (ntohl(nla_get_be32(data[IFLA_GRE_IKEY])) & ~ID_MASK))
1609 return -EINVAL;
1610
1611 if (data[IFLA_GRE_OKEY] &&
1612 (ntohl(nla_get_be32(data[IFLA_GRE_OKEY])) & ~ID_MASK))
1613 return -EINVAL;
1614
William Tu94d7d8f2017-12-13 16:38:57 -08001615 if (data[IFLA_GRE_ERSPAN_VER]) {
1616 ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]);
1617 if (ver != 1 && ver != 2)
William Tu5a963eb2017-11-30 11:51:29 -08001618 return -EINVAL;
1619 }
William Tu94d7d8f2017-12-13 16:38:57 -08001620
1621 if (ver == 1) {
1622 if (data[IFLA_GRE_ERSPAN_INDEX]) {
1623 u32 index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
1624
1625 if (index & ~INDEX_MASK)
1626 return -EINVAL;
1627 }
1628 } else if (ver == 2) {
1629 if (data[IFLA_GRE_ERSPAN_DIR]) {
1630 u16 dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]);
1631
1632 if (dir & ~(DIR_MASK >> DIR_OFFSET))
1633 return -EINVAL;
1634 }
1635
1636 if (data[IFLA_GRE_ERSPAN_HWID]) {
1637 u16 hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]);
1638
1639 if (hwid & ~(HWID_MASK >> HWID_OFFSET))
1640 return -EINVAL;
1641 }
1642 }
1643
William Tu5a963eb2017-11-30 11:51:29 -08001644 return 0;
1645}
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001646
1647static void ip6gre_netlink_parms(struct nlattr *data[],
1648 struct __ip6_tnl_parm *parms)
1649{
1650 memset(parms, 0, sizeof(*parms));
1651
1652 if (!data)
1653 return;
1654
1655 if (data[IFLA_GRE_LINK])
1656 parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
1657
1658 if (data[IFLA_GRE_IFLAGS])
Tom Herbertf41fe3c2016-05-09 17:12:09 -07001659 parms->i_flags = gre_flags_to_tnl_flags(
1660 nla_get_be16(data[IFLA_GRE_IFLAGS]));
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001661
1662 if (data[IFLA_GRE_OFLAGS])
Tom Herbertf41fe3c2016-05-09 17:12:09 -07001663 parms->o_flags = gre_flags_to_tnl_flags(
1664 nla_get_be16(data[IFLA_GRE_OFLAGS]));
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001665
1666 if (data[IFLA_GRE_IKEY])
1667 parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
1668
1669 if (data[IFLA_GRE_OKEY])
1670 parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
1671
1672 if (data[IFLA_GRE_LOCAL])
Jiri Benc67b61f62015-03-29 16:59:26 +02001673 parms->laddr = nla_get_in6_addr(data[IFLA_GRE_LOCAL]);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001674
1675 if (data[IFLA_GRE_REMOTE])
Jiri Benc67b61f62015-03-29 16:59:26 +02001676 parms->raddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001677
1678 if (data[IFLA_GRE_TTL])
1679 parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]);
1680
1681 if (data[IFLA_GRE_ENCAP_LIMIT])
1682 parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]);
1683
1684 if (data[IFLA_GRE_FLOWINFO])
Lance Richardsonc2675de2016-09-24 14:01:04 -04001685 parms->flowinfo = nla_get_be32(data[IFLA_GRE_FLOWINFO]);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001686
1687 if (data[IFLA_GRE_FLAGS])
1688 parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]);
Craig Gallek0a473b82017-04-19 12:30:53 -04001689
1690 if (data[IFLA_GRE_FWMARK])
1691 parms->fwmark = nla_get_u32(data[IFLA_GRE_FWMARK]);
William Tu5a963eb2017-11-30 11:51:29 -08001692
William Tu6712abc2017-12-01 15:26:08 -08001693 if (data[IFLA_GRE_COLLECT_METADATA])
1694 parms->collect_md = true;
William Tu94d7d8f2017-12-13 16:38:57 -08001695
1696 if (data[IFLA_GRE_ERSPAN_VER])
1697 parms->erspan_ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]);
1698
1699 if (parms->erspan_ver == 1) {
1700 if (data[IFLA_GRE_ERSPAN_INDEX])
1701 parms->index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
1702 } else if (parms->erspan_ver == 2) {
1703 if (data[IFLA_GRE_ERSPAN_DIR])
1704 parms->dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]);
1705 if (data[IFLA_GRE_ERSPAN_HWID])
1706 parms->hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]);
1707 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001708}
1709
1710static int ip6gre_tap_init(struct net_device *dev)
1711{
Martin KaFai Laua3c119d2015-09-15 14:30:05 -07001712 int ret;
1713
1714 ret = ip6gre_tunnel_init_common(dev);
1715 if (ret)
1716 return ret;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001717
Shweta Choudaha0a46baa2016-06-08 20:15:43 +01001718 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1719
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001720 return 0;
1721}
1722
1723static const struct net_device_ops ip6gre_tap_netdev_ops = {
1724 .ndo_init = ip6gre_tap_init,
1725 .ndo_uninit = ip6gre_tunnel_uninit,
1726 .ndo_start_xmit = ip6gre_tunnel_xmit,
1727 .ndo_set_mac_address = eth_mac_addr,
1728 .ndo_validate_addr = eth_validate_addr,
Tom Herbertb05229f2016-04-29 17:12:21 -07001729 .ndo_change_mtu = ip6_tnl_change_mtu,
Pravin B Shelarf61dd382013-03-25 14:50:00 +00001730 .ndo_get_stats64 = ip_tunnel_get_stats64,
Nicolas Dichtelecf2c062015-04-02 17:07:01 +02001731 .ndo_get_iflink = ip6_tnl_get_iflink,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001732};
1733
William Tu5a963eb2017-11-30 11:51:29 -08001734static int ip6erspan_tap_init(struct net_device *dev)
1735{
1736 struct ip6_tnl *tunnel;
1737 int t_hlen;
1738 int ret;
1739
1740 tunnel = netdev_priv(dev);
1741
1742 tunnel->dev = dev;
1743 tunnel->net = dev_net(dev);
1744 strcpy(tunnel->parms.name, dev->name);
1745
1746 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1747 if (!dev->tstats)
1748 return -ENOMEM;
1749
1750 ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
1751 if (ret) {
1752 free_percpu(dev->tstats);
1753 dev->tstats = NULL;
1754 return ret;
1755 }
1756
1757 tunnel->tun_hlen = 8;
1758 tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen +
William Tu94d7d8f2017-12-13 16:38:57 -08001759 erspan_hdr_len(tunnel->parms.erspan_ver);
William Tu5a963eb2017-11-30 11:51:29 -08001760 t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
1761
1762 dev->hard_header_len = LL_MAX_HEADER + t_hlen;
1763 dev->mtu = ETH_DATA_LEN - t_hlen;
1764 if (dev->type == ARPHRD_ETHER)
1765 dev->mtu -= ETH_HLEN;
1766 if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1767 dev->mtu -= 8;
1768
1769 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
William Tu5a963eb2017-11-30 11:51:29 -08001770 ip6gre_tnl_link_config(tunnel, 1);
1771
1772 return 0;
1773}
1774
1775static const struct net_device_ops ip6erspan_netdev_ops = {
1776 .ndo_init = ip6erspan_tap_init,
1777 .ndo_uninit = ip6gre_tunnel_uninit,
1778 .ndo_start_xmit = ip6erspan_tunnel_xmit,
1779 .ndo_set_mac_address = eth_mac_addr,
1780 .ndo_validate_addr = eth_validate_addr,
1781 .ndo_change_mtu = ip6_tnl_change_mtu,
1782 .ndo_get_stats64 = ip_tunnel_get_stats64,
1783 .ndo_get_iflink = ip6_tnl_get_iflink,
1784};
1785
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001786static void ip6gre_tap_setup(struct net_device *dev)
1787{
1788
1789 ether_setup(dev);
1790
Xin Long2c521292017-12-18 14:25:09 +08001791 dev->max_mtu = 0;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001792 dev->netdev_ops = &ip6gre_tap_netdev_ops;
David S. Millercf124db2017-05-08 12:52:56 -04001793 dev->needs_free_netdev = true;
1794 dev->priv_destructor = ip6gre_dev_free;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001795
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001796 dev->features |= NETIF_F_NETNS_LOCAL;
Jiri Bencd13b1612016-02-17 15:32:53 +01001797 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
Shweta Choudaha0a46baa2016-06-08 20:15:43 +01001798 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Xin Long2d405572017-09-28 13:23:50 +08001799 netif_keep_dst(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001800}
1801
Petr Machatad1b2a6c2018-02-27 14:53:37 +01001802bool is_ip6gretap_dev(const struct net_device *dev)
1803{
1804 return dev->netdev_ops == &ip6gre_tap_netdev_ops;
1805}
1806EXPORT_SYMBOL_GPL(is_ip6gretap_dev);
1807
Tom Herbert1faf3d92016-05-18 09:06:19 -07001808static bool ip6gre_netlink_encap_parms(struct nlattr *data[],
1809 struct ip_tunnel_encap *ipencap)
1810{
1811 bool ret = false;
1812
1813 memset(ipencap, 0, sizeof(*ipencap));
1814
1815 if (!data)
1816 return ret;
1817
1818 if (data[IFLA_GRE_ENCAP_TYPE]) {
1819 ret = true;
1820 ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]);
1821 }
1822
1823 if (data[IFLA_GRE_ENCAP_FLAGS]) {
1824 ret = true;
1825 ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]);
1826 }
1827
1828 if (data[IFLA_GRE_ENCAP_SPORT]) {
1829 ret = true;
1830 ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]);
1831 }
1832
1833 if (data[IFLA_GRE_ENCAP_DPORT]) {
1834 ret = true;
1835 ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]);
1836 }
1837
1838 return ret;
1839}
1840
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001841static int ip6gre_newlink(struct net *src_net, struct net_device *dev,
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02001842 struct nlattr *tb[], struct nlattr *data[],
1843 struct netlink_ext_ack *extack)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001844{
1845 struct ip6_tnl *nt;
1846 struct net *net = dev_net(dev);
1847 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
Tom Herbert1faf3d92016-05-18 09:06:19 -07001848 struct ip_tunnel_encap ipencap;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001849 int err;
1850
1851 nt = netdev_priv(dev);
Tom Herbert1faf3d92016-05-18 09:06:19 -07001852
1853 if (ip6gre_netlink_encap_parms(data, &ipencap)) {
1854 int err = ip6_tnl_encap_setup(nt, &ipencap);
1855
1856 if (err < 0)
1857 return err;
1858 }
1859
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001860 ip6gre_netlink_parms(data, &nt->parms);
1861
William Tu6712abc2017-12-01 15:26:08 -08001862 if (nt->parms.collect_md) {
1863 if (rtnl_dereference(ign->collect_md_tun))
1864 return -EEXIST;
1865 } else {
1866 if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
1867 return -EEXIST;
1868 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001869
1870 if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
1871 eth_hw_addr_random(dev);
1872
1873 nt->dev = dev;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001874 nt->net = dev_net(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001875
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001876 err = register_netdevice(dev);
1877 if (err)
1878 goto out;
1879
Alexey Kodanev128bb972018-01-18 20:51:12 +03001880 ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]);
1881
1882 if (tb[IFLA_MTU])
1883 ip6_tnl_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1884
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001885 dev_hold(dev);
1886 ip6gre_tunnel_link(ign, nt);
1887
1888out:
1889 return err;
1890}
1891
1892static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],
Matthias Schifferad744b22017-06-25 23:56:00 +02001893 struct nlattr *data[],
1894 struct netlink_ext_ack *extack)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001895{
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001896 struct ip6_tnl *t, *nt = netdev_priv(dev);
1897 struct net *net = nt->net;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001898 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1899 struct __ip6_tnl_parm p;
Tom Herbert1faf3d92016-05-18 09:06:19 -07001900 struct ip_tunnel_encap ipencap;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001901
1902 if (dev == ign->fb_tunnel_dev)
1903 return -EINVAL;
1904
Tom Herbert1faf3d92016-05-18 09:06:19 -07001905 if (ip6gre_netlink_encap_parms(data, &ipencap)) {
1906 int err = ip6_tnl_encap_setup(nt, &ipencap);
1907
1908 if (err < 0)
1909 return err;
1910 }
1911
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001912 ip6gre_netlink_parms(data, &p);
1913
1914 t = ip6gre_tunnel_locate(net, &p, 0);
1915
1916 if (t) {
1917 if (t->dev != dev)
1918 return -EEXIST;
1919 } else {
1920 t = nt;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001921 }
1922
Nicolas Dichtel6a61d4d2015-12-03 17:21:50 +01001923 ip6gre_tunnel_unlink(ign, t);
1924 ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]);
1925 ip6gre_tunnel_link(ign, t);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001926 return 0;
1927}
1928
Nicolas Dichtel54d63f782014-04-14 17:11:38 +02001929static void ip6gre_dellink(struct net_device *dev, struct list_head *head)
1930{
1931 struct net *net = dev_net(dev);
1932 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1933
1934 if (dev != ign->fb_tunnel_dev)
1935 unregister_netdevice_queue(dev, head);
1936}
1937
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001938static size_t ip6gre_get_size(const struct net_device *dev)
1939{
1940 return
1941 /* IFLA_GRE_LINK */
1942 nla_total_size(4) +
1943 /* IFLA_GRE_IFLAGS */
1944 nla_total_size(2) +
1945 /* IFLA_GRE_OFLAGS */
1946 nla_total_size(2) +
1947 /* IFLA_GRE_IKEY */
1948 nla_total_size(4) +
1949 /* IFLA_GRE_OKEY */
1950 nla_total_size(4) +
1951 /* IFLA_GRE_LOCAL */
Nicolas Dichtela3754132012-11-09 05:34:56 +00001952 nla_total_size(sizeof(struct in6_addr)) +
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001953 /* IFLA_GRE_REMOTE */
Nicolas Dichtela3754132012-11-09 05:34:56 +00001954 nla_total_size(sizeof(struct in6_addr)) +
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001955 /* IFLA_GRE_TTL */
1956 nla_total_size(1) +
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001957 /* IFLA_GRE_ENCAP_LIMIT */
1958 nla_total_size(1) +
1959 /* IFLA_GRE_FLOWINFO */
1960 nla_total_size(4) +
1961 /* IFLA_GRE_FLAGS */
1962 nla_total_size(4) +
Tom Herbert1faf3d92016-05-18 09:06:19 -07001963 /* IFLA_GRE_ENCAP_TYPE */
1964 nla_total_size(2) +
1965 /* IFLA_GRE_ENCAP_FLAGS */
1966 nla_total_size(2) +
1967 /* IFLA_GRE_ENCAP_SPORT */
1968 nla_total_size(2) +
1969 /* IFLA_GRE_ENCAP_DPORT */
1970 nla_total_size(2) +
William Tu6712abc2017-12-01 15:26:08 -08001971 /* IFLA_GRE_COLLECT_METADATA */
1972 nla_total_size(0) +
Craig Gallek0a473b82017-04-19 12:30:53 -04001973 /* IFLA_GRE_FWMARK */
1974 nla_total_size(4) +
William Tu5a963eb2017-11-30 11:51:29 -08001975 /* IFLA_GRE_ERSPAN_INDEX */
1976 nla_total_size(4) +
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001977 0;
1978}
1979
1980static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev)
1981{
1982 struct ip6_tnl *t = netdev_priv(dev);
1983 struct __ip6_tnl_parm *p = &t->parms;
1984
1985 if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
Tom Herbertf41fe3c2016-05-09 17:12:09 -07001986 nla_put_be16(skb, IFLA_GRE_IFLAGS,
1987 gre_tnl_flags_to_gre_flags(p->i_flags)) ||
1988 nla_put_be16(skb, IFLA_GRE_OFLAGS,
1989 gre_tnl_flags_to_gre_flags(p->o_flags)) ||
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001990 nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
1991 nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
Jiri Benc930345e2015-03-29 16:59:25 +02001992 nla_put_in6_addr(skb, IFLA_GRE_LOCAL, &p->laddr) ||
1993 nla_put_in6_addr(skb, IFLA_GRE_REMOTE, &p->raddr) ||
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001994 nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) ||
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001995 nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) ||
1996 nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) ||
Craig Gallek0a473b82017-04-19 12:30:53 -04001997 nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags) ||
William Tu5a963eb2017-11-30 11:51:29 -08001998 nla_put_u32(skb, IFLA_GRE_FWMARK, p->fwmark) ||
1999 nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, p->index))
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002000 goto nla_put_failure;
Tom Herbert1faf3d92016-05-18 09:06:19 -07002001
2002 if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE,
2003 t->encap.type) ||
2004 nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT,
2005 t->encap.sport) ||
2006 nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT,
2007 t->encap.dport) ||
2008 nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS,
2009 t->encap.flags))
2010 goto nla_put_failure;
2011
William Tu6712abc2017-12-01 15:26:08 -08002012 if (p->collect_md) {
2013 if (nla_put_flag(skb, IFLA_GRE_COLLECT_METADATA))
2014 goto nla_put_failure;
2015 }
2016
William Tu94d7d8f2017-12-13 16:38:57 -08002017 if (nla_put_u8(skb, IFLA_GRE_ERSPAN_VER, p->erspan_ver))
2018 goto nla_put_failure;
2019
2020 if (p->erspan_ver == 1) {
2021 if (nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, p->index))
2022 goto nla_put_failure;
2023 } else if (p->erspan_ver == 2) {
2024 if (nla_put_u8(skb, IFLA_GRE_ERSPAN_DIR, p->dir))
2025 goto nla_put_failure;
2026 if (nla_put_u16(skb, IFLA_GRE_ERSPAN_HWID, p->hwid))
2027 goto nla_put_failure;
2028 }
2029
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002030 return 0;
2031
2032nla_put_failure:
2033 return -EMSGSIZE;
2034}
2035
2036static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = {
2037 [IFLA_GRE_LINK] = { .type = NLA_U32 },
2038 [IFLA_GRE_IFLAGS] = { .type = NLA_U16 },
2039 [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
2040 [IFLA_GRE_IKEY] = { .type = NLA_U32 },
2041 [IFLA_GRE_OKEY] = { .type = NLA_U32 },
2042 [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct ipv6hdr, saddr) },
2043 [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct ipv6hdr, daddr) },
2044 [IFLA_GRE_TTL] = { .type = NLA_U8 },
2045 [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 },
2046 [IFLA_GRE_FLOWINFO] = { .type = NLA_U32 },
2047 [IFLA_GRE_FLAGS] = { .type = NLA_U32 },
Tom Herbert1faf3d92016-05-18 09:06:19 -07002048 [IFLA_GRE_ENCAP_TYPE] = { .type = NLA_U16 },
2049 [IFLA_GRE_ENCAP_FLAGS] = { .type = NLA_U16 },
2050 [IFLA_GRE_ENCAP_SPORT] = { .type = NLA_U16 },
2051 [IFLA_GRE_ENCAP_DPORT] = { .type = NLA_U16 },
William Tu6712abc2017-12-01 15:26:08 -08002052 [IFLA_GRE_COLLECT_METADATA] = { .type = NLA_FLAG },
Craig Gallek0a473b82017-04-19 12:30:53 -04002053 [IFLA_GRE_FWMARK] = { .type = NLA_U32 },
William Tu5a963eb2017-11-30 11:51:29 -08002054 [IFLA_GRE_ERSPAN_INDEX] = { .type = NLA_U32 },
William Tu94d7d8f2017-12-13 16:38:57 -08002055 [IFLA_GRE_ERSPAN_VER] = { .type = NLA_U8 },
2056 [IFLA_GRE_ERSPAN_DIR] = { .type = NLA_U8 },
2057 [IFLA_GRE_ERSPAN_HWID] = { .type = NLA_U16 },
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002058};
2059
William Tu5a963eb2017-11-30 11:51:29 -08002060static void ip6erspan_tap_setup(struct net_device *dev)
2061{
2062 ether_setup(dev);
2063
2064 dev->netdev_ops = &ip6erspan_netdev_ops;
2065 dev->needs_free_netdev = true;
2066 dev->priv_destructor = ip6gre_dev_free;
2067
2068 dev->features |= NETIF_F_NETNS_LOCAL;
2069 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
2070 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2071 netif_keep_dst(dev);
2072}
2073
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002074static struct rtnl_link_ops ip6gre_link_ops __read_mostly = {
2075 .kind = "ip6gre",
2076 .maxtype = IFLA_GRE_MAX,
2077 .policy = ip6gre_policy,
2078 .priv_size = sizeof(struct ip6_tnl),
2079 .setup = ip6gre_tunnel_setup,
2080 .validate = ip6gre_tunnel_validate,
2081 .newlink = ip6gre_newlink,
2082 .changelink = ip6gre_changelink,
Nicolas Dichtel54d63f782014-04-14 17:11:38 +02002083 .dellink = ip6gre_dellink,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002084 .get_size = ip6gre_get_size,
2085 .fill_info = ip6gre_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01002086 .get_link_net = ip6_tnl_get_link_net,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002087};
2088
2089static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = {
2090 .kind = "ip6gretap",
2091 .maxtype = IFLA_GRE_MAX,
2092 .policy = ip6gre_policy,
2093 .priv_size = sizeof(struct ip6_tnl),
2094 .setup = ip6gre_tap_setup,
2095 .validate = ip6gre_tap_validate,
2096 .newlink = ip6gre_newlink,
2097 .changelink = ip6gre_changelink,
2098 .get_size = ip6gre_get_size,
2099 .fill_info = ip6gre_fill_info,
Nicolas Dichtel3390e392015-01-20 15:15:43 +01002100 .get_link_net = ip6_tnl_get_link_net,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002101};
2102
William Tu5a963eb2017-11-30 11:51:29 -08002103static struct rtnl_link_ops ip6erspan_tap_ops __read_mostly = {
2104 .kind = "ip6erspan",
2105 .maxtype = IFLA_GRE_MAX,
2106 .policy = ip6gre_policy,
2107 .priv_size = sizeof(struct ip6_tnl),
2108 .setup = ip6erspan_tap_setup,
2109 .validate = ip6erspan_tap_validate,
2110 .newlink = ip6gre_newlink,
2111 .changelink = ip6gre_changelink,
2112 .get_size = ip6gre_get_size,
2113 .fill_info = ip6gre_fill_info,
2114 .get_link_net = ip6_tnl_get_link_net,
2115};
2116
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002117/*
2118 * And now the modules code and kernel interface.
2119 */
2120
2121static int __init ip6gre_init(void)
2122{
2123 int err;
2124
2125 pr_info("GRE over IPv6 tunneling driver\n");
2126
2127 err = register_pernet_device(&ip6gre_net_ops);
2128 if (err < 0)
2129 return err;
2130
2131 err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE);
2132 if (err < 0) {
2133 pr_info("%s: can't add protocol\n", __func__);
2134 goto add_proto_failed;
2135 }
2136
2137 err = rtnl_link_register(&ip6gre_link_ops);
2138 if (err < 0)
2139 goto rtnl_link_failed;
2140
2141 err = rtnl_link_register(&ip6gre_tap_ops);
2142 if (err < 0)
2143 goto tap_ops_failed;
2144
William Tu5a963eb2017-11-30 11:51:29 -08002145 err = rtnl_link_register(&ip6erspan_tap_ops);
2146 if (err < 0)
2147 goto erspan_link_failed;
2148
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002149out:
2150 return err;
2151
William Tu5a963eb2017-11-30 11:51:29 -08002152erspan_link_failed:
2153 rtnl_link_unregister(&ip6gre_tap_ops);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002154tap_ops_failed:
2155 rtnl_link_unregister(&ip6gre_link_ops);
2156rtnl_link_failed:
2157 inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
2158add_proto_failed:
2159 unregister_pernet_device(&ip6gre_net_ops);
2160 goto out;
2161}
2162
2163static void __exit ip6gre_fini(void)
2164{
2165 rtnl_link_unregister(&ip6gre_tap_ops);
2166 rtnl_link_unregister(&ip6gre_link_ops);
William Tu5a963eb2017-11-30 11:51:29 -08002167 rtnl_link_unregister(&ip6erspan_tap_ops);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002168 inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
2169 unregister_pernet_device(&ip6gre_net_ops);
2170}
2171
2172module_init(ip6gre_init);
2173module_exit(ip6gre_fini);
2174MODULE_LICENSE("GPL");
2175MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
2176MODULE_DESCRIPTION("GRE over IPv6 tunneling device");
2177MODULE_ALIAS_RTNL_LINK("ip6gre");
Nicolas Dichtel5a4ee9a2014-09-24 11:03:00 +02002178MODULE_ALIAS_RTNL_LINK("ip6gretap");
William Tu94d7d8f2017-12-13 16:38:57 -08002179MODULE_ALIAS_RTNL_LINK("ip6erspan");
xeb@mail.ruc12b3952012-08-10 00:51:50 +00002180MODULE_ALIAS_NETDEV("ip6gre0");