1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * GRE over IPv6 protocol decoder.
4 *
5 * Authors: Dmitry Kozlov (xeb@mail.ru)
6 */
7
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10 #include <linux/capability.h>
11 #include <linux/module.h>
12 #include <linux/types.h>
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/uaccess.h>
16 #include <linux/skbuff.h>
17 #include <linux/netdevice.h>
18 #include <linux/in.h>
19 #include <linux/tcp.h>
20 #include <linux/udp.h>
21 #include <linux/if_arp.h>
22 #include <linux/init.h>
23 #include <linux/in6.h>
24 #include <linux/inetdevice.h>
25 #include <linux/igmp.h>
26 #include <linux/netfilter_ipv4.h>
27 #include <linux/etherdevice.h>
28 #include <linux/if_ether.h>
29 #include <linux/hash.h>
30 #include <linux/if_tunnel.h>
31 #include <linux/ip6_tunnel.h>
32
33 #include <net/sock.h>
34 #include <net/ip.h>
35 #include <net/ip_tunnels.h>
36 #include <net/icmp.h>
37 #include <net/protocol.h>
38 #include <net/addrconf.h>
39 #include <net/arp.h>
40 #include <net/checksum.h>
41 #include <net/dsfield.h>
42 #include <net/inet_ecn.h>
43 #include <net/xfrm.h>
44 #include <net/net_namespace.h>
45 #include <net/netns/generic.h>
46 #include <net/netdev_lock.h>
47 #include <net/rtnetlink.h>
48
49 #include <net/ipv6.h>
50 #include <net/ip6_fib.h>
51 #include <net/ip6_route.h>
52 #include <net/ip6_tunnel.h>
53 #include <net/gre.h>
54 #include <net/erspan.h>
55 #include <net/dst_metadata.h>
56
57
58 static bool log_ecn_error = true;
59 module_param(log_ecn_error, bool, 0644);
60 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
61
62 #define IP6_GRE_HASH_SIZE_SHIFT 5
63 #define IP6_GRE_HASH_SIZE (1 << IP6_GRE_HASH_SIZE_SHIFT)
64
65 static unsigned int ip6gre_net_id __read_mostly;
66 struct ip6gre_net {
67 struct ip6_tnl __rcu *tunnels[4][IP6_GRE_HASH_SIZE];
68
69 struct ip6_tnl __rcu *collect_md_tun;
70 struct ip6_tnl __rcu *collect_md_tun_erspan;
71 struct net_device *fb_tunnel_dev;
72 };
73
74 static struct rtnl_link_ops ip6gre_link_ops __read_mostly;
75 static struct rtnl_link_ops ip6gre_tap_ops __read_mostly;
76 static struct rtnl_link_ops ip6erspan_tap_ops __read_mostly;
77 static int ip6gre_tunnel_init(struct net_device *dev);
78 static void ip6gre_tunnel_setup(struct net_device *dev);
79 static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t);
80 static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu);
81 static void ip6erspan_tnl_link_config(struct ip6_tnl *t, int set_mtu);
82
83 /* Tunnel hash table */
84
85 /*
86 4 hash tables:
87
88 3: (remote,local)
89 2: (remote,*)
90 1: (*,local)
91 0: (*,*)
92
93 We require exact key match i.e. if a key is present in packet
94 it will match only tunnel with the same key; if it is not present,
95 it will match only keyless tunnel.
96
97 All keysless packets, if not matched configured keyless tunnels
98 will match fallback tunnel.
99 */
100
101 #define HASH_KEY(key) (((__force u32)key^((__force u32)key>>4))&(IP6_GRE_HASH_SIZE - 1))
HASH_ADDR(const struct in6_addr * addr)102 static u32 HASH_ADDR(const struct in6_addr *addr)
103 {
104 u32 hash = ipv6_addr_hash(addr);
105
106 return hash_32(hash, IP6_GRE_HASH_SIZE_SHIFT);
107 }
108
109 #define tunnels_r_l tunnels[3]
110 #define tunnels_r tunnels[2]
111 #define tunnels_l tunnels[1]
112 #define tunnels_wc tunnels[0]
113
ip6gre_tunnel_match(struct ip6_tnl * t,int dev_type,int link,int * cand_score,struct ip6_tnl ** ret)114 static bool ip6gre_tunnel_match(struct ip6_tnl *t, int dev_type, int link,
115 int *cand_score, struct ip6_tnl **ret)
116 {
117 int score = 0;
118
119 if (t->dev->type != ARPHRD_IP6GRE &&
120 t->dev->type != dev_type)
121 return false;
122
123 if (t->parms.link != link)
124 score |= 1;
125 if (t->dev->type != dev_type)
126 score |= 2;
127 if (score == 0) {
128 *ret = t;
129 return true;
130 }
131
132 if (score < *cand_score) {
133 *ret = t;
134 *cand_score = score;
135 }
136 return false;
137 }
138
139 /* Given src, dst and key, find appropriate for input tunnel. */
ip6gre_tunnel_lookup(struct net_device * dev,const struct in6_addr * remote,const struct in6_addr * local,__be32 key,__be16 gre_proto)140 static struct ip6_tnl *ip6gre_tunnel_lookup(struct net_device *dev,
141 const struct in6_addr *remote, const struct in6_addr *local,
142 __be32 key, __be16 gre_proto)
143 {
144 struct net *net = dev_net(dev);
145 int link = dev->ifindex;
146 unsigned int h0 = HASH_ADDR(remote);
147 unsigned int h1 = HASH_KEY(key);
148 struct ip6_tnl *t, *cand = NULL;
149 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
150 int dev_type = (gre_proto == htons(ETH_P_TEB) ||
151 gre_proto == htons(ETH_P_ERSPAN) ||
152 gre_proto == htons(ETH_P_ERSPAN2)) ?
153 ARPHRD_ETHER : ARPHRD_IP6GRE;
154 struct net_device *ndev;
155 int cand_score = 4;
156
157 for_each_ip_tunnel_rcu(t, ign->tunnels_r_l[h0 ^ h1]) {
158 if (!ipv6_addr_equal(local, &t->parms.laddr) ||
159 !ipv6_addr_equal(remote, &t->parms.raddr) ||
160 key != t->parms.i_key ||
161 !(t->dev->flags & IFF_UP))
162 continue;
163
164 if (ip6gre_tunnel_match(t, dev_type, link, &cand_score, &cand))
165 return cand;
166 }
167
168 for_each_ip_tunnel_rcu(t, ign->tunnels_r[h0 ^ h1]) {
169 if (!ipv6_addr_equal(remote, &t->parms.raddr) ||
170 key != t->parms.i_key ||
171 !(t->dev->flags & IFF_UP))
172 continue;
173
174 if (ip6gre_tunnel_match(t, dev_type, link, &cand_score, &cand))
175 return cand;
176 }
177
178 for_each_ip_tunnel_rcu(t, ign->tunnels_l[h1]) {
179 if ((!ipv6_addr_equal(local, &t->parms.laddr) &&
180 (!ipv6_addr_equal(local, &t->parms.raddr) ||
181 !ipv6_addr_is_multicast(local))) ||
182 key != t->parms.i_key ||
183 !(t->dev->flags & IFF_UP))
184 continue;
185
186 if (ip6gre_tunnel_match(t, dev_type, link, &cand_score, &cand))
187 return cand;
188 }
189
190 for_each_ip_tunnel_rcu(t, ign->tunnels_wc[h1]) {
191 if (t->parms.i_key != key ||
192 !(t->dev->flags & IFF_UP))
193 continue;
194
195 if (ip6gre_tunnel_match(t, dev_type, link, &cand_score, &cand))
196 return cand;
197 }
198
199 if (cand)
200 return cand;
201
202 if (gre_proto == htons(ETH_P_ERSPAN) ||
203 gre_proto == htons(ETH_P_ERSPAN2))
204 t = rcu_dereference(ign->collect_md_tun_erspan);
205 else
206 t = rcu_dereference(ign->collect_md_tun);
207
208 if (t && t->dev->flags & IFF_UP)
209 return t;
210
211 ndev = READ_ONCE(ign->fb_tunnel_dev);
212 if (ndev && ndev->flags & IFF_UP)
213 return netdev_priv(ndev);
214
215 return NULL;
216 }
217
__ip6gre_bucket(struct ip6gre_net * ign,const struct __ip6_tnl_parm * p)218 static struct ip6_tnl __rcu **__ip6gre_bucket(struct ip6gre_net *ign,
219 const struct __ip6_tnl_parm *p)
220 {
221 const struct in6_addr *remote = &p->raddr;
222 const struct in6_addr *local = &p->laddr;
223 unsigned int h = HASH_KEY(p->i_key);
224 int prio = 0;
225
226 if (!ipv6_addr_any(local))
227 prio |= 1;
228 if (!ipv6_addr_any(remote) && !ipv6_addr_is_multicast(remote)) {
229 prio |= 2;
230 h ^= HASH_ADDR(remote);
231 }
232
233 return &ign->tunnels[prio][h];
234 }
235
ip6gre_tunnel_link_md(struct ip6gre_net * ign,struct ip6_tnl * t)236 static void ip6gre_tunnel_link_md(struct ip6gre_net *ign, struct ip6_tnl *t)
237 {
238 if (t->parms.collect_md)
239 rcu_assign_pointer(ign->collect_md_tun, t);
240 }
241
ip6erspan_tunnel_link_md(struct ip6gre_net * ign,struct ip6_tnl * t)242 static void ip6erspan_tunnel_link_md(struct ip6gre_net *ign, struct ip6_tnl *t)
243 {
244 if (t->parms.collect_md)
245 rcu_assign_pointer(ign->collect_md_tun_erspan, t);
246 }
247
ip6gre_tunnel_unlink_md(struct ip6gre_net * ign,struct ip6_tnl * t)248 static void ip6gre_tunnel_unlink_md(struct ip6gre_net *ign, struct ip6_tnl *t)
249 {
250 if (t->parms.collect_md)
251 rcu_assign_pointer(ign->collect_md_tun, NULL);
252 }
253
ip6erspan_tunnel_unlink_md(struct ip6gre_net * ign,struct ip6_tnl * t)254 static void ip6erspan_tunnel_unlink_md(struct ip6gre_net *ign,
255 struct ip6_tnl *t)
256 {
257 if (t->parms.collect_md)
258 rcu_assign_pointer(ign->collect_md_tun_erspan, NULL);
259 }
260
ip6gre_bucket(struct ip6gre_net * ign,const struct ip6_tnl * t)261 static inline struct ip6_tnl __rcu **ip6gre_bucket(struct ip6gre_net *ign,
262 const struct ip6_tnl *t)
263 {
264 return __ip6gre_bucket(ign, &t->parms);
265 }
266
ip6gre_tunnel_link(struct ip6gre_net * ign,struct ip6_tnl * t)267 static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t)
268 {
269 struct ip6_tnl __rcu **tp = ip6gre_bucket(ign, t);
270
271 rcu_assign_pointer(t->next, rtnl_dereference(*tp));
272 rcu_assign_pointer(*tp, t);
273 }
274
ip6gre_tunnel_unlink(struct ip6gre_net * ign,struct ip6_tnl * t)275 static void ip6gre_tunnel_unlink(struct ip6gre_net *ign, struct ip6_tnl *t)
276 {
277 struct ip6_tnl __rcu **tp;
278 struct ip6_tnl *iter;
279
280 for (tp = ip6gre_bucket(ign, t);
281 (iter = rtnl_dereference(*tp)) != NULL;
282 tp = &iter->next) {
283 if (t == iter) {
284 rcu_assign_pointer(*tp, t->next);
285 break;
286 }
287 }
288 }
289
ip6gre_tunnel_find(struct net * net,const struct __ip6_tnl_parm * parms,int type)290 static struct ip6_tnl *ip6gre_tunnel_find(struct net *net,
291 const struct __ip6_tnl_parm *parms,
292 int type)
293 {
294 const struct in6_addr *remote = &parms->raddr;
295 const struct in6_addr *local = &parms->laddr;
296 __be32 key = parms->i_key;
297 int link = parms->link;
298 struct ip6_tnl *t;
299 struct ip6_tnl __rcu **tp;
300 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
301
302 for (tp = __ip6gre_bucket(ign, parms);
303 (t = rtnl_dereference(*tp)) != NULL;
304 tp = &t->next)
305 if (ipv6_addr_equal(local, &t->parms.laddr) &&
306 ipv6_addr_equal(remote, &t->parms.raddr) &&
307 key == t->parms.i_key &&
308 link == t->parms.link &&
309 type == t->dev->type)
310 break;
311
312 return t;
313 }
314
ip6gre_tunnel_locate(struct net * net,const struct __ip6_tnl_parm * parms,int create)315 static struct ip6_tnl *ip6gre_tunnel_locate(struct net *net,
316 const struct __ip6_tnl_parm *parms, int create)
317 {
318 struct ip6_tnl *t, *nt;
319 struct net_device *dev;
320 char name[IFNAMSIZ];
321 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
322
323 t = ip6gre_tunnel_find(net, parms, ARPHRD_IP6GRE);
324 if (t && create)
325 return NULL;
326 if (t || !create)
327 return t;
328
329 if (parms->name[0]) {
330 if (!dev_valid_name(parms->name))
331 return NULL;
332 strscpy(name, parms->name);
333 } else {
334 strscpy(name, "ip6gre%d");
335 }
336 dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
337 ip6gre_tunnel_setup);
338 if (!dev)
339 return NULL;
340
341 dev_net_set(dev, net);
342
343 nt = netdev_priv(dev);
344 nt->parms = *parms;
345 dev->rtnl_link_ops = &ip6gre_link_ops;
346
347 nt->dev = dev;
348 nt->net = dev_net(dev);
349
350 if (register_netdevice(dev) < 0)
351 goto failed_free;
352
353 ip6gre_tnl_link_config(nt, 1);
354 ip6gre_tunnel_link(ign, nt);
355 return nt;
356
357 failed_free:
358 free_netdev(dev);
359 return NULL;
360 }
361
ip6erspan_tunnel_uninit(struct net_device * dev)362 static void ip6erspan_tunnel_uninit(struct net_device *dev)
363 {
364 struct ip6_tnl *t = netdev_priv(dev);
365 struct ip6gre_net *ign = net_generic(t->net, ip6gre_net_id);
366
367 ip6erspan_tunnel_unlink_md(ign, t);
368 ip6gre_tunnel_unlink(ign, t);
369 dst_cache_reset(&t->dst_cache);
370 netdev_put(dev, &t->dev_tracker);
371 }
372
ip6gre_tunnel_uninit(struct net_device * dev)373 static void ip6gre_tunnel_uninit(struct net_device *dev)
374 {
375 struct ip6_tnl *t = netdev_priv(dev);
376 struct ip6gre_net *ign = net_generic(t->net, ip6gre_net_id);
377
378 ip6gre_tunnel_unlink_md(ign, t);
379 ip6gre_tunnel_unlink(ign, t);
380 if (ign->fb_tunnel_dev == dev)
381 WRITE_ONCE(ign->fb_tunnel_dev, NULL);
382 dst_cache_reset(&t->dst_cache);
383 netdev_put(dev, &t->dev_tracker);
384 }
385
386
ip6gre_err(struct sk_buff * skb,struct inet6_skb_parm * opt,u8 type,u8 code,int offset,__be32 info)387 static int ip6gre_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
388 u8 type, u8 code, int offset, __be32 info)
389 {
390 struct net *net = dev_net(skb->dev);
391 const struct ipv6hdr *ipv6h;
392 struct tnl_ptk_info tpi;
393 struct ip6_tnl *t;
394
395 if (gre_parse_header(skb, &tpi, NULL, htons(ETH_P_IPV6),
396 offset) < 0)
397 return -EINVAL;
398
399 ipv6h = (const struct ipv6hdr *)skb->data;
400 t = ip6gre_tunnel_lookup(skb->dev, &ipv6h->daddr, &ipv6h->saddr,
401 tpi.key, tpi.proto);
402 if (!t)
403 return -ENOENT;
404
405 switch (type) {
406 case ICMPV6_DEST_UNREACH:
407 net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n",
408 t->parms.name);
409 if (code != ICMPV6_PORT_UNREACH)
410 break;
411 return 0;
412 case ICMPV6_TIME_EXCEED:
413 if (code == ICMPV6_EXC_HOPLIMIT) {
414 net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
415 t->parms.name);
416 break;
417 }
418 return 0;
419 case ICMPV6_PARAMPROB: {
420 struct ipv6_tlv_tnl_enc_lim *tel;
421 __u32 teli;
422
423 teli = 0;
424 if (code == ICMPV6_HDR_FIELD)
425 teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
426
427 if (teli && teli == be32_to_cpu(info) - 2) {
428 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
429 if (tel->encap_limit == 0) {
430 net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
431 t->parms.name);
432 }
433 } else {
434 net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
435 t->parms.name);
436 }
437 return 0;
438 }
439 case ICMPV6_PKT_TOOBIG:
440 ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
441 return 0;
442 case NDISC_REDIRECT:
443 ip6_redirect(skb, net, skb->dev->ifindex, 0,
444 sock_net_uid(net, NULL));
445 return 0;
446 }
447
448 if (time_before(jiffies, READ_ONCE(t->err_time) + IP6TUNNEL_ERR_TIMEO))
449 WRITE_ONCE(t->err_count, READ_ONCE(t->err_count) + 1);
450 else
451 WRITE_ONCE(t->err_count, 1);
452 WRITE_ONCE(t->err_time, jiffies);
453
454 return 0;
455 }
456
ip6gre_rcv(struct sk_buff * skb,const struct tnl_ptk_info * tpi)457 static int ip6gre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi)
458 {
459 const struct ipv6hdr *ipv6h;
460 struct ip6_tnl *tunnel;
461
462 ipv6h = ipv6_hdr(skb);
463 tunnel = ip6gre_tunnel_lookup(skb->dev,
464 &ipv6h->saddr, &ipv6h->daddr, tpi->key,
465 tpi->proto);
466 if (tunnel) {
467 if (tunnel->parms.collect_md) {
468 IP_TUNNEL_DECLARE_FLAGS(flags);
469 struct metadata_dst *tun_dst;
470 __be64 tun_id;
471
472 ip_tunnel_flags_copy(flags, tpi->flags);
473 tun_id = key32_to_tunnel_id(tpi->key);
474
475 tun_dst = ipv6_tun_rx_dst(skb, flags, tun_id, 0);
476 if (!tun_dst)
477 return PACKET_REJECT;
478
479 ip6_tnl_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
480 } else {
481 ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error);
482 }
483
484 return PACKET_RCVD;
485 }
486
487 return PACKET_REJECT;
488 }
489
ip6erspan_rcv(struct sk_buff * skb,struct tnl_ptk_info * tpi,int gre_hdr_len)490 static int ip6erspan_rcv(struct sk_buff *skb,
491 struct tnl_ptk_info *tpi,
492 int gre_hdr_len)
493 {
494 struct erspan_base_hdr *ershdr;
495 const struct ipv6hdr *ipv6h;
496 struct erspan_md2 *md2;
497 struct ip6_tnl *tunnel;
498 u8 ver;
499
500 if (unlikely(!pskb_may_pull(skb, sizeof(*ershdr))))
501 return PACKET_REJECT;
502
503 ipv6h = ipv6_hdr(skb);
504 ershdr = (struct erspan_base_hdr *)skb->data;
505 ver = ershdr->ver;
506
507 tunnel = ip6gre_tunnel_lookup(skb->dev,
508 &ipv6h->saddr, &ipv6h->daddr, tpi->key,
509 tpi->proto);
510 if (tunnel) {
511 int len = erspan_hdr_len(ver);
512
513 if (unlikely(!pskb_may_pull(skb, len)))
514 return PACKET_REJECT;
515
516 if (__iptunnel_pull_header(skb, len,
517 htons(ETH_P_TEB),
518 false, false) < 0)
519 return PACKET_REJECT;
520
521 if (tunnel->parms.collect_md) {
522 struct erspan_metadata *pkt_md, *md;
523 IP_TUNNEL_DECLARE_FLAGS(flags);
524 struct metadata_dst *tun_dst;
525 struct ip_tunnel_info *info;
526 unsigned char *gh;
527 __be64 tun_id;
528
529 __set_bit(IP_TUNNEL_KEY_BIT, tpi->flags);
530 ip_tunnel_flags_copy(flags, tpi->flags);
531 tun_id = key32_to_tunnel_id(tpi->key);
532
533 tun_dst = ipv6_tun_rx_dst(skb, flags, tun_id,
534 sizeof(*md));
535 if (!tun_dst)
536 return PACKET_REJECT;
537
538 /* MUST set options_len before referencing options */
539 info = &tun_dst->u.tun_info;
540 info->options_len = sizeof(*md);
541
542 /* skb can be uncloned in __iptunnel_pull_header, so
543 * old pkt_md is no longer valid and we need to reset
544 * it
545 */
546 gh = skb_network_header(skb) +
547 skb_network_header_len(skb);
548 pkt_md = (struct erspan_metadata *)(gh + gre_hdr_len +
549 sizeof(*ershdr));
550 md = ip_tunnel_info_opts(info);
551 md->version = ver;
552 md2 = &md->u.md2;
553 memcpy(md2, pkt_md, ver == 1 ? ERSPAN_V1_MDSIZE :
554 ERSPAN_V2_MDSIZE);
555 __set_bit(IP_TUNNEL_ERSPAN_OPT_BIT,
556 info->key.tun_flags);
557
558 ip6_tnl_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
559
560 } else {
561 ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error);
562 }
563
564 return PACKET_RCVD;
565 }
566
567 return PACKET_REJECT;
568 }
569
gre_rcv(struct sk_buff * skb)570 static int gre_rcv(struct sk_buff *skb)
571 {
572 struct tnl_ptk_info tpi;
573 bool csum_err = false;
574 int hdr_len;
575
576 hdr_len = gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IPV6), 0);
577 if (hdr_len < 0)
578 goto drop;
579
580 if (iptunnel_pull_header(skb, hdr_len, tpi.proto, false))
581 goto drop;
582
583 if (unlikely(tpi.proto == htons(ETH_P_ERSPAN) ||
584 tpi.proto == htons(ETH_P_ERSPAN2))) {
585 if (ip6erspan_rcv(skb, &tpi, hdr_len) == PACKET_RCVD)
586 return 0;
587 goto out;
588 }
589
590 if (ip6gre_rcv(skb, &tpi) == PACKET_RCVD)
591 return 0;
592
593 out:
594 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
595 drop:
596 dev_core_stats_rx_dropped_inc(skb->dev);
597 kfree_skb(skb);
598 return 0;
599 }
600
gre_handle_offloads(struct sk_buff * skb,bool csum)601 static int gre_handle_offloads(struct sk_buff *skb, bool csum)
602 {
603 return iptunnel_handle_offloads(skb,
604 csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE);
605 }
606
prepare_ip6gre_xmit_ipv4(struct sk_buff * skb,struct net_device * dev,struct flowi6 * fl6,__u8 * dsfield,int * encap_limit)607 static void prepare_ip6gre_xmit_ipv4(struct sk_buff *skb,
608 struct net_device *dev,
609 struct flowi6 *fl6, __u8 *dsfield,
610 int *encap_limit)
611 {
612 const struct iphdr *iph = ip_hdr(skb);
613 struct ip6_tnl *t = netdev_priv(dev);
614
615 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
616 *encap_limit = t->parms.encap_limit;
617
618 memcpy(fl6, &t->fl.u.ip6, sizeof(*fl6));
619
620 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
621 *dsfield = ipv4_get_dsfield(iph);
622 else
623 *dsfield = ip6_tclass(t->parms.flowinfo);
624
625 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
626 fl6->flowi6_mark = skb->mark;
627 else
628 fl6->flowi6_mark = t->parms.fwmark;
629
630 fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
631 }
632
prepare_ip6gre_xmit_ipv6(struct sk_buff * skb,struct net_device * dev,struct flowi6 * fl6,__u8 * dsfield,int * encap_limit)633 static int prepare_ip6gre_xmit_ipv6(struct sk_buff *skb,
634 struct net_device *dev,
635 struct flowi6 *fl6, __u8 *dsfield,
636 int *encap_limit)
637 {
638 struct ipv6hdr *ipv6h;
639 struct ip6_tnl *t = netdev_priv(dev);
640 __u16 offset;
641
642 offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
643 /* ip6_tnl_parse_tlv_enc_lim() might have reallocated skb->head */
644 ipv6h = ipv6_hdr(skb);
645
646 if (offset > 0) {
647 struct ipv6_tlv_tnl_enc_lim *tel;
648
649 tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
650 if (tel->encap_limit == 0) {
651 icmpv6_ndo_send(skb, ICMPV6_PARAMPROB,
652 ICMPV6_HDR_FIELD, offset + 2);
653 return -1;
654 }
655 *encap_limit = tel->encap_limit - 1;
656 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) {
657 *encap_limit = t->parms.encap_limit;
658 }
659
660 memcpy(fl6, &t->fl.u.ip6, sizeof(*fl6));
661
662 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
663 *dsfield = ipv6_get_dsfield(ipv6h);
664 else
665 *dsfield = ip6_tclass(t->parms.flowinfo);
666
667 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
668 fl6->flowlabel |= ip6_flowlabel(ipv6h);
669
670 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
671 fl6->flowi6_mark = skb->mark;
672 else
673 fl6->flowi6_mark = t->parms.fwmark;
674
675 fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
676
677 return 0;
678 }
679
prepare_ip6gre_xmit_other(struct sk_buff * skb,struct net_device * dev,struct flowi6 * fl6,__u8 * dsfield,int * encap_limit)680 static int prepare_ip6gre_xmit_other(struct sk_buff *skb,
681 struct net_device *dev,
682 struct flowi6 *fl6, __u8 *dsfield,
683 int *encap_limit)
684 {
685 struct ip6_tnl *t = netdev_priv(dev);
686
687 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
688 *encap_limit = t->parms.encap_limit;
689
690 memcpy(fl6, &t->fl.u.ip6, sizeof(*fl6));
691
692 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
693 *dsfield = 0;
694 else
695 *dsfield = ip6_tclass(t->parms.flowinfo);
696
697 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
698 fl6->flowi6_mark = skb->mark;
699 else
700 fl6->flowi6_mark = t->parms.fwmark;
701
702 fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
703
704 return 0;
705 }
706
skb_tunnel_info_txcheck(struct sk_buff * skb)707 static struct ip_tunnel_info *skb_tunnel_info_txcheck(struct sk_buff *skb)
708 {
709 struct ip_tunnel_info *tun_info;
710
711 tun_info = skb_tunnel_info(skb);
712 if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX)))
713 return ERR_PTR(-EINVAL);
714
715 return tun_info;
716 }
717
__gre6_xmit(struct sk_buff * skb,struct net_device * dev,__u8 dsfield,struct flowi6 * fl6,int encap_limit,__u32 * pmtu,__be16 proto)718 static netdev_tx_t __gre6_xmit(struct sk_buff *skb,
719 struct net_device *dev, __u8 dsfield,
720 struct flowi6 *fl6, int encap_limit,
721 __u32 *pmtu, __be16 proto)
722 {
723 struct ip6_tnl *tunnel = netdev_priv(dev);
724 IP_TUNNEL_DECLARE_FLAGS(flags);
725 __be16 protocol;
726
727 if (dev->type == ARPHRD_ETHER)
728 IPCB(skb)->flags = 0;
729
730 if (dev->header_ops && dev->type == ARPHRD_IP6GRE)
731 fl6->daddr = ((struct ipv6hdr *)skb->data)->daddr;
732 else
733 fl6->daddr = tunnel->parms.raddr;
734
735 /* Push GRE header. */
736 protocol = (dev->type == ARPHRD_ETHER) ? htons(ETH_P_TEB) : proto;
737
738 if (tunnel->parms.collect_md) {
739 struct ip_tunnel_info *tun_info;
740 const struct ip_tunnel_key *key;
741 int tun_hlen;
742
743 tun_info = skb_tunnel_info_txcheck(skb);
744 if (IS_ERR(tun_info) ||
745 unlikely(ip_tunnel_info_af(tun_info) != AF_INET6))
746 return -EINVAL;
747
748 key = &tun_info->key;
749 memset(fl6, 0, sizeof(*fl6));
750 fl6->flowi6_proto = IPPROTO_GRE;
751 fl6->daddr = key->u.ipv6.dst;
752 fl6->flowlabel = key->label;
753 fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
754 fl6->fl6_gre_key = tunnel_id_to_key32(key->tun_id);
755
756 dsfield = key->tos;
757 ip_tunnel_flags_zero(flags);
758 __set_bit(IP_TUNNEL_CSUM_BIT, flags);
759 __set_bit(IP_TUNNEL_KEY_BIT, flags);
760 __set_bit(IP_TUNNEL_SEQ_BIT, flags);
761 ip_tunnel_flags_and(flags, flags, key->tun_flags);
762 tun_hlen = gre_calc_hlen(flags);
763
764 if (skb_cow_head(skb, dev->needed_headroom ?: tun_hlen + tunnel->encap_hlen))
765 return -ENOMEM;
766
767 gre_build_header(skb, tun_hlen,
768 flags, protocol,
769 tunnel_id_to_key32(tun_info->key.tun_id),
770 test_bit(IP_TUNNEL_SEQ_BIT, flags) ?
771 htonl(atomic_fetch_inc(&tunnel->o_seqno)) :
772 0);
773
774 } else {
775 if (skb_cow_head(skb, dev->needed_headroom ?: tunnel->hlen))
776 return -ENOMEM;
777
778 ip_tunnel_flags_copy(flags, tunnel->parms.o_flags);
779
780 gre_build_header(skb, tunnel->tun_hlen, flags,
781 protocol, tunnel->parms.o_key,
782 test_bit(IP_TUNNEL_SEQ_BIT, flags) ?
783 htonl(atomic_fetch_inc(&tunnel->o_seqno)) :
784 0);
785 }
786
787 return ip6_tnl_xmit(skb, dev, dsfield, fl6, encap_limit, pmtu,
788 NEXTHDR_GRE);
789 }
790
ip6gre_xmit_ipv4(struct sk_buff * skb,struct net_device * dev)791 static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev)
792 {
793 struct ip6_tnl *t = netdev_priv(dev);
794 int encap_limit = -1;
795 struct flowi6 fl6;
796 __u8 dsfield = 0;
797 __u32 mtu;
798 int err;
799
800 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
801
802 if (!t->parms.collect_md)
803 prepare_ip6gre_xmit_ipv4(skb, dev, &fl6,
804 &dsfield, &encap_limit);
805
806 err = gre_handle_offloads(skb, test_bit(IP_TUNNEL_CSUM_BIT,
807 t->parms.o_flags));
808 if (err)
809 return -1;
810
811 err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
812 skb->protocol);
813 if (err != 0) {
814 /* XXX: send ICMP error even if DF is not set. */
815 if (err == -EMSGSIZE)
816 icmp_ndo_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
817 htonl(mtu));
818 return -1;
819 }
820
821 return 0;
822 }
823
ip6gre_xmit_ipv6(struct sk_buff * skb,struct net_device * dev)824 static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev)
825 {
826 struct ip6_tnl *t = netdev_priv(dev);
827 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
828 int encap_limit = -1;
829 struct flowi6 fl6;
830 __u8 dsfield = 0;
831 __u32 mtu;
832 int err;
833
834 if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
835 return -1;
836
837 if (!t->parms.collect_md &&
838 prepare_ip6gre_xmit_ipv6(skb, dev, &fl6, &dsfield, &encap_limit))
839 return -1;
840
841 if (gre_handle_offloads(skb, test_bit(IP_TUNNEL_CSUM_BIT,
842 t->parms.o_flags)))
843 return -1;
844
845 err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit,
846 &mtu, skb->protocol);
847 if (err != 0) {
848 if (err == -EMSGSIZE)
849 icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
850 return -1;
851 }
852
853 return 0;
854 }
855
ip6gre_xmit_other(struct sk_buff * skb,struct net_device * dev)856 static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev)
857 {
858 struct ip6_tnl *t = netdev_priv(dev);
859 int encap_limit = -1;
860 struct flowi6 fl6;
861 __u8 dsfield = 0;
862 __u32 mtu;
863 int err;
864
865 if (!t->parms.collect_md &&
866 prepare_ip6gre_xmit_other(skb, dev, &fl6, &dsfield, &encap_limit))
867 return -1;
868
869 err = gre_handle_offloads(skb, test_bit(IP_TUNNEL_CSUM_BIT,
870 t->parms.o_flags));
871 if (err)
872 return err;
873 err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu, skb->protocol);
874
875 return err;
876 }
877
ip6gre_tunnel_xmit(struct sk_buff * skb,struct net_device * dev)878 static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb,
879 struct net_device *dev)
880 {
881 struct ip_tunnel_info *tun_info = NULL;
882 struct ip6_tnl *t = netdev_priv(dev);
883 __be16 payload_protocol;
884 int ret;
885
886 if (!pskb_inet_may_pull(skb))
887 goto tx_err;
888
889 if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
890 goto tx_err;
891
892 if (t->parms.collect_md)
893 tun_info = skb_tunnel_info_txcheck(skb);
894
895 payload_protocol = skb_protocol(skb, true);
896 switch (payload_protocol) {
897 case htons(ETH_P_IP):
898 ret = ip6gre_xmit_ipv4(skb, dev);
899 break;
900 case htons(ETH_P_IPV6):
901 ret = ip6gre_xmit_ipv6(skb, dev);
902 break;
903 default:
904 ret = ip6gre_xmit_other(skb, dev);
905 break;
906 }
907
908 if (ret < 0)
909 goto tx_err;
910
911 return NETDEV_TX_OK;
912
913 tx_err:
914 if (!IS_ERR(tun_info))
915 DEV_STATS_INC(dev, tx_errors);
916 DEV_STATS_INC(dev, tx_dropped);
917 kfree_skb(skb);
918 return NETDEV_TX_OK;
919 }
920
ip6erspan_tunnel_xmit(struct sk_buff * skb,struct net_device * dev)921 static netdev_tx_t ip6erspan_tunnel_xmit(struct sk_buff *skb,
922 struct net_device *dev)
923 {
924 struct ip_tunnel_info *tun_info = NULL;
925 struct ip6_tnl *t = netdev_priv(dev);
926 struct dst_entry *dst = skb_dst(skb);
927 IP_TUNNEL_DECLARE_FLAGS(flags) = { };
928 bool truncate = false;
929 int encap_limit = -1;
930 __u8 dsfield = false;
931 struct flowi6 fl6;
932 int err = -EINVAL;
933 __be16 proto;
934 __u32 mtu;
935 int nhoff;
936
937 if (!pskb_inet_may_pull(skb))
938 goto tx_err;
939
940 if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
941 goto tx_err;
942
943 if (gre_handle_offloads(skb, false))
944 goto tx_err;
945
946 if (skb->len > dev->mtu + dev->hard_header_len) {
947 if (pskb_trim(skb, dev->mtu + dev->hard_header_len))
948 goto tx_err;
949 truncate = true;
950 }
951
952 nhoff = skb_network_offset(skb);
953 if (skb->protocol == htons(ETH_P_IP) &&
954 (ntohs(ip_hdr(skb)->tot_len) > skb->len - nhoff))
955 truncate = true;
956
957 if (skb->protocol == htons(ETH_P_IPV6)) {
958 int thoff;
959
960 if (skb_transport_header_was_set(skb))
961 thoff = skb_transport_offset(skb);
962 else
963 thoff = nhoff + sizeof(struct ipv6hdr);
964 if (ntohs(ipv6_hdr(skb)->payload_len) > skb->len - thoff)
965 truncate = true;
966 }
967
968 if (skb_cow_head(skb, dev->needed_headroom ?: t->hlen))
969 goto tx_err;
970
971 IPCB(skb)->flags = 0;
972
973 /* For collect_md mode, derive fl6 from the tunnel key,
974 * for native mode, call prepare_ip6gre_xmit_{ipv4,ipv6}.
975 */
976 if (t->parms.collect_md) {
977 const struct ip_tunnel_key *key;
978 struct erspan_metadata *md;
979 __be32 tun_id;
980
981 tun_info = skb_tunnel_info_txcheck(skb);
982 if (IS_ERR(tun_info) ||
983 unlikely(ip_tunnel_info_af(tun_info) != AF_INET6))
984 goto tx_err;
985
986 key = &tun_info->key;
987 memset(&fl6, 0, sizeof(fl6));
988 fl6.flowi6_proto = IPPROTO_GRE;
989 fl6.daddr = key->u.ipv6.dst;
990 fl6.flowlabel = key->label;
991 fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
992 fl6.fl6_gre_key = tunnel_id_to_key32(key->tun_id);
993
994 dsfield = key->tos;
995 if (!test_bit(IP_TUNNEL_ERSPAN_OPT_BIT,
996 tun_info->key.tun_flags))
997 goto tx_err;
998 if (tun_info->options_len < sizeof(*md))
999 goto tx_err;
1000 md = ip_tunnel_info_opts(tun_info);
1001
1002 tun_id = tunnel_id_to_key32(key->tun_id);
1003 if (md->version == 1) {
1004 erspan_build_header(skb,
1005 ntohl(tun_id),
1006 ntohl(md->u.index), truncate,
1007 false);
1008 proto = htons(ETH_P_ERSPAN);
1009 } else if (md->version == 2) {
1010 erspan_build_header_v2(skb,
1011 ntohl(tun_id),
1012 md->u.md2.dir,
1013 get_hwid(&md->u.md2),
1014 truncate, false);
1015 proto = htons(ETH_P_ERSPAN2);
1016 } else {
1017 goto tx_err;
1018 }
1019 } else {
1020 switch (skb->protocol) {
1021 case htons(ETH_P_IP):
1022 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1023 prepare_ip6gre_xmit_ipv4(skb, dev, &fl6,
1024 &dsfield, &encap_limit);
1025 break;
1026 case htons(ETH_P_IPV6):
1027 if (ipv6_addr_equal(&t->parms.raddr, &ipv6_hdr(skb)->saddr))
1028 goto tx_err;
1029 if (prepare_ip6gre_xmit_ipv6(skb, dev, &fl6,
1030 &dsfield, &encap_limit))
1031 goto tx_err;
1032 break;
1033 default:
1034 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
1035 break;
1036 }
1037
1038 if (t->parms.erspan_ver == 1) {
1039 erspan_build_header(skb, ntohl(t->parms.o_key),
1040 t->parms.index,
1041 truncate, false);
1042 proto = htons(ETH_P_ERSPAN);
1043 } else if (t->parms.erspan_ver == 2) {
1044 erspan_build_header_v2(skb, ntohl(t->parms.o_key),
1045 t->parms.dir,
1046 t->parms.hwid,
1047 truncate, false);
1048 proto = htons(ETH_P_ERSPAN2);
1049 } else {
1050 goto tx_err;
1051 }
1052
1053 fl6.daddr = t->parms.raddr;
1054 }
1055
1056 /* Push GRE header. */
1057 __set_bit(IP_TUNNEL_SEQ_BIT, flags);
1058 gre_build_header(skb, 8, flags, proto, 0,
1059 htonl(atomic_fetch_inc(&t->o_seqno)));
1060
1061 /* TooBig packet may have updated dst->dev's mtu */
1062 if (!t->parms.collect_md && dst) {
1063 mtu = READ_ONCE(dst_dev(dst)->mtu);
1064 if (dst_mtu(dst) > mtu)
1065 dst->ops->update_pmtu(dst, NULL, skb, mtu, false);
1066 }
1067 err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
1068 NEXTHDR_GRE);
1069 if (err != 0) {
1070 /* XXX: send ICMP error even if DF is not set. */
1071 if (err == -EMSGSIZE) {
1072 if (skb->protocol == htons(ETH_P_IP))
1073 icmp_ndo_send(skb, ICMP_DEST_UNREACH,
1074 ICMP_FRAG_NEEDED, htonl(mtu));
1075 else
1076 icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1077 }
1078
1079 goto tx_err;
1080 }
1081 return NETDEV_TX_OK;
1082
1083 tx_err:
1084 if (!IS_ERR(tun_info))
1085 DEV_STATS_INC(dev, tx_errors);
1086 DEV_STATS_INC(dev, tx_dropped);
1087 kfree_skb(skb);
1088 return NETDEV_TX_OK;
1089 }
1090
ip6gre_tnl_link_config_common(struct ip6_tnl * t)1091 static void ip6gre_tnl_link_config_common(struct ip6_tnl *t)
1092 {
1093 struct net_device *dev = t->dev;
1094 struct __ip6_tnl_parm *p = &t->parms;
1095 struct flowi6 *fl6 = &t->fl.u.ip6;
1096
1097 if (dev->type != ARPHRD_ETHER) {
1098 __dev_addr_set(dev, &p->laddr, sizeof(struct in6_addr));
1099 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
1100 }
1101
1102 /* Set up flowi template */
1103 fl6->saddr = p->laddr;
1104 fl6->daddr = p->raddr;
1105 fl6->flowi6_oif = p->link;
1106 fl6->flowlabel = 0;
1107 fl6->flowi6_proto = IPPROTO_GRE;
1108 fl6->fl6_gre_key = t->parms.o_key;
1109
1110 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
1111 fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
1112 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
1113 fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
1114
1115 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
1116 p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
1117
1118 if (p->flags&IP6_TNL_F_CAP_XMIT &&
1119 p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER)
1120 dev->flags |= IFF_POINTOPOINT;
1121 else
1122 dev->flags &= ~IFF_POINTOPOINT;
1123 }
1124
ip6gre_tnl_link_config_route(struct ip6_tnl * t,int set_mtu,int t_hlen)1125 static void ip6gre_tnl_link_config_route(struct ip6_tnl *t, int set_mtu,
1126 int t_hlen)
1127 {
1128 const struct __ip6_tnl_parm *p = &t->parms;
1129 struct net_device *dev = t->dev;
1130
1131 if (p->flags & IP6_TNL_F_CAP_XMIT) {
1132 int strict = (ipv6_addr_type(&p->raddr) &
1133 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
1134
1135 struct rt6_info *rt = rt6_lookup(t->net,
1136 &p->raddr, &p->laddr,
1137 p->link, NULL, strict);
1138
1139 if (!rt)
1140 return;
1141
1142 if (rt->dst.dev) {
1143 unsigned int headroom;
1144
1145 headroom = rt->dst.dev->hard_header_len + t_hlen;
1146 headroom = ip_tunnel_limit_headroom(headroom);
1147 dev->needed_headroom = headroom;
1148
1149 if (set_mtu) {
1150 int mtu = rt->dst.dev->mtu - t_hlen;
1151
1152 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1153 mtu -= 8;
1154 if (dev->type == ARPHRD_ETHER)
1155 mtu -= ETH_HLEN;
1156
1157 if (mtu < IPV6_MIN_MTU)
1158 mtu = IPV6_MIN_MTU;
1159 WRITE_ONCE(dev->mtu, mtu);
1160 }
1161 }
1162 ip6_rt_put(rt);
1163 }
1164 }
1165
ip6gre_calc_hlen(struct ip6_tnl * tunnel)1166 static int ip6gre_calc_hlen(struct ip6_tnl *tunnel)
1167 {
1168 int t_hlen;
1169
1170 tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
1171 tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen;
1172
1173 t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
1174
1175 if (tunnel->dev->header_ops && tunnel->dev->type == ARPHRD_IP6GRE)
1176 tunnel->dev->hard_header_len = t_hlen;
1177 else
1178 tunnel->dev->needed_headroom = LL_MAX_HEADER + t_hlen;
1179
1180 return t_hlen;
1181 }
1182
ip6gre_tnl_link_config(struct ip6_tnl * t,int set_mtu)1183 static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu)
1184 {
1185 ip6gre_tnl_link_config_common(t);
1186 ip6gre_tnl_link_config_route(t, set_mtu, ip6gre_calc_hlen(t));
1187 }
1188
ip6gre_tnl_copy_tnl_parm(struct ip6_tnl * t,const struct __ip6_tnl_parm * p)1189 static void ip6gre_tnl_copy_tnl_parm(struct ip6_tnl *t,
1190 const struct __ip6_tnl_parm *p)
1191 {
1192 t->parms.laddr = p->laddr;
1193 t->parms.raddr = p->raddr;
1194 t->parms.flags = p->flags;
1195 t->parms.hop_limit = p->hop_limit;
1196 t->parms.encap_limit = p->encap_limit;
1197 t->parms.flowinfo = p->flowinfo;
1198 t->parms.link = p->link;
1199 t->parms.proto = p->proto;
1200 t->parms.i_key = p->i_key;
1201 t->parms.o_key = p->o_key;
1202 ip_tunnel_flags_copy(t->parms.i_flags, p->i_flags);
1203 ip_tunnel_flags_copy(t->parms.o_flags, p->o_flags);
1204 t->parms.fwmark = p->fwmark;
1205 t->parms.erspan_ver = p->erspan_ver;
1206 t->parms.index = p->index;
1207 t->parms.dir = p->dir;
1208 t->parms.hwid = p->hwid;
1209 dst_cache_reset(&t->dst_cache);
1210 }
1211
ip6gre_tnl_change(struct ip6_tnl * t,const struct __ip6_tnl_parm * p,int set_mtu)1212 static int ip6gre_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p,
1213 int set_mtu)
1214 {
1215 ip6gre_tnl_copy_tnl_parm(t, p);
1216 ip6gre_tnl_link_config(t, set_mtu);
1217 return 0;
1218 }
1219
ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm * p,const struct ip6_tnl_parm2 * u)1220 static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p,
1221 const struct ip6_tnl_parm2 *u)
1222 {
1223 p->laddr = u->laddr;
1224 p->raddr = u->raddr;
1225 p->flags = u->flags;
1226 p->hop_limit = u->hop_limit;
1227 p->encap_limit = u->encap_limit;
1228 p->flowinfo = u->flowinfo;
1229 p->link = u->link;
1230 p->i_key = u->i_key;
1231 p->o_key = u->o_key;
1232 gre_flags_to_tnl_flags(p->i_flags, u->i_flags);
1233 gre_flags_to_tnl_flags(p->o_flags, u->o_flags);
1234 memcpy(p->name, u->name, sizeof(u->name));
1235 }
1236
ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 * u,const struct __ip6_tnl_parm * p)1237 static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u,
1238 const struct __ip6_tnl_parm *p)
1239 {
1240 u->proto = IPPROTO_GRE;
1241 u->laddr = p->laddr;
1242 u->raddr = p->raddr;
1243 u->flags = p->flags;
1244 u->hop_limit = p->hop_limit;
1245 u->encap_limit = p->encap_limit;
1246 u->flowinfo = p->flowinfo;
1247 u->link = p->link;
1248 u->i_key = p->i_key;
1249 u->o_key = p->o_key;
1250 u->i_flags = gre_tnl_flags_to_gre_flags(p->i_flags);
1251 u->o_flags = gre_tnl_flags_to_gre_flags(p->o_flags);
1252 memcpy(u->name, p->name, sizeof(u->name));
1253 }
1254
ip6gre_tunnel_siocdevprivate(struct net_device * dev,struct ifreq * ifr,void __user * data,int cmd)1255 static int ip6gre_tunnel_siocdevprivate(struct net_device *dev,
1256 struct ifreq *ifr, void __user *data,
1257 int cmd)
1258 {
1259 int err = 0;
1260 struct ip6_tnl_parm2 p;
1261 struct __ip6_tnl_parm p1;
1262 struct ip6_tnl *t = netdev_priv(dev);
1263 struct net *net = t->net;
1264 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1265
1266 memset(&p1, 0, sizeof(p1));
1267
1268 switch (cmd) {
1269 case SIOCGETTUNNEL:
1270 if (dev == ign->fb_tunnel_dev) {
1271 if (copy_from_user(&p, data, sizeof(p))) {
1272 err = -EFAULT;
1273 break;
1274 }
1275 ip6gre_tnl_parm_from_user(&p1, &p);
1276 t = ip6gre_tunnel_locate(net, &p1, 0);
1277 if (!t)
1278 t = netdev_priv(dev);
1279 }
1280 memset(&p, 0, sizeof(p));
1281 ip6gre_tnl_parm_to_user(&p, &t->parms);
1282 if (copy_to_user(data, &p, sizeof(p)))
1283 err = -EFAULT;
1284 break;
1285
1286 case SIOCADDTUNNEL:
1287 case SIOCCHGTUNNEL:
1288 err = -EPERM;
1289 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1290 goto done;
1291
1292 err = -EFAULT;
1293 if (copy_from_user(&p, data, sizeof(p)))
1294 goto done;
1295
1296 err = -EINVAL;
1297 if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))
1298 goto done;
1299
1300 if (!(p.i_flags&GRE_KEY))
1301 p.i_key = 0;
1302 if (!(p.o_flags&GRE_KEY))
1303 p.o_key = 0;
1304
1305 ip6gre_tnl_parm_from_user(&p1, &p);
1306 t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL);
1307
1308 if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
1309 if (t) {
1310 if (t->dev != dev) {
1311 err = -EEXIST;
1312 break;
1313 }
1314 } else {
1315 t = netdev_priv(dev);
1316
1317 ip6gre_tunnel_unlink(ign, t);
1318 synchronize_net();
1319 ip6gre_tnl_change(t, &p1, 1);
1320 ip6gre_tunnel_link(ign, t);
1321 netdev_state_change(dev);
1322 }
1323 }
1324
1325 if (t) {
1326 err = 0;
1327
1328 memset(&p, 0, sizeof(p));
1329 ip6gre_tnl_parm_to_user(&p, &t->parms);
1330 if (copy_to_user(data, &p, sizeof(p)))
1331 err = -EFAULT;
1332 } else
1333 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
1334 break;
1335
1336 case SIOCDELTUNNEL:
1337 err = -EPERM;
1338 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1339 goto done;
1340
1341 if (dev == ign->fb_tunnel_dev) {
1342 err = -EFAULT;
1343 if (copy_from_user(&p, data, sizeof(p)))
1344 goto done;
1345 err = -ENOENT;
1346 ip6gre_tnl_parm_from_user(&p1, &p);
1347 t = ip6gre_tunnel_locate(net, &p1, 0);
1348 if (!t)
1349 goto done;
1350 err = -EPERM;
1351 if (t == netdev_priv(ign->fb_tunnel_dev))
1352 goto done;
1353 dev = t->dev;
1354 }
1355 unregister_netdevice(dev);
1356 err = 0;
1357 break;
1358
1359 default:
1360 err = -EINVAL;
1361 }
1362
1363 done:
1364 return err;
1365 }
1366
ip6gre_header(struct sk_buff * skb,struct net_device * dev,unsigned short type,const void * daddr,const void * saddr,unsigned int len)1367 static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
1368 unsigned short type, const void *daddr,
1369 const void *saddr, unsigned int len)
1370 {
1371 struct ip6_tnl *t = netdev_priv(dev);
1372 struct ipv6hdr *ipv6h;
1373 int needed;
1374 __be16 *p;
1375
1376 needed = t->hlen + sizeof(*ipv6h);
1377 if (skb_headroom(skb) < needed &&
1378 pskb_expand_head(skb, HH_DATA_ALIGN(needed - skb_headroom(skb)),
1379 0, GFP_ATOMIC))
1380 return -needed;
1381
1382 ipv6h = skb_push(skb, needed);
1383 ip6_flow_hdr(ipv6h, 0, ip6_make_flowlabel(dev_net(dev), skb,
1384 t->fl.u.ip6.flowlabel,
1385 true, &t->fl.u.ip6));
1386 ipv6h->hop_limit = t->parms.hop_limit;
1387 ipv6h->nexthdr = NEXTHDR_GRE;
1388 ipv6h->saddr = t->parms.laddr;
1389 ipv6h->daddr = t->parms.raddr;
1390
1391 p = (__be16 *)(ipv6h + 1);
1392 p[0] = ip_tunnel_flags_to_be16(t->parms.o_flags);
1393 p[1] = htons(type);
1394
1395 /*
1396 * Set the source hardware address.
1397 */
1398
1399 if (saddr)
1400 memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr));
1401 if (daddr)
1402 memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
1403 if (!ipv6_addr_any(&ipv6h->daddr))
1404 return t->hlen;
1405
1406 return -t->hlen;
1407 }
1408
1409 static const struct header_ops ip6gre_header_ops = {
1410 .create = ip6gre_header,
1411 };
1412
1413 static const struct net_device_ops ip6gre_netdev_ops = {
1414 .ndo_init = ip6gre_tunnel_init,
1415 .ndo_uninit = ip6gre_tunnel_uninit,
1416 .ndo_start_xmit = ip6gre_tunnel_xmit,
1417 .ndo_siocdevprivate = ip6gre_tunnel_siocdevprivate,
1418 .ndo_change_mtu = ip6_tnl_change_mtu,
1419 .ndo_get_iflink = ip6_tnl_get_iflink,
1420 };
1421
ip6gre_dev_free(struct net_device * dev)1422 static void ip6gre_dev_free(struct net_device *dev)
1423 {
1424 struct ip6_tnl *t = netdev_priv(dev);
1425
1426 gro_cells_destroy(&t->gro_cells);
1427 dst_cache_destroy(&t->dst_cache);
1428 }
1429
ip6gre_tunnel_setup(struct net_device * dev)1430 static void ip6gre_tunnel_setup(struct net_device *dev)
1431 {
1432 dev->netdev_ops = &ip6gre_netdev_ops;
1433 dev->needs_free_netdev = true;
1434 dev->priv_destructor = ip6gre_dev_free;
1435
1436 dev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS;
1437 dev->type = ARPHRD_IP6GRE;
1438
1439 dev->flags |= IFF_NOARP;
1440 dev->addr_len = sizeof(struct in6_addr);
1441 netif_keep_dst(dev);
1442 /* This perm addr will be used as interface identifier by IPv6 */
1443 dev->addr_assign_type = NET_ADDR_RANDOM;
1444 eth_random_addr(dev->perm_addr);
1445 }
1446
1447 #define GRE6_FEATURES (NETIF_F_SG | \
1448 NETIF_F_FRAGLIST | \
1449 NETIF_F_HIGHDMA | \
1450 NETIF_F_HW_CSUM)
1451
ip6gre_tnl_init_features(struct net_device * dev)1452 static void ip6gre_tnl_init_features(struct net_device *dev)
1453 {
1454 struct ip6_tnl *nt = netdev_priv(dev);
1455
1456 dev->features |= GRE6_FEATURES;
1457 dev->hw_features |= GRE6_FEATURES;
1458
1459 dev->lltx = true;
1460
1461 /* TCP offload with GRE SEQ is not supported, nor can we support 2
1462 * levels of outer headers requiring an update.
1463 */
1464 if (test_bit(IP_TUNNEL_SEQ_BIT, nt->parms.o_flags))
1465 return;
1466 if (test_bit(IP_TUNNEL_CSUM_BIT, nt->parms.o_flags) &&
1467 nt->encap.type != TUNNEL_ENCAP_NONE)
1468 return;
1469
1470 dev->features |= NETIF_F_GSO_SOFTWARE;
1471 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
1472 }
1473
ip6gre_tunnel_init_common(struct net_device * dev)1474 static int ip6gre_tunnel_init_common(struct net_device *dev)
1475 {
1476 struct ip6_tnl *tunnel;
1477 int ret;
1478 int t_hlen;
1479
1480 tunnel = netdev_priv(dev);
1481
1482 tunnel->dev = dev;
1483 strscpy(tunnel->parms.name, dev->name);
1484
1485 ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
1486 if (ret)
1487 return ret;
1488
1489 ret = gro_cells_init(&tunnel->gro_cells, dev);
1490 if (ret)
1491 goto cleanup_dst_cache_init;
1492
1493 t_hlen = ip6gre_calc_hlen(tunnel);
1494 dev->mtu = ETH_DATA_LEN - t_hlen;
1495 if (dev->type == ARPHRD_ETHER)
1496 dev->mtu -= ETH_HLEN;
1497 if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1498 dev->mtu -= 8;
1499
1500 if (tunnel->parms.collect_md) {
1501 netif_keep_dst(dev);
1502 }
1503 ip6gre_tnl_init_features(dev);
1504
1505 netdev_hold(dev, &tunnel->dev_tracker, GFP_KERNEL);
1506 netdev_lockdep_set_classes(dev);
1507 return 0;
1508
1509 cleanup_dst_cache_init:
1510 dst_cache_destroy(&tunnel->dst_cache);
1511 return ret;
1512 }
1513
ip6gre_tunnel_init(struct net_device * dev)1514 static int ip6gre_tunnel_init(struct net_device *dev)
1515 {
1516 struct ip6_tnl *tunnel;
1517 int ret;
1518
1519 ret = ip6gre_tunnel_init_common(dev);
1520 if (ret)
1521 return ret;
1522
1523 tunnel = netdev_priv(dev);
1524
1525 if (tunnel->parms.collect_md)
1526 return 0;
1527
1528 __dev_addr_set(dev, &tunnel->parms.laddr, sizeof(struct in6_addr));
1529 memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr));
1530
1531 if (ipv6_addr_any(&tunnel->parms.raddr))
1532 dev->header_ops = &ip6gre_header_ops;
1533
1534 return 0;
1535 }
1536
ip6gre_fb_tunnel_init(struct net_device * dev)1537 static void ip6gre_fb_tunnel_init(struct net_device *dev)
1538 {
1539 struct ip6_tnl *tunnel = netdev_priv(dev);
1540
1541 tunnel->dev = dev;
1542 tunnel->net = dev_net(dev);
1543 strscpy(tunnel->parms.name, dev->name);
1544
1545 tunnel->hlen = sizeof(struct ipv6hdr) + 4;
1546 }
1547
1548 static struct inet6_protocol ip6gre_protocol __read_mostly = {
1549 .handler = gre_rcv,
1550 .err_handler = ip6gre_err,
1551 .flags = INET6_PROTO_FINAL,
1552 };
1553
ip6gre_exit_rtnl_net(struct net * net,struct list_head * head)1554 static void __net_exit ip6gre_exit_rtnl_net(struct net *net, struct list_head *head)
1555 {
1556 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1557 struct net_device *dev, *aux;
1558 int prio;
1559
1560 for_each_netdev_safe(net, dev, aux)
1561 if (dev->rtnl_link_ops == &ip6gre_link_ops ||
1562 dev->rtnl_link_ops == &ip6gre_tap_ops ||
1563 dev->rtnl_link_ops == &ip6erspan_tap_ops)
1564 unregister_netdevice_queue(dev, head);
1565
1566 for (prio = 0; prio < 4; prio++) {
1567 int h;
1568 for (h = 0; h < IP6_GRE_HASH_SIZE; h++) {
1569 struct ip6_tnl *t;
1570
1571 t = rtnl_net_dereference(net, ign->tunnels[prio][h]);
1572
1573 while (t) {
1574 /* If dev is in the same netns, it has already
1575 * been added to the list by the previous loop.
1576 */
1577 if (!net_eq(dev_net(t->dev), net))
1578 unregister_netdevice_queue(t->dev, head);
1579
1580 t = rtnl_net_dereference(net, t->next);
1581 }
1582 }
1583 }
1584 }
1585
ip6gre_init_net(struct net * net)1586 static int __net_init ip6gre_init_net(struct net *net)
1587 {
1588 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1589 struct net_device *ndev;
1590 int err;
1591
1592 if (!net_has_fallback_tunnels(net))
1593 return 0;
1594 ndev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0",
1595 NET_NAME_UNKNOWN, ip6gre_tunnel_setup);
1596 if (!ndev) {
1597 err = -ENOMEM;
1598 goto err_alloc_dev;
1599 }
1600 ign->fb_tunnel_dev = ndev;
1601 dev_net_set(ign->fb_tunnel_dev, net);
1602 /* FB netdevice is special: we have one, and only one per netns.
1603 * Allowing to move it to another netns is clearly unsafe.
1604 */
1605 ign->fb_tunnel_dev->netns_immutable = true;
1606
1607 ip6gre_fb_tunnel_init(ign->fb_tunnel_dev);
1608 ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops;
1609
1610 err = register_netdev(ign->fb_tunnel_dev);
1611 if (err)
1612 goto err_reg_dev;
1613
1614 rcu_assign_pointer(ign->tunnels_wc[0],
1615 netdev_priv(ign->fb_tunnel_dev));
1616 return 0;
1617
1618 err_reg_dev:
1619 free_netdev(ndev);
1620 err_alloc_dev:
1621 return err;
1622 }
1623
1624 static struct pernet_operations ip6gre_net_ops = {
1625 .init = ip6gre_init_net,
1626 .exit_rtnl = ip6gre_exit_rtnl_net,
1627 .id = &ip6gre_net_id,
1628 .size = sizeof(struct ip6gre_net),
1629 };
1630
ip6gre_tunnel_validate(struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)1631 static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[],
1632 struct netlink_ext_ack *extack)
1633 {
1634 __be16 flags;
1635
1636 if (!data)
1637 return 0;
1638
1639 flags = 0;
1640 if (data[IFLA_GRE_IFLAGS])
1641 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1642 if (data[IFLA_GRE_OFLAGS])
1643 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1644 if (flags & (GRE_VERSION|GRE_ROUTING))
1645 return -EINVAL;
1646
1647 return 0;
1648 }
1649
ip6gre_tap_validate(struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)1650 static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[],
1651 struct netlink_ext_ack *extack)
1652 {
1653 struct in6_addr daddr;
1654
1655 if (tb[IFLA_ADDRESS]) {
1656 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1657 return -EINVAL;
1658 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1659 return -EADDRNOTAVAIL;
1660 }
1661
1662 if (!data)
1663 goto out;
1664
1665 if (data[IFLA_GRE_REMOTE]) {
1666 daddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
1667 if (ipv6_addr_any(&daddr))
1668 return -EINVAL;
1669 }
1670
1671 out:
1672 return ip6gre_tunnel_validate(tb, data, extack);
1673 }
1674
ip6erspan_tap_validate(struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)1675 static int ip6erspan_tap_validate(struct nlattr *tb[], struct nlattr *data[],
1676 struct netlink_ext_ack *extack)
1677 {
1678 __be16 flags = 0;
1679 int ret, ver = 0;
1680
1681 if (!data)
1682 return 0;
1683
1684 ret = ip6gre_tap_validate(tb, data, extack);
1685 if (ret)
1686 return ret;
1687
1688 /* ERSPAN should only have GRE sequence and key flag */
1689 if (data[IFLA_GRE_OFLAGS])
1690 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1691 if (data[IFLA_GRE_IFLAGS])
1692 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1693 if (!data[IFLA_GRE_COLLECT_METADATA] &&
1694 flags != (GRE_SEQ | GRE_KEY))
1695 return -EINVAL;
1696
1697 /* ERSPAN Session ID only has 10-bit. Since we reuse
1698 * 32-bit key field as ID, check it's range.
1699 */
1700 if (data[IFLA_GRE_IKEY] &&
1701 (ntohl(nla_get_be32(data[IFLA_GRE_IKEY])) & ~ID_MASK))
1702 return -EINVAL;
1703
1704 if (data[IFLA_GRE_OKEY] &&
1705 (ntohl(nla_get_be32(data[IFLA_GRE_OKEY])) & ~ID_MASK))
1706 return -EINVAL;
1707
1708 if (data[IFLA_GRE_ERSPAN_VER]) {
1709 ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]);
1710 if (ver != 1 && ver != 2)
1711 return -EINVAL;
1712 }
1713
1714 if (ver == 1) {
1715 if (data[IFLA_GRE_ERSPAN_INDEX]) {
1716 u32 index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
1717
1718 if (index & ~INDEX_MASK)
1719 return -EINVAL;
1720 }
1721 } else if (ver == 2) {
1722 if (data[IFLA_GRE_ERSPAN_DIR]) {
1723 u16 dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]);
1724
1725 if (dir & ~(DIR_MASK >> DIR_OFFSET))
1726 return -EINVAL;
1727 }
1728
1729 if (data[IFLA_GRE_ERSPAN_HWID]) {
1730 u16 hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]);
1731
1732 if (hwid & ~(HWID_MASK >> HWID_OFFSET))
1733 return -EINVAL;
1734 }
1735 }
1736
1737 return 0;
1738 }
1739
ip6erspan_set_version(struct nlattr * data[],struct __ip6_tnl_parm * parms)1740 static void ip6erspan_set_version(struct nlattr *data[],
1741 struct __ip6_tnl_parm *parms)
1742 {
1743 if (!data)
1744 return;
1745
1746 parms->erspan_ver = 1;
1747 if (data[IFLA_GRE_ERSPAN_VER])
1748 parms->erspan_ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]);
1749
1750 if (parms->erspan_ver == 1) {
1751 if (data[IFLA_GRE_ERSPAN_INDEX])
1752 parms->index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
1753 } else if (parms->erspan_ver == 2) {
1754 if (data[IFLA_GRE_ERSPAN_DIR])
1755 parms->dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]);
1756 if (data[IFLA_GRE_ERSPAN_HWID])
1757 parms->hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]);
1758 }
1759 }
1760
ip6gre_netlink_parms(struct nlattr * data[],struct __ip6_tnl_parm * parms)1761 static void ip6gre_netlink_parms(struct nlattr *data[],
1762 struct __ip6_tnl_parm *parms)
1763 {
1764 memset(parms, 0, sizeof(*parms));
1765
1766 if (!data)
1767 return;
1768
1769 if (data[IFLA_GRE_LINK])
1770 parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
1771
1772 if (data[IFLA_GRE_IFLAGS])
1773 gre_flags_to_tnl_flags(parms->i_flags,
1774 nla_get_be16(data[IFLA_GRE_IFLAGS]));
1775
1776 if (data[IFLA_GRE_OFLAGS])
1777 gre_flags_to_tnl_flags(parms->o_flags,
1778 nla_get_be16(data[IFLA_GRE_OFLAGS]));
1779
1780 if (data[IFLA_GRE_IKEY])
1781 parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
1782
1783 if (data[IFLA_GRE_OKEY])
1784 parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
1785
1786 if (data[IFLA_GRE_LOCAL])
1787 parms->laddr = nla_get_in6_addr(data[IFLA_GRE_LOCAL]);
1788
1789 if (data[IFLA_GRE_REMOTE])
1790 parms->raddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
1791
1792 if (data[IFLA_GRE_TTL])
1793 parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]);
1794
1795 if (data[IFLA_GRE_ENCAP_LIMIT])
1796 parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]);
1797
1798 if (data[IFLA_GRE_FLOWINFO])
1799 parms->flowinfo = nla_get_be32(data[IFLA_GRE_FLOWINFO]);
1800
1801 if (data[IFLA_GRE_FLAGS])
1802 parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]);
1803
1804 if (data[IFLA_GRE_FWMARK])
1805 parms->fwmark = nla_get_u32(data[IFLA_GRE_FWMARK]);
1806
1807 if (data[IFLA_GRE_COLLECT_METADATA])
1808 parms->collect_md = true;
1809 }
1810
ip6gre_tap_init(struct net_device * dev)1811 static int ip6gre_tap_init(struct net_device *dev)
1812 {
1813 int ret;
1814
1815 ret = ip6gre_tunnel_init_common(dev);
1816 if (ret)
1817 return ret;
1818
1819 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1820
1821 return 0;
1822 }
1823
1824 static const struct net_device_ops ip6gre_tap_netdev_ops = {
1825 .ndo_init = ip6gre_tap_init,
1826 .ndo_uninit = ip6gre_tunnel_uninit,
1827 .ndo_start_xmit = ip6gre_tunnel_xmit,
1828 .ndo_set_mac_address = eth_mac_addr,
1829 .ndo_validate_addr = eth_validate_addr,
1830 .ndo_change_mtu = ip6_tnl_change_mtu,
1831 .ndo_get_iflink = ip6_tnl_get_iflink,
1832 };
1833
ip6erspan_calc_hlen(struct ip6_tnl * tunnel)1834 static int ip6erspan_calc_hlen(struct ip6_tnl *tunnel)
1835 {
1836 int t_hlen;
1837
1838 tunnel->tun_hlen = 8;
1839 tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen +
1840 erspan_hdr_len(tunnel->parms.erspan_ver);
1841
1842 t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
1843 tunnel->dev->needed_headroom = LL_MAX_HEADER + t_hlen;
1844 return t_hlen;
1845 }
1846
ip6erspan_tap_init(struct net_device * dev)1847 static int ip6erspan_tap_init(struct net_device *dev)
1848 {
1849 struct ip6_tnl *tunnel;
1850 int t_hlen;
1851 int ret;
1852
1853 tunnel = netdev_priv(dev);
1854
1855 tunnel->dev = dev;
1856 strscpy(tunnel->parms.name, dev->name);
1857
1858 ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
1859 if (ret)
1860 return ret;
1861
1862 ret = gro_cells_init(&tunnel->gro_cells, dev);
1863 if (ret)
1864 goto cleanup_dst_cache_init;
1865
1866 t_hlen = ip6erspan_calc_hlen(tunnel);
1867 dev->mtu = ETH_DATA_LEN - t_hlen;
1868 if (dev->type == ARPHRD_ETHER)
1869 dev->mtu -= ETH_HLEN;
1870 if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1871 dev->mtu -= 8;
1872
1873 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1874 ip6erspan_tnl_link_config(tunnel, 1);
1875
1876 netdev_hold(dev, &tunnel->dev_tracker, GFP_KERNEL);
1877 netdev_lockdep_set_classes(dev);
1878 return 0;
1879
1880 cleanup_dst_cache_init:
1881 dst_cache_destroy(&tunnel->dst_cache);
1882 return ret;
1883 }
1884
1885 static const struct net_device_ops ip6erspan_netdev_ops = {
1886 .ndo_init = ip6erspan_tap_init,
1887 .ndo_uninit = ip6erspan_tunnel_uninit,
1888 .ndo_start_xmit = ip6erspan_tunnel_xmit,
1889 .ndo_set_mac_address = eth_mac_addr,
1890 .ndo_validate_addr = eth_validate_addr,
1891 .ndo_change_mtu = ip6_tnl_change_mtu,
1892 .ndo_get_iflink = ip6_tnl_get_iflink,
1893 };
1894
ip6gre_tap_setup(struct net_device * dev)1895 static void ip6gre_tap_setup(struct net_device *dev)
1896 {
1897
1898 ether_setup(dev);
1899
1900 dev->max_mtu = 0;
1901 dev->netdev_ops = &ip6gre_tap_netdev_ops;
1902 dev->needs_free_netdev = true;
1903 dev->priv_destructor = ip6gre_dev_free;
1904
1905 dev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS;
1906 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1907 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1908 netif_keep_dst(dev);
1909 }
1910
ip6gre_netlink_encap_parms(struct nlattr * data[],struct ip_tunnel_encap * ipencap)1911 static bool ip6gre_netlink_encap_parms(struct nlattr *data[],
1912 struct ip_tunnel_encap *ipencap)
1913 {
1914 bool ret = false;
1915
1916 memset(ipencap, 0, sizeof(*ipencap));
1917
1918 if (!data)
1919 return ret;
1920
1921 if (data[IFLA_GRE_ENCAP_TYPE]) {
1922 ret = true;
1923 ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]);
1924 }
1925
1926 if (data[IFLA_GRE_ENCAP_FLAGS]) {
1927 ret = true;
1928 ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]);
1929 }
1930
1931 if (data[IFLA_GRE_ENCAP_SPORT]) {
1932 ret = true;
1933 ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]);
1934 }
1935
1936 if (data[IFLA_GRE_ENCAP_DPORT]) {
1937 ret = true;
1938 ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]);
1939 }
1940
1941 return ret;
1942 }
1943
ip6gre_newlink_common(struct net * link_net,struct net_device * dev,struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)1944 static int ip6gre_newlink_common(struct net *link_net, struct net_device *dev,
1945 struct nlattr *tb[], struct nlattr *data[],
1946 struct netlink_ext_ack *extack)
1947 {
1948 struct ip6_tnl *nt;
1949 struct ip_tunnel_encap ipencap;
1950 int err;
1951
1952 nt = netdev_priv(dev);
1953
1954 if (ip6gre_netlink_encap_parms(data, &ipencap)) {
1955 int err = ip6_tnl_encap_setup(nt, &ipencap);
1956
1957 if (err < 0)
1958 return err;
1959 }
1960
1961 if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
1962 eth_hw_addr_random(dev);
1963
1964 nt->dev = dev;
1965 nt->net = link_net;
1966
1967 err = register_netdevice(dev);
1968 if (err)
1969 goto out;
1970
1971 if (tb[IFLA_MTU])
1972 ip6_tnl_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1973
1974 out:
1975 return err;
1976 }
1977
ip6gre_newlink(struct net_device * dev,struct rtnl_newlink_params * params,struct netlink_ext_ack * extack)1978 static int ip6gre_newlink(struct net_device *dev,
1979 struct rtnl_newlink_params *params,
1980 struct netlink_ext_ack *extack)
1981 {
1982 struct net *net = params->link_net ? : dev_net(dev);
1983 struct ip6_tnl *nt = netdev_priv(dev);
1984 struct nlattr **data = params->data;
1985 struct nlattr **tb = params->tb;
1986 struct ip6gre_net *ign;
1987 int err;
1988
1989 ip6gre_netlink_parms(data, &nt->parms);
1990 ign = net_generic(net, ip6gre_net_id);
1991
1992 if (nt->parms.collect_md) {
1993 if (rtnl_dereference(ign->collect_md_tun))
1994 return -EEXIST;
1995 } else {
1996 if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
1997 return -EEXIST;
1998 }
1999
2000 err = ip6gre_newlink_common(net, dev, tb, data, extack);
2001 if (!err) {
2002 ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]);
2003 ip6gre_tunnel_link_md(ign, nt);
2004 ip6gre_tunnel_link(net_generic(net, ip6gre_net_id), nt);
2005 }
2006 return err;
2007 }
2008
2009 static struct ip6_tnl *
ip6gre_changelink_common(struct net_device * dev,struct nlattr * tb[],struct nlattr * data[],struct __ip6_tnl_parm * p_p,struct netlink_ext_ack * extack)2010 ip6gre_changelink_common(struct net_device *dev, struct nlattr *tb[],
2011 struct nlattr *data[], struct __ip6_tnl_parm *p_p,
2012 struct netlink_ext_ack *extack)
2013 {
2014 struct ip6_tnl *t, *nt = netdev_priv(dev);
2015 struct net *net = nt->net;
2016 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
2017 struct ip_tunnel_encap ipencap;
2018
2019 if (dev == ign->fb_tunnel_dev)
2020 return ERR_PTR(-EINVAL);
2021
2022 if (ip6gre_netlink_encap_parms(data, &ipencap)) {
2023 int err = ip6_tnl_encap_setup(nt, &ipencap);
2024
2025 if (err < 0)
2026 return ERR_PTR(err);
2027 }
2028
2029 ip6gre_netlink_parms(data, p_p);
2030
2031 t = ip6gre_tunnel_locate(net, p_p, 0);
2032
2033 if (t) {
2034 if (t->dev != dev)
2035 return ERR_PTR(-EEXIST);
2036 } else {
2037 t = nt;
2038 }
2039
2040 return t;
2041 }
2042
ip6gre_changelink(struct net_device * dev,struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)2043 static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],
2044 struct nlattr *data[],
2045 struct netlink_ext_ack *extack)
2046 {
2047 struct ip6_tnl *t = netdev_priv(dev);
2048 struct ip6gre_net *ign = net_generic(t->net, ip6gre_net_id);
2049 struct __ip6_tnl_parm p;
2050
2051 if (!rtnl_dev_link_net_capable(dev, t->net))
2052 return -EPERM;
2053
2054 t = ip6gre_changelink_common(dev, tb, data, &p, extack);
2055 if (IS_ERR(t))
2056 return PTR_ERR(t);
2057
2058 ip6gre_tunnel_unlink_md(ign, t);
2059 ip6gre_tunnel_unlink(ign, t);
2060 ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]);
2061 ip6gre_tunnel_link_md(ign, t);
2062 ip6gre_tunnel_link(ign, t);
2063 return 0;
2064 }
2065
ip6gre_dellink(struct net_device * dev,struct list_head * head)2066 static void ip6gre_dellink(struct net_device *dev, struct list_head *head)
2067 {
2068 struct net *net = dev_net(dev);
2069 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
2070
2071 if (dev != ign->fb_tunnel_dev)
2072 unregister_netdevice_queue(dev, head);
2073 }
2074
ip6gre_get_size(const struct net_device * dev)2075 static size_t ip6gre_get_size(const struct net_device *dev)
2076 {
2077 return
2078 /* IFLA_GRE_LINK */
2079 nla_total_size(4) +
2080 /* IFLA_GRE_IFLAGS */
2081 nla_total_size(2) +
2082 /* IFLA_GRE_OFLAGS */
2083 nla_total_size(2) +
2084 /* IFLA_GRE_IKEY */
2085 nla_total_size(4) +
2086 /* IFLA_GRE_OKEY */
2087 nla_total_size(4) +
2088 /* IFLA_GRE_LOCAL */
2089 nla_total_size(sizeof(struct in6_addr)) +
2090 /* IFLA_GRE_REMOTE */
2091 nla_total_size(sizeof(struct in6_addr)) +
2092 /* IFLA_GRE_TTL */
2093 nla_total_size(1) +
2094 /* IFLA_GRE_ENCAP_LIMIT */
2095 nla_total_size(1) +
2096 /* IFLA_GRE_FLOWINFO */
2097 nla_total_size(4) +
2098 /* IFLA_GRE_FLAGS */
2099 nla_total_size(4) +
2100 /* IFLA_GRE_ENCAP_TYPE */
2101 nla_total_size(2) +
2102 /* IFLA_GRE_ENCAP_FLAGS */
2103 nla_total_size(2) +
2104 /* IFLA_GRE_ENCAP_SPORT */
2105 nla_total_size(2) +
2106 /* IFLA_GRE_ENCAP_DPORT */
2107 nla_total_size(2) +
2108 /* IFLA_GRE_COLLECT_METADATA */
2109 nla_total_size(0) +
2110 /* IFLA_GRE_FWMARK */
2111 nla_total_size(4) +
2112 /* IFLA_GRE_ERSPAN_INDEX */
2113 nla_total_size(4) +
2114 0;
2115 }
2116
ip6gre_fill_info(struct sk_buff * skb,const struct net_device * dev)2117 static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev)
2118 {
2119 const struct ip6_tnl *t = netdev_priv(dev);
2120 const struct __ip6_tnl_parm *p = &t->parms;
2121 IP_TUNNEL_DECLARE_FLAGS(o_flags);
2122
2123 ip_tunnel_flags_copy(o_flags, p->o_flags);
2124
2125 if (p->erspan_ver == 1 || p->erspan_ver == 2) {
2126 if (!p->collect_md)
2127 __set_bit(IP_TUNNEL_KEY_BIT, o_flags);
2128
2129 if (nla_put_u8(skb, IFLA_GRE_ERSPAN_VER, p->erspan_ver))
2130 goto nla_put_failure;
2131
2132 if (p->erspan_ver == 1) {
2133 if (nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, p->index))
2134 goto nla_put_failure;
2135 } else {
2136 if (nla_put_u8(skb, IFLA_GRE_ERSPAN_DIR, p->dir))
2137 goto nla_put_failure;
2138 if (nla_put_u16(skb, IFLA_GRE_ERSPAN_HWID, p->hwid))
2139 goto nla_put_failure;
2140 }
2141 }
2142
2143 if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
2144 nla_put_be16(skb, IFLA_GRE_IFLAGS,
2145 gre_tnl_flags_to_gre_flags(p->i_flags)) ||
2146 nla_put_be16(skb, IFLA_GRE_OFLAGS,
2147 gre_tnl_flags_to_gre_flags(o_flags)) ||
2148 nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
2149 nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
2150 nla_put_in6_addr(skb, IFLA_GRE_LOCAL, &p->laddr) ||
2151 nla_put_in6_addr(skb, IFLA_GRE_REMOTE, &p->raddr) ||
2152 nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) ||
2153 nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) ||
2154 nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) ||
2155 nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags) ||
2156 nla_put_u32(skb, IFLA_GRE_FWMARK, p->fwmark))
2157 goto nla_put_failure;
2158
2159 if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE,
2160 t->encap.type) ||
2161 nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT,
2162 t->encap.sport) ||
2163 nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT,
2164 t->encap.dport) ||
2165 nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS,
2166 t->encap.flags))
2167 goto nla_put_failure;
2168
2169 if (p->collect_md) {
2170 if (nla_put_flag(skb, IFLA_GRE_COLLECT_METADATA))
2171 goto nla_put_failure;
2172 }
2173
2174 return 0;
2175
2176 nla_put_failure:
2177 return -EMSGSIZE;
2178 }
2179
2180 static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = {
2181 [IFLA_GRE_LINK] = { .type = NLA_U32 },
2182 [IFLA_GRE_IFLAGS] = { .type = NLA_U16 },
2183 [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
2184 [IFLA_GRE_IKEY] = { .type = NLA_U32 },
2185 [IFLA_GRE_OKEY] = { .type = NLA_U32 },
2186 [IFLA_GRE_LOCAL] = { .len = sizeof_field(struct ipv6hdr, saddr) },
2187 [IFLA_GRE_REMOTE] = { .len = sizeof_field(struct ipv6hdr, daddr) },
2188 [IFLA_GRE_TTL] = { .type = NLA_U8 },
2189 [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 },
2190 [IFLA_GRE_FLOWINFO] = { .type = NLA_U32 },
2191 [IFLA_GRE_FLAGS] = { .type = NLA_U32 },
2192 [IFLA_GRE_ENCAP_TYPE] = { .type = NLA_U16 },
2193 [IFLA_GRE_ENCAP_FLAGS] = { .type = NLA_U16 },
2194 [IFLA_GRE_ENCAP_SPORT] = { .type = NLA_U16 },
2195 [IFLA_GRE_ENCAP_DPORT] = { .type = NLA_U16 },
2196 [IFLA_GRE_COLLECT_METADATA] = { .type = NLA_FLAG },
2197 [IFLA_GRE_FWMARK] = { .type = NLA_U32 },
2198 [IFLA_GRE_ERSPAN_INDEX] = { .type = NLA_U32 },
2199 [IFLA_GRE_ERSPAN_VER] = { .type = NLA_U8 },
2200 [IFLA_GRE_ERSPAN_DIR] = { .type = NLA_U8 },
2201 [IFLA_GRE_ERSPAN_HWID] = { .type = NLA_U16 },
2202 };
2203
ip6erspan_tap_setup(struct net_device * dev)2204 static void ip6erspan_tap_setup(struct net_device *dev)
2205 {
2206 ether_setup(dev);
2207
2208 dev->max_mtu = 0;
2209 dev->netdev_ops = &ip6erspan_netdev_ops;
2210 dev->needs_free_netdev = true;
2211 dev->priv_destructor = ip6gre_dev_free;
2212
2213 dev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS;
2214 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
2215 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2216 netif_keep_dst(dev);
2217 }
2218
ip6erspan_newlink(struct net_device * dev,struct rtnl_newlink_params * params,struct netlink_ext_ack * extack)2219 static int ip6erspan_newlink(struct net_device *dev,
2220 struct rtnl_newlink_params *params,
2221 struct netlink_ext_ack *extack)
2222 {
2223 struct net *net = params->link_net ? : dev_net(dev);
2224 struct ip6_tnl *nt = netdev_priv(dev);
2225 struct nlattr **data = params->data;
2226 struct nlattr **tb = params->tb;
2227 struct ip6gre_net *ign;
2228 int err;
2229
2230 ip6gre_netlink_parms(data, &nt->parms);
2231 ip6erspan_set_version(data, &nt->parms);
2232 ign = net_generic(net, ip6gre_net_id);
2233
2234 if (nt->parms.collect_md) {
2235 if (rtnl_dereference(ign->collect_md_tun_erspan))
2236 return -EEXIST;
2237 } else {
2238 if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
2239 return -EEXIST;
2240 }
2241
2242 err = ip6gre_newlink_common(net, dev, tb, data, extack);
2243 if (!err) {
2244 ip6erspan_tnl_link_config(nt, !tb[IFLA_MTU]);
2245 ip6erspan_tunnel_link_md(ign, nt);
2246 ip6gre_tunnel_link(net_generic(net, ip6gre_net_id), nt);
2247 }
2248 return err;
2249 }
2250
ip6erspan_tnl_link_config(struct ip6_tnl * t,int set_mtu)2251 static void ip6erspan_tnl_link_config(struct ip6_tnl *t, int set_mtu)
2252 {
2253 ip6gre_tnl_link_config_common(t);
2254 ip6gre_tnl_link_config_route(t, set_mtu, ip6erspan_calc_hlen(t));
2255 }
2256
ip6erspan_tnl_change(struct ip6_tnl * t,const struct __ip6_tnl_parm * p,int set_mtu)2257 static int ip6erspan_tnl_change(struct ip6_tnl *t,
2258 const struct __ip6_tnl_parm *p, int set_mtu)
2259 {
2260 ip6gre_tnl_copy_tnl_parm(t, p);
2261 ip6erspan_tnl_link_config(t, set_mtu);
2262 return 0;
2263 }
2264
ip6erspan_changelink(struct net_device * dev,struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)2265 static int ip6erspan_changelink(struct net_device *dev, struct nlattr *tb[],
2266 struct nlattr *data[],
2267 struct netlink_ext_ack *extack)
2268 {
2269 struct ip6_tnl *t = netdev_priv(dev);
2270 struct __ip6_tnl_parm p;
2271 struct ip6gre_net *ign;
2272
2273 if (!rtnl_dev_link_net_capable(dev, t->net))
2274 return -EPERM;
2275
2276 ign = net_generic(t->net, ip6gre_net_id);
2277 t = ip6gre_changelink_common(dev, tb, data, &p, extack);
2278 if (IS_ERR(t))
2279 return PTR_ERR(t);
2280
2281 ip6erspan_set_version(data, &p);
2282 ip6gre_tunnel_unlink_md(ign, t);
2283 ip6gre_tunnel_unlink(ign, t);
2284 ip6erspan_tnl_change(t, &p, !tb[IFLA_MTU]);
2285 ip6erspan_tunnel_link_md(ign, t);
2286 ip6gre_tunnel_link(ign, t);
2287 return 0;
2288 }
2289
2290 static struct rtnl_link_ops ip6gre_link_ops __read_mostly = {
2291 .kind = "ip6gre",
2292 .maxtype = IFLA_GRE_MAX,
2293 .policy = ip6gre_policy,
2294 .priv_size = sizeof(struct ip6_tnl),
2295 .setup = ip6gre_tunnel_setup,
2296 .validate = ip6gre_tunnel_validate,
2297 .newlink = ip6gre_newlink,
2298 .changelink = ip6gre_changelink,
2299 .dellink = ip6gre_dellink,
2300 .get_size = ip6gre_get_size,
2301 .fill_info = ip6gre_fill_info,
2302 .get_link_net = ip6_tnl_get_link_net,
2303 };
2304
2305 static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = {
2306 .kind = "ip6gretap",
2307 .maxtype = IFLA_GRE_MAX,
2308 .policy = ip6gre_policy,
2309 .priv_size = sizeof(struct ip6_tnl),
2310 .setup = ip6gre_tap_setup,
2311 .validate = ip6gre_tap_validate,
2312 .newlink = ip6gre_newlink,
2313 .changelink = ip6gre_changelink,
2314 .get_size = ip6gre_get_size,
2315 .fill_info = ip6gre_fill_info,
2316 .get_link_net = ip6_tnl_get_link_net,
2317 };
2318
2319 static struct rtnl_link_ops ip6erspan_tap_ops __read_mostly = {
2320 .kind = "ip6erspan",
2321 .maxtype = IFLA_GRE_MAX,
2322 .policy = ip6gre_policy,
2323 .priv_size = sizeof(struct ip6_tnl),
2324 .setup = ip6erspan_tap_setup,
2325 .validate = ip6erspan_tap_validate,
2326 .newlink = ip6erspan_newlink,
2327 .changelink = ip6erspan_changelink,
2328 .get_size = ip6gre_get_size,
2329 .fill_info = ip6gre_fill_info,
2330 .get_link_net = ip6_tnl_get_link_net,
2331 };
2332
2333 /*
2334 * And now the modules code and kernel interface.
2335 */
2336
ip6gre_init(void)2337 static int __init ip6gre_init(void)
2338 {
2339 int err;
2340
2341 pr_info("GRE over IPv6 tunneling driver\n");
2342
2343 err = register_pernet_device(&ip6gre_net_ops);
2344 if (err < 0)
2345 return err;
2346
2347 err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE);
2348 if (err < 0) {
2349 pr_info("%s: can't add protocol\n", __func__);
2350 goto add_proto_failed;
2351 }
2352
2353 err = rtnl_link_register(&ip6gre_link_ops);
2354 if (err < 0)
2355 goto rtnl_link_failed;
2356
2357 err = rtnl_link_register(&ip6gre_tap_ops);
2358 if (err < 0)
2359 goto tap_ops_failed;
2360
2361 err = rtnl_link_register(&ip6erspan_tap_ops);
2362 if (err < 0)
2363 goto erspan_link_failed;
2364
2365 out:
2366 return err;
2367
2368 erspan_link_failed:
2369 rtnl_link_unregister(&ip6gre_tap_ops);
2370 tap_ops_failed:
2371 rtnl_link_unregister(&ip6gre_link_ops);
2372 rtnl_link_failed:
2373 inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
2374 add_proto_failed:
2375 unregister_pernet_device(&ip6gre_net_ops);
2376 goto out;
2377 }
2378
ip6gre_fini(void)2379 static void __exit ip6gre_fini(void)
2380 {
2381 rtnl_link_unregister(&ip6gre_tap_ops);
2382 rtnl_link_unregister(&ip6gre_link_ops);
2383 rtnl_link_unregister(&ip6erspan_tap_ops);
2384 inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
2385 unregister_pernet_device(&ip6gre_net_ops);
2386 }
2387
2388 module_init(ip6gre_init);
2389 module_exit(ip6gre_fini);
2390 MODULE_LICENSE("GPL");
2391 MODULE_AUTHOR("D. Kozlov <xeb@mail.ru>");
2392 MODULE_DESCRIPTION("GRE over IPv6 tunneling device");
2393 MODULE_ALIAS_RTNL_LINK("ip6gre");
2394 MODULE_ALIAS_RTNL_LINK("ip6gretap");
2395 MODULE_ALIAS_RTNL_LINK("ip6erspan");
2396 MODULE_ALIAS_NETDEV("ip6gre0");
2397