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 else 389d35a00b8SFelix Jia ndev->cnf.addr_gen_mode = ipv6_devconf_dflt.addr_gen_mode; 3909b29c696SHannes Frederic Sowa 3911da177e4SLinus Torvalds ndev->cnf.mtu6 = dev->mtu; 3921da177e4SLinus Torvalds ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl); 39363159f29SIan Morris if (!ndev->nd_parms) { 3941da177e4SLinus Torvalds kfree(ndev); 395a317a2f1SWANG Cong return ERR_PTR(err); 3961da177e4SLinus Torvalds } 3970187bdfbSBen Hutchings if (ndev->cnf.forwarding) 3980187bdfbSBen Hutchings dev_disable_lro(dev); 3991da177e4SLinus Torvalds /* We refer to the device */ 4001da177e4SLinus Torvalds dev_hold(dev); 4011da177e4SLinus Torvalds 4021da177e4SLinus Torvalds if (snmp6_alloc_dev(ndev) < 0) { 403e32ac250SJoe Perches netdev_dbg(dev, "%s: cannot allocate memory for statistics\n", 404e32ac250SJoe Perches __func__); 4051da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, ndev->nd_parms); 4068603e33dSRoy Li dev_put(dev); 4078603e33dSRoy Li kfree(ndev); 408a317a2f1SWANG Cong return ERR_PTR(err); 4091da177e4SLinus Torvalds } 4101da177e4SLinus Torvalds 4111da177e4SLinus Torvalds if (snmp6_register_dev(ndev) < 0) { 412e32ac250SJoe Perches netdev_dbg(dev, "%s: cannot create /proc/net/dev_snmp6/%s\n", 413ba3542e1Sdingtianhong __func__, dev->name); 414a317a2f1SWANG Cong goto err_release; 4151da177e4SLinus Torvalds } 4161da177e4SLinus Torvalds 4179d6280daSJiri Bohac /* One reference from device. */ 4181be92460SReshetova, Elena refcount_set(&ndev->refcnt, 1); 4191da177e4SLinus Torvalds 4201b34be74SYOSHIFUJI Hideaki if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) 4211b34be74SYOSHIFUJI Hideaki ndev->cnf.accept_dad = -1; 4221b34be74SYOSHIFUJI Hideaki 42307a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 424c7dc89c0SFred L. Templin if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) { 425f3213831SJoe Perches pr_info("%s: Disabled Multicast RS\n", dev->name); 426c7dc89c0SFred L. Templin ndev->cnf.rtr_solicits = 0; 427c7dc89c0SFred L. Templin } 428b077d7abSYOSHIFUJI Hideaki #endif 429b077d7abSYOSHIFUJI Hideaki 430372e6c8fSstephen hemminger INIT_LIST_HEAD(&ndev->tempaddr_list); 43176506a98SJiri Bohac ndev->desync_factor = U32_MAX; 4321da177e4SLinus Torvalds if ((dev->flags&IFF_LOOPBACK) || 4331da177e4SLinus Torvalds dev->type == ARPHRD_TUNNEL || 4349625ed72SYOSHIFUJI Hideaki dev->type == ARPHRD_TUNNEL6 || 4351da177e4SLinus Torvalds dev->type == ARPHRD_SIT || 4361da177e4SLinus Torvalds dev->type == ARPHRD_NONE) { 4370be669bbSJoerg Roedel ndev->cnf.use_tempaddr = -1; 4389d6280daSJiri Bohac } else 4399d6280daSJiri Bohac ipv6_regen_rndid(ndev); 4405d9efa7eSDavid S. Miller 441914faa14SDaniel Borkmann ndev->token = in6addr_any; 4421da177e4SLinus Torvalds 4431f372c7bSMike Manning if (netif_running(dev) && addrconf_link_ready(dev)) 44453aadcc9SHerbert Xu ndev->if_flags |= IF_READY; 44553aadcc9SHerbert Xu 4461da177e4SLinus Torvalds ipv6_mc_init_dev(ndev); 4471da177e4SLinus Torvalds ndev->tstamp = jiffies; 448a317a2f1SWANG Cong err = addrconf_sysctl_register(ndev); 449a317a2f1SWANG Cong if (err) { 450a317a2f1SWANG Cong ipv6_mc_destroy_dev(ndev); 4512a189f9eSSabrina Dubroca snmp6_unregister_dev(ndev); 452a317a2f1SWANG Cong goto err_release; 453a317a2f1SWANG Cong } 45430c4cf57SDavid L Stevens /* protected by rtnl_lock */ 455cf778b00SEric Dumazet rcu_assign_pointer(dev->ip6_ptr, ndev); 456d88ae4ccSYOSHIFUJI Hideaki 4572c5e8933SHannes Frederic Sowa /* Join interface-local all-node multicast group */ 4582c5e8933SHannes Frederic Sowa ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes); 4592c5e8933SHannes Frederic Sowa 460d88ae4ccSYOSHIFUJI Hideaki /* Join all-node multicast group */ 461f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes); 462d88ae4ccSYOSHIFUJI Hideaki 463d6ddef9eSLi Wei /* Join all-router multicast group if forwarding is set */ 4648b2aaedeSLi Wei if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST)) 465d6ddef9eSLi Wei ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters); 466d6ddef9eSLi Wei 4671da177e4SLinus Torvalds return ndev; 468a317a2f1SWANG Cong 469a317a2f1SWANG Cong err_release: 470a317a2f1SWANG Cong neigh_parms_release(&nd_tbl, ndev->nd_parms); 471a317a2f1SWANG Cong ndev->dead = 1; 472a317a2f1SWANG Cong in6_dev_finish_destroy(ndev); 473a317a2f1SWANG Cong return ERR_PTR(err); 4741da177e4SLinus Torvalds } 4751da177e4SLinus Torvalds 476cee89473SYOSHIFUJI Hideaki static struct inet6_dev *ipv6_find_idev(struct net_device *dev) 4771da177e4SLinus Torvalds { 4781da177e4SLinus Torvalds struct inet6_dev *idev; 4791da177e4SLinus Torvalds 4801da177e4SLinus Torvalds ASSERT_RTNL(); 4811da177e4SLinus Torvalds 482e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 483e21e8467SStephen Hemminger if (!idev) { 484e21e8467SStephen Hemminger idev = ipv6_add_dev(dev); 485a317a2f1SWANG Cong if (IS_ERR(idev)) 4861da177e4SLinus Torvalds return NULL; 4871da177e4SLinus Torvalds } 488c5e33bddSYOSHIFUJI Hideaki 4891da177e4SLinus Torvalds if (dev->flags&IFF_UP) 4901da177e4SLinus Torvalds ipv6_mc_up(idev); 4911da177e4SLinus Torvalds return idev; 4921da177e4SLinus Torvalds } 4931da177e4SLinus Torvalds 494f3a1bfb1SNicolas Dichtel static int inet6_netconf_msgsize_devconf(int type) 495f3a1bfb1SNicolas Dichtel { 496f3a1bfb1SNicolas Dichtel int size = NLMSG_ALIGN(sizeof(struct netconfmsg)) 497f3a1bfb1SNicolas Dichtel + nla_total_size(4); /* NETCONFA_IFINDEX */ 498136ba622SZhang Shengju bool all = false; 499f3a1bfb1SNicolas Dichtel 500136ba622SZhang Shengju if (type == NETCONFA_ALL) 501136ba622SZhang Shengju all = true; 502136ba622SZhang Shengju 503136ba622SZhang Shengju if (all || type == NETCONFA_FORWARDING) 504f3a1bfb1SNicolas Dichtel size += nla_total_size(4); 505b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE 506136ba622SZhang Shengju if (all || type == NETCONFA_MC_FORWARDING) 507d67b8c61SNicolas Dichtel size += nla_total_size(4); 508b1afce95SDavid S. Miller #endif 509136ba622SZhang Shengju if (all || type == NETCONFA_PROXY_NEIGH) 510c92d5491Sstephen hemminger size += nla_total_size(4); 511f3a1bfb1SNicolas Dichtel 512136ba622SZhang Shengju if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) 51335103d11SAndy Gospodarek size += nla_total_size(4); 51435103d11SAndy Gospodarek 515f3a1bfb1SNicolas Dichtel return size; 516f3a1bfb1SNicolas Dichtel } 517f3a1bfb1SNicolas Dichtel 518f3a1bfb1SNicolas Dichtel static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex, 519f3a1bfb1SNicolas Dichtel struct ipv6_devconf *devconf, u32 portid, 520f3a1bfb1SNicolas Dichtel u32 seq, int event, unsigned int flags, 521f3a1bfb1SNicolas Dichtel int type) 522f3a1bfb1SNicolas Dichtel { 523f3a1bfb1SNicolas Dichtel struct nlmsghdr *nlh; 524f3a1bfb1SNicolas Dichtel struct netconfmsg *ncm; 525136ba622SZhang Shengju bool all = false; 526f3a1bfb1SNicolas Dichtel 527f3a1bfb1SNicolas Dichtel nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg), 528f3a1bfb1SNicolas Dichtel flags); 52963159f29SIan Morris if (!nlh) 530f3a1bfb1SNicolas Dichtel return -EMSGSIZE; 531f3a1bfb1SNicolas Dichtel 532136ba622SZhang Shengju if (type == NETCONFA_ALL) 533136ba622SZhang Shengju all = true; 534136ba622SZhang Shengju 535f3a1bfb1SNicolas Dichtel ncm = nlmsg_data(nlh); 536f3a1bfb1SNicolas Dichtel ncm->ncm_family = AF_INET6; 537f3a1bfb1SNicolas Dichtel 538f3a1bfb1SNicolas Dichtel if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0) 539f3a1bfb1SNicolas Dichtel goto nla_put_failure; 540f3a1bfb1SNicolas Dichtel 54123452170SDavid Ahern if (!devconf) 54223452170SDavid Ahern goto out; 54323452170SDavid Ahern 544136ba622SZhang Shengju if ((all || type == NETCONFA_FORWARDING) && 545f3a1bfb1SNicolas Dichtel nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0) 546f3a1bfb1SNicolas Dichtel goto nla_put_failure; 547b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE 548136ba622SZhang Shengju if ((all || type == NETCONFA_MC_FORWARDING) && 549d67b8c61SNicolas Dichtel nla_put_s32(skb, NETCONFA_MC_FORWARDING, 550d67b8c61SNicolas Dichtel devconf->mc_forwarding) < 0) 551d67b8c61SNicolas Dichtel goto nla_put_failure; 552b1afce95SDavid S. Miller #endif 553136ba622SZhang Shengju if ((all || type == NETCONFA_PROXY_NEIGH) && 554c92d5491Sstephen hemminger nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0) 555c92d5491Sstephen hemminger goto nla_put_failure; 556c92d5491Sstephen hemminger 557136ba622SZhang Shengju if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) && 55835103d11SAndy Gospodarek nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 55935103d11SAndy Gospodarek devconf->ignore_routes_with_linkdown) < 0) 56035103d11SAndy Gospodarek goto nla_put_failure; 56135103d11SAndy Gospodarek 56223452170SDavid Ahern out: 563053c095aSJohannes Berg nlmsg_end(skb, nlh); 564053c095aSJohannes Berg return 0; 565f3a1bfb1SNicolas Dichtel 566f3a1bfb1SNicolas Dichtel nla_put_failure: 567f3a1bfb1SNicolas Dichtel nlmsg_cancel(skb, nlh); 568f3a1bfb1SNicolas Dichtel return -EMSGSIZE; 569f3a1bfb1SNicolas Dichtel } 570f3a1bfb1SNicolas Dichtel 57185b3daadSDavid Ahern void inet6_netconf_notify_devconf(struct net *net, int event, int type, 57285b3daadSDavid Ahern int ifindex, struct ipv6_devconf *devconf) 573f3a1bfb1SNicolas Dichtel { 574f3a1bfb1SNicolas Dichtel struct sk_buff *skb; 575f3a1bfb1SNicolas Dichtel int err = -ENOBUFS; 576f3a1bfb1SNicolas Dichtel 577927265bcSEric Dumazet skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_KERNEL); 57863159f29SIan Morris if (!skb) 579f3a1bfb1SNicolas Dichtel goto errout; 580f3a1bfb1SNicolas Dichtel 581f3a1bfb1SNicolas Dichtel err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0, 58285b3daadSDavid Ahern event, 0, type); 583f3a1bfb1SNicolas Dichtel if (err < 0) { 584f3a1bfb1SNicolas Dichtel /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */ 585f3a1bfb1SNicolas Dichtel WARN_ON(err == -EMSGSIZE); 586f3a1bfb1SNicolas Dichtel kfree_skb(skb); 587f3a1bfb1SNicolas Dichtel goto errout; 588f3a1bfb1SNicolas Dichtel } 589927265bcSEric Dumazet rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_KERNEL); 590f3a1bfb1SNicolas Dichtel return; 591f3a1bfb1SNicolas Dichtel errout: 592f3a1bfb1SNicolas Dichtel rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err); 593f3a1bfb1SNicolas Dichtel } 594f3a1bfb1SNicolas Dichtel 59576f8f6cbSNicolas Dichtel static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = { 59676f8f6cbSNicolas Dichtel [NETCONFA_IFINDEX] = { .len = sizeof(int) }, 59776f8f6cbSNicolas Dichtel [NETCONFA_FORWARDING] = { .len = sizeof(int) }, 598c92d5491Sstephen hemminger [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) }, 59935103d11SAndy Gospodarek [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) }, 60076f8f6cbSNicolas Dichtel }; 60176f8f6cbSNicolas Dichtel 60276f8f6cbSNicolas Dichtel static int inet6_netconf_get_devconf(struct sk_buff *in_skb, 603c21ef3e3SDavid Ahern struct nlmsghdr *nlh, 604c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 60576f8f6cbSNicolas Dichtel { 60676f8f6cbSNicolas Dichtel struct net *net = sock_net(in_skb->sk); 60776f8f6cbSNicolas Dichtel struct nlattr *tb[NETCONFA_MAX+1]; 6084ea2607fSFlorian Westphal struct inet6_dev *in6_dev = NULL; 6094ea2607fSFlorian Westphal struct net_device *dev = NULL; 61076f8f6cbSNicolas Dichtel struct netconfmsg *ncm; 61176f8f6cbSNicolas Dichtel struct sk_buff *skb; 61276f8f6cbSNicolas Dichtel struct ipv6_devconf *devconf; 61376f8f6cbSNicolas Dichtel int ifindex; 61476f8f6cbSNicolas Dichtel int err; 61576f8f6cbSNicolas Dichtel 61676f8f6cbSNicolas Dichtel err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX, 617c21ef3e3SDavid Ahern devconf_ipv6_policy, extack); 61876f8f6cbSNicolas Dichtel if (err < 0) 6194ea2607fSFlorian Westphal return err; 6204ea2607fSFlorian Westphal 6214ea2607fSFlorian Westphal if (!tb[NETCONFA_IFINDEX]) 6224ea2607fSFlorian Westphal return -EINVAL; 62376f8f6cbSNicolas Dichtel 624a97eb33fSAnton Protopopov err = -EINVAL; 62576f8f6cbSNicolas Dichtel ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]); 62676f8f6cbSNicolas Dichtel switch (ifindex) { 62776f8f6cbSNicolas Dichtel case NETCONFA_IFINDEX_ALL: 62876f8f6cbSNicolas Dichtel devconf = net->ipv6.devconf_all; 62976f8f6cbSNicolas Dichtel break; 63076f8f6cbSNicolas Dichtel case NETCONFA_IFINDEX_DEFAULT: 63176f8f6cbSNicolas Dichtel devconf = net->ipv6.devconf_dflt; 63276f8f6cbSNicolas Dichtel break; 63376f8f6cbSNicolas Dichtel default: 6344ea2607fSFlorian Westphal dev = dev_get_by_index(net, ifindex); 63563159f29SIan Morris if (!dev) 6364ea2607fSFlorian Westphal return -EINVAL; 6374ea2607fSFlorian Westphal in6_dev = in6_dev_get(dev); 63863159f29SIan Morris if (!in6_dev) 63976f8f6cbSNicolas Dichtel goto errout; 64076f8f6cbSNicolas Dichtel devconf = &in6_dev->cnf; 64176f8f6cbSNicolas Dichtel break; 64276f8f6cbSNicolas Dichtel } 64376f8f6cbSNicolas Dichtel 64476f8f6cbSNicolas Dichtel err = -ENOBUFS; 6454ea2607fSFlorian Westphal skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL); 64663159f29SIan Morris if (!skb) 64776f8f6cbSNicolas Dichtel goto errout; 64876f8f6cbSNicolas Dichtel 64976f8f6cbSNicolas Dichtel err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 65076f8f6cbSNicolas Dichtel NETLINK_CB(in_skb).portid, 65176f8f6cbSNicolas Dichtel nlh->nlmsg_seq, RTM_NEWNETCONF, 0, 652136ba622SZhang Shengju NETCONFA_ALL); 65376f8f6cbSNicolas Dichtel if (err < 0) { 65476f8f6cbSNicolas Dichtel /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */ 65576f8f6cbSNicolas Dichtel WARN_ON(err == -EMSGSIZE); 65676f8f6cbSNicolas Dichtel kfree_skb(skb); 65776f8f6cbSNicolas Dichtel goto errout; 65876f8f6cbSNicolas Dichtel } 65976f8f6cbSNicolas Dichtel err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 66076f8f6cbSNicolas Dichtel errout: 6614ea2607fSFlorian Westphal if (in6_dev) 6624ea2607fSFlorian Westphal in6_dev_put(in6_dev); 6634ea2607fSFlorian Westphal if (dev) 6644ea2607fSFlorian Westphal dev_put(dev); 66576f8f6cbSNicolas Dichtel return err; 66676f8f6cbSNicolas Dichtel } 66776f8f6cbSNicolas Dichtel 6687a674200SNicolas Dichtel static int inet6_netconf_dump_devconf(struct sk_buff *skb, 6697a674200SNicolas Dichtel struct netlink_callback *cb) 6707a674200SNicolas Dichtel { 6717a674200SNicolas Dichtel struct net *net = sock_net(skb->sk); 6727a674200SNicolas Dichtel int h, s_h; 6737a674200SNicolas Dichtel int idx, s_idx; 6747a674200SNicolas Dichtel struct net_device *dev; 6757a674200SNicolas Dichtel struct inet6_dev *idev; 6767a674200SNicolas Dichtel struct hlist_head *head; 6777a674200SNicolas Dichtel 6787a674200SNicolas Dichtel s_h = cb->args[0]; 6797a674200SNicolas Dichtel s_idx = idx = cb->args[1]; 6807a674200SNicolas Dichtel 6817a674200SNicolas Dichtel for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 6827a674200SNicolas Dichtel idx = 0; 6837a674200SNicolas Dichtel head = &net->dev_index_head[h]; 6847a674200SNicolas Dichtel rcu_read_lock(); 68563998ac2SNicolas Dichtel cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ 68663998ac2SNicolas Dichtel net->dev_base_seq; 6877a674200SNicolas Dichtel hlist_for_each_entry_rcu(dev, head, index_hlist) { 6887a674200SNicolas Dichtel if (idx < s_idx) 6897a674200SNicolas Dichtel goto cont; 6907a674200SNicolas Dichtel idev = __in6_dev_get(dev); 6917a674200SNicolas Dichtel if (!idev) 6927a674200SNicolas Dichtel goto cont; 6937a674200SNicolas Dichtel 6947a674200SNicolas Dichtel if (inet6_netconf_fill_devconf(skb, dev->ifindex, 6957a674200SNicolas Dichtel &idev->cnf, 6967a674200SNicolas Dichtel NETLINK_CB(cb->skb).portid, 6977a674200SNicolas Dichtel cb->nlh->nlmsg_seq, 6987a674200SNicolas Dichtel RTM_NEWNETCONF, 6997a674200SNicolas Dichtel NLM_F_MULTI, 700136ba622SZhang Shengju NETCONFA_ALL) < 0) { 7017a674200SNicolas Dichtel rcu_read_unlock(); 7027a674200SNicolas Dichtel goto done; 7037a674200SNicolas Dichtel } 70463998ac2SNicolas Dichtel nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 7057a674200SNicolas Dichtel cont: 7067a674200SNicolas Dichtel idx++; 7077a674200SNicolas Dichtel } 7087a674200SNicolas Dichtel rcu_read_unlock(); 7097a674200SNicolas Dichtel } 7107a674200SNicolas Dichtel if (h == NETDEV_HASHENTRIES) { 7117a674200SNicolas Dichtel if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL, 7127a674200SNicolas Dichtel net->ipv6.devconf_all, 7137a674200SNicolas Dichtel NETLINK_CB(cb->skb).portid, 7147a674200SNicolas Dichtel cb->nlh->nlmsg_seq, 7157a674200SNicolas Dichtel RTM_NEWNETCONF, NLM_F_MULTI, 716136ba622SZhang Shengju NETCONFA_ALL) < 0) 7177a674200SNicolas Dichtel goto done; 7187a674200SNicolas Dichtel else 7197a674200SNicolas Dichtel h++; 7207a674200SNicolas Dichtel } 7217a674200SNicolas Dichtel if (h == NETDEV_HASHENTRIES + 1) { 7227a674200SNicolas Dichtel if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT, 7237a674200SNicolas Dichtel net->ipv6.devconf_dflt, 7247a674200SNicolas Dichtel NETLINK_CB(cb->skb).portid, 7257a674200SNicolas Dichtel cb->nlh->nlmsg_seq, 7267a674200SNicolas Dichtel RTM_NEWNETCONF, NLM_F_MULTI, 727136ba622SZhang Shengju NETCONFA_ALL) < 0) 7287a674200SNicolas Dichtel goto done; 7297a674200SNicolas Dichtel else 7307a674200SNicolas Dichtel h++; 7317a674200SNicolas Dichtel } 7327a674200SNicolas Dichtel done: 7337a674200SNicolas Dichtel cb->args[0] = h; 7347a674200SNicolas Dichtel cb->args[1] = idx; 7357a674200SNicolas Dichtel 7367a674200SNicolas Dichtel return skb->len; 7377a674200SNicolas Dichtel } 7387a674200SNicolas Dichtel 7391da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 7401da177e4SLinus Torvalds static void dev_forward_change(struct inet6_dev *idev) 7411da177e4SLinus Torvalds { 7421da177e4SLinus Torvalds struct net_device *dev; 7431da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 7441da177e4SLinus Torvalds 7451da177e4SLinus Torvalds if (!idev) 7461da177e4SLinus Torvalds return; 7471da177e4SLinus Torvalds dev = idev->dev; 7480187bdfbSBen Hutchings if (idev->cnf.forwarding) 7490187bdfbSBen Hutchings dev_disable_lro(dev); 7501a940835SAmerigo Wang if (dev->flags & IFF_MULTICAST) { 7512c5e8933SHannes Frederic Sowa if (idev->cnf.forwarding) { 752f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters); 7532c5e8933SHannes Frederic Sowa ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters); 7542c5e8933SHannes Frederic Sowa ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters); 7552c5e8933SHannes Frederic Sowa } else { 756f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters); 7572c5e8933SHannes Frederic Sowa ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters); 7582c5e8933SHannes Frederic Sowa ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters); 7592c5e8933SHannes Frederic Sowa } 7601da177e4SLinus Torvalds } 761502a2ffdSstephen hemminger 762502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 7632c12a74cSMichal Wrobel if (ifa->flags&IFA_F_TENTATIVE) 7642c12a74cSMichal Wrobel continue; 7651da177e4SLinus Torvalds if (idev->cnf.forwarding) 7661da177e4SLinus Torvalds addrconf_join_anycast(ifa); 7671da177e4SLinus Torvalds else 7681da177e4SLinus Torvalds addrconf_leave_anycast(ifa); 7691da177e4SLinus Torvalds } 77085b3daadSDavid Ahern inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF, 77185b3daadSDavid Ahern NETCONFA_FORWARDING, 772f3a1bfb1SNicolas Dichtel dev->ifindex, &idev->cnf); 7731da177e4SLinus Torvalds } 7741da177e4SLinus Torvalds 7751da177e4SLinus Torvalds 776e186932bSPavel Emelyanov static void addrconf_forward_change(struct net *net, __s32 newf) 7771da177e4SLinus Torvalds { 7781da177e4SLinus Torvalds struct net_device *dev; 7791da177e4SLinus Torvalds struct inet6_dev *idev; 7801da177e4SLinus Torvalds 7814acd4945SBen Hutchings for_each_netdev(net, dev) { 7821da177e4SLinus Torvalds idev = __in6_dev_get(dev); 7831da177e4SLinus Torvalds if (idev) { 784e186932bSPavel Emelyanov int changed = (!idev->cnf.forwarding) ^ (!newf); 785e186932bSPavel Emelyanov idev->cnf.forwarding = newf; 7861da177e4SLinus Torvalds if (changed) 7871da177e4SLinus Torvalds dev_forward_change(idev); 7881da177e4SLinus Torvalds } 7891da177e4SLinus Torvalds } 7901da177e4SLinus Torvalds } 791c8fecf22SPavel Emelyanov 792013d97e9SFrancesco Ruggeri static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf) 793c8fecf22SPavel Emelyanov { 794bff16c2fSPavel Emelyanov struct net *net; 795013d97e9SFrancesco Ruggeri int old; 796013d97e9SFrancesco Ruggeri 797013d97e9SFrancesco Ruggeri if (!rtnl_trylock()) 798013d97e9SFrancesco Ruggeri return restart_syscall(); 799bff16c2fSPavel Emelyanov 800bff16c2fSPavel Emelyanov net = (struct net *)table->extra2; 801013d97e9SFrancesco Ruggeri old = *p; 802013d97e9SFrancesco Ruggeri *p = newf; 803c8fecf22SPavel Emelyanov 804013d97e9SFrancesco Ruggeri if (p == &net->ipv6.devconf_dflt->forwarding) { 805f3a1bfb1SNicolas Dichtel if ((!newf) ^ (!old)) 80685b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 80785b3daadSDavid Ahern NETCONFA_FORWARDING, 808f3a1bfb1SNicolas Dichtel NETCONFA_IFINDEX_DEFAULT, 809f3a1bfb1SNicolas Dichtel net->ipv6.devconf_dflt); 810013d97e9SFrancesco Ruggeri rtnl_unlock(); 811013d97e9SFrancesco Ruggeri return 0; 81288af182eSEric W. Biederman } 813b325fddbSStephen Hemminger 814e186932bSPavel Emelyanov if (p == &net->ipv6.devconf_all->forwarding) { 815d26c638cSNicolas Dichtel int old_dflt = net->ipv6.devconf_dflt->forwarding; 816d26c638cSNicolas Dichtel 817e186932bSPavel Emelyanov net->ipv6.devconf_dflt->forwarding = newf; 818d26c638cSNicolas Dichtel if ((!newf) ^ (!old_dflt)) 81985b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 82085b3daadSDavid Ahern NETCONFA_FORWARDING, 821d26c638cSNicolas Dichtel NETCONFA_IFINDEX_DEFAULT, 822d26c638cSNicolas Dichtel net->ipv6.devconf_dflt); 823d26c638cSNicolas Dichtel 824e186932bSPavel Emelyanov addrconf_forward_change(net, newf); 825f3a1bfb1SNicolas Dichtel if ((!newf) ^ (!old)) 82685b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 82785b3daadSDavid Ahern NETCONFA_FORWARDING, 828f3a1bfb1SNicolas Dichtel NETCONFA_IFINDEX_ALL, 829f3a1bfb1SNicolas Dichtel net->ipv6.devconf_all); 830013d97e9SFrancesco Ruggeri } else if ((!newf) ^ (!old)) 831c8fecf22SPavel Emelyanov dev_forward_change((struct inet6_dev *)table->extra1); 8320187bdfbSBen Hutchings rtnl_unlock(); 833c8fecf22SPavel Emelyanov 834013d97e9SFrancesco Ruggeri if (newf) 8357b4da532SDaniel Lezcano rt6_purge_dflt_routers(net); 836b325fddbSStephen Hemminger return 1; 837c8fecf22SPavel Emelyanov } 83835103d11SAndy Gospodarek 83935103d11SAndy Gospodarek static void addrconf_linkdown_change(struct net *net, __s32 newf) 84035103d11SAndy Gospodarek { 84135103d11SAndy Gospodarek struct net_device *dev; 84235103d11SAndy Gospodarek struct inet6_dev *idev; 84335103d11SAndy Gospodarek 84435103d11SAndy Gospodarek for_each_netdev(net, dev) { 84535103d11SAndy Gospodarek idev = __in6_dev_get(dev); 84635103d11SAndy Gospodarek if (idev) { 84735103d11SAndy Gospodarek int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf); 84835103d11SAndy Gospodarek 84935103d11SAndy Gospodarek idev->cnf.ignore_routes_with_linkdown = newf; 85035103d11SAndy Gospodarek if (changed) 85135103d11SAndy Gospodarek inet6_netconf_notify_devconf(dev_net(dev), 85285b3daadSDavid Ahern RTM_NEWNETCONF, 85335103d11SAndy Gospodarek NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 85435103d11SAndy Gospodarek dev->ifindex, 85535103d11SAndy Gospodarek &idev->cnf); 85635103d11SAndy Gospodarek } 85735103d11SAndy Gospodarek } 85835103d11SAndy Gospodarek } 85935103d11SAndy Gospodarek 86035103d11SAndy Gospodarek static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf) 86135103d11SAndy Gospodarek { 86235103d11SAndy Gospodarek struct net *net; 86335103d11SAndy Gospodarek int old; 86435103d11SAndy Gospodarek 86535103d11SAndy Gospodarek if (!rtnl_trylock()) 86635103d11SAndy Gospodarek return restart_syscall(); 86735103d11SAndy Gospodarek 86835103d11SAndy Gospodarek net = (struct net *)table->extra2; 86935103d11SAndy Gospodarek old = *p; 87035103d11SAndy Gospodarek *p = newf; 87135103d11SAndy Gospodarek 87235103d11SAndy Gospodarek if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) { 87335103d11SAndy Gospodarek if ((!newf) ^ (!old)) 87435103d11SAndy Gospodarek inet6_netconf_notify_devconf(net, 87585b3daadSDavid Ahern RTM_NEWNETCONF, 87635103d11SAndy Gospodarek NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 87735103d11SAndy Gospodarek NETCONFA_IFINDEX_DEFAULT, 87835103d11SAndy Gospodarek net->ipv6.devconf_dflt); 87935103d11SAndy Gospodarek rtnl_unlock(); 88035103d11SAndy Gospodarek return 0; 88135103d11SAndy Gospodarek } 88235103d11SAndy Gospodarek 88335103d11SAndy Gospodarek if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) { 88435103d11SAndy Gospodarek net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf; 88535103d11SAndy Gospodarek addrconf_linkdown_change(net, newf); 88635103d11SAndy Gospodarek if ((!newf) ^ (!old)) 88735103d11SAndy Gospodarek inet6_netconf_notify_devconf(net, 88885b3daadSDavid Ahern RTM_NEWNETCONF, 88935103d11SAndy Gospodarek NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 89035103d11SAndy Gospodarek NETCONFA_IFINDEX_ALL, 89135103d11SAndy Gospodarek net->ipv6.devconf_all); 89235103d11SAndy Gospodarek } 89335103d11SAndy Gospodarek rtnl_unlock(); 89435103d11SAndy Gospodarek 89535103d11SAndy Gospodarek return 1; 89635103d11SAndy Gospodarek } 89735103d11SAndy Gospodarek 8981da177e4SLinus Torvalds #endif 8991da177e4SLinus Torvalds 9005c578aedSstephen hemminger /* Nobody refers to this ifaddr, destroy it */ 9011da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp) 9021da177e4SLinus Torvalds { 903c2e21293Sstephen hemminger WARN_ON(!hlist_unhashed(&ifp->addr_lst)); 904547b792cSIlpo Järvinen 9051da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG 90691df42beSJoe Perches pr_debug("%s\n", __func__); 9071da177e4SLinus Torvalds #endif 9081da177e4SLinus Torvalds 9091da177e4SLinus Torvalds in6_dev_put(ifp->idev); 9101da177e4SLinus Torvalds 911c15b1ccaSHannes Frederic Sowa if (cancel_delayed_work(&ifp->dad_work)) 912c15b1ccaSHannes Frederic Sowa pr_notice("delayed DAD work was pending while freeing ifa=%p\n", 913c15b1ccaSHannes Frederic Sowa ifp); 9141da177e4SLinus Torvalds 915e9d3e084SHerbert Xu if (ifp->state != INET6_IFADDR_STATE_DEAD) { 916f3213831SJoe Perches pr_warn("Freeing alive inet6 address %p\n", ifp); 9171da177e4SLinus Torvalds return; 9181da177e4SLinus Torvalds } 9191da177e4SLinus Torvalds 920e5785985SLai Jiangshan kfree_rcu(ifp, rcu); 9211da177e4SLinus Torvalds } 9221da177e4SLinus Torvalds 923e55ffac6SBrian Haley static void 924e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp) 925e55ffac6SBrian Haley { 926502a2ffdSstephen hemminger struct list_head *p; 9278a6ce0c0SYOSHIFUJI Hideaki int ifp_scope = ipv6_addr_src_scope(&ifp->addr); 928e55ffac6SBrian Haley 929e55ffac6SBrian Haley /* 930e55ffac6SBrian Haley * Each device address list is sorted in order of scope - 931e55ffac6SBrian Haley * global before linklocal. 932e55ffac6SBrian Haley */ 933502a2ffdSstephen hemminger list_for_each(p, &idev->addr_list) { 934502a2ffdSstephen hemminger struct inet6_ifaddr *ifa 935502a2ffdSstephen hemminger = list_entry(p, struct inet6_ifaddr, if_list); 9368a6ce0c0SYOSHIFUJI Hideaki if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr)) 937e55ffac6SBrian Haley break; 938e55ffac6SBrian Haley } 939e55ffac6SBrian Haley 9408ef802aaSEric Dumazet list_add_tail_rcu(&ifp->if_list, p); 941e55ffac6SBrian Haley } 942e55ffac6SBrian Haley 9433f27fb23SEric Dumazet static u32 inet6_addr_hash(const struct net *net, const struct in6_addr *addr) 9443eb84f49SYOSHIFUJI Hideaki { 9453f27fb23SEric Dumazet u32 val = ipv6_addr_hash(addr) ^ net_hash_mix(net); 9463f27fb23SEric Dumazet 9473f27fb23SEric Dumazet return hash_32(val, IN6_ADDR_HSIZE_SHIFT); 9483eb84f49SYOSHIFUJI Hideaki } 9493eb84f49SYOSHIFUJI Hideaki 95056fc709bSEric Dumazet static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr, 951752a9292SEric Dumazet struct net_device *dev, unsigned int hash) 95256fc709bSEric Dumazet { 95356fc709bSEric Dumazet struct inet6_ifaddr *ifp; 95456fc709bSEric Dumazet 95556fc709bSEric Dumazet hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) { 95656fc709bSEric Dumazet if (!net_eq(dev_net(ifp->idev->dev), net)) 95756fc709bSEric Dumazet continue; 95856fc709bSEric Dumazet if (ipv6_addr_equal(&ifp->addr, addr)) { 95956fc709bSEric Dumazet if (!dev || ifp->idev->dev == dev) 96056fc709bSEric Dumazet return true; 96156fc709bSEric Dumazet } 96256fc709bSEric Dumazet } 96356fc709bSEric Dumazet return false; 96456fc709bSEric Dumazet } 96556fc709bSEric Dumazet 966f3d9832eSDavid Ahern static int ipv6_add_addr_hash(struct net_device *dev, struct inet6_ifaddr *ifa) 967f3d9832eSDavid Ahern { 9683f27fb23SEric Dumazet unsigned int hash = inet6_addr_hash(dev_net(dev), &ifa->addr); 969f3d9832eSDavid Ahern int err = 0; 970f3d9832eSDavid Ahern 971f3d9832eSDavid Ahern spin_lock(&addrconf_hash_lock); 972f3d9832eSDavid Ahern 973f3d9832eSDavid Ahern /* Ignore adding duplicate addresses on an interface */ 974752a9292SEric Dumazet if (ipv6_chk_same_addr(dev_net(dev), &ifa->addr, dev, hash)) { 975e32ac250SJoe Perches netdev_dbg(dev, "ipv6_add_addr: already assigned\n"); 976f3d9832eSDavid Ahern err = -EEXIST; 977752a9292SEric Dumazet } else { 978752a9292SEric Dumazet hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]); 979f3d9832eSDavid Ahern } 980f3d9832eSDavid Ahern 981f3d9832eSDavid Ahern spin_unlock(&addrconf_hash_lock); 982f3d9832eSDavid Ahern 983f3d9832eSDavid Ahern return err; 984f3d9832eSDavid Ahern } 985f3d9832eSDavid Ahern 9861da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */ 9871da177e4SLinus Torvalds 9881da177e4SLinus Torvalds static struct inet6_ifaddr * 989e6464b8cSDavid Ahern ipv6_add_addr(struct inet6_dev *idev, struct ifa6_config *cfg, 990de95e047SDavid Ahern bool can_block, struct netlink_ext_ack *extack) 9911da177e4SLinus Torvalds { 992f3d9832eSDavid Ahern gfp_t gfp_flags = can_block ? GFP_KERNEL : GFP_ATOMIC; 993e6464b8cSDavid Ahern int addr_type = ipv6_addr_type(cfg->pfx); 994df789fe7SDavid Forster struct net *net = dev_net(idev->dev); 9951da177e4SLinus Torvalds struct inet6_ifaddr *ifa = NULL; 996360a9887SDavid Ahern struct fib6_info *f6i = NULL; 9971da177e4SLinus Torvalds int err = 0; 998d68b8270SYOSHIFUJI Hideaki 999d68b8270SYOSHIFUJI Hideaki if (addr_type == IPV6_ADDR_ANY || 1000d68b8270SYOSHIFUJI Hideaki addr_type & IPV6_ADDR_MULTICAST || 1001d68b8270SYOSHIFUJI Hideaki (!(idev->dev->flags & IFF_LOOPBACK) && 1002d68b8270SYOSHIFUJI Hideaki addr_type & IPV6_ADDR_LOOPBACK)) 1003d68b8270SYOSHIFUJI Hideaki return ERR_PTR(-EADDRNOTAVAIL); 10041da177e4SLinus Torvalds 10051da177e4SLinus Torvalds if (idev->dead) { 10061da177e4SLinus Torvalds err = -ENODEV; /*XXX*/ 1007f3d9832eSDavid Ahern goto out; 10081da177e4SLinus Torvalds } 10091da177e4SLinus Torvalds 101056d417b1SBrian Haley if (idev->cnf.disable_ipv6) { 10119bdd8d40SBrian Haley err = -EACCES; 1012f3d9832eSDavid Ahern goto out; 10139bdd8d40SBrian Haley } 10149bdd8d40SBrian Haley 1015ff7883eaSDavid Ahern /* validator notifier needs to be blocking; 1016ff7883eaSDavid Ahern * do not call in atomic context 1017ff7883eaSDavid Ahern */ 1018ff7883eaSDavid Ahern if (can_block) { 1019ff7883eaSDavid Ahern struct in6_validator_info i6vi = { 1020e6464b8cSDavid Ahern .i6vi_addr = *cfg->pfx, 1021ff7883eaSDavid Ahern .i6vi_dev = idev, 1022de95e047SDavid Ahern .extack = extack, 1023ff7883eaSDavid Ahern }; 1024ff7883eaSDavid Ahern 10253ad7d246SKrister Johansen err = inet6addr_validator_notifier_call_chain(NETDEV_UP, &i6vi); 10263ad7d246SKrister Johansen err = notifier_to_errno(err); 1027f3d9832eSDavid Ahern if (err < 0) 10281da177e4SLinus Torvalds goto out; 1029ff7883eaSDavid Ahern } 10301da177e4SLinus Torvalds 1031f3d9832eSDavid Ahern ifa = kzalloc(sizeof(*ifa), gfp_flags); 103263159f29SIan Morris if (!ifa) { 10331da177e4SLinus Torvalds err = -ENOBUFS; 10341da177e4SLinus Torvalds goto out; 10351da177e4SLinus Torvalds } 10361da177e4SLinus Torvalds 1037e6464b8cSDavid Ahern f6i = addrconf_f6i_alloc(net, idev, cfg->pfx, false, gfp_flags); 1038360a9887SDavid Ahern if (IS_ERR(f6i)) { 1039360a9887SDavid Ahern err = PTR_ERR(f6i); 1040360a9887SDavid Ahern f6i = NULL; 10411da177e4SLinus Torvalds goto out; 10421da177e4SLinus Torvalds } 10431da177e4SLinus Torvalds 1044df789fe7SDavid Forster if (net->ipv6.devconf_all->disable_policy || 1045df789fe7SDavid Forster idev->cnf.disable_policy) 1046360a9887SDavid Ahern f6i->dst_nopolicy = true; 1047df789fe7SDavid Forster 1048bba24896SJiri Pirko neigh_parms_data_state_setall(idev->nd_parms); 1049bba24896SJiri Pirko 1050e6464b8cSDavid Ahern ifa->addr = *cfg->pfx; 1051e6464b8cSDavid Ahern if (cfg->peer_pfx) 1052e6464b8cSDavid Ahern ifa->peer_addr = *cfg->peer_pfx; 10531da177e4SLinus Torvalds 10541da177e4SLinus Torvalds spin_lock_init(&ifa->lock); 1055c15b1ccaSHannes Frederic Sowa INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work); 1056c2e21293Sstephen hemminger INIT_HLIST_NODE(&ifa->addr_lst); 1057e6464b8cSDavid Ahern ifa->scope = cfg->scope; 1058e6464b8cSDavid Ahern ifa->prefix_len = cfg->plen; 1059e6464b8cSDavid Ahern ifa->flags = cfg->ifa_flags; 106066eb9f86SMahesh Bandewar /* No need to add the TENTATIVE flag for addresses with NODAD */ 1061e6464b8cSDavid Ahern if (!(cfg->ifa_flags & IFA_F_NODAD)) 106266eb9f86SMahesh Bandewar ifa->flags |= IFA_F_TENTATIVE; 1063e6464b8cSDavid Ahern ifa->valid_lft = cfg->valid_lft; 1064e6464b8cSDavid Ahern ifa->prefered_lft = cfg->preferred_lft; 10651da177e4SLinus Torvalds ifa->cstamp = ifa->tstamp = jiffies; 1066617fe29dSDaniel Borkmann ifa->tokenized = false; 10671da177e4SLinus Torvalds 1068360a9887SDavid Ahern ifa->rt = f6i; 106957f5f544SKeir Fraser 10701da177e4SLinus Torvalds ifa->idev = idev; 1071f3d9832eSDavid Ahern in6_dev_hold(idev); 1072f3d9832eSDavid Ahern 10731da177e4SLinus Torvalds /* For caller */ 1074271201c0SReshetova, Elena refcount_set(&ifa->refcnt, 1); 10751da177e4SLinus Torvalds 1076f3d9832eSDavid Ahern rcu_read_lock_bh(); 10771da177e4SLinus Torvalds 1078f3d9832eSDavid Ahern err = ipv6_add_addr_hash(idev->dev, ifa); 1079f3d9832eSDavid Ahern if (err < 0) { 1080f3d9832eSDavid Ahern rcu_read_unlock_bh(); 1081f3d9832eSDavid Ahern goto out; 1082f3d9832eSDavid Ahern } 10831da177e4SLinus Torvalds 10841da177e4SLinus Torvalds write_lock(&idev->lock); 1085f3d9832eSDavid Ahern 10861da177e4SLinus Torvalds /* Add to inet6_dev unicast addr list. */ 1087e55ffac6SBrian Haley ipv6_link_dev_addr(idev, ifa); 10881da177e4SLinus Torvalds 10891da177e4SLinus Torvalds if (ifa->flags&IFA_F_TEMPORARY) { 1090372e6c8fSstephen hemminger list_add(&ifa->tmp_list, &idev->tempaddr_list); 10911da177e4SLinus Torvalds in6_ifa_hold(ifa); 10921da177e4SLinus Torvalds } 10931da177e4SLinus Torvalds 10941da177e4SLinus Torvalds in6_ifa_hold(ifa); 10951da177e4SLinus Torvalds write_unlock(&idev->lock); 1096f3d9832eSDavid Ahern 10978814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 10981da177e4SLinus Torvalds 1099f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_UP, ifa); 1100f3d9832eSDavid Ahern out: 1101f3d9832eSDavid Ahern if (unlikely(err < 0)) { 1102360a9887SDavid Ahern fib6_info_release(f6i); 110393531c67SDavid Ahern 1104f3d9832eSDavid Ahern if (ifa) { 1105f3d9832eSDavid Ahern if (ifa->idev) 1106f3d9832eSDavid Ahern in6_dev_put(ifa->idev); 11071da177e4SLinus Torvalds kfree(ifa); 1108f3d9832eSDavid Ahern } 11091da177e4SLinus Torvalds ifa = ERR_PTR(err); 11101da177e4SLinus Torvalds } 11111da177e4SLinus Torvalds 11121da177e4SLinus Torvalds return ifa; 11131da177e4SLinus Torvalds } 11141da177e4SLinus Torvalds 11155b84efecSThomas Haller enum cleanup_prefix_rt_t { 11165b84efecSThomas Haller CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */ 11175b84efecSThomas Haller CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */ 11185b84efecSThomas Haller CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */ 11195b84efecSThomas Haller }; 11205b84efecSThomas Haller 11215b84efecSThomas Haller /* 11225b84efecSThomas Haller * Check, whether the prefix for ifp would still need a prefix route 11235b84efecSThomas Haller * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_* 11245b84efecSThomas Haller * constants. 11255b84efecSThomas Haller * 11265b84efecSThomas Haller * 1) we don't purge prefix if address was not permanent. 11275b84efecSThomas Haller * prefix is managed by its own lifetime. 11285b84efecSThomas Haller * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE. 11295b84efecSThomas Haller * 3) if there are no addresses, delete prefix. 11305b84efecSThomas Haller * 4) if there are still other permanent address(es), 11315b84efecSThomas Haller * corresponding prefix is still permanent. 11325b84efecSThomas Haller * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE, 11335b84efecSThomas Haller * don't purge the prefix, assume user space is managing it. 11345b84efecSThomas Haller * 6) otherwise, update prefix lifetime to the 11355b84efecSThomas Haller * longest valid lifetime among the corresponding 11365b84efecSThomas Haller * addresses on the device. 11375b84efecSThomas Haller * Note: subsequent RA will update lifetime. 11385b84efecSThomas Haller **/ 11395b84efecSThomas Haller static enum cleanup_prefix_rt_t 11405b84efecSThomas Haller check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires) 11415b84efecSThomas Haller { 11425b84efecSThomas Haller struct inet6_ifaddr *ifa; 11435b84efecSThomas Haller struct inet6_dev *idev = ifp->idev; 11445b84efecSThomas Haller unsigned long lifetime; 11455b84efecSThomas Haller enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL; 11465b84efecSThomas Haller 11475b84efecSThomas Haller *expires = jiffies; 11485b84efecSThomas Haller 11495b84efecSThomas Haller list_for_each_entry(ifa, &idev->addr_list, if_list) { 11505b84efecSThomas Haller if (ifa == ifp) 11515b84efecSThomas Haller continue; 11525b84efecSThomas Haller if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr, 11535b84efecSThomas Haller ifp->prefix_len)) 11545b84efecSThomas Haller continue; 11555b84efecSThomas Haller if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE)) 11565b84efecSThomas Haller return CLEANUP_PREFIX_RT_NOP; 11575b84efecSThomas Haller 11585b84efecSThomas Haller action = CLEANUP_PREFIX_RT_EXPIRE; 11595b84efecSThomas Haller 11605b84efecSThomas Haller spin_lock(&ifa->lock); 11615b84efecSThomas Haller 11625b84efecSThomas Haller lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ); 11635b84efecSThomas Haller /* 11645b84efecSThomas Haller * Note: Because this address is 11655b84efecSThomas Haller * not permanent, lifetime < 11665b84efecSThomas Haller * LONG_MAX / HZ here. 11675b84efecSThomas Haller */ 11685b84efecSThomas Haller if (time_before(*expires, ifa->tstamp + lifetime * HZ)) 11695b84efecSThomas Haller *expires = ifa->tstamp + lifetime * HZ; 11705b84efecSThomas Haller spin_unlock(&ifa->lock); 11715b84efecSThomas Haller } 11725b84efecSThomas Haller 11735b84efecSThomas Haller return action; 11745b84efecSThomas Haller } 11755b84efecSThomas Haller 11765b84efecSThomas Haller static void 11775b84efecSThomas Haller cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt) 11785b84efecSThomas Haller { 117993c2fb25SDavid Ahern struct fib6_info *f6i; 11805b84efecSThomas Haller 118193c2fb25SDavid Ahern f6i = addrconf_get_prefix_route(&ifp->addr, 11825b84efecSThomas Haller ifp->prefix_len, 11835b84efecSThomas Haller ifp->idev->dev, 11845b84efecSThomas Haller 0, RTF_GATEWAY | RTF_DEFAULT); 118593c2fb25SDavid Ahern if (f6i) { 11865b84efecSThomas Haller if (del_rt) 118793c2fb25SDavid Ahern ip6_del_rt(dev_net(ifp->idev->dev), f6i); 11885b84efecSThomas Haller else { 118993c2fb25SDavid Ahern if (!(f6i->fib6_flags & RTF_EXPIRES)) 119093c2fb25SDavid Ahern fib6_set_expires(f6i, expires); 119193c2fb25SDavid Ahern fib6_info_release(f6i); 11925b84efecSThomas Haller } 11935b84efecSThomas Haller } 11945b84efecSThomas Haller } 11955b84efecSThomas Haller 11965b84efecSThomas Haller 11971da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */ 11981da177e4SLinus Torvalds 11991da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp) 12001da177e4SLinus Torvalds { 12014c5ff6a6SHerbert Xu int state; 12025b84efecSThomas Haller enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP; 12035b84efecSThomas Haller unsigned long expires; 12041da177e4SLinus Torvalds 1205c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 1206c15b1ccaSHannes Frederic Sowa 12078e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 12084c5ff6a6SHerbert Xu state = ifp->state; 1209e9d3e084SHerbert Xu ifp->state = INET6_IFADDR_STATE_DEAD; 12108e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 12114c5ff6a6SHerbert Xu 12124c5ff6a6SHerbert Xu if (state == INET6_IFADDR_STATE_DEAD) 12134c5ff6a6SHerbert Xu goto out; 12141da177e4SLinus Torvalds 12155c578aedSstephen hemminger spin_lock_bh(&addrconf_hash_lock); 12165c578aedSstephen hemminger hlist_del_init_rcu(&ifp->addr_lst); 12175c578aedSstephen hemminger spin_unlock_bh(&addrconf_hash_lock); 12181da177e4SLinus Torvalds 12195b84efecSThomas Haller write_lock_bh(&ifp->idev->lock); 12205d9efa7eSDavid S. Miller 12211da177e4SLinus Torvalds if (ifp->flags&IFA_F_TEMPORARY) { 1222372e6c8fSstephen hemminger list_del(&ifp->tmp_list); 12231da177e4SLinus Torvalds if (ifp->ifpub) { 12241da177e4SLinus Torvalds in6_ifa_put(ifp->ifpub); 12251da177e4SLinus Torvalds ifp->ifpub = NULL; 12261da177e4SLinus Torvalds } 12271da177e4SLinus Torvalds __in6_ifa_put(ifp); 12281da177e4SLinus Torvalds } 12291da177e4SLinus Torvalds 12305b84efecSThomas Haller if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE)) 12315b84efecSThomas Haller action = check_cleanup_prefix_route(ifp, &expires); 12325b84efecSThomas Haller 12338ef802aaSEric Dumazet list_del_rcu(&ifp->if_list); 12341da177e4SLinus Torvalds __in6_ifa_put(ifp); 1235502a2ffdSstephen hemminger 12365b84efecSThomas Haller write_unlock_bh(&ifp->idev->lock); 12371da177e4SLinus Torvalds 1238c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifp); 1239b2238566SAndrey Vagin 12401da177e4SLinus Torvalds ipv6_ifa_notify(RTM_DELADDR, ifp); 12411da177e4SLinus Torvalds 1242f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_DOWN, ifp); 12431da177e4SLinus Torvalds 12445b84efecSThomas Haller if (action != CLEANUP_PREFIX_RT_NOP) { 12455b84efecSThomas Haller cleanup_prefix_route(ifp, expires, 12465b84efecSThomas Haller action == CLEANUP_PREFIX_RT_DEL); 12471da177e4SLinus Torvalds } 12481da177e4SLinus Torvalds 1249c3968a85SDaniel Walter /* clean up prefsrc entries */ 1250c3968a85SDaniel Walter rt6_remove_prefsrc(ifp); 12514c5ff6a6SHerbert Xu out: 12521da177e4SLinus Torvalds in6_ifa_put(ifp); 12531da177e4SLinus Torvalds } 12541da177e4SLinus Torvalds 1255fffcefe9SEric Dumazet static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, 1256fffcefe9SEric Dumazet struct inet6_ifaddr *ift, 1257fffcefe9SEric Dumazet bool block) 12581da177e4SLinus Torvalds { 12591da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 12601da177e4SLinus Torvalds struct in6_addr addr, *tmpaddr; 1261e6464b8cSDavid Ahern unsigned long tmp_tstamp, age; 1262eac55bf9SBenoit Boissinot unsigned long regen_advance; 1263e6464b8cSDavid Ahern struct ifa6_config cfg; 12641da177e4SLinus Torvalds int ret = 0; 126576f793e3SLorenzo Colitti unsigned long now = jiffies; 126676506a98SJiri Bohac long max_desync_factor; 12677aa8e63fSJiri Bohac s32 cnf_temp_preferred_lft; 12681da177e4SLinus Torvalds 126953bd6749SJiri Pirko write_lock_bh(&idev->lock); 12701da177e4SLinus Torvalds if (ift) { 12711da177e4SLinus Torvalds spin_lock_bh(&ift->lock); 12721da177e4SLinus Torvalds memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8); 12731da177e4SLinus Torvalds spin_unlock_bh(&ift->lock); 12741da177e4SLinus Torvalds tmpaddr = &addr; 12751da177e4SLinus Torvalds } else { 12761da177e4SLinus Torvalds tmpaddr = NULL; 12771da177e4SLinus Torvalds } 12781da177e4SLinus Torvalds retry: 12791da177e4SLinus Torvalds in6_dev_hold(idev); 12801da177e4SLinus Torvalds if (idev->cnf.use_tempaddr <= 0) { 128153bd6749SJiri Pirko write_unlock_bh(&idev->lock); 1282f3213831SJoe Perches pr_info("%s: use_tempaddr is disabled\n", __func__); 12831da177e4SLinus Torvalds in6_dev_put(idev); 12841da177e4SLinus Torvalds ret = -1; 12851da177e4SLinus Torvalds goto out; 12861da177e4SLinus Torvalds } 12871da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 12881da177e4SLinus Torvalds if (ifp->regen_count++ >= idev->cnf.regen_max_retry) { 12891da177e4SLinus Torvalds idev->cnf.use_tempaddr = -1; /*XXX*/ 12901da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 129153bd6749SJiri Pirko write_unlock_bh(&idev->lock); 1292f3213831SJoe Perches pr_warn("%s: regeneration time exceeded - disabled temporary address support\n", 1293f3213831SJoe Perches __func__); 12941da177e4SLinus Torvalds in6_dev_put(idev); 12951da177e4SLinus Torvalds ret = -1; 12961da177e4SLinus Torvalds goto out; 12971da177e4SLinus Torvalds } 12981da177e4SLinus Torvalds in6_ifa_hold(ifp); 12991da177e4SLinus Torvalds memcpy(addr.s6_addr, ifp->addr.s6_addr, 8); 13009d6280daSJiri Bohac ipv6_try_regen_rndid(idev, tmpaddr); 13011da177e4SLinus Torvalds memcpy(&addr.s6_addr[8], idev->rndid, 8); 130276f793e3SLorenzo Colitti age = (now - ifp->tstamp) / HZ; 13031da177e4SLinus Torvalds 1304eac55bf9SBenoit Boissinot regen_advance = idev->cnf.regen_max_retry * 1305eac55bf9SBenoit Boissinot idev->cnf.dad_transmits * 13061f9248e5SJiri Pirko NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ; 130776506a98SJiri Bohac 130876506a98SJiri Bohac /* recalculate max_desync_factor each time and update 130976506a98SJiri Bohac * idev->desync_factor if it's larger 131076506a98SJiri Bohac */ 13117aa8e63fSJiri Bohac cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft); 131276506a98SJiri Bohac max_desync_factor = min_t(__u32, 131376506a98SJiri Bohac idev->cnf.max_desync_factor, 13147aa8e63fSJiri Bohac cnf_temp_preferred_lft - regen_advance); 131576506a98SJiri Bohac 131676506a98SJiri Bohac if (unlikely(idev->desync_factor > max_desync_factor)) { 131776506a98SJiri Bohac if (max_desync_factor > 0) { 131876506a98SJiri Bohac get_random_bytes(&idev->desync_factor, 131976506a98SJiri Bohac sizeof(idev->desync_factor)); 132076506a98SJiri Bohac idev->desync_factor %= max_desync_factor; 132176506a98SJiri Bohac } else { 132276506a98SJiri Bohac idev->desync_factor = 0; 132376506a98SJiri Bohac } 132476506a98SJiri Bohac } 132576506a98SJiri Bohac 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: 2252e837735eSNicolas Dichtel return addrconf_ifid_ip6tnl(eui, dev); 2253073a8e0eSYOSHIFUJI Hideaki } 22541da177e4SLinus Torvalds return -1; 22551da177e4SLinus Torvalds } 22561da177e4SLinus Torvalds 22571da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev) 22581da177e4SLinus Torvalds { 22591da177e4SLinus Torvalds int err = -1; 22601da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 22611da177e4SLinus Torvalds 22621da177e4SLinus Torvalds read_lock_bh(&idev->lock); 2263602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) { 2264602582caSHannes Frederic Sowa if (ifp->scope > IFA_LINK) 2265602582caSHannes Frederic Sowa break; 22661da177e4SLinus Torvalds if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) { 22671da177e4SLinus Torvalds memcpy(eui, ifp->addr.s6_addr+8, 8); 22681da177e4SLinus Torvalds err = 0; 22691da177e4SLinus Torvalds break; 22701da177e4SLinus Torvalds } 22711da177e4SLinus Torvalds } 22721da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 22731da177e4SLinus Torvalds return err; 22741da177e4SLinus Torvalds } 22751da177e4SLinus Torvalds 22761da177e4SLinus Torvalds /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */ 22779d6280daSJiri Bohac static void ipv6_regen_rndid(struct inet6_dev *idev) 22781da177e4SLinus Torvalds { 22791da177e4SLinus Torvalds regen: 2280955189efSYOSHIFUJI Hideaki get_random_bytes(idev->rndid, sizeof(idev->rndid)); 22811da177e4SLinus Torvalds idev->rndid[0] &= ~0x02; 22821da177e4SLinus Torvalds 22831da177e4SLinus Torvalds /* 22841da177e4SLinus Torvalds * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>: 22851da177e4SLinus Torvalds * check if generated address is not inappropriate 22861da177e4SLinus Torvalds * 22871da177e4SLinus Torvalds * - Reserved subnet anycast (RFC 2526) 22881da177e4SLinus Torvalds * 11111101 11....11 1xxxxxxx 2289c7dc89c0SFred L. Templin * - ISATAP (RFC4214) 6.1 22901da177e4SLinus Torvalds * 00-00-5E-FE-xx-xx-xx-xx 22911da177e4SLinus Torvalds * - value 0 22921da177e4SLinus Torvalds * - XXX: already assigned to an address on the device 22931da177e4SLinus Torvalds */ 22941da177e4SLinus Torvalds if (idev->rndid[0] == 0xfd && 22951da177e4SLinus Torvalds (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff && 22961da177e4SLinus Torvalds (idev->rndid[7]&0x80)) 22971da177e4SLinus Torvalds goto regen; 22981da177e4SLinus Torvalds if ((idev->rndid[0]|idev->rndid[1]) == 0) { 22991da177e4SLinus Torvalds if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe) 23001da177e4SLinus Torvalds goto regen; 23011da177e4SLinus Torvalds if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00) 23021da177e4SLinus Torvalds goto regen; 23031da177e4SLinus Torvalds } 23041da177e4SLinus Torvalds } 23051da177e4SLinus Torvalds 23069d6280daSJiri Bohac static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) 23078e5e8f30SEldad Zack { 23081da177e4SLinus Torvalds if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0) 23099d6280daSJiri Bohac ipv6_regen_rndid(idev); 23101da177e4SLinus Torvalds } 23111da177e4SLinus Torvalds 23121da177e4SLinus Torvalds /* 23131da177e4SLinus Torvalds * Add prefix route. 23141da177e4SLinus Torvalds */ 23151da177e4SLinus Torvalds 23161da177e4SLinus Torvalds static void 23171da177e4SLinus Torvalds addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev, 2318acb54e3cSDavid Ahern unsigned long expires, u32 flags, gfp_t gfp_flags) 23191da177e4SLinus Torvalds { 232086872cb5SThomas Graf struct fib6_config cfg = { 2321ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX, 232286872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 232386872cb5SThomas Graf .fc_ifindex = dev->ifindex, 232486872cb5SThomas Graf .fc_expires = expires, 232586872cb5SThomas Graf .fc_dst_len = plen, 232686872cb5SThomas Graf .fc_flags = RTF_UP | flags, 2327c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 2328f410a1fbSStephen Hemminger .fc_protocol = RTPROT_KERNEL, 2329e8478e80SDavid Ahern .fc_type = RTN_UNICAST, 233086872cb5SThomas Graf }; 23311da177e4SLinus Torvalds 23324e3fd7a0SAlexey Dobriyan cfg.fc_dst = *pfx; 23331da177e4SLinus Torvalds 23341da177e4SLinus Torvalds /* Prevent useless cloning on PtP SIT. 23351da177e4SLinus Torvalds This thing is done here expecting that the whole 23361da177e4SLinus Torvalds class of non-broadcast devices need not cloning. 23371da177e4SLinus Torvalds */ 233807a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 23391da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT)) 234086872cb5SThomas Graf cfg.fc_flags |= RTF_NONEXTHOP; 23410be669bbSJoerg Roedel #endif 23421da177e4SLinus Torvalds 2343acb54e3cSDavid Ahern ip6_route_add(&cfg, gfp_flags, NULL); 23441da177e4SLinus Torvalds } 23451da177e4SLinus Torvalds 234614ef37b6SAndreas Hofmeister 23478d1c802bSDavid Ahern static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx, 234814ef37b6SAndreas Hofmeister int plen, 234914ef37b6SAndreas Hofmeister const struct net_device *dev, 235014ef37b6SAndreas Hofmeister u32 flags, u32 noflags) 235114ef37b6SAndreas Hofmeister { 235214ef37b6SAndreas Hofmeister struct fib6_node *fn; 23538d1c802bSDavid Ahern struct fib6_info *rt = NULL; 235414ef37b6SAndreas Hofmeister struct fib6_table *table; 2355ca254490SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX; 235614ef37b6SAndreas Hofmeister 2357ca254490SDavid Ahern table = fib6_get_table(dev_net(dev), tb_id); 235863159f29SIan Morris if (!table) 235914ef37b6SAndreas Hofmeister return NULL; 236014ef37b6SAndreas Hofmeister 236166f5d6ceSWei Wang rcu_read_lock(); 236238fbeeeeSWei Wang fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0, true); 236314ef37b6SAndreas Hofmeister if (!fn) 236414ef37b6SAndreas Hofmeister goto out; 23651f56a01fSMartin KaFai Lau 236666f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 23675e670d84SDavid Ahern if (rt->fib6_nh.nh_dev->ifindex != dev->ifindex) 236814ef37b6SAndreas Hofmeister continue; 236993c2fb25SDavid Ahern if ((rt->fib6_flags & flags) != flags) 237014ef37b6SAndreas Hofmeister continue; 237193c2fb25SDavid Ahern if ((rt->fib6_flags & noflags) != 0) 237214ef37b6SAndreas Hofmeister continue; 237393531c67SDavid Ahern fib6_info_hold(rt); 237414ef37b6SAndreas Hofmeister break; 237514ef37b6SAndreas Hofmeister } 237614ef37b6SAndreas Hofmeister out: 237766f5d6ceSWei Wang rcu_read_unlock(); 237814ef37b6SAndreas Hofmeister return rt; 237914ef37b6SAndreas Hofmeister } 238014ef37b6SAndreas Hofmeister 238114ef37b6SAndreas Hofmeister 23821da177e4SLinus Torvalds /* Create "default" multicast route to the interface */ 23831da177e4SLinus Torvalds 23841da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev) 23851da177e4SLinus Torvalds { 238686872cb5SThomas Graf struct fib6_config cfg = { 2387ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL, 238886872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 238986872cb5SThomas Graf .fc_ifindex = dev->ifindex, 239086872cb5SThomas Graf .fc_dst_len = 8, 239186872cb5SThomas Graf .fc_flags = RTF_UP, 2392e8478e80SDavid Ahern .fc_type = RTN_UNICAST, 2393c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 239486872cb5SThomas Graf }; 23951da177e4SLinus Torvalds 239686872cb5SThomas Graf ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0); 239786872cb5SThomas Graf 2398acb54e3cSDavid Ahern ip6_route_add(&cfg, GFP_ATOMIC, NULL); 23991da177e4SLinus Torvalds } 24001da177e4SLinus Torvalds 24011da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev) 24021da177e4SLinus Torvalds { 24031da177e4SLinus Torvalds struct inet6_dev *idev; 24041da177e4SLinus Torvalds 24051da177e4SLinus Torvalds ASSERT_RTNL(); 24061da177e4SLinus Torvalds 2407e21e8467SStephen Hemminger idev = ipv6_find_idev(dev); 2408e21e8467SStephen Hemminger if (!idev) 240964e724f6SBrian Haley return ERR_PTR(-ENOBUFS); 241064e724f6SBrian Haley 241164e724f6SBrian Haley if (idev->cnf.disable_ipv6) 241264e724f6SBrian Haley return ERR_PTR(-EACCES); 24131da177e4SLinus Torvalds 24141da177e4SLinus Torvalds /* Add default multicast route */ 2415ba46ee4cSDavid Ahern if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev)) 24161da177e4SLinus Torvalds addrconf_add_mroute(dev); 24171da177e4SLinus Torvalds 24181da177e4SLinus Torvalds return idev; 24191da177e4SLinus Torvalds } 24201da177e4SLinus Torvalds 242153bd6749SJiri Pirko static void manage_tempaddrs(struct inet6_dev *idev, 242253bd6749SJiri Pirko struct inet6_ifaddr *ifp, 242353bd6749SJiri Pirko __u32 valid_lft, __u32 prefered_lft, 242453bd6749SJiri Pirko bool create, unsigned long now) 242553bd6749SJiri Pirko { 242653bd6749SJiri Pirko u32 flags; 242753bd6749SJiri Pirko struct inet6_ifaddr *ift; 242853bd6749SJiri Pirko 242953bd6749SJiri Pirko read_lock_bh(&idev->lock); 243053bd6749SJiri Pirko /* update all temporary addresses in the list */ 243153bd6749SJiri Pirko list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) { 243253bd6749SJiri Pirko int age, max_valid, max_prefered; 243353bd6749SJiri Pirko 243453bd6749SJiri Pirko if (ifp != ift->ifpub) 243553bd6749SJiri Pirko continue; 243653bd6749SJiri Pirko 243753bd6749SJiri Pirko /* RFC 4941 section 3.3: 243853bd6749SJiri Pirko * If a received option will extend the lifetime of a public 243953bd6749SJiri Pirko * address, the lifetimes of temporary addresses should 244053bd6749SJiri Pirko * be extended, subject to the overall constraint that no 244153bd6749SJiri Pirko * temporary addresses should ever remain "valid" or "preferred" 244253bd6749SJiri Pirko * for a time longer than (TEMP_VALID_LIFETIME) or 244353bd6749SJiri Pirko * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively. 244453bd6749SJiri Pirko */ 244553bd6749SJiri Pirko age = (now - ift->cstamp) / HZ; 244653bd6749SJiri Pirko max_valid = idev->cnf.temp_valid_lft - age; 244753bd6749SJiri Pirko if (max_valid < 0) 244853bd6749SJiri Pirko max_valid = 0; 244953bd6749SJiri Pirko 245053bd6749SJiri Pirko max_prefered = idev->cnf.temp_prefered_lft - 245176506a98SJiri Bohac idev->desync_factor - age; 245253bd6749SJiri Pirko if (max_prefered < 0) 245353bd6749SJiri Pirko max_prefered = 0; 245453bd6749SJiri Pirko 245553bd6749SJiri Pirko if (valid_lft > max_valid) 245653bd6749SJiri Pirko valid_lft = max_valid; 245753bd6749SJiri Pirko 245853bd6749SJiri Pirko if (prefered_lft > max_prefered) 245953bd6749SJiri Pirko prefered_lft = max_prefered; 246053bd6749SJiri Pirko 246153bd6749SJiri Pirko spin_lock(&ift->lock); 246253bd6749SJiri Pirko flags = ift->flags; 246353bd6749SJiri Pirko ift->valid_lft = valid_lft; 246453bd6749SJiri Pirko ift->prefered_lft = prefered_lft; 246553bd6749SJiri Pirko ift->tstamp = now; 246653bd6749SJiri Pirko if (prefered_lft > 0) 246753bd6749SJiri Pirko ift->flags &= ~IFA_F_DEPRECATED; 246853bd6749SJiri Pirko 246953bd6749SJiri Pirko spin_unlock(&ift->lock); 247053bd6749SJiri Pirko if (!(flags&IFA_F_TENTATIVE)) 247153bd6749SJiri Pirko ipv6_ifa_notify(0, ift); 247253bd6749SJiri Pirko } 247353bd6749SJiri Pirko 247453bd6749SJiri Pirko if ((create || list_empty(&idev->tempaddr_list)) && 247553bd6749SJiri Pirko idev->cnf.use_tempaddr > 0) { 247653bd6749SJiri Pirko /* When a new public address is created as described 247753bd6749SJiri Pirko * in [ADDRCONF], also create a new temporary address. 247853bd6749SJiri Pirko * Also create a temporary address if it's enabled but 247953bd6749SJiri Pirko * no temporary address currently exists. 248053bd6749SJiri Pirko */ 248153bd6749SJiri Pirko read_unlock_bh(&idev->lock); 2482fffcefe9SEric Dumazet ipv6_create_tempaddr(ifp, NULL, false); 248353bd6749SJiri Pirko } else { 248453bd6749SJiri Pirko read_unlock_bh(&idev->lock); 248553bd6749SJiri Pirko } 248653bd6749SJiri Pirko } 248753bd6749SJiri Pirko 2488cc9da6ccSBjørn Mork static bool is_addr_mode_generate_stable(struct inet6_dev *idev) 2489cc9da6ccSBjørn Mork { 2490d35a00b8SFelix Jia return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY || 2491d35a00b8SFelix Jia idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM; 2492cc9da6ccSBjørn Mork } 2493cc9da6ccSBjørn Mork 2494cc84b3c6SAlexander Aring int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev, 24954f672235SAlexander Aring const struct prefix_info *pinfo, 24964f672235SAlexander Aring struct inet6_dev *in6_dev, 2497cc84b3c6SAlexander Aring const struct in6_addr *addr, int addr_type, 2498cc84b3c6SAlexander Aring u32 addr_flags, bool sllao, bool tokenized, 24994f672235SAlexander Aring __u32 valid_lft, u32 prefered_lft) 25004f672235SAlexander Aring { 25014f672235SAlexander Aring struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1); 25024f672235SAlexander Aring int create = 0, update_lft = 0; 25034f672235SAlexander Aring 25044f672235SAlexander Aring if (!ifp && valid_lft) { 25054f672235SAlexander Aring int max_addresses = in6_dev->cnf.max_addresses; 2506e6464b8cSDavid Ahern struct ifa6_config cfg = { 2507e6464b8cSDavid Ahern .pfx = addr, 2508e6464b8cSDavid Ahern .plen = pinfo->prefix_len, 2509e6464b8cSDavid Ahern .ifa_flags = addr_flags, 2510e6464b8cSDavid Ahern .valid_lft = valid_lft, 2511e6464b8cSDavid Ahern .preferred_lft = prefered_lft, 2512e6464b8cSDavid Ahern .scope = addr_type & IPV6_ADDR_SCOPE_MASK, 2513e6464b8cSDavid Ahern }; 25144f672235SAlexander Aring 25154f672235SAlexander Aring #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 251635e015e1SMatteo Croce if ((net->ipv6.devconf_all->optimistic_dad || 251735e015e1SMatteo Croce in6_dev->cnf.optimistic_dad) && 25184f672235SAlexander Aring !net->ipv6.devconf_all->forwarding && sllao) 2519e6464b8cSDavid Ahern cfg.ifa_flags |= IFA_F_OPTIMISTIC; 25204f672235SAlexander Aring #endif 25214f672235SAlexander Aring 25224f672235SAlexander Aring /* Do not allow to create too much of autoconfigured 25234f672235SAlexander Aring * addresses; this would be too easy way to crash kernel. 25244f672235SAlexander Aring */ 25254f672235SAlexander Aring if (!max_addresses || 25264f672235SAlexander Aring ipv6_count_addresses(in6_dev) < max_addresses) 2527e6464b8cSDavid Ahern ifp = ipv6_add_addr(in6_dev, &cfg, false, NULL); 25284f672235SAlexander Aring 25294f672235SAlexander Aring if (IS_ERR_OR_NULL(ifp)) 25304f672235SAlexander Aring return -1; 25314f672235SAlexander Aring 25324f672235SAlexander Aring create = 1; 25334f672235SAlexander Aring spin_lock_bh(&ifp->lock); 25344f672235SAlexander Aring ifp->flags |= IFA_F_MANAGETEMPADDR; 25354f672235SAlexander Aring ifp->cstamp = jiffies; 25364f672235SAlexander Aring ifp->tokenized = tokenized; 25374f672235SAlexander Aring spin_unlock_bh(&ifp->lock); 25384f672235SAlexander Aring addrconf_dad_start(ifp); 25394f672235SAlexander Aring } 25404f672235SAlexander Aring 25414f672235SAlexander Aring if (ifp) { 25424f672235SAlexander Aring u32 flags; 25434f672235SAlexander Aring unsigned long now; 25444f672235SAlexander Aring u32 stored_lft; 25454f672235SAlexander Aring 25464f672235SAlexander Aring /* update lifetime (RFC2462 5.5.3 e) */ 25474f672235SAlexander Aring spin_lock_bh(&ifp->lock); 25484f672235SAlexander Aring now = jiffies; 25494f672235SAlexander Aring if (ifp->valid_lft > (now - ifp->tstamp) / HZ) 25504f672235SAlexander Aring stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ; 25514f672235SAlexander Aring else 25524f672235SAlexander Aring stored_lft = 0; 2553f85f94b8SLorenzo Bianconi if (!create && stored_lft) { 25544f672235SAlexander Aring const u32 minimum_lft = min_t(u32, 25554f672235SAlexander Aring stored_lft, MIN_VALID_LIFETIME); 25564f672235SAlexander Aring valid_lft = max(valid_lft, minimum_lft); 25574f672235SAlexander Aring 25584f672235SAlexander Aring /* RFC4862 Section 5.5.3e: 25594f672235SAlexander Aring * "Note that the preferred lifetime of the 25604f672235SAlexander Aring * corresponding address is always reset to 25614f672235SAlexander Aring * the Preferred Lifetime in the received 25624f672235SAlexander Aring * Prefix Information option, regardless of 25634f672235SAlexander Aring * whether the valid lifetime is also reset or 25644f672235SAlexander Aring * ignored." 25654f672235SAlexander Aring * 25664f672235SAlexander Aring * So we should always update prefered_lft here. 25674f672235SAlexander Aring */ 25684f672235SAlexander Aring update_lft = 1; 25694f672235SAlexander Aring } 25704f672235SAlexander Aring 25714f672235SAlexander Aring if (update_lft) { 25724f672235SAlexander Aring ifp->valid_lft = valid_lft; 25734f672235SAlexander Aring ifp->prefered_lft = prefered_lft; 25744f672235SAlexander Aring ifp->tstamp = now; 25754f672235SAlexander Aring flags = ifp->flags; 25764f672235SAlexander Aring ifp->flags &= ~IFA_F_DEPRECATED; 25774f672235SAlexander Aring spin_unlock_bh(&ifp->lock); 25784f672235SAlexander Aring 25794f672235SAlexander Aring if (!(flags&IFA_F_TENTATIVE)) 25804f672235SAlexander Aring ipv6_ifa_notify(0, ifp); 25814f672235SAlexander Aring } else 25824f672235SAlexander Aring spin_unlock_bh(&ifp->lock); 25834f672235SAlexander Aring 25844f672235SAlexander Aring manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft, 25854f672235SAlexander Aring create, now); 25864f672235SAlexander Aring 25874f672235SAlexander Aring in6_ifa_put(ifp); 25884f672235SAlexander Aring addrconf_verify(); 25894f672235SAlexander Aring } 25904f672235SAlexander Aring 25914f672235SAlexander Aring return 0; 25924f672235SAlexander Aring } 2593cc84b3c6SAlexander Aring EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr); 25944f672235SAlexander Aring 2595e6bff995SNeil Horman void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao) 25961da177e4SLinus Torvalds { 25971da177e4SLinus Torvalds struct prefix_info *pinfo; 25981da177e4SLinus Torvalds __u32 valid_lft; 25991da177e4SLinus Torvalds __u32 prefered_lft; 26004f672235SAlexander Aring int addr_type, err; 260164236f3fSHannes Frederic Sowa u32 addr_flags = 0; 26021da177e4SLinus Torvalds struct inet6_dev *in6_dev; 260356d417b1SBrian Haley struct net *net = dev_net(dev); 26041da177e4SLinus Torvalds 26051da177e4SLinus Torvalds pinfo = (struct prefix_info *) opt; 26061da177e4SLinus Torvalds 26071da177e4SLinus Torvalds if (len < sizeof(struct prefix_info)) { 2608e32ac250SJoe Perches netdev_dbg(dev, "addrconf: prefix option too short\n"); 26091da177e4SLinus Torvalds return; 26101da177e4SLinus Torvalds } 26111da177e4SLinus Torvalds 26121da177e4SLinus Torvalds /* 26131da177e4SLinus Torvalds * Validation checks ([ADDRCONF], page 19) 26141da177e4SLinus Torvalds */ 26151da177e4SLinus Torvalds 26161da177e4SLinus Torvalds addr_type = ipv6_addr_type(&pinfo->prefix); 26171da177e4SLinus Torvalds 26181da177e4SLinus Torvalds if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)) 26191da177e4SLinus Torvalds return; 26201da177e4SLinus Torvalds 26211da177e4SLinus Torvalds valid_lft = ntohl(pinfo->valid); 26221da177e4SLinus Torvalds prefered_lft = ntohl(pinfo->prefered); 26231da177e4SLinus Torvalds 26241da177e4SLinus Torvalds if (prefered_lft > valid_lft) { 2625e87cc472SJoe Perches net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n"); 26261da177e4SLinus Torvalds return; 26271da177e4SLinus Torvalds } 26281da177e4SLinus Torvalds 26291da177e4SLinus Torvalds in6_dev = in6_dev_get(dev); 26301da177e4SLinus Torvalds 263163159f29SIan Morris if (!in6_dev) { 2632e87cc472SJoe Perches net_dbg_ratelimited("addrconf: device %s not configured\n", 2633e87cc472SJoe Perches dev->name); 26341da177e4SLinus Torvalds return; 26351da177e4SLinus Torvalds } 26361da177e4SLinus Torvalds 26371da177e4SLinus Torvalds /* 26381da177e4SLinus Torvalds * Two things going on here: 26391da177e4SLinus Torvalds * 1) Add routes for on-link prefixes 26401da177e4SLinus Torvalds * 2) Configure prefixes with the auto flag set 26411da177e4SLinus Torvalds */ 26421da177e4SLinus Torvalds 26434bed72e4SYOSHIFUJI Hideaki if (pinfo->onlink) { 26448d1c802bSDavid Ahern struct fib6_info *rt; 26454bed72e4SYOSHIFUJI Hideaki unsigned long rt_expires; 26464bed72e4SYOSHIFUJI Hideaki 26471da177e4SLinus Torvalds /* Avoid arithmetic overflow. Really, we could 26486f704992SYOSHIFUJI Hideaki * save rt_expires in seconds, likely valid_lft, 26496f704992SYOSHIFUJI Hideaki * but it would require division in fib gc, that it 26506f704992SYOSHIFUJI Hideaki * not good. 26511da177e4SLinus Torvalds */ 26524bed72e4SYOSHIFUJI Hideaki if (HZ > USER_HZ) 26534bed72e4SYOSHIFUJI Hideaki rt_expires = addrconf_timeout_fixup(valid_lft, HZ); 26544bed72e4SYOSHIFUJI Hideaki else 26554bed72e4SYOSHIFUJI Hideaki rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ); 26563dd4bc68SYOSHIFUJI Hideaki 26574bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(rt_expires)) 26584bed72e4SYOSHIFUJI Hideaki rt_expires *= HZ; 26591da177e4SLinus Torvalds 266014ef37b6SAndreas Hofmeister rt = addrconf_get_prefix_route(&pinfo->prefix, 266114ef37b6SAndreas Hofmeister pinfo->prefix_len, 266214ef37b6SAndreas Hofmeister dev, 266314ef37b6SAndreas Hofmeister RTF_ADDRCONF | RTF_PREFIX_RT, 266414ef37b6SAndreas Hofmeister RTF_GATEWAY | RTF_DEFAULT); 26651da177e4SLinus Torvalds 266614ef37b6SAndreas Hofmeister if (rt) { 26676f704992SYOSHIFUJI Hideaki /* Autoconf prefix route */ 26681da177e4SLinus Torvalds if (valid_lft == 0) { 2669afb1d4b5SDavid Ahern ip6_del_rt(net, rt); 26701da177e4SLinus Torvalds rt = NULL; 26714bed72e4SYOSHIFUJI Hideaki } else if (addrconf_finite_timeout(rt_expires)) { 26726f704992SYOSHIFUJI Hideaki /* not infinity */ 267314895687SDavid Ahern fib6_set_expires(rt, jiffies + rt_expires); 26746f704992SYOSHIFUJI Hideaki } else { 267514895687SDavid Ahern fib6_clean_expires(rt); 26761da177e4SLinus Torvalds } 26771da177e4SLinus Torvalds } else if (valid_lft) { 26786f704992SYOSHIFUJI Hideaki clock_t expires = 0; 26794bed72e4SYOSHIFUJI Hideaki int flags = RTF_ADDRCONF | RTF_PREFIX_RT; 26804bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(rt_expires)) { 26816f704992SYOSHIFUJI Hideaki /* not infinity */ 26826f704992SYOSHIFUJI Hideaki flags |= RTF_EXPIRES; 26836f704992SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(rt_expires); 26846f704992SYOSHIFUJI Hideaki } 26851da177e4SLinus Torvalds addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len, 2686acb54e3cSDavid Ahern dev, expires, flags, GFP_ATOMIC); 26871da177e4SLinus Torvalds } 268893531c67SDavid Ahern fib6_info_release(rt); 26891da177e4SLinus Torvalds } 26901da177e4SLinus Torvalds 26911da177e4SLinus Torvalds /* Try to figure out our local address for this prefix */ 26921da177e4SLinus Torvalds 26931da177e4SLinus Torvalds if (pinfo->autoconf && in6_dev->cnf.autoconf) { 26941da177e4SLinus Torvalds struct in6_addr addr; 2695f997c55cSAlexander Aring bool tokenized = false, dev_addr_generated = false; 26961da177e4SLinus Torvalds 26971da177e4SLinus Torvalds if (pinfo->prefix_len == 64) { 26981da177e4SLinus Torvalds memcpy(&addr, &pinfo->prefix, 8); 2699f53adae4SDaniel Borkmann 2700f53adae4SDaniel Borkmann if (!ipv6_addr_any(&in6_dev->token)) { 2701f53adae4SDaniel Borkmann read_lock_bh(&in6_dev->lock); 2702f53adae4SDaniel Borkmann memcpy(addr.s6_addr + 8, 2703f53adae4SDaniel Borkmann in6_dev->token.s6_addr + 8, 8); 2704f53adae4SDaniel Borkmann read_unlock_bh(&in6_dev->lock); 2705617fe29dSDaniel Borkmann tokenized = true; 2706cc9da6ccSBjørn Mork } else if (is_addr_mode_generate_stable(in6_dev) && 2707622c81d5SHannes Frederic Sowa !ipv6_generate_stable_address(&addr, 0, 2708622c81d5SHannes Frederic Sowa in6_dev)) { 270964236f3fSHannes Frederic Sowa addr_flags |= IFA_F_STABLE_PRIVACY; 2710622c81d5SHannes Frederic Sowa goto ok; 2711f53adae4SDaniel Borkmann } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) && 27121da177e4SLinus Torvalds ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) { 27134f672235SAlexander Aring goto put; 2714f997c55cSAlexander Aring } else { 2715f997c55cSAlexander Aring dev_addr_generated = true; 27161da177e4SLinus Torvalds } 27171da177e4SLinus Torvalds goto ok; 27181da177e4SLinus Torvalds } 2719e87cc472SJoe Perches net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n", 27201da177e4SLinus Torvalds pinfo->prefix_len); 27214f672235SAlexander Aring goto put; 27221da177e4SLinus Torvalds 27231da177e4SLinus Torvalds ok: 27244f672235SAlexander Aring err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, 27254f672235SAlexander Aring &addr, addr_type, 27264f672235SAlexander Aring addr_flags, sllao, 27274f672235SAlexander Aring tokenized, valid_lft, 27288a226b2cSJiri Benc prefered_lft); 27294f672235SAlexander Aring if (err) 27304f672235SAlexander Aring goto put; 2731f997c55cSAlexander Aring 2732f997c55cSAlexander Aring /* Ignore error case here because previous prefix add addr was 2733f997c55cSAlexander Aring * successful which will be notified. 2734f997c55cSAlexander Aring */ 2735f997c55cSAlexander Aring ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr, 2736f997c55cSAlexander Aring addr_type, addr_flags, sllao, 2737f997c55cSAlexander Aring tokenized, valid_lft, 2738f997c55cSAlexander Aring prefered_lft, 2739f997c55cSAlexander Aring dev_addr_generated); 27401da177e4SLinus Torvalds } 27411da177e4SLinus Torvalds inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo); 27424f672235SAlexander Aring put: 27431da177e4SLinus Torvalds in6_dev_put(in6_dev); 27441da177e4SLinus Torvalds } 27451da177e4SLinus Torvalds 27461da177e4SLinus Torvalds /* 27471da177e4SLinus Torvalds * Set destination address. 27481da177e4SLinus Torvalds * Special case for SIT interfaces where we create a new "virtual" 27491da177e4SLinus Torvalds * device. 27501da177e4SLinus Torvalds */ 2751af284937SDaniel Lezcano int addrconf_set_dstaddr(struct net *net, void __user *arg) 27521da177e4SLinus Torvalds { 27531da177e4SLinus Torvalds struct in6_ifreq ireq; 27541da177e4SLinus Torvalds struct net_device *dev; 27551da177e4SLinus Torvalds int err = -EINVAL; 27561da177e4SLinus Torvalds 27571da177e4SLinus Torvalds rtnl_lock(); 27581da177e4SLinus Torvalds 27591da177e4SLinus Torvalds err = -EFAULT; 27601da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 27611da177e4SLinus Torvalds goto err_exit; 27621da177e4SLinus Torvalds 2763af284937SDaniel Lezcano dev = __dev_get_by_index(net, ireq.ifr6_ifindex); 27641da177e4SLinus Torvalds 27651da177e4SLinus Torvalds err = -ENODEV; 276663159f29SIan Morris if (!dev) 27671da177e4SLinus Torvalds goto err_exit; 27681da177e4SLinus Torvalds 276907a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 27701da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT) { 27715bc3eb7eSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 27721da177e4SLinus Torvalds struct ifreq ifr; 27731da177e4SLinus Torvalds struct ip_tunnel_parm p; 27741da177e4SLinus Torvalds 27751da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 27761da177e4SLinus Torvalds if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4)) 27771da177e4SLinus Torvalds goto err_exit; 27781da177e4SLinus Torvalds 27791da177e4SLinus Torvalds memset(&p, 0, sizeof(p)); 27801da177e4SLinus Torvalds p.iph.daddr = ireq.ifr6_addr.s6_addr32[3]; 27811da177e4SLinus Torvalds p.iph.saddr = 0; 27821da177e4SLinus Torvalds p.iph.version = 4; 27831da177e4SLinus Torvalds p.iph.ihl = 5; 27841da177e4SLinus Torvalds p.iph.protocol = IPPROTO_IPV6; 27851da177e4SLinus Torvalds p.iph.ttl = 64; 2786d20b3109SStephen Hemminger ifr.ifr_ifru.ifru_data = (__force void __user *)&p; 27871da177e4SLinus Torvalds 27885bc3eb7eSStephen Hemminger if (ops->ndo_do_ioctl) { 27895bc3eb7eSStephen Hemminger mm_segment_t oldfs = get_fs(); 27905bc3eb7eSStephen Hemminger 27915bc3eb7eSStephen Hemminger set_fs(KERNEL_DS); 27925bc3eb7eSStephen Hemminger err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL); 27931da177e4SLinus Torvalds set_fs(oldfs); 27945bc3eb7eSStephen Hemminger } else 27955bc3eb7eSStephen Hemminger err = -EOPNOTSUPP; 27961da177e4SLinus Torvalds 27971da177e4SLinus Torvalds if (err == 0) { 27981da177e4SLinus Torvalds err = -ENOBUFS; 2799af284937SDaniel Lezcano dev = __dev_get_by_name(net, p.name); 2800af284937SDaniel Lezcano if (!dev) 28011da177e4SLinus Torvalds goto err_exit; 28021da177e4SLinus Torvalds err = dev_open(dev); 28031da177e4SLinus Torvalds } 28041da177e4SLinus Torvalds } 28050be669bbSJoerg Roedel #endif 28061da177e4SLinus Torvalds 28071da177e4SLinus Torvalds err_exit: 28081da177e4SLinus Torvalds rtnl_unlock(); 28091da177e4SLinus Torvalds return err; 28101da177e4SLinus Torvalds } 28111da177e4SLinus Torvalds 281293a714d6SMadhu Challa static int ipv6_mc_config(struct sock *sk, bool join, 281393a714d6SMadhu Challa const struct in6_addr *addr, int ifindex) 281493a714d6SMadhu Challa { 281593a714d6SMadhu Challa int ret; 281693a714d6SMadhu Challa 281793a714d6SMadhu Challa ASSERT_RTNL(); 281893a714d6SMadhu Challa 281993a714d6SMadhu Challa lock_sock(sk); 282093a714d6SMadhu Challa if (join) 282154ff9ef3SMarcelo Ricardo Leitner ret = ipv6_sock_mc_join(sk, ifindex, addr); 282293a714d6SMadhu Challa else 282354ff9ef3SMarcelo Ricardo Leitner ret = ipv6_sock_mc_drop(sk, ifindex, addr); 282493a714d6SMadhu Challa release_sock(sk); 282593a714d6SMadhu Challa 282693a714d6SMadhu Challa return ret; 282793a714d6SMadhu Challa } 282893a714d6SMadhu Challa 28291da177e4SLinus Torvalds /* 28301da177e4SLinus Torvalds * Manual configuration of address on an interface 28311da177e4SLinus Torvalds */ 2832479840ffSJiri Pirko static int inet6_addr_add(struct net *net, int ifindex, 283319b1518cSDavid Ahern struct ifa6_config *cfg, 2834de95e047SDavid Ahern struct netlink_ext_ack *extack) 28351da177e4SLinus Torvalds { 28361da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 28371da177e4SLinus Torvalds struct inet6_dev *idev; 28381da177e4SLinus Torvalds struct net_device *dev; 283993a714d6SMadhu Challa unsigned long timeout; 284093a714d6SMadhu Challa clock_t expires; 28416f704992SYOSHIFUJI Hideaki u32 flags; 28421da177e4SLinus Torvalds 28431da177e4SLinus Torvalds ASSERT_RTNL(); 28441da177e4SLinus Torvalds 284519b1518cSDavid Ahern if (cfg->plen > 128) 284624ef0da7SThomas Graf return -EINVAL; 284724ef0da7SThomas Graf 28480778769dSNoriaki TAKAMIYA /* check the lifetime */ 284919b1518cSDavid Ahern if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft) 28500778769dSNoriaki TAKAMIYA return -EINVAL; 28510778769dSNoriaki TAKAMIYA 285219b1518cSDavid Ahern if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR && cfg->plen != 64) 285353bd6749SJiri Pirko return -EINVAL; 285453bd6749SJiri Pirko 2855af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifindex); 2856af284937SDaniel Lezcano if (!dev) 28571da177e4SLinus Torvalds return -ENODEV; 28581da177e4SLinus Torvalds 285964e724f6SBrian Haley idev = addrconf_add_dev(dev); 286064e724f6SBrian Haley if (IS_ERR(idev)) 286164e724f6SBrian Haley return PTR_ERR(idev); 28621da177e4SLinus Torvalds 286319b1518cSDavid Ahern if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) { 286493a714d6SMadhu Challa int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk, 286519b1518cSDavid Ahern true, cfg->pfx, ifindex); 286693a714d6SMadhu Challa 286793a714d6SMadhu Challa if (ret < 0) 286893a714d6SMadhu Challa return ret; 286993a714d6SMadhu Challa } 287093a714d6SMadhu Challa 287119b1518cSDavid Ahern cfg->scope = ipv6_addr_scope(cfg->pfx); 28721da177e4SLinus Torvalds 287319b1518cSDavid Ahern timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ); 28744bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 28754bed72e4SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(timeout * HZ); 287619b1518cSDavid Ahern cfg->valid_lft = timeout; 28776f704992SYOSHIFUJI Hideaki flags = RTF_EXPIRES; 28784bed72e4SYOSHIFUJI Hideaki } else { 28794bed72e4SYOSHIFUJI Hideaki expires = 0; 28804bed72e4SYOSHIFUJI Hideaki flags = 0; 288119b1518cSDavid Ahern cfg->ifa_flags |= IFA_F_PERMANENT; 28826f704992SYOSHIFUJI Hideaki } 28830778769dSNoriaki TAKAMIYA 288419b1518cSDavid Ahern timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ); 28854bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 28864bed72e4SYOSHIFUJI Hideaki if (timeout == 0) 288719b1518cSDavid Ahern cfg->ifa_flags |= IFA_F_DEPRECATED; 288819b1518cSDavid Ahern cfg->preferred_lft = timeout; 28894bed72e4SYOSHIFUJI Hideaki } 28900778769dSNoriaki TAKAMIYA 289119b1518cSDavid Ahern ifp = ipv6_add_addr(idev, cfg, true, extack); 28921da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 289319b1518cSDavid Ahern if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) { 289446d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 2895acb54e3cSDavid Ahern expires, flags, GFP_KERNEL); 2896761aac73SThomas Haller } 2897761aac73SThomas Haller 2898a2d481b3SLorenzo Bianconi /* Send a netlink notification if DAD is enabled and 2899a2d481b3SLorenzo Bianconi * optimistic flag is not set 2900a2d481b3SLorenzo Bianconi */ 2901a2d481b3SLorenzo Bianconi if (!(ifp->flags & (IFA_F_OPTIMISTIC | IFA_F_NODAD))) 2902a2d481b3SLorenzo Bianconi ipv6_ifa_notify(0, ifp); 290395c385b4SNeil Horman /* 290495c385b4SNeil Horman * Note that section 3.1 of RFC 4429 indicates 290595c385b4SNeil Horman * that the Optimistic flag should not be set for 290695c385b4SNeil Horman * manually configured addresses 290795c385b4SNeil Horman */ 2908cf22f9a2SDavid S. Miller addrconf_dad_start(ifp); 290919b1518cSDavid Ahern if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR) 291019b1518cSDavid Ahern manage_tempaddrs(idev, ifp, cfg->valid_lft, 291119b1518cSDavid Ahern cfg->preferred_lft, true, jiffies); 29121da177e4SLinus Torvalds in6_ifa_put(ifp); 2913c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 29141da177e4SLinus Torvalds return 0; 291519b1518cSDavid Ahern } else if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) { 291619b1518cSDavid Ahern ipv6_mc_config(net->ipv6.mc_autojoin_sk, false, 291719b1518cSDavid Ahern cfg->pfx, ifindex); 29181da177e4SLinus Torvalds } 29191da177e4SLinus Torvalds 29201da177e4SLinus Torvalds return PTR_ERR(ifp); 29211da177e4SLinus Torvalds } 29221da177e4SLinus Torvalds 29236046d5b4SHeiner Kallweit static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags, 29246046d5b4SHeiner Kallweit const struct in6_addr *pfx, unsigned int plen) 29251da177e4SLinus Torvalds { 29261da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 29271da177e4SLinus Torvalds struct inet6_dev *idev; 29281da177e4SLinus Torvalds struct net_device *dev; 29291da177e4SLinus Torvalds 293024ef0da7SThomas Graf if (plen > 128) 293124ef0da7SThomas Graf return -EINVAL; 293224ef0da7SThomas Graf 2933af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifindex); 2934af284937SDaniel Lezcano if (!dev) 29351da177e4SLinus Torvalds return -ENODEV; 29361da177e4SLinus Torvalds 2937e5d08d71SIan Morris idev = __in6_dev_get(dev); 293863159f29SIan Morris if (!idev) 29391da177e4SLinus Torvalds return -ENXIO; 29401da177e4SLinus Torvalds 29411da177e4SLinus Torvalds read_lock_bh(&idev->lock); 2942502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 29431da177e4SLinus Torvalds if (ifp->prefix_len == plen && 29441da177e4SLinus Torvalds ipv6_addr_equal(pfx, &ifp->addr)) { 29451da177e4SLinus Torvalds in6_ifa_hold(ifp); 29461da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 29471da177e4SLinus Torvalds 29486046d5b4SHeiner Kallweit if (!(ifp->flags & IFA_F_TEMPORARY) && 29496046d5b4SHeiner Kallweit (ifa_flags & IFA_F_MANAGETEMPADDR)) 29506046d5b4SHeiner Kallweit manage_tempaddrs(idev, ifp, 0, 0, false, 29516046d5b4SHeiner Kallweit jiffies); 29521da177e4SLinus Torvalds ipv6_del_addr(ifp); 29536046d5b4SHeiner Kallweit addrconf_verify_rtnl(); 295493a714d6SMadhu Challa if (ipv6_addr_is_multicast(pfx)) { 295593a714d6SMadhu Challa ipv6_mc_config(net->ipv6.mc_autojoin_sk, 295693a714d6SMadhu Challa false, pfx, dev->ifindex); 295793a714d6SMadhu Challa } 29581da177e4SLinus Torvalds return 0; 29591da177e4SLinus Torvalds } 29601da177e4SLinus Torvalds } 29611da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 29621da177e4SLinus Torvalds return -EADDRNOTAVAIL; 29631da177e4SLinus Torvalds } 29641da177e4SLinus Torvalds 29651da177e4SLinus Torvalds 2966af284937SDaniel Lezcano int addrconf_add_ifaddr(struct net *net, void __user *arg) 29671da177e4SLinus Torvalds { 296819b1518cSDavid Ahern struct ifa6_config cfg = { 296919b1518cSDavid Ahern .ifa_flags = IFA_F_PERMANENT, 297019b1518cSDavid Ahern .preferred_lft = INFINITY_LIFE_TIME, 297119b1518cSDavid Ahern .valid_lft = INFINITY_LIFE_TIME, 297219b1518cSDavid Ahern }; 29731da177e4SLinus Torvalds struct in6_ifreq ireq; 29741da177e4SLinus Torvalds int err; 29751da177e4SLinus Torvalds 2976af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 29771da177e4SLinus Torvalds return -EPERM; 29781da177e4SLinus Torvalds 29791da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 29801da177e4SLinus Torvalds return -EFAULT; 29811da177e4SLinus Torvalds 298219b1518cSDavid Ahern cfg.pfx = &ireq.ifr6_addr; 298319b1518cSDavid Ahern cfg.plen = ireq.ifr6_prefixlen; 298419b1518cSDavid Ahern 29851da177e4SLinus Torvalds rtnl_lock(); 298619b1518cSDavid Ahern err = inet6_addr_add(net, ireq.ifr6_ifindex, &cfg, NULL); 29871da177e4SLinus Torvalds rtnl_unlock(); 29881da177e4SLinus Torvalds return err; 29891da177e4SLinus Torvalds } 29901da177e4SLinus Torvalds 2991af284937SDaniel Lezcano int addrconf_del_ifaddr(struct net *net, void __user *arg) 29921da177e4SLinus Torvalds { 29931da177e4SLinus Torvalds struct in6_ifreq ireq; 29941da177e4SLinus Torvalds int err; 29951da177e4SLinus Torvalds 2996af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 29971da177e4SLinus Torvalds return -EPERM; 29981da177e4SLinus Torvalds 29991da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 30001da177e4SLinus Torvalds return -EFAULT; 30011da177e4SLinus Torvalds 30021da177e4SLinus Torvalds rtnl_lock(); 30036046d5b4SHeiner Kallweit err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr, 3004af284937SDaniel Lezcano ireq.ifr6_prefixlen); 30051da177e4SLinus Torvalds rtnl_unlock(); 30061da177e4SLinus Torvalds return err; 30071da177e4SLinus Torvalds } 30081da177e4SLinus Torvalds 3009b5f348e5SIlpo Järvinen static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr, 3010b5f348e5SIlpo Järvinen int plen, int scope) 3011b5f348e5SIlpo Järvinen { 3012b5f348e5SIlpo Järvinen struct inet6_ifaddr *ifp; 3013e6464b8cSDavid Ahern struct ifa6_config cfg = { 3014e6464b8cSDavid Ahern .pfx = addr, 3015e6464b8cSDavid Ahern .plen = plen, 3016e6464b8cSDavid Ahern .ifa_flags = IFA_F_PERMANENT, 3017e6464b8cSDavid Ahern .valid_lft = INFINITY_LIFE_TIME, 3018e6464b8cSDavid Ahern .preferred_lft = INFINITY_LIFE_TIME, 3019e6464b8cSDavid Ahern .scope = scope 3020e6464b8cSDavid Ahern }; 3021b5f348e5SIlpo Järvinen 3022e6464b8cSDavid Ahern ifp = ipv6_add_addr(idev, &cfg, true, NULL); 3023b5f348e5SIlpo Järvinen if (!IS_ERR(ifp)) { 3024b5f348e5SIlpo Järvinen spin_lock_bh(&ifp->lock); 3025b5f348e5SIlpo Järvinen ifp->flags &= ~IFA_F_TENTATIVE; 3026b5f348e5SIlpo Järvinen spin_unlock_bh(&ifp->lock); 3027764d3be6SPaolo Abeni rt_genid_bump_ipv6(dev_net(idev->dev)); 3028b5f348e5SIlpo Järvinen ipv6_ifa_notify(RTM_NEWADDR, ifp); 3029b5f348e5SIlpo Järvinen in6_ifa_put(ifp); 3030b5f348e5SIlpo Järvinen } 3031b5f348e5SIlpo Järvinen } 3032b5f348e5SIlpo Järvinen 303307a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 30341da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev) 30351da177e4SLinus Torvalds { 30361da177e4SLinus Torvalds struct in6_addr addr; 30371da177e4SLinus Torvalds struct net_device *dev; 3038c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 3039929c9cf3SNicolas Dichtel int scope, plen; 3040f0e2acfaSNicolas Dichtel u32 pflags = 0; 30411da177e4SLinus Torvalds 30421da177e4SLinus Torvalds ASSERT_RTNL(); 30431da177e4SLinus Torvalds 30441da177e4SLinus Torvalds memset(&addr, 0, sizeof(struct in6_addr)); 30451da177e4SLinus Torvalds memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4); 30461da177e4SLinus Torvalds 30471da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) { 30481da177e4SLinus Torvalds addr.s6_addr32[0] = htonl(0xfe800000); 30491da177e4SLinus Torvalds scope = IFA_LINK; 3050929c9cf3SNicolas Dichtel plen = 64; 30511da177e4SLinus Torvalds } else { 30521da177e4SLinus Torvalds scope = IPV6_ADDR_COMPATv4; 3053929c9cf3SNicolas Dichtel plen = 96; 3054f0e2acfaSNicolas Dichtel pflags |= RTF_NONEXTHOP; 30551da177e4SLinus Torvalds } 30561da177e4SLinus Torvalds 30571da177e4SLinus Torvalds if (addr.s6_addr32[3]) { 3058929c9cf3SNicolas Dichtel add_addr(idev, &addr, plen, scope); 3059acb54e3cSDavid Ahern addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags, 3060acb54e3cSDavid Ahern GFP_ATOMIC); 30611da177e4SLinus Torvalds return; 30621da177e4SLinus Torvalds } 30631da177e4SLinus Torvalds 30646fda7350SBenjamin Thery for_each_netdev(net, dev) { 3065e5ed6399SHerbert Xu struct in_device *in_dev = __in_dev_get_rtnl(dev); 30661da177e4SLinus Torvalds if (in_dev && (dev->flags & IFF_UP)) { 30671da177e4SLinus Torvalds struct in_ifaddr *ifa; 30681da177e4SLinus Torvalds 30691da177e4SLinus Torvalds int flag = scope; 30701da177e4SLinus Torvalds 30711da177e4SLinus Torvalds for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) { 30721da177e4SLinus Torvalds 30731da177e4SLinus Torvalds addr.s6_addr32[3] = ifa->ifa_local; 30741da177e4SLinus Torvalds 30751da177e4SLinus Torvalds if (ifa->ifa_scope == RT_SCOPE_LINK) 30761da177e4SLinus Torvalds continue; 30771da177e4SLinus Torvalds if (ifa->ifa_scope >= RT_SCOPE_HOST) { 30781da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) 30791da177e4SLinus Torvalds continue; 30801da177e4SLinus Torvalds flag |= IFA_HOST; 30811da177e4SLinus Torvalds } 30821da177e4SLinus Torvalds 3083b5f348e5SIlpo Järvinen add_addr(idev, &addr, plen, flag); 3084f0e2acfaSNicolas Dichtel addrconf_prefix_route(&addr, plen, idev->dev, 0, 3085acb54e3cSDavid Ahern pflags, GFP_ATOMIC); 30861da177e4SLinus Torvalds } 30871da177e4SLinus Torvalds } 30881da177e4SLinus Torvalds } 30891da177e4SLinus Torvalds } 30900be669bbSJoerg Roedel #endif 30911da177e4SLinus Torvalds 30921da177e4SLinus Torvalds static void init_loopback(struct net_device *dev) 30931da177e4SLinus Torvalds { 30941da177e4SLinus Torvalds struct inet6_dev *idev; 30951da177e4SLinus Torvalds 30961da177e4SLinus Torvalds /* ::1 */ 30971da177e4SLinus Torvalds 30981da177e4SLinus Torvalds ASSERT_RTNL(); 30991da177e4SLinus Torvalds 3100e5d08d71SIan Morris idev = ipv6_find_idev(dev); 310163159f29SIan Morris if (!idev) { 310291df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 31031da177e4SLinus Torvalds return; 31041da177e4SLinus Torvalds } 31051da177e4SLinus Torvalds 3106b5f348e5SIlpo Järvinen add_addr(idev, &in6addr_loopback, 128, IFA_HOST); 31071da177e4SLinus Torvalds } 31081da177e4SLinus Torvalds 31092ad3ed59SAlexander Aring void addrconf_add_linklocal(struct inet6_dev *idev, 311064236f3fSHannes Frederic Sowa const struct in6_addr *addr, u32 flags) 31111da177e4SLinus Torvalds { 3112e6464b8cSDavid Ahern struct ifa6_config cfg = { 3113e6464b8cSDavid Ahern .pfx = addr, 3114e6464b8cSDavid Ahern .plen = 64, 3115e6464b8cSDavid Ahern .ifa_flags = flags | IFA_F_PERMANENT, 3116e6464b8cSDavid Ahern .valid_lft = INFINITY_LIFE_TIME, 3117e6464b8cSDavid Ahern .preferred_lft = INFINITY_LIFE_TIME, 3118e6464b8cSDavid Ahern .scope = IFA_LINK 3119e6464b8cSDavid Ahern }; 31201da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 31211da177e4SLinus Torvalds 312295c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 312335e015e1SMatteo Croce if ((dev_net(idev->dev)->ipv6.devconf_all->optimistic_dad || 312435e015e1SMatteo Croce idev->cnf.optimistic_dad) && 3125702beb87SDavid Miller !dev_net(idev->dev)->ipv6.devconf_all->forwarding) 3126e6464b8cSDavid Ahern cfg.ifa_flags |= IFA_F_OPTIMISTIC; 312795c385b4SNeil Horman #endif 312895c385b4SNeil Horman 3129e6464b8cSDavid Ahern ifp = ipv6_add_addr(idev, &cfg, true, NULL); 31301da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 3131acb54e3cSDavid Ahern addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 3132acb54e3cSDavid Ahern 0, 0, GFP_ATOMIC); 3133cf22f9a2SDavid S. Miller addrconf_dad_start(ifp); 31341da177e4SLinus Torvalds in6_ifa_put(ifp); 31351da177e4SLinus Torvalds } 31361da177e4SLinus Torvalds } 31372ad3ed59SAlexander Aring EXPORT_SYMBOL_GPL(addrconf_add_linklocal); 31381da177e4SLinus Torvalds 3139622c81d5SHannes Frederic Sowa static bool ipv6_reserved_interfaceid(struct in6_addr address) 3140622c81d5SHannes Frederic Sowa { 3141622c81d5SHannes Frederic Sowa if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0) 3142622c81d5SHannes Frederic Sowa return true; 3143622c81d5SHannes Frederic Sowa 3144622c81d5SHannes Frederic Sowa if (address.s6_addr32[2] == htonl(0x02005eff) && 3145622c81d5SHannes Frederic Sowa ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000))) 3146622c81d5SHannes Frederic Sowa return true; 3147622c81d5SHannes Frederic Sowa 3148622c81d5SHannes Frederic Sowa if (address.s6_addr32[2] == htonl(0xfdffffff) && 3149622c81d5SHannes Frederic Sowa ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80))) 3150622c81d5SHannes Frederic Sowa return true; 3151622c81d5SHannes Frederic Sowa 3152622c81d5SHannes Frederic Sowa return false; 3153622c81d5SHannes Frederic Sowa } 3154622c81d5SHannes Frederic Sowa 3155622c81d5SHannes Frederic Sowa static int ipv6_generate_stable_address(struct in6_addr *address, 3156622c81d5SHannes Frederic Sowa u8 dad_count, 3157622c81d5SHannes Frederic Sowa const struct inet6_dev *idev) 3158622c81d5SHannes Frederic Sowa { 3159622c81d5SHannes Frederic Sowa static DEFINE_SPINLOCK(lock); 3160622c81d5SHannes Frederic Sowa static __u32 digest[SHA_DIGEST_WORDS]; 3161622c81d5SHannes Frederic Sowa static __u32 workspace[SHA_WORKSPACE_WORDS]; 3162622c81d5SHannes Frederic Sowa 3163622c81d5SHannes Frederic Sowa static union { 3164622c81d5SHannes Frederic Sowa char __data[SHA_MESSAGE_BYTES]; 3165622c81d5SHannes Frederic Sowa struct { 3166622c81d5SHannes Frederic Sowa struct in6_addr secret; 3167ff40217eSHannes Frederic Sowa __be32 prefix[2]; 3168622c81d5SHannes Frederic Sowa unsigned char hwaddr[MAX_ADDR_LEN]; 3169622c81d5SHannes Frederic Sowa u8 dad_count; 3170622c81d5SHannes Frederic Sowa } __packed; 3171622c81d5SHannes Frederic Sowa } data; 3172622c81d5SHannes Frederic Sowa 3173622c81d5SHannes Frederic Sowa struct in6_addr secret; 3174622c81d5SHannes Frederic Sowa struct in6_addr temp; 3175622c81d5SHannes Frederic Sowa struct net *net = dev_net(idev->dev); 3176622c81d5SHannes Frederic Sowa 3177622c81d5SHannes Frederic Sowa BUILD_BUG_ON(sizeof(data.__data) != sizeof(data)); 3178622c81d5SHannes Frederic Sowa 3179622c81d5SHannes Frederic Sowa if (idev->cnf.stable_secret.initialized) 3180622c81d5SHannes Frederic Sowa secret = idev->cnf.stable_secret.secret; 3181622c81d5SHannes Frederic Sowa else if (net->ipv6.devconf_dflt->stable_secret.initialized) 3182622c81d5SHannes Frederic Sowa secret = net->ipv6.devconf_dflt->stable_secret.secret; 3183622c81d5SHannes Frederic Sowa else 3184622c81d5SHannes Frederic Sowa return -1; 3185622c81d5SHannes Frederic Sowa 3186622c81d5SHannes Frederic Sowa retry: 3187622c81d5SHannes Frederic Sowa spin_lock_bh(&lock); 3188622c81d5SHannes Frederic Sowa 3189622c81d5SHannes Frederic Sowa sha_init(digest); 3190622c81d5SHannes Frederic Sowa memset(&data, 0, sizeof(data)); 3191622c81d5SHannes Frederic Sowa memset(workspace, 0, sizeof(workspace)); 3192622c81d5SHannes Frederic Sowa memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len); 3193ff40217eSHannes Frederic Sowa data.prefix[0] = address->s6_addr32[0]; 3194ff40217eSHannes Frederic Sowa data.prefix[1] = address->s6_addr32[1]; 3195622c81d5SHannes Frederic Sowa data.secret = secret; 3196622c81d5SHannes Frederic Sowa data.dad_count = dad_count; 3197622c81d5SHannes Frederic Sowa 3198622c81d5SHannes Frederic Sowa sha_transform(digest, data.__data, workspace); 3199622c81d5SHannes Frederic Sowa 3200622c81d5SHannes Frederic Sowa temp = *address; 3201ff40217eSHannes Frederic Sowa temp.s6_addr32[2] = (__force __be32)digest[0]; 3202ff40217eSHannes Frederic Sowa temp.s6_addr32[3] = (__force __be32)digest[1]; 3203622c81d5SHannes Frederic Sowa 3204622c81d5SHannes Frederic Sowa spin_unlock_bh(&lock); 3205622c81d5SHannes Frederic Sowa 3206622c81d5SHannes Frederic Sowa if (ipv6_reserved_interfaceid(temp)) { 3207622c81d5SHannes Frederic Sowa dad_count++; 32081855b7c3SHannes Frederic Sowa if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries) 3209622c81d5SHannes Frederic Sowa return -1; 3210622c81d5SHannes Frederic Sowa goto retry; 3211622c81d5SHannes Frederic Sowa } 3212622c81d5SHannes Frederic Sowa 3213622c81d5SHannes Frederic Sowa *address = temp; 3214622c81d5SHannes Frederic Sowa return 0; 3215622c81d5SHannes Frederic Sowa } 3216622c81d5SHannes Frederic Sowa 3217cc9da6ccSBjørn Mork static void ipv6_gen_mode_random_init(struct inet6_dev *idev) 3218cc9da6ccSBjørn Mork { 3219cc9da6ccSBjørn Mork struct ipv6_stable_secret *s = &idev->cnf.stable_secret; 3220cc9da6ccSBjørn Mork 3221cc9da6ccSBjørn Mork if (s->initialized) 3222cc9da6ccSBjørn Mork return; 3223cc9da6ccSBjørn Mork s = &idev->cnf.stable_secret; 3224cc9da6ccSBjørn Mork get_random_bytes(&s->secret, sizeof(s->secret)); 3225cc9da6ccSBjørn Mork s->initialized = true; 3226cc9da6ccSBjørn Mork } 3227cc9da6ccSBjørn Mork 3228bc91b0f0SJiri Pirko static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route) 3229bc91b0f0SJiri Pirko { 3230bc91b0f0SJiri Pirko struct in6_addr addr; 3231bc91b0f0SJiri Pirko 3232ca254490SDavid Ahern /* no link local addresses on L3 master devices */ 3233ca254490SDavid Ahern if (netif_is_l3_master(idev->dev)) 3234ca254490SDavid Ahern return; 3235ca254490SDavid Ahern 3236bc91b0f0SJiri Pirko ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0); 3237622c81d5SHannes Frederic Sowa 3238d35a00b8SFelix Jia switch (idev->cnf.addr_gen_mode) { 3239cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_RANDOM: 3240cc9da6ccSBjørn Mork ipv6_gen_mode_random_init(idev); 3241cc9da6ccSBjørn Mork /* fallthrough */ 3242cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_STABLE_PRIVACY: 3243622c81d5SHannes Frederic Sowa if (!ipv6_generate_stable_address(&addr, 0, idev)) 324464236f3fSHannes Frederic Sowa addrconf_add_linklocal(idev, &addr, 324564236f3fSHannes Frederic Sowa IFA_F_STABLE_PRIVACY); 3246622c81d5SHannes Frederic Sowa else if (prefix_route) 3247acb54e3cSDavid Ahern addrconf_prefix_route(&addr, 64, idev->dev, 3248acb54e3cSDavid Ahern 0, 0, GFP_KERNEL); 3249cc9da6ccSBjørn Mork break; 3250cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_EUI64: 3251bc91b0f0SJiri Pirko /* addrconf_add_linklocal also adds a prefix_route and we 3252bc91b0f0SJiri Pirko * only need to care about prefix routes if ipv6_generate_eui64 3253bc91b0f0SJiri Pirko * couldn't generate one. 3254bc91b0f0SJiri Pirko */ 3255bc91b0f0SJiri Pirko if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0) 325664236f3fSHannes Frederic Sowa addrconf_add_linklocal(idev, &addr, 0); 3257bc91b0f0SJiri Pirko else if (prefix_route) 3258acb54e3cSDavid Ahern addrconf_prefix_route(&addr, 64, idev->dev, 325927b10608SDavid Ahern 0, 0, GFP_KERNEL); 3260cc9da6ccSBjørn Mork break; 3261cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_NONE: 3262cc9da6ccSBjørn Mork default: 3263cc9da6ccSBjørn Mork /* will not add any link local address */ 3264cc9da6ccSBjørn Mork break; 3265bc91b0f0SJiri Pirko } 3266bc91b0f0SJiri Pirko } 3267bc91b0f0SJiri Pirko 32681da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev) 32691da177e4SLinus Torvalds { 32701da177e4SLinus Torvalds struct inet6_dev *idev; 32711da177e4SLinus Torvalds 32721da177e4SLinus Torvalds ASSERT_RTNL(); 32731da177e4SLinus Torvalds 327474235a25SHerbert Xu if ((dev->type != ARPHRD_ETHER) && 327574235a25SHerbert Xu (dev->type != ARPHRD_FDDI) && 327674235a25SHerbert Xu (dev->type != ARPHRD_ARCNET) && 327706a4c1c5Salex.bluesman.smirnov@gmail.com (dev->type != ARPHRD_INFINIBAND) && 3278e837735eSNicolas Dichtel (dev->type != ARPHRD_IEEE1394) && 3279e74bccb8SJukka Rissanen (dev->type != ARPHRD_TUNNEL6) && 3280cc9da6ccSBjørn Mork (dev->type != ARPHRD_6LOWPAN) && 328145ce0fd1SFelix Jia (dev->type != ARPHRD_IP6GRE) && 328245ce0fd1SFelix Jia (dev->type != ARPHRD_IPGRE) && 328345ce0fd1SFelix Jia (dev->type != ARPHRD_TUNNEL) && 3284cc9da6ccSBjørn Mork (dev->type != ARPHRD_NONE)) { 328574235a25SHerbert Xu /* Alas, we support only Ethernet autoconfiguration. */ 328674235a25SHerbert Xu return; 328774235a25SHerbert Xu } 328874235a25SHerbert Xu 32891da177e4SLinus Torvalds idev = addrconf_add_dev(dev); 329064e724f6SBrian Haley if (IS_ERR(idev)) 32911da177e4SLinus Torvalds return; 32921da177e4SLinus Torvalds 3293cc9da6ccSBjørn Mork /* this device type has no EUI support */ 3294cc9da6ccSBjørn Mork if (dev->type == ARPHRD_NONE && 3295d35a00b8SFelix Jia idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64) 3296d35a00b8SFelix Jia idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM; 3297cc9da6ccSBjørn Mork 3298bc91b0f0SJiri Pirko addrconf_addr_gen(idev, false); 32991da177e4SLinus Torvalds } 33001da177e4SLinus Torvalds 330107a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 33021da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev) 33031da177e4SLinus Torvalds { 33041da177e4SLinus Torvalds struct inet6_dev *idev; 33051da177e4SLinus Torvalds 33061da177e4SLinus Torvalds ASSERT_RTNL(); 33071da177e4SLinus Torvalds 33081da177e4SLinus Torvalds /* 33091da177e4SLinus Torvalds * Configure the tunnel with one of our IPv4 33101da177e4SLinus Torvalds * addresses... we should configure all of 33111da177e4SLinus Torvalds * our v4 addrs in the tunnel 33121da177e4SLinus Torvalds */ 33131da177e4SLinus Torvalds 3314e5d08d71SIan Morris idev = ipv6_find_idev(dev); 331563159f29SIan Morris if (!idev) { 331691df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 33171da177e4SLinus Torvalds return; 33181da177e4SLinus Torvalds } 33191da177e4SLinus Torvalds 3320c7dc89c0SFred L. Templin if (dev->priv_flags & IFF_ISATAP) { 3321bc91b0f0SJiri Pirko addrconf_addr_gen(idev, false); 3322c7dc89c0SFred L. Templin return; 3323c7dc89c0SFred L. Templin } 3324c7dc89c0SFred L. Templin 33251da177e4SLinus Torvalds sit_add_v4_addrs(idev); 33261da177e4SLinus Torvalds 332762b54dd9SNicolas Dichtel if (dev->flags&IFF_POINTOPOINT) 33281da177e4SLinus Torvalds addrconf_add_mroute(dev); 33291da177e4SLinus Torvalds } 33300be669bbSJoerg Roedel #endif 33311da177e4SLinus Torvalds 333207a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE) 3333aee80b54Sstephen hemminger static void addrconf_gre_config(struct net_device *dev) 3334aee80b54Sstephen hemminger { 3335aee80b54Sstephen hemminger struct inet6_dev *idev; 3336aee80b54Sstephen hemminger 3337aee80b54Sstephen hemminger ASSERT_RTNL(); 3338aee80b54Sstephen hemminger 3339e5d08d71SIan Morris idev = ipv6_find_idev(dev); 334063159f29SIan Morris if (!idev) { 334191df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 3342aee80b54Sstephen hemminger return; 3343aee80b54Sstephen hemminger } 3344aee80b54Sstephen hemminger 3345bc91b0f0SJiri Pirko addrconf_addr_gen(idev, true); 3346d9e4ce65SHannes Frederic Sowa if (dev->flags & IFF_POINTOPOINT) 3347d9e4ce65SHannes Frederic Sowa addrconf_add_mroute(dev); 3348aee80b54Sstephen hemminger } 3349aee80b54Sstephen hemminger #endif 3350aee80b54Sstephen hemminger 3351afb1d4b5SDavid Ahern static int fixup_permanent_addr(struct net *net, 3352afb1d4b5SDavid Ahern struct inet6_dev *idev, 3353f1705ec1SDavid Ahern struct inet6_ifaddr *ifp) 3354f1705ec1SDavid Ahern { 335593c2fb25SDavid Ahern /* !fib6_node means the host route was removed from the 33568048ced9SDavid Ahern * FIB, for example, if 'lo' device is taken down. In that 33578048ced9SDavid Ahern * case regenerate the host route. 33588048ced9SDavid Ahern */ 335993c2fb25SDavid Ahern if (!ifp->rt || !ifp->rt->fib6_node) { 3360360a9887SDavid Ahern struct fib6_info *f6i, *prev; 3361f1705ec1SDavid Ahern 3362360a9887SDavid Ahern f6i = addrconf_f6i_alloc(net, idev, &ifp->addr, false, 3363acb54e3cSDavid Ahern GFP_ATOMIC); 3364360a9887SDavid Ahern if (IS_ERR(f6i)) 3365360a9887SDavid Ahern return PTR_ERR(f6i); 3366f1705ec1SDavid Ahern 33678048ced9SDavid Ahern /* ifp->rt can be accessed outside of rtnl */ 33688048ced9SDavid Ahern spin_lock(&ifp->lock); 33698048ced9SDavid Ahern prev = ifp->rt; 3370360a9887SDavid Ahern ifp->rt = f6i; 33718048ced9SDavid Ahern spin_unlock(&ifp->lock); 33728048ced9SDavid Ahern 337393531c67SDavid Ahern fib6_info_release(prev); 3374f1705ec1SDavid Ahern } 3375f1705ec1SDavid Ahern 3376f1705ec1SDavid Ahern if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) { 3377f1705ec1SDavid Ahern addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 3378acb54e3cSDavid Ahern idev->dev, 0, 0, GFP_ATOMIC); 3379f1705ec1SDavid Ahern } 3380f1705ec1SDavid Ahern 33816d717134SDavid Ahern if (ifp->state == INET6_IFADDR_STATE_PREDAD) 3382f1705ec1SDavid Ahern addrconf_dad_start(ifp); 3383f1705ec1SDavid Ahern 3384f1705ec1SDavid Ahern return 0; 3385f1705ec1SDavid Ahern } 3386f1705ec1SDavid Ahern 3387afb1d4b5SDavid Ahern static void addrconf_permanent_addr(struct net *net, struct net_device *dev) 3388f1705ec1SDavid Ahern { 3389f1705ec1SDavid Ahern struct inet6_ifaddr *ifp, *tmp; 3390f1705ec1SDavid Ahern struct inet6_dev *idev; 3391f1705ec1SDavid Ahern 3392f1705ec1SDavid Ahern idev = __in6_dev_get(dev); 3393f1705ec1SDavid Ahern if (!idev) 3394f1705ec1SDavid Ahern return; 3395f1705ec1SDavid Ahern 3396f1705ec1SDavid Ahern write_lock_bh(&idev->lock); 3397f1705ec1SDavid Ahern 3398f1705ec1SDavid Ahern list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) { 3399f1705ec1SDavid Ahern if ((ifp->flags & IFA_F_PERMANENT) && 3400afb1d4b5SDavid Ahern fixup_permanent_addr(net, idev, ifp) < 0) { 3401f1705ec1SDavid Ahern write_unlock_bh(&idev->lock); 3402e669b869SEric Dumazet in6_ifa_hold(ifp); 3403f1705ec1SDavid Ahern ipv6_del_addr(ifp); 3404f1705ec1SDavid Ahern write_lock_bh(&idev->lock); 3405f1705ec1SDavid Ahern 3406f1705ec1SDavid Ahern net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n", 3407f1705ec1SDavid Ahern idev->dev->name, &ifp->addr); 3408f1705ec1SDavid Ahern } 3409f1705ec1SDavid Ahern } 3410f1705ec1SDavid Ahern 3411f1705ec1SDavid Ahern write_unlock_bh(&idev->lock); 3412f1705ec1SDavid Ahern } 3413f1705ec1SDavid Ahern 34141da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event, 3415351638e7SJiri Pirko void *ptr) 34161da177e4SLinus Torvalds { 3417351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 34184f7f34eaSDavid Ahern struct netdev_notifier_changeupper_info *info; 3419748e2d93SEric Dumazet struct inet6_dev *idev = __in6_dev_get(dev); 342060abc0beSWANG Cong struct net *net = dev_net(dev); 3421c5e33bddSYOSHIFUJI Hideaki int run_pending = 0; 3422b217d616SHerbert Xu int err; 34231da177e4SLinus Torvalds 34241da177e4SLinus Torvalds switch (event) { 342545ba9dd2SYOSHIFUJI Hideaki case NETDEV_REGISTER: 342674235a25SHerbert Xu if (!idev && dev->mtu >= IPV6_MIN_MTU) { 342745ba9dd2SYOSHIFUJI Hideaki idev = ipv6_add_dev(dev); 3428a317a2f1SWANG Cong if (IS_ERR(idev)) 3429a317a2f1SWANG Cong return notifier_from_errno(PTR_ERR(idev)); 343045ba9dd2SYOSHIFUJI Hideaki } 343145ba9dd2SYOSHIFUJI Hideaki break; 3432bcdd553fSStephen Hemminger 3433b7b0b1d2SAlexander Duyck case NETDEV_CHANGEMTU: 3434b7b0b1d2SAlexander Duyck /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */ 3435b7b0b1d2SAlexander Duyck if (dev->mtu < IPV6_MIN_MTU) { 343660abc0beSWANG Cong addrconf_ifdown(dev, dev != net->loopback_dev); 3437b7b0b1d2SAlexander Duyck break; 3438b7b0b1d2SAlexander Duyck } 3439b7b0b1d2SAlexander Duyck 3440b7b0b1d2SAlexander Duyck if (idev) { 3441b7b0b1d2SAlexander Duyck rt6_mtu_change(dev, dev->mtu); 3442b7b0b1d2SAlexander Duyck idev->cnf.mtu6 = dev->mtu; 3443b7b0b1d2SAlexander Duyck break; 3444b7b0b1d2SAlexander Duyck } 3445b7b0b1d2SAlexander Duyck 3446b7b0b1d2SAlexander Duyck /* allocate new idev */ 3447b7b0b1d2SAlexander Duyck idev = ipv6_add_dev(dev); 3448b7b0b1d2SAlexander Duyck if (IS_ERR(idev)) 3449b7b0b1d2SAlexander Duyck break; 3450b7b0b1d2SAlexander Duyck 3451b7b0b1d2SAlexander Duyck /* device is still not ready */ 3452b7b0b1d2SAlexander Duyck if (!(idev->if_flags & IF_READY)) 3453b7b0b1d2SAlexander Duyck break; 3454b7b0b1d2SAlexander Duyck 3455b7b0b1d2SAlexander Duyck run_pending = 1; 3456b7b0b1d2SAlexander Duyck 3457b7b0b1d2SAlexander Duyck /* fall through */ 3458b7b0b1d2SAlexander Duyck 34591da177e4SLinus Torvalds case NETDEV_UP: 34603c21edbdSYOSHIFUJI Hideaki case NETDEV_CHANGE: 3461c2edacf8SJay Vosburgh if (dev->flags & IFF_SLAVE) 3462c2edacf8SJay Vosburgh break; 3463c2edacf8SJay Vosburgh 34643ce62a84SWANG Cong if (idev && idev->cnf.disable_ipv6) 34653ce62a84SWANG Cong break; 34663ce62a84SWANG Cong 34673c21edbdSYOSHIFUJI Hideaki if (event == NETDEV_UP) { 346838bd10c4SDavid Ahern /* restore routes for permanent addresses */ 3469afb1d4b5SDavid Ahern addrconf_permanent_addr(net, dev); 347038bd10c4SDavid Ahern 34711f372c7bSMike Manning if (!addrconf_link_ready(dev)) { 34723c21edbdSYOSHIFUJI Hideaki /* device is not ready yet. */ 3473f3213831SJoe Perches pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n", 34743c21edbdSYOSHIFUJI Hideaki dev->name); 34753c21edbdSYOSHIFUJI Hideaki break; 34763c21edbdSYOSHIFUJI Hideaki } 347799081049SKristian Slavov 3478d31c7b8fSEvgeniy Polyakov if (!idev && dev->mtu >= IPV6_MIN_MTU) 3479d31c7b8fSEvgeniy Polyakov idev = ipv6_add_dev(dev); 3480d31c7b8fSEvgeniy Polyakov 3481a317a2f1SWANG Cong if (!IS_ERR_OR_NULL(idev)) { 348299081049SKristian Slavov idev->if_flags |= IF_READY; 3483e3ec6cfcSBenjamin Thery run_pending = 1; 3484e3ec6cfcSBenjamin Thery } 3485b7b0b1d2SAlexander Duyck } else if (event == NETDEV_CHANGE) { 34861f372c7bSMike Manning if (!addrconf_link_ready(dev)) { 34873c21edbdSYOSHIFUJI Hideaki /* device is still not ready. */ 348827c6fa73SIdo Schimmel rt6_sync_down_dev(dev, event); 34893c21edbdSYOSHIFUJI Hideaki break; 34903c21edbdSYOSHIFUJI Hideaki } 34913c21edbdSYOSHIFUJI Hideaki 34923c21edbdSYOSHIFUJI Hideaki if (idev) { 3493a088d1d7SLinus Lüssing if (idev->if_flags & IF_READY) { 3494a088d1d7SLinus Lüssing /* device is already configured - 3495a088d1d7SLinus Lüssing * but resend MLD reports, we might 3496a088d1d7SLinus Lüssing * have roamed and need to update 3497a088d1d7SLinus Lüssing * multicast snooping switches 3498a088d1d7SLinus Lüssing */ 3499a088d1d7SLinus Lüssing ipv6_mc_up(idev); 350027c6fa73SIdo Schimmel rt6_sync_up(dev, RTNH_F_LINKDOWN); 35013c21edbdSYOSHIFUJI Hideaki break; 3502a088d1d7SLinus Lüssing } 35033c21edbdSYOSHIFUJI Hideaki idev->if_flags |= IF_READY; 35043c21edbdSYOSHIFUJI Hideaki } 35053c21edbdSYOSHIFUJI Hideaki 3506f3213831SJoe Perches pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n", 35073c21edbdSYOSHIFUJI Hideaki dev->name); 35083c21edbdSYOSHIFUJI Hideaki 3509c5e33bddSYOSHIFUJI Hideaki run_pending = 1; 35103c21edbdSYOSHIFUJI Hideaki } 35113c21edbdSYOSHIFUJI Hideaki 35121da177e4SLinus Torvalds switch (dev->type) { 351307a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 35141da177e4SLinus Torvalds case ARPHRD_SIT: 35151da177e4SLinus Torvalds addrconf_sit_config(dev); 35161da177e4SLinus Torvalds break; 35170be669bbSJoerg Roedel #endif 351807a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE) 3519aee80b54Sstephen hemminger case ARPHRD_IPGRE: 3520aee80b54Sstephen hemminger addrconf_gre_config(dev); 3521aee80b54Sstephen hemminger break; 3522aee80b54Sstephen hemminger #endif 35231da177e4SLinus Torvalds case ARPHRD_LOOPBACK: 35241da177e4SLinus Torvalds init_loopback(dev); 35251da177e4SLinus Torvalds break; 35261da177e4SLinus Torvalds 35271da177e4SLinus Torvalds default: 35281da177e4SLinus Torvalds addrconf_dev_config(dev); 35291da177e4SLinus Torvalds break; 35303ff50b79SStephen Hemminger } 3531bcdd553fSStephen Hemminger 3532a317a2f1SWANG Cong if (!IS_ERR_OR_NULL(idev)) { 3533c5e33bddSYOSHIFUJI Hideaki if (run_pending) 3534c5e33bddSYOSHIFUJI Hideaki addrconf_dad_run(idev); 3535c5e33bddSYOSHIFUJI Hideaki 35362127d95aSIdo Schimmel /* Device has an address by now */ 35372127d95aSIdo Schimmel rt6_sync_up(dev, RTNH_F_DEAD); 35382127d95aSIdo Schimmel 3539bcdd553fSStephen Hemminger /* 3540bcdd553fSStephen Hemminger * If the MTU changed during the interface down, 3541bcdd553fSStephen Hemminger * when the interface up, the changed MTU must be 3542bcdd553fSStephen Hemminger * reflected in the idev as well as routers. 35431da177e4SLinus Torvalds */ 3544bcdd553fSStephen Hemminger if (idev->cnf.mtu6 != dev->mtu && 3545bcdd553fSStephen Hemminger dev->mtu >= IPV6_MIN_MTU) { 35461da177e4SLinus Torvalds rt6_mtu_change(dev, dev->mtu); 35471da177e4SLinus Torvalds idev->cnf.mtu6 = dev->mtu; 35481da177e4SLinus Torvalds } 35491da177e4SLinus Torvalds idev->tstamp = jiffies; 35501da177e4SLinus Torvalds inet6_ifinfo_notify(RTM_NEWLINK, idev); 3551bcdd553fSStephen Hemminger 3552bcdd553fSStephen Hemminger /* 3553bcdd553fSStephen Hemminger * If the changed mtu during down is lower than 3554bcdd553fSStephen Hemminger * IPV6_MIN_MTU stop IPv6 on this interface. 35551da177e4SLinus Torvalds */ 35561da177e4SLinus Torvalds if (dev->mtu < IPV6_MIN_MTU) 355760abc0beSWANG Cong addrconf_ifdown(dev, dev != net->loopback_dev); 35581da177e4SLinus Torvalds } 35591da177e4SLinus Torvalds break; 35601da177e4SLinus Torvalds 35611da177e4SLinus Torvalds case NETDEV_DOWN: 35621da177e4SLinus Torvalds case NETDEV_UNREGISTER: 35631da177e4SLinus Torvalds /* 35641da177e4SLinus Torvalds * Remove all addresses from this interface. 35651da177e4SLinus Torvalds */ 35661da177e4SLinus Torvalds addrconf_ifdown(dev, event != NETDEV_DOWN); 35671da177e4SLinus Torvalds break; 35683c21edbdSYOSHIFUJI Hideaki 35691da177e4SLinus Torvalds case NETDEV_CHANGENAME: 35701da177e4SLinus Torvalds if (idev) { 35715632c515SStephen Hemminger snmp6_unregister_dev(idev); 3572408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 3573a317a2f1SWANG Cong err = addrconf_sysctl_register(idev); 3574b217d616SHerbert Xu if (err) 3575b217d616SHerbert Xu return notifier_from_errno(err); 3576a317a2f1SWANG Cong err = snmp6_register_dev(idev); 3577a317a2f1SWANG Cong if (err) { 3578a317a2f1SWANG Cong addrconf_sysctl_unregister(idev); 3579a317a2f1SWANG Cong return notifier_from_errno(err); 3580a317a2f1SWANG Cong } 35815632c515SStephen Hemminger } 35821da177e4SLinus Torvalds break; 3583bcdd553fSStephen Hemminger 358493d9b7d7SJiri Pirko case NETDEV_PRE_TYPE_CHANGE: 358593d9b7d7SJiri Pirko case NETDEV_POST_TYPE_CHANGE: 35863ef0952cSAndrew Lunn if (idev) 358793d9b7d7SJiri Pirko addrconf_type_change(dev, event); 358875c78500SMoni Shoua break; 35894f7f34eaSDavid Ahern 35904f7f34eaSDavid Ahern case NETDEV_CHANGEUPPER: 35914f7f34eaSDavid Ahern info = ptr; 35924f7f34eaSDavid Ahern 35934f7f34eaSDavid Ahern /* flush all routes if dev is linked to or unlinked from 35944f7f34eaSDavid Ahern * an L3 master device (e.g., VRF) 35954f7f34eaSDavid Ahern */ 35964f7f34eaSDavid Ahern if (info->upper_dev && netif_is_l3_master(info->upper_dev)) 35974f7f34eaSDavid Ahern addrconf_ifdown(dev, 0); 35983ff50b79SStephen Hemminger } 35991da177e4SLinus Torvalds 36001da177e4SLinus Torvalds return NOTIFY_OK; 36011da177e4SLinus Torvalds } 36021da177e4SLinus Torvalds 36031da177e4SLinus Torvalds /* 36041da177e4SLinus Torvalds * addrconf module should be notified of a device going up 36051da177e4SLinus Torvalds */ 36061da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = { 36071da177e4SLinus Torvalds .notifier_call = addrconf_notify, 3608242d3a49SWANG Cong .priority = ADDRCONF_NOTIFY_PRIORITY, 36091da177e4SLinus Torvalds }; 36101da177e4SLinus Torvalds 361193d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, unsigned long event) 361275c78500SMoni Shoua { 361375c78500SMoni Shoua struct inet6_dev *idev; 361475c78500SMoni Shoua ASSERT_RTNL(); 361575c78500SMoni Shoua 361675c78500SMoni Shoua idev = __in6_dev_get(dev); 361775c78500SMoni Shoua 361893d9b7d7SJiri Pirko if (event == NETDEV_POST_TYPE_CHANGE) 361975c78500SMoni Shoua ipv6_mc_remap(idev); 362093d9b7d7SJiri Pirko else if (event == NETDEV_PRE_TYPE_CHANGE) 362175c78500SMoni Shoua ipv6_mc_unmap(idev); 362275c78500SMoni Shoua } 362375c78500SMoni Shoua 362470af921dSDavid Ahern static bool addr_is_local(const struct in6_addr *addr) 362570af921dSDavid Ahern { 362670af921dSDavid Ahern return ipv6_addr_type(addr) & 362770af921dSDavid Ahern (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK); 362870af921dSDavid Ahern } 362970af921dSDavid Ahern 36301da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how) 36311da177e4SLinus Torvalds { 36324c981e28SIdo Schimmel unsigned long event = how ? NETDEV_UNREGISTER : NETDEV_DOWN; 3633c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 3634502a2ffdSstephen hemminger struct inet6_dev *idev; 3635f1705ec1SDavid Ahern struct inet6_ifaddr *ifa, *tmp; 36360aef78aaSIvan Vecera bool keep_addr = false; 363773a8bd74SDavid S. Miller int state, i; 36381da177e4SLinus Torvalds 36391da177e4SLinus Torvalds ASSERT_RTNL(); 36401da177e4SLinus Torvalds 36414c981e28SIdo Schimmel rt6_disable_ip(dev, event); 36421da177e4SLinus Torvalds 36431da177e4SLinus Torvalds idev = __in6_dev_get(dev); 364463159f29SIan Morris if (!idev) 36451da177e4SLinus Torvalds return -ENODEV; 36461da177e4SLinus Torvalds 3647bcdd553fSStephen Hemminger /* 3648bcdd553fSStephen Hemminger * Step 1: remove reference to ipv6 device from parent device. 3649bcdd553fSStephen Hemminger * Do not dev_put! 36501da177e4SLinus Torvalds */ 3651439e2385SDenis V. Lunev if (how) { 36521da177e4SLinus Torvalds idev->dead = 1; 36538814c4b5SYOSHIFUJI Hideaki 36548814c4b5SYOSHIFUJI Hideaki /* protected by rtnl_lock */ 3655a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(dev->ip6_ptr, NULL); 36561da177e4SLinus Torvalds 36571da177e4SLinus Torvalds /* Step 1.5: remove snmp6 entry */ 36581da177e4SLinus Torvalds snmp6_unregister_dev(idev); 36591da177e4SLinus Torvalds 36601da177e4SLinus Torvalds } 36611da177e4SLinus Torvalds 3662f1705ec1SDavid Ahern /* combine the user config with event to determine if permanent 3663f1705ec1SDavid Ahern * addresses are to be removed from address hash table 3664f1705ec1SDavid Ahern */ 36650aef78aaSIvan Vecera if (!how && !idev->cnf.disable_ipv6) { 36660aef78aaSIvan Vecera /* aggregate the system setting and interface setting */ 36670aef78aaSIvan Vecera int _keep_addr = net->ipv6.devconf_all->keep_addr_on_down; 36680aef78aaSIvan Vecera 36690aef78aaSIvan Vecera if (!_keep_addr) 36700aef78aaSIvan Vecera _keep_addr = idev->cnf.keep_addr_on_down; 36710aef78aaSIvan Vecera 36720aef78aaSIvan Vecera keep_addr = (_keep_addr > 0); 36730aef78aaSIvan Vecera } 3674f1705ec1SDavid Ahern 367573a8bd74SDavid S. Miller /* Step 2: clear hash table */ 367673a8bd74SDavid S. Miller for (i = 0; i < IN6_ADDR_HSIZE; i++) { 367773a8bd74SDavid S. Miller struct hlist_head *h = &inet6_addr_lst[i]; 367873a8bd74SDavid S. Miller 367973a8bd74SDavid S. Miller spin_lock_bh(&addrconf_hash_lock); 368073a8bd74SDavid S. Miller restart: 3681b67bfe0dSSasha Levin hlist_for_each_entry_rcu(ifa, h, addr_lst) { 368273a8bd74SDavid S. Miller if (ifa->idev == idev) { 3683c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifa); 3684f1705ec1SDavid Ahern /* combined flag + permanent flag decide if 3685f1705ec1SDavid Ahern * address is retained on a down event 3686f1705ec1SDavid Ahern */ 3687f1705ec1SDavid Ahern if (!keep_addr || 368870af921dSDavid Ahern !(ifa->flags & IFA_F_PERMANENT) || 368970af921dSDavid Ahern addr_is_local(&ifa->addr)) { 3690f1705ec1SDavid Ahern hlist_del_init_rcu(&ifa->addr_lst); 369173a8bd74SDavid S. Miller goto restart; 369273a8bd74SDavid S. Miller } 369373a8bd74SDavid S. Miller } 3694f1705ec1SDavid Ahern } 369573a8bd74SDavid S. Miller spin_unlock_bh(&addrconf_hash_lock); 369673a8bd74SDavid S. Miller } 369773a8bd74SDavid S. Miller 36981da177e4SLinus Torvalds write_lock_bh(&idev->lock); 36991da177e4SLinus Torvalds 3700b7b1bfceSHannes Frederic Sowa addrconf_del_rs_timer(idev); 3701b7b1bfceSHannes Frederic Sowa 3702bcdd553fSStephen Hemminger /* Step 2: clear flags for stateless addrconf */ 3703439e2385SDenis V. Lunev if (!how) 37043c21edbdSYOSHIFUJI Hideaki idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY); 37051da177e4SLinus Torvalds 3706bcdd553fSStephen Hemminger /* Step 3: clear tempaddr list */ 3707372e6c8fSstephen hemminger while (!list_empty(&idev->tempaddr_list)) { 3708372e6c8fSstephen hemminger ifa = list_first_entry(&idev->tempaddr_list, 3709372e6c8fSstephen hemminger struct inet6_ifaddr, tmp_list); 3710372e6c8fSstephen hemminger list_del(&ifa->tmp_list); 37111da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 37121da177e4SLinus Torvalds spin_lock_bh(&ifa->lock); 37131da177e4SLinus Torvalds 37141da177e4SLinus Torvalds if (ifa->ifpub) { 37151da177e4SLinus Torvalds in6_ifa_put(ifa->ifpub); 37161da177e4SLinus Torvalds ifa->ifpub = NULL; 37171da177e4SLinus Torvalds } 37181da177e4SLinus Torvalds spin_unlock_bh(&ifa->lock); 37191da177e4SLinus Torvalds in6_ifa_put(ifa); 37201da177e4SLinus Torvalds write_lock_bh(&idev->lock); 37211da177e4SLinus Torvalds } 37228f37ada5Sstephen hemminger 3723f1705ec1SDavid Ahern list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) { 37248d1c802bSDavid Ahern struct fib6_info *rt = NULL; 3725a2d6cbb0SRabin Vincent bool keep; 372638bd10c4SDavid Ahern 3727c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifa); 372884e8b803Sstephen hemminger 3729a2d6cbb0SRabin Vincent keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) && 3730a2d6cbb0SRabin Vincent !addr_is_local(&ifa->addr); 3731a2d6cbb0SRabin Vincent 37324c5ff6a6SHerbert Xu write_unlock_bh(&idev->lock); 37338e8e676dSHannes Frederic Sowa spin_lock_bh(&ifa->lock); 3734f1705ec1SDavid Ahern 3735a2d6cbb0SRabin Vincent if (keep) { 3736f1705ec1SDavid Ahern /* set state to skip the notifier below */ 3737f1705ec1SDavid Ahern state = INET6_IFADDR_STATE_DEAD; 37386d717134SDavid Ahern ifa->state = INET6_IFADDR_STATE_PREDAD; 3739f1705ec1SDavid Ahern if (!(ifa->flags & IFA_F_NODAD)) 3740f1705ec1SDavid Ahern ifa->flags |= IFA_F_TENTATIVE; 374138bd10c4SDavid Ahern 374238bd10c4SDavid Ahern rt = ifa->rt; 374338bd10c4SDavid Ahern ifa->rt = NULL; 3744f1705ec1SDavid Ahern } else { 37454c5ff6a6SHerbert Xu state = ifa->state; 37464c5ff6a6SHerbert Xu ifa->state = INET6_IFADDR_STATE_DEAD; 3747f1705ec1SDavid Ahern } 3748f1705ec1SDavid Ahern 37498e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifa->lock); 37504c5ff6a6SHerbert Xu 375138bd10c4SDavid Ahern if (rt) 3752afb1d4b5SDavid Ahern ip6_del_rt(net, rt); 375338bd10c4SDavid Ahern 37549d82ca98SJohn Fastabend if (state != INET6_IFADDR_STATE_DEAD) { 37551da177e4SLinus Torvalds __ipv6_ifa_notify(RTM_DELADDR, ifa); 3756f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_DOWN, ifa); 3757ea06f717SMike Manning } else { 3758ea06f717SMike Manning if (idev->cnf.forwarding) 3759ea06f717SMike Manning addrconf_leave_anycast(ifa); 3760ea06f717SMike Manning addrconf_leave_solict(ifa->idev, &ifa->addr); 37612de79570SLorenzo Colitti } 376273a8bd74SDavid S. Miller 37631da177e4SLinus Torvalds write_lock_bh(&idev->lock); 37648ef802aaSEric Dumazet if (!keep) { 37658ef802aaSEric Dumazet list_del_rcu(&ifa->if_list); 37668ef802aaSEric Dumazet in6_ifa_put(ifa); 37678ef802aaSEric Dumazet } 37681da177e4SLinus Torvalds } 37698f37ada5Sstephen hemminger 37701da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 37711da177e4SLinus Torvalds 3772381f4dcaSSabrina Dubroca /* Step 5: Discard anycast and multicast list */ 3773381f4dcaSSabrina Dubroca if (how) { 3774381f4dcaSSabrina Dubroca ipv6_ac_destroy_dev(idev); 37751da177e4SLinus Torvalds ipv6_mc_destroy_dev(idev); 3776381f4dcaSSabrina Dubroca } else { 37771da177e4SLinus Torvalds ipv6_mc_down(idev); 3778381f4dcaSSabrina Dubroca } 37791da177e4SLinus Torvalds 37801da177e4SLinus Torvalds idev->tstamp = jiffies; 37811da177e4SLinus Torvalds 3782bcdd553fSStephen Hemminger /* Last: Shot the device (if unregistered) */ 3783439e2385SDenis V. Lunev if (how) { 3784408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 37851da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, idev->nd_parms); 37861da177e4SLinus Torvalds neigh_ifdown(&nd_tbl, dev); 37871da177e4SLinus Torvalds in6_dev_put(idev); 37881da177e4SLinus Torvalds } 37891da177e4SLinus Torvalds return 0; 37901da177e4SLinus Torvalds } 37911da177e4SLinus Torvalds 3792e99e88a9SKees Cook static void addrconf_rs_timer(struct timer_list *t) 37931da177e4SLinus Torvalds { 3794e99e88a9SKees Cook struct inet6_dev *idev = from_timer(idev, t, rs_timer); 3795caf92bc4SCong Wang struct net_device *dev = idev->dev; 3796b7b1bfceSHannes Frederic Sowa struct in6_addr lladdr; 37971da177e4SLinus Torvalds 3798b7b1bfceSHannes Frederic Sowa write_lock(&idev->lock); 37995b2a1953Sstephen hemminger if (idev->dead || !(idev->if_flags & IF_READY)) 38001da177e4SLinus Torvalds goto out; 38011da177e4SLinus Torvalds 38029ba2add3SShmulik Ladkani if (!ipv6_accept_ra(idev)) 38031da177e4SLinus Torvalds goto out; 38045b2a1953Sstephen hemminger 38055b2a1953Sstephen hemminger /* Announcement received after solicitation was sent */ 38065b2a1953Sstephen hemminger if (idev->if_flags & IF_RA_RCVD) 38075b2a1953Sstephen hemminger goto out; 38081da177e4SLinus Torvalds 3809bd11f074SMaciej Żenczykowski if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) { 3810caf92bc4SCong Wang write_unlock(&idev->lock); 3811caf92bc4SCong Wang if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE)) 3812caf92bc4SCong Wang ndisc_send_rs(dev, &lladdr, 3813b7b1bfceSHannes Frederic Sowa &in6addr_linklocal_allrouters); 3814b7b1bfceSHannes Frederic Sowa else 3815caf92bc4SCong Wang goto put; 3816b7b1bfceSHannes Frederic Sowa 3817caf92bc4SCong Wang write_lock(&idev->lock); 3818bd11f074SMaciej Żenczykowski idev->rs_interval = rfc3315_s14_backoff_update( 3819bd11f074SMaciej Żenczykowski idev->rs_interval, idev->cnf.rtr_solicit_max_interval); 38201da177e4SLinus Torvalds /* The wait after the last probe can be shorter */ 3821b7b1bfceSHannes Frederic Sowa addrconf_mod_rs_timer(idev, (idev->rs_probes == 3822b7b1bfceSHannes Frederic Sowa idev->cnf.rtr_solicits) ? 38235b2a1953Sstephen hemminger idev->cnf.rtr_solicit_delay : 3824bd11f074SMaciej Żenczykowski idev->rs_interval); 38251da177e4SLinus Torvalds } else { 38261da177e4SLinus Torvalds /* 38271da177e4SLinus Torvalds * Note: we do not support deprecated "all on-link" 38281da177e4SLinus Torvalds * assumption any longer. 38291da177e4SLinus Torvalds */ 383091df42beSJoe Perches pr_debug("%s: no IPv6 routers present\n", idev->dev->name); 38311da177e4SLinus Torvalds } 38321da177e4SLinus Torvalds 38331da177e4SLinus Torvalds out: 3834b7b1bfceSHannes Frederic Sowa write_unlock(&idev->lock); 3835caf92bc4SCong Wang put: 3836b7b1bfceSHannes Frederic Sowa in6_dev_put(idev); 38371da177e4SLinus Torvalds } 38381da177e4SLinus Torvalds 38391da177e4SLinus Torvalds /* 38401da177e4SLinus Torvalds * Duplicate Address Detection 38411da177e4SLinus Torvalds */ 38423c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp) 38433c21edbdSYOSHIFUJI Hideaki { 38443c21edbdSYOSHIFUJI Hideaki unsigned long rand_num; 38453c21edbdSYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev; 3846adc176c5SErik Nordmark u64 nonce; 38473c21edbdSYOSHIFUJI Hideaki 384895c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 384995c385b4SNeil Horman rand_num = 0; 385095c385b4SNeil Horman else 385163862b5bSAruna-Hewapathirane rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1); 385295c385b4SNeil Horman 3853adc176c5SErik Nordmark nonce = 0; 3854adc176c5SErik Nordmark if (idev->cnf.enhanced_dad || 3855adc176c5SErik Nordmark dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad) { 3856adc176c5SErik Nordmark do 3857adc176c5SErik Nordmark get_random_bytes(&nonce, 6); 3858adc176c5SErik Nordmark while (nonce == 0); 3859adc176c5SErik Nordmark } 3860adc176c5SErik Nordmark ifp->dad_nonce = nonce; 3861b7b1bfceSHannes Frederic Sowa ifp->dad_probes = idev->cnf.dad_transmits; 3862c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, rand_num); 38633c21edbdSYOSHIFUJI Hideaki } 38643c21edbdSYOSHIFUJI Hideaki 3865c15b1ccaSHannes Frederic Sowa static void addrconf_dad_begin(struct inet6_ifaddr *ifp) 38661da177e4SLinus Torvalds { 38671da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 38681da177e4SLinus Torvalds struct net_device *dev = idev->dev; 3869764d3be6SPaolo Abeni bool bump_id, notify = false; 3870afb1d4b5SDavid Ahern struct net *net; 38711da177e4SLinus Torvalds 38721da177e4SLinus Torvalds addrconf_join_solict(dev, &ifp->addr); 38731da177e4SLinus Torvalds 387463862b5bSAruna-Hewapathirane prandom_seed((__force u32) ifp->addr.s6_addr32[3]); 38751da177e4SLinus Torvalds 38761da177e4SLinus Torvalds read_lock_bh(&idev->lock); 3877622ccdf1SHerbert Xu spin_lock(&ifp->lock); 3878e9d3e084SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DEAD) 38791da177e4SLinus Torvalds goto out; 38801da177e4SLinus Torvalds 3881afb1d4b5SDavid Ahern net = dev_net(dev); 38821da177e4SLinus Torvalds if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) || 3883afb1d4b5SDavid Ahern (net->ipv6.devconf_all->accept_dad < 1 && 3884a2d3f3e3SMatteo Croce idev->cnf.accept_dad < 1) || 388555ebaef1SNoriaki TAKAMIYA !(ifp->flags&IFA_F_TENTATIVE) || 388655ebaef1SNoriaki TAKAMIYA ifp->flags & IFA_F_NODAD) { 3887c76fe2d9SDavid Ahern bool send_na = false; 3888c76fe2d9SDavid Ahern 3889c76fe2d9SDavid Ahern if (ifp->flags & IFA_F_TENTATIVE && 3890c76fe2d9SDavid Ahern !(ifp->flags & IFA_F_OPTIMISTIC)) 3891c76fe2d9SDavid Ahern send_na = true; 3892764d3be6SPaolo Abeni bump_id = ifp->flags & IFA_F_TENTATIVE; 3893cc411d0bSBrian Haley ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 389421809fafSstephen hemminger spin_unlock(&ifp->lock); 38951da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 38961da177e4SLinus Torvalds 3897c76fe2d9SDavid Ahern addrconf_dad_completed(ifp, bump_id, send_na); 38981da177e4SLinus Torvalds return; 38991da177e4SLinus Torvalds } 39001da177e4SLinus Torvalds 39016732badeSYOSHIFUJI Hideaki if (!(idev->if_flags & IF_READY)) { 390221809fafSstephen hemminger spin_unlock(&ifp->lock); 39036732badeSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 39043c21edbdSYOSHIFUJI Hideaki /* 3905590a9887SMasatake YAMATO * If the device is not ready: 39063c21edbdSYOSHIFUJI Hideaki * - keep it tentative if it is a permanent address. 39073c21edbdSYOSHIFUJI Hideaki * - otherwise, kill it. 39083c21edbdSYOSHIFUJI Hideaki */ 39093c21edbdSYOSHIFUJI Hideaki in6_ifa_hold(ifp); 3910cc411d0bSBrian Haley addrconf_dad_stop(ifp, 0); 39116732badeSYOSHIFUJI Hideaki return; 39123c21edbdSYOSHIFUJI Hideaki } 391395c385b4SNeil Horman 391495c385b4SNeil Horman /* 391595c385b4SNeil Horman * Optimistic nodes can start receiving 391695c385b4SNeil Horman * Frames right away 391795c385b4SNeil Horman */ 39187fd2561eSErik Kline if (ifp->flags & IFA_F_OPTIMISTIC) { 3919afb1d4b5SDavid Ahern ip6_ins_rt(net, ifp->rt); 3920afb1d4b5SDavid Ahern if (ipv6_use_optimistic_addr(net, idev)) { 39217fd2561eSErik Kline /* Because optimistic nodes can use this address, 39227fd2561eSErik Kline * notify listeners. If DAD fails, RTM_DELADDR is sent. 39237fd2561eSErik Kline */ 392416186a82Ssubashab@codeaurora.org notify = true; 39257fd2561eSErik Kline } 39267fd2561eSErik Kline } 392795c385b4SNeil Horman 39286732badeSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 39291da177e4SLinus Torvalds out: 3930622ccdf1SHerbert Xu spin_unlock(&ifp->lock); 39311da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 393216186a82Ssubashab@codeaurora.org if (notify) 393316186a82Ssubashab@codeaurora.org ipv6_ifa_notify(RTM_NEWADDR, ifp); 39341da177e4SLinus Torvalds } 39351da177e4SLinus Torvalds 3936c15b1ccaSHannes Frederic Sowa static void addrconf_dad_start(struct inet6_ifaddr *ifp) 39371da177e4SLinus Torvalds { 3938c15b1ccaSHannes Frederic Sowa bool begin_dad = false; 3939c15b1ccaSHannes Frederic Sowa 39408e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 3941c15b1ccaSHannes Frederic Sowa if (ifp->state != INET6_IFADDR_STATE_DEAD) { 3942c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_PREDAD; 3943c15b1ccaSHannes Frederic Sowa begin_dad = true; 3944c15b1ccaSHannes Frederic Sowa } 39458e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 3946c15b1ccaSHannes Frederic Sowa 3947c15b1ccaSHannes Frederic Sowa if (begin_dad) 3948c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 0); 3949c15b1ccaSHannes Frederic Sowa } 3950c15b1ccaSHannes Frederic Sowa 3951c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w) 3952c15b1ccaSHannes Frederic Sowa { 3953c15b1ccaSHannes Frederic Sowa struct inet6_ifaddr *ifp = container_of(to_delayed_work(w), 3954c15b1ccaSHannes Frederic Sowa struct inet6_ifaddr, 3955c15b1ccaSHannes Frederic Sowa dad_work); 39561da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 3957764d3be6SPaolo Abeni bool bump_id, disable_ipv6 = false; 39581da177e4SLinus Torvalds struct in6_addr mcaddr; 39591da177e4SLinus Torvalds 3960c15b1ccaSHannes Frederic Sowa enum { 3961c15b1ccaSHannes Frederic Sowa DAD_PROCESS, 3962c15b1ccaSHannes Frederic Sowa DAD_BEGIN, 3963c15b1ccaSHannes Frederic Sowa DAD_ABORT, 3964c15b1ccaSHannes Frederic Sowa } action = DAD_PROCESS; 3965c15b1ccaSHannes Frederic Sowa 3966c15b1ccaSHannes Frederic Sowa rtnl_lock(); 3967c15b1ccaSHannes Frederic Sowa 39688e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 3969c15b1ccaSHannes Frederic Sowa if (ifp->state == INET6_IFADDR_STATE_PREDAD) { 3970c15b1ccaSHannes Frederic Sowa action = DAD_BEGIN; 3971c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_DAD; 3972c15b1ccaSHannes Frederic Sowa } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) { 3973c15b1ccaSHannes Frederic Sowa action = DAD_ABORT; 3974c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_POSTDAD; 397585b51b12SMike Manning 397635e015e1SMatteo Croce if ((dev_net(idev->dev)->ipv6.devconf_all->accept_dad > 1 || 397735e015e1SMatteo Croce idev->cnf.accept_dad > 1) && 397835e015e1SMatteo Croce !idev->cnf.disable_ipv6 && 397985b51b12SMike Manning !(ifp->flags & IFA_F_STABLE_PRIVACY)) { 398085b51b12SMike Manning struct in6_addr addr; 398185b51b12SMike Manning 398285b51b12SMike Manning addr.s6_addr32[0] = htonl(0xfe800000); 398385b51b12SMike Manning addr.s6_addr32[1] = 0; 398485b51b12SMike Manning 398585b51b12SMike Manning if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) && 398685b51b12SMike Manning ipv6_addr_equal(&ifp->addr, &addr)) { 398785b51b12SMike Manning /* DAD failed for link-local based on MAC */ 398885b51b12SMike Manning idev->cnf.disable_ipv6 = 1; 398985b51b12SMike Manning 399085b51b12SMike Manning pr_info("%s: IPv6 being disabled!\n", 399185b51b12SMike Manning ifp->idev->dev->name); 399285b51b12SMike Manning disable_ipv6 = true; 399385b51b12SMike Manning } 399485b51b12SMike Manning } 3995c15b1ccaSHannes Frederic Sowa } 39968e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 3997c15b1ccaSHannes Frederic Sowa 3998c15b1ccaSHannes Frederic Sowa if (action == DAD_BEGIN) { 3999c15b1ccaSHannes Frederic Sowa addrconf_dad_begin(ifp); 4000c15b1ccaSHannes Frederic Sowa goto out; 4001c15b1ccaSHannes Frederic Sowa } else if (action == DAD_ABORT) { 4002751eb6b6SWei Yongjun in6_ifa_hold(ifp); 4003c15b1ccaSHannes Frederic Sowa addrconf_dad_stop(ifp, 1); 400485b51b12SMike Manning if (disable_ipv6) 400585b51b12SMike Manning addrconf_ifdown(idev->dev, 0); 4006c15b1ccaSHannes Frederic Sowa goto out; 4007c15b1ccaSHannes Frederic Sowa } 4008c15b1ccaSHannes Frederic Sowa 4009b7b1bfceSHannes Frederic Sowa if (!ifp->dad_probes && addrconf_dad_end(ifp)) 4010f2344a13SHerbert Xu goto out; 4011f2344a13SHerbert Xu 4012c15b1ccaSHannes Frederic Sowa write_lock_bh(&idev->lock); 4013122e4519Sstephen hemminger if (idev->dead || !(idev->if_flags & IF_READY)) { 4014c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 40151da177e4SLinus Torvalds goto out; 40161da177e4SLinus Torvalds } 401721809fafSstephen hemminger 401821809fafSstephen hemminger spin_lock(&ifp->lock); 4019622ccdf1SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DEAD) { 4020622ccdf1SHerbert Xu spin_unlock(&ifp->lock); 4021c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 4022622ccdf1SHerbert Xu goto out; 4023622ccdf1SHerbert Xu } 4024622ccdf1SHerbert Xu 4025b7b1bfceSHannes Frederic Sowa if (ifp->dad_probes == 0) { 4026c76fe2d9SDavid Ahern bool send_na = false; 4027c76fe2d9SDavid Ahern 40281da177e4SLinus Torvalds /* 40291da177e4SLinus Torvalds * DAD was successful 40301da177e4SLinus Torvalds */ 40311da177e4SLinus Torvalds 4032c76fe2d9SDavid Ahern if (ifp->flags & IFA_F_TENTATIVE && 4033c76fe2d9SDavid Ahern !(ifp->flags & IFA_F_OPTIMISTIC)) 4034c76fe2d9SDavid Ahern send_na = true; 4035764d3be6SPaolo Abeni bump_id = ifp->flags & IFA_F_TENTATIVE; 4036cc411d0bSBrian Haley ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 403721809fafSstephen hemminger spin_unlock(&ifp->lock); 4038c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 40391da177e4SLinus Torvalds 4040c76fe2d9SDavid Ahern addrconf_dad_completed(ifp, bump_id, send_na); 40411da177e4SLinus Torvalds 40421da177e4SLinus Torvalds goto out; 40431da177e4SLinus Torvalds } 40441da177e4SLinus Torvalds 4045b7b1bfceSHannes Frederic Sowa ifp->dad_probes--; 4046c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 40471f9248e5SJiri Pirko NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME)); 404821809fafSstephen hemminger spin_unlock(&ifp->lock); 4049c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 40501da177e4SLinus Torvalds 40511da177e4SLinus Torvalds /* send a neighbour solicitation for our addr */ 40521da177e4SLinus Torvalds addrconf_addr_solict_mult(&ifp->addr, &mcaddr); 4053adc176c5SErik Nordmark ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any, 4054adc176c5SErik Nordmark ifp->dad_nonce); 40551da177e4SLinus Torvalds out: 40561da177e4SLinus Torvalds in6_ifa_put(ifp); 4057c15b1ccaSHannes Frederic Sowa rtnl_unlock(); 40581da177e4SLinus Torvalds } 40591da177e4SLinus Torvalds 406011ffff75SHannes Frederic Sowa /* ifp->idev must be at least read locked */ 406111ffff75SHannes Frederic Sowa static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp) 406211ffff75SHannes Frederic Sowa { 406311ffff75SHannes Frederic Sowa struct inet6_ifaddr *ifpiter; 406411ffff75SHannes Frederic Sowa struct inet6_dev *idev = ifp->idev; 406511ffff75SHannes Frederic Sowa 4066602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) { 4067602582caSHannes Frederic Sowa if (ifpiter->scope > IFA_LINK) 4068602582caSHannes Frederic Sowa break; 406911ffff75SHannes Frederic Sowa if (ifp != ifpiter && ifpiter->scope == IFA_LINK && 407011ffff75SHannes Frederic Sowa (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE| 407111ffff75SHannes Frederic Sowa IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) == 407211ffff75SHannes Frederic Sowa IFA_F_PERMANENT) 407311ffff75SHannes Frederic Sowa return false; 407411ffff75SHannes Frederic Sowa } 407511ffff75SHannes Frederic Sowa return true; 407611ffff75SHannes Frederic Sowa } 407711ffff75SHannes Frederic Sowa 4078c76fe2d9SDavid Ahern static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id, 4079c76fe2d9SDavid Ahern bool send_na) 40801da177e4SLinus Torvalds { 40811da177e4SLinus Torvalds struct net_device *dev = ifp->idev->dev; 4082b7b1bfceSHannes Frederic Sowa struct in6_addr lladdr; 4083b173ee48SHannes Frederic Sowa bool send_rs, send_mld; 4084b7b1bfceSHannes Frederic Sowa 4085c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifp); 40861da177e4SLinus Torvalds 40871da177e4SLinus Torvalds /* 40881da177e4SLinus Torvalds * Configure the address for reception. Now it is valid. 40891da177e4SLinus Torvalds */ 40901da177e4SLinus Torvalds 40911da177e4SLinus Torvalds ipv6_ifa_notify(RTM_NEWADDR, ifp); 40921da177e4SLinus Torvalds 4093026359bcSTore Anderson /* If added prefix is link local and we are prepared to process 4094026359bcSTore Anderson router advertisements, start sending router solicitations. 40951da177e4SLinus Torvalds */ 40961da177e4SLinus Torvalds 40971ec047ebSHannes Frederic Sowa read_lock_bh(&ifp->idev->lock); 409811ffff75SHannes Frederic Sowa send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp); 4099b173ee48SHannes Frederic Sowa send_rs = send_mld && 4100b173ee48SHannes Frederic Sowa ipv6_accept_ra(ifp->idev) && 4101bd11f074SMaciej Żenczykowski ifp->idev->cnf.rtr_solicits != 0 && 4102b173ee48SHannes Frederic Sowa (dev->flags&IFF_LOOPBACK) == 0; 41031ec047ebSHannes Frederic Sowa read_unlock_bh(&ifp->idev->lock); 41041ec047ebSHannes Frederic Sowa 4105b173ee48SHannes Frederic Sowa /* While dad is in progress mld report's source address is in6_addrany. 4106b173ee48SHannes Frederic Sowa * Resend with proper ll now. 4107b173ee48SHannes Frederic Sowa */ 4108b173ee48SHannes Frederic Sowa if (send_mld) 4109b173ee48SHannes Frederic Sowa ipv6_mc_dad_complete(ifp->idev); 4110b173ee48SHannes Frederic Sowa 4111c76fe2d9SDavid Ahern /* send unsolicited NA if enabled */ 4112c76fe2d9SDavid Ahern if (send_na && 4113c76fe2d9SDavid Ahern (ifp->idev->cnf.ndisc_notify || 4114c76fe2d9SDavid Ahern dev_net(dev)->ipv6.devconf_all->ndisc_notify)) { 4115c76fe2d9SDavid Ahern ndisc_send_na(dev, &in6addr_linklocal_allnodes, &ifp->addr, 4116c76fe2d9SDavid Ahern /*router=*/ !!ifp->idev->cnf.forwarding, 4117c76fe2d9SDavid Ahern /*solicited=*/ false, /*override=*/ true, 4118c76fe2d9SDavid Ahern /*inc_opt=*/ true); 4119c76fe2d9SDavid Ahern } 4120c76fe2d9SDavid Ahern 41211ec047ebSHannes Frederic Sowa if (send_rs) { 41221da177e4SLinus Torvalds /* 41231da177e4SLinus Torvalds * If a host as already performed a random delay 41241da177e4SLinus Torvalds * [...] as part of DAD [...] there is no need 41251da177e4SLinus Torvalds * to delay again before sending the first RS 41261da177e4SLinus Torvalds */ 41271ec047ebSHannes Frederic Sowa if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE)) 4128b7b1bfceSHannes Frederic Sowa return; 41291ec047ebSHannes Frederic Sowa ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters); 41301da177e4SLinus Torvalds 4131b7b1bfceSHannes Frederic Sowa write_lock_bh(&ifp->idev->lock); 4132b7b1bfceSHannes Frederic Sowa spin_lock(&ifp->lock); 4133bd11f074SMaciej Żenczykowski ifp->idev->rs_interval = rfc3315_s14_backoff_init( 4134bd11f074SMaciej Żenczykowski ifp->idev->cnf.rtr_solicit_interval); 4135b7b1bfceSHannes Frederic Sowa ifp->idev->rs_probes = 1; 41361da177e4SLinus Torvalds ifp->idev->if_flags |= IF_RS_SENT; 4137bd11f074SMaciej Żenczykowski addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval); 4138b7b1bfceSHannes Frederic Sowa spin_unlock(&ifp->lock); 4139b7b1bfceSHannes Frederic Sowa write_unlock_bh(&ifp->idev->lock); 41401da177e4SLinus Torvalds } 4141764d3be6SPaolo Abeni 4142764d3be6SPaolo Abeni if (bump_id) 4143764d3be6SPaolo Abeni rt_genid_bump_ipv6(dev_net(dev)); 4144a11a7f71SMarcus Huewe 4145a11a7f71SMarcus Huewe /* Make sure that a new temporary address will be created 4146a11a7f71SMarcus Huewe * before this temporary address becomes deprecated. 4147a11a7f71SMarcus Huewe */ 4148a11a7f71SMarcus Huewe if (ifp->flags & IFA_F_TEMPORARY) 4149a11a7f71SMarcus Huewe addrconf_verify_rtnl(); 41501da177e4SLinus Torvalds } 41511da177e4SLinus Torvalds 4152e21e8467SStephen Hemminger static void addrconf_dad_run(struct inet6_dev *idev) 4153e21e8467SStephen Hemminger { 4154c5e33bddSYOSHIFUJI Hideaki struct inet6_ifaddr *ifp; 4155c5e33bddSYOSHIFUJI Hideaki 4156c5e33bddSYOSHIFUJI Hideaki read_lock_bh(&idev->lock); 4157502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 415821809fafSstephen hemminger spin_lock(&ifp->lock); 4159f2344a13SHerbert Xu if (ifp->flags & IFA_F_TENTATIVE && 4160f2344a13SHerbert Xu ifp->state == INET6_IFADDR_STATE_DAD) 4161c5e33bddSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 4162f2344a13SHerbert Xu spin_unlock(&ifp->lock); 4163c5e33bddSYOSHIFUJI Hideaki } 4164c5e33bddSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 4165c5e33bddSYOSHIFUJI Hideaki } 4166c5e33bddSYOSHIFUJI Hideaki 41671da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 41681da177e4SLinus Torvalds struct if6_iter_state { 41693c40090aSDaniel Lezcano struct seq_net_private p; 41701da177e4SLinus Torvalds int bucket; 41711d578303SMihai Maruseac int offset; 41721da177e4SLinus Torvalds }; 41731da177e4SLinus Torvalds 41741d578303SMihai Maruseac static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos) 41751da177e4SLinus Torvalds { 41761da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 41771218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 4178a5c1d98fSEric Dumazet struct inet6_ifaddr *ifa = NULL; 41791d578303SMihai Maruseac int p = 0; 41801da177e4SLinus Torvalds 41811d578303SMihai Maruseac /* initial bucket if pos is 0 */ 41821d578303SMihai Maruseac if (pos == 0) { 41831d578303SMihai Maruseac state->bucket = 0; 41841d578303SMihai Maruseac state->offset = 0; 41851d578303SMihai Maruseac } 41861d578303SMihai Maruseac 41871d578303SMihai Maruseac for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) { 4188a5c1d98fSEric Dumazet hlist_for_each_entry_rcu(ifa, &inet6_addr_lst[state->bucket], 41891d578303SMihai Maruseac addr_lst) { 41909f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 41919f0d3c27SEric Dumazet continue; 41921d578303SMihai Maruseac /* sync with offset */ 41931d578303SMihai Maruseac if (p < state->offset) { 41941d578303SMihai Maruseac p++; 41951d578303SMihai Maruseac continue; 41961d578303SMihai Maruseac } 41971d578303SMihai Maruseac state->offset++; 41981da177e4SLinus Torvalds return ifa; 41991da177e4SLinus Torvalds } 42001d578303SMihai Maruseac 42011d578303SMihai Maruseac /* prepare for next bucket */ 42021d578303SMihai Maruseac state->offset = 0; 42031d578303SMihai Maruseac p = 0; 42041d578303SMihai Maruseac } 4205c2e21293Sstephen hemminger return NULL; 4206c2e21293Sstephen hemminger } 42071da177e4SLinus Torvalds 4208c2e21293Sstephen hemminger static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, 4209c2e21293Sstephen hemminger struct inet6_ifaddr *ifa) 42101da177e4SLinus Torvalds { 42111da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 42121218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 42131da177e4SLinus Torvalds 4214a5c1d98fSEric Dumazet hlist_for_each_entry_continue_rcu(ifa, addr_lst) { 42159f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 42169f0d3c27SEric Dumazet continue; 42171d578303SMihai Maruseac state->offset++; 42181da177e4SLinus Torvalds return ifa; 42191d578303SMihai Maruseac } 42201da177e4SLinus Torvalds 4221c2e21293Sstephen hemminger while (++state->bucket < IN6_ADDR_HSIZE) { 42221d578303SMihai Maruseac state->offset = 0; 4223a5c1d98fSEric Dumazet hlist_for_each_entry_rcu(ifa, 4224c2e21293Sstephen hemminger &inet6_addr_lst[state->bucket], addr_lst) { 42259f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 42269f0d3c27SEric Dumazet continue; 42271d578303SMihai Maruseac state->offset++; 4228c2e21293Sstephen hemminger return ifa; 4229c2e21293Sstephen hemminger } 4230c2e21293Sstephen hemminger } 4231c2e21293Sstephen hemminger 4232c2e21293Sstephen hemminger return NULL; 4233c2e21293Sstephen hemminger } 4234c2e21293Sstephen hemminger 42351da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos) 4236a5c1d98fSEric Dumazet __acquires(rcu) 42371da177e4SLinus Torvalds { 4238a5c1d98fSEric Dumazet rcu_read_lock(); 42391d578303SMihai Maruseac return if6_get_first(seq, *pos); 42401da177e4SLinus Torvalds } 42411da177e4SLinus Torvalds 42421da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos) 42431da177e4SLinus Torvalds { 42441da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 42451da177e4SLinus Torvalds 42461da177e4SLinus Torvalds ifa = if6_get_next(seq, v); 42471da177e4SLinus Torvalds ++*pos; 42481da177e4SLinus Torvalds return ifa; 42491da177e4SLinus Torvalds } 42501da177e4SLinus Torvalds 42511da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v) 4252a5c1d98fSEric Dumazet __releases(rcu) 42531da177e4SLinus Torvalds { 4254a5c1d98fSEric Dumazet rcu_read_unlock(); 42551da177e4SLinus Torvalds } 42561da177e4SLinus Torvalds 42571da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v) 42581da177e4SLinus Torvalds { 42591da177e4SLinus Torvalds struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v; 4260971a351cSJiri Pirko seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n", 4261b071195dSHarvey Harrison &ifp->addr, 42621da177e4SLinus Torvalds ifp->idev->dev->ifindex, 42631da177e4SLinus Torvalds ifp->prefix_len, 42641da177e4SLinus Torvalds ifp->scope, 4265971a351cSJiri Pirko (u8) ifp->flags, 42661da177e4SLinus Torvalds ifp->idev->dev->name); 42671da177e4SLinus Torvalds return 0; 42681da177e4SLinus Torvalds } 42691da177e4SLinus Torvalds 427056b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = { 42711da177e4SLinus Torvalds .start = if6_seq_start, 42721da177e4SLinus Torvalds .next = if6_seq_next, 42731da177e4SLinus Torvalds .show = if6_seq_show, 42741da177e4SLinus Torvalds .stop = if6_seq_stop, 42751da177e4SLinus Torvalds }; 42761da177e4SLinus Torvalds 42771da177e4SLinus Torvalds static int if6_seq_open(struct inode *inode, struct file *file) 42781da177e4SLinus Torvalds { 42793c40090aSDaniel Lezcano return seq_open_net(inode, file, &if6_seq_ops, 4280cf7732e4SPavel Emelyanov sizeof(struct if6_iter_state)); 42811da177e4SLinus Torvalds } 42821da177e4SLinus Torvalds 42839a32144eSArjan van de Ven static const struct file_operations if6_fops = { 42841da177e4SLinus Torvalds .open = if6_seq_open, 42851da177e4SLinus Torvalds .read = seq_read, 42861da177e4SLinus Torvalds .llseek = seq_lseek, 42873c40090aSDaniel Lezcano .release = seq_release_net, 42881da177e4SLinus Torvalds }; 42891da177e4SLinus Torvalds 42902c8c1e72SAlexey Dobriyan static int __net_init if6_proc_net_init(struct net *net) 42911da177e4SLinus Torvalds { 4292d6444062SJoe Perches if (!proc_create("if_inet6", 0444, net->proc_net, &if6_fops)) 42931da177e4SLinus Torvalds return -ENOMEM; 42941da177e4SLinus Torvalds return 0; 42951da177e4SLinus Torvalds } 42961da177e4SLinus Torvalds 42972c8c1e72SAlexey Dobriyan static void __net_exit if6_proc_net_exit(struct net *net) 42983c40090aSDaniel Lezcano { 4299ece31ffdSGao feng remove_proc_entry("if_inet6", net->proc_net); 43003c40090aSDaniel Lezcano } 43013c40090aSDaniel Lezcano 43023c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = { 43033c40090aSDaniel Lezcano .init = if6_proc_net_init, 43043c40090aSDaniel Lezcano .exit = if6_proc_net_exit, 43053c40090aSDaniel Lezcano }; 43063c40090aSDaniel Lezcano 43073c40090aSDaniel Lezcano int __init if6_proc_init(void) 43083c40090aSDaniel Lezcano { 43093c40090aSDaniel Lezcano return register_pernet_subsys(&if6_proc_net_ops); 43103c40090aSDaniel Lezcano } 43113c40090aSDaniel Lezcano 43121da177e4SLinus Torvalds void if6_proc_exit(void) 43131da177e4SLinus Torvalds { 43143c40090aSDaniel Lezcano unregister_pernet_subsys(&if6_proc_net_ops); 43151da177e4SLinus Torvalds } 43161da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 43171da177e4SLinus Torvalds 431807a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6) 43193b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */ 4320b71d1d42SEric Dumazet int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr) 43213b9f9a1cSNoriaki TAKAMIYA { 43223f27fb23SEric Dumazet unsigned int hash = inet6_addr_hash(net, addr); 4323c2e21293Sstephen hemminger struct inet6_ifaddr *ifp = NULL; 43243f27fb23SEric Dumazet int ret = 0; 4325c2e21293Sstephen hemminger 43264e5f47abSEric Dumazet rcu_read_lock(); 43274e5f47abSEric Dumazet hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) { 4328878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 4329389f6612SDaniel Lezcano continue; 4330caad295fSYOSHIFUJI Hideaki if (ipv6_addr_equal(&ifp->addr, addr) && 43313b9f9a1cSNoriaki TAKAMIYA (ifp->flags & IFA_F_HOMEADDRESS)) { 43323b9f9a1cSNoriaki TAKAMIYA ret = 1; 43333b9f9a1cSNoriaki TAKAMIYA break; 43343b9f9a1cSNoriaki TAKAMIYA } 43353b9f9a1cSNoriaki TAKAMIYA } 43364e5f47abSEric Dumazet rcu_read_unlock(); 43373b9f9a1cSNoriaki TAKAMIYA return ret; 43383b9f9a1cSNoriaki TAKAMIYA } 43393b9f9a1cSNoriaki TAKAMIYA #endif 43403b9f9a1cSNoriaki TAKAMIYA 43411da177e4SLinus Torvalds /* 43421da177e4SLinus Torvalds * Periodic address status verification 43431da177e4SLinus Torvalds */ 43441da177e4SLinus Torvalds 4345c15b1ccaSHannes Frederic Sowa static void addrconf_verify_rtnl(void) 43461da177e4SLinus Torvalds { 4347b2db7564SYOSHIFUJI Hideaki unsigned long now, next, next_sec, next_sched; 43481da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 43491da177e4SLinus Torvalds int i; 43501da177e4SLinus Torvalds 4351c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 4352c15b1ccaSHannes Frederic Sowa 43535c578aedSstephen hemminger rcu_read_lock_bh(); 43541da177e4SLinus Torvalds now = jiffies; 4355b2db7564SYOSHIFUJI Hideaki next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY); 43561da177e4SLinus Torvalds 4357c15b1ccaSHannes Frederic Sowa cancel_delayed_work(&addr_chk_work); 43581da177e4SLinus Torvalds 43591da177e4SLinus Torvalds for (i = 0; i < IN6_ADDR_HSIZE; i++) { 43601da177e4SLinus Torvalds restart: 4361c15b1ccaSHannes Frederic Sowa hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) { 43621da177e4SLinus Torvalds unsigned long age; 43631da177e4SLinus Torvalds 4364fad8da3eSYasushi Asano /* When setting preferred_lft to a value not zero or 4365fad8da3eSYasushi Asano * infinity, while valid_lft is infinity 4366fad8da3eSYasushi Asano * IFA_F_PERMANENT has a non-infinity life time. 4367fad8da3eSYasushi Asano */ 4368fad8da3eSYasushi Asano if ((ifp->flags & IFA_F_PERMANENT) && 4369fad8da3eSYasushi Asano (ifp->prefered_lft == INFINITY_LIFE_TIME)) 43701da177e4SLinus Torvalds continue; 43711da177e4SLinus Torvalds 43721da177e4SLinus Torvalds spin_lock(&ifp->lock); 4373b2db7564SYOSHIFUJI Hideaki /* We try to batch several events at once. */ 4374b2db7564SYOSHIFUJI Hideaki age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; 43751da177e4SLinus Torvalds 43768f27ebb9SYOSHIFUJI Hideaki if (ifp->valid_lft != INFINITY_LIFE_TIME && 43778f27ebb9SYOSHIFUJI Hideaki age >= ifp->valid_lft) { 43781da177e4SLinus Torvalds spin_unlock(&ifp->lock); 43791da177e4SLinus Torvalds in6_ifa_hold(ifp); 43801da177e4SLinus Torvalds ipv6_del_addr(ifp); 43811da177e4SLinus Torvalds goto restart; 43828f27ebb9SYOSHIFUJI Hideaki } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) { 43838f27ebb9SYOSHIFUJI Hideaki spin_unlock(&ifp->lock); 43848f27ebb9SYOSHIFUJI Hideaki continue; 43851da177e4SLinus Torvalds } else if (age >= ifp->prefered_lft) { 4386a1ed0526SBrian Haley /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */ 43871da177e4SLinus Torvalds int deprecate = 0; 43881da177e4SLinus Torvalds 43891da177e4SLinus Torvalds if (!(ifp->flags&IFA_F_DEPRECATED)) { 43901da177e4SLinus Torvalds deprecate = 1; 43911da177e4SLinus Torvalds ifp->flags |= IFA_F_DEPRECATED; 43921da177e4SLinus Torvalds } 43931da177e4SLinus Torvalds 4394fad8da3eSYasushi Asano if ((ifp->valid_lft != INFINITY_LIFE_TIME) && 4395fad8da3eSYasushi Asano (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))) 43961da177e4SLinus Torvalds next = ifp->tstamp + ifp->valid_lft * HZ; 43971da177e4SLinus Torvalds 43981da177e4SLinus Torvalds spin_unlock(&ifp->lock); 43991da177e4SLinus Torvalds 44001da177e4SLinus Torvalds if (deprecate) { 44011da177e4SLinus Torvalds in6_ifa_hold(ifp); 44021da177e4SLinus Torvalds 44031da177e4SLinus Torvalds ipv6_ifa_notify(0, ifp); 44041da177e4SLinus Torvalds in6_ifa_put(ifp); 44051da177e4SLinus Torvalds goto restart; 44061da177e4SLinus Torvalds } 44071da177e4SLinus Torvalds } else if ((ifp->flags&IFA_F_TEMPORARY) && 44081da177e4SLinus Torvalds !(ifp->flags&IFA_F_TENTATIVE)) { 440988949cf4Sstephen hemminger unsigned long regen_advance = ifp->idev->cnf.regen_max_retry * 441088949cf4Sstephen hemminger ifp->idev->cnf.dad_transmits * 44111f9248e5SJiri Pirko NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ; 441288949cf4Sstephen hemminger 44131da177e4SLinus Torvalds if (age >= ifp->prefered_lft - regen_advance) { 44141da177e4SLinus Torvalds struct inet6_ifaddr *ifpub = ifp->ifpub; 44151da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 44161da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 44171da177e4SLinus Torvalds if (!ifp->regen_count && ifpub) { 44181da177e4SLinus Torvalds ifp->regen_count++; 44191da177e4SLinus Torvalds in6_ifa_hold(ifp); 44201da177e4SLinus Torvalds in6_ifa_hold(ifpub); 44211da177e4SLinus Torvalds spin_unlock(&ifp->lock); 44225c578aedSstephen hemminger 4423291d809bSHiroyuki YAMAMORI spin_lock(&ifpub->lock); 4424291d809bSHiroyuki YAMAMORI ifpub->regen_count = 0; 4425291d809bSHiroyuki YAMAMORI spin_unlock(&ifpub->lock); 4426e64e469bSEric Dumazet rcu_read_unlock_bh(); 4427fffcefe9SEric Dumazet ipv6_create_tempaddr(ifpub, ifp, true); 44281da177e4SLinus Torvalds in6_ifa_put(ifpub); 44291da177e4SLinus Torvalds in6_ifa_put(ifp); 4430e64e469bSEric Dumazet rcu_read_lock_bh(); 44311da177e4SLinus Torvalds goto restart; 44321da177e4SLinus Torvalds } 44331da177e4SLinus Torvalds } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next)) 44341da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ; 44351da177e4SLinus Torvalds spin_unlock(&ifp->lock); 44361da177e4SLinus Torvalds } else { 44371da177e4SLinus Torvalds /* ifp->prefered_lft <= ifp->valid_lft */ 44381da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 44391da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 44401da177e4SLinus Torvalds spin_unlock(&ifp->lock); 44411da177e4SLinus Torvalds } 44421da177e4SLinus Torvalds } 44431da177e4SLinus Torvalds } 44441da177e4SLinus Torvalds 4445b2db7564SYOSHIFUJI Hideaki next_sec = round_jiffies_up(next); 4446b2db7564SYOSHIFUJI Hideaki next_sched = next; 4447b2db7564SYOSHIFUJI Hideaki 4448b2db7564SYOSHIFUJI Hideaki /* If rounded timeout is accurate enough, accept it. */ 4449b2db7564SYOSHIFUJI Hideaki if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ)) 4450b2db7564SYOSHIFUJI Hideaki next_sched = next_sec; 4451b2db7564SYOSHIFUJI Hideaki 4452b2db7564SYOSHIFUJI Hideaki /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */ 4453b2db7564SYOSHIFUJI Hideaki if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX)) 4454b2db7564SYOSHIFUJI Hideaki next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX; 4455b2db7564SYOSHIFUJI Hideaki 4456e32ac250SJoe Perches pr_debug("now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n", 4457ba3542e1Sdingtianhong now, next, next_sec, next_sched); 4458c15b1ccaSHannes Frederic Sowa mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now); 44595c578aedSstephen hemminger rcu_read_unlock_bh(); 44601da177e4SLinus Torvalds } 44611da177e4SLinus Torvalds 4462c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *w) 4463c15b1ccaSHannes Frederic Sowa { 4464c15b1ccaSHannes Frederic Sowa rtnl_lock(); 4465c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 4466c15b1ccaSHannes Frederic Sowa rtnl_unlock(); 4467c15b1ccaSHannes Frederic Sowa } 4468c15b1ccaSHannes Frederic Sowa 4469c15b1ccaSHannes Frederic Sowa static void addrconf_verify(void) 4470c15b1ccaSHannes Frederic Sowa { 4471c15b1ccaSHannes Frederic Sowa mod_delayed_work(addrconf_wq, &addr_chk_work, 0); 4472c15b1ccaSHannes Frederic Sowa } 4473c15b1ccaSHannes Frederic Sowa 4474caeaba79SNicolas Dichtel static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local, 4475caeaba79SNicolas Dichtel struct in6_addr **peer_pfx) 4476461d8837SThomas Graf { 4477461d8837SThomas Graf struct in6_addr *pfx = NULL; 4478461d8837SThomas Graf 4479caeaba79SNicolas Dichtel *peer_pfx = NULL; 4480caeaba79SNicolas Dichtel 4481461d8837SThomas Graf if (addr) 4482461d8837SThomas Graf pfx = nla_data(addr); 4483461d8837SThomas Graf 4484461d8837SThomas Graf if (local) { 4485461d8837SThomas Graf if (pfx && nla_memcmp(local, pfx, sizeof(*pfx))) 4486caeaba79SNicolas Dichtel *peer_pfx = pfx; 4487461d8837SThomas Graf pfx = nla_data(local); 4488461d8837SThomas Graf } 4489461d8837SThomas Graf 4490461d8837SThomas Graf return pfx; 4491461d8837SThomas Graf } 4492461d8837SThomas Graf 4493ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = { 4494461d8837SThomas Graf [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) }, 4495461d8837SThomas Graf [IFA_LOCAL] = { .len = sizeof(struct in6_addr) }, 4496461d8837SThomas Graf [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) }, 4497479840ffSJiri Pirko [IFA_FLAGS] = { .len = sizeof(u32) }, 4498461d8837SThomas Graf }; 4499461d8837SThomas Graf 45001da177e4SLinus Torvalds static int 4501c21ef3e3SDavid Ahern inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, 4502c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 45031da177e4SLinus Torvalds { 45043b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 4505b933f716SThomas Graf struct ifaddrmsg *ifm; 4506b933f716SThomas Graf struct nlattr *tb[IFA_MAX+1]; 4507caeaba79SNicolas Dichtel struct in6_addr *pfx, *peer_pfx; 45086046d5b4SHeiner Kallweit u32 ifa_flags; 4509b933f716SThomas Graf int err; 45101da177e4SLinus Torvalds 4511fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy, 4512c21ef3e3SDavid Ahern extack); 4513b933f716SThomas Graf if (err < 0) 4514b933f716SThomas Graf return err; 4515b933f716SThomas Graf 4516b933f716SThomas Graf ifm = nlmsg_data(nlh); 4517caeaba79SNicolas Dichtel pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); 451863159f29SIan Morris if (!pfx) 45191da177e4SLinus Torvalds return -EINVAL; 45201da177e4SLinus Torvalds 45216046d5b4SHeiner Kallweit ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags; 45226046d5b4SHeiner Kallweit 45236046d5b4SHeiner Kallweit /* We ignore other flags so far. */ 45246046d5b4SHeiner Kallweit ifa_flags &= IFA_F_MANAGETEMPADDR; 45256046d5b4SHeiner Kallweit 45266046d5b4SHeiner Kallweit return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx, 45276046d5b4SHeiner Kallweit ifm->ifa_prefixlen); 45281da177e4SLinus Torvalds } 45291da177e4SLinus Torvalds 4530*d169a1f8SDavid Ahern static int inet6_addr_modify(struct inet6_ifaddr *ifp, struct ifa6_config *cfg) 4531081bba5bSNoriaki TAKAMIYA { 45326f704992SYOSHIFUJI Hideaki u32 flags; 45336f704992SYOSHIFUJI Hideaki clock_t expires; 45344bed72e4SYOSHIFUJI Hideaki unsigned long timeout; 453553bd6749SJiri Pirko bool was_managetempaddr; 45365b84efecSThomas Haller bool had_prefixroute; 453746d48046SYOSHIFUJI Hideaki 4538c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 4539c15b1ccaSHannes Frederic Sowa 4540*d169a1f8SDavid Ahern if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft) 4541081bba5bSNoriaki TAKAMIYA return -EINVAL; 4542081bba5bSNoriaki TAKAMIYA 4543*d169a1f8SDavid Ahern if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR && 454453bd6749SJiri Pirko (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64)) 454553bd6749SJiri Pirko return -EINVAL; 454653bd6749SJiri Pirko 4547f1c02cfbSSabrina Dubroca if (!(ifp->flags & IFA_F_TENTATIVE) || ifp->flags & IFA_F_DADFAILED) 4548*d169a1f8SDavid Ahern cfg->ifa_flags &= ~IFA_F_OPTIMISTIC; 4549f1c02cfbSSabrina Dubroca 4550*d169a1f8SDavid Ahern timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ); 45514bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 45524bed72e4SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(timeout * HZ); 4553*d169a1f8SDavid Ahern cfg->valid_lft = timeout; 45546f704992SYOSHIFUJI Hideaki flags = RTF_EXPIRES; 45554bed72e4SYOSHIFUJI Hideaki } else { 45564bed72e4SYOSHIFUJI Hideaki expires = 0; 45574bed72e4SYOSHIFUJI Hideaki flags = 0; 4558*d169a1f8SDavid Ahern cfg->ifa_flags |= IFA_F_PERMANENT; 45596f704992SYOSHIFUJI Hideaki } 4560081bba5bSNoriaki TAKAMIYA 4561*d169a1f8SDavid Ahern timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ); 45624bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 45634bed72e4SYOSHIFUJI Hideaki if (timeout == 0) 4564*d169a1f8SDavid Ahern cfg->ifa_flags |= IFA_F_DEPRECATED; 4565*d169a1f8SDavid Ahern cfg->preferred_lft = timeout; 45664bed72e4SYOSHIFUJI Hideaki } 4567081bba5bSNoriaki TAKAMIYA 4568081bba5bSNoriaki TAKAMIYA spin_lock_bh(&ifp->lock); 456953bd6749SJiri Pirko was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR; 45705b84efecSThomas Haller had_prefixroute = ifp->flags & IFA_F_PERMANENT && 45715b84efecSThomas Haller !(ifp->flags & IFA_F_NOPREFIXROUTE); 457253bd6749SJiri Pirko ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | 4573761aac73SThomas Haller IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR | 4574761aac73SThomas Haller IFA_F_NOPREFIXROUTE); 4575*d169a1f8SDavid Ahern ifp->flags |= cfg->ifa_flags; 4576081bba5bSNoriaki TAKAMIYA ifp->tstamp = jiffies; 4577*d169a1f8SDavid Ahern ifp->valid_lft = cfg->valid_lft; 4578*d169a1f8SDavid Ahern ifp->prefered_lft = cfg->preferred_lft; 4579081bba5bSNoriaki TAKAMIYA 4580081bba5bSNoriaki TAKAMIYA spin_unlock_bh(&ifp->lock); 4581081bba5bSNoriaki TAKAMIYA if (!(ifp->flags&IFA_F_TENTATIVE)) 4582081bba5bSNoriaki TAKAMIYA ipv6_ifa_notify(0, ifp); 4583081bba5bSNoriaki TAKAMIYA 4584*d169a1f8SDavid Ahern if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) { 4585acb54e3cSDavid Ahern addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 4586acb54e3cSDavid Ahern ifp->idev->dev, expires, flags, 4587acb54e3cSDavid Ahern GFP_KERNEL); 45885b84efecSThomas Haller } else if (had_prefixroute) { 45895b84efecSThomas Haller enum cleanup_prefix_rt_t action; 45905b84efecSThomas Haller unsigned long rt_expires; 45915b84efecSThomas Haller 45925b84efecSThomas Haller write_lock_bh(&ifp->idev->lock); 45935b84efecSThomas Haller action = check_cleanup_prefix_route(ifp, &rt_expires); 45945b84efecSThomas Haller write_unlock_bh(&ifp->idev->lock); 45955b84efecSThomas Haller 45965b84efecSThomas Haller if (action != CLEANUP_PREFIX_RT_NOP) { 45975b84efecSThomas Haller cleanup_prefix_route(ifp, rt_expires, 45985b84efecSThomas Haller action == CLEANUP_PREFIX_RT_DEL); 45995b84efecSThomas Haller } 4600761aac73SThomas Haller } 460153bd6749SJiri Pirko 460253bd6749SJiri Pirko if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) { 4603*d169a1f8SDavid Ahern if (was_managetempaddr && 4604*d169a1f8SDavid Ahern !(ifp->flags & IFA_F_MANAGETEMPADDR)) { 4605*d169a1f8SDavid Ahern cfg->valid_lft = 0; 4606*d169a1f8SDavid Ahern cfg->preferred_lft = 0; 4607*d169a1f8SDavid Ahern } 4608*d169a1f8SDavid Ahern manage_tempaddrs(ifp->idev, ifp, cfg->valid_lft, 4609*d169a1f8SDavid Ahern cfg->preferred_lft, !was_managetempaddr, 4610*d169a1f8SDavid Ahern jiffies); 461153bd6749SJiri Pirko } 461253bd6749SJiri Pirko 4613c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 4614081bba5bSNoriaki TAKAMIYA 4615081bba5bSNoriaki TAKAMIYA return 0; 4616081bba5bSNoriaki TAKAMIYA } 4617081bba5bSNoriaki TAKAMIYA 4618081bba5bSNoriaki TAKAMIYA static int 4619c21ef3e3SDavid Ahern inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, 4620c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 46211da177e4SLinus Torvalds { 46223b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 4623461d8837SThomas Graf struct ifaddrmsg *ifm; 4624461d8837SThomas Graf struct nlattr *tb[IFA_MAX+1]; 462519b1518cSDavid Ahern struct in6_addr *peer_pfx; 46267198f8ceSThomas Graf struct inet6_ifaddr *ifa; 46277198f8ceSThomas Graf struct net_device *dev; 4628f1c02cfbSSabrina Dubroca struct inet6_dev *idev; 462919b1518cSDavid Ahern struct ifa6_config cfg; 4630461d8837SThomas Graf int err; 46311da177e4SLinus Torvalds 4632fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy, 4633c21ef3e3SDavid Ahern extack); 4634461d8837SThomas Graf if (err < 0) 4635461d8837SThomas Graf return err; 4636461d8837SThomas Graf 463719b1518cSDavid Ahern memset(&cfg, 0, sizeof(cfg)); 463819b1518cSDavid Ahern 4639461d8837SThomas Graf ifm = nlmsg_data(nlh); 464019b1518cSDavid Ahern cfg.pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); 464119b1518cSDavid Ahern if (!cfg.pfx) 46421da177e4SLinus Torvalds return -EINVAL; 46431da177e4SLinus Torvalds 464419b1518cSDavid Ahern cfg.peer_pfx = peer_pfx; 464519b1518cSDavid Ahern cfg.plen = ifm->ifa_prefixlen; 464619b1518cSDavid Ahern cfg.valid_lft = INFINITY_LIFE_TIME; 464719b1518cSDavid Ahern cfg.preferred_lft = INFINITY_LIFE_TIME; 464819b1518cSDavid Ahern 4649461d8837SThomas Graf if (tb[IFA_CACHEINFO]) { 46500778769dSNoriaki TAKAMIYA struct ifa_cacheinfo *ci; 4651461d8837SThomas Graf 4652461d8837SThomas Graf ci = nla_data(tb[IFA_CACHEINFO]); 465319b1518cSDavid Ahern cfg.valid_lft = ci->ifa_valid; 465419b1518cSDavid Ahern cfg.preferred_lft = ci->ifa_prefered; 46550778769dSNoriaki TAKAMIYA } 46560778769dSNoriaki TAKAMIYA 4657af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifm->ifa_index); 465863159f29SIan Morris if (!dev) 46597198f8ceSThomas Graf return -ENODEV; 4660081bba5bSNoriaki TAKAMIYA 466119b1518cSDavid Ahern if (tb[IFA_FLAGS]) 466219b1518cSDavid Ahern cfg.ifa_flags = nla_get_u32(tb[IFA_FLAGS]); 466319b1518cSDavid Ahern else 466419b1518cSDavid Ahern cfg.ifa_flags = ifm->ifa_flags; 4665479840ffSJiri Pirko 466655ebaef1SNoriaki TAKAMIYA /* We ignore other flags so far. */ 466719b1518cSDavid Ahern cfg.ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | 466819b1518cSDavid Ahern IFA_F_MANAGETEMPADDR | IFA_F_NOPREFIXROUTE | 466919b1518cSDavid Ahern IFA_F_MCAUTOJOIN | IFA_F_OPTIMISTIC; 4670f1c02cfbSSabrina Dubroca 4671f1c02cfbSSabrina Dubroca idev = ipv6_find_idev(dev); 4672f1c02cfbSSabrina Dubroca if (IS_ERR(idev)) 4673f1c02cfbSSabrina Dubroca return PTR_ERR(idev); 4674f1c02cfbSSabrina Dubroca 4675f1c02cfbSSabrina Dubroca if (!ipv6_allow_optimistic_dad(net, idev)) 467619b1518cSDavid Ahern cfg.ifa_flags &= ~IFA_F_OPTIMISTIC; 4677f1c02cfbSSabrina Dubroca 467819b1518cSDavid Ahern if (cfg.ifa_flags & IFA_F_NODAD && 467919b1518cSDavid Ahern cfg.ifa_flags & IFA_F_OPTIMISTIC) { 4680f1c02cfbSSabrina Dubroca NL_SET_ERR_MSG(extack, "IFA_F_NODAD and IFA_F_OPTIMISTIC are mutually exclusive"); 4681f1c02cfbSSabrina Dubroca return -EINVAL; 4682f1c02cfbSSabrina Dubroca } 468355ebaef1SNoriaki TAKAMIYA 468419b1518cSDavid Ahern ifa = ipv6_get_ifaddr(net, cfg.pfx, dev, 1); 468563159f29SIan Morris if (!ifa) { 46867198f8ceSThomas Graf /* 46877198f8ceSThomas Graf * It would be best to check for !NLM_F_CREATE here but 4688db9c7c39Sstephen hemminger * userspace already relies on not having to provide this. 46897198f8ceSThomas Graf */ 469019b1518cSDavid Ahern return inet6_addr_add(net, ifm->ifa_index, &cfg, extack); 46911da177e4SLinus Torvalds } 46921da177e4SLinus Torvalds 46937198f8ceSThomas Graf if (nlh->nlmsg_flags & NLM_F_EXCL || 46947198f8ceSThomas Graf !(nlh->nlmsg_flags & NLM_F_REPLACE)) 46957198f8ceSThomas Graf err = -EEXIST; 46967198f8ceSThomas Graf else 4697*d169a1f8SDavid Ahern err = inet6_addr_modify(ifa, &cfg); 46987198f8ceSThomas Graf 46997198f8ceSThomas Graf in6_ifa_put(ifa); 47007198f8ceSThomas Graf 47017198f8ceSThomas Graf return err; 47027198f8ceSThomas Graf } 47037198f8ceSThomas Graf 4704479840ffSJiri Pirko static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags, 4705101bb229SThomas Graf u8 scope, int ifindex) 4706101bb229SThomas Graf { 4707101bb229SThomas Graf struct ifaddrmsg *ifm; 4708101bb229SThomas Graf 4709101bb229SThomas Graf ifm = nlmsg_data(nlh); 4710101bb229SThomas Graf ifm->ifa_family = AF_INET6; 4711101bb229SThomas Graf ifm->ifa_prefixlen = prefixlen; 4712101bb229SThomas Graf ifm->ifa_flags = flags; 4713101bb229SThomas Graf ifm->ifa_scope = scope; 4714101bb229SThomas Graf ifm->ifa_index = ifindex; 4715101bb229SThomas Graf } 4716101bb229SThomas Graf 471785486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp, 471885486af0SThomas Graf unsigned long tstamp, u32 preferred, u32 valid) 471985486af0SThomas Graf { 472085486af0SThomas Graf struct ifa_cacheinfo ci; 472185486af0SThomas Graf 472218a31e1eSThomas Graf ci.cstamp = cstamp_delta(cstamp); 472318a31e1eSThomas Graf ci.tstamp = cstamp_delta(tstamp); 472485486af0SThomas Graf ci.ifa_prefered = preferred; 472585486af0SThomas Graf ci.ifa_valid = valid; 472685486af0SThomas Graf 472785486af0SThomas Graf return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci); 472885486af0SThomas Graf } 472985486af0SThomas Graf 4730101bb229SThomas Graf static inline int rt_scope(int ifa_scope) 4731101bb229SThomas Graf { 4732101bb229SThomas Graf if (ifa_scope & IFA_HOST) 4733101bb229SThomas Graf return RT_SCOPE_HOST; 4734101bb229SThomas Graf else if (ifa_scope & IFA_LINK) 4735101bb229SThomas Graf return RT_SCOPE_LINK; 4736101bb229SThomas Graf else if (ifa_scope & IFA_SITE) 4737101bb229SThomas Graf return RT_SCOPE_SITE; 4738101bb229SThomas Graf else 4739101bb229SThomas Graf return RT_SCOPE_UNIVERSE; 4740101bb229SThomas Graf } 4741101bb229SThomas Graf 47420ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void) 47430ab6803bSThomas Graf { 4744339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifaddrmsg)) 4745caeaba79SNicolas Dichtel + nla_total_size(16) /* IFA_LOCAL */ 4746339bf98fSThomas Graf + nla_total_size(16) /* IFA_ADDRESS */ 4747479840ffSJiri Pirko + nla_total_size(sizeof(struct ifa_cacheinfo)) 4748479840ffSJiri Pirko + nla_total_size(4) /* IFA_FLAGS */; 47490ab6803bSThomas Graf } 4750c5396a31SYOSHIFUJI Hideaki 47511da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa, 475215e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 47531da177e4SLinus Torvalds { 47541da177e4SLinus Torvalds struct nlmsghdr *nlh; 475585486af0SThomas Graf u32 preferred, valid; 47561da177e4SLinus Torvalds 475715e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 475863159f29SIan Morris if (!nlh) 475926932566SPatrick McHardy return -EMSGSIZE; 47600ab6803bSThomas Graf 4761101bb229SThomas Graf put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope), 4762101bb229SThomas Graf ifa->idev->dev->ifindex); 4763101bb229SThomas Graf 4764fad8da3eSYasushi Asano if (!((ifa->flags&IFA_F_PERMANENT) && 4765fad8da3eSYasushi Asano (ifa->prefered_lft == INFINITY_LIFE_TIME))) { 476685486af0SThomas Graf preferred = ifa->prefered_lft; 476785486af0SThomas Graf valid = ifa->valid_lft; 476885486af0SThomas Graf if (preferred != INFINITY_LIFE_TIME) { 47691da177e4SLinus Torvalds long tval = (jiffies - ifa->tstamp)/HZ; 4770a1faa698SJens Rosenboom if (preferred > tval) 477185486af0SThomas Graf preferred -= tval; 4772a1faa698SJens Rosenboom else 4773a1faa698SJens Rosenboom preferred = 0; 4774f56619fcSBen Hutchings if (valid != INFINITY_LIFE_TIME) { 4775f56619fcSBen Hutchings if (valid > tval) 477685486af0SThomas Graf valid -= tval; 4777f56619fcSBen Hutchings else 4778f56619fcSBen Hutchings valid = 0; 4779f56619fcSBen Hutchings } 47801da177e4SLinus Torvalds } 47811da177e4SLinus Torvalds } else { 478285486af0SThomas Graf preferred = INFINITY_LIFE_TIME; 478385486af0SThomas Graf valid = INFINITY_LIFE_TIME; 47841da177e4SLinus Torvalds } 478585486af0SThomas Graf 47867996c799SCong Wang if (!ipv6_addr_any(&ifa->peer_addr)) { 4787930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 || 4788930345eaSJiri Benc nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0) 4789caeaba79SNicolas Dichtel goto error; 4790caeaba79SNicolas Dichtel } else 4791930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0) 4792caeaba79SNicolas Dichtel goto error; 4793caeaba79SNicolas Dichtel 4794caeaba79SNicolas Dichtel if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) 4795caeaba79SNicolas Dichtel goto error; 479685486af0SThomas Graf 4797479840ffSJiri Pirko if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0) 4798479840ffSJiri Pirko goto error; 4799479840ffSJiri Pirko 4800053c095aSJohannes Berg nlmsg_end(skb, nlh); 4801053c095aSJohannes Berg return 0; 4802caeaba79SNicolas Dichtel 4803caeaba79SNicolas Dichtel error: 4804caeaba79SNicolas Dichtel nlmsg_cancel(skb, nlh); 4805caeaba79SNicolas Dichtel return -EMSGSIZE; 48061da177e4SLinus Torvalds } 48071da177e4SLinus Torvalds 48081da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca, 480915e47304SEric W. Biederman u32 portid, u32 seq, int event, u16 flags) 48101da177e4SLinus Torvalds { 48111da177e4SLinus Torvalds struct nlmsghdr *nlh; 4812101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 4813101bb229SThomas Graf int ifindex = ifmca->idev->dev->ifindex; 48141da177e4SLinus Torvalds 48151da177e4SLinus Torvalds if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE) 4816101bb229SThomas Graf scope = RT_SCOPE_SITE; 4817101bb229SThomas Graf 481815e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 481963159f29SIan Morris if (!nlh) 482026932566SPatrick McHardy return -EMSGSIZE; 48210ab6803bSThomas Graf 4822101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 4823930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 || 48240ab6803bSThomas Graf put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp, 482526932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 482626932566SPatrick McHardy nlmsg_cancel(skb, nlh); 482726932566SPatrick McHardy return -EMSGSIZE; 482826932566SPatrick McHardy } 482985486af0SThomas Graf 4830053c095aSJohannes Berg nlmsg_end(skb, nlh); 4831053c095aSJohannes Berg return 0; 48321da177e4SLinus Torvalds } 48331da177e4SLinus Torvalds 48341da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca, 483515e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 48361da177e4SLinus Torvalds { 48379ee8cbb2SDavid Ahern struct net_device *dev = fib6_info_nh_dev(ifaca->aca_rt); 48389ee8cbb2SDavid Ahern int ifindex = dev ? dev->ifindex : 1; 48391da177e4SLinus Torvalds struct nlmsghdr *nlh; 4840101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 48411da177e4SLinus Torvalds 48421da177e4SLinus Torvalds if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE) 4843101bb229SThomas Graf scope = RT_SCOPE_SITE; 4844101bb229SThomas Graf 484515e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 484663159f29SIan Morris if (!nlh) 484726932566SPatrick McHardy return -EMSGSIZE; 48480ab6803bSThomas Graf 4849101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 4850930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 || 48510ab6803bSThomas Graf put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp, 485226932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 485326932566SPatrick McHardy nlmsg_cancel(skb, nlh); 485426932566SPatrick McHardy return -EMSGSIZE; 485526932566SPatrick McHardy } 485685486af0SThomas Graf 4857053c095aSJohannes Berg nlmsg_end(skb, nlh); 4858053c095aSJohannes Berg return 0; 48591da177e4SLinus Torvalds } 48601da177e4SLinus Torvalds 4861e21e8467SStephen Hemminger enum addr_type_t { 48621da177e4SLinus Torvalds UNICAST_ADDR, 48631da177e4SLinus Torvalds MULTICAST_ADDR, 48641da177e4SLinus Torvalds ANYCAST_ADDR, 48651da177e4SLinus Torvalds }; 48661da177e4SLinus Torvalds 4867234b27c3SEric Dumazet /* called with rcu_read_lock() */ 4868234b27c3SEric Dumazet static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb, 4869234b27c3SEric Dumazet struct netlink_callback *cb, enum addr_type_t type, 4870234b27c3SEric Dumazet int s_ip_idx, int *p_ip_idx) 48711da177e4SLinus Torvalds { 48721da177e4SLinus Torvalds struct ifmcaddr6 *ifmca; 48731da177e4SLinus Torvalds struct ifacaddr6 *ifaca; 4874234b27c3SEric Dumazet int err = 1; 4875234b27c3SEric Dumazet int ip_idx = *p_ip_idx; 48761da177e4SLinus Torvalds 48771da177e4SLinus Torvalds read_lock_bh(&idev->lock); 48781da177e4SLinus Torvalds switch (type) { 4879502a2ffdSstephen hemminger case UNICAST_ADDR: { 4880502a2ffdSstephen hemminger struct inet6_ifaddr *ifa; 4881502a2ffdSstephen hemminger 4882ae9cda5dSYOSHIFUJI Hideaki /* unicast address incl. temp addr */ 4883502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 4884502a2ffdSstephen hemminger if (++ip_idx < s_ip_idx) 48851da177e4SLinus Torvalds continue; 48865d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifaddr(skb, ifa, 488715e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 48885d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 48895d5619b4SYOSHIFUJI Hideaki RTM_NEWADDR, 48905d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 4891053c095aSJohannes Berg if (err < 0) 4892bcd32326SEric Dumazet break; 489363998ac2SNicolas Dichtel nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 48941da177e4SLinus Torvalds } 48951da177e4SLinus Torvalds break; 4896502a2ffdSstephen hemminger } 48971da177e4SLinus Torvalds case MULTICAST_ADDR: 48981da177e4SLinus Torvalds /* multicast address */ 48991da177e4SLinus Torvalds for (ifmca = idev->mc_list; ifmca; 49001da177e4SLinus Torvalds ifmca = ifmca->next, ip_idx++) { 49011da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 49021da177e4SLinus Torvalds continue; 49035d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifmcaddr(skb, ifmca, 490415e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 49055d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 49065d5619b4SYOSHIFUJI Hideaki RTM_GETMULTICAST, 49075d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 4908053c095aSJohannes Berg if (err < 0) 4909bcd32326SEric Dumazet break; 49101da177e4SLinus Torvalds } 49111da177e4SLinus Torvalds break; 49121da177e4SLinus Torvalds case ANYCAST_ADDR: 49131da177e4SLinus Torvalds /* anycast address */ 49141da177e4SLinus Torvalds for (ifaca = idev->ac_list; ifaca; 49151da177e4SLinus Torvalds ifaca = ifaca->aca_next, ip_idx++) { 49161da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 49171da177e4SLinus Torvalds continue; 49185d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifacaddr(skb, ifaca, 491915e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 49205d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 49215d5619b4SYOSHIFUJI Hideaki RTM_GETANYCAST, 49225d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 4923053c095aSJohannes Berg if (err < 0) 4924bcd32326SEric Dumazet break; 49251da177e4SLinus Torvalds } 49261da177e4SLinus Torvalds break; 49271da177e4SLinus Torvalds default: 49281da177e4SLinus Torvalds break; 49291da177e4SLinus Torvalds } 49301da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 4931234b27c3SEric Dumazet *p_ip_idx = ip_idx; 4932234b27c3SEric Dumazet return err; 4933234b27c3SEric Dumazet } 49345d5619b4SYOSHIFUJI Hideaki 4935234b27c3SEric Dumazet static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb, 4936234b27c3SEric Dumazet enum addr_type_t type) 4937234b27c3SEric Dumazet { 4938234b27c3SEric Dumazet struct net *net = sock_net(skb->sk); 4939234b27c3SEric Dumazet int h, s_h; 4940234b27c3SEric Dumazet int idx, ip_idx; 4941234b27c3SEric Dumazet int s_idx, s_ip_idx; 4942234b27c3SEric Dumazet struct net_device *dev; 4943234b27c3SEric Dumazet struct inet6_dev *idev; 4944234b27c3SEric Dumazet struct hlist_head *head; 4945234b27c3SEric Dumazet 4946234b27c3SEric Dumazet s_h = cb->args[0]; 4947234b27c3SEric Dumazet s_idx = idx = cb->args[1]; 4948234b27c3SEric Dumazet s_ip_idx = ip_idx = cb->args[2]; 4949234b27c3SEric Dumazet 4950234b27c3SEric Dumazet rcu_read_lock(); 495163998ac2SNicolas Dichtel cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq; 4952234b27c3SEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 4953234b27c3SEric Dumazet idx = 0; 4954234b27c3SEric Dumazet head = &net->dev_index_head[h]; 4955b67bfe0dSSasha Levin hlist_for_each_entry_rcu(dev, head, index_hlist) { 4956234b27c3SEric Dumazet if (idx < s_idx) 4957234b27c3SEric Dumazet goto cont; 49584b97efdfSPatrick McHardy if (h > s_h || idx > s_idx) 4959234b27c3SEric Dumazet s_ip_idx = 0; 4960234b27c3SEric Dumazet ip_idx = 0; 4961e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 4962e21e8467SStephen Hemminger if (!idev) 4963234b27c3SEric Dumazet goto cont; 4964234b27c3SEric Dumazet 4965234b27c3SEric Dumazet if (in6_dump_addrs(idev, skb, cb, type, 49667b46a644SDavid S. Miller s_ip_idx, &ip_idx) < 0) 4967234b27c3SEric Dumazet goto done; 49687562f876SPavel Emelianov cont: 49697562f876SPavel Emelianov idx++; 49701da177e4SLinus Torvalds } 4971234b27c3SEric Dumazet } 4972234b27c3SEric Dumazet done: 4973234b27c3SEric Dumazet rcu_read_unlock(); 4974234b27c3SEric Dumazet cb->args[0] = h; 4975234b27c3SEric Dumazet cb->args[1] = idx; 4976234b27c3SEric Dumazet cb->args[2] = ip_idx; 4977234b27c3SEric Dumazet 49781da177e4SLinus Torvalds return skb->len; 49791da177e4SLinus Torvalds } 49801da177e4SLinus Torvalds 49811da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) 49821da177e4SLinus Torvalds { 49831da177e4SLinus Torvalds enum addr_type_t type = UNICAST_ADDR; 4984b854272bSDenis V. Lunev 49851da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 49861da177e4SLinus Torvalds } 49871da177e4SLinus Torvalds 49881da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb) 49891da177e4SLinus Torvalds { 49901da177e4SLinus Torvalds enum addr_type_t type = MULTICAST_ADDR; 4991b854272bSDenis V. Lunev 49921da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 49931da177e4SLinus Torvalds } 49941da177e4SLinus Torvalds 49951da177e4SLinus Torvalds 49961da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb) 49971da177e4SLinus Torvalds { 49981da177e4SLinus Torvalds enum addr_type_t type = ANYCAST_ADDR; 4999b854272bSDenis V. Lunev 50001da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 50011da177e4SLinus Torvalds } 50021da177e4SLinus Torvalds 5003c21ef3e3SDavid Ahern static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh, 5004c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 50056c223828SNoriaki TAKAMIYA { 50063b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 50071b29fc2cSThomas Graf struct ifaddrmsg *ifm; 50081b29fc2cSThomas Graf struct nlattr *tb[IFA_MAX+1]; 5009caeaba79SNicolas Dichtel struct in6_addr *addr = NULL, *peer; 50106c223828SNoriaki TAKAMIYA struct net_device *dev = NULL; 50116c223828SNoriaki TAKAMIYA struct inet6_ifaddr *ifa; 50126c223828SNoriaki TAKAMIYA struct sk_buff *skb; 50136c223828SNoriaki TAKAMIYA int err; 50146c223828SNoriaki TAKAMIYA 5015fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy, 5016c21ef3e3SDavid Ahern extack); 50171b29fc2cSThomas Graf if (err < 0) 5018c24675f8SFlorian Westphal return err; 50196c223828SNoriaki TAKAMIYA 5020caeaba79SNicolas Dichtel addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer); 5021c24675f8SFlorian Westphal if (!addr) 5022c24675f8SFlorian Westphal return -EINVAL; 50231b29fc2cSThomas Graf 50241b29fc2cSThomas Graf ifm = nlmsg_data(nlh); 50256c223828SNoriaki TAKAMIYA if (ifm->ifa_index) 5026c24675f8SFlorian Westphal dev = dev_get_by_index(net, ifm->ifa_index); 50276c223828SNoriaki TAKAMIYA 5028e21e8467SStephen Hemminger ifa = ipv6_get_ifaddr(net, addr, dev, 1); 5029e21e8467SStephen Hemminger if (!ifa) { 50301b29fc2cSThomas Graf err = -EADDRNOTAVAIL; 50311b29fc2cSThomas Graf goto errout; 50326c223828SNoriaki TAKAMIYA } 50336c223828SNoriaki TAKAMIYA 5034e21e8467SStephen Hemminger skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL); 5035e21e8467SStephen Hemminger if (!skb) { 50361b29fc2cSThomas Graf err = -ENOBUFS; 50371b29fc2cSThomas Graf goto errout_ifa; 50381b29fc2cSThomas Graf } 50391b29fc2cSThomas Graf 504015e47304SEric W. Biederman err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid, 50416c223828SNoriaki TAKAMIYA nlh->nlmsg_seq, RTM_NEWADDR, 0); 504226932566SPatrick McHardy if (err < 0) { 504326932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 504426932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 504526932566SPatrick McHardy kfree_skb(skb); 504626932566SPatrick McHardy goto errout_ifa; 504726932566SPatrick McHardy } 504815e47304SEric W. Biederman err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 50491b29fc2cSThomas Graf errout_ifa: 50506c223828SNoriaki TAKAMIYA in6_ifa_put(ifa); 50511b29fc2cSThomas Graf errout: 5052c24675f8SFlorian Westphal if (dev) 5053c24675f8SFlorian Westphal dev_put(dev); 50546c223828SNoriaki TAKAMIYA return err; 50556c223828SNoriaki TAKAMIYA } 50566c223828SNoriaki TAKAMIYA 50571da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa) 50581da177e4SLinus Torvalds { 50591da177e4SLinus Torvalds struct sk_buff *skb; 5060c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(ifa->idev->dev); 50615d620266SThomas Graf int err = -ENOBUFS; 50621da177e4SLinus Torvalds 50630ab6803bSThomas Graf skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC); 506463159f29SIan Morris if (!skb) 50655d620266SThomas Graf goto errout; 50665d620266SThomas Graf 50675d620266SThomas Graf err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0); 506826932566SPatrick McHardy if (err < 0) { 506926932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 507026932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 507126932566SPatrick McHardy kfree_skb(skb); 507226932566SPatrick McHardy goto errout; 507326932566SPatrick McHardy } 50741ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC); 50751ce85fe4SPablo Neira Ayuso return; 50765d620266SThomas Graf errout: 50775d620266SThomas Graf if (err < 0) 50786fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err); 50791da177e4SLinus Torvalds } 50801da177e4SLinus Torvalds 5081b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf, 50821da177e4SLinus Torvalds __s32 *array, int bytes) 50831da177e4SLinus Torvalds { 508404561c1fSThomas Graf BUG_ON(bytes < (DEVCONF_MAX * 4)); 508504561c1fSThomas Graf 50861da177e4SLinus Torvalds memset(array, 0, bytes); 50871da177e4SLinus Torvalds array[DEVCONF_FORWARDING] = cnf->forwarding; 50881da177e4SLinus Torvalds array[DEVCONF_HOPLIMIT] = cnf->hop_limit; 50891da177e4SLinus Torvalds array[DEVCONF_MTU6] = cnf->mtu6; 50901da177e4SLinus Torvalds array[DEVCONF_ACCEPT_RA] = cnf->accept_ra; 50911da177e4SLinus Torvalds array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects; 50921da177e4SLinus Torvalds array[DEVCONF_AUTOCONF] = cnf->autoconf; 50931da177e4SLinus Torvalds array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits; 50941da177e4SLinus Torvalds array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits; 509593908d19SThomas Graf array[DEVCONF_RTR_SOLICIT_INTERVAL] = 509693908d19SThomas Graf jiffies_to_msecs(cnf->rtr_solicit_interval); 5097bd11f074SMaciej Żenczykowski array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] = 5098bd11f074SMaciej Żenczykowski jiffies_to_msecs(cnf->rtr_solicit_max_interval); 509993908d19SThomas Graf array[DEVCONF_RTR_SOLICIT_DELAY] = 510093908d19SThomas Graf jiffies_to_msecs(cnf->rtr_solicit_delay); 51011da177e4SLinus Torvalds array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version; 5102fc4eba58SHannes Frederic Sowa array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] = 5103fc4eba58SHannes Frederic Sowa jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval); 5104fc4eba58SHannes Frederic Sowa array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] = 5105fc4eba58SHannes Frederic Sowa jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval); 51061da177e4SLinus Torvalds array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr; 51071da177e4SLinus Torvalds array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft; 51081da177e4SLinus Torvalds array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft; 51091da177e4SLinus Torvalds array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry; 51101da177e4SLinus Torvalds array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor; 51111da177e4SLinus Torvalds array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses; 511265f5c7c1SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr; 51138013d1d7SHangbin Liu array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit; 5114c4fd30ebSYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo; 5115930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 5116930d6ff2SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref; 511793908d19SThomas Graf array[DEVCONF_RTR_PROBE_INTERVAL] = 511893908d19SThomas Graf jiffies_to_msecs(cnf->rtr_probe_interval); 5119fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 5120bbea124bSJoel Scherpelz array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] = cnf->accept_ra_rt_info_min_plen; 512109c884d4SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen; 512209c884d4SYOSHIFUJI Hideaki #endif 5123930d6ff2SYOSHIFUJI Hideaki #endif 5124fbea49e1SYOSHIFUJI Hideaki array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp; 51250bcbc926SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route; 512695c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 512795c385b4SNeil Horman array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad; 51287fd2561eSErik Kline array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic; 512995c385b4SNeil Horman #endif 51307bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 51317bc570c8SYOSHIFUJI Hideaki array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding; 51327bc570c8SYOSHIFUJI Hideaki #endif 5133778d80beSYOSHIFUJI Hideaki array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6; 51341b34be74SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad; 5135c3faca05SCosmin Ratiu array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao; 51365cb04436SHannes Frederic Sowa array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify; 5137b800c3b9SHannes Frederic Sowa array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc; 5138d9333196SBen Greear array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local; 5139c2943f14SHarout Hedeshian array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu; 514035103d11SAndy Gospodarek array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown; 51413d1bec99SHannes Frederic Sowa /* we omit DEVCONF_STABLE_SECRET for now */ 51423985e8a3SErik Kline array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only; 5143abbc3043SJohannes Berg array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast; 51447a02bf89SJohannes Berg array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na; 5145f1705ec1SDavid Ahern array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down; 51461ababebaSDavid Lebrun array[DEVCONF_SEG6_ENABLED] = cnf->seg6_enabled; 5147bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC 5148bf355b8dSDavid Lebrun array[DEVCONF_SEG6_REQUIRE_HMAC] = cnf->seg6_require_hmac; 5149bf355b8dSDavid Lebrun #endif 5150adc176c5SErik Nordmark array[DEVCONF_ENHANCED_DAD] = cnf->enhanced_dad; 5151d35a00b8SFelix Jia array[DEVCONF_ADDR_GEN_MODE] = cnf->addr_gen_mode; 5152df789fe7SDavid Forster array[DEVCONF_DISABLE_POLICY] = cnf->disable_policy; 51532210d6b2SMaciej Żenczykowski array[DEVCONF_NDISC_TCLASS] = cnf->ndisc_tclass; 51541da177e4SLinus Torvalds } 51551da177e4SLinus Torvalds 5156b382b191SThomas Graf static inline size_t inet6_ifla6_size(void) 5157b382b191SThomas Graf { 5158b382b191SThomas Graf return nla_total_size(4) /* IFLA_INET6_FLAGS */ 5159b382b191SThomas Graf + nla_total_size(sizeof(struct ifla_cacheinfo)) 5160b382b191SThomas Graf + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */ 5161b382b191SThomas Graf + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */ 5162f53adae4SDaniel Borkmann + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */ 5163f53adae4SDaniel Borkmann + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */ 5164b382b191SThomas Graf } 5165b382b191SThomas Graf 5166339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void) 5167339bf98fSThomas Graf { 5168339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 5169339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 5170339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 5171339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 5172339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 51730344338bSAndy Gospodarek + nla_total_size(1) /* IFLA_OPERSTATE */ 5174b382b191SThomas Graf + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */ 5175339bf98fSThomas Graf } 5176c5396a31SYOSHIFUJI Hideaki 5177be281e55SEric Dumazet static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib, 5178aca05671SJia He int bytes) 51797f7d9a6bSHerbert Xu { 51807f7d9a6bSHerbert Xu int i; 5181aca05671SJia He int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX; 51827f7d9a6bSHerbert Xu BUG_ON(pad < 0); 51837f7d9a6bSHerbert Xu 51847f7d9a6bSHerbert Xu /* Use put_unaligned() because stats may not be aligned for u64. */ 5185aca05671SJia He put_unaligned(ICMP6_MIB_MAX, &stats[0]); 5186aca05671SJia He for (i = 1; i < ICMP6_MIB_MAX; i++) 5187be281e55SEric Dumazet put_unaligned(atomic_long_read(&mib[i]), &stats[i]); 51887f7d9a6bSHerbert Xu 5189aca05671SJia He memset(&stats[ICMP6_MIB_MAX], 0, pad); 51907f7d9a6bSHerbert Xu } 51917f7d9a6bSHerbert Xu 5192698365faSWANG Cong static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib, 5193a3a77372SRaghavendra K T int bytes, size_t syncpoff) 51944ce3c183SEric Dumazet { 5195a3a77372SRaghavendra K T int i, c; 5196a3a77372SRaghavendra K T u64 buff[IPSTATS_MIB_MAX]; 5197a3a77372SRaghavendra K T int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX; 5198a3a77372SRaghavendra K T 51994ce3c183SEric Dumazet BUG_ON(pad < 0); 52004ce3c183SEric Dumazet 5201a3a77372SRaghavendra K T memset(buff, 0, sizeof(buff)); 5202a3a77372SRaghavendra K T buff[0] = IPSTATS_MIB_MAX; 52034ce3c183SEric Dumazet 5204a3a77372SRaghavendra K T for_each_possible_cpu(c) { 5205a3a77372SRaghavendra K T for (i = 1; i < IPSTATS_MIB_MAX; i++) 5206a3a77372SRaghavendra K T buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff); 5207a3a77372SRaghavendra K T } 5208a3a77372SRaghavendra K T 5209a3a77372SRaghavendra K T memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64)); 5210a3a77372SRaghavendra K T memset(&stats[IPSTATS_MIB_MAX], 0, pad); 52114ce3c183SEric Dumazet } 52124ce3c183SEric Dumazet 52137f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype, 52147f7d9a6bSHerbert Xu int bytes) 52157f7d9a6bSHerbert Xu { 52167f7d9a6bSHerbert Xu switch (attrtype) { 52177f7d9a6bSHerbert Xu case IFLA_INET6_STATS: 5218a3a77372SRaghavendra K T __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes, 5219a3a77372SRaghavendra K T offsetof(struct ipstats_mib, syncp)); 52207f7d9a6bSHerbert Xu break; 52217f7d9a6bSHerbert Xu case IFLA_INET6_ICMP6STATS: 5222aca05671SJia He __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes); 52237f7d9a6bSHerbert Xu break; 52247f7d9a6bSHerbert Xu } 52257f7d9a6bSHerbert Xu } 52267f7d9a6bSHerbert Xu 5227d5566fd7SSowmini Varadhan static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev, 5228d5566fd7SSowmini Varadhan u32 ext_filter_mask) 52291da177e4SLinus Torvalds { 5230bf99f1bdSYOSHIFUJI Hideaki struct nlattr *nla; 52311da177e4SLinus Torvalds struct ifla_cacheinfo ci; 52321da177e4SLinus Torvalds 5233c78679e8SDavid S. Miller if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags)) 5234c78679e8SDavid S. Miller goto nla_put_failure; 52351da177e4SLinus Torvalds ci.max_reasm_len = IPV6_MAXPLEN; 523624912420SDavid S. Miller ci.tstamp = cstamp_delta(idev->tstamp); 523724912420SDavid S. Miller ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time); 52381f9248e5SJiri Pirko ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME)); 5239c78679e8SDavid S. Miller if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci)) 5240c78679e8SDavid S. Miller goto nla_put_failure; 5241bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32)); 524263159f29SIan Morris if (!nla) 524304561c1fSThomas Graf goto nla_put_failure; 5244bf99f1bdSYOSHIFUJI Hideaki ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla)); 52451da177e4SLinus Torvalds 5246bf99f1bdSYOSHIFUJI Hideaki /* XXX - MC not implemented */ 5247bf99f1bdSYOSHIFUJI Hideaki 5248d5566fd7SSowmini Varadhan if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS) 5249d5566fd7SSowmini Varadhan return 0; 5250d5566fd7SSowmini Varadhan 5251bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64)); 525263159f29SIan Morris if (!nla) 5253bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 5254bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla)); 5255bf99f1bdSYOSHIFUJI Hideaki 5256bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64)); 525763159f29SIan Morris if (!nla) 5258bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 5259bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla)); 52601da177e4SLinus Torvalds 5261f53adae4SDaniel Borkmann nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr)); 526263159f29SIan Morris if (!nla) 5263f53adae4SDaniel Borkmann goto nla_put_failure; 5264bc91b0f0SJiri Pirko 5265d35a00b8SFelix Jia if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->cnf.addr_gen_mode)) 5266bc91b0f0SJiri Pirko goto nla_put_failure; 5267bc91b0f0SJiri Pirko 5268f53adae4SDaniel Borkmann read_lock_bh(&idev->lock); 5269f53adae4SDaniel Borkmann memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla)); 5270f53adae4SDaniel Borkmann read_unlock_bh(&idev->lock); 5271f53adae4SDaniel Borkmann 5272b382b191SThomas Graf return 0; 5273b382b191SThomas Graf 5274b382b191SThomas Graf nla_put_failure: 5275b382b191SThomas Graf return -EMSGSIZE; 5276b382b191SThomas Graf } 5277b382b191SThomas Graf 5278b1974ed0SArad, Ronen static size_t inet6_get_link_af_size(const struct net_device *dev, 5279b1974ed0SArad, Ronen u32 ext_filter_mask) 5280b382b191SThomas Graf { 5281b382b191SThomas Graf if (!__in6_dev_get(dev)) 5282b382b191SThomas Graf return 0; 5283b382b191SThomas Graf 5284b382b191SThomas Graf return inet6_ifla6_size(); 5285b382b191SThomas Graf } 5286b382b191SThomas Graf 5287d5566fd7SSowmini Varadhan static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev, 5288d5566fd7SSowmini Varadhan u32 ext_filter_mask) 5289b382b191SThomas Graf { 5290b382b191SThomas Graf struct inet6_dev *idev = __in6_dev_get(dev); 5291b382b191SThomas Graf 5292b382b191SThomas Graf if (!idev) 5293b382b191SThomas Graf return -ENODATA; 5294b382b191SThomas Graf 5295d5566fd7SSowmini Varadhan if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0) 5296b382b191SThomas Graf return -EMSGSIZE; 5297b382b191SThomas Graf 5298b382b191SThomas Graf return 0; 5299b382b191SThomas Graf } 5300b382b191SThomas Graf 5301f53adae4SDaniel Borkmann static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token) 5302f53adae4SDaniel Borkmann { 5303f53adae4SDaniel Borkmann struct inet6_ifaddr *ifp; 5304f53adae4SDaniel Borkmann struct net_device *dev = idev->dev; 530547e27d5eSDaniel Borkmann bool clear_token, update_rs = false; 5306dc848292SHannes Frederic Sowa struct in6_addr ll_addr; 5307f53adae4SDaniel Borkmann 5308c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 5309c15b1ccaSHannes Frederic Sowa 531063159f29SIan Morris if (!token) 5311f53adae4SDaniel Borkmann return -EINVAL; 5312f53adae4SDaniel Borkmann if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) 5313f53adae4SDaniel Borkmann return -EINVAL; 5314f53adae4SDaniel Borkmann if (!ipv6_accept_ra(idev)) 5315f53adae4SDaniel Borkmann return -EINVAL; 5316bd11f074SMaciej Żenczykowski if (idev->cnf.rtr_solicits == 0) 5317f53adae4SDaniel Borkmann return -EINVAL; 5318f53adae4SDaniel Borkmann 5319f53adae4SDaniel Borkmann write_lock_bh(&idev->lock); 5320f53adae4SDaniel Borkmann 5321f53adae4SDaniel Borkmann BUILD_BUG_ON(sizeof(token->s6_addr) != 16); 5322f53adae4SDaniel Borkmann memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8); 5323f53adae4SDaniel Borkmann 5324f53adae4SDaniel Borkmann write_unlock_bh(&idev->lock); 5325f53adae4SDaniel Borkmann 532647e27d5eSDaniel Borkmann clear_token = ipv6_addr_any(token); 532747e27d5eSDaniel Borkmann if (clear_token) 532847e27d5eSDaniel Borkmann goto update_lft; 532947e27d5eSDaniel Borkmann 5330dc848292SHannes Frederic Sowa if (!idev->dead && (idev->if_flags & IF_READY) && 5331dc848292SHannes Frederic Sowa !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE | 5332dc848292SHannes Frederic Sowa IFA_F_OPTIMISTIC)) { 5333fc403832SDaniel Borkmann /* If we're not ready, then normal ifup will take care 5334fc403832SDaniel Borkmann * of this. Otherwise, we need to request our rs here. 5335fc403832SDaniel Borkmann */ 5336f53adae4SDaniel Borkmann ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters); 5337fc403832SDaniel Borkmann update_rs = true; 5338fc403832SDaniel Borkmann } 5339f53adae4SDaniel Borkmann 534047e27d5eSDaniel Borkmann update_lft: 5341f53adae4SDaniel Borkmann write_lock_bh(&idev->lock); 5342fc403832SDaniel Borkmann 534377ecaaceSHannes Frederic Sowa if (update_rs) { 5344f53adae4SDaniel Borkmann idev->if_flags |= IF_RS_SENT; 5345bd11f074SMaciej Żenczykowski idev->rs_interval = rfc3315_s14_backoff_init( 5346bd11f074SMaciej Żenczykowski idev->cnf.rtr_solicit_interval); 534777ecaaceSHannes Frederic Sowa idev->rs_probes = 1; 5348bd11f074SMaciej Żenczykowski addrconf_mod_rs_timer(idev, idev->rs_interval); 534977ecaaceSHannes Frederic Sowa } 5350f53adae4SDaniel Borkmann 5351f53adae4SDaniel Borkmann /* Well, that's kinda nasty ... */ 5352f53adae4SDaniel Borkmann list_for_each_entry(ifp, &idev->addr_list, if_list) { 5353f53adae4SDaniel Borkmann spin_lock(&ifp->lock); 5354617fe29dSDaniel Borkmann if (ifp->tokenized) { 5355f53adae4SDaniel Borkmann ifp->valid_lft = 0; 5356f53adae4SDaniel Borkmann ifp->prefered_lft = 0; 5357f53adae4SDaniel Borkmann } 5358f53adae4SDaniel Borkmann spin_unlock(&ifp->lock); 5359f53adae4SDaniel Borkmann } 5360f53adae4SDaniel Borkmann 5361f53adae4SDaniel Borkmann write_unlock_bh(&idev->lock); 5362b2ed64a9SLubomir Rintel inet6_ifinfo_notify(RTM_NEWLINK, idev); 5363c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 5364f53adae4SDaniel Borkmann return 0; 5365f53adae4SDaniel Borkmann } 5366f53adae4SDaniel Borkmann 536711b1f828SDaniel Borkmann static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = { 536811b1f828SDaniel Borkmann [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 }, 536911b1f828SDaniel Borkmann [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) }, 537011b1f828SDaniel Borkmann }; 537111b1f828SDaniel Borkmann 537211b1f828SDaniel Borkmann static int inet6_validate_link_af(const struct net_device *dev, 537311b1f828SDaniel Borkmann const struct nlattr *nla) 537411b1f828SDaniel Borkmann { 537511b1f828SDaniel Borkmann struct nlattr *tb[IFLA_INET6_MAX + 1]; 537611b1f828SDaniel Borkmann 537711b1f828SDaniel Borkmann if (dev && !__in6_dev_get(dev)) 537811b1f828SDaniel Borkmann return -EAFNOSUPPORT; 537911b1f828SDaniel Borkmann 5380fceb6435SJohannes Berg return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy, 5381fceb6435SJohannes Berg NULL); 538211b1f828SDaniel Borkmann } 538311b1f828SDaniel Borkmann 5384d35a00b8SFelix Jia static int check_addr_gen_mode(int mode) 5385d35a00b8SFelix Jia { 5386d35a00b8SFelix Jia if (mode != IN6_ADDR_GEN_MODE_EUI64 && 5387d35a00b8SFelix Jia mode != IN6_ADDR_GEN_MODE_NONE && 5388d35a00b8SFelix Jia mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY && 5389d35a00b8SFelix Jia mode != IN6_ADDR_GEN_MODE_RANDOM) 5390d35a00b8SFelix Jia return -EINVAL; 5391d35a00b8SFelix Jia return 1; 5392d35a00b8SFelix Jia } 5393d35a00b8SFelix Jia 5394d35a00b8SFelix Jia static int check_stable_privacy(struct inet6_dev *idev, struct net *net, 5395d35a00b8SFelix Jia int mode) 5396d35a00b8SFelix Jia { 5397d35a00b8SFelix Jia if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY && 5398d35a00b8SFelix Jia !idev->cnf.stable_secret.initialized && 5399d35a00b8SFelix Jia !net->ipv6.devconf_dflt->stable_secret.initialized) 5400d35a00b8SFelix Jia return -EINVAL; 5401d35a00b8SFelix Jia return 1; 5402d35a00b8SFelix Jia } 5403d35a00b8SFelix Jia 5404f53adae4SDaniel Borkmann static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla) 5405f53adae4SDaniel Borkmann { 5406f53adae4SDaniel Borkmann int err = -EINVAL; 5407f53adae4SDaniel Borkmann struct inet6_dev *idev = __in6_dev_get(dev); 5408f53adae4SDaniel Borkmann struct nlattr *tb[IFLA_INET6_MAX + 1]; 5409f53adae4SDaniel Borkmann 5410f53adae4SDaniel Borkmann if (!idev) 5411f53adae4SDaniel Borkmann return -EAFNOSUPPORT; 5412f53adae4SDaniel Borkmann 5413fceb6435SJohannes Berg if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL, NULL) < 0) 5414f53adae4SDaniel Borkmann BUG(); 5415f53adae4SDaniel Borkmann 5416bc91b0f0SJiri Pirko if (tb[IFLA_INET6_TOKEN]) { 5417f53adae4SDaniel Borkmann err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN])); 5418bc91b0f0SJiri Pirko if (err) 5419bc91b0f0SJiri Pirko return err; 5420bc91b0f0SJiri Pirko } 5421bc91b0f0SJiri Pirko 5422bc91b0f0SJiri Pirko if (tb[IFLA_INET6_ADDR_GEN_MODE]) { 5423bc91b0f0SJiri Pirko u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]); 5424bc91b0f0SJiri Pirko 5425d35a00b8SFelix Jia if (check_addr_gen_mode(mode) < 0 || 5426d35a00b8SFelix Jia check_stable_privacy(idev, dev_net(dev), mode) < 0) 5427bc91b0f0SJiri Pirko return -EINVAL; 5428622c81d5SHannes Frederic Sowa 5429d35a00b8SFelix Jia idev->cnf.addr_gen_mode = mode; 5430bc91b0f0SJiri Pirko err = 0; 5431bc91b0f0SJiri Pirko } 5432f53adae4SDaniel Borkmann 5433f53adae4SDaniel Borkmann return err; 5434f53adae4SDaniel Borkmann } 5435f53adae4SDaniel Borkmann 5436b382b191SThomas Graf static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev, 543715e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 5438b382b191SThomas Graf { 5439b382b191SThomas Graf struct net_device *dev = idev->dev; 5440b382b191SThomas Graf struct ifinfomsg *hdr; 5441b382b191SThomas Graf struct nlmsghdr *nlh; 5442b382b191SThomas Graf void *protoinfo; 5443b382b191SThomas Graf 544415e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags); 544563159f29SIan Morris if (!nlh) 5446b382b191SThomas Graf return -EMSGSIZE; 5447b382b191SThomas Graf 5448b382b191SThomas Graf hdr = nlmsg_data(nlh); 5449b382b191SThomas Graf hdr->ifi_family = AF_INET6; 5450b382b191SThomas Graf hdr->__ifi_pad = 0; 5451b382b191SThomas Graf hdr->ifi_type = dev->type; 5452b382b191SThomas Graf hdr->ifi_index = dev->ifindex; 5453b382b191SThomas Graf hdr->ifi_flags = dev_get_flags(dev); 5454b382b191SThomas Graf hdr->ifi_change = 0; 5455b382b191SThomas Graf 5456c78679e8SDavid S. Miller if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 5457c78679e8SDavid S. Miller (dev->addr_len && 5458c78679e8SDavid S. Miller nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 5459c78679e8SDavid S. Miller nla_put_u32(skb, IFLA_MTU, dev->mtu) || 5460a54acb3aSNicolas Dichtel (dev->ifindex != dev_get_iflink(dev) && 54610344338bSAndy Gospodarek nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) || 54620344338bSAndy Gospodarek nla_put_u8(skb, IFLA_OPERSTATE, 54630344338bSAndy Gospodarek netif_running(dev) ? dev->operstate : IF_OPER_DOWN)) 5464c78679e8SDavid S. Miller goto nla_put_failure; 5465b382b191SThomas Graf protoinfo = nla_nest_start(skb, IFLA_PROTINFO); 546663159f29SIan Morris if (!protoinfo) 5467b382b191SThomas Graf goto nla_put_failure; 5468b382b191SThomas Graf 5469d5566fd7SSowmini Varadhan if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0) 5470b382b191SThomas Graf goto nla_put_failure; 5471b382b191SThomas Graf 547204561c1fSThomas Graf nla_nest_end(skb, protoinfo); 5473053c095aSJohannes Berg nlmsg_end(skb, nlh); 5474053c095aSJohannes Berg return 0; 54751da177e4SLinus Torvalds 547604561c1fSThomas Graf nla_put_failure: 547726932566SPatrick McHardy nlmsg_cancel(skb, nlh); 547826932566SPatrick McHardy return -EMSGSIZE; 54791da177e4SLinus Torvalds } 54801da177e4SLinus Torvalds 54811da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 54821da177e4SLinus Torvalds { 54833b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 548484d2697dSEric Dumazet int h, s_h; 5485434a8a58SDavid S. Miller int idx = 0, s_idx; 54861da177e4SLinus Torvalds struct net_device *dev; 54871da177e4SLinus Torvalds struct inet6_dev *idev; 548884d2697dSEric Dumazet struct hlist_head *head; 54891da177e4SLinus Torvalds 549084d2697dSEric Dumazet s_h = cb->args[0]; 549184d2697dSEric Dumazet s_idx = cb->args[1]; 549284d2697dSEric Dumazet 549384d2697dSEric Dumazet rcu_read_lock(); 549484d2697dSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 54957562f876SPavel Emelianov idx = 0; 549684d2697dSEric Dumazet head = &net->dev_index_head[h]; 5497b67bfe0dSSasha Levin hlist_for_each_entry_rcu(dev, head, index_hlist) { 54981da177e4SLinus Torvalds if (idx < s_idx) 54997562f876SPavel Emelianov goto cont; 550084d2697dSEric Dumazet idev = __in6_dev_get(dev); 550184d2697dSEric Dumazet if (!idev) 55027562f876SPavel Emelianov goto cont; 550384d2697dSEric Dumazet if (inet6_fill_ifinfo(skb, idev, 550415e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 550584d2697dSEric Dumazet cb->nlh->nlmsg_seq, 5506053c095aSJohannes Berg RTM_NEWLINK, NLM_F_MULTI) < 0) 550784d2697dSEric Dumazet goto out; 55087562f876SPavel Emelianov cont: 55097562f876SPavel Emelianov idx++; 55101da177e4SLinus Torvalds } 551184d2697dSEric Dumazet } 551284d2697dSEric Dumazet out: 551384d2697dSEric Dumazet rcu_read_unlock(); 551484d2697dSEric Dumazet cb->args[1] = idx; 551584d2697dSEric Dumazet cb->args[0] = h; 55161da177e4SLinus Torvalds 55171da177e4SLinus Torvalds return skb->len; 55181da177e4SLinus Torvalds } 55191da177e4SLinus Torvalds 55201da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev) 55211da177e4SLinus Torvalds { 55221da177e4SLinus Torvalds struct sk_buff *skb; 5523c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 55248d7a76c9SThomas Graf int err = -ENOBUFS; 55251da177e4SLinus Torvalds 5526339bf98fSThomas Graf skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC); 552763159f29SIan Morris if (!skb) 55288d7a76c9SThomas Graf goto errout; 55298d7a76c9SThomas Graf 55308d7a76c9SThomas Graf err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0); 553126932566SPatrick McHardy if (err < 0) { 553226932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */ 553326932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 553426932566SPatrick McHardy kfree_skb(skb); 553526932566SPatrick McHardy goto errout; 553626932566SPatrick McHardy } 55375f75a104SNicolas Dichtel rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC); 55381ce85fe4SPablo Neira Ayuso return; 55398d7a76c9SThomas Graf errout: 55408d7a76c9SThomas Graf if (err < 0) 55415f75a104SNicolas Dichtel rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err); 55421da177e4SLinus Torvalds } 55431da177e4SLinus Torvalds 5544339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void) 5545339bf98fSThomas Graf { 5546339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct prefixmsg)) 5547339bf98fSThomas Graf + nla_total_size(sizeof(struct in6_addr)) 5548339bf98fSThomas Graf + nla_total_size(sizeof(struct prefix_cacheinfo)); 5549339bf98fSThomas Graf } 5550c5396a31SYOSHIFUJI Hideaki 55511da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev, 555215e47304SEric W. Biederman struct prefix_info *pinfo, u32 portid, u32 seq, 5553b6544c0bSJamal Hadi Salim int event, unsigned int flags) 55541da177e4SLinus Torvalds { 55551da177e4SLinus Torvalds struct prefixmsg *pmsg; 55561da177e4SLinus Torvalds struct nlmsghdr *nlh; 55571da177e4SLinus Torvalds struct prefix_cacheinfo ci; 55581da177e4SLinus Torvalds 555915e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags); 556063159f29SIan Morris if (!nlh) 556126932566SPatrick McHardy return -EMSGSIZE; 55626051e2f4SThomas Graf 55636051e2f4SThomas Graf pmsg = nlmsg_data(nlh); 55641da177e4SLinus Torvalds pmsg->prefix_family = AF_INET6; 55658a47077aSPatrick McHardy pmsg->prefix_pad1 = 0; 55668a47077aSPatrick McHardy pmsg->prefix_pad2 = 0; 55671da177e4SLinus Torvalds pmsg->prefix_ifindex = idev->dev->ifindex; 55681da177e4SLinus Torvalds pmsg->prefix_len = pinfo->prefix_len; 55691da177e4SLinus Torvalds pmsg->prefix_type = pinfo->type; 55708a47077aSPatrick McHardy pmsg->prefix_pad3 = 0; 55711da177e4SLinus Torvalds pmsg->prefix_flags = 0; 55721da177e4SLinus Torvalds if (pinfo->onlink) 55731da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_ONLINK; 55741da177e4SLinus Torvalds if (pinfo->autoconf) 55751da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_AUTOCONF; 55761da177e4SLinus Torvalds 5577c78679e8SDavid S. Miller if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix)) 5578c78679e8SDavid S. Miller goto nla_put_failure; 55791da177e4SLinus Torvalds ci.preferred_time = ntohl(pinfo->prefered); 55801da177e4SLinus Torvalds ci.valid_time = ntohl(pinfo->valid); 5581c78679e8SDavid S. Miller if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci)) 5582c78679e8SDavid S. Miller goto nla_put_failure; 5583053c095aSJohannes Berg nlmsg_end(skb, nlh); 5584053c095aSJohannes Berg return 0; 55851da177e4SLinus Torvalds 55866051e2f4SThomas Graf nla_put_failure: 558726932566SPatrick McHardy nlmsg_cancel(skb, nlh); 558826932566SPatrick McHardy return -EMSGSIZE; 55891da177e4SLinus Torvalds } 55901da177e4SLinus Torvalds 55911da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev, 55921da177e4SLinus Torvalds struct prefix_info *pinfo) 55931da177e4SLinus Torvalds { 55941da177e4SLinus Torvalds struct sk_buff *skb; 5595c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 55968c384bfaSThomas Graf int err = -ENOBUFS; 55971da177e4SLinus Torvalds 5598339bf98fSThomas Graf skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC); 559963159f29SIan Morris if (!skb) 56008c384bfaSThomas Graf goto errout; 56018c384bfaSThomas Graf 56028c384bfaSThomas Graf err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0); 560326932566SPatrick McHardy if (err < 0) { 560426932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */ 560526932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 560626932566SPatrick McHardy kfree_skb(skb); 560726932566SPatrick McHardy goto errout; 560826932566SPatrick McHardy } 56091ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC); 56101ce85fe4SPablo Neira Ayuso return; 56118c384bfaSThomas Graf errout: 56128c384bfaSThomas Graf if (err < 0) 56136fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err); 56141da177e4SLinus Torvalds } 56151da177e4SLinus Torvalds 56161da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 56171da177e4SLinus Torvalds { 561863998ac2SNicolas Dichtel struct net *net = dev_net(ifp->idev->dev); 561963998ac2SNicolas Dichtel 5620c15b1ccaSHannes Frederic Sowa if (event) 5621c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 5622c15b1ccaSHannes Frederic Sowa 56231da177e4SLinus Torvalds inet6_ifa_notify(event ? : RTM_NEWADDR, ifp); 56241da177e4SLinus Torvalds 56251da177e4SLinus Torvalds switch (event) { 56261da177e4SLinus Torvalds case RTM_NEWADDR: 562795c385b4SNeil Horman /* 562895c385b4SNeil Horman * If the address was optimistic 562995c385b4SNeil Horman * we inserted the route at the start of 563095c385b4SNeil Horman * our DAD process, so we don't need 563195c385b4SNeil Horman * to do it again 563295c385b4SNeil Horman */ 563393c2fb25SDavid Ahern if (!rcu_access_pointer(ifp->rt->fib6_node)) 5634afb1d4b5SDavid Ahern ip6_ins_rt(net, ifp->rt); 56351da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 56361da177e4SLinus Torvalds addrconf_join_anycast(ifp); 56377996c799SCong Wang if (!ipv6_addr_any(&ifp->peer_addr)) 5638caeaba79SNicolas Dichtel addrconf_prefix_route(&ifp->peer_addr, 128, 5639acb54e3cSDavid Ahern ifp->idev->dev, 0, 0, 564027b10608SDavid Ahern GFP_ATOMIC); 56411da177e4SLinus Torvalds break; 56421da177e4SLinus Torvalds case RTM_DELADDR: 56431da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 56441da177e4SLinus Torvalds addrconf_leave_anycast(ifp); 56451da177e4SLinus Torvalds addrconf_leave_solict(ifp->idev, &ifp->addr); 56467996c799SCong Wang if (!ipv6_addr_any(&ifp->peer_addr)) { 56478d1c802bSDavid Ahern struct fib6_info *rt; 5648caeaba79SNicolas Dichtel 5649e7478dfcSNicolas Dichtel rt = addrconf_get_prefix_route(&ifp->peer_addr, 128, 5650e7478dfcSNicolas Dichtel ifp->idev->dev, 0, 0); 56518e3d5be7SMartin KaFai Lau if (rt) 5652afb1d4b5SDavid Ahern ip6_del_rt(net, rt); 5653caeaba79SNicolas Dichtel } 565438bd10c4SDavid Ahern if (ifp->rt) { 5655afb1d4b5SDavid Ahern ip6_del_rt(net, ifp->rt); 565693531c67SDavid Ahern ifp->rt = NULL; 565738bd10c4SDavid Ahern } 5658705f1c86SHannes Frederic Sowa rt_genid_bump_ipv6(net); 56591da177e4SLinus Torvalds break; 56601da177e4SLinus Torvalds } 566163998ac2SNicolas Dichtel atomic_inc(&net->ipv6.dev_addr_genid); 56621da177e4SLinus Torvalds } 56631da177e4SLinus Torvalds 56641da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 56651da177e4SLinus Torvalds { 56668814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 56671da177e4SLinus Torvalds if (likely(ifp->idev->dead == 0)) 56681da177e4SLinus Torvalds __ipv6_ifa_notify(event, ifp); 56698814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 56701da177e4SLinus Torvalds } 56711da177e4SLinus Torvalds 56721da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 56731da177e4SLinus Torvalds 56741da177e4SLinus Torvalds static 5675fe2c6338SJoe Perches int addrconf_sysctl_forward(struct ctl_table *ctl, int write, 56761da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 56771da177e4SLinus Torvalds { 56781da177e4SLinus Torvalds int *valp = ctl->data; 56791da177e4SLinus Torvalds int val = *valp; 568088af182eSEric W. Biederman loff_t pos = *ppos; 5681fe2c6338SJoe Perches struct ctl_table lctl; 56821da177e4SLinus Torvalds int ret; 56831da177e4SLinus Torvalds 5684013d97e9SFrancesco Ruggeri /* 5685013d97e9SFrancesco Ruggeri * ctl->data points to idev->cnf.forwarding, we should 5686013d97e9SFrancesco Ruggeri * not modify it until we get the rtnl lock. 5687013d97e9SFrancesco Ruggeri */ 5688013d97e9SFrancesco Ruggeri lctl = *ctl; 5689013d97e9SFrancesco Ruggeri lctl.data = &val; 5690013d97e9SFrancesco Ruggeri 5691013d97e9SFrancesco Ruggeri ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 56921da177e4SLinus Torvalds 5693c8fecf22SPavel Emelyanov if (write) 5694b325fddbSStephen Hemminger ret = addrconf_fixup_forwarding(ctl, valp, val); 569588af182eSEric W. Biederman if (ret) 569688af182eSEric W. Biederman *ppos = pos; 56971da177e4SLinus Torvalds return ret; 56981da177e4SLinus Torvalds } 56991da177e4SLinus Torvalds 570077751427SMarcelo Leitner static 570177751427SMarcelo Leitner int addrconf_sysctl_mtu(struct ctl_table *ctl, int write, 570277751427SMarcelo Leitner void __user *buffer, size_t *lenp, loff_t *ppos) 570377751427SMarcelo Leitner { 570477751427SMarcelo Leitner struct inet6_dev *idev = ctl->extra1; 570577751427SMarcelo Leitner int min_mtu = IPV6_MIN_MTU; 570677751427SMarcelo Leitner struct ctl_table lctl; 570777751427SMarcelo Leitner 570877751427SMarcelo Leitner lctl = *ctl; 570977751427SMarcelo Leitner lctl.extra1 = &min_mtu; 571077751427SMarcelo Leitner lctl.extra2 = idev ? &idev->dev->mtu : NULL; 571177751427SMarcelo Leitner 571277751427SMarcelo Leitner return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos); 571377751427SMarcelo Leitner } 571477751427SMarcelo Leitner 571556d417b1SBrian Haley static void dev_disable_change(struct inet6_dev *idev) 571656d417b1SBrian Haley { 571775538c2bSCong Wang struct netdev_notifier_info info; 571875538c2bSCong Wang 571956d417b1SBrian Haley if (!idev || !idev->dev) 572056d417b1SBrian Haley return; 572156d417b1SBrian Haley 572275538c2bSCong Wang netdev_notifier_info_init(&info, idev->dev); 572356d417b1SBrian Haley if (idev->cnf.disable_ipv6) 572475538c2bSCong Wang addrconf_notify(NULL, NETDEV_DOWN, &info); 572556d417b1SBrian Haley else 572675538c2bSCong Wang addrconf_notify(NULL, NETDEV_UP, &info); 572756d417b1SBrian Haley } 572856d417b1SBrian Haley 572956d417b1SBrian Haley static void addrconf_disable_change(struct net *net, __s32 newf) 573056d417b1SBrian Haley { 573156d417b1SBrian Haley struct net_device *dev; 573256d417b1SBrian Haley struct inet6_dev *idev; 573356d417b1SBrian Haley 573403e4deffSKefeng Wang for_each_netdev(net, dev) { 573556d417b1SBrian Haley idev = __in6_dev_get(dev); 573656d417b1SBrian Haley if (idev) { 573756d417b1SBrian Haley int changed = (!idev->cnf.disable_ipv6) ^ (!newf); 573856d417b1SBrian Haley idev->cnf.disable_ipv6 = newf; 573956d417b1SBrian Haley if (changed) 574056d417b1SBrian Haley dev_disable_change(idev); 574156d417b1SBrian Haley } 574256d417b1SBrian Haley } 574356d417b1SBrian Haley } 574456d417b1SBrian Haley 5745013d97e9SFrancesco Ruggeri static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf) 574656d417b1SBrian Haley { 574756d417b1SBrian Haley struct net *net; 5748013d97e9SFrancesco Ruggeri int old; 5749013d97e9SFrancesco Ruggeri 5750013d97e9SFrancesco Ruggeri if (!rtnl_trylock()) 5751013d97e9SFrancesco Ruggeri return restart_syscall(); 575256d417b1SBrian Haley 575356d417b1SBrian Haley net = (struct net *)table->extra2; 5754013d97e9SFrancesco Ruggeri old = *p; 5755013d97e9SFrancesco Ruggeri *p = newf; 575656d417b1SBrian Haley 5757013d97e9SFrancesco Ruggeri if (p == &net->ipv6.devconf_dflt->disable_ipv6) { 5758013d97e9SFrancesco Ruggeri rtnl_unlock(); 575956d417b1SBrian Haley return 0; 576088af182eSEric W. Biederman } 576156d417b1SBrian Haley 576256d417b1SBrian Haley if (p == &net->ipv6.devconf_all->disable_ipv6) { 576356d417b1SBrian Haley net->ipv6.devconf_dflt->disable_ipv6 = newf; 576456d417b1SBrian Haley addrconf_disable_change(net, newf); 5765013d97e9SFrancesco Ruggeri } else if ((!newf) ^ (!old)) 576656d417b1SBrian Haley dev_disable_change((struct inet6_dev *)table->extra1); 576756d417b1SBrian Haley 576856d417b1SBrian Haley rtnl_unlock(); 576956d417b1SBrian Haley return 0; 577056d417b1SBrian Haley } 577156d417b1SBrian Haley 577256d417b1SBrian Haley static 5773fe2c6338SJoe Perches int addrconf_sysctl_disable(struct ctl_table *ctl, int write, 577456d417b1SBrian Haley void __user *buffer, size_t *lenp, loff_t *ppos) 577556d417b1SBrian Haley { 577656d417b1SBrian Haley int *valp = ctl->data; 577756d417b1SBrian Haley int val = *valp; 577888af182eSEric W. Biederman loff_t pos = *ppos; 5779fe2c6338SJoe Perches struct ctl_table lctl; 578056d417b1SBrian Haley int ret; 578156d417b1SBrian Haley 5782013d97e9SFrancesco Ruggeri /* 5783013d97e9SFrancesco Ruggeri * ctl->data points to idev->cnf.disable_ipv6, we should 5784013d97e9SFrancesco Ruggeri * not modify it until we get the rtnl lock. 5785013d97e9SFrancesco Ruggeri */ 5786013d97e9SFrancesco Ruggeri lctl = *ctl; 5787013d97e9SFrancesco Ruggeri lctl.data = &val; 5788013d97e9SFrancesco Ruggeri 5789013d97e9SFrancesco Ruggeri ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 579056d417b1SBrian Haley 579156d417b1SBrian Haley if (write) 579256d417b1SBrian Haley ret = addrconf_disable_ipv6(ctl, valp, val); 579388af182eSEric W. Biederman if (ret) 579488af182eSEric W. Biederman *ppos = pos; 579556d417b1SBrian Haley return ret; 579656d417b1SBrian Haley } 579756d417b1SBrian Haley 5798c92d5491Sstephen hemminger static 5799c92d5491Sstephen hemminger int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write, 5800c92d5491Sstephen hemminger void __user *buffer, size_t *lenp, loff_t *ppos) 5801c92d5491Sstephen hemminger { 5802c92d5491Sstephen hemminger int *valp = ctl->data; 5803c92d5491Sstephen hemminger int ret; 5804c92d5491Sstephen hemminger int old, new; 5805c92d5491Sstephen hemminger 5806c92d5491Sstephen hemminger old = *valp; 5807c92d5491Sstephen hemminger ret = proc_dointvec(ctl, write, buffer, lenp, ppos); 5808c92d5491Sstephen hemminger new = *valp; 5809c92d5491Sstephen hemminger 5810c92d5491Sstephen hemminger if (write && old != new) { 5811c92d5491Sstephen hemminger struct net *net = ctl->extra2; 5812c92d5491Sstephen hemminger 5813c92d5491Sstephen hemminger if (!rtnl_trylock()) 5814c92d5491Sstephen hemminger return restart_syscall(); 5815c92d5491Sstephen hemminger 5816c92d5491Sstephen hemminger if (valp == &net->ipv6.devconf_dflt->proxy_ndp) 581785b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 581885b3daadSDavid Ahern NETCONFA_PROXY_NEIGH, 5819c92d5491Sstephen hemminger NETCONFA_IFINDEX_DEFAULT, 5820c92d5491Sstephen hemminger net->ipv6.devconf_dflt); 5821c92d5491Sstephen hemminger else if (valp == &net->ipv6.devconf_all->proxy_ndp) 582285b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 582385b3daadSDavid Ahern NETCONFA_PROXY_NEIGH, 5824c92d5491Sstephen hemminger NETCONFA_IFINDEX_ALL, 5825c92d5491Sstephen hemminger net->ipv6.devconf_all); 5826c92d5491Sstephen hemminger else { 5827c92d5491Sstephen hemminger struct inet6_dev *idev = ctl->extra1; 5828c92d5491Sstephen hemminger 582985b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 583085b3daadSDavid Ahern NETCONFA_PROXY_NEIGH, 5831c92d5491Sstephen hemminger idev->dev->ifindex, 5832c92d5491Sstephen hemminger &idev->cnf); 5833c92d5491Sstephen hemminger } 5834c92d5491Sstephen hemminger rtnl_unlock(); 5835c92d5491Sstephen hemminger } 5836c92d5491Sstephen hemminger 5837c92d5491Sstephen hemminger return ret; 5838c92d5491Sstephen hemminger } 5839c92d5491Sstephen hemminger 5840d35a00b8SFelix Jia static int addrconf_sysctl_addr_gen_mode(struct ctl_table *ctl, int write, 5841d35a00b8SFelix Jia void __user *buffer, size_t *lenp, 5842d35a00b8SFelix Jia loff_t *ppos) 5843d35a00b8SFelix Jia { 5844d35a00b8SFelix Jia int ret = 0; 5845d35a00b8SFelix Jia int new_val; 5846d35a00b8SFelix Jia struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1; 5847d35a00b8SFelix Jia struct net *net = (struct net *)ctl->extra2; 5848d35a00b8SFelix Jia 58498c171d6cSFelix Jia if (!rtnl_trylock()) 58508c171d6cSFelix Jia return restart_syscall(); 58518c171d6cSFelix Jia 5852d35a00b8SFelix Jia ret = proc_dointvec(ctl, write, buffer, lenp, ppos); 5853d35a00b8SFelix Jia 5854d35a00b8SFelix Jia if (write) { 5855d35a00b8SFelix Jia new_val = *((int *)ctl->data); 5856d35a00b8SFelix Jia 58578c171d6cSFelix Jia if (check_addr_gen_mode(new_val) < 0) { 58588c171d6cSFelix Jia ret = -EINVAL; 58598c171d6cSFelix Jia goto out; 58608c171d6cSFelix Jia } 5861d35a00b8SFelix Jia 5862d35a00b8SFelix Jia /* request for default */ 5863d35a00b8SFelix Jia if (&net->ipv6.devconf_dflt->addr_gen_mode == ctl->data) { 5864d35a00b8SFelix Jia ipv6_devconf_dflt.addr_gen_mode = new_val; 5865d35a00b8SFelix Jia 5866d35a00b8SFelix Jia /* request for individual net device */ 5867d35a00b8SFelix Jia } else { 5868d35a00b8SFelix Jia if (!idev) 58698c171d6cSFelix Jia goto out; 5870d35a00b8SFelix Jia 58718c171d6cSFelix Jia if (check_stable_privacy(idev, net, new_val) < 0) { 58728c171d6cSFelix Jia ret = -EINVAL; 58738c171d6cSFelix Jia goto out; 58748c171d6cSFelix Jia } 5875d35a00b8SFelix Jia 5876d35a00b8SFelix Jia if (idev->cnf.addr_gen_mode != new_val) { 5877d35a00b8SFelix Jia idev->cnf.addr_gen_mode = new_val; 5878d35a00b8SFelix Jia addrconf_dev_config(idev->dev); 58798c171d6cSFelix Jia } 58808c171d6cSFelix Jia } 58818c171d6cSFelix Jia } 58828c171d6cSFelix Jia 58838c171d6cSFelix Jia out: 5884d35a00b8SFelix Jia rtnl_unlock(); 5885d35a00b8SFelix Jia 5886d35a00b8SFelix Jia return ret; 5887d35a00b8SFelix Jia } 5888d35a00b8SFelix Jia 58893d1bec99SHannes Frederic Sowa static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write, 58903d1bec99SHannes Frederic Sowa void __user *buffer, size_t *lenp, 58913d1bec99SHannes Frederic Sowa loff_t *ppos) 58923d1bec99SHannes Frederic Sowa { 58933d1bec99SHannes Frederic Sowa int err; 58943d1bec99SHannes Frederic Sowa struct in6_addr addr; 58953d1bec99SHannes Frederic Sowa char str[IPV6_MAX_STRLEN]; 58963d1bec99SHannes Frederic Sowa struct ctl_table lctl = *ctl; 5897622c81d5SHannes Frederic Sowa struct net *net = ctl->extra2; 58983d1bec99SHannes Frederic Sowa struct ipv6_stable_secret *secret = ctl->data; 58993d1bec99SHannes Frederic Sowa 5900622c81d5SHannes Frederic Sowa if (&net->ipv6.devconf_all->stable_secret == ctl->data) 5901622c81d5SHannes Frederic Sowa return -EIO; 5902622c81d5SHannes Frederic Sowa 59033d1bec99SHannes Frederic Sowa lctl.maxlen = IPV6_MAX_STRLEN; 59043d1bec99SHannes Frederic Sowa lctl.data = str; 59053d1bec99SHannes Frederic Sowa 59063d1bec99SHannes Frederic Sowa if (!rtnl_trylock()) 59073d1bec99SHannes Frederic Sowa return restart_syscall(); 59083d1bec99SHannes Frederic Sowa 59093d1bec99SHannes Frederic Sowa if (!write && !secret->initialized) { 59103d1bec99SHannes Frederic Sowa err = -EIO; 59113d1bec99SHannes Frederic Sowa goto out; 59123d1bec99SHannes Frederic Sowa } 59133d1bec99SHannes Frederic Sowa 59145449a5caSWANG Cong err = snprintf(str, sizeof(str), "%pI6", &secret->secret); 59153d1bec99SHannes Frederic Sowa if (err >= sizeof(str)) { 59163d1bec99SHannes Frederic Sowa err = -EIO; 59173d1bec99SHannes Frederic Sowa goto out; 59183d1bec99SHannes Frederic Sowa } 59193d1bec99SHannes Frederic Sowa 59203d1bec99SHannes Frederic Sowa err = proc_dostring(&lctl, write, buffer, lenp, ppos); 59213d1bec99SHannes Frederic Sowa if (err || !write) 59223d1bec99SHannes Frederic Sowa goto out; 59233d1bec99SHannes Frederic Sowa 59243d1bec99SHannes Frederic Sowa if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) { 59253d1bec99SHannes Frederic Sowa err = -EIO; 59263d1bec99SHannes Frederic Sowa goto out; 59273d1bec99SHannes Frederic Sowa } 59283d1bec99SHannes Frederic Sowa 59293d1bec99SHannes Frederic Sowa secret->initialized = true; 59303d1bec99SHannes Frederic Sowa secret->secret = addr; 59313d1bec99SHannes Frederic Sowa 5932622c81d5SHannes Frederic Sowa if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) { 5933622c81d5SHannes Frederic Sowa struct net_device *dev; 5934622c81d5SHannes Frederic Sowa 5935622c81d5SHannes Frederic Sowa for_each_netdev(net, dev) { 5936622c81d5SHannes Frederic Sowa struct inet6_dev *idev = __in6_dev_get(dev); 5937622c81d5SHannes Frederic Sowa 5938622c81d5SHannes Frederic Sowa if (idev) { 5939d35a00b8SFelix Jia idev->cnf.addr_gen_mode = 5940622c81d5SHannes Frederic Sowa IN6_ADDR_GEN_MODE_STABLE_PRIVACY; 5941622c81d5SHannes Frederic Sowa } 5942622c81d5SHannes Frederic Sowa } 5943622c81d5SHannes Frederic Sowa } else { 5944622c81d5SHannes Frederic Sowa struct inet6_dev *idev = ctl->extra1; 5945622c81d5SHannes Frederic Sowa 5946d35a00b8SFelix Jia idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY; 5947622c81d5SHannes Frederic Sowa } 5948622c81d5SHannes Frederic Sowa 59493d1bec99SHannes Frederic Sowa out: 59503d1bec99SHannes Frederic Sowa rtnl_unlock(); 59513d1bec99SHannes Frederic Sowa 59523d1bec99SHannes Frederic Sowa return err; 59533d1bec99SHannes Frederic Sowa } 5954c92d5491Sstephen hemminger 595535103d11SAndy Gospodarek static 595635103d11SAndy Gospodarek int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl, 595735103d11SAndy Gospodarek int write, 595835103d11SAndy Gospodarek void __user *buffer, 595935103d11SAndy Gospodarek size_t *lenp, 596035103d11SAndy Gospodarek loff_t *ppos) 596135103d11SAndy Gospodarek { 596235103d11SAndy Gospodarek int *valp = ctl->data; 596335103d11SAndy Gospodarek int val = *valp; 596435103d11SAndy Gospodarek loff_t pos = *ppos; 596535103d11SAndy Gospodarek struct ctl_table lctl; 596635103d11SAndy Gospodarek int ret; 596735103d11SAndy Gospodarek 596835103d11SAndy Gospodarek /* ctl->data points to idev->cnf.ignore_routes_when_linkdown 596935103d11SAndy Gospodarek * we should not modify it until we get the rtnl lock. 597035103d11SAndy Gospodarek */ 597135103d11SAndy Gospodarek lctl = *ctl; 597235103d11SAndy Gospodarek lctl.data = &val; 597335103d11SAndy Gospodarek 597435103d11SAndy Gospodarek ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 597535103d11SAndy Gospodarek 597635103d11SAndy Gospodarek if (write) 597735103d11SAndy Gospodarek ret = addrconf_fixup_linkdown(ctl, valp, val); 597835103d11SAndy Gospodarek if (ret) 597935103d11SAndy Gospodarek *ppos = pos; 598035103d11SAndy Gospodarek return ret; 598135103d11SAndy Gospodarek } 598235103d11SAndy Gospodarek 5983df789fe7SDavid Forster static 5984df789fe7SDavid Forster void addrconf_set_nopolicy(struct rt6_info *rt, int action) 5985df789fe7SDavid Forster { 5986df789fe7SDavid Forster if (rt) { 5987df789fe7SDavid Forster if (action) 5988df789fe7SDavid Forster rt->dst.flags |= DST_NOPOLICY; 5989df789fe7SDavid Forster else 5990df789fe7SDavid Forster rt->dst.flags &= ~DST_NOPOLICY; 5991df789fe7SDavid Forster } 5992df789fe7SDavid Forster } 5993df789fe7SDavid Forster 5994df789fe7SDavid Forster static 5995df789fe7SDavid Forster void addrconf_disable_policy_idev(struct inet6_dev *idev, int val) 5996df789fe7SDavid Forster { 5997df789fe7SDavid Forster struct inet6_ifaddr *ifa; 5998df789fe7SDavid Forster 5999df789fe7SDavid Forster read_lock_bh(&idev->lock); 6000df789fe7SDavid Forster list_for_each_entry(ifa, &idev->addr_list, if_list) { 6001df789fe7SDavid Forster spin_lock(&ifa->lock); 6002df789fe7SDavid Forster if (ifa->rt) { 60038d1c802bSDavid Ahern struct fib6_info *rt = ifa->rt; 6004df789fe7SDavid Forster int cpu; 6005df789fe7SDavid Forster 600666f5d6ceSWei Wang rcu_read_lock(); 60073b6761d1SDavid Ahern ifa->rt->dst_nopolicy = val ? true : false; 6008df789fe7SDavid Forster if (rt->rt6i_pcpu) { 6009df789fe7SDavid Forster for_each_possible_cpu(cpu) { 6010df789fe7SDavid Forster struct rt6_info **rtp; 6011df789fe7SDavid Forster 6012df789fe7SDavid Forster rtp = per_cpu_ptr(rt->rt6i_pcpu, cpu); 6013df789fe7SDavid Forster addrconf_set_nopolicy(*rtp, val); 6014df789fe7SDavid Forster } 6015df789fe7SDavid Forster } 601666f5d6ceSWei Wang rcu_read_unlock(); 6017df789fe7SDavid Forster } 6018df789fe7SDavid Forster spin_unlock(&ifa->lock); 6019df789fe7SDavid Forster } 6020df789fe7SDavid Forster read_unlock_bh(&idev->lock); 6021df789fe7SDavid Forster } 6022df789fe7SDavid Forster 6023df789fe7SDavid Forster static 6024df789fe7SDavid Forster int addrconf_disable_policy(struct ctl_table *ctl, int *valp, int val) 6025df789fe7SDavid Forster { 6026df789fe7SDavid Forster struct inet6_dev *idev; 6027df789fe7SDavid Forster struct net *net; 6028df789fe7SDavid Forster 6029df789fe7SDavid Forster if (!rtnl_trylock()) 6030df789fe7SDavid Forster return restart_syscall(); 6031df789fe7SDavid Forster 6032df789fe7SDavid Forster *valp = val; 6033df789fe7SDavid Forster 6034df789fe7SDavid Forster net = (struct net *)ctl->extra2; 6035df789fe7SDavid Forster if (valp == &net->ipv6.devconf_dflt->disable_policy) { 6036df789fe7SDavid Forster rtnl_unlock(); 6037df789fe7SDavid Forster return 0; 6038df789fe7SDavid Forster } 6039df789fe7SDavid Forster 6040df789fe7SDavid Forster if (valp == &net->ipv6.devconf_all->disable_policy) { 6041df789fe7SDavid Forster struct net_device *dev; 6042df789fe7SDavid Forster 6043df789fe7SDavid Forster for_each_netdev(net, dev) { 6044df789fe7SDavid Forster idev = __in6_dev_get(dev); 6045df789fe7SDavid Forster if (idev) 6046df789fe7SDavid Forster addrconf_disable_policy_idev(idev, val); 6047df789fe7SDavid Forster } 6048df789fe7SDavid Forster } else { 6049df789fe7SDavid Forster idev = (struct inet6_dev *)ctl->extra1; 6050df789fe7SDavid Forster addrconf_disable_policy_idev(idev, val); 6051df789fe7SDavid Forster } 6052df789fe7SDavid Forster 6053df789fe7SDavid Forster rtnl_unlock(); 6054df789fe7SDavid Forster return 0; 6055df789fe7SDavid Forster } 6056df789fe7SDavid Forster 6057df789fe7SDavid Forster static 6058df789fe7SDavid Forster int addrconf_sysctl_disable_policy(struct ctl_table *ctl, int write, 6059df789fe7SDavid Forster void __user *buffer, size_t *lenp, 6060df789fe7SDavid Forster loff_t *ppos) 6061df789fe7SDavid Forster { 6062df789fe7SDavid Forster int *valp = ctl->data; 6063df789fe7SDavid Forster int val = *valp; 6064df789fe7SDavid Forster loff_t pos = *ppos; 6065df789fe7SDavid Forster struct ctl_table lctl; 6066df789fe7SDavid Forster int ret; 6067df789fe7SDavid Forster 6068df789fe7SDavid Forster lctl = *ctl; 6069df789fe7SDavid Forster lctl.data = &val; 6070df789fe7SDavid Forster ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 6071df789fe7SDavid Forster 6072df789fe7SDavid Forster if (write && (*valp != val)) 6073df789fe7SDavid Forster ret = addrconf_disable_policy(ctl, valp, val); 6074df789fe7SDavid Forster 6075df789fe7SDavid Forster if (ret) 6076df789fe7SDavid Forster *ppos = pos; 6077df789fe7SDavid Forster 6078df789fe7SDavid Forster return ret; 6079df789fe7SDavid Forster } 6080df789fe7SDavid Forster 6081cb4a4c69SMaciej Żenczykowski static int minus_one = -1; 60822210d6b2SMaciej Żenczykowski static const int zero = 0; 6083cb9e684eSMaciej Żenczykowski static const int one = 1; 6084cb9e684eSMaciej Żenczykowski static const int two_five_five = 255; 6085cb9e684eSMaciej Żenczykowski 6086607ea7cdSKonstantin Khlebnikov static const struct ctl_table addrconf_sysctl[] = { 60871da177e4SLinus Torvalds { 60881da177e4SLinus Torvalds .procname = "forwarding", 60891da177e4SLinus Torvalds .data = &ipv6_devconf.forwarding, 60901da177e4SLinus Torvalds .maxlen = sizeof(int), 60911da177e4SLinus Torvalds .mode = 0644, 60926d9f239aSAlexey Dobriyan .proc_handler = addrconf_sysctl_forward, 60931da177e4SLinus Torvalds }, 60941da177e4SLinus Torvalds { 60951da177e4SLinus Torvalds .procname = "hop_limit", 60961da177e4SLinus Torvalds .data = &ipv6_devconf.hop_limit, 60971da177e4SLinus Torvalds .maxlen = sizeof(int), 60981da177e4SLinus Torvalds .mode = 0644, 6099cb9e684eSMaciej Żenczykowski .proc_handler = proc_dointvec_minmax, 6100cb9e684eSMaciej Żenczykowski .extra1 = (void *)&one, 6101cb9e684eSMaciej Żenczykowski .extra2 = (void *)&two_five_five, 61021da177e4SLinus Torvalds }, 61031da177e4SLinus Torvalds { 61041da177e4SLinus Torvalds .procname = "mtu", 61051da177e4SLinus Torvalds .data = &ipv6_devconf.mtu6, 61061da177e4SLinus Torvalds .maxlen = sizeof(int), 61071da177e4SLinus Torvalds .mode = 0644, 610877751427SMarcelo Leitner .proc_handler = addrconf_sysctl_mtu, 61091da177e4SLinus Torvalds }, 61101da177e4SLinus Torvalds { 61111da177e4SLinus Torvalds .procname = "accept_ra", 61121da177e4SLinus Torvalds .data = &ipv6_devconf.accept_ra, 61131da177e4SLinus Torvalds .maxlen = sizeof(int), 61141da177e4SLinus Torvalds .mode = 0644, 61156d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 61161da177e4SLinus Torvalds }, 61171da177e4SLinus Torvalds { 61181da177e4SLinus Torvalds .procname = "accept_redirects", 61191da177e4SLinus Torvalds .data = &ipv6_devconf.accept_redirects, 61201da177e4SLinus Torvalds .maxlen = sizeof(int), 61211da177e4SLinus Torvalds .mode = 0644, 61226d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 61231da177e4SLinus Torvalds }, 61241da177e4SLinus Torvalds { 61251da177e4SLinus Torvalds .procname = "autoconf", 61261da177e4SLinus Torvalds .data = &ipv6_devconf.autoconf, 61271da177e4SLinus Torvalds .maxlen = sizeof(int), 61281da177e4SLinus Torvalds .mode = 0644, 61296d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 61301da177e4SLinus Torvalds }, 61311da177e4SLinus Torvalds { 61321da177e4SLinus Torvalds .procname = "dad_transmits", 61331da177e4SLinus Torvalds .data = &ipv6_devconf.dad_transmits, 61341da177e4SLinus Torvalds .maxlen = sizeof(int), 61351da177e4SLinus Torvalds .mode = 0644, 61366d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 61371da177e4SLinus Torvalds }, 61381da177e4SLinus Torvalds { 61391da177e4SLinus Torvalds .procname = "router_solicitations", 61401da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicits, 61411da177e4SLinus Torvalds .maxlen = sizeof(int), 61421da177e4SLinus Torvalds .mode = 0644, 6143cb4a4c69SMaciej Żenczykowski .proc_handler = proc_dointvec_minmax, 6144cb4a4c69SMaciej Żenczykowski .extra1 = &minus_one, 61451da177e4SLinus Torvalds }, 61461da177e4SLinus Torvalds { 61471da177e4SLinus Torvalds .procname = "router_solicitation_interval", 61481da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_interval, 61491da177e4SLinus Torvalds .maxlen = sizeof(int), 61501da177e4SLinus Torvalds .mode = 0644, 61516d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 61521da177e4SLinus Torvalds }, 61531da177e4SLinus Torvalds { 6154bd11f074SMaciej Żenczykowski .procname = "router_solicitation_max_interval", 6155bd11f074SMaciej Żenczykowski .data = &ipv6_devconf.rtr_solicit_max_interval, 6156bd11f074SMaciej Żenczykowski .maxlen = sizeof(int), 6157bd11f074SMaciej Żenczykowski .mode = 0644, 6158bd11f074SMaciej Żenczykowski .proc_handler = proc_dointvec_jiffies, 6159bd11f074SMaciej Żenczykowski }, 6160bd11f074SMaciej Żenczykowski { 61611da177e4SLinus Torvalds .procname = "router_solicitation_delay", 61621da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_delay, 61631da177e4SLinus Torvalds .maxlen = sizeof(int), 61641da177e4SLinus Torvalds .mode = 0644, 61656d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 61661da177e4SLinus Torvalds }, 61671da177e4SLinus Torvalds { 61681da177e4SLinus Torvalds .procname = "force_mld_version", 61691da177e4SLinus Torvalds .data = &ipv6_devconf.force_mld_version, 61701da177e4SLinus Torvalds .maxlen = sizeof(int), 61711da177e4SLinus Torvalds .mode = 0644, 61726d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 61731da177e4SLinus Torvalds }, 6174fc4eba58SHannes Frederic Sowa { 6175fc4eba58SHannes Frederic Sowa .procname = "mldv1_unsolicited_report_interval", 6176fc4eba58SHannes Frederic Sowa .data = 6177fc4eba58SHannes Frederic Sowa &ipv6_devconf.mldv1_unsolicited_report_interval, 6178fc4eba58SHannes Frederic Sowa .maxlen = sizeof(int), 6179fc4eba58SHannes Frederic Sowa .mode = 0644, 6180fc4eba58SHannes Frederic Sowa .proc_handler = proc_dointvec_ms_jiffies, 6181fc4eba58SHannes Frederic Sowa }, 6182fc4eba58SHannes Frederic Sowa { 6183fc4eba58SHannes Frederic Sowa .procname = "mldv2_unsolicited_report_interval", 6184fc4eba58SHannes Frederic Sowa .data = 6185fc4eba58SHannes Frederic Sowa &ipv6_devconf.mldv2_unsolicited_report_interval, 6186fc4eba58SHannes Frederic Sowa .maxlen = sizeof(int), 6187fc4eba58SHannes Frederic Sowa .mode = 0644, 6188fc4eba58SHannes Frederic Sowa .proc_handler = proc_dointvec_ms_jiffies, 6189fc4eba58SHannes Frederic Sowa }, 61901da177e4SLinus Torvalds { 61911da177e4SLinus Torvalds .procname = "use_tempaddr", 61921da177e4SLinus Torvalds .data = &ipv6_devconf.use_tempaddr, 61931da177e4SLinus Torvalds .maxlen = sizeof(int), 61941da177e4SLinus Torvalds .mode = 0644, 61956d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 61961da177e4SLinus Torvalds }, 61971da177e4SLinus Torvalds { 61981da177e4SLinus Torvalds .procname = "temp_valid_lft", 61991da177e4SLinus Torvalds .data = &ipv6_devconf.temp_valid_lft, 62001da177e4SLinus Torvalds .maxlen = sizeof(int), 62011da177e4SLinus Torvalds .mode = 0644, 62026d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 62031da177e4SLinus Torvalds }, 62041da177e4SLinus Torvalds { 62051da177e4SLinus Torvalds .procname = "temp_prefered_lft", 62061da177e4SLinus Torvalds .data = &ipv6_devconf.temp_prefered_lft, 62071da177e4SLinus Torvalds .maxlen = sizeof(int), 62081da177e4SLinus Torvalds .mode = 0644, 62096d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 62101da177e4SLinus Torvalds }, 62111da177e4SLinus Torvalds { 62121da177e4SLinus Torvalds .procname = "regen_max_retry", 62131da177e4SLinus Torvalds .data = &ipv6_devconf.regen_max_retry, 62141da177e4SLinus Torvalds .maxlen = sizeof(int), 62151da177e4SLinus Torvalds .mode = 0644, 62166d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 62171da177e4SLinus Torvalds }, 62181da177e4SLinus Torvalds { 62191da177e4SLinus Torvalds .procname = "max_desync_factor", 62201da177e4SLinus Torvalds .data = &ipv6_devconf.max_desync_factor, 62211da177e4SLinus Torvalds .maxlen = sizeof(int), 62221da177e4SLinus Torvalds .mode = 0644, 62236d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 62241da177e4SLinus Torvalds }, 62251da177e4SLinus Torvalds { 62261da177e4SLinus Torvalds .procname = "max_addresses", 62271da177e4SLinus Torvalds .data = &ipv6_devconf.max_addresses, 62281da177e4SLinus Torvalds .maxlen = sizeof(int), 62291da177e4SLinus Torvalds .mode = 0644, 62306d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 62311da177e4SLinus Torvalds }, 62321da177e4SLinus Torvalds { 623365f5c7c1SYOSHIFUJI Hideaki .procname = "accept_ra_defrtr", 623465f5c7c1SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_defrtr, 623565f5c7c1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 623665f5c7c1SYOSHIFUJI Hideaki .mode = 0644, 62376d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 623865f5c7c1SYOSHIFUJI Hideaki }, 623965f5c7c1SYOSHIFUJI Hideaki { 62408013d1d7SHangbin Liu .procname = "accept_ra_min_hop_limit", 62418013d1d7SHangbin Liu .data = &ipv6_devconf.accept_ra_min_hop_limit, 62428013d1d7SHangbin Liu .maxlen = sizeof(int), 62438013d1d7SHangbin Liu .mode = 0644, 62448013d1d7SHangbin Liu .proc_handler = proc_dointvec, 62458013d1d7SHangbin Liu }, 62468013d1d7SHangbin Liu { 6247c4fd30ebSYOSHIFUJI Hideaki .procname = "accept_ra_pinfo", 6248c4fd30ebSYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_pinfo, 6249c4fd30ebSYOSHIFUJI Hideaki .maxlen = sizeof(int), 6250c4fd30ebSYOSHIFUJI Hideaki .mode = 0644, 62516d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 6252c4fd30ebSYOSHIFUJI Hideaki }, 6253930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 6254930d6ff2SYOSHIFUJI Hideaki { 6255930d6ff2SYOSHIFUJI Hideaki .procname = "accept_ra_rtr_pref", 6256930d6ff2SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rtr_pref, 6257930d6ff2SYOSHIFUJI Hideaki .maxlen = sizeof(int), 6258930d6ff2SYOSHIFUJI Hideaki .mode = 0644, 62596d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 6260930d6ff2SYOSHIFUJI Hideaki }, 626152e16356SYOSHIFUJI Hideaki { 626252e16356SYOSHIFUJI Hideaki .procname = "router_probe_interval", 626352e16356SYOSHIFUJI Hideaki .data = &ipv6_devconf.rtr_probe_interval, 626452e16356SYOSHIFUJI Hideaki .maxlen = sizeof(int), 626552e16356SYOSHIFUJI Hideaki .mode = 0644, 62666d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 626752e16356SYOSHIFUJI Hideaki }, 6268fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 626909c884d4SYOSHIFUJI Hideaki { 6270bbea124bSJoel Scherpelz .procname = "accept_ra_rt_info_min_plen", 6271bbea124bSJoel Scherpelz .data = &ipv6_devconf.accept_ra_rt_info_min_plen, 6272bbea124bSJoel Scherpelz .maxlen = sizeof(int), 6273bbea124bSJoel Scherpelz .mode = 0644, 6274bbea124bSJoel Scherpelz .proc_handler = proc_dointvec, 6275bbea124bSJoel Scherpelz }, 6276bbea124bSJoel Scherpelz { 627709c884d4SYOSHIFUJI Hideaki .procname = "accept_ra_rt_info_max_plen", 627809c884d4SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rt_info_max_plen, 627909c884d4SYOSHIFUJI Hideaki .maxlen = sizeof(int), 628009c884d4SYOSHIFUJI Hideaki .mode = 0644, 62816d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 628209c884d4SYOSHIFUJI Hideaki }, 628309c884d4SYOSHIFUJI Hideaki #endif 6284930d6ff2SYOSHIFUJI Hideaki #endif 6285c4fd30ebSYOSHIFUJI Hideaki { 6286fbea49e1SYOSHIFUJI Hideaki .procname = "proxy_ndp", 6287fbea49e1SYOSHIFUJI Hideaki .data = &ipv6_devconf.proxy_ndp, 6288fbea49e1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 6289fbea49e1SYOSHIFUJI Hideaki .mode = 0644, 6290c92d5491Sstephen hemminger .proc_handler = addrconf_sysctl_proxy_ndp, 6291fbea49e1SYOSHIFUJI Hideaki }, 6292fbea49e1SYOSHIFUJI Hideaki { 62930bcbc926SYOSHIFUJI Hideaki .procname = "accept_source_route", 62940bcbc926SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_source_route, 62950bcbc926SYOSHIFUJI Hideaki .maxlen = sizeof(int), 62960bcbc926SYOSHIFUJI Hideaki .mode = 0644, 62976d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 62980bcbc926SYOSHIFUJI Hideaki }, 629995c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 630095c385b4SNeil Horman { 630195c385b4SNeil Horman .procname = "optimistic_dad", 630295c385b4SNeil Horman .data = &ipv6_devconf.optimistic_dad, 630395c385b4SNeil Horman .maxlen = sizeof(int), 630495c385b4SNeil Horman .mode = 0644, 63056d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 630695c385b4SNeil Horman }, 63077fd2561eSErik Kline { 63087fd2561eSErik Kline .procname = "use_optimistic", 63097fd2561eSErik Kline .data = &ipv6_devconf.use_optimistic, 63107fd2561eSErik Kline .maxlen = sizeof(int), 63117fd2561eSErik Kline .mode = 0644, 63127fd2561eSErik Kline .proc_handler = proc_dointvec, 63137fd2561eSErik Kline }, 631495c385b4SNeil Horman #endif 63157bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 63167bc570c8SYOSHIFUJI Hideaki { 63177bc570c8SYOSHIFUJI Hideaki .procname = "mc_forwarding", 63187bc570c8SYOSHIFUJI Hideaki .data = &ipv6_devconf.mc_forwarding, 63197bc570c8SYOSHIFUJI Hideaki .maxlen = sizeof(int), 6320a4e6db07SDavid S. Miller .mode = 0444, 63216d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 63227bc570c8SYOSHIFUJI Hideaki }, 63237bc570c8SYOSHIFUJI Hideaki #endif 63240bcbc926SYOSHIFUJI Hideaki { 6325778d80beSYOSHIFUJI Hideaki .procname = "disable_ipv6", 6326778d80beSYOSHIFUJI Hideaki .data = &ipv6_devconf.disable_ipv6, 6327778d80beSYOSHIFUJI Hideaki .maxlen = sizeof(int), 6328778d80beSYOSHIFUJI Hideaki .mode = 0644, 632956d417b1SBrian Haley .proc_handler = addrconf_sysctl_disable, 6330778d80beSYOSHIFUJI Hideaki }, 6331778d80beSYOSHIFUJI Hideaki { 63321b34be74SYOSHIFUJI Hideaki .procname = "accept_dad", 63331b34be74SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_dad, 63341b34be74SYOSHIFUJI Hideaki .maxlen = sizeof(int), 63351b34be74SYOSHIFUJI Hideaki .mode = 0644, 63366d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 63371b34be74SYOSHIFUJI Hideaki }, 63381b34be74SYOSHIFUJI Hideaki { 6339f7734fdfSOctavian Purdila .procname = "force_tllao", 6340f7734fdfSOctavian Purdila .data = &ipv6_devconf.force_tllao, 6341f7734fdfSOctavian Purdila .maxlen = sizeof(int), 6342f7734fdfSOctavian Purdila .mode = 0644, 6343f7734fdfSOctavian Purdila .proc_handler = proc_dointvec 6344f7734fdfSOctavian Purdila }, 6345f7734fdfSOctavian Purdila { 63465cb04436SHannes Frederic Sowa .procname = "ndisc_notify", 63475cb04436SHannes Frederic Sowa .data = &ipv6_devconf.ndisc_notify, 63485cb04436SHannes Frederic Sowa .maxlen = sizeof(int), 63495cb04436SHannes Frederic Sowa .mode = 0644, 63505cb04436SHannes Frederic Sowa .proc_handler = proc_dointvec 63515cb04436SHannes Frederic Sowa }, 63525cb04436SHannes Frederic Sowa { 6353b800c3b9SHannes Frederic Sowa .procname = "suppress_frag_ndisc", 6354b800c3b9SHannes Frederic Sowa .data = &ipv6_devconf.suppress_frag_ndisc, 6355b800c3b9SHannes Frederic Sowa .maxlen = sizeof(int), 6356b800c3b9SHannes Frederic Sowa .mode = 0644, 6357b800c3b9SHannes Frederic Sowa .proc_handler = proc_dointvec 6358b800c3b9SHannes Frederic Sowa }, 6359b800c3b9SHannes Frederic Sowa { 6360d9333196SBen Greear .procname = "accept_ra_from_local", 6361d9333196SBen Greear .data = &ipv6_devconf.accept_ra_from_local, 6362d9333196SBen Greear .maxlen = sizeof(int), 6363d9333196SBen Greear .mode = 0644, 6364d9333196SBen Greear .proc_handler = proc_dointvec, 6365d9333196SBen Greear }, 6366d9333196SBen Greear { 6367c2943f14SHarout Hedeshian .procname = "accept_ra_mtu", 6368c2943f14SHarout Hedeshian .data = &ipv6_devconf.accept_ra_mtu, 6369c2943f14SHarout Hedeshian .maxlen = sizeof(int), 6370c2943f14SHarout Hedeshian .mode = 0644, 6371c2943f14SHarout Hedeshian .proc_handler = proc_dointvec, 6372c2943f14SHarout Hedeshian }, 6373c2943f14SHarout Hedeshian { 63743d1bec99SHannes Frederic Sowa .procname = "stable_secret", 63753d1bec99SHannes Frederic Sowa .data = &ipv6_devconf.stable_secret, 63763d1bec99SHannes Frederic Sowa .maxlen = IPV6_MAX_STRLEN, 63773d1bec99SHannes Frederic Sowa .mode = 0600, 63783d1bec99SHannes Frederic Sowa .proc_handler = addrconf_sysctl_stable_secret, 63793d1bec99SHannes Frederic Sowa }, 63803d1bec99SHannes Frederic Sowa { 63813985e8a3SErik Kline .procname = "use_oif_addrs_only", 63823985e8a3SErik Kline .data = &ipv6_devconf.use_oif_addrs_only, 63833985e8a3SErik Kline .maxlen = sizeof(int), 63843985e8a3SErik Kline .mode = 0644, 63853985e8a3SErik Kline .proc_handler = proc_dointvec, 638635103d11SAndy Gospodarek }, 638735103d11SAndy Gospodarek { 638835103d11SAndy Gospodarek .procname = "ignore_routes_with_linkdown", 638935103d11SAndy Gospodarek .data = &ipv6_devconf.ignore_routes_with_linkdown, 639035103d11SAndy Gospodarek .maxlen = sizeof(int), 639135103d11SAndy Gospodarek .mode = 0644, 639235103d11SAndy Gospodarek .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown, 63933985e8a3SErik Kline }, 63943985e8a3SErik Kline { 6395abbc3043SJohannes Berg .procname = "drop_unicast_in_l2_multicast", 6396abbc3043SJohannes Berg .data = &ipv6_devconf.drop_unicast_in_l2_multicast, 6397abbc3043SJohannes Berg .maxlen = sizeof(int), 6398abbc3043SJohannes Berg .mode = 0644, 6399abbc3043SJohannes Berg .proc_handler = proc_dointvec, 6400abbc3043SJohannes Berg }, 6401abbc3043SJohannes Berg { 64027a02bf89SJohannes Berg .procname = "drop_unsolicited_na", 64037a02bf89SJohannes Berg .data = &ipv6_devconf.drop_unsolicited_na, 64047a02bf89SJohannes Berg .maxlen = sizeof(int), 64057a02bf89SJohannes Berg .mode = 0644, 64067a02bf89SJohannes Berg .proc_handler = proc_dointvec, 64077a02bf89SJohannes Berg }, 64087a02bf89SJohannes Berg { 6409f1705ec1SDavid Ahern .procname = "keep_addr_on_down", 6410f1705ec1SDavid Ahern .data = &ipv6_devconf.keep_addr_on_down, 6411f1705ec1SDavid Ahern .maxlen = sizeof(int), 6412f1705ec1SDavid Ahern .mode = 0644, 6413f1705ec1SDavid Ahern .proc_handler = proc_dointvec, 6414f1705ec1SDavid Ahern 6415f1705ec1SDavid Ahern }, 6416f1705ec1SDavid Ahern { 64171ababebaSDavid Lebrun .procname = "seg6_enabled", 64181ababebaSDavid Lebrun .data = &ipv6_devconf.seg6_enabled, 64191ababebaSDavid Lebrun .maxlen = sizeof(int), 64201ababebaSDavid Lebrun .mode = 0644, 64211ababebaSDavid Lebrun .proc_handler = proc_dointvec, 64221ababebaSDavid Lebrun }, 6423bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC 6424bf355b8dSDavid Lebrun { 6425bf355b8dSDavid Lebrun .procname = "seg6_require_hmac", 6426bf355b8dSDavid Lebrun .data = &ipv6_devconf.seg6_require_hmac, 6427bf355b8dSDavid Lebrun .maxlen = sizeof(int), 6428bf355b8dSDavid Lebrun .mode = 0644, 6429bf355b8dSDavid Lebrun .proc_handler = proc_dointvec, 6430bf355b8dSDavid Lebrun }, 6431bf355b8dSDavid Lebrun #endif 64321ababebaSDavid Lebrun { 6433adc176c5SErik Nordmark .procname = "enhanced_dad", 6434adc176c5SErik Nordmark .data = &ipv6_devconf.enhanced_dad, 6435adc176c5SErik Nordmark .maxlen = sizeof(int), 6436adc176c5SErik Nordmark .mode = 0644, 6437adc176c5SErik Nordmark .proc_handler = proc_dointvec, 6438adc176c5SErik Nordmark }, 6439adc176c5SErik Nordmark { 6440d35a00b8SFelix Jia .procname = "addr_gen_mode", 6441d35a00b8SFelix Jia .data = &ipv6_devconf.addr_gen_mode, 6442d35a00b8SFelix Jia .maxlen = sizeof(int), 6443d35a00b8SFelix Jia .mode = 0644, 6444d35a00b8SFelix Jia .proc_handler = addrconf_sysctl_addr_gen_mode, 6445d35a00b8SFelix Jia }, 6446d35a00b8SFelix Jia { 6447df789fe7SDavid Forster .procname = "disable_policy", 6448df789fe7SDavid Forster .data = &ipv6_devconf.disable_policy, 6449df789fe7SDavid Forster .maxlen = sizeof(int), 6450df789fe7SDavid Forster .mode = 0644, 6451df789fe7SDavid Forster .proc_handler = addrconf_sysctl_disable_policy, 6452df789fe7SDavid Forster }, 6453df789fe7SDavid Forster { 64542210d6b2SMaciej Żenczykowski .procname = "ndisc_tclass", 64552210d6b2SMaciej Żenczykowski .data = &ipv6_devconf.ndisc_tclass, 64562210d6b2SMaciej Żenczykowski .maxlen = sizeof(int), 64572210d6b2SMaciej Żenczykowski .mode = 0644, 64582210d6b2SMaciej Żenczykowski .proc_handler = proc_dointvec_minmax, 64592210d6b2SMaciej Żenczykowski .extra1 = (void *)&zero, 64602210d6b2SMaciej Żenczykowski .extra2 = (void *)&two_five_five, 64612210d6b2SMaciej Żenczykowski }, 64622210d6b2SMaciej Żenczykowski { 6463f8572d8fSEric W. Biederman /* sentinel */ 64641da177e4SLinus Torvalds } 64651da177e4SLinus Torvalds }; 64661da177e4SLinus Torvalds 6467bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name, 6468f8572d8fSEric W. Biederman struct inet6_dev *idev, struct ipv6_devconf *p) 64691da177e4SLinus Torvalds { 647029c994e3SNicolas Dichtel int i, ifindex; 6471607ea7cdSKonstantin Khlebnikov struct ctl_table *table; 64726105e293SEric W. Biederman char path[sizeof("net/ipv6/conf/") + IFNAMSIZ]; 64731dab6222SPavel Emelyanov 6474607ea7cdSKonstantin Khlebnikov table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL); 6475607ea7cdSKonstantin Khlebnikov if (!table) 6476f68635e6SPavel Emelyanov goto out; 6477f68635e6SPavel Emelyanov 6478607ea7cdSKonstantin Khlebnikov for (i = 0; table[i].data; i++) { 6479607ea7cdSKonstantin Khlebnikov table[i].data += (char *)p - (char *)&ipv6_devconf; 6480cb9e684eSMaciej Żenczykowski /* If one of these is already set, then it is not safe to 6481cb9e684eSMaciej Żenczykowski * overwrite either of them: this makes proc_dointvec_minmax 6482cb9e684eSMaciej Żenczykowski * usable. 6483cb9e684eSMaciej Żenczykowski */ 6484cb9e684eSMaciej Żenczykowski if (!table[i].extra1 && !table[i].extra2) { 6485607ea7cdSKonstantin Khlebnikov table[i].extra1 = idev; /* embedded; no ref */ 6486607ea7cdSKonstantin Khlebnikov table[i].extra2 = net; 64871da177e4SLinus Torvalds } 6488cb9e684eSMaciej Żenczykowski } 64891da177e4SLinus Torvalds 64906105e293SEric W. Biederman snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name); 64911da177e4SLinus Torvalds 6492607ea7cdSKonstantin Khlebnikov p->sysctl_header = register_net_sysctl(net, path, table); 6493607ea7cdSKonstantin Khlebnikov if (!p->sysctl_header) 64946105e293SEric W. Biederman goto free; 6495f68635e6SPavel Emelyanov 649629c994e3SNicolas Dichtel if (!strcmp(dev_name, "all")) 649729c994e3SNicolas Dichtel ifindex = NETCONFA_IFINDEX_ALL; 649829c994e3SNicolas Dichtel else if (!strcmp(dev_name, "default")) 649929c994e3SNicolas Dichtel ifindex = NETCONFA_IFINDEX_DEFAULT; 650029c994e3SNicolas Dichtel else 650129c994e3SNicolas Dichtel ifindex = idev->dev->ifindex; 650285b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL, 650385b3daadSDavid Ahern ifindex, p); 650495897312SPavel Emelyanov return 0; 65051da177e4SLinus Torvalds 65061da177e4SLinus Torvalds free: 6507607ea7cdSKonstantin Khlebnikov kfree(table); 6508f68635e6SPavel Emelyanov out: 650995897312SPavel Emelyanov return -ENOBUFS; 65101da177e4SLinus Torvalds } 65111da177e4SLinus Torvalds 651223452170SDavid Ahern static void __addrconf_sysctl_unregister(struct net *net, 651323452170SDavid Ahern struct ipv6_devconf *p, int ifindex) 6514f52295a9SPavel Emelyanov { 6515607ea7cdSKonstantin Khlebnikov struct ctl_table *table; 6516f52295a9SPavel Emelyanov 6517607ea7cdSKonstantin Khlebnikov if (!p->sysctl_header) 6518408c4768SPavel Emelyanov return; 6519408c4768SPavel Emelyanov 6520607ea7cdSKonstantin Khlebnikov table = p->sysctl_header->ctl_table_arg; 6521607ea7cdSKonstantin Khlebnikov unregister_net_sysctl_table(p->sysctl_header); 6522607ea7cdSKonstantin Khlebnikov p->sysctl_header = NULL; 6523607ea7cdSKonstantin Khlebnikov kfree(table); 652423452170SDavid Ahern 652523452170SDavid Ahern inet6_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL); 65261da177e4SLinus Torvalds } 6527408c4768SPavel Emelyanov 6528a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev) 6529408c4768SPavel Emelyanov { 6530a317a2f1SWANG Cong int err; 6531a317a2f1SWANG Cong 6532a317a2f1SWANG Cong if (!sysctl_dev_name_is_allowed(idev->dev->name)) 6533a317a2f1SWANG Cong return -EINVAL; 6534a317a2f1SWANG Cong 6535a317a2f1SWANG Cong err = neigh_sysctl_register(idev->dev, idev->nd_parms, 6536f8572d8fSEric W. Biederman &ndisc_ifinfo_sysctl_change); 6537a317a2f1SWANG Cong if (err) 6538a317a2f1SWANG Cong return err; 6539a317a2f1SWANG Cong err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name, 6540f8572d8fSEric W. Biederman idev, &idev->cnf); 6541a317a2f1SWANG Cong if (err) 6542a317a2f1SWANG Cong neigh_sysctl_unregister(idev->nd_parms); 6543a317a2f1SWANG Cong 6544a317a2f1SWANG Cong return err; 6545408c4768SPavel Emelyanov } 6546408c4768SPavel Emelyanov 6547408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev) 6548408c4768SPavel Emelyanov { 654923452170SDavid Ahern __addrconf_sysctl_unregister(dev_net(idev->dev), &idev->cnf, 655023452170SDavid Ahern idev->dev->ifindex); 6551408c4768SPavel Emelyanov neigh_sysctl_unregister(idev->nd_parms); 65521da177e4SLinus Torvalds } 65531da177e4SLinus Torvalds 65541da177e4SLinus Torvalds 65551da177e4SLinus Torvalds #endif 65561da177e4SLinus Torvalds 65572c8c1e72SAlexey Dobriyan static int __net_init addrconf_init_net(struct net *net) 6558e0da5a48SPavel Emelyanov { 6559a79ca223SHong Zhiguo int err = -ENOMEM; 6560e0da5a48SPavel Emelyanov struct ipv6_devconf *all, *dflt; 6561e0da5a48SPavel Emelyanov 6562a79ca223SHong Zhiguo all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL); 656363159f29SIan Morris if (!all) 6564e0da5a48SPavel Emelyanov goto err_alloc_all; 6565e0da5a48SPavel Emelyanov 6566a79ca223SHong Zhiguo dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL); 656763159f29SIan Morris if (!dflt) 6568e0da5a48SPavel Emelyanov goto err_alloc_dflt; 6569a79ca223SHong Zhiguo 657056d417b1SBrian Haley /* these will be inherited by all namespaces */ 657156d417b1SBrian Haley dflt->autoconf = ipv6_defaults.autoconf; 657256d417b1SBrian Haley dflt->disable_ipv6 = ipv6_defaults.disable_ipv6; 6573e0da5a48SPavel Emelyanov 65743d1bec99SHannes Frederic Sowa dflt->stable_secret.initialized = false; 65753d1bec99SHannes Frederic Sowa all->stable_secret.initialized = false; 65763d1bec99SHannes Frederic Sowa 6577e0da5a48SPavel Emelyanov net->ipv6.devconf_all = all; 6578e0da5a48SPavel Emelyanov net->ipv6.devconf_dflt = dflt; 6579e0da5a48SPavel Emelyanov 6580e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 6581f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "all", NULL, all); 6582e0da5a48SPavel Emelyanov if (err < 0) 6583e0da5a48SPavel Emelyanov goto err_reg_all; 6584e0da5a48SPavel Emelyanov 6585f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "default", NULL, dflt); 6586e0da5a48SPavel Emelyanov if (err < 0) 6587e0da5a48SPavel Emelyanov goto err_reg_dflt; 6588e0da5a48SPavel Emelyanov #endif 6589e0da5a48SPavel Emelyanov return 0; 6590e0da5a48SPavel Emelyanov 6591e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 6592e0da5a48SPavel Emelyanov err_reg_dflt: 659323452170SDavid Ahern __addrconf_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL); 6594e0da5a48SPavel Emelyanov err_reg_all: 6595e0da5a48SPavel Emelyanov kfree(dflt); 6596e0da5a48SPavel Emelyanov #endif 6597e0da5a48SPavel Emelyanov err_alloc_dflt: 6598e0da5a48SPavel Emelyanov kfree(all); 6599e0da5a48SPavel Emelyanov err_alloc_all: 6600e0da5a48SPavel Emelyanov return err; 6601e0da5a48SPavel Emelyanov } 6602e0da5a48SPavel Emelyanov 66032c8c1e72SAlexey Dobriyan static void __net_exit addrconf_exit_net(struct net *net) 6604e0da5a48SPavel Emelyanov { 6605e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 660623452170SDavid Ahern __addrconf_sysctl_unregister(net, net->ipv6.devconf_dflt, 660723452170SDavid Ahern NETCONFA_IFINDEX_DEFAULT); 660823452170SDavid Ahern __addrconf_sysctl_unregister(net, net->ipv6.devconf_all, 660923452170SDavid Ahern NETCONFA_IFINDEX_ALL); 6610e0da5a48SPavel Emelyanov #endif 6611e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_dflt); 6612e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_all); 6613e0da5a48SPavel Emelyanov } 6614e0da5a48SPavel Emelyanov 6615e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = { 6616e0da5a48SPavel Emelyanov .init = addrconf_init_net, 6617e0da5a48SPavel Emelyanov .exit = addrconf_exit_net, 6618e0da5a48SPavel Emelyanov }; 6619e0da5a48SPavel Emelyanov 6620207895fdSDaniel Borkmann static struct rtnl_af_ops inet6_ops __read_mostly = { 6621b382b191SThomas Graf .family = AF_INET6, 6622b382b191SThomas Graf .fill_link_af = inet6_fill_link_af, 6623b382b191SThomas Graf .get_link_af_size = inet6_get_link_af_size, 662411b1f828SDaniel Borkmann .validate_link_af = inet6_validate_link_af, 6625f53adae4SDaniel Borkmann .set_link_af = inet6_set_link_af, 6626b382b191SThomas Graf }; 6627b382b191SThomas Graf 66281da177e4SLinus Torvalds /* 66291da177e4SLinus Torvalds * Init / cleanup code 66301da177e4SLinus Torvalds */ 66311da177e4SLinus Torvalds 66321da177e4SLinus Torvalds int __init addrconf_init(void) 66331da177e4SLinus Torvalds { 6634a317a2f1SWANG Cong struct inet6_dev *idev; 6635c2e21293Sstephen hemminger int i, err; 66362a8cc6c8SYOSHIFUJI Hideaki 6637e21e8467SStephen Hemminger err = ipv6_addr_label_init(); 6638e21e8467SStephen Hemminger if (err < 0) { 6639f3213831SJoe Perches pr_crit("%s: cannot initialize default policy table: %d\n", 6640f3213831SJoe Perches __func__, err); 66412cc6d2bfSNeil Horman goto out; 66422a8cc6c8SYOSHIFUJI Hideaki } 66431da177e4SLinus Torvalds 66442cc6d2bfSNeil Horman err = register_pernet_subsys(&addrconf_ops); 66452cc6d2bfSNeil Horman if (err < 0) 66462cc6d2bfSNeil Horman goto out_addrlabel; 6647e0da5a48SPavel Emelyanov 6648c15b1ccaSHannes Frederic Sowa addrconf_wq = create_workqueue("ipv6_addrconf"); 6649c15b1ccaSHannes Frederic Sowa if (!addrconf_wq) { 6650c15b1ccaSHannes Frederic Sowa err = -ENOMEM; 6651c15b1ccaSHannes Frederic Sowa goto out_nowq; 6652c15b1ccaSHannes Frederic Sowa } 6653c15b1ccaSHannes Frederic Sowa 66541da177e4SLinus Torvalds /* The addrconf netdev notifier requires that loopback_dev 66551da177e4SLinus Torvalds * has it's ipv6 private information allocated and setup 66561da177e4SLinus Torvalds * before it can bring up and give link-local addresses 66571da177e4SLinus Torvalds * to other devices which are up. 66581da177e4SLinus Torvalds * 66591da177e4SLinus Torvalds * Unfortunately, loopback_dev is not necessarily the first 66601da177e4SLinus Torvalds * entry in the global dev_base list of net devices. In fact, 66611da177e4SLinus Torvalds * it is likely to be the very last entry on that list. 66621da177e4SLinus Torvalds * So this causes the notifier registry below to try and 66631da177e4SLinus Torvalds * give link-local addresses to all devices besides loopback_dev 66641da177e4SLinus Torvalds * first, then loopback_dev, which cases all the non-loopback_dev 66651da177e4SLinus Torvalds * devices to fail to get a link-local address. 66661da177e4SLinus Torvalds * 66671da177e4SLinus Torvalds * So, as a temporary fix, allocate the ipv6 structure for 66681da177e4SLinus Torvalds * loopback_dev first by hand. 66691da177e4SLinus Torvalds * Longer term, all of the dependencies ipv6 has upon the loopback 66701da177e4SLinus Torvalds * device and it being up should be removed. 66711da177e4SLinus Torvalds */ 66721da177e4SLinus Torvalds rtnl_lock(); 6673a317a2f1SWANG Cong idev = ipv6_add_dev(init_net.loopback_dev); 66741da177e4SLinus Torvalds rtnl_unlock(); 6675a317a2f1SWANG Cong if (IS_ERR(idev)) { 6676a317a2f1SWANG Cong err = PTR_ERR(idev); 6677e0da5a48SPavel Emelyanov goto errlo; 6678a317a2f1SWANG Cong } 66791da177e4SLinus Torvalds 66802f460933SWANG Cong ip6_route_init_special_entries(); 66812f460933SWANG Cong 6682c2e21293Sstephen hemminger for (i = 0; i < IN6_ADDR_HSIZE; i++) 6683c2e21293Sstephen hemminger INIT_HLIST_HEAD(&inet6_addr_lst[i]); 6684c2e21293Sstephen hemminger 66851da177e4SLinus Torvalds register_netdevice_notifier(&ipv6_dev_notf); 66861da177e4SLinus Torvalds 6687c15b1ccaSHannes Frederic Sowa addrconf_verify(); 6688c127ea2cSThomas Graf 66893678a9d8Sstephen hemminger rtnl_af_register(&inet6_ops); 6690b382b191SThomas Graf 669116feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETLINK, 669216feebcfSFlorian Westphal NULL, inet6_dump_ifinfo, 0); 6693c127ea2cSThomas Graf if (err < 0) 6694c127ea2cSThomas Graf goto errout; 6695c127ea2cSThomas Graf 669616feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWADDR, 669716feebcfSFlorian Westphal inet6_rtm_newaddr, NULL, 0); 669816feebcfSFlorian Westphal if (err < 0) 669916feebcfSFlorian Westphal goto errout; 670016feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELADDR, 670116feebcfSFlorian Westphal inet6_rtm_deladdr, NULL, 0); 670216feebcfSFlorian Westphal if (err < 0) 670316feebcfSFlorian Westphal goto errout; 670416feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETADDR, 670516feebcfSFlorian Westphal inet6_rtm_getaddr, inet6_dump_ifaddr, 670616feebcfSFlorian Westphal RTNL_FLAG_DOIT_UNLOCKED); 670716feebcfSFlorian Westphal if (err < 0) 670816feebcfSFlorian Westphal goto errout; 670916feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETMULTICAST, 671016feebcfSFlorian Westphal NULL, inet6_dump_ifmcaddr, 0); 671116feebcfSFlorian Westphal if (err < 0) 671216feebcfSFlorian Westphal goto errout; 671316feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETANYCAST, 671416feebcfSFlorian Westphal NULL, inet6_dump_ifacaddr, 0); 671516feebcfSFlorian Westphal if (err < 0) 671616feebcfSFlorian Westphal goto errout; 671716feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETNETCONF, 671816feebcfSFlorian Westphal inet6_netconf_get_devconf, 671916feebcfSFlorian Westphal inet6_netconf_dump_devconf, 672016feebcfSFlorian Westphal RTNL_FLAG_DOIT_UNLOCKED); 672116feebcfSFlorian Westphal if (err < 0) 672216feebcfSFlorian Westphal goto errout; 6723a3fde2adSFlorian Westphal err = ipv6_addr_label_rtnl_register(); 6724a3fde2adSFlorian Westphal if (err < 0) 6725a3fde2adSFlorian Westphal goto errout; 67262a8cc6c8SYOSHIFUJI Hideaki 67271da177e4SLinus Torvalds return 0; 6728c127ea2cSThomas Graf errout: 672916feebcfSFlorian Westphal rtnl_unregister_all(PF_INET6); 6730b382b191SThomas Graf rtnl_af_unregister(&inet6_ops); 6731c127ea2cSThomas Graf unregister_netdevice_notifier(&ipv6_dev_notf); 6732e0da5a48SPavel Emelyanov errlo: 6733c15b1ccaSHannes Frederic Sowa destroy_workqueue(addrconf_wq); 6734c15b1ccaSHannes Frederic Sowa out_nowq: 6735e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 67362cc6d2bfSNeil Horman out_addrlabel: 67372cc6d2bfSNeil Horman ipv6_addr_label_cleanup(); 67382cc6d2bfSNeil Horman out: 6739c127ea2cSThomas Graf return err; 67401da177e4SLinus Torvalds } 67411da177e4SLinus Torvalds 674209f7709fSDaniel Lezcano void addrconf_cleanup(void) 67431da177e4SLinus Torvalds { 6744176c39afSDaniel Lezcano struct net_device *dev; 67451da177e4SLinus Torvalds int i; 67461da177e4SLinus Torvalds 67471da177e4SLinus Torvalds unregister_netdevice_notifier(&ipv6_dev_notf); 6748e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 67492cc6d2bfSNeil Horman ipv6_addr_label_cleanup(); 67501da177e4SLinus Torvalds 67515c45121dSFlorian Westphal rtnl_af_unregister(&inet6_ops); 67521da177e4SLinus Torvalds 67535c45121dSFlorian Westphal rtnl_lock(); 6754b382b191SThomas Graf 6755176c39afSDaniel Lezcano /* clean dev list */ 6756176c39afSDaniel Lezcano for_each_netdev(&init_net, dev) { 6757176c39afSDaniel Lezcano if (__in6_dev_get(dev) == NULL) 6758176c39afSDaniel Lezcano continue; 6759176c39afSDaniel Lezcano addrconf_ifdown(dev, 1); 6760176c39afSDaniel Lezcano } 6761176c39afSDaniel Lezcano addrconf_ifdown(init_net.loopback_dev, 2); 6762176c39afSDaniel Lezcano 67631da177e4SLinus Torvalds /* 67641da177e4SLinus Torvalds * Check hash table. 67651da177e4SLinus Torvalds */ 67665c578aedSstephen hemminger spin_lock_bh(&addrconf_hash_lock); 6767c2e21293Sstephen hemminger for (i = 0; i < IN6_ADDR_HSIZE; i++) 6768c2e21293Sstephen hemminger WARN_ON(!hlist_empty(&inet6_addr_lst[i])); 67695c578aedSstephen hemminger spin_unlock_bh(&addrconf_hash_lock); 6770c15b1ccaSHannes Frederic Sowa cancel_delayed_work(&addr_chk_work); 67711da177e4SLinus Torvalds rtnl_unlock(); 6772c15b1ccaSHannes Frederic Sowa 6773c15b1ccaSHannes Frederic Sowa destroy_workqueue(addrconf_wq); 67741da177e4SLinus Torvalds } 6775