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