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> 46174cd4b1SIngo Molnar #include <linux/sched/signal.h> 471da177e4SLinus Torvalds #include <linux/socket.h> 481da177e4SLinus Torvalds #include <linux/sockios.h> 491da177e4SLinus Torvalds #include <linux/net.h> 503d1bec99SHannes Frederic Sowa #include <linux/inet.h> 511da177e4SLinus Torvalds #include <linux/in6.h> 521da177e4SLinus Torvalds #include <linux/netdevice.h> 531823730fSThomas Graf #include <linux/if_addr.h> 541da177e4SLinus Torvalds #include <linux/if_arp.h> 551da177e4SLinus Torvalds #include <linux/if_arcnet.h> 561da177e4SLinus Torvalds #include <linux/if_infiniband.h> 571da177e4SLinus Torvalds #include <linux/route.h> 581da177e4SLinus Torvalds #include <linux/inetdevice.h> 591da177e4SLinus Torvalds #include <linux/init.h> 605a0e3ad6STejun Heo #include <linux/slab.h> 611da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 621da177e4SLinus Torvalds #include <linux/sysctl.h> 631da177e4SLinus Torvalds #endif 644fc268d2SRandy Dunlap #include <linux/capability.h> 651da177e4SLinus Torvalds #include <linux/delay.h> 661da177e4SLinus Torvalds #include <linux/notifier.h> 67543537bdSPaulo Marques #include <linux/string.h> 68ddbe5032SEric Dumazet #include <linux/hash.h> 691da177e4SLinus Torvalds 70457c4cbcSEric W. Biederman #include <net/net_namespace.h> 711da177e4SLinus Torvalds #include <net/sock.h> 721da177e4SLinus Torvalds #include <net/snmp.h> 731da177e4SLinus Torvalds 745241c2d7SAlexander Aring #include <net/6lowpan.h> 75cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 #include <net/firewire.h> 761da177e4SLinus Torvalds #include <net/ipv6.h> 771da177e4SLinus Torvalds #include <net/protocol.h> 781da177e4SLinus Torvalds #include <net/ndisc.h> 791da177e4SLinus Torvalds #include <net/ip6_route.h> 801da177e4SLinus Torvalds #include <net/addrconf.h> 811da177e4SLinus Torvalds #include <net/tcp.h> 821da177e4SLinus Torvalds #include <net/ip.h> 835d620266SThomas Graf #include <net/netlink.h> 84f24e3d65SMitsuru Chinen #include <net/pkt_sched.h> 85ca254490SDavid Ahern #include <net/l3mdev.h> 861da177e4SLinus Torvalds #include <linux/if_tunnel.h> 871da177e4SLinus Torvalds #include <linux/rtnetlink.h> 88f3a1bfb1SNicolas Dichtel #include <linux/netconf.h> 891da177e4SLinus Torvalds #include <linux/random.h> 90e21e8467SStephen Hemminger #include <linux/uaccess.h> 917f7d9a6bSHerbert Xu #include <asm/unaligned.h> 921da177e4SLinus Torvalds 931da177e4SLinus Torvalds #include <linux/proc_fs.h> 941da177e4SLinus Torvalds #include <linux/seq_file.h> 95bc3b2d7fSPaul Gortmaker #include <linux/export.h> 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds #define INFINITY_LIFE_TIME 0xFFFFFFFF 9818a31e1eSThomas Graf 993d1bec99SHannes Frederic Sowa #define IPV6_MAX_STRLEN \ 1003d1bec99SHannes Frederic Sowa sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255") 1013d1bec99SHannes Frederic Sowa 10218a31e1eSThomas Graf static inline u32 cstamp_delta(unsigned long cstamp) 10318a31e1eSThomas Graf { 10418a31e1eSThomas Graf return (cstamp - INITIAL_JIFFIES) * 100UL / HZ; 10518a31e1eSThomas Graf } 1061da177e4SLinus Torvalds 107bd11f074SMaciej Żenczykowski static inline s32 rfc3315_s14_backoff_init(s32 irt) 108bd11f074SMaciej Żenczykowski { 109bd11f074SMaciej Żenczykowski /* multiply 'initial retransmission time' by 0.9 .. 1.1 */ 110bd11f074SMaciej Żenczykowski u64 tmp = (900000 + prandom_u32() % 200001) * (u64)irt; 111bd11f074SMaciej Żenczykowski do_div(tmp, 1000000); 112bd11f074SMaciej Żenczykowski return (s32)tmp; 113bd11f074SMaciej Żenczykowski } 114bd11f074SMaciej Żenczykowski 115bd11f074SMaciej Żenczykowski static inline s32 rfc3315_s14_backoff_update(s32 rt, s32 mrt) 116bd11f074SMaciej Żenczykowski { 117bd11f074SMaciej Żenczykowski /* multiply 'retransmission timeout' by 1.9 .. 2.1 */ 118bd11f074SMaciej Żenczykowski u64 tmp = (1900000 + prandom_u32() % 200001) * (u64)rt; 119bd11f074SMaciej Żenczykowski do_div(tmp, 1000000); 120bd11f074SMaciej Żenczykowski if ((s32)tmp > mrt) { 121bd11f074SMaciej Żenczykowski /* multiply 'maximum retransmission time' by 0.9 .. 1.1 */ 122bd11f074SMaciej Żenczykowski tmp = (900000 + prandom_u32() % 200001) * (u64)mrt; 123bd11f074SMaciej Żenczykowski do_div(tmp, 1000000); 124bd11f074SMaciej Żenczykowski } 125bd11f074SMaciej Żenczykowski return (s32)tmp; 126bd11f074SMaciej Żenczykowski } 127bd11f074SMaciej Żenczykowski 1281da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 129a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev); 130408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev); 131408c4768SPavel Emelyanov #else 132a317a2f1SWANG Cong static inline int addrconf_sysctl_register(struct inet6_dev *idev) 133408c4768SPavel Emelyanov { 134a317a2f1SWANG Cong return 0; 135408c4768SPavel Emelyanov } 136408c4768SPavel Emelyanov 137408c4768SPavel Emelyanov static inline void addrconf_sysctl_unregister(struct inet6_dev *idev) 138408c4768SPavel Emelyanov { 139408c4768SPavel Emelyanov } 1401da177e4SLinus Torvalds #endif 1411da177e4SLinus Torvalds 1429d6280daSJiri Bohac static void ipv6_regen_rndid(struct inet6_dev *idev); 1439d6280daSJiri Bohac static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr); 1441da177e4SLinus Torvalds 1451b34be74SYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev); 146d9bf82c2SEric Dumazet static int ipv6_count_addresses(const struct inet6_dev *idev); 147622c81d5SHannes Frederic Sowa static int ipv6_generate_stable_address(struct in6_addr *addr, 148622c81d5SHannes Frederic Sowa u8 dad_count, 149622c81d5SHannes Frederic Sowa const struct inet6_dev *idev); 1501da177e4SLinus Torvalds 15127c565aeSEric Dumazet #define IN6_ADDR_HSIZE_SHIFT 8 15227c565aeSEric Dumazet #define IN6_ADDR_HSIZE (1 << IN6_ADDR_HSIZE_SHIFT) 1531da177e4SLinus Torvalds /* 1541da177e4SLinus Torvalds * Configured unicast address hash table 1551da177e4SLinus Torvalds */ 156c2e21293Sstephen hemminger static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE]; 1575c578aedSstephen hemminger static DEFINE_SPINLOCK(addrconf_hash_lock); 1581da177e4SLinus Torvalds 159c15b1ccaSHannes Frederic Sowa static void addrconf_verify(void); 160c15b1ccaSHannes Frederic Sowa static void addrconf_verify_rtnl(void); 161c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *); 1621da177e4SLinus Torvalds 163c15b1ccaSHannes Frederic Sowa static struct workqueue_struct *addrconf_wq; 164c15b1ccaSHannes Frederic Sowa static DECLARE_DELAYED_WORK(addr_chk_work, addrconf_verify_work); 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds static void addrconf_join_anycast(struct inet6_ifaddr *ifp); 1671da177e4SLinus Torvalds static void addrconf_leave_anycast(struct inet6_ifaddr *ifp); 1681da177e4SLinus Torvalds 16993d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, 17075c78500SMoni Shoua unsigned long event); 1711da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how); 1721da177e4SLinus Torvalds 1738d1c802bSDavid Ahern static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx, 17421caa662SRomain Kuntz int plen, 17521caa662SRomain Kuntz const struct net_device *dev, 17621caa662SRomain Kuntz u32 flags, u32 noflags); 17721caa662SRomain Kuntz 178cf22f9a2SDavid S. Miller static void addrconf_dad_start(struct inet6_ifaddr *ifp); 179c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w); 180c76fe2d9SDavid Ahern static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id, 181c76fe2d9SDavid Ahern bool send_na); 182c5e33bddSYOSHIFUJI Hideaki static void addrconf_dad_run(struct inet6_dev *idev); 183e99e88a9SKees Cook static void addrconf_rs_timer(struct timer_list *t); 1841da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); 1851da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev, 1881da177e4SLinus Torvalds struct prefix_info *pinfo); 1891da177e4SLinus Torvalds 190888c848eSAdrian Bunk static struct ipv6_devconf ipv6_devconf __read_mostly = { 1911da177e4SLinus Torvalds .forwarding = 0, 1921da177e4SLinus Torvalds .hop_limit = IPV6_DEFAULT_HOPLIMIT, 1931da177e4SLinus Torvalds .mtu6 = IPV6_MIN_MTU, 1941da177e4SLinus Torvalds .accept_ra = 1, 1951da177e4SLinus Torvalds .accept_redirects = 1, 1961da177e4SLinus Torvalds .autoconf = 1, 1971da177e4SLinus Torvalds .force_mld_version = 0, 198fc4eba58SHannes Frederic Sowa .mldv1_unsolicited_report_interval = 10 * HZ, 199fc4eba58SHannes Frederic Sowa .mldv2_unsolicited_report_interval = HZ, 2001da177e4SLinus Torvalds .dad_transmits = 1, 2011da177e4SLinus Torvalds .rtr_solicits = MAX_RTR_SOLICITATIONS, 2021da177e4SLinus Torvalds .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, 203bd11f074SMaciej Żenczykowski .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL, 2041da177e4SLinus Torvalds .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, 2051da177e4SLinus Torvalds .use_tempaddr = 0, 2061da177e4SLinus Torvalds .temp_valid_lft = TEMP_VALID_LIFETIME, 2071da177e4SLinus Torvalds .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, 2081da177e4SLinus Torvalds .regen_max_retry = REGEN_MAX_RETRY, 2091da177e4SLinus Torvalds .max_desync_factor = MAX_DESYNC_FACTOR, 2101da177e4SLinus Torvalds .max_addresses = IPV6_MAX_ADDRESSES, 21165f5c7c1SYOSHIFUJI Hideaki .accept_ra_defrtr = 1, 212d9333196SBen Greear .accept_ra_from_local = 0, 2138013d1d7SHangbin Liu .accept_ra_min_hop_limit= 1, 214c4fd30ebSYOSHIFUJI Hideaki .accept_ra_pinfo = 1, 215930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 216930d6ff2SYOSHIFUJI Hideaki .accept_ra_rtr_pref = 1, 21752e16356SYOSHIFUJI Hideaki .rtr_probe_interval = 60 * HZ, 21809c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 219bbea124bSJoel Scherpelz .accept_ra_rt_info_min_plen = 0, 22009c884d4SYOSHIFUJI Hideaki .accept_ra_rt_info_max_plen = 0, 22109c884d4SYOSHIFUJI Hideaki #endif 222930d6ff2SYOSHIFUJI Hideaki #endif 223fbea49e1SYOSHIFUJI Hideaki .proxy_ndp = 0, 2240bcbc926SYOSHIFUJI Hideaki .accept_source_route = 0, /* we do not accept RH0 by default. */ 225778d80beSYOSHIFUJI Hideaki .disable_ipv6 = 0, 22609400953SNicolas Dichtel .accept_dad = 0, 227b800c3b9SHannes Frederic Sowa .suppress_frag_ndisc = 1, 228c2943f14SHarout Hedeshian .accept_ra_mtu = 1, 2293d1bec99SHannes Frederic Sowa .stable_secret = { 2303d1bec99SHannes Frederic Sowa .initialized = false, 2313985e8a3SErik Kline }, 2323985e8a3SErik Kline .use_oif_addrs_only = 0, 23335103d11SAndy Gospodarek .ignore_routes_with_linkdown = 0, 234f1705ec1SDavid Ahern .keep_addr_on_down = 0, 2351ababebaSDavid Lebrun .seg6_enabled = 0, 236bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC 237bf355b8dSDavid Lebrun .seg6_require_hmac = 0, 238bf355b8dSDavid Lebrun #endif 239adc176c5SErik Nordmark .enhanced_dad = 1, 240d35a00b8SFelix Jia .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64, 241df789fe7SDavid Forster .disable_policy = 0, 2421da177e4SLinus Torvalds }; 2431da177e4SLinus Torvalds 244ab32ea5dSBrian Haley static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = { 2451da177e4SLinus Torvalds .forwarding = 0, 2461da177e4SLinus Torvalds .hop_limit = IPV6_DEFAULT_HOPLIMIT, 2471da177e4SLinus Torvalds .mtu6 = IPV6_MIN_MTU, 2481da177e4SLinus Torvalds .accept_ra = 1, 2491da177e4SLinus Torvalds .accept_redirects = 1, 2501da177e4SLinus Torvalds .autoconf = 1, 251fc4eba58SHannes Frederic Sowa .force_mld_version = 0, 252fc4eba58SHannes Frederic Sowa .mldv1_unsolicited_report_interval = 10 * HZ, 253fc4eba58SHannes Frederic Sowa .mldv2_unsolicited_report_interval = HZ, 2541da177e4SLinus Torvalds .dad_transmits = 1, 2551da177e4SLinus Torvalds .rtr_solicits = MAX_RTR_SOLICITATIONS, 2561da177e4SLinus Torvalds .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, 257bd11f074SMaciej Żenczykowski .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL, 2581da177e4SLinus Torvalds .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, 2591da177e4SLinus Torvalds .use_tempaddr = 0, 2601da177e4SLinus Torvalds .temp_valid_lft = TEMP_VALID_LIFETIME, 2611da177e4SLinus Torvalds .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, 2621da177e4SLinus Torvalds .regen_max_retry = REGEN_MAX_RETRY, 2631da177e4SLinus Torvalds .max_desync_factor = MAX_DESYNC_FACTOR, 2641da177e4SLinus Torvalds .max_addresses = IPV6_MAX_ADDRESSES, 26565f5c7c1SYOSHIFUJI Hideaki .accept_ra_defrtr = 1, 266d9333196SBen Greear .accept_ra_from_local = 0, 2678013d1d7SHangbin Liu .accept_ra_min_hop_limit= 1, 268c4fd30ebSYOSHIFUJI Hideaki .accept_ra_pinfo = 1, 269930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 270930d6ff2SYOSHIFUJI Hideaki .accept_ra_rtr_pref = 1, 27152e16356SYOSHIFUJI Hideaki .rtr_probe_interval = 60 * HZ, 27209c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 273bbea124bSJoel Scherpelz .accept_ra_rt_info_min_plen = 0, 27409c884d4SYOSHIFUJI Hideaki .accept_ra_rt_info_max_plen = 0, 27509c884d4SYOSHIFUJI Hideaki #endif 276930d6ff2SYOSHIFUJI Hideaki #endif 277fbea49e1SYOSHIFUJI Hideaki .proxy_ndp = 0, 2780bcbc926SYOSHIFUJI Hideaki .accept_source_route = 0, /* we do not accept RH0 by default. */ 279778d80beSYOSHIFUJI Hideaki .disable_ipv6 = 0, 2801b34be74SYOSHIFUJI Hideaki .accept_dad = 1, 281b800c3b9SHannes Frederic Sowa .suppress_frag_ndisc = 1, 282c2943f14SHarout Hedeshian .accept_ra_mtu = 1, 2833d1bec99SHannes Frederic Sowa .stable_secret = { 2843d1bec99SHannes Frederic Sowa .initialized = false, 2853d1bec99SHannes Frederic Sowa }, 2863985e8a3SErik Kline .use_oif_addrs_only = 0, 28735103d11SAndy Gospodarek .ignore_routes_with_linkdown = 0, 288f1705ec1SDavid Ahern .keep_addr_on_down = 0, 2891ababebaSDavid Lebrun .seg6_enabled = 0, 290bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC 291bf355b8dSDavid Lebrun .seg6_require_hmac = 0, 292bf355b8dSDavid Lebrun #endif 293adc176c5SErik Nordmark .enhanced_dad = 1, 294d35a00b8SFelix Jia .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64, 295df789fe7SDavid Forster .disable_policy = 0, 2961da177e4SLinus Torvalds }; 2971da177e4SLinus Torvalds 2981f372c7bSMike Manning /* Check if link is ready: is it up and is a valid qdisc available */ 2991f372c7bSMike Manning static inline bool addrconf_link_ready(const struct net_device *dev) 300f24e3d65SMitsuru Chinen { 3011f372c7bSMike Manning return netif_oper_up(dev) && !qdisc_tx_is_noop(dev); 302f24e3d65SMitsuru Chinen } 303f24e3d65SMitsuru Chinen 304b7b1bfceSHannes Frederic Sowa static void addrconf_del_rs_timer(struct inet6_dev *idev) 3051da177e4SLinus Torvalds { 306b7b1bfceSHannes Frederic Sowa if (del_timer(&idev->rs_timer)) 307b7b1bfceSHannes Frederic Sowa __in6_dev_put(idev); 308b7b1bfceSHannes Frederic Sowa } 309b7b1bfceSHannes Frederic Sowa 310c15b1ccaSHannes Frederic Sowa static void addrconf_del_dad_work(struct inet6_ifaddr *ifp) 311b7b1bfceSHannes Frederic Sowa { 312c15b1ccaSHannes Frederic Sowa if (cancel_delayed_work(&ifp->dad_work)) 3131da177e4SLinus Torvalds __in6_ifa_put(ifp); 3141da177e4SLinus Torvalds } 3151da177e4SLinus Torvalds 316b7b1bfceSHannes Frederic Sowa static void addrconf_mod_rs_timer(struct inet6_dev *idev, 3171da177e4SLinus Torvalds unsigned long when) 3181da177e4SLinus Torvalds { 319b7b1bfceSHannes Frederic Sowa if (!timer_pending(&idev->rs_timer)) 320b7b1bfceSHannes Frederic Sowa in6_dev_hold(idev); 321b7b1bfceSHannes Frederic Sowa mod_timer(&idev->rs_timer, jiffies + when); 3221da177e4SLinus Torvalds } 323b7b1bfceSHannes Frederic Sowa 324c15b1ccaSHannes Frederic Sowa static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp, 325c15b1ccaSHannes Frederic Sowa unsigned long delay) 326b7b1bfceSHannes Frederic Sowa { 327b7b1bfceSHannes Frederic Sowa in6_ifa_hold(ifp); 328f8a894b2SXin Long if (mod_delayed_work(addrconf_wq, &ifp->dad_work, delay)) 329f8a894b2SXin Long in6_ifa_put(ifp); 3301da177e4SLinus Torvalds } 3311da177e4SLinus Torvalds 3327f7d9a6bSHerbert Xu static int snmp6_alloc_dev(struct inet6_dev *idev) 3337f7d9a6bSHerbert Xu { 334827da44cSJohn Stultz int i; 335827da44cSJohn Stultz 336698365faSWANG Cong idev->stats.ipv6 = alloc_percpu(struct ipstats_mib); 337698365faSWANG Cong if (!idev->stats.ipv6) 3387f7d9a6bSHerbert Xu goto err_ip; 339827da44cSJohn Stultz 340827da44cSJohn Stultz for_each_possible_cpu(i) { 341827da44cSJohn Stultz struct ipstats_mib *addrconf_stats; 342698365faSWANG Cong addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i); 343827da44cSJohn Stultz u64_stats_init(&addrconf_stats->syncp); 344827da44cSJohn Stultz } 345827da44cSJohn Stultz 346827da44cSJohn Stultz 347be281e55SEric Dumazet idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device), 348be281e55SEric Dumazet GFP_KERNEL); 349be281e55SEric Dumazet if (!idev->stats.icmpv6dev) 3507f7d9a6bSHerbert Xu goto err_icmp; 351be281e55SEric Dumazet idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device), 352be281e55SEric Dumazet GFP_KERNEL); 353be281e55SEric Dumazet if (!idev->stats.icmpv6msgdev) 35414878f75SDavid L Stevens goto err_icmpmsg; 3557f7d9a6bSHerbert Xu 3567f7d9a6bSHerbert Xu return 0; 3577f7d9a6bSHerbert Xu 35814878f75SDavid L Stevens err_icmpmsg: 359be281e55SEric Dumazet kfree(idev->stats.icmpv6dev); 3607f7d9a6bSHerbert Xu err_icmp: 361698365faSWANG Cong free_percpu(idev->stats.ipv6); 3627f7d9a6bSHerbert Xu err_ip: 363aaf70ec7SPavel Emelyanov return -ENOMEM; 3647f7d9a6bSHerbert Xu } 3657f7d9a6bSHerbert Xu 3661da177e4SLinus Torvalds static struct inet6_dev *ipv6_add_dev(struct net_device *dev) 3671da177e4SLinus Torvalds { 3681da177e4SLinus Torvalds struct inet6_dev *ndev; 369a317a2f1SWANG Cong int err = -ENOMEM; 3701da177e4SLinus Torvalds 3711da177e4SLinus Torvalds ASSERT_RTNL(); 3721da177e4SLinus Torvalds 3731da177e4SLinus Torvalds if (dev->mtu < IPV6_MIN_MTU) 374a317a2f1SWANG Cong return ERR_PTR(-EINVAL); 3751da177e4SLinus Torvalds 376322f74a4SIngo Oeser ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL); 37763159f29SIan Morris if (!ndev) 378a317a2f1SWANG Cong return ERR_PTR(err); 3791da177e4SLinus Torvalds 3801da177e4SLinus Torvalds rwlock_init(&ndev->lock); 3811da177e4SLinus Torvalds ndev->dev = dev; 382502a2ffdSstephen hemminger INIT_LIST_HEAD(&ndev->addr_list); 383e99e88a9SKees Cook timer_setup(&ndev->rs_timer, addrconf_rs_timer, 0); 384c346dca1SYOSHIFUJI Hideaki memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf)); 3859b29c696SHannes Frederic Sowa 3869b29c696SHannes Frederic Sowa if (ndev->cnf.stable_secret.initialized) 387d35a00b8SFelix Jia ndev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY; 3889b29c696SHannes Frederic Sowa 3891da177e4SLinus Torvalds ndev->cnf.mtu6 = dev->mtu; 3901da177e4SLinus Torvalds ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl); 39163159f29SIan Morris if (!ndev->nd_parms) { 3921da177e4SLinus Torvalds kfree(ndev); 393a317a2f1SWANG Cong return ERR_PTR(err); 3941da177e4SLinus Torvalds } 3950187bdfbSBen Hutchings if (ndev->cnf.forwarding) 3960187bdfbSBen Hutchings dev_disable_lro(dev); 3971da177e4SLinus Torvalds /* We refer to the device */ 3981da177e4SLinus Torvalds dev_hold(dev); 3991da177e4SLinus Torvalds 4001da177e4SLinus Torvalds if (snmp6_alloc_dev(ndev) < 0) { 401e32ac250SJoe Perches netdev_dbg(dev, "%s: cannot allocate memory for statistics\n", 402e32ac250SJoe Perches __func__); 4031da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, ndev->nd_parms); 4048603e33dSRoy Li dev_put(dev); 4058603e33dSRoy Li kfree(ndev); 406a317a2f1SWANG Cong return ERR_PTR(err); 4071da177e4SLinus Torvalds } 4081da177e4SLinus Torvalds 4091da177e4SLinus Torvalds if (snmp6_register_dev(ndev) < 0) { 410e32ac250SJoe Perches netdev_dbg(dev, "%s: cannot create /proc/net/dev_snmp6/%s\n", 411ba3542e1Sdingtianhong __func__, dev->name); 412a317a2f1SWANG Cong goto err_release; 4131da177e4SLinus Torvalds } 4141da177e4SLinus Torvalds 4159d6280daSJiri Bohac /* One reference from device. */ 4161be92460SReshetova, Elena refcount_set(&ndev->refcnt, 1); 4171da177e4SLinus Torvalds 4181b34be74SYOSHIFUJI Hideaki if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) 4191b34be74SYOSHIFUJI Hideaki ndev->cnf.accept_dad = -1; 4201b34be74SYOSHIFUJI Hideaki 42107a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 422c7dc89c0SFred L. Templin if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) { 423f3213831SJoe Perches pr_info("%s: Disabled Multicast RS\n", dev->name); 424c7dc89c0SFred L. Templin ndev->cnf.rtr_solicits = 0; 425c7dc89c0SFred L. Templin } 426b077d7abSYOSHIFUJI Hideaki #endif 427b077d7abSYOSHIFUJI Hideaki 428372e6c8fSstephen hemminger INIT_LIST_HEAD(&ndev->tempaddr_list); 42976506a98SJiri Bohac ndev->desync_factor = U32_MAX; 4301da177e4SLinus Torvalds if ((dev->flags&IFF_LOOPBACK) || 4311da177e4SLinus Torvalds dev->type == ARPHRD_TUNNEL || 4329625ed72SYOSHIFUJI Hideaki dev->type == ARPHRD_TUNNEL6 || 4331da177e4SLinus Torvalds dev->type == ARPHRD_SIT || 4341da177e4SLinus Torvalds dev->type == ARPHRD_NONE) { 4350be669bbSJoerg Roedel ndev->cnf.use_tempaddr = -1; 4369d6280daSJiri Bohac } else 4379d6280daSJiri Bohac ipv6_regen_rndid(ndev); 4385d9efa7eSDavid S. Miller 439914faa14SDaniel Borkmann ndev->token = in6addr_any; 4401da177e4SLinus Torvalds 4411f372c7bSMike Manning if (netif_running(dev) && addrconf_link_ready(dev)) 44253aadcc9SHerbert Xu ndev->if_flags |= IF_READY; 44353aadcc9SHerbert Xu 4441da177e4SLinus Torvalds ipv6_mc_init_dev(ndev); 4451da177e4SLinus Torvalds ndev->tstamp = jiffies; 446a317a2f1SWANG Cong err = addrconf_sysctl_register(ndev); 447a317a2f1SWANG Cong if (err) { 448a317a2f1SWANG Cong ipv6_mc_destroy_dev(ndev); 4492a189f9eSSabrina Dubroca snmp6_unregister_dev(ndev); 450a317a2f1SWANG Cong goto err_release; 451a317a2f1SWANG Cong } 45230c4cf57SDavid L Stevens /* protected by rtnl_lock */ 453cf778b00SEric Dumazet rcu_assign_pointer(dev->ip6_ptr, ndev); 454d88ae4ccSYOSHIFUJI Hideaki 4552c5e8933SHannes Frederic Sowa /* Join interface-local all-node multicast group */ 4562c5e8933SHannes Frederic Sowa ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes); 4572c5e8933SHannes Frederic Sowa 458d88ae4ccSYOSHIFUJI Hideaki /* Join all-node multicast group */ 459f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes); 460d88ae4ccSYOSHIFUJI Hideaki 461d6ddef9eSLi Wei /* Join all-router multicast group if forwarding is set */ 4628b2aaedeSLi Wei if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST)) 463d6ddef9eSLi Wei ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters); 464d6ddef9eSLi Wei 4651da177e4SLinus Torvalds return ndev; 466a317a2f1SWANG Cong 467a317a2f1SWANG Cong err_release: 468a317a2f1SWANG Cong neigh_parms_release(&nd_tbl, ndev->nd_parms); 469a317a2f1SWANG Cong ndev->dead = 1; 470a317a2f1SWANG Cong in6_dev_finish_destroy(ndev); 471a317a2f1SWANG Cong return ERR_PTR(err); 4721da177e4SLinus Torvalds } 4731da177e4SLinus Torvalds 474cee89473SYOSHIFUJI Hideaki static struct inet6_dev *ipv6_find_idev(struct net_device *dev) 4751da177e4SLinus Torvalds { 4761da177e4SLinus Torvalds struct inet6_dev *idev; 4771da177e4SLinus Torvalds 4781da177e4SLinus Torvalds ASSERT_RTNL(); 4791da177e4SLinus Torvalds 480e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 481e21e8467SStephen Hemminger if (!idev) { 482e21e8467SStephen Hemminger idev = ipv6_add_dev(dev); 483a317a2f1SWANG Cong if (IS_ERR(idev)) 4841da177e4SLinus Torvalds return NULL; 4851da177e4SLinus Torvalds } 486c5e33bddSYOSHIFUJI Hideaki 4871da177e4SLinus Torvalds if (dev->flags&IFF_UP) 4881da177e4SLinus Torvalds ipv6_mc_up(idev); 4891da177e4SLinus Torvalds return idev; 4901da177e4SLinus Torvalds } 4911da177e4SLinus Torvalds 492f3a1bfb1SNicolas Dichtel static int inet6_netconf_msgsize_devconf(int type) 493f3a1bfb1SNicolas Dichtel { 494f3a1bfb1SNicolas Dichtel int size = NLMSG_ALIGN(sizeof(struct netconfmsg)) 495f3a1bfb1SNicolas Dichtel + nla_total_size(4); /* NETCONFA_IFINDEX */ 496136ba622SZhang Shengju bool all = false; 497f3a1bfb1SNicolas Dichtel 498136ba622SZhang Shengju if (type == NETCONFA_ALL) 499136ba622SZhang Shengju all = true; 500136ba622SZhang Shengju 501136ba622SZhang Shengju if (all || type == NETCONFA_FORWARDING) 502f3a1bfb1SNicolas Dichtel size += nla_total_size(4); 503b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE 504136ba622SZhang Shengju if (all || type == NETCONFA_MC_FORWARDING) 505d67b8c61SNicolas Dichtel size += nla_total_size(4); 506b1afce95SDavid S. Miller #endif 507136ba622SZhang Shengju if (all || type == NETCONFA_PROXY_NEIGH) 508c92d5491Sstephen hemminger size += nla_total_size(4); 509f3a1bfb1SNicolas Dichtel 510136ba622SZhang Shengju if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) 51135103d11SAndy Gospodarek size += nla_total_size(4); 51235103d11SAndy Gospodarek 513f3a1bfb1SNicolas Dichtel return size; 514f3a1bfb1SNicolas Dichtel } 515f3a1bfb1SNicolas Dichtel 516f3a1bfb1SNicolas Dichtel static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex, 517f3a1bfb1SNicolas Dichtel struct ipv6_devconf *devconf, u32 portid, 518f3a1bfb1SNicolas Dichtel u32 seq, int event, unsigned int flags, 519f3a1bfb1SNicolas Dichtel int type) 520f3a1bfb1SNicolas Dichtel { 521f3a1bfb1SNicolas Dichtel struct nlmsghdr *nlh; 522f3a1bfb1SNicolas Dichtel struct netconfmsg *ncm; 523136ba622SZhang Shengju bool all = false; 524f3a1bfb1SNicolas Dichtel 525f3a1bfb1SNicolas Dichtel nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg), 526f3a1bfb1SNicolas Dichtel flags); 52763159f29SIan Morris if (!nlh) 528f3a1bfb1SNicolas Dichtel return -EMSGSIZE; 529f3a1bfb1SNicolas Dichtel 530136ba622SZhang Shengju if (type == NETCONFA_ALL) 531136ba622SZhang Shengju all = true; 532136ba622SZhang Shengju 533f3a1bfb1SNicolas Dichtel ncm = nlmsg_data(nlh); 534f3a1bfb1SNicolas Dichtel ncm->ncm_family = AF_INET6; 535f3a1bfb1SNicolas Dichtel 536f3a1bfb1SNicolas Dichtel if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0) 537f3a1bfb1SNicolas Dichtel goto nla_put_failure; 538f3a1bfb1SNicolas Dichtel 53923452170SDavid Ahern if (!devconf) 54023452170SDavid Ahern goto out; 54123452170SDavid Ahern 542136ba622SZhang Shengju if ((all || type == NETCONFA_FORWARDING) && 543f3a1bfb1SNicolas Dichtel nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0) 544f3a1bfb1SNicolas Dichtel goto nla_put_failure; 545b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE 546136ba622SZhang Shengju if ((all || type == NETCONFA_MC_FORWARDING) && 547d67b8c61SNicolas Dichtel nla_put_s32(skb, NETCONFA_MC_FORWARDING, 548d67b8c61SNicolas Dichtel devconf->mc_forwarding) < 0) 549d67b8c61SNicolas Dichtel goto nla_put_failure; 550b1afce95SDavid S. Miller #endif 551136ba622SZhang Shengju if ((all || type == NETCONFA_PROXY_NEIGH) && 552c92d5491Sstephen hemminger nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0) 553c92d5491Sstephen hemminger goto nla_put_failure; 554c92d5491Sstephen hemminger 555136ba622SZhang Shengju if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) && 55635103d11SAndy Gospodarek nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 55735103d11SAndy Gospodarek devconf->ignore_routes_with_linkdown) < 0) 55835103d11SAndy Gospodarek goto nla_put_failure; 55935103d11SAndy Gospodarek 56023452170SDavid Ahern out: 561053c095aSJohannes Berg nlmsg_end(skb, nlh); 562053c095aSJohannes Berg return 0; 563f3a1bfb1SNicolas Dichtel 564f3a1bfb1SNicolas Dichtel nla_put_failure: 565f3a1bfb1SNicolas Dichtel nlmsg_cancel(skb, nlh); 566f3a1bfb1SNicolas Dichtel return -EMSGSIZE; 567f3a1bfb1SNicolas Dichtel } 568f3a1bfb1SNicolas Dichtel 56985b3daadSDavid Ahern void inet6_netconf_notify_devconf(struct net *net, int event, int type, 57085b3daadSDavid Ahern int ifindex, struct ipv6_devconf *devconf) 571f3a1bfb1SNicolas Dichtel { 572f3a1bfb1SNicolas Dichtel struct sk_buff *skb; 573f3a1bfb1SNicolas Dichtel int err = -ENOBUFS; 574f3a1bfb1SNicolas Dichtel 575927265bcSEric Dumazet skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_KERNEL); 57663159f29SIan Morris if (!skb) 577f3a1bfb1SNicolas Dichtel goto errout; 578f3a1bfb1SNicolas Dichtel 579f3a1bfb1SNicolas Dichtel err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0, 58085b3daadSDavid Ahern event, 0, type); 581f3a1bfb1SNicolas Dichtel if (err < 0) { 582f3a1bfb1SNicolas Dichtel /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */ 583f3a1bfb1SNicolas Dichtel WARN_ON(err == -EMSGSIZE); 584f3a1bfb1SNicolas Dichtel kfree_skb(skb); 585f3a1bfb1SNicolas Dichtel goto errout; 586f3a1bfb1SNicolas Dichtel } 587927265bcSEric Dumazet rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_KERNEL); 588f3a1bfb1SNicolas Dichtel return; 589f3a1bfb1SNicolas Dichtel errout: 590f3a1bfb1SNicolas Dichtel rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err); 591f3a1bfb1SNicolas Dichtel } 592f3a1bfb1SNicolas Dichtel 59376f8f6cbSNicolas Dichtel static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = { 59476f8f6cbSNicolas Dichtel [NETCONFA_IFINDEX] = { .len = sizeof(int) }, 59576f8f6cbSNicolas Dichtel [NETCONFA_FORWARDING] = { .len = sizeof(int) }, 596c92d5491Sstephen hemminger [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) }, 59735103d11SAndy Gospodarek [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) }, 59876f8f6cbSNicolas Dichtel }; 59976f8f6cbSNicolas Dichtel 60076f8f6cbSNicolas Dichtel static int inet6_netconf_get_devconf(struct sk_buff *in_skb, 601c21ef3e3SDavid Ahern struct nlmsghdr *nlh, 602c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 60376f8f6cbSNicolas Dichtel { 60476f8f6cbSNicolas Dichtel struct net *net = sock_net(in_skb->sk); 60576f8f6cbSNicolas Dichtel struct nlattr *tb[NETCONFA_MAX+1]; 6064ea2607fSFlorian Westphal struct inet6_dev *in6_dev = NULL; 6074ea2607fSFlorian Westphal struct net_device *dev = NULL; 60876f8f6cbSNicolas Dichtel struct netconfmsg *ncm; 60976f8f6cbSNicolas Dichtel struct sk_buff *skb; 61076f8f6cbSNicolas Dichtel struct ipv6_devconf *devconf; 61176f8f6cbSNicolas Dichtel int ifindex; 61276f8f6cbSNicolas Dichtel int err; 61376f8f6cbSNicolas Dichtel 61476f8f6cbSNicolas Dichtel err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX, 615c21ef3e3SDavid Ahern devconf_ipv6_policy, extack); 61676f8f6cbSNicolas Dichtel if (err < 0) 6174ea2607fSFlorian Westphal return err; 6184ea2607fSFlorian Westphal 6194ea2607fSFlorian Westphal if (!tb[NETCONFA_IFINDEX]) 6204ea2607fSFlorian Westphal return -EINVAL; 62176f8f6cbSNicolas Dichtel 622a97eb33fSAnton Protopopov err = -EINVAL; 62376f8f6cbSNicolas Dichtel ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]); 62476f8f6cbSNicolas Dichtel switch (ifindex) { 62576f8f6cbSNicolas Dichtel case NETCONFA_IFINDEX_ALL: 62676f8f6cbSNicolas Dichtel devconf = net->ipv6.devconf_all; 62776f8f6cbSNicolas Dichtel break; 62876f8f6cbSNicolas Dichtel case NETCONFA_IFINDEX_DEFAULT: 62976f8f6cbSNicolas Dichtel devconf = net->ipv6.devconf_dflt; 63076f8f6cbSNicolas Dichtel break; 63176f8f6cbSNicolas Dichtel default: 6324ea2607fSFlorian Westphal dev = dev_get_by_index(net, ifindex); 63363159f29SIan Morris if (!dev) 6344ea2607fSFlorian Westphal return -EINVAL; 6354ea2607fSFlorian Westphal in6_dev = in6_dev_get(dev); 63663159f29SIan Morris if (!in6_dev) 63776f8f6cbSNicolas Dichtel goto errout; 63876f8f6cbSNicolas Dichtel devconf = &in6_dev->cnf; 63976f8f6cbSNicolas Dichtel break; 64076f8f6cbSNicolas Dichtel } 64176f8f6cbSNicolas Dichtel 64276f8f6cbSNicolas Dichtel err = -ENOBUFS; 6434ea2607fSFlorian Westphal skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL); 64463159f29SIan Morris if (!skb) 64576f8f6cbSNicolas Dichtel goto errout; 64676f8f6cbSNicolas Dichtel 64776f8f6cbSNicolas Dichtel err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 64876f8f6cbSNicolas Dichtel NETLINK_CB(in_skb).portid, 64976f8f6cbSNicolas Dichtel nlh->nlmsg_seq, RTM_NEWNETCONF, 0, 650136ba622SZhang Shengju NETCONFA_ALL); 65176f8f6cbSNicolas Dichtel if (err < 0) { 65276f8f6cbSNicolas Dichtel /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */ 65376f8f6cbSNicolas Dichtel WARN_ON(err == -EMSGSIZE); 65476f8f6cbSNicolas Dichtel kfree_skb(skb); 65576f8f6cbSNicolas Dichtel goto errout; 65676f8f6cbSNicolas Dichtel } 65776f8f6cbSNicolas Dichtel err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 65876f8f6cbSNicolas Dichtel errout: 6594ea2607fSFlorian Westphal if (in6_dev) 6604ea2607fSFlorian Westphal in6_dev_put(in6_dev); 6614ea2607fSFlorian Westphal if (dev) 6624ea2607fSFlorian Westphal dev_put(dev); 66376f8f6cbSNicolas Dichtel return err; 66476f8f6cbSNicolas Dichtel } 66576f8f6cbSNicolas Dichtel 6667a674200SNicolas Dichtel static int inet6_netconf_dump_devconf(struct sk_buff *skb, 6677a674200SNicolas Dichtel struct netlink_callback *cb) 6687a674200SNicolas Dichtel { 669*addd383fSDavid Ahern const struct nlmsghdr *nlh = cb->nlh; 6707a674200SNicolas Dichtel struct net *net = sock_net(skb->sk); 6717a674200SNicolas Dichtel int h, s_h; 6727a674200SNicolas Dichtel int idx, s_idx; 6737a674200SNicolas Dichtel struct net_device *dev; 6747a674200SNicolas Dichtel struct inet6_dev *idev; 6757a674200SNicolas Dichtel struct hlist_head *head; 6767a674200SNicolas Dichtel 677*addd383fSDavid Ahern if (cb->strict_check) { 678*addd383fSDavid Ahern struct netlink_ext_ack *extack = cb->extack; 679*addd383fSDavid Ahern struct netconfmsg *ncm; 680*addd383fSDavid Ahern 681*addd383fSDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ncm))) { 682*addd383fSDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf dump request"); 683*addd383fSDavid Ahern return -EINVAL; 684*addd383fSDavid Ahern } 685*addd383fSDavid Ahern 686*addd383fSDavid Ahern if (nlmsg_attrlen(nlh, sizeof(*ncm))) { 687*addd383fSDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Invalid data after header in netconf dump request"); 688*addd383fSDavid Ahern return -EINVAL; 689*addd383fSDavid Ahern } 690*addd383fSDavid Ahern } 691*addd383fSDavid Ahern 6927a674200SNicolas Dichtel s_h = cb->args[0]; 6937a674200SNicolas Dichtel s_idx = idx = cb->args[1]; 6947a674200SNicolas Dichtel 6957a674200SNicolas Dichtel for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 6967a674200SNicolas Dichtel idx = 0; 6977a674200SNicolas Dichtel head = &net->dev_index_head[h]; 6987a674200SNicolas Dichtel rcu_read_lock(); 69963998ac2SNicolas Dichtel cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ 70063998ac2SNicolas Dichtel net->dev_base_seq; 7017a674200SNicolas Dichtel hlist_for_each_entry_rcu(dev, head, index_hlist) { 7027a674200SNicolas Dichtel if (idx < s_idx) 7037a674200SNicolas Dichtel goto cont; 7047a674200SNicolas Dichtel idev = __in6_dev_get(dev); 7057a674200SNicolas Dichtel if (!idev) 7067a674200SNicolas Dichtel goto cont; 7077a674200SNicolas Dichtel 7087a674200SNicolas Dichtel if (inet6_netconf_fill_devconf(skb, dev->ifindex, 7097a674200SNicolas Dichtel &idev->cnf, 7107a674200SNicolas Dichtel NETLINK_CB(cb->skb).portid, 711*addd383fSDavid Ahern nlh->nlmsg_seq, 7127a674200SNicolas Dichtel RTM_NEWNETCONF, 7137a674200SNicolas Dichtel NLM_F_MULTI, 714136ba622SZhang Shengju NETCONFA_ALL) < 0) { 7157a674200SNicolas Dichtel rcu_read_unlock(); 7167a674200SNicolas Dichtel goto done; 7177a674200SNicolas Dichtel } 71863998ac2SNicolas Dichtel nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 7197a674200SNicolas Dichtel cont: 7207a674200SNicolas Dichtel idx++; 7217a674200SNicolas Dichtel } 7227a674200SNicolas Dichtel rcu_read_unlock(); 7237a674200SNicolas Dichtel } 7247a674200SNicolas Dichtel if (h == NETDEV_HASHENTRIES) { 7257a674200SNicolas Dichtel if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL, 7267a674200SNicolas Dichtel net->ipv6.devconf_all, 7277a674200SNicolas Dichtel NETLINK_CB(cb->skb).portid, 728*addd383fSDavid Ahern nlh->nlmsg_seq, 7297a674200SNicolas Dichtel RTM_NEWNETCONF, NLM_F_MULTI, 730136ba622SZhang Shengju NETCONFA_ALL) < 0) 7317a674200SNicolas Dichtel goto done; 7327a674200SNicolas Dichtel else 7337a674200SNicolas Dichtel h++; 7347a674200SNicolas Dichtel } 7357a674200SNicolas Dichtel if (h == NETDEV_HASHENTRIES + 1) { 7367a674200SNicolas Dichtel if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT, 7377a674200SNicolas Dichtel net->ipv6.devconf_dflt, 7387a674200SNicolas Dichtel NETLINK_CB(cb->skb).portid, 739*addd383fSDavid Ahern nlh->nlmsg_seq, 7407a674200SNicolas Dichtel RTM_NEWNETCONF, NLM_F_MULTI, 741136ba622SZhang Shengju NETCONFA_ALL) < 0) 7427a674200SNicolas Dichtel goto done; 7437a674200SNicolas Dichtel else 7447a674200SNicolas Dichtel h++; 7457a674200SNicolas Dichtel } 7467a674200SNicolas Dichtel done: 7477a674200SNicolas Dichtel cb->args[0] = h; 7487a674200SNicolas Dichtel cb->args[1] = idx; 7497a674200SNicolas Dichtel 7507a674200SNicolas Dichtel return skb->len; 7517a674200SNicolas Dichtel } 7527a674200SNicolas Dichtel 7531da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 7541da177e4SLinus Torvalds static void dev_forward_change(struct inet6_dev *idev) 7551da177e4SLinus Torvalds { 7561da177e4SLinus Torvalds struct net_device *dev; 7571da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 7581da177e4SLinus Torvalds 7591da177e4SLinus Torvalds if (!idev) 7601da177e4SLinus Torvalds return; 7611da177e4SLinus Torvalds dev = idev->dev; 7620187bdfbSBen Hutchings if (idev->cnf.forwarding) 7630187bdfbSBen Hutchings dev_disable_lro(dev); 7641a940835SAmerigo Wang if (dev->flags & IFF_MULTICAST) { 7652c5e8933SHannes Frederic Sowa if (idev->cnf.forwarding) { 766f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters); 7672c5e8933SHannes Frederic Sowa ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters); 7682c5e8933SHannes Frederic Sowa ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters); 7692c5e8933SHannes Frederic Sowa } else { 770f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters); 7712c5e8933SHannes Frederic Sowa ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters); 7722c5e8933SHannes Frederic Sowa ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters); 7732c5e8933SHannes Frederic Sowa } 7741da177e4SLinus Torvalds } 775502a2ffdSstephen hemminger 776502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 7772c12a74cSMichal Wrobel if (ifa->flags&IFA_F_TENTATIVE) 7782c12a74cSMichal Wrobel continue; 7791da177e4SLinus Torvalds if (idev->cnf.forwarding) 7801da177e4SLinus Torvalds addrconf_join_anycast(ifa); 7811da177e4SLinus Torvalds else 7821da177e4SLinus Torvalds addrconf_leave_anycast(ifa); 7831da177e4SLinus Torvalds } 78485b3daadSDavid Ahern inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF, 78585b3daadSDavid Ahern NETCONFA_FORWARDING, 786f3a1bfb1SNicolas Dichtel dev->ifindex, &idev->cnf); 7871da177e4SLinus Torvalds } 7881da177e4SLinus Torvalds 7891da177e4SLinus Torvalds 790e186932bSPavel Emelyanov static void addrconf_forward_change(struct net *net, __s32 newf) 7911da177e4SLinus Torvalds { 7921da177e4SLinus Torvalds struct net_device *dev; 7931da177e4SLinus Torvalds struct inet6_dev *idev; 7941da177e4SLinus Torvalds 7954acd4945SBen Hutchings for_each_netdev(net, dev) { 7961da177e4SLinus Torvalds idev = __in6_dev_get(dev); 7971da177e4SLinus Torvalds if (idev) { 798e186932bSPavel Emelyanov int changed = (!idev->cnf.forwarding) ^ (!newf); 799e186932bSPavel Emelyanov idev->cnf.forwarding = newf; 8001da177e4SLinus Torvalds if (changed) 8011da177e4SLinus Torvalds dev_forward_change(idev); 8021da177e4SLinus Torvalds } 8031da177e4SLinus Torvalds } 8041da177e4SLinus Torvalds } 805c8fecf22SPavel Emelyanov 806013d97e9SFrancesco Ruggeri static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf) 807c8fecf22SPavel Emelyanov { 808bff16c2fSPavel Emelyanov struct net *net; 809013d97e9SFrancesco Ruggeri int old; 810013d97e9SFrancesco Ruggeri 811013d97e9SFrancesco Ruggeri if (!rtnl_trylock()) 812013d97e9SFrancesco Ruggeri return restart_syscall(); 813bff16c2fSPavel Emelyanov 814bff16c2fSPavel Emelyanov net = (struct net *)table->extra2; 815013d97e9SFrancesco Ruggeri old = *p; 816013d97e9SFrancesco Ruggeri *p = newf; 817c8fecf22SPavel Emelyanov 818013d97e9SFrancesco Ruggeri if (p == &net->ipv6.devconf_dflt->forwarding) { 819f3a1bfb1SNicolas Dichtel if ((!newf) ^ (!old)) 82085b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 82185b3daadSDavid Ahern NETCONFA_FORWARDING, 822f3a1bfb1SNicolas Dichtel NETCONFA_IFINDEX_DEFAULT, 823f3a1bfb1SNicolas Dichtel net->ipv6.devconf_dflt); 824013d97e9SFrancesco Ruggeri rtnl_unlock(); 825013d97e9SFrancesco Ruggeri return 0; 82688af182eSEric W. Biederman } 827b325fddbSStephen Hemminger 828e186932bSPavel Emelyanov if (p == &net->ipv6.devconf_all->forwarding) { 829d26c638cSNicolas Dichtel int old_dflt = net->ipv6.devconf_dflt->forwarding; 830d26c638cSNicolas Dichtel 831e186932bSPavel Emelyanov net->ipv6.devconf_dflt->forwarding = newf; 832d26c638cSNicolas Dichtel if ((!newf) ^ (!old_dflt)) 83385b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 83485b3daadSDavid Ahern NETCONFA_FORWARDING, 835d26c638cSNicolas Dichtel NETCONFA_IFINDEX_DEFAULT, 836d26c638cSNicolas Dichtel net->ipv6.devconf_dflt); 837d26c638cSNicolas Dichtel 838e186932bSPavel Emelyanov addrconf_forward_change(net, newf); 839f3a1bfb1SNicolas Dichtel if ((!newf) ^ (!old)) 84085b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 84185b3daadSDavid Ahern NETCONFA_FORWARDING, 842f3a1bfb1SNicolas Dichtel NETCONFA_IFINDEX_ALL, 843f3a1bfb1SNicolas Dichtel net->ipv6.devconf_all); 844013d97e9SFrancesco Ruggeri } else if ((!newf) ^ (!old)) 845c8fecf22SPavel Emelyanov dev_forward_change((struct inet6_dev *)table->extra1); 8460187bdfbSBen Hutchings rtnl_unlock(); 847c8fecf22SPavel Emelyanov 848013d97e9SFrancesco Ruggeri if (newf) 8497b4da532SDaniel Lezcano rt6_purge_dflt_routers(net); 850b325fddbSStephen Hemminger return 1; 851c8fecf22SPavel Emelyanov } 85235103d11SAndy Gospodarek 85335103d11SAndy Gospodarek static void addrconf_linkdown_change(struct net *net, __s32 newf) 85435103d11SAndy Gospodarek { 85535103d11SAndy Gospodarek struct net_device *dev; 85635103d11SAndy Gospodarek struct inet6_dev *idev; 85735103d11SAndy Gospodarek 85835103d11SAndy Gospodarek for_each_netdev(net, dev) { 85935103d11SAndy Gospodarek idev = __in6_dev_get(dev); 86035103d11SAndy Gospodarek if (idev) { 86135103d11SAndy Gospodarek int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf); 86235103d11SAndy Gospodarek 86335103d11SAndy Gospodarek idev->cnf.ignore_routes_with_linkdown = newf; 86435103d11SAndy Gospodarek if (changed) 86535103d11SAndy Gospodarek inet6_netconf_notify_devconf(dev_net(dev), 86685b3daadSDavid Ahern RTM_NEWNETCONF, 86735103d11SAndy Gospodarek NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 86835103d11SAndy Gospodarek dev->ifindex, 86935103d11SAndy Gospodarek &idev->cnf); 87035103d11SAndy Gospodarek } 87135103d11SAndy Gospodarek } 87235103d11SAndy Gospodarek } 87335103d11SAndy Gospodarek 87435103d11SAndy Gospodarek static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf) 87535103d11SAndy Gospodarek { 87635103d11SAndy Gospodarek struct net *net; 87735103d11SAndy Gospodarek int old; 87835103d11SAndy Gospodarek 87935103d11SAndy Gospodarek if (!rtnl_trylock()) 88035103d11SAndy Gospodarek return restart_syscall(); 88135103d11SAndy Gospodarek 88235103d11SAndy Gospodarek net = (struct net *)table->extra2; 88335103d11SAndy Gospodarek old = *p; 88435103d11SAndy Gospodarek *p = newf; 88535103d11SAndy Gospodarek 88635103d11SAndy Gospodarek if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) { 88735103d11SAndy Gospodarek if ((!newf) ^ (!old)) 88835103d11SAndy Gospodarek inet6_netconf_notify_devconf(net, 88985b3daadSDavid Ahern RTM_NEWNETCONF, 89035103d11SAndy Gospodarek NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 89135103d11SAndy Gospodarek NETCONFA_IFINDEX_DEFAULT, 89235103d11SAndy Gospodarek net->ipv6.devconf_dflt); 89335103d11SAndy Gospodarek rtnl_unlock(); 89435103d11SAndy Gospodarek return 0; 89535103d11SAndy Gospodarek } 89635103d11SAndy Gospodarek 89735103d11SAndy Gospodarek if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) { 89835103d11SAndy Gospodarek net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf; 89935103d11SAndy Gospodarek addrconf_linkdown_change(net, newf); 90035103d11SAndy Gospodarek if ((!newf) ^ (!old)) 90135103d11SAndy Gospodarek inet6_netconf_notify_devconf(net, 90285b3daadSDavid Ahern RTM_NEWNETCONF, 90335103d11SAndy Gospodarek NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 90435103d11SAndy Gospodarek NETCONFA_IFINDEX_ALL, 90535103d11SAndy Gospodarek net->ipv6.devconf_all); 90635103d11SAndy Gospodarek } 90735103d11SAndy Gospodarek rtnl_unlock(); 90835103d11SAndy Gospodarek 90935103d11SAndy Gospodarek return 1; 91035103d11SAndy Gospodarek } 91135103d11SAndy Gospodarek 9121da177e4SLinus Torvalds #endif 9131da177e4SLinus Torvalds 9145c578aedSstephen hemminger /* Nobody refers to this ifaddr, destroy it */ 9151da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp) 9161da177e4SLinus Torvalds { 917c2e21293Sstephen hemminger WARN_ON(!hlist_unhashed(&ifp->addr_lst)); 918547b792cSIlpo Järvinen 9191da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG 92091df42beSJoe Perches pr_debug("%s\n", __func__); 9211da177e4SLinus Torvalds #endif 9221da177e4SLinus Torvalds 9231da177e4SLinus Torvalds in6_dev_put(ifp->idev); 9241da177e4SLinus Torvalds 925c15b1ccaSHannes Frederic Sowa if (cancel_delayed_work(&ifp->dad_work)) 926c15b1ccaSHannes Frederic Sowa pr_notice("delayed DAD work was pending while freeing ifa=%p\n", 927c15b1ccaSHannes Frederic Sowa ifp); 9281da177e4SLinus Torvalds 929e9d3e084SHerbert Xu if (ifp->state != INET6_IFADDR_STATE_DEAD) { 930f3213831SJoe Perches pr_warn("Freeing alive inet6 address %p\n", ifp); 9311da177e4SLinus Torvalds return; 9321da177e4SLinus Torvalds } 9331da177e4SLinus Torvalds 934e5785985SLai Jiangshan kfree_rcu(ifp, rcu); 9351da177e4SLinus Torvalds } 9361da177e4SLinus Torvalds 937e55ffac6SBrian Haley static void 938e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp) 939e55ffac6SBrian Haley { 940502a2ffdSstephen hemminger struct list_head *p; 9418a6ce0c0SYOSHIFUJI Hideaki int ifp_scope = ipv6_addr_src_scope(&ifp->addr); 942e55ffac6SBrian Haley 943e55ffac6SBrian Haley /* 944e55ffac6SBrian Haley * Each device address list is sorted in order of scope - 945e55ffac6SBrian Haley * global before linklocal. 946e55ffac6SBrian Haley */ 947502a2ffdSstephen hemminger list_for_each(p, &idev->addr_list) { 948502a2ffdSstephen hemminger struct inet6_ifaddr *ifa 949502a2ffdSstephen hemminger = list_entry(p, struct inet6_ifaddr, if_list); 9508a6ce0c0SYOSHIFUJI Hideaki if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr)) 951e55ffac6SBrian Haley break; 952e55ffac6SBrian Haley } 953e55ffac6SBrian Haley 9548ef802aaSEric Dumazet list_add_tail_rcu(&ifp->if_list, p); 955e55ffac6SBrian Haley } 956e55ffac6SBrian Haley 9573f27fb23SEric Dumazet static u32 inet6_addr_hash(const struct net *net, const struct in6_addr *addr) 9583eb84f49SYOSHIFUJI Hideaki { 9593f27fb23SEric Dumazet u32 val = ipv6_addr_hash(addr) ^ net_hash_mix(net); 9603f27fb23SEric Dumazet 9613f27fb23SEric Dumazet return hash_32(val, IN6_ADDR_HSIZE_SHIFT); 9623eb84f49SYOSHIFUJI Hideaki } 9633eb84f49SYOSHIFUJI Hideaki 96456fc709bSEric Dumazet static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr, 965752a9292SEric Dumazet struct net_device *dev, unsigned int hash) 96656fc709bSEric Dumazet { 96756fc709bSEric Dumazet struct inet6_ifaddr *ifp; 96856fc709bSEric Dumazet 96956fc709bSEric Dumazet hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) { 97056fc709bSEric Dumazet if (!net_eq(dev_net(ifp->idev->dev), net)) 97156fc709bSEric Dumazet continue; 97256fc709bSEric Dumazet if (ipv6_addr_equal(&ifp->addr, addr)) { 97356fc709bSEric Dumazet if (!dev || ifp->idev->dev == dev) 97456fc709bSEric Dumazet return true; 97556fc709bSEric Dumazet } 97656fc709bSEric Dumazet } 97756fc709bSEric Dumazet return false; 97856fc709bSEric Dumazet } 97956fc709bSEric Dumazet 980f3d9832eSDavid Ahern static int ipv6_add_addr_hash(struct net_device *dev, struct inet6_ifaddr *ifa) 981f3d9832eSDavid Ahern { 9823f27fb23SEric Dumazet unsigned int hash = inet6_addr_hash(dev_net(dev), &ifa->addr); 983f3d9832eSDavid Ahern int err = 0; 984f3d9832eSDavid Ahern 985f3d9832eSDavid Ahern spin_lock(&addrconf_hash_lock); 986f3d9832eSDavid Ahern 987f3d9832eSDavid Ahern /* Ignore adding duplicate addresses on an interface */ 988752a9292SEric Dumazet if (ipv6_chk_same_addr(dev_net(dev), &ifa->addr, dev, hash)) { 989e32ac250SJoe Perches netdev_dbg(dev, "ipv6_add_addr: already assigned\n"); 990f3d9832eSDavid Ahern err = -EEXIST; 991752a9292SEric Dumazet } else { 992752a9292SEric Dumazet hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]); 993f3d9832eSDavid Ahern } 994f3d9832eSDavid Ahern 995f3d9832eSDavid Ahern spin_unlock(&addrconf_hash_lock); 996f3d9832eSDavid Ahern 997f3d9832eSDavid Ahern return err; 998f3d9832eSDavid Ahern } 999f3d9832eSDavid Ahern 10001da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */ 10011da177e4SLinus Torvalds 10021da177e4SLinus Torvalds static struct inet6_ifaddr * 1003e6464b8cSDavid Ahern ipv6_add_addr(struct inet6_dev *idev, struct ifa6_config *cfg, 1004de95e047SDavid Ahern bool can_block, struct netlink_ext_ack *extack) 10051da177e4SLinus Torvalds { 1006f3d9832eSDavid Ahern gfp_t gfp_flags = can_block ? GFP_KERNEL : GFP_ATOMIC; 1007e6464b8cSDavid Ahern int addr_type = ipv6_addr_type(cfg->pfx); 1008df789fe7SDavid Forster struct net *net = dev_net(idev->dev); 10091da177e4SLinus Torvalds struct inet6_ifaddr *ifa = NULL; 1010360a9887SDavid Ahern struct fib6_info *f6i = NULL; 10111da177e4SLinus Torvalds int err = 0; 1012d68b8270SYOSHIFUJI Hideaki 1013d68b8270SYOSHIFUJI Hideaki if (addr_type == IPV6_ADDR_ANY || 1014d68b8270SYOSHIFUJI Hideaki addr_type & IPV6_ADDR_MULTICAST || 1015d68b8270SYOSHIFUJI Hideaki (!(idev->dev->flags & IFF_LOOPBACK) && 10163ede0bbcSRobert Shearman !netif_is_l3_master(idev->dev) && 1017d68b8270SYOSHIFUJI Hideaki addr_type & IPV6_ADDR_LOOPBACK)) 1018d68b8270SYOSHIFUJI Hideaki return ERR_PTR(-EADDRNOTAVAIL); 10191da177e4SLinus Torvalds 10201da177e4SLinus Torvalds if (idev->dead) { 10211da177e4SLinus Torvalds err = -ENODEV; /*XXX*/ 1022f3d9832eSDavid Ahern goto out; 10231da177e4SLinus Torvalds } 10241da177e4SLinus Torvalds 102556d417b1SBrian Haley if (idev->cnf.disable_ipv6) { 10269bdd8d40SBrian Haley err = -EACCES; 1027f3d9832eSDavid Ahern goto out; 10289bdd8d40SBrian Haley } 10299bdd8d40SBrian Haley 1030ff7883eaSDavid Ahern /* validator notifier needs to be blocking; 1031ff7883eaSDavid Ahern * do not call in atomic context 1032ff7883eaSDavid Ahern */ 1033ff7883eaSDavid Ahern if (can_block) { 1034ff7883eaSDavid Ahern struct in6_validator_info i6vi = { 1035e6464b8cSDavid Ahern .i6vi_addr = *cfg->pfx, 1036ff7883eaSDavid Ahern .i6vi_dev = idev, 1037de95e047SDavid Ahern .extack = extack, 1038ff7883eaSDavid Ahern }; 1039ff7883eaSDavid Ahern 10403ad7d246SKrister Johansen err = inet6addr_validator_notifier_call_chain(NETDEV_UP, &i6vi); 10413ad7d246SKrister Johansen err = notifier_to_errno(err); 1042f3d9832eSDavid Ahern if (err < 0) 10431da177e4SLinus Torvalds goto out; 1044ff7883eaSDavid Ahern } 10451da177e4SLinus Torvalds 1046f3d9832eSDavid Ahern ifa = kzalloc(sizeof(*ifa), gfp_flags); 104763159f29SIan Morris if (!ifa) { 10481da177e4SLinus Torvalds err = -ENOBUFS; 10491da177e4SLinus Torvalds goto out; 10501da177e4SLinus Torvalds } 10511da177e4SLinus Torvalds 1052e6464b8cSDavid Ahern f6i = addrconf_f6i_alloc(net, idev, cfg->pfx, false, gfp_flags); 1053360a9887SDavid Ahern if (IS_ERR(f6i)) { 1054360a9887SDavid Ahern err = PTR_ERR(f6i); 1055360a9887SDavid Ahern f6i = NULL; 10561da177e4SLinus Torvalds goto out; 10571da177e4SLinus Torvalds } 10581da177e4SLinus Torvalds 1059df789fe7SDavid Forster if (net->ipv6.devconf_all->disable_policy || 1060df789fe7SDavid Forster idev->cnf.disable_policy) 1061360a9887SDavid Ahern f6i->dst_nopolicy = true; 1062df789fe7SDavid Forster 1063bba24896SJiri Pirko neigh_parms_data_state_setall(idev->nd_parms); 1064bba24896SJiri Pirko 1065e6464b8cSDavid Ahern ifa->addr = *cfg->pfx; 1066e6464b8cSDavid Ahern if (cfg->peer_pfx) 1067e6464b8cSDavid Ahern ifa->peer_addr = *cfg->peer_pfx; 10681da177e4SLinus Torvalds 10691da177e4SLinus Torvalds spin_lock_init(&ifa->lock); 1070c15b1ccaSHannes Frederic Sowa INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work); 1071c2e21293Sstephen hemminger INIT_HLIST_NODE(&ifa->addr_lst); 1072e6464b8cSDavid Ahern ifa->scope = cfg->scope; 1073e6464b8cSDavid Ahern ifa->prefix_len = cfg->plen; 10748308f3ffSDavid Ahern ifa->rt_priority = cfg->rt_priority; 1075e6464b8cSDavid Ahern ifa->flags = cfg->ifa_flags; 107666eb9f86SMahesh Bandewar /* No need to add the TENTATIVE flag for addresses with NODAD */ 1077e6464b8cSDavid Ahern if (!(cfg->ifa_flags & IFA_F_NODAD)) 107866eb9f86SMahesh Bandewar ifa->flags |= IFA_F_TENTATIVE; 1079e6464b8cSDavid Ahern ifa->valid_lft = cfg->valid_lft; 1080e6464b8cSDavid Ahern ifa->prefered_lft = cfg->preferred_lft; 10811da177e4SLinus Torvalds ifa->cstamp = ifa->tstamp = jiffies; 1082617fe29dSDaniel Borkmann ifa->tokenized = false; 10831da177e4SLinus Torvalds 1084360a9887SDavid Ahern ifa->rt = f6i; 108557f5f544SKeir Fraser 10861da177e4SLinus Torvalds ifa->idev = idev; 1087f3d9832eSDavid Ahern in6_dev_hold(idev); 1088f3d9832eSDavid Ahern 10891da177e4SLinus Torvalds /* For caller */ 1090271201c0SReshetova, Elena refcount_set(&ifa->refcnt, 1); 10911da177e4SLinus Torvalds 1092f3d9832eSDavid Ahern rcu_read_lock_bh(); 10931da177e4SLinus Torvalds 1094f3d9832eSDavid Ahern err = ipv6_add_addr_hash(idev->dev, ifa); 1095f3d9832eSDavid Ahern if (err < 0) { 1096f3d9832eSDavid Ahern rcu_read_unlock_bh(); 1097f3d9832eSDavid Ahern goto out; 1098f3d9832eSDavid Ahern } 10991da177e4SLinus Torvalds 11001da177e4SLinus Torvalds write_lock(&idev->lock); 1101f3d9832eSDavid Ahern 11021da177e4SLinus Torvalds /* Add to inet6_dev unicast addr list. */ 1103e55ffac6SBrian Haley ipv6_link_dev_addr(idev, ifa); 11041da177e4SLinus Torvalds 11051da177e4SLinus Torvalds if (ifa->flags&IFA_F_TEMPORARY) { 1106372e6c8fSstephen hemminger list_add(&ifa->tmp_list, &idev->tempaddr_list); 11071da177e4SLinus Torvalds in6_ifa_hold(ifa); 11081da177e4SLinus Torvalds } 11091da177e4SLinus Torvalds 11101da177e4SLinus Torvalds in6_ifa_hold(ifa); 11111da177e4SLinus Torvalds write_unlock(&idev->lock); 1112f3d9832eSDavid Ahern 11138814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 11141da177e4SLinus Torvalds 1115f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_UP, ifa); 1116f3d9832eSDavid Ahern out: 1117f3d9832eSDavid Ahern if (unlikely(err < 0)) { 1118360a9887SDavid Ahern fib6_info_release(f6i); 111993531c67SDavid Ahern 1120f3d9832eSDavid Ahern if (ifa) { 1121f3d9832eSDavid Ahern if (ifa->idev) 1122f3d9832eSDavid Ahern in6_dev_put(ifa->idev); 11231da177e4SLinus Torvalds kfree(ifa); 1124f3d9832eSDavid Ahern } 11251da177e4SLinus Torvalds ifa = ERR_PTR(err); 11261da177e4SLinus Torvalds } 11271da177e4SLinus Torvalds 11281da177e4SLinus Torvalds return ifa; 11291da177e4SLinus Torvalds } 11301da177e4SLinus Torvalds 11315b84efecSThomas Haller enum cleanup_prefix_rt_t { 11325b84efecSThomas Haller CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */ 11335b84efecSThomas Haller CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */ 11345b84efecSThomas Haller CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */ 11355b84efecSThomas Haller }; 11365b84efecSThomas Haller 11375b84efecSThomas Haller /* 11385b84efecSThomas Haller * Check, whether the prefix for ifp would still need a prefix route 11395b84efecSThomas Haller * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_* 11405b84efecSThomas Haller * constants. 11415b84efecSThomas Haller * 11425b84efecSThomas Haller * 1) we don't purge prefix if address was not permanent. 11435b84efecSThomas Haller * prefix is managed by its own lifetime. 11445b84efecSThomas Haller * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE. 11455b84efecSThomas Haller * 3) if there are no addresses, delete prefix. 11465b84efecSThomas Haller * 4) if there are still other permanent address(es), 11475b84efecSThomas Haller * corresponding prefix is still permanent. 11485b84efecSThomas Haller * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE, 11495b84efecSThomas Haller * don't purge the prefix, assume user space is managing it. 11505b84efecSThomas Haller * 6) otherwise, update prefix lifetime to the 11515b84efecSThomas Haller * longest valid lifetime among the corresponding 11525b84efecSThomas Haller * addresses on the device. 11535b84efecSThomas Haller * Note: subsequent RA will update lifetime. 11545b84efecSThomas Haller **/ 11555b84efecSThomas Haller static enum cleanup_prefix_rt_t 11565b84efecSThomas Haller check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires) 11575b84efecSThomas Haller { 11585b84efecSThomas Haller struct inet6_ifaddr *ifa; 11595b84efecSThomas Haller struct inet6_dev *idev = ifp->idev; 11605b84efecSThomas Haller unsigned long lifetime; 11615b84efecSThomas Haller enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL; 11625b84efecSThomas Haller 11635b84efecSThomas Haller *expires = jiffies; 11645b84efecSThomas Haller 11655b84efecSThomas Haller list_for_each_entry(ifa, &idev->addr_list, if_list) { 11665b84efecSThomas Haller if (ifa == ifp) 11675b84efecSThomas Haller continue; 11685b84efecSThomas Haller if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr, 11695b84efecSThomas Haller ifp->prefix_len)) 11705b84efecSThomas Haller continue; 11715b84efecSThomas Haller if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE)) 11725b84efecSThomas Haller return CLEANUP_PREFIX_RT_NOP; 11735b84efecSThomas Haller 11745b84efecSThomas Haller action = CLEANUP_PREFIX_RT_EXPIRE; 11755b84efecSThomas Haller 11765b84efecSThomas Haller spin_lock(&ifa->lock); 11775b84efecSThomas Haller 11785b84efecSThomas Haller lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ); 11795b84efecSThomas Haller /* 11805b84efecSThomas Haller * Note: Because this address is 11815b84efecSThomas Haller * not permanent, lifetime < 11825b84efecSThomas Haller * LONG_MAX / HZ here. 11835b84efecSThomas Haller */ 11845b84efecSThomas Haller if (time_before(*expires, ifa->tstamp + lifetime * HZ)) 11855b84efecSThomas Haller *expires = ifa->tstamp + lifetime * HZ; 11865b84efecSThomas Haller spin_unlock(&ifa->lock); 11875b84efecSThomas Haller } 11885b84efecSThomas Haller 11895b84efecSThomas Haller return action; 11905b84efecSThomas Haller } 11915b84efecSThomas Haller 11925b84efecSThomas Haller static void 11935b84efecSThomas Haller cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt) 11945b84efecSThomas Haller { 119593c2fb25SDavid Ahern struct fib6_info *f6i; 11965b84efecSThomas Haller 119793c2fb25SDavid Ahern f6i = addrconf_get_prefix_route(&ifp->addr, 11985b84efecSThomas Haller ifp->prefix_len, 11995b84efecSThomas Haller ifp->idev->dev, 12005b84efecSThomas Haller 0, RTF_GATEWAY | RTF_DEFAULT); 120193c2fb25SDavid Ahern if (f6i) { 12025b84efecSThomas Haller if (del_rt) 120393c2fb25SDavid Ahern ip6_del_rt(dev_net(ifp->idev->dev), f6i); 12045b84efecSThomas Haller else { 120593c2fb25SDavid Ahern if (!(f6i->fib6_flags & RTF_EXPIRES)) 120693c2fb25SDavid Ahern fib6_set_expires(f6i, expires); 120793c2fb25SDavid Ahern fib6_info_release(f6i); 12085b84efecSThomas Haller } 12095b84efecSThomas Haller } 12105b84efecSThomas Haller } 12115b84efecSThomas Haller 12125b84efecSThomas Haller 12131da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */ 12141da177e4SLinus Torvalds 12151da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp) 12161da177e4SLinus Torvalds { 12174c5ff6a6SHerbert Xu int state; 12185b84efecSThomas Haller enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP; 12195b84efecSThomas Haller unsigned long expires; 12201da177e4SLinus Torvalds 1221c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 1222c15b1ccaSHannes Frederic Sowa 12238e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 12244c5ff6a6SHerbert Xu state = ifp->state; 1225e9d3e084SHerbert Xu ifp->state = INET6_IFADDR_STATE_DEAD; 12268e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 12274c5ff6a6SHerbert Xu 12284c5ff6a6SHerbert Xu if (state == INET6_IFADDR_STATE_DEAD) 12294c5ff6a6SHerbert Xu goto out; 12301da177e4SLinus Torvalds 12315c578aedSstephen hemminger spin_lock_bh(&addrconf_hash_lock); 12325c578aedSstephen hemminger hlist_del_init_rcu(&ifp->addr_lst); 12335c578aedSstephen hemminger spin_unlock_bh(&addrconf_hash_lock); 12341da177e4SLinus Torvalds 12355b84efecSThomas Haller write_lock_bh(&ifp->idev->lock); 12365d9efa7eSDavid S. Miller 12371da177e4SLinus Torvalds if (ifp->flags&IFA_F_TEMPORARY) { 1238372e6c8fSstephen hemminger list_del(&ifp->tmp_list); 12391da177e4SLinus Torvalds if (ifp->ifpub) { 12401da177e4SLinus Torvalds in6_ifa_put(ifp->ifpub); 12411da177e4SLinus Torvalds ifp->ifpub = NULL; 12421da177e4SLinus Torvalds } 12431da177e4SLinus Torvalds __in6_ifa_put(ifp); 12441da177e4SLinus Torvalds } 12451da177e4SLinus Torvalds 12465b84efecSThomas Haller if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE)) 12475b84efecSThomas Haller action = check_cleanup_prefix_route(ifp, &expires); 12485b84efecSThomas Haller 12498ef802aaSEric Dumazet list_del_rcu(&ifp->if_list); 12501da177e4SLinus Torvalds __in6_ifa_put(ifp); 1251502a2ffdSstephen hemminger 12525b84efecSThomas Haller write_unlock_bh(&ifp->idev->lock); 12531da177e4SLinus Torvalds 1254c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifp); 1255b2238566SAndrey Vagin 12561da177e4SLinus Torvalds ipv6_ifa_notify(RTM_DELADDR, ifp); 12571da177e4SLinus Torvalds 1258f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_DOWN, ifp); 12591da177e4SLinus Torvalds 12605b84efecSThomas Haller if (action != CLEANUP_PREFIX_RT_NOP) { 12615b84efecSThomas Haller cleanup_prefix_route(ifp, expires, 12625b84efecSThomas Haller action == CLEANUP_PREFIX_RT_DEL); 12631da177e4SLinus Torvalds } 12641da177e4SLinus Torvalds 1265c3968a85SDaniel Walter /* clean up prefsrc entries */ 1266c3968a85SDaniel Walter rt6_remove_prefsrc(ifp); 12674c5ff6a6SHerbert Xu out: 12681da177e4SLinus Torvalds in6_ifa_put(ifp); 12691da177e4SLinus Torvalds } 12701da177e4SLinus Torvalds 1271fffcefe9SEric Dumazet static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, 1272fffcefe9SEric Dumazet struct inet6_ifaddr *ift, 1273fffcefe9SEric Dumazet bool block) 12741da177e4SLinus Torvalds { 12751da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 12761da177e4SLinus Torvalds struct in6_addr addr, *tmpaddr; 1277e6464b8cSDavid Ahern unsigned long tmp_tstamp, age; 1278eac55bf9SBenoit Boissinot unsigned long regen_advance; 1279e6464b8cSDavid Ahern struct ifa6_config cfg; 12801da177e4SLinus Torvalds int ret = 0; 128176f793e3SLorenzo Colitti unsigned long now = jiffies; 128276506a98SJiri Bohac long max_desync_factor; 12837aa8e63fSJiri Bohac s32 cnf_temp_preferred_lft; 12841da177e4SLinus Torvalds 128553bd6749SJiri Pirko write_lock_bh(&idev->lock); 12861da177e4SLinus Torvalds if (ift) { 12871da177e4SLinus Torvalds spin_lock_bh(&ift->lock); 12881da177e4SLinus Torvalds memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8); 12891da177e4SLinus Torvalds spin_unlock_bh(&ift->lock); 12901da177e4SLinus Torvalds tmpaddr = &addr; 12911da177e4SLinus Torvalds } else { 12921da177e4SLinus Torvalds tmpaddr = NULL; 12931da177e4SLinus Torvalds } 12941da177e4SLinus Torvalds retry: 12951da177e4SLinus Torvalds in6_dev_hold(idev); 12961da177e4SLinus Torvalds if (idev->cnf.use_tempaddr <= 0) { 129753bd6749SJiri Pirko write_unlock_bh(&idev->lock); 1298f3213831SJoe Perches pr_info("%s: use_tempaddr is disabled\n", __func__); 12991da177e4SLinus Torvalds in6_dev_put(idev); 13001da177e4SLinus Torvalds ret = -1; 13011da177e4SLinus Torvalds goto out; 13021da177e4SLinus Torvalds } 13031da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 13041da177e4SLinus Torvalds if (ifp->regen_count++ >= idev->cnf.regen_max_retry) { 13051da177e4SLinus Torvalds idev->cnf.use_tempaddr = -1; /*XXX*/ 13061da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 130753bd6749SJiri Pirko write_unlock_bh(&idev->lock); 1308f3213831SJoe Perches pr_warn("%s: regeneration time exceeded - disabled temporary address support\n", 1309f3213831SJoe Perches __func__); 13101da177e4SLinus Torvalds in6_dev_put(idev); 13111da177e4SLinus Torvalds ret = -1; 13121da177e4SLinus Torvalds goto out; 13131da177e4SLinus Torvalds } 13141da177e4SLinus Torvalds in6_ifa_hold(ifp); 13151da177e4SLinus Torvalds memcpy(addr.s6_addr, ifp->addr.s6_addr, 8); 13169d6280daSJiri Bohac ipv6_try_regen_rndid(idev, tmpaddr); 13171da177e4SLinus Torvalds memcpy(&addr.s6_addr[8], idev->rndid, 8); 131876f793e3SLorenzo Colitti age = (now - ifp->tstamp) / HZ; 13191da177e4SLinus Torvalds 1320eac55bf9SBenoit Boissinot regen_advance = idev->cnf.regen_max_retry * 1321eac55bf9SBenoit Boissinot idev->cnf.dad_transmits * 13221f9248e5SJiri Pirko NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ; 132376506a98SJiri Bohac 132476506a98SJiri Bohac /* recalculate max_desync_factor each time and update 132576506a98SJiri Bohac * idev->desync_factor if it's larger 132676506a98SJiri Bohac */ 13277aa8e63fSJiri Bohac cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft); 132876506a98SJiri Bohac max_desync_factor = min_t(__u32, 132976506a98SJiri Bohac idev->cnf.max_desync_factor, 13307aa8e63fSJiri Bohac cnf_temp_preferred_lft - regen_advance); 133176506a98SJiri Bohac 133276506a98SJiri Bohac if (unlikely(idev->desync_factor > max_desync_factor)) { 133376506a98SJiri Bohac if (max_desync_factor > 0) { 133476506a98SJiri Bohac get_random_bytes(&idev->desync_factor, 133576506a98SJiri Bohac sizeof(idev->desync_factor)); 133676506a98SJiri Bohac idev->desync_factor %= max_desync_factor; 133776506a98SJiri Bohac } else { 133876506a98SJiri Bohac idev->desync_factor = 0; 133976506a98SJiri Bohac } 134076506a98SJiri Bohac } 134176506a98SJiri Bohac 13423f2d67b6SDavid Ahern memset(&cfg, 0, sizeof(cfg)); 1343e6464b8cSDavid Ahern cfg.valid_lft = min_t(__u32, ifp->valid_lft, 134476506a98SJiri Bohac idev->cnf.temp_valid_lft + age); 1345e6464b8cSDavid Ahern cfg.preferred_lft = cnf_temp_preferred_lft + age - idev->desync_factor; 1346e6464b8cSDavid Ahern cfg.preferred_lft = min_t(__u32, ifp->prefered_lft, cfg.preferred_lft); 1347e6464b8cSDavid Ahern 1348e6464b8cSDavid Ahern cfg.plen = ifp->prefix_len; 134976506a98SJiri Bohac tmp_tstamp = ifp->tstamp; 135076506a98SJiri Bohac spin_unlock_bh(&ifp->lock); 135176506a98SJiri Bohac 135253bd6749SJiri Pirko write_unlock_bh(&idev->lock); 135395c385b4SNeil Horman 1354eac55bf9SBenoit Boissinot /* A temporary address is created only if this calculated Preferred 1355eac55bf9SBenoit Boissinot * Lifetime is greater than REGEN_ADVANCE time units. In particular, 1356eac55bf9SBenoit Boissinot * an implementation must not create a temporary address with a zero 1357eac55bf9SBenoit Boissinot * Preferred Lifetime. 1358ecab6701SHeiner Kallweit * Use age calculation as in addrconf_verify to avoid unnecessary 1359ecab6701SHeiner Kallweit * temporary addresses being generated. 1360eac55bf9SBenoit Boissinot */ 1361ecab6701SHeiner Kallweit age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; 1362e6464b8cSDavid Ahern if (cfg.preferred_lft <= regen_advance + age) { 1363eac55bf9SBenoit Boissinot in6_ifa_put(ifp); 1364eac55bf9SBenoit Boissinot in6_dev_put(idev); 1365eac55bf9SBenoit Boissinot ret = -1; 1366eac55bf9SBenoit Boissinot goto out; 1367eac55bf9SBenoit Boissinot } 1368eac55bf9SBenoit Boissinot 1369e6464b8cSDavid Ahern cfg.ifa_flags = IFA_F_TEMPORARY; 137095c385b4SNeil Horman /* set in addrconf_prefix_rcv() */ 137195c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 1372e6464b8cSDavid Ahern cfg.ifa_flags |= IFA_F_OPTIMISTIC; 137395c385b4SNeil Horman 1374e6464b8cSDavid Ahern cfg.pfx = &addr; 1375e6464b8cSDavid Ahern cfg.scope = ipv6_addr_scope(cfg.pfx); 1376e6464b8cSDavid Ahern 1377e6464b8cSDavid Ahern ift = ipv6_add_addr(idev, &cfg, block, NULL); 13784b08a8f1SHannes Frederic Sowa if (IS_ERR(ift)) { 13791da177e4SLinus Torvalds in6_ifa_put(ifp); 13801da177e4SLinus Torvalds in6_dev_put(idev); 1381f3213831SJoe Perches pr_info("%s: retry temporary address regeneration\n", __func__); 13821da177e4SLinus Torvalds tmpaddr = &addr; 138353bd6749SJiri Pirko write_lock_bh(&idev->lock); 13841da177e4SLinus Torvalds goto retry; 13851da177e4SLinus Torvalds } 13861da177e4SLinus Torvalds 13871da177e4SLinus Torvalds spin_lock_bh(&ift->lock); 13881da177e4SLinus Torvalds ift->ifpub = ifp; 138976f793e3SLorenzo Colitti ift->cstamp = now; 13901da177e4SLinus Torvalds ift->tstamp = tmp_tstamp; 13911da177e4SLinus Torvalds spin_unlock_bh(&ift->lock); 13921da177e4SLinus Torvalds 1393cf22f9a2SDavid S. Miller addrconf_dad_start(ift); 13941da177e4SLinus Torvalds in6_ifa_put(ift); 13951da177e4SLinus Torvalds in6_dev_put(idev); 13961da177e4SLinus Torvalds out: 13971da177e4SLinus Torvalds return ret; 13981da177e4SLinus Torvalds } 13991da177e4SLinus Torvalds 14001da177e4SLinus Torvalds /* 1401072047e4SYOSHIFUJI Hideaki * Choose an appropriate source address (RFC3484) 14021da177e4SLinus Torvalds */ 1403a9b05723SYOSHIFUJI Hideaki enum { 1404a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_INIT = 0, 1405a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_LOCAL, 1406a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_SCOPE, 1407a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PREFERRED, 1408a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6 1409a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_HOA, 1410a9b05723SYOSHIFUJI Hideaki #endif 1411a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_OIF, 1412a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_LABEL, 1413a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PRIVACY, 1414a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_ORCHID, 1415a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PREFIX, 14167fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 14177fd2561eSErik Kline IPV6_SADDR_RULE_NOT_OPTIMISTIC, 14187fd2561eSErik Kline #endif 1419a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_MAX 1420072047e4SYOSHIFUJI Hideaki }; 1421072047e4SYOSHIFUJI Hideaki 1422a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score { 1423a9b05723SYOSHIFUJI Hideaki int rule; 1424a9b05723SYOSHIFUJI Hideaki int addr_type; 1425a9b05723SYOSHIFUJI Hideaki struct inet6_ifaddr *ifa; 1426a9b05723SYOSHIFUJI Hideaki DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX); 1427a9b05723SYOSHIFUJI Hideaki int scopedist; 1428a9b05723SYOSHIFUJI Hideaki int matchlen; 1429a9b05723SYOSHIFUJI Hideaki }; 1430a9b05723SYOSHIFUJI Hideaki 1431a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst { 14329acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *addr; 1433a9b05723SYOSHIFUJI Hideaki int ifindex; 1434a9b05723SYOSHIFUJI Hideaki int scope; 1435a9b05723SYOSHIFUJI Hideaki int label; 14367cbca67cSYOSHIFUJI Hideaki unsigned int prefs; 1437a9b05723SYOSHIFUJI Hideaki }; 1438072047e4SYOSHIFUJI Hideaki 1439b6f99a21SDave Jones static inline int ipv6_saddr_preferred(int type) 14401da177e4SLinus Torvalds { 144145bb0060SUlrich Weber if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK)) 1442072047e4SYOSHIFUJI Hideaki return 1; 1443072047e4SYOSHIFUJI Hideaki return 0; 14441da177e4SLinus Torvalds } 14451da177e4SLinus Torvalds 144635e015e1SMatteo Croce static bool ipv6_use_optimistic_addr(struct net *net, 144735e015e1SMatteo Croce struct inet6_dev *idev) 14487fd2561eSErik Kline { 14497fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 145035e015e1SMatteo Croce if (!idev) 145135e015e1SMatteo Croce return false; 145235e015e1SMatteo Croce if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad) 145335e015e1SMatteo Croce return false; 145435e015e1SMatteo Croce if (!net->ipv6.devconf_all->use_optimistic && !idev->cnf.use_optimistic) 145535e015e1SMatteo Croce return false; 145635e015e1SMatteo Croce 145735e015e1SMatteo Croce return true; 14587fd2561eSErik Kline #else 14597fd2561eSErik Kline return false; 14607fd2561eSErik Kline #endif 14617fd2561eSErik Kline } 14627fd2561eSErik Kline 1463f1c02cfbSSabrina Dubroca static bool ipv6_allow_optimistic_dad(struct net *net, 1464f1c02cfbSSabrina Dubroca struct inet6_dev *idev) 1465f1c02cfbSSabrina Dubroca { 1466f1c02cfbSSabrina Dubroca #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 1467f1c02cfbSSabrina Dubroca if (!idev) 1468f1c02cfbSSabrina Dubroca return false; 1469f1c02cfbSSabrina Dubroca if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad) 1470f1c02cfbSSabrina Dubroca return false; 1471f1c02cfbSSabrina Dubroca 1472f1c02cfbSSabrina Dubroca return true; 1473f1c02cfbSSabrina Dubroca #else 1474f1c02cfbSSabrina Dubroca return false; 1475f1c02cfbSSabrina Dubroca #endif 1476f1c02cfbSSabrina Dubroca } 1477f1c02cfbSSabrina Dubroca 14783de23255SBenjamin Thery static int ipv6_get_saddr_eval(struct net *net, 14793de23255SBenjamin Thery struct ipv6_saddr_score *score, 1480a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst *dst, 1481a9b05723SYOSHIFUJI Hideaki int i) 1482a9b05723SYOSHIFUJI Hideaki { 1483a9b05723SYOSHIFUJI Hideaki int ret; 1484a9b05723SYOSHIFUJI Hideaki 1485a9b05723SYOSHIFUJI Hideaki if (i <= score->rule) { 1486a9b05723SYOSHIFUJI Hideaki switch (i) { 1487a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_SCOPE: 1488a9b05723SYOSHIFUJI Hideaki ret = score->scopedist; 1489a9b05723SYOSHIFUJI Hideaki break; 1490a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFIX: 1491a9b05723SYOSHIFUJI Hideaki ret = score->matchlen; 1492a9b05723SYOSHIFUJI Hideaki break; 1493a9b05723SYOSHIFUJI Hideaki default: 1494a9b05723SYOSHIFUJI Hideaki ret = !!test_bit(i, score->scorebits); 1495a9b05723SYOSHIFUJI Hideaki } 1496a9b05723SYOSHIFUJI Hideaki goto out; 1497a9b05723SYOSHIFUJI Hideaki } 1498a9b05723SYOSHIFUJI Hideaki 1499a9b05723SYOSHIFUJI Hideaki switch (i) { 1500a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_INIT: 1501a9b05723SYOSHIFUJI Hideaki /* Rule 0: remember if hiscore is not ready yet */ 1502a9b05723SYOSHIFUJI Hideaki ret = !!score->ifa; 1503a9b05723SYOSHIFUJI Hideaki break; 1504a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_LOCAL: 1505a9b05723SYOSHIFUJI Hideaki /* Rule 1: Prefer same address */ 1506a9b05723SYOSHIFUJI Hideaki ret = ipv6_addr_equal(&score->ifa->addr, dst->addr); 1507a9b05723SYOSHIFUJI Hideaki break; 1508a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_SCOPE: 1509a9b05723SYOSHIFUJI Hideaki /* Rule 2: Prefer appropriate scope 1510a9b05723SYOSHIFUJI Hideaki * 1511a9b05723SYOSHIFUJI Hideaki * ret 1512a9b05723SYOSHIFUJI Hideaki * ^ 1513a9b05723SYOSHIFUJI Hideaki * -1 | d 15 1514a9b05723SYOSHIFUJI Hideaki * ---+--+-+---> scope 1515a9b05723SYOSHIFUJI Hideaki * | 1516a9b05723SYOSHIFUJI Hideaki * | d is scope of the destination. 1517a9b05723SYOSHIFUJI Hideaki * B-d | \ 1518a9b05723SYOSHIFUJI Hideaki * | \ <- smaller scope is better if 1519db9c7c39Sstephen hemminger * B-15 | \ if scope is enough for destination. 1520a9b05723SYOSHIFUJI Hideaki * | ret = B - scope (-1 <= scope >= d <= 15). 1521a9b05723SYOSHIFUJI Hideaki * d-C-1 | / 1522a9b05723SYOSHIFUJI Hideaki * |/ <- greater is better 1523a9b05723SYOSHIFUJI Hideaki * -C / if scope is not enough for destination. 1524a9b05723SYOSHIFUJI Hideaki * /| ret = scope - C (-1 <= d < scope <= 15). 1525a9b05723SYOSHIFUJI Hideaki * 1526a9b05723SYOSHIFUJI Hideaki * d - C - 1 < B -15 (for all -1 <= d <= 15). 1527a9b05723SYOSHIFUJI Hideaki * C > d + 14 - B >= 15 + 14 - B = 29 - B. 1528a9b05723SYOSHIFUJI Hideaki * Assume B = 0 and we get C > 29. 1529a9b05723SYOSHIFUJI Hideaki */ 1530a9b05723SYOSHIFUJI Hideaki ret = __ipv6_addr_src_scope(score->addr_type); 1531a9b05723SYOSHIFUJI Hideaki if (ret >= dst->scope) 1532a9b05723SYOSHIFUJI Hideaki ret = -ret; 1533a9b05723SYOSHIFUJI Hideaki else 1534a9b05723SYOSHIFUJI Hideaki ret -= 128; /* 30 is enough */ 1535a9b05723SYOSHIFUJI Hideaki score->scopedist = ret; 1536a9b05723SYOSHIFUJI Hideaki break; 1537a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFERRED: 15387fd2561eSErik Kline { 1539a9b05723SYOSHIFUJI Hideaki /* Rule 3: Avoid deprecated and optimistic addresses */ 15407fd2561eSErik Kline u8 avoid = IFA_F_DEPRECATED; 15417fd2561eSErik Kline 154235e015e1SMatteo Croce if (!ipv6_use_optimistic_addr(net, score->ifa->idev)) 15437fd2561eSErik Kline avoid |= IFA_F_OPTIMISTIC; 1544a9b05723SYOSHIFUJI Hideaki ret = ipv6_saddr_preferred(score->addr_type) || 15457fd2561eSErik Kline !(score->ifa->flags & avoid); 1546a9b05723SYOSHIFUJI Hideaki break; 15477fd2561eSErik Kline } 1548a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6 1549a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_HOA: 15507cbca67cSYOSHIFUJI Hideaki { 1551a9b05723SYOSHIFUJI Hideaki /* Rule 4: Prefer home address */ 15527cbca67cSYOSHIFUJI Hideaki int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA); 15537cbca67cSYOSHIFUJI Hideaki ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome; 1554a9b05723SYOSHIFUJI Hideaki break; 15557cbca67cSYOSHIFUJI Hideaki } 1556a9b05723SYOSHIFUJI Hideaki #endif 1557a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_OIF: 1558a9b05723SYOSHIFUJI Hideaki /* Rule 5: Prefer outgoing interface */ 1559a9b05723SYOSHIFUJI Hideaki ret = (!dst->ifindex || 1560a9b05723SYOSHIFUJI Hideaki dst->ifindex == score->ifa->idev->dev->ifindex); 1561a9b05723SYOSHIFUJI Hideaki break; 1562a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_LABEL: 1563a9b05723SYOSHIFUJI Hideaki /* Rule 6: Prefer matching label */ 15643de23255SBenjamin Thery ret = ipv6_addr_label(net, 15653de23255SBenjamin Thery &score->ifa->addr, score->addr_type, 1566a9b05723SYOSHIFUJI Hideaki score->ifa->idev->dev->ifindex) == dst->label; 1567a9b05723SYOSHIFUJI Hideaki break; 1568a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PRIVACY: 15697cbca67cSYOSHIFUJI Hideaki { 1570a9b05723SYOSHIFUJI Hideaki /* Rule 7: Prefer public address 157125985edcSLucas De Marchi * Note: prefer temporary address if use_tempaddr >= 2 1572a9b05723SYOSHIFUJI Hideaki */ 15737cbca67cSYOSHIFUJI Hideaki int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ? 15747cbca67cSYOSHIFUJI Hideaki !!(dst->prefs & IPV6_PREFER_SRC_TMP) : 15757cbca67cSYOSHIFUJI Hideaki score->ifa->idev->cnf.use_tempaddr >= 2; 15767cbca67cSYOSHIFUJI Hideaki ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp; 1577a9b05723SYOSHIFUJI Hideaki break; 15787cbca67cSYOSHIFUJI Hideaki } 1579a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_ORCHID: 1580a9b05723SYOSHIFUJI Hideaki /* Rule 8-: Prefer ORCHID vs ORCHID or 1581a9b05723SYOSHIFUJI Hideaki * non-ORCHID vs non-ORCHID 1582a9b05723SYOSHIFUJI Hideaki */ 1583a9b05723SYOSHIFUJI Hideaki ret = !(ipv6_addr_orchid(&score->ifa->addr) ^ 1584a9b05723SYOSHIFUJI Hideaki ipv6_addr_orchid(dst->addr)); 1585a9b05723SYOSHIFUJI Hideaki break; 1586a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFIX: 1587a9b05723SYOSHIFUJI Hideaki /* Rule 8: Use longest matching prefix */ 158891b4b04fSYOSHIFUJI Hideaki / 吉藤英明 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr); 158991b4b04fSYOSHIFUJI Hideaki / 吉藤英明 if (ret > score->ifa->prefix_len) 159091b4b04fSYOSHIFUJI Hideaki / 吉藤英明 ret = score->ifa->prefix_len; 159191b4b04fSYOSHIFUJI Hideaki / 吉藤英明 score->matchlen = ret; 1592a9b05723SYOSHIFUJI Hideaki break; 15937fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 15947fd2561eSErik Kline case IPV6_SADDR_RULE_NOT_OPTIMISTIC: 15957fd2561eSErik Kline /* Optimistic addresses still have lower precedence than other 15967fd2561eSErik Kline * preferred addresses. 15977fd2561eSErik Kline */ 15987fd2561eSErik Kline ret = !(score->ifa->flags & IFA_F_OPTIMISTIC); 15997fd2561eSErik Kline break; 16007fd2561eSErik Kline #endif 1601a9b05723SYOSHIFUJI Hideaki default: 1602a9b05723SYOSHIFUJI Hideaki ret = 0; 1603a9b05723SYOSHIFUJI Hideaki } 1604a9b05723SYOSHIFUJI Hideaki 1605a9b05723SYOSHIFUJI Hideaki if (ret) 1606a9b05723SYOSHIFUJI Hideaki __set_bit(i, score->scorebits); 1607a9b05723SYOSHIFUJI Hideaki score->rule = i; 1608a9b05723SYOSHIFUJI Hideaki out: 1609a9b05723SYOSHIFUJI Hideaki return ret; 1610a9b05723SYOSHIFUJI Hideaki } 1611a9b05723SYOSHIFUJI Hideaki 1612c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 static int __ipv6_dev_get_saddr(struct net *net, 16139131f3deSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_dst *dst, 16149131f3deSYOSHIFUJI Hideaki/吉藤英明 struct inet6_dev *idev, 1615c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_score *scores, 1616c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 int hiscore_idx) 16171da177e4SLinus Torvalds { 1618c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx]; 1619072047e4SYOSHIFUJI Hideaki 1620f59c031eSEric Dumazet list_for_each_entry_rcu(score->ifa, &idev->addr_list, if_list) { 1621a9b05723SYOSHIFUJI Hideaki int i; 1622072047e4SYOSHIFUJI Hideaki 1623a9b05723SYOSHIFUJI Hideaki /* 16246b3ae80aSYOSHIFUJI Hideaki * - Tentative Address (RFC2462 section 5.4) 16256b3ae80aSYOSHIFUJI Hideaki * - A tentative address is not considered 16266b3ae80aSYOSHIFUJI Hideaki * "assigned to an interface" in the traditional 162795c385b4SNeil Horman * sense, unless it is also flagged as optimistic. 16286b3ae80aSYOSHIFUJI Hideaki * - Candidate Source Address (section 4) 1629072047e4SYOSHIFUJI Hideaki * - In any case, anycast addresses, multicast 1630072047e4SYOSHIFUJI Hideaki * addresses, and the unspecified address MUST 1631072047e4SYOSHIFUJI Hideaki * NOT be included in a candidate set. 1632072047e4SYOSHIFUJI Hideaki */ 1633a9b05723SYOSHIFUJI Hideaki if ((score->ifa->flags & IFA_F_TENTATIVE) && 1634a9b05723SYOSHIFUJI Hideaki (!(score->ifa->flags & IFA_F_OPTIMISTIC))) 16356b3ae80aSYOSHIFUJI Hideaki continue; 1636a9b05723SYOSHIFUJI Hideaki 1637a9b05723SYOSHIFUJI Hideaki score->addr_type = __ipv6_addr_type(&score->ifa->addr); 1638a9b05723SYOSHIFUJI Hideaki 1639a9b05723SYOSHIFUJI Hideaki if (unlikely(score->addr_type == IPV6_ADDR_ANY || 1640a9b05723SYOSHIFUJI Hideaki score->addr_type & IPV6_ADDR_MULTICAST)) { 1641ba7a46f1SJoe Perches net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s", 16429131f3deSYOSHIFUJI Hideaki/吉藤英明 idev->dev->name); 16431da177e4SLinus Torvalds continue; 1644072047e4SYOSHIFUJI Hideaki } 1645072047e4SYOSHIFUJI Hideaki 1646a9b05723SYOSHIFUJI Hideaki score->rule = -1; 1647a9b05723SYOSHIFUJI Hideaki bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX); 1648072047e4SYOSHIFUJI Hideaki 1649a9b05723SYOSHIFUJI Hideaki for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) { 1650a9b05723SYOSHIFUJI Hideaki int minihiscore, miniscore; 1651072047e4SYOSHIFUJI Hideaki 16529131f3deSYOSHIFUJI Hideaki/吉藤英明 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i); 16539131f3deSYOSHIFUJI Hideaki/吉藤英明 miniscore = ipv6_get_saddr_eval(net, score, dst, i); 1654072047e4SYOSHIFUJI Hideaki 1655a9b05723SYOSHIFUJI Hideaki if (minihiscore > miniscore) { 1656a9b05723SYOSHIFUJI Hideaki if (i == IPV6_SADDR_RULE_SCOPE && 1657a9b05723SYOSHIFUJI Hideaki score->scopedist > 0) { 1658a9b05723SYOSHIFUJI Hideaki /* 1659a9b05723SYOSHIFUJI Hideaki * special case: 1660a9b05723SYOSHIFUJI Hideaki * each remaining entry 1661a9b05723SYOSHIFUJI Hideaki * has too small (not enough) 1662a9b05723SYOSHIFUJI Hideaki * scope, because ifa entries 1663a9b05723SYOSHIFUJI Hideaki * are sorted by their scope 1664a9b05723SYOSHIFUJI Hideaki * values. 1665072047e4SYOSHIFUJI Hideaki */ 16669131f3deSYOSHIFUJI Hideaki/吉藤英明 goto out; 1667072047e4SYOSHIFUJI Hideaki } 1668a9b05723SYOSHIFUJI Hideaki break; 1669a9b05723SYOSHIFUJI Hideaki } else if (minihiscore < miniscore) { 1670a0bffffcSIlpo Järvinen swap(hiscore, score); 1671c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 hiscore_idx = 1 - hiscore_idx; 1672a9b05723SYOSHIFUJI Hideaki 1673a9b05723SYOSHIFUJI Hideaki /* restore our iterator */ 1674a9b05723SYOSHIFUJI Hideaki score->ifa = hiscore->ifa; 1675a9b05723SYOSHIFUJI Hideaki 1676a9b05723SYOSHIFUJI Hideaki break; 16771da177e4SLinus Torvalds } 1678a9b05723SYOSHIFUJI Hideaki } 1679a9b05723SYOSHIFUJI Hideaki } 16809131f3deSYOSHIFUJI Hideaki/吉藤英明 out: 1681c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 return hiscore_idx; 16821da177e4SLinus Torvalds } 16839131f3deSYOSHIFUJI Hideaki/吉藤英明 1684afbac601SDavid Ahern static int ipv6_get_saddr_master(struct net *net, 1685afbac601SDavid Ahern const struct net_device *dst_dev, 1686afbac601SDavid Ahern const struct net_device *master, 1687afbac601SDavid Ahern struct ipv6_saddr_dst *dst, 1688afbac601SDavid Ahern struct ipv6_saddr_score *scores, 1689afbac601SDavid Ahern int hiscore_idx) 1690afbac601SDavid Ahern { 1691afbac601SDavid Ahern struct inet6_dev *idev; 1692afbac601SDavid Ahern 1693afbac601SDavid Ahern idev = __in6_dev_get(dst_dev); 1694afbac601SDavid Ahern if (idev) 1695afbac601SDavid Ahern hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev, 1696afbac601SDavid Ahern scores, hiscore_idx); 1697afbac601SDavid Ahern 1698afbac601SDavid Ahern idev = __in6_dev_get(master); 1699afbac601SDavid Ahern if (idev) 1700afbac601SDavid Ahern hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev, 1701afbac601SDavid Ahern scores, hiscore_idx); 1702afbac601SDavid Ahern 1703afbac601SDavid Ahern return hiscore_idx; 1704afbac601SDavid Ahern } 1705afbac601SDavid Ahern 17069131f3deSYOSHIFUJI Hideaki/吉藤英明 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev, 17079131f3deSYOSHIFUJI Hideaki/吉藤英明 const struct in6_addr *daddr, unsigned int prefs, 17089131f3deSYOSHIFUJI Hideaki/吉藤英明 struct in6_addr *saddr) 17099131f3deSYOSHIFUJI Hideaki/吉藤英明 { 1710c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_score scores[2], *hiscore; 17119131f3deSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_dst dst; 17129131f3deSYOSHIFUJI Hideaki/吉藤英明 struct inet6_dev *idev; 17139131f3deSYOSHIFUJI Hideaki/吉藤英明 struct net_device *dev; 17149131f3deSYOSHIFUJI Hideaki/吉藤英明 int dst_type; 17159131f3deSYOSHIFUJI Hideaki/吉藤英明 bool use_oif_addr = false; 1716c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 int hiscore_idx = 0; 1717cc429c8fSEric Dumazet int ret = 0; 17189131f3deSYOSHIFUJI Hideaki/吉藤英明 17199131f3deSYOSHIFUJI Hideaki/吉藤英明 dst_type = __ipv6_addr_type(daddr); 17209131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.addr = daddr; 17219131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.ifindex = dst_dev ? dst_dev->ifindex : 0; 17229131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.scope = __ipv6_addr_src_scope(dst_type); 17239131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex); 17249131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.prefs = prefs; 17259131f3deSYOSHIFUJI Hideaki/吉藤英明 1726c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 scores[hiscore_idx].rule = -1; 1727c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 scores[hiscore_idx].ifa = NULL; 17289131f3deSYOSHIFUJI Hideaki/吉藤英明 17299131f3deSYOSHIFUJI Hideaki/吉藤英明 rcu_read_lock(); 17309131f3deSYOSHIFUJI Hideaki/吉藤英明 17319131f3deSYOSHIFUJI Hideaki/吉藤英明 /* Candidate Source Address (section 4) 17329131f3deSYOSHIFUJI Hideaki/吉藤英明 * - multicast and link-local destination address, 17339131f3deSYOSHIFUJI Hideaki/吉藤英明 * the set of candidate source address MUST only 17349131f3deSYOSHIFUJI Hideaki/吉藤英明 * include addresses assigned to interfaces 17359131f3deSYOSHIFUJI Hideaki/吉藤英明 * belonging to the same link as the outgoing 17369131f3deSYOSHIFUJI Hideaki/吉藤英明 * interface. 17379131f3deSYOSHIFUJI Hideaki/吉藤英明 * (- For site-local destination addresses, the 17389131f3deSYOSHIFUJI Hideaki/吉藤英明 * set of candidate source addresses MUST only 17399131f3deSYOSHIFUJI Hideaki/吉藤英明 * include addresses assigned to interfaces 17409131f3deSYOSHIFUJI Hideaki/吉藤英明 * belonging to the same site as the outgoing 17419131f3deSYOSHIFUJI Hideaki/吉藤英明 * interface.) 17423985e8a3SErik Kline * - "It is RECOMMENDED that the candidate source addresses 17433985e8a3SErik Kline * be the set of unicast addresses assigned to the 17443985e8a3SErik Kline * interface that will be used to send to the destination 17453985e8a3SErik Kline * (the 'outgoing' interface)." (RFC 6724) 17469131f3deSYOSHIFUJI Hideaki/吉藤英明 */ 17479131f3deSYOSHIFUJI Hideaki/吉藤英明 if (dst_dev) { 17489131f3deSYOSHIFUJI Hideaki/吉藤英明 idev = __in6_dev_get(dst_dev); 17493985e8a3SErik Kline if ((dst_type & IPV6_ADDR_MULTICAST) || 17503985e8a3SErik Kline dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL || 17513985e8a3SErik Kline (idev && idev->cnf.use_oif_addrs_only)) { 17529131f3deSYOSHIFUJI Hideaki/吉藤英明 use_oif_addr = true; 17539131f3deSYOSHIFUJI Hideaki/吉藤英明 } 17549131f3deSYOSHIFUJI Hideaki/吉藤英明 } 17559131f3deSYOSHIFUJI Hideaki/吉藤英明 17569131f3deSYOSHIFUJI Hideaki/吉藤英明 if (use_oif_addr) { 1757c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 if (idev) 1758c15df306SYOSHIFUJI Hideaki hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx); 17599131f3deSYOSHIFUJI Hideaki/吉藤英明 } else { 1760afbac601SDavid Ahern const struct net_device *master; 1761afbac601SDavid Ahern int master_idx = 0; 1762afbac601SDavid Ahern 1763afbac601SDavid Ahern /* if dst_dev exists and is enslaved to an L3 device, then 1764afbac601SDavid Ahern * prefer addresses from dst_dev and then the master over 1765afbac601SDavid Ahern * any other enslaved devices in the L3 domain. 1766afbac601SDavid Ahern */ 1767afbac601SDavid Ahern master = l3mdev_master_dev_rcu(dst_dev); 1768afbac601SDavid Ahern if (master) { 1769afbac601SDavid Ahern master_idx = master->ifindex; 1770afbac601SDavid Ahern 1771afbac601SDavid Ahern hiscore_idx = ipv6_get_saddr_master(net, dst_dev, 1772afbac601SDavid Ahern master, &dst, 1773afbac601SDavid Ahern scores, hiscore_idx); 1774afbac601SDavid Ahern 1775afbac601SDavid Ahern if (scores[hiscore_idx].ifa) 1776afbac601SDavid Ahern goto out; 1777afbac601SDavid Ahern } 1778afbac601SDavid Ahern 17799131f3deSYOSHIFUJI Hideaki/吉藤英明 for_each_netdev_rcu(net, dev) { 1780afbac601SDavid Ahern /* only consider addresses on devices in the 1781afbac601SDavid Ahern * same L3 domain 1782afbac601SDavid Ahern */ 1783afbac601SDavid Ahern if (l3mdev_master_ifindex_rcu(dev) != master_idx) 1784afbac601SDavid Ahern continue; 17859131f3deSYOSHIFUJI Hideaki/吉藤英明 idev = __in6_dev_get(dev); 17869131f3deSYOSHIFUJI Hideaki/吉藤英明 if (!idev) 17879131f3deSYOSHIFUJI Hideaki/吉藤英明 continue; 1788c15df306SYOSHIFUJI Hideaki hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx); 17899131f3deSYOSHIFUJI Hideaki/吉藤英明 } 17909131f3deSYOSHIFUJI Hideaki/吉藤英明 } 1791afbac601SDavid Ahern 1792afbac601SDavid Ahern out: 1793c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 hiscore = &scores[hiscore_idx]; 1794a9b05723SYOSHIFUJI Hideaki if (!hiscore->ifa) 1795cc429c8fSEric Dumazet ret = -EADDRNOTAVAIL; 1796cc429c8fSEric Dumazet else 17974e3fd7a0SAlexey Dobriyan *saddr = hiscore->ifa->addr; 1798cc429c8fSEric Dumazet 1799cc429c8fSEric Dumazet rcu_read_unlock(); 1800cc429c8fSEric Dumazet return ret; 18011da177e4SLinus Torvalds } 18025e5f3f0fSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_dev_get_saddr); 18031da177e4SLinus Torvalds 18048965779dSAmerigo Wang int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr, 1805479840ffSJiri Pirko u32 banned_flags) 1806b7b1bfceSHannes Frederic Sowa { 1807b7b1bfceSHannes Frederic Sowa struct inet6_ifaddr *ifp; 1808b7b1bfceSHannes Frederic Sowa int err = -EADDRNOTAVAIL; 1809b7b1bfceSHannes Frederic Sowa 1810602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) { 1811602582caSHannes Frederic Sowa if (ifp->scope > IFA_LINK) 1812602582caSHannes Frederic Sowa break; 1813b7b1bfceSHannes Frederic Sowa if (ifp->scope == IFA_LINK && 1814b7b1bfceSHannes Frederic Sowa !(ifp->flags & banned_flags)) { 1815b7b1bfceSHannes Frederic Sowa *addr = ifp->addr; 1816b7b1bfceSHannes Frederic Sowa err = 0; 1817b7b1bfceSHannes Frederic Sowa break; 1818b7b1bfceSHannes Frederic Sowa } 1819b7b1bfceSHannes Frederic Sowa } 1820b7b1bfceSHannes Frederic Sowa return err; 1821b7b1bfceSHannes Frederic Sowa } 1822b7b1bfceSHannes Frederic Sowa 182395c385b4SNeil Horman int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr, 1824479840ffSJiri Pirko u32 banned_flags) 18251da177e4SLinus Torvalds { 18261da177e4SLinus Torvalds struct inet6_dev *idev; 18271da177e4SLinus Torvalds int err = -EADDRNOTAVAIL; 18281da177e4SLinus Torvalds 18298814c4b5SYOSHIFUJI Hideaki rcu_read_lock(); 1830e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 1831e21e8467SStephen Hemminger if (idev) { 18321da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1833b7b1bfceSHannes Frederic Sowa err = __ipv6_get_lladdr(idev, addr, banned_flags); 18341da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 18351da177e4SLinus Torvalds } 18368814c4b5SYOSHIFUJI Hideaki rcu_read_unlock(); 18371da177e4SLinus Torvalds return err; 18381da177e4SLinus Torvalds } 18391da177e4SLinus Torvalds 1840d9bf82c2SEric Dumazet static int ipv6_count_addresses(const struct inet6_dev *idev) 18411da177e4SLinus Torvalds { 1842d9bf82c2SEric Dumazet const struct inet6_ifaddr *ifp; 18431da177e4SLinus Torvalds int cnt = 0; 18441da177e4SLinus Torvalds 1845d9bf82c2SEric Dumazet rcu_read_lock(); 1846d9bf82c2SEric Dumazet list_for_each_entry_rcu(ifp, &idev->addr_list, if_list) 18471da177e4SLinus Torvalds cnt++; 1848d9bf82c2SEric Dumazet rcu_read_unlock(); 18491da177e4SLinus Torvalds return cnt; 18501da177e4SLinus Torvalds } 18511da177e4SLinus Torvalds 1852b71d1d42SEric Dumazet int ipv6_chk_addr(struct net *net, const struct in6_addr *addr, 18532a7851bfSFlorian Westphal const struct net_device *dev, int strict) 18541da177e4SLinus Torvalds { 1855232378e8SDavid Ahern return ipv6_chk_addr_and_flags(net, addr, dev, !dev, 1856232378e8SDavid Ahern strict, IFA_F_TENTATIVE); 1857c58da4c6SErik Kline } 1858c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr); 1859c58da4c6SErik Kline 18601893ff20SDavid Ahern /* device argument is used to find the L3 domain of interest. If 18611893ff20SDavid Ahern * skip_dev_check is set, then the ifp device is not checked against 18621893ff20SDavid Ahern * the passed in dev argument. So the 2 cases for addresses checks are: 18631893ff20SDavid Ahern * 1. does the address exist in the L3 domain that dev is part of 18641893ff20SDavid Ahern * (skip_dev_check = true), or 18651893ff20SDavid Ahern * 18661893ff20SDavid Ahern * 2. does the address exist on the specific device 18671893ff20SDavid Ahern * (skip_dev_check = false) 18681893ff20SDavid Ahern */ 1869c58da4c6SErik Kline int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr, 1870232378e8SDavid Ahern const struct net_device *dev, bool skip_dev_check, 1871232378e8SDavid Ahern int strict, u32 banned_flags) 1872c58da4c6SErik Kline { 18733f27fb23SEric Dumazet unsigned int hash = inet6_addr_hash(net, addr); 18741893ff20SDavid Ahern const struct net_device *l3mdev; 1875eedf042aSStephen Hemminger struct inet6_ifaddr *ifp; 1876c58da4c6SErik Kline u32 ifp_flags; 18771da177e4SLinus Torvalds 1878480318a0SEric Dumazet rcu_read_lock(); 1879232378e8SDavid Ahern 18801893ff20SDavid Ahern l3mdev = l3mdev_master_dev_rcu(dev); 1881232378e8SDavid Ahern if (skip_dev_check) 1882232378e8SDavid Ahern dev = NULL; 1883232378e8SDavid Ahern 1884b67bfe0dSSasha Levin hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) { 1885878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 1886bfeade08SDaniel Lezcano continue; 18871893ff20SDavid Ahern 18881893ff20SDavid Ahern if (l3mdev_master_dev_rcu(ifp->idev->dev) != l3mdev) 18891893ff20SDavid Ahern continue; 18901893ff20SDavid Ahern 1891c58da4c6SErik Kline /* Decouple optimistic from tentative for evaluation here. 1892c58da4c6SErik Kline * Ban optimistic addresses explicitly, when required. 1893c58da4c6SErik Kline */ 1894c58da4c6SErik Kline ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC) 1895c58da4c6SErik Kline ? (ifp->flags&~IFA_F_TENTATIVE) 1896c58da4c6SErik Kline : ifp->flags; 18971da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr) && 1898c58da4c6SErik Kline !(ifp_flags&banned_flags) && 189963159f29SIan Morris (!dev || ifp->idev->dev == dev || 1900eedf042aSStephen Hemminger !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) { 1901480318a0SEric Dumazet rcu_read_unlock(); 1902eedf042aSStephen Hemminger return 1; 1903eedf042aSStephen Hemminger } 1904eedf042aSStephen Hemminger } 19055c578aedSstephen hemminger 1906480318a0SEric Dumazet rcu_read_unlock(); 1907eedf042aSStephen Hemminger return 0; 19081da177e4SLinus Torvalds } 1909c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr_and_flags); 19107159039aSYOSHIFUJI Hideaki 19111da177e4SLinus Torvalds 19127df37ff3SCatalin\(ux\) M. BOIE /* Compares an address/prefix_len with addresses on device @dev. 19137df37ff3SCatalin\(ux\) M. BOIE * If one is found it returns true. 19147df37ff3SCatalin\(ux\) M. BOIE */ 19157df37ff3SCatalin\(ux\) M. BOIE bool ipv6_chk_custom_prefix(const struct in6_addr *addr, 19167df37ff3SCatalin\(ux\) M. BOIE const unsigned int prefix_len, struct net_device *dev) 19177df37ff3SCatalin\(ux\) M. BOIE { 191847e26941SEric Dumazet const struct inet6_ifaddr *ifa; 191947e26941SEric Dumazet const struct inet6_dev *idev; 19207df37ff3SCatalin\(ux\) M. BOIE bool ret = false; 19217df37ff3SCatalin\(ux\) M. BOIE 19227df37ff3SCatalin\(ux\) M. BOIE rcu_read_lock(); 19237df37ff3SCatalin\(ux\) M. BOIE idev = __in6_dev_get(dev); 19247df37ff3SCatalin\(ux\) M. BOIE if (idev) { 192547e26941SEric Dumazet list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) { 19267df37ff3SCatalin\(ux\) M. BOIE ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len); 19277df37ff3SCatalin\(ux\) M. BOIE if (ret) 19287df37ff3SCatalin\(ux\) M. BOIE break; 19297df37ff3SCatalin\(ux\) M. BOIE } 19307df37ff3SCatalin\(ux\) M. BOIE } 19317df37ff3SCatalin\(ux\) M. BOIE rcu_read_unlock(); 19327df37ff3SCatalin\(ux\) M. BOIE 19337df37ff3SCatalin\(ux\) M. BOIE return ret; 19347df37ff3SCatalin\(ux\) M. BOIE } 19357df37ff3SCatalin\(ux\) M. BOIE EXPORT_SYMBOL(ipv6_chk_custom_prefix); 19367df37ff3SCatalin\(ux\) M. BOIE 1937b71d1d42SEric Dumazet int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev) 193852eeeb84SYOSHIFUJI Hideaki { 193924ba333bSEric Dumazet const struct inet6_ifaddr *ifa; 194024ba333bSEric Dumazet const struct inet6_dev *idev; 194152eeeb84SYOSHIFUJI Hideaki int onlink; 194252eeeb84SYOSHIFUJI Hideaki 194352eeeb84SYOSHIFUJI Hideaki onlink = 0; 194452eeeb84SYOSHIFUJI Hideaki rcu_read_lock(); 194552eeeb84SYOSHIFUJI Hideaki idev = __in6_dev_get(dev); 194652eeeb84SYOSHIFUJI Hideaki if (idev) { 194724ba333bSEric Dumazet list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) { 194852eeeb84SYOSHIFUJI Hideaki onlink = ipv6_prefix_equal(addr, &ifa->addr, 194952eeeb84SYOSHIFUJI Hideaki ifa->prefix_len); 195052eeeb84SYOSHIFUJI Hideaki if (onlink) 195152eeeb84SYOSHIFUJI Hideaki break; 195252eeeb84SYOSHIFUJI Hideaki } 195352eeeb84SYOSHIFUJI Hideaki } 195452eeeb84SYOSHIFUJI Hideaki rcu_read_unlock(); 195552eeeb84SYOSHIFUJI Hideaki return onlink; 195652eeeb84SYOSHIFUJI Hideaki } 195752eeeb84SYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_prefix); 195852eeeb84SYOSHIFUJI Hideaki 19599acd9f3aSYOSHIFUJI Hideaki struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr, 19601cab3da6SDaniel Lezcano struct net_device *dev, int strict) 19611da177e4SLinus Torvalds { 19623f27fb23SEric Dumazet unsigned int hash = inet6_addr_hash(net, addr); 1963b79d1d54SDavid S. Miller struct inet6_ifaddr *ifp, *result = NULL; 19641da177e4SLinus Torvalds 196524f226daSEric Dumazet rcu_read_lock(); 196624f226daSEric Dumazet hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) { 1967878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 19681cab3da6SDaniel Lezcano continue; 19691da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr)) { 197063159f29SIan Morris if (!dev || ifp->idev->dev == dev || 19711da177e4SLinus Torvalds !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) { 1972b79d1d54SDavid S. Miller result = ifp; 19731da177e4SLinus Torvalds in6_ifa_hold(ifp); 19741da177e4SLinus Torvalds break; 19751da177e4SLinus Torvalds } 19761da177e4SLinus Torvalds } 19771da177e4SLinus Torvalds } 197824f226daSEric Dumazet rcu_read_unlock(); 19791da177e4SLinus Torvalds 1980b79d1d54SDavid S. Miller return result; 19811da177e4SLinus Torvalds } 19821da177e4SLinus Torvalds 19831da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */ 19841da177e4SLinus Torvalds 1985cc411d0bSBrian Haley static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed) 19861da177e4SLinus Torvalds { 19873d171f39SLubomir Rintel if (dad_failed) 19883d171f39SLubomir Rintel ifp->flags |= IFA_F_DADFAILED; 19893d171f39SLubomir Rintel 1990ec8add2aSSabrina Dubroca if (ifp->flags&IFA_F_TEMPORARY) { 19911da177e4SLinus Torvalds struct inet6_ifaddr *ifpub; 19921da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 19931da177e4SLinus Torvalds ifpub = ifp->ifpub; 19941da177e4SLinus Torvalds if (ifpub) { 19951da177e4SLinus Torvalds in6_ifa_hold(ifpub); 19961da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 1997fffcefe9SEric Dumazet ipv6_create_tempaddr(ifpub, ifp, true); 19981da177e4SLinus Torvalds in6_ifa_put(ifpub); 19991da177e4SLinus Torvalds } else { 20001da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 20011da177e4SLinus Torvalds } 20021da177e4SLinus Torvalds ipv6_del_addr(ifp); 2003ec8add2aSSabrina Dubroca } else if (ifp->flags&IFA_F_PERMANENT || !dad_failed) { 2004ec8add2aSSabrina Dubroca spin_lock_bh(&ifp->lock); 2005ec8add2aSSabrina Dubroca addrconf_del_dad_work(ifp); 2006ec8add2aSSabrina Dubroca ifp->flags |= IFA_F_TENTATIVE; 2007f1c02cfbSSabrina Dubroca if (dad_failed) 2008f1c02cfbSSabrina Dubroca ifp->flags &= ~IFA_F_OPTIMISTIC; 2009ec8add2aSSabrina Dubroca spin_unlock_bh(&ifp->lock); 2010ec8add2aSSabrina Dubroca if (dad_failed) 2011ec8add2aSSabrina Dubroca ipv6_ifa_notify(0, ifp); 2012ec8add2aSSabrina Dubroca in6_ifa_put(ifp); 2013c15b1ccaSHannes Frederic Sowa } else { 20141da177e4SLinus Torvalds ipv6_del_addr(ifp); 20151da177e4SLinus Torvalds } 2016c15b1ccaSHannes Frederic Sowa } 20171da177e4SLinus Torvalds 2018f2344a13SHerbert Xu static int addrconf_dad_end(struct inet6_ifaddr *ifp) 2019f2344a13SHerbert Xu { 2020f2344a13SHerbert Xu int err = -ENOENT; 2021f2344a13SHerbert Xu 20228e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 2023f2344a13SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DAD) { 2024f2344a13SHerbert Xu ifp->state = INET6_IFADDR_STATE_POSTDAD; 2025f2344a13SHerbert Xu err = 0; 2026f2344a13SHerbert Xu } 20278e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 2028f2344a13SHerbert Xu 2029f2344a13SHerbert Xu return err; 2030f2344a13SHerbert Xu } 2031f2344a13SHerbert Xu 2032da13c59bSVishwanath Pai void addrconf_dad_failure(struct sk_buff *skb, struct inet6_ifaddr *ifp) 20333c21edbdSYOSHIFUJI Hideaki { 20341b34be74SYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev; 20351855b7c3SHannes Frederic Sowa struct net *net = dev_net(ifp->idev->dev); 20369bdd8d40SBrian Haley 2037853dc2e0SUrsula Braun if (addrconf_dad_end(ifp)) { 2038853dc2e0SUrsula Braun in6_ifa_put(ifp); 2039f2344a13SHerbert Xu return; 2040853dc2e0SUrsula Braun } 2041f2344a13SHerbert Xu 2042da13c59bSVishwanath Pai net_info_ratelimited("%s: IPv6 duplicate address %pI6c used by %pM detected!\n", 2043da13c59bSVishwanath Pai ifp->idev->dev->name, &ifp->addr, eth_hdr(skb)->h_source); 20449bdd8d40SBrian Haley 20455f40ef77SHannes Frederic Sowa spin_lock_bh(&ifp->lock); 20461b34be74SYOSHIFUJI Hideaki 20475f40ef77SHannes Frederic Sowa if (ifp->flags & IFA_F_STABLE_PRIVACY) { 20485f40ef77SHannes Frederic Sowa struct in6_addr new_addr; 20495f40ef77SHannes Frederic Sowa struct inet6_ifaddr *ifp2; 20505f40ef77SHannes Frederic Sowa int retries = ifp->stable_privacy_retry + 1; 2051e6464b8cSDavid Ahern struct ifa6_config cfg = { 2052e6464b8cSDavid Ahern .pfx = &new_addr, 2053e6464b8cSDavid Ahern .plen = ifp->prefix_len, 2054e6464b8cSDavid Ahern .ifa_flags = ifp->flags, 2055e6464b8cSDavid Ahern .valid_lft = ifp->valid_lft, 2056e6464b8cSDavid Ahern .preferred_lft = ifp->prefered_lft, 2057e6464b8cSDavid Ahern .scope = ifp->scope, 2058e6464b8cSDavid Ahern }; 20595f40ef77SHannes Frederic Sowa 20601855b7c3SHannes Frederic Sowa if (retries > net->ipv6.sysctl.idgen_retries) { 20615f40ef77SHannes Frederic Sowa net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n", 20625f40ef77SHannes Frederic Sowa ifp->idev->dev->name); 20635f40ef77SHannes Frederic Sowa goto errdad; 20645f40ef77SHannes Frederic Sowa } 20655f40ef77SHannes Frederic Sowa 20665f40ef77SHannes Frederic Sowa new_addr = ifp->addr; 20675f40ef77SHannes Frederic Sowa if (ipv6_generate_stable_address(&new_addr, retries, 20685f40ef77SHannes Frederic Sowa idev)) 20695f40ef77SHannes Frederic Sowa goto errdad; 20705f40ef77SHannes Frederic Sowa 20715f40ef77SHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 20725f40ef77SHannes Frederic Sowa 20735f40ef77SHannes Frederic Sowa if (idev->cnf.max_addresses && 20745f40ef77SHannes Frederic Sowa ipv6_count_addresses(idev) >= 20755f40ef77SHannes Frederic Sowa idev->cnf.max_addresses) 20765f40ef77SHannes Frederic Sowa goto lock_errdad; 20775f40ef77SHannes Frederic Sowa 20785f40ef77SHannes Frederic Sowa net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n", 20795f40ef77SHannes Frederic Sowa ifp->idev->dev->name); 20805f40ef77SHannes Frederic Sowa 2081e6464b8cSDavid Ahern ifp2 = ipv6_add_addr(idev, &cfg, false, NULL); 20825f40ef77SHannes Frederic Sowa if (IS_ERR(ifp2)) 20835f40ef77SHannes Frederic Sowa goto lock_errdad; 20845f40ef77SHannes Frederic Sowa 20855f40ef77SHannes Frederic Sowa spin_lock_bh(&ifp2->lock); 20865f40ef77SHannes Frederic Sowa ifp2->stable_privacy_retry = retries; 20875f40ef77SHannes Frederic Sowa ifp2->state = INET6_IFADDR_STATE_PREDAD; 20885f40ef77SHannes Frederic Sowa spin_unlock_bh(&ifp2->lock); 20895f40ef77SHannes Frederic Sowa 20901855b7c3SHannes Frederic Sowa addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay); 20915f40ef77SHannes Frederic Sowa in6_ifa_put(ifp2); 20925f40ef77SHannes Frederic Sowa lock_errdad: 20935f40ef77SHannes Frederic Sowa spin_lock_bh(&ifp->lock); 20941b34be74SYOSHIFUJI Hideaki } 20951b34be74SYOSHIFUJI Hideaki 20965f40ef77SHannes Frederic Sowa errdad: 2097c15b1ccaSHannes Frederic Sowa /* transition from _POSTDAD to _ERRDAD */ 2098c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_ERRDAD; 20998e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 2100c15b1ccaSHannes Frederic Sowa 2101c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 0); 2102751eb6b6SWei Yongjun in6_ifa_put(ifp); 21033c21edbdSYOSHIFUJI Hideaki } 21041da177e4SLinus Torvalds 2105a9ed4a29SSabrina Dubroca /* Join to solicited addr multicast group. 2106a9ed4a29SSabrina Dubroca * caller must hold RTNL */ 2107b71d1d42SEric Dumazet void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr) 21081da177e4SLinus Torvalds { 21091da177e4SLinus Torvalds struct in6_addr maddr; 21101da177e4SLinus Torvalds 21111da177e4SLinus Torvalds if (dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 21121da177e4SLinus Torvalds return; 21131da177e4SLinus Torvalds 21141da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr); 21151da177e4SLinus Torvalds ipv6_dev_mc_inc(dev, &maddr); 21161da177e4SLinus Torvalds } 21171da177e4SLinus Torvalds 2118a9ed4a29SSabrina Dubroca /* caller must hold RTNL */ 2119b71d1d42SEric Dumazet void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr) 21201da177e4SLinus Torvalds { 21211da177e4SLinus Torvalds struct in6_addr maddr; 21221da177e4SLinus Torvalds 21231da177e4SLinus Torvalds if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 21241da177e4SLinus Torvalds return; 21251da177e4SLinus Torvalds 21261da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr); 21271da177e4SLinus Torvalds __ipv6_dev_mc_dec(idev, &maddr); 21281da177e4SLinus Torvalds } 21291da177e4SLinus Torvalds 2130a9ed4a29SSabrina Dubroca /* caller must hold RTNL */ 213120380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp) 21321da177e4SLinus Torvalds { 21331da177e4SLinus Torvalds struct in6_addr addr; 2134c15b1ccaSHannes Frederic Sowa 213588ad3149SHannes Frederic Sowa if (ifp->prefix_len >= 127) /* RFC 6164 */ 21362bda8a0cSBjørn Mork return; 21371da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 21381da177e4SLinus Torvalds if (ipv6_addr_any(&addr)) 21391da177e4SLinus Torvalds return; 2140013b4d90SWANG Cong __ipv6_dev_ac_inc(ifp->idev, &addr); 21411da177e4SLinus Torvalds } 21421da177e4SLinus Torvalds 2143a9ed4a29SSabrina Dubroca /* caller must hold RTNL */ 214420380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp) 21451da177e4SLinus Torvalds { 21461da177e4SLinus Torvalds struct in6_addr addr; 2147c15b1ccaSHannes Frederic Sowa 214888ad3149SHannes Frederic Sowa if (ifp->prefix_len >= 127) /* RFC 6164 */ 214932019e65SYOSHIFUJI Hideaki return; 21501da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 21511da177e4SLinus Torvalds if (ipv6_addr_any(&addr)) 21521da177e4SLinus Torvalds return; 21531da177e4SLinus Torvalds __ipv6_dev_ac_dec(ifp->idev, &addr); 21541da177e4SLinus Torvalds } 21551da177e4SLinus Torvalds 21568a7a4b47SAlexander Aring static int addrconf_ifid_6lowpan(u8 *eui, struct net_device *dev) 215706a4c1c5Salex.bluesman.smirnov@gmail.com { 21588a7a4b47SAlexander Aring switch (dev->addr_len) { 21598a7a4b47SAlexander Aring case ETH_ALEN: 21609dae2e03SLuiz Augusto von Dentz memcpy(eui, dev->dev_addr, 3); 21619dae2e03SLuiz Augusto von Dentz eui[3] = 0xFF; 21629dae2e03SLuiz Augusto von Dentz eui[4] = 0xFE; 21639dae2e03SLuiz Augusto von Dentz memcpy(eui + 5, dev->dev_addr + 3, 3); 21649dae2e03SLuiz Augusto von Dentz break; 21658a7a4b47SAlexander Aring case EUI64_ADDR_LEN: 21665241c2d7SAlexander Aring memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN); 21675e98a36eSYOSHIFUJI Hideaki / 吉藤英明 eui[0] ^= 2; 21688a7a4b47SAlexander Aring break; 21698a7a4b47SAlexander Aring default: 21708a7a4b47SAlexander Aring return -1; 21718a7a4b47SAlexander Aring } 21728a7a4b47SAlexander Aring 217306a4c1c5Salex.bluesman.smirnov@gmail.com return 0; 217406a4c1c5Salex.bluesman.smirnov@gmail.com } 217506a4c1c5Salex.bluesman.smirnov@gmail.com 2176cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev) 2177cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 { 2178cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 union fwnet_hwaddr *ha; 2179cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 2180cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 if (dev->addr_len != FWNET_ALEN) 2181cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return -1; 2182cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 2183cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 ha = (union fwnet_hwaddr *)dev->dev_addr; 2184cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 2185cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id)); 2186cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 eui[0] ^= 2; 2187cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return 0; 2188cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 } 2189cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 2190073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev) 2191073a8e0eSYOSHIFUJI Hideaki { 21921da177e4SLinus Torvalds /* XXX: inherit EUI-64 from other interface -- yoshfuji */ 21931da177e4SLinus Torvalds if (dev->addr_len != ARCNET_ALEN) 21941da177e4SLinus Torvalds return -1; 21951da177e4SLinus Torvalds memset(eui, 0, 7); 21961da177e4SLinus Torvalds eui[7] = *(u8 *)dev->dev_addr; 21971da177e4SLinus Torvalds return 0; 2198073a8e0eSYOSHIFUJI Hideaki } 2199073a8e0eSYOSHIFUJI Hideaki 2200073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev) 2201073a8e0eSYOSHIFUJI Hideaki { 22021da177e4SLinus Torvalds if (dev->addr_len != INFINIBAND_ALEN) 22031da177e4SLinus Torvalds return -1; 22041da177e4SLinus Torvalds memcpy(eui, dev->dev_addr + 12, 8); 22051da177e4SLinus Torvalds eui[0] |= 2; 22061da177e4SLinus Torvalds return 0; 22071da177e4SLinus Torvalds } 2208073a8e0eSYOSHIFUJI Hideaki 2209c61393eaSstephen hemminger static int __ipv6_isatap_ifid(u8 *eui, __be32 addr) 2210dfd982baSYOSHIFUJI Hideaki { 22119af28511SSascha Hlusiak if (addr == 0) 22129af28511SSascha Hlusiak return -1; 2213dfd982baSYOSHIFUJI Hideaki eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) || 2214dfd982baSYOSHIFUJI Hideaki ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) || 2215dfd982baSYOSHIFUJI Hideaki ipv4_is_private_172(addr) || ipv4_is_test_192(addr) || 2216dfd982baSYOSHIFUJI Hideaki ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) || 2217dfd982baSYOSHIFUJI Hideaki ipv4_is_test_198(addr) || ipv4_is_multicast(addr) || 2218dfd982baSYOSHIFUJI Hideaki ipv4_is_lbcast(addr)) ? 0x00 : 0x02; 2219dfd982baSYOSHIFUJI Hideaki eui[1] = 0; 2220dfd982baSYOSHIFUJI Hideaki eui[2] = 0x5E; 2221dfd982baSYOSHIFUJI Hideaki eui[3] = 0xFE; 2222dfd982baSYOSHIFUJI Hideaki memcpy(eui + 4, &addr, 4); 2223dfd982baSYOSHIFUJI Hideaki return 0; 2224dfd982baSYOSHIFUJI Hideaki } 2225dfd982baSYOSHIFUJI Hideaki 2226dfd982baSYOSHIFUJI Hideaki static int addrconf_ifid_sit(u8 *eui, struct net_device *dev) 2227dfd982baSYOSHIFUJI Hideaki { 2228dfd982baSYOSHIFUJI Hideaki if (dev->priv_flags & IFF_ISATAP) 2229dfd982baSYOSHIFUJI Hideaki return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); 2230dfd982baSYOSHIFUJI Hideaki return -1; 2231dfd982baSYOSHIFUJI Hideaki } 2232dfd982baSYOSHIFUJI Hideaki 2233aee80b54Sstephen hemminger static int addrconf_ifid_gre(u8 *eui, struct net_device *dev) 2234aee80b54Sstephen hemminger { 2235aee80b54Sstephen hemminger return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); 2236aee80b54Sstephen hemminger } 2237aee80b54Sstephen hemminger 2238e837735eSNicolas Dichtel static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev) 2239e837735eSNicolas Dichtel { 2240e837735eSNicolas Dichtel memcpy(eui, dev->perm_addr, 3); 2241e837735eSNicolas Dichtel memcpy(eui + 5, dev->perm_addr + 3, 3); 2242e837735eSNicolas Dichtel eui[3] = 0xFF; 2243e837735eSNicolas Dichtel eui[4] = 0xFE; 2244e837735eSNicolas Dichtel eui[0] ^= 2; 2245e837735eSNicolas Dichtel return 0; 2246e837735eSNicolas Dichtel } 2247e837735eSNicolas Dichtel 2248073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev) 2249073a8e0eSYOSHIFUJI Hideaki { 2250073a8e0eSYOSHIFUJI Hideaki switch (dev->type) { 2251073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ETHER: 2252073a8e0eSYOSHIFUJI Hideaki case ARPHRD_FDDI: 2253073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_eui48(eui, dev); 2254073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ARCNET: 2255073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_arcnet(eui, dev); 2256073a8e0eSYOSHIFUJI Hideaki case ARPHRD_INFINIBAND: 2257073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_infiniband(eui, dev); 2258c7dc89c0SFred L. Templin case ARPHRD_SIT: 2259dfd982baSYOSHIFUJI Hideaki return addrconf_ifid_sit(eui, dev); 2260aee80b54Sstephen hemminger case ARPHRD_IPGRE: 226145ce0fd1SFelix Jia case ARPHRD_TUNNEL: 2262aee80b54Sstephen hemminger return addrconf_ifid_gre(eui, dev); 2263e74bccb8SJukka Rissanen case ARPHRD_6LOWPAN: 22648a7a4b47SAlexander Aring return addrconf_ifid_6lowpan(eui, dev); 2265cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 case ARPHRD_IEEE1394: 2266cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return addrconf_ifid_ieee1394(eui, dev); 2267e837735eSNicolas Dichtel case ARPHRD_TUNNEL6: 226845ce0fd1SFelix Jia case ARPHRD_IP6GRE: 22699deb441cSSubash Abhinov Kasiviswanathan case ARPHRD_RAWIP: 2270e837735eSNicolas Dichtel return addrconf_ifid_ip6tnl(eui, dev); 2271073a8e0eSYOSHIFUJI Hideaki } 22721da177e4SLinus Torvalds return -1; 22731da177e4SLinus Torvalds } 22741da177e4SLinus Torvalds 22751da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev) 22761da177e4SLinus Torvalds { 22771da177e4SLinus Torvalds int err = -1; 22781da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 22791da177e4SLinus Torvalds 22801da177e4SLinus Torvalds read_lock_bh(&idev->lock); 2281602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) { 2282602582caSHannes Frederic Sowa if (ifp->scope > IFA_LINK) 2283602582caSHannes Frederic Sowa break; 22841da177e4SLinus Torvalds if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) { 22851da177e4SLinus Torvalds memcpy(eui, ifp->addr.s6_addr+8, 8); 22861da177e4SLinus Torvalds err = 0; 22871da177e4SLinus Torvalds break; 22881da177e4SLinus Torvalds } 22891da177e4SLinus Torvalds } 22901da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 22911da177e4SLinus Torvalds return err; 22921da177e4SLinus Torvalds } 22931da177e4SLinus Torvalds 22941da177e4SLinus Torvalds /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */ 22959d6280daSJiri Bohac static void ipv6_regen_rndid(struct inet6_dev *idev) 22961da177e4SLinus Torvalds { 22971da177e4SLinus Torvalds regen: 2298955189efSYOSHIFUJI Hideaki get_random_bytes(idev->rndid, sizeof(idev->rndid)); 22991da177e4SLinus Torvalds idev->rndid[0] &= ~0x02; 23001da177e4SLinus Torvalds 23011da177e4SLinus Torvalds /* 23021da177e4SLinus Torvalds * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>: 23031da177e4SLinus Torvalds * check if generated address is not inappropriate 23041da177e4SLinus Torvalds * 23051da177e4SLinus Torvalds * - Reserved subnet anycast (RFC 2526) 23061da177e4SLinus Torvalds * 11111101 11....11 1xxxxxxx 2307c7dc89c0SFred L. Templin * - ISATAP (RFC4214) 6.1 23081da177e4SLinus Torvalds * 00-00-5E-FE-xx-xx-xx-xx 23091da177e4SLinus Torvalds * - value 0 23101da177e4SLinus Torvalds * - XXX: already assigned to an address on the device 23111da177e4SLinus Torvalds */ 23121da177e4SLinus Torvalds if (idev->rndid[0] == 0xfd && 23131da177e4SLinus Torvalds (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff && 23141da177e4SLinus Torvalds (idev->rndid[7]&0x80)) 23151da177e4SLinus Torvalds goto regen; 23161da177e4SLinus Torvalds if ((idev->rndid[0]|idev->rndid[1]) == 0) { 23171da177e4SLinus Torvalds if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe) 23181da177e4SLinus Torvalds goto regen; 23191da177e4SLinus Torvalds if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00) 23201da177e4SLinus Torvalds goto regen; 23211da177e4SLinus Torvalds } 23221da177e4SLinus Torvalds } 23231da177e4SLinus Torvalds 23249d6280daSJiri Bohac static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) 23258e5e8f30SEldad Zack { 23261da177e4SLinus Torvalds if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0) 23279d6280daSJiri Bohac ipv6_regen_rndid(idev); 23281da177e4SLinus Torvalds } 23291da177e4SLinus Torvalds 23301da177e4SLinus Torvalds /* 23311da177e4SLinus Torvalds * Add prefix route. 23321da177e4SLinus Torvalds */ 23331da177e4SLinus Torvalds 23341da177e4SLinus Torvalds static void 23358308f3ffSDavid Ahern addrconf_prefix_route(struct in6_addr *pfx, int plen, u32 metric, 23368308f3ffSDavid Ahern struct net_device *dev, unsigned long expires, 23378308f3ffSDavid Ahern u32 flags, gfp_t gfp_flags) 23381da177e4SLinus Torvalds { 233986872cb5SThomas Graf struct fib6_config cfg = { 2340ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX, 23418308f3ffSDavid Ahern .fc_metric = metric ? : IP6_RT_PRIO_ADDRCONF, 234286872cb5SThomas Graf .fc_ifindex = dev->ifindex, 234386872cb5SThomas Graf .fc_expires = expires, 234486872cb5SThomas Graf .fc_dst_len = plen, 234586872cb5SThomas Graf .fc_flags = RTF_UP | flags, 2346c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 2347f410a1fbSStephen Hemminger .fc_protocol = RTPROT_KERNEL, 2348e8478e80SDavid Ahern .fc_type = RTN_UNICAST, 234986872cb5SThomas Graf }; 23501da177e4SLinus Torvalds 23514e3fd7a0SAlexey Dobriyan cfg.fc_dst = *pfx; 23521da177e4SLinus Torvalds 23531da177e4SLinus Torvalds /* Prevent useless cloning on PtP SIT. 23541da177e4SLinus Torvalds This thing is done here expecting that the whole 23551da177e4SLinus Torvalds class of non-broadcast devices need not cloning. 23561da177e4SLinus Torvalds */ 235707a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 23581da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT)) 235986872cb5SThomas Graf cfg.fc_flags |= RTF_NONEXTHOP; 23600be669bbSJoerg Roedel #endif 23611da177e4SLinus Torvalds 2362acb54e3cSDavid Ahern ip6_route_add(&cfg, gfp_flags, NULL); 23631da177e4SLinus Torvalds } 23641da177e4SLinus Torvalds 236514ef37b6SAndreas Hofmeister 23668d1c802bSDavid Ahern static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx, 236714ef37b6SAndreas Hofmeister int plen, 236814ef37b6SAndreas Hofmeister const struct net_device *dev, 236914ef37b6SAndreas Hofmeister u32 flags, u32 noflags) 237014ef37b6SAndreas Hofmeister { 237114ef37b6SAndreas Hofmeister struct fib6_node *fn; 23728d1c802bSDavid Ahern struct fib6_info *rt = NULL; 237314ef37b6SAndreas Hofmeister struct fib6_table *table; 2374ca254490SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX; 237514ef37b6SAndreas Hofmeister 2376ca254490SDavid Ahern table = fib6_get_table(dev_net(dev), tb_id); 237763159f29SIan Morris if (!table) 237814ef37b6SAndreas Hofmeister return NULL; 237914ef37b6SAndreas Hofmeister 238066f5d6ceSWei Wang rcu_read_lock(); 238138fbeeeeSWei Wang fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0, true); 238214ef37b6SAndreas Hofmeister if (!fn) 238314ef37b6SAndreas Hofmeister goto out; 23841f56a01fSMartin KaFai Lau 238566f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 23865e670d84SDavid Ahern if (rt->fib6_nh.nh_dev->ifindex != dev->ifindex) 238714ef37b6SAndreas Hofmeister continue; 238893c2fb25SDavid Ahern if ((rt->fib6_flags & flags) != flags) 238914ef37b6SAndreas Hofmeister continue; 239093c2fb25SDavid Ahern if ((rt->fib6_flags & noflags) != 0) 239114ef37b6SAndreas Hofmeister continue; 2392e873e4b9SWei Wang if (!fib6_info_hold_safe(rt)) 2393e873e4b9SWei Wang continue; 239414ef37b6SAndreas Hofmeister break; 239514ef37b6SAndreas Hofmeister } 239614ef37b6SAndreas Hofmeister out: 239766f5d6ceSWei Wang rcu_read_unlock(); 239814ef37b6SAndreas Hofmeister return rt; 239914ef37b6SAndreas Hofmeister } 240014ef37b6SAndreas Hofmeister 240114ef37b6SAndreas Hofmeister 24021da177e4SLinus Torvalds /* Create "default" multicast route to the interface */ 24031da177e4SLinus Torvalds 24041da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev) 24051da177e4SLinus Torvalds { 240686872cb5SThomas Graf struct fib6_config cfg = { 2407ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL, 240886872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 240986872cb5SThomas Graf .fc_ifindex = dev->ifindex, 241086872cb5SThomas Graf .fc_dst_len = 8, 241186872cb5SThomas Graf .fc_flags = RTF_UP, 2412e8478e80SDavid Ahern .fc_type = RTN_UNICAST, 2413c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 241486872cb5SThomas Graf }; 24151da177e4SLinus Torvalds 241686872cb5SThomas Graf ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0); 241786872cb5SThomas Graf 2418e500c6d3SCong Wang ip6_route_add(&cfg, GFP_KERNEL, NULL); 24191da177e4SLinus Torvalds } 24201da177e4SLinus Torvalds 24211da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev) 24221da177e4SLinus Torvalds { 24231da177e4SLinus Torvalds struct inet6_dev *idev; 24241da177e4SLinus Torvalds 24251da177e4SLinus Torvalds ASSERT_RTNL(); 24261da177e4SLinus Torvalds 2427e21e8467SStephen Hemminger idev = ipv6_find_idev(dev); 2428e21e8467SStephen Hemminger if (!idev) 242964e724f6SBrian Haley return ERR_PTR(-ENOBUFS); 243064e724f6SBrian Haley 243164e724f6SBrian Haley if (idev->cnf.disable_ipv6) 243264e724f6SBrian Haley return ERR_PTR(-EACCES); 24331da177e4SLinus Torvalds 24341da177e4SLinus Torvalds /* Add default multicast route */ 2435ba46ee4cSDavid Ahern if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev)) 24361da177e4SLinus Torvalds addrconf_add_mroute(dev); 24371da177e4SLinus Torvalds 24381da177e4SLinus Torvalds return idev; 24391da177e4SLinus Torvalds } 24401da177e4SLinus Torvalds 244153bd6749SJiri Pirko static void manage_tempaddrs(struct inet6_dev *idev, 244253bd6749SJiri Pirko struct inet6_ifaddr *ifp, 244353bd6749SJiri Pirko __u32 valid_lft, __u32 prefered_lft, 244453bd6749SJiri Pirko bool create, unsigned long now) 244553bd6749SJiri Pirko { 244653bd6749SJiri Pirko u32 flags; 244753bd6749SJiri Pirko struct inet6_ifaddr *ift; 244853bd6749SJiri Pirko 244953bd6749SJiri Pirko read_lock_bh(&idev->lock); 245053bd6749SJiri Pirko /* update all temporary addresses in the list */ 245153bd6749SJiri Pirko list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) { 245253bd6749SJiri Pirko int age, max_valid, max_prefered; 245353bd6749SJiri Pirko 245453bd6749SJiri Pirko if (ifp != ift->ifpub) 245553bd6749SJiri Pirko continue; 245653bd6749SJiri Pirko 245753bd6749SJiri Pirko /* RFC 4941 section 3.3: 245853bd6749SJiri Pirko * If a received option will extend the lifetime of a public 245953bd6749SJiri Pirko * address, the lifetimes of temporary addresses should 246053bd6749SJiri Pirko * be extended, subject to the overall constraint that no 246153bd6749SJiri Pirko * temporary addresses should ever remain "valid" or "preferred" 246253bd6749SJiri Pirko * for a time longer than (TEMP_VALID_LIFETIME) or 246353bd6749SJiri Pirko * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively. 246453bd6749SJiri Pirko */ 246553bd6749SJiri Pirko age = (now - ift->cstamp) / HZ; 246653bd6749SJiri Pirko max_valid = idev->cnf.temp_valid_lft - age; 246753bd6749SJiri Pirko if (max_valid < 0) 246853bd6749SJiri Pirko max_valid = 0; 246953bd6749SJiri Pirko 247053bd6749SJiri Pirko max_prefered = idev->cnf.temp_prefered_lft - 247176506a98SJiri Bohac idev->desync_factor - age; 247253bd6749SJiri Pirko if (max_prefered < 0) 247353bd6749SJiri Pirko max_prefered = 0; 247453bd6749SJiri Pirko 247553bd6749SJiri Pirko if (valid_lft > max_valid) 247653bd6749SJiri Pirko valid_lft = max_valid; 247753bd6749SJiri Pirko 247853bd6749SJiri Pirko if (prefered_lft > max_prefered) 247953bd6749SJiri Pirko prefered_lft = max_prefered; 248053bd6749SJiri Pirko 248153bd6749SJiri Pirko spin_lock(&ift->lock); 248253bd6749SJiri Pirko flags = ift->flags; 248353bd6749SJiri Pirko ift->valid_lft = valid_lft; 248453bd6749SJiri Pirko ift->prefered_lft = prefered_lft; 248553bd6749SJiri Pirko ift->tstamp = now; 248653bd6749SJiri Pirko if (prefered_lft > 0) 248753bd6749SJiri Pirko ift->flags &= ~IFA_F_DEPRECATED; 248853bd6749SJiri Pirko 248953bd6749SJiri Pirko spin_unlock(&ift->lock); 249053bd6749SJiri Pirko if (!(flags&IFA_F_TENTATIVE)) 249153bd6749SJiri Pirko ipv6_ifa_notify(0, ift); 249253bd6749SJiri Pirko } 249353bd6749SJiri Pirko 249453bd6749SJiri Pirko if ((create || list_empty(&idev->tempaddr_list)) && 249553bd6749SJiri Pirko idev->cnf.use_tempaddr > 0) { 249653bd6749SJiri Pirko /* When a new public address is created as described 249753bd6749SJiri Pirko * in [ADDRCONF], also create a new temporary address. 249853bd6749SJiri Pirko * Also create a temporary address if it's enabled but 249953bd6749SJiri Pirko * no temporary address currently exists. 250053bd6749SJiri Pirko */ 250153bd6749SJiri Pirko read_unlock_bh(&idev->lock); 2502fffcefe9SEric Dumazet ipv6_create_tempaddr(ifp, NULL, false); 250353bd6749SJiri Pirko } else { 250453bd6749SJiri Pirko read_unlock_bh(&idev->lock); 250553bd6749SJiri Pirko } 250653bd6749SJiri Pirko } 250753bd6749SJiri Pirko 2508cc9da6ccSBjørn Mork static bool is_addr_mode_generate_stable(struct inet6_dev *idev) 2509cc9da6ccSBjørn Mork { 2510d35a00b8SFelix Jia return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY || 2511d35a00b8SFelix Jia idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM; 2512cc9da6ccSBjørn Mork } 2513cc9da6ccSBjørn Mork 2514cc84b3c6SAlexander Aring int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev, 25154f672235SAlexander Aring const struct prefix_info *pinfo, 25164f672235SAlexander Aring struct inet6_dev *in6_dev, 2517cc84b3c6SAlexander Aring const struct in6_addr *addr, int addr_type, 2518cc84b3c6SAlexander Aring u32 addr_flags, bool sllao, bool tokenized, 25194f672235SAlexander Aring __u32 valid_lft, u32 prefered_lft) 25204f672235SAlexander Aring { 25214f672235SAlexander Aring struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1); 25224f672235SAlexander Aring int create = 0, update_lft = 0; 25234f672235SAlexander Aring 25244f672235SAlexander Aring if (!ifp && valid_lft) { 25254f672235SAlexander Aring int max_addresses = in6_dev->cnf.max_addresses; 2526e6464b8cSDavid Ahern struct ifa6_config cfg = { 2527e6464b8cSDavid Ahern .pfx = addr, 2528e6464b8cSDavid Ahern .plen = pinfo->prefix_len, 2529e6464b8cSDavid Ahern .ifa_flags = addr_flags, 2530e6464b8cSDavid Ahern .valid_lft = valid_lft, 2531e6464b8cSDavid Ahern .preferred_lft = prefered_lft, 2532e6464b8cSDavid Ahern .scope = addr_type & IPV6_ADDR_SCOPE_MASK, 2533e6464b8cSDavid Ahern }; 25344f672235SAlexander Aring 25354f672235SAlexander Aring #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 253635e015e1SMatteo Croce if ((net->ipv6.devconf_all->optimistic_dad || 253735e015e1SMatteo Croce in6_dev->cnf.optimistic_dad) && 25384f672235SAlexander Aring !net->ipv6.devconf_all->forwarding && sllao) 2539e6464b8cSDavid Ahern cfg.ifa_flags |= IFA_F_OPTIMISTIC; 25404f672235SAlexander Aring #endif 25414f672235SAlexander Aring 25424f672235SAlexander Aring /* Do not allow to create too much of autoconfigured 25434f672235SAlexander Aring * addresses; this would be too easy way to crash kernel. 25444f672235SAlexander Aring */ 25454f672235SAlexander Aring if (!max_addresses || 25464f672235SAlexander Aring ipv6_count_addresses(in6_dev) < max_addresses) 2547e6464b8cSDavid Ahern ifp = ipv6_add_addr(in6_dev, &cfg, false, NULL); 25484f672235SAlexander Aring 25494f672235SAlexander Aring if (IS_ERR_OR_NULL(ifp)) 25504f672235SAlexander Aring return -1; 25514f672235SAlexander Aring 25524f672235SAlexander Aring create = 1; 25534f672235SAlexander Aring spin_lock_bh(&ifp->lock); 25544f672235SAlexander Aring ifp->flags |= IFA_F_MANAGETEMPADDR; 25554f672235SAlexander Aring ifp->cstamp = jiffies; 25564f672235SAlexander Aring ifp->tokenized = tokenized; 25574f672235SAlexander Aring spin_unlock_bh(&ifp->lock); 25584f672235SAlexander Aring addrconf_dad_start(ifp); 25594f672235SAlexander Aring } 25604f672235SAlexander Aring 25614f672235SAlexander Aring if (ifp) { 25624f672235SAlexander Aring u32 flags; 25634f672235SAlexander Aring unsigned long now; 25644f672235SAlexander Aring u32 stored_lft; 25654f672235SAlexander Aring 25664f672235SAlexander Aring /* update lifetime (RFC2462 5.5.3 e) */ 25674f672235SAlexander Aring spin_lock_bh(&ifp->lock); 25684f672235SAlexander Aring now = jiffies; 25694f672235SAlexander Aring if (ifp->valid_lft > (now - ifp->tstamp) / HZ) 25704f672235SAlexander Aring stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ; 25714f672235SAlexander Aring else 25724f672235SAlexander Aring stored_lft = 0; 2573f85f94b8SLorenzo Bianconi if (!create && stored_lft) { 25744f672235SAlexander Aring const u32 minimum_lft = min_t(u32, 25754f672235SAlexander Aring stored_lft, MIN_VALID_LIFETIME); 25764f672235SAlexander Aring valid_lft = max(valid_lft, minimum_lft); 25774f672235SAlexander Aring 25784f672235SAlexander Aring /* RFC4862 Section 5.5.3e: 25794f672235SAlexander Aring * "Note that the preferred lifetime of the 25804f672235SAlexander Aring * corresponding address is always reset to 25814f672235SAlexander Aring * the Preferred Lifetime in the received 25824f672235SAlexander Aring * Prefix Information option, regardless of 25834f672235SAlexander Aring * whether the valid lifetime is also reset or 25844f672235SAlexander Aring * ignored." 25854f672235SAlexander Aring * 25864f672235SAlexander Aring * So we should always update prefered_lft here. 25874f672235SAlexander Aring */ 25884f672235SAlexander Aring update_lft = 1; 25894f672235SAlexander Aring } 25904f672235SAlexander Aring 25914f672235SAlexander Aring if (update_lft) { 25924f672235SAlexander Aring ifp->valid_lft = valid_lft; 25934f672235SAlexander Aring ifp->prefered_lft = prefered_lft; 25944f672235SAlexander Aring ifp->tstamp = now; 25954f672235SAlexander Aring flags = ifp->flags; 25964f672235SAlexander Aring ifp->flags &= ~IFA_F_DEPRECATED; 25974f672235SAlexander Aring spin_unlock_bh(&ifp->lock); 25984f672235SAlexander Aring 25994f672235SAlexander Aring if (!(flags&IFA_F_TENTATIVE)) 26004f672235SAlexander Aring ipv6_ifa_notify(0, ifp); 26014f672235SAlexander Aring } else 26024f672235SAlexander Aring spin_unlock_bh(&ifp->lock); 26034f672235SAlexander Aring 26044f672235SAlexander Aring manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft, 26054f672235SAlexander Aring create, now); 26064f672235SAlexander Aring 26074f672235SAlexander Aring in6_ifa_put(ifp); 26084f672235SAlexander Aring addrconf_verify(); 26094f672235SAlexander Aring } 26104f672235SAlexander Aring 26114f672235SAlexander Aring return 0; 26124f672235SAlexander Aring } 2613cc84b3c6SAlexander Aring EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr); 26144f672235SAlexander Aring 2615e6bff995SNeil Horman void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao) 26161da177e4SLinus Torvalds { 26171da177e4SLinus Torvalds struct prefix_info *pinfo; 26181da177e4SLinus Torvalds __u32 valid_lft; 26191da177e4SLinus Torvalds __u32 prefered_lft; 26204f672235SAlexander Aring int addr_type, err; 262164236f3fSHannes Frederic Sowa u32 addr_flags = 0; 26221da177e4SLinus Torvalds struct inet6_dev *in6_dev; 262356d417b1SBrian Haley struct net *net = dev_net(dev); 26241da177e4SLinus Torvalds 26251da177e4SLinus Torvalds pinfo = (struct prefix_info *) opt; 26261da177e4SLinus Torvalds 26271da177e4SLinus Torvalds if (len < sizeof(struct prefix_info)) { 2628e32ac250SJoe Perches netdev_dbg(dev, "addrconf: prefix option too short\n"); 26291da177e4SLinus Torvalds return; 26301da177e4SLinus Torvalds } 26311da177e4SLinus Torvalds 26321da177e4SLinus Torvalds /* 26331da177e4SLinus Torvalds * Validation checks ([ADDRCONF], page 19) 26341da177e4SLinus Torvalds */ 26351da177e4SLinus Torvalds 26361da177e4SLinus Torvalds addr_type = ipv6_addr_type(&pinfo->prefix); 26371da177e4SLinus Torvalds 26381da177e4SLinus Torvalds if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)) 26391da177e4SLinus Torvalds return; 26401da177e4SLinus Torvalds 26411da177e4SLinus Torvalds valid_lft = ntohl(pinfo->valid); 26421da177e4SLinus Torvalds prefered_lft = ntohl(pinfo->prefered); 26431da177e4SLinus Torvalds 26441da177e4SLinus Torvalds if (prefered_lft > valid_lft) { 2645e87cc472SJoe Perches net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n"); 26461da177e4SLinus Torvalds return; 26471da177e4SLinus Torvalds } 26481da177e4SLinus Torvalds 26491da177e4SLinus Torvalds in6_dev = in6_dev_get(dev); 26501da177e4SLinus Torvalds 265163159f29SIan Morris if (!in6_dev) { 2652e87cc472SJoe Perches net_dbg_ratelimited("addrconf: device %s not configured\n", 2653e87cc472SJoe Perches dev->name); 26541da177e4SLinus Torvalds return; 26551da177e4SLinus Torvalds } 26561da177e4SLinus Torvalds 26571da177e4SLinus Torvalds /* 26581da177e4SLinus Torvalds * Two things going on here: 26591da177e4SLinus Torvalds * 1) Add routes for on-link prefixes 26601da177e4SLinus Torvalds * 2) Configure prefixes with the auto flag set 26611da177e4SLinus Torvalds */ 26621da177e4SLinus Torvalds 26634bed72e4SYOSHIFUJI Hideaki if (pinfo->onlink) { 26648d1c802bSDavid Ahern struct fib6_info *rt; 26654bed72e4SYOSHIFUJI Hideaki unsigned long rt_expires; 26664bed72e4SYOSHIFUJI Hideaki 26671da177e4SLinus Torvalds /* Avoid arithmetic overflow. Really, we could 26686f704992SYOSHIFUJI Hideaki * save rt_expires in seconds, likely valid_lft, 26696f704992SYOSHIFUJI Hideaki * but it would require division in fib gc, that it 26706f704992SYOSHIFUJI Hideaki * not good. 26711da177e4SLinus Torvalds */ 26724bed72e4SYOSHIFUJI Hideaki if (HZ > USER_HZ) 26734bed72e4SYOSHIFUJI Hideaki rt_expires = addrconf_timeout_fixup(valid_lft, HZ); 26744bed72e4SYOSHIFUJI Hideaki else 26754bed72e4SYOSHIFUJI Hideaki rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ); 26763dd4bc68SYOSHIFUJI Hideaki 26774bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(rt_expires)) 26784bed72e4SYOSHIFUJI Hideaki rt_expires *= HZ; 26791da177e4SLinus Torvalds 268014ef37b6SAndreas Hofmeister rt = addrconf_get_prefix_route(&pinfo->prefix, 268114ef37b6SAndreas Hofmeister pinfo->prefix_len, 268214ef37b6SAndreas Hofmeister dev, 268314ef37b6SAndreas Hofmeister RTF_ADDRCONF | RTF_PREFIX_RT, 268414ef37b6SAndreas Hofmeister RTF_GATEWAY | RTF_DEFAULT); 26851da177e4SLinus Torvalds 268614ef37b6SAndreas Hofmeister if (rt) { 26876f704992SYOSHIFUJI Hideaki /* Autoconf prefix route */ 26881da177e4SLinus Torvalds if (valid_lft == 0) { 2689afb1d4b5SDavid Ahern ip6_del_rt(net, rt); 26901da177e4SLinus Torvalds rt = NULL; 26914bed72e4SYOSHIFUJI Hideaki } else if (addrconf_finite_timeout(rt_expires)) { 26926f704992SYOSHIFUJI Hideaki /* not infinity */ 269314895687SDavid Ahern fib6_set_expires(rt, jiffies + rt_expires); 26946f704992SYOSHIFUJI Hideaki } else { 269514895687SDavid Ahern fib6_clean_expires(rt); 26961da177e4SLinus Torvalds } 26971da177e4SLinus Torvalds } else if (valid_lft) { 26986f704992SYOSHIFUJI Hideaki clock_t expires = 0; 26994bed72e4SYOSHIFUJI Hideaki int flags = RTF_ADDRCONF | RTF_PREFIX_RT; 27004bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(rt_expires)) { 27016f704992SYOSHIFUJI Hideaki /* not infinity */ 27026f704992SYOSHIFUJI Hideaki flags |= RTF_EXPIRES; 27036f704992SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(rt_expires); 27046f704992SYOSHIFUJI Hideaki } 27051da177e4SLinus Torvalds addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len, 27068308f3ffSDavid Ahern 0, dev, expires, flags, 27078308f3ffSDavid Ahern GFP_ATOMIC); 27081da177e4SLinus Torvalds } 270993531c67SDavid Ahern fib6_info_release(rt); 27101da177e4SLinus Torvalds } 27111da177e4SLinus Torvalds 27121da177e4SLinus Torvalds /* Try to figure out our local address for this prefix */ 27131da177e4SLinus Torvalds 27141da177e4SLinus Torvalds if (pinfo->autoconf && in6_dev->cnf.autoconf) { 27151da177e4SLinus Torvalds struct in6_addr addr; 2716f997c55cSAlexander Aring bool tokenized = false, dev_addr_generated = false; 27171da177e4SLinus Torvalds 27181da177e4SLinus Torvalds if (pinfo->prefix_len == 64) { 27191da177e4SLinus Torvalds memcpy(&addr, &pinfo->prefix, 8); 2720f53adae4SDaniel Borkmann 2721f53adae4SDaniel Borkmann if (!ipv6_addr_any(&in6_dev->token)) { 2722f53adae4SDaniel Borkmann read_lock_bh(&in6_dev->lock); 2723f53adae4SDaniel Borkmann memcpy(addr.s6_addr + 8, 2724f53adae4SDaniel Borkmann in6_dev->token.s6_addr + 8, 8); 2725f53adae4SDaniel Borkmann read_unlock_bh(&in6_dev->lock); 2726617fe29dSDaniel Borkmann tokenized = true; 2727cc9da6ccSBjørn Mork } else if (is_addr_mode_generate_stable(in6_dev) && 2728622c81d5SHannes Frederic Sowa !ipv6_generate_stable_address(&addr, 0, 2729622c81d5SHannes Frederic Sowa in6_dev)) { 273064236f3fSHannes Frederic Sowa addr_flags |= IFA_F_STABLE_PRIVACY; 2731622c81d5SHannes Frederic Sowa goto ok; 2732f53adae4SDaniel Borkmann } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) && 27331da177e4SLinus Torvalds ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) { 27344f672235SAlexander Aring goto put; 2735f997c55cSAlexander Aring } else { 2736f997c55cSAlexander Aring dev_addr_generated = true; 27371da177e4SLinus Torvalds } 27381da177e4SLinus Torvalds goto ok; 27391da177e4SLinus Torvalds } 2740e87cc472SJoe Perches net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n", 27411da177e4SLinus Torvalds pinfo->prefix_len); 27424f672235SAlexander Aring goto put; 27431da177e4SLinus Torvalds 27441da177e4SLinus Torvalds ok: 27454f672235SAlexander Aring err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, 27464f672235SAlexander Aring &addr, addr_type, 27474f672235SAlexander Aring addr_flags, sllao, 27484f672235SAlexander Aring tokenized, valid_lft, 27498a226b2cSJiri Benc prefered_lft); 27504f672235SAlexander Aring if (err) 27514f672235SAlexander Aring goto put; 2752f997c55cSAlexander Aring 2753f997c55cSAlexander Aring /* Ignore error case here because previous prefix add addr was 2754f997c55cSAlexander Aring * successful which will be notified. 2755f997c55cSAlexander Aring */ 2756f997c55cSAlexander Aring ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr, 2757f997c55cSAlexander Aring addr_type, addr_flags, sllao, 2758f997c55cSAlexander Aring tokenized, valid_lft, 2759f997c55cSAlexander Aring prefered_lft, 2760f997c55cSAlexander Aring dev_addr_generated); 27611da177e4SLinus Torvalds } 27621da177e4SLinus Torvalds inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo); 27634f672235SAlexander Aring put: 27641da177e4SLinus Torvalds in6_dev_put(in6_dev); 27651da177e4SLinus Torvalds } 27661da177e4SLinus Torvalds 27671da177e4SLinus Torvalds /* 27681da177e4SLinus Torvalds * Set destination address. 27691da177e4SLinus Torvalds * Special case for SIT interfaces where we create a new "virtual" 27701da177e4SLinus Torvalds * device. 27711da177e4SLinus Torvalds */ 2772af284937SDaniel Lezcano int addrconf_set_dstaddr(struct net *net, void __user *arg) 27731da177e4SLinus Torvalds { 27741da177e4SLinus Torvalds struct in6_ifreq ireq; 27751da177e4SLinus Torvalds struct net_device *dev; 27761da177e4SLinus Torvalds int err = -EINVAL; 27771da177e4SLinus Torvalds 27781da177e4SLinus Torvalds rtnl_lock(); 27791da177e4SLinus Torvalds 27801da177e4SLinus Torvalds err = -EFAULT; 27811da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 27821da177e4SLinus Torvalds goto err_exit; 27831da177e4SLinus Torvalds 2784af284937SDaniel Lezcano dev = __dev_get_by_index(net, ireq.ifr6_ifindex); 27851da177e4SLinus Torvalds 27861da177e4SLinus Torvalds err = -ENODEV; 278763159f29SIan Morris if (!dev) 27881da177e4SLinus Torvalds goto err_exit; 27891da177e4SLinus Torvalds 279007a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 27911da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT) { 27925bc3eb7eSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 27931da177e4SLinus Torvalds struct ifreq ifr; 27941da177e4SLinus Torvalds struct ip_tunnel_parm p; 27951da177e4SLinus Torvalds 27961da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 27971da177e4SLinus Torvalds if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4)) 27981da177e4SLinus Torvalds goto err_exit; 27991da177e4SLinus Torvalds 28001da177e4SLinus Torvalds memset(&p, 0, sizeof(p)); 28011da177e4SLinus Torvalds p.iph.daddr = ireq.ifr6_addr.s6_addr32[3]; 28021da177e4SLinus Torvalds p.iph.saddr = 0; 28031da177e4SLinus Torvalds p.iph.version = 4; 28041da177e4SLinus Torvalds p.iph.ihl = 5; 28051da177e4SLinus Torvalds p.iph.protocol = IPPROTO_IPV6; 28061da177e4SLinus Torvalds p.iph.ttl = 64; 2807d20b3109SStephen Hemminger ifr.ifr_ifru.ifru_data = (__force void __user *)&p; 28081da177e4SLinus Torvalds 28095bc3eb7eSStephen Hemminger if (ops->ndo_do_ioctl) { 28105bc3eb7eSStephen Hemminger mm_segment_t oldfs = get_fs(); 28115bc3eb7eSStephen Hemminger 28125bc3eb7eSStephen Hemminger set_fs(KERNEL_DS); 28135bc3eb7eSStephen Hemminger err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL); 28141da177e4SLinus Torvalds set_fs(oldfs); 28155bc3eb7eSStephen Hemminger } else 28165bc3eb7eSStephen Hemminger err = -EOPNOTSUPP; 28171da177e4SLinus Torvalds 28181da177e4SLinus Torvalds if (err == 0) { 28191da177e4SLinus Torvalds err = -ENOBUFS; 2820af284937SDaniel Lezcano dev = __dev_get_by_name(net, p.name); 2821af284937SDaniel Lezcano if (!dev) 28221da177e4SLinus Torvalds goto err_exit; 28231da177e4SLinus Torvalds err = dev_open(dev); 28241da177e4SLinus Torvalds } 28251da177e4SLinus Torvalds } 28260be669bbSJoerg Roedel #endif 28271da177e4SLinus Torvalds 28281da177e4SLinus Torvalds err_exit: 28291da177e4SLinus Torvalds rtnl_unlock(); 28301da177e4SLinus Torvalds return err; 28311da177e4SLinus Torvalds } 28321da177e4SLinus Torvalds 283393a714d6SMadhu Challa static int ipv6_mc_config(struct sock *sk, bool join, 283493a714d6SMadhu Challa const struct in6_addr *addr, int ifindex) 283593a714d6SMadhu Challa { 283693a714d6SMadhu Challa int ret; 283793a714d6SMadhu Challa 283893a714d6SMadhu Challa ASSERT_RTNL(); 283993a714d6SMadhu Challa 284093a714d6SMadhu Challa lock_sock(sk); 284193a714d6SMadhu Challa if (join) 284254ff9ef3SMarcelo Ricardo Leitner ret = ipv6_sock_mc_join(sk, ifindex, addr); 284393a714d6SMadhu Challa else 284454ff9ef3SMarcelo Ricardo Leitner ret = ipv6_sock_mc_drop(sk, ifindex, addr); 284593a714d6SMadhu Challa release_sock(sk); 284693a714d6SMadhu Challa 284793a714d6SMadhu Challa return ret; 284893a714d6SMadhu Challa } 284993a714d6SMadhu Challa 28501da177e4SLinus Torvalds /* 28511da177e4SLinus Torvalds * Manual configuration of address on an interface 28521da177e4SLinus Torvalds */ 2853479840ffSJiri Pirko static int inet6_addr_add(struct net *net, int ifindex, 285419b1518cSDavid Ahern struct ifa6_config *cfg, 2855de95e047SDavid Ahern struct netlink_ext_ack *extack) 28561da177e4SLinus Torvalds { 28571da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 28581da177e4SLinus Torvalds struct inet6_dev *idev; 28591da177e4SLinus Torvalds struct net_device *dev; 286093a714d6SMadhu Challa unsigned long timeout; 286193a714d6SMadhu Challa clock_t expires; 28626f704992SYOSHIFUJI Hideaki u32 flags; 28631da177e4SLinus Torvalds 28641da177e4SLinus Torvalds ASSERT_RTNL(); 28651da177e4SLinus Torvalds 286619b1518cSDavid Ahern if (cfg->plen > 128) 286724ef0da7SThomas Graf return -EINVAL; 286824ef0da7SThomas Graf 28690778769dSNoriaki TAKAMIYA /* check the lifetime */ 287019b1518cSDavid Ahern if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft) 28710778769dSNoriaki TAKAMIYA return -EINVAL; 28720778769dSNoriaki TAKAMIYA 287319b1518cSDavid Ahern if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR && cfg->plen != 64) 287453bd6749SJiri Pirko return -EINVAL; 287553bd6749SJiri Pirko 2876af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifindex); 2877af284937SDaniel Lezcano if (!dev) 28781da177e4SLinus Torvalds return -ENODEV; 28791da177e4SLinus Torvalds 288064e724f6SBrian Haley idev = addrconf_add_dev(dev); 288164e724f6SBrian Haley if (IS_ERR(idev)) 288264e724f6SBrian Haley return PTR_ERR(idev); 28831da177e4SLinus Torvalds 288419b1518cSDavid Ahern if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) { 288593a714d6SMadhu Challa int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk, 288619b1518cSDavid Ahern true, cfg->pfx, ifindex); 288793a714d6SMadhu Challa 288893a714d6SMadhu Challa if (ret < 0) 288993a714d6SMadhu Challa return ret; 289093a714d6SMadhu Challa } 289193a714d6SMadhu Challa 289219b1518cSDavid Ahern cfg->scope = ipv6_addr_scope(cfg->pfx); 28931da177e4SLinus Torvalds 289419b1518cSDavid Ahern timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ); 28954bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 28964bed72e4SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(timeout * HZ); 289719b1518cSDavid Ahern cfg->valid_lft = timeout; 28986f704992SYOSHIFUJI Hideaki flags = RTF_EXPIRES; 28994bed72e4SYOSHIFUJI Hideaki } else { 29004bed72e4SYOSHIFUJI Hideaki expires = 0; 29014bed72e4SYOSHIFUJI Hideaki flags = 0; 290219b1518cSDavid Ahern cfg->ifa_flags |= IFA_F_PERMANENT; 29036f704992SYOSHIFUJI Hideaki } 29040778769dSNoriaki TAKAMIYA 290519b1518cSDavid Ahern timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ); 29064bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 29074bed72e4SYOSHIFUJI Hideaki if (timeout == 0) 290819b1518cSDavid Ahern cfg->ifa_flags |= IFA_F_DEPRECATED; 290919b1518cSDavid Ahern cfg->preferred_lft = timeout; 29104bed72e4SYOSHIFUJI Hideaki } 29110778769dSNoriaki TAKAMIYA 291219b1518cSDavid Ahern ifp = ipv6_add_addr(idev, cfg, true, extack); 29131da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 291419b1518cSDavid Ahern if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) { 29158308f3ffSDavid Ahern addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 29168308f3ffSDavid Ahern ifp->rt_priority, dev, expires, 29178308f3ffSDavid Ahern flags, GFP_KERNEL); 2918761aac73SThomas Haller } 2919761aac73SThomas Haller 2920a2d481b3SLorenzo Bianconi /* Send a netlink notification if DAD is enabled and 2921a2d481b3SLorenzo Bianconi * optimistic flag is not set 2922a2d481b3SLorenzo Bianconi */ 2923a2d481b3SLorenzo Bianconi if (!(ifp->flags & (IFA_F_OPTIMISTIC | IFA_F_NODAD))) 2924a2d481b3SLorenzo Bianconi ipv6_ifa_notify(0, ifp); 292595c385b4SNeil Horman /* 292695c385b4SNeil Horman * Note that section 3.1 of RFC 4429 indicates 292795c385b4SNeil Horman * that the Optimistic flag should not be set for 292895c385b4SNeil Horman * manually configured addresses 292995c385b4SNeil Horman */ 2930cf22f9a2SDavid S. Miller addrconf_dad_start(ifp); 293119b1518cSDavid Ahern if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR) 293219b1518cSDavid Ahern manage_tempaddrs(idev, ifp, cfg->valid_lft, 293319b1518cSDavid Ahern cfg->preferred_lft, true, jiffies); 29341da177e4SLinus Torvalds in6_ifa_put(ifp); 2935c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 29361da177e4SLinus Torvalds return 0; 293719b1518cSDavid Ahern } else if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) { 293819b1518cSDavid Ahern ipv6_mc_config(net->ipv6.mc_autojoin_sk, false, 293919b1518cSDavid Ahern cfg->pfx, ifindex); 29401da177e4SLinus Torvalds } 29411da177e4SLinus Torvalds 29421da177e4SLinus Torvalds return PTR_ERR(ifp); 29431da177e4SLinus Torvalds } 29441da177e4SLinus Torvalds 29456046d5b4SHeiner Kallweit static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags, 29466046d5b4SHeiner Kallweit const struct in6_addr *pfx, unsigned int plen) 29471da177e4SLinus Torvalds { 29481da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 29491da177e4SLinus Torvalds struct inet6_dev *idev; 29501da177e4SLinus Torvalds struct net_device *dev; 29511da177e4SLinus Torvalds 295224ef0da7SThomas Graf if (plen > 128) 295324ef0da7SThomas Graf return -EINVAL; 295424ef0da7SThomas Graf 2955af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifindex); 2956af284937SDaniel Lezcano if (!dev) 29571da177e4SLinus Torvalds return -ENODEV; 29581da177e4SLinus Torvalds 2959e5d08d71SIan Morris idev = __in6_dev_get(dev); 296063159f29SIan Morris if (!idev) 29611da177e4SLinus Torvalds return -ENXIO; 29621da177e4SLinus Torvalds 29631da177e4SLinus Torvalds read_lock_bh(&idev->lock); 2964502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 29651da177e4SLinus Torvalds if (ifp->prefix_len == plen && 29661da177e4SLinus Torvalds ipv6_addr_equal(pfx, &ifp->addr)) { 29671da177e4SLinus Torvalds in6_ifa_hold(ifp); 29681da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 29691da177e4SLinus Torvalds 29706046d5b4SHeiner Kallweit if (!(ifp->flags & IFA_F_TEMPORARY) && 29716046d5b4SHeiner Kallweit (ifa_flags & IFA_F_MANAGETEMPADDR)) 29726046d5b4SHeiner Kallweit manage_tempaddrs(idev, ifp, 0, 0, false, 29736046d5b4SHeiner Kallweit jiffies); 29741da177e4SLinus Torvalds ipv6_del_addr(ifp); 29756046d5b4SHeiner Kallweit addrconf_verify_rtnl(); 297693a714d6SMadhu Challa if (ipv6_addr_is_multicast(pfx)) { 297793a714d6SMadhu Challa ipv6_mc_config(net->ipv6.mc_autojoin_sk, 297893a714d6SMadhu Challa false, pfx, dev->ifindex); 297993a714d6SMadhu Challa } 29801da177e4SLinus Torvalds return 0; 29811da177e4SLinus Torvalds } 29821da177e4SLinus Torvalds } 29831da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 29841da177e4SLinus Torvalds return -EADDRNOTAVAIL; 29851da177e4SLinus Torvalds } 29861da177e4SLinus Torvalds 29871da177e4SLinus Torvalds 2988af284937SDaniel Lezcano int addrconf_add_ifaddr(struct net *net, void __user *arg) 29891da177e4SLinus Torvalds { 299019b1518cSDavid Ahern struct ifa6_config cfg = { 299119b1518cSDavid Ahern .ifa_flags = IFA_F_PERMANENT, 299219b1518cSDavid Ahern .preferred_lft = INFINITY_LIFE_TIME, 299319b1518cSDavid Ahern .valid_lft = INFINITY_LIFE_TIME, 299419b1518cSDavid Ahern }; 29951da177e4SLinus Torvalds struct in6_ifreq ireq; 29961da177e4SLinus Torvalds int err; 29971da177e4SLinus Torvalds 2998af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 29991da177e4SLinus Torvalds return -EPERM; 30001da177e4SLinus Torvalds 30011da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 30021da177e4SLinus Torvalds return -EFAULT; 30031da177e4SLinus Torvalds 300419b1518cSDavid Ahern cfg.pfx = &ireq.ifr6_addr; 300519b1518cSDavid Ahern cfg.plen = ireq.ifr6_prefixlen; 300619b1518cSDavid Ahern 30071da177e4SLinus Torvalds rtnl_lock(); 300819b1518cSDavid Ahern err = inet6_addr_add(net, ireq.ifr6_ifindex, &cfg, NULL); 30091da177e4SLinus Torvalds rtnl_unlock(); 30101da177e4SLinus Torvalds return err; 30111da177e4SLinus Torvalds } 30121da177e4SLinus Torvalds 3013af284937SDaniel Lezcano int addrconf_del_ifaddr(struct net *net, void __user *arg) 30141da177e4SLinus Torvalds { 30151da177e4SLinus Torvalds struct in6_ifreq ireq; 30161da177e4SLinus Torvalds int err; 30171da177e4SLinus Torvalds 3018af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 30191da177e4SLinus Torvalds return -EPERM; 30201da177e4SLinus Torvalds 30211da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 30221da177e4SLinus Torvalds return -EFAULT; 30231da177e4SLinus Torvalds 30241da177e4SLinus Torvalds rtnl_lock(); 30256046d5b4SHeiner Kallweit err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr, 3026af284937SDaniel Lezcano ireq.ifr6_prefixlen); 30271da177e4SLinus Torvalds rtnl_unlock(); 30281da177e4SLinus Torvalds return err; 30291da177e4SLinus Torvalds } 30301da177e4SLinus Torvalds 3031b5f348e5SIlpo Järvinen static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr, 3032b5f348e5SIlpo Järvinen int plen, int scope) 3033b5f348e5SIlpo Järvinen { 3034b5f348e5SIlpo Järvinen struct inet6_ifaddr *ifp; 3035e6464b8cSDavid Ahern struct ifa6_config cfg = { 3036e6464b8cSDavid Ahern .pfx = addr, 3037e6464b8cSDavid Ahern .plen = plen, 3038e6464b8cSDavid Ahern .ifa_flags = IFA_F_PERMANENT, 3039e6464b8cSDavid Ahern .valid_lft = INFINITY_LIFE_TIME, 3040e6464b8cSDavid Ahern .preferred_lft = INFINITY_LIFE_TIME, 3041e6464b8cSDavid Ahern .scope = scope 3042e6464b8cSDavid Ahern }; 3043b5f348e5SIlpo Järvinen 3044e6464b8cSDavid Ahern ifp = ipv6_add_addr(idev, &cfg, true, NULL); 3045b5f348e5SIlpo Järvinen if (!IS_ERR(ifp)) { 3046b5f348e5SIlpo Järvinen spin_lock_bh(&ifp->lock); 3047b5f348e5SIlpo Järvinen ifp->flags &= ~IFA_F_TENTATIVE; 3048b5f348e5SIlpo Järvinen spin_unlock_bh(&ifp->lock); 3049764d3be6SPaolo Abeni rt_genid_bump_ipv6(dev_net(idev->dev)); 3050b5f348e5SIlpo Järvinen ipv6_ifa_notify(RTM_NEWADDR, ifp); 3051b5f348e5SIlpo Järvinen in6_ifa_put(ifp); 3052b5f348e5SIlpo Järvinen } 3053b5f348e5SIlpo Järvinen } 3054b5f348e5SIlpo Järvinen 305507a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 30561da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev) 30571da177e4SLinus Torvalds { 30581da177e4SLinus Torvalds struct in6_addr addr; 30591da177e4SLinus Torvalds struct net_device *dev; 3060c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 3061929c9cf3SNicolas Dichtel int scope, plen; 3062f0e2acfaSNicolas Dichtel u32 pflags = 0; 30631da177e4SLinus Torvalds 30641da177e4SLinus Torvalds ASSERT_RTNL(); 30651da177e4SLinus Torvalds 30661da177e4SLinus Torvalds memset(&addr, 0, sizeof(struct in6_addr)); 30671da177e4SLinus Torvalds memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4); 30681da177e4SLinus Torvalds 30691da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) { 30701da177e4SLinus Torvalds addr.s6_addr32[0] = htonl(0xfe800000); 30711da177e4SLinus Torvalds scope = IFA_LINK; 3072929c9cf3SNicolas Dichtel plen = 64; 30731da177e4SLinus Torvalds } else { 30741da177e4SLinus Torvalds scope = IPV6_ADDR_COMPATv4; 3075929c9cf3SNicolas Dichtel plen = 96; 3076f0e2acfaSNicolas Dichtel pflags |= RTF_NONEXTHOP; 30771da177e4SLinus Torvalds } 30781da177e4SLinus Torvalds 30791da177e4SLinus Torvalds if (addr.s6_addr32[3]) { 3080929c9cf3SNicolas Dichtel add_addr(idev, &addr, plen, scope); 30818308f3ffSDavid Ahern addrconf_prefix_route(&addr, plen, 0, idev->dev, 0, pflags, 3082e500c6d3SCong Wang GFP_KERNEL); 30831da177e4SLinus Torvalds return; 30841da177e4SLinus Torvalds } 30851da177e4SLinus Torvalds 30866fda7350SBenjamin Thery for_each_netdev(net, dev) { 3087e5ed6399SHerbert Xu struct in_device *in_dev = __in_dev_get_rtnl(dev); 30881da177e4SLinus Torvalds if (in_dev && (dev->flags & IFF_UP)) { 30891da177e4SLinus Torvalds struct in_ifaddr *ifa; 30901da177e4SLinus Torvalds 30911da177e4SLinus Torvalds int flag = scope; 30921da177e4SLinus Torvalds 30931da177e4SLinus Torvalds for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) { 30941da177e4SLinus Torvalds 30951da177e4SLinus Torvalds addr.s6_addr32[3] = ifa->ifa_local; 30961da177e4SLinus Torvalds 30971da177e4SLinus Torvalds if (ifa->ifa_scope == RT_SCOPE_LINK) 30981da177e4SLinus Torvalds continue; 30991da177e4SLinus Torvalds if (ifa->ifa_scope >= RT_SCOPE_HOST) { 31001da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) 31011da177e4SLinus Torvalds continue; 31021da177e4SLinus Torvalds flag |= IFA_HOST; 31031da177e4SLinus Torvalds } 31041da177e4SLinus Torvalds 3105b5f348e5SIlpo Järvinen add_addr(idev, &addr, plen, flag); 31068308f3ffSDavid Ahern addrconf_prefix_route(&addr, plen, 0, idev->dev, 3107e500c6d3SCong Wang 0, pflags, GFP_KERNEL); 31081da177e4SLinus Torvalds } 31091da177e4SLinus Torvalds } 31101da177e4SLinus Torvalds } 31111da177e4SLinus Torvalds } 31120be669bbSJoerg Roedel #endif 31131da177e4SLinus Torvalds 31141da177e4SLinus Torvalds static void init_loopback(struct net_device *dev) 31151da177e4SLinus Torvalds { 31161da177e4SLinus Torvalds struct inet6_dev *idev; 31171da177e4SLinus Torvalds 31181da177e4SLinus Torvalds /* ::1 */ 31191da177e4SLinus Torvalds 31201da177e4SLinus Torvalds ASSERT_RTNL(); 31211da177e4SLinus Torvalds 3122e5d08d71SIan Morris idev = ipv6_find_idev(dev); 312363159f29SIan Morris if (!idev) { 312491df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 31251da177e4SLinus Torvalds return; 31261da177e4SLinus Torvalds } 31271da177e4SLinus Torvalds 3128b5f348e5SIlpo Järvinen add_addr(idev, &in6addr_loopback, 128, IFA_HOST); 31291da177e4SLinus Torvalds } 31301da177e4SLinus Torvalds 31312ad3ed59SAlexander Aring void addrconf_add_linklocal(struct inet6_dev *idev, 313264236f3fSHannes Frederic Sowa const struct in6_addr *addr, u32 flags) 31331da177e4SLinus Torvalds { 3134e6464b8cSDavid Ahern struct ifa6_config cfg = { 3135e6464b8cSDavid Ahern .pfx = addr, 3136e6464b8cSDavid Ahern .plen = 64, 3137e6464b8cSDavid Ahern .ifa_flags = flags | IFA_F_PERMANENT, 3138e6464b8cSDavid Ahern .valid_lft = INFINITY_LIFE_TIME, 3139e6464b8cSDavid Ahern .preferred_lft = INFINITY_LIFE_TIME, 3140e6464b8cSDavid Ahern .scope = IFA_LINK 3141e6464b8cSDavid Ahern }; 31421da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 31431da177e4SLinus Torvalds 314495c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 314535e015e1SMatteo Croce if ((dev_net(idev->dev)->ipv6.devconf_all->optimistic_dad || 314635e015e1SMatteo Croce idev->cnf.optimistic_dad) && 3147702beb87SDavid Miller !dev_net(idev->dev)->ipv6.devconf_all->forwarding) 3148e6464b8cSDavid Ahern cfg.ifa_flags |= IFA_F_OPTIMISTIC; 314995c385b4SNeil Horman #endif 315095c385b4SNeil Horman 3151e6464b8cSDavid Ahern ifp = ipv6_add_addr(idev, &cfg, true, NULL); 31521da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 31538308f3ffSDavid Ahern addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 0, idev->dev, 3154acb54e3cSDavid Ahern 0, 0, GFP_ATOMIC); 3155cf22f9a2SDavid S. Miller addrconf_dad_start(ifp); 31561da177e4SLinus Torvalds in6_ifa_put(ifp); 31571da177e4SLinus Torvalds } 31581da177e4SLinus Torvalds } 31592ad3ed59SAlexander Aring EXPORT_SYMBOL_GPL(addrconf_add_linklocal); 31601da177e4SLinus Torvalds 3161622c81d5SHannes Frederic Sowa static bool ipv6_reserved_interfaceid(struct in6_addr address) 3162622c81d5SHannes Frederic Sowa { 3163622c81d5SHannes Frederic Sowa if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0) 3164622c81d5SHannes Frederic Sowa return true; 3165622c81d5SHannes Frederic Sowa 3166622c81d5SHannes Frederic Sowa if (address.s6_addr32[2] == htonl(0x02005eff) && 3167622c81d5SHannes Frederic Sowa ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000))) 3168622c81d5SHannes Frederic Sowa return true; 3169622c81d5SHannes Frederic Sowa 3170622c81d5SHannes Frederic Sowa if (address.s6_addr32[2] == htonl(0xfdffffff) && 3171622c81d5SHannes Frederic Sowa ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80))) 3172622c81d5SHannes Frederic Sowa return true; 3173622c81d5SHannes Frederic Sowa 3174622c81d5SHannes Frederic Sowa return false; 3175622c81d5SHannes Frederic Sowa } 3176622c81d5SHannes Frederic Sowa 3177622c81d5SHannes Frederic Sowa static int ipv6_generate_stable_address(struct in6_addr *address, 3178622c81d5SHannes Frederic Sowa u8 dad_count, 3179622c81d5SHannes Frederic Sowa const struct inet6_dev *idev) 3180622c81d5SHannes Frederic Sowa { 3181622c81d5SHannes Frederic Sowa static DEFINE_SPINLOCK(lock); 3182622c81d5SHannes Frederic Sowa static __u32 digest[SHA_DIGEST_WORDS]; 3183622c81d5SHannes Frederic Sowa static __u32 workspace[SHA_WORKSPACE_WORDS]; 3184622c81d5SHannes Frederic Sowa 3185622c81d5SHannes Frederic Sowa static union { 3186622c81d5SHannes Frederic Sowa char __data[SHA_MESSAGE_BYTES]; 3187622c81d5SHannes Frederic Sowa struct { 3188622c81d5SHannes Frederic Sowa struct in6_addr secret; 3189ff40217eSHannes Frederic Sowa __be32 prefix[2]; 3190622c81d5SHannes Frederic Sowa unsigned char hwaddr[MAX_ADDR_LEN]; 3191622c81d5SHannes Frederic Sowa u8 dad_count; 3192622c81d5SHannes Frederic Sowa } __packed; 3193622c81d5SHannes Frederic Sowa } data; 3194622c81d5SHannes Frederic Sowa 3195622c81d5SHannes Frederic Sowa struct in6_addr secret; 3196622c81d5SHannes Frederic Sowa struct in6_addr temp; 3197622c81d5SHannes Frederic Sowa struct net *net = dev_net(idev->dev); 3198622c81d5SHannes Frederic Sowa 3199622c81d5SHannes Frederic Sowa BUILD_BUG_ON(sizeof(data.__data) != sizeof(data)); 3200622c81d5SHannes Frederic Sowa 3201622c81d5SHannes Frederic Sowa if (idev->cnf.stable_secret.initialized) 3202622c81d5SHannes Frederic Sowa secret = idev->cnf.stable_secret.secret; 3203622c81d5SHannes Frederic Sowa else if (net->ipv6.devconf_dflt->stable_secret.initialized) 3204622c81d5SHannes Frederic Sowa secret = net->ipv6.devconf_dflt->stable_secret.secret; 3205622c81d5SHannes Frederic Sowa else 3206622c81d5SHannes Frederic Sowa return -1; 3207622c81d5SHannes Frederic Sowa 3208622c81d5SHannes Frederic Sowa retry: 3209622c81d5SHannes Frederic Sowa spin_lock_bh(&lock); 3210622c81d5SHannes Frederic Sowa 3211622c81d5SHannes Frederic Sowa sha_init(digest); 3212622c81d5SHannes Frederic Sowa memset(&data, 0, sizeof(data)); 3213622c81d5SHannes Frederic Sowa memset(workspace, 0, sizeof(workspace)); 3214622c81d5SHannes Frederic Sowa memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len); 3215ff40217eSHannes Frederic Sowa data.prefix[0] = address->s6_addr32[0]; 3216ff40217eSHannes Frederic Sowa data.prefix[1] = address->s6_addr32[1]; 3217622c81d5SHannes Frederic Sowa data.secret = secret; 3218622c81d5SHannes Frederic Sowa data.dad_count = dad_count; 3219622c81d5SHannes Frederic Sowa 3220622c81d5SHannes Frederic Sowa sha_transform(digest, data.__data, workspace); 3221622c81d5SHannes Frederic Sowa 3222622c81d5SHannes Frederic Sowa temp = *address; 3223ff40217eSHannes Frederic Sowa temp.s6_addr32[2] = (__force __be32)digest[0]; 3224ff40217eSHannes Frederic Sowa temp.s6_addr32[3] = (__force __be32)digest[1]; 3225622c81d5SHannes Frederic Sowa 3226622c81d5SHannes Frederic Sowa spin_unlock_bh(&lock); 3227622c81d5SHannes Frederic Sowa 3228622c81d5SHannes Frederic Sowa if (ipv6_reserved_interfaceid(temp)) { 3229622c81d5SHannes Frederic Sowa dad_count++; 32301855b7c3SHannes Frederic Sowa if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries) 3231622c81d5SHannes Frederic Sowa return -1; 3232622c81d5SHannes Frederic Sowa goto retry; 3233622c81d5SHannes Frederic Sowa } 3234622c81d5SHannes Frederic Sowa 3235622c81d5SHannes Frederic Sowa *address = temp; 3236622c81d5SHannes Frederic Sowa return 0; 3237622c81d5SHannes Frederic Sowa } 3238622c81d5SHannes Frederic Sowa 3239cc9da6ccSBjørn Mork static void ipv6_gen_mode_random_init(struct inet6_dev *idev) 3240cc9da6ccSBjørn Mork { 3241cc9da6ccSBjørn Mork struct ipv6_stable_secret *s = &idev->cnf.stable_secret; 3242cc9da6ccSBjørn Mork 3243cc9da6ccSBjørn Mork if (s->initialized) 3244cc9da6ccSBjørn Mork return; 3245cc9da6ccSBjørn Mork s = &idev->cnf.stable_secret; 3246cc9da6ccSBjørn Mork get_random_bytes(&s->secret, sizeof(s->secret)); 3247cc9da6ccSBjørn Mork s->initialized = true; 3248cc9da6ccSBjørn Mork } 3249cc9da6ccSBjørn Mork 3250bc91b0f0SJiri Pirko static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route) 3251bc91b0f0SJiri Pirko { 3252bc91b0f0SJiri Pirko struct in6_addr addr; 3253bc91b0f0SJiri Pirko 3254ca254490SDavid Ahern /* no link local addresses on L3 master devices */ 3255ca254490SDavid Ahern if (netif_is_l3_master(idev->dev)) 3256ca254490SDavid Ahern return; 3257ca254490SDavid Ahern 3258bc91b0f0SJiri Pirko ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0); 3259622c81d5SHannes Frederic Sowa 3260d35a00b8SFelix Jia switch (idev->cnf.addr_gen_mode) { 3261cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_RANDOM: 3262cc9da6ccSBjørn Mork ipv6_gen_mode_random_init(idev); 3263cc9da6ccSBjørn Mork /* fallthrough */ 3264cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_STABLE_PRIVACY: 3265622c81d5SHannes Frederic Sowa if (!ipv6_generate_stable_address(&addr, 0, idev)) 326664236f3fSHannes Frederic Sowa addrconf_add_linklocal(idev, &addr, 326764236f3fSHannes Frederic Sowa IFA_F_STABLE_PRIVACY); 3268622c81d5SHannes Frederic Sowa else if (prefix_route) 32698308f3ffSDavid Ahern addrconf_prefix_route(&addr, 64, 0, idev->dev, 3270acb54e3cSDavid Ahern 0, 0, GFP_KERNEL); 3271cc9da6ccSBjørn Mork break; 3272cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_EUI64: 3273bc91b0f0SJiri Pirko /* addrconf_add_linklocal also adds a prefix_route and we 3274bc91b0f0SJiri Pirko * only need to care about prefix routes if ipv6_generate_eui64 3275bc91b0f0SJiri Pirko * couldn't generate one. 3276bc91b0f0SJiri Pirko */ 3277bc91b0f0SJiri Pirko if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0) 327864236f3fSHannes Frederic Sowa addrconf_add_linklocal(idev, &addr, 0); 3279bc91b0f0SJiri Pirko else if (prefix_route) 32808308f3ffSDavid Ahern addrconf_prefix_route(&addr, 64, 0, idev->dev, 328127b10608SDavid Ahern 0, 0, GFP_KERNEL); 3282cc9da6ccSBjørn Mork break; 3283cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_NONE: 3284cc9da6ccSBjørn Mork default: 3285cc9da6ccSBjørn Mork /* will not add any link local address */ 3286cc9da6ccSBjørn Mork break; 3287bc91b0f0SJiri Pirko } 3288bc91b0f0SJiri Pirko } 3289bc91b0f0SJiri Pirko 32901da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev) 32911da177e4SLinus Torvalds { 32921da177e4SLinus Torvalds struct inet6_dev *idev; 32931da177e4SLinus Torvalds 32941da177e4SLinus Torvalds ASSERT_RTNL(); 32951da177e4SLinus Torvalds 329674235a25SHerbert Xu if ((dev->type != ARPHRD_ETHER) && 329774235a25SHerbert Xu (dev->type != ARPHRD_FDDI) && 329874235a25SHerbert Xu (dev->type != ARPHRD_ARCNET) && 329906a4c1c5Salex.bluesman.smirnov@gmail.com (dev->type != ARPHRD_INFINIBAND) && 3300e837735eSNicolas Dichtel (dev->type != ARPHRD_IEEE1394) && 3301e74bccb8SJukka Rissanen (dev->type != ARPHRD_TUNNEL6) && 3302cc9da6ccSBjørn Mork (dev->type != ARPHRD_6LOWPAN) && 330345ce0fd1SFelix Jia (dev->type != ARPHRD_IP6GRE) && 330445ce0fd1SFelix Jia (dev->type != ARPHRD_IPGRE) && 330545ce0fd1SFelix Jia (dev->type != ARPHRD_TUNNEL) && 33069deb441cSSubash Abhinov Kasiviswanathan (dev->type != ARPHRD_NONE) && 33079deb441cSSubash Abhinov Kasiviswanathan (dev->type != ARPHRD_RAWIP)) { 330874235a25SHerbert Xu /* Alas, we support only Ethernet autoconfiguration. */ 330974235a25SHerbert Xu return; 331074235a25SHerbert Xu } 331174235a25SHerbert Xu 33121da177e4SLinus Torvalds idev = addrconf_add_dev(dev); 331364e724f6SBrian Haley if (IS_ERR(idev)) 33141da177e4SLinus Torvalds return; 33151da177e4SLinus Torvalds 3316cc9da6ccSBjørn Mork /* this device type has no EUI support */ 3317cc9da6ccSBjørn Mork if (dev->type == ARPHRD_NONE && 3318d35a00b8SFelix Jia idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64) 3319d35a00b8SFelix Jia idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM; 3320cc9da6ccSBjørn Mork 3321bc91b0f0SJiri Pirko addrconf_addr_gen(idev, false); 33221da177e4SLinus Torvalds } 33231da177e4SLinus Torvalds 332407a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 33251da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev) 33261da177e4SLinus Torvalds { 33271da177e4SLinus Torvalds struct inet6_dev *idev; 33281da177e4SLinus Torvalds 33291da177e4SLinus Torvalds ASSERT_RTNL(); 33301da177e4SLinus Torvalds 33311da177e4SLinus Torvalds /* 33321da177e4SLinus Torvalds * Configure the tunnel with one of our IPv4 33331da177e4SLinus Torvalds * addresses... we should configure all of 33341da177e4SLinus Torvalds * our v4 addrs in the tunnel 33351da177e4SLinus Torvalds */ 33361da177e4SLinus Torvalds 3337e5d08d71SIan Morris idev = ipv6_find_idev(dev); 333863159f29SIan Morris if (!idev) { 333991df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 33401da177e4SLinus Torvalds return; 33411da177e4SLinus Torvalds } 33421da177e4SLinus Torvalds 3343c7dc89c0SFred L. Templin if (dev->priv_flags & IFF_ISATAP) { 3344bc91b0f0SJiri Pirko addrconf_addr_gen(idev, false); 3345c7dc89c0SFred L. Templin return; 3346c7dc89c0SFred L. Templin } 3347c7dc89c0SFred L. Templin 33481da177e4SLinus Torvalds sit_add_v4_addrs(idev); 33491da177e4SLinus Torvalds 335062b54dd9SNicolas Dichtel if (dev->flags&IFF_POINTOPOINT) 33511da177e4SLinus Torvalds addrconf_add_mroute(dev); 33521da177e4SLinus Torvalds } 33530be669bbSJoerg Roedel #endif 33541da177e4SLinus Torvalds 335507a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE) 3356aee80b54Sstephen hemminger static void addrconf_gre_config(struct net_device *dev) 3357aee80b54Sstephen hemminger { 3358aee80b54Sstephen hemminger struct inet6_dev *idev; 3359aee80b54Sstephen hemminger 3360aee80b54Sstephen hemminger ASSERT_RTNL(); 3361aee80b54Sstephen hemminger 3362e5d08d71SIan Morris idev = ipv6_find_idev(dev); 336363159f29SIan Morris if (!idev) { 336491df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 3365aee80b54Sstephen hemminger return; 3366aee80b54Sstephen hemminger } 3367aee80b54Sstephen hemminger 3368bc91b0f0SJiri Pirko addrconf_addr_gen(idev, true); 3369d9e4ce65SHannes Frederic Sowa if (dev->flags & IFF_POINTOPOINT) 3370d9e4ce65SHannes Frederic Sowa addrconf_add_mroute(dev); 3371aee80b54Sstephen hemminger } 3372aee80b54Sstephen hemminger #endif 3373aee80b54Sstephen hemminger 3374afb1d4b5SDavid Ahern static int fixup_permanent_addr(struct net *net, 3375afb1d4b5SDavid Ahern struct inet6_dev *idev, 3376f1705ec1SDavid Ahern struct inet6_ifaddr *ifp) 3377f1705ec1SDavid Ahern { 337893c2fb25SDavid Ahern /* !fib6_node means the host route was removed from the 33798048ced9SDavid Ahern * FIB, for example, if 'lo' device is taken down. In that 33808048ced9SDavid Ahern * case regenerate the host route. 33818048ced9SDavid Ahern */ 338293c2fb25SDavid Ahern if (!ifp->rt || !ifp->rt->fib6_node) { 3383360a9887SDavid Ahern struct fib6_info *f6i, *prev; 3384f1705ec1SDavid Ahern 3385360a9887SDavid Ahern f6i = addrconf_f6i_alloc(net, idev, &ifp->addr, false, 3386acb54e3cSDavid Ahern GFP_ATOMIC); 3387360a9887SDavid Ahern if (IS_ERR(f6i)) 3388360a9887SDavid Ahern return PTR_ERR(f6i); 3389f1705ec1SDavid Ahern 33908048ced9SDavid Ahern /* ifp->rt can be accessed outside of rtnl */ 33918048ced9SDavid Ahern spin_lock(&ifp->lock); 33928048ced9SDavid Ahern prev = ifp->rt; 3393360a9887SDavid Ahern ifp->rt = f6i; 33948048ced9SDavid Ahern spin_unlock(&ifp->lock); 33958048ced9SDavid Ahern 339693531c67SDavid Ahern fib6_info_release(prev); 3397f1705ec1SDavid Ahern } 3398f1705ec1SDavid Ahern 3399f1705ec1SDavid Ahern if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) { 3400f1705ec1SDavid Ahern addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 34018308f3ffSDavid Ahern ifp->rt_priority, idev->dev, 0, 0, 34028308f3ffSDavid Ahern GFP_ATOMIC); 3403f1705ec1SDavid Ahern } 3404f1705ec1SDavid Ahern 34056d717134SDavid Ahern if (ifp->state == INET6_IFADDR_STATE_PREDAD) 3406f1705ec1SDavid Ahern addrconf_dad_start(ifp); 3407f1705ec1SDavid Ahern 3408f1705ec1SDavid Ahern return 0; 3409f1705ec1SDavid Ahern } 3410f1705ec1SDavid Ahern 3411afb1d4b5SDavid Ahern static void addrconf_permanent_addr(struct net *net, struct net_device *dev) 3412f1705ec1SDavid Ahern { 3413f1705ec1SDavid Ahern struct inet6_ifaddr *ifp, *tmp; 3414f1705ec1SDavid Ahern struct inet6_dev *idev; 3415f1705ec1SDavid Ahern 3416f1705ec1SDavid Ahern idev = __in6_dev_get(dev); 3417f1705ec1SDavid Ahern if (!idev) 3418f1705ec1SDavid Ahern return; 3419f1705ec1SDavid Ahern 3420f1705ec1SDavid Ahern write_lock_bh(&idev->lock); 3421f1705ec1SDavid Ahern 3422f1705ec1SDavid Ahern list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) { 3423f1705ec1SDavid Ahern if ((ifp->flags & IFA_F_PERMANENT) && 3424afb1d4b5SDavid Ahern fixup_permanent_addr(net, idev, ifp) < 0) { 3425f1705ec1SDavid Ahern write_unlock_bh(&idev->lock); 3426e669b869SEric Dumazet in6_ifa_hold(ifp); 3427f1705ec1SDavid Ahern ipv6_del_addr(ifp); 3428f1705ec1SDavid Ahern write_lock_bh(&idev->lock); 3429f1705ec1SDavid Ahern 3430f1705ec1SDavid Ahern net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n", 3431f1705ec1SDavid Ahern idev->dev->name, &ifp->addr); 3432f1705ec1SDavid Ahern } 3433f1705ec1SDavid Ahern } 3434f1705ec1SDavid Ahern 3435f1705ec1SDavid Ahern write_unlock_bh(&idev->lock); 3436f1705ec1SDavid Ahern } 3437f1705ec1SDavid Ahern 34381da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event, 3439351638e7SJiri Pirko void *ptr) 34401da177e4SLinus Torvalds { 3441351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 34424f7f34eaSDavid Ahern struct netdev_notifier_changeupper_info *info; 3443748e2d93SEric Dumazet struct inet6_dev *idev = __in6_dev_get(dev); 344460abc0beSWANG Cong struct net *net = dev_net(dev); 3445c5e33bddSYOSHIFUJI Hideaki int run_pending = 0; 3446b217d616SHerbert Xu int err; 34471da177e4SLinus Torvalds 34481da177e4SLinus Torvalds switch (event) { 344945ba9dd2SYOSHIFUJI Hideaki case NETDEV_REGISTER: 345074235a25SHerbert Xu if (!idev && dev->mtu >= IPV6_MIN_MTU) { 345145ba9dd2SYOSHIFUJI Hideaki idev = ipv6_add_dev(dev); 3452a317a2f1SWANG Cong if (IS_ERR(idev)) 3453a317a2f1SWANG Cong return notifier_from_errno(PTR_ERR(idev)); 345445ba9dd2SYOSHIFUJI Hideaki } 345545ba9dd2SYOSHIFUJI Hideaki break; 3456bcdd553fSStephen Hemminger 3457b7b0b1d2SAlexander Duyck case NETDEV_CHANGEMTU: 3458b7b0b1d2SAlexander Duyck /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */ 3459b7b0b1d2SAlexander Duyck if (dev->mtu < IPV6_MIN_MTU) { 346060abc0beSWANG Cong addrconf_ifdown(dev, dev != net->loopback_dev); 3461b7b0b1d2SAlexander Duyck break; 3462b7b0b1d2SAlexander Duyck } 3463b7b0b1d2SAlexander Duyck 3464b7b0b1d2SAlexander Duyck if (idev) { 3465b7b0b1d2SAlexander Duyck rt6_mtu_change(dev, dev->mtu); 3466b7b0b1d2SAlexander Duyck idev->cnf.mtu6 = dev->mtu; 3467b7b0b1d2SAlexander Duyck break; 3468b7b0b1d2SAlexander Duyck } 3469b7b0b1d2SAlexander Duyck 3470b7b0b1d2SAlexander Duyck /* allocate new idev */ 3471b7b0b1d2SAlexander Duyck idev = ipv6_add_dev(dev); 3472b7b0b1d2SAlexander Duyck if (IS_ERR(idev)) 3473b7b0b1d2SAlexander Duyck break; 3474b7b0b1d2SAlexander Duyck 3475b7b0b1d2SAlexander Duyck /* device is still not ready */ 3476b7b0b1d2SAlexander Duyck if (!(idev->if_flags & IF_READY)) 3477b7b0b1d2SAlexander Duyck break; 3478b7b0b1d2SAlexander Duyck 3479b7b0b1d2SAlexander Duyck run_pending = 1; 3480b7b0b1d2SAlexander Duyck 3481b7b0b1d2SAlexander Duyck /* fall through */ 3482b7b0b1d2SAlexander Duyck 34831da177e4SLinus Torvalds case NETDEV_UP: 34843c21edbdSYOSHIFUJI Hideaki case NETDEV_CHANGE: 3485c2edacf8SJay Vosburgh if (dev->flags & IFF_SLAVE) 3486c2edacf8SJay Vosburgh break; 3487c2edacf8SJay Vosburgh 34883ce62a84SWANG Cong if (idev && idev->cnf.disable_ipv6) 34893ce62a84SWANG Cong break; 34903ce62a84SWANG Cong 34913c21edbdSYOSHIFUJI Hideaki if (event == NETDEV_UP) { 349238bd10c4SDavid Ahern /* restore routes for permanent addresses */ 3493afb1d4b5SDavid Ahern addrconf_permanent_addr(net, dev); 349438bd10c4SDavid Ahern 34951f372c7bSMike Manning if (!addrconf_link_ready(dev)) { 34963c21edbdSYOSHIFUJI Hideaki /* device is not ready yet. */ 3497f3213831SJoe Perches pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n", 34983c21edbdSYOSHIFUJI Hideaki dev->name); 34993c21edbdSYOSHIFUJI Hideaki break; 35003c21edbdSYOSHIFUJI Hideaki } 350199081049SKristian Slavov 3502d31c7b8fSEvgeniy Polyakov if (!idev && dev->mtu >= IPV6_MIN_MTU) 3503d31c7b8fSEvgeniy Polyakov idev = ipv6_add_dev(dev); 3504d31c7b8fSEvgeniy Polyakov 3505a317a2f1SWANG Cong if (!IS_ERR_OR_NULL(idev)) { 350699081049SKristian Slavov idev->if_flags |= IF_READY; 3507e3ec6cfcSBenjamin Thery run_pending = 1; 3508e3ec6cfcSBenjamin Thery } 3509b7b0b1d2SAlexander Duyck } else if (event == NETDEV_CHANGE) { 35101f372c7bSMike Manning if (!addrconf_link_ready(dev)) { 35113c21edbdSYOSHIFUJI Hideaki /* device is still not ready. */ 351227c6fa73SIdo Schimmel rt6_sync_down_dev(dev, event); 35133c21edbdSYOSHIFUJI Hideaki break; 35143c21edbdSYOSHIFUJI Hideaki } 35153c21edbdSYOSHIFUJI Hideaki 35163c21edbdSYOSHIFUJI Hideaki if (idev) { 3517a088d1d7SLinus Lüssing if (idev->if_flags & IF_READY) { 3518a088d1d7SLinus Lüssing /* device is already configured - 3519a088d1d7SLinus Lüssing * but resend MLD reports, we might 3520a088d1d7SLinus Lüssing * have roamed and need to update 3521a088d1d7SLinus Lüssing * multicast snooping switches 3522a088d1d7SLinus Lüssing */ 3523a088d1d7SLinus Lüssing ipv6_mc_up(idev); 352427c6fa73SIdo Schimmel rt6_sync_up(dev, RTNH_F_LINKDOWN); 35253c21edbdSYOSHIFUJI Hideaki break; 3526a088d1d7SLinus Lüssing } 35273c21edbdSYOSHIFUJI Hideaki idev->if_flags |= IF_READY; 35283c21edbdSYOSHIFUJI Hideaki } 35293c21edbdSYOSHIFUJI Hideaki 3530f3213831SJoe Perches pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n", 35313c21edbdSYOSHIFUJI Hideaki dev->name); 35323c21edbdSYOSHIFUJI Hideaki 3533c5e33bddSYOSHIFUJI Hideaki run_pending = 1; 35343c21edbdSYOSHIFUJI Hideaki } 35353c21edbdSYOSHIFUJI Hideaki 35361da177e4SLinus Torvalds switch (dev->type) { 353707a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 35381da177e4SLinus Torvalds case ARPHRD_SIT: 35391da177e4SLinus Torvalds addrconf_sit_config(dev); 35401da177e4SLinus Torvalds break; 35410be669bbSJoerg Roedel #endif 354207a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE) 3543aee80b54Sstephen hemminger case ARPHRD_IPGRE: 3544aee80b54Sstephen hemminger addrconf_gre_config(dev); 3545aee80b54Sstephen hemminger break; 3546aee80b54Sstephen hemminger #endif 35471da177e4SLinus Torvalds case ARPHRD_LOOPBACK: 35481da177e4SLinus Torvalds init_loopback(dev); 35491da177e4SLinus Torvalds break; 35501da177e4SLinus Torvalds 35511da177e4SLinus Torvalds default: 35521da177e4SLinus Torvalds addrconf_dev_config(dev); 35531da177e4SLinus Torvalds break; 35543ff50b79SStephen Hemminger } 3555bcdd553fSStephen Hemminger 3556a317a2f1SWANG Cong if (!IS_ERR_OR_NULL(idev)) { 3557c5e33bddSYOSHIFUJI Hideaki if (run_pending) 3558c5e33bddSYOSHIFUJI Hideaki addrconf_dad_run(idev); 3559c5e33bddSYOSHIFUJI Hideaki 35602127d95aSIdo Schimmel /* Device has an address by now */ 35612127d95aSIdo Schimmel rt6_sync_up(dev, RTNH_F_DEAD); 35622127d95aSIdo Schimmel 3563bcdd553fSStephen Hemminger /* 3564bcdd553fSStephen Hemminger * If the MTU changed during the interface down, 3565bcdd553fSStephen Hemminger * when the interface up, the changed MTU must be 3566bcdd553fSStephen Hemminger * reflected in the idev as well as routers. 35671da177e4SLinus Torvalds */ 3568bcdd553fSStephen Hemminger if (idev->cnf.mtu6 != dev->mtu && 3569bcdd553fSStephen Hemminger dev->mtu >= IPV6_MIN_MTU) { 35701da177e4SLinus Torvalds rt6_mtu_change(dev, dev->mtu); 35711da177e4SLinus Torvalds idev->cnf.mtu6 = dev->mtu; 35721da177e4SLinus Torvalds } 35731da177e4SLinus Torvalds idev->tstamp = jiffies; 35741da177e4SLinus Torvalds inet6_ifinfo_notify(RTM_NEWLINK, idev); 3575bcdd553fSStephen Hemminger 3576bcdd553fSStephen Hemminger /* 3577bcdd553fSStephen Hemminger * If the changed mtu during down is lower than 3578bcdd553fSStephen Hemminger * IPV6_MIN_MTU stop IPv6 on this interface. 35791da177e4SLinus Torvalds */ 35801da177e4SLinus Torvalds if (dev->mtu < IPV6_MIN_MTU) 358160abc0beSWANG Cong addrconf_ifdown(dev, dev != net->loopback_dev); 35821da177e4SLinus Torvalds } 35831da177e4SLinus Torvalds break; 35841da177e4SLinus Torvalds 35851da177e4SLinus Torvalds case NETDEV_DOWN: 35861da177e4SLinus Torvalds case NETDEV_UNREGISTER: 35871da177e4SLinus Torvalds /* 35881da177e4SLinus Torvalds * Remove all addresses from this interface. 35891da177e4SLinus Torvalds */ 35901da177e4SLinus Torvalds addrconf_ifdown(dev, event != NETDEV_DOWN); 35911da177e4SLinus Torvalds break; 35923c21edbdSYOSHIFUJI Hideaki 35931da177e4SLinus Torvalds case NETDEV_CHANGENAME: 35941da177e4SLinus Torvalds if (idev) { 35955632c515SStephen Hemminger snmp6_unregister_dev(idev); 3596408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 3597a317a2f1SWANG Cong err = addrconf_sysctl_register(idev); 3598b217d616SHerbert Xu if (err) 3599b217d616SHerbert Xu return notifier_from_errno(err); 3600a317a2f1SWANG Cong err = snmp6_register_dev(idev); 3601a317a2f1SWANG Cong if (err) { 3602a317a2f1SWANG Cong addrconf_sysctl_unregister(idev); 3603a317a2f1SWANG Cong return notifier_from_errno(err); 3604a317a2f1SWANG Cong } 36055632c515SStephen Hemminger } 36061da177e4SLinus Torvalds break; 3607bcdd553fSStephen Hemminger 360893d9b7d7SJiri Pirko case NETDEV_PRE_TYPE_CHANGE: 360993d9b7d7SJiri Pirko case NETDEV_POST_TYPE_CHANGE: 36103ef0952cSAndrew Lunn if (idev) 361193d9b7d7SJiri Pirko addrconf_type_change(dev, event); 361275c78500SMoni Shoua break; 36134f7f34eaSDavid Ahern 36144f7f34eaSDavid Ahern case NETDEV_CHANGEUPPER: 36154f7f34eaSDavid Ahern info = ptr; 36164f7f34eaSDavid Ahern 36174f7f34eaSDavid Ahern /* flush all routes if dev is linked to or unlinked from 36184f7f34eaSDavid Ahern * an L3 master device (e.g., VRF) 36194f7f34eaSDavid Ahern */ 36204f7f34eaSDavid Ahern if (info->upper_dev && netif_is_l3_master(info->upper_dev)) 36214f7f34eaSDavid Ahern addrconf_ifdown(dev, 0); 36223ff50b79SStephen Hemminger } 36231da177e4SLinus Torvalds 36241da177e4SLinus Torvalds return NOTIFY_OK; 36251da177e4SLinus Torvalds } 36261da177e4SLinus Torvalds 36271da177e4SLinus Torvalds /* 36281da177e4SLinus Torvalds * addrconf module should be notified of a device going up 36291da177e4SLinus Torvalds */ 36301da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = { 36311da177e4SLinus Torvalds .notifier_call = addrconf_notify, 3632242d3a49SWANG Cong .priority = ADDRCONF_NOTIFY_PRIORITY, 36331da177e4SLinus Torvalds }; 36341da177e4SLinus Torvalds 363593d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, unsigned long event) 363675c78500SMoni Shoua { 363775c78500SMoni Shoua struct inet6_dev *idev; 363875c78500SMoni Shoua ASSERT_RTNL(); 363975c78500SMoni Shoua 364075c78500SMoni Shoua idev = __in6_dev_get(dev); 364175c78500SMoni Shoua 364293d9b7d7SJiri Pirko if (event == NETDEV_POST_TYPE_CHANGE) 364375c78500SMoni Shoua ipv6_mc_remap(idev); 364493d9b7d7SJiri Pirko else if (event == NETDEV_PRE_TYPE_CHANGE) 364575c78500SMoni Shoua ipv6_mc_unmap(idev); 364675c78500SMoni Shoua } 364775c78500SMoni Shoua 364870af921dSDavid Ahern static bool addr_is_local(const struct in6_addr *addr) 364970af921dSDavid Ahern { 365070af921dSDavid Ahern return ipv6_addr_type(addr) & 365170af921dSDavid Ahern (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK); 365270af921dSDavid Ahern } 365370af921dSDavid Ahern 36541da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how) 36551da177e4SLinus Torvalds { 36564c981e28SIdo Schimmel unsigned long event = how ? NETDEV_UNREGISTER : NETDEV_DOWN; 3657c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 3658502a2ffdSstephen hemminger struct inet6_dev *idev; 3659f1705ec1SDavid Ahern struct inet6_ifaddr *ifa, *tmp; 36600aef78aaSIvan Vecera bool keep_addr = false; 366173a8bd74SDavid S. Miller int state, i; 36621da177e4SLinus Torvalds 36631da177e4SLinus Torvalds ASSERT_RTNL(); 36641da177e4SLinus Torvalds 36654c981e28SIdo Schimmel rt6_disable_ip(dev, event); 36661da177e4SLinus Torvalds 36671da177e4SLinus Torvalds idev = __in6_dev_get(dev); 366863159f29SIan Morris if (!idev) 36691da177e4SLinus Torvalds return -ENODEV; 36701da177e4SLinus Torvalds 3671bcdd553fSStephen Hemminger /* 3672bcdd553fSStephen Hemminger * Step 1: remove reference to ipv6 device from parent device. 3673bcdd553fSStephen Hemminger * Do not dev_put! 36741da177e4SLinus Torvalds */ 3675439e2385SDenis V. Lunev if (how) { 36761da177e4SLinus Torvalds idev->dead = 1; 36778814c4b5SYOSHIFUJI Hideaki 36788814c4b5SYOSHIFUJI Hideaki /* protected by rtnl_lock */ 3679a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(dev->ip6_ptr, NULL); 36801da177e4SLinus Torvalds 36811da177e4SLinus Torvalds /* Step 1.5: remove snmp6 entry */ 36821da177e4SLinus Torvalds snmp6_unregister_dev(idev); 36831da177e4SLinus Torvalds 36841da177e4SLinus Torvalds } 36851da177e4SLinus Torvalds 3686f1705ec1SDavid Ahern /* combine the user config with event to determine if permanent 3687f1705ec1SDavid Ahern * addresses are to be removed from address hash table 3688f1705ec1SDavid Ahern */ 36890aef78aaSIvan Vecera if (!how && !idev->cnf.disable_ipv6) { 36900aef78aaSIvan Vecera /* aggregate the system setting and interface setting */ 36910aef78aaSIvan Vecera int _keep_addr = net->ipv6.devconf_all->keep_addr_on_down; 36920aef78aaSIvan Vecera 36930aef78aaSIvan Vecera if (!_keep_addr) 36940aef78aaSIvan Vecera _keep_addr = idev->cnf.keep_addr_on_down; 36950aef78aaSIvan Vecera 36960aef78aaSIvan Vecera keep_addr = (_keep_addr > 0); 36970aef78aaSIvan Vecera } 3698f1705ec1SDavid Ahern 369973a8bd74SDavid S. Miller /* Step 2: clear hash table */ 370073a8bd74SDavid S. Miller for (i = 0; i < IN6_ADDR_HSIZE; i++) { 370173a8bd74SDavid S. Miller struct hlist_head *h = &inet6_addr_lst[i]; 370273a8bd74SDavid S. Miller 370373a8bd74SDavid S. Miller spin_lock_bh(&addrconf_hash_lock); 370473a8bd74SDavid S. Miller restart: 3705b67bfe0dSSasha Levin hlist_for_each_entry_rcu(ifa, h, addr_lst) { 370673a8bd74SDavid S. Miller if (ifa->idev == idev) { 3707c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifa); 3708f1705ec1SDavid Ahern /* combined flag + permanent flag decide if 3709f1705ec1SDavid Ahern * address is retained on a down event 3710f1705ec1SDavid Ahern */ 3711f1705ec1SDavid Ahern if (!keep_addr || 371270af921dSDavid Ahern !(ifa->flags & IFA_F_PERMANENT) || 371370af921dSDavid Ahern addr_is_local(&ifa->addr)) { 3714f1705ec1SDavid Ahern hlist_del_init_rcu(&ifa->addr_lst); 371573a8bd74SDavid S. Miller goto restart; 371673a8bd74SDavid S. Miller } 371773a8bd74SDavid S. Miller } 3718f1705ec1SDavid Ahern } 371973a8bd74SDavid S. Miller spin_unlock_bh(&addrconf_hash_lock); 372073a8bd74SDavid S. Miller } 372173a8bd74SDavid S. Miller 37221da177e4SLinus Torvalds write_lock_bh(&idev->lock); 37231da177e4SLinus Torvalds 3724b7b1bfceSHannes Frederic Sowa addrconf_del_rs_timer(idev); 3725b7b1bfceSHannes Frederic Sowa 3726bcdd553fSStephen Hemminger /* Step 2: clear flags for stateless addrconf */ 3727439e2385SDenis V. Lunev if (!how) 37283c21edbdSYOSHIFUJI Hideaki idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY); 37291da177e4SLinus Torvalds 3730bcdd553fSStephen Hemminger /* Step 3: clear tempaddr list */ 3731372e6c8fSstephen hemminger while (!list_empty(&idev->tempaddr_list)) { 3732372e6c8fSstephen hemminger ifa = list_first_entry(&idev->tempaddr_list, 3733372e6c8fSstephen hemminger struct inet6_ifaddr, tmp_list); 3734372e6c8fSstephen hemminger list_del(&ifa->tmp_list); 37351da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 37361da177e4SLinus Torvalds spin_lock_bh(&ifa->lock); 37371da177e4SLinus Torvalds 37381da177e4SLinus Torvalds if (ifa->ifpub) { 37391da177e4SLinus Torvalds in6_ifa_put(ifa->ifpub); 37401da177e4SLinus Torvalds ifa->ifpub = NULL; 37411da177e4SLinus Torvalds } 37421da177e4SLinus Torvalds spin_unlock_bh(&ifa->lock); 37431da177e4SLinus Torvalds in6_ifa_put(ifa); 37441da177e4SLinus Torvalds write_lock_bh(&idev->lock); 37451da177e4SLinus Torvalds } 37468f37ada5Sstephen hemminger 3747f1705ec1SDavid Ahern list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) { 37488d1c802bSDavid Ahern struct fib6_info *rt = NULL; 3749a2d6cbb0SRabin Vincent bool keep; 375038bd10c4SDavid Ahern 3751c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifa); 375284e8b803Sstephen hemminger 3753a2d6cbb0SRabin Vincent keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) && 3754a2d6cbb0SRabin Vincent !addr_is_local(&ifa->addr); 3755a2d6cbb0SRabin Vincent 37564c5ff6a6SHerbert Xu write_unlock_bh(&idev->lock); 37578e8e676dSHannes Frederic Sowa spin_lock_bh(&ifa->lock); 3758f1705ec1SDavid Ahern 3759a2d6cbb0SRabin Vincent if (keep) { 3760f1705ec1SDavid Ahern /* set state to skip the notifier below */ 3761f1705ec1SDavid Ahern state = INET6_IFADDR_STATE_DEAD; 37626d717134SDavid Ahern ifa->state = INET6_IFADDR_STATE_PREDAD; 3763f1705ec1SDavid Ahern if (!(ifa->flags & IFA_F_NODAD)) 3764f1705ec1SDavid Ahern ifa->flags |= IFA_F_TENTATIVE; 376538bd10c4SDavid Ahern 376638bd10c4SDavid Ahern rt = ifa->rt; 376738bd10c4SDavid Ahern ifa->rt = NULL; 3768f1705ec1SDavid Ahern } else { 37694c5ff6a6SHerbert Xu state = ifa->state; 37704c5ff6a6SHerbert Xu ifa->state = INET6_IFADDR_STATE_DEAD; 3771f1705ec1SDavid Ahern } 3772f1705ec1SDavid Ahern 37738e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifa->lock); 37744c5ff6a6SHerbert Xu 377538bd10c4SDavid Ahern if (rt) 3776afb1d4b5SDavid Ahern ip6_del_rt(net, rt); 377738bd10c4SDavid Ahern 37789d82ca98SJohn Fastabend if (state != INET6_IFADDR_STATE_DEAD) { 37791da177e4SLinus Torvalds __ipv6_ifa_notify(RTM_DELADDR, ifa); 3780f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_DOWN, ifa); 3781ea06f717SMike Manning } else { 3782ea06f717SMike Manning if (idev->cnf.forwarding) 3783ea06f717SMike Manning addrconf_leave_anycast(ifa); 3784ea06f717SMike Manning addrconf_leave_solict(ifa->idev, &ifa->addr); 37852de79570SLorenzo Colitti } 378673a8bd74SDavid S. Miller 37871da177e4SLinus Torvalds write_lock_bh(&idev->lock); 37888ef802aaSEric Dumazet if (!keep) { 37898ef802aaSEric Dumazet list_del_rcu(&ifa->if_list); 37908ef802aaSEric Dumazet in6_ifa_put(ifa); 37918ef802aaSEric Dumazet } 37921da177e4SLinus Torvalds } 37938f37ada5Sstephen hemminger 37941da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 37951da177e4SLinus Torvalds 3796381f4dcaSSabrina Dubroca /* Step 5: Discard anycast and multicast list */ 3797381f4dcaSSabrina Dubroca if (how) { 3798381f4dcaSSabrina Dubroca ipv6_ac_destroy_dev(idev); 37991da177e4SLinus Torvalds ipv6_mc_destroy_dev(idev); 3800381f4dcaSSabrina Dubroca } else { 38011da177e4SLinus Torvalds ipv6_mc_down(idev); 3802381f4dcaSSabrina Dubroca } 38031da177e4SLinus Torvalds 38041da177e4SLinus Torvalds idev->tstamp = jiffies; 38051da177e4SLinus Torvalds 3806bcdd553fSStephen Hemminger /* Last: Shot the device (if unregistered) */ 3807439e2385SDenis V. Lunev if (how) { 3808408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 38091da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, idev->nd_parms); 38101da177e4SLinus Torvalds neigh_ifdown(&nd_tbl, dev); 38111da177e4SLinus Torvalds in6_dev_put(idev); 38121da177e4SLinus Torvalds } 38131da177e4SLinus Torvalds return 0; 38141da177e4SLinus Torvalds } 38151da177e4SLinus Torvalds 3816e99e88a9SKees Cook static void addrconf_rs_timer(struct timer_list *t) 38171da177e4SLinus Torvalds { 3818e99e88a9SKees Cook struct inet6_dev *idev = from_timer(idev, t, rs_timer); 3819caf92bc4SCong Wang struct net_device *dev = idev->dev; 3820b7b1bfceSHannes Frederic Sowa struct in6_addr lladdr; 38211da177e4SLinus Torvalds 3822b7b1bfceSHannes Frederic Sowa write_lock(&idev->lock); 38235b2a1953Sstephen hemminger if (idev->dead || !(idev->if_flags & IF_READY)) 38241da177e4SLinus Torvalds goto out; 38251da177e4SLinus Torvalds 38269ba2add3SShmulik Ladkani if (!ipv6_accept_ra(idev)) 38271da177e4SLinus Torvalds goto out; 38285b2a1953Sstephen hemminger 38295b2a1953Sstephen hemminger /* Announcement received after solicitation was sent */ 38305b2a1953Sstephen hemminger if (idev->if_flags & IF_RA_RCVD) 38315b2a1953Sstephen hemminger goto out; 38321da177e4SLinus Torvalds 3833bd11f074SMaciej Żenczykowski if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) { 3834caf92bc4SCong Wang write_unlock(&idev->lock); 3835caf92bc4SCong Wang if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE)) 3836caf92bc4SCong Wang ndisc_send_rs(dev, &lladdr, 3837b7b1bfceSHannes Frederic Sowa &in6addr_linklocal_allrouters); 3838b7b1bfceSHannes Frederic Sowa else 3839caf92bc4SCong Wang goto put; 3840b7b1bfceSHannes Frederic Sowa 3841caf92bc4SCong Wang write_lock(&idev->lock); 3842bd11f074SMaciej Żenczykowski idev->rs_interval = rfc3315_s14_backoff_update( 3843bd11f074SMaciej Żenczykowski idev->rs_interval, idev->cnf.rtr_solicit_max_interval); 38441da177e4SLinus Torvalds /* The wait after the last probe can be shorter */ 3845b7b1bfceSHannes Frederic Sowa addrconf_mod_rs_timer(idev, (idev->rs_probes == 3846b7b1bfceSHannes Frederic Sowa idev->cnf.rtr_solicits) ? 38475b2a1953Sstephen hemminger idev->cnf.rtr_solicit_delay : 3848bd11f074SMaciej Żenczykowski idev->rs_interval); 38491da177e4SLinus Torvalds } else { 38501da177e4SLinus Torvalds /* 38511da177e4SLinus Torvalds * Note: we do not support deprecated "all on-link" 38521da177e4SLinus Torvalds * assumption any longer. 38531da177e4SLinus Torvalds */ 385491df42beSJoe Perches pr_debug("%s: no IPv6 routers present\n", idev->dev->name); 38551da177e4SLinus Torvalds } 38561da177e4SLinus Torvalds 38571da177e4SLinus Torvalds out: 3858b7b1bfceSHannes Frederic Sowa write_unlock(&idev->lock); 3859caf92bc4SCong Wang put: 3860b7b1bfceSHannes Frederic Sowa in6_dev_put(idev); 38611da177e4SLinus Torvalds } 38621da177e4SLinus Torvalds 38631da177e4SLinus Torvalds /* 38641da177e4SLinus Torvalds * Duplicate Address Detection 38651da177e4SLinus Torvalds */ 38663c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp) 38673c21edbdSYOSHIFUJI Hideaki { 38683c21edbdSYOSHIFUJI Hideaki unsigned long rand_num; 38693c21edbdSYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev; 3870adc176c5SErik Nordmark u64 nonce; 38713c21edbdSYOSHIFUJI Hideaki 387295c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 387395c385b4SNeil Horman rand_num = 0; 387495c385b4SNeil Horman else 387563862b5bSAruna-Hewapathirane rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1); 387695c385b4SNeil Horman 3877adc176c5SErik Nordmark nonce = 0; 3878adc176c5SErik Nordmark if (idev->cnf.enhanced_dad || 3879adc176c5SErik Nordmark dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad) { 3880adc176c5SErik Nordmark do 3881adc176c5SErik Nordmark get_random_bytes(&nonce, 6); 3882adc176c5SErik Nordmark while (nonce == 0); 3883adc176c5SErik Nordmark } 3884adc176c5SErik Nordmark ifp->dad_nonce = nonce; 3885b7b1bfceSHannes Frederic Sowa ifp->dad_probes = idev->cnf.dad_transmits; 3886c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, rand_num); 38873c21edbdSYOSHIFUJI Hideaki } 38883c21edbdSYOSHIFUJI Hideaki 3889c15b1ccaSHannes Frederic Sowa static void addrconf_dad_begin(struct inet6_ifaddr *ifp) 38901da177e4SLinus Torvalds { 38911da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 38921da177e4SLinus Torvalds struct net_device *dev = idev->dev; 3893764d3be6SPaolo Abeni bool bump_id, notify = false; 3894afb1d4b5SDavid Ahern struct net *net; 38951da177e4SLinus Torvalds 38961da177e4SLinus Torvalds addrconf_join_solict(dev, &ifp->addr); 38971da177e4SLinus Torvalds 389863862b5bSAruna-Hewapathirane prandom_seed((__force u32) ifp->addr.s6_addr32[3]); 38991da177e4SLinus Torvalds 39001da177e4SLinus Torvalds read_lock_bh(&idev->lock); 3901622ccdf1SHerbert Xu spin_lock(&ifp->lock); 3902e9d3e084SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DEAD) 39031da177e4SLinus Torvalds goto out; 39041da177e4SLinus Torvalds 3905afb1d4b5SDavid Ahern net = dev_net(dev); 39061da177e4SLinus Torvalds if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) || 3907afb1d4b5SDavid Ahern (net->ipv6.devconf_all->accept_dad < 1 && 3908a2d3f3e3SMatteo Croce idev->cnf.accept_dad < 1) || 390955ebaef1SNoriaki TAKAMIYA !(ifp->flags&IFA_F_TENTATIVE) || 391055ebaef1SNoriaki TAKAMIYA ifp->flags & IFA_F_NODAD) { 3911c76fe2d9SDavid Ahern bool send_na = false; 3912c76fe2d9SDavid Ahern 3913c76fe2d9SDavid Ahern if (ifp->flags & IFA_F_TENTATIVE && 3914c76fe2d9SDavid Ahern !(ifp->flags & IFA_F_OPTIMISTIC)) 3915c76fe2d9SDavid Ahern send_na = true; 3916764d3be6SPaolo Abeni bump_id = ifp->flags & IFA_F_TENTATIVE; 3917cc411d0bSBrian Haley ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 391821809fafSstephen hemminger spin_unlock(&ifp->lock); 39191da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 39201da177e4SLinus Torvalds 3921c76fe2d9SDavid Ahern addrconf_dad_completed(ifp, bump_id, send_na); 39221da177e4SLinus Torvalds return; 39231da177e4SLinus Torvalds } 39241da177e4SLinus Torvalds 39256732badeSYOSHIFUJI Hideaki if (!(idev->if_flags & IF_READY)) { 392621809fafSstephen hemminger spin_unlock(&ifp->lock); 39276732badeSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 39283c21edbdSYOSHIFUJI Hideaki /* 3929590a9887SMasatake YAMATO * If the device is not ready: 39303c21edbdSYOSHIFUJI Hideaki * - keep it tentative if it is a permanent address. 39313c21edbdSYOSHIFUJI Hideaki * - otherwise, kill it. 39323c21edbdSYOSHIFUJI Hideaki */ 39333c21edbdSYOSHIFUJI Hideaki in6_ifa_hold(ifp); 3934cc411d0bSBrian Haley addrconf_dad_stop(ifp, 0); 39356732badeSYOSHIFUJI Hideaki return; 39363c21edbdSYOSHIFUJI Hideaki } 393795c385b4SNeil Horman 393895c385b4SNeil Horman /* 393995c385b4SNeil Horman * Optimistic nodes can start receiving 394095c385b4SNeil Horman * Frames right away 394195c385b4SNeil Horman */ 39427fd2561eSErik Kline if (ifp->flags & IFA_F_OPTIMISTIC) { 3943afb1d4b5SDavid Ahern ip6_ins_rt(net, ifp->rt); 3944afb1d4b5SDavid Ahern if (ipv6_use_optimistic_addr(net, idev)) { 39457fd2561eSErik Kline /* Because optimistic nodes can use this address, 39467fd2561eSErik Kline * notify listeners. If DAD fails, RTM_DELADDR is sent. 39477fd2561eSErik Kline */ 394816186a82Ssubashab@codeaurora.org notify = true; 39497fd2561eSErik Kline } 39507fd2561eSErik Kline } 395195c385b4SNeil Horman 39526732badeSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 39531da177e4SLinus Torvalds out: 3954622ccdf1SHerbert Xu spin_unlock(&ifp->lock); 39551da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 395616186a82Ssubashab@codeaurora.org if (notify) 395716186a82Ssubashab@codeaurora.org ipv6_ifa_notify(RTM_NEWADDR, ifp); 39581da177e4SLinus Torvalds } 39591da177e4SLinus Torvalds 3960c15b1ccaSHannes Frederic Sowa static void addrconf_dad_start(struct inet6_ifaddr *ifp) 39611da177e4SLinus Torvalds { 3962c15b1ccaSHannes Frederic Sowa bool begin_dad = false; 3963c15b1ccaSHannes Frederic Sowa 39648e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 3965c15b1ccaSHannes Frederic Sowa if (ifp->state != INET6_IFADDR_STATE_DEAD) { 3966c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_PREDAD; 3967c15b1ccaSHannes Frederic Sowa begin_dad = true; 3968c15b1ccaSHannes Frederic Sowa } 39698e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 3970c15b1ccaSHannes Frederic Sowa 3971c15b1ccaSHannes Frederic Sowa if (begin_dad) 3972c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 0); 3973c15b1ccaSHannes Frederic Sowa } 3974c15b1ccaSHannes Frederic Sowa 3975c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w) 3976c15b1ccaSHannes Frederic Sowa { 3977c15b1ccaSHannes Frederic Sowa struct inet6_ifaddr *ifp = container_of(to_delayed_work(w), 3978c15b1ccaSHannes Frederic Sowa struct inet6_ifaddr, 3979c15b1ccaSHannes Frederic Sowa dad_work); 39801da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 3981764d3be6SPaolo Abeni bool bump_id, disable_ipv6 = false; 39821da177e4SLinus Torvalds struct in6_addr mcaddr; 39831da177e4SLinus Torvalds 3984c15b1ccaSHannes Frederic Sowa enum { 3985c15b1ccaSHannes Frederic Sowa DAD_PROCESS, 3986c15b1ccaSHannes Frederic Sowa DAD_BEGIN, 3987c15b1ccaSHannes Frederic Sowa DAD_ABORT, 3988c15b1ccaSHannes Frederic Sowa } action = DAD_PROCESS; 3989c15b1ccaSHannes Frederic Sowa 3990c15b1ccaSHannes Frederic Sowa rtnl_lock(); 3991c15b1ccaSHannes Frederic Sowa 39928e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 3993c15b1ccaSHannes Frederic Sowa if (ifp->state == INET6_IFADDR_STATE_PREDAD) { 3994c15b1ccaSHannes Frederic Sowa action = DAD_BEGIN; 3995c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_DAD; 3996c15b1ccaSHannes Frederic Sowa } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) { 3997c15b1ccaSHannes Frederic Sowa action = DAD_ABORT; 3998c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_POSTDAD; 399985b51b12SMike Manning 400035e015e1SMatteo Croce if ((dev_net(idev->dev)->ipv6.devconf_all->accept_dad > 1 || 400135e015e1SMatteo Croce idev->cnf.accept_dad > 1) && 400235e015e1SMatteo Croce !idev->cnf.disable_ipv6 && 400385b51b12SMike Manning !(ifp->flags & IFA_F_STABLE_PRIVACY)) { 400485b51b12SMike Manning struct in6_addr addr; 400585b51b12SMike Manning 400685b51b12SMike Manning addr.s6_addr32[0] = htonl(0xfe800000); 400785b51b12SMike Manning addr.s6_addr32[1] = 0; 400885b51b12SMike Manning 400985b51b12SMike Manning if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) && 401085b51b12SMike Manning ipv6_addr_equal(&ifp->addr, &addr)) { 401185b51b12SMike Manning /* DAD failed for link-local based on MAC */ 401285b51b12SMike Manning idev->cnf.disable_ipv6 = 1; 401385b51b12SMike Manning 401485b51b12SMike Manning pr_info("%s: IPv6 being disabled!\n", 401585b51b12SMike Manning ifp->idev->dev->name); 401685b51b12SMike Manning disable_ipv6 = true; 401785b51b12SMike Manning } 401885b51b12SMike Manning } 4019c15b1ccaSHannes Frederic Sowa } 40208e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 4021c15b1ccaSHannes Frederic Sowa 4022c15b1ccaSHannes Frederic Sowa if (action == DAD_BEGIN) { 4023c15b1ccaSHannes Frederic Sowa addrconf_dad_begin(ifp); 4024c15b1ccaSHannes Frederic Sowa goto out; 4025c15b1ccaSHannes Frederic Sowa } else if (action == DAD_ABORT) { 4026751eb6b6SWei Yongjun in6_ifa_hold(ifp); 4027c15b1ccaSHannes Frederic Sowa addrconf_dad_stop(ifp, 1); 402885b51b12SMike Manning if (disable_ipv6) 402985b51b12SMike Manning addrconf_ifdown(idev->dev, 0); 4030c15b1ccaSHannes Frederic Sowa goto out; 4031c15b1ccaSHannes Frederic Sowa } 4032c15b1ccaSHannes Frederic Sowa 4033b7b1bfceSHannes Frederic Sowa if (!ifp->dad_probes && addrconf_dad_end(ifp)) 4034f2344a13SHerbert Xu goto out; 4035f2344a13SHerbert Xu 4036c15b1ccaSHannes Frederic Sowa write_lock_bh(&idev->lock); 4037122e4519Sstephen hemminger if (idev->dead || !(idev->if_flags & IF_READY)) { 4038c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 40391da177e4SLinus Torvalds goto out; 40401da177e4SLinus Torvalds } 404121809fafSstephen hemminger 404221809fafSstephen hemminger spin_lock(&ifp->lock); 4043622ccdf1SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DEAD) { 4044622ccdf1SHerbert Xu spin_unlock(&ifp->lock); 4045c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 4046622ccdf1SHerbert Xu goto out; 4047622ccdf1SHerbert Xu } 4048622ccdf1SHerbert Xu 4049b7b1bfceSHannes Frederic Sowa if (ifp->dad_probes == 0) { 4050c76fe2d9SDavid Ahern bool send_na = false; 4051c76fe2d9SDavid Ahern 40521da177e4SLinus Torvalds /* 40531da177e4SLinus Torvalds * DAD was successful 40541da177e4SLinus Torvalds */ 40551da177e4SLinus Torvalds 4056c76fe2d9SDavid Ahern if (ifp->flags & IFA_F_TENTATIVE && 4057c76fe2d9SDavid Ahern !(ifp->flags & IFA_F_OPTIMISTIC)) 4058c76fe2d9SDavid Ahern send_na = true; 4059764d3be6SPaolo Abeni bump_id = ifp->flags & IFA_F_TENTATIVE; 4060cc411d0bSBrian Haley ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 406121809fafSstephen hemminger spin_unlock(&ifp->lock); 4062c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 40631da177e4SLinus Torvalds 4064c76fe2d9SDavid Ahern addrconf_dad_completed(ifp, bump_id, send_na); 40651da177e4SLinus Torvalds 40661da177e4SLinus Torvalds goto out; 40671da177e4SLinus Torvalds } 40681da177e4SLinus Torvalds 4069b7b1bfceSHannes Frederic Sowa ifp->dad_probes--; 4070c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 40711f9248e5SJiri Pirko NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME)); 407221809fafSstephen hemminger spin_unlock(&ifp->lock); 4073c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 40741da177e4SLinus Torvalds 40751da177e4SLinus Torvalds /* send a neighbour solicitation for our addr */ 40761da177e4SLinus Torvalds addrconf_addr_solict_mult(&ifp->addr, &mcaddr); 4077adc176c5SErik Nordmark ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any, 4078adc176c5SErik Nordmark ifp->dad_nonce); 40791da177e4SLinus Torvalds out: 40801da177e4SLinus Torvalds in6_ifa_put(ifp); 4081c15b1ccaSHannes Frederic Sowa rtnl_unlock(); 40821da177e4SLinus Torvalds } 40831da177e4SLinus Torvalds 408411ffff75SHannes Frederic Sowa /* ifp->idev must be at least read locked */ 408511ffff75SHannes Frederic Sowa static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp) 408611ffff75SHannes Frederic Sowa { 408711ffff75SHannes Frederic Sowa struct inet6_ifaddr *ifpiter; 408811ffff75SHannes Frederic Sowa struct inet6_dev *idev = ifp->idev; 408911ffff75SHannes Frederic Sowa 4090602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) { 4091602582caSHannes Frederic Sowa if (ifpiter->scope > IFA_LINK) 4092602582caSHannes Frederic Sowa break; 409311ffff75SHannes Frederic Sowa if (ifp != ifpiter && ifpiter->scope == IFA_LINK && 409411ffff75SHannes Frederic Sowa (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE| 409511ffff75SHannes Frederic Sowa IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) == 409611ffff75SHannes Frederic Sowa IFA_F_PERMANENT) 409711ffff75SHannes Frederic Sowa return false; 409811ffff75SHannes Frederic Sowa } 409911ffff75SHannes Frederic Sowa return true; 410011ffff75SHannes Frederic Sowa } 410111ffff75SHannes Frederic Sowa 4102c76fe2d9SDavid Ahern static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id, 4103c76fe2d9SDavid Ahern bool send_na) 41041da177e4SLinus Torvalds { 41051da177e4SLinus Torvalds struct net_device *dev = ifp->idev->dev; 4106b7b1bfceSHannes Frederic Sowa struct in6_addr lladdr; 4107b173ee48SHannes Frederic Sowa bool send_rs, send_mld; 4108b7b1bfceSHannes Frederic Sowa 4109c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifp); 41101da177e4SLinus Torvalds 41111da177e4SLinus Torvalds /* 41121da177e4SLinus Torvalds * Configure the address for reception. Now it is valid. 41131da177e4SLinus Torvalds */ 41141da177e4SLinus Torvalds 41151da177e4SLinus Torvalds ipv6_ifa_notify(RTM_NEWADDR, ifp); 41161da177e4SLinus Torvalds 4117026359bcSTore Anderson /* If added prefix is link local and we are prepared to process 4118026359bcSTore Anderson router advertisements, start sending router solicitations. 41191da177e4SLinus Torvalds */ 41201da177e4SLinus Torvalds 41211ec047ebSHannes Frederic Sowa read_lock_bh(&ifp->idev->lock); 412211ffff75SHannes Frederic Sowa send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp); 4123b173ee48SHannes Frederic Sowa send_rs = send_mld && 4124b173ee48SHannes Frederic Sowa ipv6_accept_ra(ifp->idev) && 4125bd11f074SMaciej Żenczykowski ifp->idev->cnf.rtr_solicits != 0 && 4126b173ee48SHannes Frederic Sowa (dev->flags&IFF_LOOPBACK) == 0; 41271ec047ebSHannes Frederic Sowa read_unlock_bh(&ifp->idev->lock); 41281ec047ebSHannes Frederic Sowa 4129b173ee48SHannes Frederic Sowa /* While dad is in progress mld report's source address is in6_addrany. 4130b173ee48SHannes Frederic Sowa * Resend with proper ll now. 4131b173ee48SHannes Frederic Sowa */ 4132b173ee48SHannes Frederic Sowa if (send_mld) 4133b173ee48SHannes Frederic Sowa ipv6_mc_dad_complete(ifp->idev); 4134b173ee48SHannes Frederic Sowa 4135c76fe2d9SDavid Ahern /* send unsolicited NA if enabled */ 4136c76fe2d9SDavid Ahern if (send_na && 4137c76fe2d9SDavid Ahern (ifp->idev->cnf.ndisc_notify || 4138c76fe2d9SDavid Ahern dev_net(dev)->ipv6.devconf_all->ndisc_notify)) { 4139c76fe2d9SDavid Ahern ndisc_send_na(dev, &in6addr_linklocal_allnodes, &ifp->addr, 4140c76fe2d9SDavid Ahern /*router=*/ !!ifp->idev->cnf.forwarding, 4141c76fe2d9SDavid Ahern /*solicited=*/ false, /*override=*/ true, 4142c76fe2d9SDavid Ahern /*inc_opt=*/ true); 4143c76fe2d9SDavid Ahern } 4144c76fe2d9SDavid Ahern 41451ec047ebSHannes Frederic Sowa if (send_rs) { 41461da177e4SLinus Torvalds /* 41471da177e4SLinus Torvalds * If a host as already performed a random delay 41481da177e4SLinus Torvalds * [...] as part of DAD [...] there is no need 41491da177e4SLinus Torvalds * to delay again before sending the first RS 41501da177e4SLinus Torvalds */ 41511ec047ebSHannes Frederic Sowa if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE)) 4152b7b1bfceSHannes Frederic Sowa return; 41531ec047ebSHannes Frederic Sowa ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters); 41541da177e4SLinus Torvalds 4155b7b1bfceSHannes Frederic Sowa write_lock_bh(&ifp->idev->lock); 4156b7b1bfceSHannes Frederic Sowa spin_lock(&ifp->lock); 4157bd11f074SMaciej Żenczykowski ifp->idev->rs_interval = rfc3315_s14_backoff_init( 4158bd11f074SMaciej Żenczykowski ifp->idev->cnf.rtr_solicit_interval); 4159b7b1bfceSHannes Frederic Sowa ifp->idev->rs_probes = 1; 41601da177e4SLinus Torvalds ifp->idev->if_flags |= IF_RS_SENT; 4161bd11f074SMaciej Żenczykowski addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval); 4162b7b1bfceSHannes Frederic Sowa spin_unlock(&ifp->lock); 4163b7b1bfceSHannes Frederic Sowa write_unlock_bh(&ifp->idev->lock); 41641da177e4SLinus Torvalds } 4165764d3be6SPaolo Abeni 4166764d3be6SPaolo Abeni if (bump_id) 4167764d3be6SPaolo Abeni rt_genid_bump_ipv6(dev_net(dev)); 4168a11a7f71SMarcus Huewe 4169a11a7f71SMarcus Huewe /* Make sure that a new temporary address will be created 4170a11a7f71SMarcus Huewe * before this temporary address becomes deprecated. 4171a11a7f71SMarcus Huewe */ 4172a11a7f71SMarcus Huewe if (ifp->flags & IFA_F_TEMPORARY) 4173a11a7f71SMarcus Huewe addrconf_verify_rtnl(); 41741da177e4SLinus Torvalds } 41751da177e4SLinus Torvalds 4176e21e8467SStephen Hemminger static void addrconf_dad_run(struct inet6_dev *idev) 4177e21e8467SStephen Hemminger { 4178c5e33bddSYOSHIFUJI Hideaki struct inet6_ifaddr *ifp; 4179c5e33bddSYOSHIFUJI Hideaki 4180c5e33bddSYOSHIFUJI Hideaki read_lock_bh(&idev->lock); 4181502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 418221809fafSstephen hemminger spin_lock(&ifp->lock); 4183f2344a13SHerbert Xu if (ifp->flags & IFA_F_TENTATIVE && 4184f2344a13SHerbert Xu ifp->state == INET6_IFADDR_STATE_DAD) 4185c5e33bddSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 4186f2344a13SHerbert Xu spin_unlock(&ifp->lock); 4187c5e33bddSYOSHIFUJI Hideaki } 4188c5e33bddSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 4189c5e33bddSYOSHIFUJI Hideaki } 4190c5e33bddSYOSHIFUJI Hideaki 41911da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 41921da177e4SLinus Torvalds struct if6_iter_state { 41933c40090aSDaniel Lezcano struct seq_net_private p; 41941da177e4SLinus Torvalds int bucket; 41951d578303SMihai Maruseac int offset; 41961da177e4SLinus Torvalds }; 41971da177e4SLinus Torvalds 41981d578303SMihai Maruseac static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos) 41991da177e4SLinus Torvalds { 42001da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 42011218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 4202a5c1d98fSEric Dumazet struct inet6_ifaddr *ifa = NULL; 42031d578303SMihai Maruseac int p = 0; 42041da177e4SLinus Torvalds 42051d578303SMihai Maruseac /* initial bucket if pos is 0 */ 42061d578303SMihai Maruseac if (pos == 0) { 42071d578303SMihai Maruseac state->bucket = 0; 42081d578303SMihai Maruseac state->offset = 0; 42091d578303SMihai Maruseac } 42101d578303SMihai Maruseac 42111d578303SMihai Maruseac for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) { 4212a5c1d98fSEric Dumazet hlist_for_each_entry_rcu(ifa, &inet6_addr_lst[state->bucket], 42131d578303SMihai Maruseac addr_lst) { 42149f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 42159f0d3c27SEric Dumazet continue; 42161d578303SMihai Maruseac /* sync with offset */ 42171d578303SMihai Maruseac if (p < state->offset) { 42181d578303SMihai Maruseac p++; 42191d578303SMihai Maruseac continue; 42201d578303SMihai Maruseac } 42211da177e4SLinus Torvalds return ifa; 42221da177e4SLinus Torvalds } 42231d578303SMihai Maruseac 42241d578303SMihai Maruseac /* prepare for next bucket */ 42251d578303SMihai Maruseac state->offset = 0; 42261d578303SMihai Maruseac p = 0; 42271d578303SMihai Maruseac } 4228c2e21293Sstephen hemminger return NULL; 4229c2e21293Sstephen hemminger } 42301da177e4SLinus Torvalds 4231c2e21293Sstephen hemminger static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, 4232c2e21293Sstephen hemminger struct inet6_ifaddr *ifa) 42331da177e4SLinus Torvalds { 42341da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 42351218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 42361da177e4SLinus Torvalds 4237a5c1d98fSEric Dumazet hlist_for_each_entry_continue_rcu(ifa, addr_lst) { 42389f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 42399f0d3c27SEric Dumazet continue; 42401d578303SMihai Maruseac state->offset++; 42411da177e4SLinus Torvalds return ifa; 42421d578303SMihai Maruseac } 42431da177e4SLinus Torvalds 42441d578303SMihai Maruseac state->offset = 0; 424586f9bd1fSJeff Barnhill while (++state->bucket < IN6_ADDR_HSIZE) { 4246a5c1d98fSEric Dumazet hlist_for_each_entry_rcu(ifa, 4247c2e21293Sstephen hemminger &inet6_addr_lst[state->bucket], addr_lst) { 42489f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 42499f0d3c27SEric Dumazet continue; 4250c2e21293Sstephen hemminger return ifa; 4251c2e21293Sstephen hemminger } 4252c2e21293Sstephen hemminger } 4253c2e21293Sstephen hemminger 4254c2e21293Sstephen hemminger return NULL; 4255c2e21293Sstephen hemminger } 4256c2e21293Sstephen hemminger 42571da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos) 4258a5c1d98fSEric Dumazet __acquires(rcu) 42591da177e4SLinus Torvalds { 4260a5c1d98fSEric Dumazet rcu_read_lock(); 42611d578303SMihai Maruseac return if6_get_first(seq, *pos); 42621da177e4SLinus Torvalds } 42631da177e4SLinus Torvalds 42641da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos) 42651da177e4SLinus Torvalds { 42661da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 42671da177e4SLinus Torvalds 42681da177e4SLinus Torvalds ifa = if6_get_next(seq, v); 42691da177e4SLinus Torvalds ++*pos; 42701da177e4SLinus Torvalds return ifa; 42711da177e4SLinus Torvalds } 42721da177e4SLinus Torvalds 42731da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v) 4274a5c1d98fSEric Dumazet __releases(rcu) 42751da177e4SLinus Torvalds { 4276a5c1d98fSEric Dumazet rcu_read_unlock(); 42771da177e4SLinus Torvalds } 42781da177e4SLinus Torvalds 42791da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v) 42801da177e4SLinus Torvalds { 42811da177e4SLinus Torvalds struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v; 4282971a351cSJiri Pirko seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n", 4283b071195dSHarvey Harrison &ifp->addr, 42841da177e4SLinus Torvalds ifp->idev->dev->ifindex, 42851da177e4SLinus Torvalds ifp->prefix_len, 42861da177e4SLinus Torvalds ifp->scope, 4287971a351cSJiri Pirko (u8) ifp->flags, 42881da177e4SLinus Torvalds ifp->idev->dev->name); 42891da177e4SLinus Torvalds return 0; 42901da177e4SLinus Torvalds } 42911da177e4SLinus Torvalds 429256b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = { 42931da177e4SLinus Torvalds .start = if6_seq_start, 42941da177e4SLinus Torvalds .next = if6_seq_next, 42951da177e4SLinus Torvalds .show = if6_seq_show, 42961da177e4SLinus Torvalds .stop = if6_seq_stop, 42971da177e4SLinus Torvalds }; 42981da177e4SLinus Torvalds 42992c8c1e72SAlexey Dobriyan static int __net_init if6_proc_net_init(struct net *net) 43001da177e4SLinus Torvalds { 4301c3506372SChristoph Hellwig if (!proc_create_net("if_inet6", 0444, net->proc_net, &if6_seq_ops, 4302c3506372SChristoph Hellwig sizeof(struct if6_iter_state))) 43031da177e4SLinus Torvalds return -ENOMEM; 43041da177e4SLinus Torvalds return 0; 43051da177e4SLinus Torvalds } 43061da177e4SLinus Torvalds 43072c8c1e72SAlexey Dobriyan static void __net_exit if6_proc_net_exit(struct net *net) 43083c40090aSDaniel Lezcano { 4309ece31ffdSGao feng remove_proc_entry("if_inet6", net->proc_net); 43103c40090aSDaniel Lezcano } 43113c40090aSDaniel Lezcano 43123c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = { 43133c40090aSDaniel Lezcano .init = if6_proc_net_init, 43143c40090aSDaniel Lezcano .exit = if6_proc_net_exit, 43153c40090aSDaniel Lezcano }; 43163c40090aSDaniel Lezcano 43173c40090aSDaniel Lezcano int __init if6_proc_init(void) 43183c40090aSDaniel Lezcano { 43193c40090aSDaniel Lezcano return register_pernet_subsys(&if6_proc_net_ops); 43203c40090aSDaniel Lezcano } 43213c40090aSDaniel Lezcano 43221da177e4SLinus Torvalds void if6_proc_exit(void) 43231da177e4SLinus Torvalds { 43243c40090aSDaniel Lezcano unregister_pernet_subsys(&if6_proc_net_ops); 43251da177e4SLinus Torvalds } 43261da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 43271da177e4SLinus Torvalds 432807a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6) 43293b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */ 4330b71d1d42SEric Dumazet int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr) 43313b9f9a1cSNoriaki TAKAMIYA { 43323f27fb23SEric Dumazet unsigned int hash = inet6_addr_hash(net, addr); 4333c2e21293Sstephen hemminger struct inet6_ifaddr *ifp = NULL; 43343f27fb23SEric Dumazet int ret = 0; 4335c2e21293Sstephen hemminger 43364e5f47abSEric Dumazet rcu_read_lock(); 43374e5f47abSEric Dumazet hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) { 4338878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 4339389f6612SDaniel Lezcano continue; 4340caad295fSYOSHIFUJI Hideaki if (ipv6_addr_equal(&ifp->addr, addr) && 43413b9f9a1cSNoriaki TAKAMIYA (ifp->flags & IFA_F_HOMEADDRESS)) { 43423b9f9a1cSNoriaki TAKAMIYA ret = 1; 43433b9f9a1cSNoriaki TAKAMIYA break; 43443b9f9a1cSNoriaki TAKAMIYA } 43453b9f9a1cSNoriaki TAKAMIYA } 43464e5f47abSEric Dumazet rcu_read_unlock(); 43473b9f9a1cSNoriaki TAKAMIYA return ret; 43483b9f9a1cSNoriaki TAKAMIYA } 43493b9f9a1cSNoriaki TAKAMIYA #endif 43503b9f9a1cSNoriaki TAKAMIYA 43511da177e4SLinus Torvalds /* 43521da177e4SLinus Torvalds * Periodic address status verification 43531da177e4SLinus Torvalds */ 43541da177e4SLinus Torvalds 4355c15b1ccaSHannes Frederic Sowa static void addrconf_verify_rtnl(void) 43561da177e4SLinus Torvalds { 4357b2db7564SYOSHIFUJI Hideaki unsigned long now, next, next_sec, next_sched; 43581da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 43591da177e4SLinus Torvalds int i; 43601da177e4SLinus Torvalds 4361c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 4362c15b1ccaSHannes Frederic Sowa 43635c578aedSstephen hemminger rcu_read_lock_bh(); 43641da177e4SLinus Torvalds now = jiffies; 4365b2db7564SYOSHIFUJI Hideaki next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY); 43661da177e4SLinus Torvalds 4367c15b1ccaSHannes Frederic Sowa cancel_delayed_work(&addr_chk_work); 43681da177e4SLinus Torvalds 43691da177e4SLinus Torvalds for (i = 0; i < IN6_ADDR_HSIZE; i++) { 43701da177e4SLinus Torvalds restart: 4371c15b1ccaSHannes Frederic Sowa hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) { 43721da177e4SLinus Torvalds unsigned long age; 43731da177e4SLinus Torvalds 4374fad8da3eSYasushi Asano /* When setting preferred_lft to a value not zero or 4375fad8da3eSYasushi Asano * infinity, while valid_lft is infinity 4376fad8da3eSYasushi Asano * IFA_F_PERMANENT has a non-infinity life time. 4377fad8da3eSYasushi Asano */ 4378fad8da3eSYasushi Asano if ((ifp->flags & IFA_F_PERMANENT) && 4379fad8da3eSYasushi Asano (ifp->prefered_lft == INFINITY_LIFE_TIME)) 43801da177e4SLinus Torvalds continue; 43811da177e4SLinus Torvalds 43821da177e4SLinus Torvalds spin_lock(&ifp->lock); 4383b2db7564SYOSHIFUJI Hideaki /* We try to batch several events at once. */ 4384b2db7564SYOSHIFUJI Hideaki age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; 43851da177e4SLinus Torvalds 43868f27ebb9SYOSHIFUJI Hideaki if (ifp->valid_lft != INFINITY_LIFE_TIME && 43878f27ebb9SYOSHIFUJI Hideaki age >= ifp->valid_lft) { 43881da177e4SLinus Torvalds spin_unlock(&ifp->lock); 43891da177e4SLinus Torvalds in6_ifa_hold(ifp); 43901da177e4SLinus Torvalds ipv6_del_addr(ifp); 43911da177e4SLinus Torvalds goto restart; 43928f27ebb9SYOSHIFUJI Hideaki } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) { 43938f27ebb9SYOSHIFUJI Hideaki spin_unlock(&ifp->lock); 43948f27ebb9SYOSHIFUJI Hideaki continue; 43951da177e4SLinus Torvalds } else if (age >= ifp->prefered_lft) { 4396a1ed0526SBrian Haley /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */ 43971da177e4SLinus Torvalds int deprecate = 0; 43981da177e4SLinus Torvalds 43991da177e4SLinus Torvalds if (!(ifp->flags&IFA_F_DEPRECATED)) { 44001da177e4SLinus Torvalds deprecate = 1; 44011da177e4SLinus Torvalds ifp->flags |= IFA_F_DEPRECATED; 44021da177e4SLinus Torvalds } 44031da177e4SLinus Torvalds 4404fad8da3eSYasushi Asano if ((ifp->valid_lft != INFINITY_LIFE_TIME) && 4405fad8da3eSYasushi Asano (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))) 44061da177e4SLinus Torvalds next = ifp->tstamp + ifp->valid_lft * HZ; 44071da177e4SLinus Torvalds 44081da177e4SLinus Torvalds spin_unlock(&ifp->lock); 44091da177e4SLinus Torvalds 44101da177e4SLinus Torvalds if (deprecate) { 44111da177e4SLinus Torvalds in6_ifa_hold(ifp); 44121da177e4SLinus Torvalds 44131da177e4SLinus Torvalds ipv6_ifa_notify(0, ifp); 44141da177e4SLinus Torvalds in6_ifa_put(ifp); 44151da177e4SLinus Torvalds goto restart; 44161da177e4SLinus Torvalds } 44171da177e4SLinus Torvalds } else if ((ifp->flags&IFA_F_TEMPORARY) && 44181da177e4SLinus Torvalds !(ifp->flags&IFA_F_TENTATIVE)) { 441988949cf4Sstephen hemminger unsigned long regen_advance = ifp->idev->cnf.regen_max_retry * 442088949cf4Sstephen hemminger ifp->idev->cnf.dad_transmits * 44211f9248e5SJiri Pirko NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ; 442288949cf4Sstephen hemminger 44231da177e4SLinus Torvalds if (age >= ifp->prefered_lft - regen_advance) { 44241da177e4SLinus Torvalds struct inet6_ifaddr *ifpub = ifp->ifpub; 44251da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 44261da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 44271da177e4SLinus Torvalds if (!ifp->regen_count && ifpub) { 44281da177e4SLinus Torvalds ifp->regen_count++; 44291da177e4SLinus Torvalds in6_ifa_hold(ifp); 44301da177e4SLinus Torvalds in6_ifa_hold(ifpub); 44311da177e4SLinus Torvalds spin_unlock(&ifp->lock); 44325c578aedSstephen hemminger 4433291d809bSHiroyuki YAMAMORI spin_lock(&ifpub->lock); 4434291d809bSHiroyuki YAMAMORI ifpub->regen_count = 0; 4435291d809bSHiroyuki YAMAMORI spin_unlock(&ifpub->lock); 4436e64e469bSEric Dumazet rcu_read_unlock_bh(); 4437fffcefe9SEric Dumazet ipv6_create_tempaddr(ifpub, ifp, true); 44381da177e4SLinus Torvalds in6_ifa_put(ifpub); 44391da177e4SLinus Torvalds in6_ifa_put(ifp); 4440e64e469bSEric Dumazet rcu_read_lock_bh(); 44411da177e4SLinus Torvalds goto restart; 44421da177e4SLinus Torvalds } 44431da177e4SLinus Torvalds } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next)) 44441da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ; 44451da177e4SLinus Torvalds spin_unlock(&ifp->lock); 44461da177e4SLinus Torvalds } else { 44471da177e4SLinus Torvalds /* ifp->prefered_lft <= ifp->valid_lft */ 44481da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 44491da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 44501da177e4SLinus Torvalds spin_unlock(&ifp->lock); 44511da177e4SLinus Torvalds } 44521da177e4SLinus Torvalds } 44531da177e4SLinus Torvalds } 44541da177e4SLinus Torvalds 4455b2db7564SYOSHIFUJI Hideaki next_sec = round_jiffies_up(next); 4456b2db7564SYOSHIFUJI Hideaki next_sched = next; 4457b2db7564SYOSHIFUJI Hideaki 4458b2db7564SYOSHIFUJI Hideaki /* If rounded timeout is accurate enough, accept it. */ 4459b2db7564SYOSHIFUJI Hideaki if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ)) 4460b2db7564SYOSHIFUJI Hideaki next_sched = next_sec; 4461b2db7564SYOSHIFUJI Hideaki 4462b2db7564SYOSHIFUJI Hideaki /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */ 4463b2db7564SYOSHIFUJI Hideaki if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX)) 4464b2db7564SYOSHIFUJI Hideaki next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX; 4465b2db7564SYOSHIFUJI Hideaki 4466e32ac250SJoe Perches pr_debug("now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n", 4467ba3542e1Sdingtianhong now, next, next_sec, next_sched); 4468c15b1ccaSHannes Frederic Sowa mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now); 44695c578aedSstephen hemminger rcu_read_unlock_bh(); 44701da177e4SLinus Torvalds } 44711da177e4SLinus Torvalds 4472c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *w) 4473c15b1ccaSHannes Frederic Sowa { 4474c15b1ccaSHannes Frederic Sowa rtnl_lock(); 4475c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 4476c15b1ccaSHannes Frederic Sowa rtnl_unlock(); 4477c15b1ccaSHannes Frederic Sowa } 4478c15b1ccaSHannes Frederic Sowa 4479c15b1ccaSHannes Frederic Sowa static void addrconf_verify(void) 4480c15b1ccaSHannes Frederic Sowa { 4481c15b1ccaSHannes Frederic Sowa mod_delayed_work(addrconf_wq, &addr_chk_work, 0); 4482c15b1ccaSHannes Frederic Sowa } 4483c15b1ccaSHannes Frederic Sowa 4484caeaba79SNicolas Dichtel static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local, 4485caeaba79SNicolas Dichtel struct in6_addr **peer_pfx) 4486461d8837SThomas Graf { 4487461d8837SThomas Graf struct in6_addr *pfx = NULL; 4488461d8837SThomas Graf 4489caeaba79SNicolas Dichtel *peer_pfx = NULL; 4490caeaba79SNicolas Dichtel 4491461d8837SThomas Graf if (addr) 4492461d8837SThomas Graf pfx = nla_data(addr); 4493461d8837SThomas Graf 4494461d8837SThomas Graf if (local) { 4495461d8837SThomas Graf if (pfx && nla_memcmp(local, pfx, sizeof(*pfx))) 4496caeaba79SNicolas Dichtel *peer_pfx = pfx; 4497461d8837SThomas Graf pfx = nla_data(local); 4498461d8837SThomas Graf } 4499461d8837SThomas Graf 4500461d8837SThomas Graf return pfx; 4501461d8837SThomas Graf } 4502461d8837SThomas Graf 4503ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = { 4504461d8837SThomas Graf [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) }, 4505461d8837SThomas Graf [IFA_LOCAL] = { .len = sizeof(struct in6_addr) }, 4506461d8837SThomas Graf [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) }, 4507479840ffSJiri Pirko [IFA_FLAGS] = { .len = sizeof(u32) }, 45088308f3ffSDavid Ahern [IFA_RT_PRIORITY] = { .len = sizeof(u32) }, 45096ecf4c37SChristian Brauner [IFA_TARGET_NETNSID] = { .type = NLA_S32 }, 4510461d8837SThomas Graf }; 4511461d8837SThomas Graf 45121da177e4SLinus Torvalds static int 4513c21ef3e3SDavid Ahern inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, 4514c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 45151da177e4SLinus Torvalds { 45163b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 4517b933f716SThomas Graf struct ifaddrmsg *ifm; 4518b933f716SThomas Graf struct nlattr *tb[IFA_MAX+1]; 4519caeaba79SNicolas Dichtel struct in6_addr *pfx, *peer_pfx; 45206046d5b4SHeiner Kallweit u32 ifa_flags; 4521b933f716SThomas Graf int err; 45221da177e4SLinus Torvalds 4523fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy, 4524c21ef3e3SDavid Ahern extack); 4525b933f716SThomas Graf if (err < 0) 4526b933f716SThomas Graf return err; 4527b933f716SThomas Graf 4528b933f716SThomas Graf ifm = nlmsg_data(nlh); 4529caeaba79SNicolas Dichtel pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); 453063159f29SIan Morris if (!pfx) 45311da177e4SLinus Torvalds return -EINVAL; 45321da177e4SLinus Torvalds 45336046d5b4SHeiner Kallweit ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags; 45346046d5b4SHeiner Kallweit 45356046d5b4SHeiner Kallweit /* We ignore other flags so far. */ 45366046d5b4SHeiner Kallweit ifa_flags &= IFA_F_MANAGETEMPADDR; 45376046d5b4SHeiner Kallweit 45386046d5b4SHeiner Kallweit return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx, 45396046d5b4SHeiner Kallweit ifm->ifa_prefixlen); 45401da177e4SLinus Torvalds } 45411da177e4SLinus Torvalds 45428308f3ffSDavid Ahern static int modify_prefix_route(struct inet6_ifaddr *ifp, 45438308f3ffSDavid Ahern unsigned long expires, u32 flags) 45448308f3ffSDavid Ahern { 45458308f3ffSDavid Ahern struct fib6_info *f6i; 4546e7c7faa9SDavid Ahern u32 prio; 45478308f3ffSDavid Ahern 45488308f3ffSDavid Ahern f6i = addrconf_get_prefix_route(&ifp->addr, 45498308f3ffSDavid Ahern ifp->prefix_len, 45508308f3ffSDavid Ahern ifp->idev->dev, 45518308f3ffSDavid Ahern 0, RTF_GATEWAY | RTF_DEFAULT); 45528308f3ffSDavid Ahern if (!f6i) 45538308f3ffSDavid Ahern return -ENOENT; 45548308f3ffSDavid Ahern 4555e7c7faa9SDavid Ahern prio = ifp->rt_priority ? : IP6_RT_PRIO_ADDRCONF; 4556e7c7faa9SDavid Ahern if (f6i->fib6_metric != prio) { 4557e7c7faa9SDavid Ahern /* delete old one */ 4558e7c7faa9SDavid Ahern ip6_del_rt(dev_net(ifp->idev->dev), f6i); 4559e7c7faa9SDavid Ahern 45608308f3ffSDavid Ahern /* add new one */ 45618308f3ffSDavid Ahern addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 45628308f3ffSDavid Ahern ifp->rt_priority, ifp->idev->dev, 45638308f3ffSDavid Ahern expires, flags, GFP_KERNEL); 45648308f3ffSDavid Ahern } else { 45658308f3ffSDavid Ahern if (!expires) 45668308f3ffSDavid Ahern fib6_clean_expires(f6i); 45678308f3ffSDavid Ahern else 45688308f3ffSDavid Ahern fib6_set_expires(f6i, expires); 45698308f3ffSDavid Ahern 45708308f3ffSDavid Ahern fib6_info_release(f6i); 45718308f3ffSDavid Ahern } 45728308f3ffSDavid Ahern 45738308f3ffSDavid Ahern return 0; 45748308f3ffSDavid Ahern } 45758308f3ffSDavid Ahern 4576d169a1f8SDavid Ahern static int inet6_addr_modify(struct inet6_ifaddr *ifp, struct ifa6_config *cfg) 4577081bba5bSNoriaki TAKAMIYA { 45786f704992SYOSHIFUJI Hideaki u32 flags; 45796f704992SYOSHIFUJI Hideaki clock_t expires; 45804bed72e4SYOSHIFUJI Hideaki unsigned long timeout; 458153bd6749SJiri Pirko bool was_managetempaddr; 45825b84efecSThomas Haller bool had_prefixroute; 458346d48046SYOSHIFUJI Hideaki 4584c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 4585c15b1ccaSHannes Frederic Sowa 4586d169a1f8SDavid Ahern if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft) 4587081bba5bSNoriaki TAKAMIYA return -EINVAL; 4588081bba5bSNoriaki TAKAMIYA 4589d169a1f8SDavid Ahern if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR && 459053bd6749SJiri Pirko (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64)) 459153bd6749SJiri Pirko return -EINVAL; 459253bd6749SJiri Pirko 4593f1c02cfbSSabrina Dubroca if (!(ifp->flags & IFA_F_TENTATIVE) || ifp->flags & IFA_F_DADFAILED) 4594d169a1f8SDavid Ahern cfg->ifa_flags &= ~IFA_F_OPTIMISTIC; 4595f1c02cfbSSabrina Dubroca 4596d169a1f8SDavid Ahern timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ); 45974bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 45984bed72e4SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(timeout * HZ); 4599d169a1f8SDavid Ahern cfg->valid_lft = timeout; 46006f704992SYOSHIFUJI Hideaki flags = RTF_EXPIRES; 46014bed72e4SYOSHIFUJI Hideaki } else { 46024bed72e4SYOSHIFUJI Hideaki expires = 0; 46034bed72e4SYOSHIFUJI Hideaki flags = 0; 4604d169a1f8SDavid Ahern cfg->ifa_flags |= IFA_F_PERMANENT; 46056f704992SYOSHIFUJI Hideaki } 4606081bba5bSNoriaki TAKAMIYA 4607d169a1f8SDavid Ahern timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ); 46084bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 46094bed72e4SYOSHIFUJI Hideaki if (timeout == 0) 4610d169a1f8SDavid Ahern cfg->ifa_flags |= IFA_F_DEPRECATED; 4611d169a1f8SDavid Ahern cfg->preferred_lft = timeout; 46124bed72e4SYOSHIFUJI Hideaki } 4613081bba5bSNoriaki TAKAMIYA 4614081bba5bSNoriaki TAKAMIYA spin_lock_bh(&ifp->lock); 461553bd6749SJiri Pirko was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR; 46165b84efecSThomas Haller had_prefixroute = ifp->flags & IFA_F_PERMANENT && 46175b84efecSThomas Haller !(ifp->flags & IFA_F_NOPREFIXROUTE); 461853bd6749SJiri Pirko ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | 4619761aac73SThomas Haller IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR | 4620761aac73SThomas Haller IFA_F_NOPREFIXROUTE); 4621d169a1f8SDavid Ahern ifp->flags |= cfg->ifa_flags; 4622081bba5bSNoriaki TAKAMIYA ifp->tstamp = jiffies; 4623d169a1f8SDavid Ahern ifp->valid_lft = cfg->valid_lft; 4624d169a1f8SDavid Ahern ifp->prefered_lft = cfg->preferred_lft; 4625081bba5bSNoriaki TAKAMIYA 46268308f3ffSDavid Ahern if (cfg->rt_priority && cfg->rt_priority != ifp->rt_priority) 46278308f3ffSDavid Ahern ifp->rt_priority = cfg->rt_priority; 4628081bba5bSNoriaki TAKAMIYA 4629081bba5bSNoriaki TAKAMIYA spin_unlock_bh(&ifp->lock); 4630081bba5bSNoriaki TAKAMIYA if (!(ifp->flags&IFA_F_TENTATIVE)) 4631081bba5bSNoriaki TAKAMIYA ipv6_ifa_notify(0, ifp); 4632081bba5bSNoriaki TAKAMIYA 4633d169a1f8SDavid Ahern if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) { 46348308f3ffSDavid Ahern int rc = -ENOENT; 46358308f3ffSDavid Ahern 46368308f3ffSDavid Ahern if (had_prefixroute) 46378308f3ffSDavid Ahern rc = modify_prefix_route(ifp, expires, flags); 46388308f3ffSDavid Ahern 46398308f3ffSDavid Ahern /* prefix route could have been deleted; if so restore it */ 46408308f3ffSDavid Ahern if (rc == -ENOENT) { 4641acb54e3cSDavid Ahern addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 46428308f3ffSDavid Ahern ifp->rt_priority, ifp->idev->dev, 46438308f3ffSDavid Ahern expires, flags, GFP_KERNEL); 46448308f3ffSDavid Ahern } 46455b84efecSThomas Haller } else if (had_prefixroute) { 46465b84efecSThomas Haller enum cleanup_prefix_rt_t action; 46475b84efecSThomas Haller unsigned long rt_expires; 46485b84efecSThomas Haller 46495b84efecSThomas Haller write_lock_bh(&ifp->idev->lock); 46505b84efecSThomas Haller action = check_cleanup_prefix_route(ifp, &rt_expires); 46515b84efecSThomas Haller write_unlock_bh(&ifp->idev->lock); 46525b84efecSThomas Haller 46535b84efecSThomas Haller if (action != CLEANUP_PREFIX_RT_NOP) { 46545b84efecSThomas Haller cleanup_prefix_route(ifp, rt_expires, 46555b84efecSThomas Haller action == CLEANUP_PREFIX_RT_DEL); 46565b84efecSThomas Haller } 4657761aac73SThomas Haller } 465853bd6749SJiri Pirko 465953bd6749SJiri Pirko if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) { 4660d169a1f8SDavid Ahern if (was_managetempaddr && 4661d169a1f8SDavid Ahern !(ifp->flags & IFA_F_MANAGETEMPADDR)) { 4662d169a1f8SDavid Ahern cfg->valid_lft = 0; 4663d169a1f8SDavid Ahern cfg->preferred_lft = 0; 4664d169a1f8SDavid Ahern } 4665d169a1f8SDavid Ahern manage_tempaddrs(ifp->idev, ifp, cfg->valid_lft, 4666d169a1f8SDavid Ahern cfg->preferred_lft, !was_managetempaddr, 4667d169a1f8SDavid Ahern jiffies); 466853bd6749SJiri Pirko } 466953bd6749SJiri Pirko 4670c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 4671081bba5bSNoriaki TAKAMIYA 4672081bba5bSNoriaki TAKAMIYA return 0; 4673081bba5bSNoriaki TAKAMIYA } 4674081bba5bSNoriaki TAKAMIYA 4675081bba5bSNoriaki TAKAMIYA static int 4676c21ef3e3SDavid Ahern inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, 4677c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 46781da177e4SLinus Torvalds { 46793b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 4680461d8837SThomas Graf struct ifaddrmsg *ifm; 4681461d8837SThomas Graf struct nlattr *tb[IFA_MAX+1]; 468219b1518cSDavid Ahern struct in6_addr *peer_pfx; 46837198f8ceSThomas Graf struct inet6_ifaddr *ifa; 46847198f8ceSThomas Graf struct net_device *dev; 4685f1c02cfbSSabrina Dubroca struct inet6_dev *idev; 468619b1518cSDavid Ahern struct ifa6_config cfg; 4687461d8837SThomas Graf int err; 46881da177e4SLinus Torvalds 4689fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy, 4690c21ef3e3SDavid Ahern extack); 4691461d8837SThomas Graf if (err < 0) 4692461d8837SThomas Graf return err; 4693461d8837SThomas Graf 469419b1518cSDavid Ahern memset(&cfg, 0, sizeof(cfg)); 469519b1518cSDavid Ahern 4696461d8837SThomas Graf ifm = nlmsg_data(nlh); 469719b1518cSDavid Ahern cfg.pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); 469819b1518cSDavid Ahern if (!cfg.pfx) 46991da177e4SLinus Torvalds return -EINVAL; 47001da177e4SLinus Torvalds 470119b1518cSDavid Ahern cfg.peer_pfx = peer_pfx; 470219b1518cSDavid Ahern cfg.plen = ifm->ifa_prefixlen; 47038308f3ffSDavid Ahern if (tb[IFA_RT_PRIORITY]) 47048308f3ffSDavid Ahern cfg.rt_priority = nla_get_u32(tb[IFA_RT_PRIORITY]); 47058308f3ffSDavid Ahern 470619b1518cSDavid Ahern cfg.valid_lft = INFINITY_LIFE_TIME; 470719b1518cSDavid Ahern cfg.preferred_lft = INFINITY_LIFE_TIME; 470819b1518cSDavid Ahern 4709461d8837SThomas Graf if (tb[IFA_CACHEINFO]) { 47100778769dSNoriaki TAKAMIYA struct ifa_cacheinfo *ci; 4711461d8837SThomas Graf 4712461d8837SThomas Graf ci = nla_data(tb[IFA_CACHEINFO]); 471319b1518cSDavid Ahern cfg.valid_lft = ci->ifa_valid; 471419b1518cSDavid Ahern cfg.preferred_lft = ci->ifa_prefered; 47150778769dSNoriaki TAKAMIYA } 47160778769dSNoriaki TAKAMIYA 4717af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifm->ifa_index); 471863159f29SIan Morris if (!dev) 47197198f8ceSThomas Graf return -ENODEV; 4720081bba5bSNoriaki TAKAMIYA 472119b1518cSDavid Ahern if (tb[IFA_FLAGS]) 472219b1518cSDavid Ahern cfg.ifa_flags = nla_get_u32(tb[IFA_FLAGS]); 472319b1518cSDavid Ahern else 472419b1518cSDavid Ahern cfg.ifa_flags = ifm->ifa_flags; 4725479840ffSJiri Pirko 472655ebaef1SNoriaki TAKAMIYA /* We ignore other flags so far. */ 472719b1518cSDavid Ahern cfg.ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | 472819b1518cSDavid Ahern IFA_F_MANAGETEMPADDR | IFA_F_NOPREFIXROUTE | 472919b1518cSDavid Ahern IFA_F_MCAUTOJOIN | IFA_F_OPTIMISTIC; 4730f1c02cfbSSabrina Dubroca 4731f1c02cfbSSabrina Dubroca idev = ipv6_find_idev(dev); 4732f1c02cfbSSabrina Dubroca if (IS_ERR(idev)) 4733f1c02cfbSSabrina Dubroca return PTR_ERR(idev); 4734f1c02cfbSSabrina Dubroca 4735f1c02cfbSSabrina Dubroca if (!ipv6_allow_optimistic_dad(net, idev)) 473619b1518cSDavid Ahern cfg.ifa_flags &= ~IFA_F_OPTIMISTIC; 4737f1c02cfbSSabrina Dubroca 473819b1518cSDavid Ahern if (cfg.ifa_flags & IFA_F_NODAD && 473919b1518cSDavid Ahern cfg.ifa_flags & IFA_F_OPTIMISTIC) { 4740f1c02cfbSSabrina Dubroca NL_SET_ERR_MSG(extack, "IFA_F_NODAD and IFA_F_OPTIMISTIC are mutually exclusive"); 4741f1c02cfbSSabrina Dubroca return -EINVAL; 4742f1c02cfbSSabrina Dubroca } 474355ebaef1SNoriaki TAKAMIYA 474419b1518cSDavid Ahern ifa = ipv6_get_ifaddr(net, cfg.pfx, dev, 1); 474563159f29SIan Morris if (!ifa) { 47467198f8ceSThomas Graf /* 47477198f8ceSThomas Graf * It would be best to check for !NLM_F_CREATE here but 4748db9c7c39Sstephen hemminger * userspace already relies on not having to provide this. 47497198f8ceSThomas Graf */ 475019b1518cSDavid Ahern return inet6_addr_add(net, ifm->ifa_index, &cfg, extack); 47511da177e4SLinus Torvalds } 47521da177e4SLinus Torvalds 47537198f8ceSThomas Graf if (nlh->nlmsg_flags & NLM_F_EXCL || 47547198f8ceSThomas Graf !(nlh->nlmsg_flags & NLM_F_REPLACE)) 47557198f8ceSThomas Graf err = -EEXIST; 47567198f8ceSThomas Graf else 4757d169a1f8SDavid Ahern err = inet6_addr_modify(ifa, &cfg); 47587198f8ceSThomas Graf 47597198f8ceSThomas Graf in6_ifa_put(ifa); 47607198f8ceSThomas Graf 47617198f8ceSThomas Graf return err; 47627198f8ceSThomas Graf } 47637198f8ceSThomas Graf 4764479840ffSJiri Pirko static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags, 4765101bb229SThomas Graf u8 scope, int ifindex) 4766101bb229SThomas Graf { 4767101bb229SThomas Graf struct ifaddrmsg *ifm; 4768101bb229SThomas Graf 4769101bb229SThomas Graf ifm = nlmsg_data(nlh); 4770101bb229SThomas Graf ifm->ifa_family = AF_INET6; 4771101bb229SThomas Graf ifm->ifa_prefixlen = prefixlen; 4772101bb229SThomas Graf ifm->ifa_flags = flags; 4773101bb229SThomas Graf ifm->ifa_scope = scope; 4774101bb229SThomas Graf ifm->ifa_index = ifindex; 4775101bb229SThomas Graf } 4776101bb229SThomas Graf 477785486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp, 477885486af0SThomas Graf unsigned long tstamp, u32 preferred, u32 valid) 477985486af0SThomas Graf { 478085486af0SThomas Graf struct ifa_cacheinfo ci; 478185486af0SThomas Graf 478218a31e1eSThomas Graf ci.cstamp = cstamp_delta(cstamp); 478318a31e1eSThomas Graf ci.tstamp = cstamp_delta(tstamp); 478485486af0SThomas Graf ci.ifa_prefered = preferred; 478585486af0SThomas Graf ci.ifa_valid = valid; 478685486af0SThomas Graf 478785486af0SThomas Graf return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci); 478885486af0SThomas Graf } 478985486af0SThomas Graf 4790101bb229SThomas Graf static inline int rt_scope(int ifa_scope) 4791101bb229SThomas Graf { 4792101bb229SThomas Graf if (ifa_scope & IFA_HOST) 4793101bb229SThomas Graf return RT_SCOPE_HOST; 4794101bb229SThomas Graf else if (ifa_scope & IFA_LINK) 4795101bb229SThomas Graf return RT_SCOPE_LINK; 4796101bb229SThomas Graf else if (ifa_scope & IFA_SITE) 4797101bb229SThomas Graf return RT_SCOPE_SITE; 4798101bb229SThomas Graf else 4799101bb229SThomas Graf return RT_SCOPE_UNIVERSE; 4800101bb229SThomas Graf } 4801101bb229SThomas Graf 48020ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void) 48030ab6803bSThomas Graf { 4804339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifaddrmsg)) 4805caeaba79SNicolas Dichtel + nla_total_size(16) /* IFA_LOCAL */ 4806339bf98fSThomas Graf + nla_total_size(16) /* IFA_ADDRESS */ 4807479840ffSJiri Pirko + nla_total_size(sizeof(struct ifa_cacheinfo)) 48088308f3ffSDavid Ahern + nla_total_size(4) /* IFA_FLAGS */ 48098308f3ffSDavid Ahern + nla_total_size(4) /* IFA_RT_PRIORITY */; 48100ab6803bSThomas Graf } 4811c5396a31SYOSHIFUJI Hideaki 48126ba1e6e8SDavid Ahern enum addr_type_t { 48136ba1e6e8SDavid Ahern UNICAST_ADDR, 48146ba1e6e8SDavid Ahern MULTICAST_ADDR, 48156ba1e6e8SDavid Ahern ANYCAST_ADDR, 48166ba1e6e8SDavid Ahern }; 48176ba1e6e8SDavid Ahern 4818203651b6SChristian Brauner struct inet6_fill_args { 4819203651b6SChristian Brauner u32 portid; 4820203651b6SChristian Brauner u32 seq; 4821203651b6SChristian Brauner int event; 4822203651b6SChristian Brauner unsigned int flags; 4823203651b6SChristian Brauner int netnsid; 48246ba1e6e8SDavid Ahern enum addr_type_t type; 4825203651b6SChristian Brauner }; 4826203651b6SChristian Brauner 48271da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa, 4828203651b6SChristian Brauner struct inet6_fill_args *args) 48291da177e4SLinus Torvalds { 48301da177e4SLinus Torvalds struct nlmsghdr *nlh; 483185486af0SThomas Graf u32 preferred, valid; 48321da177e4SLinus Torvalds 4833203651b6SChristian Brauner nlh = nlmsg_put(skb, args->portid, args->seq, args->event, 4834203651b6SChristian Brauner sizeof(struct ifaddrmsg), args->flags); 483563159f29SIan Morris if (!nlh) 483626932566SPatrick McHardy return -EMSGSIZE; 48370ab6803bSThomas Graf 4838101bb229SThomas Graf put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope), 4839101bb229SThomas Graf ifa->idev->dev->ifindex); 4840101bb229SThomas Graf 4841203651b6SChristian Brauner if (args->netnsid >= 0 && 4842203651b6SChristian Brauner nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) 48436ecf4c37SChristian Brauner goto error; 48446ecf4c37SChristian Brauner 4845fad8da3eSYasushi Asano if (!((ifa->flags&IFA_F_PERMANENT) && 4846fad8da3eSYasushi Asano (ifa->prefered_lft == INFINITY_LIFE_TIME))) { 484785486af0SThomas Graf preferred = ifa->prefered_lft; 484885486af0SThomas Graf valid = ifa->valid_lft; 484985486af0SThomas Graf if (preferred != INFINITY_LIFE_TIME) { 48501da177e4SLinus Torvalds long tval = (jiffies - ifa->tstamp)/HZ; 4851a1faa698SJens Rosenboom if (preferred > tval) 485285486af0SThomas Graf preferred -= tval; 4853a1faa698SJens Rosenboom else 4854a1faa698SJens Rosenboom preferred = 0; 4855f56619fcSBen Hutchings if (valid != INFINITY_LIFE_TIME) { 4856f56619fcSBen Hutchings if (valid > tval) 485785486af0SThomas Graf valid -= tval; 4858f56619fcSBen Hutchings else 4859f56619fcSBen Hutchings valid = 0; 4860f56619fcSBen Hutchings } 48611da177e4SLinus Torvalds } 48621da177e4SLinus Torvalds } else { 486385486af0SThomas Graf preferred = INFINITY_LIFE_TIME; 486485486af0SThomas Graf valid = INFINITY_LIFE_TIME; 48651da177e4SLinus Torvalds } 486685486af0SThomas Graf 48677996c799SCong Wang if (!ipv6_addr_any(&ifa->peer_addr)) { 4868930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 || 4869930345eaSJiri Benc nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0) 4870caeaba79SNicolas Dichtel goto error; 4871caeaba79SNicolas Dichtel } else 4872930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0) 4873caeaba79SNicolas Dichtel goto error; 4874caeaba79SNicolas Dichtel 48758308f3ffSDavid Ahern if (ifa->rt_priority && 48768308f3ffSDavid Ahern nla_put_u32(skb, IFA_RT_PRIORITY, ifa->rt_priority)) 48778308f3ffSDavid Ahern goto error; 48788308f3ffSDavid Ahern 4879caeaba79SNicolas Dichtel if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) 4880caeaba79SNicolas Dichtel goto error; 488185486af0SThomas Graf 4882479840ffSJiri Pirko if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0) 4883479840ffSJiri Pirko goto error; 4884479840ffSJiri Pirko 4885053c095aSJohannes Berg nlmsg_end(skb, nlh); 4886053c095aSJohannes Berg return 0; 4887caeaba79SNicolas Dichtel 4888caeaba79SNicolas Dichtel error: 4889caeaba79SNicolas Dichtel nlmsg_cancel(skb, nlh); 4890caeaba79SNicolas Dichtel return -EMSGSIZE; 48911da177e4SLinus Torvalds } 48921da177e4SLinus Torvalds 48931da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca, 4894203651b6SChristian Brauner struct inet6_fill_args *args) 48951da177e4SLinus Torvalds { 48961da177e4SLinus Torvalds struct nlmsghdr *nlh; 4897101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 4898101bb229SThomas Graf int ifindex = ifmca->idev->dev->ifindex; 48991da177e4SLinus Torvalds 49001da177e4SLinus Torvalds if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE) 4901101bb229SThomas Graf scope = RT_SCOPE_SITE; 4902101bb229SThomas Graf 4903203651b6SChristian Brauner nlh = nlmsg_put(skb, args->portid, args->seq, args->event, 4904203651b6SChristian Brauner sizeof(struct ifaddrmsg), args->flags); 490563159f29SIan Morris if (!nlh) 490626932566SPatrick McHardy return -EMSGSIZE; 49070ab6803bSThomas Graf 4908203651b6SChristian Brauner if (args->netnsid >= 0 && 4909203651b6SChristian Brauner nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) 49106ecf4c37SChristian Brauner return -EMSGSIZE; 49116ecf4c37SChristian Brauner 4912101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 4913930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 || 49140ab6803bSThomas Graf put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp, 491526932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 491626932566SPatrick McHardy nlmsg_cancel(skb, nlh); 491726932566SPatrick McHardy return -EMSGSIZE; 491826932566SPatrick McHardy } 491985486af0SThomas Graf 4920053c095aSJohannes Berg nlmsg_end(skb, nlh); 4921053c095aSJohannes Berg return 0; 49221da177e4SLinus Torvalds } 49231da177e4SLinus Torvalds 49241da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca, 4925203651b6SChristian Brauner struct inet6_fill_args *args) 49261da177e4SLinus Torvalds { 49279ee8cbb2SDavid Ahern struct net_device *dev = fib6_info_nh_dev(ifaca->aca_rt); 49289ee8cbb2SDavid Ahern int ifindex = dev ? dev->ifindex : 1; 49291da177e4SLinus Torvalds struct nlmsghdr *nlh; 4930101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 49311da177e4SLinus Torvalds 49321da177e4SLinus Torvalds if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE) 4933101bb229SThomas Graf scope = RT_SCOPE_SITE; 4934101bb229SThomas Graf 4935203651b6SChristian Brauner nlh = nlmsg_put(skb, args->portid, args->seq, args->event, 4936203651b6SChristian Brauner sizeof(struct ifaddrmsg), args->flags); 493763159f29SIan Morris if (!nlh) 493826932566SPatrick McHardy return -EMSGSIZE; 49390ab6803bSThomas Graf 4940203651b6SChristian Brauner if (args->netnsid >= 0 && 4941203651b6SChristian Brauner nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) 49426ecf4c37SChristian Brauner return -EMSGSIZE; 49436ecf4c37SChristian Brauner 4944101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 4945930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 || 49460ab6803bSThomas Graf put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp, 494726932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 494826932566SPatrick McHardy nlmsg_cancel(skb, nlh); 494926932566SPatrick McHardy return -EMSGSIZE; 495026932566SPatrick McHardy } 495185486af0SThomas Graf 4952053c095aSJohannes Berg nlmsg_end(skb, nlh); 4953053c095aSJohannes Berg return 0; 49541da177e4SLinus Torvalds } 49551da177e4SLinus Torvalds 4956234b27c3SEric Dumazet /* called with rcu_read_lock() */ 4957234b27c3SEric Dumazet static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb, 49586ba1e6e8SDavid Ahern struct netlink_callback *cb, 49596ba1e6e8SDavid Ahern int s_ip_idx, int *p_ip_idx, 49606ba1e6e8SDavid Ahern struct inet6_fill_args *fillargs) 49611da177e4SLinus Torvalds { 49621da177e4SLinus Torvalds struct ifmcaddr6 *ifmca; 49631da177e4SLinus Torvalds struct ifacaddr6 *ifaca; 4964234b27c3SEric Dumazet int err = 1; 4965234b27c3SEric Dumazet int ip_idx = *p_ip_idx; 49661da177e4SLinus Torvalds 49671da177e4SLinus Torvalds read_lock_bh(&idev->lock); 49686ba1e6e8SDavid Ahern switch (fillargs->type) { 4969502a2ffdSstephen hemminger case UNICAST_ADDR: { 4970502a2ffdSstephen hemminger struct inet6_ifaddr *ifa; 49716ba1e6e8SDavid Ahern fillargs->event = RTM_NEWADDR; 4972502a2ffdSstephen hemminger 4973ae9cda5dSYOSHIFUJI Hideaki /* unicast address incl. temp addr */ 4974502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 4975502a2ffdSstephen hemminger if (++ip_idx < s_ip_idx) 49761da177e4SLinus Torvalds continue; 49776ba1e6e8SDavid Ahern err = inet6_fill_ifaddr(skb, ifa, fillargs); 4978053c095aSJohannes Berg if (err < 0) 4979bcd32326SEric Dumazet break; 498063998ac2SNicolas Dichtel nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 49811da177e4SLinus Torvalds } 49821da177e4SLinus Torvalds break; 4983502a2ffdSstephen hemminger } 49841da177e4SLinus Torvalds case MULTICAST_ADDR: 49856ba1e6e8SDavid Ahern fillargs->event = RTM_GETMULTICAST; 4986203651b6SChristian Brauner 49871da177e4SLinus Torvalds /* multicast address */ 49881da177e4SLinus Torvalds for (ifmca = idev->mc_list; ifmca; 49891da177e4SLinus Torvalds ifmca = ifmca->next, ip_idx++) { 49901da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 49911da177e4SLinus Torvalds continue; 49926ba1e6e8SDavid Ahern err = inet6_fill_ifmcaddr(skb, ifmca, fillargs); 4993053c095aSJohannes Berg if (err < 0) 4994bcd32326SEric Dumazet break; 49951da177e4SLinus Torvalds } 49961da177e4SLinus Torvalds break; 49971da177e4SLinus Torvalds case ANYCAST_ADDR: 49986ba1e6e8SDavid Ahern fillargs->event = RTM_GETANYCAST; 49991da177e4SLinus Torvalds /* anycast address */ 50001da177e4SLinus Torvalds for (ifaca = idev->ac_list; ifaca; 50011da177e4SLinus Torvalds ifaca = ifaca->aca_next, ip_idx++) { 50021da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 50031da177e4SLinus Torvalds continue; 50046ba1e6e8SDavid Ahern err = inet6_fill_ifacaddr(skb, ifaca, fillargs); 5005053c095aSJohannes Berg if (err < 0) 5006bcd32326SEric Dumazet break; 50071da177e4SLinus Torvalds } 50081da177e4SLinus Torvalds break; 50091da177e4SLinus Torvalds default: 50101da177e4SLinus Torvalds break; 50111da177e4SLinus Torvalds } 50121da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 5013234b27c3SEric Dumazet *p_ip_idx = ip_idx; 5014234b27c3SEric Dumazet return err; 5015234b27c3SEric Dumazet } 50165d5619b4SYOSHIFUJI Hideaki 5017ed6eff11SDavid Ahern static int inet6_valid_dump_ifaddr_req(const struct nlmsghdr *nlh, 5018ed6eff11SDavid Ahern struct inet6_fill_args *fillargs, 5019ed6eff11SDavid Ahern struct net **tgt_net, struct sock *sk, 5020ed6eff11SDavid Ahern struct netlink_ext_ack *extack) 5021ed6eff11SDavid Ahern { 5022ed6eff11SDavid Ahern struct nlattr *tb[IFA_MAX+1]; 5023ed6eff11SDavid Ahern struct ifaddrmsg *ifm; 5024ed6eff11SDavid Ahern int err, i; 5025ed6eff11SDavid Ahern 5026ed6eff11SDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 5027ed6eff11SDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Invalid header for address dump request"); 5028ed6eff11SDavid Ahern return -EINVAL; 5029ed6eff11SDavid Ahern } 5030ed6eff11SDavid Ahern 5031ed6eff11SDavid Ahern ifm = nlmsg_data(nlh); 5032ed6eff11SDavid Ahern if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) { 5033ed6eff11SDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for address dump request"); 5034ed6eff11SDavid Ahern return -EINVAL; 5035ed6eff11SDavid Ahern } 5036ed6eff11SDavid Ahern if (ifm->ifa_index) { 5037ed6eff11SDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Filter by device index not supported for address dump"); 5038ed6eff11SDavid Ahern return -EINVAL; 5039ed6eff11SDavid Ahern } 5040ed6eff11SDavid Ahern 5041ed6eff11SDavid Ahern err = nlmsg_parse_strict(nlh, sizeof(*ifm), tb, IFA_MAX, 5042ed6eff11SDavid Ahern ifa_ipv6_policy, extack); 5043ed6eff11SDavid Ahern if (err < 0) 5044ed6eff11SDavid Ahern return err; 5045ed6eff11SDavid Ahern 5046ed6eff11SDavid Ahern for (i = 0; i <= IFA_MAX; ++i) { 5047ed6eff11SDavid Ahern if (!tb[i]) 5048ed6eff11SDavid Ahern continue; 5049ed6eff11SDavid Ahern 5050ed6eff11SDavid Ahern if (i == IFA_TARGET_NETNSID) { 5051ed6eff11SDavid Ahern struct net *net; 5052ed6eff11SDavid Ahern 5053ed6eff11SDavid Ahern fillargs->netnsid = nla_get_s32(tb[i]); 5054ed6eff11SDavid Ahern net = rtnl_get_net_ns_capable(sk, fillargs->netnsid); 5055ed6eff11SDavid Ahern if (IS_ERR(net)) { 5056ed6eff11SDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Invalid target network namespace id"); 5057ed6eff11SDavid Ahern return PTR_ERR(net); 5058ed6eff11SDavid Ahern } 5059ed6eff11SDavid Ahern *tgt_net = net; 5060ed6eff11SDavid Ahern } else { 5061ed6eff11SDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in dump request"); 5062ed6eff11SDavid Ahern return -EINVAL; 5063ed6eff11SDavid Ahern } 5064ed6eff11SDavid Ahern } 5065ed6eff11SDavid Ahern 5066ed6eff11SDavid Ahern return 0; 5067ed6eff11SDavid Ahern } 5068ed6eff11SDavid Ahern 5069234b27c3SEric Dumazet static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb, 5070234b27c3SEric Dumazet enum addr_type_t type) 5071234b27c3SEric Dumazet { 5072ed6eff11SDavid Ahern const struct nlmsghdr *nlh = cb->nlh; 50736ba1e6e8SDavid Ahern struct inet6_fill_args fillargs = { 50746ba1e6e8SDavid Ahern .portid = NETLINK_CB(cb->skb).portid, 50756ba1e6e8SDavid Ahern .seq = cb->nlh->nlmsg_seq, 50766ba1e6e8SDavid Ahern .flags = NLM_F_MULTI, 50776ba1e6e8SDavid Ahern .netnsid = -1, 50786ba1e6e8SDavid Ahern .type = type, 50796ba1e6e8SDavid Ahern }; 5080234b27c3SEric Dumazet struct net *net = sock_net(skb->sk); 50816ecf4c37SChristian Brauner struct net *tgt_net = net; 5082234b27c3SEric Dumazet int h, s_h; 5083234b27c3SEric Dumazet int idx, ip_idx; 5084234b27c3SEric Dumazet int s_idx, s_ip_idx; 5085234b27c3SEric Dumazet struct net_device *dev; 5086234b27c3SEric Dumazet struct inet6_dev *idev; 5087234b27c3SEric Dumazet struct hlist_head *head; 5088234b27c3SEric Dumazet 5089234b27c3SEric Dumazet s_h = cb->args[0]; 5090234b27c3SEric Dumazet s_idx = idx = cb->args[1]; 5091234b27c3SEric Dumazet s_ip_idx = ip_idx = cb->args[2]; 5092234b27c3SEric Dumazet 5093ed6eff11SDavid Ahern if (cb->strict_check) { 5094ed6eff11SDavid Ahern int err; 50956ecf4c37SChristian Brauner 5096ed6eff11SDavid Ahern err = inet6_valid_dump_ifaddr_req(nlh, &fillargs, &tgt_net, 5097ed6eff11SDavid Ahern skb->sk, cb->extack); 5098ed6eff11SDavid Ahern if (err < 0) 5099ed6eff11SDavid Ahern return err; 51006ecf4c37SChristian Brauner } 51016ecf4c37SChristian Brauner 5102234b27c3SEric Dumazet rcu_read_lock(); 51036ecf4c37SChristian Brauner cb->seq = atomic_read(&tgt_net->ipv6.dev_addr_genid) ^ tgt_net->dev_base_seq; 5104234b27c3SEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 5105234b27c3SEric Dumazet idx = 0; 51066ecf4c37SChristian Brauner head = &tgt_net->dev_index_head[h]; 5107b67bfe0dSSasha Levin hlist_for_each_entry_rcu(dev, head, index_hlist) { 5108234b27c3SEric Dumazet if (idx < s_idx) 5109234b27c3SEric Dumazet goto cont; 51104b97efdfSPatrick McHardy if (h > s_h || idx > s_idx) 5111234b27c3SEric Dumazet s_ip_idx = 0; 5112234b27c3SEric Dumazet ip_idx = 0; 5113e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 5114e21e8467SStephen Hemminger if (!idev) 5115234b27c3SEric Dumazet goto cont; 5116234b27c3SEric Dumazet 51176ba1e6e8SDavid Ahern if (in6_dump_addrs(idev, skb, cb, s_ip_idx, &ip_idx, 51186ba1e6e8SDavid Ahern &fillargs) < 0) 5119234b27c3SEric Dumazet goto done; 51207562f876SPavel Emelianov cont: 51217562f876SPavel Emelianov idx++; 51221da177e4SLinus Torvalds } 5123234b27c3SEric Dumazet } 5124234b27c3SEric Dumazet done: 5125234b27c3SEric Dumazet rcu_read_unlock(); 5126234b27c3SEric Dumazet cb->args[0] = h; 5127234b27c3SEric Dumazet cb->args[1] = idx; 5128234b27c3SEric Dumazet cb->args[2] = ip_idx; 51296ba1e6e8SDavid Ahern if (fillargs.netnsid >= 0) 51306ecf4c37SChristian Brauner put_net(tgt_net); 5131234b27c3SEric Dumazet 51321da177e4SLinus Torvalds return skb->len; 51331da177e4SLinus Torvalds } 51341da177e4SLinus Torvalds 51351da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) 51361da177e4SLinus Torvalds { 51371da177e4SLinus Torvalds enum addr_type_t type = UNICAST_ADDR; 5138b854272bSDenis V. Lunev 51391da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 51401da177e4SLinus Torvalds } 51411da177e4SLinus Torvalds 51421da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb) 51431da177e4SLinus Torvalds { 51441da177e4SLinus Torvalds enum addr_type_t type = MULTICAST_ADDR; 5145b854272bSDenis V. Lunev 51461da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 51471da177e4SLinus Torvalds } 51481da177e4SLinus Torvalds 51491da177e4SLinus Torvalds 51501da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb) 51511da177e4SLinus Torvalds { 51521da177e4SLinus Torvalds enum addr_type_t type = ANYCAST_ADDR; 5153b854272bSDenis V. Lunev 51541da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 51551da177e4SLinus Torvalds } 51561da177e4SLinus Torvalds 5157c21ef3e3SDavid Ahern static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh, 5158c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 51596c223828SNoriaki TAKAMIYA { 51603b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 5161203651b6SChristian Brauner struct inet6_fill_args fillargs = { 5162203651b6SChristian Brauner .portid = NETLINK_CB(in_skb).portid, 5163203651b6SChristian Brauner .seq = nlh->nlmsg_seq, 5164203651b6SChristian Brauner .event = RTM_NEWADDR, 5165203651b6SChristian Brauner .flags = 0, 5166203651b6SChristian Brauner .netnsid = -1, 5167203651b6SChristian Brauner }; 51686ecf4c37SChristian Brauner struct net *tgt_net = net; 51691b29fc2cSThomas Graf struct ifaddrmsg *ifm; 51701b29fc2cSThomas Graf struct nlattr *tb[IFA_MAX+1]; 5171caeaba79SNicolas Dichtel struct in6_addr *addr = NULL, *peer; 51726c223828SNoriaki TAKAMIYA struct net_device *dev = NULL; 51736c223828SNoriaki TAKAMIYA struct inet6_ifaddr *ifa; 51746c223828SNoriaki TAKAMIYA struct sk_buff *skb; 51756c223828SNoriaki TAKAMIYA int err; 51766c223828SNoriaki TAKAMIYA 5177fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy, 5178c21ef3e3SDavid Ahern extack); 51791b29fc2cSThomas Graf if (err < 0) 5180c24675f8SFlorian Westphal return err; 51816c223828SNoriaki TAKAMIYA 51826ecf4c37SChristian Brauner if (tb[IFA_TARGET_NETNSID]) { 5183203651b6SChristian Brauner fillargs.netnsid = nla_get_s32(tb[IFA_TARGET_NETNSID]); 51846ecf4c37SChristian Brauner 51856ecf4c37SChristian Brauner tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(in_skb).sk, 5186203651b6SChristian Brauner fillargs.netnsid); 51876ecf4c37SChristian Brauner if (IS_ERR(tgt_net)) 51886ecf4c37SChristian Brauner return PTR_ERR(tgt_net); 51896ecf4c37SChristian Brauner } 51906ecf4c37SChristian Brauner 5191caeaba79SNicolas Dichtel addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer); 5192c24675f8SFlorian Westphal if (!addr) 5193c24675f8SFlorian Westphal return -EINVAL; 51941b29fc2cSThomas Graf 51951b29fc2cSThomas Graf ifm = nlmsg_data(nlh); 51966c223828SNoriaki TAKAMIYA if (ifm->ifa_index) 51976ecf4c37SChristian Brauner dev = dev_get_by_index(tgt_net, ifm->ifa_index); 51986c223828SNoriaki TAKAMIYA 51996ecf4c37SChristian Brauner ifa = ipv6_get_ifaddr(tgt_net, addr, dev, 1); 5200e21e8467SStephen Hemminger if (!ifa) { 52011b29fc2cSThomas Graf err = -EADDRNOTAVAIL; 52021b29fc2cSThomas Graf goto errout; 52036c223828SNoriaki TAKAMIYA } 52046c223828SNoriaki TAKAMIYA 5205e21e8467SStephen Hemminger skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL); 5206e21e8467SStephen Hemminger if (!skb) { 52071b29fc2cSThomas Graf err = -ENOBUFS; 52081b29fc2cSThomas Graf goto errout_ifa; 52091b29fc2cSThomas Graf } 52101b29fc2cSThomas Graf 5211203651b6SChristian Brauner err = inet6_fill_ifaddr(skb, ifa, &fillargs); 521226932566SPatrick McHardy if (err < 0) { 521326932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 521426932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 521526932566SPatrick McHardy kfree_skb(skb); 521626932566SPatrick McHardy goto errout_ifa; 521726932566SPatrick McHardy } 52186ecf4c37SChristian Brauner err = rtnl_unicast(skb, tgt_net, NETLINK_CB(in_skb).portid); 52191b29fc2cSThomas Graf errout_ifa: 52206c223828SNoriaki TAKAMIYA in6_ifa_put(ifa); 52211b29fc2cSThomas Graf errout: 5222c24675f8SFlorian Westphal if (dev) 5223c24675f8SFlorian Westphal dev_put(dev); 5224203651b6SChristian Brauner if (fillargs.netnsid >= 0) 52256ecf4c37SChristian Brauner put_net(tgt_net); 52266ecf4c37SChristian Brauner 52276c223828SNoriaki TAKAMIYA return err; 52286c223828SNoriaki TAKAMIYA } 52296c223828SNoriaki TAKAMIYA 52301da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa) 52311da177e4SLinus Torvalds { 52321da177e4SLinus Torvalds struct sk_buff *skb; 5233c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(ifa->idev->dev); 5234203651b6SChristian Brauner struct inet6_fill_args fillargs = { 5235203651b6SChristian Brauner .portid = 0, 5236203651b6SChristian Brauner .seq = 0, 5237203651b6SChristian Brauner .event = event, 5238203651b6SChristian Brauner .flags = 0, 5239203651b6SChristian Brauner .netnsid = -1, 5240203651b6SChristian Brauner }; 52415d620266SThomas Graf int err = -ENOBUFS; 52421da177e4SLinus Torvalds 52430ab6803bSThomas Graf skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC); 524463159f29SIan Morris if (!skb) 52455d620266SThomas Graf goto errout; 52465d620266SThomas Graf 5247203651b6SChristian Brauner err = inet6_fill_ifaddr(skb, ifa, &fillargs); 524826932566SPatrick McHardy if (err < 0) { 524926932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 525026932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 525126932566SPatrick McHardy kfree_skb(skb); 525226932566SPatrick McHardy goto errout; 525326932566SPatrick McHardy } 52541ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC); 52551ce85fe4SPablo Neira Ayuso return; 52565d620266SThomas Graf errout: 52575d620266SThomas Graf if (err < 0) 52586fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err); 52591da177e4SLinus Torvalds } 52601da177e4SLinus Torvalds 5261b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf, 52621da177e4SLinus Torvalds __s32 *array, int bytes) 52631da177e4SLinus Torvalds { 526404561c1fSThomas Graf BUG_ON(bytes < (DEVCONF_MAX * 4)); 526504561c1fSThomas Graf 52661da177e4SLinus Torvalds memset(array, 0, bytes); 52671da177e4SLinus Torvalds array[DEVCONF_FORWARDING] = cnf->forwarding; 52681da177e4SLinus Torvalds array[DEVCONF_HOPLIMIT] = cnf->hop_limit; 52691da177e4SLinus Torvalds array[DEVCONF_MTU6] = cnf->mtu6; 52701da177e4SLinus Torvalds array[DEVCONF_ACCEPT_RA] = cnf->accept_ra; 52711da177e4SLinus Torvalds array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects; 52721da177e4SLinus Torvalds array[DEVCONF_AUTOCONF] = cnf->autoconf; 52731da177e4SLinus Torvalds array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits; 52741da177e4SLinus Torvalds array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits; 527593908d19SThomas Graf array[DEVCONF_RTR_SOLICIT_INTERVAL] = 527693908d19SThomas Graf jiffies_to_msecs(cnf->rtr_solicit_interval); 5277bd11f074SMaciej Żenczykowski array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] = 5278bd11f074SMaciej Żenczykowski jiffies_to_msecs(cnf->rtr_solicit_max_interval); 527993908d19SThomas Graf array[DEVCONF_RTR_SOLICIT_DELAY] = 528093908d19SThomas Graf jiffies_to_msecs(cnf->rtr_solicit_delay); 52811da177e4SLinus Torvalds array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version; 5282fc4eba58SHannes Frederic Sowa array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] = 5283fc4eba58SHannes Frederic Sowa jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval); 5284fc4eba58SHannes Frederic Sowa array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] = 5285fc4eba58SHannes Frederic Sowa jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval); 52861da177e4SLinus Torvalds array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr; 52871da177e4SLinus Torvalds array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft; 52881da177e4SLinus Torvalds array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft; 52891da177e4SLinus Torvalds array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry; 52901da177e4SLinus Torvalds array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor; 52911da177e4SLinus Torvalds array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses; 529265f5c7c1SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr; 52938013d1d7SHangbin Liu array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit; 5294c4fd30ebSYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo; 5295930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 5296930d6ff2SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref; 529793908d19SThomas Graf array[DEVCONF_RTR_PROBE_INTERVAL] = 529893908d19SThomas Graf jiffies_to_msecs(cnf->rtr_probe_interval); 5299fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 5300bbea124bSJoel Scherpelz array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] = cnf->accept_ra_rt_info_min_plen; 530109c884d4SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen; 530209c884d4SYOSHIFUJI Hideaki #endif 5303930d6ff2SYOSHIFUJI Hideaki #endif 5304fbea49e1SYOSHIFUJI Hideaki array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp; 53050bcbc926SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route; 530695c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 530795c385b4SNeil Horman array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad; 53087fd2561eSErik Kline array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic; 530995c385b4SNeil Horman #endif 53107bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 53117bc570c8SYOSHIFUJI Hideaki array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding; 53127bc570c8SYOSHIFUJI Hideaki #endif 5313778d80beSYOSHIFUJI Hideaki array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6; 53141b34be74SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad; 5315c3faca05SCosmin Ratiu array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao; 53165cb04436SHannes Frederic Sowa array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify; 5317b800c3b9SHannes Frederic Sowa array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc; 5318d9333196SBen Greear array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local; 5319c2943f14SHarout Hedeshian array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu; 532035103d11SAndy Gospodarek array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown; 53213d1bec99SHannes Frederic Sowa /* we omit DEVCONF_STABLE_SECRET for now */ 53223985e8a3SErik Kline array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only; 5323abbc3043SJohannes Berg array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast; 53247a02bf89SJohannes Berg array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na; 5325f1705ec1SDavid Ahern array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down; 53261ababebaSDavid Lebrun array[DEVCONF_SEG6_ENABLED] = cnf->seg6_enabled; 5327bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC 5328bf355b8dSDavid Lebrun array[DEVCONF_SEG6_REQUIRE_HMAC] = cnf->seg6_require_hmac; 5329bf355b8dSDavid Lebrun #endif 5330adc176c5SErik Nordmark array[DEVCONF_ENHANCED_DAD] = cnf->enhanced_dad; 5331d35a00b8SFelix Jia array[DEVCONF_ADDR_GEN_MODE] = cnf->addr_gen_mode; 5332df789fe7SDavid Forster array[DEVCONF_DISABLE_POLICY] = cnf->disable_policy; 53332210d6b2SMaciej Żenczykowski array[DEVCONF_NDISC_TCLASS] = cnf->ndisc_tclass; 53341da177e4SLinus Torvalds } 53351da177e4SLinus Torvalds 5336b382b191SThomas Graf static inline size_t inet6_ifla6_size(void) 5337b382b191SThomas Graf { 5338b382b191SThomas Graf return nla_total_size(4) /* IFLA_INET6_FLAGS */ 5339b382b191SThomas Graf + nla_total_size(sizeof(struct ifla_cacheinfo)) 5340b382b191SThomas Graf + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */ 5341b382b191SThomas Graf + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */ 5342f53adae4SDaniel Borkmann + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */ 5343bdd72f41SSabrina Dubroca + nla_total_size(sizeof(struct in6_addr)) /* IFLA_INET6_TOKEN */ 5344bdd72f41SSabrina Dubroca + nla_total_size(1) /* IFLA_INET6_ADDR_GEN_MODE */ 5345bdd72f41SSabrina Dubroca + 0; 5346b382b191SThomas Graf } 5347b382b191SThomas Graf 5348339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void) 5349339bf98fSThomas Graf { 5350339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 5351339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 5352339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 5353339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 5354339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 53550344338bSAndy Gospodarek + nla_total_size(1) /* IFLA_OPERSTATE */ 5356b382b191SThomas Graf + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */ 5357339bf98fSThomas Graf } 5358c5396a31SYOSHIFUJI Hideaki 5359be281e55SEric Dumazet static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib, 5360aca05671SJia He int bytes) 53617f7d9a6bSHerbert Xu { 53627f7d9a6bSHerbert Xu int i; 5363aca05671SJia He int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX; 53647f7d9a6bSHerbert Xu BUG_ON(pad < 0); 53657f7d9a6bSHerbert Xu 53667f7d9a6bSHerbert Xu /* Use put_unaligned() because stats may not be aligned for u64. */ 5367aca05671SJia He put_unaligned(ICMP6_MIB_MAX, &stats[0]); 5368aca05671SJia He for (i = 1; i < ICMP6_MIB_MAX; i++) 5369be281e55SEric Dumazet put_unaligned(atomic_long_read(&mib[i]), &stats[i]); 53707f7d9a6bSHerbert Xu 5371aca05671SJia He memset(&stats[ICMP6_MIB_MAX], 0, pad); 53727f7d9a6bSHerbert Xu } 53737f7d9a6bSHerbert Xu 5374698365faSWANG Cong static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib, 5375a3a77372SRaghavendra K T int bytes, size_t syncpoff) 53764ce3c183SEric Dumazet { 5377a3a77372SRaghavendra K T int i, c; 5378a3a77372SRaghavendra K T u64 buff[IPSTATS_MIB_MAX]; 5379a3a77372SRaghavendra K T int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX; 5380a3a77372SRaghavendra K T 53814ce3c183SEric Dumazet BUG_ON(pad < 0); 53824ce3c183SEric Dumazet 5383a3a77372SRaghavendra K T memset(buff, 0, sizeof(buff)); 5384a3a77372SRaghavendra K T buff[0] = IPSTATS_MIB_MAX; 53854ce3c183SEric Dumazet 5386a3a77372SRaghavendra K T for_each_possible_cpu(c) { 5387a3a77372SRaghavendra K T for (i = 1; i < IPSTATS_MIB_MAX; i++) 5388a3a77372SRaghavendra K T buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff); 5389a3a77372SRaghavendra K T } 5390a3a77372SRaghavendra K T 5391a3a77372SRaghavendra K T memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64)); 5392a3a77372SRaghavendra K T memset(&stats[IPSTATS_MIB_MAX], 0, pad); 53934ce3c183SEric Dumazet } 53944ce3c183SEric Dumazet 53957f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype, 53967f7d9a6bSHerbert Xu int bytes) 53977f7d9a6bSHerbert Xu { 53987f7d9a6bSHerbert Xu switch (attrtype) { 53997f7d9a6bSHerbert Xu case IFLA_INET6_STATS: 5400a3a77372SRaghavendra K T __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes, 5401a3a77372SRaghavendra K T offsetof(struct ipstats_mib, syncp)); 54027f7d9a6bSHerbert Xu break; 54037f7d9a6bSHerbert Xu case IFLA_INET6_ICMP6STATS: 5404aca05671SJia He __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes); 54057f7d9a6bSHerbert Xu break; 54067f7d9a6bSHerbert Xu } 54077f7d9a6bSHerbert Xu } 54087f7d9a6bSHerbert Xu 5409d5566fd7SSowmini Varadhan static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev, 5410d5566fd7SSowmini Varadhan u32 ext_filter_mask) 54111da177e4SLinus Torvalds { 5412bf99f1bdSYOSHIFUJI Hideaki struct nlattr *nla; 54131da177e4SLinus Torvalds struct ifla_cacheinfo ci; 54141da177e4SLinus Torvalds 5415c78679e8SDavid S. Miller if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags)) 5416c78679e8SDavid S. Miller goto nla_put_failure; 54171da177e4SLinus Torvalds ci.max_reasm_len = IPV6_MAXPLEN; 541824912420SDavid S. Miller ci.tstamp = cstamp_delta(idev->tstamp); 541924912420SDavid S. Miller ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time); 54201f9248e5SJiri Pirko ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME)); 5421c78679e8SDavid S. Miller if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci)) 5422c78679e8SDavid S. Miller goto nla_put_failure; 5423bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32)); 542463159f29SIan Morris if (!nla) 542504561c1fSThomas Graf goto nla_put_failure; 5426bf99f1bdSYOSHIFUJI Hideaki ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla)); 54271da177e4SLinus Torvalds 5428bf99f1bdSYOSHIFUJI Hideaki /* XXX - MC not implemented */ 5429bf99f1bdSYOSHIFUJI Hideaki 5430d5566fd7SSowmini Varadhan if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS) 5431d5566fd7SSowmini Varadhan return 0; 5432d5566fd7SSowmini Varadhan 5433bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64)); 543463159f29SIan Morris if (!nla) 5435bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 5436bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla)); 5437bf99f1bdSYOSHIFUJI Hideaki 5438bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64)); 543963159f29SIan Morris if (!nla) 5440bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 5441bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla)); 54421da177e4SLinus Torvalds 5443f53adae4SDaniel Borkmann nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr)); 544463159f29SIan Morris if (!nla) 5445f53adae4SDaniel Borkmann goto nla_put_failure; 5446bc91b0f0SJiri Pirko 5447d35a00b8SFelix Jia if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->cnf.addr_gen_mode)) 5448bc91b0f0SJiri Pirko goto nla_put_failure; 5449bc91b0f0SJiri Pirko 5450f53adae4SDaniel Borkmann read_lock_bh(&idev->lock); 5451f53adae4SDaniel Borkmann memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla)); 5452f53adae4SDaniel Borkmann read_unlock_bh(&idev->lock); 5453f53adae4SDaniel Borkmann 5454b382b191SThomas Graf return 0; 5455b382b191SThomas Graf 5456b382b191SThomas Graf nla_put_failure: 5457b382b191SThomas Graf return -EMSGSIZE; 5458b382b191SThomas Graf } 5459b382b191SThomas Graf 5460b1974ed0SArad, Ronen static size_t inet6_get_link_af_size(const struct net_device *dev, 5461b1974ed0SArad, Ronen u32 ext_filter_mask) 5462b382b191SThomas Graf { 5463b382b191SThomas Graf if (!__in6_dev_get(dev)) 5464b382b191SThomas Graf return 0; 5465b382b191SThomas Graf 5466b382b191SThomas Graf return inet6_ifla6_size(); 5467b382b191SThomas Graf } 5468b382b191SThomas Graf 5469d5566fd7SSowmini Varadhan static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev, 5470d5566fd7SSowmini Varadhan u32 ext_filter_mask) 5471b382b191SThomas Graf { 5472b382b191SThomas Graf struct inet6_dev *idev = __in6_dev_get(dev); 5473b382b191SThomas Graf 5474b382b191SThomas Graf if (!idev) 5475b382b191SThomas Graf return -ENODATA; 5476b382b191SThomas Graf 5477d5566fd7SSowmini Varadhan if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0) 5478b382b191SThomas Graf return -EMSGSIZE; 5479b382b191SThomas Graf 5480b382b191SThomas Graf return 0; 5481b382b191SThomas Graf } 5482b382b191SThomas Graf 5483f53adae4SDaniel Borkmann static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token) 5484f53adae4SDaniel Borkmann { 5485f53adae4SDaniel Borkmann struct inet6_ifaddr *ifp; 5486f53adae4SDaniel Borkmann struct net_device *dev = idev->dev; 548747e27d5eSDaniel Borkmann bool clear_token, update_rs = false; 5488dc848292SHannes Frederic Sowa struct in6_addr ll_addr; 5489f53adae4SDaniel Borkmann 5490c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 5491c15b1ccaSHannes Frederic Sowa 549263159f29SIan Morris if (!token) 5493f53adae4SDaniel Borkmann return -EINVAL; 5494f53adae4SDaniel Borkmann if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) 5495f53adae4SDaniel Borkmann return -EINVAL; 5496f53adae4SDaniel Borkmann if (!ipv6_accept_ra(idev)) 5497f53adae4SDaniel Borkmann return -EINVAL; 5498bd11f074SMaciej Żenczykowski if (idev->cnf.rtr_solicits == 0) 5499f53adae4SDaniel Borkmann return -EINVAL; 5500f53adae4SDaniel Borkmann 5501f53adae4SDaniel Borkmann write_lock_bh(&idev->lock); 5502f53adae4SDaniel Borkmann 5503f53adae4SDaniel Borkmann BUILD_BUG_ON(sizeof(token->s6_addr) != 16); 5504f53adae4SDaniel Borkmann memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8); 5505f53adae4SDaniel Borkmann 5506f53adae4SDaniel Borkmann write_unlock_bh(&idev->lock); 5507f53adae4SDaniel Borkmann 550847e27d5eSDaniel Borkmann clear_token = ipv6_addr_any(token); 550947e27d5eSDaniel Borkmann if (clear_token) 551047e27d5eSDaniel Borkmann goto update_lft; 551147e27d5eSDaniel Borkmann 5512dc848292SHannes Frederic Sowa if (!idev->dead && (idev->if_flags & IF_READY) && 5513dc848292SHannes Frederic Sowa !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE | 5514dc848292SHannes Frederic Sowa IFA_F_OPTIMISTIC)) { 5515fc403832SDaniel Borkmann /* If we're not ready, then normal ifup will take care 5516fc403832SDaniel Borkmann * of this. Otherwise, we need to request our rs here. 5517fc403832SDaniel Borkmann */ 5518f53adae4SDaniel Borkmann ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters); 5519fc403832SDaniel Borkmann update_rs = true; 5520fc403832SDaniel Borkmann } 5521f53adae4SDaniel Borkmann 552247e27d5eSDaniel Borkmann update_lft: 5523f53adae4SDaniel Borkmann write_lock_bh(&idev->lock); 5524fc403832SDaniel Borkmann 552577ecaaceSHannes Frederic Sowa if (update_rs) { 5526f53adae4SDaniel Borkmann idev->if_flags |= IF_RS_SENT; 5527bd11f074SMaciej Żenczykowski idev->rs_interval = rfc3315_s14_backoff_init( 5528bd11f074SMaciej Żenczykowski idev->cnf.rtr_solicit_interval); 552977ecaaceSHannes Frederic Sowa idev->rs_probes = 1; 5530bd11f074SMaciej Żenczykowski addrconf_mod_rs_timer(idev, idev->rs_interval); 553177ecaaceSHannes Frederic Sowa } 5532f53adae4SDaniel Borkmann 5533f53adae4SDaniel Borkmann /* Well, that's kinda nasty ... */ 5534f53adae4SDaniel Borkmann list_for_each_entry(ifp, &idev->addr_list, if_list) { 5535f53adae4SDaniel Borkmann spin_lock(&ifp->lock); 5536617fe29dSDaniel Borkmann if (ifp->tokenized) { 5537f53adae4SDaniel Borkmann ifp->valid_lft = 0; 5538f53adae4SDaniel Borkmann ifp->prefered_lft = 0; 5539f53adae4SDaniel Borkmann } 5540f53adae4SDaniel Borkmann spin_unlock(&ifp->lock); 5541f53adae4SDaniel Borkmann } 5542f53adae4SDaniel Borkmann 5543f53adae4SDaniel Borkmann write_unlock_bh(&idev->lock); 5544b2ed64a9SLubomir Rintel inet6_ifinfo_notify(RTM_NEWLINK, idev); 5545c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 5546f53adae4SDaniel Borkmann return 0; 5547f53adae4SDaniel Borkmann } 5548f53adae4SDaniel Borkmann 554911b1f828SDaniel Borkmann static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = { 555011b1f828SDaniel Borkmann [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 }, 555111b1f828SDaniel Borkmann [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) }, 555211b1f828SDaniel Borkmann }; 555311b1f828SDaniel Borkmann 555411b1f828SDaniel Borkmann static int inet6_validate_link_af(const struct net_device *dev, 555511b1f828SDaniel Borkmann const struct nlattr *nla) 555611b1f828SDaniel Borkmann { 555711b1f828SDaniel Borkmann struct nlattr *tb[IFLA_INET6_MAX + 1]; 555811b1f828SDaniel Borkmann 555911b1f828SDaniel Borkmann if (dev && !__in6_dev_get(dev)) 556011b1f828SDaniel Borkmann return -EAFNOSUPPORT; 556111b1f828SDaniel Borkmann 5562fceb6435SJohannes Berg return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy, 5563fceb6435SJohannes Berg NULL); 556411b1f828SDaniel Borkmann } 556511b1f828SDaniel Borkmann 5566d35a00b8SFelix Jia static int check_addr_gen_mode(int mode) 5567d35a00b8SFelix Jia { 5568d35a00b8SFelix Jia if (mode != IN6_ADDR_GEN_MODE_EUI64 && 5569d35a00b8SFelix Jia mode != IN6_ADDR_GEN_MODE_NONE && 5570d35a00b8SFelix Jia mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY && 5571d35a00b8SFelix Jia mode != IN6_ADDR_GEN_MODE_RANDOM) 5572d35a00b8SFelix Jia return -EINVAL; 5573d35a00b8SFelix Jia return 1; 5574d35a00b8SFelix Jia } 5575d35a00b8SFelix Jia 5576d35a00b8SFelix Jia static int check_stable_privacy(struct inet6_dev *idev, struct net *net, 5577d35a00b8SFelix Jia int mode) 5578d35a00b8SFelix Jia { 5579d35a00b8SFelix Jia if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY && 5580d35a00b8SFelix Jia !idev->cnf.stable_secret.initialized && 5581d35a00b8SFelix Jia !net->ipv6.devconf_dflt->stable_secret.initialized) 5582d35a00b8SFelix Jia return -EINVAL; 5583d35a00b8SFelix Jia return 1; 5584d35a00b8SFelix Jia } 5585d35a00b8SFelix Jia 5586f53adae4SDaniel Borkmann static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla) 5587f53adae4SDaniel Borkmann { 5588f53adae4SDaniel Borkmann int err = -EINVAL; 5589f53adae4SDaniel Borkmann struct inet6_dev *idev = __in6_dev_get(dev); 5590f53adae4SDaniel Borkmann struct nlattr *tb[IFLA_INET6_MAX + 1]; 5591f53adae4SDaniel Borkmann 5592f53adae4SDaniel Borkmann if (!idev) 5593f53adae4SDaniel Borkmann return -EAFNOSUPPORT; 5594f53adae4SDaniel Borkmann 5595fceb6435SJohannes Berg if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL, NULL) < 0) 5596f53adae4SDaniel Borkmann BUG(); 5597f53adae4SDaniel Borkmann 5598bc91b0f0SJiri Pirko if (tb[IFLA_INET6_TOKEN]) { 5599f53adae4SDaniel Borkmann err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN])); 5600bc91b0f0SJiri Pirko if (err) 5601bc91b0f0SJiri Pirko return err; 5602bc91b0f0SJiri Pirko } 5603bc91b0f0SJiri Pirko 5604bc91b0f0SJiri Pirko if (tb[IFLA_INET6_ADDR_GEN_MODE]) { 5605bc91b0f0SJiri Pirko u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]); 5606bc91b0f0SJiri Pirko 5607d35a00b8SFelix Jia if (check_addr_gen_mode(mode) < 0 || 5608d35a00b8SFelix Jia check_stable_privacy(idev, dev_net(dev), mode) < 0) 5609bc91b0f0SJiri Pirko return -EINVAL; 5610622c81d5SHannes Frederic Sowa 5611d35a00b8SFelix Jia idev->cnf.addr_gen_mode = mode; 5612bc91b0f0SJiri Pirko err = 0; 5613bc91b0f0SJiri Pirko } 5614f53adae4SDaniel Borkmann 5615f53adae4SDaniel Borkmann return err; 5616f53adae4SDaniel Borkmann } 5617f53adae4SDaniel Borkmann 5618b382b191SThomas Graf static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev, 561915e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 5620b382b191SThomas Graf { 5621b382b191SThomas Graf struct net_device *dev = idev->dev; 5622b382b191SThomas Graf struct ifinfomsg *hdr; 5623b382b191SThomas Graf struct nlmsghdr *nlh; 5624b382b191SThomas Graf void *protoinfo; 5625b382b191SThomas Graf 562615e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags); 562763159f29SIan Morris if (!nlh) 5628b382b191SThomas Graf return -EMSGSIZE; 5629b382b191SThomas Graf 5630b382b191SThomas Graf hdr = nlmsg_data(nlh); 5631b382b191SThomas Graf hdr->ifi_family = AF_INET6; 5632b382b191SThomas Graf hdr->__ifi_pad = 0; 5633b382b191SThomas Graf hdr->ifi_type = dev->type; 5634b382b191SThomas Graf hdr->ifi_index = dev->ifindex; 5635b382b191SThomas Graf hdr->ifi_flags = dev_get_flags(dev); 5636b382b191SThomas Graf hdr->ifi_change = 0; 5637b382b191SThomas Graf 5638c78679e8SDavid S. Miller if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 5639c78679e8SDavid S. Miller (dev->addr_len && 5640c78679e8SDavid S. Miller nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 5641c78679e8SDavid S. Miller nla_put_u32(skb, IFLA_MTU, dev->mtu) || 5642a54acb3aSNicolas Dichtel (dev->ifindex != dev_get_iflink(dev) && 56430344338bSAndy Gospodarek nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) || 56440344338bSAndy Gospodarek nla_put_u8(skb, IFLA_OPERSTATE, 56450344338bSAndy Gospodarek netif_running(dev) ? dev->operstate : IF_OPER_DOWN)) 5646c78679e8SDavid S. Miller goto nla_put_failure; 5647b382b191SThomas Graf protoinfo = nla_nest_start(skb, IFLA_PROTINFO); 564863159f29SIan Morris if (!protoinfo) 5649b382b191SThomas Graf goto nla_put_failure; 5650b382b191SThomas Graf 5651d5566fd7SSowmini Varadhan if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0) 5652b382b191SThomas Graf goto nla_put_failure; 5653b382b191SThomas Graf 565404561c1fSThomas Graf nla_nest_end(skb, protoinfo); 5655053c095aSJohannes Berg nlmsg_end(skb, nlh); 5656053c095aSJohannes Berg return 0; 56571da177e4SLinus Torvalds 565804561c1fSThomas Graf nla_put_failure: 565926932566SPatrick McHardy nlmsg_cancel(skb, nlh); 566026932566SPatrick McHardy return -EMSGSIZE; 56611da177e4SLinus Torvalds } 56621da177e4SLinus Torvalds 5663786e0007SDavid Ahern static int inet6_valid_dump_ifinfo(const struct nlmsghdr *nlh, 5664786e0007SDavid Ahern struct netlink_ext_ack *extack) 5665786e0007SDavid Ahern { 5666786e0007SDavid Ahern struct ifinfomsg *ifm; 5667786e0007SDavid Ahern 5668786e0007SDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 5669786e0007SDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Invalid header for link dump request"); 5670786e0007SDavid Ahern return -EINVAL; 5671786e0007SDavid Ahern } 5672786e0007SDavid Ahern 5673786e0007SDavid Ahern if (nlmsg_attrlen(nlh, sizeof(*ifm))) { 5674786e0007SDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Invalid data after header"); 5675786e0007SDavid Ahern return -EINVAL; 5676786e0007SDavid Ahern } 5677786e0007SDavid Ahern 5678786e0007SDavid Ahern ifm = nlmsg_data(nlh); 5679786e0007SDavid Ahern if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 5680786e0007SDavid Ahern ifm->ifi_change || ifm->ifi_index) { 5681786e0007SDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for dump request"); 5682786e0007SDavid Ahern return -EINVAL; 5683786e0007SDavid Ahern } 5684786e0007SDavid Ahern 5685786e0007SDavid Ahern return 0; 5686786e0007SDavid Ahern } 5687786e0007SDavid Ahern 56881da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 56891da177e4SLinus Torvalds { 56903b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 569184d2697dSEric Dumazet int h, s_h; 5692434a8a58SDavid S. Miller int idx = 0, s_idx; 56931da177e4SLinus Torvalds struct net_device *dev; 56941da177e4SLinus Torvalds struct inet6_dev *idev; 569584d2697dSEric Dumazet struct hlist_head *head; 56961da177e4SLinus Torvalds 5697786e0007SDavid Ahern /* only requests using strict checking can pass data to 5698786e0007SDavid Ahern * influence the dump 5699786e0007SDavid Ahern */ 5700786e0007SDavid Ahern if (cb->strict_check) { 5701786e0007SDavid Ahern int err = inet6_valid_dump_ifinfo(cb->nlh, cb->extack); 5702786e0007SDavid Ahern 5703786e0007SDavid Ahern if (err < 0) 5704786e0007SDavid Ahern return err; 5705786e0007SDavid Ahern } 5706786e0007SDavid Ahern 570784d2697dSEric Dumazet s_h = cb->args[0]; 570884d2697dSEric Dumazet s_idx = cb->args[1]; 570984d2697dSEric Dumazet 571084d2697dSEric Dumazet rcu_read_lock(); 571184d2697dSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 57127562f876SPavel Emelianov idx = 0; 571384d2697dSEric Dumazet head = &net->dev_index_head[h]; 5714b67bfe0dSSasha Levin hlist_for_each_entry_rcu(dev, head, index_hlist) { 57151da177e4SLinus Torvalds if (idx < s_idx) 57167562f876SPavel Emelianov goto cont; 571784d2697dSEric Dumazet idev = __in6_dev_get(dev); 571884d2697dSEric Dumazet if (!idev) 57197562f876SPavel Emelianov goto cont; 572084d2697dSEric Dumazet if (inet6_fill_ifinfo(skb, idev, 572115e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 572284d2697dSEric Dumazet cb->nlh->nlmsg_seq, 5723053c095aSJohannes Berg RTM_NEWLINK, NLM_F_MULTI) < 0) 572484d2697dSEric Dumazet goto out; 57257562f876SPavel Emelianov cont: 57267562f876SPavel Emelianov idx++; 57271da177e4SLinus Torvalds } 572884d2697dSEric Dumazet } 572984d2697dSEric Dumazet out: 573084d2697dSEric Dumazet rcu_read_unlock(); 573184d2697dSEric Dumazet cb->args[1] = idx; 573284d2697dSEric Dumazet cb->args[0] = h; 57331da177e4SLinus Torvalds 57341da177e4SLinus Torvalds return skb->len; 57351da177e4SLinus Torvalds } 57361da177e4SLinus Torvalds 57371da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev) 57381da177e4SLinus Torvalds { 57391da177e4SLinus Torvalds struct sk_buff *skb; 5740c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 57418d7a76c9SThomas Graf int err = -ENOBUFS; 57421da177e4SLinus Torvalds 5743339bf98fSThomas Graf skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC); 574463159f29SIan Morris if (!skb) 57458d7a76c9SThomas Graf goto errout; 57468d7a76c9SThomas Graf 57478d7a76c9SThomas Graf err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0); 574826932566SPatrick McHardy if (err < 0) { 574926932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */ 575026932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 575126932566SPatrick McHardy kfree_skb(skb); 575226932566SPatrick McHardy goto errout; 575326932566SPatrick McHardy } 57545f75a104SNicolas Dichtel rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC); 57551ce85fe4SPablo Neira Ayuso return; 57568d7a76c9SThomas Graf errout: 57578d7a76c9SThomas Graf if (err < 0) 57585f75a104SNicolas Dichtel rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err); 57591da177e4SLinus Torvalds } 57601da177e4SLinus Torvalds 5761339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void) 5762339bf98fSThomas Graf { 5763339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct prefixmsg)) 5764339bf98fSThomas Graf + nla_total_size(sizeof(struct in6_addr)) 5765339bf98fSThomas Graf + nla_total_size(sizeof(struct prefix_cacheinfo)); 5766339bf98fSThomas Graf } 5767c5396a31SYOSHIFUJI Hideaki 57681da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev, 576915e47304SEric W. Biederman struct prefix_info *pinfo, u32 portid, u32 seq, 5770b6544c0bSJamal Hadi Salim int event, unsigned int flags) 57711da177e4SLinus Torvalds { 57721da177e4SLinus Torvalds struct prefixmsg *pmsg; 57731da177e4SLinus Torvalds struct nlmsghdr *nlh; 57741da177e4SLinus Torvalds struct prefix_cacheinfo ci; 57751da177e4SLinus Torvalds 577615e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags); 577763159f29SIan Morris if (!nlh) 577826932566SPatrick McHardy return -EMSGSIZE; 57796051e2f4SThomas Graf 57806051e2f4SThomas Graf pmsg = nlmsg_data(nlh); 57811da177e4SLinus Torvalds pmsg->prefix_family = AF_INET6; 57828a47077aSPatrick McHardy pmsg->prefix_pad1 = 0; 57838a47077aSPatrick McHardy pmsg->prefix_pad2 = 0; 57841da177e4SLinus Torvalds pmsg->prefix_ifindex = idev->dev->ifindex; 57851da177e4SLinus Torvalds pmsg->prefix_len = pinfo->prefix_len; 57861da177e4SLinus Torvalds pmsg->prefix_type = pinfo->type; 57878a47077aSPatrick McHardy pmsg->prefix_pad3 = 0; 57881da177e4SLinus Torvalds pmsg->prefix_flags = 0; 57891da177e4SLinus Torvalds if (pinfo->onlink) 57901da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_ONLINK; 57911da177e4SLinus Torvalds if (pinfo->autoconf) 57921da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_AUTOCONF; 57931da177e4SLinus Torvalds 5794c78679e8SDavid S. Miller if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix)) 5795c78679e8SDavid S. Miller goto nla_put_failure; 57961da177e4SLinus Torvalds ci.preferred_time = ntohl(pinfo->prefered); 57971da177e4SLinus Torvalds ci.valid_time = ntohl(pinfo->valid); 5798c78679e8SDavid S. Miller if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci)) 5799c78679e8SDavid S. Miller goto nla_put_failure; 5800053c095aSJohannes Berg nlmsg_end(skb, nlh); 5801053c095aSJohannes Berg return 0; 58021da177e4SLinus Torvalds 58036051e2f4SThomas Graf nla_put_failure: 580426932566SPatrick McHardy nlmsg_cancel(skb, nlh); 580526932566SPatrick McHardy return -EMSGSIZE; 58061da177e4SLinus Torvalds } 58071da177e4SLinus Torvalds 58081da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev, 58091da177e4SLinus Torvalds struct prefix_info *pinfo) 58101da177e4SLinus Torvalds { 58111da177e4SLinus Torvalds struct sk_buff *skb; 5812c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 58138c384bfaSThomas Graf int err = -ENOBUFS; 58141da177e4SLinus Torvalds 5815339bf98fSThomas Graf skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC); 581663159f29SIan Morris if (!skb) 58178c384bfaSThomas Graf goto errout; 58188c384bfaSThomas Graf 58198c384bfaSThomas Graf err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0); 582026932566SPatrick McHardy if (err < 0) { 582126932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */ 582226932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 582326932566SPatrick McHardy kfree_skb(skb); 582426932566SPatrick McHardy goto errout; 582526932566SPatrick McHardy } 58261ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC); 58271ce85fe4SPablo Neira Ayuso return; 58288c384bfaSThomas Graf errout: 58298c384bfaSThomas Graf if (err < 0) 58306fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err); 58311da177e4SLinus Torvalds } 58321da177e4SLinus Torvalds 58331da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 58341da177e4SLinus Torvalds { 583563998ac2SNicolas Dichtel struct net *net = dev_net(ifp->idev->dev); 583663998ac2SNicolas Dichtel 5837c15b1ccaSHannes Frederic Sowa if (event) 5838c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 5839c15b1ccaSHannes Frederic Sowa 58401da177e4SLinus Torvalds inet6_ifa_notify(event ? : RTM_NEWADDR, ifp); 58411da177e4SLinus Torvalds 58421da177e4SLinus Torvalds switch (event) { 58431da177e4SLinus Torvalds case RTM_NEWADDR: 584495c385b4SNeil Horman /* 584595c385b4SNeil Horman * If the address was optimistic 584695c385b4SNeil Horman * we inserted the route at the start of 584795c385b4SNeil Horman * our DAD process, so we don't need 584895c385b4SNeil Horman * to do it again 584995c385b4SNeil Horman */ 585093c2fb25SDavid Ahern if (!rcu_access_pointer(ifp->rt->fib6_node)) 5851afb1d4b5SDavid Ahern ip6_ins_rt(net, ifp->rt); 58521da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 58531da177e4SLinus Torvalds addrconf_join_anycast(ifp); 58547996c799SCong Wang if (!ipv6_addr_any(&ifp->peer_addr)) 58558308f3ffSDavid Ahern addrconf_prefix_route(&ifp->peer_addr, 128, 0, 5856acb54e3cSDavid Ahern ifp->idev->dev, 0, 0, 585727b10608SDavid Ahern GFP_ATOMIC); 58581da177e4SLinus Torvalds break; 58591da177e4SLinus Torvalds case RTM_DELADDR: 58601da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 58611da177e4SLinus Torvalds addrconf_leave_anycast(ifp); 58621da177e4SLinus Torvalds addrconf_leave_solict(ifp->idev, &ifp->addr); 58637996c799SCong Wang if (!ipv6_addr_any(&ifp->peer_addr)) { 58648d1c802bSDavid Ahern struct fib6_info *rt; 5865caeaba79SNicolas Dichtel 5866e7478dfcSNicolas Dichtel rt = addrconf_get_prefix_route(&ifp->peer_addr, 128, 5867e7478dfcSNicolas Dichtel ifp->idev->dev, 0, 0); 58688e3d5be7SMartin KaFai Lau if (rt) 5869afb1d4b5SDavid Ahern ip6_del_rt(net, rt); 5870caeaba79SNicolas Dichtel } 587138bd10c4SDavid Ahern if (ifp->rt) { 5872afb1d4b5SDavid Ahern ip6_del_rt(net, ifp->rt); 587393531c67SDavid Ahern ifp->rt = NULL; 587438bd10c4SDavid Ahern } 5875705f1c86SHannes Frederic Sowa rt_genid_bump_ipv6(net); 58761da177e4SLinus Torvalds break; 58771da177e4SLinus Torvalds } 587863998ac2SNicolas Dichtel atomic_inc(&net->ipv6.dev_addr_genid); 58791da177e4SLinus Torvalds } 58801da177e4SLinus Torvalds 58811da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 58821da177e4SLinus Torvalds { 58838814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 58841da177e4SLinus Torvalds if (likely(ifp->idev->dead == 0)) 58851da177e4SLinus Torvalds __ipv6_ifa_notify(event, ifp); 58868814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 58871da177e4SLinus Torvalds } 58881da177e4SLinus Torvalds 58891da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 58901da177e4SLinus Torvalds 58911da177e4SLinus Torvalds static 5892fe2c6338SJoe Perches int addrconf_sysctl_forward(struct ctl_table *ctl, int write, 58931da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 58941da177e4SLinus Torvalds { 58951da177e4SLinus Torvalds int *valp = ctl->data; 58961da177e4SLinus Torvalds int val = *valp; 589788af182eSEric W. Biederman loff_t pos = *ppos; 5898fe2c6338SJoe Perches struct ctl_table lctl; 58991da177e4SLinus Torvalds int ret; 59001da177e4SLinus Torvalds 5901013d97e9SFrancesco Ruggeri /* 5902013d97e9SFrancesco Ruggeri * ctl->data points to idev->cnf.forwarding, we should 5903013d97e9SFrancesco Ruggeri * not modify it until we get the rtnl lock. 5904013d97e9SFrancesco Ruggeri */ 5905013d97e9SFrancesco Ruggeri lctl = *ctl; 5906013d97e9SFrancesco Ruggeri lctl.data = &val; 5907013d97e9SFrancesco Ruggeri 5908013d97e9SFrancesco Ruggeri ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 59091da177e4SLinus Torvalds 5910c8fecf22SPavel Emelyanov if (write) 5911b325fddbSStephen Hemminger ret = addrconf_fixup_forwarding(ctl, valp, val); 591288af182eSEric W. Biederman if (ret) 591388af182eSEric W. Biederman *ppos = pos; 59141da177e4SLinus Torvalds return ret; 59151da177e4SLinus Torvalds } 59161da177e4SLinus Torvalds 591777751427SMarcelo Leitner static 591877751427SMarcelo Leitner int addrconf_sysctl_mtu(struct ctl_table *ctl, int write, 591977751427SMarcelo Leitner void __user *buffer, size_t *lenp, loff_t *ppos) 592077751427SMarcelo Leitner { 592177751427SMarcelo Leitner struct inet6_dev *idev = ctl->extra1; 592277751427SMarcelo Leitner int min_mtu = IPV6_MIN_MTU; 592377751427SMarcelo Leitner struct ctl_table lctl; 592477751427SMarcelo Leitner 592577751427SMarcelo Leitner lctl = *ctl; 592677751427SMarcelo Leitner lctl.extra1 = &min_mtu; 592777751427SMarcelo Leitner lctl.extra2 = idev ? &idev->dev->mtu : NULL; 592877751427SMarcelo Leitner 592977751427SMarcelo Leitner return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos); 593077751427SMarcelo Leitner } 593177751427SMarcelo Leitner 593256d417b1SBrian Haley static void dev_disable_change(struct inet6_dev *idev) 593356d417b1SBrian Haley { 593475538c2bSCong Wang struct netdev_notifier_info info; 593575538c2bSCong Wang 593656d417b1SBrian Haley if (!idev || !idev->dev) 593756d417b1SBrian Haley return; 593856d417b1SBrian Haley 593975538c2bSCong Wang netdev_notifier_info_init(&info, idev->dev); 594056d417b1SBrian Haley if (idev->cnf.disable_ipv6) 594175538c2bSCong Wang addrconf_notify(NULL, NETDEV_DOWN, &info); 594256d417b1SBrian Haley else 594375538c2bSCong Wang addrconf_notify(NULL, NETDEV_UP, &info); 594456d417b1SBrian Haley } 594556d417b1SBrian Haley 594656d417b1SBrian Haley static void addrconf_disable_change(struct net *net, __s32 newf) 594756d417b1SBrian Haley { 594856d417b1SBrian Haley struct net_device *dev; 594956d417b1SBrian Haley struct inet6_dev *idev; 595056d417b1SBrian Haley 595103e4deffSKefeng Wang for_each_netdev(net, dev) { 595256d417b1SBrian Haley idev = __in6_dev_get(dev); 595356d417b1SBrian Haley if (idev) { 595456d417b1SBrian Haley int changed = (!idev->cnf.disable_ipv6) ^ (!newf); 595556d417b1SBrian Haley idev->cnf.disable_ipv6 = newf; 595656d417b1SBrian Haley if (changed) 595756d417b1SBrian Haley dev_disable_change(idev); 595856d417b1SBrian Haley } 595956d417b1SBrian Haley } 596056d417b1SBrian Haley } 596156d417b1SBrian Haley 5962013d97e9SFrancesco Ruggeri static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf) 596356d417b1SBrian Haley { 596456d417b1SBrian Haley struct net *net; 5965013d97e9SFrancesco Ruggeri int old; 5966013d97e9SFrancesco Ruggeri 5967013d97e9SFrancesco Ruggeri if (!rtnl_trylock()) 5968013d97e9SFrancesco Ruggeri return restart_syscall(); 596956d417b1SBrian Haley 597056d417b1SBrian Haley net = (struct net *)table->extra2; 5971013d97e9SFrancesco Ruggeri old = *p; 5972013d97e9SFrancesco Ruggeri *p = newf; 597356d417b1SBrian Haley 5974013d97e9SFrancesco Ruggeri if (p == &net->ipv6.devconf_dflt->disable_ipv6) { 5975013d97e9SFrancesco Ruggeri rtnl_unlock(); 597656d417b1SBrian Haley return 0; 597788af182eSEric W. Biederman } 597856d417b1SBrian Haley 597956d417b1SBrian Haley if (p == &net->ipv6.devconf_all->disable_ipv6) { 598056d417b1SBrian Haley net->ipv6.devconf_dflt->disable_ipv6 = newf; 598156d417b1SBrian Haley addrconf_disable_change(net, newf); 5982013d97e9SFrancesco Ruggeri } else if ((!newf) ^ (!old)) 598356d417b1SBrian Haley dev_disable_change((struct inet6_dev *)table->extra1); 598456d417b1SBrian Haley 598556d417b1SBrian Haley rtnl_unlock(); 598656d417b1SBrian Haley return 0; 598756d417b1SBrian Haley } 598856d417b1SBrian Haley 598956d417b1SBrian Haley static 5990fe2c6338SJoe Perches int addrconf_sysctl_disable(struct ctl_table *ctl, int write, 599156d417b1SBrian Haley void __user *buffer, size_t *lenp, loff_t *ppos) 599256d417b1SBrian Haley { 599356d417b1SBrian Haley int *valp = ctl->data; 599456d417b1SBrian Haley int val = *valp; 599588af182eSEric W. Biederman loff_t pos = *ppos; 5996fe2c6338SJoe Perches struct ctl_table lctl; 599756d417b1SBrian Haley int ret; 599856d417b1SBrian Haley 5999013d97e9SFrancesco Ruggeri /* 6000013d97e9SFrancesco Ruggeri * ctl->data points to idev->cnf.disable_ipv6, we should 6001013d97e9SFrancesco Ruggeri * not modify it until we get the rtnl lock. 6002013d97e9SFrancesco Ruggeri */ 6003013d97e9SFrancesco Ruggeri lctl = *ctl; 6004013d97e9SFrancesco Ruggeri lctl.data = &val; 6005013d97e9SFrancesco Ruggeri 6006013d97e9SFrancesco Ruggeri ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 600756d417b1SBrian Haley 600856d417b1SBrian Haley if (write) 600956d417b1SBrian Haley ret = addrconf_disable_ipv6(ctl, valp, val); 601088af182eSEric W. Biederman if (ret) 601188af182eSEric W. Biederman *ppos = pos; 601256d417b1SBrian Haley return ret; 601356d417b1SBrian Haley } 601456d417b1SBrian Haley 6015c92d5491Sstephen hemminger static 6016c92d5491Sstephen hemminger int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write, 6017c92d5491Sstephen hemminger void __user *buffer, size_t *lenp, loff_t *ppos) 6018c92d5491Sstephen hemminger { 6019c92d5491Sstephen hemminger int *valp = ctl->data; 6020c92d5491Sstephen hemminger int ret; 6021c92d5491Sstephen hemminger int old, new; 6022c92d5491Sstephen hemminger 6023c92d5491Sstephen hemminger old = *valp; 6024c92d5491Sstephen hemminger ret = proc_dointvec(ctl, write, buffer, lenp, ppos); 6025c92d5491Sstephen hemminger new = *valp; 6026c92d5491Sstephen hemminger 6027c92d5491Sstephen hemminger if (write && old != new) { 6028c92d5491Sstephen hemminger struct net *net = ctl->extra2; 6029c92d5491Sstephen hemminger 6030c92d5491Sstephen hemminger if (!rtnl_trylock()) 6031c92d5491Sstephen hemminger return restart_syscall(); 6032c92d5491Sstephen hemminger 6033c92d5491Sstephen hemminger if (valp == &net->ipv6.devconf_dflt->proxy_ndp) 603485b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 603585b3daadSDavid Ahern NETCONFA_PROXY_NEIGH, 6036c92d5491Sstephen hemminger NETCONFA_IFINDEX_DEFAULT, 6037c92d5491Sstephen hemminger net->ipv6.devconf_dflt); 6038c92d5491Sstephen hemminger else if (valp == &net->ipv6.devconf_all->proxy_ndp) 603985b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 604085b3daadSDavid Ahern NETCONFA_PROXY_NEIGH, 6041c92d5491Sstephen hemminger NETCONFA_IFINDEX_ALL, 6042c92d5491Sstephen hemminger net->ipv6.devconf_all); 6043c92d5491Sstephen hemminger else { 6044c92d5491Sstephen hemminger struct inet6_dev *idev = ctl->extra1; 6045c92d5491Sstephen hemminger 604685b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 604785b3daadSDavid Ahern NETCONFA_PROXY_NEIGH, 6048c92d5491Sstephen hemminger idev->dev->ifindex, 6049c92d5491Sstephen hemminger &idev->cnf); 6050c92d5491Sstephen hemminger } 6051c92d5491Sstephen hemminger rtnl_unlock(); 6052c92d5491Sstephen hemminger } 6053c92d5491Sstephen hemminger 6054c92d5491Sstephen hemminger return ret; 6055c92d5491Sstephen hemminger } 6056c92d5491Sstephen hemminger 6057d35a00b8SFelix Jia static int addrconf_sysctl_addr_gen_mode(struct ctl_table *ctl, int write, 6058d35a00b8SFelix Jia void __user *buffer, size_t *lenp, 6059d35a00b8SFelix Jia loff_t *ppos) 6060d35a00b8SFelix Jia { 6061d35a00b8SFelix Jia int ret = 0; 6062c6dbf7aaSSabrina Dubroca u32 new_val; 6063d35a00b8SFelix Jia struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1; 6064d35a00b8SFelix Jia struct net *net = (struct net *)ctl->extra2; 6065c6dbf7aaSSabrina Dubroca struct ctl_table tmp = { 6066c6dbf7aaSSabrina Dubroca .data = &new_val, 6067c6dbf7aaSSabrina Dubroca .maxlen = sizeof(new_val), 6068c6dbf7aaSSabrina Dubroca .mode = ctl->mode, 6069c6dbf7aaSSabrina Dubroca }; 6070d35a00b8SFelix Jia 60718c171d6cSFelix Jia if (!rtnl_trylock()) 60728c171d6cSFelix Jia return restart_syscall(); 60738c171d6cSFelix Jia 6074c6dbf7aaSSabrina Dubroca new_val = *((u32 *)ctl->data); 6075c6dbf7aaSSabrina Dubroca 6076c6dbf7aaSSabrina Dubroca ret = proc_douintvec(&tmp, write, buffer, lenp, ppos); 6077c6dbf7aaSSabrina Dubroca if (ret != 0) 6078c6dbf7aaSSabrina Dubroca goto out; 6079d35a00b8SFelix Jia 6080d35a00b8SFelix Jia if (write) { 60818c171d6cSFelix Jia if (check_addr_gen_mode(new_val) < 0) { 60828c171d6cSFelix Jia ret = -EINVAL; 60838c171d6cSFelix Jia goto out; 60848c171d6cSFelix Jia } 6085d35a00b8SFelix Jia 6086c6dbf7aaSSabrina Dubroca if (idev) { 60878c171d6cSFelix Jia if (check_stable_privacy(idev, net, new_val) < 0) { 60888c171d6cSFelix Jia ret = -EINVAL; 60898c171d6cSFelix Jia goto out; 60908c171d6cSFelix Jia } 6091d35a00b8SFelix Jia 6092d35a00b8SFelix Jia if (idev->cnf.addr_gen_mode != new_val) { 6093d35a00b8SFelix Jia idev->cnf.addr_gen_mode = new_val; 6094d35a00b8SFelix Jia addrconf_dev_config(idev->dev); 60958c171d6cSFelix Jia } 6096f24c5987SSabrina Dubroca } else if (&net->ipv6.devconf_all->addr_gen_mode == ctl->data) { 6097f24c5987SSabrina Dubroca struct net_device *dev; 6098f24c5987SSabrina Dubroca 6099f24c5987SSabrina Dubroca net->ipv6.devconf_dflt->addr_gen_mode = new_val; 6100f24c5987SSabrina Dubroca for_each_netdev(net, dev) { 6101f24c5987SSabrina Dubroca idev = __in6_dev_get(dev); 6102f24c5987SSabrina Dubroca if (idev && 6103f24c5987SSabrina Dubroca idev->cnf.addr_gen_mode != new_val) { 6104f24c5987SSabrina Dubroca idev->cnf.addr_gen_mode = new_val; 6105f24c5987SSabrina Dubroca addrconf_dev_config(idev->dev); 6106f24c5987SSabrina Dubroca } 6107f24c5987SSabrina Dubroca } 61088c171d6cSFelix Jia } 6109c6dbf7aaSSabrina Dubroca 6110c6dbf7aaSSabrina Dubroca *((u32 *)ctl->data) = new_val; 61118c171d6cSFelix Jia } 61128c171d6cSFelix Jia 61138c171d6cSFelix Jia out: 6114d35a00b8SFelix Jia rtnl_unlock(); 6115d35a00b8SFelix Jia 6116d35a00b8SFelix Jia return ret; 6117d35a00b8SFelix Jia } 6118d35a00b8SFelix Jia 61193d1bec99SHannes Frederic Sowa static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write, 61203d1bec99SHannes Frederic Sowa void __user *buffer, size_t *lenp, 61213d1bec99SHannes Frederic Sowa loff_t *ppos) 61223d1bec99SHannes Frederic Sowa { 61233d1bec99SHannes Frederic Sowa int err; 61243d1bec99SHannes Frederic Sowa struct in6_addr addr; 61253d1bec99SHannes Frederic Sowa char str[IPV6_MAX_STRLEN]; 61263d1bec99SHannes Frederic Sowa struct ctl_table lctl = *ctl; 6127622c81d5SHannes Frederic Sowa struct net *net = ctl->extra2; 61283d1bec99SHannes Frederic Sowa struct ipv6_stable_secret *secret = ctl->data; 61293d1bec99SHannes Frederic Sowa 6130622c81d5SHannes Frederic Sowa if (&net->ipv6.devconf_all->stable_secret == ctl->data) 6131622c81d5SHannes Frederic Sowa return -EIO; 6132622c81d5SHannes Frederic Sowa 61333d1bec99SHannes Frederic Sowa lctl.maxlen = IPV6_MAX_STRLEN; 61343d1bec99SHannes Frederic Sowa lctl.data = str; 61353d1bec99SHannes Frederic Sowa 61363d1bec99SHannes Frederic Sowa if (!rtnl_trylock()) 61373d1bec99SHannes Frederic Sowa return restart_syscall(); 61383d1bec99SHannes Frederic Sowa 61393d1bec99SHannes Frederic Sowa if (!write && !secret->initialized) { 61403d1bec99SHannes Frederic Sowa err = -EIO; 61413d1bec99SHannes Frederic Sowa goto out; 61423d1bec99SHannes Frederic Sowa } 61433d1bec99SHannes Frederic Sowa 61445449a5caSWANG Cong err = snprintf(str, sizeof(str), "%pI6", &secret->secret); 61453d1bec99SHannes Frederic Sowa if (err >= sizeof(str)) { 61463d1bec99SHannes Frederic Sowa err = -EIO; 61473d1bec99SHannes Frederic Sowa goto out; 61483d1bec99SHannes Frederic Sowa } 61493d1bec99SHannes Frederic Sowa 61503d1bec99SHannes Frederic Sowa err = proc_dostring(&lctl, write, buffer, lenp, ppos); 61513d1bec99SHannes Frederic Sowa if (err || !write) 61523d1bec99SHannes Frederic Sowa goto out; 61533d1bec99SHannes Frederic Sowa 61543d1bec99SHannes Frederic Sowa if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) { 61553d1bec99SHannes Frederic Sowa err = -EIO; 61563d1bec99SHannes Frederic Sowa goto out; 61573d1bec99SHannes Frederic Sowa } 61583d1bec99SHannes Frederic Sowa 61593d1bec99SHannes Frederic Sowa secret->initialized = true; 61603d1bec99SHannes Frederic Sowa secret->secret = addr; 61613d1bec99SHannes Frederic Sowa 6162622c81d5SHannes Frederic Sowa if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) { 6163622c81d5SHannes Frederic Sowa struct net_device *dev; 6164622c81d5SHannes Frederic Sowa 6165622c81d5SHannes Frederic Sowa for_each_netdev(net, dev) { 6166622c81d5SHannes Frederic Sowa struct inet6_dev *idev = __in6_dev_get(dev); 6167622c81d5SHannes Frederic Sowa 6168622c81d5SHannes Frederic Sowa if (idev) { 6169d35a00b8SFelix Jia idev->cnf.addr_gen_mode = 6170622c81d5SHannes Frederic Sowa IN6_ADDR_GEN_MODE_STABLE_PRIVACY; 6171622c81d5SHannes Frederic Sowa } 6172622c81d5SHannes Frederic Sowa } 6173622c81d5SHannes Frederic Sowa } else { 6174622c81d5SHannes Frederic Sowa struct inet6_dev *idev = ctl->extra1; 6175622c81d5SHannes Frederic Sowa 6176d35a00b8SFelix Jia idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY; 6177622c81d5SHannes Frederic Sowa } 6178622c81d5SHannes Frederic Sowa 61793d1bec99SHannes Frederic Sowa out: 61803d1bec99SHannes Frederic Sowa rtnl_unlock(); 61813d1bec99SHannes Frederic Sowa 61823d1bec99SHannes Frederic Sowa return err; 61833d1bec99SHannes Frederic Sowa } 6184c92d5491Sstephen hemminger 618535103d11SAndy Gospodarek static 618635103d11SAndy Gospodarek int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl, 618735103d11SAndy Gospodarek int write, 618835103d11SAndy Gospodarek void __user *buffer, 618935103d11SAndy Gospodarek size_t *lenp, 619035103d11SAndy Gospodarek loff_t *ppos) 619135103d11SAndy Gospodarek { 619235103d11SAndy Gospodarek int *valp = ctl->data; 619335103d11SAndy Gospodarek int val = *valp; 619435103d11SAndy Gospodarek loff_t pos = *ppos; 619535103d11SAndy Gospodarek struct ctl_table lctl; 619635103d11SAndy Gospodarek int ret; 619735103d11SAndy Gospodarek 619835103d11SAndy Gospodarek /* ctl->data points to idev->cnf.ignore_routes_when_linkdown 619935103d11SAndy Gospodarek * we should not modify it until we get the rtnl lock. 620035103d11SAndy Gospodarek */ 620135103d11SAndy Gospodarek lctl = *ctl; 620235103d11SAndy Gospodarek lctl.data = &val; 620335103d11SAndy Gospodarek 620435103d11SAndy Gospodarek ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 620535103d11SAndy Gospodarek 620635103d11SAndy Gospodarek if (write) 620735103d11SAndy Gospodarek ret = addrconf_fixup_linkdown(ctl, valp, val); 620835103d11SAndy Gospodarek if (ret) 620935103d11SAndy Gospodarek *ppos = pos; 621035103d11SAndy Gospodarek return ret; 621135103d11SAndy Gospodarek } 621235103d11SAndy Gospodarek 6213df789fe7SDavid Forster static 6214df789fe7SDavid Forster void addrconf_set_nopolicy(struct rt6_info *rt, int action) 6215df789fe7SDavid Forster { 6216df789fe7SDavid Forster if (rt) { 6217df789fe7SDavid Forster if (action) 6218df789fe7SDavid Forster rt->dst.flags |= DST_NOPOLICY; 6219df789fe7SDavid Forster else 6220df789fe7SDavid Forster rt->dst.flags &= ~DST_NOPOLICY; 6221df789fe7SDavid Forster } 6222df789fe7SDavid Forster } 6223df789fe7SDavid Forster 6224df789fe7SDavid Forster static 6225df789fe7SDavid Forster void addrconf_disable_policy_idev(struct inet6_dev *idev, int val) 6226df789fe7SDavid Forster { 6227df789fe7SDavid Forster struct inet6_ifaddr *ifa; 6228df789fe7SDavid Forster 6229df789fe7SDavid Forster read_lock_bh(&idev->lock); 6230df789fe7SDavid Forster list_for_each_entry(ifa, &idev->addr_list, if_list) { 6231df789fe7SDavid Forster spin_lock(&ifa->lock); 6232df789fe7SDavid Forster if (ifa->rt) { 62338d1c802bSDavid Ahern struct fib6_info *rt = ifa->rt; 6234df789fe7SDavid Forster int cpu; 6235df789fe7SDavid Forster 623666f5d6ceSWei Wang rcu_read_lock(); 62373b6761d1SDavid Ahern ifa->rt->dst_nopolicy = val ? true : false; 6238df789fe7SDavid Forster if (rt->rt6i_pcpu) { 6239df789fe7SDavid Forster for_each_possible_cpu(cpu) { 6240df789fe7SDavid Forster struct rt6_info **rtp; 6241df789fe7SDavid Forster 6242df789fe7SDavid Forster rtp = per_cpu_ptr(rt->rt6i_pcpu, cpu); 6243df789fe7SDavid Forster addrconf_set_nopolicy(*rtp, val); 6244df789fe7SDavid Forster } 6245df789fe7SDavid Forster } 624666f5d6ceSWei Wang rcu_read_unlock(); 6247df789fe7SDavid Forster } 6248df789fe7SDavid Forster spin_unlock(&ifa->lock); 6249df789fe7SDavid Forster } 6250df789fe7SDavid Forster read_unlock_bh(&idev->lock); 6251df789fe7SDavid Forster } 6252df789fe7SDavid Forster 6253df789fe7SDavid Forster static 6254df789fe7SDavid Forster int addrconf_disable_policy(struct ctl_table *ctl, int *valp, int val) 6255df789fe7SDavid Forster { 6256df789fe7SDavid Forster struct inet6_dev *idev; 6257df789fe7SDavid Forster struct net *net; 6258df789fe7SDavid Forster 6259df789fe7SDavid Forster if (!rtnl_trylock()) 6260df789fe7SDavid Forster return restart_syscall(); 6261df789fe7SDavid Forster 6262df789fe7SDavid Forster *valp = val; 6263df789fe7SDavid Forster 6264df789fe7SDavid Forster net = (struct net *)ctl->extra2; 6265df789fe7SDavid Forster if (valp == &net->ipv6.devconf_dflt->disable_policy) { 6266df789fe7SDavid Forster rtnl_unlock(); 6267df789fe7SDavid Forster return 0; 6268df789fe7SDavid Forster } 6269df789fe7SDavid Forster 6270df789fe7SDavid Forster if (valp == &net->ipv6.devconf_all->disable_policy) { 6271df789fe7SDavid Forster struct net_device *dev; 6272df789fe7SDavid Forster 6273df789fe7SDavid Forster for_each_netdev(net, dev) { 6274df789fe7SDavid Forster idev = __in6_dev_get(dev); 6275df789fe7SDavid Forster if (idev) 6276df789fe7SDavid Forster addrconf_disable_policy_idev(idev, val); 6277df789fe7SDavid Forster } 6278df789fe7SDavid Forster } else { 6279df789fe7SDavid Forster idev = (struct inet6_dev *)ctl->extra1; 6280df789fe7SDavid Forster addrconf_disable_policy_idev(idev, val); 6281df789fe7SDavid Forster } 6282df789fe7SDavid Forster 6283df789fe7SDavid Forster rtnl_unlock(); 6284df789fe7SDavid Forster return 0; 6285df789fe7SDavid Forster } 6286df789fe7SDavid Forster 6287df789fe7SDavid Forster static 6288df789fe7SDavid Forster int addrconf_sysctl_disable_policy(struct ctl_table *ctl, int write, 6289df789fe7SDavid Forster void __user *buffer, size_t *lenp, 6290df789fe7SDavid Forster loff_t *ppos) 6291df789fe7SDavid Forster { 6292df789fe7SDavid Forster int *valp = ctl->data; 6293df789fe7SDavid Forster int val = *valp; 6294df789fe7SDavid Forster loff_t pos = *ppos; 6295df789fe7SDavid Forster struct ctl_table lctl; 6296df789fe7SDavid Forster int ret; 6297df789fe7SDavid Forster 6298df789fe7SDavid Forster lctl = *ctl; 6299df789fe7SDavid Forster lctl.data = &val; 6300df789fe7SDavid Forster ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 6301df789fe7SDavid Forster 6302df789fe7SDavid Forster if (write && (*valp != val)) 6303df789fe7SDavid Forster ret = addrconf_disable_policy(ctl, valp, val); 6304df789fe7SDavid Forster 6305df789fe7SDavid Forster if (ret) 6306df789fe7SDavid Forster *ppos = pos; 6307df789fe7SDavid Forster 6308df789fe7SDavid Forster return ret; 6309df789fe7SDavid Forster } 6310df789fe7SDavid Forster 6311cb4a4c69SMaciej Żenczykowski static int minus_one = -1; 63122210d6b2SMaciej Żenczykowski static const int zero = 0; 6313cb9e684eSMaciej Żenczykowski static const int one = 1; 6314cb9e684eSMaciej Żenczykowski static const int two_five_five = 255; 6315cb9e684eSMaciej Żenczykowski 6316607ea7cdSKonstantin Khlebnikov static const struct ctl_table addrconf_sysctl[] = { 63171da177e4SLinus Torvalds { 63181da177e4SLinus Torvalds .procname = "forwarding", 63191da177e4SLinus Torvalds .data = &ipv6_devconf.forwarding, 63201da177e4SLinus Torvalds .maxlen = sizeof(int), 63211da177e4SLinus Torvalds .mode = 0644, 63226d9f239aSAlexey Dobriyan .proc_handler = addrconf_sysctl_forward, 63231da177e4SLinus Torvalds }, 63241da177e4SLinus Torvalds { 63251da177e4SLinus Torvalds .procname = "hop_limit", 63261da177e4SLinus Torvalds .data = &ipv6_devconf.hop_limit, 63271da177e4SLinus Torvalds .maxlen = sizeof(int), 63281da177e4SLinus Torvalds .mode = 0644, 6329cb9e684eSMaciej Żenczykowski .proc_handler = proc_dointvec_minmax, 6330cb9e684eSMaciej Żenczykowski .extra1 = (void *)&one, 6331cb9e684eSMaciej Żenczykowski .extra2 = (void *)&two_five_five, 63321da177e4SLinus Torvalds }, 63331da177e4SLinus Torvalds { 63341da177e4SLinus Torvalds .procname = "mtu", 63351da177e4SLinus Torvalds .data = &ipv6_devconf.mtu6, 63361da177e4SLinus Torvalds .maxlen = sizeof(int), 63371da177e4SLinus Torvalds .mode = 0644, 633877751427SMarcelo Leitner .proc_handler = addrconf_sysctl_mtu, 63391da177e4SLinus Torvalds }, 63401da177e4SLinus Torvalds { 63411da177e4SLinus Torvalds .procname = "accept_ra", 63421da177e4SLinus Torvalds .data = &ipv6_devconf.accept_ra, 63431da177e4SLinus Torvalds .maxlen = sizeof(int), 63441da177e4SLinus Torvalds .mode = 0644, 63456d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 63461da177e4SLinus Torvalds }, 63471da177e4SLinus Torvalds { 63481da177e4SLinus Torvalds .procname = "accept_redirects", 63491da177e4SLinus Torvalds .data = &ipv6_devconf.accept_redirects, 63501da177e4SLinus Torvalds .maxlen = sizeof(int), 63511da177e4SLinus Torvalds .mode = 0644, 63526d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 63531da177e4SLinus Torvalds }, 63541da177e4SLinus Torvalds { 63551da177e4SLinus Torvalds .procname = "autoconf", 63561da177e4SLinus Torvalds .data = &ipv6_devconf.autoconf, 63571da177e4SLinus Torvalds .maxlen = sizeof(int), 63581da177e4SLinus Torvalds .mode = 0644, 63596d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 63601da177e4SLinus Torvalds }, 63611da177e4SLinus Torvalds { 63621da177e4SLinus Torvalds .procname = "dad_transmits", 63631da177e4SLinus Torvalds .data = &ipv6_devconf.dad_transmits, 63641da177e4SLinus Torvalds .maxlen = sizeof(int), 63651da177e4SLinus Torvalds .mode = 0644, 63666d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 63671da177e4SLinus Torvalds }, 63681da177e4SLinus Torvalds { 63691da177e4SLinus Torvalds .procname = "router_solicitations", 63701da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicits, 63711da177e4SLinus Torvalds .maxlen = sizeof(int), 63721da177e4SLinus Torvalds .mode = 0644, 6373cb4a4c69SMaciej Żenczykowski .proc_handler = proc_dointvec_minmax, 6374cb4a4c69SMaciej Żenczykowski .extra1 = &minus_one, 63751da177e4SLinus Torvalds }, 63761da177e4SLinus Torvalds { 63771da177e4SLinus Torvalds .procname = "router_solicitation_interval", 63781da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_interval, 63791da177e4SLinus Torvalds .maxlen = sizeof(int), 63801da177e4SLinus Torvalds .mode = 0644, 63816d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 63821da177e4SLinus Torvalds }, 63831da177e4SLinus Torvalds { 6384bd11f074SMaciej Żenczykowski .procname = "router_solicitation_max_interval", 6385bd11f074SMaciej Żenczykowski .data = &ipv6_devconf.rtr_solicit_max_interval, 6386bd11f074SMaciej Żenczykowski .maxlen = sizeof(int), 6387bd11f074SMaciej Żenczykowski .mode = 0644, 6388bd11f074SMaciej Żenczykowski .proc_handler = proc_dointvec_jiffies, 6389bd11f074SMaciej Żenczykowski }, 6390bd11f074SMaciej Żenczykowski { 63911da177e4SLinus Torvalds .procname = "router_solicitation_delay", 63921da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_delay, 63931da177e4SLinus Torvalds .maxlen = sizeof(int), 63941da177e4SLinus Torvalds .mode = 0644, 63956d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 63961da177e4SLinus Torvalds }, 63971da177e4SLinus Torvalds { 63981da177e4SLinus Torvalds .procname = "force_mld_version", 63991da177e4SLinus Torvalds .data = &ipv6_devconf.force_mld_version, 64001da177e4SLinus Torvalds .maxlen = sizeof(int), 64011da177e4SLinus Torvalds .mode = 0644, 64026d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 64031da177e4SLinus Torvalds }, 6404fc4eba58SHannes Frederic Sowa { 6405fc4eba58SHannes Frederic Sowa .procname = "mldv1_unsolicited_report_interval", 6406fc4eba58SHannes Frederic Sowa .data = 6407fc4eba58SHannes Frederic Sowa &ipv6_devconf.mldv1_unsolicited_report_interval, 6408fc4eba58SHannes Frederic Sowa .maxlen = sizeof(int), 6409fc4eba58SHannes Frederic Sowa .mode = 0644, 6410fc4eba58SHannes Frederic Sowa .proc_handler = proc_dointvec_ms_jiffies, 6411fc4eba58SHannes Frederic Sowa }, 6412fc4eba58SHannes Frederic Sowa { 6413fc4eba58SHannes Frederic Sowa .procname = "mldv2_unsolicited_report_interval", 6414fc4eba58SHannes Frederic Sowa .data = 6415fc4eba58SHannes Frederic Sowa &ipv6_devconf.mldv2_unsolicited_report_interval, 6416fc4eba58SHannes Frederic Sowa .maxlen = sizeof(int), 6417fc4eba58SHannes Frederic Sowa .mode = 0644, 6418fc4eba58SHannes Frederic Sowa .proc_handler = proc_dointvec_ms_jiffies, 6419fc4eba58SHannes Frederic Sowa }, 64201da177e4SLinus Torvalds { 64211da177e4SLinus Torvalds .procname = "use_tempaddr", 64221da177e4SLinus Torvalds .data = &ipv6_devconf.use_tempaddr, 64231da177e4SLinus Torvalds .maxlen = sizeof(int), 64241da177e4SLinus Torvalds .mode = 0644, 64256d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 64261da177e4SLinus Torvalds }, 64271da177e4SLinus Torvalds { 64281da177e4SLinus Torvalds .procname = "temp_valid_lft", 64291da177e4SLinus Torvalds .data = &ipv6_devconf.temp_valid_lft, 64301da177e4SLinus Torvalds .maxlen = sizeof(int), 64311da177e4SLinus Torvalds .mode = 0644, 64326d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 64331da177e4SLinus Torvalds }, 64341da177e4SLinus Torvalds { 64351da177e4SLinus Torvalds .procname = "temp_prefered_lft", 64361da177e4SLinus Torvalds .data = &ipv6_devconf.temp_prefered_lft, 64371da177e4SLinus Torvalds .maxlen = sizeof(int), 64381da177e4SLinus Torvalds .mode = 0644, 64396d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 64401da177e4SLinus Torvalds }, 64411da177e4SLinus Torvalds { 64421da177e4SLinus Torvalds .procname = "regen_max_retry", 64431da177e4SLinus Torvalds .data = &ipv6_devconf.regen_max_retry, 64441da177e4SLinus Torvalds .maxlen = sizeof(int), 64451da177e4SLinus Torvalds .mode = 0644, 64466d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 64471da177e4SLinus Torvalds }, 64481da177e4SLinus Torvalds { 64491da177e4SLinus Torvalds .procname = "max_desync_factor", 64501da177e4SLinus Torvalds .data = &ipv6_devconf.max_desync_factor, 64511da177e4SLinus Torvalds .maxlen = sizeof(int), 64521da177e4SLinus Torvalds .mode = 0644, 64536d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 64541da177e4SLinus Torvalds }, 64551da177e4SLinus Torvalds { 64561da177e4SLinus Torvalds .procname = "max_addresses", 64571da177e4SLinus Torvalds .data = &ipv6_devconf.max_addresses, 64581da177e4SLinus Torvalds .maxlen = sizeof(int), 64591da177e4SLinus Torvalds .mode = 0644, 64606d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 64611da177e4SLinus Torvalds }, 64621da177e4SLinus Torvalds { 646365f5c7c1SYOSHIFUJI Hideaki .procname = "accept_ra_defrtr", 646465f5c7c1SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_defrtr, 646565f5c7c1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 646665f5c7c1SYOSHIFUJI Hideaki .mode = 0644, 64676d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 646865f5c7c1SYOSHIFUJI Hideaki }, 646965f5c7c1SYOSHIFUJI Hideaki { 64708013d1d7SHangbin Liu .procname = "accept_ra_min_hop_limit", 64718013d1d7SHangbin Liu .data = &ipv6_devconf.accept_ra_min_hop_limit, 64728013d1d7SHangbin Liu .maxlen = sizeof(int), 64738013d1d7SHangbin Liu .mode = 0644, 64748013d1d7SHangbin Liu .proc_handler = proc_dointvec, 64758013d1d7SHangbin Liu }, 64768013d1d7SHangbin Liu { 6477c4fd30ebSYOSHIFUJI Hideaki .procname = "accept_ra_pinfo", 6478c4fd30ebSYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_pinfo, 6479c4fd30ebSYOSHIFUJI Hideaki .maxlen = sizeof(int), 6480c4fd30ebSYOSHIFUJI Hideaki .mode = 0644, 64816d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 6482c4fd30ebSYOSHIFUJI Hideaki }, 6483930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 6484930d6ff2SYOSHIFUJI Hideaki { 6485930d6ff2SYOSHIFUJI Hideaki .procname = "accept_ra_rtr_pref", 6486930d6ff2SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rtr_pref, 6487930d6ff2SYOSHIFUJI Hideaki .maxlen = sizeof(int), 6488930d6ff2SYOSHIFUJI Hideaki .mode = 0644, 64896d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 6490930d6ff2SYOSHIFUJI Hideaki }, 649152e16356SYOSHIFUJI Hideaki { 649252e16356SYOSHIFUJI Hideaki .procname = "router_probe_interval", 649352e16356SYOSHIFUJI Hideaki .data = &ipv6_devconf.rtr_probe_interval, 649452e16356SYOSHIFUJI Hideaki .maxlen = sizeof(int), 649552e16356SYOSHIFUJI Hideaki .mode = 0644, 64966d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 649752e16356SYOSHIFUJI Hideaki }, 6498fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 649909c884d4SYOSHIFUJI Hideaki { 6500bbea124bSJoel Scherpelz .procname = "accept_ra_rt_info_min_plen", 6501bbea124bSJoel Scherpelz .data = &ipv6_devconf.accept_ra_rt_info_min_plen, 6502bbea124bSJoel Scherpelz .maxlen = sizeof(int), 6503bbea124bSJoel Scherpelz .mode = 0644, 6504bbea124bSJoel Scherpelz .proc_handler = proc_dointvec, 6505bbea124bSJoel Scherpelz }, 6506bbea124bSJoel Scherpelz { 650709c884d4SYOSHIFUJI Hideaki .procname = "accept_ra_rt_info_max_plen", 650809c884d4SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rt_info_max_plen, 650909c884d4SYOSHIFUJI Hideaki .maxlen = sizeof(int), 651009c884d4SYOSHIFUJI Hideaki .mode = 0644, 65116d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 651209c884d4SYOSHIFUJI Hideaki }, 651309c884d4SYOSHIFUJI Hideaki #endif 6514930d6ff2SYOSHIFUJI Hideaki #endif 6515c4fd30ebSYOSHIFUJI Hideaki { 6516fbea49e1SYOSHIFUJI Hideaki .procname = "proxy_ndp", 6517fbea49e1SYOSHIFUJI Hideaki .data = &ipv6_devconf.proxy_ndp, 6518fbea49e1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 6519fbea49e1SYOSHIFUJI Hideaki .mode = 0644, 6520c92d5491Sstephen hemminger .proc_handler = addrconf_sysctl_proxy_ndp, 6521fbea49e1SYOSHIFUJI Hideaki }, 6522fbea49e1SYOSHIFUJI Hideaki { 65230bcbc926SYOSHIFUJI Hideaki .procname = "accept_source_route", 65240bcbc926SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_source_route, 65250bcbc926SYOSHIFUJI Hideaki .maxlen = sizeof(int), 65260bcbc926SYOSHIFUJI Hideaki .mode = 0644, 65276d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 65280bcbc926SYOSHIFUJI Hideaki }, 652995c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 653095c385b4SNeil Horman { 653195c385b4SNeil Horman .procname = "optimistic_dad", 653295c385b4SNeil Horman .data = &ipv6_devconf.optimistic_dad, 653395c385b4SNeil Horman .maxlen = sizeof(int), 653495c385b4SNeil Horman .mode = 0644, 65356d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 653695c385b4SNeil Horman }, 65377fd2561eSErik Kline { 65387fd2561eSErik Kline .procname = "use_optimistic", 65397fd2561eSErik Kline .data = &ipv6_devconf.use_optimistic, 65407fd2561eSErik Kline .maxlen = sizeof(int), 65417fd2561eSErik Kline .mode = 0644, 65427fd2561eSErik Kline .proc_handler = proc_dointvec, 65437fd2561eSErik Kline }, 654495c385b4SNeil Horman #endif 65457bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 65467bc570c8SYOSHIFUJI Hideaki { 65477bc570c8SYOSHIFUJI Hideaki .procname = "mc_forwarding", 65487bc570c8SYOSHIFUJI Hideaki .data = &ipv6_devconf.mc_forwarding, 65497bc570c8SYOSHIFUJI Hideaki .maxlen = sizeof(int), 6550a4e6db07SDavid S. Miller .mode = 0444, 65516d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 65527bc570c8SYOSHIFUJI Hideaki }, 65537bc570c8SYOSHIFUJI Hideaki #endif 65540bcbc926SYOSHIFUJI Hideaki { 6555778d80beSYOSHIFUJI Hideaki .procname = "disable_ipv6", 6556778d80beSYOSHIFUJI Hideaki .data = &ipv6_devconf.disable_ipv6, 6557778d80beSYOSHIFUJI Hideaki .maxlen = sizeof(int), 6558778d80beSYOSHIFUJI Hideaki .mode = 0644, 655956d417b1SBrian Haley .proc_handler = addrconf_sysctl_disable, 6560778d80beSYOSHIFUJI Hideaki }, 6561778d80beSYOSHIFUJI Hideaki { 65621b34be74SYOSHIFUJI Hideaki .procname = "accept_dad", 65631b34be74SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_dad, 65641b34be74SYOSHIFUJI Hideaki .maxlen = sizeof(int), 65651b34be74SYOSHIFUJI Hideaki .mode = 0644, 65666d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 65671b34be74SYOSHIFUJI Hideaki }, 65681b34be74SYOSHIFUJI Hideaki { 6569f7734fdfSOctavian Purdila .procname = "force_tllao", 6570f7734fdfSOctavian Purdila .data = &ipv6_devconf.force_tllao, 6571f7734fdfSOctavian Purdila .maxlen = sizeof(int), 6572f7734fdfSOctavian Purdila .mode = 0644, 6573f7734fdfSOctavian Purdila .proc_handler = proc_dointvec 6574f7734fdfSOctavian Purdila }, 6575f7734fdfSOctavian Purdila { 65765cb04436SHannes Frederic Sowa .procname = "ndisc_notify", 65775cb04436SHannes Frederic Sowa .data = &ipv6_devconf.ndisc_notify, 65785cb04436SHannes Frederic Sowa .maxlen = sizeof(int), 65795cb04436SHannes Frederic Sowa .mode = 0644, 65805cb04436SHannes Frederic Sowa .proc_handler = proc_dointvec 65815cb04436SHannes Frederic Sowa }, 65825cb04436SHannes Frederic Sowa { 6583b800c3b9SHannes Frederic Sowa .procname = "suppress_frag_ndisc", 6584b800c3b9SHannes Frederic Sowa .data = &ipv6_devconf.suppress_frag_ndisc, 6585b800c3b9SHannes Frederic Sowa .maxlen = sizeof(int), 6586b800c3b9SHannes Frederic Sowa .mode = 0644, 6587b800c3b9SHannes Frederic Sowa .proc_handler = proc_dointvec 6588b800c3b9SHannes Frederic Sowa }, 6589b800c3b9SHannes Frederic Sowa { 6590d9333196SBen Greear .procname = "accept_ra_from_local", 6591d9333196SBen Greear .data = &ipv6_devconf.accept_ra_from_local, 6592d9333196SBen Greear .maxlen = sizeof(int), 6593d9333196SBen Greear .mode = 0644, 6594d9333196SBen Greear .proc_handler = proc_dointvec, 6595d9333196SBen Greear }, 6596d9333196SBen Greear { 6597c2943f14SHarout Hedeshian .procname = "accept_ra_mtu", 6598c2943f14SHarout Hedeshian .data = &ipv6_devconf.accept_ra_mtu, 6599c2943f14SHarout Hedeshian .maxlen = sizeof(int), 6600c2943f14SHarout Hedeshian .mode = 0644, 6601c2943f14SHarout Hedeshian .proc_handler = proc_dointvec, 6602c2943f14SHarout Hedeshian }, 6603c2943f14SHarout Hedeshian { 66043d1bec99SHannes Frederic Sowa .procname = "stable_secret", 66053d1bec99SHannes Frederic Sowa .data = &ipv6_devconf.stable_secret, 66063d1bec99SHannes Frederic Sowa .maxlen = IPV6_MAX_STRLEN, 66073d1bec99SHannes Frederic Sowa .mode = 0600, 66083d1bec99SHannes Frederic Sowa .proc_handler = addrconf_sysctl_stable_secret, 66093d1bec99SHannes Frederic Sowa }, 66103d1bec99SHannes Frederic Sowa { 66113985e8a3SErik Kline .procname = "use_oif_addrs_only", 66123985e8a3SErik Kline .data = &ipv6_devconf.use_oif_addrs_only, 66133985e8a3SErik Kline .maxlen = sizeof(int), 66143985e8a3SErik Kline .mode = 0644, 66153985e8a3SErik Kline .proc_handler = proc_dointvec, 661635103d11SAndy Gospodarek }, 661735103d11SAndy Gospodarek { 661835103d11SAndy Gospodarek .procname = "ignore_routes_with_linkdown", 661935103d11SAndy Gospodarek .data = &ipv6_devconf.ignore_routes_with_linkdown, 662035103d11SAndy Gospodarek .maxlen = sizeof(int), 662135103d11SAndy Gospodarek .mode = 0644, 662235103d11SAndy Gospodarek .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown, 66233985e8a3SErik Kline }, 66243985e8a3SErik Kline { 6625abbc3043SJohannes Berg .procname = "drop_unicast_in_l2_multicast", 6626abbc3043SJohannes Berg .data = &ipv6_devconf.drop_unicast_in_l2_multicast, 6627abbc3043SJohannes Berg .maxlen = sizeof(int), 6628abbc3043SJohannes Berg .mode = 0644, 6629abbc3043SJohannes Berg .proc_handler = proc_dointvec, 6630abbc3043SJohannes Berg }, 6631abbc3043SJohannes Berg { 66327a02bf89SJohannes Berg .procname = "drop_unsolicited_na", 66337a02bf89SJohannes Berg .data = &ipv6_devconf.drop_unsolicited_na, 66347a02bf89SJohannes Berg .maxlen = sizeof(int), 66357a02bf89SJohannes Berg .mode = 0644, 66367a02bf89SJohannes Berg .proc_handler = proc_dointvec, 66377a02bf89SJohannes Berg }, 66387a02bf89SJohannes Berg { 6639f1705ec1SDavid Ahern .procname = "keep_addr_on_down", 6640f1705ec1SDavid Ahern .data = &ipv6_devconf.keep_addr_on_down, 6641f1705ec1SDavid Ahern .maxlen = sizeof(int), 6642f1705ec1SDavid Ahern .mode = 0644, 6643f1705ec1SDavid Ahern .proc_handler = proc_dointvec, 6644f1705ec1SDavid Ahern 6645f1705ec1SDavid Ahern }, 6646f1705ec1SDavid Ahern { 66471ababebaSDavid Lebrun .procname = "seg6_enabled", 66481ababebaSDavid Lebrun .data = &ipv6_devconf.seg6_enabled, 66491ababebaSDavid Lebrun .maxlen = sizeof(int), 66501ababebaSDavid Lebrun .mode = 0644, 66511ababebaSDavid Lebrun .proc_handler = proc_dointvec, 66521ababebaSDavid Lebrun }, 6653bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC 6654bf355b8dSDavid Lebrun { 6655bf355b8dSDavid Lebrun .procname = "seg6_require_hmac", 6656bf355b8dSDavid Lebrun .data = &ipv6_devconf.seg6_require_hmac, 6657bf355b8dSDavid Lebrun .maxlen = sizeof(int), 6658bf355b8dSDavid Lebrun .mode = 0644, 6659bf355b8dSDavid Lebrun .proc_handler = proc_dointvec, 6660bf355b8dSDavid Lebrun }, 6661bf355b8dSDavid Lebrun #endif 66621ababebaSDavid Lebrun { 6663adc176c5SErik Nordmark .procname = "enhanced_dad", 6664adc176c5SErik Nordmark .data = &ipv6_devconf.enhanced_dad, 6665adc176c5SErik Nordmark .maxlen = sizeof(int), 6666adc176c5SErik Nordmark .mode = 0644, 6667adc176c5SErik Nordmark .proc_handler = proc_dointvec, 6668adc176c5SErik Nordmark }, 6669adc176c5SErik Nordmark { 6670d35a00b8SFelix Jia .procname = "addr_gen_mode", 6671d35a00b8SFelix Jia .data = &ipv6_devconf.addr_gen_mode, 6672d35a00b8SFelix Jia .maxlen = sizeof(int), 6673d35a00b8SFelix Jia .mode = 0644, 6674d35a00b8SFelix Jia .proc_handler = addrconf_sysctl_addr_gen_mode, 6675d35a00b8SFelix Jia }, 6676d35a00b8SFelix Jia { 6677df789fe7SDavid Forster .procname = "disable_policy", 6678df789fe7SDavid Forster .data = &ipv6_devconf.disable_policy, 6679df789fe7SDavid Forster .maxlen = sizeof(int), 6680df789fe7SDavid Forster .mode = 0644, 6681df789fe7SDavid Forster .proc_handler = addrconf_sysctl_disable_policy, 6682df789fe7SDavid Forster }, 6683df789fe7SDavid Forster { 66842210d6b2SMaciej Żenczykowski .procname = "ndisc_tclass", 66852210d6b2SMaciej Żenczykowski .data = &ipv6_devconf.ndisc_tclass, 66862210d6b2SMaciej Żenczykowski .maxlen = sizeof(int), 66872210d6b2SMaciej Żenczykowski .mode = 0644, 66882210d6b2SMaciej Żenczykowski .proc_handler = proc_dointvec_minmax, 66892210d6b2SMaciej Żenczykowski .extra1 = (void *)&zero, 66902210d6b2SMaciej Żenczykowski .extra2 = (void *)&two_five_five, 66912210d6b2SMaciej Żenczykowski }, 66922210d6b2SMaciej Żenczykowski { 6693f8572d8fSEric W. Biederman /* sentinel */ 66941da177e4SLinus Torvalds } 66951da177e4SLinus Torvalds }; 66961da177e4SLinus Torvalds 6697bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name, 6698f8572d8fSEric W. Biederman struct inet6_dev *idev, struct ipv6_devconf *p) 66991da177e4SLinus Torvalds { 670029c994e3SNicolas Dichtel int i, ifindex; 6701607ea7cdSKonstantin Khlebnikov struct ctl_table *table; 67026105e293SEric W. Biederman char path[sizeof("net/ipv6/conf/") + IFNAMSIZ]; 67031dab6222SPavel Emelyanov 6704607ea7cdSKonstantin Khlebnikov table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL); 6705607ea7cdSKonstantin Khlebnikov if (!table) 6706f68635e6SPavel Emelyanov goto out; 6707f68635e6SPavel Emelyanov 6708607ea7cdSKonstantin Khlebnikov for (i = 0; table[i].data; i++) { 6709607ea7cdSKonstantin Khlebnikov table[i].data += (char *)p - (char *)&ipv6_devconf; 6710cb9e684eSMaciej Żenczykowski /* If one of these is already set, then it is not safe to 6711cb9e684eSMaciej Żenczykowski * overwrite either of them: this makes proc_dointvec_minmax 6712cb9e684eSMaciej Żenczykowski * usable. 6713cb9e684eSMaciej Żenczykowski */ 6714cb9e684eSMaciej Żenczykowski if (!table[i].extra1 && !table[i].extra2) { 6715607ea7cdSKonstantin Khlebnikov table[i].extra1 = idev; /* embedded; no ref */ 6716607ea7cdSKonstantin Khlebnikov table[i].extra2 = net; 67171da177e4SLinus Torvalds } 6718cb9e684eSMaciej Żenczykowski } 67191da177e4SLinus Torvalds 67206105e293SEric W. Biederman snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name); 67211da177e4SLinus Torvalds 6722607ea7cdSKonstantin Khlebnikov p->sysctl_header = register_net_sysctl(net, path, table); 6723607ea7cdSKonstantin Khlebnikov if (!p->sysctl_header) 67246105e293SEric W. Biederman goto free; 6725f68635e6SPavel Emelyanov 672629c994e3SNicolas Dichtel if (!strcmp(dev_name, "all")) 672729c994e3SNicolas Dichtel ifindex = NETCONFA_IFINDEX_ALL; 672829c994e3SNicolas Dichtel else if (!strcmp(dev_name, "default")) 672929c994e3SNicolas Dichtel ifindex = NETCONFA_IFINDEX_DEFAULT; 673029c994e3SNicolas Dichtel else 673129c994e3SNicolas Dichtel ifindex = idev->dev->ifindex; 673285b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL, 673385b3daadSDavid Ahern ifindex, p); 673495897312SPavel Emelyanov return 0; 67351da177e4SLinus Torvalds 67361da177e4SLinus Torvalds free: 6737607ea7cdSKonstantin Khlebnikov kfree(table); 6738f68635e6SPavel Emelyanov out: 673995897312SPavel Emelyanov return -ENOBUFS; 67401da177e4SLinus Torvalds } 67411da177e4SLinus Torvalds 674223452170SDavid Ahern static void __addrconf_sysctl_unregister(struct net *net, 674323452170SDavid Ahern struct ipv6_devconf *p, int ifindex) 6744f52295a9SPavel Emelyanov { 6745607ea7cdSKonstantin Khlebnikov struct ctl_table *table; 6746f52295a9SPavel Emelyanov 6747607ea7cdSKonstantin Khlebnikov if (!p->sysctl_header) 6748408c4768SPavel Emelyanov return; 6749408c4768SPavel Emelyanov 6750607ea7cdSKonstantin Khlebnikov table = p->sysctl_header->ctl_table_arg; 6751607ea7cdSKonstantin Khlebnikov unregister_net_sysctl_table(p->sysctl_header); 6752607ea7cdSKonstantin Khlebnikov p->sysctl_header = NULL; 6753607ea7cdSKonstantin Khlebnikov kfree(table); 675423452170SDavid Ahern 675523452170SDavid Ahern inet6_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL); 67561da177e4SLinus Torvalds } 6757408c4768SPavel Emelyanov 6758a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev) 6759408c4768SPavel Emelyanov { 6760a317a2f1SWANG Cong int err; 6761a317a2f1SWANG Cong 6762a317a2f1SWANG Cong if (!sysctl_dev_name_is_allowed(idev->dev->name)) 6763a317a2f1SWANG Cong return -EINVAL; 6764a317a2f1SWANG Cong 6765a317a2f1SWANG Cong err = neigh_sysctl_register(idev->dev, idev->nd_parms, 6766f8572d8fSEric W. Biederman &ndisc_ifinfo_sysctl_change); 6767a317a2f1SWANG Cong if (err) 6768a317a2f1SWANG Cong return err; 6769a317a2f1SWANG Cong err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name, 6770f8572d8fSEric W. Biederman idev, &idev->cnf); 6771a317a2f1SWANG Cong if (err) 6772a317a2f1SWANG Cong neigh_sysctl_unregister(idev->nd_parms); 6773a317a2f1SWANG Cong 6774a317a2f1SWANG Cong return err; 6775408c4768SPavel Emelyanov } 6776408c4768SPavel Emelyanov 6777408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev) 6778408c4768SPavel Emelyanov { 677923452170SDavid Ahern __addrconf_sysctl_unregister(dev_net(idev->dev), &idev->cnf, 678023452170SDavid Ahern idev->dev->ifindex); 6781408c4768SPavel Emelyanov neigh_sysctl_unregister(idev->nd_parms); 67821da177e4SLinus Torvalds } 67831da177e4SLinus Torvalds 67841da177e4SLinus Torvalds 67851da177e4SLinus Torvalds #endif 67861da177e4SLinus Torvalds 67872c8c1e72SAlexey Dobriyan static int __net_init addrconf_init_net(struct net *net) 6788e0da5a48SPavel Emelyanov { 6789a79ca223SHong Zhiguo int err = -ENOMEM; 6790e0da5a48SPavel Emelyanov struct ipv6_devconf *all, *dflt; 6791e0da5a48SPavel Emelyanov 6792a79ca223SHong Zhiguo all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL); 679363159f29SIan Morris if (!all) 6794e0da5a48SPavel Emelyanov goto err_alloc_all; 6795e0da5a48SPavel Emelyanov 6796a79ca223SHong Zhiguo dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL); 679763159f29SIan Morris if (!dflt) 6798e0da5a48SPavel Emelyanov goto err_alloc_dflt; 6799a79ca223SHong Zhiguo 680056d417b1SBrian Haley /* these will be inherited by all namespaces */ 680156d417b1SBrian Haley dflt->autoconf = ipv6_defaults.autoconf; 680256d417b1SBrian Haley dflt->disable_ipv6 = ipv6_defaults.disable_ipv6; 6803e0da5a48SPavel Emelyanov 68043d1bec99SHannes Frederic Sowa dflt->stable_secret.initialized = false; 68053d1bec99SHannes Frederic Sowa all->stable_secret.initialized = false; 68063d1bec99SHannes Frederic Sowa 6807e0da5a48SPavel Emelyanov net->ipv6.devconf_all = all; 6808e0da5a48SPavel Emelyanov net->ipv6.devconf_dflt = dflt; 6809e0da5a48SPavel Emelyanov 6810e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 6811f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "all", NULL, all); 6812e0da5a48SPavel Emelyanov if (err < 0) 6813e0da5a48SPavel Emelyanov goto err_reg_all; 6814e0da5a48SPavel Emelyanov 6815f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "default", NULL, dflt); 6816e0da5a48SPavel Emelyanov if (err < 0) 6817e0da5a48SPavel Emelyanov goto err_reg_dflt; 6818e0da5a48SPavel Emelyanov #endif 6819e0da5a48SPavel Emelyanov return 0; 6820e0da5a48SPavel Emelyanov 6821e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 6822e0da5a48SPavel Emelyanov err_reg_dflt: 682323452170SDavid Ahern __addrconf_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL); 6824e0da5a48SPavel Emelyanov err_reg_all: 6825e0da5a48SPavel Emelyanov kfree(dflt); 6826e0da5a48SPavel Emelyanov #endif 6827e0da5a48SPavel Emelyanov err_alloc_dflt: 6828e0da5a48SPavel Emelyanov kfree(all); 6829e0da5a48SPavel Emelyanov err_alloc_all: 6830e0da5a48SPavel Emelyanov return err; 6831e0da5a48SPavel Emelyanov } 6832e0da5a48SPavel Emelyanov 68332c8c1e72SAlexey Dobriyan static void __net_exit addrconf_exit_net(struct net *net) 6834e0da5a48SPavel Emelyanov { 6835e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 683623452170SDavid Ahern __addrconf_sysctl_unregister(net, net->ipv6.devconf_dflt, 683723452170SDavid Ahern NETCONFA_IFINDEX_DEFAULT); 683823452170SDavid Ahern __addrconf_sysctl_unregister(net, net->ipv6.devconf_all, 683923452170SDavid Ahern NETCONFA_IFINDEX_ALL); 6840e0da5a48SPavel Emelyanov #endif 6841e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_dflt); 6842e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_all); 6843e0da5a48SPavel Emelyanov } 6844e0da5a48SPavel Emelyanov 6845e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = { 6846e0da5a48SPavel Emelyanov .init = addrconf_init_net, 6847e0da5a48SPavel Emelyanov .exit = addrconf_exit_net, 6848e0da5a48SPavel Emelyanov }; 6849e0da5a48SPavel Emelyanov 6850207895fdSDaniel Borkmann static struct rtnl_af_ops inet6_ops __read_mostly = { 6851b382b191SThomas Graf .family = AF_INET6, 6852b382b191SThomas Graf .fill_link_af = inet6_fill_link_af, 6853b382b191SThomas Graf .get_link_af_size = inet6_get_link_af_size, 685411b1f828SDaniel Borkmann .validate_link_af = inet6_validate_link_af, 6855f53adae4SDaniel Borkmann .set_link_af = inet6_set_link_af, 6856b382b191SThomas Graf }; 6857b382b191SThomas Graf 68581da177e4SLinus Torvalds /* 68591da177e4SLinus Torvalds * Init / cleanup code 68601da177e4SLinus Torvalds */ 68611da177e4SLinus Torvalds 68621da177e4SLinus Torvalds int __init addrconf_init(void) 68631da177e4SLinus Torvalds { 6864a317a2f1SWANG Cong struct inet6_dev *idev; 6865c2e21293Sstephen hemminger int i, err; 68662a8cc6c8SYOSHIFUJI Hideaki 6867e21e8467SStephen Hemminger err = ipv6_addr_label_init(); 6868e21e8467SStephen Hemminger if (err < 0) { 6869f3213831SJoe Perches pr_crit("%s: cannot initialize default policy table: %d\n", 6870f3213831SJoe Perches __func__, err); 68712cc6d2bfSNeil Horman goto out; 68722a8cc6c8SYOSHIFUJI Hideaki } 68731da177e4SLinus Torvalds 68742cc6d2bfSNeil Horman err = register_pernet_subsys(&addrconf_ops); 68752cc6d2bfSNeil Horman if (err < 0) 68762cc6d2bfSNeil Horman goto out_addrlabel; 6877e0da5a48SPavel Emelyanov 6878c15b1ccaSHannes Frederic Sowa addrconf_wq = create_workqueue("ipv6_addrconf"); 6879c15b1ccaSHannes Frederic Sowa if (!addrconf_wq) { 6880c15b1ccaSHannes Frederic Sowa err = -ENOMEM; 6881c15b1ccaSHannes Frederic Sowa goto out_nowq; 6882c15b1ccaSHannes Frederic Sowa } 6883c15b1ccaSHannes Frederic Sowa 68841da177e4SLinus Torvalds /* The addrconf netdev notifier requires that loopback_dev 68851da177e4SLinus Torvalds * has it's ipv6 private information allocated and setup 68861da177e4SLinus Torvalds * before it can bring up and give link-local addresses 68871da177e4SLinus Torvalds * to other devices which are up. 68881da177e4SLinus Torvalds * 68891da177e4SLinus Torvalds * Unfortunately, loopback_dev is not necessarily the first 68901da177e4SLinus Torvalds * entry in the global dev_base list of net devices. In fact, 68911da177e4SLinus Torvalds * it is likely to be the very last entry on that list. 68921da177e4SLinus Torvalds * So this causes the notifier registry below to try and 68931da177e4SLinus Torvalds * give link-local addresses to all devices besides loopback_dev 68941da177e4SLinus Torvalds * first, then loopback_dev, which cases all the non-loopback_dev 68951da177e4SLinus Torvalds * devices to fail to get a link-local address. 68961da177e4SLinus Torvalds * 68971da177e4SLinus Torvalds * So, as a temporary fix, allocate the ipv6 structure for 68981da177e4SLinus Torvalds * loopback_dev first by hand. 68991da177e4SLinus Torvalds * Longer term, all of the dependencies ipv6 has upon the loopback 69001da177e4SLinus Torvalds * device and it being up should be removed. 69011da177e4SLinus Torvalds */ 69021da177e4SLinus Torvalds rtnl_lock(); 6903a317a2f1SWANG Cong idev = ipv6_add_dev(init_net.loopback_dev); 69041da177e4SLinus Torvalds rtnl_unlock(); 6905a317a2f1SWANG Cong if (IS_ERR(idev)) { 6906a317a2f1SWANG Cong err = PTR_ERR(idev); 6907e0da5a48SPavel Emelyanov goto errlo; 6908a317a2f1SWANG Cong } 69091da177e4SLinus Torvalds 69102f460933SWANG Cong ip6_route_init_special_entries(); 69112f460933SWANG Cong 6912c2e21293Sstephen hemminger for (i = 0; i < IN6_ADDR_HSIZE; i++) 6913c2e21293Sstephen hemminger INIT_HLIST_HEAD(&inet6_addr_lst[i]); 6914c2e21293Sstephen hemminger 69151da177e4SLinus Torvalds register_netdevice_notifier(&ipv6_dev_notf); 69161da177e4SLinus Torvalds 6917c15b1ccaSHannes Frederic Sowa addrconf_verify(); 6918c127ea2cSThomas Graf 69193678a9d8Sstephen hemminger rtnl_af_register(&inet6_ops); 6920b382b191SThomas Graf 692116feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETLINK, 692216feebcfSFlorian Westphal NULL, inet6_dump_ifinfo, 0); 6923c127ea2cSThomas Graf if (err < 0) 6924c127ea2cSThomas Graf goto errout; 6925c127ea2cSThomas Graf 692616feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWADDR, 692716feebcfSFlorian Westphal inet6_rtm_newaddr, NULL, 0); 692816feebcfSFlorian Westphal if (err < 0) 692916feebcfSFlorian Westphal goto errout; 693016feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELADDR, 693116feebcfSFlorian Westphal inet6_rtm_deladdr, NULL, 0); 693216feebcfSFlorian Westphal if (err < 0) 693316feebcfSFlorian Westphal goto errout; 693416feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETADDR, 693516feebcfSFlorian Westphal inet6_rtm_getaddr, inet6_dump_ifaddr, 693616feebcfSFlorian Westphal RTNL_FLAG_DOIT_UNLOCKED); 693716feebcfSFlorian Westphal if (err < 0) 693816feebcfSFlorian Westphal goto errout; 693916feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETMULTICAST, 694016feebcfSFlorian Westphal NULL, inet6_dump_ifmcaddr, 0); 694116feebcfSFlorian Westphal if (err < 0) 694216feebcfSFlorian Westphal goto errout; 694316feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETANYCAST, 694416feebcfSFlorian Westphal NULL, inet6_dump_ifacaddr, 0); 694516feebcfSFlorian Westphal if (err < 0) 694616feebcfSFlorian Westphal goto errout; 694716feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETNETCONF, 694816feebcfSFlorian Westphal inet6_netconf_get_devconf, 694916feebcfSFlorian Westphal inet6_netconf_dump_devconf, 695016feebcfSFlorian Westphal RTNL_FLAG_DOIT_UNLOCKED); 695116feebcfSFlorian Westphal if (err < 0) 695216feebcfSFlorian Westphal goto errout; 6953a3fde2adSFlorian Westphal err = ipv6_addr_label_rtnl_register(); 6954a3fde2adSFlorian Westphal if (err < 0) 6955a3fde2adSFlorian Westphal goto errout; 69562a8cc6c8SYOSHIFUJI Hideaki 69571da177e4SLinus Torvalds return 0; 6958c127ea2cSThomas Graf errout: 695916feebcfSFlorian Westphal rtnl_unregister_all(PF_INET6); 6960b382b191SThomas Graf rtnl_af_unregister(&inet6_ops); 6961c127ea2cSThomas Graf unregister_netdevice_notifier(&ipv6_dev_notf); 6962e0da5a48SPavel Emelyanov errlo: 6963c15b1ccaSHannes Frederic Sowa destroy_workqueue(addrconf_wq); 6964c15b1ccaSHannes Frederic Sowa out_nowq: 6965e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 69662cc6d2bfSNeil Horman out_addrlabel: 69672cc6d2bfSNeil Horman ipv6_addr_label_cleanup(); 69682cc6d2bfSNeil Horman out: 6969c127ea2cSThomas Graf return err; 69701da177e4SLinus Torvalds } 69711da177e4SLinus Torvalds 697209f7709fSDaniel Lezcano void addrconf_cleanup(void) 69731da177e4SLinus Torvalds { 6974176c39afSDaniel Lezcano struct net_device *dev; 69751da177e4SLinus Torvalds int i; 69761da177e4SLinus Torvalds 69771da177e4SLinus Torvalds unregister_netdevice_notifier(&ipv6_dev_notf); 6978e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 69792cc6d2bfSNeil Horman ipv6_addr_label_cleanup(); 69801da177e4SLinus Torvalds 69815c45121dSFlorian Westphal rtnl_af_unregister(&inet6_ops); 69821da177e4SLinus Torvalds 69835c45121dSFlorian Westphal rtnl_lock(); 6984b382b191SThomas Graf 6985176c39afSDaniel Lezcano /* clean dev list */ 6986176c39afSDaniel Lezcano for_each_netdev(&init_net, dev) { 6987176c39afSDaniel Lezcano if (__in6_dev_get(dev) == NULL) 6988176c39afSDaniel Lezcano continue; 6989176c39afSDaniel Lezcano addrconf_ifdown(dev, 1); 6990176c39afSDaniel Lezcano } 6991176c39afSDaniel Lezcano addrconf_ifdown(init_net.loopback_dev, 2); 6992176c39afSDaniel Lezcano 69931da177e4SLinus Torvalds /* 69941da177e4SLinus Torvalds * Check hash table. 69951da177e4SLinus Torvalds */ 69965c578aedSstephen hemminger spin_lock_bh(&addrconf_hash_lock); 6997c2e21293Sstephen hemminger for (i = 0; i < IN6_ADDR_HSIZE; i++) 6998c2e21293Sstephen hemminger WARN_ON(!hlist_empty(&inet6_addr_lst[i])); 69995c578aedSstephen hemminger spin_unlock_bh(&addrconf_hash_lock); 7000c15b1ccaSHannes Frederic Sowa cancel_delayed_work(&addr_chk_work); 70011da177e4SLinus Torvalds rtnl_unlock(); 7002c15b1ccaSHannes Frederic Sowa 7003c15b1ccaSHannes Frederic Sowa destroy_workqueue(addrconf_wq); 70041da177e4SLinus Torvalds } 7005