12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * IPv6 Address [auto]configuration 41da177e4SLinus Torvalds * Linux INET6 implementation 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Authors: 71da177e4SLinus Torvalds * Pedro Roque <roque@di.fc.ul.pt> 81da177e4SLinus Torvalds * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 111da177e4SLinus Torvalds /* 121da177e4SLinus Torvalds * Changes: 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds * Janos Farkas : delete timer on ifdown 151da177e4SLinus Torvalds * <chexum@bankinf.banki.hu> 161da177e4SLinus Torvalds * Andi Kleen : kill double kfree on module 171da177e4SLinus Torvalds * unload. 181da177e4SLinus Torvalds * Maciej W. Rozycki : FDDI support 191da177e4SLinus Torvalds * sekiya@USAGI : Don't send too many RS 201da177e4SLinus Torvalds * packets. 211da177e4SLinus Torvalds * yoshfuji@USAGI : Fixed interval between DAD 221da177e4SLinus Torvalds * packets. 231da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI : improved accuracy of 241da177e4SLinus Torvalds * address validation timer. 251da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041) 261da177e4SLinus Torvalds * support. 271da177e4SLinus Torvalds * Yuji SEKIYA @USAGI : Don't assign a same IPv6 281da177e4SLinus Torvalds * address on a same interface. 291da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI : ARCnet support 301da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to 311da177e4SLinus Torvalds * seq_file. 32b1cacb68SYOSHIFUJI Hideaki * YOSHIFUJI Hideaki @USAGI : improved source address 33b1cacb68SYOSHIFUJI Hideaki * selection; consider scope, 34b1cacb68SYOSHIFUJI Hideaki * status etc. 351da177e4SLinus Torvalds */ 361da177e4SLinus Torvalds 37f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt 38f3213831SJoe Perches 391da177e4SLinus Torvalds #include <linux/errno.h> 401da177e4SLinus Torvalds #include <linux/types.h> 41a0bffffcSIlpo Järvinen #include <linux/kernel.h> 42174cd4b1SIngo Molnar #include <linux/sched/signal.h> 431da177e4SLinus Torvalds #include <linux/socket.h> 441da177e4SLinus Torvalds #include <linux/sockios.h> 451da177e4SLinus Torvalds #include <linux/net.h> 463d1bec99SHannes Frederic Sowa #include <linux/inet.h> 471da177e4SLinus Torvalds #include <linux/in6.h> 481da177e4SLinus Torvalds #include <linux/netdevice.h> 491823730fSThomas Graf #include <linux/if_addr.h> 501da177e4SLinus Torvalds #include <linux/if_arp.h> 511da177e4SLinus Torvalds #include <linux/if_arcnet.h> 521da177e4SLinus Torvalds #include <linux/if_infiniband.h> 531da177e4SLinus Torvalds #include <linux/route.h> 541da177e4SLinus Torvalds #include <linux/inetdevice.h> 551da177e4SLinus Torvalds #include <linux/init.h> 565a0e3ad6STejun Heo #include <linux/slab.h> 571da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 581da177e4SLinus Torvalds #include <linux/sysctl.h> 591da177e4SLinus Torvalds #endif 604fc268d2SRandy Dunlap #include <linux/capability.h> 611da177e4SLinus Torvalds #include <linux/delay.h> 621da177e4SLinus Torvalds #include <linux/notifier.h> 63543537bdSPaulo Marques #include <linux/string.h> 64ddbe5032SEric Dumazet #include <linux/hash.h> 651da177e4SLinus Torvalds 66457c4cbcSEric W. Biederman #include <net/net_namespace.h> 671da177e4SLinus Torvalds #include <net/sock.h> 681da177e4SLinus Torvalds #include <net/snmp.h> 691da177e4SLinus Torvalds 705241c2d7SAlexander Aring #include <net/6lowpan.h> 71cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 #include <net/firewire.h> 721da177e4SLinus Torvalds #include <net/ipv6.h> 731da177e4SLinus Torvalds #include <net/protocol.h> 741da177e4SLinus Torvalds #include <net/ndisc.h> 751da177e4SLinus Torvalds #include <net/ip6_route.h> 761da177e4SLinus Torvalds #include <net/addrconf.h> 771da177e4SLinus Torvalds #include <net/tcp.h> 781da177e4SLinus Torvalds #include <net/ip.h> 795d620266SThomas Graf #include <net/netlink.h> 80f24e3d65SMitsuru Chinen #include <net/pkt_sched.h> 81ca254490SDavid Ahern #include <net/l3mdev.h> 821da177e4SLinus Torvalds #include <linux/if_tunnel.h> 831da177e4SLinus Torvalds #include <linux/rtnetlink.h> 84f3a1bfb1SNicolas Dichtel #include <linux/netconf.h> 851da177e4SLinus Torvalds #include <linux/random.h> 86e21e8467SStephen Hemminger #include <linux/uaccess.h> 877f7d9a6bSHerbert Xu #include <asm/unaligned.h> 881da177e4SLinus Torvalds 891da177e4SLinus Torvalds #include <linux/proc_fs.h> 901da177e4SLinus Torvalds #include <linux/seq_file.h> 91bc3b2d7fSPaul Gortmaker #include <linux/export.h> 921da177e4SLinus Torvalds 931da177e4SLinus Torvalds #define INFINITY_LIFE_TIME 0xFFFFFFFF 9418a31e1eSThomas Graf 953d1bec99SHannes Frederic Sowa #define IPV6_MAX_STRLEN \ 963d1bec99SHannes Frederic Sowa sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255") 973d1bec99SHannes Frederic Sowa 9818a31e1eSThomas Graf static inline u32 cstamp_delta(unsigned long cstamp) 9918a31e1eSThomas Graf { 10018a31e1eSThomas Graf return (cstamp - INITIAL_JIFFIES) * 100UL / HZ; 10118a31e1eSThomas Graf } 1021da177e4SLinus Torvalds 103bd11f074SMaciej Żenczykowski static inline s32 rfc3315_s14_backoff_init(s32 irt) 104bd11f074SMaciej Żenczykowski { 105bd11f074SMaciej Żenczykowski /* multiply 'initial retransmission time' by 0.9 .. 1.1 */ 106bd11f074SMaciej Żenczykowski u64 tmp = (900000 + prandom_u32() % 200001) * (u64)irt; 107bd11f074SMaciej Żenczykowski do_div(tmp, 1000000); 108bd11f074SMaciej Żenczykowski return (s32)tmp; 109bd11f074SMaciej Żenczykowski } 110bd11f074SMaciej Żenczykowski 111bd11f074SMaciej Żenczykowski static inline s32 rfc3315_s14_backoff_update(s32 rt, s32 mrt) 112bd11f074SMaciej Żenczykowski { 113bd11f074SMaciej Żenczykowski /* multiply 'retransmission timeout' by 1.9 .. 2.1 */ 114bd11f074SMaciej Żenczykowski u64 tmp = (1900000 + prandom_u32() % 200001) * (u64)rt; 115bd11f074SMaciej Żenczykowski do_div(tmp, 1000000); 116bd11f074SMaciej Żenczykowski if ((s32)tmp > mrt) { 117bd11f074SMaciej Żenczykowski /* multiply 'maximum retransmission time' by 0.9 .. 1.1 */ 118bd11f074SMaciej Żenczykowski tmp = (900000 + prandom_u32() % 200001) * (u64)mrt; 119bd11f074SMaciej Żenczykowski do_div(tmp, 1000000); 120bd11f074SMaciej Żenczykowski } 121bd11f074SMaciej Żenczykowski return (s32)tmp; 122bd11f074SMaciej Żenczykowski } 123bd11f074SMaciej Żenczykowski 1241da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 125a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev); 126408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev); 127408c4768SPavel Emelyanov #else 128a317a2f1SWANG Cong static inline int addrconf_sysctl_register(struct inet6_dev *idev) 129408c4768SPavel Emelyanov { 130a317a2f1SWANG Cong return 0; 131408c4768SPavel Emelyanov } 132408c4768SPavel Emelyanov 133408c4768SPavel Emelyanov static inline void addrconf_sysctl_unregister(struct inet6_dev *idev) 134408c4768SPavel Emelyanov { 135408c4768SPavel Emelyanov } 1361da177e4SLinus Torvalds #endif 1371da177e4SLinus Torvalds 1389d6280daSJiri Bohac static void ipv6_regen_rndid(struct inet6_dev *idev); 1399d6280daSJiri Bohac static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr); 1401da177e4SLinus Torvalds 1411b34be74SYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev); 142d9bf82c2SEric Dumazet static int ipv6_count_addresses(const struct inet6_dev *idev); 143622c81d5SHannes Frederic Sowa static int ipv6_generate_stable_address(struct in6_addr *addr, 144622c81d5SHannes Frederic Sowa u8 dad_count, 145622c81d5SHannes Frederic Sowa const struct inet6_dev *idev); 1461da177e4SLinus Torvalds 14727c565aeSEric Dumazet #define IN6_ADDR_HSIZE_SHIFT 8 14827c565aeSEric Dumazet #define IN6_ADDR_HSIZE (1 << IN6_ADDR_HSIZE_SHIFT) 1491da177e4SLinus Torvalds /* 1501da177e4SLinus Torvalds * Configured unicast address hash table 1511da177e4SLinus Torvalds */ 152c2e21293Sstephen hemminger static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE]; 1535c578aedSstephen hemminger static DEFINE_SPINLOCK(addrconf_hash_lock); 1541da177e4SLinus Torvalds 155c15b1ccaSHannes Frederic Sowa static void addrconf_verify(void); 156c15b1ccaSHannes Frederic Sowa static void addrconf_verify_rtnl(void); 157c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *); 1581da177e4SLinus Torvalds 159c15b1ccaSHannes Frederic Sowa static struct workqueue_struct *addrconf_wq; 160c15b1ccaSHannes Frederic Sowa static DECLARE_DELAYED_WORK(addr_chk_work, addrconf_verify_work); 1611da177e4SLinus Torvalds 1621da177e4SLinus Torvalds static void addrconf_join_anycast(struct inet6_ifaddr *ifp); 1631da177e4SLinus Torvalds static void addrconf_leave_anycast(struct inet6_ifaddr *ifp); 1641da177e4SLinus Torvalds 16593d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, 16675c78500SMoni Shoua unsigned long event); 1671da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how); 1681da177e4SLinus Torvalds 1698d1c802bSDavid Ahern static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx, 17021caa662SRomain Kuntz int plen, 17121caa662SRomain Kuntz const struct net_device *dev, 1722b2450caSDavid Ahern u32 flags, u32 noflags, 1732b2450caSDavid Ahern bool no_gw); 17421caa662SRomain Kuntz 175cf22f9a2SDavid S. Miller static void addrconf_dad_start(struct inet6_ifaddr *ifp); 176c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w); 177c76fe2d9SDavid Ahern static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id, 178c76fe2d9SDavid Ahern bool send_na); 179896585d4SHangbin Liu static void addrconf_dad_run(struct inet6_dev *idev, bool restart); 180e99e88a9SKees Cook static void addrconf_rs_timer(struct timer_list *t); 1811da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); 1821da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev, 1851da177e4SLinus Torvalds struct prefix_info *pinfo); 1861da177e4SLinus Torvalds 187888c848eSAdrian Bunk static struct ipv6_devconf ipv6_devconf __read_mostly = { 1881da177e4SLinus Torvalds .forwarding = 0, 1891da177e4SLinus Torvalds .hop_limit = IPV6_DEFAULT_HOPLIMIT, 1901da177e4SLinus Torvalds .mtu6 = IPV6_MIN_MTU, 1911da177e4SLinus Torvalds .accept_ra = 1, 1921da177e4SLinus Torvalds .accept_redirects = 1, 1931da177e4SLinus Torvalds .autoconf = 1, 1941da177e4SLinus Torvalds .force_mld_version = 0, 195fc4eba58SHannes Frederic Sowa .mldv1_unsolicited_report_interval = 10 * HZ, 196fc4eba58SHannes Frederic Sowa .mldv2_unsolicited_report_interval = HZ, 1971da177e4SLinus Torvalds .dad_transmits = 1, 1981da177e4SLinus Torvalds .rtr_solicits = MAX_RTR_SOLICITATIONS, 1991da177e4SLinus Torvalds .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, 200bd11f074SMaciej Żenczykowski .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL, 2011da177e4SLinus Torvalds .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, 2021da177e4SLinus Torvalds .use_tempaddr = 0, 2031da177e4SLinus Torvalds .temp_valid_lft = TEMP_VALID_LIFETIME, 2041da177e4SLinus Torvalds .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, 2051da177e4SLinus Torvalds .regen_max_retry = REGEN_MAX_RETRY, 2061da177e4SLinus Torvalds .max_desync_factor = MAX_DESYNC_FACTOR, 2071da177e4SLinus Torvalds .max_addresses = IPV6_MAX_ADDRESSES, 20865f5c7c1SYOSHIFUJI Hideaki .accept_ra_defrtr = 1, 209d9333196SBen Greear .accept_ra_from_local = 0, 2108013d1d7SHangbin Liu .accept_ra_min_hop_limit= 1, 211c4fd30ebSYOSHIFUJI Hideaki .accept_ra_pinfo = 1, 212930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 213930d6ff2SYOSHIFUJI Hideaki .accept_ra_rtr_pref = 1, 21452e16356SYOSHIFUJI Hideaki .rtr_probe_interval = 60 * HZ, 21509c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 216bbea124bSJoel Scherpelz .accept_ra_rt_info_min_plen = 0, 21709c884d4SYOSHIFUJI Hideaki .accept_ra_rt_info_max_plen = 0, 21809c884d4SYOSHIFUJI Hideaki #endif 219930d6ff2SYOSHIFUJI Hideaki #endif 220fbea49e1SYOSHIFUJI Hideaki .proxy_ndp = 0, 2210bcbc926SYOSHIFUJI Hideaki .accept_source_route = 0, /* we do not accept RH0 by default. */ 222778d80beSYOSHIFUJI Hideaki .disable_ipv6 = 0, 22309400953SNicolas Dichtel .accept_dad = 0, 224b800c3b9SHannes Frederic Sowa .suppress_frag_ndisc = 1, 225c2943f14SHarout Hedeshian .accept_ra_mtu = 1, 2263d1bec99SHannes Frederic Sowa .stable_secret = { 2273d1bec99SHannes Frederic Sowa .initialized = false, 2283985e8a3SErik Kline }, 2293985e8a3SErik Kline .use_oif_addrs_only = 0, 23035103d11SAndy Gospodarek .ignore_routes_with_linkdown = 0, 231f1705ec1SDavid Ahern .keep_addr_on_down = 0, 2321ababebaSDavid Lebrun .seg6_enabled = 0, 233bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC 234bf355b8dSDavid Lebrun .seg6_require_hmac = 0, 235bf355b8dSDavid Lebrun #endif 236adc176c5SErik Nordmark .enhanced_dad = 1, 237d35a00b8SFelix Jia .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64, 238df789fe7SDavid Forster .disable_policy = 0, 2391da177e4SLinus Torvalds }; 2401da177e4SLinus Torvalds 241ab32ea5dSBrian Haley static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = { 2421da177e4SLinus Torvalds .forwarding = 0, 2431da177e4SLinus Torvalds .hop_limit = IPV6_DEFAULT_HOPLIMIT, 2441da177e4SLinus Torvalds .mtu6 = IPV6_MIN_MTU, 2451da177e4SLinus Torvalds .accept_ra = 1, 2461da177e4SLinus Torvalds .accept_redirects = 1, 2471da177e4SLinus Torvalds .autoconf = 1, 248fc4eba58SHannes Frederic Sowa .force_mld_version = 0, 249fc4eba58SHannes Frederic Sowa .mldv1_unsolicited_report_interval = 10 * HZ, 250fc4eba58SHannes Frederic Sowa .mldv2_unsolicited_report_interval = HZ, 2511da177e4SLinus Torvalds .dad_transmits = 1, 2521da177e4SLinus Torvalds .rtr_solicits = MAX_RTR_SOLICITATIONS, 2531da177e4SLinus Torvalds .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, 254bd11f074SMaciej Żenczykowski .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL, 2551da177e4SLinus Torvalds .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, 2561da177e4SLinus Torvalds .use_tempaddr = 0, 2571da177e4SLinus Torvalds .temp_valid_lft = TEMP_VALID_LIFETIME, 2581da177e4SLinus Torvalds .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, 2591da177e4SLinus Torvalds .regen_max_retry = REGEN_MAX_RETRY, 2601da177e4SLinus Torvalds .max_desync_factor = MAX_DESYNC_FACTOR, 2611da177e4SLinus Torvalds .max_addresses = IPV6_MAX_ADDRESSES, 26265f5c7c1SYOSHIFUJI Hideaki .accept_ra_defrtr = 1, 263d9333196SBen Greear .accept_ra_from_local = 0, 2648013d1d7SHangbin Liu .accept_ra_min_hop_limit= 1, 265c4fd30ebSYOSHIFUJI Hideaki .accept_ra_pinfo = 1, 266930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 267930d6ff2SYOSHIFUJI Hideaki .accept_ra_rtr_pref = 1, 26852e16356SYOSHIFUJI Hideaki .rtr_probe_interval = 60 * HZ, 26909c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 270bbea124bSJoel Scherpelz .accept_ra_rt_info_min_plen = 0, 27109c884d4SYOSHIFUJI Hideaki .accept_ra_rt_info_max_plen = 0, 27209c884d4SYOSHIFUJI Hideaki #endif 273930d6ff2SYOSHIFUJI Hideaki #endif 274fbea49e1SYOSHIFUJI Hideaki .proxy_ndp = 0, 2750bcbc926SYOSHIFUJI Hideaki .accept_source_route = 0, /* we do not accept RH0 by default. */ 276778d80beSYOSHIFUJI Hideaki .disable_ipv6 = 0, 2771b34be74SYOSHIFUJI Hideaki .accept_dad = 1, 278b800c3b9SHannes Frederic Sowa .suppress_frag_ndisc = 1, 279c2943f14SHarout Hedeshian .accept_ra_mtu = 1, 2803d1bec99SHannes Frederic Sowa .stable_secret = { 2813d1bec99SHannes Frederic Sowa .initialized = false, 2823d1bec99SHannes Frederic Sowa }, 2833985e8a3SErik Kline .use_oif_addrs_only = 0, 28435103d11SAndy Gospodarek .ignore_routes_with_linkdown = 0, 285f1705ec1SDavid Ahern .keep_addr_on_down = 0, 2861ababebaSDavid Lebrun .seg6_enabled = 0, 287bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC 288bf355b8dSDavid Lebrun .seg6_require_hmac = 0, 289bf355b8dSDavid Lebrun #endif 290adc176c5SErik Nordmark .enhanced_dad = 1, 291d35a00b8SFelix Jia .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64, 292df789fe7SDavid Forster .disable_policy = 0, 2931da177e4SLinus Torvalds }; 2941da177e4SLinus Torvalds 2951f372c7bSMike Manning /* Check if link is ready: is it up and is a valid qdisc available */ 2961f372c7bSMike Manning static inline bool addrconf_link_ready(const struct net_device *dev) 297f24e3d65SMitsuru Chinen { 2981f372c7bSMike Manning return netif_oper_up(dev) && !qdisc_tx_is_noop(dev); 299f24e3d65SMitsuru Chinen } 300f24e3d65SMitsuru Chinen 301b7b1bfceSHannes Frederic Sowa static void addrconf_del_rs_timer(struct inet6_dev *idev) 3021da177e4SLinus Torvalds { 303b7b1bfceSHannes Frederic Sowa if (del_timer(&idev->rs_timer)) 304b7b1bfceSHannes Frederic Sowa __in6_dev_put(idev); 305b7b1bfceSHannes Frederic Sowa } 306b7b1bfceSHannes Frederic Sowa 307c15b1ccaSHannes Frederic Sowa static void addrconf_del_dad_work(struct inet6_ifaddr *ifp) 308b7b1bfceSHannes Frederic Sowa { 309c15b1ccaSHannes Frederic Sowa if (cancel_delayed_work(&ifp->dad_work)) 3101da177e4SLinus Torvalds __in6_ifa_put(ifp); 3111da177e4SLinus Torvalds } 3121da177e4SLinus Torvalds 313b7b1bfceSHannes Frederic Sowa static void addrconf_mod_rs_timer(struct inet6_dev *idev, 3141da177e4SLinus Torvalds unsigned long when) 3151da177e4SLinus Torvalds { 316b7b1bfceSHannes Frederic Sowa if (!timer_pending(&idev->rs_timer)) 317b7b1bfceSHannes Frederic Sowa in6_dev_hold(idev); 318b7b1bfceSHannes Frederic Sowa mod_timer(&idev->rs_timer, jiffies + when); 3191da177e4SLinus Torvalds } 320b7b1bfceSHannes Frederic Sowa 321c15b1ccaSHannes Frederic Sowa static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp, 322c15b1ccaSHannes Frederic Sowa unsigned long delay) 323b7b1bfceSHannes Frederic Sowa { 324b7b1bfceSHannes Frederic Sowa in6_ifa_hold(ifp); 325f8a894b2SXin Long if (mod_delayed_work(addrconf_wq, &ifp->dad_work, delay)) 326f8a894b2SXin Long in6_ifa_put(ifp); 3271da177e4SLinus Torvalds } 3281da177e4SLinus Torvalds 3297f7d9a6bSHerbert Xu static int snmp6_alloc_dev(struct inet6_dev *idev) 3307f7d9a6bSHerbert Xu { 331827da44cSJohn Stultz int i; 332827da44cSJohn Stultz 333698365faSWANG Cong idev->stats.ipv6 = alloc_percpu(struct ipstats_mib); 334698365faSWANG Cong if (!idev->stats.ipv6) 3357f7d9a6bSHerbert Xu goto err_ip; 336827da44cSJohn Stultz 337827da44cSJohn Stultz for_each_possible_cpu(i) { 338827da44cSJohn Stultz struct ipstats_mib *addrconf_stats; 339698365faSWANG Cong addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i); 340827da44cSJohn Stultz u64_stats_init(&addrconf_stats->syncp); 341827da44cSJohn Stultz } 342827da44cSJohn Stultz 343827da44cSJohn Stultz 344be281e55SEric Dumazet idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device), 345be281e55SEric Dumazet GFP_KERNEL); 346be281e55SEric Dumazet if (!idev->stats.icmpv6dev) 3477f7d9a6bSHerbert Xu goto err_icmp; 348be281e55SEric Dumazet idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device), 349be281e55SEric Dumazet GFP_KERNEL); 350be281e55SEric Dumazet if (!idev->stats.icmpv6msgdev) 35114878f75SDavid L Stevens goto err_icmpmsg; 3527f7d9a6bSHerbert Xu 3537f7d9a6bSHerbert Xu return 0; 3547f7d9a6bSHerbert Xu 35514878f75SDavid L Stevens err_icmpmsg: 356be281e55SEric Dumazet kfree(idev->stats.icmpv6dev); 3577f7d9a6bSHerbert Xu err_icmp: 358698365faSWANG Cong free_percpu(idev->stats.ipv6); 3597f7d9a6bSHerbert Xu err_ip: 360aaf70ec7SPavel Emelyanov return -ENOMEM; 3617f7d9a6bSHerbert Xu } 3627f7d9a6bSHerbert Xu 3631da177e4SLinus Torvalds static struct inet6_dev *ipv6_add_dev(struct net_device *dev) 3641da177e4SLinus Torvalds { 3651da177e4SLinus Torvalds struct inet6_dev *ndev; 366a317a2f1SWANG Cong int err = -ENOMEM; 3671da177e4SLinus Torvalds 3681da177e4SLinus Torvalds ASSERT_RTNL(); 3691da177e4SLinus Torvalds 3701da177e4SLinus Torvalds if (dev->mtu < IPV6_MIN_MTU) 371a317a2f1SWANG Cong return ERR_PTR(-EINVAL); 3721da177e4SLinus Torvalds 373322f74a4SIngo Oeser ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL); 37463159f29SIan Morris if (!ndev) 375a317a2f1SWANG Cong return ERR_PTR(err); 3761da177e4SLinus Torvalds 3771da177e4SLinus Torvalds rwlock_init(&ndev->lock); 3781da177e4SLinus Torvalds ndev->dev = dev; 379502a2ffdSstephen hemminger INIT_LIST_HEAD(&ndev->addr_list); 380e99e88a9SKees Cook timer_setup(&ndev->rs_timer, addrconf_rs_timer, 0); 381c346dca1SYOSHIFUJI Hideaki memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf)); 3829b29c696SHannes Frederic Sowa 3839b29c696SHannes Frederic Sowa if (ndev->cnf.stable_secret.initialized) 384d35a00b8SFelix Jia ndev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY; 3859b29c696SHannes Frederic Sowa 3861da177e4SLinus Torvalds ndev->cnf.mtu6 = dev->mtu; 3871da177e4SLinus Torvalds ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl); 38863159f29SIan Morris if (!ndev->nd_parms) { 3891da177e4SLinus Torvalds kfree(ndev); 390a317a2f1SWANG Cong return ERR_PTR(err); 3911da177e4SLinus Torvalds } 3920187bdfbSBen Hutchings if (ndev->cnf.forwarding) 3930187bdfbSBen Hutchings dev_disable_lro(dev); 3941da177e4SLinus Torvalds /* We refer to the device */ 3951da177e4SLinus Torvalds dev_hold(dev); 3961da177e4SLinus Torvalds 3971da177e4SLinus Torvalds if (snmp6_alloc_dev(ndev) < 0) { 398e32ac250SJoe Perches netdev_dbg(dev, "%s: cannot allocate memory for statistics\n", 399e32ac250SJoe Perches __func__); 4001da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, ndev->nd_parms); 4018603e33dSRoy Li dev_put(dev); 4028603e33dSRoy Li kfree(ndev); 403a317a2f1SWANG Cong return ERR_PTR(err); 4041da177e4SLinus Torvalds } 4051da177e4SLinus Torvalds 4061da177e4SLinus Torvalds if (snmp6_register_dev(ndev) < 0) { 407e32ac250SJoe Perches netdev_dbg(dev, "%s: cannot create /proc/net/dev_snmp6/%s\n", 408ba3542e1Sdingtianhong __func__, dev->name); 409a317a2f1SWANG Cong goto err_release; 4101da177e4SLinus Torvalds } 4111da177e4SLinus Torvalds 4129d6280daSJiri Bohac /* One reference from device. */ 4131be92460SReshetova, Elena refcount_set(&ndev->refcnt, 1); 4141da177e4SLinus Torvalds 4151b34be74SYOSHIFUJI Hideaki if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) 4161b34be74SYOSHIFUJI Hideaki ndev->cnf.accept_dad = -1; 4171b34be74SYOSHIFUJI Hideaki 41807a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 419c7dc89c0SFred L. Templin if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) { 420f3213831SJoe Perches pr_info("%s: Disabled Multicast RS\n", dev->name); 421c7dc89c0SFred L. Templin ndev->cnf.rtr_solicits = 0; 422c7dc89c0SFred L. Templin } 423b077d7abSYOSHIFUJI Hideaki #endif 424b077d7abSYOSHIFUJI Hideaki 425372e6c8fSstephen hemminger INIT_LIST_HEAD(&ndev->tempaddr_list); 42676506a98SJiri Bohac ndev->desync_factor = U32_MAX; 4271da177e4SLinus Torvalds if ((dev->flags&IFF_LOOPBACK) || 4281da177e4SLinus Torvalds dev->type == ARPHRD_TUNNEL || 4299625ed72SYOSHIFUJI Hideaki dev->type == ARPHRD_TUNNEL6 || 4301da177e4SLinus Torvalds dev->type == ARPHRD_SIT || 4311da177e4SLinus Torvalds dev->type == ARPHRD_NONE) { 4320be669bbSJoerg Roedel ndev->cnf.use_tempaddr = -1; 4339d6280daSJiri Bohac } else 4349d6280daSJiri Bohac ipv6_regen_rndid(ndev); 4355d9efa7eSDavid S. Miller 436914faa14SDaniel Borkmann ndev->token = in6addr_any; 4371da177e4SLinus Torvalds 4381f372c7bSMike Manning if (netif_running(dev) && addrconf_link_ready(dev)) 43953aadcc9SHerbert Xu ndev->if_flags |= IF_READY; 44053aadcc9SHerbert Xu 4411da177e4SLinus Torvalds ipv6_mc_init_dev(ndev); 4421da177e4SLinus Torvalds ndev->tstamp = jiffies; 443a317a2f1SWANG Cong err = addrconf_sysctl_register(ndev); 444a317a2f1SWANG Cong if (err) { 445a317a2f1SWANG Cong ipv6_mc_destroy_dev(ndev); 4462a189f9eSSabrina Dubroca snmp6_unregister_dev(ndev); 447a317a2f1SWANG Cong goto err_release; 448a317a2f1SWANG Cong } 44930c4cf57SDavid L Stevens /* protected by rtnl_lock */ 450cf778b00SEric Dumazet rcu_assign_pointer(dev->ip6_ptr, ndev); 451d88ae4ccSYOSHIFUJI Hideaki 4522c5e8933SHannes Frederic Sowa /* Join interface-local all-node multicast group */ 4532c5e8933SHannes Frederic Sowa ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes); 4542c5e8933SHannes Frederic Sowa 455d88ae4ccSYOSHIFUJI Hideaki /* Join all-node multicast group */ 456f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes); 457d88ae4ccSYOSHIFUJI Hideaki 458d6ddef9eSLi Wei /* Join all-router multicast group if forwarding is set */ 4598b2aaedeSLi Wei if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST)) 460d6ddef9eSLi Wei ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters); 461d6ddef9eSLi Wei 4621da177e4SLinus Torvalds return ndev; 463a317a2f1SWANG Cong 464a317a2f1SWANG Cong err_release: 465a317a2f1SWANG Cong neigh_parms_release(&nd_tbl, ndev->nd_parms); 466a317a2f1SWANG Cong ndev->dead = 1; 467a317a2f1SWANG Cong in6_dev_finish_destroy(ndev); 468a317a2f1SWANG Cong return ERR_PTR(err); 4691da177e4SLinus Torvalds } 4701da177e4SLinus Torvalds 471cee89473SYOSHIFUJI Hideaki static struct inet6_dev *ipv6_find_idev(struct net_device *dev) 4721da177e4SLinus Torvalds { 4731da177e4SLinus Torvalds struct inet6_dev *idev; 4741da177e4SLinus Torvalds 4751da177e4SLinus Torvalds ASSERT_RTNL(); 4761da177e4SLinus Torvalds 477e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 478e21e8467SStephen Hemminger if (!idev) { 479e21e8467SStephen Hemminger idev = ipv6_add_dev(dev); 480a317a2f1SWANG Cong if (IS_ERR(idev)) 481db0b99f5SSabrina Dubroca return idev; 4821da177e4SLinus Torvalds } 483c5e33bddSYOSHIFUJI Hideaki 4841da177e4SLinus Torvalds if (dev->flags&IFF_UP) 4851da177e4SLinus Torvalds ipv6_mc_up(idev); 4861da177e4SLinus Torvalds return idev; 4871da177e4SLinus Torvalds } 4881da177e4SLinus Torvalds 489f3a1bfb1SNicolas Dichtel static int inet6_netconf_msgsize_devconf(int type) 490f3a1bfb1SNicolas Dichtel { 491f3a1bfb1SNicolas Dichtel int size = NLMSG_ALIGN(sizeof(struct netconfmsg)) 492f3a1bfb1SNicolas Dichtel + nla_total_size(4); /* NETCONFA_IFINDEX */ 493136ba622SZhang Shengju bool all = false; 494f3a1bfb1SNicolas Dichtel 495136ba622SZhang Shengju if (type == NETCONFA_ALL) 496136ba622SZhang Shengju all = true; 497136ba622SZhang Shengju 498136ba622SZhang Shengju if (all || type == NETCONFA_FORWARDING) 499f3a1bfb1SNicolas Dichtel size += nla_total_size(4); 500b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE 501136ba622SZhang Shengju if (all || type == NETCONFA_MC_FORWARDING) 502d67b8c61SNicolas Dichtel size += nla_total_size(4); 503b1afce95SDavid S. Miller #endif 504136ba622SZhang Shengju if (all || type == NETCONFA_PROXY_NEIGH) 505c92d5491Sstephen hemminger size += nla_total_size(4); 506f3a1bfb1SNicolas Dichtel 507136ba622SZhang Shengju if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) 50835103d11SAndy Gospodarek size += nla_total_size(4); 50935103d11SAndy Gospodarek 510f3a1bfb1SNicolas Dichtel return size; 511f3a1bfb1SNicolas Dichtel } 512f3a1bfb1SNicolas Dichtel 513f3a1bfb1SNicolas Dichtel static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex, 514f3a1bfb1SNicolas Dichtel struct ipv6_devconf *devconf, u32 portid, 515f3a1bfb1SNicolas Dichtel u32 seq, int event, unsigned int flags, 516f3a1bfb1SNicolas Dichtel int type) 517f3a1bfb1SNicolas Dichtel { 518f3a1bfb1SNicolas Dichtel struct nlmsghdr *nlh; 519f3a1bfb1SNicolas Dichtel struct netconfmsg *ncm; 520136ba622SZhang Shengju bool all = false; 521f3a1bfb1SNicolas Dichtel 522f3a1bfb1SNicolas Dichtel nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg), 523f3a1bfb1SNicolas Dichtel flags); 52463159f29SIan Morris if (!nlh) 525f3a1bfb1SNicolas Dichtel return -EMSGSIZE; 526f3a1bfb1SNicolas Dichtel 527136ba622SZhang Shengju if (type == NETCONFA_ALL) 528136ba622SZhang Shengju all = true; 529136ba622SZhang Shengju 530f3a1bfb1SNicolas Dichtel ncm = nlmsg_data(nlh); 531f3a1bfb1SNicolas Dichtel ncm->ncm_family = AF_INET6; 532f3a1bfb1SNicolas Dichtel 533f3a1bfb1SNicolas Dichtel if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0) 534f3a1bfb1SNicolas Dichtel goto nla_put_failure; 535f3a1bfb1SNicolas Dichtel 53623452170SDavid Ahern if (!devconf) 53723452170SDavid Ahern goto out; 53823452170SDavid Ahern 539136ba622SZhang Shengju if ((all || type == NETCONFA_FORWARDING) && 540f3a1bfb1SNicolas Dichtel nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0) 541f3a1bfb1SNicolas Dichtel goto nla_put_failure; 542b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE 543136ba622SZhang Shengju if ((all || type == NETCONFA_MC_FORWARDING) && 544d67b8c61SNicolas Dichtel nla_put_s32(skb, NETCONFA_MC_FORWARDING, 545d67b8c61SNicolas Dichtel devconf->mc_forwarding) < 0) 546d67b8c61SNicolas Dichtel goto nla_put_failure; 547b1afce95SDavid S. Miller #endif 548136ba622SZhang Shengju if ((all || type == NETCONFA_PROXY_NEIGH) && 549c92d5491Sstephen hemminger nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0) 550c92d5491Sstephen hemminger goto nla_put_failure; 551c92d5491Sstephen hemminger 552136ba622SZhang Shengju if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) && 55335103d11SAndy Gospodarek nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 55435103d11SAndy Gospodarek devconf->ignore_routes_with_linkdown) < 0) 55535103d11SAndy Gospodarek goto nla_put_failure; 55635103d11SAndy Gospodarek 55723452170SDavid Ahern out: 558053c095aSJohannes Berg nlmsg_end(skb, nlh); 559053c095aSJohannes Berg return 0; 560f3a1bfb1SNicolas Dichtel 561f3a1bfb1SNicolas Dichtel nla_put_failure: 562f3a1bfb1SNicolas Dichtel nlmsg_cancel(skb, nlh); 563f3a1bfb1SNicolas Dichtel return -EMSGSIZE; 564f3a1bfb1SNicolas Dichtel } 565f3a1bfb1SNicolas Dichtel 56685b3daadSDavid Ahern void inet6_netconf_notify_devconf(struct net *net, int event, int type, 56785b3daadSDavid Ahern int ifindex, struct ipv6_devconf *devconf) 568f3a1bfb1SNicolas Dichtel { 569f3a1bfb1SNicolas Dichtel struct sk_buff *skb; 570f3a1bfb1SNicolas Dichtel int err = -ENOBUFS; 571f3a1bfb1SNicolas Dichtel 572927265bcSEric Dumazet skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_KERNEL); 57363159f29SIan Morris if (!skb) 574f3a1bfb1SNicolas Dichtel goto errout; 575f3a1bfb1SNicolas Dichtel 576f3a1bfb1SNicolas Dichtel err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0, 57785b3daadSDavid Ahern event, 0, type); 578f3a1bfb1SNicolas Dichtel if (err < 0) { 579f3a1bfb1SNicolas Dichtel /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */ 580f3a1bfb1SNicolas Dichtel WARN_ON(err == -EMSGSIZE); 581f3a1bfb1SNicolas Dichtel kfree_skb(skb); 582f3a1bfb1SNicolas Dichtel goto errout; 583f3a1bfb1SNicolas Dichtel } 584927265bcSEric Dumazet rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_KERNEL); 585f3a1bfb1SNicolas Dichtel return; 586f3a1bfb1SNicolas Dichtel errout: 587f3a1bfb1SNicolas Dichtel rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err); 588f3a1bfb1SNicolas Dichtel } 589f3a1bfb1SNicolas Dichtel 59076f8f6cbSNicolas Dichtel static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = { 59176f8f6cbSNicolas Dichtel [NETCONFA_IFINDEX] = { .len = sizeof(int) }, 59276f8f6cbSNicolas Dichtel [NETCONFA_FORWARDING] = { .len = sizeof(int) }, 593c92d5491Sstephen hemminger [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) }, 59435103d11SAndy Gospodarek [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) }, 59576f8f6cbSNicolas Dichtel }; 59676f8f6cbSNicolas Dichtel 59738d51810SJakub Kicinski static int inet6_netconf_valid_get_req(struct sk_buff *skb, 59838d51810SJakub Kicinski const struct nlmsghdr *nlh, 59938d51810SJakub Kicinski struct nlattr **tb, 60038d51810SJakub Kicinski struct netlink_ext_ack *extack) 60138d51810SJakub Kicinski { 60238d51810SJakub Kicinski int i, err; 60338d51810SJakub Kicinski 60438d51810SJakub Kicinski if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(struct netconfmsg))) { 60538d51810SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf get request"); 60638d51810SJakub Kicinski return -EINVAL; 60738d51810SJakub Kicinski } 60838d51810SJakub Kicinski 60938d51810SJakub Kicinski if (!netlink_strict_get_check(skb)) 6108cb08174SJohannes Berg return nlmsg_parse_deprecated(nlh, sizeof(struct netconfmsg), 6118cb08174SJohannes Berg tb, NETCONFA_MAX, 6128cb08174SJohannes Berg devconf_ipv6_policy, extack); 61338d51810SJakub Kicinski 6148cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct netconfmsg), 6158cb08174SJohannes Berg tb, NETCONFA_MAX, 6168cb08174SJohannes Berg devconf_ipv6_policy, extack); 61738d51810SJakub Kicinski if (err) 61838d51810SJakub Kicinski return err; 61938d51810SJakub Kicinski 62038d51810SJakub Kicinski for (i = 0; i <= NETCONFA_MAX; i++) { 62138d51810SJakub Kicinski if (!tb[i]) 62238d51810SJakub Kicinski continue; 62338d51810SJakub Kicinski 62438d51810SJakub Kicinski switch (i) { 62538d51810SJakub Kicinski case NETCONFA_IFINDEX: 62638d51810SJakub Kicinski break; 62738d51810SJakub Kicinski default: 62838d51810SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in netconf get request"); 62938d51810SJakub Kicinski return -EINVAL; 63038d51810SJakub Kicinski } 63138d51810SJakub Kicinski } 63238d51810SJakub Kicinski 63338d51810SJakub Kicinski return 0; 63438d51810SJakub Kicinski } 63538d51810SJakub Kicinski 63676f8f6cbSNicolas Dichtel static int inet6_netconf_get_devconf(struct sk_buff *in_skb, 637c21ef3e3SDavid Ahern struct nlmsghdr *nlh, 638c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 63976f8f6cbSNicolas Dichtel { 64076f8f6cbSNicolas Dichtel struct net *net = sock_net(in_skb->sk); 64176f8f6cbSNicolas Dichtel struct nlattr *tb[NETCONFA_MAX+1]; 6424ea2607fSFlorian Westphal struct inet6_dev *in6_dev = NULL; 6434ea2607fSFlorian Westphal struct net_device *dev = NULL; 64476f8f6cbSNicolas Dichtel struct sk_buff *skb; 64576f8f6cbSNicolas Dichtel struct ipv6_devconf *devconf; 64676f8f6cbSNicolas Dichtel int ifindex; 64776f8f6cbSNicolas Dichtel int err; 64876f8f6cbSNicolas Dichtel 64938d51810SJakub Kicinski err = inet6_netconf_valid_get_req(in_skb, nlh, tb, extack); 65076f8f6cbSNicolas Dichtel if (err < 0) 6514ea2607fSFlorian Westphal return err; 6524ea2607fSFlorian Westphal 6534ea2607fSFlorian Westphal if (!tb[NETCONFA_IFINDEX]) 6544ea2607fSFlorian Westphal return -EINVAL; 65576f8f6cbSNicolas Dichtel 656a97eb33fSAnton Protopopov err = -EINVAL; 65776f8f6cbSNicolas Dichtel ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]); 65876f8f6cbSNicolas Dichtel switch (ifindex) { 65976f8f6cbSNicolas Dichtel case NETCONFA_IFINDEX_ALL: 66076f8f6cbSNicolas Dichtel devconf = net->ipv6.devconf_all; 66176f8f6cbSNicolas Dichtel break; 66276f8f6cbSNicolas Dichtel case NETCONFA_IFINDEX_DEFAULT: 66376f8f6cbSNicolas Dichtel devconf = net->ipv6.devconf_dflt; 66476f8f6cbSNicolas Dichtel break; 66576f8f6cbSNicolas Dichtel default: 6664ea2607fSFlorian Westphal dev = dev_get_by_index(net, ifindex); 66763159f29SIan Morris if (!dev) 6684ea2607fSFlorian Westphal return -EINVAL; 6694ea2607fSFlorian Westphal in6_dev = in6_dev_get(dev); 67063159f29SIan Morris if (!in6_dev) 67176f8f6cbSNicolas Dichtel goto errout; 67276f8f6cbSNicolas Dichtel devconf = &in6_dev->cnf; 67376f8f6cbSNicolas Dichtel break; 67476f8f6cbSNicolas Dichtel } 67576f8f6cbSNicolas Dichtel 67676f8f6cbSNicolas Dichtel err = -ENOBUFS; 6774ea2607fSFlorian Westphal skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL); 67863159f29SIan Morris if (!skb) 67976f8f6cbSNicolas Dichtel goto errout; 68076f8f6cbSNicolas Dichtel 68176f8f6cbSNicolas Dichtel err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 68276f8f6cbSNicolas Dichtel NETLINK_CB(in_skb).portid, 68376f8f6cbSNicolas Dichtel nlh->nlmsg_seq, RTM_NEWNETCONF, 0, 684136ba622SZhang Shengju NETCONFA_ALL); 68576f8f6cbSNicolas Dichtel if (err < 0) { 68676f8f6cbSNicolas Dichtel /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */ 68776f8f6cbSNicolas Dichtel WARN_ON(err == -EMSGSIZE); 68876f8f6cbSNicolas Dichtel kfree_skb(skb); 68976f8f6cbSNicolas Dichtel goto errout; 69076f8f6cbSNicolas Dichtel } 69176f8f6cbSNicolas Dichtel err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 69276f8f6cbSNicolas Dichtel errout: 6934ea2607fSFlorian Westphal if (in6_dev) 6944ea2607fSFlorian Westphal in6_dev_put(in6_dev); 6954ea2607fSFlorian Westphal if (dev) 6964ea2607fSFlorian Westphal dev_put(dev); 69776f8f6cbSNicolas Dichtel return err; 69876f8f6cbSNicolas Dichtel } 69976f8f6cbSNicolas Dichtel 7007a674200SNicolas Dichtel static int inet6_netconf_dump_devconf(struct sk_buff *skb, 7017a674200SNicolas Dichtel struct netlink_callback *cb) 7027a674200SNicolas Dichtel { 703addd383fSDavid Ahern const struct nlmsghdr *nlh = cb->nlh; 7047a674200SNicolas Dichtel struct net *net = sock_net(skb->sk); 7057a674200SNicolas Dichtel int h, s_h; 7067a674200SNicolas Dichtel int idx, s_idx; 7077a674200SNicolas Dichtel struct net_device *dev; 7087a674200SNicolas Dichtel struct inet6_dev *idev; 7097a674200SNicolas Dichtel struct hlist_head *head; 7107a674200SNicolas Dichtel 711addd383fSDavid Ahern if (cb->strict_check) { 712addd383fSDavid Ahern struct netlink_ext_ack *extack = cb->extack; 713addd383fSDavid Ahern struct netconfmsg *ncm; 714addd383fSDavid Ahern 715addd383fSDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ncm))) { 716addd383fSDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf dump request"); 717addd383fSDavid Ahern return -EINVAL; 718addd383fSDavid Ahern } 719addd383fSDavid Ahern 720addd383fSDavid Ahern if (nlmsg_attrlen(nlh, sizeof(*ncm))) { 721addd383fSDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Invalid data after header in netconf dump request"); 722addd383fSDavid Ahern return -EINVAL; 723addd383fSDavid Ahern } 724addd383fSDavid Ahern } 725addd383fSDavid Ahern 7267a674200SNicolas Dichtel s_h = cb->args[0]; 7277a674200SNicolas Dichtel s_idx = idx = cb->args[1]; 7287a674200SNicolas Dichtel 7297a674200SNicolas Dichtel for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 7307a674200SNicolas Dichtel idx = 0; 7317a674200SNicolas Dichtel head = &net->dev_index_head[h]; 7327a674200SNicolas Dichtel rcu_read_lock(); 73363998ac2SNicolas Dichtel cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ 73463998ac2SNicolas Dichtel net->dev_base_seq; 7357a674200SNicolas Dichtel hlist_for_each_entry_rcu(dev, head, index_hlist) { 7367a674200SNicolas Dichtel if (idx < s_idx) 7377a674200SNicolas Dichtel goto cont; 7387a674200SNicolas Dichtel idev = __in6_dev_get(dev); 7397a674200SNicolas Dichtel if (!idev) 7407a674200SNicolas Dichtel goto cont; 7417a674200SNicolas Dichtel 7427a674200SNicolas Dichtel if (inet6_netconf_fill_devconf(skb, dev->ifindex, 7437a674200SNicolas Dichtel &idev->cnf, 7447a674200SNicolas Dichtel NETLINK_CB(cb->skb).portid, 745addd383fSDavid Ahern nlh->nlmsg_seq, 7467a674200SNicolas Dichtel RTM_NEWNETCONF, 7477a674200SNicolas Dichtel NLM_F_MULTI, 748136ba622SZhang Shengju NETCONFA_ALL) < 0) { 7497a674200SNicolas Dichtel rcu_read_unlock(); 7507a674200SNicolas Dichtel goto done; 7517a674200SNicolas Dichtel } 75263998ac2SNicolas Dichtel nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 7537a674200SNicolas Dichtel cont: 7547a674200SNicolas Dichtel idx++; 7557a674200SNicolas Dichtel } 7567a674200SNicolas Dichtel rcu_read_unlock(); 7577a674200SNicolas Dichtel } 7587a674200SNicolas Dichtel if (h == NETDEV_HASHENTRIES) { 7597a674200SNicolas Dichtel if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL, 7607a674200SNicolas Dichtel net->ipv6.devconf_all, 7617a674200SNicolas Dichtel NETLINK_CB(cb->skb).portid, 762addd383fSDavid Ahern nlh->nlmsg_seq, 7637a674200SNicolas Dichtel RTM_NEWNETCONF, NLM_F_MULTI, 764136ba622SZhang Shengju NETCONFA_ALL) < 0) 7657a674200SNicolas Dichtel goto done; 7667a674200SNicolas Dichtel else 7677a674200SNicolas Dichtel h++; 7687a674200SNicolas Dichtel } 7697a674200SNicolas Dichtel if (h == NETDEV_HASHENTRIES + 1) { 7707a674200SNicolas Dichtel if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT, 7717a674200SNicolas Dichtel net->ipv6.devconf_dflt, 7727a674200SNicolas Dichtel NETLINK_CB(cb->skb).portid, 773addd383fSDavid Ahern nlh->nlmsg_seq, 7747a674200SNicolas Dichtel RTM_NEWNETCONF, NLM_F_MULTI, 775136ba622SZhang Shengju NETCONFA_ALL) < 0) 7767a674200SNicolas Dichtel goto done; 7777a674200SNicolas Dichtel else 7787a674200SNicolas Dichtel h++; 7797a674200SNicolas Dichtel } 7807a674200SNicolas Dichtel done: 7817a674200SNicolas Dichtel cb->args[0] = h; 7827a674200SNicolas Dichtel cb->args[1] = idx; 7837a674200SNicolas Dichtel 7847a674200SNicolas Dichtel return skb->len; 7857a674200SNicolas Dichtel } 7867a674200SNicolas Dichtel 7871da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 7881da177e4SLinus Torvalds static void dev_forward_change(struct inet6_dev *idev) 7891da177e4SLinus Torvalds { 7901da177e4SLinus Torvalds struct net_device *dev; 7911da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 7921da177e4SLinus Torvalds 7931da177e4SLinus Torvalds if (!idev) 7941da177e4SLinus Torvalds return; 7951da177e4SLinus Torvalds dev = idev->dev; 7960187bdfbSBen Hutchings if (idev->cnf.forwarding) 7970187bdfbSBen Hutchings dev_disable_lro(dev); 7981a940835SAmerigo Wang if (dev->flags & IFF_MULTICAST) { 7992c5e8933SHannes Frederic Sowa if (idev->cnf.forwarding) { 800f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters); 8012c5e8933SHannes Frederic Sowa ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters); 8022c5e8933SHannes Frederic Sowa ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters); 8032c5e8933SHannes Frederic Sowa } else { 804f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters); 8052c5e8933SHannes Frederic Sowa ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters); 8062c5e8933SHannes Frederic Sowa ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters); 8072c5e8933SHannes Frederic Sowa } 8081da177e4SLinus Torvalds } 809502a2ffdSstephen hemminger 810502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 8112c12a74cSMichal Wrobel if (ifa->flags&IFA_F_TENTATIVE) 8122c12a74cSMichal Wrobel continue; 8131da177e4SLinus Torvalds if (idev->cnf.forwarding) 8141da177e4SLinus Torvalds addrconf_join_anycast(ifa); 8151da177e4SLinus Torvalds else 8161da177e4SLinus Torvalds addrconf_leave_anycast(ifa); 8171da177e4SLinus Torvalds } 81885b3daadSDavid Ahern inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF, 81985b3daadSDavid Ahern NETCONFA_FORWARDING, 820f3a1bfb1SNicolas Dichtel dev->ifindex, &idev->cnf); 8211da177e4SLinus Torvalds } 8221da177e4SLinus Torvalds 8231da177e4SLinus Torvalds 824e186932bSPavel Emelyanov static void addrconf_forward_change(struct net *net, __s32 newf) 8251da177e4SLinus Torvalds { 8261da177e4SLinus Torvalds struct net_device *dev; 8271da177e4SLinus Torvalds struct inet6_dev *idev; 8281da177e4SLinus Torvalds 8294acd4945SBen Hutchings for_each_netdev(net, dev) { 8301da177e4SLinus Torvalds idev = __in6_dev_get(dev); 8311da177e4SLinus Torvalds if (idev) { 832e186932bSPavel Emelyanov int changed = (!idev->cnf.forwarding) ^ (!newf); 833e186932bSPavel Emelyanov idev->cnf.forwarding = newf; 8341da177e4SLinus Torvalds if (changed) 8351da177e4SLinus Torvalds dev_forward_change(idev); 8361da177e4SLinus Torvalds } 8371da177e4SLinus Torvalds } 8381da177e4SLinus Torvalds } 839c8fecf22SPavel Emelyanov 840013d97e9SFrancesco Ruggeri static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf) 841c8fecf22SPavel Emelyanov { 842bff16c2fSPavel Emelyanov struct net *net; 843013d97e9SFrancesco Ruggeri int old; 844013d97e9SFrancesco Ruggeri 845013d97e9SFrancesco Ruggeri if (!rtnl_trylock()) 846013d97e9SFrancesco Ruggeri return restart_syscall(); 847bff16c2fSPavel Emelyanov 848bff16c2fSPavel Emelyanov net = (struct net *)table->extra2; 849013d97e9SFrancesco Ruggeri old = *p; 850013d97e9SFrancesco Ruggeri *p = newf; 851c8fecf22SPavel Emelyanov 852013d97e9SFrancesco Ruggeri if (p == &net->ipv6.devconf_dflt->forwarding) { 853f3a1bfb1SNicolas Dichtel if ((!newf) ^ (!old)) 85485b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 85585b3daadSDavid Ahern NETCONFA_FORWARDING, 856f3a1bfb1SNicolas Dichtel NETCONFA_IFINDEX_DEFAULT, 857f3a1bfb1SNicolas Dichtel net->ipv6.devconf_dflt); 858013d97e9SFrancesco Ruggeri rtnl_unlock(); 859013d97e9SFrancesco Ruggeri return 0; 86088af182eSEric W. Biederman } 861b325fddbSStephen Hemminger 862e186932bSPavel Emelyanov if (p == &net->ipv6.devconf_all->forwarding) { 863d26c638cSNicolas Dichtel int old_dflt = net->ipv6.devconf_dflt->forwarding; 864d26c638cSNicolas Dichtel 865e186932bSPavel Emelyanov net->ipv6.devconf_dflt->forwarding = newf; 866d26c638cSNicolas Dichtel if ((!newf) ^ (!old_dflt)) 86785b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 86885b3daadSDavid Ahern NETCONFA_FORWARDING, 869d26c638cSNicolas Dichtel NETCONFA_IFINDEX_DEFAULT, 870d26c638cSNicolas Dichtel net->ipv6.devconf_dflt); 871d26c638cSNicolas Dichtel 872e186932bSPavel Emelyanov addrconf_forward_change(net, newf); 873f3a1bfb1SNicolas Dichtel if ((!newf) ^ (!old)) 87485b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 87585b3daadSDavid Ahern NETCONFA_FORWARDING, 876f3a1bfb1SNicolas Dichtel NETCONFA_IFINDEX_ALL, 877f3a1bfb1SNicolas Dichtel net->ipv6.devconf_all); 878013d97e9SFrancesco Ruggeri } else if ((!newf) ^ (!old)) 879c8fecf22SPavel Emelyanov dev_forward_change((struct inet6_dev *)table->extra1); 8800187bdfbSBen Hutchings rtnl_unlock(); 881c8fecf22SPavel Emelyanov 882013d97e9SFrancesco Ruggeri if (newf) 8837b4da532SDaniel Lezcano rt6_purge_dflt_routers(net); 884b325fddbSStephen Hemminger return 1; 885c8fecf22SPavel Emelyanov } 88635103d11SAndy Gospodarek 88735103d11SAndy Gospodarek static void addrconf_linkdown_change(struct net *net, __s32 newf) 88835103d11SAndy Gospodarek { 88935103d11SAndy Gospodarek struct net_device *dev; 89035103d11SAndy Gospodarek struct inet6_dev *idev; 89135103d11SAndy Gospodarek 89235103d11SAndy Gospodarek for_each_netdev(net, dev) { 89335103d11SAndy Gospodarek idev = __in6_dev_get(dev); 89435103d11SAndy Gospodarek if (idev) { 89535103d11SAndy Gospodarek int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf); 89635103d11SAndy Gospodarek 89735103d11SAndy Gospodarek idev->cnf.ignore_routes_with_linkdown = newf; 89835103d11SAndy Gospodarek if (changed) 89935103d11SAndy Gospodarek inet6_netconf_notify_devconf(dev_net(dev), 90085b3daadSDavid Ahern RTM_NEWNETCONF, 90135103d11SAndy Gospodarek NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 90235103d11SAndy Gospodarek dev->ifindex, 90335103d11SAndy Gospodarek &idev->cnf); 90435103d11SAndy Gospodarek } 90535103d11SAndy Gospodarek } 90635103d11SAndy Gospodarek } 90735103d11SAndy Gospodarek 90835103d11SAndy Gospodarek static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf) 90935103d11SAndy Gospodarek { 91035103d11SAndy Gospodarek struct net *net; 91135103d11SAndy Gospodarek int old; 91235103d11SAndy Gospodarek 91335103d11SAndy Gospodarek if (!rtnl_trylock()) 91435103d11SAndy Gospodarek return restart_syscall(); 91535103d11SAndy Gospodarek 91635103d11SAndy Gospodarek net = (struct net *)table->extra2; 91735103d11SAndy Gospodarek old = *p; 91835103d11SAndy Gospodarek *p = newf; 91935103d11SAndy Gospodarek 92035103d11SAndy Gospodarek if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) { 92135103d11SAndy Gospodarek if ((!newf) ^ (!old)) 92235103d11SAndy Gospodarek inet6_netconf_notify_devconf(net, 92385b3daadSDavid Ahern RTM_NEWNETCONF, 92435103d11SAndy Gospodarek NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 92535103d11SAndy Gospodarek NETCONFA_IFINDEX_DEFAULT, 92635103d11SAndy Gospodarek net->ipv6.devconf_dflt); 92735103d11SAndy Gospodarek rtnl_unlock(); 92835103d11SAndy Gospodarek return 0; 92935103d11SAndy Gospodarek } 93035103d11SAndy Gospodarek 93135103d11SAndy Gospodarek if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) { 93235103d11SAndy Gospodarek net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf; 93335103d11SAndy Gospodarek addrconf_linkdown_change(net, newf); 93435103d11SAndy Gospodarek if ((!newf) ^ (!old)) 93535103d11SAndy Gospodarek inet6_netconf_notify_devconf(net, 93685b3daadSDavid Ahern RTM_NEWNETCONF, 93735103d11SAndy Gospodarek NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 93835103d11SAndy Gospodarek NETCONFA_IFINDEX_ALL, 93935103d11SAndy Gospodarek net->ipv6.devconf_all); 94035103d11SAndy Gospodarek } 94135103d11SAndy Gospodarek rtnl_unlock(); 94235103d11SAndy Gospodarek 94335103d11SAndy Gospodarek return 1; 94435103d11SAndy Gospodarek } 94535103d11SAndy Gospodarek 9461da177e4SLinus Torvalds #endif 9471da177e4SLinus Torvalds 9485c578aedSstephen hemminger /* Nobody refers to this ifaddr, destroy it */ 9491da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp) 9501da177e4SLinus Torvalds { 951c2e21293Sstephen hemminger WARN_ON(!hlist_unhashed(&ifp->addr_lst)); 952547b792cSIlpo Järvinen 9531da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG 95491df42beSJoe Perches pr_debug("%s\n", __func__); 9551da177e4SLinus Torvalds #endif 9561da177e4SLinus Torvalds 9571da177e4SLinus Torvalds in6_dev_put(ifp->idev); 9581da177e4SLinus Torvalds 959c15b1ccaSHannes Frederic Sowa if (cancel_delayed_work(&ifp->dad_work)) 960c15b1ccaSHannes Frederic Sowa pr_notice("delayed DAD work was pending while freeing ifa=%p\n", 961c15b1ccaSHannes Frederic Sowa ifp); 9621da177e4SLinus Torvalds 963e9d3e084SHerbert Xu if (ifp->state != INET6_IFADDR_STATE_DEAD) { 964f3213831SJoe Perches pr_warn("Freeing alive inet6 address %p\n", ifp); 9651da177e4SLinus Torvalds return; 9661da177e4SLinus Torvalds } 9671da177e4SLinus Torvalds 968e5785985SLai Jiangshan kfree_rcu(ifp, rcu); 9691da177e4SLinus Torvalds } 9701da177e4SLinus Torvalds 971e55ffac6SBrian Haley static void 972e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp) 973e55ffac6SBrian Haley { 974502a2ffdSstephen hemminger struct list_head *p; 9758a6ce0c0SYOSHIFUJI Hideaki int ifp_scope = ipv6_addr_src_scope(&ifp->addr); 976e55ffac6SBrian Haley 977e55ffac6SBrian Haley /* 978e55ffac6SBrian Haley * Each device address list is sorted in order of scope - 979e55ffac6SBrian Haley * global before linklocal. 980e55ffac6SBrian Haley */ 981502a2ffdSstephen hemminger list_for_each(p, &idev->addr_list) { 982502a2ffdSstephen hemminger struct inet6_ifaddr *ifa 983502a2ffdSstephen hemminger = list_entry(p, struct inet6_ifaddr, if_list); 9848a6ce0c0SYOSHIFUJI Hideaki if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr)) 985e55ffac6SBrian Haley break; 986e55ffac6SBrian Haley } 987e55ffac6SBrian Haley 9888ef802aaSEric Dumazet list_add_tail_rcu(&ifp->if_list, p); 989e55ffac6SBrian Haley } 990e55ffac6SBrian Haley 9913f27fb23SEric Dumazet static u32 inet6_addr_hash(const struct net *net, const struct in6_addr *addr) 9923eb84f49SYOSHIFUJI Hideaki { 9933f27fb23SEric Dumazet u32 val = ipv6_addr_hash(addr) ^ net_hash_mix(net); 9943f27fb23SEric Dumazet 9953f27fb23SEric Dumazet return hash_32(val, IN6_ADDR_HSIZE_SHIFT); 9963eb84f49SYOSHIFUJI Hideaki } 9973eb84f49SYOSHIFUJI Hideaki 99856fc709bSEric Dumazet static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr, 999752a9292SEric Dumazet struct net_device *dev, unsigned int hash) 100056fc709bSEric Dumazet { 100156fc709bSEric Dumazet struct inet6_ifaddr *ifp; 100256fc709bSEric Dumazet 100356fc709bSEric Dumazet hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) { 100456fc709bSEric Dumazet if (!net_eq(dev_net(ifp->idev->dev), net)) 100556fc709bSEric Dumazet continue; 100656fc709bSEric Dumazet if (ipv6_addr_equal(&ifp->addr, addr)) { 100756fc709bSEric Dumazet if (!dev || ifp->idev->dev == dev) 100856fc709bSEric Dumazet return true; 100956fc709bSEric Dumazet } 101056fc709bSEric Dumazet } 101156fc709bSEric Dumazet return false; 101256fc709bSEric Dumazet } 101356fc709bSEric Dumazet 1014f3d9832eSDavid Ahern static int ipv6_add_addr_hash(struct net_device *dev, struct inet6_ifaddr *ifa) 1015f3d9832eSDavid Ahern { 10163f27fb23SEric Dumazet unsigned int hash = inet6_addr_hash(dev_net(dev), &ifa->addr); 1017f3d9832eSDavid Ahern int err = 0; 1018f3d9832eSDavid Ahern 1019f3d9832eSDavid Ahern spin_lock(&addrconf_hash_lock); 1020f3d9832eSDavid Ahern 1021f3d9832eSDavid Ahern /* Ignore adding duplicate addresses on an interface */ 1022752a9292SEric Dumazet if (ipv6_chk_same_addr(dev_net(dev), &ifa->addr, dev, hash)) { 1023e32ac250SJoe Perches netdev_dbg(dev, "ipv6_add_addr: already assigned\n"); 1024f3d9832eSDavid Ahern err = -EEXIST; 1025752a9292SEric Dumazet } else { 1026752a9292SEric Dumazet hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]); 1027f3d9832eSDavid Ahern } 1028f3d9832eSDavid Ahern 1029f3d9832eSDavid Ahern spin_unlock(&addrconf_hash_lock); 1030f3d9832eSDavid Ahern 1031f3d9832eSDavid Ahern return err; 1032f3d9832eSDavid Ahern } 1033f3d9832eSDavid Ahern 10341da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */ 10351da177e4SLinus Torvalds 10361da177e4SLinus Torvalds static struct inet6_ifaddr * 1037e6464b8cSDavid Ahern ipv6_add_addr(struct inet6_dev *idev, struct ifa6_config *cfg, 1038de95e047SDavid Ahern bool can_block, struct netlink_ext_ack *extack) 10391da177e4SLinus Torvalds { 1040f3d9832eSDavid Ahern gfp_t gfp_flags = can_block ? GFP_KERNEL : GFP_ATOMIC; 1041e6464b8cSDavid Ahern int addr_type = ipv6_addr_type(cfg->pfx); 1042df789fe7SDavid Forster struct net *net = dev_net(idev->dev); 10431da177e4SLinus Torvalds struct inet6_ifaddr *ifa = NULL; 1044360a9887SDavid Ahern struct fib6_info *f6i = NULL; 10451da177e4SLinus Torvalds int err = 0; 1046d68b8270SYOSHIFUJI Hideaki 1047d68b8270SYOSHIFUJI Hideaki if (addr_type == IPV6_ADDR_ANY || 1048f17f7648SHangbin Liu (addr_type & IPV6_ADDR_MULTICAST && 1049f17f7648SHangbin Liu !(cfg->ifa_flags & IFA_F_MCAUTOJOIN)) || 1050d68b8270SYOSHIFUJI Hideaki (!(idev->dev->flags & IFF_LOOPBACK) && 10513ede0bbcSRobert Shearman !netif_is_l3_master(idev->dev) && 1052d68b8270SYOSHIFUJI Hideaki addr_type & IPV6_ADDR_LOOPBACK)) 1053d68b8270SYOSHIFUJI Hideaki return ERR_PTR(-EADDRNOTAVAIL); 10541da177e4SLinus Torvalds 10551da177e4SLinus Torvalds if (idev->dead) { 10561da177e4SLinus Torvalds err = -ENODEV; /*XXX*/ 1057f3d9832eSDavid Ahern goto out; 10581da177e4SLinus Torvalds } 10591da177e4SLinus Torvalds 106056d417b1SBrian Haley if (idev->cnf.disable_ipv6) { 10619bdd8d40SBrian Haley err = -EACCES; 1062f3d9832eSDavid Ahern goto out; 10639bdd8d40SBrian Haley } 10649bdd8d40SBrian Haley 1065ff7883eaSDavid Ahern /* validator notifier needs to be blocking; 1066ff7883eaSDavid Ahern * do not call in atomic context 1067ff7883eaSDavid Ahern */ 1068ff7883eaSDavid Ahern if (can_block) { 1069ff7883eaSDavid Ahern struct in6_validator_info i6vi = { 1070e6464b8cSDavid Ahern .i6vi_addr = *cfg->pfx, 1071ff7883eaSDavid Ahern .i6vi_dev = idev, 1072de95e047SDavid Ahern .extack = extack, 1073ff7883eaSDavid Ahern }; 1074ff7883eaSDavid Ahern 10753ad7d246SKrister Johansen err = inet6addr_validator_notifier_call_chain(NETDEV_UP, &i6vi); 10763ad7d246SKrister Johansen err = notifier_to_errno(err); 1077f3d9832eSDavid Ahern if (err < 0) 10781da177e4SLinus Torvalds goto out; 1079ff7883eaSDavid Ahern } 10801da177e4SLinus Torvalds 1081f3d9832eSDavid Ahern ifa = kzalloc(sizeof(*ifa), gfp_flags); 108263159f29SIan Morris if (!ifa) { 10831da177e4SLinus Torvalds err = -ENOBUFS; 10841da177e4SLinus Torvalds goto out; 10851da177e4SLinus Torvalds } 10861da177e4SLinus Torvalds 1087e6464b8cSDavid Ahern f6i = addrconf_f6i_alloc(net, idev, cfg->pfx, false, gfp_flags); 1088360a9887SDavid Ahern if (IS_ERR(f6i)) { 1089360a9887SDavid Ahern err = PTR_ERR(f6i); 1090360a9887SDavid Ahern f6i = NULL; 10911da177e4SLinus Torvalds goto out; 10921da177e4SLinus Torvalds } 10931da177e4SLinus Torvalds 1094df789fe7SDavid Forster if (net->ipv6.devconf_all->disable_policy || 1095df789fe7SDavid Forster idev->cnf.disable_policy) 1096360a9887SDavid Ahern f6i->dst_nopolicy = true; 1097df789fe7SDavid Forster 1098bba24896SJiri Pirko neigh_parms_data_state_setall(idev->nd_parms); 1099bba24896SJiri Pirko 1100e6464b8cSDavid Ahern ifa->addr = *cfg->pfx; 1101e6464b8cSDavid Ahern if (cfg->peer_pfx) 1102e6464b8cSDavid Ahern ifa->peer_addr = *cfg->peer_pfx; 11031da177e4SLinus Torvalds 11041da177e4SLinus Torvalds spin_lock_init(&ifa->lock); 1105c15b1ccaSHannes Frederic Sowa INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work); 1106c2e21293Sstephen hemminger INIT_HLIST_NODE(&ifa->addr_lst); 1107e6464b8cSDavid Ahern ifa->scope = cfg->scope; 1108e6464b8cSDavid Ahern ifa->prefix_len = cfg->plen; 11098308f3ffSDavid Ahern ifa->rt_priority = cfg->rt_priority; 1110e6464b8cSDavid Ahern ifa->flags = cfg->ifa_flags; 111166eb9f86SMahesh Bandewar /* No need to add the TENTATIVE flag for addresses with NODAD */ 1112e6464b8cSDavid Ahern if (!(cfg->ifa_flags & IFA_F_NODAD)) 111366eb9f86SMahesh Bandewar ifa->flags |= IFA_F_TENTATIVE; 1114e6464b8cSDavid Ahern ifa->valid_lft = cfg->valid_lft; 1115e6464b8cSDavid Ahern ifa->prefered_lft = cfg->preferred_lft; 11161da177e4SLinus Torvalds ifa->cstamp = ifa->tstamp = jiffies; 1117617fe29dSDaniel Borkmann ifa->tokenized = false; 11181da177e4SLinus Torvalds 1119360a9887SDavid Ahern ifa->rt = f6i; 112057f5f544SKeir Fraser 11211da177e4SLinus Torvalds ifa->idev = idev; 1122f3d9832eSDavid Ahern in6_dev_hold(idev); 1123f3d9832eSDavid Ahern 11241da177e4SLinus Torvalds /* For caller */ 1125271201c0SReshetova, Elena refcount_set(&ifa->refcnt, 1); 11261da177e4SLinus Torvalds 1127f3d9832eSDavid Ahern rcu_read_lock_bh(); 11281da177e4SLinus Torvalds 1129f3d9832eSDavid Ahern err = ipv6_add_addr_hash(idev->dev, ifa); 1130f3d9832eSDavid Ahern if (err < 0) { 1131f3d9832eSDavid Ahern rcu_read_unlock_bh(); 1132f3d9832eSDavid Ahern goto out; 1133f3d9832eSDavid Ahern } 11341da177e4SLinus Torvalds 11351da177e4SLinus Torvalds write_lock(&idev->lock); 1136f3d9832eSDavid Ahern 11371da177e4SLinus Torvalds /* Add to inet6_dev unicast addr list. */ 1138e55ffac6SBrian Haley ipv6_link_dev_addr(idev, ifa); 11391da177e4SLinus Torvalds 11401da177e4SLinus Torvalds if (ifa->flags&IFA_F_TEMPORARY) { 1141372e6c8fSstephen hemminger list_add(&ifa->tmp_list, &idev->tempaddr_list); 11421da177e4SLinus Torvalds in6_ifa_hold(ifa); 11431da177e4SLinus Torvalds } 11441da177e4SLinus Torvalds 11451da177e4SLinus Torvalds in6_ifa_hold(ifa); 11461da177e4SLinus Torvalds write_unlock(&idev->lock); 1147f3d9832eSDavid Ahern 11488814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 11491da177e4SLinus Torvalds 1150f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_UP, ifa); 1151f3d9832eSDavid Ahern out: 1152f3d9832eSDavid Ahern if (unlikely(err < 0)) { 1153360a9887SDavid Ahern fib6_info_release(f6i); 115493531c67SDavid Ahern 1155f3d9832eSDavid Ahern if (ifa) { 1156f3d9832eSDavid Ahern if (ifa->idev) 1157f3d9832eSDavid Ahern in6_dev_put(ifa->idev); 11581da177e4SLinus Torvalds kfree(ifa); 1159f3d9832eSDavid Ahern } 11601da177e4SLinus Torvalds ifa = ERR_PTR(err); 11611da177e4SLinus Torvalds } 11621da177e4SLinus Torvalds 11631da177e4SLinus Torvalds return ifa; 11641da177e4SLinus Torvalds } 11651da177e4SLinus Torvalds 11665b84efecSThomas Haller enum cleanup_prefix_rt_t { 11675b84efecSThomas Haller CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */ 11685b84efecSThomas Haller CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */ 11695b84efecSThomas Haller CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */ 11705b84efecSThomas Haller }; 11715b84efecSThomas Haller 11725b84efecSThomas Haller /* 11735b84efecSThomas Haller * Check, whether the prefix for ifp would still need a prefix route 11745b84efecSThomas Haller * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_* 11755b84efecSThomas Haller * constants. 11765b84efecSThomas Haller * 11775b84efecSThomas Haller * 1) we don't purge prefix if address was not permanent. 11785b84efecSThomas Haller * prefix is managed by its own lifetime. 11795b84efecSThomas Haller * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE. 11805b84efecSThomas Haller * 3) if there are no addresses, delete prefix. 11815b84efecSThomas Haller * 4) if there are still other permanent address(es), 11825b84efecSThomas Haller * corresponding prefix is still permanent. 11835b84efecSThomas Haller * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE, 11845b84efecSThomas Haller * don't purge the prefix, assume user space is managing it. 11855b84efecSThomas Haller * 6) otherwise, update prefix lifetime to the 11865b84efecSThomas Haller * longest valid lifetime among the corresponding 11875b84efecSThomas Haller * addresses on the device. 11885b84efecSThomas Haller * Note: subsequent RA will update lifetime. 11895b84efecSThomas Haller **/ 11905b84efecSThomas Haller static enum cleanup_prefix_rt_t 11915b84efecSThomas Haller check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires) 11925b84efecSThomas Haller { 11935b84efecSThomas Haller struct inet6_ifaddr *ifa; 11945b84efecSThomas Haller struct inet6_dev *idev = ifp->idev; 11955b84efecSThomas Haller unsigned long lifetime; 11965b84efecSThomas Haller enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL; 11975b84efecSThomas Haller 11985b84efecSThomas Haller *expires = jiffies; 11995b84efecSThomas Haller 12005b84efecSThomas Haller list_for_each_entry(ifa, &idev->addr_list, if_list) { 12015b84efecSThomas Haller if (ifa == ifp) 12025b84efecSThomas Haller continue; 1203e75913c9SZhiqiang Liu if (ifa->prefix_len != ifp->prefix_len || 1204e75913c9SZhiqiang Liu !ipv6_prefix_equal(&ifa->addr, &ifp->addr, 12055b84efecSThomas Haller ifp->prefix_len)) 12065b84efecSThomas Haller continue; 12075b84efecSThomas Haller if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE)) 12085b84efecSThomas Haller return CLEANUP_PREFIX_RT_NOP; 12095b84efecSThomas Haller 12105b84efecSThomas Haller action = CLEANUP_PREFIX_RT_EXPIRE; 12115b84efecSThomas Haller 12125b84efecSThomas Haller spin_lock(&ifa->lock); 12135b84efecSThomas Haller 12145b84efecSThomas Haller lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ); 12155b84efecSThomas Haller /* 12165b84efecSThomas Haller * Note: Because this address is 12175b84efecSThomas Haller * not permanent, lifetime < 12185b84efecSThomas Haller * LONG_MAX / HZ here. 12195b84efecSThomas Haller */ 12205b84efecSThomas Haller if (time_before(*expires, ifa->tstamp + lifetime * HZ)) 12215b84efecSThomas Haller *expires = ifa->tstamp + lifetime * HZ; 12225b84efecSThomas Haller spin_unlock(&ifa->lock); 12235b84efecSThomas Haller } 12245b84efecSThomas Haller 12255b84efecSThomas Haller return action; 12265b84efecSThomas Haller } 12275b84efecSThomas Haller 12285b84efecSThomas Haller static void 12295b84efecSThomas Haller cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt) 12305b84efecSThomas Haller { 123193c2fb25SDavid Ahern struct fib6_info *f6i; 12325b84efecSThomas Haller 12332b2450caSDavid Ahern f6i = addrconf_get_prefix_route(&ifp->addr, ifp->prefix_len, 12342b2450caSDavid Ahern ifp->idev->dev, 0, RTF_DEFAULT, true); 123593c2fb25SDavid Ahern if (f6i) { 12365b84efecSThomas Haller if (del_rt) 123793c2fb25SDavid Ahern ip6_del_rt(dev_net(ifp->idev->dev), f6i); 12385b84efecSThomas Haller else { 123993c2fb25SDavid Ahern if (!(f6i->fib6_flags & RTF_EXPIRES)) 124093c2fb25SDavid Ahern fib6_set_expires(f6i, expires); 124193c2fb25SDavid Ahern fib6_info_release(f6i); 12425b84efecSThomas Haller } 12435b84efecSThomas Haller } 12445b84efecSThomas Haller } 12455b84efecSThomas Haller 12465b84efecSThomas Haller 12471da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */ 12481da177e4SLinus Torvalds 12491da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp) 12501da177e4SLinus Torvalds { 12514c5ff6a6SHerbert Xu int state; 12525b84efecSThomas Haller enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP; 12535b84efecSThomas Haller unsigned long expires; 12541da177e4SLinus Torvalds 1255c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 1256c15b1ccaSHannes Frederic Sowa 12578e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 12584c5ff6a6SHerbert Xu state = ifp->state; 1259e9d3e084SHerbert Xu ifp->state = INET6_IFADDR_STATE_DEAD; 12608e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 12614c5ff6a6SHerbert Xu 12624c5ff6a6SHerbert Xu if (state == INET6_IFADDR_STATE_DEAD) 12634c5ff6a6SHerbert Xu goto out; 12641da177e4SLinus Torvalds 12655c578aedSstephen hemminger spin_lock_bh(&addrconf_hash_lock); 12665c578aedSstephen hemminger hlist_del_init_rcu(&ifp->addr_lst); 12675c578aedSstephen hemminger spin_unlock_bh(&addrconf_hash_lock); 12681da177e4SLinus Torvalds 12695b84efecSThomas Haller write_lock_bh(&ifp->idev->lock); 12705d9efa7eSDavid S. Miller 12711da177e4SLinus Torvalds if (ifp->flags&IFA_F_TEMPORARY) { 1272372e6c8fSstephen hemminger list_del(&ifp->tmp_list); 12731da177e4SLinus Torvalds if (ifp->ifpub) { 12741da177e4SLinus Torvalds in6_ifa_put(ifp->ifpub); 12751da177e4SLinus Torvalds ifp->ifpub = NULL; 12761da177e4SLinus Torvalds } 12771da177e4SLinus Torvalds __in6_ifa_put(ifp); 12781da177e4SLinus Torvalds } 12791da177e4SLinus Torvalds 12805b84efecSThomas Haller if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE)) 12815b84efecSThomas Haller action = check_cleanup_prefix_route(ifp, &expires); 12825b84efecSThomas Haller 12838ef802aaSEric Dumazet list_del_rcu(&ifp->if_list); 12841da177e4SLinus Torvalds __in6_ifa_put(ifp); 1285502a2ffdSstephen hemminger 12865b84efecSThomas Haller write_unlock_bh(&ifp->idev->lock); 12871da177e4SLinus Torvalds 1288c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifp); 1289b2238566SAndrey Vagin 12901da177e4SLinus Torvalds ipv6_ifa_notify(RTM_DELADDR, ifp); 12911da177e4SLinus Torvalds 1292f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_DOWN, ifp); 12931da177e4SLinus Torvalds 12945b84efecSThomas Haller if (action != CLEANUP_PREFIX_RT_NOP) { 12955b84efecSThomas Haller cleanup_prefix_route(ifp, expires, 12965b84efecSThomas Haller action == CLEANUP_PREFIX_RT_DEL); 12971da177e4SLinus Torvalds } 12981da177e4SLinus Torvalds 1299c3968a85SDaniel Walter /* clean up prefsrc entries */ 1300c3968a85SDaniel Walter rt6_remove_prefsrc(ifp); 13014c5ff6a6SHerbert Xu out: 13021da177e4SLinus Torvalds in6_ifa_put(ifp); 13031da177e4SLinus Torvalds } 13041da177e4SLinus Torvalds 1305fffcefe9SEric Dumazet static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, 1306fffcefe9SEric Dumazet struct inet6_ifaddr *ift, 1307fffcefe9SEric Dumazet bool block) 13081da177e4SLinus Torvalds { 13091da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 13101da177e4SLinus Torvalds struct in6_addr addr, *tmpaddr; 1311e6464b8cSDavid Ahern unsigned long tmp_tstamp, age; 1312eac55bf9SBenoit Boissinot unsigned long regen_advance; 1313e6464b8cSDavid Ahern struct ifa6_config cfg; 13141da177e4SLinus Torvalds int ret = 0; 131576f793e3SLorenzo Colitti unsigned long now = jiffies; 131676506a98SJiri Bohac long max_desync_factor; 13177aa8e63fSJiri Bohac s32 cnf_temp_preferred_lft; 13181da177e4SLinus Torvalds 131953bd6749SJiri Pirko write_lock_bh(&idev->lock); 13201da177e4SLinus Torvalds if (ift) { 13211da177e4SLinus Torvalds spin_lock_bh(&ift->lock); 13221da177e4SLinus Torvalds memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8); 13231da177e4SLinus Torvalds spin_unlock_bh(&ift->lock); 13241da177e4SLinus Torvalds tmpaddr = &addr; 13251da177e4SLinus Torvalds } else { 13261da177e4SLinus Torvalds tmpaddr = NULL; 13271da177e4SLinus Torvalds } 13281da177e4SLinus Torvalds retry: 13291da177e4SLinus Torvalds in6_dev_hold(idev); 13301da177e4SLinus Torvalds if (idev->cnf.use_tempaddr <= 0) { 133153bd6749SJiri Pirko write_unlock_bh(&idev->lock); 1332f3213831SJoe Perches pr_info("%s: use_tempaddr is disabled\n", __func__); 13331da177e4SLinus Torvalds in6_dev_put(idev); 13341da177e4SLinus Torvalds ret = -1; 13351da177e4SLinus Torvalds goto out; 13361da177e4SLinus Torvalds } 13371da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 13381da177e4SLinus Torvalds if (ifp->regen_count++ >= idev->cnf.regen_max_retry) { 13391da177e4SLinus Torvalds idev->cnf.use_tempaddr = -1; /*XXX*/ 13401da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 134153bd6749SJiri Pirko write_unlock_bh(&idev->lock); 1342f3213831SJoe Perches pr_warn("%s: regeneration time exceeded - disabled temporary address support\n", 1343f3213831SJoe Perches __func__); 13441da177e4SLinus Torvalds in6_dev_put(idev); 13451da177e4SLinus Torvalds ret = -1; 13461da177e4SLinus Torvalds goto out; 13471da177e4SLinus Torvalds } 13481da177e4SLinus Torvalds in6_ifa_hold(ifp); 13491da177e4SLinus Torvalds memcpy(addr.s6_addr, ifp->addr.s6_addr, 8); 13509d6280daSJiri Bohac ipv6_try_regen_rndid(idev, tmpaddr); 13511da177e4SLinus Torvalds memcpy(&addr.s6_addr[8], idev->rndid, 8); 135276f793e3SLorenzo Colitti age = (now - ifp->tstamp) / HZ; 13531da177e4SLinus Torvalds 1354eac55bf9SBenoit Boissinot regen_advance = idev->cnf.regen_max_retry * 1355eac55bf9SBenoit Boissinot idev->cnf.dad_transmits * 13561f9248e5SJiri Pirko NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ; 135776506a98SJiri Bohac 135876506a98SJiri Bohac /* recalculate max_desync_factor each time and update 135976506a98SJiri Bohac * idev->desync_factor if it's larger 136076506a98SJiri Bohac */ 13617aa8e63fSJiri Bohac cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft); 136276506a98SJiri Bohac max_desync_factor = min_t(__u32, 136376506a98SJiri Bohac idev->cnf.max_desync_factor, 13647aa8e63fSJiri Bohac cnf_temp_preferred_lft - regen_advance); 136576506a98SJiri Bohac 136676506a98SJiri Bohac if (unlikely(idev->desync_factor > max_desync_factor)) { 136776506a98SJiri Bohac if (max_desync_factor > 0) { 136876506a98SJiri Bohac get_random_bytes(&idev->desync_factor, 136976506a98SJiri Bohac sizeof(idev->desync_factor)); 137076506a98SJiri Bohac idev->desync_factor %= max_desync_factor; 137176506a98SJiri Bohac } else { 137276506a98SJiri Bohac idev->desync_factor = 0; 137376506a98SJiri Bohac } 137476506a98SJiri Bohac } 137576506a98SJiri Bohac 13763f2d67b6SDavid Ahern memset(&cfg, 0, sizeof(cfg)); 1377e6464b8cSDavid Ahern cfg.valid_lft = min_t(__u32, ifp->valid_lft, 137876506a98SJiri Bohac idev->cnf.temp_valid_lft + age); 1379e6464b8cSDavid Ahern cfg.preferred_lft = cnf_temp_preferred_lft + age - idev->desync_factor; 1380e6464b8cSDavid Ahern cfg.preferred_lft = min_t(__u32, ifp->prefered_lft, cfg.preferred_lft); 1381e6464b8cSDavid Ahern 1382e6464b8cSDavid Ahern cfg.plen = ifp->prefix_len; 138376506a98SJiri Bohac tmp_tstamp = ifp->tstamp; 138476506a98SJiri Bohac spin_unlock_bh(&ifp->lock); 138576506a98SJiri Bohac 138653bd6749SJiri Pirko write_unlock_bh(&idev->lock); 138795c385b4SNeil Horman 1388eac55bf9SBenoit Boissinot /* A temporary address is created only if this calculated Preferred 1389eac55bf9SBenoit Boissinot * Lifetime is greater than REGEN_ADVANCE time units. In particular, 1390eac55bf9SBenoit Boissinot * an implementation must not create a temporary address with a zero 1391eac55bf9SBenoit Boissinot * Preferred Lifetime. 1392ecab6701SHeiner Kallweit * Use age calculation as in addrconf_verify to avoid unnecessary 1393ecab6701SHeiner Kallweit * temporary addresses being generated. 1394eac55bf9SBenoit Boissinot */ 1395ecab6701SHeiner Kallweit age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; 1396e6464b8cSDavid Ahern if (cfg.preferred_lft <= regen_advance + age) { 1397eac55bf9SBenoit Boissinot in6_ifa_put(ifp); 1398eac55bf9SBenoit Boissinot in6_dev_put(idev); 1399eac55bf9SBenoit Boissinot ret = -1; 1400eac55bf9SBenoit Boissinot goto out; 1401eac55bf9SBenoit Boissinot } 1402eac55bf9SBenoit Boissinot 1403e6464b8cSDavid Ahern cfg.ifa_flags = IFA_F_TEMPORARY; 140495c385b4SNeil Horman /* set in addrconf_prefix_rcv() */ 140595c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 1406e6464b8cSDavid Ahern cfg.ifa_flags |= IFA_F_OPTIMISTIC; 140795c385b4SNeil Horman 1408e6464b8cSDavid Ahern cfg.pfx = &addr; 1409e6464b8cSDavid Ahern cfg.scope = ipv6_addr_scope(cfg.pfx); 1410e6464b8cSDavid Ahern 1411e6464b8cSDavid Ahern ift = ipv6_add_addr(idev, &cfg, block, NULL); 14124b08a8f1SHannes Frederic Sowa if (IS_ERR(ift)) { 14131da177e4SLinus Torvalds in6_ifa_put(ifp); 14141da177e4SLinus Torvalds in6_dev_put(idev); 1415f3213831SJoe Perches pr_info("%s: retry temporary address regeneration\n", __func__); 14161da177e4SLinus Torvalds tmpaddr = &addr; 141753bd6749SJiri Pirko write_lock_bh(&idev->lock); 14181da177e4SLinus Torvalds goto retry; 14191da177e4SLinus Torvalds } 14201da177e4SLinus Torvalds 14211da177e4SLinus Torvalds spin_lock_bh(&ift->lock); 14221da177e4SLinus Torvalds ift->ifpub = ifp; 142376f793e3SLorenzo Colitti ift->cstamp = now; 14241da177e4SLinus Torvalds ift->tstamp = tmp_tstamp; 14251da177e4SLinus Torvalds spin_unlock_bh(&ift->lock); 14261da177e4SLinus Torvalds 1427cf22f9a2SDavid S. Miller addrconf_dad_start(ift); 14281da177e4SLinus Torvalds in6_ifa_put(ift); 14291da177e4SLinus Torvalds in6_dev_put(idev); 14301da177e4SLinus Torvalds out: 14311da177e4SLinus Torvalds return ret; 14321da177e4SLinus Torvalds } 14331da177e4SLinus Torvalds 14341da177e4SLinus Torvalds /* 1435072047e4SYOSHIFUJI Hideaki * Choose an appropriate source address (RFC3484) 14361da177e4SLinus Torvalds */ 1437a9b05723SYOSHIFUJI Hideaki enum { 1438a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_INIT = 0, 1439a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_LOCAL, 1440a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_SCOPE, 1441a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PREFERRED, 1442a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6 1443a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_HOA, 1444a9b05723SYOSHIFUJI Hideaki #endif 1445a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_OIF, 1446a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_LABEL, 1447a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PRIVACY, 1448a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_ORCHID, 1449a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PREFIX, 14507fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 14517fd2561eSErik Kline IPV6_SADDR_RULE_NOT_OPTIMISTIC, 14527fd2561eSErik Kline #endif 1453a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_MAX 1454072047e4SYOSHIFUJI Hideaki }; 1455072047e4SYOSHIFUJI Hideaki 1456a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score { 1457a9b05723SYOSHIFUJI Hideaki int rule; 1458a9b05723SYOSHIFUJI Hideaki int addr_type; 1459a9b05723SYOSHIFUJI Hideaki struct inet6_ifaddr *ifa; 1460a9b05723SYOSHIFUJI Hideaki DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX); 1461a9b05723SYOSHIFUJI Hideaki int scopedist; 1462a9b05723SYOSHIFUJI Hideaki int matchlen; 1463a9b05723SYOSHIFUJI Hideaki }; 1464a9b05723SYOSHIFUJI Hideaki 1465a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst { 14669acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *addr; 1467a9b05723SYOSHIFUJI Hideaki int ifindex; 1468a9b05723SYOSHIFUJI Hideaki int scope; 1469a9b05723SYOSHIFUJI Hideaki int label; 14707cbca67cSYOSHIFUJI Hideaki unsigned int prefs; 1471a9b05723SYOSHIFUJI Hideaki }; 1472072047e4SYOSHIFUJI Hideaki 1473b6f99a21SDave Jones static inline int ipv6_saddr_preferred(int type) 14741da177e4SLinus Torvalds { 147545bb0060SUlrich Weber if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK)) 1476072047e4SYOSHIFUJI Hideaki return 1; 1477072047e4SYOSHIFUJI Hideaki return 0; 14781da177e4SLinus Torvalds } 14791da177e4SLinus Torvalds 148035e015e1SMatteo Croce static bool ipv6_use_optimistic_addr(struct net *net, 148135e015e1SMatteo Croce struct inet6_dev *idev) 14827fd2561eSErik Kline { 14837fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 148435e015e1SMatteo Croce if (!idev) 148535e015e1SMatteo Croce return false; 148635e015e1SMatteo Croce if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad) 148735e015e1SMatteo Croce return false; 148835e015e1SMatteo Croce if (!net->ipv6.devconf_all->use_optimistic && !idev->cnf.use_optimistic) 148935e015e1SMatteo Croce return false; 149035e015e1SMatteo Croce 149135e015e1SMatteo Croce return true; 14927fd2561eSErik Kline #else 14937fd2561eSErik Kline return false; 14947fd2561eSErik Kline #endif 14957fd2561eSErik Kline } 14967fd2561eSErik Kline 1497f1c02cfbSSabrina Dubroca static bool ipv6_allow_optimistic_dad(struct net *net, 1498f1c02cfbSSabrina Dubroca struct inet6_dev *idev) 1499f1c02cfbSSabrina Dubroca { 1500f1c02cfbSSabrina Dubroca #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 1501f1c02cfbSSabrina Dubroca if (!idev) 1502f1c02cfbSSabrina Dubroca return false; 1503f1c02cfbSSabrina Dubroca if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad) 1504f1c02cfbSSabrina Dubroca return false; 1505f1c02cfbSSabrina Dubroca 1506f1c02cfbSSabrina Dubroca return true; 1507f1c02cfbSSabrina Dubroca #else 1508f1c02cfbSSabrina Dubroca return false; 1509f1c02cfbSSabrina Dubroca #endif 1510f1c02cfbSSabrina Dubroca } 1511f1c02cfbSSabrina Dubroca 15123de23255SBenjamin Thery static int ipv6_get_saddr_eval(struct net *net, 15133de23255SBenjamin Thery struct ipv6_saddr_score *score, 1514a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst *dst, 1515a9b05723SYOSHIFUJI Hideaki int i) 1516a9b05723SYOSHIFUJI Hideaki { 1517a9b05723SYOSHIFUJI Hideaki int ret; 1518a9b05723SYOSHIFUJI Hideaki 1519a9b05723SYOSHIFUJI Hideaki if (i <= score->rule) { 1520a9b05723SYOSHIFUJI Hideaki switch (i) { 1521a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_SCOPE: 1522a9b05723SYOSHIFUJI Hideaki ret = score->scopedist; 1523a9b05723SYOSHIFUJI Hideaki break; 1524a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFIX: 1525a9b05723SYOSHIFUJI Hideaki ret = score->matchlen; 1526a9b05723SYOSHIFUJI Hideaki break; 1527a9b05723SYOSHIFUJI Hideaki default: 1528a9b05723SYOSHIFUJI Hideaki ret = !!test_bit(i, score->scorebits); 1529a9b05723SYOSHIFUJI Hideaki } 1530a9b05723SYOSHIFUJI Hideaki goto out; 1531a9b05723SYOSHIFUJI Hideaki } 1532a9b05723SYOSHIFUJI Hideaki 1533a9b05723SYOSHIFUJI Hideaki switch (i) { 1534a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_INIT: 1535a9b05723SYOSHIFUJI Hideaki /* Rule 0: remember if hiscore is not ready yet */ 1536a9b05723SYOSHIFUJI Hideaki ret = !!score->ifa; 1537a9b05723SYOSHIFUJI Hideaki break; 1538a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_LOCAL: 1539a9b05723SYOSHIFUJI Hideaki /* Rule 1: Prefer same address */ 1540a9b05723SYOSHIFUJI Hideaki ret = ipv6_addr_equal(&score->ifa->addr, dst->addr); 1541a9b05723SYOSHIFUJI Hideaki break; 1542a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_SCOPE: 1543a9b05723SYOSHIFUJI Hideaki /* Rule 2: Prefer appropriate scope 1544a9b05723SYOSHIFUJI Hideaki * 1545a9b05723SYOSHIFUJI Hideaki * ret 1546a9b05723SYOSHIFUJI Hideaki * ^ 1547a9b05723SYOSHIFUJI Hideaki * -1 | d 15 1548a9b05723SYOSHIFUJI Hideaki * ---+--+-+---> scope 1549a9b05723SYOSHIFUJI Hideaki * | 1550a9b05723SYOSHIFUJI Hideaki * | d is scope of the destination. 1551a9b05723SYOSHIFUJI Hideaki * B-d | \ 1552a9b05723SYOSHIFUJI Hideaki * | \ <- smaller scope is better if 1553db9c7c39Sstephen hemminger * B-15 | \ if scope is enough for destination. 1554a9b05723SYOSHIFUJI Hideaki * | ret = B - scope (-1 <= scope >= d <= 15). 1555a9b05723SYOSHIFUJI Hideaki * d-C-1 | / 1556a9b05723SYOSHIFUJI Hideaki * |/ <- greater is better 1557a9b05723SYOSHIFUJI Hideaki * -C / if scope is not enough for destination. 1558a9b05723SYOSHIFUJI Hideaki * /| ret = scope - C (-1 <= d < scope <= 15). 1559a9b05723SYOSHIFUJI Hideaki * 1560a9b05723SYOSHIFUJI Hideaki * d - C - 1 < B -15 (for all -1 <= d <= 15). 1561a9b05723SYOSHIFUJI Hideaki * C > d + 14 - B >= 15 + 14 - B = 29 - B. 1562a9b05723SYOSHIFUJI Hideaki * Assume B = 0 and we get C > 29. 1563a9b05723SYOSHIFUJI Hideaki */ 1564a9b05723SYOSHIFUJI Hideaki ret = __ipv6_addr_src_scope(score->addr_type); 1565a9b05723SYOSHIFUJI Hideaki if (ret >= dst->scope) 1566a9b05723SYOSHIFUJI Hideaki ret = -ret; 1567a9b05723SYOSHIFUJI Hideaki else 1568a9b05723SYOSHIFUJI Hideaki ret -= 128; /* 30 is enough */ 1569a9b05723SYOSHIFUJI Hideaki score->scopedist = ret; 1570a9b05723SYOSHIFUJI Hideaki break; 1571a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFERRED: 15727fd2561eSErik Kline { 1573a9b05723SYOSHIFUJI Hideaki /* Rule 3: Avoid deprecated and optimistic addresses */ 15747fd2561eSErik Kline u8 avoid = IFA_F_DEPRECATED; 15757fd2561eSErik Kline 157635e015e1SMatteo Croce if (!ipv6_use_optimistic_addr(net, score->ifa->idev)) 15777fd2561eSErik Kline avoid |= IFA_F_OPTIMISTIC; 1578a9b05723SYOSHIFUJI Hideaki ret = ipv6_saddr_preferred(score->addr_type) || 15797fd2561eSErik Kline !(score->ifa->flags & avoid); 1580a9b05723SYOSHIFUJI Hideaki break; 15817fd2561eSErik Kline } 1582a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6 1583a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_HOA: 15847cbca67cSYOSHIFUJI Hideaki { 1585a9b05723SYOSHIFUJI Hideaki /* Rule 4: Prefer home address */ 15867cbca67cSYOSHIFUJI Hideaki int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA); 15877cbca67cSYOSHIFUJI Hideaki ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome; 1588a9b05723SYOSHIFUJI Hideaki break; 15897cbca67cSYOSHIFUJI Hideaki } 1590a9b05723SYOSHIFUJI Hideaki #endif 1591a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_OIF: 1592a9b05723SYOSHIFUJI Hideaki /* Rule 5: Prefer outgoing interface */ 1593a9b05723SYOSHIFUJI Hideaki ret = (!dst->ifindex || 1594a9b05723SYOSHIFUJI Hideaki dst->ifindex == score->ifa->idev->dev->ifindex); 1595a9b05723SYOSHIFUJI Hideaki break; 1596a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_LABEL: 1597a9b05723SYOSHIFUJI Hideaki /* Rule 6: Prefer matching label */ 15983de23255SBenjamin Thery ret = ipv6_addr_label(net, 15993de23255SBenjamin Thery &score->ifa->addr, score->addr_type, 1600a9b05723SYOSHIFUJI Hideaki score->ifa->idev->dev->ifindex) == dst->label; 1601a9b05723SYOSHIFUJI Hideaki break; 1602a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PRIVACY: 16037cbca67cSYOSHIFUJI Hideaki { 1604a9b05723SYOSHIFUJI Hideaki /* Rule 7: Prefer public address 160525985edcSLucas De Marchi * Note: prefer temporary address if use_tempaddr >= 2 1606a9b05723SYOSHIFUJI Hideaki */ 16077cbca67cSYOSHIFUJI Hideaki int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ? 16087cbca67cSYOSHIFUJI Hideaki !!(dst->prefs & IPV6_PREFER_SRC_TMP) : 16097cbca67cSYOSHIFUJI Hideaki score->ifa->idev->cnf.use_tempaddr >= 2; 16107cbca67cSYOSHIFUJI Hideaki ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp; 1611a9b05723SYOSHIFUJI Hideaki break; 16127cbca67cSYOSHIFUJI Hideaki } 1613a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_ORCHID: 1614a9b05723SYOSHIFUJI Hideaki /* Rule 8-: Prefer ORCHID vs ORCHID or 1615a9b05723SYOSHIFUJI Hideaki * non-ORCHID vs non-ORCHID 1616a9b05723SYOSHIFUJI Hideaki */ 1617a9b05723SYOSHIFUJI Hideaki ret = !(ipv6_addr_orchid(&score->ifa->addr) ^ 1618a9b05723SYOSHIFUJI Hideaki ipv6_addr_orchid(dst->addr)); 1619a9b05723SYOSHIFUJI Hideaki break; 1620a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFIX: 1621a9b05723SYOSHIFUJI Hideaki /* Rule 8: Use longest matching prefix */ 162291b4b04fSYOSHIFUJI Hideaki / 吉藤英明 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr); 162391b4b04fSYOSHIFUJI Hideaki / 吉藤英明 if (ret > score->ifa->prefix_len) 162491b4b04fSYOSHIFUJI Hideaki / 吉藤英明 ret = score->ifa->prefix_len; 162591b4b04fSYOSHIFUJI Hideaki / 吉藤英明 score->matchlen = ret; 1626a9b05723SYOSHIFUJI Hideaki break; 16277fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 16287fd2561eSErik Kline case IPV6_SADDR_RULE_NOT_OPTIMISTIC: 16297fd2561eSErik Kline /* Optimistic addresses still have lower precedence than other 16307fd2561eSErik Kline * preferred addresses. 16317fd2561eSErik Kline */ 16327fd2561eSErik Kline ret = !(score->ifa->flags & IFA_F_OPTIMISTIC); 16337fd2561eSErik Kline break; 16347fd2561eSErik Kline #endif 1635a9b05723SYOSHIFUJI Hideaki default: 1636a9b05723SYOSHIFUJI Hideaki ret = 0; 1637a9b05723SYOSHIFUJI Hideaki } 1638a9b05723SYOSHIFUJI Hideaki 1639a9b05723SYOSHIFUJI Hideaki if (ret) 1640a9b05723SYOSHIFUJI Hideaki __set_bit(i, score->scorebits); 1641a9b05723SYOSHIFUJI Hideaki score->rule = i; 1642a9b05723SYOSHIFUJI Hideaki out: 1643a9b05723SYOSHIFUJI Hideaki return ret; 1644a9b05723SYOSHIFUJI Hideaki } 1645a9b05723SYOSHIFUJI Hideaki 1646c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 static int __ipv6_dev_get_saddr(struct net *net, 16479131f3deSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_dst *dst, 16489131f3deSYOSHIFUJI Hideaki/吉藤英明 struct inet6_dev *idev, 1649c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_score *scores, 1650c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 int hiscore_idx) 16511da177e4SLinus Torvalds { 1652c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx]; 1653072047e4SYOSHIFUJI Hideaki 1654f59c031eSEric Dumazet list_for_each_entry_rcu(score->ifa, &idev->addr_list, if_list) { 1655a9b05723SYOSHIFUJI Hideaki int i; 1656072047e4SYOSHIFUJI Hideaki 1657a9b05723SYOSHIFUJI Hideaki /* 16586b3ae80aSYOSHIFUJI Hideaki * - Tentative Address (RFC2462 section 5.4) 16596b3ae80aSYOSHIFUJI Hideaki * - A tentative address is not considered 16606b3ae80aSYOSHIFUJI Hideaki * "assigned to an interface" in the traditional 166195c385b4SNeil Horman * sense, unless it is also flagged as optimistic. 16626b3ae80aSYOSHIFUJI Hideaki * - Candidate Source Address (section 4) 1663072047e4SYOSHIFUJI Hideaki * - In any case, anycast addresses, multicast 1664072047e4SYOSHIFUJI Hideaki * addresses, and the unspecified address MUST 1665072047e4SYOSHIFUJI Hideaki * NOT be included in a candidate set. 1666072047e4SYOSHIFUJI Hideaki */ 1667a9b05723SYOSHIFUJI Hideaki if ((score->ifa->flags & IFA_F_TENTATIVE) && 1668a9b05723SYOSHIFUJI Hideaki (!(score->ifa->flags & IFA_F_OPTIMISTIC))) 16696b3ae80aSYOSHIFUJI Hideaki continue; 1670a9b05723SYOSHIFUJI Hideaki 1671a9b05723SYOSHIFUJI Hideaki score->addr_type = __ipv6_addr_type(&score->ifa->addr); 1672a9b05723SYOSHIFUJI Hideaki 1673a9b05723SYOSHIFUJI Hideaki if (unlikely(score->addr_type == IPV6_ADDR_ANY || 1674a9b05723SYOSHIFUJI Hideaki score->addr_type & IPV6_ADDR_MULTICAST)) { 1675ba7a46f1SJoe Perches net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s", 16769131f3deSYOSHIFUJI Hideaki/吉藤英明 idev->dev->name); 16771da177e4SLinus Torvalds continue; 1678072047e4SYOSHIFUJI Hideaki } 1679072047e4SYOSHIFUJI Hideaki 1680a9b05723SYOSHIFUJI Hideaki score->rule = -1; 1681a9b05723SYOSHIFUJI Hideaki bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX); 1682072047e4SYOSHIFUJI Hideaki 1683a9b05723SYOSHIFUJI Hideaki for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) { 1684a9b05723SYOSHIFUJI Hideaki int minihiscore, miniscore; 1685072047e4SYOSHIFUJI Hideaki 16869131f3deSYOSHIFUJI Hideaki/吉藤英明 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i); 16879131f3deSYOSHIFUJI Hideaki/吉藤英明 miniscore = ipv6_get_saddr_eval(net, score, dst, i); 1688072047e4SYOSHIFUJI Hideaki 1689a9b05723SYOSHIFUJI Hideaki if (minihiscore > miniscore) { 1690a9b05723SYOSHIFUJI Hideaki if (i == IPV6_SADDR_RULE_SCOPE && 1691a9b05723SYOSHIFUJI Hideaki score->scopedist > 0) { 1692a9b05723SYOSHIFUJI Hideaki /* 1693a9b05723SYOSHIFUJI Hideaki * special case: 1694a9b05723SYOSHIFUJI Hideaki * each remaining entry 1695a9b05723SYOSHIFUJI Hideaki * has too small (not enough) 1696a9b05723SYOSHIFUJI Hideaki * scope, because ifa entries 1697a9b05723SYOSHIFUJI Hideaki * are sorted by their scope 1698a9b05723SYOSHIFUJI Hideaki * values. 1699072047e4SYOSHIFUJI Hideaki */ 17009131f3deSYOSHIFUJI Hideaki/吉藤英明 goto out; 1701072047e4SYOSHIFUJI Hideaki } 1702a9b05723SYOSHIFUJI Hideaki break; 1703a9b05723SYOSHIFUJI Hideaki } else if (minihiscore < miniscore) { 1704a0bffffcSIlpo Järvinen swap(hiscore, score); 1705c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 hiscore_idx = 1 - hiscore_idx; 1706a9b05723SYOSHIFUJI Hideaki 1707a9b05723SYOSHIFUJI Hideaki /* restore our iterator */ 1708a9b05723SYOSHIFUJI Hideaki score->ifa = hiscore->ifa; 1709a9b05723SYOSHIFUJI Hideaki 1710a9b05723SYOSHIFUJI Hideaki break; 17111da177e4SLinus Torvalds } 1712a9b05723SYOSHIFUJI Hideaki } 1713a9b05723SYOSHIFUJI Hideaki } 17149131f3deSYOSHIFUJI Hideaki/吉藤英明 out: 1715c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 return hiscore_idx; 17161da177e4SLinus Torvalds } 17179131f3deSYOSHIFUJI Hideaki/吉藤英明 1718afbac601SDavid Ahern static int ipv6_get_saddr_master(struct net *net, 1719afbac601SDavid Ahern const struct net_device *dst_dev, 1720afbac601SDavid Ahern const struct net_device *master, 1721afbac601SDavid Ahern struct ipv6_saddr_dst *dst, 1722afbac601SDavid Ahern struct ipv6_saddr_score *scores, 1723afbac601SDavid Ahern int hiscore_idx) 1724afbac601SDavid Ahern { 1725afbac601SDavid Ahern struct inet6_dev *idev; 1726afbac601SDavid Ahern 1727afbac601SDavid Ahern idev = __in6_dev_get(dst_dev); 1728afbac601SDavid Ahern if (idev) 1729afbac601SDavid Ahern hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev, 1730afbac601SDavid Ahern scores, hiscore_idx); 1731afbac601SDavid Ahern 1732afbac601SDavid Ahern idev = __in6_dev_get(master); 1733afbac601SDavid Ahern if (idev) 1734afbac601SDavid Ahern hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev, 1735afbac601SDavid Ahern scores, hiscore_idx); 1736afbac601SDavid Ahern 1737afbac601SDavid Ahern return hiscore_idx; 1738afbac601SDavid Ahern } 1739afbac601SDavid Ahern 17409131f3deSYOSHIFUJI Hideaki/吉藤英明 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev, 17419131f3deSYOSHIFUJI Hideaki/吉藤英明 const struct in6_addr *daddr, unsigned int prefs, 17429131f3deSYOSHIFUJI Hideaki/吉藤英明 struct in6_addr *saddr) 17439131f3deSYOSHIFUJI Hideaki/吉藤英明 { 1744c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_score scores[2], *hiscore; 17459131f3deSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_dst dst; 17469131f3deSYOSHIFUJI Hideaki/吉藤英明 struct inet6_dev *idev; 17479131f3deSYOSHIFUJI Hideaki/吉藤英明 struct net_device *dev; 17489131f3deSYOSHIFUJI Hideaki/吉藤英明 int dst_type; 17499131f3deSYOSHIFUJI Hideaki/吉藤英明 bool use_oif_addr = false; 1750c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 int hiscore_idx = 0; 1751cc429c8fSEric Dumazet int ret = 0; 17529131f3deSYOSHIFUJI Hideaki/吉藤英明 17539131f3deSYOSHIFUJI Hideaki/吉藤英明 dst_type = __ipv6_addr_type(daddr); 17549131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.addr = daddr; 17559131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.ifindex = dst_dev ? dst_dev->ifindex : 0; 17569131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.scope = __ipv6_addr_src_scope(dst_type); 17579131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex); 17589131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.prefs = prefs; 17599131f3deSYOSHIFUJI Hideaki/吉藤英明 1760c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 scores[hiscore_idx].rule = -1; 1761c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 scores[hiscore_idx].ifa = NULL; 17629131f3deSYOSHIFUJI Hideaki/吉藤英明 17639131f3deSYOSHIFUJI Hideaki/吉藤英明 rcu_read_lock(); 17649131f3deSYOSHIFUJI Hideaki/吉藤英明 17659131f3deSYOSHIFUJI Hideaki/吉藤英明 /* Candidate Source Address (section 4) 17669131f3deSYOSHIFUJI Hideaki/吉藤英明 * - multicast and link-local destination address, 17679131f3deSYOSHIFUJI Hideaki/吉藤英明 * the set of candidate source address MUST only 17689131f3deSYOSHIFUJI Hideaki/吉藤英明 * include addresses assigned to interfaces 17699131f3deSYOSHIFUJI Hideaki/吉藤英明 * belonging to the same link as the outgoing 17709131f3deSYOSHIFUJI Hideaki/吉藤英明 * interface. 17719131f3deSYOSHIFUJI Hideaki/吉藤英明 * (- For site-local destination addresses, the 17729131f3deSYOSHIFUJI Hideaki/吉藤英明 * set of candidate source addresses MUST only 17739131f3deSYOSHIFUJI Hideaki/吉藤英明 * include addresses assigned to interfaces 17749131f3deSYOSHIFUJI Hideaki/吉藤英明 * belonging to the same site as the outgoing 17759131f3deSYOSHIFUJI Hideaki/吉藤英明 * interface.) 17763985e8a3SErik Kline * - "It is RECOMMENDED that the candidate source addresses 17773985e8a3SErik Kline * be the set of unicast addresses assigned to the 17783985e8a3SErik Kline * interface that will be used to send to the destination 17793985e8a3SErik Kline * (the 'outgoing' interface)." (RFC 6724) 17809131f3deSYOSHIFUJI Hideaki/吉藤英明 */ 17819131f3deSYOSHIFUJI Hideaki/吉藤英明 if (dst_dev) { 17829131f3deSYOSHIFUJI Hideaki/吉藤英明 idev = __in6_dev_get(dst_dev); 17833985e8a3SErik Kline if ((dst_type & IPV6_ADDR_MULTICAST) || 17843985e8a3SErik Kline dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL || 17853985e8a3SErik Kline (idev && idev->cnf.use_oif_addrs_only)) { 17869131f3deSYOSHIFUJI Hideaki/吉藤英明 use_oif_addr = true; 17879131f3deSYOSHIFUJI Hideaki/吉藤英明 } 17889131f3deSYOSHIFUJI Hideaki/吉藤英明 } 17899131f3deSYOSHIFUJI Hideaki/吉藤英明 17909131f3deSYOSHIFUJI Hideaki/吉藤英明 if (use_oif_addr) { 1791c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 if (idev) 1792c15df306SYOSHIFUJI Hideaki hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx); 17939131f3deSYOSHIFUJI Hideaki/吉藤英明 } else { 1794afbac601SDavid Ahern const struct net_device *master; 1795afbac601SDavid Ahern int master_idx = 0; 1796afbac601SDavid Ahern 1797afbac601SDavid Ahern /* if dst_dev exists and is enslaved to an L3 device, then 1798afbac601SDavid Ahern * prefer addresses from dst_dev and then the master over 1799afbac601SDavid Ahern * any other enslaved devices in the L3 domain. 1800afbac601SDavid Ahern */ 1801afbac601SDavid Ahern master = l3mdev_master_dev_rcu(dst_dev); 1802afbac601SDavid Ahern if (master) { 1803afbac601SDavid Ahern master_idx = master->ifindex; 1804afbac601SDavid Ahern 1805afbac601SDavid Ahern hiscore_idx = ipv6_get_saddr_master(net, dst_dev, 1806afbac601SDavid Ahern master, &dst, 1807afbac601SDavid Ahern scores, hiscore_idx); 1808afbac601SDavid Ahern 1809afbac601SDavid Ahern if (scores[hiscore_idx].ifa) 1810afbac601SDavid Ahern goto out; 1811afbac601SDavid Ahern } 1812afbac601SDavid Ahern 18139131f3deSYOSHIFUJI Hideaki/吉藤英明 for_each_netdev_rcu(net, dev) { 1814afbac601SDavid Ahern /* only consider addresses on devices in the 1815afbac601SDavid Ahern * same L3 domain 1816afbac601SDavid Ahern */ 1817afbac601SDavid Ahern if (l3mdev_master_ifindex_rcu(dev) != master_idx) 1818afbac601SDavid Ahern continue; 18199131f3deSYOSHIFUJI Hideaki/吉藤英明 idev = __in6_dev_get(dev); 18209131f3deSYOSHIFUJI Hideaki/吉藤英明 if (!idev) 18219131f3deSYOSHIFUJI Hideaki/吉藤英明 continue; 1822c15df306SYOSHIFUJI Hideaki hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx); 18239131f3deSYOSHIFUJI Hideaki/吉藤英明 } 18249131f3deSYOSHIFUJI Hideaki/吉藤英明 } 1825afbac601SDavid Ahern 1826afbac601SDavid Ahern out: 1827c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 hiscore = &scores[hiscore_idx]; 1828a9b05723SYOSHIFUJI Hideaki if (!hiscore->ifa) 1829cc429c8fSEric Dumazet ret = -EADDRNOTAVAIL; 1830cc429c8fSEric Dumazet else 18314e3fd7a0SAlexey Dobriyan *saddr = hiscore->ifa->addr; 1832cc429c8fSEric Dumazet 1833cc429c8fSEric Dumazet rcu_read_unlock(); 1834cc429c8fSEric Dumazet return ret; 18351da177e4SLinus Torvalds } 18365e5f3f0fSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_dev_get_saddr); 18371da177e4SLinus Torvalds 18388965779dSAmerigo Wang int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr, 1839479840ffSJiri Pirko u32 banned_flags) 1840b7b1bfceSHannes Frederic Sowa { 1841b7b1bfceSHannes Frederic Sowa struct inet6_ifaddr *ifp; 1842b7b1bfceSHannes Frederic Sowa int err = -EADDRNOTAVAIL; 1843b7b1bfceSHannes Frederic Sowa 1844602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) { 1845602582caSHannes Frederic Sowa if (ifp->scope > IFA_LINK) 1846602582caSHannes Frederic Sowa break; 1847b7b1bfceSHannes Frederic Sowa if (ifp->scope == IFA_LINK && 1848b7b1bfceSHannes Frederic Sowa !(ifp->flags & banned_flags)) { 1849b7b1bfceSHannes Frederic Sowa *addr = ifp->addr; 1850b7b1bfceSHannes Frederic Sowa err = 0; 1851b7b1bfceSHannes Frederic Sowa break; 1852b7b1bfceSHannes Frederic Sowa } 1853b7b1bfceSHannes Frederic Sowa } 1854b7b1bfceSHannes Frederic Sowa return err; 1855b7b1bfceSHannes Frederic Sowa } 1856b7b1bfceSHannes Frederic Sowa 185795c385b4SNeil Horman int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr, 1858479840ffSJiri Pirko u32 banned_flags) 18591da177e4SLinus Torvalds { 18601da177e4SLinus Torvalds struct inet6_dev *idev; 18611da177e4SLinus Torvalds int err = -EADDRNOTAVAIL; 18621da177e4SLinus Torvalds 18638814c4b5SYOSHIFUJI Hideaki rcu_read_lock(); 1864e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 1865e21e8467SStephen Hemminger if (idev) { 18661da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1867b7b1bfceSHannes Frederic Sowa err = __ipv6_get_lladdr(idev, addr, banned_flags); 18681da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 18691da177e4SLinus Torvalds } 18708814c4b5SYOSHIFUJI Hideaki rcu_read_unlock(); 18711da177e4SLinus Torvalds return err; 18721da177e4SLinus Torvalds } 18731da177e4SLinus Torvalds 1874d9bf82c2SEric Dumazet static int ipv6_count_addresses(const struct inet6_dev *idev) 18751da177e4SLinus Torvalds { 1876d9bf82c2SEric Dumazet const struct inet6_ifaddr *ifp; 18771da177e4SLinus Torvalds int cnt = 0; 18781da177e4SLinus Torvalds 1879d9bf82c2SEric Dumazet rcu_read_lock(); 1880d9bf82c2SEric Dumazet list_for_each_entry_rcu(ifp, &idev->addr_list, if_list) 18811da177e4SLinus Torvalds cnt++; 1882d9bf82c2SEric Dumazet rcu_read_unlock(); 18831da177e4SLinus Torvalds return cnt; 18841da177e4SLinus Torvalds } 18851da177e4SLinus Torvalds 1886b71d1d42SEric Dumazet int ipv6_chk_addr(struct net *net, const struct in6_addr *addr, 18872a7851bfSFlorian Westphal const struct net_device *dev, int strict) 18881da177e4SLinus Torvalds { 1889232378e8SDavid Ahern return ipv6_chk_addr_and_flags(net, addr, dev, !dev, 1890232378e8SDavid Ahern strict, IFA_F_TENTATIVE); 1891c58da4c6SErik Kline } 1892c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr); 1893c58da4c6SErik Kline 18941893ff20SDavid Ahern /* device argument is used to find the L3 domain of interest. If 18951893ff20SDavid Ahern * skip_dev_check is set, then the ifp device is not checked against 18961893ff20SDavid Ahern * the passed in dev argument. So the 2 cases for addresses checks are: 18971893ff20SDavid Ahern * 1. does the address exist in the L3 domain that dev is part of 18981893ff20SDavid Ahern * (skip_dev_check = true), or 18991893ff20SDavid Ahern * 19001893ff20SDavid Ahern * 2. does the address exist on the specific device 19011893ff20SDavid Ahern * (skip_dev_check = false) 19021893ff20SDavid Ahern */ 1903c58da4c6SErik Kline int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr, 1904232378e8SDavid Ahern const struct net_device *dev, bool skip_dev_check, 1905232378e8SDavid Ahern int strict, u32 banned_flags) 1906c58da4c6SErik Kline { 19073f27fb23SEric Dumazet unsigned int hash = inet6_addr_hash(net, addr); 19081893ff20SDavid Ahern const struct net_device *l3mdev; 1909eedf042aSStephen Hemminger struct inet6_ifaddr *ifp; 1910c58da4c6SErik Kline u32 ifp_flags; 19111da177e4SLinus Torvalds 1912480318a0SEric Dumazet rcu_read_lock(); 1913232378e8SDavid Ahern 19141893ff20SDavid Ahern l3mdev = l3mdev_master_dev_rcu(dev); 1915232378e8SDavid Ahern if (skip_dev_check) 1916232378e8SDavid Ahern dev = NULL; 1917232378e8SDavid Ahern 1918b67bfe0dSSasha Levin hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) { 1919878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 1920bfeade08SDaniel Lezcano continue; 19211893ff20SDavid Ahern 19221893ff20SDavid Ahern if (l3mdev_master_dev_rcu(ifp->idev->dev) != l3mdev) 19231893ff20SDavid Ahern continue; 19241893ff20SDavid Ahern 1925c58da4c6SErik Kline /* Decouple optimistic from tentative for evaluation here. 1926c58da4c6SErik Kline * Ban optimistic addresses explicitly, when required. 1927c58da4c6SErik Kline */ 1928c58da4c6SErik Kline ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC) 1929c58da4c6SErik Kline ? (ifp->flags&~IFA_F_TENTATIVE) 1930c58da4c6SErik Kline : ifp->flags; 19311da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr) && 1932c58da4c6SErik Kline !(ifp_flags&banned_flags) && 193363159f29SIan Morris (!dev || ifp->idev->dev == dev || 1934eedf042aSStephen Hemminger !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) { 1935480318a0SEric Dumazet rcu_read_unlock(); 1936eedf042aSStephen Hemminger return 1; 1937eedf042aSStephen Hemminger } 1938eedf042aSStephen Hemminger } 19395c578aedSstephen hemminger 1940480318a0SEric Dumazet rcu_read_unlock(); 1941eedf042aSStephen Hemminger return 0; 19421da177e4SLinus Torvalds } 1943c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr_and_flags); 19447159039aSYOSHIFUJI Hideaki 19451da177e4SLinus Torvalds 19467df37ff3SCatalin\(ux\) M. BOIE /* Compares an address/prefix_len with addresses on device @dev. 19477df37ff3SCatalin\(ux\) M. BOIE * If one is found it returns true. 19487df37ff3SCatalin\(ux\) M. BOIE */ 19497df37ff3SCatalin\(ux\) M. BOIE bool ipv6_chk_custom_prefix(const struct in6_addr *addr, 19507df37ff3SCatalin\(ux\) M. BOIE const unsigned int prefix_len, struct net_device *dev) 19517df37ff3SCatalin\(ux\) M. BOIE { 195247e26941SEric Dumazet const struct inet6_ifaddr *ifa; 195347e26941SEric Dumazet const struct inet6_dev *idev; 19547df37ff3SCatalin\(ux\) M. BOIE bool ret = false; 19557df37ff3SCatalin\(ux\) M. BOIE 19567df37ff3SCatalin\(ux\) M. BOIE rcu_read_lock(); 19577df37ff3SCatalin\(ux\) M. BOIE idev = __in6_dev_get(dev); 19587df37ff3SCatalin\(ux\) M. BOIE if (idev) { 195947e26941SEric Dumazet list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) { 19607df37ff3SCatalin\(ux\) M. BOIE ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len); 19617df37ff3SCatalin\(ux\) M. BOIE if (ret) 19627df37ff3SCatalin\(ux\) M. BOIE break; 19637df37ff3SCatalin\(ux\) M. BOIE } 19647df37ff3SCatalin\(ux\) M. BOIE } 19657df37ff3SCatalin\(ux\) M. BOIE rcu_read_unlock(); 19667df37ff3SCatalin\(ux\) M. BOIE 19677df37ff3SCatalin\(ux\) M. BOIE return ret; 19687df37ff3SCatalin\(ux\) M. BOIE } 19697df37ff3SCatalin\(ux\) M. BOIE EXPORT_SYMBOL(ipv6_chk_custom_prefix); 19707df37ff3SCatalin\(ux\) M. BOIE 1971b71d1d42SEric Dumazet int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev) 197252eeeb84SYOSHIFUJI Hideaki { 197324ba333bSEric Dumazet const struct inet6_ifaddr *ifa; 197424ba333bSEric Dumazet const struct inet6_dev *idev; 197552eeeb84SYOSHIFUJI Hideaki int onlink; 197652eeeb84SYOSHIFUJI Hideaki 197752eeeb84SYOSHIFUJI Hideaki onlink = 0; 197852eeeb84SYOSHIFUJI Hideaki rcu_read_lock(); 197952eeeb84SYOSHIFUJI Hideaki idev = __in6_dev_get(dev); 198052eeeb84SYOSHIFUJI Hideaki if (idev) { 198124ba333bSEric Dumazet list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) { 198252eeeb84SYOSHIFUJI Hideaki onlink = ipv6_prefix_equal(addr, &ifa->addr, 198352eeeb84SYOSHIFUJI Hideaki ifa->prefix_len); 198452eeeb84SYOSHIFUJI Hideaki if (onlink) 198552eeeb84SYOSHIFUJI Hideaki break; 198652eeeb84SYOSHIFUJI Hideaki } 198752eeeb84SYOSHIFUJI Hideaki } 198852eeeb84SYOSHIFUJI Hideaki rcu_read_unlock(); 198952eeeb84SYOSHIFUJI Hideaki return onlink; 199052eeeb84SYOSHIFUJI Hideaki } 199152eeeb84SYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_prefix); 199252eeeb84SYOSHIFUJI Hideaki 19939acd9f3aSYOSHIFUJI Hideaki struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr, 19941cab3da6SDaniel Lezcano struct net_device *dev, int strict) 19951da177e4SLinus Torvalds { 19963f27fb23SEric Dumazet unsigned int hash = inet6_addr_hash(net, addr); 1997b79d1d54SDavid S. Miller struct inet6_ifaddr *ifp, *result = NULL; 19981da177e4SLinus Torvalds 199924f226daSEric Dumazet rcu_read_lock(); 200024f226daSEric Dumazet hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) { 2001878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 20021cab3da6SDaniel Lezcano continue; 20031da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr)) { 200463159f29SIan Morris if (!dev || ifp->idev->dev == dev || 20051da177e4SLinus Torvalds !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) { 2006b79d1d54SDavid S. Miller result = ifp; 20071da177e4SLinus Torvalds in6_ifa_hold(ifp); 20081da177e4SLinus Torvalds break; 20091da177e4SLinus Torvalds } 20101da177e4SLinus Torvalds } 20111da177e4SLinus Torvalds } 201224f226daSEric Dumazet rcu_read_unlock(); 20131da177e4SLinus Torvalds 2014b79d1d54SDavid S. Miller return result; 20151da177e4SLinus Torvalds } 20161da177e4SLinus Torvalds 20171da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */ 20181da177e4SLinus Torvalds 2019cc411d0bSBrian Haley static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed) 20201da177e4SLinus Torvalds { 20213d171f39SLubomir Rintel if (dad_failed) 20223d171f39SLubomir Rintel ifp->flags |= IFA_F_DADFAILED; 20233d171f39SLubomir Rintel 2024ec8add2aSSabrina Dubroca if (ifp->flags&IFA_F_TEMPORARY) { 20251da177e4SLinus Torvalds struct inet6_ifaddr *ifpub; 20261da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 20271da177e4SLinus Torvalds ifpub = ifp->ifpub; 20281da177e4SLinus Torvalds if (ifpub) { 20291da177e4SLinus Torvalds in6_ifa_hold(ifpub); 20301da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 2031fffcefe9SEric Dumazet ipv6_create_tempaddr(ifpub, ifp, true); 20321da177e4SLinus Torvalds in6_ifa_put(ifpub); 20331da177e4SLinus Torvalds } else { 20341da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 20351da177e4SLinus Torvalds } 20361da177e4SLinus Torvalds ipv6_del_addr(ifp); 2037ec8add2aSSabrina Dubroca } else if (ifp->flags&IFA_F_PERMANENT || !dad_failed) { 2038ec8add2aSSabrina Dubroca spin_lock_bh(&ifp->lock); 2039ec8add2aSSabrina Dubroca addrconf_del_dad_work(ifp); 2040ec8add2aSSabrina Dubroca ifp->flags |= IFA_F_TENTATIVE; 2041f1c02cfbSSabrina Dubroca if (dad_failed) 2042f1c02cfbSSabrina Dubroca ifp->flags &= ~IFA_F_OPTIMISTIC; 2043ec8add2aSSabrina Dubroca spin_unlock_bh(&ifp->lock); 2044ec8add2aSSabrina Dubroca if (dad_failed) 2045ec8add2aSSabrina Dubroca ipv6_ifa_notify(0, ifp); 2046ec8add2aSSabrina Dubroca in6_ifa_put(ifp); 2047c15b1ccaSHannes Frederic Sowa } else { 20481da177e4SLinus Torvalds ipv6_del_addr(ifp); 20491da177e4SLinus Torvalds } 2050c15b1ccaSHannes Frederic Sowa } 20511da177e4SLinus Torvalds 2052f2344a13SHerbert Xu static int addrconf_dad_end(struct inet6_ifaddr *ifp) 2053f2344a13SHerbert Xu { 2054f2344a13SHerbert Xu int err = -ENOENT; 2055f2344a13SHerbert Xu 20568e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 2057f2344a13SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DAD) { 2058f2344a13SHerbert Xu ifp->state = INET6_IFADDR_STATE_POSTDAD; 2059f2344a13SHerbert Xu err = 0; 2060f2344a13SHerbert Xu } 20618e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 2062f2344a13SHerbert Xu 2063f2344a13SHerbert Xu return err; 2064f2344a13SHerbert Xu } 2065f2344a13SHerbert Xu 2066da13c59bSVishwanath Pai void addrconf_dad_failure(struct sk_buff *skb, struct inet6_ifaddr *ifp) 20673c21edbdSYOSHIFUJI Hideaki { 20681b34be74SYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev; 20691855b7c3SHannes Frederic Sowa struct net *net = dev_net(ifp->idev->dev); 20709bdd8d40SBrian Haley 2071853dc2e0SUrsula Braun if (addrconf_dad_end(ifp)) { 2072853dc2e0SUrsula Braun in6_ifa_put(ifp); 2073f2344a13SHerbert Xu return; 2074853dc2e0SUrsula Braun } 2075f2344a13SHerbert Xu 2076da13c59bSVishwanath Pai net_info_ratelimited("%s: IPv6 duplicate address %pI6c used by %pM detected!\n", 2077da13c59bSVishwanath Pai ifp->idev->dev->name, &ifp->addr, eth_hdr(skb)->h_source); 20789bdd8d40SBrian Haley 20795f40ef77SHannes Frederic Sowa spin_lock_bh(&ifp->lock); 20801b34be74SYOSHIFUJI Hideaki 20815f40ef77SHannes Frederic Sowa if (ifp->flags & IFA_F_STABLE_PRIVACY) { 20825f40ef77SHannes Frederic Sowa struct in6_addr new_addr; 20835f40ef77SHannes Frederic Sowa struct inet6_ifaddr *ifp2; 20845f40ef77SHannes Frederic Sowa int retries = ifp->stable_privacy_retry + 1; 2085e6464b8cSDavid Ahern struct ifa6_config cfg = { 2086e6464b8cSDavid Ahern .pfx = &new_addr, 2087e6464b8cSDavid Ahern .plen = ifp->prefix_len, 2088e6464b8cSDavid Ahern .ifa_flags = ifp->flags, 2089e6464b8cSDavid Ahern .valid_lft = ifp->valid_lft, 2090e6464b8cSDavid Ahern .preferred_lft = ifp->prefered_lft, 2091e6464b8cSDavid Ahern .scope = ifp->scope, 2092e6464b8cSDavid Ahern }; 20935f40ef77SHannes Frederic Sowa 20941855b7c3SHannes Frederic Sowa if (retries > net->ipv6.sysctl.idgen_retries) { 20955f40ef77SHannes Frederic Sowa net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n", 20965f40ef77SHannes Frederic Sowa ifp->idev->dev->name); 20975f40ef77SHannes Frederic Sowa goto errdad; 20985f40ef77SHannes Frederic Sowa } 20995f40ef77SHannes Frederic Sowa 21005f40ef77SHannes Frederic Sowa new_addr = ifp->addr; 21015f40ef77SHannes Frederic Sowa if (ipv6_generate_stable_address(&new_addr, retries, 21025f40ef77SHannes Frederic Sowa idev)) 21035f40ef77SHannes Frederic Sowa goto errdad; 21045f40ef77SHannes Frederic Sowa 21055f40ef77SHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 21065f40ef77SHannes Frederic Sowa 21075f40ef77SHannes Frederic Sowa if (idev->cnf.max_addresses && 21085f40ef77SHannes Frederic Sowa ipv6_count_addresses(idev) >= 21095f40ef77SHannes Frederic Sowa idev->cnf.max_addresses) 21105f40ef77SHannes Frederic Sowa goto lock_errdad; 21115f40ef77SHannes Frederic Sowa 21125f40ef77SHannes Frederic Sowa net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n", 21135f40ef77SHannes Frederic Sowa ifp->idev->dev->name); 21145f40ef77SHannes Frederic Sowa 2115e6464b8cSDavid Ahern ifp2 = ipv6_add_addr(idev, &cfg, false, NULL); 21165f40ef77SHannes Frederic Sowa if (IS_ERR(ifp2)) 21175f40ef77SHannes Frederic Sowa goto lock_errdad; 21185f40ef77SHannes Frederic Sowa 21195f40ef77SHannes Frederic Sowa spin_lock_bh(&ifp2->lock); 21205f40ef77SHannes Frederic Sowa ifp2->stable_privacy_retry = retries; 21215f40ef77SHannes Frederic Sowa ifp2->state = INET6_IFADDR_STATE_PREDAD; 21225f40ef77SHannes Frederic Sowa spin_unlock_bh(&ifp2->lock); 21235f40ef77SHannes Frederic Sowa 21241855b7c3SHannes Frederic Sowa addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay); 21255f40ef77SHannes Frederic Sowa in6_ifa_put(ifp2); 21265f40ef77SHannes Frederic Sowa lock_errdad: 21275f40ef77SHannes Frederic Sowa spin_lock_bh(&ifp->lock); 21281b34be74SYOSHIFUJI Hideaki } 21291b34be74SYOSHIFUJI Hideaki 21305f40ef77SHannes Frederic Sowa errdad: 2131c15b1ccaSHannes Frederic Sowa /* transition from _POSTDAD to _ERRDAD */ 2132c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_ERRDAD; 21338e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 2134c15b1ccaSHannes Frederic Sowa 2135c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 0); 2136751eb6b6SWei Yongjun in6_ifa_put(ifp); 21373c21edbdSYOSHIFUJI Hideaki } 21381da177e4SLinus Torvalds 2139a9ed4a29SSabrina Dubroca /* Join to solicited addr multicast group. 2140a9ed4a29SSabrina Dubroca * caller must hold RTNL */ 2141b71d1d42SEric Dumazet void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr) 21421da177e4SLinus Torvalds { 21431da177e4SLinus Torvalds struct in6_addr maddr; 21441da177e4SLinus Torvalds 21451da177e4SLinus Torvalds if (dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 21461da177e4SLinus Torvalds return; 21471da177e4SLinus Torvalds 21481da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr); 21491da177e4SLinus Torvalds ipv6_dev_mc_inc(dev, &maddr); 21501da177e4SLinus Torvalds } 21511da177e4SLinus Torvalds 2152a9ed4a29SSabrina Dubroca /* caller must hold RTNL */ 2153b71d1d42SEric Dumazet void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr) 21541da177e4SLinus Torvalds { 21551da177e4SLinus Torvalds struct in6_addr maddr; 21561da177e4SLinus Torvalds 21571da177e4SLinus Torvalds if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 21581da177e4SLinus Torvalds return; 21591da177e4SLinus Torvalds 21601da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr); 21611da177e4SLinus Torvalds __ipv6_dev_mc_dec(idev, &maddr); 21621da177e4SLinus Torvalds } 21631da177e4SLinus Torvalds 2164a9ed4a29SSabrina Dubroca /* caller must hold RTNL */ 216520380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp) 21661da177e4SLinus Torvalds { 21671da177e4SLinus Torvalds struct in6_addr addr; 2168c15b1ccaSHannes Frederic Sowa 216988ad3149SHannes Frederic Sowa if (ifp->prefix_len >= 127) /* RFC 6164 */ 21702bda8a0cSBjørn Mork return; 21711da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 21721da177e4SLinus Torvalds if (ipv6_addr_any(&addr)) 21731da177e4SLinus Torvalds return; 2174013b4d90SWANG Cong __ipv6_dev_ac_inc(ifp->idev, &addr); 21751da177e4SLinus Torvalds } 21761da177e4SLinus Torvalds 2177a9ed4a29SSabrina Dubroca /* caller must hold RTNL */ 217820380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp) 21791da177e4SLinus Torvalds { 21801da177e4SLinus Torvalds struct in6_addr addr; 2181c15b1ccaSHannes Frederic Sowa 218288ad3149SHannes Frederic Sowa if (ifp->prefix_len >= 127) /* RFC 6164 */ 218332019e65SYOSHIFUJI Hideaki return; 21841da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 21851da177e4SLinus Torvalds if (ipv6_addr_any(&addr)) 21861da177e4SLinus Torvalds return; 21871da177e4SLinus Torvalds __ipv6_dev_ac_dec(ifp->idev, &addr); 21881da177e4SLinus Torvalds } 21891da177e4SLinus Torvalds 21908a7a4b47SAlexander Aring static int addrconf_ifid_6lowpan(u8 *eui, struct net_device *dev) 219106a4c1c5Salex.bluesman.smirnov@gmail.com { 21928a7a4b47SAlexander Aring switch (dev->addr_len) { 21938a7a4b47SAlexander Aring case ETH_ALEN: 21949dae2e03SLuiz Augusto von Dentz memcpy(eui, dev->dev_addr, 3); 21959dae2e03SLuiz Augusto von Dentz eui[3] = 0xFF; 21969dae2e03SLuiz Augusto von Dentz eui[4] = 0xFE; 21979dae2e03SLuiz Augusto von Dentz memcpy(eui + 5, dev->dev_addr + 3, 3); 21989dae2e03SLuiz Augusto von Dentz break; 21998a7a4b47SAlexander Aring case EUI64_ADDR_LEN: 22005241c2d7SAlexander Aring memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN); 22015e98a36eSYOSHIFUJI Hideaki / 吉藤英明 eui[0] ^= 2; 22028a7a4b47SAlexander Aring break; 22038a7a4b47SAlexander Aring default: 22048a7a4b47SAlexander Aring return -1; 22058a7a4b47SAlexander Aring } 22068a7a4b47SAlexander Aring 220706a4c1c5Salex.bluesman.smirnov@gmail.com return 0; 220806a4c1c5Salex.bluesman.smirnov@gmail.com } 220906a4c1c5Salex.bluesman.smirnov@gmail.com 2210cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev) 2211cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 { 2212cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 union fwnet_hwaddr *ha; 2213cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 2214cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 if (dev->addr_len != FWNET_ALEN) 2215cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return -1; 2216cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 2217cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 ha = (union fwnet_hwaddr *)dev->dev_addr; 2218cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 2219cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id)); 2220cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 eui[0] ^= 2; 2221cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return 0; 2222cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 } 2223cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 2224073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev) 2225073a8e0eSYOSHIFUJI Hideaki { 22261da177e4SLinus Torvalds /* XXX: inherit EUI-64 from other interface -- yoshfuji */ 22271da177e4SLinus Torvalds if (dev->addr_len != ARCNET_ALEN) 22281da177e4SLinus Torvalds return -1; 22291da177e4SLinus Torvalds memset(eui, 0, 7); 22301da177e4SLinus Torvalds eui[7] = *(u8 *)dev->dev_addr; 22311da177e4SLinus Torvalds return 0; 2232073a8e0eSYOSHIFUJI Hideaki } 2233073a8e0eSYOSHIFUJI Hideaki 2234073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev) 2235073a8e0eSYOSHIFUJI Hideaki { 22361da177e4SLinus Torvalds if (dev->addr_len != INFINIBAND_ALEN) 22371da177e4SLinus Torvalds return -1; 22381da177e4SLinus Torvalds memcpy(eui, dev->dev_addr + 12, 8); 22391da177e4SLinus Torvalds eui[0] |= 2; 22401da177e4SLinus Torvalds return 0; 22411da177e4SLinus Torvalds } 2242073a8e0eSYOSHIFUJI Hideaki 2243c61393eaSstephen hemminger static int __ipv6_isatap_ifid(u8 *eui, __be32 addr) 2244dfd982baSYOSHIFUJI Hideaki { 22459af28511SSascha Hlusiak if (addr == 0) 22469af28511SSascha Hlusiak return -1; 2247dfd982baSYOSHIFUJI Hideaki eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) || 2248dfd982baSYOSHIFUJI Hideaki ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) || 2249dfd982baSYOSHIFUJI Hideaki ipv4_is_private_172(addr) || ipv4_is_test_192(addr) || 2250dfd982baSYOSHIFUJI Hideaki ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) || 2251dfd982baSYOSHIFUJI Hideaki ipv4_is_test_198(addr) || ipv4_is_multicast(addr) || 2252dfd982baSYOSHIFUJI Hideaki ipv4_is_lbcast(addr)) ? 0x00 : 0x02; 2253dfd982baSYOSHIFUJI Hideaki eui[1] = 0; 2254dfd982baSYOSHIFUJI Hideaki eui[2] = 0x5E; 2255dfd982baSYOSHIFUJI Hideaki eui[3] = 0xFE; 2256dfd982baSYOSHIFUJI Hideaki memcpy(eui + 4, &addr, 4); 2257dfd982baSYOSHIFUJI Hideaki return 0; 2258dfd982baSYOSHIFUJI Hideaki } 2259dfd982baSYOSHIFUJI Hideaki 2260dfd982baSYOSHIFUJI Hideaki static int addrconf_ifid_sit(u8 *eui, struct net_device *dev) 2261dfd982baSYOSHIFUJI Hideaki { 2262dfd982baSYOSHIFUJI Hideaki if (dev->priv_flags & IFF_ISATAP) 2263dfd982baSYOSHIFUJI Hideaki return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); 2264dfd982baSYOSHIFUJI Hideaki return -1; 2265dfd982baSYOSHIFUJI Hideaki } 2266dfd982baSYOSHIFUJI Hideaki 2267aee80b54Sstephen hemminger static int addrconf_ifid_gre(u8 *eui, struct net_device *dev) 2268aee80b54Sstephen hemminger { 2269aee80b54Sstephen hemminger return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); 2270aee80b54Sstephen hemminger } 2271aee80b54Sstephen hemminger 2272e837735eSNicolas Dichtel static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev) 2273e837735eSNicolas Dichtel { 2274e837735eSNicolas Dichtel memcpy(eui, dev->perm_addr, 3); 2275e837735eSNicolas Dichtel memcpy(eui + 5, dev->perm_addr + 3, 3); 2276e837735eSNicolas Dichtel eui[3] = 0xFF; 2277e837735eSNicolas Dichtel eui[4] = 0xFE; 2278e837735eSNicolas Dichtel eui[0] ^= 2; 2279e837735eSNicolas Dichtel return 0; 2280e837735eSNicolas Dichtel } 2281e837735eSNicolas Dichtel 2282073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev) 2283073a8e0eSYOSHIFUJI Hideaki { 2284073a8e0eSYOSHIFUJI Hideaki switch (dev->type) { 2285073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ETHER: 2286073a8e0eSYOSHIFUJI Hideaki case ARPHRD_FDDI: 2287073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_eui48(eui, dev); 2288073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ARCNET: 2289073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_arcnet(eui, dev); 2290073a8e0eSYOSHIFUJI Hideaki case ARPHRD_INFINIBAND: 2291073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_infiniband(eui, dev); 2292c7dc89c0SFred L. Templin case ARPHRD_SIT: 2293dfd982baSYOSHIFUJI Hideaki return addrconf_ifid_sit(eui, dev); 2294aee80b54Sstephen hemminger case ARPHRD_IPGRE: 229545ce0fd1SFelix Jia case ARPHRD_TUNNEL: 2296aee80b54Sstephen hemminger return addrconf_ifid_gre(eui, dev); 2297e74bccb8SJukka Rissanen case ARPHRD_6LOWPAN: 22988a7a4b47SAlexander Aring return addrconf_ifid_6lowpan(eui, dev); 2299cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 case ARPHRD_IEEE1394: 2300cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return addrconf_ifid_ieee1394(eui, dev); 2301e837735eSNicolas Dichtel case ARPHRD_TUNNEL6: 230245ce0fd1SFelix Jia case ARPHRD_IP6GRE: 23039deb441cSSubash Abhinov Kasiviswanathan case ARPHRD_RAWIP: 2304e837735eSNicolas Dichtel return addrconf_ifid_ip6tnl(eui, dev); 2305073a8e0eSYOSHIFUJI Hideaki } 23061da177e4SLinus Torvalds return -1; 23071da177e4SLinus Torvalds } 23081da177e4SLinus Torvalds 23091da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev) 23101da177e4SLinus Torvalds { 23111da177e4SLinus Torvalds int err = -1; 23121da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 23131da177e4SLinus Torvalds 23141da177e4SLinus Torvalds read_lock_bh(&idev->lock); 2315602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) { 2316602582caSHannes Frederic Sowa if (ifp->scope > IFA_LINK) 2317602582caSHannes Frederic Sowa break; 23181da177e4SLinus Torvalds if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) { 23191da177e4SLinus Torvalds memcpy(eui, ifp->addr.s6_addr+8, 8); 23201da177e4SLinus Torvalds err = 0; 23211da177e4SLinus Torvalds break; 23221da177e4SLinus Torvalds } 23231da177e4SLinus Torvalds } 23241da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 23251da177e4SLinus Torvalds return err; 23261da177e4SLinus Torvalds } 23271da177e4SLinus Torvalds 23281da177e4SLinus Torvalds /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */ 23299d6280daSJiri Bohac static void ipv6_regen_rndid(struct inet6_dev *idev) 23301da177e4SLinus Torvalds { 23311da177e4SLinus Torvalds regen: 2332955189efSYOSHIFUJI Hideaki get_random_bytes(idev->rndid, sizeof(idev->rndid)); 23331da177e4SLinus Torvalds idev->rndid[0] &= ~0x02; 23341da177e4SLinus Torvalds 23351da177e4SLinus Torvalds /* 23361da177e4SLinus Torvalds * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>: 23371da177e4SLinus Torvalds * check if generated address is not inappropriate 23381da177e4SLinus Torvalds * 23391da177e4SLinus Torvalds * - Reserved subnet anycast (RFC 2526) 23401da177e4SLinus Torvalds * 11111101 11....11 1xxxxxxx 2341c7dc89c0SFred L. Templin * - ISATAP (RFC4214) 6.1 23421da177e4SLinus Torvalds * 00-00-5E-FE-xx-xx-xx-xx 23431da177e4SLinus Torvalds * - value 0 23441da177e4SLinus Torvalds * - XXX: already assigned to an address on the device 23451da177e4SLinus Torvalds */ 23461da177e4SLinus Torvalds if (idev->rndid[0] == 0xfd && 23471da177e4SLinus Torvalds (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff && 23481da177e4SLinus Torvalds (idev->rndid[7]&0x80)) 23491da177e4SLinus Torvalds goto regen; 23501da177e4SLinus Torvalds if ((idev->rndid[0]|idev->rndid[1]) == 0) { 23511da177e4SLinus Torvalds if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe) 23521da177e4SLinus Torvalds goto regen; 23531da177e4SLinus Torvalds if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00) 23541da177e4SLinus Torvalds goto regen; 23551da177e4SLinus Torvalds } 23561da177e4SLinus Torvalds } 23571da177e4SLinus Torvalds 23589d6280daSJiri Bohac static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) 23598e5e8f30SEldad Zack { 23601da177e4SLinus Torvalds if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0) 23619d6280daSJiri Bohac ipv6_regen_rndid(idev); 23621da177e4SLinus Torvalds } 23631da177e4SLinus Torvalds 23641da177e4SLinus Torvalds /* 23651da177e4SLinus Torvalds * Add prefix route. 23661da177e4SLinus Torvalds */ 23671da177e4SLinus Torvalds 23681da177e4SLinus Torvalds static void 23698308f3ffSDavid Ahern addrconf_prefix_route(struct in6_addr *pfx, int plen, u32 metric, 23708308f3ffSDavid Ahern struct net_device *dev, unsigned long expires, 23718308f3ffSDavid Ahern u32 flags, gfp_t gfp_flags) 23721da177e4SLinus Torvalds { 237386872cb5SThomas Graf struct fib6_config cfg = { 2374ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX, 23758308f3ffSDavid Ahern .fc_metric = metric ? : IP6_RT_PRIO_ADDRCONF, 237686872cb5SThomas Graf .fc_ifindex = dev->ifindex, 237786872cb5SThomas Graf .fc_expires = expires, 237886872cb5SThomas Graf .fc_dst_len = plen, 237986872cb5SThomas Graf .fc_flags = RTF_UP | flags, 2380c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 2381f410a1fbSStephen Hemminger .fc_protocol = RTPROT_KERNEL, 2382e8478e80SDavid Ahern .fc_type = RTN_UNICAST, 238386872cb5SThomas Graf }; 23841da177e4SLinus Torvalds 23854e3fd7a0SAlexey Dobriyan cfg.fc_dst = *pfx; 23861da177e4SLinus Torvalds 23871da177e4SLinus Torvalds /* Prevent useless cloning on PtP SIT. 23881da177e4SLinus Torvalds This thing is done here expecting that the whole 23891da177e4SLinus Torvalds class of non-broadcast devices need not cloning. 23901da177e4SLinus Torvalds */ 239107a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 23921da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT)) 239386872cb5SThomas Graf cfg.fc_flags |= RTF_NONEXTHOP; 23940be669bbSJoerg Roedel #endif 23951da177e4SLinus Torvalds 2396acb54e3cSDavid Ahern ip6_route_add(&cfg, gfp_flags, NULL); 23971da177e4SLinus Torvalds } 23981da177e4SLinus Torvalds 239914ef37b6SAndreas Hofmeister 24008d1c802bSDavid Ahern static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx, 240114ef37b6SAndreas Hofmeister int plen, 240214ef37b6SAndreas Hofmeister const struct net_device *dev, 24032b2450caSDavid Ahern u32 flags, u32 noflags, 24042b2450caSDavid Ahern bool no_gw) 240514ef37b6SAndreas Hofmeister { 240614ef37b6SAndreas Hofmeister struct fib6_node *fn; 24078d1c802bSDavid Ahern struct fib6_info *rt = NULL; 240814ef37b6SAndreas Hofmeister struct fib6_table *table; 2409ca254490SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX; 241014ef37b6SAndreas Hofmeister 2411ca254490SDavid Ahern table = fib6_get_table(dev_net(dev), tb_id); 241263159f29SIan Morris if (!table) 241314ef37b6SAndreas Hofmeister return NULL; 241414ef37b6SAndreas Hofmeister 241566f5d6ceSWei Wang rcu_read_lock(); 241638fbeeeeSWei Wang fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0, true); 241714ef37b6SAndreas Hofmeister if (!fn) 241814ef37b6SAndreas Hofmeister goto out; 24191f56a01fSMartin KaFai Lau 242066f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 2421f88d8ea6SDavid Ahern /* prefix routes only use builtin fib6_nh */ 2422f88d8ea6SDavid Ahern if (rt->nh) 2423f88d8ea6SDavid Ahern continue; 2424f88d8ea6SDavid Ahern 24251cf844c7SDavid Ahern if (rt->fib6_nh->fib_nh_dev->ifindex != dev->ifindex) 242614ef37b6SAndreas Hofmeister continue; 24271cf844c7SDavid Ahern if (no_gw && rt->fib6_nh->fib_nh_gw_family) 24282b2450caSDavid Ahern continue; 242993c2fb25SDavid Ahern if ((rt->fib6_flags & flags) != flags) 243014ef37b6SAndreas Hofmeister continue; 243193c2fb25SDavid Ahern if ((rt->fib6_flags & noflags) != 0) 243214ef37b6SAndreas Hofmeister continue; 2433e873e4b9SWei Wang if (!fib6_info_hold_safe(rt)) 2434e873e4b9SWei Wang continue; 243514ef37b6SAndreas Hofmeister break; 243614ef37b6SAndreas Hofmeister } 243714ef37b6SAndreas Hofmeister out: 243866f5d6ceSWei Wang rcu_read_unlock(); 243914ef37b6SAndreas Hofmeister return rt; 244014ef37b6SAndreas Hofmeister } 244114ef37b6SAndreas Hofmeister 244214ef37b6SAndreas Hofmeister 24431da177e4SLinus Torvalds /* Create "default" multicast route to the interface */ 24441da177e4SLinus Torvalds 24451da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev) 24461da177e4SLinus Torvalds { 244786872cb5SThomas Graf struct fib6_config cfg = { 2448ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL, 244986872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 245086872cb5SThomas Graf .fc_ifindex = dev->ifindex, 245186872cb5SThomas Graf .fc_dst_len = 8, 245286872cb5SThomas Graf .fc_flags = RTF_UP, 2453e8478e80SDavid Ahern .fc_type = RTN_UNICAST, 2454c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 245586872cb5SThomas Graf }; 24561da177e4SLinus Torvalds 245786872cb5SThomas Graf ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0); 245886872cb5SThomas Graf 2459e500c6d3SCong Wang ip6_route_add(&cfg, GFP_KERNEL, NULL); 24601da177e4SLinus Torvalds } 24611da177e4SLinus Torvalds 24621da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev) 24631da177e4SLinus Torvalds { 24641da177e4SLinus Torvalds struct inet6_dev *idev; 24651da177e4SLinus Torvalds 24661da177e4SLinus Torvalds ASSERT_RTNL(); 24671da177e4SLinus Torvalds 2468e21e8467SStephen Hemminger idev = ipv6_find_idev(dev); 2469db0b99f5SSabrina Dubroca if (IS_ERR(idev)) 2470db0b99f5SSabrina Dubroca return idev; 247164e724f6SBrian Haley 247264e724f6SBrian Haley if (idev->cnf.disable_ipv6) 247364e724f6SBrian Haley return ERR_PTR(-EACCES); 24741da177e4SLinus Torvalds 24751da177e4SLinus Torvalds /* Add default multicast route */ 2476ba46ee4cSDavid Ahern if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev)) 24771da177e4SLinus Torvalds addrconf_add_mroute(dev); 24781da177e4SLinus Torvalds 24791da177e4SLinus Torvalds return idev; 24801da177e4SLinus Torvalds } 24811da177e4SLinus Torvalds 248253bd6749SJiri Pirko static void manage_tempaddrs(struct inet6_dev *idev, 248353bd6749SJiri Pirko struct inet6_ifaddr *ifp, 248453bd6749SJiri Pirko __u32 valid_lft, __u32 prefered_lft, 248553bd6749SJiri Pirko bool create, unsigned long now) 248653bd6749SJiri Pirko { 248753bd6749SJiri Pirko u32 flags; 248853bd6749SJiri Pirko struct inet6_ifaddr *ift; 248953bd6749SJiri Pirko 249053bd6749SJiri Pirko read_lock_bh(&idev->lock); 249153bd6749SJiri Pirko /* update all temporary addresses in the list */ 249253bd6749SJiri Pirko list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) { 249353bd6749SJiri Pirko int age, max_valid, max_prefered; 249453bd6749SJiri Pirko 249553bd6749SJiri Pirko if (ifp != ift->ifpub) 249653bd6749SJiri Pirko continue; 249753bd6749SJiri Pirko 249853bd6749SJiri Pirko /* RFC 4941 section 3.3: 249953bd6749SJiri Pirko * If a received option will extend the lifetime of a public 250053bd6749SJiri Pirko * address, the lifetimes of temporary addresses should 250153bd6749SJiri Pirko * be extended, subject to the overall constraint that no 250253bd6749SJiri Pirko * temporary addresses should ever remain "valid" or "preferred" 250353bd6749SJiri Pirko * for a time longer than (TEMP_VALID_LIFETIME) or 250453bd6749SJiri Pirko * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively. 250553bd6749SJiri Pirko */ 250653bd6749SJiri Pirko age = (now - ift->cstamp) / HZ; 250753bd6749SJiri Pirko max_valid = idev->cnf.temp_valid_lft - age; 250853bd6749SJiri Pirko if (max_valid < 0) 250953bd6749SJiri Pirko max_valid = 0; 251053bd6749SJiri Pirko 251153bd6749SJiri Pirko max_prefered = idev->cnf.temp_prefered_lft - 251276506a98SJiri Bohac idev->desync_factor - age; 251353bd6749SJiri Pirko if (max_prefered < 0) 251453bd6749SJiri Pirko max_prefered = 0; 251553bd6749SJiri Pirko 251653bd6749SJiri Pirko if (valid_lft > max_valid) 251753bd6749SJiri Pirko valid_lft = max_valid; 251853bd6749SJiri Pirko 251953bd6749SJiri Pirko if (prefered_lft > max_prefered) 252053bd6749SJiri Pirko prefered_lft = max_prefered; 252153bd6749SJiri Pirko 252253bd6749SJiri Pirko spin_lock(&ift->lock); 252353bd6749SJiri Pirko flags = ift->flags; 252453bd6749SJiri Pirko ift->valid_lft = valid_lft; 252553bd6749SJiri Pirko ift->prefered_lft = prefered_lft; 252653bd6749SJiri Pirko ift->tstamp = now; 252753bd6749SJiri Pirko if (prefered_lft > 0) 252853bd6749SJiri Pirko ift->flags &= ~IFA_F_DEPRECATED; 252953bd6749SJiri Pirko 253053bd6749SJiri Pirko spin_unlock(&ift->lock); 253153bd6749SJiri Pirko if (!(flags&IFA_F_TENTATIVE)) 253253bd6749SJiri Pirko ipv6_ifa_notify(0, ift); 253353bd6749SJiri Pirko } 253453bd6749SJiri Pirko 253553bd6749SJiri Pirko if ((create || list_empty(&idev->tempaddr_list)) && 253653bd6749SJiri Pirko idev->cnf.use_tempaddr > 0) { 253753bd6749SJiri Pirko /* When a new public address is created as described 253853bd6749SJiri Pirko * in [ADDRCONF], also create a new temporary address. 253953bd6749SJiri Pirko * Also create a temporary address if it's enabled but 254053bd6749SJiri Pirko * no temporary address currently exists. 254153bd6749SJiri Pirko */ 254253bd6749SJiri Pirko read_unlock_bh(&idev->lock); 2543fffcefe9SEric Dumazet ipv6_create_tempaddr(ifp, NULL, false); 254453bd6749SJiri Pirko } else { 254553bd6749SJiri Pirko read_unlock_bh(&idev->lock); 254653bd6749SJiri Pirko } 254753bd6749SJiri Pirko } 254853bd6749SJiri Pirko 2549cc9da6ccSBjørn Mork static bool is_addr_mode_generate_stable(struct inet6_dev *idev) 2550cc9da6ccSBjørn Mork { 2551d35a00b8SFelix Jia return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY || 2552d35a00b8SFelix Jia idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM; 2553cc9da6ccSBjørn Mork } 2554cc9da6ccSBjørn Mork 2555cc84b3c6SAlexander Aring int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev, 25564f672235SAlexander Aring const struct prefix_info *pinfo, 25574f672235SAlexander Aring struct inet6_dev *in6_dev, 2558cc84b3c6SAlexander Aring const struct in6_addr *addr, int addr_type, 2559cc84b3c6SAlexander Aring u32 addr_flags, bool sllao, bool tokenized, 25604f672235SAlexander Aring __u32 valid_lft, u32 prefered_lft) 25614f672235SAlexander Aring { 25624f672235SAlexander Aring struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1); 25634f672235SAlexander Aring int create = 0, update_lft = 0; 25644f672235SAlexander Aring 25654f672235SAlexander Aring if (!ifp && valid_lft) { 25664f672235SAlexander Aring int max_addresses = in6_dev->cnf.max_addresses; 2567e6464b8cSDavid Ahern struct ifa6_config cfg = { 2568e6464b8cSDavid Ahern .pfx = addr, 2569e6464b8cSDavid Ahern .plen = pinfo->prefix_len, 2570e6464b8cSDavid Ahern .ifa_flags = addr_flags, 2571e6464b8cSDavid Ahern .valid_lft = valid_lft, 2572e6464b8cSDavid Ahern .preferred_lft = prefered_lft, 2573e6464b8cSDavid Ahern .scope = addr_type & IPV6_ADDR_SCOPE_MASK, 2574e6464b8cSDavid Ahern }; 25754f672235SAlexander Aring 25764f672235SAlexander Aring #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 257735e015e1SMatteo Croce if ((net->ipv6.devconf_all->optimistic_dad || 257835e015e1SMatteo Croce in6_dev->cnf.optimistic_dad) && 25794f672235SAlexander Aring !net->ipv6.devconf_all->forwarding && sllao) 2580e6464b8cSDavid Ahern cfg.ifa_flags |= IFA_F_OPTIMISTIC; 25814f672235SAlexander Aring #endif 25824f672235SAlexander Aring 25834f672235SAlexander Aring /* Do not allow to create too much of autoconfigured 25844f672235SAlexander Aring * addresses; this would be too easy way to crash kernel. 25854f672235SAlexander Aring */ 25864f672235SAlexander Aring if (!max_addresses || 25874f672235SAlexander Aring ipv6_count_addresses(in6_dev) < max_addresses) 2588e6464b8cSDavid Ahern ifp = ipv6_add_addr(in6_dev, &cfg, false, NULL); 25894f672235SAlexander Aring 25904f672235SAlexander Aring if (IS_ERR_OR_NULL(ifp)) 25914f672235SAlexander Aring return -1; 25924f672235SAlexander Aring 25934f672235SAlexander Aring create = 1; 25944f672235SAlexander Aring spin_lock_bh(&ifp->lock); 25954f672235SAlexander Aring ifp->flags |= IFA_F_MANAGETEMPADDR; 25964f672235SAlexander Aring ifp->cstamp = jiffies; 25974f672235SAlexander Aring ifp->tokenized = tokenized; 25984f672235SAlexander Aring spin_unlock_bh(&ifp->lock); 25994f672235SAlexander Aring addrconf_dad_start(ifp); 26004f672235SAlexander Aring } 26014f672235SAlexander Aring 26024f672235SAlexander Aring if (ifp) { 26034f672235SAlexander Aring u32 flags; 26044f672235SAlexander Aring unsigned long now; 26054f672235SAlexander Aring u32 stored_lft; 26064f672235SAlexander Aring 26074f672235SAlexander Aring /* update lifetime (RFC2462 5.5.3 e) */ 26084f672235SAlexander Aring spin_lock_bh(&ifp->lock); 26094f672235SAlexander Aring now = jiffies; 26104f672235SAlexander Aring if (ifp->valid_lft > (now - ifp->tstamp) / HZ) 26114f672235SAlexander Aring stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ; 26124f672235SAlexander Aring else 26134f672235SAlexander Aring stored_lft = 0; 2614f85f94b8SLorenzo Bianconi if (!create && stored_lft) { 26154f672235SAlexander Aring const u32 minimum_lft = min_t(u32, 26164f672235SAlexander Aring stored_lft, MIN_VALID_LIFETIME); 26174f672235SAlexander Aring valid_lft = max(valid_lft, minimum_lft); 26184f672235SAlexander Aring 26194f672235SAlexander Aring /* RFC4862 Section 5.5.3e: 26204f672235SAlexander Aring * "Note that the preferred lifetime of the 26214f672235SAlexander Aring * corresponding address is always reset to 26224f672235SAlexander Aring * the Preferred Lifetime in the received 26234f672235SAlexander Aring * Prefix Information option, regardless of 26244f672235SAlexander Aring * whether the valid lifetime is also reset or 26254f672235SAlexander Aring * ignored." 26264f672235SAlexander Aring * 26274f672235SAlexander Aring * So we should always update prefered_lft here. 26284f672235SAlexander Aring */ 26294f672235SAlexander Aring update_lft = 1; 26304f672235SAlexander Aring } 26314f672235SAlexander Aring 26324f672235SAlexander Aring if (update_lft) { 26334f672235SAlexander Aring ifp->valid_lft = valid_lft; 26344f672235SAlexander Aring ifp->prefered_lft = prefered_lft; 26354f672235SAlexander Aring ifp->tstamp = now; 26364f672235SAlexander Aring flags = ifp->flags; 26374f672235SAlexander Aring ifp->flags &= ~IFA_F_DEPRECATED; 26384f672235SAlexander Aring spin_unlock_bh(&ifp->lock); 26394f672235SAlexander Aring 26404f672235SAlexander Aring if (!(flags&IFA_F_TENTATIVE)) 26414f672235SAlexander Aring ipv6_ifa_notify(0, ifp); 26424f672235SAlexander Aring } else 26434f672235SAlexander Aring spin_unlock_bh(&ifp->lock); 26444f672235SAlexander Aring 26454f672235SAlexander Aring manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft, 26464f672235SAlexander Aring create, now); 26474f672235SAlexander Aring 26484f672235SAlexander Aring in6_ifa_put(ifp); 26494f672235SAlexander Aring addrconf_verify(); 26504f672235SAlexander Aring } 26514f672235SAlexander Aring 26524f672235SAlexander Aring return 0; 26534f672235SAlexander Aring } 2654cc84b3c6SAlexander Aring EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr); 26554f672235SAlexander Aring 2656e6bff995SNeil Horman void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao) 26571da177e4SLinus Torvalds { 26581da177e4SLinus Torvalds struct prefix_info *pinfo; 26591da177e4SLinus Torvalds __u32 valid_lft; 26601da177e4SLinus Torvalds __u32 prefered_lft; 26614f672235SAlexander Aring int addr_type, err; 266264236f3fSHannes Frederic Sowa u32 addr_flags = 0; 26631da177e4SLinus Torvalds struct inet6_dev *in6_dev; 266456d417b1SBrian Haley struct net *net = dev_net(dev); 26651da177e4SLinus Torvalds 26661da177e4SLinus Torvalds pinfo = (struct prefix_info *) opt; 26671da177e4SLinus Torvalds 26681da177e4SLinus Torvalds if (len < sizeof(struct prefix_info)) { 2669e32ac250SJoe Perches netdev_dbg(dev, "addrconf: prefix option too short\n"); 26701da177e4SLinus Torvalds return; 26711da177e4SLinus Torvalds } 26721da177e4SLinus Torvalds 26731da177e4SLinus Torvalds /* 26741da177e4SLinus Torvalds * Validation checks ([ADDRCONF], page 19) 26751da177e4SLinus Torvalds */ 26761da177e4SLinus Torvalds 26771da177e4SLinus Torvalds addr_type = ipv6_addr_type(&pinfo->prefix); 26781da177e4SLinus Torvalds 26791da177e4SLinus Torvalds if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)) 26801da177e4SLinus Torvalds return; 26811da177e4SLinus Torvalds 26821da177e4SLinus Torvalds valid_lft = ntohl(pinfo->valid); 26831da177e4SLinus Torvalds prefered_lft = ntohl(pinfo->prefered); 26841da177e4SLinus Torvalds 26851da177e4SLinus Torvalds if (prefered_lft > valid_lft) { 2686e87cc472SJoe Perches net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n"); 26871da177e4SLinus Torvalds return; 26881da177e4SLinus Torvalds } 26891da177e4SLinus Torvalds 26901da177e4SLinus Torvalds in6_dev = in6_dev_get(dev); 26911da177e4SLinus Torvalds 269263159f29SIan Morris if (!in6_dev) { 2693e87cc472SJoe Perches net_dbg_ratelimited("addrconf: device %s not configured\n", 2694e87cc472SJoe Perches dev->name); 26951da177e4SLinus Torvalds return; 26961da177e4SLinus Torvalds } 26971da177e4SLinus Torvalds 26981da177e4SLinus Torvalds /* 26991da177e4SLinus Torvalds * Two things going on here: 27001da177e4SLinus Torvalds * 1) Add routes for on-link prefixes 27011da177e4SLinus Torvalds * 2) Configure prefixes with the auto flag set 27021da177e4SLinus Torvalds */ 27031da177e4SLinus Torvalds 27044bed72e4SYOSHIFUJI Hideaki if (pinfo->onlink) { 27058d1c802bSDavid Ahern struct fib6_info *rt; 27064bed72e4SYOSHIFUJI Hideaki unsigned long rt_expires; 27074bed72e4SYOSHIFUJI Hideaki 27081da177e4SLinus Torvalds /* Avoid arithmetic overflow. Really, we could 27096f704992SYOSHIFUJI Hideaki * save rt_expires in seconds, likely valid_lft, 27106f704992SYOSHIFUJI Hideaki * but it would require division in fib gc, that it 27116f704992SYOSHIFUJI Hideaki * not good. 27121da177e4SLinus Torvalds */ 27134bed72e4SYOSHIFUJI Hideaki if (HZ > USER_HZ) 27144bed72e4SYOSHIFUJI Hideaki rt_expires = addrconf_timeout_fixup(valid_lft, HZ); 27154bed72e4SYOSHIFUJI Hideaki else 27164bed72e4SYOSHIFUJI Hideaki rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ); 27173dd4bc68SYOSHIFUJI Hideaki 27184bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(rt_expires)) 27194bed72e4SYOSHIFUJI Hideaki rt_expires *= HZ; 27201da177e4SLinus Torvalds 272114ef37b6SAndreas Hofmeister rt = addrconf_get_prefix_route(&pinfo->prefix, 272214ef37b6SAndreas Hofmeister pinfo->prefix_len, 272314ef37b6SAndreas Hofmeister dev, 272414ef37b6SAndreas Hofmeister RTF_ADDRCONF | RTF_PREFIX_RT, 27252b2450caSDavid Ahern RTF_DEFAULT, true); 27261da177e4SLinus Torvalds 272714ef37b6SAndreas Hofmeister if (rt) { 27286f704992SYOSHIFUJI Hideaki /* Autoconf prefix route */ 27291da177e4SLinus Torvalds if (valid_lft == 0) { 2730afb1d4b5SDavid Ahern ip6_del_rt(net, rt); 27311da177e4SLinus Torvalds rt = NULL; 27324bed72e4SYOSHIFUJI Hideaki } else if (addrconf_finite_timeout(rt_expires)) { 27336f704992SYOSHIFUJI Hideaki /* not infinity */ 273414895687SDavid Ahern fib6_set_expires(rt, jiffies + rt_expires); 27356f704992SYOSHIFUJI Hideaki } else { 273614895687SDavid Ahern fib6_clean_expires(rt); 27371da177e4SLinus Torvalds } 27381da177e4SLinus Torvalds } else if (valid_lft) { 27396f704992SYOSHIFUJI Hideaki clock_t expires = 0; 27404bed72e4SYOSHIFUJI Hideaki int flags = RTF_ADDRCONF | RTF_PREFIX_RT; 27414bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(rt_expires)) { 27426f704992SYOSHIFUJI Hideaki /* not infinity */ 27436f704992SYOSHIFUJI Hideaki flags |= RTF_EXPIRES; 27446f704992SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(rt_expires); 27456f704992SYOSHIFUJI Hideaki } 27461da177e4SLinus Torvalds addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len, 27478308f3ffSDavid Ahern 0, dev, expires, flags, 27488308f3ffSDavid Ahern GFP_ATOMIC); 27491da177e4SLinus Torvalds } 275093531c67SDavid Ahern fib6_info_release(rt); 27511da177e4SLinus Torvalds } 27521da177e4SLinus Torvalds 27531da177e4SLinus Torvalds /* Try to figure out our local address for this prefix */ 27541da177e4SLinus Torvalds 27551da177e4SLinus Torvalds if (pinfo->autoconf && in6_dev->cnf.autoconf) { 27561da177e4SLinus Torvalds struct in6_addr addr; 2757f997c55cSAlexander Aring bool tokenized = false, dev_addr_generated = false; 27581da177e4SLinus Torvalds 27591da177e4SLinus Torvalds if (pinfo->prefix_len == 64) { 27601da177e4SLinus Torvalds memcpy(&addr, &pinfo->prefix, 8); 2761f53adae4SDaniel Borkmann 2762f53adae4SDaniel Borkmann if (!ipv6_addr_any(&in6_dev->token)) { 2763f53adae4SDaniel Borkmann read_lock_bh(&in6_dev->lock); 2764f53adae4SDaniel Borkmann memcpy(addr.s6_addr + 8, 2765f53adae4SDaniel Borkmann in6_dev->token.s6_addr + 8, 8); 2766f53adae4SDaniel Borkmann read_unlock_bh(&in6_dev->lock); 2767617fe29dSDaniel Borkmann tokenized = true; 2768cc9da6ccSBjørn Mork } else if (is_addr_mode_generate_stable(in6_dev) && 2769622c81d5SHannes Frederic Sowa !ipv6_generate_stable_address(&addr, 0, 2770622c81d5SHannes Frederic Sowa in6_dev)) { 277164236f3fSHannes Frederic Sowa addr_flags |= IFA_F_STABLE_PRIVACY; 2772622c81d5SHannes Frederic Sowa goto ok; 2773f53adae4SDaniel Borkmann } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) && 27741da177e4SLinus Torvalds ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) { 27754f672235SAlexander Aring goto put; 2776f997c55cSAlexander Aring } else { 2777f997c55cSAlexander Aring dev_addr_generated = true; 27781da177e4SLinus Torvalds } 27791da177e4SLinus Torvalds goto ok; 27801da177e4SLinus Torvalds } 2781e87cc472SJoe Perches net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n", 27821da177e4SLinus Torvalds pinfo->prefix_len); 27834f672235SAlexander Aring goto put; 27841da177e4SLinus Torvalds 27851da177e4SLinus Torvalds ok: 27864f672235SAlexander Aring err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, 27874f672235SAlexander Aring &addr, addr_type, 27884f672235SAlexander Aring addr_flags, sllao, 27894f672235SAlexander Aring tokenized, valid_lft, 27908a226b2cSJiri Benc prefered_lft); 27914f672235SAlexander Aring if (err) 27924f672235SAlexander Aring goto put; 2793f997c55cSAlexander Aring 2794f997c55cSAlexander Aring /* Ignore error case here because previous prefix add addr was 2795f997c55cSAlexander Aring * successful which will be notified. 2796f997c55cSAlexander Aring */ 2797f997c55cSAlexander Aring ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr, 2798f997c55cSAlexander Aring addr_type, addr_flags, sllao, 2799f997c55cSAlexander Aring tokenized, valid_lft, 2800f997c55cSAlexander Aring prefered_lft, 2801f997c55cSAlexander Aring dev_addr_generated); 28021da177e4SLinus Torvalds } 28031da177e4SLinus Torvalds inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo); 28044f672235SAlexander Aring put: 28051da177e4SLinus Torvalds in6_dev_put(in6_dev); 28061da177e4SLinus Torvalds } 28071da177e4SLinus Torvalds 28081da177e4SLinus Torvalds /* 28091da177e4SLinus Torvalds * Set destination address. 28101da177e4SLinus Torvalds * Special case for SIT interfaces where we create a new "virtual" 28111da177e4SLinus Torvalds * device. 28121da177e4SLinus Torvalds */ 2813af284937SDaniel Lezcano int addrconf_set_dstaddr(struct net *net, void __user *arg) 28141da177e4SLinus Torvalds { 28151da177e4SLinus Torvalds struct in6_ifreq ireq; 28161da177e4SLinus Torvalds struct net_device *dev; 28171da177e4SLinus Torvalds int err = -EINVAL; 28181da177e4SLinus Torvalds 28191da177e4SLinus Torvalds rtnl_lock(); 28201da177e4SLinus Torvalds 28211da177e4SLinus Torvalds err = -EFAULT; 28221da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 28231da177e4SLinus Torvalds goto err_exit; 28241da177e4SLinus Torvalds 2825af284937SDaniel Lezcano dev = __dev_get_by_index(net, ireq.ifr6_ifindex); 28261da177e4SLinus Torvalds 28271da177e4SLinus Torvalds err = -ENODEV; 282863159f29SIan Morris if (!dev) 28291da177e4SLinus Torvalds goto err_exit; 28301da177e4SLinus Torvalds 283107a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 28321da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT) { 28335bc3eb7eSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 28341da177e4SLinus Torvalds struct ifreq ifr; 28351da177e4SLinus Torvalds struct ip_tunnel_parm p; 28361da177e4SLinus Torvalds 28371da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 28381da177e4SLinus Torvalds if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4)) 28391da177e4SLinus Torvalds goto err_exit; 28401da177e4SLinus Torvalds 28411da177e4SLinus Torvalds memset(&p, 0, sizeof(p)); 28421da177e4SLinus Torvalds p.iph.daddr = ireq.ifr6_addr.s6_addr32[3]; 28431da177e4SLinus Torvalds p.iph.saddr = 0; 28441da177e4SLinus Torvalds p.iph.version = 4; 28451da177e4SLinus Torvalds p.iph.ihl = 5; 28461da177e4SLinus Torvalds p.iph.protocol = IPPROTO_IPV6; 28471da177e4SLinus Torvalds p.iph.ttl = 64; 2848d20b3109SStephen Hemminger ifr.ifr_ifru.ifru_data = (__force void __user *)&p; 28491da177e4SLinus Torvalds 28505bc3eb7eSStephen Hemminger if (ops->ndo_do_ioctl) { 28515bc3eb7eSStephen Hemminger mm_segment_t oldfs = get_fs(); 28525bc3eb7eSStephen Hemminger 28535bc3eb7eSStephen Hemminger set_fs(KERNEL_DS); 28545bc3eb7eSStephen Hemminger err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL); 28551da177e4SLinus Torvalds set_fs(oldfs); 28565bc3eb7eSStephen Hemminger } else 28575bc3eb7eSStephen Hemminger err = -EOPNOTSUPP; 28581da177e4SLinus Torvalds 28591da177e4SLinus Torvalds if (err == 0) { 28601da177e4SLinus Torvalds err = -ENOBUFS; 2861af284937SDaniel Lezcano dev = __dev_get_by_name(net, p.name); 2862af284937SDaniel Lezcano if (!dev) 28631da177e4SLinus Torvalds goto err_exit; 286400f54e68SPetr Machata err = dev_open(dev, NULL); 28651da177e4SLinus Torvalds } 28661da177e4SLinus Torvalds } 28670be669bbSJoerg Roedel #endif 28681da177e4SLinus Torvalds 28691da177e4SLinus Torvalds err_exit: 28701da177e4SLinus Torvalds rtnl_unlock(); 28711da177e4SLinus Torvalds return err; 28721da177e4SLinus Torvalds } 28731da177e4SLinus Torvalds 287493a714d6SMadhu Challa static int ipv6_mc_config(struct sock *sk, bool join, 287593a714d6SMadhu Challa const struct in6_addr *addr, int ifindex) 287693a714d6SMadhu Challa { 287793a714d6SMadhu Challa int ret; 287893a714d6SMadhu Challa 287993a714d6SMadhu Challa ASSERT_RTNL(); 288093a714d6SMadhu Challa 288193a714d6SMadhu Challa lock_sock(sk); 288293a714d6SMadhu Challa if (join) 288354ff9ef3SMarcelo Ricardo Leitner ret = ipv6_sock_mc_join(sk, ifindex, addr); 288493a714d6SMadhu Challa else 288554ff9ef3SMarcelo Ricardo Leitner ret = ipv6_sock_mc_drop(sk, ifindex, addr); 288693a714d6SMadhu Challa release_sock(sk); 288793a714d6SMadhu Challa 288893a714d6SMadhu Challa return ret; 288993a714d6SMadhu Challa } 289093a714d6SMadhu Challa 28911da177e4SLinus Torvalds /* 28921da177e4SLinus Torvalds * Manual configuration of address on an interface 28931da177e4SLinus Torvalds */ 2894479840ffSJiri Pirko static int inet6_addr_add(struct net *net, int ifindex, 289519b1518cSDavid Ahern struct ifa6_config *cfg, 2896de95e047SDavid Ahern struct netlink_ext_ack *extack) 28971da177e4SLinus Torvalds { 28981da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 28991da177e4SLinus Torvalds struct inet6_dev *idev; 29001da177e4SLinus Torvalds struct net_device *dev; 290193a714d6SMadhu Challa unsigned long timeout; 290293a714d6SMadhu Challa clock_t expires; 29036f704992SYOSHIFUJI Hideaki u32 flags; 29041da177e4SLinus Torvalds 29051da177e4SLinus Torvalds ASSERT_RTNL(); 29061da177e4SLinus Torvalds 290719b1518cSDavid Ahern if (cfg->plen > 128) 290824ef0da7SThomas Graf return -EINVAL; 290924ef0da7SThomas Graf 29100778769dSNoriaki TAKAMIYA /* check the lifetime */ 291119b1518cSDavid Ahern if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft) 29120778769dSNoriaki TAKAMIYA return -EINVAL; 29130778769dSNoriaki TAKAMIYA 291419b1518cSDavid Ahern if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR && cfg->plen != 64) 291553bd6749SJiri Pirko return -EINVAL; 291653bd6749SJiri Pirko 2917af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifindex); 2918af284937SDaniel Lezcano if (!dev) 29191da177e4SLinus Torvalds return -ENODEV; 29201da177e4SLinus Torvalds 292164e724f6SBrian Haley idev = addrconf_add_dev(dev); 292264e724f6SBrian Haley if (IS_ERR(idev)) 292364e724f6SBrian Haley return PTR_ERR(idev); 29241da177e4SLinus Torvalds 292519b1518cSDavid Ahern if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) { 292693a714d6SMadhu Challa int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk, 292719b1518cSDavid Ahern true, cfg->pfx, ifindex); 292893a714d6SMadhu Challa 292993a714d6SMadhu Challa if (ret < 0) 293093a714d6SMadhu Challa return ret; 293193a714d6SMadhu Challa } 293293a714d6SMadhu Challa 293319b1518cSDavid Ahern cfg->scope = ipv6_addr_scope(cfg->pfx); 29341da177e4SLinus Torvalds 293519b1518cSDavid Ahern timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ); 29364bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 29374bed72e4SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(timeout * HZ); 293819b1518cSDavid Ahern cfg->valid_lft = timeout; 29396f704992SYOSHIFUJI Hideaki flags = RTF_EXPIRES; 29404bed72e4SYOSHIFUJI Hideaki } else { 29414bed72e4SYOSHIFUJI Hideaki expires = 0; 29424bed72e4SYOSHIFUJI Hideaki flags = 0; 294319b1518cSDavid Ahern cfg->ifa_flags |= IFA_F_PERMANENT; 29446f704992SYOSHIFUJI Hideaki } 29450778769dSNoriaki TAKAMIYA 294619b1518cSDavid Ahern timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ); 29474bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 29484bed72e4SYOSHIFUJI Hideaki if (timeout == 0) 294919b1518cSDavid Ahern cfg->ifa_flags |= IFA_F_DEPRECATED; 295019b1518cSDavid Ahern cfg->preferred_lft = timeout; 29514bed72e4SYOSHIFUJI Hideaki } 29520778769dSNoriaki TAKAMIYA 295319b1518cSDavid Ahern ifp = ipv6_add_addr(idev, cfg, true, extack); 29541da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 295519b1518cSDavid Ahern if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) { 29568308f3ffSDavid Ahern addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 29578308f3ffSDavid Ahern ifp->rt_priority, dev, expires, 29588308f3ffSDavid Ahern flags, GFP_KERNEL); 2959761aac73SThomas Haller } 2960761aac73SThomas Haller 2961a2d481b3SLorenzo Bianconi /* Send a netlink notification if DAD is enabled and 2962a2d481b3SLorenzo Bianconi * optimistic flag is not set 2963a2d481b3SLorenzo Bianconi */ 2964a2d481b3SLorenzo Bianconi if (!(ifp->flags & (IFA_F_OPTIMISTIC | IFA_F_NODAD))) 2965a2d481b3SLorenzo Bianconi ipv6_ifa_notify(0, ifp); 296695c385b4SNeil Horman /* 296795c385b4SNeil Horman * Note that section 3.1 of RFC 4429 indicates 296895c385b4SNeil Horman * that the Optimistic flag should not be set for 296995c385b4SNeil Horman * manually configured addresses 297095c385b4SNeil Horman */ 2971cf22f9a2SDavid S. Miller addrconf_dad_start(ifp); 297219b1518cSDavid Ahern if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR) 297319b1518cSDavid Ahern manage_tempaddrs(idev, ifp, cfg->valid_lft, 297419b1518cSDavid Ahern cfg->preferred_lft, true, jiffies); 29751da177e4SLinus Torvalds in6_ifa_put(ifp); 2976c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 29771da177e4SLinus Torvalds return 0; 297819b1518cSDavid Ahern } else if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) { 297919b1518cSDavid Ahern ipv6_mc_config(net->ipv6.mc_autojoin_sk, false, 298019b1518cSDavid Ahern cfg->pfx, ifindex); 29811da177e4SLinus Torvalds } 29821da177e4SLinus Torvalds 29831da177e4SLinus Torvalds return PTR_ERR(ifp); 29841da177e4SLinus Torvalds } 29851da177e4SLinus Torvalds 29866046d5b4SHeiner Kallweit static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags, 29876046d5b4SHeiner Kallweit const struct in6_addr *pfx, unsigned int plen) 29881da177e4SLinus Torvalds { 29891da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 29901da177e4SLinus Torvalds struct inet6_dev *idev; 29911da177e4SLinus Torvalds struct net_device *dev; 29921da177e4SLinus Torvalds 299324ef0da7SThomas Graf if (plen > 128) 299424ef0da7SThomas Graf return -EINVAL; 299524ef0da7SThomas Graf 2996af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifindex); 2997af284937SDaniel Lezcano if (!dev) 29981da177e4SLinus Torvalds return -ENODEV; 29991da177e4SLinus Torvalds 3000e5d08d71SIan Morris idev = __in6_dev_get(dev); 300163159f29SIan Morris if (!idev) 30021da177e4SLinus Torvalds return -ENXIO; 30031da177e4SLinus Torvalds 30041da177e4SLinus Torvalds read_lock_bh(&idev->lock); 3005502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 30061da177e4SLinus Torvalds if (ifp->prefix_len == plen && 30071da177e4SLinus Torvalds ipv6_addr_equal(pfx, &ifp->addr)) { 30081da177e4SLinus Torvalds in6_ifa_hold(ifp); 30091da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 30101da177e4SLinus Torvalds 30116046d5b4SHeiner Kallweit if (!(ifp->flags & IFA_F_TEMPORARY) && 30126046d5b4SHeiner Kallweit (ifa_flags & IFA_F_MANAGETEMPADDR)) 30136046d5b4SHeiner Kallweit manage_tempaddrs(idev, ifp, 0, 0, false, 30146046d5b4SHeiner Kallweit jiffies); 30151da177e4SLinus Torvalds ipv6_del_addr(ifp); 30166046d5b4SHeiner Kallweit addrconf_verify_rtnl(); 301793a714d6SMadhu Challa if (ipv6_addr_is_multicast(pfx)) { 301893a714d6SMadhu Challa ipv6_mc_config(net->ipv6.mc_autojoin_sk, 301993a714d6SMadhu Challa false, pfx, dev->ifindex); 302093a714d6SMadhu Challa } 30211da177e4SLinus Torvalds return 0; 30221da177e4SLinus Torvalds } 30231da177e4SLinus Torvalds } 30241da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 30251da177e4SLinus Torvalds return -EADDRNOTAVAIL; 30261da177e4SLinus Torvalds } 30271da177e4SLinus Torvalds 30281da177e4SLinus Torvalds 3029af284937SDaniel Lezcano int addrconf_add_ifaddr(struct net *net, void __user *arg) 30301da177e4SLinus Torvalds { 303119b1518cSDavid Ahern struct ifa6_config cfg = { 303219b1518cSDavid Ahern .ifa_flags = IFA_F_PERMANENT, 303319b1518cSDavid Ahern .preferred_lft = INFINITY_LIFE_TIME, 303419b1518cSDavid Ahern .valid_lft = INFINITY_LIFE_TIME, 303519b1518cSDavid Ahern }; 30361da177e4SLinus Torvalds struct in6_ifreq ireq; 30371da177e4SLinus Torvalds int err; 30381da177e4SLinus Torvalds 3039af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 30401da177e4SLinus Torvalds return -EPERM; 30411da177e4SLinus Torvalds 30421da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 30431da177e4SLinus Torvalds return -EFAULT; 30441da177e4SLinus Torvalds 304519b1518cSDavid Ahern cfg.pfx = &ireq.ifr6_addr; 304619b1518cSDavid Ahern cfg.plen = ireq.ifr6_prefixlen; 304719b1518cSDavid Ahern 30481da177e4SLinus Torvalds rtnl_lock(); 304919b1518cSDavid Ahern err = inet6_addr_add(net, ireq.ifr6_ifindex, &cfg, NULL); 30501da177e4SLinus Torvalds rtnl_unlock(); 30511da177e4SLinus Torvalds return err; 30521da177e4SLinus Torvalds } 30531da177e4SLinus Torvalds 3054af284937SDaniel Lezcano int addrconf_del_ifaddr(struct net *net, void __user *arg) 30551da177e4SLinus Torvalds { 30561da177e4SLinus Torvalds struct in6_ifreq ireq; 30571da177e4SLinus Torvalds int err; 30581da177e4SLinus Torvalds 3059af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 30601da177e4SLinus Torvalds return -EPERM; 30611da177e4SLinus Torvalds 30621da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 30631da177e4SLinus Torvalds return -EFAULT; 30641da177e4SLinus Torvalds 30651da177e4SLinus Torvalds rtnl_lock(); 30666046d5b4SHeiner Kallweit err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr, 3067af284937SDaniel Lezcano ireq.ifr6_prefixlen); 30681da177e4SLinus Torvalds rtnl_unlock(); 30691da177e4SLinus Torvalds return err; 30701da177e4SLinus Torvalds } 30711da177e4SLinus Torvalds 3072b5f348e5SIlpo Järvinen static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr, 3073b5f348e5SIlpo Järvinen int plen, int scope) 3074b5f348e5SIlpo Järvinen { 3075b5f348e5SIlpo Järvinen struct inet6_ifaddr *ifp; 3076e6464b8cSDavid Ahern struct ifa6_config cfg = { 3077e6464b8cSDavid Ahern .pfx = addr, 3078e6464b8cSDavid Ahern .plen = plen, 3079e6464b8cSDavid Ahern .ifa_flags = IFA_F_PERMANENT, 3080e6464b8cSDavid Ahern .valid_lft = INFINITY_LIFE_TIME, 3081e6464b8cSDavid Ahern .preferred_lft = INFINITY_LIFE_TIME, 3082e6464b8cSDavid Ahern .scope = scope 3083e6464b8cSDavid Ahern }; 3084b5f348e5SIlpo Järvinen 3085e6464b8cSDavid Ahern ifp = ipv6_add_addr(idev, &cfg, true, NULL); 3086b5f348e5SIlpo Järvinen if (!IS_ERR(ifp)) { 3087b5f348e5SIlpo Järvinen spin_lock_bh(&ifp->lock); 3088b5f348e5SIlpo Järvinen ifp->flags &= ~IFA_F_TENTATIVE; 3089b5f348e5SIlpo Järvinen spin_unlock_bh(&ifp->lock); 3090764d3be6SPaolo Abeni rt_genid_bump_ipv6(dev_net(idev->dev)); 3091b5f348e5SIlpo Järvinen ipv6_ifa_notify(RTM_NEWADDR, ifp); 3092b5f348e5SIlpo Järvinen in6_ifa_put(ifp); 3093b5f348e5SIlpo Järvinen } 3094b5f348e5SIlpo Järvinen } 3095b5f348e5SIlpo Järvinen 309607a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 30971da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev) 30981da177e4SLinus Torvalds { 30991da177e4SLinus Torvalds struct in6_addr addr; 31001da177e4SLinus Torvalds struct net_device *dev; 3101c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 3102929c9cf3SNicolas Dichtel int scope, plen; 3103f0e2acfaSNicolas Dichtel u32 pflags = 0; 31041da177e4SLinus Torvalds 31051da177e4SLinus Torvalds ASSERT_RTNL(); 31061da177e4SLinus Torvalds 31071da177e4SLinus Torvalds memset(&addr, 0, sizeof(struct in6_addr)); 31081da177e4SLinus Torvalds memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4); 31091da177e4SLinus Torvalds 31101da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) { 31111da177e4SLinus Torvalds addr.s6_addr32[0] = htonl(0xfe800000); 31121da177e4SLinus Torvalds scope = IFA_LINK; 3113929c9cf3SNicolas Dichtel plen = 64; 31141da177e4SLinus Torvalds } else { 31151da177e4SLinus Torvalds scope = IPV6_ADDR_COMPATv4; 3116929c9cf3SNicolas Dichtel plen = 96; 3117f0e2acfaSNicolas Dichtel pflags |= RTF_NONEXTHOP; 31181da177e4SLinus Torvalds } 31191da177e4SLinus Torvalds 31201da177e4SLinus Torvalds if (addr.s6_addr32[3]) { 3121929c9cf3SNicolas Dichtel add_addr(idev, &addr, plen, scope); 31228308f3ffSDavid Ahern addrconf_prefix_route(&addr, plen, 0, idev->dev, 0, pflags, 3123e500c6d3SCong Wang GFP_KERNEL); 31241da177e4SLinus Torvalds return; 31251da177e4SLinus Torvalds } 31261da177e4SLinus Torvalds 31276fda7350SBenjamin Thery for_each_netdev(net, dev) { 3128e5ed6399SHerbert Xu struct in_device *in_dev = __in_dev_get_rtnl(dev); 31291da177e4SLinus Torvalds if (in_dev && (dev->flags & IFF_UP)) { 31301da177e4SLinus Torvalds struct in_ifaddr *ifa; 31311da177e4SLinus Torvalds int flag = scope; 31321da177e4SLinus Torvalds 3133cd5a411dSFlorian Westphal in_dev_for_each_ifa_rtnl(ifa, in_dev) { 31341da177e4SLinus Torvalds addr.s6_addr32[3] = ifa->ifa_local; 31351da177e4SLinus Torvalds 31361da177e4SLinus Torvalds if (ifa->ifa_scope == RT_SCOPE_LINK) 31371da177e4SLinus Torvalds continue; 31381da177e4SLinus Torvalds if (ifa->ifa_scope >= RT_SCOPE_HOST) { 31391da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) 31401da177e4SLinus Torvalds continue; 31411da177e4SLinus Torvalds flag |= IFA_HOST; 31421da177e4SLinus Torvalds } 31431da177e4SLinus Torvalds 3144b5f348e5SIlpo Järvinen add_addr(idev, &addr, plen, flag); 31458308f3ffSDavid Ahern addrconf_prefix_route(&addr, plen, 0, idev->dev, 3146e500c6d3SCong Wang 0, pflags, GFP_KERNEL); 31471da177e4SLinus Torvalds } 31481da177e4SLinus Torvalds } 31491da177e4SLinus Torvalds } 31501da177e4SLinus Torvalds } 31510be669bbSJoerg Roedel #endif 31521da177e4SLinus Torvalds 31531da177e4SLinus Torvalds static void init_loopback(struct net_device *dev) 31541da177e4SLinus Torvalds { 31551da177e4SLinus Torvalds struct inet6_dev *idev; 31561da177e4SLinus Torvalds 31571da177e4SLinus Torvalds /* ::1 */ 31581da177e4SLinus Torvalds 31591da177e4SLinus Torvalds ASSERT_RTNL(); 31601da177e4SLinus Torvalds 3161e5d08d71SIan Morris idev = ipv6_find_idev(dev); 3162db0b99f5SSabrina Dubroca if (IS_ERR(idev)) { 316391df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 31641da177e4SLinus Torvalds return; 31651da177e4SLinus Torvalds } 31661da177e4SLinus Torvalds 3167b5f348e5SIlpo Järvinen add_addr(idev, &in6addr_loopback, 128, IFA_HOST); 31681da177e4SLinus Torvalds } 31691da177e4SLinus Torvalds 31702ad3ed59SAlexander Aring void addrconf_add_linklocal(struct inet6_dev *idev, 317164236f3fSHannes Frederic Sowa const struct in6_addr *addr, u32 flags) 31721da177e4SLinus Torvalds { 3173e6464b8cSDavid Ahern struct ifa6_config cfg = { 3174e6464b8cSDavid Ahern .pfx = addr, 3175e6464b8cSDavid Ahern .plen = 64, 3176e6464b8cSDavid Ahern .ifa_flags = flags | IFA_F_PERMANENT, 3177e6464b8cSDavid Ahern .valid_lft = INFINITY_LIFE_TIME, 3178e6464b8cSDavid Ahern .preferred_lft = INFINITY_LIFE_TIME, 3179e6464b8cSDavid Ahern .scope = IFA_LINK 3180e6464b8cSDavid Ahern }; 31811da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 31821da177e4SLinus Torvalds 318395c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 318435e015e1SMatteo Croce if ((dev_net(idev->dev)->ipv6.devconf_all->optimistic_dad || 318535e015e1SMatteo Croce idev->cnf.optimistic_dad) && 3186702beb87SDavid Miller !dev_net(idev->dev)->ipv6.devconf_all->forwarding) 3187e6464b8cSDavid Ahern cfg.ifa_flags |= IFA_F_OPTIMISTIC; 318895c385b4SNeil Horman #endif 318995c385b4SNeil Horman 3190e6464b8cSDavid Ahern ifp = ipv6_add_addr(idev, &cfg, true, NULL); 31911da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 31928308f3ffSDavid Ahern addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 0, idev->dev, 3193acb54e3cSDavid Ahern 0, 0, GFP_ATOMIC); 3194cf22f9a2SDavid S. Miller addrconf_dad_start(ifp); 31951da177e4SLinus Torvalds in6_ifa_put(ifp); 31961da177e4SLinus Torvalds } 31971da177e4SLinus Torvalds } 31982ad3ed59SAlexander Aring EXPORT_SYMBOL_GPL(addrconf_add_linklocal); 31991da177e4SLinus Torvalds 3200622c81d5SHannes Frederic Sowa static bool ipv6_reserved_interfaceid(struct in6_addr address) 3201622c81d5SHannes Frederic Sowa { 3202622c81d5SHannes Frederic Sowa if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0) 3203622c81d5SHannes Frederic Sowa return true; 3204622c81d5SHannes Frederic Sowa 3205622c81d5SHannes Frederic Sowa if (address.s6_addr32[2] == htonl(0x02005eff) && 3206622c81d5SHannes Frederic Sowa ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000))) 3207622c81d5SHannes Frederic Sowa return true; 3208622c81d5SHannes Frederic Sowa 3209622c81d5SHannes Frederic Sowa if (address.s6_addr32[2] == htonl(0xfdffffff) && 3210622c81d5SHannes Frederic Sowa ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80))) 3211622c81d5SHannes Frederic Sowa return true; 3212622c81d5SHannes Frederic Sowa 3213622c81d5SHannes Frederic Sowa return false; 3214622c81d5SHannes Frederic Sowa } 3215622c81d5SHannes Frederic Sowa 3216622c81d5SHannes Frederic Sowa static int ipv6_generate_stable_address(struct in6_addr *address, 3217622c81d5SHannes Frederic Sowa u8 dad_count, 3218622c81d5SHannes Frederic Sowa const struct inet6_dev *idev) 3219622c81d5SHannes Frederic Sowa { 3220622c81d5SHannes Frederic Sowa static DEFINE_SPINLOCK(lock); 3221622c81d5SHannes Frederic Sowa static __u32 digest[SHA_DIGEST_WORDS]; 3222622c81d5SHannes Frederic Sowa static __u32 workspace[SHA_WORKSPACE_WORDS]; 3223622c81d5SHannes Frederic Sowa 3224622c81d5SHannes Frederic Sowa static union { 3225622c81d5SHannes Frederic Sowa char __data[SHA_MESSAGE_BYTES]; 3226622c81d5SHannes Frederic Sowa struct { 3227622c81d5SHannes Frederic Sowa struct in6_addr secret; 3228ff40217eSHannes Frederic Sowa __be32 prefix[2]; 3229622c81d5SHannes Frederic Sowa unsigned char hwaddr[MAX_ADDR_LEN]; 3230622c81d5SHannes Frederic Sowa u8 dad_count; 3231622c81d5SHannes Frederic Sowa } __packed; 3232622c81d5SHannes Frederic Sowa } data; 3233622c81d5SHannes Frederic Sowa 3234622c81d5SHannes Frederic Sowa struct in6_addr secret; 3235622c81d5SHannes Frederic Sowa struct in6_addr temp; 3236622c81d5SHannes Frederic Sowa struct net *net = dev_net(idev->dev); 3237622c81d5SHannes Frederic Sowa 3238622c81d5SHannes Frederic Sowa BUILD_BUG_ON(sizeof(data.__data) != sizeof(data)); 3239622c81d5SHannes Frederic Sowa 3240622c81d5SHannes Frederic Sowa if (idev->cnf.stable_secret.initialized) 3241622c81d5SHannes Frederic Sowa secret = idev->cnf.stable_secret.secret; 3242622c81d5SHannes Frederic Sowa else if (net->ipv6.devconf_dflt->stable_secret.initialized) 3243622c81d5SHannes Frederic Sowa secret = net->ipv6.devconf_dflt->stable_secret.secret; 3244622c81d5SHannes Frederic Sowa else 3245622c81d5SHannes Frederic Sowa return -1; 3246622c81d5SHannes Frederic Sowa 3247622c81d5SHannes Frederic Sowa retry: 3248622c81d5SHannes Frederic Sowa spin_lock_bh(&lock); 3249622c81d5SHannes Frederic Sowa 3250622c81d5SHannes Frederic Sowa sha_init(digest); 3251622c81d5SHannes Frederic Sowa memset(&data, 0, sizeof(data)); 3252622c81d5SHannes Frederic Sowa memset(workspace, 0, sizeof(workspace)); 3253622c81d5SHannes Frederic Sowa memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len); 3254ff40217eSHannes Frederic Sowa data.prefix[0] = address->s6_addr32[0]; 3255ff40217eSHannes Frederic Sowa data.prefix[1] = address->s6_addr32[1]; 3256622c81d5SHannes Frederic Sowa data.secret = secret; 3257622c81d5SHannes Frederic Sowa data.dad_count = dad_count; 3258622c81d5SHannes Frederic Sowa 3259622c81d5SHannes Frederic Sowa sha_transform(digest, data.__data, workspace); 3260622c81d5SHannes Frederic Sowa 3261622c81d5SHannes Frederic Sowa temp = *address; 3262ff40217eSHannes Frederic Sowa temp.s6_addr32[2] = (__force __be32)digest[0]; 3263ff40217eSHannes Frederic Sowa temp.s6_addr32[3] = (__force __be32)digest[1]; 3264622c81d5SHannes Frederic Sowa 3265622c81d5SHannes Frederic Sowa spin_unlock_bh(&lock); 3266622c81d5SHannes Frederic Sowa 3267622c81d5SHannes Frederic Sowa if (ipv6_reserved_interfaceid(temp)) { 3268622c81d5SHannes Frederic Sowa dad_count++; 32691855b7c3SHannes Frederic Sowa if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries) 3270622c81d5SHannes Frederic Sowa return -1; 3271622c81d5SHannes Frederic Sowa goto retry; 3272622c81d5SHannes Frederic Sowa } 3273622c81d5SHannes Frederic Sowa 3274622c81d5SHannes Frederic Sowa *address = temp; 3275622c81d5SHannes Frederic Sowa return 0; 3276622c81d5SHannes Frederic Sowa } 3277622c81d5SHannes Frederic Sowa 3278cc9da6ccSBjørn Mork static void ipv6_gen_mode_random_init(struct inet6_dev *idev) 3279cc9da6ccSBjørn Mork { 3280cc9da6ccSBjørn Mork struct ipv6_stable_secret *s = &idev->cnf.stable_secret; 3281cc9da6ccSBjørn Mork 3282cc9da6ccSBjørn Mork if (s->initialized) 3283cc9da6ccSBjørn Mork return; 3284cc9da6ccSBjørn Mork s = &idev->cnf.stable_secret; 3285cc9da6ccSBjørn Mork get_random_bytes(&s->secret, sizeof(s->secret)); 3286cc9da6ccSBjørn Mork s->initialized = true; 3287cc9da6ccSBjørn Mork } 3288cc9da6ccSBjørn Mork 3289bc91b0f0SJiri Pirko static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route) 3290bc91b0f0SJiri Pirko { 3291bc91b0f0SJiri Pirko struct in6_addr addr; 3292bc91b0f0SJiri Pirko 3293ca254490SDavid Ahern /* no link local addresses on L3 master devices */ 3294ca254490SDavid Ahern if (netif_is_l3_master(idev->dev)) 3295ca254490SDavid Ahern return; 3296ca254490SDavid Ahern 3297bc91b0f0SJiri Pirko ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0); 3298622c81d5SHannes Frederic Sowa 3299d35a00b8SFelix Jia switch (idev->cnf.addr_gen_mode) { 3300cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_RANDOM: 3301cc9da6ccSBjørn Mork ipv6_gen_mode_random_init(idev); 3302cc9da6ccSBjørn Mork /* fallthrough */ 3303cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_STABLE_PRIVACY: 3304622c81d5SHannes Frederic Sowa if (!ipv6_generate_stable_address(&addr, 0, idev)) 330564236f3fSHannes Frederic Sowa addrconf_add_linklocal(idev, &addr, 330664236f3fSHannes Frederic Sowa IFA_F_STABLE_PRIVACY); 3307622c81d5SHannes Frederic Sowa else if (prefix_route) 33088308f3ffSDavid Ahern addrconf_prefix_route(&addr, 64, 0, idev->dev, 3309acb54e3cSDavid Ahern 0, 0, GFP_KERNEL); 3310cc9da6ccSBjørn Mork break; 3311cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_EUI64: 3312bc91b0f0SJiri Pirko /* addrconf_add_linklocal also adds a prefix_route and we 3313bc91b0f0SJiri Pirko * only need to care about prefix routes if ipv6_generate_eui64 3314bc91b0f0SJiri Pirko * couldn't generate one. 3315bc91b0f0SJiri Pirko */ 3316bc91b0f0SJiri Pirko if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0) 331764236f3fSHannes Frederic Sowa addrconf_add_linklocal(idev, &addr, 0); 3318bc91b0f0SJiri Pirko else if (prefix_route) 33198308f3ffSDavid Ahern addrconf_prefix_route(&addr, 64, 0, idev->dev, 332027b10608SDavid Ahern 0, 0, GFP_KERNEL); 3321cc9da6ccSBjørn Mork break; 3322cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_NONE: 3323cc9da6ccSBjørn Mork default: 3324cc9da6ccSBjørn Mork /* will not add any link local address */ 3325cc9da6ccSBjørn Mork break; 3326bc91b0f0SJiri Pirko } 3327bc91b0f0SJiri Pirko } 3328bc91b0f0SJiri Pirko 33291da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev) 33301da177e4SLinus Torvalds { 33311da177e4SLinus Torvalds struct inet6_dev *idev; 33321da177e4SLinus Torvalds 33331da177e4SLinus Torvalds ASSERT_RTNL(); 33341da177e4SLinus Torvalds 333574235a25SHerbert Xu if ((dev->type != ARPHRD_ETHER) && 333674235a25SHerbert Xu (dev->type != ARPHRD_FDDI) && 333774235a25SHerbert Xu (dev->type != ARPHRD_ARCNET) && 333806a4c1c5Salex.bluesman.smirnov@gmail.com (dev->type != ARPHRD_INFINIBAND) && 3339e837735eSNicolas Dichtel (dev->type != ARPHRD_IEEE1394) && 3340e74bccb8SJukka Rissanen (dev->type != ARPHRD_TUNNEL6) && 3341cc9da6ccSBjørn Mork (dev->type != ARPHRD_6LOWPAN) && 334245ce0fd1SFelix Jia (dev->type != ARPHRD_IP6GRE) && 334345ce0fd1SFelix Jia (dev->type != ARPHRD_IPGRE) && 334445ce0fd1SFelix Jia (dev->type != ARPHRD_TUNNEL) && 33459deb441cSSubash Abhinov Kasiviswanathan (dev->type != ARPHRD_NONE) && 33469deb441cSSubash Abhinov Kasiviswanathan (dev->type != ARPHRD_RAWIP)) { 334774235a25SHerbert Xu /* Alas, we support only Ethernet autoconfiguration. */ 334874235a25SHerbert Xu return; 334974235a25SHerbert Xu } 335074235a25SHerbert Xu 33511da177e4SLinus Torvalds idev = addrconf_add_dev(dev); 335264e724f6SBrian Haley if (IS_ERR(idev)) 33531da177e4SLinus Torvalds return; 33541da177e4SLinus Torvalds 3355cc9da6ccSBjørn Mork /* this device type has no EUI support */ 3356cc9da6ccSBjørn Mork if (dev->type == ARPHRD_NONE && 3357d35a00b8SFelix Jia idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64) 3358d35a00b8SFelix Jia idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM; 3359cc9da6ccSBjørn Mork 3360bc91b0f0SJiri Pirko addrconf_addr_gen(idev, false); 33611da177e4SLinus Torvalds } 33621da177e4SLinus Torvalds 336307a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 33641da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev) 33651da177e4SLinus Torvalds { 33661da177e4SLinus Torvalds struct inet6_dev *idev; 33671da177e4SLinus Torvalds 33681da177e4SLinus Torvalds ASSERT_RTNL(); 33691da177e4SLinus Torvalds 33701da177e4SLinus Torvalds /* 33711da177e4SLinus Torvalds * Configure the tunnel with one of our IPv4 33721da177e4SLinus Torvalds * addresses... we should configure all of 33731da177e4SLinus Torvalds * our v4 addrs in the tunnel 33741da177e4SLinus Torvalds */ 33751da177e4SLinus Torvalds 3376e5d08d71SIan Morris idev = ipv6_find_idev(dev); 3377db0b99f5SSabrina Dubroca if (IS_ERR(idev)) { 337891df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 33791da177e4SLinus Torvalds return; 33801da177e4SLinus Torvalds } 33811da177e4SLinus Torvalds 3382c7dc89c0SFred L. Templin if (dev->priv_flags & IFF_ISATAP) { 3383bc91b0f0SJiri Pirko addrconf_addr_gen(idev, false); 3384c7dc89c0SFred L. Templin return; 3385c7dc89c0SFred L. Templin } 3386c7dc89c0SFred L. Templin 33871da177e4SLinus Torvalds sit_add_v4_addrs(idev); 33881da177e4SLinus Torvalds 338962b54dd9SNicolas Dichtel if (dev->flags&IFF_POINTOPOINT) 33901da177e4SLinus Torvalds addrconf_add_mroute(dev); 33911da177e4SLinus Torvalds } 33920be669bbSJoerg Roedel #endif 33931da177e4SLinus Torvalds 339407a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE) 3395aee80b54Sstephen hemminger static void addrconf_gre_config(struct net_device *dev) 3396aee80b54Sstephen hemminger { 3397aee80b54Sstephen hemminger struct inet6_dev *idev; 3398aee80b54Sstephen hemminger 3399aee80b54Sstephen hemminger ASSERT_RTNL(); 3400aee80b54Sstephen hemminger 3401e5d08d71SIan Morris idev = ipv6_find_idev(dev); 3402db0b99f5SSabrina Dubroca if (IS_ERR(idev)) { 340391df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 3404aee80b54Sstephen hemminger return; 3405aee80b54Sstephen hemminger } 3406aee80b54Sstephen hemminger 3407bc91b0f0SJiri Pirko addrconf_addr_gen(idev, true); 3408d9e4ce65SHannes Frederic Sowa if (dev->flags & IFF_POINTOPOINT) 3409d9e4ce65SHannes Frederic Sowa addrconf_add_mroute(dev); 3410aee80b54Sstephen hemminger } 3411aee80b54Sstephen hemminger #endif 3412aee80b54Sstephen hemminger 3413afb1d4b5SDavid Ahern static int fixup_permanent_addr(struct net *net, 3414afb1d4b5SDavid Ahern struct inet6_dev *idev, 3415f1705ec1SDavid Ahern struct inet6_ifaddr *ifp) 3416f1705ec1SDavid Ahern { 341793c2fb25SDavid Ahern /* !fib6_node means the host route was removed from the 34188048ced9SDavid Ahern * FIB, for example, if 'lo' device is taken down. In that 34198048ced9SDavid Ahern * case regenerate the host route. 34208048ced9SDavid Ahern */ 342193c2fb25SDavid Ahern if (!ifp->rt || !ifp->rt->fib6_node) { 3422360a9887SDavid Ahern struct fib6_info *f6i, *prev; 3423f1705ec1SDavid Ahern 3424360a9887SDavid Ahern f6i = addrconf_f6i_alloc(net, idev, &ifp->addr, false, 3425acb54e3cSDavid Ahern GFP_ATOMIC); 3426360a9887SDavid Ahern if (IS_ERR(f6i)) 3427360a9887SDavid Ahern return PTR_ERR(f6i); 3428f1705ec1SDavid Ahern 34298048ced9SDavid Ahern /* ifp->rt can be accessed outside of rtnl */ 34308048ced9SDavid Ahern spin_lock(&ifp->lock); 34318048ced9SDavid Ahern prev = ifp->rt; 3432360a9887SDavid Ahern ifp->rt = f6i; 34338048ced9SDavid Ahern spin_unlock(&ifp->lock); 34348048ced9SDavid Ahern 343593531c67SDavid Ahern fib6_info_release(prev); 3436f1705ec1SDavid Ahern } 3437f1705ec1SDavid Ahern 3438f1705ec1SDavid Ahern if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) { 3439f1705ec1SDavid Ahern addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 34408308f3ffSDavid Ahern ifp->rt_priority, idev->dev, 0, 0, 34418308f3ffSDavid Ahern GFP_ATOMIC); 3442f1705ec1SDavid Ahern } 3443f1705ec1SDavid Ahern 34446d717134SDavid Ahern if (ifp->state == INET6_IFADDR_STATE_PREDAD) 3445f1705ec1SDavid Ahern addrconf_dad_start(ifp); 3446f1705ec1SDavid Ahern 3447f1705ec1SDavid Ahern return 0; 3448f1705ec1SDavid Ahern } 3449f1705ec1SDavid Ahern 3450afb1d4b5SDavid Ahern static void addrconf_permanent_addr(struct net *net, struct net_device *dev) 3451f1705ec1SDavid Ahern { 3452f1705ec1SDavid Ahern struct inet6_ifaddr *ifp, *tmp; 3453f1705ec1SDavid Ahern struct inet6_dev *idev; 3454f1705ec1SDavid Ahern 3455f1705ec1SDavid Ahern idev = __in6_dev_get(dev); 3456f1705ec1SDavid Ahern if (!idev) 3457f1705ec1SDavid Ahern return; 3458f1705ec1SDavid Ahern 3459f1705ec1SDavid Ahern write_lock_bh(&idev->lock); 3460f1705ec1SDavid Ahern 3461f1705ec1SDavid Ahern list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) { 3462f1705ec1SDavid Ahern if ((ifp->flags & IFA_F_PERMANENT) && 3463afb1d4b5SDavid Ahern fixup_permanent_addr(net, idev, ifp) < 0) { 3464f1705ec1SDavid Ahern write_unlock_bh(&idev->lock); 3465e669b869SEric Dumazet in6_ifa_hold(ifp); 3466f1705ec1SDavid Ahern ipv6_del_addr(ifp); 3467f1705ec1SDavid Ahern write_lock_bh(&idev->lock); 3468f1705ec1SDavid Ahern 3469f1705ec1SDavid Ahern net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n", 3470f1705ec1SDavid Ahern idev->dev->name, &ifp->addr); 3471f1705ec1SDavid Ahern } 3472f1705ec1SDavid Ahern } 3473f1705ec1SDavid Ahern 3474f1705ec1SDavid Ahern write_unlock_bh(&idev->lock); 3475f1705ec1SDavid Ahern } 3476f1705ec1SDavid Ahern 34771da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event, 3478351638e7SJiri Pirko void *ptr) 34791da177e4SLinus Torvalds { 3480351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 3481896585d4SHangbin Liu struct netdev_notifier_change_info *change_info; 34824f7f34eaSDavid Ahern struct netdev_notifier_changeupper_info *info; 3483748e2d93SEric Dumazet struct inet6_dev *idev = __in6_dev_get(dev); 348460abc0beSWANG Cong struct net *net = dev_net(dev); 3485c5e33bddSYOSHIFUJI Hideaki int run_pending = 0; 3486b217d616SHerbert Xu int err; 34871da177e4SLinus Torvalds 34881da177e4SLinus Torvalds switch (event) { 348945ba9dd2SYOSHIFUJI Hideaki case NETDEV_REGISTER: 349074235a25SHerbert Xu if (!idev && dev->mtu >= IPV6_MIN_MTU) { 349145ba9dd2SYOSHIFUJI Hideaki idev = ipv6_add_dev(dev); 3492a317a2f1SWANG Cong if (IS_ERR(idev)) 3493a317a2f1SWANG Cong return notifier_from_errno(PTR_ERR(idev)); 349445ba9dd2SYOSHIFUJI Hideaki } 349545ba9dd2SYOSHIFUJI Hideaki break; 3496bcdd553fSStephen Hemminger 3497b7b0b1d2SAlexander Duyck case NETDEV_CHANGEMTU: 3498b7b0b1d2SAlexander Duyck /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */ 3499b7b0b1d2SAlexander Duyck if (dev->mtu < IPV6_MIN_MTU) { 350060abc0beSWANG Cong addrconf_ifdown(dev, dev != net->loopback_dev); 3501b7b0b1d2SAlexander Duyck break; 3502b7b0b1d2SAlexander Duyck } 3503b7b0b1d2SAlexander Duyck 3504b7b0b1d2SAlexander Duyck if (idev) { 3505b7b0b1d2SAlexander Duyck rt6_mtu_change(dev, dev->mtu); 3506b7b0b1d2SAlexander Duyck idev->cnf.mtu6 = dev->mtu; 3507b7b0b1d2SAlexander Duyck break; 3508b7b0b1d2SAlexander Duyck } 3509b7b0b1d2SAlexander Duyck 3510b7b0b1d2SAlexander Duyck /* allocate new idev */ 3511b7b0b1d2SAlexander Duyck idev = ipv6_add_dev(dev); 3512b7b0b1d2SAlexander Duyck if (IS_ERR(idev)) 3513b7b0b1d2SAlexander Duyck break; 3514b7b0b1d2SAlexander Duyck 3515b7b0b1d2SAlexander Duyck /* device is still not ready */ 3516b7b0b1d2SAlexander Duyck if (!(idev->if_flags & IF_READY)) 3517b7b0b1d2SAlexander Duyck break; 3518b7b0b1d2SAlexander Duyck 3519b7b0b1d2SAlexander Duyck run_pending = 1; 3520b7b0b1d2SAlexander Duyck 3521b7b0b1d2SAlexander Duyck /* fall through */ 3522b7b0b1d2SAlexander Duyck 35231da177e4SLinus Torvalds case NETDEV_UP: 35243c21edbdSYOSHIFUJI Hideaki case NETDEV_CHANGE: 3525c2edacf8SJay Vosburgh if (dev->flags & IFF_SLAVE) 3526c2edacf8SJay Vosburgh break; 3527c2edacf8SJay Vosburgh 35283ce62a84SWANG Cong if (idev && idev->cnf.disable_ipv6) 35293ce62a84SWANG Cong break; 35303ce62a84SWANG Cong 35313c21edbdSYOSHIFUJI Hideaki if (event == NETDEV_UP) { 353238bd10c4SDavid Ahern /* restore routes for permanent addresses */ 3533afb1d4b5SDavid Ahern addrconf_permanent_addr(net, dev); 353438bd10c4SDavid Ahern 35351f372c7bSMike Manning if (!addrconf_link_ready(dev)) { 35363c21edbdSYOSHIFUJI Hideaki /* device is not ready yet. */ 35377c62b8ddSLubomir Rintel pr_debug("ADDRCONF(NETDEV_UP): %s: link is not ready\n", 35383c21edbdSYOSHIFUJI Hideaki dev->name); 35393c21edbdSYOSHIFUJI Hideaki break; 35403c21edbdSYOSHIFUJI Hideaki } 354199081049SKristian Slavov 3542d31c7b8fSEvgeniy Polyakov if (!idev && dev->mtu >= IPV6_MIN_MTU) 3543d31c7b8fSEvgeniy Polyakov idev = ipv6_add_dev(dev); 3544d31c7b8fSEvgeniy Polyakov 3545a317a2f1SWANG Cong if (!IS_ERR_OR_NULL(idev)) { 354699081049SKristian Slavov idev->if_flags |= IF_READY; 3547e3ec6cfcSBenjamin Thery run_pending = 1; 3548e3ec6cfcSBenjamin Thery } 3549b7b0b1d2SAlexander Duyck } else if (event == NETDEV_CHANGE) { 35501f372c7bSMike Manning if (!addrconf_link_ready(dev)) { 35513c21edbdSYOSHIFUJI Hideaki /* device is still not ready. */ 355227c6fa73SIdo Schimmel rt6_sync_down_dev(dev, event); 35533c21edbdSYOSHIFUJI Hideaki break; 35543c21edbdSYOSHIFUJI Hideaki } 35553c21edbdSYOSHIFUJI Hideaki 3556896585d4SHangbin Liu if (!IS_ERR_OR_NULL(idev)) { 3557a088d1d7SLinus Lüssing if (idev->if_flags & IF_READY) { 3558a088d1d7SLinus Lüssing /* device is already configured - 3559a088d1d7SLinus Lüssing * but resend MLD reports, we might 3560a088d1d7SLinus Lüssing * have roamed and need to update 3561a088d1d7SLinus Lüssing * multicast snooping switches 3562a088d1d7SLinus Lüssing */ 3563a088d1d7SLinus Lüssing ipv6_mc_up(idev); 3564896585d4SHangbin Liu change_info = ptr; 3565896585d4SHangbin Liu if (change_info->flags_changed & IFF_NOARP) 3566896585d4SHangbin Liu addrconf_dad_run(idev, true); 356727c6fa73SIdo Schimmel rt6_sync_up(dev, RTNH_F_LINKDOWN); 35683c21edbdSYOSHIFUJI Hideaki break; 3569a088d1d7SLinus Lüssing } 35703c21edbdSYOSHIFUJI Hideaki idev->if_flags |= IF_READY; 35713c21edbdSYOSHIFUJI Hideaki } 35723c21edbdSYOSHIFUJI Hideaki 3573f3213831SJoe Perches pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n", 35743c21edbdSYOSHIFUJI Hideaki dev->name); 35753c21edbdSYOSHIFUJI Hideaki 3576c5e33bddSYOSHIFUJI Hideaki run_pending = 1; 35773c21edbdSYOSHIFUJI Hideaki } 35783c21edbdSYOSHIFUJI Hideaki 35791da177e4SLinus Torvalds switch (dev->type) { 358007a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 35811da177e4SLinus Torvalds case ARPHRD_SIT: 35821da177e4SLinus Torvalds addrconf_sit_config(dev); 35831da177e4SLinus Torvalds break; 35840be669bbSJoerg Roedel #endif 358507a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE) 3586aee80b54Sstephen hemminger case ARPHRD_IPGRE: 3587aee80b54Sstephen hemminger addrconf_gre_config(dev); 3588aee80b54Sstephen hemminger break; 3589aee80b54Sstephen hemminger #endif 35901da177e4SLinus Torvalds case ARPHRD_LOOPBACK: 35911da177e4SLinus Torvalds init_loopback(dev); 35921da177e4SLinus Torvalds break; 35931da177e4SLinus Torvalds 35941da177e4SLinus Torvalds default: 35951da177e4SLinus Torvalds addrconf_dev_config(dev); 35961da177e4SLinus Torvalds break; 35973ff50b79SStephen Hemminger } 3598bcdd553fSStephen Hemminger 3599a317a2f1SWANG Cong if (!IS_ERR_OR_NULL(idev)) { 3600c5e33bddSYOSHIFUJI Hideaki if (run_pending) 3601896585d4SHangbin Liu addrconf_dad_run(idev, false); 3602c5e33bddSYOSHIFUJI Hideaki 36032127d95aSIdo Schimmel /* Device has an address by now */ 36042127d95aSIdo Schimmel rt6_sync_up(dev, RTNH_F_DEAD); 36052127d95aSIdo Schimmel 3606bcdd553fSStephen Hemminger /* 3607bcdd553fSStephen Hemminger * If the MTU changed during the interface down, 3608bcdd553fSStephen Hemminger * when the interface up, the changed MTU must be 3609bcdd553fSStephen Hemminger * reflected in the idev as well as routers. 36101da177e4SLinus Torvalds */ 3611bcdd553fSStephen Hemminger if (idev->cnf.mtu6 != dev->mtu && 3612bcdd553fSStephen Hemminger dev->mtu >= IPV6_MIN_MTU) { 36131da177e4SLinus Torvalds rt6_mtu_change(dev, dev->mtu); 36141da177e4SLinus Torvalds idev->cnf.mtu6 = dev->mtu; 36151da177e4SLinus Torvalds } 36161da177e4SLinus Torvalds idev->tstamp = jiffies; 36171da177e4SLinus Torvalds inet6_ifinfo_notify(RTM_NEWLINK, idev); 3618bcdd553fSStephen Hemminger 3619bcdd553fSStephen Hemminger /* 3620bcdd553fSStephen Hemminger * If the changed mtu during down is lower than 3621bcdd553fSStephen Hemminger * IPV6_MIN_MTU stop IPv6 on this interface. 36221da177e4SLinus Torvalds */ 36231da177e4SLinus Torvalds if (dev->mtu < IPV6_MIN_MTU) 362460abc0beSWANG Cong addrconf_ifdown(dev, dev != net->loopback_dev); 36251da177e4SLinus Torvalds } 36261da177e4SLinus Torvalds break; 36271da177e4SLinus Torvalds 36281da177e4SLinus Torvalds case NETDEV_DOWN: 36291da177e4SLinus Torvalds case NETDEV_UNREGISTER: 36301da177e4SLinus Torvalds /* 36311da177e4SLinus Torvalds * Remove all addresses from this interface. 36321da177e4SLinus Torvalds */ 36331da177e4SLinus Torvalds addrconf_ifdown(dev, event != NETDEV_DOWN); 36341da177e4SLinus Torvalds break; 36353c21edbdSYOSHIFUJI Hideaki 36361da177e4SLinus Torvalds case NETDEV_CHANGENAME: 36371da177e4SLinus Torvalds if (idev) { 36385632c515SStephen Hemminger snmp6_unregister_dev(idev); 3639408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 3640a317a2f1SWANG Cong err = addrconf_sysctl_register(idev); 3641b217d616SHerbert Xu if (err) 3642b217d616SHerbert Xu return notifier_from_errno(err); 3643a317a2f1SWANG Cong err = snmp6_register_dev(idev); 3644a317a2f1SWANG Cong if (err) { 3645a317a2f1SWANG Cong addrconf_sysctl_unregister(idev); 3646a317a2f1SWANG Cong return notifier_from_errno(err); 3647a317a2f1SWANG Cong } 36485632c515SStephen Hemminger } 36491da177e4SLinus Torvalds break; 3650bcdd553fSStephen Hemminger 365193d9b7d7SJiri Pirko case NETDEV_PRE_TYPE_CHANGE: 365293d9b7d7SJiri Pirko case NETDEV_POST_TYPE_CHANGE: 36533ef0952cSAndrew Lunn if (idev) 365493d9b7d7SJiri Pirko addrconf_type_change(dev, event); 365575c78500SMoni Shoua break; 36564f7f34eaSDavid Ahern 36574f7f34eaSDavid Ahern case NETDEV_CHANGEUPPER: 36584f7f34eaSDavid Ahern info = ptr; 36594f7f34eaSDavid Ahern 36604f7f34eaSDavid Ahern /* flush all routes if dev is linked to or unlinked from 36614f7f34eaSDavid Ahern * an L3 master device (e.g., VRF) 36624f7f34eaSDavid Ahern */ 36634f7f34eaSDavid Ahern if (info->upper_dev && netif_is_l3_master(info->upper_dev)) 36644f7f34eaSDavid Ahern addrconf_ifdown(dev, 0); 36653ff50b79SStephen Hemminger } 36661da177e4SLinus Torvalds 36671da177e4SLinus Torvalds return NOTIFY_OK; 36681da177e4SLinus Torvalds } 36691da177e4SLinus Torvalds 36701da177e4SLinus Torvalds /* 36711da177e4SLinus Torvalds * addrconf module should be notified of a device going up 36721da177e4SLinus Torvalds */ 36731da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = { 36741da177e4SLinus Torvalds .notifier_call = addrconf_notify, 3675242d3a49SWANG Cong .priority = ADDRCONF_NOTIFY_PRIORITY, 36761da177e4SLinus Torvalds }; 36771da177e4SLinus Torvalds 367893d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, unsigned long event) 367975c78500SMoni Shoua { 368075c78500SMoni Shoua struct inet6_dev *idev; 368175c78500SMoni Shoua ASSERT_RTNL(); 368275c78500SMoni Shoua 368375c78500SMoni Shoua idev = __in6_dev_get(dev); 368475c78500SMoni Shoua 368593d9b7d7SJiri Pirko if (event == NETDEV_POST_TYPE_CHANGE) 368675c78500SMoni Shoua ipv6_mc_remap(idev); 368793d9b7d7SJiri Pirko else if (event == NETDEV_PRE_TYPE_CHANGE) 368875c78500SMoni Shoua ipv6_mc_unmap(idev); 368975c78500SMoni Shoua } 369075c78500SMoni Shoua 369170af921dSDavid Ahern static bool addr_is_local(const struct in6_addr *addr) 369270af921dSDavid Ahern { 369370af921dSDavid Ahern return ipv6_addr_type(addr) & 369470af921dSDavid Ahern (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK); 369570af921dSDavid Ahern } 369670af921dSDavid Ahern 36971da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how) 36981da177e4SLinus Torvalds { 36994c981e28SIdo Schimmel unsigned long event = how ? NETDEV_UNREGISTER : NETDEV_DOWN; 3700c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 3701502a2ffdSstephen hemminger struct inet6_dev *idev; 3702f1705ec1SDavid Ahern struct inet6_ifaddr *ifa, *tmp; 37030aef78aaSIvan Vecera bool keep_addr = false; 370473a8bd74SDavid S. Miller int state, i; 37051da177e4SLinus Torvalds 37061da177e4SLinus Torvalds ASSERT_RTNL(); 37071da177e4SLinus Torvalds 37084c981e28SIdo Schimmel rt6_disable_ip(dev, event); 37091da177e4SLinus Torvalds 37101da177e4SLinus Torvalds idev = __in6_dev_get(dev); 371163159f29SIan Morris if (!idev) 37121da177e4SLinus Torvalds return -ENODEV; 37131da177e4SLinus Torvalds 3714bcdd553fSStephen Hemminger /* 3715bcdd553fSStephen Hemminger * Step 1: remove reference to ipv6 device from parent device. 3716bcdd553fSStephen Hemminger * Do not dev_put! 37171da177e4SLinus Torvalds */ 3718439e2385SDenis V. Lunev if (how) { 37191da177e4SLinus Torvalds idev->dead = 1; 37208814c4b5SYOSHIFUJI Hideaki 37218814c4b5SYOSHIFUJI Hideaki /* protected by rtnl_lock */ 3722a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(dev->ip6_ptr, NULL); 37231da177e4SLinus Torvalds 37241da177e4SLinus Torvalds /* Step 1.5: remove snmp6 entry */ 37251da177e4SLinus Torvalds snmp6_unregister_dev(idev); 37261da177e4SLinus Torvalds 37271da177e4SLinus Torvalds } 37281da177e4SLinus Torvalds 3729f1705ec1SDavid Ahern /* combine the user config with event to determine if permanent 3730f1705ec1SDavid Ahern * addresses are to be removed from address hash table 3731f1705ec1SDavid Ahern */ 37320aef78aaSIvan Vecera if (!how && !idev->cnf.disable_ipv6) { 37330aef78aaSIvan Vecera /* aggregate the system setting and interface setting */ 37340aef78aaSIvan Vecera int _keep_addr = net->ipv6.devconf_all->keep_addr_on_down; 37350aef78aaSIvan Vecera 37360aef78aaSIvan Vecera if (!_keep_addr) 37370aef78aaSIvan Vecera _keep_addr = idev->cnf.keep_addr_on_down; 37380aef78aaSIvan Vecera 37390aef78aaSIvan Vecera keep_addr = (_keep_addr > 0); 37400aef78aaSIvan Vecera } 3741f1705ec1SDavid Ahern 374273a8bd74SDavid S. Miller /* Step 2: clear hash table */ 374373a8bd74SDavid S. Miller for (i = 0; i < IN6_ADDR_HSIZE; i++) { 374473a8bd74SDavid S. Miller struct hlist_head *h = &inet6_addr_lst[i]; 374573a8bd74SDavid S. Miller 374673a8bd74SDavid S. Miller spin_lock_bh(&addrconf_hash_lock); 374773a8bd74SDavid S. Miller restart: 3748b67bfe0dSSasha Levin hlist_for_each_entry_rcu(ifa, h, addr_lst) { 374973a8bd74SDavid S. Miller if (ifa->idev == idev) { 3750c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifa); 3751f1705ec1SDavid Ahern /* combined flag + permanent flag decide if 3752f1705ec1SDavid Ahern * address is retained on a down event 3753f1705ec1SDavid Ahern */ 3754f1705ec1SDavid Ahern if (!keep_addr || 375570af921dSDavid Ahern !(ifa->flags & IFA_F_PERMANENT) || 375670af921dSDavid Ahern addr_is_local(&ifa->addr)) { 3757f1705ec1SDavid Ahern hlist_del_init_rcu(&ifa->addr_lst); 375873a8bd74SDavid S. Miller goto restart; 375973a8bd74SDavid S. Miller } 376073a8bd74SDavid S. Miller } 3761f1705ec1SDavid Ahern } 376273a8bd74SDavid S. Miller spin_unlock_bh(&addrconf_hash_lock); 376373a8bd74SDavid S. Miller } 376473a8bd74SDavid S. Miller 37651da177e4SLinus Torvalds write_lock_bh(&idev->lock); 37661da177e4SLinus Torvalds 3767b7b1bfceSHannes Frederic Sowa addrconf_del_rs_timer(idev); 3768b7b1bfceSHannes Frederic Sowa 3769bcdd553fSStephen Hemminger /* Step 2: clear flags for stateless addrconf */ 3770439e2385SDenis V. Lunev if (!how) 37713c21edbdSYOSHIFUJI Hideaki idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY); 37721da177e4SLinus Torvalds 3773bcdd553fSStephen Hemminger /* Step 3: clear tempaddr list */ 3774372e6c8fSstephen hemminger while (!list_empty(&idev->tempaddr_list)) { 3775372e6c8fSstephen hemminger ifa = list_first_entry(&idev->tempaddr_list, 3776372e6c8fSstephen hemminger struct inet6_ifaddr, tmp_list); 3777372e6c8fSstephen hemminger list_del(&ifa->tmp_list); 37781da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 37791da177e4SLinus Torvalds spin_lock_bh(&ifa->lock); 37801da177e4SLinus Torvalds 37811da177e4SLinus Torvalds if (ifa->ifpub) { 37821da177e4SLinus Torvalds in6_ifa_put(ifa->ifpub); 37831da177e4SLinus Torvalds ifa->ifpub = NULL; 37841da177e4SLinus Torvalds } 37851da177e4SLinus Torvalds spin_unlock_bh(&ifa->lock); 37861da177e4SLinus Torvalds in6_ifa_put(ifa); 37871da177e4SLinus Torvalds write_lock_bh(&idev->lock); 37881da177e4SLinus Torvalds } 37898f37ada5Sstephen hemminger 3790f1705ec1SDavid Ahern list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) { 37918d1c802bSDavid Ahern struct fib6_info *rt = NULL; 3792a2d6cbb0SRabin Vincent bool keep; 379338bd10c4SDavid Ahern 3794c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifa); 379584e8b803Sstephen hemminger 3796a2d6cbb0SRabin Vincent keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) && 3797a2d6cbb0SRabin Vincent !addr_is_local(&ifa->addr); 3798a2d6cbb0SRabin Vincent 37994c5ff6a6SHerbert Xu write_unlock_bh(&idev->lock); 38008e8e676dSHannes Frederic Sowa spin_lock_bh(&ifa->lock); 3801f1705ec1SDavid Ahern 3802a2d6cbb0SRabin Vincent if (keep) { 3803f1705ec1SDavid Ahern /* set state to skip the notifier below */ 3804f1705ec1SDavid Ahern state = INET6_IFADDR_STATE_DEAD; 38056d717134SDavid Ahern ifa->state = INET6_IFADDR_STATE_PREDAD; 3806f1705ec1SDavid Ahern if (!(ifa->flags & IFA_F_NODAD)) 3807f1705ec1SDavid Ahern ifa->flags |= IFA_F_TENTATIVE; 380838bd10c4SDavid Ahern 380938bd10c4SDavid Ahern rt = ifa->rt; 381038bd10c4SDavid Ahern ifa->rt = NULL; 3811f1705ec1SDavid Ahern } else { 38124c5ff6a6SHerbert Xu state = ifa->state; 38134c5ff6a6SHerbert Xu ifa->state = INET6_IFADDR_STATE_DEAD; 3814f1705ec1SDavid Ahern } 3815f1705ec1SDavid Ahern 38168e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifa->lock); 38174c5ff6a6SHerbert Xu 381838bd10c4SDavid Ahern if (rt) 3819afb1d4b5SDavid Ahern ip6_del_rt(net, rt); 382038bd10c4SDavid Ahern 38219d82ca98SJohn Fastabend if (state != INET6_IFADDR_STATE_DEAD) { 38221da177e4SLinus Torvalds __ipv6_ifa_notify(RTM_DELADDR, ifa); 3823f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_DOWN, ifa); 3824ea06f717SMike Manning } else { 3825ea06f717SMike Manning if (idev->cnf.forwarding) 3826ea06f717SMike Manning addrconf_leave_anycast(ifa); 3827ea06f717SMike Manning addrconf_leave_solict(ifa->idev, &ifa->addr); 38282de79570SLorenzo Colitti } 382973a8bd74SDavid S. Miller 38301da177e4SLinus Torvalds write_lock_bh(&idev->lock); 38318ef802aaSEric Dumazet if (!keep) { 38328ef802aaSEric Dumazet list_del_rcu(&ifa->if_list); 38338ef802aaSEric Dumazet in6_ifa_put(ifa); 38348ef802aaSEric Dumazet } 38351da177e4SLinus Torvalds } 38368f37ada5Sstephen hemminger 38371da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 38381da177e4SLinus Torvalds 3839381f4dcaSSabrina Dubroca /* Step 5: Discard anycast and multicast list */ 3840381f4dcaSSabrina Dubroca if (how) { 3841381f4dcaSSabrina Dubroca ipv6_ac_destroy_dev(idev); 38421da177e4SLinus Torvalds ipv6_mc_destroy_dev(idev); 3843381f4dcaSSabrina Dubroca } else { 38441da177e4SLinus Torvalds ipv6_mc_down(idev); 3845381f4dcaSSabrina Dubroca } 38461da177e4SLinus Torvalds 38471da177e4SLinus Torvalds idev->tstamp = jiffies; 38481da177e4SLinus Torvalds 3849bcdd553fSStephen Hemminger /* Last: Shot the device (if unregistered) */ 3850439e2385SDenis V. Lunev if (how) { 3851408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 38521da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, idev->nd_parms); 38531da177e4SLinus Torvalds neigh_ifdown(&nd_tbl, dev); 38541da177e4SLinus Torvalds in6_dev_put(idev); 38551da177e4SLinus Torvalds } 38561da177e4SLinus Torvalds return 0; 38571da177e4SLinus Torvalds } 38581da177e4SLinus Torvalds 3859e99e88a9SKees Cook static void addrconf_rs_timer(struct timer_list *t) 38601da177e4SLinus Torvalds { 3861e99e88a9SKees Cook struct inet6_dev *idev = from_timer(idev, t, rs_timer); 3862caf92bc4SCong Wang struct net_device *dev = idev->dev; 3863b7b1bfceSHannes Frederic Sowa struct in6_addr lladdr; 38641da177e4SLinus Torvalds 3865b7b1bfceSHannes Frederic Sowa write_lock(&idev->lock); 38665b2a1953Sstephen hemminger if (idev->dead || !(idev->if_flags & IF_READY)) 38671da177e4SLinus Torvalds goto out; 38681da177e4SLinus Torvalds 38699ba2add3SShmulik Ladkani if (!ipv6_accept_ra(idev)) 38701da177e4SLinus Torvalds goto out; 38715b2a1953Sstephen hemminger 38725b2a1953Sstephen hemminger /* Announcement received after solicitation was sent */ 38735b2a1953Sstephen hemminger if (idev->if_flags & IF_RA_RCVD) 38745b2a1953Sstephen hemminger goto out; 38751da177e4SLinus Torvalds 3876bd11f074SMaciej Żenczykowski if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) { 3877caf92bc4SCong Wang write_unlock(&idev->lock); 3878caf92bc4SCong Wang if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE)) 3879caf92bc4SCong Wang ndisc_send_rs(dev, &lladdr, 3880b7b1bfceSHannes Frederic Sowa &in6addr_linklocal_allrouters); 3881b7b1bfceSHannes Frederic Sowa else 3882caf92bc4SCong Wang goto put; 3883b7b1bfceSHannes Frederic Sowa 3884caf92bc4SCong Wang write_lock(&idev->lock); 3885bd11f074SMaciej Żenczykowski idev->rs_interval = rfc3315_s14_backoff_update( 3886bd11f074SMaciej Żenczykowski idev->rs_interval, idev->cnf.rtr_solicit_max_interval); 38871da177e4SLinus Torvalds /* The wait after the last probe can be shorter */ 3888b7b1bfceSHannes Frederic Sowa addrconf_mod_rs_timer(idev, (idev->rs_probes == 3889b7b1bfceSHannes Frederic Sowa idev->cnf.rtr_solicits) ? 38905b2a1953Sstephen hemminger idev->cnf.rtr_solicit_delay : 3891bd11f074SMaciej Żenczykowski idev->rs_interval); 38921da177e4SLinus Torvalds } else { 38931da177e4SLinus Torvalds /* 38941da177e4SLinus Torvalds * Note: we do not support deprecated "all on-link" 38951da177e4SLinus Torvalds * assumption any longer. 38961da177e4SLinus Torvalds */ 389791df42beSJoe Perches pr_debug("%s: no IPv6 routers present\n", idev->dev->name); 38981da177e4SLinus Torvalds } 38991da177e4SLinus Torvalds 39001da177e4SLinus Torvalds out: 3901b7b1bfceSHannes Frederic Sowa write_unlock(&idev->lock); 3902caf92bc4SCong Wang put: 3903b7b1bfceSHannes Frederic Sowa in6_dev_put(idev); 39041da177e4SLinus Torvalds } 39051da177e4SLinus Torvalds 39061da177e4SLinus Torvalds /* 39071da177e4SLinus Torvalds * Duplicate Address Detection 39081da177e4SLinus Torvalds */ 39093c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp) 39103c21edbdSYOSHIFUJI Hideaki { 39113c21edbdSYOSHIFUJI Hideaki unsigned long rand_num; 39123c21edbdSYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev; 3913adc176c5SErik Nordmark u64 nonce; 39143c21edbdSYOSHIFUJI Hideaki 391595c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 391695c385b4SNeil Horman rand_num = 0; 391795c385b4SNeil Horman else 391863862b5bSAruna-Hewapathirane rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1); 391995c385b4SNeil Horman 3920adc176c5SErik Nordmark nonce = 0; 3921adc176c5SErik Nordmark if (idev->cnf.enhanced_dad || 3922adc176c5SErik Nordmark dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad) { 3923adc176c5SErik Nordmark do 3924adc176c5SErik Nordmark get_random_bytes(&nonce, 6); 3925adc176c5SErik Nordmark while (nonce == 0); 3926adc176c5SErik Nordmark } 3927adc176c5SErik Nordmark ifp->dad_nonce = nonce; 3928b7b1bfceSHannes Frederic Sowa ifp->dad_probes = idev->cnf.dad_transmits; 3929c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, rand_num); 39303c21edbdSYOSHIFUJI Hideaki } 39313c21edbdSYOSHIFUJI Hideaki 3932c15b1ccaSHannes Frederic Sowa static void addrconf_dad_begin(struct inet6_ifaddr *ifp) 39331da177e4SLinus Torvalds { 39341da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 39351da177e4SLinus Torvalds struct net_device *dev = idev->dev; 3936764d3be6SPaolo Abeni bool bump_id, notify = false; 3937afb1d4b5SDavid Ahern struct net *net; 39381da177e4SLinus Torvalds 39391da177e4SLinus Torvalds addrconf_join_solict(dev, &ifp->addr); 39401da177e4SLinus Torvalds 394163862b5bSAruna-Hewapathirane prandom_seed((__force u32) ifp->addr.s6_addr32[3]); 39421da177e4SLinus Torvalds 39431da177e4SLinus Torvalds read_lock_bh(&idev->lock); 3944622ccdf1SHerbert Xu spin_lock(&ifp->lock); 3945e9d3e084SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DEAD) 39461da177e4SLinus Torvalds goto out; 39471da177e4SLinus Torvalds 3948afb1d4b5SDavid Ahern net = dev_net(dev); 39491da177e4SLinus Torvalds if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) || 3950afb1d4b5SDavid Ahern (net->ipv6.devconf_all->accept_dad < 1 && 3951a2d3f3e3SMatteo Croce idev->cnf.accept_dad < 1) || 395255ebaef1SNoriaki TAKAMIYA !(ifp->flags&IFA_F_TENTATIVE) || 395355ebaef1SNoriaki TAKAMIYA ifp->flags & IFA_F_NODAD) { 3954c76fe2d9SDavid Ahern bool send_na = false; 3955c76fe2d9SDavid Ahern 3956c76fe2d9SDavid Ahern if (ifp->flags & IFA_F_TENTATIVE && 3957c76fe2d9SDavid Ahern !(ifp->flags & IFA_F_OPTIMISTIC)) 3958c76fe2d9SDavid Ahern send_na = true; 3959764d3be6SPaolo Abeni bump_id = ifp->flags & IFA_F_TENTATIVE; 3960cc411d0bSBrian Haley ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 396121809fafSstephen hemminger spin_unlock(&ifp->lock); 39621da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 39631da177e4SLinus Torvalds 3964c76fe2d9SDavid Ahern addrconf_dad_completed(ifp, bump_id, send_na); 39651da177e4SLinus Torvalds return; 39661da177e4SLinus Torvalds } 39671da177e4SLinus Torvalds 39686732badeSYOSHIFUJI Hideaki if (!(idev->if_flags & IF_READY)) { 396921809fafSstephen hemminger spin_unlock(&ifp->lock); 39706732badeSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 39713c21edbdSYOSHIFUJI Hideaki /* 3972590a9887SMasatake YAMATO * If the device is not ready: 39733c21edbdSYOSHIFUJI Hideaki * - keep it tentative if it is a permanent address. 39743c21edbdSYOSHIFUJI Hideaki * - otherwise, kill it. 39753c21edbdSYOSHIFUJI Hideaki */ 39763c21edbdSYOSHIFUJI Hideaki in6_ifa_hold(ifp); 3977cc411d0bSBrian Haley addrconf_dad_stop(ifp, 0); 39786732badeSYOSHIFUJI Hideaki return; 39793c21edbdSYOSHIFUJI Hideaki } 398095c385b4SNeil Horman 398195c385b4SNeil Horman /* 398295c385b4SNeil Horman * Optimistic nodes can start receiving 398395c385b4SNeil Horman * Frames right away 398495c385b4SNeil Horman */ 39857fd2561eSErik Kline if (ifp->flags & IFA_F_OPTIMISTIC) { 3986afb1d4b5SDavid Ahern ip6_ins_rt(net, ifp->rt); 3987afb1d4b5SDavid Ahern if (ipv6_use_optimistic_addr(net, idev)) { 39887fd2561eSErik Kline /* Because optimistic nodes can use this address, 39897fd2561eSErik Kline * notify listeners. If DAD fails, RTM_DELADDR is sent. 39907fd2561eSErik Kline */ 399116186a82Ssubashab@codeaurora.org notify = true; 39927fd2561eSErik Kline } 39937fd2561eSErik Kline } 399495c385b4SNeil Horman 39956732badeSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 39961da177e4SLinus Torvalds out: 3997622ccdf1SHerbert Xu spin_unlock(&ifp->lock); 39981da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 399916186a82Ssubashab@codeaurora.org if (notify) 400016186a82Ssubashab@codeaurora.org ipv6_ifa_notify(RTM_NEWADDR, ifp); 40011da177e4SLinus Torvalds } 40021da177e4SLinus Torvalds 4003c15b1ccaSHannes Frederic Sowa static void addrconf_dad_start(struct inet6_ifaddr *ifp) 40041da177e4SLinus Torvalds { 4005c15b1ccaSHannes Frederic Sowa bool begin_dad = false; 4006c15b1ccaSHannes Frederic Sowa 40078e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 4008c15b1ccaSHannes Frederic Sowa if (ifp->state != INET6_IFADDR_STATE_DEAD) { 4009c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_PREDAD; 4010c15b1ccaSHannes Frederic Sowa begin_dad = true; 4011c15b1ccaSHannes Frederic Sowa } 40128e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 4013c15b1ccaSHannes Frederic Sowa 4014c15b1ccaSHannes Frederic Sowa if (begin_dad) 4015c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 0); 4016c15b1ccaSHannes Frederic Sowa } 4017c15b1ccaSHannes Frederic Sowa 4018c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w) 4019c15b1ccaSHannes Frederic Sowa { 4020c15b1ccaSHannes Frederic Sowa struct inet6_ifaddr *ifp = container_of(to_delayed_work(w), 4021c15b1ccaSHannes Frederic Sowa struct inet6_ifaddr, 4022c15b1ccaSHannes Frederic Sowa dad_work); 40231da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 4024764d3be6SPaolo Abeni bool bump_id, disable_ipv6 = false; 40251da177e4SLinus Torvalds struct in6_addr mcaddr; 40261da177e4SLinus Torvalds 4027c15b1ccaSHannes Frederic Sowa enum { 4028c15b1ccaSHannes Frederic Sowa DAD_PROCESS, 4029c15b1ccaSHannes Frederic Sowa DAD_BEGIN, 4030c15b1ccaSHannes Frederic Sowa DAD_ABORT, 4031c15b1ccaSHannes Frederic Sowa } action = DAD_PROCESS; 4032c15b1ccaSHannes Frederic Sowa 4033c15b1ccaSHannes Frederic Sowa rtnl_lock(); 4034c15b1ccaSHannes Frederic Sowa 40358e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 4036c15b1ccaSHannes Frederic Sowa if (ifp->state == INET6_IFADDR_STATE_PREDAD) { 4037c15b1ccaSHannes Frederic Sowa action = DAD_BEGIN; 4038c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_DAD; 4039c15b1ccaSHannes Frederic Sowa } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) { 4040c15b1ccaSHannes Frederic Sowa action = DAD_ABORT; 4041c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_POSTDAD; 404285b51b12SMike Manning 404335e015e1SMatteo Croce if ((dev_net(idev->dev)->ipv6.devconf_all->accept_dad > 1 || 404435e015e1SMatteo Croce idev->cnf.accept_dad > 1) && 404535e015e1SMatteo Croce !idev->cnf.disable_ipv6 && 404685b51b12SMike Manning !(ifp->flags & IFA_F_STABLE_PRIVACY)) { 404785b51b12SMike Manning struct in6_addr addr; 404885b51b12SMike Manning 404985b51b12SMike Manning addr.s6_addr32[0] = htonl(0xfe800000); 405085b51b12SMike Manning addr.s6_addr32[1] = 0; 405185b51b12SMike Manning 405285b51b12SMike Manning if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) && 405385b51b12SMike Manning ipv6_addr_equal(&ifp->addr, &addr)) { 405485b51b12SMike Manning /* DAD failed for link-local based on MAC */ 405585b51b12SMike Manning idev->cnf.disable_ipv6 = 1; 405685b51b12SMike Manning 405785b51b12SMike Manning pr_info("%s: IPv6 being disabled!\n", 405885b51b12SMike Manning ifp->idev->dev->name); 405985b51b12SMike Manning disable_ipv6 = true; 406085b51b12SMike Manning } 406185b51b12SMike Manning } 4062c15b1ccaSHannes Frederic Sowa } 40638e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 4064c15b1ccaSHannes Frederic Sowa 4065c15b1ccaSHannes Frederic Sowa if (action == DAD_BEGIN) { 4066c15b1ccaSHannes Frederic Sowa addrconf_dad_begin(ifp); 4067c15b1ccaSHannes Frederic Sowa goto out; 4068c15b1ccaSHannes Frederic Sowa } else if (action == DAD_ABORT) { 4069751eb6b6SWei Yongjun in6_ifa_hold(ifp); 4070c15b1ccaSHannes Frederic Sowa addrconf_dad_stop(ifp, 1); 407185b51b12SMike Manning if (disable_ipv6) 407285b51b12SMike Manning addrconf_ifdown(idev->dev, 0); 4073c15b1ccaSHannes Frederic Sowa goto out; 4074c15b1ccaSHannes Frederic Sowa } 4075c15b1ccaSHannes Frederic Sowa 4076b7b1bfceSHannes Frederic Sowa if (!ifp->dad_probes && addrconf_dad_end(ifp)) 4077f2344a13SHerbert Xu goto out; 4078f2344a13SHerbert Xu 4079c15b1ccaSHannes Frederic Sowa write_lock_bh(&idev->lock); 4080122e4519Sstephen hemminger if (idev->dead || !(idev->if_flags & IF_READY)) { 4081c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 40821da177e4SLinus Torvalds goto out; 40831da177e4SLinus Torvalds } 408421809fafSstephen hemminger 408521809fafSstephen hemminger spin_lock(&ifp->lock); 4086622ccdf1SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DEAD) { 4087622ccdf1SHerbert Xu spin_unlock(&ifp->lock); 4088c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 4089622ccdf1SHerbert Xu goto out; 4090622ccdf1SHerbert Xu } 4091622ccdf1SHerbert Xu 4092b7b1bfceSHannes Frederic Sowa if (ifp->dad_probes == 0) { 4093c76fe2d9SDavid Ahern bool send_na = false; 4094c76fe2d9SDavid Ahern 40951da177e4SLinus Torvalds /* 40961da177e4SLinus Torvalds * DAD was successful 40971da177e4SLinus Torvalds */ 40981da177e4SLinus Torvalds 4099c76fe2d9SDavid Ahern if (ifp->flags & IFA_F_TENTATIVE && 4100c76fe2d9SDavid Ahern !(ifp->flags & IFA_F_OPTIMISTIC)) 4101c76fe2d9SDavid Ahern send_na = true; 4102764d3be6SPaolo Abeni bump_id = ifp->flags & IFA_F_TENTATIVE; 4103cc411d0bSBrian Haley ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 410421809fafSstephen hemminger spin_unlock(&ifp->lock); 4105c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 41061da177e4SLinus Torvalds 4107c76fe2d9SDavid Ahern addrconf_dad_completed(ifp, bump_id, send_na); 41081da177e4SLinus Torvalds 41091da177e4SLinus Torvalds goto out; 41101da177e4SLinus Torvalds } 41111da177e4SLinus Torvalds 4112b7b1bfceSHannes Frederic Sowa ifp->dad_probes--; 4113c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 41141f9248e5SJiri Pirko NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME)); 411521809fafSstephen hemminger spin_unlock(&ifp->lock); 4116c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 41171da177e4SLinus Torvalds 41181da177e4SLinus Torvalds /* send a neighbour solicitation for our addr */ 41191da177e4SLinus Torvalds addrconf_addr_solict_mult(&ifp->addr, &mcaddr); 4120adc176c5SErik Nordmark ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any, 4121adc176c5SErik Nordmark ifp->dad_nonce); 41221da177e4SLinus Torvalds out: 41231da177e4SLinus Torvalds in6_ifa_put(ifp); 4124c15b1ccaSHannes Frederic Sowa rtnl_unlock(); 41251da177e4SLinus Torvalds } 41261da177e4SLinus Torvalds 412711ffff75SHannes Frederic Sowa /* ifp->idev must be at least read locked */ 412811ffff75SHannes Frederic Sowa static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp) 412911ffff75SHannes Frederic Sowa { 413011ffff75SHannes Frederic Sowa struct inet6_ifaddr *ifpiter; 413111ffff75SHannes Frederic Sowa struct inet6_dev *idev = ifp->idev; 413211ffff75SHannes Frederic Sowa 4133602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) { 4134602582caSHannes Frederic Sowa if (ifpiter->scope > IFA_LINK) 4135602582caSHannes Frederic Sowa break; 413611ffff75SHannes Frederic Sowa if (ifp != ifpiter && ifpiter->scope == IFA_LINK && 413711ffff75SHannes Frederic Sowa (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE| 413811ffff75SHannes Frederic Sowa IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) == 413911ffff75SHannes Frederic Sowa IFA_F_PERMANENT) 414011ffff75SHannes Frederic Sowa return false; 414111ffff75SHannes Frederic Sowa } 414211ffff75SHannes Frederic Sowa return true; 414311ffff75SHannes Frederic Sowa } 414411ffff75SHannes Frederic Sowa 4145c76fe2d9SDavid Ahern static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id, 4146c76fe2d9SDavid Ahern bool send_na) 41471da177e4SLinus Torvalds { 41481da177e4SLinus Torvalds struct net_device *dev = ifp->idev->dev; 4149b7b1bfceSHannes Frederic Sowa struct in6_addr lladdr; 4150b173ee48SHannes Frederic Sowa bool send_rs, send_mld; 4151b7b1bfceSHannes Frederic Sowa 4152c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifp); 41531da177e4SLinus Torvalds 41541da177e4SLinus Torvalds /* 41551da177e4SLinus Torvalds * Configure the address for reception. Now it is valid. 41561da177e4SLinus Torvalds */ 41571da177e4SLinus Torvalds 41581da177e4SLinus Torvalds ipv6_ifa_notify(RTM_NEWADDR, ifp); 41591da177e4SLinus Torvalds 4160026359bcSTore Anderson /* If added prefix is link local and we are prepared to process 4161026359bcSTore Anderson router advertisements, start sending router solicitations. 41621da177e4SLinus Torvalds */ 41631da177e4SLinus Torvalds 41641ec047ebSHannes Frederic Sowa read_lock_bh(&ifp->idev->lock); 416511ffff75SHannes Frederic Sowa send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp); 4166b173ee48SHannes Frederic Sowa send_rs = send_mld && 4167b173ee48SHannes Frederic Sowa ipv6_accept_ra(ifp->idev) && 4168bd11f074SMaciej Żenczykowski ifp->idev->cnf.rtr_solicits != 0 && 4169b173ee48SHannes Frederic Sowa (dev->flags&IFF_LOOPBACK) == 0; 41701ec047ebSHannes Frederic Sowa read_unlock_bh(&ifp->idev->lock); 41711ec047ebSHannes Frederic Sowa 4172b173ee48SHannes Frederic Sowa /* While dad is in progress mld report's source address is in6_addrany. 4173b173ee48SHannes Frederic Sowa * Resend with proper ll now. 4174b173ee48SHannes Frederic Sowa */ 4175b173ee48SHannes Frederic Sowa if (send_mld) 4176b173ee48SHannes Frederic Sowa ipv6_mc_dad_complete(ifp->idev); 4177b173ee48SHannes Frederic Sowa 4178c76fe2d9SDavid Ahern /* send unsolicited NA if enabled */ 4179c76fe2d9SDavid Ahern if (send_na && 4180c76fe2d9SDavid Ahern (ifp->idev->cnf.ndisc_notify || 4181c76fe2d9SDavid Ahern dev_net(dev)->ipv6.devconf_all->ndisc_notify)) { 4182c76fe2d9SDavid Ahern ndisc_send_na(dev, &in6addr_linklocal_allnodes, &ifp->addr, 4183c76fe2d9SDavid Ahern /*router=*/ !!ifp->idev->cnf.forwarding, 4184c76fe2d9SDavid Ahern /*solicited=*/ false, /*override=*/ true, 4185c76fe2d9SDavid Ahern /*inc_opt=*/ true); 4186c76fe2d9SDavid Ahern } 4187c76fe2d9SDavid Ahern 41881ec047ebSHannes Frederic Sowa if (send_rs) { 41891da177e4SLinus Torvalds /* 41901da177e4SLinus Torvalds * If a host as already performed a random delay 41911da177e4SLinus Torvalds * [...] as part of DAD [...] there is no need 41921da177e4SLinus Torvalds * to delay again before sending the first RS 41931da177e4SLinus Torvalds */ 41941ec047ebSHannes Frederic Sowa if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE)) 4195b7b1bfceSHannes Frederic Sowa return; 41961ec047ebSHannes Frederic Sowa ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters); 41971da177e4SLinus Torvalds 4198b7b1bfceSHannes Frederic Sowa write_lock_bh(&ifp->idev->lock); 4199b7b1bfceSHannes Frederic Sowa spin_lock(&ifp->lock); 4200bd11f074SMaciej Żenczykowski ifp->idev->rs_interval = rfc3315_s14_backoff_init( 4201bd11f074SMaciej Żenczykowski ifp->idev->cnf.rtr_solicit_interval); 4202b7b1bfceSHannes Frederic Sowa ifp->idev->rs_probes = 1; 42031da177e4SLinus Torvalds ifp->idev->if_flags |= IF_RS_SENT; 4204bd11f074SMaciej Żenczykowski addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval); 4205b7b1bfceSHannes Frederic Sowa spin_unlock(&ifp->lock); 4206b7b1bfceSHannes Frederic Sowa write_unlock_bh(&ifp->idev->lock); 42071da177e4SLinus Torvalds } 4208764d3be6SPaolo Abeni 4209764d3be6SPaolo Abeni if (bump_id) 4210764d3be6SPaolo Abeni rt_genid_bump_ipv6(dev_net(dev)); 4211a11a7f71SMarcus Huewe 4212a11a7f71SMarcus Huewe /* Make sure that a new temporary address will be created 4213a11a7f71SMarcus Huewe * before this temporary address becomes deprecated. 4214a11a7f71SMarcus Huewe */ 4215a11a7f71SMarcus Huewe if (ifp->flags & IFA_F_TEMPORARY) 4216a11a7f71SMarcus Huewe addrconf_verify_rtnl(); 42171da177e4SLinus Torvalds } 42181da177e4SLinus Torvalds 4219896585d4SHangbin Liu static void addrconf_dad_run(struct inet6_dev *idev, bool restart) 4220e21e8467SStephen Hemminger { 4221c5e33bddSYOSHIFUJI Hideaki struct inet6_ifaddr *ifp; 4222c5e33bddSYOSHIFUJI Hideaki 4223c5e33bddSYOSHIFUJI Hideaki read_lock_bh(&idev->lock); 4224502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 422521809fafSstephen hemminger spin_lock(&ifp->lock); 4226896585d4SHangbin Liu if ((ifp->flags & IFA_F_TENTATIVE && 4227896585d4SHangbin Liu ifp->state == INET6_IFADDR_STATE_DAD) || restart) { 4228896585d4SHangbin Liu if (restart) 4229896585d4SHangbin Liu ifp->state = INET6_IFADDR_STATE_PREDAD; 4230c5e33bddSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 4231896585d4SHangbin Liu } 4232f2344a13SHerbert Xu spin_unlock(&ifp->lock); 4233c5e33bddSYOSHIFUJI Hideaki } 4234c5e33bddSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 4235c5e33bddSYOSHIFUJI Hideaki } 4236c5e33bddSYOSHIFUJI Hideaki 42371da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 42381da177e4SLinus Torvalds struct if6_iter_state { 42393c40090aSDaniel Lezcano struct seq_net_private p; 42401da177e4SLinus Torvalds int bucket; 42411d578303SMihai Maruseac int offset; 42421da177e4SLinus Torvalds }; 42431da177e4SLinus Torvalds 42441d578303SMihai Maruseac static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos) 42451da177e4SLinus Torvalds { 42461da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 42471218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 4248a5c1d98fSEric Dumazet struct inet6_ifaddr *ifa = NULL; 42491d578303SMihai Maruseac int p = 0; 42501da177e4SLinus Torvalds 42511d578303SMihai Maruseac /* initial bucket if pos is 0 */ 42521d578303SMihai Maruseac if (pos == 0) { 42531d578303SMihai Maruseac state->bucket = 0; 42541d578303SMihai Maruseac state->offset = 0; 42551d578303SMihai Maruseac } 42561d578303SMihai Maruseac 42571d578303SMihai Maruseac for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) { 4258a5c1d98fSEric Dumazet hlist_for_each_entry_rcu(ifa, &inet6_addr_lst[state->bucket], 42591d578303SMihai Maruseac addr_lst) { 42609f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 42619f0d3c27SEric Dumazet continue; 42621d578303SMihai Maruseac /* sync with offset */ 42631d578303SMihai Maruseac if (p < state->offset) { 42641d578303SMihai Maruseac p++; 42651d578303SMihai Maruseac continue; 42661d578303SMihai Maruseac } 42671da177e4SLinus Torvalds return ifa; 42681da177e4SLinus Torvalds } 42691d578303SMihai Maruseac 42701d578303SMihai Maruseac /* prepare for next bucket */ 42711d578303SMihai Maruseac state->offset = 0; 42721d578303SMihai Maruseac p = 0; 42731d578303SMihai Maruseac } 4274c2e21293Sstephen hemminger return NULL; 4275c2e21293Sstephen hemminger } 42761da177e4SLinus Torvalds 4277c2e21293Sstephen hemminger static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, 4278c2e21293Sstephen hemminger struct inet6_ifaddr *ifa) 42791da177e4SLinus Torvalds { 42801da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 42811218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 42821da177e4SLinus Torvalds 4283a5c1d98fSEric Dumazet hlist_for_each_entry_continue_rcu(ifa, addr_lst) { 42849f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 42859f0d3c27SEric Dumazet continue; 42861d578303SMihai Maruseac state->offset++; 42871da177e4SLinus Torvalds return ifa; 42881d578303SMihai Maruseac } 42891da177e4SLinus Torvalds 42901d578303SMihai Maruseac state->offset = 0; 429186f9bd1fSJeff Barnhill while (++state->bucket < IN6_ADDR_HSIZE) { 4292a5c1d98fSEric Dumazet hlist_for_each_entry_rcu(ifa, 4293c2e21293Sstephen hemminger &inet6_addr_lst[state->bucket], addr_lst) { 42949f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 42959f0d3c27SEric Dumazet continue; 4296c2e21293Sstephen hemminger return ifa; 4297c2e21293Sstephen hemminger } 4298c2e21293Sstephen hemminger } 4299c2e21293Sstephen hemminger 4300c2e21293Sstephen hemminger return NULL; 4301c2e21293Sstephen hemminger } 4302c2e21293Sstephen hemminger 43031da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos) 4304a5c1d98fSEric Dumazet __acquires(rcu) 43051da177e4SLinus Torvalds { 4306a5c1d98fSEric Dumazet rcu_read_lock(); 43071d578303SMihai Maruseac return if6_get_first(seq, *pos); 43081da177e4SLinus Torvalds } 43091da177e4SLinus Torvalds 43101da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos) 43111da177e4SLinus Torvalds { 43121da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 43131da177e4SLinus Torvalds 43141da177e4SLinus Torvalds ifa = if6_get_next(seq, v); 43151da177e4SLinus Torvalds ++*pos; 43161da177e4SLinus Torvalds return ifa; 43171da177e4SLinus Torvalds } 43181da177e4SLinus Torvalds 43191da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v) 4320a5c1d98fSEric Dumazet __releases(rcu) 43211da177e4SLinus Torvalds { 4322a5c1d98fSEric Dumazet rcu_read_unlock(); 43231da177e4SLinus Torvalds } 43241da177e4SLinus Torvalds 43251da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v) 43261da177e4SLinus Torvalds { 43271da177e4SLinus Torvalds struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v; 4328971a351cSJiri Pirko seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n", 4329b071195dSHarvey Harrison &ifp->addr, 43301da177e4SLinus Torvalds ifp->idev->dev->ifindex, 43311da177e4SLinus Torvalds ifp->prefix_len, 43321da177e4SLinus Torvalds ifp->scope, 4333971a351cSJiri Pirko (u8) ifp->flags, 43341da177e4SLinus Torvalds ifp->idev->dev->name); 43351da177e4SLinus Torvalds return 0; 43361da177e4SLinus Torvalds } 43371da177e4SLinus Torvalds 433856b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = { 43391da177e4SLinus Torvalds .start = if6_seq_start, 43401da177e4SLinus Torvalds .next = if6_seq_next, 43411da177e4SLinus Torvalds .show = if6_seq_show, 43421da177e4SLinus Torvalds .stop = if6_seq_stop, 43431da177e4SLinus Torvalds }; 43441da177e4SLinus Torvalds 43452c8c1e72SAlexey Dobriyan static int __net_init if6_proc_net_init(struct net *net) 43461da177e4SLinus Torvalds { 4347c3506372SChristoph Hellwig if (!proc_create_net("if_inet6", 0444, net->proc_net, &if6_seq_ops, 4348c3506372SChristoph Hellwig sizeof(struct if6_iter_state))) 43491da177e4SLinus Torvalds return -ENOMEM; 43501da177e4SLinus Torvalds return 0; 43511da177e4SLinus Torvalds } 43521da177e4SLinus Torvalds 43532c8c1e72SAlexey Dobriyan static void __net_exit if6_proc_net_exit(struct net *net) 43543c40090aSDaniel Lezcano { 4355ece31ffdSGao feng remove_proc_entry("if_inet6", net->proc_net); 43563c40090aSDaniel Lezcano } 43573c40090aSDaniel Lezcano 43583c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = { 43593c40090aSDaniel Lezcano .init = if6_proc_net_init, 43603c40090aSDaniel Lezcano .exit = if6_proc_net_exit, 43613c40090aSDaniel Lezcano }; 43623c40090aSDaniel Lezcano 43633c40090aSDaniel Lezcano int __init if6_proc_init(void) 43643c40090aSDaniel Lezcano { 43653c40090aSDaniel Lezcano return register_pernet_subsys(&if6_proc_net_ops); 43663c40090aSDaniel Lezcano } 43673c40090aSDaniel Lezcano 43681da177e4SLinus Torvalds void if6_proc_exit(void) 43691da177e4SLinus Torvalds { 43703c40090aSDaniel Lezcano unregister_pernet_subsys(&if6_proc_net_ops); 43711da177e4SLinus Torvalds } 43721da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 43731da177e4SLinus Torvalds 437407a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6) 43753b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */ 4376b71d1d42SEric Dumazet int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr) 43773b9f9a1cSNoriaki TAKAMIYA { 43783f27fb23SEric Dumazet unsigned int hash = inet6_addr_hash(net, addr); 4379c2e21293Sstephen hemminger struct inet6_ifaddr *ifp = NULL; 43803f27fb23SEric Dumazet int ret = 0; 4381c2e21293Sstephen hemminger 43824e5f47abSEric Dumazet rcu_read_lock(); 43834e5f47abSEric Dumazet hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) { 4384878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 4385389f6612SDaniel Lezcano continue; 4386caad295fSYOSHIFUJI Hideaki if (ipv6_addr_equal(&ifp->addr, addr) && 43873b9f9a1cSNoriaki TAKAMIYA (ifp->flags & IFA_F_HOMEADDRESS)) { 43883b9f9a1cSNoriaki TAKAMIYA ret = 1; 43893b9f9a1cSNoriaki TAKAMIYA break; 43903b9f9a1cSNoriaki TAKAMIYA } 43913b9f9a1cSNoriaki TAKAMIYA } 43924e5f47abSEric Dumazet rcu_read_unlock(); 43933b9f9a1cSNoriaki TAKAMIYA return ret; 43943b9f9a1cSNoriaki TAKAMIYA } 43953b9f9a1cSNoriaki TAKAMIYA #endif 43963b9f9a1cSNoriaki TAKAMIYA 43971da177e4SLinus Torvalds /* 43981da177e4SLinus Torvalds * Periodic address status verification 43991da177e4SLinus Torvalds */ 44001da177e4SLinus Torvalds 4401c15b1ccaSHannes Frederic Sowa static void addrconf_verify_rtnl(void) 44021da177e4SLinus Torvalds { 4403b2db7564SYOSHIFUJI Hideaki unsigned long now, next, next_sec, next_sched; 44041da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 44051da177e4SLinus Torvalds int i; 44061da177e4SLinus Torvalds 4407c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 4408c15b1ccaSHannes Frederic Sowa 44095c578aedSstephen hemminger rcu_read_lock_bh(); 44101da177e4SLinus Torvalds now = jiffies; 4411b2db7564SYOSHIFUJI Hideaki next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY); 44121da177e4SLinus Torvalds 4413c15b1ccaSHannes Frederic Sowa cancel_delayed_work(&addr_chk_work); 44141da177e4SLinus Torvalds 44151da177e4SLinus Torvalds for (i = 0; i < IN6_ADDR_HSIZE; i++) { 44161da177e4SLinus Torvalds restart: 4417c15b1ccaSHannes Frederic Sowa hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) { 44181da177e4SLinus Torvalds unsigned long age; 44191da177e4SLinus Torvalds 4420fad8da3eSYasushi Asano /* When setting preferred_lft to a value not zero or 4421fad8da3eSYasushi Asano * infinity, while valid_lft is infinity 4422fad8da3eSYasushi Asano * IFA_F_PERMANENT has a non-infinity life time. 4423fad8da3eSYasushi Asano */ 4424fad8da3eSYasushi Asano if ((ifp->flags & IFA_F_PERMANENT) && 4425fad8da3eSYasushi Asano (ifp->prefered_lft == INFINITY_LIFE_TIME)) 44261da177e4SLinus Torvalds continue; 44271da177e4SLinus Torvalds 44281da177e4SLinus Torvalds spin_lock(&ifp->lock); 4429b2db7564SYOSHIFUJI Hideaki /* We try to batch several events at once. */ 4430b2db7564SYOSHIFUJI Hideaki age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; 44311da177e4SLinus Torvalds 44328f27ebb9SYOSHIFUJI Hideaki if (ifp->valid_lft != INFINITY_LIFE_TIME && 44338f27ebb9SYOSHIFUJI Hideaki age >= ifp->valid_lft) { 44341da177e4SLinus Torvalds spin_unlock(&ifp->lock); 44351da177e4SLinus Torvalds in6_ifa_hold(ifp); 44361da177e4SLinus Torvalds ipv6_del_addr(ifp); 44371da177e4SLinus Torvalds goto restart; 44388f27ebb9SYOSHIFUJI Hideaki } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) { 44398f27ebb9SYOSHIFUJI Hideaki spin_unlock(&ifp->lock); 44408f27ebb9SYOSHIFUJI Hideaki continue; 44411da177e4SLinus Torvalds } else if (age >= ifp->prefered_lft) { 4442a1ed0526SBrian Haley /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */ 44431da177e4SLinus Torvalds int deprecate = 0; 44441da177e4SLinus Torvalds 44451da177e4SLinus Torvalds if (!(ifp->flags&IFA_F_DEPRECATED)) { 44461da177e4SLinus Torvalds deprecate = 1; 44471da177e4SLinus Torvalds ifp->flags |= IFA_F_DEPRECATED; 44481da177e4SLinus Torvalds } 44491da177e4SLinus Torvalds 4450fad8da3eSYasushi Asano if ((ifp->valid_lft != INFINITY_LIFE_TIME) && 4451fad8da3eSYasushi Asano (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))) 44521da177e4SLinus Torvalds next = ifp->tstamp + ifp->valid_lft * HZ; 44531da177e4SLinus Torvalds 44541da177e4SLinus Torvalds spin_unlock(&ifp->lock); 44551da177e4SLinus Torvalds 44561da177e4SLinus Torvalds if (deprecate) { 44571da177e4SLinus Torvalds in6_ifa_hold(ifp); 44581da177e4SLinus Torvalds 44591da177e4SLinus Torvalds ipv6_ifa_notify(0, ifp); 44601da177e4SLinus Torvalds in6_ifa_put(ifp); 44611da177e4SLinus Torvalds goto restart; 44621da177e4SLinus Torvalds } 44631da177e4SLinus Torvalds } else if ((ifp->flags&IFA_F_TEMPORARY) && 44641da177e4SLinus Torvalds !(ifp->flags&IFA_F_TENTATIVE)) { 446588949cf4Sstephen hemminger unsigned long regen_advance = ifp->idev->cnf.regen_max_retry * 446688949cf4Sstephen hemminger ifp->idev->cnf.dad_transmits * 44671f9248e5SJiri Pirko NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ; 446888949cf4Sstephen hemminger 44691da177e4SLinus Torvalds if (age >= ifp->prefered_lft - regen_advance) { 44701da177e4SLinus Torvalds struct inet6_ifaddr *ifpub = ifp->ifpub; 44711da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 44721da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 44731da177e4SLinus Torvalds if (!ifp->regen_count && ifpub) { 44741da177e4SLinus Torvalds ifp->regen_count++; 44751da177e4SLinus Torvalds in6_ifa_hold(ifp); 44761da177e4SLinus Torvalds in6_ifa_hold(ifpub); 44771da177e4SLinus Torvalds spin_unlock(&ifp->lock); 44785c578aedSstephen hemminger 4479291d809bSHiroyuki YAMAMORI spin_lock(&ifpub->lock); 4480291d809bSHiroyuki YAMAMORI ifpub->regen_count = 0; 4481291d809bSHiroyuki YAMAMORI spin_unlock(&ifpub->lock); 4482e64e469bSEric Dumazet rcu_read_unlock_bh(); 4483fffcefe9SEric Dumazet ipv6_create_tempaddr(ifpub, ifp, true); 44841da177e4SLinus Torvalds in6_ifa_put(ifpub); 44851da177e4SLinus Torvalds in6_ifa_put(ifp); 4486e64e469bSEric Dumazet rcu_read_lock_bh(); 44871da177e4SLinus Torvalds goto restart; 44881da177e4SLinus Torvalds } 44891da177e4SLinus Torvalds } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next)) 44901da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ; 44911da177e4SLinus Torvalds spin_unlock(&ifp->lock); 44921da177e4SLinus Torvalds } else { 44931da177e4SLinus Torvalds /* ifp->prefered_lft <= ifp->valid_lft */ 44941da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 44951da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 44961da177e4SLinus Torvalds spin_unlock(&ifp->lock); 44971da177e4SLinus Torvalds } 44981da177e4SLinus Torvalds } 44991da177e4SLinus Torvalds } 45001da177e4SLinus Torvalds 4501b2db7564SYOSHIFUJI Hideaki next_sec = round_jiffies_up(next); 4502b2db7564SYOSHIFUJI Hideaki next_sched = next; 4503b2db7564SYOSHIFUJI Hideaki 4504b2db7564SYOSHIFUJI Hideaki /* If rounded timeout is accurate enough, accept it. */ 4505b2db7564SYOSHIFUJI Hideaki if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ)) 4506b2db7564SYOSHIFUJI Hideaki next_sched = next_sec; 4507b2db7564SYOSHIFUJI Hideaki 4508b2db7564SYOSHIFUJI Hideaki /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */ 4509b2db7564SYOSHIFUJI Hideaki if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX)) 4510b2db7564SYOSHIFUJI Hideaki next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX; 4511b2db7564SYOSHIFUJI Hideaki 4512e32ac250SJoe Perches pr_debug("now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n", 4513ba3542e1Sdingtianhong now, next, next_sec, next_sched); 4514c15b1ccaSHannes Frederic Sowa mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now); 45155c578aedSstephen hemminger rcu_read_unlock_bh(); 45161da177e4SLinus Torvalds } 45171da177e4SLinus Torvalds 4518c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *w) 4519c15b1ccaSHannes Frederic Sowa { 4520c15b1ccaSHannes Frederic Sowa rtnl_lock(); 4521c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 4522c15b1ccaSHannes Frederic Sowa rtnl_unlock(); 4523c15b1ccaSHannes Frederic Sowa } 4524c15b1ccaSHannes Frederic Sowa 4525c15b1ccaSHannes Frederic Sowa static void addrconf_verify(void) 4526c15b1ccaSHannes Frederic Sowa { 4527c15b1ccaSHannes Frederic Sowa mod_delayed_work(addrconf_wq, &addr_chk_work, 0); 4528c15b1ccaSHannes Frederic Sowa } 4529c15b1ccaSHannes Frederic Sowa 4530caeaba79SNicolas Dichtel static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local, 4531caeaba79SNicolas Dichtel struct in6_addr **peer_pfx) 4532461d8837SThomas Graf { 4533461d8837SThomas Graf struct in6_addr *pfx = NULL; 4534461d8837SThomas Graf 4535caeaba79SNicolas Dichtel *peer_pfx = NULL; 4536caeaba79SNicolas Dichtel 4537461d8837SThomas Graf if (addr) 4538461d8837SThomas Graf pfx = nla_data(addr); 4539461d8837SThomas Graf 4540461d8837SThomas Graf if (local) { 4541461d8837SThomas Graf if (pfx && nla_memcmp(local, pfx, sizeof(*pfx))) 4542caeaba79SNicolas Dichtel *peer_pfx = pfx; 4543461d8837SThomas Graf pfx = nla_data(local); 4544461d8837SThomas Graf } 4545461d8837SThomas Graf 4546461d8837SThomas Graf return pfx; 4547461d8837SThomas Graf } 4548461d8837SThomas Graf 4549ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = { 4550461d8837SThomas Graf [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) }, 4551461d8837SThomas Graf [IFA_LOCAL] = { .len = sizeof(struct in6_addr) }, 4552461d8837SThomas Graf [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) }, 4553479840ffSJiri Pirko [IFA_FLAGS] = { .len = sizeof(u32) }, 45548308f3ffSDavid Ahern [IFA_RT_PRIORITY] = { .len = sizeof(u32) }, 45556ecf4c37SChristian Brauner [IFA_TARGET_NETNSID] = { .type = NLA_S32 }, 4556461d8837SThomas Graf }; 4557461d8837SThomas Graf 45581da177e4SLinus Torvalds static int 4559c21ef3e3SDavid Ahern inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, 4560c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 45611da177e4SLinus Torvalds { 45623b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 4563b933f716SThomas Graf struct ifaddrmsg *ifm; 4564b933f716SThomas Graf struct nlattr *tb[IFA_MAX+1]; 4565caeaba79SNicolas Dichtel struct in6_addr *pfx, *peer_pfx; 45666046d5b4SHeiner Kallweit u32 ifa_flags; 4567b933f716SThomas Graf int err; 45681da177e4SLinus Torvalds 45698cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX, 45708cb08174SJohannes Berg ifa_ipv6_policy, extack); 4571b933f716SThomas Graf if (err < 0) 4572b933f716SThomas Graf return err; 4573b933f716SThomas Graf 4574b933f716SThomas Graf ifm = nlmsg_data(nlh); 4575caeaba79SNicolas Dichtel pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); 457663159f29SIan Morris if (!pfx) 45771da177e4SLinus Torvalds return -EINVAL; 45781da177e4SLinus Torvalds 45796046d5b4SHeiner Kallweit ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags; 45806046d5b4SHeiner Kallweit 45816046d5b4SHeiner Kallweit /* We ignore other flags so far. */ 45826046d5b4SHeiner Kallweit ifa_flags &= IFA_F_MANAGETEMPADDR; 45836046d5b4SHeiner Kallweit 45846046d5b4SHeiner Kallweit return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx, 45856046d5b4SHeiner Kallweit ifm->ifa_prefixlen); 45861da177e4SLinus Torvalds } 45871da177e4SLinus Torvalds 45888308f3ffSDavid Ahern static int modify_prefix_route(struct inet6_ifaddr *ifp, 45898308f3ffSDavid Ahern unsigned long expires, u32 flags) 45908308f3ffSDavid Ahern { 45918308f3ffSDavid Ahern struct fib6_info *f6i; 4592e7c7faa9SDavid Ahern u32 prio; 45938308f3ffSDavid Ahern 45942b2450caSDavid Ahern f6i = addrconf_get_prefix_route(&ifp->addr, ifp->prefix_len, 45952b2450caSDavid Ahern ifp->idev->dev, 0, RTF_DEFAULT, true); 45968308f3ffSDavid Ahern if (!f6i) 45978308f3ffSDavid Ahern return -ENOENT; 45988308f3ffSDavid Ahern 4599e7c7faa9SDavid Ahern prio = ifp->rt_priority ? : IP6_RT_PRIO_ADDRCONF; 4600e7c7faa9SDavid Ahern if (f6i->fib6_metric != prio) { 4601e7c7faa9SDavid Ahern /* delete old one */ 4602e7c7faa9SDavid Ahern ip6_del_rt(dev_net(ifp->idev->dev), f6i); 4603e7c7faa9SDavid Ahern 46048308f3ffSDavid Ahern /* add new one */ 46058308f3ffSDavid Ahern addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 46068308f3ffSDavid Ahern ifp->rt_priority, ifp->idev->dev, 46078308f3ffSDavid Ahern expires, flags, GFP_KERNEL); 46088308f3ffSDavid Ahern } else { 46098308f3ffSDavid Ahern if (!expires) 46108308f3ffSDavid Ahern fib6_clean_expires(f6i); 46118308f3ffSDavid Ahern else 46128308f3ffSDavid Ahern fib6_set_expires(f6i, expires); 46138308f3ffSDavid Ahern 46148308f3ffSDavid Ahern fib6_info_release(f6i); 46158308f3ffSDavid Ahern } 46168308f3ffSDavid Ahern 46178308f3ffSDavid Ahern return 0; 46188308f3ffSDavid Ahern } 46198308f3ffSDavid Ahern 4620d169a1f8SDavid Ahern static int inet6_addr_modify(struct inet6_ifaddr *ifp, struct ifa6_config *cfg) 4621081bba5bSNoriaki TAKAMIYA { 46226f704992SYOSHIFUJI Hideaki u32 flags; 46236f704992SYOSHIFUJI Hideaki clock_t expires; 46244bed72e4SYOSHIFUJI Hideaki unsigned long timeout; 462553bd6749SJiri Pirko bool was_managetempaddr; 46265b84efecSThomas Haller bool had_prefixroute; 462746d48046SYOSHIFUJI Hideaki 4628c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 4629c15b1ccaSHannes Frederic Sowa 4630d169a1f8SDavid Ahern if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft) 4631081bba5bSNoriaki TAKAMIYA return -EINVAL; 4632081bba5bSNoriaki TAKAMIYA 4633d169a1f8SDavid Ahern if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR && 463453bd6749SJiri Pirko (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64)) 463553bd6749SJiri Pirko return -EINVAL; 463653bd6749SJiri Pirko 4637f1c02cfbSSabrina Dubroca if (!(ifp->flags & IFA_F_TENTATIVE) || ifp->flags & IFA_F_DADFAILED) 4638d169a1f8SDavid Ahern cfg->ifa_flags &= ~IFA_F_OPTIMISTIC; 4639f1c02cfbSSabrina Dubroca 4640d169a1f8SDavid Ahern timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ); 46414bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 46424bed72e4SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(timeout * HZ); 4643d169a1f8SDavid Ahern cfg->valid_lft = timeout; 46446f704992SYOSHIFUJI Hideaki flags = RTF_EXPIRES; 46454bed72e4SYOSHIFUJI Hideaki } else { 46464bed72e4SYOSHIFUJI Hideaki expires = 0; 46474bed72e4SYOSHIFUJI Hideaki flags = 0; 4648d169a1f8SDavid Ahern cfg->ifa_flags |= IFA_F_PERMANENT; 46496f704992SYOSHIFUJI Hideaki } 4650081bba5bSNoriaki TAKAMIYA 4651d169a1f8SDavid Ahern timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ); 46524bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 46534bed72e4SYOSHIFUJI Hideaki if (timeout == 0) 4654d169a1f8SDavid Ahern cfg->ifa_flags |= IFA_F_DEPRECATED; 4655d169a1f8SDavid Ahern cfg->preferred_lft = timeout; 46564bed72e4SYOSHIFUJI Hideaki } 4657081bba5bSNoriaki TAKAMIYA 4658081bba5bSNoriaki TAKAMIYA spin_lock_bh(&ifp->lock); 465953bd6749SJiri Pirko was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR; 46605b84efecSThomas Haller had_prefixroute = ifp->flags & IFA_F_PERMANENT && 46615b84efecSThomas Haller !(ifp->flags & IFA_F_NOPREFIXROUTE); 466253bd6749SJiri Pirko ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | 4663761aac73SThomas Haller IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR | 4664761aac73SThomas Haller IFA_F_NOPREFIXROUTE); 4665d169a1f8SDavid Ahern ifp->flags |= cfg->ifa_flags; 4666081bba5bSNoriaki TAKAMIYA ifp->tstamp = jiffies; 4667d169a1f8SDavid Ahern ifp->valid_lft = cfg->valid_lft; 4668d169a1f8SDavid Ahern ifp->prefered_lft = cfg->preferred_lft; 4669081bba5bSNoriaki TAKAMIYA 46708308f3ffSDavid Ahern if (cfg->rt_priority && cfg->rt_priority != ifp->rt_priority) 46718308f3ffSDavid Ahern ifp->rt_priority = cfg->rt_priority; 4672081bba5bSNoriaki TAKAMIYA 4673081bba5bSNoriaki TAKAMIYA spin_unlock_bh(&ifp->lock); 4674081bba5bSNoriaki TAKAMIYA if (!(ifp->flags&IFA_F_TENTATIVE)) 4675081bba5bSNoriaki TAKAMIYA ipv6_ifa_notify(0, ifp); 4676081bba5bSNoriaki TAKAMIYA 4677d169a1f8SDavid Ahern if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) { 46788308f3ffSDavid Ahern int rc = -ENOENT; 46798308f3ffSDavid Ahern 46808308f3ffSDavid Ahern if (had_prefixroute) 46818308f3ffSDavid Ahern rc = modify_prefix_route(ifp, expires, flags); 46828308f3ffSDavid Ahern 46838308f3ffSDavid Ahern /* prefix route could have been deleted; if so restore it */ 46848308f3ffSDavid Ahern if (rc == -ENOENT) { 4685acb54e3cSDavid Ahern addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 46868308f3ffSDavid Ahern ifp->rt_priority, ifp->idev->dev, 46878308f3ffSDavid Ahern expires, flags, GFP_KERNEL); 46888308f3ffSDavid Ahern } 46895b84efecSThomas Haller } else if (had_prefixroute) { 46905b84efecSThomas Haller enum cleanup_prefix_rt_t action; 46915b84efecSThomas Haller unsigned long rt_expires; 46925b84efecSThomas Haller 46935b84efecSThomas Haller write_lock_bh(&ifp->idev->lock); 46945b84efecSThomas Haller action = check_cleanup_prefix_route(ifp, &rt_expires); 46955b84efecSThomas Haller write_unlock_bh(&ifp->idev->lock); 46965b84efecSThomas Haller 46975b84efecSThomas Haller if (action != CLEANUP_PREFIX_RT_NOP) { 46985b84efecSThomas Haller cleanup_prefix_route(ifp, rt_expires, 46995b84efecSThomas Haller action == CLEANUP_PREFIX_RT_DEL); 47005b84efecSThomas Haller } 4701761aac73SThomas Haller } 470253bd6749SJiri Pirko 470353bd6749SJiri Pirko if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) { 4704d169a1f8SDavid Ahern if (was_managetempaddr && 4705d169a1f8SDavid Ahern !(ifp->flags & IFA_F_MANAGETEMPADDR)) { 4706d169a1f8SDavid Ahern cfg->valid_lft = 0; 4707d169a1f8SDavid Ahern cfg->preferred_lft = 0; 4708d169a1f8SDavid Ahern } 4709d169a1f8SDavid Ahern manage_tempaddrs(ifp->idev, ifp, cfg->valid_lft, 4710d169a1f8SDavid Ahern cfg->preferred_lft, !was_managetempaddr, 4711d169a1f8SDavid Ahern jiffies); 471253bd6749SJiri Pirko } 471353bd6749SJiri Pirko 4714c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 4715081bba5bSNoriaki TAKAMIYA 4716081bba5bSNoriaki TAKAMIYA return 0; 4717081bba5bSNoriaki TAKAMIYA } 4718081bba5bSNoriaki TAKAMIYA 4719081bba5bSNoriaki TAKAMIYA static int 4720c21ef3e3SDavid Ahern inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, 4721c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 47221da177e4SLinus Torvalds { 47233b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 4724461d8837SThomas Graf struct ifaddrmsg *ifm; 4725461d8837SThomas Graf struct nlattr *tb[IFA_MAX+1]; 472619b1518cSDavid Ahern struct in6_addr *peer_pfx; 47277198f8ceSThomas Graf struct inet6_ifaddr *ifa; 47287198f8ceSThomas Graf struct net_device *dev; 4729f1c02cfbSSabrina Dubroca struct inet6_dev *idev; 473019b1518cSDavid Ahern struct ifa6_config cfg; 4731461d8837SThomas Graf int err; 47321da177e4SLinus Torvalds 47338cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX, 47348cb08174SJohannes Berg ifa_ipv6_policy, extack); 4735461d8837SThomas Graf if (err < 0) 4736461d8837SThomas Graf return err; 4737461d8837SThomas Graf 473819b1518cSDavid Ahern memset(&cfg, 0, sizeof(cfg)); 473919b1518cSDavid Ahern 4740461d8837SThomas Graf ifm = nlmsg_data(nlh); 474119b1518cSDavid Ahern cfg.pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); 474219b1518cSDavid Ahern if (!cfg.pfx) 47431da177e4SLinus Torvalds return -EINVAL; 47441da177e4SLinus Torvalds 474519b1518cSDavid Ahern cfg.peer_pfx = peer_pfx; 474619b1518cSDavid Ahern cfg.plen = ifm->ifa_prefixlen; 47478308f3ffSDavid Ahern if (tb[IFA_RT_PRIORITY]) 47488308f3ffSDavid Ahern cfg.rt_priority = nla_get_u32(tb[IFA_RT_PRIORITY]); 47498308f3ffSDavid Ahern 475019b1518cSDavid Ahern cfg.valid_lft = INFINITY_LIFE_TIME; 475119b1518cSDavid Ahern cfg.preferred_lft = INFINITY_LIFE_TIME; 475219b1518cSDavid Ahern 4753461d8837SThomas Graf if (tb[IFA_CACHEINFO]) { 47540778769dSNoriaki TAKAMIYA struct ifa_cacheinfo *ci; 4755461d8837SThomas Graf 4756461d8837SThomas Graf ci = nla_data(tb[IFA_CACHEINFO]); 475719b1518cSDavid Ahern cfg.valid_lft = ci->ifa_valid; 475819b1518cSDavid Ahern cfg.preferred_lft = ci->ifa_prefered; 47590778769dSNoriaki TAKAMIYA } 47600778769dSNoriaki TAKAMIYA 4761af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifm->ifa_index); 476263159f29SIan Morris if (!dev) 47637198f8ceSThomas Graf return -ENODEV; 4764081bba5bSNoriaki TAKAMIYA 476519b1518cSDavid Ahern if (tb[IFA_FLAGS]) 476619b1518cSDavid Ahern cfg.ifa_flags = nla_get_u32(tb[IFA_FLAGS]); 476719b1518cSDavid Ahern else 476819b1518cSDavid Ahern cfg.ifa_flags = ifm->ifa_flags; 4769479840ffSJiri Pirko 477055ebaef1SNoriaki TAKAMIYA /* We ignore other flags so far. */ 477119b1518cSDavid Ahern cfg.ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | 477219b1518cSDavid Ahern IFA_F_MANAGETEMPADDR | IFA_F_NOPREFIXROUTE | 477319b1518cSDavid Ahern IFA_F_MCAUTOJOIN | IFA_F_OPTIMISTIC; 4774f1c02cfbSSabrina Dubroca 4775f1c02cfbSSabrina Dubroca idev = ipv6_find_idev(dev); 4776db0b99f5SSabrina Dubroca if (IS_ERR(idev)) 4777db0b99f5SSabrina Dubroca return PTR_ERR(idev); 4778f1c02cfbSSabrina Dubroca 4779f1c02cfbSSabrina Dubroca if (!ipv6_allow_optimistic_dad(net, idev)) 478019b1518cSDavid Ahern cfg.ifa_flags &= ~IFA_F_OPTIMISTIC; 4781f1c02cfbSSabrina Dubroca 478219b1518cSDavid Ahern if (cfg.ifa_flags & IFA_F_NODAD && 478319b1518cSDavid Ahern cfg.ifa_flags & IFA_F_OPTIMISTIC) { 4784f1c02cfbSSabrina Dubroca NL_SET_ERR_MSG(extack, "IFA_F_NODAD and IFA_F_OPTIMISTIC are mutually exclusive"); 4785f1c02cfbSSabrina Dubroca return -EINVAL; 4786f1c02cfbSSabrina Dubroca } 478755ebaef1SNoriaki TAKAMIYA 478819b1518cSDavid Ahern ifa = ipv6_get_ifaddr(net, cfg.pfx, dev, 1); 478963159f29SIan Morris if (!ifa) { 47907198f8ceSThomas Graf /* 47917198f8ceSThomas Graf * It would be best to check for !NLM_F_CREATE here but 4792db9c7c39Sstephen hemminger * userspace already relies on not having to provide this. 47937198f8ceSThomas Graf */ 479419b1518cSDavid Ahern return inet6_addr_add(net, ifm->ifa_index, &cfg, extack); 47951da177e4SLinus Torvalds } 47961da177e4SLinus Torvalds 47977198f8ceSThomas Graf if (nlh->nlmsg_flags & NLM_F_EXCL || 47987198f8ceSThomas Graf !(nlh->nlmsg_flags & NLM_F_REPLACE)) 47997198f8ceSThomas Graf err = -EEXIST; 48007198f8ceSThomas Graf else 4801d169a1f8SDavid Ahern err = inet6_addr_modify(ifa, &cfg); 48027198f8ceSThomas Graf 48037198f8ceSThomas Graf in6_ifa_put(ifa); 48047198f8ceSThomas Graf 48057198f8ceSThomas Graf return err; 48067198f8ceSThomas Graf } 48077198f8ceSThomas Graf 4808479840ffSJiri Pirko static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags, 4809101bb229SThomas Graf u8 scope, int ifindex) 4810101bb229SThomas Graf { 4811101bb229SThomas Graf struct ifaddrmsg *ifm; 4812101bb229SThomas Graf 4813101bb229SThomas Graf ifm = nlmsg_data(nlh); 4814101bb229SThomas Graf ifm->ifa_family = AF_INET6; 4815101bb229SThomas Graf ifm->ifa_prefixlen = prefixlen; 4816101bb229SThomas Graf ifm->ifa_flags = flags; 4817101bb229SThomas Graf ifm->ifa_scope = scope; 4818101bb229SThomas Graf ifm->ifa_index = ifindex; 4819101bb229SThomas Graf } 4820101bb229SThomas Graf 482185486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp, 482285486af0SThomas Graf unsigned long tstamp, u32 preferred, u32 valid) 482385486af0SThomas Graf { 482485486af0SThomas Graf struct ifa_cacheinfo ci; 482585486af0SThomas Graf 482618a31e1eSThomas Graf ci.cstamp = cstamp_delta(cstamp); 482718a31e1eSThomas Graf ci.tstamp = cstamp_delta(tstamp); 482885486af0SThomas Graf ci.ifa_prefered = preferred; 482985486af0SThomas Graf ci.ifa_valid = valid; 483085486af0SThomas Graf 483185486af0SThomas Graf return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci); 483285486af0SThomas Graf } 483385486af0SThomas Graf 4834101bb229SThomas Graf static inline int rt_scope(int ifa_scope) 4835101bb229SThomas Graf { 4836101bb229SThomas Graf if (ifa_scope & IFA_HOST) 4837101bb229SThomas Graf return RT_SCOPE_HOST; 4838101bb229SThomas Graf else if (ifa_scope & IFA_LINK) 4839101bb229SThomas Graf return RT_SCOPE_LINK; 4840101bb229SThomas Graf else if (ifa_scope & IFA_SITE) 4841101bb229SThomas Graf return RT_SCOPE_SITE; 4842101bb229SThomas Graf else 4843101bb229SThomas Graf return RT_SCOPE_UNIVERSE; 4844101bb229SThomas Graf } 4845101bb229SThomas Graf 48460ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void) 48470ab6803bSThomas Graf { 4848339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifaddrmsg)) 4849caeaba79SNicolas Dichtel + nla_total_size(16) /* IFA_LOCAL */ 4850339bf98fSThomas Graf + nla_total_size(16) /* IFA_ADDRESS */ 4851479840ffSJiri Pirko + nla_total_size(sizeof(struct ifa_cacheinfo)) 48528308f3ffSDavid Ahern + nla_total_size(4) /* IFA_FLAGS */ 48538308f3ffSDavid Ahern + nla_total_size(4) /* IFA_RT_PRIORITY */; 48540ab6803bSThomas Graf } 4855c5396a31SYOSHIFUJI Hideaki 48566ba1e6e8SDavid Ahern enum addr_type_t { 48576ba1e6e8SDavid Ahern UNICAST_ADDR, 48586ba1e6e8SDavid Ahern MULTICAST_ADDR, 48596ba1e6e8SDavid Ahern ANYCAST_ADDR, 48606ba1e6e8SDavid Ahern }; 48616ba1e6e8SDavid Ahern 4862203651b6SChristian Brauner struct inet6_fill_args { 4863203651b6SChristian Brauner u32 portid; 4864203651b6SChristian Brauner u32 seq; 4865203651b6SChristian Brauner int event; 4866203651b6SChristian Brauner unsigned int flags; 4867203651b6SChristian Brauner int netnsid; 48686371a71fSDavid Ahern int ifindex; 48696ba1e6e8SDavid Ahern enum addr_type_t type; 4870203651b6SChristian Brauner }; 4871203651b6SChristian Brauner 48721da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa, 4873203651b6SChristian Brauner struct inet6_fill_args *args) 48741da177e4SLinus Torvalds { 48751da177e4SLinus Torvalds struct nlmsghdr *nlh; 487685486af0SThomas Graf u32 preferred, valid; 48771da177e4SLinus Torvalds 4878203651b6SChristian Brauner nlh = nlmsg_put(skb, args->portid, args->seq, args->event, 4879203651b6SChristian Brauner sizeof(struct ifaddrmsg), args->flags); 488063159f29SIan Morris if (!nlh) 488126932566SPatrick McHardy return -EMSGSIZE; 48820ab6803bSThomas Graf 4883101bb229SThomas Graf put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope), 4884101bb229SThomas Graf ifa->idev->dev->ifindex); 4885101bb229SThomas Graf 4886203651b6SChristian Brauner if (args->netnsid >= 0 && 4887203651b6SChristian Brauner nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) 48886ecf4c37SChristian Brauner goto error; 48896ecf4c37SChristian Brauner 4890fad8da3eSYasushi Asano if (!((ifa->flags&IFA_F_PERMANENT) && 4891fad8da3eSYasushi Asano (ifa->prefered_lft == INFINITY_LIFE_TIME))) { 489285486af0SThomas Graf preferred = ifa->prefered_lft; 489385486af0SThomas Graf valid = ifa->valid_lft; 489485486af0SThomas Graf if (preferred != INFINITY_LIFE_TIME) { 48951da177e4SLinus Torvalds long tval = (jiffies - ifa->tstamp)/HZ; 4896a1faa698SJens Rosenboom if (preferred > tval) 489785486af0SThomas Graf preferred -= tval; 4898a1faa698SJens Rosenboom else 4899a1faa698SJens Rosenboom preferred = 0; 4900f56619fcSBen Hutchings if (valid != INFINITY_LIFE_TIME) { 4901f56619fcSBen Hutchings if (valid > tval) 490285486af0SThomas Graf valid -= tval; 4903f56619fcSBen Hutchings else 4904f56619fcSBen Hutchings valid = 0; 4905f56619fcSBen Hutchings } 49061da177e4SLinus Torvalds } 49071da177e4SLinus Torvalds } else { 490885486af0SThomas Graf preferred = INFINITY_LIFE_TIME; 490985486af0SThomas Graf valid = INFINITY_LIFE_TIME; 49101da177e4SLinus Torvalds } 491185486af0SThomas Graf 49127996c799SCong Wang if (!ipv6_addr_any(&ifa->peer_addr)) { 4913930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 || 4914930345eaSJiri Benc nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0) 4915caeaba79SNicolas Dichtel goto error; 4916caeaba79SNicolas Dichtel } else 4917930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0) 4918caeaba79SNicolas Dichtel goto error; 4919caeaba79SNicolas Dichtel 49208308f3ffSDavid Ahern if (ifa->rt_priority && 49218308f3ffSDavid Ahern nla_put_u32(skb, IFA_RT_PRIORITY, ifa->rt_priority)) 49228308f3ffSDavid Ahern goto error; 49238308f3ffSDavid Ahern 4924caeaba79SNicolas Dichtel if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) 4925caeaba79SNicolas Dichtel goto error; 492685486af0SThomas Graf 4927479840ffSJiri Pirko if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0) 4928479840ffSJiri Pirko goto error; 4929479840ffSJiri Pirko 4930053c095aSJohannes Berg nlmsg_end(skb, nlh); 4931053c095aSJohannes Berg return 0; 4932caeaba79SNicolas Dichtel 4933caeaba79SNicolas Dichtel error: 4934caeaba79SNicolas Dichtel nlmsg_cancel(skb, nlh); 4935caeaba79SNicolas Dichtel return -EMSGSIZE; 49361da177e4SLinus Torvalds } 49371da177e4SLinus Torvalds 49381da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca, 4939203651b6SChristian Brauner struct inet6_fill_args *args) 49401da177e4SLinus Torvalds { 49411da177e4SLinus Torvalds struct nlmsghdr *nlh; 4942101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 4943101bb229SThomas Graf int ifindex = ifmca->idev->dev->ifindex; 49441da177e4SLinus Torvalds 49451da177e4SLinus Torvalds if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE) 4946101bb229SThomas Graf scope = RT_SCOPE_SITE; 4947101bb229SThomas Graf 4948203651b6SChristian Brauner nlh = nlmsg_put(skb, args->portid, args->seq, args->event, 4949203651b6SChristian Brauner sizeof(struct ifaddrmsg), args->flags); 495063159f29SIan Morris if (!nlh) 495126932566SPatrick McHardy return -EMSGSIZE; 49520ab6803bSThomas Graf 4953203651b6SChristian Brauner if (args->netnsid >= 0 && 4954203651b6SChristian Brauner nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) 49556ecf4c37SChristian Brauner return -EMSGSIZE; 49566ecf4c37SChristian Brauner 4957101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 4958930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 || 49590ab6803bSThomas Graf put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp, 496026932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 496126932566SPatrick McHardy nlmsg_cancel(skb, nlh); 496226932566SPatrick McHardy return -EMSGSIZE; 496326932566SPatrick McHardy } 496485486af0SThomas Graf 4965053c095aSJohannes Berg nlmsg_end(skb, nlh); 4966053c095aSJohannes Berg return 0; 49671da177e4SLinus Torvalds } 49681da177e4SLinus Torvalds 49691da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca, 4970203651b6SChristian Brauner struct inet6_fill_args *args) 49711da177e4SLinus Torvalds { 49729ee8cbb2SDavid Ahern struct net_device *dev = fib6_info_nh_dev(ifaca->aca_rt); 49739ee8cbb2SDavid Ahern int ifindex = dev ? dev->ifindex : 1; 49741da177e4SLinus Torvalds struct nlmsghdr *nlh; 4975101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 49761da177e4SLinus Torvalds 49771da177e4SLinus Torvalds if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE) 4978101bb229SThomas Graf scope = RT_SCOPE_SITE; 4979101bb229SThomas Graf 4980203651b6SChristian Brauner nlh = nlmsg_put(skb, args->portid, args->seq, args->event, 4981203651b6SChristian Brauner sizeof(struct ifaddrmsg), args->flags); 498263159f29SIan Morris if (!nlh) 498326932566SPatrick McHardy return -EMSGSIZE; 49840ab6803bSThomas Graf 4985203651b6SChristian Brauner if (args->netnsid >= 0 && 4986203651b6SChristian Brauner nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) 49876ecf4c37SChristian Brauner return -EMSGSIZE; 49886ecf4c37SChristian Brauner 4989101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 4990930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 || 49910ab6803bSThomas Graf put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp, 499226932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 499326932566SPatrick McHardy nlmsg_cancel(skb, nlh); 499426932566SPatrick McHardy return -EMSGSIZE; 499526932566SPatrick McHardy } 499685486af0SThomas Graf 4997053c095aSJohannes Berg nlmsg_end(skb, nlh); 4998053c095aSJohannes Berg return 0; 49991da177e4SLinus Torvalds } 50001da177e4SLinus Torvalds 5001234b27c3SEric Dumazet /* called with rcu_read_lock() */ 5002234b27c3SEric Dumazet static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb, 5003fe884c2bSDavid Ahern struct netlink_callback *cb, int s_ip_idx, 50046ba1e6e8SDavid Ahern struct inet6_fill_args *fillargs) 50051da177e4SLinus Torvalds { 50061da177e4SLinus Torvalds struct ifmcaddr6 *ifmca; 50071da177e4SLinus Torvalds struct ifacaddr6 *ifaca; 5008fe884c2bSDavid Ahern int ip_idx = 0; 5009234b27c3SEric Dumazet int err = 1; 50101da177e4SLinus Torvalds 50111da177e4SLinus Torvalds read_lock_bh(&idev->lock); 50126ba1e6e8SDavid Ahern switch (fillargs->type) { 5013502a2ffdSstephen hemminger case UNICAST_ADDR: { 5014502a2ffdSstephen hemminger struct inet6_ifaddr *ifa; 50156ba1e6e8SDavid Ahern fillargs->event = RTM_NEWADDR; 5016502a2ffdSstephen hemminger 5017ae9cda5dSYOSHIFUJI Hideaki /* unicast address incl. temp addr */ 5018502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 50194ba4c566SDavid Ahern if (ip_idx < s_ip_idx) 50204ba4c566SDavid Ahern goto next; 50216ba1e6e8SDavid Ahern err = inet6_fill_ifaddr(skb, ifa, fillargs); 5022053c095aSJohannes Berg if (err < 0) 5023bcd32326SEric Dumazet break; 502463998ac2SNicolas Dichtel nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 50254ba4c566SDavid Ahern next: 50264ba4c566SDavid Ahern ip_idx++; 50271da177e4SLinus Torvalds } 50281da177e4SLinus Torvalds break; 5029502a2ffdSstephen hemminger } 50301da177e4SLinus Torvalds case MULTICAST_ADDR: 50316ba1e6e8SDavid Ahern fillargs->event = RTM_GETMULTICAST; 5032203651b6SChristian Brauner 50331da177e4SLinus Torvalds /* multicast address */ 50341da177e4SLinus Torvalds for (ifmca = idev->mc_list; ifmca; 50351da177e4SLinus Torvalds ifmca = ifmca->next, ip_idx++) { 50361da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 50371da177e4SLinus Torvalds continue; 50386ba1e6e8SDavid Ahern err = inet6_fill_ifmcaddr(skb, ifmca, fillargs); 5039053c095aSJohannes Berg if (err < 0) 5040bcd32326SEric Dumazet break; 50411da177e4SLinus Torvalds } 50421da177e4SLinus Torvalds break; 50431da177e4SLinus Torvalds case ANYCAST_ADDR: 50446ba1e6e8SDavid Ahern fillargs->event = RTM_GETANYCAST; 50451da177e4SLinus Torvalds /* anycast address */ 50461da177e4SLinus Torvalds for (ifaca = idev->ac_list; ifaca; 50471da177e4SLinus Torvalds ifaca = ifaca->aca_next, ip_idx++) { 50481da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 50491da177e4SLinus Torvalds continue; 50506ba1e6e8SDavid Ahern err = inet6_fill_ifacaddr(skb, ifaca, fillargs); 5051053c095aSJohannes Berg if (err < 0) 5052bcd32326SEric Dumazet break; 50531da177e4SLinus Torvalds } 50541da177e4SLinus Torvalds break; 50551da177e4SLinus Torvalds default: 50561da177e4SLinus Torvalds break; 50571da177e4SLinus Torvalds } 50581da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 5059fe884c2bSDavid Ahern cb->args[2] = ip_idx; 5060234b27c3SEric Dumazet return err; 5061234b27c3SEric Dumazet } 50625d5619b4SYOSHIFUJI Hideaki 5063ed6eff11SDavid Ahern static int inet6_valid_dump_ifaddr_req(const struct nlmsghdr *nlh, 5064ed6eff11SDavid Ahern struct inet6_fill_args *fillargs, 5065ed6eff11SDavid Ahern struct net **tgt_net, struct sock *sk, 50666371a71fSDavid Ahern struct netlink_callback *cb) 5067ed6eff11SDavid Ahern { 50686371a71fSDavid Ahern struct netlink_ext_ack *extack = cb->extack; 5069ed6eff11SDavid Ahern struct nlattr *tb[IFA_MAX+1]; 5070ed6eff11SDavid Ahern struct ifaddrmsg *ifm; 5071ed6eff11SDavid Ahern int err, i; 5072ed6eff11SDavid Ahern 5073ed6eff11SDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 5074ed6eff11SDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Invalid header for address dump request"); 5075ed6eff11SDavid Ahern return -EINVAL; 5076ed6eff11SDavid Ahern } 5077ed6eff11SDavid Ahern 5078ed6eff11SDavid Ahern ifm = nlmsg_data(nlh); 5079ed6eff11SDavid Ahern if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) { 5080ed6eff11SDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for address dump request"); 5081ed6eff11SDavid Ahern return -EINVAL; 5082ed6eff11SDavid Ahern } 50836371a71fSDavid Ahern 50846371a71fSDavid Ahern fillargs->ifindex = ifm->ifa_index; 50856371a71fSDavid Ahern if (fillargs->ifindex) { 50866371a71fSDavid Ahern cb->answer_flags |= NLM_F_DUMP_FILTERED; 50876371a71fSDavid Ahern fillargs->flags |= NLM_F_DUMP_FILTERED; 5088ed6eff11SDavid Ahern } 5089ed6eff11SDavid Ahern 50908cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX, 5091ed6eff11SDavid Ahern ifa_ipv6_policy, extack); 5092ed6eff11SDavid Ahern if (err < 0) 5093ed6eff11SDavid Ahern return err; 5094ed6eff11SDavid Ahern 5095ed6eff11SDavid Ahern for (i = 0; i <= IFA_MAX; ++i) { 5096ed6eff11SDavid Ahern if (!tb[i]) 5097ed6eff11SDavid Ahern continue; 5098ed6eff11SDavid Ahern 5099ed6eff11SDavid Ahern if (i == IFA_TARGET_NETNSID) { 5100ed6eff11SDavid Ahern struct net *net; 5101ed6eff11SDavid Ahern 5102ed6eff11SDavid Ahern fillargs->netnsid = nla_get_s32(tb[i]); 5103ed6eff11SDavid Ahern net = rtnl_get_net_ns_capable(sk, fillargs->netnsid); 5104ed6eff11SDavid Ahern if (IS_ERR(net)) { 5105bf4cc40eSBjørn Mork fillargs->netnsid = -1; 5106ed6eff11SDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Invalid target network namespace id"); 5107ed6eff11SDavid Ahern return PTR_ERR(net); 5108ed6eff11SDavid Ahern } 5109ed6eff11SDavid Ahern *tgt_net = net; 5110ed6eff11SDavid Ahern } else { 5111ed6eff11SDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in dump request"); 5112ed6eff11SDavid Ahern return -EINVAL; 5113ed6eff11SDavid Ahern } 5114ed6eff11SDavid Ahern } 5115ed6eff11SDavid Ahern 5116ed6eff11SDavid Ahern return 0; 5117ed6eff11SDavid Ahern } 5118ed6eff11SDavid Ahern 5119234b27c3SEric Dumazet static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb, 5120234b27c3SEric Dumazet enum addr_type_t type) 5121234b27c3SEric Dumazet { 5122ed6eff11SDavid Ahern const struct nlmsghdr *nlh = cb->nlh; 51236ba1e6e8SDavid Ahern struct inet6_fill_args fillargs = { 51246ba1e6e8SDavid Ahern .portid = NETLINK_CB(cb->skb).portid, 51256ba1e6e8SDavid Ahern .seq = cb->nlh->nlmsg_seq, 51266ba1e6e8SDavid Ahern .flags = NLM_F_MULTI, 51276ba1e6e8SDavid Ahern .netnsid = -1, 51286ba1e6e8SDavid Ahern .type = type, 51296ba1e6e8SDavid Ahern }; 5130234b27c3SEric Dumazet struct net *net = sock_net(skb->sk); 51316ecf4c37SChristian Brauner struct net *tgt_net = net; 5132fe884c2bSDavid Ahern int idx, s_idx, s_ip_idx; 5133234b27c3SEric Dumazet int h, s_h; 5134234b27c3SEric Dumazet struct net_device *dev; 5135234b27c3SEric Dumazet struct inet6_dev *idev; 5136234b27c3SEric Dumazet struct hlist_head *head; 5137242afaa6SDavid Ahern int err = 0; 5138234b27c3SEric Dumazet 5139234b27c3SEric Dumazet s_h = cb->args[0]; 5140234b27c3SEric Dumazet s_idx = idx = cb->args[1]; 5141fe884c2bSDavid Ahern s_ip_idx = cb->args[2]; 5142234b27c3SEric Dumazet 5143ed6eff11SDavid Ahern if (cb->strict_check) { 5144ed6eff11SDavid Ahern err = inet6_valid_dump_ifaddr_req(nlh, &fillargs, &tgt_net, 51456371a71fSDavid Ahern skb->sk, cb); 5146ed6eff11SDavid Ahern if (err < 0) 5147242afaa6SDavid Ahern goto put_tgt_net; 51486371a71fSDavid Ahern 5149242afaa6SDavid Ahern err = 0; 51506371a71fSDavid Ahern if (fillargs.ifindex) { 51516371a71fSDavid Ahern dev = __dev_get_by_index(tgt_net, fillargs.ifindex); 5152242afaa6SDavid Ahern if (!dev) { 5153242afaa6SDavid Ahern err = -ENODEV; 5154242afaa6SDavid Ahern goto put_tgt_net; 5155242afaa6SDavid Ahern } 51566371a71fSDavid Ahern idev = __in6_dev_get(dev); 51576371a71fSDavid Ahern if (idev) { 51586371a71fSDavid Ahern err = in6_dump_addrs(idev, skb, cb, s_ip_idx, 51596371a71fSDavid Ahern &fillargs); 51601518039fSJakub Kicinski if (err > 0) 51611518039fSJakub Kicinski err = 0; 51626371a71fSDavid Ahern } 51636371a71fSDavid Ahern goto put_tgt_net; 51646371a71fSDavid Ahern } 51656ecf4c37SChristian Brauner } 51666ecf4c37SChristian Brauner 5167234b27c3SEric Dumazet rcu_read_lock(); 51686ecf4c37SChristian Brauner cb->seq = atomic_read(&tgt_net->ipv6.dev_addr_genid) ^ tgt_net->dev_base_seq; 5169234b27c3SEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 5170234b27c3SEric Dumazet idx = 0; 51716ecf4c37SChristian Brauner head = &tgt_net->dev_index_head[h]; 5172b67bfe0dSSasha Levin hlist_for_each_entry_rcu(dev, head, index_hlist) { 5173234b27c3SEric Dumazet if (idx < s_idx) 5174234b27c3SEric Dumazet goto cont; 51754b97efdfSPatrick McHardy if (h > s_h || idx > s_idx) 5176234b27c3SEric Dumazet s_ip_idx = 0; 5177e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 5178e21e8467SStephen Hemminger if (!idev) 5179234b27c3SEric Dumazet goto cont; 5180234b27c3SEric Dumazet 5181fe884c2bSDavid Ahern if (in6_dump_addrs(idev, skb, cb, s_ip_idx, 51826ba1e6e8SDavid Ahern &fillargs) < 0) 5183234b27c3SEric Dumazet goto done; 51847562f876SPavel Emelianov cont: 51857562f876SPavel Emelianov idx++; 51861da177e4SLinus Torvalds } 5187234b27c3SEric Dumazet } 5188234b27c3SEric Dumazet done: 5189234b27c3SEric Dumazet rcu_read_unlock(); 5190234b27c3SEric Dumazet cb->args[0] = h; 5191234b27c3SEric Dumazet cb->args[1] = idx; 51926371a71fSDavid Ahern put_tgt_net: 51936ba1e6e8SDavid Ahern if (fillargs.netnsid >= 0) 51946ecf4c37SChristian Brauner put_net(tgt_net); 5195234b27c3SEric Dumazet 51967c1e8a38SArthur Gautier return skb->len ? : err; 51971da177e4SLinus Torvalds } 51981da177e4SLinus Torvalds 51991da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) 52001da177e4SLinus Torvalds { 52011da177e4SLinus Torvalds enum addr_type_t type = UNICAST_ADDR; 5202b854272bSDenis V. Lunev 52031da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 52041da177e4SLinus Torvalds } 52051da177e4SLinus Torvalds 52061da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb) 52071da177e4SLinus Torvalds { 52081da177e4SLinus Torvalds enum addr_type_t type = MULTICAST_ADDR; 5209b854272bSDenis V. Lunev 52101da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 52111da177e4SLinus Torvalds } 52121da177e4SLinus Torvalds 52131da177e4SLinus Torvalds 52141da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb) 52151da177e4SLinus Torvalds { 52161da177e4SLinus Torvalds enum addr_type_t type = ANYCAST_ADDR; 5217b854272bSDenis V. Lunev 52181da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 52191da177e4SLinus Torvalds } 52201da177e4SLinus Torvalds 52214b1373deSJakub Kicinski static int inet6_rtm_valid_getaddr_req(struct sk_buff *skb, 52224b1373deSJakub Kicinski const struct nlmsghdr *nlh, 52234b1373deSJakub Kicinski struct nlattr **tb, 52244b1373deSJakub Kicinski struct netlink_ext_ack *extack) 52254b1373deSJakub Kicinski { 52264b1373deSJakub Kicinski struct ifaddrmsg *ifm; 52274b1373deSJakub Kicinski int i, err; 52284b1373deSJakub Kicinski 52294b1373deSJakub Kicinski if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 52304b1373deSJakub Kicinski NL_SET_ERR_MSG_MOD(extack, "Invalid header for get address request"); 52314b1373deSJakub Kicinski return -EINVAL; 52324b1373deSJakub Kicinski } 52334b1373deSJakub Kicinski 5234*2beb6d29SHangbin Liu if (!netlink_strict_get_check(skb)) 5235*2beb6d29SHangbin Liu return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX, 5236*2beb6d29SHangbin Liu ifa_ipv6_policy, extack); 5237*2beb6d29SHangbin Liu 52384b1373deSJakub Kicinski ifm = nlmsg_data(nlh); 52394b1373deSJakub Kicinski if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) { 52404b1373deSJakub Kicinski NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get address request"); 52414b1373deSJakub Kicinski return -EINVAL; 52424b1373deSJakub Kicinski } 52434b1373deSJakub Kicinski 52448cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX, 52454b1373deSJakub Kicinski ifa_ipv6_policy, extack); 52464b1373deSJakub Kicinski if (err) 52474b1373deSJakub Kicinski return err; 52484b1373deSJakub Kicinski 52494b1373deSJakub Kicinski for (i = 0; i <= IFA_MAX; i++) { 52504b1373deSJakub Kicinski if (!tb[i]) 52514b1373deSJakub Kicinski continue; 52524b1373deSJakub Kicinski 52534b1373deSJakub Kicinski switch (i) { 52544b1373deSJakub Kicinski case IFA_TARGET_NETNSID: 52554b1373deSJakub Kicinski case IFA_ADDRESS: 52564b1373deSJakub Kicinski case IFA_LOCAL: 52574b1373deSJakub Kicinski break; 52584b1373deSJakub Kicinski default: 52594b1373deSJakub Kicinski NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get address request"); 52604b1373deSJakub Kicinski return -EINVAL; 52614b1373deSJakub Kicinski } 52624b1373deSJakub Kicinski } 52634b1373deSJakub Kicinski 52644b1373deSJakub Kicinski return 0; 52654b1373deSJakub Kicinski } 52664b1373deSJakub Kicinski 5267c21ef3e3SDavid Ahern static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh, 5268c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 52696c223828SNoriaki TAKAMIYA { 52703b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 5271203651b6SChristian Brauner struct inet6_fill_args fillargs = { 5272203651b6SChristian Brauner .portid = NETLINK_CB(in_skb).portid, 5273203651b6SChristian Brauner .seq = nlh->nlmsg_seq, 5274203651b6SChristian Brauner .event = RTM_NEWADDR, 5275203651b6SChristian Brauner .flags = 0, 5276203651b6SChristian Brauner .netnsid = -1, 5277203651b6SChristian Brauner }; 52786ecf4c37SChristian Brauner struct net *tgt_net = net; 52791b29fc2cSThomas Graf struct ifaddrmsg *ifm; 52801b29fc2cSThomas Graf struct nlattr *tb[IFA_MAX+1]; 5281caeaba79SNicolas Dichtel struct in6_addr *addr = NULL, *peer; 52826c223828SNoriaki TAKAMIYA struct net_device *dev = NULL; 52836c223828SNoriaki TAKAMIYA struct inet6_ifaddr *ifa; 52846c223828SNoriaki TAKAMIYA struct sk_buff *skb; 52856c223828SNoriaki TAKAMIYA int err; 52866c223828SNoriaki TAKAMIYA 52874b1373deSJakub Kicinski err = inet6_rtm_valid_getaddr_req(in_skb, nlh, tb, extack); 52881b29fc2cSThomas Graf if (err < 0) 5289c24675f8SFlorian Westphal return err; 52906c223828SNoriaki TAKAMIYA 52916ecf4c37SChristian Brauner if (tb[IFA_TARGET_NETNSID]) { 5292203651b6SChristian Brauner fillargs.netnsid = nla_get_s32(tb[IFA_TARGET_NETNSID]); 52936ecf4c37SChristian Brauner 52946ecf4c37SChristian Brauner tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(in_skb).sk, 5295203651b6SChristian Brauner fillargs.netnsid); 52966ecf4c37SChristian Brauner if (IS_ERR(tgt_net)) 52976ecf4c37SChristian Brauner return PTR_ERR(tgt_net); 52986ecf4c37SChristian Brauner } 52996ecf4c37SChristian Brauner 5300caeaba79SNicolas Dichtel addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer); 5301c24675f8SFlorian Westphal if (!addr) 5302c24675f8SFlorian Westphal return -EINVAL; 53031b29fc2cSThomas Graf 53041b29fc2cSThomas Graf ifm = nlmsg_data(nlh); 53056c223828SNoriaki TAKAMIYA if (ifm->ifa_index) 53066ecf4c37SChristian Brauner dev = dev_get_by_index(tgt_net, ifm->ifa_index); 53076c223828SNoriaki TAKAMIYA 53086ecf4c37SChristian Brauner ifa = ipv6_get_ifaddr(tgt_net, addr, dev, 1); 5309e21e8467SStephen Hemminger if (!ifa) { 53101b29fc2cSThomas Graf err = -EADDRNOTAVAIL; 53111b29fc2cSThomas Graf goto errout; 53126c223828SNoriaki TAKAMIYA } 53136c223828SNoriaki TAKAMIYA 5314e21e8467SStephen Hemminger skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL); 5315e21e8467SStephen Hemminger if (!skb) { 53161b29fc2cSThomas Graf err = -ENOBUFS; 53171b29fc2cSThomas Graf goto errout_ifa; 53181b29fc2cSThomas Graf } 53191b29fc2cSThomas Graf 5320203651b6SChristian Brauner err = inet6_fill_ifaddr(skb, ifa, &fillargs); 532126932566SPatrick McHardy if (err < 0) { 532226932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 532326932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 532426932566SPatrick McHardy kfree_skb(skb); 532526932566SPatrick McHardy goto errout_ifa; 532626932566SPatrick McHardy } 53276ecf4c37SChristian Brauner err = rtnl_unicast(skb, tgt_net, NETLINK_CB(in_skb).portid); 53281b29fc2cSThomas Graf errout_ifa: 53296c223828SNoriaki TAKAMIYA in6_ifa_put(ifa); 53301b29fc2cSThomas Graf errout: 5331c24675f8SFlorian Westphal if (dev) 5332c24675f8SFlorian Westphal dev_put(dev); 5333203651b6SChristian Brauner if (fillargs.netnsid >= 0) 53346ecf4c37SChristian Brauner put_net(tgt_net); 53356ecf4c37SChristian Brauner 53366c223828SNoriaki TAKAMIYA return err; 53376c223828SNoriaki TAKAMIYA } 53386c223828SNoriaki TAKAMIYA 53391da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa) 53401da177e4SLinus Torvalds { 53411da177e4SLinus Torvalds struct sk_buff *skb; 5342c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(ifa->idev->dev); 5343203651b6SChristian Brauner struct inet6_fill_args fillargs = { 5344203651b6SChristian Brauner .portid = 0, 5345203651b6SChristian Brauner .seq = 0, 5346203651b6SChristian Brauner .event = event, 5347203651b6SChristian Brauner .flags = 0, 5348203651b6SChristian Brauner .netnsid = -1, 5349203651b6SChristian Brauner }; 53505d620266SThomas Graf int err = -ENOBUFS; 53511da177e4SLinus Torvalds 53520ab6803bSThomas Graf skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC); 535363159f29SIan Morris if (!skb) 53545d620266SThomas Graf goto errout; 53555d620266SThomas Graf 5356203651b6SChristian Brauner err = inet6_fill_ifaddr(skb, ifa, &fillargs); 535726932566SPatrick McHardy if (err < 0) { 535826932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 535926932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 536026932566SPatrick McHardy kfree_skb(skb); 536126932566SPatrick McHardy goto errout; 536226932566SPatrick McHardy } 53631ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC); 53641ce85fe4SPablo Neira Ayuso return; 53655d620266SThomas Graf errout: 53665d620266SThomas Graf if (err < 0) 53676fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err); 53681da177e4SLinus Torvalds } 53691da177e4SLinus Torvalds 5370b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf, 53711da177e4SLinus Torvalds __s32 *array, int bytes) 53721da177e4SLinus Torvalds { 537304561c1fSThomas Graf BUG_ON(bytes < (DEVCONF_MAX * 4)); 537404561c1fSThomas Graf 53751da177e4SLinus Torvalds memset(array, 0, bytes); 53761da177e4SLinus Torvalds array[DEVCONF_FORWARDING] = cnf->forwarding; 53771da177e4SLinus Torvalds array[DEVCONF_HOPLIMIT] = cnf->hop_limit; 53781da177e4SLinus Torvalds array[DEVCONF_MTU6] = cnf->mtu6; 53791da177e4SLinus Torvalds array[DEVCONF_ACCEPT_RA] = cnf->accept_ra; 53801da177e4SLinus Torvalds array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects; 53811da177e4SLinus Torvalds array[DEVCONF_AUTOCONF] = cnf->autoconf; 53821da177e4SLinus Torvalds array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits; 53831da177e4SLinus Torvalds array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits; 538493908d19SThomas Graf array[DEVCONF_RTR_SOLICIT_INTERVAL] = 538593908d19SThomas Graf jiffies_to_msecs(cnf->rtr_solicit_interval); 5386bd11f074SMaciej Żenczykowski array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] = 5387bd11f074SMaciej Żenczykowski jiffies_to_msecs(cnf->rtr_solicit_max_interval); 538893908d19SThomas Graf array[DEVCONF_RTR_SOLICIT_DELAY] = 538993908d19SThomas Graf jiffies_to_msecs(cnf->rtr_solicit_delay); 53901da177e4SLinus Torvalds array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version; 5391fc4eba58SHannes Frederic Sowa array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] = 5392fc4eba58SHannes Frederic Sowa jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval); 5393fc4eba58SHannes Frederic Sowa array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] = 5394fc4eba58SHannes Frederic Sowa jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval); 53951da177e4SLinus Torvalds array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr; 53961da177e4SLinus Torvalds array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft; 53971da177e4SLinus Torvalds array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft; 53981da177e4SLinus Torvalds array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry; 53991da177e4SLinus Torvalds array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor; 54001da177e4SLinus Torvalds array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses; 540165f5c7c1SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr; 54028013d1d7SHangbin Liu array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit; 5403c4fd30ebSYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo; 5404930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 5405930d6ff2SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref; 540693908d19SThomas Graf array[DEVCONF_RTR_PROBE_INTERVAL] = 540793908d19SThomas Graf jiffies_to_msecs(cnf->rtr_probe_interval); 5408fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 5409bbea124bSJoel Scherpelz array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] = cnf->accept_ra_rt_info_min_plen; 541009c884d4SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen; 541109c884d4SYOSHIFUJI Hideaki #endif 5412930d6ff2SYOSHIFUJI Hideaki #endif 5413fbea49e1SYOSHIFUJI Hideaki array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp; 54140bcbc926SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route; 541595c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 541695c385b4SNeil Horman array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad; 54177fd2561eSErik Kline array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic; 541895c385b4SNeil Horman #endif 54197bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 54207bc570c8SYOSHIFUJI Hideaki array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding; 54217bc570c8SYOSHIFUJI Hideaki #endif 5422778d80beSYOSHIFUJI Hideaki array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6; 54231b34be74SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad; 5424c3faca05SCosmin Ratiu array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao; 54255cb04436SHannes Frederic Sowa array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify; 5426b800c3b9SHannes Frederic Sowa array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc; 5427d9333196SBen Greear array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local; 5428c2943f14SHarout Hedeshian array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu; 542935103d11SAndy Gospodarek array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown; 54303d1bec99SHannes Frederic Sowa /* we omit DEVCONF_STABLE_SECRET for now */ 54313985e8a3SErik Kline array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only; 5432abbc3043SJohannes Berg array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast; 54337a02bf89SJohannes Berg array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na; 5434f1705ec1SDavid Ahern array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down; 54351ababebaSDavid Lebrun array[DEVCONF_SEG6_ENABLED] = cnf->seg6_enabled; 5436bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC 5437bf355b8dSDavid Lebrun array[DEVCONF_SEG6_REQUIRE_HMAC] = cnf->seg6_require_hmac; 5438bf355b8dSDavid Lebrun #endif 5439adc176c5SErik Nordmark array[DEVCONF_ENHANCED_DAD] = cnf->enhanced_dad; 5440d35a00b8SFelix Jia array[DEVCONF_ADDR_GEN_MODE] = cnf->addr_gen_mode; 5441df789fe7SDavid Forster array[DEVCONF_DISABLE_POLICY] = cnf->disable_policy; 54422210d6b2SMaciej Żenczykowski array[DEVCONF_NDISC_TCLASS] = cnf->ndisc_tclass; 54431da177e4SLinus Torvalds } 54441da177e4SLinus Torvalds 5445b382b191SThomas Graf static inline size_t inet6_ifla6_size(void) 5446b382b191SThomas Graf { 5447b382b191SThomas Graf return nla_total_size(4) /* IFLA_INET6_FLAGS */ 5448b382b191SThomas Graf + nla_total_size(sizeof(struct ifla_cacheinfo)) 5449b382b191SThomas Graf + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */ 5450b382b191SThomas Graf + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */ 5451f53adae4SDaniel Borkmann + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */ 5452bdd72f41SSabrina Dubroca + nla_total_size(sizeof(struct in6_addr)) /* IFLA_INET6_TOKEN */ 5453bdd72f41SSabrina Dubroca + nla_total_size(1) /* IFLA_INET6_ADDR_GEN_MODE */ 5454bdd72f41SSabrina Dubroca + 0; 5455b382b191SThomas Graf } 5456b382b191SThomas Graf 5457339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void) 5458339bf98fSThomas Graf { 5459339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 5460339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 5461339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 5462339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 5463339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 54640344338bSAndy Gospodarek + nla_total_size(1) /* IFLA_OPERSTATE */ 5465b382b191SThomas Graf + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */ 5466339bf98fSThomas Graf } 5467c5396a31SYOSHIFUJI Hideaki 5468be281e55SEric Dumazet static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib, 5469aca05671SJia He int bytes) 54707f7d9a6bSHerbert Xu { 54717f7d9a6bSHerbert Xu int i; 5472aca05671SJia He int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX; 54737f7d9a6bSHerbert Xu BUG_ON(pad < 0); 54747f7d9a6bSHerbert Xu 54757f7d9a6bSHerbert Xu /* Use put_unaligned() because stats may not be aligned for u64. */ 5476aca05671SJia He put_unaligned(ICMP6_MIB_MAX, &stats[0]); 5477aca05671SJia He for (i = 1; i < ICMP6_MIB_MAX; i++) 5478be281e55SEric Dumazet put_unaligned(atomic_long_read(&mib[i]), &stats[i]); 54797f7d9a6bSHerbert Xu 5480aca05671SJia He memset(&stats[ICMP6_MIB_MAX], 0, pad); 54817f7d9a6bSHerbert Xu } 54827f7d9a6bSHerbert Xu 5483698365faSWANG Cong static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib, 5484a3a77372SRaghavendra K T int bytes, size_t syncpoff) 54854ce3c183SEric Dumazet { 5486a3a77372SRaghavendra K T int i, c; 5487a3a77372SRaghavendra K T u64 buff[IPSTATS_MIB_MAX]; 5488a3a77372SRaghavendra K T int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX; 5489a3a77372SRaghavendra K T 54904ce3c183SEric Dumazet BUG_ON(pad < 0); 54914ce3c183SEric Dumazet 5492a3a77372SRaghavendra K T memset(buff, 0, sizeof(buff)); 5493a3a77372SRaghavendra K T buff[0] = IPSTATS_MIB_MAX; 54944ce3c183SEric Dumazet 5495a3a77372SRaghavendra K T for_each_possible_cpu(c) { 5496a3a77372SRaghavendra K T for (i = 1; i < IPSTATS_MIB_MAX; i++) 5497a3a77372SRaghavendra K T buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff); 5498a3a77372SRaghavendra K T } 5499a3a77372SRaghavendra K T 5500a3a77372SRaghavendra K T memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64)); 5501a3a77372SRaghavendra K T memset(&stats[IPSTATS_MIB_MAX], 0, pad); 55024ce3c183SEric Dumazet } 55034ce3c183SEric Dumazet 55047f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype, 55057f7d9a6bSHerbert Xu int bytes) 55067f7d9a6bSHerbert Xu { 55077f7d9a6bSHerbert Xu switch (attrtype) { 55087f7d9a6bSHerbert Xu case IFLA_INET6_STATS: 5509a3a77372SRaghavendra K T __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes, 5510a3a77372SRaghavendra K T offsetof(struct ipstats_mib, syncp)); 55117f7d9a6bSHerbert Xu break; 55127f7d9a6bSHerbert Xu case IFLA_INET6_ICMP6STATS: 5513aca05671SJia He __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes); 55147f7d9a6bSHerbert Xu break; 55157f7d9a6bSHerbert Xu } 55167f7d9a6bSHerbert Xu } 55177f7d9a6bSHerbert Xu 5518d5566fd7SSowmini Varadhan static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev, 5519d5566fd7SSowmini Varadhan u32 ext_filter_mask) 55201da177e4SLinus Torvalds { 5521bf99f1bdSYOSHIFUJI Hideaki struct nlattr *nla; 55221da177e4SLinus Torvalds struct ifla_cacheinfo ci; 55231da177e4SLinus Torvalds 5524c78679e8SDavid S. Miller if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags)) 5525c78679e8SDavid S. Miller goto nla_put_failure; 55261da177e4SLinus Torvalds ci.max_reasm_len = IPV6_MAXPLEN; 552724912420SDavid S. Miller ci.tstamp = cstamp_delta(idev->tstamp); 552824912420SDavid S. Miller ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time); 55291f9248e5SJiri Pirko ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME)); 5530c78679e8SDavid S. Miller if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci)) 5531c78679e8SDavid S. Miller goto nla_put_failure; 5532bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32)); 553363159f29SIan Morris if (!nla) 553404561c1fSThomas Graf goto nla_put_failure; 5535bf99f1bdSYOSHIFUJI Hideaki ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla)); 55361da177e4SLinus Torvalds 5537bf99f1bdSYOSHIFUJI Hideaki /* XXX - MC not implemented */ 5538bf99f1bdSYOSHIFUJI Hideaki 5539d5566fd7SSowmini Varadhan if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS) 5540d5566fd7SSowmini Varadhan return 0; 5541d5566fd7SSowmini Varadhan 5542bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64)); 554363159f29SIan Morris if (!nla) 5544bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 5545bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla)); 5546bf99f1bdSYOSHIFUJI Hideaki 5547bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64)); 554863159f29SIan Morris if (!nla) 5549bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 5550bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla)); 55511da177e4SLinus Torvalds 5552f53adae4SDaniel Borkmann nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr)); 555363159f29SIan Morris if (!nla) 5554f53adae4SDaniel Borkmann goto nla_put_failure; 5555f53adae4SDaniel Borkmann read_lock_bh(&idev->lock); 5556f53adae4SDaniel Borkmann memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla)); 5557f53adae4SDaniel Borkmann read_unlock_bh(&idev->lock); 5558f53adae4SDaniel Borkmann 55590d7982ceSNicolas Dichtel if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->cnf.addr_gen_mode)) 55600d7982ceSNicolas Dichtel goto nla_put_failure; 55610d7982ceSNicolas Dichtel 5562b382b191SThomas Graf return 0; 5563b382b191SThomas Graf 5564b382b191SThomas Graf nla_put_failure: 5565b382b191SThomas Graf return -EMSGSIZE; 5566b382b191SThomas Graf } 5567b382b191SThomas Graf 5568b1974ed0SArad, Ronen static size_t inet6_get_link_af_size(const struct net_device *dev, 5569b1974ed0SArad, Ronen u32 ext_filter_mask) 5570b382b191SThomas Graf { 5571b382b191SThomas Graf if (!__in6_dev_get(dev)) 5572b382b191SThomas Graf return 0; 5573b382b191SThomas Graf 5574b382b191SThomas Graf return inet6_ifla6_size(); 5575b382b191SThomas Graf } 5576b382b191SThomas Graf 5577d5566fd7SSowmini Varadhan static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev, 5578d5566fd7SSowmini Varadhan u32 ext_filter_mask) 5579b382b191SThomas Graf { 5580b382b191SThomas Graf struct inet6_dev *idev = __in6_dev_get(dev); 5581b382b191SThomas Graf 5582b382b191SThomas Graf if (!idev) 5583b382b191SThomas Graf return -ENODATA; 5584b382b191SThomas Graf 5585d5566fd7SSowmini Varadhan if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0) 5586b382b191SThomas Graf return -EMSGSIZE; 5587b382b191SThomas Graf 5588b382b191SThomas Graf return 0; 5589b382b191SThomas Graf } 5590b382b191SThomas Graf 5591f53adae4SDaniel Borkmann static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token) 5592f53adae4SDaniel Borkmann { 5593f53adae4SDaniel Borkmann struct inet6_ifaddr *ifp; 5594f53adae4SDaniel Borkmann struct net_device *dev = idev->dev; 559547e27d5eSDaniel Borkmann bool clear_token, update_rs = false; 5596dc848292SHannes Frederic Sowa struct in6_addr ll_addr; 5597f53adae4SDaniel Borkmann 5598c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 5599c15b1ccaSHannes Frederic Sowa 560063159f29SIan Morris if (!token) 5601f53adae4SDaniel Borkmann return -EINVAL; 5602f53adae4SDaniel Borkmann if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) 5603f53adae4SDaniel Borkmann return -EINVAL; 5604f53adae4SDaniel Borkmann if (!ipv6_accept_ra(idev)) 5605f53adae4SDaniel Borkmann return -EINVAL; 5606bd11f074SMaciej Żenczykowski if (idev->cnf.rtr_solicits == 0) 5607f53adae4SDaniel Borkmann return -EINVAL; 5608f53adae4SDaniel Borkmann 5609f53adae4SDaniel Borkmann write_lock_bh(&idev->lock); 5610f53adae4SDaniel Borkmann 5611f53adae4SDaniel Borkmann BUILD_BUG_ON(sizeof(token->s6_addr) != 16); 5612f53adae4SDaniel Borkmann memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8); 5613f53adae4SDaniel Borkmann 5614f53adae4SDaniel Borkmann write_unlock_bh(&idev->lock); 5615f53adae4SDaniel Borkmann 561647e27d5eSDaniel Borkmann clear_token = ipv6_addr_any(token); 561747e27d5eSDaniel Borkmann if (clear_token) 561847e27d5eSDaniel Borkmann goto update_lft; 561947e27d5eSDaniel Borkmann 5620dc848292SHannes Frederic Sowa if (!idev->dead && (idev->if_flags & IF_READY) && 5621dc848292SHannes Frederic Sowa !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE | 5622dc848292SHannes Frederic Sowa IFA_F_OPTIMISTIC)) { 5623fc403832SDaniel Borkmann /* If we're not ready, then normal ifup will take care 5624fc403832SDaniel Borkmann * of this. Otherwise, we need to request our rs here. 5625fc403832SDaniel Borkmann */ 5626f53adae4SDaniel Borkmann ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters); 5627fc403832SDaniel Borkmann update_rs = true; 5628fc403832SDaniel Borkmann } 5629f53adae4SDaniel Borkmann 563047e27d5eSDaniel Borkmann update_lft: 5631f53adae4SDaniel Borkmann write_lock_bh(&idev->lock); 5632fc403832SDaniel Borkmann 563377ecaaceSHannes Frederic Sowa if (update_rs) { 5634f53adae4SDaniel Borkmann idev->if_flags |= IF_RS_SENT; 5635bd11f074SMaciej Żenczykowski idev->rs_interval = rfc3315_s14_backoff_init( 5636bd11f074SMaciej Żenczykowski idev->cnf.rtr_solicit_interval); 563777ecaaceSHannes Frederic Sowa idev->rs_probes = 1; 5638bd11f074SMaciej Żenczykowski addrconf_mod_rs_timer(idev, idev->rs_interval); 563977ecaaceSHannes Frederic Sowa } 5640f53adae4SDaniel Borkmann 5641f53adae4SDaniel Borkmann /* Well, that's kinda nasty ... */ 5642f53adae4SDaniel Borkmann list_for_each_entry(ifp, &idev->addr_list, if_list) { 5643f53adae4SDaniel Borkmann spin_lock(&ifp->lock); 5644617fe29dSDaniel Borkmann if (ifp->tokenized) { 5645f53adae4SDaniel Borkmann ifp->valid_lft = 0; 5646f53adae4SDaniel Borkmann ifp->prefered_lft = 0; 5647f53adae4SDaniel Borkmann } 5648f53adae4SDaniel Borkmann spin_unlock(&ifp->lock); 5649f53adae4SDaniel Borkmann } 5650f53adae4SDaniel Borkmann 5651f53adae4SDaniel Borkmann write_unlock_bh(&idev->lock); 5652b2ed64a9SLubomir Rintel inet6_ifinfo_notify(RTM_NEWLINK, idev); 5653c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 5654f53adae4SDaniel Borkmann return 0; 5655f53adae4SDaniel Borkmann } 5656f53adae4SDaniel Borkmann 565711b1f828SDaniel Borkmann static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = { 565811b1f828SDaniel Borkmann [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 }, 565911b1f828SDaniel Borkmann [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) }, 566011b1f828SDaniel Borkmann }; 566111b1f828SDaniel Borkmann 5662d35a00b8SFelix Jia static int check_addr_gen_mode(int mode) 5663d35a00b8SFelix Jia { 5664d35a00b8SFelix Jia if (mode != IN6_ADDR_GEN_MODE_EUI64 && 5665d35a00b8SFelix Jia mode != IN6_ADDR_GEN_MODE_NONE && 5666d35a00b8SFelix Jia mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY && 5667d35a00b8SFelix Jia mode != IN6_ADDR_GEN_MODE_RANDOM) 5668d35a00b8SFelix Jia return -EINVAL; 5669d35a00b8SFelix Jia return 1; 5670d35a00b8SFelix Jia } 5671d35a00b8SFelix Jia 5672d35a00b8SFelix Jia static int check_stable_privacy(struct inet6_dev *idev, struct net *net, 5673d35a00b8SFelix Jia int mode) 5674d35a00b8SFelix Jia { 5675d35a00b8SFelix Jia if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY && 5676d35a00b8SFelix Jia !idev->cnf.stable_secret.initialized && 5677d35a00b8SFelix Jia !net->ipv6.devconf_dflt->stable_secret.initialized) 5678d35a00b8SFelix Jia return -EINVAL; 5679d35a00b8SFelix Jia return 1; 5680d35a00b8SFelix Jia } 5681d35a00b8SFelix Jia 56827dc2bccaSMaxim Mikityanskiy static int inet6_validate_link_af(const struct net_device *dev, 56837dc2bccaSMaxim Mikityanskiy const struct nlattr *nla) 5684f53adae4SDaniel Borkmann { 5685f53adae4SDaniel Borkmann struct nlattr *tb[IFLA_INET6_MAX + 1]; 56867dc2bccaSMaxim Mikityanskiy struct inet6_dev *idev = NULL; 56877dc2bccaSMaxim Mikityanskiy int err; 5688f53adae4SDaniel Borkmann 56897dc2bccaSMaxim Mikityanskiy if (dev) { 56907dc2bccaSMaxim Mikityanskiy idev = __in6_dev_get(dev); 5691f53adae4SDaniel Borkmann if (!idev) 5692f53adae4SDaniel Borkmann return -EAFNOSUPPORT; 56937dc2bccaSMaxim Mikityanskiy } 56947dc2bccaSMaxim Mikityanskiy 56957dc2bccaSMaxim Mikityanskiy err = nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla, 56967dc2bccaSMaxim Mikityanskiy inet6_af_policy, NULL); 56977dc2bccaSMaxim Mikityanskiy if (err) 56987dc2bccaSMaxim Mikityanskiy return err; 56997dc2bccaSMaxim Mikityanskiy 57007dc2bccaSMaxim Mikityanskiy if (!tb[IFLA_INET6_TOKEN] && !tb[IFLA_INET6_ADDR_GEN_MODE]) 57017dc2bccaSMaxim Mikityanskiy return -EINVAL; 57027dc2bccaSMaxim Mikityanskiy 57037dc2bccaSMaxim Mikityanskiy if (tb[IFLA_INET6_ADDR_GEN_MODE]) { 57047dc2bccaSMaxim Mikityanskiy u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]); 57057dc2bccaSMaxim Mikityanskiy 57067dc2bccaSMaxim Mikityanskiy if (check_addr_gen_mode(mode) < 0) 57077dc2bccaSMaxim Mikityanskiy return -EINVAL; 57087dc2bccaSMaxim Mikityanskiy if (dev && check_stable_privacy(idev, dev_net(dev), mode) < 0) 57097dc2bccaSMaxim Mikityanskiy return -EINVAL; 57107dc2bccaSMaxim Mikityanskiy } 57117dc2bccaSMaxim Mikityanskiy 57127dc2bccaSMaxim Mikityanskiy return 0; 57137dc2bccaSMaxim Mikityanskiy } 57147dc2bccaSMaxim Mikityanskiy 57157dc2bccaSMaxim Mikityanskiy static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla) 57167dc2bccaSMaxim Mikityanskiy { 57177dc2bccaSMaxim Mikityanskiy struct inet6_dev *idev = __in6_dev_get(dev); 57187dc2bccaSMaxim Mikityanskiy struct nlattr *tb[IFLA_INET6_MAX + 1]; 57197dc2bccaSMaxim Mikityanskiy int err; 5720f53adae4SDaniel Borkmann 57218cb08174SJohannes Berg if (nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla, NULL, NULL) < 0) 5722f53adae4SDaniel Borkmann BUG(); 5723f53adae4SDaniel Borkmann 5724bc91b0f0SJiri Pirko if (tb[IFLA_INET6_TOKEN]) { 5725f53adae4SDaniel Borkmann err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN])); 5726bc91b0f0SJiri Pirko if (err) 5727bc91b0f0SJiri Pirko return err; 5728bc91b0f0SJiri Pirko } 5729bc91b0f0SJiri Pirko 5730bc91b0f0SJiri Pirko if (tb[IFLA_INET6_ADDR_GEN_MODE]) { 5731bc91b0f0SJiri Pirko u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]); 5732bc91b0f0SJiri Pirko 5733d35a00b8SFelix Jia idev->cnf.addr_gen_mode = mode; 5734bc91b0f0SJiri Pirko } 5735f53adae4SDaniel Borkmann 57367dc2bccaSMaxim Mikityanskiy return 0; 5737f53adae4SDaniel Borkmann } 5738f53adae4SDaniel Borkmann 5739b382b191SThomas Graf static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev, 574015e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 5741b382b191SThomas Graf { 5742b382b191SThomas Graf struct net_device *dev = idev->dev; 5743b382b191SThomas Graf struct ifinfomsg *hdr; 5744b382b191SThomas Graf struct nlmsghdr *nlh; 5745b382b191SThomas Graf void *protoinfo; 5746b382b191SThomas Graf 574715e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags); 574863159f29SIan Morris if (!nlh) 5749b382b191SThomas Graf return -EMSGSIZE; 5750b382b191SThomas Graf 5751b382b191SThomas Graf hdr = nlmsg_data(nlh); 5752b382b191SThomas Graf hdr->ifi_family = AF_INET6; 5753b382b191SThomas Graf hdr->__ifi_pad = 0; 5754b382b191SThomas Graf hdr->ifi_type = dev->type; 5755b382b191SThomas Graf hdr->ifi_index = dev->ifindex; 5756b382b191SThomas Graf hdr->ifi_flags = dev_get_flags(dev); 5757b382b191SThomas Graf hdr->ifi_change = 0; 5758b382b191SThomas Graf 5759c78679e8SDavid S. Miller if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 5760c78679e8SDavid S. Miller (dev->addr_len && 5761c78679e8SDavid S. Miller nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 5762c78679e8SDavid S. Miller nla_put_u32(skb, IFLA_MTU, dev->mtu) || 5763a54acb3aSNicolas Dichtel (dev->ifindex != dev_get_iflink(dev) && 57640344338bSAndy Gospodarek nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) || 57650344338bSAndy Gospodarek nla_put_u8(skb, IFLA_OPERSTATE, 57660344338bSAndy Gospodarek netif_running(dev) ? dev->operstate : IF_OPER_DOWN)) 5767c78679e8SDavid S. Miller goto nla_put_failure; 5768ae0be8deSMichal Kubecek protoinfo = nla_nest_start_noflag(skb, IFLA_PROTINFO); 576963159f29SIan Morris if (!protoinfo) 5770b382b191SThomas Graf goto nla_put_failure; 5771b382b191SThomas Graf 5772d5566fd7SSowmini Varadhan if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0) 5773b382b191SThomas Graf goto nla_put_failure; 5774b382b191SThomas Graf 577504561c1fSThomas Graf nla_nest_end(skb, protoinfo); 5776053c095aSJohannes Berg nlmsg_end(skb, nlh); 5777053c095aSJohannes Berg return 0; 57781da177e4SLinus Torvalds 577904561c1fSThomas Graf nla_put_failure: 578026932566SPatrick McHardy nlmsg_cancel(skb, nlh); 578126932566SPatrick McHardy return -EMSGSIZE; 57821da177e4SLinus Torvalds } 57831da177e4SLinus Torvalds 5784786e0007SDavid Ahern static int inet6_valid_dump_ifinfo(const struct nlmsghdr *nlh, 5785786e0007SDavid Ahern struct netlink_ext_ack *extack) 5786786e0007SDavid Ahern { 5787786e0007SDavid Ahern struct ifinfomsg *ifm; 5788786e0007SDavid Ahern 5789786e0007SDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 5790786e0007SDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Invalid header for link dump request"); 5791786e0007SDavid Ahern return -EINVAL; 5792786e0007SDavid Ahern } 5793786e0007SDavid Ahern 5794786e0007SDavid Ahern if (nlmsg_attrlen(nlh, sizeof(*ifm))) { 5795786e0007SDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Invalid data after header"); 5796786e0007SDavid Ahern return -EINVAL; 5797786e0007SDavid Ahern } 5798786e0007SDavid Ahern 5799786e0007SDavid Ahern ifm = nlmsg_data(nlh); 5800786e0007SDavid Ahern if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 5801786e0007SDavid Ahern ifm->ifi_change || ifm->ifi_index) { 5802786e0007SDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for dump request"); 5803786e0007SDavid Ahern return -EINVAL; 5804786e0007SDavid Ahern } 5805786e0007SDavid Ahern 5806786e0007SDavid Ahern return 0; 5807786e0007SDavid Ahern } 5808786e0007SDavid Ahern 58091da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 58101da177e4SLinus Torvalds { 58113b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 581284d2697dSEric Dumazet int h, s_h; 5813434a8a58SDavid S. Miller int idx = 0, s_idx; 58141da177e4SLinus Torvalds struct net_device *dev; 58151da177e4SLinus Torvalds struct inet6_dev *idev; 581684d2697dSEric Dumazet struct hlist_head *head; 58171da177e4SLinus Torvalds 5818786e0007SDavid Ahern /* only requests using strict checking can pass data to 5819786e0007SDavid Ahern * influence the dump 5820786e0007SDavid Ahern */ 5821786e0007SDavid Ahern if (cb->strict_check) { 5822786e0007SDavid Ahern int err = inet6_valid_dump_ifinfo(cb->nlh, cb->extack); 5823786e0007SDavid Ahern 5824786e0007SDavid Ahern if (err < 0) 5825786e0007SDavid Ahern return err; 5826786e0007SDavid Ahern } 5827786e0007SDavid Ahern 582884d2697dSEric Dumazet s_h = cb->args[0]; 582984d2697dSEric Dumazet s_idx = cb->args[1]; 583084d2697dSEric Dumazet 583184d2697dSEric Dumazet rcu_read_lock(); 583284d2697dSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 58337562f876SPavel Emelianov idx = 0; 583484d2697dSEric Dumazet head = &net->dev_index_head[h]; 5835b67bfe0dSSasha Levin hlist_for_each_entry_rcu(dev, head, index_hlist) { 58361da177e4SLinus Torvalds if (idx < s_idx) 58377562f876SPavel Emelianov goto cont; 583884d2697dSEric Dumazet idev = __in6_dev_get(dev); 583984d2697dSEric Dumazet if (!idev) 58407562f876SPavel Emelianov goto cont; 584184d2697dSEric Dumazet if (inet6_fill_ifinfo(skb, idev, 584215e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 584384d2697dSEric Dumazet cb->nlh->nlmsg_seq, 5844053c095aSJohannes Berg RTM_NEWLINK, NLM_F_MULTI) < 0) 584584d2697dSEric Dumazet goto out; 58467562f876SPavel Emelianov cont: 58477562f876SPavel Emelianov idx++; 58481da177e4SLinus Torvalds } 584984d2697dSEric Dumazet } 585084d2697dSEric Dumazet out: 585184d2697dSEric Dumazet rcu_read_unlock(); 585284d2697dSEric Dumazet cb->args[1] = idx; 585384d2697dSEric Dumazet cb->args[0] = h; 58541da177e4SLinus Torvalds 58551da177e4SLinus Torvalds return skb->len; 58561da177e4SLinus Torvalds } 58571da177e4SLinus Torvalds 58581da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev) 58591da177e4SLinus Torvalds { 58601da177e4SLinus Torvalds struct sk_buff *skb; 5861c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 58628d7a76c9SThomas Graf int err = -ENOBUFS; 58631da177e4SLinus Torvalds 5864339bf98fSThomas Graf skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC); 586563159f29SIan Morris if (!skb) 58668d7a76c9SThomas Graf goto errout; 58678d7a76c9SThomas Graf 58688d7a76c9SThomas Graf err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0); 586926932566SPatrick McHardy if (err < 0) { 587026932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */ 587126932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 587226932566SPatrick McHardy kfree_skb(skb); 587326932566SPatrick McHardy goto errout; 587426932566SPatrick McHardy } 58755f75a104SNicolas Dichtel rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC); 58761ce85fe4SPablo Neira Ayuso return; 58778d7a76c9SThomas Graf errout: 58788d7a76c9SThomas Graf if (err < 0) 58795f75a104SNicolas Dichtel rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err); 58801da177e4SLinus Torvalds } 58811da177e4SLinus Torvalds 5882339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void) 5883339bf98fSThomas Graf { 5884339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct prefixmsg)) 5885339bf98fSThomas Graf + nla_total_size(sizeof(struct in6_addr)) 5886339bf98fSThomas Graf + nla_total_size(sizeof(struct prefix_cacheinfo)); 5887339bf98fSThomas Graf } 5888c5396a31SYOSHIFUJI Hideaki 58891da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev, 589015e47304SEric W. Biederman struct prefix_info *pinfo, u32 portid, u32 seq, 5891b6544c0bSJamal Hadi Salim int event, unsigned int flags) 58921da177e4SLinus Torvalds { 58931da177e4SLinus Torvalds struct prefixmsg *pmsg; 58941da177e4SLinus Torvalds struct nlmsghdr *nlh; 58951da177e4SLinus Torvalds struct prefix_cacheinfo ci; 58961da177e4SLinus Torvalds 589715e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags); 589863159f29SIan Morris if (!nlh) 589926932566SPatrick McHardy return -EMSGSIZE; 59006051e2f4SThomas Graf 59016051e2f4SThomas Graf pmsg = nlmsg_data(nlh); 59021da177e4SLinus Torvalds pmsg->prefix_family = AF_INET6; 59038a47077aSPatrick McHardy pmsg->prefix_pad1 = 0; 59048a47077aSPatrick McHardy pmsg->prefix_pad2 = 0; 59051da177e4SLinus Torvalds pmsg->prefix_ifindex = idev->dev->ifindex; 59061da177e4SLinus Torvalds pmsg->prefix_len = pinfo->prefix_len; 59071da177e4SLinus Torvalds pmsg->prefix_type = pinfo->type; 59088a47077aSPatrick McHardy pmsg->prefix_pad3 = 0; 59091da177e4SLinus Torvalds pmsg->prefix_flags = 0; 59101da177e4SLinus Torvalds if (pinfo->onlink) 59111da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_ONLINK; 59121da177e4SLinus Torvalds if (pinfo->autoconf) 59131da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_AUTOCONF; 59141da177e4SLinus Torvalds 5915c78679e8SDavid S. Miller if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix)) 5916c78679e8SDavid S. Miller goto nla_put_failure; 59171da177e4SLinus Torvalds ci.preferred_time = ntohl(pinfo->prefered); 59181da177e4SLinus Torvalds ci.valid_time = ntohl(pinfo->valid); 5919c78679e8SDavid S. Miller if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci)) 5920c78679e8SDavid S. Miller goto nla_put_failure; 5921053c095aSJohannes Berg nlmsg_end(skb, nlh); 5922053c095aSJohannes Berg return 0; 59231da177e4SLinus Torvalds 59246051e2f4SThomas Graf nla_put_failure: 592526932566SPatrick McHardy nlmsg_cancel(skb, nlh); 592626932566SPatrick McHardy return -EMSGSIZE; 59271da177e4SLinus Torvalds } 59281da177e4SLinus Torvalds 59291da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev, 59301da177e4SLinus Torvalds struct prefix_info *pinfo) 59311da177e4SLinus Torvalds { 59321da177e4SLinus Torvalds struct sk_buff *skb; 5933c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 59348c384bfaSThomas Graf int err = -ENOBUFS; 59351da177e4SLinus Torvalds 5936339bf98fSThomas Graf skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC); 593763159f29SIan Morris if (!skb) 59388c384bfaSThomas Graf goto errout; 59398c384bfaSThomas Graf 59408c384bfaSThomas Graf err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0); 594126932566SPatrick McHardy if (err < 0) { 594226932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */ 594326932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 594426932566SPatrick McHardy kfree_skb(skb); 594526932566SPatrick McHardy goto errout; 594626932566SPatrick McHardy } 59471ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC); 59481ce85fe4SPablo Neira Ayuso return; 59498c384bfaSThomas Graf errout: 59508c384bfaSThomas Graf if (err < 0) 59516fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err); 59521da177e4SLinus Torvalds } 59531da177e4SLinus Torvalds 59541da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 59551da177e4SLinus Torvalds { 595663998ac2SNicolas Dichtel struct net *net = dev_net(ifp->idev->dev); 595763998ac2SNicolas Dichtel 5958c15b1ccaSHannes Frederic Sowa if (event) 5959c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 5960c15b1ccaSHannes Frederic Sowa 59611da177e4SLinus Torvalds inet6_ifa_notify(event ? : RTM_NEWADDR, ifp); 59621da177e4SLinus Torvalds 59631da177e4SLinus Torvalds switch (event) { 59641da177e4SLinus Torvalds case RTM_NEWADDR: 596595c385b4SNeil Horman /* 59662d819d25SDavid Ahern * If the address was optimistic we inserted the route at the 59672d819d25SDavid Ahern * start of our DAD process, so we don't need to do it again. 59682d819d25SDavid Ahern * If the device was taken down in the middle of the DAD 59692d819d25SDavid Ahern * cycle there is a race where we could get here without a 59702d819d25SDavid Ahern * host route, so nothing to insert. That will be fixed when 59712d819d25SDavid Ahern * the device is brought up. 597295c385b4SNeil Horman */ 59732d819d25SDavid Ahern if (ifp->rt && !rcu_access_pointer(ifp->rt->fib6_node)) { 5974afb1d4b5SDavid Ahern ip6_ins_rt(net, ifp->rt); 59752d819d25SDavid Ahern } else if (!ifp->rt && (ifp->idev->dev->flags & IFF_UP)) { 59762d819d25SDavid Ahern pr_warn("BUG: Address %pI6c on device %s is missing its host route.\n", 59772d819d25SDavid Ahern &ifp->addr, ifp->idev->dev->name); 59782d819d25SDavid Ahern } 59792d819d25SDavid Ahern 59801da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 59811da177e4SLinus Torvalds addrconf_join_anycast(ifp); 59827996c799SCong Wang if (!ipv6_addr_any(&ifp->peer_addr)) 59838308f3ffSDavid Ahern addrconf_prefix_route(&ifp->peer_addr, 128, 0, 5984acb54e3cSDavid Ahern ifp->idev->dev, 0, 0, 598527b10608SDavid Ahern GFP_ATOMIC); 59861da177e4SLinus Torvalds break; 59871da177e4SLinus Torvalds case RTM_DELADDR: 59881da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 59891da177e4SLinus Torvalds addrconf_leave_anycast(ifp); 59901da177e4SLinus Torvalds addrconf_leave_solict(ifp->idev, &ifp->addr); 59917996c799SCong Wang if (!ipv6_addr_any(&ifp->peer_addr)) { 59928d1c802bSDavid Ahern struct fib6_info *rt; 5993caeaba79SNicolas Dichtel 5994e7478dfcSNicolas Dichtel rt = addrconf_get_prefix_route(&ifp->peer_addr, 128, 59952b2450caSDavid Ahern ifp->idev->dev, 0, 0, 59962b2450caSDavid Ahern false); 59978e3d5be7SMartin KaFai Lau if (rt) 5998afb1d4b5SDavid Ahern ip6_del_rt(net, rt); 5999caeaba79SNicolas Dichtel } 600038bd10c4SDavid Ahern if (ifp->rt) { 6001afb1d4b5SDavid Ahern ip6_del_rt(net, ifp->rt); 600293531c67SDavid Ahern ifp->rt = NULL; 600338bd10c4SDavid Ahern } 6004705f1c86SHannes Frederic Sowa rt_genid_bump_ipv6(net); 60051da177e4SLinus Torvalds break; 60061da177e4SLinus Torvalds } 600763998ac2SNicolas Dichtel atomic_inc(&net->ipv6.dev_addr_genid); 60081da177e4SLinus Torvalds } 60091da177e4SLinus Torvalds 60101da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 60111da177e4SLinus Torvalds { 60128814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 60131da177e4SLinus Torvalds if (likely(ifp->idev->dead == 0)) 60141da177e4SLinus Torvalds __ipv6_ifa_notify(event, ifp); 60158814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 60161da177e4SLinus Torvalds } 60171da177e4SLinus Torvalds 60181da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 60191da177e4SLinus Torvalds 60201da177e4SLinus Torvalds static 6021fe2c6338SJoe Perches int addrconf_sysctl_forward(struct ctl_table *ctl, int write, 60221da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 60231da177e4SLinus Torvalds { 60241da177e4SLinus Torvalds int *valp = ctl->data; 60251da177e4SLinus Torvalds int val = *valp; 602688af182eSEric W. Biederman loff_t pos = *ppos; 6027fe2c6338SJoe Perches struct ctl_table lctl; 60281da177e4SLinus Torvalds int ret; 60291da177e4SLinus Torvalds 6030013d97e9SFrancesco Ruggeri /* 6031013d97e9SFrancesco Ruggeri * ctl->data points to idev->cnf.forwarding, we should 6032013d97e9SFrancesco Ruggeri * not modify it until we get the rtnl lock. 6033013d97e9SFrancesco Ruggeri */ 6034013d97e9SFrancesco Ruggeri lctl = *ctl; 6035013d97e9SFrancesco Ruggeri lctl.data = &val; 6036013d97e9SFrancesco Ruggeri 6037013d97e9SFrancesco Ruggeri ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 60381da177e4SLinus Torvalds 6039c8fecf22SPavel Emelyanov if (write) 6040b325fddbSStephen Hemminger ret = addrconf_fixup_forwarding(ctl, valp, val); 604188af182eSEric W. Biederman if (ret) 604288af182eSEric W. Biederman *ppos = pos; 60431da177e4SLinus Torvalds return ret; 60441da177e4SLinus Torvalds } 60451da177e4SLinus Torvalds 604677751427SMarcelo Leitner static 604777751427SMarcelo Leitner int addrconf_sysctl_mtu(struct ctl_table *ctl, int write, 604877751427SMarcelo Leitner void __user *buffer, size_t *lenp, loff_t *ppos) 604977751427SMarcelo Leitner { 605077751427SMarcelo Leitner struct inet6_dev *idev = ctl->extra1; 605177751427SMarcelo Leitner int min_mtu = IPV6_MIN_MTU; 605277751427SMarcelo Leitner struct ctl_table lctl; 605377751427SMarcelo Leitner 605477751427SMarcelo Leitner lctl = *ctl; 605577751427SMarcelo Leitner lctl.extra1 = &min_mtu; 605677751427SMarcelo Leitner lctl.extra2 = idev ? &idev->dev->mtu : NULL; 605777751427SMarcelo Leitner 605877751427SMarcelo Leitner return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos); 605977751427SMarcelo Leitner } 606077751427SMarcelo Leitner 606156d417b1SBrian Haley static void dev_disable_change(struct inet6_dev *idev) 606256d417b1SBrian Haley { 606375538c2bSCong Wang struct netdev_notifier_info info; 606475538c2bSCong Wang 606556d417b1SBrian Haley if (!idev || !idev->dev) 606656d417b1SBrian Haley return; 606756d417b1SBrian Haley 606875538c2bSCong Wang netdev_notifier_info_init(&info, idev->dev); 606956d417b1SBrian Haley if (idev->cnf.disable_ipv6) 607075538c2bSCong Wang addrconf_notify(NULL, NETDEV_DOWN, &info); 607156d417b1SBrian Haley else 607275538c2bSCong Wang addrconf_notify(NULL, NETDEV_UP, &info); 607356d417b1SBrian Haley } 607456d417b1SBrian Haley 607556d417b1SBrian Haley static void addrconf_disable_change(struct net *net, __s32 newf) 607656d417b1SBrian Haley { 607756d417b1SBrian Haley struct net_device *dev; 607856d417b1SBrian Haley struct inet6_dev *idev; 607956d417b1SBrian Haley 608003e4deffSKefeng Wang for_each_netdev(net, dev) { 608156d417b1SBrian Haley idev = __in6_dev_get(dev); 608256d417b1SBrian Haley if (idev) { 608356d417b1SBrian Haley int changed = (!idev->cnf.disable_ipv6) ^ (!newf); 608456d417b1SBrian Haley idev->cnf.disable_ipv6 = newf; 608556d417b1SBrian Haley if (changed) 608656d417b1SBrian Haley dev_disable_change(idev); 608756d417b1SBrian Haley } 608856d417b1SBrian Haley } 608956d417b1SBrian Haley } 609056d417b1SBrian Haley 6091013d97e9SFrancesco Ruggeri static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf) 609256d417b1SBrian Haley { 609356d417b1SBrian Haley struct net *net; 6094013d97e9SFrancesco Ruggeri int old; 6095013d97e9SFrancesco Ruggeri 6096013d97e9SFrancesco Ruggeri if (!rtnl_trylock()) 6097013d97e9SFrancesco Ruggeri return restart_syscall(); 609856d417b1SBrian Haley 609956d417b1SBrian Haley net = (struct net *)table->extra2; 6100013d97e9SFrancesco Ruggeri old = *p; 6101013d97e9SFrancesco Ruggeri *p = newf; 610256d417b1SBrian Haley 6103013d97e9SFrancesco Ruggeri if (p == &net->ipv6.devconf_dflt->disable_ipv6) { 6104013d97e9SFrancesco Ruggeri rtnl_unlock(); 610556d417b1SBrian Haley return 0; 610688af182eSEric W. Biederman } 610756d417b1SBrian Haley 610856d417b1SBrian Haley if (p == &net->ipv6.devconf_all->disable_ipv6) { 610956d417b1SBrian Haley net->ipv6.devconf_dflt->disable_ipv6 = newf; 611056d417b1SBrian Haley addrconf_disable_change(net, newf); 6111013d97e9SFrancesco Ruggeri } else if ((!newf) ^ (!old)) 611256d417b1SBrian Haley dev_disable_change((struct inet6_dev *)table->extra1); 611356d417b1SBrian Haley 611456d417b1SBrian Haley rtnl_unlock(); 611556d417b1SBrian Haley return 0; 611656d417b1SBrian Haley } 611756d417b1SBrian Haley 611856d417b1SBrian Haley static 6119fe2c6338SJoe Perches int addrconf_sysctl_disable(struct ctl_table *ctl, int write, 612056d417b1SBrian Haley void __user *buffer, size_t *lenp, loff_t *ppos) 612156d417b1SBrian Haley { 612256d417b1SBrian Haley int *valp = ctl->data; 612356d417b1SBrian Haley int val = *valp; 612488af182eSEric W. Biederman loff_t pos = *ppos; 6125fe2c6338SJoe Perches struct ctl_table lctl; 612656d417b1SBrian Haley int ret; 612756d417b1SBrian Haley 6128013d97e9SFrancesco Ruggeri /* 6129013d97e9SFrancesco Ruggeri * ctl->data points to idev->cnf.disable_ipv6, we should 6130013d97e9SFrancesco Ruggeri * not modify it until we get the rtnl lock. 6131013d97e9SFrancesco Ruggeri */ 6132013d97e9SFrancesco Ruggeri lctl = *ctl; 6133013d97e9SFrancesco Ruggeri lctl.data = &val; 6134013d97e9SFrancesco Ruggeri 6135013d97e9SFrancesco Ruggeri ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 613656d417b1SBrian Haley 613756d417b1SBrian Haley if (write) 613856d417b1SBrian Haley ret = addrconf_disable_ipv6(ctl, valp, val); 613988af182eSEric W. Biederman if (ret) 614088af182eSEric W. Biederman *ppos = pos; 614156d417b1SBrian Haley return ret; 614256d417b1SBrian Haley } 614356d417b1SBrian Haley 6144c92d5491Sstephen hemminger static 6145c92d5491Sstephen hemminger int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write, 6146c92d5491Sstephen hemminger void __user *buffer, size_t *lenp, loff_t *ppos) 6147c92d5491Sstephen hemminger { 6148c92d5491Sstephen hemminger int *valp = ctl->data; 6149c92d5491Sstephen hemminger int ret; 6150c92d5491Sstephen hemminger int old, new; 6151c92d5491Sstephen hemminger 6152c92d5491Sstephen hemminger old = *valp; 6153c92d5491Sstephen hemminger ret = proc_dointvec(ctl, write, buffer, lenp, ppos); 6154c92d5491Sstephen hemminger new = *valp; 6155c92d5491Sstephen hemminger 6156c92d5491Sstephen hemminger if (write && old != new) { 6157c92d5491Sstephen hemminger struct net *net = ctl->extra2; 6158c92d5491Sstephen hemminger 6159c92d5491Sstephen hemminger if (!rtnl_trylock()) 6160c92d5491Sstephen hemminger return restart_syscall(); 6161c92d5491Sstephen hemminger 6162c92d5491Sstephen hemminger if (valp == &net->ipv6.devconf_dflt->proxy_ndp) 616385b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 616485b3daadSDavid Ahern NETCONFA_PROXY_NEIGH, 6165c92d5491Sstephen hemminger NETCONFA_IFINDEX_DEFAULT, 6166c92d5491Sstephen hemminger net->ipv6.devconf_dflt); 6167c92d5491Sstephen hemminger else if (valp == &net->ipv6.devconf_all->proxy_ndp) 616885b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 616985b3daadSDavid Ahern NETCONFA_PROXY_NEIGH, 6170c92d5491Sstephen hemminger NETCONFA_IFINDEX_ALL, 6171c92d5491Sstephen hemminger net->ipv6.devconf_all); 6172c92d5491Sstephen hemminger else { 6173c92d5491Sstephen hemminger struct inet6_dev *idev = ctl->extra1; 6174c92d5491Sstephen hemminger 617585b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 617685b3daadSDavid Ahern NETCONFA_PROXY_NEIGH, 6177c92d5491Sstephen hemminger idev->dev->ifindex, 6178c92d5491Sstephen hemminger &idev->cnf); 6179c92d5491Sstephen hemminger } 6180c92d5491Sstephen hemminger rtnl_unlock(); 6181c92d5491Sstephen hemminger } 6182c92d5491Sstephen hemminger 6183c92d5491Sstephen hemminger return ret; 6184c92d5491Sstephen hemminger } 6185c92d5491Sstephen hemminger 6186d35a00b8SFelix Jia static int addrconf_sysctl_addr_gen_mode(struct ctl_table *ctl, int write, 6187d35a00b8SFelix Jia void __user *buffer, size_t *lenp, 6188d35a00b8SFelix Jia loff_t *ppos) 6189d35a00b8SFelix Jia { 6190d35a00b8SFelix Jia int ret = 0; 6191c6dbf7aaSSabrina Dubroca u32 new_val; 6192d35a00b8SFelix Jia struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1; 6193d35a00b8SFelix Jia struct net *net = (struct net *)ctl->extra2; 6194c6dbf7aaSSabrina Dubroca struct ctl_table tmp = { 6195c6dbf7aaSSabrina Dubroca .data = &new_val, 6196c6dbf7aaSSabrina Dubroca .maxlen = sizeof(new_val), 6197c6dbf7aaSSabrina Dubroca .mode = ctl->mode, 6198c6dbf7aaSSabrina Dubroca }; 6199d35a00b8SFelix Jia 62008c171d6cSFelix Jia if (!rtnl_trylock()) 62018c171d6cSFelix Jia return restart_syscall(); 62028c171d6cSFelix Jia 6203c6dbf7aaSSabrina Dubroca new_val = *((u32 *)ctl->data); 6204c6dbf7aaSSabrina Dubroca 6205c6dbf7aaSSabrina Dubroca ret = proc_douintvec(&tmp, write, buffer, lenp, ppos); 6206c6dbf7aaSSabrina Dubroca if (ret != 0) 6207c6dbf7aaSSabrina Dubroca goto out; 6208d35a00b8SFelix Jia 6209d35a00b8SFelix Jia if (write) { 62108c171d6cSFelix Jia if (check_addr_gen_mode(new_val) < 0) { 62118c171d6cSFelix Jia ret = -EINVAL; 62128c171d6cSFelix Jia goto out; 62138c171d6cSFelix Jia } 6214d35a00b8SFelix Jia 6215c6dbf7aaSSabrina Dubroca if (idev) { 62168c171d6cSFelix Jia if (check_stable_privacy(idev, net, new_val) < 0) { 62178c171d6cSFelix Jia ret = -EINVAL; 62188c171d6cSFelix Jia goto out; 62198c171d6cSFelix Jia } 6220d35a00b8SFelix Jia 6221d35a00b8SFelix Jia if (idev->cnf.addr_gen_mode != new_val) { 6222d35a00b8SFelix Jia idev->cnf.addr_gen_mode = new_val; 6223d35a00b8SFelix Jia addrconf_dev_config(idev->dev); 62248c171d6cSFelix Jia } 6225f24c5987SSabrina Dubroca } else if (&net->ipv6.devconf_all->addr_gen_mode == ctl->data) { 6226f24c5987SSabrina Dubroca struct net_device *dev; 6227f24c5987SSabrina Dubroca 6228f24c5987SSabrina Dubroca net->ipv6.devconf_dflt->addr_gen_mode = new_val; 6229f24c5987SSabrina Dubroca for_each_netdev(net, dev) { 6230f24c5987SSabrina Dubroca idev = __in6_dev_get(dev); 6231f24c5987SSabrina Dubroca if (idev && 6232f24c5987SSabrina Dubroca idev->cnf.addr_gen_mode != new_val) { 6233f24c5987SSabrina Dubroca idev->cnf.addr_gen_mode = new_val; 6234f24c5987SSabrina Dubroca addrconf_dev_config(idev->dev); 6235f24c5987SSabrina Dubroca } 6236f24c5987SSabrina Dubroca } 62378c171d6cSFelix Jia } 6238c6dbf7aaSSabrina Dubroca 6239c6dbf7aaSSabrina Dubroca *((u32 *)ctl->data) = new_val; 62408c171d6cSFelix Jia } 62418c171d6cSFelix Jia 62428c171d6cSFelix Jia out: 6243d35a00b8SFelix Jia rtnl_unlock(); 6244d35a00b8SFelix Jia 6245d35a00b8SFelix Jia return ret; 6246d35a00b8SFelix Jia } 6247d35a00b8SFelix Jia 62483d1bec99SHannes Frederic Sowa static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write, 62493d1bec99SHannes Frederic Sowa void __user *buffer, size_t *lenp, 62503d1bec99SHannes Frederic Sowa loff_t *ppos) 62513d1bec99SHannes Frederic Sowa { 62523d1bec99SHannes Frederic Sowa int err; 62533d1bec99SHannes Frederic Sowa struct in6_addr addr; 62543d1bec99SHannes Frederic Sowa char str[IPV6_MAX_STRLEN]; 62553d1bec99SHannes Frederic Sowa struct ctl_table lctl = *ctl; 6256622c81d5SHannes Frederic Sowa struct net *net = ctl->extra2; 62573d1bec99SHannes Frederic Sowa struct ipv6_stable_secret *secret = ctl->data; 62583d1bec99SHannes Frederic Sowa 6259622c81d5SHannes Frederic Sowa if (&net->ipv6.devconf_all->stable_secret == ctl->data) 6260622c81d5SHannes Frederic Sowa return -EIO; 6261622c81d5SHannes Frederic Sowa 62623d1bec99SHannes Frederic Sowa lctl.maxlen = IPV6_MAX_STRLEN; 62633d1bec99SHannes Frederic Sowa lctl.data = str; 62643d1bec99SHannes Frederic Sowa 62653d1bec99SHannes Frederic Sowa if (!rtnl_trylock()) 62663d1bec99SHannes Frederic Sowa return restart_syscall(); 62673d1bec99SHannes Frederic Sowa 62683d1bec99SHannes Frederic Sowa if (!write && !secret->initialized) { 62693d1bec99SHannes Frederic Sowa err = -EIO; 62703d1bec99SHannes Frederic Sowa goto out; 62713d1bec99SHannes Frederic Sowa } 62723d1bec99SHannes Frederic Sowa 62735449a5caSWANG Cong err = snprintf(str, sizeof(str), "%pI6", &secret->secret); 62743d1bec99SHannes Frederic Sowa if (err >= sizeof(str)) { 62753d1bec99SHannes Frederic Sowa err = -EIO; 62763d1bec99SHannes Frederic Sowa goto out; 62773d1bec99SHannes Frederic Sowa } 62783d1bec99SHannes Frederic Sowa 62793d1bec99SHannes Frederic Sowa err = proc_dostring(&lctl, write, buffer, lenp, ppos); 62803d1bec99SHannes Frederic Sowa if (err || !write) 62813d1bec99SHannes Frederic Sowa goto out; 62823d1bec99SHannes Frederic Sowa 62833d1bec99SHannes Frederic Sowa if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) { 62843d1bec99SHannes Frederic Sowa err = -EIO; 62853d1bec99SHannes Frederic Sowa goto out; 62863d1bec99SHannes Frederic Sowa } 62873d1bec99SHannes Frederic Sowa 62883d1bec99SHannes Frederic Sowa secret->initialized = true; 62893d1bec99SHannes Frederic Sowa secret->secret = addr; 62903d1bec99SHannes Frederic Sowa 6291622c81d5SHannes Frederic Sowa if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) { 6292622c81d5SHannes Frederic Sowa struct net_device *dev; 6293622c81d5SHannes Frederic Sowa 6294622c81d5SHannes Frederic Sowa for_each_netdev(net, dev) { 6295622c81d5SHannes Frederic Sowa struct inet6_dev *idev = __in6_dev_get(dev); 6296622c81d5SHannes Frederic Sowa 6297622c81d5SHannes Frederic Sowa if (idev) { 6298d35a00b8SFelix Jia idev->cnf.addr_gen_mode = 6299622c81d5SHannes Frederic Sowa IN6_ADDR_GEN_MODE_STABLE_PRIVACY; 6300622c81d5SHannes Frederic Sowa } 6301622c81d5SHannes Frederic Sowa } 6302622c81d5SHannes Frederic Sowa } else { 6303622c81d5SHannes Frederic Sowa struct inet6_dev *idev = ctl->extra1; 6304622c81d5SHannes Frederic Sowa 6305d35a00b8SFelix Jia idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY; 6306622c81d5SHannes Frederic Sowa } 6307622c81d5SHannes Frederic Sowa 63083d1bec99SHannes Frederic Sowa out: 63093d1bec99SHannes Frederic Sowa rtnl_unlock(); 63103d1bec99SHannes Frederic Sowa 63113d1bec99SHannes Frederic Sowa return err; 63123d1bec99SHannes Frederic Sowa } 6313c92d5491Sstephen hemminger 631435103d11SAndy Gospodarek static 631535103d11SAndy Gospodarek int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl, 631635103d11SAndy Gospodarek int write, 631735103d11SAndy Gospodarek void __user *buffer, 631835103d11SAndy Gospodarek size_t *lenp, 631935103d11SAndy Gospodarek loff_t *ppos) 632035103d11SAndy Gospodarek { 632135103d11SAndy Gospodarek int *valp = ctl->data; 632235103d11SAndy Gospodarek int val = *valp; 632335103d11SAndy Gospodarek loff_t pos = *ppos; 632435103d11SAndy Gospodarek struct ctl_table lctl; 632535103d11SAndy Gospodarek int ret; 632635103d11SAndy Gospodarek 632735103d11SAndy Gospodarek /* ctl->data points to idev->cnf.ignore_routes_when_linkdown 632835103d11SAndy Gospodarek * we should not modify it until we get the rtnl lock. 632935103d11SAndy Gospodarek */ 633035103d11SAndy Gospodarek lctl = *ctl; 633135103d11SAndy Gospodarek lctl.data = &val; 633235103d11SAndy Gospodarek 633335103d11SAndy Gospodarek ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 633435103d11SAndy Gospodarek 633535103d11SAndy Gospodarek if (write) 633635103d11SAndy Gospodarek ret = addrconf_fixup_linkdown(ctl, valp, val); 633735103d11SAndy Gospodarek if (ret) 633835103d11SAndy Gospodarek *ppos = pos; 633935103d11SAndy Gospodarek return ret; 634035103d11SAndy Gospodarek } 634135103d11SAndy Gospodarek 6342df789fe7SDavid Forster static 6343df789fe7SDavid Forster void addrconf_set_nopolicy(struct rt6_info *rt, int action) 6344df789fe7SDavid Forster { 6345df789fe7SDavid Forster if (rt) { 6346df789fe7SDavid Forster if (action) 6347df789fe7SDavid Forster rt->dst.flags |= DST_NOPOLICY; 6348df789fe7SDavid Forster else 6349df789fe7SDavid Forster rt->dst.flags &= ~DST_NOPOLICY; 6350df789fe7SDavid Forster } 6351df789fe7SDavid Forster } 6352df789fe7SDavid Forster 6353df789fe7SDavid Forster static 6354df789fe7SDavid Forster void addrconf_disable_policy_idev(struct inet6_dev *idev, int val) 6355df789fe7SDavid Forster { 6356df789fe7SDavid Forster struct inet6_ifaddr *ifa; 6357df789fe7SDavid Forster 6358df789fe7SDavid Forster read_lock_bh(&idev->lock); 6359df789fe7SDavid Forster list_for_each_entry(ifa, &idev->addr_list, if_list) { 6360df789fe7SDavid Forster spin_lock(&ifa->lock); 6361df789fe7SDavid Forster if (ifa->rt) { 6362f88d8ea6SDavid Ahern /* host routes only use builtin fib6_nh */ 63631cf844c7SDavid Ahern struct fib6_nh *nh = ifa->rt->fib6_nh; 6364df789fe7SDavid Forster int cpu; 6365df789fe7SDavid Forster 636666f5d6ceSWei Wang rcu_read_lock(); 63673b6761d1SDavid Ahern ifa->rt->dst_nopolicy = val ? true : false; 6368f40b6ae2SDavid Ahern if (nh->rt6i_pcpu) { 6369df789fe7SDavid Forster for_each_possible_cpu(cpu) { 6370df789fe7SDavid Forster struct rt6_info **rtp; 6371df789fe7SDavid Forster 6372f40b6ae2SDavid Ahern rtp = per_cpu_ptr(nh->rt6i_pcpu, cpu); 6373df789fe7SDavid Forster addrconf_set_nopolicy(*rtp, val); 6374df789fe7SDavid Forster } 6375df789fe7SDavid Forster } 637666f5d6ceSWei Wang rcu_read_unlock(); 6377df789fe7SDavid Forster } 6378df789fe7SDavid Forster spin_unlock(&ifa->lock); 6379df789fe7SDavid Forster } 6380df789fe7SDavid Forster read_unlock_bh(&idev->lock); 6381df789fe7SDavid Forster } 6382df789fe7SDavid Forster 6383df789fe7SDavid Forster static 6384df789fe7SDavid Forster int addrconf_disable_policy(struct ctl_table *ctl, int *valp, int val) 6385df789fe7SDavid Forster { 6386df789fe7SDavid Forster struct inet6_dev *idev; 6387df789fe7SDavid Forster struct net *net; 6388df789fe7SDavid Forster 6389df789fe7SDavid Forster if (!rtnl_trylock()) 6390df789fe7SDavid Forster return restart_syscall(); 6391df789fe7SDavid Forster 6392df789fe7SDavid Forster *valp = val; 6393df789fe7SDavid Forster 6394df789fe7SDavid Forster net = (struct net *)ctl->extra2; 6395df789fe7SDavid Forster if (valp == &net->ipv6.devconf_dflt->disable_policy) { 6396df789fe7SDavid Forster rtnl_unlock(); 6397df789fe7SDavid Forster return 0; 6398df789fe7SDavid Forster } 6399df789fe7SDavid Forster 6400df789fe7SDavid Forster if (valp == &net->ipv6.devconf_all->disable_policy) { 6401df789fe7SDavid Forster struct net_device *dev; 6402df789fe7SDavid Forster 6403df789fe7SDavid Forster for_each_netdev(net, dev) { 6404df789fe7SDavid Forster idev = __in6_dev_get(dev); 6405df789fe7SDavid Forster if (idev) 6406df789fe7SDavid Forster addrconf_disable_policy_idev(idev, val); 6407df789fe7SDavid Forster } 6408df789fe7SDavid Forster } else { 6409df789fe7SDavid Forster idev = (struct inet6_dev *)ctl->extra1; 6410df789fe7SDavid Forster addrconf_disable_policy_idev(idev, val); 6411df789fe7SDavid Forster } 6412df789fe7SDavid Forster 6413df789fe7SDavid Forster rtnl_unlock(); 6414df789fe7SDavid Forster return 0; 6415df789fe7SDavid Forster } 6416df789fe7SDavid Forster 6417df789fe7SDavid Forster static 6418df789fe7SDavid Forster int addrconf_sysctl_disable_policy(struct ctl_table *ctl, int write, 6419df789fe7SDavid Forster void __user *buffer, size_t *lenp, 6420df789fe7SDavid Forster loff_t *ppos) 6421df789fe7SDavid Forster { 6422df789fe7SDavid Forster int *valp = ctl->data; 6423df789fe7SDavid Forster int val = *valp; 6424df789fe7SDavid Forster loff_t pos = *ppos; 6425df789fe7SDavid Forster struct ctl_table lctl; 6426df789fe7SDavid Forster int ret; 6427df789fe7SDavid Forster 6428df789fe7SDavid Forster lctl = *ctl; 6429df789fe7SDavid Forster lctl.data = &val; 6430df789fe7SDavid Forster ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 6431df789fe7SDavid Forster 6432df789fe7SDavid Forster if (write && (*valp != val)) 6433df789fe7SDavid Forster ret = addrconf_disable_policy(ctl, valp, val); 6434df789fe7SDavid Forster 6435df789fe7SDavid Forster if (ret) 6436df789fe7SDavid Forster *ppos = pos; 6437df789fe7SDavid Forster 6438df789fe7SDavid Forster return ret; 6439df789fe7SDavid Forster } 6440df789fe7SDavid Forster 6441cb4a4c69SMaciej Żenczykowski static int minus_one = -1; 6442cb9e684eSMaciej Żenczykowski static const int two_five_five = 255; 6443cb9e684eSMaciej Żenczykowski 6444607ea7cdSKonstantin Khlebnikov static const struct ctl_table addrconf_sysctl[] = { 64451da177e4SLinus Torvalds { 64461da177e4SLinus Torvalds .procname = "forwarding", 64471da177e4SLinus Torvalds .data = &ipv6_devconf.forwarding, 64481da177e4SLinus Torvalds .maxlen = sizeof(int), 64491da177e4SLinus Torvalds .mode = 0644, 64506d9f239aSAlexey Dobriyan .proc_handler = addrconf_sysctl_forward, 64511da177e4SLinus Torvalds }, 64521da177e4SLinus Torvalds { 64531da177e4SLinus Torvalds .procname = "hop_limit", 64541da177e4SLinus Torvalds .data = &ipv6_devconf.hop_limit, 64551da177e4SLinus Torvalds .maxlen = sizeof(int), 64561da177e4SLinus Torvalds .mode = 0644, 6457cb9e684eSMaciej Żenczykowski .proc_handler = proc_dointvec_minmax, 6458eec4844fSMatteo Croce .extra1 = (void *)SYSCTL_ONE, 6459cb9e684eSMaciej Żenczykowski .extra2 = (void *)&two_five_five, 64601da177e4SLinus Torvalds }, 64611da177e4SLinus Torvalds { 64621da177e4SLinus Torvalds .procname = "mtu", 64631da177e4SLinus Torvalds .data = &ipv6_devconf.mtu6, 64641da177e4SLinus Torvalds .maxlen = sizeof(int), 64651da177e4SLinus Torvalds .mode = 0644, 646677751427SMarcelo Leitner .proc_handler = addrconf_sysctl_mtu, 64671da177e4SLinus Torvalds }, 64681da177e4SLinus Torvalds { 64691da177e4SLinus Torvalds .procname = "accept_ra", 64701da177e4SLinus Torvalds .data = &ipv6_devconf.accept_ra, 64711da177e4SLinus Torvalds .maxlen = sizeof(int), 64721da177e4SLinus Torvalds .mode = 0644, 64736d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 64741da177e4SLinus Torvalds }, 64751da177e4SLinus Torvalds { 64761da177e4SLinus Torvalds .procname = "accept_redirects", 64771da177e4SLinus Torvalds .data = &ipv6_devconf.accept_redirects, 64781da177e4SLinus Torvalds .maxlen = sizeof(int), 64791da177e4SLinus Torvalds .mode = 0644, 64806d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 64811da177e4SLinus Torvalds }, 64821da177e4SLinus Torvalds { 64831da177e4SLinus Torvalds .procname = "autoconf", 64841da177e4SLinus Torvalds .data = &ipv6_devconf.autoconf, 64851da177e4SLinus Torvalds .maxlen = sizeof(int), 64861da177e4SLinus Torvalds .mode = 0644, 64876d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 64881da177e4SLinus Torvalds }, 64891da177e4SLinus Torvalds { 64901da177e4SLinus Torvalds .procname = "dad_transmits", 64911da177e4SLinus Torvalds .data = &ipv6_devconf.dad_transmits, 64921da177e4SLinus Torvalds .maxlen = sizeof(int), 64931da177e4SLinus Torvalds .mode = 0644, 64946d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 64951da177e4SLinus Torvalds }, 64961da177e4SLinus Torvalds { 64971da177e4SLinus Torvalds .procname = "router_solicitations", 64981da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicits, 64991da177e4SLinus Torvalds .maxlen = sizeof(int), 65001da177e4SLinus Torvalds .mode = 0644, 6501cb4a4c69SMaciej Żenczykowski .proc_handler = proc_dointvec_minmax, 6502cb4a4c69SMaciej Żenczykowski .extra1 = &minus_one, 65031da177e4SLinus Torvalds }, 65041da177e4SLinus Torvalds { 65051da177e4SLinus Torvalds .procname = "router_solicitation_interval", 65061da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_interval, 65071da177e4SLinus Torvalds .maxlen = sizeof(int), 65081da177e4SLinus Torvalds .mode = 0644, 65096d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 65101da177e4SLinus Torvalds }, 65111da177e4SLinus Torvalds { 6512bd11f074SMaciej Żenczykowski .procname = "router_solicitation_max_interval", 6513bd11f074SMaciej Żenczykowski .data = &ipv6_devconf.rtr_solicit_max_interval, 6514bd11f074SMaciej Żenczykowski .maxlen = sizeof(int), 6515bd11f074SMaciej Żenczykowski .mode = 0644, 6516bd11f074SMaciej Żenczykowski .proc_handler = proc_dointvec_jiffies, 6517bd11f074SMaciej Żenczykowski }, 6518bd11f074SMaciej Żenczykowski { 65191da177e4SLinus Torvalds .procname = "router_solicitation_delay", 65201da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_delay, 65211da177e4SLinus Torvalds .maxlen = sizeof(int), 65221da177e4SLinus Torvalds .mode = 0644, 65236d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 65241da177e4SLinus Torvalds }, 65251da177e4SLinus Torvalds { 65261da177e4SLinus Torvalds .procname = "force_mld_version", 65271da177e4SLinus Torvalds .data = &ipv6_devconf.force_mld_version, 65281da177e4SLinus Torvalds .maxlen = sizeof(int), 65291da177e4SLinus Torvalds .mode = 0644, 65306d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 65311da177e4SLinus Torvalds }, 6532fc4eba58SHannes Frederic Sowa { 6533fc4eba58SHannes Frederic Sowa .procname = "mldv1_unsolicited_report_interval", 6534fc4eba58SHannes Frederic Sowa .data = 6535fc4eba58SHannes Frederic Sowa &ipv6_devconf.mldv1_unsolicited_report_interval, 6536fc4eba58SHannes Frederic Sowa .maxlen = sizeof(int), 6537fc4eba58SHannes Frederic Sowa .mode = 0644, 6538fc4eba58SHannes Frederic Sowa .proc_handler = proc_dointvec_ms_jiffies, 6539fc4eba58SHannes Frederic Sowa }, 6540fc4eba58SHannes Frederic Sowa { 6541fc4eba58SHannes Frederic Sowa .procname = "mldv2_unsolicited_report_interval", 6542fc4eba58SHannes Frederic Sowa .data = 6543fc4eba58SHannes Frederic Sowa &ipv6_devconf.mldv2_unsolicited_report_interval, 6544fc4eba58SHannes Frederic Sowa .maxlen = sizeof(int), 6545fc4eba58SHannes Frederic Sowa .mode = 0644, 6546fc4eba58SHannes Frederic Sowa .proc_handler = proc_dointvec_ms_jiffies, 6547fc4eba58SHannes Frederic Sowa }, 65481da177e4SLinus Torvalds { 65491da177e4SLinus Torvalds .procname = "use_tempaddr", 65501da177e4SLinus Torvalds .data = &ipv6_devconf.use_tempaddr, 65511da177e4SLinus Torvalds .maxlen = sizeof(int), 65521da177e4SLinus Torvalds .mode = 0644, 65536d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 65541da177e4SLinus Torvalds }, 65551da177e4SLinus Torvalds { 65561da177e4SLinus Torvalds .procname = "temp_valid_lft", 65571da177e4SLinus Torvalds .data = &ipv6_devconf.temp_valid_lft, 65581da177e4SLinus Torvalds .maxlen = sizeof(int), 65591da177e4SLinus Torvalds .mode = 0644, 65606d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 65611da177e4SLinus Torvalds }, 65621da177e4SLinus Torvalds { 65631da177e4SLinus Torvalds .procname = "temp_prefered_lft", 65641da177e4SLinus Torvalds .data = &ipv6_devconf.temp_prefered_lft, 65651da177e4SLinus Torvalds .maxlen = sizeof(int), 65661da177e4SLinus Torvalds .mode = 0644, 65676d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 65681da177e4SLinus Torvalds }, 65691da177e4SLinus Torvalds { 65701da177e4SLinus Torvalds .procname = "regen_max_retry", 65711da177e4SLinus Torvalds .data = &ipv6_devconf.regen_max_retry, 65721da177e4SLinus Torvalds .maxlen = sizeof(int), 65731da177e4SLinus Torvalds .mode = 0644, 65746d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 65751da177e4SLinus Torvalds }, 65761da177e4SLinus Torvalds { 65771da177e4SLinus Torvalds .procname = "max_desync_factor", 65781da177e4SLinus Torvalds .data = &ipv6_devconf.max_desync_factor, 65791da177e4SLinus Torvalds .maxlen = sizeof(int), 65801da177e4SLinus Torvalds .mode = 0644, 65816d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 65821da177e4SLinus Torvalds }, 65831da177e4SLinus Torvalds { 65841da177e4SLinus Torvalds .procname = "max_addresses", 65851da177e4SLinus Torvalds .data = &ipv6_devconf.max_addresses, 65861da177e4SLinus Torvalds .maxlen = sizeof(int), 65871da177e4SLinus Torvalds .mode = 0644, 65886d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 65891da177e4SLinus Torvalds }, 65901da177e4SLinus Torvalds { 659165f5c7c1SYOSHIFUJI Hideaki .procname = "accept_ra_defrtr", 659265f5c7c1SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_defrtr, 659365f5c7c1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 659465f5c7c1SYOSHIFUJI Hideaki .mode = 0644, 65956d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 659665f5c7c1SYOSHIFUJI Hideaki }, 659765f5c7c1SYOSHIFUJI Hideaki { 65988013d1d7SHangbin Liu .procname = "accept_ra_min_hop_limit", 65998013d1d7SHangbin Liu .data = &ipv6_devconf.accept_ra_min_hop_limit, 66008013d1d7SHangbin Liu .maxlen = sizeof(int), 66018013d1d7SHangbin Liu .mode = 0644, 66028013d1d7SHangbin Liu .proc_handler = proc_dointvec, 66038013d1d7SHangbin Liu }, 66048013d1d7SHangbin Liu { 6605c4fd30ebSYOSHIFUJI Hideaki .procname = "accept_ra_pinfo", 6606c4fd30ebSYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_pinfo, 6607c4fd30ebSYOSHIFUJI Hideaki .maxlen = sizeof(int), 6608c4fd30ebSYOSHIFUJI Hideaki .mode = 0644, 66096d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 6610c4fd30ebSYOSHIFUJI Hideaki }, 6611930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 6612930d6ff2SYOSHIFUJI Hideaki { 6613930d6ff2SYOSHIFUJI Hideaki .procname = "accept_ra_rtr_pref", 6614930d6ff2SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rtr_pref, 6615930d6ff2SYOSHIFUJI Hideaki .maxlen = sizeof(int), 6616930d6ff2SYOSHIFUJI Hideaki .mode = 0644, 66176d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 6618930d6ff2SYOSHIFUJI Hideaki }, 661952e16356SYOSHIFUJI Hideaki { 662052e16356SYOSHIFUJI Hideaki .procname = "router_probe_interval", 662152e16356SYOSHIFUJI Hideaki .data = &ipv6_devconf.rtr_probe_interval, 662252e16356SYOSHIFUJI Hideaki .maxlen = sizeof(int), 662352e16356SYOSHIFUJI Hideaki .mode = 0644, 66246d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 662552e16356SYOSHIFUJI Hideaki }, 6626fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 662709c884d4SYOSHIFUJI Hideaki { 6628bbea124bSJoel Scherpelz .procname = "accept_ra_rt_info_min_plen", 6629bbea124bSJoel Scherpelz .data = &ipv6_devconf.accept_ra_rt_info_min_plen, 6630bbea124bSJoel Scherpelz .maxlen = sizeof(int), 6631bbea124bSJoel Scherpelz .mode = 0644, 6632bbea124bSJoel Scherpelz .proc_handler = proc_dointvec, 6633bbea124bSJoel Scherpelz }, 6634bbea124bSJoel Scherpelz { 663509c884d4SYOSHIFUJI Hideaki .procname = "accept_ra_rt_info_max_plen", 663609c884d4SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rt_info_max_plen, 663709c884d4SYOSHIFUJI Hideaki .maxlen = sizeof(int), 663809c884d4SYOSHIFUJI Hideaki .mode = 0644, 66396d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 664009c884d4SYOSHIFUJI Hideaki }, 664109c884d4SYOSHIFUJI Hideaki #endif 6642930d6ff2SYOSHIFUJI Hideaki #endif 6643c4fd30ebSYOSHIFUJI Hideaki { 6644fbea49e1SYOSHIFUJI Hideaki .procname = "proxy_ndp", 6645fbea49e1SYOSHIFUJI Hideaki .data = &ipv6_devconf.proxy_ndp, 6646fbea49e1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 6647fbea49e1SYOSHIFUJI Hideaki .mode = 0644, 6648c92d5491Sstephen hemminger .proc_handler = addrconf_sysctl_proxy_ndp, 6649fbea49e1SYOSHIFUJI Hideaki }, 6650fbea49e1SYOSHIFUJI Hideaki { 66510bcbc926SYOSHIFUJI Hideaki .procname = "accept_source_route", 66520bcbc926SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_source_route, 66530bcbc926SYOSHIFUJI Hideaki .maxlen = sizeof(int), 66540bcbc926SYOSHIFUJI Hideaki .mode = 0644, 66556d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 66560bcbc926SYOSHIFUJI Hideaki }, 665795c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 665895c385b4SNeil Horman { 665995c385b4SNeil Horman .procname = "optimistic_dad", 666095c385b4SNeil Horman .data = &ipv6_devconf.optimistic_dad, 666195c385b4SNeil Horman .maxlen = sizeof(int), 666295c385b4SNeil Horman .mode = 0644, 66636d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 666495c385b4SNeil Horman }, 66657fd2561eSErik Kline { 66667fd2561eSErik Kline .procname = "use_optimistic", 66677fd2561eSErik Kline .data = &ipv6_devconf.use_optimistic, 66687fd2561eSErik Kline .maxlen = sizeof(int), 66697fd2561eSErik Kline .mode = 0644, 66707fd2561eSErik Kline .proc_handler = proc_dointvec, 66717fd2561eSErik Kline }, 667295c385b4SNeil Horman #endif 66737bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 66747bc570c8SYOSHIFUJI Hideaki { 66757bc570c8SYOSHIFUJI Hideaki .procname = "mc_forwarding", 66767bc570c8SYOSHIFUJI Hideaki .data = &ipv6_devconf.mc_forwarding, 66777bc570c8SYOSHIFUJI Hideaki .maxlen = sizeof(int), 6678a4e6db07SDavid S. Miller .mode = 0444, 66796d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 66807bc570c8SYOSHIFUJI Hideaki }, 66817bc570c8SYOSHIFUJI Hideaki #endif 66820bcbc926SYOSHIFUJI Hideaki { 6683778d80beSYOSHIFUJI Hideaki .procname = "disable_ipv6", 6684778d80beSYOSHIFUJI Hideaki .data = &ipv6_devconf.disable_ipv6, 6685778d80beSYOSHIFUJI Hideaki .maxlen = sizeof(int), 6686778d80beSYOSHIFUJI Hideaki .mode = 0644, 668756d417b1SBrian Haley .proc_handler = addrconf_sysctl_disable, 6688778d80beSYOSHIFUJI Hideaki }, 6689778d80beSYOSHIFUJI Hideaki { 66901b34be74SYOSHIFUJI Hideaki .procname = "accept_dad", 66911b34be74SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_dad, 66921b34be74SYOSHIFUJI Hideaki .maxlen = sizeof(int), 66931b34be74SYOSHIFUJI Hideaki .mode = 0644, 66946d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 66951b34be74SYOSHIFUJI Hideaki }, 66961b34be74SYOSHIFUJI Hideaki { 6697f7734fdfSOctavian Purdila .procname = "force_tllao", 6698f7734fdfSOctavian Purdila .data = &ipv6_devconf.force_tllao, 6699f7734fdfSOctavian Purdila .maxlen = sizeof(int), 6700f7734fdfSOctavian Purdila .mode = 0644, 6701f7734fdfSOctavian Purdila .proc_handler = proc_dointvec 6702f7734fdfSOctavian Purdila }, 6703f7734fdfSOctavian Purdila { 67045cb04436SHannes Frederic Sowa .procname = "ndisc_notify", 67055cb04436SHannes Frederic Sowa .data = &ipv6_devconf.ndisc_notify, 67065cb04436SHannes Frederic Sowa .maxlen = sizeof(int), 67075cb04436SHannes Frederic Sowa .mode = 0644, 67085cb04436SHannes Frederic Sowa .proc_handler = proc_dointvec 67095cb04436SHannes Frederic Sowa }, 67105cb04436SHannes Frederic Sowa { 6711b800c3b9SHannes Frederic Sowa .procname = "suppress_frag_ndisc", 6712b800c3b9SHannes Frederic Sowa .data = &ipv6_devconf.suppress_frag_ndisc, 6713b800c3b9SHannes Frederic Sowa .maxlen = sizeof(int), 6714b800c3b9SHannes Frederic Sowa .mode = 0644, 6715b800c3b9SHannes Frederic Sowa .proc_handler = proc_dointvec 6716b800c3b9SHannes Frederic Sowa }, 6717b800c3b9SHannes Frederic Sowa { 6718d9333196SBen Greear .procname = "accept_ra_from_local", 6719d9333196SBen Greear .data = &ipv6_devconf.accept_ra_from_local, 6720d9333196SBen Greear .maxlen = sizeof(int), 6721d9333196SBen Greear .mode = 0644, 6722d9333196SBen Greear .proc_handler = proc_dointvec, 6723d9333196SBen Greear }, 6724d9333196SBen Greear { 6725c2943f14SHarout Hedeshian .procname = "accept_ra_mtu", 6726c2943f14SHarout Hedeshian .data = &ipv6_devconf.accept_ra_mtu, 6727c2943f14SHarout Hedeshian .maxlen = sizeof(int), 6728c2943f14SHarout Hedeshian .mode = 0644, 6729c2943f14SHarout Hedeshian .proc_handler = proc_dointvec, 6730c2943f14SHarout Hedeshian }, 6731c2943f14SHarout Hedeshian { 67323d1bec99SHannes Frederic Sowa .procname = "stable_secret", 67333d1bec99SHannes Frederic Sowa .data = &ipv6_devconf.stable_secret, 67343d1bec99SHannes Frederic Sowa .maxlen = IPV6_MAX_STRLEN, 67353d1bec99SHannes Frederic Sowa .mode = 0600, 67363d1bec99SHannes Frederic Sowa .proc_handler = addrconf_sysctl_stable_secret, 67373d1bec99SHannes Frederic Sowa }, 67383d1bec99SHannes Frederic Sowa { 67393985e8a3SErik Kline .procname = "use_oif_addrs_only", 67403985e8a3SErik Kline .data = &ipv6_devconf.use_oif_addrs_only, 67413985e8a3SErik Kline .maxlen = sizeof(int), 67423985e8a3SErik Kline .mode = 0644, 67433985e8a3SErik Kline .proc_handler = proc_dointvec, 674435103d11SAndy Gospodarek }, 674535103d11SAndy Gospodarek { 674635103d11SAndy Gospodarek .procname = "ignore_routes_with_linkdown", 674735103d11SAndy Gospodarek .data = &ipv6_devconf.ignore_routes_with_linkdown, 674835103d11SAndy Gospodarek .maxlen = sizeof(int), 674935103d11SAndy Gospodarek .mode = 0644, 675035103d11SAndy Gospodarek .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown, 67513985e8a3SErik Kline }, 67523985e8a3SErik Kline { 6753abbc3043SJohannes Berg .procname = "drop_unicast_in_l2_multicast", 6754abbc3043SJohannes Berg .data = &ipv6_devconf.drop_unicast_in_l2_multicast, 6755abbc3043SJohannes Berg .maxlen = sizeof(int), 6756abbc3043SJohannes Berg .mode = 0644, 6757abbc3043SJohannes Berg .proc_handler = proc_dointvec, 6758abbc3043SJohannes Berg }, 6759abbc3043SJohannes Berg { 67607a02bf89SJohannes Berg .procname = "drop_unsolicited_na", 67617a02bf89SJohannes Berg .data = &ipv6_devconf.drop_unsolicited_na, 67627a02bf89SJohannes Berg .maxlen = sizeof(int), 67637a02bf89SJohannes Berg .mode = 0644, 67647a02bf89SJohannes Berg .proc_handler = proc_dointvec, 67657a02bf89SJohannes Berg }, 67667a02bf89SJohannes Berg { 6767f1705ec1SDavid Ahern .procname = "keep_addr_on_down", 6768f1705ec1SDavid Ahern .data = &ipv6_devconf.keep_addr_on_down, 6769f1705ec1SDavid Ahern .maxlen = sizeof(int), 6770f1705ec1SDavid Ahern .mode = 0644, 6771f1705ec1SDavid Ahern .proc_handler = proc_dointvec, 6772f1705ec1SDavid Ahern 6773f1705ec1SDavid Ahern }, 6774f1705ec1SDavid Ahern { 67751ababebaSDavid Lebrun .procname = "seg6_enabled", 67761ababebaSDavid Lebrun .data = &ipv6_devconf.seg6_enabled, 67771ababebaSDavid Lebrun .maxlen = sizeof(int), 67781ababebaSDavid Lebrun .mode = 0644, 67791ababebaSDavid Lebrun .proc_handler = proc_dointvec, 67801ababebaSDavid Lebrun }, 6781bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC 6782bf355b8dSDavid Lebrun { 6783bf355b8dSDavid Lebrun .procname = "seg6_require_hmac", 6784bf355b8dSDavid Lebrun .data = &ipv6_devconf.seg6_require_hmac, 6785bf355b8dSDavid Lebrun .maxlen = sizeof(int), 6786bf355b8dSDavid Lebrun .mode = 0644, 6787bf355b8dSDavid Lebrun .proc_handler = proc_dointvec, 6788bf355b8dSDavid Lebrun }, 6789bf355b8dSDavid Lebrun #endif 67901ababebaSDavid Lebrun { 6791adc176c5SErik Nordmark .procname = "enhanced_dad", 6792adc176c5SErik Nordmark .data = &ipv6_devconf.enhanced_dad, 6793adc176c5SErik Nordmark .maxlen = sizeof(int), 6794adc176c5SErik Nordmark .mode = 0644, 6795adc176c5SErik Nordmark .proc_handler = proc_dointvec, 6796adc176c5SErik Nordmark }, 6797adc176c5SErik Nordmark { 6798d35a00b8SFelix Jia .procname = "addr_gen_mode", 6799d35a00b8SFelix Jia .data = &ipv6_devconf.addr_gen_mode, 6800d35a00b8SFelix Jia .maxlen = sizeof(int), 6801d35a00b8SFelix Jia .mode = 0644, 6802d35a00b8SFelix Jia .proc_handler = addrconf_sysctl_addr_gen_mode, 6803d35a00b8SFelix Jia }, 6804d35a00b8SFelix Jia { 6805df789fe7SDavid Forster .procname = "disable_policy", 6806df789fe7SDavid Forster .data = &ipv6_devconf.disable_policy, 6807df789fe7SDavid Forster .maxlen = sizeof(int), 6808df789fe7SDavid Forster .mode = 0644, 6809df789fe7SDavid Forster .proc_handler = addrconf_sysctl_disable_policy, 6810df789fe7SDavid Forster }, 6811df789fe7SDavid Forster { 68122210d6b2SMaciej Żenczykowski .procname = "ndisc_tclass", 68132210d6b2SMaciej Żenczykowski .data = &ipv6_devconf.ndisc_tclass, 68142210d6b2SMaciej Żenczykowski .maxlen = sizeof(int), 68152210d6b2SMaciej Żenczykowski .mode = 0644, 68162210d6b2SMaciej Żenczykowski .proc_handler = proc_dointvec_minmax, 6817eec4844fSMatteo Croce .extra1 = (void *)SYSCTL_ZERO, 68182210d6b2SMaciej Żenczykowski .extra2 = (void *)&two_five_five, 68192210d6b2SMaciej Żenczykowski }, 68202210d6b2SMaciej Żenczykowski { 6821f8572d8fSEric W. Biederman /* sentinel */ 68221da177e4SLinus Torvalds } 68231da177e4SLinus Torvalds }; 68241da177e4SLinus Torvalds 6825bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name, 6826f8572d8fSEric W. Biederman struct inet6_dev *idev, struct ipv6_devconf *p) 68271da177e4SLinus Torvalds { 682829c994e3SNicolas Dichtel int i, ifindex; 6829607ea7cdSKonstantin Khlebnikov struct ctl_table *table; 68306105e293SEric W. Biederman char path[sizeof("net/ipv6/conf/") + IFNAMSIZ]; 68311dab6222SPavel Emelyanov 6832607ea7cdSKonstantin Khlebnikov table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL); 6833607ea7cdSKonstantin Khlebnikov if (!table) 6834f68635e6SPavel Emelyanov goto out; 6835f68635e6SPavel Emelyanov 6836607ea7cdSKonstantin Khlebnikov for (i = 0; table[i].data; i++) { 6837607ea7cdSKonstantin Khlebnikov table[i].data += (char *)p - (char *)&ipv6_devconf; 6838cb9e684eSMaciej Żenczykowski /* If one of these is already set, then it is not safe to 6839cb9e684eSMaciej Żenczykowski * overwrite either of them: this makes proc_dointvec_minmax 6840cb9e684eSMaciej Żenczykowski * usable. 6841cb9e684eSMaciej Żenczykowski */ 6842cb9e684eSMaciej Żenczykowski if (!table[i].extra1 && !table[i].extra2) { 6843607ea7cdSKonstantin Khlebnikov table[i].extra1 = idev; /* embedded; no ref */ 6844607ea7cdSKonstantin Khlebnikov table[i].extra2 = net; 68451da177e4SLinus Torvalds } 6846cb9e684eSMaciej Żenczykowski } 68471da177e4SLinus Torvalds 68486105e293SEric W. Biederman snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name); 68491da177e4SLinus Torvalds 6850607ea7cdSKonstantin Khlebnikov p->sysctl_header = register_net_sysctl(net, path, table); 6851607ea7cdSKonstantin Khlebnikov if (!p->sysctl_header) 68526105e293SEric W. Biederman goto free; 6853f68635e6SPavel Emelyanov 685429c994e3SNicolas Dichtel if (!strcmp(dev_name, "all")) 685529c994e3SNicolas Dichtel ifindex = NETCONFA_IFINDEX_ALL; 685629c994e3SNicolas Dichtel else if (!strcmp(dev_name, "default")) 685729c994e3SNicolas Dichtel ifindex = NETCONFA_IFINDEX_DEFAULT; 685829c994e3SNicolas Dichtel else 685929c994e3SNicolas Dichtel ifindex = idev->dev->ifindex; 686085b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL, 686185b3daadSDavid Ahern ifindex, p); 686295897312SPavel Emelyanov return 0; 68631da177e4SLinus Torvalds 68641da177e4SLinus Torvalds free: 6865607ea7cdSKonstantin Khlebnikov kfree(table); 6866f68635e6SPavel Emelyanov out: 686795897312SPavel Emelyanov return -ENOBUFS; 68681da177e4SLinus Torvalds } 68691da177e4SLinus Torvalds 687023452170SDavid Ahern static void __addrconf_sysctl_unregister(struct net *net, 687123452170SDavid Ahern struct ipv6_devconf *p, int ifindex) 6872f52295a9SPavel Emelyanov { 6873607ea7cdSKonstantin Khlebnikov struct ctl_table *table; 6874f52295a9SPavel Emelyanov 6875607ea7cdSKonstantin Khlebnikov if (!p->sysctl_header) 6876408c4768SPavel Emelyanov return; 6877408c4768SPavel Emelyanov 6878607ea7cdSKonstantin Khlebnikov table = p->sysctl_header->ctl_table_arg; 6879607ea7cdSKonstantin Khlebnikov unregister_net_sysctl_table(p->sysctl_header); 6880607ea7cdSKonstantin Khlebnikov p->sysctl_header = NULL; 6881607ea7cdSKonstantin Khlebnikov kfree(table); 688223452170SDavid Ahern 688323452170SDavid Ahern inet6_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL); 68841da177e4SLinus Torvalds } 6885408c4768SPavel Emelyanov 6886a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev) 6887408c4768SPavel Emelyanov { 6888a317a2f1SWANG Cong int err; 6889a317a2f1SWANG Cong 6890a317a2f1SWANG Cong if (!sysctl_dev_name_is_allowed(idev->dev->name)) 6891a317a2f1SWANG Cong return -EINVAL; 6892a317a2f1SWANG Cong 6893a317a2f1SWANG Cong err = neigh_sysctl_register(idev->dev, idev->nd_parms, 6894f8572d8fSEric W. Biederman &ndisc_ifinfo_sysctl_change); 6895a317a2f1SWANG Cong if (err) 6896a317a2f1SWANG Cong return err; 6897a317a2f1SWANG Cong err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name, 6898f8572d8fSEric W. Biederman idev, &idev->cnf); 6899a317a2f1SWANG Cong if (err) 6900a317a2f1SWANG Cong neigh_sysctl_unregister(idev->nd_parms); 6901a317a2f1SWANG Cong 6902a317a2f1SWANG Cong return err; 6903408c4768SPavel Emelyanov } 6904408c4768SPavel Emelyanov 6905408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev) 6906408c4768SPavel Emelyanov { 690723452170SDavid Ahern __addrconf_sysctl_unregister(dev_net(idev->dev), &idev->cnf, 690823452170SDavid Ahern idev->dev->ifindex); 6909408c4768SPavel Emelyanov neigh_sysctl_unregister(idev->nd_parms); 69101da177e4SLinus Torvalds } 69111da177e4SLinus Torvalds 69121da177e4SLinus Torvalds 69131da177e4SLinus Torvalds #endif 69141da177e4SLinus Torvalds 69152c8c1e72SAlexey Dobriyan static int __net_init addrconf_init_net(struct net *net) 6916e0da5a48SPavel Emelyanov { 6917a79ca223SHong Zhiguo int err = -ENOMEM; 6918e0da5a48SPavel Emelyanov struct ipv6_devconf *all, *dflt; 6919e0da5a48SPavel Emelyanov 6920a79ca223SHong Zhiguo all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL); 692163159f29SIan Morris if (!all) 6922e0da5a48SPavel Emelyanov goto err_alloc_all; 6923e0da5a48SPavel Emelyanov 6924a79ca223SHong Zhiguo dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL); 692563159f29SIan Morris if (!dflt) 6926e0da5a48SPavel Emelyanov goto err_alloc_dflt; 6927a79ca223SHong Zhiguo 6928a154d5d8SArnd Bergmann if (IS_ENABLED(CONFIG_SYSCTL) && 6929a154d5d8SArnd Bergmann sysctl_devconf_inherit_init_net == 1 && !net_eq(net, &init_net)) { 6930856c395cSCong Wang memcpy(all, init_net.ipv6.devconf_all, sizeof(ipv6_devconf)); 6931856c395cSCong Wang memcpy(dflt, init_net.ipv6.devconf_dflt, sizeof(ipv6_devconf_dflt)); 6932856c395cSCong Wang } 6933856c395cSCong Wang 693456d417b1SBrian Haley /* these will be inherited by all namespaces */ 693556d417b1SBrian Haley dflt->autoconf = ipv6_defaults.autoconf; 693656d417b1SBrian Haley dflt->disable_ipv6 = ipv6_defaults.disable_ipv6; 6937e0da5a48SPavel Emelyanov 69383d1bec99SHannes Frederic Sowa dflt->stable_secret.initialized = false; 69393d1bec99SHannes Frederic Sowa all->stable_secret.initialized = false; 69403d1bec99SHannes Frederic Sowa 6941e0da5a48SPavel Emelyanov net->ipv6.devconf_all = all; 6942e0da5a48SPavel Emelyanov net->ipv6.devconf_dflt = dflt; 6943e0da5a48SPavel Emelyanov 6944e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 6945f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "all", NULL, all); 6946e0da5a48SPavel Emelyanov if (err < 0) 6947e0da5a48SPavel Emelyanov goto err_reg_all; 6948e0da5a48SPavel Emelyanov 6949f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "default", NULL, dflt); 6950e0da5a48SPavel Emelyanov if (err < 0) 6951e0da5a48SPavel Emelyanov goto err_reg_dflt; 6952e0da5a48SPavel Emelyanov #endif 6953e0da5a48SPavel Emelyanov return 0; 6954e0da5a48SPavel Emelyanov 6955e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 6956e0da5a48SPavel Emelyanov err_reg_dflt: 695723452170SDavid Ahern __addrconf_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL); 6958e0da5a48SPavel Emelyanov err_reg_all: 6959e0da5a48SPavel Emelyanov kfree(dflt); 6960e0da5a48SPavel Emelyanov #endif 6961e0da5a48SPavel Emelyanov err_alloc_dflt: 6962e0da5a48SPavel Emelyanov kfree(all); 6963e0da5a48SPavel Emelyanov err_alloc_all: 6964e0da5a48SPavel Emelyanov return err; 6965e0da5a48SPavel Emelyanov } 6966e0da5a48SPavel Emelyanov 69672c8c1e72SAlexey Dobriyan static void __net_exit addrconf_exit_net(struct net *net) 6968e0da5a48SPavel Emelyanov { 6969e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 697023452170SDavid Ahern __addrconf_sysctl_unregister(net, net->ipv6.devconf_dflt, 697123452170SDavid Ahern NETCONFA_IFINDEX_DEFAULT); 697223452170SDavid Ahern __addrconf_sysctl_unregister(net, net->ipv6.devconf_all, 697323452170SDavid Ahern NETCONFA_IFINDEX_ALL); 6974e0da5a48SPavel Emelyanov #endif 6975e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_dflt); 6976e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_all); 6977e0da5a48SPavel Emelyanov } 6978e0da5a48SPavel Emelyanov 6979e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = { 6980e0da5a48SPavel Emelyanov .init = addrconf_init_net, 6981e0da5a48SPavel Emelyanov .exit = addrconf_exit_net, 6982e0da5a48SPavel Emelyanov }; 6983e0da5a48SPavel Emelyanov 6984207895fdSDaniel Borkmann static struct rtnl_af_ops inet6_ops __read_mostly = { 6985b382b191SThomas Graf .family = AF_INET6, 6986b382b191SThomas Graf .fill_link_af = inet6_fill_link_af, 6987b382b191SThomas Graf .get_link_af_size = inet6_get_link_af_size, 698811b1f828SDaniel Borkmann .validate_link_af = inet6_validate_link_af, 6989f53adae4SDaniel Borkmann .set_link_af = inet6_set_link_af, 6990b382b191SThomas Graf }; 6991b382b191SThomas Graf 69921da177e4SLinus Torvalds /* 69931da177e4SLinus Torvalds * Init / cleanup code 69941da177e4SLinus Torvalds */ 69951da177e4SLinus Torvalds 69961da177e4SLinus Torvalds int __init addrconf_init(void) 69971da177e4SLinus Torvalds { 6998a317a2f1SWANG Cong struct inet6_dev *idev; 6999c2e21293Sstephen hemminger int i, err; 70002a8cc6c8SYOSHIFUJI Hideaki 7001e21e8467SStephen Hemminger err = ipv6_addr_label_init(); 7002e21e8467SStephen Hemminger if (err < 0) { 7003f3213831SJoe Perches pr_crit("%s: cannot initialize default policy table: %d\n", 7004f3213831SJoe Perches __func__, err); 70052cc6d2bfSNeil Horman goto out; 70062a8cc6c8SYOSHIFUJI Hideaki } 70071da177e4SLinus Torvalds 70082cc6d2bfSNeil Horman err = register_pernet_subsys(&addrconf_ops); 70092cc6d2bfSNeil Horman if (err < 0) 70102cc6d2bfSNeil Horman goto out_addrlabel; 7011e0da5a48SPavel Emelyanov 7012c15b1ccaSHannes Frederic Sowa addrconf_wq = create_workqueue("ipv6_addrconf"); 7013c15b1ccaSHannes Frederic Sowa if (!addrconf_wq) { 7014c15b1ccaSHannes Frederic Sowa err = -ENOMEM; 7015c15b1ccaSHannes Frederic Sowa goto out_nowq; 7016c15b1ccaSHannes Frederic Sowa } 7017c15b1ccaSHannes Frederic Sowa 70181da177e4SLinus Torvalds /* The addrconf netdev notifier requires that loopback_dev 70191da177e4SLinus Torvalds * has it's ipv6 private information allocated and setup 70201da177e4SLinus Torvalds * before it can bring up and give link-local addresses 70211da177e4SLinus Torvalds * to other devices which are up. 70221da177e4SLinus Torvalds * 70231da177e4SLinus Torvalds * Unfortunately, loopback_dev is not necessarily the first 70241da177e4SLinus Torvalds * entry in the global dev_base list of net devices. In fact, 70251da177e4SLinus Torvalds * it is likely to be the very last entry on that list. 70261da177e4SLinus Torvalds * So this causes the notifier registry below to try and 70271da177e4SLinus Torvalds * give link-local addresses to all devices besides loopback_dev 70281da177e4SLinus Torvalds * first, then loopback_dev, which cases all the non-loopback_dev 70291da177e4SLinus Torvalds * devices to fail to get a link-local address. 70301da177e4SLinus Torvalds * 70311da177e4SLinus Torvalds * So, as a temporary fix, allocate the ipv6 structure for 70321da177e4SLinus Torvalds * loopback_dev first by hand. 70331da177e4SLinus Torvalds * Longer term, all of the dependencies ipv6 has upon the loopback 70341da177e4SLinus Torvalds * device and it being up should be removed. 70351da177e4SLinus Torvalds */ 70361da177e4SLinus Torvalds rtnl_lock(); 7037a317a2f1SWANG Cong idev = ipv6_add_dev(init_net.loopback_dev); 70381da177e4SLinus Torvalds rtnl_unlock(); 7039a317a2f1SWANG Cong if (IS_ERR(idev)) { 7040a317a2f1SWANG Cong err = PTR_ERR(idev); 7041e0da5a48SPavel Emelyanov goto errlo; 7042a317a2f1SWANG Cong } 70431da177e4SLinus Torvalds 70442f460933SWANG Cong ip6_route_init_special_entries(); 70452f460933SWANG Cong 7046c2e21293Sstephen hemminger for (i = 0; i < IN6_ADDR_HSIZE; i++) 7047c2e21293Sstephen hemminger INIT_HLIST_HEAD(&inet6_addr_lst[i]); 7048c2e21293Sstephen hemminger 70491da177e4SLinus Torvalds register_netdevice_notifier(&ipv6_dev_notf); 70501da177e4SLinus Torvalds 7051c15b1ccaSHannes Frederic Sowa addrconf_verify(); 7052c127ea2cSThomas Graf 70533678a9d8Sstephen hemminger rtnl_af_register(&inet6_ops); 7054b382b191SThomas Graf 705516feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETLINK, 705616feebcfSFlorian Westphal NULL, inet6_dump_ifinfo, 0); 7057c127ea2cSThomas Graf if (err < 0) 7058c127ea2cSThomas Graf goto errout; 7059c127ea2cSThomas Graf 706016feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWADDR, 706116feebcfSFlorian Westphal inet6_rtm_newaddr, NULL, 0); 706216feebcfSFlorian Westphal if (err < 0) 706316feebcfSFlorian Westphal goto errout; 706416feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELADDR, 706516feebcfSFlorian Westphal inet6_rtm_deladdr, NULL, 0); 706616feebcfSFlorian Westphal if (err < 0) 706716feebcfSFlorian Westphal goto errout; 706816feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETADDR, 706916feebcfSFlorian Westphal inet6_rtm_getaddr, inet6_dump_ifaddr, 707016feebcfSFlorian Westphal RTNL_FLAG_DOIT_UNLOCKED); 707116feebcfSFlorian Westphal if (err < 0) 707216feebcfSFlorian Westphal goto errout; 707316feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETMULTICAST, 707416feebcfSFlorian Westphal NULL, inet6_dump_ifmcaddr, 0); 707516feebcfSFlorian Westphal if (err < 0) 707616feebcfSFlorian Westphal goto errout; 707716feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETANYCAST, 707816feebcfSFlorian Westphal NULL, inet6_dump_ifacaddr, 0); 707916feebcfSFlorian Westphal if (err < 0) 708016feebcfSFlorian Westphal goto errout; 708116feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETNETCONF, 708216feebcfSFlorian Westphal inet6_netconf_get_devconf, 708316feebcfSFlorian Westphal inet6_netconf_dump_devconf, 708416feebcfSFlorian Westphal RTNL_FLAG_DOIT_UNLOCKED); 708516feebcfSFlorian Westphal if (err < 0) 708616feebcfSFlorian Westphal goto errout; 7087a3fde2adSFlorian Westphal err = ipv6_addr_label_rtnl_register(); 7088a3fde2adSFlorian Westphal if (err < 0) 7089a3fde2adSFlorian Westphal goto errout; 70902a8cc6c8SYOSHIFUJI Hideaki 70911da177e4SLinus Torvalds return 0; 7092c127ea2cSThomas Graf errout: 709316feebcfSFlorian Westphal rtnl_unregister_all(PF_INET6); 7094b382b191SThomas Graf rtnl_af_unregister(&inet6_ops); 7095c127ea2cSThomas Graf unregister_netdevice_notifier(&ipv6_dev_notf); 7096e0da5a48SPavel Emelyanov errlo: 7097c15b1ccaSHannes Frederic Sowa destroy_workqueue(addrconf_wq); 7098c15b1ccaSHannes Frederic Sowa out_nowq: 7099e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 71002cc6d2bfSNeil Horman out_addrlabel: 71012cc6d2bfSNeil Horman ipv6_addr_label_cleanup(); 71022cc6d2bfSNeil Horman out: 7103c127ea2cSThomas Graf return err; 71041da177e4SLinus Torvalds } 71051da177e4SLinus Torvalds 710609f7709fSDaniel Lezcano void addrconf_cleanup(void) 71071da177e4SLinus Torvalds { 7108176c39afSDaniel Lezcano struct net_device *dev; 71091da177e4SLinus Torvalds int i; 71101da177e4SLinus Torvalds 71111da177e4SLinus Torvalds unregister_netdevice_notifier(&ipv6_dev_notf); 7112e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 71132cc6d2bfSNeil Horman ipv6_addr_label_cleanup(); 71141da177e4SLinus Torvalds 71155c45121dSFlorian Westphal rtnl_af_unregister(&inet6_ops); 71161da177e4SLinus Torvalds 71175c45121dSFlorian Westphal rtnl_lock(); 7118b382b191SThomas Graf 7119176c39afSDaniel Lezcano /* clean dev list */ 7120176c39afSDaniel Lezcano for_each_netdev(&init_net, dev) { 7121176c39afSDaniel Lezcano if (__in6_dev_get(dev) == NULL) 7122176c39afSDaniel Lezcano continue; 7123176c39afSDaniel Lezcano addrconf_ifdown(dev, 1); 7124176c39afSDaniel Lezcano } 7125176c39afSDaniel Lezcano addrconf_ifdown(init_net.loopback_dev, 2); 7126176c39afSDaniel Lezcano 71271da177e4SLinus Torvalds /* 71281da177e4SLinus Torvalds * Check hash table. 71291da177e4SLinus Torvalds */ 71305c578aedSstephen hemminger spin_lock_bh(&addrconf_hash_lock); 7131c2e21293Sstephen hemminger for (i = 0; i < IN6_ADDR_HSIZE; i++) 7132c2e21293Sstephen hemminger WARN_ON(!hlist_empty(&inet6_addr_lst[i])); 71335c578aedSstephen hemminger spin_unlock_bh(&addrconf_hash_lock); 7134c15b1ccaSHannes Frederic Sowa cancel_delayed_work(&addr_chk_work); 71351da177e4SLinus Torvalds rtnl_unlock(); 7136c15b1ccaSHannes Frederic Sowa 7137c15b1ccaSHannes Frederic Sowa destroy_workqueue(addrconf_wq); 71381da177e4SLinus Torvalds } 7139