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; 2251da177e4SLinus Torvalds 226f24e3d65SMitsuru Chinen /* Check if a valid qdisc is available */ 227f24e3d65SMitsuru Chinen static inline int addrconf_qdisc_ok(struct net_device *dev) 228f24e3d65SMitsuru Chinen { 229f24e3d65SMitsuru Chinen return (dev->qdisc != &noop_qdisc); 230f24e3d65SMitsuru Chinen } 231f24e3d65SMitsuru Chinen 2321da177e4SLinus Torvalds static void addrconf_del_timer(struct inet6_ifaddr *ifp) 2331da177e4SLinus Torvalds { 2341da177e4SLinus Torvalds if (del_timer(&ifp->timer)) 2351da177e4SLinus Torvalds __in6_ifa_put(ifp); 2361da177e4SLinus Torvalds } 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds enum addrconf_timer_t 2391da177e4SLinus Torvalds { 2401da177e4SLinus Torvalds AC_NONE, 2411da177e4SLinus Torvalds AC_DAD, 2421da177e4SLinus Torvalds AC_RS, 2431da177e4SLinus Torvalds }; 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds static void addrconf_mod_timer(struct inet6_ifaddr *ifp, 2461da177e4SLinus Torvalds enum addrconf_timer_t what, 2471da177e4SLinus Torvalds unsigned long when) 2481da177e4SLinus Torvalds { 2491da177e4SLinus Torvalds if (!del_timer(&ifp->timer)) 2501da177e4SLinus Torvalds in6_ifa_hold(ifp); 2511da177e4SLinus Torvalds 2521da177e4SLinus Torvalds switch (what) { 2531da177e4SLinus Torvalds case AC_DAD: 2541da177e4SLinus Torvalds ifp->timer.function = addrconf_dad_timer; 2551da177e4SLinus Torvalds break; 2561da177e4SLinus Torvalds case AC_RS: 2571da177e4SLinus Torvalds ifp->timer.function = addrconf_rs_timer; 2581da177e4SLinus Torvalds break; 2591da177e4SLinus Torvalds default:; 2601da177e4SLinus Torvalds } 2611da177e4SLinus Torvalds ifp->timer.expires = jiffies + when; 2621da177e4SLinus Torvalds add_timer(&ifp->timer); 2631da177e4SLinus Torvalds } 2641da177e4SLinus Torvalds 2657f7d9a6bSHerbert Xu static int snmp6_alloc_dev(struct inet6_dev *idev) 2667f7d9a6bSHerbert Xu { 2677f7d9a6bSHerbert Xu if (snmp_mib_init((void **)idev->stats.ipv6, 268c69bce20SYOSHIFUJI Hideaki sizeof(struct ipstats_mib)) < 0) 2697f7d9a6bSHerbert Xu goto err_ip; 2707f7d9a6bSHerbert Xu if (snmp_mib_init((void **)idev->stats.icmpv6, 271c69bce20SYOSHIFUJI Hideaki sizeof(struct icmpv6_mib)) < 0) 2727f7d9a6bSHerbert Xu goto err_icmp; 27314878f75SDavid L Stevens if (snmp_mib_init((void **)idev->stats.icmpv6msg, 274c69bce20SYOSHIFUJI Hideaki sizeof(struct icmpv6msg_mib)) < 0) 27514878f75SDavid L Stevens goto err_icmpmsg; 2767f7d9a6bSHerbert Xu 2777f7d9a6bSHerbert Xu return 0; 2787f7d9a6bSHerbert Xu 27914878f75SDavid L Stevens err_icmpmsg: 28014878f75SDavid L Stevens snmp_mib_free((void **)idev->stats.icmpv6); 2817f7d9a6bSHerbert Xu err_icmp: 2827f7d9a6bSHerbert Xu snmp_mib_free((void **)idev->stats.ipv6); 2837f7d9a6bSHerbert Xu err_ip: 284aaf70ec7SPavel Emelyanov return -ENOMEM; 2857f7d9a6bSHerbert Xu } 2867f7d9a6bSHerbert Xu 28716910b98SPavel Emelyanov static void snmp6_free_dev(struct inet6_dev *idev) 2887f7d9a6bSHerbert Xu { 28914878f75SDavid L Stevens snmp_mib_free((void **)idev->stats.icmpv6msg); 2907f7d9a6bSHerbert Xu snmp_mib_free((void **)idev->stats.icmpv6); 2917f7d9a6bSHerbert Xu snmp_mib_free((void **)idev->stats.ipv6); 2927f7d9a6bSHerbert Xu } 2937f7d9a6bSHerbert Xu 2941da177e4SLinus Torvalds /* Nobody refers to this device, we may destroy it. */ 2951da177e4SLinus Torvalds 2968814c4b5SYOSHIFUJI Hideaki static void in6_dev_finish_destroy_rcu(struct rcu_head *head) 2978814c4b5SYOSHIFUJI Hideaki { 2988814c4b5SYOSHIFUJI Hideaki struct inet6_dev *idev = container_of(head, struct inet6_dev, rcu); 2998814c4b5SYOSHIFUJI Hideaki kfree(idev); 3008814c4b5SYOSHIFUJI Hideaki } 3018814c4b5SYOSHIFUJI Hideaki 3021da177e4SLinus Torvalds void in6_dev_finish_destroy(struct inet6_dev *idev) 3031da177e4SLinus Torvalds { 3041da177e4SLinus Torvalds struct net_device *dev = idev->dev; 3051da177e4SLinus Torvalds BUG_TRAP(idev->addr_list==NULL); 3061da177e4SLinus Torvalds BUG_TRAP(idev->mc_list==NULL); 3071da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG 3081da177e4SLinus Torvalds printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL"); 3091da177e4SLinus Torvalds #endif 3101da177e4SLinus Torvalds dev_put(dev); 3111da177e4SLinus Torvalds if (!idev->dead) { 3121da177e4SLinus Torvalds printk("Freeing alive inet6 device %p\n", idev); 3131da177e4SLinus Torvalds return; 3141da177e4SLinus Torvalds } 3151da177e4SLinus Torvalds snmp6_free_dev(idev); 3168814c4b5SYOSHIFUJI Hideaki call_rcu(&idev->rcu, in6_dev_finish_destroy_rcu); 3171da177e4SLinus Torvalds } 3181da177e4SLinus Torvalds 3197159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(in6_dev_finish_destroy); 3207159039aSYOSHIFUJI Hideaki 3211da177e4SLinus Torvalds static struct inet6_dev * ipv6_add_dev(struct net_device *dev) 3221da177e4SLinus Torvalds { 3231da177e4SLinus Torvalds struct inet6_dev *ndev; 324d88ae4ccSYOSHIFUJI Hideaki struct in6_addr maddr; 3251da177e4SLinus Torvalds 3261da177e4SLinus Torvalds ASSERT_RTNL(); 3271da177e4SLinus Torvalds 3281da177e4SLinus Torvalds if (dev->mtu < IPV6_MIN_MTU) 3291da177e4SLinus Torvalds return NULL; 3301da177e4SLinus Torvalds 331322f74a4SIngo Oeser ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL); 3321da177e4SLinus Torvalds 333322f74a4SIngo Oeser if (ndev == NULL) 334322f74a4SIngo Oeser return NULL; 3351da177e4SLinus Torvalds 3361da177e4SLinus Torvalds rwlock_init(&ndev->lock); 3371da177e4SLinus Torvalds ndev->dev = dev; 338441fc2a2SPavel Emelyanov memcpy(&ndev->cnf, dev->nd_net->ipv6.devconf_dflt, sizeof(ndev->cnf)); 3391da177e4SLinus Torvalds ndev->cnf.mtu6 = dev->mtu; 3401da177e4SLinus Torvalds ndev->cnf.sysctl = NULL; 3411da177e4SLinus Torvalds ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl); 3421da177e4SLinus Torvalds if (ndev->nd_parms == NULL) { 3431da177e4SLinus Torvalds kfree(ndev); 3441da177e4SLinus Torvalds return NULL; 3451da177e4SLinus Torvalds } 3461da177e4SLinus Torvalds /* We refer to the device */ 3471da177e4SLinus Torvalds dev_hold(dev); 3481da177e4SLinus Torvalds 3491da177e4SLinus Torvalds if (snmp6_alloc_dev(ndev) < 0) { 3501da177e4SLinus Torvalds ADBG((KERN_WARNING 3511da177e4SLinus Torvalds "%s(): cannot allocate memory for statistics; dev=%s.\n", 3521da177e4SLinus Torvalds __FUNCTION__, dev->name)); 3531da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, ndev->nd_parms); 3541da177e4SLinus Torvalds ndev->dead = 1; 3551da177e4SLinus Torvalds in6_dev_finish_destroy(ndev); 3561da177e4SLinus Torvalds return NULL; 3571da177e4SLinus Torvalds } 3581da177e4SLinus Torvalds 3591da177e4SLinus Torvalds if (snmp6_register_dev(ndev) < 0) { 3601da177e4SLinus Torvalds ADBG((KERN_WARNING 3611da177e4SLinus Torvalds "%s(): cannot create /proc/net/dev_snmp6/%s\n", 3621da177e4SLinus Torvalds __FUNCTION__, dev->name)); 3631da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, ndev->nd_parms); 3641da177e4SLinus Torvalds ndev->dead = 1; 3651da177e4SLinus Torvalds in6_dev_finish_destroy(ndev); 3661da177e4SLinus Torvalds return NULL; 3671da177e4SLinus Torvalds } 3681da177e4SLinus Torvalds 3691da177e4SLinus Torvalds /* One reference from device. We must do this before 3701da177e4SLinus Torvalds * we invoke __ipv6_regen_rndid(). 3711da177e4SLinus Torvalds */ 3721da177e4SLinus Torvalds in6_dev_hold(ndev); 3731da177e4SLinus Torvalds 3741da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 375b24b8a24SPavel Emelyanov setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev); 3761da177e4SLinus Torvalds if ((dev->flags&IFF_LOOPBACK) || 3771da177e4SLinus Torvalds dev->type == ARPHRD_TUNNEL || 3780be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 3790be669bbSJoerg Roedel dev->type == ARPHRD_SIT || 3800be669bbSJoerg Roedel #endif 3810be669bbSJoerg Roedel dev->type == ARPHRD_NONE) { 3821da177e4SLinus Torvalds printk(KERN_INFO 383a1493d9cSDavid S. Miller "%s: Disabled Privacy Extensions\n", 384a1493d9cSDavid S. Miller dev->name); 3851da177e4SLinus Torvalds ndev->cnf.use_tempaddr = -1; 386c7dc89c0SFred L. Templin 387c7dc89c0SFred L. Templin if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) { 388c7dc89c0SFred L. Templin printk(KERN_INFO 389c7dc89c0SFred L. Templin "%s: Disabled Multicast RS\n", 390c7dc89c0SFred L. Templin dev->name); 391c7dc89c0SFred L. Templin ndev->cnf.rtr_solicits = 0; 392c7dc89c0SFred L. Templin } 3931da177e4SLinus Torvalds } else { 3941da177e4SLinus Torvalds in6_dev_hold(ndev); 3951da177e4SLinus Torvalds ipv6_regen_rndid((unsigned long) ndev); 3961da177e4SLinus Torvalds } 3971da177e4SLinus Torvalds #endif 3981da177e4SLinus Torvalds 399f24e3d65SMitsuru Chinen if (netif_running(dev) && addrconf_qdisc_ok(dev)) 40053aadcc9SHerbert Xu ndev->if_flags |= IF_READY; 40153aadcc9SHerbert Xu 4021da177e4SLinus Torvalds ipv6_mc_init_dev(ndev); 4031da177e4SLinus Torvalds ndev->tstamp = jiffies; 404f52295a9SPavel Emelyanov addrconf_sysctl_register(ndev); 40530c4cf57SDavid L Stevens /* protected by rtnl_lock */ 40630c4cf57SDavid L Stevens rcu_assign_pointer(dev->ip6_ptr, ndev); 407d88ae4ccSYOSHIFUJI Hideaki 408d88ae4ccSYOSHIFUJI Hideaki /* Join all-node multicast group */ 409d88ae4ccSYOSHIFUJI Hideaki ipv6_addr_all_nodes(&maddr); 410d88ae4ccSYOSHIFUJI Hideaki ipv6_dev_mc_inc(dev, &maddr); 411d88ae4ccSYOSHIFUJI Hideaki 4121da177e4SLinus Torvalds return ndev; 4131da177e4SLinus Torvalds } 4141da177e4SLinus Torvalds 4151da177e4SLinus Torvalds static struct inet6_dev * ipv6_find_idev(struct net_device *dev) 4161da177e4SLinus Torvalds { 4171da177e4SLinus Torvalds struct inet6_dev *idev; 4181da177e4SLinus Torvalds 4191da177e4SLinus Torvalds ASSERT_RTNL(); 4201da177e4SLinus Torvalds 4211da177e4SLinus Torvalds if ((idev = __in6_dev_get(dev)) == NULL) { 4221da177e4SLinus Torvalds if ((idev = ipv6_add_dev(dev)) == NULL) 4231da177e4SLinus Torvalds return NULL; 4241da177e4SLinus Torvalds } 425c5e33bddSYOSHIFUJI Hideaki 4261da177e4SLinus Torvalds if (dev->flags&IFF_UP) 4271da177e4SLinus Torvalds ipv6_mc_up(idev); 4281da177e4SLinus Torvalds return idev; 4291da177e4SLinus Torvalds } 4301da177e4SLinus Torvalds 4311da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 4321da177e4SLinus Torvalds static void dev_forward_change(struct inet6_dev *idev) 4331da177e4SLinus Torvalds { 4341da177e4SLinus Torvalds struct net_device *dev; 4351da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 4361da177e4SLinus Torvalds struct in6_addr addr; 4371da177e4SLinus Torvalds 4381da177e4SLinus Torvalds if (!idev) 4391da177e4SLinus Torvalds return; 4401da177e4SLinus Torvalds dev = idev->dev; 4411da177e4SLinus Torvalds if (dev && (dev->flags & IFF_MULTICAST)) { 4421da177e4SLinus Torvalds ipv6_addr_all_routers(&addr); 4431da177e4SLinus Torvalds 4441da177e4SLinus Torvalds if (idev->cnf.forwarding) 4451da177e4SLinus Torvalds ipv6_dev_mc_inc(dev, &addr); 4461da177e4SLinus Torvalds else 4471da177e4SLinus Torvalds ipv6_dev_mc_dec(dev, &addr); 4481da177e4SLinus Torvalds } 4491da177e4SLinus Torvalds for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) { 4502c12a74cSMichal Wrobel if (ifa->flags&IFA_F_TENTATIVE) 4512c12a74cSMichal Wrobel continue; 4521da177e4SLinus Torvalds if (idev->cnf.forwarding) 4531da177e4SLinus Torvalds addrconf_join_anycast(ifa); 4541da177e4SLinus Torvalds else 4551da177e4SLinus Torvalds addrconf_leave_anycast(ifa); 4561da177e4SLinus Torvalds } 4571da177e4SLinus Torvalds } 4581da177e4SLinus Torvalds 4591da177e4SLinus Torvalds 460e186932bSPavel Emelyanov static void addrconf_forward_change(struct net *net, __s32 newf) 4611da177e4SLinus Torvalds { 4621da177e4SLinus Torvalds struct net_device *dev; 4631da177e4SLinus Torvalds struct inet6_dev *idev; 4641da177e4SLinus Torvalds 4651da177e4SLinus Torvalds read_lock(&dev_base_lock); 466bff16c2fSPavel Emelyanov for_each_netdev(net, dev) { 4678814c4b5SYOSHIFUJI Hideaki rcu_read_lock(); 4681da177e4SLinus Torvalds idev = __in6_dev_get(dev); 4691da177e4SLinus Torvalds if (idev) { 470e186932bSPavel Emelyanov int changed = (!idev->cnf.forwarding) ^ (!newf); 471e186932bSPavel Emelyanov idev->cnf.forwarding = newf; 4721da177e4SLinus Torvalds if (changed) 4731da177e4SLinus Torvalds dev_forward_change(idev); 4741da177e4SLinus Torvalds } 4758814c4b5SYOSHIFUJI Hideaki rcu_read_unlock(); 4761da177e4SLinus Torvalds } 4771da177e4SLinus Torvalds read_unlock(&dev_base_lock); 4781da177e4SLinus Torvalds } 479c8fecf22SPavel Emelyanov 480c8fecf22SPavel Emelyanov static void addrconf_fixup_forwarding(struct ctl_table *table, int *p, int old) 481c8fecf22SPavel Emelyanov { 482bff16c2fSPavel Emelyanov struct net *net; 483bff16c2fSPavel Emelyanov 484bff16c2fSPavel Emelyanov net = (struct net *)table->extra2; 485441fc2a2SPavel Emelyanov if (p == &net->ipv6.devconf_dflt->forwarding) 486c8fecf22SPavel Emelyanov return; 487c8fecf22SPavel Emelyanov 488e186932bSPavel Emelyanov if (p == &net->ipv6.devconf_all->forwarding) { 489e186932bSPavel Emelyanov __s32 newf = net->ipv6.devconf_all->forwarding; 490e186932bSPavel Emelyanov net->ipv6.devconf_dflt->forwarding = newf; 491e186932bSPavel Emelyanov addrconf_forward_change(net, newf); 492c8fecf22SPavel Emelyanov } else if ((!*p) ^ (!old)) 493c8fecf22SPavel Emelyanov dev_forward_change((struct inet6_dev *)table->extra1); 494c8fecf22SPavel Emelyanov 495c8fecf22SPavel Emelyanov if (*p) 496c8fecf22SPavel Emelyanov rt6_purge_dflt_routers(); 497c8fecf22SPavel Emelyanov } 4981da177e4SLinus Torvalds #endif 4991da177e4SLinus Torvalds 5001da177e4SLinus Torvalds /* Nobody refers to this ifaddr, destroy it */ 5011da177e4SLinus Torvalds 5021da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp) 5031da177e4SLinus Torvalds { 5041da177e4SLinus Torvalds BUG_TRAP(ifp->if_next==NULL); 5051da177e4SLinus Torvalds BUG_TRAP(ifp->lst_next==NULL); 5061da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG 5071da177e4SLinus Torvalds printk(KERN_DEBUG "inet6_ifa_finish_destroy\n"); 5081da177e4SLinus Torvalds #endif 5091da177e4SLinus Torvalds 5101da177e4SLinus Torvalds in6_dev_put(ifp->idev); 5111da177e4SLinus Torvalds 5121da177e4SLinus Torvalds if (del_timer(&ifp->timer)) 5131da177e4SLinus Torvalds printk("Timer is still running, when freeing ifa=%p\n", ifp); 5141da177e4SLinus Torvalds 5151da177e4SLinus Torvalds if (!ifp->dead) { 5161da177e4SLinus Torvalds printk("Freeing alive inet6 address %p\n", ifp); 5171da177e4SLinus Torvalds return; 5181da177e4SLinus Torvalds } 5191da177e4SLinus Torvalds dst_release(&ifp->rt->u.dst); 5201da177e4SLinus Torvalds 5211da177e4SLinus Torvalds kfree(ifp); 5221da177e4SLinus Torvalds } 5231da177e4SLinus Torvalds 524e55ffac6SBrian Haley static void 525e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp) 526e55ffac6SBrian Haley { 527e55ffac6SBrian Haley struct inet6_ifaddr *ifa, **ifap; 5288a6ce0c0SYOSHIFUJI Hideaki int ifp_scope = ipv6_addr_src_scope(&ifp->addr); 529e55ffac6SBrian Haley 530e55ffac6SBrian Haley /* 531e55ffac6SBrian Haley * Each device address list is sorted in order of scope - 532e55ffac6SBrian Haley * global before linklocal. 533e55ffac6SBrian Haley */ 534e55ffac6SBrian Haley for (ifap = &idev->addr_list; (ifa = *ifap) != NULL; 535e55ffac6SBrian Haley ifap = &ifa->if_next) { 5368a6ce0c0SYOSHIFUJI Hideaki if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr)) 537e55ffac6SBrian Haley break; 538e55ffac6SBrian Haley } 539e55ffac6SBrian Haley 540e55ffac6SBrian Haley ifp->if_next = *ifap; 541e55ffac6SBrian Haley *ifap = ifp; 542e55ffac6SBrian Haley } 543e55ffac6SBrian Haley 5441da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */ 5451da177e4SLinus Torvalds 5461da177e4SLinus Torvalds static struct inet6_ifaddr * 5471da177e4SLinus Torvalds ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen, 548e431b8c0SJamal Hadi Salim int scope, u32 flags) 5491da177e4SLinus Torvalds { 5501da177e4SLinus Torvalds struct inet6_ifaddr *ifa = NULL; 5511da177e4SLinus Torvalds struct rt6_info *rt; 5521da177e4SLinus Torvalds int hash; 5531da177e4SLinus Torvalds int err = 0; 5541da177e4SLinus Torvalds 5558814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 5561da177e4SLinus Torvalds if (idev->dead) { 5571da177e4SLinus Torvalds err = -ENODEV; /*XXX*/ 5581da177e4SLinus Torvalds goto out2; 5591da177e4SLinus Torvalds } 5601da177e4SLinus Torvalds 5611da177e4SLinus Torvalds write_lock(&addrconf_hash_lock); 5621da177e4SLinus Torvalds 5631da177e4SLinus Torvalds /* Ignore adding duplicate addresses on an interface */ 56406bfe655SDaniel Lezcano if (ipv6_chk_same_addr(&init_net, addr, idev->dev)) { 5651da177e4SLinus Torvalds ADBG(("ipv6_add_addr: already assigned\n")); 5661da177e4SLinus Torvalds err = -EEXIST; 5671da177e4SLinus Torvalds goto out; 5681da177e4SLinus Torvalds } 5691da177e4SLinus Torvalds 570322f74a4SIngo Oeser ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC); 5711da177e4SLinus Torvalds 5721da177e4SLinus Torvalds if (ifa == NULL) { 5731da177e4SLinus Torvalds ADBG(("ipv6_add_addr: malloc failed\n")); 5741da177e4SLinus Torvalds err = -ENOBUFS; 5751da177e4SLinus Torvalds goto out; 5761da177e4SLinus Torvalds } 5771da177e4SLinus Torvalds 5781da177e4SLinus Torvalds rt = addrconf_dst_alloc(idev, addr, 0); 5791da177e4SLinus Torvalds if (IS_ERR(rt)) { 5801da177e4SLinus Torvalds err = PTR_ERR(rt); 5811da177e4SLinus Torvalds goto out; 5821da177e4SLinus Torvalds } 5831da177e4SLinus Torvalds 5841da177e4SLinus Torvalds ipv6_addr_copy(&ifa->addr, addr); 5851da177e4SLinus Torvalds 5861da177e4SLinus Torvalds spin_lock_init(&ifa->lock); 5871da177e4SLinus Torvalds init_timer(&ifa->timer); 5881da177e4SLinus Torvalds ifa->timer.data = (unsigned long) ifa; 5891da177e4SLinus Torvalds ifa->scope = scope; 5901da177e4SLinus Torvalds ifa->prefix_len = pfxlen; 5911da177e4SLinus Torvalds ifa->flags = flags | IFA_F_TENTATIVE; 5921da177e4SLinus Torvalds ifa->cstamp = ifa->tstamp = jiffies; 5931da177e4SLinus Torvalds 59457f5f544SKeir Fraser ifa->rt = rt; 59557f5f544SKeir Fraser 59695c385b4SNeil Horman /* 59795c385b4SNeil Horman * part one of RFC 4429, section 3.3 59895c385b4SNeil Horman * We should not configure an address as 59995c385b4SNeil Horman * optimistic if we do not yet know the link 60095c385b4SNeil Horman * layer address of our nexhop router 60195c385b4SNeil Horman */ 60295c385b4SNeil Horman 60395c385b4SNeil Horman if (rt->rt6i_nexthop == NULL) 60495c385b4SNeil Horman ifa->flags &= ~IFA_F_OPTIMISTIC; 60595c385b4SNeil Horman 6061da177e4SLinus Torvalds ifa->idev = idev; 6071da177e4SLinus Torvalds in6_dev_hold(idev); 6081da177e4SLinus Torvalds /* For caller */ 6091da177e4SLinus Torvalds in6_ifa_hold(ifa); 6101da177e4SLinus Torvalds 6111da177e4SLinus Torvalds /* Add to big hash table */ 6121da177e4SLinus Torvalds hash = ipv6_addr_hash(addr); 6131da177e4SLinus Torvalds 6141da177e4SLinus Torvalds ifa->lst_next = inet6_addr_lst[hash]; 6151da177e4SLinus Torvalds inet6_addr_lst[hash] = ifa; 6161da177e4SLinus Torvalds in6_ifa_hold(ifa); 6171da177e4SLinus Torvalds write_unlock(&addrconf_hash_lock); 6181da177e4SLinus Torvalds 6191da177e4SLinus Torvalds write_lock(&idev->lock); 6201da177e4SLinus Torvalds /* Add to inet6_dev unicast addr list. */ 621e55ffac6SBrian Haley ipv6_link_dev_addr(idev, ifa); 6221da177e4SLinus Torvalds 6231da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 6241da177e4SLinus Torvalds if (ifa->flags&IFA_F_TEMPORARY) { 6251da177e4SLinus Torvalds ifa->tmp_next = idev->tempaddr_list; 6261da177e4SLinus Torvalds idev->tempaddr_list = ifa; 6271da177e4SLinus Torvalds in6_ifa_hold(ifa); 6281da177e4SLinus Torvalds } 6291da177e4SLinus Torvalds #endif 6301da177e4SLinus Torvalds 6311da177e4SLinus Torvalds in6_ifa_hold(ifa); 6321da177e4SLinus Torvalds write_unlock(&idev->lock); 6331da177e4SLinus Torvalds out2: 6348814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 6351da177e4SLinus Torvalds 636fd92833aSYOSHIFUJI Hideaki if (likely(err == 0)) 637e041c683SAlan Stern atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa); 6381da177e4SLinus Torvalds else { 6391da177e4SLinus Torvalds kfree(ifa); 6401da177e4SLinus Torvalds ifa = ERR_PTR(err); 6411da177e4SLinus Torvalds } 6421da177e4SLinus Torvalds 6431da177e4SLinus Torvalds return ifa; 6441da177e4SLinus Torvalds out: 6451da177e4SLinus Torvalds write_unlock(&addrconf_hash_lock); 6461da177e4SLinus Torvalds goto out2; 6471da177e4SLinus Torvalds } 6481da177e4SLinus Torvalds 6491da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */ 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp) 6521da177e4SLinus Torvalds { 6531da177e4SLinus Torvalds struct inet6_ifaddr *ifa, **ifap; 6541da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 6551da177e4SLinus Torvalds int hash; 6561da177e4SLinus Torvalds int deleted = 0, onlink = 0; 6571da177e4SLinus Torvalds unsigned long expires = jiffies; 6581da177e4SLinus Torvalds 6591da177e4SLinus Torvalds hash = ipv6_addr_hash(&ifp->addr); 6601da177e4SLinus Torvalds 6611da177e4SLinus Torvalds ifp->dead = 1; 6621da177e4SLinus Torvalds 6631da177e4SLinus Torvalds write_lock_bh(&addrconf_hash_lock); 6641da177e4SLinus Torvalds for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL; 6651da177e4SLinus Torvalds ifap = &ifa->lst_next) { 6661da177e4SLinus Torvalds if (ifa == ifp) { 6671da177e4SLinus Torvalds *ifap = ifa->lst_next; 6681da177e4SLinus Torvalds __in6_ifa_put(ifp); 6691da177e4SLinus Torvalds ifa->lst_next = NULL; 6701da177e4SLinus Torvalds break; 6711da177e4SLinus Torvalds } 6721da177e4SLinus Torvalds } 6731da177e4SLinus Torvalds write_unlock_bh(&addrconf_hash_lock); 6741da177e4SLinus Torvalds 6751da177e4SLinus Torvalds write_lock_bh(&idev->lock); 6761da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 6771da177e4SLinus Torvalds if (ifp->flags&IFA_F_TEMPORARY) { 6781da177e4SLinus Torvalds for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL; 6791da177e4SLinus Torvalds ifap = &ifa->tmp_next) { 6801da177e4SLinus Torvalds if (ifa == ifp) { 6811da177e4SLinus Torvalds *ifap = ifa->tmp_next; 6821da177e4SLinus Torvalds if (ifp->ifpub) { 6831da177e4SLinus Torvalds in6_ifa_put(ifp->ifpub); 6841da177e4SLinus Torvalds ifp->ifpub = NULL; 6851da177e4SLinus Torvalds } 6861da177e4SLinus Torvalds __in6_ifa_put(ifp); 6871da177e4SLinus Torvalds ifa->tmp_next = NULL; 6881da177e4SLinus Torvalds break; 6891da177e4SLinus Torvalds } 6901da177e4SLinus Torvalds } 6911da177e4SLinus Torvalds } 6921da177e4SLinus Torvalds #endif 6931da177e4SLinus Torvalds 6941d142804SKristian Slavov for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;) { 6951da177e4SLinus Torvalds if (ifa == ifp) { 6961da177e4SLinus Torvalds *ifap = ifa->if_next; 6971da177e4SLinus Torvalds __in6_ifa_put(ifp); 6981da177e4SLinus Torvalds ifa->if_next = NULL; 6991da177e4SLinus Torvalds if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0) 7001da177e4SLinus Torvalds break; 7011da177e4SLinus Torvalds deleted = 1; 7021d142804SKristian Slavov continue; 7031da177e4SLinus Torvalds } else if (ifp->flags & IFA_F_PERMANENT) { 7041da177e4SLinus Torvalds if (ipv6_prefix_equal(&ifa->addr, &ifp->addr, 7051da177e4SLinus Torvalds ifp->prefix_len)) { 7061da177e4SLinus Torvalds if (ifa->flags & IFA_F_PERMANENT) { 7071da177e4SLinus Torvalds onlink = 1; 7081da177e4SLinus Torvalds if (deleted) 7091da177e4SLinus Torvalds break; 7101da177e4SLinus Torvalds } else { 7111da177e4SLinus Torvalds unsigned long lifetime; 7121da177e4SLinus Torvalds 7131da177e4SLinus Torvalds if (!onlink) 7141da177e4SLinus Torvalds onlink = -1; 7151da177e4SLinus Torvalds 7161da177e4SLinus Torvalds spin_lock(&ifa->lock); 7171da177e4SLinus Torvalds lifetime = min_t(unsigned long, 7181da177e4SLinus Torvalds ifa->valid_lft, 0x7fffffffUL/HZ); 7191da177e4SLinus Torvalds if (time_before(expires, 7201da177e4SLinus Torvalds ifa->tstamp + lifetime * HZ)) 7211da177e4SLinus Torvalds expires = ifa->tstamp + lifetime * HZ; 7221da177e4SLinus Torvalds spin_unlock(&ifa->lock); 7231da177e4SLinus Torvalds } 7241da177e4SLinus Torvalds } 7251da177e4SLinus Torvalds } 7261d142804SKristian Slavov ifap = &ifa->if_next; 7271da177e4SLinus Torvalds } 7281da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 7291da177e4SLinus Torvalds 7301da177e4SLinus Torvalds ipv6_ifa_notify(RTM_DELADDR, ifp); 7311da177e4SLinus Torvalds 732e041c683SAlan Stern atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp); 7331da177e4SLinus Torvalds 7341da177e4SLinus Torvalds addrconf_del_timer(ifp); 7351da177e4SLinus Torvalds 7361da177e4SLinus Torvalds /* 7371da177e4SLinus Torvalds * Purge or update corresponding prefix 7381da177e4SLinus Torvalds * 7391da177e4SLinus Torvalds * 1) we don't purge prefix here if address was not permanent. 7401da177e4SLinus Torvalds * prefix is managed by its own lifetime. 7411da177e4SLinus Torvalds * 2) if there're no addresses, delete prefix. 7421da177e4SLinus Torvalds * 3) if there're still other permanent address(es), 7431da177e4SLinus Torvalds * corresponding prefix is still permanent. 7441da177e4SLinus Torvalds * 4) otherwise, update prefix lifetime to the 7451da177e4SLinus Torvalds * longest valid lifetime among the corresponding 7461da177e4SLinus Torvalds * addresses on the device. 7471da177e4SLinus Torvalds * Note: subsequent RA will update lifetime. 7481da177e4SLinus Torvalds * 7491da177e4SLinus Torvalds * --yoshfuji 7501da177e4SLinus Torvalds */ 7511da177e4SLinus Torvalds if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) { 7521da177e4SLinus Torvalds struct in6_addr prefix; 7531da177e4SLinus Torvalds struct rt6_info *rt; 7541da177e4SLinus Torvalds 7551da177e4SLinus Torvalds ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len); 7561da177e4SLinus Torvalds rt = rt6_lookup(&prefix, NULL, ifp->idev->dev->ifindex, 1); 7571da177e4SLinus Torvalds 7581da177e4SLinus Torvalds if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) { 7591da177e4SLinus Torvalds if (onlink == 0) { 760e0a1ad73SThomas Graf ip6_del_rt(rt); 7611da177e4SLinus Torvalds rt = NULL; 7621da177e4SLinus Torvalds } else if (!(rt->rt6i_flags & RTF_EXPIRES)) { 7631da177e4SLinus Torvalds rt->rt6i_expires = expires; 7641da177e4SLinus Torvalds rt->rt6i_flags |= RTF_EXPIRES; 7651da177e4SLinus Torvalds } 7661da177e4SLinus Torvalds } 7671da177e4SLinus Torvalds dst_release(&rt->u.dst); 7681da177e4SLinus Torvalds } 7691da177e4SLinus Torvalds 7701da177e4SLinus Torvalds in6_ifa_put(ifp); 7711da177e4SLinus Torvalds } 7721da177e4SLinus Torvalds 7731da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 7741da177e4SLinus Torvalds static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift) 7751da177e4SLinus Torvalds { 7761da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 7771da177e4SLinus Torvalds struct in6_addr addr, *tmpaddr; 7781da177e4SLinus Torvalds unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp; 7791da177e4SLinus Torvalds int tmp_plen; 7801da177e4SLinus Torvalds int ret = 0; 7811da177e4SLinus Torvalds int max_addresses; 78295c385b4SNeil Horman u32 addr_flags; 7831da177e4SLinus Torvalds 7841da177e4SLinus Torvalds write_lock(&idev->lock); 7851da177e4SLinus Torvalds if (ift) { 7861da177e4SLinus Torvalds spin_lock_bh(&ift->lock); 7871da177e4SLinus Torvalds memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8); 7881da177e4SLinus Torvalds spin_unlock_bh(&ift->lock); 7891da177e4SLinus Torvalds tmpaddr = &addr; 7901da177e4SLinus Torvalds } else { 7911da177e4SLinus Torvalds tmpaddr = NULL; 7921da177e4SLinus Torvalds } 7931da177e4SLinus Torvalds retry: 7941da177e4SLinus Torvalds in6_dev_hold(idev); 7951da177e4SLinus Torvalds if (idev->cnf.use_tempaddr <= 0) { 7961da177e4SLinus Torvalds write_unlock(&idev->lock); 7971da177e4SLinus Torvalds printk(KERN_INFO 7981da177e4SLinus Torvalds "ipv6_create_tempaddr(): use_tempaddr is disabled.\n"); 7991da177e4SLinus Torvalds in6_dev_put(idev); 8001da177e4SLinus Torvalds ret = -1; 8011da177e4SLinus Torvalds goto out; 8021da177e4SLinus Torvalds } 8031da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 8041da177e4SLinus Torvalds if (ifp->regen_count++ >= idev->cnf.regen_max_retry) { 8051da177e4SLinus Torvalds idev->cnf.use_tempaddr = -1; /*XXX*/ 8061da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 8071da177e4SLinus Torvalds write_unlock(&idev->lock); 8081da177e4SLinus Torvalds printk(KERN_WARNING 8091da177e4SLinus Torvalds "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n"); 8101da177e4SLinus Torvalds in6_dev_put(idev); 8111da177e4SLinus Torvalds ret = -1; 8121da177e4SLinus Torvalds goto out; 8131da177e4SLinus Torvalds } 8141da177e4SLinus Torvalds in6_ifa_hold(ifp); 8151da177e4SLinus Torvalds memcpy(addr.s6_addr, ifp->addr.s6_addr, 8); 8161da177e4SLinus Torvalds if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) { 8171da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 8181da177e4SLinus Torvalds write_unlock(&idev->lock); 8191da177e4SLinus Torvalds printk(KERN_WARNING 8201da177e4SLinus Torvalds "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n"); 8211da177e4SLinus Torvalds in6_ifa_put(ifp); 8221da177e4SLinus Torvalds in6_dev_put(idev); 8231da177e4SLinus Torvalds ret = -1; 8241da177e4SLinus Torvalds goto out; 8251da177e4SLinus Torvalds } 8261da177e4SLinus Torvalds memcpy(&addr.s6_addr[8], idev->rndid, 8); 8271da177e4SLinus Torvalds tmp_valid_lft = min_t(__u32, 8281da177e4SLinus Torvalds ifp->valid_lft, 8291da177e4SLinus Torvalds idev->cnf.temp_valid_lft); 8301da177e4SLinus Torvalds tmp_prefered_lft = min_t(__u32, 8311da177e4SLinus Torvalds ifp->prefered_lft, 8321da177e4SLinus Torvalds idev->cnf.temp_prefered_lft - desync_factor / HZ); 8331da177e4SLinus Torvalds tmp_plen = ifp->prefix_len; 8341da177e4SLinus Torvalds max_addresses = idev->cnf.max_addresses; 8351da177e4SLinus Torvalds tmp_cstamp = ifp->cstamp; 8361da177e4SLinus Torvalds tmp_tstamp = ifp->tstamp; 8371da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 8381da177e4SLinus Torvalds 8391da177e4SLinus Torvalds write_unlock(&idev->lock); 84095c385b4SNeil Horman 84195c385b4SNeil Horman addr_flags = IFA_F_TEMPORARY; 84295c385b4SNeil Horman /* set in addrconf_prefix_rcv() */ 84395c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 84495c385b4SNeil Horman addr_flags |= IFA_F_OPTIMISTIC; 84595c385b4SNeil Horman 8461da177e4SLinus Torvalds ift = !max_addresses || 8471da177e4SLinus Torvalds ipv6_count_addresses(idev) < max_addresses ? 8481da177e4SLinus Torvalds ipv6_add_addr(idev, &addr, tmp_plen, 84995c385b4SNeil Horman ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK, 85095c385b4SNeil Horman addr_flags) : NULL; 8511da177e4SLinus Torvalds if (!ift || IS_ERR(ift)) { 8521da177e4SLinus Torvalds in6_ifa_put(ifp); 8531da177e4SLinus Torvalds in6_dev_put(idev); 8541da177e4SLinus Torvalds printk(KERN_INFO 8551da177e4SLinus Torvalds "ipv6_create_tempaddr(): retry temporary address regeneration.\n"); 8561da177e4SLinus Torvalds tmpaddr = &addr; 8571da177e4SLinus Torvalds write_lock(&idev->lock); 8581da177e4SLinus Torvalds goto retry; 8591da177e4SLinus Torvalds } 8601da177e4SLinus Torvalds 8611da177e4SLinus Torvalds spin_lock_bh(&ift->lock); 8621da177e4SLinus Torvalds ift->ifpub = ifp; 8631da177e4SLinus Torvalds ift->valid_lft = tmp_valid_lft; 8641da177e4SLinus Torvalds ift->prefered_lft = tmp_prefered_lft; 8651da177e4SLinus Torvalds ift->cstamp = tmp_cstamp; 8661da177e4SLinus Torvalds ift->tstamp = tmp_tstamp; 8671da177e4SLinus Torvalds spin_unlock_bh(&ift->lock); 8681da177e4SLinus Torvalds 8691da177e4SLinus Torvalds addrconf_dad_start(ift, 0); 8701da177e4SLinus Torvalds in6_ifa_put(ift); 8711da177e4SLinus Torvalds in6_dev_put(idev); 8721da177e4SLinus Torvalds out: 8731da177e4SLinus Torvalds return ret; 8741da177e4SLinus Torvalds } 8751da177e4SLinus Torvalds #endif 8761da177e4SLinus Torvalds 8771da177e4SLinus Torvalds /* 878072047e4SYOSHIFUJI Hideaki * Choose an appropriate source address (RFC3484) 8791da177e4SLinus Torvalds */ 880072047e4SYOSHIFUJI Hideaki struct ipv6_saddr_score { 881072047e4SYOSHIFUJI Hideaki int addr_type; 882072047e4SYOSHIFUJI Hideaki unsigned int attrs; 883072047e4SYOSHIFUJI Hideaki int matchlen; 8840d27b427SBrian Haley int scope; 885072047e4SYOSHIFUJI Hideaki unsigned int rule; 886072047e4SYOSHIFUJI Hideaki }; 887072047e4SYOSHIFUJI Hideaki 888072047e4SYOSHIFUJI Hideaki #define IPV6_SADDR_SCORE_LOCAL 0x0001 889072047e4SYOSHIFUJI Hideaki #define IPV6_SADDR_SCORE_PREFERRED 0x0004 890072047e4SYOSHIFUJI Hideaki #define IPV6_SADDR_SCORE_HOA 0x0008 891072047e4SYOSHIFUJI Hideaki #define IPV6_SADDR_SCORE_OIF 0x0010 892072047e4SYOSHIFUJI Hideaki #define IPV6_SADDR_SCORE_LABEL 0x0020 893072047e4SYOSHIFUJI Hideaki #define IPV6_SADDR_SCORE_PRIVACY 0x0040 894072047e4SYOSHIFUJI Hideaki 895b6f99a21SDave Jones static inline int ipv6_saddr_preferred(int type) 8961da177e4SLinus Torvalds { 897072047e4SYOSHIFUJI Hideaki if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4| 898072047e4SYOSHIFUJI Hideaki IPV6_ADDR_LOOPBACK|IPV6_ADDR_RESERVED)) 899072047e4SYOSHIFUJI Hideaki return 1; 900072047e4SYOSHIFUJI Hideaki return 0; 9011da177e4SLinus Torvalds } 9021da177e4SLinus Torvalds 903072047e4SYOSHIFUJI Hideaki int ipv6_dev_get_saddr(struct net_device *daddr_dev, 9041da177e4SLinus Torvalds struct in6_addr *daddr, struct in6_addr *saddr) 9051da177e4SLinus Torvalds { 906072047e4SYOSHIFUJI Hideaki struct ipv6_saddr_score hiscore; 907072047e4SYOSHIFUJI Hideaki struct inet6_ifaddr *ifa_result = NULL; 908072047e4SYOSHIFUJI Hideaki int daddr_type = __ipv6_addr_type(daddr); 909072047e4SYOSHIFUJI Hideaki int daddr_scope = __ipv6_addr_src_scope(daddr_type); 910303065a8SYOSHIFUJI Hideaki int daddr_ifindex = daddr_dev ? daddr_dev->ifindex : 0; 911303065a8SYOSHIFUJI Hideaki u32 daddr_label = ipv6_addr_label(daddr, daddr_type, daddr_ifindex); 912072047e4SYOSHIFUJI Hideaki struct net_device *dev; 9131da177e4SLinus Torvalds 914072047e4SYOSHIFUJI Hideaki memset(&hiscore, 0, sizeof(hiscore)); 9151da177e4SLinus Torvalds 9161da177e4SLinus Torvalds read_lock(&dev_base_lock); 9178814c4b5SYOSHIFUJI Hideaki rcu_read_lock(); 918072047e4SYOSHIFUJI Hideaki 919881d966bSEric W. Biederman for_each_netdev(&init_net, dev) { 920072047e4SYOSHIFUJI Hideaki struct inet6_dev *idev; 921072047e4SYOSHIFUJI Hideaki struct inet6_ifaddr *ifa; 922072047e4SYOSHIFUJI Hideaki 923072047e4SYOSHIFUJI Hideaki /* Rule 0: Candidate Source Address (section 4) 924072047e4SYOSHIFUJI Hideaki * - multicast and link-local destination address, 925072047e4SYOSHIFUJI Hideaki * the set of candidate source address MUST only 926072047e4SYOSHIFUJI Hideaki * include addresses assigned to interfaces 927072047e4SYOSHIFUJI Hideaki * belonging to the same link as the outgoing 928072047e4SYOSHIFUJI Hideaki * interface. 929072047e4SYOSHIFUJI Hideaki * (- For site-local destination addresses, the 930072047e4SYOSHIFUJI Hideaki * set of candidate source addresses MUST only 931072047e4SYOSHIFUJI Hideaki * include addresses assigned to interfaces 932072047e4SYOSHIFUJI Hideaki * belonging to the same site as the outgoing 933072047e4SYOSHIFUJI Hideaki * interface.) 934072047e4SYOSHIFUJI Hideaki */ 935072047e4SYOSHIFUJI Hideaki if ((daddr_type & IPV6_ADDR_MULTICAST || 936072047e4SYOSHIFUJI Hideaki daddr_scope <= IPV6_ADDR_SCOPE_LINKLOCAL) && 937072047e4SYOSHIFUJI Hideaki daddr_dev && dev != daddr_dev) 938072047e4SYOSHIFUJI Hideaki continue; 939072047e4SYOSHIFUJI Hideaki 9401da177e4SLinus Torvalds idev = __in6_dev_get(dev); 941072047e4SYOSHIFUJI Hideaki if (!idev) 942072047e4SYOSHIFUJI Hideaki continue; 943072047e4SYOSHIFUJI Hideaki 9441da177e4SLinus Torvalds read_lock_bh(&idev->lock); 945072047e4SYOSHIFUJI Hideaki for (ifa = idev->addr_list; ifa; ifa = ifa->if_next) { 946072047e4SYOSHIFUJI Hideaki struct ipv6_saddr_score score; 947072047e4SYOSHIFUJI Hideaki 948072047e4SYOSHIFUJI Hideaki score.addr_type = __ipv6_addr_type(&ifa->addr); 949072047e4SYOSHIFUJI Hideaki 9506b3ae80aSYOSHIFUJI Hideaki /* Rule 0: 9516b3ae80aSYOSHIFUJI Hideaki * - Tentative Address (RFC2462 section 5.4) 9526b3ae80aSYOSHIFUJI Hideaki * - A tentative address is not considered 9536b3ae80aSYOSHIFUJI Hideaki * "assigned to an interface" in the traditional 95495c385b4SNeil Horman * sense, unless it is also flagged as optimistic. 9556b3ae80aSYOSHIFUJI Hideaki * - Candidate Source Address (section 4) 956072047e4SYOSHIFUJI Hideaki * - In any case, anycast addresses, multicast 957072047e4SYOSHIFUJI Hideaki * addresses, and the unspecified address MUST 958072047e4SYOSHIFUJI Hideaki * NOT be included in a candidate set. 959072047e4SYOSHIFUJI Hideaki */ 96095c385b4SNeil Horman if ((ifa->flags & IFA_F_TENTATIVE) && 96195c385b4SNeil Horman (!(ifa->flags & IFA_F_OPTIMISTIC))) 9626b3ae80aSYOSHIFUJI Hideaki continue; 963072047e4SYOSHIFUJI Hideaki if (unlikely(score.addr_type == IPV6_ADDR_ANY || 964072047e4SYOSHIFUJI Hideaki score.addr_type & IPV6_ADDR_MULTICAST)) { 965072047e4SYOSHIFUJI Hideaki LIMIT_NETDEBUG(KERN_DEBUG 966072047e4SYOSHIFUJI Hideaki "ADDRCONF: unspecified / multicast address " 967072047e4SYOSHIFUJI Hideaki "assigned as unicast address on %s", 968072047e4SYOSHIFUJI Hideaki dev->name); 9691da177e4SLinus Torvalds continue; 970072047e4SYOSHIFUJI Hideaki } 971072047e4SYOSHIFUJI Hideaki 972072047e4SYOSHIFUJI Hideaki score.attrs = 0; 973072047e4SYOSHIFUJI Hideaki score.matchlen = 0; 974072047e4SYOSHIFUJI Hideaki score.scope = 0; 975072047e4SYOSHIFUJI Hideaki score.rule = 0; 976072047e4SYOSHIFUJI Hideaki 977072047e4SYOSHIFUJI Hideaki if (ifa_result == NULL) { 978072047e4SYOSHIFUJI Hideaki /* record it if the first available entry */ 979072047e4SYOSHIFUJI Hideaki goto record_it; 980072047e4SYOSHIFUJI Hideaki } 981072047e4SYOSHIFUJI Hideaki 982072047e4SYOSHIFUJI Hideaki /* Rule 1: Prefer same address */ 983072047e4SYOSHIFUJI Hideaki if (hiscore.rule < 1) { 984072047e4SYOSHIFUJI Hideaki if (ipv6_addr_equal(&ifa_result->addr, daddr)) 985072047e4SYOSHIFUJI Hideaki hiscore.attrs |= IPV6_SADDR_SCORE_LOCAL; 986072047e4SYOSHIFUJI Hideaki hiscore.rule++; 987072047e4SYOSHIFUJI Hideaki } 988072047e4SYOSHIFUJI Hideaki if (ipv6_addr_equal(&ifa->addr, daddr)) { 989072047e4SYOSHIFUJI Hideaki score.attrs |= IPV6_SADDR_SCORE_LOCAL; 990072047e4SYOSHIFUJI Hideaki if (!(hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)) { 991072047e4SYOSHIFUJI Hideaki score.rule = 1; 992072047e4SYOSHIFUJI Hideaki goto record_it; 993072047e4SYOSHIFUJI Hideaki } 994072047e4SYOSHIFUJI Hideaki } else { 995072047e4SYOSHIFUJI Hideaki if (hiscore.attrs & IPV6_SADDR_SCORE_LOCAL) 996072047e4SYOSHIFUJI Hideaki continue; 997072047e4SYOSHIFUJI Hideaki } 998072047e4SYOSHIFUJI Hideaki 999072047e4SYOSHIFUJI Hideaki /* Rule 2: Prefer appropriate scope */ 1000072047e4SYOSHIFUJI Hideaki if (hiscore.rule < 2) { 1001072047e4SYOSHIFUJI Hideaki hiscore.scope = __ipv6_addr_src_scope(hiscore.addr_type); 1002072047e4SYOSHIFUJI Hideaki hiscore.rule++; 1003072047e4SYOSHIFUJI Hideaki } 1004072047e4SYOSHIFUJI Hideaki score.scope = __ipv6_addr_src_scope(score.addr_type); 1005072047e4SYOSHIFUJI Hideaki if (hiscore.scope < score.scope) { 1006072047e4SYOSHIFUJI Hideaki if (hiscore.scope < daddr_scope) { 1007072047e4SYOSHIFUJI Hideaki score.rule = 2; 1008072047e4SYOSHIFUJI Hideaki goto record_it; 1009072047e4SYOSHIFUJI Hideaki } else 1010072047e4SYOSHIFUJI Hideaki continue; 1011072047e4SYOSHIFUJI Hideaki } else if (score.scope < hiscore.scope) { 1012072047e4SYOSHIFUJI Hideaki if (score.scope < daddr_scope) 1013e55ffac6SBrian Haley break; /* addresses sorted by scope */ 1014072047e4SYOSHIFUJI Hideaki else { 1015072047e4SYOSHIFUJI Hideaki score.rule = 2; 1016072047e4SYOSHIFUJI Hideaki goto record_it; 1017072047e4SYOSHIFUJI Hideaki } 1018072047e4SYOSHIFUJI Hideaki } 1019072047e4SYOSHIFUJI Hideaki 102095c385b4SNeil Horman /* Rule 3: Avoid deprecated and optimistic addresses */ 1021072047e4SYOSHIFUJI Hideaki if (hiscore.rule < 3) { 1022072047e4SYOSHIFUJI Hideaki if (ipv6_saddr_preferred(hiscore.addr_type) || 102395c385b4SNeil Horman (((ifa_result->flags & 102495c385b4SNeil Horman (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC)) == 0))) 1025072047e4SYOSHIFUJI Hideaki hiscore.attrs |= IPV6_SADDR_SCORE_PREFERRED; 1026072047e4SYOSHIFUJI Hideaki hiscore.rule++; 1027072047e4SYOSHIFUJI Hideaki } 1028072047e4SYOSHIFUJI Hideaki if (ipv6_saddr_preferred(score.addr_type) || 10296ae5f983SJiri Kosina (((ifa->flags & 103095c385b4SNeil Horman (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC)) == 0))) { 1031072047e4SYOSHIFUJI Hideaki score.attrs |= IPV6_SADDR_SCORE_PREFERRED; 1032072047e4SYOSHIFUJI Hideaki if (!(hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)) { 1033072047e4SYOSHIFUJI Hideaki score.rule = 3; 1034072047e4SYOSHIFUJI Hideaki goto record_it; 1035072047e4SYOSHIFUJI Hideaki } 1036072047e4SYOSHIFUJI Hideaki } else { 1037072047e4SYOSHIFUJI Hideaki if (hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED) 1038072047e4SYOSHIFUJI Hideaki continue; 1039072047e4SYOSHIFUJI Hideaki } 1040072047e4SYOSHIFUJI Hideaki 10413b9f9a1cSNoriaki TAKAMIYA /* Rule 4: Prefer home address */ 104259fbb3a6SMasahide NAKAMURA #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) 10433b9f9a1cSNoriaki TAKAMIYA if (hiscore.rule < 4) { 10443b9f9a1cSNoriaki TAKAMIYA if (ifa_result->flags & IFA_F_HOMEADDRESS) 10453b9f9a1cSNoriaki TAKAMIYA hiscore.attrs |= IPV6_SADDR_SCORE_HOA; 10463b9f9a1cSNoriaki TAKAMIYA hiscore.rule++; 10473b9f9a1cSNoriaki TAKAMIYA } 10483b9f9a1cSNoriaki TAKAMIYA if (ifa->flags & IFA_F_HOMEADDRESS) { 10493b9f9a1cSNoriaki TAKAMIYA score.attrs |= IPV6_SADDR_SCORE_HOA; 10503b9f9a1cSNoriaki TAKAMIYA if (!(ifa_result->flags & IFA_F_HOMEADDRESS)) { 10513b9f9a1cSNoriaki TAKAMIYA score.rule = 4; 10523b9f9a1cSNoriaki TAKAMIYA goto record_it; 10533b9f9a1cSNoriaki TAKAMIYA } 10543b9f9a1cSNoriaki TAKAMIYA } else { 10553b9f9a1cSNoriaki TAKAMIYA if (hiscore.attrs & IPV6_SADDR_SCORE_HOA) 10563b9f9a1cSNoriaki TAKAMIYA continue; 10573b9f9a1cSNoriaki TAKAMIYA } 10583b9f9a1cSNoriaki TAKAMIYA #else 1059220bbd74SYOSHIFUJI Hideaki if (hiscore.rule < 4) 1060220bbd74SYOSHIFUJI Hideaki hiscore.rule++; 10613b9f9a1cSNoriaki TAKAMIYA #endif 1062072047e4SYOSHIFUJI Hideaki 1063072047e4SYOSHIFUJI Hideaki /* Rule 5: Prefer outgoing interface */ 1064072047e4SYOSHIFUJI Hideaki if (hiscore.rule < 5) { 1065072047e4SYOSHIFUJI Hideaki if (daddr_dev == NULL || 1066072047e4SYOSHIFUJI Hideaki daddr_dev == ifa_result->idev->dev) 1067072047e4SYOSHIFUJI Hideaki hiscore.attrs |= IPV6_SADDR_SCORE_OIF; 1068072047e4SYOSHIFUJI Hideaki hiscore.rule++; 1069072047e4SYOSHIFUJI Hideaki } 1070072047e4SYOSHIFUJI Hideaki if (daddr_dev == NULL || 1071072047e4SYOSHIFUJI Hideaki daddr_dev == ifa->idev->dev) { 1072072047e4SYOSHIFUJI Hideaki score.attrs |= IPV6_SADDR_SCORE_OIF; 1073072047e4SYOSHIFUJI Hideaki if (!(hiscore.attrs & IPV6_SADDR_SCORE_OIF)) { 1074072047e4SYOSHIFUJI Hideaki score.rule = 5; 1075072047e4SYOSHIFUJI Hideaki goto record_it; 1076072047e4SYOSHIFUJI Hideaki } 1077072047e4SYOSHIFUJI Hideaki } else { 1078072047e4SYOSHIFUJI Hideaki if (hiscore.attrs & IPV6_SADDR_SCORE_OIF) 1079072047e4SYOSHIFUJI Hideaki continue; 1080072047e4SYOSHIFUJI Hideaki } 1081072047e4SYOSHIFUJI Hideaki 1082072047e4SYOSHIFUJI Hideaki /* Rule 6: Prefer matching label */ 1083072047e4SYOSHIFUJI Hideaki if (hiscore.rule < 6) { 1084c1ee656cSYOSHIFUJI Hideaki if (ipv6_addr_label(&ifa_result->addr, 1085303065a8SYOSHIFUJI Hideaki hiscore.addr_type, 1086303065a8SYOSHIFUJI Hideaki ifa_result->idev->dev->ifindex) == daddr_label) 1087072047e4SYOSHIFUJI Hideaki hiscore.attrs |= IPV6_SADDR_SCORE_LABEL; 1088072047e4SYOSHIFUJI Hideaki hiscore.rule++; 1089072047e4SYOSHIFUJI Hideaki } 1090c1ee656cSYOSHIFUJI Hideaki if (ipv6_addr_label(&ifa->addr, 1091303065a8SYOSHIFUJI Hideaki score.addr_type, 1092303065a8SYOSHIFUJI Hideaki ifa->idev->dev->ifindex) == daddr_label) { 1093072047e4SYOSHIFUJI Hideaki score.attrs |= IPV6_SADDR_SCORE_LABEL; 1094072047e4SYOSHIFUJI Hideaki if (!(hiscore.attrs & IPV6_SADDR_SCORE_LABEL)) { 1095072047e4SYOSHIFUJI Hideaki score.rule = 6; 1096072047e4SYOSHIFUJI Hideaki goto record_it; 1097072047e4SYOSHIFUJI Hideaki } 1098072047e4SYOSHIFUJI Hideaki } else { 1099072047e4SYOSHIFUJI Hideaki if (hiscore.attrs & IPV6_SADDR_SCORE_LABEL) 1100072047e4SYOSHIFUJI Hideaki continue; 1101072047e4SYOSHIFUJI Hideaki } 1102072047e4SYOSHIFUJI Hideaki 110344fd0261SPeter Chubb #ifdef CONFIG_IPV6_PRIVACY 1104072047e4SYOSHIFUJI Hideaki /* Rule 7: Prefer public address 1105072047e4SYOSHIFUJI Hideaki * Note: prefer temprary address if use_tempaddr >= 2 1106072047e4SYOSHIFUJI Hideaki */ 1107072047e4SYOSHIFUJI Hideaki if (hiscore.rule < 7) { 1108072047e4SYOSHIFUJI Hideaki if ((!(ifa_result->flags & IFA_F_TEMPORARY)) ^ 1109072047e4SYOSHIFUJI Hideaki (ifa_result->idev->cnf.use_tempaddr >= 2)) 1110072047e4SYOSHIFUJI Hideaki hiscore.attrs |= IPV6_SADDR_SCORE_PRIVACY; 1111072047e4SYOSHIFUJI Hideaki hiscore.rule++; 1112072047e4SYOSHIFUJI Hideaki } 1113072047e4SYOSHIFUJI Hideaki if ((!(ifa->flags & IFA_F_TEMPORARY)) ^ 1114072047e4SYOSHIFUJI Hideaki (ifa->idev->cnf.use_tempaddr >= 2)) { 1115072047e4SYOSHIFUJI Hideaki score.attrs |= IPV6_SADDR_SCORE_PRIVACY; 1116072047e4SYOSHIFUJI Hideaki if (!(hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)) { 1117072047e4SYOSHIFUJI Hideaki score.rule = 7; 1118072047e4SYOSHIFUJI Hideaki goto record_it; 1119072047e4SYOSHIFUJI Hideaki } 1120072047e4SYOSHIFUJI Hideaki } else { 1121072047e4SYOSHIFUJI Hideaki if (hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY) 1122072047e4SYOSHIFUJI Hideaki continue; 1123072047e4SYOSHIFUJI Hideaki } 11245e2707faSYOSHIFUJI Hideaki #else 11255e2707faSYOSHIFUJI Hideaki if (hiscore.rule < 7) 11265e2707faSYOSHIFUJI Hideaki hiscore.rule++; 112744fd0261SPeter Chubb #endif 1128072047e4SYOSHIFUJI Hideaki /* Rule 8: Use longest matching prefix */ 112912da2a43SYan Zheng if (hiscore.rule < 8) { 1130072047e4SYOSHIFUJI Hideaki hiscore.matchlen = ipv6_addr_diff(&ifa_result->addr, daddr); 113112da2a43SYan Zheng hiscore.rule++; 113212da2a43SYan Zheng } 1133072047e4SYOSHIFUJI Hideaki score.matchlen = ipv6_addr_diff(&ifa->addr, daddr); 1134072047e4SYOSHIFUJI Hideaki if (score.matchlen > hiscore.matchlen) { 1135072047e4SYOSHIFUJI Hideaki score.rule = 8; 1136072047e4SYOSHIFUJI Hideaki goto record_it; 1137072047e4SYOSHIFUJI Hideaki } 1138072047e4SYOSHIFUJI Hideaki #if 0 1139072047e4SYOSHIFUJI Hideaki else if (score.matchlen < hiscore.matchlen) 1140072047e4SYOSHIFUJI Hideaki continue; 11411da177e4SLinus Torvalds #endif 1142072047e4SYOSHIFUJI Hideaki 1143072047e4SYOSHIFUJI Hideaki /* Final Rule: choose first available one */ 11441da177e4SLinus Torvalds continue; 1145072047e4SYOSHIFUJI Hideaki record_it: 1146072047e4SYOSHIFUJI Hideaki if (ifa_result) 1147072047e4SYOSHIFUJI Hideaki in6_ifa_put(ifa_result); 1148072047e4SYOSHIFUJI Hideaki in6_ifa_hold(ifa); 1149072047e4SYOSHIFUJI Hideaki ifa_result = ifa; 11501da177e4SLinus Torvalds hiscore = score; 11511da177e4SLinus Torvalds } 11521da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 11531da177e4SLinus Torvalds } 11548814c4b5SYOSHIFUJI Hideaki rcu_read_unlock(); 11551da177e4SLinus Torvalds read_unlock(&dev_base_lock); 11561da177e4SLinus Torvalds 1157072047e4SYOSHIFUJI Hideaki if (!ifa_result) 1158072047e4SYOSHIFUJI Hideaki return -EADDRNOTAVAIL; 11591da177e4SLinus Torvalds 1160072047e4SYOSHIFUJI Hideaki ipv6_addr_copy(saddr, &ifa_result->addr); 1161072047e4SYOSHIFUJI Hideaki in6_ifa_put(ifa_result); 1162072047e4SYOSHIFUJI Hideaki return 0; 11631da177e4SLinus Torvalds } 11641da177e4SLinus Torvalds 11651da177e4SLinus Torvalds 11661da177e4SLinus Torvalds int ipv6_get_saddr(struct dst_entry *dst, 11671da177e4SLinus Torvalds struct in6_addr *daddr, struct in6_addr *saddr) 11681da177e4SLinus Torvalds { 11697a3025b1SYOSHIFUJI Hideaki return ipv6_dev_get_saddr(dst ? ip6_dst_idev(dst)->dev : NULL, daddr, saddr); 11701da177e4SLinus Torvalds } 11711da177e4SLinus Torvalds 11727159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_get_saddr); 11731da177e4SLinus Torvalds 117495c385b4SNeil Horman int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr, 117595c385b4SNeil Horman unsigned char banned_flags) 11761da177e4SLinus Torvalds { 11771da177e4SLinus Torvalds struct inet6_dev *idev; 11781da177e4SLinus Torvalds int err = -EADDRNOTAVAIL; 11791da177e4SLinus Torvalds 11808814c4b5SYOSHIFUJI Hideaki rcu_read_lock(); 11811da177e4SLinus Torvalds if ((idev = __in6_dev_get(dev)) != NULL) { 11821da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 11831da177e4SLinus Torvalds 11841da177e4SLinus Torvalds read_lock_bh(&idev->lock); 11851da177e4SLinus Torvalds for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) { 118695c385b4SNeil Horman if (ifp->scope == IFA_LINK && !(ifp->flags & banned_flags)) { 11871da177e4SLinus Torvalds ipv6_addr_copy(addr, &ifp->addr); 11881da177e4SLinus Torvalds err = 0; 11891da177e4SLinus Torvalds break; 11901da177e4SLinus Torvalds } 11911da177e4SLinus Torvalds } 11921da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 11931da177e4SLinus Torvalds } 11948814c4b5SYOSHIFUJI Hideaki rcu_read_unlock(); 11951da177e4SLinus Torvalds return err; 11961da177e4SLinus Torvalds } 11971da177e4SLinus Torvalds 11981da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev) 11991da177e4SLinus Torvalds { 12001da177e4SLinus Torvalds int cnt = 0; 12011da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 12021da177e4SLinus Torvalds 12031da177e4SLinus Torvalds read_lock_bh(&idev->lock); 12041da177e4SLinus Torvalds for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) 12051da177e4SLinus Torvalds cnt++; 12061da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 12071da177e4SLinus Torvalds return cnt; 12081da177e4SLinus Torvalds } 12091da177e4SLinus Torvalds 1210bfeade08SDaniel Lezcano int ipv6_chk_addr(struct net *net, struct in6_addr *addr, 1211bfeade08SDaniel Lezcano struct net_device *dev, int strict) 12121da177e4SLinus Torvalds { 12131da177e4SLinus Torvalds struct inet6_ifaddr * ifp; 12141da177e4SLinus Torvalds u8 hash = ipv6_addr_hash(addr); 12151da177e4SLinus Torvalds 12161da177e4SLinus Torvalds read_lock_bh(&addrconf_hash_lock); 12171da177e4SLinus Torvalds for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) { 1218bfeade08SDaniel Lezcano if (ifp->idev->dev->nd_net != net) 1219bfeade08SDaniel Lezcano continue; 12201da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr) && 12211da177e4SLinus Torvalds !(ifp->flags&IFA_F_TENTATIVE)) { 12221da177e4SLinus Torvalds if (dev == NULL || ifp->idev->dev == dev || 12231da177e4SLinus Torvalds !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) 12241da177e4SLinus Torvalds break; 12251da177e4SLinus Torvalds } 12261da177e4SLinus Torvalds } 12271da177e4SLinus Torvalds read_unlock_bh(&addrconf_hash_lock); 12281da177e4SLinus Torvalds return ifp != NULL; 12291da177e4SLinus Torvalds } 12307159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_addr); 12317159039aSYOSHIFUJI Hideaki 12321da177e4SLinus Torvalds static 123306bfe655SDaniel Lezcano int ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr, 123406bfe655SDaniel Lezcano struct net_device *dev) 12351da177e4SLinus Torvalds { 12361da177e4SLinus Torvalds struct inet6_ifaddr * ifp; 12371da177e4SLinus Torvalds u8 hash = ipv6_addr_hash(addr); 12381da177e4SLinus Torvalds 12391da177e4SLinus Torvalds for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) { 124006bfe655SDaniel Lezcano if (ifp->idev->dev->nd_net != net) 124106bfe655SDaniel Lezcano continue; 12421da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr)) { 12431da177e4SLinus Torvalds if (dev == NULL || ifp->idev->dev == dev) 12441da177e4SLinus Torvalds break; 12451da177e4SLinus Torvalds } 12461da177e4SLinus Torvalds } 12471da177e4SLinus Torvalds return ifp != NULL; 12481da177e4SLinus Torvalds } 12491da177e4SLinus Torvalds 12501cab3da6SDaniel Lezcano struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, struct in6_addr *addr, 12511cab3da6SDaniel Lezcano struct net_device *dev, int strict) 12521da177e4SLinus Torvalds { 12531da177e4SLinus Torvalds struct inet6_ifaddr * ifp; 12541da177e4SLinus Torvalds u8 hash = ipv6_addr_hash(addr); 12551da177e4SLinus Torvalds 12561da177e4SLinus Torvalds read_lock_bh(&addrconf_hash_lock); 12571da177e4SLinus Torvalds for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) { 12581cab3da6SDaniel Lezcano if (ifp->idev->dev->nd_net != net) 12591cab3da6SDaniel Lezcano continue; 12601da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr)) { 12611da177e4SLinus Torvalds if (dev == NULL || ifp->idev->dev == dev || 12621da177e4SLinus Torvalds !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) { 12631da177e4SLinus Torvalds in6_ifa_hold(ifp); 12641da177e4SLinus Torvalds break; 12651da177e4SLinus Torvalds } 12661da177e4SLinus Torvalds } 12671da177e4SLinus Torvalds } 12681da177e4SLinus Torvalds read_unlock_bh(&addrconf_hash_lock); 12691da177e4SLinus Torvalds 12701da177e4SLinus Torvalds return ifp; 12711da177e4SLinus Torvalds } 12721da177e4SLinus Torvalds 12731da177e4SLinus Torvalds int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2) 12741da177e4SLinus Torvalds { 12751da177e4SLinus Torvalds const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr; 12760fa1a53eSArnaldo Carvalho de Melo const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2); 127782103232SAl Viro __be32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr; 127882103232SAl Viro __be32 sk2_rcv_saddr = inet_rcv_saddr(sk2); 12791da177e4SLinus Torvalds int sk_ipv6only = ipv6_only_sock(sk); 1280463c84b9SArnaldo Carvalho de Melo int sk2_ipv6only = inet_v6_ipv6only(sk2); 12811da177e4SLinus Torvalds int addr_type = ipv6_addr_type(sk_rcv_saddr6); 12821da177e4SLinus Torvalds int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED; 12831da177e4SLinus Torvalds 12841da177e4SLinus Torvalds if (!sk2_rcv_saddr && !sk_ipv6only) 12851da177e4SLinus Torvalds return 1; 12861da177e4SLinus Torvalds 12871da177e4SLinus Torvalds if (addr_type2 == IPV6_ADDR_ANY && 12881da177e4SLinus Torvalds !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED)) 12891da177e4SLinus Torvalds return 1; 12901da177e4SLinus Torvalds 12911da177e4SLinus Torvalds if (addr_type == IPV6_ADDR_ANY && 12921da177e4SLinus Torvalds !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED)) 12931da177e4SLinus Torvalds return 1; 12941da177e4SLinus Torvalds 12951da177e4SLinus Torvalds if (sk2_rcv_saddr6 && 12961da177e4SLinus Torvalds ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6)) 12971da177e4SLinus Torvalds return 1; 12981da177e4SLinus Torvalds 12991da177e4SLinus Torvalds if (addr_type == IPV6_ADDR_MAPPED && 13001da177e4SLinus Torvalds !sk2_ipv6only && 13011da177e4SLinus Torvalds (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr)) 13021da177e4SLinus Torvalds return 1; 13031da177e4SLinus Torvalds 13041da177e4SLinus Torvalds return 0; 13051da177e4SLinus Torvalds } 13061da177e4SLinus Torvalds 13071da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */ 13081da177e4SLinus Torvalds 13099f5336e2SAdrian Bunk static void addrconf_dad_stop(struct inet6_ifaddr *ifp) 13101da177e4SLinus Torvalds { 13111da177e4SLinus Torvalds if (ifp->flags&IFA_F_PERMANENT) { 13121da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 13131da177e4SLinus Torvalds addrconf_del_timer(ifp); 13141da177e4SLinus Torvalds ifp->flags |= IFA_F_TENTATIVE; 13151da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 13161da177e4SLinus Torvalds in6_ifa_put(ifp); 13171da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 13181da177e4SLinus Torvalds } else if (ifp->flags&IFA_F_TEMPORARY) { 13191da177e4SLinus Torvalds struct inet6_ifaddr *ifpub; 13201da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 13211da177e4SLinus Torvalds ifpub = ifp->ifpub; 13221da177e4SLinus Torvalds if (ifpub) { 13231da177e4SLinus Torvalds in6_ifa_hold(ifpub); 13241da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 13251da177e4SLinus Torvalds ipv6_create_tempaddr(ifpub, ifp); 13261da177e4SLinus Torvalds in6_ifa_put(ifpub); 13271da177e4SLinus Torvalds } else { 13281da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 13291da177e4SLinus Torvalds } 13301da177e4SLinus Torvalds ipv6_del_addr(ifp); 13311da177e4SLinus Torvalds #endif 13321da177e4SLinus Torvalds } else 13331da177e4SLinus Torvalds ipv6_del_addr(ifp); 13341da177e4SLinus Torvalds } 13351da177e4SLinus Torvalds 13363c21edbdSYOSHIFUJI Hideaki void addrconf_dad_failure(struct inet6_ifaddr *ifp) 13373c21edbdSYOSHIFUJI Hideaki { 13383c21edbdSYOSHIFUJI Hideaki if (net_ratelimit()) 13393c21edbdSYOSHIFUJI Hideaki printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name); 13403c21edbdSYOSHIFUJI Hideaki addrconf_dad_stop(ifp); 13413c21edbdSYOSHIFUJI Hideaki } 13421da177e4SLinus Torvalds 13431da177e4SLinus Torvalds /* Join to solicited addr multicast group. */ 13441da177e4SLinus Torvalds 13451da177e4SLinus Torvalds void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr) 13461da177e4SLinus Torvalds { 13471da177e4SLinus Torvalds struct in6_addr maddr; 13481da177e4SLinus Torvalds 13491da177e4SLinus Torvalds if (dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 13501da177e4SLinus Torvalds return; 13511da177e4SLinus Torvalds 13521da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr); 13531da177e4SLinus Torvalds ipv6_dev_mc_inc(dev, &maddr); 13541da177e4SLinus Torvalds } 13551da177e4SLinus Torvalds 13561da177e4SLinus Torvalds void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr) 13571da177e4SLinus Torvalds { 13581da177e4SLinus Torvalds struct in6_addr maddr; 13591da177e4SLinus Torvalds 13601da177e4SLinus Torvalds if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 13611da177e4SLinus Torvalds return; 13621da177e4SLinus Torvalds 13631da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr); 13641da177e4SLinus Torvalds __ipv6_dev_mc_dec(idev, &maddr); 13651da177e4SLinus Torvalds } 13661da177e4SLinus Torvalds 136720380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp) 13681da177e4SLinus Torvalds { 13691da177e4SLinus Torvalds struct in6_addr addr; 13701da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 13711da177e4SLinus Torvalds if (ipv6_addr_any(&addr)) 13721da177e4SLinus Torvalds return; 13731da177e4SLinus Torvalds ipv6_dev_ac_inc(ifp->idev->dev, &addr); 13741da177e4SLinus Torvalds } 13751da177e4SLinus Torvalds 137620380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp) 13771da177e4SLinus Torvalds { 13781da177e4SLinus Torvalds struct in6_addr addr; 13791da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 13801da177e4SLinus Torvalds if (ipv6_addr_any(&addr)) 13811da177e4SLinus Torvalds return; 13821da177e4SLinus Torvalds __ipv6_dev_ac_dec(ifp->idev, &addr); 13831da177e4SLinus Torvalds } 13841da177e4SLinus Torvalds 1385073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev) 13861da177e4SLinus Torvalds { 13871da177e4SLinus Torvalds if (dev->addr_len != ETH_ALEN) 13881da177e4SLinus Torvalds return -1; 13891da177e4SLinus Torvalds memcpy(eui, dev->dev_addr, 3); 13901da177e4SLinus Torvalds memcpy(eui + 5, dev->dev_addr + 3, 3); 13911da177e4SLinus Torvalds 13921da177e4SLinus Torvalds /* 13931da177e4SLinus Torvalds * The zSeries OSA network cards can be shared among various 13941da177e4SLinus Torvalds * OS instances, but the OSA cards have only one MAC address. 13951da177e4SLinus Torvalds * This leads to duplicate address conflicts in conjunction 13961da177e4SLinus Torvalds * with IPv6 if more than one instance uses the same card. 13971da177e4SLinus Torvalds * 13981da177e4SLinus Torvalds * The driver for these cards can deliver a unique 16-bit 13991da177e4SLinus Torvalds * identifier for each instance sharing the same card. It is 14001da177e4SLinus Torvalds * placed instead of 0xFFFE in the interface identifier. The 14011da177e4SLinus Torvalds * "u" bit of the interface identifier is not inverted in this 14021da177e4SLinus Torvalds * case. Hence the resulting interface identifier has local 14031da177e4SLinus Torvalds * scope according to RFC2373. 14041da177e4SLinus Torvalds */ 14051da177e4SLinus Torvalds if (dev->dev_id) { 14061da177e4SLinus Torvalds eui[3] = (dev->dev_id >> 8) & 0xFF; 14071da177e4SLinus Torvalds eui[4] = dev->dev_id & 0xFF; 14081da177e4SLinus Torvalds } else { 14091da177e4SLinus Torvalds eui[3] = 0xFF; 14101da177e4SLinus Torvalds eui[4] = 0xFE; 14111da177e4SLinus Torvalds eui[0] ^= 2; 14121da177e4SLinus Torvalds } 14131da177e4SLinus Torvalds return 0; 1414073a8e0eSYOSHIFUJI Hideaki } 1415073a8e0eSYOSHIFUJI Hideaki 1416073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev) 1417073a8e0eSYOSHIFUJI Hideaki { 14181da177e4SLinus Torvalds /* XXX: inherit EUI-64 from other interface -- yoshfuji */ 14191da177e4SLinus Torvalds if (dev->addr_len != ARCNET_ALEN) 14201da177e4SLinus Torvalds return -1; 14211da177e4SLinus Torvalds memset(eui, 0, 7); 14221da177e4SLinus Torvalds eui[7] = *(u8*)dev->dev_addr; 14231da177e4SLinus Torvalds return 0; 1424073a8e0eSYOSHIFUJI Hideaki } 1425073a8e0eSYOSHIFUJI Hideaki 1426073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev) 1427073a8e0eSYOSHIFUJI Hideaki { 14281da177e4SLinus Torvalds if (dev->addr_len != INFINIBAND_ALEN) 14291da177e4SLinus Torvalds return -1; 14301da177e4SLinus Torvalds memcpy(eui, dev->dev_addr + 12, 8); 14311da177e4SLinus Torvalds eui[0] |= 2; 14321da177e4SLinus Torvalds return 0; 14331da177e4SLinus Torvalds } 1434073a8e0eSYOSHIFUJI Hideaki 1435073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev) 1436073a8e0eSYOSHIFUJI Hideaki { 1437073a8e0eSYOSHIFUJI Hideaki switch (dev->type) { 1438073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ETHER: 1439073a8e0eSYOSHIFUJI Hideaki case ARPHRD_FDDI: 1440073a8e0eSYOSHIFUJI Hideaki case ARPHRD_IEEE802_TR: 1441073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_eui48(eui, dev); 1442073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ARCNET: 1443073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_arcnet(eui, dev); 1444073a8e0eSYOSHIFUJI Hideaki case ARPHRD_INFINIBAND: 1445073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_infiniband(eui, dev); 1446c7dc89c0SFred L. Templin case ARPHRD_SIT: 1447c7dc89c0SFred L. Templin if (dev->priv_flags & IFF_ISATAP) 1448c7dc89c0SFred L. Templin return ipv6_isatap_eui64(eui, *(__be32 *)dev->dev_addr); 1449073a8e0eSYOSHIFUJI Hideaki } 14501da177e4SLinus Torvalds return -1; 14511da177e4SLinus Torvalds } 14521da177e4SLinus Torvalds 14531da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev) 14541da177e4SLinus Torvalds { 14551da177e4SLinus Torvalds int err = -1; 14561da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 14571da177e4SLinus Torvalds 14581da177e4SLinus Torvalds read_lock_bh(&idev->lock); 14591da177e4SLinus Torvalds for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) { 14601da177e4SLinus Torvalds if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) { 14611da177e4SLinus Torvalds memcpy(eui, ifp->addr.s6_addr+8, 8); 14621da177e4SLinus Torvalds err = 0; 14631da177e4SLinus Torvalds break; 14641da177e4SLinus Torvalds } 14651da177e4SLinus Torvalds } 14661da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 14671da177e4SLinus Torvalds return err; 14681da177e4SLinus Torvalds } 14691da177e4SLinus Torvalds 14701da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 14711da177e4SLinus Torvalds /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */ 14721da177e4SLinus Torvalds static int __ipv6_regen_rndid(struct inet6_dev *idev) 14731da177e4SLinus Torvalds { 14741da177e4SLinus Torvalds regen: 1475955189efSYOSHIFUJI Hideaki get_random_bytes(idev->rndid, sizeof(idev->rndid)); 14761da177e4SLinus Torvalds idev->rndid[0] &= ~0x02; 14771da177e4SLinus Torvalds 14781da177e4SLinus Torvalds /* 14791da177e4SLinus Torvalds * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>: 14801da177e4SLinus Torvalds * check if generated address is not inappropriate 14811da177e4SLinus Torvalds * 14821da177e4SLinus Torvalds * - Reserved subnet anycast (RFC 2526) 14831da177e4SLinus Torvalds * 11111101 11....11 1xxxxxxx 1484c7dc89c0SFred L. Templin * - ISATAP (RFC4214) 6.1 14851da177e4SLinus Torvalds * 00-00-5E-FE-xx-xx-xx-xx 14861da177e4SLinus Torvalds * - value 0 14871da177e4SLinus Torvalds * - XXX: already assigned to an address on the device 14881da177e4SLinus Torvalds */ 14891da177e4SLinus Torvalds if (idev->rndid[0] == 0xfd && 14901da177e4SLinus Torvalds (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff && 14911da177e4SLinus Torvalds (idev->rndid[7]&0x80)) 14921da177e4SLinus Torvalds goto regen; 14931da177e4SLinus Torvalds if ((idev->rndid[0]|idev->rndid[1]) == 0) { 14941da177e4SLinus Torvalds if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe) 14951da177e4SLinus Torvalds goto regen; 14961da177e4SLinus Torvalds if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00) 14971da177e4SLinus Torvalds goto regen; 14981da177e4SLinus Torvalds } 14991da177e4SLinus Torvalds 15001da177e4SLinus Torvalds return 0; 15011da177e4SLinus Torvalds } 15021da177e4SLinus Torvalds 15031da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data) 15041da177e4SLinus Torvalds { 15051da177e4SLinus Torvalds struct inet6_dev *idev = (struct inet6_dev *) data; 15061da177e4SLinus Torvalds unsigned long expires; 15071da177e4SLinus Torvalds 15088814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 15091da177e4SLinus Torvalds write_lock_bh(&idev->lock); 15101da177e4SLinus Torvalds 15111da177e4SLinus Torvalds if (idev->dead) 15121da177e4SLinus Torvalds goto out; 15131da177e4SLinus Torvalds 15141da177e4SLinus Torvalds if (__ipv6_regen_rndid(idev) < 0) 15151da177e4SLinus Torvalds goto out; 15161da177e4SLinus Torvalds 15171da177e4SLinus Torvalds expires = jiffies + 15181da177e4SLinus Torvalds idev->cnf.temp_prefered_lft * HZ - 15191da177e4SLinus Torvalds idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor; 15201da177e4SLinus Torvalds if (time_before(expires, jiffies)) { 15211da177e4SLinus Torvalds printk(KERN_WARNING 15221da177e4SLinus Torvalds "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n", 15231da177e4SLinus Torvalds idev->dev->name); 15241da177e4SLinus Torvalds goto out; 15251da177e4SLinus Torvalds } 15261da177e4SLinus Torvalds 15271da177e4SLinus Torvalds if (!mod_timer(&idev->regen_timer, expires)) 15281da177e4SLinus Torvalds in6_dev_hold(idev); 15291da177e4SLinus Torvalds 15301da177e4SLinus Torvalds out: 15311da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 15328814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 15331da177e4SLinus Torvalds in6_dev_put(idev); 15341da177e4SLinus Torvalds } 15351da177e4SLinus Torvalds 15361da177e4SLinus Torvalds static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) { 15371da177e4SLinus Torvalds int ret = 0; 15381da177e4SLinus Torvalds 15391da177e4SLinus Torvalds if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0) 15401da177e4SLinus Torvalds ret = __ipv6_regen_rndid(idev); 15411da177e4SLinus Torvalds return ret; 15421da177e4SLinus Torvalds } 15431da177e4SLinus Torvalds #endif 15441da177e4SLinus Torvalds 15451da177e4SLinus Torvalds /* 15461da177e4SLinus Torvalds * Add prefix route. 15471da177e4SLinus Torvalds */ 15481da177e4SLinus Torvalds 15491da177e4SLinus Torvalds static void 15501da177e4SLinus Torvalds addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev, 1551e431b8c0SJamal Hadi Salim unsigned long expires, u32 flags) 15521da177e4SLinus Torvalds { 155386872cb5SThomas Graf struct fib6_config cfg = { 155486872cb5SThomas Graf .fc_table = RT6_TABLE_PREFIX, 155586872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 155686872cb5SThomas Graf .fc_ifindex = dev->ifindex, 155786872cb5SThomas Graf .fc_expires = expires, 155886872cb5SThomas Graf .fc_dst_len = plen, 155986872cb5SThomas Graf .fc_flags = RTF_UP | flags, 1560f1243c2dSBenjamin Thery .fc_nlinfo.nl_net = &init_net, 156186872cb5SThomas Graf }; 15621da177e4SLinus Torvalds 156386872cb5SThomas Graf ipv6_addr_copy(&cfg.fc_dst, pfx); 15641da177e4SLinus Torvalds 15651da177e4SLinus Torvalds /* Prevent useless cloning on PtP SIT. 15661da177e4SLinus Torvalds This thing is done here expecting that the whole 15671da177e4SLinus Torvalds class of non-broadcast devices need not cloning. 15681da177e4SLinus Torvalds */ 15690be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 15701da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT)) 157186872cb5SThomas Graf cfg.fc_flags |= RTF_NONEXTHOP; 15720be669bbSJoerg Roedel #endif 15731da177e4SLinus Torvalds 157486872cb5SThomas Graf ip6_route_add(&cfg); 15751da177e4SLinus Torvalds } 15761da177e4SLinus Torvalds 15771da177e4SLinus Torvalds /* Create "default" multicast route to the interface */ 15781da177e4SLinus Torvalds 15791da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev) 15801da177e4SLinus Torvalds { 158186872cb5SThomas Graf struct fib6_config cfg = { 158286872cb5SThomas Graf .fc_table = RT6_TABLE_LOCAL, 158386872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 158486872cb5SThomas Graf .fc_ifindex = dev->ifindex, 158586872cb5SThomas Graf .fc_dst_len = 8, 158686872cb5SThomas Graf .fc_flags = RTF_UP, 1587f1243c2dSBenjamin Thery .fc_nlinfo.nl_net = &init_net, 158886872cb5SThomas Graf }; 15891da177e4SLinus Torvalds 159086872cb5SThomas Graf ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0); 159186872cb5SThomas Graf 159286872cb5SThomas Graf ip6_route_add(&cfg); 15931da177e4SLinus Torvalds } 15941da177e4SLinus Torvalds 15950be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 15961da177e4SLinus Torvalds static void sit_route_add(struct net_device *dev) 15971da177e4SLinus Torvalds { 159886872cb5SThomas Graf struct fib6_config cfg = { 159986872cb5SThomas Graf .fc_table = RT6_TABLE_MAIN, 160086872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 160186872cb5SThomas Graf .fc_ifindex = dev->ifindex, 160286872cb5SThomas Graf .fc_dst_len = 96, 160386872cb5SThomas Graf .fc_flags = RTF_UP | RTF_NONEXTHOP, 1604f1243c2dSBenjamin Thery .fc_nlinfo.nl_net = &init_net, 160586872cb5SThomas Graf }; 16061da177e4SLinus Torvalds 16071da177e4SLinus Torvalds /* prefix length - 96 bits "::d.d.d.d" */ 160886872cb5SThomas Graf ip6_route_add(&cfg); 16091da177e4SLinus Torvalds } 16100be669bbSJoerg Roedel #endif 16111da177e4SLinus Torvalds 16121da177e4SLinus Torvalds static void addrconf_add_lroute(struct net_device *dev) 16131da177e4SLinus Torvalds { 16141da177e4SLinus Torvalds struct in6_addr addr; 16151da177e4SLinus Torvalds 16161da177e4SLinus Torvalds ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0); 16171da177e4SLinus Torvalds addrconf_prefix_route(&addr, 64, dev, 0, 0); 16181da177e4SLinus Torvalds } 16191da177e4SLinus Torvalds 16201da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev) 16211da177e4SLinus Torvalds { 16221da177e4SLinus Torvalds struct inet6_dev *idev; 16231da177e4SLinus Torvalds 16241da177e4SLinus Torvalds ASSERT_RTNL(); 16251da177e4SLinus Torvalds 16261da177e4SLinus Torvalds if ((idev = ipv6_find_idev(dev)) == NULL) 16271da177e4SLinus Torvalds return NULL; 16281da177e4SLinus Torvalds 16291da177e4SLinus Torvalds /* Add default multicast route */ 16301da177e4SLinus Torvalds addrconf_add_mroute(dev); 16311da177e4SLinus Torvalds 16321da177e4SLinus Torvalds /* Add link local route */ 16331da177e4SLinus Torvalds addrconf_add_lroute(dev); 16341da177e4SLinus Torvalds return idev; 16351da177e4SLinus Torvalds } 16361da177e4SLinus Torvalds 16371da177e4SLinus Torvalds void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len) 16381da177e4SLinus Torvalds { 16391da177e4SLinus Torvalds struct prefix_info *pinfo; 16401da177e4SLinus Torvalds __u32 valid_lft; 16411da177e4SLinus Torvalds __u32 prefered_lft; 16421da177e4SLinus Torvalds int addr_type; 16431da177e4SLinus Torvalds unsigned long rt_expires; 16441da177e4SLinus Torvalds struct inet6_dev *in6_dev; 16451da177e4SLinus Torvalds 16461da177e4SLinus Torvalds pinfo = (struct prefix_info *) opt; 16471da177e4SLinus Torvalds 16481da177e4SLinus Torvalds if (len < sizeof(struct prefix_info)) { 16491da177e4SLinus Torvalds ADBG(("addrconf: prefix option too short\n")); 16501da177e4SLinus Torvalds return; 16511da177e4SLinus Torvalds } 16521da177e4SLinus Torvalds 16531da177e4SLinus Torvalds /* 16541da177e4SLinus Torvalds * Validation checks ([ADDRCONF], page 19) 16551da177e4SLinus Torvalds */ 16561da177e4SLinus Torvalds 16571da177e4SLinus Torvalds addr_type = ipv6_addr_type(&pinfo->prefix); 16581da177e4SLinus Torvalds 16591da177e4SLinus Torvalds if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)) 16601da177e4SLinus Torvalds return; 16611da177e4SLinus Torvalds 16621da177e4SLinus Torvalds valid_lft = ntohl(pinfo->valid); 16631da177e4SLinus Torvalds prefered_lft = ntohl(pinfo->prefered); 16641da177e4SLinus Torvalds 16651da177e4SLinus Torvalds if (prefered_lft > valid_lft) { 16661da177e4SLinus Torvalds if (net_ratelimit()) 16671da177e4SLinus Torvalds printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n"); 16681da177e4SLinus Torvalds return; 16691da177e4SLinus Torvalds } 16701da177e4SLinus Torvalds 16711da177e4SLinus Torvalds in6_dev = in6_dev_get(dev); 16721da177e4SLinus Torvalds 16731da177e4SLinus Torvalds if (in6_dev == NULL) { 16741da177e4SLinus Torvalds if (net_ratelimit()) 16751da177e4SLinus Torvalds printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name); 16761da177e4SLinus Torvalds return; 16771da177e4SLinus Torvalds } 16781da177e4SLinus Torvalds 16791da177e4SLinus Torvalds /* 16801da177e4SLinus Torvalds * Two things going on here: 16811da177e4SLinus Torvalds * 1) Add routes for on-link prefixes 16821da177e4SLinus Torvalds * 2) Configure prefixes with the auto flag set 16831da177e4SLinus Torvalds */ 16841da177e4SLinus Torvalds 16851da177e4SLinus Torvalds /* Avoid arithmetic overflow. Really, we could 16861da177e4SLinus Torvalds save rt_expires in seconds, likely valid_lft, 16871da177e4SLinus Torvalds but it would require division in fib gc, that it 16881da177e4SLinus Torvalds not good. 16891da177e4SLinus Torvalds */ 16901da177e4SLinus Torvalds if (valid_lft >= 0x7FFFFFFF/HZ) 16913dd4bc68SYOSHIFUJI Hideaki rt_expires = 0x7FFFFFFF - (0x7FFFFFFF % HZ); 16921da177e4SLinus Torvalds else 16933dd4bc68SYOSHIFUJI Hideaki rt_expires = valid_lft * HZ; 16943dd4bc68SYOSHIFUJI Hideaki 16953dd4bc68SYOSHIFUJI Hideaki /* 16963dd4bc68SYOSHIFUJI Hideaki * We convert this (in jiffies) to clock_t later. 16973dd4bc68SYOSHIFUJI Hideaki * Avoid arithmetic overflow there as well. 16983dd4bc68SYOSHIFUJI Hideaki * Overflow can happen only if HZ < USER_HZ. 16993dd4bc68SYOSHIFUJI Hideaki */ 17003dd4bc68SYOSHIFUJI Hideaki if (HZ < USER_HZ && rt_expires > 0x7FFFFFFF / USER_HZ) 17013dd4bc68SYOSHIFUJI Hideaki rt_expires = 0x7FFFFFFF / USER_HZ; 17021da177e4SLinus Torvalds 17031da177e4SLinus Torvalds if (pinfo->onlink) { 17041da177e4SLinus Torvalds struct rt6_info *rt; 17051da177e4SLinus Torvalds rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1); 17061da177e4SLinus Torvalds 17071da177e4SLinus Torvalds if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) { 17081da177e4SLinus Torvalds if (rt->rt6i_flags&RTF_EXPIRES) { 17091da177e4SLinus Torvalds if (valid_lft == 0) { 1710e0a1ad73SThomas Graf ip6_del_rt(rt); 17111da177e4SLinus Torvalds rt = NULL; 17121da177e4SLinus Torvalds } else { 17133dd4bc68SYOSHIFUJI Hideaki rt->rt6i_expires = jiffies + rt_expires; 17141da177e4SLinus Torvalds } 17151da177e4SLinus Torvalds } 17161da177e4SLinus Torvalds } else if (valid_lft) { 17171da177e4SLinus Torvalds addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len, 17183dd4bc68SYOSHIFUJI Hideaki dev, jiffies_to_clock_t(rt_expires), RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT); 17191da177e4SLinus Torvalds } 17201da177e4SLinus Torvalds if (rt) 17211da177e4SLinus Torvalds dst_release(&rt->u.dst); 17221da177e4SLinus Torvalds } 17231da177e4SLinus Torvalds 17241da177e4SLinus Torvalds /* Try to figure out our local address for this prefix */ 17251da177e4SLinus Torvalds 17261da177e4SLinus Torvalds if (pinfo->autoconf && in6_dev->cnf.autoconf) { 17271da177e4SLinus Torvalds struct inet6_ifaddr * ifp; 17281da177e4SLinus Torvalds struct in6_addr addr; 17291da177e4SLinus Torvalds int create = 0, update_lft = 0; 17301da177e4SLinus Torvalds 17311da177e4SLinus Torvalds if (pinfo->prefix_len == 64) { 17321da177e4SLinus Torvalds memcpy(&addr, &pinfo->prefix, 8); 17331da177e4SLinus Torvalds if (ipv6_generate_eui64(addr.s6_addr + 8, dev) && 17341da177e4SLinus Torvalds ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) { 17351da177e4SLinus Torvalds in6_dev_put(in6_dev); 17361da177e4SLinus Torvalds return; 17371da177e4SLinus Torvalds } 17381da177e4SLinus Torvalds goto ok; 17391da177e4SLinus Torvalds } 17401da177e4SLinus Torvalds if (net_ratelimit()) 17411da177e4SLinus Torvalds printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n", 17421da177e4SLinus Torvalds pinfo->prefix_len); 17431da177e4SLinus Torvalds in6_dev_put(in6_dev); 17441da177e4SLinus Torvalds return; 17451da177e4SLinus Torvalds 17461da177e4SLinus Torvalds ok: 17471da177e4SLinus Torvalds 17481cab3da6SDaniel Lezcano ifp = ipv6_get_ifaddr(&init_net, &addr, dev, 1); 17491da177e4SLinus Torvalds 17501da177e4SLinus Torvalds if (ifp == NULL && valid_lft) { 17511da177e4SLinus Torvalds int max_addresses = in6_dev->cnf.max_addresses; 175295c385b4SNeil Horman u32 addr_flags = 0; 175395c385b4SNeil Horman 175495c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 175595c385b4SNeil Horman if (in6_dev->cnf.optimistic_dad && 175695c385b4SNeil Horman !ipv6_devconf.forwarding) 175795c385b4SNeil Horman addr_flags = IFA_F_OPTIMISTIC; 175895c385b4SNeil Horman #endif 17591da177e4SLinus Torvalds 17601da177e4SLinus Torvalds /* Do not allow to create too much of autoconfigured 17611da177e4SLinus Torvalds * addresses; this would be too easy way to crash kernel. 17621da177e4SLinus Torvalds */ 17631da177e4SLinus Torvalds if (!max_addresses || 17641da177e4SLinus Torvalds ipv6_count_addresses(in6_dev) < max_addresses) 17651da177e4SLinus Torvalds ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len, 176695c385b4SNeil Horman addr_type&IPV6_ADDR_SCOPE_MASK, 176795c385b4SNeil Horman addr_flags); 17681da177e4SLinus Torvalds 17691da177e4SLinus Torvalds if (!ifp || IS_ERR(ifp)) { 17701da177e4SLinus Torvalds in6_dev_put(in6_dev); 17711da177e4SLinus Torvalds return; 17721da177e4SLinus Torvalds } 17731da177e4SLinus Torvalds 17741da177e4SLinus Torvalds update_lft = create = 1; 17751da177e4SLinus Torvalds ifp->cstamp = jiffies; 17761da177e4SLinus Torvalds addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT); 17771da177e4SLinus Torvalds } 17781da177e4SLinus Torvalds 17791da177e4SLinus Torvalds if (ifp) { 17801da177e4SLinus Torvalds int flags; 17811da177e4SLinus Torvalds unsigned long now; 17821da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 17831da177e4SLinus Torvalds struct inet6_ifaddr *ift; 17841da177e4SLinus Torvalds #endif 17851da177e4SLinus Torvalds u32 stored_lft; 17861da177e4SLinus Torvalds 17871da177e4SLinus Torvalds /* update lifetime (RFC2462 5.5.3 e) */ 17881da177e4SLinus Torvalds spin_lock(&ifp->lock); 17891da177e4SLinus Torvalds now = jiffies; 17901da177e4SLinus Torvalds if (ifp->valid_lft > (now - ifp->tstamp) / HZ) 17911da177e4SLinus Torvalds stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ; 17921da177e4SLinus Torvalds else 17931da177e4SLinus Torvalds stored_lft = 0; 17941da177e4SLinus Torvalds if (!update_lft && stored_lft) { 17951da177e4SLinus Torvalds if (valid_lft > MIN_VALID_LIFETIME || 17961da177e4SLinus Torvalds valid_lft > stored_lft) 17971da177e4SLinus Torvalds update_lft = 1; 17981da177e4SLinus Torvalds else if (stored_lft <= MIN_VALID_LIFETIME) { 17991da177e4SLinus Torvalds /* valid_lft <= stored_lft is always true */ 18001da177e4SLinus Torvalds /* XXX: IPsec */ 18011da177e4SLinus Torvalds update_lft = 0; 18021da177e4SLinus Torvalds } else { 18031da177e4SLinus Torvalds valid_lft = MIN_VALID_LIFETIME; 18041da177e4SLinus Torvalds if (valid_lft < prefered_lft) 18051da177e4SLinus Torvalds prefered_lft = valid_lft; 18061da177e4SLinus Torvalds update_lft = 1; 18071da177e4SLinus Torvalds } 18081da177e4SLinus Torvalds } 18091da177e4SLinus Torvalds 18101da177e4SLinus Torvalds if (update_lft) { 18111da177e4SLinus Torvalds ifp->valid_lft = valid_lft; 18121da177e4SLinus Torvalds ifp->prefered_lft = prefered_lft; 18131da177e4SLinus Torvalds ifp->tstamp = now; 18141da177e4SLinus Torvalds flags = ifp->flags; 18151da177e4SLinus Torvalds ifp->flags &= ~IFA_F_DEPRECATED; 18161da177e4SLinus Torvalds spin_unlock(&ifp->lock); 18171da177e4SLinus Torvalds 18181da177e4SLinus Torvalds if (!(flags&IFA_F_TENTATIVE)) 18191da177e4SLinus Torvalds ipv6_ifa_notify(0, ifp); 18201da177e4SLinus Torvalds } else 18211da177e4SLinus Torvalds spin_unlock(&ifp->lock); 18221da177e4SLinus Torvalds 18231da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 18241da177e4SLinus Torvalds read_lock_bh(&in6_dev->lock); 18251da177e4SLinus Torvalds /* update all temporary addresses in the list */ 18261da177e4SLinus Torvalds for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) { 18271da177e4SLinus Torvalds /* 18281da177e4SLinus Torvalds * When adjusting the lifetimes of an existing 18291da177e4SLinus Torvalds * temporary address, only lower the lifetimes. 18301da177e4SLinus Torvalds * Implementations must not increase the 18311da177e4SLinus Torvalds * lifetimes of an existing temporary address 18321da177e4SLinus Torvalds * when processing a Prefix Information Option. 18331da177e4SLinus Torvalds */ 1834*c6fbfac2SBenoit Boissinot if (ifp != ift->ifpub) 1835*c6fbfac2SBenoit Boissinot continue; 1836*c6fbfac2SBenoit Boissinot 18371da177e4SLinus Torvalds spin_lock(&ift->lock); 18381da177e4SLinus Torvalds flags = ift->flags; 18391da177e4SLinus Torvalds if (ift->valid_lft > valid_lft && 18401da177e4SLinus Torvalds ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ) 18411da177e4SLinus Torvalds ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ; 18421da177e4SLinus Torvalds if (ift->prefered_lft > prefered_lft && 18431da177e4SLinus Torvalds ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ) 18441da177e4SLinus Torvalds ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ; 18451da177e4SLinus Torvalds spin_unlock(&ift->lock); 18461da177e4SLinus Torvalds if (!(flags&IFA_F_TENTATIVE)) 18471da177e4SLinus Torvalds ipv6_ifa_notify(0, ift); 18481da177e4SLinus Torvalds } 18491da177e4SLinus Torvalds 18501da177e4SLinus Torvalds if (create && in6_dev->cnf.use_tempaddr > 0) { 18511da177e4SLinus Torvalds /* 18521da177e4SLinus Torvalds * When a new public address is created as described in [ADDRCONF], 18531da177e4SLinus Torvalds * also create a new temporary address. 18541da177e4SLinus Torvalds */ 18551da177e4SLinus Torvalds read_unlock_bh(&in6_dev->lock); 18561da177e4SLinus Torvalds ipv6_create_tempaddr(ifp, NULL); 18571da177e4SLinus Torvalds } else { 18581da177e4SLinus Torvalds read_unlock_bh(&in6_dev->lock); 18591da177e4SLinus Torvalds } 18601da177e4SLinus Torvalds #endif 18611da177e4SLinus Torvalds in6_ifa_put(ifp); 18621da177e4SLinus Torvalds addrconf_verify(0); 18631da177e4SLinus Torvalds } 18641da177e4SLinus Torvalds } 18651da177e4SLinus Torvalds inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo); 18661da177e4SLinus Torvalds in6_dev_put(in6_dev); 18671da177e4SLinus Torvalds } 18681da177e4SLinus Torvalds 18691da177e4SLinus Torvalds /* 18701da177e4SLinus Torvalds * Set destination address. 18711da177e4SLinus Torvalds * Special case for SIT interfaces where we create a new "virtual" 18721da177e4SLinus Torvalds * device. 18731da177e4SLinus Torvalds */ 18741da177e4SLinus Torvalds int addrconf_set_dstaddr(void __user *arg) 18751da177e4SLinus Torvalds { 18761da177e4SLinus Torvalds struct in6_ifreq ireq; 18771da177e4SLinus Torvalds struct net_device *dev; 18781da177e4SLinus Torvalds int err = -EINVAL; 18791da177e4SLinus Torvalds 18801da177e4SLinus Torvalds rtnl_lock(); 18811da177e4SLinus Torvalds 18821da177e4SLinus Torvalds err = -EFAULT; 18831da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 18841da177e4SLinus Torvalds goto err_exit; 18851da177e4SLinus Torvalds 1886881d966bSEric W. Biederman dev = __dev_get_by_index(&init_net, ireq.ifr6_ifindex); 18871da177e4SLinus Torvalds 18881da177e4SLinus Torvalds err = -ENODEV; 18891da177e4SLinus Torvalds if (dev == NULL) 18901da177e4SLinus Torvalds goto err_exit; 18911da177e4SLinus Torvalds 18920be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 18931da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT) { 18941da177e4SLinus Torvalds struct ifreq ifr; 18951da177e4SLinus Torvalds mm_segment_t oldfs; 18961da177e4SLinus Torvalds struct ip_tunnel_parm p; 18971da177e4SLinus Torvalds 18981da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 18991da177e4SLinus Torvalds if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4)) 19001da177e4SLinus Torvalds goto err_exit; 19011da177e4SLinus Torvalds 19021da177e4SLinus Torvalds memset(&p, 0, sizeof(p)); 19031da177e4SLinus Torvalds p.iph.daddr = ireq.ifr6_addr.s6_addr32[3]; 19041da177e4SLinus Torvalds p.iph.saddr = 0; 19051da177e4SLinus Torvalds p.iph.version = 4; 19061da177e4SLinus Torvalds p.iph.ihl = 5; 19071da177e4SLinus Torvalds p.iph.protocol = IPPROTO_IPV6; 19081da177e4SLinus Torvalds p.iph.ttl = 64; 1909d20b3109SStephen Hemminger ifr.ifr_ifru.ifru_data = (__force void __user *)&p; 19101da177e4SLinus Torvalds 19111da177e4SLinus Torvalds oldfs = get_fs(); set_fs(KERNEL_DS); 19121da177e4SLinus Torvalds err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL); 19131da177e4SLinus Torvalds set_fs(oldfs); 19141da177e4SLinus Torvalds 19151da177e4SLinus Torvalds if (err == 0) { 19161da177e4SLinus Torvalds err = -ENOBUFS; 1917881d966bSEric W. Biederman if ((dev = __dev_get_by_name(&init_net, p.name)) == NULL) 19181da177e4SLinus Torvalds goto err_exit; 19191da177e4SLinus Torvalds err = dev_open(dev); 19201da177e4SLinus Torvalds } 19211da177e4SLinus Torvalds } 19220be669bbSJoerg Roedel #endif 19231da177e4SLinus Torvalds 19241da177e4SLinus Torvalds err_exit: 19251da177e4SLinus Torvalds rtnl_unlock(); 19261da177e4SLinus Torvalds return err; 19271da177e4SLinus Torvalds } 19281da177e4SLinus Torvalds 19291da177e4SLinus Torvalds /* 19301da177e4SLinus Torvalds * Manual configuration of address on an interface 19311da177e4SLinus Torvalds */ 19320778769dSNoriaki TAKAMIYA static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen, 193355ebaef1SNoriaki TAKAMIYA __u8 ifa_flags, __u32 prefered_lft, __u32 valid_lft) 19341da177e4SLinus Torvalds { 19351da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 19361da177e4SLinus Torvalds struct inet6_dev *idev; 19371da177e4SLinus Torvalds struct net_device *dev; 19381da177e4SLinus Torvalds int scope; 193946d48046SYOSHIFUJI Hideaki u32 flags = RTF_EXPIRES; 19401da177e4SLinus Torvalds 19411da177e4SLinus Torvalds ASSERT_RTNL(); 19421da177e4SLinus Torvalds 19430778769dSNoriaki TAKAMIYA /* check the lifetime */ 19440778769dSNoriaki TAKAMIYA if (!valid_lft || prefered_lft > valid_lft) 19450778769dSNoriaki TAKAMIYA return -EINVAL; 19460778769dSNoriaki TAKAMIYA 1947881d966bSEric W. Biederman if ((dev = __dev_get_by_index(&init_net, ifindex)) == NULL) 19481da177e4SLinus Torvalds return -ENODEV; 19491da177e4SLinus Torvalds 19501da177e4SLinus Torvalds if ((idev = addrconf_add_dev(dev)) == NULL) 19511da177e4SLinus Torvalds return -ENOBUFS; 19521da177e4SLinus Torvalds 19531da177e4SLinus Torvalds scope = ipv6_addr_scope(pfx); 19541da177e4SLinus Torvalds 195546d48046SYOSHIFUJI Hideaki if (valid_lft == INFINITY_LIFE_TIME) { 19560778769dSNoriaki TAKAMIYA ifa_flags |= IFA_F_PERMANENT; 195746d48046SYOSHIFUJI Hideaki flags = 0; 195846d48046SYOSHIFUJI Hideaki } else if (valid_lft >= 0x7FFFFFFF/HZ) 19590778769dSNoriaki TAKAMIYA valid_lft = 0x7FFFFFFF/HZ; 19600778769dSNoriaki TAKAMIYA 19610778769dSNoriaki TAKAMIYA if (prefered_lft == 0) 19620778769dSNoriaki TAKAMIYA ifa_flags |= IFA_F_DEPRECATED; 19630778769dSNoriaki TAKAMIYA else if ((prefered_lft >= 0x7FFFFFFF/HZ) && 19640778769dSNoriaki TAKAMIYA (prefered_lft != INFINITY_LIFE_TIME)) 19650778769dSNoriaki TAKAMIYA prefered_lft = 0x7FFFFFFF/HZ; 19660778769dSNoriaki TAKAMIYA 19670778769dSNoriaki TAKAMIYA ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags); 19680778769dSNoriaki TAKAMIYA 19691da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 197006aebfb7SHerbert Xu spin_lock_bh(&ifp->lock); 19710778769dSNoriaki TAKAMIYA ifp->valid_lft = valid_lft; 19720778769dSNoriaki TAKAMIYA ifp->prefered_lft = prefered_lft; 19730778769dSNoriaki TAKAMIYA ifp->tstamp = jiffies; 197406aebfb7SHerbert Xu spin_unlock_bh(&ifp->lock); 19750778769dSNoriaki TAKAMIYA 197646d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 197746d48046SYOSHIFUJI Hideaki jiffies_to_clock_t(valid_lft * HZ), flags); 197895c385b4SNeil Horman /* 197995c385b4SNeil Horman * Note that section 3.1 of RFC 4429 indicates 198095c385b4SNeil Horman * that the Optimistic flag should not be set for 198195c385b4SNeil Horman * manually configured addresses 198295c385b4SNeil Horman */ 19831da177e4SLinus Torvalds addrconf_dad_start(ifp, 0); 19841da177e4SLinus Torvalds in6_ifa_put(ifp); 19850778769dSNoriaki TAKAMIYA addrconf_verify(0); 19861da177e4SLinus Torvalds return 0; 19871da177e4SLinus Torvalds } 19881da177e4SLinus Torvalds 19891da177e4SLinus Torvalds return PTR_ERR(ifp); 19901da177e4SLinus Torvalds } 19911da177e4SLinus Torvalds 19921da177e4SLinus Torvalds static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen) 19931da177e4SLinus Torvalds { 19941da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 19951da177e4SLinus Torvalds struct inet6_dev *idev; 19961da177e4SLinus Torvalds struct net_device *dev; 19971da177e4SLinus Torvalds 1998881d966bSEric W. Biederman if ((dev = __dev_get_by_index(&init_net, ifindex)) == NULL) 19991da177e4SLinus Torvalds return -ENODEV; 20001da177e4SLinus Torvalds 20011da177e4SLinus Torvalds if ((idev = __in6_dev_get(dev)) == NULL) 20021da177e4SLinus Torvalds return -ENXIO; 20031da177e4SLinus Torvalds 20041da177e4SLinus Torvalds read_lock_bh(&idev->lock); 20051da177e4SLinus Torvalds for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) { 20061da177e4SLinus Torvalds if (ifp->prefix_len == plen && 20071da177e4SLinus Torvalds ipv6_addr_equal(pfx, &ifp->addr)) { 20081da177e4SLinus Torvalds in6_ifa_hold(ifp); 20091da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 20101da177e4SLinus Torvalds 20111da177e4SLinus Torvalds ipv6_del_addr(ifp); 20121da177e4SLinus Torvalds 20131da177e4SLinus Torvalds /* If the last address is deleted administratively, 20141da177e4SLinus Torvalds disable IPv6 on this interface. 20151da177e4SLinus Torvalds */ 20161da177e4SLinus Torvalds if (idev->addr_list == NULL) 20171da177e4SLinus Torvalds addrconf_ifdown(idev->dev, 1); 20181da177e4SLinus Torvalds return 0; 20191da177e4SLinus Torvalds } 20201da177e4SLinus Torvalds } 20211da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 20221da177e4SLinus Torvalds return -EADDRNOTAVAIL; 20231da177e4SLinus Torvalds } 20241da177e4SLinus Torvalds 20251da177e4SLinus Torvalds 20261da177e4SLinus Torvalds int addrconf_add_ifaddr(void __user *arg) 20271da177e4SLinus Torvalds { 20281da177e4SLinus Torvalds struct in6_ifreq ireq; 20291da177e4SLinus Torvalds int err; 20301da177e4SLinus Torvalds 20311da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 20321da177e4SLinus Torvalds return -EPERM; 20331da177e4SLinus Torvalds 20341da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 20351da177e4SLinus Torvalds return -EFAULT; 20361da177e4SLinus Torvalds 20371da177e4SLinus Torvalds rtnl_lock(); 20380778769dSNoriaki TAKAMIYA err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen, 203955ebaef1SNoriaki TAKAMIYA IFA_F_PERMANENT, INFINITY_LIFE_TIME, INFINITY_LIFE_TIME); 20401da177e4SLinus Torvalds rtnl_unlock(); 20411da177e4SLinus Torvalds return err; 20421da177e4SLinus Torvalds } 20431da177e4SLinus Torvalds 20441da177e4SLinus Torvalds int addrconf_del_ifaddr(void __user *arg) 20451da177e4SLinus Torvalds { 20461da177e4SLinus Torvalds struct in6_ifreq ireq; 20471da177e4SLinus Torvalds int err; 20481da177e4SLinus Torvalds 20491da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 20501da177e4SLinus Torvalds return -EPERM; 20511da177e4SLinus Torvalds 20521da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 20531da177e4SLinus Torvalds return -EFAULT; 20541da177e4SLinus Torvalds 20551da177e4SLinus Torvalds rtnl_lock(); 20561da177e4SLinus Torvalds err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen); 20571da177e4SLinus Torvalds rtnl_unlock(); 20581da177e4SLinus Torvalds return err; 20591da177e4SLinus Torvalds } 20601da177e4SLinus Torvalds 20610be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 20621da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev) 20631da177e4SLinus Torvalds { 20641da177e4SLinus Torvalds struct inet6_ifaddr * ifp; 20651da177e4SLinus Torvalds struct in6_addr addr; 20661da177e4SLinus Torvalds struct net_device *dev; 20671da177e4SLinus Torvalds int scope; 20681da177e4SLinus Torvalds 20691da177e4SLinus Torvalds ASSERT_RTNL(); 20701da177e4SLinus Torvalds 20711da177e4SLinus Torvalds memset(&addr, 0, sizeof(struct in6_addr)); 20721da177e4SLinus Torvalds memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4); 20731da177e4SLinus Torvalds 20741da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) { 20751da177e4SLinus Torvalds addr.s6_addr32[0] = htonl(0xfe800000); 20761da177e4SLinus Torvalds scope = IFA_LINK; 20771da177e4SLinus Torvalds } else { 20781da177e4SLinus Torvalds scope = IPV6_ADDR_COMPATv4; 20791da177e4SLinus Torvalds } 20801da177e4SLinus Torvalds 20811da177e4SLinus Torvalds if (addr.s6_addr32[3]) { 20821da177e4SLinus Torvalds ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT); 20831da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 20841da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 20851da177e4SLinus Torvalds ifp->flags &= ~IFA_F_TENTATIVE; 20861da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 20871da177e4SLinus Torvalds ipv6_ifa_notify(RTM_NEWADDR, ifp); 20881da177e4SLinus Torvalds in6_ifa_put(ifp); 20891da177e4SLinus Torvalds } 20901da177e4SLinus Torvalds return; 20911da177e4SLinus Torvalds } 20921da177e4SLinus Torvalds 2093881d966bSEric W. Biederman for_each_netdev(&init_net, dev) { 2094e5ed6399SHerbert Xu struct in_device * in_dev = __in_dev_get_rtnl(dev); 20951da177e4SLinus Torvalds if (in_dev && (dev->flags & IFF_UP)) { 20961da177e4SLinus Torvalds struct in_ifaddr * ifa; 20971da177e4SLinus Torvalds 20981da177e4SLinus Torvalds int flag = scope; 20991da177e4SLinus Torvalds 21001da177e4SLinus Torvalds for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) { 21011da177e4SLinus Torvalds int plen; 21021da177e4SLinus Torvalds 21031da177e4SLinus Torvalds addr.s6_addr32[3] = ifa->ifa_local; 21041da177e4SLinus Torvalds 21051da177e4SLinus Torvalds if (ifa->ifa_scope == RT_SCOPE_LINK) 21061da177e4SLinus Torvalds continue; 21071da177e4SLinus Torvalds if (ifa->ifa_scope >= RT_SCOPE_HOST) { 21081da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) 21091da177e4SLinus Torvalds continue; 21101da177e4SLinus Torvalds flag |= IFA_HOST; 21111da177e4SLinus Torvalds } 21121da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) 21131da177e4SLinus Torvalds plen = 64; 21141da177e4SLinus Torvalds else 21151da177e4SLinus Torvalds plen = 96; 21161da177e4SLinus Torvalds 21171da177e4SLinus Torvalds ifp = ipv6_add_addr(idev, &addr, plen, flag, 21181da177e4SLinus Torvalds IFA_F_PERMANENT); 21191da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 21201da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 21211da177e4SLinus Torvalds ifp->flags &= ~IFA_F_TENTATIVE; 21221da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 21231da177e4SLinus Torvalds ipv6_ifa_notify(RTM_NEWADDR, ifp); 21241da177e4SLinus Torvalds in6_ifa_put(ifp); 21251da177e4SLinus Torvalds } 21261da177e4SLinus Torvalds } 21271da177e4SLinus Torvalds } 21281da177e4SLinus Torvalds } 21291da177e4SLinus Torvalds } 21300be669bbSJoerg Roedel #endif 21311da177e4SLinus Torvalds 21321da177e4SLinus Torvalds static void init_loopback(struct net_device *dev) 21331da177e4SLinus Torvalds { 21341da177e4SLinus Torvalds struct inet6_dev *idev; 21351da177e4SLinus Torvalds struct inet6_ifaddr * ifp; 21361da177e4SLinus Torvalds 21371da177e4SLinus Torvalds /* ::1 */ 21381da177e4SLinus Torvalds 21391da177e4SLinus Torvalds ASSERT_RTNL(); 21401da177e4SLinus Torvalds 21411da177e4SLinus Torvalds if ((idev = ipv6_find_idev(dev)) == NULL) { 21421da177e4SLinus Torvalds printk(KERN_DEBUG "init loopback: add_dev failed\n"); 21431da177e4SLinus Torvalds return; 21441da177e4SLinus Torvalds } 21451da177e4SLinus Torvalds 21461da177e4SLinus Torvalds ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT); 21471da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 21481da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 21491da177e4SLinus Torvalds ifp->flags &= ~IFA_F_TENTATIVE; 21501da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 21511da177e4SLinus Torvalds ipv6_ifa_notify(RTM_NEWADDR, ifp); 21521da177e4SLinus Torvalds in6_ifa_put(ifp); 21531da177e4SLinus Torvalds } 21541da177e4SLinus Torvalds } 21551da177e4SLinus Torvalds 21561da177e4SLinus Torvalds static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr) 21571da177e4SLinus Torvalds { 21581da177e4SLinus Torvalds struct inet6_ifaddr * ifp; 215995c385b4SNeil Horman u32 addr_flags = IFA_F_PERMANENT; 21601da177e4SLinus Torvalds 216195c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 216295c385b4SNeil Horman if (idev->cnf.optimistic_dad && 216395c385b4SNeil Horman !ipv6_devconf.forwarding) 216495c385b4SNeil Horman addr_flags |= IFA_F_OPTIMISTIC; 216595c385b4SNeil Horman #endif 216695c385b4SNeil Horman 216795c385b4SNeil Horman 216895c385b4SNeil Horman ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, addr_flags); 21691da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 217046d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0); 21711da177e4SLinus Torvalds addrconf_dad_start(ifp, 0); 21721da177e4SLinus Torvalds in6_ifa_put(ifp); 21731da177e4SLinus Torvalds } 21741da177e4SLinus Torvalds } 21751da177e4SLinus Torvalds 21761da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev) 21771da177e4SLinus Torvalds { 21781da177e4SLinus Torvalds struct in6_addr addr; 21791da177e4SLinus Torvalds struct inet6_dev * idev; 21801da177e4SLinus Torvalds 21811da177e4SLinus Torvalds ASSERT_RTNL(); 21821da177e4SLinus Torvalds 218374235a25SHerbert Xu if ((dev->type != ARPHRD_ETHER) && 218474235a25SHerbert Xu (dev->type != ARPHRD_FDDI) && 218574235a25SHerbert Xu (dev->type != ARPHRD_IEEE802_TR) && 218674235a25SHerbert Xu (dev->type != ARPHRD_ARCNET) && 218774235a25SHerbert Xu (dev->type != ARPHRD_INFINIBAND)) { 218874235a25SHerbert Xu /* Alas, we support only Ethernet autoconfiguration. */ 218974235a25SHerbert Xu return; 219074235a25SHerbert Xu } 219174235a25SHerbert Xu 21921da177e4SLinus Torvalds idev = addrconf_add_dev(dev); 21931da177e4SLinus Torvalds if (idev == NULL) 21941da177e4SLinus Torvalds return; 21951da177e4SLinus Torvalds 21961da177e4SLinus Torvalds memset(&addr, 0, sizeof(struct in6_addr)); 21971da177e4SLinus Torvalds addr.s6_addr32[0] = htonl(0xFE800000); 21981da177e4SLinus Torvalds 21991da177e4SLinus Torvalds if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0) 22001da177e4SLinus Torvalds addrconf_add_linklocal(idev, &addr); 22011da177e4SLinus Torvalds } 22021da177e4SLinus Torvalds 22030be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 22041da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev) 22051da177e4SLinus Torvalds { 22061da177e4SLinus Torvalds struct inet6_dev *idev; 22071da177e4SLinus Torvalds 22081da177e4SLinus Torvalds ASSERT_RTNL(); 22091da177e4SLinus Torvalds 22101da177e4SLinus Torvalds /* 22111da177e4SLinus Torvalds * Configure the tunnel with one of our IPv4 22121da177e4SLinus Torvalds * addresses... we should configure all of 22131da177e4SLinus Torvalds * our v4 addrs in the tunnel 22141da177e4SLinus Torvalds */ 22151da177e4SLinus Torvalds 22161da177e4SLinus Torvalds if ((idev = ipv6_find_idev(dev)) == NULL) { 22171da177e4SLinus Torvalds printk(KERN_DEBUG "init sit: add_dev failed\n"); 22181da177e4SLinus Torvalds return; 22191da177e4SLinus Torvalds } 22201da177e4SLinus Torvalds 2221c7dc89c0SFred L. Templin if (dev->priv_flags & IFF_ISATAP) { 2222c7dc89c0SFred L. Templin struct in6_addr addr; 2223c7dc89c0SFred L. Templin 2224c7dc89c0SFred L. Templin ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0); 2225c7dc89c0SFred L. Templin addrconf_prefix_route(&addr, 64, dev, 0, 0); 2226c7dc89c0SFred L. Templin if (!ipv6_generate_eui64(addr.s6_addr + 8, dev)) 2227c7dc89c0SFred L. Templin addrconf_add_linklocal(idev, &addr); 2228c7dc89c0SFred L. Templin return; 2229c7dc89c0SFred L. Templin } 2230c7dc89c0SFred L. Templin 22311da177e4SLinus Torvalds sit_add_v4_addrs(idev); 22321da177e4SLinus Torvalds 22331da177e4SLinus Torvalds if (dev->flags&IFF_POINTOPOINT) { 22341da177e4SLinus Torvalds addrconf_add_mroute(dev); 22351da177e4SLinus Torvalds addrconf_add_lroute(dev); 22361da177e4SLinus Torvalds } else 22371da177e4SLinus Torvalds sit_route_add(dev); 22381da177e4SLinus Torvalds } 22390be669bbSJoerg Roedel #endif 22401da177e4SLinus Torvalds 22411da177e4SLinus Torvalds static inline int 22421da177e4SLinus Torvalds ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev) 22431da177e4SLinus Torvalds { 22441da177e4SLinus Torvalds struct in6_addr lladdr; 22451da177e4SLinus Torvalds 224695c385b4SNeil Horman if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) { 22471da177e4SLinus Torvalds addrconf_add_linklocal(idev, &lladdr); 22481da177e4SLinus Torvalds return 0; 22491da177e4SLinus Torvalds } 22501da177e4SLinus Torvalds return -1; 22511da177e4SLinus Torvalds } 22521da177e4SLinus Torvalds 22531da177e4SLinus Torvalds static void ip6_tnl_add_linklocal(struct inet6_dev *idev) 22541da177e4SLinus Torvalds { 22551da177e4SLinus Torvalds struct net_device *link_dev; 22561da177e4SLinus Torvalds 22571da177e4SLinus Torvalds /* first try to inherit the link-local address from the link device */ 22581da177e4SLinus Torvalds if (idev->dev->iflink && 2259881d966bSEric W. Biederman (link_dev = __dev_get_by_index(&init_net, idev->dev->iflink))) { 22601da177e4SLinus Torvalds if (!ipv6_inherit_linklocal(idev, link_dev)) 22611da177e4SLinus Torvalds return; 22621da177e4SLinus Torvalds } 22631da177e4SLinus Torvalds /* then try to inherit it from any device */ 2264881d966bSEric W. Biederman for_each_netdev(&init_net, link_dev) { 22651da177e4SLinus Torvalds if (!ipv6_inherit_linklocal(idev, link_dev)) 22661da177e4SLinus Torvalds return; 22671da177e4SLinus Torvalds } 22681da177e4SLinus Torvalds printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n"); 22691da177e4SLinus Torvalds } 22701da177e4SLinus Torvalds 22711da177e4SLinus Torvalds /* 22721da177e4SLinus Torvalds * Autoconfigure tunnel with a link-local address so routing protocols, 22731da177e4SLinus Torvalds * DHCPv6, MLD etc. can be run over the virtual link 22741da177e4SLinus Torvalds */ 22751da177e4SLinus Torvalds 22761da177e4SLinus Torvalds static void addrconf_ip6_tnl_config(struct net_device *dev) 22771da177e4SLinus Torvalds { 22781da177e4SLinus Torvalds struct inet6_dev *idev; 22791da177e4SLinus Torvalds 22801da177e4SLinus Torvalds ASSERT_RTNL(); 22811da177e4SLinus Torvalds 22821da177e4SLinus Torvalds if ((idev = addrconf_add_dev(dev)) == NULL) { 22831da177e4SLinus Torvalds printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n"); 22841da177e4SLinus Torvalds return; 22851da177e4SLinus Torvalds } 22861da177e4SLinus Torvalds ip6_tnl_add_linklocal(idev); 22871da177e4SLinus Torvalds } 22881da177e4SLinus Torvalds 22891da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event, 22901da177e4SLinus Torvalds void * data) 22911da177e4SLinus Torvalds { 22921da177e4SLinus Torvalds struct net_device *dev = (struct net_device *) data; 229374235a25SHerbert Xu struct inet6_dev *idev = __in6_dev_get(dev); 2294c5e33bddSYOSHIFUJI Hideaki int run_pending = 0; 2295b217d616SHerbert Xu int err; 22961da177e4SLinus Torvalds 2297e9dc8653SEric W. Biederman if (dev->nd_net != &init_net) 2298e9dc8653SEric W. Biederman return NOTIFY_DONE; 2299e9dc8653SEric W. Biederman 23001da177e4SLinus Torvalds switch(event) { 230145ba9dd2SYOSHIFUJI Hideaki case NETDEV_REGISTER: 230274235a25SHerbert Xu if (!idev && dev->mtu >= IPV6_MIN_MTU) { 230345ba9dd2SYOSHIFUJI Hideaki idev = ipv6_add_dev(dev); 230445ba9dd2SYOSHIFUJI Hideaki if (!idev) 2305b217d616SHerbert Xu return notifier_from_errno(-ENOMEM); 230645ba9dd2SYOSHIFUJI Hideaki } 230745ba9dd2SYOSHIFUJI Hideaki break; 23081da177e4SLinus Torvalds case NETDEV_UP: 23093c21edbdSYOSHIFUJI Hideaki case NETDEV_CHANGE: 2310c2edacf8SJay Vosburgh if (dev->flags & IFF_SLAVE) 2311c2edacf8SJay Vosburgh break; 2312c2edacf8SJay Vosburgh 23133c21edbdSYOSHIFUJI Hideaki if (event == NETDEV_UP) { 2314f24e3d65SMitsuru Chinen if (!addrconf_qdisc_ok(dev)) { 23153c21edbdSYOSHIFUJI Hideaki /* device is not ready yet. */ 23163c21edbdSYOSHIFUJI Hideaki printk(KERN_INFO 23173c21edbdSYOSHIFUJI Hideaki "ADDRCONF(NETDEV_UP): %s: " 23183c21edbdSYOSHIFUJI Hideaki "link is not ready\n", 23193c21edbdSYOSHIFUJI Hideaki dev->name); 23203c21edbdSYOSHIFUJI Hideaki break; 23213c21edbdSYOSHIFUJI Hideaki } 232299081049SKristian Slavov 2323d31c7b8fSEvgeniy Polyakov if (!idev && dev->mtu >= IPV6_MIN_MTU) 2324d31c7b8fSEvgeniy Polyakov idev = ipv6_add_dev(dev); 2325d31c7b8fSEvgeniy Polyakov 232699081049SKristian Slavov if (idev) 232799081049SKristian Slavov idev->if_flags |= IF_READY; 23283c21edbdSYOSHIFUJI Hideaki } else { 2329f24e3d65SMitsuru Chinen if (!addrconf_qdisc_ok(dev)) { 23303c21edbdSYOSHIFUJI Hideaki /* device is still not ready. */ 23313c21edbdSYOSHIFUJI Hideaki break; 23323c21edbdSYOSHIFUJI Hideaki } 23333c21edbdSYOSHIFUJI Hideaki 23343c21edbdSYOSHIFUJI Hideaki if (idev) { 23353c21edbdSYOSHIFUJI Hideaki if (idev->if_flags & IF_READY) { 23363c21edbdSYOSHIFUJI Hideaki /* device is already configured. */ 23373c21edbdSYOSHIFUJI Hideaki break; 23383c21edbdSYOSHIFUJI Hideaki } 23393c21edbdSYOSHIFUJI Hideaki idev->if_flags |= IF_READY; 23403c21edbdSYOSHIFUJI Hideaki } 23413c21edbdSYOSHIFUJI Hideaki 23423c21edbdSYOSHIFUJI Hideaki printk(KERN_INFO 23433c21edbdSYOSHIFUJI Hideaki "ADDRCONF(NETDEV_CHANGE): %s: " 23443c21edbdSYOSHIFUJI Hideaki "link becomes ready\n", 23453c21edbdSYOSHIFUJI Hideaki dev->name); 23463c21edbdSYOSHIFUJI Hideaki 2347c5e33bddSYOSHIFUJI Hideaki run_pending = 1; 23483c21edbdSYOSHIFUJI Hideaki } 23493c21edbdSYOSHIFUJI Hideaki 23501da177e4SLinus Torvalds switch(dev->type) { 23510be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 23521da177e4SLinus Torvalds case ARPHRD_SIT: 23531da177e4SLinus Torvalds addrconf_sit_config(dev); 23541da177e4SLinus Torvalds break; 23550be669bbSJoerg Roedel #endif 23561da177e4SLinus Torvalds case ARPHRD_TUNNEL6: 23571da177e4SLinus Torvalds addrconf_ip6_tnl_config(dev); 23581da177e4SLinus Torvalds break; 23591da177e4SLinus Torvalds case ARPHRD_LOOPBACK: 23601da177e4SLinus Torvalds init_loopback(dev); 23611da177e4SLinus Torvalds break; 23621da177e4SLinus Torvalds 23631da177e4SLinus Torvalds default: 23641da177e4SLinus Torvalds addrconf_dev_config(dev); 23651da177e4SLinus Torvalds break; 23663ff50b79SStephen Hemminger } 23671da177e4SLinus Torvalds if (idev) { 2368c5e33bddSYOSHIFUJI Hideaki if (run_pending) 2369c5e33bddSYOSHIFUJI Hideaki addrconf_dad_run(idev); 2370c5e33bddSYOSHIFUJI Hideaki 23711da177e4SLinus Torvalds /* If the MTU changed during the interface down, when the 23721da177e4SLinus Torvalds interface up, the changed MTU must be reflected in the 23731da177e4SLinus Torvalds idev as well as routers. 23741da177e4SLinus Torvalds */ 23751da177e4SLinus Torvalds if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) { 23761da177e4SLinus Torvalds rt6_mtu_change(dev, dev->mtu); 23771da177e4SLinus Torvalds idev->cnf.mtu6 = dev->mtu; 23781da177e4SLinus Torvalds } 23791da177e4SLinus Torvalds idev->tstamp = jiffies; 23801da177e4SLinus Torvalds inet6_ifinfo_notify(RTM_NEWLINK, idev); 23811da177e4SLinus Torvalds /* If the changed mtu during down is lower than IPV6_MIN_MTU 23821da177e4SLinus Torvalds stop IPv6 on this interface. 23831da177e4SLinus Torvalds */ 23841da177e4SLinus Torvalds if (dev->mtu < IPV6_MIN_MTU) 23851da177e4SLinus Torvalds addrconf_ifdown(dev, event != NETDEV_DOWN); 23861da177e4SLinus Torvalds } 23871da177e4SLinus Torvalds break; 23881da177e4SLinus Torvalds 23891da177e4SLinus Torvalds case NETDEV_CHANGEMTU: 23901da177e4SLinus Torvalds if (idev && dev->mtu >= IPV6_MIN_MTU) { 23911da177e4SLinus Torvalds rt6_mtu_change(dev, dev->mtu); 23921da177e4SLinus Torvalds idev->cnf.mtu6 = dev->mtu; 23931da177e4SLinus Torvalds break; 23941da177e4SLinus Torvalds } 23951da177e4SLinus Torvalds 2396d31c7b8fSEvgeniy Polyakov if (!idev && dev->mtu >= IPV6_MIN_MTU) { 2397d31c7b8fSEvgeniy Polyakov idev = ipv6_add_dev(dev); 2398d31c7b8fSEvgeniy Polyakov if (idev) 2399d31c7b8fSEvgeniy Polyakov break; 2400d31c7b8fSEvgeniy Polyakov } 2401d31c7b8fSEvgeniy Polyakov 24021da177e4SLinus Torvalds /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */ 24031da177e4SLinus Torvalds 24041da177e4SLinus Torvalds case NETDEV_DOWN: 24051da177e4SLinus Torvalds case NETDEV_UNREGISTER: 24061da177e4SLinus Torvalds /* 24071da177e4SLinus Torvalds * Remove all addresses from this interface. 24081da177e4SLinus Torvalds */ 24091da177e4SLinus Torvalds addrconf_ifdown(dev, event != NETDEV_DOWN); 24101da177e4SLinus Torvalds break; 24113c21edbdSYOSHIFUJI Hideaki 24121da177e4SLinus Torvalds case NETDEV_CHANGENAME: 24131da177e4SLinus Torvalds if (idev) { 24145632c515SStephen Hemminger snmp6_unregister_dev(idev); 2415408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 2416f52295a9SPavel Emelyanov addrconf_sysctl_register(idev); 2417b217d616SHerbert Xu err = snmp6_register_dev(idev); 2418b217d616SHerbert Xu if (err) 2419b217d616SHerbert Xu return notifier_from_errno(err); 24205632c515SStephen Hemminger } 24211da177e4SLinus Torvalds break; 24223ff50b79SStephen Hemminger } 24231da177e4SLinus Torvalds 24241da177e4SLinus Torvalds return NOTIFY_OK; 24251da177e4SLinus Torvalds } 24261da177e4SLinus Torvalds 24271da177e4SLinus Torvalds /* 24281da177e4SLinus Torvalds * addrconf module should be notified of a device going up 24291da177e4SLinus Torvalds */ 24301da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = { 24311da177e4SLinus Torvalds .notifier_call = addrconf_notify, 24321da177e4SLinus Torvalds .priority = 0 24331da177e4SLinus Torvalds }; 24341da177e4SLinus Torvalds 24351da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how) 24361da177e4SLinus Torvalds { 24371da177e4SLinus Torvalds struct inet6_dev *idev; 24381da177e4SLinus Torvalds struct inet6_ifaddr *ifa, **bifa; 24391da177e4SLinus Torvalds int i; 24401da177e4SLinus Torvalds 24411da177e4SLinus Torvalds ASSERT_RTNL(); 24421da177e4SLinus Torvalds 24432774c7abSEric W. Biederman if (dev == init_net.loopback_dev && how == 1) 24441da177e4SLinus Torvalds how = 0; 24451da177e4SLinus Torvalds 24461da177e4SLinus Torvalds rt6_ifdown(dev); 24471da177e4SLinus Torvalds neigh_ifdown(&nd_tbl, dev); 24481da177e4SLinus Torvalds 24491da177e4SLinus Torvalds idev = __in6_dev_get(dev); 24501da177e4SLinus Torvalds if (idev == NULL) 24511da177e4SLinus Torvalds return -ENODEV; 24521da177e4SLinus Torvalds 24531da177e4SLinus Torvalds /* Step 1: remove reference to ipv6 device from parent device. 24541da177e4SLinus Torvalds Do not dev_put! 24551da177e4SLinus Torvalds */ 24561da177e4SLinus Torvalds if (how == 1) { 24571da177e4SLinus Torvalds idev->dead = 1; 24588814c4b5SYOSHIFUJI Hideaki 24598814c4b5SYOSHIFUJI Hideaki /* protected by rtnl_lock */ 24608814c4b5SYOSHIFUJI Hideaki rcu_assign_pointer(dev->ip6_ptr, NULL); 24611da177e4SLinus Torvalds 24621da177e4SLinus Torvalds /* Step 1.5: remove snmp6 entry */ 24631da177e4SLinus Torvalds snmp6_unregister_dev(idev); 24641da177e4SLinus Torvalds 24651da177e4SLinus Torvalds } 24661da177e4SLinus Torvalds 24671da177e4SLinus Torvalds /* Step 2: clear hash table */ 24681da177e4SLinus Torvalds for (i=0; i<IN6_ADDR_HSIZE; i++) { 24691da177e4SLinus Torvalds bifa = &inet6_addr_lst[i]; 24701da177e4SLinus Torvalds 24711da177e4SLinus Torvalds write_lock_bh(&addrconf_hash_lock); 24721da177e4SLinus Torvalds while ((ifa = *bifa) != NULL) { 24731da177e4SLinus Torvalds if (ifa->idev == idev) { 24741da177e4SLinus Torvalds *bifa = ifa->lst_next; 24751da177e4SLinus Torvalds ifa->lst_next = NULL; 24761da177e4SLinus Torvalds addrconf_del_timer(ifa); 24771da177e4SLinus Torvalds in6_ifa_put(ifa); 24781da177e4SLinus Torvalds continue; 24791da177e4SLinus Torvalds } 24801da177e4SLinus Torvalds bifa = &ifa->lst_next; 24811da177e4SLinus Torvalds } 24821da177e4SLinus Torvalds write_unlock_bh(&addrconf_hash_lock); 24831da177e4SLinus Torvalds } 24841da177e4SLinus Torvalds 24851da177e4SLinus Torvalds write_lock_bh(&idev->lock); 24861da177e4SLinus Torvalds 24871da177e4SLinus Torvalds /* Step 3: clear flags for stateless addrconf */ 24881da177e4SLinus Torvalds if (how != 1) 24893c21edbdSYOSHIFUJI Hideaki idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY); 24901da177e4SLinus Torvalds 24911da177e4SLinus Torvalds /* Step 4: clear address list */ 24921da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 24931da177e4SLinus Torvalds if (how == 1 && del_timer(&idev->regen_timer)) 24941da177e4SLinus Torvalds in6_dev_put(idev); 24951da177e4SLinus Torvalds 24961da177e4SLinus Torvalds /* clear tempaddr list */ 24971da177e4SLinus Torvalds while ((ifa = idev->tempaddr_list) != NULL) { 24981da177e4SLinus Torvalds idev->tempaddr_list = ifa->tmp_next; 24991da177e4SLinus Torvalds ifa->tmp_next = NULL; 25001da177e4SLinus Torvalds ifa->dead = 1; 25011da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 25021da177e4SLinus Torvalds spin_lock_bh(&ifa->lock); 25031da177e4SLinus Torvalds 25041da177e4SLinus Torvalds if (ifa->ifpub) { 25051da177e4SLinus Torvalds in6_ifa_put(ifa->ifpub); 25061da177e4SLinus Torvalds ifa->ifpub = NULL; 25071da177e4SLinus Torvalds } 25081da177e4SLinus Torvalds spin_unlock_bh(&ifa->lock); 25091da177e4SLinus Torvalds in6_ifa_put(ifa); 25101da177e4SLinus Torvalds write_lock_bh(&idev->lock); 25111da177e4SLinus Torvalds } 25121da177e4SLinus Torvalds #endif 25131da177e4SLinus Torvalds while ((ifa = idev->addr_list) != NULL) { 25141da177e4SLinus Torvalds idev->addr_list = ifa->if_next; 25151da177e4SLinus Torvalds ifa->if_next = NULL; 25161da177e4SLinus Torvalds ifa->dead = 1; 25171da177e4SLinus Torvalds addrconf_del_timer(ifa); 25181da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 25191da177e4SLinus Torvalds 25201da177e4SLinus Torvalds __ipv6_ifa_notify(RTM_DELADDR, ifa); 2521063ed369SVlad Yasevich atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifa); 25221da177e4SLinus Torvalds in6_ifa_put(ifa); 25231da177e4SLinus Torvalds 25241da177e4SLinus Torvalds write_lock_bh(&idev->lock); 25251da177e4SLinus Torvalds } 25261da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 25271da177e4SLinus Torvalds 25281da177e4SLinus Torvalds /* Step 5: Discard multicast list */ 25291da177e4SLinus Torvalds 25301da177e4SLinus Torvalds if (how == 1) 25311da177e4SLinus Torvalds ipv6_mc_destroy_dev(idev); 25321da177e4SLinus Torvalds else 25331da177e4SLinus Torvalds ipv6_mc_down(idev); 25341da177e4SLinus Torvalds 25351da177e4SLinus Torvalds idev->tstamp = jiffies; 25361da177e4SLinus Torvalds 25371da177e4SLinus Torvalds /* Shot the device (if unregistered) */ 25381da177e4SLinus Torvalds 25391da177e4SLinus Torvalds if (how == 1) { 2540408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 25411da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, idev->nd_parms); 25421da177e4SLinus Torvalds neigh_ifdown(&nd_tbl, dev); 25431da177e4SLinus Torvalds in6_dev_put(idev); 25441da177e4SLinus Torvalds } 25451da177e4SLinus Torvalds return 0; 25461da177e4SLinus Torvalds } 25471da177e4SLinus Torvalds 25481da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data) 25491da177e4SLinus Torvalds { 25501da177e4SLinus Torvalds struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data; 25511da177e4SLinus Torvalds 25521da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 25531da177e4SLinus Torvalds goto out; 25541da177e4SLinus Torvalds 25551da177e4SLinus Torvalds if (ifp->idev->if_flags & IF_RA_RCVD) { 25561da177e4SLinus Torvalds /* 25571da177e4SLinus Torvalds * Announcement received after solicitation 25581da177e4SLinus Torvalds * was sent 25591da177e4SLinus Torvalds */ 25601da177e4SLinus Torvalds goto out; 25611da177e4SLinus Torvalds } 25621da177e4SLinus Torvalds 25631da177e4SLinus Torvalds spin_lock(&ifp->lock); 25641da177e4SLinus Torvalds if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) { 25651da177e4SLinus Torvalds struct in6_addr all_routers; 25661da177e4SLinus Torvalds 25671da177e4SLinus Torvalds /* The wait after the last probe can be shorter */ 25681da177e4SLinus Torvalds addrconf_mod_timer(ifp, AC_RS, 25691da177e4SLinus Torvalds (ifp->probes == ifp->idev->cnf.rtr_solicits) ? 25701da177e4SLinus Torvalds ifp->idev->cnf.rtr_solicit_delay : 25711da177e4SLinus Torvalds ifp->idev->cnf.rtr_solicit_interval); 25721da177e4SLinus Torvalds spin_unlock(&ifp->lock); 25731da177e4SLinus Torvalds 25741da177e4SLinus Torvalds ipv6_addr_all_routers(&all_routers); 25751da177e4SLinus Torvalds 25761da177e4SLinus Torvalds ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers); 25771da177e4SLinus Torvalds } else { 25781da177e4SLinus Torvalds spin_unlock(&ifp->lock); 25791da177e4SLinus Torvalds /* 25801da177e4SLinus Torvalds * Note: we do not support deprecated "all on-link" 25811da177e4SLinus Torvalds * assumption any longer. 25821da177e4SLinus Torvalds */ 25831da177e4SLinus Torvalds printk(KERN_DEBUG "%s: no IPv6 routers present\n", 25841da177e4SLinus Torvalds ifp->idev->dev->name); 25851da177e4SLinus Torvalds } 25861da177e4SLinus Torvalds 25871da177e4SLinus Torvalds out: 25881da177e4SLinus Torvalds in6_ifa_put(ifp); 25891da177e4SLinus Torvalds } 25901da177e4SLinus Torvalds 25911da177e4SLinus Torvalds /* 25921da177e4SLinus Torvalds * Duplicate Address Detection 25931da177e4SLinus Torvalds */ 25943c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp) 25953c21edbdSYOSHIFUJI Hideaki { 25963c21edbdSYOSHIFUJI Hideaki unsigned long rand_num; 25973c21edbdSYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev; 25983c21edbdSYOSHIFUJI Hideaki 259995c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 260095c385b4SNeil Horman rand_num = 0; 260195c385b4SNeil Horman else 26023c21edbdSYOSHIFUJI Hideaki rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1); 260395c385b4SNeil Horman 26043c21edbdSYOSHIFUJI Hideaki ifp->probes = idev->cnf.dad_transmits; 26053c21edbdSYOSHIFUJI Hideaki addrconf_mod_timer(ifp, AC_DAD, rand_num); 26063c21edbdSYOSHIFUJI Hideaki } 26073c21edbdSYOSHIFUJI Hideaki 2608e431b8c0SJamal Hadi Salim static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags) 26091da177e4SLinus Torvalds { 26101da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 26111da177e4SLinus Torvalds struct net_device *dev = idev->dev; 26121da177e4SLinus Torvalds 26131da177e4SLinus Torvalds addrconf_join_solict(dev, &ifp->addr); 26141da177e4SLinus Torvalds 26151da177e4SLinus Torvalds net_srandom(ifp->addr.s6_addr32[3]); 26161da177e4SLinus Torvalds 26171da177e4SLinus Torvalds read_lock_bh(&idev->lock); 26181da177e4SLinus Torvalds if (ifp->dead) 26191da177e4SLinus Torvalds goto out; 26201da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 26211da177e4SLinus Torvalds 26221da177e4SLinus Torvalds if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) || 262355ebaef1SNoriaki TAKAMIYA !(ifp->flags&IFA_F_TENTATIVE) || 262455ebaef1SNoriaki TAKAMIYA ifp->flags & IFA_F_NODAD) { 262595c385b4SNeil Horman ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC); 26261da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 26271da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 26281da177e4SLinus Torvalds 26291da177e4SLinus Torvalds addrconf_dad_completed(ifp); 26301da177e4SLinus Torvalds return; 26311da177e4SLinus Torvalds } 26321da177e4SLinus Torvalds 26336732badeSYOSHIFUJI Hideaki if (!(idev->if_flags & IF_READY)) { 26343dd3bf83SYOSHIFUJI Hideaki spin_unlock_bh(&ifp->lock); 26356732badeSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 26363c21edbdSYOSHIFUJI Hideaki /* 26373c21edbdSYOSHIFUJI Hideaki * If the defice is not ready: 26383c21edbdSYOSHIFUJI Hideaki * - keep it tentative if it is a permanent address. 26393c21edbdSYOSHIFUJI Hideaki * - otherwise, kill it. 26403c21edbdSYOSHIFUJI Hideaki */ 26413c21edbdSYOSHIFUJI Hideaki in6_ifa_hold(ifp); 26423c21edbdSYOSHIFUJI Hideaki addrconf_dad_stop(ifp); 26436732badeSYOSHIFUJI Hideaki return; 26443c21edbdSYOSHIFUJI Hideaki } 264595c385b4SNeil Horman 264695c385b4SNeil Horman /* 264795c385b4SNeil Horman * Optimistic nodes can start receiving 264895c385b4SNeil Horman * Frames right away 264995c385b4SNeil Horman */ 265095c385b4SNeil Horman if(ifp->flags & IFA_F_OPTIMISTIC) 265195c385b4SNeil Horman ip6_ins_rt(ifp->rt); 265295c385b4SNeil Horman 26536732badeSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 26546732badeSYOSHIFUJI Hideaki spin_unlock_bh(&ifp->lock); 26551da177e4SLinus Torvalds out: 26561da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 26571da177e4SLinus Torvalds } 26581da177e4SLinus Torvalds 26591da177e4SLinus Torvalds static void addrconf_dad_timer(unsigned long data) 26601da177e4SLinus Torvalds { 26611da177e4SLinus Torvalds struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data; 26621da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 26631da177e4SLinus Torvalds struct in6_addr unspec; 26641da177e4SLinus Torvalds struct in6_addr mcaddr; 26651da177e4SLinus Torvalds 26661da177e4SLinus Torvalds read_lock_bh(&idev->lock); 26671da177e4SLinus Torvalds if (idev->dead) { 26681da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 26691da177e4SLinus Torvalds goto out; 26701da177e4SLinus Torvalds } 26711da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 26721da177e4SLinus Torvalds if (ifp->probes == 0) { 26731da177e4SLinus Torvalds /* 26741da177e4SLinus Torvalds * DAD was successful 26751da177e4SLinus Torvalds */ 26761da177e4SLinus Torvalds 267795c385b4SNeil Horman ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC); 26781da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 26791da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 26801da177e4SLinus Torvalds 26811da177e4SLinus Torvalds addrconf_dad_completed(ifp); 26821da177e4SLinus Torvalds 26831da177e4SLinus Torvalds goto out; 26841da177e4SLinus Torvalds } 26851da177e4SLinus Torvalds 26861da177e4SLinus Torvalds ifp->probes--; 26871da177e4SLinus Torvalds addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time); 26881da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 26891da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 26901da177e4SLinus Torvalds 26911da177e4SLinus Torvalds /* send a neighbour solicitation for our addr */ 26921da177e4SLinus Torvalds memset(&unspec, 0, sizeof(unspec)); 26931da177e4SLinus Torvalds addrconf_addr_solict_mult(&ifp->addr, &mcaddr); 26941da177e4SLinus Torvalds ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec); 26951da177e4SLinus Torvalds out: 26961da177e4SLinus Torvalds in6_ifa_put(ifp); 26971da177e4SLinus Torvalds } 26981da177e4SLinus Torvalds 26991da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp) 27001da177e4SLinus Torvalds { 27011da177e4SLinus Torvalds struct net_device * dev = ifp->idev->dev; 27021da177e4SLinus Torvalds 27031da177e4SLinus Torvalds /* 27041da177e4SLinus Torvalds * Configure the address for reception. Now it is valid. 27051da177e4SLinus Torvalds */ 27061da177e4SLinus Torvalds 27071da177e4SLinus Torvalds ipv6_ifa_notify(RTM_NEWADDR, ifp); 27081da177e4SLinus Torvalds 27091da177e4SLinus Torvalds /* If added prefix is link local and forwarding is off, 27101da177e4SLinus Torvalds start sending router solicitations. 27111da177e4SLinus Torvalds */ 27121da177e4SLinus Torvalds 27131da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding == 0 && 27141da177e4SLinus Torvalds ifp->idev->cnf.rtr_solicits > 0 && 27151da177e4SLinus Torvalds (dev->flags&IFF_LOOPBACK) == 0 && 27161da177e4SLinus Torvalds (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) { 27171da177e4SLinus Torvalds struct in6_addr all_routers; 27181da177e4SLinus Torvalds 27191da177e4SLinus Torvalds ipv6_addr_all_routers(&all_routers); 27201da177e4SLinus Torvalds 27211da177e4SLinus Torvalds /* 27221da177e4SLinus Torvalds * If a host as already performed a random delay 27231da177e4SLinus Torvalds * [...] as part of DAD [...] there is no need 27241da177e4SLinus Torvalds * to delay again before sending the first RS 27251da177e4SLinus Torvalds */ 27261da177e4SLinus Torvalds ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers); 27271da177e4SLinus Torvalds 27281da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 27291da177e4SLinus Torvalds ifp->probes = 1; 27301da177e4SLinus Torvalds ifp->idev->if_flags |= IF_RS_SENT; 27311da177e4SLinus Torvalds addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval); 27321da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 27331da177e4SLinus Torvalds } 27341da177e4SLinus Torvalds } 27351da177e4SLinus Torvalds 2736c5e33bddSYOSHIFUJI Hideaki static void addrconf_dad_run(struct inet6_dev *idev) { 2737c5e33bddSYOSHIFUJI Hideaki struct inet6_ifaddr *ifp; 2738c5e33bddSYOSHIFUJI Hideaki 2739c5e33bddSYOSHIFUJI Hideaki read_lock_bh(&idev->lock); 2740c5e33bddSYOSHIFUJI Hideaki for (ifp = idev->addr_list; ifp; ifp = ifp->if_next) { 2741c5e33bddSYOSHIFUJI Hideaki spin_lock_bh(&ifp->lock); 2742c5e33bddSYOSHIFUJI Hideaki if (!(ifp->flags & IFA_F_TENTATIVE)) { 2743c5e33bddSYOSHIFUJI Hideaki spin_unlock_bh(&ifp->lock); 2744c5e33bddSYOSHIFUJI Hideaki continue; 2745c5e33bddSYOSHIFUJI Hideaki } 2746c5e33bddSYOSHIFUJI Hideaki spin_unlock_bh(&ifp->lock); 2747c5e33bddSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 2748c5e33bddSYOSHIFUJI Hideaki } 2749c5e33bddSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 2750c5e33bddSYOSHIFUJI Hideaki } 2751c5e33bddSYOSHIFUJI Hideaki 27521da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 27531da177e4SLinus Torvalds struct if6_iter_state { 27543c40090aSDaniel Lezcano struct seq_net_private p; 27551da177e4SLinus Torvalds int bucket; 27561da177e4SLinus Torvalds }; 27571da177e4SLinus Torvalds 27581da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_first(struct seq_file *seq) 27591da177e4SLinus Torvalds { 27601da177e4SLinus Torvalds struct inet6_ifaddr *ifa = NULL; 27611da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 27623c40090aSDaniel Lezcano struct net *net = state->p.net; 27631da177e4SLinus Torvalds 27641da177e4SLinus Torvalds for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) { 27651da177e4SLinus Torvalds ifa = inet6_addr_lst[state->bucket]; 27663c40090aSDaniel Lezcano 27673c40090aSDaniel Lezcano while (ifa && ifa->idev->dev->nd_net != net) 27683c40090aSDaniel Lezcano ifa = ifa->lst_next; 27691da177e4SLinus Torvalds if (ifa) 27701da177e4SLinus Torvalds break; 27711da177e4SLinus Torvalds } 27721da177e4SLinus Torvalds return ifa; 27731da177e4SLinus Torvalds } 27741da177e4SLinus Torvalds 27751da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa) 27761da177e4SLinus Torvalds { 27771da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 27783c40090aSDaniel Lezcano struct net *net = state->p.net; 27791da177e4SLinus Torvalds 27801da177e4SLinus Torvalds ifa = ifa->lst_next; 27811da177e4SLinus Torvalds try_again: 27823c40090aSDaniel Lezcano if (ifa) { 27833c40090aSDaniel Lezcano if (ifa->idev->dev->nd_net != net) { 27843c40090aSDaniel Lezcano ifa = ifa->lst_next; 27853c40090aSDaniel Lezcano goto try_again; 27863c40090aSDaniel Lezcano } 27873c40090aSDaniel Lezcano } 27883c40090aSDaniel Lezcano 27891da177e4SLinus Torvalds if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) { 27901da177e4SLinus Torvalds ifa = inet6_addr_lst[state->bucket]; 27911da177e4SLinus Torvalds goto try_again; 27921da177e4SLinus Torvalds } 27933c40090aSDaniel Lezcano 27941da177e4SLinus Torvalds return ifa; 27951da177e4SLinus Torvalds } 27961da177e4SLinus Torvalds 27971da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos) 27981da177e4SLinus Torvalds { 27991da177e4SLinus Torvalds struct inet6_ifaddr *ifa = if6_get_first(seq); 28001da177e4SLinus Torvalds 28011da177e4SLinus Torvalds if (ifa) 28021da177e4SLinus Torvalds while(pos && (ifa = if6_get_next(seq, ifa)) != NULL) 28031da177e4SLinus Torvalds --pos; 28041da177e4SLinus Torvalds return pos ? NULL : ifa; 28051da177e4SLinus Torvalds } 28061da177e4SLinus Torvalds 28071da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos) 2808d20b3109SStephen Hemminger __acquires(addrconf_hash_lock) 28091da177e4SLinus Torvalds { 28101da177e4SLinus Torvalds read_lock_bh(&addrconf_hash_lock); 28111da177e4SLinus Torvalds return if6_get_idx(seq, *pos); 28121da177e4SLinus Torvalds } 28131da177e4SLinus Torvalds 28141da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos) 28151da177e4SLinus Torvalds { 28161da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 28171da177e4SLinus Torvalds 28181da177e4SLinus Torvalds ifa = if6_get_next(seq, v); 28191da177e4SLinus Torvalds ++*pos; 28201da177e4SLinus Torvalds return ifa; 28211da177e4SLinus Torvalds } 28221da177e4SLinus Torvalds 28231da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v) 2824d20b3109SStephen Hemminger __releases(addrconf_hash_lock) 28251da177e4SLinus Torvalds { 28261da177e4SLinus Torvalds read_unlock_bh(&addrconf_hash_lock); 28271da177e4SLinus Torvalds } 28281da177e4SLinus Torvalds 28291da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v) 28301da177e4SLinus Torvalds { 28311da177e4SLinus Torvalds struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v; 28321da177e4SLinus Torvalds seq_printf(seq, 28339343e79aSYOSHIFUJI Hideaki NIP6_SEQFMT " %02x %02x %02x %02x %8s\n", 28341da177e4SLinus Torvalds NIP6(ifp->addr), 28351da177e4SLinus Torvalds ifp->idev->dev->ifindex, 28361da177e4SLinus Torvalds ifp->prefix_len, 28371da177e4SLinus Torvalds ifp->scope, 28381da177e4SLinus Torvalds ifp->flags, 28391da177e4SLinus Torvalds ifp->idev->dev->name); 28401da177e4SLinus Torvalds return 0; 28411da177e4SLinus Torvalds } 28421da177e4SLinus Torvalds 284356b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = { 28441da177e4SLinus Torvalds .start = if6_seq_start, 28451da177e4SLinus Torvalds .next = if6_seq_next, 28461da177e4SLinus Torvalds .show = if6_seq_show, 28471da177e4SLinus Torvalds .stop = if6_seq_stop, 28481da177e4SLinus Torvalds }; 28491da177e4SLinus Torvalds 28501da177e4SLinus Torvalds static int if6_seq_open(struct inode *inode, struct file *file) 28511da177e4SLinus Torvalds { 28523c40090aSDaniel Lezcano return seq_open_net(inode, file, &if6_seq_ops, 2853cf7732e4SPavel Emelyanov sizeof(struct if6_iter_state)); 28541da177e4SLinus Torvalds } 28551da177e4SLinus Torvalds 28569a32144eSArjan van de Ven static const struct file_operations if6_fops = { 28571da177e4SLinus Torvalds .owner = THIS_MODULE, 28581da177e4SLinus Torvalds .open = if6_seq_open, 28591da177e4SLinus Torvalds .read = seq_read, 28601da177e4SLinus Torvalds .llseek = seq_lseek, 28613c40090aSDaniel Lezcano .release = seq_release_net, 28621da177e4SLinus Torvalds }; 28631da177e4SLinus Torvalds 28643c40090aSDaniel Lezcano static int if6_proc_net_init(struct net *net) 28651da177e4SLinus Torvalds { 28663c40090aSDaniel Lezcano if (!proc_net_fops_create(net, "if_inet6", S_IRUGO, &if6_fops)) 28671da177e4SLinus Torvalds return -ENOMEM; 28681da177e4SLinus Torvalds return 0; 28691da177e4SLinus Torvalds } 28701da177e4SLinus Torvalds 28713c40090aSDaniel Lezcano static void if6_proc_net_exit(struct net *net) 28723c40090aSDaniel Lezcano { 28733c40090aSDaniel Lezcano proc_net_remove(net, "if_inet6"); 28743c40090aSDaniel Lezcano } 28753c40090aSDaniel Lezcano 28763c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = { 28773c40090aSDaniel Lezcano .init = if6_proc_net_init, 28783c40090aSDaniel Lezcano .exit = if6_proc_net_exit, 28793c40090aSDaniel Lezcano }; 28803c40090aSDaniel Lezcano 28813c40090aSDaniel Lezcano int __init if6_proc_init(void) 28823c40090aSDaniel Lezcano { 28833c40090aSDaniel Lezcano return register_pernet_subsys(&if6_proc_net_ops); 28843c40090aSDaniel Lezcano } 28853c40090aSDaniel Lezcano 28861da177e4SLinus Torvalds void if6_proc_exit(void) 28871da177e4SLinus Torvalds { 28883c40090aSDaniel Lezcano unregister_pernet_subsys(&if6_proc_net_ops); 28891da177e4SLinus Torvalds } 28901da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 28911da177e4SLinus Torvalds 289259fbb3a6SMasahide NAKAMURA #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) 28933b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */ 2894389f6612SDaniel Lezcano int ipv6_chk_home_addr(struct net *net, struct in6_addr *addr) 28953b9f9a1cSNoriaki TAKAMIYA { 28963b9f9a1cSNoriaki TAKAMIYA int ret = 0; 28973b9f9a1cSNoriaki TAKAMIYA struct inet6_ifaddr * ifp; 28983b9f9a1cSNoriaki TAKAMIYA u8 hash = ipv6_addr_hash(addr); 28993b9f9a1cSNoriaki TAKAMIYA read_lock_bh(&addrconf_hash_lock); 29003b9f9a1cSNoriaki TAKAMIYA for (ifp = inet6_addr_lst[hash]; ifp; ifp = ifp->lst_next) { 2901389f6612SDaniel Lezcano if (ifp->idev->dev->nd_net != net) 2902389f6612SDaniel Lezcano continue; 29033b9f9a1cSNoriaki TAKAMIYA if (ipv6_addr_cmp(&ifp->addr, addr) == 0 && 29043b9f9a1cSNoriaki TAKAMIYA (ifp->flags & IFA_F_HOMEADDRESS)) { 29053b9f9a1cSNoriaki TAKAMIYA ret = 1; 29063b9f9a1cSNoriaki TAKAMIYA break; 29073b9f9a1cSNoriaki TAKAMIYA } 29083b9f9a1cSNoriaki TAKAMIYA } 29093b9f9a1cSNoriaki TAKAMIYA read_unlock_bh(&addrconf_hash_lock); 29103b9f9a1cSNoriaki TAKAMIYA return ret; 29113b9f9a1cSNoriaki TAKAMIYA } 29123b9f9a1cSNoriaki TAKAMIYA #endif 29133b9f9a1cSNoriaki TAKAMIYA 29141da177e4SLinus Torvalds /* 29151da177e4SLinus Torvalds * Periodic address status verification 29161da177e4SLinus Torvalds */ 29171da177e4SLinus Torvalds 29181da177e4SLinus Torvalds static void addrconf_verify(unsigned long foo) 29191da177e4SLinus Torvalds { 29201da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 29211da177e4SLinus Torvalds unsigned long now, next; 29221da177e4SLinus Torvalds int i; 29231da177e4SLinus Torvalds 29241da177e4SLinus Torvalds spin_lock_bh(&addrconf_verify_lock); 29251da177e4SLinus Torvalds now = jiffies; 29261da177e4SLinus Torvalds next = now + ADDR_CHECK_FREQUENCY; 29271da177e4SLinus Torvalds 29281da177e4SLinus Torvalds del_timer(&addr_chk_timer); 29291da177e4SLinus Torvalds 29301da177e4SLinus Torvalds for (i=0; i < IN6_ADDR_HSIZE; i++) { 29311da177e4SLinus Torvalds 29321da177e4SLinus Torvalds restart: 29335d5780dfSYan Zheng read_lock(&addrconf_hash_lock); 29341da177e4SLinus Torvalds for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) { 29351da177e4SLinus Torvalds unsigned long age; 29361da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 29371da177e4SLinus Torvalds unsigned long regen_advance; 29381da177e4SLinus Torvalds #endif 29391da177e4SLinus Torvalds 29401da177e4SLinus Torvalds if (ifp->flags & IFA_F_PERMANENT) 29411da177e4SLinus Torvalds continue; 29421da177e4SLinus Torvalds 29431da177e4SLinus Torvalds spin_lock(&ifp->lock); 29441da177e4SLinus Torvalds age = (now - ifp->tstamp) / HZ; 29451da177e4SLinus Torvalds 29461da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 29471da177e4SLinus Torvalds regen_advance = ifp->idev->cnf.regen_max_retry * 29481da177e4SLinus Torvalds ifp->idev->cnf.dad_transmits * 29491da177e4SLinus Torvalds ifp->idev->nd_parms->retrans_time / HZ; 29501da177e4SLinus Torvalds #endif 29511da177e4SLinus Torvalds 29528f27ebb9SYOSHIFUJI Hideaki if (ifp->valid_lft != INFINITY_LIFE_TIME && 29538f27ebb9SYOSHIFUJI Hideaki age >= ifp->valid_lft) { 29541da177e4SLinus Torvalds spin_unlock(&ifp->lock); 29551da177e4SLinus Torvalds in6_ifa_hold(ifp); 29565d5780dfSYan Zheng read_unlock(&addrconf_hash_lock); 29571da177e4SLinus Torvalds ipv6_del_addr(ifp); 29581da177e4SLinus Torvalds goto restart; 29598f27ebb9SYOSHIFUJI Hideaki } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) { 29608f27ebb9SYOSHIFUJI Hideaki spin_unlock(&ifp->lock); 29618f27ebb9SYOSHIFUJI Hideaki continue; 29621da177e4SLinus Torvalds } else if (age >= ifp->prefered_lft) { 29631da177e4SLinus Torvalds /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */ 29641da177e4SLinus Torvalds int deprecate = 0; 29651da177e4SLinus Torvalds 29661da177e4SLinus Torvalds if (!(ifp->flags&IFA_F_DEPRECATED)) { 29671da177e4SLinus Torvalds deprecate = 1; 29681da177e4SLinus Torvalds ifp->flags |= IFA_F_DEPRECATED; 29691da177e4SLinus Torvalds } 29701da177e4SLinus Torvalds 29711da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)) 29721da177e4SLinus Torvalds next = ifp->tstamp + ifp->valid_lft * HZ; 29731da177e4SLinus Torvalds 29741da177e4SLinus Torvalds spin_unlock(&ifp->lock); 29751da177e4SLinus Torvalds 29761da177e4SLinus Torvalds if (deprecate) { 29771da177e4SLinus Torvalds in6_ifa_hold(ifp); 29785d5780dfSYan Zheng read_unlock(&addrconf_hash_lock); 29791da177e4SLinus Torvalds 29801da177e4SLinus Torvalds ipv6_ifa_notify(0, ifp); 29811da177e4SLinus Torvalds in6_ifa_put(ifp); 29821da177e4SLinus Torvalds goto restart; 29831da177e4SLinus Torvalds } 29841da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 29851da177e4SLinus Torvalds } else if ((ifp->flags&IFA_F_TEMPORARY) && 29861da177e4SLinus Torvalds !(ifp->flags&IFA_F_TENTATIVE)) { 29871da177e4SLinus Torvalds if (age >= ifp->prefered_lft - regen_advance) { 29881da177e4SLinus Torvalds struct inet6_ifaddr *ifpub = ifp->ifpub; 29891da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 29901da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 29911da177e4SLinus Torvalds if (!ifp->regen_count && ifpub) { 29921da177e4SLinus Torvalds ifp->regen_count++; 29931da177e4SLinus Torvalds in6_ifa_hold(ifp); 29941da177e4SLinus Torvalds in6_ifa_hold(ifpub); 29951da177e4SLinus Torvalds spin_unlock(&ifp->lock); 29965d5780dfSYan Zheng read_unlock(&addrconf_hash_lock); 2997291d809bSHiroyuki YAMAMORI spin_lock(&ifpub->lock); 2998291d809bSHiroyuki YAMAMORI ifpub->regen_count = 0; 2999291d809bSHiroyuki YAMAMORI spin_unlock(&ifpub->lock); 30001da177e4SLinus Torvalds ipv6_create_tempaddr(ifpub, ifp); 30011da177e4SLinus Torvalds in6_ifa_put(ifpub); 30021da177e4SLinus Torvalds in6_ifa_put(ifp); 30031da177e4SLinus Torvalds goto restart; 30041da177e4SLinus Torvalds } 30051da177e4SLinus Torvalds } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next)) 30061da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ; 30071da177e4SLinus Torvalds spin_unlock(&ifp->lock); 30081da177e4SLinus Torvalds #endif 30091da177e4SLinus Torvalds } else { 30101da177e4SLinus Torvalds /* ifp->prefered_lft <= ifp->valid_lft */ 30111da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 30121da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 30131da177e4SLinus Torvalds spin_unlock(&ifp->lock); 30141da177e4SLinus Torvalds } 30151da177e4SLinus Torvalds } 30165d5780dfSYan Zheng read_unlock(&addrconf_hash_lock); 30171da177e4SLinus Torvalds } 30181da177e4SLinus Torvalds 30191da177e4SLinus Torvalds addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next; 30201da177e4SLinus Torvalds add_timer(&addr_chk_timer); 30211da177e4SLinus Torvalds spin_unlock_bh(&addrconf_verify_lock); 30221da177e4SLinus Torvalds } 30231da177e4SLinus Torvalds 3024461d8837SThomas Graf static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local) 3025461d8837SThomas Graf { 3026461d8837SThomas Graf struct in6_addr *pfx = NULL; 3027461d8837SThomas Graf 3028461d8837SThomas Graf if (addr) 3029461d8837SThomas Graf pfx = nla_data(addr); 3030461d8837SThomas Graf 3031461d8837SThomas Graf if (local) { 3032461d8837SThomas Graf if (pfx && nla_memcmp(local, pfx, sizeof(*pfx))) 3033461d8837SThomas Graf pfx = NULL; 3034461d8837SThomas Graf else 3035461d8837SThomas Graf pfx = nla_data(local); 3036461d8837SThomas Graf } 3037461d8837SThomas Graf 3038461d8837SThomas Graf return pfx; 3039461d8837SThomas Graf } 3040461d8837SThomas Graf 3041ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = { 3042461d8837SThomas Graf [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) }, 3043461d8837SThomas Graf [IFA_LOCAL] = { .len = sizeof(struct in6_addr) }, 3044461d8837SThomas Graf [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) }, 3045461d8837SThomas Graf }; 3046461d8837SThomas Graf 30471da177e4SLinus Torvalds static int 30481da177e4SLinus Torvalds inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 30491da177e4SLinus Torvalds { 3050b854272bSDenis V. Lunev struct net *net = skb->sk->sk_net; 3051b933f716SThomas Graf struct ifaddrmsg *ifm; 3052b933f716SThomas Graf struct nlattr *tb[IFA_MAX+1]; 30531da177e4SLinus Torvalds struct in6_addr *pfx; 3054b933f716SThomas Graf int err; 30551da177e4SLinus Torvalds 3056b854272bSDenis V. Lunev if (net != &init_net) 3057b854272bSDenis V. Lunev return -EINVAL; 3058b854272bSDenis V. Lunev 3059b933f716SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 3060b933f716SThomas Graf if (err < 0) 3061b933f716SThomas Graf return err; 3062b933f716SThomas Graf 3063b933f716SThomas Graf ifm = nlmsg_data(nlh); 3064b933f716SThomas Graf pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]); 30651da177e4SLinus Torvalds if (pfx == NULL) 30661da177e4SLinus Torvalds return -EINVAL; 30671da177e4SLinus Torvalds 30681da177e4SLinus Torvalds return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen); 30691da177e4SLinus Torvalds } 30701da177e4SLinus Torvalds 307155ebaef1SNoriaki TAKAMIYA static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags, 307255ebaef1SNoriaki TAKAMIYA u32 prefered_lft, u32 valid_lft) 3073081bba5bSNoriaki TAKAMIYA { 307446d48046SYOSHIFUJI Hideaki u32 flags = RTF_EXPIRES; 307546d48046SYOSHIFUJI Hideaki 3076081bba5bSNoriaki TAKAMIYA if (!valid_lft || (prefered_lft > valid_lft)) 3077081bba5bSNoriaki TAKAMIYA return -EINVAL; 3078081bba5bSNoriaki TAKAMIYA 307946d48046SYOSHIFUJI Hideaki if (valid_lft == INFINITY_LIFE_TIME) { 308055ebaef1SNoriaki TAKAMIYA ifa_flags |= IFA_F_PERMANENT; 308146d48046SYOSHIFUJI Hideaki flags = 0; 308246d48046SYOSHIFUJI Hideaki } else if (valid_lft >= 0x7FFFFFFF/HZ) 3083081bba5bSNoriaki TAKAMIYA valid_lft = 0x7FFFFFFF/HZ; 3084081bba5bSNoriaki TAKAMIYA 3085081bba5bSNoriaki TAKAMIYA if (prefered_lft == 0) 308655ebaef1SNoriaki TAKAMIYA ifa_flags |= IFA_F_DEPRECATED; 3087081bba5bSNoriaki TAKAMIYA else if ((prefered_lft >= 0x7FFFFFFF/HZ) && 3088081bba5bSNoriaki TAKAMIYA (prefered_lft != INFINITY_LIFE_TIME)) 3089081bba5bSNoriaki TAKAMIYA prefered_lft = 0x7FFFFFFF/HZ; 3090081bba5bSNoriaki TAKAMIYA 3091081bba5bSNoriaki TAKAMIYA spin_lock_bh(&ifp->lock); 30923b9f9a1cSNoriaki TAKAMIYA ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags; 3093081bba5bSNoriaki TAKAMIYA ifp->tstamp = jiffies; 3094081bba5bSNoriaki TAKAMIYA ifp->valid_lft = valid_lft; 3095081bba5bSNoriaki TAKAMIYA ifp->prefered_lft = prefered_lft; 3096081bba5bSNoriaki TAKAMIYA 3097081bba5bSNoriaki TAKAMIYA spin_unlock_bh(&ifp->lock); 3098081bba5bSNoriaki TAKAMIYA if (!(ifp->flags&IFA_F_TENTATIVE)) 3099081bba5bSNoriaki TAKAMIYA ipv6_ifa_notify(0, ifp); 3100081bba5bSNoriaki TAKAMIYA 310146d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev, 310246d48046SYOSHIFUJI Hideaki jiffies_to_clock_t(valid_lft * HZ), flags); 3103081bba5bSNoriaki TAKAMIYA addrconf_verify(0); 3104081bba5bSNoriaki TAKAMIYA 3105081bba5bSNoriaki TAKAMIYA return 0; 3106081bba5bSNoriaki TAKAMIYA } 3107081bba5bSNoriaki TAKAMIYA 3108081bba5bSNoriaki TAKAMIYA static int 31091da177e4SLinus Torvalds inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 31101da177e4SLinus Torvalds { 3111b854272bSDenis V. Lunev struct net *net = skb->sk->sk_net; 3112461d8837SThomas Graf struct ifaddrmsg *ifm; 3113461d8837SThomas Graf struct nlattr *tb[IFA_MAX+1]; 31141da177e4SLinus Torvalds struct in6_addr *pfx; 31157198f8ceSThomas Graf struct inet6_ifaddr *ifa; 31167198f8ceSThomas Graf struct net_device *dev; 311755ebaef1SNoriaki TAKAMIYA u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME; 311855ebaef1SNoriaki TAKAMIYA u8 ifa_flags; 3119461d8837SThomas Graf int err; 31201da177e4SLinus Torvalds 3121b854272bSDenis V. Lunev if (net != &init_net) 3122b854272bSDenis V. Lunev return -EINVAL; 3123b854272bSDenis V. Lunev 3124461d8837SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 3125461d8837SThomas Graf if (err < 0) 3126461d8837SThomas Graf return err; 3127461d8837SThomas Graf 3128461d8837SThomas Graf ifm = nlmsg_data(nlh); 3129461d8837SThomas Graf pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]); 31301da177e4SLinus Torvalds if (pfx == NULL) 31311da177e4SLinus Torvalds return -EINVAL; 31321da177e4SLinus Torvalds 3133461d8837SThomas Graf if (tb[IFA_CACHEINFO]) { 31340778769dSNoriaki TAKAMIYA struct ifa_cacheinfo *ci; 3135461d8837SThomas Graf 3136461d8837SThomas Graf ci = nla_data(tb[IFA_CACHEINFO]); 31370778769dSNoriaki TAKAMIYA valid_lft = ci->ifa_valid; 3138461d8837SThomas Graf preferred_lft = ci->ifa_prefered; 3139461d8837SThomas Graf } else { 3140461d8837SThomas Graf preferred_lft = INFINITY_LIFE_TIME; 3141461d8837SThomas Graf valid_lft = INFINITY_LIFE_TIME; 31420778769dSNoriaki TAKAMIYA } 31430778769dSNoriaki TAKAMIYA 3144881d966bSEric W. Biederman dev = __dev_get_by_index(&init_net, ifm->ifa_index); 31457198f8ceSThomas Graf if (dev == NULL) 31467198f8ceSThomas Graf return -ENODEV; 3147081bba5bSNoriaki TAKAMIYA 314855ebaef1SNoriaki TAKAMIYA /* We ignore other flags so far. */ 31493b9f9a1cSNoriaki TAKAMIYA ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS); 315055ebaef1SNoriaki TAKAMIYA 31511cab3da6SDaniel Lezcano ifa = ipv6_get_ifaddr(net, pfx, dev, 1); 31527198f8ceSThomas Graf if (ifa == NULL) { 31537198f8ceSThomas Graf /* 31547198f8ceSThomas Graf * It would be best to check for !NLM_F_CREATE here but 31557198f8ceSThomas Graf * userspace alreay relies on not having to provide this. 31567198f8ceSThomas Graf */ 31570778769dSNoriaki TAKAMIYA return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen, 315855ebaef1SNoriaki TAKAMIYA ifa_flags, preferred_lft, valid_lft); 31591da177e4SLinus Torvalds } 31601da177e4SLinus Torvalds 31617198f8ceSThomas Graf if (nlh->nlmsg_flags & NLM_F_EXCL || 31627198f8ceSThomas Graf !(nlh->nlmsg_flags & NLM_F_REPLACE)) 31637198f8ceSThomas Graf err = -EEXIST; 31647198f8ceSThomas Graf else 316555ebaef1SNoriaki TAKAMIYA err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft); 31667198f8ceSThomas Graf 31677198f8ceSThomas Graf in6_ifa_put(ifa); 31687198f8ceSThomas Graf 31697198f8ceSThomas Graf return err; 31707198f8ceSThomas Graf } 31717198f8ceSThomas Graf 3172101bb229SThomas Graf static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags, 3173101bb229SThomas Graf u8 scope, int ifindex) 3174101bb229SThomas Graf { 3175101bb229SThomas Graf struct ifaddrmsg *ifm; 3176101bb229SThomas Graf 3177101bb229SThomas Graf ifm = nlmsg_data(nlh); 3178101bb229SThomas Graf ifm->ifa_family = AF_INET6; 3179101bb229SThomas Graf ifm->ifa_prefixlen = prefixlen; 3180101bb229SThomas Graf ifm->ifa_flags = flags; 3181101bb229SThomas Graf ifm->ifa_scope = scope; 3182101bb229SThomas Graf ifm->ifa_index = ifindex; 3183101bb229SThomas Graf } 3184101bb229SThomas Graf 318585486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp, 318685486af0SThomas Graf unsigned long tstamp, u32 preferred, u32 valid) 318785486af0SThomas Graf { 318885486af0SThomas Graf struct ifa_cacheinfo ci; 318985486af0SThomas Graf 319085486af0SThomas Graf ci.cstamp = (u32)(TIME_DELTA(cstamp, INITIAL_JIFFIES) / HZ * 100 319185486af0SThomas Graf + TIME_DELTA(cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ); 319285486af0SThomas Graf ci.tstamp = (u32)(TIME_DELTA(tstamp, INITIAL_JIFFIES) / HZ * 100 319385486af0SThomas Graf + TIME_DELTA(tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ); 319485486af0SThomas Graf ci.ifa_prefered = preferred; 319585486af0SThomas Graf ci.ifa_valid = valid; 319685486af0SThomas Graf 319785486af0SThomas Graf return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci); 319885486af0SThomas Graf } 319985486af0SThomas Graf 3200101bb229SThomas Graf static inline int rt_scope(int ifa_scope) 3201101bb229SThomas Graf { 3202101bb229SThomas Graf if (ifa_scope & IFA_HOST) 3203101bb229SThomas Graf return RT_SCOPE_HOST; 3204101bb229SThomas Graf else if (ifa_scope & IFA_LINK) 3205101bb229SThomas Graf return RT_SCOPE_LINK; 3206101bb229SThomas Graf else if (ifa_scope & IFA_SITE) 3207101bb229SThomas Graf return RT_SCOPE_SITE; 3208101bb229SThomas Graf else 3209101bb229SThomas Graf return RT_SCOPE_UNIVERSE; 3210101bb229SThomas Graf } 3211101bb229SThomas Graf 32120ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void) 32130ab6803bSThomas Graf { 3214339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifaddrmsg)) 3215339bf98fSThomas Graf + nla_total_size(16) /* IFA_ADDRESS */ 3216339bf98fSThomas Graf + nla_total_size(sizeof(struct ifa_cacheinfo)); 32170ab6803bSThomas Graf } 3218c5396a31SYOSHIFUJI Hideaki 32191da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa, 3220b6544c0bSJamal Hadi Salim u32 pid, u32 seq, int event, unsigned int flags) 32211da177e4SLinus Torvalds { 32221da177e4SLinus Torvalds struct nlmsghdr *nlh; 322385486af0SThomas Graf u32 preferred, valid; 32241da177e4SLinus Torvalds 32250ab6803bSThomas Graf nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags); 32260ab6803bSThomas Graf if (nlh == NULL) 322726932566SPatrick McHardy return -EMSGSIZE; 32280ab6803bSThomas Graf 3229101bb229SThomas Graf put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope), 3230101bb229SThomas Graf ifa->idev->dev->ifindex); 3231101bb229SThomas Graf 32321da177e4SLinus Torvalds if (!(ifa->flags&IFA_F_PERMANENT)) { 323385486af0SThomas Graf preferred = ifa->prefered_lft; 323485486af0SThomas Graf valid = ifa->valid_lft; 323585486af0SThomas Graf if (preferred != INFINITY_LIFE_TIME) { 32361da177e4SLinus Torvalds long tval = (jiffies - ifa->tstamp)/HZ; 323785486af0SThomas Graf preferred -= tval; 323885486af0SThomas Graf if (valid != INFINITY_LIFE_TIME) 323985486af0SThomas Graf valid -= tval; 32401da177e4SLinus Torvalds } 32411da177e4SLinus Torvalds } else { 324285486af0SThomas Graf preferred = INFINITY_LIFE_TIME; 324385486af0SThomas Graf valid = INFINITY_LIFE_TIME; 32441da177e4SLinus Torvalds } 324585486af0SThomas Graf 32460ab6803bSThomas Graf if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 || 324726932566SPatrick McHardy put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) { 324826932566SPatrick McHardy nlmsg_cancel(skb, nlh); 324926932566SPatrick McHardy return -EMSGSIZE; 325026932566SPatrick McHardy } 325185486af0SThomas Graf 32520ab6803bSThomas Graf return nlmsg_end(skb, nlh); 32531da177e4SLinus Torvalds } 32541da177e4SLinus Torvalds 32551da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca, 3256e431b8c0SJamal Hadi Salim u32 pid, u32 seq, int event, u16 flags) 32571da177e4SLinus Torvalds { 32581da177e4SLinus Torvalds struct nlmsghdr *nlh; 3259101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 3260101bb229SThomas Graf int ifindex = ifmca->idev->dev->ifindex; 32611da177e4SLinus Torvalds 32621da177e4SLinus Torvalds if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE) 3263101bb229SThomas Graf scope = RT_SCOPE_SITE; 3264101bb229SThomas Graf 32650ab6803bSThomas Graf nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags); 32660ab6803bSThomas Graf if (nlh == NULL) 326726932566SPatrick McHardy return -EMSGSIZE; 32680ab6803bSThomas Graf 3269101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 32700ab6803bSThomas Graf if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 || 32710ab6803bSThomas Graf put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp, 327226932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 327326932566SPatrick McHardy nlmsg_cancel(skb, nlh); 327426932566SPatrick McHardy return -EMSGSIZE; 327526932566SPatrick McHardy } 327685486af0SThomas Graf 32770ab6803bSThomas Graf return nlmsg_end(skb, nlh); 32781da177e4SLinus Torvalds } 32791da177e4SLinus Torvalds 32801da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca, 3281b6544c0bSJamal Hadi Salim u32 pid, u32 seq, int event, unsigned int flags) 32821da177e4SLinus Torvalds { 32831da177e4SLinus Torvalds struct nlmsghdr *nlh; 3284101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 3285101bb229SThomas Graf int ifindex = ifaca->aca_idev->dev->ifindex; 32861da177e4SLinus Torvalds 32871da177e4SLinus Torvalds if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE) 3288101bb229SThomas Graf scope = RT_SCOPE_SITE; 3289101bb229SThomas Graf 32900ab6803bSThomas Graf nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags); 32910ab6803bSThomas Graf if (nlh == NULL) 329226932566SPatrick McHardy return -EMSGSIZE; 32930ab6803bSThomas Graf 3294101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 32950ab6803bSThomas Graf if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 || 32960ab6803bSThomas Graf put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp, 329726932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 329826932566SPatrick McHardy nlmsg_cancel(skb, nlh); 329926932566SPatrick McHardy return -EMSGSIZE; 330026932566SPatrick McHardy } 330185486af0SThomas Graf 33020ab6803bSThomas Graf return nlmsg_end(skb, nlh); 33031da177e4SLinus Torvalds } 33041da177e4SLinus Torvalds 33051da177e4SLinus Torvalds enum addr_type_t 33061da177e4SLinus Torvalds { 33071da177e4SLinus Torvalds UNICAST_ADDR, 33081da177e4SLinus Torvalds MULTICAST_ADDR, 33091da177e4SLinus Torvalds ANYCAST_ADDR, 33101da177e4SLinus Torvalds }; 33111da177e4SLinus Torvalds 33121da177e4SLinus Torvalds static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb, 33131da177e4SLinus Torvalds enum addr_type_t type) 33141da177e4SLinus Torvalds { 33151da177e4SLinus Torvalds int idx, ip_idx; 33161da177e4SLinus Torvalds int s_idx, s_ip_idx; 33171da177e4SLinus Torvalds int err = 1; 33181da177e4SLinus Torvalds struct net_device *dev; 33191da177e4SLinus Torvalds struct inet6_dev *idev = NULL; 33201da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 33211da177e4SLinus Torvalds struct ifmcaddr6 *ifmca; 33221da177e4SLinus Torvalds struct ifacaddr6 *ifaca; 33231da177e4SLinus Torvalds 33241da177e4SLinus Torvalds s_idx = cb->args[0]; 33251da177e4SLinus Torvalds s_ip_idx = ip_idx = cb->args[1]; 33261da177e4SLinus Torvalds 33277562f876SPavel Emelianov idx = 0; 3328881d966bSEric W. Biederman for_each_netdev(&init_net, dev) { 33291da177e4SLinus Torvalds if (idx < s_idx) 33307562f876SPavel Emelianov goto cont; 33311da177e4SLinus Torvalds if (idx > s_idx) 33321da177e4SLinus Torvalds s_ip_idx = 0; 33331da177e4SLinus Torvalds ip_idx = 0; 33341da177e4SLinus Torvalds if ((idev = in6_dev_get(dev)) == NULL) 33357562f876SPavel Emelianov goto cont; 33361da177e4SLinus Torvalds read_lock_bh(&idev->lock); 33371da177e4SLinus Torvalds switch (type) { 33381da177e4SLinus Torvalds case UNICAST_ADDR: 3339ae9cda5dSYOSHIFUJI Hideaki /* unicast address incl. temp addr */ 33401da177e4SLinus Torvalds for (ifa = idev->addr_list; ifa; 33411da177e4SLinus Torvalds ifa = ifa->if_next, ip_idx++) { 33421da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 33431da177e4SLinus Torvalds continue; 33445d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifaddr(skb, ifa, 33451da177e4SLinus Torvalds NETLINK_CB(cb->skb).pid, 33465d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 33475d5619b4SYOSHIFUJI Hideaki RTM_NEWADDR, 33485d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 33491da177e4SLinus Torvalds } 33501da177e4SLinus Torvalds break; 33511da177e4SLinus Torvalds case MULTICAST_ADDR: 33521da177e4SLinus Torvalds /* multicast address */ 33531da177e4SLinus Torvalds for (ifmca = idev->mc_list; ifmca; 33541da177e4SLinus Torvalds ifmca = ifmca->next, ip_idx++) { 33551da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 33561da177e4SLinus Torvalds continue; 33575d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifmcaddr(skb, ifmca, 33581da177e4SLinus Torvalds NETLINK_CB(cb->skb).pid, 33595d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 33605d5619b4SYOSHIFUJI Hideaki RTM_GETMULTICAST, 33615d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 33621da177e4SLinus Torvalds } 33631da177e4SLinus Torvalds break; 33641da177e4SLinus Torvalds case ANYCAST_ADDR: 33651da177e4SLinus Torvalds /* anycast address */ 33661da177e4SLinus Torvalds for (ifaca = idev->ac_list; ifaca; 33671da177e4SLinus Torvalds ifaca = ifaca->aca_next, ip_idx++) { 33681da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 33691da177e4SLinus Torvalds continue; 33705d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifacaddr(skb, ifaca, 33711da177e4SLinus Torvalds NETLINK_CB(cb->skb).pid, 33725d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 33735d5619b4SYOSHIFUJI Hideaki RTM_GETANYCAST, 33745d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 33751da177e4SLinus Torvalds } 33761da177e4SLinus Torvalds break; 33771da177e4SLinus Torvalds default: 33781da177e4SLinus Torvalds break; 33791da177e4SLinus Torvalds } 33801da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 33811da177e4SLinus Torvalds in6_dev_put(idev); 33825d5619b4SYOSHIFUJI Hideaki 33835d5619b4SYOSHIFUJI Hideaki if (err <= 0) 33845d5619b4SYOSHIFUJI Hideaki break; 33857562f876SPavel Emelianov cont: 33867562f876SPavel Emelianov idx++; 33871da177e4SLinus Torvalds } 33881da177e4SLinus Torvalds cb->args[0] = idx; 33891da177e4SLinus Torvalds cb->args[1] = ip_idx; 33901da177e4SLinus Torvalds return skb->len; 33911da177e4SLinus Torvalds } 33921da177e4SLinus Torvalds 33931da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) 33941da177e4SLinus Torvalds { 3395b854272bSDenis V. Lunev struct net *net = skb->sk->sk_net; 33961da177e4SLinus Torvalds enum addr_type_t type = UNICAST_ADDR; 3397b854272bSDenis V. Lunev 3398b854272bSDenis V. Lunev if (net != &init_net) 3399b854272bSDenis V. Lunev return 0; 3400b854272bSDenis V. Lunev 34011da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 34021da177e4SLinus Torvalds } 34031da177e4SLinus Torvalds 34041da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb) 34051da177e4SLinus Torvalds { 3406b854272bSDenis V. Lunev struct net *net = skb->sk->sk_net; 34071da177e4SLinus Torvalds enum addr_type_t type = MULTICAST_ADDR; 3408b854272bSDenis V. Lunev 3409b854272bSDenis V. Lunev if (net != &init_net) 3410b854272bSDenis V. Lunev return 0; 3411b854272bSDenis V. Lunev 34121da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 34131da177e4SLinus Torvalds } 34141da177e4SLinus Torvalds 34151da177e4SLinus Torvalds 34161da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb) 34171da177e4SLinus Torvalds { 3418b854272bSDenis V. Lunev struct net *net = skb->sk->sk_net; 34191da177e4SLinus Torvalds enum addr_type_t type = ANYCAST_ADDR; 3420b854272bSDenis V. Lunev 3421b854272bSDenis V. Lunev if (net != &init_net) 3422b854272bSDenis V. Lunev return 0; 3423b854272bSDenis V. Lunev 34241da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 34251da177e4SLinus Torvalds } 34261da177e4SLinus Torvalds 34271b29fc2cSThomas Graf static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh, 34281b29fc2cSThomas Graf void *arg) 34296c223828SNoriaki TAKAMIYA { 3430b854272bSDenis V. Lunev struct net *net = in_skb->sk->sk_net; 34311b29fc2cSThomas Graf struct ifaddrmsg *ifm; 34321b29fc2cSThomas Graf struct nlattr *tb[IFA_MAX+1]; 34336c223828SNoriaki TAKAMIYA struct in6_addr *addr = NULL; 34346c223828SNoriaki TAKAMIYA struct net_device *dev = NULL; 34356c223828SNoriaki TAKAMIYA struct inet6_ifaddr *ifa; 34366c223828SNoriaki TAKAMIYA struct sk_buff *skb; 34376c223828SNoriaki TAKAMIYA int err; 34386c223828SNoriaki TAKAMIYA 3439b854272bSDenis V. Lunev if (net != &init_net) 3440b854272bSDenis V. Lunev return -EINVAL; 3441b854272bSDenis V. Lunev 34421b29fc2cSThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 34431b29fc2cSThomas Graf if (err < 0) 34441b29fc2cSThomas Graf goto errout; 34456c223828SNoriaki TAKAMIYA 34461b29fc2cSThomas Graf addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]); 34471b29fc2cSThomas Graf if (addr == NULL) { 34481b29fc2cSThomas Graf err = -EINVAL; 34491b29fc2cSThomas Graf goto errout; 34501b29fc2cSThomas Graf } 34511b29fc2cSThomas Graf 34521b29fc2cSThomas Graf ifm = nlmsg_data(nlh); 34536c223828SNoriaki TAKAMIYA if (ifm->ifa_index) 3454881d966bSEric W. Biederman dev = __dev_get_by_index(&init_net, ifm->ifa_index); 34556c223828SNoriaki TAKAMIYA 34561cab3da6SDaniel Lezcano if ((ifa = ipv6_get_ifaddr(net, addr, dev, 1)) == NULL) { 34571b29fc2cSThomas Graf err = -EADDRNOTAVAIL; 34581b29fc2cSThomas Graf goto errout; 34596c223828SNoriaki TAKAMIYA } 34606c223828SNoriaki TAKAMIYA 34610ab6803bSThomas Graf if ((skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL)) == NULL) { 34621b29fc2cSThomas Graf err = -ENOBUFS; 34631b29fc2cSThomas Graf goto errout_ifa; 34641b29fc2cSThomas Graf } 34651b29fc2cSThomas Graf 34666c223828SNoriaki TAKAMIYA err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).pid, 34676c223828SNoriaki TAKAMIYA nlh->nlmsg_seq, RTM_NEWADDR, 0); 346826932566SPatrick McHardy if (err < 0) { 346926932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 347026932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 347126932566SPatrick McHardy kfree_skb(skb); 347226932566SPatrick McHardy goto errout_ifa; 347326932566SPatrick McHardy } 347497c53cacSDenis V. Lunev err = rtnl_unicast(skb, &init_net, NETLINK_CB(in_skb).pid); 34751b29fc2cSThomas Graf errout_ifa: 34766c223828SNoriaki TAKAMIYA in6_ifa_put(ifa); 34771b29fc2cSThomas Graf errout: 34786c223828SNoriaki TAKAMIYA return err; 34796c223828SNoriaki TAKAMIYA } 34806c223828SNoriaki TAKAMIYA 34811da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa) 34821da177e4SLinus Torvalds { 34831da177e4SLinus Torvalds struct sk_buff *skb; 34845d620266SThomas Graf int err = -ENOBUFS; 34851da177e4SLinus Torvalds 34860ab6803bSThomas Graf skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC); 34875d620266SThomas Graf if (skb == NULL) 34885d620266SThomas Graf goto errout; 34895d620266SThomas Graf 34905d620266SThomas Graf err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0); 349126932566SPatrick McHardy if (err < 0) { 349226932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 349326932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 349426932566SPatrick McHardy kfree_skb(skb); 349526932566SPatrick McHardy goto errout; 349626932566SPatrick McHardy } 349797c53cacSDenis V. Lunev err = rtnl_notify(skb, &init_net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC); 34985d620266SThomas Graf errout: 34995d620266SThomas Graf if (err < 0) 350097c53cacSDenis V. Lunev rtnl_set_sk_err(&init_net, RTNLGRP_IPV6_IFADDR, err); 35011da177e4SLinus Torvalds } 35021da177e4SLinus Torvalds 3503b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf, 35041da177e4SLinus Torvalds __s32 *array, int bytes) 35051da177e4SLinus Torvalds { 350604561c1fSThomas Graf BUG_ON(bytes < (DEVCONF_MAX * 4)); 350704561c1fSThomas Graf 35081da177e4SLinus Torvalds memset(array, 0, bytes); 35091da177e4SLinus Torvalds array[DEVCONF_FORWARDING] = cnf->forwarding; 35101da177e4SLinus Torvalds array[DEVCONF_HOPLIMIT] = cnf->hop_limit; 35111da177e4SLinus Torvalds array[DEVCONF_MTU6] = cnf->mtu6; 35121da177e4SLinus Torvalds array[DEVCONF_ACCEPT_RA] = cnf->accept_ra; 35131da177e4SLinus Torvalds array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects; 35141da177e4SLinus Torvalds array[DEVCONF_AUTOCONF] = cnf->autoconf; 35151da177e4SLinus Torvalds array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits; 35161da177e4SLinus Torvalds array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits; 35171da177e4SLinus Torvalds array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval; 35181da177e4SLinus Torvalds array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay; 35191da177e4SLinus Torvalds array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version; 35201da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 35211da177e4SLinus Torvalds array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr; 35221da177e4SLinus Torvalds array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft; 35231da177e4SLinus Torvalds array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft; 35241da177e4SLinus Torvalds array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry; 35251da177e4SLinus Torvalds array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor; 35261da177e4SLinus Torvalds #endif 35271da177e4SLinus Torvalds array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses; 352865f5c7c1SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr; 3529c4fd30ebSYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo; 3530930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 3531930d6ff2SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref; 353252e16356SYOSHIFUJI Hideaki array[DEVCONF_RTR_PROBE_INTERVAL] = cnf->rtr_probe_interval; 3533fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 353409c884d4SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen; 353509c884d4SYOSHIFUJI Hideaki #endif 3536930d6ff2SYOSHIFUJI Hideaki #endif 3537fbea49e1SYOSHIFUJI Hideaki array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp; 35380bcbc926SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route; 353995c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 354095c385b4SNeil Horman array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad; 354195c385b4SNeil Horman #endif 35421da177e4SLinus Torvalds } 35431da177e4SLinus Torvalds 3544339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void) 3545339bf98fSThomas Graf { 3546339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 3547339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 3548339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 3549339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 3550339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 3551339bf98fSThomas Graf + nla_total_size( /* IFLA_PROTINFO */ 3552339bf98fSThomas Graf nla_total_size(4) /* IFLA_INET6_FLAGS */ 3553339bf98fSThomas Graf + nla_total_size(sizeof(struct ifla_cacheinfo)) 3554339bf98fSThomas Graf + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */ 3555bf99f1bdSYOSHIFUJI Hideaki + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */ 3556bf99f1bdSYOSHIFUJI Hideaki + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */ 3557339bf98fSThomas Graf ); 3558339bf98fSThomas Graf } 3559c5396a31SYOSHIFUJI Hideaki 35607f7d9a6bSHerbert Xu static inline void __snmp6_fill_stats(u64 *stats, void **mib, int items, 35617f7d9a6bSHerbert Xu int bytes) 35627f7d9a6bSHerbert Xu { 35637f7d9a6bSHerbert Xu int i; 35647f7d9a6bSHerbert Xu int pad = bytes - sizeof(u64) * items; 35657f7d9a6bSHerbert Xu BUG_ON(pad < 0); 35667f7d9a6bSHerbert Xu 35677f7d9a6bSHerbert Xu /* Use put_unaligned() because stats may not be aligned for u64. */ 35687f7d9a6bSHerbert Xu put_unaligned(items, &stats[0]); 35697f7d9a6bSHerbert Xu for (i = 1; i < items; i++) 35707f7d9a6bSHerbert Xu put_unaligned(snmp_fold_field(mib, i), &stats[i]); 35717f7d9a6bSHerbert Xu 35727f7d9a6bSHerbert Xu memset(&stats[items], 0, pad); 35737f7d9a6bSHerbert Xu } 35747f7d9a6bSHerbert Xu 35757f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype, 35767f7d9a6bSHerbert Xu int bytes) 35777f7d9a6bSHerbert Xu { 35787f7d9a6bSHerbert Xu switch(attrtype) { 35797f7d9a6bSHerbert Xu case IFLA_INET6_STATS: 35807f7d9a6bSHerbert Xu __snmp6_fill_stats(stats, (void **)idev->stats.ipv6, IPSTATS_MIB_MAX, bytes); 35817f7d9a6bSHerbert Xu break; 35827f7d9a6bSHerbert Xu case IFLA_INET6_ICMP6STATS: 35837f7d9a6bSHerbert Xu __snmp6_fill_stats(stats, (void **)idev->stats.icmpv6, ICMP6_MIB_MAX, bytes); 35847f7d9a6bSHerbert Xu break; 35857f7d9a6bSHerbert Xu } 35867f7d9a6bSHerbert Xu } 35877f7d9a6bSHerbert Xu 35881da177e4SLinus Torvalds static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev, 3589b6544c0bSJamal Hadi Salim u32 pid, u32 seq, int event, unsigned int flags) 35901da177e4SLinus Torvalds { 35911da177e4SLinus Torvalds struct net_device *dev = idev->dev; 3592bf99f1bdSYOSHIFUJI Hideaki struct nlattr *nla; 359304561c1fSThomas Graf struct ifinfomsg *hdr; 35941da177e4SLinus Torvalds struct nlmsghdr *nlh; 359504561c1fSThomas Graf void *protoinfo; 35961da177e4SLinus Torvalds struct ifla_cacheinfo ci; 35971da177e4SLinus Torvalds 359804561c1fSThomas Graf nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags); 359904561c1fSThomas Graf if (nlh == NULL) 360026932566SPatrick McHardy return -EMSGSIZE; 36011da177e4SLinus Torvalds 360204561c1fSThomas Graf hdr = nlmsg_data(nlh); 360304561c1fSThomas Graf hdr->ifi_family = AF_INET6; 360404561c1fSThomas Graf hdr->__ifi_pad = 0; 360504561c1fSThomas Graf hdr->ifi_type = dev->type; 360604561c1fSThomas Graf hdr->ifi_index = dev->ifindex; 360704561c1fSThomas Graf hdr->ifi_flags = dev_get_flags(dev); 360804561c1fSThomas Graf hdr->ifi_change = 0; 360904561c1fSThomas Graf 361004561c1fSThomas Graf NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name); 36111da177e4SLinus Torvalds 36121da177e4SLinus Torvalds if (dev->addr_len) 361304561c1fSThomas Graf NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr); 36141da177e4SLinus Torvalds 361504561c1fSThomas Graf NLA_PUT_U32(skb, IFLA_MTU, dev->mtu); 36161da177e4SLinus Torvalds if (dev->ifindex != dev->iflink) 361704561c1fSThomas Graf NLA_PUT_U32(skb, IFLA_LINK, dev->iflink); 36181da177e4SLinus Torvalds 361904561c1fSThomas Graf protoinfo = nla_nest_start(skb, IFLA_PROTINFO); 362004561c1fSThomas Graf if (protoinfo == NULL) 362104561c1fSThomas Graf goto nla_put_failure; 36221da177e4SLinus Torvalds 362304561c1fSThomas Graf NLA_PUT_U32(skb, IFLA_INET6_FLAGS, idev->if_flags); 36241da177e4SLinus Torvalds 36251da177e4SLinus Torvalds ci.max_reasm_len = IPV6_MAXPLEN; 36261da177e4SLinus Torvalds ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100 36271da177e4SLinus Torvalds + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ); 36281da177e4SLinus Torvalds ci.reachable_time = idev->nd_parms->reachable_time; 36291da177e4SLinus Torvalds ci.retrans_time = idev->nd_parms->retrans_time; 363004561c1fSThomas Graf NLA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci); 36311da177e4SLinus Torvalds 3632bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32)); 3633bf99f1bdSYOSHIFUJI Hideaki if (nla == NULL) 363404561c1fSThomas Graf goto nla_put_failure; 3635bf99f1bdSYOSHIFUJI Hideaki ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla)); 36361da177e4SLinus Torvalds 3637bf99f1bdSYOSHIFUJI Hideaki /* XXX - MC not implemented */ 3638bf99f1bdSYOSHIFUJI Hideaki 3639bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64)); 3640bf99f1bdSYOSHIFUJI Hideaki if (nla == NULL) 3641bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 3642bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla)); 3643bf99f1bdSYOSHIFUJI Hideaki 3644bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64)); 3645bf99f1bdSYOSHIFUJI Hideaki if (nla == NULL) 3646bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 3647bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla)); 36481da177e4SLinus Torvalds 364904561c1fSThomas Graf nla_nest_end(skb, protoinfo); 365004561c1fSThomas Graf return nlmsg_end(skb, nlh); 36511da177e4SLinus Torvalds 365204561c1fSThomas Graf nla_put_failure: 365326932566SPatrick McHardy nlmsg_cancel(skb, nlh); 365426932566SPatrick McHardy return -EMSGSIZE; 36551da177e4SLinus Torvalds } 36561da177e4SLinus Torvalds 36571da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 36581da177e4SLinus Torvalds { 3659b854272bSDenis V. Lunev struct net *net = skb->sk->sk_net; 36601da177e4SLinus Torvalds int idx, err; 36611da177e4SLinus Torvalds int s_idx = cb->args[0]; 36621da177e4SLinus Torvalds struct net_device *dev; 36631da177e4SLinus Torvalds struct inet6_dev *idev; 36641da177e4SLinus Torvalds 3665b854272bSDenis V. Lunev if (net != &init_net) 3666b854272bSDenis V. Lunev return 0; 3667b854272bSDenis V. Lunev 36681da177e4SLinus Torvalds read_lock(&dev_base_lock); 36697562f876SPavel Emelianov idx = 0; 3670881d966bSEric W. Biederman for_each_netdev(&init_net, dev) { 36711da177e4SLinus Torvalds if (idx < s_idx) 36727562f876SPavel Emelianov goto cont; 36731da177e4SLinus Torvalds if ((idev = in6_dev_get(dev)) == NULL) 36747562f876SPavel Emelianov goto cont; 36751da177e4SLinus Torvalds err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid, 3676b6544c0bSJamal Hadi Salim cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI); 36771da177e4SLinus Torvalds in6_dev_put(idev); 36781da177e4SLinus Torvalds if (err <= 0) 36791da177e4SLinus Torvalds break; 36807562f876SPavel Emelianov cont: 36817562f876SPavel Emelianov idx++; 36821da177e4SLinus Torvalds } 36831da177e4SLinus Torvalds read_unlock(&dev_base_lock); 36841da177e4SLinus Torvalds cb->args[0] = idx; 36851da177e4SLinus Torvalds 36861da177e4SLinus Torvalds return skb->len; 36871da177e4SLinus Torvalds } 36881da177e4SLinus Torvalds 36891da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev) 36901da177e4SLinus Torvalds { 36911da177e4SLinus Torvalds struct sk_buff *skb; 36928d7a76c9SThomas Graf int err = -ENOBUFS; 36931da177e4SLinus Torvalds 3694339bf98fSThomas Graf skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC); 36958d7a76c9SThomas Graf if (skb == NULL) 36968d7a76c9SThomas Graf goto errout; 36978d7a76c9SThomas Graf 36988d7a76c9SThomas Graf err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0); 369926932566SPatrick McHardy if (err < 0) { 370026932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */ 370126932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 370226932566SPatrick McHardy kfree_skb(skb); 370326932566SPatrick McHardy goto errout; 370426932566SPatrick McHardy } 370597c53cacSDenis V. Lunev err = rtnl_notify(skb, &init_net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC); 37068d7a76c9SThomas Graf errout: 37078d7a76c9SThomas Graf if (err < 0) 370897c53cacSDenis V. Lunev rtnl_set_sk_err(&init_net, RTNLGRP_IPV6_IFADDR, err); 37091da177e4SLinus Torvalds } 37101da177e4SLinus Torvalds 3711339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void) 3712339bf98fSThomas Graf { 3713339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct prefixmsg)) 3714339bf98fSThomas Graf + nla_total_size(sizeof(struct in6_addr)) 3715339bf98fSThomas Graf + nla_total_size(sizeof(struct prefix_cacheinfo)); 3716339bf98fSThomas Graf } 3717c5396a31SYOSHIFUJI Hideaki 37181da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev, 3719b6544c0bSJamal Hadi Salim struct prefix_info *pinfo, u32 pid, u32 seq, 3720b6544c0bSJamal Hadi Salim int event, unsigned int flags) 37211da177e4SLinus Torvalds { 37221da177e4SLinus Torvalds struct prefixmsg *pmsg; 37231da177e4SLinus Torvalds struct nlmsghdr *nlh; 37241da177e4SLinus Torvalds struct prefix_cacheinfo ci; 37251da177e4SLinus Torvalds 37266051e2f4SThomas Graf nlh = nlmsg_put(skb, pid, seq, event, sizeof(*pmsg), flags); 37276051e2f4SThomas Graf if (nlh == NULL) 372826932566SPatrick McHardy return -EMSGSIZE; 37296051e2f4SThomas Graf 37306051e2f4SThomas Graf pmsg = nlmsg_data(nlh); 37311da177e4SLinus Torvalds pmsg->prefix_family = AF_INET6; 37328a47077aSPatrick McHardy pmsg->prefix_pad1 = 0; 37338a47077aSPatrick McHardy pmsg->prefix_pad2 = 0; 37341da177e4SLinus Torvalds pmsg->prefix_ifindex = idev->dev->ifindex; 37351da177e4SLinus Torvalds pmsg->prefix_len = pinfo->prefix_len; 37361da177e4SLinus Torvalds pmsg->prefix_type = pinfo->type; 37378a47077aSPatrick McHardy pmsg->prefix_pad3 = 0; 37381da177e4SLinus Torvalds pmsg->prefix_flags = 0; 37391da177e4SLinus Torvalds if (pinfo->onlink) 37401da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_ONLINK; 37411da177e4SLinus Torvalds if (pinfo->autoconf) 37421da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_AUTOCONF; 37431da177e4SLinus Torvalds 37446051e2f4SThomas Graf NLA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix); 37451da177e4SLinus Torvalds 37461da177e4SLinus Torvalds ci.preferred_time = ntohl(pinfo->prefered); 37471da177e4SLinus Torvalds ci.valid_time = ntohl(pinfo->valid); 37486051e2f4SThomas Graf NLA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci); 37491da177e4SLinus Torvalds 37506051e2f4SThomas Graf return nlmsg_end(skb, nlh); 37511da177e4SLinus Torvalds 37526051e2f4SThomas Graf nla_put_failure: 375326932566SPatrick McHardy nlmsg_cancel(skb, nlh); 375426932566SPatrick McHardy return -EMSGSIZE; 37551da177e4SLinus Torvalds } 37561da177e4SLinus Torvalds 37571da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev, 37581da177e4SLinus Torvalds struct prefix_info *pinfo) 37591da177e4SLinus Torvalds { 37601da177e4SLinus Torvalds struct sk_buff *skb; 37618c384bfaSThomas Graf int err = -ENOBUFS; 37621da177e4SLinus Torvalds 3763339bf98fSThomas Graf skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC); 37648c384bfaSThomas Graf if (skb == NULL) 37658c384bfaSThomas Graf goto errout; 37668c384bfaSThomas Graf 37678c384bfaSThomas Graf err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0); 376826932566SPatrick McHardy if (err < 0) { 376926932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */ 377026932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 377126932566SPatrick McHardy kfree_skb(skb); 377226932566SPatrick McHardy goto errout; 377326932566SPatrick McHardy } 377497c53cacSDenis V. Lunev err = rtnl_notify(skb, &init_net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC); 37758c384bfaSThomas Graf errout: 37768c384bfaSThomas Graf if (err < 0) 377797c53cacSDenis V. Lunev rtnl_set_sk_err(&init_net, RTNLGRP_IPV6_PREFIX, err); 37781da177e4SLinus Torvalds } 37791da177e4SLinus Torvalds 37801da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 37811da177e4SLinus Torvalds { 37821da177e4SLinus Torvalds inet6_ifa_notify(event ? : RTM_NEWADDR, ifp); 37831da177e4SLinus Torvalds 37841da177e4SLinus Torvalds switch (event) { 37851da177e4SLinus Torvalds case RTM_NEWADDR: 378695c385b4SNeil Horman /* 378795c385b4SNeil Horman * If the address was optimistic 378895c385b4SNeil Horman * we inserted the route at the start of 378995c385b4SNeil Horman * our DAD process, so we don't need 379095c385b4SNeil Horman * to do it again 379195c385b4SNeil Horman */ 379295c385b4SNeil Horman if (!(ifp->rt->rt6i_node)) 379340e22e8fSThomas Graf ip6_ins_rt(ifp->rt); 37941da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 37951da177e4SLinus Torvalds addrconf_join_anycast(ifp); 37961da177e4SLinus Torvalds break; 37971da177e4SLinus Torvalds case RTM_DELADDR: 37981da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 37991da177e4SLinus Torvalds addrconf_leave_anycast(ifp); 38001da177e4SLinus Torvalds addrconf_leave_solict(ifp->idev, &ifp->addr); 38011da177e4SLinus Torvalds dst_hold(&ifp->rt->u.dst); 3802e0a1ad73SThomas Graf if (ip6_del_rt(ifp->rt)) 38031da177e4SLinus Torvalds dst_free(&ifp->rt->u.dst); 38041da177e4SLinus Torvalds break; 38051da177e4SLinus Torvalds } 38061da177e4SLinus Torvalds } 38071da177e4SLinus Torvalds 38081da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 38091da177e4SLinus Torvalds { 38108814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 38111da177e4SLinus Torvalds if (likely(ifp->idev->dead == 0)) 38121da177e4SLinus Torvalds __ipv6_ifa_notify(event, ifp); 38138814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 38141da177e4SLinus Torvalds } 38151da177e4SLinus Torvalds 38161da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 38171da177e4SLinus Torvalds 38181da177e4SLinus Torvalds static 38191da177e4SLinus Torvalds int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp, 38201da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 38211da177e4SLinus Torvalds { 38221da177e4SLinus Torvalds int *valp = ctl->data; 38231da177e4SLinus Torvalds int val = *valp; 38241da177e4SLinus Torvalds int ret; 38251da177e4SLinus Torvalds 38261da177e4SLinus Torvalds ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos); 38271da177e4SLinus Torvalds 3828c8fecf22SPavel Emelyanov if (write) 3829c8fecf22SPavel Emelyanov addrconf_fixup_forwarding(ctl, valp, val); 38301da177e4SLinus Torvalds return ret; 38311da177e4SLinus Torvalds } 38321da177e4SLinus Torvalds 38331da177e4SLinus Torvalds static int addrconf_sysctl_forward_strategy(ctl_table *table, 38341da177e4SLinus Torvalds int __user *name, int nlen, 38351da177e4SLinus Torvalds void __user *oldval, 38361da177e4SLinus Torvalds size_t __user *oldlenp, 38371f29bcd7SAlexey Dobriyan void __user *newval, size_t newlen) 38381da177e4SLinus Torvalds { 38391da177e4SLinus Torvalds int *valp = table->data; 3840c8fecf22SPavel Emelyanov int val = *valp; 38411da177e4SLinus Torvalds int new; 38421da177e4SLinus Torvalds 38431da177e4SLinus Torvalds if (!newval || !newlen) 38441da177e4SLinus Torvalds return 0; 38451da177e4SLinus Torvalds if (newlen != sizeof(int)) 38461da177e4SLinus Torvalds return -EINVAL; 38471da177e4SLinus Torvalds if (get_user(new, (int __user *)newval)) 38481da177e4SLinus Torvalds return -EFAULT; 38491da177e4SLinus Torvalds if (new == *valp) 38501da177e4SLinus Torvalds return 0; 38511da177e4SLinus Torvalds if (oldval && oldlenp) { 38521da177e4SLinus Torvalds size_t len; 38531da177e4SLinus Torvalds if (get_user(len, oldlenp)) 38541da177e4SLinus Torvalds return -EFAULT; 38551da177e4SLinus Torvalds if (len) { 38561da177e4SLinus Torvalds if (len > table->maxlen) 38571da177e4SLinus Torvalds len = table->maxlen; 38581da177e4SLinus Torvalds if (copy_to_user(oldval, valp, len)) 38591da177e4SLinus Torvalds return -EFAULT; 38601da177e4SLinus Torvalds if (put_user(len, oldlenp)) 38611da177e4SLinus Torvalds return -EFAULT; 38621da177e4SLinus Torvalds } 38631da177e4SLinus Torvalds } 38641da177e4SLinus Torvalds 38651da177e4SLinus Torvalds *valp = new; 3866c8fecf22SPavel Emelyanov addrconf_fixup_forwarding(table, valp, val); 38671da177e4SLinus Torvalds return 1; 38681da177e4SLinus Torvalds } 38691da177e4SLinus Torvalds 38701da177e4SLinus Torvalds static struct addrconf_sysctl_table 38711da177e4SLinus Torvalds { 38721da177e4SLinus Torvalds struct ctl_table_header *sysctl_header; 38731da177e4SLinus Torvalds ctl_table addrconf_vars[__NET_IPV6_MAX]; 38741dab6222SPavel Emelyanov char *dev_name; 3875ab32ea5dSBrian Haley } addrconf_sysctl __read_mostly = { 38761da177e4SLinus Torvalds .sysctl_header = NULL, 38771da177e4SLinus Torvalds .addrconf_vars = { 38781da177e4SLinus Torvalds { 38791da177e4SLinus Torvalds .ctl_name = NET_IPV6_FORWARDING, 38801da177e4SLinus Torvalds .procname = "forwarding", 38811da177e4SLinus Torvalds .data = &ipv6_devconf.forwarding, 38821da177e4SLinus Torvalds .maxlen = sizeof(int), 38831da177e4SLinus Torvalds .mode = 0644, 38841da177e4SLinus Torvalds .proc_handler = &addrconf_sysctl_forward, 38851da177e4SLinus Torvalds .strategy = &addrconf_sysctl_forward_strategy, 38861da177e4SLinus Torvalds }, 38871da177e4SLinus Torvalds { 38881da177e4SLinus Torvalds .ctl_name = NET_IPV6_HOP_LIMIT, 38891da177e4SLinus Torvalds .procname = "hop_limit", 38901da177e4SLinus Torvalds .data = &ipv6_devconf.hop_limit, 38911da177e4SLinus Torvalds .maxlen = sizeof(int), 38921da177e4SLinus Torvalds .mode = 0644, 38931da177e4SLinus Torvalds .proc_handler = proc_dointvec, 38941da177e4SLinus Torvalds }, 38951da177e4SLinus Torvalds { 38961da177e4SLinus Torvalds .ctl_name = NET_IPV6_MTU, 38971da177e4SLinus Torvalds .procname = "mtu", 38981da177e4SLinus Torvalds .data = &ipv6_devconf.mtu6, 38991da177e4SLinus Torvalds .maxlen = sizeof(int), 39001da177e4SLinus Torvalds .mode = 0644, 39011da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 39021da177e4SLinus Torvalds }, 39031da177e4SLinus Torvalds { 39041da177e4SLinus Torvalds .ctl_name = NET_IPV6_ACCEPT_RA, 39051da177e4SLinus Torvalds .procname = "accept_ra", 39061da177e4SLinus Torvalds .data = &ipv6_devconf.accept_ra, 39071da177e4SLinus Torvalds .maxlen = sizeof(int), 39081da177e4SLinus Torvalds .mode = 0644, 39091da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 39101da177e4SLinus Torvalds }, 39111da177e4SLinus Torvalds { 39121da177e4SLinus Torvalds .ctl_name = NET_IPV6_ACCEPT_REDIRECTS, 39131da177e4SLinus Torvalds .procname = "accept_redirects", 39141da177e4SLinus Torvalds .data = &ipv6_devconf.accept_redirects, 39151da177e4SLinus Torvalds .maxlen = sizeof(int), 39161da177e4SLinus Torvalds .mode = 0644, 39171da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 39181da177e4SLinus Torvalds }, 39191da177e4SLinus Torvalds { 39201da177e4SLinus Torvalds .ctl_name = NET_IPV6_AUTOCONF, 39211da177e4SLinus Torvalds .procname = "autoconf", 39221da177e4SLinus Torvalds .data = &ipv6_devconf.autoconf, 39231da177e4SLinus Torvalds .maxlen = sizeof(int), 39241da177e4SLinus Torvalds .mode = 0644, 39251da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 39261da177e4SLinus Torvalds }, 39271da177e4SLinus Torvalds { 39281da177e4SLinus Torvalds .ctl_name = NET_IPV6_DAD_TRANSMITS, 39291da177e4SLinus Torvalds .procname = "dad_transmits", 39301da177e4SLinus Torvalds .data = &ipv6_devconf.dad_transmits, 39311da177e4SLinus Torvalds .maxlen = sizeof(int), 39321da177e4SLinus Torvalds .mode = 0644, 39331da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 39341da177e4SLinus Torvalds }, 39351da177e4SLinus Torvalds { 39361da177e4SLinus Torvalds .ctl_name = NET_IPV6_RTR_SOLICITS, 39371da177e4SLinus Torvalds .procname = "router_solicitations", 39381da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicits, 39391da177e4SLinus Torvalds .maxlen = sizeof(int), 39401da177e4SLinus Torvalds .mode = 0644, 39411da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 39421da177e4SLinus Torvalds }, 39431da177e4SLinus Torvalds { 39441da177e4SLinus Torvalds .ctl_name = NET_IPV6_RTR_SOLICIT_INTERVAL, 39451da177e4SLinus Torvalds .procname = "router_solicitation_interval", 39461da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_interval, 39471da177e4SLinus Torvalds .maxlen = sizeof(int), 39481da177e4SLinus Torvalds .mode = 0644, 39491da177e4SLinus Torvalds .proc_handler = &proc_dointvec_jiffies, 39501da177e4SLinus Torvalds .strategy = &sysctl_jiffies, 39511da177e4SLinus Torvalds }, 39521da177e4SLinus Torvalds { 39531da177e4SLinus Torvalds .ctl_name = NET_IPV6_RTR_SOLICIT_DELAY, 39541da177e4SLinus Torvalds .procname = "router_solicitation_delay", 39551da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_delay, 39561da177e4SLinus Torvalds .maxlen = sizeof(int), 39571da177e4SLinus Torvalds .mode = 0644, 39581da177e4SLinus Torvalds .proc_handler = &proc_dointvec_jiffies, 39591da177e4SLinus Torvalds .strategy = &sysctl_jiffies, 39601da177e4SLinus Torvalds }, 39611da177e4SLinus Torvalds { 39621da177e4SLinus Torvalds .ctl_name = NET_IPV6_FORCE_MLD_VERSION, 39631da177e4SLinus Torvalds .procname = "force_mld_version", 39641da177e4SLinus Torvalds .data = &ipv6_devconf.force_mld_version, 39651da177e4SLinus Torvalds .maxlen = sizeof(int), 39661da177e4SLinus Torvalds .mode = 0644, 39671da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 39681da177e4SLinus Torvalds }, 39691da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 39701da177e4SLinus Torvalds { 39711da177e4SLinus Torvalds .ctl_name = NET_IPV6_USE_TEMPADDR, 39721da177e4SLinus Torvalds .procname = "use_tempaddr", 39731da177e4SLinus Torvalds .data = &ipv6_devconf.use_tempaddr, 39741da177e4SLinus Torvalds .maxlen = sizeof(int), 39751da177e4SLinus Torvalds .mode = 0644, 39761da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 39771da177e4SLinus Torvalds }, 39781da177e4SLinus Torvalds { 39791da177e4SLinus Torvalds .ctl_name = NET_IPV6_TEMP_VALID_LFT, 39801da177e4SLinus Torvalds .procname = "temp_valid_lft", 39811da177e4SLinus Torvalds .data = &ipv6_devconf.temp_valid_lft, 39821da177e4SLinus Torvalds .maxlen = sizeof(int), 39831da177e4SLinus Torvalds .mode = 0644, 39841da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 39851da177e4SLinus Torvalds }, 39861da177e4SLinus Torvalds { 39871da177e4SLinus Torvalds .ctl_name = NET_IPV6_TEMP_PREFERED_LFT, 39881da177e4SLinus Torvalds .procname = "temp_prefered_lft", 39891da177e4SLinus Torvalds .data = &ipv6_devconf.temp_prefered_lft, 39901da177e4SLinus Torvalds .maxlen = sizeof(int), 39911da177e4SLinus Torvalds .mode = 0644, 39921da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 39931da177e4SLinus Torvalds }, 39941da177e4SLinus Torvalds { 39951da177e4SLinus Torvalds .ctl_name = NET_IPV6_REGEN_MAX_RETRY, 39961da177e4SLinus Torvalds .procname = "regen_max_retry", 39971da177e4SLinus Torvalds .data = &ipv6_devconf.regen_max_retry, 39981da177e4SLinus Torvalds .maxlen = sizeof(int), 39991da177e4SLinus Torvalds .mode = 0644, 40001da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 40011da177e4SLinus Torvalds }, 40021da177e4SLinus Torvalds { 40031da177e4SLinus Torvalds .ctl_name = NET_IPV6_MAX_DESYNC_FACTOR, 40041da177e4SLinus Torvalds .procname = "max_desync_factor", 40051da177e4SLinus Torvalds .data = &ipv6_devconf.max_desync_factor, 40061da177e4SLinus Torvalds .maxlen = sizeof(int), 40071da177e4SLinus Torvalds .mode = 0644, 40081da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 40091da177e4SLinus Torvalds }, 40101da177e4SLinus Torvalds #endif 40111da177e4SLinus Torvalds { 40121da177e4SLinus Torvalds .ctl_name = NET_IPV6_MAX_ADDRESSES, 40131da177e4SLinus Torvalds .procname = "max_addresses", 40141da177e4SLinus Torvalds .data = &ipv6_devconf.max_addresses, 40151da177e4SLinus Torvalds .maxlen = sizeof(int), 40161da177e4SLinus Torvalds .mode = 0644, 40171da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 40181da177e4SLinus Torvalds }, 40191da177e4SLinus Torvalds { 402065f5c7c1SYOSHIFUJI Hideaki .ctl_name = NET_IPV6_ACCEPT_RA_DEFRTR, 402165f5c7c1SYOSHIFUJI Hideaki .procname = "accept_ra_defrtr", 402265f5c7c1SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_defrtr, 402365f5c7c1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 402465f5c7c1SYOSHIFUJI Hideaki .mode = 0644, 402565f5c7c1SYOSHIFUJI Hideaki .proc_handler = &proc_dointvec, 402665f5c7c1SYOSHIFUJI Hideaki }, 402765f5c7c1SYOSHIFUJI Hideaki { 4028c4fd30ebSYOSHIFUJI Hideaki .ctl_name = NET_IPV6_ACCEPT_RA_PINFO, 4029c4fd30ebSYOSHIFUJI Hideaki .procname = "accept_ra_pinfo", 4030c4fd30ebSYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_pinfo, 4031c4fd30ebSYOSHIFUJI Hideaki .maxlen = sizeof(int), 4032c4fd30ebSYOSHIFUJI Hideaki .mode = 0644, 4033c4fd30ebSYOSHIFUJI Hideaki .proc_handler = &proc_dointvec, 4034c4fd30ebSYOSHIFUJI Hideaki }, 4035930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 4036930d6ff2SYOSHIFUJI Hideaki { 4037930d6ff2SYOSHIFUJI Hideaki .ctl_name = NET_IPV6_ACCEPT_RA_RTR_PREF, 4038930d6ff2SYOSHIFUJI Hideaki .procname = "accept_ra_rtr_pref", 4039930d6ff2SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rtr_pref, 4040930d6ff2SYOSHIFUJI Hideaki .maxlen = sizeof(int), 4041930d6ff2SYOSHIFUJI Hideaki .mode = 0644, 4042930d6ff2SYOSHIFUJI Hideaki .proc_handler = &proc_dointvec, 4043930d6ff2SYOSHIFUJI Hideaki }, 404452e16356SYOSHIFUJI Hideaki { 404552e16356SYOSHIFUJI Hideaki .ctl_name = NET_IPV6_RTR_PROBE_INTERVAL, 404652e16356SYOSHIFUJI Hideaki .procname = "router_probe_interval", 404752e16356SYOSHIFUJI Hideaki .data = &ipv6_devconf.rtr_probe_interval, 404852e16356SYOSHIFUJI Hideaki .maxlen = sizeof(int), 404952e16356SYOSHIFUJI Hideaki .mode = 0644, 405052e16356SYOSHIFUJI Hideaki .proc_handler = &proc_dointvec_jiffies, 405152e16356SYOSHIFUJI Hideaki .strategy = &sysctl_jiffies, 405252e16356SYOSHIFUJI Hideaki }, 4053fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 405409c884d4SYOSHIFUJI Hideaki { 405509c884d4SYOSHIFUJI Hideaki .ctl_name = NET_IPV6_ACCEPT_RA_RT_INFO_MAX_PLEN, 405609c884d4SYOSHIFUJI Hideaki .procname = "accept_ra_rt_info_max_plen", 405709c884d4SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rt_info_max_plen, 405809c884d4SYOSHIFUJI Hideaki .maxlen = sizeof(int), 405909c884d4SYOSHIFUJI Hideaki .mode = 0644, 406009c884d4SYOSHIFUJI Hideaki .proc_handler = &proc_dointvec, 406109c884d4SYOSHIFUJI Hideaki }, 406209c884d4SYOSHIFUJI Hideaki #endif 4063930d6ff2SYOSHIFUJI Hideaki #endif 4064c4fd30ebSYOSHIFUJI Hideaki { 4065fbea49e1SYOSHIFUJI Hideaki .ctl_name = NET_IPV6_PROXY_NDP, 4066fbea49e1SYOSHIFUJI Hideaki .procname = "proxy_ndp", 4067fbea49e1SYOSHIFUJI Hideaki .data = &ipv6_devconf.proxy_ndp, 4068fbea49e1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 4069fbea49e1SYOSHIFUJI Hideaki .mode = 0644, 4070fbea49e1SYOSHIFUJI Hideaki .proc_handler = &proc_dointvec, 4071fbea49e1SYOSHIFUJI Hideaki }, 4072fbea49e1SYOSHIFUJI Hideaki { 40730bcbc926SYOSHIFUJI Hideaki .ctl_name = NET_IPV6_ACCEPT_SOURCE_ROUTE, 40740bcbc926SYOSHIFUJI Hideaki .procname = "accept_source_route", 40750bcbc926SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_source_route, 40760bcbc926SYOSHIFUJI Hideaki .maxlen = sizeof(int), 40770bcbc926SYOSHIFUJI Hideaki .mode = 0644, 40780bcbc926SYOSHIFUJI Hideaki .proc_handler = &proc_dointvec, 40790bcbc926SYOSHIFUJI Hideaki }, 408095c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 408195c385b4SNeil Horman { 408295c385b4SNeil Horman .ctl_name = CTL_UNNUMBERED, 408395c385b4SNeil Horman .procname = "optimistic_dad", 408495c385b4SNeil Horman .data = &ipv6_devconf.optimistic_dad, 408595c385b4SNeil Horman .maxlen = sizeof(int), 408695c385b4SNeil Horman .mode = 0644, 408795c385b4SNeil Horman .proc_handler = &proc_dointvec, 408895c385b4SNeil Horman 408995c385b4SNeil Horman }, 409095c385b4SNeil Horman #endif 40910bcbc926SYOSHIFUJI Hideaki { 40921da177e4SLinus Torvalds .ctl_name = 0, /* sentinel */ 40931da177e4SLinus Torvalds } 40941da177e4SLinus Torvalds }, 40951da177e4SLinus Torvalds }; 40961da177e4SLinus Torvalds 4097bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name, 4098bff16c2fSPavel Emelyanov int ctl_name, struct inet6_dev *idev, struct ipv6_devconf *p) 40991da177e4SLinus Torvalds { 41001da177e4SLinus Torvalds int i; 41011da177e4SLinus Torvalds struct addrconf_sysctl_table *t; 41021da177e4SLinus Torvalds 41031dab6222SPavel Emelyanov #define ADDRCONF_CTL_PATH_DEV 3 41041dab6222SPavel Emelyanov 41051dab6222SPavel Emelyanov struct ctl_path addrconf_ctl_path[] = { 41061dab6222SPavel Emelyanov { .procname = "net", .ctl_name = CTL_NET, }, 41071dab6222SPavel Emelyanov { .procname = "ipv6", .ctl_name = NET_IPV6, }, 41081dab6222SPavel Emelyanov { .procname = "conf", .ctl_name = NET_IPV6_CONF, }, 41091dab6222SPavel Emelyanov { /* to be set */ }, 41101dab6222SPavel Emelyanov { }, 41111dab6222SPavel Emelyanov }; 41121dab6222SPavel Emelyanov 41131dab6222SPavel Emelyanov 4114af879cc7SArnaldo Carvalho de Melo t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL); 41151da177e4SLinus Torvalds if (t == NULL) 4116f68635e6SPavel Emelyanov goto out; 4117f68635e6SPavel Emelyanov 41181da177e4SLinus Torvalds for (i=0; t->addrconf_vars[i].data; i++) { 41191da177e4SLinus Torvalds t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf; 41201da177e4SLinus Torvalds t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */ 4121bff16c2fSPavel Emelyanov t->addrconf_vars[i].extra2 = net; 41221da177e4SLinus Torvalds } 41231da177e4SLinus Torvalds 41241da177e4SLinus Torvalds /* 41251da177e4SLinus Torvalds * Make a copy of dev_name, because '.procname' is regarded as const 41261da177e4SLinus Torvalds * by sysctl and we wouldn't want anyone to change it under our feet 41271da177e4SLinus Torvalds * (see SIOCSIFNAME). 41281da177e4SLinus Torvalds */ 41291dab6222SPavel Emelyanov t->dev_name = kstrdup(dev_name, GFP_KERNEL); 41301dab6222SPavel Emelyanov if (!t->dev_name) 41311da177e4SLinus Torvalds goto free; 41321da177e4SLinus Torvalds 41331dab6222SPavel Emelyanov addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].procname = t->dev_name; 41341dab6222SPavel Emelyanov addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].ctl_name = ctl_name; 41351da177e4SLinus Torvalds 4136bff16c2fSPavel Emelyanov t->sysctl_header = register_net_sysctl_table(net, addrconf_ctl_path, 41371dab6222SPavel Emelyanov t->addrconf_vars); 41381da177e4SLinus Torvalds if (t->sysctl_header == NULL) 41391da177e4SLinus Torvalds goto free_procname; 4140f68635e6SPavel Emelyanov 41411da177e4SLinus Torvalds p->sysctl = t; 414295897312SPavel Emelyanov return 0; 41431da177e4SLinus Torvalds 41441da177e4SLinus Torvalds free_procname: 41451dab6222SPavel Emelyanov kfree(t->dev_name); 41461da177e4SLinus Torvalds free: 41471da177e4SLinus Torvalds kfree(t); 4148f68635e6SPavel Emelyanov out: 414995897312SPavel Emelyanov return -ENOBUFS; 41501da177e4SLinus Torvalds } 41511da177e4SLinus Torvalds 4152408c4768SPavel Emelyanov static void __addrconf_sysctl_unregister(struct ipv6_devconf *p) 4153f52295a9SPavel Emelyanov { 4154408c4768SPavel Emelyanov struct addrconf_sysctl_table *t; 4155f52295a9SPavel Emelyanov 4156408c4768SPavel Emelyanov if (p->sysctl == NULL) 4157408c4768SPavel Emelyanov return; 4158408c4768SPavel Emelyanov 4159408c4768SPavel Emelyanov t = p->sysctl; 41601da177e4SLinus Torvalds p->sysctl = NULL; 41611da177e4SLinus Torvalds unregister_sysctl_table(t->sysctl_header); 41621dab6222SPavel Emelyanov kfree(t->dev_name); 41631da177e4SLinus Torvalds kfree(t); 41641da177e4SLinus Torvalds } 4165408c4768SPavel Emelyanov 4166408c4768SPavel Emelyanov static void addrconf_sysctl_register(struct inet6_dev *idev) 4167408c4768SPavel Emelyanov { 4168408c4768SPavel Emelyanov neigh_sysctl_register(idev->dev, idev->nd_parms, NET_IPV6, 4169408c4768SPavel Emelyanov NET_IPV6_NEIGH, "ipv6", 4170408c4768SPavel Emelyanov &ndisc_ifinfo_sysctl_change, 4171408c4768SPavel Emelyanov NULL); 4172bff16c2fSPavel Emelyanov __addrconf_sysctl_register(idev->dev->nd_net, idev->dev->name, 4173bff16c2fSPavel Emelyanov idev->dev->ifindex, idev, &idev->cnf); 4174408c4768SPavel Emelyanov } 4175408c4768SPavel Emelyanov 4176408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev) 4177408c4768SPavel Emelyanov { 4178408c4768SPavel Emelyanov __addrconf_sysctl_unregister(&idev->cnf); 4179408c4768SPavel Emelyanov neigh_sysctl_unregister(idev->nd_parms); 41801da177e4SLinus Torvalds } 41811da177e4SLinus Torvalds 41821da177e4SLinus Torvalds 41831da177e4SLinus Torvalds #endif 41841da177e4SLinus Torvalds 4185e0da5a48SPavel Emelyanov static int addrconf_init_net(struct net *net) 4186e0da5a48SPavel Emelyanov { 4187e0da5a48SPavel Emelyanov int err; 4188e0da5a48SPavel Emelyanov struct ipv6_devconf *all, *dflt; 4189e0da5a48SPavel Emelyanov 4190e0da5a48SPavel Emelyanov err = -ENOMEM; 4191e0da5a48SPavel Emelyanov all = &ipv6_devconf; 4192e0da5a48SPavel Emelyanov dflt = &ipv6_devconf_dflt; 4193e0da5a48SPavel Emelyanov 4194e0da5a48SPavel Emelyanov if (net != &init_net) { 4195e0da5a48SPavel Emelyanov all = kmemdup(all, sizeof(ipv6_devconf), GFP_KERNEL); 4196e0da5a48SPavel Emelyanov if (all == NULL) 4197e0da5a48SPavel Emelyanov goto err_alloc_all; 4198e0da5a48SPavel Emelyanov 4199e0da5a48SPavel Emelyanov dflt = kmemdup(dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL); 4200e0da5a48SPavel Emelyanov if (dflt == NULL) 4201e0da5a48SPavel Emelyanov goto err_alloc_dflt; 4202e0da5a48SPavel Emelyanov } 4203e0da5a48SPavel Emelyanov 4204e0da5a48SPavel Emelyanov net->ipv6.devconf_all = all; 4205e0da5a48SPavel Emelyanov net->ipv6.devconf_dflt = dflt; 4206e0da5a48SPavel Emelyanov 4207e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 4208e0da5a48SPavel Emelyanov err = __addrconf_sysctl_register(net, "all", NET_PROTO_CONF_ALL, 4209e0da5a48SPavel Emelyanov NULL, all); 4210e0da5a48SPavel Emelyanov if (err < 0) 4211e0da5a48SPavel Emelyanov goto err_reg_all; 4212e0da5a48SPavel Emelyanov 4213e0da5a48SPavel Emelyanov err = __addrconf_sysctl_register(net, "default", NET_PROTO_CONF_DEFAULT, 4214e0da5a48SPavel Emelyanov NULL, dflt); 4215e0da5a48SPavel Emelyanov if (err < 0) 4216e0da5a48SPavel Emelyanov goto err_reg_dflt; 4217e0da5a48SPavel Emelyanov #endif 4218e0da5a48SPavel Emelyanov return 0; 4219e0da5a48SPavel Emelyanov 4220e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 4221e0da5a48SPavel Emelyanov err_reg_dflt: 4222e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(all); 4223e0da5a48SPavel Emelyanov err_reg_all: 4224e0da5a48SPavel Emelyanov kfree(dflt); 4225e0da5a48SPavel Emelyanov #endif 4226e0da5a48SPavel Emelyanov err_alloc_dflt: 4227e0da5a48SPavel Emelyanov kfree(all); 4228e0da5a48SPavel Emelyanov err_alloc_all: 4229e0da5a48SPavel Emelyanov return err; 4230e0da5a48SPavel Emelyanov } 4231e0da5a48SPavel Emelyanov 4232e0da5a48SPavel Emelyanov static void addrconf_exit_net(struct net *net) 4233e0da5a48SPavel Emelyanov { 4234e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 4235e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(net->ipv6.devconf_dflt); 4236e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(net->ipv6.devconf_all); 4237e0da5a48SPavel Emelyanov #endif 4238e0da5a48SPavel Emelyanov if (net != &init_net) { 4239e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_dflt); 4240e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_all); 4241e0da5a48SPavel Emelyanov } 4242e0da5a48SPavel Emelyanov } 4243e0da5a48SPavel Emelyanov 4244e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = { 4245e0da5a48SPavel Emelyanov .init = addrconf_init_net, 4246e0da5a48SPavel Emelyanov .exit = addrconf_exit_net, 4247e0da5a48SPavel Emelyanov }; 4248e0da5a48SPavel Emelyanov 42491da177e4SLinus Torvalds /* 42501da177e4SLinus Torvalds * Device notifier 42511da177e4SLinus Torvalds */ 42521da177e4SLinus Torvalds 42531da177e4SLinus Torvalds int register_inet6addr_notifier(struct notifier_block *nb) 42541da177e4SLinus Torvalds { 4255e041c683SAlan Stern return atomic_notifier_chain_register(&inet6addr_chain, nb); 42561da177e4SLinus Torvalds } 42571da177e4SLinus Torvalds 42587159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(register_inet6addr_notifier); 42597159039aSYOSHIFUJI Hideaki 42601da177e4SLinus Torvalds int unregister_inet6addr_notifier(struct notifier_block *nb) 42611da177e4SLinus Torvalds { 4262e041c683SAlan Stern return atomic_notifier_chain_unregister(&inet6addr_chain,nb); 42631da177e4SLinus Torvalds } 42641da177e4SLinus Torvalds 42657159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(unregister_inet6addr_notifier); 42667159039aSYOSHIFUJI Hideaki 42671da177e4SLinus Torvalds /* 42681da177e4SLinus Torvalds * Init / cleanup code 42691da177e4SLinus Torvalds */ 42701da177e4SLinus Torvalds 42711da177e4SLinus Torvalds int __init addrconf_init(void) 42721da177e4SLinus Torvalds { 42732a8cc6c8SYOSHIFUJI Hideaki int err; 42742a8cc6c8SYOSHIFUJI Hideaki 42752a8cc6c8SYOSHIFUJI Hideaki if ((err = ipv6_addr_label_init()) < 0) { 42762a8cc6c8SYOSHIFUJI Hideaki printk(KERN_CRIT "IPv6 Addrconf: cannot initialize default policy table: %d.\n", 42772a8cc6c8SYOSHIFUJI Hideaki err); 42782a8cc6c8SYOSHIFUJI Hideaki return err; 42792a8cc6c8SYOSHIFUJI Hideaki } 42801da177e4SLinus Torvalds 4281e0da5a48SPavel Emelyanov register_pernet_subsys(&addrconf_ops); 4282e0da5a48SPavel Emelyanov 42831da177e4SLinus Torvalds /* The addrconf netdev notifier requires that loopback_dev 42841da177e4SLinus Torvalds * has it's ipv6 private information allocated and setup 42851da177e4SLinus Torvalds * before it can bring up and give link-local addresses 42861da177e4SLinus Torvalds * to other devices which are up. 42871da177e4SLinus Torvalds * 42881da177e4SLinus Torvalds * Unfortunately, loopback_dev is not necessarily the first 42891da177e4SLinus Torvalds * entry in the global dev_base list of net devices. In fact, 42901da177e4SLinus Torvalds * it is likely to be the very last entry on that list. 42911da177e4SLinus Torvalds * So this causes the notifier registry below to try and 42921da177e4SLinus Torvalds * give link-local addresses to all devices besides loopback_dev 42931da177e4SLinus Torvalds * first, then loopback_dev, which cases all the non-loopback_dev 42941da177e4SLinus Torvalds * devices to fail to get a link-local address. 42951da177e4SLinus Torvalds * 42961da177e4SLinus Torvalds * So, as a temporary fix, allocate the ipv6 structure for 42971da177e4SLinus Torvalds * loopback_dev first by hand. 42981da177e4SLinus Torvalds * Longer term, all of the dependencies ipv6 has upon the loopback 42991da177e4SLinus Torvalds * device and it being up should be removed. 43001da177e4SLinus Torvalds */ 43011da177e4SLinus Torvalds rtnl_lock(); 43022774c7abSEric W. Biederman if (!ipv6_add_dev(init_net.loopback_dev)) 43031da177e4SLinus Torvalds err = -ENOMEM; 43041da177e4SLinus Torvalds rtnl_unlock(); 43051da177e4SLinus Torvalds if (err) 4306e0da5a48SPavel Emelyanov goto errlo; 43071da177e4SLinus Torvalds 43082774c7abSEric W. Biederman ip6_null_entry.u.dst.dev = init_net.loopback_dev; 43092774c7abSEric W. Biederman ip6_null_entry.rt6i_idev = in6_dev_get(init_net.loopback_dev); 43109a6bf6feSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MULTIPLE_TABLES 43112774c7abSEric W. Biederman ip6_prohibit_entry.u.dst.dev = init_net.loopback_dev; 43122774c7abSEric W. Biederman ip6_prohibit_entry.rt6i_idev = in6_dev_get(init_net.loopback_dev); 43132774c7abSEric W. Biederman ip6_blk_hole_entry.u.dst.dev = init_net.loopback_dev; 43142774c7abSEric W. Biederman ip6_blk_hole_entry.rt6i_idev = in6_dev_get(init_net.loopback_dev); 43159a6bf6feSYOSHIFUJI Hideaki #endif 4316c62dba90SHerbert Xu 43171da177e4SLinus Torvalds register_netdevice_notifier(&ipv6_dev_notf); 43181da177e4SLinus Torvalds 43191da177e4SLinus Torvalds addrconf_verify(0); 4320c127ea2cSThomas Graf 4321c127ea2cSThomas Graf err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo); 4322c127ea2cSThomas Graf if (err < 0) 4323c127ea2cSThomas Graf goto errout; 4324c127ea2cSThomas Graf 4325c127ea2cSThomas Graf /* Only the first call to __rtnl_register can fail */ 4326c127ea2cSThomas Graf __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL); 4327c127ea2cSThomas Graf __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL); 4328c127ea2cSThomas Graf __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr, inet6_dump_ifaddr); 4329c127ea2cSThomas Graf __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL, inet6_dump_ifmcaddr); 4330c127ea2cSThomas Graf __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL, inet6_dump_ifacaddr); 4331c127ea2cSThomas Graf 43322a8cc6c8SYOSHIFUJI Hideaki ipv6_addr_label_rtnl_register(); 43332a8cc6c8SYOSHIFUJI Hideaki 43341da177e4SLinus Torvalds return 0; 4335c127ea2cSThomas Graf errout: 4336c127ea2cSThomas Graf unregister_netdevice_notifier(&ipv6_dev_notf); 4337e0da5a48SPavel Emelyanov errlo: 4338e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 4339c127ea2cSThomas Graf 4340c127ea2cSThomas Graf return err; 43411da177e4SLinus Torvalds } 43421da177e4SLinus Torvalds 434309f7709fSDaniel Lezcano void addrconf_cleanup(void) 43441da177e4SLinus Torvalds { 43451da177e4SLinus Torvalds struct net_device *dev; 43461da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 43471da177e4SLinus Torvalds int i; 43481da177e4SLinus Torvalds 43491da177e4SLinus Torvalds unregister_netdevice_notifier(&ipv6_dev_notf); 43501da177e4SLinus Torvalds 4351e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 43521da177e4SLinus Torvalds 43531da177e4SLinus Torvalds rtnl_lock(); 43541da177e4SLinus Torvalds 43551da177e4SLinus Torvalds /* 43561da177e4SLinus Torvalds * clean dev list. 43571da177e4SLinus Torvalds */ 43581da177e4SLinus Torvalds 4359881d966bSEric W. Biederman for_each_netdev(&init_net, dev) { 4360dffe4f04SMicah Gruber if (__in6_dev_get(dev) == NULL) 43611da177e4SLinus Torvalds continue; 43621da177e4SLinus Torvalds addrconf_ifdown(dev, 1); 43631da177e4SLinus Torvalds } 43642774c7abSEric W. Biederman addrconf_ifdown(init_net.loopback_dev, 2); 43651da177e4SLinus Torvalds 43661da177e4SLinus Torvalds /* 43671da177e4SLinus Torvalds * Check hash table. 43681da177e4SLinus Torvalds */ 43691da177e4SLinus Torvalds 43701da177e4SLinus Torvalds write_lock_bh(&addrconf_hash_lock); 43711da177e4SLinus Torvalds for (i=0; i < IN6_ADDR_HSIZE; i++) { 43721da177e4SLinus Torvalds for (ifa=inet6_addr_lst[i]; ifa; ) { 43731da177e4SLinus Torvalds struct inet6_ifaddr *bifa; 43741da177e4SLinus Torvalds 43751da177e4SLinus Torvalds bifa = ifa; 43761da177e4SLinus Torvalds ifa = ifa->lst_next; 43771da177e4SLinus Torvalds printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa); 43781da177e4SLinus Torvalds /* Do not free it; something is wrong. 43791da177e4SLinus Torvalds Now we can investigate it with debugger. 43801da177e4SLinus Torvalds */ 43811da177e4SLinus Torvalds } 43821da177e4SLinus Torvalds } 43831da177e4SLinus Torvalds write_unlock_bh(&addrconf_hash_lock); 43841da177e4SLinus Torvalds 43851da177e4SLinus Torvalds del_timer(&addr_chk_timer); 43861da177e4SLinus Torvalds 43871da177e4SLinus Torvalds rtnl_unlock(); 43881da177e4SLinus Torvalds } 4389