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