1/* 2 * IPv6 Address [auto]configuration 3 * Linux INET6 implementation 4 * 5 * Authors: 6 * Pedro Roque <roque@di.fc.ul.pt> 7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> 8 * 9 * This program is free software; you can redistribute it and/or 10 * modify it under the terms of the GNU General Public License 11 * as published by the Free Software Foundation; either version 12 * 2 of the License, or (at your option) any later version. 13 */ 14 15/* 16 * Changes: 17 * 18 * Janos Farkas : delete timer on ifdown 19 * <chexum@bankinf.banki.hu> 20 * Andi Kleen : kill double kfree on module 21 * unload. 22 * Maciej W. Rozycki : FDDI support 23 * sekiya@USAGI : Don't send too many RS 24 * packets. 25 * yoshfuji@USAGI : Fixed interval between DAD 26 * packets. 27 * YOSHIFUJI Hideaki @USAGI : improved accuracy of 28 * address validation timer. 29 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041) 30 * support. 31 * Yuji SEKIYA @USAGI : Don't assign a same IPv6 32 * address on a same interface. 33 * YOSHIFUJI Hideaki @USAGI : ARCnet support 34 * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to 35 * seq_file. 36 * YOSHIFUJI Hideaki @USAGI : improved source address 37 * selection; consider scope, 38 * status etc. 39 */ 40 41#include <linux/errno.h> 42#include <linux/types.h> 43#include <linux/kernel.h> 44#include <linux/socket.h> 45#include <linux/sockios.h> 46#include <linux/net.h> 47#include <linux/in6.h> 48#include <linux/netdevice.h> 49#include <linux/if_addr.h> 50#include <linux/if_arp.h> 51#include <linux/if_arcnet.h> 52#include <linux/if_infiniband.h> 53#include <linux/route.h> 54#include <linux/inetdevice.h> 55#include <linux/init.h> 56#include <linux/slab.h> 57#ifdef CONFIG_SYSCTL 58#include <linux/sysctl.h> 59#endif 60#include <linux/capability.h> 61#include <linux/delay.h> 62#include <linux/notifier.h> 63#include <linux/string.h> 64 65#include <net/net_namespace.h> 66#include <net/sock.h> 67#include <net/snmp.h> 68 69#include <net/ipv6.h> 70#include <net/protocol.h> 71#include <net/ndisc.h> 72#include <net/ip6_route.h> 73#include <net/addrconf.h> 74#include <net/tcp.h> 75#include <net/ip.h> 76#include <net/netlink.h> 77#include <net/pkt_sched.h> 78#include <linux/if_tunnel.h> 79#include <linux/rtnetlink.h> 80 81#ifdef CONFIG_IPV6_PRIVACY 82#include <linux/random.h> 83#endif 84 85#include <linux/uaccess.h> 86#include <asm/unaligned.h> 87 88#include <linux/proc_fs.h> 89#include <linux/seq_file.h> 90 91/* Set to 3 to get tracing... */ 92#define ACONF_DEBUG 2 93 94#if ACONF_DEBUG >= 3 95#define ADBG(x) printk x 96#else 97#define ADBG(x) 98#endif 99 100#define INFINITY_LIFE_TIME 0xFFFFFFFF 101#define TIME_DELTA(a, b) ((unsigned long)((long)(a) - (long)(b))) 102 103#define ADDRCONF_TIMER_FUZZ_MINUS (HZ > 50 ? HZ/50 : 1) 104#define ADDRCONF_TIMER_FUZZ (HZ / 4) 105#define ADDRCONF_TIMER_FUZZ_MAX (HZ) 106 107#ifdef CONFIG_SYSCTL 108static void addrconf_sysctl_register(struct inet6_dev *idev); 109static void addrconf_sysctl_unregister(struct inet6_dev *idev); 110#else 111static inline void addrconf_sysctl_register(struct inet6_dev *idev) 112{ 113} 114 115static inline void addrconf_sysctl_unregister(struct inet6_dev *idev) 116{ 117} 118#endif 119 120#ifdef CONFIG_IPV6_PRIVACY 121static int __ipv6_regen_rndid(struct inet6_dev *idev); 122static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr); 123static void ipv6_regen_rndid(unsigned long data); 124#endif 125 126static int ipv6_generate_eui64(u8 *eui, struct net_device *dev); 127static int ipv6_count_addresses(struct inet6_dev *idev); 128 129/* 130 * Configured unicast address hash table 131 */ 132static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE]; 133static DEFINE_SPINLOCK(addrconf_hash_lock); 134 135static void addrconf_verify(unsigned long); 136 137static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0); 138static DEFINE_SPINLOCK(addrconf_verify_lock); 139 140static void addrconf_join_anycast(struct inet6_ifaddr *ifp); 141static void addrconf_leave_anycast(struct inet6_ifaddr *ifp); 142 143static void addrconf_type_change(struct net_device *dev, 144 unsigned long event); 145static int addrconf_ifdown(struct net_device *dev, int how); 146 147static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags); 148static void addrconf_dad_timer(unsigned long data); 149static void addrconf_dad_completed(struct inet6_ifaddr *ifp); 150static void addrconf_dad_run(struct inet6_dev *idev); 151/*Foxconn tab tseng add start, 2013/07/23, for dhcp6c wan DAD */ 152static struct proc_dir_entry *ipv6_wan_DAD_flag; 153/*Foxconn tab tseng add end, 2013/07/23, for dhcp6c wan DAD */ 154static void addrconf_rs_timer(unsigned long data); 155static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); 156static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); 157 158static void inet6_prefix_notify(int event, struct inet6_dev *idev, 159 struct prefix_info *pinfo); 160static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr, 161 struct net_device *dev); 162 163static ATOMIC_NOTIFIER_HEAD(inet6addr_chain); 164 165static struct ipv6_devconf ipv6_devconf __read_mostly = { 166 .forwarding = 0, 167 .hop_limit = IPV6_DEFAULT_HOPLIMIT, 168 .mtu6 = IPV6_MIN_MTU, 169 .accept_ra = 1, 170 .accept_redirects = 1, 171 .autoconf = 1, 172 .force_mld_version = 0, 173 .dad_transmits = 1, 174 .rtr_solicits = MAX_RTR_SOLICITATIONS, 175 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, 176 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, 177#ifdef CONFIG_IPV6_PRIVACY 178 .use_tempaddr = 0, 179 .temp_valid_lft = TEMP_VALID_LIFETIME, 180 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, 181 .regen_max_retry = REGEN_MAX_RETRY, 182 .max_desync_factor = MAX_DESYNC_FACTOR, 183#endif 184 .max_addresses = IPV6_MAX_ADDRESSES, 185 .accept_ra_defrtr = 1, 186 .accept_ra_pinfo = 1, 187#ifdef CONFIG_IPV6_ROUTER_PREF 188 .accept_ra_rtr_pref = 1, 189 .rtr_probe_interval = 60 * HZ, 190#ifdef CONFIG_IPV6_ROUTE_INFO 191 .accept_ra_rt_info_max_plen = 0, 192#endif 193#endif 194 .proxy_ndp = 0, 195 .accept_source_route = 0, /* we do not accept RH0 by default. */ 196 .disable_ipv6 = 0, 197 .accept_dad = 1, 198}; 199 200static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = { 201 .forwarding = 0, 202 .hop_limit = IPV6_DEFAULT_HOPLIMIT, 203 .mtu6 = IPV6_MIN_MTU, 204 .accept_ra = 1, 205 .accept_redirects = 1, 206 .autoconf = 1, 207 .dad_transmits = 1, 208 .rtr_solicits = MAX_RTR_SOLICITATIONS, 209 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, 210 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, 211#ifdef CONFIG_IPV6_PRIVACY 212 .use_tempaddr = 0, 213 .temp_valid_lft = TEMP_VALID_LIFETIME, 214 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, 215 .regen_max_retry = REGEN_MAX_RETRY, 216 .max_desync_factor = MAX_DESYNC_FACTOR, 217#endif 218 .max_addresses = IPV6_MAX_ADDRESSES, 219 .accept_ra_defrtr = 1, 220 .accept_ra_pinfo = 1, 221#ifdef CONFIG_IPV6_ROUTER_PREF 222 .accept_ra_rtr_pref = 1, 223 .rtr_probe_interval = 60 * HZ, 224#ifdef CONFIG_IPV6_ROUTE_INFO 225 .accept_ra_rt_info_max_plen = 0, 226#endif 227#endif 228 .proxy_ndp = 0, 229 .accept_source_route = 0, /* we do not accept RH0 by default. */ 230 .disable_ipv6 = 0, 231 .accept_dad = 1, 232}; 233 234/* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */ 235const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; 236const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT; 237const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT; 238const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT; 239 240/* Check if a valid qdisc is available */ 241static inline bool addrconf_qdisc_ok(const struct net_device *dev) 242{ 243 return !qdisc_tx_is_noop(dev); 244} 245 246/* Check if a route is valid prefix route */ 247static inline int addrconf_is_prefix_route(const struct rt6_info *rt) 248{ 249 return ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0); 250} 251 252static void addrconf_del_timer(struct inet6_ifaddr *ifp) 253{ 254 if (del_timer(&ifp->timer)) 255 __in6_ifa_put(ifp); 256} 257 258enum addrconf_timer_t { 259 AC_NONE, 260 AC_DAD, 261 AC_RS, 262}; 263 264static void addrconf_mod_timer(struct inet6_ifaddr *ifp, 265 enum addrconf_timer_t what, 266 unsigned long when) 267{ 268 if (!del_timer(&ifp->timer)) 269 in6_ifa_hold(ifp); 270 271 switch (what) { 272 case AC_DAD: 273 ifp->timer.function = addrconf_dad_timer; 274 break; 275 case AC_RS: 276 ifp->timer.function = addrconf_rs_timer; 277 break; 278 default: 279 break; 280 } 281 ifp->timer.expires = jiffies + when; 282 add_timer(&ifp->timer); 283} 284 285static int snmp6_alloc_dev(struct inet6_dev *idev) 286{ 287 if (snmp_mib_init((void __percpu **)idev->stats.ipv6, 288 sizeof(struct ipstats_mib), 289 __alignof__(struct ipstats_mib)) < 0) 290 goto err_ip; 291 if (snmp_mib_init((void __percpu **)idev->stats.icmpv6, 292 sizeof(struct icmpv6_mib), 293 __alignof__(struct icmpv6_mib)) < 0) 294 goto err_icmp; 295 if (snmp_mib_init((void __percpu **)idev->stats.icmpv6msg, 296 sizeof(struct icmpv6msg_mib), 297 __alignof__(struct icmpv6msg_mib)) < 0) 298 goto err_icmpmsg; 299 300 return 0; 301 302err_icmpmsg: 303 snmp_mib_free((void __percpu **)idev->stats.icmpv6); 304err_icmp: 305 snmp_mib_free((void __percpu **)idev->stats.ipv6); 306err_ip: 307 return -ENOMEM; 308} 309 310static void snmp6_free_dev(struct inet6_dev *idev) 311{ 312 snmp_mib_free((void __percpu **)idev->stats.icmpv6msg); 313 snmp_mib_free((void __percpu **)idev->stats.icmpv6); 314 snmp_mib_free((void __percpu **)idev->stats.ipv6); 315} 316 317/* Nobody refers to this device, we may destroy it. */ 318 319static void in6_dev_finish_destroy_rcu(struct rcu_head *head) 320{ 321 struct inet6_dev *idev = container_of(head, struct inet6_dev, rcu); 322 kfree(idev); 323} 324 325void in6_dev_finish_destroy(struct inet6_dev *idev) 326{ 327 struct net_device *dev = idev->dev; 328 329 WARN_ON(!list_empty(&idev->addr_list)); 330 WARN_ON(idev->mc_list != NULL); 331 332#ifdef NET_REFCNT_DEBUG 333 printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL"); 334#endif 335 dev_put(dev); 336 if (!idev->dead) { 337 pr_warning("Freeing alive inet6 device %p\n", idev); 338 return; 339 } 340 snmp6_free_dev(idev); 341 call_rcu(&idev->rcu, in6_dev_finish_destroy_rcu); 342} 343 344EXPORT_SYMBOL(in6_dev_finish_destroy); 345 346static struct inet6_dev * ipv6_add_dev(struct net_device *dev) 347{ 348 struct inet6_dev *ndev; 349 350 ASSERT_RTNL(); 351 352 if (dev->mtu < IPV6_MIN_MTU) 353 return NULL; 354 355 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL); 356 357 if (ndev == NULL) 358 return NULL; 359 360 rwlock_init(&ndev->lock); 361 ndev->dev = dev; 362 INIT_LIST_HEAD(&ndev->addr_list); 363 364 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf)); 365 ndev->cnf.mtu6 = dev->mtu; 366 ndev->cnf.sysctl = NULL; 367 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl); 368 if (ndev->nd_parms == NULL) { 369 kfree(ndev); 370 return NULL; 371 } 372 if (ndev->cnf.forwarding) 373 dev_disable_lro(dev); 374 /* We refer to the device */ 375 dev_hold(dev); 376 377 if (snmp6_alloc_dev(ndev) < 0) { 378 ADBG((KERN_WARNING 379 "%s(): cannot allocate memory for statistics; dev=%s.\n", 380 __func__, dev->name)); 381 neigh_parms_release(&nd_tbl, ndev->nd_parms); 382 ndev->dead = 1; 383 in6_dev_finish_destroy(ndev); 384 return NULL; 385 } 386 387 if (snmp6_register_dev(ndev) < 0) { 388 ADBG((KERN_WARNING 389 "%s(): cannot create /proc/net/dev_snmp6/%s\n", 390 __func__, dev->name)); 391 neigh_parms_release(&nd_tbl, ndev->nd_parms); 392 ndev->dead = 1; 393 in6_dev_finish_destroy(ndev); 394 return NULL; 395 } 396 397 /* One reference from device. We must do this before 398 * we invoke __ipv6_regen_rndid(). 399 */ 400 in6_dev_hold(ndev); 401 402 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) 403 ndev->cnf.accept_dad = -1; 404 405#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 406 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) { 407 printk(KERN_INFO 408 "%s: Disabled Multicast RS\n", 409 dev->name); 410 ndev->cnf.rtr_solicits = 0; 411 } 412#endif 413 414#ifdef CONFIG_IPV6_PRIVACY 415 INIT_LIST_HEAD(&ndev->tempaddr_list); 416 setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev); 417 if ((dev->flags&IFF_LOOPBACK) || 418 dev->type == ARPHRD_TUNNEL || 419 dev->type == ARPHRD_TUNNEL6 || 420 dev->type == ARPHRD_SIT || 421 dev->type == ARPHRD_NONE) { 422 printk(KERN_INFO 423 "%s: Disabled Privacy Extensions\n", 424 dev->name); 425 ndev->cnf.use_tempaddr = -1; 426 } else { 427 in6_dev_hold(ndev); 428 ipv6_regen_rndid((unsigned long) ndev); 429 } 430#endif 431 432 if (netif_running(dev) && addrconf_qdisc_ok(dev)) 433 ndev->if_flags |= IF_READY; 434 435 ipv6_mc_init_dev(ndev); 436 ndev->tstamp = jiffies; 437 addrconf_sysctl_register(ndev); 438 /* protected by rtnl_lock */ 439 rcu_assign_pointer(dev->ip6_ptr, ndev); 440 441 /* Join all-node multicast group */ 442 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes); 443 444 return ndev; 445} 446 447struct inet6_dev * ipv6_find_idev(struct net_device *dev) 448{ 449 struct inet6_dev *idev; 450 451 ASSERT_RTNL(); 452 453 idev = __in6_dev_get(dev); 454 if (!idev) { 455 idev = ipv6_add_dev(dev); 456 if (!idev) 457 return NULL; 458 } 459 460 if (dev->flags&IFF_UP) 461 ipv6_mc_up(idev); 462 return idev; 463} 464 465EXPORT_SYMBOL(ipv6_find_idev); 466 467#ifdef CONFIG_SYSCTL 468static void dev_forward_change(struct inet6_dev *idev) 469{ 470 struct net_device *dev; 471 struct inet6_ifaddr *ifa; 472 473 if (!idev) 474 return; 475 dev = idev->dev; 476 if (idev->cnf.forwarding) 477 dev_disable_lro(dev); 478 if (dev && (dev->flags & IFF_MULTICAST)) { 479 if (idev->cnf.forwarding) 480 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters); 481 else 482 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters); 483 } 484 485 list_for_each_entry(ifa, &idev->addr_list, if_list) { 486 if (ifa->flags&IFA_F_TENTATIVE) 487 continue; 488 if (idev->cnf.forwarding) 489 addrconf_join_anycast(ifa); 490 else 491 addrconf_leave_anycast(ifa); 492 } 493} 494 495 496static void addrconf_forward_change(struct net *net, __s32 newf) 497{ 498 struct net_device *dev; 499 struct inet6_dev *idev; 500 501 rcu_read_lock(); 502 for_each_netdev_rcu(net, dev) { 503 idev = __in6_dev_get(dev); 504 if (idev) { 505 int changed = (!idev->cnf.forwarding) ^ (!newf); 506 idev->cnf.forwarding = newf; 507 if (changed) 508 dev_forward_change(idev); 509 } 510 } 511 rcu_read_unlock(); 512} 513 514static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int old) 515{ 516 struct net *net; 517 518 net = (struct net *)table->extra2; 519 if (p == &net->ipv6.devconf_dflt->forwarding) 520 return 0; 521 522 if (!rtnl_trylock()) { 523 /* Restore the original values before restarting */ 524 *p = old; 525 return restart_syscall(); 526 } 527 528 if (p == &net->ipv6.devconf_all->forwarding) { 529 __s32 newf = net->ipv6.devconf_all->forwarding; 530 net->ipv6.devconf_dflt->forwarding = newf; 531 addrconf_forward_change(net, newf); 532 } else if ((!*p) ^ (!old)) 533 dev_forward_change((struct inet6_dev *)table->extra1); 534 rtnl_unlock(); 535 536 if (*p) 537 rt6_purge_dflt_routers(net); 538 return 1; 539} 540#endif 541 542static void inet6_ifa_finish_destroy_rcu(struct rcu_head *head) 543{ 544 struct inet6_ifaddr *ifp = container_of(head, struct inet6_ifaddr, rcu); 545 kfree(ifp); 546} 547 548/* Nobody refers to this ifaddr, destroy it */ 549void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp) 550{ 551 WARN_ON(!hlist_unhashed(&ifp->addr_lst)); 552 553#ifdef NET_REFCNT_DEBUG 554 printk(KERN_DEBUG "inet6_ifa_finish_destroy\n"); 555#endif 556 557 in6_dev_put(ifp->idev); 558 559 if (del_timer(&ifp->timer)) 560 pr_notice("Timer is still running, when freeing ifa=%p\n", ifp); 561 562 if (ifp->state != INET6_IFADDR_STATE_DEAD) { 563 pr_warning("Freeing alive inet6 address %p\n", ifp); 564 return; 565 } 566 dst_release(&ifp->rt->dst); 567 568 call_rcu(&ifp->rcu, inet6_ifa_finish_destroy_rcu); 569} 570 571static void 572ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp) 573{ 574 struct list_head *p; 575 int ifp_scope = ipv6_addr_src_scope(&ifp->addr); 576 577 /* 578 * Each device address list is sorted in order of scope - 579 * global before linklocal. 580 */ 581 list_for_each(p, &idev->addr_list) { 582 struct inet6_ifaddr *ifa 583 = list_entry(p, struct inet6_ifaddr, if_list); 584 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr)) 585 break; 586 } 587 588 list_add_tail(&ifp->if_list, p); 589} 590 591static u32 ipv6_addr_hash(const struct in6_addr *addr) 592{ 593 /* 594 * We perform the hash function over the last 64 bits of the address 595 * This will include the IEEE address token on links that support it. 596 */ 597 return jhash_2words((__force u32)addr->s6_addr32[2], 598 (__force u32)addr->s6_addr32[3], 0) 599 & (IN6_ADDR_HSIZE - 1); 600} 601 602/* On success it returns ifp with increased reference count */ 603 604static struct inet6_ifaddr * 605ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen, 606 int scope, u32 flags) 607{ 608 struct inet6_ifaddr *ifa = NULL; 609 struct rt6_info *rt; 610 unsigned int hash; 611 int err = 0; 612 int addr_type = ipv6_addr_type(addr); 613 614 if (addr_type == IPV6_ADDR_ANY || 615 addr_type & IPV6_ADDR_MULTICAST || 616 (!(idev->dev->flags & IFF_LOOPBACK) && 617 addr_type & IPV6_ADDR_LOOPBACK)) 618 return ERR_PTR(-EADDRNOTAVAIL); 619 620 rcu_read_lock_bh(); 621 if (idev->dead) { 622 err = -ENODEV; 623 goto out2; 624 } 625 626 if (idev->cnf.disable_ipv6) { 627 err = -EACCES; 628 goto out2; 629 } 630 631 spin_lock(&addrconf_hash_lock); 632 633 /* Ignore adding duplicate addresses on an interface */ 634 if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) { 635 ADBG(("ipv6_add_addr: already assigned\n")); 636 err = -EEXIST; 637 goto out; 638 } 639 640 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC); 641 642 if (ifa == NULL) { 643 ADBG(("ipv6_add_addr: malloc failed\n")); 644 err = -ENOBUFS; 645 goto out; 646 } 647 648 rt = addrconf_dst_alloc(idev, addr, 0); 649 if (IS_ERR(rt)) { 650 err = PTR_ERR(rt); 651 goto out; 652 } 653 654 ipv6_addr_copy(&ifa->addr, addr); 655 656 spin_lock_init(&ifa->lock); 657 spin_lock_init(&ifa->state_lock); 658 init_timer(&ifa->timer); 659 INIT_HLIST_NODE(&ifa->addr_lst); 660 ifa->timer.data = (unsigned long) ifa; 661 ifa->scope = scope; 662 ifa->prefix_len = pfxlen; 663 ifa->flags = flags | IFA_F_TENTATIVE; 664 ifa->cstamp = ifa->tstamp = jiffies; 665 666 ifa->rt = rt; 667 668 /* 669 * part one of RFC 4429, section 3.3 670 * We should not configure an address as 671 * optimistic if we do not yet know the link 672 * layer address of our nexhop router 673 */ 674 675 if (rt->rt6i_nexthop == NULL) 676 ifa->flags &= ~IFA_F_OPTIMISTIC; 677 678 ifa->idev = idev; 679 in6_dev_hold(idev); 680 /* For caller */ 681 in6_ifa_hold(ifa); 682 683 /* Add to big hash table */ 684 hash = ipv6_addr_hash(addr); 685 686 hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]); 687 spin_unlock(&addrconf_hash_lock); 688 689 write_lock(&idev->lock); 690 /* Add to inet6_dev unicast addr list. */ 691 ipv6_link_dev_addr(idev, ifa); 692 693#ifdef CONFIG_IPV6_PRIVACY 694 if (ifa->flags&IFA_F_TEMPORARY) { 695 list_add(&ifa->tmp_list, &idev->tempaddr_list); 696 in6_ifa_hold(ifa); 697 } 698#endif 699 700 in6_ifa_hold(ifa); 701 write_unlock(&idev->lock); 702out2: 703 rcu_read_unlock_bh(); 704 705 if (likely(err == 0)) 706 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa); 707 else { 708 kfree(ifa); 709 ifa = ERR_PTR(err); 710 } 711 712 return ifa; 713out: 714 spin_unlock(&addrconf_hash_lock); 715 goto out2; 716} 717 718/* This function wants to get referenced ifp and releases it before return */ 719 720static void ipv6_del_addr(struct inet6_ifaddr *ifp) 721{ 722 struct inet6_ifaddr *ifa, *ifn; 723 struct inet6_dev *idev = ifp->idev; 724 int state; 725 int hash; 726 int deleted = 0, onlink = 0; 727 unsigned long expires = jiffies; 728 729 hash = ipv6_addr_hash(&ifp->addr); 730 731 spin_lock_bh(&ifp->state_lock); 732 state = ifp->state; 733 ifp->state = INET6_IFADDR_STATE_DEAD; 734 spin_unlock_bh(&ifp->state_lock); 735 736 if (state == INET6_IFADDR_STATE_DEAD) 737 goto out; 738 739 spin_lock_bh(&addrconf_hash_lock); 740 hlist_del_init_rcu(&ifp->addr_lst); 741 spin_unlock_bh(&addrconf_hash_lock); 742 743 write_lock_bh(&idev->lock); 744#ifdef CONFIG_IPV6_PRIVACY 745 if (ifp->flags&IFA_F_TEMPORARY) { 746 list_del(&ifp->tmp_list); 747 if (ifp->ifpub) { 748 in6_ifa_put(ifp->ifpub); 749 ifp->ifpub = NULL; 750 } 751 __in6_ifa_put(ifp); 752 } 753#endif 754 755 list_for_each_entry_safe(ifa, ifn, &idev->addr_list, if_list) { 756 if (ifa == ifp) { 757 list_del_init(&ifp->if_list); 758 __in6_ifa_put(ifp); 759 760 if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0) 761 break; 762 deleted = 1; 763 continue; 764 } else if (ifp->flags & IFA_F_PERMANENT) { 765 if (ipv6_prefix_equal(&ifa->addr, &ifp->addr, 766 ifp->prefix_len)) { 767 if (ifa->flags & IFA_F_PERMANENT) { 768 onlink = 1; 769 if (deleted) 770 break; 771 } else { 772 unsigned long lifetime; 773 774 if (!onlink) 775 onlink = -1; 776 777 spin_lock(&ifa->lock); 778 779 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ); 780 /* 781 * Note: Because this address is 782 * not permanent, lifetime < 783 * LONG_MAX / HZ here. 784 */ 785 if (time_before(expires, 786 ifa->tstamp + lifetime * HZ)) 787 expires = ifa->tstamp + lifetime * HZ; 788 spin_unlock(&ifa->lock); 789 } 790 } 791 } 792 } 793 write_unlock_bh(&idev->lock); 794 795 addrconf_del_timer(ifp); 796 797 ipv6_ifa_notify(RTM_DELADDR, ifp); 798 799 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp); 800 801 /* 802 * Purge or update corresponding prefix 803 * 804 * 1) we don't purge prefix here if address was not permanent. 805 * prefix is managed by its own lifetime. 806 * 2) if there're no addresses, delete prefix. 807 * 3) if there're still other permanent address(es), 808 * corresponding prefix is still permanent. 809 * 4) otherwise, update prefix lifetime to the 810 * longest valid lifetime among the corresponding 811 * addresses on the device. 812 * Note: subsequent RA will update lifetime. 813 * 814 * --yoshfuji 815 */ 816 if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) { 817 struct in6_addr prefix; 818 struct rt6_info *rt; 819 struct net *net = dev_net(ifp->idev->dev); 820 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len); 821 rt = rt6_lookup(net, &prefix, NULL, ifp->idev->dev->ifindex, 1); 822 823 if (rt && addrconf_is_prefix_route(rt)) { 824 if (onlink == 0) { 825 ip6_del_rt(rt); 826 rt = NULL; 827 } else if (!(rt->rt6i_flags & RTF_EXPIRES)) { 828 rt->rt6i_expires = expires; 829 rt->rt6i_flags |= RTF_EXPIRES; 830 } 831 } 832 dst_release(&rt->dst); 833 } 834 835out: 836 in6_ifa_put(ifp); 837} 838 839#ifdef CONFIG_IPV6_PRIVACY 840static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift) 841{ 842 struct inet6_dev *idev = ifp->idev; 843 struct in6_addr addr, *tmpaddr; 844 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp; 845 unsigned long regen_advance; 846 int tmp_plen; 847 int ret = 0; 848 int max_addresses; 849 u32 addr_flags; 850 851 write_lock(&idev->lock); 852 if (ift) { 853 spin_lock_bh(&ift->lock); 854 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8); 855 spin_unlock_bh(&ift->lock); 856 tmpaddr = &addr; 857 } else { 858 tmpaddr = NULL; 859 } 860retry: 861 in6_dev_hold(idev); 862 if (idev->cnf.use_tempaddr <= 0) { 863 write_unlock(&idev->lock); 864 printk(KERN_INFO 865 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n"); 866 in6_dev_put(idev); 867 ret = -1; 868 goto out; 869 } 870 spin_lock_bh(&ifp->lock); 871 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) { 872 idev->cnf.use_tempaddr = -1; 873 spin_unlock_bh(&ifp->lock); 874 write_unlock(&idev->lock); 875 printk(KERN_WARNING 876 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n"); 877 in6_dev_put(idev); 878 ret = -1; 879 goto out; 880 } 881 in6_ifa_hold(ifp); 882 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8); 883 if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) { 884 spin_unlock_bh(&ifp->lock); 885 write_unlock(&idev->lock); 886 printk(KERN_WARNING 887 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n"); 888 in6_ifa_put(ifp); 889 in6_dev_put(idev); 890 ret = -1; 891 goto out; 892 } 893 memcpy(&addr.s6_addr[8], idev->rndid, 8); 894 tmp_valid_lft = min_t(__u32, 895 ifp->valid_lft, 896 idev->cnf.temp_valid_lft); 897 tmp_prefered_lft = min_t(__u32, 898 ifp->prefered_lft, 899 idev->cnf.temp_prefered_lft - 900 idev->cnf.max_desync_factor); 901 tmp_plen = ifp->prefix_len; 902 max_addresses = idev->cnf.max_addresses; 903 tmp_cstamp = ifp->cstamp; 904 tmp_tstamp = ifp->tstamp; 905 spin_unlock_bh(&ifp->lock); 906 907 regen_advance = idev->cnf.regen_max_retry * 908 idev->cnf.dad_transmits * 909 idev->nd_parms->retrans_time / HZ; 910 write_unlock(&idev->lock); 911 912 /* A temporary address is created only if this calculated Preferred 913 * Lifetime is greater than REGEN_ADVANCE time units. In particular, 914 * an implementation must not create a temporary address with a zero 915 * Preferred Lifetime. 916 */ 917 if (tmp_prefered_lft <= regen_advance) { 918 in6_ifa_put(ifp); 919 in6_dev_put(idev); 920 ret = -1; 921 goto out; 922 } 923 924 addr_flags = IFA_F_TEMPORARY; 925 /* set in addrconf_prefix_rcv() */ 926 if (ifp->flags & IFA_F_OPTIMISTIC) 927 addr_flags |= IFA_F_OPTIMISTIC; 928 929 ift = !max_addresses || 930 ipv6_count_addresses(idev) < max_addresses ? 931 ipv6_add_addr(idev, &addr, tmp_plen, 932 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK, 933 addr_flags) : NULL; 934 if (!ift || IS_ERR(ift)) { 935 in6_ifa_put(ifp); 936 in6_dev_put(idev); 937 printk(KERN_INFO 938 "ipv6_create_tempaddr(): retry temporary address regeneration.\n"); 939 tmpaddr = &addr; 940 write_lock(&idev->lock); 941 goto retry; 942 } 943 944 spin_lock_bh(&ift->lock); 945 ift->ifpub = ifp; 946 ift->valid_lft = tmp_valid_lft; 947 ift->prefered_lft = tmp_prefered_lft; 948 ift->cstamp = tmp_cstamp; 949 ift->tstamp = tmp_tstamp; 950 spin_unlock_bh(&ift->lock); 951 952 addrconf_dad_start(ift, 0); 953 in6_ifa_put(ift); 954 in6_dev_put(idev); 955out: 956 return ret; 957} 958#endif 959 960/* 961 * Choose an appropriate source address (RFC3484) 962 */ 963enum { 964 IPV6_SADDR_RULE_INIT = 0, 965 IPV6_SADDR_RULE_LOCAL, 966 IPV6_SADDR_RULE_SCOPE, 967 IPV6_SADDR_RULE_PREFERRED, 968#ifdef CONFIG_IPV6_MIP6 969 IPV6_SADDR_RULE_HOA, 970#endif 971 IPV6_SADDR_RULE_OIF, 972 IPV6_SADDR_RULE_LABEL, 973#ifdef CONFIG_IPV6_PRIVACY 974 IPV6_SADDR_RULE_PRIVACY, 975#endif 976 IPV6_SADDR_RULE_ORCHID, 977 IPV6_SADDR_RULE_PREFIX, 978 IPV6_SADDR_RULE_MAX 979}; 980 981struct ipv6_saddr_score { 982 int rule; 983 int addr_type; 984 struct inet6_ifaddr *ifa; 985 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX); 986 int scopedist; 987 int matchlen; 988}; 989 990struct ipv6_saddr_dst { 991 const struct in6_addr *addr; 992 int ifindex; 993 int scope; 994 int label; 995 unsigned int prefs; 996}; 997 998static inline int ipv6_saddr_preferred(int type) 999{ 1000 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK)) 1001 return 1; 1002 return 0; 1003} 1004 1005static int ipv6_get_saddr_eval(struct net *net, 1006 struct ipv6_saddr_score *score, 1007 struct ipv6_saddr_dst *dst, 1008 int i) 1009{ 1010 int ret; 1011 1012 if (i <= score->rule) { 1013 switch (i) { 1014 case IPV6_SADDR_RULE_SCOPE: 1015 ret = score->scopedist; 1016 break; 1017 case IPV6_SADDR_RULE_PREFIX: 1018 ret = score->matchlen; 1019 break; 1020 default: 1021 ret = !!test_bit(i, score->scorebits); 1022 } 1023 goto out; 1024 } 1025 1026 switch (i) { 1027 case IPV6_SADDR_RULE_INIT: 1028 /* Rule 0: remember if hiscore is not ready yet */ 1029 ret = !!score->ifa; 1030 break; 1031 case IPV6_SADDR_RULE_LOCAL: 1032 /* Rule 1: Prefer same address */ 1033 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr); 1034 break; 1035 case IPV6_SADDR_RULE_SCOPE: 1036 /* Rule 2: Prefer appropriate scope 1037 * 1038 * ret 1039 * ^ 1040 * -1 | d 15 1041 * ---+--+-+---> scope 1042 * | 1043 * | d is scope of the destination. 1044 * B-d | \ 1045 * | \ <- smaller scope is better if 1046 * B-15 | \ if scope is enough for destinaion. 1047 * | ret = B - scope (-1 <= scope >= d <= 15). 1048 * d-C-1 | / 1049 * |/ <- greater is better 1050 * -C / if scope is not enough for destination. 1051 * /| ret = scope - C (-1 <= d < scope <= 15). 1052 * 1053 * d - C - 1 < B -15 (for all -1 <= d <= 15). 1054 * C > d + 14 - B >= 15 + 14 - B = 29 - B. 1055 * Assume B = 0 and we get C > 29. 1056 */ 1057 ret = __ipv6_addr_src_scope(score->addr_type); 1058 if (ret >= dst->scope) 1059 ret = -ret; 1060 else 1061 ret -= 128; /* 30 is enough */ 1062 score->scopedist = ret; 1063 break; 1064 case IPV6_SADDR_RULE_PREFERRED: 1065 /* Rule 3: Avoid deprecated and optimistic addresses */ 1066 ret = ipv6_saddr_preferred(score->addr_type) || 1067 !(score->ifa->flags & (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC)); 1068 break; 1069#ifdef CONFIG_IPV6_MIP6 1070 case IPV6_SADDR_RULE_HOA: 1071 { 1072 /* Rule 4: Prefer home address */ 1073 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA); 1074 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome; 1075 break; 1076 } 1077#endif 1078 case IPV6_SADDR_RULE_OIF: 1079 /* Rule 5: Prefer outgoing interface */ 1080 ret = (!dst->ifindex || 1081 dst->ifindex == score->ifa->idev->dev->ifindex); 1082 break; 1083 case IPV6_SADDR_RULE_LABEL: 1084 /* Rule 6: Prefer matching label */ 1085 ret = ipv6_addr_label(net, 1086 &score->ifa->addr, score->addr_type, 1087 score->ifa->idev->dev->ifindex) == dst->label; 1088 break; 1089#ifdef CONFIG_IPV6_PRIVACY 1090 case IPV6_SADDR_RULE_PRIVACY: 1091 { 1092 /* Rule 7: Prefer public address 1093 * Note: prefer temprary address if use_tempaddr >= 2 1094 */ 1095 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ? 1096 !!(dst->prefs & IPV6_PREFER_SRC_TMP) : 1097 score->ifa->idev->cnf.use_tempaddr >= 2; 1098 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp; 1099 break; 1100 } 1101#endif 1102 case IPV6_SADDR_RULE_ORCHID: 1103 /* Rule 8-: Prefer ORCHID vs ORCHID or 1104 * non-ORCHID vs non-ORCHID 1105 */ 1106 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^ 1107 ipv6_addr_orchid(dst->addr)); 1108 break; 1109 case IPV6_SADDR_RULE_PREFIX: 1110 /* Rule 8: Use longest matching prefix */ 1111 score->matchlen = ret = ipv6_addr_diff(&score->ifa->addr, 1112 dst->addr); 1113 break; 1114 default: 1115 ret = 0; 1116 } 1117 1118 if (ret) 1119 __set_bit(i, score->scorebits); 1120 score->rule = i; 1121out: 1122 return ret; 1123} 1124 1125int ipv6_dev_get_saddr(struct net *net, struct net_device *dst_dev, 1126 const struct in6_addr *daddr, unsigned int prefs, 1127 struct in6_addr *saddr) 1128{ 1129 struct ipv6_saddr_score scores[2], 1130 *score = &scores[0], *hiscore = &scores[1]; 1131 struct ipv6_saddr_dst dst; 1132 struct net_device *dev; 1133 int dst_type; 1134 1135 dst_type = __ipv6_addr_type(daddr); 1136 dst.addr = daddr; 1137 dst.ifindex = dst_dev ? dst_dev->ifindex : 0; 1138 dst.scope = __ipv6_addr_src_scope(dst_type); 1139 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex); 1140 dst.prefs = prefs; 1141 1142 hiscore->rule = -1; 1143 hiscore->ifa = NULL; 1144 1145 rcu_read_lock(); 1146 1147 for_each_netdev_rcu(net, dev) { 1148 struct inet6_dev *idev; 1149 1150 /* Candidate Source Address (section 4) 1151 * - multicast and link-local destination address, 1152 * the set of candidate source address MUST only 1153 * include addresses assigned to interfaces 1154 * belonging to the same link as the outgoing 1155 * interface. 1156 * (- For site-local destination addresses, the 1157 * set of candidate source addresses MUST only 1158 * include addresses assigned to interfaces 1159 * belonging to the same site as the outgoing 1160 * interface.) 1161 */ 1162 if (((dst_type & IPV6_ADDR_MULTICAST) || 1163 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL) && 1164 dst.ifindex && dev->ifindex != dst.ifindex) 1165 continue; 1166 1167 idev = __in6_dev_get(dev); 1168 if (!idev) 1169 continue; 1170 1171 read_lock_bh(&idev->lock); 1172 list_for_each_entry(score->ifa, &idev->addr_list, if_list) { 1173 int i; 1174 1175 /* 1176 * - Tentative Address (RFC2462 section 5.4) 1177 * - A tentative address is not considered 1178 * "assigned to an interface" in the traditional 1179 * sense, unless it is also flagged as optimistic. 1180 * - Candidate Source Address (section 4) 1181 * - In any case, anycast addresses, multicast 1182 * addresses, and the unspecified address MUST 1183 * NOT be included in a candidate set. 1184 */ 1185 if ((score->ifa->flags & IFA_F_TENTATIVE) && 1186 (!(score->ifa->flags & IFA_F_OPTIMISTIC))) 1187 continue; 1188 1189 score->addr_type = __ipv6_addr_type(&score->ifa->addr); 1190 1191 if (unlikely(score->addr_type == IPV6_ADDR_ANY || 1192 score->addr_type & IPV6_ADDR_MULTICAST)) { 1193 LIMIT_NETDEBUG(KERN_DEBUG 1194 "ADDRCONF: unspecified / multicast address " 1195 "assigned as unicast address on %s", 1196 dev->name); 1197 continue; 1198 } 1199 1200 score->rule = -1; 1201 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX); 1202 1203 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) { 1204 int minihiscore, miniscore; 1205 1206 minihiscore = ipv6_get_saddr_eval(net, hiscore, &dst, i); 1207 miniscore = ipv6_get_saddr_eval(net, score, &dst, i); 1208 1209 if (minihiscore > miniscore) { 1210 if (i == IPV6_SADDR_RULE_SCOPE && 1211 score->scopedist > 0) { 1212 /* 1213 * special case: 1214 * each remaining entry 1215 * has too small (not enough) 1216 * scope, because ifa entries 1217 * are sorted by their scope 1218 * values. 1219 */ 1220 goto try_nextdev; 1221 } 1222 break; 1223 } else if (minihiscore < miniscore) { 1224 if (hiscore->ifa) 1225 in6_ifa_put(hiscore->ifa); 1226 1227 in6_ifa_hold(score->ifa); 1228 1229 swap(hiscore, score); 1230 1231 /* restore our iterator */ 1232 score->ifa = hiscore->ifa; 1233 1234 break; 1235 } 1236 } 1237 } 1238try_nextdev: 1239 read_unlock_bh(&idev->lock); 1240 } 1241 rcu_read_unlock(); 1242 1243 if (!hiscore->ifa) 1244 return -EADDRNOTAVAIL; 1245 1246 ipv6_addr_copy(saddr, &hiscore->ifa->addr); 1247 in6_ifa_put(hiscore->ifa); 1248 return 0; 1249} 1250EXPORT_SYMBOL(ipv6_dev_get_saddr); 1251 1252int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr, 1253 unsigned char banned_flags) 1254{ 1255 struct inet6_dev *idev; 1256 int err = -EADDRNOTAVAIL; 1257 1258 rcu_read_lock(); 1259 idev = __in6_dev_get(dev); 1260 if (idev) { 1261 struct inet6_ifaddr *ifp; 1262 1263 read_lock_bh(&idev->lock); 1264 list_for_each_entry(ifp, &idev->addr_list, if_list) { 1265 if (ifp->scope == IFA_LINK && 1266 !(ifp->flags & banned_flags)) { 1267 ipv6_addr_copy(addr, &ifp->addr); 1268 err = 0; 1269 break; 1270 } 1271 } 1272 read_unlock_bh(&idev->lock); 1273 } 1274 rcu_read_unlock(); 1275 return err; 1276} 1277 1278static int ipv6_count_addresses(struct inet6_dev *idev) 1279{ 1280 int cnt = 0; 1281 struct inet6_ifaddr *ifp; 1282 1283 read_lock_bh(&idev->lock); 1284 list_for_each_entry(ifp, &idev->addr_list, if_list) 1285 cnt++; 1286 read_unlock_bh(&idev->lock); 1287 return cnt; 1288} 1289 1290int ipv6_chk_addr(struct net *net, struct in6_addr *addr, 1291 struct net_device *dev, int strict) 1292{ 1293 struct inet6_ifaddr *ifp; 1294 struct hlist_node *node; 1295 unsigned int hash = ipv6_addr_hash(addr); 1296 1297 rcu_read_lock_bh(); 1298 hlist_for_each_entry_rcu(ifp, node, &inet6_addr_lst[hash], addr_lst) { 1299 if (!net_eq(dev_net(ifp->idev->dev), net)) 1300 continue; 1301 if (ipv6_addr_equal(&ifp->addr, addr) && 1302 !(ifp->flags&IFA_F_TENTATIVE) && 1303 (dev == NULL || ifp->idev->dev == dev || 1304 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) { 1305 rcu_read_unlock_bh(); 1306 return 1; 1307 } 1308 } 1309 1310 rcu_read_unlock_bh(); 1311 return 0; 1312} 1313EXPORT_SYMBOL(ipv6_chk_addr); 1314 1315static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr, 1316 struct net_device *dev) 1317{ 1318 unsigned int hash = ipv6_addr_hash(addr); 1319 struct inet6_ifaddr *ifp; 1320 struct hlist_node *node; 1321 1322 hlist_for_each_entry(ifp, node, &inet6_addr_lst[hash], addr_lst) { 1323 if (!net_eq(dev_net(ifp->idev->dev), net)) 1324 continue; 1325 if (ipv6_addr_equal(&ifp->addr, addr)) { 1326 if (dev == NULL || ifp->idev->dev == dev) 1327 return true; 1328 } 1329 } 1330 return false; 1331} 1332 1333int ipv6_chk_prefix(struct in6_addr *addr, struct net_device *dev) 1334{ 1335 struct inet6_dev *idev; 1336 struct inet6_ifaddr *ifa; 1337 int onlink; 1338 1339 onlink = 0; 1340 rcu_read_lock(); 1341 idev = __in6_dev_get(dev); 1342 if (idev) { 1343 read_lock_bh(&idev->lock); 1344 list_for_each_entry(ifa, &idev->addr_list, if_list) { 1345 onlink = ipv6_prefix_equal(addr, &ifa->addr, 1346 ifa->prefix_len); 1347 if (onlink) 1348 break; 1349 } 1350 read_unlock_bh(&idev->lock); 1351 } 1352 rcu_read_unlock(); 1353 return onlink; 1354} 1355 1356EXPORT_SYMBOL(ipv6_chk_prefix); 1357 1358struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr, 1359 struct net_device *dev, int strict) 1360{ 1361 struct inet6_ifaddr *ifp, *result = NULL; 1362 unsigned int hash = ipv6_addr_hash(addr); 1363 struct hlist_node *node; 1364 1365 rcu_read_lock_bh(); 1366 hlist_for_each_entry_rcu_bh(ifp, node, &inet6_addr_lst[hash], addr_lst) { 1367 if (!net_eq(dev_net(ifp->idev->dev), net)) 1368 continue; 1369 if (ipv6_addr_equal(&ifp->addr, addr)) { 1370 if (dev == NULL || ifp->idev->dev == dev || 1371 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) { 1372 result = ifp; 1373 in6_ifa_hold(ifp); 1374 break; 1375 } 1376 } 1377 } 1378 rcu_read_unlock_bh(); 1379 1380 return result; 1381} 1382 1383/* Gets referenced address, destroys ifaddr */ 1384 1385/* Foxconn added start pling 10/27/2009 */ 1386extern const char lan_if_name[]; 1387extern const char wan_if_name[]; 1388extern int lan_dad_detected; 1389extern int wan_dad_detected; 1390/* Foxconn added end pling 10/27/2009 */ 1391/*Foxconn tab tseng added, 2013/07/23, for dhcp6c wan ipv6 DAD*/ 1392/* 1393 * Create "/proc/net/ipv6_wan_DAD_detected" entry 1394 */ 1395int ipv6_read_wan_DAD_stats(char *buffer, char **start, off_t offset, int length, int *eof, void *data) 1396{ 1397 int len = 0; 1398 char temp[8]; 1399 memset(temp, 0, sizeof(temp)); 1400 buffer[0] = '\0'; 1401 sprintf(temp,"%d",wan_dad_detected); 1402 strcat(buffer, temp); 1403 len += strlen(buffer) + 1; 1404 *eof = 1; 1405 return len; 1406} 1407/*Foxconn tab tseng added, 2013/07/23, for dhcp6c wan ipv6 DAD*/ 1408/* Foxconn added start pling 11/29/2010 */ 1409static struct in6_addr dad_wan_ip_addr; 1410/* Foxconn added end pling 11/29/2010 */ 1411 1412static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed) 1413{ 1414 if (ifp->flags&IFA_F_PERMANENT) { 1415 spin_lock_bh(&ifp->lock); 1416 addrconf_del_timer(ifp); 1417 ifp->flags |= IFA_F_TENTATIVE; 1418 if (dad_failed) 1419 ifp->flags |= IFA_F_DADFAILED; 1420 spin_unlock_bh(&ifp->lock); 1421 if (dad_failed) 1422 ipv6_ifa_notify(0, ifp); 1423 in6_ifa_put(ifp); 1424 1425 /* Foxconn modified start pling 08/16/2010 */ 1426 /* Disable IPv6 forwarding if DAD is detected */ 1427 if (strcmp(ifp->idev->dev->name, lan_if_name) == 0) 1428 { 1429 ipv6_devconf.forwarding = 0; 1430 ifp->idev->cnf.forwarding = 0; 1431 lan_dad_detected = 1; 1432 } 1433 else if (strcmp(ifp->idev->dev->name, wan_if_name) == 0) 1434 { 1435 ipv6_devconf.forwarding = 0; 1436 ifp->idev->cnf.forwarding = 0; 1437 wan_dad_detected = 1; 1438 1439 /* Foxconn added start pling 11/29/2010 */ 1440 /* WNR3500L TD175: After duplicate IP detected, 1441 * kernel can't send NS anymore. So for subsequent IPv6 1442 * assignment from RA/DHCPv6 server, router can't 1443 * do DAD correctly. 1444 * Current workaround: save the DAD IP. When DHCPv6 client 1445 * later remove this IPv6 address, clear the DAD flag. 1446 */ 1447 memcpy(&dad_wan_ip_addr, &(ifp->addr), sizeof(dad_wan_ip_addr)); 1448 /* Foxconn added end pling 11/29/2010 */ 1449 } 1450 /* Foxconn modifed end pling 08/16/2010 */ 1451 1452#ifdef CONFIG_IPV6_PRIVACY 1453 } else if (ifp->flags&IFA_F_TEMPORARY) { 1454 struct inet6_ifaddr *ifpub; 1455 spin_lock_bh(&ifp->lock); 1456 ifpub = ifp->ifpub; 1457 if (ifpub) { 1458 in6_ifa_hold(ifpub); 1459 spin_unlock_bh(&ifp->lock); 1460 ipv6_create_tempaddr(ifpub, ifp); 1461 in6_ifa_put(ifpub); 1462 } else { 1463 spin_unlock_bh(&ifp->lock); 1464 } 1465 ipv6_del_addr(ifp); 1466#endif 1467 } else 1468 ipv6_del_addr(ifp); 1469} 1470 1471static int addrconf_dad_end(struct inet6_ifaddr *ifp) 1472{ 1473 int err = -ENOENT; 1474 1475 spin_lock(&ifp->state_lock); 1476 if (ifp->state == INET6_IFADDR_STATE_DAD) { 1477 ifp->state = INET6_IFADDR_STATE_POSTDAD; 1478 err = 0; 1479 } 1480 spin_unlock(&ifp->state_lock); 1481 1482 return err; 1483} 1484 1485void addrconf_dad_failure(struct inet6_ifaddr *ifp) 1486{ 1487 struct inet6_dev *idev = ifp->idev; 1488 1489 if (addrconf_dad_end(ifp)) 1490 return; 1491 1492 if (net_ratelimit()) 1493 printk(KERN_INFO "%s: IPv6 duplicate address %pI6c detected!\n", 1494 ifp->idev->dev->name, &ifp->addr); 1495 1496 if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) { 1497 struct in6_addr addr; 1498 1499 addr.s6_addr32[0] = htonl(0xfe800000); 1500 addr.s6_addr32[1] = 0; 1501 1502 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) && 1503 ipv6_addr_equal(&ifp->addr, &addr)) { 1504 /* DAD failed for link-local based on MAC address */ 1505 idev->cnf.disable_ipv6 = 1; 1506 1507 printk(KERN_INFO "%s: IPv6 being disabled!\n", 1508 ifp->idev->dev->name); 1509 } 1510 } 1511 1512 addrconf_dad_stop(ifp, 1); 1513} 1514 1515/* Join to solicited addr multicast group. */ 1516 1517void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr) 1518{ 1519 struct in6_addr maddr; 1520 1521 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 1522 return; 1523 1524 addrconf_addr_solict_mult(addr, &maddr); 1525 ipv6_dev_mc_inc(dev, &maddr); 1526} 1527 1528void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr) 1529{ 1530 struct in6_addr maddr; 1531 1532 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 1533 return; 1534 1535 addrconf_addr_solict_mult(addr, &maddr); 1536 __ipv6_dev_mc_dec(idev, &maddr); 1537} 1538 1539static void addrconf_join_anycast(struct inet6_ifaddr *ifp) 1540{ 1541 struct in6_addr addr; 1542 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 1543 if (ipv6_addr_any(&addr)) 1544 return; 1545 ipv6_dev_ac_inc(ifp->idev->dev, &addr); 1546} 1547 1548static void addrconf_leave_anycast(struct inet6_ifaddr *ifp) 1549{ 1550 struct in6_addr addr; 1551 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 1552 if (ipv6_addr_any(&addr)) 1553 return; 1554 __ipv6_dev_ac_dec(ifp->idev, &addr); 1555} 1556 1557static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev) 1558{ 1559 if (dev->addr_len != ETH_ALEN) 1560 return -1; 1561 memcpy(eui, dev->dev_addr, 3); 1562 memcpy(eui + 5, dev->dev_addr + 3, 3); 1563 1564 /* 1565 * The zSeries OSA network cards can be shared among various 1566 * OS instances, but the OSA cards have only one MAC address. 1567 * This leads to duplicate address conflicts in conjunction 1568 * with IPv6 if more than one instance uses the same card. 1569 * 1570 * The driver for these cards can deliver a unique 16-bit 1571 * identifier for each instance sharing the same card. It is 1572 * placed instead of 0xFFFE in the interface identifier. The 1573 * "u" bit of the interface identifier is not inverted in this 1574 * case. Hence the resulting interface identifier has local 1575 * scope according to RFC2373. 1576 */ 1577 if (dev->dev_id) { 1578 eui[3] = (dev->dev_id >> 8) & 0xFF; 1579 eui[4] = dev->dev_id & 0xFF; 1580 } else { 1581 eui[3] = 0xFF; 1582 eui[4] = 0xFE; 1583 eui[0] ^= 2; 1584 } 1585 return 0; 1586} 1587 1588static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev) 1589{ 1590 if (dev->addr_len != ARCNET_ALEN) 1591 return -1; 1592 memset(eui, 0, 7); 1593 eui[7] = *(u8*)dev->dev_addr; 1594 return 0; 1595} 1596 1597static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev) 1598{ 1599 if (dev->addr_len != INFINIBAND_ALEN) 1600 return -1; 1601 memcpy(eui, dev->dev_addr + 12, 8); 1602 eui[0] |= 2; 1603 return 0; 1604} 1605 1606int __ipv6_isatap_ifid(u8 *eui, __be32 addr) 1607{ 1608 if (addr == 0) 1609 return -1; 1610 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) || 1611 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) || 1612 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) || 1613 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) || 1614 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) || 1615 ipv4_is_lbcast(addr)) ? 0x00 : 0x02; 1616 eui[1] = 0; 1617 eui[2] = 0x5E; 1618 eui[3] = 0xFE; 1619 memcpy(eui + 4, &addr, 4); 1620 return 0; 1621} 1622EXPORT_SYMBOL(__ipv6_isatap_ifid); 1623 1624static int addrconf_ifid_sit(u8 *eui, struct net_device *dev) 1625{ 1626 if (dev->priv_flags & IFF_ISATAP) 1627 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); 1628 return -1; 1629} 1630 1631static int ipv6_generate_eui64(u8 *eui, struct net_device *dev) 1632{ 1633 switch (dev->type) { 1634 case ARPHRD_ETHER: 1635 case ARPHRD_FDDI: 1636 case ARPHRD_IEEE802_TR: 1637 return addrconf_ifid_eui48(eui, dev); 1638 case ARPHRD_ARCNET: 1639 return addrconf_ifid_arcnet(eui, dev); 1640 case ARPHRD_INFINIBAND: 1641 return addrconf_ifid_infiniband(eui, dev); 1642 case ARPHRD_SIT: 1643 return addrconf_ifid_sit(eui, dev); 1644 } 1645 return -1; 1646} 1647 1648static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev) 1649{ 1650 int err = -1; 1651 struct inet6_ifaddr *ifp; 1652 1653 read_lock_bh(&idev->lock); 1654 list_for_each_entry(ifp, &idev->addr_list, if_list) { 1655 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) { 1656 memcpy(eui, ifp->addr.s6_addr+8, 8); 1657 err = 0; 1658 break; 1659 } 1660 } 1661 read_unlock_bh(&idev->lock); 1662 return err; 1663} 1664 1665#ifdef CONFIG_IPV6_PRIVACY 1666/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */ 1667static int __ipv6_regen_rndid(struct inet6_dev *idev) 1668{ 1669regen: 1670 get_random_bytes(idev->rndid, sizeof(idev->rndid)); 1671 idev->rndid[0] &= ~0x02; 1672 1673 if (idev->rndid[0] == 0xfd && 1674 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff && 1675 (idev->rndid[7]&0x80)) 1676 goto regen; 1677 if ((idev->rndid[0]|idev->rndid[1]) == 0) { 1678 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe) 1679 goto regen; 1680 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00) 1681 goto regen; 1682 } 1683 1684 return 0; 1685} 1686 1687static void ipv6_regen_rndid(unsigned long data) 1688{ 1689 struct inet6_dev *idev = (struct inet6_dev *) data; 1690 unsigned long expires; 1691 1692 rcu_read_lock_bh(); 1693 write_lock_bh(&idev->lock); 1694 1695 if (idev->dead) 1696 goto out; 1697 1698 if (__ipv6_regen_rndid(idev) < 0) 1699 goto out; 1700 1701 expires = jiffies + 1702 idev->cnf.temp_prefered_lft * HZ - 1703 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - 1704 idev->cnf.max_desync_factor * HZ; 1705 if (time_before(expires, jiffies)) { 1706 printk(KERN_WARNING 1707 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n", 1708 idev->dev->name); 1709 goto out; 1710 } 1711 1712 if (!mod_timer(&idev->regen_timer, expires)) 1713 in6_dev_hold(idev); 1714 1715out: 1716 write_unlock_bh(&idev->lock); 1717 rcu_read_unlock_bh(); 1718 in6_dev_put(idev); 1719} 1720 1721static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) { 1722 int ret = 0; 1723 1724 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0) 1725 ret = __ipv6_regen_rndid(idev); 1726 return ret; 1727} 1728#endif 1729 1730/* 1731 * Add prefix route. 1732 */ 1733 1734static void 1735addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev, 1736 unsigned long expires, u32 flags) 1737{ 1738 struct fib6_config cfg = { 1739 .fc_table = RT6_TABLE_PREFIX, 1740 .fc_metric = IP6_RT_PRIO_ADDRCONF, 1741 .fc_ifindex = dev->ifindex, 1742 .fc_expires = expires, 1743 .fc_dst_len = plen, 1744 .fc_flags = RTF_UP | flags, 1745 .fc_nlinfo.nl_net = dev_net(dev), 1746 .fc_protocol = RTPROT_KERNEL, 1747 }; 1748 1749 ipv6_addr_copy(&cfg.fc_dst, pfx); 1750 1751 /* Prevent useless cloning on PtP SIT. 1752 This thing is done here expecting that the whole 1753 class of non-broadcast devices need not cloning. 1754 */ 1755#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 1756 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT)) 1757 cfg.fc_flags |= RTF_NONEXTHOP; 1758#endif 1759 1760 ip6_route_add(&cfg); 1761} 1762 1763/* Create "default" multicast route to the interface */ 1764 1765static void addrconf_add_mroute(struct net_device *dev) 1766{ 1767 struct fib6_config cfg = { 1768 .fc_table = RT6_TABLE_LOCAL, 1769 .fc_metric = IP6_RT_PRIO_ADDRCONF, 1770 .fc_ifindex = dev->ifindex, 1771 .fc_dst_len = 8, 1772 .fc_flags = RTF_UP, 1773 .fc_nlinfo.nl_net = dev_net(dev), 1774 }; 1775 1776 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0); 1777 1778 ip6_route_add(&cfg); 1779} 1780 1781#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 1782static void sit_route_add(struct net_device *dev) 1783{ 1784 struct fib6_config cfg = { 1785 .fc_table = RT6_TABLE_MAIN, 1786 .fc_metric = IP6_RT_PRIO_ADDRCONF, 1787 .fc_ifindex = dev->ifindex, 1788 .fc_dst_len = 96, 1789 .fc_flags = RTF_UP | RTF_NONEXTHOP, 1790 .fc_nlinfo.nl_net = dev_net(dev), 1791 }; 1792 1793 /* prefix length - 96 bits "::d.d.d.d" */ 1794 ip6_route_add(&cfg); 1795} 1796#endif 1797 1798static void addrconf_add_lroute(struct net_device *dev) 1799{ 1800 struct in6_addr addr; 1801 1802 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0); 1803 addrconf_prefix_route(&addr, 64, dev, 0, 0); 1804} 1805 1806static struct inet6_dev *addrconf_add_dev(struct net_device *dev) 1807{ 1808 struct inet6_dev *idev; 1809 1810 ASSERT_RTNL(); 1811 1812 idev = ipv6_find_idev(dev); 1813 if (!idev) 1814 return ERR_PTR(-ENOBUFS); 1815 1816 if (idev->cnf.disable_ipv6) 1817 return ERR_PTR(-EACCES); 1818 1819 /* Add default multicast route */ 1820 addrconf_add_mroute(dev); 1821 1822 /* Add link local route */ 1823 addrconf_add_lroute(dev); 1824 return idev; 1825} 1826 1827void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len) 1828{ 1829 struct prefix_info *pinfo; 1830 __u32 valid_lft; 1831 __u32 prefered_lft; 1832 int addr_type; 1833 struct inet6_dev *in6_dev; 1834 struct net *net = dev_net(dev); 1835 1836 pinfo = (struct prefix_info *) opt; 1837 1838 if (len < sizeof(struct prefix_info)) { 1839 ADBG(("addrconf: prefix option too short\n")); 1840 return; 1841 } 1842 1843 /* 1844 * Validation checks ([ADDRCONF], page 19) 1845 */ 1846 1847 addr_type = ipv6_addr_type(&pinfo->prefix); 1848 1849 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)) 1850 return; 1851 1852 valid_lft = ntohl(pinfo->valid); 1853 prefered_lft = ntohl(pinfo->prefered); 1854 1855 if (prefered_lft > valid_lft) { 1856 if (net_ratelimit()) 1857 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n"); 1858 return; 1859 } 1860 1861 in6_dev = in6_dev_get(dev); 1862 1863 if (in6_dev == NULL) { 1864 if (net_ratelimit()) 1865 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name); 1866 return; 1867 } 1868 1869 /* 1870 * Two things going on here: 1871 * 1) Add routes for on-link prefixes 1872 * 2) Configure prefixes with the auto flag set 1873 */ 1874 1875 if (pinfo->onlink) { 1876 struct rt6_info *rt; 1877 unsigned long rt_expires; 1878 1879 /* Avoid arithmetic overflow. Really, we could 1880 * save rt_expires in seconds, likely valid_lft, 1881 * but it would require division in fib gc, that it 1882 * not good. 1883 */ 1884 if (HZ > USER_HZ) 1885 rt_expires = addrconf_timeout_fixup(valid_lft, HZ); 1886 else 1887 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ); 1888 1889 if (addrconf_finite_timeout(rt_expires)) 1890 rt_expires *= HZ; 1891 1892 rt = rt6_lookup(net, &pinfo->prefix, NULL, 1893 dev->ifindex, 1); 1894 1895 if (rt && addrconf_is_prefix_route(rt)) { 1896 /* Autoconf prefix route */ 1897 if (valid_lft == 0) { 1898 ip6_del_rt(rt); 1899 rt = NULL; 1900 } else if (addrconf_finite_timeout(rt_expires)) { 1901 /* not infinity */ 1902 rt->rt6i_expires = jiffies + rt_expires; 1903 rt->rt6i_flags |= RTF_EXPIRES; 1904 } else { 1905 rt->rt6i_flags &= ~RTF_EXPIRES; 1906 rt->rt6i_expires = 0; 1907 } 1908 } else if (valid_lft) { 1909 clock_t expires = 0; 1910 int flags = RTF_ADDRCONF | RTF_PREFIX_RT; 1911 if (addrconf_finite_timeout(rt_expires)) { 1912 /* not infinity */ 1913 flags |= RTF_EXPIRES; 1914 expires = jiffies_to_clock_t(rt_expires); 1915 } 1916 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len, 1917 dev, expires, flags); 1918 } 1919 if (rt) 1920 dst_release(&rt->dst); 1921 } 1922 1923 /* Try to figure out our local address for this prefix */ 1924 1925 if (pinfo->autoconf && in6_dev->cnf.autoconf) { 1926 struct inet6_ifaddr * ifp; 1927 struct in6_addr addr; 1928 int create = 0, update_lft = 0; 1929 1930 if (pinfo->prefix_len == 64) { 1931 memcpy(&addr, &pinfo->prefix, 8); 1932 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) && 1933 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) { 1934 in6_dev_put(in6_dev); 1935 return; 1936 } 1937 goto ok; 1938 } 1939 if (net_ratelimit()) 1940 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n", 1941 pinfo->prefix_len); 1942 in6_dev_put(in6_dev); 1943 return; 1944 1945ok: 1946 1947 ifp = ipv6_get_ifaddr(net, &addr, dev, 1); 1948 1949 if (ifp == NULL && valid_lft) { 1950 int max_addresses = in6_dev->cnf.max_addresses; 1951 u32 addr_flags = 0; 1952 1953#ifdef CONFIG_IPV6_OPTIMISTIC_DAD 1954 if (in6_dev->cnf.optimistic_dad && 1955 !net->ipv6.devconf_all->forwarding) 1956 addr_flags = IFA_F_OPTIMISTIC; 1957#endif 1958 1959 /* Do not allow to create too much of autoconfigured 1960 * addresses; this would be too easy way to crash kernel. 1961 */ 1962 if (!max_addresses || 1963 ipv6_count_addresses(in6_dev) < max_addresses) 1964 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len, 1965 addr_type&IPV6_ADDR_SCOPE_MASK, 1966 addr_flags); 1967 1968 if (!ifp || IS_ERR(ifp)) { 1969 in6_dev_put(in6_dev); 1970 return; 1971 } 1972 1973 update_lft = create = 1; 1974 ifp->cstamp = jiffies; 1975 addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT); 1976 } 1977 1978 if (ifp) { 1979 int flags; 1980 unsigned long now; 1981#ifdef CONFIG_IPV6_PRIVACY 1982 struct inet6_ifaddr *ift; 1983#endif 1984 u32 stored_lft; 1985 1986 /* update lifetime (RFC2462 5.5.3 e) */ 1987 spin_lock(&ifp->lock); 1988 now = jiffies; 1989 if (ifp->valid_lft > (now - ifp->tstamp) / HZ) 1990 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ; 1991 else 1992 stored_lft = 0; 1993 if (!update_lft && stored_lft) { 1994 if (valid_lft > MIN_VALID_LIFETIME || 1995 valid_lft > stored_lft) 1996 update_lft = 1; 1997 else if (stored_lft <= MIN_VALID_LIFETIME) { 1998 /* valid_lft <= stored_lft is always true */ 1999 /* 2000 * RFC 4862 Section 5.5.3e: 2001 * "Note that the preferred lifetime of 2002 * the corresponding address is always 2003 * reset to the Preferred Lifetime in 2004 * the received Prefix Information 2005 * option, regardless of whether the 2006 * valid lifetime is also reset or 2007 * ignored." 2008 * 2009 * So if the preferred lifetime in 2010 * this advertisement is different 2011 * than what we have stored, but the 2012 * valid lifetime is invalid, just 2013 * reset prefered_lft. 2014 * 2015 * We must set the valid lifetime 2016 * to the stored lifetime since we'll 2017 * be updating the timestamp below, 2018 * else we'll set it back to the 2019 * minumum. 2020 */ 2021 if (prefered_lft != ifp->prefered_lft) { 2022 valid_lft = stored_lft; 2023 update_lft = 1; 2024 } 2025 } else { 2026 valid_lft = MIN_VALID_LIFETIME; 2027 if (valid_lft < prefered_lft) 2028 prefered_lft = valid_lft; 2029 update_lft = 1; 2030 } 2031 } 2032 2033 if (update_lft) { 2034 ifp->valid_lft = valid_lft; 2035 ifp->prefered_lft = prefered_lft; 2036 ifp->tstamp = now; 2037 flags = ifp->flags; 2038 ifp->flags &= ~IFA_F_DEPRECATED; 2039 spin_unlock(&ifp->lock); 2040 2041 if (!(flags&IFA_F_TENTATIVE)) 2042 ipv6_ifa_notify(0, ifp); 2043 } else 2044 spin_unlock(&ifp->lock); 2045 2046#ifdef CONFIG_IPV6_PRIVACY 2047 read_lock_bh(&in6_dev->lock); 2048 /* update all temporary addresses in the list */ 2049 list_for_each_entry(ift, &in6_dev->tempaddr_list, tmp_list) { 2050 /* 2051 * When adjusting the lifetimes of an existing 2052 * temporary address, only lower the lifetimes. 2053 * Implementations must not increase the 2054 * lifetimes of an existing temporary address 2055 * when processing a Prefix Information Option. 2056 */ 2057 if (ifp != ift->ifpub) 2058 continue; 2059 2060 spin_lock(&ift->lock); 2061 flags = ift->flags; 2062 if (ift->valid_lft > valid_lft && 2063 ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ) 2064 ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ; 2065 if (ift->prefered_lft > prefered_lft && 2066 ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ) 2067 ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ; 2068 spin_unlock(&ift->lock); 2069 if (!(flags&IFA_F_TENTATIVE)) 2070 ipv6_ifa_notify(0, ift); 2071 } 2072 2073 if (create && in6_dev->cnf.use_tempaddr > 0) { 2074 /* 2075 * When a new public address is created as described in [ADDRCONF], 2076 * also create a new temporary address. 2077 */ 2078 read_unlock_bh(&in6_dev->lock); 2079 ipv6_create_tempaddr(ifp, NULL); 2080 } else { 2081 read_unlock_bh(&in6_dev->lock); 2082 } 2083#endif 2084 in6_ifa_put(ifp); 2085 addrconf_verify(0); 2086 } 2087 } 2088 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo); 2089 in6_dev_put(in6_dev); 2090} 2091 2092/* 2093 * Set destination address. 2094 * Special case for SIT interfaces where we create a new "virtual" 2095 * device. 2096 */ 2097int addrconf_set_dstaddr(struct net *net, void __user *arg) 2098{ 2099 struct in6_ifreq ireq; 2100 struct net_device *dev; 2101 int err = -EINVAL; 2102 2103 rtnl_lock(); 2104 2105 err = -EFAULT; 2106 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 2107 goto err_exit; 2108 2109 dev = __dev_get_by_index(net, ireq.ifr6_ifindex); 2110 2111 err = -ENODEV; 2112 if (dev == NULL) 2113 goto err_exit; 2114 2115#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 2116 if (dev->type == ARPHRD_SIT) { 2117 const struct net_device_ops *ops = dev->netdev_ops; 2118 struct ifreq ifr; 2119 struct ip_tunnel_parm p; 2120 2121 err = -EADDRNOTAVAIL; 2122 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4)) 2123 goto err_exit; 2124 2125 memset(&p, 0, sizeof(p)); 2126 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3]; 2127 p.iph.saddr = 0; 2128 p.iph.version = 4; 2129 p.iph.ihl = 5; 2130 p.iph.protocol = IPPROTO_IPV6; 2131 p.iph.ttl = 64; 2132 ifr.ifr_ifru.ifru_data = (__force void __user *)&p; 2133 2134 if (ops->ndo_do_ioctl) { 2135 mm_segment_t oldfs = get_fs(); 2136 2137 set_fs(KERNEL_DS); 2138 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL); 2139 set_fs(oldfs); 2140 } else 2141 err = -EOPNOTSUPP; 2142 2143 if (err == 0) { 2144 err = -ENOBUFS; 2145 dev = __dev_get_by_name(net, p.name); 2146 if (!dev) 2147 goto err_exit; 2148 err = dev_open(dev); 2149 } 2150 } 2151#endif 2152 2153err_exit: 2154 rtnl_unlock(); 2155 return err; 2156} 2157 2158/* 2159 * Manual configuration of address on an interface 2160 */ 2161static int inet6_addr_add(struct net *net, int ifindex, struct in6_addr *pfx, 2162 unsigned int plen, __u8 ifa_flags, __u32 prefered_lft, 2163 __u32 valid_lft) 2164{ 2165 struct inet6_ifaddr *ifp; 2166 struct inet6_dev *idev; 2167 struct net_device *dev; 2168 int scope; 2169 u32 flags; 2170 clock_t expires; 2171 unsigned long timeout; 2172 2173 ASSERT_RTNL(); 2174 2175 if (plen > 128) 2176 return -EINVAL; 2177 2178 /* check the lifetime */ 2179 if (!valid_lft || prefered_lft > valid_lft) 2180 return -EINVAL; 2181 2182 dev = __dev_get_by_index(net, ifindex); 2183 if (!dev) 2184 return -ENODEV; 2185 2186 idev = addrconf_add_dev(dev); 2187 if (IS_ERR(idev)) 2188 return PTR_ERR(idev); 2189 2190 scope = ipv6_addr_scope(pfx); 2191 2192 timeout = addrconf_timeout_fixup(valid_lft, HZ); 2193 if (addrconf_finite_timeout(timeout)) { 2194 expires = jiffies_to_clock_t(timeout * HZ); 2195 valid_lft = timeout; 2196 flags = RTF_EXPIRES; 2197 } else { 2198 expires = 0; 2199 flags = 0; 2200 ifa_flags |= IFA_F_PERMANENT; 2201 } 2202 2203 timeout = addrconf_timeout_fixup(prefered_lft, HZ); 2204 if (addrconf_finite_timeout(timeout)) { 2205 if (timeout == 0) 2206 ifa_flags |= IFA_F_DEPRECATED; 2207 prefered_lft = timeout; 2208 } 2209 2210 ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags); 2211 2212 if (!IS_ERR(ifp)) { 2213 spin_lock_bh(&ifp->lock); 2214 ifp->valid_lft = valid_lft; 2215 ifp->prefered_lft = prefered_lft; 2216 ifp->tstamp = jiffies; 2217 spin_unlock_bh(&ifp->lock); 2218 2219 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 2220 expires, flags); 2221 /* 2222 * Note that section 3.1 of RFC 4429 indicates 2223 * that the Optimistic flag should not be set for 2224 * manually configured addresses 2225 */ 2226 addrconf_dad_start(ifp, 0); 2227 in6_ifa_put(ifp); 2228 addrconf_verify(0); 2229 return 0; 2230 } 2231 2232 return PTR_ERR(ifp); 2233} 2234 2235static int inet6_addr_del(struct net *net, int ifindex, struct in6_addr *pfx, 2236 unsigned int plen) 2237{ 2238 struct inet6_ifaddr *ifp; 2239 struct inet6_dev *idev; 2240 struct net_device *dev; 2241 2242 if (plen > 128) 2243 return -EINVAL; 2244 2245 dev = __dev_get_by_index(net, ifindex); 2246 if (!dev) 2247 return -ENODEV; 2248 2249 if ((idev = __in6_dev_get(dev)) == NULL) 2250 return -ENXIO; 2251 2252 read_lock_bh(&idev->lock); 2253 list_for_each_entry(ifp, &idev->addr_list, if_list) { 2254 if (ifp->prefix_len == plen && 2255 ipv6_addr_equal(pfx, &ifp->addr)) { 2256 in6_ifa_hold(ifp); 2257 read_unlock_bh(&idev->lock); 2258 2259 ipv6_del_addr(ifp); 2260 2261 /* Foxconn added start pling 11/29/2010 */ 2262 /* WNR3500L TD175: 2263 * When an IPv6 address is removed from a interface, 2264 * check whether this addr causes DAD before. 2265 * If yes, clear the DAD flag as well. 2266 */ 2267 if (strcmp(ifp->idev->dev->name, wan_if_name) == 0) { 2268 if (memcmp(&(ifp->addr), &dad_wan_ip_addr, 2269 sizeof(dad_wan_ip_addr)) == 0) { 2270 /* Clear DAD flag, and restore forwarding */ 2271 wan_dad_detected = 0; 2272 ipv6_devconf.forwarding = 1; 2273 ifp->idev->cnf.forwarding = 1; 2274 printk(KERN_EMERG "Remove DAD for WAN\n"); 2275 } 2276 } 2277 /* Foxconn added end pling 11/29/2010 */ 2278 2279 /* If the last address is deleted administratively, 2280 disable IPv6 on this interface. 2281 */ 2282 if (list_empty(&idev->addr_list)) 2283 addrconf_ifdown(idev->dev, 1); 2284 return 0; 2285 } 2286 } 2287 read_unlock_bh(&idev->lock); 2288 return -EADDRNOTAVAIL; 2289} 2290 2291 2292int addrconf_add_ifaddr(struct net *net, void __user *arg) 2293{ 2294 struct in6_ifreq ireq; 2295 int err; 2296 2297 if (!capable(CAP_NET_ADMIN)) 2298 return -EPERM; 2299 2300 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 2301 return -EFAULT; 2302 2303 rtnl_lock(); 2304 err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, 2305 ireq.ifr6_prefixlen, IFA_F_PERMANENT, 2306 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME); 2307 rtnl_unlock(); 2308 return err; 2309} 2310 2311int addrconf_del_ifaddr(struct net *net, void __user *arg) 2312{ 2313 struct in6_ifreq ireq; 2314 int err; 2315 2316 if (!capable(CAP_NET_ADMIN)) 2317 return -EPERM; 2318 2319 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 2320 return -EFAULT; 2321 2322 rtnl_lock(); 2323 err = inet6_addr_del(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, 2324 ireq.ifr6_prefixlen); 2325 rtnl_unlock(); 2326 return err; 2327} 2328 2329static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr, 2330 int plen, int scope) 2331{ 2332 struct inet6_ifaddr *ifp; 2333 2334 ifp = ipv6_add_addr(idev, addr, plen, scope, IFA_F_PERMANENT); 2335 if (!IS_ERR(ifp)) { 2336 spin_lock_bh(&ifp->lock); 2337 ifp->flags &= ~IFA_F_TENTATIVE; 2338 spin_unlock_bh(&ifp->lock); 2339 ipv6_ifa_notify(RTM_NEWADDR, ifp); 2340 in6_ifa_put(ifp); 2341 } 2342} 2343 2344#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 2345static void sit_add_v4_addrs(struct inet6_dev *idev) 2346{ 2347 struct in6_addr addr; 2348 struct net_device *dev; 2349 struct net *net = dev_net(idev->dev); 2350 int scope; 2351 2352 ASSERT_RTNL(); 2353 2354 memset(&addr, 0, sizeof(struct in6_addr)); 2355 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4); 2356 2357 if (idev->dev->flags&IFF_POINTOPOINT) { 2358 addr.s6_addr32[0] = htonl(0xfe800000); 2359 scope = IFA_LINK; 2360 } else { 2361 scope = IPV6_ADDR_COMPATv4; 2362 } 2363 2364 if (addr.s6_addr32[3]) { 2365 add_addr(idev, &addr, 128, scope); 2366 return; 2367 } 2368 2369 for_each_netdev(net, dev) { 2370 struct in_device * in_dev = __in_dev_get_rtnl(dev); 2371 if (in_dev && (dev->flags & IFF_UP)) { 2372 struct in_ifaddr * ifa; 2373 2374 int flag = scope; 2375 2376 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) { 2377 int plen; 2378 2379 addr.s6_addr32[3] = ifa->ifa_local; 2380 2381 if (ifa->ifa_scope == RT_SCOPE_LINK) 2382 continue; 2383 if (ifa->ifa_scope >= RT_SCOPE_HOST) { 2384 if (idev->dev->flags&IFF_POINTOPOINT) 2385 continue; 2386 flag |= IFA_HOST; 2387 } 2388 if (idev->dev->flags&IFF_POINTOPOINT) 2389 plen = 64; 2390 else 2391 plen = 96; 2392 2393 add_addr(idev, &addr, plen, flag); 2394 } 2395 } 2396 } 2397} 2398#endif 2399 2400static void init_loopback(struct net_device *dev) 2401{ 2402 struct inet6_dev *idev; 2403 2404 /* ::1 */ 2405 2406 ASSERT_RTNL(); 2407 2408 if ((idev = ipv6_find_idev(dev)) == NULL) { 2409 printk(KERN_DEBUG "init loopback: add_dev failed\n"); 2410 return; 2411 } 2412 2413 add_addr(idev, &in6addr_loopback, 128, IFA_HOST); 2414} 2415 2416static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr) 2417{ 2418 struct inet6_ifaddr * ifp; 2419 u32 addr_flags = IFA_F_PERMANENT; 2420 2421#ifdef CONFIG_IPV6_OPTIMISTIC_DAD 2422 if (idev->cnf.optimistic_dad && 2423 !dev_net(idev->dev)->ipv6.devconf_all->forwarding) 2424 addr_flags |= IFA_F_OPTIMISTIC; 2425#endif 2426 2427 2428 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, addr_flags); 2429 if (!IS_ERR(ifp)) { 2430 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0); 2431 addrconf_dad_start(ifp, 0); 2432 in6_ifa_put(ifp); 2433 } 2434} 2435 2436static void addrconf_dev_config(struct net_device *dev) 2437{ 2438 struct in6_addr addr; 2439 struct inet6_dev * idev; 2440 2441 ASSERT_RTNL(); 2442 2443 if ((dev->type != ARPHRD_ETHER) && 2444 (dev->type != ARPHRD_FDDI) && 2445 (dev->type != ARPHRD_IEEE802_TR) && 2446 (dev->type != ARPHRD_ARCNET) && 2447 (dev->type != ARPHRD_INFINIBAND)) { 2448 /* Alas, we support only Ethernet autoconfiguration. */ 2449 return; 2450 } 2451 2452 idev = addrconf_add_dev(dev); 2453 if (IS_ERR(idev)) 2454 return; 2455 2456 memset(&addr, 0, sizeof(struct in6_addr)); 2457 addr.s6_addr32[0] = htonl(0xFE800000); 2458 2459 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0) 2460 addrconf_add_linklocal(idev, &addr); 2461} 2462 2463#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 2464static void addrconf_sit_config(struct net_device *dev) 2465{ 2466 struct inet6_dev *idev; 2467 2468 ASSERT_RTNL(); 2469 2470 /* 2471 * Configure the tunnel with one of our IPv4 2472 * addresses... we should configure all of 2473 * our v4 addrs in the tunnel 2474 */ 2475 2476 if ((idev = ipv6_find_idev(dev)) == NULL) { 2477 printk(KERN_DEBUG "init sit: add_dev failed\n"); 2478 return; 2479 } 2480 2481 if (dev->priv_flags & IFF_ISATAP) { 2482 struct in6_addr addr; 2483 2484 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0); 2485 addrconf_prefix_route(&addr, 64, dev, 0, 0); 2486 if (!ipv6_generate_eui64(addr.s6_addr + 8, dev)) 2487 addrconf_add_linklocal(idev, &addr); 2488 return; 2489 } 2490 2491 sit_add_v4_addrs(idev); 2492 2493 if (dev->flags&IFF_POINTOPOINT) { 2494 addrconf_add_mroute(dev); 2495 addrconf_add_lroute(dev); 2496 } else 2497 sit_route_add(dev); 2498} 2499#endif 2500 2501static inline int 2502ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev) 2503{ 2504 struct in6_addr lladdr; 2505 2506 if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) { 2507 addrconf_add_linklocal(idev, &lladdr); 2508 return 0; 2509 } 2510 return -1; 2511} 2512 2513static void ip6_tnl_add_linklocal(struct inet6_dev *idev) 2514{ 2515 struct net_device *link_dev; 2516 struct net *net = dev_net(idev->dev); 2517 2518 /* first try to inherit the link-local address from the link device */ 2519 if (idev->dev->iflink && 2520 (link_dev = __dev_get_by_index(net, idev->dev->iflink))) { 2521 if (!ipv6_inherit_linklocal(idev, link_dev)) 2522 return; 2523 } 2524 /* then try to inherit it from any device */ 2525 for_each_netdev(net, link_dev) { 2526 if (!ipv6_inherit_linklocal(idev, link_dev)) 2527 return; 2528 } 2529 printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n"); 2530} 2531 2532/* 2533 * Autoconfigure tunnel with a link-local address so routing protocols, 2534 * DHCPv6, MLD etc. can be run over the virtual link 2535 */ 2536 2537static void addrconf_ip6_tnl_config(struct net_device *dev) 2538{ 2539 struct inet6_dev *idev; 2540 2541 ASSERT_RTNL(); 2542 2543 idev = addrconf_add_dev(dev); 2544 if (IS_ERR(idev)) { 2545 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n"); 2546 return; 2547 } 2548 ip6_tnl_add_linklocal(idev); 2549} 2550 2551static int addrconf_notify(struct notifier_block *this, unsigned long event, 2552 void * data) 2553{ 2554 struct net_device *dev = (struct net_device *) data; 2555 struct inet6_dev *idev = __in6_dev_get(dev); 2556 int run_pending = 0; 2557 int err; 2558 2559 switch (event) { 2560 case NETDEV_REGISTER: 2561 if (!idev && dev->mtu >= IPV6_MIN_MTU) { 2562 idev = ipv6_add_dev(dev); 2563 if (!idev) 2564 return notifier_from_errno(-ENOMEM); 2565 } 2566 break; 2567 2568 case NETDEV_UP: 2569 case NETDEV_CHANGE: 2570 if (dev->flags & IFF_SLAVE) 2571 break; 2572 2573 if (event == NETDEV_UP) { 2574 if (!addrconf_qdisc_ok(dev)) { 2575 /* device is not ready yet. */ 2576 printk(KERN_INFO 2577 "ADDRCONF(NETDEV_UP): %s: " 2578 "link is not ready\n", 2579 dev->name); 2580 break; 2581 } 2582 2583 if (!idev && dev->mtu >= IPV6_MIN_MTU) 2584 idev = ipv6_add_dev(dev); 2585 2586 if (idev) { 2587 idev->if_flags |= IF_READY; 2588 run_pending = 1; 2589 } 2590 } else { 2591 if (!addrconf_qdisc_ok(dev)) { 2592 /* device is still not ready. */ 2593 break; 2594 } 2595 2596 if (idev) { 2597 if (idev->if_flags & IF_READY) 2598 /* device is already configured. */ 2599 break; 2600 idev->if_flags |= IF_READY; 2601 } 2602 2603 printk(KERN_INFO 2604 "ADDRCONF(NETDEV_CHANGE): %s: " 2605 "link becomes ready\n", 2606 dev->name); 2607 2608 run_pending = 1; 2609 } 2610 2611 switch (dev->type) { 2612#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 2613 case ARPHRD_SIT: 2614 addrconf_sit_config(dev); 2615 break; 2616#endif 2617 case ARPHRD_TUNNEL6: 2618 addrconf_ip6_tnl_config(dev); 2619 break; 2620 case ARPHRD_LOOPBACK: 2621 init_loopback(dev); 2622 break; 2623 2624 default: 2625 addrconf_dev_config(dev); 2626 break; 2627 } 2628 2629 if (idev) { 2630 if (run_pending) 2631 addrconf_dad_run(idev); 2632 2633 /* 2634 * If the MTU changed during the interface down, 2635 * when the interface up, the changed MTU must be 2636 * reflected in the idev as well as routers. 2637 */ 2638 if (idev->cnf.mtu6 != dev->mtu && 2639 dev->mtu >= IPV6_MIN_MTU) { 2640 rt6_mtu_change(dev, dev->mtu); 2641 idev->cnf.mtu6 = dev->mtu; 2642 } 2643 idev->tstamp = jiffies; 2644 inet6_ifinfo_notify(RTM_NEWLINK, idev); 2645 2646 /* 2647 * If the changed mtu during down is lower than 2648 * IPV6_MIN_MTU stop IPv6 on this interface. 2649 */ 2650 if (dev->mtu < IPV6_MIN_MTU) 2651 addrconf_ifdown(dev, 1); 2652 } 2653 break; 2654 2655 case NETDEV_CHANGEMTU: 2656 if (idev && dev->mtu >= IPV6_MIN_MTU) { 2657 rt6_mtu_change(dev, dev->mtu); 2658 idev->cnf.mtu6 = dev->mtu; 2659 break; 2660 } 2661 2662 if (!idev && dev->mtu >= IPV6_MIN_MTU) { 2663 idev = ipv6_add_dev(dev); 2664 if (idev) 2665 break; 2666 } 2667 2668 /* 2669 * MTU falled under IPV6_MIN_MTU. 2670 * Stop IPv6 on this interface. 2671 */ 2672 2673 case NETDEV_DOWN: 2674 case NETDEV_UNREGISTER: 2675 /* 2676 * Remove all addresses from this interface. 2677 */ 2678 addrconf_ifdown(dev, event != NETDEV_DOWN); 2679 break; 2680 2681 case NETDEV_CHANGENAME: 2682 if (idev) { 2683 snmp6_unregister_dev(idev); 2684 addrconf_sysctl_unregister(idev); 2685 addrconf_sysctl_register(idev); 2686 err = snmp6_register_dev(idev); 2687 if (err) 2688 return notifier_from_errno(err); 2689 } 2690 break; 2691 2692 case NETDEV_PRE_TYPE_CHANGE: 2693 case NETDEV_POST_TYPE_CHANGE: 2694 addrconf_type_change(dev, event); 2695 break; 2696 } 2697 2698 return NOTIFY_OK; 2699} 2700 2701/* 2702 * addrconf module should be notified of a device going up 2703 */ 2704static struct notifier_block ipv6_dev_notf = { 2705 .notifier_call = addrconf_notify, 2706}; 2707 2708static void addrconf_type_change(struct net_device *dev, unsigned long event) 2709{ 2710 struct inet6_dev *idev; 2711 ASSERT_RTNL(); 2712 2713 idev = __in6_dev_get(dev); 2714 2715 if (event == NETDEV_POST_TYPE_CHANGE) 2716 ipv6_mc_remap(idev); 2717 else if (event == NETDEV_PRE_TYPE_CHANGE) 2718 ipv6_mc_unmap(idev); 2719} 2720 2721static int addrconf_ifdown(struct net_device *dev, int how) 2722{ 2723 struct net *net = dev_net(dev); 2724 struct inet6_dev *idev; 2725 struct inet6_ifaddr *ifa; 2726 LIST_HEAD(keep_list); 2727 int state; 2728 2729 ASSERT_RTNL(); 2730 2731 rt6_ifdown(net, dev); 2732 neigh_ifdown(&nd_tbl, dev); 2733 2734 idev = __in6_dev_get(dev); 2735 if (idev == NULL) 2736 return -ENODEV; 2737 2738 /* 2739 * Step 1: remove reference to ipv6 device from parent device. 2740 * Do not dev_put! 2741 */ 2742 if (how) { 2743 idev->dead = 1; 2744 2745 /* protected by rtnl_lock */ 2746 rcu_assign_pointer(dev->ip6_ptr, NULL); 2747 2748 /* Step 1.5: remove snmp6 entry */ 2749 snmp6_unregister_dev(idev); 2750 2751 } 2752 2753 write_lock_bh(&idev->lock); 2754 2755 /* Step 2: clear flags for stateless addrconf */ 2756 if (!how) 2757 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY); 2758 2759#ifdef CONFIG_IPV6_PRIVACY 2760 if (how && del_timer(&idev->regen_timer)) 2761 in6_dev_put(idev); 2762 2763 /* Step 3: clear tempaddr list */ 2764 while (!list_empty(&idev->tempaddr_list)) { 2765 ifa = list_first_entry(&idev->tempaddr_list, 2766 struct inet6_ifaddr, tmp_list); 2767 list_del(&ifa->tmp_list); 2768 write_unlock_bh(&idev->lock); 2769 spin_lock_bh(&ifa->lock); 2770 2771 if (ifa->ifpub) { 2772 in6_ifa_put(ifa->ifpub); 2773 ifa->ifpub = NULL; 2774 } 2775 spin_unlock_bh(&ifa->lock); 2776 in6_ifa_put(ifa); 2777 write_lock_bh(&idev->lock); 2778 } 2779#endif 2780 2781 while (!list_empty(&idev->addr_list)) { 2782 ifa = list_first_entry(&idev->addr_list, 2783 struct inet6_ifaddr, if_list); 2784 addrconf_del_timer(ifa); 2785 2786 /* If just doing link down, and address is permanent 2787 and not link-local, then retain it. */ 2788 if (!how && 2789 (ifa->flags&IFA_F_PERMANENT) && 2790 !(ipv6_addr_type(&ifa->addr) & IPV6_ADDR_LINKLOCAL)) { 2791 list_move_tail(&ifa->if_list, &keep_list); 2792 2793 /* If not doing DAD on this address, just keep it. */ 2794 if ((dev->flags&(IFF_NOARP|IFF_LOOPBACK)) || 2795 idev->cnf.accept_dad <= 0 || 2796 (ifa->flags & IFA_F_NODAD)) 2797 continue; 2798 2799 /* If it was tentative already, no need to notify */ 2800 if (ifa->flags & IFA_F_TENTATIVE) 2801 continue; 2802 2803 /* Flag it for later restoration when link comes up */ 2804 ifa->flags |= IFA_F_TENTATIVE; 2805 ifa->state = INET6_IFADDR_STATE_DAD; 2806 2807 write_unlock_bh(&idev->lock); 2808 2809 in6_ifa_hold(ifa); 2810 } else { 2811 list_del(&ifa->if_list); 2812 2813 /* clear hash table */ 2814 spin_lock_bh(&addrconf_hash_lock); 2815 hlist_del_init_rcu(&ifa->addr_lst); 2816 spin_unlock_bh(&addrconf_hash_lock); 2817 2818 write_unlock_bh(&idev->lock); 2819 spin_lock_bh(&ifa->state_lock); 2820 state = ifa->state; 2821 ifa->state = INET6_IFADDR_STATE_DEAD; 2822 spin_unlock_bh(&ifa->state_lock); 2823 2824 if (state == INET6_IFADDR_STATE_DEAD) 2825 goto put_ifa; 2826 } 2827 2828 __ipv6_ifa_notify(RTM_DELADDR, ifa); 2829 if (ifa->state == INET6_IFADDR_STATE_DEAD) 2830 atomic_notifier_call_chain(&inet6addr_chain, 2831 NETDEV_DOWN, ifa); 2832 2833put_ifa: 2834 in6_ifa_put(ifa); 2835 2836 write_lock_bh(&idev->lock); 2837 } 2838 2839 list_splice(&keep_list, &idev->addr_list); 2840 2841 write_unlock_bh(&idev->lock); 2842 2843 /* Step 5: Discard multicast list */ 2844 if (how) 2845 ipv6_mc_destroy_dev(idev); 2846 else 2847 ipv6_mc_down(idev); 2848 2849 idev->tstamp = jiffies; 2850 2851 /* Last: Shot the device (if unregistered) */ 2852 if (how) { 2853 addrconf_sysctl_unregister(idev); 2854 neigh_parms_release(&nd_tbl, idev->nd_parms); 2855 neigh_ifdown(&nd_tbl, dev); 2856 in6_dev_put(idev); 2857 } 2858 return 0; 2859} 2860 2861static void addrconf_rs_timer(unsigned long data) 2862{ 2863 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data; 2864 struct inet6_dev *idev = ifp->idev; 2865 2866 read_lock(&idev->lock); 2867 if (idev->dead || !(idev->if_flags & IF_READY)) 2868 goto out; 2869 2870 if (idev->cnf.forwarding) 2871 goto out; 2872 2873 /* Announcement received after solicitation was sent */ 2874 if (idev->if_flags & IF_RA_RCVD) 2875 goto out; 2876 2877 spin_lock(&ifp->lock); 2878 if (ifp->probes++ < idev->cnf.rtr_solicits) { 2879 /* The wait after the last probe can be shorter */ 2880 addrconf_mod_timer(ifp, AC_RS, 2881 (ifp->probes == idev->cnf.rtr_solicits) ? 2882 idev->cnf.rtr_solicit_delay : 2883 idev->cnf.rtr_solicit_interval); 2884 spin_unlock(&ifp->lock); 2885 2886 ndisc_send_rs(idev->dev, &ifp->addr, &in6addr_linklocal_allrouters); 2887 } else { 2888 spin_unlock(&ifp->lock); 2889 /* 2890 * Note: we do not support deprecated "all on-link" 2891 * assumption any longer. 2892 */ 2893 printk(KERN_DEBUG "%s: no IPv6 routers present\n", 2894 idev->dev->name); 2895 } 2896 2897out: 2898 read_unlock(&idev->lock); 2899 in6_ifa_put(ifp); 2900} 2901 2902/* 2903 * Duplicate Address Detection 2904 */ 2905static void addrconf_dad_kick(struct inet6_ifaddr *ifp) 2906{ 2907 unsigned long rand_num; 2908 struct inet6_dev *idev = ifp->idev; 2909 2910 if (ifp->flags & IFA_F_OPTIMISTIC) 2911 rand_num = 0; 2912 else 2913 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1); 2914 2915 ifp->probes = idev->cnf.dad_transmits; 2916 addrconf_mod_timer(ifp, AC_DAD, rand_num); 2917} 2918 2919static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags) 2920{ 2921 struct inet6_dev *idev = ifp->idev; 2922 struct net_device *dev = idev->dev; 2923 2924 addrconf_join_solict(dev, &ifp->addr); 2925 2926 net_srandom(ifp->addr.s6_addr32[3]); 2927 2928 read_lock_bh(&idev->lock); 2929 spin_lock(&ifp->lock); 2930 if (ifp->state == INET6_IFADDR_STATE_DEAD) 2931 goto out; 2932 2933 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) || 2934 idev->cnf.accept_dad < 1 || 2935 !(ifp->flags&IFA_F_TENTATIVE) || 2936 ifp->flags & IFA_F_NODAD) { 2937 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 2938 spin_unlock(&ifp->lock); 2939 read_unlock_bh(&idev->lock); 2940 2941 addrconf_dad_completed(ifp); 2942 return; 2943 } 2944 2945 if (!(idev->if_flags & IF_READY)) { 2946 spin_unlock(&ifp->lock); 2947 read_unlock_bh(&idev->lock); 2948 /* 2949 * If the device is not ready: 2950 * - keep it tentative if it is a permanent address. 2951 * - otherwise, kill it. 2952 */ 2953 in6_ifa_hold(ifp); 2954 addrconf_dad_stop(ifp, 0); 2955 return; 2956 } 2957 2958 /* 2959 * Optimistic nodes can start receiving 2960 * Frames right away 2961 */ 2962 if (ifp->flags & IFA_F_OPTIMISTIC) 2963 ip6_ins_rt(ifp->rt); 2964 2965 addrconf_dad_kick(ifp); 2966out: 2967 spin_unlock(&ifp->lock); 2968 read_unlock_bh(&idev->lock); 2969} 2970 2971static void addrconf_dad_timer(unsigned long data) 2972{ 2973 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data; 2974 struct inet6_dev *idev = ifp->idev; 2975 struct in6_addr mcaddr; 2976 2977 if (!ifp->probes && addrconf_dad_end(ifp)) 2978 goto out; 2979 2980 read_lock(&idev->lock); 2981 if (idev->dead || !(idev->if_flags & IF_READY)) { 2982 read_unlock(&idev->lock); 2983 goto out; 2984 } 2985 2986 spin_lock(&ifp->lock); 2987 if (ifp->state == INET6_IFADDR_STATE_DEAD) { 2988 spin_unlock(&ifp->lock); 2989 read_unlock(&idev->lock); 2990 goto out; 2991 } 2992 2993 if (ifp->probes == 0) { 2994 /* 2995 * DAD was successful 2996 */ 2997 2998 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 2999 spin_unlock(&ifp->lock); 3000 read_unlock(&idev->lock); 3001 3002 addrconf_dad_completed(ifp); 3003 3004 goto out; 3005 } 3006 3007 ifp->probes--; 3008 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time); 3009 spin_unlock(&ifp->lock); 3010 read_unlock(&idev->lock); 3011 3012 /* send a neighbour solicitation for our addr */ 3013 addrconf_addr_solict_mult(&ifp->addr, &mcaddr); 3014 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any); 3015out: 3016 in6_ifa_put(ifp); 3017} 3018 3019static void addrconf_dad_completed(struct inet6_ifaddr *ifp) 3020{ 3021 struct net_device *dev = ifp->idev->dev; 3022 3023 /* 3024 * Configure the address for reception. Now it is valid. 3025 */ 3026 3027 ipv6_ifa_notify(RTM_NEWADDR, ifp); 3028 3029 /* If added prefix is link local and forwarding is off, 3030 start sending router solicitations. 3031 */ 3032 3033 if (ifp->idev->cnf.forwarding == 0 && 3034 ifp->idev->cnf.rtr_solicits > 0 && 3035 (dev->flags&IFF_LOOPBACK) == 0 && 3036 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) { 3037 /* 3038 * If a host as already performed a random delay 3039 * [...] as part of DAD [...] there is no need 3040 * to delay again before sending the first RS 3041 */ 3042 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &in6addr_linklocal_allrouters); 3043 3044 spin_lock_bh(&ifp->lock); 3045 ifp->probes = 1; 3046 ifp->idev->if_flags |= IF_RS_SENT; 3047 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval); 3048 spin_unlock_bh(&ifp->lock); 3049 } 3050} 3051 3052static void addrconf_dad_run(struct inet6_dev *idev) 3053{ 3054 struct inet6_ifaddr *ifp; 3055 3056 read_lock_bh(&idev->lock); 3057 list_for_each_entry(ifp, &idev->addr_list, if_list) { 3058 spin_lock(&ifp->lock); 3059 if (ifp->flags & IFA_F_TENTATIVE && 3060 ifp->state == INET6_IFADDR_STATE_DAD) 3061 addrconf_dad_kick(ifp); 3062 spin_unlock(&ifp->lock); 3063 } 3064 read_unlock_bh(&idev->lock); 3065} 3066 3067#ifdef CONFIG_PROC_FS 3068struct if6_iter_state { 3069 struct seq_net_private p; 3070 int bucket; 3071}; 3072 3073static struct inet6_ifaddr *if6_get_first(struct seq_file *seq) 3074{ 3075 struct inet6_ifaddr *ifa = NULL; 3076 struct if6_iter_state *state = seq->private; 3077 struct net *net = seq_file_net(seq); 3078 3079 for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) { 3080 struct hlist_node *n; 3081 hlist_for_each_entry_rcu_bh(ifa, n, &inet6_addr_lst[state->bucket], 3082 addr_lst) 3083 if (net_eq(dev_net(ifa->idev->dev), net)) 3084 return ifa; 3085 } 3086 return NULL; 3087} 3088 3089static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, 3090 struct inet6_ifaddr *ifa) 3091{ 3092 struct if6_iter_state *state = seq->private; 3093 struct net *net = seq_file_net(seq); 3094 struct hlist_node *n = &ifa->addr_lst; 3095 3096 hlist_for_each_entry_continue_rcu_bh(ifa, n, addr_lst) 3097 if (net_eq(dev_net(ifa->idev->dev), net)) 3098 return ifa; 3099 3100 while (++state->bucket < IN6_ADDR_HSIZE) { 3101 hlist_for_each_entry_rcu_bh(ifa, n, 3102 &inet6_addr_lst[state->bucket], addr_lst) { 3103 if (net_eq(dev_net(ifa->idev->dev), net)) 3104 return ifa; 3105 } 3106 } 3107 3108 return NULL; 3109} 3110 3111static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos) 3112{ 3113 struct inet6_ifaddr *ifa = if6_get_first(seq); 3114 3115 if (ifa) 3116 while (pos && (ifa = if6_get_next(seq, ifa)) != NULL) 3117 --pos; 3118 return pos ? NULL : ifa; 3119} 3120 3121static void *if6_seq_start(struct seq_file *seq, loff_t *pos) 3122 __acquires(rcu_bh) 3123{ 3124 rcu_read_lock_bh(); 3125 return if6_get_idx(seq, *pos); 3126} 3127 3128static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos) 3129{ 3130 struct inet6_ifaddr *ifa; 3131 3132 ifa = if6_get_next(seq, v); 3133 ++*pos; 3134 return ifa; 3135} 3136 3137static void if6_seq_stop(struct seq_file *seq, void *v) 3138 __releases(rcu_bh) 3139{ 3140 rcu_read_unlock_bh(); 3141} 3142 3143static int if6_seq_show(struct seq_file *seq, void *v) 3144{ 3145 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v; 3146 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n", 3147 &ifp->addr, 3148 ifp->idev->dev->ifindex, 3149 ifp->prefix_len, 3150 ifp->scope, 3151 ifp->flags, 3152 ifp->idev->dev->name); 3153 return 0; 3154} 3155 3156static const struct seq_operations if6_seq_ops = { 3157 .start = if6_seq_start, 3158 .next = if6_seq_next, 3159 .show = if6_seq_show, 3160 .stop = if6_seq_stop, 3161}; 3162 3163static int if6_seq_open(struct inode *inode, struct file *file) 3164{ 3165 return seq_open_net(inode, file, &if6_seq_ops, 3166 sizeof(struct if6_iter_state)); 3167} 3168 3169static const struct file_operations if6_fops = { 3170 .owner = THIS_MODULE, 3171 .open = if6_seq_open, 3172 .read = seq_read, 3173 .llseek = seq_lseek, 3174 .release = seq_release_net, 3175}; 3176 3177static int __net_init if6_proc_net_init(struct net *net) 3178{ 3179 if (!proc_net_fops_create(net, "if_inet6", S_IRUGO, &if6_fops)) 3180 return -ENOMEM; 3181 return 0; 3182} 3183 3184static void __net_exit if6_proc_net_exit(struct net *net) 3185{ 3186 proc_net_remove(net, "if_inet6"); 3187} 3188 3189static struct pernet_operations if6_proc_net_ops = { 3190 .init = if6_proc_net_init, 3191 .exit = if6_proc_net_exit, 3192}; 3193 3194int __init if6_proc_init(void) 3195{ 3196 return register_pernet_subsys(&if6_proc_net_ops); 3197} 3198 3199void if6_proc_exit(void) 3200{ 3201 unregister_pernet_subsys(&if6_proc_net_ops); 3202} 3203#endif /* CONFIG_PROC_FS */ 3204 3205#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) 3206/* Check if address is a home address configured on any interface. */ 3207int ipv6_chk_home_addr(struct net *net, struct in6_addr *addr) 3208{ 3209 int ret = 0; 3210 struct inet6_ifaddr *ifp = NULL; 3211 struct hlist_node *n; 3212 unsigned int hash = ipv6_addr_hash(addr); 3213 3214 rcu_read_lock_bh(); 3215 hlist_for_each_entry_rcu_bh(ifp, n, &inet6_addr_lst[hash], addr_lst) { 3216 if (!net_eq(dev_net(ifp->idev->dev), net)) 3217 continue; 3218 if (ipv6_addr_equal(&ifp->addr, addr) && 3219 (ifp->flags & IFA_F_HOMEADDRESS)) { 3220 ret = 1; 3221 break; 3222 } 3223 } 3224 rcu_read_unlock_bh(); 3225 return ret; 3226} 3227#endif 3228 3229/* 3230 * Periodic address status verification 3231 */ 3232 3233static void addrconf_verify(unsigned long foo) 3234{ 3235 unsigned long now, next, next_sec, next_sched; 3236 struct inet6_ifaddr *ifp; 3237 struct hlist_node *node; 3238 int i; 3239 3240 rcu_read_lock_bh(); 3241 spin_lock(&addrconf_verify_lock); 3242 now = jiffies; 3243 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY); 3244 3245 del_timer(&addr_chk_timer); 3246 3247 for (i = 0; i < IN6_ADDR_HSIZE; i++) { 3248restart: 3249 hlist_for_each_entry_rcu_bh(ifp, node, 3250 &inet6_addr_lst[i], addr_lst) { 3251 unsigned long age; 3252 3253 if (ifp->flags & IFA_F_PERMANENT) 3254 continue; 3255 3256 spin_lock(&ifp->lock); 3257 /* We try to batch several events at once. */ 3258 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; 3259 3260 if (ifp->valid_lft != INFINITY_LIFE_TIME && 3261 age >= ifp->valid_lft) { 3262 spin_unlock(&ifp->lock); 3263 in6_ifa_hold(ifp); 3264 ipv6_del_addr(ifp); 3265 goto restart; 3266 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) { 3267 spin_unlock(&ifp->lock); 3268 continue; 3269 } else if (age >= ifp->prefered_lft) { 3270 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */ 3271 int deprecate = 0; 3272 3273 if (!(ifp->flags&IFA_F_DEPRECATED)) { 3274 deprecate = 1; 3275 ifp->flags |= IFA_F_DEPRECATED; 3276 } 3277 3278 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)) 3279 next = ifp->tstamp + ifp->valid_lft * HZ; 3280 3281 spin_unlock(&ifp->lock); 3282 3283 if (deprecate) { 3284 in6_ifa_hold(ifp); 3285 3286 ipv6_ifa_notify(0, ifp); 3287 in6_ifa_put(ifp); 3288 goto restart; 3289 } 3290#ifdef CONFIG_IPV6_PRIVACY 3291 } else if ((ifp->flags&IFA_F_TEMPORARY) && 3292 !(ifp->flags&IFA_F_TENTATIVE)) { 3293 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry * 3294 ifp->idev->cnf.dad_transmits * 3295 ifp->idev->nd_parms->retrans_time / HZ; 3296 3297 if (age >= ifp->prefered_lft - regen_advance) { 3298 struct inet6_ifaddr *ifpub = ifp->ifpub; 3299 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 3300 next = ifp->tstamp + ifp->prefered_lft * HZ; 3301 if (!ifp->regen_count && ifpub) { 3302 ifp->regen_count++; 3303 in6_ifa_hold(ifp); 3304 in6_ifa_hold(ifpub); 3305 spin_unlock(&ifp->lock); 3306 3307 spin_lock(&ifpub->lock); 3308 ifpub->regen_count = 0; 3309 spin_unlock(&ifpub->lock); 3310 ipv6_create_tempaddr(ifpub, ifp); 3311 in6_ifa_put(ifpub); 3312 in6_ifa_put(ifp); 3313 goto restart; 3314 } 3315 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next)) 3316 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ; 3317 spin_unlock(&ifp->lock); 3318#endif 3319 } else { 3320 /* ifp->prefered_lft <= ifp->valid_lft */ 3321 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 3322 next = ifp->tstamp + ifp->prefered_lft * HZ; 3323 spin_unlock(&ifp->lock); 3324 } 3325 } 3326 } 3327 3328 next_sec = round_jiffies_up(next); 3329 next_sched = next; 3330 3331 /* If rounded timeout is accurate enough, accept it. */ 3332 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ)) 3333 next_sched = next_sec; 3334 3335 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */ 3336 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX)) 3337 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX; 3338 3339 ADBG((KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n", 3340 now, next, next_sec, next_sched)); 3341 3342 addr_chk_timer.expires = next_sched; 3343 add_timer(&addr_chk_timer); 3344 spin_unlock(&addrconf_verify_lock); 3345 rcu_read_unlock_bh(); 3346} 3347 3348static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local) 3349{ 3350 struct in6_addr *pfx = NULL; 3351 3352 if (addr) 3353 pfx = nla_data(addr); 3354 3355 if (local) { 3356 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx))) 3357 pfx = NULL; 3358 else 3359 pfx = nla_data(local); 3360 } 3361 3362 return pfx; 3363} 3364 3365static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = { 3366 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) }, 3367 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) }, 3368 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) }, 3369}; 3370 3371static int 3372inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 3373{ 3374 struct net *net = sock_net(skb->sk); 3375 struct ifaddrmsg *ifm; 3376 struct nlattr *tb[IFA_MAX+1]; 3377 struct in6_addr *pfx; 3378 int err; 3379 3380 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 3381 if (err < 0) 3382 return err; 3383 3384 ifm = nlmsg_data(nlh); 3385 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]); 3386 if (pfx == NULL) 3387 return -EINVAL; 3388 3389 return inet6_addr_del(net, ifm->ifa_index, pfx, ifm->ifa_prefixlen); 3390} 3391 3392static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags, 3393 u32 prefered_lft, u32 valid_lft) 3394{ 3395 u32 flags; 3396 clock_t expires; 3397 unsigned long timeout; 3398 3399 if (!valid_lft || (prefered_lft > valid_lft)) 3400 return -EINVAL; 3401 3402 timeout = addrconf_timeout_fixup(valid_lft, HZ); 3403 if (addrconf_finite_timeout(timeout)) { 3404 expires = jiffies_to_clock_t(timeout * HZ); 3405 valid_lft = timeout; 3406 flags = RTF_EXPIRES; 3407 } else { 3408 expires = 0; 3409 flags = 0; 3410 ifa_flags |= IFA_F_PERMANENT; 3411 } 3412 3413 timeout = addrconf_timeout_fixup(prefered_lft, HZ); 3414 if (addrconf_finite_timeout(timeout)) { 3415 if (timeout == 0) 3416 ifa_flags |= IFA_F_DEPRECATED; 3417 prefered_lft = timeout; 3418 } 3419 3420 spin_lock_bh(&ifp->lock); 3421 ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags; 3422 ifp->tstamp = jiffies; 3423 ifp->valid_lft = valid_lft; 3424 ifp->prefered_lft = prefered_lft; 3425 3426 spin_unlock_bh(&ifp->lock); 3427 if (!(ifp->flags&IFA_F_TENTATIVE)) 3428 ipv6_ifa_notify(0, ifp); 3429 3430 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev, 3431 expires, flags); 3432 addrconf_verify(0); 3433 3434 return 0; 3435} 3436 3437static int 3438inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 3439{ 3440 struct net *net = sock_net(skb->sk); 3441 struct ifaddrmsg *ifm; 3442 struct nlattr *tb[IFA_MAX+1]; 3443 struct in6_addr *pfx; 3444 struct inet6_ifaddr *ifa; 3445 struct net_device *dev; 3446 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME; 3447 u8 ifa_flags; 3448 int err; 3449 3450 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 3451 if (err < 0) 3452 return err; 3453 3454 ifm = nlmsg_data(nlh); 3455 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]); 3456 if (pfx == NULL) 3457 return -EINVAL; 3458 3459 if (tb[IFA_CACHEINFO]) { 3460 struct ifa_cacheinfo *ci; 3461 3462 ci = nla_data(tb[IFA_CACHEINFO]); 3463 valid_lft = ci->ifa_valid; 3464 preferred_lft = ci->ifa_prefered; 3465 } else { 3466 preferred_lft = INFINITY_LIFE_TIME; 3467 valid_lft = INFINITY_LIFE_TIME; 3468 } 3469 3470 dev = __dev_get_by_index(net, ifm->ifa_index); 3471 if (dev == NULL) 3472 return -ENODEV; 3473 3474 /* We ignore other flags so far. */ 3475 ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS); 3476 3477 ifa = ipv6_get_ifaddr(net, pfx, dev, 1); 3478 if (ifa == NULL) { 3479 /* 3480 * It would be best to check for !NLM_F_CREATE here but 3481 * userspace alreay relies on not having to provide this. 3482 */ 3483 return inet6_addr_add(net, ifm->ifa_index, pfx, 3484 ifm->ifa_prefixlen, ifa_flags, 3485 preferred_lft, valid_lft); 3486 } 3487 3488 if (nlh->nlmsg_flags & NLM_F_EXCL || 3489 !(nlh->nlmsg_flags & NLM_F_REPLACE)) 3490 err = -EEXIST; 3491 else 3492 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft); 3493 3494 in6_ifa_put(ifa); 3495 3496 return err; 3497} 3498 3499static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags, 3500 u8 scope, int ifindex) 3501{ 3502 struct ifaddrmsg *ifm; 3503 3504 ifm = nlmsg_data(nlh); 3505 ifm->ifa_family = AF_INET6; 3506 ifm->ifa_prefixlen = prefixlen; 3507 ifm->ifa_flags = flags; 3508 ifm->ifa_scope = scope; 3509 ifm->ifa_index = ifindex; 3510} 3511 3512static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp, 3513 unsigned long tstamp, u32 preferred, u32 valid) 3514{ 3515 struct ifa_cacheinfo ci; 3516 3517 ci.cstamp = (u32)(TIME_DELTA(cstamp, INITIAL_JIFFIES) / HZ * 100 3518 + TIME_DELTA(cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ); 3519 ci.tstamp = (u32)(TIME_DELTA(tstamp, INITIAL_JIFFIES) / HZ * 100 3520 + TIME_DELTA(tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ); 3521 ci.ifa_prefered = preferred; 3522 ci.ifa_valid = valid; 3523 3524 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci); 3525} 3526 3527static inline int rt_scope(int ifa_scope) 3528{ 3529 if (ifa_scope & IFA_HOST) 3530 return RT_SCOPE_HOST; 3531 else if (ifa_scope & IFA_LINK) 3532 return RT_SCOPE_LINK; 3533 else if (ifa_scope & IFA_SITE) 3534 return RT_SCOPE_SITE; 3535 else 3536 return RT_SCOPE_UNIVERSE; 3537} 3538 3539static inline int inet6_ifaddr_msgsize(void) 3540{ 3541 return NLMSG_ALIGN(sizeof(struct ifaddrmsg)) 3542 + nla_total_size(16) /* IFA_ADDRESS */ 3543 + nla_total_size(sizeof(struct ifa_cacheinfo)); 3544} 3545 3546static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa, 3547 u32 pid, u32 seq, int event, unsigned int flags) 3548{ 3549 struct nlmsghdr *nlh; 3550 u32 preferred, valid; 3551 3552 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags); 3553 if (nlh == NULL) 3554 return -EMSGSIZE; 3555 3556 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope), 3557 ifa->idev->dev->ifindex); 3558 3559 if (!(ifa->flags&IFA_F_PERMANENT)) { 3560 preferred = ifa->prefered_lft; 3561 valid = ifa->valid_lft; 3562 if (preferred != INFINITY_LIFE_TIME) { 3563 long tval = (jiffies - ifa->tstamp)/HZ; 3564 if (preferred > tval) 3565 preferred -= tval; 3566 else 3567 preferred = 0; 3568 if (valid != INFINITY_LIFE_TIME) { 3569 if (valid > tval) 3570 valid -= tval; 3571 else 3572 valid = 0; 3573 } 3574 } 3575 } else { 3576 preferred = INFINITY_LIFE_TIME; 3577 valid = INFINITY_LIFE_TIME; 3578 } 3579 3580 if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 || 3581 put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) { 3582 nlmsg_cancel(skb, nlh); 3583 return -EMSGSIZE; 3584 } 3585 3586 return nlmsg_end(skb, nlh); 3587} 3588 3589static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca, 3590 u32 pid, u32 seq, int event, u16 flags) 3591{ 3592 struct nlmsghdr *nlh; 3593 u8 scope = RT_SCOPE_UNIVERSE; 3594 int ifindex = ifmca->idev->dev->ifindex; 3595 3596 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE) 3597 scope = RT_SCOPE_SITE; 3598 3599 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags); 3600 if (nlh == NULL) 3601 return -EMSGSIZE; 3602 3603 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 3604 if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 || 3605 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp, 3606 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 3607 nlmsg_cancel(skb, nlh); 3608 return -EMSGSIZE; 3609 } 3610 3611 return nlmsg_end(skb, nlh); 3612} 3613 3614static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca, 3615 u32 pid, u32 seq, int event, unsigned int flags) 3616{ 3617 struct nlmsghdr *nlh; 3618 u8 scope = RT_SCOPE_UNIVERSE; 3619 int ifindex = ifaca->aca_idev->dev->ifindex; 3620 3621 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE) 3622 scope = RT_SCOPE_SITE; 3623 3624 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags); 3625 if (nlh == NULL) 3626 return -EMSGSIZE; 3627 3628 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 3629 if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 || 3630 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp, 3631 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 3632 nlmsg_cancel(skb, nlh); 3633 return -EMSGSIZE; 3634 } 3635 3636 return nlmsg_end(skb, nlh); 3637} 3638 3639enum addr_type_t { 3640 UNICAST_ADDR, 3641 MULTICAST_ADDR, 3642 ANYCAST_ADDR, 3643}; 3644 3645/* called with rcu_read_lock() */ 3646static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb, 3647 struct netlink_callback *cb, enum addr_type_t type, 3648 int s_ip_idx, int *p_ip_idx) 3649{ 3650 struct ifmcaddr6 *ifmca; 3651 struct ifacaddr6 *ifaca; 3652 int err = 1; 3653 int ip_idx = *p_ip_idx; 3654 3655 read_lock_bh(&idev->lock); 3656 switch (type) { 3657 case UNICAST_ADDR: { 3658 struct inet6_ifaddr *ifa; 3659 3660 /* unicast address incl. temp addr */ 3661 list_for_each_entry(ifa, &idev->addr_list, if_list) { 3662 if (++ip_idx < s_ip_idx) 3663 continue; 3664 err = inet6_fill_ifaddr(skb, ifa, 3665 NETLINK_CB(cb->skb).pid, 3666 cb->nlh->nlmsg_seq, 3667 RTM_NEWADDR, 3668 NLM_F_MULTI); 3669 if (err <= 0) 3670 break; 3671 } 3672 break; 3673 } 3674 case MULTICAST_ADDR: 3675 /* multicast address */ 3676 for (ifmca = idev->mc_list; ifmca; 3677 ifmca = ifmca->next, ip_idx++) { 3678 if (ip_idx < s_ip_idx) 3679 continue; 3680 err = inet6_fill_ifmcaddr(skb, ifmca, 3681 NETLINK_CB(cb->skb).pid, 3682 cb->nlh->nlmsg_seq, 3683 RTM_GETMULTICAST, 3684 NLM_F_MULTI); 3685 if (err <= 0) 3686 break; 3687 } 3688 break; 3689 case ANYCAST_ADDR: 3690 /* anycast address */ 3691 for (ifaca = idev->ac_list; ifaca; 3692 ifaca = ifaca->aca_next, ip_idx++) { 3693 if (ip_idx < s_ip_idx) 3694 continue; 3695 err = inet6_fill_ifacaddr(skb, ifaca, 3696 NETLINK_CB(cb->skb).pid, 3697 cb->nlh->nlmsg_seq, 3698 RTM_GETANYCAST, 3699 NLM_F_MULTI); 3700 if (err <= 0) 3701 break; 3702 } 3703 break; 3704 default: 3705 break; 3706 } 3707 read_unlock_bh(&idev->lock); 3708 *p_ip_idx = ip_idx; 3709 return err; 3710} 3711 3712static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb, 3713 enum addr_type_t type) 3714{ 3715 struct net *net = sock_net(skb->sk); 3716 int h, s_h; 3717 int idx, ip_idx; 3718 int s_idx, s_ip_idx; 3719 struct net_device *dev; 3720 struct inet6_dev *idev; 3721 struct hlist_head *head; 3722 struct hlist_node *node; 3723 3724 s_h = cb->args[0]; 3725 s_idx = idx = cb->args[1]; 3726 s_ip_idx = ip_idx = cb->args[2]; 3727 3728 rcu_read_lock(); 3729 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 3730 idx = 0; 3731 head = &net->dev_index_head[h]; 3732 hlist_for_each_entry_rcu(dev, node, head, index_hlist) { 3733 if (idx < s_idx) 3734 goto cont; 3735 if (h > s_h || idx > s_idx) 3736 s_ip_idx = 0; 3737 ip_idx = 0; 3738 idev = __in6_dev_get(dev); 3739 if (!idev) 3740 goto cont; 3741 3742 if (in6_dump_addrs(idev, skb, cb, type, 3743 s_ip_idx, &ip_idx) <= 0) 3744 goto done; 3745cont: 3746 idx++; 3747 } 3748 } 3749done: 3750 rcu_read_unlock(); 3751 cb->args[0] = h; 3752 cb->args[1] = idx; 3753 cb->args[2] = ip_idx; 3754 3755 return skb->len; 3756} 3757 3758static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) 3759{ 3760 enum addr_type_t type = UNICAST_ADDR; 3761 3762 return inet6_dump_addr(skb, cb, type); 3763} 3764 3765static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb) 3766{ 3767 enum addr_type_t type = MULTICAST_ADDR; 3768 3769 return inet6_dump_addr(skb, cb, type); 3770} 3771 3772 3773static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb) 3774{ 3775 enum addr_type_t type = ANYCAST_ADDR; 3776 3777 return inet6_dump_addr(skb, cb, type); 3778} 3779 3780static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh, 3781 void *arg) 3782{ 3783 struct net *net = sock_net(in_skb->sk); 3784 struct ifaddrmsg *ifm; 3785 struct nlattr *tb[IFA_MAX+1]; 3786 struct in6_addr *addr = NULL; 3787 struct net_device *dev = NULL; 3788 struct inet6_ifaddr *ifa; 3789 struct sk_buff *skb; 3790 int err; 3791 3792 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 3793 if (err < 0) 3794 goto errout; 3795 3796 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]); 3797 if (addr == NULL) { 3798 err = -EINVAL; 3799 goto errout; 3800 } 3801 3802 ifm = nlmsg_data(nlh); 3803 if (ifm->ifa_index) 3804 dev = __dev_get_by_index(net, ifm->ifa_index); 3805 3806 ifa = ipv6_get_ifaddr(net, addr, dev, 1); 3807 if (!ifa) { 3808 err = -EADDRNOTAVAIL; 3809 goto errout; 3810 } 3811 3812 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL); 3813 if (!skb) { 3814 err = -ENOBUFS; 3815 goto errout_ifa; 3816 } 3817 3818 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).pid, 3819 nlh->nlmsg_seq, RTM_NEWADDR, 0); 3820 if (err < 0) { 3821 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 3822 WARN_ON(err == -EMSGSIZE); 3823 kfree_skb(skb); 3824 goto errout_ifa; 3825 } 3826 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid); 3827errout_ifa: 3828 in6_ifa_put(ifa); 3829errout: 3830 return err; 3831} 3832 3833static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa) 3834{ 3835 struct sk_buff *skb; 3836 struct net *net = dev_net(ifa->idev->dev); 3837 int err = -ENOBUFS; 3838 3839 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC); 3840 if (skb == NULL) 3841 goto errout; 3842 3843 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0); 3844 if (err < 0) { 3845 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 3846 WARN_ON(err == -EMSGSIZE); 3847 kfree_skb(skb); 3848 goto errout; 3849 } 3850 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC); 3851 return; 3852errout: 3853 if (err < 0) 3854 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err); 3855} 3856 3857static inline void ipv6_store_devconf(struct ipv6_devconf *cnf, 3858 __s32 *array, int bytes) 3859{ 3860 BUG_ON(bytes < (DEVCONF_MAX * 4)); 3861 3862 memset(array, 0, bytes); 3863 array[DEVCONF_FORWARDING] = cnf->forwarding; 3864 array[DEVCONF_HOPLIMIT] = cnf->hop_limit; 3865 array[DEVCONF_MTU6] = cnf->mtu6; 3866 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra; 3867 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects; 3868 array[DEVCONF_AUTOCONF] = cnf->autoconf; 3869 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits; 3870 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits; 3871 array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval; 3872 array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay; 3873 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version; 3874#ifdef CONFIG_IPV6_PRIVACY 3875 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr; 3876 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft; 3877 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft; 3878 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry; 3879 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor; 3880#endif 3881 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses; 3882 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr; 3883 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo; 3884#ifdef CONFIG_IPV6_ROUTER_PREF 3885 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref; 3886 array[DEVCONF_RTR_PROBE_INTERVAL] = cnf->rtr_probe_interval; 3887#ifdef CONFIG_IPV6_ROUTE_INFO 3888 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen; 3889#endif 3890#endif 3891 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp; 3892 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route; 3893#ifdef CONFIG_IPV6_OPTIMISTIC_DAD 3894 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad; 3895#endif 3896#ifdef CONFIG_IPV6_MROUTE 3897 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding; 3898#endif 3899 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6; 3900 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad; 3901 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao; 3902} 3903 3904static inline size_t inet6_if_nlmsg_size(void) 3905{ 3906 return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 3907 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 3908 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 3909 + nla_total_size(4) /* IFLA_MTU */ 3910 + nla_total_size(4) /* IFLA_LINK */ 3911 + nla_total_size( /* IFLA_PROTINFO */ 3912 nla_total_size(4) /* IFLA_INET6_FLAGS */ 3913 + nla_total_size(sizeof(struct ifla_cacheinfo)) 3914 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */ 3915 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */ 3916 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */ 3917 ); 3918} 3919 3920static inline void __snmp6_fill_stats(u64 *stats, void __percpu **mib, 3921 int items, int bytes) 3922{ 3923 int i; 3924 int pad = bytes - sizeof(u64) * items; 3925 BUG_ON(pad < 0); 3926 3927 /* Use put_unaligned() because stats may not be aligned for u64. */ 3928 put_unaligned(items, &stats[0]); 3929 for (i = 1; i < items; i++) 3930 put_unaligned(snmp_fold_field(mib, i), &stats[i]); 3931 3932 memset(&stats[items], 0, pad); 3933} 3934 3935static inline void __snmp6_fill_stats64(u64 *stats, void __percpu **mib, 3936 int items, int bytes, size_t syncpoff) 3937{ 3938 int i; 3939 int pad = bytes - sizeof(u64) * items; 3940 BUG_ON(pad < 0); 3941 3942 /* Use put_unaligned() because stats may not be aligned for u64. */ 3943 put_unaligned(items, &stats[0]); 3944 for (i = 1; i < items; i++) 3945 put_unaligned(snmp_fold_field64(mib, i, syncpoff), &stats[i]); 3946 3947 memset(&stats[items], 0, pad); 3948} 3949 3950static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype, 3951 int bytes) 3952{ 3953 switch (attrtype) { 3954 case IFLA_INET6_STATS: 3955 __snmp6_fill_stats64(stats, (void __percpu **)idev->stats.ipv6, 3956 IPSTATS_MIB_MAX, bytes, offsetof(struct ipstats_mib, syncp)); 3957 break; 3958 case IFLA_INET6_ICMP6STATS: 3959 __snmp6_fill_stats(stats, (void __percpu **)idev->stats.icmpv6, ICMP6_MIB_MAX, bytes); 3960 break; 3961 } 3962} 3963 3964static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev, 3965 u32 pid, u32 seq, int event, unsigned int flags) 3966{ 3967 struct net_device *dev = idev->dev; 3968 struct nlattr *nla; 3969 struct ifinfomsg *hdr; 3970 struct nlmsghdr *nlh; 3971 void *protoinfo; 3972 struct ifla_cacheinfo ci; 3973 3974 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags); 3975 if (nlh == NULL) 3976 return -EMSGSIZE; 3977 3978 hdr = nlmsg_data(nlh); 3979 hdr->ifi_family = AF_INET6; 3980 hdr->__ifi_pad = 0; 3981 hdr->ifi_type = dev->type; 3982 hdr->ifi_index = dev->ifindex; 3983 hdr->ifi_flags = dev_get_flags(dev); 3984 hdr->ifi_change = 0; 3985 3986 NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name); 3987 3988 if (dev->addr_len) 3989 NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr); 3990 3991 NLA_PUT_U32(skb, IFLA_MTU, dev->mtu); 3992 if (dev->ifindex != dev->iflink) 3993 NLA_PUT_U32(skb, IFLA_LINK, dev->iflink); 3994 3995 protoinfo = nla_nest_start(skb, IFLA_PROTINFO); 3996 if (protoinfo == NULL) 3997 goto nla_put_failure; 3998 3999 NLA_PUT_U32(skb, IFLA_INET6_FLAGS, idev->if_flags); 4000 4001 ci.max_reasm_len = IPV6_MAXPLEN; 4002 ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100 4003 + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ); 4004 ci.reachable_time = idev->nd_parms->reachable_time; 4005 ci.retrans_time = idev->nd_parms->retrans_time; 4006 NLA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci); 4007 4008 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32)); 4009 if (nla == NULL) 4010 goto nla_put_failure; 4011 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla)); 4012 4013 4014 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64)); 4015 if (nla == NULL) 4016 goto nla_put_failure; 4017 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla)); 4018 4019 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64)); 4020 if (nla == NULL) 4021 goto nla_put_failure; 4022 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla)); 4023 4024 nla_nest_end(skb, protoinfo); 4025 return nlmsg_end(skb, nlh); 4026 4027nla_put_failure: 4028 nlmsg_cancel(skb, nlh); 4029 return -EMSGSIZE; 4030} 4031 4032static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 4033{ 4034 struct net *net = sock_net(skb->sk); 4035 int h, s_h; 4036 int idx = 0, s_idx; 4037 struct net_device *dev; 4038 struct inet6_dev *idev; 4039 struct hlist_head *head; 4040 struct hlist_node *node; 4041 4042 s_h = cb->args[0]; 4043 s_idx = cb->args[1]; 4044 4045 rcu_read_lock(); 4046 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 4047 idx = 0; 4048 head = &net->dev_index_head[h]; 4049 hlist_for_each_entry_rcu(dev, node, head, index_hlist) { 4050 if (idx < s_idx) 4051 goto cont; 4052 idev = __in6_dev_get(dev); 4053 if (!idev) 4054 goto cont; 4055 if (inet6_fill_ifinfo(skb, idev, 4056 NETLINK_CB(cb->skb).pid, 4057 cb->nlh->nlmsg_seq, 4058 RTM_NEWLINK, NLM_F_MULTI) <= 0) 4059 goto out; 4060cont: 4061 idx++; 4062 } 4063 } 4064out: 4065 rcu_read_unlock(); 4066 cb->args[1] = idx; 4067 cb->args[0] = h; 4068 4069 return skb->len; 4070} 4071 4072void inet6_ifinfo_notify(int event, struct inet6_dev *idev) 4073{ 4074 struct sk_buff *skb; 4075 struct net *net = dev_net(idev->dev); 4076 int err = -ENOBUFS; 4077 4078 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC); 4079 if (skb == NULL) 4080 goto errout; 4081 4082 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0); 4083 if (err < 0) { 4084 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */ 4085 WARN_ON(err == -EMSGSIZE); 4086 kfree_skb(skb); 4087 goto errout; 4088 } 4089 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC); 4090 return; 4091errout: 4092 if (err < 0) 4093 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err); 4094} 4095 4096static inline size_t inet6_prefix_nlmsg_size(void) 4097{ 4098 return NLMSG_ALIGN(sizeof(struct prefixmsg)) 4099 + nla_total_size(sizeof(struct in6_addr)) 4100 + nla_total_size(sizeof(struct prefix_cacheinfo)); 4101} 4102 4103static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev, 4104 struct prefix_info *pinfo, u32 pid, u32 seq, 4105 int event, unsigned int flags) 4106{ 4107 struct prefixmsg *pmsg; 4108 struct nlmsghdr *nlh; 4109 struct prefix_cacheinfo ci; 4110 4111 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*pmsg), flags); 4112 if (nlh == NULL) 4113 return -EMSGSIZE; 4114 4115 pmsg = nlmsg_data(nlh); 4116 pmsg->prefix_family = AF_INET6; 4117 pmsg->prefix_pad1 = 0; 4118 pmsg->prefix_pad2 = 0; 4119 pmsg->prefix_ifindex = idev->dev->ifindex; 4120 pmsg->prefix_len = pinfo->prefix_len; 4121 pmsg->prefix_type = pinfo->type; 4122 pmsg->prefix_pad3 = 0; 4123 pmsg->prefix_flags = 0; 4124 if (pinfo->onlink) 4125 pmsg->prefix_flags |= IF_PREFIX_ONLINK; 4126 if (pinfo->autoconf) 4127 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF; 4128 4129 NLA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix); 4130 4131 ci.preferred_time = ntohl(pinfo->prefered); 4132 ci.valid_time = ntohl(pinfo->valid); 4133 NLA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci); 4134 4135 return nlmsg_end(skb, nlh); 4136 4137nla_put_failure: 4138 nlmsg_cancel(skb, nlh); 4139 return -EMSGSIZE; 4140} 4141 4142static void inet6_prefix_notify(int event, struct inet6_dev *idev, 4143 struct prefix_info *pinfo) 4144{ 4145 struct sk_buff *skb; 4146 struct net *net = dev_net(idev->dev); 4147 int err = -ENOBUFS; 4148 4149 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC); 4150 if (skb == NULL) 4151 goto errout; 4152 4153 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0); 4154 if (err < 0) { 4155 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */ 4156 WARN_ON(err == -EMSGSIZE); 4157 kfree_skb(skb); 4158 goto errout; 4159 } 4160 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC); 4161 return; 4162errout: 4163 if (err < 0) 4164 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err); 4165} 4166 4167static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 4168{ 4169 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp); 4170 4171 switch (event) { 4172 case RTM_NEWADDR: 4173 /* 4174 * If the address was optimistic 4175 * we inserted the route at the start of 4176 * our DAD process, so we don't need 4177 * to do it again 4178 */ 4179 if (!(ifp->rt->rt6i_node)) 4180 ip6_ins_rt(ifp->rt); 4181 if (ifp->idev->cnf.forwarding) 4182 addrconf_join_anycast(ifp); 4183 break; 4184 case RTM_DELADDR: 4185 if (ifp->idev->cnf.forwarding) 4186 addrconf_leave_anycast(ifp); 4187 addrconf_leave_solict(ifp->idev, &ifp->addr); 4188 dst_hold(&ifp->rt->dst); 4189 4190 if (ifp->state == INET6_IFADDR_STATE_DEAD && 4191 ip6_del_rt(ifp->rt)) 4192 dst_free(&ifp->rt->dst); 4193 break; 4194 } 4195} 4196 4197static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 4198{ 4199 rcu_read_lock_bh(); 4200 if (likely(ifp->idev->dead == 0)) 4201 __ipv6_ifa_notify(event, ifp); 4202 rcu_read_unlock_bh(); 4203} 4204 4205#ifdef CONFIG_SYSCTL 4206 4207static 4208int addrconf_sysctl_forward(ctl_table *ctl, int write, 4209 void __user *buffer, size_t *lenp, loff_t *ppos) 4210{ 4211 int *valp = ctl->data; 4212 int val = *valp; 4213 loff_t pos = *ppos; 4214 int ret; 4215 4216 ret = proc_dointvec(ctl, write, buffer, lenp, ppos); 4217 4218 if (write) 4219 ret = addrconf_fixup_forwarding(ctl, valp, val); 4220 if (ret) 4221 *ppos = pos; 4222 return ret; 4223} 4224 4225static void dev_disable_change(struct inet6_dev *idev) 4226{ 4227 if (!idev || !idev->dev) 4228 return; 4229 4230 if (idev->cnf.disable_ipv6) 4231 addrconf_notify(NULL, NETDEV_DOWN, idev->dev); 4232 else 4233 addrconf_notify(NULL, NETDEV_UP, idev->dev); 4234} 4235 4236static void addrconf_disable_change(struct net *net, __s32 newf) 4237{ 4238 struct net_device *dev; 4239 struct inet6_dev *idev; 4240 4241 rcu_read_lock(); 4242 for_each_netdev_rcu(net, dev) { 4243 idev = __in6_dev_get(dev); 4244 if (idev) { 4245 int changed = (!idev->cnf.disable_ipv6) ^ (!newf); 4246 idev->cnf.disable_ipv6 = newf; 4247 if (changed) 4248 dev_disable_change(idev); 4249 } 4250 } 4251 rcu_read_unlock(); 4252} 4253 4254static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int old) 4255{ 4256 struct net *net; 4257 4258 net = (struct net *)table->extra2; 4259 4260 if (p == &net->ipv6.devconf_dflt->disable_ipv6) 4261 return 0; 4262 4263 if (!rtnl_trylock()) { 4264 /* Restore the original values before restarting */ 4265 *p = old; 4266 return restart_syscall(); 4267 } 4268 4269 if (p == &net->ipv6.devconf_all->disable_ipv6) { 4270 __s32 newf = net->ipv6.devconf_all->disable_ipv6; 4271 net->ipv6.devconf_dflt->disable_ipv6 = newf; 4272 addrconf_disable_change(net, newf); 4273 } else if ((!*p) ^ (!old)) 4274 dev_disable_change((struct inet6_dev *)table->extra1); 4275 4276 rtnl_unlock(); 4277 return 0; 4278} 4279 4280static 4281int addrconf_sysctl_disable(ctl_table *ctl, int write, 4282 void __user *buffer, size_t *lenp, loff_t *ppos) 4283{ 4284 int *valp = ctl->data; 4285 int val = *valp; 4286 loff_t pos = *ppos; 4287 int ret; 4288 4289 ret = proc_dointvec(ctl, write, buffer, lenp, ppos); 4290 4291 if (write) 4292 ret = addrconf_disable_ipv6(ctl, valp, val); 4293 if (ret) 4294 *ppos = pos; 4295 return ret; 4296} 4297 4298static struct addrconf_sysctl_table 4299{ 4300 struct ctl_table_header *sysctl_header; 4301 ctl_table addrconf_vars[DEVCONF_MAX+1]; 4302 char *dev_name; 4303} addrconf_sysctl __read_mostly = { 4304 .sysctl_header = NULL, 4305 .addrconf_vars = { 4306 { 4307 .procname = "forwarding", 4308 .data = &ipv6_devconf.forwarding, 4309 .maxlen = sizeof(int), 4310 .mode = 0644, 4311 .proc_handler = addrconf_sysctl_forward, 4312 }, 4313 { 4314 .procname = "hop_limit", 4315 .data = &ipv6_devconf.hop_limit, 4316 .maxlen = sizeof(int), 4317 .mode = 0644, 4318 .proc_handler = proc_dointvec, 4319 }, 4320 { 4321 .procname = "mtu", 4322 .data = &ipv6_devconf.mtu6, 4323 .maxlen = sizeof(int), 4324 .mode = 0644, 4325 .proc_handler = proc_dointvec, 4326 }, 4327 { 4328 .procname = "accept_ra", 4329 .data = &ipv6_devconf.accept_ra, 4330 .maxlen = sizeof(int), 4331 .mode = 0644, 4332 .proc_handler = proc_dointvec, 4333 }, 4334 { 4335 .procname = "accept_redirects", 4336 .data = &ipv6_devconf.accept_redirects, 4337 .maxlen = sizeof(int), 4338 .mode = 0644, 4339 .proc_handler = proc_dointvec, 4340 }, 4341 { 4342 .procname = "autoconf", 4343 .data = &ipv6_devconf.autoconf, 4344 .maxlen = sizeof(int), 4345 .mode = 0644, 4346 .proc_handler = proc_dointvec, 4347 }, 4348 { 4349 .procname = "dad_transmits", 4350 .data = &ipv6_devconf.dad_transmits, 4351 .maxlen = sizeof(int), 4352 .mode = 0644, 4353 .proc_handler = proc_dointvec, 4354 }, 4355 { 4356 .procname = "router_solicitations", 4357 .data = &ipv6_devconf.rtr_solicits, 4358 .maxlen = sizeof(int), 4359 .mode = 0644, 4360 .proc_handler = proc_dointvec, 4361 }, 4362 { 4363 .procname = "router_solicitation_interval", 4364 .data = &ipv6_devconf.rtr_solicit_interval, 4365 .maxlen = sizeof(int), 4366 .mode = 0644, 4367 .proc_handler = proc_dointvec_jiffies, 4368 }, 4369 { 4370 .procname = "router_solicitation_delay", 4371 .data = &ipv6_devconf.rtr_solicit_delay, 4372 .maxlen = sizeof(int), 4373 .mode = 0644, 4374 .proc_handler = proc_dointvec_jiffies, 4375 }, 4376 { 4377 .procname = "force_mld_version", 4378 .data = &ipv6_devconf.force_mld_version, 4379 .maxlen = sizeof(int), 4380 .mode = 0644, 4381 .proc_handler = proc_dointvec, 4382 }, 4383#ifdef CONFIG_IPV6_PRIVACY 4384 { 4385 .procname = "use_tempaddr", 4386 .data = &ipv6_devconf.use_tempaddr, 4387 .maxlen = sizeof(int), 4388 .mode = 0644, 4389 .proc_handler = proc_dointvec, 4390 }, 4391 { 4392 .procname = "temp_valid_lft", 4393 .data = &ipv6_devconf.temp_valid_lft, 4394 .maxlen = sizeof(int), 4395 .mode = 0644, 4396 .proc_handler = proc_dointvec, 4397 }, 4398 { 4399 .procname = "temp_prefered_lft", 4400 .data = &ipv6_devconf.temp_prefered_lft, 4401 .maxlen = sizeof(int), 4402 .mode = 0644, 4403 .proc_handler = proc_dointvec, 4404 }, 4405 { 4406 .procname = "regen_max_retry", 4407 .data = &ipv6_devconf.regen_max_retry, 4408 .maxlen = sizeof(int), 4409 .mode = 0644, 4410 .proc_handler = proc_dointvec, 4411 }, 4412 { 4413 .procname = "max_desync_factor", 4414 .data = &ipv6_devconf.max_desync_factor, 4415 .maxlen = sizeof(int), 4416 .mode = 0644, 4417 .proc_handler = proc_dointvec, 4418 }, 4419#endif 4420 { 4421 .procname = "max_addresses", 4422 .data = &ipv6_devconf.max_addresses, 4423 .maxlen = sizeof(int), 4424 .mode = 0644, 4425 .proc_handler = proc_dointvec, 4426 }, 4427 { 4428 .procname = "accept_ra_defrtr", 4429 .data = &ipv6_devconf.accept_ra_defrtr, 4430 .maxlen = sizeof(int), 4431 .mode = 0644, 4432 .proc_handler = proc_dointvec, 4433 }, 4434 { 4435 .procname = "accept_ra_pinfo", 4436 .data = &ipv6_devconf.accept_ra_pinfo, 4437 .maxlen = sizeof(int), 4438 .mode = 0644, 4439 .proc_handler = proc_dointvec, 4440 }, 4441#ifdef CONFIG_IPV6_ROUTER_PREF 4442 { 4443 .procname = "accept_ra_rtr_pref", 4444 .data = &ipv6_devconf.accept_ra_rtr_pref, 4445 .maxlen = sizeof(int), 4446 .mode = 0644, 4447 .proc_handler = proc_dointvec, 4448 }, 4449 { 4450 .procname = "router_probe_interval", 4451 .data = &ipv6_devconf.rtr_probe_interval, 4452 .maxlen = sizeof(int), 4453 .mode = 0644, 4454 .proc_handler = proc_dointvec_jiffies, 4455 }, 4456#ifdef CONFIG_IPV6_ROUTE_INFO 4457 { 4458 .procname = "accept_ra_rt_info_max_plen", 4459 .data = &ipv6_devconf.accept_ra_rt_info_max_plen, 4460 .maxlen = sizeof(int), 4461 .mode = 0644, 4462 .proc_handler = proc_dointvec, 4463 }, 4464#endif 4465#endif 4466 { 4467 .procname = "proxy_ndp", 4468 .data = &ipv6_devconf.proxy_ndp, 4469 .maxlen = sizeof(int), 4470 .mode = 0644, 4471 .proc_handler = proc_dointvec, 4472 }, 4473 { 4474 .procname = "accept_source_route", 4475 .data = &ipv6_devconf.accept_source_route, 4476 .maxlen = sizeof(int), 4477 .mode = 0644, 4478 .proc_handler = proc_dointvec, 4479 }, 4480#ifdef CONFIG_IPV6_OPTIMISTIC_DAD 4481 { 4482 .procname = "optimistic_dad", 4483 .data = &ipv6_devconf.optimistic_dad, 4484 .maxlen = sizeof(int), 4485 .mode = 0644, 4486 .proc_handler = proc_dointvec, 4487 4488 }, 4489#endif 4490#ifdef CONFIG_IPV6_MROUTE 4491 { 4492 .procname = "mc_forwarding", 4493 .data = &ipv6_devconf.mc_forwarding, 4494 .maxlen = sizeof(int), 4495 .mode = 0444, 4496 .proc_handler = proc_dointvec, 4497 }, 4498#endif 4499 { 4500 .procname = "disable_ipv6", 4501 .data = &ipv6_devconf.disable_ipv6, 4502 .maxlen = sizeof(int), 4503 .mode = 0644, 4504 .proc_handler = addrconf_sysctl_disable, 4505 }, 4506 { 4507 .procname = "accept_dad", 4508 .data = &ipv6_devconf.accept_dad, 4509 .maxlen = sizeof(int), 4510 .mode = 0644, 4511 .proc_handler = proc_dointvec, 4512 }, 4513 { 4514 .procname = "force_tllao", 4515 .data = &ipv6_devconf.force_tllao, 4516 .maxlen = sizeof(int), 4517 .mode = 0644, 4518 .proc_handler = proc_dointvec 4519 }, 4520 { 4521 /* sentinel */ 4522 } 4523 }, 4524}; 4525 4526static int __addrconf_sysctl_register(struct net *net, char *dev_name, 4527 struct inet6_dev *idev, struct ipv6_devconf *p) 4528{ 4529 int i; 4530 struct addrconf_sysctl_table *t; 4531 4532#define ADDRCONF_CTL_PATH_DEV 3 4533 4534 struct ctl_path addrconf_ctl_path[] = { 4535 { .procname = "net", }, 4536 { .procname = "ipv6", }, 4537 { .procname = "conf", }, 4538 { /* to be set */ }, 4539 { }, 4540 }; 4541 4542 4543 t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL); 4544 if (t == NULL) 4545 goto out; 4546 4547 for (i = 0; t->addrconf_vars[i].data; i++) { 4548 t->addrconf_vars[i].data += (char *)p - (char *)&ipv6_devconf; 4549 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */ 4550 t->addrconf_vars[i].extra2 = net; 4551 } 4552 4553 /* 4554 * Make a copy of dev_name, because '.procname' is regarded as const 4555 * by sysctl and we wouldn't want anyone to change it under our feet 4556 * (see SIOCSIFNAME). 4557 */ 4558 t->dev_name = kstrdup(dev_name, GFP_KERNEL); 4559 if (!t->dev_name) 4560 goto free; 4561 4562 addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].procname = t->dev_name; 4563 4564 t->sysctl_header = register_net_sysctl_table(net, addrconf_ctl_path, 4565 t->addrconf_vars); 4566 if (t->sysctl_header == NULL) 4567 goto free_procname; 4568 4569 p->sysctl = t; 4570 return 0; 4571 4572free_procname: 4573 kfree(t->dev_name); 4574free: 4575 kfree(t); 4576out: 4577 return -ENOBUFS; 4578} 4579 4580static void __addrconf_sysctl_unregister(struct ipv6_devconf *p) 4581{ 4582 struct addrconf_sysctl_table *t; 4583 4584 if (p->sysctl == NULL) 4585 return; 4586 4587 t = p->sysctl; 4588 p->sysctl = NULL; 4589 unregister_sysctl_table(t->sysctl_header); 4590 kfree(t->dev_name); 4591 kfree(t); 4592} 4593 4594static void addrconf_sysctl_register(struct inet6_dev *idev) 4595{ 4596 neigh_sysctl_register(idev->dev, idev->nd_parms, "ipv6", 4597 &ndisc_ifinfo_sysctl_change); 4598 __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name, 4599 idev, &idev->cnf); 4600} 4601 4602static void addrconf_sysctl_unregister(struct inet6_dev *idev) 4603{ 4604 __addrconf_sysctl_unregister(&idev->cnf); 4605 neigh_sysctl_unregister(idev->nd_parms); 4606} 4607 4608 4609#endif 4610 4611static int __net_init addrconf_init_net(struct net *net) 4612{ 4613 int err; 4614 struct ipv6_devconf *all, *dflt; 4615 4616 err = -ENOMEM; 4617 all = &ipv6_devconf; 4618 dflt = &ipv6_devconf_dflt; 4619 4620 if (!net_eq(net, &init_net)) { 4621 all = kmemdup(all, sizeof(ipv6_devconf), GFP_KERNEL); 4622 if (all == NULL) 4623 goto err_alloc_all; 4624 4625 dflt = kmemdup(dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL); 4626 if (dflt == NULL) 4627 goto err_alloc_dflt; 4628 } else { 4629 /* these will be inherited by all namespaces */ 4630 dflt->autoconf = ipv6_defaults.autoconf; 4631 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6; 4632 } 4633 4634 net->ipv6.devconf_all = all; 4635 net->ipv6.devconf_dflt = dflt; 4636 4637#ifdef CONFIG_SYSCTL 4638 err = __addrconf_sysctl_register(net, "all", NULL, all); 4639 if (err < 0) 4640 goto err_reg_all; 4641 4642 err = __addrconf_sysctl_register(net, "default", NULL, dflt); 4643 if (err < 0) 4644 goto err_reg_dflt; 4645#endif 4646 return 0; 4647 4648#ifdef CONFIG_SYSCTL 4649err_reg_dflt: 4650 __addrconf_sysctl_unregister(all); 4651err_reg_all: 4652 kfree(dflt); 4653#endif 4654err_alloc_dflt: 4655 kfree(all); 4656err_alloc_all: 4657 return err; 4658} 4659 4660static void __net_exit addrconf_exit_net(struct net *net) 4661{ 4662#ifdef CONFIG_SYSCTL 4663 __addrconf_sysctl_unregister(net->ipv6.devconf_dflt); 4664 __addrconf_sysctl_unregister(net->ipv6.devconf_all); 4665#endif 4666 if (!net_eq(net, &init_net)) { 4667 kfree(net->ipv6.devconf_dflt); 4668 kfree(net->ipv6.devconf_all); 4669 } 4670} 4671 4672static struct pernet_operations addrconf_ops = { 4673 .init = addrconf_init_net, 4674 .exit = addrconf_exit_net, 4675}; 4676 4677/* 4678 * Device notifier 4679 */ 4680 4681int register_inet6addr_notifier(struct notifier_block *nb) 4682{ 4683 return atomic_notifier_chain_register(&inet6addr_chain, nb); 4684} 4685EXPORT_SYMBOL(register_inet6addr_notifier); 4686 4687int unregister_inet6addr_notifier(struct notifier_block *nb) 4688{ 4689 return atomic_notifier_chain_unregister(&inet6addr_chain, nb); 4690} 4691EXPORT_SYMBOL(unregister_inet6addr_notifier); 4692 4693/* 4694 * Init / cleanup code 4695 */ 4696 4697int __init addrconf_init(void) 4698{ 4699 int i, err; 4700 /*Foxconn tab tseng added, 2013/07/23, for dhcp6c wan ipv6 DAD*/ 4701 ipv6_wan_DAD_flag= create_proc_entry("ipv6_wan_DAD_detected",0,NULL); 4702 if (ipv6_wan_DAD_flag == NULL) { 4703 printk(KERN_EMERG "Error: Could not initialize /proc/\n"); 4704 }else{ 4705 ipv6_wan_DAD_flag->read_proc = ipv6_read_wan_DAD_stats; 4706 } 4707 /*Foxconn tab tseng added end, 2013/07/23, for dhcp6c wan ipv6 DAD*/ 4708 err = ipv6_addr_label_init(); 4709 if (err < 0) { 4710 printk(KERN_CRIT "IPv6 Addrconf:" 4711 " cannot initialize default policy table: %d.\n", err); 4712 goto out; 4713 } 4714 4715 err = register_pernet_subsys(&addrconf_ops); 4716 if (err < 0) 4717 goto out_addrlabel; 4718 4719 /* The addrconf netdev notifier requires that loopback_dev 4720 * has it's ipv6 private information allocated and setup 4721 * before it can bring up and give link-local addresses 4722 * to other devices which are up. 4723 * 4724 * Unfortunately, loopback_dev is not necessarily the first 4725 * entry in the global dev_base list of net devices. In fact, 4726 * it is likely to be the very last entry on that list. 4727 * So this causes the notifier registry below to try and 4728 * give link-local addresses to all devices besides loopback_dev 4729 * first, then loopback_dev, which cases all the non-loopback_dev 4730 * devices to fail to get a link-local address. 4731 * 4732 * So, as a temporary fix, allocate the ipv6 structure for 4733 * loopback_dev first by hand. 4734 * Longer term, all of the dependencies ipv6 has upon the loopback 4735 * device and it being up should be removed. 4736 */ 4737 rtnl_lock(); 4738 if (!ipv6_add_dev(init_net.loopback_dev)) 4739 err = -ENOMEM; 4740 rtnl_unlock(); 4741 if (err) 4742 goto errlo; 4743 4744 for (i = 0; i < IN6_ADDR_HSIZE; i++) 4745 INIT_HLIST_HEAD(&inet6_addr_lst[i]); 4746 4747 register_netdevice_notifier(&ipv6_dev_notf); 4748 4749 addrconf_verify(0); 4750 4751 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo); 4752 if (err < 0) 4753 goto errout; 4754 4755 /* Only the first call to __rtnl_register can fail */ 4756 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL); 4757 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL); 4758 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr, inet6_dump_ifaddr); 4759 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL, inet6_dump_ifmcaddr); 4760 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL, inet6_dump_ifacaddr); 4761 4762 ipv6_addr_label_rtnl_register(); 4763 4764 return 0; 4765errout: 4766 unregister_netdevice_notifier(&ipv6_dev_notf); 4767errlo: 4768 unregister_pernet_subsys(&addrconf_ops); 4769out_addrlabel: 4770 ipv6_addr_label_cleanup(); 4771out: 4772 return err; 4773} 4774 4775void addrconf_cleanup(void) 4776{ 4777 struct net_device *dev; 4778 int i; 4779 4780 unregister_netdevice_notifier(&ipv6_dev_notf); 4781 unregister_pernet_subsys(&addrconf_ops); 4782 ipv6_addr_label_cleanup(); 4783 4784 rtnl_lock(); 4785 4786 /* clean dev list */ 4787 for_each_netdev(&init_net, dev) { 4788 if (__in6_dev_get(dev) == NULL) 4789 continue; 4790 addrconf_ifdown(dev, 1); 4791 } 4792 addrconf_ifdown(init_net.loopback_dev, 2); 4793 4794 /* 4795 * Check hash table. 4796 */ 4797 spin_lock_bh(&addrconf_hash_lock); 4798 for (i = 0; i < IN6_ADDR_HSIZE; i++) 4799 WARN_ON(!hlist_empty(&inet6_addr_lst[i])); 4800 spin_unlock_bh(&addrconf_hash_lock); 4801 4802 del_timer(&addr_chk_timer); 4803 rtnl_unlock(); 4804} 4805 4806/* Foxconn added start pling 08/16/2010 */ 4807int restore_ipv6_forwarding(struct net_device *dev) 4808{ 4809 struct inet6_dev *idev; 4810 idev = ipv6_find_idev(dev); 4811 if (idev) 4812 { 4813 ipv6_devconf.forwarding = 1; 4814 idev->cnf.forwarding = 1; 4815 } 4816 4817 return 0; 4818} 4819/* Foxconn added end pling 08/16/2010 */ 4820 4821/* Foxconn added start 11/21/2014 */ 4822/* Export a function for IPv6 DNS hijack to use, to avoid kernel message */ 4823struct inet6_dev * ipv6_find_idev2(struct net_device *dev) 4824{ 4825 struct inet_dev *idev; 4826 4827 rtnl_lock(); 4828 idev = ipv6_find_idev(dev); 4829 rtnl_unlock(); 4830 4831 return idev; 4832} 4833EXPORT_SYMBOL(ipv6_find_idev2); 4834/* Foxconn added end 11/21/2014 */ 4835