11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * IPv6 Address [auto]configuration 31da177e4SLinus Torvalds * Linux INET6 implementation 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Authors: 61da177e4SLinus Torvalds * Pedro Roque <roque@di.fc.ul.pt> 71da177e4SLinus Torvalds * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 101da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 111da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 121da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 131da177e4SLinus Torvalds */ 141da177e4SLinus Torvalds 151da177e4SLinus Torvalds /* 161da177e4SLinus Torvalds * Changes: 171da177e4SLinus Torvalds * 181da177e4SLinus Torvalds * Janos Farkas : delete timer on ifdown 191da177e4SLinus Torvalds * <chexum@bankinf.banki.hu> 201da177e4SLinus Torvalds * Andi Kleen : kill double kfree on module 211da177e4SLinus Torvalds * unload. 221da177e4SLinus Torvalds * Maciej W. Rozycki : FDDI support 231da177e4SLinus Torvalds * sekiya@USAGI : Don't send too many RS 241da177e4SLinus Torvalds * packets. 251da177e4SLinus Torvalds * yoshfuji@USAGI : Fixed interval between DAD 261da177e4SLinus Torvalds * packets. 271da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI : improved accuracy of 281da177e4SLinus Torvalds * address validation timer. 291da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041) 301da177e4SLinus Torvalds * support. 311da177e4SLinus Torvalds * Yuji SEKIYA @USAGI : Don't assign a same IPv6 321da177e4SLinus Torvalds * address on a same interface. 331da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI : ARCnet support 341da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to 351da177e4SLinus Torvalds * seq_file. 36b1cacb68SYOSHIFUJI Hideaki * YOSHIFUJI Hideaki @USAGI : improved source address 37b1cacb68SYOSHIFUJI Hideaki * selection; consider scope, 38b1cacb68SYOSHIFUJI Hideaki * status etc. 391da177e4SLinus Torvalds */ 401da177e4SLinus Torvalds 41f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt 42f3213831SJoe Perches 431da177e4SLinus Torvalds #include <linux/errno.h> 441da177e4SLinus Torvalds #include <linux/types.h> 45a0bffffcSIlpo Järvinen #include <linux/kernel.h> 461da177e4SLinus Torvalds #include <linux/socket.h> 471da177e4SLinus Torvalds #include <linux/sockios.h> 481da177e4SLinus Torvalds #include <linux/net.h> 491da177e4SLinus Torvalds #include <linux/in6.h> 501da177e4SLinus Torvalds #include <linux/netdevice.h> 511823730fSThomas Graf #include <linux/if_addr.h> 521da177e4SLinus Torvalds #include <linux/if_arp.h> 531da177e4SLinus Torvalds #include <linux/if_arcnet.h> 541da177e4SLinus Torvalds #include <linux/if_infiniband.h> 551da177e4SLinus Torvalds #include <linux/route.h> 561da177e4SLinus Torvalds #include <linux/inetdevice.h> 571da177e4SLinus Torvalds #include <linux/init.h> 585a0e3ad6STejun Heo #include <linux/slab.h> 591da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 601da177e4SLinus Torvalds #include <linux/sysctl.h> 611da177e4SLinus Torvalds #endif 624fc268d2SRandy Dunlap #include <linux/capability.h> 631da177e4SLinus Torvalds #include <linux/delay.h> 641da177e4SLinus Torvalds #include <linux/notifier.h> 65543537bdSPaulo Marques #include <linux/string.h> 66ddbe5032SEric Dumazet #include <linux/hash.h> 671da177e4SLinus Torvalds 68457c4cbcSEric W. Biederman #include <net/net_namespace.h> 691da177e4SLinus Torvalds #include <net/sock.h> 701da177e4SLinus Torvalds #include <net/snmp.h> 711da177e4SLinus Torvalds 7206a4c1c5Salex.bluesman.smirnov@gmail.com #include <net/af_ieee802154.h> 731da177e4SLinus Torvalds #include <net/ipv6.h> 741da177e4SLinus Torvalds #include <net/protocol.h> 751da177e4SLinus Torvalds #include <net/ndisc.h> 761da177e4SLinus Torvalds #include <net/ip6_route.h> 771da177e4SLinus Torvalds #include <net/addrconf.h> 781da177e4SLinus Torvalds #include <net/tcp.h> 791da177e4SLinus Torvalds #include <net/ip.h> 805d620266SThomas Graf #include <net/netlink.h> 81f24e3d65SMitsuru Chinen #include <net/pkt_sched.h> 821da177e4SLinus Torvalds #include <linux/if_tunnel.h> 831da177e4SLinus Torvalds #include <linux/rtnetlink.h> 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 861da177e4SLinus Torvalds #include <linux/random.h> 871da177e4SLinus Torvalds #endif 881da177e4SLinus Torvalds 89e21e8467SStephen Hemminger #include <linux/uaccess.h> 907f7d9a6bSHerbert Xu #include <asm/unaligned.h> 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds #include <linux/proc_fs.h> 931da177e4SLinus Torvalds #include <linux/seq_file.h> 94bc3b2d7fSPaul Gortmaker #include <linux/export.h> 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds /* Set to 3 to get tracing... */ 971da177e4SLinus Torvalds #define ACONF_DEBUG 2 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds #if ACONF_DEBUG >= 3 1001da177e4SLinus Torvalds #define ADBG(x) printk x 1011da177e4SLinus Torvalds #else 1021da177e4SLinus Torvalds #define ADBG(x) 1031da177e4SLinus Torvalds #endif 1041da177e4SLinus Torvalds 1051da177e4SLinus Torvalds #define INFINITY_LIFE_TIME 0xFFFFFFFF 10618a31e1eSThomas Graf 10718a31e1eSThomas Graf static inline u32 cstamp_delta(unsigned long cstamp) 10818a31e1eSThomas Graf { 10918a31e1eSThomas Graf return (cstamp - INITIAL_JIFFIES) * 100UL / HZ; 11018a31e1eSThomas Graf } 1111da177e4SLinus Torvalds 112b2db7564SYOSHIFUJI Hideaki #define ADDRCONF_TIMER_FUZZ_MINUS (HZ > 50 ? HZ/50 : 1) 113b2db7564SYOSHIFUJI Hideaki #define ADDRCONF_TIMER_FUZZ (HZ / 4) 114b2db7564SYOSHIFUJI Hideaki #define ADDRCONF_TIMER_FUZZ_MAX (HZ) 115b2db7564SYOSHIFUJI Hideaki 1161da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 117f52295a9SPavel Emelyanov static void addrconf_sysctl_register(struct inet6_dev *idev); 118408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev); 119408c4768SPavel Emelyanov #else 120408c4768SPavel Emelyanov static inline void addrconf_sysctl_register(struct inet6_dev *idev) 121408c4768SPavel Emelyanov { 122408c4768SPavel Emelyanov } 123408c4768SPavel Emelyanov 124408c4768SPavel Emelyanov static inline void addrconf_sysctl_unregister(struct inet6_dev *idev) 125408c4768SPavel Emelyanov { 126408c4768SPavel Emelyanov } 1271da177e4SLinus Torvalds #endif 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 130eb7e0575SSorin Dumitru static void __ipv6_regen_rndid(struct inet6_dev *idev); 131eb7e0575SSorin Dumitru static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr); 1321da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data); 1331da177e4SLinus Torvalds #endif 1341da177e4SLinus Torvalds 1351b34be74SYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev); 1361da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev); 1371da177e4SLinus Torvalds 1381da177e4SLinus Torvalds /* 1391da177e4SLinus Torvalds * Configured unicast address hash table 1401da177e4SLinus Torvalds */ 141c2e21293Sstephen hemminger static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE]; 1425c578aedSstephen hemminger static DEFINE_SPINLOCK(addrconf_hash_lock); 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds static void addrconf_verify(unsigned long); 1451da177e4SLinus Torvalds 1468d06afabSIngo Molnar static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0); 1471da177e4SLinus Torvalds static DEFINE_SPINLOCK(addrconf_verify_lock); 1481da177e4SLinus Torvalds 1491da177e4SLinus Torvalds static void addrconf_join_anycast(struct inet6_ifaddr *ifp); 1501da177e4SLinus Torvalds static void addrconf_leave_anycast(struct inet6_ifaddr *ifp); 1511da177e4SLinus Torvalds 15293d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, 15375c78500SMoni Shoua unsigned long event); 1541da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how); 1551da177e4SLinus Torvalds 156cf22f9a2SDavid S. Miller static void addrconf_dad_start(struct inet6_ifaddr *ifp); 1571da177e4SLinus Torvalds static void addrconf_dad_timer(unsigned long data); 1581da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp); 159c5e33bddSYOSHIFUJI Hideaki static void addrconf_dad_run(struct inet6_dev *idev); 1601da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data); 1611da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); 1621da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); 1631da177e4SLinus Torvalds 1641da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev, 1651da177e4SLinus Torvalds struct prefix_info *pinfo); 1663e81c6daSDavid S. Miller static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr, 16706bfe655SDaniel Lezcano struct net_device *dev); 1681da177e4SLinus Torvalds 169e041c683SAlan Stern static ATOMIC_NOTIFIER_HEAD(inet6addr_chain); 1701da177e4SLinus Torvalds 171888c848eSAdrian Bunk static struct ipv6_devconf ipv6_devconf __read_mostly = { 1721da177e4SLinus Torvalds .forwarding = 0, 1731da177e4SLinus Torvalds .hop_limit = IPV6_DEFAULT_HOPLIMIT, 1741da177e4SLinus Torvalds .mtu6 = IPV6_MIN_MTU, 1751da177e4SLinus Torvalds .accept_ra = 1, 1761da177e4SLinus Torvalds .accept_redirects = 1, 1771da177e4SLinus Torvalds .autoconf = 1, 1781da177e4SLinus Torvalds .force_mld_version = 0, 1791da177e4SLinus Torvalds .dad_transmits = 1, 1801da177e4SLinus Torvalds .rtr_solicits = MAX_RTR_SOLICITATIONS, 1811da177e4SLinus Torvalds .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, 1821da177e4SLinus Torvalds .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, 1831da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 1841da177e4SLinus Torvalds .use_tempaddr = 0, 1851da177e4SLinus Torvalds .temp_valid_lft = TEMP_VALID_LIFETIME, 1861da177e4SLinus Torvalds .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, 1871da177e4SLinus Torvalds .regen_max_retry = REGEN_MAX_RETRY, 1881da177e4SLinus Torvalds .max_desync_factor = MAX_DESYNC_FACTOR, 1891da177e4SLinus Torvalds #endif 1901da177e4SLinus Torvalds .max_addresses = IPV6_MAX_ADDRESSES, 19165f5c7c1SYOSHIFUJI Hideaki .accept_ra_defrtr = 1, 192c4fd30ebSYOSHIFUJI Hideaki .accept_ra_pinfo = 1, 193930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 194930d6ff2SYOSHIFUJI Hideaki .accept_ra_rtr_pref = 1, 19552e16356SYOSHIFUJI Hideaki .rtr_probe_interval = 60 * HZ, 19609c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 19709c884d4SYOSHIFUJI Hideaki .accept_ra_rt_info_max_plen = 0, 19809c884d4SYOSHIFUJI Hideaki #endif 199930d6ff2SYOSHIFUJI Hideaki #endif 200fbea49e1SYOSHIFUJI Hideaki .proxy_ndp = 0, 2010bcbc926SYOSHIFUJI Hideaki .accept_source_route = 0, /* we do not accept RH0 by default. */ 202778d80beSYOSHIFUJI Hideaki .disable_ipv6 = 0, 2031b34be74SYOSHIFUJI Hideaki .accept_dad = 1, 2041da177e4SLinus Torvalds }; 2051da177e4SLinus Torvalds 206ab32ea5dSBrian Haley static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = { 2071da177e4SLinus Torvalds .forwarding = 0, 2081da177e4SLinus Torvalds .hop_limit = IPV6_DEFAULT_HOPLIMIT, 2091da177e4SLinus Torvalds .mtu6 = IPV6_MIN_MTU, 2101da177e4SLinus Torvalds .accept_ra = 1, 2111da177e4SLinus Torvalds .accept_redirects = 1, 2121da177e4SLinus Torvalds .autoconf = 1, 2131da177e4SLinus Torvalds .dad_transmits = 1, 2141da177e4SLinus Torvalds .rtr_solicits = MAX_RTR_SOLICITATIONS, 2151da177e4SLinus Torvalds .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, 2161da177e4SLinus Torvalds .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, 2171da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 2181da177e4SLinus Torvalds .use_tempaddr = 0, 2191da177e4SLinus Torvalds .temp_valid_lft = TEMP_VALID_LIFETIME, 2201da177e4SLinus Torvalds .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, 2211da177e4SLinus Torvalds .regen_max_retry = REGEN_MAX_RETRY, 2221da177e4SLinus Torvalds .max_desync_factor = MAX_DESYNC_FACTOR, 2231da177e4SLinus Torvalds #endif 2241da177e4SLinus Torvalds .max_addresses = IPV6_MAX_ADDRESSES, 22565f5c7c1SYOSHIFUJI Hideaki .accept_ra_defrtr = 1, 226c4fd30ebSYOSHIFUJI Hideaki .accept_ra_pinfo = 1, 227930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 228930d6ff2SYOSHIFUJI Hideaki .accept_ra_rtr_pref = 1, 22952e16356SYOSHIFUJI Hideaki .rtr_probe_interval = 60 * HZ, 23009c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 23109c884d4SYOSHIFUJI Hideaki .accept_ra_rt_info_max_plen = 0, 23209c884d4SYOSHIFUJI Hideaki #endif 233930d6ff2SYOSHIFUJI Hideaki #endif 234fbea49e1SYOSHIFUJI Hideaki .proxy_ndp = 0, 2350bcbc926SYOSHIFUJI Hideaki .accept_source_route = 0, /* we do not accept RH0 by default. */ 236778d80beSYOSHIFUJI Hideaki .disable_ipv6 = 0, 2371b34be74SYOSHIFUJI Hideaki .accept_dad = 1, 2381da177e4SLinus Torvalds }; 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */ 2411da177e4SLinus Torvalds const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; 2421da177e4SLinus Torvalds const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT; 243f3ee4010SYOSHIFUJI Hideaki const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT; 244f3ee4010SYOSHIFUJI Hideaki const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT; 2451da177e4SLinus Torvalds 246f24e3d65SMitsuru Chinen /* Check if a valid qdisc is available */ 24705297949SDavid S. Miller static inline bool addrconf_qdisc_ok(const struct net_device *dev) 248f24e3d65SMitsuru Chinen { 24905297949SDavid S. Miller return !qdisc_tx_is_noop(dev); 250f24e3d65SMitsuru Chinen } 251f24e3d65SMitsuru Chinen 2522b5ead46SYOSHIFUJI Hideaki /* Check if a route is valid prefix route */ 2532b5ead46SYOSHIFUJI Hideaki static inline int addrconf_is_prefix_route(const struct rt6_info *rt) 2542b5ead46SYOSHIFUJI Hideaki { 255a02cec21SEric Dumazet return (rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0; 2562b5ead46SYOSHIFUJI Hideaki } 2572b5ead46SYOSHIFUJI Hideaki 2581da177e4SLinus Torvalds static void addrconf_del_timer(struct inet6_ifaddr *ifp) 2591da177e4SLinus Torvalds { 2601da177e4SLinus Torvalds if (del_timer(&ifp->timer)) 2611da177e4SLinus Torvalds __in6_ifa_put(ifp); 2621da177e4SLinus Torvalds } 2631da177e4SLinus Torvalds 264e21e8467SStephen Hemminger enum addrconf_timer_t { 2651da177e4SLinus Torvalds AC_NONE, 2661da177e4SLinus Torvalds AC_DAD, 2671da177e4SLinus Torvalds AC_RS, 2681da177e4SLinus Torvalds }; 2691da177e4SLinus Torvalds 2701da177e4SLinus Torvalds static void addrconf_mod_timer(struct inet6_ifaddr *ifp, 2711da177e4SLinus Torvalds enum addrconf_timer_t what, 2721da177e4SLinus Torvalds unsigned long when) 2731da177e4SLinus Torvalds { 2741da177e4SLinus Torvalds if (!del_timer(&ifp->timer)) 2751da177e4SLinus Torvalds in6_ifa_hold(ifp); 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds switch (what) { 2781da177e4SLinus Torvalds case AC_DAD: 2791da177e4SLinus Torvalds ifp->timer.function = addrconf_dad_timer; 2801da177e4SLinus Torvalds break; 2811da177e4SLinus Torvalds case AC_RS: 2821da177e4SLinus Torvalds ifp->timer.function = addrconf_rs_timer; 2831da177e4SLinus Torvalds break; 284e21e8467SStephen Hemminger default: 285e21e8467SStephen Hemminger break; 2861da177e4SLinus Torvalds } 2871da177e4SLinus Torvalds ifp->timer.expires = jiffies + when; 2881da177e4SLinus Torvalds add_timer(&ifp->timer); 2891da177e4SLinus Torvalds } 2901da177e4SLinus Torvalds 2917f7d9a6bSHerbert Xu static int snmp6_alloc_dev(struct inet6_dev *idev) 2927f7d9a6bSHerbert Xu { 2937d720c3eSTejun Heo if (snmp_mib_init((void __percpu **)idev->stats.ipv6, 2941823e4c8SEric Dumazet sizeof(struct ipstats_mib), 2951823e4c8SEric Dumazet __alignof__(struct ipstats_mib)) < 0) 2967f7d9a6bSHerbert Xu goto err_ip; 297be281e55SEric Dumazet idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device), 298be281e55SEric Dumazet GFP_KERNEL); 299be281e55SEric Dumazet if (!idev->stats.icmpv6dev) 3007f7d9a6bSHerbert Xu goto err_icmp; 301be281e55SEric Dumazet idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device), 302be281e55SEric Dumazet GFP_KERNEL); 303be281e55SEric Dumazet if (!idev->stats.icmpv6msgdev) 30414878f75SDavid L Stevens goto err_icmpmsg; 3057f7d9a6bSHerbert Xu 3067f7d9a6bSHerbert Xu return 0; 3077f7d9a6bSHerbert Xu 30814878f75SDavid L Stevens err_icmpmsg: 309be281e55SEric Dumazet kfree(idev->stats.icmpv6dev); 3107f7d9a6bSHerbert Xu err_icmp: 3117d720c3eSTejun Heo snmp_mib_free((void __percpu **)idev->stats.ipv6); 3127f7d9a6bSHerbert Xu err_ip: 313aaf70ec7SPavel Emelyanov return -ENOMEM; 3147f7d9a6bSHerbert Xu } 3157f7d9a6bSHerbert Xu 31616910b98SPavel Emelyanov static void snmp6_free_dev(struct inet6_dev *idev) 3177f7d9a6bSHerbert Xu { 318be281e55SEric Dumazet kfree(idev->stats.icmpv6msgdev); 319be281e55SEric Dumazet kfree(idev->stats.icmpv6dev); 3207d720c3eSTejun Heo snmp_mib_free((void __percpu **)idev->stats.ipv6); 3217f7d9a6bSHerbert Xu } 3227f7d9a6bSHerbert Xu 3231da177e4SLinus Torvalds /* Nobody refers to this device, we may destroy it. */ 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds void in6_dev_finish_destroy(struct inet6_dev *idev) 3261da177e4SLinus Torvalds { 3271da177e4SLinus Torvalds struct net_device *dev = idev->dev; 328547b792cSIlpo Järvinen 329502a2ffdSstephen hemminger WARN_ON(!list_empty(&idev->addr_list)); 330547b792cSIlpo Järvinen WARN_ON(idev->mc_list != NULL); 331547b792cSIlpo Järvinen 3321da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG 33391df42beSJoe Perches pr_debug("%s: %s\n", __func__, dev ? dev->name : "NIL"); 3341da177e4SLinus Torvalds #endif 3351da177e4SLinus Torvalds dev_put(dev); 3361da177e4SLinus Torvalds if (!idev->dead) { 337f3213831SJoe Perches pr_warn("Freeing alive inet6 device %p\n", idev); 3381da177e4SLinus Torvalds return; 3391da177e4SLinus Torvalds } 3401da177e4SLinus Torvalds snmp6_free_dev(idev); 34138f57d1aSLai Jiangshan kfree_rcu(idev, rcu); 3421da177e4SLinus Torvalds } 3437159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(in6_dev_finish_destroy); 3447159039aSYOSHIFUJI Hideaki 3451da177e4SLinus Torvalds static struct inet6_dev *ipv6_add_dev(struct net_device *dev) 3461da177e4SLinus Torvalds { 3471da177e4SLinus Torvalds struct inet6_dev *ndev; 3481da177e4SLinus Torvalds 3491da177e4SLinus Torvalds ASSERT_RTNL(); 3501da177e4SLinus Torvalds 3511da177e4SLinus Torvalds if (dev->mtu < IPV6_MIN_MTU) 3521da177e4SLinus Torvalds return NULL; 3531da177e4SLinus Torvalds 354322f74a4SIngo Oeser ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL); 3551da177e4SLinus Torvalds 356322f74a4SIngo Oeser if (ndev == NULL) 357322f74a4SIngo Oeser return NULL; 3581da177e4SLinus Torvalds 3591da177e4SLinus Torvalds rwlock_init(&ndev->lock); 3601da177e4SLinus Torvalds ndev->dev = dev; 361502a2ffdSstephen hemminger INIT_LIST_HEAD(&ndev->addr_list); 362502a2ffdSstephen hemminger 363c346dca1SYOSHIFUJI Hideaki memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf)); 3641da177e4SLinus Torvalds ndev->cnf.mtu6 = dev->mtu; 3651da177e4SLinus Torvalds ndev->cnf.sysctl = NULL; 3661da177e4SLinus Torvalds ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl); 3671da177e4SLinus Torvalds if (ndev->nd_parms == NULL) { 3681da177e4SLinus Torvalds kfree(ndev); 3691da177e4SLinus Torvalds return NULL; 3701da177e4SLinus Torvalds } 3710187bdfbSBen Hutchings if (ndev->cnf.forwarding) 3720187bdfbSBen Hutchings dev_disable_lro(dev); 3731da177e4SLinus Torvalds /* We refer to the device */ 3741da177e4SLinus Torvalds dev_hold(dev); 3751da177e4SLinus Torvalds 3761da177e4SLinus Torvalds if (snmp6_alloc_dev(ndev) < 0) { 3771da177e4SLinus Torvalds ADBG((KERN_WARNING 378f3213831SJoe Perches "%s: cannot allocate memory for statistics; dev=%s.\n", 3790dc47877SHarvey Harrison __func__, dev->name)); 3801da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, ndev->nd_parms); 3818603e33dSRoy Li dev_put(dev); 3828603e33dSRoy Li kfree(ndev); 3831da177e4SLinus Torvalds return NULL; 3841da177e4SLinus Torvalds } 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds if (snmp6_register_dev(ndev) < 0) { 3871da177e4SLinus Torvalds ADBG((KERN_WARNING 388f3213831SJoe Perches "%s: cannot create /proc/net/dev_snmp6/%s\n", 3890dc47877SHarvey Harrison __func__, dev->name)); 3901da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, ndev->nd_parms); 3911da177e4SLinus Torvalds ndev->dead = 1; 3921da177e4SLinus Torvalds in6_dev_finish_destroy(ndev); 3931da177e4SLinus Torvalds return NULL; 3941da177e4SLinus Torvalds } 3951da177e4SLinus Torvalds 3961da177e4SLinus Torvalds /* One reference from device. We must do this before 3971da177e4SLinus Torvalds * we invoke __ipv6_regen_rndid(). 3981da177e4SLinus Torvalds */ 3991da177e4SLinus Torvalds in6_dev_hold(ndev); 4001da177e4SLinus Torvalds 4011b34be74SYOSHIFUJI Hideaki if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) 4021b34be74SYOSHIFUJI Hideaki ndev->cnf.accept_dad = -1; 4031b34be74SYOSHIFUJI Hideaki 4040be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 405c7dc89c0SFred L. Templin if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) { 406f3213831SJoe Perches pr_info("%s: Disabled Multicast RS\n", dev->name); 407c7dc89c0SFred L. Templin ndev->cnf.rtr_solicits = 0; 408c7dc89c0SFred L. Templin } 409b077d7abSYOSHIFUJI Hideaki #endif 410b077d7abSYOSHIFUJI Hideaki 4111da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 412372e6c8fSstephen hemminger INIT_LIST_HEAD(&ndev->tempaddr_list); 4131da177e4SLinus Torvalds setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev); 4141da177e4SLinus Torvalds if ((dev->flags&IFF_LOOPBACK) || 4151da177e4SLinus Torvalds dev->type == ARPHRD_TUNNEL || 4169625ed72SYOSHIFUJI Hideaki dev->type == ARPHRD_TUNNEL6 || 4171da177e4SLinus Torvalds dev->type == ARPHRD_SIT || 4181da177e4SLinus Torvalds dev->type == ARPHRD_NONE) { 4190be669bbSJoerg Roedel ndev->cnf.use_tempaddr = -1; 4201da177e4SLinus Torvalds } else { 4211da177e4SLinus Torvalds in6_dev_hold(ndev); 4221da177e4SLinus Torvalds ipv6_regen_rndid((unsigned long) ndev); 4231da177e4SLinus Torvalds } 4241da177e4SLinus Torvalds #endif 4251da177e4SLinus Torvalds 426f24e3d65SMitsuru Chinen if (netif_running(dev) && addrconf_qdisc_ok(dev)) 42753aadcc9SHerbert Xu ndev->if_flags |= IF_READY; 42853aadcc9SHerbert Xu 4291da177e4SLinus Torvalds ipv6_mc_init_dev(ndev); 4301da177e4SLinus Torvalds ndev->tstamp = jiffies; 431f52295a9SPavel Emelyanov addrconf_sysctl_register(ndev); 43230c4cf57SDavid L Stevens /* protected by rtnl_lock */ 433cf778b00SEric Dumazet rcu_assign_pointer(dev->ip6_ptr, ndev); 434d88ae4ccSYOSHIFUJI Hideaki 435d88ae4ccSYOSHIFUJI Hideaki /* Join all-node multicast group */ 436f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes); 437d88ae4ccSYOSHIFUJI Hideaki 438d6ddef9eSLi Wei /* Join all-router multicast group if forwarding is set */ 4398b2aaedeSLi Wei if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST)) 440d6ddef9eSLi Wei ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters); 441d6ddef9eSLi Wei 4421da177e4SLinus Torvalds return ndev; 4431da177e4SLinus Torvalds } 4441da177e4SLinus Torvalds 445cee89473SYOSHIFUJI Hideaki static struct inet6_dev *ipv6_find_idev(struct net_device *dev) 4461da177e4SLinus Torvalds { 4471da177e4SLinus Torvalds struct inet6_dev *idev; 4481da177e4SLinus Torvalds 4491da177e4SLinus Torvalds ASSERT_RTNL(); 4501da177e4SLinus Torvalds 451e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 452e21e8467SStephen Hemminger if (!idev) { 453e21e8467SStephen Hemminger idev = ipv6_add_dev(dev); 454e21e8467SStephen Hemminger if (!idev) 4551da177e4SLinus Torvalds return NULL; 4561da177e4SLinus Torvalds } 457c5e33bddSYOSHIFUJI Hideaki 4581da177e4SLinus Torvalds if (dev->flags&IFF_UP) 4591da177e4SLinus Torvalds ipv6_mc_up(idev); 4601da177e4SLinus Torvalds return idev; 4611da177e4SLinus Torvalds } 4621da177e4SLinus Torvalds 4631da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 4641da177e4SLinus Torvalds static void dev_forward_change(struct inet6_dev *idev) 4651da177e4SLinus Torvalds { 4661da177e4SLinus Torvalds struct net_device *dev; 4671da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 4681da177e4SLinus Torvalds 4691da177e4SLinus Torvalds if (!idev) 4701da177e4SLinus Torvalds return; 4711da177e4SLinus Torvalds dev = idev->dev; 4720187bdfbSBen Hutchings if (idev->cnf.forwarding) 4730187bdfbSBen Hutchings dev_disable_lro(dev); 4741da177e4SLinus Torvalds if (dev && (dev->flags & IFF_MULTICAST)) { 4751da177e4SLinus Torvalds if (idev->cnf.forwarding) 476f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters); 4771da177e4SLinus Torvalds else 478f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters); 4791da177e4SLinus Torvalds } 480502a2ffdSstephen hemminger 481502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 4822c12a74cSMichal Wrobel if (ifa->flags&IFA_F_TENTATIVE) 4832c12a74cSMichal Wrobel continue; 4841da177e4SLinus Torvalds if (idev->cnf.forwarding) 4851da177e4SLinus Torvalds addrconf_join_anycast(ifa); 4861da177e4SLinus Torvalds else 4871da177e4SLinus Torvalds addrconf_leave_anycast(ifa); 4881da177e4SLinus Torvalds } 4891da177e4SLinus Torvalds } 4901da177e4SLinus Torvalds 4911da177e4SLinus Torvalds 492e186932bSPavel Emelyanov static void addrconf_forward_change(struct net *net, __s32 newf) 4931da177e4SLinus Torvalds { 4941da177e4SLinus Torvalds struct net_device *dev; 4951da177e4SLinus Torvalds struct inet6_dev *idev; 4961da177e4SLinus Torvalds 4974acd4945SBen Hutchings for_each_netdev(net, dev) { 4981da177e4SLinus Torvalds idev = __in6_dev_get(dev); 4991da177e4SLinus Torvalds if (idev) { 500e186932bSPavel Emelyanov int changed = (!idev->cnf.forwarding) ^ (!newf); 501e186932bSPavel Emelyanov idev->cnf.forwarding = newf; 5021da177e4SLinus Torvalds if (changed) 5031da177e4SLinus Torvalds dev_forward_change(idev); 5041da177e4SLinus Torvalds } 5051da177e4SLinus Torvalds } 5061da177e4SLinus Torvalds } 507c8fecf22SPavel Emelyanov 508013d97e9SFrancesco Ruggeri static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf) 509c8fecf22SPavel Emelyanov { 510bff16c2fSPavel Emelyanov struct net *net; 511013d97e9SFrancesco Ruggeri int old; 512013d97e9SFrancesco Ruggeri 513013d97e9SFrancesco Ruggeri if (!rtnl_trylock()) 514013d97e9SFrancesco Ruggeri return restart_syscall(); 515bff16c2fSPavel Emelyanov 516bff16c2fSPavel Emelyanov net = (struct net *)table->extra2; 517013d97e9SFrancesco Ruggeri old = *p; 518013d97e9SFrancesco Ruggeri *p = newf; 519c8fecf22SPavel Emelyanov 520013d97e9SFrancesco Ruggeri if (p == &net->ipv6.devconf_dflt->forwarding) { 521013d97e9SFrancesco Ruggeri rtnl_unlock(); 522013d97e9SFrancesco Ruggeri return 0; 52388af182eSEric W. Biederman } 524b325fddbSStephen Hemminger 525e186932bSPavel Emelyanov if (p == &net->ipv6.devconf_all->forwarding) { 526e186932bSPavel Emelyanov net->ipv6.devconf_dflt->forwarding = newf; 527e186932bSPavel Emelyanov addrconf_forward_change(net, newf); 528013d97e9SFrancesco Ruggeri } else if ((!newf) ^ (!old)) 529c8fecf22SPavel Emelyanov dev_forward_change((struct inet6_dev *)table->extra1); 5300187bdfbSBen Hutchings rtnl_unlock(); 531c8fecf22SPavel Emelyanov 532013d97e9SFrancesco Ruggeri if (newf) 5337b4da532SDaniel Lezcano rt6_purge_dflt_routers(net); 534b325fddbSStephen Hemminger return 1; 535c8fecf22SPavel Emelyanov } 5361da177e4SLinus Torvalds #endif 5371da177e4SLinus Torvalds 5385c578aedSstephen hemminger /* Nobody refers to this ifaddr, destroy it */ 5391da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp) 5401da177e4SLinus Torvalds { 541c2e21293Sstephen hemminger WARN_ON(!hlist_unhashed(&ifp->addr_lst)); 542547b792cSIlpo Järvinen 5431da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG 54491df42beSJoe Perches pr_debug("%s\n", __func__); 5451da177e4SLinus Torvalds #endif 5461da177e4SLinus Torvalds 5471da177e4SLinus Torvalds in6_dev_put(ifp->idev); 5481da177e4SLinus Torvalds 5491da177e4SLinus Torvalds if (del_timer(&ifp->timer)) 550e21e8467SStephen Hemminger pr_notice("Timer is still running, when freeing ifa=%p\n", ifp); 5511da177e4SLinus Torvalds 552e9d3e084SHerbert Xu if (ifp->state != INET6_IFADDR_STATE_DEAD) { 553f3213831SJoe Perches pr_warn("Freeing alive inet6 address %p\n", ifp); 5541da177e4SLinus Torvalds return; 5551da177e4SLinus Torvalds } 556d8d1f30bSChangli Gao dst_release(&ifp->rt->dst); 5571da177e4SLinus Torvalds 558e5785985SLai Jiangshan kfree_rcu(ifp, rcu); 5591da177e4SLinus Torvalds } 5601da177e4SLinus Torvalds 561e55ffac6SBrian Haley static void 562e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp) 563e55ffac6SBrian Haley { 564502a2ffdSstephen hemminger struct list_head *p; 5658a6ce0c0SYOSHIFUJI Hideaki int ifp_scope = ipv6_addr_src_scope(&ifp->addr); 566e55ffac6SBrian Haley 567e55ffac6SBrian Haley /* 568e55ffac6SBrian Haley * Each device address list is sorted in order of scope - 569e55ffac6SBrian Haley * global before linklocal. 570e55ffac6SBrian Haley */ 571502a2ffdSstephen hemminger list_for_each(p, &idev->addr_list) { 572502a2ffdSstephen hemminger struct inet6_ifaddr *ifa 573502a2ffdSstephen hemminger = list_entry(p, struct inet6_ifaddr, if_list); 5748a6ce0c0SYOSHIFUJI Hideaki if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr)) 575e55ffac6SBrian Haley break; 576e55ffac6SBrian Haley } 577e55ffac6SBrian Haley 578b54c9b98SDavid S. Miller list_add_tail(&ifp->if_list, p); 579e55ffac6SBrian Haley } 580e55ffac6SBrian Haley 581ddbe5032SEric Dumazet static u32 inet6_addr_hash(const struct in6_addr *addr) 5823eb84f49SYOSHIFUJI Hideaki { 583ddbe5032SEric Dumazet return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT); 5843eb84f49SYOSHIFUJI Hideaki } 5853eb84f49SYOSHIFUJI Hideaki 5861da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */ 5871da177e4SLinus Torvalds 5881da177e4SLinus Torvalds static struct inet6_ifaddr * 5891da177e4SLinus Torvalds ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen, 590e431b8c0SJamal Hadi Salim int scope, u32 flags) 5911da177e4SLinus Torvalds { 5921da177e4SLinus Torvalds struct inet6_ifaddr *ifa = NULL; 5931da177e4SLinus Torvalds struct rt6_info *rt; 5943a88a81dSstephen hemminger unsigned int hash; 5951da177e4SLinus Torvalds int err = 0; 596d68b8270SYOSHIFUJI Hideaki int addr_type = ipv6_addr_type(addr); 597d68b8270SYOSHIFUJI Hideaki 598d68b8270SYOSHIFUJI Hideaki if (addr_type == IPV6_ADDR_ANY || 599d68b8270SYOSHIFUJI Hideaki addr_type & IPV6_ADDR_MULTICAST || 600d68b8270SYOSHIFUJI Hideaki (!(idev->dev->flags & IFF_LOOPBACK) && 601d68b8270SYOSHIFUJI Hideaki addr_type & IPV6_ADDR_LOOPBACK)) 602d68b8270SYOSHIFUJI Hideaki return ERR_PTR(-EADDRNOTAVAIL); 6031da177e4SLinus Torvalds 6048814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 6051da177e4SLinus Torvalds if (idev->dead) { 6061da177e4SLinus Torvalds err = -ENODEV; /*XXX*/ 6071da177e4SLinus Torvalds goto out2; 6081da177e4SLinus Torvalds } 6091da177e4SLinus Torvalds 61056d417b1SBrian Haley if (idev->cnf.disable_ipv6) { 6119bdd8d40SBrian Haley err = -EACCES; 6129bdd8d40SBrian Haley goto out2; 6139bdd8d40SBrian Haley } 6149bdd8d40SBrian Haley 6155c578aedSstephen hemminger spin_lock(&addrconf_hash_lock); 6161da177e4SLinus Torvalds 6171da177e4SLinus Torvalds /* Ignore adding duplicate addresses on an interface */ 618c346dca1SYOSHIFUJI Hideaki if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) { 6191da177e4SLinus Torvalds ADBG(("ipv6_add_addr: already assigned\n")); 6201da177e4SLinus Torvalds err = -EEXIST; 6211da177e4SLinus Torvalds goto out; 6221da177e4SLinus Torvalds } 6231da177e4SLinus Torvalds 624322f74a4SIngo Oeser ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC); 6251da177e4SLinus Torvalds 6261da177e4SLinus Torvalds if (ifa == NULL) { 6271da177e4SLinus Torvalds ADBG(("ipv6_add_addr: malloc failed\n")); 6281da177e4SLinus Torvalds err = -ENOBUFS; 6291da177e4SLinus Torvalds goto out; 6301da177e4SLinus Torvalds } 6311da177e4SLinus Torvalds 6328f031519SDavid S. Miller rt = addrconf_dst_alloc(idev, addr, false); 6331da177e4SLinus Torvalds if (IS_ERR(rt)) { 6341da177e4SLinus Torvalds err = PTR_ERR(rt); 6351da177e4SLinus Torvalds goto out; 6361da177e4SLinus Torvalds } 6371da177e4SLinus Torvalds 6384e3fd7a0SAlexey Dobriyan ifa->addr = *addr; 6391da177e4SLinus Torvalds 6401da177e4SLinus Torvalds spin_lock_init(&ifa->lock); 641e9d3e084SHerbert Xu spin_lock_init(&ifa->state_lock); 6421da177e4SLinus Torvalds init_timer(&ifa->timer); 643c2e21293Sstephen hemminger INIT_HLIST_NODE(&ifa->addr_lst); 6441da177e4SLinus Torvalds ifa->timer.data = (unsigned long) ifa; 6451da177e4SLinus Torvalds ifa->scope = scope; 6461da177e4SLinus Torvalds ifa->prefix_len = pfxlen; 6471da177e4SLinus Torvalds ifa->flags = flags | IFA_F_TENTATIVE; 6481da177e4SLinus Torvalds ifa->cstamp = ifa->tstamp = jiffies; 6491da177e4SLinus Torvalds 65057f5f544SKeir Fraser ifa->rt = rt; 65157f5f544SKeir Fraser 6521da177e4SLinus Torvalds ifa->idev = idev; 6531da177e4SLinus Torvalds in6_dev_hold(idev); 6541da177e4SLinus Torvalds /* For caller */ 6551da177e4SLinus Torvalds in6_ifa_hold(ifa); 6561da177e4SLinus Torvalds 6571da177e4SLinus Torvalds /* Add to big hash table */ 658ddbe5032SEric Dumazet hash = inet6_addr_hash(addr); 6591da177e4SLinus Torvalds 6605c578aedSstephen hemminger hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]); 6615c578aedSstephen hemminger spin_unlock(&addrconf_hash_lock); 6621da177e4SLinus Torvalds 6631da177e4SLinus Torvalds write_lock(&idev->lock); 6641da177e4SLinus Torvalds /* Add to inet6_dev unicast addr list. */ 665e55ffac6SBrian Haley ipv6_link_dev_addr(idev, ifa); 6661da177e4SLinus Torvalds 6671da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 6681da177e4SLinus Torvalds if (ifa->flags&IFA_F_TEMPORARY) { 669372e6c8fSstephen hemminger list_add(&ifa->tmp_list, &idev->tempaddr_list); 6701da177e4SLinus Torvalds in6_ifa_hold(ifa); 6711da177e4SLinus Torvalds } 6721da177e4SLinus Torvalds #endif 6731da177e4SLinus Torvalds 6741da177e4SLinus Torvalds in6_ifa_hold(ifa); 6751da177e4SLinus Torvalds write_unlock(&idev->lock); 6761da177e4SLinus Torvalds out2: 6778814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 6781da177e4SLinus Torvalds 679fd92833aSYOSHIFUJI Hideaki if (likely(err == 0)) 680e041c683SAlan Stern atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa); 6811da177e4SLinus Torvalds else { 6821da177e4SLinus Torvalds kfree(ifa); 6831da177e4SLinus Torvalds ifa = ERR_PTR(err); 6841da177e4SLinus Torvalds } 6851da177e4SLinus Torvalds 6861da177e4SLinus Torvalds return ifa; 6871da177e4SLinus Torvalds out: 6885c578aedSstephen hemminger spin_unlock(&addrconf_hash_lock); 6891da177e4SLinus Torvalds goto out2; 6901da177e4SLinus Torvalds } 6911da177e4SLinus Torvalds 6921da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */ 6931da177e4SLinus Torvalds 6941da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp) 6951da177e4SLinus Torvalds { 696502a2ffdSstephen hemminger struct inet6_ifaddr *ifa, *ifn; 6971da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 6984c5ff6a6SHerbert Xu int state; 6991da177e4SLinus Torvalds int deleted = 0, onlink = 0; 7001da177e4SLinus Torvalds unsigned long expires = jiffies; 7011da177e4SLinus Torvalds 7024c5ff6a6SHerbert Xu spin_lock_bh(&ifp->state_lock); 7034c5ff6a6SHerbert Xu state = ifp->state; 704e9d3e084SHerbert Xu ifp->state = INET6_IFADDR_STATE_DEAD; 7054c5ff6a6SHerbert Xu spin_unlock_bh(&ifp->state_lock); 7064c5ff6a6SHerbert Xu 7074c5ff6a6SHerbert Xu if (state == INET6_IFADDR_STATE_DEAD) 7084c5ff6a6SHerbert Xu goto out; 7091da177e4SLinus Torvalds 7105c578aedSstephen hemminger spin_lock_bh(&addrconf_hash_lock); 7115c578aedSstephen hemminger hlist_del_init_rcu(&ifp->addr_lst); 7125c578aedSstephen hemminger spin_unlock_bh(&addrconf_hash_lock); 7131da177e4SLinus Torvalds 7141da177e4SLinus Torvalds write_lock_bh(&idev->lock); 7151da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 7161da177e4SLinus Torvalds if (ifp->flags&IFA_F_TEMPORARY) { 717372e6c8fSstephen hemminger list_del(&ifp->tmp_list); 7181da177e4SLinus Torvalds if (ifp->ifpub) { 7191da177e4SLinus Torvalds in6_ifa_put(ifp->ifpub); 7201da177e4SLinus Torvalds ifp->ifpub = NULL; 7211da177e4SLinus Torvalds } 7221da177e4SLinus Torvalds __in6_ifa_put(ifp); 7231da177e4SLinus Torvalds } 7241da177e4SLinus Torvalds #endif 7251da177e4SLinus Torvalds 726502a2ffdSstephen hemminger list_for_each_entry_safe(ifa, ifn, &idev->addr_list, if_list) { 7271da177e4SLinus Torvalds if (ifa == ifp) { 728502a2ffdSstephen hemminger list_del_init(&ifp->if_list); 7291da177e4SLinus Torvalds __in6_ifa_put(ifp); 730502a2ffdSstephen hemminger 7311da177e4SLinus Torvalds if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0) 7321da177e4SLinus Torvalds break; 7331da177e4SLinus Torvalds deleted = 1; 7341d142804SKristian Slavov continue; 7351da177e4SLinus Torvalds } else if (ifp->flags & IFA_F_PERMANENT) { 7361da177e4SLinus Torvalds if (ipv6_prefix_equal(&ifa->addr, &ifp->addr, 7371da177e4SLinus Torvalds ifp->prefix_len)) { 7381da177e4SLinus Torvalds if (ifa->flags & IFA_F_PERMANENT) { 7391da177e4SLinus Torvalds onlink = 1; 7401da177e4SLinus Torvalds if (deleted) 7411da177e4SLinus Torvalds break; 7421da177e4SLinus Torvalds } else { 7431da177e4SLinus Torvalds unsigned long lifetime; 7441da177e4SLinus Torvalds 7451da177e4SLinus Torvalds if (!onlink) 7461da177e4SLinus Torvalds onlink = -1; 7471da177e4SLinus Torvalds 7481da177e4SLinus Torvalds spin_lock(&ifa->lock); 7494bed72e4SYOSHIFUJI Hideaki 7504bed72e4SYOSHIFUJI Hideaki lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ); 7514bed72e4SYOSHIFUJI Hideaki /* 7524bed72e4SYOSHIFUJI Hideaki * Note: Because this address is 7534bed72e4SYOSHIFUJI Hideaki * not permanent, lifetime < 7544bed72e4SYOSHIFUJI Hideaki * LONG_MAX / HZ here. 7554bed72e4SYOSHIFUJI Hideaki */ 7561da177e4SLinus Torvalds if (time_before(expires, 7571da177e4SLinus Torvalds ifa->tstamp + lifetime * HZ)) 7581da177e4SLinus Torvalds expires = ifa->tstamp + lifetime * HZ; 7591da177e4SLinus Torvalds spin_unlock(&ifa->lock); 7601da177e4SLinus Torvalds } 7611da177e4SLinus Torvalds } 7621da177e4SLinus Torvalds } 7631da177e4SLinus Torvalds } 7641da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 7651da177e4SLinus Torvalds 766b2238566SAndrey Vagin addrconf_del_timer(ifp); 767b2238566SAndrey Vagin 7681da177e4SLinus Torvalds ipv6_ifa_notify(RTM_DELADDR, ifp); 7691da177e4SLinus Torvalds 770e041c683SAlan Stern atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp); 7711da177e4SLinus Torvalds 7721da177e4SLinus Torvalds /* 7731da177e4SLinus Torvalds * Purge or update corresponding prefix 7741da177e4SLinus Torvalds * 7751da177e4SLinus Torvalds * 1) we don't purge prefix here if address was not permanent. 7761da177e4SLinus Torvalds * prefix is managed by its own lifetime. 7771da177e4SLinus Torvalds * 2) if there're no addresses, delete prefix. 7781da177e4SLinus Torvalds * 3) if there're still other permanent address(es), 7791da177e4SLinus Torvalds * corresponding prefix is still permanent. 7801da177e4SLinus Torvalds * 4) otherwise, update prefix lifetime to the 7811da177e4SLinus Torvalds * longest valid lifetime among the corresponding 7821da177e4SLinus Torvalds * addresses on the device. 7831da177e4SLinus Torvalds * Note: subsequent RA will update lifetime. 7841da177e4SLinus Torvalds * 7851da177e4SLinus Torvalds * --yoshfuji 7861da177e4SLinus Torvalds */ 7871da177e4SLinus Torvalds if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) { 7881da177e4SLinus Torvalds struct in6_addr prefix; 7891da177e4SLinus Torvalds struct rt6_info *rt; 790c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(ifp->idev->dev); 79164c6d08eSNicolas Dichtel struct flowi6 fl6 = {}; 7921da177e4SLinus Torvalds 79364c6d08eSNicolas Dichtel ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len); 79464c6d08eSNicolas Dichtel fl6.flowi6_oif = ifp->idev->dev->ifindex; 79564c6d08eSNicolas Dichtel fl6.daddr = prefix; 79664c6d08eSNicolas Dichtel rt = (struct rt6_info *)ip6_route_lookup(net, &fl6, 79764c6d08eSNicolas Dichtel RT6_LOOKUP_F_IFACE); 79864c6d08eSNicolas Dichtel 79964c6d08eSNicolas Dichtel if (rt != net->ipv6.ip6_null_entry && 80064c6d08eSNicolas Dichtel addrconf_is_prefix_route(rt)) { 8011da177e4SLinus Torvalds if (onlink == 0) { 802e0a1ad73SThomas Graf ip6_del_rt(rt); 8031da177e4SLinus Torvalds rt = NULL; 8041da177e4SLinus Torvalds } else if (!(rt->rt6i_flags & RTF_EXPIRES)) { 8051716a961SGao feng rt6_set_expires(rt, expires); 8061da177e4SLinus Torvalds } 8071da177e4SLinus Torvalds } 808d8d1f30bSChangli Gao dst_release(&rt->dst); 8091da177e4SLinus Torvalds } 8101da177e4SLinus Torvalds 811c3968a85SDaniel Walter /* clean up prefsrc entries */ 812c3968a85SDaniel Walter rt6_remove_prefsrc(ifp); 8134c5ff6a6SHerbert Xu out: 8141da177e4SLinus Torvalds in6_ifa_put(ifp); 8151da177e4SLinus Torvalds } 8161da177e4SLinus Torvalds 8171da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 8181da177e4SLinus Torvalds static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift) 8191da177e4SLinus Torvalds { 8201da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 8211da177e4SLinus Torvalds struct in6_addr addr, *tmpaddr; 82276f793e3SLorenzo Colitti unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age; 823eac55bf9SBenoit Boissinot unsigned long regen_advance; 8241da177e4SLinus Torvalds int tmp_plen; 8251da177e4SLinus Torvalds int ret = 0; 8261da177e4SLinus Torvalds int max_addresses; 82795c385b4SNeil Horman u32 addr_flags; 82876f793e3SLorenzo Colitti unsigned long now = jiffies; 8291da177e4SLinus Torvalds 8301da177e4SLinus Torvalds write_lock(&idev->lock); 8311da177e4SLinus Torvalds if (ift) { 8321da177e4SLinus Torvalds spin_lock_bh(&ift->lock); 8331da177e4SLinus Torvalds memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8); 8341da177e4SLinus Torvalds spin_unlock_bh(&ift->lock); 8351da177e4SLinus Torvalds tmpaddr = &addr; 8361da177e4SLinus Torvalds } else { 8371da177e4SLinus Torvalds tmpaddr = NULL; 8381da177e4SLinus Torvalds } 8391da177e4SLinus Torvalds retry: 8401da177e4SLinus Torvalds in6_dev_hold(idev); 8411da177e4SLinus Torvalds if (idev->cnf.use_tempaddr <= 0) { 8421da177e4SLinus Torvalds write_unlock(&idev->lock); 843f3213831SJoe Perches pr_info("%s: use_tempaddr is disabled\n", __func__); 8441da177e4SLinus Torvalds in6_dev_put(idev); 8451da177e4SLinus Torvalds ret = -1; 8461da177e4SLinus Torvalds goto out; 8471da177e4SLinus Torvalds } 8481da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 8491da177e4SLinus Torvalds if (ifp->regen_count++ >= idev->cnf.regen_max_retry) { 8501da177e4SLinus Torvalds idev->cnf.use_tempaddr = -1; /*XXX*/ 8511da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 8521da177e4SLinus Torvalds write_unlock(&idev->lock); 853f3213831SJoe Perches pr_warn("%s: regeneration time exceeded - disabled temporary address support\n", 854f3213831SJoe Perches __func__); 8551da177e4SLinus Torvalds in6_dev_put(idev); 8561da177e4SLinus Torvalds ret = -1; 8571da177e4SLinus Torvalds goto out; 8581da177e4SLinus Torvalds } 8591da177e4SLinus Torvalds in6_ifa_hold(ifp); 8601da177e4SLinus Torvalds memcpy(addr.s6_addr, ifp->addr.s6_addr, 8); 861eb7e0575SSorin Dumitru __ipv6_try_regen_rndid(idev, tmpaddr); 8621da177e4SLinus Torvalds memcpy(&addr.s6_addr[8], idev->rndid, 8); 86376f793e3SLorenzo Colitti age = (now - ifp->tstamp) / HZ; 8641da177e4SLinus Torvalds tmp_valid_lft = min_t(__u32, 8651da177e4SLinus Torvalds ifp->valid_lft, 8667a876b0eSGlenn Wurster idev->cnf.temp_valid_lft + age); 8671da177e4SLinus Torvalds tmp_prefered_lft = min_t(__u32, 8681da177e4SLinus Torvalds ifp->prefered_lft, 8697a876b0eSGlenn Wurster idev->cnf.temp_prefered_lft + age - 870784e2710SBen Hutchings idev->cnf.max_desync_factor); 8711da177e4SLinus Torvalds tmp_plen = ifp->prefix_len; 8721da177e4SLinus Torvalds max_addresses = idev->cnf.max_addresses; 8731da177e4SLinus Torvalds tmp_tstamp = ifp->tstamp; 8741da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 8751da177e4SLinus Torvalds 876eac55bf9SBenoit Boissinot regen_advance = idev->cnf.regen_max_retry * 877eac55bf9SBenoit Boissinot idev->cnf.dad_transmits * 878eac55bf9SBenoit Boissinot idev->nd_parms->retrans_time / HZ; 8791da177e4SLinus Torvalds write_unlock(&idev->lock); 88095c385b4SNeil Horman 881eac55bf9SBenoit Boissinot /* A temporary address is created only if this calculated Preferred 882eac55bf9SBenoit Boissinot * Lifetime is greater than REGEN_ADVANCE time units. In particular, 883eac55bf9SBenoit Boissinot * an implementation must not create a temporary address with a zero 884eac55bf9SBenoit Boissinot * Preferred Lifetime. 885eac55bf9SBenoit Boissinot */ 886eac55bf9SBenoit Boissinot if (tmp_prefered_lft <= regen_advance) { 887eac55bf9SBenoit Boissinot in6_ifa_put(ifp); 888eac55bf9SBenoit Boissinot in6_dev_put(idev); 889eac55bf9SBenoit Boissinot ret = -1; 890eac55bf9SBenoit Boissinot goto out; 891eac55bf9SBenoit Boissinot } 892eac55bf9SBenoit Boissinot 89395c385b4SNeil Horman addr_flags = IFA_F_TEMPORARY; 89495c385b4SNeil Horman /* set in addrconf_prefix_rcv() */ 89595c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 89695c385b4SNeil Horman addr_flags |= IFA_F_OPTIMISTIC; 89795c385b4SNeil Horman 8981da177e4SLinus Torvalds ift = !max_addresses || 8991da177e4SLinus Torvalds ipv6_count_addresses(idev) < max_addresses ? 9001da177e4SLinus Torvalds ipv6_add_addr(idev, &addr, tmp_plen, 90195c385b4SNeil Horman ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK, 90295c385b4SNeil Horman addr_flags) : NULL; 9031da177e4SLinus Torvalds if (!ift || IS_ERR(ift)) { 9041da177e4SLinus Torvalds in6_ifa_put(ifp); 9051da177e4SLinus Torvalds in6_dev_put(idev); 906f3213831SJoe Perches pr_info("%s: retry temporary address regeneration\n", __func__); 9071da177e4SLinus Torvalds tmpaddr = &addr; 9081da177e4SLinus Torvalds write_lock(&idev->lock); 9091da177e4SLinus Torvalds goto retry; 9101da177e4SLinus Torvalds } 9111da177e4SLinus Torvalds 9121da177e4SLinus Torvalds spin_lock_bh(&ift->lock); 9131da177e4SLinus Torvalds ift->ifpub = ifp; 9141da177e4SLinus Torvalds ift->valid_lft = tmp_valid_lft; 9151da177e4SLinus Torvalds ift->prefered_lft = tmp_prefered_lft; 91676f793e3SLorenzo Colitti ift->cstamp = now; 9171da177e4SLinus Torvalds ift->tstamp = tmp_tstamp; 9181da177e4SLinus Torvalds spin_unlock_bh(&ift->lock); 9191da177e4SLinus Torvalds 920cf22f9a2SDavid S. Miller addrconf_dad_start(ift); 9211da177e4SLinus Torvalds in6_ifa_put(ift); 9221da177e4SLinus Torvalds in6_dev_put(idev); 9231da177e4SLinus Torvalds out: 9241da177e4SLinus Torvalds return ret; 9251da177e4SLinus Torvalds } 9261da177e4SLinus Torvalds #endif 9271da177e4SLinus Torvalds 9281da177e4SLinus Torvalds /* 929072047e4SYOSHIFUJI Hideaki * Choose an appropriate source address (RFC3484) 9301da177e4SLinus Torvalds */ 931a9b05723SYOSHIFUJI Hideaki enum { 932a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_INIT = 0, 933a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_LOCAL, 934a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_SCOPE, 935a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PREFERRED, 936a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6 937a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_HOA, 938a9b05723SYOSHIFUJI Hideaki #endif 939a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_OIF, 940a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_LABEL, 941a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_PRIVACY 942a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PRIVACY, 943a9b05723SYOSHIFUJI Hideaki #endif 944a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_ORCHID, 945a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PREFIX, 946a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_MAX 947072047e4SYOSHIFUJI Hideaki }; 948072047e4SYOSHIFUJI Hideaki 949a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score { 950a9b05723SYOSHIFUJI Hideaki int rule; 951a9b05723SYOSHIFUJI Hideaki int addr_type; 952a9b05723SYOSHIFUJI Hideaki struct inet6_ifaddr *ifa; 953a9b05723SYOSHIFUJI Hideaki DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX); 954a9b05723SYOSHIFUJI Hideaki int scopedist; 955a9b05723SYOSHIFUJI Hideaki int matchlen; 956a9b05723SYOSHIFUJI Hideaki }; 957a9b05723SYOSHIFUJI Hideaki 958a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst { 9599acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *addr; 960a9b05723SYOSHIFUJI Hideaki int ifindex; 961a9b05723SYOSHIFUJI Hideaki int scope; 962a9b05723SYOSHIFUJI Hideaki int label; 9637cbca67cSYOSHIFUJI Hideaki unsigned int prefs; 964a9b05723SYOSHIFUJI Hideaki }; 965072047e4SYOSHIFUJI Hideaki 966b6f99a21SDave Jones static inline int ipv6_saddr_preferred(int type) 9671da177e4SLinus Torvalds { 96845bb0060SUlrich Weber if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK)) 969072047e4SYOSHIFUJI Hideaki return 1; 970072047e4SYOSHIFUJI Hideaki return 0; 9711da177e4SLinus Torvalds } 9721da177e4SLinus Torvalds 9733de23255SBenjamin Thery static int ipv6_get_saddr_eval(struct net *net, 9743de23255SBenjamin Thery struct ipv6_saddr_score *score, 975a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst *dst, 976a9b05723SYOSHIFUJI Hideaki int i) 977a9b05723SYOSHIFUJI Hideaki { 978a9b05723SYOSHIFUJI Hideaki int ret; 979a9b05723SYOSHIFUJI Hideaki 980a9b05723SYOSHIFUJI Hideaki if (i <= score->rule) { 981a9b05723SYOSHIFUJI Hideaki switch (i) { 982a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_SCOPE: 983a9b05723SYOSHIFUJI Hideaki ret = score->scopedist; 984a9b05723SYOSHIFUJI Hideaki break; 985a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFIX: 986a9b05723SYOSHIFUJI Hideaki ret = score->matchlen; 987a9b05723SYOSHIFUJI Hideaki break; 988a9b05723SYOSHIFUJI Hideaki default: 989a9b05723SYOSHIFUJI Hideaki ret = !!test_bit(i, score->scorebits); 990a9b05723SYOSHIFUJI Hideaki } 991a9b05723SYOSHIFUJI Hideaki goto out; 992a9b05723SYOSHIFUJI Hideaki } 993a9b05723SYOSHIFUJI Hideaki 994a9b05723SYOSHIFUJI Hideaki switch (i) { 995a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_INIT: 996a9b05723SYOSHIFUJI Hideaki /* Rule 0: remember if hiscore is not ready yet */ 997a9b05723SYOSHIFUJI Hideaki ret = !!score->ifa; 998a9b05723SYOSHIFUJI Hideaki break; 999a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_LOCAL: 1000a9b05723SYOSHIFUJI Hideaki /* Rule 1: Prefer same address */ 1001a9b05723SYOSHIFUJI Hideaki ret = ipv6_addr_equal(&score->ifa->addr, dst->addr); 1002a9b05723SYOSHIFUJI Hideaki break; 1003a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_SCOPE: 1004a9b05723SYOSHIFUJI Hideaki /* Rule 2: Prefer appropriate scope 1005a9b05723SYOSHIFUJI Hideaki * 1006a9b05723SYOSHIFUJI Hideaki * ret 1007a9b05723SYOSHIFUJI Hideaki * ^ 1008a9b05723SYOSHIFUJI Hideaki * -1 | d 15 1009a9b05723SYOSHIFUJI Hideaki * ---+--+-+---> scope 1010a9b05723SYOSHIFUJI Hideaki * | 1011a9b05723SYOSHIFUJI Hideaki * | d is scope of the destination. 1012a9b05723SYOSHIFUJI Hideaki * B-d | \ 1013a9b05723SYOSHIFUJI Hideaki * | \ <- smaller scope is better if 1014a9b05723SYOSHIFUJI Hideaki * B-15 | \ if scope is enough for destinaion. 1015a9b05723SYOSHIFUJI Hideaki * | ret = B - scope (-1 <= scope >= d <= 15). 1016a9b05723SYOSHIFUJI Hideaki * d-C-1 | / 1017a9b05723SYOSHIFUJI Hideaki * |/ <- greater is better 1018a9b05723SYOSHIFUJI Hideaki * -C / if scope is not enough for destination. 1019a9b05723SYOSHIFUJI Hideaki * /| ret = scope - C (-1 <= d < scope <= 15). 1020a9b05723SYOSHIFUJI Hideaki * 1021a9b05723SYOSHIFUJI Hideaki * d - C - 1 < B -15 (for all -1 <= d <= 15). 1022a9b05723SYOSHIFUJI Hideaki * C > d + 14 - B >= 15 + 14 - B = 29 - B. 1023a9b05723SYOSHIFUJI Hideaki * Assume B = 0 and we get C > 29. 1024a9b05723SYOSHIFUJI Hideaki */ 1025a9b05723SYOSHIFUJI Hideaki ret = __ipv6_addr_src_scope(score->addr_type); 1026a9b05723SYOSHIFUJI Hideaki if (ret >= dst->scope) 1027a9b05723SYOSHIFUJI Hideaki ret = -ret; 1028a9b05723SYOSHIFUJI Hideaki else 1029a9b05723SYOSHIFUJI Hideaki ret -= 128; /* 30 is enough */ 1030a9b05723SYOSHIFUJI Hideaki score->scopedist = ret; 1031a9b05723SYOSHIFUJI Hideaki break; 1032a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFERRED: 1033a9b05723SYOSHIFUJI Hideaki /* Rule 3: Avoid deprecated and optimistic addresses */ 1034a9b05723SYOSHIFUJI Hideaki ret = ipv6_saddr_preferred(score->addr_type) || 1035a9b05723SYOSHIFUJI Hideaki !(score->ifa->flags & (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC)); 1036a9b05723SYOSHIFUJI Hideaki break; 1037a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6 1038a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_HOA: 10397cbca67cSYOSHIFUJI Hideaki { 1040a9b05723SYOSHIFUJI Hideaki /* Rule 4: Prefer home address */ 10417cbca67cSYOSHIFUJI Hideaki int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA); 10427cbca67cSYOSHIFUJI Hideaki ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome; 1043a9b05723SYOSHIFUJI Hideaki break; 10447cbca67cSYOSHIFUJI Hideaki } 1045a9b05723SYOSHIFUJI Hideaki #endif 1046a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_OIF: 1047a9b05723SYOSHIFUJI Hideaki /* Rule 5: Prefer outgoing interface */ 1048a9b05723SYOSHIFUJI Hideaki ret = (!dst->ifindex || 1049a9b05723SYOSHIFUJI Hideaki dst->ifindex == score->ifa->idev->dev->ifindex); 1050a9b05723SYOSHIFUJI Hideaki break; 1051a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_LABEL: 1052a9b05723SYOSHIFUJI Hideaki /* Rule 6: Prefer matching label */ 10533de23255SBenjamin Thery ret = ipv6_addr_label(net, 10543de23255SBenjamin Thery &score->ifa->addr, score->addr_type, 1055a9b05723SYOSHIFUJI Hideaki score->ifa->idev->dev->ifindex) == dst->label; 1056a9b05723SYOSHIFUJI Hideaki break; 1057a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_PRIVACY 1058a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PRIVACY: 10597cbca67cSYOSHIFUJI Hideaki { 1060a9b05723SYOSHIFUJI Hideaki /* Rule 7: Prefer public address 106125985edcSLucas De Marchi * Note: prefer temporary address if use_tempaddr >= 2 1062a9b05723SYOSHIFUJI Hideaki */ 10637cbca67cSYOSHIFUJI Hideaki int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ? 10647cbca67cSYOSHIFUJI Hideaki !!(dst->prefs & IPV6_PREFER_SRC_TMP) : 10657cbca67cSYOSHIFUJI Hideaki score->ifa->idev->cnf.use_tempaddr >= 2; 10667cbca67cSYOSHIFUJI Hideaki ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp; 1067a9b05723SYOSHIFUJI Hideaki break; 10687cbca67cSYOSHIFUJI Hideaki } 1069a9b05723SYOSHIFUJI Hideaki #endif 1070a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_ORCHID: 1071a9b05723SYOSHIFUJI Hideaki /* Rule 8-: Prefer ORCHID vs ORCHID or 1072a9b05723SYOSHIFUJI Hideaki * non-ORCHID vs non-ORCHID 1073a9b05723SYOSHIFUJI Hideaki */ 1074a9b05723SYOSHIFUJI Hideaki ret = !(ipv6_addr_orchid(&score->ifa->addr) ^ 1075a9b05723SYOSHIFUJI Hideaki ipv6_addr_orchid(dst->addr)); 1076a9b05723SYOSHIFUJI Hideaki break; 1077a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFIX: 1078a9b05723SYOSHIFUJI Hideaki /* Rule 8: Use longest matching prefix */ 107991b4b04fSYOSHIFUJI Hideaki / 吉藤英明 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr); 108091b4b04fSYOSHIFUJI Hideaki / 吉藤英明 if (ret > score->ifa->prefix_len) 108191b4b04fSYOSHIFUJI Hideaki / 吉藤英明 ret = score->ifa->prefix_len; 108291b4b04fSYOSHIFUJI Hideaki / 吉藤英明 score->matchlen = ret; 1083a9b05723SYOSHIFUJI Hideaki break; 1084a9b05723SYOSHIFUJI Hideaki default: 1085a9b05723SYOSHIFUJI Hideaki ret = 0; 1086a9b05723SYOSHIFUJI Hideaki } 1087a9b05723SYOSHIFUJI Hideaki 1088a9b05723SYOSHIFUJI Hideaki if (ret) 1089a9b05723SYOSHIFUJI Hideaki __set_bit(i, score->scorebits); 1090a9b05723SYOSHIFUJI Hideaki score->rule = i; 1091a9b05723SYOSHIFUJI Hideaki out: 1092a9b05723SYOSHIFUJI Hideaki return ret; 1093a9b05723SYOSHIFUJI Hideaki } 1094a9b05723SYOSHIFUJI Hideaki 1095b3f644fcSPatrick McHardy int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev, 10969acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *daddr, unsigned int prefs, 10977cbca67cSYOSHIFUJI Hideaki struct in6_addr *saddr) 10981da177e4SLinus Torvalds { 1099a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score scores[2], 1100a9b05723SYOSHIFUJI Hideaki *score = &scores[0], *hiscore = &scores[1]; 1101a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst dst; 1102072047e4SYOSHIFUJI Hideaki struct net_device *dev; 1103a9b05723SYOSHIFUJI Hideaki int dst_type; 11041da177e4SLinus Torvalds 1105a9b05723SYOSHIFUJI Hideaki dst_type = __ipv6_addr_type(daddr); 1106a9b05723SYOSHIFUJI Hideaki dst.addr = daddr; 1107a9b05723SYOSHIFUJI Hideaki dst.ifindex = dst_dev ? dst_dev->ifindex : 0; 1108a9b05723SYOSHIFUJI Hideaki dst.scope = __ipv6_addr_src_scope(dst_type); 11093de23255SBenjamin Thery dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex); 11107cbca67cSYOSHIFUJI Hideaki dst.prefs = prefs; 1111a9b05723SYOSHIFUJI Hideaki 1112a9b05723SYOSHIFUJI Hideaki hiscore->rule = -1; 1113a9b05723SYOSHIFUJI Hideaki hiscore->ifa = NULL; 11141da177e4SLinus Torvalds 11158814c4b5SYOSHIFUJI Hideaki rcu_read_lock(); 1116072047e4SYOSHIFUJI Hideaki 1117c6d14c84SEric Dumazet for_each_netdev_rcu(net, dev) { 1118072047e4SYOSHIFUJI Hideaki struct inet6_dev *idev; 1119072047e4SYOSHIFUJI Hideaki 1120a9b05723SYOSHIFUJI Hideaki /* Candidate Source Address (section 4) 1121072047e4SYOSHIFUJI Hideaki * - multicast and link-local destination address, 1122072047e4SYOSHIFUJI Hideaki * the set of candidate source address MUST only 1123072047e4SYOSHIFUJI Hideaki * include addresses assigned to interfaces 1124072047e4SYOSHIFUJI Hideaki * belonging to the same link as the outgoing 1125072047e4SYOSHIFUJI Hideaki * interface. 1126072047e4SYOSHIFUJI Hideaki * (- For site-local destination addresses, the 1127072047e4SYOSHIFUJI Hideaki * set of candidate source addresses MUST only 1128072047e4SYOSHIFUJI Hideaki * include addresses assigned to interfaces 1129072047e4SYOSHIFUJI Hideaki * belonging to the same site as the outgoing 1130072047e4SYOSHIFUJI Hideaki * interface.) 1131072047e4SYOSHIFUJI Hideaki */ 1132a9b05723SYOSHIFUJI Hideaki if (((dst_type & IPV6_ADDR_MULTICAST) || 1133a9b05723SYOSHIFUJI Hideaki dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL) && 1134a9b05723SYOSHIFUJI Hideaki dst.ifindex && dev->ifindex != dst.ifindex) 1135072047e4SYOSHIFUJI Hideaki continue; 1136072047e4SYOSHIFUJI Hideaki 11371da177e4SLinus Torvalds idev = __in6_dev_get(dev); 1138072047e4SYOSHIFUJI Hideaki if (!idev) 1139072047e4SYOSHIFUJI Hideaki continue; 1140072047e4SYOSHIFUJI Hideaki 11411da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1142502a2ffdSstephen hemminger list_for_each_entry(score->ifa, &idev->addr_list, if_list) { 1143a9b05723SYOSHIFUJI Hideaki int i; 1144072047e4SYOSHIFUJI Hideaki 1145a9b05723SYOSHIFUJI Hideaki /* 11466b3ae80aSYOSHIFUJI Hideaki * - Tentative Address (RFC2462 section 5.4) 11476b3ae80aSYOSHIFUJI Hideaki * - A tentative address is not considered 11486b3ae80aSYOSHIFUJI Hideaki * "assigned to an interface" in the traditional 114995c385b4SNeil Horman * sense, unless it is also flagged as optimistic. 11506b3ae80aSYOSHIFUJI Hideaki * - Candidate Source Address (section 4) 1151072047e4SYOSHIFUJI Hideaki * - In any case, anycast addresses, multicast 1152072047e4SYOSHIFUJI Hideaki * addresses, and the unspecified address MUST 1153072047e4SYOSHIFUJI Hideaki * NOT be included in a candidate set. 1154072047e4SYOSHIFUJI Hideaki */ 1155a9b05723SYOSHIFUJI Hideaki if ((score->ifa->flags & IFA_F_TENTATIVE) && 1156a9b05723SYOSHIFUJI Hideaki (!(score->ifa->flags & IFA_F_OPTIMISTIC))) 11576b3ae80aSYOSHIFUJI Hideaki continue; 1158a9b05723SYOSHIFUJI Hideaki 1159a9b05723SYOSHIFUJI Hideaki score->addr_type = __ipv6_addr_type(&score->ifa->addr); 1160a9b05723SYOSHIFUJI Hideaki 1161a9b05723SYOSHIFUJI Hideaki if (unlikely(score->addr_type == IPV6_ADDR_ANY || 1162a9b05723SYOSHIFUJI Hideaki score->addr_type & IPV6_ADDR_MULTICAST)) { 1163072047e4SYOSHIFUJI Hideaki LIMIT_NETDEBUG(KERN_DEBUG 1164072047e4SYOSHIFUJI Hideaki "ADDRCONF: unspecified / multicast address " 1165072047e4SYOSHIFUJI Hideaki "assigned as unicast address on %s", 1166072047e4SYOSHIFUJI Hideaki dev->name); 11671da177e4SLinus Torvalds continue; 1168072047e4SYOSHIFUJI Hideaki } 1169072047e4SYOSHIFUJI Hideaki 1170a9b05723SYOSHIFUJI Hideaki score->rule = -1; 1171a9b05723SYOSHIFUJI Hideaki bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX); 1172072047e4SYOSHIFUJI Hideaki 1173a9b05723SYOSHIFUJI Hideaki for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) { 1174a9b05723SYOSHIFUJI Hideaki int minihiscore, miniscore; 1175072047e4SYOSHIFUJI Hideaki 11763de23255SBenjamin Thery minihiscore = ipv6_get_saddr_eval(net, hiscore, &dst, i); 11773de23255SBenjamin Thery miniscore = ipv6_get_saddr_eval(net, score, &dst, i); 1178072047e4SYOSHIFUJI Hideaki 1179a9b05723SYOSHIFUJI Hideaki if (minihiscore > miniscore) { 1180a9b05723SYOSHIFUJI Hideaki if (i == IPV6_SADDR_RULE_SCOPE && 1181a9b05723SYOSHIFUJI Hideaki score->scopedist > 0) { 1182a9b05723SYOSHIFUJI Hideaki /* 1183a9b05723SYOSHIFUJI Hideaki * special case: 1184a9b05723SYOSHIFUJI Hideaki * each remaining entry 1185a9b05723SYOSHIFUJI Hideaki * has too small (not enough) 1186a9b05723SYOSHIFUJI Hideaki * scope, because ifa entries 1187a9b05723SYOSHIFUJI Hideaki * are sorted by their scope 1188a9b05723SYOSHIFUJI Hideaki * values. 1189072047e4SYOSHIFUJI Hideaki */ 1190a9b05723SYOSHIFUJI Hideaki goto try_nextdev; 1191072047e4SYOSHIFUJI Hideaki } 1192a9b05723SYOSHIFUJI Hideaki break; 1193a9b05723SYOSHIFUJI Hideaki } else if (minihiscore < miniscore) { 1194a9b05723SYOSHIFUJI Hideaki if (hiscore->ifa) 1195a9b05723SYOSHIFUJI Hideaki in6_ifa_put(hiscore->ifa); 119699cd07a5SJuha-Matti Tapio 1197a9b05723SYOSHIFUJI Hideaki in6_ifa_hold(score->ifa); 1198072047e4SYOSHIFUJI Hideaki 1199a0bffffcSIlpo Järvinen swap(hiscore, score); 1200a9b05723SYOSHIFUJI Hideaki 1201a9b05723SYOSHIFUJI Hideaki /* restore our iterator */ 1202a9b05723SYOSHIFUJI Hideaki score->ifa = hiscore->ifa; 1203a9b05723SYOSHIFUJI Hideaki 1204a9b05723SYOSHIFUJI Hideaki break; 12051da177e4SLinus Torvalds } 1206a9b05723SYOSHIFUJI Hideaki } 1207a9b05723SYOSHIFUJI Hideaki } 1208a9b05723SYOSHIFUJI Hideaki try_nextdev: 12091da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 12101da177e4SLinus Torvalds } 12118814c4b5SYOSHIFUJI Hideaki rcu_read_unlock(); 12121da177e4SLinus Torvalds 1213a9b05723SYOSHIFUJI Hideaki if (!hiscore->ifa) 1214072047e4SYOSHIFUJI Hideaki return -EADDRNOTAVAIL; 12151da177e4SLinus Torvalds 12164e3fd7a0SAlexey Dobriyan *saddr = hiscore->ifa->addr; 1217a9b05723SYOSHIFUJI Hideaki in6_ifa_put(hiscore->ifa); 1218072047e4SYOSHIFUJI Hideaki return 0; 12191da177e4SLinus Torvalds } 12205e5f3f0fSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_dev_get_saddr); 12211da177e4SLinus Torvalds 122295c385b4SNeil Horman int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr, 122395c385b4SNeil Horman unsigned char banned_flags) 12241da177e4SLinus Torvalds { 12251da177e4SLinus Torvalds struct inet6_dev *idev; 12261da177e4SLinus Torvalds int err = -EADDRNOTAVAIL; 12271da177e4SLinus Torvalds 12288814c4b5SYOSHIFUJI Hideaki rcu_read_lock(); 1229e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 1230e21e8467SStephen Hemminger if (idev) { 12311da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 12321da177e4SLinus Torvalds 12331da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1234502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 1235502a2ffdSstephen hemminger if (ifp->scope == IFA_LINK && 1236502a2ffdSstephen hemminger !(ifp->flags & banned_flags)) { 12374e3fd7a0SAlexey Dobriyan *addr = ifp->addr; 12381da177e4SLinus Torvalds err = 0; 12391da177e4SLinus Torvalds break; 12401da177e4SLinus Torvalds } 12411da177e4SLinus Torvalds } 12421da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 12431da177e4SLinus Torvalds } 12448814c4b5SYOSHIFUJI Hideaki rcu_read_unlock(); 12451da177e4SLinus Torvalds return err; 12461da177e4SLinus Torvalds } 12471da177e4SLinus Torvalds 12481da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev) 12491da177e4SLinus Torvalds { 12501da177e4SLinus Torvalds int cnt = 0; 12511da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 12521da177e4SLinus Torvalds 12531da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1254502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) 12551da177e4SLinus Torvalds cnt++; 12561da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 12571da177e4SLinus Torvalds return cnt; 12581da177e4SLinus Torvalds } 12591da177e4SLinus Torvalds 1260b71d1d42SEric Dumazet int ipv6_chk_addr(struct net *net, const struct in6_addr *addr, 1261bfeade08SDaniel Lezcano struct net_device *dev, int strict) 12621da177e4SLinus Torvalds { 1263eedf042aSStephen Hemminger struct inet6_ifaddr *ifp; 1264c2e21293Sstephen hemminger struct hlist_node *node; 1265ddbe5032SEric Dumazet unsigned int hash = inet6_addr_hash(addr); 12661da177e4SLinus Torvalds 12675c578aedSstephen hemminger rcu_read_lock_bh(); 12685c578aedSstephen hemminger hlist_for_each_entry_rcu(ifp, node, &inet6_addr_lst[hash], addr_lst) { 1269878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 1270bfeade08SDaniel Lezcano continue; 12711da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr) && 1272eedf042aSStephen Hemminger !(ifp->flags&IFA_F_TENTATIVE) && 1273eedf042aSStephen Hemminger (dev == NULL || ifp->idev->dev == dev || 1274eedf042aSStephen Hemminger !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) { 12755c578aedSstephen hemminger rcu_read_unlock_bh(); 1276eedf042aSStephen Hemminger return 1; 1277eedf042aSStephen Hemminger } 1278eedf042aSStephen Hemminger } 12795c578aedSstephen hemminger 1280eedf042aSStephen Hemminger rcu_read_unlock_bh(); 1281eedf042aSStephen Hemminger return 0; 12821da177e4SLinus Torvalds } 12837159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_addr); 12847159039aSYOSHIFUJI Hideaki 12853e81c6daSDavid S. Miller static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr, 128606bfe655SDaniel Lezcano struct net_device *dev) 12871da177e4SLinus Torvalds { 1288ddbe5032SEric Dumazet unsigned int hash = inet6_addr_hash(addr); 12891da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 1290c2e21293Sstephen hemminger struct hlist_node *node; 12911da177e4SLinus Torvalds 1292c2e21293Sstephen hemminger hlist_for_each_entry(ifp, node, &inet6_addr_lst[hash], addr_lst) { 1293878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 129406bfe655SDaniel Lezcano continue; 12951da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr)) { 12961da177e4SLinus Torvalds if (dev == NULL || ifp->idev->dev == dev) 12973e81c6daSDavid S. Miller return true; 12981da177e4SLinus Torvalds } 12991da177e4SLinus Torvalds } 13003e81c6daSDavid S. Miller return false; 13011da177e4SLinus Torvalds } 13021da177e4SLinus Torvalds 1303b71d1d42SEric Dumazet int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev) 130452eeeb84SYOSHIFUJI Hideaki { 130552eeeb84SYOSHIFUJI Hideaki struct inet6_dev *idev; 130652eeeb84SYOSHIFUJI Hideaki struct inet6_ifaddr *ifa; 130752eeeb84SYOSHIFUJI Hideaki int onlink; 130852eeeb84SYOSHIFUJI Hideaki 130952eeeb84SYOSHIFUJI Hideaki onlink = 0; 131052eeeb84SYOSHIFUJI Hideaki rcu_read_lock(); 131152eeeb84SYOSHIFUJI Hideaki idev = __in6_dev_get(dev); 131252eeeb84SYOSHIFUJI Hideaki if (idev) { 131352eeeb84SYOSHIFUJI Hideaki read_lock_bh(&idev->lock); 1314502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 131552eeeb84SYOSHIFUJI Hideaki onlink = ipv6_prefix_equal(addr, &ifa->addr, 131652eeeb84SYOSHIFUJI Hideaki ifa->prefix_len); 131752eeeb84SYOSHIFUJI Hideaki if (onlink) 131852eeeb84SYOSHIFUJI Hideaki break; 131952eeeb84SYOSHIFUJI Hideaki } 132052eeeb84SYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 132152eeeb84SYOSHIFUJI Hideaki } 132252eeeb84SYOSHIFUJI Hideaki rcu_read_unlock(); 132352eeeb84SYOSHIFUJI Hideaki return onlink; 132452eeeb84SYOSHIFUJI Hideaki } 132552eeeb84SYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_prefix); 132652eeeb84SYOSHIFUJI Hideaki 13279acd9f3aSYOSHIFUJI Hideaki struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr, 13281cab3da6SDaniel Lezcano struct net_device *dev, int strict) 13291da177e4SLinus Torvalds { 1330b79d1d54SDavid S. Miller struct inet6_ifaddr *ifp, *result = NULL; 1331ddbe5032SEric Dumazet unsigned int hash = inet6_addr_hash(addr); 1332b79d1d54SDavid S. Miller struct hlist_node *node; 13331da177e4SLinus Torvalds 13345c578aedSstephen hemminger rcu_read_lock_bh(); 13354f70eccaSEric Dumazet hlist_for_each_entry_rcu_bh(ifp, node, &inet6_addr_lst[hash], addr_lst) { 1336878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 13371cab3da6SDaniel Lezcano continue; 13381da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr)) { 13391da177e4SLinus Torvalds if (dev == NULL || ifp->idev->dev == dev || 13401da177e4SLinus Torvalds !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) { 1341b79d1d54SDavid S. Miller result = ifp; 13421da177e4SLinus Torvalds in6_ifa_hold(ifp); 13431da177e4SLinus Torvalds break; 13441da177e4SLinus Torvalds } 13451da177e4SLinus Torvalds } 13461da177e4SLinus Torvalds } 13475c578aedSstephen hemminger rcu_read_unlock_bh(); 13481da177e4SLinus Torvalds 1349b79d1d54SDavid S. Miller return result; 13501da177e4SLinus Torvalds } 13511da177e4SLinus Torvalds 13521da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */ 13531da177e4SLinus Torvalds 1354cc411d0bSBrian Haley static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed) 13551da177e4SLinus Torvalds { 13561da177e4SLinus Torvalds if (ifp->flags&IFA_F_PERMANENT) { 13571da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 13581da177e4SLinus Torvalds addrconf_del_timer(ifp); 13591da177e4SLinus Torvalds ifp->flags |= IFA_F_TENTATIVE; 1360cc411d0bSBrian Haley if (dad_failed) 1361cc411d0bSBrian Haley ifp->flags |= IFA_F_DADFAILED; 13621da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 1363e2577a06SHerbert Xu if (dad_failed) 1364e2577a06SHerbert Xu ipv6_ifa_notify(0, ifp); 13651da177e4SLinus Torvalds in6_ifa_put(ifp); 13661da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 13671da177e4SLinus Torvalds } else if (ifp->flags&IFA_F_TEMPORARY) { 13681da177e4SLinus Torvalds struct inet6_ifaddr *ifpub; 13691da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 13701da177e4SLinus Torvalds ifpub = ifp->ifpub; 13711da177e4SLinus Torvalds if (ifpub) { 13721da177e4SLinus Torvalds in6_ifa_hold(ifpub); 13731da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 13741da177e4SLinus Torvalds ipv6_create_tempaddr(ifpub, ifp); 13751da177e4SLinus Torvalds in6_ifa_put(ifpub); 13761da177e4SLinus Torvalds } else { 13771da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 13781da177e4SLinus Torvalds } 13791da177e4SLinus Torvalds ipv6_del_addr(ifp); 13801da177e4SLinus Torvalds #endif 13811da177e4SLinus Torvalds } else 13821da177e4SLinus Torvalds ipv6_del_addr(ifp); 13831da177e4SLinus Torvalds } 13841da177e4SLinus Torvalds 1385f2344a13SHerbert Xu static int addrconf_dad_end(struct inet6_ifaddr *ifp) 1386f2344a13SHerbert Xu { 1387f2344a13SHerbert Xu int err = -ENOENT; 1388f2344a13SHerbert Xu 1389f2344a13SHerbert Xu spin_lock(&ifp->state_lock); 1390f2344a13SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DAD) { 1391f2344a13SHerbert Xu ifp->state = INET6_IFADDR_STATE_POSTDAD; 1392f2344a13SHerbert Xu err = 0; 1393f2344a13SHerbert Xu } 1394f2344a13SHerbert Xu spin_unlock(&ifp->state_lock); 1395f2344a13SHerbert Xu 1396f2344a13SHerbert Xu return err; 1397f2344a13SHerbert Xu } 1398f2344a13SHerbert Xu 13993c21edbdSYOSHIFUJI Hideaki void addrconf_dad_failure(struct inet6_ifaddr *ifp) 14003c21edbdSYOSHIFUJI Hideaki { 14011b34be74SYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev; 14029bdd8d40SBrian Haley 1403853dc2e0SUrsula Braun if (addrconf_dad_end(ifp)) { 1404853dc2e0SUrsula Braun in6_ifa_put(ifp); 1405f2344a13SHerbert Xu return; 1406853dc2e0SUrsula Braun } 1407f2344a13SHerbert Xu 1408e87cc472SJoe Perches net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n", 14090522fea6SJens Rosenboom ifp->idev->dev->name, &ifp->addr); 14109bdd8d40SBrian Haley 14111b34be74SYOSHIFUJI Hideaki if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) { 14121b34be74SYOSHIFUJI Hideaki struct in6_addr addr; 14131b34be74SYOSHIFUJI Hideaki 14141b34be74SYOSHIFUJI Hideaki addr.s6_addr32[0] = htonl(0xfe800000); 14151b34be74SYOSHIFUJI Hideaki addr.s6_addr32[1] = 0; 14161b34be74SYOSHIFUJI Hideaki 14171b34be74SYOSHIFUJI Hideaki if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) && 14181b34be74SYOSHIFUJI Hideaki ipv6_addr_equal(&ifp->addr, &addr)) { 14191b34be74SYOSHIFUJI Hideaki /* DAD failed for link-local based on MAC address */ 14201b34be74SYOSHIFUJI Hideaki idev->cnf.disable_ipv6 = 1; 14219bdd8d40SBrian Haley 1422f3213831SJoe Perches pr_info("%s: IPv6 being disabled!\n", 14239bdd8d40SBrian Haley ifp->idev->dev->name); 14241b34be74SYOSHIFUJI Hideaki } 14251b34be74SYOSHIFUJI Hideaki } 14261b34be74SYOSHIFUJI Hideaki 1427cc411d0bSBrian Haley addrconf_dad_stop(ifp, 1); 14283c21edbdSYOSHIFUJI Hideaki } 14291da177e4SLinus Torvalds 14301da177e4SLinus Torvalds /* Join to solicited addr multicast group. */ 14311da177e4SLinus Torvalds 1432b71d1d42SEric Dumazet void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr) 14331da177e4SLinus Torvalds { 14341da177e4SLinus Torvalds struct in6_addr maddr; 14351da177e4SLinus Torvalds 14361da177e4SLinus Torvalds if (dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 14371da177e4SLinus Torvalds return; 14381da177e4SLinus Torvalds 14391da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr); 14401da177e4SLinus Torvalds ipv6_dev_mc_inc(dev, &maddr); 14411da177e4SLinus Torvalds } 14421da177e4SLinus Torvalds 1443b71d1d42SEric Dumazet void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr) 14441da177e4SLinus Torvalds { 14451da177e4SLinus Torvalds struct in6_addr maddr; 14461da177e4SLinus Torvalds 14471da177e4SLinus Torvalds if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 14481da177e4SLinus Torvalds return; 14491da177e4SLinus Torvalds 14501da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr); 14511da177e4SLinus Torvalds __ipv6_dev_mc_dec(idev, &maddr); 14521da177e4SLinus Torvalds } 14531da177e4SLinus Torvalds 145420380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp) 14551da177e4SLinus Torvalds { 14561da177e4SLinus Torvalds struct in6_addr addr; 14572bda8a0cSBjørn Mork if (ifp->prefix_len == 127) /* RFC 6164 */ 14582bda8a0cSBjørn Mork return; 14591da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 14601da177e4SLinus Torvalds if (ipv6_addr_any(&addr)) 14611da177e4SLinus Torvalds return; 14621da177e4SLinus Torvalds ipv6_dev_ac_inc(ifp->idev->dev, &addr); 14631da177e4SLinus Torvalds } 14641da177e4SLinus Torvalds 146520380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp) 14661da177e4SLinus Torvalds { 14671da177e4SLinus Torvalds struct in6_addr addr; 146832019e65SYOSHIFUJI Hideaki if (ifp->prefix_len == 127) /* RFC 6164 */ 146932019e65SYOSHIFUJI Hideaki return; 14701da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 14711da177e4SLinus Torvalds if (ipv6_addr_any(&addr)) 14721da177e4SLinus Torvalds return; 14731da177e4SLinus Torvalds __ipv6_dev_ac_dec(ifp->idev, &addr); 14741da177e4SLinus Torvalds } 14751da177e4SLinus Torvalds 1476073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev) 14771da177e4SLinus Torvalds { 14781da177e4SLinus Torvalds if (dev->addr_len != ETH_ALEN) 14791da177e4SLinus Torvalds return -1; 14801da177e4SLinus Torvalds memcpy(eui, dev->dev_addr, 3); 14811da177e4SLinus Torvalds memcpy(eui + 5, dev->dev_addr + 3, 3); 14821da177e4SLinus Torvalds 14831da177e4SLinus Torvalds /* 14841da177e4SLinus Torvalds * The zSeries OSA network cards can be shared among various 14851da177e4SLinus Torvalds * OS instances, but the OSA cards have only one MAC address. 14861da177e4SLinus Torvalds * This leads to duplicate address conflicts in conjunction 14871da177e4SLinus Torvalds * with IPv6 if more than one instance uses the same card. 14881da177e4SLinus Torvalds * 14891da177e4SLinus Torvalds * The driver for these cards can deliver a unique 16-bit 14901da177e4SLinus Torvalds * identifier for each instance sharing the same card. It is 14911da177e4SLinus Torvalds * placed instead of 0xFFFE in the interface identifier. The 14921da177e4SLinus Torvalds * "u" bit of the interface identifier is not inverted in this 14931da177e4SLinus Torvalds * case. Hence the resulting interface identifier has local 14941da177e4SLinus Torvalds * scope according to RFC2373. 14951da177e4SLinus Torvalds */ 14961da177e4SLinus Torvalds if (dev->dev_id) { 14971da177e4SLinus Torvalds eui[3] = (dev->dev_id >> 8) & 0xFF; 14981da177e4SLinus Torvalds eui[4] = dev->dev_id & 0xFF; 14991da177e4SLinus Torvalds } else { 15001da177e4SLinus Torvalds eui[3] = 0xFF; 15011da177e4SLinus Torvalds eui[4] = 0xFE; 15021da177e4SLinus Torvalds eui[0] ^= 2; 15031da177e4SLinus Torvalds } 15041da177e4SLinus Torvalds return 0; 1505073a8e0eSYOSHIFUJI Hideaki } 1506073a8e0eSYOSHIFUJI Hideaki 150706a4c1c5Salex.bluesman.smirnov@gmail.com static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev) 150806a4c1c5Salex.bluesman.smirnov@gmail.com { 150906a4c1c5Salex.bluesman.smirnov@gmail.com if (dev->addr_len != IEEE802154_ADDR_LEN) 151006a4c1c5Salex.bluesman.smirnov@gmail.com return -1; 151106a4c1c5Salex.bluesman.smirnov@gmail.com memcpy(eui, dev->dev_addr, 8); 151206a4c1c5Salex.bluesman.smirnov@gmail.com return 0; 151306a4c1c5Salex.bluesman.smirnov@gmail.com } 151406a4c1c5Salex.bluesman.smirnov@gmail.com 1515073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev) 1516073a8e0eSYOSHIFUJI Hideaki { 15171da177e4SLinus Torvalds /* XXX: inherit EUI-64 from other interface -- yoshfuji */ 15181da177e4SLinus Torvalds if (dev->addr_len != ARCNET_ALEN) 15191da177e4SLinus Torvalds return -1; 15201da177e4SLinus Torvalds memset(eui, 0, 7); 15211da177e4SLinus Torvalds eui[7] = *(u8 *)dev->dev_addr; 15221da177e4SLinus Torvalds return 0; 1523073a8e0eSYOSHIFUJI Hideaki } 1524073a8e0eSYOSHIFUJI Hideaki 1525073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev) 1526073a8e0eSYOSHIFUJI Hideaki { 15271da177e4SLinus Torvalds if (dev->addr_len != INFINIBAND_ALEN) 15281da177e4SLinus Torvalds return -1; 15291da177e4SLinus Torvalds memcpy(eui, dev->dev_addr + 12, 8); 15301da177e4SLinus Torvalds eui[0] |= 2; 15311da177e4SLinus Torvalds return 0; 15321da177e4SLinus Torvalds } 1533073a8e0eSYOSHIFUJI Hideaki 1534c61393eaSstephen hemminger static int __ipv6_isatap_ifid(u8 *eui, __be32 addr) 1535dfd982baSYOSHIFUJI Hideaki { 15369af28511SSascha Hlusiak if (addr == 0) 15379af28511SSascha Hlusiak return -1; 1538dfd982baSYOSHIFUJI Hideaki eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) || 1539dfd982baSYOSHIFUJI Hideaki ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) || 1540dfd982baSYOSHIFUJI Hideaki ipv4_is_private_172(addr) || ipv4_is_test_192(addr) || 1541dfd982baSYOSHIFUJI Hideaki ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) || 1542dfd982baSYOSHIFUJI Hideaki ipv4_is_test_198(addr) || ipv4_is_multicast(addr) || 1543dfd982baSYOSHIFUJI Hideaki ipv4_is_lbcast(addr)) ? 0x00 : 0x02; 1544dfd982baSYOSHIFUJI Hideaki eui[1] = 0; 1545dfd982baSYOSHIFUJI Hideaki eui[2] = 0x5E; 1546dfd982baSYOSHIFUJI Hideaki eui[3] = 0xFE; 1547dfd982baSYOSHIFUJI Hideaki memcpy(eui + 4, &addr, 4); 1548dfd982baSYOSHIFUJI Hideaki return 0; 1549dfd982baSYOSHIFUJI Hideaki } 1550dfd982baSYOSHIFUJI Hideaki 1551dfd982baSYOSHIFUJI Hideaki static int addrconf_ifid_sit(u8 *eui, struct net_device *dev) 1552dfd982baSYOSHIFUJI Hideaki { 1553dfd982baSYOSHIFUJI Hideaki if (dev->priv_flags & IFF_ISATAP) 1554dfd982baSYOSHIFUJI Hideaki return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); 1555dfd982baSYOSHIFUJI Hideaki return -1; 1556dfd982baSYOSHIFUJI Hideaki } 1557dfd982baSYOSHIFUJI Hideaki 1558aee80b54Sstephen hemminger static int addrconf_ifid_gre(u8 *eui, struct net_device *dev) 1559aee80b54Sstephen hemminger { 1560aee80b54Sstephen hemminger return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); 1561aee80b54Sstephen hemminger } 1562aee80b54Sstephen hemminger 1563073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev) 1564073a8e0eSYOSHIFUJI Hideaki { 1565073a8e0eSYOSHIFUJI Hideaki switch (dev->type) { 1566073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ETHER: 1567073a8e0eSYOSHIFUJI Hideaki case ARPHRD_FDDI: 1568073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_eui48(eui, dev); 1569073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ARCNET: 1570073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_arcnet(eui, dev); 1571073a8e0eSYOSHIFUJI Hideaki case ARPHRD_INFINIBAND: 1572073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_infiniband(eui, dev); 1573c7dc89c0SFred L. Templin case ARPHRD_SIT: 1574dfd982baSYOSHIFUJI Hideaki return addrconf_ifid_sit(eui, dev); 1575aee80b54Sstephen hemminger case ARPHRD_IPGRE: 1576aee80b54Sstephen hemminger return addrconf_ifid_gre(eui, dev); 157706a4c1c5Salex.bluesman.smirnov@gmail.com case ARPHRD_IEEE802154: 157806a4c1c5Salex.bluesman.smirnov@gmail.com return addrconf_ifid_eui64(eui, dev); 1579073a8e0eSYOSHIFUJI Hideaki } 15801da177e4SLinus Torvalds return -1; 15811da177e4SLinus Torvalds } 15821da177e4SLinus Torvalds 15831da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev) 15841da177e4SLinus Torvalds { 15851da177e4SLinus Torvalds int err = -1; 15861da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 15871da177e4SLinus Torvalds 15881da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1589502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 15901da177e4SLinus Torvalds if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) { 15911da177e4SLinus Torvalds memcpy(eui, ifp->addr.s6_addr+8, 8); 15921da177e4SLinus Torvalds err = 0; 15931da177e4SLinus Torvalds break; 15941da177e4SLinus Torvalds } 15951da177e4SLinus Torvalds } 15961da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 15971da177e4SLinus Torvalds return err; 15981da177e4SLinus Torvalds } 15991da177e4SLinus Torvalds 16001da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 16011da177e4SLinus Torvalds /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */ 1602eb7e0575SSorin Dumitru static void __ipv6_regen_rndid(struct inet6_dev *idev) 16031da177e4SLinus Torvalds { 16041da177e4SLinus Torvalds regen: 1605955189efSYOSHIFUJI Hideaki get_random_bytes(idev->rndid, sizeof(idev->rndid)); 16061da177e4SLinus Torvalds idev->rndid[0] &= ~0x02; 16071da177e4SLinus Torvalds 16081da177e4SLinus Torvalds /* 16091da177e4SLinus Torvalds * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>: 16101da177e4SLinus Torvalds * check if generated address is not inappropriate 16111da177e4SLinus Torvalds * 16121da177e4SLinus Torvalds * - Reserved subnet anycast (RFC 2526) 16131da177e4SLinus Torvalds * 11111101 11....11 1xxxxxxx 1614c7dc89c0SFred L. Templin * - ISATAP (RFC4214) 6.1 16151da177e4SLinus Torvalds * 00-00-5E-FE-xx-xx-xx-xx 16161da177e4SLinus Torvalds * - value 0 16171da177e4SLinus Torvalds * - XXX: already assigned to an address on the device 16181da177e4SLinus Torvalds */ 16191da177e4SLinus Torvalds if (idev->rndid[0] == 0xfd && 16201da177e4SLinus Torvalds (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff && 16211da177e4SLinus Torvalds (idev->rndid[7]&0x80)) 16221da177e4SLinus Torvalds goto regen; 16231da177e4SLinus Torvalds if ((idev->rndid[0]|idev->rndid[1]) == 0) { 16241da177e4SLinus Torvalds if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe) 16251da177e4SLinus Torvalds goto regen; 16261da177e4SLinus Torvalds if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00) 16271da177e4SLinus Torvalds goto regen; 16281da177e4SLinus Torvalds } 16291da177e4SLinus Torvalds } 16301da177e4SLinus Torvalds 16311da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data) 16321da177e4SLinus Torvalds { 16331da177e4SLinus Torvalds struct inet6_dev *idev = (struct inet6_dev *) data; 16341da177e4SLinus Torvalds unsigned long expires; 16351da177e4SLinus Torvalds 16368814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 16371da177e4SLinus Torvalds write_lock_bh(&idev->lock); 16381da177e4SLinus Torvalds 16391da177e4SLinus Torvalds if (idev->dead) 16401da177e4SLinus Torvalds goto out; 16411da177e4SLinus Torvalds 1642eb7e0575SSorin Dumitru __ipv6_regen_rndid(idev); 16431da177e4SLinus Torvalds 16441da177e4SLinus Torvalds expires = jiffies + 16451da177e4SLinus Torvalds idev->cnf.temp_prefered_lft * HZ - 1646784e2710SBen Hutchings idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - 1647784e2710SBen Hutchings idev->cnf.max_desync_factor * HZ; 16481da177e4SLinus Torvalds if (time_before(expires, jiffies)) { 1649f3213831SJoe Perches pr_warn("%s: too short regeneration interval; timer disabled for %s\n", 1650f3213831SJoe Perches __func__, idev->dev->name); 16511da177e4SLinus Torvalds goto out; 16521da177e4SLinus Torvalds } 16531da177e4SLinus Torvalds 16541da177e4SLinus Torvalds if (!mod_timer(&idev->regen_timer, expires)) 16551da177e4SLinus Torvalds in6_dev_hold(idev); 16561da177e4SLinus Torvalds 16571da177e4SLinus Torvalds out: 16581da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 16598814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 16601da177e4SLinus Torvalds in6_dev_put(idev); 16611da177e4SLinus Torvalds } 16621da177e4SLinus Torvalds 1663eb7e0575SSorin Dumitru static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) 16648e5e8f30SEldad Zack { 16651da177e4SLinus Torvalds if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0) 1666eb7e0575SSorin Dumitru __ipv6_regen_rndid(idev); 16671da177e4SLinus Torvalds } 16681da177e4SLinus Torvalds #endif 16691da177e4SLinus Torvalds 16701da177e4SLinus Torvalds /* 16711da177e4SLinus Torvalds * Add prefix route. 16721da177e4SLinus Torvalds */ 16731da177e4SLinus Torvalds 16741da177e4SLinus Torvalds static void 16751da177e4SLinus Torvalds addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev, 1676e431b8c0SJamal Hadi Salim unsigned long expires, u32 flags) 16771da177e4SLinus Torvalds { 167886872cb5SThomas Graf struct fib6_config cfg = { 167986872cb5SThomas Graf .fc_table = RT6_TABLE_PREFIX, 168086872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 168186872cb5SThomas Graf .fc_ifindex = dev->ifindex, 168286872cb5SThomas Graf .fc_expires = expires, 168386872cb5SThomas Graf .fc_dst_len = plen, 168486872cb5SThomas Graf .fc_flags = RTF_UP | flags, 1685c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 1686f410a1fbSStephen Hemminger .fc_protocol = RTPROT_KERNEL, 168786872cb5SThomas Graf }; 16881da177e4SLinus Torvalds 16894e3fd7a0SAlexey Dobriyan cfg.fc_dst = *pfx; 16901da177e4SLinus Torvalds 16911da177e4SLinus Torvalds /* Prevent useless cloning on PtP SIT. 16921da177e4SLinus Torvalds This thing is done here expecting that the whole 16931da177e4SLinus Torvalds class of non-broadcast devices need not cloning. 16941da177e4SLinus Torvalds */ 16950be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 16961da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT)) 169786872cb5SThomas Graf cfg.fc_flags |= RTF_NONEXTHOP; 16980be669bbSJoerg Roedel #endif 16991da177e4SLinus Torvalds 170086872cb5SThomas Graf ip6_route_add(&cfg); 17011da177e4SLinus Torvalds } 17021da177e4SLinus Torvalds 170314ef37b6SAndreas Hofmeister 170414ef37b6SAndreas Hofmeister static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx, 170514ef37b6SAndreas Hofmeister int plen, 170614ef37b6SAndreas Hofmeister const struct net_device *dev, 170714ef37b6SAndreas Hofmeister u32 flags, u32 noflags) 170814ef37b6SAndreas Hofmeister { 170914ef37b6SAndreas Hofmeister struct fib6_node *fn; 171014ef37b6SAndreas Hofmeister struct rt6_info *rt = NULL; 171114ef37b6SAndreas Hofmeister struct fib6_table *table; 171214ef37b6SAndreas Hofmeister 171314ef37b6SAndreas Hofmeister table = fib6_get_table(dev_net(dev), RT6_TABLE_PREFIX); 171414ef37b6SAndreas Hofmeister if (table == NULL) 171514ef37b6SAndreas Hofmeister return NULL; 171614ef37b6SAndreas Hofmeister 17175744dd9bSLi RongQing read_lock_bh(&table->tb6_lock); 171814ef37b6SAndreas Hofmeister fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0); 171914ef37b6SAndreas Hofmeister if (!fn) 172014ef37b6SAndreas Hofmeister goto out; 172114ef37b6SAndreas Hofmeister for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 1722d1918542SDavid S. Miller if (rt->dst.dev->ifindex != dev->ifindex) 172314ef37b6SAndreas Hofmeister continue; 172414ef37b6SAndreas Hofmeister if ((rt->rt6i_flags & flags) != flags) 172514ef37b6SAndreas Hofmeister continue; 172614ef37b6SAndreas Hofmeister if ((noflags != 0) && ((rt->rt6i_flags & flags) != 0)) 172714ef37b6SAndreas Hofmeister continue; 172814ef37b6SAndreas Hofmeister dst_hold(&rt->dst); 172914ef37b6SAndreas Hofmeister break; 173014ef37b6SAndreas Hofmeister } 173114ef37b6SAndreas Hofmeister out: 17325744dd9bSLi RongQing read_unlock_bh(&table->tb6_lock); 173314ef37b6SAndreas Hofmeister return rt; 173414ef37b6SAndreas Hofmeister } 173514ef37b6SAndreas Hofmeister 173614ef37b6SAndreas Hofmeister 17371da177e4SLinus Torvalds /* Create "default" multicast route to the interface */ 17381da177e4SLinus Torvalds 17391da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev) 17401da177e4SLinus Torvalds { 174186872cb5SThomas Graf struct fib6_config cfg = { 174286872cb5SThomas Graf .fc_table = RT6_TABLE_LOCAL, 174386872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 174486872cb5SThomas Graf .fc_ifindex = dev->ifindex, 174586872cb5SThomas Graf .fc_dst_len = 8, 174686872cb5SThomas Graf .fc_flags = RTF_UP, 1747c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 174886872cb5SThomas Graf }; 17491da177e4SLinus Torvalds 175086872cb5SThomas Graf ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0); 175186872cb5SThomas Graf 175286872cb5SThomas Graf ip6_route_add(&cfg); 17531da177e4SLinus Torvalds } 17541da177e4SLinus Torvalds 17550be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 17561da177e4SLinus Torvalds static void sit_route_add(struct net_device *dev) 17571da177e4SLinus Torvalds { 175886872cb5SThomas Graf struct fib6_config cfg = { 175986872cb5SThomas Graf .fc_table = RT6_TABLE_MAIN, 176086872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 176186872cb5SThomas Graf .fc_ifindex = dev->ifindex, 176286872cb5SThomas Graf .fc_dst_len = 96, 176386872cb5SThomas Graf .fc_flags = RTF_UP | RTF_NONEXTHOP, 1764c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 176586872cb5SThomas Graf }; 17661da177e4SLinus Torvalds 17671da177e4SLinus Torvalds /* prefix length - 96 bits "::d.d.d.d" */ 176886872cb5SThomas Graf ip6_route_add(&cfg); 17691da177e4SLinus Torvalds } 17700be669bbSJoerg Roedel #endif 17711da177e4SLinus Torvalds 17721da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev) 17731da177e4SLinus Torvalds { 17741da177e4SLinus Torvalds struct inet6_dev *idev; 17751da177e4SLinus Torvalds 17761da177e4SLinus Torvalds ASSERT_RTNL(); 17771da177e4SLinus Torvalds 1778e21e8467SStephen Hemminger idev = ipv6_find_idev(dev); 1779e21e8467SStephen Hemminger if (!idev) 178064e724f6SBrian Haley return ERR_PTR(-ENOBUFS); 178164e724f6SBrian Haley 178264e724f6SBrian Haley if (idev->cnf.disable_ipv6) 178364e724f6SBrian Haley return ERR_PTR(-EACCES); 17841da177e4SLinus Torvalds 17851da177e4SLinus Torvalds /* Add default multicast route */ 17864af04abaSLi Wei if (!(dev->flags & IFF_LOOPBACK)) 17871da177e4SLinus Torvalds addrconf_add_mroute(dev); 17881da177e4SLinus Torvalds 17891da177e4SLinus Torvalds return idev; 17901da177e4SLinus Torvalds } 17911da177e4SLinus Torvalds 1792e6bff995SNeil Horman void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao) 17931da177e4SLinus Torvalds { 17941da177e4SLinus Torvalds struct prefix_info *pinfo; 17951da177e4SLinus Torvalds __u32 valid_lft; 17961da177e4SLinus Torvalds __u32 prefered_lft; 17971da177e4SLinus Torvalds int addr_type; 17981da177e4SLinus Torvalds struct inet6_dev *in6_dev; 179956d417b1SBrian Haley struct net *net = dev_net(dev); 18001da177e4SLinus Torvalds 18011da177e4SLinus Torvalds pinfo = (struct prefix_info *) opt; 18021da177e4SLinus Torvalds 18031da177e4SLinus Torvalds if (len < sizeof(struct prefix_info)) { 18041da177e4SLinus Torvalds ADBG(("addrconf: prefix option too short\n")); 18051da177e4SLinus Torvalds return; 18061da177e4SLinus Torvalds } 18071da177e4SLinus Torvalds 18081da177e4SLinus Torvalds /* 18091da177e4SLinus Torvalds * Validation checks ([ADDRCONF], page 19) 18101da177e4SLinus Torvalds */ 18111da177e4SLinus Torvalds 18121da177e4SLinus Torvalds addr_type = ipv6_addr_type(&pinfo->prefix); 18131da177e4SLinus Torvalds 18141da177e4SLinus Torvalds if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)) 18151da177e4SLinus Torvalds return; 18161da177e4SLinus Torvalds 18171da177e4SLinus Torvalds valid_lft = ntohl(pinfo->valid); 18181da177e4SLinus Torvalds prefered_lft = ntohl(pinfo->prefered); 18191da177e4SLinus Torvalds 18201da177e4SLinus Torvalds if (prefered_lft > valid_lft) { 1821e87cc472SJoe Perches net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n"); 18221da177e4SLinus Torvalds return; 18231da177e4SLinus Torvalds } 18241da177e4SLinus Torvalds 18251da177e4SLinus Torvalds in6_dev = in6_dev_get(dev); 18261da177e4SLinus Torvalds 18271da177e4SLinus Torvalds if (in6_dev == NULL) { 1828e87cc472SJoe Perches net_dbg_ratelimited("addrconf: device %s not configured\n", 1829e87cc472SJoe Perches dev->name); 18301da177e4SLinus Torvalds return; 18311da177e4SLinus Torvalds } 18321da177e4SLinus Torvalds 18331da177e4SLinus Torvalds /* 18341da177e4SLinus Torvalds * Two things going on here: 18351da177e4SLinus Torvalds * 1) Add routes for on-link prefixes 18361da177e4SLinus Torvalds * 2) Configure prefixes with the auto flag set 18371da177e4SLinus Torvalds */ 18381da177e4SLinus Torvalds 18394bed72e4SYOSHIFUJI Hideaki if (pinfo->onlink) { 18404bed72e4SYOSHIFUJI Hideaki struct rt6_info *rt; 18414bed72e4SYOSHIFUJI Hideaki unsigned long rt_expires; 18424bed72e4SYOSHIFUJI Hideaki 18431da177e4SLinus Torvalds /* Avoid arithmetic overflow. Really, we could 18446f704992SYOSHIFUJI Hideaki * save rt_expires in seconds, likely valid_lft, 18456f704992SYOSHIFUJI Hideaki * but it would require division in fib gc, that it 18466f704992SYOSHIFUJI Hideaki * not good. 18471da177e4SLinus Torvalds */ 18484bed72e4SYOSHIFUJI Hideaki if (HZ > USER_HZ) 18494bed72e4SYOSHIFUJI Hideaki rt_expires = addrconf_timeout_fixup(valid_lft, HZ); 18504bed72e4SYOSHIFUJI Hideaki else 18514bed72e4SYOSHIFUJI Hideaki rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ); 18523dd4bc68SYOSHIFUJI Hideaki 18534bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(rt_expires)) 18544bed72e4SYOSHIFUJI Hideaki rt_expires *= HZ; 18551da177e4SLinus Torvalds 185614ef37b6SAndreas Hofmeister rt = addrconf_get_prefix_route(&pinfo->prefix, 185714ef37b6SAndreas Hofmeister pinfo->prefix_len, 185814ef37b6SAndreas Hofmeister dev, 185914ef37b6SAndreas Hofmeister RTF_ADDRCONF | RTF_PREFIX_RT, 186014ef37b6SAndreas Hofmeister RTF_GATEWAY | RTF_DEFAULT); 18611da177e4SLinus Torvalds 186214ef37b6SAndreas Hofmeister if (rt) { 18636f704992SYOSHIFUJI Hideaki /* Autoconf prefix route */ 18641da177e4SLinus Torvalds if (valid_lft == 0) { 1865e0a1ad73SThomas Graf ip6_del_rt(rt); 18661da177e4SLinus Torvalds rt = NULL; 18674bed72e4SYOSHIFUJI Hideaki } else if (addrconf_finite_timeout(rt_expires)) { 18686f704992SYOSHIFUJI Hideaki /* not infinity */ 18691716a961SGao feng rt6_set_expires(rt, jiffies + rt_expires); 18706f704992SYOSHIFUJI Hideaki } else { 18711716a961SGao feng rt6_clean_expires(rt); 18721da177e4SLinus Torvalds } 18731da177e4SLinus Torvalds } else if (valid_lft) { 18746f704992SYOSHIFUJI Hideaki clock_t expires = 0; 18754bed72e4SYOSHIFUJI Hideaki int flags = RTF_ADDRCONF | RTF_PREFIX_RT; 18764bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(rt_expires)) { 18776f704992SYOSHIFUJI Hideaki /* not infinity */ 18786f704992SYOSHIFUJI Hideaki flags |= RTF_EXPIRES; 18796f704992SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(rt_expires); 18806f704992SYOSHIFUJI Hideaki } 18811da177e4SLinus Torvalds addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len, 18826f704992SYOSHIFUJI Hideaki dev, expires, flags); 18831da177e4SLinus Torvalds } 18841da177e4SLinus Torvalds if (rt) 1885d8d1f30bSChangli Gao dst_release(&rt->dst); 18861da177e4SLinus Torvalds } 18871da177e4SLinus Torvalds 18881da177e4SLinus Torvalds /* Try to figure out our local address for this prefix */ 18891da177e4SLinus Torvalds 18901da177e4SLinus Torvalds if (pinfo->autoconf && in6_dev->cnf.autoconf) { 18911da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 18921da177e4SLinus Torvalds struct in6_addr addr; 18931da177e4SLinus Torvalds int create = 0, update_lft = 0; 18941da177e4SLinus Torvalds 18951da177e4SLinus Torvalds if (pinfo->prefix_len == 64) { 18961da177e4SLinus Torvalds memcpy(&addr, &pinfo->prefix, 8); 18971da177e4SLinus Torvalds if (ipv6_generate_eui64(addr.s6_addr + 8, dev) && 18981da177e4SLinus Torvalds ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) { 18991da177e4SLinus Torvalds in6_dev_put(in6_dev); 19001da177e4SLinus Torvalds return; 19011da177e4SLinus Torvalds } 19021da177e4SLinus Torvalds goto ok; 19031da177e4SLinus Torvalds } 1904e87cc472SJoe Perches net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n", 19051da177e4SLinus Torvalds pinfo->prefix_len); 19061da177e4SLinus Torvalds in6_dev_put(in6_dev); 19071da177e4SLinus Torvalds return; 19081da177e4SLinus Torvalds 19091da177e4SLinus Torvalds ok: 19101da177e4SLinus Torvalds 191153b7997fSYOSHIFUJI Hideaki ifp = ipv6_get_ifaddr(net, &addr, dev, 1); 19121da177e4SLinus Torvalds 19131da177e4SLinus Torvalds if (ifp == NULL && valid_lft) { 19141da177e4SLinus Torvalds int max_addresses = in6_dev->cnf.max_addresses; 191595c385b4SNeil Horman u32 addr_flags = 0; 191695c385b4SNeil Horman 191795c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 191895c385b4SNeil Horman if (in6_dev->cnf.optimistic_dad && 1919e6bff995SNeil Horman !net->ipv6.devconf_all->forwarding && sllao) 192095c385b4SNeil Horman addr_flags = IFA_F_OPTIMISTIC; 192195c385b4SNeil Horman #endif 19221da177e4SLinus Torvalds 19231da177e4SLinus Torvalds /* Do not allow to create too much of autoconfigured 19241da177e4SLinus Torvalds * addresses; this would be too easy way to crash kernel. 19251da177e4SLinus Torvalds */ 19261da177e4SLinus Torvalds if (!max_addresses || 19271da177e4SLinus Torvalds ipv6_count_addresses(in6_dev) < max_addresses) 19281da177e4SLinus Torvalds ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len, 192995c385b4SNeil Horman addr_type&IPV6_ADDR_SCOPE_MASK, 193095c385b4SNeil Horman addr_flags); 19311da177e4SLinus Torvalds 19321da177e4SLinus Torvalds if (!ifp || IS_ERR(ifp)) { 19331da177e4SLinus Torvalds in6_dev_put(in6_dev); 19341da177e4SLinus Torvalds return; 19351da177e4SLinus Torvalds } 19361da177e4SLinus Torvalds 19371da177e4SLinus Torvalds update_lft = create = 1; 19381da177e4SLinus Torvalds ifp->cstamp = jiffies; 1939cf22f9a2SDavid S. Miller addrconf_dad_start(ifp); 19401da177e4SLinus Torvalds } 19411da177e4SLinus Torvalds 19421da177e4SLinus Torvalds if (ifp) { 19431da177e4SLinus Torvalds int flags; 19441da177e4SLinus Torvalds unsigned long now; 19451da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 19461da177e4SLinus Torvalds struct inet6_ifaddr *ift; 19471da177e4SLinus Torvalds #endif 19481da177e4SLinus Torvalds u32 stored_lft; 19491da177e4SLinus Torvalds 19501da177e4SLinus Torvalds /* update lifetime (RFC2462 5.5.3 e) */ 19511da177e4SLinus Torvalds spin_lock(&ifp->lock); 19521da177e4SLinus Torvalds now = jiffies; 19531da177e4SLinus Torvalds if (ifp->valid_lft > (now - ifp->tstamp) / HZ) 19541da177e4SLinus Torvalds stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ; 19551da177e4SLinus Torvalds else 19561da177e4SLinus Torvalds stored_lft = 0; 19571da177e4SLinus Torvalds if (!update_lft && stored_lft) { 19581da177e4SLinus Torvalds if (valid_lft > MIN_VALID_LIFETIME || 19591da177e4SLinus Torvalds valid_lft > stored_lft) 19601da177e4SLinus Torvalds update_lft = 1; 19611da177e4SLinus Torvalds else if (stored_lft <= MIN_VALID_LIFETIME) { 19621da177e4SLinus Torvalds /* valid_lft <= stored_lft is always true */ 1963a1ed0526SBrian Haley /* 1964a1ed0526SBrian Haley * RFC 4862 Section 5.5.3e: 1965a1ed0526SBrian Haley * "Note that the preferred lifetime of 1966a1ed0526SBrian Haley * the corresponding address is always 1967a1ed0526SBrian Haley * reset to the Preferred Lifetime in 1968a1ed0526SBrian Haley * the received Prefix Information 1969a1ed0526SBrian Haley * option, regardless of whether the 1970a1ed0526SBrian Haley * valid lifetime is also reset or 1971a1ed0526SBrian Haley * ignored." 1972a1ed0526SBrian Haley * 1973a1ed0526SBrian Haley * So if the preferred lifetime in 1974a1ed0526SBrian Haley * this advertisement is different 1975a1ed0526SBrian Haley * than what we have stored, but the 1976a1ed0526SBrian Haley * valid lifetime is invalid, just 1977a1ed0526SBrian Haley * reset prefered_lft. 1978a1ed0526SBrian Haley * 1979a1ed0526SBrian Haley * We must set the valid lifetime 1980a1ed0526SBrian Haley * to the stored lifetime since we'll 1981a1ed0526SBrian Haley * be updating the timestamp below, 1982a1ed0526SBrian Haley * else we'll set it back to the 198325985edcSLucas De Marchi * minimum. 1984a1ed0526SBrian Haley */ 1985a1ed0526SBrian Haley if (prefered_lft != ifp->prefered_lft) { 1986a1ed0526SBrian Haley valid_lft = stored_lft; 1987a1ed0526SBrian Haley update_lft = 1; 1988a1ed0526SBrian Haley } 19891da177e4SLinus Torvalds } else { 19901da177e4SLinus Torvalds valid_lft = MIN_VALID_LIFETIME; 19911da177e4SLinus Torvalds if (valid_lft < prefered_lft) 19921da177e4SLinus Torvalds prefered_lft = valid_lft; 19931da177e4SLinus Torvalds update_lft = 1; 19941da177e4SLinus Torvalds } 19951da177e4SLinus Torvalds } 19961da177e4SLinus Torvalds 19971da177e4SLinus Torvalds if (update_lft) { 19981da177e4SLinus Torvalds ifp->valid_lft = valid_lft; 19991da177e4SLinus Torvalds ifp->prefered_lft = prefered_lft; 20001da177e4SLinus Torvalds ifp->tstamp = now; 20011da177e4SLinus Torvalds flags = ifp->flags; 20021da177e4SLinus Torvalds ifp->flags &= ~IFA_F_DEPRECATED; 20031da177e4SLinus Torvalds spin_unlock(&ifp->lock); 20041da177e4SLinus Torvalds 20051da177e4SLinus Torvalds if (!(flags&IFA_F_TENTATIVE)) 20061da177e4SLinus Torvalds ipv6_ifa_notify(0, ifp); 20071da177e4SLinus Torvalds } else 20081da177e4SLinus Torvalds spin_unlock(&ifp->lock); 20091da177e4SLinus Torvalds 20101da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 20111da177e4SLinus Torvalds read_lock_bh(&in6_dev->lock); 20121da177e4SLinus Torvalds /* update all temporary addresses in the list */ 201376f793e3SLorenzo Colitti list_for_each_entry(ift, &in6_dev->tempaddr_list, 201476f793e3SLorenzo Colitti tmp_list) { 201576f793e3SLorenzo Colitti int age, max_valid, max_prefered; 201676f793e3SLorenzo Colitti 2017c6fbfac2SBenoit Boissinot if (ifp != ift->ifpub) 2018c6fbfac2SBenoit Boissinot continue; 2019c6fbfac2SBenoit Boissinot 202076f793e3SLorenzo Colitti /* 202176f793e3SLorenzo Colitti * RFC 4941 section 3.3: 202276f793e3SLorenzo Colitti * If a received option will extend the lifetime 202376f793e3SLorenzo Colitti * of a public address, the lifetimes of 202476f793e3SLorenzo Colitti * temporary addresses should be extended, 202576f793e3SLorenzo Colitti * subject to the overall constraint that no 202676f793e3SLorenzo Colitti * temporary addresses should ever remain 202776f793e3SLorenzo Colitti * "valid" or "preferred" for a time longer than 202876f793e3SLorenzo Colitti * (TEMP_VALID_LIFETIME) or 202976f793e3SLorenzo Colitti * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), 203076f793e3SLorenzo Colitti * respectively. 203176f793e3SLorenzo Colitti */ 203276f793e3SLorenzo Colitti age = (now - ift->cstamp) / HZ; 203376f793e3SLorenzo Colitti max_valid = in6_dev->cnf.temp_valid_lft - age; 203476f793e3SLorenzo Colitti if (max_valid < 0) 203576f793e3SLorenzo Colitti max_valid = 0; 203676f793e3SLorenzo Colitti 203776f793e3SLorenzo Colitti max_prefered = in6_dev->cnf.temp_prefered_lft - 203876f793e3SLorenzo Colitti in6_dev->cnf.max_desync_factor - 203976f793e3SLorenzo Colitti age; 204076f793e3SLorenzo Colitti if (max_prefered < 0) 204176f793e3SLorenzo Colitti max_prefered = 0; 204276f793e3SLorenzo Colitti 204376f793e3SLorenzo Colitti if (valid_lft > max_valid) 204476f793e3SLorenzo Colitti valid_lft = max_valid; 204576f793e3SLorenzo Colitti 204676f793e3SLorenzo Colitti if (prefered_lft > max_prefered) 204776f793e3SLorenzo Colitti prefered_lft = max_prefered; 204876f793e3SLorenzo Colitti 20491da177e4SLinus Torvalds spin_lock(&ift->lock); 20501da177e4SLinus Torvalds flags = ift->flags; 205176f793e3SLorenzo Colitti ift->valid_lft = valid_lft; 205276f793e3SLorenzo Colitti ift->prefered_lft = prefered_lft; 205376f793e3SLorenzo Colitti ift->tstamp = now; 205476f793e3SLorenzo Colitti if (prefered_lft > 0) 205576f793e3SLorenzo Colitti ift->flags &= ~IFA_F_DEPRECATED; 205676f793e3SLorenzo Colitti 20571da177e4SLinus Torvalds spin_unlock(&ift->lock); 20581da177e4SLinus Torvalds if (!(flags&IFA_F_TENTATIVE)) 20591da177e4SLinus Torvalds ipv6_ifa_notify(0, ift); 20601da177e4SLinus Torvalds } 20611da177e4SLinus Torvalds 2062aed65501SGlenn Wurster if ((create || list_empty(&in6_dev->tempaddr_list)) && in6_dev->cnf.use_tempaddr > 0) { 20631da177e4SLinus Torvalds /* 206476f793e3SLorenzo Colitti * When a new public address is created as 206576f793e3SLorenzo Colitti * described in [ADDRCONF], also create a new 206676f793e3SLorenzo Colitti * temporary address. Also create a temporary 206776f793e3SLorenzo Colitti * address if it's enabled but no temporary 206876f793e3SLorenzo Colitti * address currently exists. 20691da177e4SLinus Torvalds */ 20701da177e4SLinus Torvalds read_unlock_bh(&in6_dev->lock); 20711da177e4SLinus Torvalds ipv6_create_tempaddr(ifp, NULL); 20721da177e4SLinus Torvalds } else { 20731da177e4SLinus Torvalds read_unlock_bh(&in6_dev->lock); 20741da177e4SLinus Torvalds } 20751da177e4SLinus Torvalds #endif 20761da177e4SLinus Torvalds in6_ifa_put(ifp); 20771da177e4SLinus Torvalds addrconf_verify(0); 20781da177e4SLinus Torvalds } 20791da177e4SLinus Torvalds } 20801da177e4SLinus Torvalds inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo); 20811da177e4SLinus Torvalds in6_dev_put(in6_dev); 20821da177e4SLinus Torvalds } 20831da177e4SLinus Torvalds 20841da177e4SLinus Torvalds /* 20851da177e4SLinus Torvalds * Set destination address. 20861da177e4SLinus Torvalds * Special case for SIT interfaces where we create a new "virtual" 20871da177e4SLinus Torvalds * device. 20881da177e4SLinus Torvalds */ 2089af284937SDaniel Lezcano int addrconf_set_dstaddr(struct net *net, void __user *arg) 20901da177e4SLinus Torvalds { 20911da177e4SLinus Torvalds struct in6_ifreq ireq; 20921da177e4SLinus Torvalds struct net_device *dev; 20931da177e4SLinus Torvalds int err = -EINVAL; 20941da177e4SLinus Torvalds 20951da177e4SLinus Torvalds rtnl_lock(); 20961da177e4SLinus Torvalds 20971da177e4SLinus Torvalds err = -EFAULT; 20981da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 20991da177e4SLinus Torvalds goto err_exit; 21001da177e4SLinus Torvalds 2101af284937SDaniel Lezcano dev = __dev_get_by_index(net, ireq.ifr6_ifindex); 21021da177e4SLinus Torvalds 21031da177e4SLinus Torvalds err = -ENODEV; 21041da177e4SLinus Torvalds if (dev == NULL) 21051da177e4SLinus Torvalds goto err_exit; 21061da177e4SLinus Torvalds 21070be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 21081da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT) { 21095bc3eb7eSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 21101da177e4SLinus Torvalds struct ifreq ifr; 21111da177e4SLinus Torvalds struct ip_tunnel_parm p; 21121da177e4SLinus Torvalds 21131da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 21141da177e4SLinus Torvalds if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4)) 21151da177e4SLinus Torvalds goto err_exit; 21161da177e4SLinus Torvalds 21171da177e4SLinus Torvalds memset(&p, 0, sizeof(p)); 21181da177e4SLinus Torvalds p.iph.daddr = ireq.ifr6_addr.s6_addr32[3]; 21191da177e4SLinus Torvalds p.iph.saddr = 0; 21201da177e4SLinus Torvalds p.iph.version = 4; 21211da177e4SLinus Torvalds p.iph.ihl = 5; 21221da177e4SLinus Torvalds p.iph.protocol = IPPROTO_IPV6; 21231da177e4SLinus Torvalds p.iph.ttl = 64; 2124d20b3109SStephen Hemminger ifr.ifr_ifru.ifru_data = (__force void __user *)&p; 21251da177e4SLinus Torvalds 21265bc3eb7eSStephen Hemminger if (ops->ndo_do_ioctl) { 21275bc3eb7eSStephen Hemminger mm_segment_t oldfs = get_fs(); 21285bc3eb7eSStephen Hemminger 21295bc3eb7eSStephen Hemminger set_fs(KERNEL_DS); 21305bc3eb7eSStephen Hemminger err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL); 21311da177e4SLinus Torvalds set_fs(oldfs); 21325bc3eb7eSStephen Hemminger } else 21335bc3eb7eSStephen Hemminger err = -EOPNOTSUPP; 21341da177e4SLinus Torvalds 21351da177e4SLinus Torvalds if (err == 0) { 21361da177e4SLinus Torvalds err = -ENOBUFS; 2137af284937SDaniel Lezcano dev = __dev_get_by_name(net, p.name); 2138af284937SDaniel Lezcano if (!dev) 21391da177e4SLinus Torvalds goto err_exit; 21401da177e4SLinus Torvalds err = dev_open(dev); 21411da177e4SLinus Torvalds } 21421da177e4SLinus Torvalds } 21430be669bbSJoerg Roedel #endif 21441da177e4SLinus Torvalds 21451da177e4SLinus Torvalds err_exit: 21461da177e4SLinus Torvalds rtnl_unlock(); 21471da177e4SLinus Torvalds return err; 21481da177e4SLinus Torvalds } 21491da177e4SLinus Torvalds 21501da177e4SLinus Torvalds /* 21511da177e4SLinus Torvalds * Manual configuration of address on an interface 21521da177e4SLinus Torvalds */ 2153b71d1d42SEric Dumazet static int inet6_addr_add(struct net *net, int ifindex, const struct in6_addr *pfx, 215424ef0da7SThomas Graf unsigned int plen, __u8 ifa_flags, __u32 prefered_lft, 2155af284937SDaniel Lezcano __u32 valid_lft) 21561da177e4SLinus Torvalds { 21571da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 21581da177e4SLinus Torvalds struct inet6_dev *idev; 21591da177e4SLinus Torvalds struct net_device *dev; 21601da177e4SLinus Torvalds int scope; 21616f704992SYOSHIFUJI Hideaki u32 flags; 21626f704992SYOSHIFUJI Hideaki clock_t expires; 21634bed72e4SYOSHIFUJI Hideaki unsigned long timeout; 21641da177e4SLinus Torvalds 21651da177e4SLinus Torvalds ASSERT_RTNL(); 21661da177e4SLinus Torvalds 216724ef0da7SThomas Graf if (plen > 128) 216824ef0da7SThomas Graf return -EINVAL; 216924ef0da7SThomas Graf 21700778769dSNoriaki TAKAMIYA /* check the lifetime */ 21710778769dSNoriaki TAKAMIYA if (!valid_lft || prefered_lft > valid_lft) 21720778769dSNoriaki TAKAMIYA return -EINVAL; 21730778769dSNoriaki TAKAMIYA 2174af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifindex); 2175af284937SDaniel Lezcano if (!dev) 21761da177e4SLinus Torvalds return -ENODEV; 21771da177e4SLinus Torvalds 217864e724f6SBrian Haley idev = addrconf_add_dev(dev); 217964e724f6SBrian Haley if (IS_ERR(idev)) 218064e724f6SBrian Haley return PTR_ERR(idev); 21811da177e4SLinus Torvalds 21821da177e4SLinus Torvalds scope = ipv6_addr_scope(pfx); 21831da177e4SLinus Torvalds 21844bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(valid_lft, HZ); 21854bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 21864bed72e4SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(timeout * HZ); 21874bed72e4SYOSHIFUJI Hideaki valid_lft = timeout; 21886f704992SYOSHIFUJI Hideaki flags = RTF_EXPIRES; 21894bed72e4SYOSHIFUJI Hideaki } else { 21904bed72e4SYOSHIFUJI Hideaki expires = 0; 21914bed72e4SYOSHIFUJI Hideaki flags = 0; 21924bed72e4SYOSHIFUJI Hideaki ifa_flags |= IFA_F_PERMANENT; 21936f704992SYOSHIFUJI Hideaki } 21940778769dSNoriaki TAKAMIYA 21954bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(prefered_lft, HZ); 21964bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 21974bed72e4SYOSHIFUJI Hideaki if (timeout == 0) 21980778769dSNoriaki TAKAMIYA ifa_flags |= IFA_F_DEPRECATED; 21994bed72e4SYOSHIFUJI Hideaki prefered_lft = timeout; 22004bed72e4SYOSHIFUJI Hideaki } 22010778769dSNoriaki TAKAMIYA 22020778769dSNoriaki TAKAMIYA ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags); 22030778769dSNoriaki TAKAMIYA 22041da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 220506aebfb7SHerbert Xu spin_lock_bh(&ifp->lock); 22060778769dSNoriaki TAKAMIYA ifp->valid_lft = valid_lft; 22070778769dSNoriaki TAKAMIYA ifp->prefered_lft = prefered_lft; 22080778769dSNoriaki TAKAMIYA ifp->tstamp = jiffies; 220906aebfb7SHerbert Xu spin_unlock_bh(&ifp->lock); 22100778769dSNoriaki TAKAMIYA 221146d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 22126f704992SYOSHIFUJI Hideaki expires, flags); 221395c385b4SNeil Horman /* 221495c385b4SNeil Horman * Note that section 3.1 of RFC 4429 indicates 221595c385b4SNeil Horman * that the Optimistic flag should not be set for 221695c385b4SNeil Horman * manually configured addresses 221795c385b4SNeil Horman */ 2218cf22f9a2SDavid S. Miller addrconf_dad_start(ifp); 22191da177e4SLinus Torvalds in6_ifa_put(ifp); 22200778769dSNoriaki TAKAMIYA addrconf_verify(0); 22211da177e4SLinus Torvalds return 0; 22221da177e4SLinus Torvalds } 22231da177e4SLinus Torvalds 22241da177e4SLinus Torvalds return PTR_ERR(ifp); 22251da177e4SLinus Torvalds } 22261da177e4SLinus Torvalds 2227b71d1d42SEric Dumazet static int inet6_addr_del(struct net *net, int ifindex, const struct in6_addr *pfx, 222824ef0da7SThomas Graf unsigned int plen) 22291da177e4SLinus Torvalds { 22301da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 22311da177e4SLinus Torvalds struct inet6_dev *idev; 22321da177e4SLinus Torvalds struct net_device *dev; 22331da177e4SLinus Torvalds 223424ef0da7SThomas Graf if (plen > 128) 223524ef0da7SThomas Graf return -EINVAL; 223624ef0da7SThomas Graf 2237af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifindex); 2238af284937SDaniel Lezcano if (!dev) 22391da177e4SLinus Torvalds return -ENODEV; 22401da177e4SLinus Torvalds 22411da177e4SLinus Torvalds if ((idev = __in6_dev_get(dev)) == NULL) 22421da177e4SLinus Torvalds return -ENXIO; 22431da177e4SLinus Torvalds 22441da177e4SLinus Torvalds read_lock_bh(&idev->lock); 2245502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 22461da177e4SLinus Torvalds if (ifp->prefix_len == plen && 22471da177e4SLinus Torvalds ipv6_addr_equal(pfx, &ifp->addr)) { 22481da177e4SLinus Torvalds in6_ifa_hold(ifp); 22491da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 22501da177e4SLinus Torvalds 22511da177e4SLinus Torvalds ipv6_del_addr(ifp); 22521da177e4SLinus Torvalds 22531da177e4SLinus Torvalds /* If the last address is deleted administratively, 22541da177e4SLinus Torvalds disable IPv6 on this interface. 22551da177e4SLinus Torvalds */ 2256502a2ffdSstephen hemminger if (list_empty(&idev->addr_list)) 22571da177e4SLinus Torvalds addrconf_ifdown(idev->dev, 1); 22581da177e4SLinus Torvalds return 0; 22591da177e4SLinus Torvalds } 22601da177e4SLinus Torvalds } 22611da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 22621da177e4SLinus Torvalds return -EADDRNOTAVAIL; 22631da177e4SLinus Torvalds } 22641da177e4SLinus Torvalds 22651da177e4SLinus Torvalds 2266af284937SDaniel Lezcano int addrconf_add_ifaddr(struct net *net, void __user *arg) 22671da177e4SLinus Torvalds { 22681da177e4SLinus Torvalds struct in6_ifreq ireq; 22691da177e4SLinus Torvalds int err; 22701da177e4SLinus Torvalds 22711da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 22721da177e4SLinus Torvalds return -EPERM; 22731da177e4SLinus Torvalds 22741da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 22751da177e4SLinus Torvalds return -EFAULT; 22761da177e4SLinus Torvalds 22771da177e4SLinus Torvalds rtnl_lock(); 2278af284937SDaniel Lezcano err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, 2279af284937SDaniel Lezcano ireq.ifr6_prefixlen, IFA_F_PERMANENT, 2280af284937SDaniel Lezcano INFINITY_LIFE_TIME, INFINITY_LIFE_TIME); 22811da177e4SLinus Torvalds rtnl_unlock(); 22821da177e4SLinus Torvalds return err; 22831da177e4SLinus Torvalds } 22841da177e4SLinus Torvalds 2285af284937SDaniel Lezcano int addrconf_del_ifaddr(struct net *net, void __user *arg) 22861da177e4SLinus Torvalds { 22871da177e4SLinus Torvalds struct in6_ifreq ireq; 22881da177e4SLinus Torvalds int err; 22891da177e4SLinus Torvalds 22901da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 22911da177e4SLinus Torvalds return -EPERM; 22921da177e4SLinus Torvalds 22931da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 22941da177e4SLinus Torvalds return -EFAULT; 22951da177e4SLinus Torvalds 22961da177e4SLinus Torvalds rtnl_lock(); 2297af284937SDaniel Lezcano err = inet6_addr_del(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, 2298af284937SDaniel Lezcano ireq.ifr6_prefixlen); 22991da177e4SLinus Torvalds rtnl_unlock(); 23001da177e4SLinus Torvalds return err; 23011da177e4SLinus Torvalds } 23021da177e4SLinus Torvalds 2303b5f348e5SIlpo Järvinen static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr, 2304b5f348e5SIlpo Järvinen int plen, int scope) 2305b5f348e5SIlpo Järvinen { 2306b5f348e5SIlpo Järvinen struct inet6_ifaddr *ifp; 2307b5f348e5SIlpo Järvinen 2308b5f348e5SIlpo Järvinen ifp = ipv6_add_addr(idev, addr, plen, scope, IFA_F_PERMANENT); 2309b5f348e5SIlpo Järvinen if (!IS_ERR(ifp)) { 2310b5f348e5SIlpo Järvinen spin_lock_bh(&ifp->lock); 2311b5f348e5SIlpo Järvinen ifp->flags &= ~IFA_F_TENTATIVE; 2312b5f348e5SIlpo Järvinen spin_unlock_bh(&ifp->lock); 2313b5f348e5SIlpo Järvinen ipv6_ifa_notify(RTM_NEWADDR, ifp); 2314b5f348e5SIlpo Järvinen in6_ifa_put(ifp); 2315b5f348e5SIlpo Järvinen } 2316b5f348e5SIlpo Järvinen } 2317b5f348e5SIlpo Järvinen 23180be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 23191da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev) 23201da177e4SLinus Torvalds { 23211da177e4SLinus Torvalds struct in6_addr addr; 23221da177e4SLinus Torvalds struct net_device *dev; 2323c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 23241da177e4SLinus Torvalds int scope; 23251da177e4SLinus Torvalds 23261da177e4SLinus Torvalds ASSERT_RTNL(); 23271da177e4SLinus Torvalds 23281da177e4SLinus Torvalds memset(&addr, 0, sizeof(struct in6_addr)); 23291da177e4SLinus Torvalds memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4); 23301da177e4SLinus Torvalds 23311da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) { 23321da177e4SLinus Torvalds addr.s6_addr32[0] = htonl(0xfe800000); 23331da177e4SLinus Torvalds scope = IFA_LINK; 23341da177e4SLinus Torvalds } else { 23351da177e4SLinus Torvalds scope = IPV6_ADDR_COMPATv4; 23361da177e4SLinus Torvalds } 23371da177e4SLinus Torvalds 23381da177e4SLinus Torvalds if (addr.s6_addr32[3]) { 2339b5f348e5SIlpo Järvinen add_addr(idev, &addr, 128, scope); 23401da177e4SLinus Torvalds return; 23411da177e4SLinus Torvalds } 23421da177e4SLinus Torvalds 23436fda7350SBenjamin Thery for_each_netdev(net, dev) { 2344e5ed6399SHerbert Xu struct in_device *in_dev = __in_dev_get_rtnl(dev); 23451da177e4SLinus Torvalds if (in_dev && (dev->flags & IFF_UP)) { 23461da177e4SLinus Torvalds struct in_ifaddr *ifa; 23471da177e4SLinus Torvalds 23481da177e4SLinus Torvalds int flag = scope; 23491da177e4SLinus Torvalds 23501da177e4SLinus Torvalds for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) { 23511da177e4SLinus Torvalds int plen; 23521da177e4SLinus Torvalds 23531da177e4SLinus Torvalds addr.s6_addr32[3] = ifa->ifa_local; 23541da177e4SLinus Torvalds 23551da177e4SLinus Torvalds if (ifa->ifa_scope == RT_SCOPE_LINK) 23561da177e4SLinus Torvalds continue; 23571da177e4SLinus Torvalds if (ifa->ifa_scope >= RT_SCOPE_HOST) { 23581da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) 23591da177e4SLinus Torvalds continue; 23601da177e4SLinus Torvalds flag |= IFA_HOST; 23611da177e4SLinus Torvalds } 23621da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) 23631da177e4SLinus Torvalds plen = 64; 23641da177e4SLinus Torvalds else 23651da177e4SLinus Torvalds plen = 96; 23661da177e4SLinus Torvalds 2367b5f348e5SIlpo Järvinen add_addr(idev, &addr, plen, flag); 23681da177e4SLinus Torvalds } 23691da177e4SLinus Torvalds } 23701da177e4SLinus Torvalds } 23711da177e4SLinus Torvalds } 23720be669bbSJoerg Roedel #endif 23731da177e4SLinus Torvalds 23741da177e4SLinus Torvalds static void init_loopback(struct net_device *dev) 23751da177e4SLinus Torvalds { 23761da177e4SLinus Torvalds struct inet6_dev *idev; 23771da177e4SLinus Torvalds 23781da177e4SLinus Torvalds /* ::1 */ 23791da177e4SLinus Torvalds 23801da177e4SLinus Torvalds ASSERT_RTNL(); 23811da177e4SLinus Torvalds 23821da177e4SLinus Torvalds if ((idev = ipv6_find_idev(dev)) == NULL) { 238391df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 23841da177e4SLinus Torvalds return; 23851da177e4SLinus Torvalds } 23861da177e4SLinus Torvalds 2387b5f348e5SIlpo Järvinen add_addr(idev, &in6addr_loopback, 128, IFA_HOST); 23881da177e4SLinus Torvalds } 23891da177e4SLinus Torvalds 2390b71d1d42SEric Dumazet static void addrconf_add_linklocal(struct inet6_dev *idev, const struct in6_addr *addr) 23911da177e4SLinus Torvalds { 23921da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 239395c385b4SNeil Horman u32 addr_flags = IFA_F_PERMANENT; 23941da177e4SLinus Torvalds 239595c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 239695c385b4SNeil Horman if (idev->cnf.optimistic_dad && 2397702beb87SDavid Miller !dev_net(idev->dev)->ipv6.devconf_all->forwarding) 239895c385b4SNeil Horman addr_flags |= IFA_F_OPTIMISTIC; 239995c385b4SNeil Horman #endif 240095c385b4SNeil Horman 240195c385b4SNeil Horman 240295c385b4SNeil Horman ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, addr_flags); 24031da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 240446d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0); 2405cf22f9a2SDavid S. Miller addrconf_dad_start(ifp); 24061da177e4SLinus Torvalds in6_ifa_put(ifp); 24071da177e4SLinus Torvalds } 24081da177e4SLinus Torvalds } 24091da177e4SLinus Torvalds 24101da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev) 24111da177e4SLinus Torvalds { 24121da177e4SLinus Torvalds struct in6_addr addr; 24131da177e4SLinus Torvalds struct inet6_dev *idev; 24141da177e4SLinus Torvalds 24151da177e4SLinus Torvalds ASSERT_RTNL(); 24161da177e4SLinus Torvalds 241774235a25SHerbert Xu if ((dev->type != ARPHRD_ETHER) && 241874235a25SHerbert Xu (dev->type != ARPHRD_FDDI) && 241974235a25SHerbert Xu (dev->type != ARPHRD_ARCNET) && 242006a4c1c5Salex.bluesman.smirnov@gmail.com (dev->type != ARPHRD_INFINIBAND) && 242106a4c1c5Salex.bluesman.smirnov@gmail.com (dev->type != ARPHRD_IEEE802154)) { 242274235a25SHerbert Xu /* Alas, we support only Ethernet autoconfiguration. */ 242374235a25SHerbert Xu return; 242474235a25SHerbert Xu } 242574235a25SHerbert Xu 24261da177e4SLinus Torvalds idev = addrconf_add_dev(dev); 242764e724f6SBrian Haley if (IS_ERR(idev)) 24281da177e4SLinus Torvalds return; 24291da177e4SLinus Torvalds 24301da177e4SLinus Torvalds memset(&addr, 0, sizeof(struct in6_addr)); 24311da177e4SLinus Torvalds addr.s6_addr32[0] = htonl(0xFE800000); 24321da177e4SLinus Torvalds 24331da177e4SLinus Torvalds if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0) 24341da177e4SLinus Torvalds addrconf_add_linklocal(idev, &addr); 24351da177e4SLinus Torvalds } 24361da177e4SLinus Torvalds 24370be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 24381da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev) 24391da177e4SLinus Torvalds { 24401da177e4SLinus Torvalds struct inet6_dev *idev; 24411da177e4SLinus Torvalds 24421da177e4SLinus Torvalds ASSERT_RTNL(); 24431da177e4SLinus Torvalds 24441da177e4SLinus Torvalds /* 24451da177e4SLinus Torvalds * Configure the tunnel with one of our IPv4 24461da177e4SLinus Torvalds * addresses... we should configure all of 24471da177e4SLinus Torvalds * our v4 addrs in the tunnel 24481da177e4SLinus Torvalds */ 24491da177e4SLinus Torvalds 24501da177e4SLinus Torvalds if ((idev = ipv6_find_idev(dev)) == NULL) { 245191df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 24521da177e4SLinus Torvalds return; 24531da177e4SLinus Torvalds } 24541da177e4SLinus Torvalds 2455c7dc89c0SFred L. Templin if (dev->priv_flags & IFF_ISATAP) { 2456c7dc89c0SFred L. Templin struct in6_addr addr; 2457c7dc89c0SFred L. Templin 2458c7dc89c0SFred L. Templin ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0); 2459c7dc89c0SFred L. Templin addrconf_prefix_route(&addr, 64, dev, 0, 0); 2460c7dc89c0SFred L. Templin if (!ipv6_generate_eui64(addr.s6_addr + 8, dev)) 2461c7dc89c0SFred L. Templin addrconf_add_linklocal(idev, &addr); 2462c7dc89c0SFred L. Templin return; 2463c7dc89c0SFred L. Templin } 2464c7dc89c0SFred L. Templin 24651da177e4SLinus Torvalds sit_add_v4_addrs(idev); 24661da177e4SLinus Torvalds 246762b54dd9SNicolas Dichtel if (dev->flags&IFF_POINTOPOINT) 24681da177e4SLinus Torvalds addrconf_add_mroute(dev); 246962b54dd9SNicolas Dichtel else 24701da177e4SLinus Torvalds sit_route_add(dev); 24711da177e4SLinus Torvalds } 24720be669bbSJoerg Roedel #endif 24731da177e4SLinus Torvalds 2474aee80b54Sstephen hemminger #if defined(CONFIG_NET_IPGRE) || defined(CONFIG_NET_IPGRE_MODULE) 2475aee80b54Sstephen hemminger static void addrconf_gre_config(struct net_device *dev) 2476aee80b54Sstephen hemminger { 2477aee80b54Sstephen hemminger struct inet6_dev *idev; 2478aee80b54Sstephen hemminger struct in6_addr addr; 2479aee80b54Sstephen hemminger 2480f3213831SJoe Perches pr_info("%s(%s)\n", __func__, dev->name); 2481aee80b54Sstephen hemminger 2482aee80b54Sstephen hemminger ASSERT_RTNL(); 2483aee80b54Sstephen hemminger 2484aee80b54Sstephen hemminger if ((idev = ipv6_find_idev(dev)) == NULL) { 248591df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 2486aee80b54Sstephen hemminger return; 2487aee80b54Sstephen hemminger } 2488aee80b54Sstephen hemminger 2489aee80b54Sstephen hemminger ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0); 2490aee80b54Sstephen hemminger addrconf_prefix_route(&addr, 64, dev, 0, 0); 2491aee80b54Sstephen hemminger 2492aee80b54Sstephen hemminger if (!ipv6_generate_eui64(addr.s6_addr + 8, dev)) 2493aee80b54Sstephen hemminger addrconf_add_linklocal(idev, &addr); 2494aee80b54Sstephen hemminger } 2495aee80b54Sstephen hemminger #endif 2496aee80b54Sstephen hemminger 24971da177e4SLinus Torvalds static inline int 24981da177e4SLinus Torvalds ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev) 24991da177e4SLinus Torvalds { 25001da177e4SLinus Torvalds struct in6_addr lladdr; 25011da177e4SLinus Torvalds 250295c385b4SNeil Horman if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) { 25031da177e4SLinus Torvalds addrconf_add_linklocal(idev, &lladdr); 25041da177e4SLinus Torvalds return 0; 25051da177e4SLinus Torvalds } 25061da177e4SLinus Torvalds return -1; 25071da177e4SLinus Torvalds } 25081da177e4SLinus Torvalds 25091da177e4SLinus Torvalds static void ip6_tnl_add_linklocal(struct inet6_dev *idev) 25101da177e4SLinus Torvalds { 25111da177e4SLinus Torvalds struct net_device *link_dev; 2512c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 25131da177e4SLinus Torvalds 25141da177e4SLinus Torvalds /* first try to inherit the link-local address from the link device */ 25151da177e4SLinus Torvalds if (idev->dev->iflink && 25166fda7350SBenjamin Thery (link_dev = __dev_get_by_index(net, idev->dev->iflink))) { 25171da177e4SLinus Torvalds if (!ipv6_inherit_linklocal(idev, link_dev)) 25181da177e4SLinus Torvalds return; 25191da177e4SLinus Torvalds } 25201da177e4SLinus Torvalds /* then try to inherit it from any device */ 25216fda7350SBenjamin Thery for_each_netdev(net, link_dev) { 25221da177e4SLinus Torvalds if (!ipv6_inherit_linklocal(idev, link_dev)) 25231da177e4SLinus Torvalds return; 25241da177e4SLinus Torvalds } 252591df42beSJoe Perches pr_debug("init ip6-ip6: add_linklocal failed\n"); 25261da177e4SLinus Torvalds } 25271da177e4SLinus Torvalds 25281da177e4SLinus Torvalds /* 25291da177e4SLinus Torvalds * Autoconfigure tunnel with a link-local address so routing protocols, 25301da177e4SLinus Torvalds * DHCPv6, MLD etc. can be run over the virtual link 25311da177e4SLinus Torvalds */ 25321da177e4SLinus Torvalds 25331da177e4SLinus Torvalds static void addrconf_ip6_tnl_config(struct net_device *dev) 25341da177e4SLinus Torvalds { 25351da177e4SLinus Torvalds struct inet6_dev *idev; 25361da177e4SLinus Torvalds 25371da177e4SLinus Torvalds ASSERT_RTNL(); 25381da177e4SLinus Torvalds 2539e21e8467SStephen Hemminger idev = addrconf_add_dev(dev); 254064e724f6SBrian Haley if (IS_ERR(idev)) { 254191df42beSJoe Perches pr_debug("init ip6-ip6: add_dev failed\n"); 25421da177e4SLinus Torvalds return; 25431da177e4SLinus Torvalds } 25441da177e4SLinus Torvalds ip6_tnl_add_linklocal(idev); 25451da177e4SLinus Torvalds } 25461da177e4SLinus Torvalds 25471da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event, 25481da177e4SLinus Torvalds void *data) 25491da177e4SLinus Torvalds { 25501da177e4SLinus Torvalds struct net_device *dev = (struct net_device *) data; 2551748e2d93SEric Dumazet struct inet6_dev *idev = __in6_dev_get(dev); 2552c5e33bddSYOSHIFUJI Hideaki int run_pending = 0; 2553b217d616SHerbert Xu int err; 25541da177e4SLinus Torvalds 25551da177e4SLinus Torvalds switch (event) { 255645ba9dd2SYOSHIFUJI Hideaki case NETDEV_REGISTER: 255774235a25SHerbert Xu if (!idev && dev->mtu >= IPV6_MIN_MTU) { 255845ba9dd2SYOSHIFUJI Hideaki idev = ipv6_add_dev(dev); 255945ba9dd2SYOSHIFUJI Hideaki if (!idev) 2560b217d616SHerbert Xu return notifier_from_errno(-ENOMEM); 256145ba9dd2SYOSHIFUJI Hideaki } 256245ba9dd2SYOSHIFUJI Hideaki break; 2563bcdd553fSStephen Hemminger 25641da177e4SLinus Torvalds case NETDEV_UP: 25653c21edbdSYOSHIFUJI Hideaki case NETDEV_CHANGE: 2566c2edacf8SJay Vosburgh if (dev->flags & IFF_SLAVE) 2567c2edacf8SJay Vosburgh break; 2568c2edacf8SJay Vosburgh 25693c21edbdSYOSHIFUJI Hideaki if (event == NETDEV_UP) { 2570f24e3d65SMitsuru Chinen if (!addrconf_qdisc_ok(dev)) { 25713c21edbdSYOSHIFUJI Hideaki /* device is not ready yet. */ 2572f3213831SJoe Perches pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n", 25733c21edbdSYOSHIFUJI Hideaki dev->name); 25743c21edbdSYOSHIFUJI Hideaki break; 25753c21edbdSYOSHIFUJI Hideaki } 257699081049SKristian Slavov 2577d31c7b8fSEvgeniy Polyakov if (!idev && dev->mtu >= IPV6_MIN_MTU) 2578d31c7b8fSEvgeniy Polyakov idev = ipv6_add_dev(dev); 2579d31c7b8fSEvgeniy Polyakov 2580e3ec6cfcSBenjamin Thery if (idev) { 258199081049SKristian Slavov idev->if_flags |= IF_READY; 2582e3ec6cfcSBenjamin Thery run_pending = 1; 2583e3ec6cfcSBenjamin Thery } 25843c21edbdSYOSHIFUJI Hideaki } else { 2585f24e3d65SMitsuru Chinen if (!addrconf_qdisc_ok(dev)) { 25863c21edbdSYOSHIFUJI Hideaki /* device is still not ready. */ 25873c21edbdSYOSHIFUJI Hideaki break; 25883c21edbdSYOSHIFUJI Hideaki } 25893c21edbdSYOSHIFUJI Hideaki 25903c21edbdSYOSHIFUJI Hideaki if (idev) { 2591bcdd553fSStephen Hemminger if (idev->if_flags & IF_READY) 25923c21edbdSYOSHIFUJI Hideaki /* device is already configured. */ 25933c21edbdSYOSHIFUJI Hideaki break; 25943c21edbdSYOSHIFUJI Hideaki idev->if_flags |= IF_READY; 25953c21edbdSYOSHIFUJI Hideaki } 25963c21edbdSYOSHIFUJI Hideaki 2597f3213831SJoe Perches pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n", 25983c21edbdSYOSHIFUJI Hideaki dev->name); 25993c21edbdSYOSHIFUJI Hideaki 2600c5e33bddSYOSHIFUJI Hideaki run_pending = 1; 26013c21edbdSYOSHIFUJI Hideaki } 26023c21edbdSYOSHIFUJI Hideaki 26031da177e4SLinus Torvalds switch (dev->type) { 26040be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 26051da177e4SLinus Torvalds case ARPHRD_SIT: 26061da177e4SLinus Torvalds addrconf_sit_config(dev); 26071da177e4SLinus Torvalds break; 26080be669bbSJoerg Roedel #endif 2609aee80b54Sstephen hemminger #if defined(CONFIG_NET_IPGRE) || defined(CONFIG_NET_IPGRE_MODULE) 2610aee80b54Sstephen hemminger case ARPHRD_IPGRE: 2611aee80b54Sstephen hemminger addrconf_gre_config(dev); 2612aee80b54Sstephen hemminger break; 2613aee80b54Sstephen hemminger #endif 26141da177e4SLinus Torvalds case ARPHRD_TUNNEL6: 26151da177e4SLinus Torvalds addrconf_ip6_tnl_config(dev); 26161da177e4SLinus Torvalds break; 26171da177e4SLinus Torvalds case ARPHRD_LOOPBACK: 26181da177e4SLinus Torvalds init_loopback(dev); 26191da177e4SLinus Torvalds break; 26201da177e4SLinus Torvalds 26211da177e4SLinus Torvalds default: 26221da177e4SLinus Torvalds addrconf_dev_config(dev); 26231da177e4SLinus Torvalds break; 26243ff50b79SStephen Hemminger } 2625bcdd553fSStephen Hemminger 26261da177e4SLinus Torvalds if (idev) { 2627c5e33bddSYOSHIFUJI Hideaki if (run_pending) 2628c5e33bddSYOSHIFUJI Hideaki addrconf_dad_run(idev); 2629c5e33bddSYOSHIFUJI Hideaki 2630bcdd553fSStephen Hemminger /* 2631bcdd553fSStephen Hemminger * If the MTU changed during the interface down, 2632bcdd553fSStephen Hemminger * when the interface up, the changed MTU must be 2633bcdd553fSStephen Hemminger * reflected in the idev as well as routers. 26341da177e4SLinus Torvalds */ 2635bcdd553fSStephen Hemminger if (idev->cnf.mtu6 != dev->mtu && 2636bcdd553fSStephen Hemminger dev->mtu >= IPV6_MIN_MTU) { 26371da177e4SLinus Torvalds rt6_mtu_change(dev, dev->mtu); 26381da177e4SLinus Torvalds idev->cnf.mtu6 = dev->mtu; 26391da177e4SLinus Torvalds } 26401da177e4SLinus Torvalds idev->tstamp = jiffies; 26411da177e4SLinus Torvalds inet6_ifinfo_notify(RTM_NEWLINK, idev); 2642bcdd553fSStephen Hemminger 2643bcdd553fSStephen Hemminger /* 2644bcdd553fSStephen Hemminger * If the changed mtu during down is lower than 2645bcdd553fSStephen Hemminger * IPV6_MIN_MTU stop IPv6 on this interface. 26461da177e4SLinus Torvalds */ 26471da177e4SLinus Torvalds if (dev->mtu < IPV6_MIN_MTU) 2648bcdd553fSStephen Hemminger addrconf_ifdown(dev, 1); 26491da177e4SLinus Torvalds } 26501da177e4SLinus Torvalds break; 26511da177e4SLinus Torvalds 26521da177e4SLinus Torvalds case NETDEV_CHANGEMTU: 26531da177e4SLinus Torvalds if (idev && dev->mtu >= IPV6_MIN_MTU) { 26541da177e4SLinus Torvalds rt6_mtu_change(dev, dev->mtu); 26551da177e4SLinus Torvalds idev->cnf.mtu6 = dev->mtu; 26561da177e4SLinus Torvalds break; 26571da177e4SLinus Torvalds } 26581da177e4SLinus Torvalds 2659d31c7b8fSEvgeniy Polyakov if (!idev && dev->mtu >= IPV6_MIN_MTU) { 2660d31c7b8fSEvgeniy Polyakov idev = ipv6_add_dev(dev); 2661d31c7b8fSEvgeniy Polyakov if (idev) 2662d31c7b8fSEvgeniy Polyakov break; 2663d31c7b8fSEvgeniy Polyakov } 2664d31c7b8fSEvgeniy Polyakov 2665bcdd553fSStephen Hemminger /* 2666bcdd553fSStephen Hemminger * MTU falled under IPV6_MIN_MTU. 2667bcdd553fSStephen Hemminger * Stop IPv6 on this interface. 2668bcdd553fSStephen Hemminger */ 26691da177e4SLinus Torvalds 26701da177e4SLinus Torvalds case NETDEV_DOWN: 26711da177e4SLinus Torvalds case NETDEV_UNREGISTER: 26721da177e4SLinus Torvalds /* 26731da177e4SLinus Torvalds * Remove all addresses from this interface. 26741da177e4SLinus Torvalds */ 26751da177e4SLinus Torvalds addrconf_ifdown(dev, event != NETDEV_DOWN); 26761da177e4SLinus Torvalds break; 26773c21edbdSYOSHIFUJI Hideaki 26781da177e4SLinus Torvalds case NETDEV_CHANGENAME: 26791da177e4SLinus Torvalds if (idev) { 26805632c515SStephen Hemminger snmp6_unregister_dev(idev); 2681408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 2682f52295a9SPavel Emelyanov addrconf_sysctl_register(idev); 2683b217d616SHerbert Xu err = snmp6_register_dev(idev); 2684b217d616SHerbert Xu if (err) 2685b217d616SHerbert Xu return notifier_from_errno(err); 26865632c515SStephen Hemminger } 26871da177e4SLinus Torvalds break; 2688bcdd553fSStephen Hemminger 268993d9b7d7SJiri Pirko case NETDEV_PRE_TYPE_CHANGE: 269093d9b7d7SJiri Pirko case NETDEV_POST_TYPE_CHANGE: 269193d9b7d7SJiri Pirko addrconf_type_change(dev, event); 269275c78500SMoni Shoua break; 26933ff50b79SStephen Hemminger } 26941da177e4SLinus Torvalds 26951da177e4SLinus Torvalds return NOTIFY_OK; 26961da177e4SLinus Torvalds } 26971da177e4SLinus Torvalds 26981da177e4SLinus Torvalds /* 26991da177e4SLinus Torvalds * addrconf module should be notified of a device going up 27001da177e4SLinus Torvalds */ 27011da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = { 27021da177e4SLinus Torvalds .notifier_call = addrconf_notify, 27031da177e4SLinus Torvalds }; 27041da177e4SLinus Torvalds 270593d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, unsigned long event) 270675c78500SMoni Shoua { 270775c78500SMoni Shoua struct inet6_dev *idev; 270875c78500SMoni Shoua ASSERT_RTNL(); 270975c78500SMoni Shoua 271075c78500SMoni Shoua idev = __in6_dev_get(dev); 271175c78500SMoni Shoua 271293d9b7d7SJiri Pirko if (event == NETDEV_POST_TYPE_CHANGE) 271375c78500SMoni Shoua ipv6_mc_remap(idev); 271493d9b7d7SJiri Pirko else if (event == NETDEV_PRE_TYPE_CHANGE) 271575c78500SMoni Shoua ipv6_mc_unmap(idev); 271675c78500SMoni Shoua } 271775c78500SMoni Shoua 27181da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how) 27191da177e4SLinus Torvalds { 2720c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 2721502a2ffdSstephen hemminger struct inet6_dev *idev; 2722502a2ffdSstephen hemminger struct inet6_ifaddr *ifa; 272373a8bd74SDavid S. Miller int state, i; 27241da177e4SLinus Torvalds 27251da177e4SLinus Torvalds ASSERT_RTNL(); 27261da177e4SLinus Torvalds 2727f3db4851SDaniel Lezcano rt6_ifdown(net, dev); 272873a8bd74SDavid S. Miller neigh_ifdown(&nd_tbl, dev); 27291da177e4SLinus Torvalds 27301da177e4SLinus Torvalds idev = __in6_dev_get(dev); 27311da177e4SLinus Torvalds if (idev == NULL) 27321da177e4SLinus Torvalds return -ENODEV; 27331da177e4SLinus Torvalds 2734bcdd553fSStephen Hemminger /* 2735bcdd553fSStephen Hemminger * Step 1: remove reference to ipv6 device from parent device. 2736bcdd553fSStephen Hemminger * Do not dev_put! 27371da177e4SLinus Torvalds */ 2738439e2385SDenis V. Lunev if (how) { 27391da177e4SLinus Torvalds idev->dead = 1; 27408814c4b5SYOSHIFUJI Hideaki 27418814c4b5SYOSHIFUJI Hideaki /* protected by rtnl_lock */ 2742a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(dev->ip6_ptr, NULL); 27431da177e4SLinus Torvalds 27441da177e4SLinus Torvalds /* Step 1.5: remove snmp6 entry */ 27451da177e4SLinus Torvalds snmp6_unregister_dev(idev); 27461da177e4SLinus Torvalds 27471da177e4SLinus Torvalds } 27481da177e4SLinus Torvalds 274973a8bd74SDavid S. Miller /* Step 2: clear hash table */ 275073a8bd74SDavid S. Miller for (i = 0; i < IN6_ADDR_HSIZE; i++) { 275173a8bd74SDavid S. Miller struct hlist_head *h = &inet6_addr_lst[i]; 275273a8bd74SDavid S. Miller struct hlist_node *n; 275373a8bd74SDavid S. Miller 275473a8bd74SDavid S. Miller spin_lock_bh(&addrconf_hash_lock); 275573a8bd74SDavid S. Miller restart: 275673a8bd74SDavid S. Miller hlist_for_each_entry_rcu(ifa, n, h, addr_lst) { 275773a8bd74SDavid S. Miller if (ifa->idev == idev) { 275873a8bd74SDavid S. Miller hlist_del_init_rcu(&ifa->addr_lst); 275973a8bd74SDavid S. Miller addrconf_del_timer(ifa); 276073a8bd74SDavid S. Miller goto restart; 276173a8bd74SDavid S. Miller } 276273a8bd74SDavid S. Miller } 276373a8bd74SDavid S. Miller spin_unlock_bh(&addrconf_hash_lock); 276473a8bd74SDavid S. Miller } 276573a8bd74SDavid S. Miller 27661da177e4SLinus Torvalds write_lock_bh(&idev->lock); 27671da177e4SLinus Torvalds 2768bcdd553fSStephen Hemminger /* Step 2: clear flags for stateless addrconf */ 2769439e2385SDenis V. Lunev if (!how) 27703c21edbdSYOSHIFUJI Hideaki idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY); 27711da177e4SLinus Torvalds 27721da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 2773439e2385SDenis V. Lunev if (how && del_timer(&idev->regen_timer)) 27741da177e4SLinus Torvalds in6_dev_put(idev); 27751da177e4SLinus Torvalds 2776bcdd553fSStephen Hemminger /* Step 3: clear tempaddr list */ 2777372e6c8fSstephen hemminger while (!list_empty(&idev->tempaddr_list)) { 2778372e6c8fSstephen hemminger ifa = list_first_entry(&idev->tempaddr_list, 2779372e6c8fSstephen hemminger struct inet6_ifaddr, tmp_list); 2780372e6c8fSstephen hemminger list_del(&ifa->tmp_list); 27811da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 27821da177e4SLinus Torvalds spin_lock_bh(&ifa->lock); 27831da177e4SLinus Torvalds 27841da177e4SLinus Torvalds if (ifa->ifpub) { 27851da177e4SLinus Torvalds in6_ifa_put(ifa->ifpub); 27861da177e4SLinus Torvalds ifa->ifpub = NULL; 27871da177e4SLinus Torvalds } 27881da177e4SLinus Torvalds spin_unlock_bh(&ifa->lock); 27891da177e4SLinus Torvalds in6_ifa_put(ifa); 27901da177e4SLinus Torvalds write_lock_bh(&idev->lock); 27911da177e4SLinus Torvalds } 27921da177e4SLinus Torvalds #endif 27938f37ada5Sstephen hemminger 2794502a2ffdSstephen hemminger while (!list_empty(&idev->addr_list)) { 2795502a2ffdSstephen hemminger ifa = list_first_entry(&idev->addr_list, 2796502a2ffdSstephen hemminger struct inet6_ifaddr, if_list); 279784e8b803Sstephen hemminger addrconf_del_timer(ifa); 279884e8b803Sstephen hemminger 2799502a2ffdSstephen hemminger list_del(&ifa->if_list); 28001da177e4SLinus Torvalds 28014c5ff6a6SHerbert Xu write_unlock_bh(&idev->lock); 280273a8bd74SDavid S. Miller 28034c5ff6a6SHerbert Xu spin_lock_bh(&ifa->state_lock); 28044c5ff6a6SHerbert Xu state = ifa->state; 28054c5ff6a6SHerbert Xu ifa->state = INET6_IFADDR_STATE_DEAD; 28064c5ff6a6SHerbert Xu spin_unlock_bh(&ifa->state_lock); 28074c5ff6a6SHerbert Xu 28089d82ca98SJohn Fastabend if (state != INET6_IFADDR_STATE_DEAD) { 28091da177e4SLinus Torvalds __ipv6_ifa_notify(RTM_DELADDR, ifa); 281073a8bd74SDavid S. Miller atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifa); 28112de79570SLorenzo Colitti } 28129d82ca98SJohn Fastabend in6_ifa_put(ifa); 281373a8bd74SDavid S. Miller 28141da177e4SLinus Torvalds write_lock_bh(&idev->lock); 28151da177e4SLinus Torvalds } 28168f37ada5Sstephen hemminger 28171da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 28181da177e4SLinus Torvalds 28191da177e4SLinus Torvalds /* Step 5: Discard multicast list */ 2820439e2385SDenis V. Lunev if (how) 28211da177e4SLinus Torvalds ipv6_mc_destroy_dev(idev); 28221da177e4SLinus Torvalds else 28231da177e4SLinus Torvalds ipv6_mc_down(idev); 28241da177e4SLinus Torvalds 28251da177e4SLinus Torvalds idev->tstamp = jiffies; 28261da177e4SLinus Torvalds 2827bcdd553fSStephen Hemminger /* Last: Shot the device (if unregistered) */ 2828439e2385SDenis V. Lunev if (how) { 2829408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 28301da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, idev->nd_parms); 28311da177e4SLinus Torvalds neigh_ifdown(&nd_tbl, dev); 28321da177e4SLinus Torvalds in6_dev_put(idev); 28331da177e4SLinus Torvalds } 28341da177e4SLinus Torvalds return 0; 28351da177e4SLinus Torvalds } 28361da177e4SLinus Torvalds 28371da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data) 28381da177e4SLinus Torvalds { 28391da177e4SLinus Torvalds struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data; 28405b2a1953Sstephen hemminger struct inet6_dev *idev = ifp->idev; 28411da177e4SLinus Torvalds 28425b2a1953Sstephen hemminger read_lock(&idev->lock); 28435b2a1953Sstephen hemminger if (idev->dead || !(idev->if_flags & IF_READY)) 28441da177e4SLinus Torvalds goto out; 28451da177e4SLinus Torvalds 28465b2a1953Sstephen hemminger if (idev->cnf.forwarding) 28471da177e4SLinus Torvalds goto out; 28485b2a1953Sstephen hemminger 28495b2a1953Sstephen hemminger /* Announcement received after solicitation was sent */ 28505b2a1953Sstephen hemminger if (idev->if_flags & IF_RA_RCVD) 28515b2a1953Sstephen hemminger goto out; 28521da177e4SLinus Torvalds 28531da177e4SLinus Torvalds spin_lock(&ifp->lock); 28545b2a1953Sstephen hemminger if (ifp->probes++ < idev->cnf.rtr_solicits) { 28551da177e4SLinus Torvalds /* The wait after the last probe can be shorter */ 28561da177e4SLinus Torvalds addrconf_mod_timer(ifp, AC_RS, 28575b2a1953Sstephen hemminger (ifp->probes == idev->cnf.rtr_solicits) ? 28585b2a1953Sstephen hemminger idev->cnf.rtr_solicit_delay : 28595b2a1953Sstephen hemminger idev->cnf.rtr_solicit_interval); 28601da177e4SLinus Torvalds spin_unlock(&ifp->lock); 28611da177e4SLinus Torvalds 28625b2a1953Sstephen hemminger ndisc_send_rs(idev->dev, &ifp->addr, &in6addr_linklocal_allrouters); 28631da177e4SLinus Torvalds } else { 28641da177e4SLinus Torvalds spin_unlock(&ifp->lock); 28651da177e4SLinus Torvalds /* 28661da177e4SLinus Torvalds * Note: we do not support deprecated "all on-link" 28671da177e4SLinus Torvalds * assumption any longer. 28681da177e4SLinus Torvalds */ 286991df42beSJoe Perches pr_debug("%s: no IPv6 routers present\n", idev->dev->name); 28701da177e4SLinus Torvalds } 28711da177e4SLinus Torvalds 28721da177e4SLinus Torvalds out: 28735b2a1953Sstephen hemminger read_unlock(&idev->lock); 28741da177e4SLinus Torvalds in6_ifa_put(ifp); 28751da177e4SLinus Torvalds } 28761da177e4SLinus Torvalds 28771da177e4SLinus Torvalds /* 28781da177e4SLinus Torvalds * Duplicate Address Detection 28791da177e4SLinus Torvalds */ 28803c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp) 28813c21edbdSYOSHIFUJI Hideaki { 28823c21edbdSYOSHIFUJI Hideaki unsigned long rand_num; 28833c21edbdSYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev; 28843c21edbdSYOSHIFUJI Hideaki 288595c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 288695c385b4SNeil Horman rand_num = 0; 288795c385b4SNeil Horman else 28883c21edbdSYOSHIFUJI Hideaki rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1); 288995c385b4SNeil Horman 28903c21edbdSYOSHIFUJI Hideaki ifp->probes = idev->cnf.dad_transmits; 28913c21edbdSYOSHIFUJI Hideaki addrconf_mod_timer(ifp, AC_DAD, rand_num); 28923c21edbdSYOSHIFUJI Hideaki } 28933c21edbdSYOSHIFUJI Hideaki 2894cf22f9a2SDavid S. Miller static void addrconf_dad_start(struct inet6_ifaddr *ifp) 28951da177e4SLinus Torvalds { 28961da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 28971da177e4SLinus Torvalds struct net_device *dev = idev->dev; 28981da177e4SLinus Torvalds 28991da177e4SLinus Torvalds addrconf_join_solict(dev, &ifp->addr); 29001da177e4SLinus Torvalds 29011da177e4SLinus Torvalds net_srandom(ifp->addr.s6_addr32[3]); 29021da177e4SLinus Torvalds 29031da177e4SLinus Torvalds read_lock_bh(&idev->lock); 2904622ccdf1SHerbert Xu spin_lock(&ifp->lock); 2905e9d3e084SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DEAD) 29061da177e4SLinus Torvalds goto out; 29071da177e4SLinus Torvalds 29081da177e4SLinus Torvalds if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) || 29091b34be74SYOSHIFUJI Hideaki idev->cnf.accept_dad < 1 || 291055ebaef1SNoriaki TAKAMIYA !(ifp->flags&IFA_F_TENTATIVE) || 291155ebaef1SNoriaki TAKAMIYA ifp->flags & IFA_F_NODAD) { 2912cc411d0bSBrian Haley ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 291321809fafSstephen hemminger spin_unlock(&ifp->lock); 29141da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 29151da177e4SLinus Torvalds 29161da177e4SLinus Torvalds addrconf_dad_completed(ifp); 29171da177e4SLinus Torvalds return; 29181da177e4SLinus Torvalds } 29191da177e4SLinus Torvalds 29206732badeSYOSHIFUJI Hideaki if (!(idev->if_flags & IF_READY)) { 292121809fafSstephen hemminger spin_unlock(&ifp->lock); 29226732badeSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 29233c21edbdSYOSHIFUJI Hideaki /* 2924590a9887SMasatake YAMATO * If the device is not ready: 29253c21edbdSYOSHIFUJI Hideaki * - keep it tentative if it is a permanent address. 29263c21edbdSYOSHIFUJI Hideaki * - otherwise, kill it. 29273c21edbdSYOSHIFUJI Hideaki */ 29283c21edbdSYOSHIFUJI Hideaki in6_ifa_hold(ifp); 2929cc411d0bSBrian Haley addrconf_dad_stop(ifp, 0); 29306732badeSYOSHIFUJI Hideaki return; 29313c21edbdSYOSHIFUJI Hideaki } 293295c385b4SNeil Horman 293395c385b4SNeil Horman /* 293495c385b4SNeil Horman * Optimistic nodes can start receiving 293595c385b4SNeil Horman * Frames right away 293695c385b4SNeil Horman */ 293795c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 293895c385b4SNeil Horman ip6_ins_rt(ifp->rt); 293995c385b4SNeil Horman 29406732badeSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 29411da177e4SLinus Torvalds out: 2942622ccdf1SHerbert Xu spin_unlock(&ifp->lock); 29431da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 29441da177e4SLinus Torvalds } 29451da177e4SLinus Torvalds 29461da177e4SLinus Torvalds static void addrconf_dad_timer(unsigned long data) 29471da177e4SLinus Torvalds { 29481da177e4SLinus Torvalds struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data; 29491da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 29501da177e4SLinus Torvalds struct in6_addr mcaddr; 29511da177e4SLinus Torvalds 2952f2344a13SHerbert Xu if (!ifp->probes && addrconf_dad_end(ifp)) 2953f2344a13SHerbert Xu goto out; 2954f2344a13SHerbert Xu 2955122e4519Sstephen hemminger read_lock(&idev->lock); 2956122e4519Sstephen hemminger if (idev->dead || !(idev->if_flags & IF_READY)) { 2957122e4519Sstephen hemminger read_unlock(&idev->lock); 29581da177e4SLinus Torvalds goto out; 29591da177e4SLinus Torvalds } 296021809fafSstephen hemminger 296121809fafSstephen hemminger spin_lock(&ifp->lock); 2962622ccdf1SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DEAD) { 2963622ccdf1SHerbert Xu spin_unlock(&ifp->lock); 2964622ccdf1SHerbert Xu read_unlock(&idev->lock); 2965622ccdf1SHerbert Xu goto out; 2966622ccdf1SHerbert Xu } 2967622ccdf1SHerbert Xu 29681da177e4SLinus Torvalds if (ifp->probes == 0) { 29691da177e4SLinus Torvalds /* 29701da177e4SLinus Torvalds * DAD was successful 29711da177e4SLinus Torvalds */ 29721da177e4SLinus Torvalds 2973cc411d0bSBrian Haley ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 297421809fafSstephen hemminger spin_unlock(&ifp->lock); 2975122e4519Sstephen hemminger read_unlock(&idev->lock); 29761da177e4SLinus Torvalds 29771da177e4SLinus Torvalds addrconf_dad_completed(ifp); 29781da177e4SLinus Torvalds 29791da177e4SLinus Torvalds goto out; 29801da177e4SLinus Torvalds } 29811da177e4SLinus Torvalds 29821da177e4SLinus Torvalds ifp->probes--; 29831da177e4SLinus Torvalds addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time); 298421809fafSstephen hemminger spin_unlock(&ifp->lock); 2985122e4519Sstephen hemminger read_unlock(&idev->lock); 29861da177e4SLinus Torvalds 29871da177e4SLinus Torvalds /* send a neighbour solicitation for our addr */ 29881da177e4SLinus Torvalds addrconf_addr_solict_mult(&ifp->addr, &mcaddr); 2989d7aabf22SYOSHIFUJI Hideaki ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any); 29901da177e4SLinus Torvalds out: 29911da177e4SLinus Torvalds in6_ifa_put(ifp); 29921da177e4SLinus Torvalds } 29931da177e4SLinus Torvalds 29941da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp) 29951da177e4SLinus Torvalds { 29961da177e4SLinus Torvalds struct net_device *dev = ifp->idev->dev; 29971da177e4SLinus Torvalds 29981da177e4SLinus Torvalds /* 29991da177e4SLinus Torvalds * Configure the address for reception. Now it is valid. 30001da177e4SLinus Torvalds */ 30011da177e4SLinus Torvalds 30021da177e4SLinus Torvalds ipv6_ifa_notify(RTM_NEWADDR, ifp); 30031da177e4SLinus Torvalds 3004026359bcSTore Anderson /* If added prefix is link local and we are prepared to process 3005026359bcSTore Anderson router advertisements, start sending router solicitations. 30061da177e4SLinus Torvalds */ 30071da177e4SLinus Torvalds 3008026359bcSTore Anderson if (((ifp->idev->cnf.accept_ra == 1 && !ifp->idev->cnf.forwarding) || 3009026359bcSTore Anderson ifp->idev->cnf.accept_ra == 2) && 30101da177e4SLinus Torvalds ifp->idev->cnf.rtr_solicits > 0 && 30111da177e4SLinus Torvalds (dev->flags&IFF_LOOPBACK) == 0 && 30121da177e4SLinus Torvalds (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) { 30131da177e4SLinus Torvalds /* 30141da177e4SLinus Torvalds * If a host as already performed a random delay 30151da177e4SLinus Torvalds * [...] as part of DAD [...] there is no need 30161da177e4SLinus Torvalds * to delay again before sending the first RS 30171da177e4SLinus Torvalds */ 3018f3ee4010SYOSHIFUJI Hideaki ndisc_send_rs(ifp->idev->dev, &ifp->addr, &in6addr_linklocal_allrouters); 30191da177e4SLinus Torvalds 30201da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 30211da177e4SLinus Torvalds ifp->probes = 1; 30221da177e4SLinus Torvalds ifp->idev->if_flags |= IF_RS_SENT; 30231da177e4SLinus Torvalds addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval); 30241da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 30251da177e4SLinus Torvalds } 30261da177e4SLinus Torvalds } 30271da177e4SLinus Torvalds 3028e21e8467SStephen Hemminger static void addrconf_dad_run(struct inet6_dev *idev) 3029e21e8467SStephen Hemminger { 3030c5e33bddSYOSHIFUJI Hideaki struct inet6_ifaddr *ifp; 3031c5e33bddSYOSHIFUJI Hideaki 3032c5e33bddSYOSHIFUJI Hideaki read_lock_bh(&idev->lock); 3033502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 303421809fafSstephen hemminger spin_lock(&ifp->lock); 3035f2344a13SHerbert Xu if (ifp->flags & IFA_F_TENTATIVE && 3036f2344a13SHerbert Xu ifp->state == INET6_IFADDR_STATE_DAD) 3037c5e33bddSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 3038f2344a13SHerbert Xu spin_unlock(&ifp->lock); 3039c5e33bddSYOSHIFUJI Hideaki } 3040c5e33bddSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 3041c5e33bddSYOSHIFUJI Hideaki } 3042c5e33bddSYOSHIFUJI Hideaki 30431da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 30441da177e4SLinus Torvalds struct if6_iter_state { 30453c40090aSDaniel Lezcano struct seq_net_private p; 30461da177e4SLinus Torvalds int bucket; 30471d578303SMihai Maruseac int offset; 30481da177e4SLinus Torvalds }; 30491da177e4SLinus Torvalds 30501d578303SMihai Maruseac static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos) 30511da177e4SLinus Torvalds { 30521da177e4SLinus Torvalds struct inet6_ifaddr *ifa = NULL; 30531da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 30541218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 30551d578303SMihai Maruseac int p = 0; 30561da177e4SLinus Torvalds 30571d578303SMihai Maruseac /* initial bucket if pos is 0 */ 30581d578303SMihai Maruseac if (pos == 0) { 30591d578303SMihai Maruseac state->bucket = 0; 30601d578303SMihai Maruseac state->offset = 0; 30611d578303SMihai Maruseac } 30621d578303SMihai Maruseac 30631d578303SMihai Maruseac for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) { 3064c2e21293Sstephen hemminger struct hlist_node *n; 30654f70eccaSEric Dumazet hlist_for_each_entry_rcu_bh(ifa, n, &inet6_addr_lst[state->bucket], 30661d578303SMihai Maruseac addr_lst) { 3067*9f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 3068*9f0d3c27SEric Dumazet continue; 30691d578303SMihai Maruseac /* sync with offset */ 30701d578303SMihai Maruseac if (p < state->offset) { 30711d578303SMihai Maruseac p++; 30721d578303SMihai Maruseac continue; 30731d578303SMihai Maruseac } 30741d578303SMihai Maruseac state->offset++; 30751da177e4SLinus Torvalds return ifa; 30761da177e4SLinus Torvalds } 30771d578303SMihai Maruseac 30781d578303SMihai Maruseac /* prepare for next bucket */ 30791d578303SMihai Maruseac state->offset = 0; 30801d578303SMihai Maruseac p = 0; 30811d578303SMihai Maruseac } 3082c2e21293Sstephen hemminger return NULL; 3083c2e21293Sstephen hemminger } 30841da177e4SLinus Torvalds 3085c2e21293Sstephen hemminger static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, 3086c2e21293Sstephen hemminger struct inet6_ifaddr *ifa) 30871da177e4SLinus Torvalds { 30881da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 30891218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 3090c2e21293Sstephen hemminger struct hlist_node *n = &ifa->addr_lst; 30911da177e4SLinus Torvalds 30921d578303SMihai Maruseac hlist_for_each_entry_continue_rcu_bh(ifa, n, addr_lst) { 3093*9f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 3094*9f0d3c27SEric Dumazet continue; 30951d578303SMihai Maruseac state->offset++; 30961da177e4SLinus Torvalds return ifa; 30971d578303SMihai Maruseac } 30981da177e4SLinus Torvalds 3099c2e21293Sstephen hemminger while (++state->bucket < IN6_ADDR_HSIZE) { 31001d578303SMihai Maruseac state->offset = 0; 31014f70eccaSEric Dumazet hlist_for_each_entry_rcu_bh(ifa, n, 3102c2e21293Sstephen hemminger &inet6_addr_lst[state->bucket], addr_lst) { 3103*9f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 3104*9f0d3c27SEric Dumazet continue; 31051d578303SMihai Maruseac state->offset++; 3106c2e21293Sstephen hemminger return ifa; 3107c2e21293Sstephen hemminger } 3108c2e21293Sstephen hemminger } 3109c2e21293Sstephen hemminger 3110c2e21293Sstephen hemminger return NULL; 3111c2e21293Sstephen hemminger } 3112c2e21293Sstephen hemminger 31131da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos) 31144f70eccaSEric Dumazet __acquires(rcu_bh) 31151da177e4SLinus Torvalds { 31165c578aedSstephen hemminger rcu_read_lock_bh(); 31171d578303SMihai Maruseac return if6_get_first(seq, *pos); 31181da177e4SLinus Torvalds } 31191da177e4SLinus Torvalds 31201da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos) 31211da177e4SLinus Torvalds { 31221da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 31231da177e4SLinus Torvalds 31241da177e4SLinus Torvalds ifa = if6_get_next(seq, v); 31251da177e4SLinus Torvalds ++*pos; 31261da177e4SLinus Torvalds return ifa; 31271da177e4SLinus Torvalds } 31281da177e4SLinus Torvalds 31291da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v) 31304f70eccaSEric Dumazet __releases(rcu_bh) 31311da177e4SLinus Torvalds { 31325c578aedSstephen hemminger rcu_read_unlock_bh(); 31331da177e4SLinus Torvalds } 31341da177e4SLinus Torvalds 31351da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v) 31361da177e4SLinus Torvalds { 31371da177e4SLinus Torvalds struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v; 31384b7a4274SHarvey Harrison seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n", 3139b071195dSHarvey Harrison &ifp->addr, 31401da177e4SLinus Torvalds ifp->idev->dev->ifindex, 31411da177e4SLinus Torvalds ifp->prefix_len, 31421da177e4SLinus Torvalds ifp->scope, 31431da177e4SLinus Torvalds ifp->flags, 31441da177e4SLinus Torvalds ifp->idev->dev->name); 31451da177e4SLinus Torvalds return 0; 31461da177e4SLinus Torvalds } 31471da177e4SLinus Torvalds 314856b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = { 31491da177e4SLinus Torvalds .start = if6_seq_start, 31501da177e4SLinus Torvalds .next = if6_seq_next, 31511da177e4SLinus Torvalds .show = if6_seq_show, 31521da177e4SLinus Torvalds .stop = if6_seq_stop, 31531da177e4SLinus Torvalds }; 31541da177e4SLinus Torvalds 31551da177e4SLinus Torvalds static int if6_seq_open(struct inode *inode, struct file *file) 31561da177e4SLinus Torvalds { 31573c40090aSDaniel Lezcano return seq_open_net(inode, file, &if6_seq_ops, 3158cf7732e4SPavel Emelyanov sizeof(struct if6_iter_state)); 31591da177e4SLinus Torvalds } 31601da177e4SLinus Torvalds 31619a32144eSArjan van de Ven static const struct file_operations if6_fops = { 31621da177e4SLinus Torvalds .owner = THIS_MODULE, 31631da177e4SLinus Torvalds .open = if6_seq_open, 31641da177e4SLinus Torvalds .read = seq_read, 31651da177e4SLinus Torvalds .llseek = seq_lseek, 31663c40090aSDaniel Lezcano .release = seq_release_net, 31671da177e4SLinus Torvalds }; 31681da177e4SLinus Torvalds 31692c8c1e72SAlexey Dobriyan static int __net_init if6_proc_net_init(struct net *net) 31701da177e4SLinus Torvalds { 31713c40090aSDaniel Lezcano if (!proc_net_fops_create(net, "if_inet6", S_IRUGO, &if6_fops)) 31721da177e4SLinus Torvalds return -ENOMEM; 31731da177e4SLinus Torvalds return 0; 31741da177e4SLinus Torvalds } 31751da177e4SLinus Torvalds 31762c8c1e72SAlexey Dobriyan static void __net_exit if6_proc_net_exit(struct net *net) 31773c40090aSDaniel Lezcano { 31783c40090aSDaniel Lezcano proc_net_remove(net, "if_inet6"); 31793c40090aSDaniel Lezcano } 31803c40090aSDaniel Lezcano 31813c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = { 31823c40090aSDaniel Lezcano .init = if6_proc_net_init, 31833c40090aSDaniel Lezcano .exit = if6_proc_net_exit, 31843c40090aSDaniel Lezcano }; 31853c40090aSDaniel Lezcano 31863c40090aSDaniel Lezcano int __init if6_proc_init(void) 31873c40090aSDaniel Lezcano { 31883c40090aSDaniel Lezcano return register_pernet_subsys(&if6_proc_net_ops); 31893c40090aSDaniel Lezcano } 31903c40090aSDaniel Lezcano 31911da177e4SLinus Torvalds void if6_proc_exit(void) 31921da177e4SLinus Torvalds { 31933c40090aSDaniel Lezcano unregister_pernet_subsys(&if6_proc_net_ops); 31941da177e4SLinus Torvalds } 31951da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 31961da177e4SLinus Torvalds 319759fbb3a6SMasahide NAKAMURA #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) 31983b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */ 3199b71d1d42SEric Dumazet int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr) 32003b9f9a1cSNoriaki TAKAMIYA { 32013b9f9a1cSNoriaki TAKAMIYA int ret = 0; 3202c2e21293Sstephen hemminger struct inet6_ifaddr *ifp = NULL; 3203c2e21293Sstephen hemminger struct hlist_node *n; 3204ddbe5032SEric Dumazet unsigned int hash = inet6_addr_hash(addr); 3205c2e21293Sstephen hemminger 32065c578aedSstephen hemminger rcu_read_lock_bh(); 32074f70eccaSEric Dumazet hlist_for_each_entry_rcu_bh(ifp, n, &inet6_addr_lst[hash], addr_lst) { 3208878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 3209389f6612SDaniel Lezcano continue; 3210caad295fSYOSHIFUJI Hideaki if (ipv6_addr_equal(&ifp->addr, addr) && 32113b9f9a1cSNoriaki TAKAMIYA (ifp->flags & IFA_F_HOMEADDRESS)) { 32123b9f9a1cSNoriaki TAKAMIYA ret = 1; 32133b9f9a1cSNoriaki TAKAMIYA break; 32143b9f9a1cSNoriaki TAKAMIYA } 32153b9f9a1cSNoriaki TAKAMIYA } 32165c578aedSstephen hemminger rcu_read_unlock_bh(); 32173b9f9a1cSNoriaki TAKAMIYA return ret; 32183b9f9a1cSNoriaki TAKAMIYA } 32193b9f9a1cSNoriaki TAKAMIYA #endif 32203b9f9a1cSNoriaki TAKAMIYA 32211da177e4SLinus Torvalds /* 32221da177e4SLinus Torvalds * Periodic address status verification 32231da177e4SLinus Torvalds */ 32241da177e4SLinus Torvalds 32251da177e4SLinus Torvalds static void addrconf_verify(unsigned long foo) 32261da177e4SLinus Torvalds { 3227b2db7564SYOSHIFUJI Hideaki unsigned long now, next, next_sec, next_sched; 32281da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 3229c2e21293Sstephen hemminger struct hlist_node *node; 32301da177e4SLinus Torvalds int i; 32311da177e4SLinus Torvalds 32325c578aedSstephen hemminger rcu_read_lock_bh(); 32335c578aedSstephen hemminger spin_lock(&addrconf_verify_lock); 32341da177e4SLinus Torvalds now = jiffies; 3235b2db7564SYOSHIFUJI Hideaki next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY); 32361da177e4SLinus Torvalds 32371da177e4SLinus Torvalds del_timer(&addr_chk_timer); 32381da177e4SLinus Torvalds 32391da177e4SLinus Torvalds for (i = 0; i < IN6_ADDR_HSIZE; i++) { 32401da177e4SLinus Torvalds restart: 32414f70eccaSEric Dumazet hlist_for_each_entry_rcu_bh(ifp, node, 32425c578aedSstephen hemminger &inet6_addr_lst[i], addr_lst) { 32431da177e4SLinus Torvalds unsigned long age; 32441da177e4SLinus Torvalds 32451da177e4SLinus Torvalds if (ifp->flags & IFA_F_PERMANENT) 32461da177e4SLinus Torvalds continue; 32471da177e4SLinus Torvalds 32481da177e4SLinus Torvalds spin_lock(&ifp->lock); 3249b2db7564SYOSHIFUJI Hideaki /* We try to batch several events at once. */ 3250b2db7564SYOSHIFUJI Hideaki age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; 32511da177e4SLinus Torvalds 32528f27ebb9SYOSHIFUJI Hideaki if (ifp->valid_lft != INFINITY_LIFE_TIME && 32538f27ebb9SYOSHIFUJI Hideaki age >= ifp->valid_lft) { 32541da177e4SLinus Torvalds spin_unlock(&ifp->lock); 32551da177e4SLinus Torvalds in6_ifa_hold(ifp); 32561da177e4SLinus Torvalds ipv6_del_addr(ifp); 32571da177e4SLinus Torvalds goto restart; 32588f27ebb9SYOSHIFUJI Hideaki } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) { 32598f27ebb9SYOSHIFUJI Hideaki spin_unlock(&ifp->lock); 32608f27ebb9SYOSHIFUJI Hideaki continue; 32611da177e4SLinus Torvalds } else if (age >= ifp->prefered_lft) { 3262a1ed0526SBrian Haley /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */ 32631da177e4SLinus Torvalds int deprecate = 0; 32641da177e4SLinus Torvalds 32651da177e4SLinus Torvalds if (!(ifp->flags&IFA_F_DEPRECATED)) { 32661da177e4SLinus Torvalds deprecate = 1; 32671da177e4SLinus Torvalds ifp->flags |= IFA_F_DEPRECATED; 32681da177e4SLinus Torvalds } 32691da177e4SLinus Torvalds 32701da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)) 32711da177e4SLinus Torvalds next = ifp->tstamp + ifp->valid_lft * HZ; 32721da177e4SLinus Torvalds 32731da177e4SLinus Torvalds spin_unlock(&ifp->lock); 32741da177e4SLinus Torvalds 32751da177e4SLinus Torvalds if (deprecate) { 32761da177e4SLinus Torvalds in6_ifa_hold(ifp); 32771da177e4SLinus Torvalds 32781da177e4SLinus Torvalds ipv6_ifa_notify(0, ifp); 32791da177e4SLinus Torvalds in6_ifa_put(ifp); 32801da177e4SLinus Torvalds goto restart; 32811da177e4SLinus Torvalds } 32821da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 32831da177e4SLinus Torvalds } else if ((ifp->flags&IFA_F_TEMPORARY) && 32841da177e4SLinus Torvalds !(ifp->flags&IFA_F_TENTATIVE)) { 328588949cf4Sstephen hemminger unsigned long regen_advance = ifp->idev->cnf.regen_max_retry * 328688949cf4Sstephen hemminger ifp->idev->cnf.dad_transmits * 328788949cf4Sstephen hemminger ifp->idev->nd_parms->retrans_time / HZ; 328888949cf4Sstephen hemminger 32891da177e4SLinus Torvalds if (age >= ifp->prefered_lft - regen_advance) { 32901da177e4SLinus Torvalds struct inet6_ifaddr *ifpub = ifp->ifpub; 32911da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 32921da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 32931da177e4SLinus Torvalds if (!ifp->regen_count && ifpub) { 32941da177e4SLinus Torvalds ifp->regen_count++; 32951da177e4SLinus Torvalds in6_ifa_hold(ifp); 32961da177e4SLinus Torvalds in6_ifa_hold(ifpub); 32971da177e4SLinus Torvalds spin_unlock(&ifp->lock); 32985c578aedSstephen hemminger 3299291d809bSHiroyuki YAMAMORI spin_lock(&ifpub->lock); 3300291d809bSHiroyuki YAMAMORI ifpub->regen_count = 0; 3301291d809bSHiroyuki YAMAMORI spin_unlock(&ifpub->lock); 33021da177e4SLinus Torvalds ipv6_create_tempaddr(ifpub, ifp); 33031da177e4SLinus Torvalds in6_ifa_put(ifpub); 33041da177e4SLinus Torvalds in6_ifa_put(ifp); 33051da177e4SLinus Torvalds goto restart; 33061da177e4SLinus Torvalds } 33071da177e4SLinus Torvalds } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next)) 33081da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ; 33091da177e4SLinus Torvalds spin_unlock(&ifp->lock); 33101da177e4SLinus Torvalds #endif 33111da177e4SLinus Torvalds } else { 33121da177e4SLinus Torvalds /* ifp->prefered_lft <= ifp->valid_lft */ 33131da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 33141da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 33151da177e4SLinus Torvalds spin_unlock(&ifp->lock); 33161da177e4SLinus Torvalds } 33171da177e4SLinus Torvalds } 33181da177e4SLinus Torvalds } 33191da177e4SLinus Torvalds 3320b2db7564SYOSHIFUJI Hideaki next_sec = round_jiffies_up(next); 3321b2db7564SYOSHIFUJI Hideaki next_sched = next; 3322b2db7564SYOSHIFUJI Hideaki 3323b2db7564SYOSHIFUJI Hideaki /* If rounded timeout is accurate enough, accept it. */ 3324b2db7564SYOSHIFUJI Hideaki if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ)) 3325b2db7564SYOSHIFUJI Hideaki next_sched = next_sec; 3326b2db7564SYOSHIFUJI Hideaki 3327b2db7564SYOSHIFUJI Hideaki /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */ 3328b2db7564SYOSHIFUJI Hideaki if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX)) 3329b2db7564SYOSHIFUJI Hideaki next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX; 3330b2db7564SYOSHIFUJI Hideaki 3331b2db7564SYOSHIFUJI Hideaki ADBG((KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n", 3332b2db7564SYOSHIFUJI Hideaki now, next, next_sec, next_sched)); 3333b2db7564SYOSHIFUJI Hideaki 3334b2db7564SYOSHIFUJI Hideaki addr_chk_timer.expires = next_sched; 33351da177e4SLinus Torvalds add_timer(&addr_chk_timer); 33365c578aedSstephen hemminger spin_unlock(&addrconf_verify_lock); 33375c578aedSstephen hemminger rcu_read_unlock_bh(); 33381da177e4SLinus Torvalds } 33391da177e4SLinus Torvalds 3340461d8837SThomas Graf static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local) 3341461d8837SThomas Graf { 3342461d8837SThomas Graf struct in6_addr *pfx = NULL; 3343461d8837SThomas Graf 3344461d8837SThomas Graf if (addr) 3345461d8837SThomas Graf pfx = nla_data(addr); 3346461d8837SThomas Graf 3347461d8837SThomas Graf if (local) { 3348461d8837SThomas Graf if (pfx && nla_memcmp(local, pfx, sizeof(*pfx))) 3349461d8837SThomas Graf pfx = NULL; 3350461d8837SThomas Graf else 3351461d8837SThomas Graf pfx = nla_data(local); 3352461d8837SThomas Graf } 3353461d8837SThomas Graf 3354461d8837SThomas Graf return pfx; 3355461d8837SThomas Graf } 3356461d8837SThomas Graf 3357ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = { 3358461d8837SThomas Graf [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) }, 3359461d8837SThomas Graf [IFA_LOCAL] = { .len = sizeof(struct in6_addr) }, 3360461d8837SThomas Graf [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) }, 3361461d8837SThomas Graf }; 3362461d8837SThomas Graf 33631da177e4SLinus Torvalds static int 33641da177e4SLinus Torvalds inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 33651da177e4SLinus Torvalds { 33663b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 3367b933f716SThomas Graf struct ifaddrmsg *ifm; 3368b933f716SThomas Graf struct nlattr *tb[IFA_MAX+1]; 33691da177e4SLinus Torvalds struct in6_addr *pfx; 3370b933f716SThomas Graf int err; 33711da177e4SLinus Torvalds 3372b933f716SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 3373b933f716SThomas Graf if (err < 0) 3374b933f716SThomas Graf return err; 3375b933f716SThomas Graf 3376b933f716SThomas Graf ifm = nlmsg_data(nlh); 3377b933f716SThomas Graf pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]); 33781da177e4SLinus Torvalds if (pfx == NULL) 33791da177e4SLinus Torvalds return -EINVAL; 33801da177e4SLinus Torvalds 3381af284937SDaniel Lezcano return inet6_addr_del(net, ifm->ifa_index, pfx, ifm->ifa_prefixlen); 33821da177e4SLinus Torvalds } 33831da177e4SLinus Torvalds 338455ebaef1SNoriaki TAKAMIYA static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags, 338555ebaef1SNoriaki TAKAMIYA u32 prefered_lft, u32 valid_lft) 3386081bba5bSNoriaki TAKAMIYA { 33876f704992SYOSHIFUJI Hideaki u32 flags; 33886f704992SYOSHIFUJI Hideaki clock_t expires; 33894bed72e4SYOSHIFUJI Hideaki unsigned long timeout; 339046d48046SYOSHIFUJI Hideaki 3391081bba5bSNoriaki TAKAMIYA if (!valid_lft || (prefered_lft > valid_lft)) 3392081bba5bSNoriaki TAKAMIYA return -EINVAL; 3393081bba5bSNoriaki TAKAMIYA 33944bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(valid_lft, HZ); 33954bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 33964bed72e4SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(timeout * HZ); 33974bed72e4SYOSHIFUJI Hideaki valid_lft = timeout; 33986f704992SYOSHIFUJI Hideaki flags = RTF_EXPIRES; 33994bed72e4SYOSHIFUJI Hideaki } else { 34004bed72e4SYOSHIFUJI Hideaki expires = 0; 34014bed72e4SYOSHIFUJI Hideaki flags = 0; 34024bed72e4SYOSHIFUJI Hideaki ifa_flags |= IFA_F_PERMANENT; 34036f704992SYOSHIFUJI Hideaki } 3404081bba5bSNoriaki TAKAMIYA 34054bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(prefered_lft, HZ); 34064bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 34074bed72e4SYOSHIFUJI Hideaki if (timeout == 0) 340855ebaef1SNoriaki TAKAMIYA ifa_flags |= IFA_F_DEPRECATED; 34094bed72e4SYOSHIFUJI Hideaki prefered_lft = timeout; 34104bed72e4SYOSHIFUJI Hideaki } 3411081bba5bSNoriaki TAKAMIYA 3412081bba5bSNoriaki TAKAMIYA spin_lock_bh(&ifp->lock); 34133b9f9a1cSNoriaki TAKAMIYA ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags; 3414081bba5bSNoriaki TAKAMIYA ifp->tstamp = jiffies; 3415081bba5bSNoriaki TAKAMIYA ifp->valid_lft = valid_lft; 3416081bba5bSNoriaki TAKAMIYA ifp->prefered_lft = prefered_lft; 3417081bba5bSNoriaki TAKAMIYA 3418081bba5bSNoriaki TAKAMIYA spin_unlock_bh(&ifp->lock); 3419081bba5bSNoriaki TAKAMIYA if (!(ifp->flags&IFA_F_TENTATIVE)) 3420081bba5bSNoriaki TAKAMIYA ipv6_ifa_notify(0, ifp); 3421081bba5bSNoriaki TAKAMIYA 342246d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev, 34236f704992SYOSHIFUJI Hideaki expires, flags); 3424081bba5bSNoriaki TAKAMIYA addrconf_verify(0); 3425081bba5bSNoriaki TAKAMIYA 3426081bba5bSNoriaki TAKAMIYA return 0; 3427081bba5bSNoriaki TAKAMIYA } 3428081bba5bSNoriaki TAKAMIYA 3429081bba5bSNoriaki TAKAMIYA static int 34301da177e4SLinus Torvalds inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 34311da177e4SLinus Torvalds { 34323b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 3433461d8837SThomas Graf struct ifaddrmsg *ifm; 3434461d8837SThomas Graf struct nlattr *tb[IFA_MAX+1]; 34351da177e4SLinus Torvalds struct in6_addr *pfx; 34367198f8ceSThomas Graf struct inet6_ifaddr *ifa; 34377198f8ceSThomas Graf struct net_device *dev; 343855ebaef1SNoriaki TAKAMIYA u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME; 343955ebaef1SNoriaki TAKAMIYA u8 ifa_flags; 3440461d8837SThomas Graf int err; 34411da177e4SLinus Torvalds 3442461d8837SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 3443461d8837SThomas Graf if (err < 0) 3444461d8837SThomas Graf return err; 3445461d8837SThomas Graf 3446461d8837SThomas Graf ifm = nlmsg_data(nlh); 3447461d8837SThomas Graf pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]); 34481da177e4SLinus Torvalds if (pfx == NULL) 34491da177e4SLinus Torvalds return -EINVAL; 34501da177e4SLinus Torvalds 3451461d8837SThomas Graf if (tb[IFA_CACHEINFO]) { 34520778769dSNoriaki TAKAMIYA struct ifa_cacheinfo *ci; 3453461d8837SThomas Graf 3454461d8837SThomas Graf ci = nla_data(tb[IFA_CACHEINFO]); 34550778769dSNoriaki TAKAMIYA valid_lft = ci->ifa_valid; 3456461d8837SThomas Graf preferred_lft = ci->ifa_prefered; 3457461d8837SThomas Graf } else { 3458461d8837SThomas Graf preferred_lft = INFINITY_LIFE_TIME; 3459461d8837SThomas Graf valid_lft = INFINITY_LIFE_TIME; 34600778769dSNoriaki TAKAMIYA } 34610778769dSNoriaki TAKAMIYA 3462af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifm->ifa_index); 34637198f8ceSThomas Graf if (dev == NULL) 34647198f8ceSThomas Graf return -ENODEV; 3465081bba5bSNoriaki TAKAMIYA 346655ebaef1SNoriaki TAKAMIYA /* We ignore other flags so far. */ 34673b9f9a1cSNoriaki TAKAMIYA ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS); 346855ebaef1SNoriaki TAKAMIYA 34691cab3da6SDaniel Lezcano ifa = ipv6_get_ifaddr(net, pfx, dev, 1); 34707198f8ceSThomas Graf if (ifa == NULL) { 34717198f8ceSThomas Graf /* 34727198f8ceSThomas Graf * It would be best to check for !NLM_F_CREATE here but 34737198f8ceSThomas Graf * userspace alreay relies on not having to provide this. 34747198f8ceSThomas Graf */ 3475af284937SDaniel Lezcano return inet6_addr_add(net, ifm->ifa_index, pfx, 3476af284937SDaniel Lezcano ifm->ifa_prefixlen, ifa_flags, 3477af284937SDaniel Lezcano preferred_lft, valid_lft); 34781da177e4SLinus Torvalds } 34791da177e4SLinus Torvalds 34807198f8ceSThomas Graf if (nlh->nlmsg_flags & NLM_F_EXCL || 34817198f8ceSThomas Graf !(nlh->nlmsg_flags & NLM_F_REPLACE)) 34827198f8ceSThomas Graf err = -EEXIST; 34837198f8ceSThomas Graf else 348455ebaef1SNoriaki TAKAMIYA err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft); 34857198f8ceSThomas Graf 34867198f8ceSThomas Graf in6_ifa_put(ifa); 34877198f8ceSThomas Graf 34887198f8ceSThomas Graf return err; 34897198f8ceSThomas Graf } 34907198f8ceSThomas Graf 3491101bb229SThomas Graf static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags, 3492101bb229SThomas Graf u8 scope, int ifindex) 3493101bb229SThomas Graf { 3494101bb229SThomas Graf struct ifaddrmsg *ifm; 3495101bb229SThomas Graf 3496101bb229SThomas Graf ifm = nlmsg_data(nlh); 3497101bb229SThomas Graf ifm->ifa_family = AF_INET6; 3498101bb229SThomas Graf ifm->ifa_prefixlen = prefixlen; 3499101bb229SThomas Graf ifm->ifa_flags = flags; 3500101bb229SThomas Graf ifm->ifa_scope = scope; 3501101bb229SThomas Graf ifm->ifa_index = ifindex; 3502101bb229SThomas Graf } 3503101bb229SThomas Graf 350485486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp, 350585486af0SThomas Graf unsigned long tstamp, u32 preferred, u32 valid) 350685486af0SThomas Graf { 350785486af0SThomas Graf struct ifa_cacheinfo ci; 350885486af0SThomas Graf 350918a31e1eSThomas Graf ci.cstamp = cstamp_delta(cstamp); 351018a31e1eSThomas Graf ci.tstamp = cstamp_delta(tstamp); 351185486af0SThomas Graf ci.ifa_prefered = preferred; 351285486af0SThomas Graf ci.ifa_valid = valid; 351385486af0SThomas Graf 351485486af0SThomas Graf return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci); 351585486af0SThomas Graf } 351685486af0SThomas Graf 3517101bb229SThomas Graf static inline int rt_scope(int ifa_scope) 3518101bb229SThomas Graf { 3519101bb229SThomas Graf if (ifa_scope & IFA_HOST) 3520101bb229SThomas Graf return RT_SCOPE_HOST; 3521101bb229SThomas Graf else if (ifa_scope & IFA_LINK) 3522101bb229SThomas Graf return RT_SCOPE_LINK; 3523101bb229SThomas Graf else if (ifa_scope & IFA_SITE) 3524101bb229SThomas Graf return RT_SCOPE_SITE; 3525101bb229SThomas Graf else 3526101bb229SThomas Graf return RT_SCOPE_UNIVERSE; 3527101bb229SThomas Graf } 3528101bb229SThomas Graf 35290ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void) 35300ab6803bSThomas Graf { 3531339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifaddrmsg)) 3532339bf98fSThomas Graf + nla_total_size(16) /* IFA_ADDRESS */ 3533339bf98fSThomas Graf + nla_total_size(sizeof(struct ifa_cacheinfo)); 35340ab6803bSThomas Graf } 3535c5396a31SYOSHIFUJI Hideaki 35361da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa, 353715e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 35381da177e4SLinus Torvalds { 35391da177e4SLinus Torvalds struct nlmsghdr *nlh; 354085486af0SThomas Graf u32 preferred, valid; 35411da177e4SLinus Torvalds 354215e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 35430ab6803bSThomas Graf if (nlh == NULL) 354426932566SPatrick McHardy return -EMSGSIZE; 35450ab6803bSThomas Graf 3546101bb229SThomas Graf put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope), 3547101bb229SThomas Graf ifa->idev->dev->ifindex); 3548101bb229SThomas Graf 35491da177e4SLinus Torvalds if (!(ifa->flags&IFA_F_PERMANENT)) { 355085486af0SThomas Graf preferred = ifa->prefered_lft; 355185486af0SThomas Graf valid = ifa->valid_lft; 355285486af0SThomas Graf if (preferred != INFINITY_LIFE_TIME) { 35531da177e4SLinus Torvalds long tval = (jiffies - ifa->tstamp)/HZ; 3554a1faa698SJens Rosenboom if (preferred > tval) 355585486af0SThomas Graf preferred -= tval; 3556a1faa698SJens Rosenboom else 3557a1faa698SJens Rosenboom preferred = 0; 3558f56619fcSBen Hutchings if (valid != INFINITY_LIFE_TIME) { 3559f56619fcSBen Hutchings if (valid > tval) 356085486af0SThomas Graf valid -= tval; 3561f56619fcSBen Hutchings else 3562f56619fcSBen Hutchings valid = 0; 3563f56619fcSBen Hutchings } 35641da177e4SLinus Torvalds } 35651da177e4SLinus Torvalds } else { 356685486af0SThomas Graf preferred = INFINITY_LIFE_TIME; 356785486af0SThomas Graf valid = INFINITY_LIFE_TIME; 35681da177e4SLinus Torvalds } 356985486af0SThomas Graf 35700ab6803bSThomas Graf if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 || 357126932566SPatrick McHardy put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) { 357226932566SPatrick McHardy nlmsg_cancel(skb, nlh); 357326932566SPatrick McHardy return -EMSGSIZE; 357426932566SPatrick McHardy } 357585486af0SThomas Graf 35760ab6803bSThomas Graf return nlmsg_end(skb, nlh); 35771da177e4SLinus Torvalds } 35781da177e4SLinus Torvalds 35791da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca, 358015e47304SEric W. Biederman u32 portid, u32 seq, int event, u16 flags) 35811da177e4SLinus Torvalds { 35821da177e4SLinus Torvalds struct nlmsghdr *nlh; 3583101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 3584101bb229SThomas Graf int ifindex = ifmca->idev->dev->ifindex; 35851da177e4SLinus Torvalds 35861da177e4SLinus Torvalds if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE) 3587101bb229SThomas Graf scope = RT_SCOPE_SITE; 3588101bb229SThomas Graf 358915e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 35900ab6803bSThomas Graf if (nlh == NULL) 359126932566SPatrick McHardy return -EMSGSIZE; 35920ab6803bSThomas Graf 3593101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 35940ab6803bSThomas Graf if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 || 35950ab6803bSThomas Graf put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp, 359626932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 359726932566SPatrick McHardy nlmsg_cancel(skb, nlh); 359826932566SPatrick McHardy return -EMSGSIZE; 359926932566SPatrick McHardy } 360085486af0SThomas Graf 36010ab6803bSThomas Graf return nlmsg_end(skb, nlh); 36021da177e4SLinus Torvalds } 36031da177e4SLinus Torvalds 36041da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca, 360515e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 36061da177e4SLinus Torvalds { 36071da177e4SLinus Torvalds struct nlmsghdr *nlh; 3608101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 3609101bb229SThomas Graf int ifindex = ifaca->aca_idev->dev->ifindex; 36101da177e4SLinus Torvalds 36111da177e4SLinus Torvalds if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE) 3612101bb229SThomas Graf scope = RT_SCOPE_SITE; 3613101bb229SThomas Graf 361415e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 36150ab6803bSThomas Graf if (nlh == NULL) 361626932566SPatrick McHardy return -EMSGSIZE; 36170ab6803bSThomas Graf 3618101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 36190ab6803bSThomas Graf if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 || 36200ab6803bSThomas Graf put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp, 362126932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 362226932566SPatrick McHardy nlmsg_cancel(skb, nlh); 362326932566SPatrick McHardy return -EMSGSIZE; 362426932566SPatrick McHardy } 362585486af0SThomas Graf 36260ab6803bSThomas Graf return nlmsg_end(skb, nlh); 36271da177e4SLinus Torvalds } 36281da177e4SLinus Torvalds 3629e21e8467SStephen Hemminger enum addr_type_t { 36301da177e4SLinus Torvalds UNICAST_ADDR, 36311da177e4SLinus Torvalds MULTICAST_ADDR, 36321da177e4SLinus Torvalds ANYCAST_ADDR, 36331da177e4SLinus Torvalds }; 36341da177e4SLinus Torvalds 3635234b27c3SEric Dumazet /* called with rcu_read_lock() */ 3636234b27c3SEric Dumazet static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb, 3637234b27c3SEric Dumazet struct netlink_callback *cb, enum addr_type_t type, 3638234b27c3SEric Dumazet int s_ip_idx, int *p_ip_idx) 36391da177e4SLinus Torvalds { 36401da177e4SLinus Torvalds struct ifmcaddr6 *ifmca; 36411da177e4SLinus Torvalds struct ifacaddr6 *ifaca; 3642234b27c3SEric Dumazet int err = 1; 3643234b27c3SEric Dumazet int ip_idx = *p_ip_idx; 36441da177e4SLinus Torvalds 36451da177e4SLinus Torvalds read_lock_bh(&idev->lock); 36461da177e4SLinus Torvalds switch (type) { 3647502a2ffdSstephen hemminger case UNICAST_ADDR: { 3648502a2ffdSstephen hemminger struct inet6_ifaddr *ifa; 3649502a2ffdSstephen hemminger 3650ae9cda5dSYOSHIFUJI Hideaki /* unicast address incl. temp addr */ 3651502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 3652502a2ffdSstephen hemminger if (++ip_idx < s_ip_idx) 36531da177e4SLinus Torvalds continue; 36545d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifaddr(skb, ifa, 365515e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 36565d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 36575d5619b4SYOSHIFUJI Hideaki RTM_NEWADDR, 36585d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 3659bcd32326SEric Dumazet if (err <= 0) 3660bcd32326SEric Dumazet break; 36611da177e4SLinus Torvalds } 36621da177e4SLinus Torvalds break; 3663502a2ffdSstephen hemminger } 36641da177e4SLinus Torvalds case MULTICAST_ADDR: 36651da177e4SLinus Torvalds /* multicast address */ 36661da177e4SLinus Torvalds for (ifmca = idev->mc_list; ifmca; 36671da177e4SLinus Torvalds ifmca = ifmca->next, ip_idx++) { 36681da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 36691da177e4SLinus Torvalds continue; 36705d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifmcaddr(skb, ifmca, 367115e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 36725d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 36735d5619b4SYOSHIFUJI Hideaki RTM_GETMULTICAST, 36745d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 3675bcd32326SEric Dumazet if (err <= 0) 3676bcd32326SEric Dumazet break; 36771da177e4SLinus Torvalds } 36781da177e4SLinus Torvalds break; 36791da177e4SLinus Torvalds case ANYCAST_ADDR: 36801da177e4SLinus Torvalds /* anycast address */ 36811da177e4SLinus Torvalds for (ifaca = idev->ac_list; ifaca; 36821da177e4SLinus Torvalds ifaca = ifaca->aca_next, ip_idx++) { 36831da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 36841da177e4SLinus Torvalds continue; 36855d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifacaddr(skb, ifaca, 368615e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 36875d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 36885d5619b4SYOSHIFUJI Hideaki RTM_GETANYCAST, 36895d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 3690bcd32326SEric Dumazet if (err <= 0) 3691bcd32326SEric Dumazet break; 36921da177e4SLinus Torvalds } 36931da177e4SLinus Torvalds break; 36941da177e4SLinus Torvalds default: 36951da177e4SLinus Torvalds break; 36961da177e4SLinus Torvalds } 36971da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 3698234b27c3SEric Dumazet *p_ip_idx = ip_idx; 3699234b27c3SEric Dumazet return err; 3700234b27c3SEric Dumazet } 37015d5619b4SYOSHIFUJI Hideaki 3702234b27c3SEric Dumazet static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb, 3703234b27c3SEric Dumazet enum addr_type_t type) 3704234b27c3SEric Dumazet { 3705234b27c3SEric Dumazet struct net *net = sock_net(skb->sk); 3706234b27c3SEric Dumazet int h, s_h; 3707234b27c3SEric Dumazet int idx, ip_idx; 3708234b27c3SEric Dumazet int s_idx, s_ip_idx; 3709234b27c3SEric Dumazet struct net_device *dev; 3710234b27c3SEric Dumazet struct inet6_dev *idev; 3711234b27c3SEric Dumazet struct hlist_head *head; 3712234b27c3SEric Dumazet struct hlist_node *node; 3713234b27c3SEric Dumazet 3714234b27c3SEric Dumazet s_h = cb->args[0]; 3715234b27c3SEric Dumazet s_idx = idx = cb->args[1]; 3716234b27c3SEric Dumazet s_ip_idx = ip_idx = cb->args[2]; 3717234b27c3SEric Dumazet 3718234b27c3SEric Dumazet rcu_read_lock(); 3719234b27c3SEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 3720234b27c3SEric Dumazet idx = 0; 3721234b27c3SEric Dumazet head = &net->dev_index_head[h]; 3722234b27c3SEric Dumazet hlist_for_each_entry_rcu(dev, node, head, index_hlist) { 3723234b27c3SEric Dumazet if (idx < s_idx) 3724234b27c3SEric Dumazet goto cont; 37254b97efdfSPatrick McHardy if (h > s_h || idx > s_idx) 3726234b27c3SEric Dumazet s_ip_idx = 0; 3727234b27c3SEric Dumazet ip_idx = 0; 3728e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 3729e21e8467SStephen Hemminger if (!idev) 3730234b27c3SEric Dumazet goto cont; 3731234b27c3SEric Dumazet 3732234b27c3SEric Dumazet if (in6_dump_addrs(idev, skb, cb, type, 3733234b27c3SEric Dumazet s_ip_idx, &ip_idx) <= 0) 3734234b27c3SEric Dumazet goto done; 37357562f876SPavel Emelianov cont: 37367562f876SPavel Emelianov idx++; 37371da177e4SLinus Torvalds } 3738234b27c3SEric Dumazet } 3739234b27c3SEric Dumazet done: 3740234b27c3SEric Dumazet rcu_read_unlock(); 3741234b27c3SEric Dumazet cb->args[0] = h; 3742234b27c3SEric Dumazet cb->args[1] = idx; 3743234b27c3SEric Dumazet cb->args[2] = ip_idx; 3744234b27c3SEric Dumazet 37451da177e4SLinus Torvalds return skb->len; 37461da177e4SLinus Torvalds } 37471da177e4SLinus Torvalds 37481da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) 37491da177e4SLinus Torvalds { 37501da177e4SLinus Torvalds enum addr_type_t type = UNICAST_ADDR; 3751b854272bSDenis V. Lunev 37521da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 37531da177e4SLinus Torvalds } 37541da177e4SLinus Torvalds 37551da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb) 37561da177e4SLinus Torvalds { 37571da177e4SLinus Torvalds enum addr_type_t type = MULTICAST_ADDR; 3758b854272bSDenis V. Lunev 37591da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 37601da177e4SLinus Torvalds } 37611da177e4SLinus Torvalds 37621da177e4SLinus Torvalds 37631da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb) 37641da177e4SLinus Torvalds { 37651da177e4SLinus Torvalds enum addr_type_t type = ANYCAST_ADDR; 3766b854272bSDenis V. Lunev 37671da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 37681da177e4SLinus Torvalds } 37691da177e4SLinus Torvalds 37701b29fc2cSThomas Graf static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh, 37711b29fc2cSThomas Graf void *arg) 37726c223828SNoriaki TAKAMIYA { 37733b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 37741b29fc2cSThomas Graf struct ifaddrmsg *ifm; 37751b29fc2cSThomas Graf struct nlattr *tb[IFA_MAX+1]; 37766c223828SNoriaki TAKAMIYA struct in6_addr *addr = NULL; 37776c223828SNoriaki TAKAMIYA struct net_device *dev = NULL; 37786c223828SNoriaki TAKAMIYA struct inet6_ifaddr *ifa; 37796c223828SNoriaki TAKAMIYA struct sk_buff *skb; 37806c223828SNoriaki TAKAMIYA int err; 37816c223828SNoriaki TAKAMIYA 37821b29fc2cSThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 37831b29fc2cSThomas Graf if (err < 0) 37841b29fc2cSThomas Graf goto errout; 37856c223828SNoriaki TAKAMIYA 37861b29fc2cSThomas Graf addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]); 37871b29fc2cSThomas Graf if (addr == NULL) { 37881b29fc2cSThomas Graf err = -EINVAL; 37891b29fc2cSThomas Graf goto errout; 37901b29fc2cSThomas Graf } 37911b29fc2cSThomas Graf 37921b29fc2cSThomas Graf ifm = nlmsg_data(nlh); 37936c223828SNoriaki TAKAMIYA if (ifm->ifa_index) 37946fda7350SBenjamin Thery dev = __dev_get_by_index(net, ifm->ifa_index); 37956c223828SNoriaki TAKAMIYA 3796e21e8467SStephen Hemminger ifa = ipv6_get_ifaddr(net, addr, dev, 1); 3797e21e8467SStephen Hemminger if (!ifa) { 37981b29fc2cSThomas Graf err = -EADDRNOTAVAIL; 37991b29fc2cSThomas Graf goto errout; 38006c223828SNoriaki TAKAMIYA } 38016c223828SNoriaki TAKAMIYA 3802e21e8467SStephen Hemminger skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL); 3803e21e8467SStephen Hemminger if (!skb) { 38041b29fc2cSThomas Graf err = -ENOBUFS; 38051b29fc2cSThomas Graf goto errout_ifa; 38061b29fc2cSThomas Graf } 38071b29fc2cSThomas Graf 380815e47304SEric W. Biederman err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid, 38096c223828SNoriaki TAKAMIYA nlh->nlmsg_seq, RTM_NEWADDR, 0); 381026932566SPatrick McHardy if (err < 0) { 381126932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 381226932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 381326932566SPatrick McHardy kfree_skb(skb); 381426932566SPatrick McHardy goto errout_ifa; 381526932566SPatrick McHardy } 381615e47304SEric W. Biederman err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 38171b29fc2cSThomas Graf errout_ifa: 38186c223828SNoriaki TAKAMIYA in6_ifa_put(ifa); 38191b29fc2cSThomas Graf errout: 38206c223828SNoriaki TAKAMIYA return err; 38216c223828SNoriaki TAKAMIYA } 38226c223828SNoriaki TAKAMIYA 38231da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa) 38241da177e4SLinus Torvalds { 38251da177e4SLinus Torvalds struct sk_buff *skb; 3826c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(ifa->idev->dev); 38275d620266SThomas Graf int err = -ENOBUFS; 38281da177e4SLinus Torvalds 38290ab6803bSThomas Graf skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC); 38305d620266SThomas Graf if (skb == NULL) 38315d620266SThomas Graf goto errout; 38325d620266SThomas Graf 38335d620266SThomas Graf err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0); 383426932566SPatrick McHardy if (err < 0) { 383526932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 383626932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 383726932566SPatrick McHardy kfree_skb(skb); 383826932566SPatrick McHardy goto errout; 383926932566SPatrick McHardy } 38401ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC); 38411ce85fe4SPablo Neira Ayuso return; 38425d620266SThomas Graf errout: 38435d620266SThomas Graf if (err < 0) 38446fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err); 38451da177e4SLinus Torvalds } 38461da177e4SLinus Torvalds 3847b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf, 38481da177e4SLinus Torvalds __s32 *array, int bytes) 38491da177e4SLinus Torvalds { 385004561c1fSThomas Graf BUG_ON(bytes < (DEVCONF_MAX * 4)); 385104561c1fSThomas Graf 38521da177e4SLinus Torvalds memset(array, 0, bytes); 38531da177e4SLinus Torvalds array[DEVCONF_FORWARDING] = cnf->forwarding; 38541da177e4SLinus Torvalds array[DEVCONF_HOPLIMIT] = cnf->hop_limit; 38551da177e4SLinus Torvalds array[DEVCONF_MTU6] = cnf->mtu6; 38561da177e4SLinus Torvalds array[DEVCONF_ACCEPT_RA] = cnf->accept_ra; 38571da177e4SLinus Torvalds array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects; 38581da177e4SLinus Torvalds array[DEVCONF_AUTOCONF] = cnf->autoconf; 38591da177e4SLinus Torvalds array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits; 38601da177e4SLinus Torvalds array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits; 386193908d19SThomas Graf array[DEVCONF_RTR_SOLICIT_INTERVAL] = 386293908d19SThomas Graf jiffies_to_msecs(cnf->rtr_solicit_interval); 386393908d19SThomas Graf array[DEVCONF_RTR_SOLICIT_DELAY] = 386493908d19SThomas Graf jiffies_to_msecs(cnf->rtr_solicit_delay); 38651da177e4SLinus Torvalds array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version; 38661da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 38671da177e4SLinus Torvalds array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr; 38681da177e4SLinus Torvalds array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft; 38691da177e4SLinus Torvalds array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft; 38701da177e4SLinus Torvalds array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry; 38711da177e4SLinus Torvalds array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor; 38721da177e4SLinus Torvalds #endif 38731da177e4SLinus Torvalds array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses; 387465f5c7c1SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr; 3875c4fd30ebSYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo; 3876930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 3877930d6ff2SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref; 387893908d19SThomas Graf array[DEVCONF_RTR_PROBE_INTERVAL] = 387993908d19SThomas Graf jiffies_to_msecs(cnf->rtr_probe_interval); 3880fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 388109c884d4SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen; 388209c884d4SYOSHIFUJI Hideaki #endif 3883930d6ff2SYOSHIFUJI Hideaki #endif 3884fbea49e1SYOSHIFUJI Hideaki array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp; 38850bcbc926SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route; 388695c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 388795c385b4SNeil Horman array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad; 388895c385b4SNeil Horman #endif 38897bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 38907bc570c8SYOSHIFUJI Hideaki array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding; 38917bc570c8SYOSHIFUJI Hideaki #endif 3892778d80beSYOSHIFUJI Hideaki array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6; 38931b34be74SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad; 3894c3faca05SCosmin Ratiu array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao; 38951da177e4SLinus Torvalds } 38961da177e4SLinus Torvalds 3897b382b191SThomas Graf static inline size_t inet6_ifla6_size(void) 3898b382b191SThomas Graf { 3899b382b191SThomas Graf return nla_total_size(4) /* IFLA_INET6_FLAGS */ 3900b382b191SThomas Graf + nla_total_size(sizeof(struct ifla_cacheinfo)) 3901b382b191SThomas Graf + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */ 3902b382b191SThomas Graf + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */ 3903b382b191SThomas Graf + nla_total_size(ICMP6_MIB_MAX * 8); /* IFLA_INET6_ICMP6STATS */ 3904b382b191SThomas Graf } 3905b382b191SThomas Graf 3906339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void) 3907339bf98fSThomas Graf { 3908339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 3909339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 3910339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 3911339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 3912339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 3913b382b191SThomas Graf + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */ 3914339bf98fSThomas Graf } 3915c5396a31SYOSHIFUJI Hideaki 3916be281e55SEric Dumazet static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib, 39177d720c3eSTejun Heo int items, int bytes) 39187f7d9a6bSHerbert Xu { 39197f7d9a6bSHerbert Xu int i; 39207f7d9a6bSHerbert Xu int pad = bytes - sizeof(u64) * items; 39217f7d9a6bSHerbert Xu BUG_ON(pad < 0); 39227f7d9a6bSHerbert Xu 39237f7d9a6bSHerbert Xu /* Use put_unaligned() because stats may not be aligned for u64. */ 39247f7d9a6bSHerbert Xu put_unaligned(items, &stats[0]); 39257f7d9a6bSHerbert Xu for (i = 1; i < items; i++) 3926be281e55SEric Dumazet put_unaligned(atomic_long_read(&mib[i]), &stats[i]); 39277f7d9a6bSHerbert Xu 39287f7d9a6bSHerbert Xu memset(&stats[items], 0, pad); 39297f7d9a6bSHerbert Xu } 39307f7d9a6bSHerbert Xu 39314ce3c183SEric Dumazet static inline void __snmp6_fill_stats64(u64 *stats, void __percpu **mib, 39324ce3c183SEric Dumazet int items, int bytes, size_t syncpoff) 39334ce3c183SEric Dumazet { 39344ce3c183SEric Dumazet int i; 39354ce3c183SEric Dumazet int pad = bytes - sizeof(u64) * items; 39364ce3c183SEric Dumazet BUG_ON(pad < 0); 39374ce3c183SEric Dumazet 39384ce3c183SEric Dumazet /* Use put_unaligned() because stats may not be aligned for u64. */ 39394ce3c183SEric Dumazet put_unaligned(items, &stats[0]); 39404ce3c183SEric Dumazet for (i = 1; i < items; i++) 39414ce3c183SEric Dumazet put_unaligned(snmp_fold_field64(mib, i, syncpoff), &stats[i]); 39424ce3c183SEric Dumazet 39434ce3c183SEric Dumazet memset(&stats[items], 0, pad); 39444ce3c183SEric Dumazet } 39454ce3c183SEric Dumazet 39467f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype, 39477f7d9a6bSHerbert Xu int bytes) 39487f7d9a6bSHerbert Xu { 39497f7d9a6bSHerbert Xu switch (attrtype) { 39507f7d9a6bSHerbert Xu case IFLA_INET6_STATS: 39514ce3c183SEric Dumazet __snmp6_fill_stats64(stats, (void __percpu **)idev->stats.ipv6, 39524ce3c183SEric Dumazet IPSTATS_MIB_MAX, bytes, offsetof(struct ipstats_mib, syncp)); 39537f7d9a6bSHerbert Xu break; 39547f7d9a6bSHerbert Xu case IFLA_INET6_ICMP6STATS: 3955be281e55SEric Dumazet __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, ICMP6_MIB_MAX, bytes); 39567f7d9a6bSHerbert Xu break; 39577f7d9a6bSHerbert Xu } 39587f7d9a6bSHerbert Xu } 39597f7d9a6bSHerbert Xu 3960b382b191SThomas Graf static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev) 39611da177e4SLinus Torvalds { 3962bf99f1bdSYOSHIFUJI Hideaki struct nlattr *nla; 39631da177e4SLinus Torvalds struct ifla_cacheinfo ci; 39641da177e4SLinus Torvalds 3965c78679e8SDavid S. Miller if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags)) 3966c78679e8SDavid S. Miller goto nla_put_failure; 39671da177e4SLinus Torvalds ci.max_reasm_len = IPV6_MAXPLEN; 396824912420SDavid S. Miller ci.tstamp = cstamp_delta(idev->tstamp); 396924912420SDavid S. Miller ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time); 397024912420SDavid S. Miller ci.retrans_time = jiffies_to_msecs(idev->nd_parms->retrans_time); 3971c78679e8SDavid S. Miller if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci)) 3972c78679e8SDavid S. Miller goto nla_put_failure; 3973bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32)); 3974bf99f1bdSYOSHIFUJI Hideaki if (nla == NULL) 397504561c1fSThomas Graf goto nla_put_failure; 3976bf99f1bdSYOSHIFUJI Hideaki ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla)); 39771da177e4SLinus Torvalds 3978bf99f1bdSYOSHIFUJI Hideaki /* XXX - MC not implemented */ 3979bf99f1bdSYOSHIFUJI Hideaki 3980bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64)); 3981bf99f1bdSYOSHIFUJI Hideaki if (nla == NULL) 3982bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 3983bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla)); 3984bf99f1bdSYOSHIFUJI Hideaki 3985bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64)); 3986bf99f1bdSYOSHIFUJI Hideaki if (nla == NULL) 3987bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 3988bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla)); 39891da177e4SLinus Torvalds 3990b382b191SThomas Graf return 0; 3991b382b191SThomas Graf 3992b382b191SThomas Graf nla_put_failure: 3993b382b191SThomas Graf return -EMSGSIZE; 3994b382b191SThomas Graf } 3995b382b191SThomas Graf 3996b382b191SThomas Graf static size_t inet6_get_link_af_size(const struct net_device *dev) 3997b382b191SThomas Graf { 3998b382b191SThomas Graf if (!__in6_dev_get(dev)) 3999b382b191SThomas Graf return 0; 4000b382b191SThomas Graf 4001b382b191SThomas Graf return inet6_ifla6_size(); 4002b382b191SThomas Graf } 4003b382b191SThomas Graf 4004b382b191SThomas Graf static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev) 4005b382b191SThomas Graf { 4006b382b191SThomas Graf struct inet6_dev *idev = __in6_dev_get(dev); 4007b382b191SThomas Graf 4008b382b191SThomas Graf if (!idev) 4009b382b191SThomas Graf return -ENODATA; 4010b382b191SThomas Graf 4011b382b191SThomas Graf if (inet6_fill_ifla6_attrs(skb, idev) < 0) 4012b382b191SThomas Graf return -EMSGSIZE; 4013b382b191SThomas Graf 4014b382b191SThomas Graf return 0; 4015b382b191SThomas Graf } 4016b382b191SThomas Graf 4017b382b191SThomas Graf static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev, 401815e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 4019b382b191SThomas Graf { 4020b382b191SThomas Graf struct net_device *dev = idev->dev; 4021b382b191SThomas Graf struct ifinfomsg *hdr; 4022b382b191SThomas Graf struct nlmsghdr *nlh; 4023b382b191SThomas Graf void *protoinfo; 4024b382b191SThomas Graf 402515e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags); 4026b382b191SThomas Graf if (nlh == NULL) 4027b382b191SThomas Graf return -EMSGSIZE; 4028b382b191SThomas Graf 4029b382b191SThomas Graf hdr = nlmsg_data(nlh); 4030b382b191SThomas Graf hdr->ifi_family = AF_INET6; 4031b382b191SThomas Graf hdr->__ifi_pad = 0; 4032b382b191SThomas Graf hdr->ifi_type = dev->type; 4033b382b191SThomas Graf hdr->ifi_index = dev->ifindex; 4034b382b191SThomas Graf hdr->ifi_flags = dev_get_flags(dev); 4035b382b191SThomas Graf hdr->ifi_change = 0; 4036b382b191SThomas Graf 4037c78679e8SDavid S. Miller if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 4038c78679e8SDavid S. Miller (dev->addr_len && 4039c78679e8SDavid S. Miller nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 4040c78679e8SDavid S. Miller nla_put_u32(skb, IFLA_MTU, dev->mtu) || 4041c78679e8SDavid S. Miller (dev->ifindex != dev->iflink && 4042c78679e8SDavid S. Miller nla_put_u32(skb, IFLA_LINK, dev->iflink))) 4043c78679e8SDavid S. Miller goto nla_put_failure; 4044b382b191SThomas Graf protoinfo = nla_nest_start(skb, IFLA_PROTINFO); 4045b382b191SThomas Graf if (protoinfo == NULL) 4046b382b191SThomas Graf goto nla_put_failure; 4047b382b191SThomas Graf 4048b382b191SThomas Graf if (inet6_fill_ifla6_attrs(skb, idev) < 0) 4049b382b191SThomas Graf goto nla_put_failure; 4050b382b191SThomas Graf 405104561c1fSThomas Graf nla_nest_end(skb, protoinfo); 405204561c1fSThomas Graf return nlmsg_end(skb, nlh); 40531da177e4SLinus Torvalds 405404561c1fSThomas Graf nla_put_failure: 405526932566SPatrick McHardy nlmsg_cancel(skb, nlh); 405626932566SPatrick McHardy return -EMSGSIZE; 40571da177e4SLinus Torvalds } 40581da177e4SLinus Torvalds 40591da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 40601da177e4SLinus Torvalds { 40613b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 406284d2697dSEric Dumazet int h, s_h; 4063434a8a58SDavid S. Miller int idx = 0, s_idx; 40641da177e4SLinus Torvalds struct net_device *dev; 40651da177e4SLinus Torvalds struct inet6_dev *idev; 406684d2697dSEric Dumazet struct hlist_head *head; 406784d2697dSEric Dumazet struct hlist_node *node; 40681da177e4SLinus Torvalds 406984d2697dSEric Dumazet s_h = cb->args[0]; 407084d2697dSEric Dumazet s_idx = cb->args[1]; 407184d2697dSEric Dumazet 407284d2697dSEric Dumazet rcu_read_lock(); 407384d2697dSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 40747562f876SPavel Emelianov idx = 0; 407584d2697dSEric Dumazet head = &net->dev_index_head[h]; 407684d2697dSEric Dumazet hlist_for_each_entry_rcu(dev, node, head, index_hlist) { 40771da177e4SLinus Torvalds if (idx < s_idx) 40787562f876SPavel Emelianov goto cont; 407984d2697dSEric Dumazet idev = __in6_dev_get(dev); 408084d2697dSEric Dumazet if (!idev) 40817562f876SPavel Emelianov goto cont; 408284d2697dSEric Dumazet if (inet6_fill_ifinfo(skb, idev, 408315e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 408484d2697dSEric Dumazet cb->nlh->nlmsg_seq, 408584d2697dSEric Dumazet RTM_NEWLINK, NLM_F_MULTI) <= 0) 408684d2697dSEric Dumazet goto out; 40877562f876SPavel Emelianov cont: 40887562f876SPavel Emelianov idx++; 40891da177e4SLinus Torvalds } 409084d2697dSEric Dumazet } 409184d2697dSEric Dumazet out: 409284d2697dSEric Dumazet rcu_read_unlock(); 409384d2697dSEric Dumazet cb->args[1] = idx; 409484d2697dSEric Dumazet cb->args[0] = h; 40951da177e4SLinus Torvalds 40961da177e4SLinus Torvalds return skb->len; 40971da177e4SLinus Torvalds } 40981da177e4SLinus Torvalds 40991da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev) 41001da177e4SLinus Torvalds { 41011da177e4SLinus Torvalds struct sk_buff *skb; 4102c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 41038d7a76c9SThomas Graf int err = -ENOBUFS; 41041da177e4SLinus Torvalds 4105339bf98fSThomas Graf skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC); 41068d7a76c9SThomas Graf if (skb == NULL) 41078d7a76c9SThomas Graf goto errout; 41088d7a76c9SThomas Graf 41098d7a76c9SThomas Graf err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0); 411026932566SPatrick McHardy if (err < 0) { 411126932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */ 411226932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 411326932566SPatrick McHardy kfree_skb(skb); 411426932566SPatrick McHardy goto errout; 411526932566SPatrick McHardy } 41165f75a104SNicolas Dichtel rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC); 41171ce85fe4SPablo Neira Ayuso return; 41188d7a76c9SThomas Graf errout: 41198d7a76c9SThomas Graf if (err < 0) 41205f75a104SNicolas Dichtel rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err); 41211da177e4SLinus Torvalds } 41221da177e4SLinus Torvalds 4123339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void) 4124339bf98fSThomas Graf { 4125339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct prefixmsg)) 4126339bf98fSThomas Graf + nla_total_size(sizeof(struct in6_addr)) 4127339bf98fSThomas Graf + nla_total_size(sizeof(struct prefix_cacheinfo)); 4128339bf98fSThomas Graf } 4129c5396a31SYOSHIFUJI Hideaki 41301da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev, 413115e47304SEric W. Biederman struct prefix_info *pinfo, u32 portid, u32 seq, 4132b6544c0bSJamal Hadi Salim int event, unsigned int flags) 41331da177e4SLinus Torvalds { 41341da177e4SLinus Torvalds struct prefixmsg *pmsg; 41351da177e4SLinus Torvalds struct nlmsghdr *nlh; 41361da177e4SLinus Torvalds struct prefix_cacheinfo ci; 41371da177e4SLinus Torvalds 413815e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags); 41396051e2f4SThomas Graf if (nlh == NULL) 414026932566SPatrick McHardy return -EMSGSIZE; 41416051e2f4SThomas Graf 41426051e2f4SThomas Graf pmsg = nlmsg_data(nlh); 41431da177e4SLinus Torvalds pmsg->prefix_family = AF_INET6; 41448a47077aSPatrick McHardy pmsg->prefix_pad1 = 0; 41458a47077aSPatrick McHardy pmsg->prefix_pad2 = 0; 41461da177e4SLinus Torvalds pmsg->prefix_ifindex = idev->dev->ifindex; 41471da177e4SLinus Torvalds pmsg->prefix_len = pinfo->prefix_len; 41481da177e4SLinus Torvalds pmsg->prefix_type = pinfo->type; 41498a47077aSPatrick McHardy pmsg->prefix_pad3 = 0; 41501da177e4SLinus Torvalds pmsg->prefix_flags = 0; 41511da177e4SLinus Torvalds if (pinfo->onlink) 41521da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_ONLINK; 41531da177e4SLinus Torvalds if (pinfo->autoconf) 41541da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_AUTOCONF; 41551da177e4SLinus Torvalds 4156c78679e8SDavid S. Miller if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix)) 4157c78679e8SDavid S. Miller goto nla_put_failure; 41581da177e4SLinus Torvalds ci.preferred_time = ntohl(pinfo->prefered); 41591da177e4SLinus Torvalds ci.valid_time = ntohl(pinfo->valid); 4160c78679e8SDavid S. Miller if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci)) 4161c78679e8SDavid S. Miller goto nla_put_failure; 41626051e2f4SThomas Graf return nlmsg_end(skb, nlh); 41631da177e4SLinus Torvalds 41646051e2f4SThomas Graf nla_put_failure: 416526932566SPatrick McHardy nlmsg_cancel(skb, nlh); 416626932566SPatrick McHardy return -EMSGSIZE; 41671da177e4SLinus Torvalds } 41681da177e4SLinus Torvalds 41691da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev, 41701da177e4SLinus Torvalds struct prefix_info *pinfo) 41711da177e4SLinus Torvalds { 41721da177e4SLinus Torvalds struct sk_buff *skb; 4173c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 41748c384bfaSThomas Graf int err = -ENOBUFS; 41751da177e4SLinus Torvalds 4176339bf98fSThomas Graf skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC); 41778c384bfaSThomas Graf if (skb == NULL) 41788c384bfaSThomas Graf goto errout; 41798c384bfaSThomas Graf 41808c384bfaSThomas Graf err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0); 418126932566SPatrick McHardy if (err < 0) { 418226932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */ 418326932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 418426932566SPatrick McHardy kfree_skb(skb); 418526932566SPatrick McHardy goto errout; 418626932566SPatrick McHardy } 41871ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC); 41881ce85fe4SPablo Neira Ayuso return; 41898c384bfaSThomas Graf errout: 41908c384bfaSThomas Graf if (err < 0) 41916fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err); 41921da177e4SLinus Torvalds } 41931da177e4SLinus Torvalds 41941da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 41951da177e4SLinus Torvalds { 41961da177e4SLinus Torvalds inet6_ifa_notify(event ? : RTM_NEWADDR, ifp); 41971da177e4SLinus Torvalds 41981da177e4SLinus Torvalds switch (event) { 41991da177e4SLinus Torvalds case RTM_NEWADDR: 420095c385b4SNeil Horman /* 420195c385b4SNeil Horman * If the address was optimistic 420295c385b4SNeil Horman * we inserted the route at the start of 420395c385b4SNeil Horman * our DAD process, so we don't need 420495c385b4SNeil Horman * to do it again 420595c385b4SNeil Horman */ 420695c385b4SNeil Horman if (!(ifp->rt->rt6i_node)) 420740e22e8fSThomas Graf ip6_ins_rt(ifp->rt); 42081da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 42091da177e4SLinus Torvalds addrconf_join_anycast(ifp); 42101da177e4SLinus Torvalds break; 42111da177e4SLinus Torvalds case RTM_DELADDR: 42121da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 42131da177e4SLinus Torvalds addrconf_leave_anycast(ifp); 42141da177e4SLinus Torvalds addrconf_leave_solict(ifp->idev, &ifp->addr); 4215d8d1f30bSChangli Gao dst_hold(&ifp->rt->dst); 421693fa159aSstephen hemminger 421773a8bd74SDavid S. Miller if (ip6_del_rt(ifp->rt)) 4218d8d1f30bSChangli Gao dst_free(&ifp->rt->dst); 42191da177e4SLinus Torvalds break; 42201da177e4SLinus Torvalds } 42211da177e4SLinus Torvalds } 42221da177e4SLinus Torvalds 42231da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 42241da177e4SLinus Torvalds { 42258814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 42261da177e4SLinus Torvalds if (likely(ifp->idev->dead == 0)) 42271da177e4SLinus Torvalds __ipv6_ifa_notify(event, ifp); 42288814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 42291da177e4SLinus Torvalds } 42301da177e4SLinus Torvalds 42311da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 42321da177e4SLinus Torvalds 42331da177e4SLinus Torvalds static 42348d65af78SAlexey Dobriyan int addrconf_sysctl_forward(ctl_table *ctl, int write, 42351da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 42361da177e4SLinus Torvalds { 42371da177e4SLinus Torvalds int *valp = ctl->data; 42381da177e4SLinus Torvalds int val = *valp; 423988af182eSEric W. Biederman loff_t pos = *ppos; 4240013d97e9SFrancesco Ruggeri ctl_table lctl; 42411da177e4SLinus Torvalds int ret; 42421da177e4SLinus Torvalds 4243013d97e9SFrancesco Ruggeri /* 4244013d97e9SFrancesco Ruggeri * ctl->data points to idev->cnf.forwarding, we should 4245013d97e9SFrancesco Ruggeri * not modify it until we get the rtnl lock. 4246013d97e9SFrancesco Ruggeri */ 4247013d97e9SFrancesco Ruggeri lctl = *ctl; 4248013d97e9SFrancesco Ruggeri lctl.data = &val; 4249013d97e9SFrancesco Ruggeri 4250013d97e9SFrancesco Ruggeri ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 42511da177e4SLinus Torvalds 4252c8fecf22SPavel Emelyanov if (write) 4253b325fddbSStephen Hemminger ret = addrconf_fixup_forwarding(ctl, valp, val); 425488af182eSEric W. Biederman if (ret) 425588af182eSEric W. Biederman *ppos = pos; 42561da177e4SLinus Torvalds return ret; 42571da177e4SLinus Torvalds } 42581da177e4SLinus Torvalds 425956d417b1SBrian Haley static void dev_disable_change(struct inet6_dev *idev) 426056d417b1SBrian Haley { 426156d417b1SBrian Haley if (!idev || !idev->dev) 426256d417b1SBrian Haley return; 426356d417b1SBrian Haley 426456d417b1SBrian Haley if (idev->cnf.disable_ipv6) 426556d417b1SBrian Haley addrconf_notify(NULL, NETDEV_DOWN, idev->dev); 426656d417b1SBrian Haley else 426756d417b1SBrian Haley addrconf_notify(NULL, NETDEV_UP, idev->dev); 426856d417b1SBrian Haley } 426956d417b1SBrian Haley 427056d417b1SBrian Haley static void addrconf_disable_change(struct net *net, __s32 newf) 427156d417b1SBrian Haley { 427256d417b1SBrian Haley struct net_device *dev; 427356d417b1SBrian Haley struct inet6_dev *idev; 427456d417b1SBrian Haley 427556d417b1SBrian Haley rcu_read_lock(); 4276c6d14c84SEric Dumazet for_each_netdev_rcu(net, dev) { 427756d417b1SBrian Haley idev = __in6_dev_get(dev); 427856d417b1SBrian Haley if (idev) { 427956d417b1SBrian Haley int changed = (!idev->cnf.disable_ipv6) ^ (!newf); 428056d417b1SBrian Haley idev->cnf.disable_ipv6 = newf; 428156d417b1SBrian Haley if (changed) 428256d417b1SBrian Haley dev_disable_change(idev); 428356d417b1SBrian Haley } 428456d417b1SBrian Haley } 4285c6d14c84SEric Dumazet rcu_read_unlock(); 428656d417b1SBrian Haley } 428756d417b1SBrian Haley 4288013d97e9SFrancesco Ruggeri static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf) 428956d417b1SBrian Haley { 429056d417b1SBrian Haley struct net *net; 4291013d97e9SFrancesco Ruggeri int old; 4292013d97e9SFrancesco Ruggeri 4293013d97e9SFrancesco Ruggeri if (!rtnl_trylock()) 4294013d97e9SFrancesco Ruggeri return restart_syscall(); 429556d417b1SBrian Haley 429656d417b1SBrian Haley net = (struct net *)table->extra2; 4297013d97e9SFrancesco Ruggeri old = *p; 4298013d97e9SFrancesco Ruggeri *p = newf; 429956d417b1SBrian Haley 4300013d97e9SFrancesco Ruggeri if (p == &net->ipv6.devconf_dflt->disable_ipv6) { 4301013d97e9SFrancesco Ruggeri rtnl_unlock(); 430256d417b1SBrian Haley return 0; 430388af182eSEric W. Biederman } 430456d417b1SBrian Haley 430556d417b1SBrian Haley if (p == &net->ipv6.devconf_all->disable_ipv6) { 430656d417b1SBrian Haley net->ipv6.devconf_dflt->disable_ipv6 = newf; 430756d417b1SBrian Haley addrconf_disable_change(net, newf); 4308013d97e9SFrancesco Ruggeri } else if ((!newf) ^ (!old)) 430956d417b1SBrian Haley dev_disable_change((struct inet6_dev *)table->extra1); 431056d417b1SBrian Haley 431156d417b1SBrian Haley rtnl_unlock(); 431256d417b1SBrian Haley return 0; 431356d417b1SBrian Haley } 431456d417b1SBrian Haley 431556d417b1SBrian Haley static 43168d65af78SAlexey Dobriyan int addrconf_sysctl_disable(ctl_table *ctl, int write, 431756d417b1SBrian Haley void __user *buffer, size_t *lenp, loff_t *ppos) 431856d417b1SBrian Haley { 431956d417b1SBrian Haley int *valp = ctl->data; 432056d417b1SBrian Haley int val = *valp; 432188af182eSEric W. Biederman loff_t pos = *ppos; 4322013d97e9SFrancesco Ruggeri ctl_table lctl; 432356d417b1SBrian Haley int ret; 432456d417b1SBrian Haley 4325013d97e9SFrancesco Ruggeri /* 4326013d97e9SFrancesco Ruggeri * ctl->data points to idev->cnf.disable_ipv6, we should 4327013d97e9SFrancesco Ruggeri * not modify it until we get the rtnl lock. 4328013d97e9SFrancesco Ruggeri */ 4329013d97e9SFrancesco Ruggeri lctl = *ctl; 4330013d97e9SFrancesco Ruggeri lctl.data = &val; 4331013d97e9SFrancesco Ruggeri 4332013d97e9SFrancesco Ruggeri ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 433356d417b1SBrian Haley 433456d417b1SBrian Haley if (write) 433556d417b1SBrian Haley ret = addrconf_disable_ipv6(ctl, valp, val); 433688af182eSEric W. Biederman if (ret) 433788af182eSEric W. Biederman *ppos = pos; 433856d417b1SBrian Haley return ret; 433956d417b1SBrian Haley } 434056d417b1SBrian Haley 43411da177e4SLinus Torvalds static struct addrconf_sysctl_table 43421da177e4SLinus Torvalds { 43431da177e4SLinus Torvalds struct ctl_table_header *sysctl_header; 4344f6a07b29SYOSHIFUJI Hideaki ctl_table addrconf_vars[DEVCONF_MAX+1]; 4345ab32ea5dSBrian Haley } addrconf_sysctl __read_mostly = { 43461da177e4SLinus Torvalds .sysctl_header = NULL, 43471da177e4SLinus Torvalds .addrconf_vars = { 43481da177e4SLinus Torvalds { 43491da177e4SLinus Torvalds .procname = "forwarding", 43501da177e4SLinus Torvalds .data = &ipv6_devconf.forwarding, 43511da177e4SLinus Torvalds .maxlen = sizeof(int), 43521da177e4SLinus Torvalds .mode = 0644, 43536d9f239aSAlexey Dobriyan .proc_handler = addrconf_sysctl_forward, 43541da177e4SLinus Torvalds }, 43551da177e4SLinus Torvalds { 43561da177e4SLinus Torvalds .procname = "hop_limit", 43571da177e4SLinus Torvalds .data = &ipv6_devconf.hop_limit, 43581da177e4SLinus Torvalds .maxlen = sizeof(int), 43591da177e4SLinus Torvalds .mode = 0644, 43601da177e4SLinus Torvalds .proc_handler = proc_dointvec, 43611da177e4SLinus Torvalds }, 43621da177e4SLinus Torvalds { 43631da177e4SLinus Torvalds .procname = "mtu", 43641da177e4SLinus Torvalds .data = &ipv6_devconf.mtu6, 43651da177e4SLinus Torvalds .maxlen = sizeof(int), 43661da177e4SLinus Torvalds .mode = 0644, 43676d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 43681da177e4SLinus Torvalds }, 43691da177e4SLinus Torvalds { 43701da177e4SLinus Torvalds .procname = "accept_ra", 43711da177e4SLinus Torvalds .data = &ipv6_devconf.accept_ra, 43721da177e4SLinus Torvalds .maxlen = sizeof(int), 43731da177e4SLinus Torvalds .mode = 0644, 43746d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 43751da177e4SLinus Torvalds }, 43761da177e4SLinus Torvalds { 43771da177e4SLinus Torvalds .procname = "accept_redirects", 43781da177e4SLinus Torvalds .data = &ipv6_devconf.accept_redirects, 43791da177e4SLinus Torvalds .maxlen = sizeof(int), 43801da177e4SLinus Torvalds .mode = 0644, 43816d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 43821da177e4SLinus Torvalds }, 43831da177e4SLinus Torvalds { 43841da177e4SLinus Torvalds .procname = "autoconf", 43851da177e4SLinus Torvalds .data = &ipv6_devconf.autoconf, 43861da177e4SLinus Torvalds .maxlen = sizeof(int), 43871da177e4SLinus Torvalds .mode = 0644, 43886d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 43891da177e4SLinus Torvalds }, 43901da177e4SLinus Torvalds { 43911da177e4SLinus Torvalds .procname = "dad_transmits", 43921da177e4SLinus Torvalds .data = &ipv6_devconf.dad_transmits, 43931da177e4SLinus Torvalds .maxlen = sizeof(int), 43941da177e4SLinus Torvalds .mode = 0644, 43956d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 43961da177e4SLinus Torvalds }, 43971da177e4SLinus Torvalds { 43981da177e4SLinus Torvalds .procname = "router_solicitations", 43991da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicits, 44001da177e4SLinus Torvalds .maxlen = sizeof(int), 44011da177e4SLinus Torvalds .mode = 0644, 44026d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 44031da177e4SLinus Torvalds }, 44041da177e4SLinus Torvalds { 44051da177e4SLinus Torvalds .procname = "router_solicitation_interval", 44061da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_interval, 44071da177e4SLinus Torvalds .maxlen = sizeof(int), 44081da177e4SLinus Torvalds .mode = 0644, 44096d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 44101da177e4SLinus Torvalds }, 44111da177e4SLinus Torvalds { 44121da177e4SLinus Torvalds .procname = "router_solicitation_delay", 44131da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_delay, 44141da177e4SLinus Torvalds .maxlen = sizeof(int), 44151da177e4SLinus Torvalds .mode = 0644, 44166d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 44171da177e4SLinus Torvalds }, 44181da177e4SLinus Torvalds { 44191da177e4SLinus Torvalds .procname = "force_mld_version", 44201da177e4SLinus Torvalds .data = &ipv6_devconf.force_mld_version, 44211da177e4SLinus Torvalds .maxlen = sizeof(int), 44221da177e4SLinus Torvalds .mode = 0644, 44236d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 44241da177e4SLinus Torvalds }, 44251da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 44261da177e4SLinus Torvalds { 44271da177e4SLinus Torvalds .procname = "use_tempaddr", 44281da177e4SLinus Torvalds .data = &ipv6_devconf.use_tempaddr, 44291da177e4SLinus Torvalds .maxlen = sizeof(int), 44301da177e4SLinus Torvalds .mode = 0644, 44316d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 44321da177e4SLinus Torvalds }, 44331da177e4SLinus Torvalds { 44341da177e4SLinus Torvalds .procname = "temp_valid_lft", 44351da177e4SLinus Torvalds .data = &ipv6_devconf.temp_valid_lft, 44361da177e4SLinus Torvalds .maxlen = sizeof(int), 44371da177e4SLinus Torvalds .mode = 0644, 44386d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 44391da177e4SLinus Torvalds }, 44401da177e4SLinus Torvalds { 44411da177e4SLinus Torvalds .procname = "temp_prefered_lft", 44421da177e4SLinus Torvalds .data = &ipv6_devconf.temp_prefered_lft, 44431da177e4SLinus Torvalds .maxlen = sizeof(int), 44441da177e4SLinus Torvalds .mode = 0644, 44456d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 44461da177e4SLinus Torvalds }, 44471da177e4SLinus Torvalds { 44481da177e4SLinus Torvalds .procname = "regen_max_retry", 44491da177e4SLinus Torvalds .data = &ipv6_devconf.regen_max_retry, 44501da177e4SLinus Torvalds .maxlen = sizeof(int), 44511da177e4SLinus Torvalds .mode = 0644, 44526d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 44531da177e4SLinus Torvalds }, 44541da177e4SLinus Torvalds { 44551da177e4SLinus Torvalds .procname = "max_desync_factor", 44561da177e4SLinus Torvalds .data = &ipv6_devconf.max_desync_factor, 44571da177e4SLinus Torvalds .maxlen = sizeof(int), 44581da177e4SLinus Torvalds .mode = 0644, 44596d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 44601da177e4SLinus Torvalds }, 44611da177e4SLinus Torvalds #endif 44621da177e4SLinus Torvalds { 44631da177e4SLinus Torvalds .procname = "max_addresses", 44641da177e4SLinus Torvalds .data = &ipv6_devconf.max_addresses, 44651da177e4SLinus Torvalds .maxlen = sizeof(int), 44661da177e4SLinus Torvalds .mode = 0644, 44676d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 44681da177e4SLinus Torvalds }, 44691da177e4SLinus Torvalds { 447065f5c7c1SYOSHIFUJI Hideaki .procname = "accept_ra_defrtr", 447165f5c7c1SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_defrtr, 447265f5c7c1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 447365f5c7c1SYOSHIFUJI Hideaki .mode = 0644, 44746d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 447565f5c7c1SYOSHIFUJI Hideaki }, 447665f5c7c1SYOSHIFUJI Hideaki { 4477c4fd30ebSYOSHIFUJI Hideaki .procname = "accept_ra_pinfo", 4478c4fd30ebSYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_pinfo, 4479c4fd30ebSYOSHIFUJI Hideaki .maxlen = sizeof(int), 4480c4fd30ebSYOSHIFUJI Hideaki .mode = 0644, 44816d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 4482c4fd30ebSYOSHIFUJI Hideaki }, 4483930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 4484930d6ff2SYOSHIFUJI Hideaki { 4485930d6ff2SYOSHIFUJI Hideaki .procname = "accept_ra_rtr_pref", 4486930d6ff2SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rtr_pref, 4487930d6ff2SYOSHIFUJI Hideaki .maxlen = sizeof(int), 4488930d6ff2SYOSHIFUJI Hideaki .mode = 0644, 44896d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 4490930d6ff2SYOSHIFUJI Hideaki }, 449152e16356SYOSHIFUJI Hideaki { 449252e16356SYOSHIFUJI Hideaki .procname = "router_probe_interval", 449352e16356SYOSHIFUJI Hideaki .data = &ipv6_devconf.rtr_probe_interval, 449452e16356SYOSHIFUJI Hideaki .maxlen = sizeof(int), 449552e16356SYOSHIFUJI Hideaki .mode = 0644, 44966d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 449752e16356SYOSHIFUJI Hideaki }, 4498fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 449909c884d4SYOSHIFUJI Hideaki { 450009c884d4SYOSHIFUJI Hideaki .procname = "accept_ra_rt_info_max_plen", 450109c884d4SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rt_info_max_plen, 450209c884d4SYOSHIFUJI Hideaki .maxlen = sizeof(int), 450309c884d4SYOSHIFUJI Hideaki .mode = 0644, 45046d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 450509c884d4SYOSHIFUJI Hideaki }, 450609c884d4SYOSHIFUJI Hideaki #endif 4507930d6ff2SYOSHIFUJI Hideaki #endif 4508c4fd30ebSYOSHIFUJI Hideaki { 4509fbea49e1SYOSHIFUJI Hideaki .procname = "proxy_ndp", 4510fbea49e1SYOSHIFUJI Hideaki .data = &ipv6_devconf.proxy_ndp, 4511fbea49e1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 4512fbea49e1SYOSHIFUJI Hideaki .mode = 0644, 45136d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 4514fbea49e1SYOSHIFUJI Hideaki }, 4515fbea49e1SYOSHIFUJI Hideaki { 45160bcbc926SYOSHIFUJI Hideaki .procname = "accept_source_route", 45170bcbc926SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_source_route, 45180bcbc926SYOSHIFUJI Hideaki .maxlen = sizeof(int), 45190bcbc926SYOSHIFUJI Hideaki .mode = 0644, 45206d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 45210bcbc926SYOSHIFUJI Hideaki }, 452295c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 452395c385b4SNeil Horman { 452495c385b4SNeil Horman .procname = "optimistic_dad", 452595c385b4SNeil Horman .data = &ipv6_devconf.optimistic_dad, 452695c385b4SNeil Horman .maxlen = sizeof(int), 452795c385b4SNeil Horman .mode = 0644, 45286d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 452995c385b4SNeil Horman 453095c385b4SNeil Horman }, 453195c385b4SNeil Horman #endif 45327bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 45337bc570c8SYOSHIFUJI Hideaki { 45347bc570c8SYOSHIFUJI Hideaki .procname = "mc_forwarding", 45357bc570c8SYOSHIFUJI Hideaki .data = &ipv6_devconf.mc_forwarding, 45367bc570c8SYOSHIFUJI Hideaki .maxlen = sizeof(int), 4537a4e6db07SDavid S. Miller .mode = 0444, 45386d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 45397bc570c8SYOSHIFUJI Hideaki }, 45407bc570c8SYOSHIFUJI Hideaki #endif 45410bcbc926SYOSHIFUJI Hideaki { 4542778d80beSYOSHIFUJI Hideaki .procname = "disable_ipv6", 4543778d80beSYOSHIFUJI Hideaki .data = &ipv6_devconf.disable_ipv6, 4544778d80beSYOSHIFUJI Hideaki .maxlen = sizeof(int), 4545778d80beSYOSHIFUJI Hideaki .mode = 0644, 454656d417b1SBrian Haley .proc_handler = addrconf_sysctl_disable, 4547778d80beSYOSHIFUJI Hideaki }, 4548778d80beSYOSHIFUJI Hideaki { 45491b34be74SYOSHIFUJI Hideaki .procname = "accept_dad", 45501b34be74SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_dad, 45511b34be74SYOSHIFUJI Hideaki .maxlen = sizeof(int), 45521b34be74SYOSHIFUJI Hideaki .mode = 0644, 45536d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 45541b34be74SYOSHIFUJI Hideaki }, 45551b34be74SYOSHIFUJI Hideaki { 4556f7734fdfSOctavian Purdila .procname = "force_tllao", 4557f7734fdfSOctavian Purdila .data = &ipv6_devconf.force_tllao, 4558f7734fdfSOctavian Purdila .maxlen = sizeof(int), 4559f7734fdfSOctavian Purdila .mode = 0644, 4560f7734fdfSOctavian Purdila .proc_handler = proc_dointvec 4561f7734fdfSOctavian Purdila }, 4562f7734fdfSOctavian Purdila { 4563f8572d8fSEric W. Biederman /* sentinel */ 45641da177e4SLinus Torvalds } 45651da177e4SLinus Torvalds }, 45661da177e4SLinus Torvalds }; 45671da177e4SLinus Torvalds 4568bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name, 4569f8572d8fSEric W. Biederman struct inet6_dev *idev, struct ipv6_devconf *p) 45701da177e4SLinus Torvalds { 45711da177e4SLinus Torvalds int i; 45721da177e4SLinus Torvalds struct addrconf_sysctl_table *t; 45736105e293SEric W. Biederman char path[sizeof("net/ipv6/conf/") + IFNAMSIZ]; 45741dab6222SPavel Emelyanov 4575af879cc7SArnaldo Carvalho de Melo t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL); 45761da177e4SLinus Torvalds if (t == NULL) 4577f68635e6SPavel Emelyanov goto out; 4578f68635e6SPavel Emelyanov 45791da177e4SLinus Torvalds for (i = 0; t->addrconf_vars[i].data; i++) { 45801da177e4SLinus Torvalds t->addrconf_vars[i].data += (char *)p - (char *)&ipv6_devconf; 45811da177e4SLinus Torvalds t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */ 4582bff16c2fSPavel Emelyanov t->addrconf_vars[i].extra2 = net; 45831da177e4SLinus Torvalds } 45841da177e4SLinus Torvalds 45856105e293SEric W. Biederman snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name); 45861da177e4SLinus Torvalds 45876105e293SEric W. Biederman t->sysctl_header = register_net_sysctl(net, path, t->addrconf_vars); 45881da177e4SLinus Torvalds if (t->sysctl_header == NULL) 45896105e293SEric W. Biederman goto free; 4590f68635e6SPavel Emelyanov 45911da177e4SLinus Torvalds p->sysctl = t; 459295897312SPavel Emelyanov return 0; 45931da177e4SLinus Torvalds 45941da177e4SLinus Torvalds free: 45951da177e4SLinus Torvalds kfree(t); 4596f68635e6SPavel Emelyanov out: 459795897312SPavel Emelyanov return -ENOBUFS; 45981da177e4SLinus Torvalds } 45991da177e4SLinus Torvalds 4600408c4768SPavel Emelyanov static void __addrconf_sysctl_unregister(struct ipv6_devconf *p) 4601f52295a9SPavel Emelyanov { 4602408c4768SPavel Emelyanov struct addrconf_sysctl_table *t; 4603f52295a9SPavel Emelyanov 4604408c4768SPavel Emelyanov if (p->sysctl == NULL) 4605408c4768SPavel Emelyanov return; 4606408c4768SPavel Emelyanov 4607408c4768SPavel Emelyanov t = p->sysctl; 46081da177e4SLinus Torvalds p->sysctl = NULL; 4609ff538818SLucian Adrian Grijincu unregister_net_sysctl_table(t->sysctl_header); 46101da177e4SLinus Torvalds kfree(t); 46111da177e4SLinus Torvalds } 4612408c4768SPavel Emelyanov 4613408c4768SPavel Emelyanov static void addrconf_sysctl_register(struct inet6_dev *idev) 4614408c4768SPavel Emelyanov { 461554716e3bSEric W. Biederman neigh_sysctl_register(idev->dev, idev->nd_parms, "ipv6", 4616f8572d8fSEric W. Biederman &ndisc_ifinfo_sysctl_change); 4617c346dca1SYOSHIFUJI Hideaki __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name, 4618f8572d8fSEric W. Biederman idev, &idev->cnf); 4619408c4768SPavel Emelyanov } 4620408c4768SPavel Emelyanov 4621408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev) 4622408c4768SPavel Emelyanov { 4623408c4768SPavel Emelyanov __addrconf_sysctl_unregister(&idev->cnf); 4624408c4768SPavel Emelyanov neigh_sysctl_unregister(idev->nd_parms); 46251da177e4SLinus Torvalds } 46261da177e4SLinus Torvalds 46271da177e4SLinus Torvalds 46281da177e4SLinus Torvalds #endif 46291da177e4SLinus Torvalds 46302c8c1e72SAlexey Dobriyan static int __net_init addrconf_init_net(struct net *net) 4631e0da5a48SPavel Emelyanov { 4632e0da5a48SPavel Emelyanov int err; 4633e0da5a48SPavel Emelyanov struct ipv6_devconf *all, *dflt; 4634e0da5a48SPavel Emelyanov 4635e0da5a48SPavel Emelyanov err = -ENOMEM; 4636e0da5a48SPavel Emelyanov all = &ipv6_devconf; 4637e0da5a48SPavel Emelyanov dflt = &ipv6_devconf_dflt; 4638e0da5a48SPavel Emelyanov 463909ad9bc7SOctavian Purdila if (!net_eq(net, &init_net)) { 4640e0da5a48SPavel Emelyanov all = kmemdup(all, sizeof(ipv6_devconf), GFP_KERNEL); 4641e0da5a48SPavel Emelyanov if (all == NULL) 4642e0da5a48SPavel Emelyanov goto err_alloc_all; 4643e0da5a48SPavel Emelyanov 4644e0da5a48SPavel Emelyanov dflt = kmemdup(dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL); 4645e0da5a48SPavel Emelyanov if (dflt == NULL) 4646e0da5a48SPavel Emelyanov goto err_alloc_dflt; 464756d417b1SBrian Haley } else { 464856d417b1SBrian Haley /* these will be inherited by all namespaces */ 464956d417b1SBrian Haley dflt->autoconf = ipv6_defaults.autoconf; 465056d417b1SBrian Haley dflt->disable_ipv6 = ipv6_defaults.disable_ipv6; 4651e0da5a48SPavel Emelyanov } 4652e0da5a48SPavel Emelyanov 4653e0da5a48SPavel Emelyanov net->ipv6.devconf_all = all; 4654e0da5a48SPavel Emelyanov net->ipv6.devconf_dflt = dflt; 4655e0da5a48SPavel Emelyanov 4656e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 4657f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "all", NULL, all); 4658e0da5a48SPavel Emelyanov if (err < 0) 4659e0da5a48SPavel Emelyanov goto err_reg_all; 4660e0da5a48SPavel Emelyanov 4661f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "default", NULL, dflt); 4662e0da5a48SPavel Emelyanov if (err < 0) 4663e0da5a48SPavel Emelyanov goto err_reg_dflt; 4664e0da5a48SPavel Emelyanov #endif 4665e0da5a48SPavel Emelyanov return 0; 4666e0da5a48SPavel Emelyanov 4667e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 4668e0da5a48SPavel Emelyanov err_reg_dflt: 4669e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(all); 4670e0da5a48SPavel Emelyanov err_reg_all: 4671e0da5a48SPavel Emelyanov kfree(dflt); 4672e0da5a48SPavel Emelyanov #endif 4673e0da5a48SPavel Emelyanov err_alloc_dflt: 4674e0da5a48SPavel Emelyanov kfree(all); 4675e0da5a48SPavel Emelyanov err_alloc_all: 4676e0da5a48SPavel Emelyanov return err; 4677e0da5a48SPavel Emelyanov } 4678e0da5a48SPavel Emelyanov 46792c8c1e72SAlexey Dobriyan static void __net_exit addrconf_exit_net(struct net *net) 4680e0da5a48SPavel Emelyanov { 4681e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 4682e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(net->ipv6.devconf_dflt); 4683e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(net->ipv6.devconf_all); 4684e0da5a48SPavel Emelyanov #endif 468509ad9bc7SOctavian Purdila if (!net_eq(net, &init_net)) { 4686e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_dflt); 4687e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_all); 4688e0da5a48SPavel Emelyanov } 4689e0da5a48SPavel Emelyanov } 4690e0da5a48SPavel Emelyanov 4691e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = { 4692e0da5a48SPavel Emelyanov .init = addrconf_init_net, 4693e0da5a48SPavel Emelyanov .exit = addrconf_exit_net, 4694e0da5a48SPavel Emelyanov }; 4695e0da5a48SPavel Emelyanov 46961da177e4SLinus Torvalds /* 46971da177e4SLinus Torvalds * Device notifier 46981da177e4SLinus Torvalds */ 46991da177e4SLinus Torvalds 47001da177e4SLinus Torvalds int register_inet6addr_notifier(struct notifier_block *nb) 47011da177e4SLinus Torvalds { 4702e041c683SAlan Stern return atomic_notifier_chain_register(&inet6addr_chain, nb); 47031da177e4SLinus Torvalds } 47047159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(register_inet6addr_notifier); 47057159039aSYOSHIFUJI Hideaki 47061da177e4SLinus Torvalds int unregister_inet6addr_notifier(struct notifier_block *nb) 47071da177e4SLinus Torvalds { 4708e041c683SAlan Stern return atomic_notifier_chain_unregister(&inet6addr_chain, nb); 47091da177e4SLinus Torvalds } 47107159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(unregister_inet6addr_notifier); 47117159039aSYOSHIFUJI Hideaki 4712b382b191SThomas Graf static struct rtnl_af_ops inet6_ops = { 4713b382b191SThomas Graf .family = AF_INET6, 4714b382b191SThomas Graf .fill_link_af = inet6_fill_link_af, 4715b382b191SThomas Graf .get_link_af_size = inet6_get_link_af_size, 4716b382b191SThomas Graf }; 4717b382b191SThomas Graf 47181da177e4SLinus Torvalds /* 47191da177e4SLinus Torvalds * Init / cleanup code 47201da177e4SLinus Torvalds */ 47211da177e4SLinus Torvalds 47221da177e4SLinus Torvalds int __init addrconf_init(void) 47231da177e4SLinus Torvalds { 4724c2e21293Sstephen hemminger int i, err; 47252a8cc6c8SYOSHIFUJI Hideaki 4726e21e8467SStephen Hemminger err = ipv6_addr_label_init(); 4727e21e8467SStephen Hemminger if (err < 0) { 4728f3213831SJoe Perches pr_crit("%s: cannot initialize default policy table: %d\n", 4729f3213831SJoe Perches __func__, err); 47302cc6d2bfSNeil Horman goto out; 47312a8cc6c8SYOSHIFUJI Hideaki } 47321da177e4SLinus Torvalds 47332cc6d2bfSNeil Horman err = register_pernet_subsys(&addrconf_ops); 47342cc6d2bfSNeil Horman if (err < 0) 47352cc6d2bfSNeil Horman goto out_addrlabel; 4736e0da5a48SPavel Emelyanov 47371da177e4SLinus Torvalds /* The addrconf netdev notifier requires that loopback_dev 47381da177e4SLinus Torvalds * has it's ipv6 private information allocated and setup 47391da177e4SLinus Torvalds * before it can bring up and give link-local addresses 47401da177e4SLinus Torvalds * to other devices which are up. 47411da177e4SLinus Torvalds * 47421da177e4SLinus Torvalds * Unfortunately, loopback_dev is not necessarily the first 47431da177e4SLinus Torvalds * entry in the global dev_base list of net devices. In fact, 47441da177e4SLinus Torvalds * it is likely to be the very last entry on that list. 47451da177e4SLinus Torvalds * So this causes the notifier registry below to try and 47461da177e4SLinus Torvalds * give link-local addresses to all devices besides loopback_dev 47471da177e4SLinus Torvalds * first, then loopback_dev, which cases all the non-loopback_dev 47481da177e4SLinus Torvalds * devices to fail to get a link-local address. 47491da177e4SLinus Torvalds * 47501da177e4SLinus Torvalds * So, as a temporary fix, allocate the ipv6 structure for 47511da177e4SLinus Torvalds * loopback_dev first by hand. 47521da177e4SLinus Torvalds * Longer term, all of the dependencies ipv6 has upon the loopback 47531da177e4SLinus Torvalds * device and it being up should be removed. 47541da177e4SLinus Torvalds */ 47551da177e4SLinus Torvalds rtnl_lock(); 47562774c7abSEric W. Biederman if (!ipv6_add_dev(init_net.loopback_dev)) 47571da177e4SLinus Torvalds err = -ENOMEM; 47581da177e4SLinus Torvalds rtnl_unlock(); 47591da177e4SLinus Torvalds if (err) 4760e0da5a48SPavel Emelyanov goto errlo; 47611da177e4SLinus Torvalds 4762c2e21293Sstephen hemminger for (i = 0; i < IN6_ADDR_HSIZE; i++) 4763c2e21293Sstephen hemminger INIT_HLIST_HEAD(&inet6_addr_lst[i]); 4764c2e21293Sstephen hemminger 47651da177e4SLinus Torvalds register_netdevice_notifier(&ipv6_dev_notf); 47661da177e4SLinus Torvalds 47671da177e4SLinus Torvalds addrconf_verify(0); 4768c127ea2cSThomas Graf 4769b382b191SThomas Graf err = rtnl_af_register(&inet6_ops); 4770b382b191SThomas Graf if (err < 0) 4771b382b191SThomas Graf goto errout_af; 4772b382b191SThomas Graf 4773c7ac8679SGreg Rose err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo, 4774c7ac8679SGreg Rose NULL); 4775c127ea2cSThomas Graf if (err < 0) 4776c127ea2cSThomas Graf goto errout; 4777c127ea2cSThomas Graf 4778c127ea2cSThomas Graf /* Only the first call to __rtnl_register can fail */ 4779c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL); 4780c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL); 4781c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr, 4782c7ac8679SGreg Rose inet6_dump_ifaddr, NULL); 4783c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL, 4784c7ac8679SGreg Rose inet6_dump_ifmcaddr, NULL); 4785c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL, 4786c7ac8679SGreg Rose inet6_dump_ifacaddr, NULL); 4787c127ea2cSThomas Graf 47882a8cc6c8SYOSHIFUJI Hideaki ipv6_addr_label_rtnl_register(); 47892a8cc6c8SYOSHIFUJI Hideaki 47901da177e4SLinus Torvalds return 0; 4791c127ea2cSThomas Graf errout: 4792b382b191SThomas Graf rtnl_af_unregister(&inet6_ops); 4793b382b191SThomas Graf errout_af: 4794c127ea2cSThomas Graf unregister_netdevice_notifier(&ipv6_dev_notf); 4795e0da5a48SPavel Emelyanov errlo: 4796e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 47972cc6d2bfSNeil Horman out_addrlabel: 47982cc6d2bfSNeil Horman ipv6_addr_label_cleanup(); 47992cc6d2bfSNeil Horman out: 4800c127ea2cSThomas Graf return err; 48011da177e4SLinus Torvalds } 48021da177e4SLinus Torvalds 480309f7709fSDaniel Lezcano void addrconf_cleanup(void) 48041da177e4SLinus Torvalds { 4805176c39afSDaniel Lezcano struct net_device *dev; 48061da177e4SLinus Torvalds int i; 48071da177e4SLinus Torvalds 48081da177e4SLinus Torvalds unregister_netdevice_notifier(&ipv6_dev_notf); 4809e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 48102cc6d2bfSNeil Horman ipv6_addr_label_cleanup(); 48111da177e4SLinus Torvalds 48121da177e4SLinus Torvalds rtnl_lock(); 48131da177e4SLinus Torvalds 4814b382b191SThomas Graf __rtnl_af_unregister(&inet6_ops); 4815b382b191SThomas Graf 4816176c39afSDaniel Lezcano /* clean dev list */ 4817176c39afSDaniel Lezcano for_each_netdev(&init_net, dev) { 4818176c39afSDaniel Lezcano if (__in6_dev_get(dev) == NULL) 4819176c39afSDaniel Lezcano continue; 4820176c39afSDaniel Lezcano addrconf_ifdown(dev, 1); 4821176c39afSDaniel Lezcano } 4822176c39afSDaniel Lezcano addrconf_ifdown(init_net.loopback_dev, 2); 4823176c39afSDaniel Lezcano 48241da177e4SLinus Torvalds /* 48251da177e4SLinus Torvalds * Check hash table. 48261da177e4SLinus Torvalds */ 48275c578aedSstephen hemminger spin_lock_bh(&addrconf_hash_lock); 4828c2e21293Sstephen hemminger for (i = 0; i < IN6_ADDR_HSIZE; i++) 4829c2e21293Sstephen hemminger WARN_ON(!hlist_empty(&inet6_addr_lst[i])); 48305c578aedSstephen hemminger spin_unlock_bh(&addrconf_hash_lock); 48311da177e4SLinus Torvalds 48321da177e4SLinus Torvalds del_timer(&addr_chk_timer); 48331da177e4SLinus Torvalds rtnl_unlock(); 48341da177e4SLinus Torvalds } 4835