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/net.h> 481da177e4SLinus Torvalds #include <linux/in6.h> 491da177e4SLinus Torvalds #include <linux/netdevice.h> 501823730fSThomas Graf #include <linux/if_addr.h> 511da177e4SLinus Torvalds #include <linux/if_arp.h> 521da177e4SLinus Torvalds #include <linux/if_arcnet.h> 531da177e4SLinus Torvalds #include <linux/if_infiniband.h> 541da177e4SLinus Torvalds #include <linux/route.h> 551da177e4SLinus Torvalds #include <linux/inetdevice.h> 561da177e4SLinus Torvalds #include <linux/init.h> 571da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 581da177e4SLinus Torvalds #include <linux/sysctl.h> 591da177e4SLinus Torvalds #endif 604fc268d2SRandy Dunlap #include <linux/capability.h> 611da177e4SLinus Torvalds #include <linux/delay.h> 621da177e4SLinus Torvalds #include <linux/notifier.h> 63543537bdSPaulo Marques #include <linux/string.h> 641da177e4SLinus Torvalds 65457c4cbcSEric W. Biederman #include <net/net_namespace.h> 661da177e4SLinus Torvalds #include <net/sock.h> 671da177e4SLinus Torvalds #include <net/snmp.h> 681da177e4SLinus Torvalds 691da177e4SLinus Torvalds #include <net/ipv6.h> 701da177e4SLinus Torvalds #include <net/protocol.h> 711da177e4SLinus Torvalds #include <net/ndisc.h> 721da177e4SLinus Torvalds #include <net/ip6_route.h> 731da177e4SLinus Torvalds #include <net/addrconf.h> 741da177e4SLinus Torvalds #include <net/tcp.h> 751da177e4SLinus Torvalds #include <net/ip.h> 765d620266SThomas Graf #include <net/netlink.h> 77f24e3d65SMitsuru Chinen #include <net/pkt_sched.h> 781da177e4SLinus Torvalds #include <linux/if_tunnel.h> 791da177e4SLinus Torvalds #include <linux/rtnetlink.h> 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 821da177e4SLinus Torvalds #include <linux/random.h> 831da177e4SLinus Torvalds #endif 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds #include <asm/uaccess.h> 867f7d9a6bSHerbert Xu #include <asm/unaligned.h> 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds #include <linux/proc_fs.h> 891da177e4SLinus Torvalds #include <linux/seq_file.h> 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds /* Set to 3 to get tracing... */ 921da177e4SLinus Torvalds #define ACONF_DEBUG 2 931da177e4SLinus Torvalds 941da177e4SLinus Torvalds #if ACONF_DEBUG >= 3 951da177e4SLinus Torvalds #define ADBG(x) printk x 961da177e4SLinus Torvalds #else 971da177e4SLinus Torvalds #define ADBG(x) 981da177e4SLinus Torvalds #endif 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds #define INFINITY_LIFE_TIME 0xFFFFFFFF 1011da177e4SLinus Torvalds #define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b))) 1021da177e4SLinus Torvalds 1031da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 104f52295a9SPavel Emelyanov static void addrconf_sysctl_register(struct inet6_dev *idev); 105408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev); 106408c4768SPavel Emelyanov #else 107408c4768SPavel Emelyanov static inline void addrconf_sysctl_register(struct inet6_dev *idev) 108408c4768SPavel Emelyanov { 109408c4768SPavel Emelyanov } 110408c4768SPavel Emelyanov 111408c4768SPavel Emelyanov static inline void addrconf_sysctl_unregister(struct inet6_dev *idev) 112408c4768SPavel Emelyanov { 113408c4768SPavel Emelyanov } 1141da177e4SLinus Torvalds #endif 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 1171da177e4SLinus Torvalds static int __ipv6_regen_rndid(struct inet6_dev *idev); 1181da177e4SLinus Torvalds static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr); 1191da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data); 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds static int desync_factor = MAX_DESYNC_FACTOR * HZ; 1221da177e4SLinus Torvalds #endif 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev); 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds /* 1271da177e4SLinus Torvalds * Configured unicast address hash table 1281da177e4SLinus Torvalds */ 1291da177e4SLinus Torvalds static struct inet6_ifaddr *inet6_addr_lst[IN6_ADDR_HSIZE]; 1301da177e4SLinus Torvalds static DEFINE_RWLOCK(addrconf_hash_lock); 1311da177e4SLinus Torvalds 1321da177e4SLinus Torvalds static void addrconf_verify(unsigned long); 1331da177e4SLinus Torvalds 1348d06afabSIngo Molnar static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0); 1351da177e4SLinus Torvalds static DEFINE_SPINLOCK(addrconf_verify_lock); 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds static void addrconf_join_anycast(struct inet6_ifaddr *ifp); 1381da177e4SLinus Torvalds static void addrconf_leave_anycast(struct inet6_ifaddr *ifp); 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how); 1411da177e4SLinus Torvalds 142e431b8c0SJamal Hadi Salim static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags); 1431da177e4SLinus Torvalds static void addrconf_dad_timer(unsigned long data); 1441da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp); 145c5e33bddSYOSHIFUJI Hideaki static void addrconf_dad_run(struct inet6_dev *idev); 1461da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data); 1471da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); 1481da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); 1491da177e4SLinus Torvalds 1501da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev, 1511da177e4SLinus Torvalds struct prefix_info *pinfo); 15206bfe655SDaniel Lezcano static int ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr, 15306bfe655SDaniel Lezcano struct net_device *dev); 1541da177e4SLinus Torvalds 155e041c683SAlan Stern static ATOMIC_NOTIFIER_HEAD(inet6addr_chain); 1561da177e4SLinus Torvalds 157ab32ea5dSBrian Haley struct ipv6_devconf ipv6_devconf __read_mostly = { 1581da177e4SLinus Torvalds .forwarding = 0, 1591da177e4SLinus Torvalds .hop_limit = IPV6_DEFAULT_HOPLIMIT, 1601da177e4SLinus Torvalds .mtu6 = IPV6_MIN_MTU, 1611da177e4SLinus Torvalds .accept_ra = 1, 1621da177e4SLinus Torvalds .accept_redirects = 1, 1631da177e4SLinus Torvalds .autoconf = 1, 1641da177e4SLinus Torvalds .force_mld_version = 0, 1651da177e4SLinus Torvalds .dad_transmits = 1, 1661da177e4SLinus Torvalds .rtr_solicits = MAX_RTR_SOLICITATIONS, 1671da177e4SLinus Torvalds .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, 1681da177e4SLinus Torvalds .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, 1691da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 1701da177e4SLinus Torvalds .use_tempaddr = 0, 1711da177e4SLinus Torvalds .temp_valid_lft = TEMP_VALID_LIFETIME, 1721da177e4SLinus Torvalds .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, 1731da177e4SLinus Torvalds .regen_max_retry = REGEN_MAX_RETRY, 1741da177e4SLinus Torvalds .max_desync_factor = MAX_DESYNC_FACTOR, 1751da177e4SLinus Torvalds #endif 1761da177e4SLinus Torvalds .max_addresses = IPV6_MAX_ADDRESSES, 17765f5c7c1SYOSHIFUJI Hideaki .accept_ra_defrtr = 1, 178c4fd30ebSYOSHIFUJI Hideaki .accept_ra_pinfo = 1, 179930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 180930d6ff2SYOSHIFUJI Hideaki .accept_ra_rtr_pref = 1, 18152e16356SYOSHIFUJI Hideaki .rtr_probe_interval = 60 * HZ, 18209c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 18309c884d4SYOSHIFUJI Hideaki .accept_ra_rt_info_max_plen = 0, 18409c884d4SYOSHIFUJI Hideaki #endif 185930d6ff2SYOSHIFUJI Hideaki #endif 186fbea49e1SYOSHIFUJI Hideaki .proxy_ndp = 0, 1870bcbc926SYOSHIFUJI Hideaki .accept_source_route = 0, /* we do not accept RH0 by default. */ 1881da177e4SLinus Torvalds }; 1891da177e4SLinus Torvalds 190ab32ea5dSBrian Haley static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = { 1911da177e4SLinus Torvalds .forwarding = 0, 1921da177e4SLinus Torvalds .hop_limit = IPV6_DEFAULT_HOPLIMIT, 1931da177e4SLinus Torvalds .mtu6 = IPV6_MIN_MTU, 1941da177e4SLinus Torvalds .accept_ra = 1, 1951da177e4SLinus Torvalds .accept_redirects = 1, 1961da177e4SLinus Torvalds .autoconf = 1, 1971da177e4SLinus Torvalds .dad_transmits = 1, 1981da177e4SLinus Torvalds .rtr_solicits = MAX_RTR_SOLICITATIONS, 1991da177e4SLinus Torvalds .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, 2001da177e4SLinus Torvalds .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, 2011da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 2021da177e4SLinus Torvalds .use_tempaddr = 0, 2031da177e4SLinus Torvalds .temp_valid_lft = TEMP_VALID_LIFETIME, 2041da177e4SLinus Torvalds .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, 2051da177e4SLinus Torvalds .regen_max_retry = REGEN_MAX_RETRY, 2061da177e4SLinus Torvalds .max_desync_factor = MAX_DESYNC_FACTOR, 2071da177e4SLinus Torvalds #endif 2081da177e4SLinus Torvalds .max_addresses = IPV6_MAX_ADDRESSES, 20965f5c7c1SYOSHIFUJI Hideaki .accept_ra_defrtr = 1, 210c4fd30ebSYOSHIFUJI Hideaki .accept_ra_pinfo = 1, 211930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 212930d6ff2SYOSHIFUJI Hideaki .accept_ra_rtr_pref = 1, 21352e16356SYOSHIFUJI Hideaki .rtr_probe_interval = 60 * HZ, 21409c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 21509c884d4SYOSHIFUJI Hideaki .accept_ra_rt_info_max_plen = 0, 21609c884d4SYOSHIFUJI Hideaki #endif 217930d6ff2SYOSHIFUJI Hideaki #endif 218fbea49e1SYOSHIFUJI Hideaki .proxy_ndp = 0, 2190bcbc926SYOSHIFUJI Hideaki .accept_source_route = 0, /* we do not accept RH0 by default. */ 2201da177e4SLinus Torvalds }; 2211da177e4SLinus Torvalds 2221da177e4SLinus Torvalds /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */ 2231da177e4SLinus Torvalds const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; 2241da177e4SLinus Torvalds const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT; 225f3ee4010SYOSHIFUJI Hideaki const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT; 226f3ee4010SYOSHIFUJI Hideaki const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT; 2271da177e4SLinus Torvalds 228f24e3d65SMitsuru Chinen /* Check if a valid qdisc is available */ 229f24e3d65SMitsuru Chinen static inline int addrconf_qdisc_ok(struct net_device *dev) 230f24e3d65SMitsuru Chinen { 231f24e3d65SMitsuru Chinen return (dev->qdisc != &noop_qdisc); 232f24e3d65SMitsuru Chinen } 233f24e3d65SMitsuru Chinen 2341da177e4SLinus Torvalds static void addrconf_del_timer(struct inet6_ifaddr *ifp) 2351da177e4SLinus Torvalds { 2361da177e4SLinus Torvalds if (del_timer(&ifp->timer)) 2371da177e4SLinus Torvalds __in6_ifa_put(ifp); 2381da177e4SLinus Torvalds } 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds enum addrconf_timer_t 2411da177e4SLinus Torvalds { 2421da177e4SLinus Torvalds AC_NONE, 2431da177e4SLinus Torvalds AC_DAD, 2441da177e4SLinus Torvalds AC_RS, 2451da177e4SLinus Torvalds }; 2461da177e4SLinus Torvalds 2471da177e4SLinus Torvalds static void addrconf_mod_timer(struct inet6_ifaddr *ifp, 2481da177e4SLinus Torvalds enum addrconf_timer_t what, 2491da177e4SLinus Torvalds unsigned long when) 2501da177e4SLinus Torvalds { 2511da177e4SLinus Torvalds if (!del_timer(&ifp->timer)) 2521da177e4SLinus Torvalds in6_ifa_hold(ifp); 2531da177e4SLinus Torvalds 2541da177e4SLinus Torvalds switch (what) { 2551da177e4SLinus Torvalds case AC_DAD: 2561da177e4SLinus Torvalds ifp->timer.function = addrconf_dad_timer; 2571da177e4SLinus Torvalds break; 2581da177e4SLinus Torvalds case AC_RS: 2591da177e4SLinus Torvalds ifp->timer.function = addrconf_rs_timer; 2601da177e4SLinus Torvalds break; 2611da177e4SLinus Torvalds default:; 2621da177e4SLinus Torvalds } 2631da177e4SLinus Torvalds ifp->timer.expires = jiffies + when; 2641da177e4SLinus Torvalds add_timer(&ifp->timer); 2651da177e4SLinus Torvalds } 2661da177e4SLinus Torvalds 2677f7d9a6bSHerbert Xu static int snmp6_alloc_dev(struct inet6_dev *idev) 2687f7d9a6bSHerbert Xu { 2697f7d9a6bSHerbert Xu if (snmp_mib_init((void **)idev->stats.ipv6, 270c69bce20SYOSHIFUJI Hideaki sizeof(struct ipstats_mib)) < 0) 2717f7d9a6bSHerbert Xu goto err_ip; 2727f7d9a6bSHerbert Xu if (snmp_mib_init((void **)idev->stats.icmpv6, 273c69bce20SYOSHIFUJI Hideaki sizeof(struct icmpv6_mib)) < 0) 2747f7d9a6bSHerbert Xu goto err_icmp; 27514878f75SDavid L Stevens if (snmp_mib_init((void **)idev->stats.icmpv6msg, 276c69bce20SYOSHIFUJI Hideaki sizeof(struct icmpv6msg_mib)) < 0) 27714878f75SDavid L Stevens goto err_icmpmsg; 2787f7d9a6bSHerbert Xu 2797f7d9a6bSHerbert Xu return 0; 2807f7d9a6bSHerbert Xu 28114878f75SDavid L Stevens err_icmpmsg: 28214878f75SDavid L Stevens snmp_mib_free((void **)idev->stats.icmpv6); 2837f7d9a6bSHerbert Xu err_icmp: 2847f7d9a6bSHerbert Xu snmp_mib_free((void **)idev->stats.ipv6); 2857f7d9a6bSHerbert Xu err_ip: 286aaf70ec7SPavel Emelyanov return -ENOMEM; 2877f7d9a6bSHerbert Xu } 2887f7d9a6bSHerbert Xu 28916910b98SPavel Emelyanov static void snmp6_free_dev(struct inet6_dev *idev) 2907f7d9a6bSHerbert Xu { 29114878f75SDavid L Stevens snmp_mib_free((void **)idev->stats.icmpv6msg); 2927f7d9a6bSHerbert Xu snmp_mib_free((void **)idev->stats.icmpv6); 2937f7d9a6bSHerbert Xu snmp_mib_free((void **)idev->stats.ipv6); 2947f7d9a6bSHerbert Xu } 2957f7d9a6bSHerbert Xu 2961da177e4SLinus Torvalds /* Nobody refers to this device, we may destroy it. */ 2971da177e4SLinus Torvalds 2988814c4b5SYOSHIFUJI Hideaki static void in6_dev_finish_destroy_rcu(struct rcu_head *head) 2998814c4b5SYOSHIFUJI Hideaki { 3008814c4b5SYOSHIFUJI Hideaki struct inet6_dev *idev = container_of(head, struct inet6_dev, rcu); 3018814c4b5SYOSHIFUJI Hideaki kfree(idev); 3028814c4b5SYOSHIFUJI Hideaki } 3038814c4b5SYOSHIFUJI Hideaki 3041da177e4SLinus Torvalds void in6_dev_finish_destroy(struct inet6_dev *idev) 3051da177e4SLinus Torvalds { 3061da177e4SLinus Torvalds struct net_device *dev = idev->dev; 3071da177e4SLinus Torvalds BUG_TRAP(idev->addr_list==NULL); 3081da177e4SLinus Torvalds BUG_TRAP(idev->mc_list==NULL); 3091da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG 3101da177e4SLinus Torvalds printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL"); 3111da177e4SLinus Torvalds #endif 3121da177e4SLinus Torvalds dev_put(dev); 3131da177e4SLinus Torvalds if (!idev->dead) { 3141da177e4SLinus Torvalds printk("Freeing alive inet6 device %p\n", idev); 3151da177e4SLinus Torvalds return; 3161da177e4SLinus Torvalds } 3171da177e4SLinus Torvalds snmp6_free_dev(idev); 3188814c4b5SYOSHIFUJI Hideaki call_rcu(&idev->rcu, in6_dev_finish_destroy_rcu); 3191da177e4SLinus Torvalds } 3201da177e4SLinus Torvalds 3217159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(in6_dev_finish_destroy); 3227159039aSYOSHIFUJI Hideaki 3231da177e4SLinus Torvalds static struct inet6_dev * ipv6_add_dev(struct net_device *dev) 3241da177e4SLinus Torvalds { 3251da177e4SLinus Torvalds struct inet6_dev *ndev; 3261da177e4SLinus Torvalds 3271da177e4SLinus Torvalds ASSERT_RTNL(); 3281da177e4SLinus Torvalds 3291da177e4SLinus Torvalds if (dev->mtu < IPV6_MIN_MTU) 3301da177e4SLinus Torvalds return NULL; 3311da177e4SLinus Torvalds 332322f74a4SIngo Oeser ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL); 3331da177e4SLinus Torvalds 334322f74a4SIngo Oeser if (ndev == NULL) 335322f74a4SIngo Oeser return NULL; 3361da177e4SLinus Torvalds 3371da177e4SLinus Torvalds rwlock_init(&ndev->lock); 3381da177e4SLinus Torvalds ndev->dev = dev; 339c346dca1SYOSHIFUJI Hideaki memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf)); 3401da177e4SLinus Torvalds ndev->cnf.mtu6 = dev->mtu; 3411da177e4SLinus Torvalds ndev->cnf.sysctl = NULL; 3421da177e4SLinus Torvalds ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl); 3431da177e4SLinus Torvalds if (ndev->nd_parms == NULL) { 3441da177e4SLinus Torvalds kfree(ndev); 3451da177e4SLinus Torvalds return NULL; 3461da177e4SLinus Torvalds } 3471da177e4SLinus Torvalds /* We refer to the device */ 3481da177e4SLinus Torvalds dev_hold(dev); 3491da177e4SLinus Torvalds 3501da177e4SLinus Torvalds if (snmp6_alloc_dev(ndev) < 0) { 3511da177e4SLinus Torvalds ADBG((KERN_WARNING 3521da177e4SLinus Torvalds "%s(): cannot allocate memory for statistics; dev=%s.\n", 3530dc47877SHarvey Harrison __func__, dev->name)); 3541da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, ndev->nd_parms); 3551da177e4SLinus Torvalds ndev->dead = 1; 3561da177e4SLinus Torvalds in6_dev_finish_destroy(ndev); 3571da177e4SLinus Torvalds return NULL; 3581da177e4SLinus Torvalds } 3591da177e4SLinus Torvalds 3601da177e4SLinus Torvalds if (snmp6_register_dev(ndev) < 0) { 3611da177e4SLinus Torvalds ADBG((KERN_WARNING 3621da177e4SLinus Torvalds "%s(): cannot create /proc/net/dev_snmp6/%s\n", 3630dc47877SHarvey Harrison __func__, dev->name)); 3641da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, ndev->nd_parms); 3651da177e4SLinus Torvalds ndev->dead = 1; 3661da177e4SLinus Torvalds in6_dev_finish_destroy(ndev); 3671da177e4SLinus Torvalds return NULL; 3681da177e4SLinus Torvalds } 3691da177e4SLinus Torvalds 3701da177e4SLinus Torvalds /* One reference from device. We must do this before 3711da177e4SLinus Torvalds * we invoke __ipv6_regen_rndid(). 3721da177e4SLinus Torvalds */ 3731da177e4SLinus Torvalds in6_dev_hold(ndev); 3741da177e4SLinus Torvalds 3750be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 376c7dc89c0SFred L. Templin if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) { 377c7dc89c0SFred L. Templin printk(KERN_INFO 378c7dc89c0SFred L. Templin "%s: Disabled Multicast RS\n", 379c7dc89c0SFred L. Templin dev->name); 380c7dc89c0SFred L. Templin ndev->cnf.rtr_solicits = 0; 381c7dc89c0SFred L. Templin } 382b077d7abSYOSHIFUJI Hideaki #endif 383b077d7abSYOSHIFUJI Hideaki 3841da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 3851da177e4SLinus Torvalds setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev); 3861da177e4SLinus Torvalds if ((dev->flags&IFF_LOOPBACK) || 3871da177e4SLinus Torvalds dev->type == ARPHRD_TUNNEL || 3889625ed72SYOSHIFUJI Hideaki dev->type == ARPHRD_TUNNEL6 || 3891da177e4SLinus Torvalds dev->type == ARPHRD_SIT || 3901da177e4SLinus Torvalds dev->type == ARPHRD_NONE) { 3911da177e4SLinus Torvalds printk(KERN_INFO 3921da177e4SLinus Torvalds "%s: Disabled Privacy Extensions\n", 3931da177e4SLinus Torvalds dev->name); 3941da177e4SLinus Torvalds ndev->cnf.use_tempaddr = -1; 3951da177e4SLinus Torvalds } else { 3961da177e4SLinus Torvalds in6_dev_hold(ndev); 3971da177e4SLinus Torvalds ipv6_regen_rndid((unsigned long) ndev); 3981da177e4SLinus Torvalds } 3991da177e4SLinus Torvalds #endif 4001da177e4SLinus Torvalds 401f24e3d65SMitsuru Chinen if (netif_running(dev) && addrconf_qdisc_ok(dev)) 40253aadcc9SHerbert Xu ndev->if_flags |= IF_READY; 40353aadcc9SHerbert Xu 4041da177e4SLinus Torvalds ipv6_mc_init_dev(ndev); 4051da177e4SLinus Torvalds ndev->tstamp = jiffies; 406f52295a9SPavel Emelyanov addrconf_sysctl_register(ndev); 40730c4cf57SDavid L Stevens /* protected by rtnl_lock */ 40830c4cf57SDavid L Stevens rcu_assign_pointer(dev->ip6_ptr, ndev); 409d88ae4ccSYOSHIFUJI Hideaki 410d88ae4ccSYOSHIFUJI Hideaki /* Join all-node multicast group */ 411f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes); 412d88ae4ccSYOSHIFUJI Hideaki 4131da177e4SLinus Torvalds return ndev; 4141da177e4SLinus Torvalds } 4151da177e4SLinus Torvalds 416cee89473SYOSHIFUJI Hideaki static struct inet6_dev * ipv6_find_idev(struct net_device *dev) 4171da177e4SLinus Torvalds { 4181da177e4SLinus Torvalds struct inet6_dev *idev; 4191da177e4SLinus Torvalds 4201da177e4SLinus Torvalds ASSERT_RTNL(); 4211da177e4SLinus Torvalds 4221da177e4SLinus Torvalds if ((idev = __in6_dev_get(dev)) == NULL) { 4231da177e4SLinus Torvalds if ((idev = ipv6_add_dev(dev)) == NULL) 4241da177e4SLinus Torvalds return NULL; 4251da177e4SLinus Torvalds } 426c5e33bddSYOSHIFUJI Hideaki 4271da177e4SLinus Torvalds if (dev->flags&IFF_UP) 4281da177e4SLinus Torvalds ipv6_mc_up(idev); 4291da177e4SLinus Torvalds return idev; 4301da177e4SLinus Torvalds } 4311da177e4SLinus Torvalds 4321da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 4331da177e4SLinus Torvalds static void dev_forward_change(struct inet6_dev *idev) 4341da177e4SLinus Torvalds { 4351da177e4SLinus Torvalds struct net_device *dev; 4361da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 4371da177e4SLinus Torvalds 4381da177e4SLinus Torvalds if (!idev) 4391da177e4SLinus Torvalds return; 4401da177e4SLinus Torvalds dev = idev->dev; 4411da177e4SLinus Torvalds if (dev && (dev->flags & IFF_MULTICAST)) { 4421da177e4SLinus Torvalds if (idev->cnf.forwarding) 443f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters); 4441da177e4SLinus Torvalds else 445f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters); 4461da177e4SLinus Torvalds } 4471da177e4SLinus Torvalds for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) { 4482c12a74cSMichal Wrobel if (ifa->flags&IFA_F_TENTATIVE) 4492c12a74cSMichal Wrobel continue; 4501da177e4SLinus Torvalds if (idev->cnf.forwarding) 4511da177e4SLinus Torvalds addrconf_join_anycast(ifa); 4521da177e4SLinus Torvalds else 4531da177e4SLinus Torvalds addrconf_leave_anycast(ifa); 4541da177e4SLinus Torvalds } 4551da177e4SLinus Torvalds } 4561da177e4SLinus Torvalds 4571da177e4SLinus Torvalds 458e186932bSPavel Emelyanov static void addrconf_forward_change(struct net *net, __s32 newf) 4591da177e4SLinus Torvalds { 4601da177e4SLinus Torvalds struct net_device *dev; 4611da177e4SLinus Torvalds struct inet6_dev *idev; 4621da177e4SLinus Torvalds 4631da177e4SLinus Torvalds read_lock(&dev_base_lock); 464bff16c2fSPavel Emelyanov for_each_netdev(net, dev) { 4658814c4b5SYOSHIFUJI Hideaki rcu_read_lock(); 4661da177e4SLinus Torvalds idev = __in6_dev_get(dev); 4671da177e4SLinus Torvalds if (idev) { 468e186932bSPavel Emelyanov int changed = (!idev->cnf.forwarding) ^ (!newf); 469e186932bSPavel Emelyanov idev->cnf.forwarding = newf; 4701da177e4SLinus Torvalds if (changed) 4711da177e4SLinus Torvalds dev_forward_change(idev); 4721da177e4SLinus Torvalds } 4738814c4b5SYOSHIFUJI Hideaki rcu_read_unlock(); 4741da177e4SLinus Torvalds } 4751da177e4SLinus Torvalds read_unlock(&dev_base_lock); 4761da177e4SLinus Torvalds } 477c8fecf22SPavel Emelyanov 478c8fecf22SPavel Emelyanov static void addrconf_fixup_forwarding(struct ctl_table *table, int *p, int old) 479c8fecf22SPavel Emelyanov { 480bff16c2fSPavel Emelyanov struct net *net; 481bff16c2fSPavel Emelyanov 482bff16c2fSPavel Emelyanov net = (struct net *)table->extra2; 483441fc2a2SPavel Emelyanov if (p == &net->ipv6.devconf_dflt->forwarding) 484c8fecf22SPavel Emelyanov return; 485c8fecf22SPavel Emelyanov 486e186932bSPavel Emelyanov if (p == &net->ipv6.devconf_all->forwarding) { 487e186932bSPavel Emelyanov __s32 newf = net->ipv6.devconf_all->forwarding; 488e186932bSPavel Emelyanov net->ipv6.devconf_dflt->forwarding = newf; 489e186932bSPavel Emelyanov addrconf_forward_change(net, newf); 490c8fecf22SPavel Emelyanov } else if ((!*p) ^ (!old)) 491c8fecf22SPavel Emelyanov dev_forward_change((struct inet6_dev *)table->extra1); 492c8fecf22SPavel Emelyanov 493c8fecf22SPavel Emelyanov if (*p) 4947b4da532SDaniel Lezcano rt6_purge_dflt_routers(net); 495c8fecf22SPavel Emelyanov } 4961da177e4SLinus Torvalds #endif 4971da177e4SLinus Torvalds 4981da177e4SLinus Torvalds /* Nobody refers to this ifaddr, destroy it */ 4991da177e4SLinus Torvalds 5001da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp) 5011da177e4SLinus Torvalds { 5021da177e4SLinus Torvalds BUG_TRAP(ifp->if_next==NULL); 5031da177e4SLinus Torvalds BUG_TRAP(ifp->lst_next==NULL); 5041da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG 5051da177e4SLinus Torvalds printk(KERN_DEBUG "inet6_ifa_finish_destroy\n"); 5061da177e4SLinus Torvalds #endif 5071da177e4SLinus Torvalds 5081da177e4SLinus Torvalds in6_dev_put(ifp->idev); 5091da177e4SLinus Torvalds 5101da177e4SLinus Torvalds if (del_timer(&ifp->timer)) 5111da177e4SLinus Torvalds printk("Timer is still running, when freeing ifa=%p\n", ifp); 5121da177e4SLinus Torvalds 5131da177e4SLinus Torvalds if (!ifp->dead) { 5141da177e4SLinus Torvalds printk("Freeing alive inet6 address %p\n", ifp); 5151da177e4SLinus Torvalds return; 5161da177e4SLinus Torvalds } 5171da177e4SLinus Torvalds dst_release(&ifp->rt->u.dst); 5181da177e4SLinus Torvalds 5191da177e4SLinus Torvalds kfree(ifp); 5201da177e4SLinus Torvalds } 5211da177e4SLinus Torvalds 522e55ffac6SBrian Haley static void 523e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp) 524e55ffac6SBrian Haley { 525e55ffac6SBrian Haley struct inet6_ifaddr *ifa, **ifap; 5268a6ce0c0SYOSHIFUJI Hideaki int ifp_scope = ipv6_addr_src_scope(&ifp->addr); 527e55ffac6SBrian Haley 528e55ffac6SBrian Haley /* 529e55ffac6SBrian Haley * Each device address list is sorted in order of scope - 530e55ffac6SBrian Haley * global before linklocal. 531e55ffac6SBrian Haley */ 532e55ffac6SBrian Haley for (ifap = &idev->addr_list; (ifa = *ifap) != NULL; 533e55ffac6SBrian Haley ifap = &ifa->if_next) { 5348a6ce0c0SYOSHIFUJI Hideaki if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr)) 535e55ffac6SBrian Haley break; 536e55ffac6SBrian Haley } 537e55ffac6SBrian Haley 538e55ffac6SBrian Haley ifp->if_next = *ifap; 539e55ffac6SBrian Haley *ifap = ifp; 540e55ffac6SBrian Haley } 541e55ffac6SBrian Haley 5423eb84f49SYOSHIFUJI Hideaki /* 5433eb84f49SYOSHIFUJI Hideaki * Hash function taken from net_alias.c 5443eb84f49SYOSHIFUJI Hideaki */ 5453eb84f49SYOSHIFUJI Hideaki static u8 ipv6_addr_hash(const struct in6_addr *addr) 5463eb84f49SYOSHIFUJI Hideaki { 5473eb84f49SYOSHIFUJI Hideaki __u32 word; 5483eb84f49SYOSHIFUJI Hideaki 5493eb84f49SYOSHIFUJI Hideaki /* 5503eb84f49SYOSHIFUJI Hideaki * We perform the hash function over the last 64 bits of the address 5513eb84f49SYOSHIFUJI Hideaki * This will include the IEEE address token on links that support it. 5523eb84f49SYOSHIFUJI Hideaki */ 5533eb84f49SYOSHIFUJI Hideaki 5543eb84f49SYOSHIFUJI Hideaki word = (__force u32)(addr->s6_addr32[2] ^ addr->s6_addr32[3]); 5553eb84f49SYOSHIFUJI Hideaki word ^= (word >> 16); 5563eb84f49SYOSHIFUJI Hideaki word ^= (word >> 8); 5573eb84f49SYOSHIFUJI Hideaki 5583eb84f49SYOSHIFUJI Hideaki return ((word ^ (word >> 4)) & 0x0f); 5593eb84f49SYOSHIFUJI Hideaki } 5603eb84f49SYOSHIFUJI Hideaki 5611da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */ 5621da177e4SLinus Torvalds 5631da177e4SLinus Torvalds static struct inet6_ifaddr * 5641da177e4SLinus Torvalds ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen, 565e431b8c0SJamal Hadi Salim int scope, u32 flags) 5661da177e4SLinus Torvalds { 5671da177e4SLinus Torvalds struct inet6_ifaddr *ifa = NULL; 5681da177e4SLinus Torvalds struct rt6_info *rt; 5691da177e4SLinus Torvalds int hash; 5701da177e4SLinus Torvalds int err = 0; 5711da177e4SLinus Torvalds 5728814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 5731da177e4SLinus Torvalds if (idev->dead) { 5741da177e4SLinus Torvalds err = -ENODEV; /*XXX*/ 5751da177e4SLinus Torvalds goto out2; 5761da177e4SLinus Torvalds } 5771da177e4SLinus Torvalds 5781da177e4SLinus Torvalds write_lock(&addrconf_hash_lock); 5791da177e4SLinus Torvalds 5801da177e4SLinus Torvalds /* Ignore adding duplicate addresses on an interface */ 581c346dca1SYOSHIFUJI Hideaki if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) { 5821da177e4SLinus Torvalds ADBG(("ipv6_add_addr: already assigned\n")); 5831da177e4SLinus Torvalds err = -EEXIST; 5841da177e4SLinus Torvalds goto out; 5851da177e4SLinus Torvalds } 5861da177e4SLinus Torvalds 587322f74a4SIngo Oeser ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC); 5881da177e4SLinus Torvalds 5891da177e4SLinus Torvalds if (ifa == NULL) { 5901da177e4SLinus Torvalds ADBG(("ipv6_add_addr: malloc failed\n")); 5911da177e4SLinus Torvalds err = -ENOBUFS; 5921da177e4SLinus Torvalds goto out; 5931da177e4SLinus Torvalds } 5941da177e4SLinus Torvalds 5951da177e4SLinus Torvalds rt = addrconf_dst_alloc(idev, addr, 0); 5961da177e4SLinus Torvalds if (IS_ERR(rt)) { 5971da177e4SLinus Torvalds err = PTR_ERR(rt); 5981da177e4SLinus Torvalds goto out; 5991da177e4SLinus Torvalds } 6001da177e4SLinus Torvalds 6011da177e4SLinus Torvalds ipv6_addr_copy(&ifa->addr, addr); 6021da177e4SLinus Torvalds 6031da177e4SLinus Torvalds spin_lock_init(&ifa->lock); 6041da177e4SLinus Torvalds init_timer(&ifa->timer); 6051da177e4SLinus Torvalds ifa->timer.data = (unsigned long) ifa; 6061da177e4SLinus Torvalds ifa->scope = scope; 6071da177e4SLinus Torvalds ifa->prefix_len = pfxlen; 6081da177e4SLinus Torvalds ifa->flags = flags | IFA_F_TENTATIVE; 6091da177e4SLinus Torvalds ifa->cstamp = ifa->tstamp = jiffies; 6101da177e4SLinus Torvalds 61157f5f544SKeir Fraser ifa->rt = rt; 61257f5f544SKeir Fraser 61395c385b4SNeil Horman /* 61495c385b4SNeil Horman * part one of RFC 4429, section 3.3 61595c385b4SNeil Horman * We should not configure an address as 61695c385b4SNeil Horman * optimistic if we do not yet know the link 61795c385b4SNeil Horman * layer address of our nexhop router 61895c385b4SNeil Horman */ 61995c385b4SNeil Horman 62095c385b4SNeil Horman if (rt->rt6i_nexthop == NULL) 62195c385b4SNeil Horman ifa->flags &= ~IFA_F_OPTIMISTIC; 62295c385b4SNeil Horman 6231da177e4SLinus Torvalds ifa->idev = idev; 6241da177e4SLinus Torvalds in6_dev_hold(idev); 6251da177e4SLinus Torvalds /* For caller */ 6261da177e4SLinus Torvalds in6_ifa_hold(ifa); 6271da177e4SLinus Torvalds 6281da177e4SLinus Torvalds /* Add to big hash table */ 6291da177e4SLinus Torvalds hash = ipv6_addr_hash(addr); 6301da177e4SLinus Torvalds 6311da177e4SLinus Torvalds ifa->lst_next = inet6_addr_lst[hash]; 6321da177e4SLinus Torvalds inet6_addr_lst[hash] = ifa; 6331da177e4SLinus Torvalds in6_ifa_hold(ifa); 6341da177e4SLinus Torvalds write_unlock(&addrconf_hash_lock); 6351da177e4SLinus Torvalds 6361da177e4SLinus Torvalds write_lock(&idev->lock); 6371da177e4SLinus Torvalds /* Add to inet6_dev unicast addr list. */ 638e55ffac6SBrian Haley ipv6_link_dev_addr(idev, ifa); 6391da177e4SLinus Torvalds 6401da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 6411da177e4SLinus Torvalds if (ifa->flags&IFA_F_TEMPORARY) { 6421da177e4SLinus Torvalds ifa->tmp_next = idev->tempaddr_list; 6431da177e4SLinus Torvalds idev->tempaddr_list = ifa; 6441da177e4SLinus Torvalds in6_ifa_hold(ifa); 6451da177e4SLinus Torvalds } 6461da177e4SLinus Torvalds #endif 6471da177e4SLinus Torvalds 6481da177e4SLinus Torvalds in6_ifa_hold(ifa); 6491da177e4SLinus Torvalds write_unlock(&idev->lock); 6501da177e4SLinus Torvalds out2: 6518814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 6521da177e4SLinus Torvalds 653fd92833aSYOSHIFUJI Hideaki if (likely(err == 0)) 654e041c683SAlan Stern atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa); 6551da177e4SLinus Torvalds else { 6561da177e4SLinus Torvalds kfree(ifa); 6571da177e4SLinus Torvalds ifa = ERR_PTR(err); 6581da177e4SLinus Torvalds } 6591da177e4SLinus Torvalds 6601da177e4SLinus Torvalds return ifa; 6611da177e4SLinus Torvalds out: 6621da177e4SLinus Torvalds write_unlock(&addrconf_hash_lock); 6631da177e4SLinus Torvalds goto out2; 6641da177e4SLinus Torvalds } 6651da177e4SLinus Torvalds 6661da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */ 6671da177e4SLinus Torvalds 6681da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp) 6691da177e4SLinus Torvalds { 6701da177e4SLinus Torvalds struct inet6_ifaddr *ifa, **ifap; 6711da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 6721da177e4SLinus Torvalds int hash; 6731da177e4SLinus Torvalds int deleted = 0, onlink = 0; 6741da177e4SLinus Torvalds unsigned long expires = jiffies; 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds hash = ipv6_addr_hash(&ifp->addr); 6771da177e4SLinus Torvalds 6781da177e4SLinus Torvalds ifp->dead = 1; 6791da177e4SLinus Torvalds 6801da177e4SLinus Torvalds write_lock_bh(&addrconf_hash_lock); 6811da177e4SLinus Torvalds for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL; 6821da177e4SLinus Torvalds ifap = &ifa->lst_next) { 6831da177e4SLinus Torvalds if (ifa == ifp) { 6841da177e4SLinus Torvalds *ifap = ifa->lst_next; 6851da177e4SLinus Torvalds __in6_ifa_put(ifp); 6861da177e4SLinus Torvalds ifa->lst_next = NULL; 6871da177e4SLinus Torvalds break; 6881da177e4SLinus Torvalds } 6891da177e4SLinus Torvalds } 6901da177e4SLinus Torvalds write_unlock_bh(&addrconf_hash_lock); 6911da177e4SLinus Torvalds 6921da177e4SLinus Torvalds write_lock_bh(&idev->lock); 6931da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 6941da177e4SLinus Torvalds if (ifp->flags&IFA_F_TEMPORARY) { 6951da177e4SLinus Torvalds for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL; 6961da177e4SLinus Torvalds ifap = &ifa->tmp_next) { 6971da177e4SLinus Torvalds if (ifa == ifp) { 6981da177e4SLinus Torvalds *ifap = ifa->tmp_next; 6991da177e4SLinus Torvalds if (ifp->ifpub) { 7001da177e4SLinus Torvalds in6_ifa_put(ifp->ifpub); 7011da177e4SLinus Torvalds ifp->ifpub = NULL; 7021da177e4SLinus Torvalds } 7031da177e4SLinus Torvalds __in6_ifa_put(ifp); 7041da177e4SLinus Torvalds ifa->tmp_next = NULL; 7051da177e4SLinus Torvalds break; 7061da177e4SLinus Torvalds } 7071da177e4SLinus Torvalds } 7081da177e4SLinus Torvalds } 7091da177e4SLinus Torvalds #endif 7101da177e4SLinus Torvalds 7111d142804SKristian Slavov for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;) { 7121da177e4SLinus Torvalds if (ifa == ifp) { 7131da177e4SLinus Torvalds *ifap = ifa->if_next; 7141da177e4SLinus Torvalds __in6_ifa_put(ifp); 7151da177e4SLinus Torvalds ifa->if_next = NULL; 7161da177e4SLinus Torvalds if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0) 7171da177e4SLinus Torvalds break; 7181da177e4SLinus Torvalds deleted = 1; 7191d142804SKristian Slavov continue; 7201da177e4SLinus Torvalds } else if (ifp->flags & IFA_F_PERMANENT) { 7211da177e4SLinus Torvalds if (ipv6_prefix_equal(&ifa->addr, &ifp->addr, 7221da177e4SLinus Torvalds ifp->prefix_len)) { 7231da177e4SLinus Torvalds if (ifa->flags & IFA_F_PERMANENT) { 7241da177e4SLinus Torvalds onlink = 1; 7251da177e4SLinus Torvalds if (deleted) 7261da177e4SLinus Torvalds break; 7271da177e4SLinus Torvalds } else { 7281da177e4SLinus Torvalds unsigned long lifetime; 7291da177e4SLinus Torvalds 7301da177e4SLinus Torvalds if (!onlink) 7311da177e4SLinus Torvalds onlink = -1; 7321da177e4SLinus Torvalds 7331da177e4SLinus Torvalds spin_lock(&ifa->lock); 7341da177e4SLinus Torvalds lifetime = min_t(unsigned long, 7351da177e4SLinus Torvalds ifa->valid_lft, 0x7fffffffUL/HZ); 7361da177e4SLinus Torvalds if (time_before(expires, 7371da177e4SLinus Torvalds ifa->tstamp + lifetime * HZ)) 7381da177e4SLinus Torvalds expires = ifa->tstamp + lifetime * HZ; 7391da177e4SLinus Torvalds spin_unlock(&ifa->lock); 7401da177e4SLinus Torvalds } 7411da177e4SLinus Torvalds } 7421da177e4SLinus Torvalds } 7431d142804SKristian Slavov ifap = &ifa->if_next; 7441da177e4SLinus Torvalds } 7451da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 7461da177e4SLinus Torvalds 7471da177e4SLinus Torvalds ipv6_ifa_notify(RTM_DELADDR, ifp); 7481da177e4SLinus Torvalds 749e041c683SAlan Stern atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp); 7501da177e4SLinus Torvalds 7511da177e4SLinus Torvalds addrconf_del_timer(ifp); 7521da177e4SLinus Torvalds 7531da177e4SLinus Torvalds /* 7541da177e4SLinus Torvalds * Purge or update corresponding prefix 7551da177e4SLinus Torvalds * 7561da177e4SLinus Torvalds * 1) we don't purge prefix here if address was not permanent. 7571da177e4SLinus Torvalds * prefix is managed by its own lifetime. 7581da177e4SLinus Torvalds * 2) if there're no addresses, delete prefix. 7591da177e4SLinus Torvalds * 3) if there're still other permanent address(es), 7601da177e4SLinus Torvalds * corresponding prefix is still permanent. 7611da177e4SLinus Torvalds * 4) otherwise, update prefix lifetime to the 7621da177e4SLinus Torvalds * longest valid lifetime among the corresponding 7631da177e4SLinus Torvalds * addresses on the device. 7641da177e4SLinus Torvalds * Note: subsequent RA will update lifetime. 7651da177e4SLinus Torvalds * 7661da177e4SLinus Torvalds * --yoshfuji 7671da177e4SLinus Torvalds */ 7681da177e4SLinus Torvalds if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) { 7691da177e4SLinus Torvalds struct in6_addr prefix; 7701da177e4SLinus Torvalds struct rt6_info *rt; 771c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(ifp->idev->dev); 7721da177e4SLinus Torvalds ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len); 7736fda7350SBenjamin Thery rt = rt6_lookup(net, &prefix, NULL, ifp->idev->dev->ifindex, 1); 7741da177e4SLinus Torvalds 7751da177e4SLinus Torvalds if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) { 7761da177e4SLinus Torvalds if (onlink == 0) { 777e0a1ad73SThomas Graf ip6_del_rt(rt); 7781da177e4SLinus Torvalds rt = NULL; 7791da177e4SLinus Torvalds } else if (!(rt->rt6i_flags & RTF_EXPIRES)) { 7801da177e4SLinus Torvalds rt->rt6i_expires = expires; 7811da177e4SLinus Torvalds rt->rt6i_flags |= RTF_EXPIRES; 7821da177e4SLinus Torvalds } 7831da177e4SLinus Torvalds } 7841da177e4SLinus Torvalds dst_release(&rt->u.dst); 7851da177e4SLinus Torvalds } 7861da177e4SLinus Torvalds 7871da177e4SLinus Torvalds in6_ifa_put(ifp); 7881da177e4SLinus Torvalds } 7891da177e4SLinus Torvalds 7901da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 7911da177e4SLinus Torvalds static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift) 7921da177e4SLinus Torvalds { 7931da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 7941da177e4SLinus Torvalds struct in6_addr addr, *tmpaddr; 7951da177e4SLinus Torvalds unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp; 796eac55bf9SBenoit Boissinot unsigned long regen_advance; 7971da177e4SLinus Torvalds int tmp_plen; 7981da177e4SLinus Torvalds int ret = 0; 7991da177e4SLinus Torvalds int max_addresses; 80095c385b4SNeil Horman u32 addr_flags; 8011da177e4SLinus Torvalds 8021da177e4SLinus Torvalds write_lock(&idev->lock); 8031da177e4SLinus Torvalds if (ift) { 8041da177e4SLinus Torvalds spin_lock_bh(&ift->lock); 8051da177e4SLinus Torvalds memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8); 8061da177e4SLinus Torvalds spin_unlock_bh(&ift->lock); 8071da177e4SLinus Torvalds tmpaddr = &addr; 8081da177e4SLinus Torvalds } else { 8091da177e4SLinus Torvalds tmpaddr = NULL; 8101da177e4SLinus Torvalds } 8111da177e4SLinus Torvalds retry: 8121da177e4SLinus Torvalds in6_dev_hold(idev); 8131da177e4SLinus Torvalds if (idev->cnf.use_tempaddr <= 0) { 8141da177e4SLinus Torvalds write_unlock(&idev->lock); 8151da177e4SLinus Torvalds printk(KERN_INFO 8161da177e4SLinus Torvalds "ipv6_create_tempaddr(): use_tempaddr is disabled.\n"); 8171da177e4SLinus Torvalds in6_dev_put(idev); 8181da177e4SLinus Torvalds ret = -1; 8191da177e4SLinus Torvalds goto out; 8201da177e4SLinus Torvalds } 8211da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 8221da177e4SLinus Torvalds if (ifp->regen_count++ >= idev->cnf.regen_max_retry) { 8231da177e4SLinus Torvalds idev->cnf.use_tempaddr = -1; /*XXX*/ 8241da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 8251da177e4SLinus Torvalds write_unlock(&idev->lock); 8261da177e4SLinus Torvalds printk(KERN_WARNING 8271da177e4SLinus Torvalds "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n"); 8281da177e4SLinus Torvalds in6_dev_put(idev); 8291da177e4SLinus Torvalds ret = -1; 8301da177e4SLinus Torvalds goto out; 8311da177e4SLinus Torvalds } 8321da177e4SLinus Torvalds in6_ifa_hold(ifp); 8331da177e4SLinus Torvalds memcpy(addr.s6_addr, ifp->addr.s6_addr, 8); 8341da177e4SLinus Torvalds if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) { 8351da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 8361da177e4SLinus Torvalds write_unlock(&idev->lock); 8371da177e4SLinus Torvalds printk(KERN_WARNING 8381da177e4SLinus Torvalds "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n"); 8391da177e4SLinus Torvalds in6_ifa_put(ifp); 8401da177e4SLinus Torvalds in6_dev_put(idev); 8411da177e4SLinus Torvalds ret = -1; 8421da177e4SLinus Torvalds goto out; 8431da177e4SLinus Torvalds } 8441da177e4SLinus Torvalds memcpy(&addr.s6_addr[8], idev->rndid, 8); 8451da177e4SLinus Torvalds tmp_valid_lft = min_t(__u32, 8461da177e4SLinus Torvalds ifp->valid_lft, 8471da177e4SLinus Torvalds idev->cnf.temp_valid_lft); 8481da177e4SLinus Torvalds tmp_prefered_lft = min_t(__u32, 8491da177e4SLinus Torvalds ifp->prefered_lft, 8501da177e4SLinus Torvalds idev->cnf.temp_prefered_lft - desync_factor / HZ); 8511da177e4SLinus Torvalds tmp_plen = ifp->prefix_len; 8521da177e4SLinus Torvalds max_addresses = idev->cnf.max_addresses; 8531da177e4SLinus Torvalds tmp_cstamp = ifp->cstamp; 8541da177e4SLinus Torvalds tmp_tstamp = ifp->tstamp; 8551da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 8561da177e4SLinus Torvalds 857eac55bf9SBenoit Boissinot regen_advance = idev->cnf.regen_max_retry * 858eac55bf9SBenoit Boissinot idev->cnf.dad_transmits * 859eac55bf9SBenoit Boissinot idev->nd_parms->retrans_time / HZ; 8601da177e4SLinus Torvalds write_unlock(&idev->lock); 86195c385b4SNeil Horman 862eac55bf9SBenoit Boissinot /* A temporary address is created only if this calculated Preferred 863eac55bf9SBenoit Boissinot * Lifetime is greater than REGEN_ADVANCE time units. In particular, 864eac55bf9SBenoit Boissinot * an implementation must not create a temporary address with a zero 865eac55bf9SBenoit Boissinot * Preferred Lifetime. 866eac55bf9SBenoit Boissinot */ 867eac55bf9SBenoit Boissinot if (tmp_prefered_lft <= regen_advance) { 868eac55bf9SBenoit Boissinot in6_ifa_put(ifp); 869eac55bf9SBenoit Boissinot in6_dev_put(idev); 870eac55bf9SBenoit Boissinot ret = -1; 871eac55bf9SBenoit Boissinot goto out; 872eac55bf9SBenoit Boissinot } 873eac55bf9SBenoit Boissinot 87495c385b4SNeil Horman addr_flags = IFA_F_TEMPORARY; 87595c385b4SNeil Horman /* set in addrconf_prefix_rcv() */ 87695c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 87795c385b4SNeil Horman addr_flags |= IFA_F_OPTIMISTIC; 87895c385b4SNeil Horman 8791da177e4SLinus Torvalds ift = !max_addresses || 8801da177e4SLinus Torvalds ipv6_count_addresses(idev) < max_addresses ? 8811da177e4SLinus Torvalds ipv6_add_addr(idev, &addr, tmp_plen, 88295c385b4SNeil Horman ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK, 88395c385b4SNeil Horman addr_flags) : NULL; 8841da177e4SLinus Torvalds if (!ift || IS_ERR(ift)) { 8851da177e4SLinus Torvalds in6_ifa_put(ifp); 8861da177e4SLinus Torvalds in6_dev_put(idev); 8871da177e4SLinus Torvalds printk(KERN_INFO 8881da177e4SLinus Torvalds "ipv6_create_tempaddr(): retry temporary address regeneration.\n"); 8891da177e4SLinus Torvalds tmpaddr = &addr; 8901da177e4SLinus Torvalds write_lock(&idev->lock); 8911da177e4SLinus Torvalds goto retry; 8921da177e4SLinus Torvalds } 8931da177e4SLinus Torvalds 8941da177e4SLinus Torvalds spin_lock_bh(&ift->lock); 8951da177e4SLinus Torvalds ift->ifpub = ifp; 8961da177e4SLinus Torvalds ift->valid_lft = tmp_valid_lft; 8971da177e4SLinus Torvalds ift->prefered_lft = tmp_prefered_lft; 8981da177e4SLinus Torvalds ift->cstamp = tmp_cstamp; 8991da177e4SLinus Torvalds ift->tstamp = tmp_tstamp; 9001da177e4SLinus Torvalds spin_unlock_bh(&ift->lock); 9011da177e4SLinus Torvalds 9021da177e4SLinus Torvalds addrconf_dad_start(ift, 0); 9031da177e4SLinus Torvalds in6_ifa_put(ift); 9041da177e4SLinus Torvalds in6_dev_put(idev); 9051da177e4SLinus Torvalds out: 9061da177e4SLinus Torvalds return ret; 9071da177e4SLinus Torvalds } 9081da177e4SLinus Torvalds #endif 9091da177e4SLinus Torvalds 9101da177e4SLinus Torvalds /* 911072047e4SYOSHIFUJI Hideaki * Choose an appropriate source address (RFC3484) 9121da177e4SLinus Torvalds */ 913a9b05723SYOSHIFUJI Hideaki enum { 914a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_INIT = 0, 915a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_LOCAL, 916a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_SCOPE, 917a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PREFERRED, 918a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6 919a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_HOA, 920a9b05723SYOSHIFUJI Hideaki #endif 921a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_OIF, 922a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_LABEL, 923a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_PRIVACY 924a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PRIVACY, 925a9b05723SYOSHIFUJI Hideaki #endif 926a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_ORCHID, 927a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PREFIX, 928a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_MAX 929072047e4SYOSHIFUJI Hideaki }; 930072047e4SYOSHIFUJI Hideaki 931a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score { 932a9b05723SYOSHIFUJI Hideaki int rule; 933a9b05723SYOSHIFUJI Hideaki int addr_type; 934a9b05723SYOSHIFUJI Hideaki struct inet6_ifaddr *ifa; 935a9b05723SYOSHIFUJI Hideaki DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX); 936a9b05723SYOSHIFUJI Hideaki int scopedist; 937a9b05723SYOSHIFUJI Hideaki int matchlen; 938a9b05723SYOSHIFUJI Hideaki }; 939a9b05723SYOSHIFUJI Hideaki 940a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst { 9419acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *addr; 942a9b05723SYOSHIFUJI Hideaki int ifindex; 943a9b05723SYOSHIFUJI Hideaki int scope; 944a9b05723SYOSHIFUJI Hideaki int label; 9457cbca67cSYOSHIFUJI Hideaki unsigned int prefs; 946a9b05723SYOSHIFUJI Hideaki }; 947072047e4SYOSHIFUJI Hideaki 948b6f99a21SDave Jones static inline int ipv6_saddr_preferred(int type) 9491da177e4SLinus Torvalds { 950072047e4SYOSHIFUJI Hideaki if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4| 951072047e4SYOSHIFUJI Hideaki IPV6_ADDR_LOOPBACK|IPV6_ADDR_RESERVED)) 952072047e4SYOSHIFUJI Hideaki return 1; 953072047e4SYOSHIFUJI Hideaki return 0; 9541da177e4SLinus Torvalds } 9551da177e4SLinus Torvalds 956a9b05723SYOSHIFUJI Hideaki static int ipv6_get_saddr_eval(struct ipv6_saddr_score *score, 957a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst *dst, 958a9b05723SYOSHIFUJI Hideaki int i) 959a9b05723SYOSHIFUJI Hideaki { 960a9b05723SYOSHIFUJI Hideaki int ret; 961a9b05723SYOSHIFUJI Hideaki 962a9b05723SYOSHIFUJI Hideaki if (i <= score->rule) { 963a9b05723SYOSHIFUJI Hideaki switch (i) { 964a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_SCOPE: 965a9b05723SYOSHIFUJI Hideaki ret = score->scopedist; 966a9b05723SYOSHIFUJI Hideaki break; 967a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFIX: 968a9b05723SYOSHIFUJI Hideaki ret = score->matchlen; 969a9b05723SYOSHIFUJI Hideaki break; 970a9b05723SYOSHIFUJI Hideaki default: 971a9b05723SYOSHIFUJI Hideaki ret = !!test_bit(i, score->scorebits); 972a9b05723SYOSHIFUJI Hideaki } 973a9b05723SYOSHIFUJI Hideaki goto out; 974a9b05723SYOSHIFUJI Hideaki } 975a9b05723SYOSHIFUJI Hideaki 976a9b05723SYOSHIFUJI Hideaki switch (i) { 977a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_INIT: 978a9b05723SYOSHIFUJI Hideaki /* Rule 0: remember if hiscore is not ready yet */ 979a9b05723SYOSHIFUJI Hideaki ret = !!score->ifa; 980a9b05723SYOSHIFUJI Hideaki break; 981a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_LOCAL: 982a9b05723SYOSHIFUJI Hideaki /* Rule 1: Prefer same address */ 983a9b05723SYOSHIFUJI Hideaki ret = ipv6_addr_equal(&score->ifa->addr, dst->addr); 984a9b05723SYOSHIFUJI Hideaki break; 985a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_SCOPE: 986a9b05723SYOSHIFUJI Hideaki /* Rule 2: Prefer appropriate scope 987a9b05723SYOSHIFUJI Hideaki * 988a9b05723SYOSHIFUJI Hideaki * ret 989a9b05723SYOSHIFUJI Hideaki * ^ 990a9b05723SYOSHIFUJI Hideaki * -1 | d 15 991a9b05723SYOSHIFUJI Hideaki * ---+--+-+---> scope 992a9b05723SYOSHIFUJI Hideaki * | 993a9b05723SYOSHIFUJI Hideaki * | d is scope of the destination. 994a9b05723SYOSHIFUJI Hideaki * B-d | \ 995a9b05723SYOSHIFUJI Hideaki * | \ <- smaller scope is better if 996a9b05723SYOSHIFUJI Hideaki * B-15 | \ if scope is enough for destinaion. 997a9b05723SYOSHIFUJI Hideaki * | ret = B - scope (-1 <= scope >= d <= 15). 998a9b05723SYOSHIFUJI Hideaki * d-C-1 | / 999a9b05723SYOSHIFUJI Hideaki * |/ <- greater is better 1000a9b05723SYOSHIFUJI Hideaki * -C / if scope is not enough for destination. 1001a9b05723SYOSHIFUJI Hideaki * /| ret = scope - C (-1 <= d < scope <= 15). 1002a9b05723SYOSHIFUJI Hideaki * 1003a9b05723SYOSHIFUJI Hideaki * d - C - 1 < B -15 (for all -1 <= d <= 15). 1004a9b05723SYOSHIFUJI Hideaki * C > d + 14 - B >= 15 + 14 - B = 29 - B. 1005a9b05723SYOSHIFUJI Hideaki * Assume B = 0 and we get C > 29. 1006a9b05723SYOSHIFUJI Hideaki */ 1007a9b05723SYOSHIFUJI Hideaki ret = __ipv6_addr_src_scope(score->addr_type); 1008a9b05723SYOSHIFUJI Hideaki if (ret >= dst->scope) 1009a9b05723SYOSHIFUJI Hideaki ret = -ret; 1010a9b05723SYOSHIFUJI Hideaki else 1011a9b05723SYOSHIFUJI Hideaki ret -= 128; /* 30 is enough */ 1012a9b05723SYOSHIFUJI Hideaki score->scopedist = ret; 1013a9b05723SYOSHIFUJI Hideaki break; 1014a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFERRED: 1015a9b05723SYOSHIFUJI Hideaki /* Rule 3: Avoid deprecated and optimistic addresses */ 1016a9b05723SYOSHIFUJI Hideaki ret = ipv6_saddr_preferred(score->addr_type) || 1017a9b05723SYOSHIFUJI Hideaki !(score->ifa->flags & (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC)); 1018a9b05723SYOSHIFUJI Hideaki break; 1019a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6 1020a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_HOA: 10217cbca67cSYOSHIFUJI Hideaki { 1022a9b05723SYOSHIFUJI Hideaki /* Rule 4: Prefer home address */ 10237cbca67cSYOSHIFUJI Hideaki int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA); 10247cbca67cSYOSHIFUJI Hideaki ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome; 1025a9b05723SYOSHIFUJI Hideaki break; 10267cbca67cSYOSHIFUJI Hideaki } 1027a9b05723SYOSHIFUJI Hideaki #endif 1028a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_OIF: 1029a9b05723SYOSHIFUJI Hideaki /* Rule 5: Prefer outgoing interface */ 1030a9b05723SYOSHIFUJI Hideaki ret = (!dst->ifindex || 1031a9b05723SYOSHIFUJI Hideaki dst->ifindex == score->ifa->idev->dev->ifindex); 1032a9b05723SYOSHIFUJI Hideaki break; 1033a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_LABEL: 1034a9b05723SYOSHIFUJI Hideaki /* Rule 6: Prefer matching label */ 1035a9b05723SYOSHIFUJI Hideaki ret = ipv6_addr_label(&score->ifa->addr, score->addr_type, 1036a9b05723SYOSHIFUJI Hideaki score->ifa->idev->dev->ifindex) == dst->label; 1037a9b05723SYOSHIFUJI Hideaki break; 1038a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_PRIVACY 1039a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PRIVACY: 10407cbca67cSYOSHIFUJI Hideaki { 1041a9b05723SYOSHIFUJI Hideaki /* Rule 7: Prefer public address 1042a9b05723SYOSHIFUJI Hideaki * Note: prefer temprary address if use_tempaddr >= 2 1043a9b05723SYOSHIFUJI Hideaki */ 10447cbca67cSYOSHIFUJI Hideaki int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ? 10457cbca67cSYOSHIFUJI Hideaki !!(dst->prefs & IPV6_PREFER_SRC_TMP) : 10467cbca67cSYOSHIFUJI Hideaki score->ifa->idev->cnf.use_tempaddr >= 2; 10477cbca67cSYOSHIFUJI Hideaki ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp; 1048a9b05723SYOSHIFUJI Hideaki break; 10497cbca67cSYOSHIFUJI Hideaki } 1050a9b05723SYOSHIFUJI Hideaki #endif 1051a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_ORCHID: 1052a9b05723SYOSHIFUJI Hideaki /* Rule 8-: Prefer ORCHID vs ORCHID or 1053a9b05723SYOSHIFUJI Hideaki * non-ORCHID vs non-ORCHID 1054a9b05723SYOSHIFUJI Hideaki */ 1055a9b05723SYOSHIFUJI Hideaki ret = !(ipv6_addr_orchid(&score->ifa->addr) ^ 1056a9b05723SYOSHIFUJI Hideaki ipv6_addr_orchid(dst->addr)); 1057a9b05723SYOSHIFUJI Hideaki break; 1058a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFIX: 1059a9b05723SYOSHIFUJI Hideaki /* Rule 8: Use longest matching prefix */ 1060a9b05723SYOSHIFUJI Hideaki score->matchlen = ret = ipv6_addr_diff(&score->ifa->addr, 1061a9b05723SYOSHIFUJI Hideaki dst->addr); 1062a9b05723SYOSHIFUJI Hideaki break; 1063a9b05723SYOSHIFUJI Hideaki default: 1064a9b05723SYOSHIFUJI Hideaki ret = 0; 1065a9b05723SYOSHIFUJI Hideaki } 1066a9b05723SYOSHIFUJI Hideaki 1067a9b05723SYOSHIFUJI Hideaki if (ret) 1068a9b05723SYOSHIFUJI Hideaki __set_bit(i, score->scorebits); 1069a9b05723SYOSHIFUJI Hideaki score->rule = i; 1070a9b05723SYOSHIFUJI Hideaki out: 1071a9b05723SYOSHIFUJI Hideaki return ret; 1072a9b05723SYOSHIFUJI Hideaki } 1073a9b05723SYOSHIFUJI Hideaki 1074a9b05723SYOSHIFUJI Hideaki int ipv6_dev_get_saddr(struct net_device *dst_dev, 10759acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *daddr, unsigned int prefs, 10767cbca67cSYOSHIFUJI Hideaki struct in6_addr *saddr) 10771da177e4SLinus Torvalds { 1078a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score scores[2], 1079a9b05723SYOSHIFUJI Hideaki *score = &scores[0], *hiscore = &scores[1]; 1080c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dst_dev); 1081a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst dst; 1082072047e4SYOSHIFUJI Hideaki struct net_device *dev; 1083a9b05723SYOSHIFUJI Hideaki int dst_type; 10841da177e4SLinus Torvalds 1085a9b05723SYOSHIFUJI Hideaki dst_type = __ipv6_addr_type(daddr); 1086a9b05723SYOSHIFUJI Hideaki dst.addr = daddr; 1087a9b05723SYOSHIFUJI Hideaki dst.ifindex = dst_dev ? dst_dev->ifindex : 0; 1088a9b05723SYOSHIFUJI Hideaki dst.scope = __ipv6_addr_src_scope(dst_type); 1089a9b05723SYOSHIFUJI Hideaki dst.label = ipv6_addr_label(daddr, dst_type, dst.ifindex); 10907cbca67cSYOSHIFUJI Hideaki dst.prefs = prefs; 1091a9b05723SYOSHIFUJI Hideaki 1092a9b05723SYOSHIFUJI Hideaki hiscore->rule = -1; 1093a9b05723SYOSHIFUJI Hideaki hiscore->ifa = NULL; 10941da177e4SLinus Torvalds 10951da177e4SLinus Torvalds read_lock(&dev_base_lock); 10968814c4b5SYOSHIFUJI Hideaki rcu_read_lock(); 1097072047e4SYOSHIFUJI Hideaki 10986fda7350SBenjamin Thery for_each_netdev(net, dev) { 1099072047e4SYOSHIFUJI Hideaki struct inet6_dev *idev; 1100072047e4SYOSHIFUJI Hideaki 1101a9b05723SYOSHIFUJI Hideaki /* Candidate Source Address (section 4) 1102072047e4SYOSHIFUJI Hideaki * - multicast and link-local destination address, 1103072047e4SYOSHIFUJI Hideaki * the set of candidate source address MUST only 1104072047e4SYOSHIFUJI Hideaki * include addresses assigned to interfaces 1105072047e4SYOSHIFUJI Hideaki * belonging to the same link as the outgoing 1106072047e4SYOSHIFUJI Hideaki * interface. 1107072047e4SYOSHIFUJI Hideaki * (- For site-local destination addresses, the 1108072047e4SYOSHIFUJI Hideaki * set of candidate source addresses MUST only 1109072047e4SYOSHIFUJI Hideaki * include addresses assigned to interfaces 1110072047e4SYOSHIFUJI Hideaki * belonging to the same site as the outgoing 1111072047e4SYOSHIFUJI Hideaki * interface.) 1112072047e4SYOSHIFUJI Hideaki */ 1113a9b05723SYOSHIFUJI Hideaki if (((dst_type & IPV6_ADDR_MULTICAST) || 1114a9b05723SYOSHIFUJI Hideaki dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL) && 1115a9b05723SYOSHIFUJI Hideaki dst.ifindex && dev->ifindex != dst.ifindex) 1116072047e4SYOSHIFUJI Hideaki continue; 1117072047e4SYOSHIFUJI Hideaki 11181da177e4SLinus Torvalds idev = __in6_dev_get(dev); 1119072047e4SYOSHIFUJI Hideaki if (!idev) 1120072047e4SYOSHIFUJI Hideaki continue; 1121072047e4SYOSHIFUJI Hideaki 11221da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1123a9b05723SYOSHIFUJI Hideaki for (score->ifa = idev->addr_list; score->ifa; score->ifa = score->ifa->if_next) { 1124a9b05723SYOSHIFUJI Hideaki int i; 1125072047e4SYOSHIFUJI Hideaki 1126a9b05723SYOSHIFUJI Hideaki /* 11276b3ae80aSYOSHIFUJI Hideaki * - Tentative Address (RFC2462 section 5.4) 11286b3ae80aSYOSHIFUJI Hideaki * - A tentative address is not considered 11296b3ae80aSYOSHIFUJI Hideaki * "assigned to an interface" in the traditional 113095c385b4SNeil Horman * sense, unless it is also flagged as optimistic. 11316b3ae80aSYOSHIFUJI Hideaki * - Candidate Source Address (section 4) 1132072047e4SYOSHIFUJI Hideaki * - In any case, anycast addresses, multicast 1133072047e4SYOSHIFUJI Hideaki * addresses, and the unspecified address MUST 1134072047e4SYOSHIFUJI Hideaki * NOT be included in a candidate set. 1135072047e4SYOSHIFUJI Hideaki */ 1136a9b05723SYOSHIFUJI Hideaki if ((score->ifa->flags & IFA_F_TENTATIVE) && 1137a9b05723SYOSHIFUJI Hideaki (!(score->ifa->flags & IFA_F_OPTIMISTIC))) 11386b3ae80aSYOSHIFUJI Hideaki continue; 1139a9b05723SYOSHIFUJI Hideaki 1140a9b05723SYOSHIFUJI Hideaki score->addr_type = __ipv6_addr_type(&score->ifa->addr); 1141a9b05723SYOSHIFUJI Hideaki 1142a9b05723SYOSHIFUJI Hideaki if (unlikely(score->addr_type == IPV6_ADDR_ANY || 1143a9b05723SYOSHIFUJI Hideaki score->addr_type & IPV6_ADDR_MULTICAST)) { 1144072047e4SYOSHIFUJI Hideaki LIMIT_NETDEBUG(KERN_DEBUG 1145072047e4SYOSHIFUJI Hideaki "ADDRCONF: unspecified / multicast address " 1146072047e4SYOSHIFUJI Hideaki "assigned as unicast address on %s", 1147072047e4SYOSHIFUJI Hideaki dev->name); 11481da177e4SLinus Torvalds continue; 1149072047e4SYOSHIFUJI Hideaki } 1150072047e4SYOSHIFUJI Hideaki 1151a9b05723SYOSHIFUJI Hideaki score->rule = -1; 1152a9b05723SYOSHIFUJI Hideaki bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX); 1153072047e4SYOSHIFUJI Hideaki 1154a9b05723SYOSHIFUJI Hideaki for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) { 1155a9b05723SYOSHIFUJI Hideaki int minihiscore, miniscore; 1156072047e4SYOSHIFUJI Hideaki 1157a9b05723SYOSHIFUJI Hideaki minihiscore = ipv6_get_saddr_eval(hiscore, &dst, i); 1158a9b05723SYOSHIFUJI Hideaki miniscore = ipv6_get_saddr_eval(score, &dst, i); 1159072047e4SYOSHIFUJI Hideaki 1160a9b05723SYOSHIFUJI Hideaki if (minihiscore > miniscore) { 1161a9b05723SYOSHIFUJI Hideaki if (i == IPV6_SADDR_RULE_SCOPE && 1162a9b05723SYOSHIFUJI Hideaki score->scopedist > 0) { 1163a9b05723SYOSHIFUJI Hideaki /* 1164a9b05723SYOSHIFUJI Hideaki * special case: 1165a9b05723SYOSHIFUJI Hideaki * each remaining entry 1166a9b05723SYOSHIFUJI Hideaki * has too small (not enough) 1167a9b05723SYOSHIFUJI Hideaki * scope, because ifa entries 1168a9b05723SYOSHIFUJI Hideaki * are sorted by their scope 1169a9b05723SYOSHIFUJI Hideaki * values. 1170072047e4SYOSHIFUJI Hideaki */ 1171a9b05723SYOSHIFUJI Hideaki goto try_nextdev; 1172072047e4SYOSHIFUJI Hideaki } 1173a9b05723SYOSHIFUJI Hideaki break; 1174a9b05723SYOSHIFUJI Hideaki } else if (minihiscore < miniscore) { 1175a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score *tmp; 117699cd07a5SJuha-Matti Tapio 1177a9b05723SYOSHIFUJI Hideaki if (hiscore->ifa) 1178a9b05723SYOSHIFUJI Hideaki in6_ifa_put(hiscore->ifa); 117999cd07a5SJuha-Matti Tapio 1180a9b05723SYOSHIFUJI Hideaki in6_ifa_hold(score->ifa); 1181072047e4SYOSHIFUJI Hideaki 1182a9b05723SYOSHIFUJI Hideaki tmp = hiscore; 11831da177e4SLinus Torvalds hiscore = score; 1184a9b05723SYOSHIFUJI Hideaki score = tmp; 1185a9b05723SYOSHIFUJI Hideaki 1186a9b05723SYOSHIFUJI Hideaki /* restore our iterator */ 1187a9b05723SYOSHIFUJI Hideaki score->ifa = hiscore->ifa; 1188a9b05723SYOSHIFUJI Hideaki 1189a9b05723SYOSHIFUJI Hideaki break; 11901da177e4SLinus Torvalds } 1191a9b05723SYOSHIFUJI Hideaki } 1192a9b05723SYOSHIFUJI Hideaki } 1193a9b05723SYOSHIFUJI Hideaki try_nextdev: 11941da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 11951da177e4SLinus Torvalds } 11968814c4b5SYOSHIFUJI Hideaki rcu_read_unlock(); 11971da177e4SLinus Torvalds read_unlock(&dev_base_lock); 11981da177e4SLinus Torvalds 1199a9b05723SYOSHIFUJI Hideaki if (!hiscore->ifa) 1200072047e4SYOSHIFUJI Hideaki return -EADDRNOTAVAIL; 12011da177e4SLinus Torvalds 1202a9b05723SYOSHIFUJI Hideaki ipv6_addr_copy(saddr, &hiscore->ifa->addr); 1203a9b05723SYOSHIFUJI Hideaki in6_ifa_put(hiscore->ifa); 1204072047e4SYOSHIFUJI Hideaki return 0; 12051da177e4SLinus Torvalds } 12061da177e4SLinus Torvalds 12075e5f3f0fSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_dev_get_saddr); 12081da177e4SLinus Torvalds 120995c385b4SNeil Horman int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr, 121095c385b4SNeil Horman unsigned char banned_flags) 12111da177e4SLinus Torvalds { 12121da177e4SLinus Torvalds struct inet6_dev *idev; 12131da177e4SLinus Torvalds int err = -EADDRNOTAVAIL; 12141da177e4SLinus Torvalds 12158814c4b5SYOSHIFUJI Hideaki rcu_read_lock(); 12161da177e4SLinus Torvalds if ((idev = __in6_dev_get(dev)) != NULL) { 12171da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 12181da177e4SLinus Torvalds 12191da177e4SLinus Torvalds read_lock_bh(&idev->lock); 12201da177e4SLinus Torvalds for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) { 122195c385b4SNeil Horman if (ifp->scope == IFA_LINK && !(ifp->flags & banned_flags)) { 12221da177e4SLinus Torvalds ipv6_addr_copy(addr, &ifp->addr); 12231da177e4SLinus Torvalds err = 0; 12241da177e4SLinus Torvalds break; 12251da177e4SLinus Torvalds } 12261da177e4SLinus Torvalds } 12271da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 12281da177e4SLinus Torvalds } 12298814c4b5SYOSHIFUJI Hideaki rcu_read_unlock(); 12301da177e4SLinus Torvalds return err; 12311da177e4SLinus Torvalds } 12321da177e4SLinus Torvalds 12331da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev) 12341da177e4SLinus Torvalds { 12351da177e4SLinus Torvalds int cnt = 0; 12361da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 12371da177e4SLinus Torvalds 12381da177e4SLinus Torvalds read_lock_bh(&idev->lock); 12391da177e4SLinus Torvalds for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) 12401da177e4SLinus Torvalds cnt++; 12411da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 12421da177e4SLinus Torvalds return cnt; 12431da177e4SLinus Torvalds } 12441da177e4SLinus Torvalds 1245bfeade08SDaniel Lezcano int ipv6_chk_addr(struct net *net, struct in6_addr *addr, 1246bfeade08SDaniel Lezcano struct net_device *dev, int strict) 12471da177e4SLinus Torvalds { 12481da177e4SLinus Torvalds struct inet6_ifaddr * ifp; 12491da177e4SLinus Torvalds u8 hash = ipv6_addr_hash(addr); 12501da177e4SLinus Torvalds 12511da177e4SLinus Torvalds read_lock_bh(&addrconf_hash_lock); 12521da177e4SLinus Torvalds for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) { 1253878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 1254bfeade08SDaniel Lezcano continue; 12551da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr) && 12561da177e4SLinus Torvalds !(ifp->flags&IFA_F_TENTATIVE)) { 12571da177e4SLinus Torvalds if (dev == NULL || ifp->idev->dev == dev || 12581da177e4SLinus Torvalds !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) 12591da177e4SLinus Torvalds break; 12601da177e4SLinus Torvalds } 12611da177e4SLinus Torvalds } 12621da177e4SLinus Torvalds read_unlock_bh(&addrconf_hash_lock); 12631da177e4SLinus Torvalds return ifp != NULL; 12641da177e4SLinus Torvalds } 12657159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_addr); 12667159039aSYOSHIFUJI Hideaki 12671da177e4SLinus Torvalds static 126806bfe655SDaniel Lezcano int ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr, 126906bfe655SDaniel Lezcano struct net_device *dev) 12701da177e4SLinus Torvalds { 12711da177e4SLinus Torvalds struct inet6_ifaddr * ifp; 12721da177e4SLinus Torvalds u8 hash = ipv6_addr_hash(addr); 12731da177e4SLinus Torvalds 12741da177e4SLinus Torvalds for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) { 1275878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 127606bfe655SDaniel Lezcano continue; 12771da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr)) { 12781da177e4SLinus Torvalds if (dev == NULL || ifp->idev->dev == dev) 12791da177e4SLinus Torvalds break; 12801da177e4SLinus Torvalds } 12811da177e4SLinus Torvalds } 12821da177e4SLinus Torvalds return ifp != NULL; 12831da177e4SLinus Torvalds } 12841da177e4SLinus Torvalds 128552eeeb84SYOSHIFUJI Hideaki int ipv6_chk_prefix(struct in6_addr *addr, struct net_device *dev) 128652eeeb84SYOSHIFUJI Hideaki { 128752eeeb84SYOSHIFUJI Hideaki struct inet6_dev *idev; 128852eeeb84SYOSHIFUJI Hideaki struct inet6_ifaddr *ifa; 128952eeeb84SYOSHIFUJI Hideaki int onlink; 129052eeeb84SYOSHIFUJI Hideaki 129152eeeb84SYOSHIFUJI Hideaki onlink = 0; 129252eeeb84SYOSHIFUJI Hideaki rcu_read_lock(); 129352eeeb84SYOSHIFUJI Hideaki idev = __in6_dev_get(dev); 129452eeeb84SYOSHIFUJI Hideaki if (idev) { 129552eeeb84SYOSHIFUJI Hideaki read_lock_bh(&idev->lock); 129652eeeb84SYOSHIFUJI Hideaki for (ifa = idev->addr_list; ifa; ifa = ifa->if_next) { 129752eeeb84SYOSHIFUJI Hideaki onlink = ipv6_prefix_equal(addr, &ifa->addr, 129852eeeb84SYOSHIFUJI Hideaki ifa->prefix_len); 129952eeeb84SYOSHIFUJI Hideaki if (onlink) 130052eeeb84SYOSHIFUJI Hideaki break; 130152eeeb84SYOSHIFUJI Hideaki } 130252eeeb84SYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 130352eeeb84SYOSHIFUJI Hideaki } 130452eeeb84SYOSHIFUJI Hideaki rcu_read_unlock(); 130552eeeb84SYOSHIFUJI Hideaki return onlink; 130652eeeb84SYOSHIFUJI Hideaki } 130752eeeb84SYOSHIFUJI Hideaki 130852eeeb84SYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_prefix); 130952eeeb84SYOSHIFUJI Hideaki 13109acd9f3aSYOSHIFUJI Hideaki struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr, 13111cab3da6SDaniel Lezcano struct net_device *dev, int strict) 13121da177e4SLinus Torvalds { 13131da177e4SLinus Torvalds struct inet6_ifaddr * ifp; 13141da177e4SLinus Torvalds u8 hash = ipv6_addr_hash(addr); 13151da177e4SLinus Torvalds 13161da177e4SLinus Torvalds read_lock_bh(&addrconf_hash_lock); 13171da177e4SLinus Torvalds for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) { 1318878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 13191cab3da6SDaniel Lezcano continue; 13201da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr)) { 13211da177e4SLinus Torvalds if (dev == NULL || ifp->idev->dev == dev || 13221da177e4SLinus Torvalds !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) { 13231da177e4SLinus Torvalds in6_ifa_hold(ifp); 13241da177e4SLinus Torvalds break; 13251da177e4SLinus Torvalds } 13261da177e4SLinus Torvalds } 13271da177e4SLinus Torvalds } 13281da177e4SLinus Torvalds read_unlock_bh(&addrconf_hash_lock); 13291da177e4SLinus Torvalds 13301da177e4SLinus Torvalds return ifp; 13311da177e4SLinus Torvalds } 13321da177e4SLinus Torvalds 13331da177e4SLinus Torvalds int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2) 13341da177e4SLinus Torvalds { 13351da177e4SLinus Torvalds const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr; 13360fa1a53eSArnaldo Carvalho de Melo const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2); 133782103232SAl Viro __be32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr; 133882103232SAl Viro __be32 sk2_rcv_saddr = inet_rcv_saddr(sk2); 13391da177e4SLinus Torvalds int sk_ipv6only = ipv6_only_sock(sk); 1340463c84b9SArnaldo Carvalho de Melo int sk2_ipv6only = inet_v6_ipv6only(sk2); 13411da177e4SLinus Torvalds int addr_type = ipv6_addr_type(sk_rcv_saddr6); 13421da177e4SLinus Torvalds int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED; 13431da177e4SLinus Torvalds 13441da177e4SLinus Torvalds if (!sk2_rcv_saddr && !sk_ipv6only) 13451da177e4SLinus Torvalds return 1; 13461da177e4SLinus Torvalds 13471da177e4SLinus Torvalds if (addr_type2 == IPV6_ADDR_ANY && 13481da177e4SLinus Torvalds !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED)) 13491da177e4SLinus Torvalds return 1; 13501da177e4SLinus Torvalds 13511da177e4SLinus Torvalds if (addr_type == IPV6_ADDR_ANY && 13521da177e4SLinus Torvalds !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED)) 13531da177e4SLinus Torvalds return 1; 13541da177e4SLinus Torvalds 13551da177e4SLinus Torvalds if (sk2_rcv_saddr6 && 13561da177e4SLinus Torvalds ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6)) 13571da177e4SLinus Torvalds return 1; 13581da177e4SLinus Torvalds 13591da177e4SLinus Torvalds if (addr_type == IPV6_ADDR_MAPPED && 13601da177e4SLinus Torvalds !sk2_ipv6only && 13611da177e4SLinus Torvalds (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr)) 13621da177e4SLinus Torvalds return 1; 13631da177e4SLinus Torvalds 13641da177e4SLinus Torvalds return 0; 13651da177e4SLinus Torvalds } 13661da177e4SLinus Torvalds 13671da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */ 13681da177e4SLinus Torvalds 13699f5336e2SAdrian Bunk static void addrconf_dad_stop(struct inet6_ifaddr *ifp) 13701da177e4SLinus Torvalds { 13711da177e4SLinus Torvalds if (ifp->flags&IFA_F_PERMANENT) { 13721da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 13731da177e4SLinus Torvalds addrconf_del_timer(ifp); 13741da177e4SLinus Torvalds ifp->flags |= IFA_F_TENTATIVE; 13751da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 13761da177e4SLinus Torvalds in6_ifa_put(ifp); 13771da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 13781da177e4SLinus Torvalds } else if (ifp->flags&IFA_F_TEMPORARY) { 13791da177e4SLinus Torvalds struct inet6_ifaddr *ifpub; 13801da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 13811da177e4SLinus Torvalds ifpub = ifp->ifpub; 13821da177e4SLinus Torvalds if (ifpub) { 13831da177e4SLinus Torvalds in6_ifa_hold(ifpub); 13841da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 13851da177e4SLinus Torvalds ipv6_create_tempaddr(ifpub, ifp); 13861da177e4SLinus Torvalds in6_ifa_put(ifpub); 13871da177e4SLinus Torvalds } else { 13881da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 13891da177e4SLinus Torvalds } 13901da177e4SLinus Torvalds ipv6_del_addr(ifp); 13911da177e4SLinus Torvalds #endif 13921da177e4SLinus Torvalds } else 13931da177e4SLinus Torvalds ipv6_del_addr(ifp); 13941da177e4SLinus Torvalds } 13951da177e4SLinus Torvalds 13963c21edbdSYOSHIFUJI Hideaki void addrconf_dad_failure(struct inet6_ifaddr *ifp) 13973c21edbdSYOSHIFUJI Hideaki { 13983c21edbdSYOSHIFUJI Hideaki if (net_ratelimit()) 13993c21edbdSYOSHIFUJI Hideaki printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name); 14003c21edbdSYOSHIFUJI Hideaki addrconf_dad_stop(ifp); 14013c21edbdSYOSHIFUJI Hideaki } 14021da177e4SLinus Torvalds 14031da177e4SLinus Torvalds /* Join to solicited addr multicast group. */ 14041da177e4SLinus Torvalds 14051da177e4SLinus Torvalds void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr) 14061da177e4SLinus Torvalds { 14071da177e4SLinus Torvalds struct in6_addr maddr; 14081da177e4SLinus Torvalds 14091da177e4SLinus Torvalds if (dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 14101da177e4SLinus Torvalds return; 14111da177e4SLinus Torvalds 14121da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr); 14131da177e4SLinus Torvalds ipv6_dev_mc_inc(dev, &maddr); 14141da177e4SLinus Torvalds } 14151da177e4SLinus Torvalds 14161da177e4SLinus Torvalds void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr) 14171da177e4SLinus Torvalds { 14181da177e4SLinus Torvalds struct in6_addr maddr; 14191da177e4SLinus Torvalds 14201da177e4SLinus Torvalds if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 14211da177e4SLinus Torvalds return; 14221da177e4SLinus Torvalds 14231da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr); 14241da177e4SLinus Torvalds __ipv6_dev_mc_dec(idev, &maddr); 14251da177e4SLinus Torvalds } 14261da177e4SLinus Torvalds 142720380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp) 14281da177e4SLinus Torvalds { 14291da177e4SLinus Torvalds struct in6_addr addr; 14301da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 14311da177e4SLinus Torvalds if (ipv6_addr_any(&addr)) 14321da177e4SLinus Torvalds return; 14331da177e4SLinus Torvalds ipv6_dev_ac_inc(ifp->idev->dev, &addr); 14341da177e4SLinus Torvalds } 14351da177e4SLinus Torvalds 143620380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp) 14371da177e4SLinus Torvalds { 14381da177e4SLinus Torvalds struct in6_addr addr; 14391da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 14401da177e4SLinus Torvalds if (ipv6_addr_any(&addr)) 14411da177e4SLinus Torvalds return; 14421da177e4SLinus Torvalds __ipv6_dev_ac_dec(ifp->idev, &addr); 14431da177e4SLinus Torvalds } 14441da177e4SLinus Torvalds 1445073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev) 14461da177e4SLinus Torvalds { 14471da177e4SLinus Torvalds if (dev->addr_len != ETH_ALEN) 14481da177e4SLinus Torvalds return -1; 14491da177e4SLinus Torvalds memcpy(eui, dev->dev_addr, 3); 14501da177e4SLinus Torvalds memcpy(eui + 5, dev->dev_addr + 3, 3); 14511da177e4SLinus Torvalds 14521da177e4SLinus Torvalds /* 14531da177e4SLinus Torvalds * The zSeries OSA network cards can be shared among various 14541da177e4SLinus Torvalds * OS instances, but the OSA cards have only one MAC address. 14551da177e4SLinus Torvalds * This leads to duplicate address conflicts in conjunction 14561da177e4SLinus Torvalds * with IPv6 if more than one instance uses the same card. 14571da177e4SLinus Torvalds * 14581da177e4SLinus Torvalds * The driver for these cards can deliver a unique 16-bit 14591da177e4SLinus Torvalds * identifier for each instance sharing the same card. It is 14601da177e4SLinus Torvalds * placed instead of 0xFFFE in the interface identifier. The 14611da177e4SLinus Torvalds * "u" bit of the interface identifier is not inverted in this 14621da177e4SLinus Torvalds * case. Hence the resulting interface identifier has local 14631da177e4SLinus Torvalds * scope according to RFC2373. 14641da177e4SLinus Torvalds */ 14651da177e4SLinus Torvalds if (dev->dev_id) { 14661da177e4SLinus Torvalds eui[3] = (dev->dev_id >> 8) & 0xFF; 14671da177e4SLinus Torvalds eui[4] = dev->dev_id & 0xFF; 14681da177e4SLinus Torvalds } else { 14691da177e4SLinus Torvalds eui[3] = 0xFF; 14701da177e4SLinus Torvalds eui[4] = 0xFE; 14711da177e4SLinus Torvalds eui[0] ^= 2; 14721da177e4SLinus Torvalds } 14731da177e4SLinus Torvalds return 0; 1474073a8e0eSYOSHIFUJI Hideaki } 1475073a8e0eSYOSHIFUJI Hideaki 1476073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev) 1477073a8e0eSYOSHIFUJI Hideaki { 14781da177e4SLinus Torvalds /* XXX: inherit EUI-64 from other interface -- yoshfuji */ 14791da177e4SLinus Torvalds if (dev->addr_len != ARCNET_ALEN) 14801da177e4SLinus Torvalds return -1; 14811da177e4SLinus Torvalds memset(eui, 0, 7); 14821da177e4SLinus Torvalds eui[7] = *(u8*)dev->dev_addr; 14831da177e4SLinus Torvalds return 0; 1484073a8e0eSYOSHIFUJI Hideaki } 1485073a8e0eSYOSHIFUJI Hideaki 1486073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev) 1487073a8e0eSYOSHIFUJI Hideaki { 14881da177e4SLinus Torvalds if (dev->addr_len != INFINIBAND_ALEN) 14891da177e4SLinus Torvalds return -1; 14901da177e4SLinus Torvalds memcpy(eui, dev->dev_addr + 12, 8); 14911da177e4SLinus Torvalds eui[0] |= 2; 14921da177e4SLinus Torvalds return 0; 14931da177e4SLinus Torvalds } 1494073a8e0eSYOSHIFUJI Hideaki 1495dfd982baSYOSHIFUJI Hideaki int __ipv6_isatap_ifid(u8 *eui, __be32 addr) 1496dfd982baSYOSHIFUJI Hideaki { 1497dfd982baSYOSHIFUJI Hideaki eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) || 1498dfd982baSYOSHIFUJI Hideaki ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) || 1499dfd982baSYOSHIFUJI Hideaki ipv4_is_private_172(addr) || ipv4_is_test_192(addr) || 1500dfd982baSYOSHIFUJI Hideaki ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) || 1501dfd982baSYOSHIFUJI Hideaki ipv4_is_test_198(addr) || ipv4_is_multicast(addr) || 1502dfd982baSYOSHIFUJI Hideaki ipv4_is_lbcast(addr)) ? 0x00 : 0x02; 1503dfd982baSYOSHIFUJI Hideaki eui[1] = 0; 1504dfd982baSYOSHIFUJI Hideaki eui[2] = 0x5E; 1505dfd982baSYOSHIFUJI Hideaki eui[3] = 0xFE; 1506dfd982baSYOSHIFUJI Hideaki memcpy(eui + 4, &addr, 4); 1507dfd982baSYOSHIFUJI Hideaki return 0; 1508dfd982baSYOSHIFUJI Hideaki } 1509dfd982baSYOSHIFUJI Hideaki EXPORT_SYMBOL(__ipv6_isatap_ifid); 1510dfd982baSYOSHIFUJI Hideaki 1511dfd982baSYOSHIFUJI Hideaki static int addrconf_ifid_sit(u8 *eui, struct net_device *dev) 1512dfd982baSYOSHIFUJI Hideaki { 1513dfd982baSYOSHIFUJI Hideaki if (dev->priv_flags & IFF_ISATAP) 1514dfd982baSYOSHIFUJI Hideaki return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); 1515dfd982baSYOSHIFUJI Hideaki return -1; 1516dfd982baSYOSHIFUJI Hideaki } 1517dfd982baSYOSHIFUJI Hideaki 1518073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev) 1519073a8e0eSYOSHIFUJI Hideaki { 1520073a8e0eSYOSHIFUJI Hideaki switch (dev->type) { 1521073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ETHER: 1522073a8e0eSYOSHIFUJI Hideaki case ARPHRD_FDDI: 1523073a8e0eSYOSHIFUJI Hideaki case ARPHRD_IEEE802_TR: 1524073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_eui48(eui, dev); 1525073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ARCNET: 1526073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_arcnet(eui, dev); 1527073a8e0eSYOSHIFUJI Hideaki case ARPHRD_INFINIBAND: 1528073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_infiniband(eui, dev); 1529c7dc89c0SFred L. Templin case ARPHRD_SIT: 1530dfd982baSYOSHIFUJI Hideaki return addrconf_ifid_sit(eui, dev); 1531073a8e0eSYOSHIFUJI Hideaki } 15321da177e4SLinus Torvalds return -1; 15331da177e4SLinus Torvalds } 15341da177e4SLinus Torvalds 15351da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev) 15361da177e4SLinus Torvalds { 15371da177e4SLinus Torvalds int err = -1; 15381da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 15391da177e4SLinus Torvalds 15401da177e4SLinus Torvalds read_lock_bh(&idev->lock); 15411da177e4SLinus Torvalds for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) { 15421da177e4SLinus Torvalds if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) { 15431da177e4SLinus Torvalds memcpy(eui, ifp->addr.s6_addr+8, 8); 15441da177e4SLinus Torvalds err = 0; 15451da177e4SLinus Torvalds break; 15461da177e4SLinus Torvalds } 15471da177e4SLinus Torvalds } 15481da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 15491da177e4SLinus Torvalds return err; 15501da177e4SLinus Torvalds } 15511da177e4SLinus Torvalds 15521da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 15531da177e4SLinus Torvalds /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */ 15541da177e4SLinus Torvalds static int __ipv6_regen_rndid(struct inet6_dev *idev) 15551da177e4SLinus Torvalds { 15561da177e4SLinus Torvalds regen: 1557955189efSYOSHIFUJI Hideaki get_random_bytes(idev->rndid, sizeof(idev->rndid)); 15581da177e4SLinus Torvalds idev->rndid[0] &= ~0x02; 15591da177e4SLinus Torvalds 15601da177e4SLinus Torvalds /* 15611da177e4SLinus Torvalds * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>: 15621da177e4SLinus Torvalds * check if generated address is not inappropriate 15631da177e4SLinus Torvalds * 15641da177e4SLinus Torvalds * - Reserved subnet anycast (RFC 2526) 15651da177e4SLinus Torvalds * 11111101 11....11 1xxxxxxx 1566c7dc89c0SFred L. Templin * - ISATAP (RFC4214) 6.1 15671da177e4SLinus Torvalds * 00-00-5E-FE-xx-xx-xx-xx 15681da177e4SLinus Torvalds * - value 0 15691da177e4SLinus Torvalds * - XXX: already assigned to an address on the device 15701da177e4SLinus Torvalds */ 15711da177e4SLinus Torvalds if (idev->rndid[0] == 0xfd && 15721da177e4SLinus Torvalds (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff && 15731da177e4SLinus Torvalds (idev->rndid[7]&0x80)) 15741da177e4SLinus Torvalds goto regen; 15751da177e4SLinus Torvalds if ((idev->rndid[0]|idev->rndid[1]) == 0) { 15761da177e4SLinus Torvalds if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe) 15771da177e4SLinus Torvalds goto regen; 15781da177e4SLinus Torvalds if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00) 15791da177e4SLinus Torvalds goto regen; 15801da177e4SLinus Torvalds } 15811da177e4SLinus Torvalds 15821da177e4SLinus Torvalds return 0; 15831da177e4SLinus Torvalds } 15841da177e4SLinus Torvalds 15851da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data) 15861da177e4SLinus Torvalds { 15871da177e4SLinus Torvalds struct inet6_dev *idev = (struct inet6_dev *) data; 15881da177e4SLinus Torvalds unsigned long expires; 15891da177e4SLinus Torvalds 15908814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 15911da177e4SLinus Torvalds write_lock_bh(&idev->lock); 15921da177e4SLinus Torvalds 15931da177e4SLinus Torvalds if (idev->dead) 15941da177e4SLinus Torvalds goto out; 15951da177e4SLinus Torvalds 15961da177e4SLinus Torvalds if (__ipv6_regen_rndid(idev) < 0) 15971da177e4SLinus Torvalds goto out; 15981da177e4SLinus Torvalds 15991da177e4SLinus Torvalds expires = jiffies + 16001da177e4SLinus Torvalds idev->cnf.temp_prefered_lft * HZ - 16011da177e4SLinus Torvalds idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor; 16021da177e4SLinus Torvalds if (time_before(expires, jiffies)) { 16031da177e4SLinus Torvalds printk(KERN_WARNING 16041da177e4SLinus Torvalds "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n", 16051da177e4SLinus Torvalds idev->dev->name); 16061da177e4SLinus Torvalds goto out; 16071da177e4SLinus Torvalds } 16081da177e4SLinus Torvalds 16091da177e4SLinus Torvalds if (!mod_timer(&idev->regen_timer, expires)) 16101da177e4SLinus Torvalds in6_dev_hold(idev); 16111da177e4SLinus Torvalds 16121da177e4SLinus Torvalds out: 16131da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 16148814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 16151da177e4SLinus Torvalds in6_dev_put(idev); 16161da177e4SLinus Torvalds } 16171da177e4SLinus Torvalds 16181da177e4SLinus Torvalds static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) { 16191da177e4SLinus Torvalds int ret = 0; 16201da177e4SLinus Torvalds 16211da177e4SLinus Torvalds if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0) 16221da177e4SLinus Torvalds ret = __ipv6_regen_rndid(idev); 16231da177e4SLinus Torvalds return ret; 16241da177e4SLinus Torvalds } 16251da177e4SLinus Torvalds #endif 16261da177e4SLinus Torvalds 16271da177e4SLinus Torvalds /* 16281da177e4SLinus Torvalds * Add prefix route. 16291da177e4SLinus Torvalds */ 16301da177e4SLinus Torvalds 16311da177e4SLinus Torvalds static void 16321da177e4SLinus Torvalds addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev, 1633e431b8c0SJamal Hadi Salim unsigned long expires, u32 flags) 16341da177e4SLinus Torvalds { 163586872cb5SThomas Graf struct fib6_config cfg = { 163686872cb5SThomas Graf .fc_table = RT6_TABLE_PREFIX, 163786872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 163886872cb5SThomas Graf .fc_ifindex = dev->ifindex, 163986872cb5SThomas Graf .fc_expires = expires, 164086872cb5SThomas Graf .fc_dst_len = plen, 164186872cb5SThomas Graf .fc_flags = RTF_UP | flags, 1642c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 164386872cb5SThomas Graf }; 16441da177e4SLinus Torvalds 164586872cb5SThomas Graf ipv6_addr_copy(&cfg.fc_dst, pfx); 16461da177e4SLinus Torvalds 16471da177e4SLinus Torvalds /* Prevent useless cloning on PtP SIT. 16481da177e4SLinus Torvalds This thing is done here expecting that the whole 16491da177e4SLinus Torvalds class of non-broadcast devices need not cloning. 16501da177e4SLinus Torvalds */ 16510be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 16521da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT)) 165386872cb5SThomas Graf cfg.fc_flags |= RTF_NONEXTHOP; 16540be669bbSJoerg Roedel #endif 16551da177e4SLinus Torvalds 165686872cb5SThomas Graf ip6_route_add(&cfg); 16571da177e4SLinus Torvalds } 16581da177e4SLinus Torvalds 16591da177e4SLinus Torvalds /* Create "default" multicast route to the interface */ 16601da177e4SLinus Torvalds 16611da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev) 16621da177e4SLinus Torvalds { 166386872cb5SThomas Graf struct fib6_config cfg = { 166486872cb5SThomas Graf .fc_table = RT6_TABLE_LOCAL, 166586872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 166686872cb5SThomas Graf .fc_ifindex = dev->ifindex, 166786872cb5SThomas Graf .fc_dst_len = 8, 166886872cb5SThomas Graf .fc_flags = RTF_UP, 1669c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 167086872cb5SThomas Graf }; 16711da177e4SLinus Torvalds 167286872cb5SThomas Graf ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0); 167386872cb5SThomas Graf 167486872cb5SThomas Graf ip6_route_add(&cfg); 16751da177e4SLinus Torvalds } 16761da177e4SLinus Torvalds 16770be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 16781da177e4SLinus Torvalds static void sit_route_add(struct net_device *dev) 16791da177e4SLinus Torvalds { 168086872cb5SThomas Graf struct fib6_config cfg = { 168186872cb5SThomas Graf .fc_table = RT6_TABLE_MAIN, 168286872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 168386872cb5SThomas Graf .fc_ifindex = dev->ifindex, 168486872cb5SThomas Graf .fc_dst_len = 96, 168586872cb5SThomas Graf .fc_flags = RTF_UP | RTF_NONEXTHOP, 1686c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 168786872cb5SThomas Graf }; 16881da177e4SLinus Torvalds 16891da177e4SLinus Torvalds /* prefix length - 96 bits "::d.d.d.d" */ 169086872cb5SThomas Graf ip6_route_add(&cfg); 16911da177e4SLinus Torvalds } 16920be669bbSJoerg Roedel #endif 16931da177e4SLinus Torvalds 16941da177e4SLinus Torvalds static void addrconf_add_lroute(struct net_device *dev) 16951da177e4SLinus Torvalds { 16961da177e4SLinus Torvalds struct in6_addr addr; 16971da177e4SLinus Torvalds 16981da177e4SLinus Torvalds ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0); 16991da177e4SLinus Torvalds addrconf_prefix_route(&addr, 64, dev, 0, 0); 17001da177e4SLinus Torvalds } 17011da177e4SLinus Torvalds 17021da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev) 17031da177e4SLinus Torvalds { 17041da177e4SLinus Torvalds struct inet6_dev *idev; 17051da177e4SLinus Torvalds 17061da177e4SLinus Torvalds ASSERT_RTNL(); 17071da177e4SLinus Torvalds 17081da177e4SLinus Torvalds if ((idev = ipv6_find_idev(dev)) == NULL) 17091da177e4SLinus Torvalds return NULL; 17101da177e4SLinus Torvalds 17111da177e4SLinus Torvalds /* Add default multicast route */ 17121da177e4SLinus Torvalds addrconf_add_mroute(dev); 17131da177e4SLinus Torvalds 17141da177e4SLinus Torvalds /* Add link local route */ 17151da177e4SLinus Torvalds addrconf_add_lroute(dev); 17161da177e4SLinus Torvalds return idev; 17171da177e4SLinus Torvalds } 17181da177e4SLinus Torvalds 17191da177e4SLinus Torvalds void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len) 17201da177e4SLinus Torvalds { 17211da177e4SLinus Torvalds struct prefix_info *pinfo; 17221da177e4SLinus Torvalds __u32 valid_lft; 17231da177e4SLinus Torvalds __u32 prefered_lft; 17241da177e4SLinus Torvalds int addr_type; 17251da177e4SLinus Torvalds unsigned long rt_expires; 17261da177e4SLinus Torvalds struct inet6_dev *in6_dev; 17271da177e4SLinus Torvalds 17281da177e4SLinus Torvalds pinfo = (struct prefix_info *) opt; 17291da177e4SLinus Torvalds 17301da177e4SLinus Torvalds if (len < sizeof(struct prefix_info)) { 17311da177e4SLinus Torvalds ADBG(("addrconf: prefix option too short\n")); 17321da177e4SLinus Torvalds return; 17331da177e4SLinus Torvalds } 17341da177e4SLinus Torvalds 17351da177e4SLinus Torvalds /* 17361da177e4SLinus Torvalds * Validation checks ([ADDRCONF], page 19) 17371da177e4SLinus Torvalds */ 17381da177e4SLinus Torvalds 17391da177e4SLinus Torvalds addr_type = ipv6_addr_type(&pinfo->prefix); 17401da177e4SLinus Torvalds 17411da177e4SLinus Torvalds if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)) 17421da177e4SLinus Torvalds return; 17431da177e4SLinus Torvalds 17441da177e4SLinus Torvalds valid_lft = ntohl(pinfo->valid); 17451da177e4SLinus Torvalds prefered_lft = ntohl(pinfo->prefered); 17461da177e4SLinus Torvalds 17471da177e4SLinus Torvalds if (prefered_lft > valid_lft) { 17481da177e4SLinus Torvalds if (net_ratelimit()) 17491da177e4SLinus Torvalds printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n"); 17501da177e4SLinus Torvalds return; 17511da177e4SLinus Torvalds } 17521da177e4SLinus Torvalds 17531da177e4SLinus Torvalds in6_dev = in6_dev_get(dev); 17541da177e4SLinus Torvalds 17551da177e4SLinus Torvalds if (in6_dev == NULL) { 17561da177e4SLinus Torvalds if (net_ratelimit()) 17571da177e4SLinus Torvalds printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name); 17581da177e4SLinus Torvalds return; 17591da177e4SLinus Torvalds } 17601da177e4SLinus Torvalds 17611da177e4SLinus Torvalds /* 17621da177e4SLinus Torvalds * Two things going on here: 17631da177e4SLinus Torvalds * 1) Add routes for on-link prefixes 17641da177e4SLinus Torvalds * 2) Configure prefixes with the auto flag set 17651da177e4SLinus Torvalds */ 17661da177e4SLinus Torvalds 17671da177e4SLinus Torvalds /* Avoid arithmetic overflow. Really, we could 17681da177e4SLinus Torvalds save rt_expires in seconds, likely valid_lft, 17691da177e4SLinus Torvalds but it would require division in fib gc, that it 17701da177e4SLinus Torvalds not good. 17711da177e4SLinus Torvalds */ 17721da177e4SLinus Torvalds if (valid_lft >= 0x7FFFFFFF/HZ) 17733dd4bc68SYOSHIFUJI Hideaki rt_expires = 0x7FFFFFFF - (0x7FFFFFFF % HZ); 17741da177e4SLinus Torvalds else 17753dd4bc68SYOSHIFUJI Hideaki rt_expires = valid_lft * HZ; 17763dd4bc68SYOSHIFUJI Hideaki 17773dd4bc68SYOSHIFUJI Hideaki /* 17783dd4bc68SYOSHIFUJI Hideaki * We convert this (in jiffies) to clock_t later. 17793dd4bc68SYOSHIFUJI Hideaki * Avoid arithmetic overflow there as well. 17803dd4bc68SYOSHIFUJI Hideaki * Overflow can happen only if HZ < USER_HZ. 17813dd4bc68SYOSHIFUJI Hideaki */ 17823dd4bc68SYOSHIFUJI Hideaki if (HZ < USER_HZ && rt_expires > 0x7FFFFFFF / USER_HZ) 17833dd4bc68SYOSHIFUJI Hideaki rt_expires = 0x7FFFFFFF / USER_HZ; 17841da177e4SLinus Torvalds 17851da177e4SLinus Torvalds if (pinfo->onlink) { 17861da177e4SLinus Torvalds struct rt6_info *rt; 1787c346dca1SYOSHIFUJI Hideaki rt = rt6_lookup(dev_net(dev), &pinfo->prefix, NULL, 17886fda7350SBenjamin Thery dev->ifindex, 1); 17891da177e4SLinus Torvalds 17901da177e4SLinus Torvalds if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) { 17911da177e4SLinus Torvalds if (rt->rt6i_flags&RTF_EXPIRES) { 17921da177e4SLinus Torvalds if (valid_lft == 0) { 1793e0a1ad73SThomas Graf ip6_del_rt(rt); 17941da177e4SLinus Torvalds rt = NULL; 17951da177e4SLinus Torvalds } else { 17963dd4bc68SYOSHIFUJI Hideaki rt->rt6i_expires = jiffies + rt_expires; 17971da177e4SLinus Torvalds } 17981da177e4SLinus Torvalds } 17991da177e4SLinus Torvalds } else if (valid_lft) { 18001da177e4SLinus Torvalds addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len, 18013dd4bc68SYOSHIFUJI Hideaki dev, jiffies_to_clock_t(rt_expires), RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT); 18021da177e4SLinus Torvalds } 18031da177e4SLinus Torvalds if (rt) 18041da177e4SLinus Torvalds dst_release(&rt->u.dst); 18051da177e4SLinus Torvalds } 18061da177e4SLinus Torvalds 18071da177e4SLinus Torvalds /* Try to figure out our local address for this prefix */ 18081da177e4SLinus Torvalds 18091da177e4SLinus Torvalds if (pinfo->autoconf && in6_dev->cnf.autoconf) { 18101da177e4SLinus Torvalds struct inet6_ifaddr * ifp; 18111da177e4SLinus Torvalds struct in6_addr addr; 18121da177e4SLinus Torvalds int create = 0, update_lft = 0; 18131da177e4SLinus Torvalds 18141da177e4SLinus Torvalds if (pinfo->prefix_len == 64) { 18151da177e4SLinus Torvalds memcpy(&addr, &pinfo->prefix, 8); 18161da177e4SLinus Torvalds if (ipv6_generate_eui64(addr.s6_addr + 8, dev) && 18171da177e4SLinus Torvalds ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) { 18181da177e4SLinus Torvalds in6_dev_put(in6_dev); 18191da177e4SLinus Torvalds return; 18201da177e4SLinus Torvalds } 18211da177e4SLinus Torvalds goto ok; 18221da177e4SLinus Torvalds } 18231da177e4SLinus Torvalds if (net_ratelimit()) 18241da177e4SLinus Torvalds printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n", 18251da177e4SLinus Torvalds pinfo->prefix_len); 18261da177e4SLinus Torvalds in6_dev_put(in6_dev); 18271da177e4SLinus Torvalds return; 18281da177e4SLinus Torvalds 18291da177e4SLinus Torvalds ok: 18301da177e4SLinus Torvalds 1831c346dca1SYOSHIFUJI Hideaki ifp = ipv6_get_ifaddr(dev_net(dev), &addr, dev, 1); 18321da177e4SLinus Torvalds 18331da177e4SLinus Torvalds if (ifp == NULL && valid_lft) { 18341da177e4SLinus Torvalds int max_addresses = in6_dev->cnf.max_addresses; 183595c385b4SNeil Horman u32 addr_flags = 0; 183695c385b4SNeil Horman 183795c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 183895c385b4SNeil Horman if (in6_dev->cnf.optimistic_dad && 183995c385b4SNeil Horman !ipv6_devconf.forwarding) 184095c385b4SNeil Horman addr_flags = IFA_F_OPTIMISTIC; 184195c385b4SNeil Horman #endif 18421da177e4SLinus Torvalds 18431da177e4SLinus Torvalds /* Do not allow to create too much of autoconfigured 18441da177e4SLinus Torvalds * addresses; this would be too easy way to crash kernel. 18451da177e4SLinus Torvalds */ 18461da177e4SLinus Torvalds if (!max_addresses || 18471da177e4SLinus Torvalds ipv6_count_addresses(in6_dev) < max_addresses) 18481da177e4SLinus Torvalds ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len, 184995c385b4SNeil Horman addr_type&IPV6_ADDR_SCOPE_MASK, 185095c385b4SNeil Horman addr_flags); 18511da177e4SLinus Torvalds 18521da177e4SLinus Torvalds if (!ifp || IS_ERR(ifp)) { 18531da177e4SLinus Torvalds in6_dev_put(in6_dev); 18541da177e4SLinus Torvalds return; 18551da177e4SLinus Torvalds } 18561da177e4SLinus Torvalds 18571da177e4SLinus Torvalds update_lft = create = 1; 18581da177e4SLinus Torvalds ifp->cstamp = jiffies; 18591da177e4SLinus Torvalds addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT); 18601da177e4SLinus Torvalds } 18611da177e4SLinus Torvalds 18621da177e4SLinus Torvalds if (ifp) { 18631da177e4SLinus Torvalds int flags; 18641da177e4SLinus Torvalds unsigned long now; 18651da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 18661da177e4SLinus Torvalds struct inet6_ifaddr *ift; 18671da177e4SLinus Torvalds #endif 18681da177e4SLinus Torvalds u32 stored_lft; 18691da177e4SLinus Torvalds 18701da177e4SLinus Torvalds /* update lifetime (RFC2462 5.5.3 e) */ 18711da177e4SLinus Torvalds spin_lock(&ifp->lock); 18721da177e4SLinus Torvalds now = jiffies; 18731da177e4SLinus Torvalds if (ifp->valid_lft > (now - ifp->tstamp) / HZ) 18741da177e4SLinus Torvalds stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ; 18751da177e4SLinus Torvalds else 18761da177e4SLinus Torvalds stored_lft = 0; 18771da177e4SLinus Torvalds if (!update_lft && stored_lft) { 18781da177e4SLinus Torvalds if (valid_lft > MIN_VALID_LIFETIME || 18791da177e4SLinus Torvalds valid_lft > stored_lft) 18801da177e4SLinus Torvalds update_lft = 1; 18811da177e4SLinus Torvalds else if (stored_lft <= MIN_VALID_LIFETIME) { 18821da177e4SLinus Torvalds /* valid_lft <= stored_lft is always true */ 18831da177e4SLinus Torvalds /* XXX: IPsec */ 18841da177e4SLinus Torvalds update_lft = 0; 18851da177e4SLinus Torvalds } else { 18861da177e4SLinus Torvalds valid_lft = MIN_VALID_LIFETIME; 18871da177e4SLinus Torvalds if (valid_lft < prefered_lft) 18881da177e4SLinus Torvalds prefered_lft = valid_lft; 18891da177e4SLinus Torvalds update_lft = 1; 18901da177e4SLinus Torvalds } 18911da177e4SLinus Torvalds } 18921da177e4SLinus Torvalds 18931da177e4SLinus Torvalds if (update_lft) { 18941da177e4SLinus Torvalds ifp->valid_lft = valid_lft; 18951da177e4SLinus Torvalds ifp->prefered_lft = prefered_lft; 18961da177e4SLinus Torvalds ifp->tstamp = now; 18971da177e4SLinus Torvalds flags = ifp->flags; 18981da177e4SLinus Torvalds ifp->flags &= ~IFA_F_DEPRECATED; 18991da177e4SLinus Torvalds spin_unlock(&ifp->lock); 19001da177e4SLinus Torvalds 19011da177e4SLinus Torvalds if (!(flags&IFA_F_TENTATIVE)) 19021da177e4SLinus Torvalds ipv6_ifa_notify(0, ifp); 19031da177e4SLinus Torvalds } else 19041da177e4SLinus Torvalds spin_unlock(&ifp->lock); 19051da177e4SLinus Torvalds 19061da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 19071da177e4SLinus Torvalds read_lock_bh(&in6_dev->lock); 19081da177e4SLinus Torvalds /* update all temporary addresses in the list */ 19091da177e4SLinus Torvalds for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) { 19101da177e4SLinus Torvalds /* 19111da177e4SLinus Torvalds * When adjusting the lifetimes of an existing 19121da177e4SLinus Torvalds * temporary address, only lower the lifetimes. 19131da177e4SLinus Torvalds * Implementations must not increase the 19141da177e4SLinus Torvalds * lifetimes of an existing temporary address 19151da177e4SLinus Torvalds * when processing a Prefix Information Option. 19161da177e4SLinus Torvalds */ 1917c6fbfac2SBenoit Boissinot if (ifp != ift->ifpub) 1918c6fbfac2SBenoit Boissinot continue; 1919c6fbfac2SBenoit Boissinot 19201da177e4SLinus Torvalds spin_lock(&ift->lock); 19211da177e4SLinus Torvalds flags = ift->flags; 19221da177e4SLinus Torvalds if (ift->valid_lft > valid_lft && 19231da177e4SLinus Torvalds ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ) 19241da177e4SLinus Torvalds ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ; 19251da177e4SLinus Torvalds if (ift->prefered_lft > prefered_lft && 19261da177e4SLinus Torvalds ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ) 19271da177e4SLinus Torvalds ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ; 19281da177e4SLinus Torvalds spin_unlock(&ift->lock); 19291da177e4SLinus Torvalds if (!(flags&IFA_F_TENTATIVE)) 19301da177e4SLinus Torvalds ipv6_ifa_notify(0, ift); 19311da177e4SLinus Torvalds } 19321da177e4SLinus Torvalds 19331da177e4SLinus Torvalds if (create && in6_dev->cnf.use_tempaddr > 0) { 19341da177e4SLinus Torvalds /* 19351da177e4SLinus Torvalds * When a new public address is created as described in [ADDRCONF], 19361da177e4SLinus Torvalds * also create a new temporary address. 19371da177e4SLinus Torvalds */ 19381da177e4SLinus Torvalds read_unlock_bh(&in6_dev->lock); 19391da177e4SLinus Torvalds ipv6_create_tempaddr(ifp, NULL); 19401da177e4SLinus Torvalds } else { 19411da177e4SLinus Torvalds read_unlock_bh(&in6_dev->lock); 19421da177e4SLinus Torvalds } 19431da177e4SLinus Torvalds #endif 19441da177e4SLinus Torvalds in6_ifa_put(ifp); 19451da177e4SLinus Torvalds addrconf_verify(0); 19461da177e4SLinus Torvalds } 19471da177e4SLinus Torvalds } 19481da177e4SLinus Torvalds inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo); 19491da177e4SLinus Torvalds in6_dev_put(in6_dev); 19501da177e4SLinus Torvalds } 19511da177e4SLinus Torvalds 19521da177e4SLinus Torvalds /* 19531da177e4SLinus Torvalds * Set destination address. 19541da177e4SLinus Torvalds * Special case for SIT interfaces where we create a new "virtual" 19551da177e4SLinus Torvalds * device. 19561da177e4SLinus Torvalds */ 1957af284937SDaniel Lezcano int addrconf_set_dstaddr(struct net *net, void __user *arg) 19581da177e4SLinus Torvalds { 19591da177e4SLinus Torvalds struct in6_ifreq ireq; 19601da177e4SLinus Torvalds struct net_device *dev; 19611da177e4SLinus Torvalds int err = -EINVAL; 19621da177e4SLinus Torvalds 19631da177e4SLinus Torvalds rtnl_lock(); 19641da177e4SLinus Torvalds 19651da177e4SLinus Torvalds err = -EFAULT; 19661da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 19671da177e4SLinus Torvalds goto err_exit; 19681da177e4SLinus Torvalds 1969af284937SDaniel Lezcano dev = __dev_get_by_index(net, ireq.ifr6_ifindex); 19701da177e4SLinus Torvalds 19711da177e4SLinus Torvalds err = -ENODEV; 19721da177e4SLinus Torvalds if (dev == NULL) 19731da177e4SLinus Torvalds goto err_exit; 19741da177e4SLinus Torvalds 19750be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 19761da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT) { 19771da177e4SLinus Torvalds struct ifreq ifr; 19781da177e4SLinus Torvalds mm_segment_t oldfs; 19791da177e4SLinus Torvalds struct ip_tunnel_parm p; 19801da177e4SLinus Torvalds 19811da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 19821da177e4SLinus Torvalds if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4)) 19831da177e4SLinus Torvalds goto err_exit; 19841da177e4SLinus Torvalds 19851da177e4SLinus Torvalds memset(&p, 0, sizeof(p)); 19861da177e4SLinus Torvalds p.iph.daddr = ireq.ifr6_addr.s6_addr32[3]; 19871da177e4SLinus Torvalds p.iph.saddr = 0; 19881da177e4SLinus Torvalds p.iph.version = 4; 19891da177e4SLinus Torvalds p.iph.ihl = 5; 19901da177e4SLinus Torvalds p.iph.protocol = IPPROTO_IPV6; 19911da177e4SLinus Torvalds p.iph.ttl = 64; 1992d20b3109SStephen Hemminger ifr.ifr_ifru.ifru_data = (__force void __user *)&p; 19931da177e4SLinus Torvalds 19941da177e4SLinus Torvalds oldfs = get_fs(); set_fs(KERNEL_DS); 19951da177e4SLinus Torvalds err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL); 19961da177e4SLinus Torvalds set_fs(oldfs); 19971da177e4SLinus Torvalds 19981da177e4SLinus Torvalds if (err == 0) { 19991da177e4SLinus Torvalds err = -ENOBUFS; 2000af284937SDaniel Lezcano dev = __dev_get_by_name(net, p.name); 2001af284937SDaniel Lezcano if (!dev) 20021da177e4SLinus Torvalds goto err_exit; 20031da177e4SLinus Torvalds err = dev_open(dev); 20041da177e4SLinus Torvalds } 20051da177e4SLinus Torvalds } 20060be669bbSJoerg Roedel #endif 20071da177e4SLinus Torvalds 20081da177e4SLinus Torvalds err_exit: 20091da177e4SLinus Torvalds rtnl_unlock(); 20101da177e4SLinus Torvalds return err; 20111da177e4SLinus Torvalds } 20121da177e4SLinus Torvalds 20131da177e4SLinus Torvalds /* 20141da177e4SLinus Torvalds * Manual configuration of address on an interface 20151da177e4SLinus Torvalds */ 2016af284937SDaniel Lezcano static int inet6_addr_add(struct net *net, int ifindex, struct in6_addr *pfx, 2017af284937SDaniel Lezcano int plen, __u8 ifa_flags, __u32 prefered_lft, 2018af284937SDaniel Lezcano __u32 valid_lft) 20191da177e4SLinus Torvalds { 20201da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 20211da177e4SLinus Torvalds struct inet6_dev *idev; 20221da177e4SLinus Torvalds struct net_device *dev; 20231da177e4SLinus Torvalds int scope; 202446d48046SYOSHIFUJI Hideaki u32 flags = RTF_EXPIRES; 20251da177e4SLinus Torvalds 20261da177e4SLinus Torvalds ASSERT_RTNL(); 20271da177e4SLinus Torvalds 20280778769dSNoriaki TAKAMIYA /* check the lifetime */ 20290778769dSNoriaki TAKAMIYA if (!valid_lft || prefered_lft > valid_lft) 20300778769dSNoriaki TAKAMIYA return -EINVAL; 20310778769dSNoriaki TAKAMIYA 2032af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifindex); 2033af284937SDaniel Lezcano if (!dev) 20341da177e4SLinus Torvalds return -ENODEV; 20351da177e4SLinus Torvalds 20361da177e4SLinus Torvalds if ((idev = addrconf_add_dev(dev)) == NULL) 20371da177e4SLinus Torvalds return -ENOBUFS; 20381da177e4SLinus Torvalds 20391da177e4SLinus Torvalds scope = ipv6_addr_scope(pfx); 20401da177e4SLinus Torvalds 204146d48046SYOSHIFUJI Hideaki if (valid_lft == INFINITY_LIFE_TIME) { 20420778769dSNoriaki TAKAMIYA ifa_flags |= IFA_F_PERMANENT; 204346d48046SYOSHIFUJI Hideaki flags = 0; 204446d48046SYOSHIFUJI Hideaki } else if (valid_lft >= 0x7FFFFFFF/HZ) 20450778769dSNoriaki TAKAMIYA valid_lft = 0x7FFFFFFF/HZ; 20460778769dSNoriaki TAKAMIYA 20470778769dSNoriaki TAKAMIYA if (prefered_lft == 0) 20480778769dSNoriaki TAKAMIYA ifa_flags |= IFA_F_DEPRECATED; 20490778769dSNoriaki TAKAMIYA else if ((prefered_lft >= 0x7FFFFFFF/HZ) && 20500778769dSNoriaki TAKAMIYA (prefered_lft != INFINITY_LIFE_TIME)) 20510778769dSNoriaki TAKAMIYA prefered_lft = 0x7FFFFFFF/HZ; 20520778769dSNoriaki TAKAMIYA 20530778769dSNoriaki TAKAMIYA ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags); 20540778769dSNoriaki TAKAMIYA 20551da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 205606aebfb7SHerbert Xu spin_lock_bh(&ifp->lock); 20570778769dSNoriaki TAKAMIYA ifp->valid_lft = valid_lft; 20580778769dSNoriaki TAKAMIYA ifp->prefered_lft = prefered_lft; 20590778769dSNoriaki TAKAMIYA ifp->tstamp = jiffies; 206006aebfb7SHerbert Xu spin_unlock_bh(&ifp->lock); 20610778769dSNoriaki TAKAMIYA 206246d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 206346d48046SYOSHIFUJI Hideaki jiffies_to_clock_t(valid_lft * HZ), flags); 206495c385b4SNeil Horman /* 206595c385b4SNeil Horman * Note that section 3.1 of RFC 4429 indicates 206695c385b4SNeil Horman * that the Optimistic flag should not be set for 206795c385b4SNeil Horman * manually configured addresses 206895c385b4SNeil Horman */ 20691da177e4SLinus Torvalds addrconf_dad_start(ifp, 0); 20701da177e4SLinus Torvalds in6_ifa_put(ifp); 20710778769dSNoriaki TAKAMIYA addrconf_verify(0); 20721da177e4SLinus Torvalds return 0; 20731da177e4SLinus Torvalds } 20741da177e4SLinus Torvalds 20751da177e4SLinus Torvalds return PTR_ERR(ifp); 20761da177e4SLinus Torvalds } 20771da177e4SLinus Torvalds 2078af284937SDaniel Lezcano static int inet6_addr_del(struct net *net, int ifindex, struct in6_addr *pfx, 2079af284937SDaniel Lezcano int plen) 20801da177e4SLinus Torvalds { 20811da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 20821da177e4SLinus Torvalds struct inet6_dev *idev; 20831da177e4SLinus Torvalds struct net_device *dev; 20841da177e4SLinus Torvalds 2085af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifindex); 2086af284937SDaniel Lezcano if (!dev) 20871da177e4SLinus Torvalds return -ENODEV; 20881da177e4SLinus Torvalds 20891da177e4SLinus Torvalds if ((idev = __in6_dev_get(dev)) == NULL) 20901da177e4SLinus Torvalds return -ENXIO; 20911da177e4SLinus Torvalds 20921da177e4SLinus Torvalds read_lock_bh(&idev->lock); 20931da177e4SLinus Torvalds for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) { 20941da177e4SLinus Torvalds if (ifp->prefix_len == plen && 20951da177e4SLinus Torvalds ipv6_addr_equal(pfx, &ifp->addr)) { 20961da177e4SLinus Torvalds in6_ifa_hold(ifp); 20971da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 20981da177e4SLinus Torvalds 20991da177e4SLinus Torvalds ipv6_del_addr(ifp); 21001da177e4SLinus Torvalds 21011da177e4SLinus Torvalds /* If the last address is deleted administratively, 21021da177e4SLinus Torvalds disable IPv6 on this interface. 21031da177e4SLinus Torvalds */ 21041da177e4SLinus Torvalds if (idev->addr_list == NULL) 21051da177e4SLinus Torvalds addrconf_ifdown(idev->dev, 1); 21061da177e4SLinus Torvalds return 0; 21071da177e4SLinus Torvalds } 21081da177e4SLinus Torvalds } 21091da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 21101da177e4SLinus Torvalds return -EADDRNOTAVAIL; 21111da177e4SLinus Torvalds } 21121da177e4SLinus Torvalds 21131da177e4SLinus Torvalds 2114af284937SDaniel Lezcano int addrconf_add_ifaddr(struct net *net, void __user *arg) 21151da177e4SLinus Torvalds { 21161da177e4SLinus Torvalds struct in6_ifreq ireq; 21171da177e4SLinus Torvalds int err; 21181da177e4SLinus Torvalds 21191da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 21201da177e4SLinus Torvalds return -EPERM; 21211da177e4SLinus Torvalds 21221da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 21231da177e4SLinus Torvalds return -EFAULT; 21241da177e4SLinus Torvalds 21251da177e4SLinus Torvalds rtnl_lock(); 2126af284937SDaniel Lezcano err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, 2127af284937SDaniel Lezcano ireq.ifr6_prefixlen, IFA_F_PERMANENT, 2128af284937SDaniel Lezcano INFINITY_LIFE_TIME, INFINITY_LIFE_TIME); 21291da177e4SLinus Torvalds rtnl_unlock(); 21301da177e4SLinus Torvalds return err; 21311da177e4SLinus Torvalds } 21321da177e4SLinus Torvalds 2133af284937SDaniel Lezcano int addrconf_del_ifaddr(struct net *net, void __user *arg) 21341da177e4SLinus Torvalds { 21351da177e4SLinus Torvalds struct in6_ifreq ireq; 21361da177e4SLinus Torvalds int err; 21371da177e4SLinus Torvalds 21381da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 21391da177e4SLinus Torvalds return -EPERM; 21401da177e4SLinus Torvalds 21411da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 21421da177e4SLinus Torvalds return -EFAULT; 21431da177e4SLinus Torvalds 21441da177e4SLinus Torvalds rtnl_lock(); 2145af284937SDaniel Lezcano err = inet6_addr_del(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, 2146af284937SDaniel Lezcano ireq.ifr6_prefixlen); 21471da177e4SLinus Torvalds rtnl_unlock(); 21481da177e4SLinus Torvalds return err; 21491da177e4SLinus Torvalds } 21501da177e4SLinus Torvalds 21510be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 21521da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev) 21531da177e4SLinus Torvalds { 21541da177e4SLinus Torvalds struct inet6_ifaddr * ifp; 21551da177e4SLinus Torvalds struct in6_addr addr; 21561da177e4SLinus Torvalds struct net_device *dev; 2157c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 21581da177e4SLinus Torvalds int scope; 21591da177e4SLinus Torvalds 21601da177e4SLinus Torvalds ASSERT_RTNL(); 21611da177e4SLinus Torvalds 21621da177e4SLinus Torvalds memset(&addr, 0, sizeof(struct in6_addr)); 21631da177e4SLinus Torvalds memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4); 21641da177e4SLinus Torvalds 21651da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) { 21661da177e4SLinus Torvalds addr.s6_addr32[0] = htonl(0xfe800000); 21671da177e4SLinus Torvalds scope = IFA_LINK; 21681da177e4SLinus Torvalds } else { 21691da177e4SLinus Torvalds scope = IPV6_ADDR_COMPATv4; 21701da177e4SLinus Torvalds } 21711da177e4SLinus Torvalds 21721da177e4SLinus Torvalds if (addr.s6_addr32[3]) { 21731da177e4SLinus Torvalds ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT); 21741da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 21751da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 21761da177e4SLinus Torvalds ifp->flags &= ~IFA_F_TENTATIVE; 21771da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 21781da177e4SLinus Torvalds ipv6_ifa_notify(RTM_NEWADDR, ifp); 21791da177e4SLinus Torvalds in6_ifa_put(ifp); 21801da177e4SLinus Torvalds } 21811da177e4SLinus Torvalds return; 21821da177e4SLinus Torvalds } 21831da177e4SLinus Torvalds 21846fda7350SBenjamin Thery for_each_netdev(net, dev) { 2185e5ed6399SHerbert Xu struct in_device * in_dev = __in_dev_get_rtnl(dev); 21861da177e4SLinus Torvalds if (in_dev && (dev->flags & IFF_UP)) { 21871da177e4SLinus Torvalds struct in_ifaddr * ifa; 21881da177e4SLinus Torvalds 21891da177e4SLinus Torvalds int flag = scope; 21901da177e4SLinus Torvalds 21911da177e4SLinus Torvalds for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) { 21921da177e4SLinus Torvalds int plen; 21931da177e4SLinus Torvalds 21941da177e4SLinus Torvalds addr.s6_addr32[3] = ifa->ifa_local; 21951da177e4SLinus Torvalds 21961da177e4SLinus Torvalds if (ifa->ifa_scope == RT_SCOPE_LINK) 21971da177e4SLinus Torvalds continue; 21981da177e4SLinus Torvalds if (ifa->ifa_scope >= RT_SCOPE_HOST) { 21991da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) 22001da177e4SLinus Torvalds continue; 22011da177e4SLinus Torvalds flag |= IFA_HOST; 22021da177e4SLinus Torvalds } 22031da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) 22041da177e4SLinus Torvalds plen = 64; 22051da177e4SLinus Torvalds else 22061da177e4SLinus Torvalds plen = 96; 22071da177e4SLinus Torvalds 22081da177e4SLinus Torvalds ifp = ipv6_add_addr(idev, &addr, plen, flag, 22091da177e4SLinus Torvalds IFA_F_PERMANENT); 22101da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 22111da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 22121da177e4SLinus Torvalds ifp->flags &= ~IFA_F_TENTATIVE; 22131da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 22141da177e4SLinus Torvalds ipv6_ifa_notify(RTM_NEWADDR, ifp); 22151da177e4SLinus Torvalds in6_ifa_put(ifp); 22161da177e4SLinus Torvalds } 22171da177e4SLinus Torvalds } 22181da177e4SLinus Torvalds } 22191da177e4SLinus Torvalds } 22201da177e4SLinus Torvalds } 22210be669bbSJoerg Roedel #endif 22221da177e4SLinus Torvalds 22231da177e4SLinus Torvalds static void init_loopback(struct net_device *dev) 22241da177e4SLinus Torvalds { 22251da177e4SLinus Torvalds struct inet6_dev *idev; 22261da177e4SLinus Torvalds struct inet6_ifaddr * ifp; 22271da177e4SLinus Torvalds 22281da177e4SLinus Torvalds /* ::1 */ 22291da177e4SLinus Torvalds 22301da177e4SLinus Torvalds ASSERT_RTNL(); 22311da177e4SLinus Torvalds 22321da177e4SLinus Torvalds if ((idev = ipv6_find_idev(dev)) == NULL) { 22331da177e4SLinus Torvalds printk(KERN_DEBUG "init loopback: add_dev failed\n"); 22341da177e4SLinus Torvalds return; 22351da177e4SLinus Torvalds } 22361da177e4SLinus Torvalds 22371da177e4SLinus Torvalds ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT); 22381da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 22391da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 22401da177e4SLinus Torvalds ifp->flags &= ~IFA_F_TENTATIVE; 22411da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 22421da177e4SLinus Torvalds ipv6_ifa_notify(RTM_NEWADDR, ifp); 22431da177e4SLinus Torvalds in6_ifa_put(ifp); 22441da177e4SLinus Torvalds } 22451da177e4SLinus Torvalds } 22461da177e4SLinus Torvalds 22471da177e4SLinus Torvalds static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr) 22481da177e4SLinus Torvalds { 22491da177e4SLinus Torvalds struct inet6_ifaddr * ifp; 225095c385b4SNeil Horman u32 addr_flags = IFA_F_PERMANENT; 22511da177e4SLinus Torvalds 225295c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 225395c385b4SNeil Horman if (idev->cnf.optimistic_dad && 225495c385b4SNeil Horman !ipv6_devconf.forwarding) 225595c385b4SNeil Horman addr_flags |= IFA_F_OPTIMISTIC; 225695c385b4SNeil Horman #endif 225795c385b4SNeil Horman 225895c385b4SNeil Horman 225995c385b4SNeil Horman ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, addr_flags); 22601da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 226146d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0); 22621da177e4SLinus Torvalds addrconf_dad_start(ifp, 0); 22631da177e4SLinus Torvalds in6_ifa_put(ifp); 22641da177e4SLinus Torvalds } 22651da177e4SLinus Torvalds } 22661da177e4SLinus Torvalds 22671da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev) 22681da177e4SLinus Torvalds { 22691da177e4SLinus Torvalds struct in6_addr addr; 22701da177e4SLinus Torvalds struct inet6_dev * idev; 22711da177e4SLinus Torvalds 22721da177e4SLinus Torvalds ASSERT_RTNL(); 22731da177e4SLinus Torvalds 227474235a25SHerbert Xu if ((dev->type != ARPHRD_ETHER) && 227574235a25SHerbert Xu (dev->type != ARPHRD_FDDI) && 227674235a25SHerbert Xu (dev->type != ARPHRD_IEEE802_TR) && 227774235a25SHerbert Xu (dev->type != ARPHRD_ARCNET) && 227874235a25SHerbert Xu (dev->type != ARPHRD_INFINIBAND)) { 227974235a25SHerbert Xu /* Alas, we support only Ethernet autoconfiguration. */ 228074235a25SHerbert Xu return; 228174235a25SHerbert Xu } 228274235a25SHerbert Xu 22831da177e4SLinus Torvalds idev = addrconf_add_dev(dev); 22841da177e4SLinus Torvalds if (idev == NULL) 22851da177e4SLinus Torvalds return; 22861da177e4SLinus Torvalds 22871da177e4SLinus Torvalds memset(&addr, 0, sizeof(struct in6_addr)); 22881da177e4SLinus Torvalds addr.s6_addr32[0] = htonl(0xFE800000); 22891da177e4SLinus Torvalds 22901da177e4SLinus Torvalds if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0) 22911da177e4SLinus Torvalds addrconf_add_linklocal(idev, &addr); 22921da177e4SLinus Torvalds } 22931da177e4SLinus Torvalds 22940be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 22951da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev) 22961da177e4SLinus Torvalds { 22971da177e4SLinus Torvalds struct inet6_dev *idev; 22981da177e4SLinus Torvalds 22991da177e4SLinus Torvalds ASSERT_RTNL(); 23001da177e4SLinus Torvalds 23011da177e4SLinus Torvalds /* 23021da177e4SLinus Torvalds * Configure the tunnel with one of our IPv4 23031da177e4SLinus Torvalds * addresses... we should configure all of 23041da177e4SLinus Torvalds * our v4 addrs in the tunnel 23051da177e4SLinus Torvalds */ 23061da177e4SLinus Torvalds 23071da177e4SLinus Torvalds if ((idev = ipv6_find_idev(dev)) == NULL) { 23081da177e4SLinus Torvalds printk(KERN_DEBUG "init sit: add_dev failed\n"); 23091da177e4SLinus Torvalds return; 23101da177e4SLinus Torvalds } 23111da177e4SLinus Torvalds 2312c7dc89c0SFred L. Templin if (dev->priv_flags & IFF_ISATAP) { 2313c7dc89c0SFred L. Templin struct in6_addr addr; 2314c7dc89c0SFred L. Templin 2315c7dc89c0SFred L. Templin ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0); 2316c7dc89c0SFred L. Templin addrconf_prefix_route(&addr, 64, dev, 0, 0); 2317c7dc89c0SFred L. Templin if (!ipv6_generate_eui64(addr.s6_addr + 8, dev)) 2318c7dc89c0SFred L. Templin addrconf_add_linklocal(idev, &addr); 2319c7dc89c0SFred L. Templin return; 2320c7dc89c0SFred L. Templin } 2321c7dc89c0SFred L. Templin 23221da177e4SLinus Torvalds sit_add_v4_addrs(idev); 23231da177e4SLinus Torvalds 23241da177e4SLinus Torvalds if (dev->flags&IFF_POINTOPOINT) { 23251da177e4SLinus Torvalds addrconf_add_mroute(dev); 23261da177e4SLinus Torvalds addrconf_add_lroute(dev); 23271da177e4SLinus Torvalds } else 23281da177e4SLinus Torvalds sit_route_add(dev); 23291da177e4SLinus Torvalds } 23300be669bbSJoerg Roedel #endif 23311da177e4SLinus Torvalds 23321da177e4SLinus Torvalds static inline int 23331da177e4SLinus Torvalds ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev) 23341da177e4SLinus Torvalds { 23351da177e4SLinus Torvalds struct in6_addr lladdr; 23361da177e4SLinus Torvalds 233795c385b4SNeil Horman if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) { 23381da177e4SLinus Torvalds addrconf_add_linklocal(idev, &lladdr); 23391da177e4SLinus Torvalds return 0; 23401da177e4SLinus Torvalds } 23411da177e4SLinus Torvalds return -1; 23421da177e4SLinus Torvalds } 23431da177e4SLinus Torvalds 23441da177e4SLinus Torvalds static void ip6_tnl_add_linklocal(struct inet6_dev *idev) 23451da177e4SLinus Torvalds { 23461da177e4SLinus Torvalds struct net_device *link_dev; 2347c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 23481da177e4SLinus Torvalds 23491da177e4SLinus Torvalds /* first try to inherit the link-local address from the link device */ 23501da177e4SLinus Torvalds if (idev->dev->iflink && 23516fda7350SBenjamin Thery (link_dev = __dev_get_by_index(net, idev->dev->iflink))) { 23521da177e4SLinus Torvalds if (!ipv6_inherit_linklocal(idev, link_dev)) 23531da177e4SLinus Torvalds return; 23541da177e4SLinus Torvalds } 23551da177e4SLinus Torvalds /* then try to inherit it from any device */ 23566fda7350SBenjamin Thery for_each_netdev(net, link_dev) { 23571da177e4SLinus Torvalds if (!ipv6_inherit_linklocal(idev, link_dev)) 23581da177e4SLinus Torvalds return; 23591da177e4SLinus Torvalds } 23601da177e4SLinus Torvalds printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n"); 23611da177e4SLinus Torvalds } 23621da177e4SLinus Torvalds 23631da177e4SLinus Torvalds /* 23641da177e4SLinus Torvalds * Autoconfigure tunnel with a link-local address so routing protocols, 23651da177e4SLinus Torvalds * DHCPv6, MLD etc. can be run over the virtual link 23661da177e4SLinus Torvalds */ 23671da177e4SLinus Torvalds 23681da177e4SLinus Torvalds static void addrconf_ip6_tnl_config(struct net_device *dev) 23691da177e4SLinus Torvalds { 23701da177e4SLinus Torvalds struct inet6_dev *idev; 23711da177e4SLinus Torvalds 23721da177e4SLinus Torvalds ASSERT_RTNL(); 23731da177e4SLinus Torvalds 23741da177e4SLinus Torvalds if ((idev = addrconf_add_dev(dev)) == NULL) { 23751da177e4SLinus Torvalds printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n"); 23761da177e4SLinus Torvalds return; 23771da177e4SLinus Torvalds } 23781da177e4SLinus Torvalds ip6_tnl_add_linklocal(idev); 23791da177e4SLinus Torvalds } 23801da177e4SLinus Torvalds 23811da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event, 23821da177e4SLinus Torvalds void * data) 23831da177e4SLinus Torvalds { 23841da177e4SLinus Torvalds struct net_device *dev = (struct net_device *) data; 238574235a25SHerbert Xu struct inet6_dev *idev = __in6_dev_get(dev); 2386c5e33bddSYOSHIFUJI Hideaki int run_pending = 0; 2387b217d616SHerbert Xu int err; 23881da177e4SLinus Torvalds 23891da177e4SLinus Torvalds switch(event) { 239045ba9dd2SYOSHIFUJI Hideaki case NETDEV_REGISTER: 239174235a25SHerbert Xu if (!idev && dev->mtu >= IPV6_MIN_MTU) { 239245ba9dd2SYOSHIFUJI Hideaki idev = ipv6_add_dev(dev); 239345ba9dd2SYOSHIFUJI Hideaki if (!idev) 2394b217d616SHerbert Xu return notifier_from_errno(-ENOMEM); 239545ba9dd2SYOSHIFUJI Hideaki } 239645ba9dd2SYOSHIFUJI Hideaki break; 23971da177e4SLinus Torvalds case NETDEV_UP: 23983c21edbdSYOSHIFUJI Hideaki case NETDEV_CHANGE: 2399c2edacf8SJay Vosburgh if (dev->flags & IFF_SLAVE) 2400c2edacf8SJay Vosburgh break; 2401c2edacf8SJay Vosburgh 24023c21edbdSYOSHIFUJI Hideaki if (event == NETDEV_UP) { 2403f24e3d65SMitsuru Chinen if (!addrconf_qdisc_ok(dev)) { 24043c21edbdSYOSHIFUJI Hideaki /* device is not ready yet. */ 24053c21edbdSYOSHIFUJI Hideaki printk(KERN_INFO 24063c21edbdSYOSHIFUJI Hideaki "ADDRCONF(NETDEV_UP): %s: " 24073c21edbdSYOSHIFUJI Hideaki "link is not ready\n", 24083c21edbdSYOSHIFUJI Hideaki dev->name); 24093c21edbdSYOSHIFUJI Hideaki break; 24103c21edbdSYOSHIFUJI Hideaki } 241199081049SKristian Slavov 2412d31c7b8fSEvgeniy Polyakov if (!idev && dev->mtu >= IPV6_MIN_MTU) 2413d31c7b8fSEvgeniy Polyakov idev = ipv6_add_dev(dev); 2414d31c7b8fSEvgeniy Polyakov 241599081049SKristian Slavov if (idev) 241699081049SKristian Slavov idev->if_flags |= IF_READY; 24173c21edbdSYOSHIFUJI Hideaki } else { 2418f24e3d65SMitsuru Chinen if (!addrconf_qdisc_ok(dev)) { 24193c21edbdSYOSHIFUJI Hideaki /* device is still not ready. */ 24203c21edbdSYOSHIFUJI Hideaki break; 24213c21edbdSYOSHIFUJI Hideaki } 24223c21edbdSYOSHIFUJI Hideaki 24233c21edbdSYOSHIFUJI Hideaki if (idev) { 24243c21edbdSYOSHIFUJI Hideaki if (idev->if_flags & IF_READY) { 24253c21edbdSYOSHIFUJI Hideaki /* device is already configured. */ 24263c21edbdSYOSHIFUJI Hideaki break; 24273c21edbdSYOSHIFUJI Hideaki } 24283c21edbdSYOSHIFUJI Hideaki idev->if_flags |= IF_READY; 24293c21edbdSYOSHIFUJI Hideaki } 24303c21edbdSYOSHIFUJI Hideaki 24313c21edbdSYOSHIFUJI Hideaki printk(KERN_INFO 24323c21edbdSYOSHIFUJI Hideaki "ADDRCONF(NETDEV_CHANGE): %s: " 24333c21edbdSYOSHIFUJI Hideaki "link becomes ready\n", 24343c21edbdSYOSHIFUJI Hideaki dev->name); 24353c21edbdSYOSHIFUJI Hideaki 2436c5e33bddSYOSHIFUJI Hideaki run_pending = 1; 24373c21edbdSYOSHIFUJI Hideaki } 24383c21edbdSYOSHIFUJI Hideaki 24391da177e4SLinus Torvalds switch(dev->type) { 24400be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 24411da177e4SLinus Torvalds case ARPHRD_SIT: 24421da177e4SLinus Torvalds addrconf_sit_config(dev); 24431da177e4SLinus Torvalds break; 24440be669bbSJoerg Roedel #endif 24451da177e4SLinus Torvalds case ARPHRD_TUNNEL6: 24461da177e4SLinus Torvalds addrconf_ip6_tnl_config(dev); 24471da177e4SLinus Torvalds break; 24481da177e4SLinus Torvalds case ARPHRD_LOOPBACK: 24491da177e4SLinus Torvalds init_loopback(dev); 24501da177e4SLinus Torvalds break; 24511da177e4SLinus Torvalds 24521da177e4SLinus Torvalds default: 24531da177e4SLinus Torvalds addrconf_dev_config(dev); 24541da177e4SLinus Torvalds break; 24553ff50b79SStephen Hemminger } 24561da177e4SLinus Torvalds if (idev) { 2457c5e33bddSYOSHIFUJI Hideaki if (run_pending) 2458c5e33bddSYOSHIFUJI Hideaki addrconf_dad_run(idev); 2459c5e33bddSYOSHIFUJI Hideaki 24601da177e4SLinus Torvalds /* If the MTU changed during the interface down, when the 24611da177e4SLinus Torvalds interface up, the changed MTU must be reflected in the 24621da177e4SLinus Torvalds idev as well as routers. 24631da177e4SLinus Torvalds */ 24641da177e4SLinus Torvalds if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) { 24651da177e4SLinus Torvalds rt6_mtu_change(dev, dev->mtu); 24661da177e4SLinus Torvalds idev->cnf.mtu6 = dev->mtu; 24671da177e4SLinus Torvalds } 24681da177e4SLinus Torvalds idev->tstamp = jiffies; 24691da177e4SLinus Torvalds inet6_ifinfo_notify(RTM_NEWLINK, idev); 24701da177e4SLinus Torvalds /* If the changed mtu during down is lower than IPV6_MIN_MTU 24711da177e4SLinus Torvalds stop IPv6 on this interface. 24721da177e4SLinus Torvalds */ 24731da177e4SLinus Torvalds if (dev->mtu < IPV6_MIN_MTU) 24741da177e4SLinus Torvalds addrconf_ifdown(dev, event != NETDEV_DOWN); 24751da177e4SLinus Torvalds } 24761da177e4SLinus Torvalds break; 24771da177e4SLinus Torvalds 24781da177e4SLinus Torvalds case NETDEV_CHANGEMTU: 24791da177e4SLinus Torvalds if (idev && dev->mtu >= IPV6_MIN_MTU) { 24801da177e4SLinus Torvalds rt6_mtu_change(dev, dev->mtu); 24811da177e4SLinus Torvalds idev->cnf.mtu6 = dev->mtu; 24821da177e4SLinus Torvalds break; 24831da177e4SLinus Torvalds } 24841da177e4SLinus Torvalds 2485d31c7b8fSEvgeniy Polyakov if (!idev && dev->mtu >= IPV6_MIN_MTU) { 2486d31c7b8fSEvgeniy Polyakov idev = ipv6_add_dev(dev); 2487d31c7b8fSEvgeniy Polyakov if (idev) 2488d31c7b8fSEvgeniy Polyakov break; 2489d31c7b8fSEvgeniy Polyakov } 2490d31c7b8fSEvgeniy Polyakov 24911da177e4SLinus Torvalds /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */ 24921da177e4SLinus Torvalds 24931da177e4SLinus Torvalds case NETDEV_DOWN: 24941da177e4SLinus Torvalds case NETDEV_UNREGISTER: 24951da177e4SLinus Torvalds /* 24961da177e4SLinus Torvalds * Remove all addresses from this interface. 24971da177e4SLinus Torvalds */ 24981da177e4SLinus Torvalds addrconf_ifdown(dev, event != NETDEV_DOWN); 24991da177e4SLinus Torvalds break; 25003c21edbdSYOSHIFUJI Hideaki 25011da177e4SLinus Torvalds case NETDEV_CHANGENAME: 25021da177e4SLinus Torvalds if (idev) { 25035632c515SStephen Hemminger snmp6_unregister_dev(idev); 2504408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 2505f52295a9SPavel Emelyanov addrconf_sysctl_register(idev); 2506b217d616SHerbert Xu err = snmp6_register_dev(idev); 2507b217d616SHerbert Xu if (err) 2508b217d616SHerbert Xu return notifier_from_errno(err); 25095632c515SStephen Hemminger } 25101da177e4SLinus Torvalds break; 25113ff50b79SStephen Hemminger } 25121da177e4SLinus Torvalds 25131da177e4SLinus Torvalds return NOTIFY_OK; 25141da177e4SLinus Torvalds } 25151da177e4SLinus Torvalds 25161da177e4SLinus Torvalds /* 25171da177e4SLinus Torvalds * addrconf module should be notified of a device going up 25181da177e4SLinus Torvalds */ 25191da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = { 25201da177e4SLinus Torvalds .notifier_call = addrconf_notify, 25211da177e4SLinus Torvalds .priority = 0 25221da177e4SLinus Torvalds }; 25231da177e4SLinus Torvalds 25241da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how) 25251da177e4SLinus Torvalds { 25261da177e4SLinus Torvalds struct inet6_dev *idev; 25271da177e4SLinus Torvalds struct inet6_ifaddr *ifa, **bifa; 2528c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 25291da177e4SLinus Torvalds int i; 25301da177e4SLinus Torvalds 25311da177e4SLinus Torvalds ASSERT_RTNL(); 25321da177e4SLinus Torvalds 2533eb867579SDenis V. Lunev if ((dev->flags & IFF_LOOPBACK) && how == 1) 25341da177e4SLinus Torvalds how = 0; 25351da177e4SLinus Torvalds 2536f3db4851SDaniel Lezcano rt6_ifdown(net, dev); 25371da177e4SLinus Torvalds neigh_ifdown(&nd_tbl, dev); 25381da177e4SLinus Torvalds 25391da177e4SLinus Torvalds idev = __in6_dev_get(dev); 25401da177e4SLinus Torvalds if (idev == NULL) 25411da177e4SLinus Torvalds return -ENODEV; 25421da177e4SLinus Torvalds 25431da177e4SLinus Torvalds /* Step 1: remove reference to ipv6 device from parent device. 25441da177e4SLinus Torvalds Do not dev_put! 25451da177e4SLinus Torvalds */ 2546439e2385SDenis V. Lunev if (how) { 25471da177e4SLinus Torvalds idev->dead = 1; 25488814c4b5SYOSHIFUJI Hideaki 25498814c4b5SYOSHIFUJI Hideaki /* protected by rtnl_lock */ 25508814c4b5SYOSHIFUJI Hideaki rcu_assign_pointer(dev->ip6_ptr, NULL); 25511da177e4SLinus Torvalds 25521da177e4SLinus Torvalds /* Step 1.5: remove snmp6 entry */ 25531da177e4SLinus Torvalds snmp6_unregister_dev(idev); 25541da177e4SLinus Torvalds 25551da177e4SLinus Torvalds } 25561da177e4SLinus Torvalds 25571da177e4SLinus Torvalds /* Step 2: clear hash table */ 25581da177e4SLinus Torvalds for (i=0; i<IN6_ADDR_HSIZE; i++) { 25591da177e4SLinus Torvalds bifa = &inet6_addr_lst[i]; 25601da177e4SLinus Torvalds 25611da177e4SLinus Torvalds write_lock_bh(&addrconf_hash_lock); 25621da177e4SLinus Torvalds while ((ifa = *bifa) != NULL) { 25631da177e4SLinus Torvalds if (ifa->idev == idev) { 25641da177e4SLinus Torvalds *bifa = ifa->lst_next; 25651da177e4SLinus Torvalds ifa->lst_next = NULL; 25661da177e4SLinus Torvalds addrconf_del_timer(ifa); 25671da177e4SLinus Torvalds in6_ifa_put(ifa); 25681da177e4SLinus Torvalds continue; 25691da177e4SLinus Torvalds } 25701da177e4SLinus Torvalds bifa = &ifa->lst_next; 25711da177e4SLinus Torvalds } 25721da177e4SLinus Torvalds write_unlock_bh(&addrconf_hash_lock); 25731da177e4SLinus Torvalds } 25741da177e4SLinus Torvalds 25751da177e4SLinus Torvalds write_lock_bh(&idev->lock); 25761da177e4SLinus Torvalds 25771da177e4SLinus Torvalds /* Step 3: clear flags for stateless addrconf */ 2578439e2385SDenis V. Lunev if (!how) 25793c21edbdSYOSHIFUJI Hideaki idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY); 25801da177e4SLinus Torvalds 25811da177e4SLinus Torvalds /* Step 4: clear address list */ 25821da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 2583439e2385SDenis V. Lunev if (how && del_timer(&idev->regen_timer)) 25841da177e4SLinus Torvalds in6_dev_put(idev); 25851da177e4SLinus Torvalds 25861da177e4SLinus Torvalds /* clear tempaddr list */ 25871da177e4SLinus Torvalds while ((ifa = idev->tempaddr_list) != NULL) { 25881da177e4SLinus Torvalds idev->tempaddr_list = ifa->tmp_next; 25891da177e4SLinus Torvalds ifa->tmp_next = NULL; 25901da177e4SLinus Torvalds ifa->dead = 1; 25911da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 25921da177e4SLinus Torvalds spin_lock_bh(&ifa->lock); 25931da177e4SLinus Torvalds 25941da177e4SLinus Torvalds if (ifa->ifpub) { 25951da177e4SLinus Torvalds in6_ifa_put(ifa->ifpub); 25961da177e4SLinus Torvalds ifa->ifpub = NULL; 25971da177e4SLinus Torvalds } 25981da177e4SLinus Torvalds spin_unlock_bh(&ifa->lock); 25991da177e4SLinus Torvalds in6_ifa_put(ifa); 26001da177e4SLinus Torvalds write_lock_bh(&idev->lock); 26011da177e4SLinus Torvalds } 26021da177e4SLinus Torvalds #endif 26031da177e4SLinus Torvalds while ((ifa = idev->addr_list) != NULL) { 26041da177e4SLinus Torvalds idev->addr_list = ifa->if_next; 26051da177e4SLinus Torvalds ifa->if_next = NULL; 26061da177e4SLinus Torvalds ifa->dead = 1; 26071da177e4SLinus Torvalds addrconf_del_timer(ifa); 26081da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 26091da177e4SLinus Torvalds 26101da177e4SLinus Torvalds __ipv6_ifa_notify(RTM_DELADDR, ifa); 2611063ed369SVlad Yasevich atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifa); 26121da177e4SLinus Torvalds in6_ifa_put(ifa); 26131da177e4SLinus Torvalds 26141da177e4SLinus Torvalds write_lock_bh(&idev->lock); 26151da177e4SLinus Torvalds } 26161da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 26171da177e4SLinus Torvalds 26181da177e4SLinus Torvalds /* Step 5: Discard multicast list */ 26191da177e4SLinus Torvalds 2620439e2385SDenis V. Lunev if (how) 26211da177e4SLinus Torvalds ipv6_mc_destroy_dev(idev); 26221da177e4SLinus Torvalds else 26231da177e4SLinus Torvalds ipv6_mc_down(idev); 26241da177e4SLinus Torvalds 26251da177e4SLinus Torvalds idev->tstamp = jiffies; 26261da177e4SLinus Torvalds 26271da177e4SLinus Torvalds /* Shot the device (if unregistered) */ 26281da177e4SLinus Torvalds 2629439e2385SDenis V. Lunev if (how) { 2630408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 26311da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, idev->nd_parms); 26321da177e4SLinus Torvalds neigh_ifdown(&nd_tbl, dev); 26331da177e4SLinus Torvalds in6_dev_put(idev); 26341da177e4SLinus Torvalds } 26351da177e4SLinus Torvalds return 0; 26361da177e4SLinus Torvalds } 26371da177e4SLinus Torvalds 26381da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data) 26391da177e4SLinus Torvalds { 26401da177e4SLinus Torvalds struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data; 26411da177e4SLinus Torvalds 26421da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 26431da177e4SLinus Torvalds goto out; 26441da177e4SLinus Torvalds 26451da177e4SLinus Torvalds if (ifp->idev->if_flags & IF_RA_RCVD) { 26461da177e4SLinus Torvalds /* 26471da177e4SLinus Torvalds * Announcement received after solicitation 26481da177e4SLinus Torvalds * was sent 26491da177e4SLinus Torvalds */ 26501da177e4SLinus Torvalds goto out; 26511da177e4SLinus Torvalds } 26521da177e4SLinus Torvalds 26531da177e4SLinus Torvalds spin_lock(&ifp->lock); 26541da177e4SLinus Torvalds if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) { 26551da177e4SLinus Torvalds /* The wait after the last probe can be shorter */ 26561da177e4SLinus Torvalds addrconf_mod_timer(ifp, AC_RS, 26571da177e4SLinus Torvalds (ifp->probes == ifp->idev->cnf.rtr_solicits) ? 26581da177e4SLinus Torvalds ifp->idev->cnf.rtr_solicit_delay : 26591da177e4SLinus Torvalds ifp->idev->cnf.rtr_solicit_interval); 26601da177e4SLinus Torvalds spin_unlock(&ifp->lock); 26611da177e4SLinus Torvalds 2662f3ee4010SYOSHIFUJI Hideaki ndisc_send_rs(ifp->idev->dev, &ifp->addr, &in6addr_linklocal_allrouters); 26631da177e4SLinus Torvalds } else { 26641da177e4SLinus Torvalds spin_unlock(&ifp->lock); 26651da177e4SLinus Torvalds /* 26661da177e4SLinus Torvalds * Note: we do not support deprecated "all on-link" 26671da177e4SLinus Torvalds * assumption any longer. 26681da177e4SLinus Torvalds */ 26691da177e4SLinus Torvalds printk(KERN_DEBUG "%s: no IPv6 routers present\n", 26701da177e4SLinus Torvalds ifp->idev->dev->name); 26711da177e4SLinus Torvalds } 26721da177e4SLinus Torvalds 26731da177e4SLinus Torvalds out: 26741da177e4SLinus Torvalds in6_ifa_put(ifp); 26751da177e4SLinus Torvalds } 26761da177e4SLinus Torvalds 26771da177e4SLinus Torvalds /* 26781da177e4SLinus Torvalds * Duplicate Address Detection 26791da177e4SLinus Torvalds */ 26803c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp) 26813c21edbdSYOSHIFUJI Hideaki { 26823c21edbdSYOSHIFUJI Hideaki unsigned long rand_num; 26833c21edbdSYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev; 26843c21edbdSYOSHIFUJI Hideaki 268595c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 268695c385b4SNeil Horman rand_num = 0; 268795c385b4SNeil Horman else 26883c21edbdSYOSHIFUJI Hideaki rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1); 268995c385b4SNeil Horman 26903c21edbdSYOSHIFUJI Hideaki ifp->probes = idev->cnf.dad_transmits; 26913c21edbdSYOSHIFUJI Hideaki addrconf_mod_timer(ifp, AC_DAD, rand_num); 26923c21edbdSYOSHIFUJI Hideaki } 26933c21edbdSYOSHIFUJI Hideaki 2694e431b8c0SJamal Hadi Salim static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags) 26951da177e4SLinus Torvalds { 26961da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 26971da177e4SLinus Torvalds struct net_device *dev = idev->dev; 26981da177e4SLinus Torvalds 26991da177e4SLinus Torvalds addrconf_join_solict(dev, &ifp->addr); 27001da177e4SLinus Torvalds 27011da177e4SLinus Torvalds net_srandom(ifp->addr.s6_addr32[3]); 27021da177e4SLinus Torvalds 27031da177e4SLinus Torvalds read_lock_bh(&idev->lock); 27041da177e4SLinus Torvalds if (ifp->dead) 27051da177e4SLinus Torvalds goto out; 27061da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 27071da177e4SLinus Torvalds 27081da177e4SLinus Torvalds if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) || 270955ebaef1SNoriaki TAKAMIYA !(ifp->flags&IFA_F_TENTATIVE) || 271055ebaef1SNoriaki TAKAMIYA ifp->flags & IFA_F_NODAD) { 271195c385b4SNeil Horman ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC); 27121da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 27131da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 27141da177e4SLinus Torvalds 27151da177e4SLinus Torvalds addrconf_dad_completed(ifp); 27161da177e4SLinus Torvalds return; 27171da177e4SLinus Torvalds } 27181da177e4SLinus Torvalds 27196732badeSYOSHIFUJI Hideaki if (!(idev->if_flags & IF_READY)) { 27203dd3bf83SYOSHIFUJI Hideaki spin_unlock_bh(&ifp->lock); 27216732badeSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 27223c21edbdSYOSHIFUJI Hideaki /* 27233c21edbdSYOSHIFUJI Hideaki * If the defice is not ready: 27243c21edbdSYOSHIFUJI Hideaki * - keep it tentative if it is a permanent address. 27253c21edbdSYOSHIFUJI Hideaki * - otherwise, kill it. 27263c21edbdSYOSHIFUJI Hideaki */ 27273c21edbdSYOSHIFUJI Hideaki in6_ifa_hold(ifp); 27283c21edbdSYOSHIFUJI Hideaki addrconf_dad_stop(ifp); 27296732badeSYOSHIFUJI Hideaki return; 27303c21edbdSYOSHIFUJI Hideaki } 273195c385b4SNeil Horman 273295c385b4SNeil Horman /* 273395c385b4SNeil Horman * Optimistic nodes can start receiving 273495c385b4SNeil Horman * Frames right away 273595c385b4SNeil Horman */ 273695c385b4SNeil Horman if(ifp->flags & IFA_F_OPTIMISTIC) 273795c385b4SNeil Horman ip6_ins_rt(ifp->rt); 273895c385b4SNeil Horman 27396732badeSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 27406732badeSYOSHIFUJI Hideaki spin_unlock_bh(&ifp->lock); 27411da177e4SLinus Torvalds out: 27421da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 27431da177e4SLinus Torvalds } 27441da177e4SLinus Torvalds 27451da177e4SLinus Torvalds static void addrconf_dad_timer(unsigned long data) 27461da177e4SLinus Torvalds { 27471da177e4SLinus Torvalds struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data; 27481da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 27491da177e4SLinus Torvalds struct in6_addr mcaddr; 27501da177e4SLinus Torvalds 27511da177e4SLinus Torvalds read_lock_bh(&idev->lock); 27521da177e4SLinus Torvalds if (idev->dead) { 27531da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 27541da177e4SLinus Torvalds goto out; 27551da177e4SLinus Torvalds } 27561da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 27571da177e4SLinus Torvalds if (ifp->probes == 0) { 27581da177e4SLinus Torvalds /* 27591da177e4SLinus Torvalds * DAD was successful 27601da177e4SLinus Torvalds */ 27611da177e4SLinus Torvalds 276295c385b4SNeil Horman ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC); 27631da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 27641da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 27651da177e4SLinus Torvalds 27661da177e4SLinus Torvalds addrconf_dad_completed(ifp); 27671da177e4SLinus Torvalds 27681da177e4SLinus Torvalds goto out; 27691da177e4SLinus Torvalds } 27701da177e4SLinus Torvalds 27711da177e4SLinus Torvalds ifp->probes--; 27721da177e4SLinus Torvalds addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time); 27731da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 27741da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 27751da177e4SLinus Torvalds 27761da177e4SLinus Torvalds /* send a neighbour solicitation for our addr */ 27771da177e4SLinus Torvalds addrconf_addr_solict_mult(&ifp->addr, &mcaddr); 2778d7aabf22SYOSHIFUJI Hideaki ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any); 27791da177e4SLinus Torvalds out: 27801da177e4SLinus Torvalds in6_ifa_put(ifp); 27811da177e4SLinus Torvalds } 27821da177e4SLinus Torvalds 27831da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp) 27841da177e4SLinus Torvalds { 27851da177e4SLinus Torvalds struct net_device * dev = ifp->idev->dev; 27861da177e4SLinus Torvalds 27871da177e4SLinus Torvalds /* 27881da177e4SLinus Torvalds * Configure the address for reception. Now it is valid. 27891da177e4SLinus Torvalds */ 27901da177e4SLinus Torvalds 27911da177e4SLinus Torvalds ipv6_ifa_notify(RTM_NEWADDR, ifp); 27921da177e4SLinus Torvalds 27931da177e4SLinus Torvalds /* If added prefix is link local and forwarding is off, 27941da177e4SLinus Torvalds start sending router solicitations. 27951da177e4SLinus Torvalds */ 27961da177e4SLinus Torvalds 27971da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding == 0 && 27981da177e4SLinus Torvalds ifp->idev->cnf.rtr_solicits > 0 && 27991da177e4SLinus Torvalds (dev->flags&IFF_LOOPBACK) == 0 && 28001da177e4SLinus Torvalds (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) { 28011da177e4SLinus Torvalds /* 28021da177e4SLinus Torvalds * If a host as already performed a random delay 28031da177e4SLinus Torvalds * [...] as part of DAD [...] there is no need 28041da177e4SLinus Torvalds * to delay again before sending the first RS 28051da177e4SLinus Torvalds */ 2806f3ee4010SYOSHIFUJI Hideaki ndisc_send_rs(ifp->idev->dev, &ifp->addr, &in6addr_linklocal_allrouters); 28071da177e4SLinus Torvalds 28081da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 28091da177e4SLinus Torvalds ifp->probes = 1; 28101da177e4SLinus Torvalds ifp->idev->if_flags |= IF_RS_SENT; 28111da177e4SLinus Torvalds addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval); 28121da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 28131da177e4SLinus Torvalds } 28141da177e4SLinus Torvalds } 28151da177e4SLinus Torvalds 2816c5e33bddSYOSHIFUJI Hideaki static void addrconf_dad_run(struct inet6_dev *idev) { 2817c5e33bddSYOSHIFUJI Hideaki struct inet6_ifaddr *ifp; 2818c5e33bddSYOSHIFUJI Hideaki 2819c5e33bddSYOSHIFUJI Hideaki read_lock_bh(&idev->lock); 2820c5e33bddSYOSHIFUJI Hideaki for (ifp = idev->addr_list; ifp; ifp = ifp->if_next) { 2821c5e33bddSYOSHIFUJI Hideaki spin_lock_bh(&ifp->lock); 2822c5e33bddSYOSHIFUJI Hideaki if (!(ifp->flags & IFA_F_TENTATIVE)) { 2823c5e33bddSYOSHIFUJI Hideaki spin_unlock_bh(&ifp->lock); 2824c5e33bddSYOSHIFUJI Hideaki continue; 2825c5e33bddSYOSHIFUJI Hideaki } 2826c5e33bddSYOSHIFUJI Hideaki spin_unlock_bh(&ifp->lock); 2827c5e33bddSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 2828c5e33bddSYOSHIFUJI Hideaki } 2829c5e33bddSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 2830c5e33bddSYOSHIFUJI Hideaki } 2831c5e33bddSYOSHIFUJI Hideaki 28321da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 28331da177e4SLinus Torvalds struct if6_iter_state { 28343c40090aSDaniel Lezcano struct seq_net_private p; 28351da177e4SLinus Torvalds int bucket; 28361da177e4SLinus Torvalds }; 28371da177e4SLinus Torvalds 28381da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_first(struct seq_file *seq) 28391da177e4SLinus Torvalds { 28401da177e4SLinus Torvalds struct inet6_ifaddr *ifa = NULL; 28411da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 28421218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 28431da177e4SLinus Torvalds 28441da177e4SLinus Torvalds for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) { 28451da177e4SLinus Torvalds ifa = inet6_addr_lst[state->bucket]; 28463c40090aSDaniel Lezcano 2847878628fbSYOSHIFUJI Hideaki while (ifa && !net_eq(dev_net(ifa->idev->dev), net)) 28483c40090aSDaniel Lezcano ifa = ifa->lst_next; 28491da177e4SLinus Torvalds if (ifa) 28501da177e4SLinus Torvalds break; 28511da177e4SLinus Torvalds } 28521da177e4SLinus Torvalds return ifa; 28531da177e4SLinus Torvalds } 28541da177e4SLinus Torvalds 28551da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa) 28561da177e4SLinus Torvalds { 28571da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 28581218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 28591da177e4SLinus Torvalds 28601da177e4SLinus Torvalds ifa = ifa->lst_next; 28611da177e4SLinus Torvalds try_again: 28623c40090aSDaniel Lezcano if (ifa) { 2863878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifa->idev->dev), net)) { 28643c40090aSDaniel Lezcano ifa = ifa->lst_next; 28653c40090aSDaniel Lezcano goto try_again; 28663c40090aSDaniel Lezcano } 28673c40090aSDaniel Lezcano } 28683c40090aSDaniel Lezcano 28691da177e4SLinus Torvalds if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) { 28701da177e4SLinus Torvalds ifa = inet6_addr_lst[state->bucket]; 28711da177e4SLinus Torvalds goto try_again; 28721da177e4SLinus Torvalds } 28733c40090aSDaniel Lezcano 28741da177e4SLinus Torvalds return ifa; 28751da177e4SLinus Torvalds } 28761da177e4SLinus Torvalds 28771da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos) 28781da177e4SLinus Torvalds { 28791da177e4SLinus Torvalds struct inet6_ifaddr *ifa = if6_get_first(seq); 28801da177e4SLinus Torvalds 28811da177e4SLinus Torvalds if (ifa) 28821da177e4SLinus Torvalds while(pos && (ifa = if6_get_next(seq, ifa)) != NULL) 28831da177e4SLinus Torvalds --pos; 28841da177e4SLinus Torvalds return pos ? NULL : ifa; 28851da177e4SLinus Torvalds } 28861da177e4SLinus Torvalds 28871da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos) 2888d20b3109SStephen Hemminger __acquires(addrconf_hash_lock) 28891da177e4SLinus Torvalds { 28901da177e4SLinus Torvalds read_lock_bh(&addrconf_hash_lock); 28911da177e4SLinus Torvalds return if6_get_idx(seq, *pos); 28921da177e4SLinus Torvalds } 28931da177e4SLinus Torvalds 28941da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos) 28951da177e4SLinus Torvalds { 28961da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 28971da177e4SLinus Torvalds 28981da177e4SLinus Torvalds ifa = if6_get_next(seq, v); 28991da177e4SLinus Torvalds ++*pos; 29001da177e4SLinus Torvalds return ifa; 29011da177e4SLinus Torvalds } 29021da177e4SLinus Torvalds 29031da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v) 2904d20b3109SStephen Hemminger __releases(addrconf_hash_lock) 29051da177e4SLinus Torvalds { 29061da177e4SLinus Torvalds read_unlock_bh(&addrconf_hash_lock); 29071da177e4SLinus Torvalds } 29081da177e4SLinus Torvalds 29091da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v) 29101da177e4SLinus Torvalds { 29111da177e4SLinus Torvalds struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v; 29121da177e4SLinus Torvalds seq_printf(seq, 29139343e79aSYOSHIFUJI Hideaki NIP6_SEQFMT " %02x %02x %02x %02x %8s\n", 29141da177e4SLinus Torvalds NIP6(ifp->addr), 29151da177e4SLinus Torvalds ifp->idev->dev->ifindex, 29161da177e4SLinus Torvalds ifp->prefix_len, 29171da177e4SLinus Torvalds ifp->scope, 29181da177e4SLinus Torvalds ifp->flags, 29191da177e4SLinus Torvalds ifp->idev->dev->name); 29201da177e4SLinus Torvalds return 0; 29211da177e4SLinus Torvalds } 29221da177e4SLinus Torvalds 292356b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = { 29241da177e4SLinus Torvalds .start = if6_seq_start, 29251da177e4SLinus Torvalds .next = if6_seq_next, 29261da177e4SLinus Torvalds .show = if6_seq_show, 29271da177e4SLinus Torvalds .stop = if6_seq_stop, 29281da177e4SLinus Torvalds }; 29291da177e4SLinus Torvalds 29301da177e4SLinus Torvalds static int if6_seq_open(struct inode *inode, struct file *file) 29311da177e4SLinus Torvalds { 29323c40090aSDaniel Lezcano return seq_open_net(inode, file, &if6_seq_ops, 2933cf7732e4SPavel Emelyanov sizeof(struct if6_iter_state)); 29341da177e4SLinus Torvalds } 29351da177e4SLinus Torvalds 29369a32144eSArjan van de Ven static const struct file_operations if6_fops = { 29371da177e4SLinus Torvalds .owner = THIS_MODULE, 29381da177e4SLinus Torvalds .open = if6_seq_open, 29391da177e4SLinus Torvalds .read = seq_read, 29401da177e4SLinus Torvalds .llseek = seq_lseek, 29413c40090aSDaniel Lezcano .release = seq_release_net, 29421da177e4SLinus Torvalds }; 29431da177e4SLinus Torvalds 29443c40090aSDaniel Lezcano static int if6_proc_net_init(struct net *net) 29451da177e4SLinus Torvalds { 29463c40090aSDaniel Lezcano if (!proc_net_fops_create(net, "if_inet6", S_IRUGO, &if6_fops)) 29471da177e4SLinus Torvalds return -ENOMEM; 29481da177e4SLinus Torvalds return 0; 29491da177e4SLinus Torvalds } 29501da177e4SLinus Torvalds 29513c40090aSDaniel Lezcano static void if6_proc_net_exit(struct net *net) 29523c40090aSDaniel Lezcano { 29533c40090aSDaniel Lezcano proc_net_remove(net, "if_inet6"); 29543c40090aSDaniel Lezcano } 29553c40090aSDaniel Lezcano 29563c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = { 29573c40090aSDaniel Lezcano .init = if6_proc_net_init, 29583c40090aSDaniel Lezcano .exit = if6_proc_net_exit, 29593c40090aSDaniel Lezcano }; 29603c40090aSDaniel Lezcano 29613c40090aSDaniel Lezcano int __init if6_proc_init(void) 29623c40090aSDaniel Lezcano { 29633c40090aSDaniel Lezcano return register_pernet_subsys(&if6_proc_net_ops); 29643c40090aSDaniel Lezcano } 29653c40090aSDaniel Lezcano 29661da177e4SLinus Torvalds void if6_proc_exit(void) 29671da177e4SLinus Torvalds { 29683c40090aSDaniel Lezcano unregister_pernet_subsys(&if6_proc_net_ops); 29691da177e4SLinus Torvalds } 29701da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 29711da177e4SLinus Torvalds 297259fbb3a6SMasahide NAKAMURA #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) 29733b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */ 2974389f6612SDaniel Lezcano int ipv6_chk_home_addr(struct net *net, struct in6_addr *addr) 29753b9f9a1cSNoriaki TAKAMIYA { 29763b9f9a1cSNoriaki TAKAMIYA int ret = 0; 29773b9f9a1cSNoriaki TAKAMIYA struct inet6_ifaddr * ifp; 29783b9f9a1cSNoriaki TAKAMIYA u8 hash = ipv6_addr_hash(addr); 29793b9f9a1cSNoriaki TAKAMIYA read_lock_bh(&addrconf_hash_lock); 29803b9f9a1cSNoriaki TAKAMIYA for (ifp = inet6_addr_lst[hash]; ifp; ifp = ifp->lst_next) { 2981878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 2982389f6612SDaniel Lezcano continue; 2983caad295fSYOSHIFUJI Hideaki if (ipv6_addr_equal(&ifp->addr, addr) && 29843b9f9a1cSNoriaki TAKAMIYA (ifp->flags & IFA_F_HOMEADDRESS)) { 29853b9f9a1cSNoriaki TAKAMIYA ret = 1; 29863b9f9a1cSNoriaki TAKAMIYA break; 29873b9f9a1cSNoriaki TAKAMIYA } 29883b9f9a1cSNoriaki TAKAMIYA } 29893b9f9a1cSNoriaki TAKAMIYA read_unlock_bh(&addrconf_hash_lock); 29903b9f9a1cSNoriaki TAKAMIYA return ret; 29913b9f9a1cSNoriaki TAKAMIYA } 29923b9f9a1cSNoriaki TAKAMIYA #endif 29933b9f9a1cSNoriaki TAKAMIYA 29941da177e4SLinus Torvalds /* 29951da177e4SLinus Torvalds * Periodic address status verification 29961da177e4SLinus Torvalds */ 29971da177e4SLinus Torvalds 29981da177e4SLinus Torvalds static void addrconf_verify(unsigned long foo) 29991da177e4SLinus Torvalds { 30001da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 30011da177e4SLinus Torvalds unsigned long now, next; 30021da177e4SLinus Torvalds int i; 30031da177e4SLinus Torvalds 30041da177e4SLinus Torvalds spin_lock_bh(&addrconf_verify_lock); 30051da177e4SLinus Torvalds now = jiffies; 30061da177e4SLinus Torvalds next = now + ADDR_CHECK_FREQUENCY; 30071da177e4SLinus Torvalds 30081da177e4SLinus Torvalds del_timer(&addr_chk_timer); 30091da177e4SLinus Torvalds 30101da177e4SLinus Torvalds for (i=0; i < IN6_ADDR_HSIZE; i++) { 30111da177e4SLinus Torvalds 30121da177e4SLinus Torvalds restart: 30135d5780dfSYan Zheng read_lock(&addrconf_hash_lock); 30141da177e4SLinus Torvalds for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) { 30151da177e4SLinus Torvalds unsigned long age; 30161da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 30171da177e4SLinus Torvalds unsigned long regen_advance; 30181da177e4SLinus Torvalds #endif 30191da177e4SLinus Torvalds 30201da177e4SLinus Torvalds if (ifp->flags & IFA_F_PERMANENT) 30211da177e4SLinus Torvalds continue; 30221da177e4SLinus Torvalds 30231da177e4SLinus Torvalds spin_lock(&ifp->lock); 30241da177e4SLinus Torvalds age = (now - ifp->tstamp) / HZ; 30251da177e4SLinus Torvalds 30261da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 30271da177e4SLinus Torvalds regen_advance = ifp->idev->cnf.regen_max_retry * 30281da177e4SLinus Torvalds ifp->idev->cnf.dad_transmits * 30291da177e4SLinus Torvalds ifp->idev->nd_parms->retrans_time / HZ; 30301da177e4SLinus Torvalds #endif 30311da177e4SLinus Torvalds 30328f27ebb9SYOSHIFUJI Hideaki if (ifp->valid_lft != INFINITY_LIFE_TIME && 30338f27ebb9SYOSHIFUJI Hideaki age >= ifp->valid_lft) { 30341da177e4SLinus Torvalds spin_unlock(&ifp->lock); 30351da177e4SLinus Torvalds in6_ifa_hold(ifp); 30365d5780dfSYan Zheng read_unlock(&addrconf_hash_lock); 30371da177e4SLinus Torvalds ipv6_del_addr(ifp); 30381da177e4SLinus Torvalds goto restart; 30398f27ebb9SYOSHIFUJI Hideaki } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) { 30408f27ebb9SYOSHIFUJI Hideaki spin_unlock(&ifp->lock); 30418f27ebb9SYOSHIFUJI Hideaki continue; 30421da177e4SLinus Torvalds } else if (age >= ifp->prefered_lft) { 30431da177e4SLinus Torvalds /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */ 30441da177e4SLinus Torvalds int deprecate = 0; 30451da177e4SLinus Torvalds 30461da177e4SLinus Torvalds if (!(ifp->flags&IFA_F_DEPRECATED)) { 30471da177e4SLinus Torvalds deprecate = 1; 30481da177e4SLinus Torvalds ifp->flags |= IFA_F_DEPRECATED; 30491da177e4SLinus Torvalds } 30501da177e4SLinus Torvalds 30511da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)) 30521da177e4SLinus Torvalds next = ifp->tstamp + ifp->valid_lft * HZ; 30531da177e4SLinus Torvalds 30541da177e4SLinus Torvalds spin_unlock(&ifp->lock); 30551da177e4SLinus Torvalds 30561da177e4SLinus Torvalds if (deprecate) { 30571da177e4SLinus Torvalds in6_ifa_hold(ifp); 30585d5780dfSYan Zheng read_unlock(&addrconf_hash_lock); 30591da177e4SLinus Torvalds 30601da177e4SLinus Torvalds ipv6_ifa_notify(0, ifp); 30611da177e4SLinus Torvalds in6_ifa_put(ifp); 30621da177e4SLinus Torvalds goto restart; 30631da177e4SLinus Torvalds } 30641da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 30651da177e4SLinus Torvalds } else if ((ifp->flags&IFA_F_TEMPORARY) && 30661da177e4SLinus Torvalds !(ifp->flags&IFA_F_TENTATIVE)) { 30671da177e4SLinus Torvalds if (age >= ifp->prefered_lft - regen_advance) { 30681da177e4SLinus Torvalds struct inet6_ifaddr *ifpub = ifp->ifpub; 30691da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 30701da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 30711da177e4SLinus Torvalds if (!ifp->regen_count && ifpub) { 30721da177e4SLinus Torvalds ifp->regen_count++; 30731da177e4SLinus Torvalds in6_ifa_hold(ifp); 30741da177e4SLinus Torvalds in6_ifa_hold(ifpub); 30751da177e4SLinus Torvalds spin_unlock(&ifp->lock); 30765d5780dfSYan Zheng read_unlock(&addrconf_hash_lock); 3077291d809bSHiroyuki YAMAMORI spin_lock(&ifpub->lock); 3078291d809bSHiroyuki YAMAMORI ifpub->regen_count = 0; 3079291d809bSHiroyuki YAMAMORI spin_unlock(&ifpub->lock); 30801da177e4SLinus Torvalds ipv6_create_tempaddr(ifpub, ifp); 30811da177e4SLinus Torvalds in6_ifa_put(ifpub); 30821da177e4SLinus Torvalds in6_ifa_put(ifp); 30831da177e4SLinus Torvalds goto restart; 30841da177e4SLinus Torvalds } 30851da177e4SLinus Torvalds } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next)) 30861da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ; 30871da177e4SLinus Torvalds spin_unlock(&ifp->lock); 30881da177e4SLinus Torvalds #endif 30891da177e4SLinus Torvalds } else { 30901da177e4SLinus Torvalds /* ifp->prefered_lft <= ifp->valid_lft */ 30911da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 30921da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 30931da177e4SLinus Torvalds spin_unlock(&ifp->lock); 30941da177e4SLinus Torvalds } 30951da177e4SLinus Torvalds } 30965d5780dfSYan Zheng read_unlock(&addrconf_hash_lock); 30971da177e4SLinus Torvalds } 30981da177e4SLinus Torvalds 30991da177e4SLinus Torvalds addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next; 31001da177e4SLinus Torvalds add_timer(&addr_chk_timer); 31011da177e4SLinus Torvalds spin_unlock_bh(&addrconf_verify_lock); 31021da177e4SLinus Torvalds } 31031da177e4SLinus Torvalds 3104461d8837SThomas Graf static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local) 3105461d8837SThomas Graf { 3106461d8837SThomas Graf struct in6_addr *pfx = NULL; 3107461d8837SThomas Graf 3108461d8837SThomas Graf if (addr) 3109461d8837SThomas Graf pfx = nla_data(addr); 3110461d8837SThomas Graf 3111461d8837SThomas Graf if (local) { 3112461d8837SThomas Graf if (pfx && nla_memcmp(local, pfx, sizeof(*pfx))) 3113461d8837SThomas Graf pfx = NULL; 3114461d8837SThomas Graf else 3115461d8837SThomas Graf pfx = nla_data(local); 3116461d8837SThomas Graf } 3117461d8837SThomas Graf 3118461d8837SThomas Graf return pfx; 3119461d8837SThomas Graf } 3120461d8837SThomas Graf 3121ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = { 3122461d8837SThomas Graf [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) }, 3123461d8837SThomas Graf [IFA_LOCAL] = { .len = sizeof(struct in6_addr) }, 3124461d8837SThomas Graf [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) }, 3125461d8837SThomas Graf }; 3126461d8837SThomas Graf 31271da177e4SLinus Torvalds static int 31281da177e4SLinus Torvalds inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 31291da177e4SLinus Torvalds { 31303b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 3131b933f716SThomas Graf struct ifaddrmsg *ifm; 3132b933f716SThomas Graf struct nlattr *tb[IFA_MAX+1]; 31331da177e4SLinus Torvalds struct in6_addr *pfx; 3134b933f716SThomas Graf int err; 31351da177e4SLinus Torvalds 3136b933f716SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 3137b933f716SThomas Graf if (err < 0) 3138b933f716SThomas Graf return err; 3139b933f716SThomas Graf 3140b933f716SThomas Graf ifm = nlmsg_data(nlh); 3141b933f716SThomas Graf pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]); 31421da177e4SLinus Torvalds if (pfx == NULL) 31431da177e4SLinus Torvalds return -EINVAL; 31441da177e4SLinus Torvalds 3145af284937SDaniel Lezcano return inet6_addr_del(net, ifm->ifa_index, pfx, ifm->ifa_prefixlen); 31461da177e4SLinus Torvalds } 31471da177e4SLinus Torvalds 314855ebaef1SNoriaki TAKAMIYA static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags, 314955ebaef1SNoriaki TAKAMIYA u32 prefered_lft, u32 valid_lft) 3150081bba5bSNoriaki TAKAMIYA { 315146d48046SYOSHIFUJI Hideaki u32 flags = RTF_EXPIRES; 315246d48046SYOSHIFUJI Hideaki 3153081bba5bSNoriaki TAKAMIYA if (!valid_lft || (prefered_lft > valid_lft)) 3154081bba5bSNoriaki TAKAMIYA return -EINVAL; 3155081bba5bSNoriaki TAKAMIYA 315646d48046SYOSHIFUJI Hideaki if (valid_lft == INFINITY_LIFE_TIME) { 315755ebaef1SNoriaki TAKAMIYA ifa_flags |= IFA_F_PERMANENT; 315846d48046SYOSHIFUJI Hideaki flags = 0; 315946d48046SYOSHIFUJI Hideaki } else if (valid_lft >= 0x7FFFFFFF/HZ) 3160081bba5bSNoriaki TAKAMIYA valid_lft = 0x7FFFFFFF/HZ; 3161081bba5bSNoriaki TAKAMIYA 3162081bba5bSNoriaki TAKAMIYA if (prefered_lft == 0) 316355ebaef1SNoriaki TAKAMIYA ifa_flags |= IFA_F_DEPRECATED; 3164081bba5bSNoriaki TAKAMIYA else if ((prefered_lft >= 0x7FFFFFFF/HZ) && 3165081bba5bSNoriaki TAKAMIYA (prefered_lft != INFINITY_LIFE_TIME)) 3166081bba5bSNoriaki TAKAMIYA prefered_lft = 0x7FFFFFFF/HZ; 3167081bba5bSNoriaki TAKAMIYA 3168081bba5bSNoriaki TAKAMIYA spin_lock_bh(&ifp->lock); 31693b9f9a1cSNoriaki TAKAMIYA ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags; 3170081bba5bSNoriaki TAKAMIYA ifp->tstamp = jiffies; 3171081bba5bSNoriaki TAKAMIYA ifp->valid_lft = valid_lft; 3172081bba5bSNoriaki TAKAMIYA ifp->prefered_lft = prefered_lft; 3173081bba5bSNoriaki TAKAMIYA 3174081bba5bSNoriaki TAKAMIYA spin_unlock_bh(&ifp->lock); 3175081bba5bSNoriaki TAKAMIYA if (!(ifp->flags&IFA_F_TENTATIVE)) 3176081bba5bSNoriaki TAKAMIYA ipv6_ifa_notify(0, ifp); 3177081bba5bSNoriaki TAKAMIYA 317846d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev, 317946d48046SYOSHIFUJI Hideaki jiffies_to_clock_t(valid_lft * HZ), flags); 3180081bba5bSNoriaki TAKAMIYA addrconf_verify(0); 3181081bba5bSNoriaki TAKAMIYA 3182081bba5bSNoriaki TAKAMIYA return 0; 3183081bba5bSNoriaki TAKAMIYA } 3184081bba5bSNoriaki TAKAMIYA 3185081bba5bSNoriaki TAKAMIYA static int 31861da177e4SLinus Torvalds inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 31871da177e4SLinus Torvalds { 31883b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 3189461d8837SThomas Graf struct ifaddrmsg *ifm; 3190461d8837SThomas Graf struct nlattr *tb[IFA_MAX+1]; 31911da177e4SLinus Torvalds struct in6_addr *pfx; 31927198f8ceSThomas Graf struct inet6_ifaddr *ifa; 31937198f8ceSThomas Graf struct net_device *dev; 319455ebaef1SNoriaki TAKAMIYA u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME; 319555ebaef1SNoriaki TAKAMIYA u8 ifa_flags; 3196461d8837SThomas Graf int err; 31971da177e4SLinus Torvalds 3198461d8837SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 3199461d8837SThomas Graf if (err < 0) 3200461d8837SThomas Graf return err; 3201461d8837SThomas Graf 3202461d8837SThomas Graf ifm = nlmsg_data(nlh); 3203461d8837SThomas Graf pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]); 32041da177e4SLinus Torvalds if (pfx == NULL) 32051da177e4SLinus Torvalds return -EINVAL; 32061da177e4SLinus Torvalds 3207461d8837SThomas Graf if (tb[IFA_CACHEINFO]) { 32080778769dSNoriaki TAKAMIYA struct ifa_cacheinfo *ci; 3209461d8837SThomas Graf 3210461d8837SThomas Graf ci = nla_data(tb[IFA_CACHEINFO]); 32110778769dSNoriaki TAKAMIYA valid_lft = ci->ifa_valid; 3212461d8837SThomas Graf preferred_lft = ci->ifa_prefered; 3213461d8837SThomas Graf } else { 3214461d8837SThomas Graf preferred_lft = INFINITY_LIFE_TIME; 3215461d8837SThomas Graf valid_lft = INFINITY_LIFE_TIME; 32160778769dSNoriaki TAKAMIYA } 32170778769dSNoriaki TAKAMIYA 3218af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifm->ifa_index); 32197198f8ceSThomas Graf if (dev == NULL) 32207198f8ceSThomas Graf return -ENODEV; 3221081bba5bSNoriaki TAKAMIYA 322255ebaef1SNoriaki TAKAMIYA /* We ignore other flags so far. */ 32233b9f9a1cSNoriaki TAKAMIYA ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS); 322455ebaef1SNoriaki TAKAMIYA 32251cab3da6SDaniel Lezcano ifa = ipv6_get_ifaddr(net, pfx, dev, 1); 32267198f8ceSThomas Graf if (ifa == NULL) { 32277198f8ceSThomas Graf /* 32287198f8ceSThomas Graf * It would be best to check for !NLM_F_CREATE here but 32297198f8ceSThomas Graf * userspace alreay relies on not having to provide this. 32307198f8ceSThomas Graf */ 3231af284937SDaniel Lezcano return inet6_addr_add(net, ifm->ifa_index, pfx, 3232af284937SDaniel Lezcano ifm->ifa_prefixlen, ifa_flags, 3233af284937SDaniel Lezcano preferred_lft, valid_lft); 32341da177e4SLinus Torvalds } 32351da177e4SLinus Torvalds 32367198f8ceSThomas Graf if (nlh->nlmsg_flags & NLM_F_EXCL || 32377198f8ceSThomas Graf !(nlh->nlmsg_flags & NLM_F_REPLACE)) 32387198f8ceSThomas Graf err = -EEXIST; 32397198f8ceSThomas Graf else 324055ebaef1SNoriaki TAKAMIYA err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft); 32417198f8ceSThomas Graf 32427198f8ceSThomas Graf in6_ifa_put(ifa); 32437198f8ceSThomas Graf 32447198f8ceSThomas Graf return err; 32457198f8ceSThomas Graf } 32467198f8ceSThomas Graf 3247101bb229SThomas Graf static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags, 3248101bb229SThomas Graf u8 scope, int ifindex) 3249101bb229SThomas Graf { 3250101bb229SThomas Graf struct ifaddrmsg *ifm; 3251101bb229SThomas Graf 3252101bb229SThomas Graf ifm = nlmsg_data(nlh); 3253101bb229SThomas Graf ifm->ifa_family = AF_INET6; 3254101bb229SThomas Graf ifm->ifa_prefixlen = prefixlen; 3255101bb229SThomas Graf ifm->ifa_flags = flags; 3256101bb229SThomas Graf ifm->ifa_scope = scope; 3257101bb229SThomas Graf ifm->ifa_index = ifindex; 3258101bb229SThomas Graf } 3259101bb229SThomas Graf 326085486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp, 326185486af0SThomas Graf unsigned long tstamp, u32 preferred, u32 valid) 326285486af0SThomas Graf { 326385486af0SThomas Graf struct ifa_cacheinfo ci; 326485486af0SThomas Graf 326585486af0SThomas Graf ci.cstamp = (u32)(TIME_DELTA(cstamp, INITIAL_JIFFIES) / HZ * 100 326685486af0SThomas Graf + TIME_DELTA(cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ); 326785486af0SThomas Graf ci.tstamp = (u32)(TIME_DELTA(tstamp, INITIAL_JIFFIES) / HZ * 100 326885486af0SThomas Graf + TIME_DELTA(tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ); 326985486af0SThomas Graf ci.ifa_prefered = preferred; 327085486af0SThomas Graf ci.ifa_valid = valid; 327185486af0SThomas Graf 327285486af0SThomas Graf return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci); 327385486af0SThomas Graf } 327485486af0SThomas Graf 3275101bb229SThomas Graf static inline int rt_scope(int ifa_scope) 3276101bb229SThomas Graf { 3277101bb229SThomas Graf if (ifa_scope & IFA_HOST) 3278101bb229SThomas Graf return RT_SCOPE_HOST; 3279101bb229SThomas Graf else if (ifa_scope & IFA_LINK) 3280101bb229SThomas Graf return RT_SCOPE_LINK; 3281101bb229SThomas Graf else if (ifa_scope & IFA_SITE) 3282101bb229SThomas Graf return RT_SCOPE_SITE; 3283101bb229SThomas Graf else 3284101bb229SThomas Graf return RT_SCOPE_UNIVERSE; 3285101bb229SThomas Graf } 3286101bb229SThomas Graf 32870ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void) 32880ab6803bSThomas Graf { 3289339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifaddrmsg)) 3290339bf98fSThomas Graf + nla_total_size(16) /* IFA_ADDRESS */ 3291339bf98fSThomas Graf + nla_total_size(sizeof(struct ifa_cacheinfo)); 32920ab6803bSThomas Graf } 3293c5396a31SYOSHIFUJI Hideaki 32941da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa, 3295b6544c0bSJamal Hadi Salim u32 pid, u32 seq, int event, unsigned int flags) 32961da177e4SLinus Torvalds { 32971da177e4SLinus Torvalds struct nlmsghdr *nlh; 329885486af0SThomas Graf u32 preferred, valid; 32991da177e4SLinus Torvalds 33000ab6803bSThomas Graf nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags); 33010ab6803bSThomas Graf if (nlh == NULL) 330226932566SPatrick McHardy return -EMSGSIZE; 33030ab6803bSThomas Graf 3304101bb229SThomas Graf put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope), 3305101bb229SThomas Graf ifa->idev->dev->ifindex); 3306101bb229SThomas Graf 33071da177e4SLinus Torvalds if (!(ifa->flags&IFA_F_PERMANENT)) { 330885486af0SThomas Graf preferred = ifa->prefered_lft; 330985486af0SThomas Graf valid = ifa->valid_lft; 331085486af0SThomas Graf if (preferred != INFINITY_LIFE_TIME) { 33111da177e4SLinus Torvalds long tval = (jiffies - ifa->tstamp)/HZ; 331285486af0SThomas Graf preferred -= tval; 331385486af0SThomas Graf if (valid != INFINITY_LIFE_TIME) 331485486af0SThomas Graf valid -= tval; 33151da177e4SLinus Torvalds } 33161da177e4SLinus Torvalds } else { 331785486af0SThomas Graf preferred = INFINITY_LIFE_TIME; 331885486af0SThomas Graf valid = INFINITY_LIFE_TIME; 33191da177e4SLinus Torvalds } 332085486af0SThomas Graf 33210ab6803bSThomas Graf if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 || 332226932566SPatrick McHardy put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) { 332326932566SPatrick McHardy nlmsg_cancel(skb, nlh); 332426932566SPatrick McHardy return -EMSGSIZE; 332526932566SPatrick McHardy } 332685486af0SThomas Graf 33270ab6803bSThomas Graf return nlmsg_end(skb, nlh); 33281da177e4SLinus Torvalds } 33291da177e4SLinus Torvalds 33301da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca, 3331e431b8c0SJamal Hadi Salim u32 pid, u32 seq, int event, u16 flags) 33321da177e4SLinus Torvalds { 33331da177e4SLinus Torvalds struct nlmsghdr *nlh; 3334101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 3335101bb229SThomas Graf int ifindex = ifmca->idev->dev->ifindex; 33361da177e4SLinus Torvalds 33371da177e4SLinus Torvalds if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE) 3338101bb229SThomas Graf scope = RT_SCOPE_SITE; 3339101bb229SThomas Graf 33400ab6803bSThomas Graf nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags); 33410ab6803bSThomas Graf if (nlh == NULL) 334226932566SPatrick McHardy return -EMSGSIZE; 33430ab6803bSThomas Graf 3344101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 33450ab6803bSThomas Graf if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 || 33460ab6803bSThomas Graf put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp, 334726932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 334826932566SPatrick McHardy nlmsg_cancel(skb, nlh); 334926932566SPatrick McHardy return -EMSGSIZE; 335026932566SPatrick McHardy } 335185486af0SThomas Graf 33520ab6803bSThomas Graf return nlmsg_end(skb, nlh); 33531da177e4SLinus Torvalds } 33541da177e4SLinus Torvalds 33551da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca, 3356b6544c0bSJamal Hadi Salim u32 pid, u32 seq, int event, unsigned int flags) 33571da177e4SLinus Torvalds { 33581da177e4SLinus Torvalds struct nlmsghdr *nlh; 3359101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 3360101bb229SThomas Graf int ifindex = ifaca->aca_idev->dev->ifindex; 33611da177e4SLinus Torvalds 33621da177e4SLinus Torvalds if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE) 3363101bb229SThomas Graf scope = RT_SCOPE_SITE; 3364101bb229SThomas Graf 33650ab6803bSThomas Graf nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags); 33660ab6803bSThomas Graf if (nlh == NULL) 336726932566SPatrick McHardy return -EMSGSIZE; 33680ab6803bSThomas Graf 3369101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 33700ab6803bSThomas Graf if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 || 33710ab6803bSThomas Graf put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp, 337226932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 337326932566SPatrick McHardy nlmsg_cancel(skb, nlh); 337426932566SPatrick McHardy return -EMSGSIZE; 337526932566SPatrick McHardy } 337685486af0SThomas Graf 33770ab6803bSThomas Graf return nlmsg_end(skb, nlh); 33781da177e4SLinus Torvalds } 33791da177e4SLinus Torvalds 33801da177e4SLinus Torvalds enum addr_type_t 33811da177e4SLinus Torvalds { 33821da177e4SLinus Torvalds UNICAST_ADDR, 33831da177e4SLinus Torvalds MULTICAST_ADDR, 33841da177e4SLinus Torvalds ANYCAST_ADDR, 33851da177e4SLinus Torvalds }; 33861da177e4SLinus Torvalds 33871da177e4SLinus Torvalds static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb, 33881da177e4SLinus Torvalds enum addr_type_t type) 33891da177e4SLinus Torvalds { 33901da177e4SLinus Torvalds int idx, ip_idx; 33911da177e4SLinus Torvalds int s_idx, s_ip_idx; 33921da177e4SLinus Torvalds int err = 1; 33931da177e4SLinus Torvalds struct net_device *dev; 33941da177e4SLinus Torvalds struct inet6_dev *idev = NULL; 33951da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 33961da177e4SLinus Torvalds struct ifmcaddr6 *ifmca; 33971da177e4SLinus Torvalds struct ifacaddr6 *ifaca; 33983b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 33991da177e4SLinus Torvalds 34001da177e4SLinus Torvalds s_idx = cb->args[0]; 34011da177e4SLinus Torvalds s_ip_idx = ip_idx = cb->args[1]; 34021da177e4SLinus Torvalds 34037562f876SPavel Emelianov idx = 0; 34046fda7350SBenjamin Thery for_each_netdev(net, dev) { 34051da177e4SLinus Torvalds if (idx < s_idx) 34067562f876SPavel Emelianov goto cont; 34071da177e4SLinus Torvalds if (idx > s_idx) 34081da177e4SLinus Torvalds s_ip_idx = 0; 34091da177e4SLinus Torvalds ip_idx = 0; 34101da177e4SLinus Torvalds if ((idev = in6_dev_get(dev)) == NULL) 34117562f876SPavel Emelianov goto cont; 34121da177e4SLinus Torvalds read_lock_bh(&idev->lock); 34131da177e4SLinus Torvalds switch (type) { 34141da177e4SLinus Torvalds case UNICAST_ADDR: 3415ae9cda5dSYOSHIFUJI Hideaki /* unicast address incl. temp addr */ 34161da177e4SLinus Torvalds for (ifa = idev->addr_list; ifa; 34171da177e4SLinus Torvalds ifa = ifa->if_next, ip_idx++) { 34181da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 34191da177e4SLinus Torvalds continue; 34205d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifaddr(skb, ifa, 34211da177e4SLinus Torvalds NETLINK_CB(cb->skb).pid, 34225d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 34235d5619b4SYOSHIFUJI Hideaki RTM_NEWADDR, 34245d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 34251da177e4SLinus Torvalds } 34261da177e4SLinus Torvalds break; 34271da177e4SLinus Torvalds case MULTICAST_ADDR: 34281da177e4SLinus Torvalds /* multicast address */ 34291da177e4SLinus Torvalds for (ifmca = idev->mc_list; ifmca; 34301da177e4SLinus Torvalds ifmca = ifmca->next, ip_idx++) { 34311da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 34321da177e4SLinus Torvalds continue; 34335d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifmcaddr(skb, ifmca, 34341da177e4SLinus Torvalds NETLINK_CB(cb->skb).pid, 34355d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 34365d5619b4SYOSHIFUJI Hideaki RTM_GETMULTICAST, 34375d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 34381da177e4SLinus Torvalds } 34391da177e4SLinus Torvalds break; 34401da177e4SLinus Torvalds case ANYCAST_ADDR: 34411da177e4SLinus Torvalds /* anycast address */ 34421da177e4SLinus Torvalds for (ifaca = idev->ac_list; ifaca; 34431da177e4SLinus Torvalds ifaca = ifaca->aca_next, ip_idx++) { 34441da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 34451da177e4SLinus Torvalds continue; 34465d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifacaddr(skb, ifaca, 34471da177e4SLinus Torvalds NETLINK_CB(cb->skb).pid, 34485d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 34495d5619b4SYOSHIFUJI Hideaki RTM_GETANYCAST, 34505d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 34511da177e4SLinus Torvalds } 34521da177e4SLinus Torvalds break; 34531da177e4SLinus Torvalds default: 34541da177e4SLinus Torvalds break; 34551da177e4SLinus Torvalds } 34561da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 34571da177e4SLinus Torvalds in6_dev_put(idev); 34585d5619b4SYOSHIFUJI Hideaki 34595d5619b4SYOSHIFUJI Hideaki if (err <= 0) 34605d5619b4SYOSHIFUJI Hideaki break; 34617562f876SPavel Emelianov cont: 34627562f876SPavel Emelianov idx++; 34631da177e4SLinus Torvalds } 34641da177e4SLinus Torvalds cb->args[0] = idx; 34651da177e4SLinus Torvalds cb->args[1] = ip_idx; 34661da177e4SLinus Torvalds return skb->len; 34671da177e4SLinus Torvalds } 34681da177e4SLinus Torvalds 34691da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) 34701da177e4SLinus Torvalds { 34711da177e4SLinus Torvalds enum addr_type_t type = UNICAST_ADDR; 3472b854272bSDenis V. Lunev 34731da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 34741da177e4SLinus Torvalds } 34751da177e4SLinus Torvalds 34761da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb) 34771da177e4SLinus Torvalds { 34781da177e4SLinus Torvalds enum addr_type_t type = MULTICAST_ADDR; 3479b854272bSDenis V. Lunev 34801da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 34811da177e4SLinus Torvalds } 34821da177e4SLinus Torvalds 34831da177e4SLinus Torvalds 34841da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb) 34851da177e4SLinus Torvalds { 34861da177e4SLinus Torvalds enum addr_type_t type = ANYCAST_ADDR; 3487b854272bSDenis V. Lunev 34881da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 34891da177e4SLinus Torvalds } 34901da177e4SLinus Torvalds 34911b29fc2cSThomas Graf static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh, 34921b29fc2cSThomas Graf void *arg) 34936c223828SNoriaki TAKAMIYA { 34943b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 34951b29fc2cSThomas Graf struct ifaddrmsg *ifm; 34961b29fc2cSThomas Graf struct nlattr *tb[IFA_MAX+1]; 34976c223828SNoriaki TAKAMIYA struct in6_addr *addr = NULL; 34986c223828SNoriaki TAKAMIYA struct net_device *dev = NULL; 34996c223828SNoriaki TAKAMIYA struct inet6_ifaddr *ifa; 35006c223828SNoriaki TAKAMIYA struct sk_buff *skb; 35016c223828SNoriaki TAKAMIYA int err; 35026c223828SNoriaki TAKAMIYA 35031b29fc2cSThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 35041b29fc2cSThomas Graf if (err < 0) 35051b29fc2cSThomas Graf goto errout; 35066c223828SNoriaki TAKAMIYA 35071b29fc2cSThomas Graf addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]); 35081b29fc2cSThomas Graf if (addr == NULL) { 35091b29fc2cSThomas Graf err = -EINVAL; 35101b29fc2cSThomas Graf goto errout; 35111b29fc2cSThomas Graf } 35121b29fc2cSThomas Graf 35131b29fc2cSThomas Graf ifm = nlmsg_data(nlh); 35146c223828SNoriaki TAKAMIYA if (ifm->ifa_index) 35156fda7350SBenjamin Thery dev = __dev_get_by_index(net, ifm->ifa_index); 35166c223828SNoriaki TAKAMIYA 35171cab3da6SDaniel Lezcano if ((ifa = ipv6_get_ifaddr(net, addr, dev, 1)) == NULL) { 35181b29fc2cSThomas Graf err = -EADDRNOTAVAIL; 35191b29fc2cSThomas Graf goto errout; 35206c223828SNoriaki TAKAMIYA } 35216c223828SNoriaki TAKAMIYA 35220ab6803bSThomas Graf if ((skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL)) == NULL) { 35231b29fc2cSThomas Graf err = -ENOBUFS; 35241b29fc2cSThomas Graf goto errout_ifa; 35251b29fc2cSThomas Graf } 35261b29fc2cSThomas Graf 35276c223828SNoriaki TAKAMIYA err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).pid, 35286c223828SNoriaki TAKAMIYA nlh->nlmsg_seq, RTM_NEWADDR, 0); 352926932566SPatrick McHardy if (err < 0) { 353026932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 353126932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 353226932566SPatrick McHardy kfree_skb(skb); 353326932566SPatrick McHardy goto errout_ifa; 353426932566SPatrick McHardy } 35356fda7350SBenjamin Thery err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid); 35361b29fc2cSThomas Graf errout_ifa: 35376c223828SNoriaki TAKAMIYA in6_ifa_put(ifa); 35381b29fc2cSThomas Graf errout: 35396c223828SNoriaki TAKAMIYA return err; 35406c223828SNoriaki TAKAMIYA } 35416c223828SNoriaki TAKAMIYA 35421da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa) 35431da177e4SLinus Torvalds { 35441da177e4SLinus Torvalds struct sk_buff *skb; 3545c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(ifa->idev->dev); 35465d620266SThomas Graf int err = -ENOBUFS; 35471da177e4SLinus Torvalds 35480ab6803bSThomas Graf skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC); 35495d620266SThomas Graf if (skb == NULL) 35505d620266SThomas Graf goto errout; 35515d620266SThomas Graf 35525d620266SThomas Graf err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0); 355326932566SPatrick McHardy if (err < 0) { 355426932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 355526932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 355626932566SPatrick McHardy kfree_skb(skb); 355726932566SPatrick McHardy goto errout; 355826932566SPatrick McHardy } 35596fda7350SBenjamin Thery err = rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC); 35605d620266SThomas Graf errout: 35615d620266SThomas Graf if (err < 0) 35626fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err); 35631da177e4SLinus Torvalds } 35641da177e4SLinus Torvalds 3565b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf, 35661da177e4SLinus Torvalds __s32 *array, int bytes) 35671da177e4SLinus Torvalds { 356804561c1fSThomas Graf BUG_ON(bytes < (DEVCONF_MAX * 4)); 356904561c1fSThomas Graf 35701da177e4SLinus Torvalds memset(array, 0, bytes); 35711da177e4SLinus Torvalds array[DEVCONF_FORWARDING] = cnf->forwarding; 35721da177e4SLinus Torvalds array[DEVCONF_HOPLIMIT] = cnf->hop_limit; 35731da177e4SLinus Torvalds array[DEVCONF_MTU6] = cnf->mtu6; 35741da177e4SLinus Torvalds array[DEVCONF_ACCEPT_RA] = cnf->accept_ra; 35751da177e4SLinus Torvalds array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects; 35761da177e4SLinus Torvalds array[DEVCONF_AUTOCONF] = cnf->autoconf; 35771da177e4SLinus Torvalds array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits; 35781da177e4SLinus Torvalds array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits; 35791da177e4SLinus Torvalds array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval; 35801da177e4SLinus Torvalds array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay; 35811da177e4SLinus Torvalds array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version; 35821da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 35831da177e4SLinus Torvalds array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr; 35841da177e4SLinus Torvalds array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft; 35851da177e4SLinus Torvalds array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft; 35861da177e4SLinus Torvalds array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry; 35871da177e4SLinus Torvalds array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor; 35881da177e4SLinus Torvalds #endif 35891da177e4SLinus Torvalds array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses; 359065f5c7c1SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr; 3591c4fd30ebSYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo; 3592930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 3593930d6ff2SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref; 359452e16356SYOSHIFUJI Hideaki array[DEVCONF_RTR_PROBE_INTERVAL] = cnf->rtr_probe_interval; 3595fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 359609c884d4SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen; 359709c884d4SYOSHIFUJI Hideaki #endif 3598930d6ff2SYOSHIFUJI Hideaki #endif 3599fbea49e1SYOSHIFUJI Hideaki array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp; 36000bcbc926SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route; 360195c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 360295c385b4SNeil Horman array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad; 360395c385b4SNeil Horman #endif 36047bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 36057bc570c8SYOSHIFUJI Hideaki array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding; 36067bc570c8SYOSHIFUJI Hideaki #endif 36071da177e4SLinus Torvalds } 36081da177e4SLinus Torvalds 3609339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void) 3610339bf98fSThomas Graf { 3611339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 3612339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 3613339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 3614339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 3615339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 3616339bf98fSThomas Graf + nla_total_size( /* IFLA_PROTINFO */ 3617339bf98fSThomas Graf nla_total_size(4) /* IFLA_INET6_FLAGS */ 3618339bf98fSThomas Graf + nla_total_size(sizeof(struct ifla_cacheinfo)) 3619339bf98fSThomas Graf + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */ 3620bf99f1bdSYOSHIFUJI Hideaki + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */ 3621bf99f1bdSYOSHIFUJI Hideaki + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */ 3622339bf98fSThomas Graf ); 3623339bf98fSThomas Graf } 3624c5396a31SYOSHIFUJI Hideaki 36257f7d9a6bSHerbert Xu static inline void __snmp6_fill_stats(u64 *stats, void **mib, int items, 36267f7d9a6bSHerbert Xu int bytes) 36277f7d9a6bSHerbert Xu { 36287f7d9a6bSHerbert Xu int i; 36297f7d9a6bSHerbert Xu int pad = bytes - sizeof(u64) * items; 36307f7d9a6bSHerbert Xu BUG_ON(pad < 0); 36317f7d9a6bSHerbert Xu 36327f7d9a6bSHerbert Xu /* Use put_unaligned() because stats may not be aligned for u64. */ 36337f7d9a6bSHerbert Xu put_unaligned(items, &stats[0]); 36347f7d9a6bSHerbert Xu for (i = 1; i < items; i++) 36357f7d9a6bSHerbert Xu put_unaligned(snmp_fold_field(mib, i), &stats[i]); 36367f7d9a6bSHerbert Xu 36377f7d9a6bSHerbert Xu memset(&stats[items], 0, pad); 36387f7d9a6bSHerbert Xu } 36397f7d9a6bSHerbert Xu 36407f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype, 36417f7d9a6bSHerbert Xu int bytes) 36427f7d9a6bSHerbert Xu { 36437f7d9a6bSHerbert Xu switch(attrtype) { 36447f7d9a6bSHerbert Xu case IFLA_INET6_STATS: 36457f7d9a6bSHerbert Xu __snmp6_fill_stats(stats, (void **)idev->stats.ipv6, IPSTATS_MIB_MAX, bytes); 36467f7d9a6bSHerbert Xu break; 36477f7d9a6bSHerbert Xu case IFLA_INET6_ICMP6STATS: 36487f7d9a6bSHerbert Xu __snmp6_fill_stats(stats, (void **)idev->stats.icmpv6, ICMP6_MIB_MAX, bytes); 36497f7d9a6bSHerbert Xu break; 36507f7d9a6bSHerbert Xu } 36517f7d9a6bSHerbert Xu } 36527f7d9a6bSHerbert Xu 36531da177e4SLinus Torvalds static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev, 3654b6544c0bSJamal Hadi Salim u32 pid, u32 seq, int event, unsigned int flags) 36551da177e4SLinus Torvalds { 36561da177e4SLinus Torvalds struct net_device *dev = idev->dev; 3657bf99f1bdSYOSHIFUJI Hideaki struct nlattr *nla; 365804561c1fSThomas Graf struct ifinfomsg *hdr; 36591da177e4SLinus Torvalds struct nlmsghdr *nlh; 366004561c1fSThomas Graf void *protoinfo; 36611da177e4SLinus Torvalds struct ifla_cacheinfo ci; 36621da177e4SLinus Torvalds 366304561c1fSThomas Graf nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags); 366404561c1fSThomas Graf if (nlh == NULL) 366526932566SPatrick McHardy return -EMSGSIZE; 36661da177e4SLinus Torvalds 366704561c1fSThomas Graf hdr = nlmsg_data(nlh); 366804561c1fSThomas Graf hdr->ifi_family = AF_INET6; 366904561c1fSThomas Graf hdr->__ifi_pad = 0; 367004561c1fSThomas Graf hdr->ifi_type = dev->type; 367104561c1fSThomas Graf hdr->ifi_index = dev->ifindex; 367204561c1fSThomas Graf hdr->ifi_flags = dev_get_flags(dev); 367304561c1fSThomas Graf hdr->ifi_change = 0; 367404561c1fSThomas Graf 367504561c1fSThomas Graf NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name); 36761da177e4SLinus Torvalds 36771da177e4SLinus Torvalds if (dev->addr_len) 367804561c1fSThomas Graf NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr); 36791da177e4SLinus Torvalds 368004561c1fSThomas Graf NLA_PUT_U32(skb, IFLA_MTU, dev->mtu); 36811da177e4SLinus Torvalds if (dev->ifindex != dev->iflink) 368204561c1fSThomas Graf NLA_PUT_U32(skb, IFLA_LINK, dev->iflink); 36831da177e4SLinus Torvalds 368404561c1fSThomas Graf protoinfo = nla_nest_start(skb, IFLA_PROTINFO); 368504561c1fSThomas Graf if (protoinfo == NULL) 368604561c1fSThomas Graf goto nla_put_failure; 36871da177e4SLinus Torvalds 368804561c1fSThomas Graf NLA_PUT_U32(skb, IFLA_INET6_FLAGS, idev->if_flags); 36891da177e4SLinus Torvalds 36901da177e4SLinus Torvalds ci.max_reasm_len = IPV6_MAXPLEN; 36911da177e4SLinus Torvalds ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100 36921da177e4SLinus Torvalds + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ); 36931da177e4SLinus Torvalds ci.reachable_time = idev->nd_parms->reachable_time; 36941da177e4SLinus Torvalds ci.retrans_time = idev->nd_parms->retrans_time; 369504561c1fSThomas Graf NLA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci); 36961da177e4SLinus Torvalds 3697bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32)); 3698bf99f1bdSYOSHIFUJI Hideaki if (nla == NULL) 369904561c1fSThomas Graf goto nla_put_failure; 3700bf99f1bdSYOSHIFUJI Hideaki ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla)); 37011da177e4SLinus Torvalds 3702bf99f1bdSYOSHIFUJI Hideaki /* XXX - MC not implemented */ 3703bf99f1bdSYOSHIFUJI Hideaki 3704bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64)); 3705bf99f1bdSYOSHIFUJI Hideaki if (nla == NULL) 3706bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 3707bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla)); 3708bf99f1bdSYOSHIFUJI Hideaki 3709bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64)); 3710bf99f1bdSYOSHIFUJI Hideaki if (nla == NULL) 3711bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 3712bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla)); 37131da177e4SLinus Torvalds 371404561c1fSThomas Graf nla_nest_end(skb, protoinfo); 371504561c1fSThomas Graf return nlmsg_end(skb, nlh); 37161da177e4SLinus Torvalds 371704561c1fSThomas Graf nla_put_failure: 371826932566SPatrick McHardy nlmsg_cancel(skb, nlh); 371926932566SPatrick McHardy return -EMSGSIZE; 37201da177e4SLinus Torvalds } 37211da177e4SLinus Torvalds 37221da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 37231da177e4SLinus Torvalds { 37243b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 37251da177e4SLinus Torvalds int idx, err; 37261da177e4SLinus Torvalds int s_idx = cb->args[0]; 37271da177e4SLinus Torvalds struct net_device *dev; 37281da177e4SLinus Torvalds struct inet6_dev *idev; 37291da177e4SLinus Torvalds 37301da177e4SLinus Torvalds read_lock(&dev_base_lock); 37317562f876SPavel Emelianov idx = 0; 37326fda7350SBenjamin Thery for_each_netdev(net, dev) { 37331da177e4SLinus Torvalds if (idx < s_idx) 37347562f876SPavel Emelianov goto cont; 37351da177e4SLinus Torvalds if ((idev = in6_dev_get(dev)) == NULL) 37367562f876SPavel Emelianov goto cont; 37371da177e4SLinus Torvalds err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid, 3738b6544c0bSJamal Hadi Salim cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI); 37391da177e4SLinus Torvalds in6_dev_put(idev); 37401da177e4SLinus Torvalds if (err <= 0) 37411da177e4SLinus Torvalds break; 37427562f876SPavel Emelianov cont: 37437562f876SPavel Emelianov idx++; 37441da177e4SLinus Torvalds } 37451da177e4SLinus Torvalds read_unlock(&dev_base_lock); 37461da177e4SLinus Torvalds cb->args[0] = idx; 37471da177e4SLinus Torvalds 37481da177e4SLinus Torvalds return skb->len; 37491da177e4SLinus Torvalds } 37501da177e4SLinus Torvalds 37511da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev) 37521da177e4SLinus Torvalds { 37531da177e4SLinus Torvalds struct sk_buff *skb; 3754c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 37558d7a76c9SThomas Graf int err = -ENOBUFS; 37561da177e4SLinus Torvalds 3757339bf98fSThomas Graf skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC); 37588d7a76c9SThomas Graf if (skb == NULL) 37598d7a76c9SThomas Graf goto errout; 37608d7a76c9SThomas Graf 37618d7a76c9SThomas Graf err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0); 376226932566SPatrick McHardy if (err < 0) { 376326932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */ 376426932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 376526932566SPatrick McHardy kfree_skb(skb); 376626932566SPatrick McHardy goto errout; 376726932566SPatrick McHardy } 37686fda7350SBenjamin Thery err = rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC); 37698d7a76c9SThomas Graf errout: 37708d7a76c9SThomas Graf if (err < 0) 37716fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err); 37721da177e4SLinus Torvalds } 37731da177e4SLinus Torvalds 3774339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void) 3775339bf98fSThomas Graf { 3776339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct prefixmsg)) 3777339bf98fSThomas Graf + nla_total_size(sizeof(struct in6_addr)) 3778339bf98fSThomas Graf + nla_total_size(sizeof(struct prefix_cacheinfo)); 3779339bf98fSThomas Graf } 3780c5396a31SYOSHIFUJI Hideaki 37811da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev, 3782b6544c0bSJamal Hadi Salim struct prefix_info *pinfo, u32 pid, u32 seq, 3783b6544c0bSJamal Hadi Salim int event, unsigned int flags) 37841da177e4SLinus Torvalds { 37851da177e4SLinus Torvalds struct prefixmsg *pmsg; 37861da177e4SLinus Torvalds struct nlmsghdr *nlh; 37871da177e4SLinus Torvalds struct prefix_cacheinfo ci; 37881da177e4SLinus Torvalds 37896051e2f4SThomas Graf nlh = nlmsg_put(skb, pid, seq, event, sizeof(*pmsg), flags); 37906051e2f4SThomas Graf if (nlh == NULL) 379126932566SPatrick McHardy return -EMSGSIZE; 37926051e2f4SThomas Graf 37936051e2f4SThomas Graf pmsg = nlmsg_data(nlh); 37941da177e4SLinus Torvalds pmsg->prefix_family = AF_INET6; 37958a47077aSPatrick McHardy pmsg->prefix_pad1 = 0; 37968a47077aSPatrick McHardy pmsg->prefix_pad2 = 0; 37971da177e4SLinus Torvalds pmsg->prefix_ifindex = idev->dev->ifindex; 37981da177e4SLinus Torvalds pmsg->prefix_len = pinfo->prefix_len; 37991da177e4SLinus Torvalds pmsg->prefix_type = pinfo->type; 38008a47077aSPatrick McHardy pmsg->prefix_pad3 = 0; 38011da177e4SLinus Torvalds pmsg->prefix_flags = 0; 38021da177e4SLinus Torvalds if (pinfo->onlink) 38031da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_ONLINK; 38041da177e4SLinus Torvalds if (pinfo->autoconf) 38051da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_AUTOCONF; 38061da177e4SLinus Torvalds 38076051e2f4SThomas Graf NLA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix); 38081da177e4SLinus Torvalds 38091da177e4SLinus Torvalds ci.preferred_time = ntohl(pinfo->prefered); 38101da177e4SLinus Torvalds ci.valid_time = ntohl(pinfo->valid); 38116051e2f4SThomas Graf NLA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci); 38121da177e4SLinus Torvalds 38136051e2f4SThomas Graf return nlmsg_end(skb, nlh); 38141da177e4SLinus Torvalds 38156051e2f4SThomas Graf nla_put_failure: 381626932566SPatrick McHardy nlmsg_cancel(skb, nlh); 381726932566SPatrick McHardy return -EMSGSIZE; 38181da177e4SLinus Torvalds } 38191da177e4SLinus Torvalds 38201da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev, 38211da177e4SLinus Torvalds struct prefix_info *pinfo) 38221da177e4SLinus Torvalds { 38231da177e4SLinus Torvalds struct sk_buff *skb; 3824c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 38258c384bfaSThomas Graf int err = -ENOBUFS; 38261da177e4SLinus Torvalds 3827339bf98fSThomas Graf skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC); 38288c384bfaSThomas Graf if (skb == NULL) 38298c384bfaSThomas Graf goto errout; 38308c384bfaSThomas Graf 38318c384bfaSThomas Graf err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0); 383226932566SPatrick McHardy if (err < 0) { 383326932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */ 383426932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 383526932566SPatrick McHardy kfree_skb(skb); 383626932566SPatrick McHardy goto errout; 383726932566SPatrick McHardy } 38386fda7350SBenjamin Thery err = rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC); 38398c384bfaSThomas Graf errout: 38408c384bfaSThomas Graf if (err < 0) 38416fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err); 38421da177e4SLinus Torvalds } 38431da177e4SLinus Torvalds 38441da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 38451da177e4SLinus Torvalds { 38461da177e4SLinus Torvalds inet6_ifa_notify(event ? : RTM_NEWADDR, ifp); 38471da177e4SLinus Torvalds 38481da177e4SLinus Torvalds switch (event) { 38491da177e4SLinus Torvalds case RTM_NEWADDR: 385095c385b4SNeil Horman /* 385195c385b4SNeil Horman * If the address was optimistic 385295c385b4SNeil Horman * we inserted the route at the start of 385395c385b4SNeil Horman * our DAD process, so we don't need 385495c385b4SNeil Horman * to do it again 385595c385b4SNeil Horman */ 385695c385b4SNeil Horman if (!(ifp->rt->rt6i_node)) 385740e22e8fSThomas Graf ip6_ins_rt(ifp->rt); 38581da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 38591da177e4SLinus Torvalds addrconf_join_anycast(ifp); 38601da177e4SLinus Torvalds break; 38611da177e4SLinus Torvalds case RTM_DELADDR: 38621da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 38631da177e4SLinus Torvalds addrconf_leave_anycast(ifp); 38641da177e4SLinus Torvalds addrconf_leave_solict(ifp->idev, &ifp->addr); 38651da177e4SLinus Torvalds dst_hold(&ifp->rt->u.dst); 3866e0a1ad73SThomas Graf if (ip6_del_rt(ifp->rt)) 38671da177e4SLinus Torvalds dst_free(&ifp->rt->u.dst); 38681da177e4SLinus Torvalds break; 38691da177e4SLinus Torvalds } 38701da177e4SLinus Torvalds } 38711da177e4SLinus Torvalds 38721da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 38731da177e4SLinus Torvalds { 38748814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 38751da177e4SLinus Torvalds if (likely(ifp->idev->dead == 0)) 38761da177e4SLinus Torvalds __ipv6_ifa_notify(event, ifp); 38778814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 38781da177e4SLinus Torvalds } 38791da177e4SLinus Torvalds 38801da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 38811da177e4SLinus Torvalds 38821da177e4SLinus Torvalds static 38831da177e4SLinus Torvalds int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp, 38841da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 38851da177e4SLinus Torvalds { 38861da177e4SLinus Torvalds int *valp = ctl->data; 38871da177e4SLinus Torvalds int val = *valp; 38881da177e4SLinus Torvalds int ret; 38891da177e4SLinus Torvalds 38901da177e4SLinus Torvalds ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos); 38911da177e4SLinus Torvalds 3892c8fecf22SPavel Emelyanov if (write) 3893c8fecf22SPavel Emelyanov addrconf_fixup_forwarding(ctl, valp, val); 38941da177e4SLinus Torvalds return ret; 38951da177e4SLinus Torvalds } 38961da177e4SLinus Torvalds 38971da177e4SLinus Torvalds static int addrconf_sysctl_forward_strategy(ctl_table *table, 38981da177e4SLinus Torvalds int __user *name, int nlen, 38991da177e4SLinus Torvalds void __user *oldval, 39001da177e4SLinus Torvalds size_t __user *oldlenp, 39011f29bcd7SAlexey Dobriyan void __user *newval, size_t newlen) 39021da177e4SLinus Torvalds { 39031da177e4SLinus Torvalds int *valp = table->data; 3904c8fecf22SPavel Emelyanov int val = *valp; 39051da177e4SLinus Torvalds int new; 39061da177e4SLinus Torvalds 39071da177e4SLinus Torvalds if (!newval || !newlen) 39081da177e4SLinus Torvalds return 0; 39091da177e4SLinus Torvalds if (newlen != sizeof(int)) 39101da177e4SLinus Torvalds return -EINVAL; 39111da177e4SLinus Torvalds if (get_user(new, (int __user *)newval)) 39121da177e4SLinus Torvalds return -EFAULT; 39131da177e4SLinus Torvalds if (new == *valp) 39141da177e4SLinus Torvalds return 0; 39151da177e4SLinus Torvalds if (oldval && oldlenp) { 39161da177e4SLinus Torvalds size_t len; 39171da177e4SLinus Torvalds if (get_user(len, oldlenp)) 39181da177e4SLinus Torvalds return -EFAULT; 39191da177e4SLinus Torvalds if (len) { 39201da177e4SLinus Torvalds if (len > table->maxlen) 39211da177e4SLinus Torvalds len = table->maxlen; 39221da177e4SLinus Torvalds if (copy_to_user(oldval, valp, len)) 39231da177e4SLinus Torvalds return -EFAULT; 39241da177e4SLinus Torvalds if (put_user(len, oldlenp)) 39251da177e4SLinus Torvalds return -EFAULT; 39261da177e4SLinus Torvalds } 39271da177e4SLinus Torvalds } 39281da177e4SLinus Torvalds 39291da177e4SLinus Torvalds *valp = new; 3930c8fecf22SPavel Emelyanov addrconf_fixup_forwarding(table, valp, val); 39311da177e4SLinus Torvalds return 1; 39321da177e4SLinus Torvalds } 39331da177e4SLinus Torvalds 39341da177e4SLinus Torvalds static struct addrconf_sysctl_table 39351da177e4SLinus Torvalds { 39361da177e4SLinus Torvalds struct ctl_table_header *sysctl_header; 3937f6a07b29SYOSHIFUJI Hideaki ctl_table addrconf_vars[DEVCONF_MAX+1]; 39381dab6222SPavel Emelyanov char *dev_name; 3939ab32ea5dSBrian Haley } addrconf_sysctl __read_mostly = { 39401da177e4SLinus Torvalds .sysctl_header = NULL, 39411da177e4SLinus Torvalds .addrconf_vars = { 39421da177e4SLinus Torvalds { 39431da177e4SLinus Torvalds .ctl_name = NET_IPV6_FORWARDING, 39441da177e4SLinus Torvalds .procname = "forwarding", 39451da177e4SLinus Torvalds .data = &ipv6_devconf.forwarding, 39461da177e4SLinus Torvalds .maxlen = sizeof(int), 39471da177e4SLinus Torvalds .mode = 0644, 39481da177e4SLinus Torvalds .proc_handler = &addrconf_sysctl_forward, 39491da177e4SLinus Torvalds .strategy = &addrconf_sysctl_forward_strategy, 39501da177e4SLinus Torvalds }, 39511da177e4SLinus Torvalds { 39521da177e4SLinus Torvalds .ctl_name = NET_IPV6_HOP_LIMIT, 39531da177e4SLinus Torvalds .procname = "hop_limit", 39541da177e4SLinus Torvalds .data = &ipv6_devconf.hop_limit, 39551da177e4SLinus Torvalds .maxlen = sizeof(int), 39561da177e4SLinus Torvalds .mode = 0644, 39571da177e4SLinus Torvalds .proc_handler = proc_dointvec, 39581da177e4SLinus Torvalds }, 39591da177e4SLinus Torvalds { 39601da177e4SLinus Torvalds .ctl_name = NET_IPV6_MTU, 39611da177e4SLinus Torvalds .procname = "mtu", 39621da177e4SLinus Torvalds .data = &ipv6_devconf.mtu6, 39631da177e4SLinus Torvalds .maxlen = sizeof(int), 39641da177e4SLinus Torvalds .mode = 0644, 39651da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 39661da177e4SLinus Torvalds }, 39671da177e4SLinus Torvalds { 39681da177e4SLinus Torvalds .ctl_name = NET_IPV6_ACCEPT_RA, 39691da177e4SLinus Torvalds .procname = "accept_ra", 39701da177e4SLinus Torvalds .data = &ipv6_devconf.accept_ra, 39711da177e4SLinus Torvalds .maxlen = sizeof(int), 39721da177e4SLinus Torvalds .mode = 0644, 39731da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 39741da177e4SLinus Torvalds }, 39751da177e4SLinus Torvalds { 39761da177e4SLinus Torvalds .ctl_name = NET_IPV6_ACCEPT_REDIRECTS, 39771da177e4SLinus Torvalds .procname = "accept_redirects", 39781da177e4SLinus Torvalds .data = &ipv6_devconf.accept_redirects, 39791da177e4SLinus Torvalds .maxlen = sizeof(int), 39801da177e4SLinus Torvalds .mode = 0644, 39811da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 39821da177e4SLinus Torvalds }, 39831da177e4SLinus Torvalds { 39841da177e4SLinus Torvalds .ctl_name = NET_IPV6_AUTOCONF, 39851da177e4SLinus Torvalds .procname = "autoconf", 39861da177e4SLinus Torvalds .data = &ipv6_devconf.autoconf, 39871da177e4SLinus Torvalds .maxlen = sizeof(int), 39881da177e4SLinus Torvalds .mode = 0644, 39891da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 39901da177e4SLinus Torvalds }, 39911da177e4SLinus Torvalds { 39921da177e4SLinus Torvalds .ctl_name = NET_IPV6_DAD_TRANSMITS, 39931da177e4SLinus Torvalds .procname = "dad_transmits", 39941da177e4SLinus Torvalds .data = &ipv6_devconf.dad_transmits, 39951da177e4SLinus Torvalds .maxlen = sizeof(int), 39961da177e4SLinus Torvalds .mode = 0644, 39971da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 39981da177e4SLinus Torvalds }, 39991da177e4SLinus Torvalds { 40001da177e4SLinus Torvalds .ctl_name = NET_IPV6_RTR_SOLICITS, 40011da177e4SLinus Torvalds .procname = "router_solicitations", 40021da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicits, 40031da177e4SLinus Torvalds .maxlen = sizeof(int), 40041da177e4SLinus Torvalds .mode = 0644, 40051da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 40061da177e4SLinus Torvalds }, 40071da177e4SLinus Torvalds { 40081da177e4SLinus Torvalds .ctl_name = NET_IPV6_RTR_SOLICIT_INTERVAL, 40091da177e4SLinus Torvalds .procname = "router_solicitation_interval", 40101da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_interval, 40111da177e4SLinus Torvalds .maxlen = sizeof(int), 40121da177e4SLinus Torvalds .mode = 0644, 40131da177e4SLinus Torvalds .proc_handler = &proc_dointvec_jiffies, 40141da177e4SLinus Torvalds .strategy = &sysctl_jiffies, 40151da177e4SLinus Torvalds }, 40161da177e4SLinus Torvalds { 40171da177e4SLinus Torvalds .ctl_name = NET_IPV6_RTR_SOLICIT_DELAY, 40181da177e4SLinus Torvalds .procname = "router_solicitation_delay", 40191da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_delay, 40201da177e4SLinus Torvalds .maxlen = sizeof(int), 40211da177e4SLinus Torvalds .mode = 0644, 40221da177e4SLinus Torvalds .proc_handler = &proc_dointvec_jiffies, 40231da177e4SLinus Torvalds .strategy = &sysctl_jiffies, 40241da177e4SLinus Torvalds }, 40251da177e4SLinus Torvalds { 40261da177e4SLinus Torvalds .ctl_name = NET_IPV6_FORCE_MLD_VERSION, 40271da177e4SLinus Torvalds .procname = "force_mld_version", 40281da177e4SLinus Torvalds .data = &ipv6_devconf.force_mld_version, 40291da177e4SLinus Torvalds .maxlen = sizeof(int), 40301da177e4SLinus Torvalds .mode = 0644, 40311da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 40321da177e4SLinus Torvalds }, 40331da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 40341da177e4SLinus Torvalds { 40351da177e4SLinus Torvalds .ctl_name = NET_IPV6_USE_TEMPADDR, 40361da177e4SLinus Torvalds .procname = "use_tempaddr", 40371da177e4SLinus Torvalds .data = &ipv6_devconf.use_tempaddr, 40381da177e4SLinus Torvalds .maxlen = sizeof(int), 40391da177e4SLinus Torvalds .mode = 0644, 40401da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 40411da177e4SLinus Torvalds }, 40421da177e4SLinus Torvalds { 40431da177e4SLinus Torvalds .ctl_name = NET_IPV6_TEMP_VALID_LFT, 40441da177e4SLinus Torvalds .procname = "temp_valid_lft", 40451da177e4SLinus Torvalds .data = &ipv6_devconf.temp_valid_lft, 40461da177e4SLinus Torvalds .maxlen = sizeof(int), 40471da177e4SLinus Torvalds .mode = 0644, 40481da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 40491da177e4SLinus Torvalds }, 40501da177e4SLinus Torvalds { 40511da177e4SLinus Torvalds .ctl_name = NET_IPV6_TEMP_PREFERED_LFT, 40521da177e4SLinus Torvalds .procname = "temp_prefered_lft", 40531da177e4SLinus Torvalds .data = &ipv6_devconf.temp_prefered_lft, 40541da177e4SLinus Torvalds .maxlen = sizeof(int), 40551da177e4SLinus Torvalds .mode = 0644, 40561da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 40571da177e4SLinus Torvalds }, 40581da177e4SLinus Torvalds { 40591da177e4SLinus Torvalds .ctl_name = NET_IPV6_REGEN_MAX_RETRY, 40601da177e4SLinus Torvalds .procname = "regen_max_retry", 40611da177e4SLinus Torvalds .data = &ipv6_devconf.regen_max_retry, 40621da177e4SLinus Torvalds .maxlen = sizeof(int), 40631da177e4SLinus Torvalds .mode = 0644, 40641da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 40651da177e4SLinus Torvalds }, 40661da177e4SLinus Torvalds { 40671da177e4SLinus Torvalds .ctl_name = NET_IPV6_MAX_DESYNC_FACTOR, 40681da177e4SLinus Torvalds .procname = "max_desync_factor", 40691da177e4SLinus Torvalds .data = &ipv6_devconf.max_desync_factor, 40701da177e4SLinus Torvalds .maxlen = sizeof(int), 40711da177e4SLinus Torvalds .mode = 0644, 40721da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 40731da177e4SLinus Torvalds }, 40741da177e4SLinus Torvalds #endif 40751da177e4SLinus Torvalds { 40761da177e4SLinus Torvalds .ctl_name = NET_IPV6_MAX_ADDRESSES, 40771da177e4SLinus Torvalds .procname = "max_addresses", 40781da177e4SLinus Torvalds .data = &ipv6_devconf.max_addresses, 40791da177e4SLinus Torvalds .maxlen = sizeof(int), 40801da177e4SLinus Torvalds .mode = 0644, 40811da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 40821da177e4SLinus Torvalds }, 40831da177e4SLinus Torvalds { 408465f5c7c1SYOSHIFUJI Hideaki .ctl_name = NET_IPV6_ACCEPT_RA_DEFRTR, 408565f5c7c1SYOSHIFUJI Hideaki .procname = "accept_ra_defrtr", 408665f5c7c1SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_defrtr, 408765f5c7c1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 408865f5c7c1SYOSHIFUJI Hideaki .mode = 0644, 408965f5c7c1SYOSHIFUJI Hideaki .proc_handler = &proc_dointvec, 409065f5c7c1SYOSHIFUJI Hideaki }, 409165f5c7c1SYOSHIFUJI Hideaki { 4092c4fd30ebSYOSHIFUJI Hideaki .ctl_name = NET_IPV6_ACCEPT_RA_PINFO, 4093c4fd30ebSYOSHIFUJI Hideaki .procname = "accept_ra_pinfo", 4094c4fd30ebSYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_pinfo, 4095c4fd30ebSYOSHIFUJI Hideaki .maxlen = sizeof(int), 4096c4fd30ebSYOSHIFUJI Hideaki .mode = 0644, 4097c4fd30ebSYOSHIFUJI Hideaki .proc_handler = &proc_dointvec, 4098c4fd30ebSYOSHIFUJI Hideaki }, 4099930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 4100930d6ff2SYOSHIFUJI Hideaki { 4101930d6ff2SYOSHIFUJI Hideaki .ctl_name = NET_IPV6_ACCEPT_RA_RTR_PREF, 4102930d6ff2SYOSHIFUJI Hideaki .procname = "accept_ra_rtr_pref", 4103930d6ff2SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rtr_pref, 4104930d6ff2SYOSHIFUJI Hideaki .maxlen = sizeof(int), 4105930d6ff2SYOSHIFUJI Hideaki .mode = 0644, 4106930d6ff2SYOSHIFUJI Hideaki .proc_handler = &proc_dointvec, 4107930d6ff2SYOSHIFUJI Hideaki }, 410852e16356SYOSHIFUJI Hideaki { 410952e16356SYOSHIFUJI Hideaki .ctl_name = NET_IPV6_RTR_PROBE_INTERVAL, 411052e16356SYOSHIFUJI Hideaki .procname = "router_probe_interval", 411152e16356SYOSHIFUJI Hideaki .data = &ipv6_devconf.rtr_probe_interval, 411252e16356SYOSHIFUJI Hideaki .maxlen = sizeof(int), 411352e16356SYOSHIFUJI Hideaki .mode = 0644, 411452e16356SYOSHIFUJI Hideaki .proc_handler = &proc_dointvec_jiffies, 411552e16356SYOSHIFUJI Hideaki .strategy = &sysctl_jiffies, 411652e16356SYOSHIFUJI Hideaki }, 4117fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 411809c884d4SYOSHIFUJI Hideaki { 411909c884d4SYOSHIFUJI Hideaki .ctl_name = NET_IPV6_ACCEPT_RA_RT_INFO_MAX_PLEN, 412009c884d4SYOSHIFUJI Hideaki .procname = "accept_ra_rt_info_max_plen", 412109c884d4SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rt_info_max_plen, 412209c884d4SYOSHIFUJI Hideaki .maxlen = sizeof(int), 412309c884d4SYOSHIFUJI Hideaki .mode = 0644, 412409c884d4SYOSHIFUJI Hideaki .proc_handler = &proc_dointvec, 412509c884d4SYOSHIFUJI Hideaki }, 412609c884d4SYOSHIFUJI Hideaki #endif 4127930d6ff2SYOSHIFUJI Hideaki #endif 4128c4fd30ebSYOSHIFUJI Hideaki { 4129fbea49e1SYOSHIFUJI Hideaki .ctl_name = NET_IPV6_PROXY_NDP, 4130fbea49e1SYOSHIFUJI Hideaki .procname = "proxy_ndp", 4131fbea49e1SYOSHIFUJI Hideaki .data = &ipv6_devconf.proxy_ndp, 4132fbea49e1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 4133fbea49e1SYOSHIFUJI Hideaki .mode = 0644, 4134fbea49e1SYOSHIFUJI Hideaki .proc_handler = &proc_dointvec, 4135fbea49e1SYOSHIFUJI Hideaki }, 4136fbea49e1SYOSHIFUJI Hideaki { 41370bcbc926SYOSHIFUJI Hideaki .ctl_name = NET_IPV6_ACCEPT_SOURCE_ROUTE, 41380bcbc926SYOSHIFUJI Hideaki .procname = "accept_source_route", 41390bcbc926SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_source_route, 41400bcbc926SYOSHIFUJI Hideaki .maxlen = sizeof(int), 41410bcbc926SYOSHIFUJI Hideaki .mode = 0644, 41420bcbc926SYOSHIFUJI Hideaki .proc_handler = &proc_dointvec, 41430bcbc926SYOSHIFUJI Hideaki }, 414495c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 414595c385b4SNeil Horman { 414695c385b4SNeil Horman .ctl_name = CTL_UNNUMBERED, 414795c385b4SNeil Horman .procname = "optimistic_dad", 414895c385b4SNeil Horman .data = &ipv6_devconf.optimistic_dad, 414995c385b4SNeil Horman .maxlen = sizeof(int), 415095c385b4SNeil Horman .mode = 0644, 415195c385b4SNeil Horman .proc_handler = &proc_dointvec, 415295c385b4SNeil Horman 415395c385b4SNeil Horman }, 415495c385b4SNeil Horman #endif 41557bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 41567bc570c8SYOSHIFUJI Hideaki { 41577bc570c8SYOSHIFUJI Hideaki .ctl_name = CTL_UNNUMBERED, 41587bc570c8SYOSHIFUJI Hideaki .procname = "mc_forwarding", 41597bc570c8SYOSHIFUJI Hideaki .data = &ipv6_devconf.mc_forwarding, 41607bc570c8SYOSHIFUJI Hideaki .maxlen = sizeof(int), 41617bc570c8SYOSHIFUJI Hideaki .mode = 0644, 41627bc570c8SYOSHIFUJI Hideaki .proc_handler = &proc_dointvec, 41637bc570c8SYOSHIFUJI Hideaki }, 41647bc570c8SYOSHIFUJI Hideaki #endif 41650bcbc926SYOSHIFUJI Hideaki { 41661da177e4SLinus Torvalds .ctl_name = 0, /* sentinel */ 41671da177e4SLinus Torvalds } 41681da177e4SLinus Torvalds }, 41691da177e4SLinus Torvalds }; 41701da177e4SLinus Torvalds 4171bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name, 4172bff16c2fSPavel Emelyanov int ctl_name, struct inet6_dev *idev, struct ipv6_devconf *p) 41731da177e4SLinus Torvalds { 41741da177e4SLinus Torvalds int i; 41751da177e4SLinus Torvalds struct addrconf_sysctl_table *t; 41761da177e4SLinus Torvalds 41771dab6222SPavel Emelyanov #define ADDRCONF_CTL_PATH_DEV 3 41781dab6222SPavel Emelyanov 41791dab6222SPavel Emelyanov struct ctl_path addrconf_ctl_path[] = { 41801dab6222SPavel Emelyanov { .procname = "net", .ctl_name = CTL_NET, }, 41811dab6222SPavel Emelyanov { .procname = "ipv6", .ctl_name = NET_IPV6, }, 41821dab6222SPavel Emelyanov { .procname = "conf", .ctl_name = NET_IPV6_CONF, }, 41831dab6222SPavel Emelyanov { /* to be set */ }, 41841dab6222SPavel Emelyanov { }, 41851dab6222SPavel Emelyanov }; 41861dab6222SPavel Emelyanov 41871dab6222SPavel Emelyanov 4188af879cc7SArnaldo Carvalho de Melo t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL); 41891da177e4SLinus Torvalds if (t == NULL) 4190f68635e6SPavel Emelyanov goto out; 4191f68635e6SPavel Emelyanov 41921da177e4SLinus Torvalds for (i=0; t->addrconf_vars[i].data; i++) { 41931da177e4SLinus Torvalds t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf; 41941da177e4SLinus Torvalds t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */ 4195bff16c2fSPavel Emelyanov t->addrconf_vars[i].extra2 = net; 41961da177e4SLinus Torvalds } 41971da177e4SLinus Torvalds 41981da177e4SLinus Torvalds /* 41991da177e4SLinus Torvalds * Make a copy of dev_name, because '.procname' is regarded as const 42001da177e4SLinus Torvalds * by sysctl and we wouldn't want anyone to change it under our feet 42011da177e4SLinus Torvalds * (see SIOCSIFNAME). 42021da177e4SLinus Torvalds */ 42031dab6222SPavel Emelyanov t->dev_name = kstrdup(dev_name, GFP_KERNEL); 42041dab6222SPavel Emelyanov if (!t->dev_name) 42051da177e4SLinus Torvalds goto free; 42061da177e4SLinus Torvalds 42071dab6222SPavel Emelyanov addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].procname = t->dev_name; 42081dab6222SPavel Emelyanov addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].ctl_name = ctl_name; 42091da177e4SLinus Torvalds 4210bff16c2fSPavel Emelyanov t->sysctl_header = register_net_sysctl_table(net, addrconf_ctl_path, 42111dab6222SPavel Emelyanov t->addrconf_vars); 42121da177e4SLinus Torvalds if (t->sysctl_header == NULL) 42131da177e4SLinus Torvalds goto free_procname; 4214f68635e6SPavel Emelyanov 42151da177e4SLinus Torvalds p->sysctl = t; 421695897312SPavel Emelyanov return 0; 42171da177e4SLinus Torvalds 42181da177e4SLinus Torvalds free_procname: 42191dab6222SPavel Emelyanov kfree(t->dev_name); 42201da177e4SLinus Torvalds free: 42211da177e4SLinus Torvalds kfree(t); 4222f68635e6SPavel Emelyanov out: 422395897312SPavel Emelyanov return -ENOBUFS; 42241da177e4SLinus Torvalds } 42251da177e4SLinus Torvalds 4226408c4768SPavel Emelyanov static void __addrconf_sysctl_unregister(struct ipv6_devconf *p) 4227f52295a9SPavel Emelyanov { 4228408c4768SPavel Emelyanov struct addrconf_sysctl_table *t; 4229f52295a9SPavel Emelyanov 4230408c4768SPavel Emelyanov if (p->sysctl == NULL) 4231408c4768SPavel Emelyanov return; 4232408c4768SPavel Emelyanov 4233408c4768SPavel Emelyanov t = p->sysctl; 42341da177e4SLinus Torvalds p->sysctl = NULL; 42351da177e4SLinus Torvalds unregister_sysctl_table(t->sysctl_header); 42361dab6222SPavel Emelyanov kfree(t->dev_name); 42371da177e4SLinus Torvalds kfree(t); 42381da177e4SLinus Torvalds } 4239408c4768SPavel Emelyanov 4240408c4768SPavel Emelyanov static void addrconf_sysctl_register(struct inet6_dev *idev) 4241408c4768SPavel Emelyanov { 4242408c4768SPavel Emelyanov neigh_sysctl_register(idev->dev, idev->nd_parms, NET_IPV6, 4243408c4768SPavel Emelyanov NET_IPV6_NEIGH, "ipv6", 4244408c4768SPavel Emelyanov &ndisc_ifinfo_sysctl_change, 4245*0686caa3SYOSHIFUJI Hideaki ndisc_ifinfo_sysctl_strategy); 4246c346dca1SYOSHIFUJI Hideaki __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name, 4247bff16c2fSPavel Emelyanov idev->dev->ifindex, idev, &idev->cnf); 4248408c4768SPavel Emelyanov } 4249408c4768SPavel Emelyanov 4250408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev) 4251408c4768SPavel Emelyanov { 4252408c4768SPavel Emelyanov __addrconf_sysctl_unregister(&idev->cnf); 4253408c4768SPavel Emelyanov neigh_sysctl_unregister(idev->nd_parms); 42541da177e4SLinus Torvalds } 42551da177e4SLinus Torvalds 42561da177e4SLinus Torvalds 42571da177e4SLinus Torvalds #endif 42581da177e4SLinus Torvalds 4259e0da5a48SPavel Emelyanov static int addrconf_init_net(struct net *net) 4260e0da5a48SPavel Emelyanov { 4261e0da5a48SPavel Emelyanov int err; 4262e0da5a48SPavel Emelyanov struct ipv6_devconf *all, *dflt; 4263e0da5a48SPavel Emelyanov 4264e0da5a48SPavel Emelyanov err = -ENOMEM; 4265e0da5a48SPavel Emelyanov all = &ipv6_devconf; 4266e0da5a48SPavel Emelyanov dflt = &ipv6_devconf_dflt; 4267e0da5a48SPavel Emelyanov 4268e0da5a48SPavel Emelyanov if (net != &init_net) { 4269e0da5a48SPavel Emelyanov all = kmemdup(all, sizeof(ipv6_devconf), GFP_KERNEL); 4270e0da5a48SPavel Emelyanov if (all == NULL) 4271e0da5a48SPavel Emelyanov goto err_alloc_all; 4272e0da5a48SPavel Emelyanov 4273e0da5a48SPavel Emelyanov dflt = kmemdup(dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL); 4274e0da5a48SPavel Emelyanov if (dflt == NULL) 4275e0da5a48SPavel Emelyanov goto err_alloc_dflt; 4276e0da5a48SPavel Emelyanov } 4277e0da5a48SPavel Emelyanov 4278e0da5a48SPavel Emelyanov net->ipv6.devconf_all = all; 4279e0da5a48SPavel Emelyanov net->ipv6.devconf_dflt = dflt; 4280e0da5a48SPavel Emelyanov 4281e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 4282e0da5a48SPavel Emelyanov err = __addrconf_sysctl_register(net, "all", NET_PROTO_CONF_ALL, 4283e0da5a48SPavel Emelyanov NULL, all); 4284e0da5a48SPavel Emelyanov if (err < 0) 4285e0da5a48SPavel Emelyanov goto err_reg_all; 4286e0da5a48SPavel Emelyanov 4287e0da5a48SPavel Emelyanov err = __addrconf_sysctl_register(net, "default", NET_PROTO_CONF_DEFAULT, 4288e0da5a48SPavel Emelyanov NULL, dflt); 4289e0da5a48SPavel Emelyanov if (err < 0) 4290e0da5a48SPavel Emelyanov goto err_reg_dflt; 4291e0da5a48SPavel Emelyanov #endif 4292e0da5a48SPavel Emelyanov return 0; 4293e0da5a48SPavel Emelyanov 4294e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 4295e0da5a48SPavel Emelyanov err_reg_dflt: 4296e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(all); 4297e0da5a48SPavel Emelyanov err_reg_all: 4298e0da5a48SPavel Emelyanov kfree(dflt); 4299e0da5a48SPavel Emelyanov #endif 4300e0da5a48SPavel Emelyanov err_alloc_dflt: 4301e0da5a48SPavel Emelyanov kfree(all); 4302e0da5a48SPavel Emelyanov err_alloc_all: 4303e0da5a48SPavel Emelyanov return err; 4304e0da5a48SPavel Emelyanov } 4305e0da5a48SPavel Emelyanov 4306e0da5a48SPavel Emelyanov static void addrconf_exit_net(struct net *net) 4307e0da5a48SPavel Emelyanov { 4308e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 4309e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(net->ipv6.devconf_dflt); 4310e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(net->ipv6.devconf_all); 4311e0da5a48SPavel Emelyanov #endif 4312e0da5a48SPavel Emelyanov if (net != &init_net) { 4313e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_dflt); 4314e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_all); 4315e0da5a48SPavel Emelyanov } 4316e0da5a48SPavel Emelyanov } 4317e0da5a48SPavel Emelyanov 4318e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = { 4319e0da5a48SPavel Emelyanov .init = addrconf_init_net, 4320e0da5a48SPavel Emelyanov .exit = addrconf_exit_net, 4321e0da5a48SPavel Emelyanov }; 4322e0da5a48SPavel Emelyanov 43231da177e4SLinus Torvalds /* 43241da177e4SLinus Torvalds * Device notifier 43251da177e4SLinus Torvalds */ 43261da177e4SLinus Torvalds 43271da177e4SLinus Torvalds int register_inet6addr_notifier(struct notifier_block *nb) 43281da177e4SLinus Torvalds { 4329e041c683SAlan Stern return atomic_notifier_chain_register(&inet6addr_chain, nb); 43301da177e4SLinus Torvalds } 43311da177e4SLinus Torvalds 43327159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(register_inet6addr_notifier); 43337159039aSYOSHIFUJI Hideaki 43341da177e4SLinus Torvalds int unregister_inet6addr_notifier(struct notifier_block *nb) 43351da177e4SLinus Torvalds { 4336e041c683SAlan Stern return atomic_notifier_chain_unregister(&inet6addr_chain,nb); 43371da177e4SLinus Torvalds } 43381da177e4SLinus Torvalds 43397159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(unregister_inet6addr_notifier); 43407159039aSYOSHIFUJI Hideaki 4341af284937SDaniel Lezcano static void addrconf_net_exit(struct net *net) 4342af284937SDaniel Lezcano { 43436fda7350SBenjamin Thery struct net_device *dev; 43446fda7350SBenjamin Thery 43456fda7350SBenjamin Thery rtnl_lock(); 43466fda7350SBenjamin Thery /* clean dev list */ 43476fda7350SBenjamin Thery for_each_netdev(net, dev) { 43486fda7350SBenjamin Thery if (__in6_dev_get(dev) == NULL) 43496fda7350SBenjamin Thery continue; 43506fda7350SBenjamin Thery addrconf_ifdown(dev, 1); 43516fda7350SBenjamin Thery } 43526fda7350SBenjamin Thery addrconf_ifdown(net->loopback_dev, 2); 43536fda7350SBenjamin Thery rtnl_unlock(); 4354af284937SDaniel Lezcano } 4355af284937SDaniel Lezcano 4356af284937SDaniel Lezcano static struct pernet_operations addrconf_net_ops = { 4357af284937SDaniel Lezcano .exit = addrconf_net_exit, 4358af284937SDaniel Lezcano }; 4359af284937SDaniel Lezcano 43601da177e4SLinus Torvalds /* 43611da177e4SLinus Torvalds * Init / cleanup code 43621da177e4SLinus Torvalds */ 43631da177e4SLinus Torvalds 43641da177e4SLinus Torvalds int __init addrconf_init(void) 43651da177e4SLinus Torvalds { 43662a8cc6c8SYOSHIFUJI Hideaki int err; 43672a8cc6c8SYOSHIFUJI Hideaki 43682a8cc6c8SYOSHIFUJI Hideaki if ((err = ipv6_addr_label_init()) < 0) { 43692a8cc6c8SYOSHIFUJI Hideaki printk(KERN_CRIT "IPv6 Addrconf: cannot initialize default policy table: %d.\n", 43702a8cc6c8SYOSHIFUJI Hideaki err); 43712a8cc6c8SYOSHIFUJI Hideaki return err; 43722a8cc6c8SYOSHIFUJI Hideaki } 43731da177e4SLinus Torvalds 4374e0da5a48SPavel Emelyanov register_pernet_subsys(&addrconf_ops); 4375e0da5a48SPavel Emelyanov 43761da177e4SLinus Torvalds /* The addrconf netdev notifier requires that loopback_dev 43771da177e4SLinus Torvalds * has it's ipv6 private information allocated and setup 43781da177e4SLinus Torvalds * before it can bring up and give link-local addresses 43791da177e4SLinus Torvalds * to other devices which are up. 43801da177e4SLinus Torvalds * 43811da177e4SLinus Torvalds * Unfortunately, loopback_dev is not necessarily the first 43821da177e4SLinus Torvalds * entry in the global dev_base list of net devices. In fact, 43831da177e4SLinus Torvalds * it is likely to be the very last entry on that list. 43841da177e4SLinus Torvalds * So this causes the notifier registry below to try and 43851da177e4SLinus Torvalds * give link-local addresses to all devices besides loopback_dev 43861da177e4SLinus Torvalds * first, then loopback_dev, which cases all the non-loopback_dev 43871da177e4SLinus Torvalds * devices to fail to get a link-local address. 43881da177e4SLinus Torvalds * 43891da177e4SLinus Torvalds * So, as a temporary fix, allocate the ipv6 structure for 43901da177e4SLinus Torvalds * loopback_dev first by hand. 43911da177e4SLinus Torvalds * Longer term, all of the dependencies ipv6 has upon the loopback 43921da177e4SLinus Torvalds * device and it being up should be removed. 43931da177e4SLinus Torvalds */ 43941da177e4SLinus Torvalds rtnl_lock(); 43952774c7abSEric W. Biederman if (!ipv6_add_dev(init_net.loopback_dev)) 43961da177e4SLinus Torvalds err = -ENOMEM; 43971da177e4SLinus Torvalds rtnl_unlock(); 43981da177e4SLinus Torvalds if (err) 4399e0da5a48SPavel Emelyanov goto errlo; 44001da177e4SLinus Torvalds 4401af284937SDaniel Lezcano err = register_pernet_device(&addrconf_net_ops); 4402af284937SDaniel Lezcano if (err) 4403af284937SDaniel Lezcano return err; 4404af284937SDaniel Lezcano 44051da177e4SLinus Torvalds register_netdevice_notifier(&ipv6_dev_notf); 44061da177e4SLinus Torvalds 44071da177e4SLinus Torvalds addrconf_verify(0); 4408c127ea2cSThomas Graf 4409c127ea2cSThomas Graf err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo); 4410c127ea2cSThomas Graf if (err < 0) 4411c127ea2cSThomas Graf goto errout; 4412c127ea2cSThomas Graf 4413c127ea2cSThomas Graf /* Only the first call to __rtnl_register can fail */ 4414c127ea2cSThomas Graf __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL); 4415c127ea2cSThomas Graf __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL); 4416c127ea2cSThomas Graf __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr, inet6_dump_ifaddr); 4417c127ea2cSThomas Graf __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL, inet6_dump_ifmcaddr); 4418c127ea2cSThomas Graf __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL, inet6_dump_ifacaddr); 4419c127ea2cSThomas Graf 44202a8cc6c8SYOSHIFUJI Hideaki ipv6_addr_label_rtnl_register(); 44212a8cc6c8SYOSHIFUJI Hideaki 44221da177e4SLinus Torvalds return 0; 4423c127ea2cSThomas Graf errout: 4424c127ea2cSThomas Graf unregister_netdevice_notifier(&ipv6_dev_notf); 4425e0da5a48SPavel Emelyanov errlo: 4426e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 4427c127ea2cSThomas Graf 4428c127ea2cSThomas Graf return err; 44291da177e4SLinus Torvalds } 44301da177e4SLinus Torvalds 443109f7709fSDaniel Lezcano void addrconf_cleanup(void) 44321da177e4SLinus Torvalds { 44331da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 44341da177e4SLinus Torvalds int i; 44351da177e4SLinus Torvalds 44361da177e4SLinus Torvalds unregister_netdevice_notifier(&ipv6_dev_notf); 4437af284937SDaniel Lezcano unregister_pernet_device(&addrconf_net_ops); 44381da177e4SLinus Torvalds 4439e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 44401da177e4SLinus Torvalds 44411da177e4SLinus Torvalds rtnl_lock(); 44421da177e4SLinus Torvalds 44431da177e4SLinus Torvalds /* 44441da177e4SLinus Torvalds * Check hash table. 44451da177e4SLinus Torvalds */ 44461da177e4SLinus Torvalds write_lock_bh(&addrconf_hash_lock); 44471da177e4SLinus Torvalds for (i=0; i < IN6_ADDR_HSIZE; i++) { 44481da177e4SLinus Torvalds for (ifa=inet6_addr_lst[i]; ifa; ) { 44491da177e4SLinus Torvalds struct inet6_ifaddr *bifa; 44501da177e4SLinus Torvalds 44511da177e4SLinus Torvalds bifa = ifa; 44521da177e4SLinus Torvalds ifa = ifa->lst_next; 44531da177e4SLinus Torvalds printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa); 44541da177e4SLinus Torvalds /* Do not free it; something is wrong. 44551da177e4SLinus Torvalds Now we can investigate it with debugger. 44561da177e4SLinus Torvalds */ 44571da177e4SLinus Torvalds } 44581da177e4SLinus Torvalds } 44591da177e4SLinus Torvalds write_unlock_bh(&addrconf_hash_lock); 44601da177e4SLinus Torvalds 44611da177e4SLinus Torvalds del_timer(&addr_chk_timer); 44621da177e4SLinus Torvalds rtnl_unlock(); 4463af284937SDaniel Lezcano 4464af284937SDaniel Lezcano unregister_pernet_subsys(&addrconf_net_ops); 44651da177e4SLinus Torvalds } 4466