1 /*
2 * IPv6 tunneling device
3 * Linux INET6 implementation
4 *
5 * Authors:
6 * Ville Nuorvala <vnuorval@tcs.hut.fi>
7 * Yasuyuki Kozakai <kozakai@linux-ipv6.org>
8 *
9 * Based on:
10 * linux/net/ipv6/sit.c and linux/net/ipv4/ipip.c
11 *
12 * RFC 2473
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
18 *
19 */
20
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
23 #include <linux/module.h>
24 #include <linux/capability.h>
25 #include <linux/errno.h>
26 #include <linux/types.h>
27 #include <linux/sockios.h>
28 #include <linux/icmp.h>
29 #include <linux/if.h>
30 #include <linux/in.h>
31 #include <linux/ip.h>
32 #include <linux/net.h>
33 #include <linux/in6.h>
34 #include <linux/netdevice.h>
35 #include <linux/if_arp.h>
36 #include <linux/icmpv6.h>
37 #include <linux/init.h>
38 #include <linux/route.h>
39 #include <linux/rtnetlink.h>
40 #include <linux/netfilter_ipv6.h>
41 #include <linux/slab.h>
42 #include <linux/hash.h>
43 #include <linux/etherdevice.h>
44
45 #include <asm/uaccess.h>
46 #include <linux/atomic.h>
47
48 #include <net/icmp.h>
49 #include <net/ip.h>
50 #include <net/ip_tunnels.h>
51 #include <net/ipv6.h>
52 #include <net/ip6_route.h>
53 #include <net/addrconf.h>
54 #include <net/ip6_tunnel.h>
55 #include <net/xfrm.h>
56 #include <net/dsfield.h>
57 #include <net/inet_ecn.h>
58 #include <net/net_namespace.h>
59 #include <net/netns/generic.h>
60
61 MODULE_AUTHOR("Ville Nuorvala");
62 MODULE_DESCRIPTION("IPv6 tunneling device");
63 MODULE_LICENSE("GPL");
64 MODULE_ALIAS_RTNL_LINK("ip6tnl");
65 MODULE_ALIAS_NETDEV("ip6tnl0");
66
67 #ifdef IP6_TNL_DEBUG
68 #define IP6_TNL_TRACE(x...) pr_debug("%s:" x "\n", __func__)
69 #else
70 #define IP6_TNL_TRACE(x...) do {;} while(0)
71 #endif
72
73 #define HASH_SIZE_SHIFT 5
74 #define HASH_SIZE (1 << HASH_SIZE_SHIFT)
75
76 static bool log_ecn_error = true;
77 module_param(log_ecn_error, bool, 0644);
78 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
79
HASH(const struct in6_addr * addr1,const struct in6_addr * addr2)80 static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2)
81 {
82 u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2);
83
84 return hash_32(hash, HASH_SIZE_SHIFT);
85 }
86
87 static int ip6_tnl_dev_init(struct net_device *dev);
88 static void ip6_tnl_dev_setup(struct net_device *dev);
89 static struct rtnl_link_ops ip6_link_ops __read_mostly;
90
91 static int ip6_tnl_net_id __read_mostly;
92 struct ip6_tnl_net {
93 /* the IPv6 tunnel fallback device */
94 struct net_device *fb_tnl_dev;
95 /* lists for storing tunnels in use */
96 struct ip6_tnl __rcu *tnls_r_l[HASH_SIZE];
97 struct ip6_tnl __rcu *tnls_wc[1];
98 struct ip6_tnl __rcu **tnls[2];
99 };
100
ip6_get_stats(struct net_device * dev)101 static struct net_device_stats *ip6_get_stats(struct net_device *dev)
102 {
103 struct pcpu_sw_netstats tmp, sum = { 0 };
104 int i;
105
106 for_each_possible_cpu(i) {
107 unsigned int start;
108 const struct pcpu_sw_netstats *tstats =
109 per_cpu_ptr(dev->tstats, i);
110
111 do {
112 start = u64_stats_fetch_begin_irq(&tstats->syncp);
113 tmp.rx_packets = tstats->rx_packets;
114 tmp.rx_bytes = tstats->rx_bytes;
115 tmp.tx_packets = tstats->tx_packets;
116 tmp.tx_bytes = tstats->tx_bytes;
117 } while (u64_stats_fetch_retry_irq(&tstats->syncp, start));
118
119 sum.rx_packets += tmp.rx_packets;
120 sum.rx_bytes += tmp.rx_bytes;
121 sum.tx_packets += tmp.tx_packets;
122 sum.tx_bytes += tmp.tx_bytes;
123 }
124 dev->stats.rx_packets = sum.rx_packets;
125 dev->stats.rx_bytes = sum.rx_bytes;
126 dev->stats.tx_packets = sum.tx_packets;
127 dev->stats.tx_bytes = sum.tx_bytes;
128 return &dev->stats;
129 }
130
131 /*
132 * Locking : hash tables are protected by RCU and RTNL
133 */
134
ip6_tnl_dst_check(struct ip6_tnl * t)135 struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t)
136 {
137 struct dst_entry *dst = t->dst_cache;
138
139 if (dst && dst->obsolete &&
140 dst->ops->check(dst, t->dst_cookie) == NULL) {
141 t->dst_cache = NULL;
142 dst_release(dst);
143 return NULL;
144 }
145
146 return dst;
147 }
148 EXPORT_SYMBOL_GPL(ip6_tnl_dst_check);
149
ip6_tnl_dst_reset(struct ip6_tnl * t)150 void ip6_tnl_dst_reset(struct ip6_tnl *t)
151 {
152 dst_release(t->dst_cache);
153 t->dst_cache = NULL;
154 }
155 EXPORT_SYMBOL_GPL(ip6_tnl_dst_reset);
156
ip6_tnl_dst_store(struct ip6_tnl * t,struct dst_entry * dst)157 void ip6_tnl_dst_store(struct ip6_tnl *t, struct dst_entry *dst)
158 {
159 struct rt6_info *rt = (struct rt6_info *) dst;
160 t->dst_cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;
161 dst_release(t->dst_cache);
162 t->dst_cache = dst;
163 }
164 EXPORT_SYMBOL_GPL(ip6_tnl_dst_store);
165
166 /**
167 * ip6_tnl_lookup - fetch tunnel matching the end-point addresses
168 * @remote: the address of the tunnel exit-point
169 * @local: the address of the tunnel entry-point
170 *
171 * Return:
172 * tunnel matching given end-points if found,
173 * else fallback tunnel if its device is up,
174 * else %NULL
175 **/
176
177 #define for_each_ip6_tunnel_rcu(start) \
178 for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
179
180 static struct ip6_tnl *
ip6_tnl_lookup(struct net * net,const struct in6_addr * remote,const struct in6_addr * local)181 ip6_tnl_lookup(struct net *net, const struct in6_addr *remote, const struct in6_addr *local)
182 {
183 unsigned int hash = HASH(remote, local);
184 struct ip6_tnl *t;
185 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
186
187 for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
188 if (ipv6_addr_equal(local, &t->parms.laddr) &&
189 ipv6_addr_equal(remote, &t->parms.raddr) &&
190 (t->dev->flags & IFF_UP))
191 return t;
192 }
193 t = rcu_dereference(ip6n->tnls_wc[0]);
194 if (t && (t->dev->flags & IFF_UP))
195 return t;
196
197 return NULL;
198 }
199
200 /**
201 * ip6_tnl_bucket - get head of list matching given tunnel parameters
202 * @p: parameters containing tunnel end-points
203 *
204 * Description:
205 * ip6_tnl_bucket() returns the head of the list matching the
206 * &struct in6_addr entries laddr and raddr in @p.
207 *
208 * Return: head of IPv6 tunnel list
209 **/
210
211 static struct ip6_tnl __rcu **
ip6_tnl_bucket(struct ip6_tnl_net * ip6n,const struct __ip6_tnl_parm * p)212 ip6_tnl_bucket(struct ip6_tnl_net *ip6n, const struct __ip6_tnl_parm *p)
213 {
214 const struct in6_addr *remote = &p->raddr;
215 const struct in6_addr *local = &p->laddr;
216 unsigned int h = 0;
217 int prio = 0;
218
219 if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
220 prio = 1;
221 h = HASH(remote, local);
222 }
223 return &ip6n->tnls[prio][h];
224 }
225
226 /**
227 * ip6_tnl_link - add tunnel to hash table
228 * @t: tunnel to be added
229 **/
230
231 static void
ip6_tnl_link(struct ip6_tnl_net * ip6n,struct ip6_tnl * t)232 ip6_tnl_link(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
233 {
234 struct ip6_tnl __rcu **tp = ip6_tnl_bucket(ip6n, &t->parms);
235
236 rcu_assign_pointer(t->next , rtnl_dereference(*tp));
237 rcu_assign_pointer(*tp, t);
238 }
239
240 /**
241 * ip6_tnl_unlink - remove tunnel from hash table
242 * @t: tunnel to be removed
243 **/
244
245 static void
ip6_tnl_unlink(struct ip6_tnl_net * ip6n,struct ip6_tnl * t)246 ip6_tnl_unlink(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
247 {
248 struct ip6_tnl __rcu **tp;
249 struct ip6_tnl *iter;
250
251 for (tp = ip6_tnl_bucket(ip6n, &t->parms);
252 (iter = rtnl_dereference(*tp)) != NULL;
253 tp = &iter->next) {
254 if (t == iter) {
255 rcu_assign_pointer(*tp, t->next);
256 break;
257 }
258 }
259 }
260
ip6_dev_free(struct net_device * dev)261 static void ip6_dev_free(struct net_device *dev)
262 {
263 free_percpu(dev->tstats);
264 free_netdev(dev);
265 }
266
ip6_tnl_create2(struct net_device * dev)267 static int ip6_tnl_create2(struct net_device *dev)
268 {
269 struct ip6_tnl *t = netdev_priv(dev);
270 struct net *net = dev_net(dev);
271 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
272 int err;
273
274 t = netdev_priv(dev);
275
276 dev->rtnl_link_ops = &ip6_link_ops;
277 err = register_netdevice(dev);
278 if (err < 0)
279 goto out;
280
281 strcpy(t->parms.name, dev->name);
282
283 dev_hold(dev);
284 ip6_tnl_link(ip6n, t);
285 return 0;
286
287 out:
288 return err;
289 }
290
291 /**
292 * ip6_tnl_create - create a new tunnel
293 * @p: tunnel parameters
294 * @pt: pointer to new tunnel
295 *
296 * Description:
297 * Create tunnel matching given parameters.
298 *
299 * Return:
300 * created tunnel or NULL
301 **/
302
ip6_tnl_create(struct net * net,struct __ip6_tnl_parm * p)303 static struct ip6_tnl *ip6_tnl_create(struct net *net, struct __ip6_tnl_parm *p)
304 {
305 struct net_device *dev;
306 struct ip6_tnl *t;
307 char name[IFNAMSIZ];
308 int err;
309
310 if (p->name[0])
311 strlcpy(name, p->name, IFNAMSIZ);
312 else
313 sprintf(name, "ip6tnl%%d");
314
315 dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
316 ip6_tnl_dev_setup);
317 if (dev == NULL)
318 goto failed;
319
320 dev_net_set(dev, net);
321
322 t = netdev_priv(dev);
323 t->parms = *p;
324 t->net = dev_net(dev);
325 err = ip6_tnl_create2(dev);
326 if (err < 0)
327 goto failed_free;
328
329 return t;
330
331 failed_free:
332 ip6_dev_free(dev);
333 failed:
334 return NULL;
335 }
336
337 /**
338 * ip6_tnl_locate - find or create tunnel matching given parameters
339 * @p: tunnel parameters
340 * @create: != 0 if allowed to create new tunnel if no match found
341 *
342 * Description:
343 * ip6_tnl_locate() first tries to locate an existing tunnel
344 * based on @parms. If this is unsuccessful, but @create is set a new
345 * tunnel device is created and registered for use.
346 *
347 * Return:
348 * matching tunnel or NULL
349 **/
350
ip6_tnl_locate(struct net * net,struct __ip6_tnl_parm * p,int create)351 static struct ip6_tnl *ip6_tnl_locate(struct net *net,
352 struct __ip6_tnl_parm *p, int create)
353 {
354 const struct in6_addr *remote = &p->raddr;
355 const struct in6_addr *local = &p->laddr;
356 struct ip6_tnl __rcu **tp;
357 struct ip6_tnl *t;
358 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
359
360 for (tp = ip6_tnl_bucket(ip6n, p);
361 (t = rtnl_dereference(*tp)) != NULL;
362 tp = &t->next) {
363 if (ipv6_addr_equal(local, &t->parms.laddr) &&
364 ipv6_addr_equal(remote, &t->parms.raddr)) {
365 if (create)
366 return NULL;
367
368 return t;
369 }
370 }
371 if (!create)
372 return NULL;
373 return ip6_tnl_create(net, p);
374 }
375
376 /**
377 * ip6_tnl_dev_uninit - tunnel device uninitializer
378 * @dev: the device to be destroyed
379 *
380 * Description:
381 * ip6_tnl_dev_uninit() removes tunnel from its list
382 **/
383
384 static void
ip6_tnl_dev_uninit(struct net_device * dev)385 ip6_tnl_dev_uninit(struct net_device *dev)
386 {
387 struct ip6_tnl *t = netdev_priv(dev);
388 struct net *net = t->net;
389 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
390
391 if (dev == ip6n->fb_tnl_dev)
392 RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
393 else
394 ip6_tnl_unlink(ip6n, t);
395 ip6_tnl_dst_reset(t);
396 dev_put(dev);
397 }
398
399 /**
400 * parse_tvl_tnl_enc_lim - handle encapsulation limit option
401 * @skb: received socket buffer
402 *
403 * Return:
404 * 0 if none was found,
405 * else index to encapsulation limit
406 **/
407
ip6_tnl_parse_tlv_enc_lim(struct sk_buff * skb,__u8 * raw)408 __u16 ip6_tnl_parse_tlv_enc_lim(struct sk_buff *skb, __u8 *raw)
409 {
410 const struct ipv6hdr *ipv6h = (const struct ipv6hdr *)raw;
411 unsigned int nhoff = raw - skb->data;
412 unsigned int off = nhoff + sizeof(*ipv6h);
413 u8 next, nexthdr = ipv6h->nexthdr;
414
415 while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
416 struct ipv6_opt_hdr *hdr;
417 u16 optlen;
418
419 if (!pskb_may_pull(skb, off + sizeof(*hdr)))
420 break;
421
422 hdr = (struct ipv6_opt_hdr *)(skb->data + off);
423 if (nexthdr == NEXTHDR_FRAGMENT) {
424 struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
425 if (frag_hdr->frag_off)
426 break;
427 optlen = 8;
428 } else if (nexthdr == NEXTHDR_AUTH) {
429 optlen = (hdr->hdrlen + 2) << 2;
430 } else {
431 optlen = ipv6_optlen(hdr);
432 }
433 /* cache hdr->nexthdr, since pskb_may_pull() might
434 * invalidate hdr
435 */
436 next = hdr->nexthdr;
437 if (nexthdr == NEXTHDR_DEST) {
438 u16 i = 2;
439
440 /* Remember : hdr is no longer valid at this point. */
441 if (!pskb_may_pull(skb, off + optlen))
442 break;
443
444 while (1) {
445 struct ipv6_tlv_tnl_enc_lim *tel;
446
447 /* No more room for encapsulation limit */
448 if (i + sizeof(*tel) > optlen)
449 break;
450
451 tel = (struct ipv6_tlv_tnl_enc_lim *)(skb->data + off + i);
452 /* return index of option if found and valid */
453 if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
454 tel->length == 1)
455 return i + off - nhoff;
456 /* else jump to next option */
457 if (tel->type)
458 i += tel->length + 2;
459 else
460 i++;
461 }
462 }
463 nexthdr = next;
464 off += optlen;
465 }
466 return 0;
467 }
468 EXPORT_SYMBOL(ip6_tnl_parse_tlv_enc_lim);
469
470 /**
471 * ip6_tnl_err - tunnel error handler
472 *
473 * Description:
474 * ip6_tnl_err() should handle errors in the tunnel according
475 * to the specifications in RFC 2473.
476 **/
477
478 static int
ip6_tnl_err(struct sk_buff * skb,__u8 ipproto,struct inet6_skb_parm * opt,u8 * type,u8 * code,int * msg,__u32 * info,int offset)479 ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,
480 u8 *type, u8 *code, int *msg, __u32 *info, int offset)
481 {
482 const struct ipv6hdr *ipv6h = (const struct ipv6hdr *) skb->data;
483 struct ip6_tnl *t;
484 int rel_msg = 0;
485 u8 rel_type = ICMPV6_DEST_UNREACH;
486 u8 rel_code = ICMPV6_ADDR_UNREACH;
487 __u32 rel_info = 0;
488 __u16 len;
489 int err = -ENOENT;
490
491 /* If the packet doesn't contain the original IPv6 header we are
492 in trouble since we might need the source address for further
493 processing of the error. */
494
495 rcu_read_lock();
496 if ((t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->daddr,
497 &ipv6h->saddr)) == NULL)
498 goto out;
499
500 if (t->parms.proto != ipproto && t->parms.proto != 0)
501 goto out;
502
503 err = 0;
504
505 switch (*type) {
506 __u32 teli;
507 struct ipv6_tlv_tnl_enc_lim *tel;
508 __u32 mtu;
509 case ICMPV6_DEST_UNREACH:
510 net_warn_ratelimited("%s: Path to destination invalid or inactive!\n",
511 t->parms.name);
512 rel_msg = 1;
513 break;
514 case ICMPV6_TIME_EXCEED:
515 if ((*code) == ICMPV6_EXC_HOPLIMIT) {
516 net_warn_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
517 t->parms.name);
518 rel_msg = 1;
519 }
520 break;
521 case ICMPV6_PARAMPROB:
522 teli = 0;
523 if ((*code) == ICMPV6_HDR_FIELD)
524 teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
525
526 if (teli && teli == *info - 2) {
527 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
528 if (tel->encap_limit == 0) {
529 net_warn_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
530 t->parms.name);
531 rel_msg = 1;
532 }
533 } else {
534 net_warn_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
535 t->parms.name);
536 }
537 break;
538 case ICMPV6_PKT_TOOBIG:
539 mtu = *info - offset;
540 if (mtu < IPV6_MIN_MTU)
541 mtu = IPV6_MIN_MTU;
542 t->dev->mtu = mtu;
543
544 if ((len = sizeof(*ipv6h) + ntohs(ipv6h->payload_len)) > mtu) {
545 rel_type = ICMPV6_PKT_TOOBIG;
546 rel_code = 0;
547 rel_info = mtu;
548 rel_msg = 1;
549 }
550 break;
551 }
552
553 *type = rel_type;
554 *code = rel_code;
555 *info = rel_info;
556 *msg = rel_msg;
557
558 out:
559 rcu_read_unlock();
560 return err;
561 }
562
563 static int
ip4ip6_err(struct sk_buff * skb,struct inet6_skb_parm * opt,u8 type,u8 code,int offset,__be32 info)564 ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
565 u8 type, u8 code, int offset, __be32 info)
566 {
567 int rel_msg = 0;
568 u8 rel_type = type;
569 u8 rel_code = code;
570 __u32 rel_info = ntohl(info);
571 int err;
572 struct sk_buff *skb2;
573 const struct iphdr *eiph;
574 struct rtable *rt;
575 struct flowi4 fl4;
576
577 err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code,
578 &rel_msg, &rel_info, offset);
579 if (err < 0)
580 return err;
581
582 if (rel_msg == 0)
583 return 0;
584
585 switch (rel_type) {
586 case ICMPV6_DEST_UNREACH:
587 if (rel_code != ICMPV6_ADDR_UNREACH)
588 return 0;
589 rel_type = ICMP_DEST_UNREACH;
590 rel_code = ICMP_HOST_UNREACH;
591 break;
592 case ICMPV6_PKT_TOOBIG:
593 if (rel_code != 0)
594 return 0;
595 rel_type = ICMP_DEST_UNREACH;
596 rel_code = ICMP_FRAG_NEEDED;
597 break;
598 case NDISC_REDIRECT:
599 rel_type = ICMP_REDIRECT;
600 rel_code = ICMP_REDIR_HOST;
601 default:
602 return 0;
603 }
604
605 if (!pskb_may_pull(skb, offset + sizeof(struct iphdr)))
606 return 0;
607
608 skb2 = skb_clone(skb, GFP_ATOMIC);
609 if (!skb2)
610 return 0;
611
612 skb_dst_drop(skb2);
613
614 skb_pull(skb2, offset);
615 skb_reset_network_header(skb2);
616 eiph = ip_hdr(skb2);
617
618 /* Try to guess incoming interface */
619 rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
620 eiph->saddr, 0,
621 0, 0,
622 IPPROTO_IPIP, RT_TOS(eiph->tos), 0);
623 if (IS_ERR(rt))
624 goto out;
625
626 skb2->dev = rt->dst.dev;
627
628 /* route "incoming" packet */
629 if (rt->rt_flags & RTCF_LOCAL) {
630 ip_rt_put(rt);
631 rt = NULL;
632 rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
633 eiph->daddr, eiph->saddr,
634 0, 0,
635 IPPROTO_IPIP,
636 RT_TOS(eiph->tos), 0);
637 if (IS_ERR(rt) ||
638 rt->dst.dev->type != ARPHRD_TUNNEL) {
639 if (!IS_ERR(rt))
640 ip_rt_put(rt);
641 goto out;
642 }
643 skb_dst_set(skb2, &rt->dst);
644 } else {
645 ip_rt_put(rt);
646 if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos,
647 skb2->dev) ||
648 skb_dst(skb2)->dev->type != ARPHRD_TUNNEL)
649 goto out;
650 }
651
652 /* change mtu on this route */
653 if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) {
654 if (rel_info > dst_mtu(skb_dst(skb2)))
655 goto out;
656
657 skb_dst(skb2)->ops->update_pmtu(skb_dst(skb2), NULL, skb2, rel_info);
658 }
659 if (rel_type == ICMP_REDIRECT)
660 skb_dst(skb2)->ops->redirect(skb_dst(skb2), NULL, skb2);
661
662 icmp_send(skb2, rel_type, rel_code, htonl(rel_info));
663
664 out:
665 kfree_skb(skb2);
666 return 0;
667 }
668
669 static int
ip6ip6_err(struct sk_buff * skb,struct inet6_skb_parm * opt,u8 type,u8 code,int offset,__be32 info)670 ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
671 u8 type, u8 code, int offset, __be32 info)
672 {
673 int rel_msg = 0;
674 u8 rel_type = type;
675 u8 rel_code = code;
676 __u32 rel_info = ntohl(info);
677 int err;
678
679 err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code,
680 &rel_msg, &rel_info, offset);
681 if (err < 0)
682 return err;
683
684 if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) {
685 struct rt6_info *rt;
686 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
687
688 if (!skb2)
689 return 0;
690
691 skb_dst_drop(skb2);
692 skb_pull(skb2, offset);
693 skb_reset_network_header(skb2);
694
695 /* Try to guess incoming interface */
696 rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr,
697 NULL, 0, 0);
698
699 if (rt && rt->dst.dev)
700 skb2->dev = rt->dst.dev;
701
702 icmpv6_send(skb2, rel_type, rel_code, rel_info);
703
704 ip6_rt_put(rt);
705
706 kfree_skb(skb2);
707 }
708
709 return 0;
710 }
711
ip4ip6_dscp_ecn_decapsulate(const struct ip6_tnl * t,const struct ipv6hdr * ipv6h,struct sk_buff * skb)712 static int ip4ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
713 const struct ipv6hdr *ipv6h,
714 struct sk_buff *skb)
715 {
716 __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
717
718 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
719 ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
720
721 return IP6_ECN_decapsulate(ipv6h, skb);
722 }
723
ip6ip6_dscp_ecn_decapsulate(const struct ip6_tnl * t,const struct ipv6hdr * ipv6h,struct sk_buff * skb)724 static int ip6ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
725 const struct ipv6hdr *ipv6h,
726 struct sk_buff *skb)
727 {
728 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
729 ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb));
730
731 return IP6_ECN_decapsulate(ipv6h, skb);
732 }
733
ip6_tnl_get_cap(struct ip6_tnl * t,const struct in6_addr * laddr,const struct in6_addr * raddr)734 __u32 ip6_tnl_get_cap(struct ip6_tnl *t,
735 const struct in6_addr *laddr,
736 const struct in6_addr *raddr)
737 {
738 struct __ip6_tnl_parm *p = &t->parms;
739 int ltype = ipv6_addr_type(laddr);
740 int rtype = ipv6_addr_type(raddr);
741 __u32 flags = 0;
742
743 if (ltype == IPV6_ADDR_ANY || rtype == IPV6_ADDR_ANY) {
744 flags = IP6_TNL_F_CAP_PER_PACKET;
745 } else if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
746 rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
747 !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
748 (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
749 if (ltype&IPV6_ADDR_UNICAST)
750 flags |= IP6_TNL_F_CAP_XMIT;
751 if (rtype&IPV6_ADDR_UNICAST)
752 flags |= IP6_TNL_F_CAP_RCV;
753 }
754 return flags;
755 }
756 EXPORT_SYMBOL(ip6_tnl_get_cap);
757
758 /* called with rcu_read_lock() */
ip6_tnl_rcv_ctl(struct ip6_tnl * t,const struct in6_addr * laddr,const struct in6_addr * raddr)759 int ip6_tnl_rcv_ctl(struct ip6_tnl *t,
760 const struct in6_addr *laddr,
761 const struct in6_addr *raddr)
762 {
763 struct __ip6_tnl_parm *p = &t->parms;
764 int ret = 0;
765 struct net *net = t->net;
766
767 if ((p->flags & IP6_TNL_F_CAP_RCV) ||
768 ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
769 (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_RCV))) {
770 struct net_device *ldev = NULL;
771
772 if (p->link)
773 ldev = dev_get_by_index_rcu(net, p->link);
774
775 if ((ipv6_addr_is_multicast(laddr) ||
776 likely(ipv6_chk_addr(net, laddr, ldev, 0))) &&
777 likely(!ipv6_chk_addr(net, raddr, NULL, 0)))
778 ret = 1;
779 }
780 return ret;
781 }
782 EXPORT_SYMBOL_GPL(ip6_tnl_rcv_ctl);
783
784 /**
785 * ip6_tnl_rcv - decapsulate IPv6 packet and retransmit it locally
786 * @skb: received socket buffer
787 * @protocol: ethernet protocol ID
788 * @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN
789 *
790 * Return: 0
791 **/
792
ip6_tnl_rcv(struct sk_buff * skb,__u16 protocol,__u8 ipproto,int (* dscp_ecn_decapsulate)(const struct ip6_tnl * t,const struct ipv6hdr * ipv6h,struct sk_buff * skb))793 static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol,
794 __u8 ipproto,
795 int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t,
796 const struct ipv6hdr *ipv6h,
797 struct sk_buff *skb))
798 {
799 struct ip6_tnl *t;
800 const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
801 int err;
802
803 rcu_read_lock();
804
805 if ((t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr,
806 &ipv6h->daddr)) != NULL) {
807 struct pcpu_sw_netstats *tstats;
808
809 if (t->parms.proto != ipproto && t->parms.proto != 0) {
810 rcu_read_unlock();
811 goto discard;
812 }
813
814 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
815 rcu_read_unlock();
816 goto discard;
817 }
818
819 if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) {
820 t->dev->stats.rx_dropped++;
821 rcu_read_unlock();
822 goto discard;
823 }
824 skb->mac_header = skb->network_header;
825 skb_reset_network_header(skb);
826 skb->protocol = htons(protocol);
827 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
828
829 __skb_tunnel_rx(skb, t->dev, t->net);
830
831 err = dscp_ecn_decapsulate(t, ipv6h, skb);
832 if (unlikely(err)) {
833 if (log_ecn_error)
834 net_info_ratelimited("non-ECT from %pI6 with dsfield=%#x\n",
835 &ipv6h->saddr,
836 ipv6_get_dsfield(ipv6h));
837 if (err > 1) {
838 ++t->dev->stats.rx_frame_errors;
839 ++t->dev->stats.rx_errors;
840 rcu_read_unlock();
841 goto discard;
842 }
843 }
844
845 tstats = this_cpu_ptr(t->dev->tstats);
846 u64_stats_update_begin(&tstats->syncp);
847 tstats->rx_packets++;
848 tstats->rx_bytes += skb->len;
849 u64_stats_update_end(&tstats->syncp);
850
851 netif_rx(skb);
852
853 rcu_read_unlock();
854 return 0;
855 }
856 rcu_read_unlock();
857 return 1;
858
859 discard:
860 kfree_skb(skb);
861 return 0;
862 }
863
ip4ip6_rcv(struct sk_buff * skb)864 static int ip4ip6_rcv(struct sk_buff *skb)
865 {
866 return ip6_tnl_rcv(skb, ETH_P_IP, IPPROTO_IPIP,
867 ip4ip6_dscp_ecn_decapsulate);
868 }
869
ip6ip6_rcv(struct sk_buff * skb)870 static int ip6ip6_rcv(struct sk_buff *skb)
871 {
872 return ip6_tnl_rcv(skb, ETH_P_IPV6, IPPROTO_IPV6,
873 ip6ip6_dscp_ecn_decapsulate);
874 }
875
876 struct ipv6_tel_txoption {
877 struct ipv6_txoptions ops;
878 __u8 dst_opt[8];
879 };
880
init_tel_txopt(struct ipv6_tel_txoption * opt,__u8 encap_limit)881 static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
882 {
883 memset(opt, 0, sizeof(struct ipv6_tel_txoption));
884
885 opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
886 opt->dst_opt[3] = 1;
887 opt->dst_opt[4] = encap_limit;
888 opt->dst_opt[5] = IPV6_TLV_PADN;
889 opt->dst_opt[6] = 1;
890
891 opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
892 opt->ops.opt_nflen = 8;
893 }
894
895 /**
896 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
897 * @t: the outgoing tunnel device
898 * @hdr: IPv6 header from the incoming packet
899 *
900 * Description:
901 * Avoid trivial tunneling loop by checking that tunnel exit-point
902 * doesn't match source of incoming packet.
903 *
904 * Return:
905 * 1 if conflict,
906 * 0 else
907 **/
908
909 static inline bool
ip6_tnl_addr_conflict(const struct ip6_tnl * t,const struct ipv6hdr * hdr)910 ip6_tnl_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
911 {
912 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
913 }
914
ip6_tnl_xmit_ctl(struct ip6_tnl * t)915 int ip6_tnl_xmit_ctl(struct ip6_tnl *t)
916 {
917 struct __ip6_tnl_parm *p = &t->parms;
918 int ret = 0;
919 struct net *net = t->net;
920
921 if (p->flags & IP6_TNL_F_CAP_XMIT) {
922 struct net_device *ldev = NULL;
923
924 rcu_read_lock();
925 if (p->link)
926 ldev = dev_get_by_index_rcu(net, p->link);
927
928 if (unlikely(!ipv6_chk_addr(net, &p->laddr, ldev, 0)))
929 pr_warn("%s xmit: Local address not yet configured!\n",
930 p->name);
931 else if (!ipv6_addr_is_multicast(&p->raddr) &&
932 unlikely(ipv6_chk_addr(net, &p->raddr, NULL, 0)))
933 pr_warn("%s xmit: Routing loop! Remote address found on this node!\n",
934 p->name);
935 else
936 ret = 1;
937 rcu_read_unlock();
938 }
939 return ret;
940 }
941 EXPORT_SYMBOL_GPL(ip6_tnl_xmit_ctl);
942
943 /**
944 * ip6_tnl_xmit2 - encapsulate packet and send
945 * @skb: the outgoing socket buffer
946 * @dev: the outgoing tunnel device
947 * @dsfield: dscp code for outer header
948 * @fl: flow of tunneled packet
949 * @encap_limit: encapsulation limit
950 * @pmtu: Path MTU is stored if packet is too big
951 *
952 * Description:
953 * Build new header and do some sanity checks on the packet before sending
954 * it.
955 *
956 * Return:
957 * 0 on success
958 * -1 fail
959 * %-EMSGSIZE message too big. return mtu in this case.
960 **/
961
ip6_tnl_xmit2(struct sk_buff * skb,struct net_device * dev,__u8 dsfield,struct flowi6 * fl6,int encap_limit,__u32 * pmtu)962 static int ip6_tnl_xmit2(struct sk_buff *skb,
963 struct net_device *dev,
964 __u8 dsfield,
965 struct flowi6 *fl6,
966 int encap_limit,
967 __u32 *pmtu)
968 {
969 struct ip6_tnl *t = netdev_priv(dev);
970 struct net *net = t->net;
971 struct net_device_stats *stats = &t->dev->stats;
972 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
973 struct ipv6_tel_txoption opt;
974 struct dst_entry *dst = NULL, *ndst = NULL;
975 struct net_device *tdev;
976 int mtu;
977 unsigned int max_headroom = sizeof(struct ipv6hdr);
978 u8 proto;
979 int err = -1;
980
981 if (!fl6->flowi6_mark)
982 dst = ip6_tnl_dst_check(t);
983 if (!dst) {
984 ndst = ip6_route_output(net, NULL, fl6);
985
986 if (ndst->error)
987 goto tx_err_link_failure;
988 ndst = xfrm_lookup(net, ndst, flowi6_to_flowi(fl6), NULL, 0);
989 if (IS_ERR(ndst)) {
990 err = PTR_ERR(ndst);
991 ndst = NULL;
992 goto tx_err_link_failure;
993 }
994 dst = ndst;
995 }
996
997 tdev = dst->dev;
998
999 if (tdev == dev) {
1000 stats->collisions++;
1001 net_warn_ratelimited("%s: Local routing loop detected!\n",
1002 t->parms.name);
1003 goto tx_err_dst_release;
1004 }
1005 mtu = dst_mtu(dst) - sizeof(*ipv6h);
1006 if (encap_limit >= 0) {
1007 max_headroom += 8;
1008 mtu -= 8;
1009 }
1010 if (mtu < IPV6_MIN_MTU)
1011 mtu = IPV6_MIN_MTU;
1012 if (skb_dst(skb))
1013 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
1014 if (skb->len > mtu) {
1015 *pmtu = mtu;
1016 err = -EMSGSIZE;
1017 goto tx_err_dst_release;
1018 }
1019
1020 skb_scrub_packet(skb, !net_eq(t->net, dev_net(dev)));
1021
1022 /*
1023 * Okay, now see if we can stuff it in the buffer as-is.
1024 */
1025 max_headroom += LL_RESERVED_SPACE(tdev);
1026
1027 if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
1028 (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
1029 struct sk_buff *new_skb;
1030
1031 if (!(new_skb = skb_realloc_headroom(skb, max_headroom)))
1032 goto tx_err_dst_release;
1033
1034 if (skb->sk)
1035 skb_set_owner_w(new_skb, skb->sk);
1036 consume_skb(skb);
1037 skb = new_skb;
1038 }
1039 if (fl6->flowi6_mark) {
1040 skb_dst_set(skb, dst);
1041 ndst = NULL;
1042 } else {
1043 skb_dst_set_noref(skb, dst);
1044 }
1045 skb->transport_header = skb->network_header;
1046
1047 proto = fl6->flowi6_proto;
1048 if (encap_limit >= 0) {
1049 init_tel_txopt(&opt, encap_limit);
1050 ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
1051 }
1052
1053 if (likely(!skb->encapsulation)) {
1054 skb_reset_inner_headers(skb);
1055 skb->encapsulation = 1;
1056 }
1057
1058 skb_push(skb, sizeof(struct ipv6hdr));
1059 skb_reset_network_header(skb);
1060 ipv6h = ipv6_hdr(skb);
1061 ip6_flow_hdr(ipv6h, INET_ECN_encapsulate(0, dsfield),
1062 ip6_make_flowlabel(net, skb, fl6->flowlabel, false));
1063 ipv6h->hop_limit = t->parms.hop_limit;
1064 ipv6h->nexthdr = proto;
1065 ipv6h->saddr = fl6->saddr;
1066 ipv6h->daddr = fl6->daddr;
1067 ip6tunnel_xmit(skb, dev);
1068 if (ndst)
1069 ip6_tnl_dst_store(t, ndst);
1070 return 0;
1071 tx_err_link_failure:
1072 stats->tx_carrier_errors++;
1073 dst_link_failure(skb);
1074 tx_err_dst_release:
1075 dst_release(ndst);
1076 return err;
1077 }
1078
1079 static inline int
ip4ip6_tnl_xmit(struct sk_buff * skb,struct net_device * dev)1080 ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1081 {
1082 struct ip6_tnl *t = netdev_priv(dev);
1083 const struct iphdr *iph = ip_hdr(skb);
1084 int encap_limit = -1;
1085 struct flowi6 fl6;
1086 __u8 dsfield;
1087 __u32 mtu;
1088 int err;
1089
1090 if ((t->parms.proto != IPPROTO_IPIP && t->parms.proto != 0) ||
1091 !ip6_tnl_xmit_ctl(t))
1092 return -1;
1093
1094 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1095 encap_limit = t->parms.encap_limit;
1096
1097 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
1098 fl6.flowi6_proto = IPPROTO_IPIP;
1099
1100 fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
1101
1102 dsfield = ipv4_get_dsfield(iph);
1103
1104 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
1105 fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
1106 & IPV6_TCLASS_MASK;
1107 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1108 fl6.flowi6_mark = skb->mark;
1109
1110 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
1111 if (err != 0) {
1112 /* XXX: send ICMP error even if DF is not set. */
1113 if (err == -EMSGSIZE)
1114 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
1115 htonl(mtu));
1116 return -1;
1117 }
1118
1119 return 0;
1120 }
1121
1122 static inline int
ip6ip6_tnl_xmit(struct sk_buff * skb,struct net_device * dev)1123 ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1124 {
1125 struct ip6_tnl *t = netdev_priv(dev);
1126 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
1127 int encap_limit = -1;
1128 __u16 offset;
1129 struct flowi6 fl6;
1130 __u8 dsfield;
1131 __u32 mtu;
1132 int err;
1133
1134 if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) ||
1135 !ip6_tnl_xmit_ctl(t) || ip6_tnl_addr_conflict(t, ipv6h))
1136 return -1;
1137
1138 offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
1139 if (offset > 0) {
1140 struct ipv6_tlv_tnl_enc_lim *tel;
1141 tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
1142 if (tel->encap_limit == 0) {
1143 icmpv6_send(skb, ICMPV6_PARAMPROB,
1144 ICMPV6_HDR_FIELD, offset + 2);
1145 return -1;
1146 }
1147 encap_limit = tel->encap_limit - 1;
1148 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1149 encap_limit = t->parms.encap_limit;
1150
1151 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
1152 fl6.flowi6_proto = IPPROTO_IPV6;
1153 fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
1154
1155 dsfield = ipv6_get_dsfield(ipv6h);
1156 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
1157 fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
1158 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
1159 fl6.flowlabel |= ip6_flowlabel(ipv6h);
1160 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1161 fl6.flowi6_mark = skb->mark;
1162
1163 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
1164 if (err != 0) {
1165 if (err == -EMSGSIZE)
1166 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1167 return -1;
1168 }
1169
1170 return 0;
1171 }
1172
1173 static netdev_tx_t
ip6_tnl_xmit(struct sk_buff * skb,struct net_device * dev)1174 ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1175 {
1176 struct ip6_tnl *t = netdev_priv(dev);
1177 struct net_device_stats *stats = &t->dev->stats;
1178 int ret;
1179
1180 switch (skb->protocol) {
1181 case htons(ETH_P_IP):
1182 ret = ip4ip6_tnl_xmit(skb, dev);
1183 break;
1184 case htons(ETH_P_IPV6):
1185 ret = ip6ip6_tnl_xmit(skb, dev);
1186 break;
1187 default:
1188 goto tx_err;
1189 }
1190
1191 if (ret < 0)
1192 goto tx_err;
1193
1194 return NETDEV_TX_OK;
1195
1196 tx_err:
1197 stats->tx_errors++;
1198 stats->tx_dropped++;
1199 kfree_skb(skb);
1200 return NETDEV_TX_OK;
1201 }
1202
ip6_tnl_link_config(struct ip6_tnl * t)1203 static void ip6_tnl_link_config(struct ip6_tnl *t)
1204 {
1205 struct net_device *dev = t->dev;
1206 struct __ip6_tnl_parm *p = &t->parms;
1207 struct flowi6 *fl6 = &t->fl.u.ip6;
1208
1209 memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
1210 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
1211
1212 /* Set up flowi template */
1213 fl6->saddr = p->laddr;
1214 fl6->daddr = p->raddr;
1215 fl6->flowi6_oif = p->link;
1216 fl6->flowlabel = 0;
1217
1218 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
1219 fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
1220 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
1221 fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
1222
1223 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
1224 p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
1225
1226 if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
1227 dev->flags |= IFF_POINTOPOINT;
1228 else
1229 dev->flags &= ~IFF_POINTOPOINT;
1230
1231 dev->iflink = p->link;
1232
1233 if (p->flags & IP6_TNL_F_CAP_XMIT) {
1234 int strict = (ipv6_addr_type(&p->raddr) &
1235 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
1236
1237 struct rt6_info *rt = rt6_lookup(t->net,
1238 &p->raddr, &p->laddr,
1239 p->link, strict);
1240
1241 if (rt == NULL)
1242 return;
1243
1244 if (rt->dst.dev) {
1245 dev->hard_header_len = rt->dst.dev->hard_header_len +
1246 sizeof(struct ipv6hdr);
1247
1248 dev->mtu = rt->dst.dev->mtu - sizeof(struct ipv6hdr);
1249 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1250 dev->mtu -= 8;
1251
1252 if (dev->mtu < IPV6_MIN_MTU)
1253 dev->mtu = IPV6_MIN_MTU;
1254 }
1255 ip6_rt_put(rt);
1256 }
1257 }
1258
1259 /**
1260 * ip6_tnl_change - update the tunnel parameters
1261 * @t: tunnel to be changed
1262 * @p: tunnel configuration parameters
1263 *
1264 * Description:
1265 * ip6_tnl_change() updates the tunnel parameters
1266 **/
1267
1268 static int
ip6_tnl_change(struct ip6_tnl * t,const struct __ip6_tnl_parm * p)1269 ip6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
1270 {
1271 t->parms.laddr = p->laddr;
1272 t->parms.raddr = p->raddr;
1273 t->parms.flags = p->flags;
1274 t->parms.hop_limit = p->hop_limit;
1275 t->parms.encap_limit = p->encap_limit;
1276 t->parms.flowinfo = p->flowinfo;
1277 t->parms.link = p->link;
1278 t->parms.proto = p->proto;
1279 ip6_tnl_dst_reset(t);
1280 ip6_tnl_link_config(t);
1281 return 0;
1282 }
1283
ip6_tnl_update(struct ip6_tnl * t,struct __ip6_tnl_parm * p)1284 static int ip6_tnl_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
1285 {
1286 struct net *net = t->net;
1287 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1288 int err;
1289
1290 ip6_tnl_unlink(ip6n, t);
1291 synchronize_net();
1292 err = ip6_tnl_change(t, p);
1293 ip6_tnl_link(ip6n, t);
1294 netdev_state_change(t->dev);
1295 return err;
1296 }
1297
1298 static void
ip6_tnl_parm_from_user(struct __ip6_tnl_parm * p,const struct ip6_tnl_parm * u)1299 ip6_tnl_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm *u)
1300 {
1301 p->laddr = u->laddr;
1302 p->raddr = u->raddr;
1303 p->flags = u->flags;
1304 p->hop_limit = u->hop_limit;
1305 p->encap_limit = u->encap_limit;
1306 p->flowinfo = u->flowinfo;
1307 p->link = u->link;
1308 p->proto = u->proto;
1309 memcpy(p->name, u->name, sizeof(u->name));
1310 }
1311
1312 static void
ip6_tnl_parm_to_user(struct ip6_tnl_parm * u,const struct __ip6_tnl_parm * p)1313 ip6_tnl_parm_to_user(struct ip6_tnl_parm *u, const struct __ip6_tnl_parm *p)
1314 {
1315 u->laddr = p->laddr;
1316 u->raddr = p->raddr;
1317 u->flags = p->flags;
1318 u->hop_limit = p->hop_limit;
1319 u->encap_limit = p->encap_limit;
1320 u->flowinfo = p->flowinfo;
1321 u->link = p->link;
1322 u->proto = p->proto;
1323 memcpy(u->name, p->name, sizeof(u->name));
1324 }
1325
1326 /**
1327 * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
1328 * @dev: virtual device associated with tunnel
1329 * @ifr: parameters passed from userspace
1330 * @cmd: command to be performed
1331 *
1332 * Description:
1333 * ip6_tnl_ioctl() is used for managing IPv6 tunnels
1334 * from userspace.
1335 *
1336 * The possible commands are the following:
1337 * %SIOCGETTUNNEL: get tunnel parameters for device
1338 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
1339 * %SIOCCHGTUNNEL: change tunnel parameters to those given
1340 * %SIOCDELTUNNEL: delete tunnel
1341 *
1342 * The fallback device "ip6tnl0", created during module
1343 * initialization, can be used for creating other tunnel devices.
1344 *
1345 * Return:
1346 * 0 on success,
1347 * %-EFAULT if unable to copy data to or from userspace,
1348 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
1349 * %-EINVAL if passed tunnel parameters are invalid,
1350 * %-EEXIST if changing a tunnel's parameters would cause a conflict
1351 * %-ENODEV if attempting to change or delete a nonexisting device
1352 **/
1353
1354 static int
ip6_tnl_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)1355 ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1356 {
1357 int err = 0;
1358 struct ip6_tnl_parm p;
1359 struct __ip6_tnl_parm p1;
1360 struct ip6_tnl *t = netdev_priv(dev);
1361 struct net *net = t->net;
1362 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1363
1364 switch (cmd) {
1365 case SIOCGETTUNNEL:
1366 if (dev == ip6n->fb_tnl_dev) {
1367 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
1368 err = -EFAULT;
1369 break;
1370 }
1371 ip6_tnl_parm_from_user(&p1, &p);
1372 t = ip6_tnl_locate(net, &p1, 0);
1373 if (t == NULL)
1374 t = netdev_priv(dev);
1375 } else {
1376 memset(&p, 0, sizeof(p));
1377 }
1378 ip6_tnl_parm_to_user(&p, &t->parms);
1379 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) {
1380 err = -EFAULT;
1381 }
1382 break;
1383 case SIOCADDTUNNEL:
1384 case SIOCCHGTUNNEL:
1385 err = -EPERM;
1386 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1387 break;
1388 err = -EFAULT;
1389 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1390 break;
1391 err = -EINVAL;
1392 if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
1393 p.proto != 0)
1394 break;
1395 ip6_tnl_parm_from_user(&p1, &p);
1396 t = ip6_tnl_locate(net, &p1, cmd == SIOCADDTUNNEL);
1397 if (dev != ip6n->fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
1398 if (t != NULL) {
1399 if (t->dev != dev) {
1400 err = -EEXIST;
1401 break;
1402 }
1403 } else
1404 t = netdev_priv(dev);
1405
1406 err = ip6_tnl_update(t, &p1);
1407 }
1408 if (t) {
1409 err = 0;
1410 ip6_tnl_parm_to_user(&p, &t->parms);
1411 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
1412 err = -EFAULT;
1413
1414 } else
1415 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
1416 break;
1417 case SIOCDELTUNNEL:
1418 err = -EPERM;
1419 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1420 break;
1421
1422 if (dev == ip6n->fb_tnl_dev) {
1423 err = -EFAULT;
1424 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1425 break;
1426 err = -ENOENT;
1427 ip6_tnl_parm_from_user(&p1, &p);
1428 t = ip6_tnl_locate(net, &p1, 0);
1429 if (t == NULL)
1430 break;
1431 err = -EPERM;
1432 if (t->dev == ip6n->fb_tnl_dev)
1433 break;
1434 dev = t->dev;
1435 }
1436 err = 0;
1437 unregister_netdevice(dev);
1438 break;
1439 default:
1440 err = -EINVAL;
1441 }
1442 return err;
1443 }
1444
1445 /**
1446 * ip6_tnl_change_mtu - change mtu manually for tunnel device
1447 * @dev: virtual device associated with tunnel
1448 * @new_mtu: the new mtu
1449 *
1450 * Return:
1451 * 0 on success,
1452 * %-EINVAL if mtu too small
1453 **/
1454
1455 static int
ip6_tnl_change_mtu(struct net_device * dev,int new_mtu)1456 ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
1457 {
1458 struct ip6_tnl *tnl = netdev_priv(dev);
1459
1460 if (tnl->parms.proto == IPPROTO_IPIP) {
1461 if (new_mtu < 68)
1462 return -EINVAL;
1463 } else {
1464 if (new_mtu < IPV6_MIN_MTU)
1465 return -EINVAL;
1466 }
1467 if (new_mtu > 0xFFF8 - dev->hard_header_len)
1468 return -EINVAL;
1469 dev->mtu = new_mtu;
1470 return 0;
1471 }
1472
1473
1474 static const struct net_device_ops ip6_tnl_netdev_ops = {
1475 .ndo_init = ip6_tnl_dev_init,
1476 .ndo_uninit = ip6_tnl_dev_uninit,
1477 .ndo_start_xmit = ip6_tnl_xmit,
1478 .ndo_do_ioctl = ip6_tnl_ioctl,
1479 .ndo_change_mtu = ip6_tnl_change_mtu,
1480 .ndo_get_stats = ip6_get_stats,
1481 };
1482
1483
1484 /**
1485 * ip6_tnl_dev_setup - setup virtual tunnel device
1486 * @dev: virtual device associated with tunnel
1487 *
1488 * Description:
1489 * Initialize function pointers and device parameters
1490 **/
1491
ip6_tnl_dev_setup(struct net_device * dev)1492 static void ip6_tnl_dev_setup(struct net_device *dev)
1493 {
1494 struct ip6_tnl *t;
1495
1496 dev->netdev_ops = &ip6_tnl_netdev_ops;
1497 dev->destructor = ip6_dev_free;
1498
1499 dev->type = ARPHRD_TUNNEL6;
1500 dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr);
1501 dev->mtu = ETH_DATA_LEN - sizeof(struct ipv6hdr);
1502 t = netdev_priv(dev);
1503 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1504 dev->mtu -= 8;
1505 dev->flags |= IFF_NOARP;
1506 dev->addr_len = sizeof(struct in6_addr);
1507 netif_keep_dst(dev);
1508 /* This perm addr will be used as interface identifier by IPv6 */
1509 dev->addr_assign_type = NET_ADDR_RANDOM;
1510 eth_random_addr(dev->perm_addr);
1511 }
1512
1513
1514 /**
1515 * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
1516 * @dev: virtual device associated with tunnel
1517 **/
1518
1519 static inline int
ip6_tnl_dev_init_gen(struct net_device * dev)1520 ip6_tnl_dev_init_gen(struct net_device *dev)
1521 {
1522 struct ip6_tnl *t = netdev_priv(dev);
1523
1524 t->dev = dev;
1525 t->net = dev_net(dev);
1526 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1527 if (!dev->tstats)
1528 return -ENOMEM;
1529 return 0;
1530 }
1531
1532 /**
1533 * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
1534 * @dev: virtual device associated with tunnel
1535 **/
1536
ip6_tnl_dev_init(struct net_device * dev)1537 static int ip6_tnl_dev_init(struct net_device *dev)
1538 {
1539 struct ip6_tnl *t = netdev_priv(dev);
1540 int err = ip6_tnl_dev_init_gen(dev);
1541
1542 if (err)
1543 return err;
1544 ip6_tnl_link_config(t);
1545 return 0;
1546 }
1547
1548 /**
1549 * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
1550 * @dev: fallback device
1551 *
1552 * Return: 0
1553 **/
1554
ip6_fb_tnl_dev_init(struct net_device * dev)1555 static int __net_init ip6_fb_tnl_dev_init(struct net_device *dev)
1556 {
1557 struct ip6_tnl *t = netdev_priv(dev);
1558 struct net *net = dev_net(dev);
1559 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1560
1561 t->parms.proto = IPPROTO_IPV6;
1562 dev_hold(dev);
1563
1564 rcu_assign_pointer(ip6n->tnls_wc[0], t);
1565 return 0;
1566 }
1567
ip6_tnl_validate(struct nlattr * tb[],struct nlattr * data[])1568 static int ip6_tnl_validate(struct nlattr *tb[], struct nlattr *data[])
1569 {
1570 u8 proto;
1571
1572 if (!data || !data[IFLA_IPTUN_PROTO])
1573 return 0;
1574
1575 proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
1576 if (proto != IPPROTO_IPV6 &&
1577 proto != IPPROTO_IPIP &&
1578 proto != 0)
1579 return -EINVAL;
1580
1581 return 0;
1582 }
1583
ip6_tnl_netlink_parms(struct nlattr * data[],struct __ip6_tnl_parm * parms)1584 static void ip6_tnl_netlink_parms(struct nlattr *data[],
1585 struct __ip6_tnl_parm *parms)
1586 {
1587 memset(parms, 0, sizeof(*parms));
1588
1589 if (!data)
1590 return;
1591
1592 if (data[IFLA_IPTUN_LINK])
1593 parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
1594
1595 if (data[IFLA_IPTUN_LOCAL])
1596 nla_memcpy(&parms->laddr, data[IFLA_IPTUN_LOCAL],
1597 sizeof(struct in6_addr));
1598
1599 if (data[IFLA_IPTUN_REMOTE])
1600 nla_memcpy(&parms->raddr, data[IFLA_IPTUN_REMOTE],
1601 sizeof(struct in6_addr));
1602
1603 if (data[IFLA_IPTUN_TTL])
1604 parms->hop_limit = nla_get_u8(data[IFLA_IPTUN_TTL]);
1605
1606 if (data[IFLA_IPTUN_ENCAP_LIMIT])
1607 parms->encap_limit = nla_get_u8(data[IFLA_IPTUN_ENCAP_LIMIT]);
1608
1609 if (data[IFLA_IPTUN_FLOWINFO])
1610 parms->flowinfo = nla_get_be32(data[IFLA_IPTUN_FLOWINFO]);
1611
1612 if (data[IFLA_IPTUN_FLAGS])
1613 parms->flags = nla_get_u32(data[IFLA_IPTUN_FLAGS]);
1614
1615 if (data[IFLA_IPTUN_PROTO])
1616 parms->proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
1617 }
1618
ip6_tnl_newlink(struct net * src_net,struct net_device * dev,struct nlattr * tb[],struct nlattr * data[])1619 static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev,
1620 struct nlattr *tb[], struct nlattr *data[])
1621 {
1622 struct net *net = dev_net(dev);
1623 struct ip6_tnl *nt;
1624
1625 nt = netdev_priv(dev);
1626 ip6_tnl_netlink_parms(data, &nt->parms);
1627
1628 if (ip6_tnl_locate(net, &nt->parms, 0))
1629 return -EEXIST;
1630
1631 return ip6_tnl_create2(dev);
1632 }
1633
ip6_tnl_changelink(struct net_device * dev,struct nlattr * tb[],struct nlattr * data[])1634 static int ip6_tnl_changelink(struct net_device *dev, struct nlattr *tb[],
1635 struct nlattr *data[])
1636 {
1637 struct ip6_tnl *t = netdev_priv(dev);
1638 struct __ip6_tnl_parm p;
1639 struct net *net = t->net;
1640 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1641
1642 if (dev == ip6n->fb_tnl_dev)
1643 return -EINVAL;
1644
1645 ip6_tnl_netlink_parms(data, &p);
1646
1647 t = ip6_tnl_locate(net, &p, 0);
1648
1649 if (t) {
1650 if (t->dev != dev)
1651 return -EEXIST;
1652 } else
1653 t = netdev_priv(dev);
1654
1655 return ip6_tnl_update(t, &p);
1656 }
1657
ip6_tnl_dellink(struct net_device * dev,struct list_head * head)1658 static void ip6_tnl_dellink(struct net_device *dev, struct list_head *head)
1659 {
1660 struct net *net = dev_net(dev);
1661 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1662
1663 if (dev != ip6n->fb_tnl_dev)
1664 unregister_netdevice_queue(dev, head);
1665 }
1666
ip6_tnl_get_size(const struct net_device * dev)1667 static size_t ip6_tnl_get_size(const struct net_device *dev)
1668 {
1669 return
1670 /* IFLA_IPTUN_LINK */
1671 nla_total_size(4) +
1672 /* IFLA_IPTUN_LOCAL */
1673 nla_total_size(sizeof(struct in6_addr)) +
1674 /* IFLA_IPTUN_REMOTE */
1675 nla_total_size(sizeof(struct in6_addr)) +
1676 /* IFLA_IPTUN_TTL */
1677 nla_total_size(1) +
1678 /* IFLA_IPTUN_ENCAP_LIMIT */
1679 nla_total_size(1) +
1680 /* IFLA_IPTUN_FLOWINFO */
1681 nla_total_size(4) +
1682 /* IFLA_IPTUN_FLAGS */
1683 nla_total_size(4) +
1684 /* IFLA_IPTUN_PROTO */
1685 nla_total_size(1) +
1686 0;
1687 }
1688
ip6_tnl_fill_info(struct sk_buff * skb,const struct net_device * dev)1689 static int ip6_tnl_fill_info(struct sk_buff *skb, const struct net_device *dev)
1690 {
1691 struct ip6_tnl *tunnel = netdev_priv(dev);
1692 struct __ip6_tnl_parm *parm = &tunnel->parms;
1693
1694 if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
1695 nla_put(skb, IFLA_IPTUN_LOCAL, sizeof(struct in6_addr),
1696 &parm->laddr) ||
1697 nla_put(skb, IFLA_IPTUN_REMOTE, sizeof(struct in6_addr),
1698 &parm->raddr) ||
1699 nla_put_u8(skb, IFLA_IPTUN_TTL, parm->hop_limit) ||
1700 nla_put_u8(skb, IFLA_IPTUN_ENCAP_LIMIT, parm->encap_limit) ||
1701 nla_put_be32(skb, IFLA_IPTUN_FLOWINFO, parm->flowinfo) ||
1702 nla_put_u32(skb, IFLA_IPTUN_FLAGS, parm->flags) ||
1703 nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->proto))
1704 goto nla_put_failure;
1705 return 0;
1706
1707 nla_put_failure:
1708 return -EMSGSIZE;
1709 }
1710
1711 static const struct nla_policy ip6_tnl_policy[IFLA_IPTUN_MAX + 1] = {
1712 [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
1713 [IFLA_IPTUN_LOCAL] = { .len = sizeof(struct in6_addr) },
1714 [IFLA_IPTUN_REMOTE] = { .len = sizeof(struct in6_addr) },
1715 [IFLA_IPTUN_TTL] = { .type = NLA_U8 },
1716 [IFLA_IPTUN_ENCAP_LIMIT] = { .type = NLA_U8 },
1717 [IFLA_IPTUN_FLOWINFO] = { .type = NLA_U32 },
1718 [IFLA_IPTUN_FLAGS] = { .type = NLA_U32 },
1719 [IFLA_IPTUN_PROTO] = { .type = NLA_U8 },
1720 };
1721
1722 static struct rtnl_link_ops ip6_link_ops __read_mostly = {
1723 .kind = "ip6tnl",
1724 .maxtype = IFLA_IPTUN_MAX,
1725 .policy = ip6_tnl_policy,
1726 .priv_size = sizeof(struct ip6_tnl),
1727 .setup = ip6_tnl_dev_setup,
1728 .validate = ip6_tnl_validate,
1729 .newlink = ip6_tnl_newlink,
1730 .changelink = ip6_tnl_changelink,
1731 .dellink = ip6_tnl_dellink,
1732 .get_size = ip6_tnl_get_size,
1733 .fill_info = ip6_tnl_fill_info,
1734 };
1735
1736 static struct xfrm6_tunnel ip4ip6_handler __read_mostly = {
1737 .handler = ip4ip6_rcv,
1738 .err_handler = ip4ip6_err,
1739 .priority = 1,
1740 };
1741
1742 static struct xfrm6_tunnel ip6ip6_handler __read_mostly = {
1743 .handler = ip6ip6_rcv,
1744 .err_handler = ip6ip6_err,
1745 .priority = 1,
1746 };
1747
ip6_tnl_destroy_tunnels(struct net * net)1748 static void __net_exit ip6_tnl_destroy_tunnels(struct net *net)
1749 {
1750 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1751 struct net_device *dev, *aux;
1752 int h;
1753 struct ip6_tnl *t;
1754 LIST_HEAD(list);
1755
1756 for_each_netdev_safe(net, dev, aux)
1757 if (dev->rtnl_link_ops == &ip6_link_ops)
1758 unregister_netdevice_queue(dev, &list);
1759
1760 for (h = 0; h < HASH_SIZE; h++) {
1761 t = rtnl_dereference(ip6n->tnls_r_l[h]);
1762 while (t != NULL) {
1763 /* If dev is in the same netns, it has already
1764 * been added to the list by the previous loop.
1765 */
1766 if (!net_eq(dev_net(t->dev), net))
1767 unregister_netdevice_queue(t->dev, &list);
1768 t = rtnl_dereference(t->next);
1769 }
1770 }
1771
1772 unregister_netdevice_many(&list);
1773 }
1774
ip6_tnl_init_net(struct net * net)1775 static int __net_init ip6_tnl_init_net(struct net *net)
1776 {
1777 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1778 struct ip6_tnl *t = NULL;
1779 int err;
1780
1781 ip6n->tnls[0] = ip6n->tnls_wc;
1782 ip6n->tnls[1] = ip6n->tnls_r_l;
1783
1784 err = -ENOMEM;
1785 ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
1786 NET_NAME_UNKNOWN, ip6_tnl_dev_setup);
1787
1788 if (!ip6n->fb_tnl_dev)
1789 goto err_alloc_dev;
1790 dev_net_set(ip6n->fb_tnl_dev, net);
1791 ip6n->fb_tnl_dev->rtnl_link_ops = &ip6_link_ops;
1792 /* FB netdevice is special: we have one, and only one per netns.
1793 * Allowing to move it to another netns is clearly unsafe.
1794 */
1795 ip6n->fb_tnl_dev->features |= NETIF_F_NETNS_LOCAL;
1796
1797 err = ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
1798 if (err < 0)
1799 goto err_register;
1800
1801 err = register_netdev(ip6n->fb_tnl_dev);
1802 if (err < 0)
1803 goto err_register;
1804
1805 t = netdev_priv(ip6n->fb_tnl_dev);
1806
1807 strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
1808 return 0;
1809
1810 err_register:
1811 ip6_dev_free(ip6n->fb_tnl_dev);
1812 err_alloc_dev:
1813 return err;
1814 }
1815
ip6_tnl_exit_net(struct net * net)1816 static void __net_exit ip6_tnl_exit_net(struct net *net)
1817 {
1818 rtnl_lock();
1819 ip6_tnl_destroy_tunnels(net);
1820 rtnl_unlock();
1821 }
1822
1823 static struct pernet_operations ip6_tnl_net_ops = {
1824 .init = ip6_tnl_init_net,
1825 .exit = ip6_tnl_exit_net,
1826 .id = &ip6_tnl_net_id,
1827 .size = sizeof(struct ip6_tnl_net),
1828 };
1829
1830 /**
1831 * ip6_tunnel_init - register protocol and reserve needed resources
1832 *
1833 * Return: 0 on success
1834 **/
1835
ip6_tunnel_init(void)1836 static int __init ip6_tunnel_init(void)
1837 {
1838 int err;
1839
1840 err = register_pernet_device(&ip6_tnl_net_ops);
1841 if (err < 0)
1842 goto out_pernet;
1843
1844 err = xfrm6_tunnel_register(&ip4ip6_handler, AF_INET);
1845 if (err < 0) {
1846 pr_err("%s: can't register ip4ip6\n", __func__);
1847 goto out_ip4ip6;
1848 }
1849
1850 err = xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6);
1851 if (err < 0) {
1852 pr_err("%s: can't register ip6ip6\n", __func__);
1853 goto out_ip6ip6;
1854 }
1855 err = rtnl_link_register(&ip6_link_ops);
1856 if (err < 0)
1857 goto rtnl_link_failed;
1858
1859 return 0;
1860
1861 rtnl_link_failed:
1862 xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
1863 out_ip6ip6:
1864 xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
1865 out_ip4ip6:
1866 unregister_pernet_device(&ip6_tnl_net_ops);
1867 out_pernet:
1868 return err;
1869 }
1870
1871 /**
1872 * ip6_tunnel_cleanup - free resources and unregister protocol
1873 **/
1874
ip6_tunnel_cleanup(void)1875 static void __exit ip6_tunnel_cleanup(void)
1876 {
1877 rtnl_link_unregister(&ip6_link_ops);
1878 if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
1879 pr_info("%s: can't deregister ip4ip6\n", __func__);
1880
1881 if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
1882 pr_info("%s: can't deregister ip6ip6\n", __func__);
1883
1884 unregister_pernet_device(&ip6_tnl_net_ops);
1885 }
1886
1887 module_init(ip6_tunnel_init);
1888 module_exit(ip6_tunnel_cleanup);
1889