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