1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * IPv6 tunneling device 4 * Linux INET6 implementation 5 * 6 * Authors: 7 * Ville Nuorvala <vnuorval@tcs.hut.fi> 8 * Yasuyuki Kozakai <kozakai@linux-ipv6.org> 9 * 10 * Based on: 11 * linux/net/ipv6/sit.c and linux/net/ipv4/ipip.c 12 * 13 * RFC 2473 14 */ 15 16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 17 18 #include <linux/module.h> 19 #include <linux/capability.h> 20 #include <linux/errno.h> 21 #include <linux/types.h> 22 #include <linux/sockios.h> 23 #include <linux/icmp.h> 24 #include <linux/if.h> 25 #include <linux/in.h> 26 #include <linux/ip.h> 27 #include <linux/net.h> 28 #include <linux/in6.h> 29 #include <linux/netdevice.h> 30 #include <linux/if_arp.h> 31 #include <linux/icmpv6.h> 32 #include <linux/init.h> 33 #include <linux/route.h> 34 #include <linux/rtnetlink.h> 35 #include <linux/netfilter_ipv6.h> 36 #include <linux/slab.h> 37 #include <linux/hash.h> 38 #include <linux/etherdevice.h> 39 40 #include <linux/uaccess.h> 41 #include <linux/atomic.h> 42 43 #include <net/icmp.h> 44 #include <net/ip.h> 45 #include <net/ip_tunnels.h> 46 #include <net/ipv6.h> 47 #include <net/ip6_route.h> 48 #include <net/addrconf.h> 49 #include <net/ip6_tunnel.h> 50 #include <net/xfrm.h> 51 #include <net/dsfield.h> 52 #include <net/inet_ecn.h> 53 #include <net/net_namespace.h> 54 #include <net/netns/generic.h> 55 #include <net/dst_metadata.h> 56 57 MODULE_AUTHOR("Ville Nuorvala"); 58 MODULE_DESCRIPTION("IPv6 tunneling device"); 59 MODULE_LICENSE("GPL"); 60 MODULE_ALIAS_RTNL_LINK("ip6tnl"); 61 MODULE_ALIAS_NETDEV("ip6tnl0"); 62 63 #define IP6_TUNNEL_HASH_SIZE_SHIFT 5 64 #define IP6_TUNNEL_HASH_SIZE (1 << IP6_TUNNEL_HASH_SIZE_SHIFT) 65 66 static bool log_ecn_error = true; 67 module_param(log_ecn_error, bool, 0644); 68 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN"); 69 70 static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2) 71 { 72 u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2); 73 74 return hash_32(hash, IP6_TUNNEL_HASH_SIZE_SHIFT); 75 } 76 77 static int ip6_tnl_dev_init(struct net_device *dev); 78 static void ip6_tnl_dev_setup(struct net_device *dev); 79 static struct rtnl_link_ops ip6_link_ops __read_mostly; 80 81 static unsigned int ip6_tnl_net_id __read_mostly; 82 struct ip6_tnl_net { 83 /* the IPv6 tunnel fallback device */ 84 struct net_device *fb_tnl_dev; 85 /* lists for storing tunnels in use */ 86 struct ip6_tnl __rcu *tnls_r_l[IP6_TUNNEL_HASH_SIZE]; 87 struct ip6_tnl __rcu *tnls_wc[1]; 88 struct ip6_tnl __rcu **tnls[2]; 89 struct ip6_tnl __rcu *collect_md_tun; 90 }; 91 92 static struct net_device_stats *ip6_get_stats(struct net_device *dev) 93 { 94 struct pcpu_sw_netstats tmp, sum = { 0 }; 95 int i; 96 97 for_each_possible_cpu(i) { 98 unsigned int start; 99 const struct pcpu_sw_netstats *tstats = 100 per_cpu_ptr(dev->tstats, i); 101 102 do { 103 start = u64_stats_fetch_begin_irq(&tstats->syncp); 104 tmp.rx_packets = tstats->rx_packets; 105 tmp.rx_bytes = tstats->rx_bytes; 106 tmp.tx_packets = tstats->tx_packets; 107 tmp.tx_bytes = tstats->tx_bytes; 108 } while (u64_stats_fetch_retry_irq(&tstats->syncp, start)); 109 110 sum.rx_packets += tmp.rx_packets; 111 sum.rx_bytes += tmp.rx_bytes; 112 sum.tx_packets += tmp.tx_packets; 113 sum.tx_bytes += tmp.tx_bytes; 114 } 115 dev->stats.rx_packets = sum.rx_packets; 116 dev->stats.rx_bytes = sum.rx_bytes; 117 dev->stats.tx_packets = sum.tx_packets; 118 dev->stats.tx_bytes = sum.tx_bytes; 119 return &dev->stats; 120 } 121 122 /** 123 * ip6_tnl_lookup - fetch tunnel matching the end-point addresses 124 * @link: ifindex of underlying interface 125 * @remote: the address of the tunnel exit-point 126 * @local: the address of the tunnel entry-point 127 * 128 * Return: 129 * tunnel matching given end-points if found, 130 * else fallback tunnel if its device is up, 131 * else %NULL 132 **/ 133 134 #define for_each_ip6_tunnel_rcu(start) \ 135 for (t = rcu_dereference(start); t; t = rcu_dereference(t->next)) 136 137 static struct ip6_tnl * 138 ip6_tnl_lookup(struct net *net, int link, 139 const struct in6_addr *remote, const struct in6_addr *local) 140 { 141 unsigned int hash = HASH(remote, local); 142 struct ip6_tnl *t, *cand = NULL; 143 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); 144 struct in6_addr any; 145 146 for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) { 147 if (!ipv6_addr_equal(local, &t->parms.laddr) || 148 !ipv6_addr_equal(remote, &t->parms.raddr) || 149 !(t->dev->flags & IFF_UP)) 150 continue; 151 152 if (link == t->parms.link) 153 return t; 154 else 155 cand = t; 156 } 157 158 memset(&any, 0, sizeof(any)); 159 hash = HASH(&any, local); 160 for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) { 161 if (!ipv6_addr_equal(local, &t->parms.laddr) || 162 !ipv6_addr_any(&t->parms.raddr) || 163 !(t->dev->flags & IFF_UP)) 164 continue; 165 166 if (link == t->parms.link) 167 return t; 168 else if (!cand) 169 cand = t; 170 } 171 172 hash = HASH(remote, &any); 173 for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) { 174 if (!ipv6_addr_equal(remote, &t->parms.raddr) || 175 !ipv6_addr_any(&t->parms.laddr) || 176 !(t->dev->flags & IFF_UP)) 177 continue; 178 179 if (link == t->parms.link) 180 return t; 181 else if (!cand) 182 cand = t; 183 } 184 185 if (cand) 186 return cand; 187 188 t = rcu_dereference(ip6n->collect_md_tun); 189 if (t && t->dev->flags & IFF_UP) 190 return t; 191 192 t = rcu_dereference(ip6n->tnls_wc[0]); 193 if (t && (t->dev->flags & IFF_UP)) 194 return t; 195 196 return NULL; 197 } 198 199 /** 200 * ip6_tnl_bucket - get head of list matching given tunnel parameters 201 * @p: parameters containing tunnel end-points 202 * 203 * Description: 204 * ip6_tnl_bucket() returns the head of the list matching the 205 * &struct in6_addr entries laddr and raddr in @p. 206 * 207 * Return: head of IPv6 tunnel list 208 **/ 209 210 static struct ip6_tnl __rcu ** 211 ip6_tnl_bucket(struct ip6_tnl_net *ip6n, const struct __ip6_tnl_parm *p) 212 { 213 const struct in6_addr *remote = &p->raddr; 214 const struct in6_addr *local = &p->laddr; 215 unsigned int h = 0; 216 int prio = 0; 217 218 if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) { 219 prio = 1; 220 h = HASH(remote, local); 221 } 222 return &ip6n->tnls[prio][h]; 223 } 224 225 /** 226 * ip6_tnl_link - add tunnel to hash table 227 * @t: tunnel to be added 228 **/ 229 230 static void 231 ip6_tnl_link(struct ip6_tnl_net *ip6n, struct ip6_tnl *t) 232 { 233 struct ip6_tnl __rcu **tp = ip6_tnl_bucket(ip6n, &t->parms); 234 235 if (t->parms.collect_md) 236 rcu_assign_pointer(ip6n->collect_md_tun, t); 237 rcu_assign_pointer(t->next , rtnl_dereference(*tp)); 238 rcu_assign_pointer(*tp, t); 239 } 240 241 /** 242 * ip6_tnl_unlink - remove tunnel from hash table 243 * @t: tunnel to be removed 244 **/ 245 246 static void 247 ip6_tnl_unlink(struct ip6_tnl_net *ip6n, struct ip6_tnl *t) 248 { 249 struct ip6_tnl __rcu **tp; 250 struct ip6_tnl *iter; 251 252 if (t->parms.collect_md) 253 rcu_assign_pointer(ip6n->collect_md_tun, NULL); 254 255 for (tp = ip6_tnl_bucket(ip6n, &t->parms); 256 (iter = rtnl_dereference(*tp)) != NULL; 257 tp = &iter->next) { 258 if (t == iter) { 259 rcu_assign_pointer(*tp, t->next); 260 break; 261 } 262 } 263 } 264 265 static void ip6_dev_free(struct net_device *dev) 266 { 267 struct ip6_tnl *t = netdev_priv(dev); 268 269 gro_cells_destroy(&t->gro_cells); 270 dst_cache_destroy(&t->dst_cache); 271 free_percpu(dev->tstats); 272 } 273 274 static int ip6_tnl_create2(struct net_device *dev) 275 { 276 struct ip6_tnl *t = netdev_priv(dev); 277 struct net *net = dev_net(dev); 278 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); 279 int err; 280 281 t = netdev_priv(dev); 282 283 dev->rtnl_link_ops = &ip6_link_ops; 284 err = register_netdevice(dev); 285 if (err < 0) 286 goto out; 287 288 strcpy(t->parms.name, dev->name); 289 290 dev_hold(dev); 291 ip6_tnl_link(ip6n, t); 292 return 0; 293 294 out: 295 return err; 296 } 297 298 /** 299 * ip6_tnl_create - create a new tunnel 300 * @p: tunnel parameters 301 * @pt: pointer to new tunnel 302 * 303 * Description: 304 * Create tunnel matching given parameters. 305 * 306 * Return: 307 * created tunnel or error pointer 308 **/ 309 310 static struct ip6_tnl *ip6_tnl_create(struct net *net, struct __ip6_tnl_parm *p) 311 { 312 struct net_device *dev; 313 struct ip6_tnl *t; 314 char name[IFNAMSIZ]; 315 int err = -E2BIG; 316 317 if (p->name[0]) { 318 if (!dev_valid_name(p->name)) 319 goto failed; 320 strlcpy(name, p->name, IFNAMSIZ); 321 } else { 322 sprintf(name, "ip6tnl%%d"); 323 } 324 err = -ENOMEM; 325 dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN, 326 ip6_tnl_dev_setup); 327 if (!dev) 328 goto failed; 329 330 dev_net_set(dev, net); 331 332 t = netdev_priv(dev); 333 t->parms = *p; 334 t->net = dev_net(dev); 335 err = ip6_tnl_create2(dev); 336 if (err < 0) 337 goto failed_free; 338 339 return t; 340 341 failed_free: 342 free_netdev(dev); 343 failed: 344 return ERR_PTR(err); 345 } 346 347 /** 348 * ip6_tnl_locate - find or create tunnel matching given parameters 349 * @p: tunnel parameters 350 * @create: != 0 if allowed to create new tunnel if no match found 351 * 352 * Description: 353 * ip6_tnl_locate() first tries to locate an existing tunnel 354 * based on @parms. If this is unsuccessful, but @create is set a new 355 * tunnel device is created and registered for use. 356 * 357 * Return: 358 * matching tunnel or error pointer 359 **/ 360 361 static struct ip6_tnl *ip6_tnl_locate(struct net *net, 362 struct __ip6_tnl_parm *p, int create) 363 { 364 const struct in6_addr *remote = &p->raddr; 365 const struct in6_addr *local = &p->laddr; 366 struct ip6_tnl __rcu **tp; 367 struct ip6_tnl *t; 368 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); 369 370 for (tp = ip6_tnl_bucket(ip6n, p); 371 (t = rtnl_dereference(*tp)) != NULL; 372 tp = &t->next) { 373 if (ipv6_addr_equal(local, &t->parms.laddr) && 374 ipv6_addr_equal(remote, &t->parms.raddr) && 375 p->link == t->parms.link) { 376 if (create) 377 return ERR_PTR(-EEXIST); 378 379 return t; 380 } 381 } 382 if (!create) 383 return ERR_PTR(-ENODEV); 384 return ip6_tnl_create(net, p); 385 } 386 387 /** 388 * ip6_tnl_dev_uninit - tunnel device uninitializer 389 * @dev: the device to be destroyed 390 * 391 * Description: 392 * ip6_tnl_dev_uninit() removes tunnel from its list 393 **/ 394 395 static void 396 ip6_tnl_dev_uninit(struct net_device *dev) 397 { 398 struct ip6_tnl *t = netdev_priv(dev); 399 struct net *net = t->net; 400 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); 401 402 if (dev == ip6n->fb_tnl_dev) 403 RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL); 404 else 405 ip6_tnl_unlink(ip6n, t); 406 dst_cache_reset(&t->dst_cache); 407 dev_put(dev); 408 } 409 410 /** 411 * parse_tvl_tnl_enc_lim - handle encapsulation limit option 412 * @skb: received socket buffer 413 * 414 * Return: 415 * 0 if none was found, 416 * else index to encapsulation limit 417 **/ 418 419 __u16 ip6_tnl_parse_tlv_enc_lim(struct sk_buff *skb, __u8 *raw) 420 { 421 const struct ipv6hdr *ipv6h = (const struct ipv6hdr *)raw; 422 unsigned int nhoff = raw - skb->data; 423 unsigned int off = nhoff + sizeof(*ipv6h); 424 u8 next, nexthdr = ipv6h->nexthdr; 425 426 while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) { 427 struct ipv6_opt_hdr *hdr; 428 u16 optlen; 429 430 if (!pskb_may_pull(skb, off + sizeof(*hdr))) 431 break; 432 433 hdr = (struct ipv6_opt_hdr *)(skb->data + off); 434 if (nexthdr == NEXTHDR_FRAGMENT) { 435 struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr; 436 if (frag_hdr->frag_off) 437 break; 438 optlen = 8; 439 } else if (nexthdr == NEXTHDR_AUTH) { 440 optlen = ipv6_authlen(hdr); 441 } else { 442 optlen = ipv6_optlen(hdr); 443 } 444 /* cache hdr->nexthdr, since pskb_may_pull() might 445 * invalidate hdr 446 */ 447 next = hdr->nexthdr; 448 if (nexthdr == NEXTHDR_DEST) { 449 u16 i = 2; 450 451 /* Remember : hdr is no longer valid at this point. */ 452 if (!pskb_may_pull(skb, off + optlen)) 453 break; 454 455 while (1) { 456 struct ipv6_tlv_tnl_enc_lim *tel; 457 458 /* No more room for encapsulation limit */ 459 if (i + sizeof(*tel) > optlen) 460 break; 461 462 tel = (struct ipv6_tlv_tnl_enc_lim *)(skb->data + off + i); 463 /* return index of option if found and valid */ 464 if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT && 465 tel->length == 1) 466 return i + off - nhoff; 467 /* else jump to next option */ 468 if (tel->type) 469 i += tel->length + 2; 470 else 471 i++; 472 } 473 } 474 nexthdr = next; 475 off += optlen; 476 } 477 return 0; 478 } 479 EXPORT_SYMBOL(ip6_tnl_parse_tlv_enc_lim); 480 481 /** 482 * ip6_tnl_err - tunnel error handler 483 * 484 * Description: 485 * ip6_tnl_err() should handle errors in the tunnel according 486 * to the specifications in RFC 2473. 487 **/ 488 489 static int 490 ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt, 491 u8 *type, u8 *code, int *msg, __u32 *info, int offset) 492 { 493 const struct ipv6hdr *ipv6h = (const struct ipv6hdr *)skb->data; 494 struct net *net = dev_net(skb->dev); 495 u8 rel_type = ICMPV6_DEST_UNREACH; 496 u8 rel_code = ICMPV6_ADDR_UNREACH; 497 __u32 rel_info = 0; 498 struct ip6_tnl *t; 499 int err = -ENOENT; 500 int rel_msg = 0; 501 u8 tproto; 502 __u16 len; 503 504 /* If the packet doesn't contain the original IPv6 header we are 505 in trouble since we might need the source address for further 506 processing of the error. */ 507 508 rcu_read_lock(); 509 t = ip6_tnl_lookup(dev_net(skb->dev), skb->dev->ifindex, &ipv6h->daddr, &ipv6h->saddr); 510 if (!t) 511 goto out; 512 513 tproto = READ_ONCE(t->parms.proto); 514 if (tproto != ipproto && tproto != 0) 515 goto out; 516 517 err = 0; 518 519 switch (*type) { 520 struct ipv6_tlv_tnl_enc_lim *tel; 521 __u32 mtu, teli; 522 case ICMPV6_DEST_UNREACH: 523 net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n", 524 t->parms.name); 525 rel_msg = 1; 526 break; 527 case ICMPV6_TIME_EXCEED: 528 if ((*code) == ICMPV6_EXC_HOPLIMIT) { 529 net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n", 530 t->parms.name); 531 rel_msg = 1; 532 } 533 break; 534 case ICMPV6_PARAMPROB: 535 teli = 0; 536 if ((*code) == ICMPV6_HDR_FIELD) 537 teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data); 538 539 if (teli && teli == *info - 2) { 540 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli]; 541 if (tel->encap_limit == 0) { 542 net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n", 543 t->parms.name); 544 rel_msg = 1; 545 } 546 } else { 547 net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n", 548 t->parms.name); 549 } 550 break; 551 case ICMPV6_PKT_TOOBIG: 552 ip6_update_pmtu(skb, net, htonl(*info), 0, 0, 553 sock_net_uid(net, NULL)); 554 mtu = *info - offset; 555 if (mtu < IPV6_MIN_MTU) 556 mtu = IPV6_MIN_MTU; 557 len = sizeof(*ipv6h) + ntohs(ipv6h->payload_len); 558 if (len > mtu) { 559 rel_type = ICMPV6_PKT_TOOBIG; 560 rel_code = 0; 561 rel_info = mtu; 562 rel_msg = 1; 563 } 564 break; 565 case NDISC_REDIRECT: 566 ip6_redirect(skb, net, skb->dev->ifindex, 0, 567 sock_net_uid(net, NULL)); 568 break; 569 } 570 571 *type = rel_type; 572 *code = rel_code; 573 *info = rel_info; 574 *msg = rel_msg; 575 576 out: 577 rcu_read_unlock(); 578 return err; 579 } 580 581 static int 582 ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, 583 u8 type, u8 code, int offset, __be32 info) 584 { 585 __u32 rel_info = ntohl(info); 586 const struct iphdr *eiph; 587 struct sk_buff *skb2; 588 int err, rel_msg = 0; 589 u8 rel_type = type; 590 u8 rel_code = code; 591 struct rtable *rt; 592 struct flowi4 fl4; 593 594 err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code, 595 &rel_msg, &rel_info, offset); 596 if (err < 0) 597 return err; 598 599 if (rel_msg == 0) 600 return 0; 601 602 switch (rel_type) { 603 case ICMPV6_DEST_UNREACH: 604 if (rel_code != ICMPV6_ADDR_UNREACH) 605 return 0; 606 rel_type = ICMP_DEST_UNREACH; 607 rel_code = ICMP_HOST_UNREACH; 608 break; 609 case ICMPV6_PKT_TOOBIG: 610 if (rel_code != 0) 611 return 0; 612 rel_type = ICMP_DEST_UNREACH; 613 rel_code = ICMP_FRAG_NEEDED; 614 break; 615 default: 616 return 0; 617 } 618 619 if (!pskb_may_pull(skb, offset + sizeof(struct iphdr))) 620 return 0; 621 622 skb2 = skb_clone(skb, GFP_ATOMIC); 623 if (!skb2) 624 return 0; 625 626 skb_dst_drop(skb2); 627 628 skb_pull(skb2, offset); 629 skb_reset_network_header(skb2); 630 eiph = ip_hdr(skb2); 631 632 /* Try to guess incoming interface */ 633 rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL, eiph->saddr, 634 0, 0, 0, IPPROTO_IPIP, RT_TOS(eiph->tos), 0); 635 if (IS_ERR(rt)) 636 goto out; 637 638 skb2->dev = rt->dst.dev; 639 ip_rt_put(rt); 640 641 /* route "incoming" packet */ 642 if (rt->rt_flags & RTCF_LOCAL) { 643 rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL, 644 eiph->daddr, eiph->saddr, 0, 0, 645 IPPROTO_IPIP, RT_TOS(eiph->tos), 0); 646 if (IS_ERR(rt) || rt->dst.dev->type != ARPHRD_TUNNEL6) { 647 if (!IS_ERR(rt)) 648 ip_rt_put(rt); 649 goto out; 650 } 651 skb_dst_set(skb2, &rt->dst); 652 } else { 653 if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos, 654 skb2->dev) || 655 skb_dst(skb2)->dev->type != ARPHRD_TUNNEL6) 656 goto out; 657 } 658 659 /* change mtu on this route */ 660 if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) { 661 if (rel_info > dst_mtu(skb_dst(skb2))) 662 goto out; 663 664 skb_dst_update_pmtu_no_confirm(skb2, rel_info); 665 } 666 667 icmp_send(skb2, rel_type, rel_code, htonl(rel_info)); 668 669 out: 670 kfree_skb(skb2); 671 return 0; 672 } 673 674 static int 675 ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, 676 u8 type, u8 code, int offset, __be32 info) 677 { 678 __u32 rel_info = ntohl(info); 679 int err, rel_msg = 0; 680 u8 rel_type = type; 681 u8 rel_code = code; 682 683 err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code, 684 &rel_msg, &rel_info, offset); 685 if (err < 0) 686 return err; 687 688 if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) { 689 struct rt6_info *rt; 690 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); 691 692 if (!skb2) 693 return 0; 694 695 skb_dst_drop(skb2); 696 skb_pull(skb2, offset); 697 skb_reset_network_header(skb2); 698 699 /* Try to guess incoming interface */ 700 rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr, 701 NULL, 0, skb2, 0); 702 703 if (rt && rt->dst.dev) 704 skb2->dev = rt->dst.dev; 705 706 icmpv6_send(skb2, rel_type, rel_code, rel_info); 707 708 ip6_rt_put(rt); 709 710 kfree_skb(skb2); 711 } 712 713 return 0; 714 } 715 716 static int ip4ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t, 717 const struct ipv6hdr *ipv6h, 718 struct sk_buff *skb) 719 { 720 __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK; 721 722 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY) 723 ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield); 724 725 return IP6_ECN_decapsulate(ipv6h, skb); 726 } 727 728 static int ip6ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t, 729 const struct ipv6hdr *ipv6h, 730 struct sk_buff *skb) 731 { 732 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY) 733 ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb)); 734 735 return IP6_ECN_decapsulate(ipv6h, skb); 736 } 737 738 __u32 ip6_tnl_get_cap(struct ip6_tnl *t, 739 const struct in6_addr *laddr, 740 const struct in6_addr *raddr) 741 { 742 struct __ip6_tnl_parm *p = &t->parms; 743 int ltype = ipv6_addr_type(laddr); 744 int rtype = ipv6_addr_type(raddr); 745 __u32 flags = 0; 746 747 if (ltype == IPV6_ADDR_ANY || rtype == IPV6_ADDR_ANY) { 748 flags = IP6_TNL_F_CAP_PER_PACKET; 749 } else if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) && 750 rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) && 751 !((ltype|rtype) & IPV6_ADDR_LOOPBACK) && 752 (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) { 753 if (ltype&IPV6_ADDR_UNICAST) 754 flags |= IP6_TNL_F_CAP_XMIT; 755 if (rtype&IPV6_ADDR_UNICAST) 756 flags |= IP6_TNL_F_CAP_RCV; 757 } 758 return flags; 759 } 760 EXPORT_SYMBOL(ip6_tnl_get_cap); 761 762 /* called with rcu_read_lock() */ 763 int ip6_tnl_rcv_ctl(struct ip6_tnl *t, 764 const struct in6_addr *laddr, 765 const struct in6_addr *raddr) 766 { 767 struct __ip6_tnl_parm *p = &t->parms; 768 int ret = 0; 769 struct net *net = t->net; 770 771 if ((p->flags & IP6_TNL_F_CAP_RCV) || 772 ((p->flags & IP6_TNL_F_CAP_PER_PACKET) && 773 (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_RCV))) { 774 struct net_device *ldev = NULL; 775 776 if (p->link) 777 ldev = dev_get_by_index_rcu(net, p->link); 778 779 if ((ipv6_addr_is_multicast(laddr) || 780 likely(ipv6_chk_addr_and_flags(net, laddr, ldev, false, 781 0, IFA_F_TENTATIVE))) && 782 ((p->flags & IP6_TNL_F_ALLOW_LOCAL_REMOTE) || 783 likely(!ipv6_chk_addr_and_flags(net, raddr, ldev, true, 784 0, IFA_F_TENTATIVE)))) 785 ret = 1; 786 } 787 return ret; 788 } 789 EXPORT_SYMBOL_GPL(ip6_tnl_rcv_ctl); 790 791 static int __ip6_tnl_rcv(struct ip6_tnl *tunnel, struct sk_buff *skb, 792 const struct tnl_ptk_info *tpi, 793 struct metadata_dst *tun_dst, 794 int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t, 795 const struct ipv6hdr *ipv6h, 796 struct sk_buff *skb), 797 bool log_ecn_err) 798 { 799 struct pcpu_sw_netstats *tstats; 800 const struct ipv6hdr *ipv6h = ipv6_hdr(skb); 801 int err; 802 803 if ((!(tpi->flags & TUNNEL_CSUM) && 804 (tunnel->parms.i_flags & TUNNEL_CSUM)) || 805 ((tpi->flags & TUNNEL_CSUM) && 806 !(tunnel->parms.i_flags & TUNNEL_CSUM))) { 807 tunnel->dev->stats.rx_crc_errors++; 808 tunnel->dev->stats.rx_errors++; 809 goto drop; 810 } 811 812 if (tunnel->parms.i_flags & TUNNEL_SEQ) { 813 if (!(tpi->flags & TUNNEL_SEQ) || 814 (tunnel->i_seqno && 815 (s32)(ntohl(tpi->seq) - tunnel->i_seqno) < 0)) { 816 tunnel->dev->stats.rx_fifo_errors++; 817 tunnel->dev->stats.rx_errors++; 818 goto drop; 819 } 820 tunnel->i_seqno = ntohl(tpi->seq) + 1; 821 } 822 823 skb->protocol = tpi->proto; 824 825 /* Warning: All skb pointers will be invalidated! */ 826 if (tunnel->dev->type == ARPHRD_ETHER) { 827 if (!pskb_may_pull(skb, ETH_HLEN)) { 828 tunnel->dev->stats.rx_length_errors++; 829 tunnel->dev->stats.rx_errors++; 830 goto drop; 831 } 832 833 ipv6h = ipv6_hdr(skb); 834 skb->protocol = eth_type_trans(skb, tunnel->dev); 835 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN); 836 } else { 837 skb->dev = tunnel->dev; 838 } 839 840 skb_reset_network_header(skb); 841 memset(skb->cb, 0, sizeof(struct inet6_skb_parm)); 842 843 __skb_tunnel_rx(skb, tunnel->dev, tunnel->net); 844 845 err = dscp_ecn_decapsulate(tunnel, ipv6h, skb); 846 if (unlikely(err)) { 847 if (log_ecn_err) 848 net_info_ratelimited("non-ECT from %pI6 with DS=%#x\n", 849 &ipv6h->saddr, 850 ipv6_get_dsfield(ipv6h)); 851 if (err > 1) { 852 ++tunnel->dev->stats.rx_frame_errors; 853 ++tunnel->dev->stats.rx_errors; 854 goto drop; 855 } 856 } 857 858 tstats = this_cpu_ptr(tunnel->dev->tstats); 859 u64_stats_update_begin(&tstats->syncp); 860 tstats->rx_packets++; 861 tstats->rx_bytes += skb->len; 862 u64_stats_update_end(&tstats->syncp); 863 864 skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(tunnel->dev))); 865 866 if (tun_dst) 867 skb_dst_set(skb, (struct dst_entry *)tun_dst); 868 869 gro_cells_receive(&tunnel->gro_cells, skb); 870 return 0; 871 872 drop: 873 if (tun_dst) 874 dst_release((struct dst_entry *)tun_dst); 875 kfree_skb(skb); 876 return 0; 877 } 878 879 int ip6_tnl_rcv(struct ip6_tnl *t, struct sk_buff *skb, 880 const struct tnl_ptk_info *tpi, 881 struct metadata_dst *tun_dst, 882 bool log_ecn_err) 883 { 884 return __ip6_tnl_rcv(t, skb, tpi, tun_dst, ip6ip6_dscp_ecn_decapsulate, 885 log_ecn_err); 886 } 887 EXPORT_SYMBOL(ip6_tnl_rcv); 888 889 static const struct tnl_ptk_info tpi_v6 = { 890 /* no tunnel info required for ipxip6. */ 891 .proto = htons(ETH_P_IPV6), 892 }; 893 894 static const struct tnl_ptk_info tpi_v4 = { 895 /* no tunnel info required for ipxip6. */ 896 .proto = htons(ETH_P_IP), 897 }; 898 899 static int ipxip6_rcv(struct sk_buff *skb, u8 ipproto, 900 const struct tnl_ptk_info *tpi, 901 int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t, 902 const struct ipv6hdr *ipv6h, 903 struct sk_buff *skb)) 904 { 905 struct ip6_tnl *t; 906 const struct ipv6hdr *ipv6h = ipv6_hdr(skb); 907 struct metadata_dst *tun_dst = NULL; 908 int ret = -1; 909 910 rcu_read_lock(); 911 t = ip6_tnl_lookup(dev_net(skb->dev), skb->dev->ifindex, &ipv6h->saddr, &ipv6h->daddr); 912 913 if (t) { 914 u8 tproto = READ_ONCE(t->parms.proto); 915 916 if (tproto != ipproto && tproto != 0) 917 goto drop; 918 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) 919 goto drop; 920 ipv6h = ipv6_hdr(skb); 921 if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) 922 goto drop; 923 if (iptunnel_pull_header(skb, 0, tpi->proto, false)) 924 goto drop; 925 if (t->parms.collect_md) { 926 tun_dst = ipv6_tun_rx_dst(skb, 0, 0, 0); 927 if (!tun_dst) 928 goto drop; 929 } 930 ret = __ip6_tnl_rcv(t, skb, tpi, tun_dst, dscp_ecn_decapsulate, 931 log_ecn_error); 932 } 933 934 rcu_read_unlock(); 935 936 return ret; 937 938 drop: 939 rcu_read_unlock(); 940 kfree_skb(skb); 941 return 0; 942 } 943 944 static int ip4ip6_rcv(struct sk_buff *skb) 945 { 946 return ipxip6_rcv(skb, IPPROTO_IPIP, &tpi_v4, 947 ip4ip6_dscp_ecn_decapsulate); 948 } 949 950 static int ip6ip6_rcv(struct sk_buff *skb) 951 { 952 return ipxip6_rcv(skb, IPPROTO_IPV6, &tpi_v6, 953 ip6ip6_dscp_ecn_decapsulate); 954 } 955 956 struct ipv6_tel_txoption { 957 struct ipv6_txoptions ops; 958 __u8 dst_opt[8]; 959 }; 960 961 static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit) 962 { 963 memset(opt, 0, sizeof(struct ipv6_tel_txoption)); 964 965 opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT; 966 opt->dst_opt[3] = 1; 967 opt->dst_opt[4] = encap_limit; 968 opt->dst_opt[5] = IPV6_TLV_PADN; 969 opt->dst_opt[6] = 1; 970 971 opt->ops.dst1opt = (struct ipv6_opt_hdr *) opt->dst_opt; 972 opt->ops.opt_nflen = 8; 973 } 974 975 /** 976 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own 977 * @t: the outgoing tunnel device 978 * @hdr: IPv6 header from the incoming packet 979 * 980 * Description: 981 * Avoid trivial tunneling loop by checking that tunnel exit-point 982 * doesn't match source of incoming packet. 983 * 984 * Return: 985 * 1 if conflict, 986 * 0 else 987 **/ 988 989 static inline bool 990 ip6_tnl_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr) 991 { 992 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr); 993 } 994 995 int ip6_tnl_xmit_ctl(struct ip6_tnl *t, 996 const struct in6_addr *laddr, 997 const struct in6_addr *raddr) 998 { 999 struct __ip6_tnl_parm *p = &t->parms; 1000 int ret = 0; 1001 struct net *net = t->net; 1002 1003 if (t->parms.collect_md) 1004 return 1; 1005 1006 if ((p->flags & IP6_TNL_F_CAP_XMIT) || 1007 ((p->flags & IP6_TNL_F_CAP_PER_PACKET) && 1008 (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_XMIT))) { 1009 struct net_device *ldev = NULL; 1010 1011 rcu_read_lock(); 1012 if (p->link) 1013 ldev = dev_get_by_index_rcu(net, p->link); 1014 1015 if (unlikely(!ipv6_chk_addr_and_flags(net, laddr, ldev, false, 1016 0, IFA_F_TENTATIVE))) 1017 pr_warn("%s xmit: Local address not yet configured!\n", 1018 p->name); 1019 else if (!(p->flags & IP6_TNL_F_ALLOW_LOCAL_REMOTE) && 1020 !ipv6_addr_is_multicast(raddr) && 1021 unlikely(ipv6_chk_addr_and_flags(net, raddr, ldev, 1022 true, 0, IFA_F_TENTATIVE))) 1023 pr_warn("%s xmit: Routing loop! Remote address found on this node!\n", 1024 p->name); 1025 else 1026 ret = 1; 1027 rcu_read_unlock(); 1028 } 1029 return ret; 1030 } 1031 EXPORT_SYMBOL_GPL(ip6_tnl_xmit_ctl); 1032 1033 /** 1034 * ip6_tnl_xmit - encapsulate packet and send 1035 * @skb: the outgoing socket buffer 1036 * @dev: the outgoing tunnel device 1037 * @dsfield: dscp code for outer header 1038 * @fl6: flow of tunneled packet 1039 * @encap_limit: encapsulation limit 1040 * @pmtu: Path MTU is stored if packet is too big 1041 * @proto: next header value 1042 * 1043 * Description: 1044 * Build new header and do some sanity checks on the packet before sending 1045 * it. 1046 * 1047 * Return: 1048 * 0 on success 1049 * -1 fail 1050 * %-EMSGSIZE message too big. return mtu in this case. 1051 **/ 1052 1053 int ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev, __u8 dsfield, 1054 struct flowi6 *fl6, int encap_limit, __u32 *pmtu, 1055 __u8 proto) 1056 { 1057 struct ip6_tnl *t = netdev_priv(dev); 1058 struct net *net = t->net; 1059 struct net_device_stats *stats = &t->dev->stats; 1060 struct ipv6hdr *ipv6h; 1061 struct ipv6_tel_txoption opt; 1062 struct dst_entry *dst = NULL, *ndst = NULL; 1063 struct net_device *tdev; 1064 int mtu; 1065 unsigned int eth_hlen = t->dev->type == ARPHRD_ETHER ? ETH_HLEN : 0; 1066 unsigned int psh_hlen = sizeof(struct ipv6hdr) + t->encap_hlen; 1067 unsigned int max_headroom = psh_hlen; 1068 bool use_cache = false; 1069 u8 hop_limit; 1070 int err = -1; 1071 1072 if (t->parms.collect_md) { 1073 hop_limit = skb_tunnel_info(skb)->key.ttl; 1074 goto route_lookup; 1075 } else { 1076 hop_limit = t->parms.hop_limit; 1077 } 1078 1079 /* NBMA tunnel */ 1080 if (ipv6_addr_any(&t->parms.raddr)) { 1081 if (skb->protocol == htons(ETH_P_IPV6)) { 1082 struct in6_addr *addr6; 1083 struct neighbour *neigh; 1084 int addr_type; 1085 1086 if (!skb_dst(skb)) 1087 goto tx_err_link_failure; 1088 1089 neigh = dst_neigh_lookup(skb_dst(skb), 1090 &ipv6_hdr(skb)->daddr); 1091 if (!neigh) 1092 goto tx_err_link_failure; 1093 1094 addr6 = (struct in6_addr *)&neigh->primary_key; 1095 addr_type = ipv6_addr_type(addr6); 1096 1097 if (addr_type == IPV6_ADDR_ANY) 1098 addr6 = &ipv6_hdr(skb)->daddr; 1099 1100 memcpy(&fl6->daddr, addr6, sizeof(fl6->daddr)); 1101 neigh_release(neigh); 1102 } 1103 } else if (t->parms.proto != 0 && !(t->parms.flags & 1104 (IP6_TNL_F_USE_ORIG_TCLASS | 1105 IP6_TNL_F_USE_ORIG_FWMARK))) { 1106 /* enable the cache only if neither the outer protocol nor the 1107 * routing decision depends on the current inner header value 1108 */ 1109 use_cache = true; 1110 } 1111 1112 if (use_cache) 1113 dst = dst_cache_get(&t->dst_cache); 1114 1115 if (!ip6_tnl_xmit_ctl(t, &fl6->saddr, &fl6->daddr)) 1116 goto tx_err_link_failure; 1117 1118 if (!dst) { 1119 route_lookup: 1120 /* add dsfield to flowlabel for route lookup */ 1121 fl6->flowlabel = ip6_make_flowinfo(dsfield, fl6->flowlabel); 1122 1123 dst = ip6_route_output(net, NULL, fl6); 1124 1125 if (dst->error) 1126 goto tx_err_link_failure; 1127 dst = xfrm_lookup(net, dst, flowi6_to_flowi(fl6), NULL, 0); 1128 if (IS_ERR(dst)) { 1129 err = PTR_ERR(dst); 1130 dst = NULL; 1131 goto tx_err_link_failure; 1132 } 1133 if (t->parms.collect_md && ipv6_addr_any(&fl6->saddr) && 1134 ipv6_dev_get_saddr(net, ip6_dst_idev(dst)->dev, 1135 &fl6->daddr, 0, &fl6->saddr)) 1136 goto tx_err_link_failure; 1137 ndst = dst; 1138 } 1139 1140 tdev = dst->dev; 1141 1142 if (tdev == dev) { 1143 stats->collisions++; 1144 net_warn_ratelimited("%s: Local routing loop detected!\n", 1145 t->parms.name); 1146 goto tx_err_dst_release; 1147 } 1148 mtu = dst_mtu(dst) - eth_hlen - psh_hlen - t->tun_hlen; 1149 if (encap_limit >= 0) { 1150 max_headroom += 8; 1151 mtu -= 8; 1152 } 1153 mtu = max(mtu, skb->protocol == htons(ETH_P_IPV6) ? 1154 IPV6_MIN_MTU : IPV4_MIN_MTU); 1155 1156 skb_dst_update_pmtu_no_confirm(skb, mtu); 1157 if (skb->len - t->tun_hlen - eth_hlen > mtu && !skb_is_gso(skb)) { 1158 *pmtu = mtu; 1159 err = -EMSGSIZE; 1160 goto tx_err_dst_release; 1161 } 1162 1163 if (t->err_count > 0) { 1164 if (time_before(jiffies, 1165 t->err_time + IP6TUNNEL_ERR_TIMEO)) { 1166 t->err_count--; 1167 1168 dst_link_failure(skb); 1169 } else { 1170 t->err_count = 0; 1171 } 1172 } 1173 1174 skb_scrub_packet(skb, !net_eq(t->net, dev_net(dev))); 1175 1176 /* 1177 * Okay, now see if we can stuff it in the buffer as-is. 1178 */ 1179 max_headroom += LL_RESERVED_SPACE(tdev); 1180 1181 if (skb_headroom(skb) < max_headroom || skb_shared(skb) || 1182 (skb_cloned(skb) && !skb_clone_writable(skb, 0))) { 1183 struct sk_buff *new_skb; 1184 1185 new_skb = skb_realloc_headroom(skb, max_headroom); 1186 if (!new_skb) 1187 goto tx_err_dst_release; 1188 1189 if (skb->sk) 1190 skb_set_owner_w(new_skb, skb->sk); 1191 consume_skb(skb); 1192 skb = new_skb; 1193 } 1194 1195 if (t->parms.collect_md) { 1196 if (t->encap.type != TUNNEL_ENCAP_NONE) 1197 goto tx_err_dst_release; 1198 } else { 1199 if (use_cache && ndst) 1200 dst_cache_set_ip6(&t->dst_cache, ndst, &fl6->saddr); 1201 } 1202 skb_dst_set(skb, dst); 1203 1204 if (hop_limit == 0) { 1205 if (skb->protocol == htons(ETH_P_IP)) 1206 hop_limit = ip_hdr(skb)->ttl; 1207 else if (skb->protocol == htons(ETH_P_IPV6)) 1208 hop_limit = ipv6_hdr(skb)->hop_limit; 1209 else 1210 hop_limit = ip6_dst_hoplimit(dst); 1211 } 1212 1213 /* Calculate max headroom for all the headers and adjust 1214 * needed_headroom if necessary. 1215 */ 1216 max_headroom = LL_RESERVED_SPACE(dst->dev) + sizeof(struct ipv6hdr) 1217 + dst->header_len + t->hlen; 1218 if (max_headroom > dev->needed_headroom) 1219 dev->needed_headroom = max_headroom; 1220 1221 err = ip6_tnl_encap(skb, t, &proto, fl6); 1222 if (err) 1223 return err; 1224 1225 if (encap_limit >= 0) { 1226 init_tel_txopt(&opt, encap_limit); 1227 ipv6_push_frag_opts(skb, &opt.ops, &proto); 1228 } 1229 1230 skb_push(skb, sizeof(struct ipv6hdr)); 1231 skb_reset_network_header(skb); 1232 ipv6h = ipv6_hdr(skb); 1233 ip6_flow_hdr(ipv6h, dsfield, 1234 ip6_make_flowlabel(net, skb, fl6->flowlabel, true, fl6)); 1235 ipv6h->hop_limit = hop_limit; 1236 ipv6h->nexthdr = proto; 1237 ipv6h->saddr = fl6->saddr; 1238 ipv6h->daddr = fl6->daddr; 1239 ip6tunnel_xmit(NULL, skb, dev); 1240 return 0; 1241 tx_err_link_failure: 1242 stats->tx_carrier_errors++; 1243 dst_link_failure(skb); 1244 tx_err_dst_release: 1245 dst_release(dst); 1246 return err; 1247 } 1248 EXPORT_SYMBOL(ip6_tnl_xmit); 1249 1250 static inline int 1251 ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev) 1252 { 1253 struct ip6_tnl *t = netdev_priv(dev); 1254 const struct iphdr *iph; 1255 int encap_limit = -1; 1256 struct flowi6 fl6; 1257 __u8 dsfield; 1258 __u32 mtu; 1259 u8 tproto; 1260 int err; 1261 1262 iph = ip_hdr(skb); 1263 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); 1264 1265 tproto = READ_ONCE(t->parms.proto); 1266 if (tproto != IPPROTO_IPIP && tproto != 0) 1267 return -1; 1268 1269 if (t->parms.collect_md) { 1270 struct ip_tunnel_info *tun_info; 1271 const struct ip_tunnel_key *key; 1272 1273 tun_info = skb_tunnel_info(skb); 1274 if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) || 1275 ip_tunnel_info_af(tun_info) != AF_INET6)) 1276 return -1; 1277 key = &tun_info->key; 1278 memset(&fl6, 0, sizeof(fl6)); 1279 fl6.flowi6_proto = IPPROTO_IPIP; 1280 fl6.saddr = key->u.ipv6.src; 1281 fl6.daddr = key->u.ipv6.dst; 1282 fl6.flowlabel = key->label; 1283 dsfield = key->tos; 1284 } else { 1285 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) 1286 encap_limit = t->parms.encap_limit; 1287 1288 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6)); 1289 fl6.flowi6_proto = IPPROTO_IPIP; 1290 1291 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS) 1292 dsfield = ipv4_get_dsfield(iph); 1293 else 1294 dsfield = ip6_tclass(t->parms.flowinfo); 1295 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK) 1296 fl6.flowi6_mark = skb->mark; 1297 else 1298 fl6.flowi6_mark = t->parms.fwmark; 1299 } 1300 1301 fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL); 1302 dsfield = INET_ECN_encapsulate(dsfield, ipv4_get_dsfield(iph)); 1303 1304 if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP6)) 1305 return -1; 1306 1307 skb_set_inner_ipproto(skb, IPPROTO_IPIP); 1308 1309 err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu, 1310 IPPROTO_IPIP); 1311 if (err != 0) { 1312 /* XXX: send ICMP error even if DF is not set. */ 1313 if (err == -EMSGSIZE) 1314 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, 1315 htonl(mtu)); 1316 return -1; 1317 } 1318 1319 return 0; 1320 } 1321 1322 static inline int 1323 ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev) 1324 { 1325 struct ip6_tnl *t = netdev_priv(dev); 1326 struct ipv6hdr *ipv6h; 1327 int encap_limit = -1; 1328 __u16 offset; 1329 struct flowi6 fl6; 1330 __u8 dsfield; 1331 __u32 mtu; 1332 u8 tproto; 1333 int err; 1334 1335 ipv6h = ipv6_hdr(skb); 1336 tproto = READ_ONCE(t->parms.proto); 1337 if ((tproto != IPPROTO_IPV6 && tproto != 0) || 1338 ip6_tnl_addr_conflict(t, ipv6h)) 1339 return -1; 1340 1341 if (t->parms.collect_md) { 1342 struct ip_tunnel_info *tun_info; 1343 const struct ip_tunnel_key *key; 1344 1345 tun_info = skb_tunnel_info(skb); 1346 if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) || 1347 ip_tunnel_info_af(tun_info) != AF_INET6)) 1348 return -1; 1349 key = &tun_info->key; 1350 memset(&fl6, 0, sizeof(fl6)); 1351 fl6.flowi6_proto = IPPROTO_IPV6; 1352 fl6.saddr = key->u.ipv6.src; 1353 fl6.daddr = key->u.ipv6.dst; 1354 fl6.flowlabel = key->label; 1355 dsfield = key->tos; 1356 } else { 1357 offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb)); 1358 /* ip6_tnl_parse_tlv_enc_lim() might have reallocated skb->head */ 1359 ipv6h = ipv6_hdr(skb); 1360 if (offset > 0) { 1361 struct ipv6_tlv_tnl_enc_lim *tel; 1362 1363 tel = (void *)&skb_network_header(skb)[offset]; 1364 if (tel->encap_limit == 0) { 1365 icmpv6_send(skb, ICMPV6_PARAMPROB, 1366 ICMPV6_HDR_FIELD, offset + 2); 1367 return -1; 1368 } 1369 encap_limit = tel->encap_limit - 1; 1370 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) { 1371 encap_limit = t->parms.encap_limit; 1372 } 1373 1374 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6)); 1375 fl6.flowi6_proto = IPPROTO_IPV6; 1376 1377 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS) 1378 dsfield = ipv6_get_dsfield(ipv6h); 1379 else 1380 dsfield = ip6_tclass(t->parms.flowinfo); 1381 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL) 1382 fl6.flowlabel |= ip6_flowlabel(ipv6h); 1383 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK) 1384 fl6.flowi6_mark = skb->mark; 1385 else 1386 fl6.flowi6_mark = t->parms.fwmark; 1387 } 1388 1389 fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL); 1390 dsfield = INET_ECN_encapsulate(dsfield, ipv6_get_dsfield(ipv6h)); 1391 1392 if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP6)) 1393 return -1; 1394 1395 skb_set_inner_ipproto(skb, IPPROTO_IPV6); 1396 1397 err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu, 1398 IPPROTO_IPV6); 1399 if (err != 0) { 1400 if (err == -EMSGSIZE) 1401 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); 1402 return -1; 1403 } 1404 1405 return 0; 1406 } 1407 1408 static netdev_tx_t 1409 ip6_tnl_start_xmit(struct sk_buff *skb, struct net_device *dev) 1410 { 1411 struct ip6_tnl *t = netdev_priv(dev); 1412 struct net_device_stats *stats = &t->dev->stats; 1413 int ret; 1414 1415 if (!pskb_inet_may_pull(skb)) 1416 goto tx_err; 1417 1418 switch (skb->protocol) { 1419 case htons(ETH_P_IP): 1420 ret = ip4ip6_tnl_xmit(skb, dev); 1421 break; 1422 case htons(ETH_P_IPV6): 1423 ret = ip6ip6_tnl_xmit(skb, dev); 1424 break; 1425 default: 1426 goto tx_err; 1427 } 1428 1429 if (ret < 0) 1430 goto tx_err; 1431 1432 return NETDEV_TX_OK; 1433 1434 tx_err: 1435 stats->tx_errors++; 1436 stats->tx_dropped++; 1437 kfree_skb(skb); 1438 return NETDEV_TX_OK; 1439 } 1440 1441 static void ip6_tnl_link_config(struct ip6_tnl *t) 1442 { 1443 struct net_device *dev = t->dev; 1444 struct net_device *tdev = NULL; 1445 struct __ip6_tnl_parm *p = &t->parms; 1446 struct flowi6 *fl6 = &t->fl.u.ip6; 1447 unsigned int mtu; 1448 int t_hlen; 1449 1450 memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr)); 1451 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr)); 1452 1453 /* Set up flowi template */ 1454 fl6->saddr = p->laddr; 1455 fl6->daddr = p->raddr; 1456 fl6->flowi6_oif = p->link; 1457 fl6->flowlabel = 0; 1458 1459 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS)) 1460 fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo; 1461 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL)) 1462 fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo; 1463 1464 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET); 1465 p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr); 1466 1467 if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV) 1468 dev->flags |= IFF_POINTOPOINT; 1469 else 1470 dev->flags &= ~IFF_POINTOPOINT; 1471 1472 t->tun_hlen = 0; 1473 t->hlen = t->encap_hlen + t->tun_hlen; 1474 t_hlen = t->hlen + sizeof(struct ipv6hdr); 1475 1476 if (p->flags & IP6_TNL_F_CAP_XMIT) { 1477 int strict = (ipv6_addr_type(&p->raddr) & 1478 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)); 1479 1480 struct rt6_info *rt = rt6_lookup(t->net, 1481 &p->raddr, &p->laddr, 1482 p->link, NULL, strict); 1483 if (rt) { 1484 tdev = rt->dst.dev; 1485 ip6_rt_put(rt); 1486 } 1487 1488 if (!tdev && p->link) 1489 tdev = __dev_get_by_index(t->net, p->link); 1490 1491 if (tdev) { 1492 dev->hard_header_len = tdev->hard_header_len + t_hlen; 1493 mtu = min_t(unsigned int, tdev->mtu, IP6_MAX_MTU); 1494 1495 dev->mtu = mtu - t_hlen; 1496 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) 1497 dev->mtu -= 8; 1498 1499 if (dev->mtu < IPV6_MIN_MTU) 1500 dev->mtu = IPV6_MIN_MTU; 1501 } 1502 } 1503 } 1504 1505 /** 1506 * ip6_tnl_change - update the tunnel parameters 1507 * @t: tunnel to be changed 1508 * @p: tunnel configuration parameters 1509 * 1510 * Description: 1511 * ip6_tnl_change() updates the tunnel parameters 1512 **/ 1513 1514 static int 1515 ip6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p) 1516 { 1517 t->parms.laddr = p->laddr; 1518 t->parms.raddr = p->raddr; 1519 t->parms.flags = p->flags; 1520 t->parms.hop_limit = p->hop_limit; 1521 t->parms.encap_limit = p->encap_limit; 1522 t->parms.flowinfo = p->flowinfo; 1523 t->parms.link = p->link; 1524 t->parms.proto = p->proto; 1525 t->parms.fwmark = p->fwmark; 1526 dst_cache_reset(&t->dst_cache); 1527 ip6_tnl_link_config(t); 1528 return 0; 1529 } 1530 1531 static int ip6_tnl_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p) 1532 { 1533 struct net *net = t->net; 1534 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); 1535 int err; 1536 1537 ip6_tnl_unlink(ip6n, t); 1538 synchronize_net(); 1539 err = ip6_tnl_change(t, p); 1540 ip6_tnl_link(ip6n, t); 1541 netdev_state_change(t->dev); 1542 return err; 1543 } 1544 1545 static int ip6_tnl0_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p) 1546 { 1547 /* for default tnl0 device allow to change only the proto */ 1548 t->parms.proto = p->proto; 1549 netdev_state_change(t->dev); 1550 return 0; 1551 } 1552 1553 static void 1554 ip6_tnl_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm *u) 1555 { 1556 p->laddr = u->laddr; 1557 p->raddr = u->raddr; 1558 p->flags = u->flags; 1559 p->hop_limit = u->hop_limit; 1560 p->encap_limit = u->encap_limit; 1561 p->flowinfo = u->flowinfo; 1562 p->link = u->link; 1563 p->proto = u->proto; 1564 memcpy(p->name, u->name, sizeof(u->name)); 1565 } 1566 1567 static void 1568 ip6_tnl_parm_to_user(struct ip6_tnl_parm *u, const struct __ip6_tnl_parm *p) 1569 { 1570 u->laddr = p->laddr; 1571 u->raddr = p->raddr; 1572 u->flags = p->flags; 1573 u->hop_limit = p->hop_limit; 1574 u->encap_limit = p->encap_limit; 1575 u->flowinfo = p->flowinfo; 1576 u->link = p->link; 1577 u->proto = p->proto; 1578 memcpy(u->name, p->name, sizeof(u->name)); 1579 } 1580 1581 /** 1582 * ip6_tnl_ioctl - configure ipv6 tunnels from userspace 1583 * @dev: virtual device associated with tunnel 1584 * @ifr: parameters passed from userspace 1585 * @cmd: command to be performed 1586 * 1587 * Description: 1588 * ip6_tnl_ioctl() is used for managing IPv6 tunnels 1589 * from userspace. 1590 * 1591 * The possible commands are the following: 1592 * %SIOCGETTUNNEL: get tunnel parameters for device 1593 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters 1594 * %SIOCCHGTUNNEL: change tunnel parameters to those given 1595 * %SIOCDELTUNNEL: delete tunnel 1596 * 1597 * The fallback device "ip6tnl0", created during module 1598 * initialization, can be used for creating other tunnel devices. 1599 * 1600 * Return: 1601 * 0 on success, 1602 * %-EFAULT if unable to copy data to or from userspace, 1603 * %-EPERM if current process hasn't %CAP_NET_ADMIN set 1604 * %-EINVAL if passed tunnel parameters are invalid, 1605 * %-EEXIST if changing a tunnel's parameters would cause a conflict 1606 * %-ENODEV if attempting to change or delete a nonexisting device 1607 **/ 1608 1609 static int 1610 ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 1611 { 1612 int err = 0; 1613 struct ip6_tnl_parm p; 1614 struct __ip6_tnl_parm p1; 1615 struct ip6_tnl *t = netdev_priv(dev); 1616 struct net *net = t->net; 1617 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); 1618 1619 memset(&p1, 0, sizeof(p1)); 1620 1621 switch (cmd) { 1622 case SIOCGETTUNNEL: 1623 if (dev == ip6n->fb_tnl_dev) { 1624 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) { 1625 err = -EFAULT; 1626 break; 1627 } 1628 ip6_tnl_parm_from_user(&p1, &p); 1629 t = ip6_tnl_locate(net, &p1, 0); 1630 if (IS_ERR(t)) 1631 t = netdev_priv(dev); 1632 } else { 1633 memset(&p, 0, sizeof(p)); 1634 } 1635 ip6_tnl_parm_to_user(&p, &t->parms); 1636 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) { 1637 err = -EFAULT; 1638 } 1639 break; 1640 case SIOCADDTUNNEL: 1641 case SIOCCHGTUNNEL: 1642 err = -EPERM; 1643 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 1644 break; 1645 err = -EFAULT; 1646 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) 1647 break; 1648 err = -EINVAL; 1649 if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP && 1650 p.proto != 0) 1651 break; 1652 ip6_tnl_parm_from_user(&p1, &p); 1653 t = ip6_tnl_locate(net, &p1, cmd == SIOCADDTUNNEL); 1654 if (cmd == SIOCCHGTUNNEL) { 1655 if (!IS_ERR(t)) { 1656 if (t->dev != dev) { 1657 err = -EEXIST; 1658 break; 1659 } 1660 } else 1661 t = netdev_priv(dev); 1662 if (dev == ip6n->fb_tnl_dev) 1663 err = ip6_tnl0_update(t, &p1); 1664 else 1665 err = ip6_tnl_update(t, &p1); 1666 } 1667 if (!IS_ERR(t)) { 1668 err = 0; 1669 ip6_tnl_parm_to_user(&p, &t->parms); 1670 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) 1671 err = -EFAULT; 1672 1673 } else { 1674 err = PTR_ERR(t); 1675 } 1676 break; 1677 case SIOCDELTUNNEL: 1678 err = -EPERM; 1679 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 1680 break; 1681 1682 if (dev == ip6n->fb_tnl_dev) { 1683 err = -EFAULT; 1684 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) 1685 break; 1686 err = -ENOENT; 1687 ip6_tnl_parm_from_user(&p1, &p); 1688 t = ip6_tnl_locate(net, &p1, 0); 1689 if (IS_ERR(t)) 1690 break; 1691 err = -EPERM; 1692 if (t->dev == ip6n->fb_tnl_dev) 1693 break; 1694 dev = t->dev; 1695 } 1696 err = 0; 1697 unregister_netdevice(dev); 1698 break; 1699 default: 1700 err = -EINVAL; 1701 } 1702 return err; 1703 } 1704 1705 /** 1706 * ip6_tnl_change_mtu - change mtu manually for tunnel device 1707 * @dev: virtual device associated with tunnel 1708 * @new_mtu: the new mtu 1709 * 1710 * Return: 1711 * 0 on success, 1712 * %-EINVAL if mtu too small 1713 **/ 1714 1715 int ip6_tnl_change_mtu(struct net_device *dev, int new_mtu) 1716 { 1717 struct ip6_tnl *tnl = netdev_priv(dev); 1718 1719 if (tnl->parms.proto == IPPROTO_IPV6) { 1720 if (new_mtu < IPV6_MIN_MTU) 1721 return -EINVAL; 1722 } else { 1723 if (new_mtu < ETH_MIN_MTU) 1724 return -EINVAL; 1725 } 1726 if (tnl->parms.proto == IPPROTO_IPV6 || tnl->parms.proto == 0) { 1727 if (new_mtu > IP6_MAX_MTU - dev->hard_header_len) 1728 return -EINVAL; 1729 } else { 1730 if (new_mtu > IP_MAX_MTU - dev->hard_header_len) 1731 return -EINVAL; 1732 } 1733 dev->mtu = new_mtu; 1734 return 0; 1735 } 1736 EXPORT_SYMBOL(ip6_tnl_change_mtu); 1737 1738 int ip6_tnl_get_iflink(const struct net_device *dev) 1739 { 1740 struct ip6_tnl *t = netdev_priv(dev); 1741 1742 return t->parms.link; 1743 } 1744 EXPORT_SYMBOL(ip6_tnl_get_iflink); 1745 1746 int ip6_tnl_encap_add_ops(const struct ip6_tnl_encap_ops *ops, 1747 unsigned int num) 1748 { 1749 if (num >= MAX_IPTUN_ENCAP_OPS) 1750 return -ERANGE; 1751 1752 return !cmpxchg((const struct ip6_tnl_encap_ops **) 1753 &ip6tun_encaps[num], 1754 NULL, ops) ? 0 : -1; 1755 } 1756 EXPORT_SYMBOL(ip6_tnl_encap_add_ops); 1757 1758 int ip6_tnl_encap_del_ops(const struct ip6_tnl_encap_ops *ops, 1759 unsigned int num) 1760 { 1761 int ret; 1762 1763 if (num >= MAX_IPTUN_ENCAP_OPS) 1764 return -ERANGE; 1765 1766 ret = (cmpxchg((const struct ip6_tnl_encap_ops **) 1767 &ip6tun_encaps[num], 1768 ops, NULL) == ops) ? 0 : -1; 1769 1770 synchronize_net(); 1771 1772 return ret; 1773 } 1774 EXPORT_SYMBOL(ip6_tnl_encap_del_ops); 1775 1776 int ip6_tnl_encap_setup(struct ip6_tnl *t, 1777 struct ip_tunnel_encap *ipencap) 1778 { 1779 int hlen; 1780 1781 memset(&t->encap, 0, sizeof(t->encap)); 1782 1783 hlen = ip6_encap_hlen(ipencap); 1784 if (hlen < 0) 1785 return hlen; 1786 1787 t->encap.type = ipencap->type; 1788 t->encap.sport = ipencap->sport; 1789 t->encap.dport = ipencap->dport; 1790 t->encap.flags = ipencap->flags; 1791 1792 t->encap_hlen = hlen; 1793 t->hlen = t->encap_hlen + t->tun_hlen; 1794 1795 return 0; 1796 } 1797 EXPORT_SYMBOL_GPL(ip6_tnl_encap_setup); 1798 1799 static const struct net_device_ops ip6_tnl_netdev_ops = { 1800 .ndo_init = ip6_tnl_dev_init, 1801 .ndo_uninit = ip6_tnl_dev_uninit, 1802 .ndo_start_xmit = ip6_tnl_start_xmit, 1803 .ndo_do_ioctl = ip6_tnl_ioctl, 1804 .ndo_change_mtu = ip6_tnl_change_mtu, 1805 .ndo_get_stats = ip6_get_stats, 1806 .ndo_get_iflink = ip6_tnl_get_iflink, 1807 }; 1808 1809 #define IPXIPX_FEATURES (NETIF_F_SG | \ 1810 NETIF_F_FRAGLIST | \ 1811 NETIF_F_HIGHDMA | \ 1812 NETIF_F_GSO_SOFTWARE | \ 1813 NETIF_F_HW_CSUM) 1814 1815 /** 1816 * ip6_tnl_dev_setup - setup virtual tunnel device 1817 * @dev: virtual device associated with tunnel 1818 * 1819 * Description: 1820 * Initialize function pointers and device parameters 1821 **/ 1822 1823 static void ip6_tnl_dev_setup(struct net_device *dev) 1824 { 1825 dev->netdev_ops = &ip6_tnl_netdev_ops; 1826 dev->needs_free_netdev = true; 1827 dev->priv_destructor = ip6_dev_free; 1828 1829 dev->type = ARPHRD_TUNNEL6; 1830 dev->flags |= IFF_NOARP; 1831 dev->addr_len = sizeof(struct in6_addr); 1832 dev->features |= NETIF_F_LLTX; 1833 netif_keep_dst(dev); 1834 1835 dev->features |= IPXIPX_FEATURES; 1836 dev->hw_features |= IPXIPX_FEATURES; 1837 1838 /* This perm addr will be used as interface identifier by IPv6 */ 1839 dev->addr_assign_type = NET_ADDR_RANDOM; 1840 eth_random_addr(dev->perm_addr); 1841 } 1842 1843 1844 /** 1845 * ip6_tnl_dev_init_gen - general initializer for all tunnel devices 1846 * @dev: virtual device associated with tunnel 1847 **/ 1848 1849 static inline int 1850 ip6_tnl_dev_init_gen(struct net_device *dev) 1851 { 1852 struct ip6_tnl *t = netdev_priv(dev); 1853 int ret; 1854 int t_hlen; 1855 1856 t->dev = dev; 1857 t->net = dev_net(dev); 1858 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); 1859 if (!dev->tstats) 1860 return -ENOMEM; 1861 1862 ret = dst_cache_init(&t->dst_cache, GFP_KERNEL); 1863 if (ret) 1864 goto free_stats; 1865 1866 ret = gro_cells_init(&t->gro_cells, dev); 1867 if (ret) 1868 goto destroy_dst; 1869 1870 t->tun_hlen = 0; 1871 t->hlen = t->encap_hlen + t->tun_hlen; 1872 t_hlen = t->hlen + sizeof(struct ipv6hdr); 1873 1874 dev->type = ARPHRD_TUNNEL6; 1875 dev->hard_header_len = LL_MAX_HEADER + t_hlen; 1876 dev->mtu = ETH_DATA_LEN - t_hlen; 1877 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) 1878 dev->mtu -= 8; 1879 dev->min_mtu = ETH_MIN_MTU; 1880 dev->max_mtu = IP6_MAX_MTU - dev->hard_header_len; 1881 1882 return 0; 1883 1884 destroy_dst: 1885 dst_cache_destroy(&t->dst_cache); 1886 free_stats: 1887 free_percpu(dev->tstats); 1888 dev->tstats = NULL; 1889 1890 return ret; 1891 } 1892 1893 /** 1894 * ip6_tnl_dev_init - initializer for all non fallback tunnel devices 1895 * @dev: virtual device associated with tunnel 1896 **/ 1897 1898 static int ip6_tnl_dev_init(struct net_device *dev) 1899 { 1900 struct ip6_tnl *t = netdev_priv(dev); 1901 int err = ip6_tnl_dev_init_gen(dev); 1902 1903 if (err) 1904 return err; 1905 ip6_tnl_link_config(t); 1906 if (t->parms.collect_md) 1907 netif_keep_dst(dev); 1908 return 0; 1909 } 1910 1911 /** 1912 * ip6_fb_tnl_dev_init - initializer for fallback tunnel device 1913 * @dev: fallback device 1914 * 1915 * Return: 0 1916 **/ 1917 1918 static int __net_init ip6_fb_tnl_dev_init(struct net_device *dev) 1919 { 1920 struct ip6_tnl *t = netdev_priv(dev); 1921 struct net *net = dev_net(dev); 1922 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); 1923 1924 t->parms.proto = IPPROTO_IPV6; 1925 dev_hold(dev); 1926 1927 rcu_assign_pointer(ip6n->tnls_wc[0], t); 1928 return 0; 1929 } 1930 1931 static int ip6_tnl_validate(struct nlattr *tb[], struct nlattr *data[], 1932 struct netlink_ext_ack *extack) 1933 { 1934 u8 proto; 1935 1936 if (!data || !data[IFLA_IPTUN_PROTO]) 1937 return 0; 1938 1939 proto = nla_get_u8(data[IFLA_IPTUN_PROTO]); 1940 if (proto != IPPROTO_IPV6 && 1941 proto != IPPROTO_IPIP && 1942 proto != 0) 1943 return -EINVAL; 1944 1945 return 0; 1946 } 1947 1948 static void ip6_tnl_netlink_parms(struct nlattr *data[], 1949 struct __ip6_tnl_parm *parms) 1950 { 1951 memset(parms, 0, sizeof(*parms)); 1952 1953 if (!data) 1954 return; 1955 1956 if (data[IFLA_IPTUN_LINK]) 1957 parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]); 1958 1959 if (data[IFLA_IPTUN_LOCAL]) 1960 parms->laddr = nla_get_in6_addr(data[IFLA_IPTUN_LOCAL]); 1961 1962 if (data[IFLA_IPTUN_REMOTE]) 1963 parms->raddr = nla_get_in6_addr(data[IFLA_IPTUN_REMOTE]); 1964 1965 if (data[IFLA_IPTUN_TTL]) 1966 parms->hop_limit = nla_get_u8(data[IFLA_IPTUN_TTL]); 1967 1968 if (data[IFLA_IPTUN_ENCAP_LIMIT]) 1969 parms->encap_limit = nla_get_u8(data[IFLA_IPTUN_ENCAP_LIMIT]); 1970 1971 if (data[IFLA_IPTUN_FLOWINFO]) 1972 parms->flowinfo = nla_get_be32(data[IFLA_IPTUN_FLOWINFO]); 1973 1974 if (data[IFLA_IPTUN_FLAGS]) 1975 parms->flags = nla_get_u32(data[IFLA_IPTUN_FLAGS]); 1976 1977 if (data[IFLA_IPTUN_PROTO]) 1978 parms->proto = nla_get_u8(data[IFLA_IPTUN_PROTO]); 1979 1980 if (data[IFLA_IPTUN_COLLECT_METADATA]) 1981 parms->collect_md = true; 1982 1983 if (data[IFLA_IPTUN_FWMARK]) 1984 parms->fwmark = nla_get_u32(data[IFLA_IPTUN_FWMARK]); 1985 } 1986 1987 static bool ip6_tnl_netlink_encap_parms(struct nlattr *data[], 1988 struct ip_tunnel_encap *ipencap) 1989 { 1990 bool ret = false; 1991 1992 memset(ipencap, 0, sizeof(*ipencap)); 1993 1994 if (!data) 1995 return ret; 1996 1997 if (data[IFLA_IPTUN_ENCAP_TYPE]) { 1998 ret = true; 1999 ipencap->type = nla_get_u16(data[IFLA_IPTUN_ENCAP_TYPE]); 2000 } 2001 2002 if (data[IFLA_IPTUN_ENCAP_FLAGS]) { 2003 ret = true; 2004 ipencap->flags = nla_get_u16(data[IFLA_IPTUN_ENCAP_FLAGS]); 2005 } 2006 2007 if (data[IFLA_IPTUN_ENCAP_SPORT]) { 2008 ret = true; 2009 ipencap->sport = nla_get_be16(data[IFLA_IPTUN_ENCAP_SPORT]); 2010 } 2011 2012 if (data[IFLA_IPTUN_ENCAP_DPORT]) { 2013 ret = true; 2014 ipencap->dport = nla_get_be16(data[IFLA_IPTUN_ENCAP_DPORT]); 2015 } 2016 2017 return ret; 2018 } 2019 2020 static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev, 2021 struct nlattr *tb[], struct nlattr *data[], 2022 struct netlink_ext_ack *extack) 2023 { 2024 struct net *net = dev_net(dev); 2025 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); 2026 struct ip_tunnel_encap ipencap; 2027 struct ip6_tnl *nt, *t; 2028 int err; 2029 2030 nt = netdev_priv(dev); 2031 2032 if (ip6_tnl_netlink_encap_parms(data, &ipencap)) { 2033 err = ip6_tnl_encap_setup(nt, &ipencap); 2034 if (err < 0) 2035 return err; 2036 } 2037 2038 ip6_tnl_netlink_parms(data, &nt->parms); 2039 2040 if (nt->parms.collect_md) { 2041 if (rtnl_dereference(ip6n->collect_md_tun)) 2042 return -EEXIST; 2043 } else { 2044 t = ip6_tnl_locate(net, &nt->parms, 0); 2045 if (!IS_ERR(t)) 2046 return -EEXIST; 2047 } 2048 2049 err = ip6_tnl_create2(dev); 2050 if (!err && tb[IFLA_MTU]) 2051 ip6_tnl_change_mtu(dev, nla_get_u32(tb[IFLA_MTU])); 2052 2053 return err; 2054 } 2055 2056 static int ip6_tnl_changelink(struct net_device *dev, struct nlattr *tb[], 2057 struct nlattr *data[], 2058 struct netlink_ext_ack *extack) 2059 { 2060 struct ip6_tnl *t = netdev_priv(dev); 2061 struct __ip6_tnl_parm p; 2062 struct net *net = t->net; 2063 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); 2064 struct ip_tunnel_encap ipencap; 2065 2066 if (dev == ip6n->fb_tnl_dev) 2067 return -EINVAL; 2068 2069 if (ip6_tnl_netlink_encap_parms(data, &ipencap)) { 2070 int err = ip6_tnl_encap_setup(t, &ipencap); 2071 2072 if (err < 0) 2073 return err; 2074 } 2075 ip6_tnl_netlink_parms(data, &p); 2076 if (p.collect_md) 2077 return -EINVAL; 2078 2079 t = ip6_tnl_locate(net, &p, 0); 2080 if (!IS_ERR(t)) { 2081 if (t->dev != dev) 2082 return -EEXIST; 2083 } else 2084 t = netdev_priv(dev); 2085 2086 return ip6_tnl_update(t, &p); 2087 } 2088 2089 static void ip6_tnl_dellink(struct net_device *dev, struct list_head *head) 2090 { 2091 struct net *net = dev_net(dev); 2092 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); 2093 2094 if (dev != ip6n->fb_tnl_dev) 2095 unregister_netdevice_queue(dev, head); 2096 } 2097 2098 static size_t ip6_tnl_get_size(const struct net_device *dev) 2099 { 2100 return 2101 /* IFLA_IPTUN_LINK */ 2102 nla_total_size(4) + 2103 /* IFLA_IPTUN_LOCAL */ 2104 nla_total_size(sizeof(struct in6_addr)) + 2105 /* IFLA_IPTUN_REMOTE */ 2106 nla_total_size(sizeof(struct in6_addr)) + 2107 /* IFLA_IPTUN_TTL */ 2108 nla_total_size(1) + 2109 /* IFLA_IPTUN_ENCAP_LIMIT */ 2110 nla_total_size(1) + 2111 /* IFLA_IPTUN_FLOWINFO */ 2112 nla_total_size(4) + 2113 /* IFLA_IPTUN_FLAGS */ 2114 nla_total_size(4) + 2115 /* IFLA_IPTUN_PROTO */ 2116 nla_total_size(1) + 2117 /* IFLA_IPTUN_ENCAP_TYPE */ 2118 nla_total_size(2) + 2119 /* IFLA_IPTUN_ENCAP_FLAGS */ 2120 nla_total_size(2) + 2121 /* IFLA_IPTUN_ENCAP_SPORT */ 2122 nla_total_size(2) + 2123 /* IFLA_IPTUN_ENCAP_DPORT */ 2124 nla_total_size(2) + 2125 /* IFLA_IPTUN_COLLECT_METADATA */ 2126 nla_total_size(0) + 2127 /* IFLA_IPTUN_FWMARK */ 2128 nla_total_size(4) + 2129 0; 2130 } 2131 2132 static int ip6_tnl_fill_info(struct sk_buff *skb, const struct net_device *dev) 2133 { 2134 struct ip6_tnl *tunnel = netdev_priv(dev); 2135 struct __ip6_tnl_parm *parm = &tunnel->parms; 2136 2137 if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) || 2138 nla_put_in6_addr(skb, IFLA_IPTUN_LOCAL, &parm->laddr) || 2139 nla_put_in6_addr(skb, IFLA_IPTUN_REMOTE, &parm->raddr) || 2140 nla_put_u8(skb, IFLA_IPTUN_TTL, parm->hop_limit) || 2141 nla_put_u8(skb, IFLA_IPTUN_ENCAP_LIMIT, parm->encap_limit) || 2142 nla_put_be32(skb, IFLA_IPTUN_FLOWINFO, parm->flowinfo) || 2143 nla_put_u32(skb, IFLA_IPTUN_FLAGS, parm->flags) || 2144 nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->proto) || 2145 nla_put_u32(skb, IFLA_IPTUN_FWMARK, parm->fwmark)) 2146 goto nla_put_failure; 2147 2148 if (nla_put_u16(skb, IFLA_IPTUN_ENCAP_TYPE, tunnel->encap.type) || 2149 nla_put_be16(skb, IFLA_IPTUN_ENCAP_SPORT, tunnel->encap.sport) || 2150 nla_put_be16(skb, IFLA_IPTUN_ENCAP_DPORT, tunnel->encap.dport) || 2151 nla_put_u16(skb, IFLA_IPTUN_ENCAP_FLAGS, tunnel->encap.flags)) 2152 goto nla_put_failure; 2153 2154 if (parm->collect_md) 2155 if (nla_put_flag(skb, IFLA_IPTUN_COLLECT_METADATA)) 2156 goto nla_put_failure; 2157 2158 return 0; 2159 2160 nla_put_failure: 2161 return -EMSGSIZE; 2162 } 2163 2164 struct net *ip6_tnl_get_link_net(const struct net_device *dev) 2165 { 2166 struct ip6_tnl *tunnel = netdev_priv(dev); 2167 2168 return tunnel->net; 2169 } 2170 EXPORT_SYMBOL(ip6_tnl_get_link_net); 2171 2172 static const struct nla_policy ip6_tnl_policy[IFLA_IPTUN_MAX + 1] = { 2173 [IFLA_IPTUN_LINK] = { .type = NLA_U32 }, 2174 [IFLA_IPTUN_LOCAL] = { .len = sizeof(struct in6_addr) }, 2175 [IFLA_IPTUN_REMOTE] = { .len = sizeof(struct in6_addr) }, 2176 [IFLA_IPTUN_TTL] = { .type = NLA_U8 }, 2177 [IFLA_IPTUN_ENCAP_LIMIT] = { .type = NLA_U8 }, 2178 [IFLA_IPTUN_FLOWINFO] = { .type = NLA_U32 }, 2179 [IFLA_IPTUN_FLAGS] = { .type = NLA_U32 }, 2180 [IFLA_IPTUN_PROTO] = { .type = NLA_U8 }, 2181 [IFLA_IPTUN_ENCAP_TYPE] = { .type = NLA_U16 }, 2182 [IFLA_IPTUN_ENCAP_FLAGS] = { .type = NLA_U16 }, 2183 [IFLA_IPTUN_ENCAP_SPORT] = { .type = NLA_U16 }, 2184 [IFLA_IPTUN_ENCAP_DPORT] = { .type = NLA_U16 }, 2185 [IFLA_IPTUN_COLLECT_METADATA] = { .type = NLA_FLAG }, 2186 [IFLA_IPTUN_FWMARK] = { .type = NLA_U32 }, 2187 }; 2188 2189 static struct rtnl_link_ops ip6_link_ops __read_mostly = { 2190 .kind = "ip6tnl", 2191 .maxtype = IFLA_IPTUN_MAX, 2192 .policy = ip6_tnl_policy, 2193 .priv_size = sizeof(struct ip6_tnl), 2194 .setup = ip6_tnl_dev_setup, 2195 .validate = ip6_tnl_validate, 2196 .newlink = ip6_tnl_newlink, 2197 .changelink = ip6_tnl_changelink, 2198 .dellink = ip6_tnl_dellink, 2199 .get_size = ip6_tnl_get_size, 2200 .fill_info = ip6_tnl_fill_info, 2201 .get_link_net = ip6_tnl_get_link_net, 2202 }; 2203 2204 static struct xfrm6_tunnel ip4ip6_handler __read_mostly = { 2205 .handler = ip4ip6_rcv, 2206 .err_handler = ip4ip6_err, 2207 .priority = 1, 2208 }; 2209 2210 static struct xfrm6_tunnel ip6ip6_handler __read_mostly = { 2211 .handler = ip6ip6_rcv, 2212 .err_handler = ip6ip6_err, 2213 .priority = 1, 2214 }; 2215 2216 static void __net_exit ip6_tnl_destroy_tunnels(struct net *net, struct list_head *list) 2217 { 2218 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); 2219 struct net_device *dev, *aux; 2220 int h; 2221 struct ip6_tnl *t; 2222 2223 for_each_netdev_safe(net, dev, aux) 2224 if (dev->rtnl_link_ops == &ip6_link_ops) 2225 unregister_netdevice_queue(dev, list); 2226 2227 for (h = 0; h < IP6_TUNNEL_HASH_SIZE; h++) { 2228 t = rtnl_dereference(ip6n->tnls_r_l[h]); 2229 while (t) { 2230 /* If dev is in the same netns, it has already 2231 * been added to the list by the previous loop. 2232 */ 2233 if (!net_eq(dev_net(t->dev), net)) 2234 unregister_netdevice_queue(t->dev, list); 2235 t = rtnl_dereference(t->next); 2236 } 2237 } 2238 } 2239 2240 static int __net_init ip6_tnl_init_net(struct net *net) 2241 { 2242 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); 2243 struct ip6_tnl *t = NULL; 2244 int err; 2245 2246 ip6n->tnls[0] = ip6n->tnls_wc; 2247 ip6n->tnls[1] = ip6n->tnls_r_l; 2248 2249 if (!net_has_fallback_tunnels(net)) 2250 return 0; 2251 err = -ENOMEM; 2252 ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0", 2253 NET_NAME_UNKNOWN, ip6_tnl_dev_setup); 2254 2255 if (!ip6n->fb_tnl_dev) 2256 goto err_alloc_dev; 2257 dev_net_set(ip6n->fb_tnl_dev, net); 2258 ip6n->fb_tnl_dev->rtnl_link_ops = &ip6_link_ops; 2259 /* FB netdevice is special: we have one, and only one per netns. 2260 * Allowing to move it to another netns is clearly unsafe. 2261 */ 2262 ip6n->fb_tnl_dev->features |= NETIF_F_NETNS_LOCAL; 2263 2264 err = ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev); 2265 if (err < 0) 2266 goto err_register; 2267 2268 err = register_netdev(ip6n->fb_tnl_dev); 2269 if (err < 0) 2270 goto err_register; 2271 2272 t = netdev_priv(ip6n->fb_tnl_dev); 2273 2274 strcpy(t->parms.name, ip6n->fb_tnl_dev->name); 2275 return 0; 2276 2277 err_register: 2278 free_netdev(ip6n->fb_tnl_dev); 2279 err_alloc_dev: 2280 return err; 2281 } 2282 2283 static void __net_exit ip6_tnl_exit_batch_net(struct list_head *net_list) 2284 { 2285 struct net *net; 2286 LIST_HEAD(list); 2287 2288 rtnl_lock(); 2289 list_for_each_entry(net, net_list, exit_list) 2290 ip6_tnl_destroy_tunnels(net, &list); 2291 unregister_netdevice_many(&list); 2292 rtnl_unlock(); 2293 } 2294 2295 static struct pernet_operations ip6_tnl_net_ops = { 2296 .init = ip6_tnl_init_net, 2297 .exit_batch = ip6_tnl_exit_batch_net, 2298 .id = &ip6_tnl_net_id, 2299 .size = sizeof(struct ip6_tnl_net), 2300 }; 2301 2302 /** 2303 * ip6_tunnel_init - register protocol and reserve needed resources 2304 * 2305 * Return: 0 on success 2306 **/ 2307 2308 static int __init ip6_tunnel_init(void) 2309 { 2310 int err; 2311 2312 if (!ipv6_mod_enabled()) 2313 return -EOPNOTSUPP; 2314 2315 err = register_pernet_device(&ip6_tnl_net_ops); 2316 if (err < 0) 2317 goto out_pernet; 2318 2319 err = xfrm6_tunnel_register(&ip4ip6_handler, AF_INET); 2320 if (err < 0) { 2321 pr_err("%s: can't register ip4ip6\n", __func__); 2322 goto out_ip4ip6; 2323 } 2324 2325 err = xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6); 2326 if (err < 0) { 2327 pr_err("%s: can't register ip6ip6\n", __func__); 2328 goto out_ip6ip6; 2329 } 2330 err = rtnl_link_register(&ip6_link_ops); 2331 if (err < 0) 2332 goto rtnl_link_failed; 2333 2334 return 0; 2335 2336 rtnl_link_failed: 2337 xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6); 2338 out_ip6ip6: 2339 xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET); 2340 out_ip4ip6: 2341 unregister_pernet_device(&ip6_tnl_net_ops); 2342 out_pernet: 2343 return err; 2344 } 2345 2346 /** 2347 * ip6_tunnel_cleanup - free resources and unregister protocol 2348 **/ 2349 2350 static void __exit ip6_tunnel_cleanup(void) 2351 { 2352 rtnl_link_unregister(&ip6_link_ops); 2353 if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET)) 2354 pr_info("%s: can't deregister ip4ip6\n", __func__); 2355 2356 if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6)) 2357 pr_info("%s: can't deregister ip6ip6\n", __func__); 2358 2359 unregister_pernet_device(&ip6_tnl_net_ops); 2360 } 2361 2362 module_init(ip6_tunnel_init); 2363 module_exit(ip6_tunnel_cleanup); 2364