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 * $Id: addrconf.c,v 1.69 2001/10/31 21:55:54 davem Exp $ 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 121da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 131da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 141da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 151da177e4SLinus Torvalds */ 161da177e4SLinus Torvalds 171da177e4SLinus Torvalds /* 181da177e4SLinus Torvalds * Changes: 191da177e4SLinus Torvalds * 201da177e4SLinus Torvalds * Janos Farkas : delete timer on ifdown 211da177e4SLinus Torvalds * <chexum@bankinf.banki.hu> 221da177e4SLinus Torvalds * Andi Kleen : kill double kfree on module 231da177e4SLinus Torvalds * unload. 241da177e4SLinus Torvalds * Maciej W. Rozycki : FDDI support 251da177e4SLinus Torvalds * sekiya@USAGI : Don't send too many RS 261da177e4SLinus Torvalds * packets. 271da177e4SLinus Torvalds * yoshfuji@USAGI : Fixed interval between DAD 281da177e4SLinus Torvalds * packets. 291da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI : improved accuracy of 301da177e4SLinus Torvalds * address validation timer. 311da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041) 321da177e4SLinus Torvalds * support. 331da177e4SLinus Torvalds * Yuji SEKIYA @USAGI : Don't assign a same IPv6 341da177e4SLinus Torvalds * address on a same interface. 351da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI : ARCnet support 361da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to 371da177e4SLinus Torvalds * seq_file. 38b1cacb68SYOSHIFUJI Hideaki * YOSHIFUJI Hideaki @USAGI : improved source address 39b1cacb68SYOSHIFUJI Hideaki * selection; consider scope, 40b1cacb68SYOSHIFUJI Hideaki * status etc. 411da177e4SLinus Torvalds */ 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds #include <linux/errno.h> 441da177e4SLinus Torvalds #include <linux/types.h> 451da177e4SLinus Torvalds #include <linux/socket.h> 461da177e4SLinus Torvalds #include <linux/sockios.h> 471da177e4SLinus Torvalds #include <linux/sched.h> 481da177e4SLinus Torvalds #include <linux/net.h> 491da177e4SLinus Torvalds #include <linux/in6.h> 501da177e4SLinus Torvalds #include <linux/netdevice.h> 511823730fSThomas Graf #include <linux/if_addr.h> 521da177e4SLinus Torvalds #include <linux/if_arp.h> 531da177e4SLinus Torvalds #include <linux/if_arcnet.h> 541da177e4SLinus Torvalds #include <linux/if_infiniband.h> 551da177e4SLinus Torvalds #include <linux/route.h> 561da177e4SLinus Torvalds #include <linux/inetdevice.h> 571da177e4SLinus Torvalds #include <linux/init.h> 581da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 591da177e4SLinus Torvalds #include <linux/sysctl.h> 601da177e4SLinus Torvalds #endif 614fc268d2SRandy Dunlap #include <linux/capability.h> 621da177e4SLinus Torvalds #include <linux/delay.h> 631da177e4SLinus Torvalds #include <linux/notifier.h> 64543537bdSPaulo Marques #include <linux/string.h> 651da177e4SLinus Torvalds 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> 771da177e4SLinus Torvalds #include <linux/if_tunnel.h> 781da177e4SLinus Torvalds #include <linux/rtnetlink.h> 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 811da177e4SLinus Torvalds #include <linux/random.h> 821da177e4SLinus Torvalds #endif 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds #include <asm/uaccess.h> 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds #include <linux/proc_fs.h> 871da177e4SLinus Torvalds #include <linux/seq_file.h> 881da177e4SLinus Torvalds 891da177e4SLinus Torvalds /* Set to 3 to get tracing... */ 901da177e4SLinus Torvalds #define ACONF_DEBUG 2 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds #if ACONF_DEBUG >= 3 931da177e4SLinus Torvalds #define ADBG(x) printk x 941da177e4SLinus Torvalds #else 951da177e4SLinus Torvalds #define ADBG(x) 961da177e4SLinus Torvalds #endif 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds #define INFINITY_LIFE_TIME 0xFFFFFFFF 991da177e4SLinus Torvalds #define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b))) 1001da177e4SLinus Torvalds 1011da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 1021da177e4SLinus Torvalds static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p); 1031da177e4SLinus Torvalds static void addrconf_sysctl_unregister(struct ipv6_devconf *p); 1041da177e4SLinus Torvalds #endif 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 1071da177e4SLinus Torvalds static int __ipv6_regen_rndid(struct inet6_dev *idev); 1081da177e4SLinus Torvalds static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr); 1091da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data); 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds static int desync_factor = MAX_DESYNC_FACTOR * HZ; 1121da177e4SLinus Torvalds #endif 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev); 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds /* 1171da177e4SLinus Torvalds * Configured unicast address hash table 1181da177e4SLinus Torvalds */ 1191da177e4SLinus Torvalds static struct inet6_ifaddr *inet6_addr_lst[IN6_ADDR_HSIZE]; 1201da177e4SLinus Torvalds static DEFINE_RWLOCK(addrconf_hash_lock); 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds /* Protects inet6 devices */ 1231da177e4SLinus Torvalds DEFINE_RWLOCK(addrconf_lock); 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds static void addrconf_verify(unsigned long); 1261da177e4SLinus Torvalds 1278d06afabSIngo Molnar static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0); 1281da177e4SLinus Torvalds static DEFINE_SPINLOCK(addrconf_verify_lock); 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds static void addrconf_join_anycast(struct inet6_ifaddr *ifp); 1311da177e4SLinus Torvalds static void addrconf_leave_anycast(struct inet6_ifaddr *ifp); 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how); 1341da177e4SLinus Torvalds 135e431b8c0SJamal Hadi Salim static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags); 1361da177e4SLinus Torvalds static void addrconf_dad_timer(unsigned long data); 1371da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp); 138c5e33bddSYOSHIFUJI Hideaki static void addrconf_dad_run(struct inet6_dev *idev); 1391da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data); 1401da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); 1411da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev, 1441da177e4SLinus Torvalds struct prefix_info *pinfo); 1451da177e4SLinus Torvalds static int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev); 1461da177e4SLinus Torvalds 147e041c683SAlan Stern static ATOMIC_NOTIFIER_HEAD(inet6addr_chain); 1481da177e4SLinus Torvalds 149ab32ea5dSBrian Haley struct ipv6_devconf ipv6_devconf __read_mostly = { 1501da177e4SLinus Torvalds .forwarding = 0, 1511da177e4SLinus Torvalds .hop_limit = IPV6_DEFAULT_HOPLIMIT, 1521da177e4SLinus Torvalds .mtu6 = IPV6_MIN_MTU, 1531da177e4SLinus Torvalds .accept_ra = 1, 1541da177e4SLinus Torvalds .accept_redirects = 1, 1551da177e4SLinus Torvalds .autoconf = 1, 1561da177e4SLinus Torvalds .force_mld_version = 0, 1571da177e4SLinus Torvalds .dad_transmits = 1, 1581da177e4SLinus Torvalds .rtr_solicits = MAX_RTR_SOLICITATIONS, 1591da177e4SLinus Torvalds .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, 1601da177e4SLinus Torvalds .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, 1611da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 1621da177e4SLinus Torvalds .use_tempaddr = 0, 1631da177e4SLinus Torvalds .temp_valid_lft = TEMP_VALID_LIFETIME, 1641da177e4SLinus Torvalds .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, 1651da177e4SLinus Torvalds .regen_max_retry = REGEN_MAX_RETRY, 1661da177e4SLinus Torvalds .max_desync_factor = MAX_DESYNC_FACTOR, 1671da177e4SLinus Torvalds #endif 1681da177e4SLinus Torvalds .max_addresses = IPV6_MAX_ADDRESSES, 16965f5c7c1SYOSHIFUJI Hideaki .accept_ra_defrtr = 1, 170c4fd30ebSYOSHIFUJI Hideaki .accept_ra_pinfo = 1, 171930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 172930d6ff2SYOSHIFUJI Hideaki .accept_ra_rtr_pref = 1, 17352e16356SYOSHIFUJI Hideaki .rtr_probe_interval = 60 * HZ, 17409c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 17509c884d4SYOSHIFUJI Hideaki .accept_ra_rt_info_max_plen = 0, 17609c884d4SYOSHIFUJI Hideaki #endif 177930d6ff2SYOSHIFUJI Hideaki #endif 1781da177e4SLinus Torvalds }; 1791da177e4SLinus Torvalds 180ab32ea5dSBrian Haley static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = { 1811da177e4SLinus Torvalds .forwarding = 0, 1821da177e4SLinus Torvalds .hop_limit = IPV6_DEFAULT_HOPLIMIT, 1831da177e4SLinus Torvalds .mtu6 = IPV6_MIN_MTU, 1841da177e4SLinus Torvalds .accept_ra = 1, 1851da177e4SLinus Torvalds .accept_redirects = 1, 1861da177e4SLinus Torvalds .autoconf = 1, 1871da177e4SLinus Torvalds .dad_transmits = 1, 1881da177e4SLinus Torvalds .rtr_solicits = MAX_RTR_SOLICITATIONS, 1891da177e4SLinus Torvalds .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, 1901da177e4SLinus Torvalds .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, 1911da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 1921da177e4SLinus Torvalds .use_tempaddr = 0, 1931da177e4SLinus Torvalds .temp_valid_lft = TEMP_VALID_LIFETIME, 1941da177e4SLinus Torvalds .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, 1951da177e4SLinus Torvalds .regen_max_retry = REGEN_MAX_RETRY, 1961da177e4SLinus Torvalds .max_desync_factor = MAX_DESYNC_FACTOR, 1971da177e4SLinus Torvalds #endif 1981da177e4SLinus Torvalds .max_addresses = IPV6_MAX_ADDRESSES, 19965f5c7c1SYOSHIFUJI Hideaki .accept_ra_defrtr = 1, 200c4fd30ebSYOSHIFUJI Hideaki .accept_ra_pinfo = 1, 201930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 202930d6ff2SYOSHIFUJI Hideaki .accept_ra_rtr_pref = 1, 20352e16356SYOSHIFUJI Hideaki .rtr_probe_interval = 60 * HZ, 20409c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 20509c884d4SYOSHIFUJI Hideaki .accept_ra_rt_info_max_plen = 0, 20609c884d4SYOSHIFUJI Hideaki #endif 207930d6ff2SYOSHIFUJI Hideaki #endif 2081da177e4SLinus Torvalds }; 2091da177e4SLinus Torvalds 2101da177e4SLinus Torvalds /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */ 2111da177e4SLinus Torvalds #if 0 2121da177e4SLinus Torvalds const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; 2131da177e4SLinus Torvalds #endif 2141da177e4SLinus Torvalds const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT; 2151da177e4SLinus Torvalds 216b1cacb68SYOSHIFUJI Hideaki #define IPV6_ADDR_SCOPE_TYPE(scope) ((scope) << 16) 217b1cacb68SYOSHIFUJI Hideaki 218b1cacb68SYOSHIFUJI Hideaki static inline unsigned ipv6_addr_scope2type(unsigned scope) 2191da177e4SLinus Torvalds { 220b1cacb68SYOSHIFUJI Hideaki switch(scope) { 221b1cacb68SYOSHIFUJI Hideaki case IPV6_ADDR_SCOPE_NODELOCAL: 222b1cacb68SYOSHIFUJI Hideaki return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_NODELOCAL) | 223b1cacb68SYOSHIFUJI Hideaki IPV6_ADDR_LOOPBACK); 224b1cacb68SYOSHIFUJI Hideaki case IPV6_ADDR_SCOPE_LINKLOCAL: 225b1cacb68SYOSHIFUJI Hideaki return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL) | 226b1cacb68SYOSHIFUJI Hideaki IPV6_ADDR_LINKLOCAL); 227b1cacb68SYOSHIFUJI Hideaki case IPV6_ADDR_SCOPE_SITELOCAL: 228b1cacb68SYOSHIFUJI Hideaki return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL) | 229b1cacb68SYOSHIFUJI Hideaki IPV6_ADDR_SITELOCAL); 230b1cacb68SYOSHIFUJI Hideaki } 231b1cacb68SYOSHIFUJI Hideaki return IPV6_ADDR_SCOPE_TYPE(scope); 232b1cacb68SYOSHIFUJI Hideaki } 233b1cacb68SYOSHIFUJI Hideaki 234b1cacb68SYOSHIFUJI Hideaki int __ipv6_addr_type(const struct in6_addr *addr) 235b1cacb68SYOSHIFUJI Hideaki { 2361da177e4SLinus Torvalds u32 st; 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds st = addr->s6_addr32[0]; 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds /* Consider all addresses with the first three bits different of 241b1cacb68SYOSHIFUJI Hideaki 000 and 111 as unicasts. 2421da177e4SLinus Torvalds */ 2431da177e4SLinus Torvalds if ((st & htonl(0xE0000000)) != htonl(0x00000000) && 2441da177e4SLinus Torvalds (st & htonl(0xE0000000)) != htonl(0xE0000000)) 245b1cacb68SYOSHIFUJI Hideaki return (IPV6_ADDR_UNICAST | 246b1cacb68SYOSHIFUJI Hideaki IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); 247b1cacb68SYOSHIFUJI Hideaki 248b1cacb68SYOSHIFUJI Hideaki if ((st & htonl(0xFF000000)) == htonl(0xFF000000)) { 249b1cacb68SYOSHIFUJI Hideaki /* multicast */ 250b1cacb68SYOSHIFUJI Hideaki /* addr-select 3.1 */ 251b1cacb68SYOSHIFUJI Hideaki return (IPV6_ADDR_MULTICAST | 252b1cacb68SYOSHIFUJI Hideaki ipv6_addr_scope2type(IPV6_ADDR_MC_SCOPE(addr))); 253b1cacb68SYOSHIFUJI Hideaki } 2541da177e4SLinus Torvalds 2551da177e4SLinus Torvalds if ((st & htonl(0xFFC00000)) == htonl(0xFE800000)) 256b1cacb68SYOSHIFUJI Hideaki return (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST | 257b1cacb68SYOSHIFUJI Hideaki IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.1 */ 2581da177e4SLinus Torvalds if ((st & htonl(0xFFC00000)) == htonl(0xFEC00000)) 259b1cacb68SYOSHIFUJI Hideaki return (IPV6_ADDR_SITELOCAL | IPV6_ADDR_UNICAST | 260b1cacb68SYOSHIFUJI Hideaki IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL)); /* addr-select 3.1 */ 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds if ((addr->s6_addr32[0] | addr->s6_addr32[1]) == 0) { 2631da177e4SLinus Torvalds if (addr->s6_addr32[2] == 0) { 2641da177e4SLinus Torvalds if (addr->s6_addr32[3] == 0) 2651da177e4SLinus Torvalds return IPV6_ADDR_ANY; 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds if (addr->s6_addr32[3] == htonl(0x00000001)) 268b1cacb68SYOSHIFUJI Hideaki return (IPV6_ADDR_LOOPBACK | IPV6_ADDR_UNICAST | 269b1cacb68SYOSHIFUJI Hideaki IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.4 */ 2701da177e4SLinus Torvalds 271b1cacb68SYOSHIFUJI Hideaki return (IPV6_ADDR_COMPATv4 | IPV6_ADDR_UNICAST | 272b1cacb68SYOSHIFUJI Hideaki IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */ 2731da177e4SLinus Torvalds } 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds if (addr->s6_addr32[2] == htonl(0x0000ffff)) 276b1cacb68SYOSHIFUJI Hideaki return (IPV6_ADDR_MAPPED | 277b1cacb68SYOSHIFUJI Hideaki IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */ 2781da177e4SLinus Torvalds } 2791da177e4SLinus Torvalds 280b1cacb68SYOSHIFUJI Hideaki return (IPV6_ADDR_RESERVED | 281b1cacb68SYOSHIFUJI Hideaki IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.4 */ 2821da177e4SLinus Torvalds } 2831da177e4SLinus Torvalds 2841da177e4SLinus Torvalds static void addrconf_del_timer(struct inet6_ifaddr *ifp) 2851da177e4SLinus Torvalds { 2861da177e4SLinus Torvalds if (del_timer(&ifp->timer)) 2871da177e4SLinus Torvalds __in6_ifa_put(ifp); 2881da177e4SLinus Torvalds } 2891da177e4SLinus Torvalds 2901da177e4SLinus Torvalds enum addrconf_timer_t 2911da177e4SLinus Torvalds { 2921da177e4SLinus Torvalds AC_NONE, 2931da177e4SLinus Torvalds AC_DAD, 2941da177e4SLinus Torvalds AC_RS, 2951da177e4SLinus Torvalds }; 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds static void addrconf_mod_timer(struct inet6_ifaddr *ifp, 2981da177e4SLinus Torvalds enum addrconf_timer_t what, 2991da177e4SLinus Torvalds unsigned long when) 3001da177e4SLinus Torvalds { 3011da177e4SLinus Torvalds if (!del_timer(&ifp->timer)) 3021da177e4SLinus Torvalds in6_ifa_hold(ifp); 3031da177e4SLinus Torvalds 3041da177e4SLinus Torvalds switch (what) { 3051da177e4SLinus Torvalds case AC_DAD: 3061da177e4SLinus Torvalds ifp->timer.function = addrconf_dad_timer; 3071da177e4SLinus Torvalds break; 3081da177e4SLinus Torvalds case AC_RS: 3091da177e4SLinus Torvalds ifp->timer.function = addrconf_rs_timer; 3101da177e4SLinus Torvalds break; 3111da177e4SLinus Torvalds default:; 3121da177e4SLinus Torvalds } 3131da177e4SLinus Torvalds ifp->timer.expires = jiffies + when; 3141da177e4SLinus Torvalds add_timer(&ifp->timer); 3151da177e4SLinus Torvalds } 3161da177e4SLinus Torvalds 3171da177e4SLinus Torvalds /* Nobody refers to this device, we may destroy it. */ 3181da177e4SLinus Torvalds 3191da177e4SLinus Torvalds void in6_dev_finish_destroy(struct inet6_dev *idev) 3201da177e4SLinus Torvalds { 3211da177e4SLinus Torvalds struct net_device *dev = idev->dev; 3221da177e4SLinus Torvalds BUG_TRAP(idev->addr_list==NULL); 3231da177e4SLinus Torvalds BUG_TRAP(idev->mc_list==NULL); 3241da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG 3251da177e4SLinus Torvalds printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL"); 3261da177e4SLinus Torvalds #endif 3271da177e4SLinus Torvalds dev_put(dev); 3281da177e4SLinus Torvalds if (!idev->dead) { 3291da177e4SLinus Torvalds printk("Freeing alive inet6 device %p\n", idev); 3301da177e4SLinus Torvalds return; 3311da177e4SLinus Torvalds } 3321da177e4SLinus Torvalds snmp6_free_dev(idev); 3331da177e4SLinus Torvalds kfree(idev); 3341da177e4SLinus Torvalds } 3351da177e4SLinus Torvalds 3361da177e4SLinus Torvalds static struct inet6_dev * ipv6_add_dev(struct net_device *dev) 3371da177e4SLinus Torvalds { 3381da177e4SLinus Torvalds struct inet6_dev *ndev; 3391da177e4SLinus Torvalds 3401da177e4SLinus Torvalds ASSERT_RTNL(); 3411da177e4SLinus Torvalds 3421da177e4SLinus Torvalds if (dev->mtu < IPV6_MIN_MTU) 3431da177e4SLinus Torvalds return NULL; 3441da177e4SLinus Torvalds 345322f74a4SIngo Oeser ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL); 3461da177e4SLinus Torvalds 347322f74a4SIngo Oeser if (ndev == NULL) 348322f74a4SIngo Oeser return NULL; 3491da177e4SLinus Torvalds 3501da177e4SLinus Torvalds rwlock_init(&ndev->lock); 3511da177e4SLinus Torvalds ndev->dev = dev; 3521da177e4SLinus Torvalds memcpy(&ndev->cnf, &ipv6_devconf_dflt, sizeof(ndev->cnf)); 3531da177e4SLinus Torvalds ndev->cnf.mtu6 = dev->mtu; 3541da177e4SLinus Torvalds ndev->cnf.sysctl = NULL; 3551da177e4SLinus Torvalds ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl); 3561da177e4SLinus Torvalds if (ndev->nd_parms == NULL) { 3571da177e4SLinus Torvalds kfree(ndev); 3581da177e4SLinus Torvalds return NULL; 3591da177e4SLinus Torvalds } 3601da177e4SLinus Torvalds /* We refer to the device */ 3611da177e4SLinus Torvalds dev_hold(dev); 3621da177e4SLinus Torvalds 3631da177e4SLinus Torvalds if (snmp6_alloc_dev(ndev) < 0) { 3641da177e4SLinus Torvalds ADBG((KERN_WARNING 3651da177e4SLinus Torvalds "%s(): cannot allocate memory for statistics; dev=%s.\n", 3661da177e4SLinus Torvalds __FUNCTION__, dev->name)); 3671da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, ndev->nd_parms); 3681da177e4SLinus Torvalds ndev->dead = 1; 3691da177e4SLinus Torvalds in6_dev_finish_destroy(ndev); 3701da177e4SLinus Torvalds return NULL; 3711da177e4SLinus Torvalds } 3721da177e4SLinus Torvalds 3731da177e4SLinus Torvalds if (snmp6_register_dev(ndev) < 0) { 3741da177e4SLinus Torvalds ADBG((KERN_WARNING 3751da177e4SLinus Torvalds "%s(): cannot create /proc/net/dev_snmp6/%s\n", 3761da177e4SLinus Torvalds __FUNCTION__, dev->name)); 3771da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, ndev->nd_parms); 3781da177e4SLinus Torvalds ndev->dead = 1; 3791da177e4SLinus Torvalds in6_dev_finish_destroy(ndev); 3801da177e4SLinus Torvalds return NULL; 3811da177e4SLinus Torvalds } 3821da177e4SLinus Torvalds 3831da177e4SLinus Torvalds /* One reference from device. We must do this before 3841da177e4SLinus Torvalds * we invoke __ipv6_regen_rndid(). 3851da177e4SLinus Torvalds */ 3861da177e4SLinus Torvalds in6_dev_hold(ndev); 3871da177e4SLinus Torvalds 3881da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 3891da177e4SLinus Torvalds init_timer(&ndev->regen_timer); 3901da177e4SLinus Torvalds ndev->regen_timer.function = ipv6_regen_rndid; 3911da177e4SLinus Torvalds ndev->regen_timer.data = (unsigned long) ndev; 3921da177e4SLinus Torvalds if ((dev->flags&IFF_LOOPBACK) || 3931da177e4SLinus Torvalds dev->type == ARPHRD_TUNNEL || 39477bd9196SRémi Denis-Courmont dev->type == ARPHRD_NONE || 3951da177e4SLinus Torvalds dev->type == ARPHRD_SIT) { 3961da177e4SLinus Torvalds printk(KERN_INFO 397a1493d9cSDavid S. Miller "%s: Disabled Privacy Extensions\n", 398a1493d9cSDavid S. Miller dev->name); 3991da177e4SLinus Torvalds ndev->cnf.use_tempaddr = -1; 4001da177e4SLinus Torvalds } else { 4011da177e4SLinus Torvalds in6_dev_hold(ndev); 4021da177e4SLinus Torvalds ipv6_regen_rndid((unsigned long) ndev); 4031da177e4SLinus Torvalds } 4041da177e4SLinus Torvalds #endif 4051da177e4SLinus Torvalds 4063c21edbdSYOSHIFUJI Hideaki if (netif_carrier_ok(dev)) 4073c21edbdSYOSHIFUJI Hideaki ndev->if_flags |= IF_READY; 4083c21edbdSYOSHIFUJI Hideaki 4091da177e4SLinus Torvalds write_lock_bh(&addrconf_lock); 4101da177e4SLinus Torvalds dev->ip6_ptr = ndev; 4111da177e4SLinus Torvalds write_unlock_bh(&addrconf_lock); 4121da177e4SLinus Torvalds 4131da177e4SLinus Torvalds ipv6_mc_init_dev(ndev); 4141da177e4SLinus Torvalds ndev->tstamp = jiffies; 4151da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 4161da177e4SLinus Torvalds neigh_sysctl_register(dev, ndev->nd_parms, NET_IPV6, 4171da177e4SLinus Torvalds NET_IPV6_NEIGH, "ipv6", 4181da177e4SLinus Torvalds &ndisc_ifinfo_sysctl_change, 4191da177e4SLinus Torvalds NULL); 4201da177e4SLinus Torvalds addrconf_sysctl_register(ndev, &ndev->cnf); 4211da177e4SLinus Torvalds #endif 4221da177e4SLinus Torvalds return ndev; 4231da177e4SLinus Torvalds } 4241da177e4SLinus Torvalds 4251da177e4SLinus Torvalds static struct inet6_dev * ipv6_find_idev(struct net_device *dev) 4261da177e4SLinus Torvalds { 4271da177e4SLinus Torvalds struct inet6_dev *idev; 4281da177e4SLinus Torvalds 4291da177e4SLinus Torvalds ASSERT_RTNL(); 4301da177e4SLinus Torvalds 4311da177e4SLinus Torvalds if ((idev = __in6_dev_get(dev)) == NULL) { 4321da177e4SLinus Torvalds if ((idev = ipv6_add_dev(dev)) == NULL) 4331da177e4SLinus Torvalds return NULL; 4341da177e4SLinus Torvalds } 435c5e33bddSYOSHIFUJI Hideaki 4361da177e4SLinus Torvalds if (dev->flags&IFF_UP) 4371da177e4SLinus Torvalds ipv6_mc_up(idev); 4381da177e4SLinus Torvalds return idev; 4391da177e4SLinus Torvalds } 4401da177e4SLinus Torvalds 4411da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 4421da177e4SLinus Torvalds static void dev_forward_change(struct inet6_dev *idev) 4431da177e4SLinus Torvalds { 4441da177e4SLinus Torvalds struct net_device *dev; 4451da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 4461da177e4SLinus Torvalds struct in6_addr addr; 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds if (!idev) 4491da177e4SLinus Torvalds return; 4501da177e4SLinus Torvalds dev = idev->dev; 4511da177e4SLinus Torvalds if (dev && (dev->flags & IFF_MULTICAST)) { 4521da177e4SLinus Torvalds ipv6_addr_all_routers(&addr); 4531da177e4SLinus Torvalds 4541da177e4SLinus Torvalds if (idev->cnf.forwarding) 4551da177e4SLinus Torvalds ipv6_dev_mc_inc(dev, &addr); 4561da177e4SLinus Torvalds else 4571da177e4SLinus Torvalds ipv6_dev_mc_dec(dev, &addr); 4581da177e4SLinus Torvalds } 4591da177e4SLinus Torvalds for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) { 4601da177e4SLinus Torvalds if (idev->cnf.forwarding) 4611da177e4SLinus Torvalds addrconf_join_anycast(ifa); 4621da177e4SLinus Torvalds else 4631da177e4SLinus Torvalds addrconf_leave_anycast(ifa); 4641da177e4SLinus Torvalds } 4651da177e4SLinus Torvalds } 4661da177e4SLinus Torvalds 4671da177e4SLinus Torvalds 4681da177e4SLinus Torvalds static void addrconf_forward_change(void) 4691da177e4SLinus Torvalds { 4701da177e4SLinus Torvalds struct net_device *dev; 4711da177e4SLinus Torvalds struct inet6_dev *idev; 4721da177e4SLinus Torvalds 4731da177e4SLinus Torvalds read_lock(&dev_base_lock); 4741da177e4SLinus Torvalds for (dev=dev_base; dev; dev=dev->next) { 4751da177e4SLinus Torvalds read_lock(&addrconf_lock); 4761da177e4SLinus Torvalds idev = __in6_dev_get(dev); 4771da177e4SLinus Torvalds if (idev) { 4781da177e4SLinus Torvalds int changed = (!idev->cnf.forwarding) ^ (!ipv6_devconf.forwarding); 4791da177e4SLinus Torvalds idev->cnf.forwarding = ipv6_devconf.forwarding; 4801da177e4SLinus Torvalds if (changed) 4811da177e4SLinus Torvalds dev_forward_change(idev); 4821da177e4SLinus Torvalds } 4831da177e4SLinus Torvalds read_unlock(&addrconf_lock); 4841da177e4SLinus Torvalds } 4851da177e4SLinus Torvalds read_unlock(&dev_base_lock); 4861da177e4SLinus Torvalds } 4871da177e4SLinus Torvalds #endif 4881da177e4SLinus Torvalds 4891da177e4SLinus Torvalds /* Nobody refers to this ifaddr, destroy it */ 4901da177e4SLinus Torvalds 4911da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp) 4921da177e4SLinus Torvalds { 4931da177e4SLinus Torvalds BUG_TRAP(ifp->if_next==NULL); 4941da177e4SLinus Torvalds BUG_TRAP(ifp->lst_next==NULL); 4951da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG 4961da177e4SLinus Torvalds printk(KERN_DEBUG "inet6_ifa_finish_destroy\n"); 4971da177e4SLinus Torvalds #endif 4981da177e4SLinus Torvalds 4991da177e4SLinus Torvalds in6_dev_put(ifp->idev); 5001da177e4SLinus Torvalds 5011da177e4SLinus Torvalds if (del_timer(&ifp->timer)) 5021da177e4SLinus Torvalds printk("Timer is still running, when freeing ifa=%p\n", ifp); 5031da177e4SLinus Torvalds 5041da177e4SLinus Torvalds if (!ifp->dead) { 5051da177e4SLinus Torvalds printk("Freeing alive inet6 address %p\n", ifp); 5061da177e4SLinus Torvalds return; 5071da177e4SLinus Torvalds } 5081da177e4SLinus Torvalds dst_release(&ifp->rt->u.dst); 5091da177e4SLinus Torvalds 5101da177e4SLinus Torvalds kfree(ifp); 5111da177e4SLinus Torvalds } 5121da177e4SLinus Torvalds 513e55ffac6SBrian Haley static void 514e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp) 515e55ffac6SBrian Haley { 516e55ffac6SBrian Haley struct inet6_ifaddr *ifa, **ifap; 5178a6ce0c0SYOSHIFUJI Hideaki int ifp_scope = ipv6_addr_src_scope(&ifp->addr); 518e55ffac6SBrian Haley 519e55ffac6SBrian Haley /* 520e55ffac6SBrian Haley * Each device address list is sorted in order of scope - 521e55ffac6SBrian Haley * global before linklocal. 522e55ffac6SBrian Haley */ 523e55ffac6SBrian Haley for (ifap = &idev->addr_list; (ifa = *ifap) != NULL; 524e55ffac6SBrian Haley ifap = &ifa->if_next) { 5258a6ce0c0SYOSHIFUJI Hideaki if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr)) 526e55ffac6SBrian Haley break; 527e55ffac6SBrian Haley } 528e55ffac6SBrian Haley 529e55ffac6SBrian Haley ifp->if_next = *ifap; 530e55ffac6SBrian Haley *ifap = ifp; 531e55ffac6SBrian Haley } 532e55ffac6SBrian Haley 5331da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */ 5341da177e4SLinus Torvalds 5351da177e4SLinus Torvalds static struct inet6_ifaddr * 5361da177e4SLinus Torvalds ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen, 537e431b8c0SJamal Hadi Salim int scope, u32 flags) 5381da177e4SLinus Torvalds { 5391da177e4SLinus Torvalds struct inet6_ifaddr *ifa = NULL; 5401da177e4SLinus Torvalds struct rt6_info *rt; 5411da177e4SLinus Torvalds int hash; 5421da177e4SLinus Torvalds int err = 0; 5431da177e4SLinus Torvalds 5441da177e4SLinus Torvalds read_lock_bh(&addrconf_lock); 5451da177e4SLinus Torvalds if (idev->dead) { 5461da177e4SLinus Torvalds err = -ENODEV; /*XXX*/ 5471da177e4SLinus Torvalds goto out2; 5481da177e4SLinus Torvalds } 5491da177e4SLinus Torvalds 5501da177e4SLinus Torvalds write_lock(&addrconf_hash_lock); 5511da177e4SLinus Torvalds 5521da177e4SLinus Torvalds /* Ignore adding duplicate addresses on an interface */ 5531da177e4SLinus Torvalds if (ipv6_chk_same_addr(addr, idev->dev)) { 5541da177e4SLinus Torvalds ADBG(("ipv6_add_addr: already assigned\n")); 5551da177e4SLinus Torvalds err = -EEXIST; 5561da177e4SLinus Torvalds goto out; 5571da177e4SLinus Torvalds } 5581da177e4SLinus Torvalds 559322f74a4SIngo Oeser ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC); 5601da177e4SLinus Torvalds 5611da177e4SLinus Torvalds if (ifa == NULL) { 5621da177e4SLinus Torvalds ADBG(("ipv6_add_addr: malloc failed\n")); 5631da177e4SLinus Torvalds err = -ENOBUFS; 5641da177e4SLinus Torvalds goto out; 5651da177e4SLinus Torvalds } 5661da177e4SLinus Torvalds 5671da177e4SLinus Torvalds rt = addrconf_dst_alloc(idev, addr, 0); 5681da177e4SLinus Torvalds if (IS_ERR(rt)) { 5691da177e4SLinus Torvalds err = PTR_ERR(rt); 5701da177e4SLinus Torvalds goto out; 5711da177e4SLinus Torvalds } 5721da177e4SLinus Torvalds 5731da177e4SLinus Torvalds ipv6_addr_copy(&ifa->addr, addr); 5741da177e4SLinus Torvalds 5751da177e4SLinus Torvalds spin_lock_init(&ifa->lock); 5761da177e4SLinus Torvalds init_timer(&ifa->timer); 5771da177e4SLinus Torvalds ifa->timer.data = (unsigned long) ifa; 5781da177e4SLinus Torvalds ifa->scope = scope; 5791da177e4SLinus Torvalds ifa->prefix_len = pfxlen; 5801da177e4SLinus Torvalds ifa->flags = flags | IFA_F_TENTATIVE; 5811da177e4SLinus Torvalds ifa->cstamp = ifa->tstamp = jiffies; 5821da177e4SLinus Torvalds 58357f5f544SKeir Fraser ifa->rt = rt; 58457f5f544SKeir Fraser 5851da177e4SLinus Torvalds ifa->idev = idev; 5861da177e4SLinus Torvalds in6_dev_hold(idev); 5871da177e4SLinus Torvalds /* For caller */ 5881da177e4SLinus Torvalds in6_ifa_hold(ifa); 5891da177e4SLinus Torvalds 5901da177e4SLinus Torvalds /* Add to big hash table */ 5911da177e4SLinus Torvalds hash = ipv6_addr_hash(addr); 5921da177e4SLinus Torvalds 5931da177e4SLinus Torvalds ifa->lst_next = inet6_addr_lst[hash]; 5941da177e4SLinus Torvalds inet6_addr_lst[hash] = ifa; 5951da177e4SLinus Torvalds in6_ifa_hold(ifa); 5961da177e4SLinus Torvalds write_unlock(&addrconf_hash_lock); 5971da177e4SLinus Torvalds 5981da177e4SLinus Torvalds write_lock(&idev->lock); 5991da177e4SLinus Torvalds /* Add to inet6_dev unicast addr list. */ 600e55ffac6SBrian Haley ipv6_link_dev_addr(idev, ifa); 6011da177e4SLinus Torvalds 6021da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 6031da177e4SLinus Torvalds if (ifa->flags&IFA_F_TEMPORARY) { 6041da177e4SLinus Torvalds ifa->tmp_next = idev->tempaddr_list; 6051da177e4SLinus Torvalds idev->tempaddr_list = ifa; 6061da177e4SLinus Torvalds in6_ifa_hold(ifa); 6071da177e4SLinus Torvalds } 6081da177e4SLinus Torvalds #endif 6091da177e4SLinus Torvalds 6101da177e4SLinus Torvalds in6_ifa_hold(ifa); 6111da177e4SLinus Torvalds write_unlock(&idev->lock); 6121da177e4SLinus Torvalds out2: 6131da177e4SLinus Torvalds read_unlock_bh(&addrconf_lock); 6141da177e4SLinus Torvalds 615fd92833aSYOSHIFUJI Hideaki if (likely(err == 0)) 616e041c683SAlan Stern atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa); 6171da177e4SLinus Torvalds else { 6181da177e4SLinus Torvalds kfree(ifa); 6191da177e4SLinus Torvalds ifa = ERR_PTR(err); 6201da177e4SLinus Torvalds } 6211da177e4SLinus Torvalds 6221da177e4SLinus Torvalds return ifa; 6231da177e4SLinus Torvalds out: 6241da177e4SLinus Torvalds write_unlock(&addrconf_hash_lock); 6251da177e4SLinus Torvalds goto out2; 6261da177e4SLinus Torvalds } 6271da177e4SLinus Torvalds 6281da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */ 6291da177e4SLinus Torvalds 6301da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp) 6311da177e4SLinus Torvalds { 6321da177e4SLinus Torvalds struct inet6_ifaddr *ifa, **ifap; 6331da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 6341da177e4SLinus Torvalds int hash; 6351da177e4SLinus Torvalds int deleted = 0, onlink = 0; 6361da177e4SLinus Torvalds unsigned long expires = jiffies; 6371da177e4SLinus Torvalds 6381da177e4SLinus Torvalds hash = ipv6_addr_hash(&ifp->addr); 6391da177e4SLinus Torvalds 6401da177e4SLinus Torvalds ifp->dead = 1; 6411da177e4SLinus Torvalds 6421da177e4SLinus Torvalds write_lock_bh(&addrconf_hash_lock); 6431da177e4SLinus Torvalds for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL; 6441da177e4SLinus Torvalds ifap = &ifa->lst_next) { 6451da177e4SLinus Torvalds if (ifa == ifp) { 6461da177e4SLinus Torvalds *ifap = ifa->lst_next; 6471da177e4SLinus Torvalds __in6_ifa_put(ifp); 6481da177e4SLinus Torvalds ifa->lst_next = NULL; 6491da177e4SLinus Torvalds break; 6501da177e4SLinus Torvalds } 6511da177e4SLinus Torvalds } 6521da177e4SLinus Torvalds write_unlock_bh(&addrconf_hash_lock); 6531da177e4SLinus Torvalds 6541da177e4SLinus Torvalds write_lock_bh(&idev->lock); 6551da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 6561da177e4SLinus Torvalds if (ifp->flags&IFA_F_TEMPORARY) { 6571da177e4SLinus Torvalds for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL; 6581da177e4SLinus Torvalds ifap = &ifa->tmp_next) { 6591da177e4SLinus Torvalds if (ifa == ifp) { 6601da177e4SLinus Torvalds *ifap = ifa->tmp_next; 6611da177e4SLinus Torvalds if (ifp->ifpub) { 6621da177e4SLinus Torvalds in6_ifa_put(ifp->ifpub); 6631da177e4SLinus Torvalds ifp->ifpub = NULL; 6641da177e4SLinus Torvalds } 6651da177e4SLinus Torvalds __in6_ifa_put(ifp); 6661da177e4SLinus Torvalds ifa->tmp_next = NULL; 6671da177e4SLinus Torvalds break; 6681da177e4SLinus Torvalds } 6691da177e4SLinus Torvalds } 6701da177e4SLinus Torvalds } 6711da177e4SLinus Torvalds #endif 6721da177e4SLinus Torvalds 6731d142804SKristian Slavov for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;) { 6741da177e4SLinus Torvalds if (ifa == ifp) { 6751da177e4SLinus Torvalds *ifap = ifa->if_next; 6761da177e4SLinus Torvalds __in6_ifa_put(ifp); 6771da177e4SLinus Torvalds ifa->if_next = NULL; 6781da177e4SLinus Torvalds if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0) 6791da177e4SLinus Torvalds break; 6801da177e4SLinus Torvalds deleted = 1; 6811d142804SKristian Slavov continue; 6821da177e4SLinus Torvalds } else if (ifp->flags & IFA_F_PERMANENT) { 6831da177e4SLinus Torvalds if (ipv6_prefix_equal(&ifa->addr, &ifp->addr, 6841da177e4SLinus Torvalds ifp->prefix_len)) { 6851da177e4SLinus Torvalds if (ifa->flags & IFA_F_PERMANENT) { 6861da177e4SLinus Torvalds onlink = 1; 6871da177e4SLinus Torvalds if (deleted) 6881da177e4SLinus Torvalds break; 6891da177e4SLinus Torvalds } else { 6901da177e4SLinus Torvalds unsigned long lifetime; 6911da177e4SLinus Torvalds 6921da177e4SLinus Torvalds if (!onlink) 6931da177e4SLinus Torvalds onlink = -1; 6941da177e4SLinus Torvalds 6951da177e4SLinus Torvalds spin_lock(&ifa->lock); 6961da177e4SLinus Torvalds lifetime = min_t(unsigned long, 6971da177e4SLinus Torvalds ifa->valid_lft, 0x7fffffffUL/HZ); 6981da177e4SLinus Torvalds if (time_before(expires, 6991da177e4SLinus Torvalds ifa->tstamp + lifetime * HZ)) 7001da177e4SLinus Torvalds expires = ifa->tstamp + lifetime * HZ; 7011da177e4SLinus Torvalds spin_unlock(&ifa->lock); 7021da177e4SLinus Torvalds } 7031da177e4SLinus Torvalds } 7041da177e4SLinus Torvalds } 7051d142804SKristian Slavov ifap = &ifa->if_next; 7061da177e4SLinus Torvalds } 7071da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 7081da177e4SLinus Torvalds 7091da177e4SLinus Torvalds ipv6_ifa_notify(RTM_DELADDR, ifp); 7101da177e4SLinus Torvalds 711e041c683SAlan Stern atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp); 7121da177e4SLinus Torvalds 7131da177e4SLinus Torvalds addrconf_del_timer(ifp); 7141da177e4SLinus Torvalds 7151da177e4SLinus Torvalds /* 7161da177e4SLinus Torvalds * Purge or update corresponding prefix 7171da177e4SLinus Torvalds * 7181da177e4SLinus Torvalds * 1) we don't purge prefix here if address was not permanent. 7191da177e4SLinus Torvalds * prefix is managed by its own lifetime. 7201da177e4SLinus Torvalds * 2) if there're no addresses, delete prefix. 7211da177e4SLinus Torvalds * 3) if there're still other permanent address(es), 7221da177e4SLinus Torvalds * corresponding prefix is still permanent. 7231da177e4SLinus Torvalds * 4) otherwise, update prefix lifetime to the 7241da177e4SLinus Torvalds * longest valid lifetime among the corresponding 7251da177e4SLinus Torvalds * addresses on the device. 7261da177e4SLinus Torvalds * Note: subsequent RA will update lifetime. 7271da177e4SLinus Torvalds * 7281da177e4SLinus Torvalds * --yoshfuji 7291da177e4SLinus Torvalds */ 7301da177e4SLinus Torvalds if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) { 7311da177e4SLinus Torvalds struct in6_addr prefix; 7321da177e4SLinus Torvalds struct rt6_info *rt; 7331da177e4SLinus Torvalds 7341da177e4SLinus Torvalds ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len); 7351da177e4SLinus Torvalds rt = rt6_lookup(&prefix, NULL, ifp->idev->dev->ifindex, 1); 7361da177e4SLinus Torvalds 7371da177e4SLinus Torvalds if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) { 7381da177e4SLinus Torvalds if (onlink == 0) { 739e0a1ad73SThomas Graf ip6_del_rt(rt); 7401da177e4SLinus Torvalds rt = NULL; 7411da177e4SLinus Torvalds } else if (!(rt->rt6i_flags & RTF_EXPIRES)) { 7421da177e4SLinus Torvalds rt->rt6i_expires = expires; 7431da177e4SLinus Torvalds rt->rt6i_flags |= RTF_EXPIRES; 7441da177e4SLinus Torvalds } 7451da177e4SLinus Torvalds } 7461da177e4SLinus Torvalds dst_release(&rt->u.dst); 7471da177e4SLinus Torvalds } 7481da177e4SLinus Torvalds 7491da177e4SLinus Torvalds in6_ifa_put(ifp); 7501da177e4SLinus Torvalds } 7511da177e4SLinus Torvalds 7521da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 7531da177e4SLinus Torvalds static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift) 7541da177e4SLinus Torvalds { 7551da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 7561da177e4SLinus Torvalds struct in6_addr addr, *tmpaddr; 7571da177e4SLinus Torvalds unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp; 7581da177e4SLinus Torvalds int tmp_plen; 7591da177e4SLinus Torvalds int ret = 0; 7601da177e4SLinus Torvalds int max_addresses; 7611da177e4SLinus Torvalds 7621da177e4SLinus Torvalds write_lock(&idev->lock); 7631da177e4SLinus Torvalds if (ift) { 7641da177e4SLinus Torvalds spin_lock_bh(&ift->lock); 7651da177e4SLinus Torvalds memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8); 7661da177e4SLinus Torvalds spin_unlock_bh(&ift->lock); 7671da177e4SLinus Torvalds tmpaddr = &addr; 7681da177e4SLinus Torvalds } else { 7691da177e4SLinus Torvalds tmpaddr = NULL; 7701da177e4SLinus Torvalds } 7711da177e4SLinus Torvalds retry: 7721da177e4SLinus Torvalds in6_dev_hold(idev); 7731da177e4SLinus Torvalds if (idev->cnf.use_tempaddr <= 0) { 7741da177e4SLinus Torvalds write_unlock(&idev->lock); 7751da177e4SLinus Torvalds printk(KERN_INFO 7761da177e4SLinus Torvalds "ipv6_create_tempaddr(): use_tempaddr is disabled.\n"); 7771da177e4SLinus Torvalds in6_dev_put(idev); 7781da177e4SLinus Torvalds ret = -1; 7791da177e4SLinus Torvalds goto out; 7801da177e4SLinus Torvalds } 7811da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 7821da177e4SLinus Torvalds if (ifp->regen_count++ >= idev->cnf.regen_max_retry) { 7831da177e4SLinus Torvalds idev->cnf.use_tempaddr = -1; /*XXX*/ 7841da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 7851da177e4SLinus Torvalds write_unlock(&idev->lock); 7861da177e4SLinus Torvalds printk(KERN_WARNING 7871da177e4SLinus Torvalds "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n"); 7881da177e4SLinus Torvalds in6_dev_put(idev); 7891da177e4SLinus Torvalds ret = -1; 7901da177e4SLinus Torvalds goto out; 7911da177e4SLinus Torvalds } 7921da177e4SLinus Torvalds in6_ifa_hold(ifp); 7931da177e4SLinus Torvalds memcpy(addr.s6_addr, ifp->addr.s6_addr, 8); 7941da177e4SLinus Torvalds if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) { 7951da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 7961da177e4SLinus Torvalds write_unlock(&idev->lock); 7971da177e4SLinus Torvalds printk(KERN_WARNING 7981da177e4SLinus Torvalds "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n"); 7991da177e4SLinus Torvalds in6_ifa_put(ifp); 8001da177e4SLinus Torvalds in6_dev_put(idev); 8011da177e4SLinus Torvalds ret = -1; 8021da177e4SLinus Torvalds goto out; 8031da177e4SLinus Torvalds } 8041da177e4SLinus Torvalds memcpy(&addr.s6_addr[8], idev->rndid, 8); 8051da177e4SLinus Torvalds tmp_valid_lft = min_t(__u32, 8061da177e4SLinus Torvalds ifp->valid_lft, 8071da177e4SLinus Torvalds idev->cnf.temp_valid_lft); 8081da177e4SLinus Torvalds tmp_prefered_lft = min_t(__u32, 8091da177e4SLinus Torvalds ifp->prefered_lft, 8101da177e4SLinus Torvalds idev->cnf.temp_prefered_lft - desync_factor / HZ); 8111da177e4SLinus Torvalds tmp_plen = ifp->prefix_len; 8121da177e4SLinus Torvalds max_addresses = idev->cnf.max_addresses; 8131da177e4SLinus Torvalds tmp_cstamp = ifp->cstamp; 8141da177e4SLinus Torvalds tmp_tstamp = ifp->tstamp; 8151da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 8161da177e4SLinus Torvalds 8171da177e4SLinus Torvalds write_unlock(&idev->lock); 8181da177e4SLinus Torvalds ift = !max_addresses || 8191da177e4SLinus Torvalds ipv6_count_addresses(idev) < max_addresses ? 8201da177e4SLinus Torvalds ipv6_add_addr(idev, &addr, tmp_plen, 8211da177e4SLinus Torvalds ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK, IFA_F_TEMPORARY) : NULL; 8221da177e4SLinus Torvalds if (!ift || IS_ERR(ift)) { 8231da177e4SLinus Torvalds in6_ifa_put(ifp); 8241da177e4SLinus Torvalds in6_dev_put(idev); 8251da177e4SLinus Torvalds printk(KERN_INFO 8261da177e4SLinus Torvalds "ipv6_create_tempaddr(): retry temporary address regeneration.\n"); 8271da177e4SLinus Torvalds tmpaddr = &addr; 8281da177e4SLinus Torvalds write_lock(&idev->lock); 8291da177e4SLinus Torvalds goto retry; 8301da177e4SLinus Torvalds } 8311da177e4SLinus Torvalds 8321da177e4SLinus Torvalds spin_lock_bh(&ift->lock); 8331da177e4SLinus Torvalds ift->ifpub = ifp; 8341da177e4SLinus Torvalds ift->valid_lft = tmp_valid_lft; 8351da177e4SLinus Torvalds ift->prefered_lft = tmp_prefered_lft; 8361da177e4SLinus Torvalds ift->cstamp = tmp_cstamp; 8371da177e4SLinus Torvalds ift->tstamp = tmp_tstamp; 8381da177e4SLinus Torvalds spin_unlock_bh(&ift->lock); 8391da177e4SLinus Torvalds 8401da177e4SLinus Torvalds addrconf_dad_start(ift, 0); 8411da177e4SLinus Torvalds in6_ifa_put(ift); 8421da177e4SLinus Torvalds in6_dev_put(idev); 8431da177e4SLinus Torvalds out: 8441da177e4SLinus Torvalds return ret; 8451da177e4SLinus Torvalds } 8461da177e4SLinus Torvalds #endif 8471da177e4SLinus Torvalds 8481da177e4SLinus Torvalds /* 849072047e4SYOSHIFUJI Hideaki * Choose an appropriate source address (RFC3484) 8501da177e4SLinus Torvalds */ 851072047e4SYOSHIFUJI Hideaki struct ipv6_saddr_score { 852072047e4SYOSHIFUJI Hideaki int addr_type; 853072047e4SYOSHIFUJI Hideaki unsigned int attrs; 854072047e4SYOSHIFUJI Hideaki int matchlen; 8550d27b427SBrian Haley int scope; 856072047e4SYOSHIFUJI Hideaki unsigned int rule; 857072047e4SYOSHIFUJI Hideaki }; 858072047e4SYOSHIFUJI Hideaki 859072047e4SYOSHIFUJI Hideaki #define IPV6_SADDR_SCORE_LOCAL 0x0001 860072047e4SYOSHIFUJI Hideaki #define IPV6_SADDR_SCORE_PREFERRED 0x0004 861072047e4SYOSHIFUJI Hideaki #define IPV6_SADDR_SCORE_HOA 0x0008 862072047e4SYOSHIFUJI Hideaki #define IPV6_SADDR_SCORE_OIF 0x0010 863072047e4SYOSHIFUJI Hideaki #define IPV6_SADDR_SCORE_LABEL 0x0020 864072047e4SYOSHIFUJI Hideaki #define IPV6_SADDR_SCORE_PRIVACY 0x0040 865072047e4SYOSHIFUJI Hideaki 866072047e4SYOSHIFUJI Hideaki static int inline ipv6_saddr_preferred(int type) 8671da177e4SLinus Torvalds { 868072047e4SYOSHIFUJI Hideaki if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4| 869072047e4SYOSHIFUJI Hideaki IPV6_ADDR_LOOPBACK|IPV6_ADDR_RESERVED)) 870072047e4SYOSHIFUJI Hideaki return 1; 871072047e4SYOSHIFUJI Hideaki return 0; 8721da177e4SLinus Torvalds } 8731da177e4SLinus Torvalds 874072047e4SYOSHIFUJI Hideaki /* static matching label */ 875072047e4SYOSHIFUJI Hideaki static int inline ipv6_saddr_label(const struct in6_addr *addr, int type) 876072047e4SYOSHIFUJI Hideaki { 877072047e4SYOSHIFUJI Hideaki /* 878072047e4SYOSHIFUJI Hideaki * prefix (longest match) label 879072047e4SYOSHIFUJI Hideaki * ----------------------------- 880072047e4SYOSHIFUJI Hideaki * ::1/128 0 881072047e4SYOSHIFUJI Hideaki * ::/0 1 882072047e4SYOSHIFUJI Hideaki * 2002::/16 2 883072047e4SYOSHIFUJI Hideaki * ::/96 3 884072047e4SYOSHIFUJI Hideaki * ::ffff:0:0/96 4 885102128e3SŁukasz Stelmach * fc00::/7 5 886102128e3SŁukasz Stelmach * 2001::/32 6 887072047e4SYOSHIFUJI Hideaki */ 888072047e4SYOSHIFUJI Hideaki if (type & IPV6_ADDR_LOOPBACK) 889072047e4SYOSHIFUJI Hideaki return 0; 890072047e4SYOSHIFUJI Hideaki else if (type & IPV6_ADDR_COMPATv4) 891072047e4SYOSHIFUJI Hideaki return 3; 892072047e4SYOSHIFUJI Hideaki else if (type & IPV6_ADDR_MAPPED) 893072047e4SYOSHIFUJI Hideaki return 4; 894102128e3SŁukasz Stelmach else if (addr->s6_addr32[0] == htonl(0x20010000)) 895102128e3SŁukasz Stelmach return 6; 896072047e4SYOSHIFUJI Hideaki else if (addr->s6_addr16[0] == htons(0x2002)) 897072047e4SYOSHIFUJI Hideaki return 2; 898102128e3SŁukasz Stelmach else if ((addr->s6_addr[0] & 0xfe) == 0xfc) 899102128e3SŁukasz Stelmach return 5; 900072047e4SYOSHIFUJI Hideaki return 1; 901072047e4SYOSHIFUJI Hideaki } 9021da177e4SLinus Torvalds 903072047e4SYOSHIFUJI Hideaki int ipv6_dev_get_saddr(struct net_device *daddr_dev, 9041da177e4SLinus Torvalds struct in6_addr *daddr, struct in6_addr *saddr) 9051da177e4SLinus Torvalds { 906072047e4SYOSHIFUJI Hideaki struct ipv6_saddr_score hiscore; 907072047e4SYOSHIFUJI Hideaki struct inet6_ifaddr *ifa_result = NULL; 908072047e4SYOSHIFUJI Hideaki int daddr_type = __ipv6_addr_type(daddr); 909072047e4SYOSHIFUJI Hideaki int daddr_scope = __ipv6_addr_src_scope(daddr_type); 910072047e4SYOSHIFUJI Hideaki u32 daddr_label = ipv6_saddr_label(daddr, daddr_type); 911072047e4SYOSHIFUJI Hideaki struct net_device *dev; 9121da177e4SLinus Torvalds 913072047e4SYOSHIFUJI Hideaki memset(&hiscore, 0, sizeof(hiscore)); 9141da177e4SLinus Torvalds 9151da177e4SLinus Torvalds read_lock(&dev_base_lock); 9161da177e4SLinus Torvalds read_lock(&addrconf_lock); 917072047e4SYOSHIFUJI Hideaki 9181da177e4SLinus Torvalds for (dev = dev_base; dev; dev=dev->next) { 919072047e4SYOSHIFUJI Hideaki struct inet6_dev *idev; 920072047e4SYOSHIFUJI Hideaki struct inet6_ifaddr *ifa; 921072047e4SYOSHIFUJI Hideaki 922072047e4SYOSHIFUJI Hideaki /* Rule 0: Candidate Source Address (section 4) 923072047e4SYOSHIFUJI Hideaki * - multicast and link-local destination address, 924072047e4SYOSHIFUJI Hideaki * the set of candidate source address MUST only 925072047e4SYOSHIFUJI Hideaki * include addresses assigned to interfaces 926072047e4SYOSHIFUJI Hideaki * belonging to the same link as the outgoing 927072047e4SYOSHIFUJI Hideaki * interface. 928072047e4SYOSHIFUJI Hideaki * (- For site-local destination addresses, the 929072047e4SYOSHIFUJI Hideaki * set of candidate source addresses MUST only 930072047e4SYOSHIFUJI Hideaki * include addresses assigned to interfaces 931072047e4SYOSHIFUJI Hideaki * belonging to the same site as the outgoing 932072047e4SYOSHIFUJI Hideaki * interface.) 933072047e4SYOSHIFUJI Hideaki */ 934072047e4SYOSHIFUJI Hideaki if ((daddr_type & IPV6_ADDR_MULTICAST || 935072047e4SYOSHIFUJI Hideaki daddr_scope <= IPV6_ADDR_SCOPE_LINKLOCAL) && 936072047e4SYOSHIFUJI Hideaki daddr_dev && dev != daddr_dev) 937072047e4SYOSHIFUJI Hideaki continue; 938072047e4SYOSHIFUJI Hideaki 9391da177e4SLinus Torvalds idev = __in6_dev_get(dev); 940072047e4SYOSHIFUJI Hideaki if (!idev) 941072047e4SYOSHIFUJI Hideaki continue; 942072047e4SYOSHIFUJI Hideaki 9431da177e4SLinus Torvalds read_lock_bh(&idev->lock); 944072047e4SYOSHIFUJI Hideaki for (ifa = idev->addr_list; ifa; ifa = ifa->if_next) { 945072047e4SYOSHIFUJI Hideaki struct ipv6_saddr_score score; 946072047e4SYOSHIFUJI Hideaki 947072047e4SYOSHIFUJI Hideaki score.addr_type = __ipv6_addr_type(&ifa->addr); 948072047e4SYOSHIFUJI Hideaki 9496b3ae80aSYOSHIFUJI Hideaki /* Rule 0: 9506b3ae80aSYOSHIFUJI Hideaki * - Tentative Address (RFC2462 section 5.4) 9516b3ae80aSYOSHIFUJI Hideaki * - A tentative address is not considered 9526b3ae80aSYOSHIFUJI Hideaki * "assigned to an interface" in the traditional 9536b3ae80aSYOSHIFUJI Hideaki * sense. 9546b3ae80aSYOSHIFUJI Hideaki * - Candidate Source Address (section 4) 955072047e4SYOSHIFUJI Hideaki * - In any case, anycast addresses, multicast 956072047e4SYOSHIFUJI Hideaki * addresses, and the unspecified address MUST 957072047e4SYOSHIFUJI Hideaki * NOT be included in a candidate set. 958072047e4SYOSHIFUJI Hideaki */ 9596b3ae80aSYOSHIFUJI Hideaki if (ifa->flags & IFA_F_TENTATIVE) 9606b3ae80aSYOSHIFUJI Hideaki continue; 961072047e4SYOSHIFUJI Hideaki if (unlikely(score.addr_type == IPV6_ADDR_ANY || 962072047e4SYOSHIFUJI Hideaki score.addr_type & IPV6_ADDR_MULTICAST)) { 963072047e4SYOSHIFUJI Hideaki LIMIT_NETDEBUG(KERN_DEBUG 964072047e4SYOSHIFUJI Hideaki "ADDRCONF: unspecified / multicast address" 965072047e4SYOSHIFUJI Hideaki "assigned as unicast address on %s", 966072047e4SYOSHIFUJI Hideaki dev->name); 9671da177e4SLinus Torvalds continue; 968072047e4SYOSHIFUJI Hideaki } 969072047e4SYOSHIFUJI Hideaki 970072047e4SYOSHIFUJI Hideaki score.attrs = 0; 971072047e4SYOSHIFUJI Hideaki score.matchlen = 0; 972072047e4SYOSHIFUJI Hideaki score.scope = 0; 973072047e4SYOSHIFUJI Hideaki score.rule = 0; 974072047e4SYOSHIFUJI Hideaki 975072047e4SYOSHIFUJI Hideaki if (ifa_result == NULL) { 976072047e4SYOSHIFUJI Hideaki /* record it if the first available entry */ 977072047e4SYOSHIFUJI Hideaki goto record_it; 978072047e4SYOSHIFUJI Hideaki } 979072047e4SYOSHIFUJI Hideaki 980072047e4SYOSHIFUJI Hideaki /* Rule 1: Prefer same address */ 981072047e4SYOSHIFUJI Hideaki if (hiscore.rule < 1) { 982072047e4SYOSHIFUJI Hideaki if (ipv6_addr_equal(&ifa_result->addr, daddr)) 983072047e4SYOSHIFUJI Hideaki hiscore.attrs |= IPV6_SADDR_SCORE_LOCAL; 984072047e4SYOSHIFUJI Hideaki hiscore.rule++; 985072047e4SYOSHIFUJI Hideaki } 986072047e4SYOSHIFUJI Hideaki if (ipv6_addr_equal(&ifa->addr, daddr)) { 987072047e4SYOSHIFUJI Hideaki score.attrs |= IPV6_SADDR_SCORE_LOCAL; 988072047e4SYOSHIFUJI Hideaki if (!(hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)) { 989072047e4SYOSHIFUJI Hideaki score.rule = 1; 990072047e4SYOSHIFUJI Hideaki goto record_it; 991072047e4SYOSHIFUJI Hideaki } 992072047e4SYOSHIFUJI Hideaki } else { 993072047e4SYOSHIFUJI Hideaki if (hiscore.attrs & IPV6_SADDR_SCORE_LOCAL) 994072047e4SYOSHIFUJI Hideaki continue; 995072047e4SYOSHIFUJI Hideaki } 996072047e4SYOSHIFUJI Hideaki 997072047e4SYOSHIFUJI Hideaki /* Rule 2: Prefer appropriate scope */ 998072047e4SYOSHIFUJI Hideaki if (hiscore.rule < 2) { 999072047e4SYOSHIFUJI Hideaki hiscore.scope = __ipv6_addr_src_scope(hiscore.addr_type); 1000072047e4SYOSHIFUJI Hideaki hiscore.rule++; 1001072047e4SYOSHIFUJI Hideaki } 1002072047e4SYOSHIFUJI Hideaki score.scope = __ipv6_addr_src_scope(score.addr_type); 1003072047e4SYOSHIFUJI Hideaki if (hiscore.scope < score.scope) { 1004072047e4SYOSHIFUJI Hideaki if (hiscore.scope < daddr_scope) { 1005072047e4SYOSHIFUJI Hideaki score.rule = 2; 1006072047e4SYOSHIFUJI Hideaki goto record_it; 1007072047e4SYOSHIFUJI Hideaki } else 1008072047e4SYOSHIFUJI Hideaki continue; 1009072047e4SYOSHIFUJI Hideaki } else if (score.scope < hiscore.scope) { 1010072047e4SYOSHIFUJI Hideaki if (score.scope < daddr_scope) 1011e55ffac6SBrian Haley break; /* addresses sorted by scope */ 1012072047e4SYOSHIFUJI Hideaki else { 1013072047e4SYOSHIFUJI Hideaki score.rule = 2; 1014072047e4SYOSHIFUJI Hideaki goto record_it; 1015072047e4SYOSHIFUJI Hideaki } 1016072047e4SYOSHIFUJI Hideaki } 1017072047e4SYOSHIFUJI Hideaki 1018072047e4SYOSHIFUJI Hideaki /* Rule 3: Avoid deprecated address */ 1019072047e4SYOSHIFUJI Hideaki if (hiscore.rule < 3) { 1020072047e4SYOSHIFUJI Hideaki if (ipv6_saddr_preferred(hiscore.addr_type) || 1021072047e4SYOSHIFUJI Hideaki !(ifa_result->flags & IFA_F_DEPRECATED)) 1022072047e4SYOSHIFUJI Hideaki hiscore.attrs |= IPV6_SADDR_SCORE_PREFERRED; 1023072047e4SYOSHIFUJI Hideaki hiscore.rule++; 1024072047e4SYOSHIFUJI Hideaki } 1025072047e4SYOSHIFUJI Hideaki if (ipv6_saddr_preferred(score.addr_type) || 1026072047e4SYOSHIFUJI Hideaki !(ifa->flags & IFA_F_DEPRECATED)) { 1027072047e4SYOSHIFUJI Hideaki score.attrs |= IPV6_SADDR_SCORE_PREFERRED; 1028072047e4SYOSHIFUJI Hideaki if (!(hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)) { 1029072047e4SYOSHIFUJI Hideaki score.rule = 3; 1030072047e4SYOSHIFUJI Hideaki goto record_it; 1031072047e4SYOSHIFUJI Hideaki } 1032072047e4SYOSHIFUJI Hideaki } else { 1033072047e4SYOSHIFUJI Hideaki if (hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED) 1034072047e4SYOSHIFUJI Hideaki continue; 1035072047e4SYOSHIFUJI Hideaki } 1036072047e4SYOSHIFUJI Hideaki 1037072047e4SYOSHIFUJI Hideaki /* Rule 4: Prefer home address -- not implemented yet */ 1038220bbd74SYOSHIFUJI Hideaki if (hiscore.rule < 4) 1039220bbd74SYOSHIFUJI Hideaki hiscore.rule++; 1040072047e4SYOSHIFUJI Hideaki 1041072047e4SYOSHIFUJI Hideaki /* Rule 5: Prefer outgoing interface */ 1042072047e4SYOSHIFUJI Hideaki if (hiscore.rule < 5) { 1043072047e4SYOSHIFUJI Hideaki if (daddr_dev == NULL || 1044072047e4SYOSHIFUJI Hideaki daddr_dev == ifa_result->idev->dev) 1045072047e4SYOSHIFUJI Hideaki hiscore.attrs |= IPV6_SADDR_SCORE_OIF; 1046072047e4SYOSHIFUJI Hideaki hiscore.rule++; 1047072047e4SYOSHIFUJI Hideaki } 1048072047e4SYOSHIFUJI Hideaki if (daddr_dev == NULL || 1049072047e4SYOSHIFUJI Hideaki daddr_dev == ifa->idev->dev) { 1050072047e4SYOSHIFUJI Hideaki score.attrs |= IPV6_SADDR_SCORE_OIF; 1051072047e4SYOSHIFUJI Hideaki if (!(hiscore.attrs & IPV6_SADDR_SCORE_OIF)) { 1052072047e4SYOSHIFUJI Hideaki score.rule = 5; 1053072047e4SYOSHIFUJI Hideaki goto record_it; 1054072047e4SYOSHIFUJI Hideaki } 1055072047e4SYOSHIFUJI Hideaki } else { 1056072047e4SYOSHIFUJI Hideaki if (hiscore.attrs & IPV6_SADDR_SCORE_OIF) 1057072047e4SYOSHIFUJI Hideaki continue; 1058072047e4SYOSHIFUJI Hideaki } 1059072047e4SYOSHIFUJI Hideaki 1060072047e4SYOSHIFUJI Hideaki /* Rule 6: Prefer matching label */ 1061072047e4SYOSHIFUJI Hideaki if (hiscore.rule < 6) { 1062072047e4SYOSHIFUJI Hideaki if (ipv6_saddr_label(&ifa_result->addr, hiscore.addr_type) == daddr_label) 1063072047e4SYOSHIFUJI Hideaki hiscore.attrs |= IPV6_SADDR_SCORE_LABEL; 1064072047e4SYOSHIFUJI Hideaki hiscore.rule++; 1065072047e4SYOSHIFUJI Hideaki } 1066072047e4SYOSHIFUJI Hideaki if (ipv6_saddr_label(&ifa->addr, score.addr_type) == daddr_label) { 1067072047e4SYOSHIFUJI Hideaki score.attrs |= IPV6_SADDR_SCORE_LABEL; 1068072047e4SYOSHIFUJI Hideaki if (!(hiscore.attrs & IPV6_SADDR_SCORE_LABEL)) { 1069072047e4SYOSHIFUJI Hideaki score.rule = 6; 1070072047e4SYOSHIFUJI Hideaki goto record_it; 1071072047e4SYOSHIFUJI Hideaki } 1072072047e4SYOSHIFUJI Hideaki } else { 1073072047e4SYOSHIFUJI Hideaki if (hiscore.attrs & IPV6_SADDR_SCORE_LABEL) 1074072047e4SYOSHIFUJI Hideaki continue; 1075072047e4SYOSHIFUJI Hideaki } 1076072047e4SYOSHIFUJI Hideaki 107744fd0261SPeter Chubb #ifdef CONFIG_IPV6_PRIVACY 1078072047e4SYOSHIFUJI Hideaki /* Rule 7: Prefer public address 1079072047e4SYOSHIFUJI Hideaki * Note: prefer temprary address if use_tempaddr >= 2 1080072047e4SYOSHIFUJI Hideaki */ 1081072047e4SYOSHIFUJI Hideaki if (hiscore.rule < 7) { 1082072047e4SYOSHIFUJI Hideaki if ((!(ifa_result->flags & IFA_F_TEMPORARY)) ^ 1083072047e4SYOSHIFUJI Hideaki (ifa_result->idev->cnf.use_tempaddr >= 2)) 1084072047e4SYOSHIFUJI Hideaki hiscore.attrs |= IPV6_SADDR_SCORE_PRIVACY; 1085072047e4SYOSHIFUJI Hideaki hiscore.rule++; 1086072047e4SYOSHIFUJI Hideaki } 1087072047e4SYOSHIFUJI Hideaki if ((!(ifa->flags & IFA_F_TEMPORARY)) ^ 1088072047e4SYOSHIFUJI Hideaki (ifa->idev->cnf.use_tempaddr >= 2)) { 1089072047e4SYOSHIFUJI Hideaki score.attrs |= IPV6_SADDR_SCORE_PRIVACY; 1090072047e4SYOSHIFUJI Hideaki if (!(hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)) { 1091072047e4SYOSHIFUJI Hideaki score.rule = 7; 1092072047e4SYOSHIFUJI Hideaki goto record_it; 1093072047e4SYOSHIFUJI Hideaki } 1094072047e4SYOSHIFUJI Hideaki } else { 1095072047e4SYOSHIFUJI Hideaki if (hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY) 1096072047e4SYOSHIFUJI Hideaki continue; 1097072047e4SYOSHIFUJI Hideaki } 10985e2707faSYOSHIFUJI Hideaki #else 10995e2707faSYOSHIFUJI Hideaki if (hiscore.rule < 7) 11005e2707faSYOSHIFUJI Hideaki hiscore.rule++; 110144fd0261SPeter Chubb #endif 1102072047e4SYOSHIFUJI Hideaki /* Rule 8: Use longest matching prefix */ 110312da2a43SYan Zheng if (hiscore.rule < 8) { 1104072047e4SYOSHIFUJI Hideaki hiscore.matchlen = ipv6_addr_diff(&ifa_result->addr, daddr); 110512da2a43SYan Zheng hiscore.rule++; 110612da2a43SYan Zheng } 1107072047e4SYOSHIFUJI Hideaki score.matchlen = ipv6_addr_diff(&ifa->addr, daddr); 1108072047e4SYOSHIFUJI Hideaki if (score.matchlen > hiscore.matchlen) { 1109072047e4SYOSHIFUJI Hideaki score.rule = 8; 1110072047e4SYOSHIFUJI Hideaki goto record_it; 1111072047e4SYOSHIFUJI Hideaki } 1112072047e4SYOSHIFUJI Hideaki #if 0 1113072047e4SYOSHIFUJI Hideaki else if (score.matchlen < hiscore.matchlen) 1114072047e4SYOSHIFUJI Hideaki continue; 11151da177e4SLinus Torvalds #endif 1116072047e4SYOSHIFUJI Hideaki 1117072047e4SYOSHIFUJI Hideaki /* Final Rule: choose first available one */ 11181da177e4SLinus Torvalds continue; 1119072047e4SYOSHIFUJI Hideaki record_it: 1120072047e4SYOSHIFUJI Hideaki if (ifa_result) 1121072047e4SYOSHIFUJI Hideaki in6_ifa_put(ifa_result); 1122072047e4SYOSHIFUJI Hideaki in6_ifa_hold(ifa); 1123072047e4SYOSHIFUJI Hideaki ifa_result = ifa; 11241da177e4SLinus Torvalds hiscore = score; 11251da177e4SLinus Torvalds } 11261da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 11271da177e4SLinus Torvalds } 11281da177e4SLinus Torvalds read_unlock(&addrconf_lock); 11291da177e4SLinus Torvalds read_unlock(&dev_base_lock); 11301da177e4SLinus Torvalds 1131072047e4SYOSHIFUJI Hideaki if (!ifa_result) 1132072047e4SYOSHIFUJI Hideaki return -EADDRNOTAVAIL; 11331da177e4SLinus Torvalds 1134072047e4SYOSHIFUJI Hideaki ipv6_addr_copy(saddr, &ifa_result->addr); 1135072047e4SYOSHIFUJI Hideaki in6_ifa_put(ifa_result); 1136072047e4SYOSHIFUJI Hideaki return 0; 11371da177e4SLinus Torvalds } 11381da177e4SLinus Torvalds 11391da177e4SLinus Torvalds 11401da177e4SLinus Torvalds int ipv6_get_saddr(struct dst_entry *dst, 11411da177e4SLinus Torvalds struct in6_addr *daddr, struct in6_addr *saddr) 11421da177e4SLinus Torvalds { 11431da177e4SLinus Torvalds return ipv6_dev_get_saddr(dst ? ((struct rt6_info *)dst)->rt6i_idev->dev : NULL, daddr, saddr); 11441da177e4SLinus Torvalds } 11451da177e4SLinus Torvalds 11461da177e4SLinus Torvalds 11471da177e4SLinus Torvalds int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr) 11481da177e4SLinus Torvalds { 11491da177e4SLinus Torvalds struct inet6_dev *idev; 11501da177e4SLinus Torvalds int err = -EADDRNOTAVAIL; 11511da177e4SLinus Torvalds 11521da177e4SLinus Torvalds read_lock(&addrconf_lock); 11531da177e4SLinus Torvalds if ((idev = __in6_dev_get(dev)) != NULL) { 11541da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 11551da177e4SLinus Torvalds 11561da177e4SLinus Torvalds read_lock_bh(&idev->lock); 11571da177e4SLinus Torvalds for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) { 11581da177e4SLinus Torvalds if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) { 11591da177e4SLinus Torvalds ipv6_addr_copy(addr, &ifp->addr); 11601da177e4SLinus Torvalds err = 0; 11611da177e4SLinus Torvalds break; 11621da177e4SLinus Torvalds } 11631da177e4SLinus Torvalds } 11641da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 11651da177e4SLinus Torvalds } 11661da177e4SLinus Torvalds read_unlock(&addrconf_lock); 11671da177e4SLinus Torvalds return err; 11681da177e4SLinus Torvalds } 11691da177e4SLinus Torvalds 11701da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev) 11711da177e4SLinus Torvalds { 11721da177e4SLinus Torvalds int cnt = 0; 11731da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 11741da177e4SLinus Torvalds 11751da177e4SLinus Torvalds read_lock_bh(&idev->lock); 11761da177e4SLinus Torvalds for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) 11771da177e4SLinus Torvalds cnt++; 11781da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 11791da177e4SLinus Torvalds return cnt; 11801da177e4SLinus Torvalds } 11811da177e4SLinus Torvalds 11821da177e4SLinus Torvalds int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev, int strict) 11831da177e4SLinus Torvalds { 11841da177e4SLinus Torvalds struct inet6_ifaddr * ifp; 11851da177e4SLinus Torvalds u8 hash = ipv6_addr_hash(addr); 11861da177e4SLinus Torvalds 11871da177e4SLinus Torvalds read_lock_bh(&addrconf_hash_lock); 11881da177e4SLinus Torvalds for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) { 11891da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr) && 11901da177e4SLinus Torvalds !(ifp->flags&IFA_F_TENTATIVE)) { 11911da177e4SLinus Torvalds if (dev == NULL || ifp->idev->dev == dev || 11921da177e4SLinus Torvalds !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) 11931da177e4SLinus Torvalds break; 11941da177e4SLinus Torvalds } 11951da177e4SLinus Torvalds } 11961da177e4SLinus Torvalds read_unlock_bh(&addrconf_hash_lock); 11971da177e4SLinus Torvalds return ifp != NULL; 11981da177e4SLinus Torvalds } 11991da177e4SLinus Torvalds 12001da177e4SLinus Torvalds static 12011da177e4SLinus Torvalds int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev) 12021da177e4SLinus Torvalds { 12031da177e4SLinus Torvalds struct inet6_ifaddr * ifp; 12041da177e4SLinus Torvalds u8 hash = ipv6_addr_hash(addr); 12051da177e4SLinus Torvalds 12061da177e4SLinus Torvalds for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) { 12071da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr)) { 12081da177e4SLinus Torvalds if (dev == NULL || ifp->idev->dev == dev) 12091da177e4SLinus Torvalds break; 12101da177e4SLinus Torvalds } 12111da177e4SLinus Torvalds } 12121da177e4SLinus Torvalds return ifp != NULL; 12131da177e4SLinus Torvalds } 12141da177e4SLinus Torvalds 12151da177e4SLinus Torvalds struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev, int strict) 12161da177e4SLinus Torvalds { 12171da177e4SLinus Torvalds struct inet6_ifaddr * ifp; 12181da177e4SLinus Torvalds u8 hash = ipv6_addr_hash(addr); 12191da177e4SLinus Torvalds 12201da177e4SLinus Torvalds read_lock_bh(&addrconf_hash_lock); 12211da177e4SLinus Torvalds for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) { 12221da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr)) { 12231da177e4SLinus Torvalds if (dev == NULL || ifp->idev->dev == dev || 12241da177e4SLinus Torvalds !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) { 12251da177e4SLinus Torvalds in6_ifa_hold(ifp); 12261da177e4SLinus Torvalds break; 12271da177e4SLinus Torvalds } 12281da177e4SLinus Torvalds } 12291da177e4SLinus Torvalds } 12301da177e4SLinus Torvalds read_unlock_bh(&addrconf_hash_lock); 12311da177e4SLinus Torvalds 12321da177e4SLinus Torvalds return ifp; 12331da177e4SLinus Torvalds } 12341da177e4SLinus Torvalds 12351da177e4SLinus Torvalds int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2) 12361da177e4SLinus Torvalds { 12371da177e4SLinus Torvalds const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr; 12380fa1a53eSArnaldo Carvalho de Melo const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2); 12391da177e4SLinus Torvalds u32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr; 12408feaf0c0SArnaldo Carvalho de Melo u32 sk2_rcv_saddr = inet_rcv_saddr(sk2); 12411da177e4SLinus Torvalds int sk_ipv6only = ipv6_only_sock(sk); 1242463c84b9SArnaldo Carvalho de Melo int sk2_ipv6only = inet_v6_ipv6only(sk2); 12431da177e4SLinus Torvalds int addr_type = ipv6_addr_type(sk_rcv_saddr6); 12441da177e4SLinus Torvalds int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED; 12451da177e4SLinus Torvalds 12461da177e4SLinus Torvalds if (!sk2_rcv_saddr && !sk_ipv6only) 12471da177e4SLinus Torvalds return 1; 12481da177e4SLinus Torvalds 12491da177e4SLinus Torvalds if (addr_type2 == IPV6_ADDR_ANY && 12501da177e4SLinus Torvalds !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED)) 12511da177e4SLinus Torvalds return 1; 12521da177e4SLinus Torvalds 12531da177e4SLinus Torvalds if (addr_type == IPV6_ADDR_ANY && 12541da177e4SLinus Torvalds !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED)) 12551da177e4SLinus Torvalds return 1; 12561da177e4SLinus Torvalds 12571da177e4SLinus Torvalds if (sk2_rcv_saddr6 && 12581da177e4SLinus Torvalds ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6)) 12591da177e4SLinus Torvalds return 1; 12601da177e4SLinus Torvalds 12611da177e4SLinus Torvalds if (addr_type == IPV6_ADDR_MAPPED && 12621da177e4SLinus Torvalds !sk2_ipv6only && 12631da177e4SLinus Torvalds (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr)) 12641da177e4SLinus Torvalds return 1; 12651da177e4SLinus Torvalds 12661da177e4SLinus Torvalds return 0; 12671da177e4SLinus Torvalds } 12681da177e4SLinus Torvalds 12691da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */ 12701da177e4SLinus Torvalds 12719f5336e2SAdrian Bunk static void addrconf_dad_stop(struct inet6_ifaddr *ifp) 12721da177e4SLinus Torvalds { 12731da177e4SLinus Torvalds if (ifp->flags&IFA_F_PERMANENT) { 12741da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 12751da177e4SLinus Torvalds addrconf_del_timer(ifp); 12761da177e4SLinus Torvalds ifp->flags |= IFA_F_TENTATIVE; 12771da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 12781da177e4SLinus Torvalds in6_ifa_put(ifp); 12791da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 12801da177e4SLinus Torvalds } else if (ifp->flags&IFA_F_TEMPORARY) { 12811da177e4SLinus Torvalds struct inet6_ifaddr *ifpub; 12821da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 12831da177e4SLinus Torvalds ifpub = ifp->ifpub; 12841da177e4SLinus Torvalds if (ifpub) { 12851da177e4SLinus Torvalds in6_ifa_hold(ifpub); 12861da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 12871da177e4SLinus Torvalds ipv6_create_tempaddr(ifpub, ifp); 12881da177e4SLinus Torvalds in6_ifa_put(ifpub); 12891da177e4SLinus Torvalds } else { 12901da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 12911da177e4SLinus Torvalds } 12921da177e4SLinus Torvalds ipv6_del_addr(ifp); 12931da177e4SLinus Torvalds #endif 12941da177e4SLinus Torvalds } else 12951da177e4SLinus Torvalds ipv6_del_addr(ifp); 12961da177e4SLinus Torvalds } 12971da177e4SLinus Torvalds 12983c21edbdSYOSHIFUJI Hideaki void addrconf_dad_failure(struct inet6_ifaddr *ifp) 12993c21edbdSYOSHIFUJI Hideaki { 13003c21edbdSYOSHIFUJI Hideaki if (net_ratelimit()) 13013c21edbdSYOSHIFUJI Hideaki printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name); 13023c21edbdSYOSHIFUJI Hideaki addrconf_dad_stop(ifp); 13033c21edbdSYOSHIFUJI Hideaki } 13041da177e4SLinus Torvalds 13051da177e4SLinus Torvalds /* Join to solicited addr multicast group. */ 13061da177e4SLinus Torvalds 13071da177e4SLinus Torvalds void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr) 13081da177e4SLinus Torvalds { 13091da177e4SLinus Torvalds struct in6_addr maddr; 13101da177e4SLinus Torvalds 13111da177e4SLinus Torvalds if (dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 13121da177e4SLinus Torvalds return; 13131da177e4SLinus Torvalds 13141da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr); 13151da177e4SLinus Torvalds ipv6_dev_mc_inc(dev, &maddr); 13161da177e4SLinus Torvalds } 13171da177e4SLinus Torvalds 13181da177e4SLinus Torvalds void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr) 13191da177e4SLinus Torvalds { 13201da177e4SLinus Torvalds struct in6_addr maddr; 13211da177e4SLinus Torvalds 13221da177e4SLinus Torvalds if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 13231da177e4SLinus Torvalds return; 13241da177e4SLinus Torvalds 13251da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr); 13261da177e4SLinus Torvalds __ipv6_dev_mc_dec(idev, &maddr); 13271da177e4SLinus Torvalds } 13281da177e4SLinus Torvalds 132920380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp) 13301da177e4SLinus Torvalds { 13311da177e4SLinus Torvalds struct in6_addr addr; 13321da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 13331da177e4SLinus Torvalds if (ipv6_addr_any(&addr)) 13341da177e4SLinus Torvalds return; 13351da177e4SLinus Torvalds ipv6_dev_ac_inc(ifp->idev->dev, &addr); 13361da177e4SLinus Torvalds } 13371da177e4SLinus Torvalds 133820380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp) 13391da177e4SLinus Torvalds { 13401da177e4SLinus Torvalds struct in6_addr addr; 13411da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 13421da177e4SLinus Torvalds if (ipv6_addr_any(&addr)) 13431da177e4SLinus Torvalds return; 13441da177e4SLinus Torvalds __ipv6_dev_ac_dec(ifp->idev, &addr); 13451da177e4SLinus Torvalds } 13461da177e4SLinus Torvalds 1347073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev) 13481da177e4SLinus Torvalds { 13491da177e4SLinus Torvalds if (dev->addr_len != ETH_ALEN) 13501da177e4SLinus Torvalds return -1; 13511da177e4SLinus Torvalds memcpy(eui, dev->dev_addr, 3); 13521da177e4SLinus Torvalds memcpy(eui + 5, dev->dev_addr + 3, 3); 13531da177e4SLinus Torvalds 13541da177e4SLinus Torvalds /* 13551da177e4SLinus Torvalds * The zSeries OSA network cards can be shared among various 13561da177e4SLinus Torvalds * OS instances, but the OSA cards have only one MAC address. 13571da177e4SLinus Torvalds * This leads to duplicate address conflicts in conjunction 13581da177e4SLinus Torvalds * with IPv6 if more than one instance uses the same card. 13591da177e4SLinus Torvalds * 13601da177e4SLinus Torvalds * The driver for these cards can deliver a unique 16-bit 13611da177e4SLinus Torvalds * identifier for each instance sharing the same card. It is 13621da177e4SLinus Torvalds * placed instead of 0xFFFE in the interface identifier. The 13631da177e4SLinus Torvalds * "u" bit of the interface identifier is not inverted in this 13641da177e4SLinus Torvalds * case. Hence the resulting interface identifier has local 13651da177e4SLinus Torvalds * scope according to RFC2373. 13661da177e4SLinus Torvalds */ 13671da177e4SLinus Torvalds if (dev->dev_id) { 13681da177e4SLinus Torvalds eui[3] = (dev->dev_id >> 8) & 0xFF; 13691da177e4SLinus Torvalds eui[4] = dev->dev_id & 0xFF; 13701da177e4SLinus Torvalds } else { 13711da177e4SLinus Torvalds eui[3] = 0xFF; 13721da177e4SLinus Torvalds eui[4] = 0xFE; 13731da177e4SLinus Torvalds eui[0] ^= 2; 13741da177e4SLinus Torvalds } 13751da177e4SLinus Torvalds return 0; 1376073a8e0eSYOSHIFUJI Hideaki } 1377073a8e0eSYOSHIFUJI Hideaki 1378073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev) 1379073a8e0eSYOSHIFUJI Hideaki { 13801da177e4SLinus Torvalds /* XXX: inherit EUI-64 from other interface -- yoshfuji */ 13811da177e4SLinus Torvalds if (dev->addr_len != ARCNET_ALEN) 13821da177e4SLinus Torvalds return -1; 13831da177e4SLinus Torvalds memset(eui, 0, 7); 13841da177e4SLinus Torvalds eui[7] = *(u8*)dev->dev_addr; 13851da177e4SLinus Torvalds return 0; 1386073a8e0eSYOSHIFUJI Hideaki } 1387073a8e0eSYOSHIFUJI Hideaki 1388073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev) 1389073a8e0eSYOSHIFUJI Hideaki { 13901da177e4SLinus Torvalds if (dev->addr_len != INFINIBAND_ALEN) 13911da177e4SLinus Torvalds return -1; 13921da177e4SLinus Torvalds memcpy(eui, dev->dev_addr + 12, 8); 13931da177e4SLinus Torvalds eui[0] |= 2; 13941da177e4SLinus Torvalds return 0; 13951da177e4SLinus Torvalds } 1396073a8e0eSYOSHIFUJI Hideaki 1397073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev) 1398073a8e0eSYOSHIFUJI Hideaki { 1399073a8e0eSYOSHIFUJI Hideaki switch (dev->type) { 1400073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ETHER: 1401073a8e0eSYOSHIFUJI Hideaki case ARPHRD_FDDI: 1402073a8e0eSYOSHIFUJI Hideaki case ARPHRD_IEEE802_TR: 1403073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_eui48(eui, dev); 1404073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ARCNET: 1405073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_arcnet(eui, dev); 1406073a8e0eSYOSHIFUJI Hideaki case ARPHRD_INFINIBAND: 1407073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_infiniband(eui, dev); 1408073a8e0eSYOSHIFUJI Hideaki } 14091da177e4SLinus Torvalds return -1; 14101da177e4SLinus Torvalds } 14111da177e4SLinus Torvalds 14121da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev) 14131da177e4SLinus Torvalds { 14141da177e4SLinus Torvalds int err = -1; 14151da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 14161da177e4SLinus Torvalds 14171da177e4SLinus Torvalds read_lock_bh(&idev->lock); 14181da177e4SLinus Torvalds for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) { 14191da177e4SLinus Torvalds if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) { 14201da177e4SLinus Torvalds memcpy(eui, ifp->addr.s6_addr+8, 8); 14211da177e4SLinus Torvalds err = 0; 14221da177e4SLinus Torvalds break; 14231da177e4SLinus Torvalds } 14241da177e4SLinus Torvalds } 14251da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 14261da177e4SLinus Torvalds return err; 14271da177e4SLinus Torvalds } 14281da177e4SLinus Torvalds 14291da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 14301da177e4SLinus Torvalds /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */ 14311da177e4SLinus Torvalds static int __ipv6_regen_rndid(struct inet6_dev *idev) 14321da177e4SLinus Torvalds { 14331da177e4SLinus Torvalds regen: 1434955189efSYOSHIFUJI Hideaki get_random_bytes(idev->rndid, sizeof(idev->rndid)); 14351da177e4SLinus Torvalds idev->rndid[0] &= ~0x02; 14361da177e4SLinus Torvalds 14371da177e4SLinus Torvalds /* 14381da177e4SLinus Torvalds * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>: 14391da177e4SLinus Torvalds * check if generated address is not inappropriate 14401da177e4SLinus Torvalds * 14411da177e4SLinus Torvalds * - Reserved subnet anycast (RFC 2526) 14421da177e4SLinus Torvalds * 11111101 11....11 1xxxxxxx 14431da177e4SLinus Torvalds * - ISATAP (draft-ietf-ngtrans-isatap-13.txt) 5.1 14441da177e4SLinus Torvalds * 00-00-5E-FE-xx-xx-xx-xx 14451da177e4SLinus Torvalds * - value 0 14461da177e4SLinus Torvalds * - XXX: already assigned to an address on the device 14471da177e4SLinus Torvalds */ 14481da177e4SLinus Torvalds if (idev->rndid[0] == 0xfd && 14491da177e4SLinus Torvalds (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff && 14501da177e4SLinus Torvalds (idev->rndid[7]&0x80)) 14511da177e4SLinus Torvalds goto regen; 14521da177e4SLinus Torvalds if ((idev->rndid[0]|idev->rndid[1]) == 0) { 14531da177e4SLinus Torvalds if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe) 14541da177e4SLinus Torvalds goto regen; 14551da177e4SLinus Torvalds if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00) 14561da177e4SLinus Torvalds goto regen; 14571da177e4SLinus Torvalds } 14581da177e4SLinus Torvalds 14591da177e4SLinus Torvalds return 0; 14601da177e4SLinus Torvalds } 14611da177e4SLinus Torvalds 14621da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data) 14631da177e4SLinus Torvalds { 14641da177e4SLinus Torvalds struct inet6_dev *idev = (struct inet6_dev *) data; 14651da177e4SLinus Torvalds unsigned long expires; 14661da177e4SLinus Torvalds 14671da177e4SLinus Torvalds read_lock_bh(&addrconf_lock); 14681da177e4SLinus Torvalds write_lock_bh(&idev->lock); 14691da177e4SLinus Torvalds 14701da177e4SLinus Torvalds if (idev->dead) 14711da177e4SLinus Torvalds goto out; 14721da177e4SLinus Torvalds 14731da177e4SLinus Torvalds if (__ipv6_regen_rndid(idev) < 0) 14741da177e4SLinus Torvalds goto out; 14751da177e4SLinus Torvalds 14761da177e4SLinus Torvalds expires = jiffies + 14771da177e4SLinus Torvalds idev->cnf.temp_prefered_lft * HZ - 14781da177e4SLinus Torvalds idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor; 14791da177e4SLinus Torvalds if (time_before(expires, jiffies)) { 14801da177e4SLinus Torvalds printk(KERN_WARNING 14811da177e4SLinus Torvalds "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n", 14821da177e4SLinus Torvalds idev->dev->name); 14831da177e4SLinus Torvalds goto out; 14841da177e4SLinus Torvalds } 14851da177e4SLinus Torvalds 14861da177e4SLinus Torvalds if (!mod_timer(&idev->regen_timer, expires)) 14871da177e4SLinus Torvalds in6_dev_hold(idev); 14881da177e4SLinus Torvalds 14891da177e4SLinus Torvalds out: 14901da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 14911da177e4SLinus Torvalds read_unlock_bh(&addrconf_lock); 14921da177e4SLinus Torvalds in6_dev_put(idev); 14931da177e4SLinus Torvalds } 14941da177e4SLinus Torvalds 14951da177e4SLinus Torvalds static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) { 14961da177e4SLinus Torvalds int ret = 0; 14971da177e4SLinus Torvalds 14981da177e4SLinus Torvalds if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0) 14991da177e4SLinus Torvalds ret = __ipv6_regen_rndid(idev); 15001da177e4SLinus Torvalds return ret; 15011da177e4SLinus Torvalds } 15021da177e4SLinus Torvalds #endif 15031da177e4SLinus Torvalds 15041da177e4SLinus Torvalds /* 15051da177e4SLinus Torvalds * Add prefix route. 15061da177e4SLinus Torvalds */ 15071da177e4SLinus Torvalds 15081da177e4SLinus Torvalds static void 15091da177e4SLinus Torvalds addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev, 1510e431b8c0SJamal Hadi Salim unsigned long expires, u32 flags) 15111da177e4SLinus Torvalds { 151286872cb5SThomas Graf struct fib6_config cfg = { 151386872cb5SThomas Graf .fc_table = RT6_TABLE_PREFIX, 151486872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 151586872cb5SThomas Graf .fc_ifindex = dev->ifindex, 151686872cb5SThomas Graf .fc_expires = expires, 151786872cb5SThomas Graf .fc_dst_len = plen, 151886872cb5SThomas Graf .fc_flags = RTF_UP | flags, 151986872cb5SThomas Graf }; 15201da177e4SLinus Torvalds 152186872cb5SThomas Graf ipv6_addr_copy(&cfg.fc_dst, pfx); 15221da177e4SLinus Torvalds 15231da177e4SLinus Torvalds /* Prevent useless cloning on PtP SIT. 15241da177e4SLinus Torvalds This thing is done here expecting that the whole 15251da177e4SLinus Torvalds class of non-broadcast devices need not cloning. 15261da177e4SLinus Torvalds */ 15271da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT)) 152886872cb5SThomas Graf cfg.fc_flags |= RTF_NONEXTHOP; 15291da177e4SLinus Torvalds 153086872cb5SThomas Graf ip6_route_add(&cfg); 15311da177e4SLinus Torvalds } 15321da177e4SLinus Torvalds 15331da177e4SLinus Torvalds /* Create "default" multicast route to the interface */ 15341da177e4SLinus Torvalds 15351da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev) 15361da177e4SLinus Torvalds { 153786872cb5SThomas Graf struct fib6_config cfg = { 153886872cb5SThomas Graf .fc_table = RT6_TABLE_LOCAL, 153986872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 154086872cb5SThomas Graf .fc_ifindex = dev->ifindex, 154186872cb5SThomas Graf .fc_dst_len = 8, 154286872cb5SThomas Graf .fc_flags = RTF_UP, 154386872cb5SThomas Graf }; 15441da177e4SLinus Torvalds 154586872cb5SThomas Graf ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0); 154686872cb5SThomas Graf 154786872cb5SThomas Graf ip6_route_add(&cfg); 15481da177e4SLinus Torvalds } 15491da177e4SLinus Torvalds 15501da177e4SLinus Torvalds static void sit_route_add(struct net_device *dev) 15511da177e4SLinus Torvalds { 155286872cb5SThomas Graf struct fib6_config cfg = { 155386872cb5SThomas Graf .fc_table = RT6_TABLE_MAIN, 155486872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 155586872cb5SThomas Graf .fc_ifindex = dev->ifindex, 155686872cb5SThomas Graf .fc_dst_len = 96, 155786872cb5SThomas Graf .fc_flags = RTF_UP | RTF_NONEXTHOP, 155886872cb5SThomas Graf }; 15591da177e4SLinus Torvalds 15601da177e4SLinus Torvalds /* prefix length - 96 bits "::d.d.d.d" */ 156186872cb5SThomas Graf ip6_route_add(&cfg); 15621da177e4SLinus Torvalds } 15631da177e4SLinus Torvalds 15641da177e4SLinus Torvalds static void addrconf_add_lroute(struct net_device *dev) 15651da177e4SLinus Torvalds { 15661da177e4SLinus Torvalds struct in6_addr addr; 15671da177e4SLinus Torvalds 15681da177e4SLinus Torvalds ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0); 15691da177e4SLinus Torvalds addrconf_prefix_route(&addr, 64, dev, 0, 0); 15701da177e4SLinus Torvalds } 15711da177e4SLinus Torvalds 15721da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev) 15731da177e4SLinus Torvalds { 15741da177e4SLinus Torvalds struct inet6_dev *idev; 15751da177e4SLinus Torvalds 15761da177e4SLinus Torvalds ASSERT_RTNL(); 15771da177e4SLinus Torvalds 15781da177e4SLinus Torvalds if ((idev = ipv6_find_idev(dev)) == NULL) 15791da177e4SLinus Torvalds return NULL; 15801da177e4SLinus Torvalds 15811da177e4SLinus Torvalds /* Add default multicast route */ 15821da177e4SLinus Torvalds addrconf_add_mroute(dev); 15831da177e4SLinus Torvalds 15841da177e4SLinus Torvalds /* Add link local route */ 15851da177e4SLinus Torvalds addrconf_add_lroute(dev); 15861da177e4SLinus Torvalds return idev; 15871da177e4SLinus Torvalds } 15881da177e4SLinus Torvalds 15891da177e4SLinus Torvalds void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len) 15901da177e4SLinus Torvalds { 15911da177e4SLinus Torvalds struct prefix_info *pinfo; 15921da177e4SLinus Torvalds __u32 valid_lft; 15931da177e4SLinus Torvalds __u32 prefered_lft; 15941da177e4SLinus Torvalds int addr_type; 15951da177e4SLinus Torvalds unsigned long rt_expires; 15961da177e4SLinus Torvalds struct inet6_dev *in6_dev; 15971da177e4SLinus Torvalds 15981da177e4SLinus Torvalds pinfo = (struct prefix_info *) opt; 15991da177e4SLinus Torvalds 16001da177e4SLinus Torvalds if (len < sizeof(struct prefix_info)) { 16011da177e4SLinus Torvalds ADBG(("addrconf: prefix option too short\n")); 16021da177e4SLinus Torvalds return; 16031da177e4SLinus Torvalds } 16041da177e4SLinus Torvalds 16051da177e4SLinus Torvalds /* 16061da177e4SLinus Torvalds * Validation checks ([ADDRCONF], page 19) 16071da177e4SLinus Torvalds */ 16081da177e4SLinus Torvalds 16091da177e4SLinus Torvalds addr_type = ipv6_addr_type(&pinfo->prefix); 16101da177e4SLinus Torvalds 16111da177e4SLinus Torvalds if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)) 16121da177e4SLinus Torvalds return; 16131da177e4SLinus Torvalds 16141da177e4SLinus Torvalds valid_lft = ntohl(pinfo->valid); 16151da177e4SLinus Torvalds prefered_lft = ntohl(pinfo->prefered); 16161da177e4SLinus Torvalds 16171da177e4SLinus Torvalds if (prefered_lft > valid_lft) { 16181da177e4SLinus Torvalds if (net_ratelimit()) 16191da177e4SLinus Torvalds printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n"); 16201da177e4SLinus Torvalds return; 16211da177e4SLinus Torvalds } 16221da177e4SLinus Torvalds 16231da177e4SLinus Torvalds in6_dev = in6_dev_get(dev); 16241da177e4SLinus Torvalds 16251da177e4SLinus Torvalds if (in6_dev == NULL) { 16261da177e4SLinus Torvalds if (net_ratelimit()) 16271da177e4SLinus Torvalds printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name); 16281da177e4SLinus Torvalds return; 16291da177e4SLinus Torvalds } 16301da177e4SLinus Torvalds 16311da177e4SLinus Torvalds /* 16321da177e4SLinus Torvalds * Two things going on here: 16331da177e4SLinus Torvalds * 1) Add routes for on-link prefixes 16341da177e4SLinus Torvalds * 2) Configure prefixes with the auto flag set 16351da177e4SLinus Torvalds */ 16361da177e4SLinus Torvalds 16371da177e4SLinus Torvalds /* Avoid arithmetic overflow. Really, we could 16381da177e4SLinus Torvalds save rt_expires in seconds, likely valid_lft, 16391da177e4SLinus Torvalds but it would require division in fib gc, that it 16401da177e4SLinus Torvalds not good. 16411da177e4SLinus Torvalds */ 16421da177e4SLinus Torvalds if (valid_lft >= 0x7FFFFFFF/HZ) 16433dd4bc68SYOSHIFUJI Hideaki rt_expires = 0x7FFFFFFF - (0x7FFFFFFF % HZ); 16441da177e4SLinus Torvalds else 16453dd4bc68SYOSHIFUJI Hideaki rt_expires = valid_lft * HZ; 16463dd4bc68SYOSHIFUJI Hideaki 16473dd4bc68SYOSHIFUJI Hideaki /* 16483dd4bc68SYOSHIFUJI Hideaki * We convert this (in jiffies) to clock_t later. 16493dd4bc68SYOSHIFUJI Hideaki * Avoid arithmetic overflow there as well. 16503dd4bc68SYOSHIFUJI Hideaki * Overflow can happen only if HZ < USER_HZ. 16513dd4bc68SYOSHIFUJI Hideaki */ 16523dd4bc68SYOSHIFUJI Hideaki if (HZ < USER_HZ && rt_expires > 0x7FFFFFFF / USER_HZ) 16533dd4bc68SYOSHIFUJI Hideaki rt_expires = 0x7FFFFFFF / USER_HZ; 16541da177e4SLinus Torvalds 16551da177e4SLinus Torvalds if (pinfo->onlink) { 16561da177e4SLinus Torvalds struct rt6_info *rt; 16571da177e4SLinus Torvalds rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1); 16581da177e4SLinus Torvalds 16591da177e4SLinus Torvalds if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) { 16601da177e4SLinus Torvalds if (rt->rt6i_flags&RTF_EXPIRES) { 16611da177e4SLinus Torvalds if (valid_lft == 0) { 1662e0a1ad73SThomas Graf ip6_del_rt(rt); 16631da177e4SLinus Torvalds rt = NULL; 16641da177e4SLinus Torvalds } else { 16653dd4bc68SYOSHIFUJI Hideaki rt->rt6i_expires = jiffies + rt_expires; 16661da177e4SLinus Torvalds } 16671da177e4SLinus Torvalds } 16681da177e4SLinus Torvalds } else if (valid_lft) { 16691da177e4SLinus Torvalds addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len, 16703dd4bc68SYOSHIFUJI Hideaki dev, jiffies_to_clock_t(rt_expires), RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT); 16711da177e4SLinus Torvalds } 16721da177e4SLinus Torvalds if (rt) 16731da177e4SLinus Torvalds dst_release(&rt->u.dst); 16741da177e4SLinus Torvalds } 16751da177e4SLinus Torvalds 16761da177e4SLinus Torvalds /* Try to figure out our local address for this prefix */ 16771da177e4SLinus Torvalds 16781da177e4SLinus Torvalds if (pinfo->autoconf && in6_dev->cnf.autoconf) { 16791da177e4SLinus Torvalds struct inet6_ifaddr * ifp; 16801da177e4SLinus Torvalds struct in6_addr addr; 16811da177e4SLinus Torvalds int create = 0, update_lft = 0; 16821da177e4SLinus Torvalds 16831da177e4SLinus Torvalds if (pinfo->prefix_len == 64) { 16841da177e4SLinus Torvalds memcpy(&addr, &pinfo->prefix, 8); 16851da177e4SLinus Torvalds if (ipv6_generate_eui64(addr.s6_addr + 8, dev) && 16861da177e4SLinus Torvalds ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) { 16871da177e4SLinus Torvalds in6_dev_put(in6_dev); 16881da177e4SLinus Torvalds return; 16891da177e4SLinus Torvalds } 16901da177e4SLinus Torvalds goto ok; 16911da177e4SLinus Torvalds } 16921da177e4SLinus Torvalds if (net_ratelimit()) 16931da177e4SLinus Torvalds printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n", 16941da177e4SLinus Torvalds pinfo->prefix_len); 16951da177e4SLinus Torvalds in6_dev_put(in6_dev); 16961da177e4SLinus Torvalds return; 16971da177e4SLinus Torvalds 16981da177e4SLinus Torvalds ok: 16991da177e4SLinus Torvalds 17001da177e4SLinus Torvalds ifp = ipv6_get_ifaddr(&addr, dev, 1); 17011da177e4SLinus Torvalds 17021da177e4SLinus Torvalds if (ifp == NULL && valid_lft) { 17031da177e4SLinus Torvalds int max_addresses = in6_dev->cnf.max_addresses; 17041da177e4SLinus Torvalds 17051da177e4SLinus Torvalds /* Do not allow to create too much of autoconfigured 17061da177e4SLinus Torvalds * addresses; this would be too easy way to crash kernel. 17071da177e4SLinus Torvalds */ 17081da177e4SLinus Torvalds if (!max_addresses || 17091da177e4SLinus Torvalds ipv6_count_addresses(in6_dev) < max_addresses) 17101da177e4SLinus Torvalds ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len, 17111da177e4SLinus Torvalds addr_type&IPV6_ADDR_SCOPE_MASK, 0); 17121da177e4SLinus Torvalds 17131da177e4SLinus Torvalds if (!ifp || IS_ERR(ifp)) { 17141da177e4SLinus Torvalds in6_dev_put(in6_dev); 17151da177e4SLinus Torvalds return; 17161da177e4SLinus Torvalds } 17171da177e4SLinus Torvalds 17181da177e4SLinus Torvalds update_lft = create = 1; 17191da177e4SLinus Torvalds ifp->cstamp = jiffies; 17201da177e4SLinus Torvalds addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT); 17211da177e4SLinus Torvalds } 17221da177e4SLinus Torvalds 17231da177e4SLinus Torvalds if (ifp) { 17241da177e4SLinus Torvalds int flags; 17251da177e4SLinus Torvalds unsigned long now; 17261da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 17271da177e4SLinus Torvalds struct inet6_ifaddr *ift; 17281da177e4SLinus Torvalds #endif 17291da177e4SLinus Torvalds u32 stored_lft; 17301da177e4SLinus Torvalds 17311da177e4SLinus Torvalds /* update lifetime (RFC2462 5.5.3 e) */ 17321da177e4SLinus Torvalds spin_lock(&ifp->lock); 17331da177e4SLinus Torvalds now = jiffies; 17341da177e4SLinus Torvalds if (ifp->valid_lft > (now - ifp->tstamp) / HZ) 17351da177e4SLinus Torvalds stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ; 17361da177e4SLinus Torvalds else 17371da177e4SLinus Torvalds stored_lft = 0; 17381da177e4SLinus Torvalds if (!update_lft && stored_lft) { 17391da177e4SLinus Torvalds if (valid_lft > MIN_VALID_LIFETIME || 17401da177e4SLinus Torvalds valid_lft > stored_lft) 17411da177e4SLinus Torvalds update_lft = 1; 17421da177e4SLinus Torvalds else if (stored_lft <= MIN_VALID_LIFETIME) { 17431da177e4SLinus Torvalds /* valid_lft <= stored_lft is always true */ 17441da177e4SLinus Torvalds /* XXX: IPsec */ 17451da177e4SLinus Torvalds update_lft = 0; 17461da177e4SLinus Torvalds } else { 17471da177e4SLinus Torvalds valid_lft = MIN_VALID_LIFETIME; 17481da177e4SLinus Torvalds if (valid_lft < prefered_lft) 17491da177e4SLinus Torvalds prefered_lft = valid_lft; 17501da177e4SLinus Torvalds update_lft = 1; 17511da177e4SLinus Torvalds } 17521da177e4SLinus Torvalds } 17531da177e4SLinus Torvalds 17541da177e4SLinus Torvalds if (update_lft) { 17551da177e4SLinus Torvalds ifp->valid_lft = valid_lft; 17561da177e4SLinus Torvalds ifp->prefered_lft = prefered_lft; 17571da177e4SLinus Torvalds ifp->tstamp = now; 17581da177e4SLinus Torvalds flags = ifp->flags; 17591da177e4SLinus Torvalds ifp->flags &= ~IFA_F_DEPRECATED; 17601da177e4SLinus Torvalds spin_unlock(&ifp->lock); 17611da177e4SLinus Torvalds 17621da177e4SLinus Torvalds if (!(flags&IFA_F_TENTATIVE)) 17631da177e4SLinus Torvalds ipv6_ifa_notify(0, ifp); 17641da177e4SLinus Torvalds } else 17651da177e4SLinus Torvalds spin_unlock(&ifp->lock); 17661da177e4SLinus Torvalds 17671da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 17681da177e4SLinus Torvalds read_lock_bh(&in6_dev->lock); 17691da177e4SLinus Torvalds /* update all temporary addresses in the list */ 17701da177e4SLinus Torvalds for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) { 17711da177e4SLinus Torvalds /* 17721da177e4SLinus Torvalds * When adjusting the lifetimes of an existing 17731da177e4SLinus Torvalds * temporary address, only lower the lifetimes. 17741da177e4SLinus Torvalds * Implementations must not increase the 17751da177e4SLinus Torvalds * lifetimes of an existing temporary address 17761da177e4SLinus Torvalds * when processing a Prefix Information Option. 17771da177e4SLinus Torvalds */ 17781da177e4SLinus Torvalds spin_lock(&ift->lock); 17791da177e4SLinus Torvalds flags = ift->flags; 17801da177e4SLinus Torvalds if (ift->valid_lft > valid_lft && 17811da177e4SLinus Torvalds ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ) 17821da177e4SLinus Torvalds ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ; 17831da177e4SLinus Torvalds if (ift->prefered_lft > prefered_lft && 17841da177e4SLinus Torvalds ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ) 17851da177e4SLinus Torvalds ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ; 17861da177e4SLinus Torvalds spin_unlock(&ift->lock); 17871da177e4SLinus Torvalds if (!(flags&IFA_F_TENTATIVE)) 17881da177e4SLinus Torvalds ipv6_ifa_notify(0, ift); 17891da177e4SLinus Torvalds } 17901da177e4SLinus Torvalds 17911da177e4SLinus Torvalds if (create && in6_dev->cnf.use_tempaddr > 0) { 17921da177e4SLinus Torvalds /* 17931da177e4SLinus Torvalds * When a new public address is created as described in [ADDRCONF], 17941da177e4SLinus Torvalds * also create a new temporary address. 17951da177e4SLinus Torvalds */ 17961da177e4SLinus Torvalds read_unlock_bh(&in6_dev->lock); 17971da177e4SLinus Torvalds ipv6_create_tempaddr(ifp, NULL); 17981da177e4SLinus Torvalds } else { 17991da177e4SLinus Torvalds read_unlock_bh(&in6_dev->lock); 18001da177e4SLinus Torvalds } 18011da177e4SLinus Torvalds #endif 18021da177e4SLinus Torvalds in6_ifa_put(ifp); 18031da177e4SLinus Torvalds addrconf_verify(0); 18041da177e4SLinus Torvalds } 18051da177e4SLinus Torvalds } 18061da177e4SLinus Torvalds inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo); 18071da177e4SLinus Torvalds in6_dev_put(in6_dev); 18081da177e4SLinus Torvalds } 18091da177e4SLinus Torvalds 18101da177e4SLinus Torvalds /* 18111da177e4SLinus Torvalds * Set destination address. 18121da177e4SLinus Torvalds * Special case for SIT interfaces where we create a new "virtual" 18131da177e4SLinus Torvalds * device. 18141da177e4SLinus Torvalds */ 18151da177e4SLinus Torvalds int addrconf_set_dstaddr(void __user *arg) 18161da177e4SLinus Torvalds { 18171da177e4SLinus Torvalds struct in6_ifreq ireq; 18181da177e4SLinus Torvalds struct net_device *dev; 18191da177e4SLinus Torvalds int err = -EINVAL; 18201da177e4SLinus Torvalds 18211da177e4SLinus Torvalds rtnl_lock(); 18221da177e4SLinus Torvalds 18231da177e4SLinus Torvalds err = -EFAULT; 18241da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 18251da177e4SLinus Torvalds goto err_exit; 18261da177e4SLinus Torvalds 18271da177e4SLinus Torvalds dev = __dev_get_by_index(ireq.ifr6_ifindex); 18281da177e4SLinus Torvalds 18291da177e4SLinus Torvalds err = -ENODEV; 18301da177e4SLinus Torvalds if (dev == NULL) 18311da177e4SLinus Torvalds goto err_exit; 18321da177e4SLinus Torvalds 18331da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT) { 18341da177e4SLinus Torvalds struct ifreq ifr; 18351da177e4SLinus Torvalds mm_segment_t oldfs; 18361da177e4SLinus Torvalds struct ip_tunnel_parm p; 18371da177e4SLinus Torvalds 18381da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 18391da177e4SLinus Torvalds if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4)) 18401da177e4SLinus Torvalds goto err_exit; 18411da177e4SLinus Torvalds 18421da177e4SLinus Torvalds memset(&p, 0, sizeof(p)); 18431da177e4SLinus Torvalds p.iph.daddr = ireq.ifr6_addr.s6_addr32[3]; 18441da177e4SLinus Torvalds p.iph.saddr = 0; 18451da177e4SLinus Torvalds p.iph.version = 4; 18461da177e4SLinus Torvalds p.iph.ihl = 5; 18471da177e4SLinus Torvalds p.iph.protocol = IPPROTO_IPV6; 18481da177e4SLinus Torvalds p.iph.ttl = 64; 18491da177e4SLinus Torvalds ifr.ifr_ifru.ifru_data = (void __user *)&p; 18501da177e4SLinus Torvalds 18511da177e4SLinus Torvalds oldfs = get_fs(); set_fs(KERNEL_DS); 18521da177e4SLinus Torvalds err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL); 18531da177e4SLinus Torvalds set_fs(oldfs); 18541da177e4SLinus Torvalds 18551da177e4SLinus Torvalds if (err == 0) { 18561da177e4SLinus Torvalds err = -ENOBUFS; 18571da177e4SLinus Torvalds if ((dev = __dev_get_by_name(p.name)) == NULL) 18581da177e4SLinus Torvalds goto err_exit; 18591da177e4SLinus Torvalds err = dev_open(dev); 18601da177e4SLinus Torvalds } 18611da177e4SLinus Torvalds } 18621da177e4SLinus Torvalds 18631da177e4SLinus Torvalds err_exit: 18641da177e4SLinus Torvalds rtnl_unlock(); 18651da177e4SLinus Torvalds return err; 18661da177e4SLinus Torvalds } 18671da177e4SLinus Torvalds 18681da177e4SLinus Torvalds /* 18691da177e4SLinus Torvalds * Manual configuration of address on an interface 18701da177e4SLinus Torvalds */ 18710778769dSNoriaki TAKAMIYA static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen, 18720778769dSNoriaki TAKAMIYA __u32 prefered_lft, __u32 valid_lft) 18731da177e4SLinus Torvalds { 18741da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 18751da177e4SLinus Torvalds struct inet6_dev *idev; 18761da177e4SLinus Torvalds struct net_device *dev; 18770778769dSNoriaki TAKAMIYA __u8 ifa_flags = 0; 18781da177e4SLinus Torvalds int scope; 18791da177e4SLinus Torvalds 18801da177e4SLinus Torvalds ASSERT_RTNL(); 18811da177e4SLinus Torvalds 18820778769dSNoriaki TAKAMIYA /* check the lifetime */ 18830778769dSNoriaki TAKAMIYA if (!valid_lft || prefered_lft > valid_lft) 18840778769dSNoriaki TAKAMIYA return -EINVAL; 18850778769dSNoriaki TAKAMIYA 18861da177e4SLinus Torvalds if ((dev = __dev_get_by_index(ifindex)) == NULL) 18871da177e4SLinus Torvalds return -ENODEV; 18881da177e4SLinus Torvalds 18891da177e4SLinus Torvalds if (!(dev->flags&IFF_UP)) 18901da177e4SLinus Torvalds return -ENETDOWN; 18911da177e4SLinus Torvalds 18921da177e4SLinus Torvalds if ((idev = addrconf_add_dev(dev)) == NULL) 18931da177e4SLinus Torvalds return -ENOBUFS; 18941da177e4SLinus Torvalds 18951da177e4SLinus Torvalds scope = ipv6_addr_scope(pfx); 18961da177e4SLinus Torvalds 18970778769dSNoriaki TAKAMIYA if (valid_lft == INFINITY_LIFE_TIME) 18980778769dSNoriaki TAKAMIYA ifa_flags |= IFA_F_PERMANENT; 18990778769dSNoriaki TAKAMIYA else if (valid_lft >= 0x7FFFFFFF/HZ) 19000778769dSNoriaki TAKAMIYA valid_lft = 0x7FFFFFFF/HZ; 19010778769dSNoriaki TAKAMIYA 19020778769dSNoriaki TAKAMIYA if (prefered_lft == 0) 19030778769dSNoriaki TAKAMIYA ifa_flags |= IFA_F_DEPRECATED; 19040778769dSNoriaki TAKAMIYA else if ((prefered_lft >= 0x7FFFFFFF/HZ) && 19050778769dSNoriaki TAKAMIYA (prefered_lft != INFINITY_LIFE_TIME)) 19060778769dSNoriaki TAKAMIYA prefered_lft = 0x7FFFFFFF/HZ; 19070778769dSNoriaki TAKAMIYA 19080778769dSNoriaki TAKAMIYA ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags); 19090778769dSNoriaki TAKAMIYA 19101da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 191106aebfb7SHerbert Xu spin_lock_bh(&ifp->lock); 19120778769dSNoriaki TAKAMIYA ifp->valid_lft = valid_lft; 19130778769dSNoriaki TAKAMIYA ifp->prefered_lft = prefered_lft; 19140778769dSNoriaki TAKAMIYA ifp->tstamp = jiffies; 191506aebfb7SHerbert Xu spin_unlock_bh(&ifp->lock); 19160778769dSNoriaki TAKAMIYA 19171da177e4SLinus Torvalds addrconf_dad_start(ifp, 0); 19181da177e4SLinus Torvalds in6_ifa_put(ifp); 19190778769dSNoriaki TAKAMIYA addrconf_verify(0); 19201da177e4SLinus Torvalds return 0; 19211da177e4SLinus Torvalds } 19221da177e4SLinus Torvalds 19231da177e4SLinus Torvalds return PTR_ERR(ifp); 19241da177e4SLinus Torvalds } 19251da177e4SLinus Torvalds 19261da177e4SLinus Torvalds static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen) 19271da177e4SLinus Torvalds { 19281da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 19291da177e4SLinus Torvalds struct inet6_dev *idev; 19301da177e4SLinus Torvalds struct net_device *dev; 19311da177e4SLinus Torvalds 19321da177e4SLinus Torvalds if ((dev = __dev_get_by_index(ifindex)) == NULL) 19331da177e4SLinus Torvalds return -ENODEV; 19341da177e4SLinus Torvalds 19351da177e4SLinus Torvalds if ((idev = __in6_dev_get(dev)) == NULL) 19361da177e4SLinus Torvalds return -ENXIO; 19371da177e4SLinus Torvalds 19381da177e4SLinus Torvalds read_lock_bh(&idev->lock); 19391da177e4SLinus Torvalds for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) { 19401da177e4SLinus Torvalds if (ifp->prefix_len == plen && 19411da177e4SLinus Torvalds ipv6_addr_equal(pfx, &ifp->addr)) { 19421da177e4SLinus Torvalds in6_ifa_hold(ifp); 19431da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 19441da177e4SLinus Torvalds 19451da177e4SLinus Torvalds ipv6_del_addr(ifp); 19461da177e4SLinus Torvalds 19471da177e4SLinus Torvalds /* If the last address is deleted administratively, 19481da177e4SLinus Torvalds disable IPv6 on this interface. 19491da177e4SLinus Torvalds */ 19501da177e4SLinus Torvalds if (idev->addr_list == NULL) 19511da177e4SLinus Torvalds addrconf_ifdown(idev->dev, 1); 19521da177e4SLinus Torvalds return 0; 19531da177e4SLinus Torvalds } 19541da177e4SLinus Torvalds } 19551da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 19561da177e4SLinus Torvalds return -EADDRNOTAVAIL; 19571da177e4SLinus Torvalds } 19581da177e4SLinus Torvalds 19591da177e4SLinus Torvalds 19601da177e4SLinus Torvalds int addrconf_add_ifaddr(void __user *arg) 19611da177e4SLinus Torvalds { 19621da177e4SLinus Torvalds struct in6_ifreq ireq; 19631da177e4SLinus Torvalds int err; 19641da177e4SLinus Torvalds 19651da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 19661da177e4SLinus Torvalds return -EPERM; 19671da177e4SLinus Torvalds 19681da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 19691da177e4SLinus Torvalds return -EFAULT; 19701da177e4SLinus Torvalds 19711da177e4SLinus Torvalds rtnl_lock(); 19720778769dSNoriaki TAKAMIYA err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen, 19730778769dSNoriaki TAKAMIYA INFINITY_LIFE_TIME, INFINITY_LIFE_TIME); 19741da177e4SLinus Torvalds rtnl_unlock(); 19751da177e4SLinus Torvalds return err; 19761da177e4SLinus Torvalds } 19771da177e4SLinus Torvalds 19781da177e4SLinus Torvalds int addrconf_del_ifaddr(void __user *arg) 19791da177e4SLinus Torvalds { 19801da177e4SLinus Torvalds struct in6_ifreq ireq; 19811da177e4SLinus Torvalds int err; 19821da177e4SLinus Torvalds 19831da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 19841da177e4SLinus Torvalds return -EPERM; 19851da177e4SLinus Torvalds 19861da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 19871da177e4SLinus Torvalds return -EFAULT; 19881da177e4SLinus Torvalds 19891da177e4SLinus Torvalds rtnl_lock(); 19901da177e4SLinus Torvalds err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen); 19911da177e4SLinus Torvalds rtnl_unlock(); 19921da177e4SLinus Torvalds return err; 19931da177e4SLinus Torvalds } 19941da177e4SLinus Torvalds 19951da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev) 19961da177e4SLinus Torvalds { 19971da177e4SLinus Torvalds struct inet6_ifaddr * ifp; 19981da177e4SLinus Torvalds struct in6_addr addr; 19991da177e4SLinus Torvalds struct net_device *dev; 20001da177e4SLinus Torvalds int scope; 20011da177e4SLinus Torvalds 20021da177e4SLinus Torvalds ASSERT_RTNL(); 20031da177e4SLinus Torvalds 20041da177e4SLinus Torvalds memset(&addr, 0, sizeof(struct in6_addr)); 20051da177e4SLinus Torvalds memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4); 20061da177e4SLinus Torvalds 20071da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) { 20081da177e4SLinus Torvalds addr.s6_addr32[0] = htonl(0xfe800000); 20091da177e4SLinus Torvalds scope = IFA_LINK; 20101da177e4SLinus Torvalds } else { 20111da177e4SLinus Torvalds scope = IPV6_ADDR_COMPATv4; 20121da177e4SLinus Torvalds } 20131da177e4SLinus Torvalds 20141da177e4SLinus Torvalds if (addr.s6_addr32[3]) { 20151da177e4SLinus Torvalds ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT); 20161da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 20171da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 20181da177e4SLinus Torvalds ifp->flags &= ~IFA_F_TENTATIVE; 20191da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 20201da177e4SLinus Torvalds ipv6_ifa_notify(RTM_NEWADDR, ifp); 20211da177e4SLinus Torvalds in6_ifa_put(ifp); 20221da177e4SLinus Torvalds } 20231da177e4SLinus Torvalds return; 20241da177e4SLinus Torvalds } 20251da177e4SLinus Torvalds 20261da177e4SLinus Torvalds for (dev = dev_base; dev != NULL; dev = dev->next) { 2027e5ed6399SHerbert Xu struct in_device * in_dev = __in_dev_get_rtnl(dev); 20281da177e4SLinus Torvalds if (in_dev && (dev->flags & IFF_UP)) { 20291da177e4SLinus Torvalds struct in_ifaddr * ifa; 20301da177e4SLinus Torvalds 20311da177e4SLinus Torvalds int flag = scope; 20321da177e4SLinus Torvalds 20331da177e4SLinus Torvalds for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) { 20341da177e4SLinus Torvalds int plen; 20351da177e4SLinus Torvalds 20361da177e4SLinus Torvalds addr.s6_addr32[3] = ifa->ifa_local; 20371da177e4SLinus Torvalds 20381da177e4SLinus Torvalds if (ifa->ifa_scope == RT_SCOPE_LINK) 20391da177e4SLinus Torvalds continue; 20401da177e4SLinus Torvalds if (ifa->ifa_scope >= RT_SCOPE_HOST) { 20411da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) 20421da177e4SLinus Torvalds continue; 20431da177e4SLinus Torvalds flag |= IFA_HOST; 20441da177e4SLinus Torvalds } 20451da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) 20461da177e4SLinus Torvalds plen = 64; 20471da177e4SLinus Torvalds else 20481da177e4SLinus Torvalds plen = 96; 20491da177e4SLinus Torvalds 20501da177e4SLinus Torvalds ifp = ipv6_add_addr(idev, &addr, plen, flag, 20511da177e4SLinus Torvalds IFA_F_PERMANENT); 20521da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 20531da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 20541da177e4SLinus Torvalds ifp->flags &= ~IFA_F_TENTATIVE; 20551da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 20561da177e4SLinus Torvalds ipv6_ifa_notify(RTM_NEWADDR, ifp); 20571da177e4SLinus Torvalds in6_ifa_put(ifp); 20581da177e4SLinus Torvalds } 20591da177e4SLinus Torvalds } 20601da177e4SLinus Torvalds } 20611da177e4SLinus Torvalds } 20621da177e4SLinus Torvalds } 20631da177e4SLinus Torvalds 20641da177e4SLinus Torvalds static void init_loopback(struct net_device *dev) 20651da177e4SLinus Torvalds { 20661da177e4SLinus Torvalds struct inet6_dev *idev; 20671da177e4SLinus Torvalds struct inet6_ifaddr * ifp; 20681da177e4SLinus Torvalds 20691da177e4SLinus Torvalds /* ::1 */ 20701da177e4SLinus Torvalds 20711da177e4SLinus Torvalds ASSERT_RTNL(); 20721da177e4SLinus Torvalds 20731da177e4SLinus Torvalds if ((idev = ipv6_find_idev(dev)) == NULL) { 20741da177e4SLinus Torvalds printk(KERN_DEBUG "init loopback: add_dev failed\n"); 20751da177e4SLinus Torvalds return; 20761da177e4SLinus Torvalds } 20771da177e4SLinus Torvalds 20781da177e4SLinus Torvalds ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT); 20791da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 20801da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 20811da177e4SLinus Torvalds ifp->flags &= ~IFA_F_TENTATIVE; 20821da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 20831da177e4SLinus Torvalds ipv6_ifa_notify(RTM_NEWADDR, ifp); 20841da177e4SLinus Torvalds in6_ifa_put(ifp); 20851da177e4SLinus Torvalds } 20861da177e4SLinus Torvalds } 20871da177e4SLinus Torvalds 20881da177e4SLinus Torvalds static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr) 20891da177e4SLinus Torvalds { 20901da177e4SLinus Torvalds struct inet6_ifaddr * ifp; 20911da177e4SLinus Torvalds 20921da177e4SLinus Torvalds ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, IFA_F_PERMANENT); 20931da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 20941da177e4SLinus Torvalds addrconf_dad_start(ifp, 0); 20951da177e4SLinus Torvalds in6_ifa_put(ifp); 20961da177e4SLinus Torvalds } 20971da177e4SLinus Torvalds } 20981da177e4SLinus Torvalds 20991da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev) 21001da177e4SLinus Torvalds { 21011da177e4SLinus Torvalds struct in6_addr addr; 21021da177e4SLinus Torvalds struct inet6_dev * idev; 21031da177e4SLinus Torvalds 21041da177e4SLinus Torvalds ASSERT_RTNL(); 21051da177e4SLinus Torvalds 21061da177e4SLinus Torvalds if ((dev->type != ARPHRD_ETHER) && 21071da177e4SLinus Torvalds (dev->type != ARPHRD_FDDI) && 21081da177e4SLinus Torvalds (dev->type != ARPHRD_IEEE802_TR) && 21091da177e4SLinus Torvalds (dev->type != ARPHRD_ARCNET) && 21101da177e4SLinus Torvalds (dev->type != ARPHRD_INFINIBAND)) { 21111da177e4SLinus Torvalds /* Alas, we support only Ethernet autoconfiguration. */ 21121da177e4SLinus Torvalds return; 21131da177e4SLinus Torvalds } 21141da177e4SLinus Torvalds 21151da177e4SLinus Torvalds idev = addrconf_add_dev(dev); 21161da177e4SLinus Torvalds if (idev == NULL) 21171da177e4SLinus Torvalds return; 21181da177e4SLinus Torvalds 21191da177e4SLinus Torvalds memset(&addr, 0, sizeof(struct in6_addr)); 21201da177e4SLinus Torvalds addr.s6_addr32[0] = htonl(0xFE800000); 21211da177e4SLinus Torvalds 21221da177e4SLinus Torvalds if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0) 21231da177e4SLinus Torvalds addrconf_add_linklocal(idev, &addr); 21241da177e4SLinus Torvalds } 21251da177e4SLinus Torvalds 21261da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev) 21271da177e4SLinus Torvalds { 21281da177e4SLinus Torvalds struct inet6_dev *idev; 21291da177e4SLinus Torvalds 21301da177e4SLinus Torvalds ASSERT_RTNL(); 21311da177e4SLinus Torvalds 21321da177e4SLinus Torvalds /* 21331da177e4SLinus Torvalds * Configure the tunnel with one of our IPv4 21341da177e4SLinus Torvalds * addresses... we should configure all of 21351da177e4SLinus Torvalds * our v4 addrs in the tunnel 21361da177e4SLinus Torvalds */ 21371da177e4SLinus Torvalds 21381da177e4SLinus Torvalds if ((idev = ipv6_find_idev(dev)) == NULL) { 21391da177e4SLinus Torvalds printk(KERN_DEBUG "init sit: add_dev failed\n"); 21401da177e4SLinus Torvalds return; 21411da177e4SLinus Torvalds } 21421da177e4SLinus Torvalds 21431da177e4SLinus Torvalds sit_add_v4_addrs(idev); 21441da177e4SLinus Torvalds 21451da177e4SLinus Torvalds if (dev->flags&IFF_POINTOPOINT) { 21461da177e4SLinus Torvalds addrconf_add_mroute(dev); 21471da177e4SLinus Torvalds addrconf_add_lroute(dev); 21481da177e4SLinus Torvalds } else 21491da177e4SLinus Torvalds sit_route_add(dev); 21501da177e4SLinus Torvalds } 21511da177e4SLinus Torvalds 21521da177e4SLinus Torvalds static inline int 21531da177e4SLinus Torvalds ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev) 21541da177e4SLinus Torvalds { 21551da177e4SLinus Torvalds struct in6_addr lladdr; 21561da177e4SLinus Torvalds 21571da177e4SLinus Torvalds if (!ipv6_get_lladdr(link_dev, &lladdr)) { 21581da177e4SLinus Torvalds addrconf_add_linklocal(idev, &lladdr); 21591da177e4SLinus Torvalds return 0; 21601da177e4SLinus Torvalds } 21611da177e4SLinus Torvalds return -1; 21621da177e4SLinus Torvalds } 21631da177e4SLinus Torvalds 21641da177e4SLinus Torvalds static void ip6_tnl_add_linklocal(struct inet6_dev *idev) 21651da177e4SLinus Torvalds { 21661da177e4SLinus Torvalds struct net_device *link_dev; 21671da177e4SLinus Torvalds 21681da177e4SLinus Torvalds /* first try to inherit the link-local address from the link device */ 21691da177e4SLinus Torvalds if (idev->dev->iflink && 21701da177e4SLinus Torvalds (link_dev = __dev_get_by_index(idev->dev->iflink))) { 21711da177e4SLinus Torvalds if (!ipv6_inherit_linklocal(idev, link_dev)) 21721da177e4SLinus Torvalds return; 21731da177e4SLinus Torvalds } 21741da177e4SLinus Torvalds /* then try to inherit it from any device */ 21751da177e4SLinus Torvalds for (link_dev = dev_base; link_dev; link_dev = link_dev->next) { 21761da177e4SLinus Torvalds if (!ipv6_inherit_linklocal(idev, link_dev)) 21771da177e4SLinus Torvalds return; 21781da177e4SLinus Torvalds } 21791da177e4SLinus Torvalds printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n"); 21801da177e4SLinus Torvalds } 21811da177e4SLinus Torvalds 21821da177e4SLinus Torvalds /* 21831da177e4SLinus Torvalds * Autoconfigure tunnel with a link-local address so routing protocols, 21841da177e4SLinus Torvalds * DHCPv6, MLD etc. can be run over the virtual link 21851da177e4SLinus Torvalds */ 21861da177e4SLinus Torvalds 21871da177e4SLinus Torvalds static void addrconf_ip6_tnl_config(struct net_device *dev) 21881da177e4SLinus Torvalds { 21891da177e4SLinus Torvalds struct inet6_dev *idev; 21901da177e4SLinus Torvalds 21911da177e4SLinus Torvalds ASSERT_RTNL(); 21921da177e4SLinus Torvalds 21931da177e4SLinus Torvalds if ((idev = addrconf_add_dev(dev)) == NULL) { 21941da177e4SLinus Torvalds printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n"); 21951da177e4SLinus Torvalds return; 21961da177e4SLinus Torvalds } 21971da177e4SLinus Torvalds ip6_tnl_add_linklocal(idev); 21981da177e4SLinus Torvalds } 21991da177e4SLinus Torvalds 22001da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event, 22011da177e4SLinus Torvalds void * data) 22021da177e4SLinus Torvalds { 22031da177e4SLinus Torvalds struct net_device *dev = (struct net_device *) data; 22041da177e4SLinus Torvalds struct inet6_dev *idev = __in6_dev_get(dev); 2205c5e33bddSYOSHIFUJI Hideaki int run_pending = 0; 22061da177e4SLinus Torvalds 22071da177e4SLinus Torvalds switch(event) { 22081da177e4SLinus Torvalds case NETDEV_UP: 22093c21edbdSYOSHIFUJI Hideaki case NETDEV_CHANGE: 22103c21edbdSYOSHIFUJI Hideaki if (event == NETDEV_UP) { 22113c21edbdSYOSHIFUJI Hideaki if (!netif_carrier_ok(dev)) { 22123c21edbdSYOSHIFUJI Hideaki /* device is not ready yet. */ 22133c21edbdSYOSHIFUJI Hideaki printk(KERN_INFO 22143c21edbdSYOSHIFUJI Hideaki "ADDRCONF(NETDEV_UP): %s: " 22153c21edbdSYOSHIFUJI Hideaki "link is not ready\n", 22163c21edbdSYOSHIFUJI Hideaki dev->name); 22173c21edbdSYOSHIFUJI Hideaki break; 22183c21edbdSYOSHIFUJI Hideaki } 221999081049SKristian Slavov 222099081049SKristian Slavov if (idev) 222199081049SKristian Slavov idev->if_flags |= IF_READY; 22223c21edbdSYOSHIFUJI Hideaki } else { 22233c21edbdSYOSHIFUJI Hideaki if (!netif_carrier_ok(dev)) { 22243c21edbdSYOSHIFUJI Hideaki /* device is still not ready. */ 22253c21edbdSYOSHIFUJI Hideaki break; 22263c21edbdSYOSHIFUJI Hideaki } 22273c21edbdSYOSHIFUJI Hideaki 22283c21edbdSYOSHIFUJI Hideaki if (idev) { 22293c21edbdSYOSHIFUJI Hideaki if (idev->if_flags & IF_READY) { 22303c21edbdSYOSHIFUJI Hideaki /* device is already configured. */ 22313c21edbdSYOSHIFUJI Hideaki break; 22323c21edbdSYOSHIFUJI Hideaki } 22333c21edbdSYOSHIFUJI Hideaki idev->if_flags |= IF_READY; 22343c21edbdSYOSHIFUJI Hideaki } 22353c21edbdSYOSHIFUJI Hideaki 22363c21edbdSYOSHIFUJI Hideaki printk(KERN_INFO 22373c21edbdSYOSHIFUJI Hideaki "ADDRCONF(NETDEV_CHANGE): %s: " 22383c21edbdSYOSHIFUJI Hideaki "link becomes ready\n", 22393c21edbdSYOSHIFUJI Hideaki dev->name); 22403c21edbdSYOSHIFUJI Hideaki 2241c5e33bddSYOSHIFUJI Hideaki run_pending = 1; 22423c21edbdSYOSHIFUJI Hideaki } 22433c21edbdSYOSHIFUJI Hideaki 22441da177e4SLinus Torvalds switch(dev->type) { 22451da177e4SLinus Torvalds case ARPHRD_SIT: 22461da177e4SLinus Torvalds addrconf_sit_config(dev); 22471da177e4SLinus Torvalds break; 22481da177e4SLinus Torvalds case ARPHRD_TUNNEL6: 22491da177e4SLinus Torvalds addrconf_ip6_tnl_config(dev); 22501da177e4SLinus Torvalds break; 22511da177e4SLinus Torvalds case ARPHRD_LOOPBACK: 22521da177e4SLinus Torvalds init_loopback(dev); 22531da177e4SLinus Torvalds break; 22541da177e4SLinus Torvalds 22551da177e4SLinus Torvalds default: 22561da177e4SLinus Torvalds addrconf_dev_config(dev); 22571da177e4SLinus Torvalds break; 22581da177e4SLinus Torvalds }; 22591da177e4SLinus Torvalds if (idev) { 2260c5e33bddSYOSHIFUJI Hideaki if (run_pending) 2261c5e33bddSYOSHIFUJI Hideaki addrconf_dad_run(idev); 2262c5e33bddSYOSHIFUJI Hideaki 22631da177e4SLinus Torvalds /* If the MTU changed during the interface down, when the 22641da177e4SLinus Torvalds interface up, the changed MTU must be reflected in the 22651da177e4SLinus Torvalds idev as well as routers. 22661da177e4SLinus Torvalds */ 22671da177e4SLinus Torvalds if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) { 22681da177e4SLinus Torvalds rt6_mtu_change(dev, dev->mtu); 22691da177e4SLinus Torvalds idev->cnf.mtu6 = dev->mtu; 22701da177e4SLinus Torvalds } 22711da177e4SLinus Torvalds idev->tstamp = jiffies; 22721da177e4SLinus Torvalds inet6_ifinfo_notify(RTM_NEWLINK, idev); 22731da177e4SLinus Torvalds /* If the changed mtu during down is lower than IPV6_MIN_MTU 22741da177e4SLinus Torvalds stop IPv6 on this interface. 22751da177e4SLinus Torvalds */ 22761da177e4SLinus Torvalds if (dev->mtu < IPV6_MIN_MTU) 22771da177e4SLinus Torvalds addrconf_ifdown(dev, event != NETDEV_DOWN); 22781da177e4SLinus Torvalds } 22791da177e4SLinus Torvalds break; 22801da177e4SLinus Torvalds 22811da177e4SLinus Torvalds case NETDEV_CHANGEMTU: 22821da177e4SLinus Torvalds if ( idev && dev->mtu >= IPV6_MIN_MTU) { 22831da177e4SLinus Torvalds rt6_mtu_change(dev, dev->mtu); 22841da177e4SLinus Torvalds idev->cnf.mtu6 = dev->mtu; 22851da177e4SLinus Torvalds break; 22861da177e4SLinus Torvalds } 22871da177e4SLinus Torvalds 22881da177e4SLinus Torvalds /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */ 22891da177e4SLinus Torvalds 22901da177e4SLinus Torvalds case NETDEV_DOWN: 22911da177e4SLinus Torvalds case NETDEV_UNREGISTER: 22921da177e4SLinus Torvalds /* 22931da177e4SLinus Torvalds * Remove all addresses from this interface. 22941da177e4SLinus Torvalds */ 22951da177e4SLinus Torvalds addrconf_ifdown(dev, event != NETDEV_DOWN); 22961da177e4SLinus Torvalds break; 22973c21edbdSYOSHIFUJI Hideaki 22981da177e4SLinus Torvalds case NETDEV_CHANGENAME: 22991da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 23001da177e4SLinus Torvalds if (idev) { 23011da177e4SLinus Torvalds addrconf_sysctl_unregister(&idev->cnf); 23021da177e4SLinus Torvalds neigh_sysctl_unregister(idev->nd_parms); 23031da177e4SLinus Torvalds neigh_sysctl_register(dev, idev->nd_parms, 23041da177e4SLinus Torvalds NET_IPV6, NET_IPV6_NEIGH, "ipv6", 23051da177e4SLinus Torvalds &ndisc_ifinfo_sysctl_change, 23061da177e4SLinus Torvalds NULL); 23071da177e4SLinus Torvalds addrconf_sysctl_register(idev, &idev->cnf); 23081da177e4SLinus Torvalds } 23091da177e4SLinus Torvalds #endif 23101da177e4SLinus Torvalds break; 23111da177e4SLinus Torvalds }; 23121da177e4SLinus Torvalds 23131da177e4SLinus Torvalds return NOTIFY_OK; 23141da177e4SLinus Torvalds } 23151da177e4SLinus Torvalds 23161da177e4SLinus Torvalds /* 23171da177e4SLinus Torvalds * addrconf module should be notified of a device going up 23181da177e4SLinus Torvalds */ 23191da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = { 23201da177e4SLinus Torvalds .notifier_call = addrconf_notify, 23211da177e4SLinus Torvalds .priority = 0 23221da177e4SLinus Torvalds }; 23231da177e4SLinus Torvalds 23241da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how) 23251da177e4SLinus Torvalds { 23261da177e4SLinus Torvalds struct inet6_dev *idev; 23271da177e4SLinus Torvalds struct inet6_ifaddr *ifa, **bifa; 23281da177e4SLinus Torvalds int i; 23291da177e4SLinus Torvalds 23301da177e4SLinus Torvalds ASSERT_RTNL(); 23311da177e4SLinus Torvalds 23321da177e4SLinus Torvalds if (dev == &loopback_dev && how == 1) 23331da177e4SLinus Torvalds how = 0; 23341da177e4SLinus Torvalds 23351da177e4SLinus Torvalds rt6_ifdown(dev); 23361da177e4SLinus Torvalds neigh_ifdown(&nd_tbl, dev); 23371da177e4SLinus Torvalds 23381da177e4SLinus Torvalds idev = __in6_dev_get(dev); 23391da177e4SLinus Torvalds if (idev == NULL) 23401da177e4SLinus Torvalds return -ENODEV; 23411da177e4SLinus Torvalds 23421da177e4SLinus Torvalds /* Step 1: remove reference to ipv6 device from parent device. 23431da177e4SLinus Torvalds Do not dev_put! 23441da177e4SLinus Torvalds */ 23451da177e4SLinus Torvalds if (how == 1) { 23461da177e4SLinus Torvalds write_lock_bh(&addrconf_lock); 23471da177e4SLinus Torvalds dev->ip6_ptr = NULL; 23481da177e4SLinus Torvalds idev->dead = 1; 23491da177e4SLinus Torvalds write_unlock_bh(&addrconf_lock); 23501da177e4SLinus Torvalds 23511da177e4SLinus Torvalds /* Step 1.5: remove snmp6 entry */ 23521da177e4SLinus Torvalds snmp6_unregister_dev(idev); 23531da177e4SLinus Torvalds 23541da177e4SLinus Torvalds } 23551da177e4SLinus Torvalds 23561da177e4SLinus Torvalds /* Step 2: clear hash table */ 23571da177e4SLinus Torvalds for (i=0; i<IN6_ADDR_HSIZE; i++) { 23581da177e4SLinus Torvalds bifa = &inet6_addr_lst[i]; 23591da177e4SLinus Torvalds 23601da177e4SLinus Torvalds write_lock_bh(&addrconf_hash_lock); 23611da177e4SLinus Torvalds while ((ifa = *bifa) != NULL) { 23621da177e4SLinus Torvalds if (ifa->idev == idev) { 23631da177e4SLinus Torvalds *bifa = ifa->lst_next; 23641da177e4SLinus Torvalds ifa->lst_next = NULL; 23651da177e4SLinus Torvalds addrconf_del_timer(ifa); 23661da177e4SLinus Torvalds in6_ifa_put(ifa); 23671da177e4SLinus Torvalds continue; 23681da177e4SLinus Torvalds } 23691da177e4SLinus Torvalds bifa = &ifa->lst_next; 23701da177e4SLinus Torvalds } 23711da177e4SLinus Torvalds write_unlock_bh(&addrconf_hash_lock); 23721da177e4SLinus Torvalds } 23731da177e4SLinus Torvalds 23741da177e4SLinus Torvalds write_lock_bh(&idev->lock); 23751da177e4SLinus Torvalds 23761da177e4SLinus Torvalds /* Step 3: clear flags for stateless addrconf */ 23771da177e4SLinus Torvalds if (how != 1) 23783c21edbdSYOSHIFUJI Hideaki idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY); 23791da177e4SLinus Torvalds 23801da177e4SLinus Torvalds /* Step 4: clear address list */ 23811da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 23821da177e4SLinus Torvalds if (how == 1 && del_timer(&idev->regen_timer)) 23831da177e4SLinus Torvalds in6_dev_put(idev); 23841da177e4SLinus Torvalds 23851da177e4SLinus Torvalds /* clear tempaddr list */ 23861da177e4SLinus Torvalds while ((ifa = idev->tempaddr_list) != NULL) { 23871da177e4SLinus Torvalds idev->tempaddr_list = ifa->tmp_next; 23881da177e4SLinus Torvalds ifa->tmp_next = NULL; 23891da177e4SLinus Torvalds ifa->dead = 1; 23901da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 23911da177e4SLinus Torvalds spin_lock_bh(&ifa->lock); 23921da177e4SLinus Torvalds 23931da177e4SLinus Torvalds if (ifa->ifpub) { 23941da177e4SLinus Torvalds in6_ifa_put(ifa->ifpub); 23951da177e4SLinus Torvalds ifa->ifpub = NULL; 23961da177e4SLinus Torvalds } 23971da177e4SLinus Torvalds spin_unlock_bh(&ifa->lock); 23981da177e4SLinus Torvalds in6_ifa_put(ifa); 23991da177e4SLinus Torvalds write_lock_bh(&idev->lock); 24001da177e4SLinus Torvalds } 24011da177e4SLinus Torvalds #endif 24021da177e4SLinus Torvalds while ((ifa = idev->addr_list) != NULL) { 24031da177e4SLinus Torvalds idev->addr_list = ifa->if_next; 24041da177e4SLinus Torvalds ifa->if_next = NULL; 24051da177e4SLinus Torvalds ifa->dead = 1; 24061da177e4SLinus Torvalds addrconf_del_timer(ifa); 24071da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 24081da177e4SLinus Torvalds 24091da177e4SLinus Torvalds __ipv6_ifa_notify(RTM_DELADDR, ifa); 24101da177e4SLinus Torvalds in6_ifa_put(ifa); 24111da177e4SLinus Torvalds 24121da177e4SLinus Torvalds write_lock_bh(&idev->lock); 24131da177e4SLinus Torvalds } 24141da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 24151da177e4SLinus Torvalds 24161da177e4SLinus Torvalds /* Step 5: Discard multicast list */ 24171da177e4SLinus Torvalds 24181da177e4SLinus Torvalds if (how == 1) 24191da177e4SLinus Torvalds ipv6_mc_destroy_dev(idev); 24201da177e4SLinus Torvalds else 24211da177e4SLinus Torvalds ipv6_mc_down(idev); 24221da177e4SLinus Torvalds 24231da177e4SLinus Torvalds /* Step 5: netlink notification of this interface */ 24241da177e4SLinus Torvalds idev->tstamp = jiffies; 2425979ad663SYan Zheng inet6_ifinfo_notify(RTM_DELLINK, idev); 24261da177e4SLinus Torvalds 24271da177e4SLinus Torvalds /* Shot the device (if unregistered) */ 24281da177e4SLinus Torvalds 24291da177e4SLinus Torvalds if (how == 1) { 24301da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 24311da177e4SLinus Torvalds addrconf_sysctl_unregister(&idev->cnf); 24321da177e4SLinus Torvalds neigh_sysctl_unregister(idev->nd_parms); 24331da177e4SLinus Torvalds #endif 24341da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, idev->nd_parms); 24351da177e4SLinus Torvalds neigh_ifdown(&nd_tbl, dev); 24361da177e4SLinus Torvalds in6_dev_put(idev); 24371da177e4SLinus Torvalds } 24381da177e4SLinus Torvalds return 0; 24391da177e4SLinus Torvalds } 24401da177e4SLinus Torvalds 24411da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data) 24421da177e4SLinus Torvalds { 24431da177e4SLinus Torvalds struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data; 24441da177e4SLinus Torvalds 24451da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 24461da177e4SLinus Torvalds goto out; 24471da177e4SLinus Torvalds 24481da177e4SLinus Torvalds if (ifp->idev->if_flags & IF_RA_RCVD) { 24491da177e4SLinus Torvalds /* 24501da177e4SLinus Torvalds * Announcement received after solicitation 24511da177e4SLinus Torvalds * was sent 24521da177e4SLinus Torvalds */ 24531da177e4SLinus Torvalds goto out; 24541da177e4SLinus Torvalds } 24551da177e4SLinus Torvalds 24561da177e4SLinus Torvalds spin_lock(&ifp->lock); 24571da177e4SLinus Torvalds if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) { 24581da177e4SLinus Torvalds struct in6_addr all_routers; 24591da177e4SLinus Torvalds 24601da177e4SLinus Torvalds /* The wait after the last probe can be shorter */ 24611da177e4SLinus Torvalds addrconf_mod_timer(ifp, AC_RS, 24621da177e4SLinus Torvalds (ifp->probes == ifp->idev->cnf.rtr_solicits) ? 24631da177e4SLinus Torvalds ifp->idev->cnf.rtr_solicit_delay : 24641da177e4SLinus Torvalds ifp->idev->cnf.rtr_solicit_interval); 24651da177e4SLinus Torvalds spin_unlock(&ifp->lock); 24661da177e4SLinus Torvalds 24671da177e4SLinus Torvalds ipv6_addr_all_routers(&all_routers); 24681da177e4SLinus Torvalds 24691da177e4SLinus Torvalds ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers); 24701da177e4SLinus Torvalds } else { 24711da177e4SLinus Torvalds spin_unlock(&ifp->lock); 24721da177e4SLinus Torvalds /* 24731da177e4SLinus Torvalds * Note: we do not support deprecated "all on-link" 24741da177e4SLinus Torvalds * assumption any longer. 24751da177e4SLinus Torvalds */ 24761da177e4SLinus Torvalds printk(KERN_DEBUG "%s: no IPv6 routers present\n", 24771da177e4SLinus Torvalds ifp->idev->dev->name); 24781da177e4SLinus Torvalds } 24791da177e4SLinus Torvalds 24801da177e4SLinus Torvalds out: 24811da177e4SLinus Torvalds in6_ifa_put(ifp); 24821da177e4SLinus Torvalds } 24831da177e4SLinus Torvalds 24841da177e4SLinus Torvalds /* 24851da177e4SLinus Torvalds * Duplicate Address Detection 24861da177e4SLinus Torvalds */ 24873c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp) 24883c21edbdSYOSHIFUJI Hideaki { 24893c21edbdSYOSHIFUJI Hideaki unsigned long rand_num; 24903c21edbdSYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev; 24913c21edbdSYOSHIFUJI Hideaki 24923c21edbdSYOSHIFUJI Hideaki rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1); 24933c21edbdSYOSHIFUJI Hideaki ifp->probes = idev->cnf.dad_transmits; 24943c21edbdSYOSHIFUJI Hideaki addrconf_mod_timer(ifp, AC_DAD, rand_num); 24953c21edbdSYOSHIFUJI Hideaki } 24963c21edbdSYOSHIFUJI Hideaki 2497e431b8c0SJamal Hadi Salim static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags) 24981da177e4SLinus Torvalds { 24991da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 25001da177e4SLinus Torvalds struct net_device *dev = idev->dev; 25011da177e4SLinus Torvalds 25021da177e4SLinus Torvalds addrconf_join_solict(dev, &ifp->addr); 25031da177e4SLinus Torvalds 25041da177e4SLinus Torvalds if (ifp->prefix_len != 128 && (ifp->flags&IFA_F_PERMANENT)) 25051da177e4SLinus Torvalds addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 0, 25061da177e4SLinus Torvalds flags); 25071da177e4SLinus Torvalds 25081da177e4SLinus Torvalds net_srandom(ifp->addr.s6_addr32[3]); 25091da177e4SLinus Torvalds 25101da177e4SLinus Torvalds read_lock_bh(&idev->lock); 25111da177e4SLinus Torvalds if (ifp->dead) 25121da177e4SLinus Torvalds goto out; 25131da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 25141da177e4SLinus Torvalds 25151da177e4SLinus Torvalds if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) || 25161da177e4SLinus Torvalds !(ifp->flags&IFA_F_TENTATIVE)) { 25171da177e4SLinus Torvalds ifp->flags &= ~IFA_F_TENTATIVE; 25181da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 25191da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 25201da177e4SLinus Torvalds 25211da177e4SLinus Torvalds addrconf_dad_completed(ifp); 25221da177e4SLinus Torvalds return; 25231da177e4SLinus Torvalds } 25241da177e4SLinus Torvalds 25256732badeSYOSHIFUJI Hideaki if (!(idev->if_flags & IF_READY)) { 25263dd3bf83SYOSHIFUJI Hideaki spin_unlock_bh(&ifp->lock); 25276732badeSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 25283c21edbdSYOSHIFUJI Hideaki /* 25293c21edbdSYOSHIFUJI Hideaki * If the defice is not ready: 25303c21edbdSYOSHIFUJI Hideaki * - keep it tentative if it is a permanent address. 25313c21edbdSYOSHIFUJI Hideaki * - otherwise, kill it. 25323c21edbdSYOSHIFUJI Hideaki */ 25333c21edbdSYOSHIFUJI Hideaki in6_ifa_hold(ifp); 25343c21edbdSYOSHIFUJI Hideaki addrconf_dad_stop(ifp); 25356732badeSYOSHIFUJI Hideaki return; 25363c21edbdSYOSHIFUJI Hideaki } 25376732badeSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 25386732badeSYOSHIFUJI Hideaki spin_unlock_bh(&ifp->lock); 25391da177e4SLinus Torvalds out: 25401da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 25411da177e4SLinus Torvalds } 25421da177e4SLinus Torvalds 25431da177e4SLinus Torvalds static void addrconf_dad_timer(unsigned long data) 25441da177e4SLinus Torvalds { 25451da177e4SLinus Torvalds struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data; 25461da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 25471da177e4SLinus Torvalds struct in6_addr unspec; 25481da177e4SLinus Torvalds struct in6_addr mcaddr; 25491da177e4SLinus Torvalds 25501da177e4SLinus Torvalds read_lock_bh(&idev->lock); 25511da177e4SLinus Torvalds if (idev->dead) { 25521da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 25531da177e4SLinus Torvalds goto out; 25541da177e4SLinus Torvalds } 25551da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 25561da177e4SLinus Torvalds if (ifp->probes == 0) { 25571da177e4SLinus Torvalds /* 25581da177e4SLinus Torvalds * DAD was successful 25591da177e4SLinus Torvalds */ 25601da177e4SLinus Torvalds 25611da177e4SLinus Torvalds ifp->flags &= ~IFA_F_TENTATIVE; 25621da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 25631da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 25641da177e4SLinus Torvalds 25651da177e4SLinus Torvalds addrconf_dad_completed(ifp); 25661da177e4SLinus Torvalds 25671da177e4SLinus Torvalds goto out; 25681da177e4SLinus Torvalds } 25691da177e4SLinus Torvalds 25701da177e4SLinus Torvalds ifp->probes--; 25711da177e4SLinus Torvalds addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time); 25721da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 25731da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 25741da177e4SLinus Torvalds 25751da177e4SLinus Torvalds /* send a neighbour solicitation for our addr */ 25761da177e4SLinus Torvalds memset(&unspec, 0, sizeof(unspec)); 25771da177e4SLinus Torvalds addrconf_addr_solict_mult(&ifp->addr, &mcaddr); 25781da177e4SLinus Torvalds ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec); 25791da177e4SLinus Torvalds out: 25801da177e4SLinus Torvalds in6_ifa_put(ifp); 25811da177e4SLinus Torvalds } 25821da177e4SLinus Torvalds 25831da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp) 25841da177e4SLinus Torvalds { 25851da177e4SLinus Torvalds struct net_device * dev = ifp->idev->dev; 25861da177e4SLinus Torvalds 25871da177e4SLinus Torvalds /* 25881da177e4SLinus Torvalds * Configure the address for reception. Now it is valid. 25891da177e4SLinus Torvalds */ 25901da177e4SLinus Torvalds 25911da177e4SLinus Torvalds ipv6_ifa_notify(RTM_NEWADDR, ifp); 25921da177e4SLinus Torvalds 25931da177e4SLinus Torvalds /* If added prefix is link local and forwarding is off, 25941da177e4SLinus Torvalds start sending router solicitations. 25951da177e4SLinus Torvalds */ 25961da177e4SLinus Torvalds 25971da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding == 0 && 25981da177e4SLinus Torvalds ifp->idev->cnf.rtr_solicits > 0 && 25991da177e4SLinus Torvalds (dev->flags&IFF_LOOPBACK) == 0 && 26001da177e4SLinus Torvalds (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) { 26011da177e4SLinus Torvalds struct in6_addr all_routers; 26021da177e4SLinus Torvalds 26031da177e4SLinus Torvalds ipv6_addr_all_routers(&all_routers); 26041da177e4SLinus Torvalds 26051da177e4SLinus Torvalds /* 26061da177e4SLinus Torvalds * If a host as already performed a random delay 26071da177e4SLinus Torvalds * [...] as part of DAD [...] there is no need 26081da177e4SLinus Torvalds * to delay again before sending the first RS 26091da177e4SLinus Torvalds */ 26101da177e4SLinus Torvalds ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers); 26111da177e4SLinus Torvalds 26121da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 26131da177e4SLinus Torvalds ifp->probes = 1; 26141da177e4SLinus Torvalds ifp->idev->if_flags |= IF_RS_SENT; 26151da177e4SLinus Torvalds addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval); 26161da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 26171da177e4SLinus Torvalds } 26181da177e4SLinus Torvalds } 26191da177e4SLinus Torvalds 2620c5e33bddSYOSHIFUJI Hideaki static void addrconf_dad_run(struct inet6_dev *idev) { 2621c5e33bddSYOSHIFUJI Hideaki struct inet6_ifaddr *ifp; 2622c5e33bddSYOSHIFUJI Hideaki 2623c5e33bddSYOSHIFUJI Hideaki read_lock_bh(&idev->lock); 2624c5e33bddSYOSHIFUJI Hideaki for (ifp = idev->addr_list; ifp; ifp = ifp->if_next) { 2625c5e33bddSYOSHIFUJI Hideaki spin_lock_bh(&ifp->lock); 2626c5e33bddSYOSHIFUJI Hideaki if (!(ifp->flags & IFA_F_TENTATIVE)) { 2627c5e33bddSYOSHIFUJI Hideaki spin_unlock_bh(&ifp->lock); 2628c5e33bddSYOSHIFUJI Hideaki continue; 2629c5e33bddSYOSHIFUJI Hideaki } 2630c5e33bddSYOSHIFUJI Hideaki spin_unlock_bh(&ifp->lock); 2631c5e33bddSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 2632c5e33bddSYOSHIFUJI Hideaki } 2633c5e33bddSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 2634c5e33bddSYOSHIFUJI Hideaki } 2635c5e33bddSYOSHIFUJI Hideaki 26361da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 26371da177e4SLinus Torvalds struct if6_iter_state { 26381da177e4SLinus Torvalds int bucket; 26391da177e4SLinus Torvalds }; 26401da177e4SLinus Torvalds 26411da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_first(struct seq_file *seq) 26421da177e4SLinus Torvalds { 26431da177e4SLinus Torvalds struct inet6_ifaddr *ifa = NULL; 26441da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 26451da177e4SLinus Torvalds 26461da177e4SLinus Torvalds for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) { 26471da177e4SLinus Torvalds ifa = inet6_addr_lst[state->bucket]; 26481da177e4SLinus Torvalds if (ifa) 26491da177e4SLinus Torvalds break; 26501da177e4SLinus Torvalds } 26511da177e4SLinus Torvalds return ifa; 26521da177e4SLinus Torvalds } 26531da177e4SLinus Torvalds 26541da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa) 26551da177e4SLinus Torvalds { 26561da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 26571da177e4SLinus Torvalds 26581da177e4SLinus Torvalds ifa = ifa->lst_next; 26591da177e4SLinus Torvalds try_again: 26601da177e4SLinus Torvalds if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) { 26611da177e4SLinus Torvalds ifa = inet6_addr_lst[state->bucket]; 26621da177e4SLinus Torvalds goto try_again; 26631da177e4SLinus Torvalds } 26641da177e4SLinus Torvalds return ifa; 26651da177e4SLinus Torvalds } 26661da177e4SLinus Torvalds 26671da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos) 26681da177e4SLinus Torvalds { 26691da177e4SLinus Torvalds struct inet6_ifaddr *ifa = if6_get_first(seq); 26701da177e4SLinus Torvalds 26711da177e4SLinus Torvalds if (ifa) 26721da177e4SLinus Torvalds while(pos && (ifa = if6_get_next(seq, ifa)) != NULL) 26731da177e4SLinus Torvalds --pos; 26741da177e4SLinus Torvalds return pos ? NULL : ifa; 26751da177e4SLinus Torvalds } 26761da177e4SLinus Torvalds 26771da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos) 26781da177e4SLinus Torvalds { 26791da177e4SLinus Torvalds read_lock_bh(&addrconf_hash_lock); 26801da177e4SLinus Torvalds return if6_get_idx(seq, *pos); 26811da177e4SLinus Torvalds } 26821da177e4SLinus Torvalds 26831da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos) 26841da177e4SLinus Torvalds { 26851da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 26861da177e4SLinus Torvalds 26871da177e4SLinus Torvalds ifa = if6_get_next(seq, v); 26881da177e4SLinus Torvalds ++*pos; 26891da177e4SLinus Torvalds return ifa; 26901da177e4SLinus Torvalds } 26911da177e4SLinus Torvalds 26921da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v) 26931da177e4SLinus Torvalds { 26941da177e4SLinus Torvalds read_unlock_bh(&addrconf_hash_lock); 26951da177e4SLinus Torvalds } 26961da177e4SLinus Torvalds 26971da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v) 26981da177e4SLinus Torvalds { 26991da177e4SLinus Torvalds struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v; 27001da177e4SLinus Torvalds seq_printf(seq, 27019343e79aSYOSHIFUJI Hideaki NIP6_SEQFMT " %02x %02x %02x %02x %8s\n", 27021da177e4SLinus Torvalds NIP6(ifp->addr), 27031da177e4SLinus Torvalds ifp->idev->dev->ifindex, 27041da177e4SLinus Torvalds ifp->prefix_len, 27051da177e4SLinus Torvalds ifp->scope, 27061da177e4SLinus Torvalds ifp->flags, 27071da177e4SLinus Torvalds ifp->idev->dev->name); 27081da177e4SLinus Torvalds return 0; 27091da177e4SLinus Torvalds } 27101da177e4SLinus Torvalds 27111da177e4SLinus Torvalds static struct seq_operations if6_seq_ops = { 27121da177e4SLinus Torvalds .start = if6_seq_start, 27131da177e4SLinus Torvalds .next = if6_seq_next, 27141da177e4SLinus Torvalds .show = if6_seq_show, 27151da177e4SLinus Torvalds .stop = if6_seq_stop, 27161da177e4SLinus Torvalds }; 27171da177e4SLinus Torvalds 27181da177e4SLinus Torvalds static int if6_seq_open(struct inode *inode, struct file *file) 27191da177e4SLinus Torvalds { 27201da177e4SLinus Torvalds struct seq_file *seq; 27211da177e4SLinus Torvalds int rc = -ENOMEM; 2722322f74a4SIngo Oeser struct if6_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL); 27231da177e4SLinus Torvalds 27241da177e4SLinus Torvalds if (!s) 27251da177e4SLinus Torvalds goto out; 27261da177e4SLinus Torvalds 27271da177e4SLinus Torvalds rc = seq_open(file, &if6_seq_ops); 27281da177e4SLinus Torvalds if (rc) 27291da177e4SLinus Torvalds goto out_kfree; 27301da177e4SLinus Torvalds 27311da177e4SLinus Torvalds seq = file->private_data; 27321da177e4SLinus Torvalds seq->private = s; 27331da177e4SLinus Torvalds out: 27341da177e4SLinus Torvalds return rc; 27351da177e4SLinus Torvalds out_kfree: 27361da177e4SLinus Torvalds kfree(s); 27371da177e4SLinus Torvalds goto out; 27381da177e4SLinus Torvalds } 27391da177e4SLinus Torvalds 27401da177e4SLinus Torvalds static struct file_operations if6_fops = { 27411da177e4SLinus Torvalds .owner = THIS_MODULE, 27421da177e4SLinus Torvalds .open = if6_seq_open, 27431da177e4SLinus Torvalds .read = seq_read, 27441da177e4SLinus Torvalds .llseek = seq_lseek, 27451da177e4SLinus Torvalds .release = seq_release_private, 27461da177e4SLinus Torvalds }; 27471da177e4SLinus Torvalds 27481da177e4SLinus Torvalds int __init if6_proc_init(void) 27491da177e4SLinus Torvalds { 27501da177e4SLinus Torvalds if (!proc_net_fops_create("if_inet6", S_IRUGO, &if6_fops)) 27511da177e4SLinus Torvalds return -ENOMEM; 27521da177e4SLinus Torvalds return 0; 27531da177e4SLinus Torvalds } 27541da177e4SLinus Torvalds 27551da177e4SLinus Torvalds void if6_proc_exit(void) 27561da177e4SLinus Torvalds { 27571da177e4SLinus Torvalds proc_net_remove("if_inet6"); 27581da177e4SLinus Torvalds } 27591da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 27601da177e4SLinus Torvalds 27611da177e4SLinus Torvalds /* 27621da177e4SLinus Torvalds * Periodic address status verification 27631da177e4SLinus Torvalds */ 27641da177e4SLinus Torvalds 27651da177e4SLinus Torvalds static void addrconf_verify(unsigned long foo) 27661da177e4SLinus Torvalds { 27671da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 27681da177e4SLinus Torvalds unsigned long now, next; 27691da177e4SLinus Torvalds int i; 27701da177e4SLinus Torvalds 27711da177e4SLinus Torvalds spin_lock_bh(&addrconf_verify_lock); 27721da177e4SLinus Torvalds now = jiffies; 27731da177e4SLinus Torvalds next = now + ADDR_CHECK_FREQUENCY; 27741da177e4SLinus Torvalds 27751da177e4SLinus Torvalds del_timer(&addr_chk_timer); 27761da177e4SLinus Torvalds 27771da177e4SLinus Torvalds for (i=0; i < IN6_ADDR_HSIZE; i++) { 27781da177e4SLinus Torvalds 27791da177e4SLinus Torvalds restart: 27805d5780dfSYan Zheng read_lock(&addrconf_hash_lock); 27811da177e4SLinus Torvalds for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) { 27821da177e4SLinus Torvalds unsigned long age; 27831da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 27841da177e4SLinus Torvalds unsigned long regen_advance; 27851da177e4SLinus Torvalds #endif 27861da177e4SLinus Torvalds 27871da177e4SLinus Torvalds if (ifp->flags & IFA_F_PERMANENT) 27881da177e4SLinus Torvalds continue; 27891da177e4SLinus Torvalds 27901da177e4SLinus Torvalds spin_lock(&ifp->lock); 27911da177e4SLinus Torvalds age = (now - ifp->tstamp) / HZ; 27921da177e4SLinus Torvalds 27931da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 27941da177e4SLinus Torvalds regen_advance = ifp->idev->cnf.regen_max_retry * 27951da177e4SLinus Torvalds ifp->idev->cnf.dad_transmits * 27961da177e4SLinus Torvalds ifp->idev->nd_parms->retrans_time / HZ; 27971da177e4SLinus Torvalds #endif 27981da177e4SLinus Torvalds 27998f27ebb9SYOSHIFUJI Hideaki if (ifp->valid_lft != INFINITY_LIFE_TIME && 28008f27ebb9SYOSHIFUJI Hideaki age >= ifp->valid_lft) { 28011da177e4SLinus Torvalds spin_unlock(&ifp->lock); 28021da177e4SLinus Torvalds in6_ifa_hold(ifp); 28035d5780dfSYan Zheng read_unlock(&addrconf_hash_lock); 28041da177e4SLinus Torvalds ipv6_del_addr(ifp); 28051da177e4SLinus Torvalds goto restart; 28068f27ebb9SYOSHIFUJI Hideaki } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) { 28078f27ebb9SYOSHIFUJI Hideaki spin_unlock(&ifp->lock); 28088f27ebb9SYOSHIFUJI Hideaki continue; 28091da177e4SLinus Torvalds } else if (age >= ifp->prefered_lft) { 28101da177e4SLinus Torvalds /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */ 28111da177e4SLinus Torvalds int deprecate = 0; 28121da177e4SLinus Torvalds 28131da177e4SLinus Torvalds if (!(ifp->flags&IFA_F_DEPRECATED)) { 28141da177e4SLinus Torvalds deprecate = 1; 28151da177e4SLinus Torvalds ifp->flags |= IFA_F_DEPRECATED; 28161da177e4SLinus Torvalds } 28171da177e4SLinus Torvalds 28181da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)) 28191da177e4SLinus Torvalds next = ifp->tstamp + ifp->valid_lft * HZ; 28201da177e4SLinus Torvalds 28211da177e4SLinus Torvalds spin_unlock(&ifp->lock); 28221da177e4SLinus Torvalds 28231da177e4SLinus Torvalds if (deprecate) { 28241da177e4SLinus Torvalds in6_ifa_hold(ifp); 28255d5780dfSYan Zheng read_unlock(&addrconf_hash_lock); 28261da177e4SLinus Torvalds 28271da177e4SLinus Torvalds ipv6_ifa_notify(0, ifp); 28281da177e4SLinus Torvalds in6_ifa_put(ifp); 28291da177e4SLinus Torvalds goto restart; 28301da177e4SLinus Torvalds } 28311da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 28321da177e4SLinus Torvalds } else if ((ifp->flags&IFA_F_TEMPORARY) && 28331da177e4SLinus Torvalds !(ifp->flags&IFA_F_TENTATIVE)) { 28341da177e4SLinus Torvalds if (age >= ifp->prefered_lft - regen_advance) { 28351da177e4SLinus Torvalds struct inet6_ifaddr *ifpub = ifp->ifpub; 28361da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 28371da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 28381da177e4SLinus Torvalds if (!ifp->regen_count && ifpub) { 28391da177e4SLinus Torvalds ifp->regen_count++; 28401da177e4SLinus Torvalds in6_ifa_hold(ifp); 28411da177e4SLinus Torvalds in6_ifa_hold(ifpub); 28421da177e4SLinus Torvalds spin_unlock(&ifp->lock); 28435d5780dfSYan Zheng read_unlock(&addrconf_hash_lock); 2844291d809bSHiroyuki YAMAMORI spin_lock(&ifpub->lock); 2845291d809bSHiroyuki YAMAMORI ifpub->regen_count = 0; 2846291d809bSHiroyuki YAMAMORI spin_unlock(&ifpub->lock); 28471da177e4SLinus Torvalds ipv6_create_tempaddr(ifpub, ifp); 28481da177e4SLinus Torvalds in6_ifa_put(ifpub); 28491da177e4SLinus Torvalds in6_ifa_put(ifp); 28501da177e4SLinus Torvalds goto restart; 28511da177e4SLinus Torvalds } 28521da177e4SLinus Torvalds } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next)) 28531da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ; 28541da177e4SLinus Torvalds spin_unlock(&ifp->lock); 28551da177e4SLinus Torvalds #endif 28561da177e4SLinus Torvalds } else { 28571da177e4SLinus Torvalds /* ifp->prefered_lft <= ifp->valid_lft */ 28581da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 28591da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 28601da177e4SLinus Torvalds spin_unlock(&ifp->lock); 28611da177e4SLinus Torvalds } 28621da177e4SLinus Torvalds } 28635d5780dfSYan Zheng read_unlock(&addrconf_hash_lock); 28641da177e4SLinus Torvalds } 28651da177e4SLinus Torvalds 28661da177e4SLinus Torvalds addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next; 28671da177e4SLinus Torvalds add_timer(&addr_chk_timer); 28681da177e4SLinus Torvalds spin_unlock_bh(&addrconf_verify_lock); 28691da177e4SLinus Torvalds } 28701da177e4SLinus Torvalds 2871461d8837SThomas Graf static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local) 2872461d8837SThomas Graf { 2873461d8837SThomas Graf struct in6_addr *pfx = NULL; 2874461d8837SThomas Graf 2875461d8837SThomas Graf if (addr) 2876461d8837SThomas Graf pfx = nla_data(addr); 2877461d8837SThomas Graf 2878461d8837SThomas Graf if (local) { 2879461d8837SThomas Graf if (pfx && nla_memcmp(local, pfx, sizeof(*pfx))) 2880461d8837SThomas Graf pfx = NULL; 2881461d8837SThomas Graf else 2882461d8837SThomas Graf pfx = nla_data(local); 2883461d8837SThomas Graf } 2884461d8837SThomas Graf 2885461d8837SThomas Graf return pfx; 2886461d8837SThomas Graf } 2887461d8837SThomas Graf 2888461d8837SThomas Graf static struct nla_policy ifa_ipv6_policy[IFA_MAX+1] __read_mostly = { 2889461d8837SThomas Graf [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) }, 2890461d8837SThomas Graf [IFA_LOCAL] = { .len = sizeof(struct in6_addr) }, 2891461d8837SThomas Graf [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) }, 2892461d8837SThomas Graf }; 2893461d8837SThomas Graf 28941da177e4SLinus Torvalds static int 28951da177e4SLinus Torvalds inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 28961da177e4SLinus Torvalds { 2897b933f716SThomas Graf struct ifaddrmsg *ifm; 2898b933f716SThomas Graf struct nlattr *tb[IFA_MAX+1]; 28991da177e4SLinus Torvalds struct in6_addr *pfx; 2900b933f716SThomas Graf int err; 29011da177e4SLinus Torvalds 2902b933f716SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 2903b933f716SThomas Graf if (err < 0) 2904b933f716SThomas Graf return err; 2905b933f716SThomas Graf 2906b933f716SThomas Graf ifm = nlmsg_data(nlh); 2907b933f716SThomas Graf pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]); 29081da177e4SLinus Torvalds if (pfx == NULL) 29091da177e4SLinus Torvalds return -EINVAL; 29101da177e4SLinus Torvalds 29111da177e4SLinus Torvalds return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen); 29121da177e4SLinus Torvalds } 29131da177e4SLinus Torvalds 29141da177e4SLinus Torvalds static int 2915081bba5bSNoriaki TAKAMIYA inet6_addr_modify(int ifindex, struct in6_addr *pfx, 2916081bba5bSNoriaki TAKAMIYA __u32 prefered_lft, __u32 valid_lft) 2917081bba5bSNoriaki TAKAMIYA { 2918081bba5bSNoriaki TAKAMIYA struct inet6_ifaddr *ifp = NULL; 2919081bba5bSNoriaki TAKAMIYA struct net_device *dev; 2920081bba5bSNoriaki TAKAMIYA int ifa_flags = 0; 2921081bba5bSNoriaki TAKAMIYA 2922081bba5bSNoriaki TAKAMIYA if ((dev = __dev_get_by_index(ifindex)) == NULL) 2923081bba5bSNoriaki TAKAMIYA return -ENODEV; 2924081bba5bSNoriaki TAKAMIYA 2925081bba5bSNoriaki TAKAMIYA if (!(dev->flags&IFF_UP)) 2926081bba5bSNoriaki TAKAMIYA return -ENETDOWN; 2927081bba5bSNoriaki TAKAMIYA 2928081bba5bSNoriaki TAKAMIYA if (!valid_lft || (prefered_lft > valid_lft)) 2929081bba5bSNoriaki TAKAMIYA return -EINVAL; 2930081bba5bSNoriaki TAKAMIYA 2931081bba5bSNoriaki TAKAMIYA ifp = ipv6_get_ifaddr(pfx, dev, 1); 2932081bba5bSNoriaki TAKAMIYA if (ifp == NULL) 2933081bba5bSNoriaki TAKAMIYA return -ENOENT; 2934081bba5bSNoriaki TAKAMIYA 2935081bba5bSNoriaki TAKAMIYA if (valid_lft == INFINITY_LIFE_TIME) 2936081bba5bSNoriaki TAKAMIYA ifa_flags = IFA_F_PERMANENT; 2937081bba5bSNoriaki TAKAMIYA else if (valid_lft >= 0x7FFFFFFF/HZ) 2938081bba5bSNoriaki TAKAMIYA valid_lft = 0x7FFFFFFF/HZ; 2939081bba5bSNoriaki TAKAMIYA 2940081bba5bSNoriaki TAKAMIYA if (prefered_lft == 0) 2941081bba5bSNoriaki TAKAMIYA ifa_flags = IFA_F_DEPRECATED; 2942081bba5bSNoriaki TAKAMIYA else if ((prefered_lft >= 0x7FFFFFFF/HZ) && 2943081bba5bSNoriaki TAKAMIYA (prefered_lft != INFINITY_LIFE_TIME)) 2944081bba5bSNoriaki TAKAMIYA prefered_lft = 0x7FFFFFFF/HZ; 2945081bba5bSNoriaki TAKAMIYA 2946081bba5bSNoriaki TAKAMIYA spin_lock_bh(&ifp->lock); 2947081bba5bSNoriaki TAKAMIYA ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED|IFA_F_PERMANENT)) | ifa_flags; 2948081bba5bSNoriaki TAKAMIYA 2949081bba5bSNoriaki TAKAMIYA ifp->tstamp = jiffies; 2950081bba5bSNoriaki TAKAMIYA ifp->valid_lft = valid_lft; 2951081bba5bSNoriaki TAKAMIYA ifp->prefered_lft = prefered_lft; 2952081bba5bSNoriaki TAKAMIYA 2953081bba5bSNoriaki TAKAMIYA spin_unlock_bh(&ifp->lock); 2954081bba5bSNoriaki TAKAMIYA if (!(ifp->flags&IFA_F_TENTATIVE)) 2955081bba5bSNoriaki TAKAMIYA ipv6_ifa_notify(0, ifp); 2956081bba5bSNoriaki TAKAMIYA in6_ifa_put(ifp); 2957081bba5bSNoriaki TAKAMIYA 2958081bba5bSNoriaki TAKAMIYA addrconf_verify(0); 2959081bba5bSNoriaki TAKAMIYA 2960081bba5bSNoriaki TAKAMIYA return 0; 2961081bba5bSNoriaki TAKAMIYA } 2962081bba5bSNoriaki TAKAMIYA 2963081bba5bSNoriaki TAKAMIYA static int 29641da177e4SLinus Torvalds inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 29651da177e4SLinus Torvalds { 2966461d8837SThomas Graf struct ifaddrmsg *ifm; 2967461d8837SThomas Graf struct nlattr *tb[IFA_MAX+1]; 29681da177e4SLinus Torvalds struct in6_addr *pfx; 2969461d8837SThomas Graf u32 valid_lft, preferred_lft; 2970461d8837SThomas Graf int err; 29711da177e4SLinus Torvalds 2972461d8837SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 2973461d8837SThomas Graf if (err < 0) 2974461d8837SThomas Graf return err; 2975461d8837SThomas Graf 2976461d8837SThomas Graf ifm = nlmsg_data(nlh); 2977461d8837SThomas Graf pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]); 29781da177e4SLinus Torvalds if (pfx == NULL) 29791da177e4SLinus Torvalds return -EINVAL; 29801da177e4SLinus Torvalds 2981461d8837SThomas Graf if (tb[IFA_CACHEINFO]) { 29820778769dSNoriaki TAKAMIYA struct ifa_cacheinfo *ci; 2983461d8837SThomas Graf 2984461d8837SThomas Graf ci = nla_data(tb[IFA_CACHEINFO]); 29850778769dSNoriaki TAKAMIYA valid_lft = ci->ifa_valid; 2986461d8837SThomas Graf preferred_lft = ci->ifa_prefered; 2987461d8837SThomas Graf } else { 2988461d8837SThomas Graf preferred_lft = INFINITY_LIFE_TIME; 2989461d8837SThomas Graf valid_lft = INFINITY_LIFE_TIME; 29900778769dSNoriaki TAKAMIYA } 29910778769dSNoriaki TAKAMIYA 2992081bba5bSNoriaki TAKAMIYA if (nlh->nlmsg_flags & NLM_F_REPLACE) { 2993461d8837SThomas Graf err = inet6_addr_modify(ifm->ifa_index, pfx, 2994461d8837SThomas Graf preferred_lft, valid_lft); 2995461d8837SThomas Graf if (err == 0 || !(nlh->nlmsg_flags & NLM_F_CREATE)) 2996461d8837SThomas Graf return err; 2997081bba5bSNoriaki TAKAMIYA } 2998081bba5bSNoriaki TAKAMIYA 29990778769dSNoriaki TAKAMIYA return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen, 3000461d8837SThomas Graf preferred_lft, valid_lft); 30011da177e4SLinus Torvalds } 30021da177e4SLinus Torvalds 3003101bb229SThomas Graf static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags, 3004101bb229SThomas Graf u8 scope, int ifindex) 3005101bb229SThomas Graf { 3006101bb229SThomas Graf struct ifaddrmsg *ifm; 3007101bb229SThomas Graf 3008101bb229SThomas Graf ifm = nlmsg_data(nlh); 3009101bb229SThomas Graf ifm->ifa_family = AF_INET6; 3010101bb229SThomas Graf ifm->ifa_prefixlen = prefixlen; 3011101bb229SThomas Graf ifm->ifa_flags = flags; 3012101bb229SThomas Graf ifm->ifa_scope = scope; 3013101bb229SThomas Graf ifm->ifa_index = ifindex; 3014101bb229SThomas Graf } 3015101bb229SThomas Graf 301685486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp, 301785486af0SThomas Graf unsigned long tstamp, u32 preferred, u32 valid) 301885486af0SThomas Graf { 301985486af0SThomas Graf struct ifa_cacheinfo ci; 302085486af0SThomas Graf 302185486af0SThomas Graf ci.cstamp = (u32)(TIME_DELTA(cstamp, INITIAL_JIFFIES) / HZ * 100 302285486af0SThomas Graf + TIME_DELTA(cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ); 302385486af0SThomas Graf ci.tstamp = (u32)(TIME_DELTA(tstamp, INITIAL_JIFFIES) / HZ * 100 302485486af0SThomas Graf + TIME_DELTA(tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ); 302585486af0SThomas Graf ci.ifa_prefered = preferred; 302685486af0SThomas Graf ci.ifa_valid = valid; 302785486af0SThomas Graf 302885486af0SThomas Graf return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci); 302985486af0SThomas Graf } 303085486af0SThomas Graf 3031101bb229SThomas Graf static inline int rt_scope(int ifa_scope) 3032101bb229SThomas Graf { 3033101bb229SThomas Graf if (ifa_scope & IFA_HOST) 3034101bb229SThomas Graf return RT_SCOPE_HOST; 3035101bb229SThomas Graf else if (ifa_scope & IFA_LINK) 3036101bb229SThomas Graf return RT_SCOPE_LINK; 3037101bb229SThomas Graf else if (ifa_scope & IFA_SITE) 3038101bb229SThomas Graf return RT_SCOPE_SITE; 3039101bb229SThomas Graf else 3040101bb229SThomas Graf return RT_SCOPE_UNIVERSE; 3041101bb229SThomas Graf } 3042101bb229SThomas Graf 3043*0ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void) 3044*0ab6803bSThomas Graf { 3045*0ab6803bSThomas Graf return nlmsg_total_size(sizeof(struct ifaddrmsg) + 3046*0ab6803bSThomas Graf nla_total_size(16) + 3047*0ab6803bSThomas Graf nla_total_size(sizeof(struct ifa_cacheinfo)) + 3048*0ab6803bSThomas Graf 128); 3049*0ab6803bSThomas Graf } 3050c5396a31SYOSHIFUJI Hideaki 30511da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa, 3052b6544c0bSJamal Hadi Salim u32 pid, u32 seq, int event, unsigned int flags) 30531da177e4SLinus Torvalds { 30541da177e4SLinus Torvalds struct nlmsghdr *nlh; 305585486af0SThomas Graf u32 preferred, valid; 30561da177e4SLinus Torvalds 3057*0ab6803bSThomas Graf nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags); 3058*0ab6803bSThomas Graf if (nlh == NULL) 3059*0ab6803bSThomas Graf return -ENOBUFS; 3060*0ab6803bSThomas Graf 3061101bb229SThomas Graf put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope), 3062101bb229SThomas Graf ifa->idev->dev->ifindex); 3063101bb229SThomas Graf 30641da177e4SLinus Torvalds if (!(ifa->flags&IFA_F_PERMANENT)) { 306585486af0SThomas Graf preferred = ifa->prefered_lft; 306685486af0SThomas Graf valid = ifa->valid_lft; 306785486af0SThomas Graf if (preferred != INFINITY_LIFE_TIME) { 30681da177e4SLinus Torvalds long tval = (jiffies - ifa->tstamp)/HZ; 306985486af0SThomas Graf preferred -= tval; 307085486af0SThomas Graf if (valid != INFINITY_LIFE_TIME) 307185486af0SThomas Graf valid -= tval; 30721da177e4SLinus Torvalds } 30731da177e4SLinus Torvalds } else { 307485486af0SThomas Graf preferred = INFINITY_LIFE_TIME; 307585486af0SThomas Graf valid = INFINITY_LIFE_TIME; 30761da177e4SLinus Torvalds } 307785486af0SThomas Graf 3078*0ab6803bSThomas Graf if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 || 3079*0ab6803bSThomas Graf put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) 3080*0ab6803bSThomas Graf return nlmsg_cancel(skb, nlh); 308185486af0SThomas Graf 3082*0ab6803bSThomas Graf return nlmsg_end(skb, nlh); 30831da177e4SLinus Torvalds } 30841da177e4SLinus Torvalds 30851da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca, 3086e431b8c0SJamal Hadi Salim u32 pid, u32 seq, int event, u16 flags) 30871da177e4SLinus Torvalds { 30881da177e4SLinus Torvalds struct nlmsghdr *nlh; 3089101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 3090101bb229SThomas Graf int ifindex = ifmca->idev->dev->ifindex; 30911da177e4SLinus Torvalds 30921da177e4SLinus Torvalds if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE) 3093101bb229SThomas Graf scope = RT_SCOPE_SITE; 3094101bb229SThomas Graf 3095*0ab6803bSThomas Graf nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags); 3096*0ab6803bSThomas Graf if (nlh == NULL) 3097*0ab6803bSThomas Graf return -ENOBUFS; 3098*0ab6803bSThomas Graf 3099101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 3100*0ab6803bSThomas Graf if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 || 3101*0ab6803bSThomas Graf put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp, 310285486af0SThomas Graf INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) 3103*0ab6803bSThomas Graf return nlmsg_cancel(skb, nlh); 310485486af0SThomas Graf 3105*0ab6803bSThomas Graf return nlmsg_end(skb, nlh); 31061da177e4SLinus Torvalds } 31071da177e4SLinus Torvalds 31081da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca, 3109b6544c0bSJamal Hadi Salim u32 pid, u32 seq, int event, unsigned int flags) 31101da177e4SLinus Torvalds { 31111da177e4SLinus Torvalds struct nlmsghdr *nlh; 3112101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 3113101bb229SThomas Graf int ifindex = ifaca->aca_idev->dev->ifindex; 31141da177e4SLinus Torvalds 31151da177e4SLinus Torvalds if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE) 3116101bb229SThomas Graf scope = RT_SCOPE_SITE; 3117101bb229SThomas Graf 3118*0ab6803bSThomas Graf nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags); 3119*0ab6803bSThomas Graf if (nlh == NULL) 3120*0ab6803bSThomas Graf return -ENOBUFS; 3121*0ab6803bSThomas Graf 3122101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 3123*0ab6803bSThomas Graf if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 || 3124*0ab6803bSThomas Graf put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp, 312585486af0SThomas Graf INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) 3126*0ab6803bSThomas Graf return nlmsg_cancel(skb, nlh); 312785486af0SThomas Graf 3128*0ab6803bSThomas Graf return nlmsg_end(skb, nlh); 31291da177e4SLinus Torvalds } 31301da177e4SLinus Torvalds 31311da177e4SLinus Torvalds enum addr_type_t 31321da177e4SLinus Torvalds { 31331da177e4SLinus Torvalds UNICAST_ADDR, 31341da177e4SLinus Torvalds MULTICAST_ADDR, 31351da177e4SLinus Torvalds ANYCAST_ADDR, 31361da177e4SLinus Torvalds }; 31371da177e4SLinus Torvalds 31381da177e4SLinus Torvalds static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb, 31391da177e4SLinus Torvalds enum addr_type_t type) 31401da177e4SLinus Torvalds { 31411da177e4SLinus Torvalds int idx, ip_idx; 31421da177e4SLinus Torvalds int s_idx, s_ip_idx; 31431da177e4SLinus Torvalds int err = 1; 31441da177e4SLinus Torvalds struct net_device *dev; 31451da177e4SLinus Torvalds struct inet6_dev *idev = NULL; 31461da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 31471da177e4SLinus Torvalds struct ifmcaddr6 *ifmca; 31481da177e4SLinus Torvalds struct ifacaddr6 *ifaca; 31491da177e4SLinus Torvalds 31501da177e4SLinus Torvalds s_idx = cb->args[0]; 31511da177e4SLinus Torvalds s_ip_idx = ip_idx = cb->args[1]; 31521da177e4SLinus Torvalds read_lock(&dev_base_lock); 31531da177e4SLinus Torvalds 31541da177e4SLinus Torvalds for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) { 31551da177e4SLinus Torvalds if (idx < s_idx) 31561da177e4SLinus Torvalds continue; 31571da177e4SLinus Torvalds if (idx > s_idx) 31581da177e4SLinus Torvalds s_ip_idx = 0; 31591da177e4SLinus Torvalds ip_idx = 0; 31601da177e4SLinus Torvalds if ((idev = in6_dev_get(dev)) == NULL) 31611da177e4SLinus Torvalds continue; 31621da177e4SLinus Torvalds read_lock_bh(&idev->lock); 31631da177e4SLinus Torvalds switch (type) { 31641da177e4SLinus Torvalds case UNICAST_ADDR: 3165ae9cda5dSYOSHIFUJI Hideaki /* unicast address incl. temp addr */ 31661da177e4SLinus Torvalds for (ifa = idev->addr_list; ifa; 31671da177e4SLinus Torvalds ifa = ifa->if_next, ip_idx++) { 31681da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 31691da177e4SLinus Torvalds continue; 31701da177e4SLinus Torvalds if ((err = inet6_fill_ifaddr(skb, ifa, 31711da177e4SLinus Torvalds NETLINK_CB(cb->skb).pid, 3172b6544c0bSJamal Hadi Salim cb->nlh->nlmsg_seq, RTM_NEWADDR, 3173b6544c0bSJamal Hadi Salim NLM_F_MULTI)) <= 0) 31741da177e4SLinus Torvalds goto done; 31751da177e4SLinus Torvalds } 31761da177e4SLinus Torvalds break; 31771da177e4SLinus Torvalds case MULTICAST_ADDR: 31781da177e4SLinus Torvalds /* multicast address */ 31791da177e4SLinus Torvalds for (ifmca = idev->mc_list; ifmca; 31801da177e4SLinus Torvalds ifmca = ifmca->next, ip_idx++) { 31811da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 31821da177e4SLinus Torvalds continue; 31831da177e4SLinus Torvalds if ((err = inet6_fill_ifmcaddr(skb, ifmca, 31841da177e4SLinus Torvalds NETLINK_CB(cb->skb).pid, 3185b6544c0bSJamal Hadi Salim cb->nlh->nlmsg_seq, RTM_GETMULTICAST, 3186b6544c0bSJamal Hadi Salim NLM_F_MULTI)) <= 0) 31871da177e4SLinus Torvalds goto done; 31881da177e4SLinus Torvalds } 31891da177e4SLinus Torvalds break; 31901da177e4SLinus Torvalds case ANYCAST_ADDR: 31911da177e4SLinus Torvalds /* anycast address */ 31921da177e4SLinus Torvalds for (ifaca = idev->ac_list; ifaca; 31931da177e4SLinus Torvalds ifaca = ifaca->aca_next, ip_idx++) { 31941da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 31951da177e4SLinus Torvalds continue; 31961da177e4SLinus Torvalds if ((err = inet6_fill_ifacaddr(skb, ifaca, 31971da177e4SLinus Torvalds NETLINK_CB(cb->skb).pid, 3198b6544c0bSJamal Hadi Salim cb->nlh->nlmsg_seq, RTM_GETANYCAST, 3199b6544c0bSJamal Hadi Salim NLM_F_MULTI)) <= 0) 32001da177e4SLinus Torvalds goto done; 32011da177e4SLinus Torvalds } 32021da177e4SLinus Torvalds break; 32031da177e4SLinus Torvalds default: 32041da177e4SLinus Torvalds break; 32051da177e4SLinus Torvalds } 32061da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 32071da177e4SLinus Torvalds in6_dev_put(idev); 32081da177e4SLinus Torvalds } 32091da177e4SLinus Torvalds done: 32101da177e4SLinus Torvalds if (err <= 0) { 32111da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 32121da177e4SLinus Torvalds in6_dev_put(idev); 32131da177e4SLinus Torvalds } 32141da177e4SLinus Torvalds read_unlock(&dev_base_lock); 32151da177e4SLinus Torvalds cb->args[0] = idx; 32161da177e4SLinus Torvalds cb->args[1] = ip_idx; 32171da177e4SLinus Torvalds return skb->len; 32181da177e4SLinus Torvalds } 32191da177e4SLinus Torvalds 32201da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) 32211da177e4SLinus Torvalds { 32221da177e4SLinus Torvalds enum addr_type_t type = UNICAST_ADDR; 32231da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 32241da177e4SLinus Torvalds } 32251da177e4SLinus Torvalds 32261da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb) 32271da177e4SLinus Torvalds { 32281da177e4SLinus Torvalds enum addr_type_t type = MULTICAST_ADDR; 32291da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 32301da177e4SLinus Torvalds } 32311da177e4SLinus Torvalds 32321da177e4SLinus Torvalds 32331da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb) 32341da177e4SLinus Torvalds { 32351da177e4SLinus Torvalds enum addr_type_t type = ANYCAST_ADDR; 32361da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 32371da177e4SLinus Torvalds } 32381da177e4SLinus Torvalds 32391b29fc2cSThomas Graf static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh, 32401b29fc2cSThomas Graf void *arg) 32416c223828SNoriaki TAKAMIYA { 32421b29fc2cSThomas Graf struct ifaddrmsg *ifm; 32431b29fc2cSThomas Graf struct nlattr *tb[IFA_MAX+1]; 32446c223828SNoriaki TAKAMIYA struct in6_addr *addr = NULL; 32456c223828SNoriaki TAKAMIYA struct net_device *dev = NULL; 32466c223828SNoriaki TAKAMIYA struct inet6_ifaddr *ifa; 32476c223828SNoriaki TAKAMIYA struct sk_buff *skb; 32486c223828SNoriaki TAKAMIYA int err; 32496c223828SNoriaki TAKAMIYA 32501b29fc2cSThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 32511b29fc2cSThomas Graf if (err < 0) 32521b29fc2cSThomas Graf goto errout; 32536c223828SNoriaki TAKAMIYA 32541b29fc2cSThomas Graf addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]); 32551b29fc2cSThomas Graf if (addr == NULL) { 32561b29fc2cSThomas Graf err = -EINVAL; 32571b29fc2cSThomas Graf goto errout; 32581b29fc2cSThomas Graf } 32591b29fc2cSThomas Graf 32601b29fc2cSThomas Graf ifm = nlmsg_data(nlh); 32616c223828SNoriaki TAKAMIYA if (ifm->ifa_index) 32626c223828SNoriaki TAKAMIYA dev = __dev_get_by_index(ifm->ifa_index); 32636c223828SNoriaki TAKAMIYA 32641b29fc2cSThomas Graf if ((ifa = ipv6_get_ifaddr(addr, dev, 1)) == NULL) { 32651b29fc2cSThomas Graf err = -EADDRNOTAVAIL; 32661b29fc2cSThomas Graf goto errout; 32676c223828SNoriaki TAKAMIYA } 32686c223828SNoriaki TAKAMIYA 3269*0ab6803bSThomas Graf if ((skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL)) == NULL) { 32701b29fc2cSThomas Graf err = -ENOBUFS; 32711b29fc2cSThomas Graf goto errout_ifa; 32721b29fc2cSThomas Graf } 32731b29fc2cSThomas Graf 32746c223828SNoriaki TAKAMIYA err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).pid, 32756c223828SNoriaki TAKAMIYA nlh->nlmsg_seq, RTM_NEWADDR, 0); 32766c223828SNoriaki TAKAMIYA if (err < 0) { 32771b29fc2cSThomas Graf kfree_skb(skb); 32781b29fc2cSThomas Graf goto errout_ifa; 32796c223828SNoriaki TAKAMIYA } 32806c223828SNoriaki TAKAMIYA 32812942e900SThomas Graf err = rtnl_unicast(skb, NETLINK_CB(in_skb).pid); 32821b29fc2cSThomas Graf errout_ifa: 32836c223828SNoriaki TAKAMIYA in6_ifa_put(ifa); 32841b29fc2cSThomas Graf errout: 32856c223828SNoriaki TAKAMIYA return err; 32866c223828SNoriaki TAKAMIYA } 32876c223828SNoriaki TAKAMIYA 32881da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa) 32891da177e4SLinus Torvalds { 32901da177e4SLinus Torvalds struct sk_buff *skb; 32915d620266SThomas Graf int err = -ENOBUFS; 32921da177e4SLinus Torvalds 3293*0ab6803bSThomas Graf skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC); 32945d620266SThomas Graf if (skb == NULL) 32955d620266SThomas Graf goto errout; 32965d620266SThomas Graf 32975d620266SThomas Graf err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0); 32985d620266SThomas Graf if (err < 0) { 32991da177e4SLinus Torvalds kfree_skb(skb); 33005d620266SThomas Graf goto errout; 33011da177e4SLinus Torvalds } 33025d620266SThomas Graf 33035d620266SThomas Graf err = rtnl_notify(skb, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC); 33045d620266SThomas Graf errout: 33055d620266SThomas Graf if (err < 0) 33065d620266SThomas Graf rtnl_set_sk_err(RTNLGRP_IPV6_IFADDR, err); 33071da177e4SLinus Torvalds } 33081da177e4SLinus Torvalds 33091da177e4SLinus Torvalds static void inline ipv6_store_devconf(struct ipv6_devconf *cnf, 33101da177e4SLinus Torvalds __s32 *array, int bytes) 33111da177e4SLinus Torvalds { 33121da177e4SLinus Torvalds memset(array, 0, bytes); 33131da177e4SLinus Torvalds array[DEVCONF_FORWARDING] = cnf->forwarding; 33141da177e4SLinus Torvalds array[DEVCONF_HOPLIMIT] = cnf->hop_limit; 33151da177e4SLinus Torvalds array[DEVCONF_MTU6] = cnf->mtu6; 33161da177e4SLinus Torvalds array[DEVCONF_ACCEPT_RA] = cnf->accept_ra; 33171da177e4SLinus Torvalds array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects; 33181da177e4SLinus Torvalds array[DEVCONF_AUTOCONF] = cnf->autoconf; 33191da177e4SLinus Torvalds array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits; 33201da177e4SLinus Torvalds array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits; 33211da177e4SLinus Torvalds array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval; 33221da177e4SLinus Torvalds array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay; 33231da177e4SLinus Torvalds array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version; 33241da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 33251da177e4SLinus Torvalds array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr; 33261da177e4SLinus Torvalds array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft; 33271da177e4SLinus Torvalds array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft; 33281da177e4SLinus Torvalds array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry; 33291da177e4SLinus Torvalds array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor; 33301da177e4SLinus Torvalds #endif 33311da177e4SLinus Torvalds array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses; 333265f5c7c1SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr; 3333c4fd30ebSYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo; 3334930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 3335930d6ff2SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref; 333652e16356SYOSHIFUJI Hideaki array[DEVCONF_RTR_PROBE_INTERVAL] = cnf->rtr_probe_interval; 333709c884d4SYOSHIFUJI Hideaki #ifdef CONFIV_IPV6_ROUTE_INFO 333809c884d4SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen; 333909c884d4SYOSHIFUJI Hideaki #endif 3340930d6ff2SYOSHIFUJI Hideaki #endif 33411da177e4SLinus Torvalds } 33421da177e4SLinus Torvalds 3343c5396a31SYOSHIFUJI Hideaki /* Maximum length of ifinfomsg attributes */ 3344c5396a31SYOSHIFUJI Hideaki #define INET6_IFINFO_RTA_SPACE \ 3345c5396a31SYOSHIFUJI Hideaki RTA_SPACE(IFNAMSIZ) /* IFNAME */ + \ 3346c5396a31SYOSHIFUJI Hideaki RTA_SPACE(MAX_ADDR_LEN) /* ADDRESS */ + \ 3347c5396a31SYOSHIFUJI Hideaki RTA_SPACE(sizeof(u32)) /* MTU */ + \ 3348c5396a31SYOSHIFUJI Hideaki RTA_SPACE(sizeof(int)) /* LINK */ + \ 3349c5396a31SYOSHIFUJI Hideaki RTA_SPACE(0) /* PROTINFO */ + \ 3350c5396a31SYOSHIFUJI Hideaki RTA_SPACE(sizeof(u32)) /* FLAGS */ + \ 3351c5396a31SYOSHIFUJI Hideaki RTA_SPACE(sizeof(struct ifla_cacheinfo)) /* CACHEINFO */ + \ 3352c5396a31SYOSHIFUJI Hideaki RTA_SPACE(sizeof(__s32[DEVCONF_MAX])) /* CONF */ 3353c5396a31SYOSHIFUJI Hideaki 33541da177e4SLinus Torvalds static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev, 3355b6544c0bSJamal Hadi Salim u32 pid, u32 seq, int event, unsigned int flags) 33561da177e4SLinus Torvalds { 33571da177e4SLinus Torvalds struct net_device *dev = idev->dev; 33581da177e4SLinus Torvalds __s32 *array = NULL; 33591da177e4SLinus Torvalds struct ifinfomsg *r; 33601da177e4SLinus Torvalds struct nlmsghdr *nlh; 33611da177e4SLinus Torvalds unsigned char *b = skb->tail; 33621da177e4SLinus Torvalds struct rtattr *subattr; 33631da177e4SLinus Torvalds __u32 mtu = dev->mtu; 33641da177e4SLinus Torvalds struct ifla_cacheinfo ci; 33651da177e4SLinus Torvalds 3366b6544c0bSJamal Hadi Salim nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*r), flags); 33671da177e4SLinus Torvalds r = NLMSG_DATA(nlh); 33681da177e4SLinus Torvalds r->ifi_family = AF_INET6; 33699ef1d4c7SPatrick McHardy r->__ifi_pad = 0; 33701da177e4SLinus Torvalds r->ifi_type = dev->type; 33711da177e4SLinus Torvalds r->ifi_index = dev->ifindex; 33721da177e4SLinus Torvalds r->ifi_flags = dev_get_flags(dev); 33731da177e4SLinus Torvalds r->ifi_change = 0; 33741da177e4SLinus Torvalds 33751da177e4SLinus Torvalds RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name); 33761da177e4SLinus Torvalds 33771da177e4SLinus Torvalds if (dev->addr_len) 33781da177e4SLinus Torvalds RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr); 33791da177e4SLinus Torvalds 33801da177e4SLinus Torvalds RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu); 33811da177e4SLinus Torvalds if (dev->ifindex != dev->iflink) 33821da177e4SLinus Torvalds RTA_PUT(skb, IFLA_LINK, sizeof(int), &dev->iflink); 33831da177e4SLinus Torvalds 33841da177e4SLinus Torvalds subattr = (struct rtattr*)skb->tail; 33851da177e4SLinus Torvalds 33861da177e4SLinus Torvalds RTA_PUT(skb, IFLA_PROTINFO, 0, NULL); 33871da177e4SLinus Torvalds 33881da177e4SLinus Torvalds /* return the device flags */ 33891da177e4SLinus Torvalds RTA_PUT(skb, IFLA_INET6_FLAGS, sizeof(__u32), &idev->if_flags); 33901da177e4SLinus Torvalds 33911da177e4SLinus Torvalds /* return interface cacheinfo */ 33921da177e4SLinus Torvalds ci.max_reasm_len = IPV6_MAXPLEN; 33931da177e4SLinus Torvalds ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100 33941da177e4SLinus Torvalds + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ); 33951da177e4SLinus Torvalds ci.reachable_time = idev->nd_parms->reachable_time; 33961da177e4SLinus Torvalds ci.retrans_time = idev->nd_parms->retrans_time; 33971da177e4SLinus Torvalds RTA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci); 33981da177e4SLinus Torvalds 33991da177e4SLinus Torvalds /* return the device sysctl params */ 34001da177e4SLinus Torvalds if ((array = kmalloc(DEVCONF_MAX * sizeof(*array), GFP_ATOMIC)) == NULL) 34011da177e4SLinus Torvalds goto rtattr_failure; 34021da177e4SLinus Torvalds ipv6_store_devconf(&idev->cnf, array, DEVCONF_MAX * sizeof(*array)); 34031da177e4SLinus Torvalds RTA_PUT(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(*array), array); 34041da177e4SLinus Torvalds 34051da177e4SLinus Torvalds /* XXX - Statistics/MC not implemented */ 34061da177e4SLinus Torvalds subattr->rta_len = skb->tail - (u8*)subattr; 34071da177e4SLinus Torvalds 34081da177e4SLinus Torvalds nlh->nlmsg_len = skb->tail - b; 34091da177e4SLinus Torvalds kfree(array); 34101da177e4SLinus Torvalds return skb->len; 34111da177e4SLinus Torvalds 34121da177e4SLinus Torvalds nlmsg_failure: 34131da177e4SLinus Torvalds rtattr_failure: 34141da177e4SLinus Torvalds kfree(array); 34151da177e4SLinus Torvalds skb_trim(skb, b - skb->data); 34161da177e4SLinus Torvalds return -1; 34171da177e4SLinus Torvalds } 34181da177e4SLinus Torvalds 34191da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 34201da177e4SLinus Torvalds { 34211da177e4SLinus Torvalds int idx, err; 34221da177e4SLinus Torvalds int s_idx = cb->args[0]; 34231da177e4SLinus Torvalds struct net_device *dev; 34241da177e4SLinus Torvalds struct inet6_dev *idev; 34251da177e4SLinus Torvalds 34261da177e4SLinus Torvalds read_lock(&dev_base_lock); 34271da177e4SLinus Torvalds for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) { 34281da177e4SLinus Torvalds if (idx < s_idx) 34291da177e4SLinus Torvalds continue; 34301da177e4SLinus Torvalds if ((idev = in6_dev_get(dev)) == NULL) 34311da177e4SLinus Torvalds continue; 34321da177e4SLinus Torvalds err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid, 3433b6544c0bSJamal Hadi Salim cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI); 34341da177e4SLinus Torvalds in6_dev_put(idev); 34351da177e4SLinus Torvalds if (err <= 0) 34361da177e4SLinus Torvalds break; 34371da177e4SLinus Torvalds } 34381da177e4SLinus Torvalds read_unlock(&dev_base_lock); 34391da177e4SLinus Torvalds cb->args[0] = idx; 34401da177e4SLinus Torvalds 34411da177e4SLinus Torvalds return skb->len; 34421da177e4SLinus Torvalds } 34431da177e4SLinus Torvalds 34441da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev) 34451da177e4SLinus Torvalds { 34461da177e4SLinus Torvalds struct sk_buff *skb; 34478d7a76c9SThomas Graf int payload = sizeof(struct ifinfomsg) + INET6_IFINFO_RTA_SPACE; 34488d7a76c9SThomas Graf int err = -ENOBUFS; 34491da177e4SLinus Torvalds 34508d7a76c9SThomas Graf skb = nlmsg_new(nlmsg_total_size(payload), GFP_ATOMIC); 34518d7a76c9SThomas Graf if (skb == NULL) 34528d7a76c9SThomas Graf goto errout; 34538d7a76c9SThomas Graf 34548d7a76c9SThomas Graf err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0); 34558d7a76c9SThomas Graf if (err < 0) { 34561da177e4SLinus Torvalds kfree_skb(skb); 34578d7a76c9SThomas Graf goto errout; 34581da177e4SLinus Torvalds } 34598d7a76c9SThomas Graf 34608d7a76c9SThomas Graf err = rtnl_notify(skb, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC); 34618d7a76c9SThomas Graf errout: 34628d7a76c9SThomas Graf if (err < 0) 34638d7a76c9SThomas Graf rtnl_set_sk_err(RTNLGRP_IPV6_IFADDR, err); 34641da177e4SLinus Torvalds } 34651da177e4SLinus Torvalds 3466c5396a31SYOSHIFUJI Hideaki /* Maximum length of prefix_cacheinfo attributes */ 3467c5396a31SYOSHIFUJI Hideaki #define INET6_PREFIX_RTA_SPACE \ 3468c5396a31SYOSHIFUJI Hideaki RTA_SPACE(sizeof(((struct prefix_info *)NULL)->prefix)) /* ADDRESS */ + \ 3469c5396a31SYOSHIFUJI Hideaki RTA_SPACE(sizeof(struct prefix_cacheinfo)) /* CACHEINFO */ 3470c5396a31SYOSHIFUJI Hideaki 34711da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev, 3472b6544c0bSJamal Hadi Salim struct prefix_info *pinfo, u32 pid, u32 seq, 3473b6544c0bSJamal Hadi Salim int event, unsigned int flags) 34741da177e4SLinus Torvalds { 34751da177e4SLinus Torvalds struct prefixmsg *pmsg; 34761da177e4SLinus Torvalds struct nlmsghdr *nlh; 34771da177e4SLinus Torvalds unsigned char *b = skb->tail; 34781da177e4SLinus Torvalds struct prefix_cacheinfo ci; 34791da177e4SLinus Torvalds 3480b6544c0bSJamal Hadi Salim nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*pmsg), flags); 34811da177e4SLinus Torvalds pmsg = NLMSG_DATA(nlh); 34821da177e4SLinus Torvalds pmsg->prefix_family = AF_INET6; 34838a47077aSPatrick McHardy pmsg->prefix_pad1 = 0; 34848a47077aSPatrick McHardy pmsg->prefix_pad2 = 0; 34851da177e4SLinus Torvalds pmsg->prefix_ifindex = idev->dev->ifindex; 34861da177e4SLinus Torvalds pmsg->prefix_len = pinfo->prefix_len; 34871da177e4SLinus Torvalds pmsg->prefix_type = pinfo->type; 34888a47077aSPatrick McHardy pmsg->prefix_pad3 = 0; 34891da177e4SLinus Torvalds 34901da177e4SLinus Torvalds pmsg->prefix_flags = 0; 34911da177e4SLinus Torvalds if (pinfo->onlink) 34921da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_ONLINK; 34931da177e4SLinus Torvalds if (pinfo->autoconf) 34941da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_AUTOCONF; 34951da177e4SLinus Torvalds 34961da177e4SLinus Torvalds RTA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix); 34971da177e4SLinus Torvalds 34981da177e4SLinus Torvalds ci.preferred_time = ntohl(pinfo->prefered); 34991da177e4SLinus Torvalds ci.valid_time = ntohl(pinfo->valid); 35001da177e4SLinus Torvalds RTA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci); 35011da177e4SLinus Torvalds 35021da177e4SLinus Torvalds nlh->nlmsg_len = skb->tail - b; 35031da177e4SLinus Torvalds return skb->len; 35041da177e4SLinus Torvalds 35051da177e4SLinus Torvalds nlmsg_failure: 35061da177e4SLinus Torvalds rtattr_failure: 35071da177e4SLinus Torvalds skb_trim(skb, b - skb->data); 35081da177e4SLinus Torvalds return -1; 35091da177e4SLinus Torvalds } 35101da177e4SLinus Torvalds 35111da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev, 35121da177e4SLinus Torvalds struct prefix_info *pinfo) 35131da177e4SLinus Torvalds { 35141da177e4SLinus Torvalds struct sk_buff *skb; 35158c384bfaSThomas Graf int payload = sizeof(struct prefixmsg) + INET6_PREFIX_RTA_SPACE; 35168c384bfaSThomas Graf int err = -ENOBUFS; 35171da177e4SLinus Torvalds 35188c384bfaSThomas Graf skb = nlmsg_new(nlmsg_total_size(payload), GFP_ATOMIC); 35198c384bfaSThomas Graf if (skb == NULL) 35208c384bfaSThomas Graf goto errout; 35218c384bfaSThomas Graf 35228c384bfaSThomas Graf err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0); 35238c384bfaSThomas Graf if (err < 0) { 35241da177e4SLinus Torvalds kfree_skb(skb); 35258c384bfaSThomas Graf goto errout; 35261da177e4SLinus Torvalds } 35278c384bfaSThomas Graf 35288c384bfaSThomas Graf err = rtnl_notify(skb, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC); 35298c384bfaSThomas Graf errout: 35308c384bfaSThomas Graf if (err < 0) 35318c384bfaSThomas Graf rtnl_set_sk_err(RTNLGRP_IPV6_PREFIX, err); 35321da177e4SLinus Torvalds } 35331da177e4SLinus Torvalds 3534db46edc6SThomas Graf static struct rtnetlink_link inet6_rtnetlink_table[RTM_NR_MSGTYPES] = { 35351da177e4SLinus Torvalds [RTM_GETLINK - RTM_BASE] = { .dumpit = inet6_dump_ifinfo, }, 35361da177e4SLinus Torvalds [RTM_NEWADDR - RTM_BASE] = { .doit = inet6_rtm_newaddr, }, 35371da177e4SLinus Torvalds [RTM_DELADDR - RTM_BASE] = { .doit = inet6_rtm_deladdr, }, 35386c223828SNoriaki TAKAMIYA [RTM_GETADDR - RTM_BASE] = { .doit = inet6_rtm_getaddr, 35396c223828SNoriaki TAKAMIYA .dumpit = inet6_dump_ifaddr, }, 35401da177e4SLinus Torvalds [RTM_GETMULTICAST - RTM_BASE] = { .dumpit = inet6_dump_ifmcaddr, }, 35411da177e4SLinus Torvalds [RTM_GETANYCAST - RTM_BASE] = { .dumpit = inet6_dump_ifacaddr, }, 35421da177e4SLinus Torvalds [RTM_NEWROUTE - RTM_BASE] = { .doit = inet6_rtm_newroute, }, 35431da177e4SLinus Torvalds [RTM_DELROUTE - RTM_BASE] = { .doit = inet6_rtm_delroute, }, 35441da177e4SLinus Torvalds [RTM_GETROUTE - RTM_BASE] = { .doit = inet6_rtm_getroute, 35451da177e4SLinus Torvalds .dumpit = inet6_dump_fib, }, 35468423a9aaSDavid S. Miller #ifdef CONFIG_IPV6_MULTIPLE_TABLES 3547101367c2SThomas Graf [RTM_GETRULE - RTM_BASE] = { .dumpit = fib6_rules_dump, }, 35488423a9aaSDavid S. Miller #endif 35491da177e4SLinus Torvalds }; 35501da177e4SLinus Torvalds 35511da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 35521da177e4SLinus Torvalds { 35531da177e4SLinus Torvalds inet6_ifa_notify(event ? : RTM_NEWADDR, ifp); 35541da177e4SLinus Torvalds 35551da177e4SLinus Torvalds switch (event) { 35561da177e4SLinus Torvalds case RTM_NEWADDR: 355740e22e8fSThomas Graf ip6_ins_rt(ifp->rt); 35581da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 35591da177e4SLinus Torvalds addrconf_join_anycast(ifp); 35601da177e4SLinus Torvalds break; 35611da177e4SLinus Torvalds case RTM_DELADDR: 35621da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 35631da177e4SLinus Torvalds addrconf_leave_anycast(ifp); 35641da177e4SLinus Torvalds addrconf_leave_solict(ifp->idev, &ifp->addr); 35651da177e4SLinus Torvalds dst_hold(&ifp->rt->u.dst); 3566e0a1ad73SThomas Graf if (ip6_del_rt(ifp->rt)) 35671da177e4SLinus Torvalds dst_free(&ifp->rt->u.dst); 35681da177e4SLinus Torvalds break; 35691da177e4SLinus Torvalds } 35701da177e4SLinus Torvalds } 35711da177e4SLinus Torvalds 35721da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 35731da177e4SLinus Torvalds { 35741da177e4SLinus Torvalds read_lock_bh(&addrconf_lock); 35751da177e4SLinus Torvalds if (likely(ifp->idev->dead == 0)) 35761da177e4SLinus Torvalds __ipv6_ifa_notify(event, ifp); 35771da177e4SLinus Torvalds read_unlock_bh(&addrconf_lock); 35781da177e4SLinus Torvalds } 35791da177e4SLinus Torvalds 35801da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 35811da177e4SLinus Torvalds 35821da177e4SLinus Torvalds static 35831da177e4SLinus Torvalds int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp, 35841da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 35851da177e4SLinus Torvalds { 35861da177e4SLinus Torvalds int *valp = ctl->data; 35871da177e4SLinus Torvalds int val = *valp; 35881da177e4SLinus Torvalds int ret; 35891da177e4SLinus Torvalds 35901da177e4SLinus Torvalds ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos); 35911da177e4SLinus Torvalds 35921da177e4SLinus Torvalds if (write && valp != &ipv6_devconf_dflt.forwarding) { 35931da177e4SLinus Torvalds if (valp != &ipv6_devconf.forwarding) { 35941da177e4SLinus Torvalds if ((!*valp) ^ (!val)) { 35951da177e4SLinus Torvalds struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1; 35961da177e4SLinus Torvalds if (idev == NULL) 35971da177e4SLinus Torvalds return ret; 35981da177e4SLinus Torvalds dev_forward_change(idev); 35991da177e4SLinus Torvalds } 36001da177e4SLinus Torvalds } else { 36011da177e4SLinus Torvalds ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding; 36021da177e4SLinus Torvalds addrconf_forward_change(); 36031da177e4SLinus Torvalds } 36041da177e4SLinus Torvalds if (*valp) 36051da177e4SLinus Torvalds rt6_purge_dflt_routers(); 36061da177e4SLinus Torvalds } 36071da177e4SLinus Torvalds 36081da177e4SLinus Torvalds return ret; 36091da177e4SLinus Torvalds } 36101da177e4SLinus Torvalds 36111da177e4SLinus Torvalds static int addrconf_sysctl_forward_strategy(ctl_table *table, 36121da177e4SLinus Torvalds int __user *name, int nlen, 36131da177e4SLinus Torvalds void __user *oldval, 36141da177e4SLinus Torvalds size_t __user *oldlenp, 36151da177e4SLinus Torvalds void __user *newval, size_t newlen, 36161da177e4SLinus Torvalds void **context) 36171da177e4SLinus Torvalds { 36181da177e4SLinus Torvalds int *valp = table->data; 36191da177e4SLinus Torvalds int new; 36201da177e4SLinus Torvalds 36211da177e4SLinus Torvalds if (!newval || !newlen) 36221da177e4SLinus Torvalds return 0; 36231da177e4SLinus Torvalds if (newlen != sizeof(int)) 36241da177e4SLinus Torvalds return -EINVAL; 36251da177e4SLinus Torvalds if (get_user(new, (int __user *)newval)) 36261da177e4SLinus Torvalds return -EFAULT; 36271da177e4SLinus Torvalds if (new == *valp) 36281da177e4SLinus Torvalds return 0; 36291da177e4SLinus Torvalds if (oldval && oldlenp) { 36301da177e4SLinus Torvalds size_t len; 36311da177e4SLinus Torvalds if (get_user(len, oldlenp)) 36321da177e4SLinus Torvalds return -EFAULT; 36331da177e4SLinus Torvalds if (len) { 36341da177e4SLinus Torvalds if (len > table->maxlen) 36351da177e4SLinus Torvalds len = table->maxlen; 36361da177e4SLinus Torvalds if (copy_to_user(oldval, valp, len)) 36371da177e4SLinus Torvalds return -EFAULT; 36381da177e4SLinus Torvalds if (put_user(len, oldlenp)) 36391da177e4SLinus Torvalds return -EFAULT; 36401da177e4SLinus Torvalds } 36411da177e4SLinus Torvalds } 36421da177e4SLinus Torvalds 36431da177e4SLinus Torvalds if (valp != &ipv6_devconf_dflt.forwarding) { 36441da177e4SLinus Torvalds if (valp != &ipv6_devconf.forwarding) { 36451da177e4SLinus Torvalds struct inet6_dev *idev = (struct inet6_dev *)table->extra1; 36461da177e4SLinus Torvalds int changed; 36471da177e4SLinus Torvalds if (unlikely(idev == NULL)) 36481da177e4SLinus Torvalds return -ENODEV; 36491da177e4SLinus Torvalds changed = (!*valp) ^ (!new); 36501da177e4SLinus Torvalds *valp = new; 36511da177e4SLinus Torvalds if (changed) 36521da177e4SLinus Torvalds dev_forward_change(idev); 36531da177e4SLinus Torvalds } else { 36541da177e4SLinus Torvalds *valp = new; 36551da177e4SLinus Torvalds addrconf_forward_change(); 36561da177e4SLinus Torvalds } 36571da177e4SLinus Torvalds 36581da177e4SLinus Torvalds if (*valp) 36591da177e4SLinus Torvalds rt6_purge_dflt_routers(); 36601da177e4SLinus Torvalds } else 36611da177e4SLinus Torvalds *valp = new; 36621da177e4SLinus Torvalds 36631da177e4SLinus Torvalds return 1; 36641da177e4SLinus Torvalds } 36651da177e4SLinus Torvalds 36661da177e4SLinus Torvalds static struct addrconf_sysctl_table 36671da177e4SLinus Torvalds { 36681da177e4SLinus Torvalds struct ctl_table_header *sysctl_header; 36691da177e4SLinus Torvalds ctl_table addrconf_vars[__NET_IPV6_MAX]; 36701da177e4SLinus Torvalds ctl_table addrconf_dev[2]; 36711da177e4SLinus Torvalds ctl_table addrconf_conf_dir[2]; 36721da177e4SLinus Torvalds ctl_table addrconf_proto_dir[2]; 36731da177e4SLinus Torvalds ctl_table addrconf_root_dir[2]; 3674ab32ea5dSBrian Haley } addrconf_sysctl __read_mostly = { 36751da177e4SLinus Torvalds .sysctl_header = NULL, 36761da177e4SLinus Torvalds .addrconf_vars = { 36771da177e4SLinus Torvalds { 36781da177e4SLinus Torvalds .ctl_name = NET_IPV6_FORWARDING, 36791da177e4SLinus Torvalds .procname = "forwarding", 36801da177e4SLinus Torvalds .data = &ipv6_devconf.forwarding, 36811da177e4SLinus Torvalds .maxlen = sizeof(int), 36821da177e4SLinus Torvalds .mode = 0644, 36831da177e4SLinus Torvalds .proc_handler = &addrconf_sysctl_forward, 36841da177e4SLinus Torvalds .strategy = &addrconf_sysctl_forward_strategy, 36851da177e4SLinus Torvalds }, 36861da177e4SLinus Torvalds { 36871da177e4SLinus Torvalds .ctl_name = NET_IPV6_HOP_LIMIT, 36881da177e4SLinus Torvalds .procname = "hop_limit", 36891da177e4SLinus Torvalds .data = &ipv6_devconf.hop_limit, 36901da177e4SLinus Torvalds .maxlen = sizeof(int), 36911da177e4SLinus Torvalds .mode = 0644, 36921da177e4SLinus Torvalds .proc_handler = proc_dointvec, 36931da177e4SLinus Torvalds }, 36941da177e4SLinus Torvalds { 36951da177e4SLinus Torvalds .ctl_name = NET_IPV6_MTU, 36961da177e4SLinus Torvalds .procname = "mtu", 36971da177e4SLinus Torvalds .data = &ipv6_devconf.mtu6, 36981da177e4SLinus Torvalds .maxlen = sizeof(int), 36991da177e4SLinus Torvalds .mode = 0644, 37001da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 37011da177e4SLinus Torvalds }, 37021da177e4SLinus Torvalds { 37031da177e4SLinus Torvalds .ctl_name = NET_IPV6_ACCEPT_RA, 37041da177e4SLinus Torvalds .procname = "accept_ra", 37051da177e4SLinus Torvalds .data = &ipv6_devconf.accept_ra, 37061da177e4SLinus Torvalds .maxlen = sizeof(int), 37071da177e4SLinus Torvalds .mode = 0644, 37081da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 37091da177e4SLinus Torvalds }, 37101da177e4SLinus Torvalds { 37111da177e4SLinus Torvalds .ctl_name = NET_IPV6_ACCEPT_REDIRECTS, 37121da177e4SLinus Torvalds .procname = "accept_redirects", 37131da177e4SLinus Torvalds .data = &ipv6_devconf.accept_redirects, 37141da177e4SLinus Torvalds .maxlen = sizeof(int), 37151da177e4SLinus Torvalds .mode = 0644, 37161da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 37171da177e4SLinus Torvalds }, 37181da177e4SLinus Torvalds { 37191da177e4SLinus Torvalds .ctl_name = NET_IPV6_AUTOCONF, 37201da177e4SLinus Torvalds .procname = "autoconf", 37211da177e4SLinus Torvalds .data = &ipv6_devconf.autoconf, 37221da177e4SLinus Torvalds .maxlen = sizeof(int), 37231da177e4SLinus Torvalds .mode = 0644, 37241da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 37251da177e4SLinus Torvalds }, 37261da177e4SLinus Torvalds { 37271da177e4SLinus Torvalds .ctl_name = NET_IPV6_DAD_TRANSMITS, 37281da177e4SLinus Torvalds .procname = "dad_transmits", 37291da177e4SLinus Torvalds .data = &ipv6_devconf.dad_transmits, 37301da177e4SLinus Torvalds .maxlen = sizeof(int), 37311da177e4SLinus Torvalds .mode = 0644, 37321da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 37331da177e4SLinus Torvalds }, 37341da177e4SLinus Torvalds { 37351da177e4SLinus Torvalds .ctl_name = NET_IPV6_RTR_SOLICITS, 37361da177e4SLinus Torvalds .procname = "router_solicitations", 37371da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicits, 37381da177e4SLinus Torvalds .maxlen = sizeof(int), 37391da177e4SLinus Torvalds .mode = 0644, 37401da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 37411da177e4SLinus Torvalds }, 37421da177e4SLinus Torvalds { 37431da177e4SLinus Torvalds .ctl_name = NET_IPV6_RTR_SOLICIT_INTERVAL, 37441da177e4SLinus Torvalds .procname = "router_solicitation_interval", 37451da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_interval, 37461da177e4SLinus Torvalds .maxlen = sizeof(int), 37471da177e4SLinus Torvalds .mode = 0644, 37481da177e4SLinus Torvalds .proc_handler = &proc_dointvec_jiffies, 37491da177e4SLinus Torvalds .strategy = &sysctl_jiffies, 37501da177e4SLinus Torvalds }, 37511da177e4SLinus Torvalds { 37521da177e4SLinus Torvalds .ctl_name = NET_IPV6_RTR_SOLICIT_DELAY, 37531da177e4SLinus Torvalds .procname = "router_solicitation_delay", 37541da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_delay, 37551da177e4SLinus Torvalds .maxlen = sizeof(int), 37561da177e4SLinus Torvalds .mode = 0644, 37571da177e4SLinus Torvalds .proc_handler = &proc_dointvec_jiffies, 37581da177e4SLinus Torvalds .strategy = &sysctl_jiffies, 37591da177e4SLinus Torvalds }, 37601da177e4SLinus Torvalds { 37611da177e4SLinus Torvalds .ctl_name = NET_IPV6_FORCE_MLD_VERSION, 37621da177e4SLinus Torvalds .procname = "force_mld_version", 37631da177e4SLinus Torvalds .data = &ipv6_devconf.force_mld_version, 37641da177e4SLinus Torvalds .maxlen = sizeof(int), 37651da177e4SLinus Torvalds .mode = 0644, 37661da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 37671da177e4SLinus Torvalds }, 37681da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 37691da177e4SLinus Torvalds { 37701da177e4SLinus Torvalds .ctl_name = NET_IPV6_USE_TEMPADDR, 37711da177e4SLinus Torvalds .procname = "use_tempaddr", 37721da177e4SLinus Torvalds .data = &ipv6_devconf.use_tempaddr, 37731da177e4SLinus Torvalds .maxlen = sizeof(int), 37741da177e4SLinus Torvalds .mode = 0644, 37751da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 37761da177e4SLinus Torvalds }, 37771da177e4SLinus Torvalds { 37781da177e4SLinus Torvalds .ctl_name = NET_IPV6_TEMP_VALID_LFT, 37791da177e4SLinus Torvalds .procname = "temp_valid_lft", 37801da177e4SLinus Torvalds .data = &ipv6_devconf.temp_valid_lft, 37811da177e4SLinus Torvalds .maxlen = sizeof(int), 37821da177e4SLinus Torvalds .mode = 0644, 37831da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 37841da177e4SLinus Torvalds }, 37851da177e4SLinus Torvalds { 37861da177e4SLinus Torvalds .ctl_name = NET_IPV6_TEMP_PREFERED_LFT, 37871da177e4SLinus Torvalds .procname = "temp_prefered_lft", 37881da177e4SLinus Torvalds .data = &ipv6_devconf.temp_prefered_lft, 37891da177e4SLinus Torvalds .maxlen = sizeof(int), 37901da177e4SLinus Torvalds .mode = 0644, 37911da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 37921da177e4SLinus Torvalds }, 37931da177e4SLinus Torvalds { 37941da177e4SLinus Torvalds .ctl_name = NET_IPV6_REGEN_MAX_RETRY, 37951da177e4SLinus Torvalds .procname = "regen_max_retry", 37961da177e4SLinus Torvalds .data = &ipv6_devconf.regen_max_retry, 37971da177e4SLinus Torvalds .maxlen = sizeof(int), 37981da177e4SLinus Torvalds .mode = 0644, 37991da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 38001da177e4SLinus Torvalds }, 38011da177e4SLinus Torvalds { 38021da177e4SLinus Torvalds .ctl_name = NET_IPV6_MAX_DESYNC_FACTOR, 38031da177e4SLinus Torvalds .procname = "max_desync_factor", 38041da177e4SLinus Torvalds .data = &ipv6_devconf.max_desync_factor, 38051da177e4SLinus Torvalds .maxlen = sizeof(int), 38061da177e4SLinus Torvalds .mode = 0644, 38071da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 38081da177e4SLinus Torvalds }, 38091da177e4SLinus Torvalds #endif 38101da177e4SLinus Torvalds { 38111da177e4SLinus Torvalds .ctl_name = NET_IPV6_MAX_ADDRESSES, 38121da177e4SLinus Torvalds .procname = "max_addresses", 38131da177e4SLinus Torvalds .data = &ipv6_devconf.max_addresses, 38141da177e4SLinus Torvalds .maxlen = sizeof(int), 38151da177e4SLinus Torvalds .mode = 0644, 38161da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 38171da177e4SLinus Torvalds }, 38181da177e4SLinus Torvalds { 381965f5c7c1SYOSHIFUJI Hideaki .ctl_name = NET_IPV6_ACCEPT_RA_DEFRTR, 382065f5c7c1SYOSHIFUJI Hideaki .procname = "accept_ra_defrtr", 382165f5c7c1SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_defrtr, 382265f5c7c1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 382365f5c7c1SYOSHIFUJI Hideaki .mode = 0644, 382465f5c7c1SYOSHIFUJI Hideaki .proc_handler = &proc_dointvec, 382565f5c7c1SYOSHIFUJI Hideaki }, 382665f5c7c1SYOSHIFUJI Hideaki { 3827c4fd30ebSYOSHIFUJI Hideaki .ctl_name = NET_IPV6_ACCEPT_RA_PINFO, 3828c4fd30ebSYOSHIFUJI Hideaki .procname = "accept_ra_pinfo", 3829c4fd30ebSYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_pinfo, 3830c4fd30ebSYOSHIFUJI Hideaki .maxlen = sizeof(int), 3831c4fd30ebSYOSHIFUJI Hideaki .mode = 0644, 3832c4fd30ebSYOSHIFUJI Hideaki .proc_handler = &proc_dointvec, 3833c4fd30ebSYOSHIFUJI Hideaki }, 3834930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 3835930d6ff2SYOSHIFUJI Hideaki { 3836930d6ff2SYOSHIFUJI Hideaki .ctl_name = NET_IPV6_ACCEPT_RA_RTR_PREF, 3837930d6ff2SYOSHIFUJI Hideaki .procname = "accept_ra_rtr_pref", 3838930d6ff2SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rtr_pref, 3839930d6ff2SYOSHIFUJI Hideaki .maxlen = sizeof(int), 3840930d6ff2SYOSHIFUJI Hideaki .mode = 0644, 3841930d6ff2SYOSHIFUJI Hideaki .proc_handler = &proc_dointvec, 3842930d6ff2SYOSHIFUJI Hideaki }, 384352e16356SYOSHIFUJI Hideaki { 384452e16356SYOSHIFUJI Hideaki .ctl_name = NET_IPV6_RTR_PROBE_INTERVAL, 384552e16356SYOSHIFUJI Hideaki .procname = "router_probe_interval", 384652e16356SYOSHIFUJI Hideaki .data = &ipv6_devconf.rtr_probe_interval, 384752e16356SYOSHIFUJI Hideaki .maxlen = sizeof(int), 384852e16356SYOSHIFUJI Hideaki .mode = 0644, 384952e16356SYOSHIFUJI Hideaki .proc_handler = &proc_dointvec_jiffies, 385052e16356SYOSHIFUJI Hideaki .strategy = &sysctl_jiffies, 385152e16356SYOSHIFUJI Hideaki }, 385209c884d4SYOSHIFUJI Hideaki #ifdef CONFIV_IPV6_ROUTE_INFO 385309c884d4SYOSHIFUJI Hideaki { 385409c884d4SYOSHIFUJI Hideaki .ctl_name = NET_IPV6_ACCEPT_RA_RT_INFO_MAX_PLEN, 385509c884d4SYOSHIFUJI Hideaki .procname = "accept_ra_rt_info_max_plen", 385609c884d4SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rt_info_max_plen, 385709c884d4SYOSHIFUJI Hideaki .maxlen = sizeof(int), 385809c884d4SYOSHIFUJI Hideaki .mode = 0644, 385909c884d4SYOSHIFUJI Hideaki .proc_handler = &proc_dointvec, 386009c884d4SYOSHIFUJI Hideaki }, 386109c884d4SYOSHIFUJI Hideaki #endif 3862930d6ff2SYOSHIFUJI Hideaki #endif 3863c4fd30ebSYOSHIFUJI Hideaki { 38641da177e4SLinus Torvalds .ctl_name = 0, /* sentinel */ 38651da177e4SLinus Torvalds } 38661da177e4SLinus Torvalds }, 38671da177e4SLinus Torvalds .addrconf_dev = { 38681da177e4SLinus Torvalds { 38691da177e4SLinus Torvalds .ctl_name = NET_PROTO_CONF_ALL, 38701da177e4SLinus Torvalds .procname = "all", 38711da177e4SLinus Torvalds .mode = 0555, 38721da177e4SLinus Torvalds .child = addrconf_sysctl.addrconf_vars, 38731da177e4SLinus Torvalds }, 38741da177e4SLinus Torvalds { 38751da177e4SLinus Torvalds .ctl_name = 0, /* sentinel */ 38761da177e4SLinus Torvalds } 38771da177e4SLinus Torvalds }, 38781da177e4SLinus Torvalds .addrconf_conf_dir = { 38791da177e4SLinus Torvalds { 38801da177e4SLinus Torvalds .ctl_name = NET_IPV6_CONF, 38811da177e4SLinus Torvalds .procname = "conf", 38821da177e4SLinus Torvalds .mode = 0555, 38831da177e4SLinus Torvalds .child = addrconf_sysctl.addrconf_dev, 38841da177e4SLinus Torvalds }, 38851da177e4SLinus Torvalds { 38861da177e4SLinus Torvalds .ctl_name = 0, /* sentinel */ 38871da177e4SLinus Torvalds } 38881da177e4SLinus Torvalds }, 38891da177e4SLinus Torvalds .addrconf_proto_dir = { 38901da177e4SLinus Torvalds { 38911da177e4SLinus Torvalds .ctl_name = NET_IPV6, 38921da177e4SLinus Torvalds .procname = "ipv6", 38931da177e4SLinus Torvalds .mode = 0555, 38941da177e4SLinus Torvalds .child = addrconf_sysctl.addrconf_conf_dir, 38951da177e4SLinus Torvalds }, 38961da177e4SLinus Torvalds { 38971da177e4SLinus Torvalds .ctl_name = 0, /* sentinel */ 38981da177e4SLinus Torvalds } 38991da177e4SLinus Torvalds }, 39001da177e4SLinus Torvalds .addrconf_root_dir = { 39011da177e4SLinus Torvalds { 39021da177e4SLinus Torvalds .ctl_name = CTL_NET, 39031da177e4SLinus Torvalds .procname = "net", 39041da177e4SLinus Torvalds .mode = 0555, 39051da177e4SLinus Torvalds .child = addrconf_sysctl.addrconf_proto_dir, 39061da177e4SLinus Torvalds }, 39071da177e4SLinus Torvalds { 39081da177e4SLinus Torvalds .ctl_name = 0, /* sentinel */ 39091da177e4SLinus Torvalds } 39101da177e4SLinus Torvalds }, 39111da177e4SLinus Torvalds }; 39121da177e4SLinus Torvalds 39131da177e4SLinus Torvalds static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p) 39141da177e4SLinus Torvalds { 39151da177e4SLinus Torvalds int i; 39161da177e4SLinus Torvalds struct net_device *dev = idev ? idev->dev : NULL; 39171da177e4SLinus Torvalds struct addrconf_sysctl_table *t; 39181da177e4SLinus Torvalds char *dev_name = NULL; 39191da177e4SLinus Torvalds 39201da177e4SLinus Torvalds t = kmalloc(sizeof(*t), GFP_KERNEL); 39211da177e4SLinus Torvalds if (t == NULL) 39221da177e4SLinus Torvalds return; 39231da177e4SLinus Torvalds memcpy(t, &addrconf_sysctl, sizeof(*t)); 39241da177e4SLinus Torvalds for (i=0; t->addrconf_vars[i].data; i++) { 39251da177e4SLinus Torvalds t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf; 39261da177e4SLinus Torvalds t->addrconf_vars[i].de = NULL; 39271da177e4SLinus Torvalds t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */ 39281da177e4SLinus Torvalds } 39291da177e4SLinus Torvalds if (dev) { 39301da177e4SLinus Torvalds dev_name = dev->name; 39311da177e4SLinus Torvalds t->addrconf_dev[0].ctl_name = dev->ifindex; 39321da177e4SLinus Torvalds } else { 39331da177e4SLinus Torvalds dev_name = "default"; 39341da177e4SLinus Torvalds t->addrconf_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT; 39351da177e4SLinus Torvalds } 39361da177e4SLinus Torvalds 39371da177e4SLinus Torvalds /* 39381da177e4SLinus Torvalds * Make a copy of dev_name, because '.procname' is regarded as const 39391da177e4SLinus Torvalds * by sysctl and we wouldn't want anyone to change it under our feet 39401da177e4SLinus Torvalds * (see SIOCSIFNAME). 39411da177e4SLinus Torvalds */ 3942543537bdSPaulo Marques dev_name = kstrdup(dev_name, GFP_KERNEL); 39431da177e4SLinus Torvalds if (!dev_name) 39441da177e4SLinus Torvalds goto free; 39451da177e4SLinus Torvalds 39461da177e4SLinus Torvalds t->addrconf_dev[0].procname = dev_name; 39471da177e4SLinus Torvalds 39481da177e4SLinus Torvalds t->addrconf_dev[0].child = t->addrconf_vars; 39491da177e4SLinus Torvalds t->addrconf_dev[0].de = NULL; 39501da177e4SLinus Torvalds t->addrconf_conf_dir[0].child = t->addrconf_dev; 39511da177e4SLinus Torvalds t->addrconf_conf_dir[0].de = NULL; 39521da177e4SLinus Torvalds t->addrconf_proto_dir[0].child = t->addrconf_conf_dir; 39531da177e4SLinus Torvalds t->addrconf_proto_dir[0].de = NULL; 39541da177e4SLinus Torvalds t->addrconf_root_dir[0].child = t->addrconf_proto_dir; 39551da177e4SLinus Torvalds t->addrconf_root_dir[0].de = NULL; 39561da177e4SLinus Torvalds 39571da177e4SLinus Torvalds t->sysctl_header = register_sysctl_table(t->addrconf_root_dir, 0); 39581da177e4SLinus Torvalds if (t->sysctl_header == NULL) 39591da177e4SLinus Torvalds goto free_procname; 39601da177e4SLinus Torvalds else 39611da177e4SLinus Torvalds p->sysctl = t; 39621da177e4SLinus Torvalds return; 39631da177e4SLinus Torvalds 39641da177e4SLinus Torvalds /* error path */ 39651da177e4SLinus Torvalds free_procname: 39661da177e4SLinus Torvalds kfree(dev_name); 39671da177e4SLinus Torvalds free: 39681da177e4SLinus Torvalds kfree(t); 39691da177e4SLinus Torvalds 39701da177e4SLinus Torvalds return; 39711da177e4SLinus Torvalds } 39721da177e4SLinus Torvalds 39731da177e4SLinus Torvalds static void addrconf_sysctl_unregister(struct ipv6_devconf *p) 39741da177e4SLinus Torvalds { 39751da177e4SLinus Torvalds if (p->sysctl) { 39761da177e4SLinus Torvalds struct addrconf_sysctl_table *t = p->sysctl; 39771da177e4SLinus Torvalds p->sysctl = NULL; 39781da177e4SLinus Torvalds unregister_sysctl_table(t->sysctl_header); 39791da177e4SLinus Torvalds kfree(t->addrconf_dev[0].procname); 39801da177e4SLinus Torvalds kfree(t); 39811da177e4SLinus Torvalds } 39821da177e4SLinus Torvalds } 39831da177e4SLinus Torvalds 39841da177e4SLinus Torvalds 39851da177e4SLinus Torvalds #endif 39861da177e4SLinus Torvalds 39871da177e4SLinus Torvalds /* 39881da177e4SLinus Torvalds * Device notifier 39891da177e4SLinus Torvalds */ 39901da177e4SLinus Torvalds 39911da177e4SLinus Torvalds int register_inet6addr_notifier(struct notifier_block *nb) 39921da177e4SLinus Torvalds { 3993e041c683SAlan Stern return atomic_notifier_chain_register(&inet6addr_chain, nb); 39941da177e4SLinus Torvalds } 39951da177e4SLinus Torvalds 39961da177e4SLinus Torvalds int unregister_inet6addr_notifier(struct notifier_block *nb) 39971da177e4SLinus Torvalds { 3998e041c683SAlan Stern return atomic_notifier_chain_unregister(&inet6addr_chain,nb); 39991da177e4SLinus Torvalds } 40001da177e4SLinus Torvalds 40011da177e4SLinus Torvalds /* 40021da177e4SLinus Torvalds * Init / cleanup code 40031da177e4SLinus Torvalds */ 40041da177e4SLinus Torvalds 40051da177e4SLinus Torvalds int __init addrconf_init(void) 40061da177e4SLinus Torvalds { 40071da177e4SLinus Torvalds int err = 0; 40081da177e4SLinus Torvalds 40091da177e4SLinus Torvalds /* The addrconf netdev notifier requires that loopback_dev 40101da177e4SLinus Torvalds * has it's ipv6 private information allocated and setup 40111da177e4SLinus Torvalds * before it can bring up and give link-local addresses 40121da177e4SLinus Torvalds * to other devices which are up. 40131da177e4SLinus Torvalds * 40141da177e4SLinus Torvalds * Unfortunately, loopback_dev is not necessarily the first 40151da177e4SLinus Torvalds * entry in the global dev_base list of net devices. In fact, 40161da177e4SLinus Torvalds * it is likely to be the very last entry on that list. 40171da177e4SLinus Torvalds * So this causes the notifier registry below to try and 40181da177e4SLinus Torvalds * give link-local addresses to all devices besides loopback_dev 40191da177e4SLinus Torvalds * first, then loopback_dev, which cases all the non-loopback_dev 40201da177e4SLinus Torvalds * devices to fail to get a link-local address. 40211da177e4SLinus Torvalds * 40221da177e4SLinus Torvalds * So, as a temporary fix, allocate the ipv6 structure for 40231da177e4SLinus Torvalds * loopback_dev first by hand. 40241da177e4SLinus Torvalds * Longer term, all of the dependencies ipv6 has upon the loopback 40251da177e4SLinus Torvalds * device and it being up should be removed. 40261da177e4SLinus Torvalds */ 40271da177e4SLinus Torvalds rtnl_lock(); 40281da177e4SLinus Torvalds if (!ipv6_add_dev(&loopback_dev)) 40291da177e4SLinus Torvalds err = -ENOMEM; 40301da177e4SLinus Torvalds rtnl_unlock(); 40311da177e4SLinus Torvalds if (err) 40321da177e4SLinus Torvalds return err; 40331da177e4SLinus Torvalds 4034c62dba90SHerbert Xu ip6_null_entry.rt6i_idev = in6_dev_get(&loopback_dev); 4035c62dba90SHerbert Xu 40361da177e4SLinus Torvalds register_netdevice_notifier(&ipv6_dev_notf); 40371da177e4SLinus Torvalds 40381da177e4SLinus Torvalds addrconf_verify(0); 40391da177e4SLinus Torvalds rtnetlink_links[PF_INET6] = inet6_rtnetlink_table; 40401da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 40411da177e4SLinus Torvalds addrconf_sysctl.sysctl_header = 40421da177e4SLinus Torvalds register_sysctl_table(addrconf_sysctl.addrconf_root_dir, 0); 40431da177e4SLinus Torvalds addrconf_sysctl_register(NULL, &ipv6_devconf_dflt); 40441da177e4SLinus Torvalds #endif 40451da177e4SLinus Torvalds 40461da177e4SLinus Torvalds return 0; 40471da177e4SLinus Torvalds } 40481da177e4SLinus Torvalds 40491da177e4SLinus Torvalds void __exit addrconf_cleanup(void) 40501da177e4SLinus Torvalds { 40511da177e4SLinus Torvalds struct net_device *dev; 40521da177e4SLinus Torvalds struct inet6_dev *idev; 40531da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 40541da177e4SLinus Torvalds int i; 40551da177e4SLinus Torvalds 40561da177e4SLinus Torvalds unregister_netdevice_notifier(&ipv6_dev_notf); 40571da177e4SLinus Torvalds 40581da177e4SLinus Torvalds rtnetlink_links[PF_INET6] = NULL; 40591da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 40601da177e4SLinus Torvalds addrconf_sysctl_unregister(&ipv6_devconf_dflt); 40611da177e4SLinus Torvalds addrconf_sysctl_unregister(&ipv6_devconf); 40621da177e4SLinus Torvalds #endif 40631da177e4SLinus Torvalds 40641da177e4SLinus Torvalds rtnl_lock(); 40651da177e4SLinus Torvalds 40661da177e4SLinus Torvalds /* 40671da177e4SLinus Torvalds * clean dev list. 40681da177e4SLinus Torvalds */ 40691da177e4SLinus Torvalds 40701da177e4SLinus Torvalds for (dev=dev_base; dev; dev=dev->next) { 40711da177e4SLinus Torvalds if ((idev = __in6_dev_get(dev)) == NULL) 40721da177e4SLinus Torvalds continue; 40731da177e4SLinus Torvalds addrconf_ifdown(dev, 1); 40741da177e4SLinus Torvalds } 40751da177e4SLinus Torvalds addrconf_ifdown(&loopback_dev, 2); 40761da177e4SLinus Torvalds 40771da177e4SLinus Torvalds /* 40781da177e4SLinus Torvalds * Check hash table. 40791da177e4SLinus Torvalds */ 40801da177e4SLinus Torvalds 40811da177e4SLinus Torvalds write_lock_bh(&addrconf_hash_lock); 40821da177e4SLinus Torvalds for (i=0; i < IN6_ADDR_HSIZE; i++) { 40831da177e4SLinus Torvalds for (ifa=inet6_addr_lst[i]; ifa; ) { 40841da177e4SLinus Torvalds struct inet6_ifaddr *bifa; 40851da177e4SLinus Torvalds 40861da177e4SLinus Torvalds bifa = ifa; 40871da177e4SLinus Torvalds ifa = ifa->lst_next; 40881da177e4SLinus Torvalds printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa); 40891da177e4SLinus Torvalds /* Do not free it; something is wrong. 40901da177e4SLinus Torvalds Now we can investigate it with debugger. 40911da177e4SLinus Torvalds */ 40921da177e4SLinus Torvalds } 40931da177e4SLinus Torvalds } 40941da177e4SLinus Torvalds write_unlock_bh(&addrconf_hash_lock); 40951da177e4SLinus Torvalds 40961da177e4SLinus Torvalds del_timer(&addr_chk_timer); 40971da177e4SLinus Torvalds 40981da177e4SLinus Torvalds rtnl_unlock(); 40991da177e4SLinus Torvalds 41001da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 41011da177e4SLinus Torvalds proc_net_remove("if_inet6"); 41021da177e4SLinus Torvalds #endif 41031da177e4SLinus Torvalds } 4104