11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * IPv6 Address [auto]configuration 31da177e4SLinus Torvalds * Linux INET6 implementation 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Authors: 61da177e4SLinus Torvalds * Pedro Roque <roque@di.fc.ul.pt> 71da177e4SLinus Torvalds * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 101da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 111da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 121da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 131da177e4SLinus Torvalds */ 141da177e4SLinus Torvalds 151da177e4SLinus Torvalds /* 161da177e4SLinus Torvalds * Changes: 171da177e4SLinus Torvalds * 181da177e4SLinus Torvalds * Janos Farkas : delete timer on ifdown 191da177e4SLinus Torvalds * <chexum@bankinf.banki.hu> 201da177e4SLinus Torvalds * Andi Kleen : kill double kfree on module 211da177e4SLinus Torvalds * unload. 221da177e4SLinus Torvalds * Maciej W. Rozycki : FDDI support 231da177e4SLinus Torvalds * sekiya@USAGI : Don't send too many RS 241da177e4SLinus Torvalds * packets. 251da177e4SLinus Torvalds * yoshfuji@USAGI : Fixed interval between DAD 261da177e4SLinus Torvalds * packets. 271da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI : improved accuracy of 281da177e4SLinus Torvalds * address validation timer. 291da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041) 301da177e4SLinus Torvalds * support. 311da177e4SLinus Torvalds * Yuji SEKIYA @USAGI : Don't assign a same IPv6 321da177e4SLinus Torvalds * address on a same interface. 331da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI : ARCnet support 341da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to 351da177e4SLinus Torvalds * seq_file. 36b1cacb68SYOSHIFUJI Hideaki * YOSHIFUJI Hideaki @USAGI : improved source address 37b1cacb68SYOSHIFUJI Hideaki * selection; consider scope, 38b1cacb68SYOSHIFUJI Hideaki * status etc. 391da177e4SLinus Torvalds */ 401da177e4SLinus Torvalds 41f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt 42f3213831SJoe Perches 431da177e4SLinus Torvalds #include <linux/errno.h> 441da177e4SLinus Torvalds #include <linux/types.h> 45a0bffffcSIlpo Järvinen #include <linux/kernel.h> 461da177e4SLinus Torvalds #include <linux/socket.h> 471da177e4SLinus Torvalds #include <linux/sockios.h> 481da177e4SLinus Torvalds #include <linux/net.h> 493d1bec99SHannes Frederic Sowa #include <linux/inet.h> 501da177e4SLinus Torvalds #include <linux/in6.h> 511da177e4SLinus Torvalds #include <linux/netdevice.h> 521823730fSThomas Graf #include <linux/if_addr.h> 531da177e4SLinus Torvalds #include <linux/if_arp.h> 541da177e4SLinus Torvalds #include <linux/if_arcnet.h> 551da177e4SLinus Torvalds #include <linux/if_infiniband.h> 561da177e4SLinus Torvalds #include <linux/route.h> 571da177e4SLinus Torvalds #include <linux/inetdevice.h> 581da177e4SLinus Torvalds #include <linux/init.h> 595a0e3ad6STejun Heo #include <linux/slab.h> 601da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 611da177e4SLinus Torvalds #include <linux/sysctl.h> 621da177e4SLinus Torvalds #endif 634fc268d2SRandy Dunlap #include <linux/capability.h> 641da177e4SLinus Torvalds #include <linux/delay.h> 651da177e4SLinus Torvalds #include <linux/notifier.h> 66543537bdSPaulo Marques #include <linux/string.h> 67ddbe5032SEric Dumazet #include <linux/hash.h> 681da177e4SLinus Torvalds 69457c4cbcSEric W. Biederman #include <net/net_namespace.h> 701da177e4SLinus Torvalds #include <net/sock.h> 711da177e4SLinus Torvalds #include <net/snmp.h> 721da177e4SLinus Torvalds 735241c2d7SAlexander Aring #include <net/6lowpan.h> 74cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 #include <net/firewire.h> 751da177e4SLinus Torvalds #include <net/ipv6.h> 761da177e4SLinus Torvalds #include <net/protocol.h> 771da177e4SLinus Torvalds #include <net/ndisc.h> 781da177e4SLinus Torvalds #include <net/ip6_route.h> 791da177e4SLinus Torvalds #include <net/addrconf.h> 801da177e4SLinus Torvalds #include <net/tcp.h> 811da177e4SLinus Torvalds #include <net/ip.h> 825d620266SThomas Graf #include <net/netlink.h> 83f24e3d65SMitsuru Chinen #include <net/pkt_sched.h> 84ca254490SDavid Ahern #include <net/l3mdev.h> 851da177e4SLinus Torvalds #include <linux/if_tunnel.h> 861da177e4SLinus Torvalds #include <linux/rtnetlink.h> 87f3a1bfb1SNicolas Dichtel #include <linux/netconf.h> 881da177e4SLinus Torvalds #include <linux/random.h> 89e21e8467SStephen Hemminger #include <linux/uaccess.h> 907f7d9a6bSHerbert Xu #include <asm/unaligned.h> 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds #include <linux/proc_fs.h> 931da177e4SLinus Torvalds #include <linux/seq_file.h> 94bc3b2d7fSPaul Gortmaker #include <linux/export.h> 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds /* Set to 3 to get tracing... */ 971da177e4SLinus Torvalds #define ACONF_DEBUG 2 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds #if ACONF_DEBUG >= 3 100ba3542e1Sdingtianhong #define ADBG(fmt, ...) printk(fmt, ##__VA_ARGS__) 1011da177e4SLinus Torvalds #else 102ba3542e1Sdingtianhong #define ADBG(fmt, ...) do { if (0) printk(fmt, ##__VA_ARGS__); } while (0) 1031da177e4SLinus Torvalds #endif 1041da177e4SLinus Torvalds 1051da177e4SLinus Torvalds #define INFINITY_LIFE_TIME 0xFFFFFFFF 10618a31e1eSThomas Graf 1073d1bec99SHannes Frederic Sowa #define IPV6_MAX_STRLEN \ 1083d1bec99SHannes Frederic Sowa sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255") 1093d1bec99SHannes Frederic Sowa 11018a31e1eSThomas Graf static inline u32 cstamp_delta(unsigned long cstamp) 11118a31e1eSThomas Graf { 11218a31e1eSThomas Graf return (cstamp - INITIAL_JIFFIES) * 100UL / HZ; 11318a31e1eSThomas Graf } 1141da177e4SLinus Torvalds 1151da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 116a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev); 117408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev); 118408c4768SPavel Emelyanov #else 119a317a2f1SWANG Cong static inline int addrconf_sysctl_register(struct inet6_dev *idev) 120408c4768SPavel Emelyanov { 121a317a2f1SWANG Cong return 0; 122408c4768SPavel Emelyanov } 123408c4768SPavel Emelyanov 124408c4768SPavel Emelyanov static inline void addrconf_sysctl_unregister(struct inet6_dev *idev) 125408c4768SPavel Emelyanov { 126408c4768SPavel Emelyanov } 1271da177e4SLinus Torvalds #endif 1281da177e4SLinus Torvalds 129eb7e0575SSorin Dumitru static void __ipv6_regen_rndid(struct inet6_dev *idev); 130eb7e0575SSorin Dumitru static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr); 1311da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data); 1321da177e4SLinus Torvalds 1331b34be74SYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev); 1341da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev); 135622c81d5SHannes Frederic Sowa static int ipv6_generate_stable_address(struct in6_addr *addr, 136622c81d5SHannes Frederic Sowa u8 dad_count, 137622c81d5SHannes Frederic Sowa const struct inet6_dev *idev); 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds /* 1401da177e4SLinus Torvalds * Configured unicast address hash table 1411da177e4SLinus Torvalds */ 142c2e21293Sstephen hemminger static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE]; 1435c578aedSstephen hemminger static DEFINE_SPINLOCK(addrconf_hash_lock); 1441da177e4SLinus Torvalds 145c15b1ccaSHannes Frederic Sowa static void addrconf_verify(void); 146c15b1ccaSHannes Frederic Sowa static void addrconf_verify_rtnl(void); 147c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *); 1481da177e4SLinus Torvalds 149c15b1ccaSHannes Frederic Sowa static struct workqueue_struct *addrconf_wq; 150c15b1ccaSHannes Frederic Sowa static DECLARE_DELAYED_WORK(addr_chk_work, addrconf_verify_work); 1511da177e4SLinus Torvalds 1521da177e4SLinus Torvalds static void addrconf_join_anycast(struct inet6_ifaddr *ifp); 1531da177e4SLinus Torvalds static void addrconf_leave_anycast(struct inet6_ifaddr *ifp); 1541da177e4SLinus Torvalds 15593d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, 15675c78500SMoni Shoua unsigned long event); 1571da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how); 1581da177e4SLinus Torvalds 15921caa662SRomain Kuntz static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx, 16021caa662SRomain Kuntz int plen, 16121caa662SRomain Kuntz const struct net_device *dev, 16221caa662SRomain Kuntz u32 flags, u32 noflags); 16321caa662SRomain Kuntz 164cf22f9a2SDavid S. Miller static void addrconf_dad_start(struct inet6_ifaddr *ifp); 165c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w); 1661da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp); 167c5e33bddSYOSHIFUJI Hideaki static void addrconf_dad_run(struct inet6_dev *idev); 1681da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data); 1691da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); 1701da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev, 1731da177e4SLinus Torvalds struct prefix_info *pinfo); 1743e81c6daSDavid S. Miller static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr, 17506bfe655SDaniel Lezcano struct net_device *dev); 1761da177e4SLinus Torvalds 177888c848eSAdrian Bunk static struct ipv6_devconf ipv6_devconf __read_mostly = { 1781da177e4SLinus Torvalds .forwarding = 0, 1791da177e4SLinus Torvalds .hop_limit = IPV6_DEFAULT_HOPLIMIT, 1801da177e4SLinus Torvalds .mtu6 = IPV6_MIN_MTU, 1811da177e4SLinus Torvalds .accept_ra = 1, 1821da177e4SLinus Torvalds .accept_redirects = 1, 1831da177e4SLinus Torvalds .autoconf = 1, 1841da177e4SLinus Torvalds .force_mld_version = 0, 185fc4eba58SHannes Frederic Sowa .mldv1_unsolicited_report_interval = 10 * HZ, 186fc4eba58SHannes Frederic Sowa .mldv2_unsolicited_report_interval = HZ, 1871da177e4SLinus Torvalds .dad_transmits = 1, 1881da177e4SLinus Torvalds .rtr_solicits = MAX_RTR_SOLICITATIONS, 1891da177e4SLinus Torvalds .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, 1901da177e4SLinus Torvalds .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, 1911da177e4SLinus Torvalds .use_tempaddr = 0, 1921da177e4SLinus Torvalds .temp_valid_lft = TEMP_VALID_LIFETIME, 1931da177e4SLinus Torvalds .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, 1941da177e4SLinus Torvalds .regen_max_retry = REGEN_MAX_RETRY, 1951da177e4SLinus Torvalds .max_desync_factor = MAX_DESYNC_FACTOR, 1961da177e4SLinus Torvalds .max_addresses = IPV6_MAX_ADDRESSES, 19765f5c7c1SYOSHIFUJI Hideaki .accept_ra_defrtr = 1, 198d9333196SBen Greear .accept_ra_from_local = 0, 1998013d1d7SHangbin Liu .accept_ra_min_hop_limit= 1, 200c4fd30ebSYOSHIFUJI Hideaki .accept_ra_pinfo = 1, 201930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 202930d6ff2SYOSHIFUJI Hideaki .accept_ra_rtr_pref = 1, 20352e16356SYOSHIFUJI Hideaki .rtr_probe_interval = 60 * HZ, 20409c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 20509c884d4SYOSHIFUJI Hideaki .accept_ra_rt_info_max_plen = 0, 20609c884d4SYOSHIFUJI Hideaki #endif 207930d6ff2SYOSHIFUJI Hideaki #endif 208fbea49e1SYOSHIFUJI Hideaki .proxy_ndp = 0, 2090bcbc926SYOSHIFUJI Hideaki .accept_source_route = 0, /* we do not accept RH0 by default. */ 210778d80beSYOSHIFUJI Hideaki .disable_ipv6 = 0, 2111b34be74SYOSHIFUJI Hideaki .accept_dad = 1, 212b800c3b9SHannes Frederic Sowa .suppress_frag_ndisc = 1, 213c2943f14SHarout Hedeshian .accept_ra_mtu = 1, 2143d1bec99SHannes Frederic Sowa .stable_secret = { 2153d1bec99SHannes Frederic Sowa .initialized = false, 2163985e8a3SErik Kline }, 2173985e8a3SErik Kline .use_oif_addrs_only = 0, 21835103d11SAndy Gospodarek .ignore_routes_with_linkdown = 0, 219f1705ec1SDavid Ahern .keep_addr_on_down = 0, 2201da177e4SLinus Torvalds }; 2211da177e4SLinus Torvalds 222ab32ea5dSBrian Haley static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = { 2231da177e4SLinus Torvalds .forwarding = 0, 2241da177e4SLinus Torvalds .hop_limit = IPV6_DEFAULT_HOPLIMIT, 2251da177e4SLinus Torvalds .mtu6 = IPV6_MIN_MTU, 2261da177e4SLinus Torvalds .accept_ra = 1, 2271da177e4SLinus Torvalds .accept_redirects = 1, 2281da177e4SLinus Torvalds .autoconf = 1, 229fc4eba58SHannes Frederic Sowa .force_mld_version = 0, 230fc4eba58SHannes Frederic Sowa .mldv1_unsolicited_report_interval = 10 * HZ, 231fc4eba58SHannes Frederic Sowa .mldv2_unsolicited_report_interval = HZ, 2321da177e4SLinus Torvalds .dad_transmits = 1, 2331da177e4SLinus Torvalds .rtr_solicits = MAX_RTR_SOLICITATIONS, 2341da177e4SLinus Torvalds .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, 2351da177e4SLinus Torvalds .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, 2361da177e4SLinus Torvalds .use_tempaddr = 0, 2371da177e4SLinus Torvalds .temp_valid_lft = TEMP_VALID_LIFETIME, 2381da177e4SLinus Torvalds .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, 2391da177e4SLinus Torvalds .regen_max_retry = REGEN_MAX_RETRY, 2401da177e4SLinus Torvalds .max_desync_factor = MAX_DESYNC_FACTOR, 2411da177e4SLinus Torvalds .max_addresses = IPV6_MAX_ADDRESSES, 24265f5c7c1SYOSHIFUJI Hideaki .accept_ra_defrtr = 1, 243d9333196SBen Greear .accept_ra_from_local = 0, 2448013d1d7SHangbin Liu .accept_ra_min_hop_limit= 1, 245c4fd30ebSYOSHIFUJI Hideaki .accept_ra_pinfo = 1, 246930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 247930d6ff2SYOSHIFUJI Hideaki .accept_ra_rtr_pref = 1, 24852e16356SYOSHIFUJI Hideaki .rtr_probe_interval = 60 * HZ, 24909c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 25009c884d4SYOSHIFUJI Hideaki .accept_ra_rt_info_max_plen = 0, 25109c884d4SYOSHIFUJI Hideaki #endif 252930d6ff2SYOSHIFUJI Hideaki #endif 253fbea49e1SYOSHIFUJI Hideaki .proxy_ndp = 0, 2540bcbc926SYOSHIFUJI Hideaki .accept_source_route = 0, /* we do not accept RH0 by default. */ 255778d80beSYOSHIFUJI Hideaki .disable_ipv6 = 0, 2561b34be74SYOSHIFUJI Hideaki .accept_dad = 1, 257b800c3b9SHannes Frederic Sowa .suppress_frag_ndisc = 1, 258c2943f14SHarout Hedeshian .accept_ra_mtu = 1, 2593d1bec99SHannes Frederic Sowa .stable_secret = { 2603d1bec99SHannes Frederic Sowa .initialized = false, 2613d1bec99SHannes Frederic Sowa }, 2623985e8a3SErik Kline .use_oif_addrs_only = 0, 26335103d11SAndy Gospodarek .ignore_routes_with_linkdown = 0, 264f1705ec1SDavid Ahern .keep_addr_on_down = 0, 2651da177e4SLinus Torvalds }; 2661da177e4SLinus Torvalds 267f24e3d65SMitsuru Chinen /* Check if a valid qdisc is available */ 26805297949SDavid S. Miller static inline bool addrconf_qdisc_ok(const struct net_device *dev) 269f24e3d65SMitsuru Chinen { 27005297949SDavid S. Miller return !qdisc_tx_is_noop(dev); 271f24e3d65SMitsuru Chinen } 272f24e3d65SMitsuru Chinen 273b7b1bfceSHannes Frederic Sowa static void addrconf_del_rs_timer(struct inet6_dev *idev) 2741da177e4SLinus Torvalds { 275b7b1bfceSHannes Frederic Sowa if (del_timer(&idev->rs_timer)) 276b7b1bfceSHannes Frederic Sowa __in6_dev_put(idev); 277b7b1bfceSHannes Frederic Sowa } 278b7b1bfceSHannes Frederic Sowa 279c15b1ccaSHannes Frederic Sowa static void addrconf_del_dad_work(struct inet6_ifaddr *ifp) 280b7b1bfceSHannes Frederic Sowa { 281c15b1ccaSHannes Frederic Sowa if (cancel_delayed_work(&ifp->dad_work)) 2821da177e4SLinus Torvalds __in6_ifa_put(ifp); 2831da177e4SLinus Torvalds } 2841da177e4SLinus Torvalds 285b7b1bfceSHannes Frederic Sowa static void addrconf_mod_rs_timer(struct inet6_dev *idev, 2861da177e4SLinus Torvalds unsigned long when) 2871da177e4SLinus Torvalds { 288b7b1bfceSHannes Frederic Sowa if (!timer_pending(&idev->rs_timer)) 289b7b1bfceSHannes Frederic Sowa in6_dev_hold(idev); 290b7b1bfceSHannes Frederic Sowa mod_timer(&idev->rs_timer, jiffies + when); 2911da177e4SLinus Torvalds } 292b7b1bfceSHannes Frederic Sowa 293c15b1ccaSHannes Frederic Sowa static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp, 294c15b1ccaSHannes Frederic Sowa unsigned long delay) 295b7b1bfceSHannes Frederic Sowa { 296c15b1ccaSHannes Frederic Sowa if (!delayed_work_pending(&ifp->dad_work)) 297b7b1bfceSHannes Frederic Sowa in6_ifa_hold(ifp); 298c15b1ccaSHannes Frederic Sowa mod_delayed_work(addrconf_wq, &ifp->dad_work, delay); 2991da177e4SLinus Torvalds } 3001da177e4SLinus Torvalds 3017f7d9a6bSHerbert Xu static int snmp6_alloc_dev(struct inet6_dev *idev) 3027f7d9a6bSHerbert Xu { 303827da44cSJohn Stultz int i; 304827da44cSJohn Stultz 305698365faSWANG Cong idev->stats.ipv6 = alloc_percpu(struct ipstats_mib); 306698365faSWANG Cong if (!idev->stats.ipv6) 3077f7d9a6bSHerbert Xu goto err_ip; 308827da44cSJohn Stultz 309827da44cSJohn Stultz for_each_possible_cpu(i) { 310827da44cSJohn Stultz struct ipstats_mib *addrconf_stats; 311698365faSWANG Cong addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i); 312827da44cSJohn Stultz u64_stats_init(&addrconf_stats->syncp); 313827da44cSJohn Stultz } 314827da44cSJohn Stultz 315827da44cSJohn Stultz 316be281e55SEric Dumazet idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device), 317be281e55SEric Dumazet GFP_KERNEL); 318be281e55SEric Dumazet if (!idev->stats.icmpv6dev) 3197f7d9a6bSHerbert Xu goto err_icmp; 320be281e55SEric Dumazet idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device), 321be281e55SEric Dumazet GFP_KERNEL); 322be281e55SEric Dumazet if (!idev->stats.icmpv6msgdev) 32314878f75SDavid L Stevens goto err_icmpmsg; 3247f7d9a6bSHerbert Xu 3257f7d9a6bSHerbert Xu return 0; 3267f7d9a6bSHerbert Xu 32714878f75SDavid L Stevens err_icmpmsg: 328be281e55SEric Dumazet kfree(idev->stats.icmpv6dev); 3297f7d9a6bSHerbert Xu err_icmp: 330698365faSWANG Cong free_percpu(idev->stats.ipv6); 3317f7d9a6bSHerbert Xu err_ip: 332aaf70ec7SPavel Emelyanov return -ENOMEM; 3337f7d9a6bSHerbert Xu } 3347f7d9a6bSHerbert Xu 3351da177e4SLinus Torvalds static struct inet6_dev *ipv6_add_dev(struct net_device *dev) 3361da177e4SLinus Torvalds { 3371da177e4SLinus Torvalds struct inet6_dev *ndev; 338a317a2f1SWANG Cong int err = -ENOMEM; 3391da177e4SLinus Torvalds 3401da177e4SLinus Torvalds ASSERT_RTNL(); 3411da177e4SLinus Torvalds 3421da177e4SLinus Torvalds if (dev->mtu < IPV6_MIN_MTU) 343a317a2f1SWANG Cong return ERR_PTR(-EINVAL); 3441da177e4SLinus Torvalds 345322f74a4SIngo Oeser ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL); 34663159f29SIan Morris if (!ndev) 347a317a2f1SWANG Cong return ERR_PTR(err); 3481da177e4SLinus Torvalds 3491da177e4SLinus Torvalds rwlock_init(&ndev->lock); 3501da177e4SLinus Torvalds ndev->dev = dev; 351502a2ffdSstephen hemminger INIT_LIST_HEAD(&ndev->addr_list); 352b7b1bfceSHannes Frederic Sowa setup_timer(&ndev->rs_timer, addrconf_rs_timer, 353b7b1bfceSHannes Frederic Sowa (unsigned long)ndev); 354c346dca1SYOSHIFUJI Hideaki memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf)); 3559b29c696SHannes Frederic Sowa 3569b29c696SHannes Frederic Sowa if (ndev->cnf.stable_secret.initialized) 3579b29c696SHannes Frederic Sowa ndev->addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY; 3589b29c696SHannes Frederic Sowa else 3599b29c696SHannes Frederic Sowa ndev->addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64; 3609b29c696SHannes Frederic Sowa 3611da177e4SLinus Torvalds ndev->cnf.mtu6 = dev->mtu; 3621da177e4SLinus Torvalds ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl); 36363159f29SIan Morris if (!ndev->nd_parms) { 3641da177e4SLinus Torvalds kfree(ndev); 365a317a2f1SWANG Cong return ERR_PTR(err); 3661da177e4SLinus Torvalds } 3670187bdfbSBen Hutchings if (ndev->cnf.forwarding) 3680187bdfbSBen Hutchings dev_disable_lro(dev); 3691da177e4SLinus Torvalds /* We refer to the device */ 3701da177e4SLinus Torvalds dev_hold(dev); 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds if (snmp6_alloc_dev(ndev) < 0) { 373ba3542e1Sdingtianhong ADBG(KERN_WARNING 374f3213831SJoe Perches "%s: cannot allocate memory for statistics; dev=%s.\n", 375ba3542e1Sdingtianhong __func__, dev->name); 3761da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, ndev->nd_parms); 3778603e33dSRoy Li dev_put(dev); 3788603e33dSRoy Li kfree(ndev); 379a317a2f1SWANG Cong return ERR_PTR(err); 3801da177e4SLinus Torvalds } 3811da177e4SLinus Torvalds 3821da177e4SLinus Torvalds if (snmp6_register_dev(ndev) < 0) { 383ba3542e1Sdingtianhong ADBG(KERN_WARNING 384f3213831SJoe Perches "%s: cannot create /proc/net/dev_snmp6/%s\n", 385ba3542e1Sdingtianhong __func__, dev->name); 386a317a2f1SWANG Cong goto err_release; 3871da177e4SLinus Torvalds } 3881da177e4SLinus Torvalds 3891da177e4SLinus Torvalds /* One reference from device. We must do this before 3901da177e4SLinus Torvalds * we invoke __ipv6_regen_rndid(). 3911da177e4SLinus Torvalds */ 3921da177e4SLinus Torvalds in6_dev_hold(ndev); 3931da177e4SLinus Torvalds 3941b34be74SYOSHIFUJI Hideaki if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) 3951b34be74SYOSHIFUJI Hideaki ndev->cnf.accept_dad = -1; 3961b34be74SYOSHIFUJI Hideaki 39707a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 398c7dc89c0SFred L. Templin if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) { 399f3213831SJoe Perches pr_info("%s: Disabled Multicast RS\n", dev->name); 400c7dc89c0SFred L. Templin ndev->cnf.rtr_solicits = 0; 401c7dc89c0SFred L. Templin } 402b077d7abSYOSHIFUJI Hideaki #endif 403b077d7abSYOSHIFUJI Hideaki 404372e6c8fSstephen hemminger INIT_LIST_HEAD(&ndev->tempaddr_list); 4051da177e4SLinus Torvalds setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev); 4061da177e4SLinus Torvalds if ((dev->flags&IFF_LOOPBACK) || 4071da177e4SLinus Torvalds dev->type == ARPHRD_TUNNEL || 4089625ed72SYOSHIFUJI Hideaki dev->type == ARPHRD_TUNNEL6 || 4091da177e4SLinus Torvalds dev->type == ARPHRD_SIT || 4101da177e4SLinus Torvalds dev->type == ARPHRD_NONE) { 4110be669bbSJoerg Roedel ndev->cnf.use_tempaddr = -1; 4121da177e4SLinus Torvalds } else { 4131da177e4SLinus Torvalds in6_dev_hold(ndev); 4141da177e4SLinus Torvalds ipv6_regen_rndid((unsigned long) ndev); 4151da177e4SLinus Torvalds } 4165d9efa7eSDavid S. Miller 417914faa14SDaniel Borkmann ndev->token = in6addr_any; 4181da177e4SLinus Torvalds 419f24e3d65SMitsuru Chinen if (netif_running(dev) && addrconf_qdisc_ok(dev)) 42053aadcc9SHerbert Xu ndev->if_flags |= IF_READY; 42153aadcc9SHerbert Xu 4221da177e4SLinus Torvalds ipv6_mc_init_dev(ndev); 4231da177e4SLinus Torvalds ndev->tstamp = jiffies; 424a317a2f1SWANG Cong err = addrconf_sysctl_register(ndev); 425a317a2f1SWANG Cong if (err) { 426a317a2f1SWANG Cong ipv6_mc_destroy_dev(ndev); 427a317a2f1SWANG Cong del_timer(&ndev->regen_timer); 4282a189f9eSSabrina Dubroca snmp6_unregister_dev(ndev); 429a317a2f1SWANG Cong goto err_release; 430a317a2f1SWANG Cong } 43130c4cf57SDavid L Stevens /* protected by rtnl_lock */ 432cf778b00SEric Dumazet rcu_assign_pointer(dev->ip6_ptr, ndev); 433d88ae4ccSYOSHIFUJI Hideaki 4342c5e8933SHannes Frederic Sowa /* Join interface-local all-node multicast group */ 4352c5e8933SHannes Frederic Sowa ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes); 4362c5e8933SHannes Frederic Sowa 437d88ae4ccSYOSHIFUJI Hideaki /* Join all-node multicast group */ 438f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes); 439d88ae4ccSYOSHIFUJI Hideaki 440d6ddef9eSLi Wei /* Join all-router multicast group if forwarding is set */ 4418b2aaedeSLi Wei if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST)) 442d6ddef9eSLi Wei ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters); 443d6ddef9eSLi Wei 4441da177e4SLinus Torvalds return ndev; 445a317a2f1SWANG Cong 446a317a2f1SWANG Cong err_release: 447a317a2f1SWANG Cong neigh_parms_release(&nd_tbl, ndev->nd_parms); 448a317a2f1SWANG Cong ndev->dead = 1; 449a317a2f1SWANG Cong in6_dev_finish_destroy(ndev); 450a317a2f1SWANG Cong return ERR_PTR(err); 4511da177e4SLinus Torvalds } 4521da177e4SLinus Torvalds 453cee89473SYOSHIFUJI Hideaki static struct inet6_dev *ipv6_find_idev(struct net_device *dev) 4541da177e4SLinus Torvalds { 4551da177e4SLinus Torvalds struct inet6_dev *idev; 4561da177e4SLinus Torvalds 4571da177e4SLinus Torvalds ASSERT_RTNL(); 4581da177e4SLinus Torvalds 459e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 460e21e8467SStephen Hemminger if (!idev) { 461e21e8467SStephen Hemminger idev = ipv6_add_dev(dev); 462a317a2f1SWANG Cong if (IS_ERR(idev)) 4631da177e4SLinus Torvalds return NULL; 4641da177e4SLinus Torvalds } 465c5e33bddSYOSHIFUJI Hideaki 4661da177e4SLinus Torvalds if (dev->flags&IFF_UP) 4671da177e4SLinus Torvalds ipv6_mc_up(idev); 4681da177e4SLinus Torvalds return idev; 4691da177e4SLinus Torvalds } 4701da177e4SLinus Torvalds 471f3a1bfb1SNicolas Dichtel static int inet6_netconf_msgsize_devconf(int type) 472f3a1bfb1SNicolas Dichtel { 473f3a1bfb1SNicolas Dichtel int size = NLMSG_ALIGN(sizeof(struct netconfmsg)) 474f3a1bfb1SNicolas Dichtel + nla_total_size(4); /* NETCONFA_IFINDEX */ 475136ba622SZhang Shengju bool all = false; 476f3a1bfb1SNicolas Dichtel 477136ba622SZhang Shengju if (type == NETCONFA_ALL) 478136ba622SZhang Shengju all = true; 479136ba622SZhang Shengju 480136ba622SZhang Shengju if (all || type == NETCONFA_FORWARDING) 481f3a1bfb1SNicolas Dichtel size += nla_total_size(4); 482b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE 483136ba622SZhang Shengju if (all || type == NETCONFA_MC_FORWARDING) 484d67b8c61SNicolas Dichtel size += nla_total_size(4); 485b1afce95SDavid S. Miller #endif 486136ba622SZhang Shengju if (all || type == NETCONFA_PROXY_NEIGH) 487c92d5491Sstephen hemminger size += nla_total_size(4); 488f3a1bfb1SNicolas Dichtel 489136ba622SZhang Shengju if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) 49035103d11SAndy Gospodarek size += nla_total_size(4); 49135103d11SAndy Gospodarek 492f3a1bfb1SNicolas Dichtel return size; 493f3a1bfb1SNicolas Dichtel } 494f3a1bfb1SNicolas Dichtel 495f3a1bfb1SNicolas Dichtel static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex, 496f3a1bfb1SNicolas Dichtel struct ipv6_devconf *devconf, u32 portid, 497f3a1bfb1SNicolas Dichtel u32 seq, int event, unsigned int flags, 498f3a1bfb1SNicolas Dichtel int type) 499f3a1bfb1SNicolas Dichtel { 500f3a1bfb1SNicolas Dichtel struct nlmsghdr *nlh; 501f3a1bfb1SNicolas Dichtel struct netconfmsg *ncm; 502136ba622SZhang Shengju bool all = false; 503f3a1bfb1SNicolas Dichtel 504f3a1bfb1SNicolas Dichtel nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg), 505f3a1bfb1SNicolas Dichtel flags); 50663159f29SIan Morris if (!nlh) 507f3a1bfb1SNicolas Dichtel return -EMSGSIZE; 508f3a1bfb1SNicolas Dichtel 509136ba622SZhang Shengju if (type == NETCONFA_ALL) 510136ba622SZhang Shengju all = true; 511136ba622SZhang Shengju 512f3a1bfb1SNicolas Dichtel ncm = nlmsg_data(nlh); 513f3a1bfb1SNicolas Dichtel ncm->ncm_family = AF_INET6; 514f3a1bfb1SNicolas Dichtel 515f3a1bfb1SNicolas Dichtel if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0) 516f3a1bfb1SNicolas Dichtel goto nla_put_failure; 517f3a1bfb1SNicolas Dichtel 518136ba622SZhang Shengju if ((all || type == NETCONFA_FORWARDING) && 519f3a1bfb1SNicolas Dichtel nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0) 520f3a1bfb1SNicolas Dichtel goto nla_put_failure; 521b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE 522136ba622SZhang Shengju if ((all || type == NETCONFA_MC_FORWARDING) && 523d67b8c61SNicolas Dichtel nla_put_s32(skb, NETCONFA_MC_FORWARDING, 524d67b8c61SNicolas Dichtel devconf->mc_forwarding) < 0) 525d67b8c61SNicolas Dichtel goto nla_put_failure; 526b1afce95SDavid S. Miller #endif 527136ba622SZhang Shengju if ((all || type == NETCONFA_PROXY_NEIGH) && 528c92d5491Sstephen hemminger nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0) 529c92d5491Sstephen hemminger goto nla_put_failure; 530c92d5491Sstephen hemminger 531136ba622SZhang Shengju if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) && 53235103d11SAndy Gospodarek nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 53335103d11SAndy Gospodarek devconf->ignore_routes_with_linkdown) < 0) 53435103d11SAndy Gospodarek goto nla_put_failure; 53535103d11SAndy Gospodarek 536053c095aSJohannes Berg nlmsg_end(skb, nlh); 537053c095aSJohannes Berg return 0; 538f3a1bfb1SNicolas Dichtel 539f3a1bfb1SNicolas Dichtel nla_put_failure: 540f3a1bfb1SNicolas Dichtel nlmsg_cancel(skb, nlh); 541f3a1bfb1SNicolas Dichtel return -EMSGSIZE; 542f3a1bfb1SNicolas Dichtel } 543f3a1bfb1SNicolas Dichtel 544d67b8c61SNicolas Dichtel void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex, 545f3a1bfb1SNicolas Dichtel struct ipv6_devconf *devconf) 546f3a1bfb1SNicolas Dichtel { 547f3a1bfb1SNicolas Dichtel struct sk_buff *skb; 548f3a1bfb1SNicolas Dichtel int err = -ENOBUFS; 549f3a1bfb1SNicolas Dichtel 550f3a1bfb1SNicolas Dichtel skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_ATOMIC); 55163159f29SIan Morris if (!skb) 552f3a1bfb1SNicolas Dichtel goto errout; 553f3a1bfb1SNicolas Dichtel 554f3a1bfb1SNicolas Dichtel err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0, 555f3a1bfb1SNicolas Dichtel RTM_NEWNETCONF, 0, type); 556f3a1bfb1SNicolas Dichtel if (err < 0) { 557f3a1bfb1SNicolas Dichtel /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */ 558f3a1bfb1SNicolas Dichtel WARN_ON(err == -EMSGSIZE); 559f3a1bfb1SNicolas Dichtel kfree_skb(skb); 560f3a1bfb1SNicolas Dichtel goto errout; 561f3a1bfb1SNicolas Dichtel } 562f3a1bfb1SNicolas Dichtel rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_ATOMIC); 563f3a1bfb1SNicolas Dichtel return; 564f3a1bfb1SNicolas Dichtel errout: 565f3a1bfb1SNicolas Dichtel rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err); 566f3a1bfb1SNicolas Dichtel } 567f3a1bfb1SNicolas Dichtel 56876f8f6cbSNicolas Dichtel static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = { 56976f8f6cbSNicolas Dichtel [NETCONFA_IFINDEX] = { .len = sizeof(int) }, 57076f8f6cbSNicolas Dichtel [NETCONFA_FORWARDING] = { .len = sizeof(int) }, 571c92d5491Sstephen hemminger [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) }, 57235103d11SAndy Gospodarek [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) }, 57376f8f6cbSNicolas Dichtel }; 57476f8f6cbSNicolas Dichtel 57576f8f6cbSNicolas Dichtel static int inet6_netconf_get_devconf(struct sk_buff *in_skb, 576661d2967SThomas Graf struct nlmsghdr *nlh) 57776f8f6cbSNicolas Dichtel { 57876f8f6cbSNicolas Dichtel struct net *net = sock_net(in_skb->sk); 57976f8f6cbSNicolas Dichtel struct nlattr *tb[NETCONFA_MAX+1]; 58076f8f6cbSNicolas Dichtel struct netconfmsg *ncm; 58176f8f6cbSNicolas Dichtel struct sk_buff *skb; 58276f8f6cbSNicolas Dichtel struct ipv6_devconf *devconf; 58376f8f6cbSNicolas Dichtel struct inet6_dev *in6_dev; 58476f8f6cbSNicolas Dichtel struct net_device *dev; 58576f8f6cbSNicolas Dichtel int ifindex; 58676f8f6cbSNicolas Dichtel int err; 58776f8f6cbSNicolas Dichtel 58876f8f6cbSNicolas Dichtel err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX, 58976f8f6cbSNicolas Dichtel devconf_ipv6_policy); 59076f8f6cbSNicolas Dichtel if (err < 0) 59176f8f6cbSNicolas Dichtel goto errout; 59276f8f6cbSNicolas Dichtel 593a97eb33fSAnton Protopopov err = -EINVAL; 59476f8f6cbSNicolas Dichtel if (!tb[NETCONFA_IFINDEX]) 59576f8f6cbSNicolas Dichtel goto errout; 59676f8f6cbSNicolas Dichtel 59776f8f6cbSNicolas Dichtel ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]); 59876f8f6cbSNicolas Dichtel switch (ifindex) { 59976f8f6cbSNicolas Dichtel case NETCONFA_IFINDEX_ALL: 60076f8f6cbSNicolas Dichtel devconf = net->ipv6.devconf_all; 60176f8f6cbSNicolas Dichtel break; 60276f8f6cbSNicolas Dichtel case NETCONFA_IFINDEX_DEFAULT: 60376f8f6cbSNicolas Dichtel devconf = net->ipv6.devconf_dflt; 60476f8f6cbSNicolas Dichtel break; 60576f8f6cbSNicolas Dichtel default: 60676f8f6cbSNicolas Dichtel dev = __dev_get_by_index(net, ifindex); 60763159f29SIan Morris if (!dev) 60876f8f6cbSNicolas Dichtel goto errout; 60976f8f6cbSNicolas Dichtel in6_dev = __in6_dev_get(dev); 61063159f29SIan Morris if (!in6_dev) 61176f8f6cbSNicolas Dichtel goto errout; 61276f8f6cbSNicolas Dichtel devconf = &in6_dev->cnf; 61376f8f6cbSNicolas Dichtel break; 61476f8f6cbSNicolas Dichtel } 61576f8f6cbSNicolas Dichtel 61676f8f6cbSNicolas Dichtel err = -ENOBUFS; 617136ba622SZhang Shengju skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_ATOMIC); 61863159f29SIan Morris if (!skb) 61976f8f6cbSNicolas Dichtel goto errout; 62076f8f6cbSNicolas Dichtel 62176f8f6cbSNicolas Dichtel err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 62276f8f6cbSNicolas Dichtel NETLINK_CB(in_skb).portid, 62376f8f6cbSNicolas Dichtel nlh->nlmsg_seq, RTM_NEWNETCONF, 0, 624136ba622SZhang Shengju NETCONFA_ALL); 62576f8f6cbSNicolas Dichtel if (err < 0) { 62676f8f6cbSNicolas Dichtel /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */ 62776f8f6cbSNicolas Dichtel WARN_ON(err == -EMSGSIZE); 62876f8f6cbSNicolas Dichtel kfree_skb(skb); 62976f8f6cbSNicolas Dichtel goto errout; 63076f8f6cbSNicolas Dichtel } 63176f8f6cbSNicolas Dichtel err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 63276f8f6cbSNicolas Dichtel errout: 63376f8f6cbSNicolas Dichtel return err; 63476f8f6cbSNicolas Dichtel } 63576f8f6cbSNicolas Dichtel 6367a674200SNicolas Dichtel static int inet6_netconf_dump_devconf(struct sk_buff *skb, 6377a674200SNicolas Dichtel struct netlink_callback *cb) 6387a674200SNicolas Dichtel { 6397a674200SNicolas Dichtel struct net *net = sock_net(skb->sk); 6407a674200SNicolas Dichtel int h, s_h; 6417a674200SNicolas Dichtel int idx, s_idx; 6427a674200SNicolas Dichtel struct net_device *dev; 6437a674200SNicolas Dichtel struct inet6_dev *idev; 6447a674200SNicolas Dichtel struct hlist_head *head; 6457a674200SNicolas Dichtel 6467a674200SNicolas Dichtel s_h = cb->args[0]; 6477a674200SNicolas Dichtel s_idx = idx = cb->args[1]; 6487a674200SNicolas Dichtel 6497a674200SNicolas Dichtel for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 6507a674200SNicolas Dichtel idx = 0; 6517a674200SNicolas Dichtel head = &net->dev_index_head[h]; 6527a674200SNicolas Dichtel rcu_read_lock(); 65363998ac2SNicolas Dichtel cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ 65463998ac2SNicolas Dichtel net->dev_base_seq; 6557a674200SNicolas Dichtel hlist_for_each_entry_rcu(dev, head, index_hlist) { 6567a674200SNicolas Dichtel if (idx < s_idx) 6577a674200SNicolas Dichtel goto cont; 6587a674200SNicolas Dichtel idev = __in6_dev_get(dev); 6597a674200SNicolas Dichtel if (!idev) 6607a674200SNicolas Dichtel goto cont; 6617a674200SNicolas Dichtel 6627a674200SNicolas Dichtel if (inet6_netconf_fill_devconf(skb, dev->ifindex, 6637a674200SNicolas Dichtel &idev->cnf, 6647a674200SNicolas Dichtel NETLINK_CB(cb->skb).portid, 6657a674200SNicolas Dichtel cb->nlh->nlmsg_seq, 6667a674200SNicolas Dichtel RTM_NEWNETCONF, 6677a674200SNicolas Dichtel NLM_F_MULTI, 668136ba622SZhang Shengju NETCONFA_ALL) < 0) { 6697a674200SNicolas Dichtel rcu_read_unlock(); 6707a674200SNicolas Dichtel goto done; 6717a674200SNicolas Dichtel } 67263998ac2SNicolas Dichtel nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 6737a674200SNicolas Dichtel cont: 6747a674200SNicolas Dichtel idx++; 6757a674200SNicolas Dichtel } 6767a674200SNicolas Dichtel rcu_read_unlock(); 6777a674200SNicolas Dichtel } 6787a674200SNicolas Dichtel if (h == NETDEV_HASHENTRIES) { 6797a674200SNicolas Dichtel if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL, 6807a674200SNicolas Dichtel net->ipv6.devconf_all, 6817a674200SNicolas Dichtel NETLINK_CB(cb->skb).portid, 6827a674200SNicolas Dichtel cb->nlh->nlmsg_seq, 6837a674200SNicolas Dichtel RTM_NEWNETCONF, NLM_F_MULTI, 684136ba622SZhang Shengju NETCONFA_ALL) < 0) 6857a674200SNicolas Dichtel goto done; 6867a674200SNicolas Dichtel else 6877a674200SNicolas Dichtel h++; 6887a674200SNicolas Dichtel } 6897a674200SNicolas Dichtel if (h == NETDEV_HASHENTRIES + 1) { 6907a674200SNicolas Dichtel if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT, 6917a674200SNicolas Dichtel net->ipv6.devconf_dflt, 6927a674200SNicolas Dichtel NETLINK_CB(cb->skb).portid, 6937a674200SNicolas Dichtel cb->nlh->nlmsg_seq, 6947a674200SNicolas Dichtel RTM_NEWNETCONF, NLM_F_MULTI, 695136ba622SZhang Shengju NETCONFA_ALL) < 0) 6967a674200SNicolas Dichtel goto done; 6977a674200SNicolas Dichtel else 6987a674200SNicolas Dichtel h++; 6997a674200SNicolas Dichtel } 7007a674200SNicolas Dichtel done: 7017a674200SNicolas Dichtel cb->args[0] = h; 7027a674200SNicolas Dichtel cb->args[1] = idx; 7037a674200SNicolas Dichtel 7047a674200SNicolas Dichtel return skb->len; 7057a674200SNicolas Dichtel } 7067a674200SNicolas Dichtel 7071da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 7081da177e4SLinus Torvalds static void dev_forward_change(struct inet6_dev *idev) 7091da177e4SLinus Torvalds { 7101da177e4SLinus Torvalds struct net_device *dev; 7111da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 7121da177e4SLinus Torvalds 7131da177e4SLinus Torvalds if (!idev) 7141da177e4SLinus Torvalds return; 7151da177e4SLinus Torvalds dev = idev->dev; 7160187bdfbSBen Hutchings if (idev->cnf.forwarding) 7170187bdfbSBen Hutchings dev_disable_lro(dev); 7181a940835SAmerigo Wang if (dev->flags & IFF_MULTICAST) { 7192c5e8933SHannes Frederic Sowa if (idev->cnf.forwarding) { 720f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters); 7212c5e8933SHannes Frederic Sowa ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters); 7222c5e8933SHannes Frederic Sowa ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters); 7232c5e8933SHannes Frederic Sowa } else { 724f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters); 7252c5e8933SHannes Frederic Sowa ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters); 7262c5e8933SHannes Frederic Sowa ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters); 7272c5e8933SHannes Frederic Sowa } 7281da177e4SLinus Torvalds } 729502a2ffdSstephen hemminger 730502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 7312c12a74cSMichal Wrobel if (ifa->flags&IFA_F_TENTATIVE) 7322c12a74cSMichal Wrobel continue; 7331da177e4SLinus Torvalds if (idev->cnf.forwarding) 7341da177e4SLinus Torvalds addrconf_join_anycast(ifa); 7351da177e4SLinus Torvalds else 7361da177e4SLinus Torvalds addrconf_leave_anycast(ifa); 7371da177e4SLinus Torvalds } 738f3a1bfb1SNicolas Dichtel inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING, 739f3a1bfb1SNicolas Dichtel dev->ifindex, &idev->cnf); 7401da177e4SLinus Torvalds } 7411da177e4SLinus Torvalds 7421da177e4SLinus Torvalds 743e186932bSPavel Emelyanov static void addrconf_forward_change(struct net *net, __s32 newf) 7441da177e4SLinus Torvalds { 7451da177e4SLinus Torvalds struct net_device *dev; 7461da177e4SLinus Torvalds struct inet6_dev *idev; 7471da177e4SLinus Torvalds 7484acd4945SBen Hutchings for_each_netdev(net, dev) { 7491da177e4SLinus Torvalds idev = __in6_dev_get(dev); 7501da177e4SLinus Torvalds if (idev) { 751e186932bSPavel Emelyanov int changed = (!idev->cnf.forwarding) ^ (!newf); 752e186932bSPavel Emelyanov idev->cnf.forwarding = newf; 7531da177e4SLinus Torvalds if (changed) 7541da177e4SLinus Torvalds dev_forward_change(idev); 7551da177e4SLinus Torvalds } 7561da177e4SLinus Torvalds } 7571da177e4SLinus Torvalds } 758c8fecf22SPavel Emelyanov 759013d97e9SFrancesco Ruggeri static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf) 760c8fecf22SPavel Emelyanov { 761bff16c2fSPavel Emelyanov struct net *net; 762013d97e9SFrancesco Ruggeri int old; 763013d97e9SFrancesco Ruggeri 764013d97e9SFrancesco Ruggeri if (!rtnl_trylock()) 765013d97e9SFrancesco Ruggeri return restart_syscall(); 766bff16c2fSPavel Emelyanov 767bff16c2fSPavel Emelyanov net = (struct net *)table->extra2; 768013d97e9SFrancesco Ruggeri old = *p; 769013d97e9SFrancesco Ruggeri *p = newf; 770c8fecf22SPavel Emelyanov 771013d97e9SFrancesco Ruggeri if (p == &net->ipv6.devconf_dflt->forwarding) { 772f3a1bfb1SNicolas Dichtel if ((!newf) ^ (!old)) 773f3a1bfb1SNicolas Dichtel inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING, 774f3a1bfb1SNicolas Dichtel NETCONFA_IFINDEX_DEFAULT, 775f3a1bfb1SNicolas Dichtel net->ipv6.devconf_dflt); 776013d97e9SFrancesco Ruggeri rtnl_unlock(); 777013d97e9SFrancesco Ruggeri return 0; 77888af182eSEric W. Biederman } 779b325fddbSStephen Hemminger 780e186932bSPavel Emelyanov if (p == &net->ipv6.devconf_all->forwarding) { 781e186932bSPavel Emelyanov net->ipv6.devconf_dflt->forwarding = newf; 782e186932bSPavel Emelyanov addrconf_forward_change(net, newf); 783f3a1bfb1SNicolas Dichtel if ((!newf) ^ (!old)) 784f3a1bfb1SNicolas Dichtel inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING, 785f3a1bfb1SNicolas Dichtel NETCONFA_IFINDEX_ALL, 786f3a1bfb1SNicolas Dichtel net->ipv6.devconf_all); 787013d97e9SFrancesco Ruggeri } else if ((!newf) ^ (!old)) 788c8fecf22SPavel Emelyanov dev_forward_change((struct inet6_dev *)table->extra1); 7890187bdfbSBen Hutchings rtnl_unlock(); 790c8fecf22SPavel Emelyanov 791013d97e9SFrancesco Ruggeri if (newf) 7927b4da532SDaniel Lezcano rt6_purge_dflt_routers(net); 793b325fddbSStephen Hemminger return 1; 794c8fecf22SPavel Emelyanov } 79535103d11SAndy Gospodarek 79635103d11SAndy Gospodarek static void addrconf_linkdown_change(struct net *net, __s32 newf) 79735103d11SAndy Gospodarek { 79835103d11SAndy Gospodarek struct net_device *dev; 79935103d11SAndy Gospodarek struct inet6_dev *idev; 80035103d11SAndy Gospodarek 80135103d11SAndy Gospodarek for_each_netdev(net, dev) { 80235103d11SAndy Gospodarek idev = __in6_dev_get(dev); 80335103d11SAndy Gospodarek if (idev) { 80435103d11SAndy Gospodarek int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf); 80535103d11SAndy Gospodarek 80635103d11SAndy Gospodarek idev->cnf.ignore_routes_with_linkdown = newf; 80735103d11SAndy Gospodarek if (changed) 80835103d11SAndy Gospodarek inet6_netconf_notify_devconf(dev_net(dev), 80935103d11SAndy Gospodarek NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 81035103d11SAndy Gospodarek dev->ifindex, 81135103d11SAndy Gospodarek &idev->cnf); 81235103d11SAndy Gospodarek } 81335103d11SAndy Gospodarek } 81435103d11SAndy Gospodarek } 81535103d11SAndy Gospodarek 81635103d11SAndy Gospodarek static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf) 81735103d11SAndy Gospodarek { 81835103d11SAndy Gospodarek struct net *net; 81935103d11SAndy Gospodarek int old; 82035103d11SAndy Gospodarek 82135103d11SAndy Gospodarek if (!rtnl_trylock()) 82235103d11SAndy Gospodarek return restart_syscall(); 82335103d11SAndy Gospodarek 82435103d11SAndy Gospodarek net = (struct net *)table->extra2; 82535103d11SAndy Gospodarek old = *p; 82635103d11SAndy Gospodarek *p = newf; 82735103d11SAndy Gospodarek 82835103d11SAndy Gospodarek if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) { 82935103d11SAndy Gospodarek if ((!newf) ^ (!old)) 83035103d11SAndy Gospodarek inet6_netconf_notify_devconf(net, 83135103d11SAndy Gospodarek NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 83235103d11SAndy Gospodarek NETCONFA_IFINDEX_DEFAULT, 83335103d11SAndy Gospodarek net->ipv6.devconf_dflt); 83435103d11SAndy Gospodarek rtnl_unlock(); 83535103d11SAndy Gospodarek return 0; 83635103d11SAndy Gospodarek } 83735103d11SAndy Gospodarek 83835103d11SAndy Gospodarek if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) { 83935103d11SAndy Gospodarek net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf; 84035103d11SAndy Gospodarek addrconf_linkdown_change(net, newf); 84135103d11SAndy Gospodarek if ((!newf) ^ (!old)) 84235103d11SAndy Gospodarek inet6_netconf_notify_devconf(net, 84335103d11SAndy Gospodarek NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 84435103d11SAndy Gospodarek NETCONFA_IFINDEX_ALL, 84535103d11SAndy Gospodarek net->ipv6.devconf_all); 84635103d11SAndy Gospodarek } 84735103d11SAndy Gospodarek rtnl_unlock(); 84835103d11SAndy Gospodarek 84935103d11SAndy Gospodarek return 1; 85035103d11SAndy Gospodarek } 85135103d11SAndy Gospodarek 8521da177e4SLinus Torvalds #endif 8531da177e4SLinus Torvalds 8545c578aedSstephen hemminger /* Nobody refers to this ifaddr, destroy it */ 8551da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp) 8561da177e4SLinus Torvalds { 857c2e21293Sstephen hemminger WARN_ON(!hlist_unhashed(&ifp->addr_lst)); 858547b792cSIlpo Järvinen 8591da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG 86091df42beSJoe Perches pr_debug("%s\n", __func__); 8611da177e4SLinus Torvalds #endif 8621da177e4SLinus Torvalds 8631da177e4SLinus Torvalds in6_dev_put(ifp->idev); 8641da177e4SLinus Torvalds 865c15b1ccaSHannes Frederic Sowa if (cancel_delayed_work(&ifp->dad_work)) 866c15b1ccaSHannes Frederic Sowa pr_notice("delayed DAD work was pending while freeing ifa=%p\n", 867c15b1ccaSHannes Frederic Sowa ifp); 8681da177e4SLinus Torvalds 869e9d3e084SHerbert Xu if (ifp->state != INET6_IFADDR_STATE_DEAD) { 870f3213831SJoe Perches pr_warn("Freeing alive inet6 address %p\n", ifp); 8711da177e4SLinus Torvalds return; 8721da177e4SLinus Torvalds } 87394e187c0SAmerigo Wang ip6_rt_put(ifp->rt); 8741da177e4SLinus Torvalds 875e5785985SLai Jiangshan kfree_rcu(ifp, rcu); 8761da177e4SLinus Torvalds } 8771da177e4SLinus Torvalds 878e55ffac6SBrian Haley static void 879e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp) 880e55ffac6SBrian Haley { 881502a2ffdSstephen hemminger struct list_head *p; 8828a6ce0c0SYOSHIFUJI Hideaki int ifp_scope = ipv6_addr_src_scope(&ifp->addr); 883e55ffac6SBrian Haley 884e55ffac6SBrian Haley /* 885e55ffac6SBrian Haley * Each device address list is sorted in order of scope - 886e55ffac6SBrian Haley * global before linklocal. 887e55ffac6SBrian Haley */ 888502a2ffdSstephen hemminger list_for_each(p, &idev->addr_list) { 889502a2ffdSstephen hemminger struct inet6_ifaddr *ifa 890502a2ffdSstephen hemminger = list_entry(p, struct inet6_ifaddr, if_list); 8918a6ce0c0SYOSHIFUJI Hideaki if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr)) 892e55ffac6SBrian Haley break; 893e55ffac6SBrian Haley } 894e55ffac6SBrian Haley 895b54c9b98SDavid S. Miller list_add_tail(&ifp->if_list, p); 896e55ffac6SBrian Haley } 897e55ffac6SBrian Haley 898ddbe5032SEric Dumazet static u32 inet6_addr_hash(const struct in6_addr *addr) 8993eb84f49SYOSHIFUJI Hideaki { 900ddbe5032SEric Dumazet return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT); 9013eb84f49SYOSHIFUJI Hideaki } 9023eb84f49SYOSHIFUJI Hideaki 9031da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */ 9041da177e4SLinus Torvalds 9051da177e4SLinus Torvalds static struct inet6_ifaddr * 9063f8f5298SJiri Pirko ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, 9073f8f5298SJiri Pirko const struct in6_addr *peer_addr, int pfxlen, 9088a226b2cSJiri Benc int scope, u32 flags, u32 valid_lft, u32 prefered_lft) 9091da177e4SLinus Torvalds { 9101da177e4SLinus Torvalds struct inet6_ifaddr *ifa = NULL; 9111da177e4SLinus Torvalds struct rt6_info *rt; 9123a88a81dSstephen hemminger unsigned int hash; 9131da177e4SLinus Torvalds int err = 0; 914d68b8270SYOSHIFUJI Hideaki int addr_type = ipv6_addr_type(addr); 915d68b8270SYOSHIFUJI Hideaki 916d68b8270SYOSHIFUJI Hideaki if (addr_type == IPV6_ADDR_ANY || 917d68b8270SYOSHIFUJI Hideaki addr_type & IPV6_ADDR_MULTICAST || 918d68b8270SYOSHIFUJI Hideaki (!(idev->dev->flags & IFF_LOOPBACK) && 919d68b8270SYOSHIFUJI Hideaki addr_type & IPV6_ADDR_LOOPBACK)) 920d68b8270SYOSHIFUJI Hideaki return ERR_PTR(-EADDRNOTAVAIL); 9211da177e4SLinus Torvalds 9228814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 9231da177e4SLinus Torvalds if (idev->dead) { 9241da177e4SLinus Torvalds err = -ENODEV; /*XXX*/ 9251da177e4SLinus Torvalds goto out2; 9261da177e4SLinus Torvalds } 9271da177e4SLinus Torvalds 92856d417b1SBrian Haley if (idev->cnf.disable_ipv6) { 9299bdd8d40SBrian Haley err = -EACCES; 9309bdd8d40SBrian Haley goto out2; 9319bdd8d40SBrian Haley } 9329bdd8d40SBrian Haley 9335c578aedSstephen hemminger spin_lock(&addrconf_hash_lock); 9341da177e4SLinus Torvalds 9351da177e4SLinus Torvalds /* Ignore adding duplicate addresses on an interface */ 936c346dca1SYOSHIFUJI Hideaki if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) { 937ba3542e1Sdingtianhong ADBG("ipv6_add_addr: already assigned\n"); 9381da177e4SLinus Torvalds err = -EEXIST; 9391da177e4SLinus Torvalds goto out; 9401da177e4SLinus Torvalds } 9411da177e4SLinus Torvalds 942322f74a4SIngo Oeser ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC); 9431da177e4SLinus Torvalds 94463159f29SIan Morris if (!ifa) { 945ba3542e1Sdingtianhong ADBG("ipv6_add_addr: malloc failed\n"); 9461da177e4SLinus Torvalds err = -ENOBUFS; 9471da177e4SLinus Torvalds goto out; 9481da177e4SLinus Torvalds } 9491da177e4SLinus Torvalds 9508f031519SDavid S. Miller rt = addrconf_dst_alloc(idev, addr, false); 9511da177e4SLinus Torvalds if (IS_ERR(rt)) { 9521da177e4SLinus Torvalds err = PTR_ERR(rt); 9531da177e4SLinus Torvalds goto out; 9541da177e4SLinus Torvalds } 9551da177e4SLinus Torvalds 956bba24896SJiri Pirko neigh_parms_data_state_setall(idev->nd_parms); 957bba24896SJiri Pirko 9584e3fd7a0SAlexey Dobriyan ifa->addr = *addr; 9593f8f5298SJiri Pirko if (peer_addr) 9603f8f5298SJiri Pirko ifa->peer_addr = *peer_addr; 9611da177e4SLinus Torvalds 9621da177e4SLinus Torvalds spin_lock_init(&ifa->lock); 963c15b1ccaSHannes Frederic Sowa INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work); 964c2e21293Sstephen hemminger INIT_HLIST_NODE(&ifa->addr_lst); 9651da177e4SLinus Torvalds ifa->scope = scope; 9661da177e4SLinus Torvalds ifa->prefix_len = pfxlen; 9671da177e4SLinus Torvalds ifa->flags = flags | IFA_F_TENTATIVE; 9688a226b2cSJiri Benc ifa->valid_lft = valid_lft; 9698a226b2cSJiri Benc ifa->prefered_lft = prefered_lft; 9701da177e4SLinus Torvalds ifa->cstamp = ifa->tstamp = jiffies; 971617fe29dSDaniel Borkmann ifa->tokenized = false; 9721da177e4SLinus Torvalds 97357f5f544SKeir Fraser ifa->rt = rt; 97457f5f544SKeir Fraser 9751da177e4SLinus Torvalds ifa->idev = idev; 9761da177e4SLinus Torvalds in6_dev_hold(idev); 9771da177e4SLinus Torvalds /* For caller */ 9781da177e4SLinus Torvalds in6_ifa_hold(ifa); 9791da177e4SLinus Torvalds 9801da177e4SLinus Torvalds /* Add to big hash table */ 981ddbe5032SEric Dumazet hash = inet6_addr_hash(addr); 9821da177e4SLinus Torvalds 9835c578aedSstephen hemminger hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]); 9845c578aedSstephen hemminger spin_unlock(&addrconf_hash_lock); 9851da177e4SLinus Torvalds 9861da177e4SLinus Torvalds write_lock(&idev->lock); 9871da177e4SLinus Torvalds /* Add to inet6_dev unicast addr list. */ 988e55ffac6SBrian Haley ipv6_link_dev_addr(idev, ifa); 9891da177e4SLinus Torvalds 9901da177e4SLinus Torvalds if (ifa->flags&IFA_F_TEMPORARY) { 991372e6c8fSstephen hemminger list_add(&ifa->tmp_list, &idev->tempaddr_list); 9921da177e4SLinus Torvalds in6_ifa_hold(ifa); 9931da177e4SLinus Torvalds } 9941da177e4SLinus Torvalds 9951da177e4SLinus Torvalds in6_ifa_hold(ifa); 9961da177e4SLinus Torvalds write_unlock(&idev->lock); 9971da177e4SLinus Torvalds out2: 9988814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 9991da177e4SLinus Torvalds 1000fd92833aSYOSHIFUJI Hideaki if (likely(err == 0)) 1001f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_UP, ifa); 10021da177e4SLinus Torvalds else { 10031da177e4SLinus Torvalds kfree(ifa); 10041da177e4SLinus Torvalds ifa = ERR_PTR(err); 10051da177e4SLinus Torvalds } 10061da177e4SLinus Torvalds 10071da177e4SLinus Torvalds return ifa; 10081da177e4SLinus Torvalds out: 10095c578aedSstephen hemminger spin_unlock(&addrconf_hash_lock); 10101da177e4SLinus Torvalds goto out2; 10111da177e4SLinus Torvalds } 10121da177e4SLinus Torvalds 10135b84efecSThomas Haller enum cleanup_prefix_rt_t { 10145b84efecSThomas Haller CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */ 10155b84efecSThomas Haller CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */ 10165b84efecSThomas Haller CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */ 10175b84efecSThomas Haller }; 10185b84efecSThomas Haller 10195b84efecSThomas Haller /* 10205b84efecSThomas Haller * Check, whether the prefix for ifp would still need a prefix route 10215b84efecSThomas Haller * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_* 10225b84efecSThomas Haller * constants. 10235b84efecSThomas Haller * 10245b84efecSThomas Haller * 1) we don't purge prefix if address was not permanent. 10255b84efecSThomas Haller * prefix is managed by its own lifetime. 10265b84efecSThomas Haller * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE. 10275b84efecSThomas Haller * 3) if there are no addresses, delete prefix. 10285b84efecSThomas Haller * 4) if there are still other permanent address(es), 10295b84efecSThomas Haller * corresponding prefix is still permanent. 10305b84efecSThomas Haller * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE, 10315b84efecSThomas Haller * don't purge the prefix, assume user space is managing it. 10325b84efecSThomas Haller * 6) otherwise, update prefix lifetime to the 10335b84efecSThomas Haller * longest valid lifetime among the corresponding 10345b84efecSThomas Haller * addresses on the device. 10355b84efecSThomas Haller * Note: subsequent RA will update lifetime. 10365b84efecSThomas Haller **/ 10375b84efecSThomas Haller static enum cleanup_prefix_rt_t 10385b84efecSThomas Haller check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires) 10395b84efecSThomas Haller { 10405b84efecSThomas Haller struct inet6_ifaddr *ifa; 10415b84efecSThomas Haller struct inet6_dev *idev = ifp->idev; 10425b84efecSThomas Haller unsigned long lifetime; 10435b84efecSThomas Haller enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL; 10445b84efecSThomas Haller 10455b84efecSThomas Haller *expires = jiffies; 10465b84efecSThomas Haller 10475b84efecSThomas Haller list_for_each_entry(ifa, &idev->addr_list, if_list) { 10485b84efecSThomas Haller if (ifa == ifp) 10495b84efecSThomas Haller continue; 10505b84efecSThomas Haller if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr, 10515b84efecSThomas Haller ifp->prefix_len)) 10525b84efecSThomas Haller continue; 10535b84efecSThomas Haller if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE)) 10545b84efecSThomas Haller return CLEANUP_PREFIX_RT_NOP; 10555b84efecSThomas Haller 10565b84efecSThomas Haller action = CLEANUP_PREFIX_RT_EXPIRE; 10575b84efecSThomas Haller 10585b84efecSThomas Haller spin_lock(&ifa->lock); 10595b84efecSThomas Haller 10605b84efecSThomas Haller lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ); 10615b84efecSThomas Haller /* 10625b84efecSThomas Haller * Note: Because this address is 10635b84efecSThomas Haller * not permanent, lifetime < 10645b84efecSThomas Haller * LONG_MAX / HZ here. 10655b84efecSThomas Haller */ 10665b84efecSThomas Haller if (time_before(*expires, ifa->tstamp + lifetime * HZ)) 10675b84efecSThomas Haller *expires = ifa->tstamp + lifetime * HZ; 10685b84efecSThomas Haller spin_unlock(&ifa->lock); 10695b84efecSThomas Haller } 10705b84efecSThomas Haller 10715b84efecSThomas Haller return action; 10725b84efecSThomas Haller } 10735b84efecSThomas Haller 10745b84efecSThomas Haller static void 10755b84efecSThomas Haller cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt) 10765b84efecSThomas Haller { 10775b84efecSThomas Haller struct rt6_info *rt; 10785b84efecSThomas Haller 10795b84efecSThomas Haller rt = addrconf_get_prefix_route(&ifp->addr, 10805b84efecSThomas Haller ifp->prefix_len, 10815b84efecSThomas Haller ifp->idev->dev, 10825b84efecSThomas Haller 0, RTF_GATEWAY | RTF_DEFAULT); 10835b84efecSThomas Haller if (rt) { 10845b84efecSThomas Haller if (del_rt) 10855b84efecSThomas Haller ip6_del_rt(rt); 10865b84efecSThomas Haller else { 10875b84efecSThomas Haller if (!(rt->rt6i_flags & RTF_EXPIRES)) 10885b84efecSThomas Haller rt6_set_expires(rt, expires); 10895b84efecSThomas Haller ip6_rt_put(rt); 10905b84efecSThomas Haller } 10915b84efecSThomas Haller } 10925b84efecSThomas Haller } 10935b84efecSThomas Haller 10945b84efecSThomas Haller 10951da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */ 10961da177e4SLinus Torvalds 10971da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp) 10981da177e4SLinus Torvalds { 10994c5ff6a6SHerbert Xu int state; 11005b84efecSThomas Haller enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP; 11015b84efecSThomas Haller unsigned long expires; 11021da177e4SLinus Torvalds 1103c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 1104c15b1ccaSHannes Frederic Sowa 11058e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 11064c5ff6a6SHerbert Xu state = ifp->state; 1107e9d3e084SHerbert Xu ifp->state = INET6_IFADDR_STATE_DEAD; 11088e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 11094c5ff6a6SHerbert Xu 11104c5ff6a6SHerbert Xu if (state == INET6_IFADDR_STATE_DEAD) 11114c5ff6a6SHerbert Xu goto out; 11121da177e4SLinus Torvalds 11135c578aedSstephen hemminger spin_lock_bh(&addrconf_hash_lock); 11145c578aedSstephen hemminger hlist_del_init_rcu(&ifp->addr_lst); 11155c578aedSstephen hemminger spin_unlock_bh(&addrconf_hash_lock); 11161da177e4SLinus Torvalds 11175b84efecSThomas Haller write_lock_bh(&ifp->idev->lock); 11185d9efa7eSDavid S. Miller 11191da177e4SLinus Torvalds if (ifp->flags&IFA_F_TEMPORARY) { 1120372e6c8fSstephen hemminger list_del(&ifp->tmp_list); 11211da177e4SLinus Torvalds if (ifp->ifpub) { 11221da177e4SLinus Torvalds in6_ifa_put(ifp->ifpub); 11231da177e4SLinus Torvalds ifp->ifpub = NULL; 11241da177e4SLinus Torvalds } 11251da177e4SLinus Torvalds __in6_ifa_put(ifp); 11261da177e4SLinus Torvalds } 11271da177e4SLinus Torvalds 11285b84efecSThomas Haller if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE)) 11295b84efecSThomas Haller action = check_cleanup_prefix_route(ifp, &expires); 11305b84efecSThomas Haller 1131502a2ffdSstephen hemminger list_del_init(&ifp->if_list); 11321da177e4SLinus Torvalds __in6_ifa_put(ifp); 1133502a2ffdSstephen hemminger 11345b84efecSThomas Haller write_unlock_bh(&ifp->idev->lock); 11351da177e4SLinus Torvalds 1136c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifp); 1137b2238566SAndrey Vagin 11381da177e4SLinus Torvalds ipv6_ifa_notify(RTM_DELADDR, ifp); 11391da177e4SLinus Torvalds 1140f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_DOWN, ifp); 11411da177e4SLinus Torvalds 11425b84efecSThomas Haller if (action != CLEANUP_PREFIX_RT_NOP) { 11435b84efecSThomas Haller cleanup_prefix_route(ifp, expires, 11445b84efecSThomas Haller action == CLEANUP_PREFIX_RT_DEL); 11451da177e4SLinus Torvalds } 11461da177e4SLinus Torvalds 1147c3968a85SDaniel Walter /* clean up prefsrc entries */ 1148c3968a85SDaniel Walter rt6_remove_prefsrc(ifp); 11494c5ff6a6SHerbert Xu out: 11501da177e4SLinus Torvalds in6_ifa_put(ifp); 11511da177e4SLinus Torvalds } 11521da177e4SLinus Torvalds 11531da177e4SLinus Torvalds static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift) 11541da177e4SLinus Torvalds { 11551da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 11561da177e4SLinus Torvalds struct in6_addr addr, *tmpaddr; 115776f793e3SLorenzo Colitti unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age; 1158eac55bf9SBenoit Boissinot unsigned long regen_advance; 11591da177e4SLinus Torvalds int tmp_plen; 11601da177e4SLinus Torvalds int ret = 0; 116195c385b4SNeil Horman u32 addr_flags; 116276f793e3SLorenzo Colitti unsigned long now = jiffies; 11631da177e4SLinus Torvalds 116453bd6749SJiri Pirko write_lock_bh(&idev->lock); 11651da177e4SLinus Torvalds if (ift) { 11661da177e4SLinus Torvalds spin_lock_bh(&ift->lock); 11671da177e4SLinus Torvalds memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8); 11681da177e4SLinus Torvalds spin_unlock_bh(&ift->lock); 11691da177e4SLinus Torvalds tmpaddr = &addr; 11701da177e4SLinus Torvalds } else { 11711da177e4SLinus Torvalds tmpaddr = NULL; 11721da177e4SLinus Torvalds } 11731da177e4SLinus Torvalds retry: 11741da177e4SLinus Torvalds in6_dev_hold(idev); 11751da177e4SLinus Torvalds if (idev->cnf.use_tempaddr <= 0) { 117653bd6749SJiri Pirko write_unlock_bh(&idev->lock); 1177f3213831SJoe Perches pr_info("%s: use_tempaddr is disabled\n", __func__); 11781da177e4SLinus Torvalds in6_dev_put(idev); 11791da177e4SLinus Torvalds ret = -1; 11801da177e4SLinus Torvalds goto out; 11811da177e4SLinus Torvalds } 11821da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 11831da177e4SLinus Torvalds if (ifp->regen_count++ >= idev->cnf.regen_max_retry) { 11841da177e4SLinus Torvalds idev->cnf.use_tempaddr = -1; /*XXX*/ 11851da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 118653bd6749SJiri Pirko write_unlock_bh(&idev->lock); 1187f3213831SJoe Perches pr_warn("%s: regeneration time exceeded - disabled temporary address support\n", 1188f3213831SJoe Perches __func__); 11891da177e4SLinus Torvalds in6_dev_put(idev); 11901da177e4SLinus Torvalds ret = -1; 11911da177e4SLinus Torvalds goto out; 11921da177e4SLinus Torvalds } 11931da177e4SLinus Torvalds in6_ifa_hold(ifp); 11941da177e4SLinus Torvalds memcpy(addr.s6_addr, ifp->addr.s6_addr, 8); 1195eb7e0575SSorin Dumitru __ipv6_try_regen_rndid(idev, tmpaddr); 11961da177e4SLinus Torvalds memcpy(&addr.s6_addr[8], idev->rndid, 8); 119776f793e3SLorenzo Colitti age = (now - ifp->tstamp) / HZ; 11981da177e4SLinus Torvalds tmp_valid_lft = min_t(__u32, 11991da177e4SLinus Torvalds ifp->valid_lft, 12007a876b0eSGlenn Wurster idev->cnf.temp_valid_lft + age); 12011da177e4SLinus Torvalds tmp_prefered_lft = min_t(__u32, 12021da177e4SLinus Torvalds ifp->prefered_lft, 12037a876b0eSGlenn Wurster idev->cnf.temp_prefered_lft + age - 1204784e2710SBen Hutchings idev->cnf.max_desync_factor); 12051da177e4SLinus Torvalds tmp_plen = ifp->prefix_len; 12061da177e4SLinus Torvalds tmp_tstamp = ifp->tstamp; 12071da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 12081da177e4SLinus Torvalds 1209eac55bf9SBenoit Boissinot regen_advance = idev->cnf.regen_max_retry * 1210eac55bf9SBenoit Boissinot idev->cnf.dad_transmits * 12111f9248e5SJiri Pirko NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ; 121253bd6749SJiri Pirko write_unlock_bh(&idev->lock); 121395c385b4SNeil Horman 1214eac55bf9SBenoit Boissinot /* A temporary address is created only if this calculated Preferred 1215eac55bf9SBenoit Boissinot * Lifetime is greater than REGEN_ADVANCE time units. In particular, 1216eac55bf9SBenoit Boissinot * an implementation must not create a temporary address with a zero 1217eac55bf9SBenoit Boissinot * Preferred Lifetime. 1218ecab6701SHeiner Kallweit * Use age calculation as in addrconf_verify to avoid unnecessary 1219ecab6701SHeiner Kallweit * temporary addresses being generated. 1220eac55bf9SBenoit Boissinot */ 1221ecab6701SHeiner Kallweit age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; 1222ecab6701SHeiner Kallweit if (tmp_prefered_lft <= regen_advance + age) { 1223eac55bf9SBenoit Boissinot in6_ifa_put(ifp); 1224eac55bf9SBenoit Boissinot in6_dev_put(idev); 1225eac55bf9SBenoit Boissinot ret = -1; 1226eac55bf9SBenoit Boissinot goto out; 1227eac55bf9SBenoit Boissinot } 1228eac55bf9SBenoit Boissinot 122995c385b4SNeil Horman addr_flags = IFA_F_TEMPORARY; 123095c385b4SNeil Horman /* set in addrconf_prefix_rcv() */ 123195c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 123295c385b4SNeil Horman addr_flags |= IFA_F_OPTIMISTIC; 123395c385b4SNeil Horman 12344b08a8f1SHannes Frederic Sowa ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen, 12358a226b2cSJiri Benc ipv6_addr_scope(&addr), addr_flags, 12364b08a8f1SHannes Frederic Sowa tmp_valid_lft, tmp_prefered_lft); 12374b08a8f1SHannes Frederic Sowa if (IS_ERR(ift)) { 12381da177e4SLinus Torvalds in6_ifa_put(ifp); 12391da177e4SLinus Torvalds in6_dev_put(idev); 1240f3213831SJoe Perches pr_info("%s: retry temporary address regeneration\n", __func__); 12411da177e4SLinus Torvalds tmpaddr = &addr; 124253bd6749SJiri Pirko write_lock_bh(&idev->lock); 12431da177e4SLinus Torvalds goto retry; 12441da177e4SLinus Torvalds } 12451da177e4SLinus Torvalds 12461da177e4SLinus Torvalds spin_lock_bh(&ift->lock); 12471da177e4SLinus Torvalds ift->ifpub = ifp; 124876f793e3SLorenzo Colitti ift->cstamp = now; 12491da177e4SLinus Torvalds ift->tstamp = tmp_tstamp; 12501da177e4SLinus Torvalds spin_unlock_bh(&ift->lock); 12511da177e4SLinus Torvalds 1252cf22f9a2SDavid S. Miller addrconf_dad_start(ift); 12531da177e4SLinus Torvalds in6_ifa_put(ift); 12541da177e4SLinus Torvalds in6_dev_put(idev); 12551da177e4SLinus Torvalds out: 12561da177e4SLinus Torvalds return ret; 12571da177e4SLinus Torvalds } 12581da177e4SLinus Torvalds 12591da177e4SLinus Torvalds /* 1260072047e4SYOSHIFUJI Hideaki * Choose an appropriate source address (RFC3484) 12611da177e4SLinus Torvalds */ 1262a9b05723SYOSHIFUJI Hideaki enum { 1263a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_INIT = 0, 1264a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_LOCAL, 1265a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_SCOPE, 1266a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PREFERRED, 1267a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6 1268a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_HOA, 1269a9b05723SYOSHIFUJI Hideaki #endif 1270a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_OIF, 1271a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_LABEL, 1272a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PRIVACY, 1273a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_ORCHID, 1274a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PREFIX, 12757fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 12767fd2561eSErik Kline IPV6_SADDR_RULE_NOT_OPTIMISTIC, 12777fd2561eSErik Kline #endif 1278a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_MAX 1279072047e4SYOSHIFUJI Hideaki }; 1280072047e4SYOSHIFUJI Hideaki 1281a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score { 1282a9b05723SYOSHIFUJI Hideaki int rule; 1283a9b05723SYOSHIFUJI Hideaki int addr_type; 1284a9b05723SYOSHIFUJI Hideaki struct inet6_ifaddr *ifa; 1285a9b05723SYOSHIFUJI Hideaki DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX); 1286a9b05723SYOSHIFUJI Hideaki int scopedist; 1287a9b05723SYOSHIFUJI Hideaki int matchlen; 1288a9b05723SYOSHIFUJI Hideaki }; 1289a9b05723SYOSHIFUJI Hideaki 1290a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst { 12919acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *addr; 1292a9b05723SYOSHIFUJI Hideaki int ifindex; 1293a9b05723SYOSHIFUJI Hideaki int scope; 1294a9b05723SYOSHIFUJI Hideaki int label; 12957cbca67cSYOSHIFUJI Hideaki unsigned int prefs; 1296a9b05723SYOSHIFUJI Hideaki }; 1297072047e4SYOSHIFUJI Hideaki 1298b6f99a21SDave Jones static inline int ipv6_saddr_preferred(int type) 12991da177e4SLinus Torvalds { 130045bb0060SUlrich Weber if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK)) 1301072047e4SYOSHIFUJI Hideaki return 1; 1302072047e4SYOSHIFUJI Hideaki return 0; 13031da177e4SLinus Torvalds } 13041da177e4SLinus Torvalds 13057fd2561eSErik Kline static inline bool ipv6_use_optimistic_addr(struct inet6_dev *idev) 13067fd2561eSErik Kline { 13077fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 13087fd2561eSErik Kline return idev && idev->cnf.optimistic_dad && idev->cnf.use_optimistic; 13097fd2561eSErik Kline #else 13107fd2561eSErik Kline return false; 13117fd2561eSErik Kline #endif 13127fd2561eSErik Kline } 13137fd2561eSErik Kline 13143de23255SBenjamin Thery static int ipv6_get_saddr_eval(struct net *net, 13153de23255SBenjamin Thery struct ipv6_saddr_score *score, 1316a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst *dst, 1317a9b05723SYOSHIFUJI Hideaki int i) 1318a9b05723SYOSHIFUJI Hideaki { 1319a9b05723SYOSHIFUJI Hideaki int ret; 1320a9b05723SYOSHIFUJI Hideaki 1321a9b05723SYOSHIFUJI Hideaki if (i <= score->rule) { 1322a9b05723SYOSHIFUJI Hideaki switch (i) { 1323a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_SCOPE: 1324a9b05723SYOSHIFUJI Hideaki ret = score->scopedist; 1325a9b05723SYOSHIFUJI Hideaki break; 1326a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFIX: 1327a9b05723SYOSHIFUJI Hideaki ret = score->matchlen; 1328a9b05723SYOSHIFUJI Hideaki break; 1329a9b05723SYOSHIFUJI Hideaki default: 1330a9b05723SYOSHIFUJI Hideaki ret = !!test_bit(i, score->scorebits); 1331a9b05723SYOSHIFUJI Hideaki } 1332a9b05723SYOSHIFUJI Hideaki goto out; 1333a9b05723SYOSHIFUJI Hideaki } 1334a9b05723SYOSHIFUJI Hideaki 1335a9b05723SYOSHIFUJI Hideaki switch (i) { 1336a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_INIT: 1337a9b05723SYOSHIFUJI Hideaki /* Rule 0: remember if hiscore is not ready yet */ 1338a9b05723SYOSHIFUJI Hideaki ret = !!score->ifa; 1339a9b05723SYOSHIFUJI Hideaki break; 1340a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_LOCAL: 1341a9b05723SYOSHIFUJI Hideaki /* Rule 1: Prefer same address */ 1342a9b05723SYOSHIFUJI Hideaki ret = ipv6_addr_equal(&score->ifa->addr, dst->addr); 1343a9b05723SYOSHIFUJI Hideaki break; 1344a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_SCOPE: 1345a9b05723SYOSHIFUJI Hideaki /* Rule 2: Prefer appropriate scope 1346a9b05723SYOSHIFUJI Hideaki * 1347a9b05723SYOSHIFUJI Hideaki * ret 1348a9b05723SYOSHIFUJI Hideaki * ^ 1349a9b05723SYOSHIFUJI Hideaki * -1 | d 15 1350a9b05723SYOSHIFUJI Hideaki * ---+--+-+---> scope 1351a9b05723SYOSHIFUJI Hideaki * | 1352a9b05723SYOSHIFUJI Hideaki * | d is scope of the destination. 1353a9b05723SYOSHIFUJI Hideaki * B-d | \ 1354a9b05723SYOSHIFUJI Hideaki * | \ <- smaller scope is better if 1355db9c7c39Sstephen hemminger * B-15 | \ if scope is enough for destination. 1356a9b05723SYOSHIFUJI Hideaki * | ret = B - scope (-1 <= scope >= d <= 15). 1357a9b05723SYOSHIFUJI Hideaki * d-C-1 | / 1358a9b05723SYOSHIFUJI Hideaki * |/ <- greater is better 1359a9b05723SYOSHIFUJI Hideaki * -C / if scope is not enough for destination. 1360a9b05723SYOSHIFUJI Hideaki * /| ret = scope - C (-1 <= d < scope <= 15). 1361a9b05723SYOSHIFUJI Hideaki * 1362a9b05723SYOSHIFUJI Hideaki * d - C - 1 < B -15 (for all -1 <= d <= 15). 1363a9b05723SYOSHIFUJI Hideaki * C > d + 14 - B >= 15 + 14 - B = 29 - B. 1364a9b05723SYOSHIFUJI Hideaki * Assume B = 0 and we get C > 29. 1365a9b05723SYOSHIFUJI Hideaki */ 1366a9b05723SYOSHIFUJI Hideaki ret = __ipv6_addr_src_scope(score->addr_type); 1367a9b05723SYOSHIFUJI Hideaki if (ret >= dst->scope) 1368a9b05723SYOSHIFUJI Hideaki ret = -ret; 1369a9b05723SYOSHIFUJI Hideaki else 1370a9b05723SYOSHIFUJI Hideaki ret -= 128; /* 30 is enough */ 1371a9b05723SYOSHIFUJI Hideaki score->scopedist = ret; 1372a9b05723SYOSHIFUJI Hideaki break; 1373a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFERRED: 13747fd2561eSErik Kline { 1375a9b05723SYOSHIFUJI Hideaki /* Rule 3: Avoid deprecated and optimistic addresses */ 13767fd2561eSErik Kline u8 avoid = IFA_F_DEPRECATED; 13777fd2561eSErik Kline 13787fd2561eSErik Kline if (!ipv6_use_optimistic_addr(score->ifa->idev)) 13797fd2561eSErik Kline avoid |= IFA_F_OPTIMISTIC; 1380a9b05723SYOSHIFUJI Hideaki ret = ipv6_saddr_preferred(score->addr_type) || 13817fd2561eSErik Kline !(score->ifa->flags & avoid); 1382a9b05723SYOSHIFUJI Hideaki break; 13837fd2561eSErik Kline } 1384a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6 1385a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_HOA: 13867cbca67cSYOSHIFUJI Hideaki { 1387a9b05723SYOSHIFUJI Hideaki /* Rule 4: Prefer home address */ 13887cbca67cSYOSHIFUJI Hideaki int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA); 13897cbca67cSYOSHIFUJI Hideaki ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome; 1390a9b05723SYOSHIFUJI Hideaki break; 13917cbca67cSYOSHIFUJI Hideaki } 1392a9b05723SYOSHIFUJI Hideaki #endif 1393a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_OIF: 1394a9b05723SYOSHIFUJI Hideaki /* Rule 5: Prefer outgoing interface */ 1395a9b05723SYOSHIFUJI Hideaki ret = (!dst->ifindex || 1396a9b05723SYOSHIFUJI Hideaki dst->ifindex == score->ifa->idev->dev->ifindex); 1397a9b05723SYOSHIFUJI Hideaki break; 1398a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_LABEL: 1399a9b05723SYOSHIFUJI Hideaki /* Rule 6: Prefer matching label */ 14003de23255SBenjamin Thery ret = ipv6_addr_label(net, 14013de23255SBenjamin Thery &score->ifa->addr, score->addr_type, 1402a9b05723SYOSHIFUJI Hideaki score->ifa->idev->dev->ifindex) == dst->label; 1403a9b05723SYOSHIFUJI Hideaki break; 1404a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PRIVACY: 14057cbca67cSYOSHIFUJI Hideaki { 1406a9b05723SYOSHIFUJI Hideaki /* Rule 7: Prefer public address 140725985edcSLucas De Marchi * Note: prefer temporary address if use_tempaddr >= 2 1408a9b05723SYOSHIFUJI Hideaki */ 14097cbca67cSYOSHIFUJI Hideaki int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ? 14107cbca67cSYOSHIFUJI Hideaki !!(dst->prefs & IPV6_PREFER_SRC_TMP) : 14117cbca67cSYOSHIFUJI Hideaki score->ifa->idev->cnf.use_tempaddr >= 2; 14127cbca67cSYOSHIFUJI Hideaki ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp; 1413a9b05723SYOSHIFUJI Hideaki break; 14147cbca67cSYOSHIFUJI Hideaki } 1415a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_ORCHID: 1416a9b05723SYOSHIFUJI Hideaki /* Rule 8-: Prefer ORCHID vs ORCHID or 1417a9b05723SYOSHIFUJI Hideaki * non-ORCHID vs non-ORCHID 1418a9b05723SYOSHIFUJI Hideaki */ 1419a9b05723SYOSHIFUJI Hideaki ret = !(ipv6_addr_orchid(&score->ifa->addr) ^ 1420a9b05723SYOSHIFUJI Hideaki ipv6_addr_orchid(dst->addr)); 1421a9b05723SYOSHIFUJI Hideaki break; 1422a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFIX: 1423a9b05723SYOSHIFUJI Hideaki /* Rule 8: Use longest matching prefix */ 142491b4b04fSYOSHIFUJI Hideaki / 吉藤英明 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr); 142591b4b04fSYOSHIFUJI Hideaki / 吉藤英明 if (ret > score->ifa->prefix_len) 142691b4b04fSYOSHIFUJI Hideaki / 吉藤英明 ret = score->ifa->prefix_len; 142791b4b04fSYOSHIFUJI Hideaki / 吉藤英明 score->matchlen = ret; 1428a9b05723SYOSHIFUJI Hideaki break; 14297fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 14307fd2561eSErik Kline case IPV6_SADDR_RULE_NOT_OPTIMISTIC: 14317fd2561eSErik Kline /* Optimistic addresses still have lower precedence than other 14327fd2561eSErik Kline * preferred addresses. 14337fd2561eSErik Kline */ 14347fd2561eSErik Kline ret = !(score->ifa->flags & IFA_F_OPTIMISTIC); 14357fd2561eSErik Kline break; 14367fd2561eSErik Kline #endif 1437a9b05723SYOSHIFUJI Hideaki default: 1438a9b05723SYOSHIFUJI Hideaki ret = 0; 1439a9b05723SYOSHIFUJI Hideaki } 1440a9b05723SYOSHIFUJI Hideaki 1441a9b05723SYOSHIFUJI Hideaki if (ret) 1442a9b05723SYOSHIFUJI Hideaki __set_bit(i, score->scorebits); 1443a9b05723SYOSHIFUJI Hideaki score->rule = i; 1444a9b05723SYOSHIFUJI Hideaki out: 1445a9b05723SYOSHIFUJI Hideaki return ret; 1446a9b05723SYOSHIFUJI Hideaki } 1447a9b05723SYOSHIFUJI Hideaki 1448c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 static int __ipv6_dev_get_saddr(struct net *net, 14499131f3deSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_dst *dst, 14509131f3deSYOSHIFUJI Hideaki/吉藤英明 struct inet6_dev *idev, 1451c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_score *scores, 1452c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 int hiscore_idx) 14531da177e4SLinus Torvalds { 1454c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx]; 1455072047e4SYOSHIFUJI Hideaki 14561da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1457502a2ffdSstephen hemminger list_for_each_entry(score->ifa, &idev->addr_list, if_list) { 1458a9b05723SYOSHIFUJI Hideaki int i; 1459072047e4SYOSHIFUJI Hideaki 1460a9b05723SYOSHIFUJI Hideaki /* 14616b3ae80aSYOSHIFUJI Hideaki * - Tentative Address (RFC2462 section 5.4) 14626b3ae80aSYOSHIFUJI Hideaki * - A tentative address is not considered 14636b3ae80aSYOSHIFUJI Hideaki * "assigned to an interface" in the traditional 146495c385b4SNeil Horman * sense, unless it is also flagged as optimistic. 14656b3ae80aSYOSHIFUJI Hideaki * - Candidate Source Address (section 4) 1466072047e4SYOSHIFUJI Hideaki * - In any case, anycast addresses, multicast 1467072047e4SYOSHIFUJI Hideaki * addresses, and the unspecified address MUST 1468072047e4SYOSHIFUJI Hideaki * NOT be included in a candidate set. 1469072047e4SYOSHIFUJI Hideaki */ 1470a9b05723SYOSHIFUJI Hideaki if ((score->ifa->flags & IFA_F_TENTATIVE) && 1471a9b05723SYOSHIFUJI Hideaki (!(score->ifa->flags & IFA_F_OPTIMISTIC))) 14726b3ae80aSYOSHIFUJI Hideaki continue; 1473a9b05723SYOSHIFUJI Hideaki 1474a9b05723SYOSHIFUJI Hideaki score->addr_type = __ipv6_addr_type(&score->ifa->addr); 1475a9b05723SYOSHIFUJI Hideaki 1476a9b05723SYOSHIFUJI Hideaki if (unlikely(score->addr_type == IPV6_ADDR_ANY || 1477a9b05723SYOSHIFUJI Hideaki score->addr_type & IPV6_ADDR_MULTICAST)) { 1478ba7a46f1SJoe Perches net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s", 14799131f3deSYOSHIFUJI Hideaki/吉藤英明 idev->dev->name); 14801da177e4SLinus Torvalds continue; 1481072047e4SYOSHIFUJI Hideaki } 1482072047e4SYOSHIFUJI Hideaki 1483a9b05723SYOSHIFUJI Hideaki score->rule = -1; 1484a9b05723SYOSHIFUJI Hideaki bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX); 1485072047e4SYOSHIFUJI Hideaki 1486a9b05723SYOSHIFUJI Hideaki for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) { 1487a9b05723SYOSHIFUJI Hideaki int minihiscore, miniscore; 1488072047e4SYOSHIFUJI Hideaki 14899131f3deSYOSHIFUJI Hideaki/吉藤英明 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i); 14909131f3deSYOSHIFUJI Hideaki/吉藤英明 miniscore = ipv6_get_saddr_eval(net, score, dst, i); 1491072047e4SYOSHIFUJI Hideaki 1492a9b05723SYOSHIFUJI Hideaki if (minihiscore > miniscore) { 1493a9b05723SYOSHIFUJI Hideaki if (i == IPV6_SADDR_RULE_SCOPE && 1494a9b05723SYOSHIFUJI Hideaki score->scopedist > 0) { 1495a9b05723SYOSHIFUJI Hideaki /* 1496a9b05723SYOSHIFUJI Hideaki * special case: 1497a9b05723SYOSHIFUJI Hideaki * each remaining entry 1498a9b05723SYOSHIFUJI Hideaki * has too small (not enough) 1499a9b05723SYOSHIFUJI Hideaki * scope, because ifa entries 1500a9b05723SYOSHIFUJI Hideaki * are sorted by their scope 1501a9b05723SYOSHIFUJI Hideaki * values. 1502072047e4SYOSHIFUJI Hideaki */ 15039131f3deSYOSHIFUJI Hideaki/吉藤英明 goto out; 1504072047e4SYOSHIFUJI Hideaki } 1505a9b05723SYOSHIFUJI Hideaki break; 1506a9b05723SYOSHIFUJI Hideaki } else if (minihiscore < miniscore) { 1507a9b05723SYOSHIFUJI Hideaki if (hiscore->ifa) 1508a9b05723SYOSHIFUJI Hideaki in6_ifa_put(hiscore->ifa); 150999cd07a5SJuha-Matti Tapio 1510a9b05723SYOSHIFUJI Hideaki in6_ifa_hold(score->ifa); 1511072047e4SYOSHIFUJI Hideaki 1512a0bffffcSIlpo Järvinen swap(hiscore, score); 1513c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 hiscore_idx = 1 - hiscore_idx; 1514a9b05723SYOSHIFUJI Hideaki 1515a9b05723SYOSHIFUJI Hideaki /* restore our iterator */ 1516a9b05723SYOSHIFUJI Hideaki score->ifa = hiscore->ifa; 1517a9b05723SYOSHIFUJI Hideaki 1518a9b05723SYOSHIFUJI Hideaki break; 15191da177e4SLinus Torvalds } 1520a9b05723SYOSHIFUJI Hideaki } 1521a9b05723SYOSHIFUJI Hideaki } 15229131f3deSYOSHIFUJI Hideaki/吉藤英明 out: 15231da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 1524c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 return hiscore_idx; 15251da177e4SLinus Torvalds } 15269131f3deSYOSHIFUJI Hideaki/吉藤英明 15279131f3deSYOSHIFUJI Hideaki/吉藤英明 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev, 15289131f3deSYOSHIFUJI Hideaki/吉藤英明 const struct in6_addr *daddr, unsigned int prefs, 15299131f3deSYOSHIFUJI Hideaki/吉藤英明 struct in6_addr *saddr) 15309131f3deSYOSHIFUJI Hideaki/吉藤英明 { 1531c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_score scores[2], *hiscore; 15329131f3deSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_dst dst; 15339131f3deSYOSHIFUJI Hideaki/吉藤英明 struct inet6_dev *idev; 15349131f3deSYOSHIFUJI Hideaki/吉藤英明 struct net_device *dev; 15359131f3deSYOSHIFUJI Hideaki/吉藤英明 int dst_type; 15369131f3deSYOSHIFUJI Hideaki/吉藤英明 bool use_oif_addr = false; 1537c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 int hiscore_idx = 0; 15389131f3deSYOSHIFUJI Hideaki/吉藤英明 15399131f3deSYOSHIFUJI Hideaki/吉藤英明 dst_type = __ipv6_addr_type(daddr); 15409131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.addr = daddr; 15419131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.ifindex = dst_dev ? dst_dev->ifindex : 0; 15429131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.scope = __ipv6_addr_src_scope(dst_type); 15439131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex); 15449131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.prefs = prefs; 15459131f3deSYOSHIFUJI Hideaki/吉藤英明 1546c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 scores[hiscore_idx].rule = -1; 1547c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 scores[hiscore_idx].ifa = NULL; 15489131f3deSYOSHIFUJI Hideaki/吉藤英明 15499131f3deSYOSHIFUJI Hideaki/吉藤英明 rcu_read_lock(); 15509131f3deSYOSHIFUJI Hideaki/吉藤英明 15519131f3deSYOSHIFUJI Hideaki/吉藤英明 /* Candidate Source Address (section 4) 15529131f3deSYOSHIFUJI Hideaki/吉藤英明 * - multicast and link-local destination address, 15539131f3deSYOSHIFUJI Hideaki/吉藤英明 * the set of candidate source address MUST only 15549131f3deSYOSHIFUJI Hideaki/吉藤英明 * include addresses assigned to interfaces 15559131f3deSYOSHIFUJI Hideaki/吉藤英明 * belonging to the same link as the outgoing 15569131f3deSYOSHIFUJI Hideaki/吉藤英明 * interface. 15579131f3deSYOSHIFUJI Hideaki/吉藤英明 * (- For site-local destination addresses, the 15589131f3deSYOSHIFUJI Hideaki/吉藤英明 * set of candidate source addresses MUST only 15599131f3deSYOSHIFUJI Hideaki/吉藤英明 * include addresses assigned to interfaces 15609131f3deSYOSHIFUJI Hideaki/吉藤英明 * belonging to the same site as the outgoing 15619131f3deSYOSHIFUJI Hideaki/吉藤英明 * interface.) 15623985e8a3SErik Kline * - "It is RECOMMENDED that the candidate source addresses 15633985e8a3SErik Kline * be the set of unicast addresses assigned to the 15643985e8a3SErik Kline * interface that will be used to send to the destination 15653985e8a3SErik Kline * (the 'outgoing' interface)." (RFC 6724) 15669131f3deSYOSHIFUJI Hideaki/吉藤英明 */ 15679131f3deSYOSHIFUJI Hideaki/吉藤英明 if (dst_dev) { 15689131f3deSYOSHIFUJI Hideaki/吉藤英明 idev = __in6_dev_get(dst_dev); 15693985e8a3SErik Kline if ((dst_type & IPV6_ADDR_MULTICAST) || 15703985e8a3SErik Kline dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL || 15713985e8a3SErik Kline (idev && idev->cnf.use_oif_addrs_only)) { 15729131f3deSYOSHIFUJI Hideaki/吉藤英明 use_oif_addr = true; 15739131f3deSYOSHIFUJI Hideaki/吉藤英明 } 15749131f3deSYOSHIFUJI Hideaki/吉藤英明 } 15759131f3deSYOSHIFUJI Hideaki/吉藤英明 15769131f3deSYOSHIFUJI Hideaki/吉藤英明 if (use_oif_addr) { 1577c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 if (idev) 1578c15df306SYOSHIFUJI Hideaki hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx); 15799131f3deSYOSHIFUJI Hideaki/吉藤英明 } else { 15809131f3deSYOSHIFUJI Hideaki/吉藤英明 for_each_netdev_rcu(net, dev) { 15819131f3deSYOSHIFUJI Hideaki/吉藤英明 idev = __in6_dev_get(dev); 15829131f3deSYOSHIFUJI Hideaki/吉藤英明 if (!idev) 15839131f3deSYOSHIFUJI Hideaki/吉藤英明 continue; 1584c15df306SYOSHIFUJI Hideaki hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx); 15859131f3deSYOSHIFUJI Hideaki/吉藤英明 } 15869131f3deSYOSHIFUJI Hideaki/吉藤英明 } 15878814c4b5SYOSHIFUJI Hideaki rcu_read_unlock(); 15881da177e4SLinus Torvalds 1589c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 hiscore = &scores[hiscore_idx]; 1590a9b05723SYOSHIFUJI Hideaki if (!hiscore->ifa) 1591072047e4SYOSHIFUJI Hideaki return -EADDRNOTAVAIL; 15921da177e4SLinus Torvalds 15934e3fd7a0SAlexey Dobriyan *saddr = hiscore->ifa->addr; 1594a9b05723SYOSHIFUJI Hideaki in6_ifa_put(hiscore->ifa); 1595072047e4SYOSHIFUJI Hideaki return 0; 15961da177e4SLinus Torvalds } 15975e5f3f0fSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_dev_get_saddr); 15981da177e4SLinus Torvalds 15998965779dSAmerigo Wang int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr, 1600479840ffSJiri Pirko u32 banned_flags) 1601b7b1bfceSHannes Frederic Sowa { 1602b7b1bfceSHannes Frederic Sowa struct inet6_ifaddr *ifp; 1603b7b1bfceSHannes Frederic Sowa int err = -EADDRNOTAVAIL; 1604b7b1bfceSHannes Frederic Sowa 1605602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) { 1606602582caSHannes Frederic Sowa if (ifp->scope > IFA_LINK) 1607602582caSHannes Frederic Sowa break; 1608b7b1bfceSHannes Frederic Sowa if (ifp->scope == IFA_LINK && 1609b7b1bfceSHannes Frederic Sowa !(ifp->flags & banned_flags)) { 1610b7b1bfceSHannes Frederic Sowa *addr = ifp->addr; 1611b7b1bfceSHannes Frederic Sowa err = 0; 1612b7b1bfceSHannes Frederic Sowa break; 1613b7b1bfceSHannes Frederic Sowa } 1614b7b1bfceSHannes Frederic Sowa } 1615b7b1bfceSHannes Frederic Sowa return err; 1616b7b1bfceSHannes Frederic Sowa } 1617b7b1bfceSHannes Frederic Sowa 161895c385b4SNeil Horman int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr, 1619479840ffSJiri Pirko u32 banned_flags) 16201da177e4SLinus Torvalds { 16211da177e4SLinus Torvalds struct inet6_dev *idev; 16221da177e4SLinus Torvalds int err = -EADDRNOTAVAIL; 16231da177e4SLinus Torvalds 16248814c4b5SYOSHIFUJI Hideaki rcu_read_lock(); 1625e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 1626e21e8467SStephen Hemminger if (idev) { 16271da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1628b7b1bfceSHannes Frederic Sowa err = __ipv6_get_lladdr(idev, addr, banned_flags); 16291da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 16301da177e4SLinus Torvalds } 16318814c4b5SYOSHIFUJI Hideaki rcu_read_unlock(); 16321da177e4SLinus Torvalds return err; 16331da177e4SLinus Torvalds } 16341da177e4SLinus Torvalds 16351da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev) 16361da177e4SLinus Torvalds { 16371da177e4SLinus Torvalds int cnt = 0; 16381da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 16391da177e4SLinus Torvalds 16401da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1641502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) 16421da177e4SLinus Torvalds cnt++; 16431da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 16441da177e4SLinus Torvalds return cnt; 16451da177e4SLinus Torvalds } 16461da177e4SLinus Torvalds 1647b71d1d42SEric Dumazet int ipv6_chk_addr(struct net *net, const struct in6_addr *addr, 16482a7851bfSFlorian Westphal const struct net_device *dev, int strict) 16491da177e4SLinus Torvalds { 1650c58da4c6SErik Kline return ipv6_chk_addr_and_flags(net, addr, dev, strict, IFA_F_TENTATIVE); 1651c58da4c6SErik Kline } 1652c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr); 1653c58da4c6SErik Kline 1654c58da4c6SErik Kline int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr, 1655c58da4c6SErik Kline const struct net_device *dev, int strict, 1656c58da4c6SErik Kline u32 banned_flags) 1657c58da4c6SErik Kline { 1658eedf042aSStephen Hemminger struct inet6_ifaddr *ifp; 1659ddbe5032SEric Dumazet unsigned int hash = inet6_addr_hash(addr); 1660c58da4c6SErik Kline u32 ifp_flags; 16611da177e4SLinus Torvalds 16625c578aedSstephen hemminger rcu_read_lock_bh(); 1663b67bfe0dSSasha Levin hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) { 1664878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 1665bfeade08SDaniel Lezcano continue; 1666c58da4c6SErik Kline /* Decouple optimistic from tentative for evaluation here. 1667c58da4c6SErik Kline * Ban optimistic addresses explicitly, when required. 1668c58da4c6SErik Kline */ 1669c58da4c6SErik Kline ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC) 1670c58da4c6SErik Kline ? (ifp->flags&~IFA_F_TENTATIVE) 1671c58da4c6SErik Kline : ifp->flags; 16721da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr) && 1673c58da4c6SErik Kline !(ifp_flags&banned_flags) && 167463159f29SIan Morris (!dev || ifp->idev->dev == dev || 1675eedf042aSStephen Hemminger !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) { 16765c578aedSstephen hemminger rcu_read_unlock_bh(); 1677eedf042aSStephen Hemminger return 1; 1678eedf042aSStephen Hemminger } 1679eedf042aSStephen Hemminger } 16805c578aedSstephen hemminger 1681eedf042aSStephen Hemminger rcu_read_unlock_bh(); 1682eedf042aSStephen Hemminger return 0; 16831da177e4SLinus Torvalds } 1684c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr_and_flags); 16857159039aSYOSHIFUJI Hideaki 16863e81c6daSDavid S. Miller static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr, 168706bfe655SDaniel Lezcano struct net_device *dev) 16881da177e4SLinus Torvalds { 1689ddbe5032SEric Dumazet unsigned int hash = inet6_addr_hash(addr); 16901da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 16911da177e4SLinus Torvalds 1692b67bfe0dSSasha Levin hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) { 1693878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 169406bfe655SDaniel Lezcano continue; 16951da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr)) { 169663159f29SIan Morris if (!dev || ifp->idev->dev == dev) 16973e81c6daSDavid S. Miller return true; 16981da177e4SLinus Torvalds } 16991da177e4SLinus Torvalds } 17003e81c6daSDavid S. Miller return false; 17011da177e4SLinus Torvalds } 17021da177e4SLinus Torvalds 17037df37ff3SCatalin\(ux\) M. BOIE /* Compares an address/prefix_len with addresses on device @dev. 17047df37ff3SCatalin\(ux\) M. BOIE * If one is found it returns true. 17057df37ff3SCatalin\(ux\) M. BOIE */ 17067df37ff3SCatalin\(ux\) M. BOIE bool ipv6_chk_custom_prefix(const struct in6_addr *addr, 17077df37ff3SCatalin\(ux\) M. BOIE const unsigned int prefix_len, struct net_device *dev) 17087df37ff3SCatalin\(ux\) M. BOIE { 17097df37ff3SCatalin\(ux\) M. BOIE struct inet6_dev *idev; 17107df37ff3SCatalin\(ux\) M. BOIE struct inet6_ifaddr *ifa; 17117df37ff3SCatalin\(ux\) M. BOIE bool ret = false; 17127df37ff3SCatalin\(ux\) M. BOIE 17137df37ff3SCatalin\(ux\) M. BOIE rcu_read_lock(); 17147df37ff3SCatalin\(ux\) M. BOIE idev = __in6_dev_get(dev); 17157df37ff3SCatalin\(ux\) M. BOIE if (idev) { 17167df37ff3SCatalin\(ux\) M. BOIE read_lock_bh(&idev->lock); 17177df37ff3SCatalin\(ux\) M. BOIE list_for_each_entry(ifa, &idev->addr_list, if_list) { 17187df37ff3SCatalin\(ux\) M. BOIE ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len); 17197df37ff3SCatalin\(ux\) M. BOIE if (ret) 17207df37ff3SCatalin\(ux\) M. BOIE break; 17217df37ff3SCatalin\(ux\) M. BOIE } 17227df37ff3SCatalin\(ux\) M. BOIE read_unlock_bh(&idev->lock); 17237df37ff3SCatalin\(ux\) M. BOIE } 17247df37ff3SCatalin\(ux\) M. BOIE rcu_read_unlock(); 17257df37ff3SCatalin\(ux\) M. BOIE 17267df37ff3SCatalin\(ux\) M. BOIE return ret; 17277df37ff3SCatalin\(ux\) M. BOIE } 17287df37ff3SCatalin\(ux\) M. BOIE EXPORT_SYMBOL(ipv6_chk_custom_prefix); 17297df37ff3SCatalin\(ux\) M. BOIE 1730b71d1d42SEric Dumazet int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev) 173152eeeb84SYOSHIFUJI Hideaki { 173252eeeb84SYOSHIFUJI Hideaki struct inet6_dev *idev; 173352eeeb84SYOSHIFUJI Hideaki struct inet6_ifaddr *ifa; 173452eeeb84SYOSHIFUJI Hideaki int onlink; 173552eeeb84SYOSHIFUJI Hideaki 173652eeeb84SYOSHIFUJI Hideaki onlink = 0; 173752eeeb84SYOSHIFUJI Hideaki rcu_read_lock(); 173852eeeb84SYOSHIFUJI Hideaki idev = __in6_dev_get(dev); 173952eeeb84SYOSHIFUJI Hideaki if (idev) { 174052eeeb84SYOSHIFUJI Hideaki read_lock_bh(&idev->lock); 1741502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 174252eeeb84SYOSHIFUJI Hideaki onlink = ipv6_prefix_equal(addr, &ifa->addr, 174352eeeb84SYOSHIFUJI Hideaki ifa->prefix_len); 174452eeeb84SYOSHIFUJI Hideaki if (onlink) 174552eeeb84SYOSHIFUJI Hideaki break; 174652eeeb84SYOSHIFUJI Hideaki } 174752eeeb84SYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 174852eeeb84SYOSHIFUJI Hideaki } 174952eeeb84SYOSHIFUJI Hideaki rcu_read_unlock(); 175052eeeb84SYOSHIFUJI Hideaki return onlink; 175152eeeb84SYOSHIFUJI Hideaki } 175252eeeb84SYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_prefix); 175352eeeb84SYOSHIFUJI Hideaki 17549acd9f3aSYOSHIFUJI Hideaki struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr, 17551cab3da6SDaniel Lezcano struct net_device *dev, int strict) 17561da177e4SLinus Torvalds { 1757b79d1d54SDavid S. Miller struct inet6_ifaddr *ifp, *result = NULL; 1758ddbe5032SEric Dumazet unsigned int hash = inet6_addr_hash(addr); 17591da177e4SLinus Torvalds 17605c578aedSstephen hemminger rcu_read_lock_bh(); 1761b67bfe0dSSasha Levin hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) { 1762878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 17631cab3da6SDaniel Lezcano continue; 17641da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr)) { 176563159f29SIan Morris if (!dev || ifp->idev->dev == dev || 17661da177e4SLinus Torvalds !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) { 1767b79d1d54SDavid S. Miller result = ifp; 17681da177e4SLinus Torvalds in6_ifa_hold(ifp); 17691da177e4SLinus Torvalds break; 17701da177e4SLinus Torvalds } 17711da177e4SLinus Torvalds } 17721da177e4SLinus Torvalds } 17735c578aedSstephen hemminger rcu_read_unlock_bh(); 17741da177e4SLinus Torvalds 1775b79d1d54SDavid S. Miller return result; 17761da177e4SLinus Torvalds } 17771da177e4SLinus Torvalds 17781da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */ 17791da177e4SLinus Torvalds 1780cc411d0bSBrian Haley static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed) 17811da177e4SLinus Torvalds { 17823d171f39SLubomir Rintel if (dad_failed) 17833d171f39SLubomir Rintel ifp->flags |= IFA_F_DADFAILED; 17843d171f39SLubomir Rintel 17851da177e4SLinus Torvalds if (ifp->flags&IFA_F_PERMANENT) { 17861da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 1787c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifp); 17881da177e4SLinus Torvalds ifp->flags |= IFA_F_TENTATIVE; 17891da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 1790e2577a06SHerbert Xu if (dad_failed) 1791e2577a06SHerbert Xu ipv6_ifa_notify(0, ifp); 17921da177e4SLinus Torvalds in6_ifa_put(ifp); 17931da177e4SLinus Torvalds } else if (ifp->flags&IFA_F_TEMPORARY) { 17941da177e4SLinus Torvalds struct inet6_ifaddr *ifpub; 17951da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 17961da177e4SLinus Torvalds ifpub = ifp->ifpub; 17971da177e4SLinus Torvalds if (ifpub) { 17981da177e4SLinus Torvalds in6_ifa_hold(ifpub); 17991da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 18001da177e4SLinus Torvalds ipv6_create_tempaddr(ifpub, ifp); 18011da177e4SLinus Torvalds in6_ifa_put(ifpub); 18021da177e4SLinus Torvalds } else { 18031da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 18041da177e4SLinus Torvalds } 18051da177e4SLinus Torvalds ipv6_del_addr(ifp); 1806c15b1ccaSHannes Frederic Sowa } else { 18071da177e4SLinus Torvalds ipv6_del_addr(ifp); 18081da177e4SLinus Torvalds } 1809c15b1ccaSHannes Frederic Sowa } 18101da177e4SLinus Torvalds 1811f2344a13SHerbert Xu static int addrconf_dad_end(struct inet6_ifaddr *ifp) 1812f2344a13SHerbert Xu { 1813f2344a13SHerbert Xu int err = -ENOENT; 1814f2344a13SHerbert Xu 18158e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 1816f2344a13SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DAD) { 1817f2344a13SHerbert Xu ifp->state = INET6_IFADDR_STATE_POSTDAD; 1818f2344a13SHerbert Xu err = 0; 1819f2344a13SHerbert Xu } 18208e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 1821f2344a13SHerbert Xu 1822f2344a13SHerbert Xu return err; 1823f2344a13SHerbert Xu } 1824f2344a13SHerbert Xu 18253c21edbdSYOSHIFUJI Hideaki void addrconf_dad_failure(struct inet6_ifaddr *ifp) 18263c21edbdSYOSHIFUJI Hideaki { 18275f40ef77SHannes Frederic Sowa struct in6_addr addr; 18281b34be74SYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev; 18291855b7c3SHannes Frederic Sowa struct net *net = dev_net(ifp->idev->dev); 18309bdd8d40SBrian Haley 1831853dc2e0SUrsula Braun if (addrconf_dad_end(ifp)) { 1832853dc2e0SUrsula Braun in6_ifa_put(ifp); 1833f2344a13SHerbert Xu return; 1834853dc2e0SUrsula Braun } 1835f2344a13SHerbert Xu 1836e87cc472SJoe Perches net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n", 18370522fea6SJens Rosenboom ifp->idev->dev->name, &ifp->addr); 18389bdd8d40SBrian Haley 18395f40ef77SHannes Frederic Sowa spin_lock_bh(&ifp->lock); 18401b34be74SYOSHIFUJI Hideaki 18415f40ef77SHannes Frederic Sowa if (ifp->flags & IFA_F_STABLE_PRIVACY) { 18425f40ef77SHannes Frederic Sowa int scope = ifp->scope; 18435f40ef77SHannes Frederic Sowa u32 flags = ifp->flags; 18445f40ef77SHannes Frederic Sowa struct in6_addr new_addr; 18455f40ef77SHannes Frederic Sowa struct inet6_ifaddr *ifp2; 18465f40ef77SHannes Frederic Sowa u32 valid_lft, preferred_lft; 18475f40ef77SHannes Frederic Sowa int pfxlen = ifp->prefix_len; 18485f40ef77SHannes Frederic Sowa int retries = ifp->stable_privacy_retry + 1; 18495f40ef77SHannes Frederic Sowa 18501855b7c3SHannes Frederic Sowa if (retries > net->ipv6.sysctl.idgen_retries) { 18515f40ef77SHannes Frederic Sowa net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n", 18525f40ef77SHannes Frederic Sowa ifp->idev->dev->name); 18535f40ef77SHannes Frederic Sowa goto errdad; 18545f40ef77SHannes Frederic Sowa } 18555f40ef77SHannes Frederic Sowa 18565f40ef77SHannes Frederic Sowa new_addr = ifp->addr; 18575f40ef77SHannes Frederic Sowa if (ipv6_generate_stable_address(&new_addr, retries, 18585f40ef77SHannes Frederic Sowa idev)) 18595f40ef77SHannes Frederic Sowa goto errdad; 18605f40ef77SHannes Frederic Sowa 18615f40ef77SHannes Frederic Sowa valid_lft = ifp->valid_lft; 18625f40ef77SHannes Frederic Sowa preferred_lft = ifp->prefered_lft; 18635f40ef77SHannes Frederic Sowa 18645f40ef77SHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 18655f40ef77SHannes Frederic Sowa 18665f40ef77SHannes Frederic Sowa if (idev->cnf.max_addresses && 18675f40ef77SHannes Frederic Sowa ipv6_count_addresses(idev) >= 18685f40ef77SHannes Frederic Sowa idev->cnf.max_addresses) 18695f40ef77SHannes Frederic Sowa goto lock_errdad; 18705f40ef77SHannes Frederic Sowa 18715f40ef77SHannes Frederic Sowa net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n", 18725f40ef77SHannes Frederic Sowa ifp->idev->dev->name); 18735f40ef77SHannes Frederic Sowa 18745f40ef77SHannes Frederic Sowa ifp2 = ipv6_add_addr(idev, &new_addr, NULL, pfxlen, 18755f40ef77SHannes Frederic Sowa scope, flags, valid_lft, 18765f40ef77SHannes Frederic Sowa preferred_lft); 18775f40ef77SHannes Frederic Sowa if (IS_ERR(ifp2)) 18785f40ef77SHannes Frederic Sowa goto lock_errdad; 18795f40ef77SHannes Frederic Sowa 18805f40ef77SHannes Frederic Sowa spin_lock_bh(&ifp2->lock); 18815f40ef77SHannes Frederic Sowa ifp2->stable_privacy_retry = retries; 18825f40ef77SHannes Frederic Sowa ifp2->state = INET6_IFADDR_STATE_PREDAD; 18835f40ef77SHannes Frederic Sowa spin_unlock_bh(&ifp2->lock); 18845f40ef77SHannes Frederic Sowa 18851855b7c3SHannes Frederic Sowa addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay); 18865f40ef77SHannes Frederic Sowa in6_ifa_put(ifp2); 18875f40ef77SHannes Frederic Sowa lock_errdad: 18885f40ef77SHannes Frederic Sowa spin_lock_bh(&ifp->lock); 18895f40ef77SHannes Frederic Sowa } else if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) { 18901b34be74SYOSHIFUJI Hideaki addr.s6_addr32[0] = htonl(0xfe800000); 18911b34be74SYOSHIFUJI Hideaki addr.s6_addr32[1] = 0; 18921b34be74SYOSHIFUJI Hideaki 18931b34be74SYOSHIFUJI Hideaki if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) && 18941b34be74SYOSHIFUJI Hideaki ipv6_addr_equal(&ifp->addr, &addr)) { 18951b34be74SYOSHIFUJI Hideaki /* DAD failed for link-local based on MAC address */ 18961b34be74SYOSHIFUJI Hideaki idev->cnf.disable_ipv6 = 1; 18979bdd8d40SBrian Haley 1898f3213831SJoe Perches pr_info("%s: IPv6 being disabled!\n", 18999bdd8d40SBrian Haley ifp->idev->dev->name); 19001b34be74SYOSHIFUJI Hideaki } 19011b34be74SYOSHIFUJI Hideaki } 19021b34be74SYOSHIFUJI Hideaki 19035f40ef77SHannes Frederic Sowa errdad: 1904c15b1ccaSHannes Frederic Sowa /* transition from _POSTDAD to _ERRDAD */ 1905c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_ERRDAD; 19068e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 1907c15b1ccaSHannes Frederic Sowa 1908c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 0); 19093c21edbdSYOSHIFUJI Hideaki } 19101da177e4SLinus Torvalds 1911a9ed4a29SSabrina Dubroca /* Join to solicited addr multicast group. 1912a9ed4a29SSabrina Dubroca * caller must hold RTNL */ 1913b71d1d42SEric Dumazet void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr) 19141da177e4SLinus Torvalds { 19151da177e4SLinus Torvalds struct in6_addr maddr; 19161da177e4SLinus Torvalds 19171da177e4SLinus Torvalds if (dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 19181da177e4SLinus Torvalds return; 19191da177e4SLinus Torvalds 19201da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr); 19211da177e4SLinus Torvalds ipv6_dev_mc_inc(dev, &maddr); 19221da177e4SLinus Torvalds } 19231da177e4SLinus Torvalds 1924a9ed4a29SSabrina Dubroca /* caller must hold RTNL */ 1925b71d1d42SEric Dumazet void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr) 19261da177e4SLinus Torvalds { 19271da177e4SLinus Torvalds struct in6_addr maddr; 19281da177e4SLinus Torvalds 19291da177e4SLinus Torvalds if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 19301da177e4SLinus Torvalds return; 19311da177e4SLinus Torvalds 19321da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr); 19331da177e4SLinus Torvalds __ipv6_dev_mc_dec(idev, &maddr); 19341da177e4SLinus Torvalds } 19351da177e4SLinus Torvalds 1936a9ed4a29SSabrina Dubroca /* caller must hold RTNL */ 193720380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp) 19381da177e4SLinus Torvalds { 19391da177e4SLinus Torvalds struct in6_addr addr; 1940c15b1ccaSHannes Frederic Sowa 194188ad3149SHannes Frederic Sowa if (ifp->prefix_len >= 127) /* RFC 6164 */ 19422bda8a0cSBjørn Mork return; 19431da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 19441da177e4SLinus Torvalds if (ipv6_addr_any(&addr)) 19451da177e4SLinus Torvalds return; 1946013b4d90SWANG Cong __ipv6_dev_ac_inc(ifp->idev, &addr); 19471da177e4SLinus Torvalds } 19481da177e4SLinus Torvalds 1949a9ed4a29SSabrina Dubroca /* caller must hold RTNL */ 195020380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp) 19511da177e4SLinus Torvalds { 19521da177e4SLinus Torvalds struct in6_addr addr; 1953c15b1ccaSHannes Frederic Sowa 195488ad3149SHannes Frederic Sowa if (ifp->prefix_len >= 127) /* RFC 6164 */ 195532019e65SYOSHIFUJI Hideaki return; 19561da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 19571da177e4SLinus Torvalds if (ipv6_addr_any(&addr)) 19581da177e4SLinus Torvalds return; 19591da177e4SLinus Torvalds __ipv6_dev_ac_dec(ifp->idev, &addr); 19601da177e4SLinus Torvalds } 19611da177e4SLinus Torvalds 196206a4c1c5Salex.bluesman.smirnov@gmail.com static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev) 196306a4c1c5Salex.bluesman.smirnov@gmail.com { 19645241c2d7SAlexander Aring if (dev->addr_len != EUI64_ADDR_LEN) 196506a4c1c5Salex.bluesman.smirnov@gmail.com return -1; 19665241c2d7SAlexander Aring memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN); 19675e98a36eSYOSHIFUJI Hideaki / 吉藤英明 eui[0] ^= 2; 196806a4c1c5Salex.bluesman.smirnov@gmail.com return 0; 196906a4c1c5Salex.bluesman.smirnov@gmail.com } 197006a4c1c5Salex.bluesman.smirnov@gmail.com 1971cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev) 1972cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 { 1973cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 union fwnet_hwaddr *ha; 1974cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 1975cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 if (dev->addr_len != FWNET_ALEN) 1976cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return -1; 1977cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 1978cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 ha = (union fwnet_hwaddr *)dev->dev_addr; 1979cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 1980cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id)); 1981cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 eui[0] ^= 2; 1982cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return 0; 1983cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 } 1984cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 1985073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev) 1986073a8e0eSYOSHIFUJI Hideaki { 19871da177e4SLinus Torvalds /* XXX: inherit EUI-64 from other interface -- yoshfuji */ 19881da177e4SLinus Torvalds if (dev->addr_len != ARCNET_ALEN) 19891da177e4SLinus Torvalds return -1; 19901da177e4SLinus Torvalds memset(eui, 0, 7); 19911da177e4SLinus Torvalds eui[7] = *(u8 *)dev->dev_addr; 19921da177e4SLinus Torvalds return 0; 1993073a8e0eSYOSHIFUJI Hideaki } 1994073a8e0eSYOSHIFUJI Hideaki 1995073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev) 1996073a8e0eSYOSHIFUJI Hideaki { 19971da177e4SLinus Torvalds if (dev->addr_len != INFINIBAND_ALEN) 19981da177e4SLinus Torvalds return -1; 19991da177e4SLinus Torvalds memcpy(eui, dev->dev_addr + 12, 8); 20001da177e4SLinus Torvalds eui[0] |= 2; 20011da177e4SLinus Torvalds return 0; 20021da177e4SLinus Torvalds } 2003073a8e0eSYOSHIFUJI Hideaki 2004c61393eaSstephen hemminger static int __ipv6_isatap_ifid(u8 *eui, __be32 addr) 2005dfd982baSYOSHIFUJI Hideaki { 20069af28511SSascha Hlusiak if (addr == 0) 20079af28511SSascha Hlusiak return -1; 2008dfd982baSYOSHIFUJI Hideaki eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) || 2009dfd982baSYOSHIFUJI Hideaki ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) || 2010dfd982baSYOSHIFUJI Hideaki ipv4_is_private_172(addr) || ipv4_is_test_192(addr) || 2011dfd982baSYOSHIFUJI Hideaki ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) || 2012dfd982baSYOSHIFUJI Hideaki ipv4_is_test_198(addr) || ipv4_is_multicast(addr) || 2013dfd982baSYOSHIFUJI Hideaki ipv4_is_lbcast(addr)) ? 0x00 : 0x02; 2014dfd982baSYOSHIFUJI Hideaki eui[1] = 0; 2015dfd982baSYOSHIFUJI Hideaki eui[2] = 0x5E; 2016dfd982baSYOSHIFUJI Hideaki eui[3] = 0xFE; 2017dfd982baSYOSHIFUJI Hideaki memcpy(eui + 4, &addr, 4); 2018dfd982baSYOSHIFUJI Hideaki return 0; 2019dfd982baSYOSHIFUJI Hideaki } 2020dfd982baSYOSHIFUJI Hideaki 2021dfd982baSYOSHIFUJI Hideaki static int addrconf_ifid_sit(u8 *eui, struct net_device *dev) 2022dfd982baSYOSHIFUJI Hideaki { 2023dfd982baSYOSHIFUJI Hideaki if (dev->priv_flags & IFF_ISATAP) 2024dfd982baSYOSHIFUJI Hideaki return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); 2025dfd982baSYOSHIFUJI Hideaki return -1; 2026dfd982baSYOSHIFUJI Hideaki } 2027dfd982baSYOSHIFUJI Hideaki 2028aee80b54Sstephen hemminger static int addrconf_ifid_gre(u8 *eui, struct net_device *dev) 2029aee80b54Sstephen hemminger { 2030aee80b54Sstephen hemminger return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); 2031aee80b54Sstephen hemminger } 2032aee80b54Sstephen hemminger 2033e837735eSNicolas Dichtel static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev) 2034e837735eSNicolas Dichtel { 2035e837735eSNicolas Dichtel memcpy(eui, dev->perm_addr, 3); 2036e837735eSNicolas Dichtel memcpy(eui + 5, dev->perm_addr + 3, 3); 2037e837735eSNicolas Dichtel eui[3] = 0xFF; 2038e837735eSNicolas Dichtel eui[4] = 0xFE; 2039e837735eSNicolas Dichtel eui[0] ^= 2; 2040e837735eSNicolas Dichtel return 0; 2041e837735eSNicolas Dichtel } 2042e837735eSNicolas Dichtel 2043073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev) 2044073a8e0eSYOSHIFUJI Hideaki { 2045073a8e0eSYOSHIFUJI Hideaki switch (dev->type) { 2046073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ETHER: 2047073a8e0eSYOSHIFUJI Hideaki case ARPHRD_FDDI: 2048073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_eui48(eui, dev); 2049073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ARCNET: 2050073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_arcnet(eui, dev); 2051073a8e0eSYOSHIFUJI Hideaki case ARPHRD_INFINIBAND: 2052073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_infiniband(eui, dev); 2053c7dc89c0SFred L. Templin case ARPHRD_SIT: 2054dfd982baSYOSHIFUJI Hideaki return addrconf_ifid_sit(eui, dev); 2055aee80b54Sstephen hemminger case ARPHRD_IPGRE: 2056aee80b54Sstephen hemminger return addrconf_ifid_gre(eui, dev); 2057e74bccb8SJukka Rissanen case ARPHRD_6LOWPAN: 205806a4c1c5Salex.bluesman.smirnov@gmail.com return addrconf_ifid_eui64(eui, dev); 2059cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 case ARPHRD_IEEE1394: 2060cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return addrconf_ifid_ieee1394(eui, dev); 2061e837735eSNicolas Dichtel case ARPHRD_TUNNEL6: 2062e837735eSNicolas Dichtel return addrconf_ifid_ip6tnl(eui, dev); 2063073a8e0eSYOSHIFUJI Hideaki } 20641da177e4SLinus Torvalds return -1; 20651da177e4SLinus Torvalds } 20661da177e4SLinus Torvalds 20671da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev) 20681da177e4SLinus Torvalds { 20691da177e4SLinus Torvalds int err = -1; 20701da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 20711da177e4SLinus Torvalds 20721da177e4SLinus Torvalds read_lock_bh(&idev->lock); 2073602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) { 2074602582caSHannes Frederic Sowa if (ifp->scope > IFA_LINK) 2075602582caSHannes Frederic Sowa break; 20761da177e4SLinus Torvalds if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) { 20771da177e4SLinus Torvalds memcpy(eui, ifp->addr.s6_addr+8, 8); 20781da177e4SLinus Torvalds err = 0; 20791da177e4SLinus Torvalds break; 20801da177e4SLinus Torvalds } 20811da177e4SLinus Torvalds } 20821da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 20831da177e4SLinus Torvalds return err; 20841da177e4SLinus Torvalds } 20851da177e4SLinus Torvalds 20861da177e4SLinus Torvalds /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */ 2087eb7e0575SSorin Dumitru static void __ipv6_regen_rndid(struct inet6_dev *idev) 20881da177e4SLinus Torvalds { 20891da177e4SLinus Torvalds regen: 2090955189efSYOSHIFUJI Hideaki get_random_bytes(idev->rndid, sizeof(idev->rndid)); 20911da177e4SLinus Torvalds idev->rndid[0] &= ~0x02; 20921da177e4SLinus Torvalds 20931da177e4SLinus Torvalds /* 20941da177e4SLinus Torvalds * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>: 20951da177e4SLinus Torvalds * check if generated address is not inappropriate 20961da177e4SLinus Torvalds * 20971da177e4SLinus Torvalds * - Reserved subnet anycast (RFC 2526) 20981da177e4SLinus Torvalds * 11111101 11....11 1xxxxxxx 2099c7dc89c0SFred L. Templin * - ISATAP (RFC4214) 6.1 21001da177e4SLinus Torvalds * 00-00-5E-FE-xx-xx-xx-xx 21011da177e4SLinus Torvalds * - value 0 21021da177e4SLinus Torvalds * - XXX: already assigned to an address on the device 21031da177e4SLinus Torvalds */ 21041da177e4SLinus Torvalds if (idev->rndid[0] == 0xfd && 21051da177e4SLinus Torvalds (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff && 21061da177e4SLinus Torvalds (idev->rndid[7]&0x80)) 21071da177e4SLinus Torvalds goto regen; 21081da177e4SLinus Torvalds if ((idev->rndid[0]|idev->rndid[1]) == 0) { 21091da177e4SLinus Torvalds if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe) 21101da177e4SLinus Torvalds goto regen; 21111da177e4SLinus Torvalds if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00) 21121da177e4SLinus Torvalds goto regen; 21131da177e4SLinus Torvalds } 21141da177e4SLinus Torvalds } 21151da177e4SLinus Torvalds 21161da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data) 21171da177e4SLinus Torvalds { 21181da177e4SLinus Torvalds struct inet6_dev *idev = (struct inet6_dev *) data; 21191da177e4SLinus Torvalds unsigned long expires; 21201da177e4SLinus Torvalds 21218814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 21221da177e4SLinus Torvalds write_lock_bh(&idev->lock); 21231da177e4SLinus Torvalds 21241da177e4SLinus Torvalds if (idev->dead) 21251da177e4SLinus Torvalds goto out; 21261da177e4SLinus Torvalds 2127eb7e0575SSorin Dumitru __ipv6_regen_rndid(idev); 21281da177e4SLinus Torvalds 21291da177e4SLinus Torvalds expires = jiffies + 21301da177e4SLinus Torvalds idev->cnf.temp_prefered_lft * HZ - 21311f9248e5SJiri Pirko idev->cnf.regen_max_retry * idev->cnf.dad_transmits * 21321f9248e5SJiri Pirko NEIGH_VAR(idev->nd_parms, RETRANS_TIME) - 2133784e2710SBen Hutchings idev->cnf.max_desync_factor * HZ; 21341da177e4SLinus Torvalds if (time_before(expires, jiffies)) { 2135f3213831SJoe Perches pr_warn("%s: too short regeneration interval; timer disabled for %s\n", 2136f3213831SJoe Perches __func__, idev->dev->name); 21371da177e4SLinus Torvalds goto out; 21381da177e4SLinus Torvalds } 21391da177e4SLinus Torvalds 21401da177e4SLinus Torvalds if (!mod_timer(&idev->regen_timer, expires)) 21411da177e4SLinus Torvalds in6_dev_hold(idev); 21421da177e4SLinus Torvalds 21431da177e4SLinus Torvalds out: 21441da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 21458814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 21461da177e4SLinus Torvalds in6_dev_put(idev); 21471da177e4SLinus Torvalds } 21481da177e4SLinus Torvalds 2149eb7e0575SSorin Dumitru static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) 21508e5e8f30SEldad Zack { 21511da177e4SLinus Torvalds if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0) 2152eb7e0575SSorin Dumitru __ipv6_regen_rndid(idev); 21531da177e4SLinus Torvalds } 21541da177e4SLinus Torvalds 21551da177e4SLinus Torvalds /* 21561da177e4SLinus Torvalds * Add prefix route. 21571da177e4SLinus Torvalds */ 21581da177e4SLinus Torvalds 21591da177e4SLinus Torvalds static void 21601da177e4SLinus Torvalds addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev, 2161e431b8c0SJamal Hadi Salim unsigned long expires, u32 flags) 21621da177e4SLinus Torvalds { 216386872cb5SThomas Graf struct fib6_config cfg = { 2164ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX, 216586872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 216686872cb5SThomas Graf .fc_ifindex = dev->ifindex, 216786872cb5SThomas Graf .fc_expires = expires, 216886872cb5SThomas Graf .fc_dst_len = plen, 216986872cb5SThomas Graf .fc_flags = RTF_UP | flags, 2170c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 2171f410a1fbSStephen Hemminger .fc_protocol = RTPROT_KERNEL, 217286872cb5SThomas Graf }; 21731da177e4SLinus Torvalds 21744e3fd7a0SAlexey Dobriyan cfg.fc_dst = *pfx; 21751da177e4SLinus Torvalds 21761da177e4SLinus Torvalds /* Prevent useless cloning on PtP SIT. 21771da177e4SLinus Torvalds This thing is done here expecting that the whole 21781da177e4SLinus Torvalds class of non-broadcast devices need not cloning. 21791da177e4SLinus Torvalds */ 218007a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 21811da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT)) 218286872cb5SThomas Graf cfg.fc_flags |= RTF_NONEXTHOP; 21830be669bbSJoerg Roedel #endif 21841da177e4SLinus Torvalds 218586872cb5SThomas Graf ip6_route_add(&cfg); 21861da177e4SLinus Torvalds } 21871da177e4SLinus Torvalds 218814ef37b6SAndreas Hofmeister 218914ef37b6SAndreas Hofmeister static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx, 219014ef37b6SAndreas Hofmeister int plen, 219114ef37b6SAndreas Hofmeister const struct net_device *dev, 219214ef37b6SAndreas Hofmeister u32 flags, u32 noflags) 219314ef37b6SAndreas Hofmeister { 219414ef37b6SAndreas Hofmeister struct fib6_node *fn; 219514ef37b6SAndreas Hofmeister struct rt6_info *rt = NULL; 219614ef37b6SAndreas Hofmeister struct fib6_table *table; 2197ca254490SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX; 219814ef37b6SAndreas Hofmeister 2199ca254490SDavid Ahern table = fib6_get_table(dev_net(dev), tb_id); 220063159f29SIan Morris if (!table) 220114ef37b6SAndreas Hofmeister return NULL; 220214ef37b6SAndreas Hofmeister 22035744dd9bSLi RongQing read_lock_bh(&table->tb6_lock); 220414ef37b6SAndreas Hofmeister fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0); 220514ef37b6SAndreas Hofmeister if (!fn) 220614ef37b6SAndreas Hofmeister goto out; 22071f56a01fSMartin KaFai Lau 22081f56a01fSMartin KaFai Lau noflags |= RTF_CACHE; 220914ef37b6SAndreas Hofmeister for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 2210d1918542SDavid S. Miller if (rt->dst.dev->ifindex != dev->ifindex) 221114ef37b6SAndreas Hofmeister continue; 221214ef37b6SAndreas Hofmeister if ((rt->rt6i_flags & flags) != flags) 221314ef37b6SAndreas Hofmeister continue; 221485da53bfSRomain Kuntz if ((rt->rt6i_flags & noflags) != 0) 221514ef37b6SAndreas Hofmeister continue; 221614ef37b6SAndreas Hofmeister dst_hold(&rt->dst); 221714ef37b6SAndreas Hofmeister break; 221814ef37b6SAndreas Hofmeister } 221914ef37b6SAndreas Hofmeister out: 22205744dd9bSLi RongQing read_unlock_bh(&table->tb6_lock); 222114ef37b6SAndreas Hofmeister return rt; 222214ef37b6SAndreas Hofmeister } 222314ef37b6SAndreas Hofmeister 222414ef37b6SAndreas Hofmeister 22251da177e4SLinus Torvalds /* Create "default" multicast route to the interface */ 22261da177e4SLinus Torvalds 22271da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev) 22281da177e4SLinus Torvalds { 222986872cb5SThomas Graf struct fib6_config cfg = { 2230ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL, 223186872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 223286872cb5SThomas Graf .fc_ifindex = dev->ifindex, 223386872cb5SThomas Graf .fc_dst_len = 8, 223486872cb5SThomas Graf .fc_flags = RTF_UP, 2235c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 223686872cb5SThomas Graf }; 22371da177e4SLinus Torvalds 223886872cb5SThomas Graf ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0); 223986872cb5SThomas Graf 224086872cb5SThomas Graf ip6_route_add(&cfg); 22411da177e4SLinus Torvalds } 22421da177e4SLinus Torvalds 22431da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev) 22441da177e4SLinus Torvalds { 22451da177e4SLinus Torvalds struct inet6_dev *idev; 22461da177e4SLinus Torvalds 22471da177e4SLinus Torvalds ASSERT_RTNL(); 22481da177e4SLinus Torvalds 2249e21e8467SStephen Hemminger idev = ipv6_find_idev(dev); 2250e21e8467SStephen Hemminger if (!idev) 225164e724f6SBrian Haley return ERR_PTR(-ENOBUFS); 225264e724f6SBrian Haley 225364e724f6SBrian Haley if (idev->cnf.disable_ipv6) 225464e724f6SBrian Haley return ERR_PTR(-EACCES); 22551da177e4SLinus Torvalds 22561da177e4SLinus Torvalds /* Add default multicast route */ 2257ba46ee4cSDavid Ahern if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev)) 22581da177e4SLinus Torvalds addrconf_add_mroute(dev); 22591da177e4SLinus Torvalds 22601da177e4SLinus Torvalds return idev; 22611da177e4SLinus Torvalds } 22621da177e4SLinus Torvalds 226353bd6749SJiri Pirko static void manage_tempaddrs(struct inet6_dev *idev, 226453bd6749SJiri Pirko struct inet6_ifaddr *ifp, 226553bd6749SJiri Pirko __u32 valid_lft, __u32 prefered_lft, 226653bd6749SJiri Pirko bool create, unsigned long now) 226753bd6749SJiri Pirko { 226853bd6749SJiri Pirko u32 flags; 226953bd6749SJiri Pirko struct inet6_ifaddr *ift; 227053bd6749SJiri Pirko 227153bd6749SJiri Pirko read_lock_bh(&idev->lock); 227253bd6749SJiri Pirko /* update all temporary addresses in the list */ 227353bd6749SJiri Pirko list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) { 227453bd6749SJiri Pirko int age, max_valid, max_prefered; 227553bd6749SJiri Pirko 227653bd6749SJiri Pirko if (ifp != ift->ifpub) 227753bd6749SJiri Pirko continue; 227853bd6749SJiri Pirko 227953bd6749SJiri Pirko /* RFC 4941 section 3.3: 228053bd6749SJiri Pirko * If a received option will extend the lifetime of a public 228153bd6749SJiri Pirko * address, the lifetimes of temporary addresses should 228253bd6749SJiri Pirko * be extended, subject to the overall constraint that no 228353bd6749SJiri Pirko * temporary addresses should ever remain "valid" or "preferred" 228453bd6749SJiri Pirko * for a time longer than (TEMP_VALID_LIFETIME) or 228553bd6749SJiri Pirko * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively. 228653bd6749SJiri Pirko */ 228753bd6749SJiri Pirko age = (now - ift->cstamp) / HZ; 228853bd6749SJiri Pirko max_valid = idev->cnf.temp_valid_lft - age; 228953bd6749SJiri Pirko if (max_valid < 0) 229053bd6749SJiri Pirko max_valid = 0; 229153bd6749SJiri Pirko 229253bd6749SJiri Pirko max_prefered = idev->cnf.temp_prefered_lft - 229353bd6749SJiri Pirko idev->cnf.max_desync_factor - age; 229453bd6749SJiri Pirko if (max_prefered < 0) 229553bd6749SJiri Pirko max_prefered = 0; 229653bd6749SJiri Pirko 229753bd6749SJiri Pirko if (valid_lft > max_valid) 229853bd6749SJiri Pirko valid_lft = max_valid; 229953bd6749SJiri Pirko 230053bd6749SJiri Pirko if (prefered_lft > max_prefered) 230153bd6749SJiri Pirko prefered_lft = max_prefered; 230253bd6749SJiri Pirko 230353bd6749SJiri Pirko spin_lock(&ift->lock); 230453bd6749SJiri Pirko flags = ift->flags; 230553bd6749SJiri Pirko ift->valid_lft = valid_lft; 230653bd6749SJiri Pirko ift->prefered_lft = prefered_lft; 230753bd6749SJiri Pirko ift->tstamp = now; 230853bd6749SJiri Pirko if (prefered_lft > 0) 230953bd6749SJiri Pirko ift->flags &= ~IFA_F_DEPRECATED; 231053bd6749SJiri Pirko 231153bd6749SJiri Pirko spin_unlock(&ift->lock); 231253bd6749SJiri Pirko if (!(flags&IFA_F_TENTATIVE)) 231353bd6749SJiri Pirko ipv6_ifa_notify(0, ift); 231453bd6749SJiri Pirko } 231553bd6749SJiri Pirko 231653bd6749SJiri Pirko if ((create || list_empty(&idev->tempaddr_list)) && 231753bd6749SJiri Pirko idev->cnf.use_tempaddr > 0) { 231853bd6749SJiri Pirko /* When a new public address is created as described 231953bd6749SJiri Pirko * in [ADDRCONF], also create a new temporary address. 232053bd6749SJiri Pirko * Also create a temporary address if it's enabled but 232153bd6749SJiri Pirko * no temporary address currently exists. 232253bd6749SJiri Pirko */ 232353bd6749SJiri Pirko read_unlock_bh(&idev->lock); 232453bd6749SJiri Pirko ipv6_create_tempaddr(ifp, NULL); 232553bd6749SJiri Pirko } else { 232653bd6749SJiri Pirko read_unlock_bh(&idev->lock); 232753bd6749SJiri Pirko } 232853bd6749SJiri Pirko } 232953bd6749SJiri Pirko 2330cc9da6ccSBjørn Mork static bool is_addr_mode_generate_stable(struct inet6_dev *idev) 2331cc9da6ccSBjørn Mork { 2332cc9da6ccSBjørn Mork return idev->addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY || 2333cc9da6ccSBjørn Mork idev->addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM; 2334cc9da6ccSBjørn Mork } 2335cc9da6ccSBjørn Mork 2336*cc84b3c6SAlexander Aring int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev, 23374f672235SAlexander Aring const struct prefix_info *pinfo, 23384f672235SAlexander Aring struct inet6_dev *in6_dev, 2339*cc84b3c6SAlexander Aring const struct in6_addr *addr, int addr_type, 2340*cc84b3c6SAlexander Aring u32 addr_flags, bool sllao, bool tokenized, 23414f672235SAlexander Aring __u32 valid_lft, u32 prefered_lft) 23424f672235SAlexander Aring { 23434f672235SAlexander Aring struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1); 23444f672235SAlexander Aring int create = 0, update_lft = 0; 23454f672235SAlexander Aring 23464f672235SAlexander Aring if (!ifp && valid_lft) { 23474f672235SAlexander Aring int max_addresses = in6_dev->cnf.max_addresses; 23484f672235SAlexander Aring 23494f672235SAlexander Aring #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 23504f672235SAlexander Aring if (in6_dev->cnf.optimistic_dad && 23514f672235SAlexander Aring !net->ipv6.devconf_all->forwarding && sllao) 23524f672235SAlexander Aring addr_flags |= IFA_F_OPTIMISTIC; 23534f672235SAlexander Aring #endif 23544f672235SAlexander Aring 23554f672235SAlexander Aring /* Do not allow to create too much of autoconfigured 23564f672235SAlexander Aring * addresses; this would be too easy way to crash kernel. 23574f672235SAlexander Aring */ 23584f672235SAlexander Aring if (!max_addresses || 23594f672235SAlexander Aring ipv6_count_addresses(in6_dev) < max_addresses) 23604f672235SAlexander Aring ifp = ipv6_add_addr(in6_dev, addr, NULL, 23614f672235SAlexander Aring pinfo->prefix_len, 23624f672235SAlexander Aring addr_type&IPV6_ADDR_SCOPE_MASK, 23634f672235SAlexander Aring addr_flags, valid_lft, 23644f672235SAlexander Aring prefered_lft); 23654f672235SAlexander Aring 23664f672235SAlexander Aring if (IS_ERR_OR_NULL(ifp)) 23674f672235SAlexander Aring return -1; 23684f672235SAlexander Aring 23694f672235SAlexander Aring update_lft = 0; 23704f672235SAlexander Aring create = 1; 23714f672235SAlexander Aring spin_lock_bh(&ifp->lock); 23724f672235SAlexander Aring ifp->flags |= IFA_F_MANAGETEMPADDR; 23734f672235SAlexander Aring ifp->cstamp = jiffies; 23744f672235SAlexander Aring ifp->tokenized = tokenized; 23754f672235SAlexander Aring spin_unlock_bh(&ifp->lock); 23764f672235SAlexander Aring addrconf_dad_start(ifp); 23774f672235SAlexander Aring } 23784f672235SAlexander Aring 23794f672235SAlexander Aring if (ifp) { 23804f672235SAlexander Aring u32 flags; 23814f672235SAlexander Aring unsigned long now; 23824f672235SAlexander Aring u32 stored_lft; 23834f672235SAlexander Aring 23844f672235SAlexander Aring /* update lifetime (RFC2462 5.5.3 e) */ 23854f672235SAlexander Aring spin_lock_bh(&ifp->lock); 23864f672235SAlexander Aring now = jiffies; 23874f672235SAlexander Aring if (ifp->valid_lft > (now - ifp->tstamp) / HZ) 23884f672235SAlexander Aring stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ; 23894f672235SAlexander Aring else 23904f672235SAlexander Aring stored_lft = 0; 23914f672235SAlexander Aring if (!update_lft && !create && stored_lft) { 23924f672235SAlexander Aring const u32 minimum_lft = min_t(u32, 23934f672235SAlexander Aring stored_lft, MIN_VALID_LIFETIME); 23944f672235SAlexander Aring valid_lft = max(valid_lft, minimum_lft); 23954f672235SAlexander Aring 23964f672235SAlexander Aring /* RFC4862 Section 5.5.3e: 23974f672235SAlexander Aring * "Note that the preferred lifetime of the 23984f672235SAlexander Aring * corresponding address is always reset to 23994f672235SAlexander Aring * the Preferred Lifetime in the received 24004f672235SAlexander Aring * Prefix Information option, regardless of 24014f672235SAlexander Aring * whether the valid lifetime is also reset or 24024f672235SAlexander Aring * ignored." 24034f672235SAlexander Aring * 24044f672235SAlexander Aring * So we should always update prefered_lft here. 24054f672235SAlexander Aring */ 24064f672235SAlexander Aring update_lft = 1; 24074f672235SAlexander Aring } 24084f672235SAlexander Aring 24094f672235SAlexander Aring if (update_lft) { 24104f672235SAlexander Aring ifp->valid_lft = valid_lft; 24114f672235SAlexander Aring ifp->prefered_lft = prefered_lft; 24124f672235SAlexander Aring ifp->tstamp = now; 24134f672235SAlexander Aring flags = ifp->flags; 24144f672235SAlexander Aring ifp->flags &= ~IFA_F_DEPRECATED; 24154f672235SAlexander Aring spin_unlock_bh(&ifp->lock); 24164f672235SAlexander Aring 24174f672235SAlexander Aring if (!(flags&IFA_F_TENTATIVE)) 24184f672235SAlexander Aring ipv6_ifa_notify(0, ifp); 24194f672235SAlexander Aring } else 24204f672235SAlexander Aring spin_unlock_bh(&ifp->lock); 24214f672235SAlexander Aring 24224f672235SAlexander Aring manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft, 24234f672235SAlexander Aring create, now); 24244f672235SAlexander Aring 24254f672235SAlexander Aring in6_ifa_put(ifp); 24264f672235SAlexander Aring addrconf_verify(); 24274f672235SAlexander Aring } 24284f672235SAlexander Aring 24294f672235SAlexander Aring return 0; 24304f672235SAlexander Aring } 2431*cc84b3c6SAlexander Aring EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr); 24324f672235SAlexander Aring 2433e6bff995SNeil Horman void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao) 24341da177e4SLinus Torvalds { 24351da177e4SLinus Torvalds struct prefix_info *pinfo; 24361da177e4SLinus Torvalds __u32 valid_lft; 24371da177e4SLinus Torvalds __u32 prefered_lft; 24384f672235SAlexander Aring int addr_type, err; 243964236f3fSHannes Frederic Sowa u32 addr_flags = 0; 24401da177e4SLinus Torvalds struct inet6_dev *in6_dev; 244156d417b1SBrian Haley struct net *net = dev_net(dev); 24421da177e4SLinus Torvalds 24431da177e4SLinus Torvalds pinfo = (struct prefix_info *) opt; 24441da177e4SLinus Torvalds 24451da177e4SLinus Torvalds if (len < sizeof(struct prefix_info)) { 2446ba3542e1Sdingtianhong ADBG("addrconf: prefix option too short\n"); 24471da177e4SLinus Torvalds return; 24481da177e4SLinus Torvalds } 24491da177e4SLinus Torvalds 24501da177e4SLinus Torvalds /* 24511da177e4SLinus Torvalds * Validation checks ([ADDRCONF], page 19) 24521da177e4SLinus Torvalds */ 24531da177e4SLinus Torvalds 24541da177e4SLinus Torvalds addr_type = ipv6_addr_type(&pinfo->prefix); 24551da177e4SLinus Torvalds 24561da177e4SLinus Torvalds if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)) 24571da177e4SLinus Torvalds return; 24581da177e4SLinus Torvalds 24591da177e4SLinus Torvalds valid_lft = ntohl(pinfo->valid); 24601da177e4SLinus Torvalds prefered_lft = ntohl(pinfo->prefered); 24611da177e4SLinus Torvalds 24621da177e4SLinus Torvalds if (prefered_lft > valid_lft) { 2463e87cc472SJoe Perches net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n"); 24641da177e4SLinus Torvalds return; 24651da177e4SLinus Torvalds } 24661da177e4SLinus Torvalds 24671da177e4SLinus Torvalds in6_dev = in6_dev_get(dev); 24681da177e4SLinus Torvalds 246963159f29SIan Morris if (!in6_dev) { 2470e87cc472SJoe Perches net_dbg_ratelimited("addrconf: device %s not configured\n", 2471e87cc472SJoe Perches dev->name); 24721da177e4SLinus Torvalds return; 24731da177e4SLinus Torvalds } 24741da177e4SLinus Torvalds 24751da177e4SLinus Torvalds /* 24761da177e4SLinus Torvalds * Two things going on here: 24771da177e4SLinus Torvalds * 1) Add routes for on-link prefixes 24781da177e4SLinus Torvalds * 2) Configure prefixes with the auto flag set 24791da177e4SLinus Torvalds */ 24801da177e4SLinus Torvalds 24814bed72e4SYOSHIFUJI Hideaki if (pinfo->onlink) { 24824bed72e4SYOSHIFUJI Hideaki struct rt6_info *rt; 24834bed72e4SYOSHIFUJI Hideaki unsigned long rt_expires; 24844bed72e4SYOSHIFUJI Hideaki 24851da177e4SLinus Torvalds /* Avoid arithmetic overflow. Really, we could 24866f704992SYOSHIFUJI Hideaki * save rt_expires in seconds, likely valid_lft, 24876f704992SYOSHIFUJI Hideaki * but it would require division in fib gc, that it 24886f704992SYOSHIFUJI Hideaki * not good. 24891da177e4SLinus Torvalds */ 24904bed72e4SYOSHIFUJI Hideaki if (HZ > USER_HZ) 24914bed72e4SYOSHIFUJI Hideaki rt_expires = addrconf_timeout_fixup(valid_lft, HZ); 24924bed72e4SYOSHIFUJI Hideaki else 24934bed72e4SYOSHIFUJI Hideaki rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ); 24943dd4bc68SYOSHIFUJI Hideaki 24954bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(rt_expires)) 24964bed72e4SYOSHIFUJI Hideaki rt_expires *= HZ; 24971da177e4SLinus Torvalds 249814ef37b6SAndreas Hofmeister rt = addrconf_get_prefix_route(&pinfo->prefix, 249914ef37b6SAndreas Hofmeister pinfo->prefix_len, 250014ef37b6SAndreas Hofmeister dev, 250114ef37b6SAndreas Hofmeister RTF_ADDRCONF | RTF_PREFIX_RT, 250214ef37b6SAndreas Hofmeister RTF_GATEWAY | RTF_DEFAULT); 25031da177e4SLinus Torvalds 250414ef37b6SAndreas Hofmeister if (rt) { 25056f704992SYOSHIFUJI Hideaki /* Autoconf prefix route */ 25061da177e4SLinus Torvalds if (valid_lft == 0) { 2507e0a1ad73SThomas Graf ip6_del_rt(rt); 25081da177e4SLinus Torvalds rt = NULL; 25094bed72e4SYOSHIFUJI Hideaki } else if (addrconf_finite_timeout(rt_expires)) { 25106f704992SYOSHIFUJI Hideaki /* not infinity */ 25111716a961SGao feng rt6_set_expires(rt, jiffies + rt_expires); 25126f704992SYOSHIFUJI Hideaki } else { 25131716a961SGao feng rt6_clean_expires(rt); 25141da177e4SLinus Torvalds } 25151da177e4SLinus Torvalds } else if (valid_lft) { 25166f704992SYOSHIFUJI Hideaki clock_t expires = 0; 25174bed72e4SYOSHIFUJI Hideaki int flags = RTF_ADDRCONF | RTF_PREFIX_RT; 25184bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(rt_expires)) { 25196f704992SYOSHIFUJI Hideaki /* not infinity */ 25206f704992SYOSHIFUJI Hideaki flags |= RTF_EXPIRES; 25216f704992SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(rt_expires); 25226f704992SYOSHIFUJI Hideaki } 25231da177e4SLinus Torvalds addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len, 25246f704992SYOSHIFUJI Hideaki dev, expires, flags); 25251da177e4SLinus Torvalds } 252694e187c0SAmerigo Wang ip6_rt_put(rt); 25271da177e4SLinus Torvalds } 25281da177e4SLinus Torvalds 25291da177e4SLinus Torvalds /* Try to figure out our local address for this prefix */ 25301da177e4SLinus Torvalds 25311da177e4SLinus Torvalds if (pinfo->autoconf && in6_dev->cnf.autoconf) { 25321da177e4SLinus Torvalds struct in6_addr addr; 2533f997c55cSAlexander Aring bool tokenized = false, dev_addr_generated = false; 25341da177e4SLinus Torvalds 25351da177e4SLinus Torvalds if (pinfo->prefix_len == 64) { 25361da177e4SLinus Torvalds memcpy(&addr, &pinfo->prefix, 8); 2537f53adae4SDaniel Borkmann 2538f53adae4SDaniel Borkmann if (!ipv6_addr_any(&in6_dev->token)) { 2539f53adae4SDaniel Borkmann read_lock_bh(&in6_dev->lock); 2540f53adae4SDaniel Borkmann memcpy(addr.s6_addr + 8, 2541f53adae4SDaniel Borkmann in6_dev->token.s6_addr + 8, 8); 2542f53adae4SDaniel Borkmann read_unlock_bh(&in6_dev->lock); 2543617fe29dSDaniel Borkmann tokenized = true; 2544cc9da6ccSBjørn Mork } else if (is_addr_mode_generate_stable(in6_dev) && 2545622c81d5SHannes Frederic Sowa !ipv6_generate_stable_address(&addr, 0, 2546622c81d5SHannes Frederic Sowa in6_dev)) { 254764236f3fSHannes Frederic Sowa addr_flags |= IFA_F_STABLE_PRIVACY; 2548622c81d5SHannes Frederic Sowa goto ok; 2549f53adae4SDaniel Borkmann } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) && 25501da177e4SLinus Torvalds ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) { 25514f672235SAlexander Aring goto put; 2552f997c55cSAlexander Aring } else { 2553f997c55cSAlexander Aring dev_addr_generated = true; 25541da177e4SLinus Torvalds } 25551da177e4SLinus Torvalds goto ok; 25561da177e4SLinus Torvalds } 2557e87cc472SJoe Perches net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n", 25581da177e4SLinus Torvalds pinfo->prefix_len); 25594f672235SAlexander Aring goto put; 25601da177e4SLinus Torvalds 25611da177e4SLinus Torvalds ok: 25624f672235SAlexander Aring err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, 25634f672235SAlexander Aring &addr, addr_type, 25644f672235SAlexander Aring addr_flags, sllao, 25654f672235SAlexander Aring tokenized, valid_lft, 25668a226b2cSJiri Benc prefered_lft); 25674f672235SAlexander Aring if (err) 25684f672235SAlexander Aring goto put; 2569f997c55cSAlexander Aring 2570f997c55cSAlexander Aring /* Ignore error case here because previous prefix add addr was 2571f997c55cSAlexander Aring * successful which will be notified. 2572f997c55cSAlexander Aring */ 2573f997c55cSAlexander Aring ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr, 2574f997c55cSAlexander Aring addr_type, addr_flags, sllao, 2575f997c55cSAlexander Aring tokenized, valid_lft, 2576f997c55cSAlexander Aring prefered_lft, 2577f997c55cSAlexander Aring dev_addr_generated); 25781da177e4SLinus Torvalds } 25791da177e4SLinus Torvalds inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo); 25804f672235SAlexander Aring put: 25811da177e4SLinus Torvalds in6_dev_put(in6_dev); 25821da177e4SLinus Torvalds } 25831da177e4SLinus Torvalds 25841da177e4SLinus Torvalds /* 25851da177e4SLinus Torvalds * Set destination address. 25861da177e4SLinus Torvalds * Special case for SIT interfaces where we create a new "virtual" 25871da177e4SLinus Torvalds * device. 25881da177e4SLinus Torvalds */ 2589af284937SDaniel Lezcano int addrconf_set_dstaddr(struct net *net, void __user *arg) 25901da177e4SLinus Torvalds { 25911da177e4SLinus Torvalds struct in6_ifreq ireq; 25921da177e4SLinus Torvalds struct net_device *dev; 25931da177e4SLinus Torvalds int err = -EINVAL; 25941da177e4SLinus Torvalds 25951da177e4SLinus Torvalds rtnl_lock(); 25961da177e4SLinus Torvalds 25971da177e4SLinus Torvalds err = -EFAULT; 25981da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 25991da177e4SLinus Torvalds goto err_exit; 26001da177e4SLinus Torvalds 2601af284937SDaniel Lezcano dev = __dev_get_by_index(net, ireq.ifr6_ifindex); 26021da177e4SLinus Torvalds 26031da177e4SLinus Torvalds err = -ENODEV; 260463159f29SIan Morris if (!dev) 26051da177e4SLinus Torvalds goto err_exit; 26061da177e4SLinus Torvalds 260707a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 26081da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT) { 26095bc3eb7eSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 26101da177e4SLinus Torvalds struct ifreq ifr; 26111da177e4SLinus Torvalds struct ip_tunnel_parm p; 26121da177e4SLinus Torvalds 26131da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 26141da177e4SLinus Torvalds if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4)) 26151da177e4SLinus Torvalds goto err_exit; 26161da177e4SLinus Torvalds 26171da177e4SLinus Torvalds memset(&p, 0, sizeof(p)); 26181da177e4SLinus Torvalds p.iph.daddr = ireq.ifr6_addr.s6_addr32[3]; 26191da177e4SLinus Torvalds p.iph.saddr = 0; 26201da177e4SLinus Torvalds p.iph.version = 4; 26211da177e4SLinus Torvalds p.iph.ihl = 5; 26221da177e4SLinus Torvalds p.iph.protocol = IPPROTO_IPV6; 26231da177e4SLinus Torvalds p.iph.ttl = 64; 2624d20b3109SStephen Hemminger ifr.ifr_ifru.ifru_data = (__force void __user *)&p; 26251da177e4SLinus Torvalds 26265bc3eb7eSStephen Hemminger if (ops->ndo_do_ioctl) { 26275bc3eb7eSStephen Hemminger mm_segment_t oldfs = get_fs(); 26285bc3eb7eSStephen Hemminger 26295bc3eb7eSStephen Hemminger set_fs(KERNEL_DS); 26305bc3eb7eSStephen Hemminger err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL); 26311da177e4SLinus Torvalds set_fs(oldfs); 26325bc3eb7eSStephen Hemminger } else 26335bc3eb7eSStephen Hemminger err = -EOPNOTSUPP; 26341da177e4SLinus Torvalds 26351da177e4SLinus Torvalds if (err == 0) { 26361da177e4SLinus Torvalds err = -ENOBUFS; 2637af284937SDaniel Lezcano dev = __dev_get_by_name(net, p.name); 2638af284937SDaniel Lezcano if (!dev) 26391da177e4SLinus Torvalds goto err_exit; 26401da177e4SLinus Torvalds err = dev_open(dev); 26411da177e4SLinus Torvalds } 26421da177e4SLinus Torvalds } 26430be669bbSJoerg Roedel #endif 26441da177e4SLinus Torvalds 26451da177e4SLinus Torvalds err_exit: 26461da177e4SLinus Torvalds rtnl_unlock(); 26471da177e4SLinus Torvalds return err; 26481da177e4SLinus Torvalds } 26491da177e4SLinus Torvalds 265093a714d6SMadhu Challa static int ipv6_mc_config(struct sock *sk, bool join, 265193a714d6SMadhu Challa const struct in6_addr *addr, int ifindex) 265293a714d6SMadhu Challa { 265393a714d6SMadhu Challa int ret; 265493a714d6SMadhu Challa 265593a714d6SMadhu Challa ASSERT_RTNL(); 265693a714d6SMadhu Challa 265793a714d6SMadhu Challa lock_sock(sk); 265893a714d6SMadhu Challa if (join) 265954ff9ef3SMarcelo Ricardo Leitner ret = ipv6_sock_mc_join(sk, ifindex, addr); 266093a714d6SMadhu Challa else 266154ff9ef3SMarcelo Ricardo Leitner ret = ipv6_sock_mc_drop(sk, ifindex, addr); 266293a714d6SMadhu Challa release_sock(sk); 266393a714d6SMadhu Challa 266493a714d6SMadhu Challa return ret; 266593a714d6SMadhu Challa } 266693a714d6SMadhu Challa 26671da177e4SLinus Torvalds /* 26681da177e4SLinus Torvalds * Manual configuration of address on an interface 26691da177e4SLinus Torvalds */ 2670479840ffSJiri Pirko static int inet6_addr_add(struct net *net, int ifindex, 2671479840ffSJiri Pirko const struct in6_addr *pfx, 2672caeaba79SNicolas Dichtel const struct in6_addr *peer_pfx, 2673479840ffSJiri Pirko unsigned int plen, __u32 ifa_flags, 2674479840ffSJiri Pirko __u32 prefered_lft, __u32 valid_lft) 26751da177e4SLinus Torvalds { 26761da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 26771da177e4SLinus Torvalds struct inet6_dev *idev; 26781da177e4SLinus Torvalds struct net_device *dev; 267993a714d6SMadhu Challa unsigned long timeout; 268093a714d6SMadhu Challa clock_t expires; 26811da177e4SLinus Torvalds int scope; 26826f704992SYOSHIFUJI Hideaki u32 flags; 26831da177e4SLinus Torvalds 26841da177e4SLinus Torvalds ASSERT_RTNL(); 26851da177e4SLinus Torvalds 268624ef0da7SThomas Graf if (plen > 128) 268724ef0da7SThomas Graf return -EINVAL; 268824ef0da7SThomas Graf 26890778769dSNoriaki TAKAMIYA /* check the lifetime */ 26900778769dSNoriaki TAKAMIYA if (!valid_lft || prefered_lft > valid_lft) 26910778769dSNoriaki TAKAMIYA return -EINVAL; 26920778769dSNoriaki TAKAMIYA 269353bd6749SJiri Pirko if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64) 269453bd6749SJiri Pirko return -EINVAL; 269553bd6749SJiri Pirko 2696af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifindex); 2697af284937SDaniel Lezcano if (!dev) 26981da177e4SLinus Torvalds return -ENODEV; 26991da177e4SLinus Torvalds 270064e724f6SBrian Haley idev = addrconf_add_dev(dev); 270164e724f6SBrian Haley if (IS_ERR(idev)) 270264e724f6SBrian Haley return PTR_ERR(idev); 27031da177e4SLinus Torvalds 270493a714d6SMadhu Challa if (ifa_flags & IFA_F_MCAUTOJOIN) { 270593a714d6SMadhu Challa int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk, 270693a714d6SMadhu Challa true, pfx, ifindex); 270793a714d6SMadhu Challa 270893a714d6SMadhu Challa if (ret < 0) 270993a714d6SMadhu Challa return ret; 271093a714d6SMadhu Challa } 271193a714d6SMadhu Challa 27121da177e4SLinus Torvalds scope = ipv6_addr_scope(pfx); 27131da177e4SLinus Torvalds 27144bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(valid_lft, HZ); 27154bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 27164bed72e4SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(timeout * HZ); 27174bed72e4SYOSHIFUJI Hideaki valid_lft = timeout; 27186f704992SYOSHIFUJI Hideaki flags = RTF_EXPIRES; 27194bed72e4SYOSHIFUJI Hideaki } else { 27204bed72e4SYOSHIFUJI Hideaki expires = 0; 27214bed72e4SYOSHIFUJI Hideaki flags = 0; 27224bed72e4SYOSHIFUJI Hideaki ifa_flags |= IFA_F_PERMANENT; 27236f704992SYOSHIFUJI Hideaki } 27240778769dSNoriaki TAKAMIYA 27254bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(prefered_lft, HZ); 27264bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 27274bed72e4SYOSHIFUJI Hideaki if (timeout == 0) 27280778769dSNoriaki TAKAMIYA ifa_flags |= IFA_F_DEPRECATED; 27294bed72e4SYOSHIFUJI Hideaki prefered_lft = timeout; 27304bed72e4SYOSHIFUJI Hideaki } 27310778769dSNoriaki TAKAMIYA 27328a226b2cSJiri Benc ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags, 27338a226b2cSJiri Benc valid_lft, prefered_lft); 27340778769dSNoriaki TAKAMIYA 27351da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 2736761aac73SThomas Haller if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) { 273746d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 27386f704992SYOSHIFUJI Hideaki expires, flags); 2739761aac73SThomas Haller } 2740761aac73SThomas Haller 274195c385b4SNeil Horman /* 274295c385b4SNeil Horman * Note that section 3.1 of RFC 4429 indicates 274395c385b4SNeil Horman * that the Optimistic flag should not be set for 274495c385b4SNeil Horman * manually configured addresses 274595c385b4SNeil Horman */ 2746cf22f9a2SDavid S. Miller addrconf_dad_start(ifp); 274753bd6749SJiri Pirko if (ifa_flags & IFA_F_MANAGETEMPADDR) 274853bd6749SJiri Pirko manage_tempaddrs(idev, ifp, valid_lft, prefered_lft, 274953bd6749SJiri Pirko true, jiffies); 27501da177e4SLinus Torvalds in6_ifa_put(ifp); 2751c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 27521da177e4SLinus Torvalds return 0; 275393a714d6SMadhu Challa } else if (ifa_flags & IFA_F_MCAUTOJOIN) { 275493a714d6SMadhu Challa ipv6_mc_config(net->ipv6.mc_autojoin_sk, 275593a714d6SMadhu Challa false, pfx, ifindex); 27561da177e4SLinus Torvalds } 27571da177e4SLinus Torvalds 27581da177e4SLinus Torvalds return PTR_ERR(ifp); 27591da177e4SLinus Torvalds } 27601da177e4SLinus Torvalds 27616046d5b4SHeiner Kallweit static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags, 27626046d5b4SHeiner Kallweit const struct in6_addr *pfx, unsigned int plen) 27631da177e4SLinus Torvalds { 27641da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 27651da177e4SLinus Torvalds struct inet6_dev *idev; 27661da177e4SLinus Torvalds struct net_device *dev; 27671da177e4SLinus Torvalds 276824ef0da7SThomas Graf if (plen > 128) 276924ef0da7SThomas Graf return -EINVAL; 277024ef0da7SThomas Graf 2771af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifindex); 2772af284937SDaniel Lezcano if (!dev) 27731da177e4SLinus Torvalds return -ENODEV; 27741da177e4SLinus Torvalds 2775e5d08d71SIan Morris idev = __in6_dev_get(dev); 277663159f29SIan Morris if (!idev) 27771da177e4SLinus Torvalds return -ENXIO; 27781da177e4SLinus Torvalds 27791da177e4SLinus Torvalds read_lock_bh(&idev->lock); 2780502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 27811da177e4SLinus Torvalds if (ifp->prefix_len == plen && 27821da177e4SLinus Torvalds ipv6_addr_equal(pfx, &ifp->addr)) { 27831da177e4SLinus Torvalds in6_ifa_hold(ifp); 27841da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 27851da177e4SLinus Torvalds 27866046d5b4SHeiner Kallweit if (!(ifp->flags & IFA_F_TEMPORARY) && 27876046d5b4SHeiner Kallweit (ifa_flags & IFA_F_MANAGETEMPADDR)) 27886046d5b4SHeiner Kallweit manage_tempaddrs(idev, ifp, 0, 0, false, 27896046d5b4SHeiner Kallweit jiffies); 27901da177e4SLinus Torvalds ipv6_del_addr(ifp); 27916046d5b4SHeiner Kallweit addrconf_verify_rtnl(); 279293a714d6SMadhu Challa if (ipv6_addr_is_multicast(pfx)) { 279393a714d6SMadhu Challa ipv6_mc_config(net->ipv6.mc_autojoin_sk, 279493a714d6SMadhu Challa false, pfx, dev->ifindex); 279593a714d6SMadhu Challa } 27961da177e4SLinus Torvalds return 0; 27971da177e4SLinus Torvalds } 27981da177e4SLinus Torvalds } 27991da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 28001da177e4SLinus Torvalds return -EADDRNOTAVAIL; 28011da177e4SLinus Torvalds } 28021da177e4SLinus Torvalds 28031da177e4SLinus Torvalds 2804af284937SDaniel Lezcano int addrconf_add_ifaddr(struct net *net, void __user *arg) 28051da177e4SLinus Torvalds { 28061da177e4SLinus Torvalds struct in6_ifreq ireq; 28071da177e4SLinus Torvalds int err; 28081da177e4SLinus Torvalds 2809af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 28101da177e4SLinus Torvalds return -EPERM; 28111da177e4SLinus Torvalds 28121da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 28131da177e4SLinus Torvalds return -EFAULT; 28141da177e4SLinus Torvalds 28151da177e4SLinus Torvalds rtnl_lock(); 2816caeaba79SNicolas Dichtel err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL, 2817af284937SDaniel Lezcano ireq.ifr6_prefixlen, IFA_F_PERMANENT, 2818af284937SDaniel Lezcano INFINITY_LIFE_TIME, INFINITY_LIFE_TIME); 28191da177e4SLinus Torvalds rtnl_unlock(); 28201da177e4SLinus Torvalds return err; 28211da177e4SLinus Torvalds } 28221da177e4SLinus Torvalds 2823af284937SDaniel Lezcano int addrconf_del_ifaddr(struct net *net, void __user *arg) 28241da177e4SLinus Torvalds { 28251da177e4SLinus Torvalds struct in6_ifreq ireq; 28261da177e4SLinus Torvalds int err; 28271da177e4SLinus Torvalds 2828af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 28291da177e4SLinus Torvalds return -EPERM; 28301da177e4SLinus Torvalds 28311da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 28321da177e4SLinus Torvalds return -EFAULT; 28331da177e4SLinus Torvalds 28341da177e4SLinus Torvalds rtnl_lock(); 28356046d5b4SHeiner Kallweit err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr, 2836af284937SDaniel Lezcano ireq.ifr6_prefixlen); 28371da177e4SLinus Torvalds rtnl_unlock(); 28381da177e4SLinus Torvalds return err; 28391da177e4SLinus Torvalds } 28401da177e4SLinus Torvalds 2841b5f348e5SIlpo Järvinen static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr, 2842b5f348e5SIlpo Järvinen int plen, int scope) 2843b5f348e5SIlpo Järvinen { 2844b5f348e5SIlpo Järvinen struct inet6_ifaddr *ifp; 2845b5f348e5SIlpo Järvinen 28468a226b2cSJiri Benc ifp = ipv6_add_addr(idev, addr, NULL, plen, 284707edd741SHannes Frederic Sowa scope, IFA_F_PERMANENT, 284807edd741SHannes Frederic Sowa INFINITY_LIFE_TIME, INFINITY_LIFE_TIME); 2849b5f348e5SIlpo Järvinen if (!IS_ERR(ifp)) { 2850b5f348e5SIlpo Järvinen spin_lock_bh(&ifp->lock); 2851b5f348e5SIlpo Järvinen ifp->flags &= ~IFA_F_TENTATIVE; 2852b5f348e5SIlpo Järvinen spin_unlock_bh(&ifp->lock); 2853b5f348e5SIlpo Järvinen ipv6_ifa_notify(RTM_NEWADDR, ifp); 2854b5f348e5SIlpo Järvinen in6_ifa_put(ifp); 2855b5f348e5SIlpo Järvinen } 2856b5f348e5SIlpo Järvinen } 2857b5f348e5SIlpo Järvinen 285807a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 28591da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev) 28601da177e4SLinus Torvalds { 28611da177e4SLinus Torvalds struct in6_addr addr; 28621da177e4SLinus Torvalds struct net_device *dev; 2863c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 2864929c9cf3SNicolas Dichtel int scope, plen; 2865f0e2acfaSNicolas Dichtel u32 pflags = 0; 28661da177e4SLinus Torvalds 28671da177e4SLinus Torvalds ASSERT_RTNL(); 28681da177e4SLinus Torvalds 28691da177e4SLinus Torvalds memset(&addr, 0, sizeof(struct in6_addr)); 28701da177e4SLinus Torvalds memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4); 28711da177e4SLinus Torvalds 28721da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) { 28731da177e4SLinus Torvalds addr.s6_addr32[0] = htonl(0xfe800000); 28741da177e4SLinus Torvalds scope = IFA_LINK; 2875929c9cf3SNicolas Dichtel plen = 64; 28761da177e4SLinus Torvalds } else { 28771da177e4SLinus Torvalds scope = IPV6_ADDR_COMPATv4; 2878929c9cf3SNicolas Dichtel plen = 96; 2879f0e2acfaSNicolas Dichtel pflags |= RTF_NONEXTHOP; 28801da177e4SLinus Torvalds } 28811da177e4SLinus Torvalds 28821da177e4SLinus Torvalds if (addr.s6_addr32[3]) { 2883929c9cf3SNicolas Dichtel add_addr(idev, &addr, plen, scope); 2884f0e2acfaSNicolas Dichtel addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags); 28851da177e4SLinus Torvalds return; 28861da177e4SLinus Torvalds } 28871da177e4SLinus Torvalds 28886fda7350SBenjamin Thery for_each_netdev(net, dev) { 2889e5ed6399SHerbert Xu struct in_device *in_dev = __in_dev_get_rtnl(dev); 28901da177e4SLinus Torvalds if (in_dev && (dev->flags & IFF_UP)) { 28911da177e4SLinus Torvalds struct in_ifaddr *ifa; 28921da177e4SLinus Torvalds 28931da177e4SLinus Torvalds int flag = scope; 28941da177e4SLinus Torvalds 28951da177e4SLinus Torvalds for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) { 28961da177e4SLinus Torvalds 28971da177e4SLinus Torvalds addr.s6_addr32[3] = ifa->ifa_local; 28981da177e4SLinus Torvalds 28991da177e4SLinus Torvalds if (ifa->ifa_scope == RT_SCOPE_LINK) 29001da177e4SLinus Torvalds continue; 29011da177e4SLinus Torvalds if (ifa->ifa_scope >= RT_SCOPE_HOST) { 29021da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) 29031da177e4SLinus Torvalds continue; 29041da177e4SLinus Torvalds flag |= IFA_HOST; 29051da177e4SLinus Torvalds } 29061da177e4SLinus Torvalds 2907b5f348e5SIlpo Järvinen add_addr(idev, &addr, plen, flag); 2908f0e2acfaSNicolas Dichtel addrconf_prefix_route(&addr, plen, idev->dev, 0, 2909f0e2acfaSNicolas Dichtel pflags); 29101da177e4SLinus Torvalds } 29111da177e4SLinus Torvalds } 29121da177e4SLinus Torvalds } 29131da177e4SLinus Torvalds } 29140be669bbSJoerg Roedel #endif 29151da177e4SLinus Torvalds 29161da177e4SLinus Torvalds static void init_loopback(struct net_device *dev) 29171da177e4SLinus Torvalds { 29181da177e4SLinus Torvalds struct inet6_dev *idev; 291925fb6ca4SBalakumaran Kannan struct net_device *sp_dev; 292025fb6ca4SBalakumaran Kannan struct inet6_ifaddr *sp_ifa; 292125fb6ca4SBalakumaran Kannan struct rt6_info *sp_rt; 29221da177e4SLinus Torvalds 29231da177e4SLinus Torvalds /* ::1 */ 29241da177e4SLinus Torvalds 29251da177e4SLinus Torvalds ASSERT_RTNL(); 29261da177e4SLinus Torvalds 2927e5d08d71SIan Morris idev = ipv6_find_idev(dev); 292863159f29SIan Morris if (!idev) { 292991df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 29301da177e4SLinus Torvalds return; 29311da177e4SLinus Torvalds } 29321da177e4SLinus Torvalds 2933b5f348e5SIlpo Järvinen add_addr(idev, &in6addr_loopback, 128, IFA_HOST); 293425fb6ca4SBalakumaran Kannan 293525fb6ca4SBalakumaran Kannan /* Add routes to other interface's IPv6 addresses */ 293625fb6ca4SBalakumaran Kannan for_each_netdev(dev_net(dev), sp_dev) { 293725fb6ca4SBalakumaran Kannan if (!strcmp(sp_dev->name, dev->name)) 293825fb6ca4SBalakumaran Kannan continue; 293925fb6ca4SBalakumaran Kannan 294025fb6ca4SBalakumaran Kannan idev = __in6_dev_get(sp_dev); 294125fb6ca4SBalakumaran Kannan if (!idev) 294225fb6ca4SBalakumaran Kannan continue; 294325fb6ca4SBalakumaran Kannan 294425fb6ca4SBalakumaran Kannan read_lock_bh(&idev->lock); 294525fb6ca4SBalakumaran Kannan list_for_each_entry(sp_ifa, &idev->addr_list, if_list) { 294625fb6ca4SBalakumaran Kannan 294725fb6ca4SBalakumaran Kannan if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE)) 294825fb6ca4SBalakumaran Kannan continue; 294925fb6ca4SBalakumaran Kannan 295033d99113SGao feng if (sp_ifa->rt) { 295133d99113SGao feng /* This dst has been added to garbage list when 295233d99113SGao feng * lo device down, release this obsolete dst and 295333d99113SGao feng * reallocate a new router for ifa. 295433d99113SGao feng */ 295533d99113SGao feng if (sp_ifa->rt->dst.obsolete > 0) { 295633d99113SGao feng ip6_rt_put(sp_ifa->rt); 295733d99113SGao feng sp_ifa->rt = NULL; 295833d99113SGao feng } else { 2959a881ae1fSGao feng continue; 296033d99113SGao feng } 296133d99113SGao feng } 2962a881ae1fSGao feng 296357ec0afeSFrançois-Xavier Le Bail sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, false); 296425fb6ca4SBalakumaran Kannan 296525fb6ca4SBalakumaran Kannan /* Failure cases are ignored */ 2966534c8779SGao feng if (!IS_ERR(sp_rt)) { 2967534c8779SGao feng sp_ifa->rt = sp_rt; 296825fb6ca4SBalakumaran Kannan ip6_ins_rt(sp_rt); 296925fb6ca4SBalakumaran Kannan } 2970534c8779SGao feng } 297125fb6ca4SBalakumaran Kannan read_unlock_bh(&idev->lock); 297225fb6ca4SBalakumaran Kannan } 29731da177e4SLinus Torvalds } 29741da177e4SLinus Torvalds 29752ad3ed59SAlexander Aring void addrconf_add_linklocal(struct inet6_dev *idev, 297664236f3fSHannes Frederic Sowa const struct in6_addr *addr, u32 flags) 29771da177e4SLinus Torvalds { 29781da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 297964236f3fSHannes Frederic Sowa u32 addr_flags = flags | IFA_F_PERMANENT; 29801da177e4SLinus Torvalds 298195c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 298295c385b4SNeil Horman if (idev->cnf.optimistic_dad && 2983702beb87SDavid Miller !dev_net(idev->dev)->ipv6.devconf_all->forwarding) 298495c385b4SNeil Horman addr_flags |= IFA_F_OPTIMISTIC; 298595c385b4SNeil Horman #endif 298695c385b4SNeil Horman 298707edd741SHannes Frederic Sowa ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags, 298807edd741SHannes Frederic Sowa INFINITY_LIFE_TIME, INFINITY_LIFE_TIME); 29891da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 299046d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0); 2991cf22f9a2SDavid S. Miller addrconf_dad_start(ifp); 29921da177e4SLinus Torvalds in6_ifa_put(ifp); 29931da177e4SLinus Torvalds } 29941da177e4SLinus Torvalds } 29952ad3ed59SAlexander Aring EXPORT_SYMBOL_GPL(addrconf_add_linklocal); 29961da177e4SLinus Torvalds 2997622c81d5SHannes Frederic Sowa static bool ipv6_reserved_interfaceid(struct in6_addr address) 2998622c81d5SHannes Frederic Sowa { 2999622c81d5SHannes Frederic Sowa if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0) 3000622c81d5SHannes Frederic Sowa return true; 3001622c81d5SHannes Frederic Sowa 3002622c81d5SHannes Frederic Sowa if (address.s6_addr32[2] == htonl(0x02005eff) && 3003622c81d5SHannes Frederic Sowa ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000))) 3004622c81d5SHannes Frederic Sowa return true; 3005622c81d5SHannes Frederic Sowa 3006622c81d5SHannes Frederic Sowa if (address.s6_addr32[2] == htonl(0xfdffffff) && 3007622c81d5SHannes Frederic Sowa ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80))) 3008622c81d5SHannes Frederic Sowa return true; 3009622c81d5SHannes Frederic Sowa 3010622c81d5SHannes Frederic Sowa return false; 3011622c81d5SHannes Frederic Sowa } 3012622c81d5SHannes Frederic Sowa 3013622c81d5SHannes Frederic Sowa static int ipv6_generate_stable_address(struct in6_addr *address, 3014622c81d5SHannes Frederic Sowa u8 dad_count, 3015622c81d5SHannes Frederic Sowa const struct inet6_dev *idev) 3016622c81d5SHannes Frederic Sowa { 3017622c81d5SHannes Frederic Sowa static DEFINE_SPINLOCK(lock); 3018622c81d5SHannes Frederic Sowa static __u32 digest[SHA_DIGEST_WORDS]; 3019622c81d5SHannes Frederic Sowa static __u32 workspace[SHA_WORKSPACE_WORDS]; 3020622c81d5SHannes Frederic Sowa 3021622c81d5SHannes Frederic Sowa static union { 3022622c81d5SHannes Frederic Sowa char __data[SHA_MESSAGE_BYTES]; 3023622c81d5SHannes Frederic Sowa struct { 3024622c81d5SHannes Frederic Sowa struct in6_addr secret; 3025ff40217eSHannes Frederic Sowa __be32 prefix[2]; 3026622c81d5SHannes Frederic Sowa unsigned char hwaddr[MAX_ADDR_LEN]; 3027622c81d5SHannes Frederic Sowa u8 dad_count; 3028622c81d5SHannes Frederic Sowa } __packed; 3029622c81d5SHannes Frederic Sowa } data; 3030622c81d5SHannes Frederic Sowa 3031622c81d5SHannes Frederic Sowa struct in6_addr secret; 3032622c81d5SHannes Frederic Sowa struct in6_addr temp; 3033622c81d5SHannes Frederic Sowa struct net *net = dev_net(idev->dev); 3034622c81d5SHannes Frederic Sowa 3035622c81d5SHannes Frederic Sowa BUILD_BUG_ON(sizeof(data.__data) != sizeof(data)); 3036622c81d5SHannes Frederic Sowa 3037622c81d5SHannes Frederic Sowa if (idev->cnf.stable_secret.initialized) 3038622c81d5SHannes Frederic Sowa secret = idev->cnf.stable_secret.secret; 3039622c81d5SHannes Frederic Sowa else if (net->ipv6.devconf_dflt->stable_secret.initialized) 3040622c81d5SHannes Frederic Sowa secret = net->ipv6.devconf_dflt->stable_secret.secret; 3041622c81d5SHannes Frederic Sowa else 3042622c81d5SHannes Frederic Sowa return -1; 3043622c81d5SHannes Frederic Sowa 3044622c81d5SHannes Frederic Sowa retry: 3045622c81d5SHannes Frederic Sowa spin_lock_bh(&lock); 3046622c81d5SHannes Frederic Sowa 3047622c81d5SHannes Frederic Sowa sha_init(digest); 3048622c81d5SHannes Frederic Sowa memset(&data, 0, sizeof(data)); 3049622c81d5SHannes Frederic Sowa memset(workspace, 0, sizeof(workspace)); 3050622c81d5SHannes Frederic Sowa memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len); 3051ff40217eSHannes Frederic Sowa data.prefix[0] = address->s6_addr32[0]; 3052ff40217eSHannes Frederic Sowa data.prefix[1] = address->s6_addr32[1]; 3053622c81d5SHannes Frederic Sowa data.secret = secret; 3054622c81d5SHannes Frederic Sowa data.dad_count = dad_count; 3055622c81d5SHannes Frederic Sowa 3056622c81d5SHannes Frederic Sowa sha_transform(digest, data.__data, workspace); 3057622c81d5SHannes Frederic Sowa 3058622c81d5SHannes Frederic Sowa temp = *address; 3059ff40217eSHannes Frederic Sowa temp.s6_addr32[2] = (__force __be32)digest[0]; 3060ff40217eSHannes Frederic Sowa temp.s6_addr32[3] = (__force __be32)digest[1]; 3061622c81d5SHannes Frederic Sowa 3062622c81d5SHannes Frederic Sowa spin_unlock_bh(&lock); 3063622c81d5SHannes Frederic Sowa 3064622c81d5SHannes Frederic Sowa if (ipv6_reserved_interfaceid(temp)) { 3065622c81d5SHannes Frederic Sowa dad_count++; 30661855b7c3SHannes Frederic Sowa if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries) 3067622c81d5SHannes Frederic Sowa return -1; 3068622c81d5SHannes Frederic Sowa goto retry; 3069622c81d5SHannes Frederic Sowa } 3070622c81d5SHannes Frederic Sowa 3071622c81d5SHannes Frederic Sowa *address = temp; 3072622c81d5SHannes Frederic Sowa return 0; 3073622c81d5SHannes Frederic Sowa } 3074622c81d5SHannes Frederic Sowa 3075cc9da6ccSBjørn Mork static void ipv6_gen_mode_random_init(struct inet6_dev *idev) 3076cc9da6ccSBjørn Mork { 3077cc9da6ccSBjørn Mork struct ipv6_stable_secret *s = &idev->cnf.stable_secret; 3078cc9da6ccSBjørn Mork 3079cc9da6ccSBjørn Mork if (s->initialized) 3080cc9da6ccSBjørn Mork return; 3081cc9da6ccSBjørn Mork s = &idev->cnf.stable_secret; 3082cc9da6ccSBjørn Mork get_random_bytes(&s->secret, sizeof(s->secret)); 3083cc9da6ccSBjørn Mork s->initialized = true; 3084cc9da6ccSBjørn Mork } 3085cc9da6ccSBjørn Mork 3086bc91b0f0SJiri Pirko static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route) 3087bc91b0f0SJiri Pirko { 3088bc91b0f0SJiri Pirko struct in6_addr addr; 3089bc91b0f0SJiri Pirko 3090ca254490SDavid Ahern /* no link local addresses on L3 master devices */ 3091ca254490SDavid Ahern if (netif_is_l3_master(idev->dev)) 3092ca254490SDavid Ahern return; 3093ca254490SDavid Ahern 3094bc91b0f0SJiri Pirko ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0); 3095622c81d5SHannes Frederic Sowa 3096cc9da6ccSBjørn Mork switch (idev->addr_gen_mode) { 3097cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_RANDOM: 3098cc9da6ccSBjørn Mork ipv6_gen_mode_random_init(idev); 3099cc9da6ccSBjørn Mork /* fallthrough */ 3100cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_STABLE_PRIVACY: 3101622c81d5SHannes Frederic Sowa if (!ipv6_generate_stable_address(&addr, 0, idev)) 310264236f3fSHannes Frederic Sowa addrconf_add_linklocal(idev, &addr, 310364236f3fSHannes Frederic Sowa IFA_F_STABLE_PRIVACY); 3104622c81d5SHannes Frederic Sowa else if (prefix_route) 3105622c81d5SHannes Frederic Sowa addrconf_prefix_route(&addr, 64, idev->dev, 0, 0); 3106cc9da6ccSBjørn Mork break; 3107cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_EUI64: 3108bc91b0f0SJiri Pirko /* addrconf_add_linklocal also adds a prefix_route and we 3109bc91b0f0SJiri Pirko * only need to care about prefix routes if ipv6_generate_eui64 3110bc91b0f0SJiri Pirko * couldn't generate one. 3111bc91b0f0SJiri Pirko */ 3112bc91b0f0SJiri Pirko if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0) 311364236f3fSHannes Frederic Sowa addrconf_add_linklocal(idev, &addr, 0); 3114bc91b0f0SJiri Pirko else if (prefix_route) 3115bc91b0f0SJiri Pirko addrconf_prefix_route(&addr, 64, idev->dev, 0, 0); 3116cc9da6ccSBjørn Mork break; 3117cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_NONE: 3118cc9da6ccSBjørn Mork default: 3119cc9da6ccSBjørn Mork /* will not add any link local address */ 3120cc9da6ccSBjørn Mork break; 3121bc91b0f0SJiri Pirko } 3122bc91b0f0SJiri Pirko } 3123bc91b0f0SJiri Pirko 31241da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev) 31251da177e4SLinus Torvalds { 31261da177e4SLinus Torvalds struct inet6_dev *idev; 31271da177e4SLinus Torvalds 31281da177e4SLinus Torvalds ASSERT_RTNL(); 31291da177e4SLinus Torvalds 313074235a25SHerbert Xu if ((dev->type != ARPHRD_ETHER) && 313174235a25SHerbert Xu (dev->type != ARPHRD_FDDI) && 313274235a25SHerbert Xu (dev->type != ARPHRD_ARCNET) && 313306a4c1c5Salex.bluesman.smirnov@gmail.com (dev->type != ARPHRD_INFINIBAND) && 3134e837735eSNicolas Dichtel (dev->type != ARPHRD_IEEE1394) && 3135e74bccb8SJukka Rissanen (dev->type != ARPHRD_TUNNEL6) && 3136cc9da6ccSBjørn Mork (dev->type != ARPHRD_6LOWPAN) && 3137cc9da6ccSBjørn Mork (dev->type != ARPHRD_NONE)) { 313874235a25SHerbert Xu /* Alas, we support only Ethernet autoconfiguration. */ 313974235a25SHerbert Xu return; 314074235a25SHerbert Xu } 314174235a25SHerbert Xu 31421da177e4SLinus Torvalds idev = addrconf_add_dev(dev); 314364e724f6SBrian Haley if (IS_ERR(idev)) 31441da177e4SLinus Torvalds return; 31451da177e4SLinus Torvalds 3146cc9da6ccSBjørn Mork /* this device type has no EUI support */ 3147cc9da6ccSBjørn Mork if (dev->type == ARPHRD_NONE && 3148cc9da6ccSBjørn Mork idev->addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64) 3149cc9da6ccSBjørn Mork idev->addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM; 3150cc9da6ccSBjørn Mork 3151bc91b0f0SJiri Pirko addrconf_addr_gen(idev, false); 31521da177e4SLinus Torvalds } 31531da177e4SLinus Torvalds 315407a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 31551da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev) 31561da177e4SLinus Torvalds { 31571da177e4SLinus Torvalds struct inet6_dev *idev; 31581da177e4SLinus Torvalds 31591da177e4SLinus Torvalds ASSERT_RTNL(); 31601da177e4SLinus Torvalds 31611da177e4SLinus Torvalds /* 31621da177e4SLinus Torvalds * Configure the tunnel with one of our IPv4 31631da177e4SLinus Torvalds * addresses... we should configure all of 31641da177e4SLinus Torvalds * our v4 addrs in the tunnel 31651da177e4SLinus Torvalds */ 31661da177e4SLinus Torvalds 3167e5d08d71SIan Morris idev = ipv6_find_idev(dev); 316863159f29SIan Morris if (!idev) { 316991df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 31701da177e4SLinus Torvalds return; 31711da177e4SLinus Torvalds } 31721da177e4SLinus Torvalds 3173c7dc89c0SFred L. Templin if (dev->priv_flags & IFF_ISATAP) { 3174bc91b0f0SJiri Pirko addrconf_addr_gen(idev, false); 3175c7dc89c0SFred L. Templin return; 3176c7dc89c0SFred L. Templin } 3177c7dc89c0SFred L. Templin 31781da177e4SLinus Torvalds sit_add_v4_addrs(idev); 31791da177e4SLinus Torvalds 318062b54dd9SNicolas Dichtel if (dev->flags&IFF_POINTOPOINT) 31811da177e4SLinus Torvalds addrconf_add_mroute(dev); 31821da177e4SLinus Torvalds } 31830be669bbSJoerg Roedel #endif 31841da177e4SLinus Torvalds 318507a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE) 3186aee80b54Sstephen hemminger static void addrconf_gre_config(struct net_device *dev) 3187aee80b54Sstephen hemminger { 3188aee80b54Sstephen hemminger struct inet6_dev *idev; 3189aee80b54Sstephen hemminger 3190aee80b54Sstephen hemminger ASSERT_RTNL(); 3191aee80b54Sstephen hemminger 3192e5d08d71SIan Morris idev = ipv6_find_idev(dev); 319363159f29SIan Morris if (!idev) { 319491df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 3195aee80b54Sstephen hemminger return; 3196aee80b54Sstephen hemminger } 3197aee80b54Sstephen hemminger 3198bc91b0f0SJiri Pirko addrconf_addr_gen(idev, true); 3199d9e4ce65SHannes Frederic Sowa if (dev->flags & IFF_POINTOPOINT) 3200d9e4ce65SHannes Frederic Sowa addrconf_add_mroute(dev); 3201aee80b54Sstephen hemminger } 3202aee80b54Sstephen hemminger #endif 3203aee80b54Sstephen hemminger 3204f1705ec1SDavid Ahern static int fixup_permanent_addr(struct inet6_dev *idev, 3205f1705ec1SDavid Ahern struct inet6_ifaddr *ifp) 3206f1705ec1SDavid Ahern { 3207f1705ec1SDavid Ahern if (!ifp->rt) { 3208f1705ec1SDavid Ahern struct rt6_info *rt; 3209f1705ec1SDavid Ahern 3210f1705ec1SDavid Ahern rt = addrconf_dst_alloc(idev, &ifp->addr, false); 3211f1705ec1SDavid Ahern if (unlikely(IS_ERR(rt))) 3212f1705ec1SDavid Ahern return PTR_ERR(rt); 3213f1705ec1SDavid Ahern 3214f1705ec1SDavid Ahern ifp->rt = rt; 3215f1705ec1SDavid Ahern } 3216f1705ec1SDavid Ahern 3217f1705ec1SDavid Ahern if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) { 3218f1705ec1SDavid Ahern addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 3219f1705ec1SDavid Ahern idev->dev, 0, 0); 3220f1705ec1SDavid Ahern } 3221f1705ec1SDavid Ahern 3222f1705ec1SDavid Ahern addrconf_dad_start(ifp); 3223f1705ec1SDavid Ahern 3224f1705ec1SDavid Ahern return 0; 3225f1705ec1SDavid Ahern } 3226f1705ec1SDavid Ahern 3227f1705ec1SDavid Ahern static void addrconf_permanent_addr(struct net_device *dev) 3228f1705ec1SDavid Ahern { 3229f1705ec1SDavid Ahern struct inet6_ifaddr *ifp, *tmp; 3230f1705ec1SDavid Ahern struct inet6_dev *idev; 3231f1705ec1SDavid Ahern 3232f1705ec1SDavid Ahern idev = __in6_dev_get(dev); 3233f1705ec1SDavid Ahern if (!idev) 3234f1705ec1SDavid Ahern return; 3235f1705ec1SDavid Ahern 3236f1705ec1SDavid Ahern write_lock_bh(&idev->lock); 3237f1705ec1SDavid Ahern 3238f1705ec1SDavid Ahern list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) { 3239f1705ec1SDavid Ahern if ((ifp->flags & IFA_F_PERMANENT) && 3240f1705ec1SDavid Ahern fixup_permanent_addr(idev, ifp) < 0) { 3241f1705ec1SDavid Ahern write_unlock_bh(&idev->lock); 3242f1705ec1SDavid Ahern ipv6_del_addr(ifp); 3243f1705ec1SDavid Ahern write_lock_bh(&idev->lock); 3244f1705ec1SDavid Ahern 3245f1705ec1SDavid Ahern net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n", 3246f1705ec1SDavid Ahern idev->dev->name, &ifp->addr); 3247f1705ec1SDavid Ahern } 3248f1705ec1SDavid Ahern } 3249f1705ec1SDavid Ahern 3250f1705ec1SDavid Ahern write_unlock_bh(&idev->lock); 3251f1705ec1SDavid Ahern } 3252f1705ec1SDavid Ahern 32531da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event, 3254351638e7SJiri Pirko void *ptr) 32551da177e4SLinus Torvalds { 3256351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 32574f7f34eaSDavid Ahern struct netdev_notifier_changeupper_info *info; 3258748e2d93SEric Dumazet struct inet6_dev *idev = __in6_dev_get(dev); 3259c5e33bddSYOSHIFUJI Hideaki int run_pending = 0; 3260b217d616SHerbert Xu int err; 32611da177e4SLinus Torvalds 32621da177e4SLinus Torvalds switch (event) { 326345ba9dd2SYOSHIFUJI Hideaki case NETDEV_REGISTER: 326474235a25SHerbert Xu if (!idev && dev->mtu >= IPV6_MIN_MTU) { 326545ba9dd2SYOSHIFUJI Hideaki idev = ipv6_add_dev(dev); 3266a317a2f1SWANG Cong if (IS_ERR(idev)) 3267a317a2f1SWANG Cong return notifier_from_errno(PTR_ERR(idev)); 326845ba9dd2SYOSHIFUJI Hideaki } 326945ba9dd2SYOSHIFUJI Hideaki break; 3270bcdd553fSStephen Hemminger 3271b7b0b1d2SAlexander Duyck case NETDEV_CHANGEMTU: 3272b7b0b1d2SAlexander Duyck /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */ 3273b7b0b1d2SAlexander Duyck if (dev->mtu < IPV6_MIN_MTU) { 3274b7b0b1d2SAlexander Duyck addrconf_ifdown(dev, 1); 3275b7b0b1d2SAlexander Duyck break; 3276b7b0b1d2SAlexander Duyck } 3277b7b0b1d2SAlexander Duyck 3278b7b0b1d2SAlexander Duyck if (idev) { 3279b7b0b1d2SAlexander Duyck rt6_mtu_change(dev, dev->mtu); 3280b7b0b1d2SAlexander Duyck idev->cnf.mtu6 = dev->mtu; 3281b7b0b1d2SAlexander Duyck break; 3282b7b0b1d2SAlexander Duyck } 3283b7b0b1d2SAlexander Duyck 3284b7b0b1d2SAlexander Duyck /* allocate new idev */ 3285b7b0b1d2SAlexander Duyck idev = ipv6_add_dev(dev); 3286b7b0b1d2SAlexander Duyck if (IS_ERR(idev)) 3287b7b0b1d2SAlexander Duyck break; 3288b7b0b1d2SAlexander Duyck 3289b7b0b1d2SAlexander Duyck /* device is still not ready */ 3290b7b0b1d2SAlexander Duyck if (!(idev->if_flags & IF_READY)) 3291b7b0b1d2SAlexander Duyck break; 3292b7b0b1d2SAlexander Duyck 3293b7b0b1d2SAlexander Duyck run_pending = 1; 3294b7b0b1d2SAlexander Duyck 3295b7b0b1d2SAlexander Duyck /* fall through */ 3296b7b0b1d2SAlexander Duyck 32971da177e4SLinus Torvalds case NETDEV_UP: 32983c21edbdSYOSHIFUJI Hideaki case NETDEV_CHANGE: 3299c2edacf8SJay Vosburgh if (dev->flags & IFF_SLAVE) 3300c2edacf8SJay Vosburgh break; 3301c2edacf8SJay Vosburgh 33023ce62a84SWANG Cong if (idev && idev->cnf.disable_ipv6) 33033ce62a84SWANG Cong break; 33043ce62a84SWANG Cong 33053c21edbdSYOSHIFUJI Hideaki if (event == NETDEV_UP) { 330638bd10c4SDavid Ahern /* restore routes for permanent addresses */ 330738bd10c4SDavid Ahern addrconf_permanent_addr(dev); 330838bd10c4SDavid Ahern 3309f24e3d65SMitsuru Chinen if (!addrconf_qdisc_ok(dev)) { 33103c21edbdSYOSHIFUJI Hideaki /* device is not ready yet. */ 3311f3213831SJoe Perches pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n", 33123c21edbdSYOSHIFUJI Hideaki dev->name); 33133c21edbdSYOSHIFUJI Hideaki break; 33143c21edbdSYOSHIFUJI Hideaki } 331599081049SKristian Slavov 3316d31c7b8fSEvgeniy Polyakov if (!idev && dev->mtu >= IPV6_MIN_MTU) 3317d31c7b8fSEvgeniy Polyakov idev = ipv6_add_dev(dev); 3318d31c7b8fSEvgeniy Polyakov 3319a317a2f1SWANG Cong if (!IS_ERR_OR_NULL(idev)) { 332099081049SKristian Slavov idev->if_flags |= IF_READY; 3321e3ec6cfcSBenjamin Thery run_pending = 1; 3322e3ec6cfcSBenjamin Thery } 3323b7b0b1d2SAlexander Duyck } else if (event == NETDEV_CHANGE) { 3324f24e3d65SMitsuru Chinen if (!addrconf_qdisc_ok(dev)) { 33253c21edbdSYOSHIFUJI Hideaki /* device is still not ready. */ 33263c21edbdSYOSHIFUJI Hideaki break; 33273c21edbdSYOSHIFUJI Hideaki } 33283c21edbdSYOSHIFUJI Hideaki 33293c21edbdSYOSHIFUJI Hideaki if (idev) { 3330bcdd553fSStephen Hemminger if (idev->if_flags & IF_READY) 33313c21edbdSYOSHIFUJI Hideaki /* device is already configured. */ 33323c21edbdSYOSHIFUJI Hideaki break; 33333c21edbdSYOSHIFUJI Hideaki idev->if_flags |= IF_READY; 33343c21edbdSYOSHIFUJI Hideaki } 33353c21edbdSYOSHIFUJI Hideaki 3336f3213831SJoe Perches pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n", 33373c21edbdSYOSHIFUJI Hideaki dev->name); 33383c21edbdSYOSHIFUJI Hideaki 3339c5e33bddSYOSHIFUJI Hideaki run_pending = 1; 33403c21edbdSYOSHIFUJI Hideaki } 33413c21edbdSYOSHIFUJI Hideaki 33421da177e4SLinus Torvalds switch (dev->type) { 334307a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 33441da177e4SLinus Torvalds case ARPHRD_SIT: 33451da177e4SLinus Torvalds addrconf_sit_config(dev); 33461da177e4SLinus Torvalds break; 33470be669bbSJoerg Roedel #endif 334807a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE) 3349aee80b54Sstephen hemminger case ARPHRD_IPGRE: 3350aee80b54Sstephen hemminger addrconf_gre_config(dev); 3351aee80b54Sstephen hemminger break; 3352aee80b54Sstephen hemminger #endif 33531da177e4SLinus Torvalds case ARPHRD_LOOPBACK: 33541da177e4SLinus Torvalds init_loopback(dev); 33551da177e4SLinus Torvalds break; 33561da177e4SLinus Torvalds 33571da177e4SLinus Torvalds default: 33581da177e4SLinus Torvalds addrconf_dev_config(dev); 33591da177e4SLinus Torvalds break; 33603ff50b79SStephen Hemminger } 3361bcdd553fSStephen Hemminger 3362a317a2f1SWANG Cong if (!IS_ERR_OR_NULL(idev)) { 3363c5e33bddSYOSHIFUJI Hideaki if (run_pending) 3364c5e33bddSYOSHIFUJI Hideaki addrconf_dad_run(idev); 3365c5e33bddSYOSHIFUJI Hideaki 3366bcdd553fSStephen Hemminger /* 3367bcdd553fSStephen Hemminger * If the MTU changed during the interface down, 3368bcdd553fSStephen Hemminger * when the interface up, the changed MTU must be 3369bcdd553fSStephen Hemminger * reflected in the idev as well as routers. 33701da177e4SLinus Torvalds */ 3371bcdd553fSStephen Hemminger if (idev->cnf.mtu6 != dev->mtu && 3372bcdd553fSStephen Hemminger dev->mtu >= IPV6_MIN_MTU) { 33731da177e4SLinus Torvalds rt6_mtu_change(dev, dev->mtu); 33741da177e4SLinus Torvalds idev->cnf.mtu6 = dev->mtu; 33751da177e4SLinus Torvalds } 33761da177e4SLinus Torvalds idev->tstamp = jiffies; 33771da177e4SLinus Torvalds inet6_ifinfo_notify(RTM_NEWLINK, idev); 3378bcdd553fSStephen Hemminger 3379bcdd553fSStephen Hemminger /* 3380bcdd553fSStephen Hemminger * If the changed mtu during down is lower than 3381bcdd553fSStephen Hemminger * IPV6_MIN_MTU stop IPv6 on this interface. 33821da177e4SLinus Torvalds */ 33831da177e4SLinus Torvalds if (dev->mtu < IPV6_MIN_MTU) 3384bcdd553fSStephen Hemminger addrconf_ifdown(dev, 1); 33851da177e4SLinus Torvalds } 33861da177e4SLinus Torvalds break; 33871da177e4SLinus Torvalds 33881da177e4SLinus Torvalds case NETDEV_DOWN: 33891da177e4SLinus Torvalds case NETDEV_UNREGISTER: 33901da177e4SLinus Torvalds /* 33911da177e4SLinus Torvalds * Remove all addresses from this interface. 33921da177e4SLinus Torvalds */ 33931da177e4SLinus Torvalds addrconf_ifdown(dev, event != NETDEV_DOWN); 33941da177e4SLinus Torvalds break; 33953c21edbdSYOSHIFUJI Hideaki 33961da177e4SLinus Torvalds case NETDEV_CHANGENAME: 33971da177e4SLinus Torvalds if (idev) { 33985632c515SStephen Hemminger snmp6_unregister_dev(idev); 3399408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 3400a317a2f1SWANG Cong err = addrconf_sysctl_register(idev); 3401b217d616SHerbert Xu if (err) 3402b217d616SHerbert Xu return notifier_from_errno(err); 3403a317a2f1SWANG Cong err = snmp6_register_dev(idev); 3404a317a2f1SWANG Cong if (err) { 3405a317a2f1SWANG Cong addrconf_sysctl_unregister(idev); 3406a317a2f1SWANG Cong return notifier_from_errno(err); 3407a317a2f1SWANG Cong } 34085632c515SStephen Hemminger } 34091da177e4SLinus Torvalds break; 3410bcdd553fSStephen Hemminger 341193d9b7d7SJiri Pirko case NETDEV_PRE_TYPE_CHANGE: 341293d9b7d7SJiri Pirko case NETDEV_POST_TYPE_CHANGE: 34133ef0952cSAndrew Lunn if (idev) 341493d9b7d7SJiri Pirko addrconf_type_change(dev, event); 341575c78500SMoni Shoua break; 34164f7f34eaSDavid Ahern 34174f7f34eaSDavid Ahern case NETDEV_CHANGEUPPER: 34184f7f34eaSDavid Ahern info = ptr; 34194f7f34eaSDavid Ahern 34204f7f34eaSDavid Ahern /* flush all routes if dev is linked to or unlinked from 34214f7f34eaSDavid Ahern * an L3 master device (e.g., VRF) 34224f7f34eaSDavid Ahern */ 34234f7f34eaSDavid Ahern if (info->upper_dev && netif_is_l3_master(info->upper_dev)) 34244f7f34eaSDavid Ahern addrconf_ifdown(dev, 0); 34253ff50b79SStephen Hemminger } 34261da177e4SLinus Torvalds 34271da177e4SLinus Torvalds return NOTIFY_OK; 34281da177e4SLinus Torvalds } 34291da177e4SLinus Torvalds 34301da177e4SLinus Torvalds /* 34311da177e4SLinus Torvalds * addrconf module should be notified of a device going up 34321da177e4SLinus Torvalds */ 34331da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = { 34341da177e4SLinus Torvalds .notifier_call = addrconf_notify, 34351da177e4SLinus Torvalds }; 34361da177e4SLinus Torvalds 343793d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, unsigned long event) 343875c78500SMoni Shoua { 343975c78500SMoni Shoua struct inet6_dev *idev; 344075c78500SMoni Shoua ASSERT_RTNL(); 344175c78500SMoni Shoua 344275c78500SMoni Shoua idev = __in6_dev_get(dev); 344375c78500SMoni Shoua 344493d9b7d7SJiri Pirko if (event == NETDEV_POST_TYPE_CHANGE) 344575c78500SMoni Shoua ipv6_mc_remap(idev); 344693d9b7d7SJiri Pirko else if (event == NETDEV_PRE_TYPE_CHANGE) 344775c78500SMoni Shoua ipv6_mc_unmap(idev); 344875c78500SMoni Shoua } 344975c78500SMoni Shoua 345070af921dSDavid Ahern static bool addr_is_local(const struct in6_addr *addr) 345170af921dSDavid Ahern { 345270af921dSDavid Ahern return ipv6_addr_type(addr) & 345370af921dSDavid Ahern (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK); 345470af921dSDavid Ahern } 345570af921dSDavid Ahern 34561da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how) 34571da177e4SLinus Torvalds { 3458c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 3459502a2ffdSstephen hemminger struct inet6_dev *idev; 3460f1705ec1SDavid Ahern struct inet6_ifaddr *ifa, *tmp; 3461f1705ec1SDavid Ahern struct list_head del_list; 3462f1705ec1SDavid Ahern int _keep_addr; 3463f1705ec1SDavid Ahern bool keep_addr; 346473a8bd74SDavid S. Miller int state, i; 34651da177e4SLinus Torvalds 34661da177e4SLinus Torvalds ASSERT_RTNL(); 34671da177e4SLinus Torvalds 3468f3db4851SDaniel Lezcano rt6_ifdown(net, dev); 346973a8bd74SDavid S. Miller neigh_ifdown(&nd_tbl, dev); 34701da177e4SLinus Torvalds 34711da177e4SLinus Torvalds idev = __in6_dev_get(dev); 347263159f29SIan Morris if (!idev) 34731da177e4SLinus Torvalds return -ENODEV; 34741da177e4SLinus Torvalds 3475bcdd553fSStephen Hemminger /* 3476bcdd553fSStephen Hemminger * Step 1: remove reference to ipv6 device from parent device. 3477bcdd553fSStephen Hemminger * Do not dev_put! 34781da177e4SLinus Torvalds */ 3479439e2385SDenis V. Lunev if (how) { 34801da177e4SLinus Torvalds idev->dead = 1; 34818814c4b5SYOSHIFUJI Hideaki 34828814c4b5SYOSHIFUJI Hideaki /* protected by rtnl_lock */ 3483a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(dev->ip6_ptr, NULL); 34841da177e4SLinus Torvalds 34851da177e4SLinus Torvalds /* Step 1.5: remove snmp6 entry */ 34861da177e4SLinus Torvalds snmp6_unregister_dev(idev); 34871da177e4SLinus Torvalds 34881da177e4SLinus Torvalds } 34891da177e4SLinus Torvalds 3490f1705ec1SDavid Ahern /* aggregate the system setting and interface setting */ 3491f1705ec1SDavid Ahern _keep_addr = net->ipv6.devconf_all->keep_addr_on_down; 3492f1705ec1SDavid Ahern if (!_keep_addr) 3493f1705ec1SDavid Ahern _keep_addr = idev->cnf.keep_addr_on_down; 3494f1705ec1SDavid Ahern 3495f1705ec1SDavid Ahern /* combine the user config with event to determine if permanent 3496f1705ec1SDavid Ahern * addresses are to be removed from address hash table 3497f1705ec1SDavid Ahern */ 3498f1705ec1SDavid Ahern keep_addr = !(how || _keep_addr <= 0); 3499f1705ec1SDavid Ahern 350073a8bd74SDavid S. Miller /* Step 2: clear hash table */ 350173a8bd74SDavid S. Miller for (i = 0; i < IN6_ADDR_HSIZE; i++) { 350273a8bd74SDavid S. Miller struct hlist_head *h = &inet6_addr_lst[i]; 350373a8bd74SDavid S. Miller 350473a8bd74SDavid S. Miller spin_lock_bh(&addrconf_hash_lock); 350573a8bd74SDavid S. Miller restart: 3506b67bfe0dSSasha Levin hlist_for_each_entry_rcu(ifa, h, addr_lst) { 350773a8bd74SDavid S. Miller if (ifa->idev == idev) { 3508c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifa); 3509f1705ec1SDavid Ahern /* combined flag + permanent flag decide if 3510f1705ec1SDavid Ahern * address is retained on a down event 3511f1705ec1SDavid Ahern */ 3512f1705ec1SDavid Ahern if (!keep_addr || 351370af921dSDavid Ahern !(ifa->flags & IFA_F_PERMANENT) || 351470af921dSDavid Ahern addr_is_local(&ifa->addr)) { 3515f1705ec1SDavid Ahern hlist_del_init_rcu(&ifa->addr_lst); 351673a8bd74SDavid S. Miller goto restart; 351773a8bd74SDavid S. Miller } 351873a8bd74SDavid S. Miller } 3519f1705ec1SDavid Ahern } 352073a8bd74SDavid S. Miller spin_unlock_bh(&addrconf_hash_lock); 352173a8bd74SDavid S. Miller } 352273a8bd74SDavid S. Miller 35231da177e4SLinus Torvalds write_lock_bh(&idev->lock); 35241da177e4SLinus Torvalds 3525b7b1bfceSHannes Frederic Sowa addrconf_del_rs_timer(idev); 3526b7b1bfceSHannes Frederic Sowa 3527bcdd553fSStephen Hemminger /* Step 2: clear flags for stateless addrconf */ 3528439e2385SDenis V. Lunev if (!how) 35293c21edbdSYOSHIFUJI Hideaki idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY); 35301da177e4SLinus Torvalds 3531439e2385SDenis V. Lunev if (how && del_timer(&idev->regen_timer)) 35321da177e4SLinus Torvalds in6_dev_put(idev); 35331da177e4SLinus Torvalds 3534bcdd553fSStephen Hemminger /* Step 3: clear tempaddr list */ 3535372e6c8fSstephen hemminger while (!list_empty(&idev->tempaddr_list)) { 3536372e6c8fSstephen hemminger ifa = list_first_entry(&idev->tempaddr_list, 3537372e6c8fSstephen hemminger struct inet6_ifaddr, tmp_list); 3538372e6c8fSstephen hemminger list_del(&ifa->tmp_list); 35391da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 35401da177e4SLinus Torvalds spin_lock_bh(&ifa->lock); 35411da177e4SLinus Torvalds 35421da177e4SLinus Torvalds if (ifa->ifpub) { 35431da177e4SLinus Torvalds in6_ifa_put(ifa->ifpub); 35441da177e4SLinus Torvalds ifa->ifpub = NULL; 35451da177e4SLinus Torvalds } 35461da177e4SLinus Torvalds spin_unlock_bh(&ifa->lock); 35471da177e4SLinus Torvalds in6_ifa_put(ifa); 35481da177e4SLinus Torvalds write_lock_bh(&idev->lock); 35491da177e4SLinus Torvalds } 35508f37ada5Sstephen hemminger 3551f1705ec1SDavid Ahern /* re-combine the user config with event to determine if permanent 3552f1705ec1SDavid Ahern * addresses are to be removed from the interface list 3553f1705ec1SDavid Ahern */ 3554f1705ec1SDavid Ahern keep_addr = (!how && _keep_addr > 0); 3555f1705ec1SDavid Ahern 3556f1705ec1SDavid Ahern INIT_LIST_HEAD(&del_list); 3557f1705ec1SDavid Ahern list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) { 355838bd10c4SDavid Ahern struct rt6_info *rt = NULL; 355938bd10c4SDavid Ahern 3560c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifa); 356184e8b803Sstephen hemminger 35624c5ff6a6SHerbert Xu write_unlock_bh(&idev->lock); 35638e8e676dSHannes Frederic Sowa spin_lock_bh(&ifa->lock); 3564f1705ec1SDavid Ahern 356570af921dSDavid Ahern if (keep_addr && (ifa->flags & IFA_F_PERMANENT) && 356670af921dSDavid Ahern !addr_is_local(&ifa->addr)) { 3567f1705ec1SDavid Ahern /* set state to skip the notifier below */ 3568f1705ec1SDavid Ahern state = INET6_IFADDR_STATE_DEAD; 3569f1705ec1SDavid Ahern ifa->state = 0; 3570f1705ec1SDavid Ahern if (!(ifa->flags & IFA_F_NODAD)) 3571f1705ec1SDavid Ahern ifa->flags |= IFA_F_TENTATIVE; 357238bd10c4SDavid Ahern 357338bd10c4SDavid Ahern rt = ifa->rt; 357438bd10c4SDavid Ahern ifa->rt = NULL; 3575f1705ec1SDavid Ahern } else { 35764c5ff6a6SHerbert Xu state = ifa->state; 35774c5ff6a6SHerbert Xu ifa->state = INET6_IFADDR_STATE_DEAD; 3578f1705ec1SDavid Ahern 3579f1705ec1SDavid Ahern list_del(&ifa->if_list); 3580f1705ec1SDavid Ahern list_add(&ifa->if_list, &del_list); 3581f1705ec1SDavid Ahern } 3582f1705ec1SDavid Ahern 35838e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifa->lock); 35844c5ff6a6SHerbert Xu 358538bd10c4SDavid Ahern if (rt) 358638bd10c4SDavid Ahern ip6_del_rt(rt); 358738bd10c4SDavid Ahern 35889d82ca98SJohn Fastabend if (state != INET6_IFADDR_STATE_DEAD) { 35891da177e4SLinus Torvalds __ipv6_ifa_notify(RTM_DELADDR, ifa); 3590f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_DOWN, ifa); 35912de79570SLorenzo Colitti } 359273a8bd74SDavid S. Miller 35931da177e4SLinus Torvalds write_lock_bh(&idev->lock); 35941da177e4SLinus Torvalds } 35958f37ada5Sstephen hemminger 35961da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 35971da177e4SLinus Torvalds 3598f1705ec1SDavid Ahern /* now clean up addresses to be removed */ 3599f1705ec1SDavid Ahern while (!list_empty(&del_list)) { 3600f1705ec1SDavid Ahern ifa = list_first_entry(&del_list, 3601f1705ec1SDavid Ahern struct inet6_ifaddr, if_list); 3602f1705ec1SDavid Ahern list_del(&ifa->if_list); 3603f1705ec1SDavid Ahern 3604f1705ec1SDavid Ahern in6_ifa_put(ifa); 3605f1705ec1SDavid Ahern } 3606f1705ec1SDavid Ahern 3607381f4dcaSSabrina Dubroca /* Step 5: Discard anycast and multicast list */ 3608381f4dcaSSabrina Dubroca if (how) { 3609381f4dcaSSabrina Dubroca ipv6_ac_destroy_dev(idev); 36101da177e4SLinus Torvalds ipv6_mc_destroy_dev(idev); 3611381f4dcaSSabrina Dubroca } else { 36121da177e4SLinus Torvalds ipv6_mc_down(idev); 3613381f4dcaSSabrina Dubroca } 36141da177e4SLinus Torvalds 36151da177e4SLinus Torvalds idev->tstamp = jiffies; 36161da177e4SLinus Torvalds 3617bcdd553fSStephen Hemminger /* Last: Shot the device (if unregistered) */ 3618439e2385SDenis V. Lunev if (how) { 3619408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 36201da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, idev->nd_parms); 36211da177e4SLinus Torvalds neigh_ifdown(&nd_tbl, dev); 36221da177e4SLinus Torvalds in6_dev_put(idev); 36231da177e4SLinus Torvalds } 36241da177e4SLinus Torvalds return 0; 36251da177e4SLinus Torvalds } 36261da177e4SLinus Torvalds 36271da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data) 36281da177e4SLinus Torvalds { 3629b7b1bfceSHannes Frederic Sowa struct inet6_dev *idev = (struct inet6_dev *)data; 3630caf92bc4SCong Wang struct net_device *dev = idev->dev; 3631b7b1bfceSHannes Frederic Sowa struct in6_addr lladdr; 36321da177e4SLinus Torvalds 3633b7b1bfceSHannes Frederic Sowa write_lock(&idev->lock); 36345b2a1953Sstephen hemminger if (idev->dead || !(idev->if_flags & IF_READY)) 36351da177e4SLinus Torvalds goto out; 36361da177e4SLinus Torvalds 36379ba2add3SShmulik Ladkani if (!ipv6_accept_ra(idev)) 36381da177e4SLinus Torvalds goto out; 36395b2a1953Sstephen hemminger 36405b2a1953Sstephen hemminger /* Announcement received after solicitation was sent */ 36415b2a1953Sstephen hemminger if (idev->if_flags & IF_RA_RCVD) 36425b2a1953Sstephen hemminger goto out; 36431da177e4SLinus Torvalds 3644b7b1bfceSHannes Frederic Sowa if (idev->rs_probes++ < idev->cnf.rtr_solicits) { 3645caf92bc4SCong Wang write_unlock(&idev->lock); 3646caf92bc4SCong Wang if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE)) 3647caf92bc4SCong Wang ndisc_send_rs(dev, &lladdr, 3648b7b1bfceSHannes Frederic Sowa &in6addr_linklocal_allrouters); 3649b7b1bfceSHannes Frederic Sowa else 3650caf92bc4SCong Wang goto put; 3651b7b1bfceSHannes Frederic Sowa 3652caf92bc4SCong Wang write_lock(&idev->lock); 36531da177e4SLinus Torvalds /* The wait after the last probe can be shorter */ 3654b7b1bfceSHannes Frederic Sowa addrconf_mod_rs_timer(idev, (idev->rs_probes == 3655b7b1bfceSHannes Frederic Sowa idev->cnf.rtr_solicits) ? 36565b2a1953Sstephen hemminger idev->cnf.rtr_solicit_delay : 36575b2a1953Sstephen hemminger idev->cnf.rtr_solicit_interval); 36581da177e4SLinus Torvalds } else { 36591da177e4SLinus Torvalds /* 36601da177e4SLinus Torvalds * Note: we do not support deprecated "all on-link" 36611da177e4SLinus Torvalds * assumption any longer. 36621da177e4SLinus Torvalds */ 366391df42beSJoe Perches pr_debug("%s: no IPv6 routers present\n", idev->dev->name); 36641da177e4SLinus Torvalds } 36651da177e4SLinus Torvalds 36661da177e4SLinus Torvalds out: 3667b7b1bfceSHannes Frederic Sowa write_unlock(&idev->lock); 3668caf92bc4SCong Wang put: 3669b7b1bfceSHannes Frederic Sowa in6_dev_put(idev); 36701da177e4SLinus Torvalds } 36711da177e4SLinus Torvalds 36721da177e4SLinus Torvalds /* 36731da177e4SLinus Torvalds * Duplicate Address Detection 36741da177e4SLinus Torvalds */ 36753c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp) 36763c21edbdSYOSHIFUJI Hideaki { 36773c21edbdSYOSHIFUJI Hideaki unsigned long rand_num; 36783c21edbdSYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev; 36793c21edbdSYOSHIFUJI Hideaki 368095c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 368195c385b4SNeil Horman rand_num = 0; 368295c385b4SNeil Horman else 368363862b5bSAruna-Hewapathirane rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1); 368495c385b4SNeil Horman 3685b7b1bfceSHannes Frederic Sowa ifp->dad_probes = idev->cnf.dad_transmits; 3686c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, rand_num); 36873c21edbdSYOSHIFUJI Hideaki } 36883c21edbdSYOSHIFUJI Hideaki 3689c15b1ccaSHannes Frederic Sowa static void addrconf_dad_begin(struct inet6_ifaddr *ifp) 36901da177e4SLinus Torvalds { 36911da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 36921da177e4SLinus Torvalds struct net_device *dev = idev->dev; 369316186a82Ssubashab@codeaurora.org bool notify = false; 36941da177e4SLinus Torvalds 36951da177e4SLinus Torvalds addrconf_join_solict(dev, &ifp->addr); 36961da177e4SLinus Torvalds 369763862b5bSAruna-Hewapathirane prandom_seed((__force u32) ifp->addr.s6_addr32[3]); 36981da177e4SLinus Torvalds 36991da177e4SLinus Torvalds read_lock_bh(&idev->lock); 3700622ccdf1SHerbert Xu spin_lock(&ifp->lock); 3701e9d3e084SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DEAD) 37021da177e4SLinus Torvalds goto out; 37031da177e4SLinus Torvalds 37041da177e4SLinus Torvalds if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) || 37051b34be74SYOSHIFUJI Hideaki idev->cnf.accept_dad < 1 || 370655ebaef1SNoriaki TAKAMIYA !(ifp->flags&IFA_F_TENTATIVE) || 370755ebaef1SNoriaki TAKAMIYA ifp->flags & IFA_F_NODAD) { 3708cc411d0bSBrian Haley ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 370921809fafSstephen hemminger spin_unlock(&ifp->lock); 37101da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 37111da177e4SLinus Torvalds 37121da177e4SLinus Torvalds addrconf_dad_completed(ifp); 37131da177e4SLinus Torvalds return; 37141da177e4SLinus Torvalds } 37151da177e4SLinus Torvalds 37166732badeSYOSHIFUJI Hideaki if (!(idev->if_flags & IF_READY)) { 371721809fafSstephen hemminger spin_unlock(&ifp->lock); 37186732badeSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 37193c21edbdSYOSHIFUJI Hideaki /* 3720590a9887SMasatake YAMATO * If the device is not ready: 37213c21edbdSYOSHIFUJI Hideaki * - keep it tentative if it is a permanent address. 37223c21edbdSYOSHIFUJI Hideaki * - otherwise, kill it. 37233c21edbdSYOSHIFUJI Hideaki */ 37243c21edbdSYOSHIFUJI Hideaki in6_ifa_hold(ifp); 3725cc411d0bSBrian Haley addrconf_dad_stop(ifp, 0); 37266732badeSYOSHIFUJI Hideaki return; 37273c21edbdSYOSHIFUJI Hideaki } 372895c385b4SNeil Horman 372995c385b4SNeil Horman /* 373095c385b4SNeil Horman * Optimistic nodes can start receiving 373195c385b4SNeil Horman * Frames right away 373295c385b4SNeil Horman */ 37337fd2561eSErik Kline if (ifp->flags & IFA_F_OPTIMISTIC) { 373495c385b4SNeil Horman ip6_ins_rt(ifp->rt); 37357fd2561eSErik Kline if (ipv6_use_optimistic_addr(idev)) { 37367fd2561eSErik Kline /* Because optimistic nodes can use this address, 37377fd2561eSErik Kline * notify listeners. If DAD fails, RTM_DELADDR is sent. 37387fd2561eSErik Kline */ 373916186a82Ssubashab@codeaurora.org notify = true; 37407fd2561eSErik Kline } 37417fd2561eSErik Kline } 374295c385b4SNeil Horman 37436732badeSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 37441da177e4SLinus Torvalds out: 3745622ccdf1SHerbert Xu spin_unlock(&ifp->lock); 37461da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 374716186a82Ssubashab@codeaurora.org if (notify) 374816186a82Ssubashab@codeaurora.org ipv6_ifa_notify(RTM_NEWADDR, ifp); 37491da177e4SLinus Torvalds } 37501da177e4SLinus Torvalds 3751c15b1ccaSHannes Frederic Sowa static void addrconf_dad_start(struct inet6_ifaddr *ifp) 37521da177e4SLinus Torvalds { 3753c15b1ccaSHannes Frederic Sowa bool begin_dad = false; 3754c15b1ccaSHannes Frederic Sowa 37558e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 3756c15b1ccaSHannes Frederic Sowa if (ifp->state != INET6_IFADDR_STATE_DEAD) { 3757c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_PREDAD; 3758c15b1ccaSHannes Frederic Sowa begin_dad = true; 3759c15b1ccaSHannes Frederic Sowa } 37608e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 3761c15b1ccaSHannes Frederic Sowa 3762c15b1ccaSHannes Frederic Sowa if (begin_dad) 3763c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 0); 3764c15b1ccaSHannes Frederic Sowa } 3765c15b1ccaSHannes Frederic Sowa 3766c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w) 3767c15b1ccaSHannes Frederic Sowa { 3768c15b1ccaSHannes Frederic Sowa struct inet6_ifaddr *ifp = container_of(to_delayed_work(w), 3769c15b1ccaSHannes Frederic Sowa struct inet6_ifaddr, 3770c15b1ccaSHannes Frederic Sowa dad_work); 37711da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 37721da177e4SLinus Torvalds struct in6_addr mcaddr; 37731da177e4SLinus Torvalds 3774c15b1ccaSHannes Frederic Sowa enum { 3775c15b1ccaSHannes Frederic Sowa DAD_PROCESS, 3776c15b1ccaSHannes Frederic Sowa DAD_BEGIN, 3777c15b1ccaSHannes Frederic Sowa DAD_ABORT, 3778c15b1ccaSHannes Frederic Sowa } action = DAD_PROCESS; 3779c15b1ccaSHannes Frederic Sowa 3780c15b1ccaSHannes Frederic Sowa rtnl_lock(); 3781c15b1ccaSHannes Frederic Sowa 37828e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 3783c15b1ccaSHannes Frederic Sowa if (ifp->state == INET6_IFADDR_STATE_PREDAD) { 3784c15b1ccaSHannes Frederic Sowa action = DAD_BEGIN; 3785c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_DAD; 3786c15b1ccaSHannes Frederic Sowa } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) { 3787c15b1ccaSHannes Frederic Sowa action = DAD_ABORT; 3788c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_POSTDAD; 3789c15b1ccaSHannes Frederic Sowa } 37908e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 3791c15b1ccaSHannes Frederic Sowa 3792c15b1ccaSHannes Frederic Sowa if (action == DAD_BEGIN) { 3793c15b1ccaSHannes Frederic Sowa addrconf_dad_begin(ifp); 3794c15b1ccaSHannes Frederic Sowa goto out; 3795c15b1ccaSHannes Frederic Sowa } else if (action == DAD_ABORT) { 3796c15b1ccaSHannes Frederic Sowa addrconf_dad_stop(ifp, 1); 3797c15b1ccaSHannes Frederic Sowa goto out; 3798c15b1ccaSHannes Frederic Sowa } 3799c15b1ccaSHannes Frederic Sowa 3800b7b1bfceSHannes Frederic Sowa if (!ifp->dad_probes && addrconf_dad_end(ifp)) 3801f2344a13SHerbert Xu goto out; 3802f2344a13SHerbert Xu 3803c15b1ccaSHannes Frederic Sowa write_lock_bh(&idev->lock); 3804122e4519Sstephen hemminger if (idev->dead || !(idev->if_flags & IF_READY)) { 3805c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 38061da177e4SLinus Torvalds goto out; 38071da177e4SLinus Torvalds } 380821809fafSstephen hemminger 380921809fafSstephen hemminger spin_lock(&ifp->lock); 3810622ccdf1SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DEAD) { 3811622ccdf1SHerbert Xu spin_unlock(&ifp->lock); 3812c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 3813622ccdf1SHerbert Xu goto out; 3814622ccdf1SHerbert Xu } 3815622ccdf1SHerbert Xu 3816b7b1bfceSHannes Frederic Sowa if (ifp->dad_probes == 0) { 38171da177e4SLinus Torvalds /* 38181da177e4SLinus Torvalds * DAD was successful 38191da177e4SLinus Torvalds */ 38201da177e4SLinus Torvalds 3821cc411d0bSBrian Haley ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 382221809fafSstephen hemminger spin_unlock(&ifp->lock); 3823c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 38241da177e4SLinus Torvalds 38251da177e4SLinus Torvalds addrconf_dad_completed(ifp); 38261da177e4SLinus Torvalds 38271da177e4SLinus Torvalds goto out; 38281da177e4SLinus Torvalds } 38291da177e4SLinus Torvalds 3830b7b1bfceSHannes Frederic Sowa ifp->dad_probes--; 3831c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 38321f9248e5SJiri Pirko NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME)); 383321809fafSstephen hemminger spin_unlock(&ifp->lock); 3834c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 38351da177e4SLinus Torvalds 38361da177e4SLinus Torvalds /* send a neighbour solicitation for our addr */ 38371da177e4SLinus Torvalds addrconf_addr_solict_mult(&ifp->addr, &mcaddr); 3838304d888bSNicolas Dichtel ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any); 38391da177e4SLinus Torvalds out: 38401da177e4SLinus Torvalds in6_ifa_put(ifp); 3841c15b1ccaSHannes Frederic Sowa rtnl_unlock(); 38421da177e4SLinus Torvalds } 38431da177e4SLinus Torvalds 384411ffff75SHannes Frederic Sowa /* ifp->idev must be at least read locked */ 384511ffff75SHannes Frederic Sowa static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp) 384611ffff75SHannes Frederic Sowa { 384711ffff75SHannes Frederic Sowa struct inet6_ifaddr *ifpiter; 384811ffff75SHannes Frederic Sowa struct inet6_dev *idev = ifp->idev; 384911ffff75SHannes Frederic Sowa 3850602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) { 3851602582caSHannes Frederic Sowa if (ifpiter->scope > IFA_LINK) 3852602582caSHannes Frederic Sowa break; 385311ffff75SHannes Frederic Sowa if (ifp != ifpiter && ifpiter->scope == IFA_LINK && 385411ffff75SHannes Frederic Sowa (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE| 385511ffff75SHannes Frederic Sowa IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) == 385611ffff75SHannes Frederic Sowa IFA_F_PERMANENT) 385711ffff75SHannes Frederic Sowa return false; 385811ffff75SHannes Frederic Sowa } 385911ffff75SHannes Frederic Sowa return true; 386011ffff75SHannes Frederic Sowa } 386111ffff75SHannes Frederic Sowa 38621da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp) 38631da177e4SLinus Torvalds { 38641da177e4SLinus Torvalds struct net_device *dev = ifp->idev->dev; 3865b7b1bfceSHannes Frederic Sowa struct in6_addr lladdr; 3866b173ee48SHannes Frederic Sowa bool send_rs, send_mld; 3867b7b1bfceSHannes Frederic Sowa 3868c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifp); 38691da177e4SLinus Torvalds 38701da177e4SLinus Torvalds /* 38711da177e4SLinus Torvalds * Configure the address for reception. Now it is valid. 38721da177e4SLinus Torvalds */ 38731da177e4SLinus Torvalds 38741da177e4SLinus Torvalds ipv6_ifa_notify(RTM_NEWADDR, ifp); 38751da177e4SLinus Torvalds 3876026359bcSTore Anderson /* If added prefix is link local and we are prepared to process 3877026359bcSTore Anderson router advertisements, start sending router solicitations. 38781da177e4SLinus Torvalds */ 38791da177e4SLinus Torvalds 38801ec047ebSHannes Frederic Sowa read_lock_bh(&ifp->idev->lock); 388111ffff75SHannes Frederic Sowa send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp); 3882b173ee48SHannes Frederic Sowa send_rs = send_mld && 3883b173ee48SHannes Frederic Sowa ipv6_accept_ra(ifp->idev) && 3884b173ee48SHannes Frederic Sowa ifp->idev->cnf.rtr_solicits > 0 && 3885b173ee48SHannes Frederic Sowa (dev->flags&IFF_LOOPBACK) == 0; 38861ec047ebSHannes Frederic Sowa read_unlock_bh(&ifp->idev->lock); 38871ec047ebSHannes Frederic Sowa 3888b173ee48SHannes Frederic Sowa /* While dad is in progress mld report's source address is in6_addrany. 3889b173ee48SHannes Frederic Sowa * Resend with proper ll now. 3890b173ee48SHannes Frederic Sowa */ 3891b173ee48SHannes Frederic Sowa if (send_mld) 3892b173ee48SHannes Frederic Sowa ipv6_mc_dad_complete(ifp->idev); 3893b173ee48SHannes Frederic Sowa 38941ec047ebSHannes Frederic Sowa if (send_rs) { 38951da177e4SLinus Torvalds /* 38961da177e4SLinus Torvalds * If a host as already performed a random delay 38971da177e4SLinus Torvalds * [...] as part of DAD [...] there is no need 38981da177e4SLinus Torvalds * to delay again before sending the first RS 38991da177e4SLinus Torvalds */ 39001ec047ebSHannes Frederic Sowa if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE)) 3901b7b1bfceSHannes Frederic Sowa return; 39021ec047ebSHannes Frederic Sowa ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters); 39031da177e4SLinus Torvalds 3904b7b1bfceSHannes Frederic Sowa write_lock_bh(&ifp->idev->lock); 3905b7b1bfceSHannes Frederic Sowa spin_lock(&ifp->lock); 3906b7b1bfceSHannes Frederic Sowa ifp->idev->rs_probes = 1; 39071da177e4SLinus Torvalds ifp->idev->if_flags |= IF_RS_SENT; 3908b7b1bfceSHannes Frederic Sowa addrconf_mod_rs_timer(ifp->idev, 3909b7b1bfceSHannes Frederic Sowa ifp->idev->cnf.rtr_solicit_interval); 3910b7b1bfceSHannes Frederic Sowa spin_unlock(&ifp->lock); 3911b7b1bfceSHannes Frederic Sowa write_unlock_bh(&ifp->idev->lock); 39121da177e4SLinus Torvalds } 39131da177e4SLinus Torvalds } 39141da177e4SLinus Torvalds 3915e21e8467SStephen Hemminger static void addrconf_dad_run(struct inet6_dev *idev) 3916e21e8467SStephen Hemminger { 3917c5e33bddSYOSHIFUJI Hideaki struct inet6_ifaddr *ifp; 3918c5e33bddSYOSHIFUJI Hideaki 3919c5e33bddSYOSHIFUJI Hideaki read_lock_bh(&idev->lock); 3920502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 392121809fafSstephen hemminger spin_lock(&ifp->lock); 3922f2344a13SHerbert Xu if (ifp->flags & IFA_F_TENTATIVE && 3923f2344a13SHerbert Xu ifp->state == INET6_IFADDR_STATE_DAD) 3924c5e33bddSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 3925f2344a13SHerbert Xu spin_unlock(&ifp->lock); 3926c5e33bddSYOSHIFUJI Hideaki } 3927c5e33bddSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 3928c5e33bddSYOSHIFUJI Hideaki } 3929c5e33bddSYOSHIFUJI Hideaki 39301da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 39311da177e4SLinus Torvalds struct if6_iter_state { 39323c40090aSDaniel Lezcano struct seq_net_private p; 39331da177e4SLinus Torvalds int bucket; 39341d578303SMihai Maruseac int offset; 39351da177e4SLinus Torvalds }; 39361da177e4SLinus Torvalds 39371d578303SMihai Maruseac static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos) 39381da177e4SLinus Torvalds { 39391da177e4SLinus Torvalds struct inet6_ifaddr *ifa = NULL; 39401da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 39411218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 39421d578303SMihai Maruseac int p = 0; 39431da177e4SLinus Torvalds 39441d578303SMihai Maruseac /* initial bucket if pos is 0 */ 39451d578303SMihai Maruseac if (pos == 0) { 39461d578303SMihai Maruseac state->bucket = 0; 39471d578303SMihai Maruseac state->offset = 0; 39481d578303SMihai Maruseac } 39491d578303SMihai Maruseac 39501d578303SMihai Maruseac for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) { 3951b67bfe0dSSasha Levin hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket], 39521d578303SMihai Maruseac addr_lst) { 39539f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 39549f0d3c27SEric Dumazet continue; 39551d578303SMihai Maruseac /* sync with offset */ 39561d578303SMihai Maruseac if (p < state->offset) { 39571d578303SMihai Maruseac p++; 39581d578303SMihai Maruseac continue; 39591d578303SMihai Maruseac } 39601d578303SMihai Maruseac state->offset++; 39611da177e4SLinus Torvalds return ifa; 39621da177e4SLinus Torvalds } 39631d578303SMihai Maruseac 39641d578303SMihai Maruseac /* prepare for next bucket */ 39651d578303SMihai Maruseac state->offset = 0; 39661d578303SMihai Maruseac p = 0; 39671d578303SMihai Maruseac } 3968c2e21293Sstephen hemminger return NULL; 3969c2e21293Sstephen hemminger } 39701da177e4SLinus Torvalds 3971c2e21293Sstephen hemminger static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, 3972c2e21293Sstephen hemminger struct inet6_ifaddr *ifa) 39731da177e4SLinus Torvalds { 39741da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 39751218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 39761da177e4SLinus Torvalds 3977b67bfe0dSSasha Levin hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) { 39789f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 39799f0d3c27SEric Dumazet continue; 39801d578303SMihai Maruseac state->offset++; 39811da177e4SLinus Torvalds return ifa; 39821d578303SMihai Maruseac } 39831da177e4SLinus Torvalds 3984c2e21293Sstephen hemminger while (++state->bucket < IN6_ADDR_HSIZE) { 39851d578303SMihai Maruseac state->offset = 0; 3986b67bfe0dSSasha Levin hlist_for_each_entry_rcu_bh(ifa, 3987c2e21293Sstephen hemminger &inet6_addr_lst[state->bucket], addr_lst) { 39889f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 39899f0d3c27SEric Dumazet continue; 39901d578303SMihai Maruseac state->offset++; 3991c2e21293Sstephen hemminger return ifa; 3992c2e21293Sstephen hemminger } 3993c2e21293Sstephen hemminger } 3994c2e21293Sstephen hemminger 3995c2e21293Sstephen hemminger return NULL; 3996c2e21293Sstephen hemminger } 3997c2e21293Sstephen hemminger 39981da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos) 39994f70eccaSEric Dumazet __acquires(rcu_bh) 40001da177e4SLinus Torvalds { 40015c578aedSstephen hemminger rcu_read_lock_bh(); 40021d578303SMihai Maruseac return if6_get_first(seq, *pos); 40031da177e4SLinus Torvalds } 40041da177e4SLinus Torvalds 40051da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos) 40061da177e4SLinus Torvalds { 40071da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 40081da177e4SLinus Torvalds 40091da177e4SLinus Torvalds ifa = if6_get_next(seq, v); 40101da177e4SLinus Torvalds ++*pos; 40111da177e4SLinus Torvalds return ifa; 40121da177e4SLinus Torvalds } 40131da177e4SLinus Torvalds 40141da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v) 40154f70eccaSEric Dumazet __releases(rcu_bh) 40161da177e4SLinus Torvalds { 40175c578aedSstephen hemminger rcu_read_unlock_bh(); 40181da177e4SLinus Torvalds } 40191da177e4SLinus Torvalds 40201da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v) 40211da177e4SLinus Torvalds { 40221da177e4SLinus Torvalds struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v; 4023971a351cSJiri Pirko seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n", 4024b071195dSHarvey Harrison &ifp->addr, 40251da177e4SLinus Torvalds ifp->idev->dev->ifindex, 40261da177e4SLinus Torvalds ifp->prefix_len, 40271da177e4SLinus Torvalds ifp->scope, 4028971a351cSJiri Pirko (u8) ifp->flags, 40291da177e4SLinus Torvalds ifp->idev->dev->name); 40301da177e4SLinus Torvalds return 0; 40311da177e4SLinus Torvalds } 40321da177e4SLinus Torvalds 403356b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = { 40341da177e4SLinus Torvalds .start = if6_seq_start, 40351da177e4SLinus Torvalds .next = if6_seq_next, 40361da177e4SLinus Torvalds .show = if6_seq_show, 40371da177e4SLinus Torvalds .stop = if6_seq_stop, 40381da177e4SLinus Torvalds }; 40391da177e4SLinus Torvalds 40401da177e4SLinus Torvalds static int if6_seq_open(struct inode *inode, struct file *file) 40411da177e4SLinus Torvalds { 40423c40090aSDaniel Lezcano return seq_open_net(inode, file, &if6_seq_ops, 4043cf7732e4SPavel Emelyanov sizeof(struct if6_iter_state)); 40441da177e4SLinus Torvalds } 40451da177e4SLinus Torvalds 40469a32144eSArjan van de Ven static const struct file_operations if6_fops = { 40471da177e4SLinus Torvalds .owner = THIS_MODULE, 40481da177e4SLinus Torvalds .open = if6_seq_open, 40491da177e4SLinus Torvalds .read = seq_read, 40501da177e4SLinus Torvalds .llseek = seq_lseek, 40513c40090aSDaniel Lezcano .release = seq_release_net, 40521da177e4SLinus Torvalds }; 40531da177e4SLinus Torvalds 40542c8c1e72SAlexey Dobriyan static int __net_init if6_proc_net_init(struct net *net) 40551da177e4SLinus Torvalds { 4056d4beaa66SGao feng if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops)) 40571da177e4SLinus Torvalds return -ENOMEM; 40581da177e4SLinus Torvalds return 0; 40591da177e4SLinus Torvalds } 40601da177e4SLinus Torvalds 40612c8c1e72SAlexey Dobriyan static void __net_exit if6_proc_net_exit(struct net *net) 40623c40090aSDaniel Lezcano { 4063ece31ffdSGao feng remove_proc_entry("if_inet6", net->proc_net); 40643c40090aSDaniel Lezcano } 40653c40090aSDaniel Lezcano 40663c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = { 40673c40090aSDaniel Lezcano .init = if6_proc_net_init, 40683c40090aSDaniel Lezcano .exit = if6_proc_net_exit, 40693c40090aSDaniel Lezcano }; 40703c40090aSDaniel Lezcano 40713c40090aSDaniel Lezcano int __init if6_proc_init(void) 40723c40090aSDaniel Lezcano { 40733c40090aSDaniel Lezcano return register_pernet_subsys(&if6_proc_net_ops); 40743c40090aSDaniel Lezcano } 40753c40090aSDaniel Lezcano 40761da177e4SLinus Torvalds void if6_proc_exit(void) 40771da177e4SLinus Torvalds { 40783c40090aSDaniel Lezcano unregister_pernet_subsys(&if6_proc_net_ops); 40791da177e4SLinus Torvalds } 40801da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 40811da177e4SLinus Torvalds 408207a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6) 40833b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */ 4084b71d1d42SEric Dumazet int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr) 40853b9f9a1cSNoriaki TAKAMIYA { 40863b9f9a1cSNoriaki TAKAMIYA int ret = 0; 4087c2e21293Sstephen hemminger struct inet6_ifaddr *ifp = NULL; 4088ddbe5032SEric Dumazet unsigned int hash = inet6_addr_hash(addr); 4089c2e21293Sstephen hemminger 40905c578aedSstephen hemminger rcu_read_lock_bh(); 4091b67bfe0dSSasha Levin hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) { 4092878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 4093389f6612SDaniel Lezcano continue; 4094caad295fSYOSHIFUJI Hideaki if (ipv6_addr_equal(&ifp->addr, addr) && 40953b9f9a1cSNoriaki TAKAMIYA (ifp->flags & IFA_F_HOMEADDRESS)) { 40963b9f9a1cSNoriaki TAKAMIYA ret = 1; 40973b9f9a1cSNoriaki TAKAMIYA break; 40983b9f9a1cSNoriaki TAKAMIYA } 40993b9f9a1cSNoriaki TAKAMIYA } 41005c578aedSstephen hemminger rcu_read_unlock_bh(); 41013b9f9a1cSNoriaki TAKAMIYA return ret; 41023b9f9a1cSNoriaki TAKAMIYA } 41033b9f9a1cSNoriaki TAKAMIYA #endif 41043b9f9a1cSNoriaki TAKAMIYA 41051da177e4SLinus Torvalds /* 41061da177e4SLinus Torvalds * Periodic address status verification 41071da177e4SLinus Torvalds */ 41081da177e4SLinus Torvalds 4109c15b1ccaSHannes Frederic Sowa static void addrconf_verify_rtnl(void) 41101da177e4SLinus Torvalds { 4111b2db7564SYOSHIFUJI Hideaki unsigned long now, next, next_sec, next_sched; 41121da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 41131da177e4SLinus Torvalds int i; 41141da177e4SLinus Torvalds 4115c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 4116c15b1ccaSHannes Frederic Sowa 41175c578aedSstephen hemminger rcu_read_lock_bh(); 41181da177e4SLinus Torvalds now = jiffies; 4119b2db7564SYOSHIFUJI Hideaki next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY); 41201da177e4SLinus Torvalds 4121c15b1ccaSHannes Frederic Sowa cancel_delayed_work(&addr_chk_work); 41221da177e4SLinus Torvalds 41231da177e4SLinus Torvalds for (i = 0; i < IN6_ADDR_HSIZE; i++) { 41241da177e4SLinus Torvalds restart: 4125c15b1ccaSHannes Frederic Sowa hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) { 41261da177e4SLinus Torvalds unsigned long age; 41271da177e4SLinus Torvalds 4128fad8da3eSYasushi Asano /* When setting preferred_lft to a value not zero or 4129fad8da3eSYasushi Asano * infinity, while valid_lft is infinity 4130fad8da3eSYasushi Asano * IFA_F_PERMANENT has a non-infinity life time. 4131fad8da3eSYasushi Asano */ 4132fad8da3eSYasushi Asano if ((ifp->flags & IFA_F_PERMANENT) && 4133fad8da3eSYasushi Asano (ifp->prefered_lft == INFINITY_LIFE_TIME)) 41341da177e4SLinus Torvalds continue; 41351da177e4SLinus Torvalds 41361da177e4SLinus Torvalds spin_lock(&ifp->lock); 4137b2db7564SYOSHIFUJI Hideaki /* We try to batch several events at once. */ 4138b2db7564SYOSHIFUJI Hideaki age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; 41391da177e4SLinus Torvalds 41408f27ebb9SYOSHIFUJI Hideaki if (ifp->valid_lft != INFINITY_LIFE_TIME && 41418f27ebb9SYOSHIFUJI Hideaki age >= ifp->valid_lft) { 41421da177e4SLinus Torvalds spin_unlock(&ifp->lock); 41431da177e4SLinus Torvalds in6_ifa_hold(ifp); 41441da177e4SLinus Torvalds ipv6_del_addr(ifp); 41451da177e4SLinus Torvalds goto restart; 41468f27ebb9SYOSHIFUJI Hideaki } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) { 41478f27ebb9SYOSHIFUJI Hideaki spin_unlock(&ifp->lock); 41488f27ebb9SYOSHIFUJI Hideaki continue; 41491da177e4SLinus Torvalds } else if (age >= ifp->prefered_lft) { 4150a1ed0526SBrian Haley /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */ 41511da177e4SLinus Torvalds int deprecate = 0; 41521da177e4SLinus Torvalds 41531da177e4SLinus Torvalds if (!(ifp->flags&IFA_F_DEPRECATED)) { 41541da177e4SLinus Torvalds deprecate = 1; 41551da177e4SLinus Torvalds ifp->flags |= IFA_F_DEPRECATED; 41561da177e4SLinus Torvalds } 41571da177e4SLinus Torvalds 4158fad8da3eSYasushi Asano if ((ifp->valid_lft != INFINITY_LIFE_TIME) && 4159fad8da3eSYasushi Asano (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))) 41601da177e4SLinus Torvalds next = ifp->tstamp + ifp->valid_lft * HZ; 41611da177e4SLinus Torvalds 41621da177e4SLinus Torvalds spin_unlock(&ifp->lock); 41631da177e4SLinus Torvalds 41641da177e4SLinus Torvalds if (deprecate) { 41651da177e4SLinus Torvalds in6_ifa_hold(ifp); 41661da177e4SLinus Torvalds 41671da177e4SLinus Torvalds ipv6_ifa_notify(0, ifp); 41681da177e4SLinus Torvalds in6_ifa_put(ifp); 41691da177e4SLinus Torvalds goto restart; 41701da177e4SLinus Torvalds } 41711da177e4SLinus Torvalds } else if ((ifp->flags&IFA_F_TEMPORARY) && 41721da177e4SLinus Torvalds !(ifp->flags&IFA_F_TENTATIVE)) { 417388949cf4Sstephen hemminger unsigned long regen_advance = ifp->idev->cnf.regen_max_retry * 417488949cf4Sstephen hemminger ifp->idev->cnf.dad_transmits * 41751f9248e5SJiri Pirko NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ; 417688949cf4Sstephen hemminger 41771da177e4SLinus Torvalds if (age >= ifp->prefered_lft - regen_advance) { 41781da177e4SLinus Torvalds struct inet6_ifaddr *ifpub = ifp->ifpub; 41791da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 41801da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 41811da177e4SLinus Torvalds if (!ifp->regen_count && ifpub) { 41821da177e4SLinus Torvalds ifp->regen_count++; 41831da177e4SLinus Torvalds in6_ifa_hold(ifp); 41841da177e4SLinus Torvalds in6_ifa_hold(ifpub); 41851da177e4SLinus Torvalds spin_unlock(&ifp->lock); 41865c578aedSstephen hemminger 4187291d809bSHiroyuki YAMAMORI spin_lock(&ifpub->lock); 4188291d809bSHiroyuki YAMAMORI ifpub->regen_count = 0; 4189291d809bSHiroyuki YAMAMORI spin_unlock(&ifpub->lock); 41901da177e4SLinus Torvalds ipv6_create_tempaddr(ifpub, ifp); 41911da177e4SLinus Torvalds in6_ifa_put(ifpub); 41921da177e4SLinus Torvalds in6_ifa_put(ifp); 41931da177e4SLinus Torvalds goto restart; 41941da177e4SLinus Torvalds } 41951da177e4SLinus Torvalds } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next)) 41961da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ; 41971da177e4SLinus Torvalds spin_unlock(&ifp->lock); 41981da177e4SLinus Torvalds } else { 41991da177e4SLinus Torvalds /* ifp->prefered_lft <= ifp->valid_lft */ 42001da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 42011da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 42021da177e4SLinus Torvalds spin_unlock(&ifp->lock); 42031da177e4SLinus Torvalds } 42041da177e4SLinus Torvalds } 42051da177e4SLinus Torvalds } 42061da177e4SLinus Torvalds 4207b2db7564SYOSHIFUJI Hideaki next_sec = round_jiffies_up(next); 4208b2db7564SYOSHIFUJI Hideaki next_sched = next; 4209b2db7564SYOSHIFUJI Hideaki 4210b2db7564SYOSHIFUJI Hideaki /* If rounded timeout is accurate enough, accept it. */ 4211b2db7564SYOSHIFUJI Hideaki if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ)) 4212b2db7564SYOSHIFUJI Hideaki next_sched = next_sec; 4213b2db7564SYOSHIFUJI Hideaki 4214b2db7564SYOSHIFUJI Hideaki /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */ 4215b2db7564SYOSHIFUJI Hideaki if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX)) 4216b2db7564SYOSHIFUJI Hideaki next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX; 4217b2db7564SYOSHIFUJI Hideaki 4218ba3542e1Sdingtianhong ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n", 4219ba3542e1Sdingtianhong now, next, next_sec, next_sched); 4220c15b1ccaSHannes Frederic Sowa mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now); 42215c578aedSstephen hemminger rcu_read_unlock_bh(); 42221da177e4SLinus Torvalds } 42231da177e4SLinus Torvalds 4224c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *w) 4225c15b1ccaSHannes Frederic Sowa { 4226c15b1ccaSHannes Frederic Sowa rtnl_lock(); 4227c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 4228c15b1ccaSHannes Frederic Sowa rtnl_unlock(); 4229c15b1ccaSHannes Frederic Sowa } 4230c15b1ccaSHannes Frederic Sowa 4231c15b1ccaSHannes Frederic Sowa static void addrconf_verify(void) 4232c15b1ccaSHannes Frederic Sowa { 4233c15b1ccaSHannes Frederic Sowa mod_delayed_work(addrconf_wq, &addr_chk_work, 0); 4234c15b1ccaSHannes Frederic Sowa } 4235c15b1ccaSHannes Frederic Sowa 4236caeaba79SNicolas Dichtel static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local, 4237caeaba79SNicolas Dichtel struct in6_addr **peer_pfx) 4238461d8837SThomas Graf { 4239461d8837SThomas Graf struct in6_addr *pfx = NULL; 4240461d8837SThomas Graf 4241caeaba79SNicolas Dichtel *peer_pfx = NULL; 4242caeaba79SNicolas Dichtel 4243461d8837SThomas Graf if (addr) 4244461d8837SThomas Graf pfx = nla_data(addr); 4245461d8837SThomas Graf 4246461d8837SThomas Graf if (local) { 4247461d8837SThomas Graf if (pfx && nla_memcmp(local, pfx, sizeof(*pfx))) 4248caeaba79SNicolas Dichtel *peer_pfx = pfx; 4249461d8837SThomas Graf pfx = nla_data(local); 4250461d8837SThomas Graf } 4251461d8837SThomas Graf 4252461d8837SThomas Graf return pfx; 4253461d8837SThomas Graf } 4254461d8837SThomas Graf 4255ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = { 4256461d8837SThomas Graf [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) }, 4257461d8837SThomas Graf [IFA_LOCAL] = { .len = sizeof(struct in6_addr) }, 4258461d8837SThomas Graf [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) }, 4259479840ffSJiri Pirko [IFA_FLAGS] = { .len = sizeof(u32) }, 4260461d8837SThomas Graf }; 4261461d8837SThomas Graf 42621da177e4SLinus Torvalds static int 4263661d2967SThomas Graf inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh) 42641da177e4SLinus Torvalds { 42653b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 4266b933f716SThomas Graf struct ifaddrmsg *ifm; 4267b933f716SThomas Graf struct nlattr *tb[IFA_MAX+1]; 4268caeaba79SNicolas Dichtel struct in6_addr *pfx, *peer_pfx; 42696046d5b4SHeiner Kallweit u32 ifa_flags; 4270b933f716SThomas Graf int err; 42711da177e4SLinus Torvalds 4272b933f716SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 4273b933f716SThomas Graf if (err < 0) 4274b933f716SThomas Graf return err; 4275b933f716SThomas Graf 4276b933f716SThomas Graf ifm = nlmsg_data(nlh); 4277caeaba79SNicolas Dichtel pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); 427863159f29SIan Morris if (!pfx) 42791da177e4SLinus Torvalds return -EINVAL; 42801da177e4SLinus Torvalds 42816046d5b4SHeiner Kallweit ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags; 42826046d5b4SHeiner Kallweit 42836046d5b4SHeiner Kallweit /* We ignore other flags so far. */ 42846046d5b4SHeiner Kallweit ifa_flags &= IFA_F_MANAGETEMPADDR; 42856046d5b4SHeiner Kallweit 42866046d5b4SHeiner Kallweit return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx, 42876046d5b4SHeiner Kallweit ifm->ifa_prefixlen); 42881da177e4SLinus Torvalds } 42891da177e4SLinus Torvalds 4290479840ffSJiri Pirko static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags, 429155ebaef1SNoriaki TAKAMIYA u32 prefered_lft, u32 valid_lft) 4292081bba5bSNoriaki TAKAMIYA { 42936f704992SYOSHIFUJI Hideaki u32 flags; 42946f704992SYOSHIFUJI Hideaki clock_t expires; 42954bed72e4SYOSHIFUJI Hideaki unsigned long timeout; 429653bd6749SJiri Pirko bool was_managetempaddr; 42975b84efecSThomas Haller bool had_prefixroute; 429846d48046SYOSHIFUJI Hideaki 4299c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 4300c15b1ccaSHannes Frederic Sowa 4301081bba5bSNoriaki TAKAMIYA if (!valid_lft || (prefered_lft > valid_lft)) 4302081bba5bSNoriaki TAKAMIYA return -EINVAL; 4303081bba5bSNoriaki TAKAMIYA 430453bd6749SJiri Pirko if (ifa_flags & IFA_F_MANAGETEMPADDR && 430553bd6749SJiri Pirko (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64)) 430653bd6749SJiri Pirko return -EINVAL; 430753bd6749SJiri Pirko 43084bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(valid_lft, HZ); 43094bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 43104bed72e4SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(timeout * HZ); 43114bed72e4SYOSHIFUJI Hideaki valid_lft = timeout; 43126f704992SYOSHIFUJI Hideaki flags = RTF_EXPIRES; 43134bed72e4SYOSHIFUJI Hideaki } else { 43144bed72e4SYOSHIFUJI Hideaki expires = 0; 43154bed72e4SYOSHIFUJI Hideaki flags = 0; 43164bed72e4SYOSHIFUJI Hideaki ifa_flags |= IFA_F_PERMANENT; 43176f704992SYOSHIFUJI Hideaki } 4318081bba5bSNoriaki TAKAMIYA 43194bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(prefered_lft, HZ); 43204bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 43214bed72e4SYOSHIFUJI Hideaki if (timeout == 0) 432255ebaef1SNoriaki TAKAMIYA ifa_flags |= IFA_F_DEPRECATED; 43234bed72e4SYOSHIFUJI Hideaki prefered_lft = timeout; 43244bed72e4SYOSHIFUJI Hideaki } 4325081bba5bSNoriaki TAKAMIYA 4326081bba5bSNoriaki TAKAMIYA spin_lock_bh(&ifp->lock); 432753bd6749SJiri Pirko was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR; 43285b84efecSThomas Haller had_prefixroute = ifp->flags & IFA_F_PERMANENT && 43295b84efecSThomas Haller !(ifp->flags & IFA_F_NOPREFIXROUTE); 433053bd6749SJiri Pirko ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | 4331761aac73SThomas Haller IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR | 4332761aac73SThomas Haller IFA_F_NOPREFIXROUTE); 433353bd6749SJiri Pirko ifp->flags |= ifa_flags; 4334081bba5bSNoriaki TAKAMIYA ifp->tstamp = jiffies; 4335081bba5bSNoriaki TAKAMIYA ifp->valid_lft = valid_lft; 4336081bba5bSNoriaki TAKAMIYA ifp->prefered_lft = prefered_lft; 4337081bba5bSNoriaki TAKAMIYA 4338081bba5bSNoriaki TAKAMIYA spin_unlock_bh(&ifp->lock); 4339081bba5bSNoriaki TAKAMIYA if (!(ifp->flags&IFA_F_TENTATIVE)) 4340081bba5bSNoriaki TAKAMIYA ipv6_ifa_notify(0, ifp); 4341081bba5bSNoriaki TAKAMIYA 4342761aac73SThomas Haller if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) { 434346d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev, 43446f704992SYOSHIFUJI Hideaki expires, flags); 43455b84efecSThomas Haller } else if (had_prefixroute) { 43465b84efecSThomas Haller enum cleanup_prefix_rt_t action; 43475b84efecSThomas Haller unsigned long rt_expires; 43485b84efecSThomas Haller 43495b84efecSThomas Haller write_lock_bh(&ifp->idev->lock); 43505b84efecSThomas Haller action = check_cleanup_prefix_route(ifp, &rt_expires); 43515b84efecSThomas Haller write_unlock_bh(&ifp->idev->lock); 43525b84efecSThomas Haller 43535b84efecSThomas Haller if (action != CLEANUP_PREFIX_RT_NOP) { 43545b84efecSThomas Haller cleanup_prefix_route(ifp, rt_expires, 43555b84efecSThomas Haller action == CLEANUP_PREFIX_RT_DEL); 43565b84efecSThomas Haller } 4357761aac73SThomas Haller } 435853bd6749SJiri Pirko 435953bd6749SJiri Pirko if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) { 436053bd6749SJiri Pirko if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR)) 436153bd6749SJiri Pirko valid_lft = prefered_lft = 0; 436253bd6749SJiri Pirko manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft, 436353bd6749SJiri Pirko !was_managetempaddr, jiffies); 436453bd6749SJiri Pirko } 436553bd6749SJiri Pirko 4366c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 4367081bba5bSNoriaki TAKAMIYA 4368081bba5bSNoriaki TAKAMIYA return 0; 4369081bba5bSNoriaki TAKAMIYA } 4370081bba5bSNoriaki TAKAMIYA 4371081bba5bSNoriaki TAKAMIYA static int 4372661d2967SThomas Graf inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh) 43731da177e4SLinus Torvalds { 43743b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 4375461d8837SThomas Graf struct ifaddrmsg *ifm; 4376461d8837SThomas Graf struct nlattr *tb[IFA_MAX+1]; 4377caeaba79SNicolas Dichtel struct in6_addr *pfx, *peer_pfx; 43787198f8ceSThomas Graf struct inet6_ifaddr *ifa; 43797198f8ceSThomas Graf struct net_device *dev; 438055ebaef1SNoriaki TAKAMIYA u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME; 4381479840ffSJiri Pirko u32 ifa_flags; 4382461d8837SThomas Graf int err; 43831da177e4SLinus Torvalds 4384461d8837SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 4385461d8837SThomas Graf if (err < 0) 4386461d8837SThomas Graf return err; 4387461d8837SThomas Graf 4388461d8837SThomas Graf ifm = nlmsg_data(nlh); 4389caeaba79SNicolas Dichtel pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); 439063159f29SIan Morris if (!pfx) 43911da177e4SLinus Torvalds return -EINVAL; 43921da177e4SLinus Torvalds 4393461d8837SThomas Graf if (tb[IFA_CACHEINFO]) { 43940778769dSNoriaki TAKAMIYA struct ifa_cacheinfo *ci; 4395461d8837SThomas Graf 4396461d8837SThomas Graf ci = nla_data(tb[IFA_CACHEINFO]); 43970778769dSNoriaki TAKAMIYA valid_lft = ci->ifa_valid; 4398461d8837SThomas Graf preferred_lft = ci->ifa_prefered; 4399461d8837SThomas Graf } else { 4400461d8837SThomas Graf preferred_lft = INFINITY_LIFE_TIME; 4401461d8837SThomas Graf valid_lft = INFINITY_LIFE_TIME; 44020778769dSNoriaki TAKAMIYA } 44030778769dSNoriaki TAKAMIYA 4404af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifm->ifa_index); 440563159f29SIan Morris if (!dev) 44067198f8ceSThomas Graf return -ENODEV; 4407081bba5bSNoriaki TAKAMIYA 4408479840ffSJiri Pirko ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags; 4409479840ffSJiri Pirko 441055ebaef1SNoriaki TAKAMIYA /* We ignore other flags so far. */ 4411761aac73SThomas Haller ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR | 441293a714d6SMadhu Challa IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN; 441355ebaef1SNoriaki TAKAMIYA 44141cab3da6SDaniel Lezcano ifa = ipv6_get_ifaddr(net, pfx, dev, 1); 441563159f29SIan Morris if (!ifa) { 44167198f8ceSThomas Graf /* 44177198f8ceSThomas Graf * It would be best to check for !NLM_F_CREATE here but 4418db9c7c39Sstephen hemminger * userspace already relies on not having to provide this. 44197198f8ceSThomas Graf */ 4420caeaba79SNicolas Dichtel return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx, 4421af284937SDaniel Lezcano ifm->ifa_prefixlen, ifa_flags, 4422af284937SDaniel Lezcano preferred_lft, valid_lft); 44231da177e4SLinus Torvalds } 44241da177e4SLinus Torvalds 44257198f8ceSThomas Graf if (nlh->nlmsg_flags & NLM_F_EXCL || 44267198f8ceSThomas Graf !(nlh->nlmsg_flags & NLM_F_REPLACE)) 44277198f8ceSThomas Graf err = -EEXIST; 44287198f8ceSThomas Graf else 442955ebaef1SNoriaki TAKAMIYA err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft); 44307198f8ceSThomas Graf 44317198f8ceSThomas Graf in6_ifa_put(ifa); 44327198f8ceSThomas Graf 44337198f8ceSThomas Graf return err; 44347198f8ceSThomas Graf } 44357198f8ceSThomas Graf 4436479840ffSJiri Pirko static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags, 4437101bb229SThomas Graf u8 scope, int ifindex) 4438101bb229SThomas Graf { 4439101bb229SThomas Graf struct ifaddrmsg *ifm; 4440101bb229SThomas Graf 4441101bb229SThomas Graf ifm = nlmsg_data(nlh); 4442101bb229SThomas Graf ifm->ifa_family = AF_INET6; 4443101bb229SThomas Graf ifm->ifa_prefixlen = prefixlen; 4444101bb229SThomas Graf ifm->ifa_flags = flags; 4445101bb229SThomas Graf ifm->ifa_scope = scope; 4446101bb229SThomas Graf ifm->ifa_index = ifindex; 4447101bb229SThomas Graf } 4448101bb229SThomas Graf 444985486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp, 445085486af0SThomas Graf unsigned long tstamp, u32 preferred, u32 valid) 445185486af0SThomas Graf { 445285486af0SThomas Graf struct ifa_cacheinfo ci; 445385486af0SThomas Graf 445418a31e1eSThomas Graf ci.cstamp = cstamp_delta(cstamp); 445518a31e1eSThomas Graf ci.tstamp = cstamp_delta(tstamp); 445685486af0SThomas Graf ci.ifa_prefered = preferred; 445785486af0SThomas Graf ci.ifa_valid = valid; 445885486af0SThomas Graf 445985486af0SThomas Graf return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci); 446085486af0SThomas Graf } 446185486af0SThomas Graf 4462101bb229SThomas Graf static inline int rt_scope(int ifa_scope) 4463101bb229SThomas Graf { 4464101bb229SThomas Graf if (ifa_scope & IFA_HOST) 4465101bb229SThomas Graf return RT_SCOPE_HOST; 4466101bb229SThomas Graf else if (ifa_scope & IFA_LINK) 4467101bb229SThomas Graf return RT_SCOPE_LINK; 4468101bb229SThomas Graf else if (ifa_scope & IFA_SITE) 4469101bb229SThomas Graf return RT_SCOPE_SITE; 4470101bb229SThomas Graf else 4471101bb229SThomas Graf return RT_SCOPE_UNIVERSE; 4472101bb229SThomas Graf } 4473101bb229SThomas Graf 44740ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void) 44750ab6803bSThomas Graf { 4476339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifaddrmsg)) 4477caeaba79SNicolas Dichtel + nla_total_size(16) /* IFA_LOCAL */ 4478339bf98fSThomas Graf + nla_total_size(16) /* IFA_ADDRESS */ 4479479840ffSJiri Pirko + nla_total_size(sizeof(struct ifa_cacheinfo)) 4480479840ffSJiri Pirko + nla_total_size(4) /* IFA_FLAGS */; 44810ab6803bSThomas Graf } 4482c5396a31SYOSHIFUJI Hideaki 44831da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa, 448415e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 44851da177e4SLinus Torvalds { 44861da177e4SLinus Torvalds struct nlmsghdr *nlh; 448785486af0SThomas Graf u32 preferred, valid; 44881da177e4SLinus Torvalds 448915e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 449063159f29SIan Morris if (!nlh) 449126932566SPatrick McHardy return -EMSGSIZE; 44920ab6803bSThomas Graf 4493101bb229SThomas Graf put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope), 4494101bb229SThomas Graf ifa->idev->dev->ifindex); 4495101bb229SThomas Graf 4496fad8da3eSYasushi Asano if (!((ifa->flags&IFA_F_PERMANENT) && 4497fad8da3eSYasushi Asano (ifa->prefered_lft == INFINITY_LIFE_TIME))) { 449885486af0SThomas Graf preferred = ifa->prefered_lft; 449985486af0SThomas Graf valid = ifa->valid_lft; 450085486af0SThomas Graf if (preferred != INFINITY_LIFE_TIME) { 45011da177e4SLinus Torvalds long tval = (jiffies - ifa->tstamp)/HZ; 4502a1faa698SJens Rosenboom if (preferred > tval) 450385486af0SThomas Graf preferred -= tval; 4504a1faa698SJens Rosenboom else 4505a1faa698SJens Rosenboom preferred = 0; 4506f56619fcSBen Hutchings if (valid != INFINITY_LIFE_TIME) { 4507f56619fcSBen Hutchings if (valid > tval) 450885486af0SThomas Graf valid -= tval; 4509f56619fcSBen Hutchings else 4510f56619fcSBen Hutchings valid = 0; 4511f56619fcSBen Hutchings } 45121da177e4SLinus Torvalds } 45131da177e4SLinus Torvalds } else { 451485486af0SThomas Graf preferred = INFINITY_LIFE_TIME; 451585486af0SThomas Graf valid = INFINITY_LIFE_TIME; 45161da177e4SLinus Torvalds } 451785486af0SThomas Graf 45187996c799SCong Wang if (!ipv6_addr_any(&ifa->peer_addr)) { 4519930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 || 4520930345eaSJiri Benc nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0) 4521caeaba79SNicolas Dichtel goto error; 4522caeaba79SNicolas Dichtel } else 4523930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0) 4524caeaba79SNicolas Dichtel goto error; 4525caeaba79SNicolas Dichtel 4526caeaba79SNicolas Dichtel if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) 4527caeaba79SNicolas Dichtel goto error; 452885486af0SThomas Graf 4529479840ffSJiri Pirko if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0) 4530479840ffSJiri Pirko goto error; 4531479840ffSJiri Pirko 4532053c095aSJohannes Berg nlmsg_end(skb, nlh); 4533053c095aSJohannes Berg return 0; 4534caeaba79SNicolas Dichtel 4535caeaba79SNicolas Dichtel error: 4536caeaba79SNicolas Dichtel nlmsg_cancel(skb, nlh); 4537caeaba79SNicolas Dichtel return -EMSGSIZE; 45381da177e4SLinus Torvalds } 45391da177e4SLinus Torvalds 45401da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca, 454115e47304SEric W. Biederman u32 portid, u32 seq, int event, u16 flags) 45421da177e4SLinus Torvalds { 45431da177e4SLinus Torvalds struct nlmsghdr *nlh; 4544101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 4545101bb229SThomas Graf int ifindex = ifmca->idev->dev->ifindex; 45461da177e4SLinus Torvalds 45471da177e4SLinus Torvalds if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE) 4548101bb229SThomas Graf scope = RT_SCOPE_SITE; 4549101bb229SThomas Graf 455015e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 455163159f29SIan Morris if (!nlh) 455226932566SPatrick McHardy return -EMSGSIZE; 45530ab6803bSThomas Graf 4554101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 4555930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 || 45560ab6803bSThomas Graf put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp, 455726932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 455826932566SPatrick McHardy nlmsg_cancel(skb, nlh); 455926932566SPatrick McHardy return -EMSGSIZE; 456026932566SPatrick McHardy } 456185486af0SThomas Graf 4562053c095aSJohannes Berg nlmsg_end(skb, nlh); 4563053c095aSJohannes Berg return 0; 45641da177e4SLinus Torvalds } 45651da177e4SLinus Torvalds 45661da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca, 456715e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 45681da177e4SLinus Torvalds { 45691da177e4SLinus Torvalds struct nlmsghdr *nlh; 4570101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 4571101bb229SThomas Graf int ifindex = ifaca->aca_idev->dev->ifindex; 45721da177e4SLinus Torvalds 45731da177e4SLinus Torvalds if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE) 4574101bb229SThomas Graf scope = RT_SCOPE_SITE; 4575101bb229SThomas Graf 457615e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 457763159f29SIan Morris if (!nlh) 457826932566SPatrick McHardy return -EMSGSIZE; 45790ab6803bSThomas Graf 4580101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 4581930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 || 45820ab6803bSThomas Graf put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp, 458326932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 458426932566SPatrick McHardy nlmsg_cancel(skb, nlh); 458526932566SPatrick McHardy return -EMSGSIZE; 458626932566SPatrick McHardy } 458785486af0SThomas Graf 4588053c095aSJohannes Berg nlmsg_end(skb, nlh); 4589053c095aSJohannes Berg return 0; 45901da177e4SLinus Torvalds } 45911da177e4SLinus Torvalds 4592e21e8467SStephen Hemminger enum addr_type_t { 45931da177e4SLinus Torvalds UNICAST_ADDR, 45941da177e4SLinus Torvalds MULTICAST_ADDR, 45951da177e4SLinus Torvalds ANYCAST_ADDR, 45961da177e4SLinus Torvalds }; 45971da177e4SLinus Torvalds 4598234b27c3SEric Dumazet /* called with rcu_read_lock() */ 4599234b27c3SEric Dumazet static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb, 4600234b27c3SEric Dumazet struct netlink_callback *cb, enum addr_type_t type, 4601234b27c3SEric Dumazet int s_ip_idx, int *p_ip_idx) 46021da177e4SLinus Torvalds { 46031da177e4SLinus Torvalds struct ifmcaddr6 *ifmca; 46041da177e4SLinus Torvalds struct ifacaddr6 *ifaca; 4605234b27c3SEric Dumazet int err = 1; 4606234b27c3SEric Dumazet int ip_idx = *p_ip_idx; 46071da177e4SLinus Torvalds 46081da177e4SLinus Torvalds read_lock_bh(&idev->lock); 46091da177e4SLinus Torvalds switch (type) { 4610502a2ffdSstephen hemminger case UNICAST_ADDR: { 4611502a2ffdSstephen hemminger struct inet6_ifaddr *ifa; 4612502a2ffdSstephen hemminger 4613ae9cda5dSYOSHIFUJI Hideaki /* unicast address incl. temp addr */ 4614502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 4615502a2ffdSstephen hemminger if (++ip_idx < s_ip_idx) 46161da177e4SLinus Torvalds continue; 46175d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifaddr(skb, ifa, 461815e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 46195d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 46205d5619b4SYOSHIFUJI Hideaki RTM_NEWADDR, 46215d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 4622053c095aSJohannes Berg if (err < 0) 4623bcd32326SEric Dumazet break; 462463998ac2SNicolas Dichtel nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 46251da177e4SLinus Torvalds } 46261da177e4SLinus Torvalds break; 4627502a2ffdSstephen hemminger } 46281da177e4SLinus Torvalds case MULTICAST_ADDR: 46291da177e4SLinus Torvalds /* multicast address */ 46301da177e4SLinus Torvalds for (ifmca = idev->mc_list; ifmca; 46311da177e4SLinus Torvalds ifmca = ifmca->next, ip_idx++) { 46321da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 46331da177e4SLinus Torvalds continue; 46345d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifmcaddr(skb, ifmca, 463515e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 46365d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 46375d5619b4SYOSHIFUJI Hideaki RTM_GETMULTICAST, 46385d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 4639053c095aSJohannes Berg if (err < 0) 4640bcd32326SEric Dumazet break; 46411da177e4SLinus Torvalds } 46421da177e4SLinus Torvalds break; 46431da177e4SLinus Torvalds case ANYCAST_ADDR: 46441da177e4SLinus Torvalds /* anycast address */ 46451da177e4SLinus Torvalds for (ifaca = idev->ac_list; ifaca; 46461da177e4SLinus Torvalds ifaca = ifaca->aca_next, ip_idx++) { 46471da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 46481da177e4SLinus Torvalds continue; 46495d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifacaddr(skb, ifaca, 465015e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 46515d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 46525d5619b4SYOSHIFUJI Hideaki RTM_GETANYCAST, 46535d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 4654053c095aSJohannes Berg if (err < 0) 4655bcd32326SEric Dumazet break; 46561da177e4SLinus Torvalds } 46571da177e4SLinus Torvalds break; 46581da177e4SLinus Torvalds default: 46591da177e4SLinus Torvalds break; 46601da177e4SLinus Torvalds } 46611da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 4662234b27c3SEric Dumazet *p_ip_idx = ip_idx; 4663234b27c3SEric Dumazet return err; 4664234b27c3SEric Dumazet } 46655d5619b4SYOSHIFUJI Hideaki 4666234b27c3SEric Dumazet static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb, 4667234b27c3SEric Dumazet enum addr_type_t type) 4668234b27c3SEric Dumazet { 4669234b27c3SEric Dumazet struct net *net = sock_net(skb->sk); 4670234b27c3SEric Dumazet int h, s_h; 4671234b27c3SEric Dumazet int idx, ip_idx; 4672234b27c3SEric Dumazet int s_idx, s_ip_idx; 4673234b27c3SEric Dumazet struct net_device *dev; 4674234b27c3SEric Dumazet struct inet6_dev *idev; 4675234b27c3SEric Dumazet struct hlist_head *head; 4676234b27c3SEric Dumazet 4677234b27c3SEric Dumazet s_h = cb->args[0]; 4678234b27c3SEric Dumazet s_idx = idx = cb->args[1]; 4679234b27c3SEric Dumazet s_ip_idx = ip_idx = cb->args[2]; 4680234b27c3SEric Dumazet 4681234b27c3SEric Dumazet rcu_read_lock(); 468263998ac2SNicolas Dichtel cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq; 4683234b27c3SEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 4684234b27c3SEric Dumazet idx = 0; 4685234b27c3SEric Dumazet head = &net->dev_index_head[h]; 4686b67bfe0dSSasha Levin hlist_for_each_entry_rcu(dev, head, index_hlist) { 4687234b27c3SEric Dumazet if (idx < s_idx) 4688234b27c3SEric Dumazet goto cont; 46894b97efdfSPatrick McHardy if (h > s_h || idx > s_idx) 4690234b27c3SEric Dumazet s_ip_idx = 0; 4691234b27c3SEric Dumazet ip_idx = 0; 4692e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 4693e21e8467SStephen Hemminger if (!idev) 4694234b27c3SEric Dumazet goto cont; 4695234b27c3SEric Dumazet 4696234b27c3SEric Dumazet if (in6_dump_addrs(idev, skb, cb, type, 46977b46a644SDavid S. Miller s_ip_idx, &ip_idx) < 0) 4698234b27c3SEric Dumazet goto done; 46997562f876SPavel Emelianov cont: 47007562f876SPavel Emelianov idx++; 47011da177e4SLinus Torvalds } 4702234b27c3SEric Dumazet } 4703234b27c3SEric Dumazet done: 4704234b27c3SEric Dumazet rcu_read_unlock(); 4705234b27c3SEric Dumazet cb->args[0] = h; 4706234b27c3SEric Dumazet cb->args[1] = idx; 4707234b27c3SEric Dumazet cb->args[2] = ip_idx; 4708234b27c3SEric Dumazet 47091da177e4SLinus Torvalds return skb->len; 47101da177e4SLinus Torvalds } 47111da177e4SLinus Torvalds 47121da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) 47131da177e4SLinus Torvalds { 47141da177e4SLinus Torvalds enum addr_type_t type = UNICAST_ADDR; 4715b854272bSDenis V. Lunev 47161da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 47171da177e4SLinus Torvalds } 47181da177e4SLinus Torvalds 47191da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb) 47201da177e4SLinus Torvalds { 47211da177e4SLinus Torvalds enum addr_type_t type = MULTICAST_ADDR; 4722b854272bSDenis V. Lunev 47231da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 47241da177e4SLinus Torvalds } 47251da177e4SLinus Torvalds 47261da177e4SLinus Torvalds 47271da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb) 47281da177e4SLinus Torvalds { 47291da177e4SLinus Torvalds enum addr_type_t type = ANYCAST_ADDR; 4730b854272bSDenis V. Lunev 47311da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 47321da177e4SLinus Torvalds } 47331da177e4SLinus Torvalds 4734661d2967SThomas Graf static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh) 47356c223828SNoriaki TAKAMIYA { 47363b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 47371b29fc2cSThomas Graf struct ifaddrmsg *ifm; 47381b29fc2cSThomas Graf struct nlattr *tb[IFA_MAX+1]; 4739caeaba79SNicolas Dichtel struct in6_addr *addr = NULL, *peer; 47406c223828SNoriaki TAKAMIYA struct net_device *dev = NULL; 47416c223828SNoriaki TAKAMIYA struct inet6_ifaddr *ifa; 47426c223828SNoriaki TAKAMIYA struct sk_buff *skb; 47436c223828SNoriaki TAKAMIYA int err; 47446c223828SNoriaki TAKAMIYA 47451b29fc2cSThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 47461b29fc2cSThomas Graf if (err < 0) 47471b29fc2cSThomas Graf goto errout; 47486c223828SNoriaki TAKAMIYA 4749caeaba79SNicolas Dichtel addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer); 475063159f29SIan Morris if (!addr) { 47511b29fc2cSThomas Graf err = -EINVAL; 47521b29fc2cSThomas Graf goto errout; 47531b29fc2cSThomas Graf } 47541b29fc2cSThomas Graf 47551b29fc2cSThomas Graf ifm = nlmsg_data(nlh); 47566c223828SNoriaki TAKAMIYA if (ifm->ifa_index) 47576fda7350SBenjamin Thery dev = __dev_get_by_index(net, ifm->ifa_index); 47586c223828SNoriaki TAKAMIYA 4759e21e8467SStephen Hemminger ifa = ipv6_get_ifaddr(net, addr, dev, 1); 4760e21e8467SStephen Hemminger if (!ifa) { 47611b29fc2cSThomas Graf err = -EADDRNOTAVAIL; 47621b29fc2cSThomas Graf goto errout; 47636c223828SNoriaki TAKAMIYA } 47646c223828SNoriaki TAKAMIYA 4765e21e8467SStephen Hemminger skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL); 4766e21e8467SStephen Hemminger if (!skb) { 47671b29fc2cSThomas Graf err = -ENOBUFS; 47681b29fc2cSThomas Graf goto errout_ifa; 47691b29fc2cSThomas Graf } 47701b29fc2cSThomas Graf 477115e47304SEric W. Biederman err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid, 47726c223828SNoriaki TAKAMIYA nlh->nlmsg_seq, RTM_NEWADDR, 0); 477326932566SPatrick McHardy if (err < 0) { 477426932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 477526932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 477626932566SPatrick McHardy kfree_skb(skb); 477726932566SPatrick McHardy goto errout_ifa; 477826932566SPatrick McHardy } 477915e47304SEric W. Biederman err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 47801b29fc2cSThomas Graf errout_ifa: 47816c223828SNoriaki TAKAMIYA in6_ifa_put(ifa); 47821b29fc2cSThomas Graf errout: 47836c223828SNoriaki TAKAMIYA return err; 47846c223828SNoriaki TAKAMIYA } 47856c223828SNoriaki TAKAMIYA 47861da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa) 47871da177e4SLinus Torvalds { 47881da177e4SLinus Torvalds struct sk_buff *skb; 4789c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(ifa->idev->dev); 47905d620266SThomas Graf int err = -ENOBUFS; 47911da177e4SLinus Torvalds 47920ab6803bSThomas Graf skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC); 479363159f29SIan Morris if (!skb) 47945d620266SThomas Graf goto errout; 47955d620266SThomas Graf 47965d620266SThomas Graf err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0); 479726932566SPatrick McHardy if (err < 0) { 479826932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 479926932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 480026932566SPatrick McHardy kfree_skb(skb); 480126932566SPatrick McHardy goto errout; 480226932566SPatrick McHardy } 48031ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC); 48041ce85fe4SPablo Neira Ayuso return; 48055d620266SThomas Graf errout: 48065d620266SThomas Graf if (err < 0) 48076fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err); 48081da177e4SLinus Torvalds } 48091da177e4SLinus Torvalds 4810b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf, 48111da177e4SLinus Torvalds __s32 *array, int bytes) 48121da177e4SLinus Torvalds { 481304561c1fSThomas Graf BUG_ON(bytes < (DEVCONF_MAX * 4)); 481404561c1fSThomas Graf 48151da177e4SLinus Torvalds memset(array, 0, bytes); 48161da177e4SLinus Torvalds array[DEVCONF_FORWARDING] = cnf->forwarding; 48171da177e4SLinus Torvalds array[DEVCONF_HOPLIMIT] = cnf->hop_limit; 48181da177e4SLinus Torvalds array[DEVCONF_MTU6] = cnf->mtu6; 48191da177e4SLinus Torvalds array[DEVCONF_ACCEPT_RA] = cnf->accept_ra; 48201da177e4SLinus Torvalds array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects; 48211da177e4SLinus Torvalds array[DEVCONF_AUTOCONF] = cnf->autoconf; 48221da177e4SLinus Torvalds array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits; 48231da177e4SLinus Torvalds array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits; 482493908d19SThomas Graf array[DEVCONF_RTR_SOLICIT_INTERVAL] = 482593908d19SThomas Graf jiffies_to_msecs(cnf->rtr_solicit_interval); 482693908d19SThomas Graf array[DEVCONF_RTR_SOLICIT_DELAY] = 482793908d19SThomas Graf jiffies_to_msecs(cnf->rtr_solicit_delay); 48281da177e4SLinus Torvalds array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version; 4829fc4eba58SHannes Frederic Sowa array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] = 4830fc4eba58SHannes Frederic Sowa jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval); 4831fc4eba58SHannes Frederic Sowa array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] = 4832fc4eba58SHannes Frederic Sowa jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval); 48331da177e4SLinus Torvalds array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr; 48341da177e4SLinus Torvalds array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft; 48351da177e4SLinus Torvalds array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft; 48361da177e4SLinus Torvalds array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry; 48371da177e4SLinus Torvalds array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor; 48381da177e4SLinus Torvalds array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses; 483965f5c7c1SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr; 48408013d1d7SHangbin Liu array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit; 4841c4fd30ebSYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo; 4842930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 4843930d6ff2SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref; 484493908d19SThomas Graf array[DEVCONF_RTR_PROBE_INTERVAL] = 484593908d19SThomas Graf jiffies_to_msecs(cnf->rtr_probe_interval); 4846fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 484709c884d4SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen; 484809c884d4SYOSHIFUJI Hideaki #endif 4849930d6ff2SYOSHIFUJI Hideaki #endif 4850fbea49e1SYOSHIFUJI Hideaki array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp; 48510bcbc926SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route; 485295c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 485395c385b4SNeil Horman array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad; 48547fd2561eSErik Kline array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic; 485595c385b4SNeil Horman #endif 48567bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 48577bc570c8SYOSHIFUJI Hideaki array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding; 48587bc570c8SYOSHIFUJI Hideaki #endif 4859778d80beSYOSHIFUJI Hideaki array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6; 48601b34be74SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad; 4861c3faca05SCosmin Ratiu array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao; 48625cb04436SHannes Frederic Sowa array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify; 4863b800c3b9SHannes Frederic Sowa array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc; 4864d9333196SBen Greear array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local; 4865c2943f14SHarout Hedeshian array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu; 486635103d11SAndy Gospodarek array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown; 48673d1bec99SHannes Frederic Sowa /* we omit DEVCONF_STABLE_SECRET for now */ 48683985e8a3SErik Kline array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only; 4869abbc3043SJohannes Berg array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast; 48707a02bf89SJohannes Berg array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na; 4871f1705ec1SDavid Ahern array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down; 48721da177e4SLinus Torvalds } 48731da177e4SLinus Torvalds 4874b382b191SThomas Graf static inline size_t inet6_ifla6_size(void) 4875b382b191SThomas Graf { 4876b382b191SThomas Graf return nla_total_size(4) /* IFLA_INET6_FLAGS */ 4877b382b191SThomas Graf + nla_total_size(sizeof(struct ifla_cacheinfo)) 4878b382b191SThomas Graf + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */ 4879b382b191SThomas Graf + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */ 4880f53adae4SDaniel Borkmann + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */ 4881f53adae4SDaniel Borkmann + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */ 4882b382b191SThomas Graf } 4883b382b191SThomas Graf 4884339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void) 4885339bf98fSThomas Graf { 4886339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 4887339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 4888339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 4889339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 4890339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 48910344338bSAndy Gospodarek + nla_total_size(1) /* IFLA_OPERSTATE */ 4892b382b191SThomas Graf + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */ 4893339bf98fSThomas Graf } 4894c5396a31SYOSHIFUJI Hideaki 4895be281e55SEric Dumazet static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib, 48967d720c3eSTejun Heo int items, int bytes) 48977f7d9a6bSHerbert Xu { 48987f7d9a6bSHerbert Xu int i; 48997f7d9a6bSHerbert Xu int pad = bytes - sizeof(u64) * items; 49007f7d9a6bSHerbert Xu BUG_ON(pad < 0); 49017f7d9a6bSHerbert Xu 49027f7d9a6bSHerbert Xu /* Use put_unaligned() because stats may not be aligned for u64. */ 49037f7d9a6bSHerbert Xu put_unaligned(items, &stats[0]); 49047f7d9a6bSHerbert Xu for (i = 1; i < items; i++) 4905be281e55SEric Dumazet put_unaligned(atomic_long_read(&mib[i]), &stats[i]); 49067f7d9a6bSHerbert Xu 49077f7d9a6bSHerbert Xu memset(&stats[items], 0, pad); 49087f7d9a6bSHerbert Xu } 49097f7d9a6bSHerbert Xu 4910698365faSWANG Cong static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib, 4911a3a77372SRaghavendra K T int bytes, size_t syncpoff) 49124ce3c183SEric Dumazet { 4913a3a77372SRaghavendra K T int i, c; 4914a3a77372SRaghavendra K T u64 buff[IPSTATS_MIB_MAX]; 4915a3a77372SRaghavendra K T int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX; 4916a3a77372SRaghavendra K T 49174ce3c183SEric Dumazet BUG_ON(pad < 0); 49184ce3c183SEric Dumazet 4919a3a77372SRaghavendra K T memset(buff, 0, sizeof(buff)); 4920a3a77372SRaghavendra K T buff[0] = IPSTATS_MIB_MAX; 49214ce3c183SEric Dumazet 4922a3a77372SRaghavendra K T for_each_possible_cpu(c) { 4923a3a77372SRaghavendra K T for (i = 1; i < IPSTATS_MIB_MAX; i++) 4924a3a77372SRaghavendra K T buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff); 4925a3a77372SRaghavendra K T } 4926a3a77372SRaghavendra K T 4927a3a77372SRaghavendra K T memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64)); 4928a3a77372SRaghavendra K T memset(&stats[IPSTATS_MIB_MAX], 0, pad); 49294ce3c183SEric Dumazet } 49304ce3c183SEric Dumazet 49317f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype, 49327f7d9a6bSHerbert Xu int bytes) 49337f7d9a6bSHerbert Xu { 49347f7d9a6bSHerbert Xu switch (attrtype) { 49357f7d9a6bSHerbert Xu case IFLA_INET6_STATS: 4936a3a77372SRaghavendra K T __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes, 4937a3a77372SRaghavendra K T offsetof(struct ipstats_mib, syncp)); 49387f7d9a6bSHerbert Xu break; 49397f7d9a6bSHerbert Xu case IFLA_INET6_ICMP6STATS: 4940be281e55SEric Dumazet __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, ICMP6_MIB_MAX, bytes); 49417f7d9a6bSHerbert Xu break; 49427f7d9a6bSHerbert Xu } 49437f7d9a6bSHerbert Xu } 49447f7d9a6bSHerbert Xu 4945d5566fd7SSowmini Varadhan static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev, 4946d5566fd7SSowmini Varadhan u32 ext_filter_mask) 49471da177e4SLinus Torvalds { 4948bf99f1bdSYOSHIFUJI Hideaki struct nlattr *nla; 49491da177e4SLinus Torvalds struct ifla_cacheinfo ci; 49501da177e4SLinus Torvalds 4951c78679e8SDavid S. Miller if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags)) 4952c78679e8SDavid S. Miller goto nla_put_failure; 49531da177e4SLinus Torvalds ci.max_reasm_len = IPV6_MAXPLEN; 495424912420SDavid S. Miller ci.tstamp = cstamp_delta(idev->tstamp); 495524912420SDavid S. Miller ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time); 49561f9248e5SJiri Pirko ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME)); 4957c78679e8SDavid S. Miller if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci)) 4958c78679e8SDavid S. Miller goto nla_put_failure; 4959bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32)); 496063159f29SIan Morris if (!nla) 496104561c1fSThomas Graf goto nla_put_failure; 4962bf99f1bdSYOSHIFUJI Hideaki ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla)); 49631da177e4SLinus Torvalds 4964bf99f1bdSYOSHIFUJI Hideaki /* XXX - MC not implemented */ 4965bf99f1bdSYOSHIFUJI Hideaki 4966d5566fd7SSowmini Varadhan if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS) 4967d5566fd7SSowmini Varadhan return 0; 4968d5566fd7SSowmini Varadhan 4969bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64)); 497063159f29SIan Morris if (!nla) 4971bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 4972bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla)); 4973bf99f1bdSYOSHIFUJI Hideaki 4974bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64)); 497563159f29SIan Morris if (!nla) 4976bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 4977bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla)); 49781da177e4SLinus Torvalds 4979f53adae4SDaniel Borkmann nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr)); 498063159f29SIan Morris if (!nla) 4981f53adae4SDaniel Borkmann goto nla_put_failure; 4982bc91b0f0SJiri Pirko 4983bc91b0f0SJiri Pirko if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->addr_gen_mode)) 4984bc91b0f0SJiri Pirko goto nla_put_failure; 4985bc91b0f0SJiri Pirko 4986f53adae4SDaniel Borkmann read_lock_bh(&idev->lock); 4987f53adae4SDaniel Borkmann memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla)); 4988f53adae4SDaniel Borkmann read_unlock_bh(&idev->lock); 4989f53adae4SDaniel Borkmann 4990b382b191SThomas Graf return 0; 4991b382b191SThomas Graf 4992b382b191SThomas Graf nla_put_failure: 4993b382b191SThomas Graf return -EMSGSIZE; 4994b382b191SThomas Graf } 4995b382b191SThomas Graf 4996b1974ed0SArad, Ronen static size_t inet6_get_link_af_size(const struct net_device *dev, 4997b1974ed0SArad, Ronen u32 ext_filter_mask) 4998b382b191SThomas Graf { 4999b382b191SThomas Graf if (!__in6_dev_get(dev)) 5000b382b191SThomas Graf return 0; 5001b382b191SThomas Graf 5002b382b191SThomas Graf return inet6_ifla6_size(); 5003b382b191SThomas Graf } 5004b382b191SThomas Graf 5005d5566fd7SSowmini Varadhan static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev, 5006d5566fd7SSowmini Varadhan u32 ext_filter_mask) 5007b382b191SThomas Graf { 5008b382b191SThomas Graf struct inet6_dev *idev = __in6_dev_get(dev); 5009b382b191SThomas Graf 5010b382b191SThomas Graf if (!idev) 5011b382b191SThomas Graf return -ENODATA; 5012b382b191SThomas Graf 5013d5566fd7SSowmini Varadhan if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0) 5014b382b191SThomas Graf return -EMSGSIZE; 5015b382b191SThomas Graf 5016b382b191SThomas Graf return 0; 5017b382b191SThomas Graf } 5018b382b191SThomas Graf 5019f53adae4SDaniel Borkmann static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token) 5020f53adae4SDaniel Borkmann { 5021f53adae4SDaniel Borkmann struct inet6_ifaddr *ifp; 5022f53adae4SDaniel Borkmann struct net_device *dev = idev->dev; 502347e27d5eSDaniel Borkmann bool clear_token, update_rs = false; 5024dc848292SHannes Frederic Sowa struct in6_addr ll_addr; 5025f53adae4SDaniel Borkmann 5026c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 5027c15b1ccaSHannes Frederic Sowa 502863159f29SIan Morris if (!token) 5029f53adae4SDaniel Borkmann return -EINVAL; 5030f53adae4SDaniel Borkmann if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) 5031f53adae4SDaniel Borkmann return -EINVAL; 5032f53adae4SDaniel Borkmann if (!ipv6_accept_ra(idev)) 5033f53adae4SDaniel Borkmann return -EINVAL; 5034f53adae4SDaniel Borkmann if (idev->cnf.rtr_solicits <= 0) 5035f53adae4SDaniel Borkmann return -EINVAL; 5036f53adae4SDaniel Borkmann 5037f53adae4SDaniel Borkmann write_lock_bh(&idev->lock); 5038f53adae4SDaniel Borkmann 5039f53adae4SDaniel Borkmann BUILD_BUG_ON(sizeof(token->s6_addr) != 16); 5040f53adae4SDaniel Borkmann memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8); 5041f53adae4SDaniel Borkmann 5042f53adae4SDaniel Borkmann write_unlock_bh(&idev->lock); 5043f53adae4SDaniel Borkmann 504447e27d5eSDaniel Borkmann clear_token = ipv6_addr_any(token); 504547e27d5eSDaniel Borkmann if (clear_token) 504647e27d5eSDaniel Borkmann goto update_lft; 504747e27d5eSDaniel Borkmann 5048dc848292SHannes Frederic Sowa if (!idev->dead && (idev->if_flags & IF_READY) && 5049dc848292SHannes Frederic Sowa !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE | 5050dc848292SHannes Frederic Sowa IFA_F_OPTIMISTIC)) { 5051fc403832SDaniel Borkmann /* If we're not ready, then normal ifup will take care 5052fc403832SDaniel Borkmann * of this. Otherwise, we need to request our rs here. 5053fc403832SDaniel Borkmann */ 5054f53adae4SDaniel Borkmann ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters); 5055fc403832SDaniel Borkmann update_rs = true; 5056fc403832SDaniel Borkmann } 5057f53adae4SDaniel Borkmann 505847e27d5eSDaniel Borkmann update_lft: 5059f53adae4SDaniel Borkmann write_lock_bh(&idev->lock); 5060fc403832SDaniel Borkmann 506177ecaaceSHannes Frederic Sowa if (update_rs) { 5062f53adae4SDaniel Borkmann idev->if_flags |= IF_RS_SENT; 506377ecaaceSHannes Frederic Sowa idev->rs_probes = 1; 506477ecaaceSHannes Frederic Sowa addrconf_mod_rs_timer(idev, idev->cnf.rtr_solicit_interval); 506577ecaaceSHannes Frederic Sowa } 5066f53adae4SDaniel Borkmann 5067f53adae4SDaniel Borkmann /* Well, that's kinda nasty ... */ 5068f53adae4SDaniel Borkmann list_for_each_entry(ifp, &idev->addr_list, if_list) { 5069f53adae4SDaniel Borkmann spin_lock(&ifp->lock); 5070617fe29dSDaniel Borkmann if (ifp->tokenized) { 5071f53adae4SDaniel Borkmann ifp->valid_lft = 0; 5072f53adae4SDaniel Borkmann ifp->prefered_lft = 0; 5073f53adae4SDaniel Borkmann } 5074f53adae4SDaniel Borkmann spin_unlock(&ifp->lock); 5075f53adae4SDaniel Borkmann } 5076f53adae4SDaniel Borkmann 5077f53adae4SDaniel Borkmann write_unlock_bh(&idev->lock); 5078b2ed64a9SLubomir Rintel inet6_ifinfo_notify(RTM_NEWLINK, idev); 5079c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 5080f53adae4SDaniel Borkmann return 0; 5081f53adae4SDaniel Borkmann } 5082f53adae4SDaniel Borkmann 508311b1f828SDaniel Borkmann static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = { 508411b1f828SDaniel Borkmann [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 }, 508511b1f828SDaniel Borkmann [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) }, 508611b1f828SDaniel Borkmann }; 508711b1f828SDaniel Borkmann 508811b1f828SDaniel Borkmann static int inet6_validate_link_af(const struct net_device *dev, 508911b1f828SDaniel Borkmann const struct nlattr *nla) 509011b1f828SDaniel Borkmann { 509111b1f828SDaniel Borkmann struct nlattr *tb[IFLA_INET6_MAX + 1]; 509211b1f828SDaniel Borkmann 509311b1f828SDaniel Borkmann if (dev && !__in6_dev_get(dev)) 509411b1f828SDaniel Borkmann return -EAFNOSUPPORT; 509511b1f828SDaniel Borkmann 509611b1f828SDaniel Borkmann return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy); 509711b1f828SDaniel Borkmann } 509811b1f828SDaniel Borkmann 5099f53adae4SDaniel Borkmann static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla) 5100f53adae4SDaniel Borkmann { 5101f53adae4SDaniel Borkmann int err = -EINVAL; 5102f53adae4SDaniel Borkmann struct inet6_dev *idev = __in6_dev_get(dev); 5103f53adae4SDaniel Borkmann struct nlattr *tb[IFLA_INET6_MAX + 1]; 5104f53adae4SDaniel Borkmann 5105f53adae4SDaniel Borkmann if (!idev) 5106f53adae4SDaniel Borkmann return -EAFNOSUPPORT; 5107f53adae4SDaniel Borkmann 5108f53adae4SDaniel Borkmann if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0) 5109f53adae4SDaniel Borkmann BUG(); 5110f53adae4SDaniel Borkmann 5111bc91b0f0SJiri Pirko if (tb[IFLA_INET6_TOKEN]) { 5112f53adae4SDaniel Borkmann err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN])); 5113bc91b0f0SJiri Pirko if (err) 5114bc91b0f0SJiri Pirko return err; 5115bc91b0f0SJiri Pirko } 5116bc91b0f0SJiri Pirko 5117bc91b0f0SJiri Pirko if (tb[IFLA_INET6_ADDR_GEN_MODE]) { 5118bc91b0f0SJiri Pirko u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]); 5119bc91b0f0SJiri Pirko 5120bc91b0f0SJiri Pirko if (mode != IN6_ADDR_GEN_MODE_EUI64 && 5121622c81d5SHannes Frederic Sowa mode != IN6_ADDR_GEN_MODE_NONE && 5122cc9da6ccSBjørn Mork mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY && 5123cc9da6ccSBjørn Mork mode != IN6_ADDR_GEN_MODE_RANDOM) 5124bc91b0f0SJiri Pirko return -EINVAL; 5125622c81d5SHannes Frederic Sowa 5126622c81d5SHannes Frederic Sowa if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY && 5127622c81d5SHannes Frederic Sowa !idev->cnf.stable_secret.initialized && 5128622c81d5SHannes Frederic Sowa !dev_net(dev)->ipv6.devconf_dflt->stable_secret.initialized) 5129622c81d5SHannes Frederic Sowa return -EINVAL; 5130622c81d5SHannes Frederic Sowa 5131bc91b0f0SJiri Pirko idev->addr_gen_mode = mode; 5132bc91b0f0SJiri Pirko err = 0; 5133bc91b0f0SJiri Pirko } 5134f53adae4SDaniel Borkmann 5135f53adae4SDaniel Borkmann return err; 5136f53adae4SDaniel Borkmann } 5137f53adae4SDaniel Borkmann 5138b382b191SThomas Graf static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev, 513915e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 5140b382b191SThomas Graf { 5141b382b191SThomas Graf struct net_device *dev = idev->dev; 5142b382b191SThomas Graf struct ifinfomsg *hdr; 5143b382b191SThomas Graf struct nlmsghdr *nlh; 5144b382b191SThomas Graf void *protoinfo; 5145b382b191SThomas Graf 514615e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags); 514763159f29SIan Morris if (!nlh) 5148b382b191SThomas Graf return -EMSGSIZE; 5149b382b191SThomas Graf 5150b382b191SThomas Graf hdr = nlmsg_data(nlh); 5151b382b191SThomas Graf hdr->ifi_family = AF_INET6; 5152b382b191SThomas Graf hdr->__ifi_pad = 0; 5153b382b191SThomas Graf hdr->ifi_type = dev->type; 5154b382b191SThomas Graf hdr->ifi_index = dev->ifindex; 5155b382b191SThomas Graf hdr->ifi_flags = dev_get_flags(dev); 5156b382b191SThomas Graf hdr->ifi_change = 0; 5157b382b191SThomas Graf 5158c78679e8SDavid S. Miller if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 5159c78679e8SDavid S. Miller (dev->addr_len && 5160c78679e8SDavid S. Miller nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 5161c78679e8SDavid S. Miller nla_put_u32(skb, IFLA_MTU, dev->mtu) || 5162a54acb3aSNicolas Dichtel (dev->ifindex != dev_get_iflink(dev) && 51630344338bSAndy Gospodarek nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) || 51640344338bSAndy Gospodarek nla_put_u8(skb, IFLA_OPERSTATE, 51650344338bSAndy Gospodarek netif_running(dev) ? dev->operstate : IF_OPER_DOWN)) 5166c78679e8SDavid S. Miller goto nla_put_failure; 5167b382b191SThomas Graf protoinfo = nla_nest_start(skb, IFLA_PROTINFO); 516863159f29SIan Morris if (!protoinfo) 5169b382b191SThomas Graf goto nla_put_failure; 5170b382b191SThomas Graf 5171d5566fd7SSowmini Varadhan if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0) 5172b382b191SThomas Graf goto nla_put_failure; 5173b382b191SThomas Graf 517404561c1fSThomas Graf nla_nest_end(skb, protoinfo); 5175053c095aSJohannes Berg nlmsg_end(skb, nlh); 5176053c095aSJohannes Berg return 0; 51771da177e4SLinus Torvalds 517804561c1fSThomas Graf nla_put_failure: 517926932566SPatrick McHardy nlmsg_cancel(skb, nlh); 518026932566SPatrick McHardy return -EMSGSIZE; 51811da177e4SLinus Torvalds } 51821da177e4SLinus Torvalds 51831da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 51841da177e4SLinus Torvalds { 51853b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 518684d2697dSEric Dumazet int h, s_h; 5187434a8a58SDavid S. Miller int idx = 0, s_idx; 51881da177e4SLinus Torvalds struct net_device *dev; 51891da177e4SLinus Torvalds struct inet6_dev *idev; 519084d2697dSEric Dumazet struct hlist_head *head; 51911da177e4SLinus Torvalds 519284d2697dSEric Dumazet s_h = cb->args[0]; 519384d2697dSEric Dumazet s_idx = cb->args[1]; 519484d2697dSEric Dumazet 519584d2697dSEric Dumazet rcu_read_lock(); 519684d2697dSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 51977562f876SPavel Emelianov idx = 0; 519884d2697dSEric Dumazet head = &net->dev_index_head[h]; 5199b67bfe0dSSasha Levin hlist_for_each_entry_rcu(dev, head, index_hlist) { 52001da177e4SLinus Torvalds if (idx < s_idx) 52017562f876SPavel Emelianov goto cont; 520284d2697dSEric Dumazet idev = __in6_dev_get(dev); 520384d2697dSEric Dumazet if (!idev) 52047562f876SPavel Emelianov goto cont; 520584d2697dSEric Dumazet if (inet6_fill_ifinfo(skb, idev, 520615e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 520784d2697dSEric Dumazet cb->nlh->nlmsg_seq, 5208053c095aSJohannes Berg RTM_NEWLINK, NLM_F_MULTI) < 0) 520984d2697dSEric Dumazet goto out; 52107562f876SPavel Emelianov cont: 52117562f876SPavel Emelianov idx++; 52121da177e4SLinus Torvalds } 521384d2697dSEric Dumazet } 521484d2697dSEric Dumazet out: 521584d2697dSEric Dumazet rcu_read_unlock(); 521684d2697dSEric Dumazet cb->args[1] = idx; 521784d2697dSEric Dumazet cb->args[0] = h; 52181da177e4SLinus Torvalds 52191da177e4SLinus Torvalds return skb->len; 52201da177e4SLinus Torvalds } 52211da177e4SLinus Torvalds 52221da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev) 52231da177e4SLinus Torvalds { 52241da177e4SLinus Torvalds struct sk_buff *skb; 5225c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 52268d7a76c9SThomas Graf int err = -ENOBUFS; 52271da177e4SLinus Torvalds 5228339bf98fSThomas Graf skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC); 522963159f29SIan Morris if (!skb) 52308d7a76c9SThomas Graf goto errout; 52318d7a76c9SThomas Graf 52328d7a76c9SThomas Graf err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0); 523326932566SPatrick McHardy if (err < 0) { 523426932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */ 523526932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 523626932566SPatrick McHardy kfree_skb(skb); 523726932566SPatrick McHardy goto errout; 523826932566SPatrick McHardy } 52395f75a104SNicolas Dichtel rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC); 52401ce85fe4SPablo Neira Ayuso return; 52418d7a76c9SThomas Graf errout: 52428d7a76c9SThomas Graf if (err < 0) 52435f75a104SNicolas Dichtel rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err); 52441da177e4SLinus Torvalds } 52451da177e4SLinus Torvalds 5246339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void) 5247339bf98fSThomas Graf { 5248339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct prefixmsg)) 5249339bf98fSThomas Graf + nla_total_size(sizeof(struct in6_addr)) 5250339bf98fSThomas Graf + nla_total_size(sizeof(struct prefix_cacheinfo)); 5251339bf98fSThomas Graf } 5252c5396a31SYOSHIFUJI Hideaki 52531da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev, 525415e47304SEric W. Biederman struct prefix_info *pinfo, u32 portid, u32 seq, 5255b6544c0bSJamal Hadi Salim int event, unsigned int flags) 52561da177e4SLinus Torvalds { 52571da177e4SLinus Torvalds struct prefixmsg *pmsg; 52581da177e4SLinus Torvalds struct nlmsghdr *nlh; 52591da177e4SLinus Torvalds struct prefix_cacheinfo ci; 52601da177e4SLinus Torvalds 526115e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags); 526263159f29SIan Morris if (!nlh) 526326932566SPatrick McHardy return -EMSGSIZE; 52646051e2f4SThomas Graf 52656051e2f4SThomas Graf pmsg = nlmsg_data(nlh); 52661da177e4SLinus Torvalds pmsg->prefix_family = AF_INET6; 52678a47077aSPatrick McHardy pmsg->prefix_pad1 = 0; 52688a47077aSPatrick McHardy pmsg->prefix_pad2 = 0; 52691da177e4SLinus Torvalds pmsg->prefix_ifindex = idev->dev->ifindex; 52701da177e4SLinus Torvalds pmsg->prefix_len = pinfo->prefix_len; 52711da177e4SLinus Torvalds pmsg->prefix_type = pinfo->type; 52728a47077aSPatrick McHardy pmsg->prefix_pad3 = 0; 52731da177e4SLinus Torvalds pmsg->prefix_flags = 0; 52741da177e4SLinus Torvalds if (pinfo->onlink) 52751da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_ONLINK; 52761da177e4SLinus Torvalds if (pinfo->autoconf) 52771da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_AUTOCONF; 52781da177e4SLinus Torvalds 5279c78679e8SDavid S. Miller if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix)) 5280c78679e8SDavid S. Miller goto nla_put_failure; 52811da177e4SLinus Torvalds ci.preferred_time = ntohl(pinfo->prefered); 52821da177e4SLinus Torvalds ci.valid_time = ntohl(pinfo->valid); 5283c78679e8SDavid S. Miller if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci)) 5284c78679e8SDavid S. Miller goto nla_put_failure; 5285053c095aSJohannes Berg nlmsg_end(skb, nlh); 5286053c095aSJohannes Berg return 0; 52871da177e4SLinus Torvalds 52886051e2f4SThomas Graf nla_put_failure: 528926932566SPatrick McHardy nlmsg_cancel(skb, nlh); 529026932566SPatrick McHardy return -EMSGSIZE; 52911da177e4SLinus Torvalds } 52921da177e4SLinus Torvalds 52931da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev, 52941da177e4SLinus Torvalds struct prefix_info *pinfo) 52951da177e4SLinus Torvalds { 52961da177e4SLinus Torvalds struct sk_buff *skb; 5297c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 52988c384bfaSThomas Graf int err = -ENOBUFS; 52991da177e4SLinus Torvalds 5300339bf98fSThomas Graf skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC); 530163159f29SIan Morris if (!skb) 53028c384bfaSThomas Graf goto errout; 53038c384bfaSThomas Graf 53048c384bfaSThomas Graf err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0); 530526932566SPatrick McHardy if (err < 0) { 530626932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */ 530726932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 530826932566SPatrick McHardy kfree_skb(skb); 530926932566SPatrick McHardy goto errout; 531026932566SPatrick McHardy } 53111ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC); 53121ce85fe4SPablo Neira Ayuso return; 53138c384bfaSThomas Graf errout: 53148c384bfaSThomas Graf if (err < 0) 53156fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err); 53161da177e4SLinus Torvalds } 53171da177e4SLinus Torvalds 53181da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 53191da177e4SLinus Torvalds { 532063998ac2SNicolas Dichtel struct net *net = dev_net(ifp->idev->dev); 532163998ac2SNicolas Dichtel 5322c15b1ccaSHannes Frederic Sowa if (event) 5323c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 5324c15b1ccaSHannes Frederic Sowa 53251da177e4SLinus Torvalds inet6_ifa_notify(event ? : RTM_NEWADDR, ifp); 53261da177e4SLinus Torvalds 53271da177e4SLinus Torvalds switch (event) { 53281da177e4SLinus Torvalds case RTM_NEWADDR: 532995c385b4SNeil Horman /* 533095c385b4SNeil Horman * If the address was optimistic 533195c385b4SNeil Horman * we inserted the route at the start of 533295c385b4SNeil Horman * our DAD process, so we don't need 533395c385b4SNeil Horman * to do it again 533495c385b4SNeil Horman */ 533595c385b4SNeil Horman if (!(ifp->rt->rt6i_node)) 533640e22e8fSThomas Graf ip6_ins_rt(ifp->rt); 53371da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 53381da177e4SLinus Torvalds addrconf_join_anycast(ifp); 53397996c799SCong Wang if (!ipv6_addr_any(&ifp->peer_addr)) 5340caeaba79SNicolas Dichtel addrconf_prefix_route(&ifp->peer_addr, 128, 5341caeaba79SNicolas Dichtel ifp->idev->dev, 0, 0); 53421da177e4SLinus Torvalds break; 53431da177e4SLinus Torvalds case RTM_DELADDR: 53441da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 53451da177e4SLinus Torvalds addrconf_leave_anycast(ifp); 53461da177e4SLinus Torvalds addrconf_leave_solict(ifp->idev, &ifp->addr); 53477996c799SCong Wang if (!ipv6_addr_any(&ifp->peer_addr)) { 5348caeaba79SNicolas Dichtel struct rt6_info *rt; 5349caeaba79SNicolas Dichtel 5350e7478dfcSNicolas Dichtel rt = addrconf_get_prefix_route(&ifp->peer_addr, 128, 5351e7478dfcSNicolas Dichtel ifp->idev->dev, 0, 0); 53528e3d5be7SMartin KaFai Lau if (rt) 53538e3d5be7SMartin KaFai Lau ip6_del_rt(rt); 5354caeaba79SNicolas Dichtel } 535538bd10c4SDavid Ahern if (ifp->rt) { 5356d8d1f30bSChangli Gao dst_hold(&ifp->rt->dst); 53578e3d5be7SMartin KaFai Lau ip6_del_rt(ifp->rt); 535838bd10c4SDavid Ahern } 5359705f1c86SHannes Frederic Sowa rt_genid_bump_ipv6(net); 53601da177e4SLinus Torvalds break; 53611da177e4SLinus Torvalds } 536263998ac2SNicolas Dichtel atomic_inc(&net->ipv6.dev_addr_genid); 53631da177e4SLinus Torvalds } 53641da177e4SLinus Torvalds 53651da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 53661da177e4SLinus Torvalds { 53678814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 53681da177e4SLinus Torvalds if (likely(ifp->idev->dead == 0)) 53691da177e4SLinus Torvalds __ipv6_ifa_notify(event, ifp); 53708814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 53711da177e4SLinus Torvalds } 53721da177e4SLinus Torvalds 53731da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 53741da177e4SLinus Torvalds 53751da177e4SLinus Torvalds static 5376fe2c6338SJoe Perches int addrconf_sysctl_forward(struct ctl_table *ctl, int write, 53771da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 53781da177e4SLinus Torvalds { 53791da177e4SLinus Torvalds int *valp = ctl->data; 53801da177e4SLinus Torvalds int val = *valp; 538188af182eSEric W. Biederman loff_t pos = *ppos; 5382fe2c6338SJoe Perches struct ctl_table lctl; 53831da177e4SLinus Torvalds int ret; 53841da177e4SLinus Torvalds 5385013d97e9SFrancesco Ruggeri /* 5386013d97e9SFrancesco Ruggeri * ctl->data points to idev->cnf.forwarding, we should 5387013d97e9SFrancesco Ruggeri * not modify it until we get the rtnl lock. 5388013d97e9SFrancesco Ruggeri */ 5389013d97e9SFrancesco Ruggeri lctl = *ctl; 5390013d97e9SFrancesco Ruggeri lctl.data = &val; 5391013d97e9SFrancesco Ruggeri 5392013d97e9SFrancesco Ruggeri ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 53931da177e4SLinus Torvalds 5394c8fecf22SPavel Emelyanov if (write) 5395b325fddbSStephen Hemminger ret = addrconf_fixup_forwarding(ctl, valp, val); 539688af182eSEric W. Biederman if (ret) 539788af182eSEric W. Biederman *ppos = pos; 53981da177e4SLinus Torvalds return ret; 53991da177e4SLinus Torvalds } 54001da177e4SLinus Torvalds 540177751427SMarcelo Leitner static 5402d6df198dSPhil Sutter int addrconf_sysctl_hop_limit(struct ctl_table *ctl, int write, 5403d6df198dSPhil Sutter void __user *buffer, size_t *lenp, loff_t *ppos) 5404d6df198dSPhil Sutter { 5405d6df198dSPhil Sutter struct ctl_table lctl; 5406d6df198dSPhil Sutter int min_hl = 1, max_hl = 255; 5407d6df198dSPhil Sutter 5408d6df198dSPhil Sutter lctl = *ctl; 5409d6df198dSPhil Sutter lctl.extra1 = &min_hl; 5410d6df198dSPhil Sutter lctl.extra2 = &max_hl; 5411d6df198dSPhil Sutter 5412d6df198dSPhil Sutter return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos); 5413d6df198dSPhil Sutter } 5414d6df198dSPhil Sutter 5415d6df198dSPhil Sutter static 541677751427SMarcelo Leitner int addrconf_sysctl_mtu(struct ctl_table *ctl, int write, 541777751427SMarcelo Leitner void __user *buffer, size_t *lenp, loff_t *ppos) 541877751427SMarcelo Leitner { 541977751427SMarcelo Leitner struct inet6_dev *idev = ctl->extra1; 542077751427SMarcelo Leitner int min_mtu = IPV6_MIN_MTU; 542177751427SMarcelo Leitner struct ctl_table lctl; 542277751427SMarcelo Leitner 542377751427SMarcelo Leitner lctl = *ctl; 542477751427SMarcelo Leitner lctl.extra1 = &min_mtu; 542577751427SMarcelo Leitner lctl.extra2 = idev ? &idev->dev->mtu : NULL; 542677751427SMarcelo Leitner 542777751427SMarcelo Leitner return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos); 542877751427SMarcelo Leitner } 542977751427SMarcelo Leitner 543056d417b1SBrian Haley static void dev_disable_change(struct inet6_dev *idev) 543156d417b1SBrian Haley { 543275538c2bSCong Wang struct netdev_notifier_info info; 543375538c2bSCong Wang 543456d417b1SBrian Haley if (!idev || !idev->dev) 543556d417b1SBrian Haley return; 543656d417b1SBrian Haley 543775538c2bSCong Wang netdev_notifier_info_init(&info, idev->dev); 543856d417b1SBrian Haley if (idev->cnf.disable_ipv6) 543975538c2bSCong Wang addrconf_notify(NULL, NETDEV_DOWN, &info); 544056d417b1SBrian Haley else 544175538c2bSCong Wang addrconf_notify(NULL, NETDEV_UP, &info); 544256d417b1SBrian Haley } 544356d417b1SBrian Haley 544456d417b1SBrian Haley static void addrconf_disable_change(struct net *net, __s32 newf) 544556d417b1SBrian Haley { 544656d417b1SBrian Haley struct net_device *dev; 544756d417b1SBrian Haley struct inet6_dev *idev; 544856d417b1SBrian Haley 544956d417b1SBrian Haley rcu_read_lock(); 5450c6d14c84SEric Dumazet for_each_netdev_rcu(net, dev) { 545156d417b1SBrian Haley idev = __in6_dev_get(dev); 545256d417b1SBrian Haley if (idev) { 545356d417b1SBrian Haley int changed = (!idev->cnf.disable_ipv6) ^ (!newf); 545456d417b1SBrian Haley idev->cnf.disable_ipv6 = newf; 545556d417b1SBrian Haley if (changed) 545656d417b1SBrian Haley dev_disable_change(idev); 545756d417b1SBrian Haley } 545856d417b1SBrian Haley } 5459c6d14c84SEric Dumazet rcu_read_unlock(); 546056d417b1SBrian Haley } 546156d417b1SBrian Haley 5462013d97e9SFrancesco Ruggeri static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf) 546356d417b1SBrian Haley { 546456d417b1SBrian Haley struct net *net; 5465013d97e9SFrancesco Ruggeri int old; 5466013d97e9SFrancesco Ruggeri 5467013d97e9SFrancesco Ruggeri if (!rtnl_trylock()) 5468013d97e9SFrancesco Ruggeri return restart_syscall(); 546956d417b1SBrian Haley 547056d417b1SBrian Haley net = (struct net *)table->extra2; 5471013d97e9SFrancesco Ruggeri old = *p; 5472013d97e9SFrancesco Ruggeri *p = newf; 547356d417b1SBrian Haley 5474013d97e9SFrancesco Ruggeri if (p == &net->ipv6.devconf_dflt->disable_ipv6) { 5475013d97e9SFrancesco Ruggeri rtnl_unlock(); 547656d417b1SBrian Haley return 0; 547788af182eSEric W. Biederman } 547856d417b1SBrian Haley 547956d417b1SBrian Haley if (p == &net->ipv6.devconf_all->disable_ipv6) { 548056d417b1SBrian Haley net->ipv6.devconf_dflt->disable_ipv6 = newf; 548156d417b1SBrian Haley addrconf_disable_change(net, newf); 5482013d97e9SFrancesco Ruggeri } else if ((!newf) ^ (!old)) 548356d417b1SBrian Haley dev_disable_change((struct inet6_dev *)table->extra1); 548456d417b1SBrian Haley 548556d417b1SBrian Haley rtnl_unlock(); 548656d417b1SBrian Haley return 0; 548756d417b1SBrian Haley } 548856d417b1SBrian Haley 548956d417b1SBrian Haley static 5490fe2c6338SJoe Perches int addrconf_sysctl_disable(struct ctl_table *ctl, int write, 549156d417b1SBrian Haley void __user *buffer, size_t *lenp, loff_t *ppos) 549256d417b1SBrian Haley { 549356d417b1SBrian Haley int *valp = ctl->data; 549456d417b1SBrian Haley int val = *valp; 549588af182eSEric W. Biederman loff_t pos = *ppos; 5496fe2c6338SJoe Perches struct ctl_table lctl; 549756d417b1SBrian Haley int ret; 549856d417b1SBrian Haley 5499013d97e9SFrancesco Ruggeri /* 5500013d97e9SFrancesco Ruggeri * ctl->data points to idev->cnf.disable_ipv6, we should 5501013d97e9SFrancesco Ruggeri * not modify it until we get the rtnl lock. 5502013d97e9SFrancesco Ruggeri */ 5503013d97e9SFrancesco Ruggeri lctl = *ctl; 5504013d97e9SFrancesco Ruggeri lctl.data = &val; 5505013d97e9SFrancesco Ruggeri 5506013d97e9SFrancesco Ruggeri ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 550756d417b1SBrian Haley 550856d417b1SBrian Haley if (write) 550956d417b1SBrian Haley ret = addrconf_disable_ipv6(ctl, valp, val); 551088af182eSEric W. Biederman if (ret) 551188af182eSEric W. Biederman *ppos = pos; 551256d417b1SBrian Haley return ret; 551356d417b1SBrian Haley } 551456d417b1SBrian Haley 5515c92d5491Sstephen hemminger static 5516c92d5491Sstephen hemminger int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write, 5517c92d5491Sstephen hemminger void __user *buffer, size_t *lenp, loff_t *ppos) 5518c92d5491Sstephen hemminger { 5519c92d5491Sstephen hemminger int *valp = ctl->data; 5520c92d5491Sstephen hemminger int ret; 5521c92d5491Sstephen hemminger int old, new; 5522c92d5491Sstephen hemminger 5523c92d5491Sstephen hemminger old = *valp; 5524c92d5491Sstephen hemminger ret = proc_dointvec(ctl, write, buffer, lenp, ppos); 5525c92d5491Sstephen hemminger new = *valp; 5526c92d5491Sstephen hemminger 5527c92d5491Sstephen hemminger if (write && old != new) { 5528c92d5491Sstephen hemminger struct net *net = ctl->extra2; 5529c92d5491Sstephen hemminger 5530c92d5491Sstephen hemminger if (!rtnl_trylock()) 5531c92d5491Sstephen hemminger return restart_syscall(); 5532c92d5491Sstephen hemminger 5533c92d5491Sstephen hemminger if (valp == &net->ipv6.devconf_dflt->proxy_ndp) 5534c92d5491Sstephen hemminger inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH, 5535c92d5491Sstephen hemminger NETCONFA_IFINDEX_DEFAULT, 5536c92d5491Sstephen hemminger net->ipv6.devconf_dflt); 5537c92d5491Sstephen hemminger else if (valp == &net->ipv6.devconf_all->proxy_ndp) 5538c92d5491Sstephen hemminger inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH, 5539c92d5491Sstephen hemminger NETCONFA_IFINDEX_ALL, 5540c92d5491Sstephen hemminger net->ipv6.devconf_all); 5541c92d5491Sstephen hemminger else { 5542c92d5491Sstephen hemminger struct inet6_dev *idev = ctl->extra1; 5543c92d5491Sstephen hemminger 5544c92d5491Sstephen hemminger inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH, 5545c92d5491Sstephen hemminger idev->dev->ifindex, 5546c92d5491Sstephen hemminger &idev->cnf); 5547c92d5491Sstephen hemminger } 5548c92d5491Sstephen hemminger rtnl_unlock(); 5549c92d5491Sstephen hemminger } 5550c92d5491Sstephen hemminger 5551c92d5491Sstephen hemminger return ret; 5552c92d5491Sstephen hemminger } 5553c92d5491Sstephen hemminger 55543d1bec99SHannes Frederic Sowa static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write, 55553d1bec99SHannes Frederic Sowa void __user *buffer, size_t *lenp, 55563d1bec99SHannes Frederic Sowa loff_t *ppos) 55573d1bec99SHannes Frederic Sowa { 55583d1bec99SHannes Frederic Sowa int err; 55593d1bec99SHannes Frederic Sowa struct in6_addr addr; 55603d1bec99SHannes Frederic Sowa char str[IPV6_MAX_STRLEN]; 55613d1bec99SHannes Frederic Sowa struct ctl_table lctl = *ctl; 5562622c81d5SHannes Frederic Sowa struct net *net = ctl->extra2; 55633d1bec99SHannes Frederic Sowa struct ipv6_stable_secret *secret = ctl->data; 55643d1bec99SHannes Frederic Sowa 5565622c81d5SHannes Frederic Sowa if (&net->ipv6.devconf_all->stable_secret == ctl->data) 5566622c81d5SHannes Frederic Sowa return -EIO; 5567622c81d5SHannes Frederic Sowa 55683d1bec99SHannes Frederic Sowa lctl.maxlen = IPV6_MAX_STRLEN; 55693d1bec99SHannes Frederic Sowa lctl.data = str; 55703d1bec99SHannes Frederic Sowa 55713d1bec99SHannes Frederic Sowa if (!rtnl_trylock()) 55723d1bec99SHannes Frederic Sowa return restart_syscall(); 55733d1bec99SHannes Frederic Sowa 55743d1bec99SHannes Frederic Sowa if (!write && !secret->initialized) { 55753d1bec99SHannes Frederic Sowa err = -EIO; 55763d1bec99SHannes Frederic Sowa goto out; 55773d1bec99SHannes Frederic Sowa } 55783d1bec99SHannes Frederic Sowa 55795449a5caSWANG Cong err = snprintf(str, sizeof(str), "%pI6", &secret->secret); 55803d1bec99SHannes Frederic Sowa if (err >= sizeof(str)) { 55813d1bec99SHannes Frederic Sowa err = -EIO; 55823d1bec99SHannes Frederic Sowa goto out; 55833d1bec99SHannes Frederic Sowa } 55843d1bec99SHannes Frederic Sowa 55853d1bec99SHannes Frederic Sowa err = proc_dostring(&lctl, write, buffer, lenp, ppos); 55863d1bec99SHannes Frederic Sowa if (err || !write) 55873d1bec99SHannes Frederic Sowa goto out; 55883d1bec99SHannes Frederic Sowa 55893d1bec99SHannes Frederic Sowa if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) { 55903d1bec99SHannes Frederic Sowa err = -EIO; 55913d1bec99SHannes Frederic Sowa goto out; 55923d1bec99SHannes Frederic Sowa } 55933d1bec99SHannes Frederic Sowa 55943d1bec99SHannes Frederic Sowa secret->initialized = true; 55953d1bec99SHannes Frederic Sowa secret->secret = addr; 55963d1bec99SHannes Frederic Sowa 5597622c81d5SHannes Frederic Sowa if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) { 5598622c81d5SHannes Frederic Sowa struct net_device *dev; 5599622c81d5SHannes Frederic Sowa 5600622c81d5SHannes Frederic Sowa for_each_netdev(net, dev) { 5601622c81d5SHannes Frederic Sowa struct inet6_dev *idev = __in6_dev_get(dev); 5602622c81d5SHannes Frederic Sowa 5603622c81d5SHannes Frederic Sowa if (idev) { 5604622c81d5SHannes Frederic Sowa idev->addr_gen_mode = 5605622c81d5SHannes Frederic Sowa IN6_ADDR_GEN_MODE_STABLE_PRIVACY; 5606622c81d5SHannes Frederic Sowa } 5607622c81d5SHannes Frederic Sowa } 5608622c81d5SHannes Frederic Sowa } else { 5609622c81d5SHannes Frederic Sowa struct inet6_dev *idev = ctl->extra1; 5610622c81d5SHannes Frederic Sowa 5611622c81d5SHannes Frederic Sowa idev->addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY; 5612622c81d5SHannes Frederic Sowa } 5613622c81d5SHannes Frederic Sowa 56143d1bec99SHannes Frederic Sowa out: 56153d1bec99SHannes Frederic Sowa rtnl_unlock(); 56163d1bec99SHannes Frederic Sowa 56173d1bec99SHannes Frederic Sowa return err; 56183d1bec99SHannes Frederic Sowa } 5619c92d5491Sstephen hemminger 562035103d11SAndy Gospodarek static 562135103d11SAndy Gospodarek int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl, 562235103d11SAndy Gospodarek int write, 562335103d11SAndy Gospodarek void __user *buffer, 562435103d11SAndy Gospodarek size_t *lenp, 562535103d11SAndy Gospodarek loff_t *ppos) 562635103d11SAndy Gospodarek { 562735103d11SAndy Gospodarek int *valp = ctl->data; 562835103d11SAndy Gospodarek int val = *valp; 562935103d11SAndy Gospodarek loff_t pos = *ppos; 563035103d11SAndy Gospodarek struct ctl_table lctl; 563135103d11SAndy Gospodarek int ret; 563235103d11SAndy Gospodarek 563335103d11SAndy Gospodarek /* ctl->data points to idev->cnf.ignore_routes_when_linkdown 563435103d11SAndy Gospodarek * we should not modify it until we get the rtnl lock. 563535103d11SAndy Gospodarek */ 563635103d11SAndy Gospodarek lctl = *ctl; 563735103d11SAndy Gospodarek lctl.data = &val; 563835103d11SAndy Gospodarek 563935103d11SAndy Gospodarek ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 564035103d11SAndy Gospodarek 564135103d11SAndy Gospodarek if (write) 564235103d11SAndy Gospodarek ret = addrconf_fixup_linkdown(ctl, valp, val); 564335103d11SAndy Gospodarek if (ret) 564435103d11SAndy Gospodarek *ppos = pos; 564535103d11SAndy Gospodarek return ret; 564635103d11SAndy Gospodarek } 564735103d11SAndy Gospodarek 5648607ea7cdSKonstantin Khlebnikov static const struct ctl_table addrconf_sysctl[] = { 56491da177e4SLinus Torvalds { 56501da177e4SLinus Torvalds .procname = "forwarding", 56511da177e4SLinus Torvalds .data = &ipv6_devconf.forwarding, 56521da177e4SLinus Torvalds .maxlen = sizeof(int), 56531da177e4SLinus Torvalds .mode = 0644, 56546d9f239aSAlexey Dobriyan .proc_handler = addrconf_sysctl_forward, 56551da177e4SLinus Torvalds }, 56561da177e4SLinus Torvalds { 56571da177e4SLinus Torvalds .procname = "hop_limit", 56581da177e4SLinus Torvalds .data = &ipv6_devconf.hop_limit, 56591da177e4SLinus Torvalds .maxlen = sizeof(int), 56601da177e4SLinus Torvalds .mode = 0644, 5661d6df198dSPhil Sutter .proc_handler = addrconf_sysctl_hop_limit, 56621da177e4SLinus Torvalds }, 56631da177e4SLinus Torvalds { 56641da177e4SLinus Torvalds .procname = "mtu", 56651da177e4SLinus Torvalds .data = &ipv6_devconf.mtu6, 56661da177e4SLinus Torvalds .maxlen = sizeof(int), 56671da177e4SLinus Torvalds .mode = 0644, 566877751427SMarcelo Leitner .proc_handler = addrconf_sysctl_mtu, 56691da177e4SLinus Torvalds }, 56701da177e4SLinus Torvalds { 56711da177e4SLinus Torvalds .procname = "accept_ra", 56721da177e4SLinus Torvalds .data = &ipv6_devconf.accept_ra, 56731da177e4SLinus Torvalds .maxlen = sizeof(int), 56741da177e4SLinus Torvalds .mode = 0644, 56756d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 56761da177e4SLinus Torvalds }, 56771da177e4SLinus Torvalds { 56781da177e4SLinus Torvalds .procname = "accept_redirects", 56791da177e4SLinus Torvalds .data = &ipv6_devconf.accept_redirects, 56801da177e4SLinus Torvalds .maxlen = sizeof(int), 56811da177e4SLinus Torvalds .mode = 0644, 56826d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 56831da177e4SLinus Torvalds }, 56841da177e4SLinus Torvalds { 56851da177e4SLinus Torvalds .procname = "autoconf", 56861da177e4SLinus Torvalds .data = &ipv6_devconf.autoconf, 56871da177e4SLinus Torvalds .maxlen = sizeof(int), 56881da177e4SLinus Torvalds .mode = 0644, 56896d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 56901da177e4SLinus Torvalds }, 56911da177e4SLinus Torvalds { 56921da177e4SLinus Torvalds .procname = "dad_transmits", 56931da177e4SLinus Torvalds .data = &ipv6_devconf.dad_transmits, 56941da177e4SLinus Torvalds .maxlen = sizeof(int), 56951da177e4SLinus Torvalds .mode = 0644, 56966d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 56971da177e4SLinus Torvalds }, 56981da177e4SLinus Torvalds { 56991da177e4SLinus Torvalds .procname = "router_solicitations", 57001da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicits, 57011da177e4SLinus Torvalds .maxlen = sizeof(int), 57021da177e4SLinus Torvalds .mode = 0644, 57036d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 57041da177e4SLinus Torvalds }, 57051da177e4SLinus Torvalds { 57061da177e4SLinus Torvalds .procname = "router_solicitation_interval", 57071da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_interval, 57081da177e4SLinus Torvalds .maxlen = sizeof(int), 57091da177e4SLinus Torvalds .mode = 0644, 57106d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 57111da177e4SLinus Torvalds }, 57121da177e4SLinus Torvalds { 57131da177e4SLinus Torvalds .procname = "router_solicitation_delay", 57141da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_delay, 57151da177e4SLinus Torvalds .maxlen = sizeof(int), 57161da177e4SLinus Torvalds .mode = 0644, 57176d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 57181da177e4SLinus Torvalds }, 57191da177e4SLinus Torvalds { 57201da177e4SLinus Torvalds .procname = "force_mld_version", 57211da177e4SLinus Torvalds .data = &ipv6_devconf.force_mld_version, 57221da177e4SLinus Torvalds .maxlen = sizeof(int), 57231da177e4SLinus Torvalds .mode = 0644, 57246d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 57251da177e4SLinus Torvalds }, 5726fc4eba58SHannes Frederic Sowa { 5727fc4eba58SHannes Frederic Sowa .procname = "mldv1_unsolicited_report_interval", 5728fc4eba58SHannes Frederic Sowa .data = 5729fc4eba58SHannes Frederic Sowa &ipv6_devconf.mldv1_unsolicited_report_interval, 5730fc4eba58SHannes Frederic Sowa .maxlen = sizeof(int), 5731fc4eba58SHannes Frederic Sowa .mode = 0644, 5732fc4eba58SHannes Frederic Sowa .proc_handler = proc_dointvec_ms_jiffies, 5733fc4eba58SHannes Frederic Sowa }, 5734fc4eba58SHannes Frederic Sowa { 5735fc4eba58SHannes Frederic Sowa .procname = "mldv2_unsolicited_report_interval", 5736fc4eba58SHannes Frederic Sowa .data = 5737fc4eba58SHannes Frederic Sowa &ipv6_devconf.mldv2_unsolicited_report_interval, 5738fc4eba58SHannes Frederic Sowa .maxlen = sizeof(int), 5739fc4eba58SHannes Frederic Sowa .mode = 0644, 5740fc4eba58SHannes Frederic Sowa .proc_handler = proc_dointvec_ms_jiffies, 5741fc4eba58SHannes Frederic Sowa }, 57421da177e4SLinus Torvalds { 57431da177e4SLinus Torvalds .procname = "use_tempaddr", 57441da177e4SLinus Torvalds .data = &ipv6_devconf.use_tempaddr, 57451da177e4SLinus Torvalds .maxlen = sizeof(int), 57461da177e4SLinus Torvalds .mode = 0644, 57476d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 57481da177e4SLinus Torvalds }, 57491da177e4SLinus Torvalds { 57501da177e4SLinus Torvalds .procname = "temp_valid_lft", 57511da177e4SLinus Torvalds .data = &ipv6_devconf.temp_valid_lft, 57521da177e4SLinus Torvalds .maxlen = sizeof(int), 57531da177e4SLinus Torvalds .mode = 0644, 57546d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 57551da177e4SLinus Torvalds }, 57561da177e4SLinus Torvalds { 57571da177e4SLinus Torvalds .procname = "temp_prefered_lft", 57581da177e4SLinus Torvalds .data = &ipv6_devconf.temp_prefered_lft, 57591da177e4SLinus Torvalds .maxlen = sizeof(int), 57601da177e4SLinus Torvalds .mode = 0644, 57616d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 57621da177e4SLinus Torvalds }, 57631da177e4SLinus Torvalds { 57641da177e4SLinus Torvalds .procname = "regen_max_retry", 57651da177e4SLinus Torvalds .data = &ipv6_devconf.regen_max_retry, 57661da177e4SLinus Torvalds .maxlen = sizeof(int), 57671da177e4SLinus Torvalds .mode = 0644, 57686d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 57691da177e4SLinus Torvalds }, 57701da177e4SLinus Torvalds { 57711da177e4SLinus Torvalds .procname = "max_desync_factor", 57721da177e4SLinus Torvalds .data = &ipv6_devconf.max_desync_factor, 57731da177e4SLinus Torvalds .maxlen = sizeof(int), 57741da177e4SLinus Torvalds .mode = 0644, 57756d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 57761da177e4SLinus Torvalds }, 57771da177e4SLinus Torvalds { 57781da177e4SLinus Torvalds .procname = "max_addresses", 57791da177e4SLinus Torvalds .data = &ipv6_devconf.max_addresses, 57801da177e4SLinus Torvalds .maxlen = sizeof(int), 57811da177e4SLinus Torvalds .mode = 0644, 57826d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 57831da177e4SLinus Torvalds }, 57841da177e4SLinus Torvalds { 578565f5c7c1SYOSHIFUJI Hideaki .procname = "accept_ra_defrtr", 578665f5c7c1SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_defrtr, 578765f5c7c1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 578865f5c7c1SYOSHIFUJI Hideaki .mode = 0644, 57896d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 579065f5c7c1SYOSHIFUJI Hideaki }, 579165f5c7c1SYOSHIFUJI Hideaki { 57928013d1d7SHangbin Liu .procname = "accept_ra_min_hop_limit", 57938013d1d7SHangbin Liu .data = &ipv6_devconf.accept_ra_min_hop_limit, 57948013d1d7SHangbin Liu .maxlen = sizeof(int), 57958013d1d7SHangbin Liu .mode = 0644, 57968013d1d7SHangbin Liu .proc_handler = proc_dointvec, 57978013d1d7SHangbin Liu }, 57988013d1d7SHangbin Liu { 5799c4fd30ebSYOSHIFUJI Hideaki .procname = "accept_ra_pinfo", 5800c4fd30ebSYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_pinfo, 5801c4fd30ebSYOSHIFUJI Hideaki .maxlen = sizeof(int), 5802c4fd30ebSYOSHIFUJI Hideaki .mode = 0644, 58036d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 5804c4fd30ebSYOSHIFUJI Hideaki }, 5805930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 5806930d6ff2SYOSHIFUJI Hideaki { 5807930d6ff2SYOSHIFUJI Hideaki .procname = "accept_ra_rtr_pref", 5808930d6ff2SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rtr_pref, 5809930d6ff2SYOSHIFUJI Hideaki .maxlen = sizeof(int), 5810930d6ff2SYOSHIFUJI Hideaki .mode = 0644, 58116d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 5812930d6ff2SYOSHIFUJI Hideaki }, 581352e16356SYOSHIFUJI Hideaki { 581452e16356SYOSHIFUJI Hideaki .procname = "router_probe_interval", 581552e16356SYOSHIFUJI Hideaki .data = &ipv6_devconf.rtr_probe_interval, 581652e16356SYOSHIFUJI Hideaki .maxlen = sizeof(int), 581752e16356SYOSHIFUJI Hideaki .mode = 0644, 58186d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 581952e16356SYOSHIFUJI Hideaki }, 5820fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 582109c884d4SYOSHIFUJI Hideaki { 582209c884d4SYOSHIFUJI Hideaki .procname = "accept_ra_rt_info_max_plen", 582309c884d4SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rt_info_max_plen, 582409c884d4SYOSHIFUJI Hideaki .maxlen = sizeof(int), 582509c884d4SYOSHIFUJI Hideaki .mode = 0644, 58266d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 582709c884d4SYOSHIFUJI Hideaki }, 582809c884d4SYOSHIFUJI Hideaki #endif 5829930d6ff2SYOSHIFUJI Hideaki #endif 5830c4fd30ebSYOSHIFUJI Hideaki { 5831fbea49e1SYOSHIFUJI Hideaki .procname = "proxy_ndp", 5832fbea49e1SYOSHIFUJI Hideaki .data = &ipv6_devconf.proxy_ndp, 5833fbea49e1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 5834fbea49e1SYOSHIFUJI Hideaki .mode = 0644, 5835c92d5491Sstephen hemminger .proc_handler = addrconf_sysctl_proxy_ndp, 5836fbea49e1SYOSHIFUJI Hideaki }, 5837fbea49e1SYOSHIFUJI Hideaki { 58380bcbc926SYOSHIFUJI Hideaki .procname = "accept_source_route", 58390bcbc926SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_source_route, 58400bcbc926SYOSHIFUJI Hideaki .maxlen = sizeof(int), 58410bcbc926SYOSHIFUJI Hideaki .mode = 0644, 58426d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 58430bcbc926SYOSHIFUJI Hideaki }, 584495c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 584595c385b4SNeil Horman { 584695c385b4SNeil Horman .procname = "optimistic_dad", 584795c385b4SNeil Horman .data = &ipv6_devconf.optimistic_dad, 584895c385b4SNeil Horman .maxlen = sizeof(int), 584995c385b4SNeil Horman .mode = 0644, 58506d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 585195c385b4SNeil Horman }, 58527fd2561eSErik Kline { 58537fd2561eSErik Kline .procname = "use_optimistic", 58547fd2561eSErik Kline .data = &ipv6_devconf.use_optimistic, 58557fd2561eSErik Kline .maxlen = sizeof(int), 58567fd2561eSErik Kline .mode = 0644, 58577fd2561eSErik Kline .proc_handler = proc_dointvec, 58587fd2561eSErik Kline }, 585995c385b4SNeil Horman #endif 58607bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 58617bc570c8SYOSHIFUJI Hideaki { 58627bc570c8SYOSHIFUJI Hideaki .procname = "mc_forwarding", 58637bc570c8SYOSHIFUJI Hideaki .data = &ipv6_devconf.mc_forwarding, 58647bc570c8SYOSHIFUJI Hideaki .maxlen = sizeof(int), 5865a4e6db07SDavid S. Miller .mode = 0444, 58666d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 58677bc570c8SYOSHIFUJI Hideaki }, 58687bc570c8SYOSHIFUJI Hideaki #endif 58690bcbc926SYOSHIFUJI Hideaki { 5870778d80beSYOSHIFUJI Hideaki .procname = "disable_ipv6", 5871778d80beSYOSHIFUJI Hideaki .data = &ipv6_devconf.disable_ipv6, 5872778d80beSYOSHIFUJI Hideaki .maxlen = sizeof(int), 5873778d80beSYOSHIFUJI Hideaki .mode = 0644, 587456d417b1SBrian Haley .proc_handler = addrconf_sysctl_disable, 5875778d80beSYOSHIFUJI Hideaki }, 5876778d80beSYOSHIFUJI Hideaki { 58771b34be74SYOSHIFUJI Hideaki .procname = "accept_dad", 58781b34be74SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_dad, 58791b34be74SYOSHIFUJI Hideaki .maxlen = sizeof(int), 58801b34be74SYOSHIFUJI Hideaki .mode = 0644, 58816d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 58821b34be74SYOSHIFUJI Hideaki }, 58831b34be74SYOSHIFUJI Hideaki { 5884f7734fdfSOctavian Purdila .procname = "force_tllao", 5885f7734fdfSOctavian Purdila .data = &ipv6_devconf.force_tllao, 5886f7734fdfSOctavian Purdila .maxlen = sizeof(int), 5887f7734fdfSOctavian Purdila .mode = 0644, 5888f7734fdfSOctavian Purdila .proc_handler = proc_dointvec 5889f7734fdfSOctavian Purdila }, 5890f7734fdfSOctavian Purdila { 58915cb04436SHannes Frederic Sowa .procname = "ndisc_notify", 58925cb04436SHannes Frederic Sowa .data = &ipv6_devconf.ndisc_notify, 58935cb04436SHannes Frederic Sowa .maxlen = sizeof(int), 58945cb04436SHannes Frederic Sowa .mode = 0644, 58955cb04436SHannes Frederic Sowa .proc_handler = proc_dointvec 58965cb04436SHannes Frederic Sowa }, 58975cb04436SHannes Frederic Sowa { 5898b800c3b9SHannes Frederic Sowa .procname = "suppress_frag_ndisc", 5899b800c3b9SHannes Frederic Sowa .data = &ipv6_devconf.suppress_frag_ndisc, 5900b800c3b9SHannes Frederic Sowa .maxlen = sizeof(int), 5901b800c3b9SHannes Frederic Sowa .mode = 0644, 5902b800c3b9SHannes Frederic Sowa .proc_handler = proc_dointvec 5903b800c3b9SHannes Frederic Sowa }, 5904b800c3b9SHannes Frederic Sowa { 5905d9333196SBen Greear .procname = "accept_ra_from_local", 5906d9333196SBen Greear .data = &ipv6_devconf.accept_ra_from_local, 5907d9333196SBen Greear .maxlen = sizeof(int), 5908d9333196SBen Greear .mode = 0644, 5909d9333196SBen Greear .proc_handler = proc_dointvec, 5910d9333196SBen Greear }, 5911d9333196SBen Greear { 5912c2943f14SHarout Hedeshian .procname = "accept_ra_mtu", 5913c2943f14SHarout Hedeshian .data = &ipv6_devconf.accept_ra_mtu, 5914c2943f14SHarout Hedeshian .maxlen = sizeof(int), 5915c2943f14SHarout Hedeshian .mode = 0644, 5916c2943f14SHarout Hedeshian .proc_handler = proc_dointvec, 5917c2943f14SHarout Hedeshian }, 5918c2943f14SHarout Hedeshian { 59193d1bec99SHannes Frederic Sowa .procname = "stable_secret", 59203d1bec99SHannes Frederic Sowa .data = &ipv6_devconf.stable_secret, 59213d1bec99SHannes Frederic Sowa .maxlen = IPV6_MAX_STRLEN, 59223d1bec99SHannes Frederic Sowa .mode = 0600, 59233d1bec99SHannes Frederic Sowa .proc_handler = addrconf_sysctl_stable_secret, 59243d1bec99SHannes Frederic Sowa }, 59253d1bec99SHannes Frederic Sowa { 59263985e8a3SErik Kline .procname = "use_oif_addrs_only", 59273985e8a3SErik Kline .data = &ipv6_devconf.use_oif_addrs_only, 59283985e8a3SErik Kline .maxlen = sizeof(int), 59293985e8a3SErik Kline .mode = 0644, 59303985e8a3SErik Kline .proc_handler = proc_dointvec, 593135103d11SAndy Gospodarek }, 593235103d11SAndy Gospodarek { 593335103d11SAndy Gospodarek .procname = "ignore_routes_with_linkdown", 593435103d11SAndy Gospodarek .data = &ipv6_devconf.ignore_routes_with_linkdown, 593535103d11SAndy Gospodarek .maxlen = sizeof(int), 593635103d11SAndy Gospodarek .mode = 0644, 593735103d11SAndy Gospodarek .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown, 59383985e8a3SErik Kline }, 59393985e8a3SErik Kline { 5940abbc3043SJohannes Berg .procname = "drop_unicast_in_l2_multicast", 5941abbc3043SJohannes Berg .data = &ipv6_devconf.drop_unicast_in_l2_multicast, 5942abbc3043SJohannes Berg .maxlen = sizeof(int), 5943abbc3043SJohannes Berg .mode = 0644, 5944abbc3043SJohannes Berg .proc_handler = proc_dointvec, 5945abbc3043SJohannes Berg }, 5946abbc3043SJohannes Berg { 59477a02bf89SJohannes Berg .procname = "drop_unsolicited_na", 59487a02bf89SJohannes Berg .data = &ipv6_devconf.drop_unsolicited_na, 59497a02bf89SJohannes Berg .maxlen = sizeof(int), 59507a02bf89SJohannes Berg .mode = 0644, 59517a02bf89SJohannes Berg .proc_handler = proc_dointvec, 59527a02bf89SJohannes Berg }, 59537a02bf89SJohannes Berg { 5954f1705ec1SDavid Ahern .procname = "keep_addr_on_down", 5955f1705ec1SDavid Ahern .data = &ipv6_devconf.keep_addr_on_down, 5956f1705ec1SDavid Ahern .maxlen = sizeof(int), 5957f1705ec1SDavid Ahern .mode = 0644, 5958f1705ec1SDavid Ahern .proc_handler = proc_dointvec, 5959f1705ec1SDavid Ahern 5960f1705ec1SDavid Ahern }, 5961f1705ec1SDavid Ahern { 5962f8572d8fSEric W. Biederman /* sentinel */ 59631da177e4SLinus Torvalds } 59641da177e4SLinus Torvalds }; 59651da177e4SLinus Torvalds 5966bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name, 5967f8572d8fSEric W. Biederman struct inet6_dev *idev, struct ipv6_devconf *p) 59681da177e4SLinus Torvalds { 59691da177e4SLinus Torvalds int i; 5970607ea7cdSKonstantin Khlebnikov struct ctl_table *table; 59716105e293SEric W. Biederman char path[sizeof("net/ipv6/conf/") + IFNAMSIZ]; 59721dab6222SPavel Emelyanov 5973607ea7cdSKonstantin Khlebnikov table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL); 5974607ea7cdSKonstantin Khlebnikov if (!table) 5975f68635e6SPavel Emelyanov goto out; 5976f68635e6SPavel Emelyanov 5977607ea7cdSKonstantin Khlebnikov for (i = 0; table[i].data; i++) { 5978607ea7cdSKonstantin Khlebnikov table[i].data += (char *)p - (char *)&ipv6_devconf; 5979607ea7cdSKonstantin Khlebnikov table[i].extra1 = idev; /* embedded; no ref */ 5980607ea7cdSKonstantin Khlebnikov table[i].extra2 = net; 59811da177e4SLinus Torvalds } 59821da177e4SLinus Torvalds 59836105e293SEric W. Biederman snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name); 59841da177e4SLinus Torvalds 5985607ea7cdSKonstantin Khlebnikov p->sysctl_header = register_net_sysctl(net, path, table); 5986607ea7cdSKonstantin Khlebnikov if (!p->sysctl_header) 59876105e293SEric W. Biederman goto free; 5988f68635e6SPavel Emelyanov 598995897312SPavel Emelyanov return 0; 59901da177e4SLinus Torvalds 59911da177e4SLinus Torvalds free: 5992607ea7cdSKonstantin Khlebnikov kfree(table); 5993f68635e6SPavel Emelyanov out: 599495897312SPavel Emelyanov return -ENOBUFS; 59951da177e4SLinus Torvalds } 59961da177e4SLinus Torvalds 5997408c4768SPavel Emelyanov static void __addrconf_sysctl_unregister(struct ipv6_devconf *p) 5998f52295a9SPavel Emelyanov { 5999607ea7cdSKonstantin Khlebnikov struct ctl_table *table; 6000f52295a9SPavel Emelyanov 6001607ea7cdSKonstantin Khlebnikov if (!p->sysctl_header) 6002408c4768SPavel Emelyanov return; 6003408c4768SPavel Emelyanov 6004607ea7cdSKonstantin Khlebnikov table = p->sysctl_header->ctl_table_arg; 6005607ea7cdSKonstantin Khlebnikov unregister_net_sysctl_table(p->sysctl_header); 6006607ea7cdSKonstantin Khlebnikov p->sysctl_header = NULL; 6007607ea7cdSKonstantin Khlebnikov kfree(table); 60081da177e4SLinus Torvalds } 6009408c4768SPavel Emelyanov 6010a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev) 6011408c4768SPavel Emelyanov { 6012a317a2f1SWANG Cong int err; 6013a317a2f1SWANG Cong 6014a317a2f1SWANG Cong if (!sysctl_dev_name_is_allowed(idev->dev->name)) 6015a317a2f1SWANG Cong return -EINVAL; 6016a317a2f1SWANG Cong 6017a317a2f1SWANG Cong err = neigh_sysctl_register(idev->dev, idev->nd_parms, 6018f8572d8fSEric W. Biederman &ndisc_ifinfo_sysctl_change); 6019a317a2f1SWANG Cong if (err) 6020a317a2f1SWANG Cong return err; 6021a317a2f1SWANG Cong err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name, 6022f8572d8fSEric W. Biederman idev, &idev->cnf); 6023a317a2f1SWANG Cong if (err) 6024a317a2f1SWANG Cong neigh_sysctl_unregister(idev->nd_parms); 6025a317a2f1SWANG Cong 6026a317a2f1SWANG Cong return err; 6027408c4768SPavel Emelyanov } 6028408c4768SPavel Emelyanov 6029408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev) 6030408c4768SPavel Emelyanov { 6031408c4768SPavel Emelyanov __addrconf_sysctl_unregister(&idev->cnf); 6032408c4768SPavel Emelyanov neigh_sysctl_unregister(idev->nd_parms); 60331da177e4SLinus Torvalds } 60341da177e4SLinus Torvalds 60351da177e4SLinus Torvalds 60361da177e4SLinus Torvalds #endif 60371da177e4SLinus Torvalds 60382c8c1e72SAlexey Dobriyan static int __net_init addrconf_init_net(struct net *net) 6039e0da5a48SPavel Emelyanov { 6040a79ca223SHong Zhiguo int err = -ENOMEM; 6041e0da5a48SPavel Emelyanov struct ipv6_devconf *all, *dflt; 6042e0da5a48SPavel Emelyanov 6043a79ca223SHong Zhiguo all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL); 604463159f29SIan Morris if (!all) 6045e0da5a48SPavel Emelyanov goto err_alloc_all; 6046e0da5a48SPavel Emelyanov 6047a79ca223SHong Zhiguo dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL); 604863159f29SIan Morris if (!dflt) 6049e0da5a48SPavel Emelyanov goto err_alloc_dflt; 6050a79ca223SHong Zhiguo 605156d417b1SBrian Haley /* these will be inherited by all namespaces */ 605256d417b1SBrian Haley dflt->autoconf = ipv6_defaults.autoconf; 605356d417b1SBrian Haley dflt->disable_ipv6 = ipv6_defaults.disable_ipv6; 6054e0da5a48SPavel Emelyanov 60553d1bec99SHannes Frederic Sowa dflt->stable_secret.initialized = false; 60563d1bec99SHannes Frederic Sowa all->stable_secret.initialized = false; 60573d1bec99SHannes Frederic Sowa 6058e0da5a48SPavel Emelyanov net->ipv6.devconf_all = all; 6059e0da5a48SPavel Emelyanov net->ipv6.devconf_dflt = dflt; 6060e0da5a48SPavel Emelyanov 6061e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 6062f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "all", NULL, all); 6063e0da5a48SPavel Emelyanov if (err < 0) 6064e0da5a48SPavel Emelyanov goto err_reg_all; 6065e0da5a48SPavel Emelyanov 6066f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "default", NULL, dflt); 6067e0da5a48SPavel Emelyanov if (err < 0) 6068e0da5a48SPavel Emelyanov goto err_reg_dflt; 6069e0da5a48SPavel Emelyanov #endif 6070e0da5a48SPavel Emelyanov return 0; 6071e0da5a48SPavel Emelyanov 6072e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 6073e0da5a48SPavel Emelyanov err_reg_dflt: 6074e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(all); 6075e0da5a48SPavel Emelyanov err_reg_all: 6076e0da5a48SPavel Emelyanov kfree(dflt); 6077e0da5a48SPavel Emelyanov #endif 6078e0da5a48SPavel Emelyanov err_alloc_dflt: 6079e0da5a48SPavel Emelyanov kfree(all); 6080e0da5a48SPavel Emelyanov err_alloc_all: 6081e0da5a48SPavel Emelyanov return err; 6082e0da5a48SPavel Emelyanov } 6083e0da5a48SPavel Emelyanov 60842c8c1e72SAlexey Dobriyan static void __net_exit addrconf_exit_net(struct net *net) 6085e0da5a48SPavel Emelyanov { 6086e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 6087e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(net->ipv6.devconf_dflt); 6088e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(net->ipv6.devconf_all); 6089e0da5a48SPavel Emelyanov #endif 6090e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_dflt); 6091e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_all); 6092e0da5a48SPavel Emelyanov } 6093e0da5a48SPavel Emelyanov 6094e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = { 6095e0da5a48SPavel Emelyanov .init = addrconf_init_net, 6096e0da5a48SPavel Emelyanov .exit = addrconf_exit_net, 6097e0da5a48SPavel Emelyanov }; 6098e0da5a48SPavel Emelyanov 6099207895fdSDaniel Borkmann static struct rtnl_af_ops inet6_ops __read_mostly = { 6100b382b191SThomas Graf .family = AF_INET6, 6101b382b191SThomas Graf .fill_link_af = inet6_fill_link_af, 6102b382b191SThomas Graf .get_link_af_size = inet6_get_link_af_size, 610311b1f828SDaniel Borkmann .validate_link_af = inet6_validate_link_af, 6104f53adae4SDaniel Borkmann .set_link_af = inet6_set_link_af, 6105b382b191SThomas Graf }; 6106b382b191SThomas Graf 61071da177e4SLinus Torvalds /* 61081da177e4SLinus Torvalds * Init / cleanup code 61091da177e4SLinus Torvalds */ 61101da177e4SLinus Torvalds 61111da177e4SLinus Torvalds int __init addrconf_init(void) 61121da177e4SLinus Torvalds { 6113a317a2f1SWANG Cong struct inet6_dev *idev; 6114c2e21293Sstephen hemminger int i, err; 61152a8cc6c8SYOSHIFUJI Hideaki 6116e21e8467SStephen Hemminger err = ipv6_addr_label_init(); 6117e21e8467SStephen Hemminger if (err < 0) { 6118f3213831SJoe Perches pr_crit("%s: cannot initialize default policy table: %d\n", 6119f3213831SJoe Perches __func__, err); 61202cc6d2bfSNeil Horman goto out; 61212a8cc6c8SYOSHIFUJI Hideaki } 61221da177e4SLinus Torvalds 61232cc6d2bfSNeil Horman err = register_pernet_subsys(&addrconf_ops); 61242cc6d2bfSNeil Horman if (err < 0) 61252cc6d2bfSNeil Horman goto out_addrlabel; 6126e0da5a48SPavel Emelyanov 6127c15b1ccaSHannes Frederic Sowa addrconf_wq = create_workqueue("ipv6_addrconf"); 6128c15b1ccaSHannes Frederic Sowa if (!addrconf_wq) { 6129c15b1ccaSHannes Frederic Sowa err = -ENOMEM; 6130c15b1ccaSHannes Frederic Sowa goto out_nowq; 6131c15b1ccaSHannes Frederic Sowa } 6132c15b1ccaSHannes Frederic Sowa 61331da177e4SLinus Torvalds /* The addrconf netdev notifier requires that loopback_dev 61341da177e4SLinus Torvalds * has it's ipv6 private information allocated and setup 61351da177e4SLinus Torvalds * before it can bring up and give link-local addresses 61361da177e4SLinus Torvalds * to other devices which are up. 61371da177e4SLinus Torvalds * 61381da177e4SLinus Torvalds * Unfortunately, loopback_dev is not necessarily the first 61391da177e4SLinus Torvalds * entry in the global dev_base list of net devices. In fact, 61401da177e4SLinus Torvalds * it is likely to be the very last entry on that list. 61411da177e4SLinus Torvalds * So this causes the notifier registry below to try and 61421da177e4SLinus Torvalds * give link-local addresses to all devices besides loopback_dev 61431da177e4SLinus Torvalds * first, then loopback_dev, which cases all the non-loopback_dev 61441da177e4SLinus Torvalds * devices to fail to get a link-local address. 61451da177e4SLinus Torvalds * 61461da177e4SLinus Torvalds * So, as a temporary fix, allocate the ipv6 structure for 61471da177e4SLinus Torvalds * loopback_dev first by hand. 61481da177e4SLinus Torvalds * Longer term, all of the dependencies ipv6 has upon the loopback 61491da177e4SLinus Torvalds * device and it being up should be removed. 61501da177e4SLinus Torvalds */ 61511da177e4SLinus Torvalds rtnl_lock(); 6152a317a2f1SWANG Cong idev = ipv6_add_dev(init_net.loopback_dev); 61531da177e4SLinus Torvalds rtnl_unlock(); 6154a317a2f1SWANG Cong if (IS_ERR(idev)) { 6155a317a2f1SWANG Cong err = PTR_ERR(idev); 6156e0da5a48SPavel Emelyanov goto errlo; 6157a317a2f1SWANG Cong } 61581da177e4SLinus Torvalds 6159c2e21293Sstephen hemminger for (i = 0; i < IN6_ADDR_HSIZE; i++) 6160c2e21293Sstephen hemminger INIT_HLIST_HEAD(&inet6_addr_lst[i]); 6161c2e21293Sstephen hemminger 61621da177e4SLinus Torvalds register_netdevice_notifier(&ipv6_dev_notf); 61631da177e4SLinus Torvalds 6164c15b1ccaSHannes Frederic Sowa addrconf_verify(); 6165c127ea2cSThomas Graf 61663678a9d8Sstephen hemminger rtnl_af_register(&inet6_ops); 6167b382b191SThomas Graf 6168c7ac8679SGreg Rose err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo, 6169c7ac8679SGreg Rose NULL); 6170c127ea2cSThomas Graf if (err < 0) 6171c127ea2cSThomas Graf goto errout; 6172c127ea2cSThomas Graf 6173c127ea2cSThomas Graf /* Only the first call to __rtnl_register can fail */ 6174c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL); 6175c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL); 6176c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr, 6177c7ac8679SGreg Rose inet6_dump_ifaddr, NULL); 6178c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL, 6179c7ac8679SGreg Rose inet6_dump_ifmcaddr, NULL); 6180c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL, 6181c7ac8679SGreg Rose inet6_dump_ifacaddr, NULL); 618276f8f6cbSNicolas Dichtel __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf, 61837a674200SNicolas Dichtel inet6_netconf_dump_devconf, NULL); 6184c127ea2cSThomas Graf 61852a8cc6c8SYOSHIFUJI Hideaki ipv6_addr_label_rtnl_register(); 61862a8cc6c8SYOSHIFUJI Hideaki 61871da177e4SLinus Torvalds return 0; 6188c127ea2cSThomas Graf errout: 6189b382b191SThomas Graf rtnl_af_unregister(&inet6_ops); 6190c127ea2cSThomas Graf unregister_netdevice_notifier(&ipv6_dev_notf); 6191e0da5a48SPavel Emelyanov errlo: 6192c15b1ccaSHannes Frederic Sowa destroy_workqueue(addrconf_wq); 6193c15b1ccaSHannes Frederic Sowa out_nowq: 6194e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 61952cc6d2bfSNeil Horman out_addrlabel: 61962cc6d2bfSNeil Horman ipv6_addr_label_cleanup(); 61972cc6d2bfSNeil Horman out: 6198c127ea2cSThomas Graf return err; 61991da177e4SLinus Torvalds } 62001da177e4SLinus Torvalds 620109f7709fSDaniel Lezcano void addrconf_cleanup(void) 62021da177e4SLinus Torvalds { 6203176c39afSDaniel Lezcano struct net_device *dev; 62041da177e4SLinus Torvalds int i; 62051da177e4SLinus Torvalds 62061da177e4SLinus Torvalds unregister_netdevice_notifier(&ipv6_dev_notf); 6207e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 62082cc6d2bfSNeil Horman ipv6_addr_label_cleanup(); 62091da177e4SLinus Torvalds 62101da177e4SLinus Torvalds rtnl_lock(); 62111da177e4SLinus Torvalds 6212b382b191SThomas Graf __rtnl_af_unregister(&inet6_ops); 6213b382b191SThomas Graf 6214176c39afSDaniel Lezcano /* clean dev list */ 6215176c39afSDaniel Lezcano for_each_netdev(&init_net, dev) { 6216176c39afSDaniel Lezcano if (__in6_dev_get(dev) == NULL) 6217176c39afSDaniel Lezcano continue; 6218176c39afSDaniel Lezcano addrconf_ifdown(dev, 1); 6219176c39afSDaniel Lezcano } 6220176c39afSDaniel Lezcano addrconf_ifdown(init_net.loopback_dev, 2); 6221176c39afSDaniel Lezcano 62221da177e4SLinus Torvalds /* 62231da177e4SLinus Torvalds * Check hash table. 62241da177e4SLinus Torvalds */ 62255c578aedSstephen hemminger spin_lock_bh(&addrconf_hash_lock); 6226c2e21293Sstephen hemminger for (i = 0; i < IN6_ADDR_HSIZE; i++) 6227c2e21293Sstephen hemminger WARN_ON(!hlist_empty(&inet6_addr_lst[i])); 62285c578aedSstephen hemminger spin_unlock_bh(&addrconf_hash_lock); 6229c15b1ccaSHannes Frederic Sowa cancel_delayed_work(&addr_chk_work); 62301da177e4SLinus Torvalds rtnl_unlock(); 6231c15b1ccaSHannes Frederic Sowa 6232c15b1ccaSHannes Frederic Sowa destroy_workqueue(addrconf_wq); 62331da177e4SLinus Torvalds } 6234