]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/ipv6/ip6_gre.c
x86/speculation/mds: Conditionally clear CPU buffers on idle entry
[mirror_ubuntu-bionic-kernel.git] / net / ipv6 / ip6_gre.c
CommitLineData
c12b395a 1/*
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>
c12b395a 27#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>
c5441932 40#include <net/ip_tunnels.h>
c12b395a 41#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>
308edfdf 57#include <net/gre.h>
c12b395a 58
59
eccc1bb8 60static bool log_ecn_error = true;
61module_param(log_ecn_error, bool, 0644);
62MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
63
e87a8f24
JK
64#define IP6_GRE_HASH_SIZE_SHIFT 5
65#define IP6_GRE_HASH_SIZE (1 << IP6_GRE_HASH_SIZE_SHIFT)
c12b395a 66
c7d03a00 67static unsigned int ip6gre_net_id __read_mostly;
c12b395a 68struct ip6gre_net {
e87a8f24 69 struct ip6_tnl __rcu *tunnels[4][IP6_GRE_HASH_SIZE];
c12b395a 70
71 struct net_device *fb_tunnel_dev;
72};
73
74static struct rtnl_link_ops ip6gre_link_ops __read_mostly;
22f08069 75static struct rtnl_link_ops ip6gre_tap_ops __read_mostly;
c12b395a 76static int ip6gre_tunnel_init(struct net_device *dev);
77static void ip6gre_tunnel_setup(struct net_device *dev);
78static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t);
79static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu);
80
81/* Tunnel hash table */
82
83/*
84 4 hash tables:
85
86 3: (remote,local)
87 2: (remote,*)
88 1: (*,local)
89 0: (*,*)
90
91 We require exact key match i.e. if a key is present in packet
92 it will match only tunnel with the same key; if it is not present,
93 it will match only keyless tunnel.
94
95 All keysless packets, if not matched configured keyless tunnels
96 will match fallback tunnel.
97 */
98
e87a8f24 99#define HASH_KEY(key) (((__force u32)key^((__force u32)key>>4))&(IP6_GRE_HASH_SIZE - 1))
c12b395a 100static u32 HASH_ADDR(const struct in6_addr *addr)
101{
102 u32 hash = ipv6_addr_hash(addr);
103
e87a8f24 104 return hash_32(hash, IP6_GRE_HASH_SIZE_SHIFT);
c12b395a 105}
106
107#define tunnels_r_l tunnels[3]
108#define tunnels_r tunnels[2]
109#define tunnels_l tunnels[1]
110#define tunnels_wc tunnels[0]
c12b395a 111
c12b395a 112/* Given src, dst and key, find appropriate for input tunnel. */
113
114static struct ip6_tnl *ip6gre_tunnel_lookup(struct net_device *dev,
115 const struct in6_addr *remote, const struct in6_addr *local,
116 __be32 key, __be16 gre_proto)
117{
118 struct net *net = dev_net(dev);
119 int link = dev->ifindex;
120 unsigned int h0 = HASH_ADDR(remote);
121 unsigned int h1 = HASH_KEY(key);
122 struct ip6_tnl *t, *cand = NULL;
123 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
124 int dev_type = (gre_proto == htons(ETH_P_TEB)) ?
125 ARPHRD_ETHER : ARPHRD_IP6GRE;
126 int score, cand_score = 4;
127
e086cadc 128 for_each_ip_tunnel_rcu(t, ign->tunnels_r_l[h0 ^ h1]) {
c12b395a 129 if (!ipv6_addr_equal(local, &t->parms.laddr) ||
130 !ipv6_addr_equal(remote, &t->parms.raddr) ||
131 key != t->parms.i_key ||
132 !(t->dev->flags & IFF_UP))
133 continue;
134
135 if (t->dev->type != ARPHRD_IP6GRE &&
136 t->dev->type != dev_type)
137 continue;
138
139 score = 0;
140 if (t->parms.link != link)
141 score |= 1;
142 if (t->dev->type != dev_type)
143 score |= 2;
144 if (score == 0)
145 return t;
146
147 if (score < cand_score) {
148 cand = t;
149 cand_score = score;
150 }
151 }
152
e086cadc 153 for_each_ip_tunnel_rcu(t, ign->tunnels_r[h0 ^ h1]) {
c12b395a 154 if (!ipv6_addr_equal(remote, &t->parms.raddr) ||
155 key != t->parms.i_key ||
156 !(t->dev->flags & IFF_UP))
157 continue;
158
159 if (t->dev->type != ARPHRD_IP6GRE &&
160 t->dev->type != dev_type)
161 continue;
162
163 score = 0;
164 if (t->parms.link != link)
165 score |= 1;
166 if (t->dev->type != dev_type)
167 score |= 2;
168 if (score == 0)
169 return t;
170
171 if (score < cand_score) {
172 cand = t;
173 cand_score = score;
174 }
175 }
176
e086cadc 177 for_each_ip_tunnel_rcu(t, ign->tunnels_l[h1]) {
c12b395a 178 if ((!ipv6_addr_equal(local, &t->parms.laddr) &&
179 (!ipv6_addr_equal(local, &t->parms.raddr) ||
180 !ipv6_addr_is_multicast(local))) ||
181 key != t->parms.i_key ||
182 !(t->dev->flags & IFF_UP))
183 continue;
184
185 if (t->dev->type != ARPHRD_IP6GRE &&
186 t->dev->type != dev_type)
187 continue;
188
189 score = 0;
190 if (t->parms.link != link)
191 score |= 1;
192 if (t->dev->type != dev_type)
193 score |= 2;
194 if (score == 0)
195 return t;
196
197 if (score < cand_score) {
198 cand = t;
199 cand_score = score;
200 }
201 }
202
e086cadc 203 for_each_ip_tunnel_rcu(t, ign->tunnels_wc[h1]) {
c12b395a 204 if (t->parms.i_key != key ||
205 !(t->dev->flags & IFF_UP))
206 continue;
207
208 if (t->dev->type != ARPHRD_IP6GRE &&
209 t->dev->type != dev_type)
210 continue;
211
212 score = 0;
213 if (t->parms.link != link)
214 score |= 1;
215 if (t->dev->type != dev_type)
216 score |= 2;
217 if (score == 0)
218 return t;
219
220 if (score < cand_score) {
221 cand = t;
222 cand_score = score;
223 }
224 }
225
53b24b8f 226 if (cand)
c12b395a 227 return cand;
228
229 dev = ign->fb_tunnel_dev;
230 if (dev->flags & IFF_UP)
231 return netdev_priv(dev);
232
233 return NULL;
234}
235
236static struct ip6_tnl __rcu **__ip6gre_bucket(struct ip6gre_net *ign,
237 const struct __ip6_tnl_parm *p)
238{
239 const struct in6_addr *remote = &p->raddr;
240 const struct in6_addr *local = &p->laddr;
241 unsigned int h = HASH_KEY(p->i_key);
242 int prio = 0;
243
244 if (!ipv6_addr_any(local))
245 prio |= 1;
246 if (!ipv6_addr_any(remote) && !ipv6_addr_is_multicast(remote)) {
247 prio |= 2;
248 h ^= HASH_ADDR(remote);
249 }
250
251 return &ign->tunnels[prio][h];
252}
253
254static inline struct ip6_tnl __rcu **ip6gre_bucket(struct ip6gre_net *ign,
255 const struct ip6_tnl *t)
256{
257 return __ip6gre_bucket(ign, &t->parms);
258}
259
260static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t)
261{
262 struct ip6_tnl __rcu **tp = ip6gre_bucket(ign, t);
263
264 rcu_assign_pointer(t->next, rtnl_dereference(*tp));
265 rcu_assign_pointer(*tp, t);
266}
267
268static void ip6gre_tunnel_unlink(struct ip6gre_net *ign, struct ip6_tnl *t)
269{
270 struct ip6_tnl __rcu **tp;
271 struct ip6_tnl *iter;
272
273 for (tp = ip6gre_bucket(ign, t);
274 (iter = rtnl_dereference(*tp)) != NULL;
275 tp = &iter->next) {
276 if (t == iter) {
277 rcu_assign_pointer(*tp, t->next);
278 break;
279 }
280 }
281}
282
283static struct ip6_tnl *ip6gre_tunnel_find(struct net *net,
284 const struct __ip6_tnl_parm *parms,
285 int type)
286{
287 const struct in6_addr *remote = &parms->raddr;
288 const struct in6_addr *local = &parms->laddr;
289 __be32 key = parms->i_key;
290 int link = parms->link;
291 struct ip6_tnl *t;
292 struct ip6_tnl __rcu **tp;
293 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
294
295 for (tp = __ip6gre_bucket(ign, parms);
296 (t = rtnl_dereference(*tp)) != NULL;
297 tp = &t->next)
298 if (ipv6_addr_equal(local, &t->parms.laddr) &&
299 ipv6_addr_equal(remote, &t->parms.raddr) &&
300 key == t->parms.i_key &&
301 link == t->parms.link &&
302 type == t->dev->type)
303 break;
304
305 return t;
306}
307
308static struct ip6_tnl *ip6gre_tunnel_locate(struct net *net,
309 const struct __ip6_tnl_parm *parms, int create)
310{
311 struct ip6_tnl *t, *nt;
312 struct net_device *dev;
313 char name[IFNAMSIZ];
314 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
315
316 t = ip6gre_tunnel_find(net, parms, ARPHRD_IP6GRE);
cd0a0bd9
SK
317 if (t && create)
318 return NULL;
c12b395a 319 if (t || !create)
320 return t;
321
370bbc00
ED
322 if (parms->name[0]) {
323 if (!dev_valid_name(parms->name))
324 return NULL;
c12b395a 325 strlcpy(name, parms->name, IFNAMSIZ);
370bbc00 326 } else {
c12b395a 327 strcpy(name, "ip6gre%d");
370bbc00 328 }
c835a677
TG
329 dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
330 ip6gre_tunnel_setup);
c12b395a 331 if (!dev)
332 return NULL;
333
334 dev_net_set(dev, net);
335
336 nt = netdev_priv(dev);
337 nt->parms = *parms;
338 dev->rtnl_link_ops = &ip6gre_link_ops;
339
340 nt->dev = dev;
0bd87628 341 nt->net = dev_net(dev);
c12b395a 342
343 if (register_netdevice(dev) < 0)
344 goto failed_free;
345
128bb975
AK
346 ip6gre_tnl_link_config(nt, 1);
347
c12b395a 348 /* Can use a lockless transmit, unless we generate output sequences */
b45bd1d7 349 if (!(nt->parms.o_flags & TUNNEL_SEQ))
c12b395a 350 dev->features |= NETIF_F_LLTX;
351
352 dev_hold(dev);
353 ip6gre_tunnel_link(ign, nt);
354 return nt;
355
356failed_free:
357 free_netdev(dev);
358 return NULL;
359}
360
361static void ip6gre_tunnel_uninit(struct net_device *dev)
362{
22f08069
ND
363 struct ip6_tnl *t = netdev_priv(dev);
364 struct ip6gre_net *ign = net_generic(t->net, ip6gre_net_id);
c12b395a 365
22f08069 366 ip6gre_tunnel_unlink(ign, t);
607f725f 367 dst_cache_reset(&t->dst_cache);
c12b395a 368 dev_put(dev);
369}
370
371
372static void ip6gre_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
7892032c 373 u8 type, u8 code, int offset, __be32 info)
c12b395a 374{
929fc032 375 struct net *net = dev_net(skb->dev);
7892032c
ED
376 const struct gre_base_hdr *greh;
377 const struct ipv6hdr *ipv6h;
378 int grehlen = sizeof(*greh);
c12b395a 379 struct ip6_tnl *t;
7892032c 380 int key_off = 0;
c12b395a 381 __be16 flags;
7892032c 382 __be32 key;
c12b395a 383
7892032c
ED
384 if (!pskb_may_pull(skb, offset + grehlen))
385 return;
386 greh = (const struct gre_base_hdr *)(skb->data + offset);
387 flags = greh->flags;
388 if (flags & (GRE_VERSION | GRE_ROUTING))
389 return;
390 if (flags & GRE_CSUM)
391 grehlen += 4;
392 if (flags & GRE_KEY) {
393 key_off = grehlen + offset;
394 grehlen += 4;
c12b395a 395 }
396
7892032c 397 if (!pskb_may_pull(skb, offset + grehlen))
c12b395a 398 return;
b87fb39e 399 ipv6h = (const struct ipv6hdr *)skb->data;
7892032c
ED
400 greh = (const struct gre_base_hdr *)(skb->data + offset);
401 key = key_off ? *(__be32 *)(skb->data + key_off) : 0;
c12b395a 402
c12b395a 403 t = ip6gre_tunnel_lookup(skb->dev, &ipv6h->daddr, &ipv6h->saddr,
7892032c 404 key, greh->protocol);
63159f29 405 if (!t)
0c5794a6 406 return;
c12b395a 407
408 switch (type) {
c12b395a 409 struct ipv6_tlv_tnl_enc_lim *tel;
fe1a4ca0 410 __u32 teli;
c12b395a 411 case ICMPV6_DEST_UNREACH:
a46496ce
MB
412 net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n",
413 t->parms.name);
f8d20b46
XL
414 if (code != ICMPV6_PORT_UNREACH)
415 break;
416 return;
c12b395a 417 case ICMPV6_TIME_EXCEED:
418 if (code == ICMPV6_EXC_HOPLIMIT) {
a46496ce
MB
419 net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
420 t->parms.name);
f8d20b46 421 break;
c12b395a 422 }
f8d20b46 423 return;
c12b395a 424 case ICMPV6_PARAMPROB:
425 teli = 0;
426 if (code == ICMPV6_HDR_FIELD)
427 teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
428
d1e158e2 429 if (teli && teli == be32_to_cpu(info) - 2) {
c12b395a 430 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
431 if (tel->encap_limit == 0) {
a46496ce
MB
432 net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
433 t->parms.name);
c12b395a 434 }
435 } else {
a46496ce
MB
436 net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
437 t->parms.name);
c12b395a 438 }
f8d20b46 439 return;
c12b395a 440 case ICMPV6_PKT_TOOBIG:
fe1a4ca0 441 ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
f8d20b46 442 return;
929fc032
XL
443 case NDISC_REDIRECT:
444 ip6_redirect(skb, net, skb->dev->ifindex, 0,
445 sock_net_uid(net, NULL));
446 return;
c12b395a 447 }
448
449 if (time_before(jiffies, t->err_time + IP6TUNNEL_ERR_TIMEO))
450 t->err_count++;
451 else
452 t->err_count = 1;
453 t->err_time = jiffies;
c12b395a 454}
455
308edfdf 456static int ip6gre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi)
c12b395a 457{
458 const struct ipv6hdr *ipv6h;
c12b395a 459 struct ip6_tnl *tunnel;
c12b395a 460
461 ipv6h = ipv6_hdr(skb);
c12b395a 462 tunnel = ip6gre_tunnel_lookup(skb->dev,
308edfdf
TH
463 &ipv6h->saddr, &ipv6h->daddr, tpi->key,
464 tpi->proto);
c12b395a 465 if (tunnel) {
981542c5 466 ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error);
c12b395a 467
308edfdf
TH
468 return PACKET_RCVD;
469 }
eccc1bb8 470
308edfdf
TH
471 return PACKET_REJECT;
472}
eccc1bb8 473
308edfdf
TH
474static int gre_rcv(struct sk_buff *skb)
475{
476 struct tnl_ptk_info tpi;
477 bool csum_err = false;
478 int hdr_len;
eccc1bb8 479
e582615a 480 hdr_len = gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IPV6), 0);
f132ae7c 481 if (hdr_len < 0)
308edfdf 482 goto drop;
c12b395a 483
308edfdf
TH
484 if (iptunnel_pull_header(skb, hdr_len, tpi.proto, false))
485 goto drop;
c12b395a 486
308edfdf 487 if (ip6gre_rcv(skb, &tpi) == PACKET_RCVD)
c12b395a 488 return 0;
c12b395a 489
308edfdf 490 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
c12b395a 491drop:
c12b395a 492 kfree_skb(skb);
493 return 0;
494}
495
b05229f4 496static int gre_handle_offloads(struct sk_buff *skb, bool csum)
c12b395a 497{
b05229f4
TH
498 return iptunnel_handle_offloads(skb,
499 csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE);
c12b395a 500}
501
b05229f4
TH
502static netdev_tx_t __gre6_xmit(struct sk_buff *skb,
503 struct net_device *dev, __u8 dsfield,
504 struct flowi6 *fl6, int encap_limit,
505 __u32 *pmtu, __be16 proto)
c12b395a 506{
c12b395a 507 struct ip6_tnl *tunnel = netdev_priv(dev);
8aec4959 508 __be16 protocol;
c12b395a 509
510 if (dev->type == ARPHRD_ETHER)
511 IPCB(skb)->flags = 0;
512
b05229f4
TH
513 if (dev->header_ops && dev->type == ARPHRD_IP6GRE)
514 fl6->daddr = ((struct ipv6hdr *)skb->data)->daddr;
515 else
c12b395a 516 fl6->daddr = tunnel->parms.raddr;
c12b395a 517
b05229f4
TH
518 if (tunnel->parms.o_flags & TUNNEL_SEQ)
519 tunnel->o_seqno++;
c12b395a 520
336701db
PM
521 if (skb_cow_head(skb, dev->needed_headroom ?: tunnel->hlen))
522 return -ENOMEM;
523
b05229f4 524 /* Push GRE header. */
8aec4959 525 protocol = (dev->type == ARPHRD_ETHER) ? htons(ETH_P_TEB) : proto;
b05229f4
TH
526 gre_build_header(skb, tunnel->tun_hlen, tunnel->parms.o_flags,
527 protocol, tunnel->parms.o_key, htonl(tunnel->o_seqno));
c12b395a 528
b05229f4
TH
529 return ip6_tnl_xmit(skb, dev, dsfield, fl6, encap_limit, pmtu,
530 NEXTHDR_GRE);
c12b395a 531}
532
533static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev)
534{
535 struct ip6_tnl *t = netdev_priv(dev);
536 const struct iphdr *iph = ip_hdr(skb);
537 int encap_limit = -1;
538 struct flowi6 fl6;
539 __u8 dsfield;
540 __u32 mtu;
541 int err;
542
5146d1f1
BH
543 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
544
c12b395a 545 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
546 encap_limit = t->parms.encap_limit;
547
548 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
c12b395a 549
c12b395a 550 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
0e9a7095
PD
551 dsfield = ipv4_get_dsfield(iph);
552 else
553 dsfield = ip6_tclass(t->parms.flowinfo);
c12b395a 554 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
555 fl6.flowi6_mark = skb->mark;
0a473b82
CG
556 else
557 fl6.flowi6_mark = t->parms.fwmark;
c12b395a 558
e2d118a1
LC
559 fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
560
b05229f4
TH
561 err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
562 if (err)
563 return -1;
564
565 err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
566 skb->protocol);
c12b395a 567 if (err != 0) {
568 /* XXX: send ICMP error even if DF is not set. */
569 if (err == -EMSGSIZE)
570 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
571 htonl(mtu));
572 return -1;
573 }
574
575 return 0;
576}
577
578static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev)
579{
580 struct ip6_tnl *t = netdev_priv(dev);
581 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
582 int encap_limit = -1;
583 __u16 offset;
584 struct flowi6 fl6;
585 __u8 dsfield;
586 __u32 mtu;
587 int err;
588
589 if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
590 return -1;
591
592 offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
21b995a9
ED
593 /* ip6_tnl_parse_tlv_enc_lim() might have reallocated skb->head */
594 ipv6h = ipv6_hdr(skb);
595
c12b395a 596 if (offset > 0) {
597 struct ipv6_tlv_tnl_enc_lim *tel;
598 tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
599 if (tel->encap_limit == 0) {
600 icmpv6_send(skb, ICMPV6_PARAMPROB,
601 ICMPV6_HDR_FIELD, offset + 2);
602 return -1;
603 }
604 encap_limit = tel->encap_limit - 1;
605 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
606 encap_limit = t->parms.encap_limit;
607
608 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
c12b395a 609
c12b395a 610 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
0e9a7095
PD
611 dsfield = ipv6_get_dsfield(ipv6h);
612 else
613 dsfield = ip6_tclass(t->parms.flowinfo);
614
c12b395a 615 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
3308de2b 616 fl6.flowlabel |= ip6_flowlabel(ipv6h);
c12b395a 617 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
618 fl6.flowi6_mark = skb->mark;
0a473b82
CG
619 else
620 fl6.flowi6_mark = t->parms.fwmark;
c12b395a 621
e2d118a1
LC
622 fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
623
b05229f4
TH
624 if (gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM)))
625 return -1;
626
627 err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit,
628 &mtu, skb->protocol);
c12b395a 629 if (err != 0) {
630 if (err == -EMSGSIZE)
631 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
632 return -1;
633 }
634
635 return 0;
636}
637
638/**
639 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
640 * @t: the outgoing tunnel device
641 * @hdr: IPv6 header from the incoming packet
642 *
643 * Description:
644 * Avoid trivial tunneling loop by checking that tunnel exit-point
645 * doesn't match source of incoming packet.
646 *
647 * Return:
648 * 1 if conflict,
649 * 0 else
650 **/
651
652static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl *t,
653 const struct ipv6hdr *hdr)
654{
655 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
656}
657
658static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev)
659{
660 struct ip6_tnl *t = netdev_priv(dev);
661 int encap_limit = -1;
662 struct flowi6 fl6;
663 __u32 mtu;
664 int err;
665
666 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
667 encap_limit = t->parms.encap_limit;
668
669 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
c12b395a 670
b05229f4
TH
671 err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
672 if (err)
673 return err;
674
675 err = __gre6_xmit(skb, dev, 0, &fl6, encap_limit, &mtu, skb->protocol);
c12b395a 676
677 return err;
678}
679
680static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb,
681 struct net_device *dev)
682{
683 struct ip6_tnl *t = netdev_priv(dev);
684 struct net_device_stats *stats = &t->dev->stats;
685 int ret;
686
d5005140 687 if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
41ab3e31 688 goto tx_err;
c12b395a 689
690 switch (skb->protocol) {
691 case htons(ETH_P_IP):
692 ret = ip6gre_xmit_ipv4(skb, dev);
693 break;
694 case htons(ETH_P_IPV6):
695 ret = ip6gre_xmit_ipv6(skb, dev);
696 break;
697 default:
698 ret = ip6gre_xmit_other(skb, dev);
699 break;
700 }
701
702 if (ret < 0)
703 goto tx_err;
704
705 return NETDEV_TX_OK;
706
707tx_err:
708 stats->tx_errors++;
709 stats->tx_dropped++;
710 kfree_skb(skb);
711 return NETDEV_TX_OK;
712}
713
e8d67209 714static void ip6gre_tnl_link_config_common(struct ip6_tnl *t)
c12b395a 715{
716 struct net_device *dev = t->dev;
717 struct __ip6_tnl_parm *p = &t->parms;
718 struct flowi6 *fl6 = &t->fl.u.ip6;
c12b395a 719
720 if (dev->type != ARPHRD_ETHER) {
721 memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
722 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
723 }
724
725 /* Set up flowi template */
726 fl6->saddr = p->laddr;
727 fl6->daddr = p->raddr;
728 fl6->flowi6_oif = p->link;
729 fl6->flowlabel = 0;
252f3f5a 730 fl6->flowi6_proto = IPPROTO_GRE;
c12b395a 731
732 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
733 fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
734 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
735 fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
736
737 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
738 p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
739
740 if (p->flags&IP6_TNL_F_CAP_XMIT &&
741 p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER)
742 dev->flags |= IFF_POINTOPOINT;
743 else
744 dev->flags &= ~IFF_POINTOPOINT;
e8d67209 745}
c12b395a 746
e8d67209
PM
747static void ip6gre_tnl_link_config_route(struct ip6_tnl *t, int set_mtu,
748 int t_hlen)
749{
750 const struct __ip6_tnl_parm *p = &t->parms;
751 struct net_device *dev = t->dev;
c12b395a 752
753 if (p->flags & IP6_TNL_F_CAP_XMIT) {
754 int strict = (ipv6_addr_type(&p->raddr) &
755 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
756
22f08069 757 struct rt6_info *rt = rt6_lookup(t->net,
c12b395a 758 &p->raddr, &p->laddr,
759 p->link, strict);
760
63159f29 761 if (!rt)
c12b395a 762 return;
763
764 if (rt->dst.dev) {
db2ec95d
TH
765 dev->hard_header_len = rt->dst.dev->hard_header_len +
766 t_hlen;
c12b395a 767
768 if (set_mtu) {
db2ec95d 769 dev->mtu = rt->dst.dev->mtu - t_hlen;
c12b395a 770 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
771 dev->mtu -= 8;
a9e242ca
AD
772 if (dev->type == ARPHRD_ETHER)
773 dev->mtu -= ETH_HLEN;
c12b395a 774
775 if (dev->mtu < IPV6_MIN_MTU)
776 dev->mtu = IPV6_MIN_MTU;
777 }
778 }
94e187c0 779 ip6_rt_put(rt);
c12b395a 780 }
c12b395a 781}
782
e8d67209
PM
783static int ip6gre_calc_hlen(struct ip6_tnl *tunnel)
784{
785 int t_hlen;
786
787 tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
788 tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen;
789
790 t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
791 tunnel->dev->hard_header_len = LL_MAX_HEADER + t_hlen;
792 return t_hlen;
793}
794
795static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu)
796{
797 ip6gre_tnl_link_config_common(t);
798 ip6gre_tnl_link_config_route(t, set_mtu, ip6gre_calc_hlen(t));
799}
800
7586f067
PM
801static void ip6gre_tnl_copy_tnl_parm(struct ip6_tnl *t,
802 const struct __ip6_tnl_parm *p)
c12b395a 803{
804 t->parms.laddr = p->laddr;
805 t->parms.raddr = p->raddr;
806 t->parms.flags = p->flags;
807 t->parms.hop_limit = p->hop_limit;
808 t->parms.encap_limit = p->encap_limit;
809 t->parms.flowinfo = p->flowinfo;
810 t->parms.link = p->link;
811 t->parms.proto = p->proto;
812 t->parms.i_key = p->i_key;
813 t->parms.o_key = p->o_key;
814 t->parms.i_flags = p->i_flags;
815 t->parms.o_flags = p->o_flags;
0a473b82 816 t->parms.fwmark = p->fwmark;
607f725f 817 dst_cache_reset(&t->dst_cache);
7586f067
PM
818}
819
820static int ip6gre_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p,
821 int set_mtu)
822{
823 ip6gre_tnl_copy_tnl_parm(t, p);
c12b395a 824 ip6gre_tnl_link_config(t, set_mtu);
825 return 0;
826}
827
828static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p,
829 const struct ip6_tnl_parm2 *u)
830{
831 p->laddr = u->laddr;
832 p->raddr = u->raddr;
833 p->flags = u->flags;
834 p->hop_limit = u->hop_limit;
835 p->encap_limit = u->encap_limit;
836 p->flowinfo = u->flowinfo;
837 p->link = u->link;
838 p->i_key = u->i_key;
839 p->o_key = u->o_key;
f41fe3c2
TH
840 p->i_flags = gre_flags_to_tnl_flags(u->i_flags);
841 p->o_flags = gre_flags_to_tnl_flags(u->o_flags);
c12b395a 842 memcpy(p->name, u->name, sizeof(u->name));
843}
844
845static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u,
846 const struct __ip6_tnl_parm *p)
847{
848 u->proto = IPPROTO_GRE;
849 u->laddr = p->laddr;
850 u->raddr = p->raddr;
851 u->flags = p->flags;
852 u->hop_limit = p->hop_limit;
853 u->encap_limit = p->encap_limit;
854 u->flowinfo = p->flowinfo;
855 u->link = p->link;
856 u->i_key = p->i_key;
857 u->o_key = p->o_key;
f41fe3c2
TH
858 u->i_flags = gre_tnl_flags_to_gre_flags(p->i_flags);
859 u->o_flags = gre_tnl_flags_to_gre_flags(p->o_flags);
c12b395a 860 memcpy(u->name, p->name, sizeof(u->name));
861}
862
863static int ip6gre_tunnel_ioctl(struct net_device *dev,
864 struct ifreq *ifr, int cmd)
865{
866 int err = 0;
867 struct ip6_tnl_parm2 p;
868 struct __ip6_tnl_parm p1;
22f08069
ND
869 struct ip6_tnl *t = netdev_priv(dev);
870 struct net *net = t->net;
c12b395a 871 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
872
308edfdf
TH
873 memset(&p1, 0, sizeof(p1));
874
c12b395a 875 switch (cmd) {
876 case SIOCGETTUNNEL:
c12b395a 877 if (dev == ign->fb_tunnel_dev) {
878 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
879 err = -EFAULT;
880 break;
881 }
882 ip6gre_tnl_parm_from_user(&p1, &p);
883 t = ip6gre_tunnel_locate(net, &p1, 0);
63159f29 884 if (!t)
22f08069 885 t = netdev_priv(dev);
c12b395a 886 }
5dbd5068 887 memset(&p, 0, sizeof(p));
c12b395a 888 ip6gre_tnl_parm_to_user(&p, &t->parms);
889 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
890 err = -EFAULT;
891 break;
892
893 case SIOCADDTUNNEL:
894 case SIOCCHGTUNNEL:
895 err = -EPERM;
af31f412 896 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
c12b395a 897 goto done;
898
899 err = -EFAULT;
900 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
901 goto done;
902
903 err = -EINVAL;
904 if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))
905 goto done;
906
907 if (!(p.i_flags&GRE_KEY))
908 p.i_key = 0;
909 if (!(p.o_flags&GRE_KEY))
910 p.o_key = 0;
911
912 ip6gre_tnl_parm_from_user(&p1, &p);
913 t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL);
914
915 if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
53b24b8f 916 if (t) {
c12b395a 917 if (t->dev != dev) {
918 err = -EEXIST;
919 break;
920 }
921 } else {
922 t = netdev_priv(dev);
923
924 ip6gre_tunnel_unlink(ign, t);
925 synchronize_net();
926 ip6gre_tnl_change(t, &p1, 1);
927 ip6gre_tunnel_link(ign, t);
928 netdev_state_change(dev);
929 }
930 }
931
932 if (t) {
933 err = 0;
934
5dbd5068 935 memset(&p, 0, sizeof(p));
c12b395a 936 ip6gre_tnl_parm_to_user(&p, &t->parms);
937 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
938 err = -EFAULT;
939 } else
940 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
941 break;
942
943 case SIOCDELTUNNEL:
944 err = -EPERM;
af31f412 945 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
c12b395a 946 goto done;
947
948 if (dev == ign->fb_tunnel_dev) {
949 err = -EFAULT;
950 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
951 goto done;
952 err = -ENOENT;
953 ip6gre_tnl_parm_from_user(&p1, &p);
954 t = ip6gre_tunnel_locate(net, &p1, 0);
63159f29 955 if (!t)
c12b395a 956 goto done;
957 err = -EPERM;
958 if (t == netdev_priv(ign->fb_tunnel_dev))
959 goto done;
960 dev = t->dev;
961 }
962 unregister_netdevice(dev);
963 err = 0;
964 break;
965
966 default:
967 err = -EINVAL;
968 }
969
970done:
971 return err;
972}
973
c12b395a 974static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
76cc0d32
XL
975 unsigned short type, const void *daddr,
976 const void *saddr, unsigned int len)
c12b395a 977{
978 struct ip6_tnl *t = netdev_priv(dev);
76cc0d32
XL
979 struct ipv6hdr *ipv6h;
980 __be16 *p;
c12b395a 981
76cc0d32
XL
982 ipv6h = skb_push(skb, t->hlen + sizeof(*ipv6h));
983 ip6_flow_hdr(ipv6h, 0, ip6_make_flowlabel(dev_net(dev), skb,
984 t->fl.u.ip6.flowlabel,
985 true, &t->fl.u.ip6));
c12b395a 986 ipv6h->hop_limit = t->parms.hop_limit;
987 ipv6h->nexthdr = NEXTHDR_GRE;
988 ipv6h->saddr = t->parms.laddr;
989 ipv6h->daddr = t->parms.raddr;
990
76cc0d32
XL
991 p = (__be16 *)(ipv6h + 1);
992 p[0] = t->parms.o_flags;
993 p[1] = htons(type);
c12b395a 994
995 /*
996 * Set the source hardware address.
997 */
998
999 if (saddr)
1000 memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr));
1001 if (daddr)
1002 memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
1003 if (!ipv6_addr_any(&ipv6h->daddr))
1004 return t->hlen;
1005
1006 return -t->hlen;
1007}
1008
c12b395a 1009static const struct header_ops ip6gre_header_ops = {
1010 .create = ip6gre_header,
c12b395a 1011};
1012
1013static const struct net_device_ops ip6gre_netdev_ops = {
1014 .ndo_init = ip6gre_tunnel_init,
1015 .ndo_uninit = ip6gre_tunnel_uninit,
1016 .ndo_start_xmit = ip6gre_tunnel_xmit,
1017 .ndo_do_ioctl = ip6gre_tunnel_ioctl,
b05229f4 1018 .ndo_change_mtu = ip6_tnl_change_mtu,
f61dd388 1019 .ndo_get_stats64 = ip_tunnel_get_stats64,
ecf2c06a 1020 .ndo_get_iflink = ip6_tnl_get_iflink,
c12b395a 1021};
1022
1023static void ip6gre_dev_free(struct net_device *dev)
1024{
cdf3464e
MKL
1025 struct ip6_tnl *t = netdev_priv(dev);
1026
607f725f 1027 dst_cache_destroy(&t->dst_cache);
c12b395a 1028 free_percpu(dev->tstats);
c12b395a 1029}
1030
1031static void ip6gre_tunnel_setup(struct net_device *dev)
1032{
c12b395a 1033 dev->netdev_ops = &ip6gre_netdev_ops;
cf124db5
DM
1034 dev->needs_free_netdev = true;
1035 dev->priv_destructor = ip6gre_dev_free;
c12b395a 1036
1037 dev->type = ARPHRD_IP6GRE;
b05229f4 1038
c12b395a 1039 dev->flags |= IFF_NOARP;
c12b395a 1040 dev->addr_len = sizeof(struct in6_addr);
02875878 1041 netif_keep_dst(dev);
45ce0fd1
FJ
1042 /* This perm addr will be used as interface identifier by IPv6 */
1043 dev->addr_assign_type = NET_ADDR_RANDOM;
1044 eth_random_addr(dev->perm_addr);
c12b395a 1045}
1046
e5a9336a
AK
1047#define GRE6_FEATURES (NETIF_F_SG | \
1048 NETIF_F_FRAGLIST | \
1049 NETIF_F_HIGHDMA | \
1050 NETIF_F_HW_CSUM)
1051
1052static void ip6gre_tnl_init_features(struct net_device *dev)
1053{
1054 struct ip6_tnl *nt = netdev_priv(dev);
1055
1056 dev->features |= GRE6_FEATURES;
1057 dev->hw_features |= GRE6_FEATURES;
1058
1059 if (!(nt->parms.o_flags & TUNNEL_SEQ)) {
1060 /* TCP offload with GRE SEQ is not supported, nor
1061 * can we support 2 levels of outer headers requiring
1062 * an update.
1063 */
1064 if (!(nt->parms.o_flags & TUNNEL_CSUM) ||
1065 nt->encap.type == TUNNEL_ENCAP_NONE) {
1066 dev->features |= NETIF_F_GSO_SOFTWARE;
1067 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
1068 }
1069
1070 /* Can use a lockless transmit, unless we generate
1071 * output sequences
1072 */
1073 dev->features |= NETIF_F_LLTX;
1074 }
1075}
1076
a3c119d3 1077static int ip6gre_tunnel_init_common(struct net_device *dev)
c12b395a 1078{
1079 struct ip6_tnl *tunnel;
cdf3464e 1080 int ret;
b05229f4 1081 int t_hlen;
c12b395a 1082
1083 tunnel = netdev_priv(dev);
1084
1085 tunnel->dev = dev;
0bd87628 1086 tunnel->net = dev_net(dev);
c12b395a 1087 strcpy(tunnel->parms.name, dev->name);
1088
a3c119d3
MKL
1089 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1090 if (!dev->tstats)
1091 return -ENOMEM;
1092
607f725f 1093 ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
cdf3464e
MKL
1094 if (ret) {
1095 free_percpu(dev->tstats);
1096 dev->tstats = NULL;
1097 return ret;
1098 }
1099
e8d67209 1100 t_hlen = ip6gre_calc_hlen(tunnel);
db2ec95d 1101 dev->mtu = ETH_DATA_LEN - t_hlen;
1b227e53
HY
1102 if (dev->type == ARPHRD_ETHER)
1103 dev->mtu -= ETH_HLEN;
b05229f4
TH
1104 if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1105 dev->mtu -= 8;
1106
e5a9336a
AK
1107 ip6gre_tnl_init_features(dev);
1108
a3c119d3
MKL
1109 return 0;
1110}
1111
1112static int ip6gre_tunnel_init(struct net_device *dev)
1113{
1114 struct ip6_tnl *tunnel;
1115 int ret;
1116
1117 ret = ip6gre_tunnel_init_common(dev);
1118 if (ret)
1119 return ret;
1120
1121 tunnel = netdev_priv(dev);
1122
c12b395a 1123 memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr));
1124 memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr));
1125
1126 if (ipv6_addr_any(&tunnel->parms.raddr))
1127 dev->header_ops = &ip6gre_header_ops;
1128
c12b395a 1129 return 0;
1130}
1131
1132static void ip6gre_fb_tunnel_init(struct net_device *dev)
1133{
1134 struct ip6_tnl *tunnel = netdev_priv(dev);
1135
1136 tunnel->dev = dev;
0bd87628 1137 tunnel->net = dev_net(dev);
c12b395a 1138 strcpy(tunnel->parms.name, dev->name);
1139
1140 tunnel->hlen = sizeof(struct ipv6hdr) + 4;
1141
1142 dev_hold(dev);
1143}
1144
1145
1146static struct inet6_protocol ip6gre_protocol __read_mostly = {
308edfdf 1147 .handler = gre_rcv,
c12b395a 1148 .err_handler = ip6gre_err,
1149 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1150};
1151
22f08069 1152static void ip6gre_destroy_tunnels(struct net *net, struct list_head *head)
c12b395a 1153{
22f08069
ND
1154 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1155 struct net_device *dev, *aux;
c12b395a 1156 int prio;
1157
22f08069
ND
1158 for_each_netdev_safe(net, dev, aux)
1159 if (dev->rtnl_link_ops == &ip6gre_link_ops ||
1160 dev->rtnl_link_ops == &ip6gre_tap_ops)
1161 unregister_netdevice_queue(dev, head);
1162
c12b395a 1163 for (prio = 0; prio < 4; prio++) {
1164 int h;
e87a8f24 1165 for (h = 0; h < IP6_GRE_HASH_SIZE; h++) {
c12b395a 1166 struct ip6_tnl *t;
1167
1168 t = rtnl_dereference(ign->tunnels[prio][h]);
1169
53b24b8f 1170 while (t) {
22f08069
ND
1171 /* If dev is in the same netns, it has already
1172 * been added to the list by the previous loop.
1173 */
1174 if (!net_eq(dev_net(t->dev), net))
1175 unregister_netdevice_queue(t->dev,
1176 head);
c12b395a 1177 t = rtnl_dereference(t->next);
1178 }
1179 }
1180 }
1181}
1182
1183static int __net_init ip6gre_init_net(struct net *net)
1184{
1185 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1186 int err;
1187
1188 ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0",
c835a677
TG
1189 NET_NAME_UNKNOWN,
1190 ip6gre_tunnel_setup);
c12b395a 1191 if (!ign->fb_tunnel_dev) {
1192 err = -ENOMEM;
1193 goto err_alloc_dev;
1194 }
1195 dev_net_set(ign->fb_tunnel_dev, net);
22f08069
ND
1196 /* FB netdevice is special: we have one, and only one per netns.
1197 * Allowing to move it to another netns is clearly unsafe.
1198 */
1199 ign->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL;
1200
c12b395a 1201
1202 ip6gre_fb_tunnel_init(ign->fb_tunnel_dev);
1203 ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops;
1204
1205 err = register_netdev(ign->fb_tunnel_dev);
1206 if (err)
1207 goto err_reg_dev;
1208
1209 rcu_assign_pointer(ign->tunnels_wc[0],
1210 netdev_priv(ign->fb_tunnel_dev));
1211 return 0;
1212
1213err_reg_dev:
cf124db5 1214 free_netdev(ign->fb_tunnel_dev);
c12b395a 1215err_alloc_dev:
1216 return err;
1217}
1218
bb401cae 1219static void __net_exit ip6gre_exit_batch_net(struct list_head *net_list)
c12b395a 1220{
bb401cae 1221 struct net *net;
c12b395a 1222 LIST_HEAD(list);
1223
c12b395a 1224 rtnl_lock();
bb401cae
ED
1225 list_for_each_entry(net, net_list, exit_list)
1226 ip6gre_destroy_tunnels(net, &list);
c12b395a 1227 unregister_netdevice_many(&list);
1228 rtnl_unlock();
1229}
1230
1231static struct pernet_operations ip6gre_net_ops = {
1232 .init = ip6gre_init_net,
bb401cae 1233 .exit_batch = ip6gre_exit_batch_net,
c12b395a 1234 .id = &ip6gre_net_id,
1235 .size = sizeof(struct ip6gre_net),
1236};
1237
a8b8a889
MS
1238static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[],
1239 struct netlink_ext_ack *extack)
c12b395a 1240{
1241 __be16 flags;
1242
1243 if (!data)
1244 return 0;
1245
1246 flags = 0;
1247 if (data[IFLA_GRE_IFLAGS])
1248 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1249 if (data[IFLA_GRE_OFLAGS])
1250 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1251 if (flags & (GRE_VERSION|GRE_ROUTING))
1252 return -EINVAL;
1253
1254 return 0;
1255}
1256
a8b8a889
MS
1257static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[],
1258 struct netlink_ext_ack *extack)
c12b395a 1259{
1260 struct in6_addr daddr;
1261
1262 if (tb[IFLA_ADDRESS]) {
1263 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1264 return -EINVAL;
1265 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1266 return -EADDRNOTAVAIL;
1267 }
1268
1269 if (!data)
1270 goto out;
1271
1272 if (data[IFLA_GRE_REMOTE]) {
67b61f6c 1273 daddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
c12b395a 1274 if (ipv6_addr_any(&daddr))
1275 return -EINVAL;
1276 }
1277
1278out:
a8b8a889 1279 return ip6gre_tunnel_validate(tb, data, extack);
c12b395a 1280}
1281
1282
1283static void ip6gre_netlink_parms(struct nlattr *data[],
1284 struct __ip6_tnl_parm *parms)
1285{
1286 memset(parms, 0, sizeof(*parms));
1287
1288 if (!data)
1289 return;
1290
1291 if (data[IFLA_GRE_LINK])
1292 parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
1293
1294 if (data[IFLA_GRE_IFLAGS])
f41fe3c2
TH
1295 parms->i_flags = gre_flags_to_tnl_flags(
1296 nla_get_be16(data[IFLA_GRE_IFLAGS]));
c12b395a 1297
1298 if (data[IFLA_GRE_OFLAGS])
f41fe3c2
TH
1299 parms->o_flags = gre_flags_to_tnl_flags(
1300 nla_get_be16(data[IFLA_GRE_OFLAGS]));
c12b395a 1301
1302 if (data[IFLA_GRE_IKEY])
1303 parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
1304
1305 if (data[IFLA_GRE_OKEY])
1306 parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
1307
1308 if (data[IFLA_GRE_LOCAL])
67b61f6c 1309 parms->laddr = nla_get_in6_addr(data[IFLA_GRE_LOCAL]);
c12b395a 1310
1311 if (data[IFLA_GRE_REMOTE])
67b61f6c 1312 parms->raddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
c12b395a 1313
1314 if (data[IFLA_GRE_TTL])
1315 parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]);
1316
1317 if (data[IFLA_GRE_ENCAP_LIMIT])
1318 parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]);
1319
1320 if (data[IFLA_GRE_FLOWINFO])
c2675de4 1321 parms->flowinfo = nla_get_be32(data[IFLA_GRE_FLOWINFO]);
c12b395a 1322
1323 if (data[IFLA_GRE_FLAGS])
1324 parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]);
0a473b82
CG
1325
1326 if (data[IFLA_GRE_FWMARK])
1327 parms->fwmark = nla_get_u32(data[IFLA_GRE_FWMARK]);
c12b395a 1328}
1329
1330static int ip6gre_tap_init(struct net_device *dev)
1331{
a3c119d3 1332 int ret;
c12b395a 1333
a3c119d3
MKL
1334 ret = ip6gre_tunnel_init_common(dev);
1335 if (ret)
1336 return ret;
c12b395a 1337
0a46baaf
SC
1338 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1339
c12b395a 1340 return 0;
1341}
1342
1343static const struct net_device_ops ip6gre_tap_netdev_ops = {
1344 .ndo_init = ip6gre_tap_init,
1345 .ndo_uninit = ip6gre_tunnel_uninit,
1346 .ndo_start_xmit = ip6gre_tunnel_xmit,
1347 .ndo_set_mac_address = eth_mac_addr,
1348 .ndo_validate_addr = eth_validate_addr,
b05229f4 1349 .ndo_change_mtu = ip6_tnl_change_mtu,
f61dd388 1350 .ndo_get_stats64 = ip_tunnel_get_stats64,
ecf2c06a 1351 .ndo_get_iflink = ip6_tnl_get_iflink,
c12b395a 1352};
1353
1354static void ip6gre_tap_setup(struct net_device *dev)
1355{
1356
1357 ether_setup(dev);
1358
2c52129a 1359 dev->max_mtu = 0;
c12b395a 1360 dev->netdev_ops = &ip6gre_tap_netdev_ops;
cf124db5
DM
1361 dev->needs_free_netdev = true;
1362 dev->priv_destructor = ip6gre_dev_free;
c12b395a 1363
c12b395a 1364 dev->features |= NETIF_F_NETNS_LOCAL;
d13b161c 1365 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
0a46baaf 1366 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2d40557c 1367 netif_keep_dst(dev);
c12b395a 1368}
1369
1faf3d9f
TH
1370static bool ip6gre_netlink_encap_parms(struct nlattr *data[],
1371 struct ip_tunnel_encap *ipencap)
1372{
1373 bool ret = false;
1374
1375 memset(ipencap, 0, sizeof(*ipencap));
1376
1377 if (!data)
1378 return ret;
1379
1380 if (data[IFLA_GRE_ENCAP_TYPE]) {
1381 ret = true;
1382 ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]);
1383 }
1384
1385 if (data[IFLA_GRE_ENCAP_FLAGS]) {
1386 ret = true;
1387 ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]);
1388 }
1389
1390 if (data[IFLA_GRE_ENCAP_SPORT]) {
1391 ret = true;
1392 ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]);
1393 }
1394
1395 if (data[IFLA_GRE_ENCAP_DPORT]) {
1396 ret = true;
1397 ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]);
1398 }
1399
1400 return ret;
1401}
1402
cda5a758
PM
1403static int ip6gre_newlink_common(struct net *src_net, struct net_device *dev,
1404 struct nlattr *tb[], struct nlattr *data[],
1405 struct netlink_ext_ack *extack)
c12b395a 1406{
1407 struct ip6_tnl *nt;
1408 struct net *net = dev_net(dev);
1faf3d9f 1409 struct ip_tunnel_encap ipencap;
c12b395a 1410 int err;
1411
1412 nt = netdev_priv(dev);
1faf3d9f
TH
1413
1414 if (ip6gre_netlink_encap_parms(data, &ipencap)) {
1415 int err = ip6_tnl_encap_setup(nt, &ipencap);
1416
1417 if (err < 0)
1418 return err;
1419 }
1420
c12b395a 1421 ip6gre_netlink_parms(data, &nt->parms);
1422
1423 if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
1424 return -EEXIST;
1425
1426 if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
1427 eth_hw_addr_random(dev);
1428
1429 nt->dev = dev;
0bd87628 1430 nt->net = dev_net(dev);
c12b395a 1431
c12b395a 1432 err = register_netdevice(dev);
1433 if (err)
1434 goto out;
1435
128bb975
AK
1436 if (tb[IFLA_MTU])
1437 ip6_tnl_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1438
c12b395a 1439 dev_hold(dev);
c12b395a 1440
1441out:
1442 return err;
cda5a758
PM
1443}
1444
1445static int ip6gre_newlink(struct net *src_net, struct net_device *dev,
1446 struct nlattr *tb[], struct nlattr *data[],
1447 struct netlink_ext_ack *extack)
1448{
1449 int err = ip6gre_newlink_common(src_net, dev, tb, data, extack);
1450 struct ip6_tnl *nt = netdev_priv(dev);
1451 struct net *net = dev_net(dev);
1452
1453 if (!err) {
1454 ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]);
1455 ip6gre_tunnel_link(net_generic(net, ip6gre_net_id), nt);
1456 }
1457 return err;
c12b395a 1458}
1459
6c35dfed
PM
1460static struct ip6_tnl *
1461ip6gre_changelink_common(struct net_device *dev, struct nlattr *tb[],
1462 struct nlattr *data[], struct __ip6_tnl_parm *p_p,
1463 struct netlink_ext_ack *extack)
c12b395a 1464{
22f08069
ND
1465 struct ip6_tnl *t, *nt = netdev_priv(dev);
1466 struct net *net = nt->net;
c12b395a 1467 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1faf3d9f 1468 struct ip_tunnel_encap ipencap;
c12b395a 1469
1470 if (dev == ign->fb_tunnel_dev)
6c35dfed 1471 return ERR_PTR(-EINVAL);
c12b395a 1472
1faf3d9f
TH
1473 if (ip6gre_netlink_encap_parms(data, &ipencap)) {
1474 int err = ip6_tnl_encap_setup(nt, &ipencap);
1475
1476 if (err < 0)
6c35dfed 1477 return ERR_PTR(err);
1faf3d9f
TH
1478 }
1479
6c35dfed 1480 ip6gre_netlink_parms(data, p_p);
c12b395a 1481
6c35dfed 1482 t = ip6gre_tunnel_locate(net, p_p, 0);
c12b395a 1483
1484 if (t) {
1485 if (t->dev != dev)
6c35dfed 1486 return ERR_PTR(-EEXIST);
c12b395a 1487 } else {
1488 t = nt;
c12b395a 1489 }
1490
6c35dfed
PM
1491 return t;
1492}
1493
1494static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],
1495 struct nlattr *data[],
1496 struct netlink_ext_ack *extack)
1497{
7b7d41fb
OM
1498 struct ip6_tnl *t = netdev_priv(dev);
1499 struct ip6gre_net *ign = net_generic(t->net, ip6gre_net_id);
6c35dfed 1500 struct __ip6_tnl_parm p;
6c35dfed
PM
1501
1502 t = ip6gre_changelink_common(dev, tb, data, &p, extack);
1503 if (IS_ERR(t))
1504 return PTR_ERR(t);
1505
6a61d4db
ND
1506 ip6gre_tunnel_unlink(ign, t);
1507 ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]);
1508 ip6gre_tunnel_link(ign, t);
c12b395a 1509 return 0;
1510}
1511
54d63f78
ND
1512static void ip6gre_dellink(struct net_device *dev, struct list_head *head)
1513{
1514 struct net *net = dev_net(dev);
1515 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1516
1517 if (dev != ign->fb_tunnel_dev)
1518 unregister_netdevice_queue(dev, head);
1519}
1520
c12b395a 1521static size_t ip6gre_get_size(const struct net_device *dev)
1522{
1523 return
1524 /* IFLA_GRE_LINK */
1525 nla_total_size(4) +
1526 /* IFLA_GRE_IFLAGS */
1527 nla_total_size(2) +
1528 /* IFLA_GRE_OFLAGS */
1529 nla_total_size(2) +
1530 /* IFLA_GRE_IKEY */
1531 nla_total_size(4) +
1532 /* IFLA_GRE_OKEY */
1533 nla_total_size(4) +
1534 /* IFLA_GRE_LOCAL */
a3754133 1535 nla_total_size(sizeof(struct in6_addr)) +
c12b395a 1536 /* IFLA_GRE_REMOTE */
a3754133 1537 nla_total_size(sizeof(struct in6_addr)) +
c12b395a 1538 /* IFLA_GRE_TTL */
1539 nla_total_size(1) +
c12b395a 1540 /* IFLA_GRE_ENCAP_LIMIT */
1541 nla_total_size(1) +
1542 /* IFLA_GRE_FLOWINFO */
1543 nla_total_size(4) +
1544 /* IFLA_GRE_FLAGS */
1545 nla_total_size(4) +
1faf3d9f
TH
1546 /* IFLA_GRE_ENCAP_TYPE */
1547 nla_total_size(2) +
1548 /* IFLA_GRE_ENCAP_FLAGS */
1549 nla_total_size(2) +
1550 /* IFLA_GRE_ENCAP_SPORT */
1551 nla_total_size(2) +
1552 /* IFLA_GRE_ENCAP_DPORT */
1553 nla_total_size(2) +
0a473b82
CG
1554 /* IFLA_GRE_FWMARK */
1555 nla_total_size(4) +
c12b395a 1556 0;
1557}
1558
1559static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev)
1560{
1561 struct ip6_tnl *t = netdev_priv(dev);
1562 struct __ip6_tnl_parm *p = &t->parms;
1563
1564 if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
f41fe3c2
TH
1565 nla_put_be16(skb, IFLA_GRE_IFLAGS,
1566 gre_tnl_flags_to_gre_flags(p->i_flags)) ||
1567 nla_put_be16(skb, IFLA_GRE_OFLAGS,
1568 gre_tnl_flags_to_gre_flags(p->o_flags)) ||
c12b395a 1569 nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
1570 nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
930345ea
JB
1571 nla_put_in6_addr(skb, IFLA_GRE_LOCAL, &p->laddr) ||
1572 nla_put_in6_addr(skb, IFLA_GRE_REMOTE, &p->raddr) ||
c12b395a 1573 nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) ||
c12b395a 1574 nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) ||
1575 nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) ||
0a473b82
CG
1576 nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags) ||
1577 nla_put_u32(skb, IFLA_GRE_FWMARK, p->fwmark))
c12b395a 1578 goto nla_put_failure;
1faf3d9f
TH
1579
1580 if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE,
1581 t->encap.type) ||
1582 nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT,
1583 t->encap.sport) ||
1584 nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT,
1585 t->encap.dport) ||
1586 nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS,
1587 t->encap.flags))
1588 goto nla_put_failure;
1589
c12b395a 1590 return 0;
1591
1592nla_put_failure:
1593 return -EMSGSIZE;
1594}
1595
1596static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = {
1597 [IFLA_GRE_LINK] = { .type = NLA_U32 },
1598 [IFLA_GRE_IFLAGS] = { .type = NLA_U16 },
1599 [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
1600 [IFLA_GRE_IKEY] = { .type = NLA_U32 },
1601 [IFLA_GRE_OKEY] = { .type = NLA_U32 },
1602 [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct ipv6hdr, saddr) },
1603 [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct ipv6hdr, daddr) },
1604 [IFLA_GRE_TTL] = { .type = NLA_U8 },
1605 [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 },
1606 [IFLA_GRE_FLOWINFO] = { .type = NLA_U32 },
1607 [IFLA_GRE_FLAGS] = { .type = NLA_U32 },
1faf3d9f
TH
1608 [IFLA_GRE_ENCAP_TYPE] = { .type = NLA_U16 },
1609 [IFLA_GRE_ENCAP_FLAGS] = { .type = NLA_U16 },
1610 [IFLA_GRE_ENCAP_SPORT] = { .type = NLA_U16 },
1611 [IFLA_GRE_ENCAP_DPORT] = { .type = NLA_U16 },
0a473b82 1612 [IFLA_GRE_FWMARK] = { .type = NLA_U32 },
c12b395a 1613};
1614
1615static struct rtnl_link_ops ip6gre_link_ops __read_mostly = {
1616 .kind = "ip6gre",
1617 .maxtype = IFLA_GRE_MAX,
1618 .policy = ip6gre_policy,
1619 .priv_size = sizeof(struct ip6_tnl),
1620 .setup = ip6gre_tunnel_setup,
1621 .validate = ip6gre_tunnel_validate,
1622 .newlink = ip6gre_newlink,
1623 .changelink = ip6gre_changelink,
54d63f78 1624 .dellink = ip6gre_dellink,
c12b395a 1625 .get_size = ip6gre_get_size,
1626 .fill_info = ip6gre_fill_info,
1728d4fa 1627 .get_link_net = ip6_tnl_get_link_net,
c12b395a 1628};
1629
1630static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = {
1631 .kind = "ip6gretap",
1632 .maxtype = IFLA_GRE_MAX,
1633 .policy = ip6gre_policy,
1634 .priv_size = sizeof(struct ip6_tnl),
1635 .setup = ip6gre_tap_setup,
1636 .validate = ip6gre_tap_validate,
1637 .newlink = ip6gre_newlink,
1638 .changelink = ip6gre_changelink,
1639 .get_size = ip6gre_get_size,
1640 .fill_info = ip6gre_fill_info,
3390e397 1641 .get_link_net = ip6_tnl_get_link_net,
c12b395a 1642};
1643
1644/*
1645 * And now the modules code and kernel interface.
1646 */
1647
1648static int __init ip6gre_init(void)
1649{
1650 int err;
1651
1652 pr_info("GRE over IPv6 tunneling driver\n");
1653
1654 err = register_pernet_device(&ip6gre_net_ops);
1655 if (err < 0)
1656 return err;
1657
1658 err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE);
1659 if (err < 0) {
1660 pr_info("%s: can't add protocol\n", __func__);
1661 goto add_proto_failed;
1662 }
1663
1664 err = rtnl_link_register(&ip6gre_link_ops);
1665 if (err < 0)
1666 goto rtnl_link_failed;
1667
1668 err = rtnl_link_register(&ip6gre_tap_ops);
1669 if (err < 0)
1670 goto tap_ops_failed;
1671
1672out:
1673 return err;
1674
1675tap_ops_failed:
1676 rtnl_link_unregister(&ip6gre_link_ops);
1677rtnl_link_failed:
1678 inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
1679add_proto_failed:
1680 unregister_pernet_device(&ip6gre_net_ops);
1681 goto out;
1682}
1683
1684static void __exit ip6gre_fini(void)
1685{
1686 rtnl_link_unregister(&ip6gre_tap_ops);
1687 rtnl_link_unregister(&ip6gre_link_ops);
1688 inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
1689 unregister_pernet_device(&ip6gre_net_ops);
1690}
1691
1692module_init(ip6gre_init);
1693module_exit(ip6gre_fini);
1694MODULE_LICENSE("GPL");
1695MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
1696MODULE_DESCRIPTION("GRE over IPv6 tunneling device");
1697MODULE_ALIAS_RTNL_LINK("ip6gre");
5a4ee9a9 1698MODULE_ALIAS_RTNL_LINK("ip6gretap");
c12b395a 1699MODULE_ALIAS_NETDEV("ip6gre0");