1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * IPv6 over IPv4 tunnel device - Simple Internet Transition (SIT) 4 * Linux INET6 implementation 5 * 6 * Authors: 7 * Pedro Roque <roque@di.fc.ul.pt> 8 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> 9 * 10 * Changes: 11 * Roger Venning <r.venning@telstra.com>: 6to4 support 12 * Nate Thompson <nate@thebog.net>: 6to4 support 13 * Fred Templin <fred.l.templin@boeing.com>: isatap support 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/socket.h> 23 #include <linux/sockios.h> 24 #include <linux/string.h> 25 #include <linux/net.h> 26 #include <linux/in6.h> 27 #include <linux/netdevice.h> 28 #include <linux/if_arp.h> 29 #include <linux/icmp.h> 30 #include <linux/slab.h> 31 #include <linux/uaccess.h> 32 #include <linux/init.h> 33 #include <linux/netfilter_ipv4.h> 34 #include <linux/if_ether.h> 35 36 #include <net/sock.h> 37 #include <net/snmp.h> 38 39 #include <net/ipv6.h> 40 #include <net/protocol.h> 41 #include <net/transp_v6.h> 42 #include <net/ip6_fib.h> 43 #include <net/ip6_route.h> 44 #include <net/ndisc.h> 45 #include <net/addrconf.h> 46 #include <net/ip.h> 47 #include <net/udp.h> 48 #include <net/icmp.h> 49 #include <net/ip_tunnels.h> 50 #include <net/inet_ecn.h> 51 #include <net/xfrm.h> 52 #include <net/dsfield.h> 53 #include <net/net_namespace.h> 54 #include <net/netns/generic.h> 55 #include <net/netdev_lock.h> 56 #include <net/inet_dscp.h> 57 58 /* 59 This version of net/ipv6/sit.c is cloned of net/ipv4/ip_gre.c 60 61 For comments look at net/ipv4/ip_gre.c --ANK 62 */ 63 64 #define IP6_SIT_HASH_SIZE 16 65 #define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&0xF) 66 67 static bool log_ecn_error = true; 68 module_param(log_ecn_error, bool, 0644); 69 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN"); 70 71 static int ipip6_tunnel_init(struct net_device *dev); 72 static void ipip6_tunnel_setup(struct net_device *dev); 73 static void ipip6_dev_free(struct net_device *dev); 74 static bool check_6rd(struct ip_tunnel *tunnel, const struct in6_addr *v6dst, 75 __be32 *v4dst); 76 static struct rtnl_link_ops sit_link_ops __read_mostly; 77 78 static unsigned int sit_net_id __read_mostly; 79 struct sit_net { 80 struct ip_tunnel __rcu *tunnels_r_l[IP6_SIT_HASH_SIZE]; 81 struct ip_tunnel __rcu *tunnels_r[IP6_SIT_HASH_SIZE]; 82 struct ip_tunnel __rcu *tunnels_l[IP6_SIT_HASH_SIZE]; 83 struct ip_tunnel __rcu *tunnels_wc[1]; 84 struct ip_tunnel __rcu **tunnels[4]; 85 86 struct net_device *fb_tunnel_dev; 87 }; 88 89 static inline struct sit_net *dev_to_sit_net(struct net_device *dev) 90 { 91 struct ip_tunnel *t = netdev_priv(dev); 92 93 return net_generic(t->net, sit_net_id); 94 } 95 96 /* 97 * Must be invoked with rcu_read_lock 98 */ 99 static struct ip_tunnel *ipip6_tunnel_lookup(struct net *net, 100 struct net_device *dev, 101 __be32 remote, __be32 local, 102 int sifindex) 103 { 104 unsigned int h0 = HASH(remote); 105 unsigned int h1 = HASH(local); 106 struct ip_tunnel *t; 107 struct sit_net *sitn = net_generic(net, sit_net_id); 108 int ifindex = dev ? dev->ifindex : 0; 109 110 for_each_ip_tunnel_rcu(t, sitn->tunnels_r_l[h0 ^ h1]) { 111 const struct ip_tunnel_parm_kern *parms; 112 113 parms = rcu_dereference(t->sit_parms); 114 if (local == parms->iph.saddr && 115 remote == parms->iph.daddr && 116 (!dev || !parms->link || ifindex == parms->link || 117 sifindex == parms->link) && 118 (t->dev->flags & IFF_UP)) 119 return t; 120 } 121 for_each_ip_tunnel_rcu(t, sitn->tunnels_r[h0]) { 122 const struct ip_tunnel_parm_kern *parms; 123 124 parms = rcu_dereference(t->sit_parms); 125 if (remote == parms->iph.daddr && 126 (!dev || !parms->link || ifindex == parms->link || 127 sifindex == parms->link) && 128 (t->dev->flags & IFF_UP)) 129 return t; 130 } 131 for_each_ip_tunnel_rcu(t, sitn->tunnels_l[h1]) { 132 const struct ip_tunnel_parm_kern *parms; 133 134 parms = rcu_dereference(t->sit_parms); 135 if (local == parms->iph.saddr && 136 (!dev || !parms->link || ifindex == parms->link || 137 sifindex == parms->link) && 138 (t->dev->flags & IFF_UP)) 139 return t; 140 } 141 t = rcu_dereference(sitn->tunnels_wc[0]); 142 if (t && (t->dev->flags & IFF_UP)) 143 return t; 144 return NULL; 145 } 146 147 static struct ip_tunnel __rcu ** 148 __ipip6_bucket(struct sit_net *sitn, struct ip_tunnel_parm_kern *parms) 149 { 150 __be32 remote = parms->iph.daddr; 151 __be32 local = parms->iph.saddr; 152 unsigned int h = 0; 153 int prio = 0; 154 155 if (remote) { 156 prio |= 2; 157 h ^= HASH(remote); 158 } 159 if (local) { 160 prio |= 1; 161 h ^= HASH(local); 162 } 163 return &sitn->tunnels[prio][h]; 164 } 165 166 static inline struct ip_tunnel __rcu **ipip6_bucket(struct sit_net *sitn, 167 struct ip_tunnel *t) 168 { 169 return __ipip6_bucket(sitn, rtnl_dereference(t->sit_parms)); 170 } 171 172 static void ipip6_tunnel_unlink(struct sit_net *sitn, struct ip_tunnel *t) 173 { 174 struct ip_tunnel __rcu **tp; 175 struct ip_tunnel *iter; 176 177 for (tp = ipip6_bucket(sitn, t); 178 (iter = rtnl_dereference(*tp)) != NULL; 179 tp = &iter->next) { 180 if (t == iter) { 181 rcu_assign_pointer(*tp, t->next); 182 break; 183 } 184 } 185 } 186 187 static void ipip6_tunnel_link(struct sit_net *sitn, struct ip_tunnel *t) 188 { 189 struct ip_tunnel __rcu **tp = ipip6_bucket(sitn, t); 190 191 rcu_assign_pointer(t->next, rtnl_dereference(*tp)); 192 rcu_assign_pointer(*tp, t); 193 } 194 195 static int ipip6_tunnel_clone_6rd(struct net_device *dev, struct sit_net *sitn) 196 { 197 #ifdef CONFIG_IPV6_SIT_6RD 198 struct ip_tunnel *t = netdev_priv(dev); 199 struct ip_tunnel_6rd_parm *new_6rd, *old_6rd; 200 201 new_6rd = kmalloc_obj(*new_6rd); 202 if (!new_6rd) 203 return -ENOMEM; 204 205 if (dev == sitn->fb_tunnel_dev || !sitn->fb_tunnel_dev) { 206 ipv6_addr_set(&new_6rd->prefix, htonl(0x20020000), 0, 0, 0); 207 new_6rd->relay_prefix = 0; 208 new_6rd->prefixlen = 16; 209 new_6rd->relay_prefixlen = 0; 210 } else { 211 struct ip_tunnel *t0 = netdev_priv(sitn->fb_tunnel_dev); 212 struct ip_tunnel_6rd_parm *t0_6rd; 213 214 t0_6rd = rtnl_dereference(t0->ip6rd); 215 if (t0_6rd) { 216 *new_6rd = *t0_6rd; 217 } else { 218 ipv6_addr_set(&new_6rd->prefix, htonl(0x20020000), 0, 0, 0); 219 new_6rd->relay_prefix = 0; 220 new_6rd->prefixlen = 16; 221 new_6rd->relay_prefixlen = 0; 222 } 223 } 224 225 old_6rd = rcu_dereference_protected(t->ip6rd, 226 lockdep_rtnl_is_held() || 227 dev->reg_state == NETREG_UNINITIALIZED); 228 rcu_assign_pointer(t->ip6rd, new_6rd); 229 if (old_6rd) { 230 dst_cache_reset(&t->dst_cache); 231 netdev_state_change(t->dev); 232 kfree_rcu(old_6rd, rcu); 233 } 234 #endif 235 return 0; 236 } 237 238 static int ipip6_tunnel_create(struct net_device *dev) 239 { 240 struct ip_tunnel *t = netdev_priv(dev); 241 struct sit_net *sitn = net_generic(t->net, sit_net_id); 242 struct ip_tunnel_parm_kern *parms; 243 int err; 244 245 err = ipip6_tunnel_clone_6rd(dev, sitn); 246 if (err < 0) 247 goto out; 248 249 parms = rtnl_dereference(t->sit_parms); 250 t->parms = *parms; 251 __dev_addr_set(dev, &parms->iph.saddr, 4); 252 memcpy(dev->broadcast, &parms->iph.daddr, 4); 253 254 if (test_bit(IP_TUNNEL_SIT_ISATAP_BIT, parms->i_flags)) 255 dev->priv_flags |= IFF_ISATAP; 256 257 dev->rtnl_link_ops = &sit_link_ops; 258 259 err = register_netdevice(dev); 260 if (err < 0) 261 goto out; 262 263 ipip6_tunnel_link(sitn, t); 264 return 0; 265 266 out: 267 return err; 268 } 269 270 static struct ip_tunnel *ipip6_tunnel_locate(struct net *net, 271 struct ip_tunnel_parm_kern *parms, 272 int create) 273 { 274 __be32 remote = parms->iph.daddr; 275 __be32 local = parms->iph.saddr; 276 struct ip_tunnel *t, *nt; 277 struct ip_tunnel __rcu **tp; 278 struct ip_tunnel_parm_kern *nt_parms; 279 struct net_device *dev; 280 char name[IFNAMSIZ]; 281 struct sit_net *sitn = net_generic(net, sit_net_id); 282 283 for (tp = __ipip6_bucket(sitn, parms); 284 (t = rtnl_dereference(*tp)) != NULL; 285 tp = &t->next) { 286 const struct ip_tunnel_parm_kern *tparms; 287 288 tparms = rtnl_dereference(t->sit_parms); 289 if (local == tparms->iph.saddr && 290 remote == tparms->iph.daddr && 291 parms->link == tparms->link) { 292 if (create) 293 return NULL; 294 else 295 return t; 296 } 297 } 298 if (!create) 299 goto failed; 300 301 if (parms->name[0]) { 302 if (!dev_valid_name(parms->name)) 303 goto failed; 304 strscpy(name, parms->name); 305 } else { 306 strscpy(name, "sit%d"); 307 } 308 dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN, 309 ipip6_tunnel_setup); 310 if (!dev) 311 return NULL; 312 313 dev_net_set(dev, net); 314 315 nt = netdev_priv(dev); 316 317 nt->net = net; 318 nt_parms = kmalloc_obj(*nt_parms); 319 if (!nt_parms) 320 goto failed_free; 321 *nt_parms = *parms; 322 rcu_assign_pointer(nt->sit_parms, nt_parms); 323 if (ipip6_tunnel_create(dev) < 0) 324 goto failed_free; 325 326 if (!parms->name[0]) 327 strscpy(parms->name, dev->name); 328 329 return nt; 330 331 failed_free: 332 ipip6_dev_free(dev); 333 free_netdev(dev); 334 failed: 335 return NULL; 336 } 337 338 #define for_each_prl_rcu(start) \ 339 for (prl = rcu_dereference(start); \ 340 prl; \ 341 prl = rcu_dereference(prl->next)) 342 343 static struct ip_tunnel_prl_entry * 344 __ipip6_tunnel_locate_prl(struct ip_tunnel *t, __be32 addr) 345 { 346 struct ip_tunnel_prl_entry *prl; 347 348 for_each_prl_rcu(t->prl) 349 if (prl->addr == addr) 350 break; 351 return prl; 352 353 } 354 355 static int ipip6_tunnel_get_prl(struct net_device *dev, struct ip_tunnel_prl __user *a) 356 { 357 struct ip_tunnel *t = netdev_priv(dev); 358 struct ip_tunnel_prl kprl, *kp; 359 struct ip_tunnel_prl_entry *prl; 360 unsigned int cmax, c = 0, ca, len; 361 int ret; 362 363 if (dev == dev_to_sit_net(dev)->fb_tunnel_dev) 364 return -EINVAL; 365 366 if (copy_from_user(&kprl, a, sizeof(kprl))) 367 return -EFAULT; 368 cmax = kprl.datalen / sizeof(kprl); 369 if (cmax > 1 && kprl.addr != htonl(INADDR_ANY)) 370 cmax = 1; 371 372 /* For simple GET or for root users, 373 * we try harder to allocate. 374 */ 375 kp = (cmax <= 1 || capable(CAP_NET_ADMIN)) ? 376 kzalloc_objs(*kp, cmax, GFP_KERNEL_ACCOUNT | __GFP_NOWARN) : 377 NULL; 378 379 ca = min(t->prl_count, cmax); 380 381 if (!kp) { 382 /* We don't try hard to allocate much memory for 383 * non-root users. 384 * For root users, retry allocating enough memory for 385 * the answer. 386 */ 387 kp = kzalloc_objs(*kp, ca, 388 GFP_ATOMIC | __GFP_ACCOUNT | __GFP_NOWARN); 389 if (!kp) { 390 ret = -ENOMEM; 391 goto out; 392 } 393 } 394 395 rcu_read_lock(); 396 for_each_prl_rcu(t->prl) { 397 if (c >= cmax) 398 break; 399 if (kprl.addr != htonl(INADDR_ANY) && prl->addr != kprl.addr) 400 continue; 401 kp[c].addr = prl->addr; 402 kp[c].flags = prl->flags; 403 c++; 404 if (kprl.addr != htonl(INADDR_ANY)) 405 break; 406 } 407 408 rcu_read_unlock(); 409 410 len = sizeof(*kp) * c; 411 ret = 0; 412 if ((len && copy_to_user(a + 1, kp, len)) || put_user(len, &a->datalen)) 413 ret = -EFAULT; 414 415 kfree(kp); 416 out: 417 return ret; 418 } 419 420 static int 421 ipip6_tunnel_add_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a, int chg) 422 { 423 struct ip_tunnel_prl_entry *p; 424 int err = 0; 425 426 if (a->addr == htonl(INADDR_ANY)) 427 return -EINVAL; 428 429 ASSERT_RTNL(); 430 431 for (p = rtnl_dereference(t->prl); p; p = rtnl_dereference(p->next)) { 432 if (p->addr == a->addr) { 433 if (chg) { 434 p->flags = a->flags; 435 goto out; 436 } 437 err = -EEXIST; 438 goto out; 439 } 440 } 441 442 if (chg) { 443 err = -ENXIO; 444 goto out; 445 } 446 447 p = kzalloc_obj(struct ip_tunnel_prl_entry, GFP_KERNEL_ACCOUNT); 448 if (!p) { 449 err = -ENOBUFS; 450 goto out; 451 } 452 453 p->next = t->prl; 454 p->addr = a->addr; 455 p->flags = a->flags; 456 t->prl_count++; 457 rcu_assign_pointer(t->prl, p); 458 out: 459 return err; 460 } 461 462 static void prl_list_destroy_rcu(struct rcu_head *head) 463 { 464 struct ip_tunnel_prl_entry *p, *n; 465 466 p = container_of(head, struct ip_tunnel_prl_entry, rcu_head); 467 do { 468 n = rcu_dereference_protected(p->next, 1); 469 kfree(p); 470 p = n; 471 } while (p); 472 } 473 474 static int 475 ipip6_tunnel_del_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a) 476 { 477 struct ip_tunnel_prl_entry *x; 478 struct ip_tunnel_prl_entry __rcu **p; 479 int err = 0; 480 481 ASSERT_RTNL(); 482 483 if (a && a->addr != htonl(INADDR_ANY)) { 484 for (p = &t->prl; 485 (x = rtnl_dereference(*p)) != NULL; 486 p = &x->next) { 487 if (x->addr == a->addr) { 488 rcu_assign_pointer(*p, rtnl_dereference(x->next)); 489 kfree_rcu(x, rcu_head); 490 t->prl_count--; 491 goto out; 492 } 493 } 494 err = -ENXIO; 495 } else { 496 x = rtnl_dereference(t->prl); 497 if (x) { 498 t->prl_count = 0; 499 RCU_INIT_POINTER(t->prl, NULL); 500 call_rcu(&x->rcu_head, prl_list_destroy_rcu); 501 } 502 } 503 out: 504 return err; 505 } 506 507 static int ipip6_tunnel_prl_ctl(struct net_device *dev, 508 struct ip_tunnel_prl __user *data, int cmd) 509 { 510 struct ip_tunnel *t = netdev_priv(dev); 511 struct ip_tunnel_prl prl; 512 int err; 513 514 if (!ns_capable(t->net->user_ns, CAP_NET_ADMIN)) 515 return -EPERM; 516 if (dev == dev_to_sit_net(dev)->fb_tunnel_dev) 517 return -EINVAL; 518 519 if (copy_from_user(&prl, data, sizeof(prl))) 520 return -EFAULT; 521 522 switch (cmd) { 523 case SIOCDELPRL: 524 err = ipip6_tunnel_del_prl(t, &prl); 525 break; 526 case SIOCADDPRL: 527 case SIOCCHGPRL: 528 err = ipip6_tunnel_add_prl(t, &prl, cmd == SIOCCHGPRL); 529 break; 530 } 531 dst_cache_reset(&t->dst_cache); 532 netdev_state_change(dev); 533 return err; 534 } 535 536 static int 537 isatap_chksrc(struct sk_buff *skb, const struct iphdr *iph, struct ip_tunnel *t) 538 { 539 struct ip_tunnel_prl_entry *p; 540 int ok = 1; 541 542 rcu_read_lock(); 543 p = __ipip6_tunnel_locate_prl(t, iph->saddr); 544 if (p) { 545 if (p->flags & PRL_DEFAULT) 546 skb->ndisc_nodetype = NDISC_NODETYPE_DEFAULT; 547 else 548 skb->ndisc_nodetype = NDISC_NODETYPE_NODEFAULT; 549 } else { 550 const struct in6_addr *addr6 = &ipv6_hdr(skb)->saddr; 551 552 if (ipv6_addr_is_isatap(addr6) && 553 (addr6->s6_addr32[3] == iph->saddr) && 554 ipv6_chk_prefix(addr6, t->dev)) 555 skb->ndisc_nodetype = NDISC_NODETYPE_HOST; 556 else 557 ok = 0; 558 } 559 rcu_read_unlock(); 560 return ok; 561 } 562 563 static void ipip6_tunnel_uninit(struct net_device *dev) 564 { 565 struct ip_tunnel *tunnel = netdev_priv(dev); 566 struct sit_net *sitn = net_generic(tunnel->net, sit_net_id); 567 568 if (dev == sitn->fb_tunnel_dev) { 569 RCU_INIT_POINTER(sitn->tunnels_wc[0], NULL); 570 } else { 571 ipip6_tunnel_unlink(sitn, tunnel); 572 ipip6_tunnel_del_prl(tunnel, NULL); 573 } 574 dst_cache_reset(&tunnel->dst_cache); 575 netdev_put(dev, &tunnel->dev_tracker); 576 } 577 578 static int ipip6_err(struct sk_buff *skb, u32 info) 579 { 580 const struct iphdr *iph = (const struct iphdr *)skb->data; 581 const int type = icmp_hdr(skb)->type; 582 const int code = icmp_hdr(skb)->code; 583 unsigned int data_len = 0; 584 struct ip_tunnel *t; 585 int sifindex; 586 int err; 587 588 switch (type) { 589 default: 590 case ICMP_PARAMETERPROB: 591 return 0; 592 593 case ICMP_DEST_UNREACH: 594 switch (code) { 595 case ICMP_SR_FAILED: 596 /* Impossible event. */ 597 return 0; 598 default: 599 /* All others are translated to HOST_UNREACH. 600 rfc2003 contains "deep thoughts" about NET_UNREACH, 601 I believe they are just ether pollution. --ANK 602 */ 603 break; 604 } 605 break; 606 case ICMP_TIME_EXCEEDED: 607 if (code != ICMP_EXC_TTL) 608 return 0; 609 data_len = icmp_hdr(skb)->un.reserved[1] * 4; /* RFC 4884 4.1 */ 610 break; 611 case ICMP_REDIRECT: 612 break; 613 } 614 615 err = -ENOENT; 616 617 sifindex = netif_is_l3_master(skb->dev) ? IPCB(skb)->iif : 0; 618 rcu_read_lock(); 619 t = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev, 620 iph->daddr, iph->saddr, sifindex); 621 if (t) { 622 const struct ip_tunnel_parm_kern *parms; 623 624 parms = rcu_dereference(t->sit_parms); 625 if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) { 626 ipv4_update_pmtu(skb, dev_net(skb->dev), info, 627 parms->link, iph->protocol); 628 err = 0; 629 goto out; 630 } 631 if (type == ICMP_REDIRECT) { 632 ipv4_redirect(skb, dev_net(skb->dev), parms->link, 633 iph->protocol); 634 err = 0; 635 goto out; 636 } 637 638 err = 0; 639 if (__in6_dev_get(skb->dev) && 640 !ip6_err_gen_icmpv6_unreach(skb, iph->ihl * 4, type, data_len)) 641 goto out; 642 643 if (parms->iph.daddr == 0) 644 goto out; 645 646 if (parms->iph.ttl == 0 && type == ICMP_TIME_EXCEEDED) 647 goto out; 648 649 if (time_before(jiffies, READ_ONCE(t->err_time) + IPTUNNEL_ERR_TIMEO)) 650 WRITE_ONCE(t->err_count, READ_ONCE(t->err_count) + 1); 651 else 652 WRITE_ONCE(t->err_count, 1); 653 WRITE_ONCE(t->err_time, jiffies); 654 } 655 out: 656 rcu_read_unlock(); 657 return err; 658 } 659 660 static inline bool is_spoofed_6rd(struct ip_tunnel *tunnel, const __be32 v4addr, 661 const struct in6_addr *v6addr) 662 { 663 __be32 v4embed = 0; 664 if (check_6rd(tunnel, v6addr, &v4embed) && v4addr != v4embed) 665 return true; 666 return false; 667 } 668 669 /* Checks if an address matches an address on the tunnel interface. 670 * Used to detect the NAT of proto 41 packets and let them pass spoofing test. 671 * Long story: 672 * This function is called after we considered the packet as spoofed 673 * in is_spoofed_6rd. 674 * We may have a router that is doing NAT for proto 41 packets 675 * for an internal station. Destination a.a.a.a/PREFIX:bbbb:bbbb 676 * will be translated to n.n.n.n/PREFIX:bbbb:bbbb. And is_spoofed_6rd 677 * function will return true, dropping the packet. 678 * But, we can still check if is spoofed against the IP 679 * addresses associated with the interface. 680 */ 681 static bool only_dnatted(const struct ip_tunnel *tunnel, 682 const struct in6_addr *v6dst) 683 { 684 int prefix_len; 685 686 #ifdef CONFIG_IPV6_SIT_6RD 687 const struct ip_tunnel_6rd_parm *ip6rd; 688 689 ip6rd = rcu_dereference(tunnel->ip6rd); 690 if (ip6rd) 691 prefix_len = ip6rd->prefixlen + 32 - ip6rd->relay_prefixlen; 692 else 693 prefix_len = 48; 694 #else 695 prefix_len = 48; 696 #endif 697 return ipv6_chk_custom_prefix(v6dst, prefix_len, tunnel->dev); 698 } 699 700 /* Returns true if a packet is spoofed */ 701 static bool packet_is_spoofed(struct sk_buff *skb, 702 const struct iphdr *iph, 703 struct ip_tunnel *tunnel) 704 { 705 const struct ipv6hdr *ipv6h; 706 707 if (tunnel->dev->priv_flags & IFF_ISATAP) { 708 if (!isatap_chksrc(skb, iph, tunnel)) 709 return true; 710 711 return false; 712 } 713 714 if (tunnel->dev->flags & IFF_POINTOPOINT) 715 return false; 716 717 ipv6h = ipv6_hdr(skb); 718 719 if (unlikely(is_spoofed_6rd(tunnel, iph->saddr, &ipv6h->saddr))) { 720 net_warn_ratelimited("Src spoofed %pI4/%pI6c -> %pI4/%pI6c\n", 721 &iph->saddr, &ipv6h->saddr, 722 &iph->daddr, &ipv6h->daddr); 723 return true; 724 } 725 726 if (likely(!is_spoofed_6rd(tunnel, iph->daddr, &ipv6h->daddr))) 727 return false; 728 729 if (only_dnatted(tunnel, &ipv6h->daddr)) 730 return false; 731 732 net_warn_ratelimited("Dst spoofed %pI4/%pI6c -> %pI4/%pI6c\n", 733 &iph->saddr, &ipv6h->saddr, 734 &iph->daddr, &ipv6h->daddr); 735 return true; 736 } 737 738 static int ipip6_rcv(struct sk_buff *skb) 739 { 740 const struct iphdr *iph = ip_hdr(skb); 741 struct ip_tunnel *tunnel; 742 int sifindex; 743 int err; 744 745 sifindex = netif_is_l3_master(skb->dev) ? IPCB(skb)->iif : 0; 746 tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev, 747 iph->saddr, iph->daddr, sifindex); 748 if (tunnel) { 749 const struct ip_tunnel_parm_kern *parms; 750 751 parms = rcu_dereference(tunnel->sit_parms); 752 if (parms->iph.protocol != IPPROTO_IPV6 && 753 parms->iph.protocol != 0) 754 goto out; 755 756 skb->mac_header = skb->network_header; 757 skb_reset_network_header(skb); 758 IPCB(skb)->flags = 0; 759 skb->dev = tunnel->dev; 760 761 if (packet_is_spoofed(skb, iph, tunnel)) { 762 DEV_STATS_INC(tunnel->dev, rx_errors); 763 goto out; 764 } 765 766 if (iptunnel_pull_header(skb, 0, htons(ETH_P_IPV6), 767 !net_eq(tunnel->net, dev_net(tunnel->dev)))) 768 goto out; 769 770 /* skb can be uncloned in iptunnel_pull_header, so 771 * old iph is no longer valid 772 */ 773 iph = (const struct iphdr *)skb_mac_header(skb); 774 skb_reset_mac_header(skb); 775 776 err = IP_ECN_decapsulate(iph, skb); 777 if (unlikely(err)) { 778 if (log_ecn_error) 779 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n", 780 &iph->saddr, iph->tos); 781 if (err > 1) { 782 DEV_STATS_INC(tunnel->dev, rx_frame_errors); 783 DEV_STATS_INC(tunnel->dev, rx_errors); 784 goto out; 785 } 786 } 787 788 dev_sw_netstats_rx_add(tunnel->dev, skb->len); 789 790 netif_rx(skb); 791 792 return 0; 793 } 794 795 /* no tunnel matched, let upstream know, ipsec may handle it */ 796 return 1; 797 out: 798 kfree_skb(skb); 799 return 0; 800 } 801 802 static const struct tnl_ptk_info ipip_tpi = { 803 /* no tunnel info required for ipip. */ 804 .proto = htons(ETH_P_IP), 805 }; 806 807 #if IS_ENABLED(CONFIG_MPLS) 808 static const struct tnl_ptk_info mplsip_tpi = { 809 /* no tunnel info required for mplsip. */ 810 .proto = htons(ETH_P_MPLS_UC), 811 }; 812 #endif 813 814 static int sit_tunnel_rcv(struct sk_buff *skb, u8 ipproto) 815 { 816 const struct iphdr *iph; 817 struct ip_tunnel *tunnel; 818 int sifindex; 819 820 sifindex = netif_is_l3_master(skb->dev) ? IPCB(skb)->iif : 0; 821 822 iph = ip_hdr(skb); 823 tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev, 824 iph->saddr, iph->daddr, sifindex); 825 if (tunnel) { 826 const struct ip_tunnel_parm_kern *parms; 827 const struct tnl_ptk_info *tpi; 828 829 parms = rcu_dereference(tunnel->sit_parms); 830 if (parms->iph.protocol != ipproto && 831 parms->iph.protocol != 0) 832 goto drop; 833 834 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) 835 goto drop; 836 #if IS_ENABLED(CONFIG_MPLS) 837 if (ipproto == IPPROTO_MPLS) 838 tpi = &mplsip_tpi; 839 else 840 #endif 841 tpi = &ipip_tpi; 842 if (iptunnel_pull_header(skb, 0, tpi->proto, false)) 843 goto drop; 844 skb_reset_mac_header(skb); 845 846 return ip_tunnel_rcv(tunnel, skb, tpi, NULL, log_ecn_error); 847 } 848 849 return 1; 850 851 drop: 852 kfree_skb(skb); 853 return 0; 854 } 855 856 static int ipip_rcv(struct sk_buff *skb) 857 { 858 return sit_tunnel_rcv(skb, IPPROTO_IPIP); 859 } 860 861 #if IS_ENABLED(CONFIG_MPLS) 862 static int mplsip_rcv(struct sk_buff *skb) 863 { 864 return sit_tunnel_rcv(skb, IPPROTO_MPLS); 865 } 866 #endif 867 868 /* 869 * If the IPv6 address comes from 6rd / 6to4 (RFC 3056) addr space this function 870 * stores the embedded IPv4 address in v4dst and returns true. 871 */ 872 static bool check_6rd(struct ip_tunnel *tunnel, const struct in6_addr *v6dst, 873 __be32 *v4dst) 874 { 875 #ifdef CONFIG_IPV6_SIT_6RD 876 const struct ip_tunnel_6rd_parm *ip6rd; 877 878 ip6rd = rcu_dereference(tunnel->ip6rd); 879 if (ip6rd && ipv6_prefix_equal(v6dst, &ip6rd->prefix, 880 ip6rd->prefixlen)) { 881 unsigned int pbw0, pbi0; 882 int pbi1; 883 u32 d; 884 885 pbw0 = ip6rd->prefixlen >> 5; 886 pbi0 = ip6rd->prefixlen & 0x1f; 887 888 d = ip6rd->relay_prefixlen < 32 ? 889 (ntohl(v6dst->s6_addr32[pbw0]) << pbi0) >> 890 ip6rd->relay_prefixlen : 0; 891 892 pbi1 = pbi0 - ip6rd->relay_prefixlen; 893 if (pbi1 > 0) 894 d |= ntohl(v6dst->s6_addr32[pbw0 + 1]) >> 895 (32 - pbi1); 896 897 *v4dst = ip6rd->relay_prefix | htonl(d); 898 return true; 899 } 900 #else 901 if (v6dst->s6_addr16[0] == htons(0x2002)) { 902 /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */ 903 memcpy(v4dst, &v6dst->s6_addr16[1], 4); 904 return true; 905 } 906 #endif 907 return false; 908 } 909 910 static inline __be32 try_6rd(struct ip_tunnel *tunnel, 911 const struct in6_addr *v6dst) 912 { 913 __be32 dst = 0; 914 check_6rd(tunnel, v6dst, &dst); 915 return dst; 916 } 917 918 static bool ipip6_tunnel_dst_find(struct sk_buff *skb, __be32 *dst, 919 bool is_isatap) 920 { 921 const struct ipv6hdr *iph6 = ipv6_hdr(skb); 922 struct neighbour *neigh = NULL; 923 const struct in6_addr *addr6; 924 bool found = false; 925 int addr_type; 926 927 if (skb_dst(skb)) 928 neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr); 929 930 if (!neigh) { 931 net_dbg_ratelimited("nexthop == NULL\n"); 932 return false; 933 } 934 935 addr6 = (const struct in6_addr *)&neigh->primary_key; 936 addr_type = ipv6_addr_type(addr6); 937 938 if (is_isatap) { 939 if ((addr_type & IPV6_ADDR_UNICAST) && 940 ipv6_addr_is_isatap(addr6)) { 941 *dst = addr6->s6_addr32[3]; 942 found = true; 943 } 944 } else { 945 if (addr_type == IPV6_ADDR_ANY) { 946 addr6 = &ipv6_hdr(skb)->daddr; 947 addr_type = ipv6_addr_type(addr6); 948 } 949 950 if ((addr_type & IPV6_ADDR_COMPATv4) != 0) { 951 *dst = addr6->s6_addr32[3]; 952 found = true; 953 } 954 } 955 956 neigh_release(neigh); 957 958 return found; 959 } 960 961 /* 962 * This function assumes it is being called from dev_queue_xmit() 963 * and that skb is filled properly by that function. 964 */ 965 966 static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb, 967 struct net_device *dev) 968 { 969 struct ip_tunnel *tunnel = netdev_priv(dev); 970 const struct ip_tunnel_parm_kern *parms; 971 const struct iphdr *tiph; 972 const struct ipv6hdr *iph6 = ipv6_hdr(skb); 973 u8 tos; 974 __be16 df; 975 struct rtable *rt; /* Route to the other host */ 976 struct net_device *tdev; /* Device to other host */ 977 unsigned int max_headroom; /* The extra header space needed */ 978 __be32 dst; 979 int err_count, mtu; 980 struct flowi4 fl4; 981 u8 ttl; 982 u8 protocol = IPPROTO_IPV6; 983 int t_hlen = tunnel->hlen + sizeof(struct iphdr); 984 985 parms = rcu_dereference(tunnel->sit_parms); 986 tiph = &parms->iph; 987 tos = parms->iph.tos; 988 df = tiph->frag_off; 989 dst = tiph->daddr; 990 991 if (tos == 1) 992 tos = ipv6_get_dsfield(iph6); 993 994 /* ISATAP (RFC4214) - must come before 6to4 */ 995 if ((dev->priv_flags & IFF_ISATAP) && 996 !ipip6_tunnel_dst_find(skb, &dst, true)) 997 goto tx_error; 998 999 if (!dst) 1000 dst = try_6rd(tunnel, &iph6->daddr); 1001 1002 if (!dst && !ipip6_tunnel_dst_find(skb, &dst, false)) 1003 goto tx_error; 1004 1005 flowi4_init_output(&fl4, parms->link, READ_ONCE(tunnel->fwmark), 1006 tos & INET_DSCP_MASK, RT_SCOPE_UNIVERSE, 1007 IPPROTO_IPV6, 0, dst, tiph->saddr, 0, 0, 1008 sock_net_uid(tunnel->net, NULL)); 1009 1010 rt = dst_cache_get_ip4(&tunnel->dst_cache, &fl4.saddr); 1011 if (!rt) { 1012 rt = ip_route_output_flow(tunnel->net, &fl4, NULL); 1013 if (IS_ERR(rt)) { 1014 DEV_STATS_INC(dev, tx_carrier_errors); 1015 goto tx_error_icmp; 1016 } 1017 dst_cache_set_ip4(&tunnel->dst_cache, &rt->dst, fl4.saddr); 1018 } 1019 1020 if (rt->rt_type != RTN_UNICAST && rt->rt_type != RTN_LOCAL) { 1021 ip_rt_put(rt); 1022 DEV_STATS_INC(dev, tx_carrier_errors); 1023 goto tx_error_icmp; 1024 } 1025 tdev = rt->dst.dev; 1026 1027 if (tdev == dev) { 1028 ip_rt_put(rt); 1029 DEV_STATS_INC(dev, collisions); 1030 goto tx_error; 1031 } 1032 1033 if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP4)) { 1034 ip_rt_put(rt); 1035 goto tx_error; 1036 } 1037 iph6 = ipv6_hdr(skb); 1038 1039 if (df) { 1040 mtu = dst4_mtu(&rt->dst) - t_hlen; 1041 1042 if (mtu < IPV4_MIN_MTU) { 1043 DEV_STATS_INC(dev, collisions); 1044 ip_rt_put(rt); 1045 goto tx_error; 1046 } 1047 1048 if (mtu < IPV6_MIN_MTU) { 1049 mtu = IPV6_MIN_MTU; 1050 df = 0; 1051 } 1052 1053 if (parms->iph.daddr) 1054 skb_dst_update_pmtu_no_confirm(skb, mtu); 1055 1056 if (skb->len > mtu && !skb_is_gso(skb)) { 1057 icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); 1058 ip_rt_put(rt); 1059 goto tx_error; 1060 } 1061 } 1062 1063 err_count = READ_ONCE(tunnel->err_count); 1064 if (err_count > 0) { 1065 if (time_before(jiffies, 1066 READ_ONCE(tunnel->err_time) + IPTUNNEL_ERR_TIMEO)) { 1067 WRITE_ONCE(tunnel->err_count, err_count - 1); 1068 dst_link_failure(skb); 1069 } else { 1070 WRITE_ONCE(tunnel->err_count, 0); 1071 } 1072 } 1073 1074 /* 1075 * Okay, now see if we can stuff it in the buffer as-is. 1076 */ 1077 max_headroom = LL_RESERVED_SPACE(tdev) + t_hlen; 1078 1079 if (skb_headroom(skb) < max_headroom || skb_shared(skb) || 1080 (skb_cloned(skb) && !skb_clone_writable(skb, 0))) { 1081 struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom); 1082 if (!new_skb) { 1083 ip_rt_put(rt); 1084 DEV_STATS_INC(dev, tx_dropped); 1085 kfree_skb(skb); 1086 return NETDEV_TX_OK; 1087 } 1088 if (skb->sk) 1089 skb_set_owner_w(new_skb, skb->sk); 1090 dev_kfree_skb(skb); 1091 skb = new_skb; 1092 iph6 = ipv6_hdr(skb); 1093 } 1094 ttl = tiph->ttl; 1095 if (ttl == 0) 1096 ttl = iph6->hop_limit; 1097 tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6)); 1098 1099 if (ip_tunnel_encap(skb, &tunnel->encap, &protocol, &fl4) < 0) { 1100 ip_rt_put(rt); 1101 goto tx_error; 1102 } 1103 1104 skb_set_inner_ipproto(skb, IPPROTO_IPV6); 1105 1106 iptunnel_xmit(NULL, rt, skb, fl4.saddr, fl4.daddr, protocol, tos, ttl, 1107 df, !net_eq(tunnel->net, dev_net(dev)), 0); 1108 return NETDEV_TX_OK; 1109 1110 tx_error_icmp: 1111 dst_link_failure(skb); 1112 tx_error: 1113 kfree_skb(skb); 1114 DEV_STATS_INC(dev, tx_errors); 1115 return NETDEV_TX_OK; 1116 } 1117 1118 static netdev_tx_t sit_tunnel_xmit__(struct sk_buff *skb, 1119 struct net_device *dev, u8 ipproto) 1120 { 1121 struct ip_tunnel *tunnel = netdev_priv(dev); 1122 const struct ip_tunnel_parm_kern *parms; 1123 const struct iphdr *tiph; 1124 1125 if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP4)) 1126 goto tx_error; 1127 1128 skb_set_inner_ipproto(skb, ipproto); 1129 1130 parms = rcu_dereference(tunnel->sit_parms); 1131 tiph = &parms->iph; 1132 1133 ip_tunnel_xmit(skb, dev, tiph, ipproto); 1134 return NETDEV_TX_OK; 1135 tx_error: 1136 kfree_skb(skb); 1137 DEV_STATS_INC(dev, tx_errors); 1138 return NETDEV_TX_OK; 1139 } 1140 1141 static netdev_tx_t sit_tunnel_xmit(struct sk_buff *skb, 1142 struct net_device *dev) 1143 { 1144 if (!pskb_inet_may_pull(skb)) 1145 goto tx_err; 1146 1147 rcu_read_lock(); 1148 switch (skb->protocol) { 1149 case htons(ETH_P_IP): 1150 sit_tunnel_xmit__(skb, dev, IPPROTO_IPIP); 1151 break; 1152 case htons(ETH_P_IPV6): 1153 ipip6_tunnel_xmit(skb, dev); 1154 break; 1155 #if IS_ENABLED(CONFIG_MPLS) 1156 case htons(ETH_P_MPLS_UC): 1157 sit_tunnel_xmit__(skb, dev, IPPROTO_MPLS); 1158 break; 1159 #endif 1160 default: 1161 rcu_read_unlock(); 1162 goto tx_err; 1163 } 1164 rcu_read_unlock(); 1165 1166 return NETDEV_TX_OK; 1167 1168 tx_err: 1169 DEV_STATS_INC(dev, tx_errors); 1170 kfree_skb(skb); 1171 return NETDEV_TX_OK; 1172 } 1173 1174 static void ipip6_tunnel_bind_dev(struct net_device *dev) 1175 { 1176 struct ip_tunnel *tunnel = netdev_priv(dev); 1177 int t_hlen = tunnel->hlen + sizeof(struct iphdr); 1178 const struct ip_tunnel_parm_kern *parms; 1179 struct net_device *tdev = NULL; 1180 int hlen = LL_MAX_HEADER; 1181 const struct iphdr *iph; 1182 struct flowi4 fl4; 1183 1184 parms = rtnl_dereference(tunnel->sit_parms); 1185 iph = &parms->iph; 1186 1187 if (iph->daddr) { 1188 struct rtable *rt = ip_route_output_ports(tunnel->net, &fl4, 1189 NULL, 1190 iph->daddr, iph->saddr, 1191 0, 0, 1192 IPPROTO_IPV6, 1193 iph->tos & INET_DSCP_MASK, 1194 parms->link); 1195 1196 if (!IS_ERR(rt)) { 1197 tdev = rt->dst.dev; 1198 ip_rt_put(rt); 1199 } 1200 dev->flags |= IFF_POINTOPOINT; 1201 } 1202 1203 if (!tdev && parms->link) 1204 tdev = __dev_get_by_index(tunnel->net, parms->link); 1205 1206 if (tdev && !netif_is_l3_master(tdev)) { 1207 int mtu; 1208 1209 mtu = tdev->mtu - t_hlen; 1210 if (mtu < IPV6_MIN_MTU) 1211 mtu = IPV6_MIN_MTU; 1212 WRITE_ONCE(dev->mtu, mtu); 1213 hlen = tdev->hard_header_len + tdev->needed_headroom; 1214 } 1215 dev->needed_headroom = ip_tunnel_limit_headroom(t_hlen + hlen); 1216 } 1217 1218 static int ipip6_tunnel_update(struct ip_tunnel *t, 1219 struct ip_tunnel_parm_kern *p, 1220 __u32 fwmark) 1221 { 1222 struct net *net = t->net; 1223 struct sit_net *sitn = net_generic(net, sit_net_id); 1224 struct ip_tunnel_parm_kern *new_p, *old_p; 1225 bool move; 1226 1227 old_p = rtnl_dereference(t->sit_parms); 1228 new_p = kmalloc_obj(*new_p); 1229 if (!new_p) 1230 return -ENOMEM; 1231 *new_p = *old_p; 1232 new_p->iph.saddr = p->iph.saddr; 1233 new_p->iph.daddr = p->iph.daddr; 1234 new_p->iph.ttl = p->iph.ttl; 1235 new_p->iph.tos = p->iph.tos; 1236 new_p->iph.frag_off = p->iph.frag_off; 1237 new_p->link = p->link; 1238 move = old_p->iph.saddr != p->iph.saddr || 1239 old_p->iph.daddr != p->iph.daddr; 1240 1241 if (move) 1242 ipip6_tunnel_unlink(sitn, t); 1243 1244 t->parms.iph = new_p->iph; 1245 WRITE_ONCE(t->parms.link, new_p->link); 1246 rcu_assign_pointer(t->sit_parms, new_p); 1247 1248 if (move) { 1249 synchronize_net(); 1250 __dev_addr_set(t->dev, &p->iph.saddr, 4); 1251 memcpy(t->dev->broadcast, &p->iph.daddr, 4); 1252 ipip6_tunnel_link(sitn, t); 1253 } 1254 if (old_p->link != p->link || t->fwmark != fwmark) { 1255 WRITE_ONCE(t->fwmark, fwmark); 1256 ipip6_tunnel_bind_dev(t->dev); 1257 } 1258 dst_cache_reset(&t->dst_cache); 1259 netdev_state_change(t->dev); 1260 kfree_rcu(old_p, rcu); 1261 return 0; 1262 } 1263 1264 #ifdef CONFIG_IPV6_SIT_6RD 1265 static int ipip6_tunnel_update_6rd(struct ip_tunnel *t, 1266 struct ip_tunnel_6rd *ip6rd) 1267 { 1268 struct ip_tunnel_6rd_parm *new_6rd, *old_6rd; 1269 struct in6_addr prefix; 1270 __be32 relay_prefix; 1271 1272 if (ip6rd->relay_prefixlen > 32 || 1273 ip6rd->prefixlen + (32 - ip6rd->relay_prefixlen) > 64) 1274 return -EINVAL; 1275 1276 ipv6_addr_prefix(&prefix, &ip6rd->prefix, ip6rd->prefixlen); 1277 if (!ipv6_addr_equal(&prefix, &ip6rd->prefix)) 1278 return -EINVAL; 1279 if (ip6rd->relay_prefixlen) 1280 relay_prefix = ip6rd->relay_prefix & 1281 htonl(0xffffffffUL << 1282 (32 - ip6rd->relay_prefixlen)); 1283 else 1284 relay_prefix = 0; 1285 if (relay_prefix != ip6rd->relay_prefix) 1286 return -EINVAL; 1287 1288 new_6rd = kmalloc_obj(*new_6rd); 1289 if (!new_6rd) 1290 return -ENOMEM; 1291 1292 new_6rd->prefix = prefix; 1293 new_6rd->relay_prefix = relay_prefix; 1294 new_6rd->prefixlen = ip6rd->prefixlen; 1295 new_6rd->relay_prefixlen = ip6rd->relay_prefixlen; 1296 1297 old_6rd = rtnl_dereference(t->ip6rd); 1298 rcu_assign_pointer(t->ip6rd, new_6rd); 1299 if (old_6rd) 1300 kfree_rcu(old_6rd, rcu); 1301 1302 dst_cache_reset(&t->dst_cache); 1303 netdev_state_change(t->dev); 1304 return 0; 1305 } 1306 1307 static int 1308 ipip6_tunnel_get6rd(struct net_device *dev, struct ip_tunnel_parm __user *data) 1309 { 1310 const struct ip_tunnel_6rd_parm *ip6rd_parm; 1311 struct ip_tunnel *t = netdev_priv(dev); 1312 struct ip_tunnel_parm_kern p; 1313 struct ip_tunnel_6rd ip6rd; 1314 1315 if (dev == dev_to_sit_net(dev)->fb_tunnel_dev) { 1316 if (!ip_tunnel_parm_from_user(&p, data)) 1317 return -EFAULT; 1318 t = ipip6_tunnel_locate(t->net, &p, 0); 1319 } 1320 if (!t) 1321 t = netdev_priv(dev); 1322 1323 ip6rd_parm = rtnl_dereference(t->ip6rd); 1324 if (ip6rd_parm) { 1325 ip6rd.prefix = ip6rd_parm->prefix; 1326 ip6rd.relay_prefix = ip6rd_parm->relay_prefix; 1327 ip6rd.prefixlen = ip6rd_parm->prefixlen; 1328 ip6rd.relay_prefixlen = ip6rd_parm->relay_prefixlen; 1329 } else { 1330 memset(&ip6rd, 0, sizeof(ip6rd)); 1331 } 1332 if (copy_to_user(data, &ip6rd, sizeof(ip6rd))) 1333 return -EFAULT; 1334 return 0; 1335 } 1336 1337 static int 1338 ipip6_tunnel_6rdctl(struct net_device *dev, struct ip_tunnel_6rd __user *data, 1339 int cmd) 1340 { 1341 struct ip_tunnel *t = netdev_priv(dev); 1342 struct ip_tunnel_6rd ip6rd; 1343 1344 if (!ns_capable(t->net->user_ns, CAP_NET_ADMIN)) 1345 return -EPERM; 1346 if (copy_from_user(&ip6rd, data, sizeof(ip6rd))) 1347 return -EFAULT; 1348 1349 if (cmd != SIOCDEL6RD) 1350 return ipip6_tunnel_update_6rd(t, &ip6rd); 1351 1352 return ipip6_tunnel_clone_6rd(dev, dev_to_sit_net(dev)); 1353 } 1354 1355 #endif /* CONFIG_IPV6_SIT_6RD */ 1356 1357 static bool ipip6_valid_ip_proto(u8 ipproto) 1358 { 1359 return ipproto == IPPROTO_IPV6 || 1360 ipproto == IPPROTO_IPIP || 1361 #if IS_ENABLED(CONFIG_MPLS) 1362 ipproto == IPPROTO_MPLS || 1363 #endif 1364 ipproto == 0; 1365 } 1366 1367 static int 1368 __ipip6_tunnel_ioctl_validate(struct net *net, struct ip_tunnel_parm_kern *p) 1369 { 1370 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 1371 return -EPERM; 1372 1373 if (!ipip6_valid_ip_proto(p->iph.protocol)) 1374 return -EINVAL; 1375 if (p->iph.version != 4 || 1376 p->iph.ihl != 5 || (p->iph.frag_off & htons(~IP_DF))) 1377 return -EINVAL; 1378 1379 if (p->iph.ttl) 1380 p->iph.frag_off |= htons(IP_DF); 1381 return 0; 1382 } 1383 1384 static int 1385 ipip6_tunnel_get(struct net_device *dev, struct ip_tunnel_parm_kern *p) 1386 { 1387 struct ip_tunnel *t = netdev_priv(dev); 1388 const struct ip_tunnel_parm_kern *parms; 1389 1390 if (dev == dev_to_sit_net(dev)->fb_tunnel_dev) 1391 t = ipip6_tunnel_locate(t->net, p, 0); 1392 if (!t) 1393 t = netdev_priv(dev); 1394 parms = rtnl_dereference(t->sit_parms); 1395 memcpy(p, parms, sizeof(*p)); 1396 return 0; 1397 } 1398 1399 static int 1400 ipip6_tunnel_add(struct net_device *dev, struct ip_tunnel_parm_kern *p) 1401 { 1402 struct ip_tunnel *t = netdev_priv(dev); 1403 int err; 1404 1405 err = __ipip6_tunnel_ioctl_validate(t->net, p); 1406 if (err) 1407 return err; 1408 1409 t = ipip6_tunnel_locate(t->net, p, 1); 1410 if (!t) 1411 return -ENOBUFS; 1412 return 0; 1413 } 1414 1415 static int 1416 ipip6_tunnel_change(struct net_device *dev, struct ip_tunnel_parm_kern *p) 1417 { 1418 struct ip_tunnel *t = netdev_priv(dev); 1419 int err; 1420 1421 err = __ipip6_tunnel_ioctl_validate(t->net, p); 1422 if (err) 1423 return err; 1424 1425 t = ipip6_tunnel_locate(t->net, p, 0); 1426 if (dev == dev_to_sit_net(dev)->fb_tunnel_dev) { 1427 if (!t) 1428 return -ENOENT; 1429 } else { 1430 if (t) { 1431 if (t->dev != dev) 1432 return -EEXIST; 1433 } else { 1434 if (((dev->flags & IFF_POINTOPOINT) && !p->iph.daddr) || 1435 (!(dev->flags & IFF_POINTOPOINT) && p->iph.daddr)) 1436 return -EINVAL; 1437 t = netdev_priv(dev); 1438 } 1439 1440 err = ipip6_tunnel_update(t, p, t->fwmark); 1441 if (err) 1442 return err; 1443 } 1444 1445 return 0; 1446 } 1447 1448 static int 1449 ipip6_tunnel_del(struct net_device *dev, struct ip_tunnel_parm_kern *p) 1450 { 1451 struct ip_tunnel *t = netdev_priv(dev); 1452 1453 if (!ns_capable(t->net->user_ns, CAP_NET_ADMIN)) 1454 return -EPERM; 1455 1456 if (dev == dev_to_sit_net(dev)->fb_tunnel_dev) { 1457 t = ipip6_tunnel_locate(t->net, p, 0); 1458 if (!t) 1459 return -ENOENT; 1460 if (t == netdev_priv(dev_to_sit_net(dev)->fb_tunnel_dev)) 1461 return -EPERM; 1462 dev = t->dev; 1463 } 1464 unregister_netdevice(dev); 1465 return 0; 1466 } 1467 1468 static int 1469 ipip6_tunnel_ctl(struct net_device *dev, struct ip_tunnel_parm_kern *p, 1470 int cmd) 1471 { 1472 switch (cmd) { 1473 case SIOCGETTUNNEL: 1474 return ipip6_tunnel_get(dev, p); 1475 case SIOCADDTUNNEL: 1476 return ipip6_tunnel_add(dev, p); 1477 case SIOCCHGTUNNEL: 1478 return ipip6_tunnel_change(dev, p); 1479 case SIOCDELTUNNEL: 1480 return ipip6_tunnel_del(dev, p); 1481 default: 1482 return -EINVAL; 1483 } 1484 } 1485 1486 static int 1487 ipip6_tunnel_siocdevprivate(struct net_device *dev, struct ifreq *ifr, 1488 void __user *data, int cmd) 1489 { 1490 switch (cmd) { 1491 case SIOCGETTUNNEL: 1492 case SIOCADDTUNNEL: 1493 case SIOCCHGTUNNEL: 1494 case SIOCDELTUNNEL: 1495 return ip_tunnel_siocdevprivate(dev, ifr, data, cmd); 1496 case SIOCGETPRL: 1497 return ipip6_tunnel_get_prl(dev, data); 1498 case SIOCADDPRL: 1499 case SIOCDELPRL: 1500 case SIOCCHGPRL: 1501 return ipip6_tunnel_prl_ctl(dev, data, cmd); 1502 #ifdef CONFIG_IPV6_SIT_6RD 1503 case SIOCGET6RD: 1504 return ipip6_tunnel_get6rd(dev, data); 1505 case SIOCADD6RD: 1506 case SIOCCHG6RD: 1507 case SIOCDEL6RD: 1508 return ipip6_tunnel_6rdctl(dev, data, cmd); 1509 #endif 1510 default: 1511 return -EINVAL; 1512 } 1513 } 1514 1515 static int ipip6_get_iflink(const struct net_device *dev) 1516 { 1517 struct ip_tunnel *tunnel = netdev_priv(dev); 1518 const struct ip_tunnel_parm_kern *parms; 1519 int link; 1520 1521 rcu_read_lock(); 1522 parms = rcu_dereference(tunnel->sit_parms); 1523 link = parms ? parms->link : 0; 1524 rcu_read_unlock(); 1525 1526 return link; 1527 } 1528 1529 static const struct net_device_ops ipip6_netdev_ops = { 1530 .ndo_init = ipip6_tunnel_init, 1531 .ndo_uninit = ipip6_tunnel_uninit, 1532 .ndo_start_xmit = sit_tunnel_xmit, 1533 .ndo_siocdevprivate = ipip6_tunnel_siocdevprivate, 1534 .ndo_get_iflink = ipip6_get_iflink, 1535 .ndo_tunnel_ctl = ipip6_tunnel_ctl, 1536 }; 1537 1538 static void ipip6_dev_free(struct net_device *dev) 1539 { 1540 struct ip_tunnel *tunnel = netdev_priv(dev); 1541 struct ip_tunnel_parm_kern *parms; 1542 #ifdef CONFIG_IPV6_SIT_6RD 1543 struct ip_tunnel_6rd_parm *ip6rd; 1544 1545 ip6rd = rcu_dereference_protected(tunnel->ip6rd, 1); 1546 RCU_INIT_POINTER(tunnel->ip6rd, NULL); 1547 kfree(ip6rd); 1548 #endif 1549 parms = rcu_dereference_protected(tunnel->sit_parms, 1); 1550 RCU_INIT_POINTER(tunnel->sit_parms, NULL); 1551 kfree(parms); 1552 if (tunnel->dst_cache.cache) { 1553 dst_cache_destroy(&tunnel->dst_cache); 1554 tunnel->dst_cache.cache = NULL; 1555 } 1556 } 1557 1558 #define SIT_FEATURES (NETIF_F_SG | \ 1559 NETIF_F_FRAGLIST | \ 1560 NETIF_F_HIGHDMA | \ 1561 NETIF_F_GSO_SOFTWARE | \ 1562 NETIF_F_HW_CSUM) 1563 1564 static void ipip6_tunnel_setup(struct net_device *dev) 1565 { 1566 struct ip_tunnel *tunnel = netdev_priv(dev); 1567 int t_hlen = tunnel->hlen + sizeof(struct iphdr); 1568 1569 dev->netdev_ops = &ipip6_netdev_ops; 1570 dev->header_ops = &ip_tunnel_header_ops; 1571 dev->needs_free_netdev = true; 1572 dev->priv_destructor = ipip6_dev_free; 1573 1574 dev->type = ARPHRD_SIT; 1575 dev->mtu = ETH_DATA_LEN - t_hlen; 1576 dev->min_mtu = IPV6_MIN_MTU; 1577 dev->max_mtu = IP6_MAX_MTU - t_hlen; 1578 dev->flags = IFF_NOARP; 1579 netif_keep_dst(dev); 1580 dev->addr_len = 4; 1581 dev->lltx = true; 1582 dev->features |= SIT_FEATURES; 1583 dev->hw_features |= SIT_FEATURES; 1584 dev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS; 1585 1586 } 1587 1588 static int ipip6_tunnel_init(struct net_device *dev) 1589 { 1590 struct ip_tunnel *tunnel = netdev_priv(dev); 1591 struct ip_tunnel_parm_kern *parms; 1592 int err; 1593 1594 tunnel->dev = dev; 1595 parms = rtnl_dereference(tunnel->sit_parms); 1596 strscpy(parms->name, dev->name); 1597 1598 ipip6_tunnel_bind_dev(dev); 1599 1600 err = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL); 1601 if (err) 1602 return err; 1603 1604 netdev_hold(dev, &tunnel->dev_tracker, GFP_KERNEL); 1605 netdev_lockdep_set_classes(dev); 1606 return 0; 1607 } 1608 1609 static void __net_init ipip6_fb_tunnel_init(struct net_device *dev) 1610 { 1611 struct ip_tunnel *tunnel = netdev_priv(dev); 1612 struct net *net = dev_net(dev); 1613 struct sit_net *sitn = net_generic(net, sit_net_id); 1614 rcu_assign_pointer(sitn->tunnels_wc[0], tunnel); 1615 } 1616 1617 static int ipip6_validate(struct nlattr *tb[], struct nlattr *data[], 1618 struct netlink_ext_ack *extack) 1619 { 1620 u8 proto; 1621 1622 if (!data || !data[IFLA_IPTUN_PROTO]) 1623 return 0; 1624 1625 proto = nla_get_u8(data[IFLA_IPTUN_PROTO]); 1626 if (!ipip6_valid_ip_proto(proto)) 1627 return -EINVAL; 1628 1629 return 0; 1630 } 1631 1632 static void ipip6_netlink_parms(struct nlattr *data[], 1633 struct ip_tunnel_parm_kern *parms, 1634 __u32 *fwmark) 1635 { 1636 memset(parms, 0, sizeof(*parms)); 1637 1638 parms->iph.version = 4; 1639 parms->iph.protocol = IPPROTO_IPV6; 1640 parms->iph.ihl = 5; 1641 parms->iph.ttl = 64; 1642 1643 if (!data) 1644 return; 1645 1646 ip_tunnel_netlink_parms(data, parms); 1647 1648 if (data[IFLA_IPTUN_FWMARK]) 1649 *fwmark = nla_get_u32(data[IFLA_IPTUN_FWMARK]); 1650 } 1651 1652 #ifdef CONFIG_IPV6_SIT_6RD 1653 /* This function returns true when 6RD attributes are present in the nl msg */ 1654 static bool ipip6_netlink_6rd_parms(struct nlattr *data[], 1655 struct ip_tunnel_6rd *ip6rd) 1656 { 1657 bool ret = false; 1658 memset(ip6rd, 0, sizeof(*ip6rd)); 1659 1660 if (!data) 1661 return ret; 1662 1663 if (data[IFLA_IPTUN_6RD_PREFIX]) { 1664 ret = true; 1665 ip6rd->prefix = nla_get_in6_addr(data[IFLA_IPTUN_6RD_PREFIX]); 1666 } 1667 1668 if (data[IFLA_IPTUN_6RD_RELAY_PREFIX]) { 1669 ret = true; 1670 ip6rd->relay_prefix = 1671 nla_get_be32(data[IFLA_IPTUN_6RD_RELAY_PREFIX]); 1672 } 1673 1674 if (data[IFLA_IPTUN_6RD_PREFIXLEN]) { 1675 ret = true; 1676 ip6rd->prefixlen = nla_get_u16(data[IFLA_IPTUN_6RD_PREFIXLEN]); 1677 } 1678 1679 if (data[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]) { 1680 ret = true; 1681 ip6rd->relay_prefixlen = 1682 nla_get_u16(data[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]); 1683 } 1684 1685 return ret; 1686 } 1687 #endif 1688 1689 static int ipip6_newlink(struct net_device *dev, 1690 struct rtnl_newlink_params *params, 1691 struct netlink_ext_ack *extack) 1692 { 1693 struct nlattr **data = params->data; 1694 struct nlattr **tb = params->tb; 1695 struct ip_tunnel *nt; 1696 struct ip_tunnel_encap ipencap; 1697 #ifdef CONFIG_IPV6_SIT_6RD 1698 struct ip_tunnel_6rd ip6rd; 1699 #endif 1700 struct ip_tunnel_parm_kern *nt_parms; 1701 struct ip_tunnel_parm_kern p; 1702 struct net *net; 1703 int err; 1704 1705 net = params->link_net ? : dev_net(dev); 1706 nt = netdev_priv(dev); 1707 nt->net = net; 1708 1709 if (ip_tunnel_netlink_encap_parms(data, &ipencap)) { 1710 err = ip_tunnel_encap_setup(nt, &ipencap); 1711 if (err < 0) 1712 return err; 1713 } 1714 1715 ipip6_netlink_parms(data, &p, &nt->fwmark); 1716 1717 if (ipip6_tunnel_locate(net, &p, 0)) 1718 return -EEXIST; 1719 1720 nt_parms = kmalloc_obj(*nt_parms); 1721 if (!nt_parms) 1722 return -ENOMEM; 1723 *nt_parms = p; 1724 rcu_assign_pointer(nt->sit_parms, nt_parms); 1725 1726 err = ipip6_tunnel_create(dev); 1727 if (err < 0) { 1728 ipip6_dev_free(dev); 1729 return err; 1730 } 1731 1732 if (tb[IFLA_MTU]) { 1733 u32 mtu = nla_get_u32(tb[IFLA_MTU]); 1734 1735 if (mtu >= IPV6_MIN_MTU && 1736 mtu <= IP6_MAX_MTU - dev->hard_header_len) 1737 dev->mtu = mtu; 1738 } 1739 1740 #ifdef CONFIG_IPV6_SIT_6RD 1741 if (ipip6_netlink_6rd_parms(data, &ip6rd)) { 1742 err = ipip6_tunnel_update_6rd(nt, &ip6rd); 1743 if (err < 0) 1744 unregister_netdevice_queue(dev, NULL); 1745 } 1746 #endif 1747 1748 return err; 1749 } 1750 1751 static int ipip6_changelink(struct net_device *dev, struct nlattr *tb[], 1752 struct nlattr *data[], 1753 struct netlink_ext_ack *extack) 1754 { 1755 struct ip_tunnel *t = netdev_priv(dev); 1756 struct ip_tunnel_encap ipencap; 1757 struct ip_tunnel_parm_kern p; 1758 struct net *net = t->net; 1759 struct sit_net *sitn = net_generic(net, sit_net_id); 1760 #ifdef CONFIG_IPV6_SIT_6RD 1761 struct ip_tunnel_6rd ip6rd; 1762 #endif 1763 __u32 fwmark = t->fwmark; 1764 int err; 1765 1766 if (!rtnl_dev_link_net_capable(dev, net)) 1767 return -EPERM; 1768 1769 if (dev == sitn->fb_tunnel_dev) 1770 return -EINVAL; 1771 1772 if (ip_tunnel_netlink_encap_parms(data, &ipencap)) { 1773 err = ip_tunnel_encap_setup(t, &ipencap); 1774 if (err < 0) 1775 return err; 1776 } 1777 1778 ipip6_netlink_parms(data, &p, &fwmark); 1779 1780 if (((dev->flags & IFF_POINTOPOINT) && !p.iph.daddr) || 1781 (!(dev->flags & IFF_POINTOPOINT) && p.iph.daddr)) 1782 return -EINVAL; 1783 1784 t = ipip6_tunnel_locate(net, &p, 0); 1785 1786 if (t) { 1787 if (t->dev != dev) 1788 return -EEXIST; 1789 } else 1790 t = netdev_priv(dev); 1791 1792 err = ipip6_tunnel_update(t, &p, fwmark); 1793 if (err) 1794 return err; 1795 1796 #ifdef CONFIG_IPV6_SIT_6RD 1797 if (ipip6_netlink_6rd_parms(data, &ip6rd)) 1798 return ipip6_tunnel_update_6rd(t, &ip6rd); 1799 #endif 1800 1801 return 0; 1802 } 1803 1804 static size_t ipip6_get_size(const struct net_device *dev) 1805 { 1806 return 1807 /* IFLA_IPTUN_LINK */ 1808 nla_total_size(4) + 1809 /* IFLA_IPTUN_LOCAL */ 1810 nla_total_size(4) + 1811 /* IFLA_IPTUN_REMOTE */ 1812 nla_total_size(4) + 1813 /* IFLA_IPTUN_TTL */ 1814 nla_total_size(1) + 1815 /* IFLA_IPTUN_TOS */ 1816 nla_total_size(1) + 1817 /* IFLA_IPTUN_PMTUDISC */ 1818 nla_total_size(1) + 1819 /* IFLA_IPTUN_FLAGS */ 1820 nla_total_size(2) + 1821 /* IFLA_IPTUN_PROTO */ 1822 nla_total_size(1) + 1823 #ifdef CONFIG_IPV6_SIT_6RD 1824 /* IFLA_IPTUN_6RD_PREFIX */ 1825 nla_total_size(sizeof(struct in6_addr)) + 1826 /* IFLA_IPTUN_6RD_RELAY_PREFIX */ 1827 nla_total_size(4) + 1828 /* IFLA_IPTUN_6RD_PREFIXLEN */ 1829 nla_total_size(2) + 1830 /* IFLA_IPTUN_6RD_RELAY_PREFIXLEN */ 1831 nla_total_size(2) + 1832 #endif 1833 /* IFLA_IPTUN_ENCAP_TYPE */ 1834 nla_total_size(2) + 1835 /* IFLA_IPTUN_ENCAP_FLAGS */ 1836 nla_total_size(2) + 1837 /* IFLA_IPTUN_ENCAP_SPORT */ 1838 nla_total_size(2) + 1839 /* IFLA_IPTUN_ENCAP_DPORT */ 1840 nla_total_size(2) + 1841 /* IFLA_IPTUN_FWMARK */ 1842 nla_total_size(4) + 1843 0; 1844 } 1845 1846 static int ipip6_fill_info(struct sk_buff *skb, const struct net_device *dev) 1847 { 1848 struct ip_tunnel *tunnel = netdev_priv(dev); 1849 const struct ip_tunnel_parm_kern *parm; 1850 #ifdef CONFIG_IPV6_SIT_6RD 1851 const struct ip_tunnel_6rd_parm *ip6rd; 1852 #endif 1853 1854 rcu_read_lock(); 1855 parm = rcu_dereference(tunnel->sit_parms); 1856 /* If the device is being dismantled, there is nothing to report. */ 1857 if (!parm) { 1858 rcu_read_unlock(); 1859 return 0; 1860 } 1861 1862 if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) || 1863 nla_put_in_addr(skb, IFLA_IPTUN_LOCAL, parm->iph.saddr) || 1864 nla_put_in_addr(skb, IFLA_IPTUN_REMOTE, parm->iph.daddr) || 1865 nla_put_u8(skb, IFLA_IPTUN_TTL, parm->iph.ttl) || 1866 nla_put_u8(skb, IFLA_IPTUN_TOS, parm->iph.tos) || 1867 nla_put_u8(skb, IFLA_IPTUN_PMTUDISC, 1868 !!(parm->iph.frag_off & htons(IP_DF))) || 1869 nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->iph.protocol) || 1870 nla_put_be16(skb, IFLA_IPTUN_FLAGS, 1871 ip_tunnel_flags_to_be16(parm->i_flags)) || 1872 nla_put_u32(skb, IFLA_IPTUN_FWMARK, READ_ONCE(tunnel->fwmark))) 1873 goto nla_put_failure; 1874 1875 #ifdef CONFIG_IPV6_SIT_6RD 1876 ip6rd = rcu_dereference(tunnel->ip6rd); 1877 if (ip6rd && 1878 (nla_put_in6_addr(skb, IFLA_IPTUN_6RD_PREFIX, 1879 &ip6rd->prefix) || 1880 nla_put_in_addr(skb, IFLA_IPTUN_6RD_RELAY_PREFIX, 1881 ip6rd->relay_prefix) || 1882 nla_put_u16(skb, IFLA_IPTUN_6RD_PREFIXLEN, 1883 ip6rd->prefixlen) || 1884 nla_put_u16(skb, IFLA_IPTUN_6RD_RELAY_PREFIXLEN, 1885 ip6rd->relay_prefixlen))) 1886 goto nla_put_failure; 1887 #endif 1888 1889 if (nla_put_u16(skb, IFLA_IPTUN_ENCAP_TYPE, 1890 READ_ONCE(tunnel->encap.type)) || 1891 nla_put_be16(skb, IFLA_IPTUN_ENCAP_SPORT, 1892 READ_ONCE(tunnel->encap.sport)) || 1893 nla_put_be16(skb, IFLA_IPTUN_ENCAP_DPORT, 1894 READ_ONCE(tunnel->encap.dport)) || 1895 nla_put_u16(skb, IFLA_IPTUN_ENCAP_FLAGS, 1896 READ_ONCE(tunnel->encap.flags))) 1897 goto nla_put_failure; 1898 1899 rcu_read_unlock(); 1900 return 0; 1901 1902 nla_put_failure: 1903 rcu_read_unlock(); 1904 return -EMSGSIZE; 1905 } 1906 1907 static const struct nla_policy ipip6_policy[IFLA_IPTUN_MAX + 1] = { 1908 [IFLA_IPTUN_LINK] = { .type = NLA_U32 }, 1909 [IFLA_IPTUN_LOCAL] = { .type = NLA_U32 }, 1910 [IFLA_IPTUN_REMOTE] = { .type = NLA_U32 }, 1911 [IFLA_IPTUN_TTL] = { .type = NLA_U8 }, 1912 [IFLA_IPTUN_TOS] = { .type = NLA_U8 }, 1913 [IFLA_IPTUN_PMTUDISC] = { .type = NLA_U8 }, 1914 [IFLA_IPTUN_FLAGS] = { .type = NLA_U16 }, 1915 [IFLA_IPTUN_PROTO] = { .type = NLA_U8 }, 1916 #ifdef CONFIG_IPV6_SIT_6RD 1917 [IFLA_IPTUN_6RD_PREFIX] = { .len = sizeof(struct in6_addr) }, 1918 [IFLA_IPTUN_6RD_RELAY_PREFIX] = { .type = NLA_U32 }, 1919 [IFLA_IPTUN_6RD_PREFIXLEN] = { .type = NLA_U16 }, 1920 [IFLA_IPTUN_6RD_RELAY_PREFIXLEN] = { .type = NLA_U16 }, 1921 #endif 1922 [IFLA_IPTUN_ENCAP_TYPE] = { .type = NLA_U16 }, 1923 [IFLA_IPTUN_ENCAP_FLAGS] = { .type = NLA_U16 }, 1924 [IFLA_IPTUN_ENCAP_SPORT] = { .type = NLA_U16 }, 1925 [IFLA_IPTUN_ENCAP_DPORT] = { .type = NLA_U16 }, 1926 [IFLA_IPTUN_FWMARK] = { .type = NLA_U32 }, 1927 }; 1928 1929 static void ipip6_dellink(struct net_device *dev, struct list_head *head) 1930 { 1931 struct net *net = dev_net(dev); 1932 struct sit_net *sitn = net_generic(net, sit_net_id); 1933 1934 if (dev != sitn->fb_tunnel_dev) 1935 unregister_netdevice_queue(dev, head); 1936 } 1937 1938 static struct rtnl_link_ops sit_link_ops __read_mostly = { 1939 .kind = "sit", 1940 .maxtype = IFLA_IPTUN_MAX, 1941 .policy = ipip6_policy, 1942 .priv_size = sizeof(struct ip_tunnel), 1943 .setup = ipip6_tunnel_setup, 1944 .validate = ipip6_validate, 1945 .newlink = ipip6_newlink, 1946 .changelink = ipip6_changelink, 1947 .get_size = ipip6_get_size, 1948 .fill_info = ipip6_fill_info, 1949 .dellink = ipip6_dellink, 1950 .get_link_net = ip_tunnel_get_link_net, 1951 }; 1952 1953 static struct xfrm_tunnel sit_handler __read_mostly = { 1954 .handler = ipip6_rcv, 1955 .err_handler = ipip6_err, 1956 .priority = 1, 1957 }; 1958 1959 static struct xfrm_tunnel ipip_handler __read_mostly = { 1960 .handler = ipip_rcv, 1961 .err_handler = ipip6_err, 1962 .priority = 2, 1963 }; 1964 1965 #if IS_ENABLED(CONFIG_MPLS) 1966 static struct xfrm_tunnel mplsip_handler __read_mostly = { 1967 .handler = mplsip_rcv, 1968 .err_handler = ipip6_err, 1969 .priority = 2, 1970 }; 1971 #endif 1972 1973 static void __net_exit sit_exit_rtnl_net(struct net *net, struct list_head *head) 1974 { 1975 struct sit_net *sitn = net_generic(net, sit_net_id); 1976 struct net_device *dev, *aux; 1977 int prio; 1978 1979 for_each_netdev_safe(net, dev, aux) 1980 if (dev->rtnl_link_ops == &sit_link_ops) 1981 unregister_netdevice_queue(dev, head); 1982 1983 for (prio = 0; prio < 4; prio++) { 1984 int h; 1985 for (h = 0; h < (prio ? IP6_SIT_HASH_SIZE : 1); h++) { 1986 struct ip_tunnel *t; 1987 1988 t = rtnl_net_dereference(net, sitn->tunnels[prio][h]); 1989 while (t) { 1990 /* If dev is in the same netns, it has already 1991 * been added to the list by the previous loop. 1992 */ 1993 if (!net_eq(dev_net(t->dev), net)) 1994 unregister_netdevice_queue(t->dev, head); 1995 1996 t = rtnl_net_dereference(net, t->next); 1997 } 1998 } 1999 } 2000 } 2001 2002 static int __net_init sit_init_net(struct net *net) 2003 { 2004 struct sit_net *sitn = net_generic(net, sit_net_id); 2005 struct ip_tunnel_parm_kern *nt_parms; 2006 struct ip_tunnel *t; 2007 int err; 2008 2009 sitn->tunnels[0] = sitn->tunnels_wc; 2010 sitn->tunnels[1] = sitn->tunnels_l; 2011 sitn->tunnels[2] = sitn->tunnels_r; 2012 sitn->tunnels[3] = sitn->tunnels_r_l; 2013 2014 if (!net_has_fallback_tunnels(net)) 2015 return 0; 2016 2017 sitn->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0", 2018 NET_NAME_UNKNOWN, 2019 ipip6_tunnel_setup); 2020 if (!sitn->fb_tunnel_dev) { 2021 err = -ENOMEM; 2022 goto err_alloc_dev; 2023 } 2024 dev_net_set(sitn->fb_tunnel_dev, net); 2025 sitn->fb_tunnel_dev->rtnl_link_ops = &sit_link_ops; 2026 /* FB netdevice is special: we have one, and only one per netns. 2027 * Allowing to move it to another netns is clearly unsafe. 2028 */ 2029 sitn->fb_tunnel_dev->netns_immutable = true; 2030 2031 t = netdev_priv(sitn->fb_tunnel_dev); 2032 t->net = net; 2033 nt_parms = kzalloc_obj(*nt_parms); 2034 if (!nt_parms) { 2035 err = -ENOMEM; 2036 goto err_reg_dev; 2037 } 2038 nt_parms->iph.version = 4; 2039 nt_parms->iph.protocol = IPPROTO_IPV6; 2040 nt_parms->iph.ihl = 5; 2041 nt_parms->iph.ttl = 64; 2042 strscpy(nt_parms->name, sitn->fb_tunnel_dev->name); 2043 t->parms = *nt_parms; 2044 rcu_assign_pointer(t->sit_parms, nt_parms); 2045 2046 err = ipip6_tunnel_clone_6rd(sitn->fb_tunnel_dev, sitn); 2047 if (err < 0) 2048 goto err_reg_dev; 2049 2050 err = register_netdev(sitn->fb_tunnel_dev); 2051 if (err) 2052 goto err_reg_dev; 2053 2054 ipip6_fb_tunnel_init(sitn->fb_tunnel_dev); 2055 return 0; 2056 2057 err_reg_dev: 2058 ipip6_dev_free(sitn->fb_tunnel_dev); 2059 free_netdev(sitn->fb_tunnel_dev); 2060 err_alloc_dev: 2061 return err; 2062 } 2063 2064 static struct pernet_operations sit_net_ops = { 2065 .init = sit_init_net, 2066 .exit_rtnl = sit_exit_rtnl_net, 2067 .id = &sit_net_id, 2068 .size = sizeof(struct sit_net), 2069 }; 2070 2071 static void __exit sit_cleanup(void) 2072 { 2073 rtnl_link_unregister(&sit_link_ops); 2074 xfrm4_tunnel_deregister(&sit_handler, AF_INET6); 2075 xfrm4_tunnel_deregister(&ipip_handler, AF_INET); 2076 #if IS_ENABLED(CONFIG_MPLS) 2077 xfrm4_tunnel_deregister(&mplsip_handler, AF_MPLS); 2078 #endif 2079 2080 unregister_pernet_device(&sit_net_ops); 2081 rcu_barrier(); /* Wait for completion of call_rcu()'s */ 2082 } 2083 2084 static int __init sit_init(void) 2085 { 2086 int err; 2087 2088 pr_info("IPv6, IPv4 and MPLS over IPv4 tunneling driver\n"); 2089 2090 err = register_pernet_device(&sit_net_ops); 2091 if (err < 0) 2092 return err; 2093 err = xfrm4_tunnel_register(&sit_handler, AF_INET6); 2094 if (err < 0) { 2095 pr_info("%s: can't register ip6ip4\n", __func__); 2096 goto xfrm_tunnel_failed; 2097 } 2098 err = xfrm4_tunnel_register(&ipip_handler, AF_INET); 2099 if (err < 0) { 2100 pr_info("%s: can't register ip4ip4\n", __func__); 2101 goto xfrm_tunnel4_failed; 2102 } 2103 #if IS_ENABLED(CONFIG_MPLS) 2104 err = xfrm4_tunnel_register(&mplsip_handler, AF_MPLS); 2105 if (err < 0) { 2106 pr_info("%s: can't register mplsip\n", __func__); 2107 goto xfrm_tunnel_mpls_failed; 2108 } 2109 #endif 2110 err = rtnl_link_register(&sit_link_ops); 2111 if (err < 0) 2112 goto rtnl_link_failed; 2113 2114 out: 2115 return err; 2116 2117 rtnl_link_failed: 2118 #if IS_ENABLED(CONFIG_MPLS) 2119 xfrm4_tunnel_deregister(&mplsip_handler, AF_MPLS); 2120 xfrm_tunnel_mpls_failed: 2121 #endif 2122 xfrm4_tunnel_deregister(&ipip_handler, AF_INET); 2123 xfrm_tunnel4_failed: 2124 xfrm4_tunnel_deregister(&sit_handler, AF_INET6); 2125 xfrm_tunnel_failed: 2126 unregister_pernet_device(&sit_net_ops); 2127 goto out; 2128 } 2129 2130 module_init(sit_init); 2131 module_exit(sit_cleanup); 2132 MODULE_DESCRIPTION("IPv6-in-IPv4 tunnel SIT driver"); 2133 MODULE_LICENSE("GPL"); 2134 MODULE_ALIAS_RTNL_LINK("sit"); 2135 MODULE_ALIAS_NETDEV("sit0"); 2136