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 { 6697a674200SNicolas Dichtel struct net *net = sock_net(skb->sk); 6707a674200SNicolas Dichtel int h, s_h; 6717a674200SNicolas Dichtel int idx, s_idx; 6727a674200SNicolas Dichtel struct net_device *dev; 6737a674200SNicolas Dichtel struct inet6_dev *idev; 6747a674200SNicolas Dichtel struct hlist_head *head; 6757a674200SNicolas Dichtel 6767a674200SNicolas Dichtel s_h = cb->args[0]; 6777a674200SNicolas Dichtel s_idx = idx = cb->args[1]; 6787a674200SNicolas Dichtel 6797a674200SNicolas Dichtel for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 6807a674200SNicolas Dichtel idx = 0; 6817a674200SNicolas Dichtel head = &net->dev_index_head[h]; 6827a674200SNicolas Dichtel rcu_read_lock(); 68363998ac2SNicolas Dichtel cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ 68463998ac2SNicolas Dichtel net->dev_base_seq; 6857a674200SNicolas Dichtel hlist_for_each_entry_rcu(dev, head, index_hlist) { 6867a674200SNicolas Dichtel if (idx < s_idx) 6877a674200SNicolas Dichtel goto cont; 6887a674200SNicolas Dichtel idev = __in6_dev_get(dev); 6897a674200SNicolas Dichtel if (!idev) 6907a674200SNicolas Dichtel goto cont; 6917a674200SNicolas Dichtel 6927a674200SNicolas Dichtel if (inet6_netconf_fill_devconf(skb, dev->ifindex, 6937a674200SNicolas Dichtel &idev->cnf, 6947a674200SNicolas Dichtel NETLINK_CB(cb->skb).portid, 6957a674200SNicolas Dichtel cb->nlh->nlmsg_seq, 6967a674200SNicolas Dichtel RTM_NEWNETCONF, 6977a674200SNicolas Dichtel NLM_F_MULTI, 698136ba622SZhang Shengju NETCONFA_ALL) < 0) { 6997a674200SNicolas Dichtel rcu_read_unlock(); 7007a674200SNicolas Dichtel goto done; 7017a674200SNicolas Dichtel } 70263998ac2SNicolas Dichtel nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 7037a674200SNicolas Dichtel cont: 7047a674200SNicolas Dichtel idx++; 7057a674200SNicolas Dichtel } 7067a674200SNicolas Dichtel rcu_read_unlock(); 7077a674200SNicolas Dichtel } 7087a674200SNicolas Dichtel if (h == NETDEV_HASHENTRIES) { 7097a674200SNicolas Dichtel if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL, 7107a674200SNicolas Dichtel net->ipv6.devconf_all, 7117a674200SNicolas Dichtel NETLINK_CB(cb->skb).portid, 7127a674200SNicolas Dichtel cb->nlh->nlmsg_seq, 7137a674200SNicolas Dichtel RTM_NEWNETCONF, NLM_F_MULTI, 714136ba622SZhang Shengju NETCONFA_ALL) < 0) 7157a674200SNicolas Dichtel goto done; 7167a674200SNicolas Dichtel else 7177a674200SNicolas Dichtel h++; 7187a674200SNicolas Dichtel } 7197a674200SNicolas Dichtel if (h == NETDEV_HASHENTRIES + 1) { 7207a674200SNicolas Dichtel if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT, 7217a674200SNicolas Dichtel net->ipv6.devconf_dflt, 7227a674200SNicolas Dichtel NETLINK_CB(cb->skb).portid, 7237a674200SNicolas Dichtel cb->nlh->nlmsg_seq, 7247a674200SNicolas Dichtel RTM_NEWNETCONF, NLM_F_MULTI, 725136ba622SZhang Shengju NETCONFA_ALL) < 0) 7267a674200SNicolas Dichtel goto done; 7277a674200SNicolas Dichtel else 7287a674200SNicolas Dichtel h++; 7297a674200SNicolas Dichtel } 7307a674200SNicolas Dichtel done: 7317a674200SNicolas Dichtel cb->args[0] = h; 7327a674200SNicolas Dichtel cb->args[1] = idx; 7337a674200SNicolas Dichtel 7347a674200SNicolas Dichtel return skb->len; 7357a674200SNicolas Dichtel } 7367a674200SNicolas Dichtel 7371da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 7381da177e4SLinus Torvalds static void dev_forward_change(struct inet6_dev *idev) 7391da177e4SLinus Torvalds { 7401da177e4SLinus Torvalds struct net_device *dev; 7411da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 7421da177e4SLinus Torvalds 7431da177e4SLinus Torvalds if (!idev) 7441da177e4SLinus Torvalds return; 7451da177e4SLinus Torvalds dev = idev->dev; 7460187bdfbSBen Hutchings if (idev->cnf.forwarding) 7470187bdfbSBen Hutchings dev_disable_lro(dev); 7481a940835SAmerigo Wang if (dev->flags & IFF_MULTICAST) { 7492c5e8933SHannes Frederic Sowa if (idev->cnf.forwarding) { 750f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters); 7512c5e8933SHannes Frederic Sowa ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters); 7522c5e8933SHannes Frederic Sowa ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters); 7532c5e8933SHannes Frederic Sowa } else { 754f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters); 7552c5e8933SHannes Frederic Sowa ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters); 7562c5e8933SHannes Frederic Sowa ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters); 7572c5e8933SHannes Frederic Sowa } 7581da177e4SLinus Torvalds } 759502a2ffdSstephen hemminger 760502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 7612c12a74cSMichal Wrobel if (ifa->flags&IFA_F_TENTATIVE) 7622c12a74cSMichal Wrobel continue; 7631da177e4SLinus Torvalds if (idev->cnf.forwarding) 7641da177e4SLinus Torvalds addrconf_join_anycast(ifa); 7651da177e4SLinus Torvalds else 7661da177e4SLinus Torvalds addrconf_leave_anycast(ifa); 7671da177e4SLinus Torvalds } 76885b3daadSDavid Ahern inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF, 76985b3daadSDavid Ahern NETCONFA_FORWARDING, 770f3a1bfb1SNicolas Dichtel dev->ifindex, &idev->cnf); 7711da177e4SLinus Torvalds } 7721da177e4SLinus Torvalds 7731da177e4SLinus Torvalds 774e186932bSPavel Emelyanov static void addrconf_forward_change(struct net *net, __s32 newf) 7751da177e4SLinus Torvalds { 7761da177e4SLinus Torvalds struct net_device *dev; 7771da177e4SLinus Torvalds struct inet6_dev *idev; 7781da177e4SLinus Torvalds 7794acd4945SBen Hutchings for_each_netdev(net, dev) { 7801da177e4SLinus Torvalds idev = __in6_dev_get(dev); 7811da177e4SLinus Torvalds if (idev) { 782e186932bSPavel Emelyanov int changed = (!idev->cnf.forwarding) ^ (!newf); 783e186932bSPavel Emelyanov idev->cnf.forwarding = newf; 7841da177e4SLinus Torvalds if (changed) 7851da177e4SLinus Torvalds dev_forward_change(idev); 7861da177e4SLinus Torvalds } 7871da177e4SLinus Torvalds } 7881da177e4SLinus Torvalds } 789c8fecf22SPavel Emelyanov 790013d97e9SFrancesco Ruggeri static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf) 791c8fecf22SPavel Emelyanov { 792bff16c2fSPavel Emelyanov struct net *net; 793013d97e9SFrancesco Ruggeri int old; 794013d97e9SFrancesco Ruggeri 795013d97e9SFrancesco Ruggeri if (!rtnl_trylock()) 796013d97e9SFrancesco Ruggeri return restart_syscall(); 797bff16c2fSPavel Emelyanov 798bff16c2fSPavel Emelyanov net = (struct net *)table->extra2; 799013d97e9SFrancesco Ruggeri old = *p; 800013d97e9SFrancesco Ruggeri *p = newf; 801c8fecf22SPavel Emelyanov 802013d97e9SFrancesco Ruggeri if (p == &net->ipv6.devconf_dflt->forwarding) { 803f3a1bfb1SNicolas Dichtel if ((!newf) ^ (!old)) 80485b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 80585b3daadSDavid Ahern NETCONFA_FORWARDING, 806f3a1bfb1SNicolas Dichtel NETCONFA_IFINDEX_DEFAULT, 807f3a1bfb1SNicolas Dichtel net->ipv6.devconf_dflt); 808013d97e9SFrancesco Ruggeri rtnl_unlock(); 809013d97e9SFrancesco Ruggeri return 0; 81088af182eSEric W. Biederman } 811b325fddbSStephen Hemminger 812e186932bSPavel Emelyanov if (p == &net->ipv6.devconf_all->forwarding) { 813d26c638cSNicolas Dichtel int old_dflt = net->ipv6.devconf_dflt->forwarding; 814d26c638cSNicolas Dichtel 815e186932bSPavel Emelyanov net->ipv6.devconf_dflt->forwarding = newf; 816d26c638cSNicolas Dichtel if ((!newf) ^ (!old_dflt)) 81785b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 81885b3daadSDavid Ahern NETCONFA_FORWARDING, 819d26c638cSNicolas Dichtel NETCONFA_IFINDEX_DEFAULT, 820d26c638cSNicolas Dichtel net->ipv6.devconf_dflt); 821d26c638cSNicolas Dichtel 822e186932bSPavel Emelyanov addrconf_forward_change(net, newf); 823f3a1bfb1SNicolas Dichtel if ((!newf) ^ (!old)) 82485b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 82585b3daadSDavid Ahern NETCONFA_FORWARDING, 826f3a1bfb1SNicolas Dichtel NETCONFA_IFINDEX_ALL, 827f3a1bfb1SNicolas Dichtel net->ipv6.devconf_all); 828013d97e9SFrancesco Ruggeri } else if ((!newf) ^ (!old)) 829c8fecf22SPavel Emelyanov dev_forward_change((struct inet6_dev *)table->extra1); 8300187bdfbSBen Hutchings rtnl_unlock(); 831c8fecf22SPavel Emelyanov 832013d97e9SFrancesco Ruggeri if (newf) 8337b4da532SDaniel Lezcano rt6_purge_dflt_routers(net); 834b325fddbSStephen Hemminger return 1; 835c8fecf22SPavel Emelyanov } 83635103d11SAndy Gospodarek 83735103d11SAndy Gospodarek static void addrconf_linkdown_change(struct net *net, __s32 newf) 83835103d11SAndy Gospodarek { 83935103d11SAndy Gospodarek struct net_device *dev; 84035103d11SAndy Gospodarek struct inet6_dev *idev; 84135103d11SAndy Gospodarek 84235103d11SAndy Gospodarek for_each_netdev(net, dev) { 84335103d11SAndy Gospodarek idev = __in6_dev_get(dev); 84435103d11SAndy Gospodarek if (idev) { 84535103d11SAndy Gospodarek int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf); 84635103d11SAndy Gospodarek 84735103d11SAndy Gospodarek idev->cnf.ignore_routes_with_linkdown = newf; 84835103d11SAndy Gospodarek if (changed) 84935103d11SAndy Gospodarek inet6_netconf_notify_devconf(dev_net(dev), 85085b3daadSDavid Ahern RTM_NEWNETCONF, 85135103d11SAndy Gospodarek NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 85235103d11SAndy Gospodarek dev->ifindex, 85335103d11SAndy Gospodarek &idev->cnf); 85435103d11SAndy Gospodarek } 85535103d11SAndy Gospodarek } 85635103d11SAndy Gospodarek } 85735103d11SAndy Gospodarek 85835103d11SAndy Gospodarek static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf) 85935103d11SAndy Gospodarek { 86035103d11SAndy Gospodarek struct net *net; 86135103d11SAndy Gospodarek int old; 86235103d11SAndy Gospodarek 86335103d11SAndy Gospodarek if (!rtnl_trylock()) 86435103d11SAndy Gospodarek return restart_syscall(); 86535103d11SAndy Gospodarek 86635103d11SAndy Gospodarek net = (struct net *)table->extra2; 86735103d11SAndy Gospodarek old = *p; 86835103d11SAndy Gospodarek *p = newf; 86935103d11SAndy Gospodarek 87035103d11SAndy Gospodarek if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) { 87135103d11SAndy Gospodarek if ((!newf) ^ (!old)) 87235103d11SAndy Gospodarek inet6_netconf_notify_devconf(net, 87385b3daadSDavid Ahern RTM_NEWNETCONF, 87435103d11SAndy Gospodarek NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 87535103d11SAndy Gospodarek NETCONFA_IFINDEX_DEFAULT, 87635103d11SAndy Gospodarek net->ipv6.devconf_dflt); 87735103d11SAndy Gospodarek rtnl_unlock(); 87835103d11SAndy Gospodarek return 0; 87935103d11SAndy Gospodarek } 88035103d11SAndy Gospodarek 88135103d11SAndy Gospodarek if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) { 88235103d11SAndy Gospodarek net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf; 88335103d11SAndy Gospodarek addrconf_linkdown_change(net, newf); 88435103d11SAndy Gospodarek if ((!newf) ^ (!old)) 88535103d11SAndy Gospodarek inet6_netconf_notify_devconf(net, 88685b3daadSDavid Ahern RTM_NEWNETCONF, 88735103d11SAndy Gospodarek NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 88835103d11SAndy Gospodarek NETCONFA_IFINDEX_ALL, 88935103d11SAndy Gospodarek net->ipv6.devconf_all); 89035103d11SAndy Gospodarek } 89135103d11SAndy Gospodarek rtnl_unlock(); 89235103d11SAndy Gospodarek 89335103d11SAndy Gospodarek return 1; 89435103d11SAndy Gospodarek } 89535103d11SAndy Gospodarek 8961da177e4SLinus Torvalds #endif 8971da177e4SLinus Torvalds 8985c578aedSstephen hemminger /* Nobody refers to this ifaddr, destroy it */ 8991da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp) 9001da177e4SLinus Torvalds { 901c2e21293Sstephen hemminger WARN_ON(!hlist_unhashed(&ifp->addr_lst)); 902547b792cSIlpo Järvinen 9031da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG 90491df42beSJoe Perches pr_debug("%s\n", __func__); 9051da177e4SLinus Torvalds #endif 9061da177e4SLinus Torvalds 9071da177e4SLinus Torvalds in6_dev_put(ifp->idev); 9081da177e4SLinus Torvalds 909c15b1ccaSHannes Frederic Sowa if (cancel_delayed_work(&ifp->dad_work)) 910c15b1ccaSHannes Frederic Sowa pr_notice("delayed DAD work was pending while freeing ifa=%p\n", 911c15b1ccaSHannes Frederic Sowa ifp); 9121da177e4SLinus Torvalds 913e9d3e084SHerbert Xu if (ifp->state != INET6_IFADDR_STATE_DEAD) { 914f3213831SJoe Perches pr_warn("Freeing alive inet6 address %p\n", ifp); 9151da177e4SLinus Torvalds return; 9161da177e4SLinus Torvalds } 9171da177e4SLinus Torvalds 918e5785985SLai Jiangshan kfree_rcu(ifp, rcu); 9191da177e4SLinus Torvalds } 9201da177e4SLinus Torvalds 921e55ffac6SBrian Haley static void 922e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp) 923e55ffac6SBrian Haley { 924502a2ffdSstephen hemminger struct list_head *p; 9258a6ce0c0SYOSHIFUJI Hideaki int ifp_scope = ipv6_addr_src_scope(&ifp->addr); 926e55ffac6SBrian Haley 927e55ffac6SBrian Haley /* 928e55ffac6SBrian Haley * Each device address list is sorted in order of scope - 929e55ffac6SBrian Haley * global before linklocal. 930e55ffac6SBrian Haley */ 931502a2ffdSstephen hemminger list_for_each(p, &idev->addr_list) { 932502a2ffdSstephen hemminger struct inet6_ifaddr *ifa 933502a2ffdSstephen hemminger = list_entry(p, struct inet6_ifaddr, if_list); 9348a6ce0c0SYOSHIFUJI Hideaki if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr)) 935e55ffac6SBrian Haley break; 936e55ffac6SBrian Haley } 937e55ffac6SBrian Haley 9388ef802aaSEric Dumazet list_add_tail_rcu(&ifp->if_list, p); 939e55ffac6SBrian Haley } 940e55ffac6SBrian Haley 9413f27fb23SEric Dumazet static u32 inet6_addr_hash(const struct net *net, const struct in6_addr *addr) 9423eb84f49SYOSHIFUJI Hideaki { 9433f27fb23SEric Dumazet u32 val = ipv6_addr_hash(addr) ^ net_hash_mix(net); 9443f27fb23SEric Dumazet 9453f27fb23SEric Dumazet return hash_32(val, IN6_ADDR_HSIZE_SHIFT); 9463eb84f49SYOSHIFUJI Hideaki } 9473eb84f49SYOSHIFUJI Hideaki 94856fc709bSEric Dumazet static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr, 949752a9292SEric Dumazet struct net_device *dev, unsigned int hash) 95056fc709bSEric Dumazet { 95156fc709bSEric Dumazet struct inet6_ifaddr *ifp; 95256fc709bSEric Dumazet 95356fc709bSEric Dumazet hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) { 95456fc709bSEric Dumazet if (!net_eq(dev_net(ifp->idev->dev), net)) 95556fc709bSEric Dumazet continue; 95656fc709bSEric Dumazet if (ipv6_addr_equal(&ifp->addr, addr)) { 95756fc709bSEric Dumazet if (!dev || ifp->idev->dev == dev) 95856fc709bSEric Dumazet return true; 95956fc709bSEric Dumazet } 96056fc709bSEric Dumazet } 96156fc709bSEric Dumazet return false; 96256fc709bSEric Dumazet } 96356fc709bSEric Dumazet 964f3d9832eSDavid Ahern static int ipv6_add_addr_hash(struct net_device *dev, struct inet6_ifaddr *ifa) 965f3d9832eSDavid Ahern { 9663f27fb23SEric Dumazet unsigned int hash = inet6_addr_hash(dev_net(dev), &ifa->addr); 967f3d9832eSDavid Ahern int err = 0; 968f3d9832eSDavid Ahern 969f3d9832eSDavid Ahern spin_lock(&addrconf_hash_lock); 970f3d9832eSDavid Ahern 971f3d9832eSDavid Ahern /* Ignore adding duplicate addresses on an interface */ 972752a9292SEric Dumazet if (ipv6_chk_same_addr(dev_net(dev), &ifa->addr, dev, hash)) { 973e32ac250SJoe Perches netdev_dbg(dev, "ipv6_add_addr: already assigned\n"); 974f3d9832eSDavid Ahern err = -EEXIST; 975752a9292SEric Dumazet } else { 976752a9292SEric Dumazet hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]); 977f3d9832eSDavid Ahern } 978f3d9832eSDavid Ahern 979f3d9832eSDavid Ahern spin_unlock(&addrconf_hash_lock); 980f3d9832eSDavid Ahern 981f3d9832eSDavid Ahern return err; 982f3d9832eSDavid Ahern } 983f3d9832eSDavid Ahern 9841da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */ 9851da177e4SLinus Torvalds 9861da177e4SLinus Torvalds static struct inet6_ifaddr * 987e6464b8cSDavid Ahern ipv6_add_addr(struct inet6_dev *idev, struct ifa6_config *cfg, 988de95e047SDavid Ahern bool can_block, struct netlink_ext_ack *extack) 9891da177e4SLinus Torvalds { 990f3d9832eSDavid Ahern gfp_t gfp_flags = can_block ? GFP_KERNEL : GFP_ATOMIC; 991e6464b8cSDavid Ahern int addr_type = ipv6_addr_type(cfg->pfx); 992df789fe7SDavid Forster struct net *net = dev_net(idev->dev); 9931da177e4SLinus Torvalds struct inet6_ifaddr *ifa = NULL; 994360a9887SDavid Ahern struct fib6_info *f6i = NULL; 9951da177e4SLinus Torvalds int err = 0; 996d68b8270SYOSHIFUJI Hideaki 997d68b8270SYOSHIFUJI Hideaki if (addr_type == IPV6_ADDR_ANY || 998d68b8270SYOSHIFUJI Hideaki addr_type & IPV6_ADDR_MULTICAST || 999d68b8270SYOSHIFUJI Hideaki (!(idev->dev->flags & IFF_LOOPBACK) && 1000d68b8270SYOSHIFUJI Hideaki addr_type & IPV6_ADDR_LOOPBACK)) 1001d68b8270SYOSHIFUJI Hideaki return ERR_PTR(-EADDRNOTAVAIL); 10021da177e4SLinus Torvalds 10031da177e4SLinus Torvalds if (idev->dead) { 10041da177e4SLinus Torvalds err = -ENODEV; /*XXX*/ 1005f3d9832eSDavid Ahern goto out; 10061da177e4SLinus Torvalds } 10071da177e4SLinus Torvalds 100856d417b1SBrian Haley if (idev->cnf.disable_ipv6) { 10099bdd8d40SBrian Haley err = -EACCES; 1010f3d9832eSDavid Ahern goto out; 10119bdd8d40SBrian Haley } 10129bdd8d40SBrian Haley 1013ff7883eaSDavid Ahern /* validator notifier needs to be blocking; 1014ff7883eaSDavid Ahern * do not call in atomic context 1015ff7883eaSDavid Ahern */ 1016ff7883eaSDavid Ahern if (can_block) { 1017ff7883eaSDavid Ahern struct in6_validator_info i6vi = { 1018e6464b8cSDavid Ahern .i6vi_addr = *cfg->pfx, 1019ff7883eaSDavid Ahern .i6vi_dev = idev, 1020de95e047SDavid Ahern .extack = extack, 1021ff7883eaSDavid Ahern }; 1022ff7883eaSDavid Ahern 10233ad7d246SKrister Johansen err = inet6addr_validator_notifier_call_chain(NETDEV_UP, &i6vi); 10243ad7d246SKrister Johansen err = notifier_to_errno(err); 1025f3d9832eSDavid Ahern if (err < 0) 10261da177e4SLinus Torvalds goto out; 1027ff7883eaSDavid Ahern } 10281da177e4SLinus Torvalds 1029f3d9832eSDavid Ahern ifa = kzalloc(sizeof(*ifa), gfp_flags); 103063159f29SIan Morris if (!ifa) { 10311da177e4SLinus Torvalds err = -ENOBUFS; 10321da177e4SLinus Torvalds goto out; 10331da177e4SLinus Torvalds } 10341da177e4SLinus Torvalds 1035e6464b8cSDavid Ahern f6i = addrconf_f6i_alloc(net, idev, cfg->pfx, false, gfp_flags); 1036360a9887SDavid Ahern if (IS_ERR(f6i)) { 1037360a9887SDavid Ahern err = PTR_ERR(f6i); 1038360a9887SDavid Ahern f6i = NULL; 10391da177e4SLinus Torvalds goto out; 10401da177e4SLinus Torvalds } 10411da177e4SLinus Torvalds 1042df789fe7SDavid Forster if (net->ipv6.devconf_all->disable_policy || 1043df789fe7SDavid Forster idev->cnf.disable_policy) 1044360a9887SDavid Ahern f6i->dst_nopolicy = true; 1045df789fe7SDavid Forster 1046bba24896SJiri Pirko neigh_parms_data_state_setall(idev->nd_parms); 1047bba24896SJiri Pirko 1048e6464b8cSDavid Ahern ifa->addr = *cfg->pfx; 1049e6464b8cSDavid Ahern if (cfg->peer_pfx) 1050e6464b8cSDavid Ahern ifa->peer_addr = *cfg->peer_pfx; 10511da177e4SLinus Torvalds 10521da177e4SLinus Torvalds spin_lock_init(&ifa->lock); 1053c15b1ccaSHannes Frederic Sowa INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work); 1054c2e21293Sstephen hemminger INIT_HLIST_NODE(&ifa->addr_lst); 1055e6464b8cSDavid Ahern ifa->scope = cfg->scope; 1056e6464b8cSDavid Ahern ifa->prefix_len = cfg->plen; 10578308f3ffSDavid Ahern ifa->rt_priority = cfg->rt_priority; 1058e6464b8cSDavid Ahern ifa->flags = cfg->ifa_flags; 105966eb9f86SMahesh Bandewar /* No need to add the TENTATIVE flag for addresses with NODAD */ 1060e6464b8cSDavid Ahern if (!(cfg->ifa_flags & IFA_F_NODAD)) 106166eb9f86SMahesh Bandewar ifa->flags |= IFA_F_TENTATIVE; 1062e6464b8cSDavid Ahern ifa->valid_lft = cfg->valid_lft; 1063e6464b8cSDavid Ahern ifa->prefered_lft = cfg->preferred_lft; 10641da177e4SLinus Torvalds ifa->cstamp = ifa->tstamp = jiffies; 1065617fe29dSDaniel Borkmann ifa->tokenized = false; 10661da177e4SLinus Torvalds 1067360a9887SDavid Ahern ifa->rt = f6i; 106857f5f544SKeir Fraser 10691da177e4SLinus Torvalds ifa->idev = idev; 1070f3d9832eSDavid Ahern in6_dev_hold(idev); 1071f3d9832eSDavid Ahern 10721da177e4SLinus Torvalds /* For caller */ 1073271201c0SReshetova, Elena refcount_set(&ifa->refcnt, 1); 10741da177e4SLinus Torvalds 1075f3d9832eSDavid Ahern rcu_read_lock_bh(); 10761da177e4SLinus Torvalds 1077f3d9832eSDavid Ahern err = ipv6_add_addr_hash(idev->dev, ifa); 1078f3d9832eSDavid Ahern if (err < 0) { 1079f3d9832eSDavid Ahern rcu_read_unlock_bh(); 1080f3d9832eSDavid Ahern goto out; 1081f3d9832eSDavid Ahern } 10821da177e4SLinus Torvalds 10831da177e4SLinus Torvalds write_lock(&idev->lock); 1084f3d9832eSDavid Ahern 10851da177e4SLinus Torvalds /* Add to inet6_dev unicast addr list. */ 1086e55ffac6SBrian Haley ipv6_link_dev_addr(idev, ifa); 10871da177e4SLinus Torvalds 10881da177e4SLinus Torvalds if (ifa->flags&IFA_F_TEMPORARY) { 1089372e6c8fSstephen hemminger list_add(&ifa->tmp_list, &idev->tempaddr_list); 10901da177e4SLinus Torvalds in6_ifa_hold(ifa); 10911da177e4SLinus Torvalds } 10921da177e4SLinus Torvalds 10931da177e4SLinus Torvalds in6_ifa_hold(ifa); 10941da177e4SLinus Torvalds write_unlock(&idev->lock); 1095f3d9832eSDavid Ahern 10968814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 10971da177e4SLinus Torvalds 1098f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_UP, ifa); 1099f3d9832eSDavid Ahern out: 1100f3d9832eSDavid Ahern if (unlikely(err < 0)) { 1101360a9887SDavid Ahern fib6_info_release(f6i); 110293531c67SDavid Ahern 1103f3d9832eSDavid Ahern if (ifa) { 1104f3d9832eSDavid Ahern if (ifa->idev) 1105f3d9832eSDavid Ahern in6_dev_put(ifa->idev); 11061da177e4SLinus Torvalds kfree(ifa); 1107f3d9832eSDavid Ahern } 11081da177e4SLinus Torvalds ifa = ERR_PTR(err); 11091da177e4SLinus Torvalds } 11101da177e4SLinus Torvalds 11111da177e4SLinus Torvalds return ifa; 11121da177e4SLinus Torvalds } 11131da177e4SLinus Torvalds 11145b84efecSThomas Haller enum cleanup_prefix_rt_t { 11155b84efecSThomas Haller CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */ 11165b84efecSThomas Haller CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */ 11175b84efecSThomas Haller CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */ 11185b84efecSThomas Haller }; 11195b84efecSThomas Haller 11205b84efecSThomas Haller /* 11215b84efecSThomas Haller * Check, whether the prefix for ifp would still need a prefix route 11225b84efecSThomas Haller * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_* 11235b84efecSThomas Haller * constants. 11245b84efecSThomas Haller * 11255b84efecSThomas Haller * 1) we don't purge prefix if address was not permanent. 11265b84efecSThomas Haller * prefix is managed by its own lifetime. 11275b84efecSThomas Haller * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE. 11285b84efecSThomas Haller * 3) if there are no addresses, delete prefix. 11295b84efecSThomas Haller * 4) if there are still other permanent address(es), 11305b84efecSThomas Haller * corresponding prefix is still permanent. 11315b84efecSThomas Haller * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE, 11325b84efecSThomas Haller * don't purge the prefix, assume user space is managing it. 11335b84efecSThomas Haller * 6) otherwise, update prefix lifetime to the 11345b84efecSThomas Haller * longest valid lifetime among the corresponding 11355b84efecSThomas Haller * addresses on the device. 11365b84efecSThomas Haller * Note: subsequent RA will update lifetime. 11375b84efecSThomas Haller **/ 11385b84efecSThomas Haller static enum cleanup_prefix_rt_t 11395b84efecSThomas Haller check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires) 11405b84efecSThomas Haller { 11415b84efecSThomas Haller struct inet6_ifaddr *ifa; 11425b84efecSThomas Haller struct inet6_dev *idev = ifp->idev; 11435b84efecSThomas Haller unsigned long lifetime; 11445b84efecSThomas Haller enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL; 11455b84efecSThomas Haller 11465b84efecSThomas Haller *expires = jiffies; 11475b84efecSThomas Haller 11485b84efecSThomas Haller list_for_each_entry(ifa, &idev->addr_list, if_list) { 11495b84efecSThomas Haller if (ifa == ifp) 11505b84efecSThomas Haller continue; 11515b84efecSThomas Haller if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr, 11525b84efecSThomas Haller ifp->prefix_len)) 11535b84efecSThomas Haller continue; 11545b84efecSThomas Haller if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE)) 11555b84efecSThomas Haller return CLEANUP_PREFIX_RT_NOP; 11565b84efecSThomas Haller 11575b84efecSThomas Haller action = CLEANUP_PREFIX_RT_EXPIRE; 11585b84efecSThomas Haller 11595b84efecSThomas Haller spin_lock(&ifa->lock); 11605b84efecSThomas Haller 11615b84efecSThomas Haller lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ); 11625b84efecSThomas Haller /* 11635b84efecSThomas Haller * Note: Because this address is 11645b84efecSThomas Haller * not permanent, lifetime < 11655b84efecSThomas Haller * LONG_MAX / HZ here. 11665b84efecSThomas Haller */ 11675b84efecSThomas Haller if (time_before(*expires, ifa->tstamp + lifetime * HZ)) 11685b84efecSThomas Haller *expires = ifa->tstamp + lifetime * HZ; 11695b84efecSThomas Haller spin_unlock(&ifa->lock); 11705b84efecSThomas Haller } 11715b84efecSThomas Haller 11725b84efecSThomas Haller return action; 11735b84efecSThomas Haller } 11745b84efecSThomas Haller 11755b84efecSThomas Haller static void 11765b84efecSThomas Haller cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt) 11775b84efecSThomas Haller { 117893c2fb25SDavid Ahern struct fib6_info *f6i; 11795b84efecSThomas Haller 118093c2fb25SDavid Ahern f6i = addrconf_get_prefix_route(&ifp->addr, 11815b84efecSThomas Haller ifp->prefix_len, 11825b84efecSThomas Haller ifp->idev->dev, 11835b84efecSThomas Haller 0, RTF_GATEWAY | RTF_DEFAULT); 118493c2fb25SDavid Ahern if (f6i) { 11855b84efecSThomas Haller if (del_rt) 118693c2fb25SDavid Ahern ip6_del_rt(dev_net(ifp->idev->dev), f6i); 11875b84efecSThomas Haller else { 118893c2fb25SDavid Ahern if (!(f6i->fib6_flags & RTF_EXPIRES)) 118993c2fb25SDavid Ahern fib6_set_expires(f6i, expires); 119093c2fb25SDavid Ahern fib6_info_release(f6i); 11915b84efecSThomas Haller } 11925b84efecSThomas Haller } 11935b84efecSThomas Haller } 11945b84efecSThomas Haller 11955b84efecSThomas Haller 11961da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */ 11971da177e4SLinus Torvalds 11981da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp) 11991da177e4SLinus Torvalds { 12004c5ff6a6SHerbert Xu int state; 12015b84efecSThomas Haller enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP; 12025b84efecSThomas Haller unsigned long expires; 12031da177e4SLinus Torvalds 1204c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 1205c15b1ccaSHannes Frederic Sowa 12068e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 12074c5ff6a6SHerbert Xu state = ifp->state; 1208e9d3e084SHerbert Xu ifp->state = INET6_IFADDR_STATE_DEAD; 12098e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 12104c5ff6a6SHerbert Xu 12114c5ff6a6SHerbert Xu if (state == INET6_IFADDR_STATE_DEAD) 12124c5ff6a6SHerbert Xu goto out; 12131da177e4SLinus Torvalds 12145c578aedSstephen hemminger spin_lock_bh(&addrconf_hash_lock); 12155c578aedSstephen hemminger hlist_del_init_rcu(&ifp->addr_lst); 12165c578aedSstephen hemminger spin_unlock_bh(&addrconf_hash_lock); 12171da177e4SLinus Torvalds 12185b84efecSThomas Haller write_lock_bh(&ifp->idev->lock); 12195d9efa7eSDavid S. Miller 12201da177e4SLinus Torvalds if (ifp->flags&IFA_F_TEMPORARY) { 1221372e6c8fSstephen hemminger list_del(&ifp->tmp_list); 12221da177e4SLinus Torvalds if (ifp->ifpub) { 12231da177e4SLinus Torvalds in6_ifa_put(ifp->ifpub); 12241da177e4SLinus Torvalds ifp->ifpub = NULL; 12251da177e4SLinus Torvalds } 12261da177e4SLinus Torvalds __in6_ifa_put(ifp); 12271da177e4SLinus Torvalds } 12281da177e4SLinus Torvalds 12295b84efecSThomas Haller if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE)) 12305b84efecSThomas Haller action = check_cleanup_prefix_route(ifp, &expires); 12315b84efecSThomas Haller 12328ef802aaSEric Dumazet list_del_rcu(&ifp->if_list); 12331da177e4SLinus Torvalds __in6_ifa_put(ifp); 1234502a2ffdSstephen hemminger 12355b84efecSThomas Haller write_unlock_bh(&ifp->idev->lock); 12361da177e4SLinus Torvalds 1237c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifp); 1238b2238566SAndrey Vagin 12391da177e4SLinus Torvalds ipv6_ifa_notify(RTM_DELADDR, ifp); 12401da177e4SLinus Torvalds 1241f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_DOWN, ifp); 12421da177e4SLinus Torvalds 12435b84efecSThomas Haller if (action != CLEANUP_PREFIX_RT_NOP) { 12445b84efecSThomas Haller cleanup_prefix_route(ifp, expires, 12455b84efecSThomas Haller action == CLEANUP_PREFIX_RT_DEL); 12461da177e4SLinus Torvalds } 12471da177e4SLinus Torvalds 1248c3968a85SDaniel Walter /* clean up prefsrc entries */ 1249c3968a85SDaniel Walter rt6_remove_prefsrc(ifp); 12504c5ff6a6SHerbert Xu out: 12511da177e4SLinus Torvalds in6_ifa_put(ifp); 12521da177e4SLinus Torvalds } 12531da177e4SLinus Torvalds 1254fffcefe9SEric Dumazet static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, 1255fffcefe9SEric Dumazet struct inet6_ifaddr *ift, 1256fffcefe9SEric Dumazet bool block) 12571da177e4SLinus Torvalds { 12581da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 12591da177e4SLinus Torvalds struct in6_addr addr, *tmpaddr; 1260e6464b8cSDavid Ahern unsigned long tmp_tstamp, age; 1261eac55bf9SBenoit Boissinot unsigned long regen_advance; 1262e6464b8cSDavid Ahern struct ifa6_config cfg; 12631da177e4SLinus Torvalds int ret = 0; 126476f793e3SLorenzo Colitti unsigned long now = jiffies; 126576506a98SJiri Bohac long max_desync_factor; 12667aa8e63fSJiri Bohac s32 cnf_temp_preferred_lft; 12671da177e4SLinus Torvalds 126853bd6749SJiri Pirko write_lock_bh(&idev->lock); 12691da177e4SLinus Torvalds if (ift) { 12701da177e4SLinus Torvalds spin_lock_bh(&ift->lock); 12711da177e4SLinus Torvalds memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8); 12721da177e4SLinus Torvalds spin_unlock_bh(&ift->lock); 12731da177e4SLinus Torvalds tmpaddr = &addr; 12741da177e4SLinus Torvalds } else { 12751da177e4SLinus Torvalds tmpaddr = NULL; 12761da177e4SLinus Torvalds } 12771da177e4SLinus Torvalds retry: 12781da177e4SLinus Torvalds in6_dev_hold(idev); 12791da177e4SLinus Torvalds if (idev->cnf.use_tempaddr <= 0) { 128053bd6749SJiri Pirko write_unlock_bh(&idev->lock); 1281f3213831SJoe Perches pr_info("%s: use_tempaddr is disabled\n", __func__); 12821da177e4SLinus Torvalds in6_dev_put(idev); 12831da177e4SLinus Torvalds ret = -1; 12841da177e4SLinus Torvalds goto out; 12851da177e4SLinus Torvalds } 12861da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 12871da177e4SLinus Torvalds if (ifp->regen_count++ >= idev->cnf.regen_max_retry) { 12881da177e4SLinus Torvalds idev->cnf.use_tempaddr = -1; /*XXX*/ 12891da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 129053bd6749SJiri Pirko write_unlock_bh(&idev->lock); 1291f3213831SJoe Perches pr_warn("%s: regeneration time exceeded - disabled temporary address support\n", 1292f3213831SJoe Perches __func__); 12931da177e4SLinus Torvalds in6_dev_put(idev); 12941da177e4SLinus Torvalds ret = -1; 12951da177e4SLinus Torvalds goto out; 12961da177e4SLinus Torvalds } 12971da177e4SLinus Torvalds in6_ifa_hold(ifp); 12981da177e4SLinus Torvalds memcpy(addr.s6_addr, ifp->addr.s6_addr, 8); 12999d6280daSJiri Bohac ipv6_try_regen_rndid(idev, tmpaddr); 13001da177e4SLinus Torvalds memcpy(&addr.s6_addr[8], idev->rndid, 8); 130176f793e3SLorenzo Colitti age = (now - ifp->tstamp) / HZ; 13021da177e4SLinus Torvalds 1303eac55bf9SBenoit Boissinot regen_advance = idev->cnf.regen_max_retry * 1304eac55bf9SBenoit Boissinot idev->cnf.dad_transmits * 13051f9248e5SJiri Pirko NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ; 130676506a98SJiri Bohac 130776506a98SJiri Bohac /* recalculate max_desync_factor each time and update 130876506a98SJiri Bohac * idev->desync_factor if it's larger 130976506a98SJiri Bohac */ 13107aa8e63fSJiri Bohac cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft); 131176506a98SJiri Bohac max_desync_factor = min_t(__u32, 131276506a98SJiri Bohac idev->cnf.max_desync_factor, 13137aa8e63fSJiri Bohac cnf_temp_preferred_lft - regen_advance); 131476506a98SJiri Bohac 131576506a98SJiri Bohac if (unlikely(idev->desync_factor > max_desync_factor)) { 131676506a98SJiri Bohac if (max_desync_factor > 0) { 131776506a98SJiri Bohac get_random_bytes(&idev->desync_factor, 131876506a98SJiri Bohac sizeof(idev->desync_factor)); 131976506a98SJiri Bohac idev->desync_factor %= max_desync_factor; 132076506a98SJiri Bohac } else { 132176506a98SJiri Bohac idev->desync_factor = 0; 132276506a98SJiri Bohac } 132376506a98SJiri Bohac } 132476506a98SJiri Bohac 13253f2d67b6SDavid Ahern memset(&cfg, 0, sizeof(cfg)); 1326e6464b8cSDavid Ahern cfg.valid_lft = min_t(__u32, ifp->valid_lft, 132776506a98SJiri Bohac idev->cnf.temp_valid_lft + age); 1328e6464b8cSDavid Ahern cfg.preferred_lft = cnf_temp_preferred_lft + age - idev->desync_factor; 1329e6464b8cSDavid Ahern cfg.preferred_lft = min_t(__u32, ifp->prefered_lft, cfg.preferred_lft); 1330e6464b8cSDavid Ahern 1331e6464b8cSDavid Ahern cfg.plen = ifp->prefix_len; 133276506a98SJiri Bohac tmp_tstamp = ifp->tstamp; 133376506a98SJiri Bohac spin_unlock_bh(&ifp->lock); 133476506a98SJiri Bohac 133553bd6749SJiri Pirko write_unlock_bh(&idev->lock); 133695c385b4SNeil Horman 1337eac55bf9SBenoit Boissinot /* A temporary address is created only if this calculated Preferred 1338eac55bf9SBenoit Boissinot * Lifetime is greater than REGEN_ADVANCE time units. In particular, 1339eac55bf9SBenoit Boissinot * an implementation must not create a temporary address with a zero 1340eac55bf9SBenoit Boissinot * Preferred Lifetime. 1341ecab6701SHeiner Kallweit * Use age calculation as in addrconf_verify to avoid unnecessary 1342ecab6701SHeiner Kallweit * temporary addresses being generated. 1343eac55bf9SBenoit Boissinot */ 1344ecab6701SHeiner Kallweit age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; 1345e6464b8cSDavid Ahern if (cfg.preferred_lft <= regen_advance + age) { 1346eac55bf9SBenoit Boissinot in6_ifa_put(ifp); 1347eac55bf9SBenoit Boissinot in6_dev_put(idev); 1348eac55bf9SBenoit Boissinot ret = -1; 1349eac55bf9SBenoit Boissinot goto out; 1350eac55bf9SBenoit Boissinot } 1351eac55bf9SBenoit Boissinot 1352e6464b8cSDavid Ahern cfg.ifa_flags = IFA_F_TEMPORARY; 135395c385b4SNeil Horman /* set in addrconf_prefix_rcv() */ 135495c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 1355e6464b8cSDavid Ahern cfg.ifa_flags |= IFA_F_OPTIMISTIC; 135695c385b4SNeil Horman 1357e6464b8cSDavid Ahern cfg.pfx = &addr; 1358e6464b8cSDavid Ahern cfg.scope = ipv6_addr_scope(cfg.pfx); 1359e6464b8cSDavid Ahern 1360e6464b8cSDavid Ahern ift = ipv6_add_addr(idev, &cfg, block, NULL); 13614b08a8f1SHannes Frederic Sowa if (IS_ERR(ift)) { 13621da177e4SLinus Torvalds in6_ifa_put(ifp); 13631da177e4SLinus Torvalds in6_dev_put(idev); 1364f3213831SJoe Perches pr_info("%s: retry temporary address regeneration\n", __func__); 13651da177e4SLinus Torvalds tmpaddr = &addr; 136653bd6749SJiri Pirko write_lock_bh(&idev->lock); 13671da177e4SLinus Torvalds goto retry; 13681da177e4SLinus Torvalds } 13691da177e4SLinus Torvalds 13701da177e4SLinus Torvalds spin_lock_bh(&ift->lock); 13711da177e4SLinus Torvalds ift->ifpub = ifp; 137276f793e3SLorenzo Colitti ift->cstamp = now; 13731da177e4SLinus Torvalds ift->tstamp = tmp_tstamp; 13741da177e4SLinus Torvalds spin_unlock_bh(&ift->lock); 13751da177e4SLinus Torvalds 1376cf22f9a2SDavid S. Miller addrconf_dad_start(ift); 13771da177e4SLinus Torvalds in6_ifa_put(ift); 13781da177e4SLinus Torvalds in6_dev_put(idev); 13791da177e4SLinus Torvalds out: 13801da177e4SLinus Torvalds return ret; 13811da177e4SLinus Torvalds } 13821da177e4SLinus Torvalds 13831da177e4SLinus Torvalds /* 1384072047e4SYOSHIFUJI Hideaki * Choose an appropriate source address (RFC3484) 13851da177e4SLinus Torvalds */ 1386a9b05723SYOSHIFUJI Hideaki enum { 1387a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_INIT = 0, 1388a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_LOCAL, 1389a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_SCOPE, 1390a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PREFERRED, 1391a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6 1392a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_HOA, 1393a9b05723SYOSHIFUJI Hideaki #endif 1394a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_OIF, 1395a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_LABEL, 1396a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PRIVACY, 1397a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_ORCHID, 1398a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PREFIX, 13997fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 14007fd2561eSErik Kline IPV6_SADDR_RULE_NOT_OPTIMISTIC, 14017fd2561eSErik Kline #endif 1402a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_MAX 1403072047e4SYOSHIFUJI Hideaki }; 1404072047e4SYOSHIFUJI Hideaki 1405a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score { 1406a9b05723SYOSHIFUJI Hideaki int rule; 1407a9b05723SYOSHIFUJI Hideaki int addr_type; 1408a9b05723SYOSHIFUJI Hideaki struct inet6_ifaddr *ifa; 1409a9b05723SYOSHIFUJI Hideaki DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX); 1410a9b05723SYOSHIFUJI Hideaki int scopedist; 1411a9b05723SYOSHIFUJI Hideaki int matchlen; 1412a9b05723SYOSHIFUJI Hideaki }; 1413a9b05723SYOSHIFUJI Hideaki 1414a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst { 14159acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *addr; 1416a9b05723SYOSHIFUJI Hideaki int ifindex; 1417a9b05723SYOSHIFUJI Hideaki int scope; 1418a9b05723SYOSHIFUJI Hideaki int label; 14197cbca67cSYOSHIFUJI Hideaki unsigned int prefs; 1420a9b05723SYOSHIFUJI Hideaki }; 1421072047e4SYOSHIFUJI Hideaki 1422b6f99a21SDave Jones static inline int ipv6_saddr_preferred(int type) 14231da177e4SLinus Torvalds { 142445bb0060SUlrich Weber if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK)) 1425072047e4SYOSHIFUJI Hideaki return 1; 1426072047e4SYOSHIFUJI Hideaki return 0; 14271da177e4SLinus Torvalds } 14281da177e4SLinus Torvalds 142935e015e1SMatteo Croce static bool ipv6_use_optimistic_addr(struct net *net, 143035e015e1SMatteo Croce struct inet6_dev *idev) 14317fd2561eSErik Kline { 14327fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 143335e015e1SMatteo Croce if (!idev) 143435e015e1SMatteo Croce return false; 143535e015e1SMatteo Croce if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad) 143635e015e1SMatteo Croce return false; 143735e015e1SMatteo Croce if (!net->ipv6.devconf_all->use_optimistic && !idev->cnf.use_optimistic) 143835e015e1SMatteo Croce return false; 143935e015e1SMatteo Croce 144035e015e1SMatteo Croce return true; 14417fd2561eSErik Kline #else 14427fd2561eSErik Kline return false; 14437fd2561eSErik Kline #endif 14447fd2561eSErik Kline } 14457fd2561eSErik Kline 1446f1c02cfbSSabrina Dubroca static bool ipv6_allow_optimistic_dad(struct net *net, 1447f1c02cfbSSabrina Dubroca struct inet6_dev *idev) 1448f1c02cfbSSabrina Dubroca { 1449f1c02cfbSSabrina Dubroca #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 1450f1c02cfbSSabrina Dubroca if (!idev) 1451f1c02cfbSSabrina Dubroca return false; 1452f1c02cfbSSabrina Dubroca if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad) 1453f1c02cfbSSabrina Dubroca return false; 1454f1c02cfbSSabrina Dubroca 1455f1c02cfbSSabrina Dubroca return true; 1456f1c02cfbSSabrina Dubroca #else 1457f1c02cfbSSabrina Dubroca return false; 1458f1c02cfbSSabrina Dubroca #endif 1459f1c02cfbSSabrina Dubroca } 1460f1c02cfbSSabrina Dubroca 14613de23255SBenjamin Thery static int ipv6_get_saddr_eval(struct net *net, 14623de23255SBenjamin Thery struct ipv6_saddr_score *score, 1463a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst *dst, 1464a9b05723SYOSHIFUJI Hideaki int i) 1465a9b05723SYOSHIFUJI Hideaki { 1466a9b05723SYOSHIFUJI Hideaki int ret; 1467a9b05723SYOSHIFUJI Hideaki 1468a9b05723SYOSHIFUJI Hideaki if (i <= score->rule) { 1469a9b05723SYOSHIFUJI Hideaki switch (i) { 1470a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_SCOPE: 1471a9b05723SYOSHIFUJI Hideaki ret = score->scopedist; 1472a9b05723SYOSHIFUJI Hideaki break; 1473a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFIX: 1474a9b05723SYOSHIFUJI Hideaki ret = score->matchlen; 1475a9b05723SYOSHIFUJI Hideaki break; 1476a9b05723SYOSHIFUJI Hideaki default: 1477a9b05723SYOSHIFUJI Hideaki ret = !!test_bit(i, score->scorebits); 1478a9b05723SYOSHIFUJI Hideaki } 1479a9b05723SYOSHIFUJI Hideaki goto out; 1480a9b05723SYOSHIFUJI Hideaki } 1481a9b05723SYOSHIFUJI Hideaki 1482a9b05723SYOSHIFUJI Hideaki switch (i) { 1483a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_INIT: 1484a9b05723SYOSHIFUJI Hideaki /* Rule 0: remember if hiscore is not ready yet */ 1485a9b05723SYOSHIFUJI Hideaki ret = !!score->ifa; 1486a9b05723SYOSHIFUJI Hideaki break; 1487a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_LOCAL: 1488a9b05723SYOSHIFUJI Hideaki /* Rule 1: Prefer same address */ 1489a9b05723SYOSHIFUJI Hideaki ret = ipv6_addr_equal(&score->ifa->addr, dst->addr); 1490a9b05723SYOSHIFUJI Hideaki break; 1491a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_SCOPE: 1492a9b05723SYOSHIFUJI Hideaki /* Rule 2: Prefer appropriate scope 1493a9b05723SYOSHIFUJI Hideaki * 1494a9b05723SYOSHIFUJI Hideaki * ret 1495a9b05723SYOSHIFUJI Hideaki * ^ 1496a9b05723SYOSHIFUJI Hideaki * -1 | d 15 1497a9b05723SYOSHIFUJI Hideaki * ---+--+-+---> scope 1498a9b05723SYOSHIFUJI Hideaki * | 1499a9b05723SYOSHIFUJI Hideaki * | d is scope of the destination. 1500a9b05723SYOSHIFUJI Hideaki * B-d | \ 1501a9b05723SYOSHIFUJI Hideaki * | \ <- smaller scope is better if 1502db9c7c39Sstephen hemminger * B-15 | \ if scope is enough for destination. 1503a9b05723SYOSHIFUJI Hideaki * | ret = B - scope (-1 <= scope >= d <= 15). 1504a9b05723SYOSHIFUJI Hideaki * d-C-1 | / 1505a9b05723SYOSHIFUJI Hideaki * |/ <- greater is better 1506a9b05723SYOSHIFUJI Hideaki * -C / if scope is not enough for destination. 1507a9b05723SYOSHIFUJI Hideaki * /| ret = scope - C (-1 <= d < scope <= 15). 1508a9b05723SYOSHIFUJI Hideaki * 1509a9b05723SYOSHIFUJI Hideaki * d - C - 1 < B -15 (for all -1 <= d <= 15). 1510a9b05723SYOSHIFUJI Hideaki * C > d + 14 - B >= 15 + 14 - B = 29 - B. 1511a9b05723SYOSHIFUJI Hideaki * Assume B = 0 and we get C > 29. 1512a9b05723SYOSHIFUJI Hideaki */ 1513a9b05723SYOSHIFUJI Hideaki ret = __ipv6_addr_src_scope(score->addr_type); 1514a9b05723SYOSHIFUJI Hideaki if (ret >= dst->scope) 1515a9b05723SYOSHIFUJI Hideaki ret = -ret; 1516a9b05723SYOSHIFUJI Hideaki else 1517a9b05723SYOSHIFUJI Hideaki ret -= 128; /* 30 is enough */ 1518a9b05723SYOSHIFUJI Hideaki score->scopedist = ret; 1519a9b05723SYOSHIFUJI Hideaki break; 1520a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFERRED: 15217fd2561eSErik Kline { 1522a9b05723SYOSHIFUJI Hideaki /* Rule 3: Avoid deprecated and optimistic addresses */ 15237fd2561eSErik Kline u8 avoid = IFA_F_DEPRECATED; 15247fd2561eSErik Kline 152535e015e1SMatteo Croce if (!ipv6_use_optimistic_addr(net, score->ifa->idev)) 15267fd2561eSErik Kline avoid |= IFA_F_OPTIMISTIC; 1527a9b05723SYOSHIFUJI Hideaki ret = ipv6_saddr_preferred(score->addr_type) || 15287fd2561eSErik Kline !(score->ifa->flags & avoid); 1529a9b05723SYOSHIFUJI Hideaki break; 15307fd2561eSErik Kline } 1531a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6 1532a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_HOA: 15337cbca67cSYOSHIFUJI Hideaki { 1534a9b05723SYOSHIFUJI Hideaki /* Rule 4: Prefer home address */ 15357cbca67cSYOSHIFUJI Hideaki int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA); 15367cbca67cSYOSHIFUJI Hideaki ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome; 1537a9b05723SYOSHIFUJI Hideaki break; 15387cbca67cSYOSHIFUJI Hideaki } 1539a9b05723SYOSHIFUJI Hideaki #endif 1540a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_OIF: 1541a9b05723SYOSHIFUJI Hideaki /* Rule 5: Prefer outgoing interface */ 1542a9b05723SYOSHIFUJI Hideaki ret = (!dst->ifindex || 1543a9b05723SYOSHIFUJI Hideaki dst->ifindex == score->ifa->idev->dev->ifindex); 1544a9b05723SYOSHIFUJI Hideaki break; 1545a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_LABEL: 1546a9b05723SYOSHIFUJI Hideaki /* Rule 6: Prefer matching label */ 15473de23255SBenjamin Thery ret = ipv6_addr_label(net, 15483de23255SBenjamin Thery &score->ifa->addr, score->addr_type, 1549a9b05723SYOSHIFUJI Hideaki score->ifa->idev->dev->ifindex) == dst->label; 1550a9b05723SYOSHIFUJI Hideaki break; 1551a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PRIVACY: 15527cbca67cSYOSHIFUJI Hideaki { 1553a9b05723SYOSHIFUJI Hideaki /* Rule 7: Prefer public address 155425985edcSLucas De Marchi * Note: prefer temporary address if use_tempaddr >= 2 1555a9b05723SYOSHIFUJI Hideaki */ 15567cbca67cSYOSHIFUJI Hideaki int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ? 15577cbca67cSYOSHIFUJI Hideaki !!(dst->prefs & IPV6_PREFER_SRC_TMP) : 15587cbca67cSYOSHIFUJI Hideaki score->ifa->idev->cnf.use_tempaddr >= 2; 15597cbca67cSYOSHIFUJI Hideaki ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp; 1560a9b05723SYOSHIFUJI Hideaki break; 15617cbca67cSYOSHIFUJI Hideaki } 1562a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_ORCHID: 1563a9b05723SYOSHIFUJI Hideaki /* Rule 8-: Prefer ORCHID vs ORCHID or 1564a9b05723SYOSHIFUJI Hideaki * non-ORCHID vs non-ORCHID 1565a9b05723SYOSHIFUJI Hideaki */ 1566a9b05723SYOSHIFUJI Hideaki ret = !(ipv6_addr_orchid(&score->ifa->addr) ^ 1567a9b05723SYOSHIFUJI Hideaki ipv6_addr_orchid(dst->addr)); 1568a9b05723SYOSHIFUJI Hideaki break; 1569a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFIX: 1570a9b05723SYOSHIFUJI Hideaki /* Rule 8: Use longest matching prefix */ 157191b4b04fSYOSHIFUJI Hideaki / 吉藤英明 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr); 157291b4b04fSYOSHIFUJI Hideaki / 吉藤英明 if (ret > score->ifa->prefix_len) 157391b4b04fSYOSHIFUJI Hideaki / 吉藤英明 ret = score->ifa->prefix_len; 157491b4b04fSYOSHIFUJI Hideaki / 吉藤英明 score->matchlen = ret; 1575a9b05723SYOSHIFUJI Hideaki break; 15767fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 15777fd2561eSErik Kline case IPV6_SADDR_RULE_NOT_OPTIMISTIC: 15787fd2561eSErik Kline /* Optimistic addresses still have lower precedence than other 15797fd2561eSErik Kline * preferred addresses. 15807fd2561eSErik Kline */ 15817fd2561eSErik Kline ret = !(score->ifa->flags & IFA_F_OPTIMISTIC); 15827fd2561eSErik Kline break; 15837fd2561eSErik Kline #endif 1584a9b05723SYOSHIFUJI Hideaki default: 1585a9b05723SYOSHIFUJI Hideaki ret = 0; 1586a9b05723SYOSHIFUJI Hideaki } 1587a9b05723SYOSHIFUJI Hideaki 1588a9b05723SYOSHIFUJI Hideaki if (ret) 1589a9b05723SYOSHIFUJI Hideaki __set_bit(i, score->scorebits); 1590a9b05723SYOSHIFUJI Hideaki score->rule = i; 1591a9b05723SYOSHIFUJI Hideaki out: 1592a9b05723SYOSHIFUJI Hideaki return ret; 1593a9b05723SYOSHIFUJI Hideaki } 1594a9b05723SYOSHIFUJI Hideaki 1595c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 static int __ipv6_dev_get_saddr(struct net *net, 15969131f3deSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_dst *dst, 15979131f3deSYOSHIFUJI Hideaki/吉藤英明 struct inet6_dev *idev, 1598c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_score *scores, 1599c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 int hiscore_idx) 16001da177e4SLinus Torvalds { 1601c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx]; 1602072047e4SYOSHIFUJI Hideaki 1603f59c031eSEric Dumazet list_for_each_entry_rcu(score->ifa, &idev->addr_list, if_list) { 1604a9b05723SYOSHIFUJI Hideaki int i; 1605072047e4SYOSHIFUJI Hideaki 1606a9b05723SYOSHIFUJI Hideaki /* 16076b3ae80aSYOSHIFUJI Hideaki * - Tentative Address (RFC2462 section 5.4) 16086b3ae80aSYOSHIFUJI Hideaki * - A tentative address is not considered 16096b3ae80aSYOSHIFUJI Hideaki * "assigned to an interface" in the traditional 161095c385b4SNeil Horman * sense, unless it is also flagged as optimistic. 16116b3ae80aSYOSHIFUJI Hideaki * - Candidate Source Address (section 4) 1612072047e4SYOSHIFUJI Hideaki * - In any case, anycast addresses, multicast 1613072047e4SYOSHIFUJI Hideaki * addresses, and the unspecified address MUST 1614072047e4SYOSHIFUJI Hideaki * NOT be included in a candidate set. 1615072047e4SYOSHIFUJI Hideaki */ 1616a9b05723SYOSHIFUJI Hideaki if ((score->ifa->flags & IFA_F_TENTATIVE) && 1617a9b05723SYOSHIFUJI Hideaki (!(score->ifa->flags & IFA_F_OPTIMISTIC))) 16186b3ae80aSYOSHIFUJI Hideaki continue; 1619a9b05723SYOSHIFUJI Hideaki 1620a9b05723SYOSHIFUJI Hideaki score->addr_type = __ipv6_addr_type(&score->ifa->addr); 1621a9b05723SYOSHIFUJI Hideaki 1622a9b05723SYOSHIFUJI Hideaki if (unlikely(score->addr_type == IPV6_ADDR_ANY || 1623a9b05723SYOSHIFUJI Hideaki score->addr_type & IPV6_ADDR_MULTICAST)) { 1624ba7a46f1SJoe Perches net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s", 16259131f3deSYOSHIFUJI Hideaki/吉藤英明 idev->dev->name); 16261da177e4SLinus Torvalds continue; 1627072047e4SYOSHIFUJI Hideaki } 1628072047e4SYOSHIFUJI Hideaki 1629a9b05723SYOSHIFUJI Hideaki score->rule = -1; 1630a9b05723SYOSHIFUJI Hideaki bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX); 1631072047e4SYOSHIFUJI Hideaki 1632a9b05723SYOSHIFUJI Hideaki for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) { 1633a9b05723SYOSHIFUJI Hideaki int minihiscore, miniscore; 1634072047e4SYOSHIFUJI Hideaki 16359131f3deSYOSHIFUJI Hideaki/吉藤英明 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i); 16369131f3deSYOSHIFUJI Hideaki/吉藤英明 miniscore = ipv6_get_saddr_eval(net, score, dst, i); 1637072047e4SYOSHIFUJI Hideaki 1638a9b05723SYOSHIFUJI Hideaki if (minihiscore > miniscore) { 1639a9b05723SYOSHIFUJI Hideaki if (i == IPV6_SADDR_RULE_SCOPE && 1640a9b05723SYOSHIFUJI Hideaki score->scopedist > 0) { 1641a9b05723SYOSHIFUJI Hideaki /* 1642a9b05723SYOSHIFUJI Hideaki * special case: 1643a9b05723SYOSHIFUJI Hideaki * each remaining entry 1644a9b05723SYOSHIFUJI Hideaki * has too small (not enough) 1645a9b05723SYOSHIFUJI Hideaki * scope, because ifa entries 1646a9b05723SYOSHIFUJI Hideaki * are sorted by their scope 1647a9b05723SYOSHIFUJI Hideaki * values. 1648072047e4SYOSHIFUJI Hideaki */ 16499131f3deSYOSHIFUJI Hideaki/吉藤英明 goto out; 1650072047e4SYOSHIFUJI Hideaki } 1651a9b05723SYOSHIFUJI Hideaki break; 1652a9b05723SYOSHIFUJI Hideaki } else if (minihiscore < miniscore) { 1653a0bffffcSIlpo Järvinen swap(hiscore, score); 1654c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 hiscore_idx = 1 - hiscore_idx; 1655a9b05723SYOSHIFUJI Hideaki 1656a9b05723SYOSHIFUJI Hideaki /* restore our iterator */ 1657a9b05723SYOSHIFUJI Hideaki score->ifa = hiscore->ifa; 1658a9b05723SYOSHIFUJI Hideaki 1659a9b05723SYOSHIFUJI Hideaki break; 16601da177e4SLinus Torvalds } 1661a9b05723SYOSHIFUJI Hideaki } 1662a9b05723SYOSHIFUJI Hideaki } 16639131f3deSYOSHIFUJI Hideaki/吉藤英明 out: 1664c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 return hiscore_idx; 16651da177e4SLinus Torvalds } 16669131f3deSYOSHIFUJI Hideaki/吉藤英明 1667afbac601SDavid Ahern static int ipv6_get_saddr_master(struct net *net, 1668afbac601SDavid Ahern const struct net_device *dst_dev, 1669afbac601SDavid Ahern const struct net_device *master, 1670afbac601SDavid Ahern struct ipv6_saddr_dst *dst, 1671afbac601SDavid Ahern struct ipv6_saddr_score *scores, 1672afbac601SDavid Ahern int hiscore_idx) 1673afbac601SDavid Ahern { 1674afbac601SDavid Ahern struct inet6_dev *idev; 1675afbac601SDavid Ahern 1676afbac601SDavid Ahern idev = __in6_dev_get(dst_dev); 1677afbac601SDavid Ahern if (idev) 1678afbac601SDavid Ahern hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev, 1679afbac601SDavid Ahern scores, hiscore_idx); 1680afbac601SDavid Ahern 1681afbac601SDavid Ahern idev = __in6_dev_get(master); 1682afbac601SDavid Ahern if (idev) 1683afbac601SDavid Ahern hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev, 1684afbac601SDavid Ahern scores, hiscore_idx); 1685afbac601SDavid Ahern 1686afbac601SDavid Ahern return hiscore_idx; 1687afbac601SDavid Ahern } 1688afbac601SDavid Ahern 16899131f3deSYOSHIFUJI Hideaki/吉藤英明 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev, 16909131f3deSYOSHIFUJI Hideaki/吉藤英明 const struct in6_addr *daddr, unsigned int prefs, 16919131f3deSYOSHIFUJI Hideaki/吉藤英明 struct in6_addr *saddr) 16929131f3deSYOSHIFUJI Hideaki/吉藤英明 { 1693c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_score scores[2], *hiscore; 16949131f3deSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_dst dst; 16959131f3deSYOSHIFUJI Hideaki/吉藤英明 struct inet6_dev *idev; 16969131f3deSYOSHIFUJI Hideaki/吉藤英明 struct net_device *dev; 16979131f3deSYOSHIFUJI Hideaki/吉藤英明 int dst_type; 16989131f3deSYOSHIFUJI Hideaki/吉藤英明 bool use_oif_addr = false; 1699c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 int hiscore_idx = 0; 1700cc429c8fSEric Dumazet int ret = 0; 17019131f3deSYOSHIFUJI Hideaki/吉藤英明 17029131f3deSYOSHIFUJI Hideaki/吉藤英明 dst_type = __ipv6_addr_type(daddr); 17039131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.addr = daddr; 17049131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.ifindex = dst_dev ? dst_dev->ifindex : 0; 17059131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.scope = __ipv6_addr_src_scope(dst_type); 17069131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex); 17079131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.prefs = prefs; 17089131f3deSYOSHIFUJI Hideaki/吉藤英明 1709c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 scores[hiscore_idx].rule = -1; 1710c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 scores[hiscore_idx].ifa = NULL; 17119131f3deSYOSHIFUJI Hideaki/吉藤英明 17129131f3deSYOSHIFUJI Hideaki/吉藤英明 rcu_read_lock(); 17139131f3deSYOSHIFUJI Hideaki/吉藤英明 17149131f3deSYOSHIFUJI Hideaki/吉藤英明 /* Candidate Source Address (section 4) 17159131f3deSYOSHIFUJI Hideaki/吉藤英明 * - multicast and link-local destination address, 17169131f3deSYOSHIFUJI Hideaki/吉藤英明 * the set of candidate source address MUST only 17179131f3deSYOSHIFUJI Hideaki/吉藤英明 * include addresses assigned to interfaces 17189131f3deSYOSHIFUJI Hideaki/吉藤英明 * belonging to the same link as the outgoing 17199131f3deSYOSHIFUJI Hideaki/吉藤英明 * interface. 17209131f3deSYOSHIFUJI Hideaki/吉藤英明 * (- For site-local destination addresses, the 17219131f3deSYOSHIFUJI Hideaki/吉藤英明 * set of candidate source addresses MUST only 17229131f3deSYOSHIFUJI Hideaki/吉藤英明 * include addresses assigned to interfaces 17239131f3deSYOSHIFUJI Hideaki/吉藤英明 * belonging to the same site as the outgoing 17249131f3deSYOSHIFUJI Hideaki/吉藤英明 * interface.) 17253985e8a3SErik Kline * - "It is RECOMMENDED that the candidate source addresses 17263985e8a3SErik Kline * be the set of unicast addresses assigned to the 17273985e8a3SErik Kline * interface that will be used to send to the destination 17283985e8a3SErik Kline * (the 'outgoing' interface)." (RFC 6724) 17299131f3deSYOSHIFUJI Hideaki/吉藤英明 */ 17309131f3deSYOSHIFUJI Hideaki/吉藤英明 if (dst_dev) { 17319131f3deSYOSHIFUJI Hideaki/吉藤英明 idev = __in6_dev_get(dst_dev); 17323985e8a3SErik Kline if ((dst_type & IPV6_ADDR_MULTICAST) || 17333985e8a3SErik Kline dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL || 17343985e8a3SErik Kline (idev && idev->cnf.use_oif_addrs_only)) { 17359131f3deSYOSHIFUJI Hideaki/吉藤英明 use_oif_addr = true; 17369131f3deSYOSHIFUJI Hideaki/吉藤英明 } 17379131f3deSYOSHIFUJI Hideaki/吉藤英明 } 17389131f3deSYOSHIFUJI Hideaki/吉藤英明 17399131f3deSYOSHIFUJI Hideaki/吉藤英明 if (use_oif_addr) { 1740c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 if (idev) 1741c15df306SYOSHIFUJI Hideaki hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx); 17429131f3deSYOSHIFUJI Hideaki/吉藤英明 } else { 1743afbac601SDavid Ahern const struct net_device *master; 1744afbac601SDavid Ahern int master_idx = 0; 1745afbac601SDavid Ahern 1746afbac601SDavid Ahern /* if dst_dev exists and is enslaved to an L3 device, then 1747afbac601SDavid Ahern * prefer addresses from dst_dev and then the master over 1748afbac601SDavid Ahern * any other enslaved devices in the L3 domain. 1749afbac601SDavid Ahern */ 1750afbac601SDavid Ahern master = l3mdev_master_dev_rcu(dst_dev); 1751afbac601SDavid Ahern if (master) { 1752afbac601SDavid Ahern master_idx = master->ifindex; 1753afbac601SDavid Ahern 1754afbac601SDavid Ahern hiscore_idx = ipv6_get_saddr_master(net, dst_dev, 1755afbac601SDavid Ahern master, &dst, 1756afbac601SDavid Ahern scores, hiscore_idx); 1757afbac601SDavid Ahern 1758afbac601SDavid Ahern if (scores[hiscore_idx].ifa) 1759afbac601SDavid Ahern goto out; 1760afbac601SDavid Ahern } 1761afbac601SDavid Ahern 17629131f3deSYOSHIFUJI Hideaki/吉藤英明 for_each_netdev_rcu(net, dev) { 1763afbac601SDavid Ahern /* only consider addresses on devices in the 1764afbac601SDavid Ahern * same L3 domain 1765afbac601SDavid Ahern */ 1766afbac601SDavid Ahern if (l3mdev_master_ifindex_rcu(dev) != master_idx) 1767afbac601SDavid Ahern continue; 17689131f3deSYOSHIFUJI Hideaki/吉藤英明 idev = __in6_dev_get(dev); 17699131f3deSYOSHIFUJI Hideaki/吉藤英明 if (!idev) 17709131f3deSYOSHIFUJI Hideaki/吉藤英明 continue; 1771c15df306SYOSHIFUJI Hideaki hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx); 17729131f3deSYOSHIFUJI Hideaki/吉藤英明 } 17739131f3deSYOSHIFUJI Hideaki/吉藤英明 } 1774afbac601SDavid Ahern 1775afbac601SDavid Ahern out: 1776c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 hiscore = &scores[hiscore_idx]; 1777a9b05723SYOSHIFUJI Hideaki if (!hiscore->ifa) 1778cc429c8fSEric Dumazet ret = -EADDRNOTAVAIL; 1779cc429c8fSEric Dumazet else 17804e3fd7a0SAlexey Dobriyan *saddr = hiscore->ifa->addr; 1781cc429c8fSEric Dumazet 1782cc429c8fSEric Dumazet rcu_read_unlock(); 1783cc429c8fSEric Dumazet return ret; 17841da177e4SLinus Torvalds } 17855e5f3f0fSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_dev_get_saddr); 17861da177e4SLinus Torvalds 17878965779dSAmerigo Wang int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr, 1788479840ffSJiri Pirko u32 banned_flags) 1789b7b1bfceSHannes Frederic Sowa { 1790b7b1bfceSHannes Frederic Sowa struct inet6_ifaddr *ifp; 1791b7b1bfceSHannes Frederic Sowa int err = -EADDRNOTAVAIL; 1792b7b1bfceSHannes Frederic Sowa 1793602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) { 1794602582caSHannes Frederic Sowa if (ifp->scope > IFA_LINK) 1795602582caSHannes Frederic Sowa break; 1796b7b1bfceSHannes Frederic Sowa if (ifp->scope == IFA_LINK && 1797b7b1bfceSHannes Frederic Sowa !(ifp->flags & banned_flags)) { 1798b7b1bfceSHannes Frederic Sowa *addr = ifp->addr; 1799b7b1bfceSHannes Frederic Sowa err = 0; 1800b7b1bfceSHannes Frederic Sowa break; 1801b7b1bfceSHannes Frederic Sowa } 1802b7b1bfceSHannes Frederic Sowa } 1803b7b1bfceSHannes Frederic Sowa return err; 1804b7b1bfceSHannes Frederic Sowa } 1805b7b1bfceSHannes Frederic Sowa 180695c385b4SNeil Horman int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr, 1807479840ffSJiri Pirko u32 banned_flags) 18081da177e4SLinus Torvalds { 18091da177e4SLinus Torvalds struct inet6_dev *idev; 18101da177e4SLinus Torvalds int err = -EADDRNOTAVAIL; 18111da177e4SLinus Torvalds 18128814c4b5SYOSHIFUJI Hideaki rcu_read_lock(); 1813e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 1814e21e8467SStephen Hemminger if (idev) { 18151da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1816b7b1bfceSHannes Frederic Sowa err = __ipv6_get_lladdr(idev, addr, banned_flags); 18171da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 18181da177e4SLinus Torvalds } 18198814c4b5SYOSHIFUJI Hideaki rcu_read_unlock(); 18201da177e4SLinus Torvalds return err; 18211da177e4SLinus Torvalds } 18221da177e4SLinus Torvalds 1823d9bf82c2SEric Dumazet static int ipv6_count_addresses(const struct inet6_dev *idev) 18241da177e4SLinus Torvalds { 1825d9bf82c2SEric Dumazet const struct inet6_ifaddr *ifp; 18261da177e4SLinus Torvalds int cnt = 0; 18271da177e4SLinus Torvalds 1828d9bf82c2SEric Dumazet rcu_read_lock(); 1829d9bf82c2SEric Dumazet list_for_each_entry_rcu(ifp, &idev->addr_list, if_list) 18301da177e4SLinus Torvalds cnt++; 1831d9bf82c2SEric Dumazet rcu_read_unlock(); 18321da177e4SLinus Torvalds return cnt; 18331da177e4SLinus Torvalds } 18341da177e4SLinus Torvalds 1835b71d1d42SEric Dumazet int ipv6_chk_addr(struct net *net, const struct in6_addr *addr, 18362a7851bfSFlorian Westphal const struct net_device *dev, int strict) 18371da177e4SLinus Torvalds { 1838232378e8SDavid Ahern return ipv6_chk_addr_and_flags(net, addr, dev, !dev, 1839232378e8SDavid Ahern strict, IFA_F_TENTATIVE); 1840c58da4c6SErik Kline } 1841c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr); 1842c58da4c6SErik Kline 18431893ff20SDavid Ahern /* device argument is used to find the L3 domain of interest. If 18441893ff20SDavid Ahern * skip_dev_check is set, then the ifp device is not checked against 18451893ff20SDavid Ahern * the passed in dev argument. So the 2 cases for addresses checks are: 18461893ff20SDavid Ahern * 1. does the address exist in the L3 domain that dev is part of 18471893ff20SDavid Ahern * (skip_dev_check = true), or 18481893ff20SDavid Ahern * 18491893ff20SDavid Ahern * 2. does the address exist on the specific device 18501893ff20SDavid Ahern * (skip_dev_check = false) 18511893ff20SDavid Ahern */ 1852c58da4c6SErik Kline int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr, 1853232378e8SDavid Ahern const struct net_device *dev, bool skip_dev_check, 1854232378e8SDavid Ahern int strict, u32 banned_flags) 1855c58da4c6SErik Kline { 18563f27fb23SEric Dumazet unsigned int hash = inet6_addr_hash(net, addr); 18571893ff20SDavid Ahern const struct net_device *l3mdev; 1858eedf042aSStephen Hemminger struct inet6_ifaddr *ifp; 1859c58da4c6SErik Kline u32 ifp_flags; 18601da177e4SLinus Torvalds 1861480318a0SEric Dumazet rcu_read_lock(); 1862232378e8SDavid Ahern 18631893ff20SDavid Ahern l3mdev = l3mdev_master_dev_rcu(dev); 1864232378e8SDavid Ahern if (skip_dev_check) 1865232378e8SDavid Ahern dev = NULL; 1866232378e8SDavid Ahern 1867b67bfe0dSSasha Levin hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) { 1868878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 1869bfeade08SDaniel Lezcano continue; 18701893ff20SDavid Ahern 18711893ff20SDavid Ahern if (l3mdev_master_dev_rcu(ifp->idev->dev) != l3mdev) 18721893ff20SDavid Ahern continue; 18731893ff20SDavid Ahern 1874c58da4c6SErik Kline /* Decouple optimistic from tentative for evaluation here. 1875c58da4c6SErik Kline * Ban optimistic addresses explicitly, when required. 1876c58da4c6SErik Kline */ 1877c58da4c6SErik Kline ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC) 1878c58da4c6SErik Kline ? (ifp->flags&~IFA_F_TENTATIVE) 1879c58da4c6SErik Kline : ifp->flags; 18801da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr) && 1881c58da4c6SErik Kline !(ifp_flags&banned_flags) && 188263159f29SIan Morris (!dev || ifp->idev->dev == dev || 1883eedf042aSStephen Hemminger !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) { 1884480318a0SEric Dumazet rcu_read_unlock(); 1885eedf042aSStephen Hemminger return 1; 1886eedf042aSStephen Hemminger } 1887eedf042aSStephen Hemminger } 18885c578aedSstephen hemminger 1889480318a0SEric Dumazet rcu_read_unlock(); 1890eedf042aSStephen Hemminger return 0; 18911da177e4SLinus Torvalds } 1892c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr_and_flags); 18937159039aSYOSHIFUJI Hideaki 18941da177e4SLinus Torvalds 18957df37ff3SCatalin\(ux\) M. BOIE /* Compares an address/prefix_len with addresses on device @dev. 18967df37ff3SCatalin\(ux\) M. BOIE * If one is found it returns true. 18977df37ff3SCatalin\(ux\) M. BOIE */ 18987df37ff3SCatalin\(ux\) M. BOIE bool ipv6_chk_custom_prefix(const struct in6_addr *addr, 18997df37ff3SCatalin\(ux\) M. BOIE const unsigned int prefix_len, struct net_device *dev) 19007df37ff3SCatalin\(ux\) M. BOIE { 190147e26941SEric Dumazet const struct inet6_ifaddr *ifa; 190247e26941SEric Dumazet const struct inet6_dev *idev; 19037df37ff3SCatalin\(ux\) M. BOIE bool ret = false; 19047df37ff3SCatalin\(ux\) M. BOIE 19057df37ff3SCatalin\(ux\) M. BOIE rcu_read_lock(); 19067df37ff3SCatalin\(ux\) M. BOIE idev = __in6_dev_get(dev); 19077df37ff3SCatalin\(ux\) M. BOIE if (idev) { 190847e26941SEric Dumazet list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) { 19097df37ff3SCatalin\(ux\) M. BOIE ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len); 19107df37ff3SCatalin\(ux\) M. BOIE if (ret) 19117df37ff3SCatalin\(ux\) M. BOIE break; 19127df37ff3SCatalin\(ux\) M. BOIE } 19137df37ff3SCatalin\(ux\) M. BOIE } 19147df37ff3SCatalin\(ux\) M. BOIE rcu_read_unlock(); 19157df37ff3SCatalin\(ux\) M. BOIE 19167df37ff3SCatalin\(ux\) M. BOIE return ret; 19177df37ff3SCatalin\(ux\) M. BOIE } 19187df37ff3SCatalin\(ux\) M. BOIE EXPORT_SYMBOL(ipv6_chk_custom_prefix); 19197df37ff3SCatalin\(ux\) M. BOIE 1920b71d1d42SEric Dumazet int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev) 192152eeeb84SYOSHIFUJI Hideaki { 192224ba333bSEric Dumazet const struct inet6_ifaddr *ifa; 192324ba333bSEric Dumazet const struct inet6_dev *idev; 192452eeeb84SYOSHIFUJI Hideaki int onlink; 192552eeeb84SYOSHIFUJI Hideaki 192652eeeb84SYOSHIFUJI Hideaki onlink = 0; 192752eeeb84SYOSHIFUJI Hideaki rcu_read_lock(); 192852eeeb84SYOSHIFUJI Hideaki idev = __in6_dev_get(dev); 192952eeeb84SYOSHIFUJI Hideaki if (idev) { 193024ba333bSEric Dumazet list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) { 193152eeeb84SYOSHIFUJI Hideaki onlink = ipv6_prefix_equal(addr, &ifa->addr, 193252eeeb84SYOSHIFUJI Hideaki ifa->prefix_len); 193352eeeb84SYOSHIFUJI Hideaki if (onlink) 193452eeeb84SYOSHIFUJI Hideaki break; 193552eeeb84SYOSHIFUJI Hideaki } 193652eeeb84SYOSHIFUJI Hideaki } 193752eeeb84SYOSHIFUJI Hideaki rcu_read_unlock(); 193852eeeb84SYOSHIFUJI Hideaki return onlink; 193952eeeb84SYOSHIFUJI Hideaki } 194052eeeb84SYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_prefix); 194152eeeb84SYOSHIFUJI Hideaki 19429acd9f3aSYOSHIFUJI Hideaki struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr, 19431cab3da6SDaniel Lezcano struct net_device *dev, int strict) 19441da177e4SLinus Torvalds { 19453f27fb23SEric Dumazet unsigned int hash = inet6_addr_hash(net, addr); 1946b79d1d54SDavid S. Miller struct inet6_ifaddr *ifp, *result = NULL; 19471da177e4SLinus Torvalds 194824f226daSEric Dumazet rcu_read_lock(); 194924f226daSEric Dumazet hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) { 1950878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 19511cab3da6SDaniel Lezcano continue; 19521da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr)) { 195363159f29SIan Morris if (!dev || ifp->idev->dev == dev || 19541da177e4SLinus Torvalds !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) { 1955b79d1d54SDavid S. Miller result = ifp; 19561da177e4SLinus Torvalds in6_ifa_hold(ifp); 19571da177e4SLinus Torvalds break; 19581da177e4SLinus Torvalds } 19591da177e4SLinus Torvalds } 19601da177e4SLinus Torvalds } 196124f226daSEric Dumazet rcu_read_unlock(); 19621da177e4SLinus Torvalds 1963b79d1d54SDavid S. Miller return result; 19641da177e4SLinus Torvalds } 19651da177e4SLinus Torvalds 19661da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */ 19671da177e4SLinus Torvalds 1968cc411d0bSBrian Haley static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed) 19691da177e4SLinus Torvalds { 19703d171f39SLubomir Rintel if (dad_failed) 19713d171f39SLubomir Rintel ifp->flags |= IFA_F_DADFAILED; 19723d171f39SLubomir Rintel 1973ec8add2aSSabrina Dubroca if (ifp->flags&IFA_F_TEMPORARY) { 19741da177e4SLinus Torvalds struct inet6_ifaddr *ifpub; 19751da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 19761da177e4SLinus Torvalds ifpub = ifp->ifpub; 19771da177e4SLinus Torvalds if (ifpub) { 19781da177e4SLinus Torvalds in6_ifa_hold(ifpub); 19791da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 1980fffcefe9SEric Dumazet ipv6_create_tempaddr(ifpub, ifp, true); 19811da177e4SLinus Torvalds in6_ifa_put(ifpub); 19821da177e4SLinus Torvalds } else { 19831da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 19841da177e4SLinus Torvalds } 19851da177e4SLinus Torvalds ipv6_del_addr(ifp); 1986ec8add2aSSabrina Dubroca } else if (ifp->flags&IFA_F_PERMANENT || !dad_failed) { 1987ec8add2aSSabrina Dubroca spin_lock_bh(&ifp->lock); 1988ec8add2aSSabrina Dubroca addrconf_del_dad_work(ifp); 1989ec8add2aSSabrina Dubroca ifp->flags |= IFA_F_TENTATIVE; 1990f1c02cfbSSabrina Dubroca if (dad_failed) 1991f1c02cfbSSabrina Dubroca ifp->flags &= ~IFA_F_OPTIMISTIC; 1992ec8add2aSSabrina Dubroca spin_unlock_bh(&ifp->lock); 1993ec8add2aSSabrina Dubroca if (dad_failed) 1994ec8add2aSSabrina Dubroca ipv6_ifa_notify(0, ifp); 1995ec8add2aSSabrina Dubroca in6_ifa_put(ifp); 1996c15b1ccaSHannes Frederic Sowa } else { 19971da177e4SLinus Torvalds ipv6_del_addr(ifp); 19981da177e4SLinus Torvalds } 1999c15b1ccaSHannes Frederic Sowa } 20001da177e4SLinus Torvalds 2001f2344a13SHerbert Xu static int addrconf_dad_end(struct inet6_ifaddr *ifp) 2002f2344a13SHerbert Xu { 2003f2344a13SHerbert Xu int err = -ENOENT; 2004f2344a13SHerbert Xu 20058e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 2006f2344a13SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DAD) { 2007f2344a13SHerbert Xu ifp->state = INET6_IFADDR_STATE_POSTDAD; 2008f2344a13SHerbert Xu err = 0; 2009f2344a13SHerbert Xu } 20108e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 2011f2344a13SHerbert Xu 2012f2344a13SHerbert Xu return err; 2013f2344a13SHerbert Xu } 2014f2344a13SHerbert Xu 2015da13c59bSVishwanath Pai void addrconf_dad_failure(struct sk_buff *skb, struct inet6_ifaddr *ifp) 20163c21edbdSYOSHIFUJI Hideaki { 20171b34be74SYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev; 20181855b7c3SHannes Frederic Sowa struct net *net = dev_net(ifp->idev->dev); 20199bdd8d40SBrian Haley 2020853dc2e0SUrsula Braun if (addrconf_dad_end(ifp)) { 2021853dc2e0SUrsula Braun in6_ifa_put(ifp); 2022f2344a13SHerbert Xu return; 2023853dc2e0SUrsula Braun } 2024f2344a13SHerbert Xu 2025da13c59bSVishwanath Pai net_info_ratelimited("%s: IPv6 duplicate address %pI6c used by %pM detected!\n", 2026da13c59bSVishwanath Pai ifp->idev->dev->name, &ifp->addr, eth_hdr(skb)->h_source); 20279bdd8d40SBrian Haley 20285f40ef77SHannes Frederic Sowa spin_lock_bh(&ifp->lock); 20291b34be74SYOSHIFUJI Hideaki 20305f40ef77SHannes Frederic Sowa if (ifp->flags & IFA_F_STABLE_PRIVACY) { 20315f40ef77SHannes Frederic Sowa struct in6_addr new_addr; 20325f40ef77SHannes Frederic Sowa struct inet6_ifaddr *ifp2; 20335f40ef77SHannes Frederic Sowa int retries = ifp->stable_privacy_retry + 1; 2034e6464b8cSDavid Ahern struct ifa6_config cfg = { 2035e6464b8cSDavid Ahern .pfx = &new_addr, 2036e6464b8cSDavid Ahern .plen = ifp->prefix_len, 2037e6464b8cSDavid Ahern .ifa_flags = ifp->flags, 2038e6464b8cSDavid Ahern .valid_lft = ifp->valid_lft, 2039e6464b8cSDavid Ahern .preferred_lft = ifp->prefered_lft, 2040e6464b8cSDavid Ahern .scope = ifp->scope, 2041e6464b8cSDavid Ahern }; 20425f40ef77SHannes Frederic Sowa 20431855b7c3SHannes Frederic Sowa if (retries > net->ipv6.sysctl.idgen_retries) { 20445f40ef77SHannes Frederic Sowa net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n", 20455f40ef77SHannes Frederic Sowa ifp->idev->dev->name); 20465f40ef77SHannes Frederic Sowa goto errdad; 20475f40ef77SHannes Frederic Sowa } 20485f40ef77SHannes Frederic Sowa 20495f40ef77SHannes Frederic Sowa new_addr = ifp->addr; 20505f40ef77SHannes Frederic Sowa if (ipv6_generate_stable_address(&new_addr, retries, 20515f40ef77SHannes Frederic Sowa idev)) 20525f40ef77SHannes Frederic Sowa goto errdad; 20535f40ef77SHannes Frederic Sowa 20545f40ef77SHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 20555f40ef77SHannes Frederic Sowa 20565f40ef77SHannes Frederic Sowa if (idev->cnf.max_addresses && 20575f40ef77SHannes Frederic Sowa ipv6_count_addresses(idev) >= 20585f40ef77SHannes Frederic Sowa idev->cnf.max_addresses) 20595f40ef77SHannes Frederic Sowa goto lock_errdad; 20605f40ef77SHannes Frederic Sowa 20615f40ef77SHannes Frederic Sowa net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n", 20625f40ef77SHannes Frederic Sowa ifp->idev->dev->name); 20635f40ef77SHannes Frederic Sowa 2064e6464b8cSDavid Ahern ifp2 = ipv6_add_addr(idev, &cfg, false, NULL); 20655f40ef77SHannes Frederic Sowa if (IS_ERR(ifp2)) 20665f40ef77SHannes Frederic Sowa goto lock_errdad; 20675f40ef77SHannes Frederic Sowa 20685f40ef77SHannes Frederic Sowa spin_lock_bh(&ifp2->lock); 20695f40ef77SHannes Frederic Sowa ifp2->stable_privacy_retry = retries; 20705f40ef77SHannes Frederic Sowa ifp2->state = INET6_IFADDR_STATE_PREDAD; 20715f40ef77SHannes Frederic Sowa spin_unlock_bh(&ifp2->lock); 20725f40ef77SHannes Frederic Sowa 20731855b7c3SHannes Frederic Sowa addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay); 20745f40ef77SHannes Frederic Sowa in6_ifa_put(ifp2); 20755f40ef77SHannes Frederic Sowa lock_errdad: 20765f40ef77SHannes Frederic Sowa spin_lock_bh(&ifp->lock); 20771b34be74SYOSHIFUJI Hideaki } 20781b34be74SYOSHIFUJI Hideaki 20795f40ef77SHannes Frederic Sowa errdad: 2080c15b1ccaSHannes Frederic Sowa /* transition from _POSTDAD to _ERRDAD */ 2081c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_ERRDAD; 20828e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 2083c15b1ccaSHannes Frederic Sowa 2084c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 0); 2085751eb6b6SWei Yongjun in6_ifa_put(ifp); 20863c21edbdSYOSHIFUJI Hideaki } 20871da177e4SLinus Torvalds 2088a9ed4a29SSabrina Dubroca /* Join to solicited addr multicast group. 2089a9ed4a29SSabrina Dubroca * caller must hold RTNL */ 2090b71d1d42SEric Dumazet void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr) 20911da177e4SLinus Torvalds { 20921da177e4SLinus Torvalds struct in6_addr maddr; 20931da177e4SLinus Torvalds 20941da177e4SLinus Torvalds if (dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 20951da177e4SLinus Torvalds return; 20961da177e4SLinus Torvalds 20971da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr); 20981da177e4SLinus Torvalds ipv6_dev_mc_inc(dev, &maddr); 20991da177e4SLinus Torvalds } 21001da177e4SLinus Torvalds 2101a9ed4a29SSabrina Dubroca /* caller must hold RTNL */ 2102b71d1d42SEric Dumazet void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr) 21031da177e4SLinus Torvalds { 21041da177e4SLinus Torvalds struct in6_addr maddr; 21051da177e4SLinus Torvalds 21061da177e4SLinus Torvalds if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 21071da177e4SLinus Torvalds return; 21081da177e4SLinus Torvalds 21091da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr); 21101da177e4SLinus Torvalds __ipv6_dev_mc_dec(idev, &maddr); 21111da177e4SLinus Torvalds } 21121da177e4SLinus Torvalds 2113a9ed4a29SSabrina Dubroca /* caller must hold RTNL */ 211420380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp) 21151da177e4SLinus Torvalds { 21161da177e4SLinus Torvalds struct in6_addr addr; 2117c15b1ccaSHannes Frederic Sowa 211888ad3149SHannes Frederic Sowa if (ifp->prefix_len >= 127) /* RFC 6164 */ 21192bda8a0cSBjørn Mork return; 21201da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 21211da177e4SLinus Torvalds if (ipv6_addr_any(&addr)) 21221da177e4SLinus Torvalds return; 2123013b4d90SWANG Cong __ipv6_dev_ac_inc(ifp->idev, &addr); 21241da177e4SLinus Torvalds } 21251da177e4SLinus Torvalds 2126a9ed4a29SSabrina Dubroca /* caller must hold RTNL */ 212720380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp) 21281da177e4SLinus Torvalds { 21291da177e4SLinus Torvalds struct in6_addr addr; 2130c15b1ccaSHannes Frederic Sowa 213188ad3149SHannes Frederic Sowa if (ifp->prefix_len >= 127) /* RFC 6164 */ 213232019e65SYOSHIFUJI Hideaki return; 21331da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 21341da177e4SLinus Torvalds if (ipv6_addr_any(&addr)) 21351da177e4SLinus Torvalds return; 21361da177e4SLinus Torvalds __ipv6_dev_ac_dec(ifp->idev, &addr); 21371da177e4SLinus Torvalds } 21381da177e4SLinus Torvalds 21398a7a4b47SAlexander Aring static int addrconf_ifid_6lowpan(u8 *eui, struct net_device *dev) 214006a4c1c5Salex.bluesman.smirnov@gmail.com { 21418a7a4b47SAlexander Aring switch (dev->addr_len) { 21428a7a4b47SAlexander Aring case ETH_ALEN: 21439dae2e03SLuiz Augusto von Dentz memcpy(eui, dev->dev_addr, 3); 21449dae2e03SLuiz Augusto von Dentz eui[3] = 0xFF; 21459dae2e03SLuiz Augusto von Dentz eui[4] = 0xFE; 21469dae2e03SLuiz Augusto von Dentz memcpy(eui + 5, dev->dev_addr + 3, 3); 21479dae2e03SLuiz Augusto von Dentz break; 21488a7a4b47SAlexander Aring case EUI64_ADDR_LEN: 21495241c2d7SAlexander Aring memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN); 21505e98a36eSYOSHIFUJI Hideaki / 吉藤英明 eui[0] ^= 2; 21518a7a4b47SAlexander Aring break; 21528a7a4b47SAlexander Aring default: 21538a7a4b47SAlexander Aring return -1; 21548a7a4b47SAlexander Aring } 21558a7a4b47SAlexander Aring 215606a4c1c5Salex.bluesman.smirnov@gmail.com return 0; 215706a4c1c5Salex.bluesman.smirnov@gmail.com } 215806a4c1c5Salex.bluesman.smirnov@gmail.com 2159cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev) 2160cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 { 2161cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 union fwnet_hwaddr *ha; 2162cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 2163cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 if (dev->addr_len != FWNET_ALEN) 2164cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return -1; 2165cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 2166cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 ha = (union fwnet_hwaddr *)dev->dev_addr; 2167cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 2168cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id)); 2169cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 eui[0] ^= 2; 2170cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return 0; 2171cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 } 2172cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 2173073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev) 2174073a8e0eSYOSHIFUJI Hideaki { 21751da177e4SLinus Torvalds /* XXX: inherit EUI-64 from other interface -- yoshfuji */ 21761da177e4SLinus Torvalds if (dev->addr_len != ARCNET_ALEN) 21771da177e4SLinus Torvalds return -1; 21781da177e4SLinus Torvalds memset(eui, 0, 7); 21791da177e4SLinus Torvalds eui[7] = *(u8 *)dev->dev_addr; 21801da177e4SLinus Torvalds return 0; 2181073a8e0eSYOSHIFUJI Hideaki } 2182073a8e0eSYOSHIFUJI Hideaki 2183073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev) 2184073a8e0eSYOSHIFUJI Hideaki { 21851da177e4SLinus Torvalds if (dev->addr_len != INFINIBAND_ALEN) 21861da177e4SLinus Torvalds return -1; 21871da177e4SLinus Torvalds memcpy(eui, dev->dev_addr + 12, 8); 21881da177e4SLinus Torvalds eui[0] |= 2; 21891da177e4SLinus Torvalds return 0; 21901da177e4SLinus Torvalds } 2191073a8e0eSYOSHIFUJI Hideaki 2192c61393eaSstephen hemminger static int __ipv6_isatap_ifid(u8 *eui, __be32 addr) 2193dfd982baSYOSHIFUJI Hideaki { 21949af28511SSascha Hlusiak if (addr == 0) 21959af28511SSascha Hlusiak return -1; 2196dfd982baSYOSHIFUJI Hideaki eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) || 2197dfd982baSYOSHIFUJI Hideaki ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) || 2198dfd982baSYOSHIFUJI Hideaki ipv4_is_private_172(addr) || ipv4_is_test_192(addr) || 2199dfd982baSYOSHIFUJI Hideaki ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) || 2200dfd982baSYOSHIFUJI Hideaki ipv4_is_test_198(addr) || ipv4_is_multicast(addr) || 2201dfd982baSYOSHIFUJI Hideaki ipv4_is_lbcast(addr)) ? 0x00 : 0x02; 2202dfd982baSYOSHIFUJI Hideaki eui[1] = 0; 2203dfd982baSYOSHIFUJI Hideaki eui[2] = 0x5E; 2204dfd982baSYOSHIFUJI Hideaki eui[3] = 0xFE; 2205dfd982baSYOSHIFUJI Hideaki memcpy(eui + 4, &addr, 4); 2206dfd982baSYOSHIFUJI Hideaki return 0; 2207dfd982baSYOSHIFUJI Hideaki } 2208dfd982baSYOSHIFUJI Hideaki 2209dfd982baSYOSHIFUJI Hideaki static int addrconf_ifid_sit(u8 *eui, struct net_device *dev) 2210dfd982baSYOSHIFUJI Hideaki { 2211dfd982baSYOSHIFUJI Hideaki if (dev->priv_flags & IFF_ISATAP) 2212dfd982baSYOSHIFUJI Hideaki return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); 2213dfd982baSYOSHIFUJI Hideaki return -1; 2214dfd982baSYOSHIFUJI Hideaki } 2215dfd982baSYOSHIFUJI Hideaki 2216aee80b54Sstephen hemminger static int addrconf_ifid_gre(u8 *eui, struct net_device *dev) 2217aee80b54Sstephen hemminger { 2218aee80b54Sstephen hemminger return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); 2219aee80b54Sstephen hemminger } 2220aee80b54Sstephen hemminger 2221e837735eSNicolas Dichtel static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev) 2222e837735eSNicolas Dichtel { 2223e837735eSNicolas Dichtel memcpy(eui, dev->perm_addr, 3); 2224e837735eSNicolas Dichtel memcpy(eui + 5, dev->perm_addr + 3, 3); 2225e837735eSNicolas Dichtel eui[3] = 0xFF; 2226e837735eSNicolas Dichtel eui[4] = 0xFE; 2227e837735eSNicolas Dichtel eui[0] ^= 2; 2228e837735eSNicolas Dichtel return 0; 2229e837735eSNicolas Dichtel } 2230e837735eSNicolas Dichtel 2231073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev) 2232073a8e0eSYOSHIFUJI Hideaki { 2233073a8e0eSYOSHIFUJI Hideaki switch (dev->type) { 2234073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ETHER: 2235073a8e0eSYOSHIFUJI Hideaki case ARPHRD_FDDI: 2236073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_eui48(eui, dev); 2237073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ARCNET: 2238073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_arcnet(eui, dev); 2239073a8e0eSYOSHIFUJI Hideaki case ARPHRD_INFINIBAND: 2240073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_infiniband(eui, dev); 2241c7dc89c0SFred L. Templin case ARPHRD_SIT: 2242dfd982baSYOSHIFUJI Hideaki return addrconf_ifid_sit(eui, dev); 2243aee80b54Sstephen hemminger case ARPHRD_IPGRE: 224445ce0fd1SFelix Jia case ARPHRD_TUNNEL: 2245aee80b54Sstephen hemminger return addrconf_ifid_gre(eui, dev); 2246e74bccb8SJukka Rissanen case ARPHRD_6LOWPAN: 22478a7a4b47SAlexander Aring return addrconf_ifid_6lowpan(eui, dev); 2248cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 case ARPHRD_IEEE1394: 2249cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return addrconf_ifid_ieee1394(eui, dev); 2250e837735eSNicolas Dichtel case ARPHRD_TUNNEL6: 225145ce0fd1SFelix Jia case ARPHRD_IP6GRE: 22529deb441cSSubash Abhinov Kasiviswanathan case ARPHRD_RAWIP: 2253e837735eSNicolas Dichtel return addrconf_ifid_ip6tnl(eui, dev); 2254073a8e0eSYOSHIFUJI Hideaki } 22551da177e4SLinus Torvalds return -1; 22561da177e4SLinus Torvalds } 22571da177e4SLinus Torvalds 22581da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev) 22591da177e4SLinus Torvalds { 22601da177e4SLinus Torvalds int err = -1; 22611da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 22621da177e4SLinus Torvalds 22631da177e4SLinus Torvalds read_lock_bh(&idev->lock); 2264602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) { 2265602582caSHannes Frederic Sowa if (ifp->scope > IFA_LINK) 2266602582caSHannes Frederic Sowa break; 22671da177e4SLinus Torvalds if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) { 22681da177e4SLinus Torvalds memcpy(eui, ifp->addr.s6_addr+8, 8); 22691da177e4SLinus Torvalds err = 0; 22701da177e4SLinus Torvalds break; 22711da177e4SLinus Torvalds } 22721da177e4SLinus Torvalds } 22731da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 22741da177e4SLinus Torvalds return err; 22751da177e4SLinus Torvalds } 22761da177e4SLinus Torvalds 22771da177e4SLinus Torvalds /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */ 22789d6280daSJiri Bohac static void ipv6_regen_rndid(struct inet6_dev *idev) 22791da177e4SLinus Torvalds { 22801da177e4SLinus Torvalds regen: 2281955189efSYOSHIFUJI Hideaki get_random_bytes(idev->rndid, sizeof(idev->rndid)); 22821da177e4SLinus Torvalds idev->rndid[0] &= ~0x02; 22831da177e4SLinus Torvalds 22841da177e4SLinus Torvalds /* 22851da177e4SLinus Torvalds * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>: 22861da177e4SLinus Torvalds * check if generated address is not inappropriate 22871da177e4SLinus Torvalds * 22881da177e4SLinus Torvalds * - Reserved subnet anycast (RFC 2526) 22891da177e4SLinus Torvalds * 11111101 11....11 1xxxxxxx 2290c7dc89c0SFred L. Templin * - ISATAP (RFC4214) 6.1 22911da177e4SLinus Torvalds * 00-00-5E-FE-xx-xx-xx-xx 22921da177e4SLinus Torvalds * - value 0 22931da177e4SLinus Torvalds * - XXX: already assigned to an address on the device 22941da177e4SLinus Torvalds */ 22951da177e4SLinus Torvalds if (idev->rndid[0] == 0xfd && 22961da177e4SLinus Torvalds (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff && 22971da177e4SLinus Torvalds (idev->rndid[7]&0x80)) 22981da177e4SLinus Torvalds goto regen; 22991da177e4SLinus Torvalds if ((idev->rndid[0]|idev->rndid[1]) == 0) { 23001da177e4SLinus Torvalds if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe) 23011da177e4SLinus Torvalds goto regen; 23021da177e4SLinus Torvalds if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00) 23031da177e4SLinus Torvalds goto regen; 23041da177e4SLinus Torvalds } 23051da177e4SLinus Torvalds } 23061da177e4SLinus Torvalds 23079d6280daSJiri Bohac static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) 23088e5e8f30SEldad Zack { 23091da177e4SLinus Torvalds if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0) 23109d6280daSJiri Bohac ipv6_regen_rndid(idev); 23111da177e4SLinus Torvalds } 23121da177e4SLinus Torvalds 23131da177e4SLinus Torvalds /* 23141da177e4SLinus Torvalds * Add prefix route. 23151da177e4SLinus Torvalds */ 23161da177e4SLinus Torvalds 23171da177e4SLinus Torvalds static void 23188308f3ffSDavid Ahern addrconf_prefix_route(struct in6_addr *pfx, int plen, u32 metric, 23198308f3ffSDavid Ahern struct net_device *dev, unsigned long expires, 23208308f3ffSDavid Ahern u32 flags, gfp_t gfp_flags) 23211da177e4SLinus Torvalds { 232286872cb5SThomas Graf struct fib6_config cfg = { 2323ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX, 23248308f3ffSDavid Ahern .fc_metric = metric ? : IP6_RT_PRIO_ADDRCONF, 232586872cb5SThomas Graf .fc_ifindex = dev->ifindex, 232686872cb5SThomas Graf .fc_expires = expires, 232786872cb5SThomas Graf .fc_dst_len = plen, 232886872cb5SThomas Graf .fc_flags = RTF_UP | flags, 2329c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 2330f410a1fbSStephen Hemminger .fc_protocol = RTPROT_KERNEL, 2331e8478e80SDavid Ahern .fc_type = RTN_UNICAST, 233286872cb5SThomas Graf }; 23331da177e4SLinus Torvalds 23344e3fd7a0SAlexey Dobriyan cfg.fc_dst = *pfx; 23351da177e4SLinus Torvalds 23361da177e4SLinus Torvalds /* Prevent useless cloning on PtP SIT. 23371da177e4SLinus Torvalds This thing is done here expecting that the whole 23381da177e4SLinus Torvalds class of non-broadcast devices need not cloning. 23391da177e4SLinus Torvalds */ 234007a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 23411da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT)) 234286872cb5SThomas Graf cfg.fc_flags |= RTF_NONEXTHOP; 23430be669bbSJoerg Roedel #endif 23441da177e4SLinus Torvalds 2345acb54e3cSDavid Ahern ip6_route_add(&cfg, gfp_flags, NULL); 23461da177e4SLinus Torvalds } 23471da177e4SLinus Torvalds 234814ef37b6SAndreas Hofmeister 23498d1c802bSDavid Ahern static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx, 235014ef37b6SAndreas Hofmeister int plen, 235114ef37b6SAndreas Hofmeister const struct net_device *dev, 235214ef37b6SAndreas Hofmeister u32 flags, u32 noflags) 235314ef37b6SAndreas Hofmeister { 235414ef37b6SAndreas Hofmeister struct fib6_node *fn; 23558d1c802bSDavid Ahern struct fib6_info *rt = NULL; 235614ef37b6SAndreas Hofmeister struct fib6_table *table; 2357ca254490SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX; 235814ef37b6SAndreas Hofmeister 2359ca254490SDavid Ahern table = fib6_get_table(dev_net(dev), tb_id); 236063159f29SIan Morris if (!table) 236114ef37b6SAndreas Hofmeister return NULL; 236214ef37b6SAndreas Hofmeister 236366f5d6ceSWei Wang rcu_read_lock(); 236438fbeeeeSWei Wang fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0, true); 236514ef37b6SAndreas Hofmeister if (!fn) 236614ef37b6SAndreas Hofmeister goto out; 23671f56a01fSMartin KaFai Lau 236866f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 23695e670d84SDavid Ahern if (rt->fib6_nh.nh_dev->ifindex != dev->ifindex) 237014ef37b6SAndreas Hofmeister continue; 237193c2fb25SDavid Ahern if ((rt->fib6_flags & flags) != flags) 237214ef37b6SAndreas Hofmeister continue; 237393c2fb25SDavid Ahern if ((rt->fib6_flags & noflags) != 0) 237414ef37b6SAndreas Hofmeister continue; 2375e873e4b9SWei Wang if (!fib6_info_hold_safe(rt)) 2376e873e4b9SWei Wang continue; 237714ef37b6SAndreas Hofmeister break; 237814ef37b6SAndreas Hofmeister } 237914ef37b6SAndreas Hofmeister out: 238066f5d6ceSWei Wang rcu_read_unlock(); 238114ef37b6SAndreas Hofmeister return rt; 238214ef37b6SAndreas Hofmeister } 238314ef37b6SAndreas Hofmeister 238414ef37b6SAndreas Hofmeister 23851da177e4SLinus Torvalds /* Create "default" multicast route to the interface */ 23861da177e4SLinus Torvalds 23871da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev) 23881da177e4SLinus Torvalds { 238986872cb5SThomas Graf struct fib6_config cfg = { 2390ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL, 239186872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 239286872cb5SThomas Graf .fc_ifindex = dev->ifindex, 239386872cb5SThomas Graf .fc_dst_len = 8, 239486872cb5SThomas Graf .fc_flags = RTF_UP, 2395e8478e80SDavid Ahern .fc_type = RTN_UNICAST, 2396c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 239786872cb5SThomas Graf }; 23981da177e4SLinus Torvalds 239986872cb5SThomas Graf ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0); 240086872cb5SThomas Graf 2401e500c6d3SCong Wang ip6_route_add(&cfg, GFP_KERNEL, NULL); 24021da177e4SLinus Torvalds } 24031da177e4SLinus Torvalds 24041da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev) 24051da177e4SLinus Torvalds { 24061da177e4SLinus Torvalds struct inet6_dev *idev; 24071da177e4SLinus Torvalds 24081da177e4SLinus Torvalds ASSERT_RTNL(); 24091da177e4SLinus Torvalds 2410e21e8467SStephen Hemminger idev = ipv6_find_idev(dev); 2411e21e8467SStephen Hemminger if (!idev) 241264e724f6SBrian Haley return ERR_PTR(-ENOBUFS); 241364e724f6SBrian Haley 241464e724f6SBrian Haley if (idev->cnf.disable_ipv6) 241564e724f6SBrian Haley return ERR_PTR(-EACCES); 24161da177e4SLinus Torvalds 24171da177e4SLinus Torvalds /* Add default multicast route */ 2418ba46ee4cSDavid Ahern if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev)) 24191da177e4SLinus Torvalds addrconf_add_mroute(dev); 24201da177e4SLinus Torvalds 24211da177e4SLinus Torvalds return idev; 24221da177e4SLinus Torvalds } 24231da177e4SLinus Torvalds 242453bd6749SJiri Pirko static void manage_tempaddrs(struct inet6_dev *idev, 242553bd6749SJiri Pirko struct inet6_ifaddr *ifp, 242653bd6749SJiri Pirko __u32 valid_lft, __u32 prefered_lft, 242753bd6749SJiri Pirko bool create, unsigned long now) 242853bd6749SJiri Pirko { 242953bd6749SJiri Pirko u32 flags; 243053bd6749SJiri Pirko struct inet6_ifaddr *ift; 243153bd6749SJiri Pirko 243253bd6749SJiri Pirko read_lock_bh(&idev->lock); 243353bd6749SJiri Pirko /* update all temporary addresses in the list */ 243453bd6749SJiri Pirko list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) { 243553bd6749SJiri Pirko int age, max_valid, max_prefered; 243653bd6749SJiri Pirko 243753bd6749SJiri Pirko if (ifp != ift->ifpub) 243853bd6749SJiri Pirko continue; 243953bd6749SJiri Pirko 244053bd6749SJiri Pirko /* RFC 4941 section 3.3: 244153bd6749SJiri Pirko * If a received option will extend the lifetime of a public 244253bd6749SJiri Pirko * address, the lifetimes of temporary addresses should 244353bd6749SJiri Pirko * be extended, subject to the overall constraint that no 244453bd6749SJiri Pirko * temporary addresses should ever remain "valid" or "preferred" 244553bd6749SJiri Pirko * for a time longer than (TEMP_VALID_LIFETIME) or 244653bd6749SJiri Pirko * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively. 244753bd6749SJiri Pirko */ 244853bd6749SJiri Pirko age = (now - ift->cstamp) / HZ; 244953bd6749SJiri Pirko max_valid = idev->cnf.temp_valid_lft - age; 245053bd6749SJiri Pirko if (max_valid < 0) 245153bd6749SJiri Pirko max_valid = 0; 245253bd6749SJiri Pirko 245353bd6749SJiri Pirko max_prefered = idev->cnf.temp_prefered_lft - 245476506a98SJiri Bohac idev->desync_factor - age; 245553bd6749SJiri Pirko if (max_prefered < 0) 245653bd6749SJiri Pirko max_prefered = 0; 245753bd6749SJiri Pirko 245853bd6749SJiri Pirko if (valid_lft > max_valid) 245953bd6749SJiri Pirko valid_lft = max_valid; 246053bd6749SJiri Pirko 246153bd6749SJiri Pirko if (prefered_lft > max_prefered) 246253bd6749SJiri Pirko prefered_lft = max_prefered; 246353bd6749SJiri Pirko 246453bd6749SJiri Pirko spin_lock(&ift->lock); 246553bd6749SJiri Pirko flags = ift->flags; 246653bd6749SJiri Pirko ift->valid_lft = valid_lft; 246753bd6749SJiri Pirko ift->prefered_lft = prefered_lft; 246853bd6749SJiri Pirko ift->tstamp = now; 246953bd6749SJiri Pirko if (prefered_lft > 0) 247053bd6749SJiri Pirko ift->flags &= ~IFA_F_DEPRECATED; 247153bd6749SJiri Pirko 247253bd6749SJiri Pirko spin_unlock(&ift->lock); 247353bd6749SJiri Pirko if (!(flags&IFA_F_TENTATIVE)) 247453bd6749SJiri Pirko ipv6_ifa_notify(0, ift); 247553bd6749SJiri Pirko } 247653bd6749SJiri Pirko 247753bd6749SJiri Pirko if ((create || list_empty(&idev->tempaddr_list)) && 247853bd6749SJiri Pirko idev->cnf.use_tempaddr > 0) { 247953bd6749SJiri Pirko /* When a new public address is created as described 248053bd6749SJiri Pirko * in [ADDRCONF], also create a new temporary address. 248153bd6749SJiri Pirko * Also create a temporary address if it's enabled but 248253bd6749SJiri Pirko * no temporary address currently exists. 248353bd6749SJiri Pirko */ 248453bd6749SJiri Pirko read_unlock_bh(&idev->lock); 2485fffcefe9SEric Dumazet ipv6_create_tempaddr(ifp, NULL, false); 248653bd6749SJiri Pirko } else { 248753bd6749SJiri Pirko read_unlock_bh(&idev->lock); 248853bd6749SJiri Pirko } 248953bd6749SJiri Pirko } 249053bd6749SJiri Pirko 2491cc9da6ccSBjørn Mork static bool is_addr_mode_generate_stable(struct inet6_dev *idev) 2492cc9da6ccSBjørn Mork { 2493d35a00b8SFelix Jia return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY || 2494d35a00b8SFelix Jia idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM; 2495cc9da6ccSBjørn Mork } 2496cc9da6ccSBjørn Mork 2497cc84b3c6SAlexander Aring int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev, 24984f672235SAlexander Aring const struct prefix_info *pinfo, 24994f672235SAlexander Aring struct inet6_dev *in6_dev, 2500cc84b3c6SAlexander Aring const struct in6_addr *addr, int addr_type, 2501cc84b3c6SAlexander Aring u32 addr_flags, bool sllao, bool tokenized, 25024f672235SAlexander Aring __u32 valid_lft, u32 prefered_lft) 25034f672235SAlexander Aring { 25044f672235SAlexander Aring struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1); 25054f672235SAlexander Aring int create = 0, update_lft = 0; 25064f672235SAlexander Aring 25074f672235SAlexander Aring if (!ifp && valid_lft) { 25084f672235SAlexander Aring int max_addresses = in6_dev->cnf.max_addresses; 2509e6464b8cSDavid Ahern struct ifa6_config cfg = { 2510e6464b8cSDavid Ahern .pfx = addr, 2511e6464b8cSDavid Ahern .plen = pinfo->prefix_len, 2512e6464b8cSDavid Ahern .ifa_flags = addr_flags, 2513e6464b8cSDavid Ahern .valid_lft = valid_lft, 2514e6464b8cSDavid Ahern .preferred_lft = prefered_lft, 2515e6464b8cSDavid Ahern .scope = addr_type & IPV6_ADDR_SCOPE_MASK, 2516e6464b8cSDavid Ahern }; 25174f672235SAlexander Aring 25184f672235SAlexander Aring #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 251935e015e1SMatteo Croce if ((net->ipv6.devconf_all->optimistic_dad || 252035e015e1SMatteo Croce in6_dev->cnf.optimistic_dad) && 25214f672235SAlexander Aring !net->ipv6.devconf_all->forwarding && sllao) 2522e6464b8cSDavid Ahern cfg.ifa_flags |= IFA_F_OPTIMISTIC; 25234f672235SAlexander Aring #endif 25244f672235SAlexander Aring 25254f672235SAlexander Aring /* Do not allow to create too much of autoconfigured 25264f672235SAlexander Aring * addresses; this would be too easy way to crash kernel. 25274f672235SAlexander Aring */ 25284f672235SAlexander Aring if (!max_addresses || 25294f672235SAlexander Aring ipv6_count_addresses(in6_dev) < max_addresses) 2530e6464b8cSDavid Ahern ifp = ipv6_add_addr(in6_dev, &cfg, false, NULL); 25314f672235SAlexander Aring 25324f672235SAlexander Aring if (IS_ERR_OR_NULL(ifp)) 25334f672235SAlexander Aring return -1; 25344f672235SAlexander Aring 25354f672235SAlexander Aring create = 1; 25364f672235SAlexander Aring spin_lock_bh(&ifp->lock); 25374f672235SAlexander Aring ifp->flags |= IFA_F_MANAGETEMPADDR; 25384f672235SAlexander Aring ifp->cstamp = jiffies; 25394f672235SAlexander Aring ifp->tokenized = tokenized; 25404f672235SAlexander Aring spin_unlock_bh(&ifp->lock); 25414f672235SAlexander Aring addrconf_dad_start(ifp); 25424f672235SAlexander Aring } 25434f672235SAlexander Aring 25444f672235SAlexander Aring if (ifp) { 25454f672235SAlexander Aring u32 flags; 25464f672235SAlexander Aring unsigned long now; 25474f672235SAlexander Aring u32 stored_lft; 25484f672235SAlexander Aring 25494f672235SAlexander Aring /* update lifetime (RFC2462 5.5.3 e) */ 25504f672235SAlexander Aring spin_lock_bh(&ifp->lock); 25514f672235SAlexander Aring now = jiffies; 25524f672235SAlexander Aring if (ifp->valid_lft > (now - ifp->tstamp) / HZ) 25534f672235SAlexander Aring stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ; 25544f672235SAlexander Aring else 25554f672235SAlexander Aring stored_lft = 0; 2556f85f94b8SLorenzo Bianconi if (!create && stored_lft) { 25574f672235SAlexander Aring const u32 minimum_lft = min_t(u32, 25584f672235SAlexander Aring stored_lft, MIN_VALID_LIFETIME); 25594f672235SAlexander Aring valid_lft = max(valid_lft, minimum_lft); 25604f672235SAlexander Aring 25614f672235SAlexander Aring /* RFC4862 Section 5.5.3e: 25624f672235SAlexander Aring * "Note that the preferred lifetime of the 25634f672235SAlexander Aring * corresponding address is always reset to 25644f672235SAlexander Aring * the Preferred Lifetime in the received 25654f672235SAlexander Aring * Prefix Information option, regardless of 25664f672235SAlexander Aring * whether the valid lifetime is also reset or 25674f672235SAlexander Aring * ignored." 25684f672235SAlexander Aring * 25694f672235SAlexander Aring * So we should always update prefered_lft here. 25704f672235SAlexander Aring */ 25714f672235SAlexander Aring update_lft = 1; 25724f672235SAlexander Aring } 25734f672235SAlexander Aring 25744f672235SAlexander Aring if (update_lft) { 25754f672235SAlexander Aring ifp->valid_lft = valid_lft; 25764f672235SAlexander Aring ifp->prefered_lft = prefered_lft; 25774f672235SAlexander Aring ifp->tstamp = now; 25784f672235SAlexander Aring flags = ifp->flags; 25794f672235SAlexander Aring ifp->flags &= ~IFA_F_DEPRECATED; 25804f672235SAlexander Aring spin_unlock_bh(&ifp->lock); 25814f672235SAlexander Aring 25824f672235SAlexander Aring if (!(flags&IFA_F_TENTATIVE)) 25834f672235SAlexander Aring ipv6_ifa_notify(0, ifp); 25844f672235SAlexander Aring } else 25854f672235SAlexander Aring spin_unlock_bh(&ifp->lock); 25864f672235SAlexander Aring 25874f672235SAlexander Aring manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft, 25884f672235SAlexander Aring create, now); 25894f672235SAlexander Aring 25904f672235SAlexander Aring in6_ifa_put(ifp); 25914f672235SAlexander Aring addrconf_verify(); 25924f672235SAlexander Aring } 25934f672235SAlexander Aring 25944f672235SAlexander Aring return 0; 25954f672235SAlexander Aring } 2596cc84b3c6SAlexander Aring EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr); 25974f672235SAlexander Aring 2598e6bff995SNeil Horman void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao) 25991da177e4SLinus Torvalds { 26001da177e4SLinus Torvalds struct prefix_info *pinfo; 26011da177e4SLinus Torvalds __u32 valid_lft; 26021da177e4SLinus Torvalds __u32 prefered_lft; 26034f672235SAlexander Aring int addr_type, err; 260464236f3fSHannes Frederic Sowa u32 addr_flags = 0; 26051da177e4SLinus Torvalds struct inet6_dev *in6_dev; 260656d417b1SBrian Haley struct net *net = dev_net(dev); 26071da177e4SLinus Torvalds 26081da177e4SLinus Torvalds pinfo = (struct prefix_info *) opt; 26091da177e4SLinus Torvalds 26101da177e4SLinus Torvalds if (len < sizeof(struct prefix_info)) { 2611e32ac250SJoe Perches netdev_dbg(dev, "addrconf: prefix option too short\n"); 26121da177e4SLinus Torvalds return; 26131da177e4SLinus Torvalds } 26141da177e4SLinus Torvalds 26151da177e4SLinus Torvalds /* 26161da177e4SLinus Torvalds * Validation checks ([ADDRCONF], page 19) 26171da177e4SLinus Torvalds */ 26181da177e4SLinus Torvalds 26191da177e4SLinus Torvalds addr_type = ipv6_addr_type(&pinfo->prefix); 26201da177e4SLinus Torvalds 26211da177e4SLinus Torvalds if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)) 26221da177e4SLinus Torvalds return; 26231da177e4SLinus Torvalds 26241da177e4SLinus Torvalds valid_lft = ntohl(pinfo->valid); 26251da177e4SLinus Torvalds prefered_lft = ntohl(pinfo->prefered); 26261da177e4SLinus Torvalds 26271da177e4SLinus Torvalds if (prefered_lft > valid_lft) { 2628e87cc472SJoe Perches net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n"); 26291da177e4SLinus Torvalds return; 26301da177e4SLinus Torvalds } 26311da177e4SLinus Torvalds 26321da177e4SLinus Torvalds in6_dev = in6_dev_get(dev); 26331da177e4SLinus Torvalds 263463159f29SIan Morris if (!in6_dev) { 2635e87cc472SJoe Perches net_dbg_ratelimited("addrconf: device %s not configured\n", 2636e87cc472SJoe Perches dev->name); 26371da177e4SLinus Torvalds return; 26381da177e4SLinus Torvalds } 26391da177e4SLinus Torvalds 26401da177e4SLinus Torvalds /* 26411da177e4SLinus Torvalds * Two things going on here: 26421da177e4SLinus Torvalds * 1) Add routes for on-link prefixes 26431da177e4SLinus Torvalds * 2) Configure prefixes with the auto flag set 26441da177e4SLinus Torvalds */ 26451da177e4SLinus Torvalds 26464bed72e4SYOSHIFUJI Hideaki if (pinfo->onlink) { 26478d1c802bSDavid Ahern struct fib6_info *rt; 26484bed72e4SYOSHIFUJI Hideaki unsigned long rt_expires; 26494bed72e4SYOSHIFUJI Hideaki 26501da177e4SLinus Torvalds /* Avoid arithmetic overflow. Really, we could 26516f704992SYOSHIFUJI Hideaki * save rt_expires in seconds, likely valid_lft, 26526f704992SYOSHIFUJI Hideaki * but it would require division in fib gc, that it 26536f704992SYOSHIFUJI Hideaki * not good. 26541da177e4SLinus Torvalds */ 26554bed72e4SYOSHIFUJI Hideaki if (HZ > USER_HZ) 26564bed72e4SYOSHIFUJI Hideaki rt_expires = addrconf_timeout_fixup(valid_lft, HZ); 26574bed72e4SYOSHIFUJI Hideaki else 26584bed72e4SYOSHIFUJI Hideaki rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ); 26593dd4bc68SYOSHIFUJI Hideaki 26604bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(rt_expires)) 26614bed72e4SYOSHIFUJI Hideaki rt_expires *= HZ; 26621da177e4SLinus Torvalds 266314ef37b6SAndreas Hofmeister rt = addrconf_get_prefix_route(&pinfo->prefix, 266414ef37b6SAndreas Hofmeister pinfo->prefix_len, 266514ef37b6SAndreas Hofmeister dev, 266614ef37b6SAndreas Hofmeister RTF_ADDRCONF | RTF_PREFIX_RT, 266714ef37b6SAndreas Hofmeister RTF_GATEWAY | RTF_DEFAULT); 26681da177e4SLinus Torvalds 266914ef37b6SAndreas Hofmeister if (rt) { 26706f704992SYOSHIFUJI Hideaki /* Autoconf prefix route */ 26711da177e4SLinus Torvalds if (valid_lft == 0) { 2672afb1d4b5SDavid Ahern ip6_del_rt(net, rt); 26731da177e4SLinus Torvalds rt = NULL; 26744bed72e4SYOSHIFUJI Hideaki } else if (addrconf_finite_timeout(rt_expires)) { 26756f704992SYOSHIFUJI Hideaki /* not infinity */ 267614895687SDavid Ahern fib6_set_expires(rt, jiffies + rt_expires); 26776f704992SYOSHIFUJI Hideaki } else { 267814895687SDavid Ahern fib6_clean_expires(rt); 26791da177e4SLinus Torvalds } 26801da177e4SLinus Torvalds } else if (valid_lft) { 26816f704992SYOSHIFUJI Hideaki clock_t expires = 0; 26824bed72e4SYOSHIFUJI Hideaki int flags = RTF_ADDRCONF | RTF_PREFIX_RT; 26834bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(rt_expires)) { 26846f704992SYOSHIFUJI Hideaki /* not infinity */ 26856f704992SYOSHIFUJI Hideaki flags |= RTF_EXPIRES; 26866f704992SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(rt_expires); 26876f704992SYOSHIFUJI Hideaki } 26881da177e4SLinus Torvalds addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len, 26898308f3ffSDavid Ahern 0, dev, expires, flags, 26908308f3ffSDavid Ahern GFP_ATOMIC); 26911da177e4SLinus Torvalds } 269293531c67SDavid Ahern fib6_info_release(rt); 26931da177e4SLinus Torvalds } 26941da177e4SLinus Torvalds 26951da177e4SLinus Torvalds /* Try to figure out our local address for this prefix */ 26961da177e4SLinus Torvalds 26971da177e4SLinus Torvalds if (pinfo->autoconf && in6_dev->cnf.autoconf) { 26981da177e4SLinus Torvalds struct in6_addr addr; 2699f997c55cSAlexander Aring bool tokenized = false, dev_addr_generated = false; 27001da177e4SLinus Torvalds 27011da177e4SLinus Torvalds if (pinfo->prefix_len == 64) { 27021da177e4SLinus Torvalds memcpy(&addr, &pinfo->prefix, 8); 2703f53adae4SDaniel Borkmann 2704f53adae4SDaniel Borkmann if (!ipv6_addr_any(&in6_dev->token)) { 2705f53adae4SDaniel Borkmann read_lock_bh(&in6_dev->lock); 2706f53adae4SDaniel Borkmann memcpy(addr.s6_addr + 8, 2707f53adae4SDaniel Borkmann in6_dev->token.s6_addr + 8, 8); 2708f53adae4SDaniel Borkmann read_unlock_bh(&in6_dev->lock); 2709617fe29dSDaniel Borkmann tokenized = true; 2710cc9da6ccSBjørn Mork } else if (is_addr_mode_generate_stable(in6_dev) && 2711622c81d5SHannes Frederic Sowa !ipv6_generate_stable_address(&addr, 0, 2712622c81d5SHannes Frederic Sowa in6_dev)) { 271364236f3fSHannes Frederic Sowa addr_flags |= IFA_F_STABLE_PRIVACY; 2714622c81d5SHannes Frederic Sowa goto ok; 2715f53adae4SDaniel Borkmann } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) && 27161da177e4SLinus Torvalds ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) { 27174f672235SAlexander Aring goto put; 2718f997c55cSAlexander Aring } else { 2719f997c55cSAlexander Aring dev_addr_generated = true; 27201da177e4SLinus Torvalds } 27211da177e4SLinus Torvalds goto ok; 27221da177e4SLinus Torvalds } 2723e87cc472SJoe Perches net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n", 27241da177e4SLinus Torvalds pinfo->prefix_len); 27254f672235SAlexander Aring goto put; 27261da177e4SLinus Torvalds 27271da177e4SLinus Torvalds ok: 27284f672235SAlexander Aring err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, 27294f672235SAlexander Aring &addr, addr_type, 27304f672235SAlexander Aring addr_flags, sllao, 27314f672235SAlexander Aring tokenized, valid_lft, 27328a226b2cSJiri Benc prefered_lft); 27334f672235SAlexander Aring if (err) 27344f672235SAlexander Aring goto put; 2735f997c55cSAlexander Aring 2736f997c55cSAlexander Aring /* Ignore error case here because previous prefix add addr was 2737f997c55cSAlexander Aring * successful which will be notified. 2738f997c55cSAlexander Aring */ 2739f997c55cSAlexander Aring ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr, 2740f997c55cSAlexander Aring addr_type, addr_flags, sllao, 2741f997c55cSAlexander Aring tokenized, valid_lft, 2742f997c55cSAlexander Aring prefered_lft, 2743f997c55cSAlexander Aring dev_addr_generated); 27441da177e4SLinus Torvalds } 27451da177e4SLinus Torvalds inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo); 27464f672235SAlexander Aring put: 27471da177e4SLinus Torvalds in6_dev_put(in6_dev); 27481da177e4SLinus Torvalds } 27491da177e4SLinus Torvalds 27501da177e4SLinus Torvalds /* 27511da177e4SLinus Torvalds * Set destination address. 27521da177e4SLinus Torvalds * Special case for SIT interfaces where we create a new "virtual" 27531da177e4SLinus Torvalds * device. 27541da177e4SLinus Torvalds */ 2755af284937SDaniel Lezcano int addrconf_set_dstaddr(struct net *net, void __user *arg) 27561da177e4SLinus Torvalds { 27571da177e4SLinus Torvalds struct in6_ifreq ireq; 27581da177e4SLinus Torvalds struct net_device *dev; 27591da177e4SLinus Torvalds int err = -EINVAL; 27601da177e4SLinus Torvalds 27611da177e4SLinus Torvalds rtnl_lock(); 27621da177e4SLinus Torvalds 27631da177e4SLinus Torvalds err = -EFAULT; 27641da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 27651da177e4SLinus Torvalds goto err_exit; 27661da177e4SLinus Torvalds 2767af284937SDaniel Lezcano dev = __dev_get_by_index(net, ireq.ifr6_ifindex); 27681da177e4SLinus Torvalds 27691da177e4SLinus Torvalds err = -ENODEV; 277063159f29SIan Morris if (!dev) 27711da177e4SLinus Torvalds goto err_exit; 27721da177e4SLinus Torvalds 277307a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 27741da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT) { 27755bc3eb7eSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 27761da177e4SLinus Torvalds struct ifreq ifr; 27771da177e4SLinus Torvalds struct ip_tunnel_parm p; 27781da177e4SLinus Torvalds 27791da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 27801da177e4SLinus Torvalds if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4)) 27811da177e4SLinus Torvalds goto err_exit; 27821da177e4SLinus Torvalds 27831da177e4SLinus Torvalds memset(&p, 0, sizeof(p)); 27841da177e4SLinus Torvalds p.iph.daddr = ireq.ifr6_addr.s6_addr32[3]; 27851da177e4SLinus Torvalds p.iph.saddr = 0; 27861da177e4SLinus Torvalds p.iph.version = 4; 27871da177e4SLinus Torvalds p.iph.ihl = 5; 27881da177e4SLinus Torvalds p.iph.protocol = IPPROTO_IPV6; 27891da177e4SLinus Torvalds p.iph.ttl = 64; 2790d20b3109SStephen Hemminger ifr.ifr_ifru.ifru_data = (__force void __user *)&p; 27911da177e4SLinus Torvalds 27925bc3eb7eSStephen Hemminger if (ops->ndo_do_ioctl) { 27935bc3eb7eSStephen Hemminger mm_segment_t oldfs = get_fs(); 27945bc3eb7eSStephen Hemminger 27955bc3eb7eSStephen Hemminger set_fs(KERNEL_DS); 27965bc3eb7eSStephen Hemminger err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL); 27971da177e4SLinus Torvalds set_fs(oldfs); 27985bc3eb7eSStephen Hemminger } else 27995bc3eb7eSStephen Hemminger err = -EOPNOTSUPP; 28001da177e4SLinus Torvalds 28011da177e4SLinus Torvalds if (err == 0) { 28021da177e4SLinus Torvalds err = -ENOBUFS; 2803af284937SDaniel Lezcano dev = __dev_get_by_name(net, p.name); 2804af284937SDaniel Lezcano if (!dev) 28051da177e4SLinus Torvalds goto err_exit; 28061da177e4SLinus Torvalds err = dev_open(dev); 28071da177e4SLinus Torvalds } 28081da177e4SLinus Torvalds } 28090be669bbSJoerg Roedel #endif 28101da177e4SLinus Torvalds 28111da177e4SLinus Torvalds err_exit: 28121da177e4SLinus Torvalds rtnl_unlock(); 28131da177e4SLinus Torvalds return err; 28141da177e4SLinus Torvalds } 28151da177e4SLinus Torvalds 281693a714d6SMadhu Challa static int ipv6_mc_config(struct sock *sk, bool join, 281793a714d6SMadhu Challa const struct in6_addr *addr, int ifindex) 281893a714d6SMadhu Challa { 281993a714d6SMadhu Challa int ret; 282093a714d6SMadhu Challa 282193a714d6SMadhu Challa ASSERT_RTNL(); 282293a714d6SMadhu Challa 282393a714d6SMadhu Challa lock_sock(sk); 282493a714d6SMadhu Challa if (join) 282554ff9ef3SMarcelo Ricardo Leitner ret = ipv6_sock_mc_join(sk, ifindex, addr); 282693a714d6SMadhu Challa else 282754ff9ef3SMarcelo Ricardo Leitner ret = ipv6_sock_mc_drop(sk, ifindex, addr); 282893a714d6SMadhu Challa release_sock(sk); 282993a714d6SMadhu Challa 283093a714d6SMadhu Challa return ret; 283193a714d6SMadhu Challa } 283293a714d6SMadhu Challa 28331da177e4SLinus Torvalds /* 28341da177e4SLinus Torvalds * Manual configuration of address on an interface 28351da177e4SLinus Torvalds */ 2836479840ffSJiri Pirko static int inet6_addr_add(struct net *net, int ifindex, 283719b1518cSDavid Ahern struct ifa6_config *cfg, 2838de95e047SDavid Ahern struct netlink_ext_ack *extack) 28391da177e4SLinus Torvalds { 28401da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 28411da177e4SLinus Torvalds struct inet6_dev *idev; 28421da177e4SLinus Torvalds struct net_device *dev; 284393a714d6SMadhu Challa unsigned long timeout; 284493a714d6SMadhu Challa clock_t expires; 28456f704992SYOSHIFUJI Hideaki u32 flags; 28461da177e4SLinus Torvalds 28471da177e4SLinus Torvalds ASSERT_RTNL(); 28481da177e4SLinus Torvalds 284919b1518cSDavid Ahern if (cfg->plen > 128) 285024ef0da7SThomas Graf return -EINVAL; 285124ef0da7SThomas Graf 28520778769dSNoriaki TAKAMIYA /* check the lifetime */ 285319b1518cSDavid Ahern if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft) 28540778769dSNoriaki TAKAMIYA return -EINVAL; 28550778769dSNoriaki TAKAMIYA 285619b1518cSDavid Ahern if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR && cfg->plen != 64) 285753bd6749SJiri Pirko return -EINVAL; 285853bd6749SJiri Pirko 2859af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifindex); 2860af284937SDaniel Lezcano if (!dev) 28611da177e4SLinus Torvalds return -ENODEV; 28621da177e4SLinus Torvalds 286364e724f6SBrian Haley idev = addrconf_add_dev(dev); 286464e724f6SBrian Haley if (IS_ERR(idev)) 286564e724f6SBrian Haley return PTR_ERR(idev); 28661da177e4SLinus Torvalds 286719b1518cSDavid Ahern if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) { 286893a714d6SMadhu Challa int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk, 286919b1518cSDavid Ahern true, cfg->pfx, ifindex); 287093a714d6SMadhu Challa 287193a714d6SMadhu Challa if (ret < 0) 287293a714d6SMadhu Challa return ret; 287393a714d6SMadhu Challa } 287493a714d6SMadhu Challa 287519b1518cSDavid Ahern cfg->scope = ipv6_addr_scope(cfg->pfx); 28761da177e4SLinus Torvalds 287719b1518cSDavid Ahern timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ); 28784bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 28794bed72e4SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(timeout * HZ); 288019b1518cSDavid Ahern cfg->valid_lft = timeout; 28816f704992SYOSHIFUJI Hideaki flags = RTF_EXPIRES; 28824bed72e4SYOSHIFUJI Hideaki } else { 28834bed72e4SYOSHIFUJI Hideaki expires = 0; 28844bed72e4SYOSHIFUJI Hideaki flags = 0; 288519b1518cSDavid Ahern cfg->ifa_flags |= IFA_F_PERMANENT; 28866f704992SYOSHIFUJI Hideaki } 28870778769dSNoriaki TAKAMIYA 288819b1518cSDavid Ahern timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ); 28894bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 28904bed72e4SYOSHIFUJI Hideaki if (timeout == 0) 289119b1518cSDavid Ahern cfg->ifa_flags |= IFA_F_DEPRECATED; 289219b1518cSDavid Ahern cfg->preferred_lft = timeout; 28934bed72e4SYOSHIFUJI Hideaki } 28940778769dSNoriaki TAKAMIYA 289519b1518cSDavid Ahern ifp = ipv6_add_addr(idev, cfg, true, extack); 28961da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 289719b1518cSDavid Ahern if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) { 28988308f3ffSDavid Ahern addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 28998308f3ffSDavid Ahern ifp->rt_priority, dev, expires, 29008308f3ffSDavid Ahern flags, GFP_KERNEL); 2901761aac73SThomas Haller } 2902761aac73SThomas Haller 2903a2d481b3SLorenzo Bianconi /* Send a netlink notification if DAD is enabled and 2904a2d481b3SLorenzo Bianconi * optimistic flag is not set 2905a2d481b3SLorenzo Bianconi */ 2906a2d481b3SLorenzo Bianconi if (!(ifp->flags & (IFA_F_OPTIMISTIC | IFA_F_NODAD))) 2907a2d481b3SLorenzo Bianconi ipv6_ifa_notify(0, ifp); 290895c385b4SNeil Horman /* 290995c385b4SNeil Horman * Note that section 3.1 of RFC 4429 indicates 291095c385b4SNeil Horman * that the Optimistic flag should not be set for 291195c385b4SNeil Horman * manually configured addresses 291295c385b4SNeil Horman */ 2913cf22f9a2SDavid S. Miller addrconf_dad_start(ifp); 291419b1518cSDavid Ahern if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR) 291519b1518cSDavid Ahern manage_tempaddrs(idev, ifp, cfg->valid_lft, 291619b1518cSDavid Ahern cfg->preferred_lft, true, jiffies); 29171da177e4SLinus Torvalds in6_ifa_put(ifp); 2918c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 29191da177e4SLinus Torvalds return 0; 292019b1518cSDavid Ahern } else if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) { 292119b1518cSDavid Ahern ipv6_mc_config(net->ipv6.mc_autojoin_sk, false, 292219b1518cSDavid Ahern cfg->pfx, ifindex); 29231da177e4SLinus Torvalds } 29241da177e4SLinus Torvalds 29251da177e4SLinus Torvalds return PTR_ERR(ifp); 29261da177e4SLinus Torvalds } 29271da177e4SLinus Torvalds 29286046d5b4SHeiner Kallweit static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags, 29296046d5b4SHeiner Kallweit const struct in6_addr *pfx, unsigned int plen) 29301da177e4SLinus Torvalds { 29311da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 29321da177e4SLinus Torvalds struct inet6_dev *idev; 29331da177e4SLinus Torvalds struct net_device *dev; 29341da177e4SLinus Torvalds 293524ef0da7SThomas Graf if (plen > 128) 293624ef0da7SThomas Graf return -EINVAL; 293724ef0da7SThomas Graf 2938af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifindex); 2939af284937SDaniel Lezcano if (!dev) 29401da177e4SLinus Torvalds return -ENODEV; 29411da177e4SLinus Torvalds 2942e5d08d71SIan Morris idev = __in6_dev_get(dev); 294363159f29SIan Morris if (!idev) 29441da177e4SLinus Torvalds return -ENXIO; 29451da177e4SLinus Torvalds 29461da177e4SLinus Torvalds read_lock_bh(&idev->lock); 2947502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 29481da177e4SLinus Torvalds if (ifp->prefix_len == plen && 29491da177e4SLinus Torvalds ipv6_addr_equal(pfx, &ifp->addr)) { 29501da177e4SLinus Torvalds in6_ifa_hold(ifp); 29511da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 29521da177e4SLinus Torvalds 29536046d5b4SHeiner Kallweit if (!(ifp->flags & IFA_F_TEMPORARY) && 29546046d5b4SHeiner Kallweit (ifa_flags & IFA_F_MANAGETEMPADDR)) 29556046d5b4SHeiner Kallweit manage_tempaddrs(idev, ifp, 0, 0, false, 29566046d5b4SHeiner Kallweit jiffies); 29571da177e4SLinus Torvalds ipv6_del_addr(ifp); 29586046d5b4SHeiner Kallweit addrconf_verify_rtnl(); 295993a714d6SMadhu Challa if (ipv6_addr_is_multicast(pfx)) { 296093a714d6SMadhu Challa ipv6_mc_config(net->ipv6.mc_autojoin_sk, 296193a714d6SMadhu Challa false, pfx, dev->ifindex); 296293a714d6SMadhu Challa } 29631da177e4SLinus Torvalds return 0; 29641da177e4SLinus Torvalds } 29651da177e4SLinus Torvalds } 29661da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 29671da177e4SLinus Torvalds return -EADDRNOTAVAIL; 29681da177e4SLinus Torvalds } 29691da177e4SLinus Torvalds 29701da177e4SLinus Torvalds 2971af284937SDaniel Lezcano int addrconf_add_ifaddr(struct net *net, void __user *arg) 29721da177e4SLinus Torvalds { 297319b1518cSDavid Ahern struct ifa6_config cfg = { 297419b1518cSDavid Ahern .ifa_flags = IFA_F_PERMANENT, 297519b1518cSDavid Ahern .preferred_lft = INFINITY_LIFE_TIME, 297619b1518cSDavid Ahern .valid_lft = INFINITY_LIFE_TIME, 297719b1518cSDavid Ahern }; 29781da177e4SLinus Torvalds struct in6_ifreq ireq; 29791da177e4SLinus Torvalds int err; 29801da177e4SLinus Torvalds 2981af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 29821da177e4SLinus Torvalds return -EPERM; 29831da177e4SLinus Torvalds 29841da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 29851da177e4SLinus Torvalds return -EFAULT; 29861da177e4SLinus Torvalds 298719b1518cSDavid Ahern cfg.pfx = &ireq.ifr6_addr; 298819b1518cSDavid Ahern cfg.plen = ireq.ifr6_prefixlen; 298919b1518cSDavid Ahern 29901da177e4SLinus Torvalds rtnl_lock(); 299119b1518cSDavid Ahern err = inet6_addr_add(net, ireq.ifr6_ifindex, &cfg, NULL); 29921da177e4SLinus Torvalds rtnl_unlock(); 29931da177e4SLinus Torvalds return err; 29941da177e4SLinus Torvalds } 29951da177e4SLinus Torvalds 2996af284937SDaniel Lezcano int addrconf_del_ifaddr(struct net *net, void __user *arg) 29971da177e4SLinus Torvalds { 29981da177e4SLinus Torvalds struct in6_ifreq ireq; 29991da177e4SLinus Torvalds int err; 30001da177e4SLinus Torvalds 3001af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 30021da177e4SLinus Torvalds return -EPERM; 30031da177e4SLinus Torvalds 30041da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 30051da177e4SLinus Torvalds return -EFAULT; 30061da177e4SLinus Torvalds 30071da177e4SLinus Torvalds rtnl_lock(); 30086046d5b4SHeiner Kallweit err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr, 3009af284937SDaniel Lezcano ireq.ifr6_prefixlen); 30101da177e4SLinus Torvalds rtnl_unlock(); 30111da177e4SLinus Torvalds return err; 30121da177e4SLinus Torvalds } 30131da177e4SLinus Torvalds 3014b5f348e5SIlpo Järvinen static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr, 3015b5f348e5SIlpo Järvinen int plen, int scope) 3016b5f348e5SIlpo Järvinen { 3017b5f348e5SIlpo Järvinen struct inet6_ifaddr *ifp; 3018e6464b8cSDavid Ahern struct ifa6_config cfg = { 3019e6464b8cSDavid Ahern .pfx = addr, 3020e6464b8cSDavid Ahern .plen = plen, 3021e6464b8cSDavid Ahern .ifa_flags = IFA_F_PERMANENT, 3022e6464b8cSDavid Ahern .valid_lft = INFINITY_LIFE_TIME, 3023e6464b8cSDavid Ahern .preferred_lft = INFINITY_LIFE_TIME, 3024e6464b8cSDavid Ahern .scope = scope 3025e6464b8cSDavid Ahern }; 3026b5f348e5SIlpo Järvinen 3027e6464b8cSDavid Ahern ifp = ipv6_add_addr(idev, &cfg, true, NULL); 3028b5f348e5SIlpo Järvinen if (!IS_ERR(ifp)) { 3029b5f348e5SIlpo Järvinen spin_lock_bh(&ifp->lock); 3030b5f348e5SIlpo Järvinen ifp->flags &= ~IFA_F_TENTATIVE; 3031b5f348e5SIlpo Järvinen spin_unlock_bh(&ifp->lock); 3032764d3be6SPaolo Abeni rt_genid_bump_ipv6(dev_net(idev->dev)); 3033b5f348e5SIlpo Järvinen ipv6_ifa_notify(RTM_NEWADDR, ifp); 3034b5f348e5SIlpo Järvinen in6_ifa_put(ifp); 3035b5f348e5SIlpo Järvinen } 3036b5f348e5SIlpo Järvinen } 3037b5f348e5SIlpo Järvinen 303807a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 30391da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev) 30401da177e4SLinus Torvalds { 30411da177e4SLinus Torvalds struct in6_addr addr; 30421da177e4SLinus Torvalds struct net_device *dev; 3043c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 3044929c9cf3SNicolas Dichtel int scope, plen; 3045f0e2acfaSNicolas Dichtel u32 pflags = 0; 30461da177e4SLinus Torvalds 30471da177e4SLinus Torvalds ASSERT_RTNL(); 30481da177e4SLinus Torvalds 30491da177e4SLinus Torvalds memset(&addr, 0, sizeof(struct in6_addr)); 30501da177e4SLinus Torvalds memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4); 30511da177e4SLinus Torvalds 30521da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) { 30531da177e4SLinus Torvalds addr.s6_addr32[0] = htonl(0xfe800000); 30541da177e4SLinus Torvalds scope = IFA_LINK; 3055929c9cf3SNicolas Dichtel plen = 64; 30561da177e4SLinus Torvalds } else { 30571da177e4SLinus Torvalds scope = IPV6_ADDR_COMPATv4; 3058929c9cf3SNicolas Dichtel plen = 96; 3059f0e2acfaSNicolas Dichtel pflags |= RTF_NONEXTHOP; 30601da177e4SLinus Torvalds } 30611da177e4SLinus Torvalds 30621da177e4SLinus Torvalds if (addr.s6_addr32[3]) { 3063929c9cf3SNicolas Dichtel add_addr(idev, &addr, plen, scope); 30648308f3ffSDavid Ahern addrconf_prefix_route(&addr, plen, 0, idev->dev, 0, pflags, 3065e500c6d3SCong Wang GFP_KERNEL); 30661da177e4SLinus Torvalds return; 30671da177e4SLinus Torvalds } 30681da177e4SLinus Torvalds 30696fda7350SBenjamin Thery for_each_netdev(net, dev) { 3070e5ed6399SHerbert Xu struct in_device *in_dev = __in_dev_get_rtnl(dev); 30711da177e4SLinus Torvalds if (in_dev && (dev->flags & IFF_UP)) { 30721da177e4SLinus Torvalds struct in_ifaddr *ifa; 30731da177e4SLinus Torvalds 30741da177e4SLinus Torvalds int flag = scope; 30751da177e4SLinus Torvalds 30761da177e4SLinus Torvalds for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) { 30771da177e4SLinus Torvalds 30781da177e4SLinus Torvalds addr.s6_addr32[3] = ifa->ifa_local; 30791da177e4SLinus Torvalds 30801da177e4SLinus Torvalds if (ifa->ifa_scope == RT_SCOPE_LINK) 30811da177e4SLinus Torvalds continue; 30821da177e4SLinus Torvalds if (ifa->ifa_scope >= RT_SCOPE_HOST) { 30831da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) 30841da177e4SLinus Torvalds continue; 30851da177e4SLinus Torvalds flag |= IFA_HOST; 30861da177e4SLinus Torvalds } 30871da177e4SLinus Torvalds 3088b5f348e5SIlpo Järvinen add_addr(idev, &addr, plen, flag); 30898308f3ffSDavid Ahern addrconf_prefix_route(&addr, plen, 0, idev->dev, 3090e500c6d3SCong Wang 0, pflags, GFP_KERNEL); 30911da177e4SLinus Torvalds } 30921da177e4SLinus Torvalds } 30931da177e4SLinus Torvalds } 30941da177e4SLinus Torvalds } 30950be669bbSJoerg Roedel #endif 30961da177e4SLinus Torvalds 30971da177e4SLinus Torvalds static void init_loopback(struct net_device *dev) 30981da177e4SLinus Torvalds { 30991da177e4SLinus Torvalds struct inet6_dev *idev; 31001da177e4SLinus Torvalds 31011da177e4SLinus Torvalds /* ::1 */ 31021da177e4SLinus Torvalds 31031da177e4SLinus Torvalds ASSERT_RTNL(); 31041da177e4SLinus Torvalds 3105e5d08d71SIan Morris idev = ipv6_find_idev(dev); 310663159f29SIan Morris if (!idev) { 310791df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 31081da177e4SLinus Torvalds return; 31091da177e4SLinus Torvalds } 31101da177e4SLinus Torvalds 3111b5f348e5SIlpo Järvinen add_addr(idev, &in6addr_loopback, 128, IFA_HOST); 31121da177e4SLinus Torvalds } 31131da177e4SLinus Torvalds 31142ad3ed59SAlexander Aring void addrconf_add_linklocal(struct inet6_dev *idev, 311564236f3fSHannes Frederic Sowa const struct in6_addr *addr, u32 flags) 31161da177e4SLinus Torvalds { 3117e6464b8cSDavid Ahern struct ifa6_config cfg = { 3118e6464b8cSDavid Ahern .pfx = addr, 3119e6464b8cSDavid Ahern .plen = 64, 3120e6464b8cSDavid Ahern .ifa_flags = flags | IFA_F_PERMANENT, 3121e6464b8cSDavid Ahern .valid_lft = INFINITY_LIFE_TIME, 3122e6464b8cSDavid Ahern .preferred_lft = INFINITY_LIFE_TIME, 3123e6464b8cSDavid Ahern .scope = IFA_LINK 3124e6464b8cSDavid Ahern }; 31251da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 31261da177e4SLinus Torvalds 312795c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 312835e015e1SMatteo Croce if ((dev_net(idev->dev)->ipv6.devconf_all->optimistic_dad || 312935e015e1SMatteo Croce idev->cnf.optimistic_dad) && 3130702beb87SDavid Miller !dev_net(idev->dev)->ipv6.devconf_all->forwarding) 3131e6464b8cSDavid Ahern cfg.ifa_flags |= IFA_F_OPTIMISTIC; 313295c385b4SNeil Horman #endif 313395c385b4SNeil Horman 3134e6464b8cSDavid Ahern ifp = ipv6_add_addr(idev, &cfg, true, NULL); 31351da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 31368308f3ffSDavid Ahern addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 0, idev->dev, 3137acb54e3cSDavid Ahern 0, 0, GFP_ATOMIC); 3138cf22f9a2SDavid S. Miller addrconf_dad_start(ifp); 31391da177e4SLinus Torvalds in6_ifa_put(ifp); 31401da177e4SLinus Torvalds } 31411da177e4SLinus Torvalds } 31422ad3ed59SAlexander Aring EXPORT_SYMBOL_GPL(addrconf_add_linklocal); 31431da177e4SLinus Torvalds 3144622c81d5SHannes Frederic Sowa static bool ipv6_reserved_interfaceid(struct in6_addr address) 3145622c81d5SHannes Frederic Sowa { 3146622c81d5SHannes Frederic Sowa if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0) 3147622c81d5SHannes Frederic Sowa return true; 3148622c81d5SHannes Frederic Sowa 3149622c81d5SHannes Frederic Sowa if (address.s6_addr32[2] == htonl(0x02005eff) && 3150622c81d5SHannes Frederic Sowa ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000))) 3151622c81d5SHannes Frederic Sowa return true; 3152622c81d5SHannes Frederic Sowa 3153622c81d5SHannes Frederic Sowa if (address.s6_addr32[2] == htonl(0xfdffffff) && 3154622c81d5SHannes Frederic Sowa ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80))) 3155622c81d5SHannes Frederic Sowa return true; 3156622c81d5SHannes Frederic Sowa 3157622c81d5SHannes Frederic Sowa return false; 3158622c81d5SHannes Frederic Sowa } 3159622c81d5SHannes Frederic Sowa 3160622c81d5SHannes Frederic Sowa static int ipv6_generate_stable_address(struct in6_addr *address, 3161622c81d5SHannes Frederic Sowa u8 dad_count, 3162622c81d5SHannes Frederic Sowa const struct inet6_dev *idev) 3163622c81d5SHannes Frederic Sowa { 3164622c81d5SHannes Frederic Sowa static DEFINE_SPINLOCK(lock); 3165622c81d5SHannes Frederic Sowa static __u32 digest[SHA_DIGEST_WORDS]; 3166622c81d5SHannes Frederic Sowa static __u32 workspace[SHA_WORKSPACE_WORDS]; 3167622c81d5SHannes Frederic Sowa 3168622c81d5SHannes Frederic Sowa static union { 3169622c81d5SHannes Frederic Sowa char __data[SHA_MESSAGE_BYTES]; 3170622c81d5SHannes Frederic Sowa struct { 3171622c81d5SHannes Frederic Sowa struct in6_addr secret; 3172ff40217eSHannes Frederic Sowa __be32 prefix[2]; 3173622c81d5SHannes Frederic Sowa unsigned char hwaddr[MAX_ADDR_LEN]; 3174622c81d5SHannes Frederic Sowa u8 dad_count; 3175622c81d5SHannes Frederic Sowa } __packed; 3176622c81d5SHannes Frederic Sowa } data; 3177622c81d5SHannes Frederic Sowa 3178622c81d5SHannes Frederic Sowa struct in6_addr secret; 3179622c81d5SHannes Frederic Sowa struct in6_addr temp; 3180622c81d5SHannes Frederic Sowa struct net *net = dev_net(idev->dev); 3181622c81d5SHannes Frederic Sowa 3182622c81d5SHannes Frederic Sowa BUILD_BUG_ON(sizeof(data.__data) != sizeof(data)); 3183622c81d5SHannes Frederic Sowa 3184622c81d5SHannes Frederic Sowa if (idev->cnf.stable_secret.initialized) 3185622c81d5SHannes Frederic Sowa secret = idev->cnf.stable_secret.secret; 3186622c81d5SHannes Frederic Sowa else if (net->ipv6.devconf_dflt->stable_secret.initialized) 3187622c81d5SHannes Frederic Sowa secret = net->ipv6.devconf_dflt->stable_secret.secret; 3188622c81d5SHannes Frederic Sowa else 3189622c81d5SHannes Frederic Sowa return -1; 3190622c81d5SHannes Frederic Sowa 3191622c81d5SHannes Frederic Sowa retry: 3192622c81d5SHannes Frederic Sowa spin_lock_bh(&lock); 3193622c81d5SHannes Frederic Sowa 3194622c81d5SHannes Frederic Sowa sha_init(digest); 3195622c81d5SHannes Frederic Sowa memset(&data, 0, sizeof(data)); 3196622c81d5SHannes Frederic Sowa memset(workspace, 0, sizeof(workspace)); 3197622c81d5SHannes Frederic Sowa memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len); 3198ff40217eSHannes Frederic Sowa data.prefix[0] = address->s6_addr32[0]; 3199ff40217eSHannes Frederic Sowa data.prefix[1] = address->s6_addr32[1]; 3200622c81d5SHannes Frederic Sowa data.secret = secret; 3201622c81d5SHannes Frederic Sowa data.dad_count = dad_count; 3202622c81d5SHannes Frederic Sowa 3203622c81d5SHannes Frederic Sowa sha_transform(digest, data.__data, workspace); 3204622c81d5SHannes Frederic Sowa 3205622c81d5SHannes Frederic Sowa temp = *address; 3206ff40217eSHannes Frederic Sowa temp.s6_addr32[2] = (__force __be32)digest[0]; 3207ff40217eSHannes Frederic Sowa temp.s6_addr32[3] = (__force __be32)digest[1]; 3208622c81d5SHannes Frederic Sowa 3209622c81d5SHannes Frederic Sowa spin_unlock_bh(&lock); 3210622c81d5SHannes Frederic Sowa 3211622c81d5SHannes Frederic Sowa if (ipv6_reserved_interfaceid(temp)) { 3212622c81d5SHannes Frederic Sowa dad_count++; 32131855b7c3SHannes Frederic Sowa if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries) 3214622c81d5SHannes Frederic Sowa return -1; 3215622c81d5SHannes Frederic Sowa goto retry; 3216622c81d5SHannes Frederic Sowa } 3217622c81d5SHannes Frederic Sowa 3218622c81d5SHannes Frederic Sowa *address = temp; 3219622c81d5SHannes Frederic Sowa return 0; 3220622c81d5SHannes Frederic Sowa } 3221622c81d5SHannes Frederic Sowa 3222cc9da6ccSBjørn Mork static void ipv6_gen_mode_random_init(struct inet6_dev *idev) 3223cc9da6ccSBjørn Mork { 3224cc9da6ccSBjørn Mork struct ipv6_stable_secret *s = &idev->cnf.stable_secret; 3225cc9da6ccSBjørn Mork 3226cc9da6ccSBjørn Mork if (s->initialized) 3227cc9da6ccSBjørn Mork return; 3228cc9da6ccSBjørn Mork s = &idev->cnf.stable_secret; 3229cc9da6ccSBjørn Mork get_random_bytes(&s->secret, sizeof(s->secret)); 3230cc9da6ccSBjørn Mork s->initialized = true; 3231cc9da6ccSBjørn Mork } 3232cc9da6ccSBjørn Mork 3233bc91b0f0SJiri Pirko static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route) 3234bc91b0f0SJiri Pirko { 3235bc91b0f0SJiri Pirko struct in6_addr addr; 3236bc91b0f0SJiri Pirko 3237ca254490SDavid Ahern /* no link local addresses on L3 master devices */ 3238ca254490SDavid Ahern if (netif_is_l3_master(idev->dev)) 3239ca254490SDavid Ahern return; 3240ca254490SDavid Ahern 3241bc91b0f0SJiri Pirko ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0); 3242622c81d5SHannes Frederic Sowa 3243d35a00b8SFelix Jia switch (idev->cnf.addr_gen_mode) { 3244cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_RANDOM: 3245cc9da6ccSBjørn Mork ipv6_gen_mode_random_init(idev); 3246cc9da6ccSBjørn Mork /* fallthrough */ 3247cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_STABLE_PRIVACY: 3248622c81d5SHannes Frederic Sowa if (!ipv6_generate_stable_address(&addr, 0, idev)) 324964236f3fSHannes Frederic Sowa addrconf_add_linklocal(idev, &addr, 325064236f3fSHannes Frederic Sowa IFA_F_STABLE_PRIVACY); 3251622c81d5SHannes Frederic Sowa else if (prefix_route) 32528308f3ffSDavid Ahern addrconf_prefix_route(&addr, 64, 0, idev->dev, 3253acb54e3cSDavid Ahern 0, 0, GFP_KERNEL); 3254cc9da6ccSBjørn Mork break; 3255cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_EUI64: 3256bc91b0f0SJiri Pirko /* addrconf_add_linklocal also adds a prefix_route and we 3257bc91b0f0SJiri Pirko * only need to care about prefix routes if ipv6_generate_eui64 3258bc91b0f0SJiri Pirko * couldn't generate one. 3259bc91b0f0SJiri Pirko */ 3260bc91b0f0SJiri Pirko if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0) 326164236f3fSHannes Frederic Sowa addrconf_add_linklocal(idev, &addr, 0); 3262bc91b0f0SJiri Pirko else if (prefix_route) 32638308f3ffSDavid Ahern addrconf_prefix_route(&addr, 64, 0, idev->dev, 326427b10608SDavid Ahern 0, 0, GFP_KERNEL); 3265cc9da6ccSBjørn Mork break; 3266cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_NONE: 3267cc9da6ccSBjørn Mork default: 3268cc9da6ccSBjørn Mork /* will not add any link local address */ 3269cc9da6ccSBjørn Mork break; 3270bc91b0f0SJiri Pirko } 3271bc91b0f0SJiri Pirko } 3272bc91b0f0SJiri Pirko 32731da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev) 32741da177e4SLinus Torvalds { 32751da177e4SLinus Torvalds struct inet6_dev *idev; 32761da177e4SLinus Torvalds 32771da177e4SLinus Torvalds ASSERT_RTNL(); 32781da177e4SLinus Torvalds 327974235a25SHerbert Xu if ((dev->type != ARPHRD_ETHER) && 328074235a25SHerbert Xu (dev->type != ARPHRD_FDDI) && 328174235a25SHerbert Xu (dev->type != ARPHRD_ARCNET) && 328206a4c1c5Salex.bluesman.smirnov@gmail.com (dev->type != ARPHRD_INFINIBAND) && 3283e837735eSNicolas Dichtel (dev->type != ARPHRD_IEEE1394) && 3284e74bccb8SJukka Rissanen (dev->type != ARPHRD_TUNNEL6) && 3285cc9da6ccSBjørn Mork (dev->type != ARPHRD_6LOWPAN) && 328645ce0fd1SFelix Jia (dev->type != ARPHRD_IP6GRE) && 328745ce0fd1SFelix Jia (dev->type != ARPHRD_IPGRE) && 328845ce0fd1SFelix Jia (dev->type != ARPHRD_TUNNEL) && 32899deb441cSSubash Abhinov Kasiviswanathan (dev->type != ARPHRD_NONE) && 32909deb441cSSubash Abhinov Kasiviswanathan (dev->type != ARPHRD_RAWIP)) { 329174235a25SHerbert Xu /* Alas, we support only Ethernet autoconfiguration. */ 329274235a25SHerbert Xu return; 329374235a25SHerbert Xu } 329474235a25SHerbert Xu 32951da177e4SLinus Torvalds idev = addrconf_add_dev(dev); 329664e724f6SBrian Haley if (IS_ERR(idev)) 32971da177e4SLinus Torvalds return; 32981da177e4SLinus Torvalds 3299cc9da6ccSBjørn Mork /* this device type has no EUI support */ 3300cc9da6ccSBjørn Mork if (dev->type == ARPHRD_NONE && 3301d35a00b8SFelix Jia idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64) 3302d35a00b8SFelix Jia idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM; 3303cc9da6ccSBjørn Mork 3304bc91b0f0SJiri Pirko addrconf_addr_gen(idev, false); 33051da177e4SLinus Torvalds } 33061da177e4SLinus Torvalds 330707a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 33081da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev) 33091da177e4SLinus Torvalds { 33101da177e4SLinus Torvalds struct inet6_dev *idev; 33111da177e4SLinus Torvalds 33121da177e4SLinus Torvalds ASSERT_RTNL(); 33131da177e4SLinus Torvalds 33141da177e4SLinus Torvalds /* 33151da177e4SLinus Torvalds * Configure the tunnel with one of our IPv4 33161da177e4SLinus Torvalds * addresses... we should configure all of 33171da177e4SLinus Torvalds * our v4 addrs in the tunnel 33181da177e4SLinus Torvalds */ 33191da177e4SLinus Torvalds 3320e5d08d71SIan Morris idev = ipv6_find_idev(dev); 332163159f29SIan Morris if (!idev) { 332291df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 33231da177e4SLinus Torvalds return; 33241da177e4SLinus Torvalds } 33251da177e4SLinus Torvalds 3326c7dc89c0SFred L. Templin if (dev->priv_flags & IFF_ISATAP) { 3327bc91b0f0SJiri Pirko addrconf_addr_gen(idev, false); 3328c7dc89c0SFred L. Templin return; 3329c7dc89c0SFred L. Templin } 3330c7dc89c0SFred L. Templin 33311da177e4SLinus Torvalds sit_add_v4_addrs(idev); 33321da177e4SLinus Torvalds 333362b54dd9SNicolas Dichtel if (dev->flags&IFF_POINTOPOINT) 33341da177e4SLinus Torvalds addrconf_add_mroute(dev); 33351da177e4SLinus Torvalds } 33360be669bbSJoerg Roedel #endif 33371da177e4SLinus Torvalds 333807a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE) 3339aee80b54Sstephen hemminger static void addrconf_gre_config(struct net_device *dev) 3340aee80b54Sstephen hemminger { 3341aee80b54Sstephen hemminger struct inet6_dev *idev; 3342aee80b54Sstephen hemminger 3343aee80b54Sstephen hemminger ASSERT_RTNL(); 3344aee80b54Sstephen hemminger 3345e5d08d71SIan Morris idev = ipv6_find_idev(dev); 334663159f29SIan Morris if (!idev) { 334791df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 3348aee80b54Sstephen hemminger return; 3349aee80b54Sstephen hemminger } 3350aee80b54Sstephen hemminger 3351bc91b0f0SJiri Pirko addrconf_addr_gen(idev, true); 3352d9e4ce65SHannes Frederic Sowa if (dev->flags & IFF_POINTOPOINT) 3353d9e4ce65SHannes Frederic Sowa addrconf_add_mroute(dev); 3354aee80b54Sstephen hemminger } 3355aee80b54Sstephen hemminger #endif 3356aee80b54Sstephen hemminger 3357afb1d4b5SDavid Ahern static int fixup_permanent_addr(struct net *net, 3358afb1d4b5SDavid Ahern struct inet6_dev *idev, 3359f1705ec1SDavid Ahern struct inet6_ifaddr *ifp) 3360f1705ec1SDavid Ahern { 336193c2fb25SDavid Ahern /* !fib6_node means the host route was removed from the 33628048ced9SDavid Ahern * FIB, for example, if 'lo' device is taken down. In that 33638048ced9SDavid Ahern * case regenerate the host route. 33648048ced9SDavid Ahern */ 336593c2fb25SDavid Ahern if (!ifp->rt || !ifp->rt->fib6_node) { 3366360a9887SDavid Ahern struct fib6_info *f6i, *prev; 3367f1705ec1SDavid Ahern 3368360a9887SDavid Ahern f6i = addrconf_f6i_alloc(net, idev, &ifp->addr, false, 3369acb54e3cSDavid Ahern GFP_ATOMIC); 3370360a9887SDavid Ahern if (IS_ERR(f6i)) 3371360a9887SDavid Ahern return PTR_ERR(f6i); 3372f1705ec1SDavid Ahern 33738048ced9SDavid Ahern /* ifp->rt can be accessed outside of rtnl */ 33748048ced9SDavid Ahern spin_lock(&ifp->lock); 33758048ced9SDavid Ahern prev = ifp->rt; 3376360a9887SDavid Ahern ifp->rt = f6i; 33778048ced9SDavid Ahern spin_unlock(&ifp->lock); 33788048ced9SDavid Ahern 337993531c67SDavid Ahern fib6_info_release(prev); 3380f1705ec1SDavid Ahern } 3381f1705ec1SDavid Ahern 3382f1705ec1SDavid Ahern if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) { 3383f1705ec1SDavid Ahern addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 33848308f3ffSDavid Ahern ifp->rt_priority, idev->dev, 0, 0, 33858308f3ffSDavid Ahern GFP_ATOMIC); 3386f1705ec1SDavid Ahern } 3387f1705ec1SDavid Ahern 33886d717134SDavid Ahern if (ifp->state == INET6_IFADDR_STATE_PREDAD) 3389f1705ec1SDavid Ahern addrconf_dad_start(ifp); 3390f1705ec1SDavid Ahern 3391f1705ec1SDavid Ahern return 0; 3392f1705ec1SDavid Ahern } 3393f1705ec1SDavid Ahern 3394afb1d4b5SDavid Ahern static void addrconf_permanent_addr(struct net *net, struct net_device *dev) 3395f1705ec1SDavid Ahern { 3396f1705ec1SDavid Ahern struct inet6_ifaddr *ifp, *tmp; 3397f1705ec1SDavid Ahern struct inet6_dev *idev; 3398f1705ec1SDavid Ahern 3399f1705ec1SDavid Ahern idev = __in6_dev_get(dev); 3400f1705ec1SDavid Ahern if (!idev) 3401f1705ec1SDavid Ahern return; 3402f1705ec1SDavid Ahern 3403f1705ec1SDavid Ahern write_lock_bh(&idev->lock); 3404f1705ec1SDavid Ahern 3405f1705ec1SDavid Ahern list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) { 3406f1705ec1SDavid Ahern if ((ifp->flags & IFA_F_PERMANENT) && 3407afb1d4b5SDavid Ahern fixup_permanent_addr(net, idev, ifp) < 0) { 3408f1705ec1SDavid Ahern write_unlock_bh(&idev->lock); 3409e669b869SEric Dumazet in6_ifa_hold(ifp); 3410f1705ec1SDavid Ahern ipv6_del_addr(ifp); 3411f1705ec1SDavid Ahern write_lock_bh(&idev->lock); 3412f1705ec1SDavid Ahern 3413f1705ec1SDavid Ahern net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n", 3414f1705ec1SDavid Ahern idev->dev->name, &ifp->addr); 3415f1705ec1SDavid Ahern } 3416f1705ec1SDavid Ahern } 3417f1705ec1SDavid Ahern 3418f1705ec1SDavid Ahern write_unlock_bh(&idev->lock); 3419f1705ec1SDavid Ahern } 3420f1705ec1SDavid Ahern 34211da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event, 3422351638e7SJiri Pirko void *ptr) 34231da177e4SLinus Torvalds { 3424351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 34254f7f34eaSDavid Ahern struct netdev_notifier_changeupper_info *info; 3426748e2d93SEric Dumazet struct inet6_dev *idev = __in6_dev_get(dev); 342760abc0beSWANG Cong struct net *net = dev_net(dev); 3428c5e33bddSYOSHIFUJI Hideaki int run_pending = 0; 3429b217d616SHerbert Xu int err; 34301da177e4SLinus Torvalds 34311da177e4SLinus Torvalds switch (event) { 343245ba9dd2SYOSHIFUJI Hideaki case NETDEV_REGISTER: 343374235a25SHerbert Xu if (!idev && dev->mtu >= IPV6_MIN_MTU) { 343445ba9dd2SYOSHIFUJI Hideaki idev = ipv6_add_dev(dev); 3435a317a2f1SWANG Cong if (IS_ERR(idev)) 3436a317a2f1SWANG Cong return notifier_from_errno(PTR_ERR(idev)); 343745ba9dd2SYOSHIFUJI Hideaki } 343845ba9dd2SYOSHIFUJI Hideaki break; 3439bcdd553fSStephen Hemminger 3440b7b0b1d2SAlexander Duyck case NETDEV_CHANGEMTU: 3441b7b0b1d2SAlexander Duyck /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */ 3442b7b0b1d2SAlexander Duyck if (dev->mtu < IPV6_MIN_MTU) { 344360abc0beSWANG Cong addrconf_ifdown(dev, dev != net->loopback_dev); 3444b7b0b1d2SAlexander Duyck break; 3445b7b0b1d2SAlexander Duyck } 3446b7b0b1d2SAlexander Duyck 3447b7b0b1d2SAlexander Duyck if (idev) { 3448b7b0b1d2SAlexander Duyck rt6_mtu_change(dev, dev->mtu); 3449b7b0b1d2SAlexander Duyck idev->cnf.mtu6 = dev->mtu; 3450b7b0b1d2SAlexander Duyck break; 3451b7b0b1d2SAlexander Duyck } 3452b7b0b1d2SAlexander Duyck 3453b7b0b1d2SAlexander Duyck /* allocate new idev */ 3454b7b0b1d2SAlexander Duyck idev = ipv6_add_dev(dev); 3455b7b0b1d2SAlexander Duyck if (IS_ERR(idev)) 3456b7b0b1d2SAlexander Duyck break; 3457b7b0b1d2SAlexander Duyck 3458b7b0b1d2SAlexander Duyck /* device is still not ready */ 3459b7b0b1d2SAlexander Duyck if (!(idev->if_flags & IF_READY)) 3460b7b0b1d2SAlexander Duyck break; 3461b7b0b1d2SAlexander Duyck 3462b7b0b1d2SAlexander Duyck run_pending = 1; 3463b7b0b1d2SAlexander Duyck 3464b7b0b1d2SAlexander Duyck /* fall through */ 3465b7b0b1d2SAlexander Duyck 34661da177e4SLinus Torvalds case NETDEV_UP: 34673c21edbdSYOSHIFUJI Hideaki case NETDEV_CHANGE: 3468c2edacf8SJay Vosburgh if (dev->flags & IFF_SLAVE) 3469c2edacf8SJay Vosburgh break; 3470c2edacf8SJay Vosburgh 34713ce62a84SWANG Cong if (idev && idev->cnf.disable_ipv6) 34723ce62a84SWANG Cong break; 34733ce62a84SWANG Cong 34743c21edbdSYOSHIFUJI Hideaki if (event == NETDEV_UP) { 347538bd10c4SDavid Ahern /* restore routes for permanent addresses */ 3476afb1d4b5SDavid Ahern addrconf_permanent_addr(net, dev); 347738bd10c4SDavid Ahern 34781f372c7bSMike Manning if (!addrconf_link_ready(dev)) { 34793c21edbdSYOSHIFUJI Hideaki /* device is not ready yet. */ 3480f3213831SJoe Perches pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n", 34813c21edbdSYOSHIFUJI Hideaki dev->name); 34823c21edbdSYOSHIFUJI Hideaki break; 34833c21edbdSYOSHIFUJI Hideaki } 348499081049SKristian Slavov 3485d31c7b8fSEvgeniy Polyakov if (!idev && dev->mtu >= IPV6_MIN_MTU) 3486d31c7b8fSEvgeniy Polyakov idev = ipv6_add_dev(dev); 3487d31c7b8fSEvgeniy Polyakov 3488a317a2f1SWANG Cong if (!IS_ERR_OR_NULL(idev)) { 348999081049SKristian Slavov idev->if_flags |= IF_READY; 3490e3ec6cfcSBenjamin Thery run_pending = 1; 3491e3ec6cfcSBenjamin Thery } 3492b7b0b1d2SAlexander Duyck } else if (event == NETDEV_CHANGE) { 34931f372c7bSMike Manning if (!addrconf_link_ready(dev)) { 34943c21edbdSYOSHIFUJI Hideaki /* device is still not ready. */ 349527c6fa73SIdo Schimmel rt6_sync_down_dev(dev, event); 34963c21edbdSYOSHIFUJI Hideaki break; 34973c21edbdSYOSHIFUJI Hideaki } 34983c21edbdSYOSHIFUJI Hideaki 34993c21edbdSYOSHIFUJI Hideaki if (idev) { 3500a088d1d7SLinus Lüssing if (idev->if_flags & IF_READY) { 3501a088d1d7SLinus Lüssing /* device is already configured - 3502a088d1d7SLinus Lüssing * but resend MLD reports, we might 3503a088d1d7SLinus Lüssing * have roamed and need to update 3504a088d1d7SLinus Lüssing * multicast snooping switches 3505a088d1d7SLinus Lüssing */ 3506a088d1d7SLinus Lüssing ipv6_mc_up(idev); 350727c6fa73SIdo Schimmel rt6_sync_up(dev, RTNH_F_LINKDOWN); 35083c21edbdSYOSHIFUJI Hideaki break; 3509a088d1d7SLinus Lüssing } 35103c21edbdSYOSHIFUJI Hideaki idev->if_flags |= IF_READY; 35113c21edbdSYOSHIFUJI Hideaki } 35123c21edbdSYOSHIFUJI Hideaki 3513f3213831SJoe Perches pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n", 35143c21edbdSYOSHIFUJI Hideaki dev->name); 35153c21edbdSYOSHIFUJI Hideaki 3516c5e33bddSYOSHIFUJI Hideaki run_pending = 1; 35173c21edbdSYOSHIFUJI Hideaki } 35183c21edbdSYOSHIFUJI Hideaki 35191da177e4SLinus Torvalds switch (dev->type) { 352007a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 35211da177e4SLinus Torvalds case ARPHRD_SIT: 35221da177e4SLinus Torvalds addrconf_sit_config(dev); 35231da177e4SLinus Torvalds break; 35240be669bbSJoerg Roedel #endif 352507a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE) 3526aee80b54Sstephen hemminger case ARPHRD_IPGRE: 3527aee80b54Sstephen hemminger addrconf_gre_config(dev); 3528aee80b54Sstephen hemminger break; 3529aee80b54Sstephen hemminger #endif 35301da177e4SLinus Torvalds case ARPHRD_LOOPBACK: 35311da177e4SLinus Torvalds init_loopback(dev); 35321da177e4SLinus Torvalds break; 35331da177e4SLinus Torvalds 35341da177e4SLinus Torvalds default: 35351da177e4SLinus Torvalds addrconf_dev_config(dev); 35361da177e4SLinus Torvalds break; 35373ff50b79SStephen Hemminger } 3538bcdd553fSStephen Hemminger 3539a317a2f1SWANG Cong if (!IS_ERR_OR_NULL(idev)) { 3540c5e33bddSYOSHIFUJI Hideaki if (run_pending) 3541c5e33bddSYOSHIFUJI Hideaki addrconf_dad_run(idev); 3542c5e33bddSYOSHIFUJI Hideaki 35432127d95aSIdo Schimmel /* Device has an address by now */ 35442127d95aSIdo Schimmel rt6_sync_up(dev, RTNH_F_DEAD); 35452127d95aSIdo Schimmel 3546bcdd553fSStephen Hemminger /* 3547bcdd553fSStephen Hemminger * If the MTU changed during the interface down, 3548bcdd553fSStephen Hemminger * when the interface up, the changed MTU must be 3549bcdd553fSStephen Hemminger * reflected in the idev as well as routers. 35501da177e4SLinus Torvalds */ 3551bcdd553fSStephen Hemminger if (idev->cnf.mtu6 != dev->mtu && 3552bcdd553fSStephen Hemminger dev->mtu >= IPV6_MIN_MTU) { 35531da177e4SLinus Torvalds rt6_mtu_change(dev, dev->mtu); 35541da177e4SLinus Torvalds idev->cnf.mtu6 = dev->mtu; 35551da177e4SLinus Torvalds } 35561da177e4SLinus Torvalds idev->tstamp = jiffies; 35571da177e4SLinus Torvalds inet6_ifinfo_notify(RTM_NEWLINK, idev); 3558bcdd553fSStephen Hemminger 3559bcdd553fSStephen Hemminger /* 3560bcdd553fSStephen Hemminger * If the changed mtu during down is lower than 3561bcdd553fSStephen Hemminger * IPV6_MIN_MTU stop IPv6 on this interface. 35621da177e4SLinus Torvalds */ 35631da177e4SLinus Torvalds if (dev->mtu < IPV6_MIN_MTU) 356460abc0beSWANG Cong addrconf_ifdown(dev, dev != net->loopback_dev); 35651da177e4SLinus Torvalds } 35661da177e4SLinus Torvalds break; 35671da177e4SLinus Torvalds 35681da177e4SLinus Torvalds case NETDEV_DOWN: 35691da177e4SLinus Torvalds case NETDEV_UNREGISTER: 35701da177e4SLinus Torvalds /* 35711da177e4SLinus Torvalds * Remove all addresses from this interface. 35721da177e4SLinus Torvalds */ 35731da177e4SLinus Torvalds addrconf_ifdown(dev, event != NETDEV_DOWN); 35741da177e4SLinus Torvalds break; 35753c21edbdSYOSHIFUJI Hideaki 35761da177e4SLinus Torvalds case NETDEV_CHANGENAME: 35771da177e4SLinus Torvalds if (idev) { 35785632c515SStephen Hemminger snmp6_unregister_dev(idev); 3579408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 3580a317a2f1SWANG Cong err = addrconf_sysctl_register(idev); 3581b217d616SHerbert Xu if (err) 3582b217d616SHerbert Xu return notifier_from_errno(err); 3583a317a2f1SWANG Cong err = snmp6_register_dev(idev); 3584a317a2f1SWANG Cong if (err) { 3585a317a2f1SWANG Cong addrconf_sysctl_unregister(idev); 3586a317a2f1SWANG Cong return notifier_from_errno(err); 3587a317a2f1SWANG Cong } 35885632c515SStephen Hemminger } 35891da177e4SLinus Torvalds break; 3590bcdd553fSStephen Hemminger 359193d9b7d7SJiri Pirko case NETDEV_PRE_TYPE_CHANGE: 359293d9b7d7SJiri Pirko case NETDEV_POST_TYPE_CHANGE: 35933ef0952cSAndrew Lunn if (idev) 359493d9b7d7SJiri Pirko addrconf_type_change(dev, event); 359575c78500SMoni Shoua break; 35964f7f34eaSDavid Ahern 35974f7f34eaSDavid Ahern case NETDEV_CHANGEUPPER: 35984f7f34eaSDavid Ahern info = ptr; 35994f7f34eaSDavid Ahern 36004f7f34eaSDavid Ahern /* flush all routes if dev is linked to or unlinked from 36014f7f34eaSDavid Ahern * an L3 master device (e.g., VRF) 36024f7f34eaSDavid Ahern */ 36034f7f34eaSDavid Ahern if (info->upper_dev && netif_is_l3_master(info->upper_dev)) 36044f7f34eaSDavid Ahern addrconf_ifdown(dev, 0); 36053ff50b79SStephen Hemminger } 36061da177e4SLinus Torvalds 36071da177e4SLinus Torvalds return NOTIFY_OK; 36081da177e4SLinus Torvalds } 36091da177e4SLinus Torvalds 36101da177e4SLinus Torvalds /* 36111da177e4SLinus Torvalds * addrconf module should be notified of a device going up 36121da177e4SLinus Torvalds */ 36131da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = { 36141da177e4SLinus Torvalds .notifier_call = addrconf_notify, 3615242d3a49SWANG Cong .priority = ADDRCONF_NOTIFY_PRIORITY, 36161da177e4SLinus Torvalds }; 36171da177e4SLinus Torvalds 361893d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, unsigned long event) 361975c78500SMoni Shoua { 362075c78500SMoni Shoua struct inet6_dev *idev; 362175c78500SMoni Shoua ASSERT_RTNL(); 362275c78500SMoni Shoua 362375c78500SMoni Shoua idev = __in6_dev_get(dev); 362475c78500SMoni Shoua 362593d9b7d7SJiri Pirko if (event == NETDEV_POST_TYPE_CHANGE) 362675c78500SMoni Shoua ipv6_mc_remap(idev); 362793d9b7d7SJiri Pirko else if (event == NETDEV_PRE_TYPE_CHANGE) 362875c78500SMoni Shoua ipv6_mc_unmap(idev); 362975c78500SMoni Shoua } 363075c78500SMoni Shoua 363170af921dSDavid Ahern static bool addr_is_local(const struct in6_addr *addr) 363270af921dSDavid Ahern { 363370af921dSDavid Ahern return ipv6_addr_type(addr) & 363470af921dSDavid Ahern (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK); 363570af921dSDavid Ahern } 363670af921dSDavid Ahern 36371da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how) 36381da177e4SLinus Torvalds { 36394c981e28SIdo Schimmel unsigned long event = how ? NETDEV_UNREGISTER : NETDEV_DOWN; 3640c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 3641502a2ffdSstephen hemminger struct inet6_dev *idev; 3642f1705ec1SDavid Ahern struct inet6_ifaddr *ifa, *tmp; 36430aef78aaSIvan Vecera bool keep_addr = false; 364473a8bd74SDavid S. Miller int state, i; 36451da177e4SLinus Torvalds 36461da177e4SLinus Torvalds ASSERT_RTNL(); 36471da177e4SLinus Torvalds 36484c981e28SIdo Schimmel rt6_disable_ip(dev, event); 36491da177e4SLinus Torvalds 36501da177e4SLinus Torvalds idev = __in6_dev_get(dev); 365163159f29SIan Morris if (!idev) 36521da177e4SLinus Torvalds return -ENODEV; 36531da177e4SLinus Torvalds 3654bcdd553fSStephen Hemminger /* 3655bcdd553fSStephen Hemminger * Step 1: remove reference to ipv6 device from parent device. 3656bcdd553fSStephen Hemminger * Do not dev_put! 36571da177e4SLinus Torvalds */ 3658439e2385SDenis V. Lunev if (how) { 36591da177e4SLinus Torvalds idev->dead = 1; 36608814c4b5SYOSHIFUJI Hideaki 36618814c4b5SYOSHIFUJI Hideaki /* protected by rtnl_lock */ 3662a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(dev->ip6_ptr, NULL); 36631da177e4SLinus Torvalds 36641da177e4SLinus Torvalds /* Step 1.5: remove snmp6 entry */ 36651da177e4SLinus Torvalds snmp6_unregister_dev(idev); 36661da177e4SLinus Torvalds 36671da177e4SLinus Torvalds } 36681da177e4SLinus Torvalds 3669f1705ec1SDavid Ahern /* combine the user config with event to determine if permanent 3670f1705ec1SDavid Ahern * addresses are to be removed from address hash table 3671f1705ec1SDavid Ahern */ 36720aef78aaSIvan Vecera if (!how && !idev->cnf.disable_ipv6) { 36730aef78aaSIvan Vecera /* aggregate the system setting and interface setting */ 36740aef78aaSIvan Vecera int _keep_addr = net->ipv6.devconf_all->keep_addr_on_down; 36750aef78aaSIvan Vecera 36760aef78aaSIvan Vecera if (!_keep_addr) 36770aef78aaSIvan Vecera _keep_addr = idev->cnf.keep_addr_on_down; 36780aef78aaSIvan Vecera 36790aef78aaSIvan Vecera keep_addr = (_keep_addr > 0); 36800aef78aaSIvan Vecera } 3681f1705ec1SDavid Ahern 368273a8bd74SDavid S. Miller /* Step 2: clear hash table */ 368373a8bd74SDavid S. Miller for (i = 0; i < IN6_ADDR_HSIZE; i++) { 368473a8bd74SDavid S. Miller struct hlist_head *h = &inet6_addr_lst[i]; 368573a8bd74SDavid S. Miller 368673a8bd74SDavid S. Miller spin_lock_bh(&addrconf_hash_lock); 368773a8bd74SDavid S. Miller restart: 3688b67bfe0dSSasha Levin hlist_for_each_entry_rcu(ifa, h, addr_lst) { 368973a8bd74SDavid S. Miller if (ifa->idev == idev) { 3690c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifa); 3691f1705ec1SDavid Ahern /* combined flag + permanent flag decide if 3692f1705ec1SDavid Ahern * address is retained on a down event 3693f1705ec1SDavid Ahern */ 3694f1705ec1SDavid Ahern if (!keep_addr || 369570af921dSDavid Ahern !(ifa->flags & IFA_F_PERMANENT) || 369670af921dSDavid Ahern addr_is_local(&ifa->addr)) { 3697f1705ec1SDavid Ahern hlist_del_init_rcu(&ifa->addr_lst); 369873a8bd74SDavid S. Miller goto restart; 369973a8bd74SDavid S. Miller } 370073a8bd74SDavid S. Miller } 3701f1705ec1SDavid Ahern } 370273a8bd74SDavid S. Miller spin_unlock_bh(&addrconf_hash_lock); 370373a8bd74SDavid S. Miller } 370473a8bd74SDavid S. Miller 37051da177e4SLinus Torvalds write_lock_bh(&idev->lock); 37061da177e4SLinus Torvalds 3707b7b1bfceSHannes Frederic Sowa addrconf_del_rs_timer(idev); 3708b7b1bfceSHannes Frederic Sowa 3709bcdd553fSStephen Hemminger /* Step 2: clear flags for stateless addrconf */ 3710439e2385SDenis V. Lunev if (!how) 37113c21edbdSYOSHIFUJI Hideaki idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY); 37121da177e4SLinus Torvalds 3713bcdd553fSStephen Hemminger /* Step 3: clear tempaddr list */ 3714372e6c8fSstephen hemminger while (!list_empty(&idev->tempaddr_list)) { 3715372e6c8fSstephen hemminger ifa = list_first_entry(&idev->tempaddr_list, 3716372e6c8fSstephen hemminger struct inet6_ifaddr, tmp_list); 3717372e6c8fSstephen hemminger list_del(&ifa->tmp_list); 37181da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 37191da177e4SLinus Torvalds spin_lock_bh(&ifa->lock); 37201da177e4SLinus Torvalds 37211da177e4SLinus Torvalds if (ifa->ifpub) { 37221da177e4SLinus Torvalds in6_ifa_put(ifa->ifpub); 37231da177e4SLinus Torvalds ifa->ifpub = NULL; 37241da177e4SLinus Torvalds } 37251da177e4SLinus Torvalds spin_unlock_bh(&ifa->lock); 37261da177e4SLinus Torvalds in6_ifa_put(ifa); 37271da177e4SLinus Torvalds write_lock_bh(&idev->lock); 37281da177e4SLinus Torvalds } 37298f37ada5Sstephen hemminger 3730f1705ec1SDavid Ahern list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) { 37318d1c802bSDavid Ahern struct fib6_info *rt = NULL; 3732a2d6cbb0SRabin Vincent bool keep; 373338bd10c4SDavid Ahern 3734c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifa); 373584e8b803Sstephen hemminger 3736a2d6cbb0SRabin Vincent keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) && 3737a2d6cbb0SRabin Vincent !addr_is_local(&ifa->addr); 3738a2d6cbb0SRabin Vincent 37394c5ff6a6SHerbert Xu write_unlock_bh(&idev->lock); 37408e8e676dSHannes Frederic Sowa spin_lock_bh(&ifa->lock); 3741f1705ec1SDavid Ahern 3742a2d6cbb0SRabin Vincent if (keep) { 3743f1705ec1SDavid Ahern /* set state to skip the notifier below */ 3744f1705ec1SDavid Ahern state = INET6_IFADDR_STATE_DEAD; 37456d717134SDavid Ahern ifa->state = INET6_IFADDR_STATE_PREDAD; 3746f1705ec1SDavid Ahern if (!(ifa->flags & IFA_F_NODAD)) 3747f1705ec1SDavid Ahern ifa->flags |= IFA_F_TENTATIVE; 374838bd10c4SDavid Ahern 374938bd10c4SDavid Ahern rt = ifa->rt; 375038bd10c4SDavid Ahern ifa->rt = NULL; 3751f1705ec1SDavid Ahern } else { 37524c5ff6a6SHerbert Xu state = ifa->state; 37534c5ff6a6SHerbert Xu ifa->state = INET6_IFADDR_STATE_DEAD; 3754f1705ec1SDavid Ahern } 3755f1705ec1SDavid Ahern 37568e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifa->lock); 37574c5ff6a6SHerbert Xu 375838bd10c4SDavid Ahern if (rt) 3759afb1d4b5SDavid Ahern ip6_del_rt(net, rt); 376038bd10c4SDavid Ahern 37619d82ca98SJohn Fastabend if (state != INET6_IFADDR_STATE_DEAD) { 37621da177e4SLinus Torvalds __ipv6_ifa_notify(RTM_DELADDR, ifa); 3763f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_DOWN, ifa); 3764ea06f717SMike Manning } else { 3765ea06f717SMike Manning if (idev->cnf.forwarding) 3766ea06f717SMike Manning addrconf_leave_anycast(ifa); 3767ea06f717SMike Manning addrconf_leave_solict(ifa->idev, &ifa->addr); 37682de79570SLorenzo Colitti } 376973a8bd74SDavid S. Miller 37701da177e4SLinus Torvalds write_lock_bh(&idev->lock); 37718ef802aaSEric Dumazet if (!keep) { 37728ef802aaSEric Dumazet list_del_rcu(&ifa->if_list); 37738ef802aaSEric Dumazet in6_ifa_put(ifa); 37748ef802aaSEric Dumazet } 37751da177e4SLinus Torvalds } 37768f37ada5Sstephen hemminger 37771da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 37781da177e4SLinus Torvalds 3779381f4dcaSSabrina Dubroca /* Step 5: Discard anycast and multicast list */ 3780381f4dcaSSabrina Dubroca if (how) { 3781381f4dcaSSabrina Dubroca ipv6_ac_destroy_dev(idev); 37821da177e4SLinus Torvalds ipv6_mc_destroy_dev(idev); 3783381f4dcaSSabrina Dubroca } else { 37841da177e4SLinus Torvalds ipv6_mc_down(idev); 3785381f4dcaSSabrina Dubroca } 37861da177e4SLinus Torvalds 37871da177e4SLinus Torvalds idev->tstamp = jiffies; 37881da177e4SLinus Torvalds 3789bcdd553fSStephen Hemminger /* Last: Shot the device (if unregistered) */ 3790439e2385SDenis V. Lunev if (how) { 3791408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 37921da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, idev->nd_parms); 37931da177e4SLinus Torvalds neigh_ifdown(&nd_tbl, dev); 37941da177e4SLinus Torvalds in6_dev_put(idev); 37951da177e4SLinus Torvalds } 37961da177e4SLinus Torvalds return 0; 37971da177e4SLinus Torvalds } 37981da177e4SLinus Torvalds 3799e99e88a9SKees Cook static void addrconf_rs_timer(struct timer_list *t) 38001da177e4SLinus Torvalds { 3801e99e88a9SKees Cook struct inet6_dev *idev = from_timer(idev, t, rs_timer); 3802caf92bc4SCong Wang struct net_device *dev = idev->dev; 3803b7b1bfceSHannes Frederic Sowa struct in6_addr lladdr; 38041da177e4SLinus Torvalds 3805b7b1bfceSHannes Frederic Sowa write_lock(&idev->lock); 38065b2a1953Sstephen hemminger if (idev->dead || !(idev->if_flags & IF_READY)) 38071da177e4SLinus Torvalds goto out; 38081da177e4SLinus Torvalds 38099ba2add3SShmulik Ladkani if (!ipv6_accept_ra(idev)) 38101da177e4SLinus Torvalds goto out; 38115b2a1953Sstephen hemminger 38125b2a1953Sstephen hemminger /* Announcement received after solicitation was sent */ 38135b2a1953Sstephen hemminger if (idev->if_flags & IF_RA_RCVD) 38145b2a1953Sstephen hemminger goto out; 38151da177e4SLinus Torvalds 3816bd11f074SMaciej Żenczykowski if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) { 3817caf92bc4SCong Wang write_unlock(&idev->lock); 3818caf92bc4SCong Wang if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE)) 3819caf92bc4SCong Wang ndisc_send_rs(dev, &lladdr, 3820b7b1bfceSHannes Frederic Sowa &in6addr_linklocal_allrouters); 3821b7b1bfceSHannes Frederic Sowa else 3822caf92bc4SCong Wang goto put; 3823b7b1bfceSHannes Frederic Sowa 3824caf92bc4SCong Wang write_lock(&idev->lock); 3825bd11f074SMaciej Żenczykowski idev->rs_interval = rfc3315_s14_backoff_update( 3826bd11f074SMaciej Żenczykowski idev->rs_interval, idev->cnf.rtr_solicit_max_interval); 38271da177e4SLinus Torvalds /* The wait after the last probe can be shorter */ 3828b7b1bfceSHannes Frederic Sowa addrconf_mod_rs_timer(idev, (idev->rs_probes == 3829b7b1bfceSHannes Frederic Sowa idev->cnf.rtr_solicits) ? 38305b2a1953Sstephen hemminger idev->cnf.rtr_solicit_delay : 3831bd11f074SMaciej Żenczykowski idev->rs_interval); 38321da177e4SLinus Torvalds } else { 38331da177e4SLinus Torvalds /* 38341da177e4SLinus Torvalds * Note: we do not support deprecated "all on-link" 38351da177e4SLinus Torvalds * assumption any longer. 38361da177e4SLinus Torvalds */ 383791df42beSJoe Perches pr_debug("%s: no IPv6 routers present\n", idev->dev->name); 38381da177e4SLinus Torvalds } 38391da177e4SLinus Torvalds 38401da177e4SLinus Torvalds out: 3841b7b1bfceSHannes Frederic Sowa write_unlock(&idev->lock); 3842caf92bc4SCong Wang put: 3843b7b1bfceSHannes Frederic Sowa in6_dev_put(idev); 38441da177e4SLinus Torvalds } 38451da177e4SLinus Torvalds 38461da177e4SLinus Torvalds /* 38471da177e4SLinus Torvalds * Duplicate Address Detection 38481da177e4SLinus Torvalds */ 38493c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp) 38503c21edbdSYOSHIFUJI Hideaki { 38513c21edbdSYOSHIFUJI Hideaki unsigned long rand_num; 38523c21edbdSYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev; 3853adc176c5SErik Nordmark u64 nonce; 38543c21edbdSYOSHIFUJI Hideaki 385595c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 385695c385b4SNeil Horman rand_num = 0; 385795c385b4SNeil Horman else 385863862b5bSAruna-Hewapathirane rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1); 385995c385b4SNeil Horman 3860adc176c5SErik Nordmark nonce = 0; 3861adc176c5SErik Nordmark if (idev->cnf.enhanced_dad || 3862adc176c5SErik Nordmark dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad) { 3863adc176c5SErik Nordmark do 3864adc176c5SErik Nordmark get_random_bytes(&nonce, 6); 3865adc176c5SErik Nordmark while (nonce == 0); 3866adc176c5SErik Nordmark } 3867adc176c5SErik Nordmark ifp->dad_nonce = nonce; 3868b7b1bfceSHannes Frederic Sowa ifp->dad_probes = idev->cnf.dad_transmits; 3869c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, rand_num); 38703c21edbdSYOSHIFUJI Hideaki } 38713c21edbdSYOSHIFUJI Hideaki 3872c15b1ccaSHannes Frederic Sowa static void addrconf_dad_begin(struct inet6_ifaddr *ifp) 38731da177e4SLinus Torvalds { 38741da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 38751da177e4SLinus Torvalds struct net_device *dev = idev->dev; 3876764d3be6SPaolo Abeni bool bump_id, notify = false; 3877afb1d4b5SDavid Ahern struct net *net; 38781da177e4SLinus Torvalds 38791da177e4SLinus Torvalds addrconf_join_solict(dev, &ifp->addr); 38801da177e4SLinus Torvalds 388163862b5bSAruna-Hewapathirane prandom_seed((__force u32) ifp->addr.s6_addr32[3]); 38821da177e4SLinus Torvalds 38831da177e4SLinus Torvalds read_lock_bh(&idev->lock); 3884622ccdf1SHerbert Xu spin_lock(&ifp->lock); 3885e9d3e084SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DEAD) 38861da177e4SLinus Torvalds goto out; 38871da177e4SLinus Torvalds 3888afb1d4b5SDavid Ahern net = dev_net(dev); 38891da177e4SLinus Torvalds if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) || 3890afb1d4b5SDavid Ahern (net->ipv6.devconf_all->accept_dad < 1 && 3891a2d3f3e3SMatteo Croce idev->cnf.accept_dad < 1) || 389255ebaef1SNoriaki TAKAMIYA !(ifp->flags&IFA_F_TENTATIVE) || 389355ebaef1SNoriaki TAKAMIYA ifp->flags & IFA_F_NODAD) { 3894c76fe2d9SDavid Ahern bool send_na = false; 3895c76fe2d9SDavid Ahern 3896c76fe2d9SDavid Ahern if (ifp->flags & IFA_F_TENTATIVE && 3897c76fe2d9SDavid Ahern !(ifp->flags & IFA_F_OPTIMISTIC)) 3898c76fe2d9SDavid Ahern send_na = true; 3899764d3be6SPaolo Abeni bump_id = ifp->flags & IFA_F_TENTATIVE; 3900cc411d0bSBrian Haley ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 390121809fafSstephen hemminger spin_unlock(&ifp->lock); 39021da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 39031da177e4SLinus Torvalds 3904c76fe2d9SDavid Ahern addrconf_dad_completed(ifp, bump_id, send_na); 39051da177e4SLinus Torvalds return; 39061da177e4SLinus Torvalds } 39071da177e4SLinus Torvalds 39086732badeSYOSHIFUJI Hideaki if (!(idev->if_flags & IF_READY)) { 390921809fafSstephen hemminger spin_unlock(&ifp->lock); 39106732badeSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 39113c21edbdSYOSHIFUJI Hideaki /* 3912590a9887SMasatake YAMATO * If the device is not ready: 39133c21edbdSYOSHIFUJI Hideaki * - keep it tentative if it is a permanent address. 39143c21edbdSYOSHIFUJI Hideaki * - otherwise, kill it. 39153c21edbdSYOSHIFUJI Hideaki */ 39163c21edbdSYOSHIFUJI Hideaki in6_ifa_hold(ifp); 3917cc411d0bSBrian Haley addrconf_dad_stop(ifp, 0); 39186732badeSYOSHIFUJI Hideaki return; 39193c21edbdSYOSHIFUJI Hideaki } 392095c385b4SNeil Horman 392195c385b4SNeil Horman /* 392295c385b4SNeil Horman * Optimistic nodes can start receiving 392395c385b4SNeil Horman * Frames right away 392495c385b4SNeil Horman */ 39257fd2561eSErik Kline if (ifp->flags & IFA_F_OPTIMISTIC) { 3926afb1d4b5SDavid Ahern ip6_ins_rt(net, ifp->rt); 3927afb1d4b5SDavid Ahern if (ipv6_use_optimistic_addr(net, idev)) { 39287fd2561eSErik Kline /* Because optimistic nodes can use this address, 39297fd2561eSErik Kline * notify listeners. If DAD fails, RTM_DELADDR is sent. 39307fd2561eSErik Kline */ 393116186a82Ssubashab@codeaurora.org notify = true; 39327fd2561eSErik Kline } 39337fd2561eSErik Kline } 393495c385b4SNeil Horman 39356732badeSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 39361da177e4SLinus Torvalds out: 3937622ccdf1SHerbert Xu spin_unlock(&ifp->lock); 39381da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 393916186a82Ssubashab@codeaurora.org if (notify) 394016186a82Ssubashab@codeaurora.org ipv6_ifa_notify(RTM_NEWADDR, ifp); 39411da177e4SLinus Torvalds } 39421da177e4SLinus Torvalds 3943c15b1ccaSHannes Frederic Sowa static void addrconf_dad_start(struct inet6_ifaddr *ifp) 39441da177e4SLinus Torvalds { 3945c15b1ccaSHannes Frederic Sowa bool begin_dad = false; 3946c15b1ccaSHannes Frederic Sowa 39478e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 3948c15b1ccaSHannes Frederic Sowa if (ifp->state != INET6_IFADDR_STATE_DEAD) { 3949c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_PREDAD; 3950c15b1ccaSHannes Frederic Sowa begin_dad = true; 3951c15b1ccaSHannes Frederic Sowa } 39528e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 3953c15b1ccaSHannes Frederic Sowa 3954c15b1ccaSHannes Frederic Sowa if (begin_dad) 3955c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 0); 3956c15b1ccaSHannes Frederic Sowa } 3957c15b1ccaSHannes Frederic Sowa 3958c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w) 3959c15b1ccaSHannes Frederic Sowa { 3960c15b1ccaSHannes Frederic Sowa struct inet6_ifaddr *ifp = container_of(to_delayed_work(w), 3961c15b1ccaSHannes Frederic Sowa struct inet6_ifaddr, 3962c15b1ccaSHannes Frederic Sowa dad_work); 39631da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 3964764d3be6SPaolo Abeni bool bump_id, disable_ipv6 = false; 39651da177e4SLinus Torvalds struct in6_addr mcaddr; 39661da177e4SLinus Torvalds 3967c15b1ccaSHannes Frederic Sowa enum { 3968c15b1ccaSHannes Frederic Sowa DAD_PROCESS, 3969c15b1ccaSHannes Frederic Sowa DAD_BEGIN, 3970c15b1ccaSHannes Frederic Sowa DAD_ABORT, 3971c15b1ccaSHannes Frederic Sowa } action = DAD_PROCESS; 3972c15b1ccaSHannes Frederic Sowa 3973c15b1ccaSHannes Frederic Sowa rtnl_lock(); 3974c15b1ccaSHannes Frederic Sowa 39758e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 3976c15b1ccaSHannes Frederic Sowa if (ifp->state == INET6_IFADDR_STATE_PREDAD) { 3977c15b1ccaSHannes Frederic Sowa action = DAD_BEGIN; 3978c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_DAD; 3979c15b1ccaSHannes Frederic Sowa } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) { 3980c15b1ccaSHannes Frederic Sowa action = DAD_ABORT; 3981c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_POSTDAD; 398285b51b12SMike Manning 398335e015e1SMatteo Croce if ((dev_net(idev->dev)->ipv6.devconf_all->accept_dad > 1 || 398435e015e1SMatteo Croce idev->cnf.accept_dad > 1) && 398535e015e1SMatteo Croce !idev->cnf.disable_ipv6 && 398685b51b12SMike Manning !(ifp->flags & IFA_F_STABLE_PRIVACY)) { 398785b51b12SMike Manning struct in6_addr addr; 398885b51b12SMike Manning 398985b51b12SMike Manning addr.s6_addr32[0] = htonl(0xfe800000); 399085b51b12SMike Manning addr.s6_addr32[1] = 0; 399185b51b12SMike Manning 399285b51b12SMike Manning if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) && 399385b51b12SMike Manning ipv6_addr_equal(&ifp->addr, &addr)) { 399485b51b12SMike Manning /* DAD failed for link-local based on MAC */ 399585b51b12SMike Manning idev->cnf.disable_ipv6 = 1; 399685b51b12SMike Manning 399785b51b12SMike Manning pr_info("%s: IPv6 being disabled!\n", 399885b51b12SMike Manning ifp->idev->dev->name); 399985b51b12SMike Manning disable_ipv6 = true; 400085b51b12SMike Manning } 400185b51b12SMike Manning } 4002c15b1ccaSHannes Frederic Sowa } 40038e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 4004c15b1ccaSHannes Frederic Sowa 4005c15b1ccaSHannes Frederic Sowa if (action == DAD_BEGIN) { 4006c15b1ccaSHannes Frederic Sowa addrconf_dad_begin(ifp); 4007c15b1ccaSHannes Frederic Sowa goto out; 4008c15b1ccaSHannes Frederic Sowa } else if (action == DAD_ABORT) { 4009751eb6b6SWei Yongjun in6_ifa_hold(ifp); 4010c15b1ccaSHannes Frederic Sowa addrconf_dad_stop(ifp, 1); 401185b51b12SMike Manning if (disable_ipv6) 401285b51b12SMike Manning addrconf_ifdown(idev->dev, 0); 4013c15b1ccaSHannes Frederic Sowa goto out; 4014c15b1ccaSHannes Frederic Sowa } 4015c15b1ccaSHannes Frederic Sowa 4016b7b1bfceSHannes Frederic Sowa if (!ifp->dad_probes && addrconf_dad_end(ifp)) 4017f2344a13SHerbert Xu goto out; 4018f2344a13SHerbert Xu 4019c15b1ccaSHannes Frederic Sowa write_lock_bh(&idev->lock); 4020122e4519Sstephen hemminger if (idev->dead || !(idev->if_flags & IF_READY)) { 4021c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 40221da177e4SLinus Torvalds goto out; 40231da177e4SLinus Torvalds } 402421809fafSstephen hemminger 402521809fafSstephen hemminger spin_lock(&ifp->lock); 4026622ccdf1SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DEAD) { 4027622ccdf1SHerbert Xu spin_unlock(&ifp->lock); 4028c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 4029622ccdf1SHerbert Xu goto out; 4030622ccdf1SHerbert Xu } 4031622ccdf1SHerbert Xu 4032b7b1bfceSHannes Frederic Sowa if (ifp->dad_probes == 0) { 4033c76fe2d9SDavid Ahern bool send_na = false; 4034c76fe2d9SDavid Ahern 40351da177e4SLinus Torvalds /* 40361da177e4SLinus Torvalds * DAD was successful 40371da177e4SLinus Torvalds */ 40381da177e4SLinus Torvalds 4039c76fe2d9SDavid Ahern if (ifp->flags & IFA_F_TENTATIVE && 4040c76fe2d9SDavid Ahern !(ifp->flags & IFA_F_OPTIMISTIC)) 4041c76fe2d9SDavid Ahern send_na = true; 4042764d3be6SPaolo Abeni bump_id = ifp->flags & IFA_F_TENTATIVE; 4043cc411d0bSBrian Haley ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 404421809fafSstephen hemminger spin_unlock(&ifp->lock); 4045c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 40461da177e4SLinus Torvalds 4047c76fe2d9SDavid Ahern addrconf_dad_completed(ifp, bump_id, send_na); 40481da177e4SLinus Torvalds 40491da177e4SLinus Torvalds goto out; 40501da177e4SLinus Torvalds } 40511da177e4SLinus Torvalds 4052b7b1bfceSHannes Frederic Sowa ifp->dad_probes--; 4053c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 40541f9248e5SJiri Pirko NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME)); 405521809fafSstephen hemminger spin_unlock(&ifp->lock); 4056c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 40571da177e4SLinus Torvalds 40581da177e4SLinus Torvalds /* send a neighbour solicitation for our addr */ 40591da177e4SLinus Torvalds addrconf_addr_solict_mult(&ifp->addr, &mcaddr); 4060adc176c5SErik Nordmark ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any, 4061adc176c5SErik Nordmark ifp->dad_nonce); 40621da177e4SLinus Torvalds out: 40631da177e4SLinus Torvalds in6_ifa_put(ifp); 4064c15b1ccaSHannes Frederic Sowa rtnl_unlock(); 40651da177e4SLinus Torvalds } 40661da177e4SLinus Torvalds 406711ffff75SHannes Frederic Sowa /* ifp->idev must be at least read locked */ 406811ffff75SHannes Frederic Sowa static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp) 406911ffff75SHannes Frederic Sowa { 407011ffff75SHannes Frederic Sowa struct inet6_ifaddr *ifpiter; 407111ffff75SHannes Frederic Sowa struct inet6_dev *idev = ifp->idev; 407211ffff75SHannes Frederic Sowa 4073602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) { 4074602582caSHannes Frederic Sowa if (ifpiter->scope > IFA_LINK) 4075602582caSHannes Frederic Sowa break; 407611ffff75SHannes Frederic Sowa if (ifp != ifpiter && ifpiter->scope == IFA_LINK && 407711ffff75SHannes Frederic Sowa (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE| 407811ffff75SHannes Frederic Sowa IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) == 407911ffff75SHannes Frederic Sowa IFA_F_PERMANENT) 408011ffff75SHannes Frederic Sowa return false; 408111ffff75SHannes Frederic Sowa } 408211ffff75SHannes Frederic Sowa return true; 408311ffff75SHannes Frederic Sowa } 408411ffff75SHannes Frederic Sowa 4085c76fe2d9SDavid Ahern static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id, 4086c76fe2d9SDavid Ahern bool send_na) 40871da177e4SLinus Torvalds { 40881da177e4SLinus Torvalds struct net_device *dev = ifp->idev->dev; 4089b7b1bfceSHannes Frederic Sowa struct in6_addr lladdr; 4090b173ee48SHannes Frederic Sowa bool send_rs, send_mld; 4091b7b1bfceSHannes Frederic Sowa 4092c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifp); 40931da177e4SLinus Torvalds 40941da177e4SLinus Torvalds /* 40951da177e4SLinus Torvalds * Configure the address for reception. Now it is valid. 40961da177e4SLinus Torvalds */ 40971da177e4SLinus Torvalds 40981da177e4SLinus Torvalds ipv6_ifa_notify(RTM_NEWADDR, ifp); 40991da177e4SLinus Torvalds 4100026359bcSTore Anderson /* If added prefix is link local and we are prepared to process 4101026359bcSTore Anderson router advertisements, start sending router solicitations. 41021da177e4SLinus Torvalds */ 41031da177e4SLinus Torvalds 41041ec047ebSHannes Frederic Sowa read_lock_bh(&ifp->idev->lock); 410511ffff75SHannes Frederic Sowa send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp); 4106b173ee48SHannes Frederic Sowa send_rs = send_mld && 4107b173ee48SHannes Frederic Sowa ipv6_accept_ra(ifp->idev) && 4108bd11f074SMaciej Żenczykowski ifp->idev->cnf.rtr_solicits != 0 && 4109b173ee48SHannes Frederic Sowa (dev->flags&IFF_LOOPBACK) == 0; 41101ec047ebSHannes Frederic Sowa read_unlock_bh(&ifp->idev->lock); 41111ec047ebSHannes Frederic Sowa 4112b173ee48SHannes Frederic Sowa /* While dad is in progress mld report's source address is in6_addrany. 4113b173ee48SHannes Frederic Sowa * Resend with proper ll now. 4114b173ee48SHannes Frederic Sowa */ 4115b173ee48SHannes Frederic Sowa if (send_mld) 4116b173ee48SHannes Frederic Sowa ipv6_mc_dad_complete(ifp->idev); 4117b173ee48SHannes Frederic Sowa 4118c76fe2d9SDavid Ahern /* send unsolicited NA if enabled */ 4119c76fe2d9SDavid Ahern if (send_na && 4120c76fe2d9SDavid Ahern (ifp->idev->cnf.ndisc_notify || 4121c76fe2d9SDavid Ahern dev_net(dev)->ipv6.devconf_all->ndisc_notify)) { 4122c76fe2d9SDavid Ahern ndisc_send_na(dev, &in6addr_linklocal_allnodes, &ifp->addr, 4123c76fe2d9SDavid Ahern /*router=*/ !!ifp->idev->cnf.forwarding, 4124c76fe2d9SDavid Ahern /*solicited=*/ false, /*override=*/ true, 4125c76fe2d9SDavid Ahern /*inc_opt=*/ true); 4126c76fe2d9SDavid Ahern } 4127c76fe2d9SDavid Ahern 41281ec047ebSHannes Frederic Sowa if (send_rs) { 41291da177e4SLinus Torvalds /* 41301da177e4SLinus Torvalds * If a host as already performed a random delay 41311da177e4SLinus Torvalds * [...] as part of DAD [...] there is no need 41321da177e4SLinus Torvalds * to delay again before sending the first RS 41331da177e4SLinus Torvalds */ 41341ec047ebSHannes Frederic Sowa if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE)) 4135b7b1bfceSHannes Frederic Sowa return; 41361ec047ebSHannes Frederic Sowa ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters); 41371da177e4SLinus Torvalds 4138b7b1bfceSHannes Frederic Sowa write_lock_bh(&ifp->idev->lock); 4139b7b1bfceSHannes Frederic Sowa spin_lock(&ifp->lock); 4140bd11f074SMaciej Żenczykowski ifp->idev->rs_interval = rfc3315_s14_backoff_init( 4141bd11f074SMaciej Żenczykowski ifp->idev->cnf.rtr_solicit_interval); 4142b7b1bfceSHannes Frederic Sowa ifp->idev->rs_probes = 1; 41431da177e4SLinus Torvalds ifp->idev->if_flags |= IF_RS_SENT; 4144bd11f074SMaciej Żenczykowski addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval); 4145b7b1bfceSHannes Frederic Sowa spin_unlock(&ifp->lock); 4146b7b1bfceSHannes Frederic Sowa write_unlock_bh(&ifp->idev->lock); 41471da177e4SLinus Torvalds } 4148764d3be6SPaolo Abeni 4149764d3be6SPaolo Abeni if (bump_id) 4150764d3be6SPaolo Abeni rt_genid_bump_ipv6(dev_net(dev)); 4151a11a7f71SMarcus Huewe 4152a11a7f71SMarcus Huewe /* Make sure that a new temporary address will be created 4153a11a7f71SMarcus Huewe * before this temporary address becomes deprecated. 4154a11a7f71SMarcus Huewe */ 4155a11a7f71SMarcus Huewe if (ifp->flags & IFA_F_TEMPORARY) 4156a11a7f71SMarcus Huewe addrconf_verify_rtnl(); 41571da177e4SLinus Torvalds } 41581da177e4SLinus Torvalds 4159e21e8467SStephen Hemminger static void addrconf_dad_run(struct inet6_dev *idev) 4160e21e8467SStephen Hemminger { 4161c5e33bddSYOSHIFUJI Hideaki struct inet6_ifaddr *ifp; 4162c5e33bddSYOSHIFUJI Hideaki 4163c5e33bddSYOSHIFUJI Hideaki read_lock_bh(&idev->lock); 4164502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 416521809fafSstephen hemminger spin_lock(&ifp->lock); 4166f2344a13SHerbert Xu if (ifp->flags & IFA_F_TENTATIVE && 4167f2344a13SHerbert Xu ifp->state == INET6_IFADDR_STATE_DAD) 4168c5e33bddSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 4169f2344a13SHerbert Xu spin_unlock(&ifp->lock); 4170c5e33bddSYOSHIFUJI Hideaki } 4171c5e33bddSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 4172c5e33bddSYOSHIFUJI Hideaki } 4173c5e33bddSYOSHIFUJI Hideaki 41741da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 41751da177e4SLinus Torvalds struct if6_iter_state { 41763c40090aSDaniel Lezcano struct seq_net_private p; 41771da177e4SLinus Torvalds int bucket; 41781d578303SMihai Maruseac int offset; 41791da177e4SLinus Torvalds }; 41801da177e4SLinus Torvalds 41811d578303SMihai Maruseac static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos) 41821da177e4SLinus Torvalds { 41831da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 41841218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 4185a5c1d98fSEric Dumazet struct inet6_ifaddr *ifa = NULL; 41861d578303SMihai Maruseac int p = 0; 41871da177e4SLinus Torvalds 41881d578303SMihai Maruseac /* initial bucket if pos is 0 */ 41891d578303SMihai Maruseac if (pos == 0) { 41901d578303SMihai Maruseac state->bucket = 0; 41911d578303SMihai Maruseac state->offset = 0; 41921d578303SMihai Maruseac } 41931d578303SMihai Maruseac 41941d578303SMihai Maruseac for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) { 4195a5c1d98fSEric Dumazet hlist_for_each_entry_rcu(ifa, &inet6_addr_lst[state->bucket], 41961d578303SMihai Maruseac addr_lst) { 41979f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 41989f0d3c27SEric Dumazet continue; 41991d578303SMihai Maruseac /* sync with offset */ 42001d578303SMihai Maruseac if (p < state->offset) { 42011d578303SMihai Maruseac p++; 42021d578303SMihai Maruseac continue; 42031d578303SMihai Maruseac } 42041da177e4SLinus Torvalds return ifa; 42051da177e4SLinus Torvalds } 42061d578303SMihai Maruseac 42071d578303SMihai Maruseac /* prepare for next bucket */ 42081d578303SMihai Maruseac state->offset = 0; 42091d578303SMihai Maruseac p = 0; 42101d578303SMihai Maruseac } 4211c2e21293Sstephen hemminger return NULL; 4212c2e21293Sstephen hemminger } 42131da177e4SLinus Torvalds 4214c2e21293Sstephen hemminger static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, 4215c2e21293Sstephen hemminger struct inet6_ifaddr *ifa) 42161da177e4SLinus Torvalds { 42171da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 42181218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 42191da177e4SLinus Torvalds 4220a5c1d98fSEric Dumazet hlist_for_each_entry_continue_rcu(ifa, addr_lst) { 42219f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 42229f0d3c27SEric Dumazet continue; 42231d578303SMihai Maruseac state->offset++; 42241da177e4SLinus Torvalds return ifa; 42251d578303SMihai Maruseac } 42261da177e4SLinus Torvalds 42271d578303SMihai Maruseac state->offset = 0; 4228*86f9bd1fSJeff Barnhill while (++state->bucket < IN6_ADDR_HSIZE) { 4229a5c1d98fSEric Dumazet hlist_for_each_entry_rcu(ifa, 4230c2e21293Sstephen hemminger &inet6_addr_lst[state->bucket], addr_lst) { 42319f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 42329f0d3c27SEric Dumazet continue; 4233c2e21293Sstephen hemminger return ifa; 4234c2e21293Sstephen hemminger } 4235c2e21293Sstephen hemminger } 4236c2e21293Sstephen hemminger 4237c2e21293Sstephen hemminger return NULL; 4238c2e21293Sstephen hemminger } 4239c2e21293Sstephen hemminger 42401da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos) 4241a5c1d98fSEric Dumazet __acquires(rcu) 42421da177e4SLinus Torvalds { 4243a5c1d98fSEric Dumazet rcu_read_lock(); 42441d578303SMihai Maruseac return if6_get_first(seq, *pos); 42451da177e4SLinus Torvalds } 42461da177e4SLinus Torvalds 42471da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos) 42481da177e4SLinus Torvalds { 42491da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 42501da177e4SLinus Torvalds 42511da177e4SLinus Torvalds ifa = if6_get_next(seq, v); 42521da177e4SLinus Torvalds ++*pos; 42531da177e4SLinus Torvalds return ifa; 42541da177e4SLinus Torvalds } 42551da177e4SLinus Torvalds 42561da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v) 4257a5c1d98fSEric Dumazet __releases(rcu) 42581da177e4SLinus Torvalds { 4259a5c1d98fSEric Dumazet rcu_read_unlock(); 42601da177e4SLinus Torvalds } 42611da177e4SLinus Torvalds 42621da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v) 42631da177e4SLinus Torvalds { 42641da177e4SLinus Torvalds struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v; 4265971a351cSJiri Pirko seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n", 4266b071195dSHarvey Harrison &ifp->addr, 42671da177e4SLinus Torvalds ifp->idev->dev->ifindex, 42681da177e4SLinus Torvalds ifp->prefix_len, 42691da177e4SLinus Torvalds ifp->scope, 4270971a351cSJiri Pirko (u8) ifp->flags, 42711da177e4SLinus Torvalds ifp->idev->dev->name); 42721da177e4SLinus Torvalds return 0; 42731da177e4SLinus Torvalds } 42741da177e4SLinus Torvalds 427556b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = { 42761da177e4SLinus Torvalds .start = if6_seq_start, 42771da177e4SLinus Torvalds .next = if6_seq_next, 42781da177e4SLinus Torvalds .show = if6_seq_show, 42791da177e4SLinus Torvalds .stop = if6_seq_stop, 42801da177e4SLinus Torvalds }; 42811da177e4SLinus Torvalds 42822c8c1e72SAlexey Dobriyan static int __net_init if6_proc_net_init(struct net *net) 42831da177e4SLinus Torvalds { 4284c3506372SChristoph Hellwig if (!proc_create_net("if_inet6", 0444, net->proc_net, &if6_seq_ops, 4285c3506372SChristoph Hellwig sizeof(struct if6_iter_state))) 42861da177e4SLinus Torvalds return -ENOMEM; 42871da177e4SLinus Torvalds return 0; 42881da177e4SLinus Torvalds } 42891da177e4SLinus Torvalds 42902c8c1e72SAlexey Dobriyan static void __net_exit if6_proc_net_exit(struct net *net) 42913c40090aSDaniel Lezcano { 4292ece31ffdSGao feng remove_proc_entry("if_inet6", net->proc_net); 42933c40090aSDaniel Lezcano } 42943c40090aSDaniel Lezcano 42953c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = { 42963c40090aSDaniel Lezcano .init = if6_proc_net_init, 42973c40090aSDaniel Lezcano .exit = if6_proc_net_exit, 42983c40090aSDaniel Lezcano }; 42993c40090aSDaniel Lezcano 43003c40090aSDaniel Lezcano int __init if6_proc_init(void) 43013c40090aSDaniel Lezcano { 43023c40090aSDaniel Lezcano return register_pernet_subsys(&if6_proc_net_ops); 43033c40090aSDaniel Lezcano } 43043c40090aSDaniel Lezcano 43051da177e4SLinus Torvalds void if6_proc_exit(void) 43061da177e4SLinus Torvalds { 43073c40090aSDaniel Lezcano unregister_pernet_subsys(&if6_proc_net_ops); 43081da177e4SLinus Torvalds } 43091da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 43101da177e4SLinus Torvalds 431107a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6) 43123b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */ 4313b71d1d42SEric Dumazet int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr) 43143b9f9a1cSNoriaki TAKAMIYA { 43153f27fb23SEric Dumazet unsigned int hash = inet6_addr_hash(net, addr); 4316c2e21293Sstephen hemminger struct inet6_ifaddr *ifp = NULL; 43173f27fb23SEric Dumazet int ret = 0; 4318c2e21293Sstephen hemminger 43194e5f47abSEric Dumazet rcu_read_lock(); 43204e5f47abSEric Dumazet hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) { 4321878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 4322389f6612SDaniel Lezcano continue; 4323caad295fSYOSHIFUJI Hideaki if (ipv6_addr_equal(&ifp->addr, addr) && 43243b9f9a1cSNoriaki TAKAMIYA (ifp->flags & IFA_F_HOMEADDRESS)) { 43253b9f9a1cSNoriaki TAKAMIYA ret = 1; 43263b9f9a1cSNoriaki TAKAMIYA break; 43273b9f9a1cSNoriaki TAKAMIYA } 43283b9f9a1cSNoriaki TAKAMIYA } 43294e5f47abSEric Dumazet rcu_read_unlock(); 43303b9f9a1cSNoriaki TAKAMIYA return ret; 43313b9f9a1cSNoriaki TAKAMIYA } 43323b9f9a1cSNoriaki TAKAMIYA #endif 43333b9f9a1cSNoriaki TAKAMIYA 43341da177e4SLinus Torvalds /* 43351da177e4SLinus Torvalds * Periodic address status verification 43361da177e4SLinus Torvalds */ 43371da177e4SLinus Torvalds 4338c15b1ccaSHannes Frederic Sowa static void addrconf_verify_rtnl(void) 43391da177e4SLinus Torvalds { 4340b2db7564SYOSHIFUJI Hideaki unsigned long now, next, next_sec, next_sched; 43411da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 43421da177e4SLinus Torvalds int i; 43431da177e4SLinus Torvalds 4344c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 4345c15b1ccaSHannes Frederic Sowa 43465c578aedSstephen hemminger rcu_read_lock_bh(); 43471da177e4SLinus Torvalds now = jiffies; 4348b2db7564SYOSHIFUJI Hideaki next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY); 43491da177e4SLinus Torvalds 4350c15b1ccaSHannes Frederic Sowa cancel_delayed_work(&addr_chk_work); 43511da177e4SLinus Torvalds 43521da177e4SLinus Torvalds for (i = 0; i < IN6_ADDR_HSIZE; i++) { 43531da177e4SLinus Torvalds restart: 4354c15b1ccaSHannes Frederic Sowa hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) { 43551da177e4SLinus Torvalds unsigned long age; 43561da177e4SLinus Torvalds 4357fad8da3eSYasushi Asano /* When setting preferred_lft to a value not zero or 4358fad8da3eSYasushi Asano * infinity, while valid_lft is infinity 4359fad8da3eSYasushi Asano * IFA_F_PERMANENT has a non-infinity life time. 4360fad8da3eSYasushi Asano */ 4361fad8da3eSYasushi Asano if ((ifp->flags & IFA_F_PERMANENT) && 4362fad8da3eSYasushi Asano (ifp->prefered_lft == INFINITY_LIFE_TIME)) 43631da177e4SLinus Torvalds continue; 43641da177e4SLinus Torvalds 43651da177e4SLinus Torvalds spin_lock(&ifp->lock); 4366b2db7564SYOSHIFUJI Hideaki /* We try to batch several events at once. */ 4367b2db7564SYOSHIFUJI Hideaki age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; 43681da177e4SLinus Torvalds 43698f27ebb9SYOSHIFUJI Hideaki if (ifp->valid_lft != INFINITY_LIFE_TIME && 43708f27ebb9SYOSHIFUJI Hideaki age >= ifp->valid_lft) { 43711da177e4SLinus Torvalds spin_unlock(&ifp->lock); 43721da177e4SLinus Torvalds in6_ifa_hold(ifp); 43731da177e4SLinus Torvalds ipv6_del_addr(ifp); 43741da177e4SLinus Torvalds goto restart; 43758f27ebb9SYOSHIFUJI Hideaki } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) { 43768f27ebb9SYOSHIFUJI Hideaki spin_unlock(&ifp->lock); 43778f27ebb9SYOSHIFUJI Hideaki continue; 43781da177e4SLinus Torvalds } else if (age >= ifp->prefered_lft) { 4379a1ed0526SBrian Haley /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */ 43801da177e4SLinus Torvalds int deprecate = 0; 43811da177e4SLinus Torvalds 43821da177e4SLinus Torvalds if (!(ifp->flags&IFA_F_DEPRECATED)) { 43831da177e4SLinus Torvalds deprecate = 1; 43841da177e4SLinus Torvalds ifp->flags |= IFA_F_DEPRECATED; 43851da177e4SLinus Torvalds } 43861da177e4SLinus Torvalds 4387fad8da3eSYasushi Asano if ((ifp->valid_lft != INFINITY_LIFE_TIME) && 4388fad8da3eSYasushi Asano (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))) 43891da177e4SLinus Torvalds next = ifp->tstamp + ifp->valid_lft * HZ; 43901da177e4SLinus Torvalds 43911da177e4SLinus Torvalds spin_unlock(&ifp->lock); 43921da177e4SLinus Torvalds 43931da177e4SLinus Torvalds if (deprecate) { 43941da177e4SLinus Torvalds in6_ifa_hold(ifp); 43951da177e4SLinus Torvalds 43961da177e4SLinus Torvalds ipv6_ifa_notify(0, ifp); 43971da177e4SLinus Torvalds in6_ifa_put(ifp); 43981da177e4SLinus Torvalds goto restart; 43991da177e4SLinus Torvalds } 44001da177e4SLinus Torvalds } else if ((ifp->flags&IFA_F_TEMPORARY) && 44011da177e4SLinus Torvalds !(ifp->flags&IFA_F_TENTATIVE)) { 440288949cf4Sstephen hemminger unsigned long regen_advance = ifp->idev->cnf.regen_max_retry * 440388949cf4Sstephen hemminger ifp->idev->cnf.dad_transmits * 44041f9248e5SJiri Pirko NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ; 440588949cf4Sstephen hemminger 44061da177e4SLinus Torvalds if (age >= ifp->prefered_lft - regen_advance) { 44071da177e4SLinus Torvalds struct inet6_ifaddr *ifpub = ifp->ifpub; 44081da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 44091da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 44101da177e4SLinus Torvalds if (!ifp->regen_count && ifpub) { 44111da177e4SLinus Torvalds ifp->regen_count++; 44121da177e4SLinus Torvalds in6_ifa_hold(ifp); 44131da177e4SLinus Torvalds in6_ifa_hold(ifpub); 44141da177e4SLinus Torvalds spin_unlock(&ifp->lock); 44155c578aedSstephen hemminger 4416291d809bSHiroyuki YAMAMORI spin_lock(&ifpub->lock); 4417291d809bSHiroyuki YAMAMORI ifpub->regen_count = 0; 4418291d809bSHiroyuki YAMAMORI spin_unlock(&ifpub->lock); 4419e64e469bSEric Dumazet rcu_read_unlock_bh(); 4420fffcefe9SEric Dumazet ipv6_create_tempaddr(ifpub, ifp, true); 44211da177e4SLinus Torvalds in6_ifa_put(ifpub); 44221da177e4SLinus Torvalds in6_ifa_put(ifp); 4423e64e469bSEric Dumazet rcu_read_lock_bh(); 44241da177e4SLinus Torvalds goto restart; 44251da177e4SLinus Torvalds } 44261da177e4SLinus Torvalds } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next)) 44271da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ; 44281da177e4SLinus Torvalds spin_unlock(&ifp->lock); 44291da177e4SLinus Torvalds } else { 44301da177e4SLinus Torvalds /* ifp->prefered_lft <= ifp->valid_lft */ 44311da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 44321da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 44331da177e4SLinus Torvalds spin_unlock(&ifp->lock); 44341da177e4SLinus Torvalds } 44351da177e4SLinus Torvalds } 44361da177e4SLinus Torvalds } 44371da177e4SLinus Torvalds 4438b2db7564SYOSHIFUJI Hideaki next_sec = round_jiffies_up(next); 4439b2db7564SYOSHIFUJI Hideaki next_sched = next; 4440b2db7564SYOSHIFUJI Hideaki 4441b2db7564SYOSHIFUJI Hideaki /* If rounded timeout is accurate enough, accept it. */ 4442b2db7564SYOSHIFUJI Hideaki if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ)) 4443b2db7564SYOSHIFUJI Hideaki next_sched = next_sec; 4444b2db7564SYOSHIFUJI Hideaki 4445b2db7564SYOSHIFUJI Hideaki /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */ 4446b2db7564SYOSHIFUJI Hideaki if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX)) 4447b2db7564SYOSHIFUJI Hideaki next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX; 4448b2db7564SYOSHIFUJI Hideaki 4449e32ac250SJoe Perches pr_debug("now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n", 4450ba3542e1Sdingtianhong now, next, next_sec, next_sched); 4451c15b1ccaSHannes Frederic Sowa mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now); 44525c578aedSstephen hemminger rcu_read_unlock_bh(); 44531da177e4SLinus Torvalds } 44541da177e4SLinus Torvalds 4455c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *w) 4456c15b1ccaSHannes Frederic Sowa { 4457c15b1ccaSHannes Frederic Sowa rtnl_lock(); 4458c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 4459c15b1ccaSHannes Frederic Sowa rtnl_unlock(); 4460c15b1ccaSHannes Frederic Sowa } 4461c15b1ccaSHannes Frederic Sowa 4462c15b1ccaSHannes Frederic Sowa static void addrconf_verify(void) 4463c15b1ccaSHannes Frederic Sowa { 4464c15b1ccaSHannes Frederic Sowa mod_delayed_work(addrconf_wq, &addr_chk_work, 0); 4465c15b1ccaSHannes Frederic Sowa } 4466c15b1ccaSHannes Frederic Sowa 4467caeaba79SNicolas Dichtel static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local, 4468caeaba79SNicolas Dichtel struct in6_addr **peer_pfx) 4469461d8837SThomas Graf { 4470461d8837SThomas Graf struct in6_addr *pfx = NULL; 4471461d8837SThomas Graf 4472caeaba79SNicolas Dichtel *peer_pfx = NULL; 4473caeaba79SNicolas Dichtel 4474461d8837SThomas Graf if (addr) 4475461d8837SThomas Graf pfx = nla_data(addr); 4476461d8837SThomas Graf 4477461d8837SThomas Graf if (local) { 4478461d8837SThomas Graf if (pfx && nla_memcmp(local, pfx, sizeof(*pfx))) 4479caeaba79SNicolas Dichtel *peer_pfx = pfx; 4480461d8837SThomas Graf pfx = nla_data(local); 4481461d8837SThomas Graf } 4482461d8837SThomas Graf 4483461d8837SThomas Graf return pfx; 4484461d8837SThomas Graf } 4485461d8837SThomas Graf 4486ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = { 4487461d8837SThomas Graf [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) }, 4488461d8837SThomas Graf [IFA_LOCAL] = { .len = sizeof(struct in6_addr) }, 4489461d8837SThomas Graf [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) }, 4490479840ffSJiri Pirko [IFA_FLAGS] = { .len = sizeof(u32) }, 44918308f3ffSDavid Ahern [IFA_RT_PRIORITY] = { .len = sizeof(u32) }, 4492461d8837SThomas Graf }; 4493461d8837SThomas Graf 44941da177e4SLinus Torvalds static int 4495c21ef3e3SDavid Ahern inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, 4496c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 44971da177e4SLinus Torvalds { 44983b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 4499b933f716SThomas Graf struct ifaddrmsg *ifm; 4500b933f716SThomas Graf struct nlattr *tb[IFA_MAX+1]; 4501caeaba79SNicolas Dichtel struct in6_addr *pfx, *peer_pfx; 45026046d5b4SHeiner Kallweit u32 ifa_flags; 4503b933f716SThomas Graf int err; 45041da177e4SLinus Torvalds 4505fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy, 4506c21ef3e3SDavid Ahern extack); 4507b933f716SThomas Graf if (err < 0) 4508b933f716SThomas Graf return err; 4509b933f716SThomas Graf 4510b933f716SThomas Graf ifm = nlmsg_data(nlh); 4511caeaba79SNicolas Dichtel pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); 451263159f29SIan Morris if (!pfx) 45131da177e4SLinus Torvalds return -EINVAL; 45141da177e4SLinus Torvalds 45156046d5b4SHeiner Kallweit ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags; 45166046d5b4SHeiner Kallweit 45176046d5b4SHeiner Kallweit /* We ignore other flags so far. */ 45186046d5b4SHeiner Kallweit ifa_flags &= IFA_F_MANAGETEMPADDR; 45196046d5b4SHeiner Kallweit 45206046d5b4SHeiner Kallweit return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx, 45216046d5b4SHeiner Kallweit ifm->ifa_prefixlen); 45221da177e4SLinus Torvalds } 45231da177e4SLinus Torvalds 45248308f3ffSDavid Ahern static int modify_prefix_route(struct inet6_ifaddr *ifp, 45258308f3ffSDavid Ahern unsigned long expires, u32 flags) 45268308f3ffSDavid Ahern { 45278308f3ffSDavid Ahern struct fib6_info *f6i; 4528e7c7faa9SDavid Ahern u32 prio; 45298308f3ffSDavid Ahern 45308308f3ffSDavid Ahern f6i = addrconf_get_prefix_route(&ifp->addr, 45318308f3ffSDavid Ahern ifp->prefix_len, 45328308f3ffSDavid Ahern ifp->idev->dev, 45338308f3ffSDavid Ahern 0, RTF_GATEWAY | RTF_DEFAULT); 45348308f3ffSDavid Ahern if (!f6i) 45358308f3ffSDavid Ahern return -ENOENT; 45368308f3ffSDavid Ahern 4537e7c7faa9SDavid Ahern prio = ifp->rt_priority ? : IP6_RT_PRIO_ADDRCONF; 4538e7c7faa9SDavid Ahern if (f6i->fib6_metric != prio) { 4539e7c7faa9SDavid Ahern /* delete old one */ 4540e7c7faa9SDavid Ahern ip6_del_rt(dev_net(ifp->idev->dev), f6i); 4541e7c7faa9SDavid Ahern 45428308f3ffSDavid Ahern /* add new one */ 45438308f3ffSDavid Ahern addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 45448308f3ffSDavid Ahern ifp->rt_priority, ifp->idev->dev, 45458308f3ffSDavid Ahern expires, flags, GFP_KERNEL); 45468308f3ffSDavid Ahern } else { 45478308f3ffSDavid Ahern if (!expires) 45488308f3ffSDavid Ahern fib6_clean_expires(f6i); 45498308f3ffSDavid Ahern else 45508308f3ffSDavid Ahern fib6_set_expires(f6i, expires); 45518308f3ffSDavid Ahern 45528308f3ffSDavid Ahern fib6_info_release(f6i); 45538308f3ffSDavid Ahern } 45548308f3ffSDavid Ahern 45558308f3ffSDavid Ahern return 0; 45568308f3ffSDavid Ahern } 45578308f3ffSDavid Ahern 4558d169a1f8SDavid Ahern static int inet6_addr_modify(struct inet6_ifaddr *ifp, struct ifa6_config *cfg) 4559081bba5bSNoriaki TAKAMIYA { 45606f704992SYOSHIFUJI Hideaki u32 flags; 45616f704992SYOSHIFUJI Hideaki clock_t expires; 45624bed72e4SYOSHIFUJI Hideaki unsigned long timeout; 456353bd6749SJiri Pirko bool was_managetempaddr; 45645b84efecSThomas Haller bool had_prefixroute; 456546d48046SYOSHIFUJI Hideaki 4566c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 4567c15b1ccaSHannes Frederic Sowa 4568d169a1f8SDavid Ahern if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft) 4569081bba5bSNoriaki TAKAMIYA return -EINVAL; 4570081bba5bSNoriaki TAKAMIYA 4571d169a1f8SDavid Ahern if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR && 457253bd6749SJiri Pirko (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64)) 457353bd6749SJiri Pirko return -EINVAL; 457453bd6749SJiri Pirko 4575f1c02cfbSSabrina Dubroca if (!(ifp->flags & IFA_F_TENTATIVE) || ifp->flags & IFA_F_DADFAILED) 4576d169a1f8SDavid Ahern cfg->ifa_flags &= ~IFA_F_OPTIMISTIC; 4577f1c02cfbSSabrina Dubroca 4578d169a1f8SDavid Ahern timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ); 45794bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 45804bed72e4SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(timeout * HZ); 4581d169a1f8SDavid Ahern cfg->valid_lft = timeout; 45826f704992SYOSHIFUJI Hideaki flags = RTF_EXPIRES; 45834bed72e4SYOSHIFUJI Hideaki } else { 45844bed72e4SYOSHIFUJI Hideaki expires = 0; 45854bed72e4SYOSHIFUJI Hideaki flags = 0; 4586d169a1f8SDavid Ahern cfg->ifa_flags |= IFA_F_PERMANENT; 45876f704992SYOSHIFUJI Hideaki } 4588081bba5bSNoriaki TAKAMIYA 4589d169a1f8SDavid Ahern timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ); 45904bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 45914bed72e4SYOSHIFUJI Hideaki if (timeout == 0) 4592d169a1f8SDavid Ahern cfg->ifa_flags |= IFA_F_DEPRECATED; 4593d169a1f8SDavid Ahern cfg->preferred_lft = timeout; 45944bed72e4SYOSHIFUJI Hideaki } 4595081bba5bSNoriaki TAKAMIYA 4596081bba5bSNoriaki TAKAMIYA spin_lock_bh(&ifp->lock); 459753bd6749SJiri Pirko was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR; 45985b84efecSThomas Haller had_prefixroute = ifp->flags & IFA_F_PERMANENT && 45995b84efecSThomas Haller !(ifp->flags & IFA_F_NOPREFIXROUTE); 460053bd6749SJiri Pirko ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | 4601761aac73SThomas Haller IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR | 4602761aac73SThomas Haller IFA_F_NOPREFIXROUTE); 4603d169a1f8SDavid Ahern ifp->flags |= cfg->ifa_flags; 4604081bba5bSNoriaki TAKAMIYA ifp->tstamp = jiffies; 4605d169a1f8SDavid Ahern ifp->valid_lft = cfg->valid_lft; 4606d169a1f8SDavid Ahern ifp->prefered_lft = cfg->preferred_lft; 4607081bba5bSNoriaki TAKAMIYA 46088308f3ffSDavid Ahern if (cfg->rt_priority && cfg->rt_priority != ifp->rt_priority) 46098308f3ffSDavid Ahern ifp->rt_priority = cfg->rt_priority; 4610081bba5bSNoriaki TAKAMIYA 4611081bba5bSNoriaki TAKAMIYA spin_unlock_bh(&ifp->lock); 4612081bba5bSNoriaki TAKAMIYA if (!(ifp->flags&IFA_F_TENTATIVE)) 4613081bba5bSNoriaki TAKAMIYA ipv6_ifa_notify(0, ifp); 4614081bba5bSNoriaki TAKAMIYA 4615d169a1f8SDavid Ahern if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) { 46168308f3ffSDavid Ahern int rc = -ENOENT; 46178308f3ffSDavid Ahern 46188308f3ffSDavid Ahern if (had_prefixroute) 46198308f3ffSDavid Ahern rc = modify_prefix_route(ifp, expires, flags); 46208308f3ffSDavid Ahern 46218308f3ffSDavid Ahern /* prefix route could have been deleted; if so restore it */ 46228308f3ffSDavid Ahern if (rc == -ENOENT) { 4623acb54e3cSDavid Ahern addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 46248308f3ffSDavid Ahern ifp->rt_priority, ifp->idev->dev, 46258308f3ffSDavid Ahern expires, flags, GFP_KERNEL); 46268308f3ffSDavid Ahern } 46275b84efecSThomas Haller } else if (had_prefixroute) { 46285b84efecSThomas Haller enum cleanup_prefix_rt_t action; 46295b84efecSThomas Haller unsigned long rt_expires; 46305b84efecSThomas Haller 46315b84efecSThomas Haller write_lock_bh(&ifp->idev->lock); 46325b84efecSThomas Haller action = check_cleanup_prefix_route(ifp, &rt_expires); 46335b84efecSThomas Haller write_unlock_bh(&ifp->idev->lock); 46345b84efecSThomas Haller 46355b84efecSThomas Haller if (action != CLEANUP_PREFIX_RT_NOP) { 46365b84efecSThomas Haller cleanup_prefix_route(ifp, rt_expires, 46375b84efecSThomas Haller action == CLEANUP_PREFIX_RT_DEL); 46385b84efecSThomas Haller } 4639761aac73SThomas Haller } 464053bd6749SJiri Pirko 464153bd6749SJiri Pirko if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) { 4642d169a1f8SDavid Ahern if (was_managetempaddr && 4643d169a1f8SDavid Ahern !(ifp->flags & IFA_F_MANAGETEMPADDR)) { 4644d169a1f8SDavid Ahern cfg->valid_lft = 0; 4645d169a1f8SDavid Ahern cfg->preferred_lft = 0; 4646d169a1f8SDavid Ahern } 4647d169a1f8SDavid Ahern manage_tempaddrs(ifp->idev, ifp, cfg->valid_lft, 4648d169a1f8SDavid Ahern cfg->preferred_lft, !was_managetempaddr, 4649d169a1f8SDavid Ahern jiffies); 465053bd6749SJiri Pirko } 465153bd6749SJiri Pirko 4652c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 4653081bba5bSNoriaki TAKAMIYA 4654081bba5bSNoriaki TAKAMIYA return 0; 4655081bba5bSNoriaki TAKAMIYA } 4656081bba5bSNoriaki TAKAMIYA 4657081bba5bSNoriaki TAKAMIYA static int 4658c21ef3e3SDavid Ahern inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, 4659c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 46601da177e4SLinus Torvalds { 46613b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 4662461d8837SThomas Graf struct ifaddrmsg *ifm; 4663461d8837SThomas Graf struct nlattr *tb[IFA_MAX+1]; 466419b1518cSDavid Ahern struct in6_addr *peer_pfx; 46657198f8ceSThomas Graf struct inet6_ifaddr *ifa; 46667198f8ceSThomas Graf struct net_device *dev; 4667f1c02cfbSSabrina Dubroca struct inet6_dev *idev; 466819b1518cSDavid Ahern struct ifa6_config cfg; 4669461d8837SThomas Graf int err; 46701da177e4SLinus Torvalds 4671fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy, 4672c21ef3e3SDavid Ahern extack); 4673461d8837SThomas Graf if (err < 0) 4674461d8837SThomas Graf return err; 4675461d8837SThomas Graf 467619b1518cSDavid Ahern memset(&cfg, 0, sizeof(cfg)); 467719b1518cSDavid Ahern 4678461d8837SThomas Graf ifm = nlmsg_data(nlh); 467919b1518cSDavid Ahern cfg.pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); 468019b1518cSDavid Ahern if (!cfg.pfx) 46811da177e4SLinus Torvalds return -EINVAL; 46821da177e4SLinus Torvalds 468319b1518cSDavid Ahern cfg.peer_pfx = peer_pfx; 468419b1518cSDavid Ahern cfg.plen = ifm->ifa_prefixlen; 46858308f3ffSDavid Ahern if (tb[IFA_RT_PRIORITY]) 46868308f3ffSDavid Ahern cfg.rt_priority = nla_get_u32(tb[IFA_RT_PRIORITY]); 46878308f3ffSDavid Ahern 468819b1518cSDavid Ahern cfg.valid_lft = INFINITY_LIFE_TIME; 468919b1518cSDavid Ahern cfg.preferred_lft = INFINITY_LIFE_TIME; 469019b1518cSDavid Ahern 4691461d8837SThomas Graf if (tb[IFA_CACHEINFO]) { 46920778769dSNoriaki TAKAMIYA struct ifa_cacheinfo *ci; 4693461d8837SThomas Graf 4694461d8837SThomas Graf ci = nla_data(tb[IFA_CACHEINFO]); 469519b1518cSDavid Ahern cfg.valid_lft = ci->ifa_valid; 469619b1518cSDavid Ahern cfg.preferred_lft = ci->ifa_prefered; 46970778769dSNoriaki TAKAMIYA } 46980778769dSNoriaki TAKAMIYA 4699af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifm->ifa_index); 470063159f29SIan Morris if (!dev) 47017198f8ceSThomas Graf return -ENODEV; 4702081bba5bSNoriaki TAKAMIYA 470319b1518cSDavid Ahern if (tb[IFA_FLAGS]) 470419b1518cSDavid Ahern cfg.ifa_flags = nla_get_u32(tb[IFA_FLAGS]); 470519b1518cSDavid Ahern else 470619b1518cSDavid Ahern cfg.ifa_flags = ifm->ifa_flags; 4707479840ffSJiri Pirko 470855ebaef1SNoriaki TAKAMIYA /* We ignore other flags so far. */ 470919b1518cSDavid Ahern cfg.ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | 471019b1518cSDavid Ahern IFA_F_MANAGETEMPADDR | IFA_F_NOPREFIXROUTE | 471119b1518cSDavid Ahern IFA_F_MCAUTOJOIN | IFA_F_OPTIMISTIC; 4712f1c02cfbSSabrina Dubroca 4713f1c02cfbSSabrina Dubroca idev = ipv6_find_idev(dev); 4714f1c02cfbSSabrina Dubroca if (IS_ERR(idev)) 4715f1c02cfbSSabrina Dubroca return PTR_ERR(idev); 4716f1c02cfbSSabrina Dubroca 4717f1c02cfbSSabrina Dubroca if (!ipv6_allow_optimistic_dad(net, idev)) 471819b1518cSDavid Ahern cfg.ifa_flags &= ~IFA_F_OPTIMISTIC; 4719f1c02cfbSSabrina Dubroca 472019b1518cSDavid Ahern if (cfg.ifa_flags & IFA_F_NODAD && 472119b1518cSDavid Ahern cfg.ifa_flags & IFA_F_OPTIMISTIC) { 4722f1c02cfbSSabrina Dubroca NL_SET_ERR_MSG(extack, "IFA_F_NODAD and IFA_F_OPTIMISTIC are mutually exclusive"); 4723f1c02cfbSSabrina Dubroca return -EINVAL; 4724f1c02cfbSSabrina Dubroca } 472555ebaef1SNoriaki TAKAMIYA 472619b1518cSDavid Ahern ifa = ipv6_get_ifaddr(net, cfg.pfx, dev, 1); 472763159f29SIan Morris if (!ifa) { 47287198f8ceSThomas Graf /* 47297198f8ceSThomas Graf * It would be best to check for !NLM_F_CREATE here but 4730db9c7c39Sstephen hemminger * userspace already relies on not having to provide this. 47317198f8ceSThomas Graf */ 473219b1518cSDavid Ahern return inet6_addr_add(net, ifm->ifa_index, &cfg, extack); 47331da177e4SLinus Torvalds } 47341da177e4SLinus Torvalds 47357198f8ceSThomas Graf if (nlh->nlmsg_flags & NLM_F_EXCL || 47367198f8ceSThomas Graf !(nlh->nlmsg_flags & NLM_F_REPLACE)) 47377198f8ceSThomas Graf err = -EEXIST; 47387198f8ceSThomas Graf else 4739d169a1f8SDavid Ahern err = inet6_addr_modify(ifa, &cfg); 47407198f8ceSThomas Graf 47417198f8ceSThomas Graf in6_ifa_put(ifa); 47427198f8ceSThomas Graf 47437198f8ceSThomas Graf return err; 47447198f8ceSThomas Graf } 47457198f8ceSThomas Graf 4746479840ffSJiri Pirko static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags, 4747101bb229SThomas Graf u8 scope, int ifindex) 4748101bb229SThomas Graf { 4749101bb229SThomas Graf struct ifaddrmsg *ifm; 4750101bb229SThomas Graf 4751101bb229SThomas Graf ifm = nlmsg_data(nlh); 4752101bb229SThomas Graf ifm->ifa_family = AF_INET6; 4753101bb229SThomas Graf ifm->ifa_prefixlen = prefixlen; 4754101bb229SThomas Graf ifm->ifa_flags = flags; 4755101bb229SThomas Graf ifm->ifa_scope = scope; 4756101bb229SThomas Graf ifm->ifa_index = ifindex; 4757101bb229SThomas Graf } 4758101bb229SThomas Graf 475985486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp, 476085486af0SThomas Graf unsigned long tstamp, u32 preferred, u32 valid) 476185486af0SThomas Graf { 476285486af0SThomas Graf struct ifa_cacheinfo ci; 476385486af0SThomas Graf 476418a31e1eSThomas Graf ci.cstamp = cstamp_delta(cstamp); 476518a31e1eSThomas Graf ci.tstamp = cstamp_delta(tstamp); 476685486af0SThomas Graf ci.ifa_prefered = preferred; 476785486af0SThomas Graf ci.ifa_valid = valid; 476885486af0SThomas Graf 476985486af0SThomas Graf return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci); 477085486af0SThomas Graf } 477185486af0SThomas Graf 4772101bb229SThomas Graf static inline int rt_scope(int ifa_scope) 4773101bb229SThomas Graf { 4774101bb229SThomas Graf if (ifa_scope & IFA_HOST) 4775101bb229SThomas Graf return RT_SCOPE_HOST; 4776101bb229SThomas Graf else if (ifa_scope & IFA_LINK) 4777101bb229SThomas Graf return RT_SCOPE_LINK; 4778101bb229SThomas Graf else if (ifa_scope & IFA_SITE) 4779101bb229SThomas Graf return RT_SCOPE_SITE; 4780101bb229SThomas Graf else 4781101bb229SThomas Graf return RT_SCOPE_UNIVERSE; 4782101bb229SThomas Graf } 4783101bb229SThomas Graf 47840ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void) 47850ab6803bSThomas Graf { 4786339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifaddrmsg)) 4787caeaba79SNicolas Dichtel + nla_total_size(16) /* IFA_LOCAL */ 4788339bf98fSThomas Graf + nla_total_size(16) /* IFA_ADDRESS */ 4789479840ffSJiri Pirko + nla_total_size(sizeof(struct ifa_cacheinfo)) 47908308f3ffSDavid Ahern + nla_total_size(4) /* IFA_FLAGS */ 47918308f3ffSDavid Ahern + nla_total_size(4) /* IFA_RT_PRIORITY */; 47920ab6803bSThomas Graf } 4793c5396a31SYOSHIFUJI Hideaki 47941da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa, 479515e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 47961da177e4SLinus Torvalds { 47971da177e4SLinus Torvalds struct nlmsghdr *nlh; 479885486af0SThomas Graf u32 preferred, valid; 47991da177e4SLinus Torvalds 480015e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 480163159f29SIan Morris if (!nlh) 480226932566SPatrick McHardy return -EMSGSIZE; 48030ab6803bSThomas Graf 4804101bb229SThomas Graf put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope), 4805101bb229SThomas Graf ifa->idev->dev->ifindex); 4806101bb229SThomas Graf 4807fad8da3eSYasushi Asano if (!((ifa->flags&IFA_F_PERMANENT) && 4808fad8da3eSYasushi Asano (ifa->prefered_lft == INFINITY_LIFE_TIME))) { 480985486af0SThomas Graf preferred = ifa->prefered_lft; 481085486af0SThomas Graf valid = ifa->valid_lft; 481185486af0SThomas Graf if (preferred != INFINITY_LIFE_TIME) { 48121da177e4SLinus Torvalds long tval = (jiffies - ifa->tstamp)/HZ; 4813a1faa698SJens Rosenboom if (preferred > tval) 481485486af0SThomas Graf preferred -= tval; 4815a1faa698SJens Rosenboom else 4816a1faa698SJens Rosenboom preferred = 0; 4817f56619fcSBen Hutchings if (valid != INFINITY_LIFE_TIME) { 4818f56619fcSBen Hutchings if (valid > tval) 481985486af0SThomas Graf valid -= tval; 4820f56619fcSBen Hutchings else 4821f56619fcSBen Hutchings valid = 0; 4822f56619fcSBen Hutchings } 48231da177e4SLinus Torvalds } 48241da177e4SLinus Torvalds } else { 482585486af0SThomas Graf preferred = INFINITY_LIFE_TIME; 482685486af0SThomas Graf valid = INFINITY_LIFE_TIME; 48271da177e4SLinus Torvalds } 482885486af0SThomas Graf 48297996c799SCong Wang if (!ipv6_addr_any(&ifa->peer_addr)) { 4830930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 || 4831930345eaSJiri Benc nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0) 4832caeaba79SNicolas Dichtel goto error; 4833caeaba79SNicolas Dichtel } else 4834930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0) 4835caeaba79SNicolas Dichtel goto error; 4836caeaba79SNicolas Dichtel 48378308f3ffSDavid Ahern if (ifa->rt_priority && 48388308f3ffSDavid Ahern nla_put_u32(skb, IFA_RT_PRIORITY, ifa->rt_priority)) 48398308f3ffSDavid Ahern goto error; 48408308f3ffSDavid Ahern 4841caeaba79SNicolas Dichtel if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) 4842caeaba79SNicolas Dichtel goto error; 484385486af0SThomas Graf 4844479840ffSJiri Pirko if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0) 4845479840ffSJiri Pirko goto error; 4846479840ffSJiri Pirko 4847053c095aSJohannes Berg nlmsg_end(skb, nlh); 4848053c095aSJohannes Berg return 0; 4849caeaba79SNicolas Dichtel 4850caeaba79SNicolas Dichtel error: 4851caeaba79SNicolas Dichtel nlmsg_cancel(skb, nlh); 4852caeaba79SNicolas Dichtel return -EMSGSIZE; 48531da177e4SLinus Torvalds } 48541da177e4SLinus Torvalds 48551da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca, 485615e47304SEric W. Biederman u32 portid, u32 seq, int event, u16 flags) 48571da177e4SLinus Torvalds { 48581da177e4SLinus Torvalds struct nlmsghdr *nlh; 4859101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 4860101bb229SThomas Graf int ifindex = ifmca->idev->dev->ifindex; 48611da177e4SLinus Torvalds 48621da177e4SLinus Torvalds if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE) 4863101bb229SThomas Graf scope = RT_SCOPE_SITE; 4864101bb229SThomas Graf 486515e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 486663159f29SIan Morris if (!nlh) 486726932566SPatrick McHardy return -EMSGSIZE; 48680ab6803bSThomas Graf 4869101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 4870930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 || 48710ab6803bSThomas Graf put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp, 487226932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 487326932566SPatrick McHardy nlmsg_cancel(skb, nlh); 487426932566SPatrick McHardy return -EMSGSIZE; 487526932566SPatrick McHardy } 487685486af0SThomas Graf 4877053c095aSJohannes Berg nlmsg_end(skb, nlh); 4878053c095aSJohannes Berg return 0; 48791da177e4SLinus Torvalds } 48801da177e4SLinus Torvalds 48811da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca, 488215e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 48831da177e4SLinus Torvalds { 48849ee8cbb2SDavid Ahern struct net_device *dev = fib6_info_nh_dev(ifaca->aca_rt); 48859ee8cbb2SDavid Ahern int ifindex = dev ? dev->ifindex : 1; 48861da177e4SLinus Torvalds struct nlmsghdr *nlh; 4887101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 48881da177e4SLinus Torvalds 48891da177e4SLinus Torvalds if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE) 4890101bb229SThomas Graf scope = RT_SCOPE_SITE; 4891101bb229SThomas Graf 489215e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 489363159f29SIan Morris if (!nlh) 489426932566SPatrick McHardy return -EMSGSIZE; 48950ab6803bSThomas Graf 4896101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 4897930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 || 48980ab6803bSThomas Graf put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp, 489926932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 490026932566SPatrick McHardy nlmsg_cancel(skb, nlh); 490126932566SPatrick McHardy return -EMSGSIZE; 490226932566SPatrick McHardy } 490385486af0SThomas Graf 4904053c095aSJohannes Berg nlmsg_end(skb, nlh); 4905053c095aSJohannes Berg return 0; 49061da177e4SLinus Torvalds } 49071da177e4SLinus Torvalds 4908e21e8467SStephen Hemminger enum addr_type_t { 49091da177e4SLinus Torvalds UNICAST_ADDR, 49101da177e4SLinus Torvalds MULTICAST_ADDR, 49111da177e4SLinus Torvalds ANYCAST_ADDR, 49121da177e4SLinus Torvalds }; 49131da177e4SLinus Torvalds 4914234b27c3SEric Dumazet /* called with rcu_read_lock() */ 4915234b27c3SEric Dumazet static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb, 4916234b27c3SEric Dumazet struct netlink_callback *cb, enum addr_type_t type, 4917234b27c3SEric Dumazet int s_ip_idx, int *p_ip_idx) 49181da177e4SLinus Torvalds { 49191da177e4SLinus Torvalds struct ifmcaddr6 *ifmca; 49201da177e4SLinus Torvalds struct ifacaddr6 *ifaca; 4921234b27c3SEric Dumazet int err = 1; 4922234b27c3SEric Dumazet int ip_idx = *p_ip_idx; 49231da177e4SLinus Torvalds 49241da177e4SLinus Torvalds read_lock_bh(&idev->lock); 49251da177e4SLinus Torvalds switch (type) { 4926502a2ffdSstephen hemminger case UNICAST_ADDR: { 4927502a2ffdSstephen hemminger struct inet6_ifaddr *ifa; 4928502a2ffdSstephen hemminger 4929ae9cda5dSYOSHIFUJI Hideaki /* unicast address incl. temp addr */ 4930502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 4931502a2ffdSstephen hemminger if (++ip_idx < s_ip_idx) 49321da177e4SLinus Torvalds continue; 49335d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifaddr(skb, ifa, 493415e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 49355d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 49365d5619b4SYOSHIFUJI Hideaki RTM_NEWADDR, 49375d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 4938053c095aSJohannes Berg if (err < 0) 4939bcd32326SEric Dumazet break; 494063998ac2SNicolas Dichtel nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 49411da177e4SLinus Torvalds } 49421da177e4SLinus Torvalds break; 4943502a2ffdSstephen hemminger } 49441da177e4SLinus Torvalds case MULTICAST_ADDR: 49451da177e4SLinus Torvalds /* multicast address */ 49461da177e4SLinus Torvalds for (ifmca = idev->mc_list; ifmca; 49471da177e4SLinus Torvalds ifmca = ifmca->next, ip_idx++) { 49481da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 49491da177e4SLinus Torvalds continue; 49505d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifmcaddr(skb, ifmca, 495115e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 49525d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 49535d5619b4SYOSHIFUJI Hideaki RTM_GETMULTICAST, 49545d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 4955053c095aSJohannes Berg if (err < 0) 4956bcd32326SEric Dumazet break; 49571da177e4SLinus Torvalds } 49581da177e4SLinus Torvalds break; 49591da177e4SLinus Torvalds case ANYCAST_ADDR: 49601da177e4SLinus Torvalds /* anycast address */ 49611da177e4SLinus Torvalds for (ifaca = idev->ac_list; ifaca; 49621da177e4SLinus Torvalds ifaca = ifaca->aca_next, ip_idx++) { 49631da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 49641da177e4SLinus Torvalds continue; 49655d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifacaddr(skb, ifaca, 496615e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 49675d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 49685d5619b4SYOSHIFUJI Hideaki RTM_GETANYCAST, 49695d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 4970053c095aSJohannes Berg if (err < 0) 4971bcd32326SEric Dumazet break; 49721da177e4SLinus Torvalds } 49731da177e4SLinus Torvalds break; 49741da177e4SLinus Torvalds default: 49751da177e4SLinus Torvalds break; 49761da177e4SLinus Torvalds } 49771da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 4978234b27c3SEric Dumazet *p_ip_idx = ip_idx; 4979234b27c3SEric Dumazet return err; 4980234b27c3SEric Dumazet } 49815d5619b4SYOSHIFUJI Hideaki 4982234b27c3SEric Dumazet static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb, 4983234b27c3SEric Dumazet enum addr_type_t type) 4984234b27c3SEric Dumazet { 4985234b27c3SEric Dumazet struct net *net = sock_net(skb->sk); 4986234b27c3SEric Dumazet int h, s_h; 4987234b27c3SEric Dumazet int idx, ip_idx; 4988234b27c3SEric Dumazet int s_idx, s_ip_idx; 4989234b27c3SEric Dumazet struct net_device *dev; 4990234b27c3SEric Dumazet struct inet6_dev *idev; 4991234b27c3SEric Dumazet struct hlist_head *head; 4992234b27c3SEric Dumazet 4993234b27c3SEric Dumazet s_h = cb->args[0]; 4994234b27c3SEric Dumazet s_idx = idx = cb->args[1]; 4995234b27c3SEric Dumazet s_ip_idx = ip_idx = cb->args[2]; 4996234b27c3SEric Dumazet 4997234b27c3SEric Dumazet rcu_read_lock(); 499863998ac2SNicolas Dichtel cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq; 4999234b27c3SEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 5000234b27c3SEric Dumazet idx = 0; 5001234b27c3SEric Dumazet head = &net->dev_index_head[h]; 5002b67bfe0dSSasha Levin hlist_for_each_entry_rcu(dev, head, index_hlist) { 5003234b27c3SEric Dumazet if (idx < s_idx) 5004234b27c3SEric Dumazet goto cont; 50054b97efdfSPatrick McHardy if (h > s_h || idx > s_idx) 5006234b27c3SEric Dumazet s_ip_idx = 0; 5007234b27c3SEric Dumazet ip_idx = 0; 5008e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 5009e21e8467SStephen Hemminger if (!idev) 5010234b27c3SEric Dumazet goto cont; 5011234b27c3SEric Dumazet 5012234b27c3SEric Dumazet if (in6_dump_addrs(idev, skb, cb, type, 50137b46a644SDavid S. Miller s_ip_idx, &ip_idx) < 0) 5014234b27c3SEric Dumazet goto done; 50157562f876SPavel Emelianov cont: 50167562f876SPavel Emelianov idx++; 50171da177e4SLinus Torvalds } 5018234b27c3SEric Dumazet } 5019234b27c3SEric Dumazet done: 5020234b27c3SEric Dumazet rcu_read_unlock(); 5021234b27c3SEric Dumazet cb->args[0] = h; 5022234b27c3SEric Dumazet cb->args[1] = idx; 5023234b27c3SEric Dumazet cb->args[2] = ip_idx; 5024234b27c3SEric Dumazet 50251da177e4SLinus Torvalds return skb->len; 50261da177e4SLinus Torvalds } 50271da177e4SLinus Torvalds 50281da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) 50291da177e4SLinus Torvalds { 50301da177e4SLinus Torvalds enum addr_type_t type = UNICAST_ADDR; 5031b854272bSDenis V. Lunev 50321da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 50331da177e4SLinus Torvalds } 50341da177e4SLinus Torvalds 50351da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb) 50361da177e4SLinus Torvalds { 50371da177e4SLinus Torvalds enum addr_type_t type = MULTICAST_ADDR; 5038b854272bSDenis V. Lunev 50391da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 50401da177e4SLinus Torvalds } 50411da177e4SLinus Torvalds 50421da177e4SLinus Torvalds 50431da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb) 50441da177e4SLinus Torvalds { 50451da177e4SLinus Torvalds enum addr_type_t type = ANYCAST_ADDR; 5046b854272bSDenis V. Lunev 50471da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 50481da177e4SLinus Torvalds } 50491da177e4SLinus Torvalds 5050c21ef3e3SDavid Ahern static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh, 5051c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 50526c223828SNoriaki TAKAMIYA { 50533b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 50541b29fc2cSThomas Graf struct ifaddrmsg *ifm; 50551b29fc2cSThomas Graf struct nlattr *tb[IFA_MAX+1]; 5056caeaba79SNicolas Dichtel struct in6_addr *addr = NULL, *peer; 50576c223828SNoriaki TAKAMIYA struct net_device *dev = NULL; 50586c223828SNoriaki TAKAMIYA struct inet6_ifaddr *ifa; 50596c223828SNoriaki TAKAMIYA struct sk_buff *skb; 50606c223828SNoriaki TAKAMIYA int err; 50616c223828SNoriaki TAKAMIYA 5062fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy, 5063c21ef3e3SDavid Ahern extack); 50641b29fc2cSThomas Graf if (err < 0) 5065c24675f8SFlorian Westphal return err; 50666c223828SNoriaki TAKAMIYA 5067caeaba79SNicolas Dichtel addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer); 5068c24675f8SFlorian Westphal if (!addr) 5069c24675f8SFlorian Westphal return -EINVAL; 50701b29fc2cSThomas Graf 50711b29fc2cSThomas Graf ifm = nlmsg_data(nlh); 50726c223828SNoriaki TAKAMIYA if (ifm->ifa_index) 5073c24675f8SFlorian Westphal dev = dev_get_by_index(net, ifm->ifa_index); 50746c223828SNoriaki TAKAMIYA 5075e21e8467SStephen Hemminger ifa = ipv6_get_ifaddr(net, addr, dev, 1); 5076e21e8467SStephen Hemminger if (!ifa) { 50771b29fc2cSThomas Graf err = -EADDRNOTAVAIL; 50781b29fc2cSThomas Graf goto errout; 50796c223828SNoriaki TAKAMIYA } 50806c223828SNoriaki TAKAMIYA 5081e21e8467SStephen Hemminger skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL); 5082e21e8467SStephen Hemminger if (!skb) { 50831b29fc2cSThomas Graf err = -ENOBUFS; 50841b29fc2cSThomas Graf goto errout_ifa; 50851b29fc2cSThomas Graf } 50861b29fc2cSThomas Graf 508715e47304SEric W. Biederman err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid, 50886c223828SNoriaki TAKAMIYA nlh->nlmsg_seq, RTM_NEWADDR, 0); 508926932566SPatrick McHardy if (err < 0) { 509026932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 509126932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 509226932566SPatrick McHardy kfree_skb(skb); 509326932566SPatrick McHardy goto errout_ifa; 509426932566SPatrick McHardy } 509515e47304SEric W. Biederman err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 50961b29fc2cSThomas Graf errout_ifa: 50976c223828SNoriaki TAKAMIYA in6_ifa_put(ifa); 50981b29fc2cSThomas Graf errout: 5099c24675f8SFlorian Westphal if (dev) 5100c24675f8SFlorian Westphal dev_put(dev); 51016c223828SNoriaki TAKAMIYA return err; 51026c223828SNoriaki TAKAMIYA } 51036c223828SNoriaki TAKAMIYA 51041da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa) 51051da177e4SLinus Torvalds { 51061da177e4SLinus Torvalds struct sk_buff *skb; 5107c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(ifa->idev->dev); 51085d620266SThomas Graf int err = -ENOBUFS; 51091da177e4SLinus Torvalds 51100ab6803bSThomas Graf skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC); 511163159f29SIan Morris if (!skb) 51125d620266SThomas Graf goto errout; 51135d620266SThomas Graf 51145d620266SThomas Graf err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0); 511526932566SPatrick McHardy if (err < 0) { 511626932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 511726932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 511826932566SPatrick McHardy kfree_skb(skb); 511926932566SPatrick McHardy goto errout; 512026932566SPatrick McHardy } 51211ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC); 51221ce85fe4SPablo Neira Ayuso return; 51235d620266SThomas Graf errout: 51245d620266SThomas Graf if (err < 0) 51256fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err); 51261da177e4SLinus Torvalds } 51271da177e4SLinus Torvalds 5128b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf, 51291da177e4SLinus Torvalds __s32 *array, int bytes) 51301da177e4SLinus Torvalds { 513104561c1fSThomas Graf BUG_ON(bytes < (DEVCONF_MAX * 4)); 513204561c1fSThomas Graf 51331da177e4SLinus Torvalds memset(array, 0, bytes); 51341da177e4SLinus Torvalds array[DEVCONF_FORWARDING] = cnf->forwarding; 51351da177e4SLinus Torvalds array[DEVCONF_HOPLIMIT] = cnf->hop_limit; 51361da177e4SLinus Torvalds array[DEVCONF_MTU6] = cnf->mtu6; 51371da177e4SLinus Torvalds array[DEVCONF_ACCEPT_RA] = cnf->accept_ra; 51381da177e4SLinus Torvalds array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects; 51391da177e4SLinus Torvalds array[DEVCONF_AUTOCONF] = cnf->autoconf; 51401da177e4SLinus Torvalds array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits; 51411da177e4SLinus Torvalds array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits; 514293908d19SThomas Graf array[DEVCONF_RTR_SOLICIT_INTERVAL] = 514393908d19SThomas Graf jiffies_to_msecs(cnf->rtr_solicit_interval); 5144bd11f074SMaciej Żenczykowski array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] = 5145bd11f074SMaciej Żenczykowski jiffies_to_msecs(cnf->rtr_solicit_max_interval); 514693908d19SThomas Graf array[DEVCONF_RTR_SOLICIT_DELAY] = 514793908d19SThomas Graf jiffies_to_msecs(cnf->rtr_solicit_delay); 51481da177e4SLinus Torvalds array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version; 5149fc4eba58SHannes Frederic Sowa array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] = 5150fc4eba58SHannes Frederic Sowa jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval); 5151fc4eba58SHannes Frederic Sowa array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] = 5152fc4eba58SHannes Frederic Sowa jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval); 51531da177e4SLinus Torvalds array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr; 51541da177e4SLinus Torvalds array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft; 51551da177e4SLinus Torvalds array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft; 51561da177e4SLinus Torvalds array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry; 51571da177e4SLinus Torvalds array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor; 51581da177e4SLinus Torvalds array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses; 515965f5c7c1SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr; 51608013d1d7SHangbin Liu array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit; 5161c4fd30ebSYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo; 5162930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 5163930d6ff2SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref; 516493908d19SThomas Graf array[DEVCONF_RTR_PROBE_INTERVAL] = 516593908d19SThomas Graf jiffies_to_msecs(cnf->rtr_probe_interval); 5166fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 5167bbea124bSJoel Scherpelz array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] = cnf->accept_ra_rt_info_min_plen; 516809c884d4SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen; 516909c884d4SYOSHIFUJI Hideaki #endif 5170930d6ff2SYOSHIFUJI Hideaki #endif 5171fbea49e1SYOSHIFUJI Hideaki array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp; 51720bcbc926SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route; 517395c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 517495c385b4SNeil Horman array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad; 51757fd2561eSErik Kline array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic; 517695c385b4SNeil Horman #endif 51777bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 51787bc570c8SYOSHIFUJI Hideaki array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding; 51797bc570c8SYOSHIFUJI Hideaki #endif 5180778d80beSYOSHIFUJI Hideaki array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6; 51811b34be74SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad; 5182c3faca05SCosmin Ratiu array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao; 51835cb04436SHannes Frederic Sowa array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify; 5184b800c3b9SHannes Frederic Sowa array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc; 5185d9333196SBen Greear array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local; 5186c2943f14SHarout Hedeshian array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu; 518735103d11SAndy Gospodarek array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown; 51883d1bec99SHannes Frederic Sowa /* we omit DEVCONF_STABLE_SECRET for now */ 51893985e8a3SErik Kline array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only; 5190abbc3043SJohannes Berg array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast; 51917a02bf89SJohannes Berg array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na; 5192f1705ec1SDavid Ahern array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down; 51931ababebaSDavid Lebrun array[DEVCONF_SEG6_ENABLED] = cnf->seg6_enabled; 5194bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC 5195bf355b8dSDavid Lebrun array[DEVCONF_SEG6_REQUIRE_HMAC] = cnf->seg6_require_hmac; 5196bf355b8dSDavid Lebrun #endif 5197adc176c5SErik Nordmark array[DEVCONF_ENHANCED_DAD] = cnf->enhanced_dad; 5198d35a00b8SFelix Jia array[DEVCONF_ADDR_GEN_MODE] = cnf->addr_gen_mode; 5199df789fe7SDavid Forster array[DEVCONF_DISABLE_POLICY] = cnf->disable_policy; 52002210d6b2SMaciej Żenczykowski array[DEVCONF_NDISC_TCLASS] = cnf->ndisc_tclass; 52011da177e4SLinus Torvalds } 52021da177e4SLinus Torvalds 5203b382b191SThomas Graf static inline size_t inet6_ifla6_size(void) 5204b382b191SThomas Graf { 5205b382b191SThomas Graf return nla_total_size(4) /* IFLA_INET6_FLAGS */ 5206b382b191SThomas Graf + nla_total_size(sizeof(struct ifla_cacheinfo)) 5207b382b191SThomas Graf + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */ 5208b382b191SThomas Graf + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */ 5209f53adae4SDaniel Borkmann + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */ 5210bdd72f41SSabrina Dubroca + nla_total_size(sizeof(struct in6_addr)) /* IFLA_INET6_TOKEN */ 5211bdd72f41SSabrina Dubroca + nla_total_size(1) /* IFLA_INET6_ADDR_GEN_MODE */ 5212bdd72f41SSabrina Dubroca + 0; 5213b382b191SThomas Graf } 5214b382b191SThomas Graf 5215339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void) 5216339bf98fSThomas Graf { 5217339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 5218339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 5219339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 5220339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 5221339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 52220344338bSAndy Gospodarek + nla_total_size(1) /* IFLA_OPERSTATE */ 5223b382b191SThomas Graf + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */ 5224339bf98fSThomas Graf } 5225c5396a31SYOSHIFUJI Hideaki 5226be281e55SEric Dumazet static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib, 5227aca05671SJia He int bytes) 52287f7d9a6bSHerbert Xu { 52297f7d9a6bSHerbert Xu int i; 5230aca05671SJia He int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX; 52317f7d9a6bSHerbert Xu BUG_ON(pad < 0); 52327f7d9a6bSHerbert Xu 52337f7d9a6bSHerbert Xu /* Use put_unaligned() because stats may not be aligned for u64. */ 5234aca05671SJia He put_unaligned(ICMP6_MIB_MAX, &stats[0]); 5235aca05671SJia He for (i = 1; i < ICMP6_MIB_MAX; i++) 5236be281e55SEric Dumazet put_unaligned(atomic_long_read(&mib[i]), &stats[i]); 52377f7d9a6bSHerbert Xu 5238aca05671SJia He memset(&stats[ICMP6_MIB_MAX], 0, pad); 52397f7d9a6bSHerbert Xu } 52407f7d9a6bSHerbert Xu 5241698365faSWANG Cong static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib, 5242a3a77372SRaghavendra K T int bytes, size_t syncpoff) 52434ce3c183SEric Dumazet { 5244a3a77372SRaghavendra K T int i, c; 5245a3a77372SRaghavendra K T u64 buff[IPSTATS_MIB_MAX]; 5246a3a77372SRaghavendra K T int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX; 5247a3a77372SRaghavendra K T 52484ce3c183SEric Dumazet BUG_ON(pad < 0); 52494ce3c183SEric Dumazet 5250a3a77372SRaghavendra K T memset(buff, 0, sizeof(buff)); 5251a3a77372SRaghavendra K T buff[0] = IPSTATS_MIB_MAX; 52524ce3c183SEric Dumazet 5253a3a77372SRaghavendra K T for_each_possible_cpu(c) { 5254a3a77372SRaghavendra K T for (i = 1; i < IPSTATS_MIB_MAX; i++) 5255a3a77372SRaghavendra K T buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff); 5256a3a77372SRaghavendra K T } 5257a3a77372SRaghavendra K T 5258a3a77372SRaghavendra K T memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64)); 5259a3a77372SRaghavendra K T memset(&stats[IPSTATS_MIB_MAX], 0, pad); 52604ce3c183SEric Dumazet } 52614ce3c183SEric Dumazet 52627f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype, 52637f7d9a6bSHerbert Xu int bytes) 52647f7d9a6bSHerbert Xu { 52657f7d9a6bSHerbert Xu switch (attrtype) { 52667f7d9a6bSHerbert Xu case IFLA_INET6_STATS: 5267a3a77372SRaghavendra K T __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes, 5268a3a77372SRaghavendra K T offsetof(struct ipstats_mib, syncp)); 52697f7d9a6bSHerbert Xu break; 52707f7d9a6bSHerbert Xu case IFLA_INET6_ICMP6STATS: 5271aca05671SJia He __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes); 52727f7d9a6bSHerbert Xu break; 52737f7d9a6bSHerbert Xu } 52747f7d9a6bSHerbert Xu } 52757f7d9a6bSHerbert Xu 5276d5566fd7SSowmini Varadhan static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev, 5277d5566fd7SSowmini Varadhan u32 ext_filter_mask) 52781da177e4SLinus Torvalds { 5279bf99f1bdSYOSHIFUJI Hideaki struct nlattr *nla; 52801da177e4SLinus Torvalds struct ifla_cacheinfo ci; 52811da177e4SLinus Torvalds 5282c78679e8SDavid S. Miller if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags)) 5283c78679e8SDavid S. Miller goto nla_put_failure; 52841da177e4SLinus Torvalds ci.max_reasm_len = IPV6_MAXPLEN; 528524912420SDavid S. Miller ci.tstamp = cstamp_delta(idev->tstamp); 528624912420SDavid S. Miller ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time); 52871f9248e5SJiri Pirko ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME)); 5288c78679e8SDavid S. Miller if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci)) 5289c78679e8SDavid S. Miller goto nla_put_failure; 5290bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32)); 529163159f29SIan Morris if (!nla) 529204561c1fSThomas Graf goto nla_put_failure; 5293bf99f1bdSYOSHIFUJI Hideaki ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla)); 52941da177e4SLinus Torvalds 5295bf99f1bdSYOSHIFUJI Hideaki /* XXX - MC not implemented */ 5296bf99f1bdSYOSHIFUJI Hideaki 5297d5566fd7SSowmini Varadhan if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS) 5298d5566fd7SSowmini Varadhan return 0; 5299d5566fd7SSowmini Varadhan 5300bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64)); 530163159f29SIan Morris if (!nla) 5302bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 5303bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla)); 5304bf99f1bdSYOSHIFUJI Hideaki 5305bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64)); 530663159f29SIan Morris if (!nla) 5307bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 5308bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla)); 53091da177e4SLinus Torvalds 5310f53adae4SDaniel Borkmann nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr)); 531163159f29SIan Morris if (!nla) 5312f53adae4SDaniel Borkmann goto nla_put_failure; 5313bc91b0f0SJiri Pirko 5314d35a00b8SFelix Jia if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->cnf.addr_gen_mode)) 5315bc91b0f0SJiri Pirko goto nla_put_failure; 5316bc91b0f0SJiri Pirko 5317f53adae4SDaniel Borkmann read_lock_bh(&idev->lock); 5318f53adae4SDaniel Borkmann memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla)); 5319f53adae4SDaniel Borkmann read_unlock_bh(&idev->lock); 5320f53adae4SDaniel Borkmann 5321b382b191SThomas Graf return 0; 5322b382b191SThomas Graf 5323b382b191SThomas Graf nla_put_failure: 5324b382b191SThomas Graf return -EMSGSIZE; 5325b382b191SThomas Graf } 5326b382b191SThomas Graf 5327b1974ed0SArad, Ronen static size_t inet6_get_link_af_size(const struct net_device *dev, 5328b1974ed0SArad, Ronen u32 ext_filter_mask) 5329b382b191SThomas Graf { 5330b382b191SThomas Graf if (!__in6_dev_get(dev)) 5331b382b191SThomas Graf return 0; 5332b382b191SThomas Graf 5333b382b191SThomas Graf return inet6_ifla6_size(); 5334b382b191SThomas Graf } 5335b382b191SThomas Graf 5336d5566fd7SSowmini Varadhan static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev, 5337d5566fd7SSowmini Varadhan u32 ext_filter_mask) 5338b382b191SThomas Graf { 5339b382b191SThomas Graf struct inet6_dev *idev = __in6_dev_get(dev); 5340b382b191SThomas Graf 5341b382b191SThomas Graf if (!idev) 5342b382b191SThomas Graf return -ENODATA; 5343b382b191SThomas Graf 5344d5566fd7SSowmini Varadhan if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0) 5345b382b191SThomas Graf return -EMSGSIZE; 5346b382b191SThomas Graf 5347b382b191SThomas Graf return 0; 5348b382b191SThomas Graf } 5349b382b191SThomas Graf 5350f53adae4SDaniel Borkmann static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token) 5351f53adae4SDaniel Borkmann { 5352f53adae4SDaniel Borkmann struct inet6_ifaddr *ifp; 5353f53adae4SDaniel Borkmann struct net_device *dev = idev->dev; 535447e27d5eSDaniel Borkmann bool clear_token, update_rs = false; 5355dc848292SHannes Frederic Sowa struct in6_addr ll_addr; 5356f53adae4SDaniel Borkmann 5357c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 5358c15b1ccaSHannes Frederic Sowa 535963159f29SIan Morris if (!token) 5360f53adae4SDaniel Borkmann return -EINVAL; 5361f53adae4SDaniel Borkmann if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) 5362f53adae4SDaniel Borkmann return -EINVAL; 5363f53adae4SDaniel Borkmann if (!ipv6_accept_ra(idev)) 5364f53adae4SDaniel Borkmann return -EINVAL; 5365bd11f074SMaciej Żenczykowski if (idev->cnf.rtr_solicits == 0) 5366f53adae4SDaniel Borkmann return -EINVAL; 5367f53adae4SDaniel Borkmann 5368f53adae4SDaniel Borkmann write_lock_bh(&idev->lock); 5369f53adae4SDaniel Borkmann 5370f53adae4SDaniel Borkmann BUILD_BUG_ON(sizeof(token->s6_addr) != 16); 5371f53adae4SDaniel Borkmann memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8); 5372f53adae4SDaniel Borkmann 5373f53adae4SDaniel Borkmann write_unlock_bh(&idev->lock); 5374f53adae4SDaniel Borkmann 537547e27d5eSDaniel Borkmann clear_token = ipv6_addr_any(token); 537647e27d5eSDaniel Borkmann if (clear_token) 537747e27d5eSDaniel Borkmann goto update_lft; 537847e27d5eSDaniel Borkmann 5379dc848292SHannes Frederic Sowa if (!idev->dead && (idev->if_flags & IF_READY) && 5380dc848292SHannes Frederic Sowa !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE | 5381dc848292SHannes Frederic Sowa IFA_F_OPTIMISTIC)) { 5382fc403832SDaniel Borkmann /* If we're not ready, then normal ifup will take care 5383fc403832SDaniel Borkmann * of this. Otherwise, we need to request our rs here. 5384fc403832SDaniel Borkmann */ 5385f53adae4SDaniel Borkmann ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters); 5386fc403832SDaniel Borkmann update_rs = true; 5387fc403832SDaniel Borkmann } 5388f53adae4SDaniel Borkmann 538947e27d5eSDaniel Borkmann update_lft: 5390f53adae4SDaniel Borkmann write_lock_bh(&idev->lock); 5391fc403832SDaniel Borkmann 539277ecaaceSHannes Frederic Sowa if (update_rs) { 5393f53adae4SDaniel Borkmann idev->if_flags |= IF_RS_SENT; 5394bd11f074SMaciej Żenczykowski idev->rs_interval = rfc3315_s14_backoff_init( 5395bd11f074SMaciej Żenczykowski idev->cnf.rtr_solicit_interval); 539677ecaaceSHannes Frederic Sowa idev->rs_probes = 1; 5397bd11f074SMaciej Żenczykowski addrconf_mod_rs_timer(idev, idev->rs_interval); 539877ecaaceSHannes Frederic Sowa } 5399f53adae4SDaniel Borkmann 5400f53adae4SDaniel Borkmann /* Well, that's kinda nasty ... */ 5401f53adae4SDaniel Borkmann list_for_each_entry(ifp, &idev->addr_list, if_list) { 5402f53adae4SDaniel Borkmann spin_lock(&ifp->lock); 5403617fe29dSDaniel Borkmann if (ifp->tokenized) { 5404f53adae4SDaniel Borkmann ifp->valid_lft = 0; 5405f53adae4SDaniel Borkmann ifp->prefered_lft = 0; 5406f53adae4SDaniel Borkmann } 5407f53adae4SDaniel Borkmann spin_unlock(&ifp->lock); 5408f53adae4SDaniel Borkmann } 5409f53adae4SDaniel Borkmann 5410f53adae4SDaniel Borkmann write_unlock_bh(&idev->lock); 5411b2ed64a9SLubomir Rintel inet6_ifinfo_notify(RTM_NEWLINK, idev); 5412c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 5413f53adae4SDaniel Borkmann return 0; 5414f53adae4SDaniel Borkmann } 5415f53adae4SDaniel Borkmann 541611b1f828SDaniel Borkmann static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = { 541711b1f828SDaniel Borkmann [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 }, 541811b1f828SDaniel Borkmann [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) }, 541911b1f828SDaniel Borkmann }; 542011b1f828SDaniel Borkmann 542111b1f828SDaniel Borkmann static int inet6_validate_link_af(const struct net_device *dev, 542211b1f828SDaniel Borkmann const struct nlattr *nla) 542311b1f828SDaniel Borkmann { 542411b1f828SDaniel Borkmann struct nlattr *tb[IFLA_INET6_MAX + 1]; 542511b1f828SDaniel Borkmann 542611b1f828SDaniel Borkmann if (dev && !__in6_dev_get(dev)) 542711b1f828SDaniel Borkmann return -EAFNOSUPPORT; 542811b1f828SDaniel Borkmann 5429fceb6435SJohannes Berg return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy, 5430fceb6435SJohannes Berg NULL); 543111b1f828SDaniel Borkmann } 543211b1f828SDaniel Borkmann 5433d35a00b8SFelix Jia static int check_addr_gen_mode(int mode) 5434d35a00b8SFelix Jia { 5435d35a00b8SFelix Jia if (mode != IN6_ADDR_GEN_MODE_EUI64 && 5436d35a00b8SFelix Jia mode != IN6_ADDR_GEN_MODE_NONE && 5437d35a00b8SFelix Jia mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY && 5438d35a00b8SFelix Jia mode != IN6_ADDR_GEN_MODE_RANDOM) 5439d35a00b8SFelix Jia return -EINVAL; 5440d35a00b8SFelix Jia return 1; 5441d35a00b8SFelix Jia } 5442d35a00b8SFelix Jia 5443d35a00b8SFelix Jia static int check_stable_privacy(struct inet6_dev *idev, struct net *net, 5444d35a00b8SFelix Jia int mode) 5445d35a00b8SFelix Jia { 5446d35a00b8SFelix Jia if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY && 5447d35a00b8SFelix Jia !idev->cnf.stable_secret.initialized && 5448d35a00b8SFelix Jia !net->ipv6.devconf_dflt->stable_secret.initialized) 5449d35a00b8SFelix Jia return -EINVAL; 5450d35a00b8SFelix Jia return 1; 5451d35a00b8SFelix Jia } 5452d35a00b8SFelix Jia 5453f53adae4SDaniel Borkmann static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla) 5454f53adae4SDaniel Borkmann { 5455f53adae4SDaniel Borkmann int err = -EINVAL; 5456f53adae4SDaniel Borkmann struct inet6_dev *idev = __in6_dev_get(dev); 5457f53adae4SDaniel Borkmann struct nlattr *tb[IFLA_INET6_MAX + 1]; 5458f53adae4SDaniel Borkmann 5459f53adae4SDaniel Borkmann if (!idev) 5460f53adae4SDaniel Borkmann return -EAFNOSUPPORT; 5461f53adae4SDaniel Borkmann 5462fceb6435SJohannes Berg if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL, NULL) < 0) 5463f53adae4SDaniel Borkmann BUG(); 5464f53adae4SDaniel Borkmann 5465bc91b0f0SJiri Pirko if (tb[IFLA_INET6_TOKEN]) { 5466f53adae4SDaniel Borkmann err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN])); 5467bc91b0f0SJiri Pirko if (err) 5468bc91b0f0SJiri Pirko return err; 5469bc91b0f0SJiri Pirko } 5470bc91b0f0SJiri Pirko 5471bc91b0f0SJiri Pirko if (tb[IFLA_INET6_ADDR_GEN_MODE]) { 5472bc91b0f0SJiri Pirko u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]); 5473bc91b0f0SJiri Pirko 5474d35a00b8SFelix Jia if (check_addr_gen_mode(mode) < 0 || 5475d35a00b8SFelix Jia check_stable_privacy(idev, dev_net(dev), mode) < 0) 5476bc91b0f0SJiri Pirko return -EINVAL; 5477622c81d5SHannes Frederic Sowa 5478d35a00b8SFelix Jia idev->cnf.addr_gen_mode = mode; 5479bc91b0f0SJiri Pirko err = 0; 5480bc91b0f0SJiri Pirko } 5481f53adae4SDaniel Borkmann 5482f53adae4SDaniel Borkmann return err; 5483f53adae4SDaniel Borkmann } 5484f53adae4SDaniel Borkmann 5485b382b191SThomas Graf static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev, 548615e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 5487b382b191SThomas Graf { 5488b382b191SThomas Graf struct net_device *dev = idev->dev; 5489b382b191SThomas Graf struct ifinfomsg *hdr; 5490b382b191SThomas Graf struct nlmsghdr *nlh; 5491b382b191SThomas Graf void *protoinfo; 5492b382b191SThomas Graf 549315e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags); 549463159f29SIan Morris if (!nlh) 5495b382b191SThomas Graf return -EMSGSIZE; 5496b382b191SThomas Graf 5497b382b191SThomas Graf hdr = nlmsg_data(nlh); 5498b382b191SThomas Graf hdr->ifi_family = AF_INET6; 5499b382b191SThomas Graf hdr->__ifi_pad = 0; 5500b382b191SThomas Graf hdr->ifi_type = dev->type; 5501b382b191SThomas Graf hdr->ifi_index = dev->ifindex; 5502b382b191SThomas Graf hdr->ifi_flags = dev_get_flags(dev); 5503b382b191SThomas Graf hdr->ifi_change = 0; 5504b382b191SThomas Graf 5505c78679e8SDavid S. Miller if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 5506c78679e8SDavid S. Miller (dev->addr_len && 5507c78679e8SDavid S. Miller nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 5508c78679e8SDavid S. Miller nla_put_u32(skb, IFLA_MTU, dev->mtu) || 5509a54acb3aSNicolas Dichtel (dev->ifindex != dev_get_iflink(dev) && 55100344338bSAndy Gospodarek nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) || 55110344338bSAndy Gospodarek nla_put_u8(skb, IFLA_OPERSTATE, 55120344338bSAndy Gospodarek netif_running(dev) ? dev->operstate : IF_OPER_DOWN)) 5513c78679e8SDavid S. Miller goto nla_put_failure; 5514b382b191SThomas Graf protoinfo = nla_nest_start(skb, IFLA_PROTINFO); 551563159f29SIan Morris if (!protoinfo) 5516b382b191SThomas Graf goto nla_put_failure; 5517b382b191SThomas Graf 5518d5566fd7SSowmini Varadhan if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0) 5519b382b191SThomas Graf goto nla_put_failure; 5520b382b191SThomas Graf 552104561c1fSThomas Graf nla_nest_end(skb, protoinfo); 5522053c095aSJohannes Berg nlmsg_end(skb, nlh); 5523053c095aSJohannes Berg return 0; 55241da177e4SLinus Torvalds 552504561c1fSThomas Graf nla_put_failure: 552626932566SPatrick McHardy nlmsg_cancel(skb, nlh); 552726932566SPatrick McHardy return -EMSGSIZE; 55281da177e4SLinus Torvalds } 55291da177e4SLinus Torvalds 55301da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 55311da177e4SLinus Torvalds { 55323b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 553384d2697dSEric Dumazet int h, s_h; 5534434a8a58SDavid S. Miller int idx = 0, s_idx; 55351da177e4SLinus Torvalds struct net_device *dev; 55361da177e4SLinus Torvalds struct inet6_dev *idev; 553784d2697dSEric Dumazet struct hlist_head *head; 55381da177e4SLinus Torvalds 553984d2697dSEric Dumazet s_h = cb->args[0]; 554084d2697dSEric Dumazet s_idx = cb->args[1]; 554184d2697dSEric Dumazet 554284d2697dSEric Dumazet rcu_read_lock(); 554384d2697dSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 55447562f876SPavel Emelianov idx = 0; 554584d2697dSEric Dumazet head = &net->dev_index_head[h]; 5546b67bfe0dSSasha Levin hlist_for_each_entry_rcu(dev, head, index_hlist) { 55471da177e4SLinus Torvalds if (idx < s_idx) 55487562f876SPavel Emelianov goto cont; 554984d2697dSEric Dumazet idev = __in6_dev_get(dev); 555084d2697dSEric Dumazet if (!idev) 55517562f876SPavel Emelianov goto cont; 555284d2697dSEric Dumazet if (inet6_fill_ifinfo(skb, idev, 555315e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 555484d2697dSEric Dumazet cb->nlh->nlmsg_seq, 5555053c095aSJohannes Berg RTM_NEWLINK, NLM_F_MULTI) < 0) 555684d2697dSEric Dumazet goto out; 55577562f876SPavel Emelianov cont: 55587562f876SPavel Emelianov idx++; 55591da177e4SLinus Torvalds } 556084d2697dSEric Dumazet } 556184d2697dSEric Dumazet out: 556284d2697dSEric Dumazet rcu_read_unlock(); 556384d2697dSEric Dumazet cb->args[1] = idx; 556484d2697dSEric Dumazet cb->args[0] = h; 55651da177e4SLinus Torvalds 55661da177e4SLinus Torvalds return skb->len; 55671da177e4SLinus Torvalds } 55681da177e4SLinus Torvalds 55691da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev) 55701da177e4SLinus Torvalds { 55711da177e4SLinus Torvalds struct sk_buff *skb; 5572c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 55738d7a76c9SThomas Graf int err = -ENOBUFS; 55741da177e4SLinus Torvalds 5575339bf98fSThomas Graf skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC); 557663159f29SIan Morris if (!skb) 55778d7a76c9SThomas Graf goto errout; 55788d7a76c9SThomas Graf 55798d7a76c9SThomas Graf err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0); 558026932566SPatrick McHardy if (err < 0) { 558126932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */ 558226932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 558326932566SPatrick McHardy kfree_skb(skb); 558426932566SPatrick McHardy goto errout; 558526932566SPatrick McHardy } 55865f75a104SNicolas Dichtel rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC); 55871ce85fe4SPablo Neira Ayuso return; 55888d7a76c9SThomas Graf errout: 55898d7a76c9SThomas Graf if (err < 0) 55905f75a104SNicolas Dichtel rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err); 55911da177e4SLinus Torvalds } 55921da177e4SLinus Torvalds 5593339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void) 5594339bf98fSThomas Graf { 5595339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct prefixmsg)) 5596339bf98fSThomas Graf + nla_total_size(sizeof(struct in6_addr)) 5597339bf98fSThomas Graf + nla_total_size(sizeof(struct prefix_cacheinfo)); 5598339bf98fSThomas Graf } 5599c5396a31SYOSHIFUJI Hideaki 56001da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev, 560115e47304SEric W. Biederman struct prefix_info *pinfo, u32 portid, u32 seq, 5602b6544c0bSJamal Hadi Salim int event, unsigned int flags) 56031da177e4SLinus Torvalds { 56041da177e4SLinus Torvalds struct prefixmsg *pmsg; 56051da177e4SLinus Torvalds struct nlmsghdr *nlh; 56061da177e4SLinus Torvalds struct prefix_cacheinfo ci; 56071da177e4SLinus Torvalds 560815e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags); 560963159f29SIan Morris if (!nlh) 561026932566SPatrick McHardy return -EMSGSIZE; 56116051e2f4SThomas Graf 56126051e2f4SThomas Graf pmsg = nlmsg_data(nlh); 56131da177e4SLinus Torvalds pmsg->prefix_family = AF_INET6; 56148a47077aSPatrick McHardy pmsg->prefix_pad1 = 0; 56158a47077aSPatrick McHardy pmsg->prefix_pad2 = 0; 56161da177e4SLinus Torvalds pmsg->prefix_ifindex = idev->dev->ifindex; 56171da177e4SLinus Torvalds pmsg->prefix_len = pinfo->prefix_len; 56181da177e4SLinus Torvalds pmsg->prefix_type = pinfo->type; 56198a47077aSPatrick McHardy pmsg->prefix_pad3 = 0; 56201da177e4SLinus Torvalds pmsg->prefix_flags = 0; 56211da177e4SLinus Torvalds if (pinfo->onlink) 56221da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_ONLINK; 56231da177e4SLinus Torvalds if (pinfo->autoconf) 56241da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_AUTOCONF; 56251da177e4SLinus Torvalds 5626c78679e8SDavid S. Miller if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix)) 5627c78679e8SDavid S. Miller goto nla_put_failure; 56281da177e4SLinus Torvalds ci.preferred_time = ntohl(pinfo->prefered); 56291da177e4SLinus Torvalds ci.valid_time = ntohl(pinfo->valid); 5630c78679e8SDavid S. Miller if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci)) 5631c78679e8SDavid S. Miller goto nla_put_failure; 5632053c095aSJohannes Berg nlmsg_end(skb, nlh); 5633053c095aSJohannes Berg return 0; 56341da177e4SLinus Torvalds 56356051e2f4SThomas Graf nla_put_failure: 563626932566SPatrick McHardy nlmsg_cancel(skb, nlh); 563726932566SPatrick McHardy return -EMSGSIZE; 56381da177e4SLinus Torvalds } 56391da177e4SLinus Torvalds 56401da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev, 56411da177e4SLinus Torvalds struct prefix_info *pinfo) 56421da177e4SLinus Torvalds { 56431da177e4SLinus Torvalds struct sk_buff *skb; 5644c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 56458c384bfaSThomas Graf int err = -ENOBUFS; 56461da177e4SLinus Torvalds 5647339bf98fSThomas Graf skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC); 564863159f29SIan Morris if (!skb) 56498c384bfaSThomas Graf goto errout; 56508c384bfaSThomas Graf 56518c384bfaSThomas Graf err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0); 565226932566SPatrick McHardy if (err < 0) { 565326932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */ 565426932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 565526932566SPatrick McHardy kfree_skb(skb); 565626932566SPatrick McHardy goto errout; 565726932566SPatrick McHardy } 56581ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC); 56591ce85fe4SPablo Neira Ayuso return; 56608c384bfaSThomas Graf errout: 56618c384bfaSThomas Graf if (err < 0) 56626fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err); 56631da177e4SLinus Torvalds } 56641da177e4SLinus Torvalds 56651da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 56661da177e4SLinus Torvalds { 566763998ac2SNicolas Dichtel struct net *net = dev_net(ifp->idev->dev); 566863998ac2SNicolas Dichtel 5669c15b1ccaSHannes Frederic Sowa if (event) 5670c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 5671c15b1ccaSHannes Frederic Sowa 56721da177e4SLinus Torvalds inet6_ifa_notify(event ? : RTM_NEWADDR, ifp); 56731da177e4SLinus Torvalds 56741da177e4SLinus Torvalds switch (event) { 56751da177e4SLinus Torvalds case RTM_NEWADDR: 567695c385b4SNeil Horman /* 567795c385b4SNeil Horman * If the address was optimistic 567895c385b4SNeil Horman * we inserted the route at the start of 567995c385b4SNeil Horman * our DAD process, so we don't need 568095c385b4SNeil Horman * to do it again 568195c385b4SNeil Horman */ 568293c2fb25SDavid Ahern if (!rcu_access_pointer(ifp->rt->fib6_node)) 5683afb1d4b5SDavid Ahern ip6_ins_rt(net, ifp->rt); 56841da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 56851da177e4SLinus Torvalds addrconf_join_anycast(ifp); 56867996c799SCong Wang if (!ipv6_addr_any(&ifp->peer_addr)) 56878308f3ffSDavid Ahern addrconf_prefix_route(&ifp->peer_addr, 128, 0, 5688acb54e3cSDavid Ahern ifp->idev->dev, 0, 0, 568927b10608SDavid Ahern GFP_ATOMIC); 56901da177e4SLinus Torvalds break; 56911da177e4SLinus Torvalds case RTM_DELADDR: 56921da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 56931da177e4SLinus Torvalds addrconf_leave_anycast(ifp); 56941da177e4SLinus Torvalds addrconf_leave_solict(ifp->idev, &ifp->addr); 56957996c799SCong Wang if (!ipv6_addr_any(&ifp->peer_addr)) { 56968d1c802bSDavid Ahern struct fib6_info *rt; 5697caeaba79SNicolas Dichtel 5698e7478dfcSNicolas Dichtel rt = addrconf_get_prefix_route(&ifp->peer_addr, 128, 5699e7478dfcSNicolas Dichtel ifp->idev->dev, 0, 0); 57008e3d5be7SMartin KaFai Lau if (rt) 5701afb1d4b5SDavid Ahern ip6_del_rt(net, rt); 5702caeaba79SNicolas Dichtel } 570338bd10c4SDavid Ahern if (ifp->rt) { 5704afb1d4b5SDavid Ahern ip6_del_rt(net, ifp->rt); 570593531c67SDavid Ahern ifp->rt = NULL; 570638bd10c4SDavid Ahern } 5707705f1c86SHannes Frederic Sowa rt_genid_bump_ipv6(net); 57081da177e4SLinus Torvalds break; 57091da177e4SLinus Torvalds } 571063998ac2SNicolas Dichtel atomic_inc(&net->ipv6.dev_addr_genid); 57111da177e4SLinus Torvalds } 57121da177e4SLinus Torvalds 57131da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 57141da177e4SLinus Torvalds { 57158814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 57161da177e4SLinus Torvalds if (likely(ifp->idev->dead == 0)) 57171da177e4SLinus Torvalds __ipv6_ifa_notify(event, ifp); 57188814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 57191da177e4SLinus Torvalds } 57201da177e4SLinus Torvalds 57211da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 57221da177e4SLinus Torvalds 57231da177e4SLinus Torvalds static 5724fe2c6338SJoe Perches int addrconf_sysctl_forward(struct ctl_table *ctl, int write, 57251da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 57261da177e4SLinus Torvalds { 57271da177e4SLinus Torvalds int *valp = ctl->data; 57281da177e4SLinus Torvalds int val = *valp; 572988af182eSEric W. Biederman loff_t pos = *ppos; 5730fe2c6338SJoe Perches struct ctl_table lctl; 57311da177e4SLinus Torvalds int ret; 57321da177e4SLinus Torvalds 5733013d97e9SFrancesco Ruggeri /* 5734013d97e9SFrancesco Ruggeri * ctl->data points to idev->cnf.forwarding, we should 5735013d97e9SFrancesco Ruggeri * not modify it until we get the rtnl lock. 5736013d97e9SFrancesco Ruggeri */ 5737013d97e9SFrancesco Ruggeri lctl = *ctl; 5738013d97e9SFrancesco Ruggeri lctl.data = &val; 5739013d97e9SFrancesco Ruggeri 5740013d97e9SFrancesco Ruggeri ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 57411da177e4SLinus Torvalds 5742c8fecf22SPavel Emelyanov if (write) 5743b325fddbSStephen Hemminger ret = addrconf_fixup_forwarding(ctl, valp, val); 574488af182eSEric W. Biederman if (ret) 574588af182eSEric W. Biederman *ppos = pos; 57461da177e4SLinus Torvalds return ret; 57471da177e4SLinus Torvalds } 57481da177e4SLinus Torvalds 574977751427SMarcelo Leitner static 575077751427SMarcelo Leitner int addrconf_sysctl_mtu(struct ctl_table *ctl, int write, 575177751427SMarcelo Leitner void __user *buffer, size_t *lenp, loff_t *ppos) 575277751427SMarcelo Leitner { 575377751427SMarcelo Leitner struct inet6_dev *idev = ctl->extra1; 575477751427SMarcelo Leitner int min_mtu = IPV6_MIN_MTU; 575577751427SMarcelo Leitner struct ctl_table lctl; 575677751427SMarcelo Leitner 575777751427SMarcelo Leitner lctl = *ctl; 575877751427SMarcelo Leitner lctl.extra1 = &min_mtu; 575977751427SMarcelo Leitner lctl.extra2 = idev ? &idev->dev->mtu : NULL; 576077751427SMarcelo Leitner 576177751427SMarcelo Leitner return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos); 576277751427SMarcelo Leitner } 576377751427SMarcelo Leitner 576456d417b1SBrian Haley static void dev_disable_change(struct inet6_dev *idev) 576556d417b1SBrian Haley { 576675538c2bSCong Wang struct netdev_notifier_info info; 576775538c2bSCong Wang 576856d417b1SBrian Haley if (!idev || !idev->dev) 576956d417b1SBrian Haley return; 577056d417b1SBrian Haley 577175538c2bSCong Wang netdev_notifier_info_init(&info, idev->dev); 577256d417b1SBrian Haley if (idev->cnf.disable_ipv6) 577375538c2bSCong Wang addrconf_notify(NULL, NETDEV_DOWN, &info); 577456d417b1SBrian Haley else 577575538c2bSCong Wang addrconf_notify(NULL, NETDEV_UP, &info); 577656d417b1SBrian Haley } 577756d417b1SBrian Haley 577856d417b1SBrian Haley static void addrconf_disable_change(struct net *net, __s32 newf) 577956d417b1SBrian Haley { 578056d417b1SBrian Haley struct net_device *dev; 578156d417b1SBrian Haley struct inet6_dev *idev; 578256d417b1SBrian Haley 578303e4deffSKefeng Wang for_each_netdev(net, dev) { 578456d417b1SBrian Haley idev = __in6_dev_get(dev); 578556d417b1SBrian Haley if (idev) { 578656d417b1SBrian Haley int changed = (!idev->cnf.disable_ipv6) ^ (!newf); 578756d417b1SBrian Haley idev->cnf.disable_ipv6 = newf; 578856d417b1SBrian Haley if (changed) 578956d417b1SBrian Haley dev_disable_change(idev); 579056d417b1SBrian Haley } 579156d417b1SBrian Haley } 579256d417b1SBrian Haley } 579356d417b1SBrian Haley 5794013d97e9SFrancesco Ruggeri static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf) 579556d417b1SBrian Haley { 579656d417b1SBrian Haley struct net *net; 5797013d97e9SFrancesco Ruggeri int old; 5798013d97e9SFrancesco Ruggeri 5799013d97e9SFrancesco Ruggeri if (!rtnl_trylock()) 5800013d97e9SFrancesco Ruggeri return restart_syscall(); 580156d417b1SBrian Haley 580256d417b1SBrian Haley net = (struct net *)table->extra2; 5803013d97e9SFrancesco Ruggeri old = *p; 5804013d97e9SFrancesco Ruggeri *p = newf; 580556d417b1SBrian Haley 5806013d97e9SFrancesco Ruggeri if (p == &net->ipv6.devconf_dflt->disable_ipv6) { 5807013d97e9SFrancesco Ruggeri rtnl_unlock(); 580856d417b1SBrian Haley return 0; 580988af182eSEric W. Biederman } 581056d417b1SBrian Haley 581156d417b1SBrian Haley if (p == &net->ipv6.devconf_all->disable_ipv6) { 581256d417b1SBrian Haley net->ipv6.devconf_dflt->disable_ipv6 = newf; 581356d417b1SBrian Haley addrconf_disable_change(net, newf); 5814013d97e9SFrancesco Ruggeri } else if ((!newf) ^ (!old)) 581556d417b1SBrian Haley dev_disable_change((struct inet6_dev *)table->extra1); 581656d417b1SBrian Haley 581756d417b1SBrian Haley rtnl_unlock(); 581856d417b1SBrian Haley return 0; 581956d417b1SBrian Haley } 582056d417b1SBrian Haley 582156d417b1SBrian Haley static 5822fe2c6338SJoe Perches int addrconf_sysctl_disable(struct ctl_table *ctl, int write, 582356d417b1SBrian Haley void __user *buffer, size_t *lenp, loff_t *ppos) 582456d417b1SBrian Haley { 582556d417b1SBrian Haley int *valp = ctl->data; 582656d417b1SBrian Haley int val = *valp; 582788af182eSEric W. Biederman loff_t pos = *ppos; 5828fe2c6338SJoe Perches struct ctl_table lctl; 582956d417b1SBrian Haley int ret; 583056d417b1SBrian Haley 5831013d97e9SFrancesco Ruggeri /* 5832013d97e9SFrancesco Ruggeri * ctl->data points to idev->cnf.disable_ipv6, we should 5833013d97e9SFrancesco Ruggeri * not modify it until we get the rtnl lock. 5834013d97e9SFrancesco Ruggeri */ 5835013d97e9SFrancesco Ruggeri lctl = *ctl; 5836013d97e9SFrancesco Ruggeri lctl.data = &val; 5837013d97e9SFrancesco Ruggeri 5838013d97e9SFrancesco Ruggeri ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 583956d417b1SBrian Haley 584056d417b1SBrian Haley if (write) 584156d417b1SBrian Haley ret = addrconf_disable_ipv6(ctl, valp, val); 584288af182eSEric W. Biederman if (ret) 584388af182eSEric W. Biederman *ppos = pos; 584456d417b1SBrian Haley return ret; 584556d417b1SBrian Haley } 584656d417b1SBrian Haley 5847c92d5491Sstephen hemminger static 5848c92d5491Sstephen hemminger int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write, 5849c92d5491Sstephen hemminger void __user *buffer, size_t *lenp, loff_t *ppos) 5850c92d5491Sstephen hemminger { 5851c92d5491Sstephen hemminger int *valp = ctl->data; 5852c92d5491Sstephen hemminger int ret; 5853c92d5491Sstephen hemminger int old, new; 5854c92d5491Sstephen hemminger 5855c92d5491Sstephen hemminger old = *valp; 5856c92d5491Sstephen hemminger ret = proc_dointvec(ctl, write, buffer, lenp, ppos); 5857c92d5491Sstephen hemminger new = *valp; 5858c92d5491Sstephen hemminger 5859c92d5491Sstephen hemminger if (write && old != new) { 5860c92d5491Sstephen hemminger struct net *net = ctl->extra2; 5861c92d5491Sstephen hemminger 5862c92d5491Sstephen hemminger if (!rtnl_trylock()) 5863c92d5491Sstephen hemminger return restart_syscall(); 5864c92d5491Sstephen hemminger 5865c92d5491Sstephen hemminger if (valp == &net->ipv6.devconf_dflt->proxy_ndp) 586685b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 586785b3daadSDavid Ahern NETCONFA_PROXY_NEIGH, 5868c92d5491Sstephen hemminger NETCONFA_IFINDEX_DEFAULT, 5869c92d5491Sstephen hemminger net->ipv6.devconf_dflt); 5870c92d5491Sstephen hemminger else if (valp == &net->ipv6.devconf_all->proxy_ndp) 587185b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 587285b3daadSDavid Ahern NETCONFA_PROXY_NEIGH, 5873c92d5491Sstephen hemminger NETCONFA_IFINDEX_ALL, 5874c92d5491Sstephen hemminger net->ipv6.devconf_all); 5875c92d5491Sstephen hemminger else { 5876c92d5491Sstephen hemminger struct inet6_dev *idev = ctl->extra1; 5877c92d5491Sstephen hemminger 587885b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 587985b3daadSDavid Ahern NETCONFA_PROXY_NEIGH, 5880c92d5491Sstephen hemminger idev->dev->ifindex, 5881c92d5491Sstephen hemminger &idev->cnf); 5882c92d5491Sstephen hemminger } 5883c92d5491Sstephen hemminger rtnl_unlock(); 5884c92d5491Sstephen hemminger } 5885c92d5491Sstephen hemminger 5886c92d5491Sstephen hemminger return ret; 5887c92d5491Sstephen hemminger } 5888c92d5491Sstephen hemminger 5889d35a00b8SFelix Jia static int addrconf_sysctl_addr_gen_mode(struct ctl_table *ctl, int write, 5890d35a00b8SFelix Jia void __user *buffer, size_t *lenp, 5891d35a00b8SFelix Jia loff_t *ppos) 5892d35a00b8SFelix Jia { 5893d35a00b8SFelix Jia int ret = 0; 5894c6dbf7aaSSabrina Dubroca u32 new_val; 5895d35a00b8SFelix Jia struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1; 5896d35a00b8SFelix Jia struct net *net = (struct net *)ctl->extra2; 5897c6dbf7aaSSabrina Dubroca struct ctl_table tmp = { 5898c6dbf7aaSSabrina Dubroca .data = &new_val, 5899c6dbf7aaSSabrina Dubroca .maxlen = sizeof(new_val), 5900c6dbf7aaSSabrina Dubroca .mode = ctl->mode, 5901c6dbf7aaSSabrina Dubroca }; 5902d35a00b8SFelix Jia 59038c171d6cSFelix Jia if (!rtnl_trylock()) 59048c171d6cSFelix Jia return restart_syscall(); 59058c171d6cSFelix Jia 5906c6dbf7aaSSabrina Dubroca new_val = *((u32 *)ctl->data); 5907c6dbf7aaSSabrina Dubroca 5908c6dbf7aaSSabrina Dubroca ret = proc_douintvec(&tmp, write, buffer, lenp, ppos); 5909c6dbf7aaSSabrina Dubroca if (ret != 0) 5910c6dbf7aaSSabrina Dubroca goto out; 5911d35a00b8SFelix Jia 5912d35a00b8SFelix Jia if (write) { 59138c171d6cSFelix Jia if (check_addr_gen_mode(new_val) < 0) { 59148c171d6cSFelix Jia ret = -EINVAL; 59158c171d6cSFelix Jia goto out; 59168c171d6cSFelix Jia } 5917d35a00b8SFelix Jia 5918c6dbf7aaSSabrina Dubroca if (idev) { 59198c171d6cSFelix Jia if (check_stable_privacy(idev, net, new_val) < 0) { 59208c171d6cSFelix Jia ret = -EINVAL; 59218c171d6cSFelix Jia goto out; 59228c171d6cSFelix Jia } 5923d35a00b8SFelix Jia 5924d35a00b8SFelix Jia if (idev->cnf.addr_gen_mode != new_val) { 5925d35a00b8SFelix Jia idev->cnf.addr_gen_mode = new_val; 5926d35a00b8SFelix Jia addrconf_dev_config(idev->dev); 59278c171d6cSFelix Jia } 5928f24c5987SSabrina Dubroca } else if (&net->ipv6.devconf_all->addr_gen_mode == ctl->data) { 5929f24c5987SSabrina Dubroca struct net_device *dev; 5930f24c5987SSabrina Dubroca 5931f24c5987SSabrina Dubroca net->ipv6.devconf_dflt->addr_gen_mode = new_val; 5932f24c5987SSabrina Dubroca for_each_netdev(net, dev) { 5933f24c5987SSabrina Dubroca idev = __in6_dev_get(dev); 5934f24c5987SSabrina Dubroca if (idev && 5935f24c5987SSabrina Dubroca idev->cnf.addr_gen_mode != new_val) { 5936f24c5987SSabrina Dubroca idev->cnf.addr_gen_mode = new_val; 5937f24c5987SSabrina Dubroca addrconf_dev_config(idev->dev); 5938f24c5987SSabrina Dubroca } 5939f24c5987SSabrina Dubroca } 59408c171d6cSFelix Jia } 5941c6dbf7aaSSabrina Dubroca 5942c6dbf7aaSSabrina Dubroca *((u32 *)ctl->data) = new_val; 59438c171d6cSFelix Jia } 59448c171d6cSFelix Jia 59458c171d6cSFelix Jia out: 5946d35a00b8SFelix Jia rtnl_unlock(); 5947d35a00b8SFelix Jia 5948d35a00b8SFelix Jia return ret; 5949d35a00b8SFelix Jia } 5950d35a00b8SFelix Jia 59513d1bec99SHannes Frederic Sowa static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write, 59523d1bec99SHannes Frederic Sowa void __user *buffer, size_t *lenp, 59533d1bec99SHannes Frederic Sowa loff_t *ppos) 59543d1bec99SHannes Frederic Sowa { 59553d1bec99SHannes Frederic Sowa int err; 59563d1bec99SHannes Frederic Sowa struct in6_addr addr; 59573d1bec99SHannes Frederic Sowa char str[IPV6_MAX_STRLEN]; 59583d1bec99SHannes Frederic Sowa struct ctl_table lctl = *ctl; 5959622c81d5SHannes Frederic Sowa struct net *net = ctl->extra2; 59603d1bec99SHannes Frederic Sowa struct ipv6_stable_secret *secret = ctl->data; 59613d1bec99SHannes Frederic Sowa 5962622c81d5SHannes Frederic Sowa if (&net->ipv6.devconf_all->stable_secret == ctl->data) 5963622c81d5SHannes Frederic Sowa return -EIO; 5964622c81d5SHannes Frederic Sowa 59653d1bec99SHannes Frederic Sowa lctl.maxlen = IPV6_MAX_STRLEN; 59663d1bec99SHannes Frederic Sowa lctl.data = str; 59673d1bec99SHannes Frederic Sowa 59683d1bec99SHannes Frederic Sowa if (!rtnl_trylock()) 59693d1bec99SHannes Frederic Sowa return restart_syscall(); 59703d1bec99SHannes Frederic Sowa 59713d1bec99SHannes Frederic Sowa if (!write && !secret->initialized) { 59723d1bec99SHannes Frederic Sowa err = -EIO; 59733d1bec99SHannes Frederic Sowa goto out; 59743d1bec99SHannes Frederic Sowa } 59753d1bec99SHannes Frederic Sowa 59765449a5caSWANG Cong err = snprintf(str, sizeof(str), "%pI6", &secret->secret); 59773d1bec99SHannes Frederic Sowa if (err >= sizeof(str)) { 59783d1bec99SHannes Frederic Sowa err = -EIO; 59793d1bec99SHannes Frederic Sowa goto out; 59803d1bec99SHannes Frederic Sowa } 59813d1bec99SHannes Frederic Sowa 59823d1bec99SHannes Frederic Sowa err = proc_dostring(&lctl, write, buffer, lenp, ppos); 59833d1bec99SHannes Frederic Sowa if (err || !write) 59843d1bec99SHannes Frederic Sowa goto out; 59853d1bec99SHannes Frederic Sowa 59863d1bec99SHannes Frederic Sowa if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) { 59873d1bec99SHannes Frederic Sowa err = -EIO; 59883d1bec99SHannes Frederic Sowa goto out; 59893d1bec99SHannes Frederic Sowa } 59903d1bec99SHannes Frederic Sowa 59913d1bec99SHannes Frederic Sowa secret->initialized = true; 59923d1bec99SHannes Frederic Sowa secret->secret = addr; 59933d1bec99SHannes Frederic Sowa 5994622c81d5SHannes Frederic Sowa if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) { 5995622c81d5SHannes Frederic Sowa struct net_device *dev; 5996622c81d5SHannes Frederic Sowa 5997622c81d5SHannes Frederic Sowa for_each_netdev(net, dev) { 5998622c81d5SHannes Frederic Sowa struct inet6_dev *idev = __in6_dev_get(dev); 5999622c81d5SHannes Frederic Sowa 6000622c81d5SHannes Frederic Sowa if (idev) { 6001d35a00b8SFelix Jia idev->cnf.addr_gen_mode = 6002622c81d5SHannes Frederic Sowa IN6_ADDR_GEN_MODE_STABLE_PRIVACY; 6003622c81d5SHannes Frederic Sowa } 6004622c81d5SHannes Frederic Sowa } 6005622c81d5SHannes Frederic Sowa } else { 6006622c81d5SHannes Frederic Sowa struct inet6_dev *idev = ctl->extra1; 6007622c81d5SHannes Frederic Sowa 6008d35a00b8SFelix Jia idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY; 6009622c81d5SHannes Frederic Sowa } 6010622c81d5SHannes Frederic Sowa 60113d1bec99SHannes Frederic Sowa out: 60123d1bec99SHannes Frederic Sowa rtnl_unlock(); 60133d1bec99SHannes Frederic Sowa 60143d1bec99SHannes Frederic Sowa return err; 60153d1bec99SHannes Frederic Sowa } 6016c92d5491Sstephen hemminger 601735103d11SAndy Gospodarek static 601835103d11SAndy Gospodarek int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl, 601935103d11SAndy Gospodarek int write, 602035103d11SAndy Gospodarek void __user *buffer, 602135103d11SAndy Gospodarek size_t *lenp, 602235103d11SAndy Gospodarek loff_t *ppos) 602335103d11SAndy Gospodarek { 602435103d11SAndy Gospodarek int *valp = ctl->data; 602535103d11SAndy Gospodarek int val = *valp; 602635103d11SAndy Gospodarek loff_t pos = *ppos; 602735103d11SAndy Gospodarek struct ctl_table lctl; 602835103d11SAndy Gospodarek int ret; 602935103d11SAndy Gospodarek 603035103d11SAndy Gospodarek /* ctl->data points to idev->cnf.ignore_routes_when_linkdown 603135103d11SAndy Gospodarek * we should not modify it until we get the rtnl lock. 603235103d11SAndy Gospodarek */ 603335103d11SAndy Gospodarek lctl = *ctl; 603435103d11SAndy Gospodarek lctl.data = &val; 603535103d11SAndy Gospodarek 603635103d11SAndy Gospodarek ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 603735103d11SAndy Gospodarek 603835103d11SAndy Gospodarek if (write) 603935103d11SAndy Gospodarek ret = addrconf_fixup_linkdown(ctl, valp, val); 604035103d11SAndy Gospodarek if (ret) 604135103d11SAndy Gospodarek *ppos = pos; 604235103d11SAndy Gospodarek return ret; 604335103d11SAndy Gospodarek } 604435103d11SAndy Gospodarek 6045df789fe7SDavid Forster static 6046df789fe7SDavid Forster void addrconf_set_nopolicy(struct rt6_info *rt, int action) 6047df789fe7SDavid Forster { 6048df789fe7SDavid Forster if (rt) { 6049df789fe7SDavid Forster if (action) 6050df789fe7SDavid Forster rt->dst.flags |= DST_NOPOLICY; 6051df789fe7SDavid Forster else 6052df789fe7SDavid Forster rt->dst.flags &= ~DST_NOPOLICY; 6053df789fe7SDavid Forster } 6054df789fe7SDavid Forster } 6055df789fe7SDavid Forster 6056df789fe7SDavid Forster static 6057df789fe7SDavid Forster void addrconf_disable_policy_idev(struct inet6_dev *idev, int val) 6058df789fe7SDavid Forster { 6059df789fe7SDavid Forster struct inet6_ifaddr *ifa; 6060df789fe7SDavid Forster 6061df789fe7SDavid Forster read_lock_bh(&idev->lock); 6062df789fe7SDavid Forster list_for_each_entry(ifa, &idev->addr_list, if_list) { 6063df789fe7SDavid Forster spin_lock(&ifa->lock); 6064df789fe7SDavid Forster if (ifa->rt) { 60658d1c802bSDavid Ahern struct fib6_info *rt = ifa->rt; 6066df789fe7SDavid Forster int cpu; 6067df789fe7SDavid Forster 606866f5d6ceSWei Wang rcu_read_lock(); 60693b6761d1SDavid Ahern ifa->rt->dst_nopolicy = val ? true : false; 6070df789fe7SDavid Forster if (rt->rt6i_pcpu) { 6071df789fe7SDavid Forster for_each_possible_cpu(cpu) { 6072df789fe7SDavid Forster struct rt6_info **rtp; 6073df789fe7SDavid Forster 6074df789fe7SDavid Forster rtp = per_cpu_ptr(rt->rt6i_pcpu, cpu); 6075df789fe7SDavid Forster addrconf_set_nopolicy(*rtp, val); 6076df789fe7SDavid Forster } 6077df789fe7SDavid Forster } 607866f5d6ceSWei Wang rcu_read_unlock(); 6079df789fe7SDavid Forster } 6080df789fe7SDavid Forster spin_unlock(&ifa->lock); 6081df789fe7SDavid Forster } 6082df789fe7SDavid Forster read_unlock_bh(&idev->lock); 6083df789fe7SDavid Forster } 6084df789fe7SDavid Forster 6085df789fe7SDavid Forster static 6086df789fe7SDavid Forster int addrconf_disable_policy(struct ctl_table *ctl, int *valp, int val) 6087df789fe7SDavid Forster { 6088df789fe7SDavid Forster struct inet6_dev *idev; 6089df789fe7SDavid Forster struct net *net; 6090df789fe7SDavid Forster 6091df789fe7SDavid Forster if (!rtnl_trylock()) 6092df789fe7SDavid Forster return restart_syscall(); 6093df789fe7SDavid Forster 6094df789fe7SDavid Forster *valp = val; 6095df789fe7SDavid Forster 6096df789fe7SDavid Forster net = (struct net *)ctl->extra2; 6097df789fe7SDavid Forster if (valp == &net->ipv6.devconf_dflt->disable_policy) { 6098df789fe7SDavid Forster rtnl_unlock(); 6099df789fe7SDavid Forster return 0; 6100df789fe7SDavid Forster } 6101df789fe7SDavid Forster 6102df789fe7SDavid Forster if (valp == &net->ipv6.devconf_all->disable_policy) { 6103df789fe7SDavid Forster struct net_device *dev; 6104df789fe7SDavid Forster 6105df789fe7SDavid Forster for_each_netdev(net, dev) { 6106df789fe7SDavid Forster idev = __in6_dev_get(dev); 6107df789fe7SDavid Forster if (idev) 6108df789fe7SDavid Forster addrconf_disable_policy_idev(idev, val); 6109df789fe7SDavid Forster } 6110df789fe7SDavid Forster } else { 6111df789fe7SDavid Forster idev = (struct inet6_dev *)ctl->extra1; 6112df789fe7SDavid Forster addrconf_disable_policy_idev(idev, val); 6113df789fe7SDavid Forster } 6114df789fe7SDavid Forster 6115df789fe7SDavid Forster rtnl_unlock(); 6116df789fe7SDavid Forster return 0; 6117df789fe7SDavid Forster } 6118df789fe7SDavid Forster 6119df789fe7SDavid Forster static 6120df789fe7SDavid Forster int addrconf_sysctl_disable_policy(struct ctl_table *ctl, int write, 6121df789fe7SDavid Forster void __user *buffer, size_t *lenp, 6122df789fe7SDavid Forster loff_t *ppos) 6123df789fe7SDavid Forster { 6124df789fe7SDavid Forster int *valp = ctl->data; 6125df789fe7SDavid Forster int val = *valp; 6126df789fe7SDavid Forster loff_t pos = *ppos; 6127df789fe7SDavid Forster struct ctl_table lctl; 6128df789fe7SDavid Forster int ret; 6129df789fe7SDavid Forster 6130df789fe7SDavid Forster lctl = *ctl; 6131df789fe7SDavid Forster lctl.data = &val; 6132df789fe7SDavid Forster ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 6133df789fe7SDavid Forster 6134df789fe7SDavid Forster if (write && (*valp != val)) 6135df789fe7SDavid Forster ret = addrconf_disable_policy(ctl, valp, val); 6136df789fe7SDavid Forster 6137df789fe7SDavid Forster if (ret) 6138df789fe7SDavid Forster *ppos = pos; 6139df789fe7SDavid Forster 6140df789fe7SDavid Forster return ret; 6141df789fe7SDavid Forster } 6142df789fe7SDavid Forster 6143cb4a4c69SMaciej Żenczykowski static int minus_one = -1; 61442210d6b2SMaciej Żenczykowski static const int zero = 0; 6145cb9e684eSMaciej Żenczykowski static const int one = 1; 6146cb9e684eSMaciej Żenczykowski static const int two_five_five = 255; 6147cb9e684eSMaciej Żenczykowski 6148607ea7cdSKonstantin Khlebnikov static const struct ctl_table addrconf_sysctl[] = { 61491da177e4SLinus Torvalds { 61501da177e4SLinus Torvalds .procname = "forwarding", 61511da177e4SLinus Torvalds .data = &ipv6_devconf.forwarding, 61521da177e4SLinus Torvalds .maxlen = sizeof(int), 61531da177e4SLinus Torvalds .mode = 0644, 61546d9f239aSAlexey Dobriyan .proc_handler = addrconf_sysctl_forward, 61551da177e4SLinus Torvalds }, 61561da177e4SLinus Torvalds { 61571da177e4SLinus Torvalds .procname = "hop_limit", 61581da177e4SLinus Torvalds .data = &ipv6_devconf.hop_limit, 61591da177e4SLinus Torvalds .maxlen = sizeof(int), 61601da177e4SLinus Torvalds .mode = 0644, 6161cb9e684eSMaciej Żenczykowski .proc_handler = proc_dointvec_minmax, 6162cb9e684eSMaciej Żenczykowski .extra1 = (void *)&one, 6163cb9e684eSMaciej Żenczykowski .extra2 = (void *)&two_five_five, 61641da177e4SLinus Torvalds }, 61651da177e4SLinus Torvalds { 61661da177e4SLinus Torvalds .procname = "mtu", 61671da177e4SLinus Torvalds .data = &ipv6_devconf.mtu6, 61681da177e4SLinus Torvalds .maxlen = sizeof(int), 61691da177e4SLinus Torvalds .mode = 0644, 617077751427SMarcelo Leitner .proc_handler = addrconf_sysctl_mtu, 61711da177e4SLinus Torvalds }, 61721da177e4SLinus Torvalds { 61731da177e4SLinus Torvalds .procname = "accept_ra", 61741da177e4SLinus Torvalds .data = &ipv6_devconf.accept_ra, 61751da177e4SLinus Torvalds .maxlen = sizeof(int), 61761da177e4SLinus Torvalds .mode = 0644, 61776d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 61781da177e4SLinus Torvalds }, 61791da177e4SLinus Torvalds { 61801da177e4SLinus Torvalds .procname = "accept_redirects", 61811da177e4SLinus Torvalds .data = &ipv6_devconf.accept_redirects, 61821da177e4SLinus Torvalds .maxlen = sizeof(int), 61831da177e4SLinus Torvalds .mode = 0644, 61846d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 61851da177e4SLinus Torvalds }, 61861da177e4SLinus Torvalds { 61871da177e4SLinus Torvalds .procname = "autoconf", 61881da177e4SLinus Torvalds .data = &ipv6_devconf.autoconf, 61891da177e4SLinus Torvalds .maxlen = sizeof(int), 61901da177e4SLinus Torvalds .mode = 0644, 61916d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 61921da177e4SLinus Torvalds }, 61931da177e4SLinus Torvalds { 61941da177e4SLinus Torvalds .procname = "dad_transmits", 61951da177e4SLinus Torvalds .data = &ipv6_devconf.dad_transmits, 61961da177e4SLinus Torvalds .maxlen = sizeof(int), 61971da177e4SLinus Torvalds .mode = 0644, 61986d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 61991da177e4SLinus Torvalds }, 62001da177e4SLinus Torvalds { 62011da177e4SLinus Torvalds .procname = "router_solicitations", 62021da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicits, 62031da177e4SLinus Torvalds .maxlen = sizeof(int), 62041da177e4SLinus Torvalds .mode = 0644, 6205cb4a4c69SMaciej Żenczykowski .proc_handler = proc_dointvec_minmax, 6206cb4a4c69SMaciej Żenczykowski .extra1 = &minus_one, 62071da177e4SLinus Torvalds }, 62081da177e4SLinus Torvalds { 62091da177e4SLinus Torvalds .procname = "router_solicitation_interval", 62101da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_interval, 62111da177e4SLinus Torvalds .maxlen = sizeof(int), 62121da177e4SLinus Torvalds .mode = 0644, 62136d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 62141da177e4SLinus Torvalds }, 62151da177e4SLinus Torvalds { 6216bd11f074SMaciej Żenczykowski .procname = "router_solicitation_max_interval", 6217bd11f074SMaciej Żenczykowski .data = &ipv6_devconf.rtr_solicit_max_interval, 6218bd11f074SMaciej Żenczykowski .maxlen = sizeof(int), 6219bd11f074SMaciej Żenczykowski .mode = 0644, 6220bd11f074SMaciej Żenczykowski .proc_handler = proc_dointvec_jiffies, 6221bd11f074SMaciej Żenczykowski }, 6222bd11f074SMaciej Żenczykowski { 62231da177e4SLinus Torvalds .procname = "router_solicitation_delay", 62241da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_delay, 62251da177e4SLinus Torvalds .maxlen = sizeof(int), 62261da177e4SLinus Torvalds .mode = 0644, 62276d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 62281da177e4SLinus Torvalds }, 62291da177e4SLinus Torvalds { 62301da177e4SLinus Torvalds .procname = "force_mld_version", 62311da177e4SLinus Torvalds .data = &ipv6_devconf.force_mld_version, 62321da177e4SLinus Torvalds .maxlen = sizeof(int), 62331da177e4SLinus Torvalds .mode = 0644, 62346d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 62351da177e4SLinus Torvalds }, 6236fc4eba58SHannes Frederic Sowa { 6237fc4eba58SHannes Frederic Sowa .procname = "mldv1_unsolicited_report_interval", 6238fc4eba58SHannes Frederic Sowa .data = 6239fc4eba58SHannes Frederic Sowa &ipv6_devconf.mldv1_unsolicited_report_interval, 6240fc4eba58SHannes Frederic Sowa .maxlen = sizeof(int), 6241fc4eba58SHannes Frederic Sowa .mode = 0644, 6242fc4eba58SHannes Frederic Sowa .proc_handler = proc_dointvec_ms_jiffies, 6243fc4eba58SHannes Frederic Sowa }, 6244fc4eba58SHannes Frederic Sowa { 6245fc4eba58SHannes Frederic Sowa .procname = "mldv2_unsolicited_report_interval", 6246fc4eba58SHannes Frederic Sowa .data = 6247fc4eba58SHannes Frederic Sowa &ipv6_devconf.mldv2_unsolicited_report_interval, 6248fc4eba58SHannes Frederic Sowa .maxlen = sizeof(int), 6249fc4eba58SHannes Frederic Sowa .mode = 0644, 6250fc4eba58SHannes Frederic Sowa .proc_handler = proc_dointvec_ms_jiffies, 6251fc4eba58SHannes Frederic Sowa }, 62521da177e4SLinus Torvalds { 62531da177e4SLinus Torvalds .procname = "use_tempaddr", 62541da177e4SLinus Torvalds .data = &ipv6_devconf.use_tempaddr, 62551da177e4SLinus Torvalds .maxlen = sizeof(int), 62561da177e4SLinus Torvalds .mode = 0644, 62576d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 62581da177e4SLinus Torvalds }, 62591da177e4SLinus Torvalds { 62601da177e4SLinus Torvalds .procname = "temp_valid_lft", 62611da177e4SLinus Torvalds .data = &ipv6_devconf.temp_valid_lft, 62621da177e4SLinus Torvalds .maxlen = sizeof(int), 62631da177e4SLinus Torvalds .mode = 0644, 62646d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 62651da177e4SLinus Torvalds }, 62661da177e4SLinus Torvalds { 62671da177e4SLinus Torvalds .procname = "temp_prefered_lft", 62681da177e4SLinus Torvalds .data = &ipv6_devconf.temp_prefered_lft, 62691da177e4SLinus Torvalds .maxlen = sizeof(int), 62701da177e4SLinus Torvalds .mode = 0644, 62716d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 62721da177e4SLinus Torvalds }, 62731da177e4SLinus Torvalds { 62741da177e4SLinus Torvalds .procname = "regen_max_retry", 62751da177e4SLinus Torvalds .data = &ipv6_devconf.regen_max_retry, 62761da177e4SLinus Torvalds .maxlen = sizeof(int), 62771da177e4SLinus Torvalds .mode = 0644, 62786d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 62791da177e4SLinus Torvalds }, 62801da177e4SLinus Torvalds { 62811da177e4SLinus Torvalds .procname = "max_desync_factor", 62821da177e4SLinus Torvalds .data = &ipv6_devconf.max_desync_factor, 62831da177e4SLinus Torvalds .maxlen = sizeof(int), 62841da177e4SLinus Torvalds .mode = 0644, 62856d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 62861da177e4SLinus Torvalds }, 62871da177e4SLinus Torvalds { 62881da177e4SLinus Torvalds .procname = "max_addresses", 62891da177e4SLinus Torvalds .data = &ipv6_devconf.max_addresses, 62901da177e4SLinus Torvalds .maxlen = sizeof(int), 62911da177e4SLinus Torvalds .mode = 0644, 62926d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 62931da177e4SLinus Torvalds }, 62941da177e4SLinus Torvalds { 629565f5c7c1SYOSHIFUJI Hideaki .procname = "accept_ra_defrtr", 629665f5c7c1SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_defrtr, 629765f5c7c1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 629865f5c7c1SYOSHIFUJI Hideaki .mode = 0644, 62996d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 630065f5c7c1SYOSHIFUJI Hideaki }, 630165f5c7c1SYOSHIFUJI Hideaki { 63028013d1d7SHangbin Liu .procname = "accept_ra_min_hop_limit", 63038013d1d7SHangbin Liu .data = &ipv6_devconf.accept_ra_min_hop_limit, 63048013d1d7SHangbin Liu .maxlen = sizeof(int), 63058013d1d7SHangbin Liu .mode = 0644, 63068013d1d7SHangbin Liu .proc_handler = proc_dointvec, 63078013d1d7SHangbin Liu }, 63088013d1d7SHangbin Liu { 6309c4fd30ebSYOSHIFUJI Hideaki .procname = "accept_ra_pinfo", 6310c4fd30ebSYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_pinfo, 6311c4fd30ebSYOSHIFUJI Hideaki .maxlen = sizeof(int), 6312c4fd30ebSYOSHIFUJI Hideaki .mode = 0644, 63136d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 6314c4fd30ebSYOSHIFUJI Hideaki }, 6315930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 6316930d6ff2SYOSHIFUJI Hideaki { 6317930d6ff2SYOSHIFUJI Hideaki .procname = "accept_ra_rtr_pref", 6318930d6ff2SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rtr_pref, 6319930d6ff2SYOSHIFUJI Hideaki .maxlen = sizeof(int), 6320930d6ff2SYOSHIFUJI Hideaki .mode = 0644, 63216d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 6322930d6ff2SYOSHIFUJI Hideaki }, 632352e16356SYOSHIFUJI Hideaki { 632452e16356SYOSHIFUJI Hideaki .procname = "router_probe_interval", 632552e16356SYOSHIFUJI Hideaki .data = &ipv6_devconf.rtr_probe_interval, 632652e16356SYOSHIFUJI Hideaki .maxlen = sizeof(int), 632752e16356SYOSHIFUJI Hideaki .mode = 0644, 63286d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 632952e16356SYOSHIFUJI Hideaki }, 6330fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 633109c884d4SYOSHIFUJI Hideaki { 6332bbea124bSJoel Scherpelz .procname = "accept_ra_rt_info_min_plen", 6333bbea124bSJoel Scherpelz .data = &ipv6_devconf.accept_ra_rt_info_min_plen, 6334bbea124bSJoel Scherpelz .maxlen = sizeof(int), 6335bbea124bSJoel Scherpelz .mode = 0644, 6336bbea124bSJoel Scherpelz .proc_handler = proc_dointvec, 6337bbea124bSJoel Scherpelz }, 6338bbea124bSJoel Scherpelz { 633909c884d4SYOSHIFUJI Hideaki .procname = "accept_ra_rt_info_max_plen", 634009c884d4SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rt_info_max_plen, 634109c884d4SYOSHIFUJI Hideaki .maxlen = sizeof(int), 634209c884d4SYOSHIFUJI Hideaki .mode = 0644, 63436d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 634409c884d4SYOSHIFUJI Hideaki }, 634509c884d4SYOSHIFUJI Hideaki #endif 6346930d6ff2SYOSHIFUJI Hideaki #endif 6347c4fd30ebSYOSHIFUJI Hideaki { 6348fbea49e1SYOSHIFUJI Hideaki .procname = "proxy_ndp", 6349fbea49e1SYOSHIFUJI Hideaki .data = &ipv6_devconf.proxy_ndp, 6350fbea49e1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 6351fbea49e1SYOSHIFUJI Hideaki .mode = 0644, 6352c92d5491Sstephen hemminger .proc_handler = addrconf_sysctl_proxy_ndp, 6353fbea49e1SYOSHIFUJI Hideaki }, 6354fbea49e1SYOSHIFUJI Hideaki { 63550bcbc926SYOSHIFUJI Hideaki .procname = "accept_source_route", 63560bcbc926SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_source_route, 63570bcbc926SYOSHIFUJI Hideaki .maxlen = sizeof(int), 63580bcbc926SYOSHIFUJI Hideaki .mode = 0644, 63596d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 63600bcbc926SYOSHIFUJI Hideaki }, 636195c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 636295c385b4SNeil Horman { 636395c385b4SNeil Horman .procname = "optimistic_dad", 636495c385b4SNeil Horman .data = &ipv6_devconf.optimistic_dad, 636595c385b4SNeil Horman .maxlen = sizeof(int), 636695c385b4SNeil Horman .mode = 0644, 63676d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 636895c385b4SNeil Horman }, 63697fd2561eSErik Kline { 63707fd2561eSErik Kline .procname = "use_optimistic", 63717fd2561eSErik Kline .data = &ipv6_devconf.use_optimistic, 63727fd2561eSErik Kline .maxlen = sizeof(int), 63737fd2561eSErik Kline .mode = 0644, 63747fd2561eSErik Kline .proc_handler = proc_dointvec, 63757fd2561eSErik Kline }, 637695c385b4SNeil Horman #endif 63777bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 63787bc570c8SYOSHIFUJI Hideaki { 63797bc570c8SYOSHIFUJI Hideaki .procname = "mc_forwarding", 63807bc570c8SYOSHIFUJI Hideaki .data = &ipv6_devconf.mc_forwarding, 63817bc570c8SYOSHIFUJI Hideaki .maxlen = sizeof(int), 6382a4e6db07SDavid S. Miller .mode = 0444, 63836d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 63847bc570c8SYOSHIFUJI Hideaki }, 63857bc570c8SYOSHIFUJI Hideaki #endif 63860bcbc926SYOSHIFUJI Hideaki { 6387778d80beSYOSHIFUJI Hideaki .procname = "disable_ipv6", 6388778d80beSYOSHIFUJI Hideaki .data = &ipv6_devconf.disable_ipv6, 6389778d80beSYOSHIFUJI Hideaki .maxlen = sizeof(int), 6390778d80beSYOSHIFUJI Hideaki .mode = 0644, 639156d417b1SBrian Haley .proc_handler = addrconf_sysctl_disable, 6392778d80beSYOSHIFUJI Hideaki }, 6393778d80beSYOSHIFUJI Hideaki { 63941b34be74SYOSHIFUJI Hideaki .procname = "accept_dad", 63951b34be74SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_dad, 63961b34be74SYOSHIFUJI Hideaki .maxlen = sizeof(int), 63971b34be74SYOSHIFUJI Hideaki .mode = 0644, 63986d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 63991b34be74SYOSHIFUJI Hideaki }, 64001b34be74SYOSHIFUJI Hideaki { 6401f7734fdfSOctavian Purdila .procname = "force_tllao", 6402f7734fdfSOctavian Purdila .data = &ipv6_devconf.force_tllao, 6403f7734fdfSOctavian Purdila .maxlen = sizeof(int), 6404f7734fdfSOctavian Purdila .mode = 0644, 6405f7734fdfSOctavian Purdila .proc_handler = proc_dointvec 6406f7734fdfSOctavian Purdila }, 6407f7734fdfSOctavian Purdila { 64085cb04436SHannes Frederic Sowa .procname = "ndisc_notify", 64095cb04436SHannes Frederic Sowa .data = &ipv6_devconf.ndisc_notify, 64105cb04436SHannes Frederic Sowa .maxlen = sizeof(int), 64115cb04436SHannes Frederic Sowa .mode = 0644, 64125cb04436SHannes Frederic Sowa .proc_handler = proc_dointvec 64135cb04436SHannes Frederic Sowa }, 64145cb04436SHannes Frederic Sowa { 6415b800c3b9SHannes Frederic Sowa .procname = "suppress_frag_ndisc", 6416b800c3b9SHannes Frederic Sowa .data = &ipv6_devconf.suppress_frag_ndisc, 6417b800c3b9SHannes Frederic Sowa .maxlen = sizeof(int), 6418b800c3b9SHannes Frederic Sowa .mode = 0644, 6419b800c3b9SHannes Frederic Sowa .proc_handler = proc_dointvec 6420b800c3b9SHannes Frederic Sowa }, 6421b800c3b9SHannes Frederic Sowa { 6422d9333196SBen Greear .procname = "accept_ra_from_local", 6423d9333196SBen Greear .data = &ipv6_devconf.accept_ra_from_local, 6424d9333196SBen Greear .maxlen = sizeof(int), 6425d9333196SBen Greear .mode = 0644, 6426d9333196SBen Greear .proc_handler = proc_dointvec, 6427d9333196SBen Greear }, 6428d9333196SBen Greear { 6429c2943f14SHarout Hedeshian .procname = "accept_ra_mtu", 6430c2943f14SHarout Hedeshian .data = &ipv6_devconf.accept_ra_mtu, 6431c2943f14SHarout Hedeshian .maxlen = sizeof(int), 6432c2943f14SHarout Hedeshian .mode = 0644, 6433c2943f14SHarout Hedeshian .proc_handler = proc_dointvec, 6434c2943f14SHarout Hedeshian }, 6435c2943f14SHarout Hedeshian { 64363d1bec99SHannes Frederic Sowa .procname = "stable_secret", 64373d1bec99SHannes Frederic Sowa .data = &ipv6_devconf.stable_secret, 64383d1bec99SHannes Frederic Sowa .maxlen = IPV6_MAX_STRLEN, 64393d1bec99SHannes Frederic Sowa .mode = 0600, 64403d1bec99SHannes Frederic Sowa .proc_handler = addrconf_sysctl_stable_secret, 64413d1bec99SHannes Frederic Sowa }, 64423d1bec99SHannes Frederic Sowa { 64433985e8a3SErik Kline .procname = "use_oif_addrs_only", 64443985e8a3SErik Kline .data = &ipv6_devconf.use_oif_addrs_only, 64453985e8a3SErik Kline .maxlen = sizeof(int), 64463985e8a3SErik Kline .mode = 0644, 64473985e8a3SErik Kline .proc_handler = proc_dointvec, 644835103d11SAndy Gospodarek }, 644935103d11SAndy Gospodarek { 645035103d11SAndy Gospodarek .procname = "ignore_routes_with_linkdown", 645135103d11SAndy Gospodarek .data = &ipv6_devconf.ignore_routes_with_linkdown, 645235103d11SAndy Gospodarek .maxlen = sizeof(int), 645335103d11SAndy Gospodarek .mode = 0644, 645435103d11SAndy Gospodarek .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown, 64553985e8a3SErik Kline }, 64563985e8a3SErik Kline { 6457abbc3043SJohannes Berg .procname = "drop_unicast_in_l2_multicast", 6458abbc3043SJohannes Berg .data = &ipv6_devconf.drop_unicast_in_l2_multicast, 6459abbc3043SJohannes Berg .maxlen = sizeof(int), 6460abbc3043SJohannes Berg .mode = 0644, 6461abbc3043SJohannes Berg .proc_handler = proc_dointvec, 6462abbc3043SJohannes Berg }, 6463abbc3043SJohannes Berg { 64647a02bf89SJohannes Berg .procname = "drop_unsolicited_na", 64657a02bf89SJohannes Berg .data = &ipv6_devconf.drop_unsolicited_na, 64667a02bf89SJohannes Berg .maxlen = sizeof(int), 64677a02bf89SJohannes Berg .mode = 0644, 64687a02bf89SJohannes Berg .proc_handler = proc_dointvec, 64697a02bf89SJohannes Berg }, 64707a02bf89SJohannes Berg { 6471f1705ec1SDavid Ahern .procname = "keep_addr_on_down", 6472f1705ec1SDavid Ahern .data = &ipv6_devconf.keep_addr_on_down, 6473f1705ec1SDavid Ahern .maxlen = sizeof(int), 6474f1705ec1SDavid Ahern .mode = 0644, 6475f1705ec1SDavid Ahern .proc_handler = proc_dointvec, 6476f1705ec1SDavid Ahern 6477f1705ec1SDavid Ahern }, 6478f1705ec1SDavid Ahern { 64791ababebaSDavid Lebrun .procname = "seg6_enabled", 64801ababebaSDavid Lebrun .data = &ipv6_devconf.seg6_enabled, 64811ababebaSDavid Lebrun .maxlen = sizeof(int), 64821ababebaSDavid Lebrun .mode = 0644, 64831ababebaSDavid Lebrun .proc_handler = proc_dointvec, 64841ababebaSDavid Lebrun }, 6485bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC 6486bf355b8dSDavid Lebrun { 6487bf355b8dSDavid Lebrun .procname = "seg6_require_hmac", 6488bf355b8dSDavid Lebrun .data = &ipv6_devconf.seg6_require_hmac, 6489bf355b8dSDavid Lebrun .maxlen = sizeof(int), 6490bf355b8dSDavid Lebrun .mode = 0644, 6491bf355b8dSDavid Lebrun .proc_handler = proc_dointvec, 6492bf355b8dSDavid Lebrun }, 6493bf355b8dSDavid Lebrun #endif 64941ababebaSDavid Lebrun { 6495adc176c5SErik Nordmark .procname = "enhanced_dad", 6496adc176c5SErik Nordmark .data = &ipv6_devconf.enhanced_dad, 6497adc176c5SErik Nordmark .maxlen = sizeof(int), 6498adc176c5SErik Nordmark .mode = 0644, 6499adc176c5SErik Nordmark .proc_handler = proc_dointvec, 6500adc176c5SErik Nordmark }, 6501adc176c5SErik Nordmark { 6502d35a00b8SFelix Jia .procname = "addr_gen_mode", 6503d35a00b8SFelix Jia .data = &ipv6_devconf.addr_gen_mode, 6504d35a00b8SFelix Jia .maxlen = sizeof(int), 6505d35a00b8SFelix Jia .mode = 0644, 6506d35a00b8SFelix Jia .proc_handler = addrconf_sysctl_addr_gen_mode, 6507d35a00b8SFelix Jia }, 6508d35a00b8SFelix Jia { 6509df789fe7SDavid Forster .procname = "disable_policy", 6510df789fe7SDavid Forster .data = &ipv6_devconf.disable_policy, 6511df789fe7SDavid Forster .maxlen = sizeof(int), 6512df789fe7SDavid Forster .mode = 0644, 6513df789fe7SDavid Forster .proc_handler = addrconf_sysctl_disable_policy, 6514df789fe7SDavid Forster }, 6515df789fe7SDavid Forster { 65162210d6b2SMaciej Żenczykowski .procname = "ndisc_tclass", 65172210d6b2SMaciej Żenczykowski .data = &ipv6_devconf.ndisc_tclass, 65182210d6b2SMaciej Żenczykowski .maxlen = sizeof(int), 65192210d6b2SMaciej Żenczykowski .mode = 0644, 65202210d6b2SMaciej Żenczykowski .proc_handler = proc_dointvec_minmax, 65212210d6b2SMaciej Żenczykowski .extra1 = (void *)&zero, 65222210d6b2SMaciej Żenczykowski .extra2 = (void *)&two_five_five, 65232210d6b2SMaciej Żenczykowski }, 65242210d6b2SMaciej Żenczykowski { 6525f8572d8fSEric W. Biederman /* sentinel */ 65261da177e4SLinus Torvalds } 65271da177e4SLinus Torvalds }; 65281da177e4SLinus Torvalds 6529bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name, 6530f8572d8fSEric W. Biederman struct inet6_dev *idev, struct ipv6_devconf *p) 65311da177e4SLinus Torvalds { 653229c994e3SNicolas Dichtel int i, ifindex; 6533607ea7cdSKonstantin Khlebnikov struct ctl_table *table; 65346105e293SEric W. Biederman char path[sizeof("net/ipv6/conf/") + IFNAMSIZ]; 65351dab6222SPavel Emelyanov 6536607ea7cdSKonstantin Khlebnikov table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL); 6537607ea7cdSKonstantin Khlebnikov if (!table) 6538f68635e6SPavel Emelyanov goto out; 6539f68635e6SPavel Emelyanov 6540607ea7cdSKonstantin Khlebnikov for (i = 0; table[i].data; i++) { 6541607ea7cdSKonstantin Khlebnikov table[i].data += (char *)p - (char *)&ipv6_devconf; 6542cb9e684eSMaciej Żenczykowski /* If one of these is already set, then it is not safe to 6543cb9e684eSMaciej Żenczykowski * overwrite either of them: this makes proc_dointvec_minmax 6544cb9e684eSMaciej Żenczykowski * usable. 6545cb9e684eSMaciej Żenczykowski */ 6546cb9e684eSMaciej Żenczykowski if (!table[i].extra1 && !table[i].extra2) { 6547607ea7cdSKonstantin Khlebnikov table[i].extra1 = idev; /* embedded; no ref */ 6548607ea7cdSKonstantin Khlebnikov table[i].extra2 = net; 65491da177e4SLinus Torvalds } 6550cb9e684eSMaciej Żenczykowski } 65511da177e4SLinus Torvalds 65526105e293SEric W. Biederman snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name); 65531da177e4SLinus Torvalds 6554607ea7cdSKonstantin Khlebnikov p->sysctl_header = register_net_sysctl(net, path, table); 6555607ea7cdSKonstantin Khlebnikov if (!p->sysctl_header) 65566105e293SEric W. Biederman goto free; 6557f68635e6SPavel Emelyanov 655829c994e3SNicolas Dichtel if (!strcmp(dev_name, "all")) 655929c994e3SNicolas Dichtel ifindex = NETCONFA_IFINDEX_ALL; 656029c994e3SNicolas Dichtel else if (!strcmp(dev_name, "default")) 656129c994e3SNicolas Dichtel ifindex = NETCONFA_IFINDEX_DEFAULT; 656229c994e3SNicolas Dichtel else 656329c994e3SNicolas Dichtel ifindex = idev->dev->ifindex; 656485b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL, 656585b3daadSDavid Ahern ifindex, p); 656695897312SPavel Emelyanov return 0; 65671da177e4SLinus Torvalds 65681da177e4SLinus Torvalds free: 6569607ea7cdSKonstantin Khlebnikov kfree(table); 6570f68635e6SPavel Emelyanov out: 657195897312SPavel Emelyanov return -ENOBUFS; 65721da177e4SLinus Torvalds } 65731da177e4SLinus Torvalds 657423452170SDavid Ahern static void __addrconf_sysctl_unregister(struct net *net, 657523452170SDavid Ahern struct ipv6_devconf *p, int ifindex) 6576f52295a9SPavel Emelyanov { 6577607ea7cdSKonstantin Khlebnikov struct ctl_table *table; 6578f52295a9SPavel Emelyanov 6579607ea7cdSKonstantin Khlebnikov if (!p->sysctl_header) 6580408c4768SPavel Emelyanov return; 6581408c4768SPavel Emelyanov 6582607ea7cdSKonstantin Khlebnikov table = p->sysctl_header->ctl_table_arg; 6583607ea7cdSKonstantin Khlebnikov unregister_net_sysctl_table(p->sysctl_header); 6584607ea7cdSKonstantin Khlebnikov p->sysctl_header = NULL; 6585607ea7cdSKonstantin Khlebnikov kfree(table); 658623452170SDavid Ahern 658723452170SDavid Ahern inet6_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL); 65881da177e4SLinus Torvalds } 6589408c4768SPavel Emelyanov 6590a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev) 6591408c4768SPavel Emelyanov { 6592a317a2f1SWANG Cong int err; 6593a317a2f1SWANG Cong 6594a317a2f1SWANG Cong if (!sysctl_dev_name_is_allowed(idev->dev->name)) 6595a317a2f1SWANG Cong return -EINVAL; 6596a317a2f1SWANG Cong 6597a317a2f1SWANG Cong err = neigh_sysctl_register(idev->dev, idev->nd_parms, 6598f8572d8fSEric W. Biederman &ndisc_ifinfo_sysctl_change); 6599a317a2f1SWANG Cong if (err) 6600a317a2f1SWANG Cong return err; 6601a317a2f1SWANG Cong err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name, 6602f8572d8fSEric W. Biederman idev, &idev->cnf); 6603a317a2f1SWANG Cong if (err) 6604a317a2f1SWANG Cong neigh_sysctl_unregister(idev->nd_parms); 6605a317a2f1SWANG Cong 6606a317a2f1SWANG Cong return err; 6607408c4768SPavel Emelyanov } 6608408c4768SPavel Emelyanov 6609408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev) 6610408c4768SPavel Emelyanov { 661123452170SDavid Ahern __addrconf_sysctl_unregister(dev_net(idev->dev), &idev->cnf, 661223452170SDavid Ahern idev->dev->ifindex); 6613408c4768SPavel Emelyanov neigh_sysctl_unregister(idev->nd_parms); 66141da177e4SLinus Torvalds } 66151da177e4SLinus Torvalds 66161da177e4SLinus Torvalds 66171da177e4SLinus Torvalds #endif 66181da177e4SLinus Torvalds 66192c8c1e72SAlexey Dobriyan static int __net_init addrconf_init_net(struct net *net) 6620e0da5a48SPavel Emelyanov { 6621a79ca223SHong Zhiguo int err = -ENOMEM; 6622e0da5a48SPavel Emelyanov struct ipv6_devconf *all, *dflt; 6623e0da5a48SPavel Emelyanov 6624a79ca223SHong Zhiguo all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL); 662563159f29SIan Morris if (!all) 6626e0da5a48SPavel Emelyanov goto err_alloc_all; 6627e0da5a48SPavel Emelyanov 6628a79ca223SHong Zhiguo dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL); 662963159f29SIan Morris if (!dflt) 6630e0da5a48SPavel Emelyanov goto err_alloc_dflt; 6631a79ca223SHong Zhiguo 663256d417b1SBrian Haley /* these will be inherited by all namespaces */ 663356d417b1SBrian Haley dflt->autoconf = ipv6_defaults.autoconf; 663456d417b1SBrian Haley dflt->disable_ipv6 = ipv6_defaults.disable_ipv6; 6635e0da5a48SPavel Emelyanov 66363d1bec99SHannes Frederic Sowa dflt->stable_secret.initialized = false; 66373d1bec99SHannes Frederic Sowa all->stable_secret.initialized = false; 66383d1bec99SHannes Frederic Sowa 6639e0da5a48SPavel Emelyanov net->ipv6.devconf_all = all; 6640e0da5a48SPavel Emelyanov net->ipv6.devconf_dflt = dflt; 6641e0da5a48SPavel Emelyanov 6642e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 6643f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "all", NULL, all); 6644e0da5a48SPavel Emelyanov if (err < 0) 6645e0da5a48SPavel Emelyanov goto err_reg_all; 6646e0da5a48SPavel Emelyanov 6647f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "default", NULL, dflt); 6648e0da5a48SPavel Emelyanov if (err < 0) 6649e0da5a48SPavel Emelyanov goto err_reg_dflt; 6650e0da5a48SPavel Emelyanov #endif 6651e0da5a48SPavel Emelyanov return 0; 6652e0da5a48SPavel Emelyanov 6653e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 6654e0da5a48SPavel Emelyanov err_reg_dflt: 665523452170SDavid Ahern __addrconf_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL); 6656e0da5a48SPavel Emelyanov err_reg_all: 6657e0da5a48SPavel Emelyanov kfree(dflt); 6658e0da5a48SPavel Emelyanov #endif 6659e0da5a48SPavel Emelyanov err_alloc_dflt: 6660e0da5a48SPavel Emelyanov kfree(all); 6661e0da5a48SPavel Emelyanov err_alloc_all: 6662e0da5a48SPavel Emelyanov return err; 6663e0da5a48SPavel Emelyanov } 6664e0da5a48SPavel Emelyanov 66652c8c1e72SAlexey Dobriyan static void __net_exit addrconf_exit_net(struct net *net) 6666e0da5a48SPavel Emelyanov { 6667e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 666823452170SDavid Ahern __addrconf_sysctl_unregister(net, net->ipv6.devconf_dflt, 666923452170SDavid Ahern NETCONFA_IFINDEX_DEFAULT); 667023452170SDavid Ahern __addrconf_sysctl_unregister(net, net->ipv6.devconf_all, 667123452170SDavid Ahern NETCONFA_IFINDEX_ALL); 6672e0da5a48SPavel Emelyanov #endif 6673e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_dflt); 6674e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_all); 6675e0da5a48SPavel Emelyanov } 6676e0da5a48SPavel Emelyanov 6677e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = { 6678e0da5a48SPavel Emelyanov .init = addrconf_init_net, 6679e0da5a48SPavel Emelyanov .exit = addrconf_exit_net, 6680e0da5a48SPavel Emelyanov }; 6681e0da5a48SPavel Emelyanov 6682207895fdSDaniel Borkmann static struct rtnl_af_ops inet6_ops __read_mostly = { 6683b382b191SThomas Graf .family = AF_INET6, 6684b382b191SThomas Graf .fill_link_af = inet6_fill_link_af, 6685b382b191SThomas Graf .get_link_af_size = inet6_get_link_af_size, 668611b1f828SDaniel Borkmann .validate_link_af = inet6_validate_link_af, 6687f53adae4SDaniel Borkmann .set_link_af = inet6_set_link_af, 6688b382b191SThomas Graf }; 6689b382b191SThomas Graf 66901da177e4SLinus Torvalds /* 66911da177e4SLinus Torvalds * Init / cleanup code 66921da177e4SLinus Torvalds */ 66931da177e4SLinus Torvalds 66941da177e4SLinus Torvalds int __init addrconf_init(void) 66951da177e4SLinus Torvalds { 6696a317a2f1SWANG Cong struct inet6_dev *idev; 6697c2e21293Sstephen hemminger int i, err; 66982a8cc6c8SYOSHIFUJI Hideaki 6699e21e8467SStephen Hemminger err = ipv6_addr_label_init(); 6700e21e8467SStephen Hemminger if (err < 0) { 6701f3213831SJoe Perches pr_crit("%s: cannot initialize default policy table: %d\n", 6702f3213831SJoe Perches __func__, err); 67032cc6d2bfSNeil Horman goto out; 67042a8cc6c8SYOSHIFUJI Hideaki } 67051da177e4SLinus Torvalds 67062cc6d2bfSNeil Horman err = register_pernet_subsys(&addrconf_ops); 67072cc6d2bfSNeil Horman if (err < 0) 67082cc6d2bfSNeil Horman goto out_addrlabel; 6709e0da5a48SPavel Emelyanov 6710c15b1ccaSHannes Frederic Sowa addrconf_wq = create_workqueue("ipv6_addrconf"); 6711c15b1ccaSHannes Frederic Sowa if (!addrconf_wq) { 6712c15b1ccaSHannes Frederic Sowa err = -ENOMEM; 6713c15b1ccaSHannes Frederic Sowa goto out_nowq; 6714c15b1ccaSHannes Frederic Sowa } 6715c15b1ccaSHannes Frederic Sowa 67161da177e4SLinus Torvalds /* The addrconf netdev notifier requires that loopback_dev 67171da177e4SLinus Torvalds * has it's ipv6 private information allocated and setup 67181da177e4SLinus Torvalds * before it can bring up and give link-local addresses 67191da177e4SLinus Torvalds * to other devices which are up. 67201da177e4SLinus Torvalds * 67211da177e4SLinus Torvalds * Unfortunately, loopback_dev is not necessarily the first 67221da177e4SLinus Torvalds * entry in the global dev_base list of net devices. In fact, 67231da177e4SLinus Torvalds * it is likely to be the very last entry on that list. 67241da177e4SLinus Torvalds * So this causes the notifier registry below to try and 67251da177e4SLinus Torvalds * give link-local addresses to all devices besides loopback_dev 67261da177e4SLinus Torvalds * first, then loopback_dev, which cases all the non-loopback_dev 67271da177e4SLinus Torvalds * devices to fail to get a link-local address. 67281da177e4SLinus Torvalds * 67291da177e4SLinus Torvalds * So, as a temporary fix, allocate the ipv6 structure for 67301da177e4SLinus Torvalds * loopback_dev first by hand. 67311da177e4SLinus Torvalds * Longer term, all of the dependencies ipv6 has upon the loopback 67321da177e4SLinus Torvalds * device and it being up should be removed. 67331da177e4SLinus Torvalds */ 67341da177e4SLinus Torvalds rtnl_lock(); 6735a317a2f1SWANG Cong idev = ipv6_add_dev(init_net.loopback_dev); 67361da177e4SLinus Torvalds rtnl_unlock(); 6737a317a2f1SWANG Cong if (IS_ERR(idev)) { 6738a317a2f1SWANG Cong err = PTR_ERR(idev); 6739e0da5a48SPavel Emelyanov goto errlo; 6740a317a2f1SWANG Cong } 67411da177e4SLinus Torvalds 67422f460933SWANG Cong ip6_route_init_special_entries(); 67432f460933SWANG Cong 6744c2e21293Sstephen hemminger for (i = 0; i < IN6_ADDR_HSIZE; i++) 6745c2e21293Sstephen hemminger INIT_HLIST_HEAD(&inet6_addr_lst[i]); 6746c2e21293Sstephen hemminger 67471da177e4SLinus Torvalds register_netdevice_notifier(&ipv6_dev_notf); 67481da177e4SLinus Torvalds 6749c15b1ccaSHannes Frederic Sowa addrconf_verify(); 6750c127ea2cSThomas Graf 67513678a9d8Sstephen hemminger rtnl_af_register(&inet6_ops); 6752b382b191SThomas Graf 675316feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETLINK, 675416feebcfSFlorian Westphal NULL, inet6_dump_ifinfo, 0); 6755c127ea2cSThomas Graf if (err < 0) 6756c127ea2cSThomas Graf goto errout; 6757c127ea2cSThomas Graf 675816feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWADDR, 675916feebcfSFlorian Westphal inet6_rtm_newaddr, NULL, 0); 676016feebcfSFlorian Westphal if (err < 0) 676116feebcfSFlorian Westphal goto errout; 676216feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELADDR, 676316feebcfSFlorian Westphal inet6_rtm_deladdr, NULL, 0); 676416feebcfSFlorian Westphal if (err < 0) 676516feebcfSFlorian Westphal goto errout; 676616feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETADDR, 676716feebcfSFlorian Westphal inet6_rtm_getaddr, inet6_dump_ifaddr, 676816feebcfSFlorian Westphal RTNL_FLAG_DOIT_UNLOCKED); 676916feebcfSFlorian Westphal if (err < 0) 677016feebcfSFlorian Westphal goto errout; 677116feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETMULTICAST, 677216feebcfSFlorian Westphal NULL, inet6_dump_ifmcaddr, 0); 677316feebcfSFlorian Westphal if (err < 0) 677416feebcfSFlorian Westphal goto errout; 677516feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETANYCAST, 677616feebcfSFlorian Westphal NULL, inet6_dump_ifacaddr, 0); 677716feebcfSFlorian Westphal if (err < 0) 677816feebcfSFlorian Westphal goto errout; 677916feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETNETCONF, 678016feebcfSFlorian Westphal inet6_netconf_get_devconf, 678116feebcfSFlorian Westphal inet6_netconf_dump_devconf, 678216feebcfSFlorian Westphal RTNL_FLAG_DOIT_UNLOCKED); 678316feebcfSFlorian Westphal if (err < 0) 678416feebcfSFlorian Westphal goto errout; 6785a3fde2adSFlorian Westphal err = ipv6_addr_label_rtnl_register(); 6786a3fde2adSFlorian Westphal if (err < 0) 6787a3fde2adSFlorian Westphal goto errout; 67882a8cc6c8SYOSHIFUJI Hideaki 67891da177e4SLinus Torvalds return 0; 6790c127ea2cSThomas Graf errout: 679116feebcfSFlorian Westphal rtnl_unregister_all(PF_INET6); 6792b382b191SThomas Graf rtnl_af_unregister(&inet6_ops); 6793c127ea2cSThomas Graf unregister_netdevice_notifier(&ipv6_dev_notf); 6794e0da5a48SPavel Emelyanov errlo: 6795c15b1ccaSHannes Frederic Sowa destroy_workqueue(addrconf_wq); 6796c15b1ccaSHannes Frederic Sowa out_nowq: 6797e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 67982cc6d2bfSNeil Horman out_addrlabel: 67992cc6d2bfSNeil Horman ipv6_addr_label_cleanup(); 68002cc6d2bfSNeil Horman out: 6801c127ea2cSThomas Graf return err; 68021da177e4SLinus Torvalds } 68031da177e4SLinus Torvalds 680409f7709fSDaniel Lezcano void addrconf_cleanup(void) 68051da177e4SLinus Torvalds { 6806176c39afSDaniel Lezcano struct net_device *dev; 68071da177e4SLinus Torvalds int i; 68081da177e4SLinus Torvalds 68091da177e4SLinus Torvalds unregister_netdevice_notifier(&ipv6_dev_notf); 6810e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 68112cc6d2bfSNeil Horman ipv6_addr_label_cleanup(); 68121da177e4SLinus Torvalds 68135c45121dSFlorian Westphal rtnl_af_unregister(&inet6_ops); 68141da177e4SLinus Torvalds 68155c45121dSFlorian Westphal rtnl_lock(); 6816b382b191SThomas Graf 6817176c39afSDaniel Lezcano /* clean dev list */ 6818176c39afSDaniel Lezcano for_each_netdev(&init_net, dev) { 6819176c39afSDaniel Lezcano if (__in6_dev_get(dev) == NULL) 6820176c39afSDaniel Lezcano continue; 6821176c39afSDaniel Lezcano addrconf_ifdown(dev, 1); 6822176c39afSDaniel Lezcano } 6823176c39afSDaniel Lezcano addrconf_ifdown(init_net.loopback_dev, 2); 6824176c39afSDaniel Lezcano 68251da177e4SLinus Torvalds /* 68261da177e4SLinus Torvalds * Check hash table. 68271da177e4SLinus Torvalds */ 68285c578aedSstephen hemminger spin_lock_bh(&addrconf_hash_lock); 6829c2e21293Sstephen hemminger for (i = 0; i < IN6_ADDR_HSIZE; i++) 6830c2e21293Sstephen hemminger WARN_ON(!hlist_empty(&inet6_addr_lst[i])); 68315c578aedSstephen hemminger spin_unlock_bh(&addrconf_hash_lock); 6832c15b1ccaSHannes Frederic Sowa cancel_delayed_work(&addr_chk_work); 68331da177e4SLinus Torvalds rtnl_unlock(); 6834c15b1ccaSHannes Frederic Sowa 6835c15b1ccaSHannes Frederic Sowa destroy_workqueue(addrconf_wq); 68361da177e4SLinus Torvalds } 6837