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_headroom(skb) < max_headroom || skb_shared(skb) || 1240 (skb_cloned(skb) && !skb_clone_writable(skb, 0))) { 1241 struct sk_buff *new_skb; 1242 1243 new_skb = skb_realloc_headroom(skb, max_headroom); 1244 if (!new_skb) 1245 goto tx_err_dst_release; 1246 1247 if (skb->sk) 1248 skb_set_owner_w(new_skb, skb->sk); 1249 consume_skb(skb); 1250 skb = new_skb; 1251 } 1252 1253 if (t->parms.collect_md) { 1254 if (t->encap.type != TUNNEL_ENCAP_NONE) 1255 goto tx_err_dst_release; 1256 } else { 1257 if (use_cache && ndst) 1258 dst_cache_set_ip6(&t->dst_cache, ndst, &fl6->saddr); 1259 } 1260 skb_dst_set(skb, dst); 1261 1262 if (hop_limit == 0) { 1263 if (payload_protocol == htons(ETH_P_IP)) 1264 hop_limit = ip_hdr(skb)->ttl; 1265 else if (payload_protocol == htons(ETH_P_IPV6)) 1266 hop_limit = ipv6_hdr(skb)->hop_limit; 1267 else 1268 hop_limit = ip6_dst_hoplimit(dst); 1269 } 1270 1271 /* Calculate max headroom for all the headers and adjust 1272 * needed_headroom if necessary. 1273 */ 1274 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct ipv6hdr) 1275 + dst->header_len + t->hlen; 1276 ip_tunnel_adj_headroom(dev, max_headroom); 1277 1278 err = ip6_tnl_encap(skb, t, &proto, fl6); 1279 if (err) 1280 return err; 1281 1282 if (encap_limit >= 0) { 1283 init_tel_txopt(&opt, encap_limit); 1284 proto = ipv6_push_frag_opts(skb, &opt.ops, proto); 1285 } 1286 1287 skb_push(skb, sizeof(struct ipv6hdr)); 1288 skb_reset_network_header(skb); 1289 ipv6h = ipv6_hdr(skb); 1290 ip6_flow_hdr(ipv6h, dsfield, 1291 ip6_make_flowlabel(net, skb, fl6->flowlabel, true, fl6)); 1292 ipv6h->hop_limit = hop_limit; 1293 ipv6h->nexthdr = proto; 1294 ipv6h->saddr = fl6->saddr; 1295 ipv6h->daddr = fl6->daddr; 1296 ip6tunnel_xmit(NULL, skb, dev, 0); 1297 return 0; 1298 tx_err_link_failure: 1299 DEV_STATS_INC(dev, tx_carrier_errors); 1300 dst_link_failure(skb); 1301 tx_err_dst_release: 1302 dst_release(dst); 1303 return err; 1304 } 1305 EXPORT_SYMBOL(ip6_tnl_xmit); 1306 1307 static inline int 1308 ipxip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev, 1309 u8 protocol) 1310 { 1311 struct ip6_tnl *t = netdev_priv(dev); 1312 struct ipv6hdr *ipv6h; 1313 const struct iphdr *iph; 1314 int encap_limit = -1; 1315 __u16 offset; 1316 struct flowi6 fl6; 1317 __u8 dsfield, orig_dsfield; 1318 __u32 mtu; 1319 u8 tproto; 1320 int err; 1321 1322 tproto = READ_ONCE(t->parms.proto); 1323 if (tproto != protocol && tproto != 0) 1324 return -1; 1325 1326 if (t->parms.collect_md) { 1327 struct ip_tunnel_info *tun_info; 1328 const struct ip_tunnel_key *key; 1329 1330 tun_info = skb_tunnel_info(skb); 1331 if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) || 1332 ip_tunnel_info_af(tun_info) != AF_INET6)) 1333 return -1; 1334 key = &tun_info->key; 1335 memset(&fl6, 0, sizeof(fl6)); 1336 fl6.flowi6_proto = protocol; 1337 fl6.saddr = key->u.ipv6.src; 1338 fl6.daddr = key->u.ipv6.dst; 1339 fl6.flowlabel = key->label; 1340 dsfield = key->tos; 1341 switch (protocol) { 1342 case IPPROTO_IPIP: 1343 iph = ip_hdr(skb); 1344 orig_dsfield = ipv4_get_dsfield(iph); 1345 break; 1346 case IPPROTO_IPV6: 1347 ipv6h = ipv6_hdr(skb); 1348 orig_dsfield = ipv6_get_dsfield(ipv6h); 1349 break; 1350 default: 1351 orig_dsfield = dsfield; 1352 break; 1353 } 1354 } else { 1355 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) 1356 encap_limit = t->parms.encap_limit; 1357 if (protocol == IPPROTO_IPV6) { 1358 offset = ip6_tnl_parse_tlv_enc_lim(skb, 1359 skb_network_header(skb)); 1360 /* ip6_tnl_parse_tlv_enc_lim() might have 1361 * reallocated skb->head 1362 */ 1363 if (offset > 0) { 1364 struct ipv6_tlv_tnl_enc_lim *tel; 1365 1366 tel = (void *)&skb_network_header(skb)[offset]; 1367 if (tel->encap_limit == 0) { 1368 icmpv6_ndo_send(skb, ICMPV6_PARAMPROB, 1369 ICMPV6_HDR_FIELD, offset + 2); 1370 return -1; 1371 } 1372 encap_limit = tel->encap_limit - 1; 1373 } 1374 } 1375 1376 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6)); 1377 fl6.flowi6_proto = protocol; 1378 1379 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK) 1380 fl6.flowi6_mark = skb->mark; 1381 else 1382 fl6.flowi6_mark = t->parms.fwmark; 1383 switch (protocol) { 1384 case IPPROTO_IPIP: 1385 iph = ip_hdr(skb); 1386 orig_dsfield = ipv4_get_dsfield(iph); 1387 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS) 1388 dsfield = orig_dsfield; 1389 else 1390 dsfield = ip6_tclass(t->parms.flowinfo); 1391 break; 1392 case IPPROTO_IPV6: 1393 ipv6h = ipv6_hdr(skb); 1394 orig_dsfield = ipv6_get_dsfield(ipv6h); 1395 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS) 1396 dsfield = orig_dsfield; 1397 else 1398 dsfield = ip6_tclass(t->parms.flowinfo); 1399 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL) 1400 fl6.flowlabel |= ip6_flowlabel(ipv6h); 1401 break; 1402 default: 1403 orig_dsfield = dsfield = ip6_tclass(t->parms.flowinfo); 1404 break; 1405 } 1406 } 1407 1408 fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL); 1409 dsfield = INET_ECN_encapsulate(dsfield, orig_dsfield); 1410 1411 if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP6)) 1412 return -1; 1413 1414 skb_set_inner_ipproto(skb, protocol); 1415 1416 err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu, 1417 protocol); 1418 if (err != 0) { 1419 /* XXX: send ICMP error even if DF is not set. */ 1420 if (err == -EMSGSIZE) 1421 switch (protocol) { 1422 case IPPROTO_IPIP: 1423 icmp_ndo_send(skb, ICMP_DEST_UNREACH, 1424 ICMP_FRAG_NEEDED, htonl(mtu)); 1425 break; 1426 case IPPROTO_IPV6: 1427 icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); 1428 break; 1429 default: 1430 break; 1431 } 1432 return -1; 1433 } 1434 1435 return 0; 1436 } 1437 1438 static netdev_tx_t 1439 ip6_tnl_start_xmit(struct sk_buff *skb, struct net_device *dev) 1440 { 1441 struct ip6_tnl *t = netdev_priv(dev); 1442 u8 ipproto; 1443 int ret; 1444 1445 if (!pskb_inet_may_pull(skb)) 1446 goto tx_err; 1447 1448 switch (skb->protocol) { 1449 case htons(ETH_P_IP): 1450 ipproto = IPPROTO_IPIP; 1451 break; 1452 case htons(ETH_P_IPV6): 1453 if (ip6_tnl_addr_conflict(t, ipv6_hdr(skb))) 1454 goto tx_err; 1455 ipproto = IPPROTO_IPV6; 1456 break; 1457 case htons(ETH_P_MPLS_UC): 1458 ipproto = IPPROTO_MPLS; 1459 break; 1460 default: 1461 goto tx_err; 1462 } 1463 1464 ret = ipxip6_tnl_xmit(skb, dev, ipproto); 1465 if (ret < 0) 1466 goto tx_err; 1467 1468 return NETDEV_TX_OK; 1469 1470 tx_err: 1471 DEV_STATS_INC(dev, tx_errors); 1472 DEV_STATS_INC(dev, tx_dropped); 1473 kfree_skb(skb); 1474 return NETDEV_TX_OK; 1475 } 1476 1477 static void ip6_tnl_link_config(struct ip6_tnl *t) 1478 { 1479 struct net_device *dev = t->dev; 1480 struct net_device *tdev = NULL; 1481 struct __ip6_tnl_parm *p = &t->parms; 1482 struct flowi6 *fl6 = &t->fl.u.ip6; 1483 int t_hlen; 1484 int mtu; 1485 1486 __dev_addr_set(dev, &p->laddr, sizeof(struct in6_addr)); 1487 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr)); 1488 1489 /* Set up flowi template */ 1490 fl6->saddr = p->laddr; 1491 fl6->daddr = p->raddr; 1492 fl6->flowi6_oif = p->link; 1493 fl6->flowlabel = 0; 1494 1495 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS)) 1496 fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo; 1497 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL)) 1498 fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo; 1499 1500 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET); 1501 p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr); 1502 1503 if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV) 1504 dev->flags |= IFF_POINTOPOINT; 1505 else 1506 dev->flags &= ~IFF_POINTOPOINT; 1507 1508 t->tun_hlen = 0; 1509 t->hlen = t->encap_hlen + t->tun_hlen; 1510 t_hlen = t->hlen + sizeof(struct ipv6hdr); 1511 1512 if (p->flags & IP6_TNL_F_CAP_XMIT) { 1513 int strict = (ipv6_addr_type(&p->raddr) & 1514 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)); 1515 1516 struct rt6_info *rt = rt6_lookup(t->net, 1517 &p->raddr, &p->laddr, 1518 p->link, NULL, strict); 1519 if (rt) { 1520 tdev = rt->dst.dev; 1521 ip6_rt_put(rt); 1522 } 1523 1524 if (!tdev && p->link) 1525 tdev = __dev_get_by_index(t->net, p->link); 1526 1527 if (tdev) { 1528 dev->needed_headroom = tdev->hard_header_len + 1529 tdev->needed_headroom + t_hlen; 1530 mtu = min_t(unsigned int, tdev->mtu, IP6_MAX_MTU); 1531 1532 mtu = mtu - t_hlen; 1533 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) 1534 mtu -= 8; 1535 1536 if (mtu < IPV6_MIN_MTU) 1537 mtu = IPV6_MIN_MTU; 1538 WRITE_ONCE(dev->mtu, mtu); 1539 } 1540 } 1541 } 1542 1543 /** 1544 * ip6_tnl_change - update the tunnel parameters 1545 * @t: tunnel to be changed 1546 * @p: tunnel configuration parameters 1547 * 1548 * Description: 1549 * ip6_tnl_change() updates the tunnel parameters 1550 **/ 1551 1552 static void 1553 ip6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p) 1554 { 1555 t->parms.laddr = p->laddr; 1556 t->parms.raddr = p->raddr; 1557 t->parms.flags = p->flags; 1558 t->parms.hop_limit = p->hop_limit; 1559 t->parms.encap_limit = p->encap_limit; 1560 t->parms.flowinfo = p->flowinfo; 1561 t->parms.link = p->link; 1562 t->parms.proto = p->proto; 1563 t->parms.fwmark = p->fwmark; 1564 dst_cache_reset(&t->dst_cache); 1565 ip6_tnl_link_config(t); 1566 } 1567 1568 static void ip6_tnl_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p) 1569 { 1570 struct net *net = t->net; 1571 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); 1572 1573 ip6_tnl_unlink(ip6n, t); 1574 synchronize_net(); 1575 ip6_tnl_change(t, p); 1576 ip6_tnl_link(ip6n, t); 1577 netdev_state_change(t->dev); 1578 } 1579 1580 static int ip6_tnl0_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p, 1581 bool strict) 1582 { 1583 /* For the default ip6tnl0 device, allow changing only the protocol 1584 * (the IP6_TNL_F_CAP_PER_PACKET flag is set on ip6tnl0, and all other 1585 * parameters are 0). 1586 */ 1587 if (strict && 1588 (!ipv6_addr_any(&p->laddr) || !ipv6_addr_any(&p->raddr) || 1589 p->flags != t->parms.flags || p->hop_limit || p->encap_limit || 1590 p->flowinfo || p->link || p->fwmark || p->collect_md)) 1591 return -EINVAL; 1592 1593 t->parms.proto = p->proto; 1594 netdev_state_change(t->dev); 1595 return 0; 1596 } 1597 1598 static void 1599 ip6_tnl_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm *u) 1600 { 1601 p->laddr = u->laddr; 1602 p->raddr = u->raddr; 1603 p->flags = u->flags; 1604 p->hop_limit = u->hop_limit; 1605 p->encap_limit = u->encap_limit; 1606 p->flowinfo = u->flowinfo; 1607 p->link = u->link; 1608 p->proto = u->proto; 1609 memcpy(p->name, u->name, sizeof(u->name)); 1610 } 1611 1612 static void 1613 ip6_tnl_parm_to_user(struct ip6_tnl_parm *u, const struct __ip6_tnl_parm *p) 1614 { 1615 u->laddr = p->laddr; 1616 u->raddr = p->raddr; 1617 u->flags = p->flags; 1618 u->hop_limit = p->hop_limit; 1619 u->encap_limit = p->encap_limit; 1620 u->flowinfo = p->flowinfo; 1621 u->link = p->link; 1622 u->proto = p->proto; 1623 memcpy(u->name, p->name, sizeof(u->name)); 1624 } 1625 1626 /** 1627 * ip6_tnl_siocdevprivate - configure ipv6 tunnels from userspace 1628 * @dev: virtual device associated with tunnel 1629 * @ifr: unused 1630 * @data: parameters passed from userspace 1631 * @cmd: command to be performed 1632 * 1633 * Description: 1634 * ip6_tnl_ioctl() is used for managing IPv6 tunnels 1635 * from userspace. 1636 * 1637 * The possible commands are the following: 1638 * %SIOCGETTUNNEL: get tunnel parameters for device 1639 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters 1640 * %SIOCCHGTUNNEL: change tunnel parameters to those given 1641 * %SIOCDELTUNNEL: delete tunnel 1642 * 1643 * The fallback device "ip6tnl0", created during module 1644 * initialization, can be used for creating other tunnel devices. 1645 * 1646 * Return: 1647 * 0 on success, 1648 * %-EFAULT if unable to copy data to or from userspace, 1649 * %-EPERM if current process hasn't %CAP_NET_ADMIN set 1650 * %-EINVAL if passed tunnel parameters are invalid, 1651 * %-EEXIST if changing a tunnel's parameters would cause a conflict 1652 * %-ENODEV if attempting to change or delete a nonexisting device 1653 **/ 1654 1655 static int 1656 ip6_tnl_siocdevprivate(struct net_device *dev, struct ifreq *ifr, 1657 void __user *data, int cmd) 1658 { 1659 int err = 0; 1660 struct ip6_tnl_parm p; 1661 struct __ip6_tnl_parm p1; 1662 struct ip6_tnl *t = netdev_priv(dev); 1663 struct net *net = t->net; 1664 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); 1665 1666 memset(&p1, 0, sizeof(p1)); 1667 1668 switch (cmd) { 1669 case SIOCGETTUNNEL: 1670 if (dev == ip6n->fb_tnl_dev) { 1671 if (copy_from_user(&p, data, sizeof(p))) { 1672 err = -EFAULT; 1673 break; 1674 } 1675 ip6_tnl_parm_from_user(&p1, &p); 1676 t = ip6_tnl_locate(net, &p1, 0); 1677 if (IS_ERR(t)) 1678 t = netdev_priv(dev); 1679 } else { 1680 memset(&p, 0, sizeof(p)); 1681 } 1682 ip6_tnl_parm_to_user(&p, &t->parms); 1683 if (copy_to_user(data, &p, sizeof(p))) 1684 err = -EFAULT; 1685 break; 1686 case SIOCADDTUNNEL: 1687 case SIOCCHGTUNNEL: 1688 err = -EPERM; 1689 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 1690 break; 1691 err = -EFAULT; 1692 if (copy_from_user(&p, data, sizeof(p))) 1693 break; 1694 err = -EINVAL; 1695 if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP && 1696 p.proto != 0) 1697 break; 1698 ip6_tnl_parm_from_user(&p1, &p); 1699 t = ip6_tnl_locate(net, &p1, cmd == SIOCADDTUNNEL); 1700 if (cmd == SIOCCHGTUNNEL) { 1701 if (!IS_ERR(t)) { 1702 if (t->dev != dev) { 1703 err = -EEXIST; 1704 break; 1705 } 1706 } else 1707 t = netdev_priv(dev); 1708 if (dev == ip6n->fb_tnl_dev) 1709 ip6_tnl0_update(t, &p1, false); 1710 else 1711 ip6_tnl_update(t, &p1); 1712 } 1713 if (!IS_ERR(t)) { 1714 err = 0; 1715 ip6_tnl_parm_to_user(&p, &t->parms); 1716 if (copy_to_user(data, &p, sizeof(p))) 1717 err = -EFAULT; 1718 1719 } else { 1720 err = PTR_ERR(t); 1721 } 1722 break; 1723 case SIOCDELTUNNEL: 1724 err = -EPERM; 1725 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 1726 break; 1727 1728 if (dev == ip6n->fb_tnl_dev) { 1729 err = -EFAULT; 1730 if (copy_from_user(&p, data, sizeof(p))) 1731 break; 1732 err = -ENOENT; 1733 ip6_tnl_parm_from_user(&p1, &p); 1734 t = ip6_tnl_locate(net, &p1, 0); 1735 if (IS_ERR(t)) 1736 break; 1737 err = -EPERM; 1738 if (t->dev == ip6n->fb_tnl_dev) 1739 break; 1740 dev = t->dev; 1741 } 1742 err = 0; 1743 unregister_netdevice(dev); 1744 break; 1745 default: 1746 err = -EINVAL; 1747 } 1748 return err; 1749 } 1750 1751 /** 1752 * ip6_tnl_change_mtu - change mtu manually for tunnel device 1753 * @dev: virtual device associated with tunnel 1754 * @new_mtu: the new mtu 1755 * 1756 * Return: 1757 * 0 on success, 1758 * %-EINVAL if mtu too small 1759 **/ 1760 1761 int ip6_tnl_change_mtu(struct net_device *dev, int new_mtu) 1762 { 1763 struct ip6_tnl *tnl = netdev_priv(dev); 1764 int t_hlen; 1765 1766 t_hlen = tnl->hlen + sizeof(struct ipv6hdr); 1767 if (tnl->parms.proto == IPPROTO_IPV6) { 1768 if (new_mtu < IPV6_MIN_MTU) 1769 return -EINVAL; 1770 } else { 1771 if (new_mtu < ETH_MIN_MTU) 1772 return -EINVAL; 1773 } 1774 if (tnl->parms.proto == IPPROTO_IPV6 || tnl->parms.proto == 0) { 1775 if (new_mtu > IP6_MAX_MTU - dev->hard_header_len - t_hlen) 1776 return -EINVAL; 1777 } else { 1778 if (new_mtu > IP_MAX_MTU - dev->hard_header_len - t_hlen) 1779 return -EINVAL; 1780 } 1781 WRITE_ONCE(dev->mtu, new_mtu); 1782 return 0; 1783 } 1784 EXPORT_SYMBOL(ip6_tnl_change_mtu); 1785 1786 int ip6_tnl_get_iflink(const struct net_device *dev) 1787 { 1788 struct ip6_tnl *t = netdev_priv(dev); 1789 1790 return READ_ONCE(t->parms.link); 1791 } 1792 EXPORT_SYMBOL(ip6_tnl_get_iflink); 1793 1794 int ip6_tnl_encap_add_ops(const struct ip6_tnl_encap_ops *ops, 1795 unsigned int num) 1796 { 1797 if (num >= MAX_IPTUN_ENCAP_OPS) 1798 return -ERANGE; 1799 1800 return !cmpxchg((const struct ip6_tnl_encap_ops **) 1801 &ip6tun_encaps[num], 1802 NULL, ops) ? 0 : -1; 1803 } 1804 EXPORT_SYMBOL(ip6_tnl_encap_add_ops); 1805 1806 int ip6_tnl_encap_del_ops(const struct ip6_tnl_encap_ops *ops, 1807 unsigned int num) 1808 { 1809 int ret; 1810 1811 if (num >= MAX_IPTUN_ENCAP_OPS) 1812 return -ERANGE; 1813 1814 ret = (cmpxchg((const struct ip6_tnl_encap_ops **) 1815 &ip6tun_encaps[num], 1816 ops, NULL) == ops) ? 0 : -1; 1817 1818 synchronize_net(); 1819 1820 return ret; 1821 } 1822 EXPORT_SYMBOL(ip6_tnl_encap_del_ops); 1823 1824 int ip6_tnl_encap_setup(struct ip6_tnl *t, 1825 struct ip_tunnel_encap *ipencap) 1826 { 1827 int hlen; 1828 1829 memset(&t->encap, 0, sizeof(t->encap)); 1830 1831 hlen = ip6_encap_hlen(ipencap); 1832 if (hlen < 0) 1833 return hlen; 1834 1835 t->encap.type = ipencap->type; 1836 t->encap.sport = ipencap->sport; 1837 t->encap.dport = ipencap->dport; 1838 t->encap.flags = ipencap->flags; 1839 1840 t->encap_hlen = hlen; 1841 t->hlen = t->encap_hlen + t->tun_hlen; 1842 1843 return 0; 1844 } 1845 EXPORT_SYMBOL_GPL(ip6_tnl_encap_setup); 1846 1847 static int ip6_tnl_fill_forward_path(struct net_device_path_ctx *ctx, 1848 struct net_device_path *path) 1849 { 1850 struct ip6_tnl *t = netdev_priv(ctx->dev); 1851 struct dst_entry *dst; 1852 struct flowi6 fl6; 1853 int err; 1854 1855 if (t->parms.flags & (IP6_TNL_F_USE_ORIG_TCLASS | 1856 IP6_TNL_F_USE_ORIG_FLOWLABEL | 1857 IP6_TNL_F_USE_ORIG_FWMARK)) 1858 return -EOPNOTSUPP; 1859 1860 if (t->parms.collect_md) 1861 return -EOPNOTSUPP; 1862 1863 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) 1864 return -EOPNOTSUPP; 1865 1866 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6)); 1867 fl6.flowi6_mark = t->parms.fwmark; 1868 fl6.flowi6_proto = 0; 1869 1870 dst = ip6_route_output(dev_net(ctx->dev), NULL, &fl6); 1871 if (!dst->error) { 1872 path->type = DEV_PATH_TUN; 1873 path->tun.src_v6 = fl6.saddr; 1874 path->tun.dst_v6 = fl6.daddr; 1875 path->tun.l3_proto = IPPROTO_IPV6; 1876 path->tun.dst = dst; 1877 path->dev = ctx->dev; 1878 ctx->dev = dst->dev; 1879 } 1880 1881 err = dst->error; 1882 if (err) 1883 dst_release(dst); 1884 1885 return err; 1886 } 1887 1888 static const struct net_device_ops ip6_tnl_netdev_ops = { 1889 .ndo_init = ip6_tnl_dev_init, 1890 .ndo_uninit = ip6_tnl_dev_uninit, 1891 .ndo_start_xmit = ip6_tnl_start_xmit, 1892 .ndo_siocdevprivate = ip6_tnl_siocdevprivate, 1893 .ndo_change_mtu = ip6_tnl_change_mtu, 1894 .ndo_get_stats64 = dev_get_tstats64, 1895 .ndo_get_iflink = ip6_tnl_get_iflink, 1896 .ndo_fill_forward_path = ip6_tnl_fill_forward_path, 1897 }; 1898 1899 #define IPXIPX_FEATURES (NETIF_F_SG | \ 1900 NETIF_F_FRAGLIST | \ 1901 NETIF_F_HIGHDMA | \ 1902 NETIF_F_GSO_SOFTWARE | \ 1903 NETIF_F_HW_CSUM) 1904 1905 /** 1906 * ip6_tnl_dev_setup - setup virtual tunnel device 1907 * @dev: virtual device associated with tunnel 1908 * 1909 * Description: 1910 * Initialize function pointers and device parameters 1911 **/ 1912 1913 static void ip6_tnl_dev_setup(struct net_device *dev) 1914 { 1915 dev->netdev_ops = &ip6_tnl_netdev_ops; 1916 dev->header_ops = &ip_tunnel_header_ops; 1917 dev->needs_free_netdev = true; 1918 dev->priv_destructor = ip6_dev_free; 1919 1920 dev->type = ARPHRD_TUNNEL6; 1921 dev->flags |= IFF_NOARP; 1922 dev->addr_len = sizeof(struct in6_addr); 1923 dev->lltx = true; 1924 dev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS; 1925 netif_keep_dst(dev); 1926 1927 dev->features |= IPXIPX_FEATURES; 1928 dev->hw_features |= IPXIPX_FEATURES; 1929 1930 /* This perm addr will be used as interface identifier by IPv6 */ 1931 dev->addr_assign_type = NET_ADDR_RANDOM; 1932 eth_random_addr(dev->perm_addr); 1933 } 1934 1935 1936 /** 1937 * ip6_tnl_dev_init_gen - general initializer for all tunnel devices 1938 * @dev: virtual device associated with tunnel 1939 **/ 1940 1941 static inline int 1942 ip6_tnl_dev_init_gen(struct net_device *dev) 1943 { 1944 struct ip6_tnl *t = netdev_priv(dev); 1945 int ret; 1946 int t_hlen; 1947 1948 t->dev = dev; 1949 1950 ret = dst_cache_init(&t->dst_cache, GFP_KERNEL); 1951 if (ret) 1952 return ret; 1953 1954 ret = gro_cells_init(&t->gro_cells, dev); 1955 if (ret) 1956 goto destroy_dst; 1957 1958 t->tun_hlen = 0; 1959 t->hlen = t->encap_hlen + t->tun_hlen; 1960 t_hlen = t->hlen + sizeof(struct ipv6hdr); 1961 1962 dev->type = ARPHRD_TUNNEL6; 1963 dev->mtu = ETH_DATA_LEN - t_hlen; 1964 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) 1965 dev->mtu -= 8; 1966 dev->min_mtu = ETH_MIN_MTU; 1967 dev->max_mtu = IP6_MAX_MTU - dev->hard_header_len - t_hlen; 1968 1969 netdev_hold(dev, &t->dev_tracker, GFP_KERNEL); 1970 netdev_lockdep_set_classes(dev); 1971 return 0; 1972 1973 destroy_dst: 1974 dst_cache_destroy(&t->dst_cache); 1975 1976 return ret; 1977 } 1978 1979 /** 1980 * ip6_tnl_dev_init - initializer for all non fallback tunnel devices 1981 * @dev: virtual device associated with tunnel 1982 **/ 1983 1984 static int ip6_tnl_dev_init(struct net_device *dev) 1985 { 1986 struct ip6_tnl *t = netdev_priv(dev); 1987 int err = ip6_tnl_dev_init_gen(dev); 1988 1989 if (err) 1990 return err; 1991 ip6_tnl_link_config(t); 1992 if (t->parms.collect_md) 1993 netif_keep_dst(dev); 1994 return 0; 1995 } 1996 1997 /** 1998 * ip6_fb_tnl_dev_init - initializer for fallback tunnel device 1999 * @dev: fallback device 2000 * 2001 * Return: 0 2002 **/ 2003 2004 static int __net_init ip6_fb_tnl_dev_init(struct net_device *dev) 2005 { 2006 struct ip6_tnl *t = netdev_priv(dev); 2007 struct net *net = dev_net(dev); 2008 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); 2009 2010 t->net = net; 2011 t->parms.proto = IPPROTO_IPV6; 2012 2013 rcu_assign_pointer(ip6n->tnls_wc[0], t); 2014 return 0; 2015 } 2016 2017 static int ip6_tnl_validate(struct nlattr *tb[], struct nlattr *data[], 2018 struct netlink_ext_ack *extack) 2019 { 2020 u8 proto; 2021 2022 if (!data || !data[IFLA_IPTUN_PROTO]) 2023 return 0; 2024 2025 proto = nla_get_u8(data[IFLA_IPTUN_PROTO]); 2026 if (proto != IPPROTO_IPV6 && 2027 proto != IPPROTO_IPIP && 2028 proto != 0) 2029 return -EINVAL; 2030 2031 return 0; 2032 } 2033 2034 static void ip6_tnl_netlink_parms(struct nlattr *data[], 2035 struct __ip6_tnl_parm *parms) 2036 { 2037 memset(parms, 0, sizeof(*parms)); 2038 2039 if (!data) 2040 return; 2041 2042 if (data[IFLA_IPTUN_LINK]) 2043 parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]); 2044 2045 if (data[IFLA_IPTUN_LOCAL]) 2046 parms->laddr = nla_get_in6_addr(data[IFLA_IPTUN_LOCAL]); 2047 2048 if (data[IFLA_IPTUN_REMOTE]) 2049 parms->raddr = nla_get_in6_addr(data[IFLA_IPTUN_REMOTE]); 2050 2051 if (data[IFLA_IPTUN_TTL]) 2052 parms->hop_limit = nla_get_u8(data[IFLA_IPTUN_TTL]); 2053 2054 if (data[IFLA_IPTUN_ENCAP_LIMIT]) 2055 parms->encap_limit = nla_get_u8(data[IFLA_IPTUN_ENCAP_LIMIT]); 2056 2057 if (data[IFLA_IPTUN_FLOWINFO]) 2058 parms->flowinfo = nla_get_be32(data[IFLA_IPTUN_FLOWINFO]); 2059 2060 if (data[IFLA_IPTUN_FLAGS]) 2061 parms->flags = nla_get_u32(data[IFLA_IPTUN_FLAGS]); 2062 2063 if (data[IFLA_IPTUN_PROTO]) 2064 parms->proto = nla_get_u8(data[IFLA_IPTUN_PROTO]); 2065 2066 if (data[IFLA_IPTUN_COLLECT_METADATA]) 2067 parms->collect_md = true; 2068 2069 if (data[IFLA_IPTUN_FWMARK]) 2070 parms->fwmark = nla_get_u32(data[IFLA_IPTUN_FWMARK]); 2071 } 2072 2073 static int ip6_tnl_newlink(struct net_device *dev, 2074 struct rtnl_newlink_params *params, 2075 struct netlink_ext_ack *extack) 2076 { 2077 struct nlattr **data = params->data; 2078 struct nlattr **tb = params->tb; 2079 struct ip_tunnel_encap ipencap; 2080 struct ip6_tnl_net *ip6n; 2081 struct ip6_tnl *nt, *t; 2082 struct net *net; 2083 int err; 2084 2085 net = params->link_net ? : dev_net(dev); 2086 ip6n = net_generic(net, ip6_tnl_net_id); 2087 nt = netdev_priv(dev); 2088 nt->net = net; 2089 2090 if (ip_tunnel_netlink_encap_parms(data, &ipencap)) { 2091 err = ip6_tnl_encap_setup(nt, &ipencap); 2092 if (err < 0) 2093 return err; 2094 } 2095 2096 ip6_tnl_netlink_parms(data, &nt->parms); 2097 2098 if (nt->parms.collect_md) { 2099 if (rtnl_dereference(ip6n->collect_md_tun)) 2100 return -EEXIST; 2101 } else { 2102 t = ip6_tnl_locate(net, &nt->parms, 0); 2103 if (!IS_ERR(t)) 2104 return -EEXIST; 2105 } 2106 2107 err = ip6_tnl_create2(dev); 2108 if (!err && tb[IFLA_MTU]) 2109 ip6_tnl_change_mtu(dev, nla_get_u32(tb[IFLA_MTU])); 2110 2111 return err; 2112 } 2113 2114 static int ip6_tnl_changelink(struct net_device *dev, struct nlattr *tb[], 2115 struct nlattr *data[], 2116 struct netlink_ext_ack *extack) 2117 { 2118 struct ip6_tnl *t = netdev_priv(dev); 2119 struct __ip6_tnl_parm p; 2120 struct net *net = t->net; 2121 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); 2122 struct ip_tunnel_encap ipencap; 2123 2124 if (!rtnl_dev_link_net_capable(dev, net)) 2125 return -EPERM; 2126 2127 if (dev == ip6n->fb_tnl_dev) { 2128 if (ip_tunnel_netlink_encap_parms(data, &ipencap)) { 2129 /* iproute2 always sets TUNNEL_ENCAP_FLAG_CSUM6, so 2130 * let's ignore this flag. 2131 */ 2132 ipencap.flags &= ~TUNNEL_ENCAP_FLAG_CSUM6; 2133 if (memchr_inv(&ipencap, 0, sizeof(ipencap))) { 2134 NL_SET_ERR_MSG(extack, 2135 "Only protocol can be changed for fallback tunnel, not encap params"); 2136 return -EINVAL; 2137 } 2138 } 2139 2140 ip6_tnl_netlink_parms(data, &p); 2141 if (ip6_tnl0_update(t, &p, true) < 0) { 2142 NL_SET_ERR_MSG(extack, 2143 "Only protocol can be changed for fallback tunnel"); 2144 return -EINVAL; 2145 } 2146 2147 return 0; 2148 } 2149 2150 if (ip_tunnel_netlink_encap_parms(data, &ipencap)) { 2151 int err = ip6_tnl_encap_setup(t, &ipencap); 2152 2153 if (err < 0) 2154 return err; 2155 } 2156 ip6_tnl_netlink_parms(data, &p); 2157 if (p.collect_md) 2158 return -EINVAL; 2159 2160 t = ip6_tnl_locate(net, &p, 0); 2161 if (!IS_ERR(t)) { 2162 if (t->dev != dev) 2163 return -EEXIST; 2164 } else 2165 t = netdev_priv(dev); 2166 2167 ip6_tnl_update(t, &p); 2168 return 0; 2169 } 2170 2171 static void ip6_tnl_dellink(struct net_device *dev, struct list_head *head) 2172 { 2173 struct net *net = dev_net(dev); 2174 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); 2175 2176 if (dev != ip6n->fb_tnl_dev) 2177 unregister_netdevice_queue(dev, head); 2178 } 2179 2180 static size_t ip6_tnl_get_size(const struct net_device *dev) 2181 { 2182 return 2183 /* IFLA_IPTUN_LINK */ 2184 nla_total_size(4) + 2185 /* IFLA_IPTUN_LOCAL */ 2186 nla_total_size(sizeof(struct in6_addr)) + 2187 /* IFLA_IPTUN_REMOTE */ 2188 nla_total_size(sizeof(struct in6_addr)) + 2189 /* IFLA_IPTUN_TTL */ 2190 nla_total_size(1) + 2191 /* IFLA_IPTUN_ENCAP_LIMIT */ 2192 nla_total_size(1) + 2193 /* IFLA_IPTUN_FLOWINFO */ 2194 nla_total_size(4) + 2195 /* IFLA_IPTUN_FLAGS */ 2196 nla_total_size(4) + 2197 /* IFLA_IPTUN_PROTO */ 2198 nla_total_size(1) + 2199 /* IFLA_IPTUN_ENCAP_TYPE */ 2200 nla_total_size(2) + 2201 /* IFLA_IPTUN_ENCAP_FLAGS */ 2202 nla_total_size(2) + 2203 /* IFLA_IPTUN_ENCAP_SPORT */ 2204 nla_total_size(2) + 2205 /* IFLA_IPTUN_ENCAP_DPORT */ 2206 nla_total_size(2) + 2207 /* IFLA_IPTUN_COLLECT_METADATA */ 2208 nla_total_size(0) + 2209 /* IFLA_IPTUN_FWMARK */ 2210 nla_total_size(4) + 2211 0; 2212 } 2213 2214 static int ip6_tnl_fill_info(struct sk_buff *skb, const struct net_device *dev) 2215 { 2216 struct ip6_tnl *tunnel = netdev_priv(dev); 2217 struct __ip6_tnl_parm *parm = &tunnel->parms; 2218 2219 if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) || 2220 nla_put_in6_addr(skb, IFLA_IPTUN_LOCAL, &parm->laddr) || 2221 nla_put_in6_addr(skb, IFLA_IPTUN_REMOTE, &parm->raddr) || 2222 nla_put_u8(skb, IFLA_IPTUN_TTL, parm->hop_limit) || 2223 nla_put_u8(skb, IFLA_IPTUN_ENCAP_LIMIT, parm->encap_limit) || 2224 nla_put_be32(skb, IFLA_IPTUN_FLOWINFO, parm->flowinfo) || 2225 nla_put_u32(skb, IFLA_IPTUN_FLAGS, parm->flags) || 2226 nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->proto) || 2227 nla_put_u32(skb, IFLA_IPTUN_FWMARK, parm->fwmark)) 2228 goto nla_put_failure; 2229 2230 if (nla_put_u16(skb, IFLA_IPTUN_ENCAP_TYPE, tunnel->encap.type) || 2231 nla_put_be16(skb, IFLA_IPTUN_ENCAP_SPORT, tunnel->encap.sport) || 2232 nla_put_be16(skb, IFLA_IPTUN_ENCAP_DPORT, tunnel->encap.dport) || 2233 nla_put_u16(skb, IFLA_IPTUN_ENCAP_FLAGS, tunnel->encap.flags)) 2234 goto nla_put_failure; 2235 2236 if (parm->collect_md) 2237 if (nla_put_flag(skb, IFLA_IPTUN_COLLECT_METADATA)) 2238 goto nla_put_failure; 2239 2240 return 0; 2241 2242 nla_put_failure: 2243 return -EMSGSIZE; 2244 } 2245 2246 struct net *ip6_tnl_get_link_net(const struct net_device *dev) 2247 { 2248 struct ip6_tnl *tunnel = netdev_priv(dev); 2249 2250 return READ_ONCE(tunnel->net); 2251 } 2252 EXPORT_SYMBOL(ip6_tnl_get_link_net); 2253 2254 static const struct nla_policy ip6_tnl_policy[IFLA_IPTUN_MAX + 1] = { 2255 [IFLA_IPTUN_LINK] = { .type = NLA_U32 }, 2256 [IFLA_IPTUN_LOCAL] = { .len = sizeof(struct in6_addr) }, 2257 [IFLA_IPTUN_REMOTE] = { .len = sizeof(struct in6_addr) }, 2258 [IFLA_IPTUN_TTL] = { .type = NLA_U8 }, 2259 [IFLA_IPTUN_ENCAP_LIMIT] = { .type = NLA_U8 }, 2260 [IFLA_IPTUN_FLOWINFO] = { .type = NLA_U32 }, 2261 [IFLA_IPTUN_FLAGS] = { .type = NLA_U32 }, 2262 [IFLA_IPTUN_PROTO] = { .type = NLA_U8 }, 2263 [IFLA_IPTUN_ENCAP_TYPE] = { .type = NLA_U16 }, 2264 [IFLA_IPTUN_ENCAP_FLAGS] = { .type = NLA_U16 }, 2265 [IFLA_IPTUN_ENCAP_SPORT] = { .type = NLA_U16 }, 2266 [IFLA_IPTUN_ENCAP_DPORT] = { .type = NLA_U16 }, 2267 [IFLA_IPTUN_COLLECT_METADATA] = { .type = NLA_FLAG }, 2268 [IFLA_IPTUN_FWMARK] = { .type = NLA_U32 }, 2269 }; 2270 2271 static struct rtnl_link_ops ip6_link_ops __read_mostly = { 2272 .kind = "ip6tnl", 2273 .maxtype = IFLA_IPTUN_MAX, 2274 .policy = ip6_tnl_policy, 2275 .priv_size = sizeof(struct ip6_tnl), 2276 .setup = ip6_tnl_dev_setup, 2277 .validate = ip6_tnl_validate, 2278 .newlink = ip6_tnl_newlink, 2279 .changelink = ip6_tnl_changelink, 2280 .dellink = ip6_tnl_dellink, 2281 .get_size = ip6_tnl_get_size, 2282 .fill_info = ip6_tnl_fill_info, 2283 .get_link_net = ip6_tnl_get_link_net, 2284 }; 2285 2286 static struct xfrm6_tunnel ip4ip6_handler __read_mostly = { 2287 .handler = ip4ip6_rcv, 2288 .err_handler = ip4ip6_err, 2289 .priority = 1, 2290 }; 2291 2292 static struct xfrm6_tunnel ip6ip6_handler __read_mostly = { 2293 .handler = ip6ip6_rcv, 2294 .err_handler = ip6ip6_err, 2295 .priority = 1, 2296 }; 2297 2298 static struct xfrm6_tunnel mplsip6_handler __read_mostly = { 2299 .handler = mplsip6_rcv, 2300 .err_handler = mplsip6_err, 2301 .priority = 1, 2302 }; 2303 2304 static void __net_exit ip6_tnl_exit_rtnl_net(struct net *net, struct list_head *list) 2305 { 2306 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); 2307 struct net_device *dev, *aux; 2308 int h; 2309 struct ip6_tnl *t; 2310 2311 for_each_netdev_safe(net, dev, aux) 2312 if (dev->rtnl_link_ops == &ip6_link_ops) 2313 unregister_netdevice_queue(dev, list); 2314 2315 for (h = 0; h < IP6_TUNNEL_HASH_SIZE; h++) { 2316 t = rtnl_net_dereference(net, ip6n->tnls_r_l[h]); 2317 while (t) { 2318 /* If dev is in the same netns, it has already 2319 * been added to the list by the previous loop. 2320 */ 2321 if (!net_eq(dev_net(t->dev), net)) 2322 unregister_netdevice_queue(t->dev, list); 2323 2324 t = rtnl_net_dereference(net, t->next); 2325 } 2326 } 2327 2328 t = rtnl_net_dereference(net, ip6n->tnls_wc[0]); 2329 while (t) { 2330 /* If dev is in the same netns, it has already 2331 * been added to the list by the previous loop. 2332 */ 2333 if (!net_eq(dev_net(t->dev), net)) 2334 unregister_netdevice_queue(t->dev, list); 2335 2336 t = rtnl_net_dereference(net, t->next); 2337 } 2338 } 2339 2340 static int __net_init ip6_tnl_init_net(struct net *net) 2341 { 2342 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); 2343 struct ip6_tnl *t = NULL; 2344 int err; 2345 2346 ip6n->tnls[0] = ip6n->tnls_wc; 2347 ip6n->tnls[1] = ip6n->tnls_r_l; 2348 2349 if (!net_has_fallback_tunnels(net)) 2350 return 0; 2351 err = -ENOMEM; 2352 ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0", 2353 NET_NAME_UNKNOWN, ip6_tnl_dev_setup); 2354 2355 if (!ip6n->fb_tnl_dev) 2356 goto err_alloc_dev; 2357 dev_net_set(ip6n->fb_tnl_dev, net); 2358 ip6n->fb_tnl_dev->rtnl_link_ops = &ip6_link_ops; 2359 /* FB netdevice is special: we have one, and only one per netns. 2360 * Allowing to move it to another netns is clearly unsafe. 2361 */ 2362 ip6n->fb_tnl_dev->netns_immutable = true; 2363 2364 err = ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev); 2365 if (err < 0) 2366 goto err_register; 2367 2368 err = register_netdev(ip6n->fb_tnl_dev); 2369 if (err < 0) 2370 goto err_register; 2371 2372 t = netdev_priv(ip6n->fb_tnl_dev); 2373 2374 strcpy(t->parms.name, ip6n->fb_tnl_dev->name); 2375 return 0; 2376 2377 err_register: 2378 free_netdev(ip6n->fb_tnl_dev); 2379 err_alloc_dev: 2380 return err; 2381 } 2382 2383 static struct pernet_operations ip6_tnl_net_ops = { 2384 .init = ip6_tnl_init_net, 2385 .exit_rtnl = ip6_tnl_exit_rtnl_net, 2386 .id = &ip6_tnl_net_id, 2387 .size = sizeof(struct ip6_tnl_net), 2388 }; 2389 2390 /** 2391 * ip6_tunnel_init - register protocol and reserve needed resources 2392 * 2393 * Return: 0 on success 2394 **/ 2395 2396 static int __init ip6_tunnel_init(void) 2397 { 2398 int err; 2399 2400 if (!ipv6_mod_enabled()) 2401 return -EOPNOTSUPP; 2402 2403 err = register_pernet_device(&ip6_tnl_net_ops); 2404 if (err < 0) 2405 goto out_pernet; 2406 2407 err = xfrm6_tunnel_register(&ip4ip6_handler, AF_INET); 2408 if (err < 0) { 2409 pr_err("%s: can't register ip4ip6\n", __func__); 2410 goto out_ip4ip6; 2411 } 2412 2413 err = xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6); 2414 if (err < 0) { 2415 pr_err("%s: can't register ip6ip6\n", __func__); 2416 goto out_ip6ip6; 2417 } 2418 2419 if (ip6_tnl_mpls_supported()) { 2420 err = xfrm6_tunnel_register(&mplsip6_handler, AF_MPLS); 2421 if (err < 0) { 2422 pr_err("%s: can't register mplsip6\n", __func__); 2423 goto out_mplsip6; 2424 } 2425 } 2426 2427 err = rtnl_link_register(&ip6_link_ops); 2428 if (err < 0) 2429 goto rtnl_link_failed; 2430 2431 return 0; 2432 2433 rtnl_link_failed: 2434 if (ip6_tnl_mpls_supported()) 2435 xfrm6_tunnel_deregister(&mplsip6_handler, AF_MPLS); 2436 out_mplsip6: 2437 xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6); 2438 out_ip6ip6: 2439 xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET); 2440 out_ip4ip6: 2441 unregister_pernet_device(&ip6_tnl_net_ops); 2442 out_pernet: 2443 return err; 2444 } 2445 2446 /** 2447 * ip6_tunnel_cleanup - free resources and unregister protocol 2448 **/ 2449 2450 static void __exit ip6_tunnel_cleanup(void) 2451 { 2452 rtnl_link_unregister(&ip6_link_ops); 2453 if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET)) 2454 pr_info("%s: can't deregister ip4ip6\n", __func__); 2455 2456 if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6)) 2457 pr_info("%s: can't deregister ip6ip6\n", __func__); 2458 2459 if (ip6_tnl_mpls_supported() && 2460 xfrm6_tunnel_deregister(&mplsip6_handler, AF_MPLS)) 2461 pr_info("%s: can't deregister mplsip6\n", __func__); 2462 unregister_pernet_device(&ip6_tnl_net_ops); 2463 } 2464 2465 module_init(ip6_tunnel_init); 2466 module_exit(ip6_tunnel_cleanup); 2467