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; 10598308f3ffSDavid Ahern ifa->rt_priority = cfg->rt_priority; 1060e6464b8cSDavid Ahern ifa->flags = cfg->ifa_flags; 106166eb9f86SMahesh Bandewar /* No need to add the TENTATIVE flag for addresses with NODAD */ 1062e6464b8cSDavid Ahern if (!(cfg->ifa_flags & IFA_F_NODAD)) 106366eb9f86SMahesh Bandewar ifa->flags |= IFA_F_TENTATIVE; 1064e6464b8cSDavid Ahern ifa->valid_lft = cfg->valid_lft; 1065e6464b8cSDavid Ahern ifa->prefered_lft = cfg->preferred_lft; 10661da177e4SLinus Torvalds ifa->cstamp = ifa->tstamp = jiffies; 1067617fe29dSDaniel Borkmann ifa->tokenized = false; 10681da177e4SLinus Torvalds 1069360a9887SDavid Ahern ifa->rt = f6i; 107057f5f544SKeir Fraser 10711da177e4SLinus Torvalds ifa->idev = idev; 1072f3d9832eSDavid Ahern in6_dev_hold(idev); 1073f3d9832eSDavid Ahern 10741da177e4SLinus Torvalds /* For caller */ 1075271201c0SReshetova, Elena refcount_set(&ifa->refcnt, 1); 10761da177e4SLinus Torvalds 1077f3d9832eSDavid Ahern rcu_read_lock_bh(); 10781da177e4SLinus Torvalds 1079f3d9832eSDavid Ahern err = ipv6_add_addr_hash(idev->dev, ifa); 1080f3d9832eSDavid Ahern if (err < 0) { 1081f3d9832eSDavid Ahern rcu_read_unlock_bh(); 1082f3d9832eSDavid Ahern goto out; 1083f3d9832eSDavid Ahern } 10841da177e4SLinus Torvalds 10851da177e4SLinus Torvalds write_lock(&idev->lock); 1086f3d9832eSDavid Ahern 10871da177e4SLinus Torvalds /* Add to inet6_dev unicast addr list. */ 1088e55ffac6SBrian Haley ipv6_link_dev_addr(idev, ifa); 10891da177e4SLinus Torvalds 10901da177e4SLinus Torvalds if (ifa->flags&IFA_F_TEMPORARY) { 1091372e6c8fSstephen hemminger list_add(&ifa->tmp_list, &idev->tempaddr_list); 10921da177e4SLinus Torvalds in6_ifa_hold(ifa); 10931da177e4SLinus Torvalds } 10941da177e4SLinus Torvalds 10951da177e4SLinus Torvalds in6_ifa_hold(ifa); 10961da177e4SLinus Torvalds write_unlock(&idev->lock); 1097f3d9832eSDavid Ahern 10988814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 10991da177e4SLinus Torvalds 1100f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_UP, ifa); 1101f3d9832eSDavid Ahern out: 1102f3d9832eSDavid Ahern if (unlikely(err < 0)) { 1103360a9887SDavid Ahern fib6_info_release(f6i); 110493531c67SDavid Ahern 1105f3d9832eSDavid Ahern if (ifa) { 1106f3d9832eSDavid Ahern if (ifa->idev) 1107f3d9832eSDavid Ahern in6_dev_put(ifa->idev); 11081da177e4SLinus Torvalds kfree(ifa); 1109f3d9832eSDavid Ahern } 11101da177e4SLinus Torvalds ifa = ERR_PTR(err); 11111da177e4SLinus Torvalds } 11121da177e4SLinus Torvalds 11131da177e4SLinus Torvalds return ifa; 11141da177e4SLinus Torvalds } 11151da177e4SLinus Torvalds 11165b84efecSThomas Haller enum cleanup_prefix_rt_t { 11175b84efecSThomas Haller CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */ 11185b84efecSThomas Haller CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */ 11195b84efecSThomas Haller CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */ 11205b84efecSThomas Haller }; 11215b84efecSThomas Haller 11225b84efecSThomas Haller /* 11235b84efecSThomas Haller * Check, whether the prefix for ifp would still need a prefix route 11245b84efecSThomas Haller * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_* 11255b84efecSThomas Haller * constants. 11265b84efecSThomas Haller * 11275b84efecSThomas Haller * 1) we don't purge prefix if address was not permanent. 11285b84efecSThomas Haller * prefix is managed by its own lifetime. 11295b84efecSThomas Haller * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE. 11305b84efecSThomas Haller * 3) if there are no addresses, delete prefix. 11315b84efecSThomas Haller * 4) if there are still other permanent address(es), 11325b84efecSThomas Haller * corresponding prefix is still permanent. 11335b84efecSThomas Haller * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE, 11345b84efecSThomas Haller * don't purge the prefix, assume user space is managing it. 11355b84efecSThomas Haller * 6) otherwise, update prefix lifetime to the 11365b84efecSThomas Haller * longest valid lifetime among the corresponding 11375b84efecSThomas Haller * addresses on the device. 11385b84efecSThomas Haller * Note: subsequent RA will update lifetime. 11395b84efecSThomas Haller **/ 11405b84efecSThomas Haller static enum cleanup_prefix_rt_t 11415b84efecSThomas Haller check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires) 11425b84efecSThomas Haller { 11435b84efecSThomas Haller struct inet6_ifaddr *ifa; 11445b84efecSThomas Haller struct inet6_dev *idev = ifp->idev; 11455b84efecSThomas Haller unsigned long lifetime; 11465b84efecSThomas Haller enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL; 11475b84efecSThomas Haller 11485b84efecSThomas Haller *expires = jiffies; 11495b84efecSThomas Haller 11505b84efecSThomas Haller list_for_each_entry(ifa, &idev->addr_list, if_list) { 11515b84efecSThomas Haller if (ifa == ifp) 11525b84efecSThomas Haller continue; 11535b84efecSThomas Haller if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr, 11545b84efecSThomas Haller ifp->prefix_len)) 11555b84efecSThomas Haller continue; 11565b84efecSThomas Haller if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE)) 11575b84efecSThomas Haller return CLEANUP_PREFIX_RT_NOP; 11585b84efecSThomas Haller 11595b84efecSThomas Haller action = CLEANUP_PREFIX_RT_EXPIRE; 11605b84efecSThomas Haller 11615b84efecSThomas Haller spin_lock(&ifa->lock); 11625b84efecSThomas Haller 11635b84efecSThomas Haller lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ); 11645b84efecSThomas Haller /* 11655b84efecSThomas Haller * Note: Because this address is 11665b84efecSThomas Haller * not permanent, lifetime < 11675b84efecSThomas Haller * LONG_MAX / HZ here. 11685b84efecSThomas Haller */ 11695b84efecSThomas Haller if (time_before(*expires, ifa->tstamp + lifetime * HZ)) 11705b84efecSThomas Haller *expires = ifa->tstamp + lifetime * HZ; 11715b84efecSThomas Haller spin_unlock(&ifa->lock); 11725b84efecSThomas Haller } 11735b84efecSThomas Haller 11745b84efecSThomas Haller return action; 11755b84efecSThomas Haller } 11765b84efecSThomas Haller 11775b84efecSThomas Haller static void 11785b84efecSThomas Haller cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt) 11795b84efecSThomas Haller { 118093c2fb25SDavid Ahern struct fib6_info *f6i; 11815b84efecSThomas Haller 118293c2fb25SDavid Ahern f6i = addrconf_get_prefix_route(&ifp->addr, 11835b84efecSThomas Haller ifp->prefix_len, 11845b84efecSThomas Haller ifp->idev->dev, 11855b84efecSThomas Haller 0, RTF_GATEWAY | RTF_DEFAULT); 118693c2fb25SDavid Ahern if (f6i) { 11875b84efecSThomas Haller if (del_rt) 118893c2fb25SDavid Ahern ip6_del_rt(dev_net(ifp->idev->dev), f6i); 11895b84efecSThomas Haller else { 119093c2fb25SDavid Ahern if (!(f6i->fib6_flags & RTF_EXPIRES)) 119193c2fb25SDavid Ahern fib6_set_expires(f6i, expires); 119293c2fb25SDavid Ahern fib6_info_release(f6i); 11935b84efecSThomas Haller } 11945b84efecSThomas Haller } 11955b84efecSThomas Haller } 11965b84efecSThomas Haller 11975b84efecSThomas Haller 11981da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */ 11991da177e4SLinus Torvalds 12001da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp) 12011da177e4SLinus Torvalds { 12024c5ff6a6SHerbert Xu int state; 12035b84efecSThomas Haller enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP; 12045b84efecSThomas Haller unsigned long expires; 12051da177e4SLinus Torvalds 1206c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 1207c15b1ccaSHannes Frederic Sowa 12088e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 12094c5ff6a6SHerbert Xu state = ifp->state; 1210e9d3e084SHerbert Xu ifp->state = INET6_IFADDR_STATE_DEAD; 12118e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 12124c5ff6a6SHerbert Xu 12134c5ff6a6SHerbert Xu if (state == INET6_IFADDR_STATE_DEAD) 12144c5ff6a6SHerbert Xu goto out; 12151da177e4SLinus Torvalds 12165c578aedSstephen hemminger spin_lock_bh(&addrconf_hash_lock); 12175c578aedSstephen hemminger hlist_del_init_rcu(&ifp->addr_lst); 12185c578aedSstephen hemminger spin_unlock_bh(&addrconf_hash_lock); 12191da177e4SLinus Torvalds 12205b84efecSThomas Haller write_lock_bh(&ifp->idev->lock); 12215d9efa7eSDavid S. Miller 12221da177e4SLinus Torvalds if (ifp->flags&IFA_F_TEMPORARY) { 1223372e6c8fSstephen hemminger list_del(&ifp->tmp_list); 12241da177e4SLinus Torvalds if (ifp->ifpub) { 12251da177e4SLinus Torvalds in6_ifa_put(ifp->ifpub); 12261da177e4SLinus Torvalds ifp->ifpub = NULL; 12271da177e4SLinus Torvalds } 12281da177e4SLinus Torvalds __in6_ifa_put(ifp); 12291da177e4SLinus Torvalds } 12301da177e4SLinus Torvalds 12315b84efecSThomas Haller if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE)) 12325b84efecSThomas Haller action = check_cleanup_prefix_route(ifp, &expires); 12335b84efecSThomas Haller 12348ef802aaSEric Dumazet list_del_rcu(&ifp->if_list); 12351da177e4SLinus Torvalds __in6_ifa_put(ifp); 1236502a2ffdSstephen hemminger 12375b84efecSThomas Haller write_unlock_bh(&ifp->idev->lock); 12381da177e4SLinus Torvalds 1239c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifp); 1240b2238566SAndrey Vagin 12411da177e4SLinus Torvalds ipv6_ifa_notify(RTM_DELADDR, ifp); 12421da177e4SLinus Torvalds 1243f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_DOWN, ifp); 12441da177e4SLinus Torvalds 12455b84efecSThomas Haller if (action != CLEANUP_PREFIX_RT_NOP) { 12465b84efecSThomas Haller cleanup_prefix_route(ifp, expires, 12475b84efecSThomas Haller action == CLEANUP_PREFIX_RT_DEL); 12481da177e4SLinus Torvalds } 12491da177e4SLinus Torvalds 1250c3968a85SDaniel Walter /* clean up prefsrc entries */ 1251c3968a85SDaniel Walter rt6_remove_prefsrc(ifp); 12524c5ff6a6SHerbert Xu out: 12531da177e4SLinus Torvalds in6_ifa_put(ifp); 12541da177e4SLinus Torvalds } 12551da177e4SLinus Torvalds 1256fffcefe9SEric Dumazet static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, 1257fffcefe9SEric Dumazet struct inet6_ifaddr *ift, 1258fffcefe9SEric Dumazet bool block) 12591da177e4SLinus Torvalds { 12601da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 12611da177e4SLinus Torvalds struct in6_addr addr, *tmpaddr; 1262e6464b8cSDavid Ahern unsigned long tmp_tstamp, age; 1263eac55bf9SBenoit Boissinot unsigned long regen_advance; 1264e6464b8cSDavid Ahern struct ifa6_config cfg; 12651da177e4SLinus Torvalds int ret = 0; 126676f793e3SLorenzo Colitti unsigned long now = jiffies; 126776506a98SJiri Bohac long max_desync_factor; 12687aa8e63fSJiri Bohac s32 cnf_temp_preferred_lft; 12691da177e4SLinus Torvalds 127053bd6749SJiri Pirko write_lock_bh(&idev->lock); 12711da177e4SLinus Torvalds if (ift) { 12721da177e4SLinus Torvalds spin_lock_bh(&ift->lock); 12731da177e4SLinus Torvalds memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8); 12741da177e4SLinus Torvalds spin_unlock_bh(&ift->lock); 12751da177e4SLinus Torvalds tmpaddr = &addr; 12761da177e4SLinus Torvalds } else { 12771da177e4SLinus Torvalds tmpaddr = NULL; 12781da177e4SLinus Torvalds } 12791da177e4SLinus Torvalds retry: 12801da177e4SLinus Torvalds in6_dev_hold(idev); 12811da177e4SLinus Torvalds if (idev->cnf.use_tempaddr <= 0) { 128253bd6749SJiri Pirko write_unlock_bh(&idev->lock); 1283f3213831SJoe Perches pr_info("%s: use_tempaddr is disabled\n", __func__); 12841da177e4SLinus Torvalds in6_dev_put(idev); 12851da177e4SLinus Torvalds ret = -1; 12861da177e4SLinus Torvalds goto out; 12871da177e4SLinus Torvalds } 12881da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 12891da177e4SLinus Torvalds if (ifp->regen_count++ >= idev->cnf.regen_max_retry) { 12901da177e4SLinus Torvalds idev->cnf.use_tempaddr = -1; /*XXX*/ 12911da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 129253bd6749SJiri Pirko write_unlock_bh(&idev->lock); 1293f3213831SJoe Perches pr_warn("%s: regeneration time exceeded - disabled temporary address support\n", 1294f3213831SJoe Perches __func__); 12951da177e4SLinus Torvalds in6_dev_put(idev); 12961da177e4SLinus Torvalds ret = -1; 12971da177e4SLinus Torvalds goto out; 12981da177e4SLinus Torvalds } 12991da177e4SLinus Torvalds in6_ifa_hold(ifp); 13001da177e4SLinus Torvalds memcpy(addr.s6_addr, ifp->addr.s6_addr, 8); 13019d6280daSJiri Bohac ipv6_try_regen_rndid(idev, tmpaddr); 13021da177e4SLinus Torvalds memcpy(&addr.s6_addr[8], idev->rndid, 8); 130376f793e3SLorenzo Colitti age = (now - ifp->tstamp) / HZ; 13041da177e4SLinus Torvalds 1305eac55bf9SBenoit Boissinot regen_advance = idev->cnf.regen_max_retry * 1306eac55bf9SBenoit Boissinot idev->cnf.dad_transmits * 13071f9248e5SJiri Pirko NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ; 130876506a98SJiri Bohac 130976506a98SJiri Bohac /* recalculate max_desync_factor each time and update 131076506a98SJiri Bohac * idev->desync_factor if it's larger 131176506a98SJiri Bohac */ 13127aa8e63fSJiri Bohac cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft); 131376506a98SJiri Bohac max_desync_factor = min_t(__u32, 131476506a98SJiri Bohac idev->cnf.max_desync_factor, 13157aa8e63fSJiri Bohac cnf_temp_preferred_lft - regen_advance); 131676506a98SJiri Bohac 131776506a98SJiri Bohac if (unlikely(idev->desync_factor > max_desync_factor)) { 131876506a98SJiri Bohac if (max_desync_factor > 0) { 131976506a98SJiri Bohac get_random_bytes(&idev->desync_factor, 132076506a98SJiri Bohac sizeof(idev->desync_factor)); 132176506a98SJiri Bohac idev->desync_factor %= max_desync_factor; 132276506a98SJiri Bohac } else { 132376506a98SJiri Bohac idev->desync_factor = 0; 132476506a98SJiri Bohac } 132576506a98SJiri Bohac } 132676506a98SJiri Bohac 1327e6464b8cSDavid Ahern cfg.valid_lft = min_t(__u32, ifp->valid_lft, 132876506a98SJiri Bohac idev->cnf.temp_valid_lft + age); 1329e6464b8cSDavid Ahern cfg.preferred_lft = cnf_temp_preferred_lft + age - idev->desync_factor; 1330e6464b8cSDavid Ahern cfg.preferred_lft = min_t(__u32, ifp->prefered_lft, cfg.preferred_lft); 1331e6464b8cSDavid Ahern 1332e6464b8cSDavid Ahern cfg.plen = ifp->prefix_len; 133376506a98SJiri Bohac tmp_tstamp = ifp->tstamp; 133476506a98SJiri Bohac spin_unlock_bh(&ifp->lock); 133576506a98SJiri Bohac 133653bd6749SJiri Pirko write_unlock_bh(&idev->lock); 133795c385b4SNeil Horman 1338eac55bf9SBenoit Boissinot /* A temporary address is created only if this calculated Preferred 1339eac55bf9SBenoit Boissinot * Lifetime is greater than REGEN_ADVANCE time units. In particular, 1340eac55bf9SBenoit Boissinot * an implementation must not create a temporary address with a zero 1341eac55bf9SBenoit Boissinot * Preferred Lifetime. 1342ecab6701SHeiner Kallweit * Use age calculation as in addrconf_verify to avoid unnecessary 1343ecab6701SHeiner Kallweit * temporary addresses being generated. 1344eac55bf9SBenoit Boissinot */ 1345ecab6701SHeiner Kallweit age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; 1346e6464b8cSDavid Ahern if (cfg.preferred_lft <= regen_advance + age) { 1347eac55bf9SBenoit Boissinot in6_ifa_put(ifp); 1348eac55bf9SBenoit Boissinot in6_dev_put(idev); 1349eac55bf9SBenoit Boissinot ret = -1; 1350eac55bf9SBenoit Boissinot goto out; 1351eac55bf9SBenoit Boissinot } 1352eac55bf9SBenoit Boissinot 1353e6464b8cSDavid Ahern cfg.ifa_flags = IFA_F_TEMPORARY; 135495c385b4SNeil Horman /* set in addrconf_prefix_rcv() */ 135595c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 1356e6464b8cSDavid Ahern cfg.ifa_flags |= IFA_F_OPTIMISTIC; 135795c385b4SNeil Horman 1358e6464b8cSDavid Ahern cfg.pfx = &addr; 1359e6464b8cSDavid Ahern cfg.scope = ipv6_addr_scope(cfg.pfx); 13608308f3ffSDavid Ahern cfg.rt_priority = 0; 1361e6464b8cSDavid Ahern 1362e6464b8cSDavid Ahern ift = ipv6_add_addr(idev, &cfg, block, NULL); 13634b08a8f1SHannes Frederic Sowa if (IS_ERR(ift)) { 13641da177e4SLinus Torvalds in6_ifa_put(ifp); 13651da177e4SLinus Torvalds in6_dev_put(idev); 1366f3213831SJoe Perches pr_info("%s: retry temporary address regeneration\n", __func__); 13671da177e4SLinus Torvalds tmpaddr = &addr; 136853bd6749SJiri Pirko write_lock_bh(&idev->lock); 13691da177e4SLinus Torvalds goto retry; 13701da177e4SLinus Torvalds } 13711da177e4SLinus Torvalds 13721da177e4SLinus Torvalds spin_lock_bh(&ift->lock); 13731da177e4SLinus Torvalds ift->ifpub = ifp; 137476f793e3SLorenzo Colitti ift->cstamp = now; 13751da177e4SLinus Torvalds ift->tstamp = tmp_tstamp; 13761da177e4SLinus Torvalds spin_unlock_bh(&ift->lock); 13771da177e4SLinus Torvalds 1378cf22f9a2SDavid S. Miller addrconf_dad_start(ift); 13791da177e4SLinus Torvalds in6_ifa_put(ift); 13801da177e4SLinus Torvalds in6_dev_put(idev); 13811da177e4SLinus Torvalds out: 13821da177e4SLinus Torvalds return ret; 13831da177e4SLinus Torvalds } 13841da177e4SLinus Torvalds 13851da177e4SLinus Torvalds /* 1386072047e4SYOSHIFUJI Hideaki * Choose an appropriate source address (RFC3484) 13871da177e4SLinus Torvalds */ 1388a9b05723SYOSHIFUJI Hideaki enum { 1389a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_INIT = 0, 1390a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_LOCAL, 1391a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_SCOPE, 1392a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PREFERRED, 1393a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6 1394a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_HOA, 1395a9b05723SYOSHIFUJI Hideaki #endif 1396a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_OIF, 1397a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_LABEL, 1398a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PRIVACY, 1399a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_ORCHID, 1400a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PREFIX, 14017fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 14027fd2561eSErik Kline IPV6_SADDR_RULE_NOT_OPTIMISTIC, 14037fd2561eSErik Kline #endif 1404a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_MAX 1405072047e4SYOSHIFUJI Hideaki }; 1406072047e4SYOSHIFUJI Hideaki 1407a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score { 1408a9b05723SYOSHIFUJI Hideaki int rule; 1409a9b05723SYOSHIFUJI Hideaki int addr_type; 1410a9b05723SYOSHIFUJI Hideaki struct inet6_ifaddr *ifa; 1411a9b05723SYOSHIFUJI Hideaki DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX); 1412a9b05723SYOSHIFUJI Hideaki int scopedist; 1413a9b05723SYOSHIFUJI Hideaki int matchlen; 1414a9b05723SYOSHIFUJI Hideaki }; 1415a9b05723SYOSHIFUJI Hideaki 1416a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst { 14179acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *addr; 1418a9b05723SYOSHIFUJI Hideaki int ifindex; 1419a9b05723SYOSHIFUJI Hideaki int scope; 1420a9b05723SYOSHIFUJI Hideaki int label; 14217cbca67cSYOSHIFUJI Hideaki unsigned int prefs; 1422a9b05723SYOSHIFUJI Hideaki }; 1423072047e4SYOSHIFUJI Hideaki 1424b6f99a21SDave Jones static inline int ipv6_saddr_preferred(int type) 14251da177e4SLinus Torvalds { 142645bb0060SUlrich Weber if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK)) 1427072047e4SYOSHIFUJI Hideaki return 1; 1428072047e4SYOSHIFUJI Hideaki return 0; 14291da177e4SLinus Torvalds } 14301da177e4SLinus Torvalds 143135e015e1SMatteo Croce static bool ipv6_use_optimistic_addr(struct net *net, 143235e015e1SMatteo Croce struct inet6_dev *idev) 14337fd2561eSErik Kline { 14347fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 143535e015e1SMatteo Croce if (!idev) 143635e015e1SMatteo Croce return false; 143735e015e1SMatteo Croce if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad) 143835e015e1SMatteo Croce return false; 143935e015e1SMatteo Croce if (!net->ipv6.devconf_all->use_optimistic && !idev->cnf.use_optimistic) 144035e015e1SMatteo Croce return false; 144135e015e1SMatteo Croce 144235e015e1SMatteo Croce return true; 14437fd2561eSErik Kline #else 14447fd2561eSErik Kline return false; 14457fd2561eSErik Kline #endif 14467fd2561eSErik Kline } 14477fd2561eSErik Kline 1448f1c02cfbSSabrina Dubroca static bool ipv6_allow_optimistic_dad(struct net *net, 1449f1c02cfbSSabrina Dubroca struct inet6_dev *idev) 1450f1c02cfbSSabrina Dubroca { 1451f1c02cfbSSabrina Dubroca #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 1452f1c02cfbSSabrina Dubroca if (!idev) 1453f1c02cfbSSabrina Dubroca return false; 1454f1c02cfbSSabrina Dubroca if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad) 1455f1c02cfbSSabrina Dubroca return false; 1456f1c02cfbSSabrina Dubroca 1457f1c02cfbSSabrina Dubroca return true; 1458f1c02cfbSSabrina Dubroca #else 1459f1c02cfbSSabrina Dubroca return false; 1460f1c02cfbSSabrina Dubroca #endif 1461f1c02cfbSSabrina Dubroca } 1462f1c02cfbSSabrina Dubroca 14633de23255SBenjamin Thery static int ipv6_get_saddr_eval(struct net *net, 14643de23255SBenjamin Thery struct ipv6_saddr_score *score, 1465a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst *dst, 1466a9b05723SYOSHIFUJI Hideaki int i) 1467a9b05723SYOSHIFUJI Hideaki { 1468a9b05723SYOSHIFUJI Hideaki int ret; 1469a9b05723SYOSHIFUJI Hideaki 1470a9b05723SYOSHIFUJI Hideaki if (i <= score->rule) { 1471a9b05723SYOSHIFUJI Hideaki switch (i) { 1472a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_SCOPE: 1473a9b05723SYOSHIFUJI Hideaki ret = score->scopedist; 1474a9b05723SYOSHIFUJI Hideaki break; 1475a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFIX: 1476a9b05723SYOSHIFUJI Hideaki ret = score->matchlen; 1477a9b05723SYOSHIFUJI Hideaki break; 1478a9b05723SYOSHIFUJI Hideaki default: 1479a9b05723SYOSHIFUJI Hideaki ret = !!test_bit(i, score->scorebits); 1480a9b05723SYOSHIFUJI Hideaki } 1481a9b05723SYOSHIFUJI Hideaki goto out; 1482a9b05723SYOSHIFUJI Hideaki } 1483a9b05723SYOSHIFUJI Hideaki 1484a9b05723SYOSHIFUJI Hideaki switch (i) { 1485a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_INIT: 1486a9b05723SYOSHIFUJI Hideaki /* Rule 0: remember if hiscore is not ready yet */ 1487a9b05723SYOSHIFUJI Hideaki ret = !!score->ifa; 1488a9b05723SYOSHIFUJI Hideaki break; 1489a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_LOCAL: 1490a9b05723SYOSHIFUJI Hideaki /* Rule 1: Prefer same address */ 1491a9b05723SYOSHIFUJI Hideaki ret = ipv6_addr_equal(&score->ifa->addr, dst->addr); 1492a9b05723SYOSHIFUJI Hideaki break; 1493a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_SCOPE: 1494a9b05723SYOSHIFUJI Hideaki /* Rule 2: Prefer appropriate scope 1495a9b05723SYOSHIFUJI Hideaki * 1496a9b05723SYOSHIFUJI Hideaki * ret 1497a9b05723SYOSHIFUJI Hideaki * ^ 1498a9b05723SYOSHIFUJI Hideaki * -1 | d 15 1499a9b05723SYOSHIFUJI Hideaki * ---+--+-+---> scope 1500a9b05723SYOSHIFUJI Hideaki * | 1501a9b05723SYOSHIFUJI Hideaki * | d is scope of the destination. 1502a9b05723SYOSHIFUJI Hideaki * B-d | \ 1503a9b05723SYOSHIFUJI Hideaki * | \ <- smaller scope is better if 1504db9c7c39Sstephen hemminger * B-15 | \ if scope is enough for destination. 1505a9b05723SYOSHIFUJI Hideaki * | ret = B - scope (-1 <= scope >= d <= 15). 1506a9b05723SYOSHIFUJI Hideaki * d-C-1 | / 1507a9b05723SYOSHIFUJI Hideaki * |/ <- greater is better 1508a9b05723SYOSHIFUJI Hideaki * -C / if scope is not enough for destination. 1509a9b05723SYOSHIFUJI Hideaki * /| ret = scope - C (-1 <= d < scope <= 15). 1510a9b05723SYOSHIFUJI Hideaki * 1511a9b05723SYOSHIFUJI Hideaki * d - C - 1 < B -15 (for all -1 <= d <= 15). 1512a9b05723SYOSHIFUJI Hideaki * C > d + 14 - B >= 15 + 14 - B = 29 - B. 1513a9b05723SYOSHIFUJI Hideaki * Assume B = 0 and we get C > 29. 1514a9b05723SYOSHIFUJI Hideaki */ 1515a9b05723SYOSHIFUJI Hideaki ret = __ipv6_addr_src_scope(score->addr_type); 1516a9b05723SYOSHIFUJI Hideaki if (ret >= dst->scope) 1517a9b05723SYOSHIFUJI Hideaki ret = -ret; 1518a9b05723SYOSHIFUJI Hideaki else 1519a9b05723SYOSHIFUJI Hideaki ret -= 128; /* 30 is enough */ 1520a9b05723SYOSHIFUJI Hideaki score->scopedist = ret; 1521a9b05723SYOSHIFUJI Hideaki break; 1522a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFERRED: 15237fd2561eSErik Kline { 1524a9b05723SYOSHIFUJI Hideaki /* Rule 3: Avoid deprecated and optimistic addresses */ 15257fd2561eSErik Kline u8 avoid = IFA_F_DEPRECATED; 15267fd2561eSErik Kline 152735e015e1SMatteo Croce if (!ipv6_use_optimistic_addr(net, score->ifa->idev)) 15287fd2561eSErik Kline avoid |= IFA_F_OPTIMISTIC; 1529a9b05723SYOSHIFUJI Hideaki ret = ipv6_saddr_preferred(score->addr_type) || 15307fd2561eSErik Kline !(score->ifa->flags & avoid); 1531a9b05723SYOSHIFUJI Hideaki break; 15327fd2561eSErik Kline } 1533a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6 1534a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_HOA: 15357cbca67cSYOSHIFUJI Hideaki { 1536a9b05723SYOSHIFUJI Hideaki /* Rule 4: Prefer home address */ 15377cbca67cSYOSHIFUJI Hideaki int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA); 15387cbca67cSYOSHIFUJI Hideaki ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome; 1539a9b05723SYOSHIFUJI Hideaki break; 15407cbca67cSYOSHIFUJI Hideaki } 1541a9b05723SYOSHIFUJI Hideaki #endif 1542a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_OIF: 1543a9b05723SYOSHIFUJI Hideaki /* Rule 5: Prefer outgoing interface */ 1544a9b05723SYOSHIFUJI Hideaki ret = (!dst->ifindex || 1545a9b05723SYOSHIFUJI Hideaki dst->ifindex == score->ifa->idev->dev->ifindex); 1546a9b05723SYOSHIFUJI Hideaki break; 1547a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_LABEL: 1548a9b05723SYOSHIFUJI Hideaki /* Rule 6: Prefer matching label */ 15493de23255SBenjamin Thery ret = ipv6_addr_label(net, 15503de23255SBenjamin Thery &score->ifa->addr, score->addr_type, 1551a9b05723SYOSHIFUJI Hideaki score->ifa->idev->dev->ifindex) == dst->label; 1552a9b05723SYOSHIFUJI Hideaki break; 1553a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PRIVACY: 15547cbca67cSYOSHIFUJI Hideaki { 1555a9b05723SYOSHIFUJI Hideaki /* Rule 7: Prefer public address 155625985edcSLucas De Marchi * Note: prefer temporary address if use_tempaddr >= 2 1557a9b05723SYOSHIFUJI Hideaki */ 15587cbca67cSYOSHIFUJI Hideaki int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ? 15597cbca67cSYOSHIFUJI Hideaki !!(dst->prefs & IPV6_PREFER_SRC_TMP) : 15607cbca67cSYOSHIFUJI Hideaki score->ifa->idev->cnf.use_tempaddr >= 2; 15617cbca67cSYOSHIFUJI Hideaki ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp; 1562a9b05723SYOSHIFUJI Hideaki break; 15637cbca67cSYOSHIFUJI Hideaki } 1564a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_ORCHID: 1565a9b05723SYOSHIFUJI Hideaki /* Rule 8-: Prefer ORCHID vs ORCHID or 1566a9b05723SYOSHIFUJI Hideaki * non-ORCHID vs non-ORCHID 1567a9b05723SYOSHIFUJI Hideaki */ 1568a9b05723SYOSHIFUJI Hideaki ret = !(ipv6_addr_orchid(&score->ifa->addr) ^ 1569a9b05723SYOSHIFUJI Hideaki ipv6_addr_orchid(dst->addr)); 1570a9b05723SYOSHIFUJI Hideaki break; 1571a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFIX: 1572a9b05723SYOSHIFUJI Hideaki /* Rule 8: Use longest matching prefix */ 157391b4b04fSYOSHIFUJI Hideaki / 吉藤英明 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr); 157491b4b04fSYOSHIFUJI Hideaki / 吉藤英明 if (ret > score->ifa->prefix_len) 157591b4b04fSYOSHIFUJI Hideaki / 吉藤英明 ret = score->ifa->prefix_len; 157691b4b04fSYOSHIFUJI Hideaki / 吉藤英明 score->matchlen = ret; 1577a9b05723SYOSHIFUJI Hideaki break; 15787fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 15797fd2561eSErik Kline case IPV6_SADDR_RULE_NOT_OPTIMISTIC: 15807fd2561eSErik Kline /* Optimistic addresses still have lower precedence than other 15817fd2561eSErik Kline * preferred addresses. 15827fd2561eSErik Kline */ 15837fd2561eSErik Kline ret = !(score->ifa->flags & IFA_F_OPTIMISTIC); 15847fd2561eSErik Kline break; 15857fd2561eSErik Kline #endif 1586a9b05723SYOSHIFUJI Hideaki default: 1587a9b05723SYOSHIFUJI Hideaki ret = 0; 1588a9b05723SYOSHIFUJI Hideaki } 1589a9b05723SYOSHIFUJI Hideaki 1590a9b05723SYOSHIFUJI Hideaki if (ret) 1591a9b05723SYOSHIFUJI Hideaki __set_bit(i, score->scorebits); 1592a9b05723SYOSHIFUJI Hideaki score->rule = i; 1593a9b05723SYOSHIFUJI Hideaki out: 1594a9b05723SYOSHIFUJI Hideaki return ret; 1595a9b05723SYOSHIFUJI Hideaki } 1596a9b05723SYOSHIFUJI Hideaki 1597c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 static int __ipv6_dev_get_saddr(struct net *net, 15989131f3deSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_dst *dst, 15999131f3deSYOSHIFUJI Hideaki/吉藤英明 struct inet6_dev *idev, 1600c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_score *scores, 1601c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 int hiscore_idx) 16021da177e4SLinus Torvalds { 1603c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx]; 1604072047e4SYOSHIFUJI Hideaki 1605f59c031eSEric Dumazet list_for_each_entry_rcu(score->ifa, &idev->addr_list, if_list) { 1606a9b05723SYOSHIFUJI Hideaki int i; 1607072047e4SYOSHIFUJI Hideaki 1608a9b05723SYOSHIFUJI Hideaki /* 16096b3ae80aSYOSHIFUJI Hideaki * - Tentative Address (RFC2462 section 5.4) 16106b3ae80aSYOSHIFUJI Hideaki * - A tentative address is not considered 16116b3ae80aSYOSHIFUJI Hideaki * "assigned to an interface" in the traditional 161295c385b4SNeil Horman * sense, unless it is also flagged as optimistic. 16136b3ae80aSYOSHIFUJI Hideaki * - Candidate Source Address (section 4) 1614072047e4SYOSHIFUJI Hideaki * - In any case, anycast addresses, multicast 1615072047e4SYOSHIFUJI Hideaki * addresses, and the unspecified address MUST 1616072047e4SYOSHIFUJI Hideaki * NOT be included in a candidate set. 1617072047e4SYOSHIFUJI Hideaki */ 1618a9b05723SYOSHIFUJI Hideaki if ((score->ifa->flags & IFA_F_TENTATIVE) && 1619a9b05723SYOSHIFUJI Hideaki (!(score->ifa->flags & IFA_F_OPTIMISTIC))) 16206b3ae80aSYOSHIFUJI Hideaki continue; 1621a9b05723SYOSHIFUJI Hideaki 1622a9b05723SYOSHIFUJI Hideaki score->addr_type = __ipv6_addr_type(&score->ifa->addr); 1623a9b05723SYOSHIFUJI Hideaki 1624a9b05723SYOSHIFUJI Hideaki if (unlikely(score->addr_type == IPV6_ADDR_ANY || 1625a9b05723SYOSHIFUJI Hideaki score->addr_type & IPV6_ADDR_MULTICAST)) { 1626ba7a46f1SJoe Perches net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s", 16279131f3deSYOSHIFUJI Hideaki/吉藤英明 idev->dev->name); 16281da177e4SLinus Torvalds continue; 1629072047e4SYOSHIFUJI Hideaki } 1630072047e4SYOSHIFUJI Hideaki 1631a9b05723SYOSHIFUJI Hideaki score->rule = -1; 1632a9b05723SYOSHIFUJI Hideaki bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX); 1633072047e4SYOSHIFUJI Hideaki 1634a9b05723SYOSHIFUJI Hideaki for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) { 1635a9b05723SYOSHIFUJI Hideaki int minihiscore, miniscore; 1636072047e4SYOSHIFUJI Hideaki 16379131f3deSYOSHIFUJI Hideaki/吉藤英明 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i); 16389131f3deSYOSHIFUJI Hideaki/吉藤英明 miniscore = ipv6_get_saddr_eval(net, score, dst, i); 1639072047e4SYOSHIFUJI Hideaki 1640a9b05723SYOSHIFUJI Hideaki if (minihiscore > miniscore) { 1641a9b05723SYOSHIFUJI Hideaki if (i == IPV6_SADDR_RULE_SCOPE && 1642a9b05723SYOSHIFUJI Hideaki score->scopedist > 0) { 1643a9b05723SYOSHIFUJI Hideaki /* 1644a9b05723SYOSHIFUJI Hideaki * special case: 1645a9b05723SYOSHIFUJI Hideaki * each remaining entry 1646a9b05723SYOSHIFUJI Hideaki * has too small (not enough) 1647a9b05723SYOSHIFUJI Hideaki * scope, because ifa entries 1648a9b05723SYOSHIFUJI Hideaki * are sorted by their scope 1649a9b05723SYOSHIFUJI Hideaki * values. 1650072047e4SYOSHIFUJI Hideaki */ 16519131f3deSYOSHIFUJI Hideaki/吉藤英明 goto out; 1652072047e4SYOSHIFUJI Hideaki } 1653a9b05723SYOSHIFUJI Hideaki break; 1654a9b05723SYOSHIFUJI Hideaki } else if (minihiscore < miniscore) { 1655a0bffffcSIlpo Järvinen swap(hiscore, score); 1656c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 hiscore_idx = 1 - hiscore_idx; 1657a9b05723SYOSHIFUJI Hideaki 1658a9b05723SYOSHIFUJI Hideaki /* restore our iterator */ 1659a9b05723SYOSHIFUJI Hideaki score->ifa = hiscore->ifa; 1660a9b05723SYOSHIFUJI Hideaki 1661a9b05723SYOSHIFUJI Hideaki break; 16621da177e4SLinus Torvalds } 1663a9b05723SYOSHIFUJI Hideaki } 1664a9b05723SYOSHIFUJI Hideaki } 16659131f3deSYOSHIFUJI Hideaki/吉藤英明 out: 1666c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 return hiscore_idx; 16671da177e4SLinus Torvalds } 16689131f3deSYOSHIFUJI Hideaki/吉藤英明 1669afbac601SDavid Ahern static int ipv6_get_saddr_master(struct net *net, 1670afbac601SDavid Ahern const struct net_device *dst_dev, 1671afbac601SDavid Ahern const struct net_device *master, 1672afbac601SDavid Ahern struct ipv6_saddr_dst *dst, 1673afbac601SDavid Ahern struct ipv6_saddr_score *scores, 1674afbac601SDavid Ahern int hiscore_idx) 1675afbac601SDavid Ahern { 1676afbac601SDavid Ahern struct inet6_dev *idev; 1677afbac601SDavid Ahern 1678afbac601SDavid Ahern idev = __in6_dev_get(dst_dev); 1679afbac601SDavid Ahern if (idev) 1680afbac601SDavid Ahern hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev, 1681afbac601SDavid Ahern scores, hiscore_idx); 1682afbac601SDavid Ahern 1683afbac601SDavid Ahern idev = __in6_dev_get(master); 1684afbac601SDavid Ahern if (idev) 1685afbac601SDavid Ahern hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev, 1686afbac601SDavid Ahern scores, hiscore_idx); 1687afbac601SDavid Ahern 1688afbac601SDavid Ahern return hiscore_idx; 1689afbac601SDavid Ahern } 1690afbac601SDavid Ahern 16919131f3deSYOSHIFUJI Hideaki/吉藤英明 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev, 16929131f3deSYOSHIFUJI Hideaki/吉藤英明 const struct in6_addr *daddr, unsigned int prefs, 16939131f3deSYOSHIFUJI Hideaki/吉藤英明 struct in6_addr *saddr) 16949131f3deSYOSHIFUJI Hideaki/吉藤英明 { 1695c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_score scores[2], *hiscore; 16969131f3deSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_dst dst; 16979131f3deSYOSHIFUJI Hideaki/吉藤英明 struct inet6_dev *idev; 16989131f3deSYOSHIFUJI Hideaki/吉藤英明 struct net_device *dev; 16999131f3deSYOSHIFUJI Hideaki/吉藤英明 int dst_type; 17009131f3deSYOSHIFUJI Hideaki/吉藤英明 bool use_oif_addr = false; 1701c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 int hiscore_idx = 0; 1702cc429c8fSEric Dumazet int ret = 0; 17039131f3deSYOSHIFUJI Hideaki/吉藤英明 17049131f3deSYOSHIFUJI Hideaki/吉藤英明 dst_type = __ipv6_addr_type(daddr); 17059131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.addr = daddr; 17069131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.ifindex = dst_dev ? dst_dev->ifindex : 0; 17079131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.scope = __ipv6_addr_src_scope(dst_type); 17089131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex); 17099131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.prefs = prefs; 17109131f3deSYOSHIFUJI Hideaki/吉藤英明 1711c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 scores[hiscore_idx].rule = -1; 1712c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 scores[hiscore_idx].ifa = NULL; 17139131f3deSYOSHIFUJI Hideaki/吉藤英明 17149131f3deSYOSHIFUJI Hideaki/吉藤英明 rcu_read_lock(); 17159131f3deSYOSHIFUJI Hideaki/吉藤英明 17169131f3deSYOSHIFUJI Hideaki/吉藤英明 /* Candidate Source Address (section 4) 17179131f3deSYOSHIFUJI Hideaki/吉藤英明 * - multicast and link-local destination address, 17189131f3deSYOSHIFUJI Hideaki/吉藤英明 * the set of candidate source address MUST only 17199131f3deSYOSHIFUJI Hideaki/吉藤英明 * include addresses assigned to interfaces 17209131f3deSYOSHIFUJI Hideaki/吉藤英明 * belonging to the same link as the outgoing 17219131f3deSYOSHIFUJI Hideaki/吉藤英明 * interface. 17229131f3deSYOSHIFUJI Hideaki/吉藤英明 * (- For site-local destination addresses, the 17239131f3deSYOSHIFUJI Hideaki/吉藤英明 * set of candidate source addresses MUST only 17249131f3deSYOSHIFUJI Hideaki/吉藤英明 * include addresses assigned to interfaces 17259131f3deSYOSHIFUJI Hideaki/吉藤英明 * belonging to the same site as the outgoing 17269131f3deSYOSHIFUJI Hideaki/吉藤英明 * interface.) 17273985e8a3SErik Kline * - "It is RECOMMENDED that the candidate source addresses 17283985e8a3SErik Kline * be the set of unicast addresses assigned to the 17293985e8a3SErik Kline * interface that will be used to send to the destination 17303985e8a3SErik Kline * (the 'outgoing' interface)." (RFC 6724) 17319131f3deSYOSHIFUJI Hideaki/吉藤英明 */ 17329131f3deSYOSHIFUJI Hideaki/吉藤英明 if (dst_dev) { 17339131f3deSYOSHIFUJI Hideaki/吉藤英明 idev = __in6_dev_get(dst_dev); 17343985e8a3SErik Kline if ((dst_type & IPV6_ADDR_MULTICAST) || 17353985e8a3SErik Kline dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL || 17363985e8a3SErik Kline (idev && idev->cnf.use_oif_addrs_only)) { 17379131f3deSYOSHIFUJI Hideaki/吉藤英明 use_oif_addr = true; 17389131f3deSYOSHIFUJI Hideaki/吉藤英明 } 17399131f3deSYOSHIFUJI Hideaki/吉藤英明 } 17409131f3deSYOSHIFUJI Hideaki/吉藤英明 17419131f3deSYOSHIFUJI Hideaki/吉藤英明 if (use_oif_addr) { 1742c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 if (idev) 1743c15df306SYOSHIFUJI Hideaki hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx); 17449131f3deSYOSHIFUJI Hideaki/吉藤英明 } else { 1745afbac601SDavid Ahern const struct net_device *master; 1746afbac601SDavid Ahern int master_idx = 0; 1747afbac601SDavid Ahern 1748afbac601SDavid Ahern /* if dst_dev exists and is enslaved to an L3 device, then 1749afbac601SDavid Ahern * prefer addresses from dst_dev and then the master over 1750afbac601SDavid Ahern * any other enslaved devices in the L3 domain. 1751afbac601SDavid Ahern */ 1752afbac601SDavid Ahern master = l3mdev_master_dev_rcu(dst_dev); 1753afbac601SDavid Ahern if (master) { 1754afbac601SDavid Ahern master_idx = master->ifindex; 1755afbac601SDavid Ahern 1756afbac601SDavid Ahern hiscore_idx = ipv6_get_saddr_master(net, dst_dev, 1757afbac601SDavid Ahern master, &dst, 1758afbac601SDavid Ahern scores, hiscore_idx); 1759afbac601SDavid Ahern 1760afbac601SDavid Ahern if (scores[hiscore_idx].ifa) 1761afbac601SDavid Ahern goto out; 1762afbac601SDavid Ahern } 1763afbac601SDavid Ahern 17649131f3deSYOSHIFUJI Hideaki/吉藤英明 for_each_netdev_rcu(net, dev) { 1765afbac601SDavid Ahern /* only consider addresses on devices in the 1766afbac601SDavid Ahern * same L3 domain 1767afbac601SDavid Ahern */ 1768afbac601SDavid Ahern if (l3mdev_master_ifindex_rcu(dev) != master_idx) 1769afbac601SDavid Ahern continue; 17709131f3deSYOSHIFUJI Hideaki/吉藤英明 idev = __in6_dev_get(dev); 17719131f3deSYOSHIFUJI Hideaki/吉藤英明 if (!idev) 17729131f3deSYOSHIFUJI Hideaki/吉藤英明 continue; 1773c15df306SYOSHIFUJI Hideaki hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx); 17749131f3deSYOSHIFUJI Hideaki/吉藤英明 } 17759131f3deSYOSHIFUJI Hideaki/吉藤英明 } 1776afbac601SDavid Ahern 1777afbac601SDavid Ahern out: 1778c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 hiscore = &scores[hiscore_idx]; 1779a9b05723SYOSHIFUJI Hideaki if (!hiscore->ifa) 1780cc429c8fSEric Dumazet ret = -EADDRNOTAVAIL; 1781cc429c8fSEric Dumazet else 17824e3fd7a0SAlexey Dobriyan *saddr = hiscore->ifa->addr; 1783cc429c8fSEric Dumazet 1784cc429c8fSEric Dumazet rcu_read_unlock(); 1785cc429c8fSEric Dumazet return ret; 17861da177e4SLinus Torvalds } 17875e5f3f0fSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_dev_get_saddr); 17881da177e4SLinus Torvalds 17898965779dSAmerigo Wang int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr, 1790479840ffSJiri Pirko u32 banned_flags) 1791b7b1bfceSHannes Frederic Sowa { 1792b7b1bfceSHannes Frederic Sowa struct inet6_ifaddr *ifp; 1793b7b1bfceSHannes Frederic Sowa int err = -EADDRNOTAVAIL; 1794b7b1bfceSHannes Frederic Sowa 1795602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) { 1796602582caSHannes Frederic Sowa if (ifp->scope > IFA_LINK) 1797602582caSHannes Frederic Sowa break; 1798b7b1bfceSHannes Frederic Sowa if (ifp->scope == IFA_LINK && 1799b7b1bfceSHannes Frederic Sowa !(ifp->flags & banned_flags)) { 1800b7b1bfceSHannes Frederic Sowa *addr = ifp->addr; 1801b7b1bfceSHannes Frederic Sowa err = 0; 1802b7b1bfceSHannes Frederic Sowa break; 1803b7b1bfceSHannes Frederic Sowa } 1804b7b1bfceSHannes Frederic Sowa } 1805b7b1bfceSHannes Frederic Sowa return err; 1806b7b1bfceSHannes Frederic Sowa } 1807b7b1bfceSHannes Frederic Sowa 180895c385b4SNeil Horman int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr, 1809479840ffSJiri Pirko u32 banned_flags) 18101da177e4SLinus Torvalds { 18111da177e4SLinus Torvalds struct inet6_dev *idev; 18121da177e4SLinus Torvalds int err = -EADDRNOTAVAIL; 18131da177e4SLinus Torvalds 18148814c4b5SYOSHIFUJI Hideaki rcu_read_lock(); 1815e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 1816e21e8467SStephen Hemminger if (idev) { 18171da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1818b7b1bfceSHannes Frederic Sowa err = __ipv6_get_lladdr(idev, addr, banned_flags); 18191da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 18201da177e4SLinus Torvalds } 18218814c4b5SYOSHIFUJI Hideaki rcu_read_unlock(); 18221da177e4SLinus Torvalds return err; 18231da177e4SLinus Torvalds } 18241da177e4SLinus Torvalds 1825d9bf82c2SEric Dumazet static int ipv6_count_addresses(const struct inet6_dev *idev) 18261da177e4SLinus Torvalds { 1827d9bf82c2SEric Dumazet const struct inet6_ifaddr *ifp; 18281da177e4SLinus Torvalds int cnt = 0; 18291da177e4SLinus Torvalds 1830d9bf82c2SEric Dumazet rcu_read_lock(); 1831d9bf82c2SEric Dumazet list_for_each_entry_rcu(ifp, &idev->addr_list, if_list) 18321da177e4SLinus Torvalds cnt++; 1833d9bf82c2SEric Dumazet rcu_read_unlock(); 18341da177e4SLinus Torvalds return cnt; 18351da177e4SLinus Torvalds } 18361da177e4SLinus Torvalds 1837b71d1d42SEric Dumazet int ipv6_chk_addr(struct net *net, const struct in6_addr *addr, 18382a7851bfSFlorian Westphal const struct net_device *dev, int strict) 18391da177e4SLinus Torvalds { 1840232378e8SDavid Ahern return ipv6_chk_addr_and_flags(net, addr, dev, !dev, 1841232378e8SDavid Ahern strict, IFA_F_TENTATIVE); 1842c58da4c6SErik Kline } 1843c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr); 1844c58da4c6SErik Kline 18451893ff20SDavid Ahern /* device argument is used to find the L3 domain of interest. If 18461893ff20SDavid Ahern * skip_dev_check is set, then the ifp device is not checked against 18471893ff20SDavid Ahern * the passed in dev argument. So the 2 cases for addresses checks are: 18481893ff20SDavid Ahern * 1. does the address exist in the L3 domain that dev is part of 18491893ff20SDavid Ahern * (skip_dev_check = true), or 18501893ff20SDavid Ahern * 18511893ff20SDavid Ahern * 2. does the address exist on the specific device 18521893ff20SDavid Ahern * (skip_dev_check = false) 18531893ff20SDavid Ahern */ 1854c58da4c6SErik Kline int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr, 1855232378e8SDavid Ahern const struct net_device *dev, bool skip_dev_check, 1856232378e8SDavid Ahern int strict, u32 banned_flags) 1857c58da4c6SErik Kline { 18583f27fb23SEric Dumazet unsigned int hash = inet6_addr_hash(net, addr); 18591893ff20SDavid Ahern const struct net_device *l3mdev; 1860eedf042aSStephen Hemminger struct inet6_ifaddr *ifp; 1861c58da4c6SErik Kline u32 ifp_flags; 18621da177e4SLinus Torvalds 1863480318a0SEric Dumazet rcu_read_lock(); 1864232378e8SDavid Ahern 18651893ff20SDavid Ahern l3mdev = l3mdev_master_dev_rcu(dev); 1866232378e8SDavid Ahern if (skip_dev_check) 1867232378e8SDavid Ahern dev = NULL; 1868232378e8SDavid Ahern 1869b67bfe0dSSasha Levin hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) { 1870878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 1871bfeade08SDaniel Lezcano continue; 18721893ff20SDavid Ahern 18731893ff20SDavid Ahern if (l3mdev_master_dev_rcu(ifp->idev->dev) != l3mdev) 18741893ff20SDavid Ahern continue; 18751893ff20SDavid Ahern 1876c58da4c6SErik Kline /* Decouple optimistic from tentative for evaluation here. 1877c58da4c6SErik Kline * Ban optimistic addresses explicitly, when required. 1878c58da4c6SErik Kline */ 1879c58da4c6SErik Kline ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC) 1880c58da4c6SErik Kline ? (ifp->flags&~IFA_F_TENTATIVE) 1881c58da4c6SErik Kline : ifp->flags; 18821da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr) && 1883c58da4c6SErik Kline !(ifp_flags&banned_flags) && 188463159f29SIan Morris (!dev || ifp->idev->dev == dev || 1885eedf042aSStephen Hemminger !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) { 1886480318a0SEric Dumazet rcu_read_unlock(); 1887eedf042aSStephen Hemminger return 1; 1888eedf042aSStephen Hemminger } 1889eedf042aSStephen Hemminger } 18905c578aedSstephen hemminger 1891480318a0SEric Dumazet rcu_read_unlock(); 1892eedf042aSStephen Hemminger return 0; 18931da177e4SLinus Torvalds } 1894c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr_and_flags); 18957159039aSYOSHIFUJI Hideaki 18961da177e4SLinus Torvalds 18977df37ff3SCatalin\(ux\) M. BOIE /* Compares an address/prefix_len with addresses on device @dev. 18987df37ff3SCatalin\(ux\) M. BOIE * If one is found it returns true. 18997df37ff3SCatalin\(ux\) M. BOIE */ 19007df37ff3SCatalin\(ux\) M. BOIE bool ipv6_chk_custom_prefix(const struct in6_addr *addr, 19017df37ff3SCatalin\(ux\) M. BOIE const unsigned int prefix_len, struct net_device *dev) 19027df37ff3SCatalin\(ux\) M. BOIE { 190347e26941SEric Dumazet const struct inet6_ifaddr *ifa; 190447e26941SEric Dumazet const struct inet6_dev *idev; 19057df37ff3SCatalin\(ux\) M. BOIE bool ret = false; 19067df37ff3SCatalin\(ux\) M. BOIE 19077df37ff3SCatalin\(ux\) M. BOIE rcu_read_lock(); 19087df37ff3SCatalin\(ux\) M. BOIE idev = __in6_dev_get(dev); 19097df37ff3SCatalin\(ux\) M. BOIE if (idev) { 191047e26941SEric Dumazet list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) { 19117df37ff3SCatalin\(ux\) M. BOIE ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len); 19127df37ff3SCatalin\(ux\) M. BOIE if (ret) 19137df37ff3SCatalin\(ux\) M. BOIE break; 19147df37ff3SCatalin\(ux\) M. BOIE } 19157df37ff3SCatalin\(ux\) M. BOIE } 19167df37ff3SCatalin\(ux\) M. BOIE rcu_read_unlock(); 19177df37ff3SCatalin\(ux\) M. BOIE 19187df37ff3SCatalin\(ux\) M. BOIE return ret; 19197df37ff3SCatalin\(ux\) M. BOIE } 19207df37ff3SCatalin\(ux\) M. BOIE EXPORT_SYMBOL(ipv6_chk_custom_prefix); 19217df37ff3SCatalin\(ux\) M. BOIE 1922b71d1d42SEric Dumazet int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev) 192352eeeb84SYOSHIFUJI Hideaki { 192424ba333bSEric Dumazet const struct inet6_ifaddr *ifa; 192524ba333bSEric Dumazet const struct inet6_dev *idev; 192652eeeb84SYOSHIFUJI Hideaki int onlink; 192752eeeb84SYOSHIFUJI Hideaki 192852eeeb84SYOSHIFUJI Hideaki onlink = 0; 192952eeeb84SYOSHIFUJI Hideaki rcu_read_lock(); 193052eeeb84SYOSHIFUJI Hideaki idev = __in6_dev_get(dev); 193152eeeb84SYOSHIFUJI Hideaki if (idev) { 193224ba333bSEric Dumazet list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) { 193352eeeb84SYOSHIFUJI Hideaki onlink = ipv6_prefix_equal(addr, &ifa->addr, 193452eeeb84SYOSHIFUJI Hideaki ifa->prefix_len); 193552eeeb84SYOSHIFUJI Hideaki if (onlink) 193652eeeb84SYOSHIFUJI Hideaki break; 193752eeeb84SYOSHIFUJI Hideaki } 193852eeeb84SYOSHIFUJI Hideaki } 193952eeeb84SYOSHIFUJI Hideaki rcu_read_unlock(); 194052eeeb84SYOSHIFUJI Hideaki return onlink; 194152eeeb84SYOSHIFUJI Hideaki } 194252eeeb84SYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_prefix); 194352eeeb84SYOSHIFUJI Hideaki 19449acd9f3aSYOSHIFUJI Hideaki struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr, 19451cab3da6SDaniel Lezcano struct net_device *dev, int strict) 19461da177e4SLinus Torvalds { 19473f27fb23SEric Dumazet unsigned int hash = inet6_addr_hash(net, addr); 1948b79d1d54SDavid S. Miller struct inet6_ifaddr *ifp, *result = NULL; 19491da177e4SLinus Torvalds 195024f226daSEric Dumazet rcu_read_lock(); 195124f226daSEric Dumazet hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) { 1952878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 19531cab3da6SDaniel Lezcano continue; 19541da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr)) { 195563159f29SIan Morris if (!dev || ifp->idev->dev == dev || 19561da177e4SLinus Torvalds !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) { 1957b79d1d54SDavid S. Miller result = ifp; 19581da177e4SLinus Torvalds in6_ifa_hold(ifp); 19591da177e4SLinus Torvalds break; 19601da177e4SLinus Torvalds } 19611da177e4SLinus Torvalds } 19621da177e4SLinus Torvalds } 196324f226daSEric Dumazet rcu_read_unlock(); 19641da177e4SLinus Torvalds 1965b79d1d54SDavid S. Miller return result; 19661da177e4SLinus Torvalds } 19671da177e4SLinus Torvalds 19681da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */ 19691da177e4SLinus Torvalds 1970cc411d0bSBrian Haley static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed) 19711da177e4SLinus Torvalds { 19723d171f39SLubomir Rintel if (dad_failed) 19733d171f39SLubomir Rintel ifp->flags |= IFA_F_DADFAILED; 19743d171f39SLubomir Rintel 1975ec8add2aSSabrina Dubroca if (ifp->flags&IFA_F_TEMPORARY) { 19761da177e4SLinus Torvalds struct inet6_ifaddr *ifpub; 19771da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 19781da177e4SLinus Torvalds ifpub = ifp->ifpub; 19791da177e4SLinus Torvalds if (ifpub) { 19801da177e4SLinus Torvalds in6_ifa_hold(ifpub); 19811da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 1982fffcefe9SEric Dumazet ipv6_create_tempaddr(ifpub, ifp, true); 19831da177e4SLinus Torvalds in6_ifa_put(ifpub); 19841da177e4SLinus Torvalds } else { 19851da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 19861da177e4SLinus Torvalds } 19871da177e4SLinus Torvalds ipv6_del_addr(ifp); 1988ec8add2aSSabrina Dubroca } else if (ifp->flags&IFA_F_PERMANENT || !dad_failed) { 1989ec8add2aSSabrina Dubroca spin_lock_bh(&ifp->lock); 1990ec8add2aSSabrina Dubroca addrconf_del_dad_work(ifp); 1991ec8add2aSSabrina Dubroca ifp->flags |= IFA_F_TENTATIVE; 1992f1c02cfbSSabrina Dubroca if (dad_failed) 1993f1c02cfbSSabrina Dubroca ifp->flags &= ~IFA_F_OPTIMISTIC; 1994ec8add2aSSabrina Dubroca spin_unlock_bh(&ifp->lock); 1995ec8add2aSSabrina Dubroca if (dad_failed) 1996ec8add2aSSabrina Dubroca ipv6_ifa_notify(0, ifp); 1997ec8add2aSSabrina Dubroca in6_ifa_put(ifp); 1998c15b1ccaSHannes Frederic Sowa } else { 19991da177e4SLinus Torvalds ipv6_del_addr(ifp); 20001da177e4SLinus Torvalds } 2001c15b1ccaSHannes Frederic Sowa } 20021da177e4SLinus Torvalds 2003f2344a13SHerbert Xu static int addrconf_dad_end(struct inet6_ifaddr *ifp) 2004f2344a13SHerbert Xu { 2005f2344a13SHerbert Xu int err = -ENOENT; 2006f2344a13SHerbert Xu 20078e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 2008f2344a13SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DAD) { 2009f2344a13SHerbert Xu ifp->state = INET6_IFADDR_STATE_POSTDAD; 2010f2344a13SHerbert Xu err = 0; 2011f2344a13SHerbert Xu } 20128e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 2013f2344a13SHerbert Xu 2014f2344a13SHerbert Xu return err; 2015f2344a13SHerbert Xu } 2016f2344a13SHerbert Xu 2017da13c59bSVishwanath Pai void addrconf_dad_failure(struct sk_buff *skb, struct inet6_ifaddr *ifp) 20183c21edbdSYOSHIFUJI Hideaki { 20191b34be74SYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev; 20201855b7c3SHannes Frederic Sowa struct net *net = dev_net(ifp->idev->dev); 20219bdd8d40SBrian Haley 2022853dc2e0SUrsula Braun if (addrconf_dad_end(ifp)) { 2023853dc2e0SUrsula Braun in6_ifa_put(ifp); 2024f2344a13SHerbert Xu return; 2025853dc2e0SUrsula Braun } 2026f2344a13SHerbert Xu 2027da13c59bSVishwanath Pai net_info_ratelimited("%s: IPv6 duplicate address %pI6c used by %pM detected!\n", 2028da13c59bSVishwanath Pai ifp->idev->dev->name, &ifp->addr, eth_hdr(skb)->h_source); 20299bdd8d40SBrian Haley 20305f40ef77SHannes Frederic Sowa spin_lock_bh(&ifp->lock); 20311b34be74SYOSHIFUJI Hideaki 20325f40ef77SHannes Frederic Sowa if (ifp->flags & IFA_F_STABLE_PRIVACY) { 20335f40ef77SHannes Frederic Sowa struct in6_addr new_addr; 20345f40ef77SHannes Frederic Sowa struct inet6_ifaddr *ifp2; 20355f40ef77SHannes Frederic Sowa int retries = ifp->stable_privacy_retry + 1; 2036e6464b8cSDavid Ahern struct ifa6_config cfg = { 2037e6464b8cSDavid Ahern .pfx = &new_addr, 2038e6464b8cSDavid Ahern .plen = ifp->prefix_len, 2039e6464b8cSDavid Ahern .ifa_flags = ifp->flags, 2040e6464b8cSDavid Ahern .valid_lft = ifp->valid_lft, 2041e6464b8cSDavid Ahern .preferred_lft = ifp->prefered_lft, 2042e6464b8cSDavid Ahern .scope = ifp->scope, 2043e6464b8cSDavid Ahern }; 20445f40ef77SHannes Frederic Sowa 20451855b7c3SHannes Frederic Sowa if (retries > net->ipv6.sysctl.idgen_retries) { 20465f40ef77SHannes Frederic Sowa net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n", 20475f40ef77SHannes Frederic Sowa ifp->idev->dev->name); 20485f40ef77SHannes Frederic Sowa goto errdad; 20495f40ef77SHannes Frederic Sowa } 20505f40ef77SHannes Frederic Sowa 20515f40ef77SHannes Frederic Sowa new_addr = ifp->addr; 20525f40ef77SHannes Frederic Sowa if (ipv6_generate_stable_address(&new_addr, retries, 20535f40ef77SHannes Frederic Sowa idev)) 20545f40ef77SHannes Frederic Sowa goto errdad; 20555f40ef77SHannes Frederic Sowa 20565f40ef77SHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 20575f40ef77SHannes Frederic Sowa 20585f40ef77SHannes Frederic Sowa if (idev->cnf.max_addresses && 20595f40ef77SHannes Frederic Sowa ipv6_count_addresses(idev) >= 20605f40ef77SHannes Frederic Sowa idev->cnf.max_addresses) 20615f40ef77SHannes Frederic Sowa goto lock_errdad; 20625f40ef77SHannes Frederic Sowa 20635f40ef77SHannes Frederic Sowa net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n", 20645f40ef77SHannes Frederic Sowa ifp->idev->dev->name); 20655f40ef77SHannes Frederic Sowa 2066e6464b8cSDavid Ahern ifp2 = ipv6_add_addr(idev, &cfg, false, NULL); 20675f40ef77SHannes Frederic Sowa if (IS_ERR(ifp2)) 20685f40ef77SHannes Frederic Sowa goto lock_errdad; 20695f40ef77SHannes Frederic Sowa 20705f40ef77SHannes Frederic Sowa spin_lock_bh(&ifp2->lock); 20715f40ef77SHannes Frederic Sowa ifp2->stable_privacy_retry = retries; 20725f40ef77SHannes Frederic Sowa ifp2->state = INET6_IFADDR_STATE_PREDAD; 20735f40ef77SHannes Frederic Sowa spin_unlock_bh(&ifp2->lock); 20745f40ef77SHannes Frederic Sowa 20751855b7c3SHannes Frederic Sowa addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay); 20765f40ef77SHannes Frederic Sowa in6_ifa_put(ifp2); 20775f40ef77SHannes Frederic Sowa lock_errdad: 20785f40ef77SHannes Frederic Sowa spin_lock_bh(&ifp->lock); 20791b34be74SYOSHIFUJI Hideaki } 20801b34be74SYOSHIFUJI Hideaki 20815f40ef77SHannes Frederic Sowa errdad: 2082c15b1ccaSHannes Frederic Sowa /* transition from _POSTDAD to _ERRDAD */ 2083c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_ERRDAD; 20848e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 2085c15b1ccaSHannes Frederic Sowa 2086c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 0); 2087751eb6b6SWei Yongjun in6_ifa_put(ifp); 20883c21edbdSYOSHIFUJI Hideaki } 20891da177e4SLinus Torvalds 2090a9ed4a29SSabrina Dubroca /* Join to solicited addr multicast group. 2091a9ed4a29SSabrina Dubroca * caller must hold RTNL */ 2092b71d1d42SEric Dumazet void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr) 20931da177e4SLinus Torvalds { 20941da177e4SLinus Torvalds struct in6_addr maddr; 20951da177e4SLinus Torvalds 20961da177e4SLinus Torvalds if (dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 20971da177e4SLinus Torvalds return; 20981da177e4SLinus Torvalds 20991da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr); 21001da177e4SLinus Torvalds ipv6_dev_mc_inc(dev, &maddr); 21011da177e4SLinus Torvalds } 21021da177e4SLinus Torvalds 2103a9ed4a29SSabrina Dubroca /* caller must hold RTNL */ 2104b71d1d42SEric Dumazet void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr) 21051da177e4SLinus Torvalds { 21061da177e4SLinus Torvalds struct in6_addr maddr; 21071da177e4SLinus Torvalds 21081da177e4SLinus Torvalds if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 21091da177e4SLinus Torvalds return; 21101da177e4SLinus Torvalds 21111da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr); 21121da177e4SLinus Torvalds __ipv6_dev_mc_dec(idev, &maddr); 21131da177e4SLinus Torvalds } 21141da177e4SLinus Torvalds 2115a9ed4a29SSabrina Dubroca /* caller must hold RTNL */ 211620380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp) 21171da177e4SLinus Torvalds { 21181da177e4SLinus Torvalds struct in6_addr addr; 2119c15b1ccaSHannes Frederic Sowa 212088ad3149SHannes Frederic Sowa if (ifp->prefix_len >= 127) /* RFC 6164 */ 21212bda8a0cSBjørn Mork return; 21221da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 21231da177e4SLinus Torvalds if (ipv6_addr_any(&addr)) 21241da177e4SLinus Torvalds return; 2125013b4d90SWANG Cong __ipv6_dev_ac_inc(ifp->idev, &addr); 21261da177e4SLinus Torvalds } 21271da177e4SLinus Torvalds 2128a9ed4a29SSabrina Dubroca /* caller must hold RTNL */ 212920380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp) 21301da177e4SLinus Torvalds { 21311da177e4SLinus Torvalds struct in6_addr addr; 2132c15b1ccaSHannes Frederic Sowa 213388ad3149SHannes Frederic Sowa if (ifp->prefix_len >= 127) /* RFC 6164 */ 213432019e65SYOSHIFUJI Hideaki return; 21351da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 21361da177e4SLinus Torvalds if (ipv6_addr_any(&addr)) 21371da177e4SLinus Torvalds return; 21381da177e4SLinus Torvalds __ipv6_dev_ac_dec(ifp->idev, &addr); 21391da177e4SLinus Torvalds } 21401da177e4SLinus Torvalds 21418a7a4b47SAlexander Aring static int addrconf_ifid_6lowpan(u8 *eui, struct net_device *dev) 214206a4c1c5Salex.bluesman.smirnov@gmail.com { 21438a7a4b47SAlexander Aring switch (dev->addr_len) { 21448a7a4b47SAlexander Aring case ETH_ALEN: 21459dae2e03SLuiz Augusto von Dentz memcpy(eui, dev->dev_addr, 3); 21469dae2e03SLuiz Augusto von Dentz eui[3] = 0xFF; 21479dae2e03SLuiz Augusto von Dentz eui[4] = 0xFE; 21489dae2e03SLuiz Augusto von Dentz memcpy(eui + 5, dev->dev_addr + 3, 3); 21499dae2e03SLuiz Augusto von Dentz break; 21508a7a4b47SAlexander Aring case EUI64_ADDR_LEN: 21515241c2d7SAlexander Aring memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN); 21525e98a36eSYOSHIFUJI Hideaki / 吉藤英明 eui[0] ^= 2; 21538a7a4b47SAlexander Aring break; 21548a7a4b47SAlexander Aring default: 21558a7a4b47SAlexander Aring return -1; 21568a7a4b47SAlexander Aring } 21578a7a4b47SAlexander Aring 215806a4c1c5Salex.bluesman.smirnov@gmail.com return 0; 215906a4c1c5Salex.bluesman.smirnov@gmail.com } 216006a4c1c5Salex.bluesman.smirnov@gmail.com 2161cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev) 2162cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 { 2163cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 union fwnet_hwaddr *ha; 2164cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 2165cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 if (dev->addr_len != FWNET_ALEN) 2166cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return -1; 2167cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 2168cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 ha = (union fwnet_hwaddr *)dev->dev_addr; 2169cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 2170cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id)); 2171cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 eui[0] ^= 2; 2172cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return 0; 2173cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 } 2174cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 2175073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev) 2176073a8e0eSYOSHIFUJI Hideaki { 21771da177e4SLinus Torvalds /* XXX: inherit EUI-64 from other interface -- yoshfuji */ 21781da177e4SLinus Torvalds if (dev->addr_len != ARCNET_ALEN) 21791da177e4SLinus Torvalds return -1; 21801da177e4SLinus Torvalds memset(eui, 0, 7); 21811da177e4SLinus Torvalds eui[7] = *(u8 *)dev->dev_addr; 21821da177e4SLinus Torvalds return 0; 2183073a8e0eSYOSHIFUJI Hideaki } 2184073a8e0eSYOSHIFUJI Hideaki 2185073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev) 2186073a8e0eSYOSHIFUJI Hideaki { 21871da177e4SLinus Torvalds if (dev->addr_len != INFINIBAND_ALEN) 21881da177e4SLinus Torvalds return -1; 21891da177e4SLinus Torvalds memcpy(eui, dev->dev_addr + 12, 8); 21901da177e4SLinus Torvalds eui[0] |= 2; 21911da177e4SLinus Torvalds return 0; 21921da177e4SLinus Torvalds } 2193073a8e0eSYOSHIFUJI Hideaki 2194c61393eaSstephen hemminger static int __ipv6_isatap_ifid(u8 *eui, __be32 addr) 2195dfd982baSYOSHIFUJI Hideaki { 21969af28511SSascha Hlusiak if (addr == 0) 21979af28511SSascha Hlusiak return -1; 2198dfd982baSYOSHIFUJI Hideaki eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) || 2199dfd982baSYOSHIFUJI Hideaki ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) || 2200dfd982baSYOSHIFUJI Hideaki ipv4_is_private_172(addr) || ipv4_is_test_192(addr) || 2201dfd982baSYOSHIFUJI Hideaki ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) || 2202dfd982baSYOSHIFUJI Hideaki ipv4_is_test_198(addr) || ipv4_is_multicast(addr) || 2203dfd982baSYOSHIFUJI Hideaki ipv4_is_lbcast(addr)) ? 0x00 : 0x02; 2204dfd982baSYOSHIFUJI Hideaki eui[1] = 0; 2205dfd982baSYOSHIFUJI Hideaki eui[2] = 0x5E; 2206dfd982baSYOSHIFUJI Hideaki eui[3] = 0xFE; 2207dfd982baSYOSHIFUJI Hideaki memcpy(eui + 4, &addr, 4); 2208dfd982baSYOSHIFUJI Hideaki return 0; 2209dfd982baSYOSHIFUJI Hideaki } 2210dfd982baSYOSHIFUJI Hideaki 2211dfd982baSYOSHIFUJI Hideaki static int addrconf_ifid_sit(u8 *eui, struct net_device *dev) 2212dfd982baSYOSHIFUJI Hideaki { 2213dfd982baSYOSHIFUJI Hideaki if (dev->priv_flags & IFF_ISATAP) 2214dfd982baSYOSHIFUJI Hideaki return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); 2215dfd982baSYOSHIFUJI Hideaki return -1; 2216dfd982baSYOSHIFUJI Hideaki } 2217dfd982baSYOSHIFUJI Hideaki 2218aee80b54Sstephen hemminger static int addrconf_ifid_gre(u8 *eui, struct net_device *dev) 2219aee80b54Sstephen hemminger { 2220aee80b54Sstephen hemminger return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); 2221aee80b54Sstephen hemminger } 2222aee80b54Sstephen hemminger 2223e837735eSNicolas Dichtel static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev) 2224e837735eSNicolas Dichtel { 2225e837735eSNicolas Dichtel memcpy(eui, dev->perm_addr, 3); 2226e837735eSNicolas Dichtel memcpy(eui + 5, dev->perm_addr + 3, 3); 2227e837735eSNicolas Dichtel eui[3] = 0xFF; 2228e837735eSNicolas Dichtel eui[4] = 0xFE; 2229e837735eSNicolas Dichtel eui[0] ^= 2; 2230e837735eSNicolas Dichtel return 0; 2231e837735eSNicolas Dichtel } 2232e837735eSNicolas Dichtel 2233073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev) 2234073a8e0eSYOSHIFUJI Hideaki { 2235073a8e0eSYOSHIFUJI Hideaki switch (dev->type) { 2236073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ETHER: 2237073a8e0eSYOSHIFUJI Hideaki case ARPHRD_FDDI: 2238073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_eui48(eui, dev); 2239073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ARCNET: 2240073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_arcnet(eui, dev); 2241073a8e0eSYOSHIFUJI Hideaki case ARPHRD_INFINIBAND: 2242073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_infiniband(eui, dev); 2243c7dc89c0SFred L. Templin case ARPHRD_SIT: 2244dfd982baSYOSHIFUJI Hideaki return addrconf_ifid_sit(eui, dev); 2245aee80b54Sstephen hemminger case ARPHRD_IPGRE: 224645ce0fd1SFelix Jia case ARPHRD_TUNNEL: 2247aee80b54Sstephen hemminger return addrconf_ifid_gre(eui, dev); 2248e74bccb8SJukka Rissanen case ARPHRD_6LOWPAN: 22498a7a4b47SAlexander Aring return addrconf_ifid_6lowpan(eui, dev); 2250cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 case ARPHRD_IEEE1394: 2251cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return addrconf_ifid_ieee1394(eui, dev); 2252e837735eSNicolas Dichtel case ARPHRD_TUNNEL6: 225345ce0fd1SFelix Jia case ARPHRD_IP6GRE: 2254*9deb441cSSubash Abhinov Kasiviswanathan case ARPHRD_RAWIP: 2255e837735eSNicolas Dichtel return addrconf_ifid_ip6tnl(eui, dev); 2256073a8e0eSYOSHIFUJI Hideaki } 22571da177e4SLinus Torvalds return -1; 22581da177e4SLinus Torvalds } 22591da177e4SLinus Torvalds 22601da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev) 22611da177e4SLinus Torvalds { 22621da177e4SLinus Torvalds int err = -1; 22631da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 22641da177e4SLinus Torvalds 22651da177e4SLinus Torvalds read_lock_bh(&idev->lock); 2266602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) { 2267602582caSHannes Frederic Sowa if (ifp->scope > IFA_LINK) 2268602582caSHannes Frederic Sowa break; 22691da177e4SLinus Torvalds if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) { 22701da177e4SLinus Torvalds memcpy(eui, ifp->addr.s6_addr+8, 8); 22711da177e4SLinus Torvalds err = 0; 22721da177e4SLinus Torvalds break; 22731da177e4SLinus Torvalds } 22741da177e4SLinus Torvalds } 22751da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 22761da177e4SLinus Torvalds return err; 22771da177e4SLinus Torvalds } 22781da177e4SLinus Torvalds 22791da177e4SLinus Torvalds /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */ 22809d6280daSJiri Bohac static void ipv6_regen_rndid(struct inet6_dev *idev) 22811da177e4SLinus Torvalds { 22821da177e4SLinus Torvalds regen: 2283955189efSYOSHIFUJI Hideaki get_random_bytes(idev->rndid, sizeof(idev->rndid)); 22841da177e4SLinus Torvalds idev->rndid[0] &= ~0x02; 22851da177e4SLinus Torvalds 22861da177e4SLinus Torvalds /* 22871da177e4SLinus Torvalds * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>: 22881da177e4SLinus Torvalds * check if generated address is not inappropriate 22891da177e4SLinus Torvalds * 22901da177e4SLinus Torvalds * - Reserved subnet anycast (RFC 2526) 22911da177e4SLinus Torvalds * 11111101 11....11 1xxxxxxx 2292c7dc89c0SFred L. Templin * - ISATAP (RFC4214) 6.1 22931da177e4SLinus Torvalds * 00-00-5E-FE-xx-xx-xx-xx 22941da177e4SLinus Torvalds * - value 0 22951da177e4SLinus Torvalds * - XXX: already assigned to an address on the device 22961da177e4SLinus Torvalds */ 22971da177e4SLinus Torvalds if (idev->rndid[0] == 0xfd && 22981da177e4SLinus Torvalds (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff && 22991da177e4SLinus Torvalds (idev->rndid[7]&0x80)) 23001da177e4SLinus Torvalds goto regen; 23011da177e4SLinus Torvalds if ((idev->rndid[0]|idev->rndid[1]) == 0) { 23021da177e4SLinus Torvalds if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe) 23031da177e4SLinus Torvalds goto regen; 23041da177e4SLinus Torvalds if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00) 23051da177e4SLinus Torvalds goto regen; 23061da177e4SLinus Torvalds } 23071da177e4SLinus Torvalds } 23081da177e4SLinus Torvalds 23099d6280daSJiri Bohac static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) 23108e5e8f30SEldad Zack { 23111da177e4SLinus Torvalds if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0) 23129d6280daSJiri Bohac ipv6_regen_rndid(idev); 23131da177e4SLinus Torvalds } 23141da177e4SLinus Torvalds 23151da177e4SLinus Torvalds /* 23161da177e4SLinus Torvalds * Add prefix route. 23171da177e4SLinus Torvalds */ 23181da177e4SLinus Torvalds 23191da177e4SLinus Torvalds static void 23208308f3ffSDavid Ahern addrconf_prefix_route(struct in6_addr *pfx, int plen, u32 metric, 23218308f3ffSDavid Ahern struct net_device *dev, unsigned long expires, 23228308f3ffSDavid Ahern u32 flags, gfp_t gfp_flags) 23231da177e4SLinus Torvalds { 232486872cb5SThomas Graf struct fib6_config cfg = { 2325ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX, 23268308f3ffSDavid Ahern .fc_metric = metric ? : IP6_RT_PRIO_ADDRCONF, 232786872cb5SThomas Graf .fc_ifindex = dev->ifindex, 232886872cb5SThomas Graf .fc_expires = expires, 232986872cb5SThomas Graf .fc_dst_len = plen, 233086872cb5SThomas Graf .fc_flags = RTF_UP | flags, 2331c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 2332f410a1fbSStephen Hemminger .fc_protocol = RTPROT_KERNEL, 2333e8478e80SDavid Ahern .fc_type = RTN_UNICAST, 233486872cb5SThomas Graf }; 23351da177e4SLinus Torvalds 23364e3fd7a0SAlexey Dobriyan cfg.fc_dst = *pfx; 23371da177e4SLinus Torvalds 23381da177e4SLinus Torvalds /* Prevent useless cloning on PtP SIT. 23391da177e4SLinus Torvalds This thing is done here expecting that the whole 23401da177e4SLinus Torvalds class of non-broadcast devices need not cloning. 23411da177e4SLinus Torvalds */ 234207a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 23431da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT)) 234486872cb5SThomas Graf cfg.fc_flags |= RTF_NONEXTHOP; 23450be669bbSJoerg Roedel #endif 23461da177e4SLinus Torvalds 2347acb54e3cSDavid Ahern ip6_route_add(&cfg, gfp_flags, NULL); 23481da177e4SLinus Torvalds } 23491da177e4SLinus Torvalds 235014ef37b6SAndreas Hofmeister 23518d1c802bSDavid Ahern static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx, 235214ef37b6SAndreas Hofmeister int plen, 235314ef37b6SAndreas Hofmeister const struct net_device *dev, 235414ef37b6SAndreas Hofmeister u32 flags, u32 noflags) 235514ef37b6SAndreas Hofmeister { 235614ef37b6SAndreas Hofmeister struct fib6_node *fn; 23578d1c802bSDavid Ahern struct fib6_info *rt = NULL; 235814ef37b6SAndreas Hofmeister struct fib6_table *table; 2359ca254490SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX; 236014ef37b6SAndreas Hofmeister 2361ca254490SDavid Ahern table = fib6_get_table(dev_net(dev), tb_id); 236263159f29SIan Morris if (!table) 236314ef37b6SAndreas Hofmeister return NULL; 236414ef37b6SAndreas Hofmeister 236566f5d6ceSWei Wang rcu_read_lock(); 236638fbeeeeSWei Wang fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0, true); 236714ef37b6SAndreas Hofmeister if (!fn) 236814ef37b6SAndreas Hofmeister goto out; 23691f56a01fSMartin KaFai Lau 237066f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 23715e670d84SDavid Ahern if (rt->fib6_nh.nh_dev->ifindex != dev->ifindex) 237214ef37b6SAndreas Hofmeister continue; 237393c2fb25SDavid Ahern if ((rt->fib6_flags & flags) != flags) 237414ef37b6SAndreas Hofmeister continue; 237593c2fb25SDavid Ahern if ((rt->fib6_flags & noflags) != 0) 237614ef37b6SAndreas Hofmeister continue; 237793531c67SDavid Ahern fib6_info_hold(rt); 237814ef37b6SAndreas Hofmeister break; 237914ef37b6SAndreas Hofmeister } 238014ef37b6SAndreas Hofmeister out: 238166f5d6ceSWei Wang rcu_read_unlock(); 238214ef37b6SAndreas Hofmeister return rt; 238314ef37b6SAndreas Hofmeister } 238414ef37b6SAndreas Hofmeister 238514ef37b6SAndreas Hofmeister 23861da177e4SLinus Torvalds /* Create "default" multicast route to the interface */ 23871da177e4SLinus Torvalds 23881da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev) 23891da177e4SLinus Torvalds { 239086872cb5SThomas Graf struct fib6_config cfg = { 2391ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL, 239286872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 239386872cb5SThomas Graf .fc_ifindex = dev->ifindex, 239486872cb5SThomas Graf .fc_dst_len = 8, 239586872cb5SThomas Graf .fc_flags = RTF_UP, 2396e8478e80SDavid Ahern .fc_type = RTN_UNICAST, 2397c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 239886872cb5SThomas Graf }; 23991da177e4SLinus Torvalds 240086872cb5SThomas Graf ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0); 240186872cb5SThomas Graf 2402acb54e3cSDavid Ahern ip6_route_add(&cfg, GFP_ATOMIC, NULL); 24031da177e4SLinus Torvalds } 24041da177e4SLinus Torvalds 24051da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev) 24061da177e4SLinus Torvalds { 24071da177e4SLinus Torvalds struct inet6_dev *idev; 24081da177e4SLinus Torvalds 24091da177e4SLinus Torvalds ASSERT_RTNL(); 24101da177e4SLinus Torvalds 2411e21e8467SStephen Hemminger idev = ipv6_find_idev(dev); 2412e21e8467SStephen Hemminger if (!idev) 241364e724f6SBrian Haley return ERR_PTR(-ENOBUFS); 241464e724f6SBrian Haley 241564e724f6SBrian Haley if (idev->cnf.disable_ipv6) 241664e724f6SBrian Haley return ERR_PTR(-EACCES); 24171da177e4SLinus Torvalds 24181da177e4SLinus Torvalds /* Add default multicast route */ 2419ba46ee4cSDavid Ahern if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev)) 24201da177e4SLinus Torvalds addrconf_add_mroute(dev); 24211da177e4SLinus Torvalds 24221da177e4SLinus Torvalds return idev; 24231da177e4SLinus Torvalds } 24241da177e4SLinus Torvalds 242553bd6749SJiri Pirko static void manage_tempaddrs(struct inet6_dev *idev, 242653bd6749SJiri Pirko struct inet6_ifaddr *ifp, 242753bd6749SJiri Pirko __u32 valid_lft, __u32 prefered_lft, 242853bd6749SJiri Pirko bool create, unsigned long now) 242953bd6749SJiri Pirko { 243053bd6749SJiri Pirko u32 flags; 243153bd6749SJiri Pirko struct inet6_ifaddr *ift; 243253bd6749SJiri Pirko 243353bd6749SJiri Pirko read_lock_bh(&idev->lock); 243453bd6749SJiri Pirko /* update all temporary addresses in the list */ 243553bd6749SJiri Pirko list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) { 243653bd6749SJiri Pirko int age, max_valid, max_prefered; 243753bd6749SJiri Pirko 243853bd6749SJiri Pirko if (ifp != ift->ifpub) 243953bd6749SJiri Pirko continue; 244053bd6749SJiri Pirko 244153bd6749SJiri Pirko /* RFC 4941 section 3.3: 244253bd6749SJiri Pirko * If a received option will extend the lifetime of a public 244353bd6749SJiri Pirko * address, the lifetimes of temporary addresses should 244453bd6749SJiri Pirko * be extended, subject to the overall constraint that no 244553bd6749SJiri Pirko * temporary addresses should ever remain "valid" or "preferred" 244653bd6749SJiri Pirko * for a time longer than (TEMP_VALID_LIFETIME) or 244753bd6749SJiri Pirko * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively. 244853bd6749SJiri Pirko */ 244953bd6749SJiri Pirko age = (now - ift->cstamp) / HZ; 245053bd6749SJiri Pirko max_valid = idev->cnf.temp_valid_lft - age; 245153bd6749SJiri Pirko if (max_valid < 0) 245253bd6749SJiri Pirko max_valid = 0; 245353bd6749SJiri Pirko 245453bd6749SJiri Pirko max_prefered = idev->cnf.temp_prefered_lft - 245576506a98SJiri Bohac idev->desync_factor - age; 245653bd6749SJiri Pirko if (max_prefered < 0) 245753bd6749SJiri Pirko max_prefered = 0; 245853bd6749SJiri Pirko 245953bd6749SJiri Pirko if (valid_lft > max_valid) 246053bd6749SJiri Pirko valid_lft = max_valid; 246153bd6749SJiri Pirko 246253bd6749SJiri Pirko if (prefered_lft > max_prefered) 246353bd6749SJiri Pirko prefered_lft = max_prefered; 246453bd6749SJiri Pirko 246553bd6749SJiri Pirko spin_lock(&ift->lock); 246653bd6749SJiri Pirko flags = ift->flags; 246753bd6749SJiri Pirko ift->valid_lft = valid_lft; 246853bd6749SJiri Pirko ift->prefered_lft = prefered_lft; 246953bd6749SJiri Pirko ift->tstamp = now; 247053bd6749SJiri Pirko if (prefered_lft > 0) 247153bd6749SJiri Pirko ift->flags &= ~IFA_F_DEPRECATED; 247253bd6749SJiri Pirko 247353bd6749SJiri Pirko spin_unlock(&ift->lock); 247453bd6749SJiri Pirko if (!(flags&IFA_F_TENTATIVE)) 247553bd6749SJiri Pirko ipv6_ifa_notify(0, ift); 247653bd6749SJiri Pirko } 247753bd6749SJiri Pirko 247853bd6749SJiri Pirko if ((create || list_empty(&idev->tempaddr_list)) && 247953bd6749SJiri Pirko idev->cnf.use_tempaddr > 0) { 248053bd6749SJiri Pirko /* When a new public address is created as described 248153bd6749SJiri Pirko * in [ADDRCONF], also create a new temporary address. 248253bd6749SJiri Pirko * Also create a temporary address if it's enabled but 248353bd6749SJiri Pirko * no temporary address currently exists. 248453bd6749SJiri Pirko */ 248553bd6749SJiri Pirko read_unlock_bh(&idev->lock); 2486fffcefe9SEric Dumazet ipv6_create_tempaddr(ifp, NULL, false); 248753bd6749SJiri Pirko } else { 248853bd6749SJiri Pirko read_unlock_bh(&idev->lock); 248953bd6749SJiri Pirko } 249053bd6749SJiri Pirko } 249153bd6749SJiri Pirko 2492cc9da6ccSBjørn Mork static bool is_addr_mode_generate_stable(struct inet6_dev *idev) 2493cc9da6ccSBjørn Mork { 2494d35a00b8SFelix Jia return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY || 2495d35a00b8SFelix Jia idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM; 2496cc9da6ccSBjørn Mork } 2497cc9da6ccSBjørn Mork 2498cc84b3c6SAlexander Aring int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev, 24994f672235SAlexander Aring const struct prefix_info *pinfo, 25004f672235SAlexander Aring struct inet6_dev *in6_dev, 2501cc84b3c6SAlexander Aring const struct in6_addr *addr, int addr_type, 2502cc84b3c6SAlexander Aring u32 addr_flags, bool sllao, bool tokenized, 25034f672235SAlexander Aring __u32 valid_lft, u32 prefered_lft) 25044f672235SAlexander Aring { 25054f672235SAlexander Aring struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1); 25064f672235SAlexander Aring int create = 0, update_lft = 0; 25074f672235SAlexander Aring 25084f672235SAlexander Aring if (!ifp && valid_lft) { 25094f672235SAlexander Aring int max_addresses = in6_dev->cnf.max_addresses; 2510e6464b8cSDavid Ahern struct ifa6_config cfg = { 2511e6464b8cSDavid Ahern .pfx = addr, 2512e6464b8cSDavid Ahern .plen = pinfo->prefix_len, 2513e6464b8cSDavid Ahern .ifa_flags = addr_flags, 2514e6464b8cSDavid Ahern .valid_lft = valid_lft, 2515e6464b8cSDavid Ahern .preferred_lft = prefered_lft, 2516e6464b8cSDavid Ahern .scope = addr_type & IPV6_ADDR_SCOPE_MASK, 2517e6464b8cSDavid Ahern }; 25184f672235SAlexander Aring 25194f672235SAlexander Aring #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 252035e015e1SMatteo Croce if ((net->ipv6.devconf_all->optimistic_dad || 252135e015e1SMatteo Croce in6_dev->cnf.optimistic_dad) && 25224f672235SAlexander Aring !net->ipv6.devconf_all->forwarding && sllao) 2523e6464b8cSDavid Ahern cfg.ifa_flags |= IFA_F_OPTIMISTIC; 25244f672235SAlexander Aring #endif 25254f672235SAlexander Aring 25264f672235SAlexander Aring /* Do not allow to create too much of autoconfigured 25274f672235SAlexander Aring * addresses; this would be too easy way to crash kernel. 25284f672235SAlexander Aring */ 25294f672235SAlexander Aring if (!max_addresses || 25304f672235SAlexander Aring ipv6_count_addresses(in6_dev) < max_addresses) 2531e6464b8cSDavid Ahern ifp = ipv6_add_addr(in6_dev, &cfg, false, NULL); 25324f672235SAlexander Aring 25334f672235SAlexander Aring if (IS_ERR_OR_NULL(ifp)) 25344f672235SAlexander Aring return -1; 25354f672235SAlexander Aring 25364f672235SAlexander Aring create = 1; 25374f672235SAlexander Aring spin_lock_bh(&ifp->lock); 25384f672235SAlexander Aring ifp->flags |= IFA_F_MANAGETEMPADDR; 25394f672235SAlexander Aring ifp->cstamp = jiffies; 25404f672235SAlexander Aring ifp->tokenized = tokenized; 25414f672235SAlexander Aring spin_unlock_bh(&ifp->lock); 25424f672235SAlexander Aring addrconf_dad_start(ifp); 25434f672235SAlexander Aring } 25444f672235SAlexander Aring 25454f672235SAlexander Aring if (ifp) { 25464f672235SAlexander Aring u32 flags; 25474f672235SAlexander Aring unsigned long now; 25484f672235SAlexander Aring u32 stored_lft; 25494f672235SAlexander Aring 25504f672235SAlexander Aring /* update lifetime (RFC2462 5.5.3 e) */ 25514f672235SAlexander Aring spin_lock_bh(&ifp->lock); 25524f672235SAlexander Aring now = jiffies; 25534f672235SAlexander Aring if (ifp->valid_lft > (now - ifp->tstamp) / HZ) 25544f672235SAlexander Aring stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ; 25554f672235SAlexander Aring else 25564f672235SAlexander Aring stored_lft = 0; 2557f85f94b8SLorenzo Bianconi if (!create && stored_lft) { 25584f672235SAlexander Aring const u32 minimum_lft = min_t(u32, 25594f672235SAlexander Aring stored_lft, MIN_VALID_LIFETIME); 25604f672235SAlexander Aring valid_lft = max(valid_lft, minimum_lft); 25614f672235SAlexander Aring 25624f672235SAlexander Aring /* RFC4862 Section 5.5.3e: 25634f672235SAlexander Aring * "Note that the preferred lifetime of the 25644f672235SAlexander Aring * corresponding address is always reset to 25654f672235SAlexander Aring * the Preferred Lifetime in the received 25664f672235SAlexander Aring * Prefix Information option, regardless of 25674f672235SAlexander Aring * whether the valid lifetime is also reset or 25684f672235SAlexander Aring * ignored." 25694f672235SAlexander Aring * 25704f672235SAlexander Aring * So we should always update prefered_lft here. 25714f672235SAlexander Aring */ 25724f672235SAlexander Aring update_lft = 1; 25734f672235SAlexander Aring } 25744f672235SAlexander Aring 25754f672235SAlexander Aring if (update_lft) { 25764f672235SAlexander Aring ifp->valid_lft = valid_lft; 25774f672235SAlexander Aring ifp->prefered_lft = prefered_lft; 25784f672235SAlexander Aring ifp->tstamp = now; 25794f672235SAlexander Aring flags = ifp->flags; 25804f672235SAlexander Aring ifp->flags &= ~IFA_F_DEPRECATED; 25814f672235SAlexander Aring spin_unlock_bh(&ifp->lock); 25824f672235SAlexander Aring 25834f672235SAlexander Aring if (!(flags&IFA_F_TENTATIVE)) 25844f672235SAlexander Aring ipv6_ifa_notify(0, ifp); 25854f672235SAlexander Aring } else 25864f672235SAlexander Aring spin_unlock_bh(&ifp->lock); 25874f672235SAlexander Aring 25884f672235SAlexander Aring manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft, 25894f672235SAlexander Aring create, now); 25904f672235SAlexander Aring 25914f672235SAlexander Aring in6_ifa_put(ifp); 25924f672235SAlexander Aring addrconf_verify(); 25934f672235SAlexander Aring } 25944f672235SAlexander Aring 25954f672235SAlexander Aring return 0; 25964f672235SAlexander Aring } 2597cc84b3c6SAlexander Aring EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr); 25984f672235SAlexander Aring 2599e6bff995SNeil Horman void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao) 26001da177e4SLinus Torvalds { 26011da177e4SLinus Torvalds struct prefix_info *pinfo; 26021da177e4SLinus Torvalds __u32 valid_lft; 26031da177e4SLinus Torvalds __u32 prefered_lft; 26044f672235SAlexander Aring int addr_type, err; 260564236f3fSHannes Frederic Sowa u32 addr_flags = 0; 26061da177e4SLinus Torvalds struct inet6_dev *in6_dev; 260756d417b1SBrian Haley struct net *net = dev_net(dev); 26081da177e4SLinus Torvalds 26091da177e4SLinus Torvalds pinfo = (struct prefix_info *) opt; 26101da177e4SLinus Torvalds 26111da177e4SLinus Torvalds if (len < sizeof(struct prefix_info)) { 2612e32ac250SJoe Perches netdev_dbg(dev, "addrconf: prefix option too short\n"); 26131da177e4SLinus Torvalds return; 26141da177e4SLinus Torvalds } 26151da177e4SLinus Torvalds 26161da177e4SLinus Torvalds /* 26171da177e4SLinus Torvalds * Validation checks ([ADDRCONF], page 19) 26181da177e4SLinus Torvalds */ 26191da177e4SLinus Torvalds 26201da177e4SLinus Torvalds addr_type = ipv6_addr_type(&pinfo->prefix); 26211da177e4SLinus Torvalds 26221da177e4SLinus Torvalds if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)) 26231da177e4SLinus Torvalds return; 26241da177e4SLinus Torvalds 26251da177e4SLinus Torvalds valid_lft = ntohl(pinfo->valid); 26261da177e4SLinus Torvalds prefered_lft = ntohl(pinfo->prefered); 26271da177e4SLinus Torvalds 26281da177e4SLinus Torvalds if (prefered_lft > valid_lft) { 2629e87cc472SJoe Perches net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n"); 26301da177e4SLinus Torvalds return; 26311da177e4SLinus Torvalds } 26321da177e4SLinus Torvalds 26331da177e4SLinus Torvalds in6_dev = in6_dev_get(dev); 26341da177e4SLinus Torvalds 263563159f29SIan Morris if (!in6_dev) { 2636e87cc472SJoe Perches net_dbg_ratelimited("addrconf: device %s not configured\n", 2637e87cc472SJoe Perches dev->name); 26381da177e4SLinus Torvalds return; 26391da177e4SLinus Torvalds } 26401da177e4SLinus Torvalds 26411da177e4SLinus Torvalds /* 26421da177e4SLinus Torvalds * Two things going on here: 26431da177e4SLinus Torvalds * 1) Add routes for on-link prefixes 26441da177e4SLinus Torvalds * 2) Configure prefixes with the auto flag set 26451da177e4SLinus Torvalds */ 26461da177e4SLinus Torvalds 26474bed72e4SYOSHIFUJI Hideaki if (pinfo->onlink) { 26488d1c802bSDavid Ahern struct fib6_info *rt; 26494bed72e4SYOSHIFUJI Hideaki unsigned long rt_expires; 26504bed72e4SYOSHIFUJI Hideaki 26511da177e4SLinus Torvalds /* Avoid arithmetic overflow. Really, we could 26526f704992SYOSHIFUJI Hideaki * save rt_expires in seconds, likely valid_lft, 26536f704992SYOSHIFUJI Hideaki * but it would require division in fib gc, that it 26546f704992SYOSHIFUJI Hideaki * not good. 26551da177e4SLinus Torvalds */ 26564bed72e4SYOSHIFUJI Hideaki if (HZ > USER_HZ) 26574bed72e4SYOSHIFUJI Hideaki rt_expires = addrconf_timeout_fixup(valid_lft, HZ); 26584bed72e4SYOSHIFUJI Hideaki else 26594bed72e4SYOSHIFUJI Hideaki rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ); 26603dd4bc68SYOSHIFUJI Hideaki 26614bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(rt_expires)) 26624bed72e4SYOSHIFUJI Hideaki rt_expires *= HZ; 26631da177e4SLinus Torvalds 266414ef37b6SAndreas Hofmeister rt = addrconf_get_prefix_route(&pinfo->prefix, 266514ef37b6SAndreas Hofmeister pinfo->prefix_len, 266614ef37b6SAndreas Hofmeister dev, 266714ef37b6SAndreas Hofmeister RTF_ADDRCONF | RTF_PREFIX_RT, 266814ef37b6SAndreas Hofmeister RTF_GATEWAY | RTF_DEFAULT); 26691da177e4SLinus Torvalds 267014ef37b6SAndreas Hofmeister if (rt) { 26716f704992SYOSHIFUJI Hideaki /* Autoconf prefix route */ 26721da177e4SLinus Torvalds if (valid_lft == 0) { 2673afb1d4b5SDavid Ahern ip6_del_rt(net, rt); 26741da177e4SLinus Torvalds rt = NULL; 26754bed72e4SYOSHIFUJI Hideaki } else if (addrconf_finite_timeout(rt_expires)) { 26766f704992SYOSHIFUJI Hideaki /* not infinity */ 267714895687SDavid Ahern fib6_set_expires(rt, jiffies + rt_expires); 26786f704992SYOSHIFUJI Hideaki } else { 267914895687SDavid Ahern fib6_clean_expires(rt); 26801da177e4SLinus Torvalds } 26811da177e4SLinus Torvalds } else if (valid_lft) { 26826f704992SYOSHIFUJI Hideaki clock_t expires = 0; 26834bed72e4SYOSHIFUJI Hideaki int flags = RTF_ADDRCONF | RTF_PREFIX_RT; 26844bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(rt_expires)) { 26856f704992SYOSHIFUJI Hideaki /* not infinity */ 26866f704992SYOSHIFUJI Hideaki flags |= RTF_EXPIRES; 26876f704992SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(rt_expires); 26886f704992SYOSHIFUJI Hideaki } 26891da177e4SLinus Torvalds addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len, 26908308f3ffSDavid Ahern 0, dev, expires, flags, 26918308f3ffSDavid Ahern GFP_ATOMIC); 26921da177e4SLinus Torvalds } 269393531c67SDavid Ahern fib6_info_release(rt); 26941da177e4SLinus Torvalds } 26951da177e4SLinus Torvalds 26961da177e4SLinus Torvalds /* Try to figure out our local address for this prefix */ 26971da177e4SLinus Torvalds 26981da177e4SLinus Torvalds if (pinfo->autoconf && in6_dev->cnf.autoconf) { 26991da177e4SLinus Torvalds struct in6_addr addr; 2700f997c55cSAlexander Aring bool tokenized = false, dev_addr_generated = false; 27011da177e4SLinus Torvalds 27021da177e4SLinus Torvalds if (pinfo->prefix_len == 64) { 27031da177e4SLinus Torvalds memcpy(&addr, &pinfo->prefix, 8); 2704f53adae4SDaniel Borkmann 2705f53adae4SDaniel Borkmann if (!ipv6_addr_any(&in6_dev->token)) { 2706f53adae4SDaniel Borkmann read_lock_bh(&in6_dev->lock); 2707f53adae4SDaniel Borkmann memcpy(addr.s6_addr + 8, 2708f53adae4SDaniel Borkmann in6_dev->token.s6_addr + 8, 8); 2709f53adae4SDaniel Borkmann read_unlock_bh(&in6_dev->lock); 2710617fe29dSDaniel Borkmann tokenized = true; 2711cc9da6ccSBjørn Mork } else if (is_addr_mode_generate_stable(in6_dev) && 2712622c81d5SHannes Frederic Sowa !ipv6_generate_stable_address(&addr, 0, 2713622c81d5SHannes Frederic Sowa in6_dev)) { 271464236f3fSHannes Frederic Sowa addr_flags |= IFA_F_STABLE_PRIVACY; 2715622c81d5SHannes Frederic Sowa goto ok; 2716f53adae4SDaniel Borkmann } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) && 27171da177e4SLinus Torvalds ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) { 27184f672235SAlexander Aring goto put; 2719f997c55cSAlexander Aring } else { 2720f997c55cSAlexander Aring dev_addr_generated = true; 27211da177e4SLinus Torvalds } 27221da177e4SLinus Torvalds goto ok; 27231da177e4SLinus Torvalds } 2724e87cc472SJoe Perches net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n", 27251da177e4SLinus Torvalds pinfo->prefix_len); 27264f672235SAlexander Aring goto put; 27271da177e4SLinus Torvalds 27281da177e4SLinus Torvalds ok: 27294f672235SAlexander Aring err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, 27304f672235SAlexander Aring &addr, addr_type, 27314f672235SAlexander Aring addr_flags, sllao, 27324f672235SAlexander Aring tokenized, valid_lft, 27338a226b2cSJiri Benc prefered_lft); 27344f672235SAlexander Aring if (err) 27354f672235SAlexander Aring goto put; 2736f997c55cSAlexander Aring 2737f997c55cSAlexander Aring /* Ignore error case here because previous prefix add addr was 2738f997c55cSAlexander Aring * successful which will be notified. 2739f997c55cSAlexander Aring */ 2740f997c55cSAlexander Aring ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr, 2741f997c55cSAlexander Aring addr_type, addr_flags, sllao, 2742f997c55cSAlexander Aring tokenized, valid_lft, 2743f997c55cSAlexander Aring prefered_lft, 2744f997c55cSAlexander Aring dev_addr_generated); 27451da177e4SLinus Torvalds } 27461da177e4SLinus Torvalds inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo); 27474f672235SAlexander Aring put: 27481da177e4SLinus Torvalds in6_dev_put(in6_dev); 27491da177e4SLinus Torvalds } 27501da177e4SLinus Torvalds 27511da177e4SLinus Torvalds /* 27521da177e4SLinus Torvalds * Set destination address. 27531da177e4SLinus Torvalds * Special case for SIT interfaces where we create a new "virtual" 27541da177e4SLinus Torvalds * device. 27551da177e4SLinus Torvalds */ 2756af284937SDaniel Lezcano int addrconf_set_dstaddr(struct net *net, void __user *arg) 27571da177e4SLinus Torvalds { 27581da177e4SLinus Torvalds struct in6_ifreq ireq; 27591da177e4SLinus Torvalds struct net_device *dev; 27601da177e4SLinus Torvalds int err = -EINVAL; 27611da177e4SLinus Torvalds 27621da177e4SLinus Torvalds rtnl_lock(); 27631da177e4SLinus Torvalds 27641da177e4SLinus Torvalds err = -EFAULT; 27651da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 27661da177e4SLinus Torvalds goto err_exit; 27671da177e4SLinus Torvalds 2768af284937SDaniel Lezcano dev = __dev_get_by_index(net, ireq.ifr6_ifindex); 27691da177e4SLinus Torvalds 27701da177e4SLinus Torvalds err = -ENODEV; 277163159f29SIan Morris if (!dev) 27721da177e4SLinus Torvalds goto err_exit; 27731da177e4SLinus Torvalds 277407a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 27751da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT) { 27765bc3eb7eSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 27771da177e4SLinus Torvalds struct ifreq ifr; 27781da177e4SLinus Torvalds struct ip_tunnel_parm p; 27791da177e4SLinus Torvalds 27801da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 27811da177e4SLinus Torvalds if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4)) 27821da177e4SLinus Torvalds goto err_exit; 27831da177e4SLinus Torvalds 27841da177e4SLinus Torvalds memset(&p, 0, sizeof(p)); 27851da177e4SLinus Torvalds p.iph.daddr = ireq.ifr6_addr.s6_addr32[3]; 27861da177e4SLinus Torvalds p.iph.saddr = 0; 27871da177e4SLinus Torvalds p.iph.version = 4; 27881da177e4SLinus Torvalds p.iph.ihl = 5; 27891da177e4SLinus Torvalds p.iph.protocol = IPPROTO_IPV6; 27901da177e4SLinus Torvalds p.iph.ttl = 64; 2791d20b3109SStephen Hemminger ifr.ifr_ifru.ifru_data = (__force void __user *)&p; 27921da177e4SLinus Torvalds 27935bc3eb7eSStephen Hemminger if (ops->ndo_do_ioctl) { 27945bc3eb7eSStephen Hemminger mm_segment_t oldfs = get_fs(); 27955bc3eb7eSStephen Hemminger 27965bc3eb7eSStephen Hemminger set_fs(KERNEL_DS); 27975bc3eb7eSStephen Hemminger err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL); 27981da177e4SLinus Torvalds set_fs(oldfs); 27995bc3eb7eSStephen Hemminger } else 28005bc3eb7eSStephen Hemminger err = -EOPNOTSUPP; 28011da177e4SLinus Torvalds 28021da177e4SLinus Torvalds if (err == 0) { 28031da177e4SLinus Torvalds err = -ENOBUFS; 2804af284937SDaniel Lezcano dev = __dev_get_by_name(net, p.name); 2805af284937SDaniel Lezcano if (!dev) 28061da177e4SLinus Torvalds goto err_exit; 28071da177e4SLinus Torvalds err = dev_open(dev); 28081da177e4SLinus Torvalds } 28091da177e4SLinus Torvalds } 28100be669bbSJoerg Roedel #endif 28111da177e4SLinus Torvalds 28121da177e4SLinus Torvalds err_exit: 28131da177e4SLinus Torvalds rtnl_unlock(); 28141da177e4SLinus Torvalds return err; 28151da177e4SLinus Torvalds } 28161da177e4SLinus Torvalds 281793a714d6SMadhu Challa static int ipv6_mc_config(struct sock *sk, bool join, 281893a714d6SMadhu Challa const struct in6_addr *addr, int ifindex) 281993a714d6SMadhu Challa { 282093a714d6SMadhu Challa int ret; 282193a714d6SMadhu Challa 282293a714d6SMadhu Challa ASSERT_RTNL(); 282393a714d6SMadhu Challa 282493a714d6SMadhu Challa lock_sock(sk); 282593a714d6SMadhu Challa if (join) 282654ff9ef3SMarcelo Ricardo Leitner ret = ipv6_sock_mc_join(sk, ifindex, addr); 282793a714d6SMadhu Challa else 282854ff9ef3SMarcelo Ricardo Leitner ret = ipv6_sock_mc_drop(sk, ifindex, addr); 282993a714d6SMadhu Challa release_sock(sk); 283093a714d6SMadhu Challa 283193a714d6SMadhu Challa return ret; 283293a714d6SMadhu Challa } 283393a714d6SMadhu Challa 28341da177e4SLinus Torvalds /* 28351da177e4SLinus Torvalds * Manual configuration of address on an interface 28361da177e4SLinus Torvalds */ 2837479840ffSJiri Pirko static int inet6_addr_add(struct net *net, int ifindex, 283819b1518cSDavid Ahern struct ifa6_config *cfg, 2839de95e047SDavid Ahern struct netlink_ext_ack *extack) 28401da177e4SLinus Torvalds { 28411da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 28421da177e4SLinus Torvalds struct inet6_dev *idev; 28431da177e4SLinus Torvalds struct net_device *dev; 284493a714d6SMadhu Challa unsigned long timeout; 284593a714d6SMadhu Challa clock_t expires; 28466f704992SYOSHIFUJI Hideaki u32 flags; 28471da177e4SLinus Torvalds 28481da177e4SLinus Torvalds ASSERT_RTNL(); 28491da177e4SLinus Torvalds 285019b1518cSDavid Ahern if (cfg->plen > 128) 285124ef0da7SThomas Graf return -EINVAL; 285224ef0da7SThomas Graf 28530778769dSNoriaki TAKAMIYA /* check the lifetime */ 285419b1518cSDavid Ahern if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft) 28550778769dSNoriaki TAKAMIYA return -EINVAL; 28560778769dSNoriaki TAKAMIYA 285719b1518cSDavid Ahern if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR && cfg->plen != 64) 285853bd6749SJiri Pirko return -EINVAL; 285953bd6749SJiri Pirko 2860af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifindex); 2861af284937SDaniel Lezcano if (!dev) 28621da177e4SLinus Torvalds return -ENODEV; 28631da177e4SLinus Torvalds 286464e724f6SBrian Haley idev = addrconf_add_dev(dev); 286564e724f6SBrian Haley if (IS_ERR(idev)) 286664e724f6SBrian Haley return PTR_ERR(idev); 28671da177e4SLinus Torvalds 286819b1518cSDavid Ahern if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) { 286993a714d6SMadhu Challa int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk, 287019b1518cSDavid Ahern true, cfg->pfx, ifindex); 287193a714d6SMadhu Challa 287293a714d6SMadhu Challa if (ret < 0) 287393a714d6SMadhu Challa return ret; 287493a714d6SMadhu Challa } 287593a714d6SMadhu Challa 287619b1518cSDavid Ahern cfg->scope = ipv6_addr_scope(cfg->pfx); 28771da177e4SLinus Torvalds 287819b1518cSDavid Ahern timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ); 28794bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 28804bed72e4SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(timeout * HZ); 288119b1518cSDavid Ahern cfg->valid_lft = timeout; 28826f704992SYOSHIFUJI Hideaki flags = RTF_EXPIRES; 28834bed72e4SYOSHIFUJI Hideaki } else { 28844bed72e4SYOSHIFUJI Hideaki expires = 0; 28854bed72e4SYOSHIFUJI Hideaki flags = 0; 288619b1518cSDavid Ahern cfg->ifa_flags |= IFA_F_PERMANENT; 28876f704992SYOSHIFUJI Hideaki } 28880778769dSNoriaki TAKAMIYA 288919b1518cSDavid Ahern timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ); 28904bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 28914bed72e4SYOSHIFUJI Hideaki if (timeout == 0) 289219b1518cSDavid Ahern cfg->ifa_flags |= IFA_F_DEPRECATED; 289319b1518cSDavid Ahern cfg->preferred_lft = timeout; 28944bed72e4SYOSHIFUJI Hideaki } 28950778769dSNoriaki TAKAMIYA 289619b1518cSDavid Ahern ifp = ipv6_add_addr(idev, cfg, true, extack); 28971da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 289819b1518cSDavid Ahern if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) { 28998308f3ffSDavid Ahern addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 29008308f3ffSDavid Ahern ifp->rt_priority, dev, expires, 29018308f3ffSDavid Ahern flags, GFP_KERNEL); 2902761aac73SThomas Haller } 2903761aac73SThomas Haller 2904a2d481b3SLorenzo Bianconi /* Send a netlink notification if DAD is enabled and 2905a2d481b3SLorenzo Bianconi * optimistic flag is not set 2906a2d481b3SLorenzo Bianconi */ 2907a2d481b3SLorenzo Bianconi if (!(ifp->flags & (IFA_F_OPTIMISTIC | IFA_F_NODAD))) 2908a2d481b3SLorenzo Bianconi ipv6_ifa_notify(0, ifp); 290995c385b4SNeil Horman /* 291095c385b4SNeil Horman * Note that section 3.1 of RFC 4429 indicates 291195c385b4SNeil Horman * that the Optimistic flag should not be set for 291295c385b4SNeil Horman * manually configured addresses 291395c385b4SNeil Horman */ 2914cf22f9a2SDavid S. Miller addrconf_dad_start(ifp); 291519b1518cSDavid Ahern if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR) 291619b1518cSDavid Ahern manage_tempaddrs(idev, ifp, cfg->valid_lft, 291719b1518cSDavid Ahern cfg->preferred_lft, true, jiffies); 29181da177e4SLinus Torvalds in6_ifa_put(ifp); 2919c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 29201da177e4SLinus Torvalds return 0; 292119b1518cSDavid Ahern } else if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) { 292219b1518cSDavid Ahern ipv6_mc_config(net->ipv6.mc_autojoin_sk, false, 292319b1518cSDavid Ahern cfg->pfx, ifindex); 29241da177e4SLinus Torvalds } 29251da177e4SLinus Torvalds 29261da177e4SLinus Torvalds return PTR_ERR(ifp); 29271da177e4SLinus Torvalds } 29281da177e4SLinus Torvalds 29296046d5b4SHeiner Kallweit static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags, 29306046d5b4SHeiner Kallweit const struct in6_addr *pfx, unsigned int plen) 29311da177e4SLinus Torvalds { 29321da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 29331da177e4SLinus Torvalds struct inet6_dev *idev; 29341da177e4SLinus Torvalds struct net_device *dev; 29351da177e4SLinus Torvalds 293624ef0da7SThomas Graf if (plen > 128) 293724ef0da7SThomas Graf return -EINVAL; 293824ef0da7SThomas Graf 2939af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifindex); 2940af284937SDaniel Lezcano if (!dev) 29411da177e4SLinus Torvalds return -ENODEV; 29421da177e4SLinus Torvalds 2943e5d08d71SIan Morris idev = __in6_dev_get(dev); 294463159f29SIan Morris if (!idev) 29451da177e4SLinus Torvalds return -ENXIO; 29461da177e4SLinus Torvalds 29471da177e4SLinus Torvalds read_lock_bh(&idev->lock); 2948502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 29491da177e4SLinus Torvalds if (ifp->prefix_len == plen && 29501da177e4SLinus Torvalds ipv6_addr_equal(pfx, &ifp->addr)) { 29511da177e4SLinus Torvalds in6_ifa_hold(ifp); 29521da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 29531da177e4SLinus Torvalds 29546046d5b4SHeiner Kallweit if (!(ifp->flags & IFA_F_TEMPORARY) && 29556046d5b4SHeiner Kallweit (ifa_flags & IFA_F_MANAGETEMPADDR)) 29566046d5b4SHeiner Kallweit manage_tempaddrs(idev, ifp, 0, 0, false, 29576046d5b4SHeiner Kallweit jiffies); 29581da177e4SLinus Torvalds ipv6_del_addr(ifp); 29596046d5b4SHeiner Kallweit addrconf_verify_rtnl(); 296093a714d6SMadhu Challa if (ipv6_addr_is_multicast(pfx)) { 296193a714d6SMadhu Challa ipv6_mc_config(net->ipv6.mc_autojoin_sk, 296293a714d6SMadhu Challa false, pfx, dev->ifindex); 296393a714d6SMadhu Challa } 29641da177e4SLinus Torvalds return 0; 29651da177e4SLinus Torvalds } 29661da177e4SLinus Torvalds } 29671da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 29681da177e4SLinus Torvalds return -EADDRNOTAVAIL; 29691da177e4SLinus Torvalds } 29701da177e4SLinus Torvalds 29711da177e4SLinus Torvalds 2972af284937SDaniel Lezcano int addrconf_add_ifaddr(struct net *net, void __user *arg) 29731da177e4SLinus Torvalds { 297419b1518cSDavid Ahern struct ifa6_config cfg = { 297519b1518cSDavid Ahern .ifa_flags = IFA_F_PERMANENT, 297619b1518cSDavid Ahern .preferred_lft = INFINITY_LIFE_TIME, 297719b1518cSDavid Ahern .valid_lft = INFINITY_LIFE_TIME, 297819b1518cSDavid Ahern }; 29791da177e4SLinus Torvalds struct in6_ifreq ireq; 29801da177e4SLinus Torvalds int err; 29811da177e4SLinus Torvalds 2982af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 29831da177e4SLinus Torvalds return -EPERM; 29841da177e4SLinus Torvalds 29851da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 29861da177e4SLinus Torvalds return -EFAULT; 29871da177e4SLinus Torvalds 298819b1518cSDavid Ahern cfg.pfx = &ireq.ifr6_addr; 298919b1518cSDavid Ahern cfg.plen = ireq.ifr6_prefixlen; 299019b1518cSDavid Ahern 29911da177e4SLinus Torvalds rtnl_lock(); 299219b1518cSDavid Ahern err = inet6_addr_add(net, ireq.ifr6_ifindex, &cfg, NULL); 29931da177e4SLinus Torvalds rtnl_unlock(); 29941da177e4SLinus Torvalds return err; 29951da177e4SLinus Torvalds } 29961da177e4SLinus Torvalds 2997af284937SDaniel Lezcano int addrconf_del_ifaddr(struct net *net, void __user *arg) 29981da177e4SLinus Torvalds { 29991da177e4SLinus Torvalds struct in6_ifreq ireq; 30001da177e4SLinus Torvalds int err; 30011da177e4SLinus Torvalds 3002af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 30031da177e4SLinus Torvalds return -EPERM; 30041da177e4SLinus Torvalds 30051da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 30061da177e4SLinus Torvalds return -EFAULT; 30071da177e4SLinus Torvalds 30081da177e4SLinus Torvalds rtnl_lock(); 30096046d5b4SHeiner Kallweit err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr, 3010af284937SDaniel Lezcano ireq.ifr6_prefixlen); 30111da177e4SLinus Torvalds rtnl_unlock(); 30121da177e4SLinus Torvalds return err; 30131da177e4SLinus Torvalds } 30141da177e4SLinus Torvalds 3015b5f348e5SIlpo Järvinen static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr, 3016b5f348e5SIlpo Järvinen int plen, int scope) 3017b5f348e5SIlpo Järvinen { 3018b5f348e5SIlpo Järvinen struct inet6_ifaddr *ifp; 3019e6464b8cSDavid Ahern struct ifa6_config cfg = { 3020e6464b8cSDavid Ahern .pfx = addr, 3021e6464b8cSDavid Ahern .plen = plen, 3022e6464b8cSDavid Ahern .ifa_flags = IFA_F_PERMANENT, 3023e6464b8cSDavid Ahern .valid_lft = INFINITY_LIFE_TIME, 3024e6464b8cSDavid Ahern .preferred_lft = INFINITY_LIFE_TIME, 3025e6464b8cSDavid Ahern .scope = scope 3026e6464b8cSDavid Ahern }; 3027b5f348e5SIlpo Järvinen 3028e6464b8cSDavid Ahern ifp = ipv6_add_addr(idev, &cfg, true, NULL); 3029b5f348e5SIlpo Järvinen if (!IS_ERR(ifp)) { 3030b5f348e5SIlpo Järvinen spin_lock_bh(&ifp->lock); 3031b5f348e5SIlpo Järvinen ifp->flags &= ~IFA_F_TENTATIVE; 3032b5f348e5SIlpo Järvinen spin_unlock_bh(&ifp->lock); 3033764d3be6SPaolo Abeni rt_genid_bump_ipv6(dev_net(idev->dev)); 3034b5f348e5SIlpo Järvinen ipv6_ifa_notify(RTM_NEWADDR, ifp); 3035b5f348e5SIlpo Järvinen in6_ifa_put(ifp); 3036b5f348e5SIlpo Järvinen } 3037b5f348e5SIlpo Järvinen } 3038b5f348e5SIlpo Järvinen 303907a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 30401da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev) 30411da177e4SLinus Torvalds { 30421da177e4SLinus Torvalds struct in6_addr addr; 30431da177e4SLinus Torvalds struct net_device *dev; 3044c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 3045929c9cf3SNicolas Dichtel int scope, plen; 3046f0e2acfaSNicolas Dichtel u32 pflags = 0; 30471da177e4SLinus Torvalds 30481da177e4SLinus Torvalds ASSERT_RTNL(); 30491da177e4SLinus Torvalds 30501da177e4SLinus Torvalds memset(&addr, 0, sizeof(struct in6_addr)); 30511da177e4SLinus Torvalds memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4); 30521da177e4SLinus Torvalds 30531da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) { 30541da177e4SLinus Torvalds addr.s6_addr32[0] = htonl(0xfe800000); 30551da177e4SLinus Torvalds scope = IFA_LINK; 3056929c9cf3SNicolas Dichtel plen = 64; 30571da177e4SLinus Torvalds } else { 30581da177e4SLinus Torvalds scope = IPV6_ADDR_COMPATv4; 3059929c9cf3SNicolas Dichtel plen = 96; 3060f0e2acfaSNicolas Dichtel pflags |= RTF_NONEXTHOP; 30611da177e4SLinus Torvalds } 30621da177e4SLinus Torvalds 30631da177e4SLinus Torvalds if (addr.s6_addr32[3]) { 3064929c9cf3SNicolas Dichtel add_addr(idev, &addr, plen, scope); 30658308f3ffSDavid Ahern addrconf_prefix_route(&addr, plen, 0, idev->dev, 0, pflags, 3066acb54e3cSDavid Ahern GFP_ATOMIC); 30671da177e4SLinus Torvalds return; 30681da177e4SLinus Torvalds } 30691da177e4SLinus Torvalds 30706fda7350SBenjamin Thery for_each_netdev(net, dev) { 3071e5ed6399SHerbert Xu struct in_device *in_dev = __in_dev_get_rtnl(dev); 30721da177e4SLinus Torvalds if (in_dev && (dev->flags & IFF_UP)) { 30731da177e4SLinus Torvalds struct in_ifaddr *ifa; 30741da177e4SLinus Torvalds 30751da177e4SLinus Torvalds int flag = scope; 30761da177e4SLinus Torvalds 30771da177e4SLinus Torvalds for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) { 30781da177e4SLinus Torvalds 30791da177e4SLinus Torvalds addr.s6_addr32[3] = ifa->ifa_local; 30801da177e4SLinus Torvalds 30811da177e4SLinus Torvalds if (ifa->ifa_scope == RT_SCOPE_LINK) 30821da177e4SLinus Torvalds continue; 30831da177e4SLinus Torvalds if (ifa->ifa_scope >= RT_SCOPE_HOST) { 30841da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) 30851da177e4SLinus Torvalds continue; 30861da177e4SLinus Torvalds flag |= IFA_HOST; 30871da177e4SLinus Torvalds } 30881da177e4SLinus Torvalds 3089b5f348e5SIlpo Järvinen add_addr(idev, &addr, plen, flag); 30908308f3ffSDavid Ahern addrconf_prefix_route(&addr, plen, 0, idev->dev, 30918308f3ffSDavid Ahern 0, pflags, GFP_ATOMIC); 30921da177e4SLinus Torvalds } 30931da177e4SLinus Torvalds } 30941da177e4SLinus Torvalds } 30951da177e4SLinus Torvalds } 30960be669bbSJoerg Roedel #endif 30971da177e4SLinus Torvalds 30981da177e4SLinus Torvalds static void init_loopback(struct net_device *dev) 30991da177e4SLinus Torvalds { 31001da177e4SLinus Torvalds struct inet6_dev *idev; 31011da177e4SLinus Torvalds 31021da177e4SLinus Torvalds /* ::1 */ 31031da177e4SLinus Torvalds 31041da177e4SLinus Torvalds ASSERT_RTNL(); 31051da177e4SLinus Torvalds 3106e5d08d71SIan Morris idev = ipv6_find_idev(dev); 310763159f29SIan Morris if (!idev) { 310891df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 31091da177e4SLinus Torvalds return; 31101da177e4SLinus Torvalds } 31111da177e4SLinus Torvalds 3112b5f348e5SIlpo Järvinen add_addr(idev, &in6addr_loopback, 128, IFA_HOST); 31131da177e4SLinus Torvalds } 31141da177e4SLinus Torvalds 31152ad3ed59SAlexander Aring void addrconf_add_linklocal(struct inet6_dev *idev, 311664236f3fSHannes Frederic Sowa const struct in6_addr *addr, u32 flags) 31171da177e4SLinus Torvalds { 3118e6464b8cSDavid Ahern struct ifa6_config cfg = { 3119e6464b8cSDavid Ahern .pfx = addr, 3120e6464b8cSDavid Ahern .plen = 64, 3121e6464b8cSDavid Ahern .ifa_flags = flags | IFA_F_PERMANENT, 3122e6464b8cSDavid Ahern .valid_lft = INFINITY_LIFE_TIME, 3123e6464b8cSDavid Ahern .preferred_lft = INFINITY_LIFE_TIME, 3124e6464b8cSDavid Ahern .scope = IFA_LINK 3125e6464b8cSDavid Ahern }; 31261da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 31271da177e4SLinus Torvalds 312895c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 312935e015e1SMatteo Croce if ((dev_net(idev->dev)->ipv6.devconf_all->optimistic_dad || 313035e015e1SMatteo Croce idev->cnf.optimistic_dad) && 3131702beb87SDavid Miller !dev_net(idev->dev)->ipv6.devconf_all->forwarding) 3132e6464b8cSDavid Ahern cfg.ifa_flags |= IFA_F_OPTIMISTIC; 313395c385b4SNeil Horman #endif 313495c385b4SNeil Horman 3135e6464b8cSDavid Ahern ifp = ipv6_add_addr(idev, &cfg, true, NULL); 31361da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 31378308f3ffSDavid Ahern addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 0, idev->dev, 3138acb54e3cSDavid Ahern 0, 0, GFP_ATOMIC); 3139cf22f9a2SDavid S. Miller addrconf_dad_start(ifp); 31401da177e4SLinus Torvalds in6_ifa_put(ifp); 31411da177e4SLinus Torvalds } 31421da177e4SLinus Torvalds } 31432ad3ed59SAlexander Aring EXPORT_SYMBOL_GPL(addrconf_add_linklocal); 31441da177e4SLinus Torvalds 3145622c81d5SHannes Frederic Sowa static bool ipv6_reserved_interfaceid(struct in6_addr address) 3146622c81d5SHannes Frederic Sowa { 3147622c81d5SHannes Frederic Sowa if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0) 3148622c81d5SHannes Frederic Sowa return true; 3149622c81d5SHannes Frederic Sowa 3150622c81d5SHannes Frederic Sowa if (address.s6_addr32[2] == htonl(0x02005eff) && 3151622c81d5SHannes Frederic Sowa ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000))) 3152622c81d5SHannes Frederic Sowa return true; 3153622c81d5SHannes Frederic Sowa 3154622c81d5SHannes Frederic Sowa if (address.s6_addr32[2] == htonl(0xfdffffff) && 3155622c81d5SHannes Frederic Sowa ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80))) 3156622c81d5SHannes Frederic Sowa return true; 3157622c81d5SHannes Frederic Sowa 3158622c81d5SHannes Frederic Sowa return false; 3159622c81d5SHannes Frederic Sowa } 3160622c81d5SHannes Frederic Sowa 3161622c81d5SHannes Frederic Sowa static int ipv6_generate_stable_address(struct in6_addr *address, 3162622c81d5SHannes Frederic Sowa u8 dad_count, 3163622c81d5SHannes Frederic Sowa const struct inet6_dev *idev) 3164622c81d5SHannes Frederic Sowa { 3165622c81d5SHannes Frederic Sowa static DEFINE_SPINLOCK(lock); 3166622c81d5SHannes Frederic Sowa static __u32 digest[SHA_DIGEST_WORDS]; 3167622c81d5SHannes Frederic Sowa static __u32 workspace[SHA_WORKSPACE_WORDS]; 3168622c81d5SHannes Frederic Sowa 3169622c81d5SHannes Frederic Sowa static union { 3170622c81d5SHannes Frederic Sowa char __data[SHA_MESSAGE_BYTES]; 3171622c81d5SHannes Frederic Sowa struct { 3172622c81d5SHannes Frederic Sowa struct in6_addr secret; 3173ff40217eSHannes Frederic Sowa __be32 prefix[2]; 3174622c81d5SHannes Frederic Sowa unsigned char hwaddr[MAX_ADDR_LEN]; 3175622c81d5SHannes Frederic Sowa u8 dad_count; 3176622c81d5SHannes Frederic Sowa } __packed; 3177622c81d5SHannes Frederic Sowa } data; 3178622c81d5SHannes Frederic Sowa 3179622c81d5SHannes Frederic Sowa struct in6_addr secret; 3180622c81d5SHannes Frederic Sowa struct in6_addr temp; 3181622c81d5SHannes Frederic Sowa struct net *net = dev_net(idev->dev); 3182622c81d5SHannes Frederic Sowa 3183622c81d5SHannes Frederic Sowa BUILD_BUG_ON(sizeof(data.__data) != sizeof(data)); 3184622c81d5SHannes Frederic Sowa 3185622c81d5SHannes Frederic Sowa if (idev->cnf.stable_secret.initialized) 3186622c81d5SHannes Frederic Sowa secret = idev->cnf.stable_secret.secret; 3187622c81d5SHannes Frederic Sowa else if (net->ipv6.devconf_dflt->stable_secret.initialized) 3188622c81d5SHannes Frederic Sowa secret = net->ipv6.devconf_dflt->stable_secret.secret; 3189622c81d5SHannes Frederic Sowa else 3190622c81d5SHannes Frederic Sowa return -1; 3191622c81d5SHannes Frederic Sowa 3192622c81d5SHannes Frederic Sowa retry: 3193622c81d5SHannes Frederic Sowa spin_lock_bh(&lock); 3194622c81d5SHannes Frederic Sowa 3195622c81d5SHannes Frederic Sowa sha_init(digest); 3196622c81d5SHannes Frederic Sowa memset(&data, 0, sizeof(data)); 3197622c81d5SHannes Frederic Sowa memset(workspace, 0, sizeof(workspace)); 3198622c81d5SHannes Frederic Sowa memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len); 3199ff40217eSHannes Frederic Sowa data.prefix[0] = address->s6_addr32[0]; 3200ff40217eSHannes Frederic Sowa data.prefix[1] = address->s6_addr32[1]; 3201622c81d5SHannes Frederic Sowa data.secret = secret; 3202622c81d5SHannes Frederic Sowa data.dad_count = dad_count; 3203622c81d5SHannes Frederic Sowa 3204622c81d5SHannes Frederic Sowa sha_transform(digest, data.__data, workspace); 3205622c81d5SHannes Frederic Sowa 3206622c81d5SHannes Frederic Sowa temp = *address; 3207ff40217eSHannes Frederic Sowa temp.s6_addr32[2] = (__force __be32)digest[0]; 3208ff40217eSHannes Frederic Sowa temp.s6_addr32[3] = (__force __be32)digest[1]; 3209622c81d5SHannes Frederic Sowa 3210622c81d5SHannes Frederic Sowa spin_unlock_bh(&lock); 3211622c81d5SHannes Frederic Sowa 3212622c81d5SHannes Frederic Sowa if (ipv6_reserved_interfaceid(temp)) { 3213622c81d5SHannes Frederic Sowa dad_count++; 32141855b7c3SHannes Frederic Sowa if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries) 3215622c81d5SHannes Frederic Sowa return -1; 3216622c81d5SHannes Frederic Sowa goto retry; 3217622c81d5SHannes Frederic Sowa } 3218622c81d5SHannes Frederic Sowa 3219622c81d5SHannes Frederic Sowa *address = temp; 3220622c81d5SHannes Frederic Sowa return 0; 3221622c81d5SHannes Frederic Sowa } 3222622c81d5SHannes Frederic Sowa 3223cc9da6ccSBjørn Mork static void ipv6_gen_mode_random_init(struct inet6_dev *idev) 3224cc9da6ccSBjørn Mork { 3225cc9da6ccSBjørn Mork struct ipv6_stable_secret *s = &idev->cnf.stable_secret; 3226cc9da6ccSBjørn Mork 3227cc9da6ccSBjørn Mork if (s->initialized) 3228cc9da6ccSBjørn Mork return; 3229cc9da6ccSBjørn Mork s = &idev->cnf.stable_secret; 3230cc9da6ccSBjørn Mork get_random_bytes(&s->secret, sizeof(s->secret)); 3231cc9da6ccSBjørn Mork s->initialized = true; 3232cc9da6ccSBjørn Mork } 3233cc9da6ccSBjørn Mork 3234bc91b0f0SJiri Pirko static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route) 3235bc91b0f0SJiri Pirko { 3236bc91b0f0SJiri Pirko struct in6_addr addr; 3237bc91b0f0SJiri Pirko 3238ca254490SDavid Ahern /* no link local addresses on L3 master devices */ 3239ca254490SDavid Ahern if (netif_is_l3_master(idev->dev)) 3240ca254490SDavid Ahern return; 3241ca254490SDavid Ahern 3242bc91b0f0SJiri Pirko ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0); 3243622c81d5SHannes Frederic Sowa 3244d35a00b8SFelix Jia switch (idev->cnf.addr_gen_mode) { 3245cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_RANDOM: 3246cc9da6ccSBjørn Mork ipv6_gen_mode_random_init(idev); 3247cc9da6ccSBjørn Mork /* fallthrough */ 3248cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_STABLE_PRIVACY: 3249622c81d5SHannes Frederic Sowa if (!ipv6_generate_stable_address(&addr, 0, idev)) 325064236f3fSHannes Frederic Sowa addrconf_add_linklocal(idev, &addr, 325164236f3fSHannes Frederic Sowa IFA_F_STABLE_PRIVACY); 3252622c81d5SHannes Frederic Sowa else if (prefix_route) 32538308f3ffSDavid Ahern addrconf_prefix_route(&addr, 64, 0, idev->dev, 3254acb54e3cSDavid Ahern 0, 0, GFP_KERNEL); 3255cc9da6ccSBjørn Mork break; 3256cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_EUI64: 3257bc91b0f0SJiri Pirko /* addrconf_add_linklocal also adds a prefix_route and we 3258bc91b0f0SJiri Pirko * only need to care about prefix routes if ipv6_generate_eui64 3259bc91b0f0SJiri Pirko * couldn't generate one. 3260bc91b0f0SJiri Pirko */ 3261bc91b0f0SJiri Pirko if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0) 326264236f3fSHannes Frederic Sowa addrconf_add_linklocal(idev, &addr, 0); 3263bc91b0f0SJiri Pirko else if (prefix_route) 32648308f3ffSDavid Ahern addrconf_prefix_route(&addr, 64, 0, idev->dev, 326527b10608SDavid Ahern 0, 0, GFP_KERNEL); 3266cc9da6ccSBjørn Mork break; 3267cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_NONE: 3268cc9da6ccSBjørn Mork default: 3269cc9da6ccSBjørn Mork /* will not add any link local address */ 3270cc9da6ccSBjørn Mork break; 3271bc91b0f0SJiri Pirko } 3272bc91b0f0SJiri Pirko } 3273bc91b0f0SJiri Pirko 32741da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev) 32751da177e4SLinus Torvalds { 32761da177e4SLinus Torvalds struct inet6_dev *idev; 32771da177e4SLinus Torvalds 32781da177e4SLinus Torvalds ASSERT_RTNL(); 32791da177e4SLinus Torvalds 328074235a25SHerbert Xu if ((dev->type != ARPHRD_ETHER) && 328174235a25SHerbert Xu (dev->type != ARPHRD_FDDI) && 328274235a25SHerbert Xu (dev->type != ARPHRD_ARCNET) && 328306a4c1c5Salex.bluesman.smirnov@gmail.com (dev->type != ARPHRD_INFINIBAND) && 3284e837735eSNicolas Dichtel (dev->type != ARPHRD_IEEE1394) && 3285e74bccb8SJukka Rissanen (dev->type != ARPHRD_TUNNEL6) && 3286cc9da6ccSBjørn Mork (dev->type != ARPHRD_6LOWPAN) && 328745ce0fd1SFelix Jia (dev->type != ARPHRD_IP6GRE) && 328845ce0fd1SFelix Jia (dev->type != ARPHRD_IPGRE) && 328945ce0fd1SFelix Jia (dev->type != ARPHRD_TUNNEL) && 3290*9deb441cSSubash Abhinov Kasiviswanathan (dev->type != ARPHRD_NONE) && 3291*9deb441cSSubash Abhinov Kasiviswanathan (dev->type != ARPHRD_RAWIP)) { 329274235a25SHerbert Xu /* Alas, we support only Ethernet autoconfiguration. */ 329374235a25SHerbert Xu return; 329474235a25SHerbert Xu } 329574235a25SHerbert Xu 32961da177e4SLinus Torvalds idev = addrconf_add_dev(dev); 329764e724f6SBrian Haley if (IS_ERR(idev)) 32981da177e4SLinus Torvalds return; 32991da177e4SLinus Torvalds 3300cc9da6ccSBjørn Mork /* this device type has no EUI support */ 3301cc9da6ccSBjørn Mork if (dev->type == ARPHRD_NONE && 3302d35a00b8SFelix Jia idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64) 3303d35a00b8SFelix Jia idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM; 3304cc9da6ccSBjørn Mork 3305bc91b0f0SJiri Pirko addrconf_addr_gen(idev, false); 33061da177e4SLinus Torvalds } 33071da177e4SLinus Torvalds 330807a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 33091da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev) 33101da177e4SLinus Torvalds { 33111da177e4SLinus Torvalds struct inet6_dev *idev; 33121da177e4SLinus Torvalds 33131da177e4SLinus Torvalds ASSERT_RTNL(); 33141da177e4SLinus Torvalds 33151da177e4SLinus Torvalds /* 33161da177e4SLinus Torvalds * Configure the tunnel with one of our IPv4 33171da177e4SLinus Torvalds * addresses... we should configure all of 33181da177e4SLinus Torvalds * our v4 addrs in the tunnel 33191da177e4SLinus Torvalds */ 33201da177e4SLinus Torvalds 3321e5d08d71SIan Morris idev = ipv6_find_idev(dev); 332263159f29SIan Morris if (!idev) { 332391df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 33241da177e4SLinus Torvalds return; 33251da177e4SLinus Torvalds } 33261da177e4SLinus Torvalds 3327c7dc89c0SFred L. Templin if (dev->priv_flags & IFF_ISATAP) { 3328bc91b0f0SJiri Pirko addrconf_addr_gen(idev, false); 3329c7dc89c0SFred L. Templin return; 3330c7dc89c0SFred L. Templin } 3331c7dc89c0SFred L. Templin 33321da177e4SLinus Torvalds sit_add_v4_addrs(idev); 33331da177e4SLinus Torvalds 333462b54dd9SNicolas Dichtel if (dev->flags&IFF_POINTOPOINT) 33351da177e4SLinus Torvalds addrconf_add_mroute(dev); 33361da177e4SLinus Torvalds } 33370be669bbSJoerg Roedel #endif 33381da177e4SLinus Torvalds 333907a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE) 3340aee80b54Sstephen hemminger static void addrconf_gre_config(struct net_device *dev) 3341aee80b54Sstephen hemminger { 3342aee80b54Sstephen hemminger struct inet6_dev *idev; 3343aee80b54Sstephen hemminger 3344aee80b54Sstephen hemminger ASSERT_RTNL(); 3345aee80b54Sstephen hemminger 3346e5d08d71SIan Morris idev = ipv6_find_idev(dev); 334763159f29SIan Morris if (!idev) { 334891df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 3349aee80b54Sstephen hemminger return; 3350aee80b54Sstephen hemminger } 3351aee80b54Sstephen hemminger 3352bc91b0f0SJiri Pirko addrconf_addr_gen(idev, true); 3353d9e4ce65SHannes Frederic Sowa if (dev->flags & IFF_POINTOPOINT) 3354d9e4ce65SHannes Frederic Sowa addrconf_add_mroute(dev); 3355aee80b54Sstephen hemminger } 3356aee80b54Sstephen hemminger #endif 3357aee80b54Sstephen hemminger 3358afb1d4b5SDavid Ahern static int fixup_permanent_addr(struct net *net, 3359afb1d4b5SDavid Ahern struct inet6_dev *idev, 3360f1705ec1SDavid Ahern struct inet6_ifaddr *ifp) 3361f1705ec1SDavid Ahern { 336293c2fb25SDavid Ahern /* !fib6_node means the host route was removed from the 33638048ced9SDavid Ahern * FIB, for example, if 'lo' device is taken down. In that 33648048ced9SDavid Ahern * case regenerate the host route. 33658048ced9SDavid Ahern */ 336693c2fb25SDavid Ahern if (!ifp->rt || !ifp->rt->fib6_node) { 3367360a9887SDavid Ahern struct fib6_info *f6i, *prev; 3368f1705ec1SDavid Ahern 3369360a9887SDavid Ahern f6i = addrconf_f6i_alloc(net, idev, &ifp->addr, false, 3370acb54e3cSDavid Ahern GFP_ATOMIC); 3371360a9887SDavid Ahern if (IS_ERR(f6i)) 3372360a9887SDavid Ahern return PTR_ERR(f6i); 3373f1705ec1SDavid Ahern 33748048ced9SDavid Ahern /* ifp->rt can be accessed outside of rtnl */ 33758048ced9SDavid Ahern spin_lock(&ifp->lock); 33768048ced9SDavid Ahern prev = ifp->rt; 3377360a9887SDavid Ahern ifp->rt = f6i; 33788048ced9SDavid Ahern spin_unlock(&ifp->lock); 33798048ced9SDavid Ahern 338093531c67SDavid Ahern fib6_info_release(prev); 3381f1705ec1SDavid Ahern } 3382f1705ec1SDavid Ahern 3383f1705ec1SDavid Ahern if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) { 3384f1705ec1SDavid Ahern addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 33858308f3ffSDavid Ahern ifp->rt_priority, idev->dev, 0, 0, 33868308f3ffSDavid Ahern GFP_ATOMIC); 3387f1705ec1SDavid Ahern } 3388f1705ec1SDavid Ahern 33896d717134SDavid Ahern if (ifp->state == INET6_IFADDR_STATE_PREDAD) 3390f1705ec1SDavid Ahern addrconf_dad_start(ifp); 3391f1705ec1SDavid Ahern 3392f1705ec1SDavid Ahern return 0; 3393f1705ec1SDavid Ahern } 3394f1705ec1SDavid Ahern 3395afb1d4b5SDavid Ahern static void addrconf_permanent_addr(struct net *net, struct net_device *dev) 3396f1705ec1SDavid Ahern { 3397f1705ec1SDavid Ahern struct inet6_ifaddr *ifp, *tmp; 3398f1705ec1SDavid Ahern struct inet6_dev *idev; 3399f1705ec1SDavid Ahern 3400f1705ec1SDavid Ahern idev = __in6_dev_get(dev); 3401f1705ec1SDavid Ahern if (!idev) 3402f1705ec1SDavid Ahern return; 3403f1705ec1SDavid Ahern 3404f1705ec1SDavid Ahern write_lock_bh(&idev->lock); 3405f1705ec1SDavid Ahern 3406f1705ec1SDavid Ahern list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) { 3407f1705ec1SDavid Ahern if ((ifp->flags & IFA_F_PERMANENT) && 3408afb1d4b5SDavid Ahern fixup_permanent_addr(net, idev, ifp) < 0) { 3409f1705ec1SDavid Ahern write_unlock_bh(&idev->lock); 3410e669b869SEric Dumazet in6_ifa_hold(ifp); 3411f1705ec1SDavid Ahern ipv6_del_addr(ifp); 3412f1705ec1SDavid Ahern write_lock_bh(&idev->lock); 3413f1705ec1SDavid Ahern 3414f1705ec1SDavid Ahern net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n", 3415f1705ec1SDavid Ahern idev->dev->name, &ifp->addr); 3416f1705ec1SDavid Ahern } 3417f1705ec1SDavid Ahern } 3418f1705ec1SDavid Ahern 3419f1705ec1SDavid Ahern write_unlock_bh(&idev->lock); 3420f1705ec1SDavid Ahern } 3421f1705ec1SDavid Ahern 34221da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event, 3423351638e7SJiri Pirko void *ptr) 34241da177e4SLinus Torvalds { 3425351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 34264f7f34eaSDavid Ahern struct netdev_notifier_changeupper_info *info; 3427748e2d93SEric Dumazet struct inet6_dev *idev = __in6_dev_get(dev); 342860abc0beSWANG Cong struct net *net = dev_net(dev); 3429c5e33bddSYOSHIFUJI Hideaki int run_pending = 0; 3430b217d616SHerbert Xu int err; 34311da177e4SLinus Torvalds 34321da177e4SLinus Torvalds switch (event) { 343345ba9dd2SYOSHIFUJI Hideaki case NETDEV_REGISTER: 343474235a25SHerbert Xu if (!idev && dev->mtu >= IPV6_MIN_MTU) { 343545ba9dd2SYOSHIFUJI Hideaki idev = ipv6_add_dev(dev); 3436a317a2f1SWANG Cong if (IS_ERR(idev)) 3437a317a2f1SWANG Cong return notifier_from_errno(PTR_ERR(idev)); 343845ba9dd2SYOSHIFUJI Hideaki } 343945ba9dd2SYOSHIFUJI Hideaki break; 3440bcdd553fSStephen Hemminger 3441b7b0b1d2SAlexander Duyck case NETDEV_CHANGEMTU: 3442b7b0b1d2SAlexander Duyck /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */ 3443b7b0b1d2SAlexander Duyck if (dev->mtu < IPV6_MIN_MTU) { 344460abc0beSWANG Cong addrconf_ifdown(dev, dev != net->loopback_dev); 3445b7b0b1d2SAlexander Duyck break; 3446b7b0b1d2SAlexander Duyck } 3447b7b0b1d2SAlexander Duyck 3448b7b0b1d2SAlexander Duyck if (idev) { 3449b7b0b1d2SAlexander Duyck rt6_mtu_change(dev, dev->mtu); 3450b7b0b1d2SAlexander Duyck idev->cnf.mtu6 = dev->mtu; 3451b7b0b1d2SAlexander Duyck break; 3452b7b0b1d2SAlexander Duyck } 3453b7b0b1d2SAlexander Duyck 3454b7b0b1d2SAlexander Duyck /* allocate new idev */ 3455b7b0b1d2SAlexander Duyck idev = ipv6_add_dev(dev); 3456b7b0b1d2SAlexander Duyck if (IS_ERR(idev)) 3457b7b0b1d2SAlexander Duyck break; 3458b7b0b1d2SAlexander Duyck 3459b7b0b1d2SAlexander Duyck /* device is still not ready */ 3460b7b0b1d2SAlexander Duyck if (!(idev->if_flags & IF_READY)) 3461b7b0b1d2SAlexander Duyck break; 3462b7b0b1d2SAlexander Duyck 3463b7b0b1d2SAlexander Duyck run_pending = 1; 3464b7b0b1d2SAlexander Duyck 3465b7b0b1d2SAlexander Duyck /* fall through */ 3466b7b0b1d2SAlexander Duyck 34671da177e4SLinus Torvalds case NETDEV_UP: 34683c21edbdSYOSHIFUJI Hideaki case NETDEV_CHANGE: 3469c2edacf8SJay Vosburgh if (dev->flags & IFF_SLAVE) 3470c2edacf8SJay Vosburgh break; 3471c2edacf8SJay Vosburgh 34723ce62a84SWANG Cong if (idev && idev->cnf.disable_ipv6) 34733ce62a84SWANG Cong break; 34743ce62a84SWANG Cong 34753c21edbdSYOSHIFUJI Hideaki if (event == NETDEV_UP) { 347638bd10c4SDavid Ahern /* restore routes for permanent addresses */ 3477afb1d4b5SDavid Ahern addrconf_permanent_addr(net, dev); 347838bd10c4SDavid Ahern 34791f372c7bSMike Manning if (!addrconf_link_ready(dev)) { 34803c21edbdSYOSHIFUJI Hideaki /* device is not ready yet. */ 3481f3213831SJoe Perches pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n", 34823c21edbdSYOSHIFUJI Hideaki dev->name); 34833c21edbdSYOSHIFUJI Hideaki break; 34843c21edbdSYOSHIFUJI Hideaki } 348599081049SKristian Slavov 3486d31c7b8fSEvgeniy Polyakov if (!idev && dev->mtu >= IPV6_MIN_MTU) 3487d31c7b8fSEvgeniy Polyakov idev = ipv6_add_dev(dev); 3488d31c7b8fSEvgeniy Polyakov 3489a317a2f1SWANG Cong if (!IS_ERR_OR_NULL(idev)) { 349099081049SKristian Slavov idev->if_flags |= IF_READY; 3491e3ec6cfcSBenjamin Thery run_pending = 1; 3492e3ec6cfcSBenjamin Thery } 3493b7b0b1d2SAlexander Duyck } else if (event == NETDEV_CHANGE) { 34941f372c7bSMike Manning if (!addrconf_link_ready(dev)) { 34953c21edbdSYOSHIFUJI Hideaki /* device is still not ready. */ 349627c6fa73SIdo Schimmel rt6_sync_down_dev(dev, event); 34973c21edbdSYOSHIFUJI Hideaki break; 34983c21edbdSYOSHIFUJI Hideaki } 34993c21edbdSYOSHIFUJI Hideaki 35003c21edbdSYOSHIFUJI Hideaki if (idev) { 3501a088d1d7SLinus Lüssing if (idev->if_flags & IF_READY) { 3502a088d1d7SLinus Lüssing /* device is already configured - 3503a088d1d7SLinus Lüssing * but resend MLD reports, we might 3504a088d1d7SLinus Lüssing * have roamed and need to update 3505a088d1d7SLinus Lüssing * multicast snooping switches 3506a088d1d7SLinus Lüssing */ 3507a088d1d7SLinus Lüssing ipv6_mc_up(idev); 350827c6fa73SIdo Schimmel rt6_sync_up(dev, RTNH_F_LINKDOWN); 35093c21edbdSYOSHIFUJI Hideaki break; 3510a088d1d7SLinus Lüssing } 35113c21edbdSYOSHIFUJI Hideaki idev->if_flags |= IF_READY; 35123c21edbdSYOSHIFUJI Hideaki } 35133c21edbdSYOSHIFUJI Hideaki 3514f3213831SJoe Perches pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n", 35153c21edbdSYOSHIFUJI Hideaki dev->name); 35163c21edbdSYOSHIFUJI Hideaki 3517c5e33bddSYOSHIFUJI Hideaki run_pending = 1; 35183c21edbdSYOSHIFUJI Hideaki } 35193c21edbdSYOSHIFUJI Hideaki 35201da177e4SLinus Torvalds switch (dev->type) { 352107a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 35221da177e4SLinus Torvalds case ARPHRD_SIT: 35231da177e4SLinus Torvalds addrconf_sit_config(dev); 35241da177e4SLinus Torvalds break; 35250be669bbSJoerg Roedel #endif 352607a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE) 3527aee80b54Sstephen hemminger case ARPHRD_IPGRE: 3528aee80b54Sstephen hemminger addrconf_gre_config(dev); 3529aee80b54Sstephen hemminger break; 3530aee80b54Sstephen hemminger #endif 35311da177e4SLinus Torvalds case ARPHRD_LOOPBACK: 35321da177e4SLinus Torvalds init_loopback(dev); 35331da177e4SLinus Torvalds break; 35341da177e4SLinus Torvalds 35351da177e4SLinus Torvalds default: 35361da177e4SLinus Torvalds addrconf_dev_config(dev); 35371da177e4SLinus Torvalds break; 35383ff50b79SStephen Hemminger } 3539bcdd553fSStephen Hemminger 3540a317a2f1SWANG Cong if (!IS_ERR_OR_NULL(idev)) { 3541c5e33bddSYOSHIFUJI Hideaki if (run_pending) 3542c5e33bddSYOSHIFUJI Hideaki addrconf_dad_run(idev); 3543c5e33bddSYOSHIFUJI Hideaki 35442127d95aSIdo Schimmel /* Device has an address by now */ 35452127d95aSIdo Schimmel rt6_sync_up(dev, RTNH_F_DEAD); 35462127d95aSIdo Schimmel 3547bcdd553fSStephen Hemminger /* 3548bcdd553fSStephen Hemminger * If the MTU changed during the interface down, 3549bcdd553fSStephen Hemminger * when the interface up, the changed MTU must be 3550bcdd553fSStephen Hemminger * reflected in the idev as well as routers. 35511da177e4SLinus Torvalds */ 3552bcdd553fSStephen Hemminger if (idev->cnf.mtu6 != dev->mtu && 3553bcdd553fSStephen Hemminger dev->mtu >= IPV6_MIN_MTU) { 35541da177e4SLinus Torvalds rt6_mtu_change(dev, dev->mtu); 35551da177e4SLinus Torvalds idev->cnf.mtu6 = dev->mtu; 35561da177e4SLinus Torvalds } 35571da177e4SLinus Torvalds idev->tstamp = jiffies; 35581da177e4SLinus Torvalds inet6_ifinfo_notify(RTM_NEWLINK, idev); 3559bcdd553fSStephen Hemminger 3560bcdd553fSStephen Hemminger /* 3561bcdd553fSStephen Hemminger * If the changed mtu during down is lower than 3562bcdd553fSStephen Hemminger * IPV6_MIN_MTU stop IPv6 on this interface. 35631da177e4SLinus Torvalds */ 35641da177e4SLinus Torvalds if (dev->mtu < IPV6_MIN_MTU) 356560abc0beSWANG Cong addrconf_ifdown(dev, dev != net->loopback_dev); 35661da177e4SLinus Torvalds } 35671da177e4SLinus Torvalds break; 35681da177e4SLinus Torvalds 35691da177e4SLinus Torvalds case NETDEV_DOWN: 35701da177e4SLinus Torvalds case NETDEV_UNREGISTER: 35711da177e4SLinus Torvalds /* 35721da177e4SLinus Torvalds * Remove all addresses from this interface. 35731da177e4SLinus Torvalds */ 35741da177e4SLinus Torvalds addrconf_ifdown(dev, event != NETDEV_DOWN); 35751da177e4SLinus Torvalds break; 35763c21edbdSYOSHIFUJI Hideaki 35771da177e4SLinus Torvalds case NETDEV_CHANGENAME: 35781da177e4SLinus Torvalds if (idev) { 35795632c515SStephen Hemminger snmp6_unregister_dev(idev); 3580408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 3581a317a2f1SWANG Cong err = addrconf_sysctl_register(idev); 3582b217d616SHerbert Xu if (err) 3583b217d616SHerbert Xu return notifier_from_errno(err); 3584a317a2f1SWANG Cong err = snmp6_register_dev(idev); 3585a317a2f1SWANG Cong if (err) { 3586a317a2f1SWANG Cong addrconf_sysctl_unregister(idev); 3587a317a2f1SWANG Cong return notifier_from_errno(err); 3588a317a2f1SWANG Cong } 35895632c515SStephen Hemminger } 35901da177e4SLinus Torvalds break; 3591bcdd553fSStephen Hemminger 359293d9b7d7SJiri Pirko case NETDEV_PRE_TYPE_CHANGE: 359393d9b7d7SJiri Pirko case NETDEV_POST_TYPE_CHANGE: 35943ef0952cSAndrew Lunn if (idev) 359593d9b7d7SJiri Pirko addrconf_type_change(dev, event); 359675c78500SMoni Shoua break; 35974f7f34eaSDavid Ahern 35984f7f34eaSDavid Ahern case NETDEV_CHANGEUPPER: 35994f7f34eaSDavid Ahern info = ptr; 36004f7f34eaSDavid Ahern 36014f7f34eaSDavid Ahern /* flush all routes if dev is linked to or unlinked from 36024f7f34eaSDavid Ahern * an L3 master device (e.g., VRF) 36034f7f34eaSDavid Ahern */ 36044f7f34eaSDavid Ahern if (info->upper_dev && netif_is_l3_master(info->upper_dev)) 36054f7f34eaSDavid Ahern addrconf_ifdown(dev, 0); 36063ff50b79SStephen Hemminger } 36071da177e4SLinus Torvalds 36081da177e4SLinus Torvalds return NOTIFY_OK; 36091da177e4SLinus Torvalds } 36101da177e4SLinus Torvalds 36111da177e4SLinus Torvalds /* 36121da177e4SLinus Torvalds * addrconf module should be notified of a device going up 36131da177e4SLinus Torvalds */ 36141da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = { 36151da177e4SLinus Torvalds .notifier_call = addrconf_notify, 3616242d3a49SWANG Cong .priority = ADDRCONF_NOTIFY_PRIORITY, 36171da177e4SLinus Torvalds }; 36181da177e4SLinus Torvalds 361993d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, unsigned long event) 362075c78500SMoni Shoua { 362175c78500SMoni Shoua struct inet6_dev *idev; 362275c78500SMoni Shoua ASSERT_RTNL(); 362375c78500SMoni Shoua 362475c78500SMoni Shoua idev = __in6_dev_get(dev); 362575c78500SMoni Shoua 362693d9b7d7SJiri Pirko if (event == NETDEV_POST_TYPE_CHANGE) 362775c78500SMoni Shoua ipv6_mc_remap(idev); 362893d9b7d7SJiri Pirko else if (event == NETDEV_PRE_TYPE_CHANGE) 362975c78500SMoni Shoua ipv6_mc_unmap(idev); 363075c78500SMoni Shoua } 363175c78500SMoni Shoua 363270af921dSDavid Ahern static bool addr_is_local(const struct in6_addr *addr) 363370af921dSDavid Ahern { 363470af921dSDavid Ahern return ipv6_addr_type(addr) & 363570af921dSDavid Ahern (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK); 363670af921dSDavid Ahern } 363770af921dSDavid Ahern 36381da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how) 36391da177e4SLinus Torvalds { 36404c981e28SIdo Schimmel unsigned long event = how ? NETDEV_UNREGISTER : NETDEV_DOWN; 3641c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 3642502a2ffdSstephen hemminger struct inet6_dev *idev; 3643f1705ec1SDavid Ahern struct inet6_ifaddr *ifa, *tmp; 36440aef78aaSIvan Vecera bool keep_addr = false; 364573a8bd74SDavid S. Miller int state, i; 36461da177e4SLinus Torvalds 36471da177e4SLinus Torvalds ASSERT_RTNL(); 36481da177e4SLinus Torvalds 36494c981e28SIdo Schimmel rt6_disable_ip(dev, event); 36501da177e4SLinus Torvalds 36511da177e4SLinus Torvalds idev = __in6_dev_get(dev); 365263159f29SIan Morris if (!idev) 36531da177e4SLinus Torvalds return -ENODEV; 36541da177e4SLinus Torvalds 3655bcdd553fSStephen Hemminger /* 3656bcdd553fSStephen Hemminger * Step 1: remove reference to ipv6 device from parent device. 3657bcdd553fSStephen Hemminger * Do not dev_put! 36581da177e4SLinus Torvalds */ 3659439e2385SDenis V. Lunev if (how) { 36601da177e4SLinus Torvalds idev->dead = 1; 36618814c4b5SYOSHIFUJI Hideaki 36628814c4b5SYOSHIFUJI Hideaki /* protected by rtnl_lock */ 3663a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(dev->ip6_ptr, NULL); 36641da177e4SLinus Torvalds 36651da177e4SLinus Torvalds /* Step 1.5: remove snmp6 entry */ 36661da177e4SLinus Torvalds snmp6_unregister_dev(idev); 36671da177e4SLinus Torvalds 36681da177e4SLinus Torvalds } 36691da177e4SLinus Torvalds 3670f1705ec1SDavid Ahern /* combine the user config with event to determine if permanent 3671f1705ec1SDavid Ahern * addresses are to be removed from address hash table 3672f1705ec1SDavid Ahern */ 36730aef78aaSIvan Vecera if (!how && !idev->cnf.disable_ipv6) { 36740aef78aaSIvan Vecera /* aggregate the system setting and interface setting */ 36750aef78aaSIvan Vecera int _keep_addr = net->ipv6.devconf_all->keep_addr_on_down; 36760aef78aaSIvan Vecera 36770aef78aaSIvan Vecera if (!_keep_addr) 36780aef78aaSIvan Vecera _keep_addr = idev->cnf.keep_addr_on_down; 36790aef78aaSIvan Vecera 36800aef78aaSIvan Vecera keep_addr = (_keep_addr > 0); 36810aef78aaSIvan Vecera } 3682f1705ec1SDavid Ahern 368373a8bd74SDavid S. Miller /* Step 2: clear hash table */ 368473a8bd74SDavid S. Miller for (i = 0; i < IN6_ADDR_HSIZE; i++) { 368573a8bd74SDavid S. Miller struct hlist_head *h = &inet6_addr_lst[i]; 368673a8bd74SDavid S. Miller 368773a8bd74SDavid S. Miller spin_lock_bh(&addrconf_hash_lock); 368873a8bd74SDavid S. Miller restart: 3689b67bfe0dSSasha Levin hlist_for_each_entry_rcu(ifa, h, addr_lst) { 369073a8bd74SDavid S. Miller if (ifa->idev == idev) { 3691c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifa); 3692f1705ec1SDavid Ahern /* combined flag + permanent flag decide if 3693f1705ec1SDavid Ahern * address is retained on a down event 3694f1705ec1SDavid Ahern */ 3695f1705ec1SDavid Ahern if (!keep_addr || 369670af921dSDavid Ahern !(ifa->flags & IFA_F_PERMANENT) || 369770af921dSDavid Ahern addr_is_local(&ifa->addr)) { 3698f1705ec1SDavid Ahern hlist_del_init_rcu(&ifa->addr_lst); 369973a8bd74SDavid S. Miller goto restart; 370073a8bd74SDavid S. Miller } 370173a8bd74SDavid S. Miller } 3702f1705ec1SDavid Ahern } 370373a8bd74SDavid S. Miller spin_unlock_bh(&addrconf_hash_lock); 370473a8bd74SDavid S. Miller } 370573a8bd74SDavid S. Miller 37061da177e4SLinus Torvalds write_lock_bh(&idev->lock); 37071da177e4SLinus Torvalds 3708b7b1bfceSHannes Frederic Sowa addrconf_del_rs_timer(idev); 3709b7b1bfceSHannes Frederic Sowa 3710bcdd553fSStephen Hemminger /* Step 2: clear flags for stateless addrconf */ 3711439e2385SDenis V. Lunev if (!how) 37123c21edbdSYOSHIFUJI Hideaki idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY); 37131da177e4SLinus Torvalds 3714bcdd553fSStephen Hemminger /* Step 3: clear tempaddr list */ 3715372e6c8fSstephen hemminger while (!list_empty(&idev->tempaddr_list)) { 3716372e6c8fSstephen hemminger ifa = list_first_entry(&idev->tempaddr_list, 3717372e6c8fSstephen hemminger struct inet6_ifaddr, tmp_list); 3718372e6c8fSstephen hemminger list_del(&ifa->tmp_list); 37191da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 37201da177e4SLinus Torvalds spin_lock_bh(&ifa->lock); 37211da177e4SLinus Torvalds 37221da177e4SLinus Torvalds if (ifa->ifpub) { 37231da177e4SLinus Torvalds in6_ifa_put(ifa->ifpub); 37241da177e4SLinus Torvalds ifa->ifpub = NULL; 37251da177e4SLinus Torvalds } 37261da177e4SLinus Torvalds spin_unlock_bh(&ifa->lock); 37271da177e4SLinus Torvalds in6_ifa_put(ifa); 37281da177e4SLinus Torvalds write_lock_bh(&idev->lock); 37291da177e4SLinus Torvalds } 37308f37ada5Sstephen hemminger 3731f1705ec1SDavid Ahern list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) { 37328d1c802bSDavid Ahern struct fib6_info *rt = NULL; 3733a2d6cbb0SRabin Vincent bool keep; 373438bd10c4SDavid Ahern 3735c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifa); 373684e8b803Sstephen hemminger 3737a2d6cbb0SRabin Vincent keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) && 3738a2d6cbb0SRabin Vincent !addr_is_local(&ifa->addr); 3739a2d6cbb0SRabin Vincent 37404c5ff6a6SHerbert Xu write_unlock_bh(&idev->lock); 37418e8e676dSHannes Frederic Sowa spin_lock_bh(&ifa->lock); 3742f1705ec1SDavid Ahern 3743a2d6cbb0SRabin Vincent if (keep) { 3744f1705ec1SDavid Ahern /* set state to skip the notifier below */ 3745f1705ec1SDavid Ahern state = INET6_IFADDR_STATE_DEAD; 37466d717134SDavid Ahern ifa->state = INET6_IFADDR_STATE_PREDAD; 3747f1705ec1SDavid Ahern if (!(ifa->flags & IFA_F_NODAD)) 3748f1705ec1SDavid Ahern ifa->flags |= IFA_F_TENTATIVE; 374938bd10c4SDavid Ahern 375038bd10c4SDavid Ahern rt = ifa->rt; 375138bd10c4SDavid Ahern ifa->rt = NULL; 3752f1705ec1SDavid Ahern } else { 37534c5ff6a6SHerbert Xu state = ifa->state; 37544c5ff6a6SHerbert Xu ifa->state = INET6_IFADDR_STATE_DEAD; 3755f1705ec1SDavid Ahern } 3756f1705ec1SDavid Ahern 37578e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifa->lock); 37584c5ff6a6SHerbert Xu 375938bd10c4SDavid Ahern if (rt) 3760afb1d4b5SDavid Ahern ip6_del_rt(net, rt); 376138bd10c4SDavid Ahern 37629d82ca98SJohn Fastabend if (state != INET6_IFADDR_STATE_DEAD) { 37631da177e4SLinus Torvalds __ipv6_ifa_notify(RTM_DELADDR, ifa); 3764f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_DOWN, ifa); 3765ea06f717SMike Manning } else { 3766ea06f717SMike Manning if (idev->cnf.forwarding) 3767ea06f717SMike Manning addrconf_leave_anycast(ifa); 3768ea06f717SMike Manning addrconf_leave_solict(ifa->idev, &ifa->addr); 37692de79570SLorenzo Colitti } 377073a8bd74SDavid S. Miller 37711da177e4SLinus Torvalds write_lock_bh(&idev->lock); 37728ef802aaSEric Dumazet if (!keep) { 37738ef802aaSEric Dumazet list_del_rcu(&ifa->if_list); 37748ef802aaSEric Dumazet in6_ifa_put(ifa); 37758ef802aaSEric Dumazet } 37761da177e4SLinus Torvalds } 37778f37ada5Sstephen hemminger 37781da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 37791da177e4SLinus Torvalds 3780381f4dcaSSabrina Dubroca /* Step 5: Discard anycast and multicast list */ 3781381f4dcaSSabrina Dubroca if (how) { 3782381f4dcaSSabrina Dubroca ipv6_ac_destroy_dev(idev); 37831da177e4SLinus Torvalds ipv6_mc_destroy_dev(idev); 3784381f4dcaSSabrina Dubroca } else { 37851da177e4SLinus Torvalds ipv6_mc_down(idev); 3786381f4dcaSSabrina Dubroca } 37871da177e4SLinus Torvalds 37881da177e4SLinus Torvalds idev->tstamp = jiffies; 37891da177e4SLinus Torvalds 3790bcdd553fSStephen Hemminger /* Last: Shot the device (if unregistered) */ 3791439e2385SDenis V. Lunev if (how) { 3792408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 37931da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, idev->nd_parms); 37941da177e4SLinus Torvalds neigh_ifdown(&nd_tbl, dev); 37951da177e4SLinus Torvalds in6_dev_put(idev); 37961da177e4SLinus Torvalds } 37971da177e4SLinus Torvalds return 0; 37981da177e4SLinus Torvalds } 37991da177e4SLinus Torvalds 3800e99e88a9SKees Cook static void addrconf_rs_timer(struct timer_list *t) 38011da177e4SLinus Torvalds { 3802e99e88a9SKees Cook struct inet6_dev *idev = from_timer(idev, t, rs_timer); 3803caf92bc4SCong Wang struct net_device *dev = idev->dev; 3804b7b1bfceSHannes Frederic Sowa struct in6_addr lladdr; 38051da177e4SLinus Torvalds 3806b7b1bfceSHannes Frederic Sowa write_lock(&idev->lock); 38075b2a1953Sstephen hemminger if (idev->dead || !(idev->if_flags & IF_READY)) 38081da177e4SLinus Torvalds goto out; 38091da177e4SLinus Torvalds 38109ba2add3SShmulik Ladkani if (!ipv6_accept_ra(idev)) 38111da177e4SLinus Torvalds goto out; 38125b2a1953Sstephen hemminger 38135b2a1953Sstephen hemminger /* Announcement received after solicitation was sent */ 38145b2a1953Sstephen hemminger if (idev->if_flags & IF_RA_RCVD) 38155b2a1953Sstephen hemminger goto out; 38161da177e4SLinus Torvalds 3817bd11f074SMaciej Żenczykowski if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) { 3818caf92bc4SCong Wang write_unlock(&idev->lock); 3819caf92bc4SCong Wang if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE)) 3820caf92bc4SCong Wang ndisc_send_rs(dev, &lladdr, 3821b7b1bfceSHannes Frederic Sowa &in6addr_linklocal_allrouters); 3822b7b1bfceSHannes Frederic Sowa else 3823caf92bc4SCong Wang goto put; 3824b7b1bfceSHannes Frederic Sowa 3825caf92bc4SCong Wang write_lock(&idev->lock); 3826bd11f074SMaciej Żenczykowski idev->rs_interval = rfc3315_s14_backoff_update( 3827bd11f074SMaciej Żenczykowski idev->rs_interval, idev->cnf.rtr_solicit_max_interval); 38281da177e4SLinus Torvalds /* The wait after the last probe can be shorter */ 3829b7b1bfceSHannes Frederic Sowa addrconf_mod_rs_timer(idev, (idev->rs_probes == 3830b7b1bfceSHannes Frederic Sowa idev->cnf.rtr_solicits) ? 38315b2a1953Sstephen hemminger idev->cnf.rtr_solicit_delay : 3832bd11f074SMaciej Żenczykowski idev->rs_interval); 38331da177e4SLinus Torvalds } else { 38341da177e4SLinus Torvalds /* 38351da177e4SLinus Torvalds * Note: we do not support deprecated "all on-link" 38361da177e4SLinus Torvalds * assumption any longer. 38371da177e4SLinus Torvalds */ 383891df42beSJoe Perches pr_debug("%s: no IPv6 routers present\n", idev->dev->name); 38391da177e4SLinus Torvalds } 38401da177e4SLinus Torvalds 38411da177e4SLinus Torvalds out: 3842b7b1bfceSHannes Frederic Sowa write_unlock(&idev->lock); 3843caf92bc4SCong Wang put: 3844b7b1bfceSHannes Frederic Sowa in6_dev_put(idev); 38451da177e4SLinus Torvalds } 38461da177e4SLinus Torvalds 38471da177e4SLinus Torvalds /* 38481da177e4SLinus Torvalds * Duplicate Address Detection 38491da177e4SLinus Torvalds */ 38503c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp) 38513c21edbdSYOSHIFUJI Hideaki { 38523c21edbdSYOSHIFUJI Hideaki unsigned long rand_num; 38533c21edbdSYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev; 3854adc176c5SErik Nordmark u64 nonce; 38553c21edbdSYOSHIFUJI Hideaki 385695c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 385795c385b4SNeil Horman rand_num = 0; 385895c385b4SNeil Horman else 385963862b5bSAruna-Hewapathirane rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1); 386095c385b4SNeil Horman 3861adc176c5SErik Nordmark nonce = 0; 3862adc176c5SErik Nordmark if (idev->cnf.enhanced_dad || 3863adc176c5SErik Nordmark dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad) { 3864adc176c5SErik Nordmark do 3865adc176c5SErik Nordmark get_random_bytes(&nonce, 6); 3866adc176c5SErik Nordmark while (nonce == 0); 3867adc176c5SErik Nordmark } 3868adc176c5SErik Nordmark ifp->dad_nonce = nonce; 3869b7b1bfceSHannes Frederic Sowa ifp->dad_probes = idev->cnf.dad_transmits; 3870c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, rand_num); 38713c21edbdSYOSHIFUJI Hideaki } 38723c21edbdSYOSHIFUJI Hideaki 3873c15b1ccaSHannes Frederic Sowa static void addrconf_dad_begin(struct inet6_ifaddr *ifp) 38741da177e4SLinus Torvalds { 38751da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 38761da177e4SLinus Torvalds struct net_device *dev = idev->dev; 3877764d3be6SPaolo Abeni bool bump_id, notify = false; 3878afb1d4b5SDavid Ahern struct net *net; 38791da177e4SLinus Torvalds 38801da177e4SLinus Torvalds addrconf_join_solict(dev, &ifp->addr); 38811da177e4SLinus Torvalds 388263862b5bSAruna-Hewapathirane prandom_seed((__force u32) ifp->addr.s6_addr32[3]); 38831da177e4SLinus Torvalds 38841da177e4SLinus Torvalds read_lock_bh(&idev->lock); 3885622ccdf1SHerbert Xu spin_lock(&ifp->lock); 3886e9d3e084SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DEAD) 38871da177e4SLinus Torvalds goto out; 38881da177e4SLinus Torvalds 3889afb1d4b5SDavid Ahern net = dev_net(dev); 38901da177e4SLinus Torvalds if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) || 3891afb1d4b5SDavid Ahern (net->ipv6.devconf_all->accept_dad < 1 && 3892a2d3f3e3SMatteo Croce idev->cnf.accept_dad < 1) || 389355ebaef1SNoriaki TAKAMIYA !(ifp->flags&IFA_F_TENTATIVE) || 389455ebaef1SNoriaki TAKAMIYA ifp->flags & IFA_F_NODAD) { 3895c76fe2d9SDavid Ahern bool send_na = false; 3896c76fe2d9SDavid Ahern 3897c76fe2d9SDavid Ahern if (ifp->flags & IFA_F_TENTATIVE && 3898c76fe2d9SDavid Ahern !(ifp->flags & IFA_F_OPTIMISTIC)) 3899c76fe2d9SDavid Ahern send_na = true; 3900764d3be6SPaolo Abeni bump_id = ifp->flags & IFA_F_TENTATIVE; 3901cc411d0bSBrian Haley ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 390221809fafSstephen hemminger spin_unlock(&ifp->lock); 39031da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 39041da177e4SLinus Torvalds 3905c76fe2d9SDavid Ahern addrconf_dad_completed(ifp, bump_id, send_na); 39061da177e4SLinus Torvalds return; 39071da177e4SLinus Torvalds } 39081da177e4SLinus Torvalds 39096732badeSYOSHIFUJI Hideaki if (!(idev->if_flags & IF_READY)) { 391021809fafSstephen hemminger spin_unlock(&ifp->lock); 39116732badeSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 39123c21edbdSYOSHIFUJI Hideaki /* 3913590a9887SMasatake YAMATO * If the device is not ready: 39143c21edbdSYOSHIFUJI Hideaki * - keep it tentative if it is a permanent address. 39153c21edbdSYOSHIFUJI Hideaki * - otherwise, kill it. 39163c21edbdSYOSHIFUJI Hideaki */ 39173c21edbdSYOSHIFUJI Hideaki in6_ifa_hold(ifp); 3918cc411d0bSBrian Haley addrconf_dad_stop(ifp, 0); 39196732badeSYOSHIFUJI Hideaki return; 39203c21edbdSYOSHIFUJI Hideaki } 392195c385b4SNeil Horman 392295c385b4SNeil Horman /* 392395c385b4SNeil Horman * Optimistic nodes can start receiving 392495c385b4SNeil Horman * Frames right away 392595c385b4SNeil Horman */ 39267fd2561eSErik Kline if (ifp->flags & IFA_F_OPTIMISTIC) { 3927afb1d4b5SDavid Ahern ip6_ins_rt(net, ifp->rt); 3928afb1d4b5SDavid Ahern if (ipv6_use_optimistic_addr(net, idev)) { 39297fd2561eSErik Kline /* Because optimistic nodes can use this address, 39307fd2561eSErik Kline * notify listeners. If DAD fails, RTM_DELADDR is sent. 39317fd2561eSErik Kline */ 393216186a82Ssubashab@codeaurora.org notify = true; 39337fd2561eSErik Kline } 39347fd2561eSErik Kline } 393595c385b4SNeil Horman 39366732badeSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 39371da177e4SLinus Torvalds out: 3938622ccdf1SHerbert Xu spin_unlock(&ifp->lock); 39391da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 394016186a82Ssubashab@codeaurora.org if (notify) 394116186a82Ssubashab@codeaurora.org ipv6_ifa_notify(RTM_NEWADDR, ifp); 39421da177e4SLinus Torvalds } 39431da177e4SLinus Torvalds 3944c15b1ccaSHannes Frederic Sowa static void addrconf_dad_start(struct inet6_ifaddr *ifp) 39451da177e4SLinus Torvalds { 3946c15b1ccaSHannes Frederic Sowa bool begin_dad = false; 3947c15b1ccaSHannes Frederic Sowa 39488e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 3949c15b1ccaSHannes Frederic Sowa if (ifp->state != INET6_IFADDR_STATE_DEAD) { 3950c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_PREDAD; 3951c15b1ccaSHannes Frederic Sowa begin_dad = true; 3952c15b1ccaSHannes Frederic Sowa } 39538e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 3954c15b1ccaSHannes Frederic Sowa 3955c15b1ccaSHannes Frederic Sowa if (begin_dad) 3956c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 0); 3957c15b1ccaSHannes Frederic Sowa } 3958c15b1ccaSHannes Frederic Sowa 3959c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w) 3960c15b1ccaSHannes Frederic Sowa { 3961c15b1ccaSHannes Frederic Sowa struct inet6_ifaddr *ifp = container_of(to_delayed_work(w), 3962c15b1ccaSHannes Frederic Sowa struct inet6_ifaddr, 3963c15b1ccaSHannes Frederic Sowa dad_work); 39641da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 3965764d3be6SPaolo Abeni bool bump_id, disable_ipv6 = false; 39661da177e4SLinus Torvalds struct in6_addr mcaddr; 39671da177e4SLinus Torvalds 3968c15b1ccaSHannes Frederic Sowa enum { 3969c15b1ccaSHannes Frederic Sowa DAD_PROCESS, 3970c15b1ccaSHannes Frederic Sowa DAD_BEGIN, 3971c15b1ccaSHannes Frederic Sowa DAD_ABORT, 3972c15b1ccaSHannes Frederic Sowa } action = DAD_PROCESS; 3973c15b1ccaSHannes Frederic Sowa 3974c15b1ccaSHannes Frederic Sowa rtnl_lock(); 3975c15b1ccaSHannes Frederic Sowa 39768e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 3977c15b1ccaSHannes Frederic Sowa if (ifp->state == INET6_IFADDR_STATE_PREDAD) { 3978c15b1ccaSHannes Frederic Sowa action = DAD_BEGIN; 3979c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_DAD; 3980c15b1ccaSHannes Frederic Sowa } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) { 3981c15b1ccaSHannes Frederic Sowa action = DAD_ABORT; 3982c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_POSTDAD; 398385b51b12SMike Manning 398435e015e1SMatteo Croce if ((dev_net(idev->dev)->ipv6.devconf_all->accept_dad > 1 || 398535e015e1SMatteo Croce idev->cnf.accept_dad > 1) && 398635e015e1SMatteo Croce !idev->cnf.disable_ipv6 && 398785b51b12SMike Manning !(ifp->flags & IFA_F_STABLE_PRIVACY)) { 398885b51b12SMike Manning struct in6_addr addr; 398985b51b12SMike Manning 399085b51b12SMike Manning addr.s6_addr32[0] = htonl(0xfe800000); 399185b51b12SMike Manning addr.s6_addr32[1] = 0; 399285b51b12SMike Manning 399385b51b12SMike Manning if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) && 399485b51b12SMike Manning ipv6_addr_equal(&ifp->addr, &addr)) { 399585b51b12SMike Manning /* DAD failed for link-local based on MAC */ 399685b51b12SMike Manning idev->cnf.disable_ipv6 = 1; 399785b51b12SMike Manning 399885b51b12SMike Manning pr_info("%s: IPv6 being disabled!\n", 399985b51b12SMike Manning ifp->idev->dev->name); 400085b51b12SMike Manning disable_ipv6 = true; 400185b51b12SMike Manning } 400285b51b12SMike Manning } 4003c15b1ccaSHannes Frederic Sowa } 40048e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 4005c15b1ccaSHannes Frederic Sowa 4006c15b1ccaSHannes Frederic Sowa if (action == DAD_BEGIN) { 4007c15b1ccaSHannes Frederic Sowa addrconf_dad_begin(ifp); 4008c15b1ccaSHannes Frederic Sowa goto out; 4009c15b1ccaSHannes Frederic Sowa } else if (action == DAD_ABORT) { 4010751eb6b6SWei Yongjun in6_ifa_hold(ifp); 4011c15b1ccaSHannes Frederic Sowa addrconf_dad_stop(ifp, 1); 401285b51b12SMike Manning if (disable_ipv6) 401385b51b12SMike Manning addrconf_ifdown(idev->dev, 0); 4014c15b1ccaSHannes Frederic Sowa goto out; 4015c15b1ccaSHannes Frederic Sowa } 4016c15b1ccaSHannes Frederic Sowa 4017b7b1bfceSHannes Frederic Sowa if (!ifp->dad_probes && addrconf_dad_end(ifp)) 4018f2344a13SHerbert Xu goto out; 4019f2344a13SHerbert Xu 4020c15b1ccaSHannes Frederic Sowa write_lock_bh(&idev->lock); 4021122e4519Sstephen hemminger if (idev->dead || !(idev->if_flags & IF_READY)) { 4022c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 40231da177e4SLinus Torvalds goto out; 40241da177e4SLinus Torvalds } 402521809fafSstephen hemminger 402621809fafSstephen hemminger spin_lock(&ifp->lock); 4027622ccdf1SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DEAD) { 4028622ccdf1SHerbert Xu spin_unlock(&ifp->lock); 4029c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 4030622ccdf1SHerbert Xu goto out; 4031622ccdf1SHerbert Xu } 4032622ccdf1SHerbert Xu 4033b7b1bfceSHannes Frederic Sowa if (ifp->dad_probes == 0) { 4034c76fe2d9SDavid Ahern bool send_na = false; 4035c76fe2d9SDavid Ahern 40361da177e4SLinus Torvalds /* 40371da177e4SLinus Torvalds * DAD was successful 40381da177e4SLinus Torvalds */ 40391da177e4SLinus Torvalds 4040c76fe2d9SDavid Ahern if (ifp->flags & IFA_F_TENTATIVE && 4041c76fe2d9SDavid Ahern !(ifp->flags & IFA_F_OPTIMISTIC)) 4042c76fe2d9SDavid Ahern send_na = true; 4043764d3be6SPaolo Abeni bump_id = ifp->flags & IFA_F_TENTATIVE; 4044cc411d0bSBrian Haley ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 404521809fafSstephen hemminger spin_unlock(&ifp->lock); 4046c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 40471da177e4SLinus Torvalds 4048c76fe2d9SDavid Ahern addrconf_dad_completed(ifp, bump_id, send_na); 40491da177e4SLinus Torvalds 40501da177e4SLinus Torvalds goto out; 40511da177e4SLinus Torvalds } 40521da177e4SLinus Torvalds 4053b7b1bfceSHannes Frederic Sowa ifp->dad_probes--; 4054c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 40551f9248e5SJiri Pirko NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME)); 405621809fafSstephen hemminger spin_unlock(&ifp->lock); 4057c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 40581da177e4SLinus Torvalds 40591da177e4SLinus Torvalds /* send a neighbour solicitation for our addr */ 40601da177e4SLinus Torvalds addrconf_addr_solict_mult(&ifp->addr, &mcaddr); 4061adc176c5SErik Nordmark ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any, 4062adc176c5SErik Nordmark ifp->dad_nonce); 40631da177e4SLinus Torvalds out: 40641da177e4SLinus Torvalds in6_ifa_put(ifp); 4065c15b1ccaSHannes Frederic Sowa rtnl_unlock(); 40661da177e4SLinus Torvalds } 40671da177e4SLinus Torvalds 406811ffff75SHannes Frederic Sowa /* ifp->idev must be at least read locked */ 406911ffff75SHannes Frederic Sowa static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp) 407011ffff75SHannes Frederic Sowa { 407111ffff75SHannes Frederic Sowa struct inet6_ifaddr *ifpiter; 407211ffff75SHannes Frederic Sowa struct inet6_dev *idev = ifp->idev; 407311ffff75SHannes Frederic Sowa 4074602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) { 4075602582caSHannes Frederic Sowa if (ifpiter->scope > IFA_LINK) 4076602582caSHannes Frederic Sowa break; 407711ffff75SHannes Frederic Sowa if (ifp != ifpiter && ifpiter->scope == IFA_LINK && 407811ffff75SHannes Frederic Sowa (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE| 407911ffff75SHannes Frederic Sowa IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) == 408011ffff75SHannes Frederic Sowa IFA_F_PERMANENT) 408111ffff75SHannes Frederic Sowa return false; 408211ffff75SHannes Frederic Sowa } 408311ffff75SHannes Frederic Sowa return true; 408411ffff75SHannes Frederic Sowa } 408511ffff75SHannes Frederic Sowa 4086c76fe2d9SDavid Ahern static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id, 4087c76fe2d9SDavid Ahern bool send_na) 40881da177e4SLinus Torvalds { 40891da177e4SLinus Torvalds struct net_device *dev = ifp->idev->dev; 4090b7b1bfceSHannes Frederic Sowa struct in6_addr lladdr; 4091b173ee48SHannes Frederic Sowa bool send_rs, send_mld; 4092b7b1bfceSHannes Frederic Sowa 4093c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifp); 40941da177e4SLinus Torvalds 40951da177e4SLinus Torvalds /* 40961da177e4SLinus Torvalds * Configure the address for reception. Now it is valid. 40971da177e4SLinus Torvalds */ 40981da177e4SLinus Torvalds 40991da177e4SLinus Torvalds ipv6_ifa_notify(RTM_NEWADDR, ifp); 41001da177e4SLinus Torvalds 4101026359bcSTore Anderson /* If added prefix is link local and we are prepared to process 4102026359bcSTore Anderson router advertisements, start sending router solicitations. 41031da177e4SLinus Torvalds */ 41041da177e4SLinus Torvalds 41051ec047ebSHannes Frederic Sowa read_lock_bh(&ifp->idev->lock); 410611ffff75SHannes Frederic Sowa send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp); 4107b173ee48SHannes Frederic Sowa send_rs = send_mld && 4108b173ee48SHannes Frederic Sowa ipv6_accept_ra(ifp->idev) && 4109bd11f074SMaciej Żenczykowski ifp->idev->cnf.rtr_solicits != 0 && 4110b173ee48SHannes Frederic Sowa (dev->flags&IFF_LOOPBACK) == 0; 41111ec047ebSHannes Frederic Sowa read_unlock_bh(&ifp->idev->lock); 41121ec047ebSHannes Frederic Sowa 4113b173ee48SHannes Frederic Sowa /* While dad is in progress mld report's source address is in6_addrany. 4114b173ee48SHannes Frederic Sowa * Resend with proper ll now. 4115b173ee48SHannes Frederic Sowa */ 4116b173ee48SHannes Frederic Sowa if (send_mld) 4117b173ee48SHannes Frederic Sowa ipv6_mc_dad_complete(ifp->idev); 4118b173ee48SHannes Frederic Sowa 4119c76fe2d9SDavid Ahern /* send unsolicited NA if enabled */ 4120c76fe2d9SDavid Ahern if (send_na && 4121c76fe2d9SDavid Ahern (ifp->idev->cnf.ndisc_notify || 4122c76fe2d9SDavid Ahern dev_net(dev)->ipv6.devconf_all->ndisc_notify)) { 4123c76fe2d9SDavid Ahern ndisc_send_na(dev, &in6addr_linklocal_allnodes, &ifp->addr, 4124c76fe2d9SDavid Ahern /*router=*/ !!ifp->idev->cnf.forwarding, 4125c76fe2d9SDavid Ahern /*solicited=*/ false, /*override=*/ true, 4126c76fe2d9SDavid Ahern /*inc_opt=*/ true); 4127c76fe2d9SDavid Ahern } 4128c76fe2d9SDavid Ahern 41291ec047ebSHannes Frederic Sowa if (send_rs) { 41301da177e4SLinus Torvalds /* 41311da177e4SLinus Torvalds * If a host as already performed a random delay 41321da177e4SLinus Torvalds * [...] as part of DAD [...] there is no need 41331da177e4SLinus Torvalds * to delay again before sending the first RS 41341da177e4SLinus Torvalds */ 41351ec047ebSHannes Frederic Sowa if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE)) 4136b7b1bfceSHannes Frederic Sowa return; 41371ec047ebSHannes Frederic Sowa ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters); 41381da177e4SLinus Torvalds 4139b7b1bfceSHannes Frederic Sowa write_lock_bh(&ifp->idev->lock); 4140b7b1bfceSHannes Frederic Sowa spin_lock(&ifp->lock); 4141bd11f074SMaciej Żenczykowski ifp->idev->rs_interval = rfc3315_s14_backoff_init( 4142bd11f074SMaciej Żenczykowski ifp->idev->cnf.rtr_solicit_interval); 4143b7b1bfceSHannes Frederic Sowa ifp->idev->rs_probes = 1; 41441da177e4SLinus Torvalds ifp->idev->if_flags |= IF_RS_SENT; 4145bd11f074SMaciej Żenczykowski addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval); 4146b7b1bfceSHannes Frederic Sowa spin_unlock(&ifp->lock); 4147b7b1bfceSHannes Frederic Sowa write_unlock_bh(&ifp->idev->lock); 41481da177e4SLinus Torvalds } 4149764d3be6SPaolo Abeni 4150764d3be6SPaolo Abeni if (bump_id) 4151764d3be6SPaolo Abeni rt_genid_bump_ipv6(dev_net(dev)); 4152a11a7f71SMarcus Huewe 4153a11a7f71SMarcus Huewe /* Make sure that a new temporary address will be created 4154a11a7f71SMarcus Huewe * before this temporary address becomes deprecated. 4155a11a7f71SMarcus Huewe */ 4156a11a7f71SMarcus Huewe if (ifp->flags & IFA_F_TEMPORARY) 4157a11a7f71SMarcus Huewe addrconf_verify_rtnl(); 41581da177e4SLinus Torvalds } 41591da177e4SLinus Torvalds 4160e21e8467SStephen Hemminger static void addrconf_dad_run(struct inet6_dev *idev) 4161e21e8467SStephen Hemminger { 4162c5e33bddSYOSHIFUJI Hideaki struct inet6_ifaddr *ifp; 4163c5e33bddSYOSHIFUJI Hideaki 4164c5e33bddSYOSHIFUJI Hideaki read_lock_bh(&idev->lock); 4165502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 416621809fafSstephen hemminger spin_lock(&ifp->lock); 4167f2344a13SHerbert Xu if (ifp->flags & IFA_F_TENTATIVE && 4168f2344a13SHerbert Xu ifp->state == INET6_IFADDR_STATE_DAD) 4169c5e33bddSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 4170f2344a13SHerbert Xu spin_unlock(&ifp->lock); 4171c5e33bddSYOSHIFUJI Hideaki } 4172c5e33bddSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 4173c5e33bddSYOSHIFUJI Hideaki } 4174c5e33bddSYOSHIFUJI Hideaki 41751da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 41761da177e4SLinus Torvalds struct if6_iter_state { 41773c40090aSDaniel Lezcano struct seq_net_private p; 41781da177e4SLinus Torvalds int bucket; 41791d578303SMihai Maruseac int offset; 41801da177e4SLinus Torvalds }; 41811da177e4SLinus Torvalds 41821d578303SMihai Maruseac static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos) 41831da177e4SLinus Torvalds { 41841da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 41851218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 4186a5c1d98fSEric Dumazet struct inet6_ifaddr *ifa = NULL; 41871d578303SMihai Maruseac int p = 0; 41881da177e4SLinus Torvalds 41891d578303SMihai Maruseac /* initial bucket if pos is 0 */ 41901d578303SMihai Maruseac if (pos == 0) { 41911d578303SMihai Maruseac state->bucket = 0; 41921d578303SMihai Maruseac state->offset = 0; 41931d578303SMihai Maruseac } 41941d578303SMihai Maruseac 41951d578303SMihai Maruseac for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) { 4196a5c1d98fSEric Dumazet hlist_for_each_entry_rcu(ifa, &inet6_addr_lst[state->bucket], 41971d578303SMihai Maruseac addr_lst) { 41989f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 41999f0d3c27SEric Dumazet continue; 42001d578303SMihai Maruseac /* sync with offset */ 42011d578303SMihai Maruseac if (p < state->offset) { 42021d578303SMihai Maruseac p++; 42031d578303SMihai Maruseac continue; 42041d578303SMihai Maruseac } 42051d578303SMihai Maruseac state->offset++; 42061da177e4SLinus Torvalds return ifa; 42071da177e4SLinus Torvalds } 42081d578303SMihai Maruseac 42091d578303SMihai Maruseac /* prepare for next bucket */ 42101d578303SMihai Maruseac state->offset = 0; 42111d578303SMihai Maruseac p = 0; 42121d578303SMihai Maruseac } 4213c2e21293Sstephen hemminger return NULL; 4214c2e21293Sstephen hemminger } 42151da177e4SLinus Torvalds 4216c2e21293Sstephen hemminger static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, 4217c2e21293Sstephen hemminger struct inet6_ifaddr *ifa) 42181da177e4SLinus Torvalds { 42191da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 42201218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 42211da177e4SLinus Torvalds 4222a5c1d98fSEric Dumazet hlist_for_each_entry_continue_rcu(ifa, addr_lst) { 42239f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 42249f0d3c27SEric Dumazet continue; 42251d578303SMihai Maruseac state->offset++; 42261da177e4SLinus Torvalds return ifa; 42271d578303SMihai Maruseac } 42281da177e4SLinus Torvalds 4229c2e21293Sstephen hemminger while (++state->bucket < IN6_ADDR_HSIZE) { 42301d578303SMihai Maruseac state->offset = 0; 4231a5c1d98fSEric Dumazet hlist_for_each_entry_rcu(ifa, 4232c2e21293Sstephen hemminger &inet6_addr_lst[state->bucket], addr_lst) { 42339f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 42349f0d3c27SEric Dumazet continue; 42351d578303SMihai Maruseac state->offset++; 4236c2e21293Sstephen hemminger return ifa; 4237c2e21293Sstephen hemminger } 4238c2e21293Sstephen hemminger } 4239c2e21293Sstephen hemminger 4240c2e21293Sstephen hemminger return NULL; 4241c2e21293Sstephen hemminger } 4242c2e21293Sstephen hemminger 42431da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos) 4244a5c1d98fSEric Dumazet __acquires(rcu) 42451da177e4SLinus Torvalds { 4246a5c1d98fSEric Dumazet rcu_read_lock(); 42471d578303SMihai Maruseac return if6_get_first(seq, *pos); 42481da177e4SLinus Torvalds } 42491da177e4SLinus Torvalds 42501da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos) 42511da177e4SLinus Torvalds { 42521da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 42531da177e4SLinus Torvalds 42541da177e4SLinus Torvalds ifa = if6_get_next(seq, v); 42551da177e4SLinus Torvalds ++*pos; 42561da177e4SLinus Torvalds return ifa; 42571da177e4SLinus Torvalds } 42581da177e4SLinus Torvalds 42591da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v) 4260a5c1d98fSEric Dumazet __releases(rcu) 42611da177e4SLinus Torvalds { 4262a5c1d98fSEric Dumazet rcu_read_unlock(); 42631da177e4SLinus Torvalds } 42641da177e4SLinus Torvalds 42651da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v) 42661da177e4SLinus Torvalds { 42671da177e4SLinus Torvalds struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v; 4268971a351cSJiri Pirko seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n", 4269b071195dSHarvey Harrison &ifp->addr, 42701da177e4SLinus Torvalds ifp->idev->dev->ifindex, 42711da177e4SLinus Torvalds ifp->prefix_len, 42721da177e4SLinus Torvalds ifp->scope, 4273971a351cSJiri Pirko (u8) ifp->flags, 42741da177e4SLinus Torvalds ifp->idev->dev->name); 42751da177e4SLinus Torvalds return 0; 42761da177e4SLinus Torvalds } 42771da177e4SLinus Torvalds 427856b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = { 42791da177e4SLinus Torvalds .start = if6_seq_start, 42801da177e4SLinus Torvalds .next = if6_seq_next, 42811da177e4SLinus Torvalds .show = if6_seq_show, 42821da177e4SLinus Torvalds .stop = if6_seq_stop, 42831da177e4SLinus Torvalds }; 42841da177e4SLinus Torvalds 42851da177e4SLinus Torvalds static int if6_seq_open(struct inode *inode, struct file *file) 42861da177e4SLinus Torvalds { 42873c40090aSDaniel Lezcano return seq_open_net(inode, file, &if6_seq_ops, 4288cf7732e4SPavel Emelyanov sizeof(struct if6_iter_state)); 42891da177e4SLinus Torvalds } 42901da177e4SLinus Torvalds 42919a32144eSArjan van de Ven static const struct file_operations if6_fops = { 42921da177e4SLinus Torvalds .open = if6_seq_open, 42931da177e4SLinus Torvalds .read = seq_read, 42941da177e4SLinus Torvalds .llseek = seq_lseek, 42953c40090aSDaniel Lezcano .release = seq_release_net, 42961da177e4SLinus Torvalds }; 42971da177e4SLinus Torvalds 42982c8c1e72SAlexey Dobriyan static int __net_init if6_proc_net_init(struct net *net) 42991da177e4SLinus Torvalds { 4300d6444062SJoe Perches if (!proc_create("if_inet6", 0444, net->proc_net, &if6_fops)) 43011da177e4SLinus Torvalds return -ENOMEM; 43021da177e4SLinus Torvalds return 0; 43031da177e4SLinus Torvalds } 43041da177e4SLinus Torvalds 43052c8c1e72SAlexey Dobriyan static void __net_exit if6_proc_net_exit(struct net *net) 43063c40090aSDaniel Lezcano { 4307ece31ffdSGao feng remove_proc_entry("if_inet6", net->proc_net); 43083c40090aSDaniel Lezcano } 43093c40090aSDaniel Lezcano 43103c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = { 43113c40090aSDaniel Lezcano .init = if6_proc_net_init, 43123c40090aSDaniel Lezcano .exit = if6_proc_net_exit, 43133c40090aSDaniel Lezcano }; 43143c40090aSDaniel Lezcano 43153c40090aSDaniel Lezcano int __init if6_proc_init(void) 43163c40090aSDaniel Lezcano { 43173c40090aSDaniel Lezcano return register_pernet_subsys(&if6_proc_net_ops); 43183c40090aSDaniel Lezcano } 43193c40090aSDaniel Lezcano 43201da177e4SLinus Torvalds void if6_proc_exit(void) 43211da177e4SLinus Torvalds { 43223c40090aSDaniel Lezcano unregister_pernet_subsys(&if6_proc_net_ops); 43231da177e4SLinus Torvalds } 43241da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 43251da177e4SLinus Torvalds 432607a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6) 43273b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */ 4328b71d1d42SEric Dumazet int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr) 43293b9f9a1cSNoriaki TAKAMIYA { 43303f27fb23SEric Dumazet unsigned int hash = inet6_addr_hash(net, addr); 4331c2e21293Sstephen hemminger struct inet6_ifaddr *ifp = NULL; 43323f27fb23SEric Dumazet int ret = 0; 4333c2e21293Sstephen hemminger 43344e5f47abSEric Dumazet rcu_read_lock(); 43354e5f47abSEric Dumazet hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) { 4336878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 4337389f6612SDaniel Lezcano continue; 4338caad295fSYOSHIFUJI Hideaki if (ipv6_addr_equal(&ifp->addr, addr) && 43393b9f9a1cSNoriaki TAKAMIYA (ifp->flags & IFA_F_HOMEADDRESS)) { 43403b9f9a1cSNoriaki TAKAMIYA ret = 1; 43413b9f9a1cSNoriaki TAKAMIYA break; 43423b9f9a1cSNoriaki TAKAMIYA } 43433b9f9a1cSNoriaki TAKAMIYA } 43444e5f47abSEric Dumazet rcu_read_unlock(); 43453b9f9a1cSNoriaki TAKAMIYA return ret; 43463b9f9a1cSNoriaki TAKAMIYA } 43473b9f9a1cSNoriaki TAKAMIYA #endif 43483b9f9a1cSNoriaki TAKAMIYA 43491da177e4SLinus Torvalds /* 43501da177e4SLinus Torvalds * Periodic address status verification 43511da177e4SLinus Torvalds */ 43521da177e4SLinus Torvalds 4353c15b1ccaSHannes Frederic Sowa static void addrconf_verify_rtnl(void) 43541da177e4SLinus Torvalds { 4355b2db7564SYOSHIFUJI Hideaki unsigned long now, next, next_sec, next_sched; 43561da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 43571da177e4SLinus Torvalds int i; 43581da177e4SLinus Torvalds 4359c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 4360c15b1ccaSHannes Frederic Sowa 43615c578aedSstephen hemminger rcu_read_lock_bh(); 43621da177e4SLinus Torvalds now = jiffies; 4363b2db7564SYOSHIFUJI Hideaki next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY); 43641da177e4SLinus Torvalds 4365c15b1ccaSHannes Frederic Sowa cancel_delayed_work(&addr_chk_work); 43661da177e4SLinus Torvalds 43671da177e4SLinus Torvalds for (i = 0; i < IN6_ADDR_HSIZE; i++) { 43681da177e4SLinus Torvalds restart: 4369c15b1ccaSHannes Frederic Sowa hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) { 43701da177e4SLinus Torvalds unsigned long age; 43711da177e4SLinus Torvalds 4372fad8da3eSYasushi Asano /* When setting preferred_lft to a value not zero or 4373fad8da3eSYasushi Asano * infinity, while valid_lft is infinity 4374fad8da3eSYasushi Asano * IFA_F_PERMANENT has a non-infinity life time. 4375fad8da3eSYasushi Asano */ 4376fad8da3eSYasushi Asano if ((ifp->flags & IFA_F_PERMANENT) && 4377fad8da3eSYasushi Asano (ifp->prefered_lft == INFINITY_LIFE_TIME)) 43781da177e4SLinus Torvalds continue; 43791da177e4SLinus Torvalds 43801da177e4SLinus Torvalds spin_lock(&ifp->lock); 4381b2db7564SYOSHIFUJI Hideaki /* We try to batch several events at once. */ 4382b2db7564SYOSHIFUJI Hideaki age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; 43831da177e4SLinus Torvalds 43848f27ebb9SYOSHIFUJI Hideaki if (ifp->valid_lft != INFINITY_LIFE_TIME && 43858f27ebb9SYOSHIFUJI Hideaki age >= ifp->valid_lft) { 43861da177e4SLinus Torvalds spin_unlock(&ifp->lock); 43871da177e4SLinus Torvalds in6_ifa_hold(ifp); 43881da177e4SLinus Torvalds ipv6_del_addr(ifp); 43891da177e4SLinus Torvalds goto restart; 43908f27ebb9SYOSHIFUJI Hideaki } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) { 43918f27ebb9SYOSHIFUJI Hideaki spin_unlock(&ifp->lock); 43928f27ebb9SYOSHIFUJI Hideaki continue; 43931da177e4SLinus Torvalds } else if (age >= ifp->prefered_lft) { 4394a1ed0526SBrian Haley /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */ 43951da177e4SLinus Torvalds int deprecate = 0; 43961da177e4SLinus Torvalds 43971da177e4SLinus Torvalds if (!(ifp->flags&IFA_F_DEPRECATED)) { 43981da177e4SLinus Torvalds deprecate = 1; 43991da177e4SLinus Torvalds ifp->flags |= IFA_F_DEPRECATED; 44001da177e4SLinus Torvalds } 44011da177e4SLinus Torvalds 4402fad8da3eSYasushi Asano if ((ifp->valid_lft != INFINITY_LIFE_TIME) && 4403fad8da3eSYasushi Asano (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))) 44041da177e4SLinus Torvalds next = ifp->tstamp + ifp->valid_lft * HZ; 44051da177e4SLinus Torvalds 44061da177e4SLinus Torvalds spin_unlock(&ifp->lock); 44071da177e4SLinus Torvalds 44081da177e4SLinus Torvalds if (deprecate) { 44091da177e4SLinus Torvalds in6_ifa_hold(ifp); 44101da177e4SLinus Torvalds 44111da177e4SLinus Torvalds ipv6_ifa_notify(0, ifp); 44121da177e4SLinus Torvalds in6_ifa_put(ifp); 44131da177e4SLinus Torvalds goto restart; 44141da177e4SLinus Torvalds } 44151da177e4SLinus Torvalds } else if ((ifp->flags&IFA_F_TEMPORARY) && 44161da177e4SLinus Torvalds !(ifp->flags&IFA_F_TENTATIVE)) { 441788949cf4Sstephen hemminger unsigned long regen_advance = ifp->idev->cnf.regen_max_retry * 441888949cf4Sstephen hemminger ifp->idev->cnf.dad_transmits * 44191f9248e5SJiri Pirko NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ; 442088949cf4Sstephen hemminger 44211da177e4SLinus Torvalds if (age >= ifp->prefered_lft - regen_advance) { 44221da177e4SLinus Torvalds struct inet6_ifaddr *ifpub = ifp->ifpub; 44231da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 44241da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 44251da177e4SLinus Torvalds if (!ifp->regen_count && ifpub) { 44261da177e4SLinus Torvalds ifp->regen_count++; 44271da177e4SLinus Torvalds in6_ifa_hold(ifp); 44281da177e4SLinus Torvalds in6_ifa_hold(ifpub); 44291da177e4SLinus Torvalds spin_unlock(&ifp->lock); 44305c578aedSstephen hemminger 4431291d809bSHiroyuki YAMAMORI spin_lock(&ifpub->lock); 4432291d809bSHiroyuki YAMAMORI ifpub->regen_count = 0; 4433291d809bSHiroyuki YAMAMORI spin_unlock(&ifpub->lock); 4434e64e469bSEric Dumazet rcu_read_unlock_bh(); 4435fffcefe9SEric Dumazet ipv6_create_tempaddr(ifpub, ifp, true); 44361da177e4SLinus Torvalds in6_ifa_put(ifpub); 44371da177e4SLinus Torvalds in6_ifa_put(ifp); 4438e64e469bSEric Dumazet rcu_read_lock_bh(); 44391da177e4SLinus Torvalds goto restart; 44401da177e4SLinus Torvalds } 44411da177e4SLinus Torvalds } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next)) 44421da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ; 44431da177e4SLinus Torvalds spin_unlock(&ifp->lock); 44441da177e4SLinus Torvalds } else { 44451da177e4SLinus Torvalds /* ifp->prefered_lft <= ifp->valid_lft */ 44461da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 44471da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 44481da177e4SLinus Torvalds spin_unlock(&ifp->lock); 44491da177e4SLinus Torvalds } 44501da177e4SLinus Torvalds } 44511da177e4SLinus Torvalds } 44521da177e4SLinus Torvalds 4453b2db7564SYOSHIFUJI Hideaki next_sec = round_jiffies_up(next); 4454b2db7564SYOSHIFUJI Hideaki next_sched = next; 4455b2db7564SYOSHIFUJI Hideaki 4456b2db7564SYOSHIFUJI Hideaki /* If rounded timeout is accurate enough, accept it. */ 4457b2db7564SYOSHIFUJI Hideaki if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ)) 4458b2db7564SYOSHIFUJI Hideaki next_sched = next_sec; 4459b2db7564SYOSHIFUJI Hideaki 4460b2db7564SYOSHIFUJI Hideaki /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */ 4461b2db7564SYOSHIFUJI Hideaki if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX)) 4462b2db7564SYOSHIFUJI Hideaki next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX; 4463b2db7564SYOSHIFUJI Hideaki 4464e32ac250SJoe Perches pr_debug("now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n", 4465ba3542e1Sdingtianhong now, next, next_sec, next_sched); 4466c15b1ccaSHannes Frederic Sowa mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now); 44675c578aedSstephen hemminger rcu_read_unlock_bh(); 44681da177e4SLinus Torvalds } 44691da177e4SLinus Torvalds 4470c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *w) 4471c15b1ccaSHannes Frederic Sowa { 4472c15b1ccaSHannes Frederic Sowa rtnl_lock(); 4473c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 4474c15b1ccaSHannes Frederic Sowa rtnl_unlock(); 4475c15b1ccaSHannes Frederic Sowa } 4476c15b1ccaSHannes Frederic Sowa 4477c15b1ccaSHannes Frederic Sowa static void addrconf_verify(void) 4478c15b1ccaSHannes Frederic Sowa { 4479c15b1ccaSHannes Frederic Sowa mod_delayed_work(addrconf_wq, &addr_chk_work, 0); 4480c15b1ccaSHannes Frederic Sowa } 4481c15b1ccaSHannes Frederic Sowa 4482caeaba79SNicolas Dichtel static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local, 4483caeaba79SNicolas Dichtel struct in6_addr **peer_pfx) 4484461d8837SThomas Graf { 4485461d8837SThomas Graf struct in6_addr *pfx = NULL; 4486461d8837SThomas Graf 4487caeaba79SNicolas Dichtel *peer_pfx = NULL; 4488caeaba79SNicolas Dichtel 4489461d8837SThomas Graf if (addr) 4490461d8837SThomas Graf pfx = nla_data(addr); 4491461d8837SThomas Graf 4492461d8837SThomas Graf if (local) { 4493461d8837SThomas Graf if (pfx && nla_memcmp(local, pfx, sizeof(*pfx))) 4494caeaba79SNicolas Dichtel *peer_pfx = pfx; 4495461d8837SThomas Graf pfx = nla_data(local); 4496461d8837SThomas Graf } 4497461d8837SThomas Graf 4498461d8837SThomas Graf return pfx; 4499461d8837SThomas Graf } 4500461d8837SThomas Graf 4501ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = { 4502461d8837SThomas Graf [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) }, 4503461d8837SThomas Graf [IFA_LOCAL] = { .len = sizeof(struct in6_addr) }, 4504461d8837SThomas Graf [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) }, 4505479840ffSJiri Pirko [IFA_FLAGS] = { .len = sizeof(u32) }, 45068308f3ffSDavid Ahern [IFA_RT_PRIORITY] = { .len = sizeof(u32) }, 4507461d8837SThomas Graf }; 4508461d8837SThomas Graf 45091da177e4SLinus Torvalds static int 4510c21ef3e3SDavid Ahern inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, 4511c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 45121da177e4SLinus Torvalds { 45133b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 4514b933f716SThomas Graf struct ifaddrmsg *ifm; 4515b933f716SThomas Graf struct nlattr *tb[IFA_MAX+1]; 4516caeaba79SNicolas Dichtel struct in6_addr *pfx, *peer_pfx; 45176046d5b4SHeiner Kallweit u32 ifa_flags; 4518b933f716SThomas Graf int err; 45191da177e4SLinus Torvalds 4520fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy, 4521c21ef3e3SDavid Ahern extack); 4522b933f716SThomas Graf if (err < 0) 4523b933f716SThomas Graf return err; 4524b933f716SThomas Graf 4525b933f716SThomas Graf ifm = nlmsg_data(nlh); 4526caeaba79SNicolas Dichtel pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); 452763159f29SIan Morris if (!pfx) 45281da177e4SLinus Torvalds return -EINVAL; 45291da177e4SLinus Torvalds 45306046d5b4SHeiner Kallweit ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags; 45316046d5b4SHeiner Kallweit 45326046d5b4SHeiner Kallweit /* We ignore other flags so far. */ 45336046d5b4SHeiner Kallweit ifa_flags &= IFA_F_MANAGETEMPADDR; 45346046d5b4SHeiner Kallweit 45356046d5b4SHeiner Kallweit return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx, 45366046d5b4SHeiner Kallweit ifm->ifa_prefixlen); 45371da177e4SLinus Torvalds } 45381da177e4SLinus Torvalds 45398308f3ffSDavid Ahern static int modify_prefix_route(struct inet6_ifaddr *ifp, 45408308f3ffSDavid Ahern unsigned long expires, u32 flags) 45418308f3ffSDavid Ahern { 45428308f3ffSDavid Ahern struct fib6_info *f6i; 45438308f3ffSDavid Ahern 45448308f3ffSDavid Ahern f6i = addrconf_get_prefix_route(&ifp->addr, 45458308f3ffSDavid Ahern ifp->prefix_len, 45468308f3ffSDavid Ahern ifp->idev->dev, 45478308f3ffSDavid Ahern 0, RTF_GATEWAY | RTF_DEFAULT); 45488308f3ffSDavid Ahern if (!f6i) 45498308f3ffSDavid Ahern return -ENOENT; 45508308f3ffSDavid Ahern 45518308f3ffSDavid Ahern if (f6i->fib6_metric != ifp->rt_priority) { 45528308f3ffSDavid Ahern /* add new one */ 45538308f3ffSDavid Ahern addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 45548308f3ffSDavid Ahern ifp->rt_priority, ifp->idev->dev, 45558308f3ffSDavid Ahern expires, flags, GFP_KERNEL); 45568308f3ffSDavid Ahern /* delete old one */ 45578308f3ffSDavid Ahern ip6_del_rt(dev_net(ifp->idev->dev), f6i); 45588308f3ffSDavid Ahern } else { 45598308f3ffSDavid Ahern if (!expires) 45608308f3ffSDavid Ahern fib6_clean_expires(f6i); 45618308f3ffSDavid Ahern else 45628308f3ffSDavid Ahern fib6_set_expires(f6i, expires); 45638308f3ffSDavid Ahern 45648308f3ffSDavid Ahern fib6_info_release(f6i); 45658308f3ffSDavid Ahern } 45668308f3ffSDavid Ahern 45678308f3ffSDavid Ahern return 0; 45688308f3ffSDavid Ahern } 45698308f3ffSDavid Ahern 4570d169a1f8SDavid Ahern static int inet6_addr_modify(struct inet6_ifaddr *ifp, struct ifa6_config *cfg) 4571081bba5bSNoriaki TAKAMIYA { 45726f704992SYOSHIFUJI Hideaki u32 flags; 45736f704992SYOSHIFUJI Hideaki clock_t expires; 45744bed72e4SYOSHIFUJI Hideaki unsigned long timeout; 457553bd6749SJiri Pirko bool was_managetempaddr; 45765b84efecSThomas Haller bool had_prefixroute; 457746d48046SYOSHIFUJI Hideaki 4578c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 4579c15b1ccaSHannes Frederic Sowa 4580d169a1f8SDavid Ahern if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft) 4581081bba5bSNoriaki TAKAMIYA return -EINVAL; 4582081bba5bSNoriaki TAKAMIYA 4583d169a1f8SDavid Ahern if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR && 458453bd6749SJiri Pirko (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64)) 458553bd6749SJiri Pirko return -EINVAL; 458653bd6749SJiri Pirko 4587f1c02cfbSSabrina Dubroca if (!(ifp->flags & IFA_F_TENTATIVE) || ifp->flags & IFA_F_DADFAILED) 4588d169a1f8SDavid Ahern cfg->ifa_flags &= ~IFA_F_OPTIMISTIC; 4589f1c02cfbSSabrina Dubroca 4590d169a1f8SDavid Ahern timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ); 45914bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 45924bed72e4SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(timeout * HZ); 4593d169a1f8SDavid Ahern cfg->valid_lft = timeout; 45946f704992SYOSHIFUJI Hideaki flags = RTF_EXPIRES; 45954bed72e4SYOSHIFUJI Hideaki } else { 45964bed72e4SYOSHIFUJI Hideaki expires = 0; 45974bed72e4SYOSHIFUJI Hideaki flags = 0; 4598d169a1f8SDavid Ahern cfg->ifa_flags |= IFA_F_PERMANENT; 45996f704992SYOSHIFUJI Hideaki } 4600081bba5bSNoriaki TAKAMIYA 4601d169a1f8SDavid Ahern timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ); 46024bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 46034bed72e4SYOSHIFUJI Hideaki if (timeout == 0) 4604d169a1f8SDavid Ahern cfg->ifa_flags |= IFA_F_DEPRECATED; 4605d169a1f8SDavid Ahern cfg->preferred_lft = timeout; 46064bed72e4SYOSHIFUJI Hideaki } 4607081bba5bSNoriaki TAKAMIYA 4608081bba5bSNoriaki TAKAMIYA spin_lock_bh(&ifp->lock); 460953bd6749SJiri Pirko was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR; 46105b84efecSThomas Haller had_prefixroute = ifp->flags & IFA_F_PERMANENT && 46115b84efecSThomas Haller !(ifp->flags & IFA_F_NOPREFIXROUTE); 461253bd6749SJiri Pirko ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | 4613761aac73SThomas Haller IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR | 4614761aac73SThomas Haller IFA_F_NOPREFIXROUTE); 4615d169a1f8SDavid Ahern ifp->flags |= cfg->ifa_flags; 4616081bba5bSNoriaki TAKAMIYA ifp->tstamp = jiffies; 4617d169a1f8SDavid Ahern ifp->valid_lft = cfg->valid_lft; 4618d169a1f8SDavid Ahern ifp->prefered_lft = cfg->preferred_lft; 4619081bba5bSNoriaki TAKAMIYA 46208308f3ffSDavid Ahern if (cfg->rt_priority && cfg->rt_priority != ifp->rt_priority) 46218308f3ffSDavid Ahern ifp->rt_priority = cfg->rt_priority; 46228308f3ffSDavid Ahern 4623081bba5bSNoriaki TAKAMIYA spin_unlock_bh(&ifp->lock); 4624081bba5bSNoriaki TAKAMIYA if (!(ifp->flags&IFA_F_TENTATIVE)) 4625081bba5bSNoriaki TAKAMIYA ipv6_ifa_notify(0, ifp); 4626081bba5bSNoriaki TAKAMIYA 4627d169a1f8SDavid Ahern if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) { 46288308f3ffSDavid Ahern int rc = -ENOENT; 46298308f3ffSDavid Ahern 46308308f3ffSDavid Ahern if (had_prefixroute) 46318308f3ffSDavid Ahern rc = modify_prefix_route(ifp, expires, flags); 46328308f3ffSDavid Ahern 46338308f3ffSDavid Ahern /* prefix route could have been deleted; if so restore it */ 46348308f3ffSDavid Ahern if (rc == -ENOENT) { 4635acb54e3cSDavid Ahern addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 46368308f3ffSDavid Ahern ifp->rt_priority, ifp->idev->dev, 46378308f3ffSDavid Ahern expires, flags, GFP_KERNEL); 46388308f3ffSDavid Ahern } 46395b84efecSThomas Haller } else if (had_prefixroute) { 46405b84efecSThomas Haller enum cleanup_prefix_rt_t action; 46415b84efecSThomas Haller unsigned long rt_expires; 46425b84efecSThomas Haller 46435b84efecSThomas Haller write_lock_bh(&ifp->idev->lock); 46445b84efecSThomas Haller action = check_cleanup_prefix_route(ifp, &rt_expires); 46455b84efecSThomas Haller write_unlock_bh(&ifp->idev->lock); 46465b84efecSThomas Haller 46475b84efecSThomas Haller if (action != CLEANUP_PREFIX_RT_NOP) { 46485b84efecSThomas Haller cleanup_prefix_route(ifp, rt_expires, 46495b84efecSThomas Haller action == CLEANUP_PREFIX_RT_DEL); 46505b84efecSThomas Haller } 4651761aac73SThomas Haller } 465253bd6749SJiri Pirko 465353bd6749SJiri Pirko if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) { 4654d169a1f8SDavid Ahern if (was_managetempaddr && 4655d169a1f8SDavid Ahern !(ifp->flags & IFA_F_MANAGETEMPADDR)) { 4656d169a1f8SDavid Ahern cfg->valid_lft = 0; 4657d169a1f8SDavid Ahern cfg->preferred_lft = 0; 4658d169a1f8SDavid Ahern } 4659d169a1f8SDavid Ahern manage_tempaddrs(ifp->idev, ifp, cfg->valid_lft, 4660d169a1f8SDavid Ahern cfg->preferred_lft, !was_managetempaddr, 4661d169a1f8SDavid Ahern jiffies); 466253bd6749SJiri Pirko } 466353bd6749SJiri Pirko 4664c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 4665081bba5bSNoriaki TAKAMIYA 4666081bba5bSNoriaki TAKAMIYA return 0; 4667081bba5bSNoriaki TAKAMIYA } 4668081bba5bSNoriaki TAKAMIYA 4669081bba5bSNoriaki TAKAMIYA static int 4670c21ef3e3SDavid Ahern inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, 4671c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 46721da177e4SLinus Torvalds { 46733b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 4674461d8837SThomas Graf struct ifaddrmsg *ifm; 4675461d8837SThomas Graf struct nlattr *tb[IFA_MAX+1]; 467619b1518cSDavid Ahern struct in6_addr *peer_pfx; 46777198f8ceSThomas Graf struct inet6_ifaddr *ifa; 46787198f8ceSThomas Graf struct net_device *dev; 4679f1c02cfbSSabrina Dubroca struct inet6_dev *idev; 468019b1518cSDavid Ahern struct ifa6_config cfg; 4681461d8837SThomas Graf int err; 46821da177e4SLinus Torvalds 4683fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy, 4684c21ef3e3SDavid Ahern extack); 4685461d8837SThomas Graf if (err < 0) 4686461d8837SThomas Graf return err; 4687461d8837SThomas Graf 468819b1518cSDavid Ahern memset(&cfg, 0, sizeof(cfg)); 468919b1518cSDavid Ahern 4690461d8837SThomas Graf ifm = nlmsg_data(nlh); 469119b1518cSDavid Ahern cfg.pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); 469219b1518cSDavid Ahern if (!cfg.pfx) 46931da177e4SLinus Torvalds return -EINVAL; 46941da177e4SLinus Torvalds 469519b1518cSDavid Ahern cfg.peer_pfx = peer_pfx; 469619b1518cSDavid Ahern cfg.plen = ifm->ifa_prefixlen; 46978308f3ffSDavid Ahern if (tb[IFA_RT_PRIORITY]) 46988308f3ffSDavid Ahern cfg.rt_priority = nla_get_u32(tb[IFA_RT_PRIORITY]); 46998308f3ffSDavid Ahern 470019b1518cSDavid Ahern cfg.valid_lft = INFINITY_LIFE_TIME; 470119b1518cSDavid Ahern cfg.preferred_lft = INFINITY_LIFE_TIME; 470219b1518cSDavid Ahern 4703461d8837SThomas Graf if (tb[IFA_CACHEINFO]) { 47040778769dSNoriaki TAKAMIYA struct ifa_cacheinfo *ci; 4705461d8837SThomas Graf 4706461d8837SThomas Graf ci = nla_data(tb[IFA_CACHEINFO]); 470719b1518cSDavid Ahern cfg.valid_lft = ci->ifa_valid; 470819b1518cSDavid Ahern cfg.preferred_lft = ci->ifa_prefered; 47090778769dSNoriaki TAKAMIYA } 47100778769dSNoriaki TAKAMIYA 4711af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifm->ifa_index); 471263159f29SIan Morris if (!dev) 47137198f8ceSThomas Graf return -ENODEV; 4714081bba5bSNoriaki TAKAMIYA 471519b1518cSDavid Ahern if (tb[IFA_FLAGS]) 471619b1518cSDavid Ahern cfg.ifa_flags = nla_get_u32(tb[IFA_FLAGS]); 471719b1518cSDavid Ahern else 471819b1518cSDavid Ahern cfg.ifa_flags = ifm->ifa_flags; 4719479840ffSJiri Pirko 472055ebaef1SNoriaki TAKAMIYA /* We ignore other flags so far. */ 472119b1518cSDavid Ahern cfg.ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | 472219b1518cSDavid Ahern IFA_F_MANAGETEMPADDR | IFA_F_NOPREFIXROUTE | 472319b1518cSDavid Ahern IFA_F_MCAUTOJOIN | IFA_F_OPTIMISTIC; 4724f1c02cfbSSabrina Dubroca 4725f1c02cfbSSabrina Dubroca idev = ipv6_find_idev(dev); 4726f1c02cfbSSabrina Dubroca if (IS_ERR(idev)) 4727f1c02cfbSSabrina Dubroca return PTR_ERR(idev); 4728f1c02cfbSSabrina Dubroca 4729f1c02cfbSSabrina Dubroca if (!ipv6_allow_optimistic_dad(net, idev)) 473019b1518cSDavid Ahern cfg.ifa_flags &= ~IFA_F_OPTIMISTIC; 4731f1c02cfbSSabrina Dubroca 473219b1518cSDavid Ahern if (cfg.ifa_flags & IFA_F_NODAD && 473319b1518cSDavid Ahern cfg.ifa_flags & IFA_F_OPTIMISTIC) { 4734f1c02cfbSSabrina Dubroca NL_SET_ERR_MSG(extack, "IFA_F_NODAD and IFA_F_OPTIMISTIC are mutually exclusive"); 4735f1c02cfbSSabrina Dubroca return -EINVAL; 4736f1c02cfbSSabrina Dubroca } 473755ebaef1SNoriaki TAKAMIYA 473819b1518cSDavid Ahern ifa = ipv6_get_ifaddr(net, cfg.pfx, dev, 1); 473963159f29SIan Morris if (!ifa) { 47407198f8ceSThomas Graf /* 47417198f8ceSThomas Graf * It would be best to check for !NLM_F_CREATE here but 4742db9c7c39Sstephen hemminger * userspace already relies on not having to provide this. 47437198f8ceSThomas Graf */ 474419b1518cSDavid Ahern return inet6_addr_add(net, ifm->ifa_index, &cfg, extack); 47451da177e4SLinus Torvalds } 47461da177e4SLinus Torvalds 47477198f8ceSThomas Graf if (nlh->nlmsg_flags & NLM_F_EXCL || 47487198f8ceSThomas Graf !(nlh->nlmsg_flags & NLM_F_REPLACE)) 47497198f8ceSThomas Graf err = -EEXIST; 47507198f8ceSThomas Graf else 4751d169a1f8SDavid Ahern err = inet6_addr_modify(ifa, &cfg); 47527198f8ceSThomas Graf 47537198f8ceSThomas Graf in6_ifa_put(ifa); 47547198f8ceSThomas Graf 47557198f8ceSThomas Graf return err; 47567198f8ceSThomas Graf } 47577198f8ceSThomas Graf 4758479840ffSJiri Pirko static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags, 4759101bb229SThomas Graf u8 scope, int ifindex) 4760101bb229SThomas Graf { 4761101bb229SThomas Graf struct ifaddrmsg *ifm; 4762101bb229SThomas Graf 4763101bb229SThomas Graf ifm = nlmsg_data(nlh); 4764101bb229SThomas Graf ifm->ifa_family = AF_INET6; 4765101bb229SThomas Graf ifm->ifa_prefixlen = prefixlen; 4766101bb229SThomas Graf ifm->ifa_flags = flags; 4767101bb229SThomas Graf ifm->ifa_scope = scope; 4768101bb229SThomas Graf ifm->ifa_index = ifindex; 4769101bb229SThomas Graf } 4770101bb229SThomas Graf 477185486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp, 477285486af0SThomas Graf unsigned long tstamp, u32 preferred, u32 valid) 477385486af0SThomas Graf { 477485486af0SThomas Graf struct ifa_cacheinfo ci; 477585486af0SThomas Graf 477618a31e1eSThomas Graf ci.cstamp = cstamp_delta(cstamp); 477718a31e1eSThomas Graf ci.tstamp = cstamp_delta(tstamp); 477885486af0SThomas Graf ci.ifa_prefered = preferred; 477985486af0SThomas Graf ci.ifa_valid = valid; 478085486af0SThomas Graf 478185486af0SThomas Graf return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci); 478285486af0SThomas Graf } 478385486af0SThomas Graf 4784101bb229SThomas Graf static inline int rt_scope(int ifa_scope) 4785101bb229SThomas Graf { 4786101bb229SThomas Graf if (ifa_scope & IFA_HOST) 4787101bb229SThomas Graf return RT_SCOPE_HOST; 4788101bb229SThomas Graf else if (ifa_scope & IFA_LINK) 4789101bb229SThomas Graf return RT_SCOPE_LINK; 4790101bb229SThomas Graf else if (ifa_scope & IFA_SITE) 4791101bb229SThomas Graf return RT_SCOPE_SITE; 4792101bb229SThomas Graf else 4793101bb229SThomas Graf return RT_SCOPE_UNIVERSE; 4794101bb229SThomas Graf } 4795101bb229SThomas Graf 47960ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void) 47970ab6803bSThomas Graf { 4798339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifaddrmsg)) 4799caeaba79SNicolas Dichtel + nla_total_size(16) /* IFA_LOCAL */ 4800339bf98fSThomas Graf + nla_total_size(16) /* IFA_ADDRESS */ 4801479840ffSJiri Pirko + nla_total_size(sizeof(struct ifa_cacheinfo)) 48028308f3ffSDavid Ahern + nla_total_size(4) /* IFA_FLAGS */ 48038308f3ffSDavid Ahern + nla_total_size(4) /* IFA_RT_PRIORITY */; 48040ab6803bSThomas Graf } 4805c5396a31SYOSHIFUJI Hideaki 48061da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa, 480715e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 48081da177e4SLinus Torvalds { 48091da177e4SLinus Torvalds struct nlmsghdr *nlh; 481085486af0SThomas Graf u32 preferred, valid; 48111da177e4SLinus Torvalds 481215e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 481363159f29SIan Morris if (!nlh) 481426932566SPatrick McHardy return -EMSGSIZE; 48150ab6803bSThomas Graf 4816101bb229SThomas Graf put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope), 4817101bb229SThomas Graf ifa->idev->dev->ifindex); 4818101bb229SThomas Graf 4819fad8da3eSYasushi Asano if (!((ifa->flags&IFA_F_PERMANENT) && 4820fad8da3eSYasushi Asano (ifa->prefered_lft == INFINITY_LIFE_TIME))) { 482185486af0SThomas Graf preferred = ifa->prefered_lft; 482285486af0SThomas Graf valid = ifa->valid_lft; 482385486af0SThomas Graf if (preferred != INFINITY_LIFE_TIME) { 48241da177e4SLinus Torvalds long tval = (jiffies - ifa->tstamp)/HZ; 4825a1faa698SJens Rosenboom if (preferred > tval) 482685486af0SThomas Graf preferred -= tval; 4827a1faa698SJens Rosenboom else 4828a1faa698SJens Rosenboom preferred = 0; 4829f56619fcSBen Hutchings if (valid != INFINITY_LIFE_TIME) { 4830f56619fcSBen Hutchings if (valid > tval) 483185486af0SThomas Graf valid -= tval; 4832f56619fcSBen Hutchings else 4833f56619fcSBen Hutchings valid = 0; 4834f56619fcSBen Hutchings } 48351da177e4SLinus Torvalds } 48361da177e4SLinus Torvalds } else { 483785486af0SThomas Graf preferred = INFINITY_LIFE_TIME; 483885486af0SThomas Graf valid = INFINITY_LIFE_TIME; 48391da177e4SLinus Torvalds } 484085486af0SThomas Graf 48417996c799SCong Wang if (!ipv6_addr_any(&ifa->peer_addr)) { 4842930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 || 4843930345eaSJiri Benc nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0) 4844caeaba79SNicolas Dichtel goto error; 4845caeaba79SNicolas Dichtel } else 4846930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0) 4847caeaba79SNicolas Dichtel goto error; 4848caeaba79SNicolas Dichtel 48498308f3ffSDavid Ahern if (ifa->rt_priority && 48508308f3ffSDavid Ahern nla_put_u32(skb, IFA_RT_PRIORITY, ifa->rt_priority)) 48518308f3ffSDavid Ahern goto error; 48528308f3ffSDavid Ahern 4853caeaba79SNicolas Dichtel if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) 4854caeaba79SNicolas Dichtel goto error; 485585486af0SThomas Graf 4856479840ffSJiri Pirko if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0) 4857479840ffSJiri Pirko goto error; 4858479840ffSJiri Pirko 4859053c095aSJohannes Berg nlmsg_end(skb, nlh); 4860053c095aSJohannes Berg return 0; 4861caeaba79SNicolas Dichtel 4862caeaba79SNicolas Dichtel error: 4863caeaba79SNicolas Dichtel nlmsg_cancel(skb, nlh); 4864caeaba79SNicolas Dichtel return -EMSGSIZE; 48651da177e4SLinus Torvalds } 48661da177e4SLinus Torvalds 48671da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca, 486815e47304SEric W. Biederman u32 portid, u32 seq, int event, u16 flags) 48691da177e4SLinus Torvalds { 48701da177e4SLinus Torvalds struct nlmsghdr *nlh; 4871101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 4872101bb229SThomas Graf int ifindex = ifmca->idev->dev->ifindex; 48731da177e4SLinus Torvalds 48741da177e4SLinus Torvalds if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE) 4875101bb229SThomas Graf scope = RT_SCOPE_SITE; 4876101bb229SThomas Graf 487715e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 487863159f29SIan Morris if (!nlh) 487926932566SPatrick McHardy return -EMSGSIZE; 48800ab6803bSThomas Graf 4881101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 4882930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 || 48830ab6803bSThomas Graf put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp, 488426932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 488526932566SPatrick McHardy nlmsg_cancel(skb, nlh); 488626932566SPatrick McHardy return -EMSGSIZE; 488726932566SPatrick McHardy } 488885486af0SThomas Graf 4889053c095aSJohannes Berg nlmsg_end(skb, nlh); 4890053c095aSJohannes Berg return 0; 48911da177e4SLinus Torvalds } 48921da177e4SLinus Torvalds 48931da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca, 489415e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 48951da177e4SLinus Torvalds { 48969ee8cbb2SDavid Ahern struct net_device *dev = fib6_info_nh_dev(ifaca->aca_rt); 48979ee8cbb2SDavid Ahern int ifindex = dev ? dev->ifindex : 1; 48981da177e4SLinus Torvalds struct nlmsghdr *nlh; 4899101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 49001da177e4SLinus Torvalds 49011da177e4SLinus Torvalds if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE) 4902101bb229SThomas Graf scope = RT_SCOPE_SITE; 4903101bb229SThomas Graf 490415e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 490563159f29SIan Morris if (!nlh) 490626932566SPatrick McHardy return -EMSGSIZE; 49070ab6803bSThomas Graf 4908101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 4909930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 || 49100ab6803bSThomas Graf put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp, 491126932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 491226932566SPatrick McHardy nlmsg_cancel(skb, nlh); 491326932566SPatrick McHardy return -EMSGSIZE; 491426932566SPatrick McHardy } 491585486af0SThomas Graf 4916053c095aSJohannes Berg nlmsg_end(skb, nlh); 4917053c095aSJohannes Berg return 0; 49181da177e4SLinus Torvalds } 49191da177e4SLinus Torvalds 4920e21e8467SStephen Hemminger enum addr_type_t { 49211da177e4SLinus Torvalds UNICAST_ADDR, 49221da177e4SLinus Torvalds MULTICAST_ADDR, 49231da177e4SLinus Torvalds ANYCAST_ADDR, 49241da177e4SLinus Torvalds }; 49251da177e4SLinus Torvalds 4926234b27c3SEric Dumazet /* called with rcu_read_lock() */ 4927234b27c3SEric Dumazet static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb, 4928234b27c3SEric Dumazet struct netlink_callback *cb, enum addr_type_t type, 4929234b27c3SEric Dumazet int s_ip_idx, int *p_ip_idx) 49301da177e4SLinus Torvalds { 49311da177e4SLinus Torvalds struct ifmcaddr6 *ifmca; 49321da177e4SLinus Torvalds struct ifacaddr6 *ifaca; 4933234b27c3SEric Dumazet int err = 1; 4934234b27c3SEric Dumazet int ip_idx = *p_ip_idx; 49351da177e4SLinus Torvalds 49361da177e4SLinus Torvalds read_lock_bh(&idev->lock); 49371da177e4SLinus Torvalds switch (type) { 4938502a2ffdSstephen hemminger case UNICAST_ADDR: { 4939502a2ffdSstephen hemminger struct inet6_ifaddr *ifa; 4940502a2ffdSstephen hemminger 4941ae9cda5dSYOSHIFUJI Hideaki /* unicast address incl. temp addr */ 4942502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 4943502a2ffdSstephen hemminger if (++ip_idx < s_ip_idx) 49441da177e4SLinus Torvalds continue; 49455d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifaddr(skb, ifa, 494615e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 49475d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 49485d5619b4SYOSHIFUJI Hideaki RTM_NEWADDR, 49495d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 4950053c095aSJohannes Berg if (err < 0) 4951bcd32326SEric Dumazet break; 495263998ac2SNicolas Dichtel nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 49531da177e4SLinus Torvalds } 49541da177e4SLinus Torvalds break; 4955502a2ffdSstephen hemminger } 49561da177e4SLinus Torvalds case MULTICAST_ADDR: 49571da177e4SLinus Torvalds /* multicast address */ 49581da177e4SLinus Torvalds for (ifmca = idev->mc_list; ifmca; 49591da177e4SLinus Torvalds ifmca = ifmca->next, ip_idx++) { 49601da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 49611da177e4SLinus Torvalds continue; 49625d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifmcaddr(skb, ifmca, 496315e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 49645d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 49655d5619b4SYOSHIFUJI Hideaki RTM_GETMULTICAST, 49665d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 4967053c095aSJohannes Berg if (err < 0) 4968bcd32326SEric Dumazet break; 49691da177e4SLinus Torvalds } 49701da177e4SLinus Torvalds break; 49711da177e4SLinus Torvalds case ANYCAST_ADDR: 49721da177e4SLinus Torvalds /* anycast address */ 49731da177e4SLinus Torvalds for (ifaca = idev->ac_list; ifaca; 49741da177e4SLinus Torvalds ifaca = ifaca->aca_next, ip_idx++) { 49751da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 49761da177e4SLinus Torvalds continue; 49775d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifacaddr(skb, ifaca, 497815e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 49795d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 49805d5619b4SYOSHIFUJI Hideaki RTM_GETANYCAST, 49815d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 4982053c095aSJohannes Berg if (err < 0) 4983bcd32326SEric Dumazet break; 49841da177e4SLinus Torvalds } 49851da177e4SLinus Torvalds break; 49861da177e4SLinus Torvalds default: 49871da177e4SLinus Torvalds break; 49881da177e4SLinus Torvalds } 49891da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 4990234b27c3SEric Dumazet *p_ip_idx = ip_idx; 4991234b27c3SEric Dumazet return err; 4992234b27c3SEric Dumazet } 49935d5619b4SYOSHIFUJI Hideaki 4994234b27c3SEric Dumazet static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb, 4995234b27c3SEric Dumazet enum addr_type_t type) 4996234b27c3SEric Dumazet { 4997234b27c3SEric Dumazet struct net *net = sock_net(skb->sk); 4998234b27c3SEric Dumazet int h, s_h; 4999234b27c3SEric Dumazet int idx, ip_idx; 5000234b27c3SEric Dumazet int s_idx, s_ip_idx; 5001234b27c3SEric Dumazet struct net_device *dev; 5002234b27c3SEric Dumazet struct inet6_dev *idev; 5003234b27c3SEric Dumazet struct hlist_head *head; 5004234b27c3SEric Dumazet 5005234b27c3SEric Dumazet s_h = cb->args[0]; 5006234b27c3SEric Dumazet s_idx = idx = cb->args[1]; 5007234b27c3SEric Dumazet s_ip_idx = ip_idx = cb->args[2]; 5008234b27c3SEric Dumazet 5009234b27c3SEric Dumazet rcu_read_lock(); 501063998ac2SNicolas Dichtel cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq; 5011234b27c3SEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 5012234b27c3SEric Dumazet idx = 0; 5013234b27c3SEric Dumazet head = &net->dev_index_head[h]; 5014b67bfe0dSSasha Levin hlist_for_each_entry_rcu(dev, head, index_hlist) { 5015234b27c3SEric Dumazet if (idx < s_idx) 5016234b27c3SEric Dumazet goto cont; 50174b97efdfSPatrick McHardy if (h > s_h || idx > s_idx) 5018234b27c3SEric Dumazet s_ip_idx = 0; 5019234b27c3SEric Dumazet ip_idx = 0; 5020e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 5021e21e8467SStephen Hemminger if (!idev) 5022234b27c3SEric Dumazet goto cont; 5023234b27c3SEric Dumazet 5024234b27c3SEric Dumazet if (in6_dump_addrs(idev, skb, cb, type, 50257b46a644SDavid S. Miller s_ip_idx, &ip_idx) < 0) 5026234b27c3SEric Dumazet goto done; 50277562f876SPavel Emelianov cont: 50287562f876SPavel Emelianov idx++; 50291da177e4SLinus Torvalds } 5030234b27c3SEric Dumazet } 5031234b27c3SEric Dumazet done: 5032234b27c3SEric Dumazet rcu_read_unlock(); 5033234b27c3SEric Dumazet cb->args[0] = h; 5034234b27c3SEric Dumazet cb->args[1] = idx; 5035234b27c3SEric Dumazet cb->args[2] = ip_idx; 5036234b27c3SEric Dumazet 50371da177e4SLinus Torvalds return skb->len; 50381da177e4SLinus Torvalds } 50391da177e4SLinus Torvalds 50401da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) 50411da177e4SLinus Torvalds { 50421da177e4SLinus Torvalds enum addr_type_t type = UNICAST_ADDR; 5043b854272bSDenis V. Lunev 50441da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 50451da177e4SLinus Torvalds } 50461da177e4SLinus Torvalds 50471da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb) 50481da177e4SLinus Torvalds { 50491da177e4SLinus Torvalds enum addr_type_t type = MULTICAST_ADDR; 5050b854272bSDenis V. Lunev 50511da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 50521da177e4SLinus Torvalds } 50531da177e4SLinus Torvalds 50541da177e4SLinus Torvalds 50551da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb) 50561da177e4SLinus Torvalds { 50571da177e4SLinus Torvalds enum addr_type_t type = ANYCAST_ADDR; 5058b854272bSDenis V. Lunev 50591da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 50601da177e4SLinus Torvalds } 50611da177e4SLinus Torvalds 5062c21ef3e3SDavid Ahern static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh, 5063c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 50646c223828SNoriaki TAKAMIYA { 50653b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 50661b29fc2cSThomas Graf struct ifaddrmsg *ifm; 50671b29fc2cSThomas Graf struct nlattr *tb[IFA_MAX+1]; 5068caeaba79SNicolas Dichtel struct in6_addr *addr = NULL, *peer; 50696c223828SNoriaki TAKAMIYA struct net_device *dev = NULL; 50706c223828SNoriaki TAKAMIYA struct inet6_ifaddr *ifa; 50716c223828SNoriaki TAKAMIYA struct sk_buff *skb; 50726c223828SNoriaki TAKAMIYA int err; 50736c223828SNoriaki TAKAMIYA 5074fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy, 5075c21ef3e3SDavid Ahern extack); 50761b29fc2cSThomas Graf if (err < 0) 5077c24675f8SFlorian Westphal return err; 50786c223828SNoriaki TAKAMIYA 5079caeaba79SNicolas Dichtel addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer); 5080c24675f8SFlorian Westphal if (!addr) 5081c24675f8SFlorian Westphal return -EINVAL; 50821b29fc2cSThomas Graf 50831b29fc2cSThomas Graf ifm = nlmsg_data(nlh); 50846c223828SNoriaki TAKAMIYA if (ifm->ifa_index) 5085c24675f8SFlorian Westphal dev = dev_get_by_index(net, ifm->ifa_index); 50866c223828SNoriaki TAKAMIYA 5087e21e8467SStephen Hemminger ifa = ipv6_get_ifaddr(net, addr, dev, 1); 5088e21e8467SStephen Hemminger if (!ifa) { 50891b29fc2cSThomas Graf err = -EADDRNOTAVAIL; 50901b29fc2cSThomas Graf goto errout; 50916c223828SNoriaki TAKAMIYA } 50926c223828SNoriaki TAKAMIYA 5093e21e8467SStephen Hemminger skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL); 5094e21e8467SStephen Hemminger if (!skb) { 50951b29fc2cSThomas Graf err = -ENOBUFS; 50961b29fc2cSThomas Graf goto errout_ifa; 50971b29fc2cSThomas Graf } 50981b29fc2cSThomas Graf 509915e47304SEric W. Biederman err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid, 51006c223828SNoriaki TAKAMIYA nlh->nlmsg_seq, RTM_NEWADDR, 0); 510126932566SPatrick McHardy if (err < 0) { 510226932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 510326932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 510426932566SPatrick McHardy kfree_skb(skb); 510526932566SPatrick McHardy goto errout_ifa; 510626932566SPatrick McHardy } 510715e47304SEric W. Biederman err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 51081b29fc2cSThomas Graf errout_ifa: 51096c223828SNoriaki TAKAMIYA in6_ifa_put(ifa); 51101b29fc2cSThomas Graf errout: 5111c24675f8SFlorian Westphal if (dev) 5112c24675f8SFlorian Westphal dev_put(dev); 51136c223828SNoriaki TAKAMIYA return err; 51146c223828SNoriaki TAKAMIYA } 51156c223828SNoriaki TAKAMIYA 51161da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa) 51171da177e4SLinus Torvalds { 51181da177e4SLinus Torvalds struct sk_buff *skb; 5119c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(ifa->idev->dev); 51205d620266SThomas Graf int err = -ENOBUFS; 51211da177e4SLinus Torvalds 51220ab6803bSThomas Graf skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC); 512363159f29SIan Morris if (!skb) 51245d620266SThomas Graf goto errout; 51255d620266SThomas Graf 51265d620266SThomas Graf err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0); 512726932566SPatrick McHardy if (err < 0) { 512826932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 512926932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 513026932566SPatrick McHardy kfree_skb(skb); 513126932566SPatrick McHardy goto errout; 513226932566SPatrick McHardy } 51331ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC); 51341ce85fe4SPablo Neira Ayuso return; 51355d620266SThomas Graf errout: 51365d620266SThomas Graf if (err < 0) 51376fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err); 51381da177e4SLinus Torvalds } 51391da177e4SLinus Torvalds 5140b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf, 51411da177e4SLinus Torvalds __s32 *array, int bytes) 51421da177e4SLinus Torvalds { 514304561c1fSThomas Graf BUG_ON(bytes < (DEVCONF_MAX * 4)); 514404561c1fSThomas Graf 51451da177e4SLinus Torvalds memset(array, 0, bytes); 51461da177e4SLinus Torvalds array[DEVCONF_FORWARDING] = cnf->forwarding; 51471da177e4SLinus Torvalds array[DEVCONF_HOPLIMIT] = cnf->hop_limit; 51481da177e4SLinus Torvalds array[DEVCONF_MTU6] = cnf->mtu6; 51491da177e4SLinus Torvalds array[DEVCONF_ACCEPT_RA] = cnf->accept_ra; 51501da177e4SLinus Torvalds array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects; 51511da177e4SLinus Torvalds array[DEVCONF_AUTOCONF] = cnf->autoconf; 51521da177e4SLinus Torvalds array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits; 51531da177e4SLinus Torvalds array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits; 515493908d19SThomas Graf array[DEVCONF_RTR_SOLICIT_INTERVAL] = 515593908d19SThomas Graf jiffies_to_msecs(cnf->rtr_solicit_interval); 5156bd11f074SMaciej Żenczykowski array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] = 5157bd11f074SMaciej Żenczykowski jiffies_to_msecs(cnf->rtr_solicit_max_interval); 515893908d19SThomas Graf array[DEVCONF_RTR_SOLICIT_DELAY] = 515993908d19SThomas Graf jiffies_to_msecs(cnf->rtr_solicit_delay); 51601da177e4SLinus Torvalds array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version; 5161fc4eba58SHannes Frederic Sowa array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] = 5162fc4eba58SHannes Frederic Sowa jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval); 5163fc4eba58SHannes Frederic Sowa array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] = 5164fc4eba58SHannes Frederic Sowa jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval); 51651da177e4SLinus Torvalds array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr; 51661da177e4SLinus Torvalds array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft; 51671da177e4SLinus Torvalds array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft; 51681da177e4SLinus Torvalds array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry; 51691da177e4SLinus Torvalds array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor; 51701da177e4SLinus Torvalds array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses; 517165f5c7c1SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr; 51728013d1d7SHangbin Liu array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit; 5173c4fd30ebSYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo; 5174930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 5175930d6ff2SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref; 517693908d19SThomas Graf array[DEVCONF_RTR_PROBE_INTERVAL] = 517793908d19SThomas Graf jiffies_to_msecs(cnf->rtr_probe_interval); 5178fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 5179bbea124bSJoel Scherpelz array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] = cnf->accept_ra_rt_info_min_plen; 518009c884d4SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen; 518109c884d4SYOSHIFUJI Hideaki #endif 5182930d6ff2SYOSHIFUJI Hideaki #endif 5183fbea49e1SYOSHIFUJI Hideaki array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp; 51840bcbc926SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route; 518595c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 518695c385b4SNeil Horman array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad; 51877fd2561eSErik Kline array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic; 518895c385b4SNeil Horman #endif 51897bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 51907bc570c8SYOSHIFUJI Hideaki array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding; 51917bc570c8SYOSHIFUJI Hideaki #endif 5192778d80beSYOSHIFUJI Hideaki array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6; 51931b34be74SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad; 5194c3faca05SCosmin Ratiu array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao; 51955cb04436SHannes Frederic Sowa array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify; 5196b800c3b9SHannes Frederic Sowa array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc; 5197d9333196SBen Greear array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local; 5198c2943f14SHarout Hedeshian array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu; 519935103d11SAndy Gospodarek array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown; 52003d1bec99SHannes Frederic Sowa /* we omit DEVCONF_STABLE_SECRET for now */ 52013985e8a3SErik Kline array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only; 5202abbc3043SJohannes Berg array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast; 52037a02bf89SJohannes Berg array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na; 5204f1705ec1SDavid Ahern array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down; 52051ababebaSDavid Lebrun array[DEVCONF_SEG6_ENABLED] = cnf->seg6_enabled; 5206bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC 5207bf355b8dSDavid Lebrun array[DEVCONF_SEG6_REQUIRE_HMAC] = cnf->seg6_require_hmac; 5208bf355b8dSDavid Lebrun #endif 5209adc176c5SErik Nordmark array[DEVCONF_ENHANCED_DAD] = cnf->enhanced_dad; 5210d35a00b8SFelix Jia array[DEVCONF_ADDR_GEN_MODE] = cnf->addr_gen_mode; 5211df789fe7SDavid Forster array[DEVCONF_DISABLE_POLICY] = cnf->disable_policy; 52122210d6b2SMaciej Żenczykowski array[DEVCONF_NDISC_TCLASS] = cnf->ndisc_tclass; 52131da177e4SLinus Torvalds } 52141da177e4SLinus Torvalds 5215b382b191SThomas Graf static inline size_t inet6_ifla6_size(void) 5216b382b191SThomas Graf { 5217b382b191SThomas Graf return nla_total_size(4) /* IFLA_INET6_FLAGS */ 5218b382b191SThomas Graf + nla_total_size(sizeof(struct ifla_cacheinfo)) 5219b382b191SThomas Graf + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */ 5220b382b191SThomas Graf + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */ 5221f53adae4SDaniel Borkmann + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */ 5222f53adae4SDaniel Borkmann + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */ 5223b382b191SThomas Graf } 5224b382b191SThomas Graf 5225339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void) 5226339bf98fSThomas Graf { 5227339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 5228339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 5229339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 5230339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 5231339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 52320344338bSAndy Gospodarek + nla_total_size(1) /* IFLA_OPERSTATE */ 5233b382b191SThomas Graf + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */ 5234339bf98fSThomas Graf } 5235c5396a31SYOSHIFUJI Hideaki 5236be281e55SEric Dumazet static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib, 5237aca05671SJia He int bytes) 52387f7d9a6bSHerbert Xu { 52397f7d9a6bSHerbert Xu int i; 5240aca05671SJia He int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX; 52417f7d9a6bSHerbert Xu BUG_ON(pad < 0); 52427f7d9a6bSHerbert Xu 52437f7d9a6bSHerbert Xu /* Use put_unaligned() because stats may not be aligned for u64. */ 5244aca05671SJia He put_unaligned(ICMP6_MIB_MAX, &stats[0]); 5245aca05671SJia He for (i = 1; i < ICMP6_MIB_MAX; i++) 5246be281e55SEric Dumazet put_unaligned(atomic_long_read(&mib[i]), &stats[i]); 52477f7d9a6bSHerbert Xu 5248aca05671SJia He memset(&stats[ICMP6_MIB_MAX], 0, pad); 52497f7d9a6bSHerbert Xu } 52507f7d9a6bSHerbert Xu 5251698365faSWANG Cong static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib, 5252a3a77372SRaghavendra K T int bytes, size_t syncpoff) 52534ce3c183SEric Dumazet { 5254a3a77372SRaghavendra K T int i, c; 5255a3a77372SRaghavendra K T u64 buff[IPSTATS_MIB_MAX]; 5256a3a77372SRaghavendra K T int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX; 5257a3a77372SRaghavendra K T 52584ce3c183SEric Dumazet BUG_ON(pad < 0); 52594ce3c183SEric Dumazet 5260a3a77372SRaghavendra K T memset(buff, 0, sizeof(buff)); 5261a3a77372SRaghavendra K T buff[0] = IPSTATS_MIB_MAX; 52624ce3c183SEric Dumazet 5263a3a77372SRaghavendra K T for_each_possible_cpu(c) { 5264a3a77372SRaghavendra K T for (i = 1; i < IPSTATS_MIB_MAX; i++) 5265a3a77372SRaghavendra K T buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff); 5266a3a77372SRaghavendra K T } 5267a3a77372SRaghavendra K T 5268a3a77372SRaghavendra K T memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64)); 5269a3a77372SRaghavendra K T memset(&stats[IPSTATS_MIB_MAX], 0, pad); 52704ce3c183SEric Dumazet } 52714ce3c183SEric Dumazet 52727f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype, 52737f7d9a6bSHerbert Xu int bytes) 52747f7d9a6bSHerbert Xu { 52757f7d9a6bSHerbert Xu switch (attrtype) { 52767f7d9a6bSHerbert Xu case IFLA_INET6_STATS: 5277a3a77372SRaghavendra K T __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes, 5278a3a77372SRaghavendra K T offsetof(struct ipstats_mib, syncp)); 52797f7d9a6bSHerbert Xu break; 52807f7d9a6bSHerbert Xu case IFLA_INET6_ICMP6STATS: 5281aca05671SJia He __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes); 52827f7d9a6bSHerbert Xu break; 52837f7d9a6bSHerbert Xu } 52847f7d9a6bSHerbert Xu } 52857f7d9a6bSHerbert Xu 5286d5566fd7SSowmini Varadhan static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev, 5287d5566fd7SSowmini Varadhan u32 ext_filter_mask) 52881da177e4SLinus Torvalds { 5289bf99f1bdSYOSHIFUJI Hideaki struct nlattr *nla; 52901da177e4SLinus Torvalds struct ifla_cacheinfo ci; 52911da177e4SLinus Torvalds 5292c78679e8SDavid S. Miller if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags)) 5293c78679e8SDavid S. Miller goto nla_put_failure; 52941da177e4SLinus Torvalds ci.max_reasm_len = IPV6_MAXPLEN; 529524912420SDavid S. Miller ci.tstamp = cstamp_delta(idev->tstamp); 529624912420SDavid S. Miller ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time); 52971f9248e5SJiri Pirko ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME)); 5298c78679e8SDavid S. Miller if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci)) 5299c78679e8SDavid S. Miller goto nla_put_failure; 5300bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32)); 530163159f29SIan Morris if (!nla) 530204561c1fSThomas Graf goto nla_put_failure; 5303bf99f1bdSYOSHIFUJI Hideaki ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla)); 53041da177e4SLinus Torvalds 5305bf99f1bdSYOSHIFUJI Hideaki /* XXX - MC not implemented */ 5306bf99f1bdSYOSHIFUJI Hideaki 5307d5566fd7SSowmini Varadhan if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS) 5308d5566fd7SSowmini Varadhan return 0; 5309d5566fd7SSowmini Varadhan 5310bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64)); 531163159f29SIan Morris if (!nla) 5312bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 5313bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla)); 5314bf99f1bdSYOSHIFUJI Hideaki 5315bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64)); 531663159f29SIan Morris if (!nla) 5317bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 5318bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla)); 53191da177e4SLinus Torvalds 5320f53adae4SDaniel Borkmann nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr)); 532163159f29SIan Morris if (!nla) 5322f53adae4SDaniel Borkmann goto nla_put_failure; 5323bc91b0f0SJiri Pirko 5324d35a00b8SFelix Jia if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->cnf.addr_gen_mode)) 5325bc91b0f0SJiri Pirko goto nla_put_failure; 5326bc91b0f0SJiri Pirko 5327f53adae4SDaniel Borkmann read_lock_bh(&idev->lock); 5328f53adae4SDaniel Borkmann memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla)); 5329f53adae4SDaniel Borkmann read_unlock_bh(&idev->lock); 5330f53adae4SDaniel Borkmann 5331b382b191SThomas Graf return 0; 5332b382b191SThomas Graf 5333b382b191SThomas Graf nla_put_failure: 5334b382b191SThomas Graf return -EMSGSIZE; 5335b382b191SThomas Graf } 5336b382b191SThomas Graf 5337b1974ed0SArad, Ronen static size_t inet6_get_link_af_size(const struct net_device *dev, 5338b1974ed0SArad, Ronen u32 ext_filter_mask) 5339b382b191SThomas Graf { 5340b382b191SThomas Graf if (!__in6_dev_get(dev)) 5341b382b191SThomas Graf return 0; 5342b382b191SThomas Graf 5343b382b191SThomas Graf return inet6_ifla6_size(); 5344b382b191SThomas Graf } 5345b382b191SThomas Graf 5346d5566fd7SSowmini Varadhan static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev, 5347d5566fd7SSowmini Varadhan u32 ext_filter_mask) 5348b382b191SThomas Graf { 5349b382b191SThomas Graf struct inet6_dev *idev = __in6_dev_get(dev); 5350b382b191SThomas Graf 5351b382b191SThomas Graf if (!idev) 5352b382b191SThomas Graf return -ENODATA; 5353b382b191SThomas Graf 5354d5566fd7SSowmini Varadhan if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0) 5355b382b191SThomas Graf return -EMSGSIZE; 5356b382b191SThomas Graf 5357b382b191SThomas Graf return 0; 5358b382b191SThomas Graf } 5359b382b191SThomas Graf 5360f53adae4SDaniel Borkmann static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token) 5361f53adae4SDaniel Borkmann { 5362f53adae4SDaniel Borkmann struct inet6_ifaddr *ifp; 5363f53adae4SDaniel Borkmann struct net_device *dev = idev->dev; 536447e27d5eSDaniel Borkmann bool clear_token, update_rs = false; 5365dc848292SHannes Frederic Sowa struct in6_addr ll_addr; 5366f53adae4SDaniel Borkmann 5367c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 5368c15b1ccaSHannes Frederic Sowa 536963159f29SIan Morris if (!token) 5370f53adae4SDaniel Borkmann return -EINVAL; 5371f53adae4SDaniel Borkmann if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) 5372f53adae4SDaniel Borkmann return -EINVAL; 5373f53adae4SDaniel Borkmann if (!ipv6_accept_ra(idev)) 5374f53adae4SDaniel Borkmann return -EINVAL; 5375bd11f074SMaciej Żenczykowski if (idev->cnf.rtr_solicits == 0) 5376f53adae4SDaniel Borkmann return -EINVAL; 5377f53adae4SDaniel Borkmann 5378f53adae4SDaniel Borkmann write_lock_bh(&idev->lock); 5379f53adae4SDaniel Borkmann 5380f53adae4SDaniel Borkmann BUILD_BUG_ON(sizeof(token->s6_addr) != 16); 5381f53adae4SDaniel Borkmann memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8); 5382f53adae4SDaniel Borkmann 5383f53adae4SDaniel Borkmann write_unlock_bh(&idev->lock); 5384f53adae4SDaniel Borkmann 538547e27d5eSDaniel Borkmann clear_token = ipv6_addr_any(token); 538647e27d5eSDaniel Borkmann if (clear_token) 538747e27d5eSDaniel Borkmann goto update_lft; 538847e27d5eSDaniel Borkmann 5389dc848292SHannes Frederic Sowa if (!idev->dead && (idev->if_flags & IF_READY) && 5390dc848292SHannes Frederic Sowa !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE | 5391dc848292SHannes Frederic Sowa IFA_F_OPTIMISTIC)) { 5392fc403832SDaniel Borkmann /* If we're not ready, then normal ifup will take care 5393fc403832SDaniel Borkmann * of this. Otherwise, we need to request our rs here. 5394fc403832SDaniel Borkmann */ 5395f53adae4SDaniel Borkmann ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters); 5396fc403832SDaniel Borkmann update_rs = true; 5397fc403832SDaniel Borkmann } 5398f53adae4SDaniel Borkmann 539947e27d5eSDaniel Borkmann update_lft: 5400f53adae4SDaniel Borkmann write_lock_bh(&idev->lock); 5401fc403832SDaniel Borkmann 540277ecaaceSHannes Frederic Sowa if (update_rs) { 5403f53adae4SDaniel Borkmann idev->if_flags |= IF_RS_SENT; 5404bd11f074SMaciej Żenczykowski idev->rs_interval = rfc3315_s14_backoff_init( 5405bd11f074SMaciej Żenczykowski idev->cnf.rtr_solicit_interval); 540677ecaaceSHannes Frederic Sowa idev->rs_probes = 1; 5407bd11f074SMaciej Żenczykowski addrconf_mod_rs_timer(idev, idev->rs_interval); 540877ecaaceSHannes Frederic Sowa } 5409f53adae4SDaniel Borkmann 5410f53adae4SDaniel Borkmann /* Well, that's kinda nasty ... */ 5411f53adae4SDaniel Borkmann list_for_each_entry(ifp, &idev->addr_list, if_list) { 5412f53adae4SDaniel Borkmann spin_lock(&ifp->lock); 5413617fe29dSDaniel Borkmann if (ifp->tokenized) { 5414f53adae4SDaniel Borkmann ifp->valid_lft = 0; 5415f53adae4SDaniel Borkmann ifp->prefered_lft = 0; 5416f53adae4SDaniel Borkmann } 5417f53adae4SDaniel Borkmann spin_unlock(&ifp->lock); 5418f53adae4SDaniel Borkmann } 5419f53adae4SDaniel Borkmann 5420f53adae4SDaniel Borkmann write_unlock_bh(&idev->lock); 5421b2ed64a9SLubomir Rintel inet6_ifinfo_notify(RTM_NEWLINK, idev); 5422c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 5423f53adae4SDaniel Borkmann return 0; 5424f53adae4SDaniel Borkmann } 5425f53adae4SDaniel Borkmann 542611b1f828SDaniel Borkmann static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = { 542711b1f828SDaniel Borkmann [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 }, 542811b1f828SDaniel Borkmann [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) }, 542911b1f828SDaniel Borkmann }; 543011b1f828SDaniel Borkmann 543111b1f828SDaniel Borkmann static int inet6_validate_link_af(const struct net_device *dev, 543211b1f828SDaniel Borkmann const struct nlattr *nla) 543311b1f828SDaniel Borkmann { 543411b1f828SDaniel Borkmann struct nlattr *tb[IFLA_INET6_MAX + 1]; 543511b1f828SDaniel Borkmann 543611b1f828SDaniel Borkmann if (dev && !__in6_dev_get(dev)) 543711b1f828SDaniel Borkmann return -EAFNOSUPPORT; 543811b1f828SDaniel Borkmann 5439fceb6435SJohannes Berg return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy, 5440fceb6435SJohannes Berg NULL); 544111b1f828SDaniel Borkmann } 544211b1f828SDaniel Borkmann 5443d35a00b8SFelix Jia static int check_addr_gen_mode(int mode) 5444d35a00b8SFelix Jia { 5445d35a00b8SFelix Jia if (mode != IN6_ADDR_GEN_MODE_EUI64 && 5446d35a00b8SFelix Jia mode != IN6_ADDR_GEN_MODE_NONE && 5447d35a00b8SFelix Jia mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY && 5448d35a00b8SFelix Jia mode != IN6_ADDR_GEN_MODE_RANDOM) 5449d35a00b8SFelix Jia return -EINVAL; 5450d35a00b8SFelix Jia return 1; 5451d35a00b8SFelix Jia } 5452d35a00b8SFelix Jia 5453d35a00b8SFelix Jia static int check_stable_privacy(struct inet6_dev *idev, struct net *net, 5454d35a00b8SFelix Jia int mode) 5455d35a00b8SFelix Jia { 5456d35a00b8SFelix Jia if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY && 5457d35a00b8SFelix Jia !idev->cnf.stable_secret.initialized && 5458d35a00b8SFelix Jia !net->ipv6.devconf_dflt->stable_secret.initialized) 5459d35a00b8SFelix Jia return -EINVAL; 5460d35a00b8SFelix Jia return 1; 5461d35a00b8SFelix Jia } 5462d35a00b8SFelix Jia 5463f53adae4SDaniel Borkmann static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla) 5464f53adae4SDaniel Borkmann { 5465f53adae4SDaniel Borkmann int err = -EINVAL; 5466f53adae4SDaniel Borkmann struct inet6_dev *idev = __in6_dev_get(dev); 5467f53adae4SDaniel Borkmann struct nlattr *tb[IFLA_INET6_MAX + 1]; 5468f53adae4SDaniel Borkmann 5469f53adae4SDaniel Borkmann if (!idev) 5470f53adae4SDaniel Borkmann return -EAFNOSUPPORT; 5471f53adae4SDaniel Borkmann 5472fceb6435SJohannes Berg if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL, NULL) < 0) 5473f53adae4SDaniel Borkmann BUG(); 5474f53adae4SDaniel Borkmann 5475bc91b0f0SJiri Pirko if (tb[IFLA_INET6_TOKEN]) { 5476f53adae4SDaniel Borkmann err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN])); 5477bc91b0f0SJiri Pirko if (err) 5478bc91b0f0SJiri Pirko return err; 5479bc91b0f0SJiri Pirko } 5480bc91b0f0SJiri Pirko 5481bc91b0f0SJiri Pirko if (tb[IFLA_INET6_ADDR_GEN_MODE]) { 5482bc91b0f0SJiri Pirko u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]); 5483bc91b0f0SJiri Pirko 5484d35a00b8SFelix Jia if (check_addr_gen_mode(mode) < 0 || 5485d35a00b8SFelix Jia check_stable_privacy(idev, dev_net(dev), mode) < 0) 5486bc91b0f0SJiri Pirko return -EINVAL; 5487622c81d5SHannes Frederic Sowa 5488d35a00b8SFelix Jia idev->cnf.addr_gen_mode = mode; 5489bc91b0f0SJiri Pirko err = 0; 5490bc91b0f0SJiri Pirko } 5491f53adae4SDaniel Borkmann 5492f53adae4SDaniel Borkmann return err; 5493f53adae4SDaniel Borkmann } 5494f53adae4SDaniel Borkmann 5495b382b191SThomas Graf static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev, 549615e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 5497b382b191SThomas Graf { 5498b382b191SThomas Graf struct net_device *dev = idev->dev; 5499b382b191SThomas Graf struct ifinfomsg *hdr; 5500b382b191SThomas Graf struct nlmsghdr *nlh; 5501b382b191SThomas Graf void *protoinfo; 5502b382b191SThomas Graf 550315e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags); 550463159f29SIan Morris if (!nlh) 5505b382b191SThomas Graf return -EMSGSIZE; 5506b382b191SThomas Graf 5507b382b191SThomas Graf hdr = nlmsg_data(nlh); 5508b382b191SThomas Graf hdr->ifi_family = AF_INET6; 5509b382b191SThomas Graf hdr->__ifi_pad = 0; 5510b382b191SThomas Graf hdr->ifi_type = dev->type; 5511b382b191SThomas Graf hdr->ifi_index = dev->ifindex; 5512b382b191SThomas Graf hdr->ifi_flags = dev_get_flags(dev); 5513b382b191SThomas Graf hdr->ifi_change = 0; 5514b382b191SThomas Graf 5515c78679e8SDavid S. Miller if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 5516c78679e8SDavid S. Miller (dev->addr_len && 5517c78679e8SDavid S. Miller nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 5518c78679e8SDavid S. Miller nla_put_u32(skb, IFLA_MTU, dev->mtu) || 5519a54acb3aSNicolas Dichtel (dev->ifindex != dev_get_iflink(dev) && 55200344338bSAndy Gospodarek nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) || 55210344338bSAndy Gospodarek nla_put_u8(skb, IFLA_OPERSTATE, 55220344338bSAndy Gospodarek netif_running(dev) ? dev->operstate : IF_OPER_DOWN)) 5523c78679e8SDavid S. Miller goto nla_put_failure; 5524b382b191SThomas Graf protoinfo = nla_nest_start(skb, IFLA_PROTINFO); 552563159f29SIan Morris if (!protoinfo) 5526b382b191SThomas Graf goto nla_put_failure; 5527b382b191SThomas Graf 5528d5566fd7SSowmini Varadhan if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0) 5529b382b191SThomas Graf goto nla_put_failure; 5530b382b191SThomas Graf 553104561c1fSThomas Graf nla_nest_end(skb, protoinfo); 5532053c095aSJohannes Berg nlmsg_end(skb, nlh); 5533053c095aSJohannes Berg return 0; 55341da177e4SLinus Torvalds 553504561c1fSThomas Graf nla_put_failure: 553626932566SPatrick McHardy nlmsg_cancel(skb, nlh); 553726932566SPatrick McHardy return -EMSGSIZE; 55381da177e4SLinus Torvalds } 55391da177e4SLinus Torvalds 55401da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 55411da177e4SLinus Torvalds { 55423b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 554384d2697dSEric Dumazet int h, s_h; 5544434a8a58SDavid S. Miller int idx = 0, s_idx; 55451da177e4SLinus Torvalds struct net_device *dev; 55461da177e4SLinus Torvalds struct inet6_dev *idev; 554784d2697dSEric Dumazet struct hlist_head *head; 55481da177e4SLinus Torvalds 554984d2697dSEric Dumazet s_h = cb->args[0]; 555084d2697dSEric Dumazet s_idx = cb->args[1]; 555184d2697dSEric Dumazet 555284d2697dSEric Dumazet rcu_read_lock(); 555384d2697dSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 55547562f876SPavel Emelianov idx = 0; 555584d2697dSEric Dumazet head = &net->dev_index_head[h]; 5556b67bfe0dSSasha Levin hlist_for_each_entry_rcu(dev, head, index_hlist) { 55571da177e4SLinus Torvalds if (idx < s_idx) 55587562f876SPavel Emelianov goto cont; 555984d2697dSEric Dumazet idev = __in6_dev_get(dev); 556084d2697dSEric Dumazet if (!idev) 55617562f876SPavel Emelianov goto cont; 556284d2697dSEric Dumazet if (inet6_fill_ifinfo(skb, idev, 556315e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 556484d2697dSEric Dumazet cb->nlh->nlmsg_seq, 5565053c095aSJohannes Berg RTM_NEWLINK, NLM_F_MULTI) < 0) 556684d2697dSEric Dumazet goto out; 55677562f876SPavel Emelianov cont: 55687562f876SPavel Emelianov idx++; 55691da177e4SLinus Torvalds } 557084d2697dSEric Dumazet } 557184d2697dSEric Dumazet out: 557284d2697dSEric Dumazet rcu_read_unlock(); 557384d2697dSEric Dumazet cb->args[1] = idx; 557484d2697dSEric Dumazet cb->args[0] = h; 55751da177e4SLinus Torvalds 55761da177e4SLinus Torvalds return skb->len; 55771da177e4SLinus Torvalds } 55781da177e4SLinus Torvalds 55791da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev) 55801da177e4SLinus Torvalds { 55811da177e4SLinus Torvalds struct sk_buff *skb; 5582c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 55838d7a76c9SThomas Graf int err = -ENOBUFS; 55841da177e4SLinus Torvalds 5585339bf98fSThomas Graf skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC); 558663159f29SIan Morris if (!skb) 55878d7a76c9SThomas Graf goto errout; 55888d7a76c9SThomas Graf 55898d7a76c9SThomas Graf err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0); 559026932566SPatrick McHardy if (err < 0) { 559126932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */ 559226932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 559326932566SPatrick McHardy kfree_skb(skb); 559426932566SPatrick McHardy goto errout; 559526932566SPatrick McHardy } 55965f75a104SNicolas Dichtel rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC); 55971ce85fe4SPablo Neira Ayuso return; 55988d7a76c9SThomas Graf errout: 55998d7a76c9SThomas Graf if (err < 0) 56005f75a104SNicolas Dichtel rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err); 56011da177e4SLinus Torvalds } 56021da177e4SLinus Torvalds 5603339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void) 5604339bf98fSThomas Graf { 5605339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct prefixmsg)) 5606339bf98fSThomas Graf + nla_total_size(sizeof(struct in6_addr)) 5607339bf98fSThomas Graf + nla_total_size(sizeof(struct prefix_cacheinfo)); 5608339bf98fSThomas Graf } 5609c5396a31SYOSHIFUJI Hideaki 56101da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev, 561115e47304SEric W. Biederman struct prefix_info *pinfo, u32 portid, u32 seq, 5612b6544c0bSJamal Hadi Salim int event, unsigned int flags) 56131da177e4SLinus Torvalds { 56141da177e4SLinus Torvalds struct prefixmsg *pmsg; 56151da177e4SLinus Torvalds struct nlmsghdr *nlh; 56161da177e4SLinus Torvalds struct prefix_cacheinfo ci; 56171da177e4SLinus Torvalds 561815e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags); 561963159f29SIan Morris if (!nlh) 562026932566SPatrick McHardy return -EMSGSIZE; 56216051e2f4SThomas Graf 56226051e2f4SThomas Graf pmsg = nlmsg_data(nlh); 56231da177e4SLinus Torvalds pmsg->prefix_family = AF_INET6; 56248a47077aSPatrick McHardy pmsg->prefix_pad1 = 0; 56258a47077aSPatrick McHardy pmsg->prefix_pad2 = 0; 56261da177e4SLinus Torvalds pmsg->prefix_ifindex = idev->dev->ifindex; 56271da177e4SLinus Torvalds pmsg->prefix_len = pinfo->prefix_len; 56281da177e4SLinus Torvalds pmsg->prefix_type = pinfo->type; 56298a47077aSPatrick McHardy pmsg->prefix_pad3 = 0; 56301da177e4SLinus Torvalds pmsg->prefix_flags = 0; 56311da177e4SLinus Torvalds if (pinfo->onlink) 56321da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_ONLINK; 56331da177e4SLinus Torvalds if (pinfo->autoconf) 56341da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_AUTOCONF; 56351da177e4SLinus Torvalds 5636c78679e8SDavid S. Miller if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix)) 5637c78679e8SDavid S. Miller goto nla_put_failure; 56381da177e4SLinus Torvalds ci.preferred_time = ntohl(pinfo->prefered); 56391da177e4SLinus Torvalds ci.valid_time = ntohl(pinfo->valid); 5640c78679e8SDavid S. Miller if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci)) 5641c78679e8SDavid S. Miller goto nla_put_failure; 5642053c095aSJohannes Berg nlmsg_end(skb, nlh); 5643053c095aSJohannes Berg return 0; 56441da177e4SLinus Torvalds 56456051e2f4SThomas Graf nla_put_failure: 564626932566SPatrick McHardy nlmsg_cancel(skb, nlh); 564726932566SPatrick McHardy return -EMSGSIZE; 56481da177e4SLinus Torvalds } 56491da177e4SLinus Torvalds 56501da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev, 56511da177e4SLinus Torvalds struct prefix_info *pinfo) 56521da177e4SLinus Torvalds { 56531da177e4SLinus Torvalds struct sk_buff *skb; 5654c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 56558c384bfaSThomas Graf int err = -ENOBUFS; 56561da177e4SLinus Torvalds 5657339bf98fSThomas Graf skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC); 565863159f29SIan Morris if (!skb) 56598c384bfaSThomas Graf goto errout; 56608c384bfaSThomas Graf 56618c384bfaSThomas Graf err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0); 566226932566SPatrick McHardy if (err < 0) { 566326932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */ 566426932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 566526932566SPatrick McHardy kfree_skb(skb); 566626932566SPatrick McHardy goto errout; 566726932566SPatrick McHardy } 56681ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC); 56691ce85fe4SPablo Neira Ayuso return; 56708c384bfaSThomas Graf errout: 56718c384bfaSThomas Graf if (err < 0) 56726fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err); 56731da177e4SLinus Torvalds } 56741da177e4SLinus Torvalds 56751da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 56761da177e4SLinus Torvalds { 567763998ac2SNicolas Dichtel struct net *net = dev_net(ifp->idev->dev); 567863998ac2SNicolas Dichtel 5679c15b1ccaSHannes Frederic Sowa if (event) 5680c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 5681c15b1ccaSHannes Frederic Sowa 56821da177e4SLinus Torvalds inet6_ifa_notify(event ? : RTM_NEWADDR, ifp); 56831da177e4SLinus Torvalds 56841da177e4SLinus Torvalds switch (event) { 56851da177e4SLinus Torvalds case RTM_NEWADDR: 568695c385b4SNeil Horman /* 568795c385b4SNeil Horman * If the address was optimistic 568895c385b4SNeil Horman * we inserted the route at the start of 568995c385b4SNeil Horman * our DAD process, so we don't need 569095c385b4SNeil Horman * to do it again 569195c385b4SNeil Horman */ 569293c2fb25SDavid Ahern if (!rcu_access_pointer(ifp->rt->fib6_node)) 5693afb1d4b5SDavid Ahern ip6_ins_rt(net, ifp->rt); 56941da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 56951da177e4SLinus Torvalds addrconf_join_anycast(ifp); 56967996c799SCong Wang if (!ipv6_addr_any(&ifp->peer_addr)) 56978308f3ffSDavid Ahern addrconf_prefix_route(&ifp->peer_addr, 128, 0, 5698acb54e3cSDavid Ahern ifp->idev->dev, 0, 0, 569927b10608SDavid Ahern GFP_ATOMIC); 57001da177e4SLinus Torvalds break; 57011da177e4SLinus Torvalds case RTM_DELADDR: 57021da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 57031da177e4SLinus Torvalds addrconf_leave_anycast(ifp); 57041da177e4SLinus Torvalds addrconf_leave_solict(ifp->idev, &ifp->addr); 57057996c799SCong Wang if (!ipv6_addr_any(&ifp->peer_addr)) { 57068d1c802bSDavid Ahern struct fib6_info *rt; 5707caeaba79SNicolas Dichtel 5708e7478dfcSNicolas Dichtel rt = addrconf_get_prefix_route(&ifp->peer_addr, 128, 5709e7478dfcSNicolas Dichtel ifp->idev->dev, 0, 0); 57108e3d5be7SMartin KaFai Lau if (rt) 5711afb1d4b5SDavid Ahern ip6_del_rt(net, rt); 5712caeaba79SNicolas Dichtel } 571338bd10c4SDavid Ahern if (ifp->rt) { 5714afb1d4b5SDavid Ahern ip6_del_rt(net, ifp->rt); 571593531c67SDavid Ahern ifp->rt = NULL; 571638bd10c4SDavid Ahern } 5717705f1c86SHannes Frederic Sowa rt_genid_bump_ipv6(net); 57181da177e4SLinus Torvalds break; 57191da177e4SLinus Torvalds } 572063998ac2SNicolas Dichtel atomic_inc(&net->ipv6.dev_addr_genid); 57211da177e4SLinus Torvalds } 57221da177e4SLinus Torvalds 57231da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 57241da177e4SLinus Torvalds { 57258814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 57261da177e4SLinus Torvalds if (likely(ifp->idev->dead == 0)) 57271da177e4SLinus Torvalds __ipv6_ifa_notify(event, ifp); 57288814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 57291da177e4SLinus Torvalds } 57301da177e4SLinus Torvalds 57311da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 57321da177e4SLinus Torvalds 57331da177e4SLinus Torvalds static 5734fe2c6338SJoe Perches int addrconf_sysctl_forward(struct ctl_table *ctl, int write, 57351da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 57361da177e4SLinus Torvalds { 57371da177e4SLinus Torvalds int *valp = ctl->data; 57381da177e4SLinus Torvalds int val = *valp; 573988af182eSEric W. Biederman loff_t pos = *ppos; 5740fe2c6338SJoe Perches struct ctl_table lctl; 57411da177e4SLinus Torvalds int ret; 57421da177e4SLinus Torvalds 5743013d97e9SFrancesco Ruggeri /* 5744013d97e9SFrancesco Ruggeri * ctl->data points to idev->cnf.forwarding, we should 5745013d97e9SFrancesco Ruggeri * not modify it until we get the rtnl lock. 5746013d97e9SFrancesco Ruggeri */ 5747013d97e9SFrancesco Ruggeri lctl = *ctl; 5748013d97e9SFrancesco Ruggeri lctl.data = &val; 5749013d97e9SFrancesco Ruggeri 5750013d97e9SFrancesco Ruggeri ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 57511da177e4SLinus Torvalds 5752c8fecf22SPavel Emelyanov if (write) 5753b325fddbSStephen Hemminger ret = addrconf_fixup_forwarding(ctl, valp, val); 575488af182eSEric W. Biederman if (ret) 575588af182eSEric W. Biederman *ppos = pos; 57561da177e4SLinus Torvalds return ret; 57571da177e4SLinus Torvalds } 57581da177e4SLinus Torvalds 575977751427SMarcelo Leitner static 576077751427SMarcelo Leitner int addrconf_sysctl_mtu(struct ctl_table *ctl, int write, 576177751427SMarcelo Leitner void __user *buffer, size_t *lenp, loff_t *ppos) 576277751427SMarcelo Leitner { 576377751427SMarcelo Leitner struct inet6_dev *idev = ctl->extra1; 576477751427SMarcelo Leitner int min_mtu = IPV6_MIN_MTU; 576577751427SMarcelo Leitner struct ctl_table lctl; 576677751427SMarcelo Leitner 576777751427SMarcelo Leitner lctl = *ctl; 576877751427SMarcelo Leitner lctl.extra1 = &min_mtu; 576977751427SMarcelo Leitner lctl.extra2 = idev ? &idev->dev->mtu : NULL; 577077751427SMarcelo Leitner 577177751427SMarcelo Leitner return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos); 577277751427SMarcelo Leitner } 577377751427SMarcelo Leitner 577456d417b1SBrian Haley static void dev_disable_change(struct inet6_dev *idev) 577556d417b1SBrian Haley { 577675538c2bSCong Wang struct netdev_notifier_info info; 577775538c2bSCong Wang 577856d417b1SBrian Haley if (!idev || !idev->dev) 577956d417b1SBrian Haley return; 578056d417b1SBrian Haley 578175538c2bSCong Wang netdev_notifier_info_init(&info, idev->dev); 578256d417b1SBrian Haley if (idev->cnf.disable_ipv6) 578375538c2bSCong Wang addrconf_notify(NULL, NETDEV_DOWN, &info); 578456d417b1SBrian Haley else 578575538c2bSCong Wang addrconf_notify(NULL, NETDEV_UP, &info); 578656d417b1SBrian Haley } 578756d417b1SBrian Haley 578856d417b1SBrian Haley static void addrconf_disable_change(struct net *net, __s32 newf) 578956d417b1SBrian Haley { 579056d417b1SBrian Haley struct net_device *dev; 579156d417b1SBrian Haley struct inet6_dev *idev; 579256d417b1SBrian Haley 579303e4deffSKefeng Wang for_each_netdev(net, dev) { 579456d417b1SBrian Haley idev = __in6_dev_get(dev); 579556d417b1SBrian Haley if (idev) { 579656d417b1SBrian Haley int changed = (!idev->cnf.disable_ipv6) ^ (!newf); 579756d417b1SBrian Haley idev->cnf.disable_ipv6 = newf; 579856d417b1SBrian Haley if (changed) 579956d417b1SBrian Haley dev_disable_change(idev); 580056d417b1SBrian Haley } 580156d417b1SBrian Haley } 580256d417b1SBrian Haley } 580356d417b1SBrian Haley 5804013d97e9SFrancesco Ruggeri static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf) 580556d417b1SBrian Haley { 580656d417b1SBrian Haley struct net *net; 5807013d97e9SFrancesco Ruggeri int old; 5808013d97e9SFrancesco Ruggeri 5809013d97e9SFrancesco Ruggeri if (!rtnl_trylock()) 5810013d97e9SFrancesco Ruggeri return restart_syscall(); 581156d417b1SBrian Haley 581256d417b1SBrian Haley net = (struct net *)table->extra2; 5813013d97e9SFrancesco Ruggeri old = *p; 5814013d97e9SFrancesco Ruggeri *p = newf; 581556d417b1SBrian Haley 5816013d97e9SFrancesco Ruggeri if (p == &net->ipv6.devconf_dflt->disable_ipv6) { 5817013d97e9SFrancesco Ruggeri rtnl_unlock(); 581856d417b1SBrian Haley return 0; 581988af182eSEric W. Biederman } 582056d417b1SBrian Haley 582156d417b1SBrian Haley if (p == &net->ipv6.devconf_all->disable_ipv6) { 582256d417b1SBrian Haley net->ipv6.devconf_dflt->disable_ipv6 = newf; 582356d417b1SBrian Haley addrconf_disable_change(net, newf); 5824013d97e9SFrancesco Ruggeri } else if ((!newf) ^ (!old)) 582556d417b1SBrian Haley dev_disable_change((struct inet6_dev *)table->extra1); 582656d417b1SBrian Haley 582756d417b1SBrian Haley rtnl_unlock(); 582856d417b1SBrian Haley return 0; 582956d417b1SBrian Haley } 583056d417b1SBrian Haley 583156d417b1SBrian Haley static 5832fe2c6338SJoe Perches int addrconf_sysctl_disable(struct ctl_table *ctl, int write, 583356d417b1SBrian Haley void __user *buffer, size_t *lenp, loff_t *ppos) 583456d417b1SBrian Haley { 583556d417b1SBrian Haley int *valp = ctl->data; 583656d417b1SBrian Haley int val = *valp; 583788af182eSEric W. Biederman loff_t pos = *ppos; 5838fe2c6338SJoe Perches struct ctl_table lctl; 583956d417b1SBrian Haley int ret; 584056d417b1SBrian Haley 5841013d97e9SFrancesco Ruggeri /* 5842013d97e9SFrancesco Ruggeri * ctl->data points to idev->cnf.disable_ipv6, we should 5843013d97e9SFrancesco Ruggeri * not modify it until we get the rtnl lock. 5844013d97e9SFrancesco Ruggeri */ 5845013d97e9SFrancesco Ruggeri lctl = *ctl; 5846013d97e9SFrancesco Ruggeri lctl.data = &val; 5847013d97e9SFrancesco Ruggeri 5848013d97e9SFrancesco Ruggeri ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 584956d417b1SBrian Haley 585056d417b1SBrian Haley if (write) 585156d417b1SBrian Haley ret = addrconf_disable_ipv6(ctl, valp, val); 585288af182eSEric W. Biederman if (ret) 585388af182eSEric W. Biederman *ppos = pos; 585456d417b1SBrian Haley return ret; 585556d417b1SBrian Haley } 585656d417b1SBrian Haley 5857c92d5491Sstephen hemminger static 5858c92d5491Sstephen hemminger int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write, 5859c92d5491Sstephen hemminger void __user *buffer, size_t *lenp, loff_t *ppos) 5860c92d5491Sstephen hemminger { 5861c92d5491Sstephen hemminger int *valp = ctl->data; 5862c92d5491Sstephen hemminger int ret; 5863c92d5491Sstephen hemminger int old, new; 5864c92d5491Sstephen hemminger 5865c92d5491Sstephen hemminger old = *valp; 5866c92d5491Sstephen hemminger ret = proc_dointvec(ctl, write, buffer, lenp, ppos); 5867c92d5491Sstephen hemminger new = *valp; 5868c92d5491Sstephen hemminger 5869c92d5491Sstephen hemminger if (write && old != new) { 5870c92d5491Sstephen hemminger struct net *net = ctl->extra2; 5871c92d5491Sstephen hemminger 5872c92d5491Sstephen hemminger if (!rtnl_trylock()) 5873c92d5491Sstephen hemminger return restart_syscall(); 5874c92d5491Sstephen hemminger 5875c92d5491Sstephen hemminger if (valp == &net->ipv6.devconf_dflt->proxy_ndp) 587685b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 587785b3daadSDavid Ahern NETCONFA_PROXY_NEIGH, 5878c92d5491Sstephen hemminger NETCONFA_IFINDEX_DEFAULT, 5879c92d5491Sstephen hemminger net->ipv6.devconf_dflt); 5880c92d5491Sstephen hemminger else if (valp == &net->ipv6.devconf_all->proxy_ndp) 588185b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 588285b3daadSDavid Ahern NETCONFA_PROXY_NEIGH, 5883c92d5491Sstephen hemminger NETCONFA_IFINDEX_ALL, 5884c92d5491Sstephen hemminger net->ipv6.devconf_all); 5885c92d5491Sstephen hemminger else { 5886c92d5491Sstephen hemminger struct inet6_dev *idev = ctl->extra1; 5887c92d5491Sstephen hemminger 588885b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 588985b3daadSDavid Ahern NETCONFA_PROXY_NEIGH, 5890c92d5491Sstephen hemminger idev->dev->ifindex, 5891c92d5491Sstephen hemminger &idev->cnf); 5892c92d5491Sstephen hemminger } 5893c92d5491Sstephen hemminger rtnl_unlock(); 5894c92d5491Sstephen hemminger } 5895c92d5491Sstephen hemminger 5896c92d5491Sstephen hemminger return ret; 5897c92d5491Sstephen hemminger } 5898c92d5491Sstephen hemminger 5899d35a00b8SFelix Jia static int addrconf_sysctl_addr_gen_mode(struct ctl_table *ctl, int write, 5900d35a00b8SFelix Jia void __user *buffer, size_t *lenp, 5901d35a00b8SFelix Jia loff_t *ppos) 5902d35a00b8SFelix Jia { 5903d35a00b8SFelix Jia int ret = 0; 5904d35a00b8SFelix Jia int new_val; 5905d35a00b8SFelix Jia struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1; 5906d35a00b8SFelix Jia struct net *net = (struct net *)ctl->extra2; 5907d35a00b8SFelix Jia 59088c171d6cSFelix Jia if (!rtnl_trylock()) 59098c171d6cSFelix Jia return restart_syscall(); 59108c171d6cSFelix Jia 5911d35a00b8SFelix Jia ret = proc_dointvec(ctl, write, buffer, lenp, ppos); 5912d35a00b8SFelix Jia 5913d35a00b8SFelix Jia if (write) { 5914d35a00b8SFelix Jia new_val = *((int *)ctl->data); 5915d35a00b8SFelix Jia 59168c171d6cSFelix Jia if (check_addr_gen_mode(new_val) < 0) { 59178c171d6cSFelix Jia ret = -EINVAL; 59188c171d6cSFelix Jia goto out; 59198c171d6cSFelix Jia } 5920d35a00b8SFelix Jia 5921d35a00b8SFelix Jia /* request for default */ 5922d35a00b8SFelix Jia if (&net->ipv6.devconf_dflt->addr_gen_mode == ctl->data) { 5923d35a00b8SFelix Jia ipv6_devconf_dflt.addr_gen_mode = new_val; 5924d35a00b8SFelix Jia 5925d35a00b8SFelix Jia /* request for individual net device */ 5926d35a00b8SFelix Jia } else { 5927d35a00b8SFelix Jia if (!idev) 59288c171d6cSFelix Jia goto out; 5929d35a00b8SFelix Jia 59308c171d6cSFelix Jia if (check_stable_privacy(idev, net, new_val) < 0) { 59318c171d6cSFelix Jia ret = -EINVAL; 59328c171d6cSFelix Jia goto out; 59338c171d6cSFelix Jia } 5934d35a00b8SFelix Jia 5935d35a00b8SFelix Jia if (idev->cnf.addr_gen_mode != new_val) { 5936d35a00b8SFelix Jia idev->cnf.addr_gen_mode = new_val; 5937d35a00b8SFelix Jia addrconf_dev_config(idev->dev); 59388c171d6cSFelix Jia } 59398c171d6cSFelix Jia } 59408c171d6cSFelix Jia } 59418c171d6cSFelix Jia 59428c171d6cSFelix Jia out: 5943d35a00b8SFelix Jia rtnl_unlock(); 5944d35a00b8SFelix Jia 5945d35a00b8SFelix Jia return ret; 5946d35a00b8SFelix Jia } 5947d35a00b8SFelix Jia 59483d1bec99SHannes Frederic Sowa static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write, 59493d1bec99SHannes Frederic Sowa void __user *buffer, size_t *lenp, 59503d1bec99SHannes Frederic Sowa loff_t *ppos) 59513d1bec99SHannes Frederic Sowa { 59523d1bec99SHannes Frederic Sowa int err; 59533d1bec99SHannes Frederic Sowa struct in6_addr addr; 59543d1bec99SHannes Frederic Sowa char str[IPV6_MAX_STRLEN]; 59553d1bec99SHannes Frederic Sowa struct ctl_table lctl = *ctl; 5956622c81d5SHannes Frederic Sowa struct net *net = ctl->extra2; 59573d1bec99SHannes Frederic Sowa struct ipv6_stable_secret *secret = ctl->data; 59583d1bec99SHannes Frederic Sowa 5959622c81d5SHannes Frederic Sowa if (&net->ipv6.devconf_all->stable_secret == ctl->data) 5960622c81d5SHannes Frederic Sowa return -EIO; 5961622c81d5SHannes Frederic Sowa 59623d1bec99SHannes Frederic Sowa lctl.maxlen = IPV6_MAX_STRLEN; 59633d1bec99SHannes Frederic Sowa lctl.data = str; 59643d1bec99SHannes Frederic Sowa 59653d1bec99SHannes Frederic Sowa if (!rtnl_trylock()) 59663d1bec99SHannes Frederic Sowa return restart_syscall(); 59673d1bec99SHannes Frederic Sowa 59683d1bec99SHannes Frederic Sowa if (!write && !secret->initialized) { 59693d1bec99SHannes Frederic Sowa err = -EIO; 59703d1bec99SHannes Frederic Sowa goto out; 59713d1bec99SHannes Frederic Sowa } 59723d1bec99SHannes Frederic Sowa 59735449a5caSWANG Cong err = snprintf(str, sizeof(str), "%pI6", &secret->secret); 59743d1bec99SHannes Frederic Sowa if (err >= sizeof(str)) { 59753d1bec99SHannes Frederic Sowa err = -EIO; 59763d1bec99SHannes Frederic Sowa goto out; 59773d1bec99SHannes Frederic Sowa } 59783d1bec99SHannes Frederic Sowa 59793d1bec99SHannes Frederic Sowa err = proc_dostring(&lctl, write, buffer, lenp, ppos); 59803d1bec99SHannes Frederic Sowa if (err || !write) 59813d1bec99SHannes Frederic Sowa goto out; 59823d1bec99SHannes Frederic Sowa 59833d1bec99SHannes Frederic Sowa if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) { 59843d1bec99SHannes Frederic Sowa err = -EIO; 59853d1bec99SHannes Frederic Sowa goto out; 59863d1bec99SHannes Frederic Sowa } 59873d1bec99SHannes Frederic Sowa 59883d1bec99SHannes Frederic Sowa secret->initialized = true; 59893d1bec99SHannes Frederic Sowa secret->secret = addr; 59903d1bec99SHannes Frederic Sowa 5991622c81d5SHannes Frederic Sowa if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) { 5992622c81d5SHannes Frederic Sowa struct net_device *dev; 5993622c81d5SHannes Frederic Sowa 5994622c81d5SHannes Frederic Sowa for_each_netdev(net, dev) { 5995622c81d5SHannes Frederic Sowa struct inet6_dev *idev = __in6_dev_get(dev); 5996622c81d5SHannes Frederic Sowa 5997622c81d5SHannes Frederic Sowa if (idev) { 5998d35a00b8SFelix Jia idev->cnf.addr_gen_mode = 5999622c81d5SHannes Frederic Sowa IN6_ADDR_GEN_MODE_STABLE_PRIVACY; 6000622c81d5SHannes Frederic Sowa } 6001622c81d5SHannes Frederic Sowa } 6002622c81d5SHannes Frederic Sowa } else { 6003622c81d5SHannes Frederic Sowa struct inet6_dev *idev = ctl->extra1; 6004622c81d5SHannes Frederic Sowa 6005d35a00b8SFelix Jia idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY; 6006622c81d5SHannes Frederic Sowa } 6007622c81d5SHannes Frederic Sowa 60083d1bec99SHannes Frederic Sowa out: 60093d1bec99SHannes Frederic Sowa rtnl_unlock(); 60103d1bec99SHannes Frederic Sowa 60113d1bec99SHannes Frederic Sowa return err; 60123d1bec99SHannes Frederic Sowa } 6013c92d5491Sstephen hemminger 601435103d11SAndy Gospodarek static 601535103d11SAndy Gospodarek int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl, 601635103d11SAndy Gospodarek int write, 601735103d11SAndy Gospodarek void __user *buffer, 601835103d11SAndy Gospodarek size_t *lenp, 601935103d11SAndy Gospodarek loff_t *ppos) 602035103d11SAndy Gospodarek { 602135103d11SAndy Gospodarek int *valp = ctl->data; 602235103d11SAndy Gospodarek int val = *valp; 602335103d11SAndy Gospodarek loff_t pos = *ppos; 602435103d11SAndy Gospodarek struct ctl_table lctl; 602535103d11SAndy Gospodarek int ret; 602635103d11SAndy Gospodarek 602735103d11SAndy Gospodarek /* ctl->data points to idev->cnf.ignore_routes_when_linkdown 602835103d11SAndy Gospodarek * we should not modify it until we get the rtnl lock. 602935103d11SAndy Gospodarek */ 603035103d11SAndy Gospodarek lctl = *ctl; 603135103d11SAndy Gospodarek lctl.data = &val; 603235103d11SAndy Gospodarek 603335103d11SAndy Gospodarek ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 603435103d11SAndy Gospodarek 603535103d11SAndy Gospodarek if (write) 603635103d11SAndy Gospodarek ret = addrconf_fixup_linkdown(ctl, valp, val); 603735103d11SAndy Gospodarek if (ret) 603835103d11SAndy Gospodarek *ppos = pos; 603935103d11SAndy Gospodarek return ret; 604035103d11SAndy Gospodarek } 604135103d11SAndy Gospodarek 6042df789fe7SDavid Forster static 6043df789fe7SDavid Forster void addrconf_set_nopolicy(struct rt6_info *rt, int action) 6044df789fe7SDavid Forster { 6045df789fe7SDavid Forster if (rt) { 6046df789fe7SDavid Forster if (action) 6047df789fe7SDavid Forster rt->dst.flags |= DST_NOPOLICY; 6048df789fe7SDavid Forster else 6049df789fe7SDavid Forster rt->dst.flags &= ~DST_NOPOLICY; 6050df789fe7SDavid Forster } 6051df789fe7SDavid Forster } 6052df789fe7SDavid Forster 6053df789fe7SDavid Forster static 6054df789fe7SDavid Forster void addrconf_disable_policy_idev(struct inet6_dev *idev, int val) 6055df789fe7SDavid Forster { 6056df789fe7SDavid Forster struct inet6_ifaddr *ifa; 6057df789fe7SDavid Forster 6058df789fe7SDavid Forster read_lock_bh(&idev->lock); 6059df789fe7SDavid Forster list_for_each_entry(ifa, &idev->addr_list, if_list) { 6060df789fe7SDavid Forster spin_lock(&ifa->lock); 6061df789fe7SDavid Forster if (ifa->rt) { 60628d1c802bSDavid Ahern struct fib6_info *rt = ifa->rt; 6063df789fe7SDavid Forster int cpu; 6064df789fe7SDavid Forster 606566f5d6ceSWei Wang rcu_read_lock(); 60663b6761d1SDavid Ahern ifa->rt->dst_nopolicy = val ? true : false; 6067df789fe7SDavid Forster if (rt->rt6i_pcpu) { 6068df789fe7SDavid Forster for_each_possible_cpu(cpu) { 6069df789fe7SDavid Forster struct rt6_info **rtp; 6070df789fe7SDavid Forster 6071df789fe7SDavid Forster rtp = per_cpu_ptr(rt->rt6i_pcpu, cpu); 6072df789fe7SDavid Forster addrconf_set_nopolicy(*rtp, val); 6073df789fe7SDavid Forster } 6074df789fe7SDavid Forster } 607566f5d6ceSWei Wang rcu_read_unlock(); 6076df789fe7SDavid Forster } 6077df789fe7SDavid Forster spin_unlock(&ifa->lock); 6078df789fe7SDavid Forster } 6079df789fe7SDavid Forster read_unlock_bh(&idev->lock); 6080df789fe7SDavid Forster } 6081df789fe7SDavid Forster 6082df789fe7SDavid Forster static 6083df789fe7SDavid Forster int addrconf_disable_policy(struct ctl_table *ctl, int *valp, int val) 6084df789fe7SDavid Forster { 6085df789fe7SDavid Forster struct inet6_dev *idev; 6086df789fe7SDavid Forster struct net *net; 6087df789fe7SDavid Forster 6088df789fe7SDavid Forster if (!rtnl_trylock()) 6089df789fe7SDavid Forster return restart_syscall(); 6090df789fe7SDavid Forster 6091df789fe7SDavid Forster *valp = val; 6092df789fe7SDavid Forster 6093df789fe7SDavid Forster net = (struct net *)ctl->extra2; 6094df789fe7SDavid Forster if (valp == &net->ipv6.devconf_dflt->disable_policy) { 6095df789fe7SDavid Forster rtnl_unlock(); 6096df789fe7SDavid Forster return 0; 6097df789fe7SDavid Forster } 6098df789fe7SDavid Forster 6099df789fe7SDavid Forster if (valp == &net->ipv6.devconf_all->disable_policy) { 6100df789fe7SDavid Forster struct net_device *dev; 6101df789fe7SDavid Forster 6102df789fe7SDavid Forster for_each_netdev(net, dev) { 6103df789fe7SDavid Forster idev = __in6_dev_get(dev); 6104df789fe7SDavid Forster if (idev) 6105df789fe7SDavid Forster addrconf_disable_policy_idev(idev, val); 6106df789fe7SDavid Forster } 6107df789fe7SDavid Forster } else { 6108df789fe7SDavid Forster idev = (struct inet6_dev *)ctl->extra1; 6109df789fe7SDavid Forster addrconf_disable_policy_idev(idev, val); 6110df789fe7SDavid Forster } 6111df789fe7SDavid Forster 6112df789fe7SDavid Forster rtnl_unlock(); 6113df789fe7SDavid Forster return 0; 6114df789fe7SDavid Forster } 6115df789fe7SDavid Forster 6116df789fe7SDavid Forster static 6117df789fe7SDavid Forster int addrconf_sysctl_disable_policy(struct ctl_table *ctl, int write, 6118df789fe7SDavid Forster void __user *buffer, size_t *lenp, 6119df789fe7SDavid Forster loff_t *ppos) 6120df789fe7SDavid Forster { 6121df789fe7SDavid Forster int *valp = ctl->data; 6122df789fe7SDavid Forster int val = *valp; 6123df789fe7SDavid Forster loff_t pos = *ppos; 6124df789fe7SDavid Forster struct ctl_table lctl; 6125df789fe7SDavid Forster int ret; 6126df789fe7SDavid Forster 6127df789fe7SDavid Forster lctl = *ctl; 6128df789fe7SDavid Forster lctl.data = &val; 6129df789fe7SDavid Forster ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 6130df789fe7SDavid Forster 6131df789fe7SDavid Forster if (write && (*valp != val)) 6132df789fe7SDavid Forster ret = addrconf_disable_policy(ctl, valp, val); 6133df789fe7SDavid Forster 6134df789fe7SDavid Forster if (ret) 6135df789fe7SDavid Forster *ppos = pos; 6136df789fe7SDavid Forster 6137df789fe7SDavid Forster return ret; 6138df789fe7SDavid Forster } 6139df789fe7SDavid Forster 6140cb4a4c69SMaciej Żenczykowski static int minus_one = -1; 61412210d6b2SMaciej Żenczykowski static const int zero = 0; 6142cb9e684eSMaciej Żenczykowski static const int one = 1; 6143cb9e684eSMaciej Żenczykowski static const int two_five_five = 255; 6144cb9e684eSMaciej Żenczykowski 6145607ea7cdSKonstantin Khlebnikov static const struct ctl_table addrconf_sysctl[] = { 61461da177e4SLinus Torvalds { 61471da177e4SLinus Torvalds .procname = "forwarding", 61481da177e4SLinus Torvalds .data = &ipv6_devconf.forwarding, 61491da177e4SLinus Torvalds .maxlen = sizeof(int), 61501da177e4SLinus Torvalds .mode = 0644, 61516d9f239aSAlexey Dobriyan .proc_handler = addrconf_sysctl_forward, 61521da177e4SLinus Torvalds }, 61531da177e4SLinus Torvalds { 61541da177e4SLinus Torvalds .procname = "hop_limit", 61551da177e4SLinus Torvalds .data = &ipv6_devconf.hop_limit, 61561da177e4SLinus Torvalds .maxlen = sizeof(int), 61571da177e4SLinus Torvalds .mode = 0644, 6158cb9e684eSMaciej Żenczykowski .proc_handler = proc_dointvec_minmax, 6159cb9e684eSMaciej Żenczykowski .extra1 = (void *)&one, 6160cb9e684eSMaciej Żenczykowski .extra2 = (void *)&two_five_five, 61611da177e4SLinus Torvalds }, 61621da177e4SLinus Torvalds { 61631da177e4SLinus Torvalds .procname = "mtu", 61641da177e4SLinus Torvalds .data = &ipv6_devconf.mtu6, 61651da177e4SLinus Torvalds .maxlen = sizeof(int), 61661da177e4SLinus Torvalds .mode = 0644, 616777751427SMarcelo Leitner .proc_handler = addrconf_sysctl_mtu, 61681da177e4SLinus Torvalds }, 61691da177e4SLinus Torvalds { 61701da177e4SLinus Torvalds .procname = "accept_ra", 61711da177e4SLinus Torvalds .data = &ipv6_devconf.accept_ra, 61721da177e4SLinus Torvalds .maxlen = sizeof(int), 61731da177e4SLinus Torvalds .mode = 0644, 61746d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 61751da177e4SLinus Torvalds }, 61761da177e4SLinus Torvalds { 61771da177e4SLinus Torvalds .procname = "accept_redirects", 61781da177e4SLinus Torvalds .data = &ipv6_devconf.accept_redirects, 61791da177e4SLinus Torvalds .maxlen = sizeof(int), 61801da177e4SLinus Torvalds .mode = 0644, 61816d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 61821da177e4SLinus Torvalds }, 61831da177e4SLinus Torvalds { 61841da177e4SLinus Torvalds .procname = "autoconf", 61851da177e4SLinus Torvalds .data = &ipv6_devconf.autoconf, 61861da177e4SLinus Torvalds .maxlen = sizeof(int), 61871da177e4SLinus Torvalds .mode = 0644, 61886d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 61891da177e4SLinus Torvalds }, 61901da177e4SLinus Torvalds { 61911da177e4SLinus Torvalds .procname = "dad_transmits", 61921da177e4SLinus Torvalds .data = &ipv6_devconf.dad_transmits, 61931da177e4SLinus Torvalds .maxlen = sizeof(int), 61941da177e4SLinus Torvalds .mode = 0644, 61956d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 61961da177e4SLinus Torvalds }, 61971da177e4SLinus Torvalds { 61981da177e4SLinus Torvalds .procname = "router_solicitations", 61991da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicits, 62001da177e4SLinus Torvalds .maxlen = sizeof(int), 62011da177e4SLinus Torvalds .mode = 0644, 6202cb4a4c69SMaciej Żenczykowski .proc_handler = proc_dointvec_minmax, 6203cb4a4c69SMaciej Żenczykowski .extra1 = &minus_one, 62041da177e4SLinus Torvalds }, 62051da177e4SLinus Torvalds { 62061da177e4SLinus Torvalds .procname = "router_solicitation_interval", 62071da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_interval, 62081da177e4SLinus Torvalds .maxlen = sizeof(int), 62091da177e4SLinus Torvalds .mode = 0644, 62106d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 62111da177e4SLinus Torvalds }, 62121da177e4SLinus Torvalds { 6213bd11f074SMaciej Żenczykowski .procname = "router_solicitation_max_interval", 6214bd11f074SMaciej Żenczykowski .data = &ipv6_devconf.rtr_solicit_max_interval, 6215bd11f074SMaciej Żenczykowski .maxlen = sizeof(int), 6216bd11f074SMaciej Żenczykowski .mode = 0644, 6217bd11f074SMaciej Żenczykowski .proc_handler = proc_dointvec_jiffies, 6218bd11f074SMaciej Żenczykowski }, 6219bd11f074SMaciej Żenczykowski { 62201da177e4SLinus Torvalds .procname = "router_solicitation_delay", 62211da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_delay, 62221da177e4SLinus Torvalds .maxlen = sizeof(int), 62231da177e4SLinus Torvalds .mode = 0644, 62246d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 62251da177e4SLinus Torvalds }, 62261da177e4SLinus Torvalds { 62271da177e4SLinus Torvalds .procname = "force_mld_version", 62281da177e4SLinus Torvalds .data = &ipv6_devconf.force_mld_version, 62291da177e4SLinus Torvalds .maxlen = sizeof(int), 62301da177e4SLinus Torvalds .mode = 0644, 62316d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 62321da177e4SLinus Torvalds }, 6233fc4eba58SHannes Frederic Sowa { 6234fc4eba58SHannes Frederic Sowa .procname = "mldv1_unsolicited_report_interval", 6235fc4eba58SHannes Frederic Sowa .data = 6236fc4eba58SHannes Frederic Sowa &ipv6_devconf.mldv1_unsolicited_report_interval, 6237fc4eba58SHannes Frederic Sowa .maxlen = sizeof(int), 6238fc4eba58SHannes Frederic Sowa .mode = 0644, 6239fc4eba58SHannes Frederic Sowa .proc_handler = proc_dointvec_ms_jiffies, 6240fc4eba58SHannes Frederic Sowa }, 6241fc4eba58SHannes Frederic Sowa { 6242fc4eba58SHannes Frederic Sowa .procname = "mldv2_unsolicited_report_interval", 6243fc4eba58SHannes Frederic Sowa .data = 6244fc4eba58SHannes Frederic Sowa &ipv6_devconf.mldv2_unsolicited_report_interval, 6245fc4eba58SHannes Frederic Sowa .maxlen = sizeof(int), 6246fc4eba58SHannes Frederic Sowa .mode = 0644, 6247fc4eba58SHannes Frederic Sowa .proc_handler = proc_dointvec_ms_jiffies, 6248fc4eba58SHannes Frederic Sowa }, 62491da177e4SLinus Torvalds { 62501da177e4SLinus Torvalds .procname = "use_tempaddr", 62511da177e4SLinus Torvalds .data = &ipv6_devconf.use_tempaddr, 62521da177e4SLinus Torvalds .maxlen = sizeof(int), 62531da177e4SLinus Torvalds .mode = 0644, 62546d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 62551da177e4SLinus Torvalds }, 62561da177e4SLinus Torvalds { 62571da177e4SLinus Torvalds .procname = "temp_valid_lft", 62581da177e4SLinus Torvalds .data = &ipv6_devconf.temp_valid_lft, 62591da177e4SLinus Torvalds .maxlen = sizeof(int), 62601da177e4SLinus Torvalds .mode = 0644, 62616d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 62621da177e4SLinus Torvalds }, 62631da177e4SLinus Torvalds { 62641da177e4SLinus Torvalds .procname = "temp_prefered_lft", 62651da177e4SLinus Torvalds .data = &ipv6_devconf.temp_prefered_lft, 62661da177e4SLinus Torvalds .maxlen = sizeof(int), 62671da177e4SLinus Torvalds .mode = 0644, 62686d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 62691da177e4SLinus Torvalds }, 62701da177e4SLinus Torvalds { 62711da177e4SLinus Torvalds .procname = "regen_max_retry", 62721da177e4SLinus Torvalds .data = &ipv6_devconf.regen_max_retry, 62731da177e4SLinus Torvalds .maxlen = sizeof(int), 62741da177e4SLinus Torvalds .mode = 0644, 62756d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 62761da177e4SLinus Torvalds }, 62771da177e4SLinus Torvalds { 62781da177e4SLinus Torvalds .procname = "max_desync_factor", 62791da177e4SLinus Torvalds .data = &ipv6_devconf.max_desync_factor, 62801da177e4SLinus Torvalds .maxlen = sizeof(int), 62811da177e4SLinus Torvalds .mode = 0644, 62826d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 62831da177e4SLinus Torvalds }, 62841da177e4SLinus Torvalds { 62851da177e4SLinus Torvalds .procname = "max_addresses", 62861da177e4SLinus Torvalds .data = &ipv6_devconf.max_addresses, 62871da177e4SLinus Torvalds .maxlen = sizeof(int), 62881da177e4SLinus Torvalds .mode = 0644, 62896d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 62901da177e4SLinus Torvalds }, 62911da177e4SLinus Torvalds { 629265f5c7c1SYOSHIFUJI Hideaki .procname = "accept_ra_defrtr", 629365f5c7c1SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_defrtr, 629465f5c7c1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 629565f5c7c1SYOSHIFUJI Hideaki .mode = 0644, 62966d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 629765f5c7c1SYOSHIFUJI Hideaki }, 629865f5c7c1SYOSHIFUJI Hideaki { 62998013d1d7SHangbin Liu .procname = "accept_ra_min_hop_limit", 63008013d1d7SHangbin Liu .data = &ipv6_devconf.accept_ra_min_hop_limit, 63018013d1d7SHangbin Liu .maxlen = sizeof(int), 63028013d1d7SHangbin Liu .mode = 0644, 63038013d1d7SHangbin Liu .proc_handler = proc_dointvec, 63048013d1d7SHangbin Liu }, 63058013d1d7SHangbin Liu { 6306c4fd30ebSYOSHIFUJI Hideaki .procname = "accept_ra_pinfo", 6307c4fd30ebSYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_pinfo, 6308c4fd30ebSYOSHIFUJI Hideaki .maxlen = sizeof(int), 6309c4fd30ebSYOSHIFUJI Hideaki .mode = 0644, 63106d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 6311c4fd30ebSYOSHIFUJI Hideaki }, 6312930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 6313930d6ff2SYOSHIFUJI Hideaki { 6314930d6ff2SYOSHIFUJI Hideaki .procname = "accept_ra_rtr_pref", 6315930d6ff2SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rtr_pref, 6316930d6ff2SYOSHIFUJI Hideaki .maxlen = sizeof(int), 6317930d6ff2SYOSHIFUJI Hideaki .mode = 0644, 63186d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 6319930d6ff2SYOSHIFUJI Hideaki }, 632052e16356SYOSHIFUJI Hideaki { 632152e16356SYOSHIFUJI Hideaki .procname = "router_probe_interval", 632252e16356SYOSHIFUJI Hideaki .data = &ipv6_devconf.rtr_probe_interval, 632352e16356SYOSHIFUJI Hideaki .maxlen = sizeof(int), 632452e16356SYOSHIFUJI Hideaki .mode = 0644, 63256d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 632652e16356SYOSHIFUJI Hideaki }, 6327fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 632809c884d4SYOSHIFUJI Hideaki { 6329bbea124bSJoel Scherpelz .procname = "accept_ra_rt_info_min_plen", 6330bbea124bSJoel Scherpelz .data = &ipv6_devconf.accept_ra_rt_info_min_plen, 6331bbea124bSJoel Scherpelz .maxlen = sizeof(int), 6332bbea124bSJoel Scherpelz .mode = 0644, 6333bbea124bSJoel Scherpelz .proc_handler = proc_dointvec, 6334bbea124bSJoel Scherpelz }, 6335bbea124bSJoel Scherpelz { 633609c884d4SYOSHIFUJI Hideaki .procname = "accept_ra_rt_info_max_plen", 633709c884d4SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rt_info_max_plen, 633809c884d4SYOSHIFUJI Hideaki .maxlen = sizeof(int), 633909c884d4SYOSHIFUJI Hideaki .mode = 0644, 63406d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 634109c884d4SYOSHIFUJI Hideaki }, 634209c884d4SYOSHIFUJI Hideaki #endif 6343930d6ff2SYOSHIFUJI Hideaki #endif 6344c4fd30ebSYOSHIFUJI Hideaki { 6345fbea49e1SYOSHIFUJI Hideaki .procname = "proxy_ndp", 6346fbea49e1SYOSHIFUJI Hideaki .data = &ipv6_devconf.proxy_ndp, 6347fbea49e1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 6348fbea49e1SYOSHIFUJI Hideaki .mode = 0644, 6349c92d5491Sstephen hemminger .proc_handler = addrconf_sysctl_proxy_ndp, 6350fbea49e1SYOSHIFUJI Hideaki }, 6351fbea49e1SYOSHIFUJI Hideaki { 63520bcbc926SYOSHIFUJI Hideaki .procname = "accept_source_route", 63530bcbc926SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_source_route, 63540bcbc926SYOSHIFUJI Hideaki .maxlen = sizeof(int), 63550bcbc926SYOSHIFUJI Hideaki .mode = 0644, 63566d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 63570bcbc926SYOSHIFUJI Hideaki }, 635895c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 635995c385b4SNeil Horman { 636095c385b4SNeil Horman .procname = "optimistic_dad", 636195c385b4SNeil Horman .data = &ipv6_devconf.optimistic_dad, 636295c385b4SNeil Horman .maxlen = sizeof(int), 636395c385b4SNeil Horman .mode = 0644, 63646d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 636595c385b4SNeil Horman }, 63667fd2561eSErik Kline { 63677fd2561eSErik Kline .procname = "use_optimistic", 63687fd2561eSErik Kline .data = &ipv6_devconf.use_optimistic, 63697fd2561eSErik Kline .maxlen = sizeof(int), 63707fd2561eSErik Kline .mode = 0644, 63717fd2561eSErik Kline .proc_handler = proc_dointvec, 63727fd2561eSErik Kline }, 637395c385b4SNeil Horman #endif 63747bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 63757bc570c8SYOSHIFUJI Hideaki { 63767bc570c8SYOSHIFUJI Hideaki .procname = "mc_forwarding", 63777bc570c8SYOSHIFUJI Hideaki .data = &ipv6_devconf.mc_forwarding, 63787bc570c8SYOSHIFUJI Hideaki .maxlen = sizeof(int), 6379a4e6db07SDavid S. Miller .mode = 0444, 63806d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 63817bc570c8SYOSHIFUJI Hideaki }, 63827bc570c8SYOSHIFUJI Hideaki #endif 63830bcbc926SYOSHIFUJI Hideaki { 6384778d80beSYOSHIFUJI Hideaki .procname = "disable_ipv6", 6385778d80beSYOSHIFUJI Hideaki .data = &ipv6_devconf.disable_ipv6, 6386778d80beSYOSHIFUJI Hideaki .maxlen = sizeof(int), 6387778d80beSYOSHIFUJI Hideaki .mode = 0644, 638856d417b1SBrian Haley .proc_handler = addrconf_sysctl_disable, 6389778d80beSYOSHIFUJI Hideaki }, 6390778d80beSYOSHIFUJI Hideaki { 63911b34be74SYOSHIFUJI Hideaki .procname = "accept_dad", 63921b34be74SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_dad, 63931b34be74SYOSHIFUJI Hideaki .maxlen = sizeof(int), 63941b34be74SYOSHIFUJI Hideaki .mode = 0644, 63956d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 63961b34be74SYOSHIFUJI Hideaki }, 63971b34be74SYOSHIFUJI Hideaki { 6398f7734fdfSOctavian Purdila .procname = "force_tllao", 6399f7734fdfSOctavian Purdila .data = &ipv6_devconf.force_tllao, 6400f7734fdfSOctavian Purdila .maxlen = sizeof(int), 6401f7734fdfSOctavian Purdila .mode = 0644, 6402f7734fdfSOctavian Purdila .proc_handler = proc_dointvec 6403f7734fdfSOctavian Purdila }, 6404f7734fdfSOctavian Purdila { 64055cb04436SHannes Frederic Sowa .procname = "ndisc_notify", 64065cb04436SHannes Frederic Sowa .data = &ipv6_devconf.ndisc_notify, 64075cb04436SHannes Frederic Sowa .maxlen = sizeof(int), 64085cb04436SHannes Frederic Sowa .mode = 0644, 64095cb04436SHannes Frederic Sowa .proc_handler = proc_dointvec 64105cb04436SHannes Frederic Sowa }, 64115cb04436SHannes Frederic Sowa { 6412b800c3b9SHannes Frederic Sowa .procname = "suppress_frag_ndisc", 6413b800c3b9SHannes Frederic Sowa .data = &ipv6_devconf.suppress_frag_ndisc, 6414b800c3b9SHannes Frederic Sowa .maxlen = sizeof(int), 6415b800c3b9SHannes Frederic Sowa .mode = 0644, 6416b800c3b9SHannes Frederic Sowa .proc_handler = proc_dointvec 6417b800c3b9SHannes Frederic Sowa }, 6418b800c3b9SHannes Frederic Sowa { 6419d9333196SBen Greear .procname = "accept_ra_from_local", 6420d9333196SBen Greear .data = &ipv6_devconf.accept_ra_from_local, 6421d9333196SBen Greear .maxlen = sizeof(int), 6422d9333196SBen Greear .mode = 0644, 6423d9333196SBen Greear .proc_handler = proc_dointvec, 6424d9333196SBen Greear }, 6425d9333196SBen Greear { 6426c2943f14SHarout Hedeshian .procname = "accept_ra_mtu", 6427c2943f14SHarout Hedeshian .data = &ipv6_devconf.accept_ra_mtu, 6428c2943f14SHarout Hedeshian .maxlen = sizeof(int), 6429c2943f14SHarout Hedeshian .mode = 0644, 6430c2943f14SHarout Hedeshian .proc_handler = proc_dointvec, 6431c2943f14SHarout Hedeshian }, 6432c2943f14SHarout Hedeshian { 64333d1bec99SHannes Frederic Sowa .procname = "stable_secret", 64343d1bec99SHannes Frederic Sowa .data = &ipv6_devconf.stable_secret, 64353d1bec99SHannes Frederic Sowa .maxlen = IPV6_MAX_STRLEN, 64363d1bec99SHannes Frederic Sowa .mode = 0600, 64373d1bec99SHannes Frederic Sowa .proc_handler = addrconf_sysctl_stable_secret, 64383d1bec99SHannes Frederic Sowa }, 64393d1bec99SHannes Frederic Sowa { 64403985e8a3SErik Kline .procname = "use_oif_addrs_only", 64413985e8a3SErik Kline .data = &ipv6_devconf.use_oif_addrs_only, 64423985e8a3SErik Kline .maxlen = sizeof(int), 64433985e8a3SErik Kline .mode = 0644, 64443985e8a3SErik Kline .proc_handler = proc_dointvec, 644535103d11SAndy Gospodarek }, 644635103d11SAndy Gospodarek { 644735103d11SAndy Gospodarek .procname = "ignore_routes_with_linkdown", 644835103d11SAndy Gospodarek .data = &ipv6_devconf.ignore_routes_with_linkdown, 644935103d11SAndy Gospodarek .maxlen = sizeof(int), 645035103d11SAndy Gospodarek .mode = 0644, 645135103d11SAndy Gospodarek .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown, 64523985e8a3SErik Kline }, 64533985e8a3SErik Kline { 6454abbc3043SJohannes Berg .procname = "drop_unicast_in_l2_multicast", 6455abbc3043SJohannes Berg .data = &ipv6_devconf.drop_unicast_in_l2_multicast, 6456abbc3043SJohannes Berg .maxlen = sizeof(int), 6457abbc3043SJohannes Berg .mode = 0644, 6458abbc3043SJohannes Berg .proc_handler = proc_dointvec, 6459abbc3043SJohannes Berg }, 6460abbc3043SJohannes Berg { 64617a02bf89SJohannes Berg .procname = "drop_unsolicited_na", 64627a02bf89SJohannes Berg .data = &ipv6_devconf.drop_unsolicited_na, 64637a02bf89SJohannes Berg .maxlen = sizeof(int), 64647a02bf89SJohannes Berg .mode = 0644, 64657a02bf89SJohannes Berg .proc_handler = proc_dointvec, 64667a02bf89SJohannes Berg }, 64677a02bf89SJohannes Berg { 6468f1705ec1SDavid Ahern .procname = "keep_addr_on_down", 6469f1705ec1SDavid Ahern .data = &ipv6_devconf.keep_addr_on_down, 6470f1705ec1SDavid Ahern .maxlen = sizeof(int), 6471f1705ec1SDavid Ahern .mode = 0644, 6472f1705ec1SDavid Ahern .proc_handler = proc_dointvec, 6473f1705ec1SDavid Ahern 6474f1705ec1SDavid Ahern }, 6475f1705ec1SDavid Ahern { 64761ababebaSDavid Lebrun .procname = "seg6_enabled", 64771ababebaSDavid Lebrun .data = &ipv6_devconf.seg6_enabled, 64781ababebaSDavid Lebrun .maxlen = sizeof(int), 64791ababebaSDavid Lebrun .mode = 0644, 64801ababebaSDavid Lebrun .proc_handler = proc_dointvec, 64811ababebaSDavid Lebrun }, 6482bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC 6483bf355b8dSDavid Lebrun { 6484bf355b8dSDavid Lebrun .procname = "seg6_require_hmac", 6485bf355b8dSDavid Lebrun .data = &ipv6_devconf.seg6_require_hmac, 6486bf355b8dSDavid Lebrun .maxlen = sizeof(int), 6487bf355b8dSDavid Lebrun .mode = 0644, 6488bf355b8dSDavid Lebrun .proc_handler = proc_dointvec, 6489bf355b8dSDavid Lebrun }, 6490bf355b8dSDavid Lebrun #endif 64911ababebaSDavid Lebrun { 6492adc176c5SErik Nordmark .procname = "enhanced_dad", 6493adc176c5SErik Nordmark .data = &ipv6_devconf.enhanced_dad, 6494adc176c5SErik Nordmark .maxlen = sizeof(int), 6495adc176c5SErik Nordmark .mode = 0644, 6496adc176c5SErik Nordmark .proc_handler = proc_dointvec, 6497adc176c5SErik Nordmark }, 6498adc176c5SErik Nordmark { 6499d35a00b8SFelix Jia .procname = "addr_gen_mode", 6500d35a00b8SFelix Jia .data = &ipv6_devconf.addr_gen_mode, 6501d35a00b8SFelix Jia .maxlen = sizeof(int), 6502d35a00b8SFelix Jia .mode = 0644, 6503d35a00b8SFelix Jia .proc_handler = addrconf_sysctl_addr_gen_mode, 6504d35a00b8SFelix Jia }, 6505d35a00b8SFelix Jia { 6506df789fe7SDavid Forster .procname = "disable_policy", 6507df789fe7SDavid Forster .data = &ipv6_devconf.disable_policy, 6508df789fe7SDavid Forster .maxlen = sizeof(int), 6509df789fe7SDavid Forster .mode = 0644, 6510df789fe7SDavid Forster .proc_handler = addrconf_sysctl_disable_policy, 6511df789fe7SDavid Forster }, 6512df789fe7SDavid Forster { 65132210d6b2SMaciej Żenczykowski .procname = "ndisc_tclass", 65142210d6b2SMaciej Żenczykowski .data = &ipv6_devconf.ndisc_tclass, 65152210d6b2SMaciej Żenczykowski .maxlen = sizeof(int), 65162210d6b2SMaciej Żenczykowski .mode = 0644, 65172210d6b2SMaciej Żenczykowski .proc_handler = proc_dointvec_minmax, 65182210d6b2SMaciej Żenczykowski .extra1 = (void *)&zero, 65192210d6b2SMaciej Żenczykowski .extra2 = (void *)&two_five_five, 65202210d6b2SMaciej Żenczykowski }, 65212210d6b2SMaciej Żenczykowski { 6522f8572d8fSEric W. Biederman /* sentinel */ 65231da177e4SLinus Torvalds } 65241da177e4SLinus Torvalds }; 65251da177e4SLinus Torvalds 6526bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name, 6527f8572d8fSEric W. Biederman struct inet6_dev *idev, struct ipv6_devconf *p) 65281da177e4SLinus Torvalds { 652929c994e3SNicolas Dichtel int i, ifindex; 6530607ea7cdSKonstantin Khlebnikov struct ctl_table *table; 65316105e293SEric W. Biederman char path[sizeof("net/ipv6/conf/") + IFNAMSIZ]; 65321dab6222SPavel Emelyanov 6533607ea7cdSKonstantin Khlebnikov table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL); 6534607ea7cdSKonstantin Khlebnikov if (!table) 6535f68635e6SPavel Emelyanov goto out; 6536f68635e6SPavel Emelyanov 6537607ea7cdSKonstantin Khlebnikov for (i = 0; table[i].data; i++) { 6538607ea7cdSKonstantin Khlebnikov table[i].data += (char *)p - (char *)&ipv6_devconf; 6539cb9e684eSMaciej Żenczykowski /* If one of these is already set, then it is not safe to 6540cb9e684eSMaciej Żenczykowski * overwrite either of them: this makes proc_dointvec_minmax 6541cb9e684eSMaciej Żenczykowski * usable. 6542cb9e684eSMaciej Żenczykowski */ 6543cb9e684eSMaciej Żenczykowski if (!table[i].extra1 && !table[i].extra2) { 6544607ea7cdSKonstantin Khlebnikov table[i].extra1 = idev; /* embedded; no ref */ 6545607ea7cdSKonstantin Khlebnikov table[i].extra2 = net; 65461da177e4SLinus Torvalds } 6547cb9e684eSMaciej Żenczykowski } 65481da177e4SLinus Torvalds 65496105e293SEric W. Biederman snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name); 65501da177e4SLinus Torvalds 6551607ea7cdSKonstantin Khlebnikov p->sysctl_header = register_net_sysctl(net, path, table); 6552607ea7cdSKonstantin Khlebnikov if (!p->sysctl_header) 65536105e293SEric W. Biederman goto free; 6554f68635e6SPavel Emelyanov 655529c994e3SNicolas Dichtel if (!strcmp(dev_name, "all")) 655629c994e3SNicolas Dichtel ifindex = NETCONFA_IFINDEX_ALL; 655729c994e3SNicolas Dichtel else if (!strcmp(dev_name, "default")) 655829c994e3SNicolas Dichtel ifindex = NETCONFA_IFINDEX_DEFAULT; 655929c994e3SNicolas Dichtel else 656029c994e3SNicolas Dichtel ifindex = idev->dev->ifindex; 656185b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL, 656285b3daadSDavid Ahern ifindex, p); 656395897312SPavel Emelyanov return 0; 65641da177e4SLinus Torvalds 65651da177e4SLinus Torvalds free: 6566607ea7cdSKonstantin Khlebnikov kfree(table); 6567f68635e6SPavel Emelyanov out: 656895897312SPavel Emelyanov return -ENOBUFS; 65691da177e4SLinus Torvalds } 65701da177e4SLinus Torvalds 657123452170SDavid Ahern static void __addrconf_sysctl_unregister(struct net *net, 657223452170SDavid Ahern struct ipv6_devconf *p, int ifindex) 6573f52295a9SPavel Emelyanov { 6574607ea7cdSKonstantin Khlebnikov struct ctl_table *table; 6575f52295a9SPavel Emelyanov 6576607ea7cdSKonstantin Khlebnikov if (!p->sysctl_header) 6577408c4768SPavel Emelyanov return; 6578408c4768SPavel Emelyanov 6579607ea7cdSKonstantin Khlebnikov table = p->sysctl_header->ctl_table_arg; 6580607ea7cdSKonstantin Khlebnikov unregister_net_sysctl_table(p->sysctl_header); 6581607ea7cdSKonstantin Khlebnikov p->sysctl_header = NULL; 6582607ea7cdSKonstantin Khlebnikov kfree(table); 658323452170SDavid Ahern 658423452170SDavid Ahern inet6_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL); 65851da177e4SLinus Torvalds } 6586408c4768SPavel Emelyanov 6587a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev) 6588408c4768SPavel Emelyanov { 6589a317a2f1SWANG Cong int err; 6590a317a2f1SWANG Cong 6591a317a2f1SWANG Cong if (!sysctl_dev_name_is_allowed(idev->dev->name)) 6592a317a2f1SWANG Cong return -EINVAL; 6593a317a2f1SWANG Cong 6594a317a2f1SWANG Cong err = neigh_sysctl_register(idev->dev, idev->nd_parms, 6595f8572d8fSEric W. Biederman &ndisc_ifinfo_sysctl_change); 6596a317a2f1SWANG Cong if (err) 6597a317a2f1SWANG Cong return err; 6598a317a2f1SWANG Cong err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name, 6599f8572d8fSEric W. Biederman idev, &idev->cnf); 6600a317a2f1SWANG Cong if (err) 6601a317a2f1SWANG Cong neigh_sysctl_unregister(idev->nd_parms); 6602a317a2f1SWANG Cong 6603a317a2f1SWANG Cong return err; 6604408c4768SPavel Emelyanov } 6605408c4768SPavel Emelyanov 6606408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev) 6607408c4768SPavel Emelyanov { 660823452170SDavid Ahern __addrconf_sysctl_unregister(dev_net(idev->dev), &idev->cnf, 660923452170SDavid Ahern idev->dev->ifindex); 6610408c4768SPavel Emelyanov neigh_sysctl_unregister(idev->nd_parms); 66111da177e4SLinus Torvalds } 66121da177e4SLinus Torvalds 66131da177e4SLinus Torvalds 66141da177e4SLinus Torvalds #endif 66151da177e4SLinus Torvalds 66162c8c1e72SAlexey Dobriyan static int __net_init addrconf_init_net(struct net *net) 6617e0da5a48SPavel Emelyanov { 6618a79ca223SHong Zhiguo int err = -ENOMEM; 6619e0da5a48SPavel Emelyanov struct ipv6_devconf *all, *dflt; 6620e0da5a48SPavel Emelyanov 6621a79ca223SHong Zhiguo all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL); 662263159f29SIan Morris if (!all) 6623e0da5a48SPavel Emelyanov goto err_alloc_all; 6624e0da5a48SPavel Emelyanov 6625a79ca223SHong Zhiguo dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL); 662663159f29SIan Morris if (!dflt) 6627e0da5a48SPavel Emelyanov goto err_alloc_dflt; 6628a79ca223SHong Zhiguo 662956d417b1SBrian Haley /* these will be inherited by all namespaces */ 663056d417b1SBrian Haley dflt->autoconf = ipv6_defaults.autoconf; 663156d417b1SBrian Haley dflt->disable_ipv6 = ipv6_defaults.disable_ipv6; 6632e0da5a48SPavel Emelyanov 66333d1bec99SHannes Frederic Sowa dflt->stable_secret.initialized = false; 66343d1bec99SHannes Frederic Sowa all->stable_secret.initialized = false; 66353d1bec99SHannes Frederic Sowa 6636e0da5a48SPavel Emelyanov net->ipv6.devconf_all = all; 6637e0da5a48SPavel Emelyanov net->ipv6.devconf_dflt = dflt; 6638e0da5a48SPavel Emelyanov 6639e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 6640f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "all", NULL, all); 6641e0da5a48SPavel Emelyanov if (err < 0) 6642e0da5a48SPavel Emelyanov goto err_reg_all; 6643e0da5a48SPavel Emelyanov 6644f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "default", NULL, dflt); 6645e0da5a48SPavel Emelyanov if (err < 0) 6646e0da5a48SPavel Emelyanov goto err_reg_dflt; 6647e0da5a48SPavel Emelyanov #endif 6648e0da5a48SPavel Emelyanov return 0; 6649e0da5a48SPavel Emelyanov 6650e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 6651e0da5a48SPavel Emelyanov err_reg_dflt: 665223452170SDavid Ahern __addrconf_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL); 6653e0da5a48SPavel Emelyanov err_reg_all: 6654e0da5a48SPavel Emelyanov kfree(dflt); 6655e0da5a48SPavel Emelyanov #endif 6656e0da5a48SPavel Emelyanov err_alloc_dflt: 6657e0da5a48SPavel Emelyanov kfree(all); 6658e0da5a48SPavel Emelyanov err_alloc_all: 6659e0da5a48SPavel Emelyanov return err; 6660e0da5a48SPavel Emelyanov } 6661e0da5a48SPavel Emelyanov 66622c8c1e72SAlexey Dobriyan static void __net_exit addrconf_exit_net(struct net *net) 6663e0da5a48SPavel Emelyanov { 6664e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 666523452170SDavid Ahern __addrconf_sysctl_unregister(net, net->ipv6.devconf_dflt, 666623452170SDavid Ahern NETCONFA_IFINDEX_DEFAULT); 666723452170SDavid Ahern __addrconf_sysctl_unregister(net, net->ipv6.devconf_all, 666823452170SDavid Ahern NETCONFA_IFINDEX_ALL); 6669e0da5a48SPavel Emelyanov #endif 6670e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_dflt); 6671e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_all); 6672e0da5a48SPavel Emelyanov } 6673e0da5a48SPavel Emelyanov 6674e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = { 6675e0da5a48SPavel Emelyanov .init = addrconf_init_net, 6676e0da5a48SPavel Emelyanov .exit = addrconf_exit_net, 6677e0da5a48SPavel Emelyanov }; 6678e0da5a48SPavel Emelyanov 6679207895fdSDaniel Borkmann static struct rtnl_af_ops inet6_ops __read_mostly = { 6680b382b191SThomas Graf .family = AF_INET6, 6681b382b191SThomas Graf .fill_link_af = inet6_fill_link_af, 6682b382b191SThomas Graf .get_link_af_size = inet6_get_link_af_size, 668311b1f828SDaniel Borkmann .validate_link_af = inet6_validate_link_af, 6684f53adae4SDaniel Borkmann .set_link_af = inet6_set_link_af, 6685b382b191SThomas Graf }; 6686b382b191SThomas Graf 66871da177e4SLinus Torvalds /* 66881da177e4SLinus Torvalds * Init / cleanup code 66891da177e4SLinus Torvalds */ 66901da177e4SLinus Torvalds 66911da177e4SLinus Torvalds int __init addrconf_init(void) 66921da177e4SLinus Torvalds { 6693a317a2f1SWANG Cong struct inet6_dev *idev; 6694c2e21293Sstephen hemminger int i, err; 66952a8cc6c8SYOSHIFUJI Hideaki 6696e21e8467SStephen Hemminger err = ipv6_addr_label_init(); 6697e21e8467SStephen Hemminger if (err < 0) { 6698f3213831SJoe Perches pr_crit("%s: cannot initialize default policy table: %d\n", 6699f3213831SJoe Perches __func__, err); 67002cc6d2bfSNeil Horman goto out; 67012a8cc6c8SYOSHIFUJI Hideaki } 67021da177e4SLinus Torvalds 67032cc6d2bfSNeil Horman err = register_pernet_subsys(&addrconf_ops); 67042cc6d2bfSNeil Horman if (err < 0) 67052cc6d2bfSNeil Horman goto out_addrlabel; 6706e0da5a48SPavel Emelyanov 6707c15b1ccaSHannes Frederic Sowa addrconf_wq = create_workqueue("ipv6_addrconf"); 6708c15b1ccaSHannes Frederic Sowa if (!addrconf_wq) { 6709c15b1ccaSHannes Frederic Sowa err = -ENOMEM; 6710c15b1ccaSHannes Frederic Sowa goto out_nowq; 6711c15b1ccaSHannes Frederic Sowa } 6712c15b1ccaSHannes Frederic Sowa 67131da177e4SLinus Torvalds /* The addrconf netdev notifier requires that loopback_dev 67141da177e4SLinus Torvalds * has it's ipv6 private information allocated and setup 67151da177e4SLinus Torvalds * before it can bring up and give link-local addresses 67161da177e4SLinus Torvalds * to other devices which are up. 67171da177e4SLinus Torvalds * 67181da177e4SLinus Torvalds * Unfortunately, loopback_dev is not necessarily the first 67191da177e4SLinus Torvalds * entry in the global dev_base list of net devices. In fact, 67201da177e4SLinus Torvalds * it is likely to be the very last entry on that list. 67211da177e4SLinus Torvalds * So this causes the notifier registry below to try and 67221da177e4SLinus Torvalds * give link-local addresses to all devices besides loopback_dev 67231da177e4SLinus Torvalds * first, then loopback_dev, which cases all the non-loopback_dev 67241da177e4SLinus Torvalds * devices to fail to get a link-local address. 67251da177e4SLinus Torvalds * 67261da177e4SLinus Torvalds * So, as a temporary fix, allocate the ipv6 structure for 67271da177e4SLinus Torvalds * loopback_dev first by hand. 67281da177e4SLinus Torvalds * Longer term, all of the dependencies ipv6 has upon the loopback 67291da177e4SLinus Torvalds * device and it being up should be removed. 67301da177e4SLinus Torvalds */ 67311da177e4SLinus Torvalds rtnl_lock(); 6732a317a2f1SWANG Cong idev = ipv6_add_dev(init_net.loopback_dev); 67331da177e4SLinus Torvalds rtnl_unlock(); 6734a317a2f1SWANG Cong if (IS_ERR(idev)) { 6735a317a2f1SWANG Cong err = PTR_ERR(idev); 6736e0da5a48SPavel Emelyanov goto errlo; 6737a317a2f1SWANG Cong } 67381da177e4SLinus Torvalds 67392f460933SWANG Cong ip6_route_init_special_entries(); 67402f460933SWANG Cong 6741c2e21293Sstephen hemminger for (i = 0; i < IN6_ADDR_HSIZE; i++) 6742c2e21293Sstephen hemminger INIT_HLIST_HEAD(&inet6_addr_lst[i]); 6743c2e21293Sstephen hemminger 67441da177e4SLinus Torvalds register_netdevice_notifier(&ipv6_dev_notf); 67451da177e4SLinus Torvalds 6746c15b1ccaSHannes Frederic Sowa addrconf_verify(); 6747c127ea2cSThomas Graf 67483678a9d8Sstephen hemminger rtnl_af_register(&inet6_ops); 6749b382b191SThomas Graf 675016feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETLINK, 675116feebcfSFlorian Westphal NULL, inet6_dump_ifinfo, 0); 6752c127ea2cSThomas Graf if (err < 0) 6753c127ea2cSThomas Graf goto errout; 6754c127ea2cSThomas Graf 675516feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWADDR, 675616feebcfSFlorian Westphal inet6_rtm_newaddr, NULL, 0); 675716feebcfSFlorian Westphal if (err < 0) 675816feebcfSFlorian Westphal goto errout; 675916feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELADDR, 676016feebcfSFlorian Westphal inet6_rtm_deladdr, NULL, 0); 676116feebcfSFlorian Westphal if (err < 0) 676216feebcfSFlorian Westphal goto errout; 676316feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETADDR, 676416feebcfSFlorian Westphal inet6_rtm_getaddr, inet6_dump_ifaddr, 676516feebcfSFlorian Westphal RTNL_FLAG_DOIT_UNLOCKED); 676616feebcfSFlorian Westphal if (err < 0) 676716feebcfSFlorian Westphal goto errout; 676816feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETMULTICAST, 676916feebcfSFlorian Westphal NULL, inet6_dump_ifmcaddr, 0); 677016feebcfSFlorian Westphal if (err < 0) 677116feebcfSFlorian Westphal goto errout; 677216feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETANYCAST, 677316feebcfSFlorian Westphal NULL, inet6_dump_ifacaddr, 0); 677416feebcfSFlorian Westphal if (err < 0) 677516feebcfSFlorian Westphal goto errout; 677616feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETNETCONF, 677716feebcfSFlorian Westphal inet6_netconf_get_devconf, 677816feebcfSFlorian Westphal inet6_netconf_dump_devconf, 677916feebcfSFlorian Westphal RTNL_FLAG_DOIT_UNLOCKED); 678016feebcfSFlorian Westphal if (err < 0) 678116feebcfSFlorian Westphal goto errout; 6782a3fde2adSFlorian Westphal err = ipv6_addr_label_rtnl_register(); 6783a3fde2adSFlorian Westphal if (err < 0) 6784a3fde2adSFlorian Westphal goto errout; 67852a8cc6c8SYOSHIFUJI Hideaki 67861da177e4SLinus Torvalds return 0; 6787c127ea2cSThomas Graf errout: 678816feebcfSFlorian Westphal rtnl_unregister_all(PF_INET6); 6789b382b191SThomas Graf rtnl_af_unregister(&inet6_ops); 6790c127ea2cSThomas Graf unregister_netdevice_notifier(&ipv6_dev_notf); 6791e0da5a48SPavel Emelyanov errlo: 6792c15b1ccaSHannes Frederic Sowa destroy_workqueue(addrconf_wq); 6793c15b1ccaSHannes Frederic Sowa out_nowq: 6794e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 67952cc6d2bfSNeil Horman out_addrlabel: 67962cc6d2bfSNeil Horman ipv6_addr_label_cleanup(); 67972cc6d2bfSNeil Horman out: 6798c127ea2cSThomas Graf return err; 67991da177e4SLinus Torvalds } 68001da177e4SLinus Torvalds 680109f7709fSDaniel Lezcano void addrconf_cleanup(void) 68021da177e4SLinus Torvalds { 6803176c39afSDaniel Lezcano struct net_device *dev; 68041da177e4SLinus Torvalds int i; 68051da177e4SLinus Torvalds 68061da177e4SLinus Torvalds unregister_netdevice_notifier(&ipv6_dev_notf); 6807e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 68082cc6d2bfSNeil Horman ipv6_addr_label_cleanup(); 68091da177e4SLinus Torvalds 68105c45121dSFlorian Westphal rtnl_af_unregister(&inet6_ops); 68111da177e4SLinus Torvalds 68125c45121dSFlorian Westphal rtnl_lock(); 6813b382b191SThomas Graf 6814176c39afSDaniel Lezcano /* clean dev list */ 6815176c39afSDaniel Lezcano for_each_netdev(&init_net, dev) { 6816176c39afSDaniel Lezcano if (__in6_dev_get(dev) == NULL) 6817176c39afSDaniel Lezcano continue; 6818176c39afSDaniel Lezcano addrconf_ifdown(dev, 1); 6819176c39afSDaniel Lezcano } 6820176c39afSDaniel Lezcano addrconf_ifdown(init_net.loopback_dev, 2); 6821176c39afSDaniel Lezcano 68221da177e4SLinus Torvalds /* 68231da177e4SLinus Torvalds * Check hash table. 68241da177e4SLinus Torvalds */ 68255c578aedSstephen hemminger spin_lock_bh(&addrconf_hash_lock); 6826c2e21293Sstephen hemminger for (i = 0; i < IN6_ADDR_HSIZE; i++) 6827c2e21293Sstephen hemminger WARN_ON(!hlist_empty(&inet6_addr_lst[i])); 68285c578aedSstephen hemminger spin_unlock_bh(&addrconf_hash_lock); 6829c15b1ccaSHannes Frederic Sowa cancel_delayed_work(&addr_chk_work); 68301da177e4SLinus Torvalds rtnl_unlock(); 6831c15b1ccaSHannes Frederic Sowa 6832c15b1ccaSHannes Frederic Sowa destroy_workqueue(addrconf_wq); 68331da177e4SLinus Torvalds } 6834