11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * IPv6 Address [auto]configuration 31da177e4SLinus Torvalds * Linux INET6 implementation 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Authors: 61da177e4SLinus Torvalds * Pedro Roque <roque@di.fc.ul.pt> 71da177e4SLinus Torvalds * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 101da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 111da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 121da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 131da177e4SLinus Torvalds */ 141da177e4SLinus Torvalds 151da177e4SLinus Torvalds /* 161da177e4SLinus Torvalds * Changes: 171da177e4SLinus Torvalds * 181da177e4SLinus Torvalds * Janos Farkas : delete timer on ifdown 191da177e4SLinus Torvalds * <chexum@bankinf.banki.hu> 201da177e4SLinus Torvalds * Andi Kleen : kill double kfree on module 211da177e4SLinus Torvalds * unload. 221da177e4SLinus Torvalds * Maciej W. Rozycki : FDDI support 231da177e4SLinus Torvalds * sekiya@USAGI : Don't send too many RS 241da177e4SLinus Torvalds * packets. 251da177e4SLinus Torvalds * yoshfuji@USAGI : Fixed interval between DAD 261da177e4SLinus Torvalds * packets. 271da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI : improved accuracy of 281da177e4SLinus Torvalds * address validation timer. 291da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041) 301da177e4SLinus Torvalds * support. 311da177e4SLinus Torvalds * Yuji SEKIYA @USAGI : Don't assign a same IPv6 321da177e4SLinus Torvalds * address on a same interface. 331da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI : ARCnet support 341da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to 351da177e4SLinus Torvalds * seq_file. 36b1cacb68SYOSHIFUJI Hideaki * YOSHIFUJI Hideaki @USAGI : improved source address 37b1cacb68SYOSHIFUJI Hideaki * selection; consider scope, 38b1cacb68SYOSHIFUJI Hideaki * status etc. 391da177e4SLinus Torvalds */ 401da177e4SLinus Torvalds 41f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt 42f3213831SJoe Perches 431da177e4SLinus Torvalds #include <linux/errno.h> 441da177e4SLinus Torvalds #include <linux/types.h> 45a0bffffcSIlpo Järvinen #include <linux/kernel.h> 46174cd4b1SIngo Molnar #include <linux/sched/signal.h> 471da177e4SLinus Torvalds #include <linux/socket.h> 481da177e4SLinus Torvalds #include <linux/sockios.h> 491da177e4SLinus Torvalds #include <linux/net.h> 503d1bec99SHannes Frederic Sowa #include <linux/inet.h> 511da177e4SLinus Torvalds #include <linux/in6.h> 521da177e4SLinus Torvalds #include <linux/netdevice.h> 531823730fSThomas Graf #include <linux/if_addr.h> 541da177e4SLinus Torvalds #include <linux/if_arp.h> 551da177e4SLinus Torvalds #include <linux/if_arcnet.h> 561da177e4SLinus Torvalds #include <linux/if_infiniband.h> 571da177e4SLinus Torvalds #include <linux/route.h> 581da177e4SLinus Torvalds #include <linux/inetdevice.h> 591da177e4SLinus Torvalds #include <linux/init.h> 605a0e3ad6STejun Heo #include <linux/slab.h> 611da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 621da177e4SLinus Torvalds #include <linux/sysctl.h> 631da177e4SLinus Torvalds #endif 644fc268d2SRandy Dunlap #include <linux/capability.h> 651da177e4SLinus Torvalds #include <linux/delay.h> 661da177e4SLinus Torvalds #include <linux/notifier.h> 67543537bdSPaulo Marques #include <linux/string.h> 68ddbe5032SEric Dumazet #include <linux/hash.h> 691da177e4SLinus Torvalds 70457c4cbcSEric W. Biederman #include <net/net_namespace.h> 711da177e4SLinus Torvalds #include <net/sock.h> 721da177e4SLinus Torvalds #include <net/snmp.h> 731da177e4SLinus Torvalds 745241c2d7SAlexander Aring #include <net/6lowpan.h> 75cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 #include <net/firewire.h> 761da177e4SLinus Torvalds #include <net/ipv6.h> 771da177e4SLinus Torvalds #include <net/protocol.h> 781da177e4SLinus Torvalds #include <net/ndisc.h> 791da177e4SLinus Torvalds #include <net/ip6_route.h> 801da177e4SLinus Torvalds #include <net/addrconf.h> 811da177e4SLinus Torvalds #include <net/tcp.h> 821da177e4SLinus Torvalds #include <net/ip.h> 835d620266SThomas Graf #include <net/netlink.h> 84f24e3d65SMitsuru Chinen #include <net/pkt_sched.h> 85ca254490SDavid Ahern #include <net/l3mdev.h> 861da177e4SLinus Torvalds #include <linux/if_tunnel.h> 871da177e4SLinus Torvalds #include <linux/rtnetlink.h> 88f3a1bfb1SNicolas Dichtel #include <linux/netconf.h> 891da177e4SLinus Torvalds #include <linux/random.h> 90e21e8467SStephen Hemminger #include <linux/uaccess.h> 917f7d9a6bSHerbert Xu #include <asm/unaligned.h> 921da177e4SLinus Torvalds 931da177e4SLinus Torvalds #include <linux/proc_fs.h> 941da177e4SLinus Torvalds #include <linux/seq_file.h> 95bc3b2d7fSPaul Gortmaker #include <linux/export.h> 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds /* Set to 3 to get tracing... */ 981da177e4SLinus Torvalds #define ACONF_DEBUG 2 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds #if ACONF_DEBUG >= 3 101ba3542e1Sdingtianhong #define ADBG(fmt, ...) printk(fmt, ##__VA_ARGS__) 1021da177e4SLinus Torvalds #else 103ba3542e1Sdingtianhong #define ADBG(fmt, ...) do { if (0) printk(fmt, ##__VA_ARGS__); } while (0) 1041da177e4SLinus Torvalds #endif 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds #define INFINITY_LIFE_TIME 0xFFFFFFFF 10718a31e1eSThomas Graf 1083d1bec99SHannes Frederic Sowa #define IPV6_MAX_STRLEN \ 1093d1bec99SHannes Frederic Sowa sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255") 1103d1bec99SHannes Frederic Sowa 11118a31e1eSThomas Graf static inline u32 cstamp_delta(unsigned long cstamp) 11218a31e1eSThomas Graf { 11318a31e1eSThomas Graf return (cstamp - INITIAL_JIFFIES) * 100UL / HZ; 11418a31e1eSThomas Graf } 1151da177e4SLinus Torvalds 116bd11f074SMaciej Żenczykowski static inline s32 rfc3315_s14_backoff_init(s32 irt) 117bd11f074SMaciej Żenczykowski { 118bd11f074SMaciej Żenczykowski /* multiply 'initial retransmission time' by 0.9 .. 1.1 */ 119bd11f074SMaciej Żenczykowski u64 tmp = (900000 + prandom_u32() % 200001) * (u64)irt; 120bd11f074SMaciej Żenczykowski do_div(tmp, 1000000); 121bd11f074SMaciej Żenczykowski return (s32)tmp; 122bd11f074SMaciej Żenczykowski } 123bd11f074SMaciej Żenczykowski 124bd11f074SMaciej Żenczykowski static inline s32 rfc3315_s14_backoff_update(s32 rt, s32 mrt) 125bd11f074SMaciej Żenczykowski { 126bd11f074SMaciej Żenczykowski /* multiply 'retransmission timeout' by 1.9 .. 2.1 */ 127bd11f074SMaciej Żenczykowski u64 tmp = (1900000 + prandom_u32() % 200001) * (u64)rt; 128bd11f074SMaciej Żenczykowski do_div(tmp, 1000000); 129bd11f074SMaciej Żenczykowski if ((s32)tmp > mrt) { 130bd11f074SMaciej Żenczykowski /* multiply 'maximum retransmission time' by 0.9 .. 1.1 */ 131bd11f074SMaciej Żenczykowski tmp = (900000 + prandom_u32() % 200001) * (u64)mrt; 132bd11f074SMaciej Żenczykowski do_div(tmp, 1000000); 133bd11f074SMaciej Żenczykowski } 134bd11f074SMaciej Żenczykowski return (s32)tmp; 135bd11f074SMaciej Żenczykowski } 136bd11f074SMaciej Żenczykowski 1371da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 138a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev); 139408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev); 140408c4768SPavel Emelyanov #else 141a317a2f1SWANG Cong static inline int addrconf_sysctl_register(struct inet6_dev *idev) 142408c4768SPavel Emelyanov { 143a317a2f1SWANG Cong return 0; 144408c4768SPavel Emelyanov } 145408c4768SPavel Emelyanov 146408c4768SPavel Emelyanov static inline void addrconf_sysctl_unregister(struct inet6_dev *idev) 147408c4768SPavel Emelyanov { 148408c4768SPavel Emelyanov } 1491da177e4SLinus Torvalds #endif 1501da177e4SLinus Torvalds 1519d6280daSJiri Bohac static void ipv6_regen_rndid(struct inet6_dev *idev); 1529d6280daSJiri Bohac static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr); 1531da177e4SLinus Torvalds 1541b34be74SYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev); 1551da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev); 156622c81d5SHannes Frederic Sowa static int ipv6_generate_stable_address(struct in6_addr *addr, 157622c81d5SHannes Frederic Sowa u8 dad_count, 158622c81d5SHannes Frederic Sowa const struct inet6_dev *idev); 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds /* 1611da177e4SLinus Torvalds * Configured unicast address hash table 1621da177e4SLinus Torvalds */ 163c2e21293Sstephen hemminger static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE]; 1645c578aedSstephen hemminger static DEFINE_SPINLOCK(addrconf_hash_lock); 1651da177e4SLinus Torvalds 166c15b1ccaSHannes Frederic Sowa static void addrconf_verify(void); 167c15b1ccaSHannes Frederic Sowa static void addrconf_verify_rtnl(void); 168c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *); 1691da177e4SLinus Torvalds 170c15b1ccaSHannes Frederic Sowa static struct workqueue_struct *addrconf_wq; 171c15b1ccaSHannes Frederic Sowa static DECLARE_DELAYED_WORK(addr_chk_work, addrconf_verify_work); 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds static void addrconf_join_anycast(struct inet6_ifaddr *ifp); 1741da177e4SLinus Torvalds static void addrconf_leave_anycast(struct inet6_ifaddr *ifp); 1751da177e4SLinus Torvalds 17693d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, 17775c78500SMoni Shoua unsigned long event); 1781da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how); 1791da177e4SLinus Torvalds 18021caa662SRomain Kuntz static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx, 18121caa662SRomain Kuntz int plen, 18221caa662SRomain Kuntz const struct net_device *dev, 18321caa662SRomain Kuntz u32 flags, u32 noflags); 18421caa662SRomain Kuntz 185cf22f9a2SDavid S. Miller static void addrconf_dad_start(struct inet6_ifaddr *ifp); 186c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w); 187764d3be6SPaolo Abeni static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id); 188c5e33bddSYOSHIFUJI Hideaki static void addrconf_dad_run(struct inet6_dev *idev); 1891da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data); 1901da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); 1911da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); 1921da177e4SLinus Torvalds 1931da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev, 1941da177e4SLinus Torvalds struct prefix_info *pinfo); 1953e81c6daSDavid S. Miller static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr, 19606bfe655SDaniel Lezcano struct net_device *dev); 1971da177e4SLinus Torvalds 198888c848eSAdrian Bunk static struct ipv6_devconf ipv6_devconf __read_mostly = { 1991da177e4SLinus Torvalds .forwarding = 0, 2001da177e4SLinus Torvalds .hop_limit = IPV6_DEFAULT_HOPLIMIT, 2011da177e4SLinus Torvalds .mtu6 = IPV6_MIN_MTU, 2021da177e4SLinus Torvalds .accept_ra = 1, 2031da177e4SLinus Torvalds .accept_redirects = 1, 2041da177e4SLinus Torvalds .autoconf = 1, 2051da177e4SLinus Torvalds .force_mld_version = 0, 206fc4eba58SHannes Frederic Sowa .mldv1_unsolicited_report_interval = 10 * HZ, 207fc4eba58SHannes Frederic Sowa .mldv2_unsolicited_report_interval = HZ, 2081da177e4SLinus Torvalds .dad_transmits = 1, 2091da177e4SLinus Torvalds .rtr_solicits = MAX_RTR_SOLICITATIONS, 2101da177e4SLinus Torvalds .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, 211bd11f074SMaciej Żenczykowski .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL, 2121da177e4SLinus Torvalds .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, 2131da177e4SLinus Torvalds .use_tempaddr = 0, 2141da177e4SLinus Torvalds .temp_valid_lft = TEMP_VALID_LIFETIME, 2151da177e4SLinus Torvalds .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, 2161da177e4SLinus Torvalds .regen_max_retry = REGEN_MAX_RETRY, 2171da177e4SLinus Torvalds .max_desync_factor = MAX_DESYNC_FACTOR, 2181da177e4SLinus Torvalds .max_addresses = IPV6_MAX_ADDRESSES, 21965f5c7c1SYOSHIFUJI Hideaki .accept_ra_defrtr = 1, 220d9333196SBen Greear .accept_ra_from_local = 0, 2218013d1d7SHangbin Liu .accept_ra_min_hop_limit= 1, 222c4fd30ebSYOSHIFUJI Hideaki .accept_ra_pinfo = 1, 223930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 224930d6ff2SYOSHIFUJI Hideaki .accept_ra_rtr_pref = 1, 22552e16356SYOSHIFUJI Hideaki .rtr_probe_interval = 60 * HZ, 22609c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 22709c884d4SYOSHIFUJI Hideaki .accept_ra_rt_info_max_plen = 0, 22809c884d4SYOSHIFUJI Hideaki #endif 229930d6ff2SYOSHIFUJI Hideaki #endif 230fbea49e1SYOSHIFUJI Hideaki .proxy_ndp = 0, 2310bcbc926SYOSHIFUJI Hideaki .accept_source_route = 0, /* we do not accept RH0 by default. */ 232778d80beSYOSHIFUJI Hideaki .disable_ipv6 = 0, 2331b34be74SYOSHIFUJI Hideaki .accept_dad = 1, 234b800c3b9SHannes Frederic Sowa .suppress_frag_ndisc = 1, 235c2943f14SHarout Hedeshian .accept_ra_mtu = 1, 2363d1bec99SHannes Frederic Sowa .stable_secret = { 2373d1bec99SHannes Frederic Sowa .initialized = false, 2383985e8a3SErik Kline }, 2393985e8a3SErik Kline .use_oif_addrs_only = 0, 24035103d11SAndy Gospodarek .ignore_routes_with_linkdown = 0, 241f1705ec1SDavid Ahern .keep_addr_on_down = 0, 2421ababebaSDavid Lebrun .seg6_enabled = 0, 243bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC 244bf355b8dSDavid Lebrun .seg6_require_hmac = 0, 245bf355b8dSDavid Lebrun #endif 246adc176c5SErik Nordmark .enhanced_dad = 1, 247d35a00b8SFelix Jia .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64, 248*df789fe7SDavid Forster .disable_policy = 0, 2491da177e4SLinus Torvalds }; 2501da177e4SLinus Torvalds 251ab32ea5dSBrian Haley static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = { 2521da177e4SLinus Torvalds .forwarding = 0, 2531da177e4SLinus Torvalds .hop_limit = IPV6_DEFAULT_HOPLIMIT, 2541da177e4SLinus Torvalds .mtu6 = IPV6_MIN_MTU, 2551da177e4SLinus Torvalds .accept_ra = 1, 2561da177e4SLinus Torvalds .accept_redirects = 1, 2571da177e4SLinus Torvalds .autoconf = 1, 258fc4eba58SHannes Frederic Sowa .force_mld_version = 0, 259fc4eba58SHannes Frederic Sowa .mldv1_unsolicited_report_interval = 10 * HZ, 260fc4eba58SHannes Frederic Sowa .mldv2_unsolicited_report_interval = HZ, 2611da177e4SLinus Torvalds .dad_transmits = 1, 2621da177e4SLinus Torvalds .rtr_solicits = MAX_RTR_SOLICITATIONS, 2631da177e4SLinus Torvalds .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, 264bd11f074SMaciej Żenczykowski .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL, 2651da177e4SLinus Torvalds .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, 2661da177e4SLinus Torvalds .use_tempaddr = 0, 2671da177e4SLinus Torvalds .temp_valid_lft = TEMP_VALID_LIFETIME, 2681da177e4SLinus Torvalds .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, 2691da177e4SLinus Torvalds .regen_max_retry = REGEN_MAX_RETRY, 2701da177e4SLinus Torvalds .max_desync_factor = MAX_DESYNC_FACTOR, 2711da177e4SLinus Torvalds .max_addresses = IPV6_MAX_ADDRESSES, 27265f5c7c1SYOSHIFUJI Hideaki .accept_ra_defrtr = 1, 273d9333196SBen Greear .accept_ra_from_local = 0, 2748013d1d7SHangbin Liu .accept_ra_min_hop_limit= 1, 275c4fd30ebSYOSHIFUJI Hideaki .accept_ra_pinfo = 1, 276930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 277930d6ff2SYOSHIFUJI Hideaki .accept_ra_rtr_pref = 1, 27852e16356SYOSHIFUJI Hideaki .rtr_probe_interval = 60 * HZ, 27909c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 28009c884d4SYOSHIFUJI Hideaki .accept_ra_rt_info_max_plen = 0, 28109c884d4SYOSHIFUJI Hideaki #endif 282930d6ff2SYOSHIFUJI Hideaki #endif 283fbea49e1SYOSHIFUJI Hideaki .proxy_ndp = 0, 2840bcbc926SYOSHIFUJI Hideaki .accept_source_route = 0, /* we do not accept RH0 by default. */ 285778d80beSYOSHIFUJI Hideaki .disable_ipv6 = 0, 2861b34be74SYOSHIFUJI Hideaki .accept_dad = 1, 287b800c3b9SHannes Frederic Sowa .suppress_frag_ndisc = 1, 288c2943f14SHarout Hedeshian .accept_ra_mtu = 1, 2893d1bec99SHannes Frederic Sowa .stable_secret = { 2903d1bec99SHannes Frederic Sowa .initialized = false, 2913d1bec99SHannes Frederic Sowa }, 2923985e8a3SErik Kline .use_oif_addrs_only = 0, 29335103d11SAndy Gospodarek .ignore_routes_with_linkdown = 0, 294f1705ec1SDavid Ahern .keep_addr_on_down = 0, 2951ababebaSDavid Lebrun .seg6_enabled = 0, 296bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC 297bf355b8dSDavid Lebrun .seg6_require_hmac = 0, 298bf355b8dSDavid Lebrun #endif 299adc176c5SErik Nordmark .enhanced_dad = 1, 300d35a00b8SFelix Jia .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64, 301*df789fe7SDavid Forster .disable_policy = 0, 3021da177e4SLinus Torvalds }; 3031da177e4SLinus Torvalds 304f24e3d65SMitsuru Chinen /* Check if a valid qdisc is available */ 30505297949SDavid S. Miller static inline bool addrconf_qdisc_ok(const struct net_device *dev) 306f24e3d65SMitsuru Chinen { 30705297949SDavid S. Miller return !qdisc_tx_is_noop(dev); 308f24e3d65SMitsuru Chinen } 309f24e3d65SMitsuru Chinen 310b7b1bfceSHannes Frederic Sowa static void addrconf_del_rs_timer(struct inet6_dev *idev) 3111da177e4SLinus Torvalds { 312b7b1bfceSHannes Frederic Sowa if (del_timer(&idev->rs_timer)) 313b7b1bfceSHannes Frederic Sowa __in6_dev_put(idev); 314b7b1bfceSHannes Frederic Sowa } 315b7b1bfceSHannes Frederic Sowa 316c15b1ccaSHannes Frederic Sowa static void addrconf_del_dad_work(struct inet6_ifaddr *ifp) 317b7b1bfceSHannes Frederic Sowa { 318c15b1ccaSHannes Frederic Sowa if (cancel_delayed_work(&ifp->dad_work)) 3191da177e4SLinus Torvalds __in6_ifa_put(ifp); 3201da177e4SLinus Torvalds } 3211da177e4SLinus Torvalds 322b7b1bfceSHannes Frederic Sowa static void addrconf_mod_rs_timer(struct inet6_dev *idev, 3231da177e4SLinus Torvalds unsigned long when) 3241da177e4SLinus Torvalds { 325b7b1bfceSHannes Frederic Sowa if (!timer_pending(&idev->rs_timer)) 326b7b1bfceSHannes Frederic Sowa in6_dev_hold(idev); 327b7b1bfceSHannes Frederic Sowa mod_timer(&idev->rs_timer, jiffies + when); 3281da177e4SLinus Torvalds } 329b7b1bfceSHannes Frederic Sowa 330c15b1ccaSHannes Frederic Sowa static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp, 331c15b1ccaSHannes Frederic Sowa unsigned long delay) 332b7b1bfceSHannes Frederic Sowa { 333c15b1ccaSHannes Frederic Sowa if (!delayed_work_pending(&ifp->dad_work)) 334b7b1bfceSHannes Frederic Sowa in6_ifa_hold(ifp); 335c15b1ccaSHannes Frederic Sowa mod_delayed_work(addrconf_wq, &ifp->dad_work, delay); 3361da177e4SLinus Torvalds } 3371da177e4SLinus Torvalds 3387f7d9a6bSHerbert Xu static int snmp6_alloc_dev(struct inet6_dev *idev) 3397f7d9a6bSHerbert Xu { 340827da44cSJohn Stultz int i; 341827da44cSJohn Stultz 342698365faSWANG Cong idev->stats.ipv6 = alloc_percpu(struct ipstats_mib); 343698365faSWANG Cong if (!idev->stats.ipv6) 3447f7d9a6bSHerbert Xu goto err_ip; 345827da44cSJohn Stultz 346827da44cSJohn Stultz for_each_possible_cpu(i) { 347827da44cSJohn Stultz struct ipstats_mib *addrconf_stats; 348698365faSWANG Cong addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i); 349827da44cSJohn Stultz u64_stats_init(&addrconf_stats->syncp); 350827da44cSJohn Stultz } 351827da44cSJohn Stultz 352827da44cSJohn Stultz 353be281e55SEric Dumazet idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device), 354be281e55SEric Dumazet GFP_KERNEL); 355be281e55SEric Dumazet if (!idev->stats.icmpv6dev) 3567f7d9a6bSHerbert Xu goto err_icmp; 357be281e55SEric Dumazet idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device), 358be281e55SEric Dumazet GFP_KERNEL); 359be281e55SEric Dumazet if (!idev->stats.icmpv6msgdev) 36014878f75SDavid L Stevens goto err_icmpmsg; 3617f7d9a6bSHerbert Xu 3627f7d9a6bSHerbert Xu return 0; 3637f7d9a6bSHerbert Xu 36414878f75SDavid L Stevens err_icmpmsg: 365be281e55SEric Dumazet kfree(idev->stats.icmpv6dev); 3667f7d9a6bSHerbert Xu err_icmp: 367698365faSWANG Cong free_percpu(idev->stats.ipv6); 3687f7d9a6bSHerbert Xu err_ip: 369aaf70ec7SPavel Emelyanov return -ENOMEM; 3707f7d9a6bSHerbert Xu } 3717f7d9a6bSHerbert Xu 3721da177e4SLinus Torvalds static struct inet6_dev *ipv6_add_dev(struct net_device *dev) 3731da177e4SLinus Torvalds { 3741da177e4SLinus Torvalds struct inet6_dev *ndev; 375a317a2f1SWANG Cong int err = -ENOMEM; 3761da177e4SLinus Torvalds 3771da177e4SLinus Torvalds ASSERT_RTNL(); 3781da177e4SLinus Torvalds 3791da177e4SLinus Torvalds if (dev->mtu < IPV6_MIN_MTU) 380a317a2f1SWANG Cong return ERR_PTR(-EINVAL); 3811da177e4SLinus Torvalds 382322f74a4SIngo Oeser ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL); 38363159f29SIan Morris if (!ndev) 384a317a2f1SWANG Cong return ERR_PTR(err); 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds rwlock_init(&ndev->lock); 3871da177e4SLinus Torvalds ndev->dev = dev; 388502a2ffdSstephen hemminger INIT_LIST_HEAD(&ndev->addr_list); 389b7b1bfceSHannes Frederic Sowa setup_timer(&ndev->rs_timer, addrconf_rs_timer, 390b7b1bfceSHannes Frederic Sowa (unsigned long)ndev); 391c346dca1SYOSHIFUJI Hideaki memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf)); 3929b29c696SHannes Frederic Sowa 3939b29c696SHannes Frederic Sowa if (ndev->cnf.stable_secret.initialized) 394d35a00b8SFelix Jia ndev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY; 3959b29c696SHannes Frederic Sowa else 396d35a00b8SFelix Jia ndev->cnf.addr_gen_mode = ipv6_devconf_dflt.addr_gen_mode; 3979b29c696SHannes Frederic Sowa 3981da177e4SLinus Torvalds ndev->cnf.mtu6 = dev->mtu; 3991da177e4SLinus Torvalds ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl); 40063159f29SIan Morris if (!ndev->nd_parms) { 4011da177e4SLinus Torvalds kfree(ndev); 402a317a2f1SWANG Cong return ERR_PTR(err); 4031da177e4SLinus Torvalds } 4040187bdfbSBen Hutchings if (ndev->cnf.forwarding) 4050187bdfbSBen Hutchings dev_disable_lro(dev); 4061da177e4SLinus Torvalds /* We refer to the device */ 4071da177e4SLinus Torvalds dev_hold(dev); 4081da177e4SLinus Torvalds 4091da177e4SLinus Torvalds if (snmp6_alloc_dev(ndev) < 0) { 410ba3542e1Sdingtianhong ADBG(KERN_WARNING 411f3213831SJoe Perches "%s: cannot allocate memory for statistics; dev=%s.\n", 412ba3542e1Sdingtianhong __func__, dev->name); 4131da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, ndev->nd_parms); 4148603e33dSRoy Li dev_put(dev); 4158603e33dSRoy Li kfree(ndev); 416a317a2f1SWANG Cong return ERR_PTR(err); 4171da177e4SLinus Torvalds } 4181da177e4SLinus Torvalds 4191da177e4SLinus Torvalds if (snmp6_register_dev(ndev) < 0) { 420ba3542e1Sdingtianhong ADBG(KERN_WARNING 421f3213831SJoe Perches "%s: cannot create /proc/net/dev_snmp6/%s\n", 422ba3542e1Sdingtianhong __func__, dev->name); 423a317a2f1SWANG Cong goto err_release; 4241da177e4SLinus Torvalds } 4251da177e4SLinus Torvalds 4269d6280daSJiri Bohac /* One reference from device. */ 4271da177e4SLinus Torvalds in6_dev_hold(ndev); 4281da177e4SLinus Torvalds 4291b34be74SYOSHIFUJI Hideaki if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) 4301b34be74SYOSHIFUJI Hideaki ndev->cnf.accept_dad = -1; 4311b34be74SYOSHIFUJI Hideaki 43207a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 433c7dc89c0SFred L. Templin if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) { 434f3213831SJoe Perches pr_info("%s: Disabled Multicast RS\n", dev->name); 435c7dc89c0SFred L. Templin ndev->cnf.rtr_solicits = 0; 436c7dc89c0SFred L. Templin } 437b077d7abSYOSHIFUJI Hideaki #endif 438b077d7abSYOSHIFUJI Hideaki 439372e6c8fSstephen hemminger INIT_LIST_HEAD(&ndev->tempaddr_list); 44076506a98SJiri Bohac ndev->desync_factor = U32_MAX; 4411da177e4SLinus Torvalds if ((dev->flags&IFF_LOOPBACK) || 4421da177e4SLinus Torvalds dev->type == ARPHRD_TUNNEL || 4439625ed72SYOSHIFUJI Hideaki dev->type == ARPHRD_TUNNEL6 || 4441da177e4SLinus Torvalds dev->type == ARPHRD_SIT || 4451da177e4SLinus Torvalds dev->type == ARPHRD_NONE) { 4460be669bbSJoerg Roedel ndev->cnf.use_tempaddr = -1; 4479d6280daSJiri Bohac } else 4489d6280daSJiri Bohac ipv6_regen_rndid(ndev); 4495d9efa7eSDavid S. Miller 450914faa14SDaniel Borkmann ndev->token = in6addr_any; 4511da177e4SLinus Torvalds 452f24e3d65SMitsuru Chinen if (netif_running(dev) && addrconf_qdisc_ok(dev)) 45353aadcc9SHerbert Xu ndev->if_flags |= IF_READY; 45453aadcc9SHerbert Xu 4551da177e4SLinus Torvalds ipv6_mc_init_dev(ndev); 4561da177e4SLinus Torvalds ndev->tstamp = jiffies; 457a317a2f1SWANG Cong err = addrconf_sysctl_register(ndev); 458a317a2f1SWANG Cong if (err) { 459a317a2f1SWANG Cong ipv6_mc_destroy_dev(ndev); 4602a189f9eSSabrina Dubroca snmp6_unregister_dev(ndev); 461a317a2f1SWANG Cong goto err_release; 462a317a2f1SWANG Cong } 46330c4cf57SDavid L Stevens /* protected by rtnl_lock */ 464cf778b00SEric Dumazet rcu_assign_pointer(dev->ip6_ptr, ndev); 465d88ae4ccSYOSHIFUJI Hideaki 4662c5e8933SHannes Frederic Sowa /* Join interface-local all-node multicast group */ 4672c5e8933SHannes Frederic Sowa ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes); 4682c5e8933SHannes Frederic Sowa 469d88ae4ccSYOSHIFUJI Hideaki /* Join all-node multicast group */ 470f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes); 471d88ae4ccSYOSHIFUJI Hideaki 472d6ddef9eSLi Wei /* Join all-router multicast group if forwarding is set */ 4738b2aaedeSLi Wei if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST)) 474d6ddef9eSLi Wei ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters); 475d6ddef9eSLi Wei 4761da177e4SLinus Torvalds return ndev; 477a317a2f1SWANG Cong 478a317a2f1SWANG Cong err_release: 479a317a2f1SWANG Cong neigh_parms_release(&nd_tbl, ndev->nd_parms); 480a317a2f1SWANG Cong ndev->dead = 1; 481a317a2f1SWANG Cong in6_dev_finish_destroy(ndev); 482a317a2f1SWANG Cong return ERR_PTR(err); 4831da177e4SLinus Torvalds } 4841da177e4SLinus Torvalds 485cee89473SYOSHIFUJI Hideaki static struct inet6_dev *ipv6_find_idev(struct net_device *dev) 4861da177e4SLinus Torvalds { 4871da177e4SLinus Torvalds struct inet6_dev *idev; 4881da177e4SLinus Torvalds 4891da177e4SLinus Torvalds ASSERT_RTNL(); 4901da177e4SLinus Torvalds 491e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 492e21e8467SStephen Hemminger if (!idev) { 493e21e8467SStephen Hemminger idev = ipv6_add_dev(dev); 494a317a2f1SWANG Cong if (IS_ERR(idev)) 4951da177e4SLinus Torvalds return NULL; 4961da177e4SLinus Torvalds } 497c5e33bddSYOSHIFUJI Hideaki 4981da177e4SLinus Torvalds if (dev->flags&IFF_UP) 4991da177e4SLinus Torvalds ipv6_mc_up(idev); 5001da177e4SLinus Torvalds return idev; 5011da177e4SLinus Torvalds } 5021da177e4SLinus Torvalds 503f3a1bfb1SNicolas Dichtel static int inet6_netconf_msgsize_devconf(int type) 504f3a1bfb1SNicolas Dichtel { 505f3a1bfb1SNicolas Dichtel int size = NLMSG_ALIGN(sizeof(struct netconfmsg)) 506f3a1bfb1SNicolas Dichtel + nla_total_size(4); /* NETCONFA_IFINDEX */ 507136ba622SZhang Shengju bool all = false; 508f3a1bfb1SNicolas Dichtel 509136ba622SZhang Shengju if (type == NETCONFA_ALL) 510136ba622SZhang Shengju all = true; 511136ba622SZhang Shengju 512136ba622SZhang Shengju if (all || type == NETCONFA_FORWARDING) 513f3a1bfb1SNicolas Dichtel size += nla_total_size(4); 514b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE 515136ba622SZhang Shengju if (all || type == NETCONFA_MC_FORWARDING) 516d67b8c61SNicolas Dichtel size += nla_total_size(4); 517b1afce95SDavid S. Miller #endif 518136ba622SZhang Shengju if (all || type == NETCONFA_PROXY_NEIGH) 519c92d5491Sstephen hemminger size += nla_total_size(4); 520f3a1bfb1SNicolas Dichtel 521136ba622SZhang Shengju if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) 52235103d11SAndy Gospodarek size += nla_total_size(4); 52335103d11SAndy Gospodarek 524f3a1bfb1SNicolas Dichtel return size; 525f3a1bfb1SNicolas Dichtel } 526f3a1bfb1SNicolas Dichtel 527f3a1bfb1SNicolas Dichtel static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex, 528f3a1bfb1SNicolas Dichtel struct ipv6_devconf *devconf, u32 portid, 529f3a1bfb1SNicolas Dichtel u32 seq, int event, unsigned int flags, 530f3a1bfb1SNicolas Dichtel int type) 531f3a1bfb1SNicolas Dichtel { 532f3a1bfb1SNicolas Dichtel struct nlmsghdr *nlh; 533f3a1bfb1SNicolas Dichtel struct netconfmsg *ncm; 534136ba622SZhang Shengju bool all = false; 535f3a1bfb1SNicolas Dichtel 536f3a1bfb1SNicolas Dichtel nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg), 537f3a1bfb1SNicolas Dichtel flags); 53863159f29SIan Morris if (!nlh) 539f3a1bfb1SNicolas Dichtel return -EMSGSIZE; 540f3a1bfb1SNicolas Dichtel 541136ba622SZhang Shengju if (type == NETCONFA_ALL) 542136ba622SZhang Shengju all = true; 543136ba622SZhang Shengju 544f3a1bfb1SNicolas Dichtel ncm = nlmsg_data(nlh); 545f3a1bfb1SNicolas Dichtel ncm->ncm_family = AF_INET6; 546f3a1bfb1SNicolas Dichtel 547f3a1bfb1SNicolas Dichtel if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0) 548f3a1bfb1SNicolas Dichtel goto nla_put_failure; 549f3a1bfb1SNicolas Dichtel 550136ba622SZhang Shengju if ((all || type == NETCONFA_FORWARDING) && 551f3a1bfb1SNicolas Dichtel nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0) 552f3a1bfb1SNicolas Dichtel goto nla_put_failure; 553b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE 554136ba622SZhang Shengju if ((all || type == NETCONFA_MC_FORWARDING) && 555d67b8c61SNicolas Dichtel nla_put_s32(skb, NETCONFA_MC_FORWARDING, 556d67b8c61SNicolas Dichtel devconf->mc_forwarding) < 0) 557d67b8c61SNicolas Dichtel goto nla_put_failure; 558b1afce95SDavid S. Miller #endif 559136ba622SZhang Shengju if ((all || type == NETCONFA_PROXY_NEIGH) && 560c92d5491Sstephen hemminger nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0) 561c92d5491Sstephen hemminger goto nla_put_failure; 562c92d5491Sstephen hemminger 563136ba622SZhang Shengju if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) && 56435103d11SAndy Gospodarek nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 56535103d11SAndy Gospodarek devconf->ignore_routes_with_linkdown) < 0) 56635103d11SAndy Gospodarek goto nla_put_failure; 56735103d11SAndy Gospodarek 568053c095aSJohannes Berg nlmsg_end(skb, nlh); 569053c095aSJohannes Berg return 0; 570f3a1bfb1SNicolas Dichtel 571f3a1bfb1SNicolas Dichtel nla_put_failure: 572f3a1bfb1SNicolas Dichtel nlmsg_cancel(skb, nlh); 573f3a1bfb1SNicolas Dichtel return -EMSGSIZE; 574f3a1bfb1SNicolas Dichtel } 575f3a1bfb1SNicolas Dichtel 576d67b8c61SNicolas Dichtel void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex, 577f3a1bfb1SNicolas Dichtel struct ipv6_devconf *devconf) 578f3a1bfb1SNicolas Dichtel { 579f3a1bfb1SNicolas Dichtel struct sk_buff *skb; 580f3a1bfb1SNicolas Dichtel int err = -ENOBUFS; 581f3a1bfb1SNicolas Dichtel 582927265bcSEric Dumazet skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_KERNEL); 58363159f29SIan Morris if (!skb) 584f3a1bfb1SNicolas Dichtel goto errout; 585f3a1bfb1SNicolas Dichtel 586f3a1bfb1SNicolas Dichtel err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0, 587f3a1bfb1SNicolas Dichtel RTM_NEWNETCONF, 0, type); 588f3a1bfb1SNicolas Dichtel if (err < 0) { 589f3a1bfb1SNicolas Dichtel /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */ 590f3a1bfb1SNicolas Dichtel WARN_ON(err == -EMSGSIZE); 591f3a1bfb1SNicolas Dichtel kfree_skb(skb); 592f3a1bfb1SNicolas Dichtel goto errout; 593f3a1bfb1SNicolas Dichtel } 594927265bcSEric Dumazet rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_KERNEL); 595f3a1bfb1SNicolas Dichtel return; 596f3a1bfb1SNicolas Dichtel errout: 597f3a1bfb1SNicolas Dichtel rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err); 598f3a1bfb1SNicolas Dichtel } 599f3a1bfb1SNicolas Dichtel 60076f8f6cbSNicolas Dichtel static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = { 60176f8f6cbSNicolas Dichtel [NETCONFA_IFINDEX] = { .len = sizeof(int) }, 60276f8f6cbSNicolas Dichtel [NETCONFA_FORWARDING] = { .len = sizeof(int) }, 603c92d5491Sstephen hemminger [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) }, 60435103d11SAndy Gospodarek [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) }, 60576f8f6cbSNicolas Dichtel }; 60676f8f6cbSNicolas Dichtel 60776f8f6cbSNicolas Dichtel static int inet6_netconf_get_devconf(struct sk_buff *in_skb, 608661d2967SThomas Graf struct nlmsghdr *nlh) 60976f8f6cbSNicolas Dichtel { 61076f8f6cbSNicolas Dichtel struct net *net = sock_net(in_skb->sk); 61176f8f6cbSNicolas Dichtel struct nlattr *tb[NETCONFA_MAX+1]; 61276f8f6cbSNicolas Dichtel struct netconfmsg *ncm; 61376f8f6cbSNicolas Dichtel struct sk_buff *skb; 61476f8f6cbSNicolas Dichtel struct ipv6_devconf *devconf; 61576f8f6cbSNicolas Dichtel struct inet6_dev *in6_dev; 61676f8f6cbSNicolas Dichtel struct net_device *dev; 61776f8f6cbSNicolas Dichtel int ifindex; 61876f8f6cbSNicolas Dichtel int err; 61976f8f6cbSNicolas Dichtel 62076f8f6cbSNicolas Dichtel err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX, 62176f8f6cbSNicolas Dichtel devconf_ipv6_policy); 62276f8f6cbSNicolas Dichtel if (err < 0) 62376f8f6cbSNicolas Dichtel goto errout; 62476f8f6cbSNicolas Dichtel 625a97eb33fSAnton Protopopov err = -EINVAL; 62676f8f6cbSNicolas Dichtel if (!tb[NETCONFA_IFINDEX]) 62776f8f6cbSNicolas Dichtel goto errout; 62876f8f6cbSNicolas Dichtel 62976f8f6cbSNicolas Dichtel ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]); 63076f8f6cbSNicolas Dichtel switch (ifindex) { 63176f8f6cbSNicolas Dichtel case NETCONFA_IFINDEX_ALL: 63276f8f6cbSNicolas Dichtel devconf = net->ipv6.devconf_all; 63376f8f6cbSNicolas Dichtel break; 63476f8f6cbSNicolas Dichtel case NETCONFA_IFINDEX_DEFAULT: 63576f8f6cbSNicolas Dichtel devconf = net->ipv6.devconf_dflt; 63676f8f6cbSNicolas Dichtel break; 63776f8f6cbSNicolas Dichtel default: 63876f8f6cbSNicolas Dichtel dev = __dev_get_by_index(net, ifindex); 63963159f29SIan Morris if (!dev) 64076f8f6cbSNicolas Dichtel goto errout; 64176f8f6cbSNicolas Dichtel in6_dev = __in6_dev_get(dev); 64263159f29SIan Morris if (!in6_dev) 64376f8f6cbSNicolas Dichtel goto errout; 64476f8f6cbSNicolas Dichtel devconf = &in6_dev->cnf; 64576f8f6cbSNicolas Dichtel break; 64676f8f6cbSNicolas Dichtel } 64776f8f6cbSNicolas Dichtel 64876f8f6cbSNicolas Dichtel err = -ENOBUFS; 649136ba622SZhang Shengju skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_ATOMIC); 65063159f29SIan Morris if (!skb) 65176f8f6cbSNicolas Dichtel goto errout; 65276f8f6cbSNicolas Dichtel 65376f8f6cbSNicolas Dichtel err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 65476f8f6cbSNicolas Dichtel NETLINK_CB(in_skb).portid, 65576f8f6cbSNicolas Dichtel nlh->nlmsg_seq, RTM_NEWNETCONF, 0, 656136ba622SZhang Shengju NETCONFA_ALL); 65776f8f6cbSNicolas Dichtel if (err < 0) { 65876f8f6cbSNicolas Dichtel /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */ 65976f8f6cbSNicolas Dichtel WARN_ON(err == -EMSGSIZE); 66076f8f6cbSNicolas Dichtel kfree_skb(skb); 66176f8f6cbSNicolas Dichtel goto errout; 66276f8f6cbSNicolas Dichtel } 66376f8f6cbSNicolas Dichtel err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 66476f8f6cbSNicolas Dichtel errout: 66576f8f6cbSNicolas Dichtel return err; 66676f8f6cbSNicolas Dichtel } 66776f8f6cbSNicolas Dichtel 6687a674200SNicolas Dichtel static int inet6_netconf_dump_devconf(struct sk_buff *skb, 6697a674200SNicolas Dichtel struct netlink_callback *cb) 6707a674200SNicolas Dichtel { 6717a674200SNicolas Dichtel struct net *net = sock_net(skb->sk); 6727a674200SNicolas Dichtel int h, s_h; 6737a674200SNicolas Dichtel int idx, s_idx; 6747a674200SNicolas Dichtel struct net_device *dev; 6757a674200SNicolas Dichtel struct inet6_dev *idev; 6767a674200SNicolas Dichtel struct hlist_head *head; 6777a674200SNicolas Dichtel 6787a674200SNicolas Dichtel s_h = cb->args[0]; 6797a674200SNicolas Dichtel s_idx = idx = cb->args[1]; 6807a674200SNicolas Dichtel 6817a674200SNicolas Dichtel for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 6827a674200SNicolas Dichtel idx = 0; 6837a674200SNicolas Dichtel head = &net->dev_index_head[h]; 6847a674200SNicolas Dichtel rcu_read_lock(); 68563998ac2SNicolas Dichtel cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ 68663998ac2SNicolas Dichtel net->dev_base_seq; 6877a674200SNicolas Dichtel hlist_for_each_entry_rcu(dev, head, index_hlist) { 6887a674200SNicolas Dichtel if (idx < s_idx) 6897a674200SNicolas Dichtel goto cont; 6907a674200SNicolas Dichtel idev = __in6_dev_get(dev); 6917a674200SNicolas Dichtel if (!idev) 6927a674200SNicolas Dichtel goto cont; 6937a674200SNicolas Dichtel 6947a674200SNicolas Dichtel if (inet6_netconf_fill_devconf(skb, dev->ifindex, 6957a674200SNicolas Dichtel &idev->cnf, 6967a674200SNicolas Dichtel NETLINK_CB(cb->skb).portid, 6977a674200SNicolas Dichtel cb->nlh->nlmsg_seq, 6987a674200SNicolas Dichtel RTM_NEWNETCONF, 6997a674200SNicolas Dichtel NLM_F_MULTI, 700136ba622SZhang Shengju NETCONFA_ALL) < 0) { 7017a674200SNicolas Dichtel rcu_read_unlock(); 7027a674200SNicolas Dichtel goto done; 7037a674200SNicolas Dichtel } 70463998ac2SNicolas Dichtel nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 7057a674200SNicolas Dichtel cont: 7067a674200SNicolas Dichtel idx++; 7077a674200SNicolas Dichtel } 7087a674200SNicolas Dichtel rcu_read_unlock(); 7097a674200SNicolas Dichtel } 7107a674200SNicolas Dichtel if (h == NETDEV_HASHENTRIES) { 7117a674200SNicolas Dichtel if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL, 7127a674200SNicolas Dichtel net->ipv6.devconf_all, 7137a674200SNicolas Dichtel NETLINK_CB(cb->skb).portid, 7147a674200SNicolas Dichtel cb->nlh->nlmsg_seq, 7157a674200SNicolas Dichtel RTM_NEWNETCONF, NLM_F_MULTI, 716136ba622SZhang Shengju NETCONFA_ALL) < 0) 7177a674200SNicolas Dichtel goto done; 7187a674200SNicolas Dichtel else 7197a674200SNicolas Dichtel h++; 7207a674200SNicolas Dichtel } 7217a674200SNicolas Dichtel if (h == NETDEV_HASHENTRIES + 1) { 7227a674200SNicolas Dichtel if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT, 7237a674200SNicolas Dichtel net->ipv6.devconf_dflt, 7247a674200SNicolas Dichtel NETLINK_CB(cb->skb).portid, 7257a674200SNicolas Dichtel cb->nlh->nlmsg_seq, 7267a674200SNicolas Dichtel RTM_NEWNETCONF, NLM_F_MULTI, 727136ba622SZhang Shengju NETCONFA_ALL) < 0) 7287a674200SNicolas Dichtel goto done; 7297a674200SNicolas Dichtel else 7307a674200SNicolas Dichtel h++; 7317a674200SNicolas Dichtel } 7327a674200SNicolas Dichtel done: 7337a674200SNicolas Dichtel cb->args[0] = h; 7347a674200SNicolas Dichtel cb->args[1] = idx; 7357a674200SNicolas Dichtel 7367a674200SNicolas Dichtel return skb->len; 7377a674200SNicolas Dichtel } 7387a674200SNicolas Dichtel 7391da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 7401da177e4SLinus Torvalds static void dev_forward_change(struct inet6_dev *idev) 7411da177e4SLinus Torvalds { 7421da177e4SLinus Torvalds struct net_device *dev; 7431da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 7441da177e4SLinus Torvalds 7451da177e4SLinus Torvalds if (!idev) 7461da177e4SLinus Torvalds return; 7471da177e4SLinus Torvalds dev = idev->dev; 7480187bdfbSBen Hutchings if (idev->cnf.forwarding) 7490187bdfbSBen Hutchings dev_disable_lro(dev); 7501a940835SAmerigo Wang if (dev->flags & IFF_MULTICAST) { 7512c5e8933SHannes Frederic Sowa if (idev->cnf.forwarding) { 752f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters); 7532c5e8933SHannes Frederic Sowa ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters); 7542c5e8933SHannes Frederic Sowa ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters); 7552c5e8933SHannes Frederic Sowa } else { 756f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters); 7572c5e8933SHannes Frederic Sowa ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters); 7582c5e8933SHannes Frederic Sowa ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters); 7592c5e8933SHannes Frederic Sowa } 7601da177e4SLinus Torvalds } 761502a2ffdSstephen hemminger 762502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 7632c12a74cSMichal Wrobel if (ifa->flags&IFA_F_TENTATIVE) 7642c12a74cSMichal Wrobel continue; 7651da177e4SLinus Torvalds if (idev->cnf.forwarding) 7661da177e4SLinus Torvalds addrconf_join_anycast(ifa); 7671da177e4SLinus Torvalds else 7681da177e4SLinus Torvalds addrconf_leave_anycast(ifa); 7691da177e4SLinus Torvalds } 770f3a1bfb1SNicolas Dichtel inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING, 771f3a1bfb1SNicolas Dichtel dev->ifindex, &idev->cnf); 7721da177e4SLinus Torvalds } 7731da177e4SLinus Torvalds 7741da177e4SLinus Torvalds 775e186932bSPavel Emelyanov static void addrconf_forward_change(struct net *net, __s32 newf) 7761da177e4SLinus Torvalds { 7771da177e4SLinus Torvalds struct net_device *dev; 7781da177e4SLinus Torvalds struct inet6_dev *idev; 7791da177e4SLinus Torvalds 7804acd4945SBen Hutchings for_each_netdev(net, dev) { 7811da177e4SLinus Torvalds idev = __in6_dev_get(dev); 7821da177e4SLinus Torvalds if (idev) { 783e186932bSPavel Emelyanov int changed = (!idev->cnf.forwarding) ^ (!newf); 784e186932bSPavel Emelyanov idev->cnf.forwarding = newf; 7851da177e4SLinus Torvalds if (changed) 7861da177e4SLinus Torvalds dev_forward_change(idev); 7871da177e4SLinus Torvalds } 7881da177e4SLinus Torvalds } 7891da177e4SLinus Torvalds } 790c8fecf22SPavel Emelyanov 791013d97e9SFrancesco Ruggeri static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf) 792c8fecf22SPavel Emelyanov { 793bff16c2fSPavel Emelyanov struct net *net; 794013d97e9SFrancesco Ruggeri int old; 795013d97e9SFrancesco Ruggeri 796013d97e9SFrancesco Ruggeri if (!rtnl_trylock()) 797013d97e9SFrancesco Ruggeri return restart_syscall(); 798bff16c2fSPavel Emelyanov 799bff16c2fSPavel Emelyanov net = (struct net *)table->extra2; 800013d97e9SFrancesco Ruggeri old = *p; 801013d97e9SFrancesco Ruggeri *p = newf; 802c8fecf22SPavel Emelyanov 803013d97e9SFrancesco Ruggeri if (p == &net->ipv6.devconf_dflt->forwarding) { 804f3a1bfb1SNicolas Dichtel if ((!newf) ^ (!old)) 805f3a1bfb1SNicolas Dichtel inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING, 806f3a1bfb1SNicolas Dichtel NETCONFA_IFINDEX_DEFAULT, 807f3a1bfb1SNicolas Dichtel net->ipv6.devconf_dflt); 808013d97e9SFrancesco Ruggeri rtnl_unlock(); 809013d97e9SFrancesco Ruggeri return 0; 81088af182eSEric W. Biederman } 811b325fddbSStephen Hemminger 812e186932bSPavel Emelyanov if (p == &net->ipv6.devconf_all->forwarding) { 813d26c638cSNicolas Dichtel int old_dflt = net->ipv6.devconf_dflt->forwarding; 814d26c638cSNicolas Dichtel 815e186932bSPavel Emelyanov net->ipv6.devconf_dflt->forwarding = newf; 816d26c638cSNicolas Dichtel if ((!newf) ^ (!old_dflt)) 817d26c638cSNicolas Dichtel inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING, 818d26c638cSNicolas Dichtel NETCONFA_IFINDEX_DEFAULT, 819d26c638cSNicolas Dichtel net->ipv6.devconf_dflt); 820d26c638cSNicolas Dichtel 821e186932bSPavel Emelyanov addrconf_forward_change(net, newf); 822f3a1bfb1SNicolas Dichtel if ((!newf) ^ (!old)) 823f3a1bfb1SNicolas Dichtel inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING, 824f3a1bfb1SNicolas Dichtel NETCONFA_IFINDEX_ALL, 825f3a1bfb1SNicolas Dichtel net->ipv6.devconf_all); 826013d97e9SFrancesco Ruggeri } else if ((!newf) ^ (!old)) 827c8fecf22SPavel Emelyanov dev_forward_change((struct inet6_dev *)table->extra1); 8280187bdfbSBen Hutchings rtnl_unlock(); 829c8fecf22SPavel Emelyanov 830013d97e9SFrancesco Ruggeri if (newf) 8317b4da532SDaniel Lezcano rt6_purge_dflt_routers(net); 832b325fddbSStephen Hemminger return 1; 833c8fecf22SPavel Emelyanov } 83435103d11SAndy Gospodarek 83535103d11SAndy Gospodarek static void addrconf_linkdown_change(struct net *net, __s32 newf) 83635103d11SAndy Gospodarek { 83735103d11SAndy Gospodarek struct net_device *dev; 83835103d11SAndy Gospodarek struct inet6_dev *idev; 83935103d11SAndy Gospodarek 84035103d11SAndy Gospodarek for_each_netdev(net, dev) { 84135103d11SAndy Gospodarek idev = __in6_dev_get(dev); 84235103d11SAndy Gospodarek if (idev) { 84335103d11SAndy Gospodarek int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf); 84435103d11SAndy Gospodarek 84535103d11SAndy Gospodarek idev->cnf.ignore_routes_with_linkdown = newf; 84635103d11SAndy Gospodarek if (changed) 84735103d11SAndy Gospodarek inet6_netconf_notify_devconf(dev_net(dev), 84835103d11SAndy Gospodarek NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 84935103d11SAndy Gospodarek dev->ifindex, 85035103d11SAndy Gospodarek &idev->cnf); 85135103d11SAndy Gospodarek } 85235103d11SAndy Gospodarek } 85335103d11SAndy Gospodarek } 85435103d11SAndy Gospodarek 85535103d11SAndy Gospodarek static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf) 85635103d11SAndy Gospodarek { 85735103d11SAndy Gospodarek struct net *net; 85835103d11SAndy Gospodarek int old; 85935103d11SAndy Gospodarek 86035103d11SAndy Gospodarek if (!rtnl_trylock()) 86135103d11SAndy Gospodarek return restart_syscall(); 86235103d11SAndy Gospodarek 86335103d11SAndy Gospodarek net = (struct net *)table->extra2; 86435103d11SAndy Gospodarek old = *p; 86535103d11SAndy Gospodarek *p = newf; 86635103d11SAndy Gospodarek 86735103d11SAndy Gospodarek if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) { 86835103d11SAndy Gospodarek if ((!newf) ^ (!old)) 86935103d11SAndy Gospodarek inet6_netconf_notify_devconf(net, 87035103d11SAndy Gospodarek NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 87135103d11SAndy Gospodarek NETCONFA_IFINDEX_DEFAULT, 87235103d11SAndy Gospodarek net->ipv6.devconf_dflt); 87335103d11SAndy Gospodarek rtnl_unlock(); 87435103d11SAndy Gospodarek return 0; 87535103d11SAndy Gospodarek } 87635103d11SAndy Gospodarek 87735103d11SAndy Gospodarek if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) { 87835103d11SAndy Gospodarek net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf; 87935103d11SAndy Gospodarek addrconf_linkdown_change(net, newf); 88035103d11SAndy Gospodarek if ((!newf) ^ (!old)) 88135103d11SAndy Gospodarek inet6_netconf_notify_devconf(net, 88235103d11SAndy Gospodarek NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 88335103d11SAndy Gospodarek NETCONFA_IFINDEX_ALL, 88435103d11SAndy Gospodarek net->ipv6.devconf_all); 88535103d11SAndy Gospodarek } 88635103d11SAndy Gospodarek rtnl_unlock(); 88735103d11SAndy Gospodarek 88835103d11SAndy Gospodarek return 1; 88935103d11SAndy Gospodarek } 89035103d11SAndy Gospodarek 8911da177e4SLinus Torvalds #endif 8921da177e4SLinus Torvalds 8935c578aedSstephen hemminger /* Nobody refers to this ifaddr, destroy it */ 8941da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp) 8951da177e4SLinus Torvalds { 896c2e21293Sstephen hemminger WARN_ON(!hlist_unhashed(&ifp->addr_lst)); 897547b792cSIlpo Järvinen 8981da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG 89991df42beSJoe Perches pr_debug("%s\n", __func__); 9001da177e4SLinus Torvalds #endif 9011da177e4SLinus Torvalds 9021da177e4SLinus Torvalds in6_dev_put(ifp->idev); 9031da177e4SLinus Torvalds 904c15b1ccaSHannes Frederic Sowa if (cancel_delayed_work(&ifp->dad_work)) 905c15b1ccaSHannes Frederic Sowa pr_notice("delayed DAD work was pending while freeing ifa=%p\n", 906c15b1ccaSHannes Frederic Sowa ifp); 9071da177e4SLinus Torvalds 908e9d3e084SHerbert Xu if (ifp->state != INET6_IFADDR_STATE_DEAD) { 909f3213831SJoe Perches pr_warn("Freeing alive inet6 address %p\n", ifp); 9101da177e4SLinus Torvalds return; 9111da177e4SLinus Torvalds } 91294e187c0SAmerigo Wang ip6_rt_put(ifp->rt); 9131da177e4SLinus Torvalds 914e5785985SLai Jiangshan kfree_rcu(ifp, rcu); 9151da177e4SLinus Torvalds } 9161da177e4SLinus Torvalds 917e55ffac6SBrian Haley static void 918e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp) 919e55ffac6SBrian Haley { 920502a2ffdSstephen hemminger struct list_head *p; 9218a6ce0c0SYOSHIFUJI Hideaki int ifp_scope = ipv6_addr_src_scope(&ifp->addr); 922e55ffac6SBrian Haley 923e55ffac6SBrian Haley /* 924e55ffac6SBrian Haley * Each device address list is sorted in order of scope - 925e55ffac6SBrian Haley * global before linklocal. 926e55ffac6SBrian Haley */ 927502a2ffdSstephen hemminger list_for_each(p, &idev->addr_list) { 928502a2ffdSstephen hemminger struct inet6_ifaddr *ifa 929502a2ffdSstephen hemminger = list_entry(p, struct inet6_ifaddr, if_list); 9308a6ce0c0SYOSHIFUJI Hideaki if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr)) 931e55ffac6SBrian Haley break; 932e55ffac6SBrian Haley } 933e55ffac6SBrian Haley 934b54c9b98SDavid S. Miller list_add_tail(&ifp->if_list, p); 935e55ffac6SBrian Haley } 936e55ffac6SBrian Haley 937ddbe5032SEric Dumazet static u32 inet6_addr_hash(const struct in6_addr *addr) 9383eb84f49SYOSHIFUJI Hideaki { 939ddbe5032SEric Dumazet return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT); 9403eb84f49SYOSHIFUJI Hideaki } 9413eb84f49SYOSHIFUJI Hideaki 9421da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */ 9431da177e4SLinus Torvalds 9441da177e4SLinus Torvalds static struct inet6_ifaddr * 9453f8f5298SJiri Pirko ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, 9463f8f5298SJiri Pirko const struct in6_addr *peer_addr, int pfxlen, 9478a226b2cSJiri Benc int scope, u32 flags, u32 valid_lft, u32 prefered_lft) 9481da177e4SLinus Torvalds { 949*df789fe7SDavid Forster struct net *net = dev_net(idev->dev); 9501da177e4SLinus Torvalds struct inet6_ifaddr *ifa = NULL; 9511da177e4SLinus Torvalds struct rt6_info *rt; 9523a88a81dSstephen hemminger unsigned int hash; 9531da177e4SLinus Torvalds int err = 0; 954d68b8270SYOSHIFUJI Hideaki int addr_type = ipv6_addr_type(addr); 955d68b8270SYOSHIFUJI Hideaki 956d68b8270SYOSHIFUJI Hideaki if (addr_type == IPV6_ADDR_ANY || 957d68b8270SYOSHIFUJI Hideaki addr_type & IPV6_ADDR_MULTICAST || 958d68b8270SYOSHIFUJI Hideaki (!(idev->dev->flags & IFF_LOOPBACK) && 959d68b8270SYOSHIFUJI Hideaki addr_type & IPV6_ADDR_LOOPBACK)) 960d68b8270SYOSHIFUJI Hideaki return ERR_PTR(-EADDRNOTAVAIL); 9611da177e4SLinus Torvalds 9628814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 9631da177e4SLinus Torvalds if (idev->dead) { 9641da177e4SLinus Torvalds err = -ENODEV; /*XXX*/ 9651da177e4SLinus Torvalds goto out2; 9661da177e4SLinus Torvalds } 9671da177e4SLinus Torvalds 96856d417b1SBrian Haley if (idev->cnf.disable_ipv6) { 9699bdd8d40SBrian Haley err = -EACCES; 9709bdd8d40SBrian Haley goto out2; 9719bdd8d40SBrian Haley } 9729bdd8d40SBrian Haley 9735c578aedSstephen hemminger spin_lock(&addrconf_hash_lock); 9741da177e4SLinus Torvalds 9751da177e4SLinus Torvalds /* Ignore adding duplicate addresses on an interface */ 976c346dca1SYOSHIFUJI Hideaki if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) { 977ba3542e1Sdingtianhong ADBG("ipv6_add_addr: already assigned\n"); 9781da177e4SLinus Torvalds err = -EEXIST; 9791da177e4SLinus Torvalds goto out; 9801da177e4SLinus Torvalds } 9811da177e4SLinus Torvalds 982322f74a4SIngo Oeser ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC); 9831da177e4SLinus Torvalds 98463159f29SIan Morris if (!ifa) { 985ba3542e1Sdingtianhong ADBG("ipv6_add_addr: malloc failed\n"); 9861da177e4SLinus Torvalds err = -ENOBUFS; 9871da177e4SLinus Torvalds goto out; 9881da177e4SLinus Torvalds } 9891da177e4SLinus Torvalds 9908f031519SDavid S. Miller rt = addrconf_dst_alloc(idev, addr, false); 9911da177e4SLinus Torvalds if (IS_ERR(rt)) { 9921da177e4SLinus Torvalds err = PTR_ERR(rt); 9931da177e4SLinus Torvalds goto out; 9941da177e4SLinus Torvalds } 9951da177e4SLinus Torvalds 996*df789fe7SDavid Forster if (net->ipv6.devconf_all->disable_policy || 997*df789fe7SDavid Forster idev->cnf.disable_policy) 998*df789fe7SDavid Forster rt->dst.flags |= DST_NOPOLICY; 999*df789fe7SDavid Forster 1000bba24896SJiri Pirko neigh_parms_data_state_setall(idev->nd_parms); 1001bba24896SJiri Pirko 10024e3fd7a0SAlexey Dobriyan ifa->addr = *addr; 10033f8f5298SJiri Pirko if (peer_addr) 10043f8f5298SJiri Pirko ifa->peer_addr = *peer_addr; 10051da177e4SLinus Torvalds 10061da177e4SLinus Torvalds spin_lock_init(&ifa->lock); 1007c15b1ccaSHannes Frederic Sowa INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work); 1008c2e21293Sstephen hemminger INIT_HLIST_NODE(&ifa->addr_lst); 10091da177e4SLinus Torvalds ifa->scope = scope; 10101da177e4SLinus Torvalds ifa->prefix_len = pfxlen; 10111da177e4SLinus Torvalds ifa->flags = flags | IFA_F_TENTATIVE; 10128a226b2cSJiri Benc ifa->valid_lft = valid_lft; 10138a226b2cSJiri Benc ifa->prefered_lft = prefered_lft; 10141da177e4SLinus Torvalds ifa->cstamp = ifa->tstamp = jiffies; 1015617fe29dSDaniel Borkmann ifa->tokenized = false; 10161da177e4SLinus Torvalds 101757f5f544SKeir Fraser ifa->rt = rt; 101857f5f544SKeir Fraser 10191da177e4SLinus Torvalds ifa->idev = idev; 10201da177e4SLinus Torvalds in6_dev_hold(idev); 10211da177e4SLinus Torvalds /* For caller */ 10221da177e4SLinus Torvalds in6_ifa_hold(ifa); 10231da177e4SLinus Torvalds 10241da177e4SLinus Torvalds /* Add to big hash table */ 1025ddbe5032SEric Dumazet hash = inet6_addr_hash(addr); 10261da177e4SLinus Torvalds 10275c578aedSstephen hemminger hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]); 10285c578aedSstephen hemminger spin_unlock(&addrconf_hash_lock); 10291da177e4SLinus Torvalds 10301da177e4SLinus Torvalds write_lock(&idev->lock); 10311da177e4SLinus Torvalds /* Add to inet6_dev unicast addr list. */ 1032e55ffac6SBrian Haley ipv6_link_dev_addr(idev, ifa); 10331da177e4SLinus Torvalds 10341da177e4SLinus Torvalds if (ifa->flags&IFA_F_TEMPORARY) { 1035372e6c8fSstephen hemminger list_add(&ifa->tmp_list, &idev->tempaddr_list); 10361da177e4SLinus Torvalds in6_ifa_hold(ifa); 10371da177e4SLinus Torvalds } 10381da177e4SLinus Torvalds 10391da177e4SLinus Torvalds in6_ifa_hold(ifa); 10401da177e4SLinus Torvalds write_unlock(&idev->lock); 10411da177e4SLinus Torvalds out2: 10428814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 10431da177e4SLinus Torvalds 1044fd92833aSYOSHIFUJI Hideaki if (likely(err == 0)) 1045f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_UP, ifa); 10461da177e4SLinus Torvalds else { 10471da177e4SLinus Torvalds kfree(ifa); 10481da177e4SLinus Torvalds ifa = ERR_PTR(err); 10491da177e4SLinus Torvalds } 10501da177e4SLinus Torvalds 10511da177e4SLinus Torvalds return ifa; 10521da177e4SLinus Torvalds out: 10535c578aedSstephen hemminger spin_unlock(&addrconf_hash_lock); 10541da177e4SLinus Torvalds goto out2; 10551da177e4SLinus Torvalds } 10561da177e4SLinus Torvalds 10575b84efecSThomas Haller enum cleanup_prefix_rt_t { 10585b84efecSThomas Haller CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */ 10595b84efecSThomas Haller CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */ 10605b84efecSThomas Haller CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */ 10615b84efecSThomas Haller }; 10625b84efecSThomas Haller 10635b84efecSThomas Haller /* 10645b84efecSThomas Haller * Check, whether the prefix for ifp would still need a prefix route 10655b84efecSThomas Haller * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_* 10665b84efecSThomas Haller * constants. 10675b84efecSThomas Haller * 10685b84efecSThomas Haller * 1) we don't purge prefix if address was not permanent. 10695b84efecSThomas Haller * prefix is managed by its own lifetime. 10705b84efecSThomas Haller * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE. 10715b84efecSThomas Haller * 3) if there are no addresses, delete prefix. 10725b84efecSThomas Haller * 4) if there are still other permanent address(es), 10735b84efecSThomas Haller * corresponding prefix is still permanent. 10745b84efecSThomas Haller * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE, 10755b84efecSThomas Haller * don't purge the prefix, assume user space is managing it. 10765b84efecSThomas Haller * 6) otherwise, update prefix lifetime to the 10775b84efecSThomas Haller * longest valid lifetime among the corresponding 10785b84efecSThomas Haller * addresses on the device. 10795b84efecSThomas Haller * Note: subsequent RA will update lifetime. 10805b84efecSThomas Haller **/ 10815b84efecSThomas Haller static enum cleanup_prefix_rt_t 10825b84efecSThomas Haller check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires) 10835b84efecSThomas Haller { 10845b84efecSThomas Haller struct inet6_ifaddr *ifa; 10855b84efecSThomas Haller struct inet6_dev *idev = ifp->idev; 10865b84efecSThomas Haller unsigned long lifetime; 10875b84efecSThomas Haller enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL; 10885b84efecSThomas Haller 10895b84efecSThomas Haller *expires = jiffies; 10905b84efecSThomas Haller 10915b84efecSThomas Haller list_for_each_entry(ifa, &idev->addr_list, if_list) { 10925b84efecSThomas Haller if (ifa == ifp) 10935b84efecSThomas Haller continue; 10945b84efecSThomas Haller if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr, 10955b84efecSThomas Haller ifp->prefix_len)) 10965b84efecSThomas Haller continue; 10975b84efecSThomas Haller if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE)) 10985b84efecSThomas Haller return CLEANUP_PREFIX_RT_NOP; 10995b84efecSThomas Haller 11005b84efecSThomas Haller action = CLEANUP_PREFIX_RT_EXPIRE; 11015b84efecSThomas Haller 11025b84efecSThomas Haller spin_lock(&ifa->lock); 11035b84efecSThomas Haller 11045b84efecSThomas Haller lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ); 11055b84efecSThomas Haller /* 11065b84efecSThomas Haller * Note: Because this address is 11075b84efecSThomas Haller * not permanent, lifetime < 11085b84efecSThomas Haller * LONG_MAX / HZ here. 11095b84efecSThomas Haller */ 11105b84efecSThomas Haller if (time_before(*expires, ifa->tstamp + lifetime * HZ)) 11115b84efecSThomas Haller *expires = ifa->tstamp + lifetime * HZ; 11125b84efecSThomas Haller spin_unlock(&ifa->lock); 11135b84efecSThomas Haller } 11145b84efecSThomas Haller 11155b84efecSThomas Haller return action; 11165b84efecSThomas Haller } 11175b84efecSThomas Haller 11185b84efecSThomas Haller static void 11195b84efecSThomas Haller cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt) 11205b84efecSThomas Haller { 11215b84efecSThomas Haller struct rt6_info *rt; 11225b84efecSThomas Haller 11235b84efecSThomas Haller rt = addrconf_get_prefix_route(&ifp->addr, 11245b84efecSThomas Haller ifp->prefix_len, 11255b84efecSThomas Haller ifp->idev->dev, 11265b84efecSThomas Haller 0, RTF_GATEWAY | RTF_DEFAULT); 11275b84efecSThomas Haller if (rt) { 11285b84efecSThomas Haller if (del_rt) 11295b84efecSThomas Haller ip6_del_rt(rt); 11305b84efecSThomas Haller else { 11315b84efecSThomas Haller if (!(rt->rt6i_flags & RTF_EXPIRES)) 11325b84efecSThomas Haller rt6_set_expires(rt, expires); 11335b84efecSThomas Haller ip6_rt_put(rt); 11345b84efecSThomas Haller } 11355b84efecSThomas Haller } 11365b84efecSThomas Haller } 11375b84efecSThomas Haller 11385b84efecSThomas Haller 11391da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */ 11401da177e4SLinus Torvalds 11411da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp) 11421da177e4SLinus Torvalds { 11434c5ff6a6SHerbert Xu int state; 11445b84efecSThomas Haller enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP; 11455b84efecSThomas Haller unsigned long expires; 11461da177e4SLinus Torvalds 1147c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 1148c15b1ccaSHannes Frederic Sowa 11498e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 11504c5ff6a6SHerbert Xu state = ifp->state; 1151e9d3e084SHerbert Xu ifp->state = INET6_IFADDR_STATE_DEAD; 11528e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 11534c5ff6a6SHerbert Xu 11544c5ff6a6SHerbert Xu if (state == INET6_IFADDR_STATE_DEAD) 11554c5ff6a6SHerbert Xu goto out; 11561da177e4SLinus Torvalds 11575c578aedSstephen hemminger spin_lock_bh(&addrconf_hash_lock); 11585c578aedSstephen hemminger hlist_del_init_rcu(&ifp->addr_lst); 11595c578aedSstephen hemminger spin_unlock_bh(&addrconf_hash_lock); 11601da177e4SLinus Torvalds 11615b84efecSThomas Haller write_lock_bh(&ifp->idev->lock); 11625d9efa7eSDavid S. Miller 11631da177e4SLinus Torvalds if (ifp->flags&IFA_F_TEMPORARY) { 1164372e6c8fSstephen hemminger list_del(&ifp->tmp_list); 11651da177e4SLinus Torvalds if (ifp->ifpub) { 11661da177e4SLinus Torvalds in6_ifa_put(ifp->ifpub); 11671da177e4SLinus Torvalds ifp->ifpub = NULL; 11681da177e4SLinus Torvalds } 11691da177e4SLinus Torvalds __in6_ifa_put(ifp); 11701da177e4SLinus Torvalds } 11711da177e4SLinus Torvalds 11725b84efecSThomas Haller if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE)) 11735b84efecSThomas Haller action = check_cleanup_prefix_route(ifp, &expires); 11745b84efecSThomas Haller 1175502a2ffdSstephen hemminger list_del_init(&ifp->if_list); 11761da177e4SLinus Torvalds __in6_ifa_put(ifp); 1177502a2ffdSstephen hemminger 11785b84efecSThomas Haller write_unlock_bh(&ifp->idev->lock); 11791da177e4SLinus Torvalds 1180c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifp); 1181b2238566SAndrey Vagin 11821da177e4SLinus Torvalds ipv6_ifa_notify(RTM_DELADDR, ifp); 11831da177e4SLinus Torvalds 1184f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_DOWN, ifp); 11851da177e4SLinus Torvalds 11865b84efecSThomas Haller if (action != CLEANUP_PREFIX_RT_NOP) { 11875b84efecSThomas Haller cleanup_prefix_route(ifp, expires, 11885b84efecSThomas Haller action == CLEANUP_PREFIX_RT_DEL); 11891da177e4SLinus Torvalds } 11901da177e4SLinus Torvalds 1191c3968a85SDaniel Walter /* clean up prefsrc entries */ 1192c3968a85SDaniel Walter rt6_remove_prefsrc(ifp); 11934c5ff6a6SHerbert Xu out: 11941da177e4SLinus Torvalds in6_ifa_put(ifp); 11951da177e4SLinus Torvalds } 11961da177e4SLinus Torvalds 11971da177e4SLinus Torvalds static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift) 11981da177e4SLinus Torvalds { 11991da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 12001da177e4SLinus Torvalds struct in6_addr addr, *tmpaddr; 120176f793e3SLorenzo Colitti unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age; 1202eac55bf9SBenoit Boissinot unsigned long regen_advance; 12031da177e4SLinus Torvalds int tmp_plen; 12041da177e4SLinus Torvalds int ret = 0; 120595c385b4SNeil Horman u32 addr_flags; 120676f793e3SLorenzo Colitti unsigned long now = jiffies; 120776506a98SJiri Bohac long max_desync_factor; 12087aa8e63fSJiri Bohac s32 cnf_temp_preferred_lft; 12091da177e4SLinus Torvalds 121053bd6749SJiri Pirko write_lock_bh(&idev->lock); 12111da177e4SLinus Torvalds if (ift) { 12121da177e4SLinus Torvalds spin_lock_bh(&ift->lock); 12131da177e4SLinus Torvalds memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8); 12141da177e4SLinus Torvalds spin_unlock_bh(&ift->lock); 12151da177e4SLinus Torvalds tmpaddr = &addr; 12161da177e4SLinus Torvalds } else { 12171da177e4SLinus Torvalds tmpaddr = NULL; 12181da177e4SLinus Torvalds } 12191da177e4SLinus Torvalds retry: 12201da177e4SLinus Torvalds in6_dev_hold(idev); 12211da177e4SLinus Torvalds if (idev->cnf.use_tempaddr <= 0) { 122253bd6749SJiri Pirko write_unlock_bh(&idev->lock); 1223f3213831SJoe Perches pr_info("%s: use_tempaddr is disabled\n", __func__); 12241da177e4SLinus Torvalds in6_dev_put(idev); 12251da177e4SLinus Torvalds ret = -1; 12261da177e4SLinus Torvalds goto out; 12271da177e4SLinus Torvalds } 12281da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 12291da177e4SLinus Torvalds if (ifp->regen_count++ >= idev->cnf.regen_max_retry) { 12301da177e4SLinus Torvalds idev->cnf.use_tempaddr = -1; /*XXX*/ 12311da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 123253bd6749SJiri Pirko write_unlock_bh(&idev->lock); 1233f3213831SJoe Perches pr_warn("%s: regeneration time exceeded - disabled temporary address support\n", 1234f3213831SJoe Perches __func__); 12351da177e4SLinus Torvalds in6_dev_put(idev); 12361da177e4SLinus Torvalds ret = -1; 12371da177e4SLinus Torvalds goto out; 12381da177e4SLinus Torvalds } 12391da177e4SLinus Torvalds in6_ifa_hold(ifp); 12401da177e4SLinus Torvalds memcpy(addr.s6_addr, ifp->addr.s6_addr, 8); 12419d6280daSJiri Bohac ipv6_try_regen_rndid(idev, tmpaddr); 12421da177e4SLinus Torvalds memcpy(&addr.s6_addr[8], idev->rndid, 8); 124376f793e3SLorenzo Colitti age = (now - ifp->tstamp) / HZ; 12441da177e4SLinus Torvalds 1245eac55bf9SBenoit Boissinot regen_advance = idev->cnf.regen_max_retry * 1246eac55bf9SBenoit Boissinot idev->cnf.dad_transmits * 12471f9248e5SJiri Pirko NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ; 124876506a98SJiri Bohac 124976506a98SJiri Bohac /* recalculate max_desync_factor each time and update 125076506a98SJiri Bohac * idev->desync_factor if it's larger 125176506a98SJiri Bohac */ 12527aa8e63fSJiri Bohac cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft); 125376506a98SJiri Bohac max_desync_factor = min_t(__u32, 125476506a98SJiri Bohac idev->cnf.max_desync_factor, 12557aa8e63fSJiri Bohac cnf_temp_preferred_lft - regen_advance); 125676506a98SJiri Bohac 125776506a98SJiri Bohac if (unlikely(idev->desync_factor > max_desync_factor)) { 125876506a98SJiri Bohac if (max_desync_factor > 0) { 125976506a98SJiri Bohac get_random_bytes(&idev->desync_factor, 126076506a98SJiri Bohac sizeof(idev->desync_factor)); 126176506a98SJiri Bohac idev->desync_factor %= max_desync_factor; 126276506a98SJiri Bohac } else { 126376506a98SJiri Bohac idev->desync_factor = 0; 126476506a98SJiri Bohac } 126576506a98SJiri Bohac } 126676506a98SJiri Bohac 126776506a98SJiri Bohac tmp_valid_lft = min_t(__u32, 126876506a98SJiri Bohac ifp->valid_lft, 126976506a98SJiri Bohac idev->cnf.temp_valid_lft + age); 12707aa8e63fSJiri Bohac tmp_prefered_lft = cnf_temp_preferred_lft + age - 127176506a98SJiri Bohac idev->desync_factor; 127276506a98SJiri Bohac tmp_prefered_lft = min_t(__u32, ifp->prefered_lft, tmp_prefered_lft); 127376506a98SJiri Bohac tmp_plen = ifp->prefix_len; 127476506a98SJiri Bohac tmp_tstamp = ifp->tstamp; 127576506a98SJiri Bohac spin_unlock_bh(&ifp->lock); 127676506a98SJiri Bohac 127753bd6749SJiri Pirko write_unlock_bh(&idev->lock); 127895c385b4SNeil Horman 1279eac55bf9SBenoit Boissinot /* A temporary address is created only if this calculated Preferred 1280eac55bf9SBenoit Boissinot * Lifetime is greater than REGEN_ADVANCE time units. In particular, 1281eac55bf9SBenoit Boissinot * an implementation must not create a temporary address with a zero 1282eac55bf9SBenoit Boissinot * Preferred Lifetime. 1283ecab6701SHeiner Kallweit * Use age calculation as in addrconf_verify to avoid unnecessary 1284ecab6701SHeiner Kallweit * temporary addresses being generated. 1285eac55bf9SBenoit Boissinot */ 1286ecab6701SHeiner Kallweit age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; 1287ecab6701SHeiner Kallweit if (tmp_prefered_lft <= regen_advance + age) { 1288eac55bf9SBenoit Boissinot in6_ifa_put(ifp); 1289eac55bf9SBenoit Boissinot in6_dev_put(idev); 1290eac55bf9SBenoit Boissinot ret = -1; 1291eac55bf9SBenoit Boissinot goto out; 1292eac55bf9SBenoit Boissinot } 1293eac55bf9SBenoit Boissinot 129495c385b4SNeil Horman addr_flags = IFA_F_TEMPORARY; 129595c385b4SNeil Horman /* set in addrconf_prefix_rcv() */ 129695c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 129795c385b4SNeil Horman addr_flags |= IFA_F_OPTIMISTIC; 129895c385b4SNeil Horman 12994b08a8f1SHannes Frederic Sowa ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen, 13008a226b2cSJiri Benc ipv6_addr_scope(&addr), addr_flags, 13014b08a8f1SHannes Frederic Sowa tmp_valid_lft, tmp_prefered_lft); 13024b08a8f1SHannes Frederic Sowa if (IS_ERR(ift)) { 13031da177e4SLinus Torvalds in6_ifa_put(ifp); 13041da177e4SLinus Torvalds in6_dev_put(idev); 1305f3213831SJoe Perches pr_info("%s: retry temporary address regeneration\n", __func__); 13061da177e4SLinus Torvalds tmpaddr = &addr; 130753bd6749SJiri Pirko write_lock_bh(&idev->lock); 13081da177e4SLinus Torvalds goto retry; 13091da177e4SLinus Torvalds } 13101da177e4SLinus Torvalds 13111da177e4SLinus Torvalds spin_lock_bh(&ift->lock); 13121da177e4SLinus Torvalds ift->ifpub = ifp; 131376f793e3SLorenzo Colitti ift->cstamp = now; 13141da177e4SLinus Torvalds ift->tstamp = tmp_tstamp; 13151da177e4SLinus Torvalds spin_unlock_bh(&ift->lock); 13161da177e4SLinus Torvalds 1317cf22f9a2SDavid S. Miller addrconf_dad_start(ift); 13181da177e4SLinus Torvalds in6_ifa_put(ift); 13191da177e4SLinus Torvalds in6_dev_put(idev); 13201da177e4SLinus Torvalds out: 13211da177e4SLinus Torvalds return ret; 13221da177e4SLinus Torvalds } 13231da177e4SLinus Torvalds 13241da177e4SLinus Torvalds /* 1325072047e4SYOSHIFUJI Hideaki * Choose an appropriate source address (RFC3484) 13261da177e4SLinus Torvalds */ 1327a9b05723SYOSHIFUJI Hideaki enum { 1328a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_INIT = 0, 1329a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_LOCAL, 1330a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_SCOPE, 1331a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PREFERRED, 1332a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6 1333a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_HOA, 1334a9b05723SYOSHIFUJI Hideaki #endif 1335a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_OIF, 1336a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_LABEL, 1337a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PRIVACY, 1338a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_ORCHID, 1339a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PREFIX, 13407fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 13417fd2561eSErik Kline IPV6_SADDR_RULE_NOT_OPTIMISTIC, 13427fd2561eSErik Kline #endif 1343a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_MAX 1344072047e4SYOSHIFUJI Hideaki }; 1345072047e4SYOSHIFUJI Hideaki 1346a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score { 1347a9b05723SYOSHIFUJI Hideaki int rule; 1348a9b05723SYOSHIFUJI Hideaki int addr_type; 1349a9b05723SYOSHIFUJI Hideaki struct inet6_ifaddr *ifa; 1350a9b05723SYOSHIFUJI Hideaki DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX); 1351a9b05723SYOSHIFUJI Hideaki int scopedist; 1352a9b05723SYOSHIFUJI Hideaki int matchlen; 1353a9b05723SYOSHIFUJI Hideaki }; 1354a9b05723SYOSHIFUJI Hideaki 1355a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst { 13569acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *addr; 1357a9b05723SYOSHIFUJI Hideaki int ifindex; 1358a9b05723SYOSHIFUJI Hideaki int scope; 1359a9b05723SYOSHIFUJI Hideaki int label; 13607cbca67cSYOSHIFUJI Hideaki unsigned int prefs; 1361a9b05723SYOSHIFUJI Hideaki }; 1362072047e4SYOSHIFUJI Hideaki 1363b6f99a21SDave Jones static inline int ipv6_saddr_preferred(int type) 13641da177e4SLinus Torvalds { 136545bb0060SUlrich Weber if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK)) 1366072047e4SYOSHIFUJI Hideaki return 1; 1367072047e4SYOSHIFUJI Hideaki return 0; 13681da177e4SLinus Torvalds } 13691da177e4SLinus Torvalds 13707fd2561eSErik Kline static inline bool ipv6_use_optimistic_addr(struct inet6_dev *idev) 13717fd2561eSErik Kline { 13727fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 13737fd2561eSErik Kline return idev && idev->cnf.optimistic_dad && idev->cnf.use_optimistic; 13747fd2561eSErik Kline #else 13757fd2561eSErik Kline return false; 13767fd2561eSErik Kline #endif 13777fd2561eSErik Kline } 13787fd2561eSErik Kline 13793de23255SBenjamin Thery static int ipv6_get_saddr_eval(struct net *net, 13803de23255SBenjamin Thery struct ipv6_saddr_score *score, 1381a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst *dst, 1382a9b05723SYOSHIFUJI Hideaki int i) 1383a9b05723SYOSHIFUJI Hideaki { 1384a9b05723SYOSHIFUJI Hideaki int ret; 1385a9b05723SYOSHIFUJI Hideaki 1386a9b05723SYOSHIFUJI Hideaki if (i <= score->rule) { 1387a9b05723SYOSHIFUJI Hideaki switch (i) { 1388a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_SCOPE: 1389a9b05723SYOSHIFUJI Hideaki ret = score->scopedist; 1390a9b05723SYOSHIFUJI Hideaki break; 1391a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFIX: 1392a9b05723SYOSHIFUJI Hideaki ret = score->matchlen; 1393a9b05723SYOSHIFUJI Hideaki break; 1394a9b05723SYOSHIFUJI Hideaki default: 1395a9b05723SYOSHIFUJI Hideaki ret = !!test_bit(i, score->scorebits); 1396a9b05723SYOSHIFUJI Hideaki } 1397a9b05723SYOSHIFUJI Hideaki goto out; 1398a9b05723SYOSHIFUJI Hideaki } 1399a9b05723SYOSHIFUJI Hideaki 1400a9b05723SYOSHIFUJI Hideaki switch (i) { 1401a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_INIT: 1402a9b05723SYOSHIFUJI Hideaki /* Rule 0: remember if hiscore is not ready yet */ 1403a9b05723SYOSHIFUJI Hideaki ret = !!score->ifa; 1404a9b05723SYOSHIFUJI Hideaki break; 1405a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_LOCAL: 1406a9b05723SYOSHIFUJI Hideaki /* Rule 1: Prefer same address */ 1407a9b05723SYOSHIFUJI Hideaki ret = ipv6_addr_equal(&score->ifa->addr, dst->addr); 1408a9b05723SYOSHIFUJI Hideaki break; 1409a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_SCOPE: 1410a9b05723SYOSHIFUJI Hideaki /* Rule 2: Prefer appropriate scope 1411a9b05723SYOSHIFUJI Hideaki * 1412a9b05723SYOSHIFUJI Hideaki * ret 1413a9b05723SYOSHIFUJI Hideaki * ^ 1414a9b05723SYOSHIFUJI Hideaki * -1 | d 15 1415a9b05723SYOSHIFUJI Hideaki * ---+--+-+---> scope 1416a9b05723SYOSHIFUJI Hideaki * | 1417a9b05723SYOSHIFUJI Hideaki * | d is scope of the destination. 1418a9b05723SYOSHIFUJI Hideaki * B-d | \ 1419a9b05723SYOSHIFUJI Hideaki * | \ <- smaller scope is better if 1420db9c7c39Sstephen hemminger * B-15 | \ if scope is enough for destination. 1421a9b05723SYOSHIFUJI Hideaki * | ret = B - scope (-1 <= scope >= d <= 15). 1422a9b05723SYOSHIFUJI Hideaki * d-C-1 | / 1423a9b05723SYOSHIFUJI Hideaki * |/ <- greater is better 1424a9b05723SYOSHIFUJI Hideaki * -C / if scope is not enough for destination. 1425a9b05723SYOSHIFUJI Hideaki * /| ret = scope - C (-1 <= d < scope <= 15). 1426a9b05723SYOSHIFUJI Hideaki * 1427a9b05723SYOSHIFUJI Hideaki * d - C - 1 < B -15 (for all -1 <= d <= 15). 1428a9b05723SYOSHIFUJI Hideaki * C > d + 14 - B >= 15 + 14 - B = 29 - B. 1429a9b05723SYOSHIFUJI Hideaki * Assume B = 0 and we get C > 29. 1430a9b05723SYOSHIFUJI Hideaki */ 1431a9b05723SYOSHIFUJI Hideaki ret = __ipv6_addr_src_scope(score->addr_type); 1432a9b05723SYOSHIFUJI Hideaki if (ret >= dst->scope) 1433a9b05723SYOSHIFUJI Hideaki ret = -ret; 1434a9b05723SYOSHIFUJI Hideaki else 1435a9b05723SYOSHIFUJI Hideaki ret -= 128; /* 30 is enough */ 1436a9b05723SYOSHIFUJI Hideaki score->scopedist = ret; 1437a9b05723SYOSHIFUJI Hideaki break; 1438a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFERRED: 14397fd2561eSErik Kline { 1440a9b05723SYOSHIFUJI Hideaki /* Rule 3: Avoid deprecated and optimistic addresses */ 14417fd2561eSErik Kline u8 avoid = IFA_F_DEPRECATED; 14427fd2561eSErik Kline 14437fd2561eSErik Kline if (!ipv6_use_optimistic_addr(score->ifa->idev)) 14447fd2561eSErik Kline avoid |= IFA_F_OPTIMISTIC; 1445a9b05723SYOSHIFUJI Hideaki ret = ipv6_saddr_preferred(score->addr_type) || 14467fd2561eSErik Kline !(score->ifa->flags & avoid); 1447a9b05723SYOSHIFUJI Hideaki break; 14487fd2561eSErik Kline } 1449a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6 1450a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_HOA: 14517cbca67cSYOSHIFUJI Hideaki { 1452a9b05723SYOSHIFUJI Hideaki /* Rule 4: Prefer home address */ 14537cbca67cSYOSHIFUJI Hideaki int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA); 14547cbca67cSYOSHIFUJI Hideaki ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome; 1455a9b05723SYOSHIFUJI Hideaki break; 14567cbca67cSYOSHIFUJI Hideaki } 1457a9b05723SYOSHIFUJI Hideaki #endif 1458a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_OIF: 1459a9b05723SYOSHIFUJI Hideaki /* Rule 5: Prefer outgoing interface */ 1460a9b05723SYOSHIFUJI Hideaki ret = (!dst->ifindex || 1461a9b05723SYOSHIFUJI Hideaki dst->ifindex == score->ifa->idev->dev->ifindex); 1462a9b05723SYOSHIFUJI Hideaki break; 1463a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_LABEL: 1464a9b05723SYOSHIFUJI Hideaki /* Rule 6: Prefer matching label */ 14653de23255SBenjamin Thery ret = ipv6_addr_label(net, 14663de23255SBenjamin Thery &score->ifa->addr, score->addr_type, 1467a9b05723SYOSHIFUJI Hideaki score->ifa->idev->dev->ifindex) == dst->label; 1468a9b05723SYOSHIFUJI Hideaki break; 1469a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PRIVACY: 14707cbca67cSYOSHIFUJI Hideaki { 1471a9b05723SYOSHIFUJI Hideaki /* Rule 7: Prefer public address 147225985edcSLucas De Marchi * Note: prefer temporary address if use_tempaddr >= 2 1473a9b05723SYOSHIFUJI Hideaki */ 14747cbca67cSYOSHIFUJI Hideaki int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ? 14757cbca67cSYOSHIFUJI Hideaki !!(dst->prefs & IPV6_PREFER_SRC_TMP) : 14767cbca67cSYOSHIFUJI Hideaki score->ifa->idev->cnf.use_tempaddr >= 2; 14777cbca67cSYOSHIFUJI Hideaki ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp; 1478a9b05723SYOSHIFUJI Hideaki break; 14797cbca67cSYOSHIFUJI Hideaki } 1480a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_ORCHID: 1481a9b05723SYOSHIFUJI Hideaki /* Rule 8-: Prefer ORCHID vs ORCHID or 1482a9b05723SYOSHIFUJI Hideaki * non-ORCHID vs non-ORCHID 1483a9b05723SYOSHIFUJI Hideaki */ 1484a9b05723SYOSHIFUJI Hideaki ret = !(ipv6_addr_orchid(&score->ifa->addr) ^ 1485a9b05723SYOSHIFUJI Hideaki ipv6_addr_orchid(dst->addr)); 1486a9b05723SYOSHIFUJI Hideaki break; 1487a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFIX: 1488a9b05723SYOSHIFUJI Hideaki /* Rule 8: Use longest matching prefix */ 148991b4b04fSYOSHIFUJI Hideaki / 吉藤英明 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr); 149091b4b04fSYOSHIFUJI Hideaki / 吉藤英明 if (ret > score->ifa->prefix_len) 149191b4b04fSYOSHIFUJI Hideaki / 吉藤英明 ret = score->ifa->prefix_len; 149291b4b04fSYOSHIFUJI Hideaki / 吉藤英明 score->matchlen = ret; 1493a9b05723SYOSHIFUJI Hideaki break; 14947fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 14957fd2561eSErik Kline case IPV6_SADDR_RULE_NOT_OPTIMISTIC: 14967fd2561eSErik Kline /* Optimistic addresses still have lower precedence than other 14977fd2561eSErik Kline * preferred addresses. 14987fd2561eSErik Kline */ 14997fd2561eSErik Kline ret = !(score->ifa->flags & IFA_F_OPTIMISTIC); 15007fd2561eSErik Kline break; 15017fd2561eSErik Kline #endif 1502a9b05723SYOSHIFUJI Hideaki default: 1503a9b05723SYOSHIFUJI Hideaki ret = 0; 1504a9b05723SYOSHIFUJI Hideaki } 1505a9b05723SYOSHIFUJI Hideaki 1506a9b05723SYOSHIFUJI Hideaki if (ret) 1507a9b05723SYOSHIFUJI Hideaki __set_bit(i, score->scorebits); 1508a9b05723SYOSHIFUJI Hideaki score->rule = i; 1509a9b05723SYOSHIFUJI Hideaki out: 1510a9b05723SYOSHIFUJI Hideaki return ret; 1511a9b05723SYOSHIFUJI Hideaki } 1512a9b05723SYOSHIFUJI Hideaki 1513c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 static int __ipv6_dev_get_saddr(struct net *net, 15149131f3deSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_dst *dst, 15159131f3deSYOSHIFUJI Hideaki/吉藤英明 struct inet6_dev *idev, 1516c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_score *scores, 1517c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 int hiscore_idx) 15181da177e4SLinus Torvalds { 1519c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx]; 1520072047e4SYOSHIFUJI Hideaki 15211da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1522502a2ffdSstephen hemminger list_for_each_entry(score->ifa, &idev->addr_list, if_list) { 1523a9b05723SYOSHIFUJI Hideaki int i; 1524072047e4SYOSHIFUJI Hideaki 1525a9b05723SYOSHIFUJI Hideaki /* 15266b3ae80aSYOSHIFUJI Hideaki * - Tentative Address (RFC2462 section 5.4) 15276b3ae80aSYOSHIFUJI Hideaki * - A tentative address is not considered 15286b3ae80aSYOSHIFUJI Hideaki * "assigned to an interface" in the traditional 152995c385b4SNeil Horman * sense, unless it is also flagged as optimistic. 15306b3ae80aSYOSHIFUJI Hideaki * - Candidate Source Address (section 4) 1531072047e4SYOSHIFUJI Hideaki * - In any case, anycast addresses, multicast 1532072047e4SYOSHIFUJI Hideaki * addresses, and the unspecified address MUST 1533072047e4SYOSHIFUJI Hideaki * NOT be included in a candidate set. 1534072047e4SYOSHIFUJI Hideaki */ 1535a9b05723SYOSHIFUJI Hideaki if ((score->ifa->flags & IFA_F_TENTATIVE) && 1536a9b05723SYOSHIFUJI Hideaki (!(score->ifa->flags & IFA_F_OPTIMISTIC))) 15376b3ae80aSYOSHIFUJI Hideaki continue; 1538a9b05723SYOSHIFUJI Hideaki 1539a9b05723SYOSHIFUJI Hideaki score->addr_type = __ipv6_addr_type(&score->ifa->addr); 1540a9b05723SYOSHIFUJI Hideaki 1541a9b05723SYOSHIFUJI Hideaki if (unlikely(score->addr_type == IPV6_ADDR_ANY || 1542a9b05723SYOSHIFUJI Hideaki score->addr_type & IPV6_ADDR_MULTICAST)) { 1543ba7a46f1SJoe Perches net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s", 15449131f3deSYOSHIFUJI Hideaki/吉藤英明 idev->dev->name); 15451da177e4SLinus Torvalds continue; 1546072047e4SYOSHIFUJI Hideaki } 1547072047e4SYOSHIFUJI Hideaki 1548a9b05723SYOSHIFUJI Hideaki score->rule = -1; 1549a9b05723SYOSHIFUJI Hideaki bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX); 1550072047e4SYOSHIFUJI Hideaki 1551a9b05723SYOSHIFUJI Hideaki for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) { 1552a9b05723SYOSHIFUJI Hideaki int minihiscore, miniscore; 1553072047e4SYOSHIFUJI Hideaki 15549131f3deSYOSHIFUJI Hideaki/吉藤英明 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i); 15559131f3deSYOSHIFUJI Hideaki/吉藤英明 miniscore = ipv6_get_saddr_eval(net, score, dst, i); 1556072047e4SYOSHIFUJI Hideaki 1557a9b05723SYOSHIFUJI Hideaki if (minihiscore > miniscore) { 1558a9b05723SYOSHIFUJI Hideaki if (i == IPV6_SADDR_RULE_SCOPE && 1559a9b05723SYOSHIFUJI Hideaki score->scopedist > 0) { 1560a9b05723SYOSHIFUJI Hideaki /* 1561a9b05723SYOSHIFUJI Hideaki * special case: 1562a9b05723SYOSHIFUJI Hideaki * each remaining entry 1563a9b05723SYOSHIFUJI Hideaki * has too small (not enough) 1564a9b05723SYOSHIFUJI Hideaki * scope, because ifa entries 1565a9b05723SYOSHIFUJI Hideaki * are sorted by their scope 1566a9b05723SYOSHIFUJI Hideaki * values. 1567072047e4SYOSHIFUJI Hideaki */ 15689131f3deSYOSHIFUJI Hideaki/吉藤英明 goto out; 1569072047e4SYOSHIFUJI Hideaki } 1570a9b05723SYOSHIFUJI Hideaki break; 1571a9b05723SYOSHIFUJI Hideaki } else if (minihiscore < miniscore) { 1572a9b05723SYOSHIFUJI Hideaki if (hiscore->ifa) 1573a9b05723SYOSHIFUJI Hideaki in6_ifa_put(hiscore->ifa); 157499cd07a5SJuha-Matti Tapio 1575a9b05723SYOSHIFUJI Hideaki in6_ifa_hold(score->ifa); 1576072047e4SYOSHIFUJI Hideaki 1577a0bffffcSIlpo Järvinen swap(hiscore, score); 1578c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 hiscore_idx = 1 - hiscore_idx; 1579a9b05723SYOSHIFUJI Hideaki 1580a9b05723SYOSHIFUJI Hideaki /* restore our iterator */ 1581a9b05723SYOSHIFUJI Hideaki score->ifa = hiscore->ifa; 1582a9b05723SYOSHIFUJI Hideaki 1583a9b05723SYOSHIFUJI Hideaki break; 15841da177e4SLinus Torvalds } 1585a9b05723SYOSHIFUJI Hideaki } 1586a9b05723SYOSHIFUJI Hideaki } 15879131f3deSYOSHIFUJI Hideaki/吉藤英明 out: 15881da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 1589c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 return hiscore_idx; 15901da177e4SLinus Torvalds } 15919131f3deSYOSHIFUJI Hideaki/吉藤英明 1592afbac601SDavid Ahern static int ipv6_get_saddr_master(struct net *net, 1593afbac601SDavid Ahern const struct net_device *dst_dev, 1594afbac601SDavid Ahern const struct net_device *master, 1595afbac601SDavid Ahern struct ipv6_saddr_dst *dst, 1596afbac601SDavid Ahern struct ipv6_saddr_score *scores, 1597afbac601SDavid Ahern int hiscore_idx) 1598afbac601SDavid Ahern { 1599afbac601SDavid Ahern struct inet6_dev *idev; 1600afbac601SDavid Ahern 1601afbac601SDavid Ahern idev = __in6_dev_get(dst_dev); 1602afbac601SDavid Ahern if (idev) 1603afbac601SDavid Ahern hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev, 1604afbac601SDavid Ahern scores, hiscore_idx); 1605afbac601SDavid Ahern 1606afbac601SDavid Ahern idev = __in6_dev_get(master); 1607afbac601SDavid Ahern if (idev) 1608afbac601SDavid Ahern hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev, 1609afbac601SDavid Ahern scores, hiscore_idx); 1610afbac601SDavid Ahern 1611afbac601SDavid Ahern return hiscore_idx; 1612afbac601SDavid Ahern } 1613afbac601SDavid Ahern 16149131f3deSYOSHIFUJI Hideaki/吉藤英明 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev, 16159131f3deSYOSHIFUJI Hideaki/吉藤英明 const struct in6_addr *daddr, unsigned int prefs, 16169131f3deSYOSHIFUJI Hideaki/吉藤英明 struct in6_addr *saddr) 16179131f3deSYOSHIFUJI Hideaki/吉藤英明 { 1618c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_score scores[2], *hiscore; 16199131f3deSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_dst dst; 16209131f3deSYOSHIFUJI Hideaki/吉藤英明 struct inet6_dev *idev; 16219131f3deSYOSHIFUJI Hideaki/吉藤英明 struct net_device *dev; 16229131f3deSYOSHIFUJI Hideaki/吉藤英明 int dst_type; 16239131f3deSYOSHIFUJI Hideaki/吉藤英明 bool use_oif_addr = false; 1624c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 int hiscore_idx = 0; 16259131f3deSYOSHIFUJI Hideaki/吉藤英明 16269131f3deSYOSHIFUJI Hideaki/吉藤英明 dst_type = __ipv6_addr_type(daddr); 16279131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.addr = daddr; 16289131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.ifindex = dst_dev ? dst_dev->ifindex : 0; 16299131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.scope = __ipv6_addr_src_scope(dst_type); 16309131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex); 16319131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.prefs = prefs; 16329131f3deSYOSHIFUJI Hideaki/吉藤英明 1633c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 scores[hiscore_idx].rule = -1; 1634c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 scores[hiscore_idx].ifa = NULL; 16359131f3deSYOSHIFUJI Hideaki/吉藤英明 16369131f3deSYOSHIFUJI Hideaki/吉藤英明 rcu_read_lock(); 16379131f3deSYOSHIFUJI Hideaki/吉藤英明 16389131f3deSYOSHIFUJI Hideaki/吉藤英明 /* Candidate Source Address (section 4) 16399131f3deSYOSHIFUJI Hideaki/吉藤英明 * - multicast and link-local destination address, 16409131f3deSYOSHIFUJI Hideaki/吉藤英明 * the set of candidate source address MUST only 16419131f3deSYOSHIFUJI Hideaki/吉藤英明 * include addresses assigned to interfaces 16429131f3deSYOSHIFUJI Hideaki/吉藤英明 * belonging to the same link as the outgoing 16439131f3deSYOSHIFUJI Hideaki/吉藤英明 * interface. 16449131f3deSYOSHIFUJI Hideaki/吉藤英明 * (- For site-local destination addresses, the 16459131f3deSYOSHIFUJI Hideaki/吉藤英明 * set of candidate source addresses MUST only 16469131f3deSYOSHIFUJI Hideaki/吉藤英明 * include addresses assigned to interfaces 16479131f3deSYOSHIFUJI Hideaki/吉藤英明 * belonging to the same site as the outgoing 16489131f3deSYOSHIFUJI Hideaki/吉藤英明 * interface.) 16493985e8a3SErik Kline * - "It is RECOMMENDED that the candidate source addresses 16503985e8a3SErik Kline * be the set of unicast addresses assigned to the 16513985e8a3SErik Kline * interface that will be used to send to the destination 16523985e8a3SErik Kline * (the 'outgoing' interface)." (RFC 6724) 16539131f3deSYOSHIFUJI Hideaki/吉藤英明 */ 16549131f3deSYOSHIFUJI Hideaki/吉藤英明 if (dst_dev) { 16559131f3deSYOSHIFUJI Hideaki/吉藤英明 idev = __in6_dev_get(dst_dev); 16563985e8a3SErik Kline if ((dst_type & IPV6_ADDR_MULTICAST) || 16573985e8a3SErik Kline dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL || 16583985e8a3SErik Kline (idev && idev->cnf.use_oif_addrs_only)) { 16599131f3deSYOSHIFUJI Hideaki/吉藤英明 use_oif_addr = true; 16609131f3deSYOSHIFUJI Hideaki/吉藤英明 } 16619131f3deSYOSHIFUJI Hideaki/吉藤英明 } 16629131f3deSYOSHIFUJI Hideaki/吉藤英明 16639131f3deSYOSHIFUJI Hideaki/吉藤英明 if (use_oif_addr) { 1664c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 if (idev) 1665c15df306SYOSHIFUJI Hideaki hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx); 16669131f3deSYOSHIFUJI Hideaki/吉藤英明 } else { 1667afbac601SDavid Ahern const struct net_device *master; 1668afbac601SDavid Ahern int master_idx = 0; 1669afbac601SDavid Ahern 1670afbac601SDavid Ahern /* if dst_dev exists and is enslaved to an L3 device, then 1671afbac601SDavid Ahern * prefer addresses from dst_dev and then the master over 1672afbac601SDavid Ahern * any other enslaved devices in the L3 domain. 1673afbac601SDavid Ahern */ 1674afbac601SDavid Ahern master = l3mdev_master_dev_rcu(dst_dev); 1675afbac601SDavid Ahern if (master) { 1676afbac601SDavid Ahern master_idx = master->ifindex; 1677afbac601SDavid Ahern 1678afbac601SDavid Ahern hiscore_idx = ipv6_get_saddr_master(net, dst_dev, 1679afbac601SDavid Ahern master, &dst, 1680afbac601SDavid Ahern scores, hiscore_idx); 1681afbac601SDavid Ahern 1682afbac601SDavid Ahern if (scores[hiscore_idx].ifa) 1683afbac601SDavid Ahern goto out; 1684afbac601SDavid Ahern } 1685afbac601SDavid Ahern 16869131f3deSYOSHIFUJI Hideaki/吉藤英明 for_each_netdev_rcu(net, dev) { 1687afbac601SDavid Ahern /* only consider addresses on devices in the 1688afbac601SDavid Ahern * same L3 domain 1689afbac601SDavid Ahern */ 1690afbac601SDavid Ahern if (l3mdev_master_ifindex_rcu(dev) != master_idx) 1691afbac601SDavid Ahern continue; 16929131f3deSYOSHIFUJI Hideaki/吉藤英明 idev = __in6_dev_get(dev); 16939131f3deSYOSHIFUJI Hideaki/吉藤英明 if (!idev) 16949131f3deSYOSHIFUJI Hideaki/吉藤英明 continue; 1695c15df306SYOSHIFUJI Hideaki hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx); 16969131f3deSYOSHIFUJI Hideaki/吉藤英明 } 16979131f3deSYOSHIFUJI Hideaki/吉藤英明 } 1698afbac601SDavid Ahern 1699afbac601SDavid Ahern out: 17008814c4b5SYOSHIFUJI Hideaki rcu_read_unlock(); 17011da177e4SLinus Torvalds 1702c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 hiscore = &scores[hiscore_idx]; 1703a9b05723SYOSHIFUJI Hideaki if (!hiscore->ifa) 1704072047e4SYOSHIFUJI Hideaki return -EADDRNOTAVAIL; 17051da177e4SLinus Torvalds 17064e3fd7a0SAlexey Dobriyan *saddr = hiscore->ifa->addr; 1707a9b05723SYOSHIFUJI Hideaki in6_ifa_put(hiscore->ifa); 1708072047e4SYOSHIFUJI Hideaki return 0; 17091da177e4SLinus Torvalds } 17105e5f3f0fSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_dev_get_saddr); 17111da177e4SLinus Torvalds 17128965779dSAmerigo Wang int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr, 1713479840ffSJiri Pirko u32 banned_flags) 1714b7b1bfceSHannes Frederic Sowa { 1715b7b1bfceSHannes Frederic Sowa struct inet6_ifaddr *ifp; 1716b7b1bfceSHannes Frederic Sowa int err = -EADDRNOTAVAIL; 1717b7b1bfceSHannes Frederic Sowa 1718602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) { 1719602582caSHannes Frederic Sowa if (ifp->scope > IFA_LINK) 1720602582caSHannes Frederic Sowa break; 1721b7b1bfceSHannes Frederic Sowa if (ifp->scope == IFA_LINK && 1722b7b1bfceSHannes Frederic Sowa !(ifp->flags & banned_flags)) { 1723b7b1bfceSHannes Frederic Sowa *addr = ifp->addr; 1724b7b1bfceSHannes Frederic Sowa err = 0; 1725b7b1bfceSHannes Frederic Sowa break; 1726b7b1bfceSHannes Frederic Sowa } 1727b7b1bfceSHannes Frederic Sowa } 1728b7b1bfceSHannes Frederic Sowa return err; 1729b7b1bfceSHannes Frederic Sowa } 1730b7b1bfceSHannes Frederic Sowa 173195c385b4SNeil Horman int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr, 1732479840ffSJiri Pirko u32 banned_flags) 17331da177e4SLinus Torvalds { 17341da177e4SLinus Torvalds struct inet6_dev *idev; 17351da177e4SLinus Torvalds int err = -EADDRNOTAVAIL; 17361da177e4SLinus Torvalds 17378814c4b5SYOSHIFUJI Hideaki rcu_read_lock(); 1738e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 1739e21e8467SStephen Hemminger if (idev) { 17401da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1741b7b1bfceSHannes Frederic Sowa err = __ipv6_get_lladdr(idev, addr, banned_flags); 17421da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 17431da177e4SLinus Torvalds } 17448814c4b5SYOSHIFUJI Hideaki rcu_read_unlock(); 17451da177e4SLinus Torvalds return err; 17461da177e4SLinus Torvalds } 17471da177e4SLinus Torvalds 17481da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev) 17491da177e4SLinus Torvalds { 17501da177e4SLinus Torvalds int cnt = 0; 17511da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 17521da177e4SLinus Torvalds 17531da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1754502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) 17551da177e4SLinus Torvalds cnt++; 17561da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 17571da177e4SLinus Torvalds return cnt; 17581da177e4SLinus Torvalds } 17591da177e4SLinus Torvalds 1760b71d1d42SEric Dumazet int ipv6_chk_addr(struct net *net, const struct in6_addr *addr, 17612a7851bfSFlorian Westphal const struct net_device *dev, int strict) 17621da177e4SLinus Torvalds { 1763c58da4c6SErik Kline return ipv6_chk_addr_and_flags(net, addr, dev, strict, IFA_F_TENTATIVE); 1764c58da4c6SErik Kline } 1765c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr); 1766c58da4c6SErik Kline 1767c58da4c6SErik Kline int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr, 1768c58da4c6SErik Kline const struct net_device *dev, int strict, 1769c58da4c6SErik Kline u32 banned_flags) 1770c58da4c6SErik Kline { 1771eedf042aSStephen Hemminger struct inet6_ifaddr *ifp; 1772ddbe5032SEric Dumazet unsigned int hash = inet6_addr_hash(addr); 1773c58da4c6SErik Kline u32 ifp_flags; 17741da177e4SLinus Torvalds 17755c578aedSstephen hemminger rcu_read_lock_bh(); 1776b67bfe0dSSasha Levin hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) { 1777878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 1778bfeade08SDaniel Lezcano continue; 1779c58da4c6SErik Kline /* Decouple optimistic from tentative for evaluation here. 1780c58da4c6SErik Kline * Ban optimistic addresses explicitly, when required. 1781c58da4c6SErik Kline */ 1782c58da4c6SErik Kline ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC) 1783c58da4c6SErik Kline ? (ifp->flags&~IFA_F_TENTATIVE) 1784c58da4c6SErik Kline : ifp->flags; 17851da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr) && 1786c58da4c6SErik Kline !(ifp_flags&banned_flags) && 178763159f29SIan Morris (!dev || ifp->idev->dev == dev || 1788eedf042aSStephen Hemminger !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) { 17895c578aedSstephen hemminger rcu_read_unlock_bh(); 1790eedf042aSStephen Hemminger return 1; 1791eedf042aSStephen Hemminger } 1792eedf042aSStephen Hemminger } 17935c578aedSstephen hemminger 1794eedf042aSStephen Hemminger rcu_read_unlock_bh(); 1795eedf042aSStephen Hemminger return 0; 17961da177e4SLinus Torvalds } 1797c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr_and_flags); 17987159039aSYOSHIFUJI Hideaki 17993e81c6daSDavid S. Miller static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr, 180006bfe655SDaniel Lezcano struct net_device *dev) 18011da177e4SLinus Torvalds { 1802ddbe5032SEric Dumazet unsigned int hash = inet6_addr_hash(addr); 18031da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 18041da177e4SLinus Torvalds 1805b67bfe0dSSasha Levin hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) { 1806878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 180706bfe655SDaniel Lezcano continue; 18081da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr)) { 180963159f29SIan Morris if (!dev || ifp->idev->dev == dev) 18103e81c6daSDavid S. Miller return true; 18111da177e4SLinus Torvalds } 18121da177e4SLinus Torvalds } 18133e81c6daSDavid S. Miller return false; 18141da177e4SLinus Torvalds } 18151da177e4SLinus Torvalds 18167df37ff3SCatalin\(ux\) M. BOIE /* Compares an address/prefix_len with addresses on device @dev. 18177df37ff3SCatalin\(ux\) M. BOIE * If one is found it returns true. 18187df37ff3SCatalin\(ux\) M. BOIE */ 18197df37ff3SCatalin\(ux\) M. BOIE bool ipv6_chk_custom_prefix(const struct in6_addr *addr, 18207df37ff3SCatalin\(ux\) M. BOIE const unsigned int prefix_len, struct net_device *dev) 18217df37ff3SCatalin\(ux\) M. BOIE { 18227df37ff3SCatalin\(ux\) M. BOIE struct inet6_dev *idev; 18237df37ff3SCatalin\(ux\) M. BOIE struct inet6_ifaddr *ifa; 18247df37ff3SCatalin\(ux\) M. BOIE bool ret = false; 18257df37ff3SCatalin\(ux\) M. BOIE 18267df37ff3SCatalin\(ux\) M. BOIE rcu_read_lock(); 18277df37ff3SCatalin\(ux\) M. BOIE idev = __in6_dev_get(dev); 18287df37ff3SCatalin\(ux\) M. BOIE if (idev) { 18297df37ff3SCatalin\(ux\) M. BOIE read_lock_bh(&idev->lock); 18307df37ff3SCatalin\(ux\) M. BOIE list_for_each_entry(ifa, &idev->addr_list, if_list) { 18317df37ff3SCatalin\(ux\) M. BOIE ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len); 18327df37ff3SCatalin\(ux\) M. BOIE if (ret) 18337df37ff3SCatalin\(ux\) M. BOIE break; 18347df37ff3SCatalin\(ux\) M. BOIE } 18357df37ff3SCatalin\(ux\) M. BOIE read_unlock_bh(&idev->lock); 18367df37ff3SCatalin\(ux\) M. BOIE } 18377df37ff3SCatalin\(ux\) M. BOIE rcu_read_unlock(); 18387df37ff3SCatalin\(ux\) M. BOIE 18397df37ff3SCatalin\(ux\) M. BOIE return ret; 18407df37ff3SCatalin\(ux\) M. BOIE } 18417df37ff3SCatalin\(ux\) M. BOIE EXPORT_SYMBOL(ipv6_chk_custom_prefix); 18427df37ff3SCatalin\(ux\) M. BOIE 1843b71d1d42SEric Dumazet int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev) 184452eeeb84SYOSHIFUJI Hideaki { 184552eeeb84SYOSHIFUJI Hideaki struct inet6_dev *idev; 184652eeeb84SYOSHIFUJI Hideaki struct inet6_ifaddr *ifa; 184752eeeb84SYOSHIFUJI Hideaki int onlink; 184852eeeb84SYOSHIFUJI Hideaki 184952eeeb84SYOSHIFUJI Hideaki onlink = 0; 185052eeeb84SYOSHIFUJI Hideaki rcu_read_lock(); 185152eeeb84SYOSHIFUJI Hideaki idev = __in6_dev_get(dev); 185252eeeb84SYOSHIFUJI Hideaki if (idev) { 185352eeeb84SYOSHIFUJI Hideaki read_lock_bh(&idev->lock); 1854502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 185552eeeb84SYOSHIFUJI Hideaki onlink = ipv6_prefix_equal(addr, &ifa->addr, 185652eeeb84SYOSHIFUJI Hideaki ifa->prefix_len); 185752eeeb84SYOSHIFUJI Hideaki if (onlink) 185852eeeb84SYOSHIFUJI Hideaki break; 185952eeeb84SYOSHIFUJI Hideaki } 186052eeeb84SYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 186152eeeb84SYOSHIFUJI Hideaki } 186252eeeb84SYOSHIFUJI Hideaki rcu_read_unlock(); 186352eeeb84SYOSHIFUJI Hideaki return onlink; 186452eeeb84SYOSHIFUJI Hideaki } 186552eeeb84SYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_prefix); 186652eeeb84SYOSHIFUJI Hideaki 18679acd9f3aSYOSHIFUJI Hideaki struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr, 18681cab3da6SDaniel Lezcano struct net_device *dev, int strict) 18691da177e4SLinus Torvalds { 1870b79d1d54SDavid S. Miller struct inet6_ifaddr *ifp, *result = NULL; 1871ddbe5032SEric Dumazet unsigned int hash = inet6_addr_hash(addr); 18721da177e4SLinus Torvalds 18735c578aedSstephen hemminger rcu_read_lock_bh(); 1874b67bfe0dSSasha Levin hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) { 1875878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 18761cab3da6SDaniel Lezcano continue; 18771da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr)) { 187863159f29SIan Morris if (!dev || ifp->idev->dev == dev || 18791da177e4SLinus Torvalds !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) { 1880b79d1d54SDavid S. Miller result = ifp; 18811da177e4SLinus Torvalds in6_ifa_hold(ifp); 18821da177e4SLinus Torvalds break; 18831da177e4SLinus Torvalds } 18841da177e4SLinus Torvalds } 18851da177e4SLinus Torvalds } 18865c578aedSstephen hemminger rcu_read_unlock_bh(); 18871da177e4SLinus Torvalds 1888b79d1d54SDavid S. Miller return result; 18891da177e4SLinus Torvalds } 18901da177e4SLinus Torvalds 18911da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */ 18921da177e4SLinus Torvalds 1893cc411d0bSBrian Haley static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed) 18941da177e4SLinus Torvalds { 18953d171f39SLubomir Rintel if (dad_failed) 18963d171f39SLubomir Rintel ifp->flags |= IFA_F_DADFAILED; 18973d171f39SLubomir Rintel 18981da177e4SLinus Torvalds if (ifp->flags&IFA_F_PERMANENT) { 18991da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 1900c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifp); 19011da177e4SLinus Torvalds ifp->flags |= IFA_F_TENTATIVE; 19021da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 1903e2577a06SHerbert Xu if (dad_failed) 1904e2577a06SHerbert Xu ipv6_ifa_notify(0, ifp); 19051da177e4SLinus Torvalds in6_ifa_put(ifp); 19061da177e4SLinus Torvalds } else if (ifp->flags&IFA_F_TEMPORARY) { 19071da177e4SLinus Torvalds struct inet6_ifaddr *ifpub; 19081da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 19091da177e4SLinus Torvalds ifpub = ifp->ifpub; 19101da177e4SLinus Torvalds if (ifpub) { 19111da177e4SLinus Torvalds in6_ifa_hold(ifpub); 19121da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 19131da177e4SLinus Torvalds ipv6_create_tempaddr(ifpub, ifp); 19141da177e4SLinus Torvalds in6_ifa_put(ifpub); 19151da177e4SLinus Torvalds } else { 19161da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 19171da177e4SLinus Torvalds } 19181da177e4SLinus Torvalds ipv6_del_addr(ifp); 1919c15b1ccaSHannes Frederic Sowa } else { 19201da177e4SLinus Torvalds ipv6_del_addr(ifp); 19211da177e4SLinus Torvalds } 1922c15b1ccaSHannes Frederic Sowa } 19231da177e4SLinus Torvalds 1924f2344a13SHerbert Xu static int addrconf_dad_end(struct inet6_ifaddr *ifp) 1925f2344a13SHerbert Xu { 1926f2344a13SHerbert Xu int err = -ENOENT; 1927f2344a13SHerbert Xu 19288e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 1929f2344a13SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DAD) { 1930f2344a13SHerbert Xu ifp->state = INET6_IFADDR_STATE_POSTDAD; 1931f2344a13SHerbert Xu err = 0; 1932f2344a13SHerbert Xu } 19338e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 1934f2344a13SHerbert Xu 1935f2344a13SHerbert Xu return err; 1936f2344a13SHerbert Xu } 1937f2344a13SHerbert Xu 19383c21edbdSYOSHIFUJI Hideaki void addrconf_dad_failure(struct inet6_ifaddr *ifp) 19393c21edbdSYOSHIFUJI Hideaki { 19401b34be74SYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev; 19411855b7c3SHannes Frederic Sowa struct net *net = dev_net(ifp->idev->dev); 19429bdd8d40SBrian Haley 1943853dc2e0SUrsula Braun if (addrconf_dad_end(ifp)) { 1944853dc2e0SUrsula Braun in6_ifa_put(ifp); 1945f2344a13SHerbert Xu return; 1946853dc2e0SUrsula Braun } 1947f2344a13SHerbert Xu 1948e87cc472SJoe Perches net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n", 19490522fea6SJens Rosenboom ifp->idev->dev->name, &ifp->addr); 19509bdd8d40SBrian Haley 19515f40ef77SHannes Frederic Sowa spin_lock_bh(&ifp->lock); 19521b34be74SYOSHIFUJI Hideaki 19535f40ef77SHannes Frederic Sowa if (ifp->flags & IFA_F_STABLE_PRIVACY) { 19545f40ef77SHannes Frederic Sowa int scope = ifp->scope; 19555f40ef77SHannes Frederic Sowa u32 flags = ifp->flags; 19565f40ef77SHannes Frederic Sowa struct in6_addr new_addr; 19575f40ef77SHannes Frederic Sowa struct inet6_ifaddr *ifp2; 19585f40ef77SHannes Frederic Sowa u32 valid_lft, preferred_lft; 19595f40ef77SHannes Frederic Sowa int pfxlen = ifp->prefix_len; 19605f40ef77SHannes Frederic Sowa int retries = ifp->stable_privacy_retry + 1; 19615f40ef77SHannes Frederic Sowa 19621855b7c3SHannes Frederic Sowa if (retries > net->ipv6.sysctl.idgen_retries) { 19635f40ef77SHannes Frederic Sowa net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n", 19645f40ef77SHannes Frederic Sowa ifp->idev->dev->name); 19655f40ef77SHannes Frederic Sowa goto errdad; 19665f40ef77SHannes Frederic Sowa } 19675f40ef77SHannes Frederic Sowa 19685f40ef77SHannes Frederic Sowa new_addr = ifp->addr; 19695f40ef77SHannes Frederic Sowa if (ipv6_generate_stable_address(&new_addr, retries, 19705f40ef77SHannes Frederic Sowa idev)) 19715f40ef77SHannes Frederic Sowa goto errdad; 19725f40ef77SHannes Frederic Sowa 19735f40ef77SHannes Frederic Sowa valid_lft = ifp->valid_lft; 19745f40ef77SHannes Frederic Sowa preferred_lft = ifp->prefered_lft; 19755f40ef77SHannes Frederic Sowa 19765f40ef77SHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 19775f40ef77SHannes Frederic Sowa 19785f40ef77SHannes Frederic Sowa if (idev->cnf.max_addresses && 19795f40ef77SHannes Frederic Sowa ipv6_count_addresses(idev) >= 19805f40ef77SHannes Frederic Sowa idev->cnf.max_addresses) 19815f40ef77SHannes Frederic Sowa goto lock_errdad; 19825f40ef77SHannes Frederic Sowa 19835f40ef77SHannes Frederic Sowa net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n", 19845f40ef77SHannes Frederic Sowa ifp->idev->dev->name); 19855f40ef77SHannes Frederic Sowa 19865f40ef77SHannes Frederic Sowa ifp2 = ipv6_add_addr(idev, &new_addr, NULL, pfxlen, 19875f40ef77SHannes Frederic Sowa scope, flags, valid_lft, 19885f40ef77SHannes Frederic Sowa preferred_lft); 19895f40ef77SHannes Frederic Sowa if (IS_ERR(ifp2)) 19905f40ef77SHannes Frederic Sowa goto lock_errdad; 19915f40ef77SHannes Frederic Sowa 19925f40ef77SHannes Frederic Sowa spin_lock_bh(&ifp2->lock); 19935f40ef77SHannes Frederic Sowa ifp2->stable_privacy_retry = retries; 19945f40ef77SHannes Frederic Sowa ifp2->state = INET6_IFADDR_STATE_PREDAD; 19955f40ef77SHannes Frederic Sowa spin_unlock_bh(&ifp2->lock); 19965f40ef77SHannes Frederic Sowa 19971855b7c3SHannes Frederic Sowa addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay); 19985f40ef77SHannes Frederic Sowa in6_ifa_put(ifp2); 19995f40ef77SHannes Frederic Sowa lock_errdad: 20005f40ef77SHannes Frederic Sowa spin_lock_bh(&ifp->lock); 20011b34be74SYOSHIFUJI Hideaki } 20021b34be74SYOSHIFUJI Hideaki 20035f40ef77SHannes Frederic Sowa errdad: 2004c15b1ccaSHannes Frederic Sowa /* transition from _POSTDAD to _ERRDAD */ 2005c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_ERRDAD; 20068e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 2007c15b1ccaSHannes Frederic Sowa 2008c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 0); 2009751eb6b6SWei Yongjun in6_ifa_put(ifp); 20103c21edbdSYOSHIFUJI Hideaki } 20111da177e4SLinus Torvalds 2012a9ed4a29SSabrina Dubroca /* Join to solicited addr multicast group. 2013a9ed4a29SSabrina Dubroca * caller must hold RTNL */ 2014b71d1d42SEric Dumazet void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr) 20151da177e4SLinus Torvalds { 20161da177e4SLinus Torvalds struct in6_addr maddr; 20171da177e4SLinus Torvalds 20181da177e4SLinus Torvalds if (dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 20191da177e4SLinus Torvalds return; 20201da177e4SLinus Torvalds 20211da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr); 20221da177e4SLinus Torvalds ipv6_dev_mc_inc(dev, &maddr); 20231da177e4SLinus Torvalds } 20241da177e4SLinus Torvalds 2025a9ed4a29SSabrina Dubroca /* caller must hold RTNL */ 2026b71d1d42SEric Dumazet void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr) 20271da177e4SLinus Torvalds { 20281da177e4SLinus Torvalds struct in6_addr maddr; 20291da177e4SLinus Torvalds 20301da177e4SLinus Torvalds if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 20311da177e4SLinus Torvalds return; 20321da177e4SLinus Torvalds 20331da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr); 20341da177e4SLinus Torvalds __ipv6_dev_mc_dec(idev, &maddr); 20351da177e4SLinus Torvalds } 20361da177e4SLinus Torvalds 2037a9ed4a29SSabrina Dubroca /* caller must hold RTNL */ 203820380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp) 20391da177e4SLinus Torvalds { 20401da177e4SLinus Torvalds struct in6_addr addr; 2041c15b1ccaSHannes Frederic Sowa 204288ad3149SHannes Frederic Sowa if (ifp->prefix_len >= 127) /* RFC 6164 */ 20432bda8a0cSBjørn Mork return; 20441da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 20451da177e4SLinus Torvalds if (ipv6_addr_any(&addr)) 20461da177e4SLinus Torvalds return; 2047013b4d90SWANG Cong __ipv6_dev_ac_inc(ifp->idev, &addr); 20481da177e4SLinus Torvalds } 20491da177e4SLinus Torvalds 2050a9ed4a29SSabrina Dubroca /* caller must hold RTNL */ 205120380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp) 20521da177e4SLinus Torvalds { 20531da177e4SLinus Torvalds struct in6_addr addr; 2054c15b1ccaSHannes Frederic Sowa 205588ad3149SHannes Frederic Sowa if (ifp->prefix_len >= 127) /* RFC 6164 */ 205632019e65SYOSHIFUJI Hideaki return; 20571da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 20581da177e4SLinus Torvalds if (ipv6_addr_any(&addr)) 20591da177e4SLinus Torvalds return; 20601da177e4SLinus Torvalds __ipv6_dev_ac_dec(ifp->idev, &addr); 20611da177e4SLinus Torvalds } 20621da177e4SLinus Torvalds 206306a4c1c5Salex.bluesman.smirnov@gmail.com static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev) 206406a4c1c5Salex.bluesman.smirnov@gmail.com { 20655241c2d7SAlexander Aring if (dev->addr_len != EUI64_ADDR_LEN) 206606a4c1c5Salex.bluesman.smirnov@gmail.com return -1; 20675241c2d7SAlexander Aring memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN); 20685e98a36eSYOSHIFUJI Hideaki / 吉藤英明 eui[0] ^= 2; 206906a4c1c5Salex.bluesman.smirnov@gmail.com return 0; 207006a4c1c5Salex.bluesman.smirnov@gmail.com } 207106a4c1c5Salex.bluesman.smirnov@gmail.com 2072cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev) 2073cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 { 2074cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 union fwnet_hwaddr *ha; 2075cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 2076cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 if (dev->addr_len != FWNET_ALEN) 2077cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return -1; 2078cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 2079cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 ha = (union fwnet_hwaddr *)dev->dev_addr; 2080cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 2081cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id)); 2082cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 eui[0] ^= 2; 2083cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return 0; 2084cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 } 2085cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 2086073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev) 2087073a8e0eSYOSHIFUJI Hideaki { 20881da177e4SLinus Torvalds /* XXX: inherit EUI-64 from other interface -- yoshfuji */ 20891da177e4SLinus Torvalds if (dev->addr_len != ARCNET_ALEN) 20901da177e4SLinus Torvalds return -1; 20911da177e4SLinus Torvalds memset(eui, 0, 7); 20921da177e4SLinus Torvalds eui[7] = *(u8 *)dev->dev_addr; 20931da177e4SLinus Torvalds return 0; 2094073a8e0eSYOSHIFUJI Hideaki } 2095073a8e0eSYOSHIFUJI Hideaki 2096073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev) 2097073a8e0eSYOSHIFUJI Hideaki { 20981da177e4SLinus Torvalds if (dev->addr_len != INFINIBAND_ALEN) 20991da177e4SLinus Torvalds return -1; 21001da177e4SLinus Torvalds memcpy(eui, dev->dev_addr + 12, 8); 21011da177e4SLinus Torvalds eui[0] |= 2; 21021da177e4SLinus Torvalds return 0; 21031da177e4SLinus Torvalds } 2104073a8e0eSYOSHIFUJI Hideaki 2105c61393eaSstephen hemminger static int __ipv6_isatap_ifid(u8 *eui, __be32 addr) 2106dfd982baSYOSHIFUJI Hideaki { 21079af28511SSascha Hlusiak if (addr == 0) 21089af28511SSascha Hlusiak return -1; 2109dfd982baSYOSHIFUJI Hideaki eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) || 2110dfd982baSYOSHIFUJI Hideaki ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) || 2111dfd982baSYOSHIFUJI Hideaki ipv4_is_private_172(addr) || ipv4_is_test_192(addr) || 2112dfd982baSYOSHIFUJI Hideaki ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) || 2113dfd982baSYOSHIFUJI Hideaki ipv4_is_test_198(addr) || ipv4_is_multicast(addr) || 2114dfd982baSYOSHIFUJI Hideaki ipv4_is_lbcast(addr)) ? 0x00 : 0x02; 2115dfd982baSYOSHIFUJI Hideaki eui[1] = 0; 2116dfd982baSYOSHIFUJI Hideaki eui[2] = 0x5E; 2117dfd982baSYOSHIFUJI Hideaki eui[3] = 0xFE; 2118dfd982baSYOSHIFUJI Hideaki memcpy(eui + 4, &addr, 4); 2119dfd982baSYOSHIFUJI Hideaki return 0; 2120dfd982baSYOSHIFUJI Hideaki } 2121dfd982baSYOSHIFUJI Hideaki 2122dfd982baSYOSHIFUJI Hideaki static int addrconf_ifid_sit(u8 *eui, struct net_device *dev) 2123dfd982baSYOSHIFUJI Hideaki { 2124dfd982baSYOSHIFUJI Hideaki if (dev->priv_flags & IFF_ISATAP) 2125dfd982baSYOSHIFUJI Hideaki return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); 2126dfd982baSYOSHIFUJI Hideaki return -1; 2127dfd982baSYOSHIFUJI Hideaki } 2128dfd982baSYOSHIFUJI Hideaki 2129aee80b54Sstephen hemminger static int addrconf_ifid_gre(u8 *eui, struct net_device *dev) 2130aee80b54Sstephen hemminger { 2131aee80b54Sstephen hemminger return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); 2132aee80b54Sstephen hemminger } 2133aee80b54Sstephen hemminger 2134e837735eSNicolas Dichtel static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev) 2135e837735eSNicolas Dichtel { 2136e837735eSNicolas Dichtel memcpy(eui, dev->perm_addr, 3); 2137e837735eSNicolas Dichtel memcpy(eui + 5, dev->perm_addr + 3, 3); 2138e837735eSNicolas Dichtel eui[3] = 0xFF; 2139e837735eSNicolas Dichtel eui[4] = 0xFE; 2140e837735eSNicolas Dichtel eui[0] ^= 2; 2141e837735eSNicolas Dichtel return 0; 2142e837735eSNicolas Dichtel } 2143e837735eSNicolas Dichtel 2144073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev) 2145073a8e0eSYOSHIFUJI Hideaki { 2146073a8e0eSYOSHIFUJI Hideaki switch (dev->type) { 2147073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ETHER: 2148073a8e0eSYOSHIFUJI Hideaki case ARPHRD_FDDI: 2149073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_eui48(eui, dev); 2150073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ARCNET: 2151073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_arcnet(eui, dev); 2152073a8e0eSYOSHIFUJI Hideaki case ARPHRD_INFINIBAND: 2153073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_infiniband(eui, dev); 2154c7dc89c0SFred L. Templin case ARPHRD_SIT: 2155dfd982baSYOSHIFUJI Hideaki return addrconf_ifid_sit(eui, dev); 2156aee80b54Sstephen hemminger case ARPHRD_IPGRE: 215745ce0fd1SFelix Jia case ARPHRD_TUNNEL: 2158aee80b54Sstephen hemminger return addrconf_ifid_gre(eui, dev); 2159e74bccb8SJukka Rissanen case ARPHRD_6LOWPAN: 216006a4c1c5Salex.bluesman.smirnov@gmail.com return addrconf_ifid_eui64(eui, dev); 2161cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 case ARPHRD_IEEE1394: 2162cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return addrconf_ifid_ieee1394(eui, dev); 2163e837735eSNicolas Dichtel case ARPHRD_TUNNEL6: 216445ce0fd1SFelix Jia case ARPHRD_IP6GRE: 2165e837735eSNicolas Dichtel return addrconf_ifid_ip6tnl(eui, dev); 2166073a8e0eSYOSHIFUJI Hideaki } 21671da177e4SLinus Torvalds return -1; 21681da177e4SLinus Torvalds } 21691da177e4SLinus Torvalds 21701da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev) 21711da177e4SLinus Torvalds { 21721da177e4SLinus Torvalds int err = -1; 21731da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 21741da177e4SLinus Torvalds 21751da177e4SLinus Torvalds read_lock_bh(&idev->lock); 2176602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) { 2177602582caSHannes Frederic Sowa if (ifp->scope > IFA_LINK) 2178602582caSHannes Frederic Sowa break; 21791da177e4SLinus Torvalds if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) { 21801da177e4SLinus Torvalds memcpy(eui, ifp->addr.s6_addr+8, 8); 21811da177e4SLinus Torvalds err = 0; 21821da177e4SLinus Torvalds break; 21831da177e4SLinus Torvalds } 21841da177e4SLinus Torvalds } 21851da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 21861da177e4SLinus Torvalds return err; 21871da177e4SLinus Torvalds } 21881da177e4SLinus Torvalds 21891da177e4SLinus Torvalds /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */ 21909d6280daSJiri Bohac static void ipv6_regen_rndid(struct inet6_dev *idev) 21911da177e4SLinus Torvalds { 21921da177e4SLinus Torvalds regen: 2193955189efSYOSHIFUJI Hideaki get_random_bytes(idev->rndid, sizeof(idev->rndid)); 21941da177e4SLinus Torvalds idev->rndid[0] &= ~0x02; 21951da177e4SLinus Torvalds 21961da177e4SLinus Torvalds /* 21971da177e4SLinus Torvalds * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>: 21981da177e4SLinus Torvalds * check if generated address is not inappropriate 21991da177e4SLinus Torvalds * 22001da177e4SLinus Torvalds * - Reserved subnet anycast (RFC 2526) 22011da177e4SLinus Torvalds * 11111101 11....11 1xxxxxxx 2202c7dc89c0SFred L. Templin * - ISATAP (RFC4214) 6.1 22031da177e4SLinus Torvalds * 00-00-5E-FE-xx-xx-xx-xx 22041da177e4SLinus Torvalds * - value 0 22051da177e4SLinus Torvalds * - XXX: already assigned to an address on the device 22061da177e4SLinus Torvalds */ 22071da177e4SLinus Torvalds if (idev->rndid[0] == 0xfd && 22081da177e4SLinus Torvalds (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff && 22091da177e4SLinus Torvalds (idev->rndid[7]&0x80)) 22101da177e4SLinus Torvalds goto regen; 22111da177e4SLinus Torvalds if ((idev->rndid[0]|idev->rndid[1]) == 0) { 22121da177e4SLinus Torvalds if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe) 22131da177e4SLinus Torvalds goto regen; 22141da177e4SLinus Torvalds if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00) 22151da177e4SLinus Torvalds goto regen; 22161da177e4SLinus Torvalds } 22171da177e4SLinus Torvalds } 22181da177e4SLinus Torvalds 22199d6280daSJiri Bohac static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) 22208e5e8f30SEldad Zack { 22211da177e4SLinus Torvalds if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0) 22229d6280daSJiri Bohac ipv6_regen_rndid(idev); 22231da177e4SLinus Torvalds } 22241da177e4SLinus Torvalds 22251da177e4SLinus Torvalds /* 22261da177e4SLinus Torvalds * Add prefix route. 22271da177e4SLinus Torvalds */ 22281da177e4SLinus Torvalds 22291da177e4SLinus Torvalds static void 22301da177e4SLinus Torvalds addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev, 2231e431b8c0SJamal Hadi Salim unsigned long expires, u32 flags) 22321da177e4SLinus Torvalds { 223386872cb5SThomas Graf struct fib6_config cfg = { 2234ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX, 223586872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 223686872cb5SThomas Graf .fc_ifindex = dev->ifindex, 223786872cb5SThomas Graf .fc_expires = expires, 223886872cb5SThomas Graf .fc_dst_len = plen, 223986872cb5SThomas Graf .fc_flags = RTF_UP | flags, 2240c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 2241f410a1fbSStephen Hemminger .fc_protocol = RTPROT_KERNEL, 224286872cb5SThomas Graf }; 22431da177e4SLinus Torvalds 22444e3fd7a0SAlexey Dobriyan cfg.fc_dst = *pfx; 22451da177e4SLinus Torvalds 22461da177e4SLinus Torvalds /* Prevent useless cloning on PtP SIT. 22471da177e4SLinus Torvalds This thing is done here expecting that the whole 22481da177e4SLinus Torvalds class of non-broadcast devices need not cloning. 22491da177e4SLinus Torvalds */ 225007a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 22511da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT)) 225286872cb5SThomas Graf cfg.fc_flags |= RTF_NONEXTHOP; 22530be669bbSJoerg Roedel #endif 22541da177e4SLinus Torvalds 225586872cb5SThomas Graf ip6_route_add(&cfg); 22561da177e4SLinus Torvalds } 22571da177e4SLinus Torvalds 225814ef37b6SAndreas Hofmeister 225914ef37b6SAndreas Hofmeister static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx, 226014ef37b6SAndreas Hofmeister int plen, 226114ef37b6SAndreas Hofmeister const struct net_device *dev, 226214ef37b6SAndreas Hofmeister u32 flags, u32 noflags) 226314ef37b6SAndreas Hofmeister { 226414ef37b6SAndreas Hofmeister struct fib6_node *fn; 226514ef37b6SAndreas Hofmeister struct rt6_info *rt = NULL; 226614ef37b6SAndreas Hofmeister struct fib6_table *table; 2267ca254490SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX; 226814ef37b6SAndreas Hofmeister 2269ca254490SDavid Ahern table = fib6_get_table(dev_net(dev), tb_id); 227063159f29SIan Morris if (!table) 227114ef37b6SAndreas Hofmeister return NULL; 227214ef37b6SAndreas Hofmeister 22735744dd9bSLi RongQing read_lock_bh(&table->tb6_lock); 227414ef37b6SAndreas Hofmeister fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0); 227514ef37b6SAndreas Hofmeister if (!fn) 227614ef37b6SAndreas Hofmeister goto out; 22771f56a01fSMartin KaFai Lau 22781f56a01fSMartin KaFai Lau noflags |= RTF_CACHE; 227914ef37b6SAndreas Hofmeister for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 2280d1918542SDavid S. Miller if (rt->dst.dev->ifindex != dev->ifindex) 228114ef37b6SAndreas Hofmeister continue; 228214ef37b6SAndreas Hofmeister if ((rt->rt6i_flags & flags) != flags) 228314ef37b6SAndreas Hofmeister continue; 228485da53bfSRomain Kuntz if ((rt->rt6i_flags & noflags) != 0) 228514ef37b6SAndreas Hofmeister continue; 228614ef37b6SAndreas Hofmeister dst_hold(&rt->dst); 228714ef37b6SAndreas Hofmeister break; 228814ef37b6SAndreas Hofmeister } 228914ef37b6SAndreas Hofmeister out: 22905744dd9bSLi RongQing read_unlock_bh(&table->tb6_lock); 229114ef37b6SAndreas Hofmeister return rt; 229214ef37b6SAndreas Hofmeister } 229314ef37b6SAndreas Hofmeister 229414ef37b6SAndreas Hofmeister 22951da177e4SLinus Torvalds /* Create "default" multicast route to the interface */ 22961da177e4SLinus Torvalds 22971da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev) 22981da177e4SLinus Torvalds { 229986872cb5SThomas Graf struct fib6_config cfg = { 2300ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL, 230186872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 230286872cb5SThomas Graf .fc_ifindex = dev->ifindex, 230386872cb5SThomas Graf .fc_dst_len = 8, 230486872cb5SThomas Graf .fc_flags = RTF_UP, 2305c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 230686872cb5SThomas Graf }; 23071da177e4SLinus Torvalds 230886872cb5SThomas Graf ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0); 230986872cb5SThomas Graf 231086872cb5SThomas Graf ip6_route_add(&cfg); 23111da177e4SLinus Torvalds } 23121da177e4SLinus Torvalds 23131da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev) 23141da177e4SLinus Torvalds { 23151da177e4SLinus Torvalds struct inet6_dev *idev; 23161da177e4SLinus Torvalds 23171da177e4SLinus Torvalds ASSERT_RTNL(); 23181da177e4SLinus Torvalds 2319e21e8467SStephen Hemminger idev = ipv6_find_idev(dev); 2320e21e8467SStephen Hemminger if (!idev) 232164e724f6SBrian Haley return ERR_PTR(-ENOBUFS); 232264e724f6SBrian Haley 232364e724f6SBrian Haley if (idev->cnf.disable_ipv6) 232464e724f6SBrian Haley return ERR_PTR(-EACCES); 23251da177e4SLinus Torvalds 23261da177e4SLinus Torvalds /* Add default multicast route */ 2327ba46ee4cSDavid Ahern if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev)) 23281da177e4SLinus Torvalds addrconf_add_mroute(dev); 23291da177e4SLinus Torvalds 23301da177e4SLinus Torvalds return idev; 23311da177e4SLinus Torvalds } 23321da177e4SLinus Torvalds 233353bd6749SJiri Pirko static void manage_tempaddrs(struct inet6_dev *idev, 233453bd6749SJiri Pirko struct inet6_ifaddr *ifp, 233553bd6749SJiri Pirko __u32 valid_lft, __u32 prefered_lft, 233653bd6749SJiri Pirko bool create, unsigned long now) 233753bd6749SJiri Pirko { 233853bd6749SJiri Pirko u32 flags; 233953bd6749SJiri Pirko struct inet6_ifaddr *ift; 234053bd6749SJiri Pirko 234153bd6749SJiri Pirko read_lock_bh(&idev->lock); 234253bd6749SJiri Pirko /* update all temporary addresses in the list */ 234353bd6749SJiri Pirko list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) { 234453bd6749SJiri Pirko int age, max_valid, max_prefered; 234553bd6749SJiri Pirko 234653bd6749SJiri Pirko if (ifp != ift->ifpub) 234753bd6749SJiri Pirko continue; 234853bd6749SJiri Pirko 234953bd6749SJiri Pirko /* RFC 4941 section 3.3: 235053bd6749SJiri Pirko * If a received option will extend the lifetime of a public 235153bd6749SJiri Pirko * address, the lifetimes of temporary addresses should 235253bd6749SJiri Pirko * be extended, subject to the overall constraint that no 235353bd6749SJiri Pirko * temporary addresses should ever remain "valid" or "preferred" 235453bd6749SJiri Pirko * for a time longer than (TEMP_VALID_LIFETIME) or 235553bd6749SJiri Pirko * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively. 235653bd6749SJiri Pirko */ 235753bd6749SJiri Pirko age = (now - ift->cstamp) / HZ; 235853bd6749SJiri Pirko max_valid = idev->cnf.temp_valid_lft - age; 235953bd6749SJiri Pirko if (max_valid < 0) 236053bd6749SJiri Pirko max_valid = 0; 236153bd6749SJiri Pirko 236253bd6749SJiri Pirko max_prefered = idev->cnf.temp_prefered_lft - 236376506a98SJiri Bohac idev->desync_factor - age; 236453bd6749SJiri Pirko if (max_prefered < 0) 236553bd6749SJiri Pirko max_prefered = 0; 236653bd6749SJiri Pirko 236753bd6749SJiri Pirko if (valid_lft > max_valid) 236853bd6749SJiri Pirko valid_lft = max_valid; 236953bd6749SJiri Pirko 237053bd6749SJiri Pirko if (prefered_lft > max_prefered) 237153bd6749SJiri Pirko prefered_lft = max_prefered; 237253bd6749SJiri Pirko 237353bd6749SJiri Pirko spin_lock(&ift->lock); 237453bd6749SJiri Pirko flags = ift->flags; 237553bd6749SJiri Pirko ift->valid_lft = valid_lft; 237653bd6749SJiri Pirko ift->prefered_lft = prefered_lft; 237753bd6749SJiri Pirko ift->tstamp = now; 237853bd6749SJiri Pirko if (prefered_lft > 0) 237953bd6749SJiri Pirko ift->flags &= ~IFA_F_DEPRECATED; 238053bd6749SJiri Pirko 238153bd6749SJiri Pirko spin_unlock(&ift->lock); 238253bd6749SJiri Pirko if (!(flags&IFA_F_TENTATIVE)) 238353bd6749SJiri Pirko ipv6_ifa_notify(0, ift); 238453bd6749SJiri Pirko } 238553bd6749SJiri Pirko 238653bd6749SJiri Pirko if ((create || list_empty(&idev->tempaddr_list)) && 238753bd6749SJiri Pirko idev->cnf.use_tempaddr > 0) { 238853bd6749SJiri Pirko /* When a new public address is created as described 238953bd6749SJiri Pirko * in [ADDRCONF], also create a new temporary address. 239053bd6749SJiri Pirko * Also create a temporary address if it's enabled but 239153bd6749SJiri Pirko * no temporary address currently exists. 239253bd6749SJiri Pirko */ 239353bd6749SJiri Pirko read_unlock_bh(&idev->lock); 239453bd6749SJiri Pirko ipv6_create_tempaddr(ifp, NULL); 239553bd6749SJiri Pirko } else { 239653bd6749SJiri Pirko read_unlock_bh(&idev->lock); 239753bd6749SJiri Pirko } 239853bd6749SJiri Pirko } 239953bd6749SJiri Pirko 2400cc9da6ccSBjørn Mork static bool is_addr_mode_generate_stable(struct inet6_dev *idev) 2401cc9da6ccSBjørn Mork { 2402d35a00b8SFelix Jia return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY || 2403d35a00b8SFelix Jia idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM; 2404cc9da6ccSBjørn Mork } 2405cc9da6ccSBjørn Mork 2406cc84b3c6SAlexander Aring int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev, 24074f672235SAlexander Aring const struct prefix_info *pinfo, 24084f672235SAlexander Aring struct inet6_dev *in6_dev, 2409cc84b3c6SAlexander Aring const struct in6_addr *addr, int addr_type, 2410cc84b3c6SAlexander Aring u32 addr_flags, bool sllao, bool tokenized, 24114f672235SAlexander Aring __u32 valid_lft, u32 prefered_lft) 24124f672235SAlexander Aring { 24134f672235SAlexander Aring struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1); 24144f672235SAlexander Aring int create = 0, update_lft = 0; 24154f672235SAlexander Aring 24164f672235SAlexander Aring if (!ifp && valid_lft) { 24174f672235SAlexander Aring int max_addresses = in6_dev->cnf.max_addresses; 24184f672235SAlexander Aring 24194f672235SAlexander Aring #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 24204f672235SAlexander Aring if (in6_dev->cnf.optimistic_dad && 24214f672235SAlexander Aring !net->ipv6.devconf_all->forwarding && sllao) 24224f672235SAlexander Aring addr_flags |= IFA_F_OPTIMISTIC; 24234f672235SAlexander Aring #endif 24244f672235SAlexander Aring 24254f672235SAlexander Aring /* Do not allow to create too much of autoconfigured 24264f672235SAlexander Aring * addresses; this would be too easy way to crash kernel. 24274f672235SAlexander Aring */ 24284f672235SAlexander Aring if (!max_addresses || 24294f672235SAlexander Aring ipv6_count_addresses(in6_dev) < max_addresses) 24304f672235SAlexander Aring ifp = ipv6_add_addr(in6_dev, addr, NULL, 24314f672235SAlexander Aring pinfo->prefix_len, 24324f672235SAlexander Aring addr_type&IPV6_ADDR_SCOPE_MASK, 24334f672235SAlexander Aring addr_flags, valid_lft, 24344f672235SAlexander Aring prefered_lft); 24354f672235SAlexander Aring 24364f672235SAlexander Aring if (IS_ERR_OR_NULL(ifp)) 24374f672235SAlexander Aring return -1; 24384f672235SAlexander Aring 24394f672235SAlexander Aring update_lft = 0; 24404f672235SAlexander Aring create = 1; 24414f672235SAlexander Aring spin_lock_bh(&ifp->lock); 24424f672235SAlexander Aring ifp->flags |= IFA_F_MANAGETEMPADDR; 24434f672235SAlexander Aring ifp->cstamp = jiffies; 24444f672235SAlexander Aring ifp->tokenized = tokenized; 24454f672235SAlexander Aring spin_unlock_bh(&ifp->lock); 24464f672235SAlexander Aring addrconf_dad_start(ifp); 24474f672235SAlexander Aring } 24484f672235SAlexander Aring 24494f672235SAlexander Aring if (ifp) { 24504f672235SAlexander Aring u32 flags; 24514f672235SAlexander Aring unsigned long now; 24524f672235SAlexander Aring u32 stored_lft; 24534f672235SAlexander Aring 24544f672235SAlexander Aring /* update lifetime (RFC2462 5.5.3 e) */ 24554f672235SAlexander Aring spin_lock_bh(&ifp->lock); 24564f672235SAlexander Aring now = jiffies; 24574f672235SAlexander Aring if (ifp->valid_lft > (now - ifp->tstamp) / HZ) 24584f672235SAlexander Aring stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ; 24594f672235SAlexander Aring else 24604f672235SAlexander Aring stored_lft = 0; 24614f672235SAlexander Aring if (!update_lft && !create && stored_lft) { 24624f672235SAlexander Aring const u32 minimum_lft = min_t(u32, 24634f672235SAlexander Aring stored_lft, MIN_VALID_LIFETIME); 24644f672235SAlexander Aring valid_lft = max(valid_lft, minimum_lft); 24654f672235SAlexander Aring 24664f672235SAlexander Aring /* RFC4862 Section 5.5.3e: 24674f672235SAlexander Aring * "Note that the preferred lifetime of the 24684f672235SAlexander Aring * corresponding address is always reset to 24694f672235SAlexander Aring * the Preferred Lifetime in the received 24704f672235SAlexander Aring * Prefix Information option, regardless of 24714f672235SAlexander Aring * whether the valid lifetime is also reset or 24724f672235SAlexander Aring * ignored." 24734f672235SAlexander Aring * 24744f672235SAlexander Aring * So we should always update prefered_lft here. 24754f672235SAlexander Aring */ 24764f672235SAlexander Aring update_lft = 1; 24774f672235SAlexander Aring } 24784f672235SAlexander Aring 24794f672235SAlexander Aring if (update_lft) { 24804f672235SAlexander Aring ifp->valid_lft = valid_lft; 24814f672235SAlexander Aring ifp->prefered_lft = prefered_lft; 24824f672235SAlexander Aring ifp->tstamp = now; 24834f672235SAlexander Aring flags = ifp->flags; 24844f672235SAlexander Aring ifp->flags &= ~IFA_F_DEPRECATED; 24854f672235SAlexander Aring spin_unlock_bh(&ifp->lock); 24864f672235SAlexander Aring 24874f672235SAlexander Aring if (!(flags&IFA_F_TENTATIVE)) 24884f672235SAlexander Aring ipv6_ifa_notify(0, ifp); 24894f672235SAlexander Aring } else 24904f672235SAlexander Aring spin_unlock_bh(&ifp->lock); 24914f672235SAlexander Aring 24924f672235SAlexander Aring manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft, 24934f672235SAlexander Aring create, now); 24944f672235SAlexander Aring 24954f672235SAlexander Aring in6_ifa_put(ifp); 24964f672235SAlexander Aring addrconf_verify(); 24974f672235SAlexander Aring } 24984f672235SAlexander Aring 24994f672235SAlexander Aring return 0; 25004f672235SAlexander Aring } 2501cc84b3c6SAlexander Aring EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr); 25024f672235SAlexander Aring 2503e6bff995SNeil Horman void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao) 25041da177e4SLinus Torvalds { 25051da177e4SLinus Torvalds struct prefix_info *pinfo; 25061da177e4SLinus Torvalds __u32 valid_lft; 25071da177e4SLinus Torvalds __u32 prefered_lft; 25084f672235SAlexander Aring int addr_type, err; 250964236f3fSHannes Frederic Sowa u32 addr_flags = 0; 25101da177e4SLinus Torvalds struct inet6_dev *in6_dev; 251156d417b1SBrian Haley struct net *net = dev_net(dev); 25121da177e4SLinus Torvalds 25131da177e4SLinus Torvalds pinfo = (struct prefix_info *) opt; 25141da177e4SLinus Torvalds 25151da177e4SLinus Torvalds if (len < sizeof(struct prefix_info)) { 2516ba3542e1Sdingtianhong ADBG("addrconf: prefix option too short\n"); 25171da177e4SLinus Torvalds return; 25181da177e4SLinus Torvalds } 25191da177e4SLinus Torvalds 25201da177e4SLinus Torvalds /* 25211da177e4SLinus Torvalds * Validation checks ([ADDRCONF], page 19) 25221da177e4SLinus Torvalds */ 25231da177e4SLinus Torvalds 25241da177e4SLinus Torvalds addr_type = ipv6_addr_type(&pinfo->prefix); 25251da177e4SLinus Torvalds 25261da177e4SLinus Torvalds if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)) 25271da177e4SLinus Torvalds return; 25281da177e4SLinus Torvalds 25291da177e4SLinus Torvalds valid_lft = ntohl(pinfo->valid); 25301da177e4SLinus Torvalds prefered_lft = ntohl(pinfo->prefered); 25311da177e4SLinus Torvalds 25321da177e4SLinus Torvalds if (prefered_lft > valid_lft) { 2533e87cc472SJoe Perches net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n"); 25341da177e4SLinus Torvalds return; 25351da177e4SLinus Torvalds } 25361da177e4SLinus Torvalds 25371da177e4SLinus Torvalds in6_dev = in6_dev_get(dev); 25381da177e4SLinus Torvalds 253963159f29SIan Morris if (!in6_dev) { 2540e87cc472SJoe Perches net_dbg_ratelimited("addrconf: device %s not configured\n", 2541e87cc472SJoe Perches dev->name); 25421da177e4SLinus Torvalds return; 25431da177e4SLinus Torvalds } 25441da177e4SLinus Torvalds 25451da177e4SLinus Torvalds /* 25461da177e4SLinus Torvalds * Two things going on here: 25471da177e4SLinus Torvalds * 1) Add routes for on-link prefixes 25481da177e4SLinus Torvalds * 2) Configure prefixes with the auto flag set 25491da177e4SLinus Torvalds */ 25501da177e4SLinus Torvalds 25514bed72e4SYOSHIFUJI Hideaki if (pinfo->onlink) { 25524bed72e4SYOSHIFUJI Hideaki struct rt6_info *rt; 25534bed72e4SYOSHIFUJI Hideaki unsigned long rt_expires; 25544bed72e4SYOSHIFUJI Hideaki 25551da177e4SLinus Torvalds /* Avoid arithmetic overflow. Really, we could 25566f704992SYOSHIFUJI Hideaki * save rt_expires in seconds, likely valid_lft, 25576f704992SYOSHIFUJI Hideaki * but it would require division in fib gc, that it 25586f704992SYOSHIFUJI Hideaki * not good. 25591da177e4SLinus Torvalds */ 25604bed72e4SYOSHIFUJI Hideaki if (HZ > USER_HZ) 25614bed72e4SYOSHIFUJI Hideaki rt_expires = addrconf_timeout_fixup(valid_lft, HZ); 25624bed72e4SYOSHIFUJI Hideaki else 25634bed72e4SYOSHIFUJI Hideaki rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ); 25643dd4bc68SYOSHIFUJI Hideaki 25654bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(rt_expires)) 25664bed72e4SYOSHIFUJI Hideaki rt_expires *= HZ; 25671da177e4SLinus Torvalds 256814ef37b6SAndreas Hofmeister rt = addrconf_get_prefix_route(&pinfo->prefix, 256914ef37b6SAndreas Hofmeister pinfo->prefix_len, 257014ef37b6SAndreas Hofmeister dev, 257114ef37b6SAndreas Hofmeister RTF_ADDRCONF | RTF_PREFIX_RT, 257214ef37b6SAndreas Hofmeister RTF_GATEWAY | RTF_DEFAULT); 25731da177e4SLinus Torvalds 257414ef37b6SAndreas Hofmeister if (rt) { 25756f704992SYOSHIFUJI Hideaki /* Autoconf prefix route */ 25761da177e4SLinus Torvalds if (valid_lft == 0) { 2577e0a1ad73SThomas Graf ip6_del_rt(rt); 25781da177e4SLinus Torvalds rt = NULL; 25794bed72e4SYOSHIFUJI Hideaki } else if (addrconf_finite_timeout(rt_expires)) { 25806f704992SYOSHIFUJI Hideaki /* not infinity */ 25811716a961SGao feng rt6_set_expires(rt, jiffies + rt_expires); 25826f704992SYOSHIFUJI Hideaki } else { 25831716a961SGao feng rt6_clean_expires(rt); 25841da177e4SLinus Torvalds } 25851da177e4SLinus Torvalds } else if (valid_lft) { 25866f704992SYOSHIFUJI Hideaki clock_t expires = 0; 25874bed72e4SYOSHIFUJI Hideaki int flags = RTF_ADDRCONF | RTF_PREFIX_RT; 25884bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(rt_expires)) { 25896f704992SYOSHIFUJI Hideaki /* not infinity */ 25906f704992SYOSHIFUJI Hideaki flags |= RTF_EXPIRES; 25916f704992SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(rt_expires); 25926f704992SYOSHIFUJI Hideaki } 25931da177e4SLinus Torvalds addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len, 25946f704992SYOSHIFUJI Hideaki dev, expires, flags); 25951da177e4SLinus Torvalds } 259694e187c0SAmerigo Wang ip6_rt_put(rt); 25971da177e4SLinus Torvalds } 25981da177e4SLinus Torvalds 25991da177e4SLinus Torvalds /* Try to figure out our local address for this prefix */ 26001da177e4SLinus Torvalds 26011da177e4SLinus Torvalds if (pinfo->autoconf && in6_dev->cnf.autoconf) { 26021da177e4SLinus Torvalds struct in6_addr addr; 2603f997c55cSAlexander Aring bool tokenized = false, dev_addr_generated = false; 26041da177e4SLinus Torvalds 26051da177e4SLinus Torvalds if (pinfo->prefix_len == 64) { 26061da177e4SLinus Torvalds memcpy(&addr, &pinfo->prefix, 8); 2607f53adae4SDaniel Borkmann 2608f53adae4SDaniel Borkmann if (!ipv6_addr_any(&in6_dev->token)) { 2609f53adae4SDaniel Borkmann read_lock_bh(&in6_dev->lock); 2610f53adae4SDaniel Borkmann memcpy(addr.s6_addr + 8, 2611f53adae4SDaniel Borkmann in6_dev->token.s6_addr + 8, 8); 2612f53adae4SDaniel Borkmann read_unlock_bh(&in6_dev->lock); 2613617fe29dSDaniel Borkmann tokenized = true; 2614cc9da6ccSBjørn Mork } else if (is_addr_mode_generate_stable(in6_dev) && 2615622c81d5SHannes Frederic Sowa !ipv6_generate_stable_address(&addr, 0, 2616622c81d5SHannes Frederic Sowa in6_dev)) { 261764236f3fSHannes Frederic Sowa addr_flags |= IFA_F_STABLE_PRIVACY; 2618622c81d5SHannes Frederic Sowa goto ok; 2619f53adae4SDaniel Borkmann } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) && 26201da177e4SLinus Torvalds ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) { 26214f672235SAlexander Aring goto put; 2622f997c55cSAlexander Aring } else { 2623f997c55cSAlexander Aring dev_addr_generated = true; 26241da177e4SLinus Torvalds } 26251da177e4SLinus Torvalds goto ok; 26261da177e4SLinus Torvalds } 2627e87cc472SJoe Perches net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n", 26281da177e4SLinus Torvalds pinfo->prefix_len); 26294f672235SAlexander Aring goto put; 26301da177e4SLinus Torvalds 26311da177e4SLinus Torvalds ok: 26324f672235SAlexander Aring err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, 26334f672235SAlexander Aring &addr, addr_type, 26344f672235SAlexander Aring addr_flags, sllao, 26354f672235SAlexander Aring tokenized, valid_lft, 26368a226b2cSJiri Benc prefered_lft); 26374f672235SAlexander Aring if (err) 26384f672235SAlexander Aring goto put; 2639f997c55cSAlexander Aring 2640f997c55cSAlexander Aring /* Ignore error case here because previous prefix add addr was 2641f997c55cSAlexander Aring * successful which will be notified. 2642f997c55cSAlexander Aring */ 2643f997c55cSAlexander Aring ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr, 2644f997c55cSAlexander Aring addr_type, addr_flags, sllao, 2645f997c55cSAlexander Aring tokenized, valid_lft, 2646f997c55cSAlexander Aring prefered_lft, 2647f997c55cSAlexander Aring dev_addr_generated); 26481da177e4SLinus Torvalds } 26491da177e4SLinus Torvalds inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo); 26504f672235SAlexander Aring put: 26511da177e4SLinus Torvalds in6_dev_put(in6_dev); 26521da177e4SLinus Torvalds } 26531da177e4SLinus Torvalds 26541da177e4SLinus Torvalds /* 26551da177e4SLinus Torvalds * Set destination address. 26561da177e4SLinus Torvalds * Special case for SIT interfaces where we create a new "virtual" 26571da177e4SLinus Torvalds * device. 26581da177e4SLinus Torvalds */ 2659af284937SDaniel Lezcano int addrconf_set_dstaddr(struct net *net, void __user *arg) 26601da177e4SLinus Torvalds { 26611da177e4SLinus Torvalds struct in6_ifreq ireq; 26621da177e4SLinus Torvalds struct net_device *dev; 26631da177e4SLinus Torvalds int err = -EINVAL; 26641da177e4SLinus Torvalds 26651da177e4SLinus Torvalds rtnl_lock(); 26661da177e4SLinus Torvalds 26671da177e4SLinus Torvalds err = -EFAULT; 26681da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 26691da177e4SLinus Torvalds goto err_exit; 26701da177e4SLinus Torvalds 2671af284937SDaniel Lezcano dev = __dev_get_by_index(net, ireq.ifr6_ifindex); 26721da177e4SLinus Torvalds 26731da177e4SLinus Torvalds err = -ENODEV; 267463159f29SIan Morris if (!dev) 26751da177e4SLinus Torvalds goto err_exit; 26761da177e4SLinus Torvalds 267707a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 26781da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT) { 26795bc3eb7eSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 26801da177e4SLinus Torvalds struct ifreq ifr; 26811da177e4SLinus Torvalds struct ip_tunnel_parm p; 26821da177e4SLinus Torvalds 26831da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 26841da177e4SLinus Torvalds if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4)) 26851da177e4SLinus Torvalds goto err_exit; 26861da177e4SLinus Torvalds 26871da177e4SLinus Torvalds memset(&p, 0, sizeof(p)); 26881da177e4SLinus Torvalds p.iph.daddr = ireq.ifr6_addr.s6_addr32[3]; 26891da177e4SLinus Torvalds p.iph.saddr = 0; 26901da177e4SLinus Torvalds p.iph.version = 4; 26911da177e4SLinus Torvalds p.iph.ihl = 5; 26921da177e4SLinus Torvalds p.iph.protocol = IPPROTO_IPV6; 26931da177e4SLinus Torvalds p.iph.ttl = 64; 2694d20b3109SStephen Hemminger ifr.ifr_ifru.ifru_data = (__force void __user *)&p; 26951da177e4SLinus Torvalds 26965bc3eb7eSStephen Hemminger if (ops->ndo_do_ioctl) { 26975bc3eb7eSStephen Hemminger mm_segment_t oldfs = get_fs(); 26985bc3eb7eSStephen Hemminger 26995bc3eb7eSStephen Hemminger set_fs(KERNEL_DS); 27005bc3eb7eSStephen Hemminger err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL); 27011da177e4SLinus Torvalds set_fs(oldfs); 27025bc3eb7eSStephen Hemminger } else 27035bc3eb7eSStephen Hemminger err = -EOPNOTSUPP; 27041da177e4SLinus Torvalds 27051da177e4SLinus Torvalds if (err == 0) { 27061da177e4SLinus Torvalds err = -ENOBUFS; 2707af284937SDaniel Lezcano dev = __dev_get_by_name(net, p.name); 2708af284937SDaniel Lezcano if (!dev) 27091da177e4SLinus Torvalds goto err_exit; 27101da177e4SLinus Torvalds err = dev_open(dev); 27111da177e4SLinus Torvalds } 27121da177e4SLinus Torvalds } 27130be669bbSJoerg Roedel #endif 27141da177e4SLinus Torvalds 27151da177e4SLinus Torvalds err_exit: 27161da177e4SLinus Torvalds rtnl_unlock(); 27171da177e4SLinus Torvalds return err; 27181da177e4SLinus Torvalds } 27191da177e4SLinus Torvalds 272093a714d6SMadhu Challa static int ipv6_mc_config(struct sock *sk, bool join, 272193a714d6SMadhu Challa const struct in6_addr *addr, int ifindex) 272293a714d6SMadhu Challa { 272393a714d6SMadhu Challa int ret; 272493a714d6SMadhu Challa 272593a714d6SMadhu Challa ASSERT_RTNL(); 272693a714d6SMadhu Challa 272793a714d6SMadhu Challa lock_sock(sk); 272893a714d6SMadhu Challa if (join) 272954ff9ef3SMarcelo Ricardo Leitner ret = ipv6_sock_mc_join(sk, ifindex, addr); 273093a714d6SMadhu Challa else 273154ff9ef3SMarcelo Ricardo Leitner ret = ipv6_sock_mc_drop(sk, ifindex, addr); 273293a714d6SMadhu Challa release_sock(sk); 273393a714d6SMadhu Challa 273493a714d6SMadhu Challa return ret; 273593a714d6SMadhu Challa } 273693a714d6SMadhu Challa 27371da177e4SLinus Torvalds /* 27381da177e4SLinus Torvalds * Manual configuration of address on an interface 27391da177e4SLinus Torvalds */ 2740479840ffSJiri Pirko static int inet6_addr_add(struct net *net, int ifindex, 2741479840ffSJiri Pirko const struct in6_addr *pfx, 2742caeaba79SNicolas Dichtel const struct in6_addr *peer_pfx, 2743479840ffSJiri Pirko unsigned int plen, __u32 ifa_flags, 2744479840ffSJiri Pirko __u32 prefered_lft, __u32 valid_lft) 27451da177e4SLinus Torvalds { 27461da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 27471da177e4SLinus Torvalds struct inet6_dev *idev; 27481da177e4SLinus Torvalds struct net_device *dev; 274993a714d6SMadhu Challa unsigned long timeout; 275093a714d6SMadhu Challa clock_t expires; 27511da177e4SLinus Torvalds int scope; 27526f704992SYOSHIFUJI Hideaki u32 flags; 27531da177e4SLinus Torvalds 27541da177e4SLinus Torvalds ASSERT_RTNL(); 27551da177e4SLinus Torvalds 275624ef0da7SThomas Graf if (plen > 128) 275724ef0da7SThomas Graf return -EINVAL; 275824ef0da7SThomas Graf 27590778769dSNoriaki TAKAMIYA /* check the lifetime */ 27600778769dSNoriaki TAKAMIYA if (!valid_lft || prefered_lft > valid_lft) 27610778769dSNoriaki TAKAMIYA return -EINVAL; 27620778769dSNoriaki TAKAMIYA 276353bd6749SJiri Pirko if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64) 276453bd6749SJiri Pirko return -EINVAL; 276553bd6749SJiri Pirko 2766af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifindex); 2767af284937SDaniel Lezcano if (!dev) 27681da177e4SLinus Torvalds return -ENODEV; 27691da177e4SLinus Torvalds 277064e724f6SBrian Haley idev = addrconf_add_dev(dev); 277164e724f6SBrian Haley if (IS_ERR(idev)) 277264e724f6SBrian Haley return PTR_ERR(idev); 27731da177e4SLinus Torvalds 277493a714d6SMadhu Challa if (ifa_flags & IFA_F_MCAUTOJOIN) { 277593a714d6SMadhu Challa int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk, 277693a714d6SMadhu Challa true, pfx, ifindex); 277793a714d6SMadhu Challa 277893a714d6SMadhu Challa if (ret < 0) 277993a714d6SMadhu Challa return ret; 278093a714d6SMadhu Challa } 278193a714d6SMadhu Challa 27821da177e4SLinus Torvalds scope = ipv6_addr_scope(pfx); 27831da177e4SLinus Torvalds 27844bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(valid_lft, HZ); 27854bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 27864bed72e4SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(timeout * HZ); 27874bed72e4SYOSHIFUJI Hideaki valid_lft = timeout; 27886f704992SYOSHIFUJI Hideaki flags = RTF_EXPIRES; 27894bed72e4SYOSHIFUJI Hideaki } else { 27904bed72e4SYOSHIFUJI Hideaki expires = 0; 27914bed72e4SYOSHIFUJI Hideaki flags = 0; 27924bed72e4SYOSHIFUJI Hideaki ifa_flags |= IFA_F_PERMANENT; 27936f704992SYOSHIFUJI Hideaki } 27940778769dSNoriaki TAKAMIYA 27954bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(prefered_lft, HZ); 27964bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 27974bed72e4SYOSHIFUJI Hideaki if (timeout == 0) 27980778769dSNoriaki TAKAMIYA ifa_flags |= IFA_F_DEPRECATED; 27994bed72e4SYOSHIFUJI Hideaki prefered_lft = timeout; 28004bed72e4SYOSHIFUJI Hideaki } 28010778769dSNoriaki TAKAMIYA 28028a226b2cSJiri Benc ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags, 28038a226b2cSJiri Benc valid_lft, prefered_lft); 28040778769dSNoriaki TAKAMIYA 28051da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 2806761aac73SThomas Haller if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) { 280746d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 28086f704992SYOSHIFUJI Hideaki expires, flags); 2809761aac73SThomas Haller } 2810761aac73SThomas Haller 281195c385b4SNeil Horman /* 281295c385b4SNeil Horman * Note that section 3.1 of RFC 4429 indicates 281395c385b4SNeil Horman * that the Optimistic flag should not be set for 281495c385b4SNeil Horman * manually configured addresses 281595c385b4SNeil Horman */ 2816cf22f9a2SDavid S. Miller addrconf_dad_start(ifp); 281753bd6749SJiri Pirko if (ifa_flags & IFA_F_MANAGETEMPADDR) 281853bd6749SJiri Pirko manage_tempaddrs(idev, ifp, valid_lft, prefered_lft, 281953bd6749SJiri Pirko true, jiffies); 28201da177e4SLinus Torvalds in6_ifa_put(ifp); 2821c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 28221da177e4SLinus Torvalds return 0; 282393a714d6SMadhu Challa } else if (ifa_flags & IFA_F_MCAUTOJOIN) { 282493a714d6SMadhu Challa ipv6_mc_config(net->ipv6.mc_autojoin_sk, 282593a714d6SMadhu Challa false, pfx, ifindex); 28261da177e4SLinus Torvalds } 28271da177e4SLinus Torvalds 28281da177e4SLinus Torvalds return PTR_ERR(ifp); 28291da177e4SLinus Torvalds } 28301da177e4SLinus Torvalds 28316046d5b4SHeiner Kallweit static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags, 28326046d5b4SHeiner Kallweit const struct in6_addr *pfx, unsigned int plen) 28331da177e4SLinus Torvalds { 28341da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 28351da177e4SLinus Torvalds struct inet6_dev *idev; 28361da177e4SLinus Torvalds struct net_device *dev; 28371da177e4SLinus Torvalds 283824ef0da7SThomas Graf if (plen > 128) 283924ef0da7SThomas Graf return -EINVAL; 284024ef0da7SThomas Graf 2841af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifindex); 2842af284937SDaniel Lezcano if (!dev) 28431da177e4SLinus Torvalds return -ENODEV; 28441da177e4SLinus Torvalds 2845e5d08d71SIan Morris idev = __in6_dev_get(dev); 284663159f29SIan Morris if (!idev) 28471da177e4SLinus Torvalds return -ENXIO; 28481da177e4SLinus Torvalds 28491da177e4SLinus Torvalds read_lock_bh(&idev->lock); 2850502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 28511da177e4SLinus Torvalds if (ifp->prefix_len == plen && 28521da177e4SLinus Torvalds ipv6_addr_equal(pfx, &ifp->addr)) { 28531da177e4SLinus Torvalds in6_ifa_hold(ifp); 28541da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 28551da177e4SLinus Torvalds 28566046d5b4SHeiner Kallweit if (!(ifp->flags & IFA_F_TEMPORARY) && 28576046d5b4SHeiner Kallweit (ifa_flags & IFA_F_MANAGETEMPADDR)) 28586046d5b4SHeiner Kallweit manage_tempaddrs(idev, ifp, 0, 0, false, 28596046d5b4SHeiner Kallweit jiffies); 28601da177e4SLinus Torvalds ipv6_del_addr(ifp); 28616046d5b4SHeiner Kallweit addrconf_verify_rtnl(); 286293a714d6SMadhu Challa if (ipv6_addr_is_multicast(pfx)) { 286393a714d6SMadhu Challa ipv6_mc_config(net->ipv6.mc_autojoin_sk, 286493a714d6SMadhu Challa false, pfx, dev->ifindex); 286593a714d6SMadhu Challa } 28661da177e4SLinus Torvalds return 0; 28671da177e4SLinus Torvalds } 28681da177e4SLinus Torvalds } 28691da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 28701da177e4SLinus Torvalds return -EADDRNOTAVAIL; 28711da177e4SLinus Torvalds } 28721da177e4SLinus Torvalds 28731da177e4SLinus Torvalds 2874af284937SDaniel Lezcano int addrconf_add_ifaddr(struct net *net, void __user *arg) 28751da177e4SLinus Torvalds { 28761da177e4SLinus Torvalds struct in6_ifreq ireq; 28771da177e4SLinus Torvalds int err; 28781da177e4SLinus Torvalds 2879af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 28801da177e4SLinus Torvalds return -EPERM; 28811da177e4SLinus Torvalds 28821da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 28831da177e4SLinus Torvalds return -EFAULT; 28841da177e4SLinus Torvalds 28851da177e4SLinus Torvalds rtnl_lock(); 2886caeaba79SNicolas Dichtel err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL, 2887af284937SDaniel Lezcano ireq.ifr6_prefixlen, IFA_F_PERMANENT, 2888af284937SDaniel Lezcano INFINITY_LIFE_TIME, INFINITY_LIFE_TIME); 28891da177e4SLinus Torvalds rtnl_unlock(); 28901da177e4SLinus Torvalds return err; 28911da177e4SLinus Torvalds } 28921da177e4SLinus Torvalds 2893af284937SDaniel Lezcano int addrconf_del_ifaddr(struct net *net, void __user *arg) 28941da177e4SLinus Torvalds { 28951da177e4SLinus Torvalds struct in6_ifreq ireq; 28961da177e4SLinus Torvalds int err; 28971da177e4SLinus Torvalds 2898af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 28991da177e4SLinus Torvalds return -EPERM; 29001da177e4SLinus Torvalds 29011da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 29021da177e4SLinus Torvalds return -EFAULT; 29031da177e4SLinus Torvalds 29041da177e4SLinus Torvalds rtnl_lock(); 29056046d5b4SHeiner Kallweit err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr, 2906af284937SDaniel Lezcano ireq.ifr6_prefixlen); 29071da177e4SLinus Torvalds rtnl_unlock(); 29081da177e4SLinus Torvalds return err; 29091da177e4SLinus Torvalds } 29101da177e4SLinus Torvalds 2911b5f348e5SIlpo Järvinen static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr, 2912b5f348e5SIlpo Järvinen int plen, int scope) 2913b5f348e5SIlpo Järvinen { 2914b5f348e5SIlpo Järvinen struct inet6_ifaddr *ifp; 2915b5f348e5SIlpo Järvinen 29168a226b2cSJiri Benc ifp = ipv6_add_addr(idev, addr, NULL, plen, 291707edd741SHannes Frederic Sowa scope, IFA_F_PERMANENT, 291807edd741SHannes Frederic Sowa INFINITY_LIFE_TIME, INFINITY_LIFE_TIME); 2919b5f348e5SIlpo Järvinen if (!IS_ERR(ifp)) { 2920b5f348e5SIlpo Järvinen spin_lock_bh(&ifp->lock); 2921b5f348e5SIlpo Järvinen ifp->flags &= ~IFA_F_TENTATIVE; 2922b5f348e5SIlpo Järvinen spin_unlock_bh(&ifp->lock); 2923764d3be6SPaolo Abeni rt_genid_bump_ipv6(dev_net(idev->dev)); 2924b5f348e5SIlpo Järvinen ipv6_ifa_notify(RTM_NEWADDR, ifp); 2925b5f348e5SIlpo Järvinen in6_ifa_put(ifp); 2926b5f348e5SIlpo Järvinen } 2927b5f348e5SIlpo Järvinen } 2928b5f348e5SIlpo Järvinen 292907a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 29301da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev) 29311da177e4SLinus Torvalds { 29321da177e4SLinus Torvalds struct in6_addr addr; 29331da177e4SLinus Torvalds struct net_device *dev; 2934c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 2935929c9cf3SNicolas Dichtel int scope, plen; 2936f0e2acfaSNicolas Dichtel u32 pflags = 0; 29371da177e4SLinus Torvalds 29381da177e4SLinus Torvalds ASSERT_RTNL(); 29391da177e4SLinus Torvalds 29401da177e4SLinus Torvalds memset(&addr, 0, sizeof(struct in6_addr)); 29411da177e4SLinus Torvalds memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4); 29421da177e4SLinus Torvalds 29431da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) { 29441da177e4SLinus Torvalds addr.s6_addr32[0] = htonl(0xfe800000); 29451da177e4SLinus Torvalds scope = IFA_LINK; 2946929c9cf3SNicolas Dichtel plen = 64; 29471da177e4SLinus Torvalds } else { 29481da177e4SLinus Torvalds scope = IPV6_ADDR_COMPATv4; 2949929c9cf3SNicolas Dichtel plen = 96; 2950f0e2acfaSNicolas Dichtel pflags |= RTF_NONEXTHOP; 29511da177e4SLinus Torvalds } 29521da177e4SLinus Torvalds 29531da177e4SLinus Torvalds if (addr.s6_addr32[3]) { 2954929c9cf3SNicolas Dichtel add_addr(idev, &addr, plen, scope); 2955f0e2acfaSNicolas Dichtel addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags); 29561da177e4SLinus Torvalds return; 29571da177e4SLinus Torvalds } 29581da177e4SLinus Torvalds 29596fda7350SBenjamin Thery for_each_netdev(net, dev) { 2960e5ed6399SHerbert Xu struct in_device *in_dev = __in_dev_get_rtnl(dev); 29611da177e4SLinus Torvalds if (in_dev && (dev->flags & IFF_UP)) { 29621da177e4SLinus Torvalds struct in_ifaddr *ifa; 29631da177e4SLinus Torvalds 29641da177e4SLinus Torvalds int flag = scope; 29651da177e4SLinus Torvalds 29661da177e4SLinus Torvalds for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) { 29671da177e4SLinus Torvalds 29681da177e4SLinus Torvalds addr.s6_addr32[3] = ifa->ifa_local; 29691da177e4SLinus Torvalds 29701da177e4SLinus Torvalds if (ifa->ifa_scope == RT_SCOPE_LINK) 29711da177e4SLinus Torvalds continue; 29721da177e4SLinus Torvalds if (ifa->ifa_scope >= RT_SCOPE_HOST) { 29731da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) 29741da177e4SLinus Torvalds continue; 29751da177e4SLinus Torvalds flag |= IFA_HOST; 29761da177e4SLinus Torvalds } 29771da177e4SLinus Torvalds 2978b5f348e5SIlpo Järvinen add_addr(idev, &addr, plen, flag); 2979f0e2acfaSNicolas Dichtel addrconf_prefix_route(&addr, plen, idev->dev, 0, 2980f0e2acfaSNicolas Dichtel pflags); 29811da177e4SLinus Torvalds } 29821da177e4SLinus Torvalds } 29831da177e4SLinus Torvalds } 29841da177e4SLinus Torvalds } 29850be669bbSJoerg Roedel #endif 29861da177e4SLinus Torvalds 29871da177e4SLinus Torvalds static void init_loopback(struct net_device *dev) 29881da177e4SLinus Torvalds { 29891da177e4SLinus Torvalds struct inet6_dev *idev; 299025fb6ca4SBalakumaran Kannan struct net_device *sp_dev; 299125fb6ca4SBalakumaran Kannan struct inet6_ifaddr *sp_ifa; 299225fb6ca4SBalakumaran Kannan struct rt6_info *sp_rt; 29931da177e4SLinus Torvalds 29941da177e4SLinus Torvalds /* ::1 */ 29951da177e4SLinus Torvalds 29961da177e4SLinus Torvalds ASSERT_RTNL(); 29971da177e4SLinus Torvalds 2998e5d08d71SIan Morris idev = ipv6_find_idev(dev); 299963159f29SIan Morris if (!idev) { 300091df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 30011da177e4SLinus Torvalds return; 30021da177e4SLinus Torvalds } 30031da177e4SLinus Torvalds 3004b5f348e5SIlpo Järvinen add_addr(idev, &in6addr_loopback, 128, IFA_HOST); 300525fb6ca4SBalakumaran Kannan 300625fb6ca4SBalakumaran Kannan /* Add routes to other interface's IPv6 addresses */ 300725fb6ca4SBalakumaran Kannan for_each_netdev(dev_net(dev), sp_dev) { 300825fb6ca4SBalakumaran Kannan if (!strcmp(sp_dev->name, dev->name)) 300925fb6ca4SBalakumaran Kannan continue; 301025fb6ca4SBalakumaran Kannan 301125fb6ca4SBalakumaran Kannan idev = __in6_dev_get(sp_dev); 301225fb6ca4SBalakumaran Kannan if (!idev) 301325fb6ca4SBalakumaran Kannan continue; 301425fb6ca4SBalakumaran Kannan 301525fb6ca4SBalakumaran Kannan read_lock_bh(&idev->lock); 301625fb6ca4SBalakumaran Kannan list_for_each_entry(sp_ifa, &idev->addr_list, if_list) { 301725fb6ca4SBalakumaran Kannan 301825fb6ca4SBalakumaran Kannan if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE)) 301925fb6ca4SBalakumaran Kannan continue; 302025fb6ca4SBalakumaran Kannan 302133d99113SGao feng if (sp_ifa->rt) { 302233d99113SGao feng /* This dst has been added to garbage list when 302333d99113SGao feng * lo device down, release this obsolete dst and 302433d99113SGao feng * reallocate a new router for ifa. 302533d99113SGao feng */ 3026a220445fSNicolas Dichtel if (!atomic_read(&sp_ifa->rt->rt6i_ref)) { 302733d99113SGao feng ip6_rt_put(sp_ifa->rt); 302833d99113SGao feng sp_ifa->rt = NULL; 302933d99113SGao feng } else { 3030a881ae1fSGao feng continue; 303133d99113SGao feng } 303233d99113SGao feng } 3033a881ae1fSGao feng 303457ec0afeSFrançois-Xavier Le Bail sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, false); 303525fb6ca4SBalakumaran Kannan 303625fb6ca4SBalakumaran Kannan /* Failure cases are ignored */ 3037534c8779SGao feng if (!IS_ERR(sp_rt)) { 3038534c8779SGao feng sp_ifa->rt = sp_rt; 303925fb6ca4SBalakumaran Kannan ip6_ins_rt(sp_rt); 304025fb6ca4SBalakumaran Kannan } 3041534c8779SGao feng } 304225fb6ca4SBalakumaran Kannan read_unlock_bh(&idev->lock); 304325fb6ca4SBalakumaran Kannan } 30441da177e4SLinus Torvalds } 30451da177e4SLinus Torvalds 30462ad3ed59SAlexander Aring void addrconf_add_linklocal(struct inet6_dev *idev, 304764236f3fSHannes Frederic Sowa const struct in6_addr *addr, u32 flags) 30481da177e4SLinus Torvalds { 30491da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 305064236f3fSHannes Frederic Sowa u32 addr_flags = flags | IFA_F_PERMANENT; 30511da177e4SLinus Torvalds 305295c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 305395c385b4SNeil Horman if (idev->cnf.optimistic_dad && 3054702beb87SDavid Miller !dev_net(idev->dev)->ipv6.devconf_all->forwarding) 305595c385b4SNeil Horman addr_flags |= IFA_F_OPTIMISTIC; 305695c385b4SNeil Horman #endif 305795c385b4SNeil Horman 305807edd741SHannes Frederic Sowa ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags, 305907edd741SHannes Frederic Sowa INFINITY_LIFE_TIME, INFINITY_LIFE_TIME); 30601da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 306146d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0); 3062cf22f9a2SDavid S. Miller addrconf_dad_start(ifp); 30631da177e4SLinus Torvalds in6_ifa_put(ifp); 30641da177e4SLinus Torvalds } 30651da177e4SLinus Torvalds } 30662ad3ed59SAlexander Aring EXPORT_SYMBOL_GPL(addrconf_add_linklocal); 30671da177e4SLinus Torvalds 3068622c81d5SHannes Frederic Sowa static bool ipv6_reserved_interfaceid(struct in6_addr address) 3069622c81d5SHannes Frederic Sowa { 3070622c81d5SHannes Frederic Sowa if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0) 3071622c81d5SHannes Frederic Sowa return true; 3072622c81d5SHannes Frederic Sowa 3073622c81d5SHannes Frederic Sowa if (address.s6_addr32[2] == htonl(0x02005eff) && 3074622c81d5SHannes Frederic Sowa ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000))) 3075622c81d5SHannes Frederic Sowa return true; 3076622c81d5SHannes Frederic Sowa 3077622c81d5SHannes Frederic Sowa if (address.s6_addr32[2] == htonl(0xfdffffff) && 3078622c81d5SHannes Frederic Sowa ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80))) 3079622c81d5SHannes Frederic Sowa return true; 3080622c81d5SHannes Frederic Sowa 3081622c81d5SHannes Frederic Sowa return false; 3082622c81d5SHannes Frederic Sowa } 3083622c81d5SHannes Frederic Sowa 3084622c81d5SHannes Frederic Sowa static int ipv6_generate_stable_address(struct in6_addr *address, 3085622c81d5SHannes Frederic Sowa u8 dad_count, 3086622c81d5SHannes Frederic Sowa const struct inet6_dev *idev) 3087622c81d5SHannes Frederic Sowa { 3088622c81d5SHannes Frederic Sowa static DEFINE_SPINLOCK(lock); 3089622c81d5SHannes Frederic Sowa static __u32 digest[SHA_DIGEST_WORDS]; 3090622c81d5SHannes Frederic Sowa static __u32 workspace[SHA_WORKSPACE_WORDS]; 3091622c81d5SHannes Frederic Sowa 3092622c81d5SHannes Frederic Sowa static union { 3093622c81d5SHannes Frederic Sowa char __data[SHA_MESSAGE_BYTES]; 3094622c81d5SHannes Frederic Sowa struct { 3095622c81d5SHannes Frederic Sowa struct in6_addr secret; 3096ff40217eSHannes Frederic Sowa __be32 prefix[2]; 3097622c81d5SHannes Frederic Sowa unsigned char hwaddr[MAX_ADDR_LEN]; 3098622c81d5SHannes Frederic Sowa u8 dad_count; 3099622c81d5SHannes Frederic Sowa } __packed; 3100622c81d5SHannes Frederic Sowa } data; 3101622c81d5SHannes Frederic Sowa 3102622c81d5SHannes Frederic Sowa struct in6_addr secret; 3103622c81d5SHannes Frederic Sowa struct in6_addr temp; 3104622c81d5SHannes Frederic Sowa struct net *net = dev_net(idev->dev); 3105622c81d5SHannes Frederic Sowa 3106622c81d5SHannes Frederic Sowa BUILD_BUG_ON(sizeof(data.__data) != sizeof(data)); 3107622c81d5SHannes Frederic Sowa 3108622c81d5SHannes Frederic Sowa if (idev->cnf.stable_secret.initialized) 3109622c81d5SHannes Frederic Sowa secret = idev->cnf.stable_secret.secret; 3110622c81d5SHannes Frederic Sowa else if (net->ipv6.devconf_dflt->stable_secret.initialized) 3111622c81d5SHannes Frederic Sowa secret = net->ipv6.devconf_dflt->stable_secret.secret; 3112622c81d5SHannes Frederic Sowa else 3113622c81d5SHannes Frederic Sowa return -1; 3114622c81d5SHannes Frederic Sowa 3115622c81d5SHannes Frederic Sowa retry: 3116622c81d5SHannes Frederic Sowa spin_lock_bh(&lock); 3117622c81d5SHannes Frederic Sowa 3118622c81d5SHannes Frederic Sowa sha_init(digest); 3119622c81d5SHannes Frederic Sowa memset(&data, 0, sizeof(data)); 3120622c81d5SHannes Frederic Sowa memset(workspace, 0, sizeof(workspace)); 3121622c81d5SHannes Frederic Sowa memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len); 3122ff40217eSHannes Frederic Sowa data.prefix[0] = address->s6_addr32[0]; 3123ff40217eSHannes Frederic Sowa data.prefix[1] = address->s6_addr32[1]; 3124622c81d5SHannes Frederic Sowa data.secret = secret; 3125622c81d5SHannes Frederic Sowa data.dad_count = dad_count; 3126622c81d5SHannes Frederic Sowa 3127622c81d5SHannes Frederic Sowa sha_transform(digest, data.__data, workspace); 3128622c81d5SHannes Frederic Sowa 3129622c81d5SHannes Frederic Sowa temp = *address; 3130ff40217eSHannes Frederic Sowa temp.s6_addr32[2] = (__force __be32)digest[0]; 3131ff40217eSHannes Frederic Sowa temp.s6_addr32[3] = (__force __be32)digest[1]; 3132622c81d5SHannes Frederic Sowa 3133622c81d5SHannes Frederic Sowa spin_unlock_bh(&lock); 3134622c81d5SHannes Frederic Sowa 3135622c81d5SHannes Frederic Sowa if (ipv6_reserved_interfaceid(temp)) { 3136622c81d5SHannes Frederic Sowa dad_count++; 31371855b7c3SHannes Frederic Sowa if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries) 3138622c81d5SHannes Frederic Sowa return -1; 3139622c81d5SHannes Frederic Sowa goto retry; 3140622c81d5SHannes Frederic Sowa } 3141622c81d5SHannes Frederic Sowa 3142622c81d5SHannes Frederic Sowa *address = temp; 3143622c81d5SHannes Frederic Sowa return 0; 3144622c81d5SHannes Frederic Sowa } 3145622c81d5SHannes Frederic Sowa 3146cc9da6ccSBjørn Mork static void ipv6_gen_mode_random_init(struct inet6_dev *idev) 3147cc9da6ccSBjørn Mork { 3148cc9da6ccSBjørn Mork struct ipv6_stable_secret *s = &idev->cnf.stable_secret; 3149cc9da6ccSBjørn Mork 3150cc9da6ccSBjørn Mork if (s->initialized) 3151cc9da6ccSBjørn Mork return; 3152cc9da6ccSBjørn Mork s = &idev->cnf.stable_secret; 3153cc9da6ccSBjørn Mork get_random_bytes(&s->secret, sizeof(s->secret)); 3154cc9da6ccSBjørn Mork s->initialized = true; 3155cc9da6ccSBjørn Mork } 3156cc9da6ccSBjørn Mork 3157bc91b0f0SJiri Pirko static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route) 3158bc91b0f0SJiri Pirko { 3159bc91b0f0SJiri Pirko struct in6_addr addr; 3160bc91b0f0SJiri Pirko 3161ca254490SDavid Ahern /* no link local addresses on L3 master devices */ 3162ca254490SDavid Ahern if (netif_is_l3_master(idev->dev)) 3163ca254490SDavid Ahern return; 3164ca254490SDavid Ahern 3165bc91b0f0SJiri Pirko ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0); 3166622c81d5SHannes Frederic Sowa 3167d35a00b8SFelix Jia switch (idev->cnf.addr_gen_mode) { 3168cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_RANDOM: 3169cc9da6ccSBjørn Mork ipv6_gen_mode_random_init(idev); 3170cc9da6ccSBjørn Mork /* fallthrough */ 3171cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_STABLE_PRIVACY: 3172622c81d5SHannes Frederic Sowa if (!ipv6_generate_stable_address(&addr, 0, idev)) 317364236f3fSHannes Frederic Sowa addrconf_add_linklocal(idev, &addr, 317464236f3fSHannes Frederic Sowa IFA_F_STABLE_PRIVACY); 3175622c81d5SHannes Frederic Sowa else if (prefix_route) 3176622c81d5SHannes Frederic Sowa addrconf_prefix_route(&addr, 64, idev->dev, 0, 0); 3177cc9da6ccSBjørn Mork break; 3178cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_EUI64: 3179bc91b0f0SJiri Pirko /* addrconf_add_linklocal also adds a prefix_route and we 3180bc91b0f0SJiri Pirko * only need to care about prefix routes if ipv6_generate_eui64 3181bc91b0f0SJiri Pirko * couldn't generate one. 3182bc91b0f0SJiri Pirko */ 3183bc91b0f0SJiri Pirko if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0) 318464236f3fSHannes Frederic Sowa addrconf_add_linklocal(idev, &addr, 0); 3185bc91b0f0SJiri Pirko else if (prefix_route) 3186bc91b0f0SJiri Pirko addrconf_prefix_route(&addr, 64, idev->dev, 0, 0); 3187cc9da6ccSBjørn Mork break; 3188cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_NONE: 3189cc9da6ccSBjørn Mork default: 3190cc9da6ccSBjørn Mork /* will not add any link local address */ 3191cc9da6ccSBjørn Mork break; 3192bc91b0f0SJiri Pirko } 3193bc91b0f0SJiri Pirko } 3194bc91b0f0SJiri Pirko 31951da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev) 31961da177e4SLinus Torvalds { 31971da177e4SLinus Torvalds struct inet6_dev *idev; 31981da177e4SLinus Torvalds 31991da177e4SLinus Torvalds ASSERT_RTNL(); 32001da177e4SLinus Torvalds 320174235a25SHerbert Xu if ((dev->type != ARPHRD_ETHER) && 320274235a25SHerbert Xu (dev->type != ARPHRD_FDDI) && 320374235a25SHerbert Xu (dev->type != ARPHRD_ARCNET) && 320406a4c1c5Salex.bluesman.smirnov@gmail.com (dev->type != ARPHRD_INFINIBAND) && 3205e837735eSNicolas Dichtel (dev->type != ARPHRD_IEEE1394) && 3206e74bccb8SJukka Rissanen (dev->type != ARPHRD_TUNNEL6) && 3207cc9da6ccSBjørn Mork (dev->type != ARPHRD_6LOWPAN) && 320845ce0fd1SFelix Jia (dev->type != ARPHRD_IP6GRE) && 320945ce0fd1SFelix Jia (dev->type != ARPHRD_IPGRE) && 321045ce0fd1SFelix Jia (dev->type != ARPHRD_TUNNEL) && 3211cc9da6ccSBjørn Mork (dev->type != ARPHRD_NONE)) { 321274235a25SHerbert Xu /* Alas, we support only Ethernet autoconfiguration. */ 321374235a25SHerbert Xu return; 321474235a25SHerbert Xu } 321574235a25SHerbert Xu 32161da177e4SLinus Torvalds idev = addrconf_add_dev(dev); 321764e724f6SBrian Haley if (IS_ERR(idev)) 32181da177e4SLinus Torvalds return; 32191da177e4SLinus Torvalds 3220cc9da6ccSBjørn Mork /* this device type has no EUI support */ 3221cc9da6ccSBjørn Mork if (dev->type == ARPHRD_NONE && 3222d35a00b8SFelix Jia idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64) 3223d35a00b8SFelix Jia idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM; 3224cc9da6ccSBjørn Mork 3225bc91b0f0SJiri Pirko addrconf_addr_gen(idev, false); 32261da177e4SLinus Torvalds } 32271da177e4SLinus Torvalds 322807a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 32291da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev) 32301da177e4SLinus Torvalds { 32311da177e4SLinus Torvalds struct inet6_dev *idev; 32321da177e4SLinus Torvalds 32331da177e4SLinus Torvalds ASSERT_RTNL(); 32341da177e4SLinus Torvalds 32351da177e4SLinus Torvalds /* 32361da177e4SLinus Torvalds * Configure the tunnel with one of our IPv4 32371da177e4SLinus Torvalds * addresses... we should configure all of 32381da177e4SLinus Torvalds * our v4 addrs in the tunnel 32391da177e4SLinus Torvalds */ 32401da177e4SLinus Torvalds 3241e5d08d71SIan Morris idev = ipv6_find_idev(dev); 324263159f29SIan Morris if (!idev) { 324391df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 32441da177e4SLinus Torvalds return; 32451da177e4SLinus Torvalds } 32461da177e4SLinus Torvalds 3247c7dc89c0SFred L. Templin if (dev->priv_flags & IFF_ISATAP) { 3248bc91b0f0SJiri Pirko addrconf_addr_gen(idev, false); 3249c7dc89c0SFred L. Templin return; 3250c7dc89c0SFred L. Templin } 3251c7dc89c0SFred L. Templin 32521da177e4SLinus Torvalds sit_add_v4_addrs(idev); 32531da177e4SLinus Torvalds 325462b54dd9SNicolas Dichtel if (dev->flags&IFF_POINTOPOINT) 32551da177e4SLinus Torvalds addrconf_add_mroute(dev); 32561da177e4SLinus Torvalds } 32570be669bbSJoerg Roedel #endif 32581da177e4SLinus Torvalds 325907a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE) 3260aee80b54Sstephen hemminger static void addrconf_gre_config(struct net_device *dev) 3261aee80b54Sstephen hemminger { 3262aee80b54Sstephen hemminger struct inet6_dev *idev; 3263aee80b54Sstephen hemminger 3264aee80b54Sstephen hemminger ASSERT_RTNL(); 3265aee80b54Sstephen hemminger 3266e5d08d71SIan Morris idev = ipv6_find_idev(dev); 326763159f29SIan Morris if (!idev) { 326891df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 3269aee80b54Sstephen hemminger return; 3270aee80b54Sstephen hemminger } 3271aee80b54Sstephen hemminger 3272bc91b0f0SJiri Pirko addrconf_addr_gen(idev, true); 3273d9e4ce65SHannes Frederic Sowa if (dev->flags & IFF_POINTOPOINT) 3274d9e4ce65SHannes Frederic Sowa addrconf_add_mroute(dev); 3275aee80b54Sstephen hemminger } 3276aee80b54Sstephen hemminger #endif 3277aee80b54Sstephen hemminger 3278f1705ec1SDavid Ahern static int fixup_permanent_addr(struct inet6_dev *idev, 3279f1705ec1SDavid Ahern struct inet6_ifaddr *ifp) 3280f1705ec1SDavid Ahern { 3281f1705ec1SDavid Ahern if (!ifp->rt) { 3282f1705ec1SDavid Ahern struct rt6_info *rt; 3283f1705ec1SDavid Ahern 3284f1705ec1SDavid Ahern rt = addrconf_dst_alloc(idev, &ifp->addr, false); 3285f1705ec1SDavid Ahern if (unlikely(IS_ERR(rt))) 3286f1705ec1SDavid Ahern return PTR_ERR(rt); 3287f1705ec1SDavid Ahern 3288f1705ec1SDavid Ahern ifp->rt = rt; 3289f1705ec1SDavid Ahern } 3290f1705ec1SDavid Ahern 3291f1705ec1SDavid Ahern if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) { 3292f1705ec1SDavid Ahern addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 3293f1705ec1SDavid Ahern idev->dev, 0, 0); 3294f1705ec1SDavid Ahern } 3295f1705ec1SDavid Ahern 3296f1705ec1SDavid Ahern addrconf_dad_start(ifp); 3297f1705ec1SDavid Ahern 3298f1705ec1SDavid Ahern return 0; 3299f1705ec1SDavid Ahern } 3300f1705ec1SDavid Ahern 3301f1705ec1SDavid Ahern static void addrconf_permanent_addr(struct net_device *dev) 3302f1705ec1SDavid Ahern { 3303f1705ec1SDavid Ahern struct inet6_ifaddr *ifp, *tmp; 3304f1705ec1SDavid Ahern struct inet6_dev *idev; 3305f1705ec1SDavid Ahern 3306f1705ec1SDavid Ahern idev = __in6_dev_get(dev); 3307f1705ec1SDavid Ahern if (!idev) 3308f1705ec1SDavid Ahern return; 3309f1705ec1SDavid Ahern 3310f1705ec1SDavid Ahern write_lock_bh(&idev->lock); 3311f1705ec1SDavid Ahern 3312f1705ec1SDavid Ahern list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) { 3313f1705ec1SDavid Ahern if ((ifp->flags & IFA_F_PERMANENT) && 3314f1705ec1SDavid Ahern fixup_permanent_addr(idev, ifp) < 0) { 3315f1705ec1SDavid Ahern write_unlock_bh(&idev->lock); 3316f1705ec1SDavid Ahern ipv6_del_addr(ifp); 3317f1705ec1SDavid Ahern write_lock_bh(&idev->lock); 3318f1705ec1SDavid Ahern 3319f1705ec1SDavid Ahern net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n", 3320f1705ec1SDavid Ahern idev->dev->name, &ifp->addr); 3321f1705ec1SDavid Ahern } 3322f1705ec1SDavid Ahern } 3323f1705ec1SDavid Ahern 3324f1705ec1SDavid Ahern write_unlock_bh(&idev->lock); 3325f1705ec1SDavid Ahern } 3326f1705ec1SDavid Ahern 33271da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event, 3328351638e7SJiri Pirko void *ptr) 33291da177e4SLinus Torvalds { 3330351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 33314f7f34eaSDavid Ahern struct netdev_notifier_changeupper_info *info; 3332748e2d93SEric Dumazet struct inet6_dev *idev = __in6_dev_get(dev); 3333c5e33bddSYOSHIFUJI Hideaki int run_pending = 0; 3334b217d616SHerbert Xu int err; 33351da177e4SLinus Torvalds 33361da177e4SLinus Torvalds switch (event) { 333745ba9dd2SYOSHIFUJI Hideaki case NETDEV_REGISTER: 333874235a25SHerbert Xu if (!idev && dev->mtu >= IPV6_MIN_MTU) { 333945ba9dd2SYOSHIFUJI Hideaki idev = ipv6_add_dev(dev); 3340a317a2f1SWANG Cong if (IS_ERR(idev)) 3341a317a2f1SWANG Cong return notifier_from_errno(PTR_ERR(idev)); 334245ba9dd2SYOSHIFUJI Hideaki } 334345ba9dd2SYOSHIFUJI Hideaki break; 3344bcdd553fSStephen Hemminger 3345b7b0b1d2SAlexander Duyck case NETDEV_CHANGEMTU: 3346b7b0b1d2SAlexander Duyck /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */ 3347b7b0b1d2SAlexander Duyck if (dev->mtu < IPV6_MIN_MTU) { 3348b7b0b1d2SAlexander Duyck addrconf_ifdown(dev, 1); 3349b7b0b1d2SAlexander Duyck break; 3350b7b0b1d2SAlexander Duyck } 3351b7b0b1d2SAlexander Duyck 3352b7b0b1d2SAlexander Duyck if (idev) { 3353b7b0b1d2SAlexander Duyck rt6_mtu_change(dev, dev->mtu); 3354b7b0b1d2SAlexander Duyck idev->cnf.mtu6 = dev->mtu; 3355b7b0b1d2SAlexander Duyck break; 3356b7b0b1d2SAlexander Duyck } 3357b7b0b1d2SAlexander Duyck 3358b7b0b1d2SAlexander Duyck /* allocate new idev */ 3359b7b0b1d2SAlexander Duyck idev = ipv6_add_dev(dev); 3360b7b0b1d2SAlexander Duyck if (IS_ERR(idev)) 3361b7b0b1d2SAlexander Duyck break; 3362b7b0b1d2SAlexander Duyck 3363b7b0b1d2SAlexander Duyck /* device is still not ready */ 3364b7b0b1d2SAlexander Duyck if (!(idev->if_flags & IF_READY)) 3365b7b0b1d2SAlexander Duyck break; 3366b7b0b1d2SAlexander Duyck 3367b7b0b1d2SAlexander Duyck run_pending = 1; 3368b7b0b1d2SAlexander Duyck 3369b7b0b1d2SAlexander Duyck /* fall through */ 3370b7b0b1d2SAlexander Duyck 33711da177e4SLinus Torvalds case NETDEV_UP: 33723c21edbdSYOSHIFUJI Hideaki case NETDEV_CHANGE: 3373c2edacf8SJay Vosburgh if (dev->flags & IFF_SLAVE) 3374c2edacf8SJay Vosburgh break; 3375c2edacf8SJay Vosburgh 33763ce62a84SWANG Cong if (idev && idev->cnf.disable_ipv6) 33773ce62a84SWANG Cong break; 33783ce62a84SWANG Cong 33793c21edbdSYOSHIFUJI Hideaki if (event == NETDEV_UP) { 338038bd10c4SDavid Ahern /* restore routes for permanent addresses */ 338138bd10c4SDavid Ahern addrconf_permanent_addr(dev); 338238bd10c4SDavid Ahern 3383f24e3d65SMitsuru Chinen if (!addrconf_qdisc_ok(dev)) { 33843c21edbdSYOSHIFUJI Hideaki /* device is not ready yet. */ 3385f3213831SJoe Perches pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n", 33863c21edbdSYOSHIFUJI Hideaki dev->name); 33873c21edbdSYOSHIFUJI Hideaki break; 33883c21edbdSYOSHIFUJI Hideaki } 338999081049SKristian Slavov 3390d31c7b8fSEvgeniy Polyakov if (!idev && dev->mtu >= IPV6_MIN_MTU) 3391d31c7b8fSEvgeniy Polyakov idev = ipv6_add_dev(dev); 3392d31c7b8fSEvgeniy Polyakov 3393a317a2f1SWANG Cong if (!IS_ERR_OR_NULL(idev)) { 339499081049SKristian Slavov idev->if_flags |= IF_READY; 3395e3ec6cfcSBenjamin Thery run_pending = 1; 3396e3ec6cfcSBenjamin Thery } 3397b7b0b1d2SAlexander Duyck } else if (event == NETDEV_CHANGE) { 3398f24e3d65SMitsuru Chinen if (!addrconf_qdisc_ok(dev)) { 33993c21edbdSYOSHIFUJI Hideaki /* device is still not ready. */ 34003c21edbdSYOSHIFUJI Hideaki break; 34013c21edbdSYOSHIFUJI Hideaki } 34023c21edbdSYOSHIFUJI Hideaki 34033c21edbdSYOSHIFUJI Hideaki if (idev) { 3404a088d1d7SLinus Lüssing if (idev->if_flags & IF_READY) { 3405a088d1d7SLinus Lüssing /* device is already configured - 3406a088d1d7SLinus Lüssing * but resend MLD reports, we might 3407a088d1d7SLinus Lüssing * have roamed and need to update 3408a088d1d7SLinus Lüssing * multicast snooping switches 3409a088d1d7SLinus Lüssing */ 3410a088d1d7SLinus Lüssing ipv6_mc_up(idev); 34113c21edbdSYOSHIFUJI Hideaki break; 3412a088d1d7SLinus Lüssing } 34133c21edbdSYOSHIFUJI Hideaki idev->if_flags |= IF_READY; 34143c21edbdSYOSHIFUJI Hideaki } 34153c21edbdSYOSHIFUJI Hideaki 3416f3213831SJoe Perches pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n", 34173c21edbdSYOSHIFUJI Hideaki dev->name); 34183c21edbdSYOSHIFUJI Hideaki 3419c5e33bddSYOSHIFUJI Hideaki run_pending = 1; 34203c21edbdSYOSHIFUJI Hideaki } 34213c21edbdSYOSHIFUJI Hideaki 34221da177e4SLinus Torvalds switch (dev->type) { 342307a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 34241da177e4SLinus Torvalds case ARPHRD_SIT: 34251da177e4SLinus Torvalds addrconf_sit_config(dev); 34261da177e4SLinus Torvalds break; 34270be669bbSJoerg Roedel #endif 342807a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE) 3429aee80b54Sstephen hemminger case ARPHRD_IPGRE: 3430aee80b54Sstephen hemminger addrconf_gre_config(dev); 3431aee80b54Sstephen hemminger break; 3432aee80b54Sstephen hemminger #endif 34331da177e4SLinus Torvalds case ARPHRD_LOOPBACK: 34341da177e4SLinus Torvalds init_loopback(dev); 34351da177e4SLinus Torvalds break; 34361da177e4SLinus Torvalds 34371da177e4SLinus Torvalds default: 34381da177e4SLinus Torvalds addrconf_dev_config(dev); 34391da177e4SLinus Torvalds break; 34403ff50b79SStephen Hemminger } 3441bcdd553fSStephen Hemminger 3442a317a2f1SWANG Cong if (!IS_ERR_OR_NULL(idev)) { 3443c5e33bddSYOSHIFUJI Hideaki if (run_pending) 3444c5e33bddSYOSHIFUJI Hideaki addrconf_dad_run(idev); 3445c5e33bddSYOSHIFUJI Hideaki 3446bcdd553fSStephen Hemminger /* 3447bcdd553fSStephen Hemminger * If the MTU changed during the interface down, 3448bcdd553fSStephen Hemminger * when the interface up, the changed MTU must be 3449bcdd553fSStephen Hemminger * reflected in the idev as well as routers. 34501da177e4SLinus Torvalds */ 3451bcdd553fSStephen Hemminger if (idev->cnf.mtu6 != dev->mtu && 3452bcdd553fSStephen Hemminger dev->mtu >= IPV6_MIN_MTU) { 34531da177e4SLinus Torvalds rt6_mtu_change(dev, dev->mtu); 34541da177e4SLinus Torvalds idev->cnf.mtu6 = dev->mtu; 34551da177e4SLinus Torvalds } 34561da177e4SLinus Torvalds idev->tstamp = jiffies; 34571da177e4SLinus Torvalds inet6_ifinfo_notify(RTM_NEWLINK, idev); 3458bcdd553fSStephen Hemminger 3459bcdd553fSStephen Hemminger /* 3460bcdd553fSStephen Hemminger * If the changed mtu during down is lower than 3461bcdd553fSStephen Hemminger * IPV6_MIN_MTU stop IPv6 on this interface. 34621da177e4SLinus Torvalds */ 34631da177e4SLinus Torvalds if (dev->mtu < IPV6_MIN_MTU) 3464bcdd553fSStephen Hemminger addrconf_ifdown(dev, 1); 34651da177e4SLinus Torvalds } 34661da177e4SLinus Torvalds break; 34671da177e4SLinus Torvalds 34681da177e4SLinus Torvalds case NETDEV_DOWN: 34691da177e4SLinus Torvalds case NETDEV_UNREGISTER: 34701da177e4SLinus Torvalds /* 34711da177e4SLinus Torvalds * Remove all addresses from this interface. 34721da177e4SLinus Torvalds */ 34731da177e4SLinus Torvalds addrconf_ifdown(dev, event != NETDEV_DOWN); 34741da177e4SLinus Torvalds break; 34753c21edbdSYOSHIFUJI Hideaki 34761da177e4SLinus Torvalds case NETDEV_CHANGENAME: 34771da177e4SLinus Torvalds if (idev) { 34785632c515SStephen Hemminger snmp6_unregister_dev(idev); 3479408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 3480a317a2f1SWANG Cong err = addrconf_sysctl_register(idev); 3481b217d616SHerbert Xu if (err) 3482b217d616SHerbert Xu return notifier_from_errno(err); 3483a317a2f1SWANG Cong err = snmp6_register_dev(idev); 3484a317a2f1SWANG Cong if (err) { 3485a317a2f1SWANG Cong addrconf_sysctl_unregister(idev); 3486a317a2f1SWANG Cong return notifier_from_errno(err); 3487a317a2f1SWANG Cong } 34885632c515SStephen Hemminger } 34891da177e4SLinus Torvalds break; 3490bcdd553fSStephen Hemminger 349193d9b7d7SJiri Pirko case NETDEV_PRE_TYPE_CHANGE: 349293d9b7d7SJiri Pirko case NETDEV_POST_TYPE_CHANGE: 34933ef0952cSAndrew Lunn if (idev) 349493d9b7d7SJiri Pirko addrconf_type_change(dev, event); 349575c78500SMoni Shoua break; 34964f7f34eaSDavid Ahern 34974f7f34eaSDavid Ahern case NETDEV_CHANGEUPPER: 34984f7f34eaSDavid Ahern info = ptr; 34994f7f34eaSDavid Ahern 35004f7f34eaSDavid Ahern /* flush all routes if dev is linked to or unlinked from 35014f7f34eaSDavid Ahern * an L3 master device (e.g., VRF) 35024f7f34eaSDavid Ahern */ 35034f7f34eaSDavid Ahern if (info->upper_dev && netif_is_l3_master(info->upper_dev)) 35044f7f34eaSDavid Ahern addrconf_ifdown(dev, 0); 35053ff50b79SStephen Hemminger } 35061da177e4SLinus Torvalds 35071da177e4SLinus Torvalds return NOTIFY_OK; 35081da177e4SLinus Torvalds } 35091da177e4SLinus Torvalds 35101da177e4SLinus Torvalds /* 35111da177e4SLinus Torvalds * addrconf module should be notified of a device going up 35121da177e4SLinus Torvalds */ 35131da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = { 35141da177e4SLinus Torvalds .notifier_call = addrconf_notify, 35151da177e4SLinus Torvalds }; 35161da177e4SLinus Torvalds 351793d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, unsigned long event) 351875c78500SMoni Shoua { 351975c78500SMoni Shoua struct inet6_dev *idev; 352075c78500SMoni Shoua ASSERT_RTNL(); 352175c78500SMoni Shoua 352275c78500SMoni Shoua idev = __in6_dev_get(dev); 352375c78500SMoni Shoua 352493d9b7d7SJiri Pirko if (event == NETDEV_POST_TYPE_CHANGE) 352575c78500SMoni Shoua ipv6_mc_remap(idev); 352693d9b7d7SJiri Pirko else if (event == NETDEV_PRE_TYPE_CHANGE) 352775c78500SMoni Shoua ipv6_mc_unmap(idev); 352875c78500SMoni Shoua } 352975c78500SMoni Shoua 353070af921dSDavid Ahern static bool addr_is_local(const struct in6_addr *addr) 353170af921dSDavid Ahern { 353270af921dSDavid Ahern return ipv6_addr_type(addr) & 353370af921dSDavid Ahern (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK); 353470af921dSDavid Ahern } 353570af921dSDavid Ahern 35361da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how) 35371da177e4SLinus Torvalds { 3538c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 3539502a2ffdSstephen hemminger struct inet6_dev *idev; 3540f1705ec1SDavid Ahern struct inet6_ifaddr *ifa, *tmp; 3541f1705ec1SDavid Ahern struct list_head del_list; 3542f1705ec1SDavid Ahern int _keep_addr; 3543f1705ec1SDavid Ahern bool keep_addr; 354473a8bd74SDavid S. Miller int state, i; 35451da177e4SLinus Torvalds 35461da177e4SLinus Torvalds ASSERT_RTNL(); 35471da177e4SLinus Torvalds 3548f3db4851SDaniel Lezcano rt6_ifdown(net, dev); 354973a8bd74SDavid S. Miller neigh_ifdown(&nd_tbl, dev); 35501da177e4SLinus Torvalds 35511da177e4SLinus Torvalds idev = __in6_dev_get(dev); 355263159f29SIan Morris if (!idev) 35531da177e4SLinus Torvalds return -ENODEV; 35541da177e4SLinus Torvalds 3555bcdd553fSStephen Hemminger /* 3556bcdd553fSStephen Hemminger * Step 1: remove reference to ipv6 device from parent device. 3557bcdd553fSStephen Hemminger * Do not dev_put! 35581da177e4SLinus Torvalds */ 3559439e2385SDenis V. Lunev if (how) { 35601da177e4SLinus Torvalds idev->dead = 1; 35618814c4b5SYOSHIFUJI Hideaki 35628814c4b5SYOSHIFUJI Hideaki /* protected by rtnl_lock */ 3563a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(dev->ip6_ptr, NULL); 35641da177e4SLinus Torvalds 35651da177e4SLinus Torvalds /* Step 1.5: remove snmp6 entry */ 35661da177e4SLinus Torvalds snmp6_unregister_dev(idev); 35671da177e4SLinus Torvalds 35681da177e4SLinus Torvalds } 35691da177e4SLinus Torvalds 3570f1705ec1SDavid Ahern /* aggregate the system setting and interface setting */ 3571f1705ec1SDavid Ahern _keep_addr = net->ipv6.devconf_all->keep_addr_on_down; 3572f1705ec1SDavid Ahern if (!_keep_addr) 3573f1705ec1SDavid Ahern _keep_addr = idev->cnf.keep_addr_on_down; 3574f1705ec1SDavid Ahern 3575f1705ec1SDavid Ahern /* combine the user config with event to determine if permanent 3576f1705ec1SDavid Ahern * addresses are to be removed from address hash table 3577f1705ec1SDavid Ahern */ 3578bc561632SMike Manning keep_addr = !(how || _keep_addr <= 0 || idev->cnf.disable_ipv6); 3579f1705ec1SDavid Ahern 358073a8bd74SDavid S. Miller /* Step 2: clear hash table */ 358173a8bd74SDavid S. Miller for (i = 0; i < IN6_ADDR_HSIZE; i++) { 358273a8bd74SDavid S. Miller struct hlist_head *h = &inet6_addr_lst[i]; 358373a8bd74SDavid S. Miller 358473a8bd74SDavid S. Miller spin_lock_bh(&addrconf_hash_lock); 358573a8bd74SDavid S. Miller restart: 3586b67bfe0dSSasha Levin hlist_for_each_entry_rcu(ifa, h, addr_lst) { 358773a8bd74SDavid S. Miller if (ifa->idev == idev) { 3588c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifa); 3589f1705ec1SDavid Ahern /* combined flag + permanent flag decide if 3590f1705ec1SDavid Ahern * address is retained on a down event 3591f1705ec1SDavid Ahern */ 3592f1705ec1SDavid Ahern if (!keep_addr || 359370af921dSDavid Ahern !(ifa->flags & IFA_F_PERMANENT) || 359470af921dSDavid Ahern addr_is_local(&ifa->addr)) { 3595f1705ec1SDavid Ahern hlist_del_init_rcu(&ifa->addr_lst); 359673a8bd74SDavid S. Miller goto restart; 359773a8bd74SDavid S. Miller } 359873a8bd74SDavid S. Miller } 3599f1705ec1SDavid Ahern } 360073a8bd74SDavid S. Miller spin_unlock_bh(&addrconf_hash_lock); 360173a8bd74SDavid S. Miller } 360273a8bd74SDavid S. Miller 36031da177e4SLinus Torvalds write_lock_bh(&idev->lock); 36041da177e4SLinus Torvalds 3605b7b1bfceSHannes Frederic Sowa addrconf_del_rs_timer(idev); 3606b7b1bfceSHannes Frederic Sowa 3607bcdd553fSStephen Hemminger /* Step 2: clear flags for stateless addrconf */ 3608439e2385SDenis V. Lunev if (!how) 36093c21edbdSYOSHIFUJI Hideaki idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY); 36101da177e4SLinus Torvalds 3611bcdd553fSStephen Hemminger /* Step 3: clear tempaddr list */ 3612372e6c8fSstephen hemminger while (!list_empty(&idev->tempaddr_list)) { 3613372e6c8fSstephen hemminger ifa = list_first_entry(&idev->tempaddr_list, 3614372e6c8fSstephen hemminger struct inet6_ifaddr, tmp_list); 3615372e6c8fSstephen hemminger list_del(&ifa->tmp_list); 36161da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 36171da177e4SLinus Torvalds spin_lock_bh(&ifa->lock); 36181da177e4SLinus Torvalds 36191da177e4SLinus Torvalds if (ifa->ifpub) { 36201da177e4SLinus Torvalds in6_ifa_put(ifa->ifpub); 36211da177e4SLinus Torvalds ifa->ifpub = NULL; 36221da177e4SLinus Torvalds } 36231da177e4SLinus Torvalds spin_unlock_bh(&ifa->lock); 36241da177e4SLinus Torvalds in6_ifa_put(ifa); 36251da177e4SLinus Torvalds write_lock_bh(&idev->lock); 36261da177e4SLinus Torvalds } 36278f37ada5Sstephen hemminger 3628f1705ec1SDavid Ahern /* re-combine the user config with event to determine if permanent 3629f1705ec1SDavid Ahern * addresses are to be removed from the interface list 3630f1705ec1SDavid Ahern */ 3631bc561632SMike Manning keep_addr = (!how && _keep_addr > 0 && !idev->cnf.disable_ipv6); 3632f1705ec1SDavid Ahern 3633f1705ec1SDavid Ahern INIT_LIST_HEAD(&del_list); 3634f1705ec1SDavid Ahern list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) { 363538bd10c4SDavid Ahern struct rt6_info *rt = NULL; 363638bd10c4SDavid Ahern 3637c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifa); 363884e8b803Sstephen hemminger 36394c5ff6a6SHerbert Xu write_unlock_bh(&idev->lock); 36408e8e676dSHannes Frederic Sowa spin_lock_bh(&ifa->lock); 3641f1705ec1SDavid Ahern 364270af921dSDavid Ahern if (keep_addr && (ifa->flags & IFA_F_PERMANENT) && 364370af921dSDavid Ahern !addr_is_local(&ifa->addr)) { 3644f1705ec1SDavid Ahern /* set state to skip the notifier below */ 3645f1705ec1SDavid Ahern state = INET6_IFADDR_STATE_DEAD; 3646f1705ec1SDavid Ahern ifa->state = 0; 3647f1705ec1SDavid Ahern if (!(ifa->flags & IFA_F_NODAD)) 3648f1705ec1SDavid Ahern ifa->flags |= IFA_F_TENTATIVE; 364938bd10c4SDavid Ahern 365038bd10c4SDavid Ahern rt = ifa->rt; 365138bd10c4SDavid Ahern ifa->rt = NULL; 3652f1705ec1SDavid Ahern } else { 36534c5ff6a6SHerbert Xu state = ifa->state; 36544c5ff6a6SHerbert Xu ifa->state = INET6_IFADDR_STATE_DEAD; 3655f1705ec1SDavid Ahern 3656c882219aSWei Yongjun list_move(&ifa->if_list, &del_list); 3657f1705ec1SDavid Ahern } 3658f1705ec1SDavid Ahern 36598e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifa->lock); 36604c5ff6a6SHerbert Xu 366138bd10c4SDavid Ahern if (rt) 366238bd10c4SDavid Ahern ip6_del_rt(rt); 366338bd10c4SDavid Ahern 36649d82ca98SJohn Fastabend if (state != INET6_IFADDR_STATE_DEAD) { 36651da177e4SLinus Torvalds __ipv6_ifa_notify(RTM_DELADDR, ifa); 3666f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_DOWN, ifa); 3667ea06f717SMike Manning } else { 3668ea06f717SMike Manning if (idev->cnf.forwarding) 3669ea06f717SMike Manning addrconf_leave_anycast(ifa); 3670ea06f717SMike Manning addrconf_leave_solict(ifa->idev, &ifa->addr); 36712de79570SLorenzo Colitti } 367273a8bd74SDavid S. Miller 36731da177e4SLinus Torvalds write_lock_bh(&idev->lock); 36741da177e4SLinus Torvalds } 36758f37ada5Sstephen hemminger 36761da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 36771da177e4SLinus Torvalds 3678f1705ec1SDavid Ahern /* now clean up addresses to be removed */ 3679f1705ec1SDavid Ahern while (!list_empty(&del_list)) { 3680f1705ec1SDavid Ahern ifa = list_first_entry(&del_list, 3681f1705ec1SDavid Ahern struct inet6_ifaddr, if_list); 3682f1705ec1SDavid Ahern list_del(&ifa->if_list); 3683f1705ec1SDavid Ahern 3684f1705ec1SDavid Ahern in6_ifa_put(ifa); 3685f1705ec1SDavid Ahern } 3686f1705ec1SDavid Ahern 3687381f4dcaSSabrina Dubroca /* Step 5: Discard anycast and multicast list */ 3688381f4dcaSSabrina Dubroca if (how) { 3689381f4dcaSSabrina Dubroca ipv6_ac_destroy_dev(idev); 36901da177e4SLinus Torvalds ipv6_mc_destroy_dev(idev); 3691381f4dcaSSabrina Dubroca } else { 36921da177e4SLinus Torvalds ipv6_mc_down(idev); 3693381f4dcaSSabrina Dubroca } 36941da177e4SLinus Torvalds 36951da177e4SLinus Torvalds idev->tstamp = jiffies; 36961da177e4SLinus Torvalds 3697bcdd553fSStephen Hemminger /* Last: Shot the device (if unregistered) */ 3698439e2385SDenis V. Lunev if (how) { 3699408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 37001da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, idev->nd_parms); 37011da177e4SLinus Torvalds neigh_ifdown(&nd_tbl, dev); 37021da177e4SLinus Torvalds in6_dev_put(idev); 37031da177e4SLinus Torvalds } 37041da177e4SLinus Torvalds return 0; 37051da177e4SLinus Torvalds } 37061da177e4SLinus Torvalds 37071da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data) 37081da177e4SLinus Torvalds { 3709b7b1bfceSHannes Frederic Sowa struct inet6_dev *idev = (struct inet6_dev *)data; 3710caf92bc4SCong Wang struct net_device *dev = idev->dev; 3711b7b1bfceSHannes Frederic Sowa struct in6_addr lladdr; 37121da177e4SLinus Torvalds 3713b7b1bfceSHannes Frederic Sowa write_lock(&idev->lock); 37145b2a1953Sstephen hemminger if (idev->dead || !(idev->if_flags & IF_READY)) 37151da177e4SLinus Torvalds goto out; 37161da177e4SLinus Torvalds 37179ba2add3SShmulik Ladkani if (!ipv6_accept_ra(idev)) 37181da177e4SLinus Torvalds goto out; 37195b2a1953Sstephen hemminger 37205b2a1953Sstephen hemminger /* Announcement received after solicitation was sent */ 37215b2a1953Sstephen hemminger if (idev->if_flags & IF_RA_RCVD) 37225b2a1953Sstephen hemminger goto out; 37231da177e4SLinus Torvalds 3724bd11f074SMaciej Żenczykowski if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) { 3725caf92bc4SCong Wang write_unlock(&idev->lock); 3726caf92bc4SCong Wang if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE)) 3727caf92bc4SCong Wang ndisc_send_rs(dev, &lladdr, 3728b7b1bfceSHannes Frederic Sowa &in6addr_linklocal_allrouters); 3729b7b1bfceSHannes Frederic Sowa else 3730caf92bc4SCong Wang goto put; 3731b7b1bfceSHannes Frederic Sowa 3732caf92bc4SCong Wang write_lock(&idev->lock); 3733bd11f074SMaciej Żenczykowski idev->rs_interval = rfc3315_s14_backoff_update( 3734bd11f074SMaciej Żenczykowski idev->rs_interval, idev->cnf.rtr_solicit_max_interval); 37351da177e4SLinus Torvalds /* The wait after the last probe can be shorter */ 3736b7b1bfceSHannes Frederic Sowa addrconf_mod_rs_timer(idev, (idev->rs_probes == 3737b7b1bfceSHannes Frederic Sowa idev->cnf.rtr_solicits) ? 37385b2a1953Sstephen hemminger idev->cnf.rtr_solicit_delay : 3739bd11f074SMaciej Żenczykowski idev->rs_interval); 37401da177e4SLinus Torvalds } else { 37411da177e4SLinus Torvalds /* 37421da177e4SLinus Torvalds * Note: we do not support deprecated "all on-link" 37431da177e4SLinus Torvalds * assumption any longer. 37441da177e4SLinus Torvalds */ 374591df42beSJoe Perches pr_debug("%s: no IPv6 routers present\n", idev->dev->name); 37461da177e4SLinus Torvalds } 37471da177e4SLinus Torvalds 37481da177e4SLinus Torvalds out: 3749b7b1bfceSHannes Frederic Sowa write_unlock(&idev->lock); 3750caf92bc4SCong Wang put: 3751b7b1bfceSHannes Frederic Sowa in6_dev_put(idev); 37521da177e4SLinus Torvalds } 37531da177e4SLinus Torvalds 37541da177e4SLinus Torvalds /* 37551da177e4SLinus Torvalds * Duplicate Address Detection 37561da177e4SLinus Torvalds */ 37573c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp) 37583c21edbdSYOSHIFUJI Hideaki { 37593c21edbdSYOSHIFUJI Hideaki unsigned long rand_num; 37603c21edbdSYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev; 3761adc176c5SErik Nordmark u64 nonce; 37623c21edbdSYOSHIFUJI Hideaki 376395c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 376495c385b4SNeil Horman rand_num = 0; 376595c385b4SNeil Horman else 376663862b5bSAruna-Hewapathirane rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1); 376795c385b4SNeil Horman 3768adc176c5SErik Nordmark nonce = 0; 3769adc176c5SErik Nordmark if (idev->cnf.enhanced_dad || 3770adc176c5SErik Nordmark dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad) { 3771adc176c5SErik Nordmark do 3772adc176c5SErik Nordmark get_random_bytes(&nonce, 6); 3773adc176c5SErik Nordmark while (nonce == 0); 3774adc176c5SErik Nordmark } 3775adc176c5SErik Nordmark ifp->dad_nonce = nonce; 3776b7b1bfceSHannes Frederic Sowa ifp->dad_probes = idev->cnf.dad_transmits; 3777c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, rand_num); 37783c21edbdSYOSHIFUJI Hideaki } 37793c21edbdSYOSHIFUJI Hideaki 3780c15b1ccaSHannes Frederic Sowa static void addrconf_dad_begin(struct inet6_ifaddr *ifp) 37811da177e4SLinus Torvalds { 37821da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 37831da177e4SLinus Torvalds struct net_device *dev = idev->dev; 3784764d3be6SPaolo Abeni bool bump_id, notify = false; 37851da177e4SLinus Torvalds 37861da177e4SLinus Torvalds addrconf_join_solict(dev, &ifp->addr); 37871da177e4SLinus Torvalds 378863862b5bSAruna-Hewapathirane prandom_seed((__force u32) ifp->addr.s6_addr32[3]); 37891da177e4SLinus Torvalds 37901da177e4SLinus Torvalds read_lock_bh(&idev->lock); 3791622ccdf1SHerbert Xu spin_lock(&ifp->lock); 3792e9d3e084SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DEAD) 37931da177e4SLinus Torvalds goto out; 37941da177e4SLinus Torvalds 37951da177e4SLinus Torvalds if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) || 37961b34be74SYOSHIFUJI Hideaki idev->cnf.accept_dad < 1 || 379755ebaef1SNoriaki TAKAMIYA !(ifp->flags&IFA_F_TENTATIVE) || 379855ebaef1SNoriaki TAKAMIYA ifp->flags & IFA_F_NODAD) { 3799764d3be6SPaolo Abeni bump_id = ifp->flags & IFA_F_TENTATIVE; 3800cc411d0bSBrian Haley ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 380121809fafSstephen hemminger spin_unlock(&ifp->lock); 38021da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 38031da177e4SLinus Torvalds 3804764d3be6SPaolo Abeni addrconf_dad_completed(ifp, bump_id); 38051da177e4SLinus Torvalds return; 38061da177e4SLinus Torvalds } 38071da177e4SLinus Torvalds 38086732badeSYOSHIFUJI Hideaki if (!(idev->if_flags & IF_READY)) { 380921809fafSstephen hemminger spin_unlock(&ifp->lock); 38106732badeSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 38113c21edbdSYOSHIFUJI Hideaki /* 3812590a9887SMasatake YAMATO * If the device is not ready: 38133c21edbdSYOSHIFUJI Hideaki * - keep it tentative if it is a permanent address. 38143c21edbdSYOSHIFUJI Hideaki * - otherwise, kill it. 38153c21edbdSYOSHIFUJI Hideaki */ 38163c21edbdSYOSHIFUJI Hideaki in6_ifa_hold(ifp); 3817cc411d0bSBrian Haley addrconf_dad_stop(ifp, 0); 38186732badeSYOSHIFUJI Hideaki return; 38193c21edbdSYOSHIFUJI Hideaki } 382095c385b4SNeil Horman 382195c385b4SNeil Horman /* 382295c385b4SNeil Horman * Optimistic nodes can start receiving 382395c385b4SNeil Horman * Frames right away 382495c385b4SNeil Horman */ 38257fd2561eSErik Kline if (ifp->flags & IFA_F_OPTIMISTIC) { 382695c385b4SNeil Horman ip6_ins_rt(ifp->rt); 38277fd2561eSErik Kline if (ipv6_use_optimistic_addr(idev)) { 38287fd2561eSErik Kline /* Because optimistic nodes can use this address, 38297fd2561eSErik Kline * notify listeners. If DAD fails, RTM_DELADDR is sent. 38307fd2561eSErik Kline */ 383116186a82Ssubashab@codeaurora.org notify = true; 38327fd2561eSErik Kline } 38337fd2561eSErik Kline } 383495c385b4SNeil Horman 38356732badeSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 38361da177e4SLinus Torvalds out: 3837622ccdf1SHerbert Xu spin_unlock(&ifp->lock); 38381da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 383916186a82Ssubashab@codeaurora.org if (notify) 384016186a82Ssubashab@codeaurora.org ipv6_ifa_notify(RTM_NEWADDR, ifp); 38411da177e4SLinus Torvalds } 38421da177e4SLinus Torvalds 3843c15b1ccaSHannes Frederic Sowa static void addrconf_dad_start(struct inet6_ifaddr *ifp) 38441da177e4SLinus Torvalds { 3845c15b1ccaSHannes Frederic Sowa bool begin_dad = false; 3846c15b1ccaSHannes Frederic Sowa 38478e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 3848c15b1ccaSHannes Frederic Sowa if (ifp->state != INET6_IFADDR_STATE_DEAD) { 3849c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_PREDAD; 3850c15b1ccaSHannes Frederic Sowa begin_dad = true; 3851c15b1ccaSHannes Frederic Sowa } 38528e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 3853c15b1ccaSHannes Frederic Sowa 3854c15b1ccaSHannes Frederic Sowa if (begin_dad) 3855c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 0); 3856c15b1ccaSHannes Frederic Sowa } 3857c15b1ccaSHannes Frederic Sowa 3858c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w) 3859c15b1ccaSHannes Frederic Sowa { 3860c15b1ccaSHannes Frederic Sowa struct inet6_ifaddr *ifp = container_of(to_delayed_work(w), 3861c15b1ccaSHannes Frederic Sowa struct inet6_ifaddr, 3862c15b1ccaSHannes Frederic Sowa dad_work); 38631da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 3864764d3be6SPaolo Abeni bool bump_id, disable_ipv6 = false; 38651da177e4SLinus Torvalds struct in6_addr mcaddr; 38661da177e4SLinus Torvalds 3867c15b1ccaSHannes Frederic Sowa enum { 3868c15b1ccaSHannes Frederic Sowa DAD_PROCESS, 3869c15b1ccaSHannes Frederic Sowa DAD_BEGIN, 3870c15b1ccaSHannes Frederic Sowa DAD_ABORT, 3871c15b1ccaSHannes Frederic Sowa } action = DAD_PROCESS; 3872c15b1ccaSHannes Frederic Sowa 3873c15b1ccaSHannes Frederic Sowa rtnl_lock(); 3874c15b1ccaSHannes Frederic Sowa 38758e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 3876c15b1ccaSHannes Frederic Sowa if (ifp->state == INET6_IFADDR_STATE_PREDAD) { 3877c15b1ccaSHannes Frederic Sowa action = DAD_BEGIN; 3878c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_DAD; 3879c15b1ccaSHannes Frederic Sowa } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) { 3880c15b1ccaSHannes Frederic Sowa action = DAD_ABORT; 3881c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_POSTDAD; 388285b51b12SMike Manning 388385b51b12SMike Manning if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6 && 388485b51b12SMike Manning !(ifp->flags & IFA_F_STABLE_PRIVACY)) { 388585b51b12SMike Manning struct in6_addr addr; 388685b51b12SMike Manning 388785b51b12SMike Manning addr.s6_addr32[0] = htonl(0xfe800000); 388885b51b12SMike Manning addr.s6_addr32[1] = 0; 388985b51b12SMike Manning 389085b51b12SMike Manning if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) && 389185b51b12SMike Manning ipv6_addr_equal(&ifp->addr, &addr)) { 389285b51b12SMike Manning /* DAD failed for link-local based on MAC */ 389385b51b12SMike Manning idev->cnf.disable_ipv6 = 1; 389485b51b12SMike Manning 389585b51b12SMike Manning pr_info("%s: IPv6 being disabled!\n", 389685b51b12SMike Manning ifp->idev->dev->name); 389785b51b12SMike Manning disable_ipv6 = true; 389885b51b12SMike Manning } 389985b51b12SMike Manning } 3900c15b1ccaSHannes Frederic Sowa } 39018e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 3902c15b1ccaSHannes Frederic Sowa 3903c15b1ccaSHannes Frederic Sowa if (action == DAD_BEGIN) { 3904c15b1ccaSHannes Frederic Sowa addrconf_dad_begin(ifp); 3905c15b1ccaSHannes Frederic Sowa goto out; 3906c15b1ccaSHannes Frederic Sowa } else if (action == DAD_ABORT) { 3907751eb6b6SWei Yongjun in6_ifa_hold(ifp); 3908c15b1ccaSHannes Frederic Sowa addrconf_dad_stop(ifp, 1); 390985b51b12SMike Manning if (disable_ipv6) 391085b51b12SMike Manning addrconf_ifdown(idev->dev, 0); 3911c15b1ccaSHannes Frederic Sowa goto out; 3912c15b1ccaSHannes Frederic Sowa } 3913c15b1ccaSHannes Frederic Sowa 3914b7b1bfceSHannes Frederic Sowa if (!ifp->dad_probes && addrconf_dad_end(ifp)) 3915f2344a13SHerbert Xu goto out; 3916f2344a13SHerbert Xu 3917c15b1ccaSHannes Frederic Sowa write_lock_bh(&idev->lock); 3918122e4519Sstephen hemminger if (idev->dead || !(idev->if_flags & IF_READY)) { 3919c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 39201da177e4SLinus Torvalds goto out; 39211da177e4SLinus Torvalds } 392221809fafSstephen hemminger 392321809fafSstephen hemminger spin_lock(&ifp->lock); 3924622ccdf1SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DEAD) { 3925622ccdf1SHerbert Xu spin_unlock(&ifp->lock); 3926c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 3927622ccdf1SHerbert Xu goto out; 3928622ccdf1SHerbert Xu } 3929622ccdf1SHerbert Xu 3930b7b1bfceSHannes Frederic Sowa if (ifp->dad_probes == 0) { 39311da177e4SLinus Torvalds /* 39321da177e4SLinus Torvalds * DAD was successful 39331da177e4SLinus Torvalds */ 39341da177e4SLinus Torvalds 3935764d3be6SPaolo Abeni bump_id = ifp->flags & IFA_F_TENTATIVE; 3936cc411d0bSBrian Haley ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 393721809fafSstephen hemminger spin_unlock(&ifp->lock); 3938c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 39391da177e4SLinus Torvalds 3940764d3be6SPaolo Abeni addrconf_dad_completed(ifp, bump_id); 39411da177e4SLinus Torvalds 39421da177e4SLinus Torvalds goto out; 39431da177e4SLinus Torvalds } 39441da177e4SLinus Torvalds 3945b7b1bfceSHannes Frederic Sowa ifp->dad_probes--; 3946c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 39471f9248e5SJiri Pirko NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME)); 394821809fafSstephen hemminger spin_unlock(&ifp->lock); 3949c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 39501da177e4SLinus Torvalds 39511da177e4SLinus Torvalds /* send a neighbour solicitation for our addr */ 39521da177e4SLinus Torvalds addrconf_addr_solict_mult(&ifp->addr, &mcaddr); 3953adc176c5SErik Nordmark ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any, 3954adc176c5SErik Nordmark ifp->dad_nonce); 39551da177e4SLinus Torvalds out: 39561da177e4SLinus Torvalds in6_ifa_put(ifp); 3957c15b1ccaSHannes Frederic Sowa rtnl_unlock(); 39581da177e4SLinus Torvalds } 39591da177e4SLinus Torvalds 396011ffff75SHannes Frederic Sowa /* ifp->idev must be at least read locked */ 396111ffff75SHannes Frederic Sowa static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp) 396211ffff75SHannes Frederic Sowa { 396311ffff75SHannes Frederic Sowa struct inet6_ifaddr *ifpiter; 396411ffff75SHannes Frederic Sowa struct inet6_dev *idev = ifp->idev; 396511ffff75SHannes Frederic Sowa 3966602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) { 3967602582caSHannes Frederic Sowa if (ifpiter->scope > IFA_LINK) 3968602582caSHannes Frederic Sowa break; 396911ffff75SHannes Frederic Sowa if (ifp != ifpiter && ifpiter->scope == IFA_LINK && 397011ffff75SHannes Frederic Sowa (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE| 397111ffff75SHannes Frederic Sowa IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) == 397211ffff75SHannes Frederic Sowa IFA_F_PERMANENT) 397311ffff75SHannes Frederic Sowa return false; 397411ffff75SHannes Frederic Sowa } 397511ffff75SHannes Frederic Sowa return true; 397611ffff75SHannes Frederic Sowa } 397711ffff75SHannes Frederic Sowa 3978764d3be6SPaolo Abeni static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id) 39791da177e4SLinus Torvalds { 39801da177e4SLinus Torvalds struct net_device *dev = ifp->idev->dev; 3981b7b1bfceSHannes Frederic Sowa struct in6_addr lladdr; 3982b173ee48SHannes Frederic Sowa bool send_rs, send_mld; 3983b7b1bfceSHannes Frederic Sowa 3984c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifp); 39851da177e4SLinus Torvalds 39861da177e4SLinus Torvalds /* 39871da177e4SLinus Torvalds * Configure the address for reception. Now it is valid. 39881da177e4SLinus Torvalds */ 39891da177e4SLinus Torvalds 39901da177e4SLinus Torvalds ipv6_ifa_notify(RTM_NEWADDR, ifp); 39911da177e4SLinus Torvalds 3992026359bcSTore Anderson /* If added prefix is link local and we are prepared to process 3993026359bcSTore Anderson router advertisements, start sending router solicitations. 39941da177e4SLinus Torvalds */ 39951da177e4SLinus Torvalds 39961ec047ebSHannes Frederic Sowa read_lock_bh(&ifp->idev->lock); 399711ffff75SHannes Frederic Sowa send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp); 3998b173ee48SHannes Frederic Sowa send_rs = send_mld && 3999b173ee48SHannes Frederic Sowa ipv6_accept_ra(ifp->idev) && 4000bd11f074SMaciej Żenczykowski ifp->idev->cnf.rtr_solicits != 0 && 4001b173ee48SHannes Frederic Sowa (dev->flags&IFF_LOOPBACK) == 0; 40021ec047ebSHannes Frederic Sowa read_unlock_bh(&ifp->idev->lock); 40031ec047ebSHannes Frederic Sowa 4004b173ee48SHannes Frederic Sowa /* While dad is in progress mld report's source address is in6_addrany. 4005b173ee48SHannes Frederic Sowa * Resend with proper ll now. 4006b173ee48SHannes Frederic Sowa */ 4007b173ee48SHannes Frederic Sowa if (send_mld) 4008b173ee48SHannes Frederic Sowa ipv6_mc_dad_complete(ifp->idev); 4009b173ee48SHannes Frederic Sowa 40101ec047ebSHannes Frederic Sowa if (send_rs) { 40111da177e4SLinus Torvalds /* 40121da177e4SLinus Torvalds * If a host as already performed a random delay 40131da177e4SLinus Torvalds * [...] as part of DAD [...] there is no need 40141da177e4SLinus Torvalds * to delay again before sending the first RS 40151da177e4SLinus Torvalds */ 40161ec047ebSHannes Frederic Sowa if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE)) 4017b7b1bfceSHannes Frederic Sowa return; 40181ec047ebSHannes Frederic Sowa ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters); 40191da177e4SLinus Torvalds 4020b7b1bfceSHannes Frederic Sowa write_lock_bh(&ifp->idev->lock); 4021b7b1bfceSHannes Frederic Sowa spin_lock(&ifp->lock); 4022bd11f074SMaciej Żenczykowski ifp->idev->rs_interval = rfc3315_s14_backoff_init( 4023bd11f074SMaciej Żenczykowski ifp->idev->cnf.rtr_solicit_interval); 4024b7b1bfceSHannes Frederic Sowa ifp->idev->rs_probes = 1; 40251da177e4SLinus Torvalds ifp->idev->if_flags |= IF_RS_SENT; 4026bd11f074SMaciej Żenczykowski addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval); 4027b7b1bfceSHannes Frederic Sowa spin_unlock(&ifp->lock); 4028b7b1bfceSHannes Frederic Sowa write_unlock_bh(&ifp->idev->lock); 40291da177e4SLinus Torvalds } 4030764d3be6SPaolo Abeni 4031764d3be6SPaolo Abeni if (bump_id) 4032764d3be6SPaolo Abeni rt_genid_bump_ipv6(dev_net(dev)); 4033a11a7f71SMarcus Huewe 4034a11a7f71SMarcus Huewe /* Make sure that a new temporary address will be created 4035a11a7f71SMarcus Huewe * before this temporary address becomes deprecated. 4036a11a7f71SMarcus Huewe */ 4037a11a7f71SMarcus Huewe if (ifp->flags & IFA_F_TEMPORARY) 4038a11a7f71SMarcus Huewe addrconf_verify_rtnl(); 40391da177e4SLinus Torvalds } 40401da177e4SLinus Torvalds 4041e21e8467SStephen Hemminger static void addrconf_dad_run(struct inet6_dev *idev) 4042e21e8467SStephen Hemminger { 4043c5e33bddSYOSHIFUJI Hideaki struct inet6_ifaddr *ifp; 4044c5e33bddSYOSHIFUJI Hideaki 4045c5e33bddSYOSHIFUJI Hideaki read_lock_bh(&idev->lock); 4046502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 404721809fafSstephen hemminger spin_lock(&ifp->lock); 4048f2344a13SHerbert Xu if (ifp->flags & IFA_F_TENTATIVE && 4049f2344a13SHerbert Xu ifp->state == INET6_IFADDR_STATE_DAD) 4050c5e33bddSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 4051f2344a13SHerbert Xu spin_unlock(&ifp->lock); 4052c5e33bddSYOSHIFUJI Hideaki } 4053c5e33bddSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 4054c5e33bddSYOSHIFUJI Hideaki } 4055c5e33bddSYOSHIFUJI Hideaki 40561da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 40571da177e4SLinus Torvalds struct if6_iter_state { 40583c40090aSDaniel Lezcano struct seq_net_private p; 40591da177e4SLinus Torvalds int bucket; 40601d578303SMihai Maruseac int offset; 40611da177e4SLinus Torvalds }; 40621da177e4SLinus Torvalds 40631d578303SMihai Maruseac static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos) 40641da177e4SLinus Torvalds { 40651da177e4SLinus Torvalds struct inet6_ifaddr *ifa = NULL; 40661da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 40671218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 40681d578303SMihai Maruseac int p = 0; 40691da177e4SLinus Torvalds 40701d578303SMihai Maruseac /* initial bucket if pos is 0 */ 40711d578303SMihai Maruseac if (pos == 0) { 40721d578303SMihai Maruseac state->bucket = 0; 40731d578303SMihai Maruseac state->offset = 0; 40741d578303SMihai Maruseac } 40751d578303SMihai Maruseac 40761d578303SMihai Maruseac for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) { 4077b67bfe0dSSasha Levin hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket], 40781d578303SMihai Maruseac addr_lst) { 40799f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 40809f0d3c27SEric Dumazet continue; 40811d578303SMihai Maruseac /* sync with offset */ 40821d578303SMihai Maruseac if (p < state->offset) { 40831d578303SMihai Maruseac p++; 40841d578303SMihai Maruseac continue; 40851d578303SMihai Maruseac } 40861d578303SMihai Maruseac state->offset++; 40871da177e4SLinus Torvalds return ifa; 40881da177e4SLinus Torvalds } 40891d578303SMihai Maruseac 40901d578303SMihai Maruseac /* prepare for next bucket */ 40911d578303SMihai Maruseac state->offset = 0; 40921d578303SMihai Maruseac p = 0; 40931d578303SMihai Maruseac } 4094c2e21293Sstephen hemminger return NULL; 4095c2e21293Sstephen hemminger } 40961da177e4SLinus Torvalds 4097c2e21293Sstephen hemminger static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, 4098c2e21293Sstephen hemminger struct inet6_ifaddr *ifa) 40991da177e4SLinus Torvalds { 41001da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 41011218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 41021da177e4SLinus Torvalds 4103b67bfe0dSSasha Levin hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) { 41049f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 41059f0d3c27SEric Dumazet continue; 41061d578303SMihai Maruseac state->offset++; 41071da177e4SLinus Torvalds return ifa; 41081d578303SMihai Maruseac } 41091da177e4SLinus Torvalds 4110c2e21293Sstephen hemminger while (++state->bucket < IN6_ADDR_HSIZE) { 41111d578303SMihai Maruseac state->offset = 0; 4112b67bfe0dSSasha Levin hlist_for_each_entry_rcu_bh(ifa, 4113c2e21293Sstephen hemminger &inet6_addr_lst[state->bucket], addr_lst) { 41149f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 41159f0d3c27SEric Dumazet continue; 41161d578303SMihai Maruseac state->offset++; 4117c2e21293Sstephen hemminger return ifa; 4118c2e21293Sstephen hemminger } 4119c2e21293Sstephen hemminger } 4120c2e21293Sstephen hemminger 4121c2e21293Sstephen hemminger return NULL; 4122c2e21293Sstephen hemminger } 4123c2e21293Sstephen hemminger 41241da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos) 41254f70eccaSEric Dumazet __acquires(rcu_bh) 41261da177e4SLinus Torvalds { 41275c578aedSstephen hemminger rcu_read_lock_bh(); 41281d578303SMihai Maruseac return if6_get_first(seq, *pos); 41291da177e4SLinus Torvalds } 41301da177e4SLinus Torvalds 41311da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos) 41321da177e4SLinus Torvalds { 41331da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 41341da177e4SLinus Torvalds 41351da177e4SLinus Torvalds ifa = if6_get_next(seq, v); 41361da177e4SLinus Torvalds ++*pos; 41371da177e4SLinus Torvalds return ifa; 41381da177e4SLinus Torvalds } 41391da177e4SLinus Torvalds 41401da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v) 41414f70eccaSEric Dumazet __releases(rcu_bh) 41421da177e4SLinus Torvalds { 41435c578aedSstephen hemminger rcu_read_unlock_bh(); 41441da177e4SLinus Torvalds } 41451da177e4SLinus Torvalds 41461da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v) 41471da177e4SLinus Torvalds { 41481da177e4SLinus Torvalds struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v; 4149971a351cSJiri Pirko seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n", 4150b071195dSHarvey Harrison &ifp->addr, 41511da177e4SLinus Torvalds ifp->idev->dev->ifindex, 41521da177e4SLinus Torvalds ifp->prefix_len, 41531da177e4SLinus Torvalds ifp->scope, 4154971a351cSJiri Pirko (u8) ifp->flags, 41551da177e4SLinus Torvalds ifp->idev->dev->name); 41561da177e4SLinus Torvalds return 0; 41571da177e4SLinus Torvalds } 41581da177e4SLinus Torvalds 415956b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = { 41601da177e4SLinus Torvalds .start = if6_seq_start, 41611da177e4SLinus Torvalds .next = if6_seq_next, 41621da177e4SLinus Torvalds .show = if6_seq_show, 41631da177e4SLinus Torvalds .stop = if6_seq_stop, 41641da177e4SLinus Torvalds }; 41651da177e4SLinus Torvalds 41661da177e4SLinus Torvalds static int if6_seq_open(struct inode *inode, struct file *file) 41671da177e4SLinus Torvalds { 41683c40090aSDaniel Lezcano return seq_open_net(inode, file, &if6_seq_ops, 4169cf7732e4SPavel Emelyanov sizeof(struct if6_iter_state)); 41701da177e4SLinus Torvalds } 41711da177e4SLinus Torvalds 41729a32144eSArjan van de Ven static const struct file_operations if6_fops = { 41731da177e4SLinus Torvalds .owner = THIS_MODULE, 41741da177e4SLinus Torvalds .open = if6_seq_open, 41751da177e4SLinus Torvalds .read = seq_read, 41761da177e4SLinus Torvalds .llseek = seq_lseek, 41773c40090aSDaniel Lezcano .release = seq_release_net, 41781da177e4SLinus Torvalds }; 41791da177e4SLinus Torvalds 41802c8c1e72SAlexey Dobriyan static int __net_init if6_proc_net_init(struct net *net) 41811da177e4SLinus Torvalds { 4182d4beaa66SGao feng if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops)) 41831da177e4SLinus Torvalds return -ENOMEM; 41841da177e4SLinus Torvalds return 0; 41851da177e4SLinus Torvalds } 41861da177e4SLinus Torvalds 41872c8c1e72SAlexey Dobriyan static void __net_exit if6_proc_net_exit(struct net *net) 41883c40090aSDaniel Lezcano { 4189ece31ffdSGao feng remove_proc_entry("if_inet6", net->proc_net); 41903c40090aSDaniel Lezcano } 41913c40090aSDaniel Lezcano 41923c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = { 41933c40090aSDaniel Lezcano .init = if6_proc_net_init, 41943c40090aSDaniel Lezcano .exit = if6_proc_net_exit, 41953c40090aSDaniel Lezcano }; 41963c40090aSDaniel Lezcano 41973c40090aSDaniel Lezcano int __init if6_proc_init(void) 41983c40090aSDaniel Lezcano { 41993c40090aSDaniel Lezcano return register_pernet_subsys(&if6_proc_net_ops); 42003c40090aSDaniel Lezcano } 42013c40090aSDaniel Lezcano 42021da177e4SLinus Torvalds void if6_proc_exit(void) 42031da177e4SLinus Torvalds { 42043c40090aSDaniel Lezcano unregister_pernet_subsys(&if6_proc_net_ops); 42051da177e4SLinus Torvalds } 42061da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 42071da177e4SLinus Torvalds 420807a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6) 42093b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */ 4210b71d1d42SEric Dumazet int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr) 42113b9f9a1cSNoriaki TAKAMIYA { 42123b9f9a1cSNoriaki TAKAMIYA int ret = 0; 4213c2e21293Sstephen hemminger struct inet6_ifaddr *ifp = NULL; 4214ddbe5032SEric Dumazet unsigned int hash = inet6_addr_hash(addr); 4215c2e21293Sstephen hemminger 42165c578aedSstephen hemminger rcu_read_lock_bh(); 4217b67bfe0dSSasha Levin hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) { 4218878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 4219389f6612SDaniel Lezcano continue; 4220caad295fSYOSHIFUJI Hideaki if (ipv6_addr_equal(&ifp->addr, addr) && 42213b9f9a1cSNoriaki TAKAMIYA (ifp->flags & IFA_F_HOMEADDRESS)) { 42223b9f9a1cSNoriaki TAKAMIYA ret = 1; 42233b9f9a1cSNoriaki TAKAMIYA break; 42243b9f9a1cSNoriaki TAKAMIYA } 42253b9f9a1cSNoriaki TAKAMIYA } 42265c578aedSstephen hemminger rcu_read_unlock_bh(); 42273b9f9a1cSNoriaki TAKAMIYA return ret; 42283b9f9a1cSNoriaki TAKAMIYA } 42293b9f9a1cSNoriaki TAKAMIYA #endif 42303b9f9a1cSNoriaki TAKAMIYA 42311da177e4SLinus Torvalds /* 42321da177e4SLinus Torvalds * Periodic address status verification 42331da177e4SLinus Torvalds */ 42341da177e4SLinus Torvalds 4235c15b1ccaSHannes Frederic Sowa static void addrconf_verify_rtnl(void) 42361da177e4SLinus Torvalds { 4237b2db7564SYOSHIFUJI Hideaki unsigned long now, next, next_sec, next_sched; 42381da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 42391da177e4SLinus Torvalds int i; 42401da177e4SLinus Torvalds 4241c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 4242c15b1ccaSHannes Frederic Sowa 42435c578aedSstephen hemminger rcu_read_lock_bh(); 42441da177e4SLinus Torvalds now = jiffies; 4245b2db7564SYOSHIFUJI Hideaki next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY); 42461da177e4SLinus Torvalds 4247c15b1ccaSHannes Frederic Sowa cancel_delayed_work(&addr_chk_work); 42481da177e4SLinus Torvalds 42491da177e4SLinus Torvalds for (i = 0; i < IN6_ADDR_HSIZE; i++) { 42501da177e4SLinus Torvalds restart: 4251c15b1ccaSHannes Frederic Sowa hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) { 42521da177e4SLinus Torvalds unsigned long age; 42531da177e4SLinus Torvalds 4254fad8da3eSYasushi Asano /* When setting preferred_lft to a value not zero or 4255fad8da3eSYasushi Asano * infinity, while valid_lft is infinity 4256fad8da3eSYasushi Asano * IFA_F_PERMANENT has a non-infinity life time. 4257fad8da3eSYasushi Asano */ 4258fad8da3eSYasushi Asano if ((ifp->flags & IFA_F_PERMANENT) && 4259fad8da3eSYasushi Asano (ifp->prefered_lft == INFINITY_LIFE_TIME)) 42601da177e4SLinus Torvalds continue; 42611da177e4SLinus Torvalds 42621da177e4SLinus Torvalds spin_lock(&ifp->lock); 4263b2db7564SYOSHIFUJI Hideaki /* We try to batch several events at once. */ 4264b2db7564SYOSHIFUJI Hideaki age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; 42651da177e4SLinus Torvalds 42668f27ebb9SYOSHIFUJI Hideaki if (ifp->valid_lft != INFINITY_LIFE_TIME && 42678f27ebb9SYOSHIFUJI Hideaki age >= ifp->valid_lft) { 42681da177e4SLinus Torvalds spin_unlock(&ifp->lock); 42691da177e4SLinus Torvalds in6_ifa_hold(ifp); 42701da177e4SLinus Torvalds ipv6_del_addr(ifp); 42711da177e4SLinus Torvalds goto restart; 42728f27ebb9SYOSHIFUJI Hideaki } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) { 42738f27ebb9SYOSHIFUJI Hideaki spin_unlock(&ifp->lock); 42748f27ebb9SYOSHIFUJI Hideaki continue; 42751da177e4SLinus Torvalds } else if (age >= ifp->prefered_lft) { 4276a1ed0526SBrian Haley /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */ 42771da177e4SLinus Torvalds int deprecate = 0; 42781da177e4SLinus Torvalds 42791da177e4SLinus Torvalds if (!(ifp->flags&IFA_F_DEPRECATED)) { 42801da177e4SLinus Torvalds deprecate = 1; 42811da177e4SLinus Torvalds ifp->flags |= IFA_F_DEPRECATED; 42821da177e4SLinus Torvalds } 42831da177e4SLinus Torvalds 4284fad8da3eSYasushi Asano if ((ifp->valid_lft != INFINITY_LIFE_TIME) && 4285fad8da3eSYasushi Asano (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))) 42861da177e4SLinus Torvalds next = ifp->tstamp + ifp->valid_lft * HZ; 42871da177e4SLinus Torvalds 42881da177e4SLinus Torvalds spin_unlock(&ifp->lock); 42891da177e4SLinus Torvalds 42901da177e4SLinus Torvalds if (deprecate) { 42911da177e4SLinus Torvalds in6_ifa_hold(ifp); 42921da177e4SLinus Torvalds 42931da177e4SLinus Torvalds ipv6_ifa_notify(0, ifp); 42941da177e4SLinus Torvalds in6_ifa_put(ifp); 42951da177e4SLinus Torvalds goto restart; 42961da177e4SLinus Torvalds } 42971da177e4SLinus Torvalds } else if ((ifp->flags&IFA_F_TEMPORARY) && 42981da177e4SLinus Torvalds !(ifp->flags&IFA_F_TENTATIVE)) { 429988949cf4Sstephen hemminger unsigned long regen_advance = ifp->idev->cnf.regen_max_retry * 430088949cf4Sstephen hemminger ifp->idev->cnf.dad_transmits * 43011f9248e5SJiri Pirko NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ; 430288949cf4Sstephen hemminger 43031da177e4SLinus Torvalds if (age >= ifp->prefered_lft - regen_advance) { 43041da177e4SLinus Torvalds struct inet6_ifaddr *ifpub = ifp->ifpub; 43051da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 43061da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 43071da177e4SLinus Torvalds if (!ifp->regen_count && ifpub) { 43081da177e4SLinus Torvalds ifp->regen_count++; 43091da177e4SLinus Torvalds in6_ifa_hold(ifp); 43101da177e4SLinus Torvalds in6_ifa_hold(ifpub); 43111da177e4SLinus Torvalds spin_unlock(&ifp->lock); 43125c578aedSstephen hemminger 4313291d809bSHiroyuki YAMAMORI spin_lock(&ifpub->lock); 4314291d809bSHiroyuki YAMAMORI ifpub->regen_count = 0; 4315291d809bSHiroyuki YAMAMORI spin_unlock(&ifpub->lock); 43161da177e4SLinus Torvalds ipv6_create_tempaddr(ifpub, ifp); 43171da177e4SLinus Torvalds in6_ifa_put(ifpub); 43181da177e4SLinus Torvalds in6_ifa_put(ifp); 43191da177e4SLinus Torvalds goto restart; 43201da177e4SLinus Torvalds } 43211da177e4SLinus Torvalds } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next)) 43221da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ; 43231da177e4SLinus Torvalds spin_unlock(&ifp->lock); 43241da177e4SLinus Torvalds } else { 43251da177e4SLinus Torvalds /* ifp->prefered_lft <= ifp->valid_lft */ 43261da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 43271da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 43281da177e4SLinus Torvalds spin_unlock(&ifp->lock); 43291da177e4SLinus Torvalds } 43301da177e4SLinus Torvalds } 43311da177e4SLinus Torvalds } 43321da177e4SLinus Torvalds 4333b2db7564SYOSHIFUJI Hideaki next_sec = round_jiffies_up(next); 4334b2db7564SYOSHIFUJI Hideaki next_sched = next; 4335b2db7564SYOSHIFUJI Hideaki 4336b2db7564SYOSHIFUJI Hideaki /* If rounded timeout is accurate enough, accept it. */ 4337b2db7564SYOSHIFUJI Hideaki if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ)) 4338b2db7564SYOSHIFUJI Hideaki next_sched = next_sec; 4339b2db7564SYOSHIFUJI Hideaki 4340b2db7564SYOSHIFUJI Hideaki /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */ 4341b2db7564SYOSHIFUJI Hideaki if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX)) 4342b2db7564SYOSHIFUJI Hideaki next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX; 4343b2db7564SYOSHIFUJI Hideaki 4344ba3542e1Sdingtianhong ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n", 4345ba3542e1Sdingtianhong now, next, next_sec, next_sched); 4346c15b1ccaSHannes Frederic Sowa mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now); 43475c578aedSstephen hemminger rcu_read_unlock_bh(); 43481da177e4SLinus Torvalds } 43491da177e4SLinus Torvalds 4350c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *w) 4351c15b1ccaSHannes Frederic Sowa { 4352c15b1ccaSHannes Frederic Sowa rtnl_lock(); 4353c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 4354c15b1ccaSHannes Frederic Sowa rtnl_unlock(); 4355c15b1ccaSHannes Frederic Sowa } 4356c15b1ccaSHannes Frederic Sowa 4357c15b1ccaSHannes Frederic Sowa static void addrconf_verify(void) 4358c15b1ccaSHannes Frederic Sowa { 4359c15b1ccaSHannes Frederic Sowa mod_delayed_work(addrconf_wq, &addr_chk_work, 0); 4360c15b1ccaSHannes Frederic Sowa } 4361c15b1ccaSHannes Frederic Sowa 4362caeaba79SNicolas Dichtel static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local, 4363caeaba79SNicolas Dichtel struct in6_addr **peer_pfx) 4364461d8837SThomas Graf { 4365461d8837SThomas Graf struct in6_addr *pfx = NULL; 4366461d8837SThomas Graf 4367caeaba79SNicolas Dichtel *peer_pfx = NULL; 4368caeaba79SNicolas Dichtel 4369461d8837SThomas Graf if (addr) 4370461d8837SThomas Graf pfx = nla_data(addr); 4371461d8837SThomas Graf 4372461d8837SThomas Graf if (local) { 4373461d8837SThomas Graf if (pfx && nla_memcmp(local, pfx, sizeof(*pfx))) 4374caeaba79SNicolas Dichtel *peer_pfx = pfx; 4375461d8837SThomas Graf pfx = nla_data(local); 4376461d8837SThomas Graf } 4377461d8837SThomas Graf 4378461d8837SThomas Graf return pfx; 4379461d8837SThomas Graf } 4380461d8837SThomas Graf 4381ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = { 4382461d8837SThomas Graf [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) }, 4383461d8837SThomas Graf [IFA_LOCAL] = { .len = sizeof(struct in6_addr) }, 4384461d8837SThomas Graf [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) }, 4385479840ffSJiri Pirko [IFA_FLAGS] = { .len = sizeof(u32) }, 4386461d8837SThomas Graf }; 4387461d8837SThomas Graf 43881da177e4SLinus Torvalds static int 4389661d2967SThomas Graf inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh) 43901da177e4SLinus Torvalds { 43913b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 4392b933f716SThomas Graf struct ifaddrmsg *ifm; 4393b933f716SThomas Graf struct nlattr *tb[IFA_MAX+1]; 4394caeaba79SNicolas Dichtel struct in6_addr *pfx, *peer_pfx; 43956046d5b4SHeiner Kallweit u32 ifa_flags; 4396b933f716SThomas Graf int err; 43971da177e4SLinus Torvalds 4398b933f716SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 4399b933f716SThomas Graf if (err < 0) 4400b933f716SThomas Graf return err; 4401b933f716SThomas Graf 4402b933f716SThomas Graf ifm = nlmsg_data(nlh); 4403caeaba79SNicolas Dichtel pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); 440463159f29SIan Morris if (!pfx) 44051da177e4SLinus Torvalds return -EINVAL; 44061da177e4SLinus Torvalds 44076046d5b4SHeiner Kallweit ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags; 44086046d5b4SHeiner Kallweit 44096046d5b4SHeiner Kallweit /* We ignore other flags so far. */ 44106046d5b4SHeiner Kallweit ifa_flags &= IFA_F_MANAGETEMPADDR; 44116046d5b4SHeiner Kallweit 44126046d5b4SHeiner Kallweit return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx, 44136046d5b4SHeiner Kallweit ifm->ifa_prefixlen); 44141da177e4SLinus Torvalds } 44151da177e4SLinus Torvalds 4416479840ffSJiri Pirko static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags, 441755ebaef1SNoriaki TAKAMIYA u32 prefered_lft, u32 valid_lft) 4418081bba5bSNoriaki TAKAMIYA { 44196f704992SYOSHIFUJI Hideaki u32 flags; 44206f704992SYOSHIFUJI Hideaki clock_t expires; 44214bed72e4SYOSHIFUJI Hideaki unsigned long timeout; 442253bd6749SJiri Pirko bool was_managetempaddr; 44235b84efecSThomas Haller bool had_prefixroute; 442446d48046SYOSHIFUJI Hideaki 4425c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 4426c15b1ccaSHannes Frederic Sowa 4427081bba5bSNoriaki TAKAMIYA if (!valid_lft || (prefered_lft > valid_lft)) 4428081bba5bSNoriaki TAKAMIYA return -EINVAL; 4429081bba5bSNoriaki TAKAMIYA 443053bd6749SJiri Pirko if (ifa_flags & IFA_F_MANAGETEMPADDR && 443153bd6749SJiri Pirko (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64)) 443253bd6749SJiri Pirko return -EINVAL; 443353bd6749SJiri Pirko 44344bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(valid_lft, HZ); 44354bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 44364bed72e4SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(timeout * HZ); 44374bed72e4SYOSHIFUJI Hideaki valid_lft = timeout; 44386f704992SYOSHIFUJI Hideaki flags = RTF_EXPIRES; 44394bed72e4SYOSHIFUJI Hideaki } else { 44404bed72e4SYOSHIFUJI Hideaki expires = 0; 44414bed72e4SYOSHIFUJI Hideaki flags = 0; 44424bed72e4SYOSHIFUJI Hideaki ifa_flags |= IFA_F_PERMANENT; 44436f704992SYOSHIFUJI Hideaki } 4444081bba5bSNoriaki TAKAMIYA 44454bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(prefered_lft, HZ); 44464bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 44474bed72e4SYOSHIFUJI Hideaki if (timeout == 0) 444855ebaef1SNoriaki TAKAMIYA ifa_flags |= IFA_F_DEPRECATED; 44494bed72e4SYOSHIFUJI Hideaki prefered_lft = timeout; 44504bed72e4SYOSHIFUJI Hideaki } 4451081bba5bSNoriaki TAKAMIYA 4452081bba5bSNoriaki TAKAMIYA spin_lock_bh(&ifp->lock); 445353bd6749SJiri Pirko was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR; 44545b84efecSThomas Haller had_prefixroute = ifp->flags & IFA_F_PERMANENT && 44555b84efecSThomas Haller !(ifp->flags & IFA_F_NOPREFIXROUTE); 445653bd6749SJiri Pirko ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | 4457761aac73SThomas Haller IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR | 4458761aac73SThomas Haller IFA_F_NOPREFIXROUTE); 445953bd6749SJiri Pirko ifp->flags |= ifa_flags; 4460081bba5bSNoriaki TAKAMIYA ifp->tstamp = jiffies; 4461081bba5bSNoriaki TAKAMIYA ifp->valid_lft = valid_lft; 4462081bba5bSNoriaki TAKAMIYA ifp->prefered_lft = prefered_lft; 4463081bba5bSNoriaki TAKAMIYA 4464081bba5bSNoriaki TAKAMIYA spin_unlock_bh(&ifp->lock); 4465081bba5bSNoriaki TAKAMIYA if (!(ifp->flags&IFA_F_TENTATIVE)) 4466081bba5bSNoriaki TAKAMIYA ipv6_ifa_notify(0, ifp); 4467081bba5bSNoriaki TAKAMIYA 4468761aac73SThomas Haller if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) { 446946d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev, 44706f704992SYOSHIFUJI Hideaki expires, flags); 44715b84efecSThomas Haller } else if (had_prefixroute) { 44725b84efecSThomas Haller enum cleanup_prefix_rt_t action; 44735b84efecSThomas Haller unsigned long rt_expires; 44745b84efecSThomas Haller 44755b84efecSThomas Haller write_lock_bh(&ifp->idev->lock); 44765b84efecSThomas Haller action = check_cleanup_prefix_route(ifp, &rt_expires); 44775b84efecSThomas Haller write_unlock_bh(&ifp->idev->lock); 44785b84efecSThomas Haller 44795b84efecSThomas Haller if (action != CLEANUP_PREFIX_RT_NOP) { 44805b84efecSThomas Haller cleanup_prefix_route(ifp, rt_expires, 44815b84efecSThomas Haller action == CLEANUP_PREFIX_RT_DEL); 44825b84efecSThomas Haller } 4483761aac73SThomas Haller } 448453bd6749SJiri Pirko 448553bd6749SJiri Pirko if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) { 448653bd6749SJiri Pirko if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR)) 448753bd6749SJiri Pirko valid_lft = prefered_lft = 0; 448853bd6749SJiri Pirko manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft, 448953bd6749SJiri Pirko !was_managetempaddr, jiffies); 449053bd6749SJiri Pirko } 449153bd6749SJiri Pirko 4492c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 4493081bba5bSNoriaki TAKAMIYA 4494081bba5bSNoriaki TAKAMIYA return 0; 4495081bba5bSNoriaki TAKAMIYA } 4496081bba5bSNoriaki TAKAMIYA 4497081bba5bSNoriaki TAKAMIYA static int 4498661d2967SThomas Graf inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh) 44991da177e4SLinus Torvalds { 45003b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 4501461d8837SThomas Graf struct ifaddrmsg *ifm; 4502461d8837SThomas Graf struct nlattr *tb[IFA_MAX+1]; 4503caeaba79SNicolas Dichtel struct in6_addr *pfx, *peer_pfx; 45047198f8ceSThomas Graf struct inet6_ifaddr *ifa; 45057198f8ceSThomas Graf struct net_device *dev; 450655ebaef1SNoriaki TAKAMIYA u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME; 4507479840ffSJiri Pirko u32 ifa_flags; 4508461d8837SThomas Graf int err; 45091da177e4SLinus Torvalds 4510461d8837SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 4511461d8837SThomas Graf if (err < 0) 4512461d8837SThomas Graf return err; 4513461d8837SThomas Graf 4514461d8837SThomas Graf ifm = nlmsg_data(nlh); 4515caeaba79SNicolas Dichtel pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); 451663159f29SIan Morris if (!pfx) 45171da177e4SLinus Torvalds return -EINVAL; 45181da177e4SLinus Torvalds 4519461d8837SThomas Graf if (tb[IFA_CACHEINFO]) { 45200778769dSNoriaki TAKAMIYA struct ifa_cacheinfo *ci; 4521461d8837SThomas Graf 4522461d8837SThomas Graf ci = nla_data(tb[IFA_CACHEINFO]); 45230778769dSNoriaki TAKAMIYA valid_lft = ci->ifa_valid; 4524461d8837SThomas Graf preferred_lft = ci->ifa_prefered; 4525461d8837SThomas Graf } else { 4526461d8837SThomas Graf preferred_lft = INFINITY_LIFE_TIME; 4527461d8837SThomas Graf valid_lft = INFINITY_LIFE_TIME; 45280778769dSNoriaki TAKAMIYA } 45290778769dSNoriaki TAKAMIYA 4530af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifm->ifa_index); 453163159f29SIan Morris if (!dev) 45327198f8ceSThomas Graf return -ENODEV; 4533081bba5bSNoriaki TAKAMIYA 4534479840ffSJiri Pirko ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags; 4535479840ffSJiri Pirko 453655ebaef1SNoriaki TAKAMIYA /* We ignore other flags so far. */ 4537761aac73SThomas Haller ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR | 453893a714d6SMadhu Challa IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN; 453955ebaef1SNoriaki TAKAMIYA 45401cab3da6SDaniel Lezcano ifa = ipv6_get_ifaddr(net, pfx, dev, 1); 454163159f29SIan Morris if (!ifa) { 45427198f8ceSThomas Graf /* 45437198f8ceSThomas Graf * It would be best to check for !NLM_F_CREATE here but 4544db9c7c39Sstephen hemminger * userspace already relies on not having to provide this. 45457198f8ceSThomas Graf */ 4546caeaba79SNicolas Dichtel return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx, 4547af284937SDaniel Lezcano ifm->ifa_prefixlen, ifa_flags, 4548af284937SDaniel Lezcano preferred_lft, valid_lft); 45491da177e4SLinus Torvalds } 45501da177e4SLinus Torvalds 45517198f8ceSThomas Graf if (nlh->nlmsg_flags & NLM_F_EXCL || 45527198f8ceSThomas Graf !(nlh->nlmsg_flags & NLM_F_REPLACE)) 45537198f8ceSThomas Graf err = -EEXIST; 45547198f8ceSThomas Graf else 455555ebaef1SNoriaki TAKAMIYA err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft); 45567198f8ceSThomas Graf 45577198f8ceSThomas Graf in6_ifa_put(ifa); 45587198f8ceSThomas Graf 45597198f8ceSThomas Graf return err; 45607198f8ceSThomas Graf } 45617198f8ceSThomas Graf 4562479840ffSJiri Pirko static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags, 4563101bb229SThomas Graf u8 scope, int ifindex) 4564101bb229SThomas Graf { 4565101bb229SThomas Graf struct ifaddrmsg *ifm; 4566101bb229SThomas Graf 4567101bb229SThomas Graf ifm = nlmsg_data(nlh); 4568101bb229SThomas Graf ifm->ifa_family = AF_INET6; 4569101bb229SThomas Graf ifm->ifa_prefixlen = prefixlen; 4570101bb229SThomas Graf ifm->ifa_flags = flags; 4571101bb229SThomas Graf ifm->ifa_scope = scope; 4572101bb229SThomas Graf ifm->ifa_index = ifindex; 4573101bb229SThomas Graf } 4574101bb229SThomas Graf 457585486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp, 457685486af0SThomas Graf unsigned long tstamp, u32 preferred, u32 valid) 457785486af0SThomas Graf { 457885486af0SThomas Graf struct ifa_cacheinfo ci; 457985486af0SThomas Graf 458018a31e1eSThomas Graf ci.cstamp = cstamp_delta(cstamp); 458118a31e1eSThomas Graf ci.tstamp = cstamp_delta(tstamp); 458285486af0SThomas Graf ci.ifa_prefered = preferred; 458385486af0SThomas Graf ci.ifa_valid = valid; 458485486af0SThomas Graf 458585486af0SThomas Graf return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci); 458685486af0SThomas Graf } 458785486af0SThomas Graf 4588101bb229SThomas Graf static inline int rt_scope(int ifa_scope) 4589101bb229SThomas Graf { 4590101bb229SThomas Graf if (ifa_scope & IFA_HOST) 4591101bb229SThomas Graf return RT_SCOPE_HOST; 4592101bb229SThomas Graf else if (ifa_scope & IFA_LINK) 4593101bb229SThomas Graf return RT_SCOPE_LINK; 4594101bb229SThomas Graf else if (ifa_scope & IFA_SITE) 4595101bb229SThomas Graf return RT_SCOPE_SITE; 4596101bb229SThomas Graf else 4597101bb229SThomas Graf return RT_SCOPE_UNIVERSE; 4598101bb229SThomas Graf } 4599101bb229SThomas Graf 46000ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void) 46010ab6803bSThomas Graf { 4602339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifaddrmsg)) 4603caeaba79SNicolas Dichtel + nla_total_size(16) /* IFA_LOCAL */ 4604339bf98fSThomas Graf + nla_total_size(16) /* IFA_ADDRESS */ 4605479840ffSJiri Pirko + nla_total_size(sizeof(struct ifa_cacheinfo)) 4606479840ffSJiri Pirko + nla_total_size(4) /* IFA_FLAGS */; 46070ab6803bSThomas Graf } 4608c5396a31SYOSHIFUJI Hideaki 46091da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa, 461015e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 46111da177e4SLinus Torvalds { 46121da177e4SLinus Torvalds struct nlmsghdr *nlh; 461385486af0SThomas Graf u32 preferred, valid; 46141da177e4SLinus Torvalds 461515e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 461663159f29SIan Morris if (!nlh) 461726932566SPatrick McHardy return -EMSGSIZE; 46180ab6803bSThomas Graf 4619101bb229SThomas Graf put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope), 4620101bb229SThomas Graf ifa->idev->dev->ifindex); 4621101bb229SThomas Graf 4622fad8da3eSYasushi Asano if (!((ifa->flags&IFA_F_PERMANENT) && 4623fad8da3eSYasushi Asano (ifa->prefered_lft == INFINITY_LIFE_TIME))) { 462485486af0SThomas Graf preferred = ifa->prefered_lft; 462585486af0SThomas Graf valid = ifa->valid_lft; 462685486af0SThomas Graf if (preferred != INFINITY_LIFE_TIME) { 46271da177e4SLinus Torvalds long tval = (jiffies - ifa->tstamp)/HZ; 4628a1faa698SJens Rosenboom if (preferred > tval) 462985486af0SThomas Graf preferred -= tval; 4630a1faa698SJens Rosenboom else 4631a1faa698SJens Rosenboom preferred = 0; 4632f56619fcSBen Hutchings if (valid != INFINITY_LIFE_TIME) { 4633f56619fcSBen Hutchings if (valid > tval) 463485486af0SThomas Graf valid -= tval; 4635f56619fcSBen Hutchings else 4636f56619fcSBen Hutchings valid = 0; 4637f56619fcSBen Hutchings } 46381da177e4SLinus Torvalds } 46391da177e4SLinus Torvalds } else { 464085486af0SThomas Graf preferred = INFINITY_LIFE_TIME; 464185486af0SThomas Graf valid = INFINITY_LIFE_TIME; 46421da177e4SLinus Torvalds } 464385486af0SThomas Graf 46447996c799SCong Wang if (!ipv6_addr_any(&ifa->peer_addr)) { 4645930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 || 4646930345eaSJiri Benc nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0) 4647caeaba79SNicolas Dichtel goto error; 4648caeaba79SNicolas Dichtel } else 4649930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0) 4650caeaba79SNicolas Dichtel goto error; 4651caeaba79SNicolas Dichtel 4652caeaba79SNicolas Dichtel if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) 4653caeaba79SNicolas Dichtel goto error; 465485486af0SThomas Graf 4655479840ffSJiri Pirko if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0) 4656479840ffSJiri Pirko goto error; 4657479840ffSJiri Pirko 4658053c095aSJohannes Berg nlmsg_end(skb, nlh); 4659053c095aSJohannes Berg return 0; 4660caeaba79SNicolas Dichtel 4661caeaba79SNicolas Dichtel error: 4662caeaba79SNicolas Dichtel nlmsg_cancel(skb, nlh); 4663caeaba79SNicolas Dichtel return -EMSGSIZE; 46641da177e4SLinus Torvalds } 46651da177e4SLinus Torvalds 46661da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca, 466715e47304SEric W. Biederman u32 portid, u32 seq, int event, u16 flags) 46681da177e4SLinus Torvalds { 46691da177e4SLinus Torvalds struct nlmsghdr *nlh; 4670101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 4671101bb229SThomas Graf int ifindex = ifmca->idev->dev->ifindex; 46721da177e4SLinus Torvalds 46731da177e4SLinus Torvalds if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE) 4674101bb229SThomas Graf scope = RT_SCOPE_SITE; 4675101bb229SThomas Graf 467615e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 467763159f29SIan Morris if (!nlh) 467826932566SPatrick McHardy return -EMSGSIZE; 46790ab6803bSThomas Graf 4680101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 4681930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 || 46820ab6803bSThomas Graf put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp, 468326932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 468426932566SPatrick McHardy nlmsg_cancel(skb, nlh); 468526932566SPatrick McHardy return -EMSGSIZE; 468626932566SPatrick McHardy } 468785486af0SThomas Graf 4688053c095aSJohannes Berg nlmsg_end(skb, nlh); 4689053c095aSJohannes Berg return 0; 46901da177e4SLinus Torvalds } 46911da177e4SLinus Torvalds 46921da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca, 469315e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 46941da177e4SLinus Torvalds { 46951da177e4SLinus Torvalds struct nlmsghdr *nlh; 4696101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 4697101bb229SThomas Graf int ifindex = ifaca->aca_idev->dev->ifindex; 46981da177e4SLinus Torvalds 46991da177e4SLinus Torvalds if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE) 4700101bb229SThomas Graf scope = RT_SCOPE_SITE; 4701101bb229SThomas Graf 470215e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 470363159f29SIan Morris if (!nlh) 470426932566SPatrick McHardy return -EMSGSIZE; 47050ab6803bSThomas Graf 4706101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 4707930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 || 47080ab6803bSThomas Graf put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp, 470926932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 471026932566SPatrick McHardy nlmsg_cancel(skb, nlh); 471126932566SPatrick McHardy return -EMSGSIZE; 471226932566SPatrick McHardy } 471385486af0SThomas Graf 4714053c095aSJohannes Berg nlmsg_end(skb, nlh); 4715053c095aSJohannes Berg return 0; 47161da177e4SLinus Torvalds } 47171da177e4SLinus Torvalds 4718e21e8467SStephen Hemminger enum addr_type_t { 47191da177e4SLinus Torvalds UNICAST_ADDR, 47201da177e4SLinus Torvalds MULTICAST_ADDR, 47211da177e4SLinus Torvalds ANYCAST_ADDR, 47221da177e4SLinus Torvalds }; 47231da177e4SLinus Torvalds 4724234b27c3SEric Dumazet /* called with rcu_read_lock() */ 4725234b27c3SEric Dumazet static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb, 4726234b27c3SEric Dumazet struct netlink_callback *cb, enum addr_type_t type, 4727234b27c3SEric Dumazet int s_ip_idx, int *p_ip_idx) 47281da177e4SLinus Torvalds { 47291da177e4SLinus Torvalds struct ifmcaddr6 *ifmca; 47301da177e4SLinus Torvalds struct ifacaddr6 *ifaca; 4731234b27c3SEric Dumazet int err = 1; 4732234b27c3SEric Dumazet int ip_idx = *p_ip_idx; 47331da177e4SLinus Torvalds 47341da177e4SLinus Torvalds read_lock_bh(&idev->lock); 47351da177e4SLinus Torvalds switch (type) { 4736502a2ffdSstephen hemminger case UNICAST_ADDR: { 4737502a2ffdSstephen hemminger struct inet6_ifaddr *ifa; 4738502a2ffdSstephen hemminger 4739ae9cda5dSYOSHIFUJI Hideaki /* unicast address incl. temp addr */ 4740502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 4741502a2ffdSstephen hemminger if (++ip_idx < s_ip_idx) 47421da177e4SLinus Torvalds continue; 47435d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifaddr(skb, ifa, 474415e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 47455d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 47465d5619b4SYOSHIFUJI Hideaki RTM_NEWADDR, 47475d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 4748053c095aSJohannes Berg if (err < 0) 4749bcd32326SEric Dumazet break; 475063998ac2SNicolas Dichtel nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 47511da177e4SLinus Torvalds } 47521da177e4SLinus Torvalds break; 4753502a2ffdSstephen hemminger } 47541da177e4SLinus Torvalds case MULTICAST_ADDR: 47551da177e4SLinus Torvalds /* multicast address */ 47561da177e4SLinus Torvalds for (ifmca = idev->mc_list; ifmca; 47571da177e4SLinus Torvalds ifmca = ifmca->next, ip_idx++) { 47581da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 47591da177e4SLinus Torvalds continue; 47605d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifmcaddr(skb, ifmca, 476115e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 47625d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 47635d5619b4SYOSHIFUJI Hideaki RTM_GETMULTICAST, 47645d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 4765053c095aSJohannes Berg if (err < 0) 4766bcd32326SEric Dumazet break; 47671da177e4SLinus Torvalds } 47681da177e4SLinus Torvalds break; 47691da177e4SLinus Torvalds case ANYCAST_ADDR: 47701da177e4SLinus Torvalds /* anycast address */ 47711da177e4SLinus Torvalds for (ifaca = idev->ac_list; ifaca; 47721da177e4SLinus Torvalds ifaca = ifaca->aca_next, ip_idx++) { 47731da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 47741da177e4SLinus Torvalds continue; 47755d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifacaddr(skb, ifaca, 477615e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 47775d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 47785d5619b4SYOSHIFUJI Hideaki RTM_GETANYCAST, 47795d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 4780053c095aSJohannes Berg if (err < 0) 4781bcd32326SEric Dumazet break; 47821da177e4SLinus Torvalds } 47831da177e4SLinus Torvalds break; 47841da177e4SLinus Torvalds default: 47851da177e4SLinus Torvalds break; 47861da177e4SLinus Torvalds } 47871da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 4788234b27c3SEric Dumazet *p_ip_idx = ip_idx; 4789234b27c3SEric Dumazet return err; 4790234b27c3SEric Dumazet } 47915d5619b4SYOSHIFUJI Hideaki 4792234b27c3SEric Dumazet static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb, 4793234b27c3SEric Dumazet enum addr_type_t type) 4794234b27c3SEric Dumazet { 4795234b27c3SEric Dumazet struct net *net = sock_net(skb->sk); 4796234b27c3SEric Dumazet int h, s_h; 4797234b27c3SEric Dumazet int idx, ip_idx; 4798234b27c3SEric Dumazet int s_idx, s_ip_idx; 4799234b27c3SEric Dumazet struct net_device *dev; 4800234b27c3SEric Dumazet struct inet6_dev *idev; 4801234b27c3SEric Dumazet struct hlist_head *head; 4802234b27c3SEric Dumazet 4803234b27c3SEric Dumazet s_h = cb->args[0]; 4804234b27c3SEric Dumazet s_idx = idx = cb->args[1]; 4805234b27c3SEric Dumazet s_ip_idx = ip_idx = cb->args[2]; 4806234b27c3SEric Dumazet 4807234b27c3SEric Dumazet rcu_read_lock(); 480863998ac2SNicolas Dichtel cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq; 4809234b27c3SEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 4810234b27c3SEric Dumazet idx = 0; 4811234b27c3SEric Dumazet head = &net->dev_index_head[h]; 4812b67bfe0dSSasha Levin hlist_for_each_entry_rcu(dev, head, index_hlist) { 4813234b27c3SEric Dumazet if (idx < s_idx) 4814234b27c3SEric Dumazet goto cont; 48154b97efdfSPatrick McHardy if (h > s_h || idx > s_idx) 4816234b27c3SEric Dumazet s_ip_idx = 0; 4817234b27c3SEric Dumazet ip_idx = 0; 4818e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 4819e21e8467SStephen Hemminger if (!idev) 4820234b27c3SEric Dumazet goto cont; 4821234b27c3SEric Dumazet 4822234b27c3SEric Dumazet if (in6_dump_addrs(idev, skb, cb, type, 48237b46a644SDavid S. Miller s_ip_idx, &ip_idx) < 0) 4824234b27c3SEric Dumazet goto done; 48257562f876SPavel Emelianov cont: 48267562f876SPavel Emelianov idx++; 48271da177e4SLinus Torvalds } 4828234b27c3SEric Dumazet } 4829234b27c3SEric Dumazet done: 4830234b27c3SEric Dumazet rcu_read_unlock(); 4831234b27c3SEric Dumazet cb->args[0] = h; 4832234b27c3SEric Dumazet cb->args[1] = idx; 4833234b27c3SEric Dumazet cb->args[2] = ip_idx; 4834234b27c3SEric Dumazet 48351da177e4SLinus Torvalds return skb->len; 48361da177e4SLinus Torvalds } 48371da177e4SLinus Torvalds 48381da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) 48391da177e4SLinus Torvalds { 48401da177e4SLinus Torvalds enum addr_type_t type = UNICAST_ADDR; 4841b854272bSDenis V. Lunev 48421da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 48431da177e4SLinus Torvalds } 48441da177e4SLinus Torvalds 48451da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb) 48461da177e4SLinus Torvalds { 48471da177e4SLinus Torvalds enum addr_type_t type = MULTICAST_ADDR; 4848b854272bSDenis V. Lunev 48491da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 48501da177e4SLinus Torvalds } 48511da177e4SLinus Torvalds 48521da177e4SLinus Torvalds 48531da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb) 48541da177e4SLinus Torvalds { 48551da177e4SLinus Torvalds enum addr_type_t type = ANYCAST_ADDR; 4856b854272bSDenis V. Lunev 48571da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 48581da177e4SLinus Torvalds } 48591da177e4SLinus Torvalds 4860661d2967SThomas Graf static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh) 48616c223828SNoriaki TAKAMIYA { 48623b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 48631b29fc2cSThomas Graf struct ifaddrmsg *ifm; 48641b29fc2cSThomas Graf struct nlattr *tb[IFA_MAX+1]; 4865caeaba79SNicolas Dichtel struct in6_addr *addr = NULL, *peer; 48666c223828SNoriaki TAKAMIYA struct net_device *dev = NULL; 48676c223828SNoriaki TAKAMIYA struct inet6_ifaddr *ifa; 48686c223828SNoriaki TAKAMIYA struct sk_buff *skb; 48696c223828SNoriaki TAKAMIYA int err; 48706c223828SNoriaki TAKAMIYA 48711b29fc2cSThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 48721b29fc2cSThomas Graf if (err < 0) 48731b29fc2cSThomas Graf goto errout; 48746c223828SNoriaki TAKAMIYA 4875caeaba79SNicolas Dichtel addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer); 487663159f29SIan Morris if (!addr) { 48771b29fc2cSThomas Graf err = -EINVAL; 48781b29fc2cSThomas Graf goto errout; 48791b29fc2cSThomas Graf } 48801b29fc2cSThomas Graf 48811b29fc2cSThomas Graf ifm = nlmsg_data(nlh); 48826c223828SNoriaki TAKAMIYA if (ifm->ifa_index) 48836fda7350SBenjamin Thery dev = __dev_get_by_index(net, ifm->ifa_index); 48846c223828SNoriaki TAKAMIYA 4885e21e8467SStephen Hemminger ifa = ipv6_get_ifaddr(net, addr, dev, 1); 4886e21e8467SStephen Hemminger if (!ifa) { 48871b29fc2cSThomas Graf err = -EADDRNOTAVAIL; 48881b29fc2cSThomas Graf goto errout; 48896c223828SNoriaki TAKAMIYA } 48906c223828SNoriaki TAKAMIYA 4891e21e8467SStephen Hemminger skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL); 4892e21e8467SStephen Hemminger if (!skb) { 48931b29fc2cSThomas Graf err = -ENOBUFS; 48941b29fc2cSThomas Graf goto errout_ifa; 48951b29fc2cSThomas Graf } 48961b29fc2cSThomas Graf 489715e47304SEric W. Biederman err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid, 48986c223828SNoriaki TAKAMIYA nlh->nlmsg_seq, RTM_NEWADDR, 0); 489926932566SPatrick McHardy if (err < 0) { 490026932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 490126932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 490226932566SPatrick McHardy kfree_skb(skb); 490326932566SPatrick McHardy goto errout_ifa; 490426932566SPatrick McHardy } 490515e47304SEric W. Biederman err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 49061b29fc2cSThomas Graf errout_ifa: 49076c223828SNoriaki TAKAMIYA in6_ifa_put(ifa); 49081b29fc2cSThomas Graf errout: 49096c223828SNoriaki TAKAMIYA return err; 49106c223828SNoriaki TAKAMIYA } 49116c223828SNoriaki TAKAMIYA 49121da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa) 49131da177e4SLinus Torvalds { 49141da177e4SLinus Torvalds struct sk_buff *skb; 4915c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(ifa->idev->dev); 49165d620266SThomas Graf int err = -ENOBUFS; 49171da177e4SLinus Torvalds 4918f784ad3dSMahesh Bandewar /* Don't send DELADDR notification for TENTATIVE address, 4919f784ad3dSMahesh Bandewar * since NEWADDR notification is sent only after removing 4920f784ad3dSMahesh Bandewar * TENTATIVE flag. 4921f784ad3dSMahesh Bandewar */ 4922f784ad3dSMahesh Bandewar if (ifa->flags & IFA_F_TENTATIVE && event == RTM_DELADDR) 4923f784ad3dSMahesh Bandewar return; 4924f784ad3dSMahesh Bandewar 49250ab6803bSThomas Graf skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC); 492663159f29SIan Morris if (!skb) 49275d620266SThomas Graf goto errout; 49285d620266SThomas Graf 49295d620266SThomas Graf err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0); 493026932566SPatrick McHardy if (err < 0) { 493126932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 493226932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 493326932566SPatrick McHardy kfree_skb(skb); 493426932566SPatrick McHardy goto errout; 493526932566SPatrick McHardy } 49361ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC); 49371ce85fe4SPablo Neira Ayuso return; 49385d620266SThomas Graf errout: 49395d620266SThomas Graf if (err < 0) 49406fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err); 49411da177e4SLinus Torvalds } 49421da177e4SLinus Torvalds 4943b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf, 49441da177e4SLinus Torvalds __s32 *array, int bytes) 49451da177e4SLinus Torvalds { 494604561c1fSThomas Graf BUG_ON(bytes < (DEVCONF_MAX * 4)); 494704561c1fSThomas Graf 49481da177e4SLinus Torvalds memset(array, 0, bytes); 49491da177e4SLinus Torvalds array[DEVCONF_FORWARDING] = cnf->forwarding; 49501da177e4SLinus Torvalds array[DEVCONF_HOPLIMIT] = cnf->hop_limit; 49511da177e4SLinus Torvalds array[DEVCONF_MTU6] = cnf->mtu6; 49521da177e4SLinus Torvalds array[DEVCONF_ACCEPT_RA] = cnf->accept_ra; 49531da177e4SLinus Torvalds array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects; 49541da177e4SLinus Torvalds array[DEVCONF_AUTOCONF] = cnf->autoconf; 49551da177e4SLinus Torvalds array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits; 49561da177e4SLinus Torvalds array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits; 495793908d19SThomas Graf array[DEVCONF_RTR_SOLICIT_INTERVAL] = 495893908d19SThomas Graf jiffies_to_msecs(cnf->rtr_solicit_interval); 4959bd11f074SMaciej Żenczykowski array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] = 4960bd11f074SMaciej Żenczykowski jiffies_to_msecs(cnf->rtr_solicit_max_interval); 496193908d19SThomas Graf array[DEVCONF_RTR_SOLICIT_DELAY] = 496293908d19SThomas Graf jiffies_to_msecs(cnf->rtr_solicit_delay); 49631da177e4SLinus Torvalds array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version; 4964fc4eba58SHannes Frederic Sowa array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] = 4965fc4eba58SHannes Frederic Sowa jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval); 4966fc4eba58SHannes Frederic Sowa array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] = 4967fc4eba58SHannes Frederic Sowa jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval); 49681da177e4SLinus Torvalds array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr; 49691da177e4SLinus Torvalds array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft; 49701da177e4SLinus Torvalds array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft; 49711da177e4SLinus Torvalds array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry; 49721da177e4SLinus Torvalds array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor; 49731da177e4SLinus Torvalds array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses; 497465f5c7c1SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr; 49758013d1d7SHangbin Liu array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit; 4976c4fd30ebSYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo; 4977930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 4978930d6ff2SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref; 497993908d19SThomas Graf array[DEVCONF_RTR_PROBE_INTERVAL] = 498093908d19SThomas Graf jiffies_to_msecs(cnf->rtr_probe_interval); 4981fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 498209c884d4SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen; 498309c884d4SYOSHIFUJI Hideaki #endif 4984930d6ff2SYOSHIFUJI Hideaki #endif 4985fbea49e1SYOSHIFUJI Hideaki array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp; 49860bcbc926SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route; 498795c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 498895c385b4SNeil Horman array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad; 49897fd2561eSErik Kline array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic; 499095c385b4SNeil Horman #endif 49917bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 49927bc570c8SYOSHIFUJI Hideaki array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding; 49937bc570c8SYOSHIFUJI Hideaki #endif 4994778d80beSYOSHIFUJI Hideaki array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6; 49951b34be74SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad; 4996c3faca05SCosmin Ratiu array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao; 49975cb04436SHannes Frederic Sowa array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify; 4998b800c3b9SHannes Frederic Sowa array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc; 4999d9333196SBen Greear array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local; 5000c2943f14SHarout Hedeshian array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu; 500135103d11SAndy Gospodarek array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown; 50023d1bec99SHannes Frederic Sowa /* we omit DEVCONF_STABLE_SECRET for now */ 50033985e8a3SErik Kline array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only; 5004abbc3043SJohannes Berg array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast; 50057a02bf89SJohannes Berg array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na; 5006f1705ec1SDavid Ahern array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down; 50071ababebaSDavid Lebrun array[DEVCONF_SEG6_ENABLED] = cnf->seg6_enabled; 5008bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC 5009bf355b8dSDavid Lebrun array[DEVCONF_SEG6_REQUIRE_HMAC] = cnf->seg6_require_hmac; 5010bf355b8dSDavid Lebrun #endif 5011adc176c5SErik Nordmark array[DEVCONF_ENHANCED_DAD] = cnf->enhanced_dad; 5012d35a00b8SFelix Jia array[DEVCONF_ADDR_GEN_MODE] = cnf->addr_gen_mode; 5013*df789fe7SDavid Forster array[DEVCONF_DISABLE_POLICY] = cnf->disable_policy; 50141da177e4SLinus Torvalds } 50151da177e4SLinus Torvalds 5016b382b191SThomas Graf static inline size_t inet6_ifla6_size(void) 5017b382b191SThomas Graf { 5018b382b191SThomas Graf return nla_total_size(4) /* IFLA_INET6_FLAGS */ 5019b382b191SThomas Graf + nla_total_size(sizeof(struct ifla_cacheinfo)) 5020b382b191SThomas Graf + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */ 5021b382b191SThomas Graf + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */ 5022f53adae4SDaniel Borkmann + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */ 5023f53adae4SDaniel Borkmann + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */ 5024b382b191SThomas Graf } 5025b382b191SThomas Graf 5026339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void) 5027339bf98fSThomas Graf { 5028339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 5029339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 5030339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 5031339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 5032339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 50330344338bSAndy Gospodarek + nla_total_size(1) /* IFLA_OPERSTATE */ 5034b382b191SThomas Graf + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */ 5035339bf98fSThomas Graf } 5036c5396a31SYOSHIFUJI Hideaki 5037be281e55SEric Dumazet static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib, 5038aca05671SJia He int bytes) 50397f7d9a6bSHerbert Xu { 50407f7d9a6bSHerbert Xu int i; 5041aca05671SJia He int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX; 50427f7d9a6bSHerbert Xu BUG_ON(pad < 0); 50437f7d9a6bSHerbert Xu 50447f7d9a6bSHerbert Xu /* Use put_unaligned() because stats may not be aligned for u64. */ 5045aca05671SJia He put_unaligned(ICMP6_MIB_MAX, &stats[0]); 5046aca05671SJia He for (i = 1; i < ICMP6_MIB_MAX; i++) 5047be281e55SEric Dumazet put_unaligned(atomic_long_read(&mib[i]), &stats[i]); 50487f7d9a6bSHerbert Xu 5049aca05671SJia He memset(&stats[ICMP6_MIB_MAX], 0, pad); 50507f7d9a6bSHerbert Xu } 50517f7d9a6bSHerbert Xu 5052698365faSWANG Cong static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib, 5053a3a77372SRaghavendra K T int bytes, size_t syncpoff) 50544ce3c183SEric Dumazet { 5055a3a77372SRaghavendra K T int i, c; 5056a3a77372SRaghavendra K T u64 buff[IPSTATS_MIB_MAX]; 5057a3a77372SRaghavendra K T int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX; 5058a3a77372SRaghavendra K T 50594ce3c183SEric Dumazet BUG_ON(pad < 0); 50604ce3c183SEric Dumazet 5061a3a77372SRaghavendra K T memset(buff, 0, sizeof(buff)); 5062a3a77372SRaghavendra K T buff[0] = IPSTATS_MIB_MAX; 50634ce3c183SEric Dumazet 5064a3a77372SRaghavendra K T for_each_possible_cpu(c) { 5065a3a77372SRaghavendra K T for (i = 1; i < IPSTATS_MIB_MAX; i++) 5066a3a77372SRaghavendra K T buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff); 5067a3a77372SRaghavendra K T } 5068a3a77372SRaghavendra K T 5069a3a77372SRaghavendra K T memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64)); 5070a3a77372SRaghavendra K T memset(&stats[IPSTATS_MIB_MAX], 0, pad); 50714ce3c183SEric Dumazet } 50724ce3c183SEric Dumazet 50737f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype, 50747f7d9a6bSHerbert Xu int bytes) 50757f7d9a6bSHerbert Xu { 50767f7d9a6bSHerbert Xu switch (attrtype) { 50777f7d9a6bSHerbert Xu case IFLA_INET6_STATS: 5078a3a77372SRaghavendra K T __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes, 5079a3a77372SRaghavendra K T offsetof(struct ipstats_mib, syncp)); 50807f7d9a6bSHerbert Xu break; 50817f7d9a6bSHerbert Xu case IFLA_INET6_ICMP6STATS: 5082aca05671SJia He __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes); 50837f7d9a6bSHerbert Xu break; 50847f7d9a6bSHerbert Xu } 50857f7d9a6bSHerbert Xu } 50867f7d9a6bSHerbert Xu 5087d5566fd7SSowmini Varadhan static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev, 5088d5566fd7SSowmini Varadhan u32 ext_filter_mask) 50891da177e4SLinus Torvalds { 5090bf99f1bdSYOSHIFUJI Hideaki struct nlattr *nla; 50911da177e4SLinus Torvalds struct ifla_cacheinfo ci; 50921da177e4SLinus Torvalds 5093c78679e8SDavid S. Miller if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags)) 5094c78679e8SDavid S. Miller goto nla_put_failure; 50951da177e4SLinus Torvalds ci.max_reasm_len = IPV6_MAXPLEN; 509624912420SDavid S. Miller ci.tstamp = cstamp_delta(idev->tstamp); 509724912420SDavid S. Miller ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time); 50981f9248e5SJiri Pirko ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME)); 5099c78679e8SDavid S. Miller if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci)) 5100c78679e8SDavid S. Miller goto nla_put_failure; 5101bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32)); 510263159f29SIan Morris if (!nla) 510304561c1fSThomas Graf goto nla_put_failure; 5104bf99f1bdSYOSHIFUJI Hideaki ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla)); 51051da177e4SLinus Torvalds 5106bf99f1bdSYOSHIFUJI Hideaki /* XXX - MC not implemented */ 5107bf99f1bdSYOSHIFUJI Hideaki 5108d5566fd7SSowmini Varadhan if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS) 5109d5566fd7SSowmini Varadhan return 0; 5110d5566fd7SSowmini Varadhan 5111bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64)); 511263159f29SIan Morris if (!nla) 5113bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 5114bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla)); 5115bf99f1bdSYOSHIFUJI Hideaki 5116bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64)); 511763159f29SIan Morris if (!nla) 5118bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 5119bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla)); 51201da177e4SLinus Torvalds 5121f53adae4SDaniel Borkmann nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr)); 512263159f29SIan Morris if (!nla) 5123f53adae4SDaniel Borkmann goto nla_put_failure; 5124bc91b0f0SJiri Pirko 5125d35a00b8SFelix Jia if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->cnf.addr_gen_mode)) 5126bc91b0f0SJiri Pirko goto nla_put_failure; 5127bc91b0f0SJiri Pirko 5128f53adae4SDaniel Borkmann read_lock_bh(&idev->lock); 5129f53adae4SDaniel Borkmann memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla)); 5130f53adae4SDaniel Borkmann read_unlock_bh(&idev->lock); 5131f53adae4SDaniel Borkmann 5132b382b191SThomas Graf return 0; 5133b382b191SThomas Graf 5134b382b191SThomas Graf nla_put_failure: 5135b382b191SThomas Graf return -EMSGSIZE; 5136b382b191SThomas Graf } 5137b382b191SThomas Graf 5138b1974ed0SArad, Ronen static size_t inet6_get_link_af_size(const struct net_device *dev, 5139b1974ed0SArad, Ronen u32 ext_filter_mask) 5140b382b191SThomas Graf { 5141b382b191SThomas Graf if (!__in6_dev_get(dev)) 5142b382b191SThomas Graf return 0; 5143b382b191SThomas Graf 5144b382b191SThomas Graf return inet6_ifla6_size(); 5145b382b191SThomas Graf } 5146b382b191SThomas Graf 5147d5566fd7SSowmini Varadhan static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev, 5148d5566fd7SSowmini Varadhan u32 ext_filter_mask) 5149b382b191SThomas Graf { 5150b382b191SThomas Graf struct inet6_dev *idev = __in6_dev_get(dev); 5151b382b191SThomas Graf 5152b382b191SThomas Graf if (!idev) 5153b382b191SThomas Graf return -ENODATA; 5154b382b191SThomas Graf 5155d5566fd7SSowmini Varadhan if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0) 5156b382b191SThomas Graf return -EMSGSIZE; 5157b382b191SThomas Graf 5158b382b191SThomas Graf return 0; 5159b382b191SThomas Graf } 5160b382b191SThomas Graf 5161f53adae4SDaniel Borkmann static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token) 5162f53adae4SDaniel Borkmann { 5163f53adae4SDaniel Borkmann struct inet6_ifaddr *ifp; 5164f53adae4SDaniel Borkmann struct net_device *dev = idev->dev; 516547e27d5eSDaniel Borkmann bool clear_token, update_rs = false; 5166dc848292SHannes Frederic Sowa struct in6_addr ll_addr; 5167f53adae4SDaniel Borkmann 5168c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 5169c15b1ccaSHannes Frederic Sowa 517063159f29SIan Morris if (!token) 5171f53adae4SDaniel Borkmann return -EINVAL; 5172f53adae4SDaniel Borkmann if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) 5173f53adae4SDaniel Borkmann return -EINVAL; 5174f53adae4SDaniel Borkmann if (!ipv6_accept_ra(idev)) 5175f53adae4SDaniel Borkmann return -EINVAL; 5176bd11f074SMaciej Żenczykowski if (idev->cnf.rtr_solicits == 0) 5177f53adae4SDaniel Borkmann return -EINVAL; 5178f53adae4SDaniel Borkmann 5179f53adae4SDaniel Borkmann write_lock_bh(&idev->lock); 5180f53adae4SDaniel Borkmann 5181f53adae4SDaniel Borkmann BUILD_BUG_ON(sizeof(token->s6_addr) != 16); 5182f53adae4SDaniel Borkmann memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8); 5183f53adae4SDaniel Borkmann 5184f53adae4SDaniel Borkmann write_unlock_bh(&idev->lock); 5185f53adae4SDaniel Borkmann 518647e27d5eSDaniel Borkmann clear_token = ipv6_addr_any(token); 518747e27d5eSDaniel Borkmann if (clear_token) 518847e27d5eSDaniel Borkmann goto update_lft; 518947e27d5eSDaniel Borkmann 5190dc848292SHannes Frederic Sowa if (!idev->dead && (idev->if_flags & IF_READY) && 5191dc848292SHannes Frederic Sowa !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE | 5192dc848292SHannes Frederic Sowa IFA_F_OPTIMISTIC)) { 5193fc403832SDaniel Borkmann /* If we're not ready, then normal ifup will take care 5194fc403832SDaniel Borkmann * of this. Otherwise, we need to request our rs here. 5195fc403832SDaniel Borkmann */ 5196f53adae4SDaniel Borkmann ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters); 5197fc403832SDaniel Borkmann update_rs = true; 5198fc403832SDaniel Borkmann } 5199f53adae4SDaniel Borkmann 520047e27d5eSDaniel Borkmann update_lft: 5201f53adae4SDaniel Borkmann write_lock_bh(&idev->lock); 5202fc403832SDaniel Borkmann 520377ecaaceSHannes Frederic Sowa if (update_rs) { 5204f53adae4SDaniel Borkmann idev->if_flags |= IF_RS_SENT; 5205bd11f074SMaciej Żenczykowski idev->rs_interval = rfc3315_s14_backoff_init( 5206bd11f074SMaciej Żenczykowski idev->cnf.rtr_solicit_interval); 520777ecaaceSHannes Frederic Sowa idev->rs_probes = 1; 5208bd11f074SMaciej Żenczykowski addrconf_mod_rs_timer(idev, idev->rs_interval); 520977ecaaceSHannes Frederic Sowa } 5210f53adae4SDaniel Borkmann 5211f53adae4SDaniel Borkmann /* Well, that's kinda nasty ... */ 5212f53adae4SDaniel Borkmann list_for_each_entry(ifp, &idev->addr_list, if_list) { 5213f53adae4SDaniel Borkmann spin_lock(&ifp->lock); 5214617fe29dSDaniel Borkmann if (ifp->tokenized) { 5215f53adae4SDaniel Borkmann ifp->valid_lft = 0; 5216f53adae4SDaniel Borkmann ifp->prefered_lft = 0; 5217f53adae4SDaniel Borkmann } 5218f53adae4SDaniel Borkmann spin_unlock(&ifp->lock); 5219f53adae4SDaniel Borkmann } 5220f53adae4SDaniel Borkmann 5221f53adae4SDaniel Borkmann write_unlock_bh(&idev->lock); 5222b2ed64a9SLubomir Rintel inet6_ifinfo_notify(RTM_NEWLINK, idev); 5223c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 5224f53adae4SDaniel Borkmann return 0; 5225f53adae4SDaniel Borkmann } 5226f53adae4SDaniel Borkmann 522711b1f828SDaniel Borkmann static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = { 522811b1f828SDaniel Borkmann [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 }, 522911b1f828SDaniel Borkmann [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) }, 523011b1f828SDaniel Borkmann }; 523111b1f828SDaniel Borkmann 523211b1f828SDaniel Borkmann static int inet6_validate_link_af(const struct net_device *dev, 523311b1f828SDaniel Borkmann const struct nlattr *nla) 523411b1f828SDaniel Borkmann { 523511b1f828SDaniel Borkmann struct nlattr *tb[IFLA_INET6_MAX + 1]; 523611b1f828SDaniel Borkmann 523711b1f828SDaniel Borkmann if (dev && !__in6_dev_get(dev)) 523811b1f828SDaniel Borkmann return -EAFNOSUPPORT; 523911b1f828SDaniel Borkmann 524011b1f828SDaniel Borkmann return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy); 524111b1f828SDaniel Borkmann } 524211b1f828SDaniel Borkmann 5243d35a00b8SFelix Jia static int check_addr_gen_mode(int mode) 5244d35a00b8SFelix Jia { 5245d35a00b8SFelix Jia if (mode != IN6_ADDR_GEN_MODE_EUI64 && 5246d35a00b8SFelix Jia mode != IN6_ADDR_GEN_MODE_NONE && 5247d35a00b8SFelix Jia mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY && 5248d35a00b8SFelix Jia mode != IN6_ADDR_GEN_MODE_RANDOM) 5249d35a00b8SFelix Jia return -EINVAL; 5250d35a00b8SFelix Jia return 1; 5251d35a00b8SFelix Jia } 5252d35a00b8SFelix Jia 5253d35a00b8SFelix Jia static int check_stable_privacy(struct inet6_dev *idev, struct net *net, 5254d35a00b8SFelix Jia int mode) 5255d35a00b8SFelix Jia { 5256d35a00b8SFelix Jia if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY && 5257d35a00b8SFelix Jia !idev->cnf.stable_secret.initialized && 5258d35a00b8SFelix Jia !net->ipv6.devconf_dflt->stable_secret.initialized) 5259d35a00b8SFelix Jia return -EINVAL; 5260d35a00b8SFelix Jia return 1; 5261d35a00b8SFelix Jia } 5262d35a00b8SFelix Jia 5263f53adae4SDaniel Borkmann static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla) 5264f53adae4SDaniel Borkmann { 5265f53adae4SDaniel Borkmann int err = -EINVAL; 5266f53adae4SDaniel Borkmann struct inet6_dev *idev = __in6_dev_get(dev); 5267f53adae4SDaniel Borkmann struct nlattr *tb[IFLA_INET6_MAX + 1]; 5268f53adae4SDaniel Borkmann 5269f53adae4SDaniel Borkmann if (!idev) 5270f53adae4SDaniel Borkmann return -EAFNOSUPPORT; 5271f53adae4SDaniel Borkmann 5272f53adae4SDaniel Borkmann if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0) 5273f53adae4SDaniel Borkmann BUG(); 5274f53adae4SDaniel Borkmann 5275bc91b0f0SJiri Pirko if (tb[IFLA_INET6_TOKEN]) { 5276f53adae4SDaniel Borkmann err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN])); 5277bc91b0f0SJiri Pirko if (err) 5278bc91b0f0SJiri Pirko return err; 5279bc91b0f0SJiri Pirko } 5280bc91b0f0SJiri Pirko 5281bc91b0f0SJiri Pirko if (tb[IFLA_INET6_ADDR_GEN_MODE]) { 5282bc91b0f0SJiri Pirko u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]); 5283bc91b0f0SJiri Pirko 5284d35a00b8SFelix Jia if (check_addr_gen_mode(mode) < 0 || 5285d35a00b8SFelix Jia check_stable_privacy(idev, dev_net(dev), mode) < 0) 5286bc91b0f0SJiri Pirko return -EINVAL; 5287622c81d5SHannes Frederic Sowa 5288d35a00b8SFelix Jia idev->cnf.addr_gen_mode = mode; 5289bc91b0f0SJiri Pirko err = 0; 5290bc91b0f0SJiri Pirko } 5291f53adae4SDaniel Borkmann 5292f53adae4SDaniel Borkmann return err; 5293f53adae4SDaniel Borkmann } 5294f53adae4SDaniel Borkmann 5295b382b191SThomas Graf static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev, 529615e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 5297b382b191SThomas Graf { 5298b382b191SThomas Graf struct net_device *dev = idev->dev; 5299b382b191SThomas Graf struct ifinfomsg *hdr; 5300b382b191SThomas Graf struct nlmsghdr *nlh; 5301b382b191SThomas Graf void *protoinfo; 5302b382b191SThomas Graf 530315e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags); 530463159f29SIan Morris if (!nlh) 5305b382b191SThomas Graf return -EMSGSIZE; 5306b382b191SThomas Graf 5307b382b191SThomas Graf hdr = nlmsg_data(nlh); 5308b382b191SThomas Graf hdr->ifi_family = AF_INET6; 5309b382b191SThomas Graf hdr->__ifi_pad = 0; 5310b382b191SThomas Graf hdr->ifi_type = dev->type; 5311b382b191SThomas Graf hdr->ifi_index = dev->ifindex; 5312b382b191SThomas Graf hdr->ifi_flags = dev_get_flags(dev); 5313b382b191SThomas Graf hdr->ifi_change = 0; 5314b382b191SThomas Graf 5315c78679e8SDavid S. Miller if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 5316c78679e8SDavid S. Miller (dev->addr_len && 5317c78679e8SDavid S. Miller nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 5318c78679e8SDavid S. Miller nla_put_u32(skb, IFLA_MTU, dev->mtu) || 5319a54acb3aSNicolas Dichtel (dev->ifindex != dev_get_iflink(dev) && 53200344338bSAndy Gospodarek nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) || 53210344338bSAndy Gospodarek nla_put_u8(skb, IFLA_OPERSTATE, 53220344338bSAndy Gospodarek netif_running(dev) ? dev->operstate : IF_OPER_DOWN)) 5323c78679e8SDavid S. Miller goto nla_put_failure; 5324b382b191SThomas Graf protoinfo = nla_nest_start(skb, IFLA_PROTINFO); 532563159f29SIan Morris if (!protoinfo) 5326b382b191SThomas Graf goto nla_put_failure; 5327b382b191SThomas Graf 5328d5566fd7SSowmini Varadhan if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0) 5329b382b191SThomas Graf goto nla_put_failure; 5330b382b191SThomas Graf 533104561c1fSThomas Graf nla_nest_end(skb, protoinfo); 5332053c095aSJohannes Berg nlmsg_end(skb, nlh); 5333053c095aSJohannes Berg return 0; 53341da177e4SLinus Torvalds 533504561c1fSThomas Graf nla_put_failure: 533626932566SPatrick McHardy nlmsg_cancel(skb, nlh); 533726932566SPatrick McHardy return -EMSGSIZE; 53381da177e4SLinus Torvalds } 53391da177e4SLinus Torvalds 53401da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 53411da177e4SLinus Torvalds { 53423b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 534384d2697dSEric Dumazet int h, s_h; 5344434a8a58SDavid S. Miller int idx = 0, s_idx; 53451da177e4SLinus Torvalds struct net_device *dev; 53461da177e4SLinus Torvalds struct inet6_dev *idev; 534784d2697dSEric Dumazet struct hlist_head *head; 53481da177e4SLinus Torvalds 534984d2697dSEric Dumazet s_h = cb->args[0]; 535084d2697dSEric Dumazet s_idx = cb->args[1]; 535184d2697dSEric Dumazet 535284d2697dSEric Dumazet rcu_read_lock(); 535384d2697dSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 53547562f876SPavel Emelianov idx = 0; 535584d2697dSEric Dumazet head = &net->dev_index_head[h]; 5356b67bfe0dSSasha Levin hlist_for_each_entry_rcu(dev, head, index_hlist) { 53571da177e4SLinus Torvalds if (idx < s_idx) 53587562f876SPavel Emelianov goto cont; 535984d2697dSEric Dumazet idev = __in6_dev_get(dev); 536084d2697dSEric Dumazet if (!idev) 53617562f876SPavel Emelianov goto cont; 536284d2697dSEric Dumazet if (inet6_fill_ifinfo(skb, idev, 536315e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 536484d2697dSEric Dumazet cb->nlh->nlmsg_seq, 5365053c095aSJohannes Berg RTM_NEWLINK, NLM_F_MULTI) < 0) 536684d2697dSEric Dumazet goto out; 53677562f876SPavel Emelianov cont: 53687562f876SPavel Emelianov idx++; 53691da177e4SLinus Torvalds } 537084d2697dSEric Dumazet } 537184d2697dSEric Dumazet out: 537284d2697dSEric Dumazet rcu_read_unlock(); 537384d2697dSEric Dumazet cb->args[1] = idx; 537484d2697dSEric Dumazet cb->args[0] = h; 53751da177e4SLinus Torvalds 53761da177e4SLinus Torvalds return skb->len; 53771da177e4SLinus Torvalds } 53781da177e4SLinus Torvalds 53791da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev) 53801da177e4SLinus Torvalds { 53811da177e4SLinus Torvalds struct sk_buff *skb; 5382c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 53838d7a76c9SThomas Graf int err = -ENOBUFS; 53841da177e4SLinus Torvalds 5385339bf98fSThomas Graf skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC); 538663159f29SIan Morris if (!skb) 53878d7a76c9SThomas Graf goto errout; 53888d7a76c9SThomas Graf 53898d7a76c9SThomas Graf err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0); 539026932566SPatrick McHardy if (err < 0) { 539126932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */ 539226932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 539326932566SPatrick McHardy kfree_skb(skb); 539426932566SPatrick McHardy goto errout; 539526932566SPatrick McHardy } 53965f75a104SNicolas Dichtel rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC); 53971ce85fe4SPablo Neira Ayuso return; 53988d7a76c9SThomas Graf errout: 53998d7a76c9SThomas Graf if (err < 0) 54005f75a104SNicolas Dichtel rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err); 54011da177e4SLinus Torvalds } 54021da177e4SLinus Torvalds 5403339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void) 5404339bf98fSThomas Graf { 5405339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct prefixmsg)) 5406339bf98fSThomas Graf + nla_total_size(sizeof(struct in6_addr)) 5407339bf98fSThomas Graf + nla_total_size(sizeof(struct prefix_cacheinfo)); 5408339bf98fSThomas Graf } 5409c5396a31SYOSHIFUJI Hideaki 54101da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev, 541115e47304SEric W. Biederman struct prefix_info *pinfo, u32 portid, u32 seq, 5412b6544c0bSJamal Hadi Salim int event, unsigned int flags) 54131da177e4SLinus Torvalds { 54141da177e4SLinus Torvalds struct prefixmsg *pmsg; 54151da177e4SLinus Torvalds struct nlmsghdr *nlh; 54161da177e4SLinus Torvalds struct prefix_cacheinfo ci; 54171da177e4SLinus Torvalds 541815e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags); 541963159f29SIan Morris if (!nlh) 542026932566SPatrick McHardy return -EMSGSIZE; 54216051e2f4SThomas Graf 54226051e2f4SThomas Graf pmsg = nlmsg_data(nlh); 54231da177e4SLinus Torvalds pmsg->prefix_family = AF_INET6; 54248a47077aSPatrick McHardy pmsg->prefix_pad1 = 0; 54258a47077aSPatrick McHardy pmsg->prefix_pad2 = 0; 54261da177e4SLinus Torvalds pmsg->prefix_ifindex = idev->dev->ifindex; 54271da177e4SLinus Torvalds pmsg->prefix_len = pinfo->prefix_len; 54281da177e4SLinus Torvalds pmsg->prefix_type = pinfo->type; 54298a47077aSPatrick McHardy pmsg->prefix_pad3 = 0; 54301da177e4SLinus Torvalds pmsg->prefix_flags = 0; 54311da177e4SLinus Torvalds if (pinfo->onlink) 54321da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_ONLINK; 54331da177e4SLinus Torvalds if (pinfo->autoconf) 54341da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_AUTOCONF; 54351da177e4SLinus Torvalds 5436c78679e8SDavid S. Miller if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix)) 5437c78679e8SDavid S. Miller goto nla_put_failure; 54381da177e4SLinus Torvalds ci.preferred_time = ntohl(pinfo->prefered); 54391da177e4SLinus Torvalds ci.valid_time = ntohl(pinfo->valid); 5440c78679e8SDavid S. Miller if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci)) 5441c78679e8SDavid S. Miller goto nla_put_failure; 5442053c095aSJohannes Berg nlmsg_end(skb, nlh); 5443053c095aSJohannes Berg return 0; 54441da177e4SLinus Torvalds 54456051e2f4SThomas Graf nla_put_failure: 544626932566SPatrick McHardy nlmsg_cancel(skb, nlh); 544726932566SPatrick McHardy return -EMSGSIZE; 54481da177e4SLinus Torvalds } 54491da177e4SLinus Torvalds 54501da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev, 54511da177e4SLinus Torvalds struct prefix_info *pinfo) 54521da177e4SLinus Torvalds { 54531da177e4SLinus Torvalds struct sk_buff *skb; 5454c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 54558c384bfaSThomas Graf int err = -ENOBUFS; 54561da177e4SLinus Torvalds 5457339bf98fSThomas Graf skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC); 545863159f29SIan Morris if (!skb) 54598c384bfaSThomas Graf goto errout; 54608c384bfaSThomas Graf 54618c384bfaSThomas Graf err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0); 546226932566SPatrick McHardy if (err < 0) { 546326932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */ 546426932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 546526932566SPatrick McHardy kfree_skb(skb); 546626932566SPatrick McHardy goto errout; 546726932566SPatrick McHardy } 54681ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC); 54691ce85fe4SPablo Neira Ayuso return; 54708c384bfaSThomas Graf errout: 54718c384bfaSThomas Graf if (err < 0) 54726fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err); 54731da177e4SLinus Torvalds } 54741da177e4SLinus Torvalds 54751da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 54761da177e4SLinus Torvalds { 547763998ac2SNicolas Dichtel struct net *net = dev_net(ifp->idev->dev); 547863998ac2SNicolas Dichtel 5479c15b1ccaSHannes Frederic Sowa if (event) 5480c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 5481c15b1ccaSHannes Frederic Sowa 54821da177e4SLinus Torvalds inet6_ifa_notify(event ? : RTM_NEWADDR, ifp); 54831da177e4SLinus Torvalds 54841da177e4SLinus Torvalds switch (event) { 54851da177e4SLinus Torvalds case RTM_NEWADDR: 548695c385b4SNeil Horman /* 548795c385b4SNeil Horman * If the address was optimistic 548895c385b4SNeil Horman * we inserted the route at the start of 548995c385b4SNeil Horman * our DAD process, so we don't need 549095c385b4SNeil Horman * to do it again 549195c385b4SNeil Horman */ 549295c385b4SNeil Horman if (!(ifp->rt->rt6i_node)) 549340e22e8fSThomas Graf ip6_ins_rt(ifp->rt); 54941da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 54951da177e4SLinus Torvalds addrconf_join_anycast(ifp); 54967996c799SCong Wang if (!ipv6_addr_any(&ifp->peer_addr)) 5497caeaba79SNicolas Dichtel addrconf_prefix_route(&ifp->peer_addr, 128, 5498caeaba79SNicolas Dichtel ifp->idev->dev, 0, 0); 54991da177e4SLinus Torvalds break; 55001da177e4SLinus Torvalds case RTM_DELADDR: 55011da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 55021da177e4SLinus Torvalds addrconf_leave_anycast(ifp); 55031da177e4SLinus Torvalds addrconf_leave_solict(ifp->idev, &ifp->addr); 55047996c799SCong Wang if (!ipv6_addr_any(&ifp->peer_addr)) { 5505caeaba79SNicolas Dichtel struct rt6_info *rt; 5506caeaba79SNicolas Dichtel 5507e7478dfcSNicolas Dichtel rt = addrconf_get_prefix_route(&ifp->peer_addr, 128, 5508e7478dfcSNicolas Dichtel ifp->idev->dev, 0, 0); 55098e3d5be7SMartin KaFai Lau if (rt) 55108e3d5be7SMartin KaFai Lau ip6_del_rt(rt); 5511caeaba79SNicolas Dichtel } 551238bd10c4SDavid Ahern if (ifp->rt) { 5513d8d1f30bSChangli Gao dst_hold(&ifp->rt->dst); 55148e3d5be7SMartin KaFai Lau ip6_del_rt(ifp->rt); 551538bd10c4SDavid Ahern } 5516705f1c86SHannes Frederic Sowa rt_genid_bump_ipv6(net); 55171da177e4SLinus Torvalds break; 55181da177e4SLinus Torvalds } 551963998ac2SNicolas Dichtel atomic_inc(&net->ipv6.dev_addr_genid); 55201da177e4SLinus Torvalds } 55211da177e4SLinus Torvalds 55221da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 55231da177e4SLinus Torvalds { 55248814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 55251da177e4SLinus Torvalds if (likely(ifp->idev->dead == 0)) 55261da177e4SLinus Torvalds __ipv6_ifa_notify(event, ifp); 55278814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 55281da177e4SLinus Torvalds } 55291da177e4SLinus Torvalds 55301da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 55311da177e4SLinus Torvalds 55321da177e4SLinus Torvalds static 5533fe2c6338SJoe Perches int addrconf_sysctl_forward(struct ctl_table *ctl, int write, 55341da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 55351da177e4SLinus Torvalds { 55361da177e4SLinus Torvalds int *valp = ctl->data; 55371da177e4SLinus Torvalds int val = *valp; 553888af182eSEric W. Biederman loff_t pos = *ppos; 5539fe2c6338SJoe Perches struct ctl_table lctl; 55401da177e4SLinus Torvalds int ret; 55411da177e4SLinus Torvalds 5542013d97e9SFrancesco Ruggeri /* 5543013d97e9SFrancesco Ruggeri * ctl->data points to idev->cnf.forwarding, we should 5544013d97e9SFrancesco Ruggeri * not modify it until we get the rtnl lock. 5545013d97e9SFrancesco Ruggeri */ 5546013d97e9SFrancesco Ruggeri lctl = *ctl; 5547013d97e9SFrancesco Ruggeri lctl.data = &val; 5548013d97e9SFrancesco Ruggeri 5549013d97e9SFrancesco Ruggeri ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 55501da177e4SLinus Torvalds 5551c8fecf22SPavel Emelyanov if (write) 5552b325fddbSStephen Hemminger ret = addrconf_fixup_forwarding(ctl, valp, val); 555388af182eSEric W. Biederman if (ret) 555488af182eSEric W. Biederman *ppos = pos; 55551da177e4SLinus Torvalds return ret; 55561da177e4SLinus Torvalds } 55571da177e4SLinus Torvalds 555877751427SMarcelo Leitner static 555977751427SMarcelo Leitner int addrconf_sysctl_mtu(struct ctl_table *ctl, int write, 556077751427SMarcelo Leitner void __user *buffer, size_t *lenp, loff_t *ppos) 556177751427SMarcelo Leitner { 556277751427SMarcelo Leitner struct inet6_dev *idev = ctl->extra1; 556377751427SMarcelo Leitner int min_mtu = IPV6_MIN_MTU; 556477751427SMarcelo Leitner struct ctl_table lctl; 556577751427SMarcelo Leitner 556677751427SMarcelo Leitner lctl = *ctl; 556777751427SMarcelo Leitner lctl.extra1 = &min_mtu; 556877751427SMarcelo Leitner lctl.extra2 = idev ? &idev->dev->mtu : NULL; 556977751427SMarcelo Leitner 557077751427SMarcelo Leitner return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos); 557177751427SMarcelo Leitner } 557277751427SMarcelo Leitner 557356d417b1SBrian Haley static void dev_disable_change(struct inet6_dev *idev) 557456d417b1SBrian Haley { 557575538c2bSCong Wang struct netdev_notifier_info info; 557675538c2bSCong Wang 557756d417b1SBrian Haley if (!idev || !idev->dev) 557856d417b1SBrian Haley return; 557956d417b1SBrian Haley 558075538c2bSCong Wang netdev_notifier_info_init(&info, idev->dev); 558156d417b1SBrian Haley if (idev->cnf.disable_ipv6) 558275538c2bSCong Wang addrconf_notify(NULL, NETDEV_DOWN, &info); 558356d417b1SBrian Haley else 558475538c2bSCong Wang addrconf_notify(NULL, NETDEV_UP, &info); 558556d417b1SBrian Haley } 558656d417b1SBrian Haley 558756d417b1SBrian Haley static void addrconf_disable_change(struct net *net, __s32 newf) 558856d417b1SBrian Haley { 558956d417b1SBrian Haley struct net_device *dev; 559056d417b1SBrian Haley struct inet6_dev *idev; 559156d417b1SBrian Haley 559203e4deffSKefeng Wang for_each_netdev(net, dev) { 559356d417b1SBrian Haley idev = __in6_dev_get(dev); 559456d417b1SBrian Haley if (idev) { 559556d417b1SBrian Haley int changed = (!idev->cnf.disable_ipv6) ^ (!newf); 559656d417b1SBrian Haley idev->cnf.disable_ipv6 = newf; 559756d417b1SBrian Haley if (changed) 559856d417b1SBrian Haley dev_disable_change(idev); 559956d417b1SBrian Haley } 560056d417b1SBrian Haley } 560156d417b1SBrian Haley } 560256d417b1SBrian Haley 5603013d97e9SFrancesco Ruggeri static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf) 560456d417b1SBrian Haley { 560556d417b1SBrian Haley struct net *net; 5606013d97e9SFrancesco Ruggeri int old; 5607013d97e9SFrancesco Ruggeri 5608013d97e9SFrancesco Ruggeri if (!rtnl_trylock()) 5609013d97e9SFrancesco Ruggeri return restart_syscall(); 561056d417b1SBrian Haley 561156d417b1SBrian Haley net = (struct net *)table->extra2; 5612013d97e9SFrancesco Ruggeri old = *p; 5613013d97e9SFrancesco Ruggeri *p = newf; 561456d417b1SBrian Haley 5615013d97e9SFrancesco Ruggeri if (p == &net->ipv6.devconf_dflt->disable_ipv6) { 5616013d97e9SFrancesco Ruggeri rtnl_unlock(); 561756d417b1SBrian Haley return 0; 561888af182eSEric W. Biederman } 561956d417b1SBrian Haley 562056d417b1SBrian Haley if (p == &net->ipv6.devconf_all->disable_ipv6) { 562156d417b1SBrian Haley net->ipv6.devconf_dflt->disable_ipv6 = newf; 562256d417b1SBrian Haley addrconf_disable_change(net, newf); 5623013d97e9SFrancesco Ruggeri } else if ((!newf) ^ (!old)) 562456d417b1SBrian Haley dev_disable_change((struct inet6_dev *)table->extra1); 562556d417b1SBrian Haley 562656d417b1SBrian Haley rtnl_unlock(); 562756d417b1SBrian Haley return 0; 562856d417b1SBrian Haley } 562956d417b1SBrian Haley 563056d417b1SBrian Haley static 5631fe2c6338SJoe Perches int addrconf_sysctl_disable(struct ctl_table *ctl, int write, 563256d417b1SBrian Haley void __user *buffer, size_t *lenp, loff_t *ppos) 563356d417b1SBrian Haley { 563456d417b1SBrian Haley int *valp = ctl->data; 563556d417b1SBrian Haley int val = *valp; 563688af182eSEric W. Biederman loff_t pos = *ppos; 5637fe2c6338SJoe Perches struct ctl_table lctl; 563856d417b1SBrian Haley int ret; 563956d417b1SBrian Haley 5640013d97e9SFrancesco Ruggeri /* 5641013d97e9SFrancesco Ruggeri * ctl->data points to idev->cnf.disable_ipv6, we should 5642013d97e9SFrancesco Ruggeri * not modify it until we get the rtnl lock. 5643013d97e9SFrancesco Ruggeri */ 5644013d97e9SFrancesco Ruggeri lctl = *ctl; 5645013d97e9SFrancesco Ruggeri lctl.data = &val; 5646013d97e9SFrancesco Ruggeri 5647013d97e9SFrancesco Ruggeri ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 564856d417b1SBrian Haley 564956d417b1SBrian Haley if (write) 565056d417b1SBrian Haley ret = addrconf_disable_ipv6(ctl, valp, val); 565188af182eSEric W. Biederman if (ret) 565288af182eSEric W. Biederman *ppos = pos; 565356d417b1SBrian Haley return ret; 565456d417b1SBrian Haley } 565556d417b1SBrian Haley 5656c92d5491Sstephen hemminger static 5657c92d5491Sstephen hemminger int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write, 5658c92d5491Sstephen hemminger void __user *buffer, size_t *lenp, loff_t *ppos) 5659c92d5491Sstephen hemminger { 5660c92d5491Sstephen hemminger int *valp = ctl->data; 5661c92d5491Sstephen hemminger int ret; 5662c92d5491Sstephen hemminger int old, new; 5663c92d5491Sstephen hemminger 5664c92d5491Sstephen hemminger old = *valp; 5665c92d5491Sstephen hemminger ret = proc_dointvec(ctl, write, buffer, lenp, ppos); 5666c92d5491Sstephen hemminger new = *valp; 5667c92d5491Sstephen hemminger 5668c92d5491Sstephen hemminger if (write && old != new) { 5669c92d5491Sstephen hemminger struct net *net = ctl->extra2; 5670c92d5491Sstephen hemminger 5671c92d5491Sstephen hemminger if (!rtnl_trylock()) 5672c92d5491Sstephen hemminger return restart_syscall(); 5673c92d5491Sstephen hemminger 5674c92d5491Sstephen hemminger if (valp == &net->ipv6.devconf_dflt->proxy_ndp) 5675c92d5491Sstephen hemminger inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH, 5676c92d5491Sstephen hemminger NETCONFA_IFINDEX_DEFAULT, 5677c92d5491Sstephen hemminger net->ipv6.devconf_dflt); 5678c92d5491Sstephen hemminger else if (valp == &net->ipv6.devconf_all->proxy_ndp) 5679c92d5491Sstephen hemminger inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH, 5680c92d5491Sstephen hemminger NETCONFA_IFINDEX_ALL, 5681c92d5491Sstephen hemminger net->ipv6.devconf_all); 5682c92d5491Sstephen hemminger else { 5683c92d5491Sstephen hemminger struct inet6_dev *idev = ctl->extra1; 5684c92d5491Sstephen hemminger 5685c92d5491Sstephen hemminger inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH, 5686c92d5491Sstephen hemminger idev->dev->ifindex, 5687c92d5491Sstephen hemminger &idev->cnf); 5688c92d5491Sstephen hemminger } 5689c92d5491Sstephen hemminger rtnl_unlock(); 5690c92d5491Sstephen hemminger } 5691c92d5491Sstephen hemminger 5692c92d5491Sstephen hemminger return ret; 5693c92d5491Sstephen hemminger } 5694c92d5491Sstephen hemminger 5695d35a00b8SFelix Jia static int addrconf_sysctl_addr_gen_mode(struct ctl_table *ctl, int write, 5696d35a00b8SFelix Jia void __user *buffer, size_t *lenp, 5697d35a00b8SFelix Jia loff_t *ppos) 5698d35a00b8SFelix Jia { 5699d35a00b8SFelix Jia int ret = 0; 5700d35a00b8SFelix Jia int new_val; 5701d35a00b8SFelix Jia struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1; 5702d35a00b8SFelix Jia struct net *net = (struct net *)ctl->extra2; 5703d35a00b8SFelix Jia 57048c171d6cSFelix Jia if (!rtnl_trylock()) 57058c171d6cSFelix Jia return restart_syscall(); 57068c171d6cSFelix Jia 5707d35a00b8SFelix Jia ret = proc_dointvec(ctl, write, buffer, lenp, ppos); 5708d35a00b8SFelix Jia 5709d35a00b8SFelix Jia if (write) { 5710d35a00b8SFelix Jia new_val = *((int *)ctl->data); 5711d35a00b8SFelix Jia 57128c171d6cSFelix Jia if (check_addr_gen_mode(new_val) < 0) { 57138c171d6cSFelix Jia ret = -EINVAL; 57148c171d6cSFelix Jia goto out; 57158c171d6cSFelix Jia } 5716d35a00b8SFelix Jia 5717d35a00b8SFelix Jia /* request for default */ 5718d35a00b8SFelix Jia if (&net->ipv6.devconf_dflt->addr_gen_mode == ctl->data) { 5719d35a00b8SFelix Jia ipv6_devconf_dflt.addr_gen_mode = new_val; 5720d35a00b8SFelix Jia 5721d35a00b8SFelix Jia /* request for individual net device */ 5722d35a00b8SFelix Jia } else { 5723d35a00b8SFelix Jia if (!idev) 57248c171d6cSFelix Jia goto out; 5725d35a00b8SFelix Jia 57268c171d6cSFelix Jia if (check_stable_privacy(idev, net, new_val) < 0) { 57278c171d6cSFelix Jia ret = -EINVAL; 57288c171d6cSFelix Jia goto out; 57298c171d6cSFelix Jia } 5730d35a00b8SFelix Jia 5731d35a00b8SFelix Jia if (idev->cnf.addr_gen_mode != new_val) { 5732d35a00b8SFelix Jia idev->cnf.addr_gen_mode = new_val; 5733d35a00b8SFelix Jia addrconf_dev_config(idev->dev); 57348c171d6cSFelix Jia } 57358c171d6cSFelix Jia } 57368c171d6cSFelix Jia } 57378c171d6cSFelix Jia 57388c171d6cSFelix Jia out: 5739d35a00b8SFelix Jia rtnl_unlock(); 5740d35a00b8SFelix Jia 5741d35a00b8SFelix Jia return ret; 5742d35a00b8SFelix Jia } 5743d35a00b8SFelix Jia 57443d1bec99SHannes Frederic Sowa static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write, 57453d1bec99SHannes Frederic Sowa void __user *buffer, size_t *lenp, 57463d1bec99SHannes Frederic Sowa loff_t *ppos) 57473d1bec99SHannes Frederic Sowa { 57483d1bec99SHannes Frederic Sowa int err; 57493d1bec99SHannes Frederic Sowa struct in6_addr addr; 57503d1bec99SHannes Frederic Sowa char str[IPV6_MAX_STRLEN]; 57513d1bec99SHannes Frederic Sowa struct ctl_table lctl = *ctl; 5752622c81d5SHannes Frederic Sowa struct net *net = ctl->extra2; 57533d1bec99SHannes Frederic Sowa struct ipv6_stable_secret *secret = ctl->data; 57543d1bec99SHannes Frederic Sowa 5755622c81d5SHannes Frederic Sowa if (&net->ipv6.devconf_all->stable_secret == ctl->data) 5756622c81d5SHannes Frederic Sowa return -EIO; 5757622c81d5SHannes Frederic Sowa 57583d1bec99SHannes Frederic Sowa lctl.maxlen = IPV6_MAX_STRLEN; 57593d1bec99SHannes Frederic Sowa lctl.data = str; 57603d1bec99SHannes Frederic Sowa 57613d1bec99SHannes Frederic Sowa if (!rtnl_trylock()) 57623d1bec99SHannes Frederic Sowa return restart_syscall(); 57633d1bec99SHannes Frederic Sowa 57643d1bec99SHannes Frederic Sowa if (!write && !secret->initialized) { 57653d1bec99SHannes Frederic Sowa err = -EIO; 57663d1bec99SHannes Frederic Sowa goto out; 57673d1bec99SHannes Frederic Sowa } 57683d1bec99SHannes Frederic Sowa 57695449a5caSWANG Cong err = snprintf(str, sizeof(str), "%pI6", &secret->secret); 57703d1bec99SHannes Frederic Sowa if (err >= sizeof(str)) { 57713d1bec99SHannes Frederic Sowa err = -EIO; 57723d1bec99SHannes Frederic Sowa goto out; 57733d1bec99SHannes Frederic Sowa } 57743d1bec99SHannes Frederic Sowa 57753d1bec99SHannes Frederic Sowa err = proc_dostring(&lctl, write, buffer, lenp, ppos); 57763d1bec99SHannes Frederic Sowa if (err || !write) 57773d1bec99SHannes Frederic Sowa goto out; 57783d1bec99SHannes Frederic Sowa 57793d1bec99SHannes Frederic Sowa if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) { 57803d1bec99SHannes Frederic Sowa err = -EIO; 57813d1bec99SHannes Frederic Sowa goto out; 57823d1bec99SHannes Frederic Sowa } 57833d1bec99SHannes Frederic Sowa 57843d1bec99SHannes Frederic Sowa secret->initialized = true; 57853d1bec99SHannes Frederic Sowa secret->secret = addr; 57863d1bec99SHannes Frederic Sowa 5787622c81d5SHannes Frederic Sowa if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) { 5788622c81d5SHannes Frederic Sowa struct net_device *dev; 5789622c81d5SHannes Frederic Sowa 5790622c81d5SHannes Frederic Sowa for_each_netdev(net, dev) { 5791622c81d5SHannes Frederic Sowa struct inet6_dev *idev = __in6_dev_get(dev); 5792622c81d5SHannes Frederic Sowa 5793622c81d5SHannes Frederic Sowa if (idev) { 5794d35a00b8SFelix Jia idev->cnf.addr_gen_mode = 5795622c81d5SHannes Frederic Sowa IN6_ADDR_GEN_MODE_STABLE_PRIVACY; 5796622c81d5SHannes Frederic Sowa } 5797622c81d5SHannes Frederic Sowa } 5798622c81d5SHannes Frederic Sowa } else { 5799622c81d5SHannes Frederic Sowa struct inet6_dev *idev = ctl->extra1; 5800622c81d5SHannes Frederic Sowa 5801d35a00b8SFelix Jia idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY; 5802622c81d5SHannes Frederic Sowa } 5803622c81d5SHannes Frederic Sowa 58043d1bec99SHannes Frederic Sowa out: 58053d1bec99SHannes Frederic Sowa rtnl_unlock(); 58063d1bec99SHannes Frederic Sowa 58073d1bec99SHannes Frederic Sowa return err; 58083d1bec99SHannes Frederic Sowa } 5809c92d5491Sstephen hemminger 581035103d11SAndy Gospodarek static 581135103d11SAndy Gospodarek int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl, 581235103d11SAndy Gospodarek int write, 581335103d11SAndy Gospodarek void __user *buffer, 581435103d11SAndy Gospodarek size_t *lenp, 581535103d11SAndy Gospodarek loff_t *ppos) 581635103d11SAndy Gospodarek { 581735103d11SAndy Gospodarek int *valp = ctl->data; 581835103d11SAndy Gospodarek int val = *valp; 581935103d11SAndy Gospodarek loff_t pos = *ppos; 582035103d11SAndy Gospodarek struct ctl_table lctl; 582135103d11SAndy Gospodarek int ret; 582235103d11SAndy Gospodarek 582335103d11SAndy Gospodarek /* ctl->data points to idev->cnf.ignore_routes_when_linkdown 582435103d11SAndy Gospodarek * we should not modify it until we get the rtnl lock. 582535103d11SAndy Gospodarek */ 582635103d11SAndy Gospodarek lctl = *ctl; 582735103d11SAndy Gospodarek lctl.data = &val; 582835103d11SAndy Gospodarek 582935103d11SAndy Gospodarek ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 583035103d11SAndy Gospodarek 583135103d11SAndy Gospodarek if (write) 583235103d11SAndy Gospodarek ret = addrconf_fixup_linkdown(ctl, valp, val); 583335103d11SAndy Gospodarek if (ret) 583435103d11SAndy Gospodarek *ppos = pos; 583535103d11SAndy Gospodarek return ret; 583635103d11SAndy Gospodarek } 583735103d11SAndy Gospodarek 5838*df789fe7SDavid Forster static 5839*df789fe7SDavid Forster void addrconf_set_nopolicy(struct rt6_info *rt, int action) 5840*df789fe7SDavid Forster { 5841*df789fe7SDavid Forster if (rt) { 5842*df789fe7SDavid Forster if (action) 5843*df789fe7SDavid Forster rt->dst.flags |= DST_NOPOLICY; 5844*df789fe7SDavid Forster else 5845*df789fe7SDavid Forster rt->dst.flags &= ~DST_NOPOLICY; 5846*df789fe7SDavid Forster } 5847*df789fe7SDavid Forster } 5848*df789fe7SDavid Forster 5849*df789fe7SDavid Forster static 5850*df789fe7SDavid Forster void addrconf_disable_policy_idev(struct inet6_dev *idev, int val) 5851*df789fe7SDavid Forster { 5852*df789fe7SDavid Forster struct inet6_ifaddr *ifa; 5853*df789fe7SDavid Forster 5854*df789fe7SDavid Forster read_lock_bh(&idev->lock); 5855*df789fe7SDavid Forster list_for_each_entry(ifa, &idev->addr_list, if_list) { 5856*df789fe7SDavid Forster spin_lock(&ifa->lock); 5857*df789fe7SDavid Forster if (ifa->rt) { 5858*df789fe7SDavid Forster struct rt6_info *rt = ifa->rt; 5859*df789fe7SDavid Forster struct fib6_table *table = rt->rt6i_table; 5860*df789fe7SDavid Forster int cpu; 5861*df789fe7SDavid Forster 5862*df789fe7SDavid Forster read_lock(&table->tb6_lock); 5863*df789fe7SDavid Forster addrconf_set_nopolicy(ifa->rt, val); 5864*df789fe7SDavid Forster if (rt->rt6i_pcpu) { 5865*df789fe7SDavid Forster for_each_possible_cpu(cpu) { 5866*df789fe7SDavid Forster struct rt6_info **rtp; 5867*df789fe7SDavid Forster 5868*df789fe7SDavid Forster rtp = per_cpu_ptr(rt->rt6i_pcpu, cpu); 5869*df789fe7SDavid Forster addrconf_set_nopolicy(*rtp, val); 5870*df789fe7SDavid Forster } 5871*df789fe7SDavid Forster } 5872*df789fe7SDavid Forster read_unlock(&table->tb6_lock); 5873*df789fe7SDavid Forster } 5874*df789fe7SDavid Forster spin_unlock(&ifa->lock); 5875*df789fe7SDavid Forster } 5876*df789fe7SDavid Forster read_unlock_bh(&idev->lock); 5877*df789fe7SDavid Forster } 5878*df789fe7SDavid Forster 5879*df789fe7SDavid Forster static 5880*df789fe7SDavid Forster int addrconf_disable_policy(struct ctl_table *ctl, int *valp, int val) 5881*df789fe7SDavid Forster { 5882*df789fe7SDavid Forster struct inet6_dev *idev; 5883*df789fe7SDavid Forster struct net *net; 5884*df789fe7SDavid Forster 5885*df789fe7SDavid Forster if (!rtnl_trylock()) 5886*df789fe7SDavid Forster return restart_syscall(); 5887*df789fe7SDavid Forster 5888*df789fe7SDavid Forster *valp = val; 5889*df789fe7SDavid Forster 5890*df789fe7SDavid Forster net = (struct net *)ctl->extra2; 5891*df789fe7SDavid Forster if (valp == &net->ipv6.devconf_dflt->disable_policy) { 5892*df789fe7SDavid Forster rtnl_unlock(); 5893*df789fe7SDavid Forster return 0; 5894*df789fe7SDavid Forster } 5895*df789fe7SDavid Forster 5896*df789fe7SDavid Forster if (valp == &net->ipv6.devconf_all->disable_policy) { 5897*df789fe7SDavid Forster struct net_device *dev; 5898*df789fe7SDavid Forster 5899*df789fe7SDavid Forster for_each_netdev(net, dev) { 5900*df789fe7SDavid Forster idev = __in6_dev_get(dev); 5901*df789fe7SDavid Forster if (idev) 5902*df789fe7SDavid Forster addrconf_disable_policy_idev(idev, val); 5903*df789fe7SDavid Forster } 5904*df789fe7SDavid Forster } else { 5905*df789fe7SDavid Forster idev = (struct inet6_dev *)ctl->extra1; 5906*df789fe7SDavid Forster addrconf_disable_policy_idev(idev, val); 5907*df789fe7SDavid Forster } 5908*df789fe7SDavid Forster 5909*df789fe7SDavid Forster rtnl_unlock(); 5910*df789fe7SDavid Forster return 0; 5911*df789fe7SDavid Forster } 5912*df789fe7SDavid Forster 5913*df789fe7SDavid Forster static 5914*df789fe7SDavid Forster int addrconf_sysctl_disable_policy(struct ctl_table *ctl, int write, 5915*df789fe7SDavid Forster void __user *buffer, size_t *lenp, 5916*df789fe7SDavid Forster loff_t *ppos) 5917*df789fe7SDavid Forster { 5918*df789fe7SDavid Forster int *valp = ctl->data; 5919*df789fe7SDavid Forster int val = *valp; 5920*df789fe7SDavid Forster loff_t pos = *ppos; 5921*df789fe7SDavid Forster struct ctl_table lctl; 5922*df789fe7SDavid Forster int ret; 5923*df789fe7SDavid Forster 5924*df789fe7SDavid Forster lctl = *ctl; 5925*df789fe7SDavid Forster lctl.data = &val; 5926*df789fe7SDavid Forster ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 5927*df789fe7SDavid Forster 5928*df789fe7SDavid Forster if (write && (*valp != val)) 5929*df789fe7SDavid Forster ret = addrconf_disable_policy(ctl, valp, val); 5930*df789fe7SDavid Forster 5931*df789fe7SDavid Forster if (ret) 5932*df789fe7SDavid Forster *ppos = pos; 5933*df789fe7SDavid Forster 5934*df789fe7SDavid Forster return ret; 5935*df789fe7SDavid Forster } 5936*df789fe7SDavid Forster 5937cb4a4c69SMaciej Żenczykowski static int minus_one = -1; 5938cb9e684eSMaciej Żenczykowski static const int one = 1; 5939cb9e684eSMaciej Żenczykowski static const int two_five_five = 255; 5940cb9e684eSMaciej Żenczykowski 5941607ea7cdSKonstantin Khlebnikov static const struct ctl_table addrconf_sysctl[] = { 59421da177e4SLinus Torvalds { 59431da177e4SLinus Torvalds .procname = "forwarding", 59441da177e4SLinus Torvalds .data = &ipv6_devconf.forwarding, 59451da177e4SLinus Torvalds .maxlen = sizeof(int), 59461da177e4SLinus Torvalds .mode = 0644, 59476d9f239aSAlexey Dobriyan .proc_handler = addrconf_sysctl_forward, 59481da177e4SLinus Torvalds }, 59491da177e4SLinus Torvalds { 59501da177e4SLinus Torvalds .procname = "hop_limit", 59511da177e4SLinus Torvalds .data = &ipv6_devconf.hop_limit, 59521da177e4SLinus Torvalds .maxlen = sizeof(int), 59531da177e4SLinus Torvalds .mode = 0644, 5954cb9e684eSMaciej Żenczykowski .proc_handler = proc_dointvec_minmax, 5955cb9e684eSMaciej Żenczykowski .extra1 = (void *)&one, 5956cb9e684eSMaciej Żenczykowski .extra2 = (void *)&two_five_five, 59571da177e4SLinus Torvalds }, 59581da177e4SLinus Torvalds { 59591da177e4SLinus Torvalds .procname = "mtu", 59601da177e4SLinus Torvalds .data = &ipv6_devconf.mtu6, 59611da177e4SLinus Torvalds .maxlen = sizeof(int), 59621da177e4SLinus Torvalds .mode = 0644, 596377751427SMarcelo Leitner .proc_handler = addrconf_sysctl_mtu, 59641da177e4SLinus Torvalds }, 59651da177e4SLinus Torvalds { 59661da177e4SLinus Torvalds .procname = "accept_ra", 59671da177e4SLinus Torvalds .data = &ipv6_devconf.accept_ra, 59681da177e4SLinus Torvalds .maxlen = sizeof(int), 59691da177e4SLinus Torvalds .mode = 0644, 59706d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 59711da177e4SLinus Torvalds }, 59721da177e4SLinus Torvalds { 59731da177e4SLinus Torvalds .procname = "accept_redirects", 59741da177e4SLinus Torvalds .data = &ipv6_devconf.accept_redirects, 59751da177e4SLinus Torvalds .maxlen = sizeof(int), 59761da177e4SLinus Torvalds .mode = 0644, 59776d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 59781da177e4SLinus Torvalds }, 59791da177e4SLinus Torvalds { 59801da177e4SLinus Torvalds .procname = "autoconf", 59811da177e4SLinus Torvalds .data = &ipv6_devconf.autoconf, 59821da177e4SLinus Torvalds .maxlen = sizeof(int), 59831da177e4SLinus Torvalds .mode = 0644, 59846d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 59851da177e4SLinus Torvalds }, 59861da177e4SLinus Torvalds { 59871da177e4SLinus Torvalds .procname = "dad_transmits", 59881da177e4SLinus Torvalds .data = &ipv6_devconf.dad_transmits, 59891da177e4SLinus Torvalds .maxlen = sizeof(int), 59901da177e4SLinus Torvalds .mode = 0644, 59916d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 59921da177e4SLinus Torvalds }, 59931da177e4SLinus Torvalds { 59941da177e4SLinus Torvalds .procname = "router_solicitations", 59951da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicits, 59961da177e4SLinus Torvalds .maxlen = sizeof(int), 59971da177e4SLinus Torvalds .mode = 0644, 5998cb4a4c69SMaciej Żenczykowski .proc_handler = proc_dointvec_minmax, 5999cb4a4c69SMaciej Żenczykowski .extra1 = &minus_one, 60001da177e4SLinus Torvalds }, 60011da177e4SLinus Torvalds { 60021da177e4SLinus Torvalds .procname = "router_solicitation_interval", 60031da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_interval, 60041da177e4SLinus Torvalds .maxlen = sizeof(int), 60051da177e4SLinus Torvalds .mode = 0644, 60066d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 60071da177e4SLinus Torvalds }, 60081da177e4SLinus Torvalds { 6009bd11f074SMaciej Żenczykowski .procname = "router_solicitation_max_interval", 6010bd11f074SMaciej Żenczykowski .data = &ipv6_devconf.rtr_solicit_max_interval, 6011bd11f074SMaciej Żenczykowski .maxlen = sizeof(int), 6012bd11f074SMaciej Żenczykowski .mode = 0644, 6013bd11f074SMaciej Żenczykowski .proc_handler = proc_dointvec_jiffies, 6014bd11f074SMaciej Żenczykowski }, 6015bd11f074SMaciej Żenczykowski { 60161da177e4SLinus Torvalds .procname = "router_solicitation_delay", 60171da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_delay, 60181da177e4SLinus Torvalds .maxlen = sizeof(int), 60191da177e4SLinus Torvalds .mode = 0644, 60206d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 60211da177e4SLinus Torvalds }, 60221da177e4SLinus Torvalds { 60231da177e4SLinus Torvalds .procname = "force_mld_version", 60241da177e4SLinus Torvalds .data = &ipv6_devconf.force_mld_version, 60251da177e4SLinus Torvalds .maxlen = sizeof(int), 60261da177e4SLinus Torvalds .mode = 0644, 60276d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 60281da177e4SLinus Torvalds }, 6029fc4eba58SHannes Frederic Sowa { 6030fc4eba58SHannes Frederic Sowa .procname = "mldv1_unsolicited_report_interval", 6031fc4eba58SHannes Frederic Sowa .data = 6032fc4eba58SHannes Frederic Sowa &ipv6_devconf.mldv1_unsolicited_report_interval, 6033fc4eba58SHannes Frederic Sowa .maxlen = sizeof(int), 6034fc4eba58SHannes Frederic Sowa .mode = 0644, 6035fc4eba58SHannes Frederic Sowa .proc_handler = proc_dointvec_ms_jiffies, 6036fc4eba58SHannes Frederic Sowa }, 6037fc4eba58SHannes Frederic Sowa { 6038fc4eba58SHannes Frederic Sowa .procname = "mldv2_unsolicited_report_interval", 6039fc4eba58SHannes Frederic Sowa .data = 6040fc4eba58SHannes Frederic Sowa &ipv6_devconf.mldv2_unsolicited_report_interval, 6041fc4eba58SHannes Frederic Sowa .maxlen = sizeof(int), 6042fc4eba58SHannes Frederic Sowa .mode = 0644, 6043fc4eba58SHannes Frederic Sowa .proc_handler = proc_dointvec_ms_jiffies, 6044fc4eba58SHannes Frederic Sowa }, 60451da177e4SLinus Torvalds { 60461da177e4SLinus Torvalds .procname = "use_tempaddr", 60471da177e4SLinus Torvalds .data = &ipv6_devconf.use_tempaddr, 60481da177e4SLinus Torvalds .maxlen = sizeof(int), 60491da177e4SLinus Torvalds .mode = 0644, 60506d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 60511da177e4SLinus Torvalds }, 60521da177e4SLinus Torvalds { 60531da177e4SLinus Torvalds .procname = "temp_valid_lft", 60541da177e4SLinus Torvalds .data = &ipv6_devconf.temp_valid_lft, 60551da177e4SLinus Torvalds .maxlen = sizeof(int), 60561da177e4SLinus Torvalds .mode = 0644, 60576d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 60581da177e4SLinus Torvalds }, 60591da177e4SLinus Torvalds { 60601da177e4SLinus Torvalds .procname = "temp_prefered_lft", 60611da177e4SLinus Torvalds .data = &ipv6_devconf.temp_prefered_lft, 60621da177e4SLinus Torvalds .maxlen = sizeof(int), 60631da177e4SLinus Torvalds .mode = 0644, 60646d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 60651da177e4SLinus Torvalds }, 60661da177e4SLinus Torvalds { 60671da177e4SLinus Torvalds .procname = "regen_max_retry", 60681da177e4SLinus Torvalds .data = &ipv6_devconf.regen_max_retry, 60691da177e4SLinus Torvalds .maxlen = sizeof(int), 60701da177e4SLinus Torvalds .mode = 0644, 60716d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 60721da177e4SLinus Torvalds }, 60731da177e4SLinus Torvalds { 60741da177e4SLinus Torvalds .procname = "max_desync_factor", 60751da177e4SLinus Torvalds .data = &ipv6_devconf.max_desync_factor, 60761da177e4SLinus Torvalds .maxlen = sizeof(int), 60771da177e4SLinus Torvalds .mode = 0644, 60786d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 60791da177e4SLinus Torvalds }, 60801da177e4SLinus Torvalds { 60811da177e4SLinus Torvalds .procname = "max_addresses", 60821da177e4SLinus Torvalds .data = &ipv6_devconf.max_addresses, 60831da177e4SLinus Torvalds .maxlen = sizeof(int), 60841da177e4SLinus Torvalds .mode = 0644, 60856d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 60861da177e4SLinus Torvalds }, 60871da177e4SLinus Torvalds { 608865f5c7c1SYOSHIFUJI Hideaki .procname = "accept_ra_defrtr", 608965f5c7c1SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_defrtr, 609065f5c7c1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 609165f5c7c1SYOSHIFUJI Hideaki .mode = 0644, 60926d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 609365f5c7c1SYOSHIFUJI Hideaki }, 609465f5c7c1SYOSHIFUJI Hideaki { 60958013d1d7SHangbin Liu .procname = "accept_ra_min_hop_limit", 60968013d1d7SHangbin Liu .data = &ipv6_devconf.accept_ra_min_hop_limit, 60978013d1d7SHangbin Liu .maxlen = sizeof(int), 60988013d1d7SHangbin Liu .mode = 0644, 60998013d1d7SHangbin Liu .proc_handler = proc_dointvec, 61008013d1d7SHangbin Liu }, 61018013d1d7SHangbin Liu { 6102c4fd30ebSYOSHIFUJI Hideaki .procname = "accept_ra_pinfo", 6103c4fd30ebSYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_pinfo, 6104c4fd30ebSYOSHIFUJI Hideaki .maxlen = sizeof(int), 6105c4fd30ebSYOSHIFUJI Hideaki .mode = 0644, 61066d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 6107c4fd30ebSYOSHIFUJI Hideaki }, 6108930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 6109930d6ff2SYOSHIFUJI Hideaki { 6110930d6ff2SYOSHIFUJI Hideaki .procname = "accept_ra_rtr_pref", 6111930d6ff2SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rtr_pref, 6112930d6ff2SYOSHIFUJI Hideaki .maxlen = sizeof(int), 6113930d6ff2SYOSHIFUJI Hideaki .mode = 0644, 61146d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 6115930d6ff2SYOSHIFUJI Hideaki }, 611652e16356SYOSHIFUJI Hideaki { 611752e16356SYOSHIFUJI Hideaki .procname = "router_probe_interval", 611852e16356SYOSHIFUJI Hideaki .data = &ipv6_devconf.rtr_probe_interval, 611952e16356SYOSHIFUJI Hideaki .maxlen = sizeof(int), 612052e16356SYOSHIFUJI Hideaki .mode = 0644, 61216d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 612252e16356SYOSHIFUJI Hideaki }, 6123fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 612409c884d4SYOSHIFUJI Hideaki { 612509c884d4SYOSHIFUJI Hideaki .procname = "accept_ra_rt_info_max_plen", 612609c884d4SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rt_info_max_plen, 612709c884d4SYOSHIFUJI Hideaki .maxlen = sizeof(int), 612809c884d4SYOSHIFUJI Hideaki .mode = 0644, 61296d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 613009c884d4SYOSHIFUJI Hideaki }, 613109c884d4SYOSHIFUJI Hideaki #endif 6132930d6ff2SYOSHIFUJI Hideaki #endif 6133c4fd30ebSYOSHIFUJI Hideaki { 6134fbea49e1SYOSHIFUJI Hideaki .procname = "proxy_ndp", 6135fbea49e1SYOSHIFUJI Hideaki .data = &ipv6_devconf.proxy_ndp, 6136fbea49e1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 6137fbea49e1SYOSHIFUJI Hideaki .mode = 0644, 6138c92d5491Sstephen hemminger .proc_handler = addrconf_sysctl_proxy_ndp, 6139fbea49e1SYOSHIFUJI Hideaki }, 6140fbea49e1SYOSHIFUJI Hideaki { 61410bcbc926SYOSHIFUJI Hideaki .procname = "accept_source_route", 61420bcbc926SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_source_route, 61430bcbc926SYOSHIFUJI Hideaki .maxlen = sizeof(int), 61440bcbc926SYOSHIFUJI Hideaki .mode = 0644, 61456d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 61460bcbc926SYOSHIFUJI Hideaki }, 614795c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 614895c385b4SNeil Horman { 614995c385b4SNeil Horman .procname = "optimistic_dad", 615095c385b4SNeil Horman .data = &ipv6_devconf.optimistic_dad, 615195c385b4SNeil Horman .maxlen = sizeof(int), 615295c385b4SNeil Horman .mode = 0644, 61536d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 615495c385b4SNeil Horman }, 61557fd2561eSErik Kline { 61567fd2561eSErik Kline .procname = "use_optimistic", 61577fd2561eSErik Kline .data = &ipv6_devconf.use_optimistic, 61587fd2561eSErik Kline .maxlen = sizeof(int), 61597fd2561eSErik Kline .mode = 0644, 61607fd2561eSErik Kline .proc_handler = proc_dointvec, 61617fd2561eSErik Kline }, 616295c385b4SNeil Horman #endif 61637bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 61647bc570c8SYOSHIFUJI Hideaki { 61657bc570c8SYOSHIFUJI Hideaki .procname = "mc_forwarding", 61667bc570c8SYOSHIFUJI Hideaki .data = &ipv6_devconf.mc_forwarding, 61677bc570c8SYOSHIFUJI Hideaki .maxlen = sizeof(int), 6168a4e6db07SDavid S. Miller .mode = 0444, 61696d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 61707bc570c8SYOSHIFUJI Hideaki }, 61717bc570c8SYOSHIFUJI Hideaki #endif 61720bcbc926SYOSHIFUJI Hideaki { 6173778d80beSYOSHIFUJI Hideaki .procname = "disable_ipv6", 6174778d80beSYOSHIFUJI Hideaki .data = &ipv6_devconf.disable_ipv6, 6175778d80beSYOSHIFUJI Hideaki .maxlen = sizeof(int), 6176778d80beSYOSHIFUJI Hideaki .mode = 0644, 617756d417b1SBrian Haley .proc_handler = addrconf_sysctl_disable, 6178778d80beSYOSHIFUJI Hideaki }, 6179778d80beSYOSHIFUJI Hideaki { 61801b34be74SYOSHIFUJI Hideaki .procname = "accept_dad", 61811b34be74SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_dad, 61821b34be74SYOSHIFUJI Hideaki .maxlen = sizeof(int), 61831b34be74SYOSHIFUJI Hideaki .mode = 0644, 61846d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 61851b34be74SYOSHIFUJI Hideaki }, 61861b34be74SYOSHIFUJI Hideaki { 6187f7734fdfSOctavian Purdila .procname = "force_tllao", 6188f7734fdfSOctavian Purdila .data = &ipv6_devconf.force_tllao, 6189f7734fdfSOctavian Purdila .maxlen = sizeof(int), 6190f7734fdfSOctavian Purdila .mode = 0644, 6191f7734fdfSOctavian Purdila .proc_handler = proc_dointvec 6192f7734fdfSOctavian Purdila }, 6193f7734fdfSOctavian Purdila { 61945cb04436SHannes Frederic Sowa .procname = "ndisc_notify", 61955cb04436SHannes Frederic Sowa .data = &ipv6_devconf.ndisc_notify, 61965cb04436SHannes Frederic Sowa .maxlen = sizeof(int), 61975cb04436SHannes Frederic Sowa .mode = 0644, 61985cb04436SHannes Frederic Sowa .proc_handler = proc_dointvec 61995cb04436SHannes Frederic Sowa }, 62005cb04436SHannes Frederic Sowa { 6201b800c3b9SHannes Frederic Sowa .procname = "suppress_frag_ndisc", 6202b800c3b9SHannes Frederic Sowa .data = &ipv6_devconf.suppress_frag_ndisc, 6203b800c3b9SHannes Frederic Sowa .maxlen = sizeof(int), 6204b800c3b9SHannes Frederic Sowa .mode = 0644, 6205b800c3b9SHannes Frederic Sowa .proc_handler = proc_dointvec 6206b800c3b9SHannes Frederic Sowa }, 6207b800c3b9SHannes Frederic Sowa { 6208d9333196SBen Greear .procname = "accept_ra_from_local", 6209d9333196SBen Greear .data = &ipv6_devconf.accept_ra_from_local, 6210d9333196SBen Greear .maxlen = sizeof(int), 6211d9333196SBen Greear .mode = 0644, 6212d9333196SBen Greear .proc_handler = proc_dointvec, 6213d9333196SBen Greear }, 6214d9333196SBen Greear { 6215c2943f14SHarout Hedeshian .procname = "accept_ra_mtu", 6216c2943f14SHarout Hedeshian .data = &ipv6_devconf.accept_ra_mtu, 6217c2943f14SHarout Hedeshian .maxlen = sizeof(int), 6218c2943f14SHarout Hedeshian .mode = 0644, 6219c2943f14SHarout Hedeshian .proc_handler = proc_dointvec, 6220c2943f14SHarout Hedeshian }, 6221c2943f14SHarout Hedeshian { 62223d1bec99SHannes Frederic Sowa .procname = "stable_secret", 62233d1bec99SHannes Frederic Sowa .data = &ipv6_devconf.stable_secret, 62243d1bec99SHannes Frederic Sowa .maxlen = IPV6_MAX_STRLEN, 62253d1bec99SHannes Frederic Sowa .mode = 0600, 62263d1bec99SHannes Frederic Sowa .proc_handler = addrconf_sysctl_stable_secret, 62273d1bec99SHannes Frederic Sowa }, 62283d1bec99SHannes Frederic Sowa { 62293985e8a3SErik Kline .procname = "use_oif_addrs_only", 62303985e8a3SErik Kline .data = &ipv6_devconf.use_oif_addrs_only, 62313985e8a3SErik Kline .maxlen = sizeof(int), 62323985e8a3SErik Kline .mode = 0644, 62333985e8a3SErik Kline .proc_handler = proc_dointvec, 623435103d11SAndy Gospodarek }, 623535103d11SAndy Gospodarek { 623635103d11SAndy Gospodarek .procname = "ignore_routes_with_linkdown", 623735103d11SAndy Gospodarek .data = &ipv6_devconf.ignore_routes_with_linkdown, 623835103d11SAndy Gospodarek .maxlen = sizeof(int), 623935103d11SAndy Gospodarek .mode = 0644, 624035103d11SAndy Gospodarek .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown, 62413985e8a3SErik Kline }, 62423985e8a3SErik Kline { 6243abbc3043SJohannes Berg .procname = "drop_unicast_in_l2_multicast", 6244abbc3043SJohannes Berg .data = &ipv6_devconf.drop_unicast_in_l2_multicast, 6245abbc3043SJohannes Berg .maxlen = sizeof(int), 6246abbc3043SJohannes Berg .mode = 0644, 6247abbc3043SJohannes Berg .proc_handler = proc_dointvec, 6248abbc3043SJohannes Berg }, 6249abbc3043SJohannes Berg { 62507a02bf89SJohannes Berg .procname = "drop_unsolicited_na", 62517a02bf89SJohannes Berg .data = &ipv6_devconf.drop_unsolicited_na, 62527a02bf89SJohannes Berg .maxlen = sizeof(int), 62537a02bf89SJohannes Berg .mode = 0644, 62547a02bf89SJohannes Berg .proc_handler = proc_dointvec, 62557a02bf89SJohannes Berg }, 62567a02bf89SJohannes Berg { 6257f1705ec1SDavid Ahern .procname = "keep_addr_on_down", 6258f1705ec1SDavid Ahern .data = &ipv6_devconf.keep_addr_on_down, 6259f1705ec1SDavid Ahern .maxlen = sizeof(int), 6260f1705ec1SDavid Ahern .mode = 0644, 6261f1705ec1SDavid Ahern .proc_handler = proc_dointvec, 6262f1705ec1SDavid Ahern 6263f1705ec1SDavid Ahern }, 6264f1705ec1SDavid Ahern { 62651ababebaSDavid Lebrun .procname = "seg6_enabled", 62661ababebaSDavid Lebrun .data = &ipv6_devconf.seg6_enabled, 62671ababebaSDavid Lebrun .maxlen = sizeof(int), 62681ababebaSDavid Lebrun .mode = 0644, 62691ababebaSDavid Lebrun .proc_handler = proc_dointvec, 62701ababebaSDavid Lebrun }, 6271bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC 6272bf355b8dSDavid Lebrun { 6273bf355b8dSDavid Lebrun .procname = "seg6_require_hmac", 6274bf355b8dSDavid Lebrun .data = &ipv6_devconf.seg6_require_hmac, 6275bf355b8dSDavid Lebrun .maxlen = sizeof(int), 6276bf355b8dSDavid Lebrun .mode = 0644, 6277bf355b8dSDavid Lebrun .proc_handler = proc_dointvec, 6278bf355b8dSDavid Lebrun }, 6279bf355b8dSDavid Lebrun #endif 62801ababebaSDavid Lebrun { 6281adc176c5SErik Nordmark .procname = "enhanced_dad", 6282adc176c5SErik Nordmark .data = &ipv6_devconf.enhanced_dad, 6283adc176c5SErik Nordmark .maxlen = sizeof(int), 6284adc176c5SErik Nordmark .mode = 0644, 6285adc176c5SErik Nordmark .proc_handler = proc_dointvec, 6286adc176c5SErik Nordmark }, 6287adc176c5SErik Nordmark { 6288d35a00b8SFelix Jia .procname = "addr_gen_mode", 6289d35a00b8SFelix Jia .data = &ipv6_devconf.addr_gen_mode, 6290d35a00b8SFelix Jia .maxlen = sizeof(int), 6291d35a00b8SFelix Jia .mode = 0644, 6292d35a00b8SFelix Jia .proc_handler = addrconf_sysctl_addr_gen_mode, 6293d35a00b8SFelix Jia }, 6294d35a00b8SFelix Jia { 6295*df789fe7SDavid Forster .procname = "disable_policy", 6296*df789fe7SDavid Forster .data = &ipv6_devconf.disable_policy, 6297*df789fe7SDavid Forster .maxlen = sizeof(int), 6298*df789fe7SDavid Forster .mode = 0644, 6299*df789fe7SDavid Forster .proc_handler = addrconf_sysctl_disable_policy, 6300*df789fe7SDavid Forster }, 6301*df789fe7SDavid Forster { 6302f8572d8fSEric W. Biederman /* sentinel */ 63031da177e4SLinus Torvalds } 63041da177e4SLinus Torvalds }; 63051da177e4SLinus Torvalds 6306bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name, 6307f8572d8fSEric W. Biederman struct inet6_dev *idev, struct ipv6_devconf *p) 63081da177e4SLinus Torvalds { 630929c994e3SNicolas Dichtel int i, ifindex; 6310607ea7cdSKonstantin Khlebnikov struct ctl_table *table; 63116105e293SEric W. Biederman char path[sizeof("net/ipv6/conf/") + IFNAMSIZ]; 63121dab6222SPavel Emelyanov 6313607ea7cdSKonstantin Khlebnikov table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL); 6314607ea7cdSKonstantin Khlebnikov if (!table) 6315f68635e6SPavel Emelyanov goto out; 6316f68635e6SPavel Emelyanov 6317607ea7cdSKonstantin Khlebnikov for (i = 0; table[i].data; i++) { 6318607ea7cdSKonstantin Khlebnikov table[i].data += (char *)p - (char *)&ipv6_devconf; 6319cb9e684eSMaciej Żenczykowski /* If one of these is already set, then it is not safe to 6320cb9e684eSMaciej Żenczykowski * overwrite either of them: this makes proc_dointvec_minmax 6321cb9e684eSMaciej Żenczykowski * usable. 6322cb9e684eSMaciej Żenczykowski */ 6323cb9e684eSMaciej Żenczykowski if (!table[i].extra1 && !table[i].extra2) { 6324607ea7cdSKonstantin Khlebnikov table[i].extra1 = idev; /* embedded; no ref */ 6325607ea7cdSKonstantin Khlebnikov table[i].extra2 = net; 63261da177e4SLinus Torvalds } 6327cb9e684eSMaciej Żenczykowski } 63281da177e4SLinus Torvalds 63296105e293SEric W. Biederman snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name); 63301da177e4SLinus Torvalds 6331607ea7cdSKonstantin Khlebnikov p->sysctl_header = register_net_sysctl(net, path, table); 6332607ea7cdSKonstantin Khlebnikov if (!p->sysctl_header) 63336105e293SEric W. Biederman goto free; 6334f68635e6SPavel Emelyanov 633529c994e3SNicolas Dichtel if (!strcmp(dev_name, "all")) 633629c994e3SNicolas Dichtel ifindex = NETCONFA_IFINDEX_ALL; 633729c994e3SNicolas Dichtel else if (!strcmp(dev_name, "default")) 633829c994e3SNicolas Dichtel ifindex = NETCONFA_IFINDEX_DEFAULT; 633929c994e3SNicolas Dichtel else 634029c994e3SNicolas Dichtel ifindex = idev->dev->ifindex; 634129c994e3SNicolas Dichtel inet6_netconf_notify_devconf(net, NETCONFA_ALL, ifindex, p); 634295897312SPavel Emelyanov return 0; 63431da177e4SLinus Torvalds 63441da177e4SLinus Torvalds free: 6345607ea7cdSKonstantin Khlebnikov kfree(table); 6346f68635e6SPavel Emelyanov out: 634795897312SPavel Emelyanov return -ENOBUFS; 63481da177e4SLinus Torvalds } 63491da177e4SLinus Torvalds 6350408c4768SPavel Emelyanov static void __addrconf_sysctl_unregister(struct ipv6_devconf *p) 6351f52295a9SPavel Emelyanov { 6352607ea7cdSKonstantin Khlebnikov struct ctl_table *table; 6353f52295a9SPavel Emelyanov 6354607ea7cdSKonstantin Khlebnikov if (!p->sysctl_header) 6355408c4768SPavel Emelyanov return; 6356408c4768SPavel Emelyanov 6357607ea7cdSKonstantin Khlebnikov table = p->sysctl_header->ctl_table_arg; 6358607ea7cdSKonstantin Khlebnikov unregister_net_sysctl_table(p->sysctl_header); 6359607ea7cdSKonstantin Khlebnikov p->sysctl_header = NULL; 6360607ea7cdSKonstantin Khlebnikov kfree(table); 63611da177e4SLinus Torvalds } 6362408c4768SPavel Emelyanov 6363a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev) 6364408c4768SPavel Emelyanov { 6365a317a2f1SWANG Cong int err; 6366a317a2f1SWANG Cong 6367a317a2f1SWANG Cong if (!sysctl_dev_name_is_allowed(idev->dev->name)) 6368a317a2f1SWANG Cong return -EINVAL; 6369a317a2f1SWANG Cong 6370a317a2f1SWANG Cong err = neigh_sysctl_register(idev->dev, idev->nd_parms, 6371f8572d8fSEric W. Biederman &ndisc_ifinfo_sysctl_change); 6372a317a2f1SWANG Cong if (err) 6373a317a2f1SWANG Cong return err; 6374a317a2f1SWANG Cong err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name, 6375f8572d8fSEric W. Biederman idev, &idev->cnf); 6376a317a2f1SWANG Cong if (err) 6377a317a2f1SWANG Cong neigh_sysctl_unregister(idev->nd_parms); 6378a317a2f1SWANG Cong 6379a317a2f1SWANG Cong return err; 6380408c4768SPavel Emelyanov } 6381408c4768SPavel Emelyanov 6382408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev) 6383408c4768SPavel Emelyanov { 6384408c4768SPavel Emelyanov __addrconf_sysctl_unregister(&idev->cnf); 6385408c4768SPavel Emelyanov neigh_sysctl_unregister(idev->nd_parms); 63861da177e4SLinus Torvalds } 63871da177e4SLinus Torvalds 63881da177e4SLinus Torvalds 63891da177e4SLinus Torvalds #endif 63901da177e4SLinus Torvalds 63912c8c1e72SAlexey Dobriyan static int __net_init addrconf_init_net(struct net *net) 6392e0da5a48SPavel Emelyanov { 6393a79ca223SHong Zhiguo int err = -ENOMEM; 6394e0da5a48SPavel Emelyanov struct ipv6_devconf *all, *dflt; 6395e0da5a48SPavel Emelyanov 6396a79ca223SHong Zhiguo all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL); 639763159f29SIan Morris if (!all) 6398e0da5a48SPavel Emelyanov goto err_alloc_all; 6399e0da5a48SPavel Emelyanov 6400a79ca223SHong Zhiguo dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL); 640163159f29SIan Morris if (!dflt) 6402e0da5a48SPavel Emelyanov goto err_alloc_dflt; 6403a79ca223SHong Zhiguo 640456d417b1SBrian Haley /* these will be inherited by all namespaces */ 640556d417b1SBrian Haley dflt->autoconf = ipv6_defaults.autoconf; 640656d417b1SBrian Haley dflt->disable_ipv6 = ipv6_defaults.disable_ipv6; 6407e0da5a48SPavel Emelyanov 64083d1bec99SHannes Frederic Sowa dflt->stable_secret.initialized = false; 64093d1bec99SHannes Frederic Sowa all->stable_secret.initialized = false; 64103d1bec99SHannes Frederic Sowa 6411e0da5a48SPavel Emelyanov net->ipv6.devconf_all = all; 6412e0da5a48SPavel Emelyanov net->ipv6.devconf_dflt = dflt; 6413e0da5a48SPavel Emelyanov 6414e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 6415f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "all", NULL, all); 6416e0da5a48SPavel Emelyanov if (err < 0) 6417e0da5a48SPavel Emelyanov goto err_reg_all; 6418e0da5a48SPavel Emelyanov 6419f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "default", NULL, dflt); 6420e0da5a48SPavel Emelyanov if (err < 0) 6421e0da5a48SPavel Emelyanov goto err_reg_dflt; 6422e0da5a48SPavel Emelyanov #endif 6423e0da5a48SPavel Emelyanov return 0; 6424e0da5a48SPavel Emelyanov 6425e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 6426e0da5a48SPavel Emelyanov err_reg_dflt: 6427e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(all); 6428e0da5a48SPavel Emelyanov err_reg_all: 6429e0da5a48SPavel Emelyanov kfree(dflt); 6430e0da5a48SPavel Emelyanov #endif 6431e0da5a48SPavel Emelyanov err_alloc_dflt: 6432e0da5a48SPavel Emelyanov kfree(all); 6433e0da5a48SPavel Emelyanov err_alloc_all: 6434e0da5a48SPavel Emelyanov return err; 6435e0da5a48SPavel Emelyanov } 6436e0da5a48SPavel Emelyanov 64372c8c1e72SAlexey Dobriyan static void __net_exit addrconf_exit_net(struct net *net) 6438e0da5a48SPavel Emelyanov { 6439e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 6440e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(net->ipv6.devconf_dflt); 6441e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(net->ipv6.devconf_all); 6442e0da5a48SPavel Emelyanov #endif 6443e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_dflt); 6444e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_all); 6445e0da5a48SPavel Emelyanov } 6446e0da5a48SPavel Emelyanov 6447e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = { 6448e0da5a48SPavel Emelyanov .init = addrconf_init_net, 6449e0da5a48SPavel Emelyanov .exit = addrconf_exit_net, 6450e0da5a48SPavel Emelyanov }; 6451e0da5a48SPavel Emelyanov 6452207895fdSDaniel Borkmann static struct rtnl_af_ops inet6_ops __read_mostly = { 6453b382b191SThomas Graf .family = AF_INET6, 6454b382b191SThomas Graf .fill_link_af = inet6_fill_link_af, 6455b382b191SThomas Graf .get_link_af_size = inet6_get_link_af_size, 645611b1f828SDaniel Borkmann .validate_link_af = inet6_validate_link_af, 6457f53adae4SDaniel Borkmann .set_link_af = inet6_set_link_af, 6458b382b191SThomas Graf }; 6459b382b191SThomas Graf 64601da177e4SLinus Torvalds /* 64611da177e4SLinus Torvalds * Init / cleanup code 64621da177e4SLinus Torvalds */ 64631da177e4SLinus Torvalds 64641da177e4SLinus Torvalds int __init addrconf_init(void) 64651da177e4SLinus Torvalds { 6466a317a2f1SWANG Cong struct inet6_dev *idev; 6467c2e21293Sstephen hemminger int i, err; 64682a8cc6c8SYOSHIFUJI Hideaki 6469e21e8467SStephen Hemminger err = ipv6_addr_label_init(); 6470e21e8467SStephen Hemminger if (err < 0) { 6471f3213831SJoe Perches pr_crit("%s: cannot initialize default policy table: %d\n", 6472f3213831SJoe Perches __func__, err); 64732cc6d2bfSNeil Horman goto out; 64742a8cc6c8SYOSHIFUJI Hideaki } 64751da177e4SLinus Torvalds 64762cc6d2bfSNeil Horman err = register_pernet_subsys(&addrconf_ops); 64772cc6d2bfSNeil Horman if (err < 0) 64782cc6d2bfSNeil Horman goto out_addrlabel; 6479e0da5a48SPavel Emelyanov 6480c15b1ccaSHannes Frederic Sowa addrconf_wq = create_workqueue("ipv6_addrconf"); 6481c15b1ccaSHannes Frederic Sowa if (!addrconf_wq) { 6482c15b1ccaSHannes Frederic Sowa err = -ENOMEM; 6483c15b1ccaSHannes Frederic Sowa goto out_nowq; 6484c15b1ccaSHannes Frederic Sowa } 6485c15b1ccaSHannes Frederic Sowa 64861da177e4SLinus Torvalds /* The addrconf netdev notifier requires that loopback_dev 64871da177e4SLinus Torvalds * has it's ipv6 private information allocated and setup 64881da177e4SLinus Torvalds * before it can bring up and give link-local addresses 64891da177e4SLinus Torvalds * to other devices which are up. 64901da177e4SLinus Torvalds * 64911da177e4SLinus Torvalds * Unfortunately, loopback_dev is not necessarily the first 64921da177e4SLinus Torvalds * entry in the global dev_base list of net devices. In fact, 64931da177e4SLinus Torvalds * it is likely to be the very last entry on that list. 64941da177e4SLinus Torvalds * So this causes the notifier registry below to try and 64951da177e4SLinus Torvalds * give link-local addresses to all devices besides loopback_dev 64961da177e4SLinus Torvalds * first, then loopback_dev, which cases all the non-loopback_dev 64971da177e4SLinus Torvalds * devices to fail to get a link-local address. 64981da177e4SLinus Torvalds * 64991da177e4SLinus Torvalds * So, as a temporary fix, allocate the ipv6 structure for 65001da177e4SLinus Torvalds * loopback_dev first by hand. 65011da177e4SLinus Torvalds * Longer term, all of the dependencies ipv6 has upon the loopback 65021da177e4SLinus Torvalds * device and it being up should be removed. 65031da177e4SLinus Torvalds */ 65041da177e4SLinus Torvalds rtnl_lock(); 6505a317a2f1SWANG Cong idev = ipv6_add_dev(init_net.loopback_dev); 65061da177e4SLinus Torvalds rtnl_unlock(); 6507a317a2f1SWANG Cong if (IS_ERR(idev)) { 6508a317a2f1SWANG Cong err = PTR_ERR(idev); 6509e0da5a48SPavel Emelyanov goto errlo; 6510a317a2f1SWANG Cong } 65111da177e4SLinus Torvalds 6512c2e21293Sstephen hemminger for (i = 0; i < IN6_ADDR_HSIZE; i++) 6513c2e21293Sstephen hemminger INIT_HLIST_HEAD(&inet6_addr_lst[i]); 6514c2e21293Sstephen hemminger 65151da177e4SLinus Torvalds register_netdevice_notifier(&ipv6_dev_notf); 65161da177e4SLinus Torvalds 6517c15b1ccaSHannes Frederic Sowa addrconf_verify(); 6518c127ea2cSThomas Graf 65193678a9d8Sstephen hemminger rtnl_af_register(&inet6_ops); 6520b382b191SThomas Graf 6521c7ac8679SGreg Rose err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo, 6522c7ac8679SGreg Rose NULL); 6523c127ea2cSThomas Graf if (err < 0) 6524c127ea2cSThomas Graf goto errout; 6525c127ea2cSThomas Graf 6526c127ea2cSThomas Graf /* Only the first call to __rtnl_register can fail */ 6527c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL); 6528c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL); 6529c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr, 6530c7ac8679SGreg Rose inet6_dump_ifaddr, NULL); 6531c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL, 6532c7ac8679SGreg Rose inet6_dump_ifmcaddr, NULL); 6533c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL, 6534c7ac8679SGreg Rose inet6_dump_ifacaddr, NULL); 653576f8f6cbSNicolas Dichtel __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf, 65367a674200SNicolas Dichtel inet6_netconf_dump_devconf, NULL); 6537c127ea2cSThomas Graf 65382a8cc6c8SYOSHIFUJI Hideaki ipv6_addr_label_rtnl_register(); 65392a8cc6c8SYOSHIFUJI Hideaki 65401da177e4SLinus Torvalds return 0; 6541c127ea2cSThomas Graf errout: 6542b382b191SThomas Graf rtnl_af_unregister(&inet6_ops); 6543c127ea2cSThomas Graf unregister_netdevice_notifier(&ipv6_dev_notf); 6544e0da5a48SPavel Emelyanov errlo: 6545c15b1ccaSHannes Frederic Sowa destroy_workqueue(addrconf_wq); 6546c15b1ccaSHannes Frederic Sowa out_nowq: 6547e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 65482cc6d2bfSNeil Horman out_addrlabel: 65492cc6d2bfSNeil Horman ipv6_addr_label_cleanup(); 65502cc6d2bfSNeil Horman out: 6551c127ea2cSThomas Graf return err; 65521da177e4SLinus Torvalds } 65531da177e4SLinus Torvalds 655409f7709fSDaniel Lezcano void addrconf_cleanup(void) 65551da177e4SLinus Torvalds { 6556176c39afSDaniel Lezcano struct net_device *dev; 65571da177e4SLinus Torvalds int i; 65581da177e4SLinus Torvalds 65591da177e4SLinus Torvalds unregister_netdevice_notifier(&ipv6_dev_notf); 6560e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 65612cc6d2bfSNeil Horman ipv6_addr_label_cleanup(); 65621da177e4SLinus Torvalds 65631da177e4SLinus Torvalds rtnl_lock(); 65641da177e4SLinus Torvalds 6565b382b191SThomas Graf __rtnl_af_unregister(&inet6_ops); 6566b382b191SThomas Graf 6567176c39afSDaniel Lezcano /* clean dev list */ 6568176c39afSDaniel Lezcano for_each_netdev(&init_net, dev) { 6569176c39afSDaniel Lezcano if (__in6_dev_get(dev) == NULL) 6570176c39afSDaniel Lezcano continue; 6571176c39afSDaniel Lezcano addrconf_ifdown(dev, 1); 6572176c39afSDaniel Lezcano } 6573176c39afSDaniel Lezcano addrconf_ifdown(init_net.loopback_dev, 2); 6574176c39afSDaniel Lezcano 65751da177e4SLinus Torvalds /* 65761da177e4SLinus Torvalds * Check hash table. 65771da177e4SLinus Torvalds */ 65785c578aedSstephen hemminger spin_lock_bh(&addrconf_hash_lock); 6579c2e21293Sstephen hemminger for (i = 0; i < IN6_ADDR_HSIZE; i++) 6580c2e21293Sstephen hemminger WARN_ON(!hlist_empty(&inet6_addr_lst[i])); 65815c578aedSstephen hemminger spin_unlock_bh(&addrconf_hash_lock); 6582c15b1ccaSHannes Frederic Sowa cancel_delayed_work(&addr_chk_work); 65831da177e4SLinus Torvalds rtnl_unlock(); 6584c15b1ccaSHannes Frederic Sowa 6585c15b1ccaSHannes Frederic Sowa destroy_workqueue(addrconf_wq); 65861da177e4SLinus Torvalds } 6587