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 411da177e4SLinus Torvalds #include <linux/errno.h> 421da177e4SLinus Torvalds #include <linux/types.h> 43a0bffffcSIlpo Järvinen #include <linux/kernel.h> 441da177e4SLinus Torvalds #include <linux/socket.h> 451da177e4SLinus Torvalds #include <linux/sockios.h> 461da177e4SLinus Torvalds #include <linux/net.h> 471da177e4SLinus Torvalds #include <linux/in6.h> 481da177e4SLinus Torvalds #include <linux/netdevice.h> 491823730fSThomas Graf #include <linux/if_addr.h> 501da177e4SLinus Torvalds #include <linux/if_arp.h> 511da177e4SLinus Torvalds #include <linux/if_arcnet.h> 521da177e4SLinus Torvalds #include <linux/if_infiniband.h> 531da177e4SLinus Torvalds #include <linux/route.h> 541da177e4SLinus Torvalds #include <linux/inetdevice.h> 551da177e4SLinus Torvalds #include <linux/init.h> 565a0e3ad6STejun Heo #include <linux/slab.h> 571da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 581da177e4SLinus Torvalds #include <linux/sysctl.h> 591da177e4SLinus Torvalds #endif 604fc268d2SRandy Dunlap #include <linux/capability.h> 611da177e4SLinus Torvalds #include <linux/delay.h> 621da177e4SLinus Torvalds #include <linux/notifier.h> 63543537bdSPaulo Marques #include <linux/string.h> 641da177e4SLinus Torvalds 65457c4cbcSEric W. Biederman #include <net/net_namespace.h> 661da177e4SLinus Torvalds #include <net/sock.h> 671da177e4SLinus Torvalds #include <net/snmp.h> 681da177e4SLinus Torvalds 691da177e4SLinus Torvalds #include <net/ipv6.h> 701da177e4SLinus Torvalds #include <net/protocol.h> 711da177e4SLinus Torvalds #include <net/ndisc.h> 721da177e4SLinus Torvalds #include <net/ip6_route.h> 731da177e4SLinus Torvalds #include <net/addrconf.h> 741da177e4SLinus Torvalds #include <net/tcp.h> 751da177e4SLinus Torvalds #include <net/ip.h> 765d620266SThomas Graf #include <net/netlink.h> 77f24e3d65SMitsuru Chinen #include <net/pkt_sched.h> 781da177e4SLinus Torvalds #include <linux/if_tunnel.h> 791da177e4SLinus Torvalds #include <linux/rtnetlink.h> 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 821da177e4SLinus Torvalds #include <linux/random.h> 831da177e4SLinus Torvalds #endif 841da177e4SLinus Torvalds 85e21e8467SStephen Hemminger #include <linux/uaccess.h> 867f7d9a6bSHerbert Xu #include <asm/unaligned.h> 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds #include <linux/proc_fs.h> 891da177e4SLinus Torvalds #include <linux/seq_file.h> 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds /* Set to 3 to get tracing... */ 921da177e4SLinus Torvalds #define ACONF_DEBUG 2 931da177e4SLinus Torvalds 941da177e4SLinus Torvalds #if ACONF_DEBUG >= 3 951da177e4SLinus Torvalds #define ADBG(x) printk x 961da177e4SLinus Torvalds #else 971da177e4SLinus Torvalds #define ADBG(x) 981da177e4SLinus Torvalds #endif 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds #define INFINITY_LIFE_TIME 0xFFFFFFFF 10118a31e1eSThomas Graf 10218a31e1eSThomas Graf static inline u32 cstamp_delta(unsigned long cstamp) 10318a31e1eSThomas Graf { 10418a31e1eSThomas Graf return (cstamp - INITIAL_JIFFIES) * 100UL / HZ; 10518a31e1eSThomas Graf } 1061da177e4SLinus Torvalds 107b2db7564SYOSHIFUJI Hideaki #define ADDRCONF_TIMER_FUZZ_MINUS (HZ > 50 ? HZ/50 : 1) 108b2db7564SYOSHIFUJI Hideaki #define ADDRCONF_TIMER_FUZZ (HZ / 4) 109b2db7564SYOSHIFUJI Hideaki #define ADDRCONF_TIMER_FUZZ_MAX (HZ) 110b2db7564SYOSHIFUJI Hideaki 1111da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 112f52295a9SPavel Emelyanov static void addrconf_sysctl_register(struct inet6_dev *idev); 113408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev); 114408c4768SPavel Emelyanov #else 115408c4768SPavel Emelyanov static inline void addrconf_sysctl_register(struct inet6_dev *idev) 116408c4768SPavel Emelyanov { 117408c4768SPavel Emelyanov } 118408c4768SPavel Emelyanov 119408c4768SPavel Emelyanov static inline void addrconf_sysctl_unregister(struct inet6_dev *idev) 120408c4768SPavel Emelyanov { 121408c4768SPavel Emelyanov } 1221da177e4SLinus Torvalds #endif 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 1251da177e4SLinus Torvalds static int __ipv6_regen_rndid(struct inet6_dev *idev); 1261da177e4SLinus Torvalds static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr); 1271da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data); 1281da177e4SLinus Torvalds #endif 1291da177e4SLinus Torvalds 1301b34be74SYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev); 1311da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev); 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds /* 1341da177e4SLinus Torvalds * Configured unicast address hash table 1351da177e4SLinus Torvalds */ 136c2e21293Sstephen hemminger static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE]; 1375c578aedSstephen hemminger static DEFINE_SPINLOCK(addrconf_hash_lock); 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds static void addrconf_verify(unsigned long); 1401da177e4SLinus Torvalds 1418d06afabSIngo Molnar static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0); 1421da177e4SLinus Torvalds static DEFINE_SPINLOCK(addrconf_verify_lock); 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds static void addrconf_join_anycast(struct inet6_ifaddr *ifp); 1451da177e4SLinus Torvalds static void addrconf_leave_anycast(struct inet6_ifaddr *ifp); 1461da177e4SLinus Torvalds 14793d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, 14875c78500SMoni Shoua unsigned long event); 1491da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how); 1501da177e4SLinus Torvalds 151e431b8c0SJamal Hadi Salim static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags); 1521da177e4SLinus Torvalds static void addrconf_dad_timer(unsigned long data); 1531da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp); 154c5e33bddSYOSHIFUJI Hideaki static void addrconf_dad_run(struct inet6_dev *idev); 1551da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data); 1561da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); 1571da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); 1581da177e4SLinus Torvalds 1591da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev, 1601da177e4SLinus Torvalds struct prefix_info *pinfo); 1613e81c6daSDavid S. Miller static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr, 16206bfe655SDaniel Lezcano struct net_device *dev); 1631da177e4SLinus Torvalds 164e041c683SAlan Stern static ATOMIC_NOTIFIER_HEAD(inet6addr_chain); 1651da177e4SLinus Torvalds 166888c848eSAdrian Bunk static struct ipv6_devconf ipv6_devconf __read_mostly = { 1671da177e4SLinus Torvalds .forwarding = 0, 1681da177e4SLinus Torvalds .hop_limit = IPV6_DEFAULT_HOPLIMIT, 1691da177e4SLinus Torvalds .mtu6 = IPV6_MIN_MTU, 1701da177e4SLinus Torvalds .accept_ra = 1, 1711da177e4SLinus Torvalds .accept_redirects = 1, 1721da177e4SLinus Torvalds .autoconf = 1, 1731da177e4SLinus Torvalds .force_mld_version = 0, 1741da177e4SLinus Torvalds .dad_transmits = 1, 1751da177e4SLinus Torvalds .rtr_solicits = MAX_RTR_SOLICITATIONS, 1761da177e4SLinus Torvalds .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, 1771da177e4SLinus Torvalds .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, 1781da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 1791da177e4SLinus Torvalds .use_tempaddr = 0, 1801da177e4SLinus Torvalds .temp_valid_lft = TEMP_VALID_LIFETIME, 1811da177e4SLinus Torvalds .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, 1821da177e4SLinus Torvalds .regen_max_retry = REGEN_MAX_RETRY, 1831da177e4SLinus Torvalds .max_desync_factor = MAX_DESYNC_FACTOR, 1841da177e4SLinus Torvalds #endif 1851da177e4SLinus Torvalds .max_addresses = IPV6_MAX_ADDRESSES, 18665f5c7c1SYOSHIFUJI Hideaki .accept_ra_defrtr = 1, 187c4fd30ebSYOSHIFUJI Hideaki .accept_ra_pinfo = 1, 188930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 189930d6ff2SYOSHIFUJI Hideaki .accept_ra_rtr_pref = 1, 19052e16356SYOSHIFUJI Hideaki .rtr_probe_interval = 60 * HZ, 19109c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 19209c884d4SYOSHIFUJI Hideaki .accept_ra_rt_info_max_plen = 0, 19309c884d4SYOSHIFUJI Hideaki #endif 194930d6ff2SYOSHIFUJI Hideaki #endif 195fbea49e1SYOSHIFUJI Hideaki .proxy_ndp = 0, 1960bcbc926SYOSHIFUJI Hideaki .accept_source_route = 0, /* we do not accept RH0 by default. */ 197778d80beSYOSHIFUJI Hideaki .disable_ipv6 = 0, 1981b34be74SYOSHIFUJI Hideaki .accept_dad = 1, 1991da177e4SLinus Torvalds }; 2001da177e4SLinus Torvalds 201ab32ea5dSBrian Haley static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = { 2021da177e4SLinus Torvalds .forwarding = 0, 2031da177e4SLinus Torvalds .hop_limit = IPV6_DEFAULT_HOPLIMIT, 2041da177e4SLinus Torvalds .mtu6 = IPV6_MIN_MTU, 2051da177e4SLinus Torvalds .accept_ra = 1, 2061da177e4SLinus Torvalds .accept_redirects = 1, 2071da177e4SLinus Torvalds .autoconf = 1, 2081da177e4SLinus Torvalds .dad_transmits = 1, 2091da177e4SLinus Torvalds .rtr_solicits = MAX_RTR_SOLICITATIONS, 2101da177e4SLinus Torvalds .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, 2111da177e4SLinus Torvalds .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, 2121da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 2131da177e4SLinus Torvalds .use_tempaddr = 0, 2141da177e4SLinus Torvalds .temp_valid_lft = TEMP_VALID_LIFETIME, 2151da177e4SLinus Torvalds .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, 2161da177e4SLinus Torvalds .regen_max_retry = REGEN_MAX_RETRY, 2171da177e4SLinus Torvalds .max_desync_factor = MAX_DESYNC_FACTOR, 2181da177e4SLinus Torvalds #endif 2191da177e4SLinus Torvalds .max_addresses = IPV6_MAX_ADDRESSES, 22065f5c7c1SYOSHIFUJI Hideaki .accept_ra_defrtr = 1, 221c4fd30ebSYOSHIFUJI Hideaki .accept_ra_pinfo = 1, 222930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 223930d6ff2SYOSHIFUJI Hideaki .accept_ra_rtr_pref = 1, 22452e16356SYOSHIFUJI Hideaki .rtr_probe_interval = 60 * HZ, 22509c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 22609c884d4SYOSHIFUJI Hideaki .accept_ra_rt_info_max_plen = 0, 22709c884d4SYOSHIFUJI Hideaki #endif 228930d6ff2SYOSHIFUJI Hideaki #endif 229fbea49e1SYOSHIFUJI Hideaki .proxy_ndp = 0, 2300bcbc926SYOSHIFUJI Hideaki .accept_source_route = 0, /* we do not accept RH0 by default. */ 231778d80beSYOSHIFUJI Hideaki .disable_ipv6 = 0, 2321b34be74SYOSHIFUJI Hideaki .accept_dad = 1, 2331da177e4SLinus Torvalds }; 2341da177e4SLinus Torvalds 2351da177e4SLinus Torvalds /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */ 2361da177e4SLinus Torvalds const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; 2371da177e4SLinus Torvalds const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT; 238f3ee4010SYOSHIFUJI Hideaki const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT; 239f3ee4010SYOSHIFUJI Hideaki const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT; 2401da177e4SLinus Torvalds 241f24e3d65SMitsuru Chinen /* Check if a valid qdisc is available */ 24205297949SDavid S. Miller static inline bool addrconf_qdisc_ok(const struct net_device *dev) 243f24e3d65SMitsuru Chinen { 24405297949SDavid S. Miller return !qdisc_tx_is_noop(dev); 245f24e3d65SMitsuru Chinen } 246f24e3d65SMitsuru Chinen 2472b5ead46SYOSHIFUJI Hideaki /* Check if a route is valid prefix route */ 2482b5ead46SYOSHIFUJI Hideaki static inline int addrconf_is_prefix_route(const struct rt6_info *rt) 2492b5ead46SYOSHIFUJI Hideaki { 250a02cec21SEric Dumazet return (rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0; 2512b5ead46SYOSHIFUJI Hideaki } 2522b5ead46SYOSHIFUJI Hideaki 2531da177e4SLinus Torvalds static void addrconf_del_timer(struct inet6_ifaddr *ifp) 2541da177e4SLinus Torvalds { 2551da177e4SLinus Torvalds if (del_timer(&ifp->timer)) 2561da177e4SLinus Torvalds __in6_ifa_put(ifp); 2571da177e4SLinus Torvalds } 2581da177e4SLinus Torvalds 259e21e8467SStephen Hemminger enum addrconf_timer_t { 2601da177e4SLinus Torvalds AC_NONE, 2611da177e4SLinus Torvalds AC_DAD, 2621da177e4SLinus Torvalds AC_RS, 2631da177e4SLinus Torvalds }; 2641da177e4SLinus Torvalds 2651da177e4SLinus Torvalds static void addrconf_mod_timer(struct inet6_ifaddr *ifp, 2661da177e4SLinus Torvalds enum addrconf_timer_t what, 2671da177e4SLinus Torvalds unsigned long when) 2681da177e4SLinus Torvalds { 2691da177e4SLinus Torvalds if (!del_timer(&ifp->timer)) 2701da177e4SLinus Torvalds in6_ifa_hold(ifp); 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds switch (what) { 2731da177e4SLinus Torvalds case AC_DAD: 2741da177e4SLinus Torvalds ifp->timer.function = addrconf_dad_timer; 2751da177e4SLinus Torvalds break; 2761da177e4SLinus Torvalds case AC_RS: 2771da177e4SLinus Torvalds ifp->timer.function = addrconf_rs_timer; 2781da177e4SLinus Torvalds break; 279e21e8467SStephen Hemminger default: 280e21e8467SStephen Hemminger break; 2811da177e4SLinus Torvalds } 2821da177e4SLinus Torvalds ifp->timer.expires = jiffies + when; 2831da177e4SLinus Torvalds add_timer(&ifp->timer); 2841da177e4SLinus Torvalds } 2851da177e4SLinus Torvalds 2867f7d9a6bSHerbert Xu static int snmp6_alloc_dev(struct inet6_dev *idev) 2877f7d9a6bSHerbert Xu { 2887d720c3eSTejun Heo if (snmp_mib_init((void __percpu **)idev->stats.ipv6, 2891823e4c8SEric Dumazet sizeof(struct ipstats_mib), 2901823e4c8SEric Dumazet __alignof__(struct ipstats_mib)) < 0) 2917f7d9a6bSHerbert Xu goto err_ip; 2927d720c3eSTejun Heo if (snmp_mib_init((void __percpu **)idev->stats.icmpv6, 2931823e4c8SEric Dumazet sizeof(struct icmpv6_mib), 2941823e4c8SEric Dumazet __alignof__(struct icmpv6_mib)) < 0) 2957f7d9a6bSHerbert Xu goto err_icmp; 2967d720c3eSTejun Heo if (snmp_mib_init((void __percpu **)idev->stats.icmpv6msg, 2971823e4c8SEric Dumazet sizeof(struct icmpv6msg_mib), 2981823e4c8SEric Dumazet __alignof__(struct icmpv6msg_mib)) < 0) 29914878f75SDavid L Stevens goto err_icmpmsg; 3007f7d9a6bSHerbert Xu 3017f7d9a6bSHerbert Xu return 0; 3027f7d9a6bSHerbert Xu 30314878f75SDavid L Stevens err_icmpmsg: 3047d720c3eSTejun Heo snmp_mib_free((void __percpu **)idev->stats.icmpv6); 3057f7d9a6bSHerbert Xu err_icmp: 3067d720c3eSTejun Heo snmp_mib_free((void __percpu **)idev->stats.ipv6); 3077f7d9a6bSHerbert Xu err_ip: 308aaf70ec7SPavel Emelyanov return -ENOMEM; 3097f7d9a6bSHerbert Xu } 3107f7d9a6bSHerbert Xu 31116910b98SPavel Emelyanov static void snmp6_free_dev(struct inet6_dev *idev) 3127f7d9a6bSHerbert Xu { 3137d720c3eSTejun Heo snmp_mib_free((void __percpu **)idev->stats.icmpv6msg); 3147d720c3eSTejun Heo snmp_mib_free((void __percpu **)idev->stats.icmpv6); 3157d720c3eSTejun Heo snmp_mib_free((void __percpu **)idev->stats.ipv6); 3167f7d9a6bSHerbert Xu } 3177f7d9a6bSHerbert Xu 3181da177e4SLinus Torvalds /* Nobody refers to this device, we may destroy it. */ 3191da177e4SLinus Torvalds 3208814c4b5SYOSHIFUJI Hideaki static void in6_dev_finish_destroy_rcu(struct rcu_head *head) 3218814c4b5SYOSHIFUJI Hideaki { 3228814c4b5SYOSHIFUJI Hideaki struct inet6_dev *idev = container_of(head, struct inet6_dev, rcu); 3238814c4b5SYOSHIFUJI Hideaki kfree(idev); 3248814c4b5SYOSHIFUJI Hideaki } 3258814c4b5SYOSHIFUJI Hideaki 3261da177e4SLinus Torvalds void in6_dev_finish_destroy(struct inet6_dev *idev) 3271da177e4SLinus Torvalds { 3281da177e4SLinus Torvalds struct net_device *dev = idev->dev; 329547b792cSIlpo Järvinen 330502a2ffdSstephen hemminger WARN_ON(!list_empty(&idev->addr_list)); 331547b792cSIlpo Järvinen WARN_ON(idev->mc_list != NULL); 332547b792cSIlpo Järvinen 3331da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG 3341da177e4SLinus Torvalds printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL"); 3351da177e4SLinus Torvalds #endif 3361da177e4SLinus Torvalds dev_put(dev); 3371da177e4SLinus Torvalds if (!idev->dead) { 338e21e8467SStephen Hemminger pr_warning("Freeing alive inet6 device %p\n", idev); 3391da177e4SLinus Torvalds return; 3401da177e4SLinus Torvalds } 3411da177e4SLinus Torvalds snmp6_free_dev(idev); 3428814c4b5SYOSHIFUJI Hideaki call_rcu(&idev->rcu, in6_dev_finish_destroy_rcu); 3431da177e4SLinus Torvalds } 3441da177e4SLinus Torvalds 3457159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(in6_dev_finish_destroy); 3467159039aSYOSHIFUJI Hideaki 3471da177e4SLinus Torvalds static struct inet6_dev * ipv6_add_dev(struct net_device *dev) 3481da177e4SLinus Torvalds { 3491da177e4SLinus Torvalds struct inet6_dev *ndev; 3501da177e4SLinus Torvalds 3511da177e4SLinus Torvalds ASSERT_RTNL(); 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds if (dev->mtu < IPV6_MIN_MTU) 3541da177e4SLinus Torvalds return NULL; 3551da177e4SLinus Torvalds 356322f74a4SIngo Oeser ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL); 3571da177e4SLinus Torvalds 358322f74a4SIngo Oeser if (ndev == NULL) 359322f74a4SIngo Oeser return NULL; 3601da177e4SLinus Torvalds 3611da177e4SLinus Torvalds rwlock_init(&ndev->lock); 3621da177e4SLinus Torvalds ndev->dev = dev; 363502a2ffdSstephen hemminger INIT_LIST_HEAD(&ndev->addr_list); 364502a2ffdSstephen hemminger 365c346dca1SYOSHIFUJI Hideaki memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf)); 3661da177e4SLinus Torvalds ndev->cnf.mtu6 = dev->mtu; 3671da177e4SLinus Torvalds ndev->cnf.sysctl = NULL; 3681da177e4SLinus Torvalds ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl); 3691da177e4SLinus Torvalds if (ndev->nd_parms == NULL) { 3701da177e4SLinus Torvalds kfree(ndev); 3711da177e4SLinus Torvalds return NULL; 3721da177e4SLinus Torvalds } 3730187bdfbSBen Hutchings if (ndev->cnf.forwarding) 3740187bdfbSBen Hutchings dev_disable_lro(dev); 3751da177e4SLinus Torvalds /* We refer to the device */ 3761da177e4SLinus Torvalds dev_hold(dev); 3771da177e4SLinus Torvalds 3781da177e4SLinus Torvalds if (snmp6_alloc_dev(ndev) < 0) { 3791da177e4SLinus Torvalds ADBG((KERN_WARNING 3801da177e4SLinus Torvalds "%s(): cannot allocate memory for statistics; dev=%s.\n", 3810dc47877SHarvey Harrison __func__, dev->name)); 3821da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, ndev->nd_parms); 3831da177e4SLinus Torvalds ndev->dead = 1; 3841da177e4SLinus Torvalds in6_dev_finish_destroy(ndev); 3851da177e4SLinus Torvalds return NULL; 3861da177e4SLinus Torvalds } 3871da177e4SLinus Torvalds 3881da177e4SLinus Torvalds if (snmp6_register_dev(ndev) < 0) { 3891da177e4SLinus Torvalds ADBG((KERN_WARNING 3901da177e4SLinus Torvalds "%s(): cannot create /proc/net/dev_snmp6/%s\n", 3910dc47877SHarvey Harrison __func__, dev->name)); 3921da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, ndev->nd_parms); 3931da177e4SLinus Torvalds ndev->dead = 1; 3941da177e4SLinus Torvalds in6_dev_finish_destroy(ndev); 3951da177e4SLinus Torvalds return NULL; 3961da177e4SLinus Torvalds } 3971da177e4SLinus Torvalds 3981da177e4SLinus Torvalds /* One reference from device. We must do this before 3991da177e4SLinus Torvalds * we invoke __ipv6_regen_rndid(). 4001da177e4SLinus Torvalds */ 4011da177e4SLinus Torvalds in6_dev_hold(ndev); 4021da177e4SLinus Torvalds 4031b34be74SYOSHIFUJI Hideaki if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) 4041b34be74SYOSHIFUJI Hideaki ndev->cnf.accept_dad = -1; 4051b34be74SYOSHIFUJI Hideaki 4060be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 407c7dc89c0SFred L. Templin if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) { 408c7dc89c0SFred L. Templin printk(KERN_INFO 409c7dc89c0SFred L. Templin "%s: Disabled Multicast RS\n", 410c7dc89c0SFred L. Templin dev->name); 411c7dc89c0SFred L. Templin ndev->cnf.rtr_solicits = 0; 412c7dc89c0SFred L. Templin } 413b077d7abSYOSHIFUJI Hideaki #endif 414b077d7abSYOSHIFUJI Hideaki 4151da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 416372e6c8fSstephen hemminger INIT_LIST_HEAD(&ndev->tempaddr_list); 4171da177e4SLinus Torvalds setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev); 4181da177e4SLinus Torvalds if ((dev->flags&IFF_LOOPBACK) || 4191da177e4SLinus Torvalds dev->type == ARPHRD_TUNNEL || 4209625ed72SYOSHIFUJI Hideaki dev->type == ARPHRD_TUNNEL6 || 4211da177e4SLinus Torvalds dev->type == ARPHRD_SIT || 4221da177e4SLinus Torvalds dev->type == ARPHRD_NONE) { 4230be669bbSJoerg Roedel ndev->cnf.use_tempaddr = -1; 4241da177e4SLinus Torvalds } else { 4251da177e4SLinus Torvalds in6_dev_hold(ndev); 4261da177e4SLinus Torvalds ipv6_regen_rndid((unsigned long) ndev); 4271da177e4SLinus Torvalds } 4281da177e4SLinus Torvalds #endif 4291da177e4SLinus Torvalds 430f24e3d65SMitsuru Chinen if (netif_running(dev) && addrconf_qdisc_ok(dev)) 43153aadcc9SHerbert Xu ndev->if_flags |= IF_READY; 43253aadcc9SHerbert Xu 4331da177e4SLinus Torvalds ipv6_mc_init_dev(ndev); 4341da177e4SLinus Torvalds ndev->tstamp = jiffies; 435f52295a9SPavel Emelyanov addrconf_sysctl_register(ndev); 43630c4cf57SDavid L Stevens /* protected by rtnl_lock */ 43730c4cf57SDavid L Stevens rcu_assign_pointer(dev->ip6_ptr, ndev); 438d88ae4ccSYOSHIFUJI Hideaki 439d88ae4ccSYOSHIFUJI Hideaki /* Join all-node multicast group */ 440f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes); 441d88ae4ccSYOSHIFUJI Hideaki 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 4978814c4b5SYOSHIFUJI Hideaki rcu_read_lock(); 498c6d14c84SEric Dumazet for_each_netdev_rcu(net, dev) { 4991da177e4SLinus Torvalds idev = __in6_dev_get(dev); 5001da177e4SLinus Torvalds if (idev) { 501e186932bSPavel Emelyanov int changed = (!idev->cnf.forwarding) ^ (!newf); 502e186932bSPavel Emelyanov idev->cnf.forwarding = newf; 5031da177e4SLinus Torvalds if (changed) 5041da177e4SLinus Torvalds dev_forward_change(idev); 5051da177e4SLinus Torvalds } 5061da177e4SLinus Torvalds } 507c6d14c84SEric Dumazet rcu_read_unlock(); 5081da177e4SLinus Torvalds } 509c8fecf22SPavel Emelyanov 510b325fddbSStephen Hemminger static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int old) 511c8fecf22SPavel Emelyanov { 512bff16c2fSPavel Emelyanov struct net *net; 513bff16c2fSPavel Emelyanov 514bff16c2fSPavel Emelyanov net = (struct net *)table->extra2; 515441fc2a2SPavel Emelyanov if (p == &net->ipv6.devconf_dflt->forwarding) 516b325fddbSStephen Hemminger return 0; 517c8fecf22SPavel Emelyanov 51888af182eSEric W. Biederman if (!rtnl_trylock()) { 51988af182eSEric W. Biederman /* Restore the original values before restarting */ 52088af182eSEric W. Biederman *p = old; 5215007392dSEric W. Biederman return restart_syscall(); 52288af182eSEric W. Biederman } 523b325fddbSStephen Hemminger 524e186932bSPavel Emelyanov if (p == &net->ipv6.devconf_all->forwarding) { 525e186932bSPavel Emelyanov __s32 newf = net->ipv6.devconf_all->forwarding; 526e186932bSPavel Emelyanov net->ipv6.devconf_dflt->forwarding = newf; 527e186932bSPavel Emelyanov addrconf_forward_change(net, newf); 528c8fecf22SPavel Emelyanov } else if ((!*p) ^ (!old)) 529c8fecf22SPavel Emelyanov dev_forward_change((struct inet6_dev *)table->extra1); 5300187bdfbSBen Hutchings rtnl_unlock(); 531c8fecf22SPavel Emelyanov 532c8fecf22SPavel Emelyanov if (*p) 5337b4da532SDaniel Lezcano rt6_purge_dflt_routers(net); 534b325fddbSStephen Hemminger return 1; 535c8fecf22SPavel Emelyanov } 5361da177e4SLinus Torvalds #endif 5371da177e4SLinus Torvalds 5385c578aedSstephen hemminger static void inet6_ifa_finish_destroy_rcu(struct rcu_head *head) 5395c578aedSstephen hemminger { 5405c578aedSstephen hemminger struct inet6_ifaddr *ifp = container_of(head, struct inet6_ifaddr, rcu); 5415c578aedSstephen hemminger kfree(ifp); 5425c578aedSstephen hemminger } 5431da177e4SLinus Torvalds 5445c578aedSstephen hemminger /* Nobody refers to this ifaddr, destroy it */ 5451da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp) 5461da177e4SLinus Torvalds { 547c2e21293Sstephen hemminger WARN_ON(!hlist_unhashed(&ifp->addr_lst)); 548547b792cSIlpo Järvinen 5491da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG 5501da177e4SLinus Torvalds printk(KERN_DEBUG "inet6_ifa_finish_destroy\n"); 5511da177e4SLinus Torvalds #endif 5521da177e4SLinus Torvalds 5531da177e4SLinus Torvalds in6_dev_put(ifp->idev); 5541da177e4SLinus Torvalds 5551da177e4SLinus Torvalds if (del_timer(&ifp->timer)) 556e21e8467SStephen Hemminger pr_notice("Timer is still running, when freeing ifa=%p\n", ifp); 5571da177e4SLinus Torvalds 558e9d3e084SHerbert Xu if (ifp->state != INET6_IFADDR_STATE_DEAD) { 559e21e8467SStephen Hemminger pr_warning("Freeing alive inet6 address %p\n", ifp); 5601da177e4SLinus Torvalds return; 5611da177e4SLinus Torvalds } 562d8d1f30bSChangli Gao dst_release(&ifp->rt->dst); 5631da177e4SLinus Torvalds 5645c578aedSstephen hemminger call_rcu(&ifp->rcu, inet6_ifa_finish_destroy_rcu); 5651da177e4SLinus Torvalds } 5661da177e4SLinus Torvalds 567e55ffac6SBrian Haley static void 568e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp) 569e55ffac6SBrian Haley { 570502a2ffdSstephen hemminger struct list_head *p; 5718a6ce0c0SYOSHIFUJI Hideaki int ifp_scope = ipv6_addr_src_scope(&ifp->addr); 572e55ffac6SBrian Haley 573e55ffac6SBrian Haley /* 574e55ffac6SBrian Haley * Each device address list is sorted in order of scope - 575e55ffac6SBrian Haley * global before linklocal. 576e55ffac6SBrian Haley */ 577502a2ffdSstephen hemminger list_for_each(p, &idev->addr_list) { 578502a2ffdSstephen hemminger struct inet6_ifaddr *ifa 579502a2ffdSstephen hemminger = list_entry(p, struct inet6_ifaddr, if_list); 5808a6ce0c0SYOSHIFUJI Hideaki if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr)) 581e55ffac6SBrian Haley break; 582e55ffac6SBrian Haley } 583e55ffac6SBrian Haley 584b54c9b98SDavid S. Miller list_add_tail(&ifp->if_list, p); 585e55ffac6SBrian Haley } 586e55ffac6SBrian Haley 5873a88a81dSstephen hemminger static u32 ipv6_addr_hash(const struct in6_addr *addr) 5883eb84f49SYOSHIFUJI Hideaki { 5893eb84f49SYOSHIFUJI Hideaki /* 5903eb84f49SYOSHIFUJI Hideaki * We perform the hash function over the last 64 bits of the address 5913eb84f49SYOSHIFUJI Hideaki * This will include the IEEE address token on links that support it. 5923eb84f49SYOSHIFUJI Hideaki */ 5930eae88f3SEric Dumazet return jhash_2words((__force u32)addr->s6_addr32[2], 5940eae88f3SEric Dumazet (__force u32)addr->s6_addr32[3], 0) 5953a88a81dSstephen hemminger & (IN6_ADDR_HSIZE - 1); 5963eb84f49SYOSHIFUJI Hideaki } 5973eb84f49SYOSHIFUJI Hideaki 5981da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */ 5991da177e4SLinus Torvalds 6001da177e4SLinus Torvalds static struct inet6_ifaddr * 6011da177e4SLinus Torvalds ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen, 602e431b8c0SJamal Hadi Salim int scope, u32 flags) 6031da177e4SLinus Torvalds { 6041da177e4SLinus Torvalds struct inet6_ifaddr *ifa = NULL; 6051da177e4SLinus Torvalds struct rt6_info *rt; 6063a88a81dSstephen hemminger unsigned int hash; 6071da177e4SLinus Torvalds int err = 0; 608d68b8270SYOSHIFUJI Hideaki int addr_type = ipv6_addr_type(addr); 609d68b8270SYOSHIFUJI Hideaki 610d68b8270SYOSHIFUJI Hideaki if (addr_type == IPV6_ADDR_ANY || 611d68b8270SYOSHIFUJI Hideaki addr_type & IPV6_ADDR_MULTICAST || 612d68b8270SYOSHIFUJI Hideaki (!(idev->dev->flags & IFF_LOOPBACK) && 613d68b8270SYOSHIFUJI Hideaki addr_type & IPV6_ADDR_LOOPBACK)) 614d68b8270SYOSHIFUJI Hideaki return ERR_PTR(-EADDRNOTAVAIL); 6151da177e4SLinus Torvalds 6168814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 6171da177e4SLinus Torvalds if (idev->dead) { 6181da177e4SLinus Torvalds err = -ENODEV; /*XXX*/ 6191da177e4SLinus Torvalds goto out2; 6201da177e4SLinus Torvalds } 6211da177e4SLinus Torvalds 62256d417b1SBrian Haley if (idev->cnf.disable_ipv6) { 6239bdd8d40SBrian Haley err = -EACCES; 6249bdd8d40SBrian Haley goto out2; 6259bdd8d40SBrian Haley } 6269bdd8d40SBrian Haley 6275c578aedSstephen hemminger spin_lock(&addrconf_hash_lock); 6281da177e4SLinus Torvalds 6291da177e4SLinus Torvalds /* Ignore adding duplicate addresses on an interface */ 630c346dca1SYOSHIFUJI Hideaki if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) { 6311da177e4SLinus Torvalds ADBG(("ipv6_add_addr: already assigned\n")); 6321da177e4SLinus Torvalds err = -EEXIST; 6331da177e4SLinus Torvalds goto out; 6341da177e4SLinus Torvalds } 6351da177e4SLinus Torvalds 636322f74a4SIngo Oeser ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC); 6371da177e4SLinus Torvalds 6381da177e4SLinus Torvalds if (ifa == NULL) { 6391da177e4SLinus Torvalds ADBG(("ipv6_add_addr: malloc failed\n")); 6401da177e4SLinus Torvalds err = -ENOBUFS; 6411da177e4SLinus Torvalds goto out; 6421da177e4SLinus Torvalds } 6431da177e4SLinus Torvalds 6441da177e4SLinus Torvalds rt = addrconf_dst_alloc(idev, addr, 0); 6451da177e4SLinus Torvalds if (IS_ERR(rt)) { 6461da177e4SLinus Torvalds err = PTR_ERR(rt); 6471da177e4SLinus Torvalds goto out; 6481da177e4SLinus Torvalds } 6491da177e4SLinus Torvalds 6501da177e4SLinus Torvalds ipv6_addr_copy(&ifa->addr, addr); 6511da177e4SLinus Torvalds 6521da177e4SLinus Torvalds spin_lock_init(&ifa->lock); 653e9d3e084SHerbert Xu spin_lock_init(&ifa->state_lock); 6541da177e4SLinus Torvalds init_timer(&ifa->timer); 655c2e21293Sstephen hemminger INIT_HLIST_NODE(&ifa->addr_lst); 6561da177e4SLinus Torvalds ifa->timer.data = (unsigned long) ifa; 6571da177e4SLinus Torvalds ifa->scope = scope; 6581da177e4SLinus Torvalds ifa->prefix_len = pfxlen; 6591da177e4SLinus Torvalds ifa->flags = flags | IFA_F_TENTATIVE; 6601da177e4SLinus Torvalds ifa->cstamp = ifa->tstamp = jiffies; 6611da177e4SLinus Torvalds 66257f5f544SKeir Fraser ifa->rt = rt; 66357f5f544SKeir Fraser 66495c385b4SNeil Horman /* 66595c385b4SNeil Horman * part one of RFC 4429, section 3.3 66695c385b4SNeil Horman * We should not configure an address as 66795c385b4SNeil Horman * optimistic if we do not yet know the link 66895c385b4SNeil Horman * layer address of our nexhop router 66995c385b4SNeil Horman */ 67095c385b4SNeil Horman 67195c385b4SNeil Horman if (rt->rt6i_nexthop == NULL) 67295c385b4SNeil Horman ifa->flags &= ~IFA_F_OPTIMISTIC; 67395c385b4SNeil Horman 6741da177e4SLinus Torvalds ifa->idev = idev; 6751da177e4SLinus Torvalds in6_dev_hold(idev); 6761da177e4SLinus Torvalds /* For caller */ 6771da177e4SLinus Torvalds in6_ifa_hold(ifa); 6781da177e4SLinus Torvalds 6791da177e4SLinus Torvalds /* Add to big hash table */ 6801da177e4SLinus Torvalds hash = ipv6_addr_hash(addr); 6811da177e4SLinus Torvalds 6825c578aedSstephen hemminger hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]); 6835c578aedSstephen hemminger spin_unlock(&addrconf_hash_lock); 6841da177e4SLinus Torvalds 6851da177e4SLinus Torvalds write_lock(&idev->lock); 6861da177e4SLinus Torvalds /* Add to inet6_dev unicast addr list. */ 687e55ffac6SBrian Haley ipv6_link_dev_addr(idev, ifa); 6881da177e4SLinus Torvalds 6891da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 6901da177e4SLinus Torvalds if (ifa->flags&IFA_F_TEMPORARY) { 691372e6c8fSstephen hemminger list_add(&ifa->tmp_list, &idev->tempaddr_list); 6921da177e4SLinus Torvalds in6_ifa_hold(ifa); 6931da177e4SLinus Torvalds } 6941da177e4SLinus Torvalds #endif 6951da177e4SLinus Torvalds 6961da177e4SLinus Torvalds in6_ifa_hold(ifa); 6971da177e4SLinus Torvalds write_unlock(&idev->lock); 6981da177e4SLinus Torvalds out2: 6998814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 7001da177e4SLinus Torvalds 701fd92833aSYOSHIFUJI Hideaki if (likely(err == 0)) 702e041c683SAlan Stern atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa); 7031da177e4SLinus Torvalds else { 7041da177e4SLinus Torvalds kfree(ifa); 7051da177e4SLinus Torvalds ifa = ERR_PTR(err); 7061da177e4SLinus Torvalds } 7071da177e4SLinus Torvalds 7081da177e4SLinus Torvalds return ifa; 7091da177e4SLinus Torvalds out: 7105c578aedSstephen hemminger spin_unlock(&addrconf_hash_lock); 7111da177e4SLinus Torvalds goto out2; 7121da177e4SLinus Torvalds } 7131da177e4SLinus Torvalds 7141da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */ 7151da177e4SLinus Torvalds 7161da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp) 7171da177e4SLinus Torvalds { 718502a2ffdSstephen hemminger struct inet6_ifaddr *ifa, *ifn; 7191da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 7204c5ff6a6SHerbert Xu int state; 7211da177e4SLinus Torvalds int deleted = 0, onlink = 0; 7221da177e4SLinus Torvalds unsigned long expires = jiffies; 7231da177e4SLinus Torvalds 7244c5ff6a6SHerbert Xu spin_lock_bh(&ifp->state_lock); 7254c5ff6a6SHerbert Xu state = ifp->state; 726e9d3e084SHerbert Xu ifp->state = INET6_IFADDR_STATE_DEAD; 7274c5ff6a6SHerbert Xu spin_unlock_bh(&ifp->state_lock); 7284c5ff6a6SHerbert Xu 7294c5ff6a6SHerbert Xu if (state == INET6_IFADDR_STATE_DEAD) 7304c5ff6a6SHerbert Xu goto out; 7311da177e4SLinus Torvalds 7325c578aedSstephen hemminger spin_lock_bh(&addrconf_hash_lock); 7335c578aedSstephen hemminger hlist_del_init_rcu(&ifp->addr_lst); 7345c578aedSstephen hemminger spin_unlock_bh(&addrconf_hash_lock); 7351da177e4SLinus Torvalds 7361da177e4SLinus Torvalds write_lock_bh(&idev->lock); 7371da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 7381da177e4SLinus Torvalds if (ifp->flags&IFA_F_TEMPORARY) { 739372e6c8fSstephen hemminger list_del(&ifp->tmp_list); 7401da177e4SLinus Torvalds if (ifp->ifpub) { 7411da177e4SLinus Torvalds in6_ifa_put(ifp->ifpub); 7421da177e4SLinus Torvalds ifp->ifpub = NULL; 7431da177e4SLinus Torvalds } 7441da177e4SLinus Torvalds __in6_ifa_put(ifp); 7451da177e4SLinus Torvalds } 7461da177e4SLinus Torvalds #endif 7471da177e4SLinus Torvalds 748502a2ffdSstephen hemminger list_for_each_entry_safe(ifa, ifn, &idev->addr_list, if_list) { 7491da177e4SLinus Torvalds if (ifa == ifp) { 750502a2ffdSstephen hemminger list_del_init(&ifp->if_list); 7511da177e4SLinus Torvalds __in6_ifa_put(ifp); 752502a2ffdSstephen hemminger 7531da177e4SLinus Torvalds if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0) 7541da177e4SLinus Torvalds break; 7551da177e4SLinus Torvalds deleted = 1; 7561d142804SKristian Slavov continue; 7571da177e4SLinus Torvalds } else if (ifp->flags & IFA_F_PERMANENT) { 7581da177e4SLinus Torvalds if (ipv6_prefix_equal(&ifa->addr, &ifp->addr, 7591da177e4SLinus Torvalds ifp->prefix_len)) { 7601da177e4SLinus Torvalds if (ifa->flags & IFA_F_PERMANENT) { 7611da177e4SLinus Torvalds onlink = 1; 7621da177e4SLinus Torvalds if (deleted) 7631da177e4SLinus Torvalds break; 7641da177e4SLinus Torvalds } else { 7651da177e4SLinus Torvalds unsigned long lifetime; 7661da177e4SLinus Torvalds 7671da177e4SLinus Torvalds if (!onlink) 7681da177e4SLinus Torvalds onlink = -1; 7691da177e4SLinus Torvalds 7701da177e4SLinus Torvalds spin_lock(&ifa->lock); 7714bed72e4SYOSHIFUJI Hideaki 7724bed72e4SYOSHIFUJI Hideaki lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ); 7734bed72e4SYOSHIFUJI Hideaki /* 7744bed72e4SYOSHIFUJI Hideaki * Note: Because this address is 7754bed72e4SYOSHIFUJI Hideaki * not permanent, lifetime < 7764bed72e4SYOSHIFUJI Hideaki * LONG_MAX / HZ here. 7774bed72e4SYOSHIFUJI Hideaki */ 7781da177e4SLinus Torvalds if (time_before(expires, 7791da177e4SLinus Torvalds ifa->tstamp + lifetime * HZ)) 7801da177e4SLinus Torvalds expires = ifa->tstamp + lifetime * HZ; 7811da177e4SLinus Torvalds spin_unlock(&ifa->lock); 7821da177e4SLinus Torvalds } 7831da177e4SLinus Torvalds } 7841da177e4SLinus Torvalds } 7851da177e4SLinus Torvalds } 7861da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 7871da177e4SLinus Torvalds 788b2238566SAndrey Vagin addrconf_del_timer(ifp); 789b2238566SAndrey Vagin 7901da177e4SLinus Torvalds ipv6_ifa_notify(RTM_DELADDR, ifp); 7911da177e4SLinus Torvalds 792e041c683SAlan Stern atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp); 7931da177e4SLinus Torvalds 7941da177e4SLinus Torvalds /* 7951da177e4SLinus Torvalds * Purge or update corresponding prefix 7961da177e4SLinus Torvalds * 7971da177e4SLinus Torvalds * 1) we don't purge prefix here if address was not permanent. 7981da177e4SLinus Torvalds * prefix is managed by its own lifetime. 7991da177e4SLinus Torvalds * 2) if there're no addresses, delete prefix. 8001da177e4SLinus Torvalds * 3) if there're still other permanent address(es), 8011da177e4SLinus Torvalds * corresponding prefix is still permanent. 8021da177e4SLinus Torvalds * 4) otherwise, update prefix lifetime to the 8031da177e4SLinus Torvalds * longest valid lifetime among the corresponding 8041da177e4SLinus Torvalds * addresses on the device. 8051da177e4SLinus Torvalds * Note: subsequent RA will update lifetime. 8061da177e4SLinus Torvalds * 8071da177e4SLinus Torvalds * --yoshfuji 8081da177e4SLinus Torvalds */ 8091da177e4SLinus Torvalds if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) { 8101da177e4SLinus Torvalds struct in6_addr prefix; 8111da177e4SLinus Torvalds struct rt6_info *rt; 812c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(ifp->idev->dev); 8131da177e4SLinus Torvalds ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len); 8146fda7350SBenjamin Thery rt = rt6_lookup(net, &prefix, NULL, ifp->idev->dev->ifindex, 1); 8151da177e4SLinus Torvalds 8162b5ead46SYOSHIFUJI Hideaki if (rt && addrconf_is_prefix_route(rt)) { 8171da177e4SLinus Torvalds if (onlink == 0) { 818e0a1ad73SThomas Graf ip6_del_rt(rt); 8191da177e4SLinus Torvalds rt = NULL; 8201da177e4SLinus Torvalds } else if (!(rt->rt6i_flags & RTF_EXPIRES)) { 8211da177e4SLinus Torvalds rt->rt6i_expires = expires; 8221da177e4SLinus Torvalds rt->rt6i_flags |= RTF_EXPIRES; 8231da177e4SLinus Torvalds } 8241da177e4SLinus Torvalds } 825d8d1f30bSChangli Gao dst_release(&rt->dst); 8261da177e4SLinus Torvalds } 8271da177e4SLinus Torvalds 8284c5ff6a6SHerbert Xu out: 8291da177e4SLinus Torvalds in6_ifa_put(ifp); 8301da177e4SLinus Torvalds } 8311da177e4SLinus Torvalds 8321da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 8331da177e4SLinus Torvalds static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift) 8341da177e4SLinus Torvalds { 8351da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 8361da177e4SLinus Torvalds struct in6_addr addr, *tmpaddr; 8377a876b0eSGlenn Wurster unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp, age; 838eac55bf9SBenoit Boissinot unsigned long regen_advance; 8391da177e4SLinus Torvalds int tmp_plen; 8401da177e4SLinus Torvalds int ret = 0; 8411da177e4SLinus Torvalds int max_addresses; 84295c385b4SNeil Horman u32 addr_flags; 8431da177e4SLinus Torvalds 8441da177e4SLinus Torvalds write_lock(&idev->lock); 8451da177e4SLinus Torvalds if (ift) { 8461da177e4SLinus Torvalds spin_lock_bh(&ift->lock); 8471da177e4SLinus Torvalds memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8); 8481da177e4SLinus Torvalds spin_unlock_bh(&ift->lock); 8491da177e4SLinus Torvalds tmpaddr = &addr; 8501da177e4SLinus Torvalds } else { 8511da177e4SLinus Torvalds tmpaddr = NULL; 8521da177e4SLinus Torvalds } 8531da177e4SLinus Torvalds retry: 8541da177e4SLinus Torvalds in6_dev_hold(idev); 8551da177e4SLinus Torvalds if (idev->cnf.use_tempaddr <= 0) { 8561da177e4SLinus Torvalds write_unlock(&idev->lock); 8571da177e4SLinus Torvalds printk(KERN_INFO 8581da177e4SLinus Torvalds "ipv6_create_tempaddr(): use_tempaddr is disabled.\n"); 8591da177e4SLinus Torvalds in6_dev_put(idev); 8601da177e4SLinus Torvalds ret = -1; 8611da177e4SLinus Torvalds goto out; 8621da177e4SLinus Torvalds } 8631da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 8641da177e4SLinus Torvalds if (ifp->regen_count++ >= idev->cnf.regen_max_retry) { 8651da177e4SLinus Torvalds idev->cnf.use_tempaddr = -1; /*XXX*/ 8661da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 8671da177e4SLinus Torvalds write_unlock(&idev->lock); 8681da177e4SLinus Torvalds printk(KERN_WARNING 8691da177e4SLinus Torvalds "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n"); 8701da177e4SLinus Torvalds in6_dev_put(idev); 8711da177e4SLinus Torvalds ret = -1; 8721da177e4SLinus Torvalds goto out; 8731da177e4SLinus Torvalds } 8741da177e4SLinus Torvalds in6_ifa_hold(ifp); 8751da177e4SLinus Torvalds memcpy(addr.s6_addr, ifp->addr.s6_addr, 8); 8761da177e4SLinus Torvalds if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) { 8771da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 8781da177e4SLinus Torvalds write_unlock(&idev->lock); 8791da177e4SLinus Torvalds printk(KERN_WARNING 8801da177e4SLinus Torvalds "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n"); 8811da177e4SLinus Torvalds in6_ifa_put(ifp); 8821da177e4SLinus Torvalds in6_dev_put(idev); 8831da177e4SLinus Torvalds ret = -1; 8841da177e4SLinus Torvalds goto out; 8851da177e4SLinus Torvalds } 8861da177e4SLinus Torvalds memcpy(&addr.s6_addr[8], idev->rndid, 8); 8877a876b0eSGlenn Wurster age = (jiffies - ifp->tstamp) / HZ; 8881da177e4SLinus Torvalds tmp_valid_lft = min_t(__u32, 8891da177e4SLinus Torvalds ifp->valid_lft, 8907a876b0eSGlenn Wurster idev->cnf.temp_valid_lft + age); 8911da177e4SLinus Torvalds tmp_prefered_lft = min_t(__u32, 8921da177e4SLinus Torvalds ifp->prefered_lft, 8937a876b0eSGlenn Wurster idev->cnf.temp_prefered_lft + age - 894784e2710SBen Hutchings idev->cnf.max_desync_factor); 8951da177e4SLinus Torvalds tmp_plen = ifp->prefix_len; 8961da177e4SLinus Torvalds max_addresses = idev->cnf.max_addresses; 8971da177e4SLinus Torvalds tmp_cstamp = ifp->cstamp; 8981da177e4SLinus Torvalds tmp_tstamp = ifp->tstamp; 8991da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 9001da177e4SLinus Torvalds 901eac55bf9SBenoit Boissinot regen_advance = idev->cnf.regen_max_retry * 902eac55bf9SBenoit Boissinot idev->cnf.dad_transmits * 903eac55bf9SBenoit Boissinot idev->nd_parms->retrans_time / HZ; 9041da177e4SLinus Torvalds write_unlock(&idev->lock); 90595c385b4SNeil Horman 906eac55bf9SBenoit Boissinot /* A temporary address is created only if this calculated Preferred 907eac55bf9SBenoit Boissinot * Lifetime is greater than REGEN_ADVANCE time units. In particular, 908eac55bf9SBenoit Boissinot * an implementation must not create a temporary address with a zero 909eac55bf9SBenoit Boissinot * Preferred Lifetime. 910eac55bf9SBenoit Boissinot */ 911eac55bf9SBenoit Boissinot if (tmp_prefered_lft <= regen_advance) { 912eac55bf9SBenoit Boissinot in6_ifa_put(ifp); 913eac55bf9SBenoit Boissinot in6_dev_put(idev); 914eac55bf9SBenoit Boissinot ret = -1; 915eac55bf9SBenoit Boissinot goto out; 916eac55bf9SBenoit Boissinot } 917eac55bf9SBenoit Boissinot 91895c385b4SNeil Horman addr_flags = IFA_F_TEMPORARY; 91995c385b4SNeil Horman /* set in addrconf_prefix_rcv() */ 92095c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 92195c385b4SNeil Horman addr_flags |= IFA_F_OPTIMISTIC; 92295c385b4SNeil Horman 9231da177e4SLinus Torvalds ift = !max_addresses || 9241da177e4SLinus Torvalds ipv6_count_addresses(idev) < max_addresses ? 9251da177e4SLinus Torvalds ipv6_add_addr(idev, &addr, tmp_plen, 92695c385b4SNeil Horman ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK, 92795c385b4SNeil Horman addr_flags) : NULL; 9281da177e4SLinus Torvalds if (!ift || IS_ERR(ift)) { 9291da177e4SLinus Torvalds in6_ifa_put(ifp); 9301da177e4SLinus Torvalds in6_dev_put(idev); 9311da177e4SLinus Torvalds printk(KERN_INFO 9321da177e4SLinus Torvalds "ipv6_create_tempaddr(): retry temporary address regeneration.\n"); 9331da177e4SLinus Torvalds tmpaddr = &addr; 9341da177e4SLinus Torvalds write_lock(&idev->lock); 9351da177e4SLinus Torvalds goto retry; 9361da177e4SLinus Torvalds } 9371da177e4SLinus Torvalds 9381da177e4SLinus Torvalds spin_lock_bh(&ift->lock); 9391da177e4SLinus Torvalds ift->ifpub = ifp; 9401da177e4SLinus Torvalds ift->valid_lft = tmp_valid_lft; 9411da177e4SLinus Torvalds ift->prefered_lft = tmp_prefered_lft; 9421da177e4SLinus Torvalds ift->cstamp = tmp_cstamp; 9431da177e4SLinus Torvalds ift->tstamp = tmp_tstamp; 9441da177e4SLinus Torvalds spin_unlock_bh(&ift->lock); 9451da177e4SLinus Torvalds 9461da177e4SLinus Torvalds addrconf_dad_start(ift, 0); 9471da177e4SLinus Torvalds in6_ifa_put(ift); 9481da177e4SLinus Torvalds in6_dev_put(idev); 9491da177e4SLinus Torvalds out: 9501da177e4SLinus Torvalds return ret; 9511da177e4SLinus Torvalds } 9521da177e4SLinus Torvalds #endif 9531da177e4SLinus Torvalds 9541da177e4SLinus Torvalds /* 955072047e4SYOSHIFUJI Hideaki * Choose an appropriate source address (RFC3484) 9561da177e4SLinus Torvalds */ 957a9b05723SYOSHIFUJI Hideaki enum { 958a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_INIT = 0, 959a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_LOCAL, 960a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_SCOPE, 961a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PREFERRED, 962a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6 963a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_HOA, 964a9b05723SYOSHIFUJI Hideaki #endif 965a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_OIF, 966a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_LABEL, 967a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_PRIVACY 968a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PRIVACY, 969a9b05723SYOSHIFUJI Hideaki #endif 970a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_ORCHID, 971a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PREFIX, 972a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_MAX 973072047e4SYOSHIFUJI Hideaki }; 974072047e4SYOSHIFUJI Hideaki 975a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score { 976a9b05723SYOSHIFUJI Hideaki int rule; 977a9b05723SYOSHIFUJI Hideaki int addr_type; 978a9b05723SYOSHIFUJI Hideaki struct inet6_ifaddr *ifa; 979a9b05723SYOSHIFUJI Hideaki DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX); 980a9b05723SYOSHIFUJI Hideaki int scopedist; 981a9b05723SYOSHIFUJI Hideaki int matchlen; 982a9b05723SYOSHIFUJI Hideaki }; 983a9b05723SYOSHIFUJI Hideaki 984a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst { 9859acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *addr; 986a9b05723SYOSHIFUJI Hideaki int ifindex; 987a9b05723SYOSHIFUJI Hideaki int scope; 988a9b05723SYOSHIFUJI Hideaki int label; 9897cbca67cSYOSHIFUJI Hideaki unsigned int prefs; 990a9b05723SYOSHIFUJI Hideaki }; 991072047e4SYOSHIFUJI Hideaki 992b6f99a21SDave Jones static inline int ipv6_saddr_preferred(int type) 9931da177e4SLinus Torvalds { 99445bb0060SUlrich Weber if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK)) 995072047e4SYOSHIFUJI Hideaki return 1; 996072047e4SYOSHIFUJI Hideaki return 0; 9971da177e4SLinus Torvalds } 9981da177e4SLinus Torvalds 9993de23255SBenjamin Thery static int ipv6_get_saddr_eval(struct net *net, 10003de23255SBenjamin Thery struct ipv6_saddr_score *score, 1001a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst *dst, 1002a9b05723SYOSHIFUJI Hideaki int i) 1003a9b05723SYOSHIFUJI Hideaki { 1004a9b05723SYOSHIFUJI Hideaki int ret; 1005a9b05723SYOSHIFUJI Hideaki 1006a9b05723SYOSHIFUJI Hideaki if (i <= score->rule) { 1007a9b05723SYOSHIFUJI Hideaki switch (i) { 1008a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_SCOPE: 1009a9b05723SYOSHIFUJI Hideaki ret = score->scopedist; 1010a9b05723SYOSHIFUJI Hideaki break; 1011a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFIX: 1012a9b05723SYOSHIFUJI Hideaki ret = score->matchlen; 1013a9b05723SYOSHIFUJI Hideaki break; 1014a9b05723SYOSHIFUJI Hideaki default: 1015a9b05723SYOSHIFUJI Hideaki ret = !!test_bit(i, score->scorebits); 1016a9b05723SYOSHIFUJI Hideaki } 1017a9b05723SYOSHIFUJI Hideaki goto out; 1018a9b05723SYOSHIFUJI Hideaki } 1019a9b05723SYOSHIFUJI Hideaki 1020a9b05723SYOSHIFUJI Hideaki switch (i) { 1021a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_INIT: 1022a9b05723SYOSHIFUJI Hideaki /* Rule 0: remember if hiscore is not ready yet */ 1023a9b05723SYOSHIFUJI Hideaki ret = !!score->ifa; 1024a9b05723SYOSHIFUJI Hideaki break; 1025a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_LOCAL: 1026a9b05723SYOSHIFUJI Hideaki /* Rule 1: Prefer same address */ 1027a9b05723SYOSHIFUJI Hideaki ret = ipv6_addr_equal(&score->ifa->addr, dst->addr); 1028a9b05723SYOSHIFUJI Hideaki break; 1029a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_SCOPE: 1030a9b05723SYOSHIFUJI Hideaki /* Rule 2: Prefer appropriate scope 1031a9b05723SYOSHIFUJI Hideaki * 1032a9b05723SYOSHIFUJI Hideaki * ret 1033a9b05723SYOSHIFUJI Hideaki * ^ 1034a9b05723SYOSHIFUJI Hideaki * -1 | d 15 1035a9b05723SYOSHIFUJI Hideaki * ---+--+-+---> scope 1036a9b05723SYOSHIFUJI Hideaki * | 1037a9b05723SYOSHIFUJI Hideaki * | d is scope of the destination. 1038a9b05723SYOSHIFUJI Hideaki * B-d | \ 1039a9b05723SYOSHIFUJI Hideaki * | \ <- smaller scope is better if 1040a9b05723SYOSHIFUJI Hideaki * B-15 | \ if scope is enough for destinaion. 1041a9b05723SYOSHIFUJI Hideaki * | ret = B - scope (-1 <= scope >= d <= 15). 1042a9b05723SYOSHIFUJI Hideaki * d-C-1 | / 1043a9b05723SYOSHIFUJI Hideaki * |/ <- greater is better 1044a9b05723SYOSHIFUJI Hideaki * -C / if scope is not enough for destination. 1045a9b05723SYOSHIFUJI Hideaki * /| ret = scope - C (-1 <= d < scope <= 15). 1046a9b05723SYOSHIFUJI Hideaki * 1047a9b05723SYOSHIFUJI Hideaki * d - C - 1 < B -15 (for all -1 <= d <= 15). 1048a9b05723SYOSHIFUJI Hideaki * C > d + 14 - B >= 15 + 14 - B = 29 - B. 1049a9b05723SYOSHIFUJI Hideaki * Assume B = 0 and we get C > 29. 1050a9b05723SYOSHIFUJI Hideaki */ 1051a9b05723SYOSHIFUJI Hideaki ret = __ipv6_addr_src_scope(score->addr_type); 1052a9b05723SYOSHIFUJI Hideaki if (ret >= dst->scope) 1053a9b05723SYOSHIFUJI Hideaki ret = -ret; 1054a9b05723SYOSHIFUJI Hideaki else 1055a9b05723SYOSHIFUJI Hideaki ret -= 128; /* 30 is enough */ 1056a9b05723SYOSHIFUJI Hideaki score->scopedist = ret; 1057a9b05723SYOSHIFUJI Hideaki break; 1058a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFERRED: 1059a9b05723SYOSHIFUJI Hideaki /* Rule 3: Avoid deprecated and optimistic addresses */ 1060a9b05723SYOSHIFUJI Hideaki ret = ipv6_saddr_preferred(score->addr_type) || 1061a9b05723SYOSHIFUJI Hideaki !(score->ifa->flags & (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC)); 1062a9b05723SYOSHIFUJI Hideaki break; 1063a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6 1064a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_HOA: 10657cbca67cSYOSHIFUJI Hideaki { 1066a9b05723SYOSHIFUJI Hideaki /* Rule 4: Prefer home address */ 10677cbca67cSYOSHIFUJI Hideaki int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA); 10687cbca67cSYOSHIFUJI Hideaki ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome; 1069a9b05723SYOSHIFUJI Hideaki break; 10707cbca67cSYOSHIFUJI Hideaki } 1071a9b05723SYOSHIFUJI Hideaki #endif 1072a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_OIF: 1073a9b05723SYOSHIFUJI Hideaki /* Rule 5: Prefer outgoing interface */ 1074a9b05723SYOSHIFUJI Hideaki ret = (!dst->ifindex || 1075a9b05723SYOSHIFUJI Hideaki dst->ifindex == score->ifa->idev->dev->ifindex); 1076a9b05723SYOSHIFUJI Hideaki break; 1077a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_LABEL: 1078a9b05723SYOSHIFUJI Hideaki /* Rule 6: Prefer matching label */ 10793de23255SBenjamin Thery ret = ipv6_addr_label(net, 10803de23255SBenjamin Thery &score->ifa->addr, score->addr_type, 1081a9b05723SYOSHIFUJI Hideaki score->ifa->idev->dev->ifindex) == dst->label; 1082a9b05723SYOSHIFUJI Hideaki break; 1083a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_PRIVACY 1084a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PRIVACY: 10857cbca67cSYOSHIFUJI Hideaki { 1086a9b05723SYOSHIFUJI Hideaki /* Rule 7: Prefer public address 108725985edcSLucas De Marchi * Note: prefer temporary address if use_tempaddr >= 2 1088a9b05723SYOSHIFUJI Hideaki */ 10897cbca67cSYOSHIFUJI Hideaki int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ? 10907cbca67cSYOSHIFUJI Hideaki !!(dst->prefs & IPV6_PREFER_SRC_TMP) : 10917cbca67cSYOSHIFUJI Hideaki score->ifa->idev->cnf.use_tempaddr >= 2; 10927cbca67cSYOSHIFUJI Hideaki ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp; 1093a9b05723SYOSHIFUJI Hideaki break; 10947cbca67cSYOSHIFUJI Hideaki } 1095a9b05723SYOSHIFUJI Hideaki #endif 1096a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_ORCHID: 1097a9b05723SYOSHIFUJI Hideaki /* Rule 8-: Prefer ORCHID vs ORCHID or 1098a9b05723SYOSHIFUJI Hideaki * non-ORCHID vs non-ORCHID 1099a9b05723SYOSHIFUJI Hideaki */ 1100a9b05723SYOSHIFUJI Hideaki ret = !(ipv6_addr_orchid(&score->ifa->addr) ^ 1101a9b05723SYOSHIFUJI Hideaki ipv6_addr_orchid(dst->addr)); 1102a9b05723SYOSHIFUJI Hideaki break; 1103a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFIX: 1104a9b05723SYOSHIFUJI Hideaki /* Rule 8: Use longest matching prefix */ 1105a9b05723SYOSHIFUJI Hideaki score->matchlen = ret = ipv6_addr_diff(&score->ifa->addr, 1106a9b05723SYOSHIFUJI Hideaki dst->addr); 1107a9b05723SYOSHIFUJI Hideaki break; 1108a9b05723SYOSHIFUJI Hideaki default: 1109a9b05723SYOSHIFUJI Hideaki ret = 0; 1110a9b05723SYOSHIFUJI Hideaki } 1111a9b05723SYOSHIFUJI Hideaki 1112a9b05723SYOSHIFUJI Hideaki if (ret) 1113a9b05723SYOSHIFUJI Hideaki __set_bit(i, score->scorebits); 1114a9b05723SYOSHIFUJI Hideaki score->rule = i; 1115a9b05723SYOSHIFUJI Hideaki out: 1116a9b05723SYOSHIFUJI Hideaki return ret; 1117a9b05723SYOSHIFUJI Hideaki } 1118a9b05723SYOSHIFUJI Hideaki 1119191cd582SBrian Haley int ipv6_dev_get_saddr(struct net *net, struct net_device *dst_dev, 11209acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *daddr, unsigned int prefs, 11217cbca67cSYOSHIFUJI Hideaki struct in6_addr *saddr) 11221da177e4SLinus Torvalds { 1123a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score scores[2], 1124a9b05723SYOSHIFUJI Hideaki *score = &scores[0], *hiscore = &scores[1]; 1125a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst dst; 1126072047e4SYOSHIFUJI Hideaki struct net_device *dev; 1127a9b05723SYOSHIFUJI Hideaki int dst_type; 11281da177e4SLinus Torvalds 1129a9b05723SYOSHIFUJI Hideaki dst_type = __ipv6_addr_type(daddr); 1130a9b05723SYOSHIFUJI Hideaki dst.addr = daddr; 1131a9b05723SYOSHIFUJI Hideaki dst.ifindex = dst_dev ? dst_dev->ifindex : 0; 1132a9b05723SYOSHIFUJI Hideaki dst.scope = __ipv6_addr_src_scope(dst_type); 11333de23255SBenjamin Thery dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex); 11347cbca67cSYOSHIFUJI Hideaki dst.prefs = prefs; 1135a9b05723SYOSHIFUJI Hideaki 1136a9b05723SYOSHIFUJI Hideaki hiscore->rule = -1; 1137a9b05723SYOSHIFUJI Hideaki hiscore->ifa = NULL; 11381da177e4SLinus Torvalds 11398814c4b5SYOSHIFUJI Hideaki rcu_read_lock(); 1140072047e4SYOSHIFUJI Hideaki 1141c6d14c84SEric Dumazet for_each_netdev_rcu(net, dev) { 1142072047e4SYOSHIFUJI Hideaki struct inet6_dev *idev; 1143072047e4SYOSHIFUJI Hideaki 1144a9b05723SYOSHIFUJI Hideaki /* Candidate Source Address (section 4) 1145072047e4SYOSHIFUJI Hideaki * - multicast and link-local destination address, 1146072047e4SYOSHIFUJI Hideaki * the set of candidate source address MUST only 1147072047e4SYOSHIFUJI Hideaki * include addresses assigned to interfaces 1148072047e4SYOSHIFUJI Hideaki * belonging to the same link as the outgoing 1149072047e4SYOSHIFUJI Hideaki * interface. 1150072047e4SYOSHIFUJI Hideaki * (- For site-local destination addresses, the 1151072047e4SYOSHIFUJI Hideaki * set of candidate source addresses MUST only 1152072047e4SYOSHIFUJI Hideaki * include addresses assigned to interfaces 1153072047e4SYOSHIFUJI Hideaki * belonging to the same site as the outgoing 1154072047e4SYOSHIFUJI Hideaki * interface.) 1155072047e4SYOSHIFUJI Hideaki */ 1156a9b05723SYOSHIFUJI Hideaki if (((dst_type & IPV6_ADDR_MULTICAST) || 1157a9b05723SYOSHIFUJI Hideaki dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL) && 1158a9b05723SYOSHIFUJI Hideaki dst.ifindex && dev->ifindex != dst.ifindex) 1159072047e4SYOSHIFUJI Hideaki continue; 1160072047e4SYOSHIFUJI Hideaki 11611da177e4SLinus Torvalds idev = __in6_dev_get(dev); 1162072047e4SYOSHIFUJI Hideaki if (!idev) 1163072047e4SYOSHIFUJI Hideaki continue; 1164072047e4SYOSHIFUJI Hideaki 11651da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1166502a2ffdSstephen hemminger list_for_each_entry(score->ifa, &idev->addr_list, if_list) { 1167a9b05723SYOSHIFUJI Hideaki int i; 1168072047e4SYOSHIFUJI Hideaki 1169a9b05723SYOSHIFUJI Hideaki /* 11706b3ae80aSYOSHIFUJI Hideaki * - Tentative Address (RFC2462 section 5.4) 11716b3ae80aSYOSHIFUJI Hideaki * - A tentative address is not considered 11726b3ae80aSYOSHIFUJI Hideaki * "assigned to an interface" in the traditional 117395c385b4SNeil Horman * sense, unless it is also flagged as optimistic. 11746b3ae80aSYOSHIFUJI Hideaki * - Candidate Source Address (section 4) 1175072047e4SYOSHIFUJI Hideaki * - In any case, anycast addresses, multicast 1176072047e4SYOSHIFUJI Hideaki * addresses, and the unspecified address MUST 1177072047e4SYOSHIFUJI Hideaki * NOT be included in a candidate set. 1178072047e4SYOSHIFUJI Hideaki */ 1179a9b05723SYOSHIFUJI Hideaki if ((score->ifa->flags & IFA_F_TENTATIVE) && 1180a9b05723SYOSHIFUJI Hideaki (!(score->ifa->flags & IFA_F_OPTIMISTIC))) 11816b3ae80aSYOSHIFUJI Hideaki continue; 1182a9b05723SYOSHIFUJI Hideaki 1183a9b05723SYOSHIFUJI Hideaki score->addr_type = __ipv6_addr_type(&score->ifa->addr); 1184a9b05723SYOSHIFUJI Hideaki 1185a9b05723SYOSHIFUJI Hideaki if (unlikely(score->addr_type == IPV6_ADDR_ANY || 1186a9b05723SYOSHIFUJI Hideaki score->addr_type & IPV6_ADDR_MULTICAST)) { 1187072047e4SYOSHIFUJI Hideaki LIMIT_NETDEBUG(KERN_DEBUG 1188072047e4SYOSHIFUJI Hideaki "ADDRCONF: unspecified / multicast address " 1189072047e4SYOSHIFUJI Hideaki "assigned as unicast address on %s", 1190072047e4SYOSHIFUJI Hideaki dev->name); 11911da177e4SLinus Torvalds continue; 1192072047e4SYOSHIFUJI Hideaki } 1193072047e4SYOSHIFUJI Hideaki 1194a9b05723SYOSHIFUJI Hideaki score->rule = -1; 1195a9b05723SYOSHIFUJI Hideaki bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX); 1196072047e4SYOSHIFUJI Hideaki 1197a9b05723SYOSHIFUJI Hideaki for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) { 1198a9b05723SYOSHIFUJI Hideaki int minihiscore, miniscore; 1199072047e4SYOSHIFUJI Hideaki 12003de23255SBenjamin Thery minihiscore = ipv6_get_saddr_eval(net, hiscore, &dst, i); 12013de23255SBenjamin Thery miniscore = ipv6_get_saddr_eval(net, score, &dst, i); 1202072047e4SYOSHIFUJI Hideaki 1203a9b05723SYOSHIFUJI Hideaki if (minihiscore > miniscore) { 1204a9b05723SYOSHIFUJI Hideaki if (i == IPV6_SADDR_RULE_SCOPE && 1205a9b05723SYOSHIFUJI Hideaki score->scopedist > 0) { 1206a9b05723SYOSHIFUJI Hideaki /* 1207a9b05723SYOSHIFUJI Hideaki * special case: 1208a9b05723SYOSHIFUJI Hideaki * each remaining entry 1209a9b05723SYOSHIFUJI Hideaki * has too small (not enough) 1210a9b05723SYOSHIFUJI Hideaki * scope, because ifa entries 1211a9b05723SYOSHIFUJI Hideaki * are sorted by their scope 1212a9b05723SYOSHIFUJI Hideaki * values. 1213072047e4SYOSHIFUJI Hideaki */ 1214a9b05723SYOSHIFUJI Hideaki goto try_nextdev; 1215072047e4SYOSHIFUJI Hideaki } 1216a9b05723SYOSHIFUJI Hideaki break; 1217a9b05723SYOSHIFUJI Hideaki } else if (minihiscore < miniscore) { 1218a9b05723SYOSHIFUJI Hideaki if (hiscore->ifa) 1219a9b05723SYOSHIFUJI Hideaki in6_ifa_put(hiscore->ifa); 122099cd07a5SJuha-Matti Tapio 1221a9b05723SYOSHIFUJI Hideaki in6_ifa_hold(score->ifa); 1222072047e4SYOSHIFUJI Hideaki 1223a0bffffcSIlpo Järvinen swap(hiscore, score); 1224a9b05723SYOSHIFUJI Hideaki 1225a9b05723SYOSHIFUJI Hideaki /* restore our iterator */ 1226a9b05723SYOSHIFUJI Hideaki score->ifa = hiscore->ifa; 1227a9b05723SYOSHIFUJI Hideaki 1228a9b05723SYOSHIFUJI Hideaki break; 12291da177e4SLinus Torvalds } 1230a9b05723SYOSHIFUJI Hideaki } 1231a9b05723SYOSHIFUJI Hideaki } 1232a9b05723SYOSHIFUJI Hideaki try_nextdev: 12331da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 12341da177e4SLinus Torvalds } 12358814c4b5SYOSHIFUJI Hideaki rcu_read_unlock(); 12361da177e4SLinus Torvalds 1237a9b05723SYOSHIFUJI Hideaki if (!hiscore->ifa) 1238072047e4SYOSHIFUJI Hideaki return -EADDRNOTAVAIL; 12391da177e4SLinus Torvalds 1240a9b05723SYOSHIFUJI Hideaki ipv6_addr_copy(saddr, &hiscore->ifa->addr); 1241a9b05723SYOSHIFUJI Hideaki in6_ifa_put(hiscore->ifa); 1242072047e4SYOSHIFUJI Hideaki return 0; 12431da177e4SLinus Torvalds } 12445e5f3f0fSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_dev_get_saddr); 12451da177e4SLinus Torvalds 124695c385b4SNeil Horman int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr, 124795c385b4SNeil Horman unsigned char banned_flags) 12481da177e4SLinus Torvalds { 12491da177e4SLinus Torvalds struct inet6_dev *idev; 12501da177e4SLinus Torvalds int err = -EADDRNOTAVAIL; 12511da177e4SLinus Torvalds 12528814c4b5SYOSHIFUJI Hideaki rcu_read_lock(); 1253e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 1254e21e8467SStephen Hemminger if (idev) { 12551da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 12561da177e4SLinus Torvalds 12571da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1258502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 1259502a2ffdSstephen hemminger if (ifp->scope == IFA_LINK && 1260502a2ffdSstephen hemminger !(ifp->flags & banned_flags)) { 12611da177e4SLinus Torvalds ipv6_addr_copy(addr, &ifp->addr); 12621da177e4SLinus Torvalds err = 0; 12631da177e4SLinus Torvalds break; 12641da177e4SLinus Torvalds } 12651da177e4SLinus Torvalds } 12661da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 12671da177e4SLinus Torvalds } 12688814c4b5SYOSHIFUJI Hideaki rcu_read_unlock(); 12691da177e4SLinus Torvalds return err; 12701da177e4SLinus Torvalds } 12711da177e4SLinus Torvalds 12721da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev) 12731da177e4SLinus Torvalds { 12741da177e4SLinus Torvalds int cnt = 0; 12751da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 12761da177e4SLinus Torvalds 12771da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1278502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) 12791da177e4SLinus Torvalds cnt++; 12801da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 12811da177e4SLinus Torvalds return cnt; 12821da177e4SLinus Torvalds } 12831da177e4SLinus Torvalds 1284bfeade08SDaniel Lezcano int ipv6_chk_addr(struct net *net, struct in6_addr *addr, 1285bfeade08SDaniel Lezcano struct net_device *dev, int strict) 12861da177e4SLinus Torvalds { 1287eedf042aSStephen Hemminger struct inet6_ifaddr *ifp; 1288c2e21293Sstephen hemminger struct hlist_node *node; 12893a88a81dSstephen hemminger unsigned int hash = ipv6_addr_hash(addr); 12901da177e4SLinus Torvalds 12915c578aedSstephen hemminger rcu_read_lock_bh(); 12925c578aedSstephen hemminger hlist_for_each_entry_rcu(ifp, node, &inet6_addr_lst[hash], addr_lst) { 1293878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 1294bfeade08SDaniel Lezcano continue; 12951da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr) && 1296eedf042aSStephen Hemminger !(ifp->flags&IFA_F_TENTATIVE) && 1297eedf042aSStephen Hemminger (dev == NULL || ifp->idev->dev == dev || 1298eedf042aSStephen Hemminger !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) { 12995c578aedSstephen hemminger rcu_read_unlock_bh(); 1300eedf042aSStephen Hemminger return 1; 1301eedf042aSStephen Hemminger } 1302eedf042aSStephen Hemminger } 13035c578aedSstephen hemminger 1304eedf042aSStephen Hemminger rcu_read_unlock_bh(); 1305eedf042aSStephen Hemminger return 0; 13061da177e4SLinus Torvalds } 13077159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_addr); 13087159039aSYOSHIFUJI Hideaki 13093e81c6daSDavid S. Miller static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr, 131006bfe655SDaniel Lezcano struct net_device *dev) 13111da177e4SLinus Torvalds { 13123e81c6daSDavid S. Miller unsigned int hash = ipv6_addr_hash(addr); 13131da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 1314c2e21293Sstephen hemminger struct hlist_node *node; 13151da177e4SLinus Torvalds 1316c2e21293Sstephen hemminger hlist_for_each_entry(ifp, node, &inet6_addr_lst[hash], addr_lst) { 1317878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 131806bfe655SDaniel Lezcano continue; 13191da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr)) { 13201da177e4SLinus Torvalds if (dev == NULL || ifp->idev->dev == dev) 13213e81c6daSDavid S. Miller return true; 13221da177e4SLinus Torvalds } 13231da177e4SLinus Torvalds } 13243e81c6daSDavid S. Miller return false; 13251da177e4SLinus Torvalds } 13261da177e4SLinus Torvalds 132752eeeb84SYOSHIFUJI Hideaki int ipv6_chk_prefix(struct in6_addr *addr, struct net_device *dev) 132852eeeb84SYOSHIFUJI Hideaki { 132952eeeb84SYOSHIFUJI Hideaki struct inet6_dev *idev; 133052eeeb84SYOSHIFUJI Hideaki struct inet6_ifaddr *ifa; 133152eeeb84SYOSHIFUJI Hideaki int onlink; 133252eeeb84SYOSHIFUJI Hideaki 133352eeeb84SYOSHIFUJI Hideaki onlink = 0; 133452eeeb84SYOSHIFUJI Hideaki rcu_read_lock(); 133552eeeb84SYOSHIFUJI Hideaki idev = __in6_dev_get(dev); 133652eeeb84SYOSHIFUJI Hideaki if (idev) { 133752eeeb84SYOSHIFUJI Hideaki read_lock_bh(&idev->lock); 1338502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 133952eeeb84SYOSHIFUJI Hideaki onlink = ipv6_prefix_equal(addr, &ifa->addr, 134052eeeb84SYOSHIFUJI Hideaki ifa->prefix_len); 134152eeeb84SYOSHIFUJI Hideaki if (onlink) 134252eeeb84SYOSHIFUJI Hideaki break; 134352eeeb84SYOSHIFUJI Hideaki } 134452eeeb84SYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 134552eeeb84SYOSHIFUJI Hideaki } 134652eeeb84SYOSHIFUJI Hideaki rcu_read_unlock(); 134752eeeb84SYOSHIFUJI Hideaki return onlink; 134852eeeb84SYOSHIFUJI Hideaki } 134952eeeb84SYOSHIFUJI Hideaki 135052eeeb84SYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_prefix); 135152eeeb84SYOSHIFUJI Hideaki 13529acd9f3aSYOSHIFUJI Hideaki struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr, 13531cab3da6SDaniel Lezcano struct net_device *dev, int strict) 13541da177e4SLinus Torvalds { 1355b79d1d54SDavid S. Miller struct inet6_ifaddr *ifp, *result = NULL; 13563a88a81dSstephen hemminger unsigned int hash = ipv6_addr_hash(addr); 1357b79d1d54SDavid S. Miller struct hlist_node *node; 13581da177e4SLinus Torvalds 13595c578aedSstephen hemminger rcu_read_lock_bh(); 13604f70eccaSEric Dumazet hlist_for_each_entry_rcu_bh(ifp, node, &inet6_addr_lst[hash], addr_lst) { 1361878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 13621cab3da6SDaniel Lezcano continue; 13631da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr)) { 13641da177e4SLinus Torvalds if (dev == NULL || ifp->idev->dev == dev || 13651da177e4SLinus Torvalds !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) { 1366b79d1d54SDavid S. Miller result = ifp; 13671da177e4SLinus Torvalds in6_ifa_hold(ifp); 13681da177e4SLinus Torvalds break; 13691da177e4SLinus Torvalds } 13701da177e4SLinus Torvalds } 13711da177e4SLinus Torvalds } 13725c578aedSstephen hemminger rcu_read_unlock_bh(); 13731da177e4SLinus Torvalds 1374b79d1d54SDavid S. Miller return result; 13751da177e4SLinus Torvalds } 13761da177e4SLinus Torvalds 13771da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */ 13781da177e4SLinus Torvalds 1379cc411d0bSBrian Haley static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed) 13801da177e4SLinus Torvalds { 13811da177e4SLinus Torvalds if (ifp->flags&IFA_F_PERMANENT) { 13821da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 13831da177e4SLinus Torvalds addrconf_del_timer(ifp); 13841da177e4SLinus Torvalds ifp->flags |= IFA_F_TENTATIVE; 1385cc411d0bSBrian Haley if (dad_failed) 1386cc411d0bSBrian Haley ifp->flags |= IFA_F_DADFAILED; 13871da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 1388e2577a06SHerbert Xu if (dad_failed) 1389e2577a06SHerbert Xu ipv6_ifa_notify(0, ifp); 13901da177e4SLinus Torvalds in6_ifa_put(ifp); 13911da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 13921da177e4SLinus Torvalds } else if (ifp->flags&IFA_F_TEMPORARY) { 13931da177e4SLinus Torvalds struct inet6_ifaddr *ifpub; 13941da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 13951da177e4SLinus Torvalds ifpub = ifp->ifpub; 13961da177e4SLinus Torvalds if (ifpub) { 13971da177e4SLinus Torvalds in6_ifa_hold(ifpub); 13981da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 13991da177e4SLinus Torvalds ipv6_create_tempaddr(ifpub, ifp); 14001da177e4SLinus Torvalds in6_ifa_put(ifpub); 14011da177e4SLinus Torvalds } else { 14021da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 14031da177e4SLinus Torvalds } 14041da177e4SLinus Torvalds ipv6_del_addr(ifp); 14051da177e4SLinus Torvalds #endif 14061da177e4SLinus Torvalds } else 14071da177e4SLinus Torvalds ipv6_del_addr(ifp); 14081da177e4SLinus Torvalds } 14091da177e4SLinus Torvalds 1410f2344a13SHerbert Xu static int addrconf_dad_end(struct inet6_ifaddr *ifp) 1411f2344a13SHerbert Xu { 1412f2344a13SHerbert Xu int err = -ENOENT; 1413f2344a13SHerbert Xu 1414f2344a13SHerbert Xu spin_lock(&ifp->state_lock); 1415f2344a13SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DAD) { 1416f2344a13SHerbert Xu ifp->state = INET6_IFADDR_STATE_POSTDAD; 1417f2344a13SHerbert Xu err = 0; 1418f2344a13SHerbert Xu } 1419f2344a13SHerbert Xu spin_unlock(&ifp->state_lock); 1420f2344a13SHerbert Xu 1421f2344a13SHerbert Xu return err; 1422f2344a13SHerbert Xu } 1423f2344a13SHerbert Xu 14243c21edbdSYOSHIFUJI Hideaki void addrconf_dad_failure(struct inet6_ifaddr *ifp) 14253c21edbdSYOSHIFUJI Hideaki { 14261b34be74SYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev; 14279bdd8d40SBrian Haley 1428853dc2e0SUrsula Braun if (addrconf_dad_end(ifp)) { 1429853dc2e0SUrsula Braun in6_ifa_put(ifp); 1430f2344a13SHerbert Xu return; 1431853dc2e0SUrsula Braun } 1432f2344a13SHerbert Xu 14339bdd8d40SBrian Haley if (net_ratelimit()) 14340522fea6SJens Rosenboom printk(KERN_INFO "%s: IPv6 duplicate address %pI6c detected!\n", 14350522fea6SJens Rosenboom ifp->idev->dev->name, &ifp->addr); 14369bdd8d40SBrian Haley 14371b34be74SYOSHIFUJI Hideaki if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) { 14381b34be74SYOSHIFUJI Hideaki struct in6_addr addr; 14391b34be74SYOSHIFUJI Hideaki 14401b34be74SYOSHIFUJI Hideaki addr.s6_addr32[0] = htonl(0xfe800000); 14411b34be74SYOSHIFUJI Hideaki addr.s6_addr32[1] = 0; 14421b34be74SYOSHIFUJI Hideaki 14431b34be74SYOSHIFUJI Hideaki if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) && 14441b34be74SYOSHIFUJI Hideaki ipv6_addr_equal(&ifp->addr, &addr)) { 14451b34be74SYOSHIFUJI Hideaki /* DAD failed for link-local based on MAC address */ 14461b34be74SYOSHIFUJI Hideaki idev->cnf.disable_ipv6 = 1; 14479bdd8d40SBrian Haley 14489bdd8d40SBrian Haley printk(KERN_INFO "%s: IPv6 being disabled!\n", 14499bdd8d40SBrian Haley ifp->idev->dev->name); 14501b34be74SYOSHIFUJI Hideaki } 14511b34be74SYOSHIFUJI Hideaki } 14521b34be74SYOSHIFUJI Hideaki 1453cc411d0bSBrian Haley addrconf_dad_stop(ifp, 1); 14543c21edbdSYOSHIFUJI Hideaki } 14551da177e4SLinus Torvalds 14561da177e4SLinus Torvalds /* Join to solicited addr multicast group. */ 14571da177e4SLinus Torvalds 14581da177e4SLinus Torvalds void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr) 14591da177e4SLinus Torvalds { 14601da177e4SLinus Torvalds struct in6_addr maddr; 14611da177e4SLinus Torvalds 14621da177e4SLinus Torvalds if (dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 14631da177e4SLinus Torvalds return; 14641da177e4SLinus Torvalds 14651da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr); 14661da177e4SLinus Torvalds ipv6_dev_mc_inc(dev, &maddr); 14671da177e4SLinus Torvalds } 14681da177e4SLinus Torvalds 14691da177e4SLinus Torvalds void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr) 14701da177e4SLinus Torvalds { 14711da177e4SLinus Torvalds struct in6_addr maddr; 14721da177e4SLinus Torvalds 14731da177e4SLinus Torvalds if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 14741da177e4SLinus Torvalds return; 14751da177e4SLinus Torvalds 14761da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr); 14771da177e4SLinus Torvalds __ipv6_dev_mc_dec(idev, &maddr); 14781da177e4SLinus Torvalds } 14791da177e4SLinus Torvalds 148020380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp) 14811da177e4SLinus Torvalds { 14821da177e4SLinus Torvalds struct in6_addr addr; 14831da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 14841da177e4SLinus Torvalds if (ipv6_addr_any(&addr)) 14851da177e4SLinus Torvalds return; 14861da177e4SLinus Torvalds ipv6_dev_ac_inc(ifp->idev->dev, &addr); 14871da177e4SLinus Torvalds } 14881da177e4SLinus Torvalds 148920380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp) 14901da177e4SLinus Torvalds { 14911da177e4SLinus Torvalds struct in6_addr addr; 14921da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 14931da177e4SLinus Torvalds if (ipv6_addr_any(&addr)) 14941da177e4SLinus Torvalds return; 14951da177e4SLinus Torvalds __ipv6_dev_ac_dec(ifp->idev, &addr); 14961da177e4SLinus Torvalds } 14971da177e4SLinus Torvalds 1498073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev) 14991da177e4SLinus Torvalds { 15001da177e4SLinus Torvalds if (dev->addr_len != ETH_ALEN) 15011da177e4SLinus Torvalds return -1; 15021da177e4SLinus Torvalds memcpy(eui, dev->dev_addr, 3); 15031da177e4SLinus Torvalds memcpy(eui + 5, dev->dev_addr + 3, 3); 15041da177e4SLinus Torvalds 15051da177e4SLinus Torvalds /* 15061da177e4SLinus Torvalds * The zSeries OSA network cards can be shared among various 15071da177e4SLinus Torvalds * OS instances, but the OSA cards have only one MAC address. 15081da177e4SLinus Torvalds * This leads to duplicate address conflicts in conjunction 15091da177e4SLinus Torvalds * with IPv6 if more than one instance uses the same card. 15101da177e4SLinus Torvalds * 15111da177e4SLinus Torvalds * The driver for these cards can deliver a unique 16-bit 15121da177e4SLinus Torvalds * identifier for each instance sharing the same card. It is 15131da177e4SLinus Torvalds * placed instead of 0xFFFE in the interface identifier. The 15141da177e4SLinus Torvalds * "u" bit of the interface identifier is not inverted in this 15151da177e4SLinus Torvalds * case. Hence the resulting interface identifier has local 15161da177e4SLinus Torvalds * scope according to RFC2373. 15171da177e4SLinus Torvalds */ 15181da177e4SLinus Torvalds if (dev->dev_id) { 15191da177e4SLinus Torvalds eui[3] = (dev->dev_id >> 8) & 0xFF; 15201da177e4SLinus Torvalds eui[4] = dev->dev_id & 0xFF; 15211da177e4SLinus Torvalds } else { 15221da177e4SLinus Torvalds eui[3] = 0xFF; 15231da177e4SLinus Torvalds eui[4] = 0xFE; 15241da177e4SLinus Torvalds eui[0] ^= 2; 15251da177e4SLinus Torvalds } 15261da177e4SLinus Torvalds return 0; 1527073a8e0eSYOSHIFUJI Hideaki } 1528073a8e0eSYOSHIFUJI Hideaki 1529073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev) 1530073a8e0eSYOSHIFUJI Hideaki { 15311da177e4SLinus Torvalds /* XXX: inherit EUI-64 from other interface -- yoshfuji */ 15321da177e4SLinus Torvalds if (dev->addr_len != ARCNET_ALEN) 15331da177e4SLinus Torvalds return -1; 15341da177e4SLinus Torvalds memset(eui, 0, 7); 15351da177e4SLinus Torvalds eui[7] = *(u8*)dev->dev_addr; 15361da177e4SLinus Torvalds return 0; 1537073a8e0eSYOSHIFUJI Hideaki } 1538073a8e0eSYOSHIFUJI Hideaki 1539073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev) 1540073a8e0eSYOSHIFUJI Hideaki { 15411da177e4SLinus Torvalds if (dev->addr_len != INFINIBAND_ALEN) 15421da177e4SLinus Torvalds return -1; 15431da177e4SLinus Torvalds memcpy(eui, dev->dev_addr + 12, 8); 15441da177e4SLinus Torvalds eui[0] |= 2; 15451da177e4SLinus Torvalds return 0; 15461da177e4SLinus Torvalds } 1547073a8e0eSYOSHIFUJI Hideaki 1548c61393eaSstephen hemminger static int __ipv6_isatap_ifid(u8 *eui, __be32 addr) 1549dfd982baSYOSHIFUJI Hideaki { 15509af28511SSascha Hlusiak if (addr == 0) 15519af28511SSascha Hlusiak return -1; 1552dfd982baSYOSHIFUJI Hideaki eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) || 1553dfd982baSYOSHIFUJI Hideaki ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) || 1554dfd982baSYOSHIFUJI Hideaki ipv4_is_private_172(addr) || ipv4_is_test_192(addr) || 1555dfd982baSYOSHIFUJI Hideaki ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) || 1556dfd982baSYOSHIFUJI Hideaki ipv4_is_test_198(addr) || ipv4_is_multicast(addr) || 1557dfd982baSYOSHIFUJI Hideaki ipv4_is_lbcast(addr)) ? 0x00 : 0x02; 1558dfd982baSYOSHIFUJI Hideaki eui[1] = 0; 1559dfd982baSYOSHIFUJI Hideaki eui[2] = 0x5E; 1560dfd982baSYOSHIFUJI Hideaki eui[3] = 0xFE; 1561dfd982baSYOSHIFUJI Hideaki memcpy(eui + 4, &addr, 4); 1562dfd982baSYOSHIFUJI Hideaki return 0; 1563dfd982baSYOSHIFUJI Hideaki } 1564dfd982baSYOSHIFUJI Hideaki 1565dfd982baSYOSHIFUJI Hideaki static int addrconf_ifid_sit(u8 *eui, struct net_device *dev) 1566dfd982baSYOSHIFUJI Hideaki { 1567dfd982baSYOSHIFUJI Hideaki if (dev->priv_flags & IFF_ISATAP) 1568dfd982baSYOSHIFUJI Hideaki return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); 1569dfd982baSYOSHIFUJI Hideaki return -1; 1570dfd982baSYOSHIFUJI Hideaki } 1571dfd982baSYOSHIFUJI Hideaki 1572073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev) 1573073a8e0eSYOSHIFUJI Hideaki { 1574073a8e0eSYOSHIFUJI Hideaki switch (dev->type) { 1575073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ETHER: 1576073a8e0eSYOSHIFUJI Hideaki case ARPHRD_FDDI: 1577073a8e0eSYOSHIFUJI Hideaki case ARPHRD_IEEE802_TR: 1578073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_eui48(eui, dev); 1579073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ARCNET: 1580073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_arcnet(eui, dev); 1581073a8e0eSYOSHIFUJI Hideaki case ARPHRD_INFINIBAND: 1582073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_infiniband(eui, dev); 1583c7dc89c0SFred L. Templin case ARPHRD_SIT: 1584dfd982baSYOSHIFUJI Hideaki return addrconf_ifid_sit(eui, dev); 1585073a8e0eSYOSHIFUJI Hideaki } 15861da177e4SLinus Torvalds return -1; 15871da177e4SLinus Torvalds } 15881da177e4SLinus Torvalds 15891da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev) 15901da177e4SLinus Torvalds { 15911da177e4SLinus Torvalds int err = -1; 15921da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 15931da177e4SLinus Torvalds 15941da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1595502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 15961da177e4SLinus Torvalds if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) { 15971da177e4SLinus Torvalds memcpy(eui, ifp->addr.s6_addr+8, 8); 15981da177e4SLinus Torvalds err = 0; 15991da177e4SLinus Torvalds break; 16001da177e4SLinus Torvalds } 16011da177e4SLinus Torvalds } 16021da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 16031da177e4SLinus Torvalds return err; 16041da177e4SLinus Torvalds } 16051da177e4SLinus Torvalds 16061da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 16071da177e4SLinus Torvalds /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */ 16081da177e4SLinus Torvalds static int __ipv6_regen_rndid(struct inet6_dev *idev) 16091da177e4SLinus Torvalds { 16101da177e4SLinus Torvalds regen: 1611955189efSYOSHIFUJI Hideaki get_random_bytes(idev->rndid, sizeof(idev->rndid)); 16121da177e4SLinus Torvalds idev->rndid[0] &= ~0x02; 16131da177e4SLinus Torvalds 16141da177e4SLinus Torvalds /* 16151da177e4SLinus Torvalds * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>: 16161da177e4SLinus Torvalds * check if generated address is not inappropriate 16171da177e4SLinus Torvalds * 16181da177e4SLinus Torvalds * - Reserved subnet anycast (RFC 2526) 16191da177e4SLinus Torvalds * 11111101 11....11 1xxxxxxx 1620c7dc89c0SFred L. Templin * - ISATAP (RFC4214) 6.1 16211da177e4SLinus Torvalds * 00-00-5E-FE-xx-xx-xx-xx 16221da177e4SLinus Torvalds * - value 0 16231da177e4SLinus Torvalds * - XXX: already assigned to an address on the device 16241da177e4SLinus Torvalds */ 16251da177e4SLinus Torvalds if (idev->rndid[0] == 0xfd && 16261da177e4SLinus Torvalds (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff && 16271da177e4SLinus Torvalds (idev->rndid[7]&0x80)) 16281da177e4SLinus Torvalds goto regen; 16291da177e4SLinus Torvalds if ((idev->rndid[0]|idev->rndid[1]) == 0) { 16301da177e4SLinus Torvalds if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe) 16311da177e4SLinus Torvalds goto regen; 16321da177e4SLinus Torvalds if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00) 16331da177e4SLinus Torvalds goto regen; 16341da177e4SLinus Torvalds } 16351da177e4SLinus Torvalds 16361da177e4SLinus Torvalds return 0; 16371da177e4SLinus Torvalds } 16381da177e4SLinus Torvalds 16391da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data) 16401da177e4SLinus Torvalds { 16411da177e4SLinus Torvalds struct inet6_dev *idev = (struct inet6_dev *) data; 16421da177e4SLinus Torvalds unsigned long expires; 16431da177e4SLinus Torvalds 16448814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 16451da177e4SLinus Torvalds write_lock_bh(&idev->lock); 16461da177e4SLinus Torvalds 16471da177e4SLinus Torvalds if (idev->dead) 16481da177e4SLinus Torvalds goto out; 16491da177e4SLinus Torvalds 16501da177e4SLinus Torvalds if (__ipv6_regen_rndid(idev) < 0) 16511da177e4SLinus Torvalds goto out; 16521da177e4SLinus Torvalds 16531da177e4SLinus Torvalds expires = jiffies + 16541da177e4SLinus Torvalds idev->cnf.temp_prefered_lft * HZ - 1655784e2710SBen Hutchings idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - 1656784e2710SBen Hutchings idev->cnf.max_desync_factor * HZ; 16571da177e4SLinus Torvalds if (time_before(expires, jiffies)) { 16581da177e4SLinus Torvalds printk(KERN_WARNING 16591da177e4SLinus Torvalds "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n", 16601da177e4SLinus Torvalds idev->dev->name); 16611da177e4SLinus Torvalds goto out; 16621da177e4SLinus Torvalds } 16631da177e4SLinus Torvalds 16641da177e4SLinus Torvalds if (!mod_timer(&idev->regen_timer, expires)) 16651da177e4SLinus Torvalds in6_dev_hold(idev); 16661da177e4SLinus Torvalds 16671da177e4SLinus Torvalds out: 16681da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 16698814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 16701da177e4SLinus Torvalds in6_dev_put(idev); 16711da177e4SLinus Torvalds } 16721da177e4SLinus Torvalds 16731da177e4SLinus Torvalds static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) { 16741da177e4SLinus Torvalds int ret = 0; 16751da177e4SLinus Torvalds 16761da177e4SLinus Torvalds if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0) 16771da177e4SLinus Torvalds ret = __ipv6_regen_rndid(idev); 16781da177e4SLinus Torvalds return ret; 16791da177e4SLinus Torvalds } 16801da177e4SLinus Torvalds #endif 16811da177e4SLinus Torvalds 16821da177e4SLinus Torvalds /* 16831da177e4SLinus Torvalds * Add prefix route. 16841da177e4SLinus Torvalds */ 16851da177e4SLinus Torvalds 16861da177e4SLinus Torvalds static void 16871da177e4SLinus Torvalds addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev, 1688e431b8c0SJamal Hadi Salim unsigned long expires, u32 flags) 16891da177e4SLinus Torvalds { 169086872cb5SThomas Graf struct fib6_config cfg = { 169186872cb5SThomas Graf .fc_table = RT6_TABLE_PREFIX, 169286872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 169386872cb5SThomas Graf .fc_ifindex = dev->ifindex, 169486872cb5SThomas Graf .fc_expires = expires, 169586872cb5SThomas Graf .fc_dst_len = plen, 169686872cb5SThomas Graf .fc_flags = RTF_UP | flags, 1697c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 1698f410a1fbSStephen Hemminger .fc_protocol = RTPROT_KERNEL, 169986872cb5SThomas Graf }; 17001da177e4SLinus Torvalds 170186872cb5SThomas Graf ipv6_addr_copy(&cfg.fc_dst, pfx); 17021da177e4SLinus Torvalds 17031da177e4SLinus Torvalds /* Prevent useless cloning on PtP SIT. 17041da177e4SLinus Torvalds This thing is done here expecting that the whole 17051da177e4SLinus Torvalds class of non-broadcast devices need not cloning. 17061da177e4SLinus Torvalds */ 17070be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 17081da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT)) 170986872cb5SThomas Graf cfg.fc_flags |= RTF_NONEXTHOP; 17100be669bbSJoerg Roedel #endif 17111da177e4SLinus Torvalds 171286872cb5SThomas Graf ip6_route_add(&cfg); 17131da177e4SLinus Torvalds } 17141da177e4SLinus Torvalds 17151da177e4SLinus Torvalds /* Create "default" multicast route to the interface */ 17161da177e4SLinus Torvalds 17171da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev) 17181da177e4SLinus Torvalds { 171986872cb5SThomas Graf struct fib6_config cfg = { 172086872cb5SThomas Graf .fc_table = RT6_TABLE_LOCAL, 172186872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 172286872cb5SThomas Graf .fc_ifindex = dev->ifindex, 172386872cb5SThomas Graf .fc_dst_len = 8, 172486872cb5SThomas Graf .fc_flags = RTF_UP, 1725c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 172686872cb5SThomas Graf }; 17271da177e4SLinus Torvalds 172886872cb5SThomas Graf ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0); 172986872cb5SThomas Graf 173086872cb5SThomas Graf ip6_route_add(&cfg); 17311da177e4SLinus Torvalds } 17321da177e4SLinus Torvalds 17330be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 17341da177e4SLinus Torvalds static void sit_route_add(struct net_device *dev) 17351da177e4SLinus Torvalds { 173686872cb5SThomas Graf struct fib6_config cfg = { 173786872cb5SThomas Graf .fc_table = RT6_TABLE_MAIN, 173886872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 173986872cb5SThomas Graf .fc_ifindex = dev->ifindex, 174086872cb5SThomas Graf .fc_dst_len = 96, 174186872cb5SThomas Graf .fc_flags = RTF_UP | RTF_NONEXTHOP, 1742c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 174386872cb5SThomas Graf }; 17441da177e4SLinus Torvalds 17451da177e4SLinus Torvalds /* prefix length - 96 bits "::d.d.d.d" */ 174686872cb5SThomas Graf ip6_route_add(&cfg); 17471da177e4SLinus Torvalds } 17480be669bbSJoerg Roedel #endif 17491da177e4SLinus Torvalds 17501da177e4SLinus Torvalds static void addrconf_add_lroute(struct net_device *dev) 17511da177e4SLinus Torvalds { 17521da177e4SLinus Torvalds struct in6_addr addr; 17531da177e4SLinus Torvalds 17541da177e4SLinus Torvalds ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0); 17551da177e4SLinus Torvalds addrconf_prefix_route(&addr, 64, dev, 0, 0); 17561da177e4SLinus Torvalds } 17571da177e4SLinus Torvalds 17581da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev) 17591da177e4SLinus Torvalds { 17601da177e4SLinus Torvalds struct inet6_dev *idev; 17611da177e4SLinus Torvalds 17621da177e4SLinus Torvalds ASSERT_RTNL(); 17631da177e4SLinus Torvalds 1764e21e8467SStephen Hemminger idev = ipv6_find_idev(dev); 1765e21e8467SStephen Hemminger if (!idev) 176664e724f6SBrian Haley return ERR_PTR(-ENOBUFS); 176764e724f6SBrian Haley 176864e724f6SBrian Haley if (idev->cnf.disable_ipv6) 176964e724f6SBrian Haley return ERR_PTR(-EACCES); 17701da177e4SLinus Torvalds 17711da177e4SLinus Torvalds /* Add default multicast route */ 17721da177e4SLinus Torvalds addrconf_add_mroute(dev); 17731da177e4SLinus Torvalds 17741da177e4SLinus Torvalds /* Add link local route */ 17751da177e4SLinus Torvalds addrconf_add_lroute(dev); 17761da177e4SLinus Torvalds return idev; 17771da177e4SLinus Torvalds } 17781da177e4SLinus Torvalds 17791da177e4SLinus Torvalds void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len) 17801da177e4SLinus Torvalds { 17811da177e4SLinus Torvalds struct prefix_info *pinfo; 17821da177e4SLinus Torvalds __u32 valid_lft; 17831da177e4SLinus Torvalds __u32 prefered_lft; 17841da177e4SLinus Torvalds int addr_type; 17851da177e4SLinus Torvalds struct inet6_dev *in6_dev; 178656d417b1SBrian Haley struct net *net = dev_net(dev); 17871da177e4SLinus Torvalds 17881da177e4SLinus Torvalds pinfo = (struct prefix_info *) opt; 17891da177e4SLinus Torvalds 17901da177e4SLinus Torvalds if (len < sizeof(struct prefix_info)) { 17911da177e4SLinus Torvalds ADBG(("addrconf: prefix option too short\n")); 17921da177e4SLinus Torvalds return; 17931da177e4SLinus Torvalds } 17941da177e4SLinus Torvalds 17951da177e4SLinus Torvalds /* 17961da177e4SLinus Torvalds * Validation checks ([ADDRCONF], page 19) 17971da177e4SLinus Torvalds */ 17981da177e4SLinus Torvalds 17991da177e4SLinus Torvalds addr_type = ipv6_addr_type(&pinfo->prefix); 18001da177e4SLinus Torvalds 18011da177e4SLinus Torvalds if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)) 18021da177e4SLinus Torvalds return; 18031da177e4SLinus Torvalds 18041da177e4SLinus Torvalds valid_lft = ntohl(pinfo->valid); 18051da177e4SLinus Torvalds prefered_lft = ntohl(pinfo->prefered); 18061da177e4SLinus Torvalds 18071da177e4SLinus Torvalds if (prefered_lft > valid_lft) { 18081da177e4SLinus Torvalds if (net_ratelimit()) 18091da177e4SLinus Torvalds printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n"); 18101da177e4SLinus Torvalds return; 18111da177e4SLinus Torvalds } 18121da177e4SLinus Torvalds 18131da177e4SLinus Torvalds in6_dev = in6_dev_get(dev); 18141da177e4SLinus Torvalds 18151da177e4SLinus Torvalds if (in6_dev == NULL) { 18161da177e4SLinus Torvalds if (net_ratelimit()) 18171da177e4SLinus Torvalds printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name); 18181da177e4SLinus Torvalds return; 18191da177e4SLinus Torvalds } 18201da177e4SLinus Torvalds 18211da177e4SLinus Torvalds /* 18221da177e4SLinus Torvalds * Two things going on here: 18231da177e4SLinus Torvalds * 1) Add routes for on-link prefixes 18241da177e4SLinus Torvalds * 2) Configure prefixes with the auto flag set 18251da177e4SLinus Torvalds */ 18261da177e4SLinus Torvalds 18274bed72e4SYOSHIFUJI Hideaki if (pinfo->onlink) { 18284bed72e4SYOSHIFUJI Hideaki struct rt6_info *rt; 18294bed72e4SYOSHIFUJI Hideaki unsigned long rt_expires; 18304bed72e4SYOSHIFUJI Hideaki 18311da177e4SLinus Torvalds /* Avoid arithmetic overflow. Really, we could 18326f704992SYOSHIFUJI Hideaki * save rt_expires in seconds, likely valid_lft, 18336f704992SYOSHIFUJI Hideaki * but it would require division in fib gc, that it 18346f704992SYOSHIFUJI Hideaki * not good. 18351da177e4SLinus Torvalds */ 18364bed72e4SYOSHIFUJI Hideaki if (HZ > USER_HZ) 18374bed72e4SYOSHIFUJI Hideaki rt_expires = addrconf_timeout_fixup(valid_lft, HZ); 18384bed72e4SYOSHIFUJI Hideaki else 18394bed72e4SYOSHIFUJI Hideaki rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ); 18403dd4bc68SYOSHIFUJI Hideaki 18414bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(rt_expires)) 18424bed72e4SYOSHIFUJI Hideaki rt_expires *= HZ; 18431da177e4SLinus Torvalds 184456d417b1SBrian Haley rt = rt6_lookup(net, &pinfo->prefix, NULL, 18456fda7350SBenjamin Thery dev->ifindex, 1); 18461da177e4SLinus Torvalds 18472b5ead46SYOSHIFUJI Hideaki if (rt && addrconf_is_prefix_route(rt)) { 18486f704992SYOSHIFUJI Hideaki /* Autoconf prefix route */ 18491da177e4SLinus Torvalds if (valid_lft == 0) { 1850e0a1ad73SThomas Graf ip6_del_rt(rt); 18511da177e4SLinus Torvalds rt = NULL; 18524bed72e4SYOSHIFUJI Hideaki } else if (addrconf_finite_timeout(rt_expires)) { 18536f704992SYOSHIFUJI Hideaki /* not infinity */ 18543dd4bc68SYOSHIFUJI Hideaki rt->rt6i_expires = jiffies + rt_expires; 18556f704992SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_EXPIRES; 18566f704992SYOSHIFUJI Hideaki } else { 18576f704992SYOSHIFUJI Hideaki rt->rt6i_flags &= ~RTF_EXPIRES; 18586f704992SYOSHIFUJI Hideaki rt->rt6i_expires = 0; 18591da177e4SLinus Torvalds } 18601da177e4SLinus Torvalds } else if (valid_lft) { 18616f704992SYOSHIFUJI Hideaki clock_t expires = 0; 18624bed72e4SYOSHIFUJI Hideaki int flags = RTF_ADDRCONF | RTF_PREFIX_RT; 18634bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(rt_expires)) { 18646f704992SYOSHIFUJI Hideaki /* not infinity */ 18656f704992SYOSHIFUJI Hideaki flags |= RTF_EXPIRES; 18666f704992SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(rt_expires); 18676f704992SYOSHIFUJI Hideaki } 18681da177e4SLinus Torvalds addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len, 18696f704992SYOSHIFUJI Hideaki dev, expires, flags); 18701da177e4SLinus Torvalds } 18711da177e4SLinus Torvalds if (rt) 1872d8d1f30bSChangli Gao dst_release(&rt->dst); 18731da177e4SLinus Torvalds } 18741da177e4SLinus Torvalds 18751da177e4SLinus Torvalds /* Try to figure out our local address for this prefix */ 18761da177e4SLinus Torvalds 18771da177e4SLinus Torvalds if (pinfo->autoconf && in6_dev->cnf.autoconf) { 18781da177e4SLinus Torvalds struct inet6_ifaddr * ifp; 18791da177e4SLinus Torvalds struct in6_addr addr; 18801da177e4SLinus Torvalds int create = 0, update_lft = 0; 18811da177e4SLinus Torvalds 18821da177e4SLinus Torvalds if (pinfo->prefix_len == 64) { 18831da177e4SLinus Torvalds memcpy(&addr, &pinfo->prefix, 8); 18841da177e4SLinus Torvalds if (ipv6_generate_eui64(addr.s6_addr + 8, dev) && 18851da177e4SLinus Torvalds ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) { 18861da177e4SLinus Torvalds in6_dev_put(in6_dev); 18871da177e4SLinus Torvalds return; 18881da177e4SLinus Torvalds } 18891da177e4SLinus Torvalds goto ok; 18901da177e4SLinus Torvalds } 18911da177e4SLinus Torvalds if (net_ratelimit()) 18921da177e4SLinus Torvalds printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n", 18931da177e4SLinus Torvalds pinfo->prefix_len); 18941da177e4SLinus Torvalds in6_dev_put(in6_dev); 18951da177e4SLinus Torvalds return; 18961da177e4SLinus Torvalds 18971da177e4SLinus Torvalds ok: 18981da177e4SLinus Torvalds 189953b7997fSYOSHIFUJI Hideaki ifp = ipv6_get_ifaddr(net, &addr, dev, 1); 19001da177e4SLinus Torvalds 19011da177e4SLinus Torvalds if (ifp == NULL && valid_lft) { 19021da177e4SLinus Torvalds int max_addresses = in6_dev->cnf.max_addresses; 190395c385b4SNeil Horman u32 addr_flags = 0; 190495c385b4SNeil Horman 190595c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 190695c385b4SNeil Horman if (in6_dev->cnf.optimistic_dad && 190753b7997fSYOSHIFUJI Hideaki !net->ipv6.devconf_all->forwarding) 190895c385b4SNeil Horman addr_flags = IFA_F_OPTIMISTIC; 190995c385b4SNeil Horman #endif 19101da177e4SLinus Torvalds 19111da177e4SLinus Torvalds /* Do not allow to create too much of autoconfigured 19121da177e4SLinus Torvalds * addresses; this would be too easy way to crash kernel. 19131da177e4SLinus Torvalds */ 19141da177e4SLinus Torvalds if (!max_addresses || 19151da177e4SLinus Torvalds ipv6_count_addresses(in6_dev) < max_addresses) 19161da177e4SLinus Torvalds ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len, 191795c385b4SNeil Horman addr_type&IPV6_ADDR_SCOPE_MASK, 191895c385b4SNeil Horman addr_flags); 19191da177e4SLinus Torvalds 19201da177e4SLinus Torvalds if (!ifp || IS_ERR(ifp)) { 19211da177e4SLinus Torvalds in6_dev_put(in6_dev); 19221da177e4SLinus Torvalds return; 19231da177e4SLinus Torvalds } 19241da177e4SLinus Torvalds 19251da177e4SLinus Torvalds update_lft = create = 1; 19261da177e4SLinus Torvalds ifp->cstamp = jiffies; 19271da177e4SLinus Torvalds addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT); 19281da177e4SLinus Torvalds } 19291da177e4SLinus Torvalds 19301da177e4SLinus Torvalds if (ifp) { 19311da177e4SLinus Torvalds int flags; 19321da177e4SLinus Torvalds unsigned long now; 19331da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 19341da177e4SLinus Torvalds struct inet6_ifaddr *ift; 19351da177e4SLinus Torvalds #endif 19361da177e4SLinus Torvalds u32 stored_lft; 19371da177e4SLinus Torvalds 19381da177e4SLinus Torvalds /* update lifetime (RFC2462 5.5.3 e) */ 19391da177e4SLinus Torvalds spin_lock(&ifp->lock); 19401da177e4SLinus Torvalds now = jiffies; 19411da177e4SLinus Torvalds if (ifp->valid_lft > (now - ifp->tstamp) / HZ) 19421da177e4SLinus Torvalds stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ; 19431da177e4SLinus Torvalds else 19441da177e4SLinus Torvalds stored_lft = 0; 19451da177e4SLinus Torvalds if (!update_lft && stored_lft) { 19461da177e4SLinus Torvalds if (valid_lft > MIN_VALID_LIFETIME || 19471da177e4SLinus Torvalds valid_lft > stored_lft) 19481da177e4SLinus Torvalds update_lft = 1; 19491da177e4SLinus Torvalds else if (stored_lft <= MIN_VALID_LIFETIME) { 19501da177e4SLinus Torvalds /* valid_lft <= stored_lft is always true */ 1951a1ed0526SBrian Haley /* 1952a1ed0526SBrian Haley * RFC 4862 Section 5.5.3e: 1953a1ed0526SBrian Haley * "Note that the preferred lifetime of 1954a1ed0526SBrian Haley * the corresponding address is always 1955a1ed0526SBrian Haley * reset to the Preferred Lifetime in 1956a1ed0526SBrian Haley * the received Prefix Information 1957a1ed0526SBrian Haley * option, regardless of whether the 1958a1ed0526SBrian Haley * valid lifetime is also reset or 1959a1ed0526SBrian Haley * ignored." 1960a1ed0526SBrian Haley * 1961a1ed0526SBrian Haley * So if the preferred lifetime in 1962a1ed0526SBrian Haley * this advertisement is different 1963a1ed0526SBrian Haley * than what we have stored, but the 1964a1ed0526SBrian Haley * valid lifetime is invalid, just 1965a1ed0526SBrian Haley * reset prefered_lft. 1966a1ed0526SBrian Haley * 1967a1ed0526SBrian Haley * We must set the valid lifetime 1968a1ed0526SBrian Haley * to the stored lifetime since we'll 1969a1ed0526SBrian Haley * be updating the timestamp below, 1970a1ed0526SBrian Haley * else we'll set it back to the 197125985edcSLucas De Marchi * minimum. 1972a1ed0526SBrian Haley */ 1973a1ed0526SBrian Haley if (prefered_lft != ifp->prefered_lft) { 1974a1ed0526SBrian Haley valid_lft = stored_lft; 1975a1ed0526SBrian Haley update_lft = 1; 1976a1ed0526SBrian Haley } 19771da177e4SLinus Torvalds } else { 19781da177e4SLinus Torvalds valid_lft = MIN_VALID_LIFETIME; 19791da177e4SLinus Torvalds if (valid_lft < prefered_lft) 19801da177e4SLinus Torvalds prefered_lft = valid_lft; 19811da177e4SLinus Torvalds update_lft = 1; 19821da177e4SLinus Torvalds } 19831da177e4SLinus Torvalds } 19841da177e4SLinus Torvalds 19851da177e4SLinus Torvalds if (update_lft) { 19861da177e4SLinus Torvalds ifp->valid_lft = valid_lft; 19871da177e4SLinus Torvalds ifp->prefered_lft = prefered_lft; 19881da177e4SLinus Torvalds ifp->tstamp = now; 19891da177e4SLinus Torvalds flags = ifp->flags; 19901da177e4SLinus Torvalds ifp->flags &= ~IFA_F_DEPRECATED; 19911da177e4SLinus Torvalds spin_unlock(&ifp->lock); 19921da177e4SLinus Torvalds 19931da177e4SLinus Torvalds if (!(flags&IFA_F_TENTATIVE)) 19941da177e4SLinus Torvalds ipv6_ifa_notify(0, ifp); 19951da177e4SLinus Torvalds } else 19961da177e4SLinus Torvalds spin_unlock(&ifp->lock); 19971da177e4SLinus Torvalds 19981da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 19991da177e4SLinus Torvalds read_lock_bh(&in6_dev->lock); 20001da177e4SLinus Torvalds /* update all temporary addresses in the list */ 2001372e6c8fSstephen hemminger list_for_each_entry(ift, &in6_dev->tempaddr_list, tmp_list) { 20021da177e4SLinus Torvalds /* 20031da177e4SLinus Torvalds * When adjusting the lifetimes of an existing 20041da177e4SLinus Torvalds * temporary address, only lower the lifetimes. 20051da177e4SLinus Torvalds * Implementations must not increase the 20061da177e4SLinus Torvalds * lifetimes of an existing temporary address 20071da177e4SLinus Torvalds * when processing a Prefix Information Option. 20081da177e4SLinus Torvalds */ 2009c6fbfac2SBenoit Boissinot if (ifp != ift->ifpub) 2010c6fbfac2SBenoit Boissinot continue; 2011c6fbfac2SBenoit Boissinot 20121da177e4SLinus Torvalds spin_lock(&ift->lock); 20131da177e4SLinus Torvalds flags = ift->flags; 20141da177e4SLinus Torvalds if (ift->valid_lft > valid_lft && 20151da177e4SLinus Torvalds ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ) 20161da177e4SLinus Torvalds ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ; 20171da177e4SLinus Torvalds if (ift->prefered_lft > prefered_lft && 20181da177e4SLinus Torvalds ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ) 20191da177e4SLinus Torvalds ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ; 20201da177e4SLinus Torvalds spin_unlock(&ift->lock); 20211da177e4SLinus Torvalds if (!(flags&IFA_F_TENTATIVE)) 20221da177e4SLinus Torvalds ipv6_ifa_notify(0, ift); 20231da177e4SLinus Torvalds } 20241da177e4SLinus Torvalds 2025aed65501SGlenn Wurster if ((create || list_empty(&in6_dev->tempaddr_list)) && in6_dev->cnf.use_tempaddr > 0) { 20261da177e4SLinus Torvalds /* 20271da177e4SLinus Torvalds * When a new public address is created as described in [ADDRCONF], 2028aed65501SGlenn Wurster * also create a new temporary address. Also create a temporary 2029aed65501SGlenn Wurster * address if it's enabled but no temporary address currently exists. 20301da177e4SLinus Torvalds */ 20311da177e4SLinus Torvalds read_unlock_bh(&in6_dev->lock); 20321da177e4SLinus Torvalds ipv6_create_tempaddr(ifp, NULL); 20331da177e4SLinus Torvalds } else { 20341da177e4SLinus Torvalds read_unlock_bh(&in6_dev->lock); 20351da177e4SLinus Torvalds } 20361da177e4SLinus Torvalds #endif 20371da177e4SLinus Torvalds in6_ifa_put(ifp); 20381da177e4SLinus Torvalds addrconf_verify(0); 20391da177e4SLinus Torvalds } 20401da177e4SLinus Torvalds } 20411da177e4SLinus Torvalds inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo); 20421da177e4SLinus Torvalds in6_dev_put(in6_dev); 20431da177e4SLinus Torvalds } 20441da177e4SLinus Torvalds 20451da177e4SLinus Torvalds /* 20461da177e4SLinus Torvalds * Set destination address. 20471da177e4SLinus Torvalds * Special case for SIT interfaces where we create a new "virtual" 20481da177e4SLinus Torvalds * device. 20491da177e4SLinus Torvalds */ 2050af284937SDaniel Lezcano int addrconf_set_dstaddr(struct net *net, void __user *arg) 20511da177e4SLinus Torvalds { 20521da177e4SLinus Torvalds struct in6_ifreq ireq; 20531da177e4SLinus Torvalds struct net_device *dev; 20541da177e4SLinus Torvalds int err = -EINVAL; 20551da177e4SLinus Torvalds 20561da177e4SLinus Torvalds rtnl_lock(); 20571da177e4SLinus Torvalds 20581da177e4SLinus Torvalds err = -EFAULT; 20591da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 20601da177e4SLinus Torvalds goto err_exit; 20611da177e4SLinus Torvalds 2062af284937SDaniel Lezcano dev = __dev_get_by_index(net, ireq.ifr6_ifindex); 20631da177e4SLinus Torvalds 20641da177e4SLinus Torvalds err = -ENODEV; 20651da177e4SLinus Torvalds if (dev == NULL) 20661da177e4SLinus Torvalds goto err_exit; 20671da177e4SLinus Torvalds 20680be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 20691da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT) { 20705bc3eb7eSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 20711da177e4SLinus Torvalds struct ifreq ifr; 20721da177e4SLinus Torvalds struct ip_tunnel_parm p; 20731da177e4SLinus Torvalds 20741da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 20751da177e4SLinus Torvalds if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4)) 20761da177e4SLinus Torvalds goto err_exit; 20771da177e4SLinus Torvalds 20781da177e4SLinus Torvalds memset(&p, 0, sizeof(p)); 20791da177e4SLinus Torvalds p.iph.daddr = ireq.ifr6_addr.s6_addr32[3]; 20801da177e4SLinus Torvalds p.iph.saddr = 0; 20811da177e4SLinus Torvalds p.iph.version = 4; 20821da177e4SLinus Torvalds p.iph.ihl = 5; 20831da177e4SLinus Torvalds p.iph.protocol = IPPROTO_IPV6; 20841da177e4SLinus Torvalds p.iph.ttl = 64; 2085d20b3109SStephen Hemminger ifr.ifr_ifru.ifru_data = (__force void __user *)&p; 20861da177e4SLinus Torvalds 20875bc3eb7eSStephen Hemminger if (ops->ndo_do_ioctl) { 20885bc3eb7eSStephen Hemminger mm_segment_t oldfs = get_fs(); 20895bc3eb7eSStephen Hemminger 20905bc3eb7eSStephen Hemminger set_fs(KERNEL_DS); 20915bc3eb7eSStephen Hemminger err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL); 20921da177e4SLinus Torvalds set_fs(oldfs); 20935bc3eb7eSStephen Hemminger } else 20945bc3eb7eSStephen Hemminger err = -EOPNOTSUPP; 20951da177e4SLinus Torvalds 20961da177e4SLinus Torvalds if (err == 0) { 20971da177e4SLinus Torvalds err = -ENOBUFS; 2098af284937SDaniel Lezcano dev = __dev_get_by_name(net, p.name); 2099af284937SDaniel Lezcano if (!dev) 21001da177e4SLinus Torvalds goto err_exit; 21011da177e4SLinus Torvalds err = dev_open(dev); 21021da177e4SLinus Torvalds } 21031da177e4SLinus Torvalds } 21040be669bbSJoerg Roedel #endif 21051da177e4SLinus Torvalds 21061da177e4SLinus Torvalds err_exit: 21071da177e4SLinus Torvalds rtnl_unlock(); 21081da177e4SLinus Torvalds return err; 21091da177e4SLinus Torvalds } 21101da177e4SLinus Torvalds 21111da177e4SLinus Torvalds /* 21121da177e4SLinus Torvalds * Manual configuration of address on an interface 21131da177e4SLinus Torvalds */ 2114af284937SDaniel Lezcano static int inet6_addr_add(struct net *net, int ifindex, struct in6_addr *pfx, 211524ef0da7SThomas Graf unsigned int plen, __u8 ifa_flags, __u32 prefered_lft, 2116af284937SDaniel Lezcano __u32 valid_lft) 21171da177e4SLinus Torvalds { 21181da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 21191da177e4SLinus Torvalds struct inet6_dev *idev; 21201da177e4SLinus Torvalds struct net_device *dev; 21211da177e4SLinus Torvalds int scope; 21226f704992SYOSHIFUJI Hideaki u32 flags; 21236f704992SYOSHIFUJI Hideaki clock_t expires; 21244bed72e4SYOSHIFUJI Hideaki unsigned long timeout; 21251da177e4SLinus Torvalds 21261da177e4SLinus Torvalds ASSERT_RTNL(); 21271da177e4SLinus Torvalds 212824ef0da7SThomas Graf if (plen > 128) 212924ef0da7SThomas Graf return -EINVAL; 213024ef0da7SThomas Graf 21310778769dSNoriaki TAKAMIYA /* check the lifetime */ 21320778769dSNoriaki TAKAMIYA if (!valid_lft || prefered_lft > valid_lft) 21330778769dSNoriaki TAKAMIYA return -EINVAL; 21340778769dSNoriaki TAKAMIYA 2135af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifindex); 2136af284937SDaniel Lezcano if (!dev) 21371da177e4SLinus Torvalds return -ENODEV; 21381da177e4SLinus Torvalds 213964e724f6SBrian Haley idev = addrconf_add_dev(dev); 214064e724f6SBrian Haley if (IS_ERR(idev)) 214164e724f6SBrian Haley return PTR_ERR(idev); 21421da177e4SLinus Torvalds 21431da177e4SLinus Torvalds scope = ipv6_addr_scope(pfx); 21441da177e4SLinus Torvalds 21454bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(valid_lft, HZ); 21464bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 21474bed72e4SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(timeout * HZ); 21484bed72e4SYOSHIFUJI Hideaki valid_lft = timeout; 21496f704992SYOSHIFUJI Hideaki flags = RTF_EXPIRES; 21504bed72e4SYOSHIFUJI Hideaki } else { 21514bed72e4SYOSHIFUJI Hideaki expires = 0; 21524bed72e4SYOSHIFUJI Hideaki flags = 0; 21534bed72e4SYOSHIFUJI Hideaki ifa_flags |= IFA_F_PERMANENT; 21546f704992SYOSHIFUJI Hideaki } 21550778769dSNoriaki TAKAMIYA 21564bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(prefered_lft, HZ); 21574bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 21584bed72e4SYOSHIFUJI Hideaki if (timeout == 0) 21590778769dSNoriaki TAKAMIYA ifa_flags |= IFA_F_DEPRECATED; 21604bed72e4SYOSHIFUJI Hideaki prefered_lft = timeout; 21614bed72e4SYOSHIFUJI Hideaki } 21620778769dSNoriaki TAKAMIYA 21630778769dSNoriaki TAKAMIYA ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags); 21640778769dSNoriaki TAKAMIYA 21651da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 216606aebfb7SHerbert Xu spin_lock_bh(&ifp->lock); 21670778769dSNoriaki TAKAMIYA ifp->valid_lft = valid_lft; 21680778769dSNoriaki TAKAMIYA ifp->prefered_lft = prefered_lft; 21690778769dSNoriaki TAKAMIYA ifp->tstamp = jiffies; 217006aebfb7SHerbert Xu spin_unlock_bh(&ifp->lock); 21710778769dSNoriaki TAKAMIYA 217246d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 21736f704992SYOSHIFUJI Hideaki expires, flags); 217495c385b4SNeil Horman /* 217595c385b4SNeil Horman * Note that section 3.1 of RFC 4429 indicates 217695c385b4SNeil Horman * that the Optimistic flag should not be set for 217795c385b4SNeil Horman * manually configured addresses 217895c385b4SNeil Horman */ 21791da177e4SLinus Torvalds addrconf_dad_start(ifp, 0); 21801da177e4SLinus Torvalds in6_ifa_put(ifp); 21810778769dSNoriaki TAKAMIYA addrconf_verify(0); 21821da177e4SLinus Torvalds return 0; 21831da177e4SLinus Torvalds } 21841da177e4SLinus Torvalds 21851da177e4SLinus Torvalds return PTR_ERR(ifp); 21861da177e4SLinus Torvalds } 21871da177e4SLinus Torvalds 2188af284937SDaniel Lezcano static int inet6_addr_del(struct net *net, int ifindex, struct in6_addr *pfx, 218924ef0da7SThomas Graf unsigned int plen) 21901da177e4SLinus Torvalds { 21911da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 21921da177e4SLinus Torvalds struct inet6_dev *idev; 21931da177e4SLinus Torvalds struct net_device *dev; 21941da177e4SLinus Torvalds 219524ef0da7SThomas Graf if (plen > 128) 219624ef0da7SThomas Graf return -EINVAL; 219724ef0da7SThomas Graf 2198af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifindex); 2199af284937SDaniel Lezcano if (!dev) 22001da177e4SLinus Torvalds return -ENODEV; 22011da177e4SLinus Torvalds 22021da177e4SLinus Torvalds if ((idev = __in6_dev_get(dev)) == NULL) 22031da177e4SLinus Torvalds return -ENXIO; 22041da177e4SLinus Torvalds 22051da177e4SLinus Torvalds read_lock_bh(&idev->lock); 2206502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 22071da177e4SLinus Torvalds if (ifp->prefix_len == plen && 22081da177e4SLinus Torvalds ipv6_addr_equal(pfx, &ifp->addr)) { 22091da177e4SLinus Torvalds in6_ifa_hold(ifp); 22101da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 22111da177e4SLinus Torvalds 22121da177e4SLinus Torvalds ipv6_del_addr(ifp); 22131da177e4SLinus Torvalds 22141da177e4SLinus Torvalds /* If the last address is deleted administratively, 22151da177e4SLinus Torvalds disable IPv6 on this interface. 22161da177e4SLinus Torvalds */ 2217502a2ffdSstephen hemminger if (list_empty(&idev->addr_list)) 22181da177e4SLinus Torvalds addrconf_ifdown(idev->dev, 1); 22191da177e4SLinus Torvalds return 0; 22201da177e4SLinus Torvalds } 22211da177e4SLinus Torvalds } 22221da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 22231da177e4SLinus Torvalds return -EADDRNOTAVAIL; 22241da177e4SLinus Torvalds } 22251da177e4SLinus Torvalds 22261da177e4SLinus Torvalds 2227af284937SDaniel Lezcano int addrconf_add_ifaddr(struct net *net, void __user *arg) 22281da177e4SLinus Torvalds { 22291da177e4SLinus Torvalds struct in6_ifreq ireq; 22301da177e4SLinus Torvalds int err; 22311da177e4SLinus Torvalds 22321da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 22331da177e4SLinus Torvalds return -EPERM; 22341da177e4SLinus Torvalds 22351da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 22361da177e4SLinus Torvalds return -EFAULT; 22371da177e4SLinus Torvalds 22381da177e4SLinus Torvalds rtnl_lock(); 2239af284937SDaniel Lezcano err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, 2240af284937SDaniel Lezcano ireq.ifr6_prefixlen, IFA_F_PERMANENT, 2241af284937SDaniel Lezcano INFINITY_LIFE_TIME, INFINITY_LIFE_TIME); 22421da177e4SLinus Torvalds rtnl_unlock(); 22431da177e4SLinus Torvalds return err; 22441da177e4SLinus Torvalds } 22451da177e4SLinus Torvalds 2246af284937SDaniel Lezcano int addrconf_del_ifaddr(struct net *net, void __user *arg) 22471da177e4SLinus Torvalds { 22481da177e4SLinus Torvalds struct in6_ifreq ireq; 22491da177e4SLinus Torvalds int err; 22501da177e4SLinus Torvalds 22511da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 22521da177e4SLinus Torvalds return -EPERM; 22531da177e4SLinus Torvalds 22541da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 22551da177e4SLinus Torvalds return -EFAULT; 22561da177e4SLinus Torvalds 22571da177e4SLinus Torvalds rtnl_lock(); 2258af284937SDaniel Lezcano err = inet6_addr_del(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, 2259af284937SDaniel Lezcano ireq.ifr6_prefixlen); 22601da177e4SLinus Torvalds rtnl_unlock(); 22611da177e4SLinus Torvalds return err; 22621da177e4SLinus Torvalds } 22631da177e4SLinus Torvalds 2264b5f348e5SIlpo Järvinen static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr, 2265b5f348e5SIlpo Järvinen int plen, int scope) 2266b5f348e5SIlpo Järvinen { 2267b5f348e5SIlpo Järvinen struct inet6_ifaddr *ifp; 2268b5f348e5SIlpo Järvinen 2269b5f348e5SIlpo Järvinen ifp = ipv6_add_addr(idev, addr, plen, scope, IFA_F_PERMANENT); 2270b5f348e5SIlpo Järvinen if (!IS_ERR(ifp)) { 2271b5f348e5SIlpo Järvinen spin_lock_bh(&ifp->lock); 2272b5f348e5SIlpo Järvinen ifp->flags &= ~IFA_F_TENTATIVE; 2273b5f348e5SIlpo Järvinen spin_unlock_bh(&ifp->lock); 2274b5f348e5SIlpo Järvinen ipv6_ifa_notify(RTM_NEWADDR, ifp); 2275b5f348e5SIlpo Järvinen in6_ifa_put(ifp); 2276b5f348e5SIlpo Järvinen } 2277b5f348e5SIlpo Järvinen } 2278b5f348e5SIlpo Järvinen 22790be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 22801da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev) 22811da177e4SLinus Torvalds { 22821da177e4SLinus Torvalds struct in6_addr addr; 22831da177e4SLinus Torvalds struct net_device *dev; 2284c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 22851da177e4SLinus Torvalds int scope; 22861da177e4SLinus Torvalds 22871da177e4SLinus Torvalds ASSERT_RTNL(); 22881da177e4SLinus Torvalds 22891da177e4SLinus Torvalds memset(&addr, 0, sizeof(struct in6_addr)); 22901da177e4SLinus Torvalds memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4); 22911da177e4SLinus Torvalds 22921da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) { 22931da177e4SLinus Torvalds addr.s6_addr32[0] = htonl(0xfe800000); 22941da177e4SLinus Torvalds scope = IFA_LINK; 22951da177e4SLinus Torvalds } else { 22961da177e4SLinus Torvalds scope = IPV6_ADDR_COMPATv4; 22971da177e4SLinus Torvalds } 22981da177e4SLinus Torvalds 22991da177e4SLinus Torvalds if (addr.s6_addr32[3]) { 2300b5f348e5SIlpo Järvinen add_addr(idev, &addr, 128, scope); 23011da177e4SLinus Torvalds return; 23021da177e4SLinus Torvalds } 23031da177e4SLinus Torvalds 23046fda7350SBenjamin Thery for_each_netdev(net, dev) { 2305e5ed6399SHerbert Xu struct in_device * in_dev = __in_dev_get_rtnl(dev); 23061da177e4SLinus Torvalds if (in_dev && (dev->flags & IFF_UP)) { 23071da177e4SLinus Torvalds struct in_ifaddr * ifa; 23081da177e4SLinus Torvalds 23091da177e4SLinus Torvalds int flag = scope; 23101da177e4SLinus Torvalds 23111da177e4SLinus Torvalds for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) { 23121da177e4SLinus Torvalds int plen; 23131da177e4SLinus Torvalds 23141da177e4SLinus Torvalds addr.s6_addr32[3] = ifa->ifa_local; 23151da177e4SLinus Torvalds 23161da177e4SLinus Torvalds if (ifa->ifa_scope == RT_SCOPE_LINK) 23171da177e4SLinus Torvalds continue; 23181da177e4SLinus Torvalds if (ifa->ifa_scope >= RT_SCOPE_HOST) { 23191da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) 23201da177e4SLinus Torvalds continue; 23211da177e4SLinus Torvalds flag |= IFA_HOST; 23221da177e4SLinus Torvalds } 23231da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) 23241da177e4SLinus Torvalds plen = 64; 23251da177e4SLinus Torvalds else 23261da177e4SLinus Torvalds plen = 96; 23271da177e4SLinus Torvalds 2328b5f348e5SIlpo Järvinen add_addr(idev, &addr, plen, flag); 23291da177e4SLinus Torvalds } 23301da177e4SLinus Torvalds } 23311da177e4SLinus Torvalds } 23321da177e4SLinus Torvalds } 23330be669bbSJoerg Roedel #endif 23341da177e4SLinus Torvalds 23351da177e4SLinus Torvalds static void init_loopback(struct net_device *dev) 23361da177e4SLinus Torvalds { 23371da177e4SLinus Torvalds struct inet6_dev *idev; 23381da177e4SLinus Torvalds 23391da177e4SLinus Torvalds /* ::1 */ 23401da177e4SLinus Torvalds 23411da177e4SLinus Torvalds ASSERT_RTNL(); 23421da177e4SLinus Torvalds 23431da177e4SLinus Torvalds if ((idev = ipv6_find_idev(dev)) == NULL) { 23441da177e4SLinus Torvalds printk(KERN_DEBUG "init loopback: add_dev failed\n"); 23451da177e4SLinus Torvalds return; 23461da177e4SLinus Torvalds } 23471da177e4SLinus Torvalds 2348b5f348e5SIlpo Järvinen add_addr(idev, &in6addr_loopback, 128, IFA_HOST); 23491da177e4SLinus Torvalds } 23501da177e4SLinus Torvalds 23511da177e4SLinus Torvalds static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr) 23521da177e4SLinus Torvalds { 23531da177e4SLinus Torvalds struct inet6_ifaddr * ifp; 235495c385b4SNeil Horman u32 addr_flags = IFA_F_PERMANENT; 23551da177e4SLinus Torvalds 235695c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 235795c385b4SNeil Horman if (idev->cnf.optimistic_dad && 2358702beb87SDavid Miller !dev_net(idev->dev)->ipv6.devconf_all->forwarding) 235995c385b4SNeil Horman addr_flags |= IFA_F_OPTIMISTIC; 236095c385b4SNeil Horman #endif 236195c385b4SNeil Horman 236295c385b4SNeil Horman 236395c385b4SNeil Horman ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, addr_flags); 23641da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 236546d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0); 23661da177e4SLinus Torvalds addrconf_dad_start(ifp, 0); 23671da177e4SLinus Torvalds in6_ifa_put(ifp); 23681da177e4SLinus Torvalds } 23691da177e4SLinus Torvalds } 23701da177e4SLinus Torvalds 23711da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev) 23721da177e4SLinus Torvalds { 23731da177e4SLinus Torvalds struct in6_addr addr; 23741da177e4SLinus Torvalds struct inet6_dev * idev; 23751da177e4SLinus Torvalds 23761da177e4SLinus Torvalds ASSERT_RTNL(); 23771da177e4SLinus Torvalds 237874235a25SHerbert Xu if ((dev->type != ARPHRD_ETHER) && 237974235a25SHerbert Xu (dev->type != ARPHRD_FDDI) && 238074235a25SHerbert Xu (dev->type != ARPHRD_IEEE802_TR) && 238174235a25SHerbert Xu (dev->type != ARPHRD_ARCNET) && 238274235a25SHerbert Xu (dev->type != ARPHRD_INFINIBAND)) { 238374235a25SHerbert Xu /* Alas, we support only Ethernet autoconfiguration. */ 238474235a25SHerbert Xu return; 238574235a25SHerbert Xu } 238674235a25SHerbert Xu 23871da177e4SLinus Torvalds idev = addrconf_add_dev(dev); 238864e724f6SBrian Haley if (IS_ERR(idev)) 23891da177e4SLinus Torvalds return; 23901da177e4SLinus Torvalds 23911da177e4SLinus Torvalds memset(&addr, 0, sizeof(struct in6_addr)); 23921da177e4SLinus Torvalds addr.s6_addr32[0] = htonl(0xFE800000); 23931da177e4SLinus Torvalds 23941da177e4SLinus Torvalds if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0) 23951da177e4SLinus Torvalds addrconf_add_linklocal(idev, &addr); 23961da177e4SLinus Torvalds } 23971da177e4SLinus Torvalds 23980be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 23991da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev) 24001da177e4SLinus Torvalds { 24011da177e4SLinus Torvalds struct inet6_dev *idev; 24021da177e4SLinus Torvalds 24031da177e4SLinus Torvalds ASSERT_RTNL(); 24041da177e4SLinus Torvalds 24051da177e4SLinus Torvalds /* 24061da177e4SLinus Torvalds * Configure the tunnel with one of our IPv4 24071da177e4SLinus Torvalds * addresses... we should configure all of 24081da177e4SLinus Torvalds * our v4 addrs in the tunnel 24091da177e4SLinus Torvalds */ 24101da177e4SLinus Torvalds 24111da177e4SLinus Torvalds if ((idev = ipv6_find_idev(dev)) == NULL) { 24121da177e4SLinus Torvalds printk(KERN_DEBUG "init sit: add_dev failed\n"); 24131da177e4SLinus Torvalds return; 24141da177e4SLinus Torvalds } 24151da177e4SLinus Torvalds 2416c7dc89c0SFred L. Templin if (dev->priv_flags & IFF_ISATAP) { 2417c7dc89c0SFred L. Templin struct in6_addr addr; 2418c7dc89c0SFred L. Templin 2419c7dc89c0SFred L. Templin ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0); 2420c7dc89c0SFred L. Templin addrconf_prefix_route(&addr, 64, dev, 0, 0); 2421c7dc89c0SFred L. Templin if (!ipv6_generate_eui64(addr.s6_addr + 8, dev)) 2422c7dc89c0SFred L. Templin addrconf_add_linklocal(idev, &addr); 2423c7dc89c0SFred L. Templin return; 2424c7dc89c0SFred L. Templin } 2425c7dc89c0SFred L. Templin 24261da177e4SLinus Torvalds sit_add_v4_addrs(idev); 24271da177e4SLinus Torvalds 24281da177e4SLinus Torvalds if (dev->flags&IFF_POINTOPOINT) { 24291da177e4SLinus Torvalds addrconf_add_mroute(dev); 24301da177e4SLinus Torvalds addrconf_add_lroute(dev); 24311da177e4SLinus Torvalds } else 24321da177e4SLinus Torvalds sit_route_add(dev); 24331da177e4SLinus Torvalds } 24340be669bbSJoerg Roedel #endif 24351da177e4SLinus Torvalds 24361da177e4SLinus Torvalds static inline int 24371da177e4SLinus Torvalds ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev) 24381da177e4SLinus Torvalds { 24391da177e4SLinus Torvalds struct in6_addr lladdr; 24401da177e4SLinus Torvalds 244195c385b4SNeil Horman if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) { 24421da177e4SLinus Torvalds addrconf_add_linklocal(idev, &lladdr); 24431da177e4SLinus Torvalds return 0; 24441da177e4SLinus Torvalds } 24451da177e4SLinus Torvalds return -1; 24461da177e4SLinus Torvalds } 24471da177e4SLinus Torvalds 24481da177e4SLinus Torvalds static void ip6_tnl_add_linklocal(struct inet6_dev *idev) 24491da177e4SLinus Torvalds { 24501da177e4SLinus Torvalds struct net_device *link_dev; 2451c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 24521da177e4SLinus Torvalds 24531da177e4SLinus Torvalds /* first try to inherit the link-local address from the link device */ 24541da177e4SLinus Torvalds if (idev->dev->iflink && 24556fda7350SBenjamin Thery (link_dev = __dev_get_by_index(net, idev->dev->iflink))) { 24561da177e4SLinus Torvalds if (!ipv6_inherit_linklocal(idev, link_dev)) 24571da177e4SLinus Torvalds return; 24581da177e4SLinus Torvalds } 24591da177e4SLinus Torvalds /* then try to inherit it from any device */ 24606fda7350SBenjamin Thery for_each_netdev(net, link_dev) { 24611da177e4SLinus Torvalds if (!ipv6_inherit_linklocal(idev, link_dev)) 24621da177e4SLinus Torvalds return; 24631da177e4SLinus Torvalds } 24641da177e4SLinus Torvalds printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n"); 24651da177e4SLinus Torvalds } 24661da177e4SLinus Torvalds 24671da177e4SLinus Torvalds /* 24681da177e4SLinus Torvalds * Autoconfigure tunnel with a link-local address so routing protocols, 24691da177e4SLinus Torvalds * DHCPv6, MLD etc. can be run over the virtual link 24701da177e4SLinus Torvalds */ 24711da177e4SLinus Torvalds 24721da177e4SLinus Torvalds static void addrconf_ip6_tnl_config(struct net_device *dev) 24731da177e4SLinus Torvalds { 24741da177e4SLinus Torvalds struct inet6_dev *idev; 24751da177e4SLinus Torvalds 24761da177e4SLinus Torvalds ASSERT_RTNL(); 24771da177e4SLinus Torvalds 2478e21e8467SStephen Hemminger idev = addrconf_add_dev(dev); 247964e724f6SBrian Haley if (IS_ERR(idev)) { 24801da177e4SLinus Torvalds printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n"); 24811da177e4SLinus Torvalds return; 24821da177e4SLinus Torvalds } 24831da177e4SLinus Torvalds ip6_tnl_add_linklocal(idev); 24841da177e4SLinus Torvalds } 24851da177e4SLinus Torvalds 24861da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event, 24871da177e4SLinus Torvalds void * data) 24881da177e4SLinus Torvalds { 24891da177e4SLinus Torvalds struct net_device *dev = (struct net_device *) data; 249074235a25SHerbert Xu struct inet6_dev *idev = __in6_dev_get(dev); 2491c5e33bddSYOSHIFUJI Hideaki int run_pending = 0; 2492b217d616SHerbert Xu int err; 24931da177e4SLinus Torvalds 24941da177e4SLinus Torvalds switch (event) { 249545ba9dd2SYOSHIFUJI Hideaki case NETDEV_REGISTER: 249674235a25SHerbert Xu if (!idev && dev->mtu >= IPV6_MIN_MTU) { 249745ba9dd2SYOSHIFUJI Hideaki idev = ipv6_add_dev(dev); 249845ba9dd2SYOSHIFUJI Hideaki if (!idev) 2499b217d616SHerbert Xu return notifier_from_errno(-ENOMEM); 250045ba9dd2SYOSHIFUJI Hideaki } 250145ba9dd2SYOSHIFUJI Hideaki break; 2502bcdd553fSStephen Hemminger 25031da177e4SLinus Torvalds case NETDEV_UP: 25043c21edbdSYOSHIFUJI Hideaki case NETDEV_CHANGE: 2505c2edacf8SJay Vosburgh if (dev->flags & IFF_SLAVE) 2506c2edacf8SJay Vosburgh break; 2507c2edacf8SJay Vosburgh 25083c21edbdSYOSHIFUJI Hideaki if (event == NETDEV_UP) { 2509f24e3d65SMitsuru Chinen if (!addrconf_qdisc_ok(dev)) { 25103c21edbdSYOSHIFUJI Hideaki /* device is not ready yet. */ 25113c21edbdSYOSHIFUJI Hideaki printk(KERN_INFO 25123c21edbdSYOSHIFUJI Hideaki "ADDRCONF(NETDEV_UP): %s: " 25133c21edbdSYOSHIFUJI Hideaki "link is not ready\n", 25143c21edbdSYOSHIFUJI Hideaki dev->name); 25153c21edbdSYOSHIFUJI Hideaki break; 25163c21edbdSYOSHIFUJI Hideaki } 251799081049SKristian Slavov 2518d31c7b8fSEvgeniy Polyakov if (!idev && dev->mtu >= IPV6_MIN_MTU) 2519d31c7b8fSEvgeniy Polyakov idev = ipv6_add_dev(dev); 2520d31c7b8fSEvgeniy Polyakov 2521e3ec6cfcSBenjamin Thery if (idev) { 252299081049SKristian Slavov idev->if_flags |= IF_READY; 2523e3ec6cfcSBenjamin Thery run_pending = 1; 2524e3ec6cfcSBenjamin Thery } 25253c21edbdSYOSHIFUJI Hideaki } else { 2526f24e3d65SMitsuru Chinen if (!addrconf_qdisc_ok(dev)) { 25273c21edbdSYOSHIFUJI Hideaki /* device is still not ready. */ 25283c21edbdSYOSHIFUJI Hideaki break; 25293c21edbdSYOSHIFUJI Hideaki } 25303c21edbdSYOSHIFUJI Hideaki 25313c21edbdSYOSHIFUJI Hideaki if (idev) { 2532bcdd553fSStephen Hemminger if (idev->if_flags & IF_READY) 25333c21edbdSYOSHIFUJI Hideaki /* device is already configured. */ 25343c21edbdSYOSHIFUJI Hideaki break; 25353c21edbdSYOSHIFUJI Hideaki idev->if_flags |= IF_READY; 25363c21edbdSYOSHIFUJI Hideaki } 25373c21edbdSYOSHIFUJI Hideaki 25383c21edbdSYOSHIFUJI Hideaki printk(KERN_INFO 25393c21edbdSYOSHIFUJI Hideaki "ADDRCONF(NETDEV_CHANGE): %s: " 25403c21edbdSYOSHIFUJI Hideaki "link becomes ready\n", 25413c21edbdSYOSHIFUJI Hideaki dev->name); 25423c21edbdSYOSHIFUJI Hideaki 2543c5e33bddSYOSHIFUJI Hideaki run_pending = 1; 25443c21edbdSYOSHIFUJI Hideaki } 25453c21edbdSYOSHIFUJI Hideaki 25461da177e4SLinus Torvalds switch (dev->type) { 25470be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 25481da177e4SLinus Torvalds case ARPHRD_SIT: 25491da177e4SLinus Torvalds addrconf_sit_config(dev); 25501da177e4SLinus Torvalds break; 25510be669bbSJoerg Roedel #endif 25521da177e4SLinus Torvalds case ARPHRD_TUNNEL6: 25531da177e4SLinus Torvalds addrconf_ip6_tnl_config(dev); 25541da177e4SLinus Torvalds break; 25551da177e4SLinus Torvalds case ARPHRD_LOOPBACK: 25561da177e4SLinus Torvalds init_loopback(dev); 25571da177e4SLinus Torvalds break; 25581da177e4SLinus Torvalds 25591da177e4SLinus Torvalds default: 25601da177e4SLinus Torvalds addrconf_dev_config(dev); 25611da177e4SLinus Torvalds break; 25623ff50b79SStephen Hemminger } 2563bcdd553fSStephen Hemminger 25641da177e4SLinus Torvalds if (idev) { 2565c5e33bddSYOSHIFUJI Hideaki if (run_pending) 2566c5e33bddSYOSHIFUJI Hideaki addrconf_dad_run(idev); 2567c5e33bddSYOSHIFUJI Hideaki 2568bcdd553fSStephen Hemminger /* 2569bcdd553fSStephen Hemminger * If the MTU changed during the interface down, 2570bcdd553fSStephen Hemminger * when the interface up, the changed MTU must be 2571bcdd553fSStephen Hemminger * reflected in the idev as well as routers. 25721da177e4SLinus Torvalds */ 2573bcdd553fSStephen Hemminger if (idev->cnf.mtu6 != dev->mtu && 2574bcdd553fSStephen Hemminger dev->mtu >= IPV6_MIN_MTU) { 25751da177e4SLinus Torvalds rt6_mtu_change(dev, dev->mtu); 25761da177e4SLinus Torvalds idev->cnf.mtu6 = dev->mtu; 25771da177e4SLinus Torvalds } 25781da177e4SLinus Torvalds idev->tstamp = jiffies; 25791da177e4SLinus Torvalds inet6_ifinfo_notify(RTM_NEWLINK, idev); 2580bcdd553fSStephen Hemminger 2581bcdd553fSStephen Hemminger /* 2582bcdd553fSStephen Hemminger * If the changed mtu during down is lower than 2583bcdd553fSStephen Hemminger * IPV6_MIN_MTU stop IPv6 on this interface. 25841da177e4SLinus Torvalds */ 25851da177e4SLinus Torvalds if (dev->mtu < IPV6_MIN_MTU) 2586bcdd553fSStephen Hemminger addrconf_ifdown(dev, 1); 25871da177e4SLinus Torvalds } 25881da177e4SLinus Torvalds break; 25891da177e4SLinus Torvalds 25901da177e4SLinus Torvalds case NETDEV_CHANGEMTU: 25911da177e4SLinus Torvalds if (idev && dev->mtu >= IPV6_MIN_MTU) { 25921da177e4SLinus Torvalds rt6_mtu_change(dev, dev->mtu); 25931da177e4SLinus Torvalds idev->cnf.mtu6 = dev->mtu; 25941da177e4SLinus Torvalds break; 25951da177e4SLinus Torvalds } 25961da177e4SLinus Torvalds 2597d31c7b8fSEvgeniy Polyakov if (!idev && dev->mtu >= IPV6_MIN_MTU) { 2598d31c7b8fSEvgeniy Polyakov idev = ipv6_add_dev(dev); 2599d31c7b8fSEvgeniy Polyakov if (idev) 2600d31c7b8fSEvgeniy Polyakov break; 2601d31c7b8fSEvgeniy Polyakov } 2602d31c7b8fSEvgeniy Polyakov 2603bcdd553fSStephen Hemminger /* 2604bcdd553fSStephen Hemminger * MTU falled under IPV6_MIN_MTU. 2605bcdd553fSStephen Hemminger * Stop IPv6 on this interface. 2606bcdd553fSStephen Hemminger */ 26071da177e4SLinus Torvalds 26081da177e4SLinus Torvalds case NETDEV_DOWN: 26091da177e4SLinus Torvalds case NETDEV_UNREGISTER: 26101da177e4SLinus Torvalds /* 26111da177e4SLinus Torvalds * Remove all addresses from this interface. 26121da177e4SLinus Torvalds */ 26131da177e4SLinus Torvalds addrconf_ifdown(dev, event != NETDEV_DOWN); 26141da177e4SLinus Torvalds break; 26153c21edbdSYOSHIFUJI Hideaki 26161da177e4SLinus Torvalds case NETDEV_CHANGENAME: 26171da177e4SLinus Torvalds if (idev) { 26185632c515SStephen Hemminger snmp6_unregister_dev(idev); 2619408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 2620f52295a9SPavel Emelyanov addrconf_sysctl_register(idev); 2621b217d616SHerbert Xu err = snmp6_register_dev(idev); 2622b217d616SHerbert Xu if (err) 2623b217d616SHerbert Xu return notifier_from_errno(err); 26245632c515SStephen Hemminger } 26251da177e4SLinus Torvalds break; 2626bcdd553fSStephen Hemminger 262793d9b7d7SJiri Pirko case NETDEV_PRE_TYPE_CHANGE: 262893d9b7d7SJiri Pirko case NETDEV_POST_TYPE_CHANGE: 262993d9b7d7SJiri Pirko addrconf_type_change(dev, event); 263075c78500SMoni Shoua break; 26313ff50b79SStephen Hemminger } 26321da177e4SLinus Torvalds 26331da177e4SLinus Torvalds return NOTIFY_OK; 26341da177e4SLinus Torvalds } 26351da177e4SLinus Torvalds 26361da177e4SLinus Torvalds /* 26371da177e4SLinus Torvalds * addrconf module should be notified of a device going up 26381da177e4SLinus Torvalds */ 26391da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = { 26401da177e4SLinus Torvalds .notifier_call = addrconf_notify, 26411da177e4SLinus Torvalds }; 26421da177e4SLinus Torvalds 264393d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, unsigned long event) 264475c78500SMoni Shoua { 264575c78500SMoni Shoua struct inet6_dev *idev; 264675c78500SMoni Shoua ASSERT_RTNL(); 264775c78500SMoni Shoua 264875c78500SMoni Shoua idev = __in6_dev_get(dev); 264975c78500SMoni Shoua 265093d9b7d7SJiri Pirko if (event == NETDEV_POST_TYPE_CHANGE) 265175c78500SMoni Shoua ipv6_mc_remap(idev); 265293d9b7d7SJiri Pirko else if (event == NETDEV_PRE_TYPE_CHANGE) 265375c78500SMoni Shoua ipv6_mc_unmap(idev); 265475c78500SMoni Shoua } 265575c78500SMoni Shoua 26561da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how) 26571da177e4SLinus Torvalds { 2658c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 2659502a2ffdSstephen hemminger struct inet6_dev *idev; 2660502a2ffdSstephen hemminger struct inet6_ifaddr *ifa; 266173a8bd74SDavid S. Miller int state, i; 26621da177e4SLinus Torvalds 26631da177e4SLinus Torvalds ASSERT_RTNL(); 26641da177e4SLinus Torvalds 2665f3db4851SDaniel Lezcano rt6_ifdown(net, dev); 266673a8bd74SDavid S. Miller neigh_ifdown(&nd_tbl, dev); 26671da177e4SLinus Torvalds 26681da177e4SLinus Torvalds idev = __in6_dev_get(dev); 26691da177e4SLinus Torvalds if (idev == NULL) 26701da177e4SLinus Torvalds return -ENODEV; 26711da177e4SLinus Torvalds 2672bcdd553fSStephen Hemminger /* 2673bcdd553fSStephen Hemminger * Step 1: remove reference to ipv6 device from parent device. 2674bcdd553fSStephen Hemminger * Do not dev_put! 26751da177e4SLinus Torvalds */ 2676439e2385SDenis V. Lunev if (how) { 26771da177e4SLinus Torvalds idev->dead = 1; 26788814c4b5SYOSHIFUJI Hideaki 26798814c4b5SYOSHIFUJI Hideaki /* protected by rtnl_lock */ 26808814c4b5SYOSHIFUJI Hideaki rcu_assign_pointer(dev->ip6_ptr, NULL); 26811da177e4SLinus Torvalds 26821da177e4SLinus Torvalds /* Step 1.5: remove snmp6 entry */ 26831da177e4SLinus Torvalds snmp6_unregister_dev(idev); 26841da177e4SLinus Torvalds 26851da177e4SLinus Torvalds } 26861da177e4SLinus Torvalds 268773a8bd74SDavid S. Miller /* Step 2: clear hash table */ 268873a8bd74SDavid S. Miller for (i = 0; i < IN6_ADDR_HSIZE; i++) { 268973a8bd74SDavid S. Miller struct hlist_head *h = &inet6_addr_lst[i]; 269073a8bd74SDavid S. Miller struct hlist_node *n; 269173a8bd74SDavid S. Miller 269273a8bd74SDavid S. Miller spin_lock_bh(&addrconf_hash_lock); 269373a8bd74SDavid S. Miller restart: 269473a8bd74SDavid S. Miller hlist_for_each_entry_rcu(ifa, n, h, addr_lst) { 269573a8bd74SDavid S. Miller if (ifa->idev == idev) { 269673a8bd74SDavid S. Miller hlist_del_init_rcu(&ifa->addr_lst); 269773a8bd74SDavid S. Miller addrconf_del_timer(ifa); 269873a8bd74SDavid S. Miller goto restart; 269973a8bd74SDavid S. Miller } 270073a8bd74SDavid S. Miller } 270173a8bd74SDavid S. Miller spin_unlock_bh(&addrconf_hash_lock); 270273a8bd74SDavid S. Miller } 270373a8bd74SDavid S. Miller 27041da177e4SLinus Torvalds write_lock_bh(&idev->lock); 27051da177e4SLinus Torvalds 2706bcdd553fSStephen Hemminger /* Step 2: clear flags for stateless addrconf */ 2707439e2385SDenis V. Lunev if (!how) 27083c21edbdSYOSHIFUJI Hideaki idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY); 27091da177e4SLinus Torvalds 27101da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 2711439e2385SDenis V. Lunev if (how && del_timer(&idev->regen_timer)) 27121da177e4SLinus Torvalds in6_dev_put(idev); 27131da177e4SLinus Torvalds 2714bcdd553fSStephen Hemminger /* Step 3: clear tempaddr list */ 2715372e6c8fSstephen hemminger while (!list_empty(&idev->tempaddr_list)) { 2716372e6c8fSstephen hemminger ifa = list_first_entry(&idev->tempaddr_list, 2717372e6c8fSstephen hemminger struct inet6_ifaddr, tmp_list); 2718372e6c8fSstephen hemminger list_del(&ifa->tmp_list); 27191da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 27201da177e4SLinus Torvalds spin_lock_bh(&ifa->lock); 27211da177e4SLinus Torvalds 27221da177e4SLinus Torvalds if (ifa->ifpub) { 27231da177e4SLinus Torvalds in6_ifa_put(ifa->ifpub); 27241da177e4SLinus Torvalds ifa->ifpub = NULL; 27251da177e4SLinus Torvalds } 27261da177e4SLinus Torvalds spin_unlock_bh(&ifa->lock); 27271da177e4SLinus Torvalds in6_ifa_put(ifa); 27281da177e4SLinus Torvalds write_lock_bh(&idev->lock); 27291da177e4SLinus Torvalds } 27301da177e4SLinus Torvalds #endif 27318f37ada5Sstephen hemminger 2732502a2ffdSstephen hemminger while (!list_empty(&idev->addr_list)) { 2733502a2ffdSstephen hemminger ifa = list_first_entry(&idev->addr_list, 2734502a2ffdSstephen hemminger struct inet6_ifaddr, if_list); 273584e8b803Sstephen hemminger addrconf_del_timer(ifa); 273684e8b803Sstephen hemminger 2737502a2ffdSstephen hemminger list_del(&ifa->if_list); 27381da177e4SLinus Torvalds 27394c5ff6a6SHerbert Xu write_unlock_bh(&idev->lock); 274073a8bd74SDavid S. Miller 27414c5ff6a6SHerbert Xu spin_lock_bh(&ifa->state_lock); 27424c5ff6a6SHerbert Xu state = ifa->state; 27434c5ff6a6SHerbert Xu ifa->state = INET6_IFADDR_STATE_DEAD; 27444c5ff6a6SHerbert Xu spin_unlock_bh(&ifa->state_lock); 27454c5ff6a6SHerbert Xu 27469d82ca98SJohn Fastabend if (state != INET6_IFADDR_STATE_DEAD) { 27471da177e4SLinus Torvalds __ipv6_ifa_notify(RTM_DELADDR, ifa); 274873a8bd74SDavid S. Miller atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifa); 27492de79570SLorenzo Colitti } 27509d82ca98SJohn Fastabend in6_ifa_put(ifa); 275173a8bd74SDavid S. Miller 27521da177e4SLinus Torvalds write_lock_bh(&idev->lock); 27531da177e4SLinus Torvalds } 27548f37ada5Sstephen hemminger 27551da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 27561da177e4SLinus Torvalds 27571da177e4SLinus Torvalds /* Step 5: Discard multicast list */ 2758439e2385SDenis V. Lunev if (how) 27591da177e4SLinus Torvalds ipv6_mc_destroy_dev(idev); 27601da177e4SLinus Torvalds else 27611da177e4SLinus Torvalds ipv6_mc_down(idev); 27621da177e4SLinus Torvalds 27631da177e4SLinus Torvalds idev->tstamp = jiffies; 27641da177e4SLinus Torvalds 2765bcdd553fSStephen Hemminger /* Last: Shot the device (if unregistered) */ 2766439e2385SDenis V. Lunev if (how) { 2767408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 27681da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, idev->nd_parms); 27691da177e4SLinus Torvalds neigh_ifdown(&nd_tbl, dev); 27701da177e4SLinus Torvalds in6_dev_put(idev); 27711da177e4SLinus Torvalds } 27721da177e4SLinus Torvalds return 0; 27731da177e4SLinus Torvalds } 27741da177e4SLinus Torvalds 27751da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data) 27761da177e4SLinus Torvalds { 27771da177e4SLinus Torvalds struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data; 27785b2a1953Sstephen hemminger struct inet6_dev *idev = ifp->idev; 27791da177e4SLinus Torvalds 27805b2a1953Sstephen hemminger read_lock(&idev->lock); 27815b2a1953Sstephen hemminger if (idev->dead || !(idev->if_flags & IF_READY)) 27821da177e4SLinus Torvalds goto out; 27831da177e4SLinus Torvalds 27845b2a1953Sstephen hemminger if (idev->cnf.forwarding) 27851da177e4SLinus Torvalds goto out; 27865b2a1953Sstephen hemminger 27875b2a1953Sstephen hemminger /* Announcement received after solicitation was sent */ 27885b2a1953Sstephen hemminger if (idev->if_flags & IF_RA_RCVD) 27895b2a1953Sstephen hemminger goto out; 27901da177e4SLinus Torvalds 27911da177e4SLinus Torvalds spin_lock(&ifp->lock); 27925b2a1953Sstephen hemminger if (ifp->probes++ < idev->cnf.rtr_solicits) { 27931da177e4SLinus Torvalds /* The wait after the last probe can be shorter */ 27941da177e4SLinus Torvalds addrconf_mod_timer(ifp, AC_RS, 27955b2a1953Sstephen hemminger (ifp->probes == idev->cnf.rtr_solicits) ? 27965b2a1953Sstephen hemminger idev->cnf.rtr_solicit_delay : 27975b2a1953Sstephen hemminger idev->cnf.rtr_solicit_interval); 27981da177e4SLinus Torvalds spin_unlock(&ifp->lock); 27991da177e4SLinus Torvalds 28005b2a1953Sstephen hemminger ndisc_send_rs(idev->dev, &ifp->addr, &in6addr_linklocal_allrouters); 28011da177e4SLinus Torvalds } else { 28021da177e4SLinus Torvalds spin_unlock(&ifp->lock); 28031da177e4SLinus Torvalds /* 28041da177e4SLinus Torvalds * Note: we do not support deprecated "all on-link" 28051da177e4SLinus Torvalds * assumption any longer. 28061da177e4SLinus Torvalds */ 28071da177e4SLinus Torvalds printk(KERN_DEBUG "%s: no IPv6 routers present\n", 28085b2a1953Sstephen hemminger idev->dev->name); 28091da177e4SLinus Torvalds } 28101da177e4SLinus Torvalds 28111da177e4SLinus Torvalds out: 28125b2a1953Sstephen hemminger read_unlock(&idev->lock); 28131da177e4SLinus Torvalds in6_ifa_put(ifp); 28141da177e4SLinus Torvalds } 28151da177e4SLinus Torvalds 28161da177e4SLinus Torvalds /* 28171da177e4SLinus Torvalds * Duplicate Address Detection 28181da177e4SLinus Torvalds */ 28193c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp) 28203c21edbdSYOSHIFUJI Hideaki { 28213c21edbdSYOSHIFUJI Hideaki unsigned long rand_num; 28223c21edbdSYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev; 28233c21edbdSYOSHIFUJI Hideaki 282495c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 282595c385b4SNeil Horman rand_num = 0; 282695c385b4SNeil Horman else 28273c21edbdSYOSHIFUJI Hideaki rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1); 282895c385b4SNeil Horman 28293c21edbdSYOSHIFUJI Hideaki ifp->probes = idev->cnf.dad_transmits; 28303c21edbdSYOSHIFUJI Hideaki addrconf_mod_timer(ifp, AC_DAD, rand_num); 28313c21edbdSYOSHIFUJI Hideaki } 28323c21edbdSYOSHIFUJI Hideaki 2833e431b8c0SJamal Hadi Salim static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags) 28341da177e4SLinus Torvalds { 28351da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 28361da177e4SLinus Torvalds struct net_device *dev = idev->dev; 28371da177e4SLinus Torvalds 28381da177e4SLinus Torvalds addrconf_join_solict(dev, &ifp->addr); 28391da177e4SLinus Torvalds 28401da177e4SLinus Torvalds net_srandom(ifp->addr.s6_addr32[3]); 28411da177e4SLinus Torvalds 28421da177e4SLinus Torvalds read_lock_bh(&idev->lock); 2843622ccdf1SHerbert Xu spin_lock(&ifp->lock); 2844e9d3e084SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DEAD) 28451da177e4SLinus Torvalds goto out; 28461da177e4SLinus Torvalds 28471da177e4SLinus Torvalds if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) || 28481b34be74SYOSHIFUJI Hideaki idev->cnf.accept_dad < 1 || 284955ebaef1SNoriaki TAKAMIYA !(ifp->flags&IFA_F_TENTATIVE) || 285055ebaef1SNoriaki TAKAMIYA ifp->flags & IFA_F_NODAD) { 2851cc411d0bSBrian Haley ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 285221809fafSstephen hemminger spin_unlock(&ifp->lock); 28531da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 28541da177e4SLinus Torvalds 28551da177e4SLinus Torvalds addrconf_dad_completed(ifp); 28561da177e4SLinus Torvalds return; 28571da177e4SLinus Torvalds } 28581da177e4SLinus Torvalds 28596732badeSYOSHIFUJI Hideaki if (!(idev->if_flags & IF_READY)) { 286021809fafSstephen hemminger spin_unlock(&ifp->lock); 28616732badeSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 28623c21edbdSYOSHIFUJI Hideaki /* 2863590a9887SMasatake YAMATO * If the device is not ready: 28643c21edbdSYOSHIFUJI Hideaki * - keep it tentative if it is a permanent address. 28653c21edbdSYOSHIFUJI Hideaki * - otherwise, kill it. 28663c21edbdSYOSHIFUJI Hideaki */ 28673c21edbdSYOSHIFUJI Hideaki in6_ifa_hold(ifp); 2868cc411d0bSBrian Haley addrconf_dad_stop(ifp, 0); 28696732badeSYOSHIFUJI Hideaki return; 28703c21edbdSYOSHIFUJI Hideaki } 287195c385b4SNeil Horman 287295c385b4SNeil Horman /* 287395c385b4SNeil Horman * Optimistic nodes can start receiving 287495c385b4SNeil Horman * Frames right away 287595c385b4SNeil Horman */ 287695c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 287795c385b4SNeil Horman ip6_ins_rt(ifp->rt); 287895c385b4SNeil Horman 28796732badeSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 28801da177e4SLinus Torvalds out: 2881622ccdf1SHerbert Xu spin_unlock(&ifp->lock); 28821da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 28831da177e4SLinus Torvalds } 28841da177e4SLinus Torvalds 28851da177e4SLinus Torvalds static void addrconf_dad_timer(unsigned long data) 28861da177e4SLinus Torvalds { 28871da177e4SLinus Torvalds struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data; 28881da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 28891da177e4SLinus Torvalds struct in6_addr mcaddr; 28901da177e4SLinus Torvalds 2891f2344a13SHerbert Xu if (!ifp->probes && addrconf_dad_end(ifp)) 2892f2344a13SHerbert Xu goto out; 2893f2344a13SHerbert Xu 2894122e4519Sstephen hemminger read_lock(&idev->lock); 2895122e4519Sstephen hemminger if (idev->dead || !(idev->if_flags & IF_READY)) { 2896122e4519Sstephen hemminger read_unlock(&idev->lock); 28971da177e4SLinus Torvalds goto out; 28981da177e4SLinus Torvalds } 289921809fafSstephen hemminger 290021809fafSstephen hemminger spin_lock(&ifp->lock); 2901622ccdf1SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DEAD) { 2902622ccdf1SHerbert Xu spin_unlock(&ifp->lock); 2903622ccdf1SHerbert Xu read_unlock(&idev->lock); 2904622ccdf1SHerbert Xu goto out; 2905622ccdf1SHerbert Xu } 2906622ccdf1SHerbert Xu 29071da177e4SLinus Torvalds if (ifp->probes == 0) { 29081da177e4SLinus Torvalds /* 29091da177e4SLinus Torvalds * DAD was successful 29101da177e4SLinus Torvalds */ 29111da177e4SLinus Torvalds 2912cc411d0bSBrian Haley ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 291321809fafSstephen hemminger spin_unlock(&ifp->lock); 2914122e4519Sstephen hemminger read_unlock(&idev->lock); 29151da177e4SLinus Torvalds 29161da177e4SLinus Torvalds addrconf_dad_completed(ifp); 29171da177e4SLinus Torvalds 29181da177e4SLinus Torvalds goto out; 29191da177e4SLinus Torvalds } 29201da177e4SLinus Torvalds 29211da177e4SLinus Torvalds ifp->probes--; 29221da177e4SLinus Torvalds addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time); 292321809fafSstephen hemminger spin_unlock(&ifp->lock); 2924122e4519Sstephen hemminger read_unlock(&idev->lock); 29251da177e4SLinus Torvalds 29261da177e4SLinus Torvalds /* send a neighbour solicitation for our addr */ 29271da177e4SLinus Torvalds addrconf_addr_solict_mult(&ifp->addr, &mcaddr); 2928d7aabf22SYOSHIFUJI Hideaki ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any); 29291da177e4SLinus Torvalds out: 29301da177e4SLinus Torvalds in6_ifa_put(ifp); 29311da177e4SLinus Torvalds } 29321da177e4SLinus Torvalds 29331da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp) 29341da177e4SLinus Torvalds { 29351da177e4SLinus Torvalds struct net_device *dev = ifp->idev->dev; 29361da177e4SLinus Torvalds 29371da177e4SLinus Torvalds /* 29381da177e4SLinus Torvalds * Configure the address for reception. Now it is valid. 29391da177e4SLinus Torvalds */ 29401da177e4SLinus Torvalds 29411da177e4SLinus Torvalds ipv6_ifa_notify(RTM_NEWADDR, ifp); 29421da177e4SLinus Torvalds 29431da177e4SLinus Torvalds /* If added prefix is link local and forwarding is off, 29441da177e4SLinus Torvalds start sending router solicitations. 29451da177e4SLinus Torvalds */ 29461da177e4SLinus Torvalds 2947c3bccac2SThomas Graf if ((ifp->idev->cnf.forwarding == 0 || 2948c3bccac2SThomas Graf ifp->idev->cnf.forwarding == 2) && 29491da177e4SLinus Torvalds ifp->idev->cnf.rtr_solicits > 0 && 29501da177e4SLinus Torvalds (dev->flags&IFF_LOOPBACK) == 0 && 29511da177e4SLinus Torvalds (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) { 29521da177e4SLinus Torvalds /* 29531da177e4SLinus Torvalds * If a host as already performed a random delay 29541da177e4SLinus Torvalds * [...] as part of DAD [...] there is no need 29551da177e4SLinus Torvalds * to delay again before sending the first RS 29561da177e4SLinus Torvalds */ 2957f3ee4010SYOSHIFUJI Hideaki ndisc_send_rs(ifp->idev->dev, &ifp->addr, &in6addr_linklocal_allrouters); 29581da177e4SLinus Torvalds 29591da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 29601da177e4SLinus Torvalds ifp->probes = 1; 29611da177e4SLinus Torvalds ifp->idev->if_flags |= IF_RS_SENT; 29621da177e4SLinus Torvalds addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval); 29631da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 29641da177e4SLinus Torvalds } 29651da177e4SLinus Torvalds } 29661da177e4SLinus Torvalds 2967e21e8467SStephen Hemminger static void addrconf_dad_run(struct inet6_dev *idev) 2968e21e8467SStephen Hemminger { 2969c5e33bddSYOSHIFUJI Hideaki struct inet6_ifaddr *ifp; 2970c5e33bddSYOSHIFUJI Hideaki 2971c5e33bddSYOSHIFUJI Hideaki read_lock_bh(&idev->lock); 2972502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 297321809fafSstephen hemminger spin_lock(&ifp->lock); 2974f2344a13SHerbert Xu if (ifp->flags & IFA_F_TENTATIVE && 2975f2344a13SHerbert Xu ifp->state == INET6_IFADDR_STATE_DAD) 2976c5e33bddSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 2977f2344a13SHerbert Xu spin_unlock(&ifp->lock); 2978c5e33bddSYOSHIFUJI Hideaki } 2979c5e33bddSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 2980c5e33bddSYOSHIFUJI Hideaki } 2981c5e33bddSYOSHIFUJI Hideaki 29821da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 29831da177e4SLinus Torvalds struct if6_iter_state { 29843c40090aSDaniel Lezcano struct seq_net_private p; 29851da177e4SLinus Torvalds int bucket; 29861da177e4SLinus Torvalds }; 29871da177e4SLinus Torvalds 29881da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_first(struct seq_file *seq) 29891da177e4SLinus Torvalds { 29901da177e4SLinus Torvalds struct inet6_ifaddr *ifa = NULL; 29911da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 29921218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 29931da177e4SLinus Torvalds 29941da177e4SLinus Torvalds for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) { 2995c2e21293Sstephen hemminger struct hlist_node *n; 29964f70eccaSEric Dumazet hlist_for_each_entry_rcu_bh(ifa, n, &inet6_addr_lst[state->bucket], 29975c578aedSstephen hemminger addr_lst) 2998c2e21293Sstephen hemminger if (net_eq(dev_net(ifa->idev->dev), net)) 29991da177e4SLinus Torvalds return ifa; 30001da177e4SLinus Torvalds } 3001c2e21293Sstephen hemminger return NULL; 3002c2e21293Sstephen hemminger } 30031da177e4SLinus Torvalds 3004c2e21293Sstephen hemminger static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, 3005c2e21293Sstephen hemminger struct inet6_ifaddr *ifa) 30061da177e4SLinus Torvalds { 30071da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 30081218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 3009c2e21293Sstephen hemminger struct hlist_node *n = &ifa->addr_lst; 30101da177e4SLinus Torvalds 30114f70eccaSEric Dumazet hlist_for_each_entry_continue_rcu_bh(ifa, n, addr_lst) 3012c2e21293Sstephen hemminger if (net_eq(dev_net(ifa->idev->dev), net)) 30131da177e4SLinus Torvalds return ifa; 30141da177e4SLinus Torvalds 3015c2e21293Sstephen hemminger while (++state->bucket < IN6_ADDR_HSIZE) { 30164f70eccaSEric Dumazet hlist_for_each_entry_rcu_bh(ifa, n, 3017c2e21293Sstephen hemminger &inet6_addr_lst[state->bucket], addr_lst) { 3018c2e21293Sstephen hemminger if (net_eq(dev_net(ifa->idev->dev), net)) 3019c2e21293Sstephen hemminger return ifa; 3020c2e21293Sstephen hemminger } 3021c2e21293Sstephen hemminger } 3022c2e21293Sstephen hemminger 3023c2e21293Sstephen hemminger return NULL; 3024c2e21293Sstephen hemminger } 3025c2e21293Sstephen hemminger 30261da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos) 30271da177e4SLinus Torvalds { 30281da177e4SLinus Torvalds struct inet6_ifaddr *ifa = if6_get_first(seq); 30291da177e4SLinus Torvalds 30301da177e4SLinus Torvalds if (ifa) 30311da177e4SLinus Torvalds while (pos && (ifa = if6_get_next(seq, ifa)) != NULL) 30321da177e4SLinus Torvalds --pos; 30331da177e4SLinus Torvalds return pos ? NULL : ifa; 30341da177e4SLinus Torvalds } 30351da177e4SLinus Torvalds 30361da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos) 30374f70eccaSEric Dumazet __acquires(rcu_bh) 30381da177e4SLinus Torvalds { 30395c578aedSstephen hemminger rcu_read_lock_bh(); 30401da177e4SLinus Torvalds return if6_get_idx(seq, *pos); 30411da177e4SLinus Torvalds } 30421da177e4SLinus Torvalds 30431da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos) 30441da177e4SLinus Torvalds { 30451da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 30461da177e4SLinus Torvalds 30471da177e4SLinus Torvalds ifa = if6_get_next(seq, v); 30481da177e4SLinus Torvalds ++*pos; 30491da177e4SLinus Torvalds return ifa; 30501da177e4SLinus Torvalds } 30511da177e4SLinus Torvalds 30521da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v) 30534f70eccaSEric Dumazet __releases(rcu_bh) 30541da177e4SLinus Torvalds { 30555c578aedSstephen hemminger rcu_read_unlock_bh(); 30561da177e4SLinus Torvalds } 30571da177e4SLinus Torvalds 30581da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v) 30591da177e4SLinus Torvalds { 30601da177e4SLinus Torvalds struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v; 30614b7a4274SHarvey Harrison seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n", 3062b071195dSHarvey Harrison &ifp->addr, 30631da177e4SLinus Torvalds ifp->idev->dev->ifindex, 30641da177e4SLinus Torvalds ifp->prefix_len, 30651da177e4SLinus Torvalds ifp->scope, 30661da177e4SLinus Torvalds ifp->flags, 30671da177e4SLinus Torvalds ifp->idev->dev->name); 30681da177e4SLinus Torvalds return 0; 30691da177e4SLinus Torvalds } 30701da177e4SLinus Torvalds 307156b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = { 30721da177e4SLinus Torvalds .start = if6_seq_start, 30731da177e4SLinus Torvalds .next = if6_seq_next, 30741da177e4SLinus Torvalds .show = if6_seq_show, 30751da177e4SLinus Torvalds .stop = if6_seq_stop, 30761da177e4SLinus Torvalds }; 30771da177e4SLinus Torvalds 30781da177e4SLinus Torvalds static int if6_seq_open(struct inode *inode, struct file *file) 30791da177e4SLinus Torvalds { 30803c40090aSDaniel Lezcano return seq_open_net(inode, file, &if6_seq_ops, 3081cf7732e4SPavel Emelyanov sizeof(struct if6_iter_state)); 30821da177e4SLinus Torvalds } 30831da177e4SLinus Torvalds 30849a32144eSArjan van de Ven static const struct file_operations if6_fops = { 30851da177e4SLinus Torvalds .owner = THIS_MODULE, 30861da177e4SLinus Torvalds .open = if6_seq_open, 30871da177e4SLinus Torvalds .read = seq_read, 30881da177e4SLinus Torvalds .llseek = seq_lseek, 30893c40090aSDaniel Lezcano .release = seq_release_net, 30901da177e4SLinus Torvalds }; 30911da177e4SLinus Torvalds 30922c8c1e72SAlexey Dobriyan static int __net_init if6_proc_net_init(struct net *net) 30931da177e4SLinus Torvalds { 30943c40090aSDaniel Lezcano if (!proc_net_fops_create(net, "if_inet6", S_IRUGO, &if6_fops)) 30951da177e4SLinus Torvalds return -ENOMEM; 30961da177e4SLinus Torvalds return 0; 30971da177e4SLinus Torvalds } 30981da177e4SLinus Torvalds 30992c8c1e72SAlexey Dobriyan static void __net_exit if6_proc_net_exit(struct net *net) 31003c40090aSDaniel Lezcano { 31013c40090aSDaniel Lezcano proc_net_remove(net, "if_inet6"); 31023c40090aSDaniel Lezcano } 31033c40090aSDaniel Lezcano 31043c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = { 31053c40090aSDaniel Lezcano .init = if6_proc_net_init, 31063c40090aSDaniel Lezcano .exit = if6_proc_net_exit, 31073c40090aSDaniel Lezcano }; 31083c40090aSDaniel Lezcano 31093c40090aSDaniel Lezcano int __init if6_proc_init(void) 31103c40090aSDaniel Lezcano { 31113c40090aSDaniel Lezcano return register_pernet_subsys(&if6_proc_net_ops); 31123c40090aSDaniel Lezcano } 31133c40090aSDaniel Lezcano 31141da177e4SLinus Torvalds void if6_proc_exit(void) 31151da177e4SLinus Torvalds { 31163c40090aSDaniel Lezcano unregister_pernet_subsys(&if6_proc_net_ops); 31171da177e4SLinus Torvalds } 31181da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 31191da177e4SLinus Torvalds 312059fbb3a6SMasahide NAKAMURA #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) 31213b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */ 3122389f6612SDaniel Lezcano int ipv6_chk_home_addr(struct net *net, struct in6_addr *addr) 31233b9f9a1cSNoriaki TAKAMIYA { 31243b9f9a1cSNoriaki TAKAMIYA int ret = 0; 3125c2e21293Sstephen hemminger struct inet6_ifaddr *ifp = NULL; 3126c2e21293Sstephen hemminger struct hlist_node *n; 31273a88a81dSstephen hemminger unsigned int hash = ipv6_addr_hash(addr); 3128c2e21293Sstephen hemminger 31295c578aedSstephen hemminger rcu_read_lock_bh(); 31304f70eccaSEric Dumazet hlist_for_each_entry_rcu_bh(ifp, n, &inet6_addr_lst[hash], addr_lst) { 3131878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 3132389f6612SDaniel Lezcano continue; 3133caad295fSYOSHIFUJI Hideaki if (ipv6_addr_equal(&ifp->addr, addr) && 31343b9f9a1cSNoriaki TAKAMIYA (ifp->flags & IFA_F_HOMEADDRESS)) { 31353b9f9a1cSNoriaki TAKAMIYA ret = 1; 31363b9f9a1cSNoriaki TAKAMIYA break; 31373b9f9a1cSNoriaki TAKAMIYA } 31383b9f9a1cSNoriaki TAKAMIYA } 31395c578aedSstephen hemminger rcu_read_unlock_bh(); 31403b9f9a1cSNoriaki TAKAMIYA return ret; 31413b9f9a1cSNoriaki TAKAMIYA } 31423b9f9a1cSNoriaki TAKAMIYA #endif 31433b9f9a1cSNoriaki TAKAMIYA 31441da177e4SLinus Torvalds /* 31451da177e4SLinus Torvalds * Periodic address status verification 31461da177e4SLinus Torvalds */ 31471da177e4SLinus Torvalds 31481da177e4SLinus Torvalds static void addrconf_verify(unsigned long foo) 31491da177e4SLinus Torvalds { 3150b2db7564SYOSHIFUJI Hideaki unsigned long now, next, next_sec, next_sched; 31511da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 3152c2e21293Sstephen hemminger struct hlist_node *node; 31531da177e4SLinus Torvalds int i; 31541da177e4SLinus Torvalds 31555c578aedSstephen hemminger rcu_read_lock_bh(); 31565c578aedSstephen hemminger spin_lock(&addrconf_verify_lock); 31571da177e4SLinus Torvalds now = jiffies; 3158b2db7564SYOSHIFUJI Hideaki next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY); 31591da177e4SLinus Torvalds 31601da177e4SLinus Torvalds del_timer(&addr_chk_timer); 31611da177e4SLinus Torvalds 31621da177e4SLinus Torvalds for (i = 0; i < IN6_ADDR_HSIZE; i++) { 31631da177e4SLinus Torvalds restart: 31644f70eccaSEric Dumazet hlist_for_each_entry_rcu_bh(ifp, node, 31655c578aedSstephen hemminger &inet6_addr_lst[i], addr_lst) { 31661da177e4SLinus Torvalds unsigned long age; 31671da177e4SLinus Torvalds 31681da177e4SLinus Torvalds if (ifp->flags & IFA_F_PERMANENT) 31691da177e4SLinus Torvalds continue; 31701da177e4SLinus Torvalds 31711da177e4SLinus Torvalds spin_lock(&ifp->lock); 3172b2db7564SYOSHIFUJI Hideaki /* We try to batch several events at once. */ 3173b2db7564SYOSHIFUJI Hideaki age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; 31741da177e4SLinus Torvalds 31758f27ebb9SYOSHIFUJI Hideaki if (ifp->valid_lft != INFINITY_LIFE_TIME && 31768f27ebb9SYOSHIFUJI Hideaki age >= ifp->valid_lft) { 31771da177e4SLinus Torvalds spin_unlock(&ifp->lock); 31781da177e4SLinus Torvalds in6_ifa_hold(ifp); 31791da177e4SLinus Torvalds ipv6_del_addr(ifp); 31801da177e4SLinus Torvalds goto restart; 31818f27ebb9SYOSHIFUJI Hideaki } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) { 31828f27ebb9SYOSHIFUJI Hideaki spin_unlock(&ifp->lock); 31838f27ebb9SYOSHIFUJI Hideaki continue; 31841da177e4SLinus Torvalds } else if (age >= ifp->prefered_lft) { 3185a1ed0526SBrian Haley /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */ 31861da177e4SLinus Torvalds int deprecate = 0; 31871da177e4SLinus Torvalds 31881da177e4SLinus Torvalds if (!(ifp->flags&IFA_F_DEPRECATED)) { 31891da177e4SLinus Torvalds deprecate = 1; 31901da177e4SLinus Torvalds ifp->flags |= IFA_F_DEPRECATED; 31911da177e4SLinus Torvalds } 31921da177e4SLinus Torvalds 31931da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)) 31941da177e4SLinus Torvalds next = ifp->tstamp + ifp->valid_lft * HZ; 31951da177e4SLinus Torvalds 31961da177e4SLinus Torvalds spin_unlock(&ifp->lock); 31971da177e4SLinus Torvalds 31981da177e4SLinus Torvalds if (deprecate) { 31991da177e4SLinus Torvalds in6_ifa_hold(ifp); 32001da177e4SLinus Torvalds 32011da177e4SLinus Torvalds ipv6_ifa_notify(0, ifp); 32021da177e4SLinus Torvalds in6_ifa_put(ifp); 32031da177e4SLinus Torvalds goto restart; 32041da177e4SLinus Torvalds } 32051da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 32061da177e4SLinus Torvalds } else if ((ifp->flags&IFA_F_TEMPORARY) && 32071da177e4SLinus Torvalds !(ifp->flags&IFA_F_TENTATIVE)) { 320888949cf4Sstephen hemminger unsigned long regen_advance = ifp->idev->cnf.regen_max_retry * 320988949cf4Sstephen hemminger ifp->idev->cnf.dad_transmits * 321088949cf4Sstephen hemminger ifp->idev->nd_parms->retrans_time / HZ; 321188949cf4Sstephen hemminger 32121da177e4SLinus Torvalds if (age >= ifp->prefered_lft - regen_advance) { 32131da177e4SLinus Torvalds struct inet6_ifaddr *ifpub = ifp->ifpub; 32141da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 32151da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 32161da177e4SLinus Torvalds if (!ifp->regen_count && ifpub) { 32171da177e4SLinus Torvalds ifp->regen_count++; 32181da177e4SLinus Torvalds in6_ifa_hold(ifp); 32191da177e4SLinus Torvalds in6_ifa_hold(ifpub); 32201da177e4SLinus Torvalds spin_unlock(&ifp->lock); 32215c578aedSstephen hemminger 3222291d809bSHiroyuki YAMAMORI spin_lock(&ifpub->lock); 3223291d809bSHiroyuki YAMAMORI ifpub->regen_count = 0; 3224291d809bSHiroyuki YAMAMORI spin_unlock(&ifpub->lock); 32251da177e4SLinus Torvalds ipv6_create_tempaddr(ifpub, ifp); 32261da177e4SLinus Torvalds in6_ifa_put(ifpub); 32271da177e4SLinus Torvalds in6_ifa_put(ifp); 32281da177e4SLinus Torvalds goto restart; 32291da177e4SLinus Torvalds } 32301da177e4SLinus Torvalds } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next)) 32311da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ; 32321da177e4SLinus Torvalds spin_unlock(&ifp->lock); 32331da177e4SLinus Torvalds #endif 32341da177e4SLinus Torvalds } else { 32351da177e4SLinus Torvalds /* ifp->prefered_lft <= ifp->valid_lft */ 32361da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 32371da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 32381da177e4SLinus Torvalds spin_unlock(&ifp->lock); 32391da177e4SLinus Torvalds } 32401da177e4SLinus Torvalds } 32411da177e4SLinus Torvalds } 32421da177e4SLinus Torvalds 3243b2db7564SYOSHIFUJI Hideaki next_sec = round_jiffies_up(next); 3244b2db7564SYOSHIFUJI Hideaki next_sched = next; 3245b2db7564SYOSHIFUJI Hideaki 3246b2db7564SYOSHIFUJI Hideaki /* If rounded timeout is accurate enough, accept it. */ 3247b2db7564SYOSHIFUJI Hideaki if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ)) 3248b2db7564SYOSHIFUJI Hideaki next_sched = next_sec; 3249b2db7564SYOSHIFUJI Hideaki 3250b2db7564SYOSHIFUJI Hideaki /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */ 3251b2db7564SYOSHIFUJI Hideaki if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX)) 3252b2db7564SYOSHIFUJI Hideaki next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX; 3253b2db7564SYOSHIFUJI Hideaki 3254b2db7564SYOSHIFUJI Hideaki ADBG((KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n", 3255b2db7564SYOSHIFUJI Hideaki now, next, next_sec, next_sched)); 3256b2db7564SYOSHIFUJI Hideaki 3257b2db7564SYOSHIFUJI Hideaki addr_chk_timer.expires = next_sched; 32581da177e4SLinus Torvalds add_timer(&addr_chk_timer); 32595c578aedSstephen hemminger spin_unlock(&addrconf_verify_lock); 32605c578aedSstephen hemminger rcu_read_unlock_bh(); 32611da177e4SLinus Torvalds } 32621da177e4SLinus Torvalds 3263461d8837SThomas Graf static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local) 3264461d8837SThomas Graf { 3265461d8837SThomas Graf struct in6_addr *pfx = NULL; 3266461d8837SThomas Graf 3267461d8837SThomas Graf if (addr) 3268461d8837SThomas Graf pfx = nla_data(addr); 3269461d8837SThomas Graf 3270461d8837SThomas Graf if (local) { 3271461d8837SThomas Graf if (pfx && nla_memcmp(local, pfx, sizeof(*pfx))) 3272461d8837SThomas Graf pfx = NULL; 3273461d8837SThomas Graf else 3274461d8837SThomas Graf pfx = nla_data(local); 3275461d8837SThomas Graf } 3276461d8837SThomas Graf 3277461d8837SThomas Graf return pfx; 3278461d8837SThomas Graf } 3279461d8837SThomas Graf 3280ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = { 3281461d8837SThomas Graf [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) }, 3282461d8837SThomas Graf [IFA_LOCAL] = { .len = sizeof(struct in6_addr) }, 3283461d8837SThomas Graf [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) }, 3284461d8837SThomas Graf }; 3285461d8837SThomas Graf 32861da177e4SLinus Torvalds static int 32871da177e4SLinus Torvalds inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 32881da177e4SLinus Torvalds { 32893b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 3290b933f716SThomas Graf struct ifaddrmsg *ifm; 3291b933f716SThomas Graf struct nlattr *tb[IFA_MAX+1]; 32921da177e4SLinus Torvalds struct in6_addr *pfx; 3293b933f716SThomas Graf int err; 32941da177e4SLinus Torvalds 3295b933f716SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 3296b933f716SThomas Graf if (err < 0) 3297b933f716SThomas Graf return err; 3298b933f716SThomas Graf 3299b933f716SThomas Graf ifm = nlmsg_data(nlh); 3300b933f716SThomas Graf pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]); 33011da177e4SLinus Torvalds if (pfx == NULL) 33021da177e4SLinus Torvalds return -EINVAL; 33031da177e4SLinus Torvalds 3304af284937SDaniel Lezcano return inet6_addr_del(net, ifm->ifa_index, pfx, ifm->ifa_prefixlen); 33051da177e4SLinus Torvalds } 33061da177e4SLinus Torvalds 330755ebaef1SNoriaki TAKAMIYA static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags, 330855ebaef1SNoriaki TAKAMIYA u32 prefered_lft, u32 valid_lft) 3309081bba5bSNoriaki TAKAMIYA { 33106f704992SYOSHIFUJI Hideaki u32 flags; 33116f704992SYOSHIFUJI Hideaki clock_t expires; 33124bed72e4SYOSHIFUJI Hideaki unsigned long timeout; 331346d48046SYOSHIFUJI Hideaki 3314081bba5bSNoriaki TAKAMIYA if (!valid_lft || (prefered_lft > valid_lft)) 3315081bba5bSNoriaki TAKAMIYA return -EINVAL; 3316081bba5bSNoriaki TAKAMIYA 33174bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(valid_lft, HZ); 33184bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 33194bed72e4SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(timeout * HZ); 33204bed72e4SYOSHIFUJI Hideaki valid_lft = timeout; 33216f704992SYOSHIFUJI Hideaki flags = RTF_EXPIRES; 33224bed72e4SYOSHIFUJI Hideaki } else { 33234bed72e4SYOSHIFUJI Hideaki expires = 0; 33244bed72e4SYOSHIFUJI Hideaki flags = 0; 33254bed72e4SYOSHIFUJI Hideaki ifa_flags |= IFA_F_PERMANENT; 33266f704992SYOSHIFUJI Hideaki } 3327081bba5bSNoriaki TAKAMIYA 33284bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(prefered_lft, HZ); 33294bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 33304bed72e4SYOSHIFUJI Hideaki if (timeout == 0) 333155ebaef1SNoriaki TAKAMIYA ifa_flags |= IFA_F_DEPRECATED; 33324bed72e4SYOSHIFUJI Hideaki prefered_lft = timeout; 33334bed72e4SYOSHIFUJI Hideaki } 3334081bba5bSNoriaki TAKAMIYA 3335081bba5bSNoriaki TAKAMIYA spin_lock_bh(&ifp->lock); 33363b9f9a1cSNoriaki TAKAMIYA ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags; 3337081bba5bSNoriaki TAKAMIYA ifp->tstamp = jiffies; 3338081bba5bSNoriaki TAKAMIYA ifp->valid_lft = valid_lft; 3339081bba5bSNoriaki TAKAMIYA ifp->prefered_lft = prefered_lft; 3340081bba5bSNoriaki TAKAMIYA 3341081bba5bSNoriaki TAKAMIYA spin_unlock_bh(&ifp->lock); 3342081bba5bSNoriaki TAKAMIYA if (!(ifp->flags&IFA_F_TENTATIVE)) 3343081bba5bSNoriaki TAKAMIYA ipv6_ifa_notify(0, ifp); 3344081bba5bSNoriaki TAKAMIYA 334546d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev, 33466f704992SYOSHIFUJI Hideaki expires, flags); 3347081bba5bSNoriaki TAKAMIYA addrconf_verify(0); 3348081bba5bSNoriaki TAKAMIYA 3349081bba5bSNoriaki TAKAMIYA return 0; 3350081bba5bSNoriaki TAKAMIYA } 3351081bba5bSNoriaki TAKAMIYA 3352081bba5bSNoriaki TAKAMIYA static int 33531da177e4SLinus Torvalds inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 33541da177e4SLinus Torvalds { 33553b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 3356461d8837SThomas Graf struct ifaddrmsg *ifm; 3357461d8837SThomas Graf struct nlattr *tb[IFA_MAX+1]; 33581da177e4SLinus Torvalds struct in6_addr *pfx; 33597198f8ceSThomas Graf struct inet6_ifaddr *ifa; 33607198f8ceSThomas Graf struct net_device *dev; 336155ebaef1SNoriaki TAKAMIYA u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME; 336255ebaef1SNoriaki TAKAMIYA u8 ifa_flags; 3363461d8837SThomas Graf int err; 33641da177e4SLinus Torvalds 3365461d8837SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 3366461d8837SThomas Graf if (err < 0) 3367461d8837SThomas Graf return err; 3368461d8837SThomas Graf 3369461d8837SThomas Graf ifm = nlmsg_data(nlh); 3370461d8837SThomas Graf pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]); 33711da177e4SLinus Torvalds if (pfx == NULL) 33721da177e4SLinus Torvalds return -EINVAL; 33731da177e4SLinus Torvalds 3374461d8837SThomas Graf if (tb[IFA_CACHEINFO]) { 33750778769dSNoriaki TAKAMIYA struct ifa_cacheinfo *ci; 3376461d8837SThomas Graf 3377461d8837SThomas Graf ci = nla_data(tb[IFA_CACHEINFO]); 33780778769dSNoriaki TAKAMIYA valid_lft = ci->ifa_valid; 3379461d8837SThomas Graf preferred_lft = ci->ifa_prefered; 3380461d8837SThomas Graf } else { 3381461d8837SThomas Graf preferred_lft = INFINITY_LIFE_TIME; 3382461d8837SThomas Graf valid_lft = INFINITY_LIFE_TIME; 33830778769dSNoriaki TAKAMIYA } 33840778769dSNoriaki TAKAMIYA 3385af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifm->ifa_index); 33867198f8ceSThomas Graf if (dev == NULL) 33877198f8ceSThomas Graf return -ENODEV; 3388081bba5bSNoriaki TAKAMIYA 338955ebaef1SNoriaki TAKAMIYA /* We ignore other flags so far. */ 33903b9f9a1cSNoriaki TAKAMIYA ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS); 339155ebaef1SNoriaki TAKAMIYA 33921cab3da6SDaniel Lezcano ifa = ipv6_get_ifaddr(net, pfx, dev, 1); 33937198f8ceSThomas Graf if (ifa == NULL) { 33947198f8ceSThomas Graf /* 33957198f8ceSThomas Graf * It would be best to check for !NLM_F_CREATE here but 33967198f8ceSThomas Graf * userspace alreay relies on not having to provide this. 33977198f8ceSThomas Graf */ 3398af284937SDaniel Lezcano return inet6_addr_add(net, ifm->ifa_index, pfx, 3399af284937SDaniel Lezcano ifm->ifa_prefixlen, ifa_flags, 3400af284937SDaniel Lezcano preferred_lft, valid_lft); 34011da177e4SLinus Torvalds } 34021da177e4SLinus Torvalds 34037198f8ceSThomas Graf if (nlh->nlmsg_flags & NLM_F_EXCL || 34047198f8ceSThomas Graf !(nlh->nlmsg_flags & NLM_F_REPLACE)) 34057198f8ceSThomas Graf err = -EEXIST; 34067198f8ceSThomas Graf else 340755ebaef1SNoriaki TAKAMIYA err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft); 34087198f8ceSThomas Graf 34097198f8ceSThomas Graf in6_ifa_put(ifa); 34107198f8ceSThomas Graf 34117198f8ceSThomas Graf return err; 34127198f8ceSThomas Graf } 34137198f8ceSThomas Graf 3414101bb229SThomas Graf static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags, 3415101bb229SThomas Graf u8 scope, int ifindex) 3416101bb229SThomas Graf { 3417101bb229SThomas Graf struct ifaddrmsg *ifm; 3418101bb229SThomas Graf 3419101bb229SThomas Graf ifm = nlmsg_data(nlh); 3420101bb229SThomas Graf ifm->ifa_family = AF_INET6; 3421101bb229SThomas Graf ifm->ifa_prefixlen = prefixlen; 3422101bb229SThomas Graf ifm->ifa_flags = flags; 3423101bb229SThomas Graf ifm->ifa_scope = scope; 3424101bb229SThomas Graf ifm->ifa_index = ifindex; 3425101bb229SThomas Graf } 3426101bb229SThomas Graf 342785486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp, 342885486af0SThomas Graf unsigned long tstamp, u32 preferred, u32 valid) 342985486af0SThomas Graf { 343085486af0SThomas Graf struct ifa_cacheinfo ci; 343185486af0SThomas Graf 343218a31e1eSThomas Graf ci.cstamp = cstamp_delta(cstamp); 343318a31e1eSThomas Graf ci.tstamp = cstamp_delta(tstamp); 343485486af0SThomas Graf ci.ifa_prefered = preferred; 343585486af0SThomas Graf ci.ifa_valid = valid; 343685486af0SThomas Graf 343785486af0SThomas Graf return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci); 343885486af0SThomas Graf } 343985486af0SThomas Graf 3440101bb229SThomas Graf static inline int rt_scope(int ifa_scope) 3441101bb229SThomas Graf { 3442101bb229SThomas Graf if (ifa_scope & IFA_HOST) 3443101bb229SThomas Graf return RT_SCOPE_HOST; 3444101bb229SThomas Graf else if (ifa_scope & IFA_LINK) 3445101bb229SThomas Graf return RT_SCOPE_LINK; 3446101bb229SThomas Graf else if (ifa_scope & IFA_SITE) 3447101bb229SThomas Graf return RT_SCOPE_SITE; 3448101bb229SThomas Graf else 3449101bb229SThomas Graf return RT_SCOPE_UNIVERSE; 3450101bb229SThomas Graf } 3451101bb229SThomas Graf 34520ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void) 34530ab6803bSThomas Graf { 3454339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifaddrmsg)) 3455339bf98fSThomas Graf + nla_total_size(16) /* IFA_ADDRESS */ 3456339bf98fSThomas Graf + nla_total_size(sizeof(struct ifa_cacheinfo)); 34570ab6803bSThomas Graf } 3458c5396a31SYOSHIFUJI Hideaki 34591da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa, 3460b6544c0bSJamal Hadi Salim u32 pid, u32 seq, int event, unsigned int flags) 34611da177e4SLinus Torvalds { 34621da177e4SLinus Torvalds struct nlmsghdr *nlh; 346385486af0SThomas Graf u32 preferred, valid; 34641da177e4SLinus Torvalds 34650ab6803bSThomas Graf nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags); 34660ab6803bSThomas Graf if (nlh == NULL) 346726932566SPatrick McHardy return -EMSGSIZE; 34680ab6803bSThomas Graf 3469101bb229SThomas Graf put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope), 3470101bb229SThomas Graf ifa->idev->dev->ifindex); 3471101bb229SThomas Graf 34721da177e4SLinus Torvalds if (!(ifa->flags&IFA_F_PERMANENT)) { 347385486af0SThomas Graf preferred = ifa->prefered_lft; 347485486af0SThomas Graf valid = ifa->valid_lft; 347585486af0SThomas Graf if (preferred != INFINITY_LIFE_TIME) { 34761da177e4SLinus Torvalds long tval = (jiffies - ifa->tstamp)/HZ; 3477a1faa698SJens Rosenboom if (preferred > tval) 347885486af0SThomas Graf preferred -= tval; 3479a1faa698SJens Rosenboom else 3480a1faa698SJens Rosenboom preferred = 0; 3481f56619fcSBen Hutchings if (valid != INFINITY_LIFE_TIME) { 3482f56619fcSBen Hutchings if (valid > tval) 348385486af0SThomas Graf valid -= tval; 3484f56619fcSBen Hutchings else 3485f56619fcSBen Hutchings valid = 0; 3486f56619fcSBen Hutchings } 34871da177e4SLinus Torvalds } 34881da177e4SLinus Torvalds } else { 348985486af0SThomas Graf preferred = INFINITY_LIFE_TIME; 349085486af0SThomas Graf valid = INFINITY_LIFE_TIME; 34911da177e4SLinus Torvalds } 349285486af0SThomas Graf 34930ab6803bSThomas Graf if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 || 349426932566SPatrick McHardy put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) { 349526932566SPatrick McHardy nlmsg_cancel(skb, nlh); 349626932566SPatrick McHardy return -EMSGSIZE; 349726932566SPatrick McHardy } 349885486af0SThomas Graf 34990ab6803bSThomas Graf return nlmsg_end(skb, nlh); 35001da177e4SLinus Torvalds } 35011da177e4SLinus Torvalds 35021da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca, 3503e431b8c0SJamal Hadi Salim u32 pid, u32 seq, int event, u16 flags) 35041da177e4SLinus Torvalds { 35051da177e4SLinus Torvalds struct nlmsghdr *nlh; 3506101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 3507101bb229SThomas Graf int ifindex = ifmca->idev->dev->ifindex; 35081da177e4SLinus Torvalds 35091da177e4SLinus Torvalds if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE) 3510101bb229SThomas Graf scope = RT_SCOPE_SITE; 3511101bb229SThomas Graf 35120ab6803bSThomas Graf nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags); 35130ab6803bSThomas Graf if (nlh == NULL) 351426932566SPatrick McHardy return -EMSGSIZE; 35150ab6803bSThomas Graf 3516101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 35170ab6803bSThomas Graf if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 || 35180ab6803bSThomas Graf put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp, 351926932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 352026932566SPatrick McHardy nlmsg_cancel(skb, nlh); 352126932566SPatrick McHardy return -EMSGSIZE; 352226932566SPatrick McHardy } 352385486af0SThomas Graf 35240ab6803bSThomas Graf return nlmsg_end(skb, nlh); 35251da177e4SLinus Torvalds } 35261da177e4SLinus Torvalds 35271da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca, 3528b6544c0bSJamal Hadi Salim u32 pid, u32 seq, int event, unsigned int flags) 35291da177e4SLinus Torvalds { 35301da177e4SLinus Torvalds struct nlmsghdr *nlh; 3531101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 3532101bb229SThomas Graf int ifindex = ifaca->aca_idev->dev->ifindex; 35331da177e4SLinus Torvalds 35341da177e4SLinus Torvalds if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE) 3535101bb229SThomas Graf scope = RT_SCOPE_SITE; 3536101bb229SThomas Graf 35370ab6803bSThomas Graf nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags); 35380ab6803bSThomas Graf if (nlh == NULL) 353926932566SPatrick McHardy return -EMSGSIZE; 35400ab6803bSThomas Graf 3541101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 35420ab6803bSThomas Graf if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 || 35430ab6803bSThomas Graf put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp, 354426932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 354526932566SPatrick McHardy nlmsg_cancel(skb, nlh); 354626932566SPatrick McHardy return -EMSGSIZE; 354726932566SPatrick McHardy } 354885486af0SThomas Graf 35490ab6803bSThomas Graf return nlmsg_end(skb, nlh); 35501da177e4SLinus Torvalds } 35511da177e4SLinus Torvalds 3552e21e8467SStephen Hemminger enum addr_type_t { 35531da177e4SLinus Torvalds UNICAST_ADDR, 35541da177e4SLinus Torvalds MULTICAST_ADDR, 35551da177e4SLinus Torvalds ANYCAST_ADDR, 35561da177e4SLinus Torvalds }; 35571da177e4SLinus Torvalds 3558234b27c3SEric Dumazet /* called with rcu_read_lock() */ 3559234b27c3SEric Dumazet static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb, 3560234b27c3SEric Dumazet struct netlink_callback *cb, enum addr_type_t type, 3561234b27c3SEric Dumazet int s_ip_idx, int *p_ip_idx) 35621da177e4SLinus Torvalds { 35631da177e4SLinus Torvalds struct ifmcaddr6 *ifmca; 35641da177e4SLinus Torvalds struct ifacaddr6 *ifaca; 3565234b27c3SEric Dumazet int err = 1; 3566234b27c3SEric Dumazet int ip_idx = *p_ip_idx; 35671da177e4SLinus Torvalds 35681da177e4SLinus Torvalds read_lock_bh(&idev->lock); 35691da177e4SLinus Torvalds switch (type) { 3570502a2ffdSstephen hemminger case UNICAST_ADDR: { 3571502a2ffdSstephen hemminger struct inet6_ifaddr *ifa; 3572502a2ffdSstephen hemminger 3573ae9cda5dSYOSHIFUJI Hideaki /* unicast address incl. temp addr */ 3574502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 3575502a2ffdSstephen hemminger if (++ip_idx < s_ip_idx) 35761da177e4SLinus Torvalds continue; 35775d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifaddr(skb, ifa, 35781da177e4SLinus Torvalds NETLINK_CB(cb->skb).pid, 35795d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 35805d5619b4SYOSHIFUJI Hideaki RTM_NEWADDR, 35815d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 3582bcd32326SEric Dumazet if (err <= 0) 3583bcd32326SEric Dumazet break; 35841da177e4SLinus Torvalds } 35851da177e4SLinus Torvalds break; 3586502a2ffdSstephen hemminger } 35871da177e4SLinus Torvalds case MULTICAST_ADDR: 35881da177e4SLinus Torvalds /* multicast address */ 35891da177e4SLinus Torvalds for (ifmca = idev->mc_list; ifmca; 35901da177e4SLinus Torvalds ifmca = ifmca->next, ip_idx++) { 35911da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 35921da177e4SLinus Torvalds continue; 35935d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifmcaddr(skb, ifmca, 35941da177e4SLinus Torvalds NETLINK_CB(cb->skb).pid, 35955d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 35965d5619b4SYOSHIFUJI Hideaki RTM_GETMULTICAST, 35975d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 3598bcd32326SEric Dumazet if (err <= 0) 3599bcd32326SEric Dumazet break; 36001da177e4SLinus Torvalds } 36011da177e4SLinus Torvalds break; 36021da177e4SLinus Torvalds case ANYCAST_ADDR: 36031da177e4SLinus Torvalds /* anycast address */ 36041da177e4SLinus Torvalds for (ifaca = idev->ac_list; ifaca; 36051da177e4SLinus Torvalds ifaca = ifaca->aca_next, ip_idx++) { 36061da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 36071da177e4SLinus Torvalds continue; 36085d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifacaddr(skb, ifaca, 36091da177e4SLinus Torvalds NETLINK_CB(cb->skb).pid, 36105d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 36115d5619b4SYOSHIFUJI Hideaki RTM_GETANYCAST, 36125d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 3613bcd32326SEric Dumazet if (err <= 0) 3614bcd32326SEric Dumazet break; 36151da177e4SLinus Torvalds } 36161da177e4SLinus Torvalds break; 36171da177e4SLinus Torvalds default: 36181da177e4SLinus Torvalds break; 36191da177e4SLinus Torvalds } 36201da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 3621234b27c3SEric Dumazet *p_ip_idx = ip_idx; 3622234b27c3SEric Dumazet return err; 3623234b27c3SEric Dumazet } 36245d5619b4SYOSHIFUJI Hideaki 3625234b27c3SEric Dumazet static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb, 3626234b27c3SEric Dumazet enum addr_type_t type) 3627234b27c3SEric Dumazet { 3628234b27c3SEric Dumazet struct net *net = sock_net(skb->sk); 3629234b27c3SEric Dumazet int h, s_h; 3630234b27c3SEric Dumazet int idx, ip_idx; 3631234b27c3SEric Dumazet int s_idx, s_ip_idx; 3632234b27c3SEric Dumazet struct net_device *dev; 3633234b27c3SEric Dumazet struct inet6_dev *idev; 3634234b27c3SEric Dumazet struct hlist_head *head; 3635234b27c3SEric Dumazet struct hlist_node *node; 3636234b27c3SEric Dumazet 3637234b27c3SEric Dumazet s_h = cb->args[0]; 3638234b27c3SEric Dumazet s_idx = idx = cb->args[1]; 3639234b27c3SEric Dumazet s_ip_idx = ip_idx = cb->args[2]; 3640234b27c3SEric Dumazet 3641234b27c3SEric Dumazet rcu_read_lock(); 3642234b27c3SEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 3643234b27c3SEric Dumazet idx = 0; 3644234b27c3SEric Dumazet head = &net->dev_index_head[h]; 3645234b27c3SEric Dumazet hlist_for_each_entry_rcu(dev, node, head, index_hlist) { 3646234b27c3SEric Dumazet if (idx < s_idx) 3647234b27c3SEric Dumazet goto cont; 36484b97efdfSPatrick McHardy if (h > s_h || idx > s_idx) 3649234b27c3SEric Dumazet s_ip_idx = 0; 3650234b27c3SEric Dumazet ip_idx = 0; 3651e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 3652e21e8467SStephen Hemminger if (!idev) 3653234b27c3SEric Dumazet goto cont; 3654234b27c3SEric Dumazet 3655234b27c3SEric Dumazet if (in6_dump_addrs(idev, skb, cb, type, 3656234b27c3SEric Dumazet s_ip_idx, &ip_idx) <= 0) 3657234b27c3SEric Dumazet goto done; 36587562f876SPavel Emelianov cont: 36597562f876SPavel Emelianov idx++; 36601da177e4SLinus Torvalds } 3661234b27c3SEric Dumazet } 3662234b27c3SEric Dumazet done: 3663234b27c3SEric Dumazet rcu_read_unlock(); 3664234b27c3SEric Dumazet cb->args[0] = h; 3665234b27c3SEric Dumazet cb->args[1] = idx; 3666234b27c3SEric Dumazet cb->args[2] = ip_idx; 3667234b27c3SEric Dumazet 36681da177e4SLinus Torvalds return skb->len; 36691da177e4SLinus Torvalds } 36701da177e4SLinus Torvalds 36711da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) 36721da177e4SLinus Torvalds { 36731da177e4SLinus Torvalds enum addr_type_t type = UNICAST_ADDR; 3674b854272bSDenis V. Lunev 36751da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 36761da177e4SLinus Torvalds } 36771da177e4SLinus Torvalds 36781da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb) 36791da177e4SLinus Torvalds { 36801da177e4SLinus Torvalds enum addr_type_t type = MULTICAST_ADDR; 3681b854272bSDenis V. Lunev 36821da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 36831da177e4SLinus Torvalds } 36841da177e4SLinus Torvalds 36851da177e4SLinus Torvalds 36861da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb) 36871da177e4SLinus Torvalds { 36881da177e4SLinus Torvalds enum addr_type_t type = ANYCAST_ADDR; 3689b854272bSDenis V. Lunev 36901da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 36911da177e4SLinus Torvalds } 36921da177e4SLinus Torvalds 36931b29fc2cSThomas Graf static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh, 36941b29fc2cSThomas Graf void *arg) 36956c223828SNoriaki TAKAMIYA { 36963b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 36971b29fc2cSThomas Graf struct ifaddrmsg *ifm; 36981b29fc2cSThomas Graf struct nlattr *tb[IFA_MAX+1]; 36996c223828SNoriaki TAKAMIYA struct in6_addr *addr = NULL; 37006c223828SNoriaki TAKAMIYA struct net_device *dev = NULL; 37016c223828SNoriaki TAKAMIYA struct inet6_ifaddr *ifa; 37026c223828SNoriaki TAKAMIYA struct sk_buff *skb; 37036c223828SNoriaki TAKAMIYA int err; 37046c223828SNoriaki TAKAMIYA 37051b29fc2cSThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 37061b29fc2cSThomas Graf if (err < 0) 37071b29fc2cSThomas Graf goto errout; 37086c223828SNoriaki TAKAMIYA 37091b29fc2cSThomas Graf addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]); 37101b29fc2cSThomas Graf if (addr == NULL) { 37111b29fc2cSThomas Graf err = -EINVAL; 37121b29fc2cSThomas Graf goto errout; 37131b29fc2cSThomas Graf } 37141b29fc2cSThomas Graf 37151b29fc2cSThomas Graf ifm = nlmsg_data(nlh); 37166c223828SNoriaki TAKAMIYA if (ifm->ifa_index) 37176fda7350SBenjamin Thery dev = __dev_get_by_index(net, ifm->ifa_index); 37186c223828SNoriaki TAKAMIYA 3719e21e8467SStephen Hemminger ifa = ipv6_get_ifaddr(net, addr, dev, 1); 3720e21e8467SStephen Hemminger if (!ifa) { 37211b29fc2cSThomas Graf err = -EADDRNOTAVAIL; 37221b29fc2cSThomas Graf goto errout; 37236c223828SNoriaki TAKAMIYA } 37246c223828SNoriaki TAKAMIYA 3725e21e8467SStephen Hemminger skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL); 3726e21e8467SStephen Hemminger if (!skb) { 37271b29fc2cSThomas Graf err = -ENOBUFS; 37281b29fc2cSThomas Graf goto errout_ifa; 37291b29fc2cSThomas Graf } 37301b29fc2cSThomas Graf 37316c223828SNoriaki TAKAMIYA err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).pid, 37326c223828SNoriaki TAKAMIYA nlh->nlmsg_seq, RTM_NEWADDR, 0); 373326932566SPatrick McHardy if (err < 0) { 373426932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 373526932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 373626932566SPatrick McHardy kfree_skb(skb); 373726932566SPatrick McHardy goto errout_ifa; 373826932566SPatrick McHardy } 37396fda7350SBenjamin Thery err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid); 37401b29fc2cSThomas Graf errout_ifa: 37416c223828SNoriaki TAKAMIYA in6_ifa_put(ifa); 37421b29fc2cSThomas Graf errout: 37436c223828SNoriaki TAKAMIYA return err; 37446c223828SNoriaki TAKAMIYA } 37456c223828SNoriaki TAKAMIYA 37461da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa) 37471da177e4SLinus Torvalds { 37481da177e4SLinus Torvalds struct sk_buff *skb; 3749c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(ifa->idev->dev); 37505d620266SThomas Graf int err = -ENOBUFS; 37511da177e4SLinus Torvalds 37520ab6803bSThomas Graf skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC); 37535d620266SThomas Graf if (skb == NULL) 37545d620266SThomas Graf goto errout; 37555d620266SThomas Graf 37565d620266SThomas Graf err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0); 375726932566SPatrick McHardy if (err < 0) { 375826932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 375926932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 376026932566SPatrick McHardy kfree_skb(skb); 376126932566SPatrick McHardy goto errout; 376226932566SPatrick McHardy } 37631ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC); 37641ce85fe4SPablo Neira Ayuso return; 37655d620266SThomas Graf errout: 37665d620266SThomas Graf if (err < 0) 37676fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err); 37681da177e4SLinus Torvalds } 37691da177e4SLinus Torvalds 3770b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf, 37711da177e4SLinus Torvalds __s32 *array, int bytes) 37721da177e4SLinus Torvalds { 377304561c1fSThomas Graf BUG_ON(bytes < (DEVCONF_MAX * 4)); 377404561c1fSThomas Graf 37751da177e4SLinus Torvalds memset(array, 0, bytes); 37761da177e4SLinus Torvalds array[DEVCONF_FORWARDING] = cnf->forwarding; 37771da177e4SLinus Torvalds array[DEVCONF_HOPLIMIT] = cnf->hop_limit; 37781da177e4SLinus Torvalds array[DEVCONF_MTU6] = cnf->mtu6; 37791da177e4SLinus Torvalds array[DEVCONF_ACCEPT_RA] = cnf->accept_ra; 37801da177e4SLinus Torvalds array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects; 37811da177e4SLinus Torvalds array[DEVCONF_AUTOCONF] = cnf->autoconf; 37821da177e4SLinus Torvalds array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits; 37831da177e4SLinus Torvalds array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits; 378493908d19SThomas Graf array[DEVCONF_RTR_SOLICIT_INTERVAL] = 378593908d19SThomas Graf jiffies_to_msecs(cnf->rtr_solicit_interval); 378693908d19SThomas Graf array[DEVCONF_RTR_SOLICIT_DELAY] = 378793908d19SThomas Graf jiffies_to_msecs(cnf->rtr_solicit_delay); 37881da177e4SLinus Torvalds array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version; 37891da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 37901da177e4SLinus Torvalds array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr; 37911da177e4SLinus Torvalds array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft; 37921da177e4SLinus Torvalds array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft; 37931da177e4SLinus Torvalds array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry; 37941da177e4SLinus Torvalds array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor; 37951da177e4SLinus Torvalds #endif 37961da177e4SLinus Torvalds array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses; 379765f5c7c1SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr; 3798c4fd30ebSYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo; 3799930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 3800930d6ff2SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref; 380193908d19SThomas Graf array[DEVCONF_RTR_PROBE_INTERVAL] = 380293908d19SThomas Graf jiffies_to_msecs(cnf->rtr_probe_interval); 3803fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 380409c884d4SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen; 380509c884d4SYOSHIFUJI Hideaki #endif 3806930d6ff2SYOSHIFUJI Hideaki #endif 3807fbea49e1SYOSHIFUJI Hideaki array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp; 38080bcbc926SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route; 380995c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 381095c385b4SNeil Horman array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad; 381195c385b4SNeil Horman #endif 38127bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 38137bc570c8SYOSHIFUJI Hideaki array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding; 38147bc570c8SYOSHIFUJI Hideaki #endif 3815778d80beSYOSHIFUJI Hideaki array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6; 38161b34be74SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad; 3817c3faca05SCosmin Ratiu array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao; 38181da177e4SLinus Torvalds } 38191da177e4SLinus Torvalds 3820b382b191SThomas Graf static inline size_t inet6_ifla6_size(void) 3821b382b191SThomas Graf { 3822b382b191SThomas Graf return nla_total_size(4) /* IFLA_INET6_FLAGS */ 3823b382b191SThomas Graf + nla_total_size(sizeof(struct ifla_cacheinfo)) 3824b382b191SThomas Graf + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */ 3825b382b191SThomas Graf + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */ 3826b382b191SThomas Graf + nla_total_size(ICMP6_MIB_MAX * 8); /* IFLA_INET6_ICMP6STATS */ 3827b382b191SThomas Graf } 3828b382b191SThomas Graf 3829339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void) 3830339bf98fSThomas Graf { 3831339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 3832339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 3833339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 3834339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 3835339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 3836b382b191SThomas Graf + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */ 3837339bf98fSThomas Graf } 3838c5396a31SYOSHIFUJI Hideaki 38397d720c3eSTejun Heo static inline void __snmp6_fill_stats(u64 *stats, void __percpu **mib, 38407d720c3eSTejun Heo int items, int bytes) 38417f7d9a6bSHerbert Xu { 38427f7d9a6bSHerbert Xu int i; 38437f7d9a6bSHerbert Xu int pad = bytes - sizeof(u64) * items; 38447f7d9a6bSHerbert Xu BUG_ON(pad < 0); 38457f7d9a6bSHerbert Xu 38467f7d9a6bSHerbert Xu /* Use put_unaligned() because stats may not be aligned for u64. */ 38477f7d9a6bSHerbert Xu put_unaligned(items, &stats[0]); 38487f7d9a6bSHerbert Xu for (i = 1; i < items; i++) 38497f7d9a6bSHerbert Xu put_unaligned(snmp_fold_field(mib, i), &stats[i]); 38507f7d9a6bSHerbert Xu 38517f7d9a6bSHerbert Xu memset(&stats[items], 0, pad); 38527f7d9a6bSHerbert Xu } 38537f7d9a6bSHerbert Xu 38544ce3c183SEric Dumazet static inline void __snmp6_fill_stats64(u64 *stats, void __percpu **mib, 38554ce3c183SEric Dumazet int items, int bytes, size_t syncpoff) 38564ce3c183SEric Dumazet { 38574ce3c183SEric Dumazet int i; 38584ce3c183SEric Dumazet int pad = bytes - sizeof(u64) * items; 38594ce3c183SEric Dumazet BUG_ON(pad < 0); 38604ce3c183SEric Dumazet 38614ce3c183SEric Dumazet /* Use put_unaligned() because stats may not be aligned for u64. */ 38624ce3c183SEric Dumazet put_unaligned(items, &stats[0]); 38634ce3c183SEric Dumazet for (i = 1; i < items; i++) 38644ce3c183SEric Dumazet put_unaligned(snmp_fold_field64(mib, i, syncpoff), &stats[i]); 38654ce3c183SEric Dumazet 38664ce3c183SEric Dumazet memset(&stats[items], 0, pad); 38674ce3c183SEric Dumazet } 38684ce3c183SEric Dumazet 38697f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype, 38707f7d9a6bSHerbert Xu int bytes) 38717f7d9a6bSHerbert Xu { 38727f7d9a6bSHerbert Xu switch (attrtype) { 38737f7d9a6bSHerbert Xu case IFLA_INET6_STATS: 38744ce3c183SEric Dumazet __snmp6_fill_stats64(stats, (void __percpu **)idev->stats.ipv6, 38754ce3c183SEric Dumazet IPSTATS_MIB_MAX, bytes, offsetof(struct ipstats_mib, syncp)); 38767f7d9a6bSHerbert Xu break; 38777f7d9a6bSHerbert Xu case IFLA_INET6_ICMP6STATS: 38787d720c3eSTejun Heo __snmp6_fill_stats(stats, (void __percpu **)idev->stats.icmpv6, ICMP6_MIB_MAX, bytes); 38797f7d9a6bSHerbert Xu break; 38807f7d9a6bSHerbert Xu } 38817f7d9a6bSHerbert Xu } 38827f7d9a6bSHerbert Xu 3883b382b191SThomas Graf static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev) 38841da177e4SLinus Torvalds { 3885bf99f1bdSYOSHIFUJI Hideaki struct nlattr *nla; 38861da177e4SLinus Torvalds struct ifla_cacheinfo ci; 38871da177e4SLinus Torvalds 388804561c1fSThomas Graf NLA_PUT_U32(skb, IFLA_INET6_FLAGS, idev->if_flags); 38891da177e4SLinus Torvalds 38901da177e4SLinus Torvalds ci.max_reasm_len = IPV6_MAXPLEN; 389124912420SDavid S. Miller ci.tstamp = cstamp_delta(idev->tstamp); 389224912420SDavid S. Miller ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time); 389324912420SDavid S. Miller ci.retrans_time = jiffies_to_msecs(idev->nd_parms->retrans_time); 389404561c1fSThomas Graf NLA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci); 38951da177e4SLinus Torvalds 3896bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32)); 3897bf99f1bdSYOSHIFUJI Hideaki if (nla == NULL) 389804561c1fSThomas Graf goto nla_put_failure; 3899bf99f1bdSYOSHIFUJI Hideaki ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla)); 39001da177e4SLinus Torvalds 3901bf99f1bdSYOSHIFUJI Hideaki /* XXX - MC not implemented */ 3902bf99f1bdSYOSHIFUJI Hideaki 3903bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64)); 3904bf99f1bdSYOSHIFUJI Hideaki if (nla == NULL) 3905bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 3906bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla)); 3907bf99f1bdSYOSHIFUJI Hideaki 3908bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64)); 3909bf99f1bdSYOSHIFUJI Hideaki if (nla == NULL) 3910bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 3911bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla)); 39121da177e4SLinus Torvalds 3913b382b191SThomas Graf return 0; 3914b382b191SThomas Graf 3915b382b191SThomas Graf nla_put_failure: 3916b382b191SThomas Graf return -EMSGSIZE; 3917b382b191SThomas Graf } 3918b382b191SThomas Graf 3919b382b191SThomas Graf static size_t inet6_get_link_af_size(const struct net_device *dev) 3920b382b191SThomas Graf { 3921b382b191SThomas Graf if (!__in6_dev_get(dev)) 3922b382b191SThomas Graf return 0; 3923b382b191SThomas Graf 3924b382b191SThomas Graf return inet6_ifla6_size(); 3925b382b191SThomas Graf } 3926b382b191SThomas Graf 3927b382b191SThomas Graf static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev) 3928b382b191SThomas Graf { 3929b382b191SThomas Graf struct inet6_dev *idev = __in6_dev_get(dev); 3930b382b191SThomas Graf 3931b382b191SThomas Graf if (!idev) 3932b382b191SThomas Graf return -ENODATA; 3933b382b191SThomas Graf 3934b382b191SThomas Graf if (inet6_fill_ifla6_attrs(skb, idev) < 0) 3935b382b191SThomas Graf return -EMSGSIZE; 3936b382b191SThomas Graf 3937b382b191SThomas Graf return 0; 3938b382b191SThomas Graf } 3939b382b191SThomas Graf 3940b382b191SThomas Graf static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev, 3941b382b191SThomas Graf u32 pid, u32 seq, int event, unsigned int flags) 3942b382b191SThomas Graf { 3943b382b191SThomas Graf struct net_device *dev = idev->dev; 3944b382b191SThomas Graf struct ifinfomsg *hdr; 3945b382b191SThomas Graf struct nlmsghdr *nlh; 3946b382b191SThomas Graf void *protoinfo; 3947b382b191SThomas Graf 3948b382b191SThomas Graf nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags); 3949b382b191SThomas Graf if (nlh == NULL) 3950b382b191SThomas Graf return -EMSGSIZE; 3951b382b191SThomas Graf 3952b382b191SThomas Graf hdr = nlmsg_data(nlh); 3953b382b191SThomas Graf hdr->ifi_family = AF_INET6; 3954b382b191SThomas Graf hdr->__ifi_pad = 0; 3955b382b191SThomas Graf hdr->ifi_type = dev->type; 3956b382b191SThomas Graf hdr->ifi_index = dev->ifindex; 3957b382b191SThomas Graf hdr->ifi_flags = dev_get_flags(dev); 3958b382b191SThomas Graf hdr->ifi_change = 0; 3959b382b191SThomas Graf 3960b382b191SThomas Graf NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name); 3961b382b191SThomas Graf 3962b382b191SThomas Graf if (dev->addr_len) 3963b382b191SThomas Graf NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr); 3964b382b191SThomas Graf 3965b382b191SThomas Graf NLA_PUT_U32(skb, IFLA_MTU, dev->mtu); 3966b382b191SThomas Graf if (dev->ifindex != dev->iflink) 3967b382b191SThomas Graf NLA_PUT_U32(skb, IFLA_LINK, dev->iflink); 3968b382b191SThomas Graf 3969b382b191SThomas Graf protoinfo = nla_nest_start(skb, IFLA_PROTINFO); 3970b382b191SThomas Graf if (protoinfo == NULL) 3971b382b191SThomas Graf goto nla_put_failure; 3972b382b191SThomas Graf 3973b382b191SThomas Graf if (inet6_fill_ifla6_attrs(skb, idev) < 0) 3974b382b191SThomas Graf goto nla_put_failure; 3975b382b191SThomas Graf 397604561c1fSThomas Graf nla_nest_end(skb, protoinfo); 397704561c1fSThomas Graf return nlmsg_end(skb, nlh); 39781da177e4SLinus Torvalds 397904561c1fSThomas Graf nla_put_failure: 398026932566SPatrick McHardy nlmsg_cancel(skb, nlh); 398126932566SPatrick McHardy return -EMSGSIZE; 39821da177e4SLinus Torvalds } 39831da177e4SLinus Torvalds 39841da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 39851da177e4SLinus Torvalds { 39863b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 398784d2697dSEric Dumazet int h, s_h; 3988434a8a58SDavid S. Miller int idx = 0, s_idx; 39891da177e4SLinus Torvalds struct net_device *dev; 39901da177e4SLinus Torvalds struct inet6_dev *idev; 399184d2697dSEric Dumazet struct hlist_head *head; 399284d2697dSEric Dumazet struct hlist_node *node; 39931da177e4SLinus Torvalds 399484d2697dSEric Dumazet s_h = cb->args[0]; 399584d2697dSEric Dumazet s_idx = cb->args[1]; 399684d2697dSEric Dumazet 399784d2697dSEric Dumazet rcu_read_lock(); 399884d2697dSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 39997562f876SPavel Emelianov idx = 0; 400084d2697dSEric Dumazet head = &net->dev_index_head[h]; 400184d2697dSEric Dumazet hlist_for_each_entry_rcu(dev, node, head, index_hlist) { 40021da177e4SLinus Torvalds if (idx < s_idx) 40037562f876SPavel Emelianov goto cont; 400484d2697dSEric Dumazet idev = __in6_dev_get(dev); 400584d2697dSEric Dumazet if (!idev) 40067562f876SPavel Emelianov goto cont; 400784d2697dSEric Dumazet if (inet6_fill_ifinfo(skb, idev, 400884d2697dSEric Dumazet NETLINK_CB(cb->skb).pid, 400984d2697dSEric Dumazet cb->nlh->nlmsg_seq, 401084d2697dSEric Dumazet RTM_NEWLINK, NLM_F_MULTI) <= 0) 401184d2697dSEric Dumazet goto out; 40127562f876SPavel Emelianov cont: 40137562f876SPavel Emelianov idx++; 40141da177e4SLinus Torvalds } 401584d2697dSEric Dumazet } 401684d2697dSEric Dumazet out: 401784d2697dSEric Dumazet rcu_read_unlock(); 401884d2697dSEric Dumazet cb->args[1] = idx; 401984d2697dSEric Dumazet cb->args[0] = h; 40201da177e4SLinus Torvalds 40211da177e4SLinus Torvalds return skb->len; 40221da177e4SLinus Torvalds } 40231da177e4SLinus Torvalds 40241da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev) 40251da177e4SLinus Torvalds { 40261da177e4SLinus Torvalds struct sk_buff *skb; 4027c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 40288d7a76c9SThomas Graf int err = -ENOBUFS; 40291da177e4SLinus Torvalds 4030339bf98fSThomas Graf skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC); 40318d7a76c9SThomas Graf if (skb == NULL) 40328d7a76c9SThomas Graf goto errout; 40338d7a76c9SThomas Graf 40348d7a76c9SThomas Graf err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0); 403526932566SPatrick McHardy if (err < 0) { 403626932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */ 403726932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 403826932566SPatrick McHardy kfree_skb(skb); 403926932566SPatrick McHardy goto errout; 404026932566SPatrick McHardy } 40415f75a104SNicolas Dichtel rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC); 40421ce85fe4SPablo Neira Ayuso return; 40438d7a76c9SThomas Graf errout: 40448d7a76c9SThomas Graf if (err < 0) 40455f75a104SNicolas Dichtel rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err); 40461da177e4SLinus Torvalds } 40471da177e4SLinus Torvalds 4048339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void) 4049339bf98fSThomas Graf { 4050339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct prefixmsg)) 4051339bf98fSThomas Graf + nla_total_size(sizeof(struct in6_addr)) 4052339bf98fSThomas Graf + nla_total_size(sizeof(struct prefix_cacheinfo)); 4053339bf98fSThomas Graf } 4054c5396a31SYOSHIFUJI Hideaki 40551da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev, 4056b6544c0bSJamal Hadi Salim struct prefix_info *pinfo, u32 pid, u32 seq, 4057b6544c0bSJamal Hadi Salim int event, unsigned int flags) 40581da177e4SLinus Torvalds { 40591da177e4SLinus Torvalds struct prefixmsg *pmsg; 40601da177e4SLinus Torvalds struct nlmsghdr *nlh; 40611da177e4SLinus Torvalds struct prefix_cacheinfo ci; 40621da177e4SLinus Torvalds 40636051e2f4SThomas Graf nlh = nlmsg_put(skb, pid, seq, event, sizeof(*pmsg), flags); 40646051e2f4SThomas Graf if (nlh == NULL) 406526932566SPatrick McHardy return -EMSGSIZE; 40666051e2f4SThomas Graf 40676051e2f4SThomas Graf pmsg = nlmsg_data(nlh); 40681da177e4SLinus Torvalds pmsg->prefix_family = AF_INET6; 40698a47077aSPatrick McHardy pmsg->prefix_pad1 = 0; 40708a47077aSPatrick McHardy pmsg->prefix_pad2 = 0; 40711da177e4SLinus Torvalds pmsg->prefix_ifindex = idev->dev->ifindex; 40721da177e4SLinus Torvalds pmsg->prefix_len = pinfo->prefix_len; 40731da177e4SLinus Torvalds pmsg->prefix_type = pinfo->type; 40748a47077aSPatrick McHardy pmsg->prefix_pad3 = 0; 40751da177e4SLinus Torvalds pmsg->prefix_flags = 0; 40761da177e4SLinus Torvalds if (pinfo->onlink) 40771da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_ONLINK; 40781da177e4SLinus Torvalds if (pinfo->autoconf) 40791da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_AUTOCONF; 40801da177e4SLinus Torvalds 40816051e2f4SThomas Graf NLA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix); 40821da177e4SLinus Torvalds 40831da177e4SLinus Torvalds ci.preferred_time = ntohl(pinfo->prefered); 40841da177e4SLinus Torvalds ci.valid_time = ntohl(pinfo->valid); 40856051e2f4SThomas Graf NLA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci); 40861da177e4SLinus Torvalds 40876051e2f4SThomas Graf return nlmsg_end(skb, nlh); 40881da177e4SLinus Torvalds 40896051e2f4SThomas Graf nla_put_failure: 409026932566SPatrick McHardy nlmsg_cancel(skb, nlh); 409126932566SPatrick McHardy return -EMSGSIZE; 40921da177e4SLinus Torvalds } 40931da177e4SLinus Torvalds 40941da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev, 40951da177e4SLinus Torvalds struct prefix_info *pinfo) 40961da177e4SLinus Torvalds { 40971da177e4SLinus Torvalds struct sk_buff *skb; 4098c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 40998c384bfaSThomas Graf int err = -ENOBUFS; 41001da177e4SLinus Torvalds 4101339bf98fSThomas Graf skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC); 41028c384bfaSThomas Graf if (skb == NULL) 41038c384bfaSThomas Graf goto errout; 41048c384bfaSThomas Graf 41058c384bfaSThomas Graf err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0); 410626932566SPatrick McHardy if (err < 0) { 410726932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */ 410826932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 410926932566SPatrick McHardy kfree_skb(skb); 411026932566SPatrick McHardy goto errout; 411126932566SPatrick McHardy } 41121ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC); 41131ce85fe4SPablo Neira Ayuso return; 41148c384bfaSThomas Graf errout: 41158c384bfaSThomas Graf if (err < 0) 41166fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err); 41171da177e4SLinus Torvalds } 41181da177e4SLinus Torvalds 41191da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 41201da177e4SLinus Torvalds { 41211da177e4SLinus Torvalds inet6_ifa_notify(event ? : RTM_NEWADDR, ifp); 41221da177e4SLinus Torvalds 41231da177e4SLinus Torvalds switch (event) { 41241da177e4SLinus Torvalds case RTM_NEWADDR: 412595c385b4SNeil Horman /* 412695c385b4SNeil Horman * If the address was optimistic 412795c385b4SNeil Horman * we inserted the route at the start of 412895c385b4SNeil Horman * our DAD process, so we don't need 412995c385b4SNeil Horman * to do it again 413095c385b4SNeil Horman */ 413195c385b4SNeil Horman if (!(ifp->rt->rt6i_node)) 413240e22e8fSThomas Graf ip6_ins_rt(ifp->rt); 41331da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 41341da177e4SLinus Torvalds addrconf_join_anycast(ifp); 41351da177e4SLinus Torvalds break; 41361da177e4SLinus Torvalds case RTM_DELADDR: 41371da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 41381da177e4SLinus Torvalds addrconf_leave_anycast(ifp); 41391da177e4SLinus Torvalds addrconf_leave_solict(ifp->idev, &ifp->addr); 4140d8d1f30bSChangli Gao dst_hold(&ifp->rt->dst); 414193fa159aSstephen hemminger 414273a8bd74SDavid S. Miller if (ip6_del_rt(ifp->rt)) 4143d8d1f30bSChangli Gao dst_free(&ifp->rt->dst); 41441da177e4SLinus Torvalds break; 41451da177e4SLinus Torvalds } 41461da177e4SLinus Torvalds } 41471da177e4SLinus Torvalds 41481da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 41491da177e4SLinus Torvalds { 41508814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 41511da177e4SLinus Torvalds if (likely(ifp->idev->dead == 0)) 41521da177e4SLinus Torvalds __ipv6_ifa_notify(event, ifp); 41538814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 41541da177e4SLinus Torvalds } 41551da177e4SLinus Torvalds 41561da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 41571da177e4SLinus Torvalds 41581da177e4SLinus Torvalds static 41598d65af78SAlexey Dobriyan int addrconf_sysctl_forward(ctl_table *ctl, int write, 41601da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 41611da177e4SLinus Torvalds { 41621da177e4SLinus Torvalds int *valp = ctl->data; 41631da177e4SLinus Torvalds int val = *valp; 416488af182eSEric W. Biederman loff_t pos = *ppos; 41651da177e4SLinus Torvalds int ret; 41661da177e4SLinus Torvalds 41678d65af78SAlexey Dobriyan ret = proc_dointvec(ctl, write, buffer, lenp, ppos); 41681da177e4SLinus Torvalds 4169c8fecf22SPavel Emelyanov if (write) 4170b325fddbSStephen Hemminger ret = addrconf_fixup_forwarding(ctl, valp, val); 417188af182eSEric W. Biederman if (ret) 417288af182eSEric W. Biederman *ppos = pos; 41731da177e4SLinus Torvalds return ret; 41741da177e4SLinus Torvalds } 41751da177e4SLinus Torvalds 417656d417b1SBrian Haley static void dev_disable_change(struct inet6_dev *idev) 417756d417b1SBrian Haley { 417856d417b1SBrian Haley if (!idev || !idev->dev) 417956d417b1SBrian Haley return; 418056d417b1SBrian Haley 418156d417b1SBrian Haley if (idev->cnf.disable_ipv6) 418256d417b1SBrian Haley addrconf_notify(NULL, NETDEV_DOWN, idev->dev); 418356d417b1SBrian Haley else 418456d417b1SBrian Haley addrconf_notify(NULL, NETDEV_UP, idev->dev); 418556d417b1SBrian Haley } 418656d417b1SBrian Haley 418756d417b1SBrian Haley static void addrconf_disable_change(struct net *net, __s32 newf) 418856d417b1SBrian Haley { 418956d417b1SBrian Haley struct net_device *dev; 419056d417b1SBrian Haley struct inet6_dev *idev; 419156d417b1SBrian Haley 419256d417b1SBrian Haley rcu_read_lock(); 4193c6d14c84SEric Dumazet for_each_netdev_rcu(net, dev) { 419456d417b1SBrian Haley idev = __in6_dev_get(dev); 419556d417b1SBrian Haley if (idev) { 419656d417b1SBrian Haley int changed = (!idev->cnf.disable_ipv6) ^ (!newf); 419756d417b1SBrian Haley idev->cnf.disable_ipv6 = newf; 419856d417b1SBrian Haley if (changed) 419956d417b1SBrian Haley dev_disable_change(idev); 420056d417b1SBrian Haley } 420156d417b1SBrian Haley } 4202c6d14c84SEric Dumazet rcu_read_unlock(); 420356d417b1SBrian Haley } 420456d417b1SBrian Haley 420556d417b1SBrian Haley static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int old) 420656d417b1SBrian Haley { 420756d417b1SBrian Haley struct net *net; 420856d417b1SBrian Haley 420956d417b1SBrian Haley net = (struct net *)table->extra2; 421056d417b1SBrian Haley 421156d417b1SBrian Haley if (p == &net->ipv6.devconf_dflt->disable_ipv6) 421256d417b1SBrian Haley return 0; 421356d417b1SBrian Haley 421488af182eSEric W. Biederman if (!rtnl_trylock()) { 421588af182eSEric W. Biederman /* Restore the original values before restarting */ 421688af182eSEric W. Biederman *p = old; 421756d417b1SBrian Haley return restart_syscall(); 421888af182eSEric W. Biederman } 421956d417b1SBrian Haley 422056d417b1SBrian Haley if (p == &net->ipv6.devconf_all->disable_ipv6) { 422156d417b1SBrian Haley __s32 newf = net->ipv6.devconf_all->disable_ipv6; 422256d417b1SBrian Haley net->ipv6.devconf_dflt->disable_ipv6 = newf; 422356d417b1SBrian Haley addrconf_disable_change(net, newf); 422456d417b1SBrian Haley } else if ((!*p) ^ (!old)) 422556d417b1SBrian Haley dev_disable_change((struct inet6_dev *)table->extra1); 422656d417b1SBrian Haley 422756d417b1SBrian Haley rtnl_unlock(); 422856d417b1SBrian Haley return 0; 422956d417b1SBrian Haley } 423056d417b1SBrian Haley 423156d417b1SBrian Haley static 42328d65af78SAlexey Dobriyan int addrconf_sysctl_disable(ctl_table *ctl, int write, 423356d417b1SBrian Haley void __user *buffer, size_t *lenp, loff_t *ppos) 423456d417b1SBrian Haley { 423556d417b1SBrian Haley int *valp = ctl->data; 423656d417b1SBrian Haley int val = *valp; 423788af182eSEric W. Biederman loff_t pos = *ppos; 423856d417b1SBrian Haley int ret; 423956d417b1SBrian Haley 42408d65af78SAlexey Dobriyan ret = proc_dointvec(ctl, write, buffer, lenp, ppos); 424156d417b1SBrian Haley 424256d417b1SBrian Haley if (write) 424356d417b1SBrian Haley ret = addrconf_disable_ipv6(ctl, valp, val); 424488af182eSEric W. Biederman if (ret) 424588af182eSEric W. Biederman *ppos = pos; 424656d417b1SBrian Haley return ret; 424756d417b1SBrian Haley } 424856d417b1SBrian Haley 42491da177e4SLinus Torvalds static struct addrconf_sysctl_table 42501da177e4SLinus Torvalds { 42511da177e4SLinus Torvalds struct ctl_table_header *sysctl_header; 4252f6a07b29SYOSHIFUJI Hideaki ctl_table addrconf_vars[DEVCONF_MAX+1]; 42531dab6222SPavel Emelyanov char *dev_name; 4254ab32ea5dSBrian Haley } addrconf_sysctl __read_mostly = { 42551da177e4SLinus Torvalds .sysctl_header = NULL, 42561da177e4SLinus Torvalds .addrconf_vars = { 42571da177e4SLinus Torvalds { 42581da177e4SLinus Torvalds .procname = "forwarding", 42591da177e4SLinus Torvalds .data = &ipv6_devconf.forwarding, 42601da177e4SLinus Torvalds .maxlen = sizeof(int), 42611da177e4SLinus Torvalds .mode = 0644, 42626d9f239aSAlexey Dobriyan .proc_handler = addrconf_sysctl_forward, 42631da177e4SLinus Torvalds }, 42641da177e4SLinus Torvalds { 42651da177e4SLinus Torvalds .procname = "hop_limit", 42661da177e4SLinus Torvalds .data = &ipv6_devconf.hop_limit, 42671da177e4SLinus Torvalds .maxlen = sizeof(int), 42681da177e4SLinus Torvalds .mode = 0644, 42691da177e4SLinus Torvalds .proc_handler = proc_dointvec, 42701da177e4SLinus Torvalds }, 42711da177e4SLinus Torvalds { 42721da177e4SLinus Torvalds .procname = "mtu", 42731da177e4SLinus Torvalds .data = &ipv6_devconf.mtu6, 42741da177e4SLinus Torvalds .maxlen = sizeof(int), 42751da177e4SLinus Torvalds .mode = 0644, 42766d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 42771da177e4SLinus Torvalds }, 42781da177e4SLinus Torvalds { 42791da177e4SLinus Torvalds .procname = "accept_ra", 42801da177e4SLinus Torvalds .data = &ipv6_devconf.accept_ra, 42811da177e4SLinus Torvalds .maxlen = sizeof(int), 42821da177e4SLinus Torvalds .mode = 0644, 42836d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 42841da177e4SLinus Torvalds }, 42851da177e4SLinus Torvalds { 42861da177e4SLinus Torvalds .procname = "accept_redirects", 42871da177e4SLinus Torvalds .data = &ipv6_devconf.accept_redirects, 42881da177e4SLinus Torvalds .maxlen = sizeof(int), 42891da177e4SLinus Torvalds .mode = 0644, 42906d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 42911da177e4SLinus Torvalds }, 42921da177e4SLinus Torvalds { 42931da177e4SLinus Torvalds .procname = "autoconf", 42941da177e4SLinus Torvalds .data = &ipv6_devconf.autoconf, 42951da177e4SLinus Torvalds .maxlen = sizeof(int), 42961da177e4SLinus Torvalds .mode = 0644, 42976d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 42981da177e4SLinus Torvalds }, 42991da177e4SLinus Torvalds { 43001da177e4SLinus Torvalds .procname = "dad_transmits", 43011da177e4SLinus Torvalds .data = &ipv6_devconf.dad_transmits, 43021da177e4SLinus Torvalds .maxlen = sizeof(int), 43031da177e4SLinus Torvalds .mode = 0644, 43046d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 43051da177e4SLinus Torvalds }, 43061da177e4SLinus Torvalds { 43071da177e4SLinus Torvalds .procname = "router_solicitations", 43081da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicits, 43091da177e4SLinus Torvalds .maxlen = sizeof(int), 43101da177e4SLinus Torvalds .mode = 0644, 43116d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 43121da177e4SLinus Torvalds }, 43131da177e4SLinus Torvalds { 43141da177e4SLinus Torvalds .procname = "router_solicitation_interval", 43151da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_interval, 43161da177e4SLinus Torvalds .maxlen = sizeof(int), 43171da177e4SLinus Torvalds .mode = 0644, 43186d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 43191da177e4SLinus Torvalds }, 43201da177e4SLinus Torvalds { 43211da177e4SLinus Torvalds .procname = "router_solicitation_delay", 43221da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_delay, 43231da177e4SLinus Torvalds .maxlen = sizeof(int), 43241da177e4SLinus Torvalds .mode = 0644, 43256d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 43261da177e4SLinus Torvalds }, 43271da177e4SLinus Torvalds { 43281da177e4SLinus Torvalds .procname = "force_mld_version", 43291da177e4SLinus Torvalds .data = &ipv6_devconf.force_mld_version, 43301da177e4SLinus Torvalds .maxlen = sizeof(int), 43311da177e4SLinus Torvalds .mode = 0644, 43326d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 43331da177e4SLinus Torvalds }, 43341da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 43351da177e4SLinus Torvalds { 43361da177e4SLinus Torvalds .procname = "use_tempaddr", 43371da177e4SLinus Torvalds .data = &ipv6_devconf.use_tempaddr, 43381da177e4SLinus Torvalds .maxlen = sizeof(int), 43391da177e4SLinus Torvalds .mode = 0644, 43406d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 43411da177e4SLinus Torvalds }, 43421da177e4SLinus Torvalds { 43431da177e4SLinus Torvalds .procname = "temp_valid_lft", 43441da177e4SLinus Torvalds .data = &ipv6_devconf.temp_valid_lft, 43451da177e4SLinus Torvalds .maxlen = sizeof(int), 43461da177e4SLinus Torvalds .mode = 0644, 43476d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 43481da177e4SLinus Torvalds }, 43491da177e4SLinus Torvalds { 43501da177e4SLinus Torvalds .procname = "temp_prefered_lft", 43511da177e4SLinus Torvalds .data = &ipv6_devconf.temp_prefered_lft, 43521da177e4SLinus Torvalds .maxlen = sizeof(int), 43531da177e4SLinus Torvalds .mode = 0644, 43546d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 43551da177e4SLinus Torvalds }, 43561da177e4SLinus Torvalds { 43571da177e4SLinus Torvalds .procname = "regen_max_retry", 43581da177e4SLinus Torvalds .data = &ipv6_devconf.regen_max_retry, 43591da177e4SLinus Torvalds .maxlen = sizeof(int), 43601da177e4SLinus Torvalds .mode = 0644, 43616d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 43621da177e4SLinus Torvalds }, 43631da177e4SLinus Torvalds { 43641da177e4SLinus Torvalds .procname = "max_desync_factor", 43651da177e4SLinus Torvalds .data = &ipv6_devconf.max_desync_factor, 43661da177e4SLinus Torvalds .maxlen = sizeof(int), 43671da177e4SLinus Torvalds .mode = 0644, 43686d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 43691da177e4SLinus Torvalds }, 43701da177e4SLinus Torvalds #endif 43711da177e4SLinus Torvalds { 43721da177e4SLinus Torvalds .procname = "max_addresses", 43731da177e4SLinus Torvalds .data = &ipv6_devconf.max_addresses, 43741da177e4SLinus Torvalds .maxlen = sizeof(int), 43751da177e4SLinus Torvalds .mode = 0644, 43766d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 43771da177e4SLinus Torvalds }, 43781da177e4SLinus Torvalds { 437965f5c7c1SYOSHIFUJI Hideaki .procname = "accept_ra_defrtr", 438065f5c7c1SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_defrtr, 438165f5c7c1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 438265f5c7c1SYOSHIFUJI Hideaki .mode = 0644, 43836d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 438465f5c7c1SYOSHIFUJI Hideaki }, 438565f5c7c1SYOSHIFUJI Hideaki { 4386c4fd30ebSYOSHIFUJI Hideaki .procname = "accept_ra_pinfo", 4387c4fd30ebSYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_pinfo, 4388c4fd30ebSYOSHIFUJI Hideaki .maxlen = sizeof(int), 4389c4fd30ebSYOSHIFUJI Hideaki .mode = 0644, 43906d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 4391c4fd30ebSYOSHIFUJI Hideaki }, 4392930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 4393930d6ff2SYOSHIFUJI Hideaki { 4394930d6ff2SYOSHIFUJI Hideaki .procname = "accept_ra_rtr_pref", 4395930d6ff2SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rtr_pref, 4396930d6ff2SYOSHIFUJI Hideaki .maxlen = sizeof(int), 4397930d6ff2SYOSHIFUJI Hideaki .mode = 0644, 43986d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 4399930d6ff2SYOSHIFUJI Hideaki }, 440052e16356SYOSHIFUJI Hideaki { 440152e16356SYOSHIFUJI Hideaki .procname = "router_probe_interval", 440252e16356SYOSHIFUJI Hideaki .data = &ipv6_devconf.rtr_probe_interval, 440352e16356SYOSHIFUJI Hideaki .maxlen = sizeof(int), 440452e16356SYOSHIFUJI Hideaki .mode = 0644, 44056d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 440652e16356SYOSHIFUJI Hideaki }, 4407fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 440809c884d4SYOSHIFUJI Hideaki { 440909c884d4SYOSHIFUJI Hideaki .procname = "accept_ra_rt_info_max_plen", 441009c884d4SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rt_info_max_plen, 441109c884d4SYOSHIFUJI Hideaki .maxlen = sizeof(int), 441209c884d4SYOSHIFUJI Hideaki .mode = 0644, 44136d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 441409c884d4SYOSHIFUJI Hideaki }, 441509c884d4SYOSHIFUJI Hideaki #endif 4416930d6ff2SYOSHIFUJI Hideaki #endif 4417c4fd30ebSYOSHIFUJI Hideaki { 4418fbea49e1SYOSHIFUJI Hideaki .procname = "proxy_ndp", 4419fbea49e1SYOSHIFUJI Hideaki .data = &ipv6_devconf.proxy_ndp, 4420fbea49e1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 4421fbea49e1SYOSHIFUJI Hideaki .mode = 0644, 44226d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 4423fbea49e1SYOSHIFUJI Hideaki }, 4424fbea49e1SYOSHIFUJI Hideaki { 44250bcbc926SYOSHIFUJI Hideaki .procname = "accept_source_route", 44260bcbc926SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_source_route, 44270bcbc926SYOSHIFUJI Hideaki .maxlen = sizeof(int), 44280bcbc926SYOSHIFUJI Hideaki .mode = 0644, 44296d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 44300bcbc926SYOSHIFUJI Hideaki }, 443195c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 443295c385b4SNeil Horman { 443395c385b4SNeil Horman .procname = "optimistic_dad", 443495c385b4SNeil Horman .data = &ipv6_devconf.optimistic_dad, 443595c385b4SNeil Horman .maxlen = sizeof(int), 443695c385b4SNeil Horman .mode = 0644, 44376d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 443895c385b4SNeil Horman 443995c385b4SNeil Horman }, 444095c385b4SNeil Horman #endif 44417bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 44427bc570c8SYOSHIFUJI Hideaki { 44437bc570c8SYOSHIFUJI Hideaki .procname = "mc_forwarding", 44447bc570c8SYOSHIFUJI Hideaki .data = &ipv6_devconf.mc_forwarding, 44457bc570c8SYOSHIFUJI Hideaki .maxlen = sizeof(int), 4446a4e6db07SDavid S. Miller .mode = 0444, 44476d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 44487bc570c8SYOSHIFUJI Hideaki }, 44497bc570c8SYOSHIFUJI Hideaki #endif 44500bcbc926SYOSHIFUJI Hideaki { 4451778d80beSYOSHIFUJI Hideaki .procname = "disable_ipv6", 4452778d80beSYOSHIFUJI Hideaki .data = &ipv6_devconf.disable_ipv6, 4453778d80beSYOSHIFUJI Hideaki .maxlen = sizeof(int), 4454778d80beSYOSHIFUJI Hideaki .mode = 0644, 445556d417b1SBrian Haley .proc_handler = addrconf_sysctl_disable, 4456778d80beSYOSHIFUJI Hideaki }, 4457778d80beSYOSHIFUJI Hideaki { 44581b34be74SYOSHIFUJI Hideaki .procname = "accept_dad", 44591b34be74SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_dad, 44601b34be74SYOSHIFUJI Hideaki .maxlen = sizeof(int), 44611b34be74SYOSHIFUJI Hideaki .mode = 0644, 44626d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 44631b34be74SYOSHIFUJI Hideaki }, 44641b34be74SYOSHIFUJI Hideaki { 4465f7734fdfSOctavian Purdila .procname = "force_tllao", 4466f7734fdfSOctavian Purdila .data = &ipv6_devconf.force_tllao, 4467f7734fdfSOctavian Purdila .maxlen = sizeof(int), 4468f7734fdfSOctavian Purdila .mode = 0644, 4469f7734fdfSOctavian Purdila .proc_handler = proc_dointvec 4470f7734fdfSOctavian Purdila }, 4471f7734fdfSOctavian Purdila { 4472f8572d8fSEric W. Biederman /* sentinel */ 44731da177e4SLinus Torvalds } 44741da177e4SLinus Torvalds }, 44751da177e4SLinus Torvalds }; 44761da177e4SLinus Torvalds 4477bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name, 4478f8572d8fSEric W. Biederman struct inet6_dev *idev, struct ipv6_devconf *p) 44791da177e4SLinus Torvalds { 44801da177e4SLinus Torvalds int i; 44811da177e4SLinus Torvalds struct addrconf_sysctl_table *t; 44821da177e4SLinus Torvalds 44831dab6222SPavel Emelyanov #define ADDRCONF_CTL_PATH_DEV 3 44841dab6222SPavel Emelyanov 44851dab6222SPavel Emelyanov struct ctl_path addrconf_ctl_path[] = { 4486f8572d8fSEric W. Biederman { .procname = "net", }, 4487f8572d8fSEric W. Biederman { .procname = "ipv6", }, 4488f8572d8fSEric W. Biederman { .procname = "conf", }, 44891dab6222SPavel Emelyanov { /* to be set */ }, 44901dab6222SPavel Emelyanov { }, 44911dab6222SPavel Emelyanov }; 44921dab6222SPavel Emelyanov 44931dab6222SPavel Emelyanov 4494af879cc7SArnaldo Carvalho de Melo t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL); 44951da177e4SLinus Torvalds if (t == NULL) 4496f68635e6SPavel Emelyanov goto out; 4497f68635e6SPavel Emelyanov 44981da177e4SLinus Torvalds for (i = 0; t->addrconf_vars[i].data; i++) { 44991da177e4SLinus Torvalds t->addrconf_vars[i].data += (char *)p - (char *)&ipv6_devconf; 45001da177e4SLinus Torvalds t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */ 4501bff16c2fSPavel Emelyanov t->addrconf_vars[i].extra2 = net; 45021da177e4SLinus Torvalds } 45031da177e4SLinus Torvalds 45041da177e4SLinus Torvalds /* 45051da177e4SLinus Torvalds * Make a copy of dev_name, because '.procname' is regarded as const 45061da177e4SLinus Torvalds * by sysctl and we wouldn't want anyone to change it under our feet 45071da177e4SLinus Torvalds * (see SIOCSIFNAME). 45081da177e4SLinus Torvalds */ 45091dab6222SPavel Emelyanov t->dev_name = kstrdup(dev_name, GFP_KERNEL); 45101dab6222SPavel Emelyanov if (!t->dev_name) 45111da177e4SLinus Torvalds goto free; 45121da177e4SLinus Torvalds 45131dab6222SPavel Emelyanov addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].procname = t->dev_name; 45141da177e4SLinus Torvalds 4515bff16c2fSPavel Emelyanov t->sysctl_header = register_net_sysctl_table(net, addrconf_ctl_path, 45161dab6222SPavel Emelyanov t->addrconf_vars); 45171da177e4SLinus Torvalds if (t->sysctl_header == NULL) 45181da177e4SLinus Torvalds goto free_procname; 4519f68635e6SPavel Emelyanov 45201da177e4SLinus Torvalds p->sysctl = t; 452195897312SPavel Emelyanov return 0; 45221da177e4SLinus Torvalds 45231da177e4SLinus Torvalds free_procname: 45241dab6222SPavel Emelyanov kfree(t->dev_name); 45251da177e4SLinus Torvalds free: 45261da177e4SLinus Torvalds kfree(t); 4527f68635e6SPavel Emelyanov out: 452895897312SPavel Emelyanov return -ENOBUFS; 45291da177e4SLinus Torvalds } 45301da177e4SLinus Torvalds 4531408c4768SPavel Emelyanov static void __addrconf_sysctl_unregister(struct ipv6_devconf *p) 4532f52295a9SPavel Emelyanov { 4533408c4768SPavel Emelyanov struct addrconf_sysctl_table *t; 4534f52295a9SPavel Emelyanov 4535408c4768SPavel Emelyanov if (p->sysctl == NULL) 4536408c4768SPavel Emelyanov return; 4537408c4768SPavel Emelyanov 4538408c4768SPavel Emelyanov t = p->sysctl; 45391da177e4SLinus Torvalds p->sysctl = NULL; 4540*ff538818SLucian Adrian Grijincu unregister_net_sysctl_table(t->sysctl_header); 45411dab6222SPavel Emelyanov kfree(t->dev_name); 45421da177e4SLinus Torvalds kfree(t); 45431da177e4SLinus Torvalds } 4544408c4768SPavel Emelyanov 4545408c4768SPavel Emelyanov static void addrconf_sysctl_register(struct inet6_dev *idev) 4546408c4768SPavel Emelyanov { 454754716e3bSEric W. Biederman neigh_sysctl_register(idev->dev, idev->nd_parms, "ipv6", 4548f8572d8fSEric W. Biederman &ndisc_ifinfo_sysctl_change); 4549c346dca1SYOSHIFUJI Hideaki __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name, 4550f8572d8fSEric W. Biederman idev, &idev->cnf); 4551408c4768SPavel Emelyanov } 4552408c4768SPavel Emelyanov 4553408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev) 4554408c4768SPavel Emelyanov { 4555408c4768SPavel Emelyanov __addrconf_sysctl_unregister(&idev->cnf); 4556408c4768SPavel Emelyanov neigh_sysctl_unregister(idev->nd_parms); 45571da177e4SLinus Torvalds } 45581da177e4SLinus Torvalds 45591da177e4SLinus Torvalds 45601da177e4SLinus Torvalds #endif 45611da177e4SLinus Torvalds 45622c8c1e72SAlexey Dobriyan static int __net_init addrconf_init_net(struct net *net) 4563e0da5a48SPavel Emelyanov { 4564e0da5a48SPavel Emelyanov int err; 4565e0da5a48SPavel Emelyanov struct ipv6_devconf *all, *dflt; 4566e0da5a48SPavel Emelyanov 4567e0da5a48SPavel Emelyanov err = -ENOMEM; 4568e0da5a48SPavel Emelyanov all = &ipv6_devconf; 4569e0da5a48SPavel Emelyanov dflt = &ipv6_devconf_dflt; 4570e0da5a48SPavel Emelyanov 457109ad9bc7SOctavian Purdila if (!net_eq(net, &init_net)) { 4572e0da5a48SPavel Emelyanov all = kmemdup(all, sizeof(ipv6_devconf), GFP_KERNEL); 4573e0da5a48SPavel Emelyanov if (all == NULL) 4574e0da5a48SPavel Emelyanov goto err_alloc_all; 4575e0da5a48SPavel Emelyanov 4576e0da5a48SPavel Emelyanov dflt = kmemdup(dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL); 4577e0da5a48SPavel Emelyanov if (dflt == NULL) 4578e0da5a48SPavel Emelyanov goto err_alloc_dflt; 457956d417b1SBrian Haley } else { 458056d417b1SBrian Haley /* these will be inherited by all namespaces */ 458156d417b1SBrian Haley dflt->autoconf = ipv6_defaults.autoconf; 458256d417b1SBrian Haley dflt->disable_ipv6 = ipv6_defaults.disable_ipv6; 4583e0da5a48SPavel Emelyanov } 4584e0da5a48SPavel Emelyanov 4585e0da5a48SPavel Emelyanov net->ipv6.devconf_all = all; 4586e0da5a48SPavel Emelyanov net->ipv6.devconf_dflt = dflt; 4587e0da5a48SPavel Emelyanov 4588e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 4589f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "all", NULL, all); 4590e0da5a48SPavel Emelyanov if (err < 0) 4591e0da5a48SPavel Emelyanov goto err_reg_all; 4592e0da5a48SPavel Emelyanov 4593f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "default", NULL, dflt); 4594e0da5a48SPavel Emelyanov if (err < 0) 4595e0da5a48SPavel Emelyanov goto err_reg_dflt; 4596e0da5a48SPavel Emelyanov #endif 4597e0da5a48SPavel Emelyanov return 0; 4598e0da5a48SPavel Emelyanov 4599e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 4600e0da5a48SPavel Emelyanov err_reg_dflt: 4601e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(all); 4602e0da5a48SPavel Emelyanov err_reg_all: 4603e0da5a48SPavel Emelyanov kfree(dflt); 4604e0da5a48SPavel Emelyanov #endif 4605e0da5a48SPavel Emelyanov err_alloc_dflt: 4606e0da5a48SPavel Emelyanov kfree(all); 4607e0da5a48SPavel Emelyanov err_alloc_all: 4608e0da5a48SPavel Emelyanov return err; 4609e0da5a48SPavel Emelyanov } 4610e0da5a48SPavel Emelyanov 46112c8c1e72SAlexey Dobriyan static void __net_exit addrconf_exit_net(struct net *net) 4612e0da5a48SPavel Emelyanov { 4613e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 4614e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(net->ipv6.devconf_dflt); 4615e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(net->ipv6.devconf_all); 4616e0da5a48SPavel Emelyanov #endif 461709ad9bc7SOctavian Purdila if (!net_eq(net, &init_net)) { 4618e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_dflt); 4619e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_all); 4620e0da5a48SPavel Emelyanov } 4621e0da5a48SPavel Emelyanov } 4622e0da5a48SPavel Emelyanov 4623e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = { 4624e0da5a48SPavel Emelyanov .init = addrconf_init_net, 4625e0da5a48SPavel Emelyanov .exit = addrconf_exit_net, 4626e0da5a48SPavel Emelyanov }; 4627e0da5a48SPavel Emelyanov 46281da177e4SLinus Torvalds /* 46291da177e4SLinus Torvalds * Device notifier 46301da177e4SLinus Torvalds */ 46311da177e4SLinus Torvalds 46321da177e4SLinus Torvalds int register_inet6addr_notifier(struct notifier_block *nb) 46331da177e4SLinus Torvalds { 4634e041c683SAlan Stern return atomic_notifier_chain_register(&inet6addr_chain, nb); 46351da177e4SLinus Torvalds } 46367159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(register_inet6addr_notifier); 46377159039aSYOSHIFUJI Hideaki 46381da177e4SLinus Torvalds int unregister_inet6addr_notifier(struct notifier_block *nb) 46391da177e4SLinus Torvalds { 4640e041c683SAlan Stern return atomic_notifier_chain_unregister(&inet6addr_chain, nb); 46411da177e4SLinus Torvalds } 46427159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(unregister_inet6addr_notifier); 46437159039aSYOSHIFUJI Hideaki 4644b382b191SThomas Graf static struct rtnl_af_ops inet6_ops = { 4645b382b191SThomas Graf .family = AF_INET6, 4646b382b191SThomas Graf .fill_link_af = inet6_fill_link_af, 4647b382b191SThomas Graf .get_link_af_size = inet6_get_link_af_size, 4648b382b191SThomas Graf }; 4649b382b191SThomas Graf 46501da177e4SLinus Torvalds /* 46511da177e4SLinus Torvalds * Init / cleanup code 46521da177e4SLinus Torvalds */ 46531da177e4SLinus Torvalds 46541da177e4SLinus Torvalds int __init addrconf_init(void) 46551da177e4SLinus Torvalds { 4656c2e21293Sstephen hemminger int i, err; 46572a8cc6c8SYOSHIFUJI Hideaki 4658e21e8467SStephen Hemminger err = ipv6_addr_label_init(); 4659e21e8467SStephen Hemminger if (err < 0) { 4660e21e8467SStephen Hemminger printk(KERN_CRIT "IPv6 Addrconf:" 4661e21e8467SStephen Hemminger " cannot initialize default policy table: %d.\n", err); 46622cc6d2bfSNeil Horman goto out; 46632a8cc6c8SYOSHIFUJI Hideaki } 46641da177e4SLinus Torvalds 46652cc6d2bfSNeil Horman err = register_pernet_subsys(&addrconf_ops); 46662cc6d2bfSNeil Horman if (err < 0) 46672cc6d2bfSNeil Horman goto out_addrlabel; 4668e0da5a48SPavel Emelyanov 46691da177e4SLinus Torvalds /* The addrconf netdev notifier requires that loopback_dev 46701da177e4SLinus Torvalds * has it's ipv6 private information allocated and setup 46711da177e4SLinus Torvalds * before it can bring up and give link-local addresses 46721da177e4SLinus Torvalds * to other devices which are up. 46731da177e4SLinus Torvalds * 46741da177e4SLinus Torvalds * Unfortunately, loopback_dev is not necessarily the first 46751da177e4SLinus Torvalds * entry in the global dev_base list of net devices. In fact, 46761da177e4SLinus Torvalds * it is likely to be the very last entry on that list. 46771da177e4SLinus Torvalds * So this causes the notifier registry below to try and 46781da177e4SLinus Torvalds * give link-local addresses to all devices besides loopback_dev 46791da177e4SLinus Torvalds * first, then loopback_dev, which cases all the non-loopback_dev 46801da177e4SLinus Torvalds * devices to fail to get a link-local address. 46811da177e4SLinus Torvalds * 46821da177e4SLinus Torvalds * So, as a temporary fix, allocate the ipv6 structure for 46831da177e4SLinus Torvalds * loopback_dev first by hand. 46841da177e4SLinus Torvalds * Longer term, all of the dependencies ipv6 has upon the loopback 46851da177e4SLinus Torvalds * device and it being up should be removed. 46861da177e4SLinus Torvalds */ 46871da177e4SLinus Torvalds rtnl_lock(); 46882774c7abSEric W. Biederman if (!ipv6_add_dev(init_net.loopback_dev)) 46891da177e4SLinus Torvalds err = -ENOMEM; 46901da177e4SLinus Torvalds rtnl_unlock(); 46911da177e4SLinus Torvalds if (err) 4692e0da5a48SPavel Emelyanov goto errlo; 46931da177e4SLinus Torvalds 4694c2e21293Sstephen hemminger for (i = 0; i < IN6_ADDR_HSIZE; i++) 4695c2e21293Sstephen hemminger INIT_HLIST_HEAD(&inet6_addr_lst[i]); 4696c2e21293Sstephen hemminger 46971da177e4SLinus Torvalds register_netdevice_notifier(&ipv6_dev_notf); 46981da177e4SLinus Torvalds 46991da177e4SLinus Torvalds addrconf_verify(0); 4700c127ea2cSThomas Graf 4701b382b191SThomas Graf err = rtnl_af_register(&inet6_ops); 4702b382b191SThomas Graf if (err < 0) 4703b382b191SThomas Graf goto errout_af; 4704b382b191SThomas Graf 4705c127ea2cSThomas Graf err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo); 4706c127ea2cSThomas Graf if (err < 0) 4707c127ea2cSThomas Graf goto errout; 4708c127ea2cSThomas Graf 4709c127ea2cSThomas Graf /* Only the first call to __rtnl_register can fail */ 4710c127ea2cSThomas Graf __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL); 4711c127ea2cSThomas Graf __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL); 4712c127ea2cSThomas Graf __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr, inet6_dump_ifaddr); 4713c127ea2cSThomas Graf __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL, inet6_dump_ifmcaddr); 4714c127ea2cSThomas Graf __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL, inet6_dump_ifacaddr); 4715c127ea2cSThomas Graf 47162a8cc6c8SYOSHIFUJI Hideaki ipv6_addr_label_rtnl_register(); 47172a8cc6c8SYOSHIFUJI Hideaki 47181da177e4SLinus Torvalds return 0; 4719c127ea2cSThomas Graf errout: 4720b382b191SThomas Graf rtnl_af_unregister(&inet6_ops); 4721b382b191SThomas Graf errout_af: 4722c127ea2cSThomas Graf unregister_netdevice_notifier(&ipv6_dev_notf); 4723e0da5a48SPavel Emelyanov errlo: 4724e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 47252cc6d2bfSNeil Horman out_addrlabel: 47262cc6d2bfSNeil Horman ipv6_addr_label_cleanup(); 47272cc6d2bfSNeil Horman out: 4728c127ea2cSThomas Graf return err; 47291da177e4SLinus Torvalds } 47301da177e4SLinus Torvalds 473109f7709fSDaniel Lezcano void addrconf_cleanup(void) 47321da177e4SLinus Torvalds { 4733176c39afSDaniel Lezcano struct net_device *dev; 47341da177e4SLinus Torvalds int i; 47351da177e4SLinus Torvalds 47361da177e4SLinus Torvalds unregister_netdevice_notifier(&ipv6_dev_notf); 4737e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 47382cc6d2bfSNeil Horman ipv6_addr_label_cleanup(); 47391da177e4SLinus Torvalds 47401da177e4SLinus Torvalds rtnl_lock(); 47411da177e4SLinus Torvalds 4742b382b191SThomas Graf __rtnl_af_unregister(&inet6_ops); 4743b382b191SThomas Graf 4744176c39afSDaniel Lezcano /* clean dev list */ 4745176c39afSDaniel Lezcano for_each_netdev(&init_net, dev) { 4746176c39afSDaniel Lezcano if (__in6_dev_get(dev) == NULL) 4747176c39afSDaniel Lezcano continue; 4748176c39afSDaniel Lezcano addrconf_ifdown(dev, 1); 4749176c39afSDaniel Lezcano } 4750176c39afSDaniel Lezcano addrconf_ifdown(init_net.loopback_dev, 2); 4751176c39afSDaniel Lezcano 47521da177e4SLinus Torvalds /* 47531da177e4SLinus Torvalds * Check hash table. 47541da177e4SLinus Torvalds */ 47555c578aedSstephen hemminger spin_lock_bh(&addrconf_hash_lock); 4756c2e21293Sstephen hemminger for (i = 0; i < IN6_ADDR_HSIZE; i++) 4757c2e21293Sstephen hemminger WARN_ON(!hlist_empty(&inet6_addr_lst[i])); 47585c578aedSstephen hemminger spin_unlock_bh(&addrconf_hash_lock); 47591da177e4SLinus Torvalds 47601da177e4SLinus Torvalds del_timer(&addr_chk_timer); 47611da177e4SLinus Torvalds rtnl_unlock(); 47621da177e4SLinus Torvalds } 4763