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 227bbea124bSJoel Scherpelz .accept_ra_rt_info_min_plen = 0, 22809c884d4SYOSHIFUJI Hideaki .accept_ra_rt_info_max_plen = 0, 22909c884d4SYOSHIFUJI Hideaki #endif 230930d6ff2SYOSHIFUJI Hideaki #endif 231fbea49e1SYOSHIFUJI Hideaki .proxy_ndp = 0, 2320bcbc926SYOSHIFUJI Hideaki .accept_source_route = 0, /* we do not accept RH0 by default. */ 233778d80beSYOSHIFUJI Hideaki .disable_ipv6 = 0, 2341b34be74SYOSHIFUJI Hideaki .accept_dad = 1, 235b800c3b9SHannes Frederic Sowa .suppress_frag_ndisc = 1, 236c2943f14SHarout Hedeshian .accept_ra_mtu = 1, 2373d1bec99SHannes Frederic Sowa .stable_secret = { 2383d1bec99SHannes Frederic Sowa .initialized = false, 2393985e8a3SErik Kline }, 2403985e8a3SErik Kline .use_oif_addrs_only = 0, 24135103d11SAndy Gospodarek .ignore_routes_with_linkdown = 0, 242f1705ec1SDavid Ahern .keep_addr_on_down = 0, 2431ababebaSDavid Lebrun .seg6_enabled = 0, 244bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC 245bf355b8dSDavid Lebrun .seg6_require_hmac = 0, 246bf355b8dSDavid Lebrun #endif 247adc176c5SErik Nordmark .enhanced_dad = 1, 248d35a00b8SFelix Jia .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64, 249df789fe7SDavid Forster .disable_policy = 0, 2501da177e4SLinus Torvalds }; 2511da177e4SLinus Torvalds 252ab32ea5dSBrian Haley static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = { 2531da177e4SLinus Torvalds .forwarding = 0, 2541da177e4SLinus Torvalds .hop_limit = IPV6_DEFAULT_HOPLIMIT, 2551da177e4SLinus Torvalds .mtu6 = IPV6_MIN_MTU, 2561da177e4SLinus Torvalds .accept_ra = 1, 2571da177e4SLinus Torvalds .accept_redirects = 1, 2581da177e4SLinus Torvalds .autoconf = 1, 259fc4eba58SHannes Frederic Sowa .force_mld_version = 0, 260fc4eba58SHannes Frederic Sowa .mldv1_unsolicited_report_interval = 10 * HZ, 261fc4eba58SHannes Frederic Sowa .mldv2_unsolicited_report_interval = HZ, 2621da177e4SLinus Torvalds .dad_transmits = 1, 2631da177e4SLinus Torvalds .rtr_solicits = MAX_RTR_SOLICITATIONS, 2641da177e4SLinus Torvalds .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, 265bd11f074SMaciej Żenczykowski .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL, 2661da177e4SLinus Torvalds .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, 2671da177e4SLinus Torvalds .use_tempaddr = 0, 2681da177e4SLinus Torvalds .temp_valid_lft = TEMP_VALID_LIFETIME, 2691da177e4SLinus Torvalds .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, 2701da177e4SLinus Torvalds .regen_max_retry = REGEN_MAX_RETRY, 2711da177e4SLinus Torvalds .max_desync_factor = MAX_DESYNC_FACTOR, 2721da177e4SLinus Torvalds .max_addresses = IPV6_MAX_ADDRESSES, 27365f5c7c1SYOSHIFUJI Hideaki .accept_ra_defrtr = 1, 274d9333196SBen Greear .accept_ra_from_local = 0, 2758013d1d7SHangbin Liu .accept_ra_min_hop_limit= 1, 276c4fd30ebSYOSHIFUJI Hideaki .accept_ra_pinfo = 1, 277930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 278930d6ff2SYOSHIFUJI Hideaki .accept_ra_rtr_pref = 1, 27952e16356SYOSHIFUJI Hideaki .rtr_probe_interval = 60 * HZ, 28009c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 281bbea124bSJoel Scherpelz .accept_ra_rt_info_min_plen = 0, 28209c884d4SYOSHIFUJI Hideaki .accept_ra_rt_info_max_plen = 0, 28309c884d4SYOSHIFUJI Hideaki #endif 284930d6ff2SYOSHIFUJI Hideaki #endif 285fbea49e1SYOSHIFUJI Hideaki .proxy_ndp = 0, 2860bcbc926SYOSHIFUJI Hideaki .accept_source_route = 0, /* we do not accept RH0 by default. */ 287778d80beSYOSHIFUJI Hideaki .disable_ipv6 = 0, 2881b34be74SYOSHIFUJI Hideaki .accept_dad = 1, 289b800c3b9SHannes Frederic Sowa .suppress_frag_ndisc = 1, 290c2943f14SHarout Hedeshian .accept_ra_mtu = 1, 2913d1bec99SHannes Frederic Sowa .stable_secret = { 2923d1bec99SHannes Frederic Sowa .initialized = false, 2933d1bec99SHannes Frederic Sowa }, 2943985e8a3SErik Kline .use_oif_addrs_only = 0, 29535103d11SAndy Gospodarek .ignore_routes_with_linkdown = 0, 296f1705ec1SDavid Ahern .keep_addr_on_down = 0, 2971ababebaSDavid Lebrun .seg6_enabled = 0, 298bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC 299bf355b8dSDavid Lebrun .seg6_require_hmac = 0, 300bf355b8dSDavid Lebrun #endif 301adc176c5SErik Nordmark .enhanced_dad = 1, 302d35a00b8SFelix Jia .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64, 303df789fe7SDavid Forster .disable_policy = 0, 3041da177e4SLinus Torvalds }; 3051da177e4SLinus Torvalds 306f24e3d65SMitsuru Chinen /* Check if a valid qdisc is available */ 30705297949SDavid S. Miller static inline bool addrconf_qdisc_ok(const struct net_device *dev) 308f24e3d65SMitsuru Chinen { 30905297949SDavid S. Miller return !qdisc_tx_is_noop(dev); 310f24e3d65SMitsuru Chinen } 311f24e3d65SMitsuru Chinen 312b7b1bfceSHannes Frederic Sowa static void addrconf_del_rs_timer(struct inet6_dev *idev) 3131da177e4SLinus Torvalds { 314b7b1bfceSHannes Frederic Sowa if (del_timer(&idev->rs_timer)) 315b7b1bfceSHannes Frederic Sowa __in6_dev_put(idev); 316b7b1bfceSHannes Frederic Sowa } 317b7b1bfceSHannes Frederic Sowa 318c15b1ccaSHannes Frederic Sowa static void addrconf_del_dad_work(struct inet6_ifaddr *ifp) 319b7b1bfceSHannes Frederic Sowa { 320c15b1ccaSHannes Frederic Sowa if (cancel_delayed_work(&ifp->dad_work)) 3211da177e4SLinus Torvalds __in6_ifa_put(ifp); 3221da177e4SLinus Torvalds } 3231da177e4SLinus Torvalds 324b7b1bfceSHannes Frederic Sowa static void addrconf_mod_rs_timer(struct inet6_dev *idev, 3251da177e4SLinus Torvalds unsigned long when) 3261da177e4SLinus Torvalds { 327b7b1bfceSHannes Frederic Sowa if (!timer_pending(&idev->rs_timer)) 328b7b1bfceSHannes Frederic Sowa in6_dev_hold(idev); 329b7b1bfceSHannes Frederic Sowa mod_timer(&idev->rs_timer, jiffies + when); 3301da177e4SLinus Torvalds } 331b7b1bfceSHannes Frederic Sowa 332c15b1ccaSHannes Frederic Sowa static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp, 333c15b1ccaSHannes Frederic Sowa unsigned long delay) 334b7b1bfceSHannes Frederic Sowa { 335b7b1bfceSHannes Frederic Sowa in6_ifa_hold(ifp); 336f8a894b2SXin Long if (mod_delayed_work(addrconf_wq, &ifp->dad_work, delay)) 337f8a894b2SXin Long in6_ifa_put(ifp); 3381da177e4SLinus Torvalds } 3391da177e4SLinus Torvalds 3407f7d9a6bSHerbert Xu static int snmp6_alloc_dev(struct inet6_dev *idev) 3417f7d9a6bSHerbert Xu { 342827da44cSJohn Stultz int i; 343827da44cSJohn Stultz 344698365faSWANG Cong idev->stats.ipv6 = alloc_percpu(struct ipstats_mib); 345698365faSWANG Cong if (!idev->stats.ipv6) 3467f7d9a6bSHerbert Xu goto err_ip; 347827da44cSJohn Stultz 348827da44cSJohn Stultz for_each_possible_cpu(i) { 349827da44cSJohn Stultz struct ipstats_mib *addrconf_stats; 350698365faSWANG Cong addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i); 351827da44cSJohn Stultz u64_stats_init(&addrconf_stats->syncp); 352827da44cSJohn Stultz } 353827da44cSJohn Stultz 354827da44cSJohn Stultz 355be281e55SEric Dumazet idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device), 356be281e55SEric Dumazet GFP_KERNEL); 357be281e55SEric Dumazet if (!idev->stats.icmpv6dev) 3587f7d9a6bSHerbert Xu goto err_icmp; 359be281e55SEric Dumazet idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device), 360be281e55SEric Dumazet GFP_KERNEL); 361be281e55SEric Dumazet if (!idev->stats.icmpv6msgdev) 36214878f75SDavid L Stevens goto err_icmpmsg; 3637f7d9a6bSHerbert Xu 3647f7d9a6bSHerbert Xu return 0; 3657f7d9a6bSHerbert Xu 36614878f75SDavid L Stevens err_icmpmsg: 367be281e55SEric Dumazet kfree(idev->stats.icmpv6dev); 3687f7d9a6bSHerbert Xu err_icmp: 369698365faSWANG Cong free_percpu(idev->stats.ipv6); 3707f7d9a6bSHerbert Xu err_ip: 371aaf70ec7SPavel Emelyanov return -ENOMEM; 3727f7d9a6bSHerbert Xu } 3737f7d9a6bSHerbert Xu 3741da177e4SLinus Torvalds static struct inet6_dev *ipv6_add_dev(struct net_device *dev) 3751da177e4SLinus Torvalds { 3761da177e4SLinus Torvalds struct inet6_dev *ndev; 377a317a2f1SWANG Cong int err = -ENOMEM; 3781da177e4SLinus Torvalds 3791da177e4SLinus Torvalds ASSERT_RTNL(); 3801da177e4SLinus Torvalds 3811da177e4SLinus Torvalds if (dev->mtu < IPV6_MIN_MTU) 382a317a2f1SWANG Cong return ERR_PTR(-EINVAL); 3831da177e4SLinus Torvalds 384322f74a4SIngo Oeser ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL); 38563159f29SIan Morris if (!ndev) 386a317a2f1SWANG Cong return ERR_PTR(err); 3871da177e4SLinus Torvalds 3881da177e4SLinus Torvalds rwlock_init(&ndev->lock); 3891da177e4SLinus Torvalds ndev->dev = dev; 390502a2ffdSstephen hemminger INIT_LIST_HEAD(&ndev->addr_list); 391b7b1bfceSHannes Frederic Sowa setup_timer(&ndev->rs_timer, addrconf_rs_timer, 392b7b1bfceSHannes Frederic Sowa (unsigned long)ndev); 393c346dca1SYOSHIFUJI Hideaki memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf)); 3949b29c696SHannes Frederic Sowa 3959b29c696SHannes Frederic Sowa if (ndev->cnf.stable_secret.initialized) 396d35a00b8SFelix Jia ndev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY; 3979b29c696SHannes Frederic Sowa else 398d35a00b8SFelix Jia ndev->cnf.addr_gen_mode = ipv6_devconf_dflt.addr_gen_mode; 3999b29c696SHannes Frederic Sowa 4001da177e4SLinus Torvalds ndev->cnf.mtu6 = dev->mtu; 4011da177e4SLinus Torvalds ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl); 40263159f29SIan Morris if (!ndev->nd_parms) { 4031da177e4SLinus Torvalds kfree(ndev); 404a317a2f1SWANG Cong return ERR_PTR(err); 4051da177e4SLinus Torvalds } 4060187bdfbSBen Hutchings if (ndev->cnf.forwarding) 4070187bdfbSBen Hutchings dev_disable_lro(dev); 4081da177e4SLinus Torvalds /* We refer to the device */ 4091da177e4SLinus Torvalds dev_hold(dev); 4101da177e4SLinus Torvalds 4111da177e4SLinus Torvalds if (snmp6_alloc_dev(ndev) < 0) { 412ba3542e1Sdingtianhong ADBG(KERN_WARNING 413f3213831SJoe Perches "%s: cannot allocate memory for statistics; dev=%s.\n", 414ba3542e1Sdingtianhong __func__, dev->name); 4151da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, ndev->nd_parms); 4168603e33dSRoy Li dev_put(dev); 4178603e33dSRoy Li kfree(ndev); 418a317a2f1SWANG Cong return ERR_PTR(err); 4191da177e4SLinus Torvalds } 4201da177e4SLinus Torvalds 4211da177e4SLinus Torvalds if (snmp6_register_dev(ndev) < 0) { 422ba3542e1Sdingtianhong ADBG(KERN_WARNING 423f3213831SJoe Perches "%s: cannot create /proc/net/dev_snmp6/%s\n", 424ba3542e1Sdingtianhong __func__, dev->name); 425a317a2f1SWANG Cong goto err_release; 4261da177e4SLinus Torvalds } 4271da177e4SLinus Torvalds 4289d6280daSJiri Bohac /* One reference from device. */ 4291be92460SReshetova, Elena refcount_set(&ndev->refcnt, 1); 4301da177e4SLinus Torvalds 4311b34be74SYOSHIFUJI Hideaki if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) 4321b34be74SYOSHIFUJI Hideaki ndev->cnf.accept_dad = -1; 4331b34be74SYOSHIFUJI Hideaki 43407a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 435c7dc89c0SFred L. Templin if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) { 436f3213831SJoe Perches pr_info("%s: Disabled Multicast RS\n", dev->name); 437c7dc89c0SFred L. Templin ndev->cnf.rtr_solicits = 0; 438c7dc89c0SFred L. Templin } 439b077d7abSYOSHIFUJI Hideaki #endif 440b077d7abSYOSHIFUJI Hideaki 441372e6c8fSstephen hemminger INIT_LIST_HEAD(&ndev->tempaddr_list); 44276506a98SJiri Bohac ndev->desync_factor = U32_MAX; 4431da177e4SLinus Torvalds if ((dev->flags&IFF_LOOPBACK) || 4441da177e4SLinus Torvalds dev->type == ARPHRD_TUNNEL || 4459625ed72SYOSHIFUJI Hideaki dev->type == ARPHRD_TUNNEL6 || 4461da177e4SLinus Torvalds dev->type == ARPHRD_SIT || 4471da177e4SLinus Torvalds dev->type == ARPHRD_NONE) { 4480be669bbSJoerg Roedel ndev->cnf.use_tempaddr = -1; 4499d6280daSJiri Bohac } else 4509d6280daSJiri Bohac ipv6_regen_rndid(ndev); 4515d9efa7eSDavid S. Miller 452914faa14SDaniel Borkmann ndev->token = in6addr_any; 4531da177e4SLinus Torvalds 454f24e3d65SMitsuru Chinen if (netif_running(dev) && addrconf_qdisc_ok(dev)) 45553aadcc9SHerbert Xu ndev->if_flags |= IF_READY; 45653aadcc9SHerbert Xu 4571da177e4SLinus Torvalds ipv6_mc_init_dev(ndev); 4581da177e4SLinus Torvalds ndev->tstamp = jiffies; 459a317a2f1SWANG Cong err = addrconf_sysctl_register(ndev); 460a317a2f1SWANG Cong if (err) { 461a317a2f1SWANG Cong ipv6_mc_destroy_dev(ndev); 4622a189f9eSSabrina Dubroca snmp6_unregister_dev(ndev); 463a317a2f1SWANG Cong goto err_release; 464a317a2f1SWANG Cong } 46530c4cf57SDavid L Stevens /* protected by rtnl_lock */ 466cf778b00SEric Dumazet rcu_assign_pointer(dev->ip6_ptr, ndev); 467d88ae4ccSYOSHIFUJI Hideaki 4682c5e8933SHannes Frederic Sowa /* Join interface-local all-node multicast group */ 4692c5e8933SHannes Frederic Sowa ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes); 4702c5e8933SHannes Frederic Sowa 471d88ae4ccSYOSHIFUJI Hideaki /* Join all-node multicast group */ 472f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes); 473d88ae4ccSYOSHIFUJI Hideaki 474d6ddef9eSLi Wei /* Join all-router multicast group if forwarding is set */ 4758b2aaedeSLi Wei if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST)) 476d6ddef9eSLi Wei ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters); 477d6ddef9eSLi Wei 4781da177e4SLinus Torvalds return ndev; 479a317a2f1SWANG Cong 480a317a2f1SWANG Cong err_release: 481a317a2f1SWANG Cong neigh_parms_release(&nd_tbl, ndev->nd_parms); 482a317a2f1SWANG Cong ndev->dead = 1; 483a317a2f1SWANG Cong in6_dev_finish_destroy(ndev); 484a317a2f1SWANG Cong return ERR_PTR(err); 4851da177e4SLinus Torvalds } 4861da177e4SLinus Torvalds 487cee89473SYOSHIFUJI Hideaki static struct inet6_dev *ipv6_find_idev(struct net_device *dev) 4881da177e4SLinus Torvalds { 4891da177e4SLinus Torvalds struct inet6_dev *idev; 4901da177e4SLinus Torvalds 4911da177e4SLinus Torvalds ASSERT_RTNL(); 4921da177e4SLinus Torvalds 493e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 494e21e8467SStephen Hemminger if (!idev) { 495e21e8467SStephen Hemminger idev = ipv6_add_dev(dev); 496a317a2f1SWANG Cong if (IS_ERR(idev)) 4971da177e4SLinus Torvalds return NULL; 4981da177e4SLinus Torvalds } 499c5e33bddSYOSHIFUJI Hideaki 5001da177e4SLinus Torvalds if (dev->flags&IFF_UP) 5011da177e4SLinus Torvalds ipv6_mc_up(idev); 5021da177e4SLinus Torvalds return idev; 5031da177e4SLinus Torvalds } 5041da177e4SLinus Torvalds 505f3a1bfb1SNicolas Dichtel static int inet6_netconf_msgsize_devconf(int type) 506f3a1bfb1SNicolas Dichtel { 507f3a1bfb1SNicolas Dichtel int size = NLMSG_ALIGN(sizeof(struct netconfmsg)) 508f3a1bfb1SNicolas Dichtel + nla_total_size(4); /* NETCONFA_IFINDEX */ 509136ba622SZhang Shengju bool all = false; 510f3a1bfb1SNicolas Dichtel 511136ba622SZhang Shengju if (type == NETCONFA_ALL) 512136ba622SZhang Shengju all = true; 513136ba622SZhang Shengju 514136ba622SZhang Shengju if (all || type == NETCONFA_FORWARDING) 515f3a1bfb1SNicolas Dichtel size += nla_total_size(4); 516b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE 517136ba622SZhang Shengju if (all || type == NETCONFA_MC_FORWARDING) 518d67b8c61SNicolas Dichtel size += nla_total_size(4); 519b1afce95SDavid S. Miller #endif 520136ba622SZhang Shengju if (all || type == NETCONFA_PROXY_NEIGH) 521c92d5491Sstephen hemminger size += nla_total_size(4); 522f3a1bfb1SNicolas Dichtel 523136ba622SZhang Shengju if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) 52435103d11SAndy Gospodarek size += nla_total_size(4); 52535103d11SAndy Gospodarek 526f3a1bfb1SNicolas Dichtel return size; 527f3a1bfb1SNicolas Dichtel } 528f3a1bfb1SNicolas Dichtel 529f3a1bfb1SNicolas Dichtel static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex, 530f3a1bfb1SNicolas Dichtel struct ipv6_devconf *devconf, u32 portid, 531f3a1bfb1SNicolas Dichtel u32 seq, int event, unsigned int flags, 532f3a1bfb1SNicolas Dichtel int type) 533f3a1bfb1SNicolas Dichtel { 534f3a1bfb1SNicolas Dichtel struct nlmsghdr *nlh; 535f3a1bfb1SNicolas Dichtel struct netconfmsg *ncm; 536136ba622SZhang Shengju bool all = false; 537f3a1bfb1SNicolas Dichtel 538f3a1bfb1SNicolas Dichtel nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg), 539f3a1bfb1SNicolas Dichtel flags); 54063159f29SIan Morris if (!nlh) 541f3a1bfb1SNicolas Dichtel return -EMSGSIZE; 542f3a1bfb1SNicolas Dichtel 543136ba622SZhang Shengju if (type == NETCONFA_ALL) 544136ba622SZhang Shengju all = true; 545136ba622SZhang Shengju 546f3a1bfb1SNicolas Dichtel ncm = nlmsg_data(nlh); 547f3a1bfb1SNicolas Dichtel ncm->ncm_family = AF_INET6; 548f3a1bfb1SNicolas Dichtel 549f3a1bfb1SNicolas Dichtel if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0) 550f3a1bfb1SNicolas Dichtel goto nla_put_failure; 551f3a1bfb1SNicolas Dichtel 55223452170SDavid Ahern if (!devconf) 55323452170SDavid Ahern goto out; 55423452170SDavid Ahern 555136ba622SZhang Shengju if ((all || type == NETCONFA_FORWARDING) && 556f3a1bfb1SNicolas Dichtel nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0) 557f3a1bfb1SNicolas Dichtel goto nla_put_failure; 558b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE 559136ba622SZhang Shengju if ((all || type == NETCONFA_MC_FORWARDING) && 560d67b8c61SNicolas Dichtel nla_put_s32(skb, NETCONFA_MC_FORWARDING, 561d67b8c61SNicolas Dichtel devconf->mc_forwarding) < 0) 562d67b8c61SNicolas Dichtel goto nla_put_failure; 563b1afce95SDavid S. Miller #endif 564136ba622SZhang Shengju if ((all || type == NETCONFA_PROXY_NEIGH) && 565c92d5491Sstephen hemminger nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0) 566c92d5491Sstephen hemminger goto nla_put_failure; 567c92d5491Sstephen hemminger 568136ba622SZhang Shengju if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) && 56935103d11SAndy Gospodarek nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 57035103d11SAndy Gospodarek devconf->ignore_routes_with_linkdown) < 0) 57135103d11SAndy Gospodarek goto nla_put_failure; 57235103d11SAndy Gospodarek 57323452170SDavid Ahern out: 574053c095aSJohannes Berg nlmsg_end(skb, nlh); 575053c095aSJohannes Berg return 0; 576f3a1bfb1SNicolas Dichtel 577f3a1bfb1SNicolas Dichtel nla_put_failure: 578f3a1bfb1SNicolas Dichtel nlmsg_cancel(skb, nlh); 579f3a1bfb1SNicolas Dichtel return -EMSGSIZE; 580f3a1bfb1SNicolas Dichtel } 581f3a1bfb1SNicolas Dichtel 58285b3daadSDavid Ahern void inet6_netconf_notify_devconf(struct net *net, int event, int type, 58385b3daadSDavid Ahern int ifindex, struct ipv6_devconf *devconf) 584f3a1bfb1SNicolas Dichtel { 585f3a1bfb1SNicolas Dichtel struct sk_buff *skb; 586f3a1bfb1SNicolas Dichtel int err = -ENOBUFS; 587f3a1bfb1SNicolas Dichtel 588927265bcSEric Dumazet skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_KERNEL); 58963159f29SIan Morris if (!skb) 590f3a1bfb1SNicolas Dichtel goto errout; 591f3a1bfb1SNicolas Dichtel 592f3a1bfb1SNicolas Dichtel err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0, 59385b3daadSDavid Ahern event, 0, type); 594f3a1bfb1SNicolas Dichtel if (err < 0) { 595f3a1bfb1SNicolas Dichtel /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */ 596f3a1bfb1SNicolas Dichtel WARN_ON(err == -EMSGSIZE); 597f3a1bfb1SNicolas Dichtel kfree_skb(skb); 598f3a1bfb1SNicolas Dichtel goto errout; 599f3a1bfb1SNicolas Dichtel } 600927265bcSEric Dumazet rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_KERNEL); 601f3a1bfb1SNicolas Dichtel return; 602f3a1bfb1SNicolas Dichtel errout: 603f3a1bfb1SNicolas Dichtel rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err); 604f3a1bfb1SNicolas Dichtel } 605f3a1bfb1SNicolas Dichtel 60676f8f6cbSNicolas Dichtel static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = { 60776f8f6cbSNicolas Dichtel [NETCONFA_IFINDEX] = { .len = sizeof(int) }, 60876f8f6cbSNicolas Dichtel [NETCONFA_FORWARDING] = { .len = sizeof(int) }, 609c92d5491Sstephen hemminger [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) }, 61035103d11SAndy Gospodarek [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) }, 61176f8f6cbSNicolas Dichtel }; 61276f8f6cbSNicolas Dichtel 61376f8f6cbSNicolas Dichtel static int inet6_netconf_get_devconf(struct sk_buff *in_skb, 614c21ef3e3SDavid Ahern struct nlmsghdr *nlh, 615c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 61676f8f6cbSNicolas Dichtel { 61776f8f6cbSNicolas Dichtel struct net *net = sock_net(in_skb->sk); 61876f8f6cbSNicolas Dichtel struct nlattr *tb[NETCONFA_MAX+1]; 61976f8f6cbSNicolas Dichtel struct netconfmsg *ncm; 62076f8f6cbSNicolas Dichtel struct sk_buff *skb; 62176f8f6cbSNicolas Dichtel struct ipv6_devconf *devconf; 62276f8f6cbSNicolas Dichtel struct inet6_dev *in6_dev; 62376f8f6cbSNicolas Dichtel struct net_device *dev; 62476f8f6cbSNicolas Dichtel int ifindex; 62576f8f6cbSNicolas Dichtel int err; 62676f8f6cbSNicolas Dichtel 62776f8f6cbSNicolas Dichtel err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX, 628c21ef3e3SDavid Ahern devconf_ipv6_policy, extack); 62976f8f6cbSNicolas Dichtel if (err < 0) 63076f8f6cbSNicolas Dichtel goto errout; 63176f8f6cbSNicolas Dichtel 632a97eb33fSAnton Protopopov err = -EINVAL; 63376f8f6cbSNicolas Dichtel if (!tb[NETCONFA_IFINDEX]) 63476f8f6cbSNicolas Dichtel goto errout; 63576f8f6cbSNicolas Dichtel 63676f8f6cbSNicolas Dichtel ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]); 63776f8f6cbSNicolas Dichtel switch (ifindex) { 63876f8f6cbSNicolas Dichtel case NETCONFA_IFINDEX_ALL: 63976f8f6cbSNicolas Dichtel devconf = net->ipv6.devconf_all; 64076f8f6cbSNicolas Dichtel break; 64176f8f6cbSNicolas Dichtel case NETCONFA_IFINDEX_DEFAULT: 64276f8f6cbSNicolas Dichtel devconf = net->ipv6.devconf_dflt; 64376f8f6cbSNicolas Dichtel break; 64476f8f6cbSNicolas Dichtel default: 64576f8f6cbSNicolas Dichtel dev = __dev_get_by_index(net, ifindex); 64663159f29SIan Morris if (!dev) 64776f8f6cbSNicolas Dichtel goto errout; 64876f8f6cbSNicolas Dichtel in6_dev = __in6_dev_get(dev); 64963159f29SIan Morris if (!in6_dev) 65076f8f6cbSNicolas Dichtel goto errout; 65176f8f6cbSNicolas Dichtel devconf = &in6_dev->cnf; 65276f8f6cbSNicolas Dichtel break; 65376f8f6cbSNicolas Dichtel } 65476f8f6cbSNicolas Dichtel 65576f8f6cbSNicolas Dichtel err = -ENOBUFS; 656136ba622SZhang Shengju skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_ATOMIC); 65763159f29SIan Morris if (!skb) 65876f8f6cbSNicolas Dichtel goto errout; 65976f8f6cbSNicolas Dichtel 66076f8f6cbSNicolas Dichtel err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 66176f8f6cbSNicolas Dichtel NETLINK_CB(in_skb).portid, 66276f8f6cbSNicolas Dichtel nlh->nlmsg_seq, RTM_NEWNETCONF, 0, 663136ba622SZhang Shengju NETCONFA_ALL); 66476f8f6cbSNicolas Dichtel if (err < 0) { 66576f8f6cbSNicolas Dichtel /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */ 66676f8f6cbSNicolas Dichtel WARN_ON(err == -EMSGSIZE); 66776f8f6cbSNicolas Dichtel kfree_skb(skb); 66876f8f6cbSNicolas Dichtel goto errout; 66976f8f6cbSNicolas Dichtel } 67076f8f6cbSNicolas Dichtel err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 67176f8f6cbSNicolas Dichtel errout: 67276f8f6cbSNicolas Dichtel return err; 67376f8f6cbSNicolas Dichtel } 67476f8f6cbSNicolas Dichtel 6757a674200SNicolas Dichtel static int inet6_netconf_dump_devconf(struct sk_buff *skb, 6767a674200SNicolas Dichtel struct netlink_callback *cb) 6777a674200SNicolas Dichtel { 6787a674200SNicolas Dichtel struct net *net = sock_net(skb->sk); 6797a674200SNicolas Dichtel int h, s_h; 6807a674200SNicolas Dichtel int idx, s_idx; 6817a674200SNicolas Dichtel struct net_device *dev; 6827a674200SNicolas Dichtel struct inet6_dev *idev; 6837a674200SNicolas Dichtel struct hlist_head *head; 6847a674200SNicolas Dichtel 6857a674200SNicolas Dichtel s_h = cb->args[0]; 6867a674200SNicolas Dichtel s_idx = idx = cb->args[1]; 6877a674200SNicolas Dichtel 6887a674200SNicolas Dichtel for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 6897a674200SNicolas Dichtel idx = 0; 6907a674200SNicolas Dichtel head = &net->dev_index_head[h]; 6917a674200SNicolas Dichtel rcu_read_lock(); 69263998ac2SNicolas Dichtel cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ 69363998ac2SNicolas Dichtel net->dev_base_seq; 6947a674200SNicolas Dichtel hlist_for_each_entry_rcu(dev, head, index_hlist) { 6957a674200SNicolas Dichtel if (idx < s_idx) 6967a674200SNicolas Dichtel goto cont; 6977a674200SNicolas Dichtel idev = __in6_dev_get(dev); 6987a674200SNicolas Dichtel if (!idev) 6997a674200SNicolas Dichtel goto cont; 7007a674200SNicolas Dichtel 7017a674200SNicolas Dichtel if (inet6_netconf_fill_devconf(skb, dev->ifindex, 7027a674200SNicolas Dichtel &idev->cnf, 7037a674200SNicolas Dichtel NETLINK_CB(cb->skb).portid, 7047a674200SNicolas Dichtel cb->nlh->nlmsg_seq, 7057a674200SNicolas Dichtel RTM_NEWNETCONF, 7067a674200SNicolas Dichtel NLM_F_MULTI, 707136ba622SZhang Shengju NETCONFA_ALL) < 0) { 7087a674200SNicolas Dichtel rcu_read_unlock(); 7097a674200SNicolas Dichtel goto done; 7107a674200SNicolas Dichtel } 71163998ac2SNicolas Dichtel nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 7127a674200SNicolas Dichtel cont: 7137a674200SNicolas Dichtel idx++; 7147a674200SNicolas Dichtel } 7157a674200SNicolas Dichtel rcu_read_unlock(); 7167a674200SNicolas Dichtel } 7177a674200SNicolas Dichtel if (h == NETDEV_HASHENTRIES) { 7187a674200SNicolas Dichtel if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL, 7197a674200SNicolas Dichtel net->ipv6.devconf_all, 7207a674200SNicolas Dichtel NETLINK_CB(cb->skb).portid, 7217a674200SNicolas Dichtel cb->nlh->nlmsg_seq, 7227a674200SNicolas Dichtel RTM_NEWNETCONF, NLM_F_MULTI, 723136ba622SZhang Shengju NETCONFA_ALL) < 0) 7247a674200SNicolas Dichtel goto done; 7257a674200SNicolas Dichtel else 7267a674200SNicolas Dichtel h++; 7277a674200SNicolas Dichtel } 7287a674200SNicolas Dichtel if (h == NETDEV_HASHENTRIES + 1) { 7297a674200SNicolas Dichtel if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT, 7307a674200SNicolas Dichtel net->ipv6.devconf_dflt, 7317a674200SNicolas Dichtel NETLINK_CB(cb->skb).portid, 7327a674200SNicolas Dichtel cb->nlh->nlmsg_seq, 7337a674200SNicolas Dichtel RTM_NEWNETCONF, NLM_F_MULTI, 734136ba622SZhang Shengju NETCONFA_ALL) < 0) 7357a674200SNicolas Dichtel goto done; 7367a674200SNicolas Dichtel else 7377a674200SNicolas Dichtel h++; 7387a674200SNicolas Dichtel } 7397a674200SNicolas Dichtel done: 7407a674200SNicolas Dichtel cb->args[0] = h; 7417a674200SNicolas Dichtel cb->args[1] = idx; 7427a674200SNicolas Dichtel 7437a674200SNicolas Dichtel return skb->len; 7447a674200SNicolas Dichtel } 7457a674200SNicolas Dichtel 7461da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 7471da177e4SLinus Torvalds static void dev_forward_change(struct inet6_dev *idev) 7481da177e4SLinus Torvalds { 7491da177e4SLinus Torvalds struct net_device *dev; 7501da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 7511da177e4SLinus Torvalds 7521da177e4SLinus Torvalds if (!idev) 7531da177e4SLinus Torvalds return; 7541da177e4SLinus Torvalds dev = idev->dev; 7550187bdfbSBen Hutchings if (idev->cnf.forwarding) 7560187bdfbSBen Hutchings dev_disable_lro(dev); 7571a940835SAmerigo Wang if (dev->flags & IFF_MULTICAST) { 7582c5e8933SHannes Frederic Sowa if (idev->cnf.forwarding) { 759f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters); 7602c5e8933SHannes Frederic Sowa ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters); 7612c5e8933SHannes Frederic Sowa ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters); 7622c5e8933SHannes Frederic Sowa } else { 763f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters); 7642c5e8933SHannes Frederic Sowa ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters); 7652c5e8933SHannes Frederic Sowa ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters); 7662c5e8933SHannes Frederic Sowa } 7671da177e4SLinus Torvalds } 768502a2ffdSstephen hemminger 769502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 7702c12a74cSMichal Wrobel if (ifa->flags&IFA_F_TENTATIVE) 7712c12a74cSMichal Wrobel continue; 7721da177e4SLinus Torvalds if (idev->cnf.forwarding) 7731da177e4SLinus Torvalds addrconf_join_anycast(ifa); 7741da177e4SLinus Torvalds else 7751da177e4SLinus Torvalds addrconf_leave_anycast(ifa); 7761da177e4SLinus Torvalds } 77785b3daadSDavid Ahern inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF, 77885b3daadSDavid Ahern NETCONFA_FORWARDING, 779f3a1bfb1SNicolas Dichtel dev->ifindex, &idev->cnf); 7801da177e4SLinus Torvalds } 7811da177e4SLinus Torvalds 7821da177e4SLinus Torvalds 783e186932bSPavel Emelyanov static void addrconf_forward_change(struct net *net, __s32 newf) 7841da177e4SLinus Torvalds { 7851da177e4SLinus Torvalds struct net_device *dev; 7861da177e4SLinus Torvalds struct inet6_dev *idev; 7871da177e4SLinus Torvalds 7884acd4945SBen Hutchings for_each_netdev(net, dev) { 7891da177e4SLinus Torvalds idev = __in6_dev_get(dev); 7901da177e4SLinus Torvalds if (idev) { 791e186932bSPavel Emelyanov int changed = (!idev->cnf.forwarding) ^ (!newf); 792e186932bSPavel Emelyanov idev->cnf.forwarding = newf; 7931da177e4SLinus Torvalds if (changed) 7941da177e4SLinus Torvalds dev_forward_change(idev); 7951da177e4SLinus Torvalds } 7961da177e4SLinus Torvalds } 7971da177e4SLinus Torvalds } 798c8fecf22SPavel Emelyanov 799013d97e9SFrancesco Ruggeri static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf) 800c8fecf22SPavel Emelyanov { 801bff16c2fSPavel Emelyanov struct net *net; 802013d97e9SFrancesco Ruggeri int old; 803013d97e9SFrancesco Ruggeri 804013d97e9SFrancesco Ruggeri if (!rtnl_trylock()) 805013d97e9SFrancesco Ruggeri return restart_syscall(); 806bff16c2fSPavel Emelyanov 807bff16c2fSPavel Emelyanov net = (struct net *)table->extra2; 808013d97e9SFrancesco Ruggeri old = *p; 809013d97e9SFrancesco Ruggeri *p = newf; 810c8fecf22SPavel Emelyanov 811013d97e9SFrancesco Ruggeri if (p == &net->ipv6.devconf_dflt->forwarding) { 812f3a1bfb1SNicolas Dichtel if ((!newf) ^ (!old)) 81385b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 81485b3daadSDavid Ahern NETCONFA_FORWARDING, 815f3a1bfb1SNicolas Dichtel NETCONFA_IFINDEX_DEFAULT, 816f3a1bfb1SNicolas Dichtel net->ipv6.devconf_dflt); 817013d97e9SFrancesco Ruggeri rtnl_unlock(); 818013d97e9SFrancesco Ruggeri return 0; 81988af182eSEric W. Biederman } 820b325fddbSStephen Hemminger 821e186932bSPavel Emelyanov if (p == &net->ipv6.devconf_all->forwarding) { 822d26c638cSNicolas Dichtel int old_dflt = net->ipv6.devconf_dflt->forwarding; 823d26c638cSNicolas Dichtel 824e186932bSPavel Emelyanov net->ipv6.devconf_dflt->forwarding = newf; 825d26c638cSNicolas Dichtel if ((!newf) ^ (!old_dflt)) 82685b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 82785b3daadSDavid Ahern NETCONFA_FORWARDING, 828d26c638cSNicolas Dichtel NETCONFA_IFINDEX_DEFAULT, 829d26c638cSNicolas Dichtel net->ipv6.devconf_dflt); 830d26c638cSNicolas Dichtel 831e186932bSPavel Emelyanov addrconf_forward_change(net, newf); 832f3a1bfb1SNicolas Dichtel if ((!newf) ^ (!old)) 83385b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 83485b3daadSDavid Ahern NETCONFA_FORWARDING, 835f3a1bfb1SNicolas Dichtel NETCONFA_IFINDEX_ALL, 836f3a1bfb1SNicolas Dichtel net->ipv6.devconf_all); 837013d97e9SFrancesco Ruggeri } else if ((!newf) ^ (!old)) 838c8fecf22SPavel Emelyanov dev_forward_change((struct inet6_dev *)table->extra1); 8390187bdfbSBen Hutchings rtnl_unlock(); 840c8fecf22SPavel Emelyanov 841013d97e9SFrancesco Ruggeri if (newf) 8427b4da532SDaniel Lezcano rt6_purge_dflt_routers(net); 843b325fddbSStephen Hemminger return 1; 844c8fecf22SPavel Emelyanov } 84535103d11SAndy Gospodarek 84635103d11SAndy Gospodarek static void addrconf_linkdown_change(struct net *net, __s32 newf) 84735103d11SAndy Gospodarek { 84835103d11SAndy Gospodarek struct net_device *dev; 84935103d11SAndy Gospodarek struct inet6_dev *idev; 85035103d11SAndy Gospodarek 85135103d11SAndy Gospodarek for_each_netdev(net, dev) { 85235103d11SAndy Gospodarek idev = __in6_dev_get(dev); 85335103d11SAndy Gospodarek if (idev) { 85435103d11SAndy Gospodarek int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf); 85535103d11SAndy Gospodarek 85635103d11SAndy Gospodarek idev->cnf.ignore_routes_with_linkdown = newf; 85735103d11SAndy Gospodarek if (changed) 85835103d11SAndy Gospodarek inet6_netconf_notify_devconf(dev_net(dev), 85985b3daadSDavid Ahern RTM_NEWNETCONF, 86035103d11SAndy Gospodarek NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 86135103d11SAndy Gospodarek dev->ifindex, 86235103d11SAndy Gospodarek &idev->cnf); 86335103d11SAndy Gospodarek } 86435103d11SAndy Gospodarek } 86535103d11SAndy Gospodarek } 86635103d11SAndy Gospodarek 86735103d11SAndy Gospodarek static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf) 86835103d11SAndy Gospodarek { 86935103d11SAndy Gospodarek struct net *net; 87035103d11SAndy Gospodarek int old; 87135103d11SAndy Gospodarek 87235103d11SAndy Gospodarek if (!rtnl_trylock()) 87335103d11SAndy Gospodarek return restart_syscall(); 87435103d11SAndy Gospodarek 87535103d11SAndy Gospodarek net = (struct net *)table->extra2; 87635103d11SAndy Gospodarek old = *p; 87735103d11SAndy Gospodarek *p = newf; 87835103d11SAndy Gospodarek 87935103d11SAndy Gospodarek if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) { 88035103d11SAndy Gospodarek if ((!newf) ^ (!old)) 88135103d11SAndy Gospodarek inet6_netconf_notify_devconf(net, 88285b3daadSDavid Ahern RTM_NEWNETCONF, 88335103d11SAndy Gospodarek NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 88435103d11SAndy Gospodarek NETCONFA_IFINDEX_DEFAULT, 88535103d11SAndy Gospodarek net->ipv6.devconf_dflt); 88635103d11SAndy Gospodarek rtnl_unlock(); 88735103d11SAndy Gospodarek return 0; 88835103d11SAndy Gospodarek } 88935103d11SAndy Gospodarek 89035103d11SAndy Gospodarek if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) { 89135103d11SAndy Gospodarek net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf; 89235103d11SAndy Gospodarek addrconf_linkdown_change(net, newf); 89335103d11SAndy Gospodarek if ((!newf) ^ (!old)) 89435103d11SAndy Gospodarek inet6_netconf_notify_devconf(net, 89585b3daadSDavid Ahern RTM_NEWNETCONF, 89635103d11SAndy Gospodarek NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 89735103d11SAndy Gospodarek NETCONFA_IFINDEX_ALL, 89835103d11SAndy Gospodarek net->ipv6.devconf_all); 89935103d11SAndy Gospodarek } 90035103d11SAndy Gospodarek rtnl_unlock(); 90135103d11SAndy Gospodarek 90235103d11SAndy Gospodarek return 1; 90335103d11SAndy Gospodarek } 90435103d11SAndy Gospodarek 9051da177e4SLinus Torvalds #endif 9061da177e4SLinus Torvalds 9075c578aedSstephen hemminger /* Nobody refers to this ifaddr, destroy it */ 9081da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp) 9091da177e4SLinus Torvalds { 910c2e21293Sstephen hemminger WARN_ON(!hlist_unhashed(&ifp->addr_lst)); 911547b792cSIlpo Järvinen 9121da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG 91391df42beSJoe Perches pr_debug("%s\n", __func__); 9141da177e4SLinus Torvalds #endif 9151da177e4SLinus Torvalds 9161da177e4SLinus Torvalds in6_dev_put(ifp->idev); 9171da177e4SLinus Torvalds 918c15b1ccaSHannes Frederic Sowa if (cancel_delayed_work(&ifp->dad_work)) 919c15b1ccaSHannes Frederic Sowa pr_notice("delayed DAD work was pending while freeing ifa=%p\n", 920c15b1ccaSHannes Frederic Sowa ifp); 9211da177e4SLinus Torvalds 922e9d3e084SHerbert Xu if (ifp->state != INET6_IFADDR_STATE_DEAD) { 923f3213831SJoe Perches pr_warn("Freeing alive inet6 address %p\n", ifp); 9241da177e4SLinus Torvalds return; 9251da177e4SLinus Torvalds } 92694e187c0SAmerigo Wang ip6_rt_put(ifp->rt); 9271da177e4SLinus Torvalds 928e5785985SLai Jiangshan kfree_rcu(ifp, rcu); 9291da177e4SLinus Torvalds } 9301da177e4SLinus Torvalds 931e55ffac6SBrian Haley static void 932e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp) 933e55ffac6SBrian Haley { 934502a2ffdSstephen hemminger struct list_head *p; 9358a6ce0c0SYOSHIFUJI Hideaki int ifp_scope = ipv6_addr_src_scope(&ifp->addr); 936e55ffac6SBrian Haley 937e55ffac6SBrian Haley /* 938e55ffac6SBrian Haley * Each device address list is sorted in order of scope - 939e55ffac6SBrian Haley * global before linklocal. 940e55ffac6SBrian Haley */ 941502a2ffdSstephen hemminger list_for_each(p, &idev->addr_list) { 942502a2ffdSstephen hemminger struct inet6_ifaddr *ifa 943502a2ffdSstephen hemminger = list_entry(p, struct inet6_ifaddr, if_list); 9448a6ce0c0SYOSHIFUJI Hideaki if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr)) 945e55ffac6SBrian Haley break; 946e55ffac6SBrian Haley } 947e55ffac6SBrian Haley 948b54c9b98SDavid S. Miller list_add_tail(&ifp->if_list, p); 949e55ffac6SBrian Haley } 950e55ffac6SBrian Haley 951ddbe5032SEric Dumazet static u32 inet6_addr_hash(const struct in6_addr *addr) 9523eb84f49SYOSHIFUJI Hideaki { 953ddbe5032SEric Dumazet return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT); 9543eb84f49SYOSHIFUJI Hideaki } 9553eb84f49SYOSHIFUJI Hideaki 9561da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */ 9571da177e4SLinus Torvalds 9581da177e4SLinus Torvalds static struct inet6_ifaddr * 9593f8f5298SJiri Pirko ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, 9603f8f5298SJiri Pirko const struct in6_addr *peer_addr, int pfxlen, 9618a226b2cSJiri Benc int scope, u32 flags, u32 valid_lft, u32 prefered_lft) 9621da177e4SLinus Torvalds { 963df789fe7SDavid Forster struct net *net = dev_net(idev->dev); 9641da177e4SLinus Torvalds struct inet6_ifaddr *ifa = NULL; 9651da177e4SLinus Torvalds struct rt6_info *rt; 9663ad7d246SKrister Johansen struct in6_validator_info i6vi; 9673a88a81dSstephen hemminger unsigned int hash; 9681da177e4SLinus Torvalds int err = 0; 969d68b8270SYOSHIFUJI Hideaki int addr_type = ipv6_addr_type(addr); 970d68b8270SYOSHIFUJI Hideaki 971d68b8270SYOSHIFUJI Hideaki if (addr_type == IPV6_ADDR_ANY || 972d68b8270SYOSHIFUJI Hideaki addr_type & IPV6_ADDR_MULTICAST || 973d68b8270SYOSHIFUJI Hideaki (!(idev->dev->flags & IFF_LOOPBACK) && 974d68b8270SYOSHIFUJI Hideaki addr_type & IPV6_ADDR_LOOPBACK)) 975d68b8270SYOSHIFUJI Hideaki return ERR_PTR(-EADDRNOTAVAIL); 9761da177e4SLinus Torvalds 9778814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 9783ad7d246SKrister Johansen 9793ad7d246SKrister Johansen in6_dev_hold(idev); 9803ad7d246SKrister Johansen 9811da177e4SLinus Torvalds if (idev->dead) { 9821da177e4SLinus Torvalds err = -ENODEV; /*XXX*/ 9831da177e4SLinus Torvalds goto out2; 9841da177e4SLinus Torvalds } 9851da177e4SLinus Torvalds 98656d417b1SBrian Haley if (idev->cnf.disable_ipv6) { 9879bdd8d40SBrian Haley err = -EACCES; 9889bdd8d40SBrian Haley goto out2; 9899bdd8d40SBrian Haley } 9909bdd8d40SBrian Haley 9913ad7d246SKrister Johansen i6vi.i6vi_addr = *addr; 9923ad7d246SKrister Johansen i6vi.i6vi_dev = idev; 9933ad7d246SKrister Johansen rcu_read_unlock_bh(); 9943ad7d246SKrister Johansen 9953ad7d246SKrister Johansen err = inet6addr_validator_notifier_call_chain(NETDEV_UP, &i6vi); 9963ad7d246SKrister Johansen 9973ad7d246SKrister Johansen rcu_read_lock_bh(); 9983ad7d246SKrister Johansen err = notifier_to_errno(err); 9993ad7d246SKrister Johansen if (err) 10003ad7d246SKrister Johansen goto out2; 10013ad7d246SKrister Johansen 10025c578aedSstephen hemminger spin_lock(&addrconf_hash_lock); 10031da177e4SLinus Torvalds 10041da177e4SLinus Torvalds /* Ignore adding duplicate addresses on an interface */ 1005c346dca1SYOSHIFUJI Hideaki if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) { 1006ba3542e1Sdingtianhong ADBG("ipv6_add_addr: already assigned\n"); 10071da177e4SLinus Torvalds err = -EEXIST; 10081da177e4SLinus Torvalds goto out; 10091da177e4SLinus Torvalds } 10101da177e4SLinus Torvalds 1011322f74a4SIngo Oeser ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC); 10121da177e4SLinus Torvalds 101363159f29SIan Morris if (!ifa) { 1014ba3542e1Sdingtianhong ADBG("ipv6_add_addr: malloc failed\n"); 10151da177e4SLinus Torvalds err = -ENOBUFS; 10161da177e4SLinus Torvalds goto out; 10171da177e4SLinus Torvalds } 10181da177e4SLinus Torvalds 10198f031519SDavid S. Miller rt = addrconf_dst_alloc(idev, addr, false); 10201da177e4SLinus Torvalds if (IS_ERR(rt)) { 10211da177e4SLinus Torvalds err = PTR_ERR(rt); 10221da177e4SLinus Torvalds goto out; 10231da177e4SLinus Torvalds } 10241da177e4SLinus Torvalds 1025df789fe7SDavid Forster if (net->ipv6.devconf_all->disable_policy || 1026df789fe7SDavid Forster idev->cnf.disable_policy) 1027df789fe7SDavid Forster rt->dst.flags |= DST_NOPOLICY; 1028df789fe7SDavid Forster 1029bba24896SJiri Pirko neigh_parms_data_state_setall(idev->nd_parms); 1030bba24896SJiri Pirko 10314e3fd7a0SAlexey Dobriyan ifa->addr = *addr; 10323f8f5298SJiri Pirko if (peer_addr) 10333f8f5298SJiri Pirko ifa->peer_addr = *peer_addr; 10341da177e4SLinus Torvalds 10351da177e4SLinus Torvalds spin_lock_init(&ifa->lock); 1036c15b1ccaSHannes Frederic Sowa INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work); 1037c2e21293Sstephen hemminger INIT_HLIST_NODE(&ifa->addr_lst); 10381da177e4SLinus Torvalds ifa->scope = scope; 10391da177e4SLinus Torvalds ifa->prefix_len = pfxlen; 104066eb9f86SMahesh Bandewar ifa->flags = flags; 104166eb9f86SMahesh Bandewar /* No need to add the TENTATIVE flag for addresses with NODAD */ 104266eb9f86SMahesh Bandewar if (!(flags & IFA_F_NODAD)) 104366eb9f86SMahesh Bandewar ifa->flags |= IFA_F_TENTATIVE; 10448a226b2cSJiri Benc ifa->valid_lft = valid_lft; 10458a226b2cSJiri Benc ifa->prefered_lft = prefered_lft; 10461da177e4SLinus Torvalds ifa->cstamp = ifa->tstamp = jiffies; 1047617fe29dSDaniel Borkmann ifa->tokenized = false; 10481da177e4SLinus Torvalds 104957f5f544SKeir Fraser ifa->rt = rt; 105057f5f544SKeir Fraser 10511da177e4SLinus Torvalds ifa->idev = idev; 10521da177e4SLinus Torvalds /* For caller */ 1053271201c0SReshetova, Elena refcount_set(&ifa->refcnt, 1); 10541da177e4SLinus Torvalds 10551da177e4SLinus Torvalds /* Add to big hash table */ 1056ddbe5032SEric Dumazet hash = inet6_addr_hash(addr); 10571da177e4SLinus Torvalds 10585c578aedSstephen hemminger hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]); 10595c578aedSstephen hemminger spin_unlock(&addrconf_hash_lock); 10601da177e4SLinus Torvalds 10611da177e4SLinus Torvalds write_lock(&idev->lock); 10621da177e4SLinus Torvalds /* Add to inet6_dev unicast addr list. */ 1063e55ffac6SBrian Haley ipv6_link_dev_addr(idev, ifa); 10641da177e4SLinus Torvalds 10651da177e4SLinus Torvalds if (ifa->flags&IFA_F_TEMPORARY) { 1066372e6c8fSstephen hemminger list_add(&ifa->tmp_list, &idev->tempaddr_list); 10671da177e4SLinus Torvalds in6_ifa_hold(ifa); 10681da177e4SLinus Torvalds } 10691da177e4SLinus Torvalds 10701da177e4SLinus Torvalds in6_ifa_hold(ifa); 10711da177e4SLinus Torvalds write_unlock(&idev->lock); 10721da177e4SLinus Torvalds out2: 10738814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 10741da177e4SLinus Torvalds 1075fd92833aSYOSHIFUJI Hideaki if (likely(err == 0)) 1076f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_UP, ifa); 10771da177e4SLinus Torvalds else { 10781da177e4SLinus Torvalds kfree(ifa); 10793ad7d246SKrister Johansen in6_dev_put(idev); 10801da177e4SLinus Torvalds ifa = ERR_PTR(err); 10811da177e4SLinus Torvalds } 10821da177e4SLinus Torvalds 10831da177e4SLinus Torvalds return ifa; 10841da177e4SLinus Torvalds out: 10855c578aedSstephen hemminger spin_unlock(&addrconf_hash_lock); 10861da177e4SLinus Torvalds goto out2; 10871da177e4SLinus Torvalds } 10881da177e4SLinus Torvalds 10895b84efecSThomas Haller enum cleanup_prefix_rt_t { 10905b84efecSThomas Haller CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */ 10915b84efecSThomas Haller CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */ 10925b84efecSThomas Haller CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */ 10935b84efecSThomas Haller }; 10945b84efecSThomas Haller 10955b84efecSThomas Haller /* 10965b84efecSThomas Haller * Check, whether the prefix for ifp would still need a prefix route 10975b84efecSThomas Haller * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_* 10985b84efecSThomas Haller * constants. 10995b84efecSThomas Haller * 11005b84efecSThomas Haller * 1) we don't purge prefix if address was not permanent. 11015b84efecSThomas Haller * prefix is managed by its own lifetime. 11025b84efecSThomas Haller * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE. 11035b84efecSThomas Haller * 3) if there are no addresses, delete prefix. 11045b84efecSThomas Haller * 4) if there are still other permanent address(es), 11055b84efecSThomas Haller * corresponding prefix is still permanent. 11065b84efecSThomas Haller * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE, 11075b84efecSThomas Haller * don't purge the prefix, assume user space is managing it. 11085b84efecSThomas Haller * 6) otherwise, update prefix lifetime to the 11095b84efecSThomas Haller * longest valid lifetime among the corresponding 11105b84efecSThomas Haller * addresses on the device. 11115b84efecSThomas Haller * Note: subsequent RA will update lifetime. 11125b84efecSThomas Haller **/ 11135b84efecSThomas Haller static enum cleanup_prefix_rt_t 11145b84efecSThomas Haller check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires) 11155b84efecSThomas Haller { 11165b84efecSThomas Haller struct inet6_ifaddr *ifa; 11175b84efecSThomas Haller struct inet6_dev *idev = ifp->idev; 11185b84efecSThomas Haller unsigned long lifetime; 11195b84efecSThomas Haller enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL; 11205b84efecSThomas Haller 11215b84efecSThomas Haller *expires = jiffies; 11225b84efecSThomas Haller 11235b84efecSThomas Haller list_for_each_entry(ifa, &idev->addr_list, if_list) { 11245b84efecSThomas Haller if (ifa == ifp) 11255b84efecSThomas Haller continue; 11265b84efecSThomas Haller if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr, 11275b84efecSThomas Haller ifp->prefix_len)) 11285b84efecSThomas Haller continue; 11295b84efecSThomas Haller if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE)) 11305b84efecSThomas Haller return CLEANUP_PREFIX_RT_NOP; 11315b84efecSThomas Haller 11325b84efecSThomas Haller action = CLEANUP_PREFIX_RT_EXPIRE; 11335b84efecSThomas Haller 11345b84efecSThomas Haller spin_lock(&ifa->lock); 11355b84efecSThomas Haller 11365b84efecSThomas Haller lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ); 11375b84efecSThomas Haller /* 11385b84efecSThomas Haller * Note: Because this address is 11395b84efecSThomas Haller * not permanent, lifetime < 11405b84efecSThomas Haller * LONG_MAX / HZ here. 11415b84efecSThomas Haller */ 11425b84efecSThomas Haller if (time_before(*expires, ifa->tstamp + lifetime * HZ)) 11435b84efecSThomas Haller *expires = ifa->tstamp + lifetime * HZ; 11445b84efecSThomas Haller spin_unlock(&ifa->lock); 11455b84efecSThomas Haller } 11465b84efecSThomas Haller 11475b84efecSThomas Haller return action; 11485b84efecSThomas Haller } 11495b84efecSThomas Haller 11505b84efecSThomas Haller static void 11515b84efecSThomas Haller cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt) 11525b84efecSThomas Haller { 11535b84efecSThomas Haller struct rt6_info *rt; 11545b84efecSThomas Haller 11555b84efecSThomas Haller rt = addrconf_get_prefix_route(&ifp->addr, 11565b84efecSThomas Haller ifp->prefix_len, 11575b84efecSThomas Haller ifp->idev->dev, 11585b84efecSThomas Haller 0, RTF_GATEWAY | RTF_DEFAULT); 11595b84efecSThomas Haller if (rt) { 11605b84efecSThomas Haller if (del_rt) 11615b84efecSThomas Haller ip6_del_rt(rt); 11625b84efecSThomas Haller else { 11635b84efecSThomas Haller if (!(rt->rt6i_flags & RTF_EXPIRES)) 11645b84efecSThomas Haller rt6_set_expires(rt, expires); 11655b84efecSThomas Haller ip6_rt_put(rt); 11665b84efecSThomas Haller } 11675b84efecSThomas Haller } 11685b84efecSThomas Haller } 11695b84efecSThomas Haller 11705b84efecSThomas Haller 11711da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */ 11721da177e4SLinus Torvalds 11731da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp) 11741da177e4SLinus Torvalds { 11754c5ff6a6SHerbert Xu int state; 11765b84efecSThomas Haller enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP; 11775b84efecSThomas Haller unsigned long expires; 11781da177e4SLinus Torvalds 1179c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 1180c15b1ccaSHannes Frederic Sowa 11818e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 11824c5ff6a6SHerbert Xu state = ifp->state; 1183e9d3e084SHerbert Xu ifp->state = INET6_IFADDR_STATE_DEAD; 11848e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 11854c5ff6a6SHerbert Xu 11864c5ff6a6SHerbert Xu if (state == INET6_IFADDR_STATE_DEAD) 11874c5ff6a6SHerbert Xu goto out; 11881da177e4SLinus Torvalds 11895c578aedSstephen hemminger spin_lock_bh(&addrconf_hash_lock); 11905c578aedSstephen hemminger hlist_del_init_rcu(&ifp->addr_lst); 11915c578aedSstephen hemminger spin_unlock_bh(&addrconf_hash_lock); 11921da177e4SLinus Torvalds 11935b84efecSThomas Haller write_lock_bh(&ifp->idev->lock); 11945d9efa7eSDavid S. Miller 11951da177e4SLinus Torvalds if (ifp->flags&IFA_F_TEMPORARY) { 1196372e6c8fSstephen hemminger list_del(&ifp->tmp_list); 11971da177e4SLinus Torvalds if (ifp->ifpub) { 11981da177e4SLinus Torvalds in6_ifa_put(ifp->ifpub); 11991da177e4SLinus Torvalds ifp->ifpub = NULL; 12001da177e4SLinus Torvalds } 12011da177e4SLinus Torvalds __in6_ifa_put(ifp); 12021da177e4SLinus Torvalds } 12031da177e4SLinus Torvalds 12045b84efecSThomas Haller if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE)) 12055b84efecSThomas Haller action = check_cleanup_prefix_route(ifp, &expires); 12065b84efecSThomas Haller 1207502a2ffdSstephen hemminger list_del_init(&ifp->if_list); 12081da177e4SLinus Torvalds __in6_ifa_put(ifp); 1209502a2ffdSstephen hemminger 12105b84efecSThomas Haller write_unlock_bh(&ifp->idev->lock); 12111da177e4SLinus Torvalds 1212c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifp); 1213b2238566SAndrey Vagin 12141da177e4SLinus Torvalds ipv6_ifa_notify(RTM_DELADDR, ifp); 12151da177e4SLinus Torvalds 1216f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_DOWN, ifp); 12171da177e4SLinus Torvalds 12185b84efecSThomas Haller if (action != CLEANUP_PREFIX_RT_NOP) { 12195b84efecSThomas Haller cleanup_prefix_route(ifp, expires, 12205b84efecSThomas Haller action == CLEANUP_PREFIX_RT_DEL); 12211da177e4SLinus Torvalds } 12221da177e4SLinus Torvalds 1223c3968a85SDaniel Walter /* clean up prefsrc entries */ 1224c3968a85SDaniel Walter rt6_remove_prefsrc(ifp); 12254c5ff6a6SHerbert Xu out: 12261da177e4SLinus Torvalds in6_ifa_put(ifp); 12271da177e4SLinus Torvalds } 12281da177e4SLinus Torvalds 12291da177e4SLinus Torvalds static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift) 12301da177e4SLinus Torvalds { 12311da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 12321da177e4SLinus Torvalds struct in6_addr addr, *tmpaddr; 123376f793e3SLorenzo Colitti unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age; 1234eac55bf9SBenoit Boissinot unsigned long regen_advance; 12351da177e4SLinus Torvalds int tmp_plen; 12361da177e4SLinus Torvalds int ret = 0; 123795c385b4SNeil Horman u32 addr_flags; 123876f793e3SLorenzo Colitti unsigned long now = jiffies; 123976506a98SJiri Bohac long max_desync_factor; 12407aa8e63fSJiri Bohac s32 cnf_temp_preferred_lft; 12411da177e4SLinus Torvalds 124253bd6749SJiri Pirko write_lock_bh(&idev->lock); 12431da177e4SLinus Torvalds if (ift) { 12441da177e4SLinus Torvalds spin_lock_bh(&ift->lock); 12451da177e4SLinus Torvalds memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8); 12461da177e4SLinus Torvalds spin_unlock_bh(&ift->lock); 12471da177e4SLinus Torvalds tmpaddr = &addr; 12481da177e4SLinus Torvalds } else { 12491da177e4SLinus Torvalds tmpaddr = NULL; 12501da177e4SLinus Torvalds } 12511da177e4SLinus Torvalds retry: 12521da177e4SLinus Torvalds in6_dev_hold(idev); 12531da177e4SLinus Torvalds if (idev->cnf.use_tempaddr <= 0) { 125453bd6749SJiri Pirko write_unlock_bh(&idev->lock); 1255f3213831SJoe Perches pr_info("%s: use_tempaddr is disabled\n", __func__); 12561da177e4SLinus Torvalds in6_dev_put(idev); 12571da177e4SLinus Torvalds ret = -1; 12581da177e4SLinus Torvalds goto out; 12591da177e4SLinus Torvalds } 12601da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 12611da177e4SLinus Torvalds if (ifp->regen_count++ >= idev->cnf.regen_max_retry) { 12621da177e4SLinus Torvalds idev->cnf.use_tempaddr = -1; /*XXX*/ 12631da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 126453bd6749SJiri Pirko write_unlock_bh(&idev->lock); 1265f3213831SJoe Perches pr_warn("%s: regeneration time exceeded - disabled temporary address support\n", 1266f3213831SJoe Perches __func__); 12671da177e4SLinus Torvalds in6_dev_put(idev); 12681da177e4SLinus Torvalds ret = -1; 12691da177e4SLinus Torvalds goto out; 12701da177e4SLinus Torvalds } 12711da177e4SLinus Torvalds in6_ifa_hold(ifp); 12721da177e4SLinus Torvalds memcpy(addr.s6_addr, ifp->addr.s6_addr, 8); 12739d6280daSJiri Bohac ipv6_try_regen_rndid(idev, tmpaddr); 12741da177e4SLinus Torvalds memcpy(&addr.s6_addr[8], idev->rndid, 8); 127576f793e3SLorenzo Colitti age = (now - ifp->tstamp) / HZ; 12761da177e4SLinus Torvalds 1277eac55bf9SBenoit Boissinot regen_advance = idev->cnf.regen_max_retry * 1278eac55bf9SBenoit Boissinot idev->cnf.dad_transmits * 12791f9248e5SJiri Pirko NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ; 128076506a98SJiri Bohac 128176506a98SJiri Bohac /* recalculate max_desync_factor each time and update 128276506a98SJiri Bohac * idev->desync_factor if it's larger 128376506a98SJiri Bohac */ 12847aa8e63fSJiri Bohac cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft); 128576506a98SJiri Bohac max_desync_factor = min_t(__u32, 128676506a98SJiri Bohac idev->cnf.max_desync_factor, 12877aa8e63fSJiri Bohac cnf_temp_preferred_lft - regen_advance); 128876506a98SJiri Bohac 128976506a98SJiri Bohac if (unlikely(idev->desync_factor > max_desync_factor)) { 129076506a98SJiri Bohac if (max_desync_factor > 0) { 129176506a98SJiri Bohac get_random_bytes(&idev->desync_factor, 129276506a98SJiri Bohac sizeof(idev->desync_factor)); 129376506a98SJiri Bohac idev->desync_factor %= max_desync_factor; 129476506a98SJiri Bohac } else { 129576506a98SJiri Bohac idev->desync_factor = 0; 129676506a98SJiri Bohac } 129776506a98SJiri Bohac } 129876506a98SJiri Bohac 129976506a98SJiri Bohac tmp_valid_lft = min_t(__u32, 130076506a98SJiri Bohac ifp->valid_lft, 130176506a98SJiri Bohac idev->cnf.temp_valid_lft + age); 13027aa8e63fSJiri Bohac tmp_prefered_lft = cnf_temp_preferred_lft + age - 130376506a98SJiri Bohac idev->desync_factor; 130476506a98SJiri Bohac tmp_prefered_lft = min_t(__u32, ifp->prefered_lft, tmp_prefered_lft); 130576506a98SJiri Bohac tmp_plen = ifp->prefix_len; 130676506a98SJiri Bohac tmp_tstamp = ifp->tstamp; 130776506a98SJiri Bohac spin_unlock_bh(&ifp->lock); 130876506a98SJiri Bohac 130953bd6749SJiri Pirko write_unlock_bh(&idev->lock); 131095c385b4SNeil Horman 1311eac55bf9SBenoit Boissinot /* A temporary address is created only if this calculated Preferred 1312eac55bf9SBenoit Boissinot * Lifetime is greater than REGEN_ADVANCE time units. In particular, 1313eac55bf9SBenoit Boissinot * an implementation must not create a temporary address with a zero 1314eac55bf9SBenoit Boissinot * Preferred Lifetime. 1315ecab6701SHeiner Kallweit * Use age calculation as in addrconf_verify to avoid unnecessary 1316ecab6701SHeiner Kallweit * temporary addresses being generated. 1317eac55bf9SBenoit Boissinot */ 1318ecab6701SHeiner Kallweit age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; 1319ecab6701SHeiner Kallweit if (tmp_prefered_lft <= regen_advance + age) { 1320eac55bf9SBenoit Boissinot in6_ifa_put(ifp); 1321eac55bf9SBenoit Boissinot in6_dev_put(idev); 1322eac55bf9SBenoit Boissinot ret = -1; 1323eac55bf9SBenoit Boissinot goto out; 1324eac55bf9SBenoit Boissinot } 1325eac55bf9SBenoit Boissinot 132695c385b4SNeil Horman addr_flags = IFA_F_TEMPORARY; 132795c385b4SNeil Horman /* set in addrconf_prefix_rcv() */ 132895c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 132995c385b4SNeil Horman addr_flags |= IFA_F_OPTIMISTIC; 133095c385b4SNeil Horman 13314b08a8f1SHannes Frederic Sowa ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen, 13328a226b2cSJiri Benc ipv6_addr_scope(&addr), addr_flags, 13334b08a8f1SHannes Frederic Sowa tmp_valid_lft, tmp_prefered_lft); 13344b08a8f1SHannes Frederic Sowa if (IS_ERR(ift)) { 13351da177e4SLinus Torvalds in6_ifa_put(ifp); 13361da177e4SLinus Torvalds in6_dev_put(idev); 1337f3213831SJoe Perches pr_info("%s: retry temporary address regeneration\n", __func__); 13381da177e4SLinus Torvalds tmpaddr = &addr; 133953bd6749SJiri Pirko write_lock_bh(&idev->lock); 13401da177e4SLinus Torvalds goto retry; 13411da177e4SLinus Torvalds } 13421da177e4SLinus Torvalds 13431da177e4SLinus Torvalds spin_lock_bh(&ift->lock); 13441da177e4SLinus Torvalds ift->ifpub = ifp; 134576f793e3SLorenzo Colitti ift->cstamp = now; 13461da177e4SLinus Torvalds ift->tstamp = tmp_tstamp; 13471da177e4SLinus Torvalds spin_unlock_bh(&ift->lock); 13481da177e4SLinus Torvalds 1349cf22f9a2SDavid S. Miller addrconf_dad_start(ift); 13501da177e4SLinus Torvalds in6_ifa_put(ift); 13511da177e4SLinus Torvalds in6_dev_put(idev); 13521da177e4SLinus Torvalds out: 13531da177e4SLinus Torvalds return ret; 13541da177e4SLinus Torvalds } 13551da177e4SLinus Torvalds 13561da177e4SLinus Torvalds /* 1357072047e4SYOSHIFUJI Hideaki * Choose an appropriate source address (RFC3484) 13581da177e4SLinus Torvalds */ 1359a9b05723SYOSHIFUJI Hideaki enum { 1360a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_INIT = 0, 1361a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_LOCAL, 1362a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_SCOPE, 1363a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PREFERRED, 1364a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6 1365a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_HOA, 1366a9b05723SYOSHIFUJI Hideaki #endif 1367a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_OIF, 1368a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_LABEL, 1369a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PRIVACY, 1370a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_ORCHID, 1371a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PREFIX, 13727fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 13737fd2561eSErik Kline IPV6_SADDR_RULE_NOT_OPTIMISTIC, 13747fd2561eSErik Kline #endif 1375a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_MAX 1376072047e4SYOSHIFUJI Hideaki }; 1377072047e4SYOSHIFUJI Hideaki 1378a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score { 1379a9b05723SYOSHIFUJI Hideaki int rule; 1380a9b05723SYOSHIFUJI Hideaki int addr_type; 1381a9b05723SYOSHIFUJI Hideaki struct inet6_ifaddr *ifa; 1382a9b05723SYOSHIFUJI Hideaki DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX); 1383a9b05723SYOSHIFUJI Hideaki int scopedist; 1384a9b05723SYOSHIFUJI Hideaki int matchlen; 1385a9b05723SYOSHIFUJI Hideaki }; 1386a9b05723SYOSHIFUJI Hideaki 1387a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst { 13889acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *addr; 1389a9b05723SYOSHIFUJI Hideaki int ifindex; 1390a9b05723SYOSHIFUJI Hideaki int scope; 1391a9b05723SYOSHIFUJI Hideaki int label; 13927cbca67cSYOSHIFUJI Hideaki unsigned int prefs; 1393a9b05723SYOSHIFUJI Hideaki }; 1394072047e4SYOSHIFUJI Hideaki 1395b6f99a21SDave Jones static inline int ipv6_saddr_preferred(int type) 13961da177e4SLinus Torvalds { 139745bb0060SUlrich Weber if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK)) 1398072047e4SYOSHIFUJI Hideaki return 1; 1399072047e4SYOSHIFUJI Hideaki return 0; 14001da177e4SLinus Torvalds } 14011da177e4SLinus Torvalds 14027fd2561eSErik Kline static inline bool ipv6_use_optimistic_addr(struct inet6_dev *idev) 14037fd2561eSErik Kline { 14047fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 14057fd2561eSErik Kline return idev && idev->cnf.optimistic_dad && idev->cnf.use_optimistic; 14067fd2561eSErik Kline #else 14077fd2561eSErik Kline return false; 14087fd2561eSErik Kline #endif 14097fd2561eSErik Kline } 14107fd2561eSErik Kline 14113de23255SBenjamin Thery static int ipv6_get_saddr_eval(struct net *net, 14123de23255SBenjamin Thery struct ipv6_saddr_score *score, 1413a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst *dst, 1414a9b05723SYOSHIFUJI Hideaki int i) 1415a9b05723SYOSHIFUJI Hideaki { 1416a9b05723SYOSHIFUJI Hideaki int ret; 1417a9b05723SYOSHIFUJI Hideaki 1418a9b05723SYOSHIFUJI Hideaki if (i <= score->rule) { 1419a9b05723SYOSHIFUJI Hideaki switch (i) { 1420a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_SCOPE: 1421a9b05723SYOSHIFUJI Hideaki ret = score->scopedist; 1422a9b05723SYOSHIFUJI Hideaki break; 1423a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFIX: 1424a9b05723SYOSHIFUJI Hideaki ret = score->matchlen; 1425a9b05723SYOSHIFUJI Hideaki break; 1426a9b05723SYOSHIFUJI Hideaki default: 1427a9b05723SYOSHIFUJI Hideaki ret = !!test_bit(i, score->scorebits); 1428a9b05723SYOSHIFUJI Hideaki } 1429a9b05723SYOSHIFUJI Hideaki goto out; 1430a9b05723SYOSHIFUJI Hideaki } 1431a9b05723SYOSHIFUJI Hideaki 1432a9b05723SYOSHIFUJI Hideaki switch (i) { 1433a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_INIT: 1434a9b05723SYOSHIFUJI Hideaki /* Rule 0: remember if hiscore is not ready yet */ 1435a9b05723SYOSHIFUJI Hideaki ret = !!score->ifa; 1436a9b05723SYOSHIFUJI Hideaki break; 1437a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_LOCAL: 1438a9b05723SYOSHIFUJI Hideaki /* Rule 1: Prefer same address */ 1439a9b05723SYOSHIFUJI Hideaki ret = ipv6_addr_equal(&score->ifa->addr, dst->addr); 1440a9b05723SYOSHIFUJI Hideaki break; 1441a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_SCOPE: 1442a9b05723SYOSHIFUJI Hideaki /* Rule 2: Prefer appropriate scope 1443a9b05723SYOSHIFUJI Hideaki * 1444a9b05723SYOSHIFUJI Hideaki * ret 1445a9b05723SYOSHIFUJI Hideaki * ^ 1446a9b05723SYOSHIFUJI Hideaki * -1 | d 15 1447a9b05723SYOSHIFUJI Hideaki * ---+--+-+---> scope 1448a9b05723SYOSHIFUJI Hideaki * | 1449a9b05723SYOSHIFUJI Hideaki * | d is scope of the destination. 1450a9b05723SYOSHIFUJI Hideaki * B-d | \ 1451a9b05723SYOSHIFUJI Hideaki * | \ <- smaller scope is better if 1452db9c7c39Sstephen hemminger * B-15 | \ if scope is enough for destination. 1453a9b05723SYOSHIFUJI Hideaki * | ret = B - scope (-1 <= scope >= d <= 15). 1454a9b05723SYOSHIFUJI Hideaki * d-C-1 | / 1455a9b05723SYOSHIFUJI Hideaki * |/ <- greater is better 1456a9b05723SYOSHIFUJI Hideaki * -C / if scope is not enough for destination. 1457a9b05723SYOSHIFUJI Hideaki * /| ret = scope - C (-1 <= d < scope <= 15). 1458a9b05723SYOSHIFUJI Hideaki * 1459a9b05723SYOSHIFUJI Hideaki * d - C - 1 < B -15 (for all -1 <= d <= 15). 1460a9b05723SYOSHIFUJI Hideaki * C > d + 14 - B >= 15 + 14 - B = 29 - B. 1461a9b05723SYOSHIFUJI Hideaki * Assume B = 0 and we get C > 29. 1462a9b05723SYOSHIFUJI Hideaki */ 1463a9b05723SYOSHIFUJI Hideaki ret = __ipv6_addr_src_scope(score->addr_type); 1464a9b05723SYOSHIFUJI Hideaki if (ret >= dst->scope) 1465a9b05723SYOSHIFUJI Hideaki ret = -ret; 1466a9b05723SYOSHIFUJI Hideaki else 1467a9b05723SYOSHIFUJI Hideaki ret -= 128; /* 30 is enough */ 1468a9b05723SYOSHIFUJI Hideaki score->scopedist = ret; 1469a9b05723SYOSHIFUJI Hideaki break; 1470a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFERRED: 14717fd2561eSErik Kline { 1472a9b05723SYOSHIFUJI Hideaki /* Rule 3: Avoid deprecated and optimistic addresses */ 14737fd2561eSErik Kline u8 avoid = IFA_F_DEPRECATED; 14747fd2561eSErik Kline 14757fd2561eSErik Kline if (!ipv6_use_optimistic_addr(score->ifa->idev)) 14767fd2561eSErik Kline avoid |= IFA_F_OPTIMISTIC; 1477a9b05723SYOSHIFUJI Hideaki ret = ipv6_saddr_preferred(score->addr_type) || 14787fd2561eSErik Kline !(score->ifa->flags & avoid); 1479a9b05723SYOSHIFUJI Hideaki break; 14807fd2561eSErik Kline } 1481a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6 1482a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_HOA: 14837cbca67cSYOSHIFUJI Hideaki { 1484a9b05723SYOSHIFUJI Hideaki /* Rule 4: Prefer home address */ 14857cbca67cSYOSHIFUJI Hideaki int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA); 14867cbca67cSYOSHIFUJI Hideaki ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome; 1487a9b05723SYOSHIFUJI Hideaki break; 14887cbca67cSYOSHIFUJI Hideaki } 1489a9b05723SYOSHIFUJI Hideaki #endif 1490a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_OIF: 1491a9b05723SYOSHIFUJI Hideaki /* Rule 5: Prefer outgoing interface */ 1492a9b05723SYOSHIFUJI Hideaki ret = (!dst->ifindex || 1493a9b05723SYOSHIFUJI Hideaki dst->ifindex == score->ifa->idev->dev->ifindex); 1494a9b05723SYOSHIFUJI Hideaki break; 1495a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_LABEL: 1496a9b05723SYOSHIFUJI Hideaki /* Rule 6: Prefer matching label */ 14973de23255SBenjamin Thery ret = ipv6_addr_label(net, 14983de23255SBenjamin Thery &score->ifa->addr, score->addr_type, 1499a9b05723SYOSHIFUJI Hideaki score->ifa->idev->dev->ifindex) == dst->label; 1500a9b05723SYOSHIFUJI Hideaki break; 1501a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PRIVACY: 15027cbca67cSYOSHIFUJI Hideaki { 1503a9b05723SYOSHIFUJI Hideaki /* Rule 7: Prefer public address 150425985edcSLucas De Marchi * Note: prefer temporary address if use_tempaddr >= 2 1505a9b05723SYOSHIFUJI Hideaki */ 15067cbca67cSYOSHIFUJI Hideaki int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ? 15077cbca67cSYOSHIFUJI Hideaki !!(dst->prefs & IPV6_PREFER_SRC_TMP) : 15087cbca67cSYOSHIFUJI Hideaki score->ifa->idev->cnf.use_tempaddr >= 2; 15097cbca67cSYOSHIFUJI Hideaki ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp; 1510a9b05723SYOSHIFUJI Hideaki break; 15117cbca67cSYOSHIFUJI Hideaki } 1512a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_ORCHID: 1513a9b05723SYOSHIFUJI Hideaki /* Rule 8-: Prefer ORCHID vs ORCHID or 1514a9b05723SYOSHIFUJI Hideaki * non-ORCHID vs non-ORCHID 1515a9b05723SYOSHIFUJI Hideaki */ 1516a9b05723SYOSHIFUJI Hideaki ret = !(ipv6_addr_orchid(&score->ifa->addr) ^ 1517a9b05723SYOSHIFUJI Hideaki ipv6_addr_orchid(dst->addr)); 1518a9b05723SYOSHIFUJI Hideaki break; 1519a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFIX: 1520a9b05723SYOSHIFUJI Hideaki /* Rule 8: Use longest matching prefix */ 152191b4b04fSYOSHIFUJI Hideaki / 吉藤英明 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr); 152291b4b04fSYOSHIFUJI Hideaki / 吉藤英明 if (ret > score->ifa->prefix_len) 152391b4b04fSYOSHIFUJI Hideaki / 吉藤英明 ret = score->ifa->prefix_len; 152491b4b04fSYOSHIFUJI Hideaki / 吉藤英明 score->matchlen = ret; 1525a9b05723SYOSHIFUJI Hideaki break; 15267fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 15277fd2561eSErik Kline case IPV6_SADDR_RULE_NOT_OPTIMISTIC: 15287fd2561eSErik Kline /* Optimistic addresses still have lower precedence than other 15297fd2561eSErik Kline * preferred addresses. 15307fd2561eSErik Kline */ 15317fd2561eSErik Kline ret = !(score->ifa->flags & IFA_F_OPTIMISTIC); 15327fd2561eSErik Kline break; 15337fd2561eSErik Kline #endif 1534a9b05723SYOSHIFUJI Hideaki default: 1535a9b05723SYOSHIFUJI Hideaki ret = 0; 1536a9b05723SYOSHIFUJI Hideaki } 1537a9b05723SYOSHIFUJI Hideaki 1538a9b05723SYOSHIFUJI Hideaki if (ret) 1539a9b05723SYOSHIFUJI Hideaki __set_bit(i, score->scorebits); 1540a9b05723SYOSHIFUJI Hideaki score->rule = i; 1541a9b05723SYOSHIFUJI Hideaki out: 1542a9b05723SYOSHIFUJI Hideaki return ret; 1543a9b05723SYOSHIFUJI Hideaki } 1544a9b05723SYOSHIFUJI Hideaki 1545c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 static int __ipv6_dev_get_saddr(struct net *net, 15469131f3deSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_dst *dst, 15479131f3deSYOSHIFUJI Hideaki/吉藤英明 struct inet6_dev *idev, 1548c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_score *scores, 1549c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 int hiscore_idx) 15501da177e4SLinus Torvalds { 1551c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx]; 1552072047e4SYOSHIFUJI Hideaki 15531da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1554502a2ffdSstephen hemminger list_for_each_entry(score->ifa, &idev->addr_list, if_list) { 1555a9b05723SYOSHIFUJI Hideaki int i; 1556072047e4SYOSHIFUJI Hideaki 1557a9b05723SYOSHIFUJI Hideaki /* 15586b3ae80aSYOSHIFUJI Hideaki * - Tentative Address (RFC2462 section 5.4) 15596b3ae80aSYOSHIFUJI Hideaki * - A tentative address is not considered 15606b3ae80aSYOSHIFUJI Hideaki * "assigned to an interface" in the traditional 156195c385b4SNeil Horman * sense, unless it is also flagged as optimistic. 15626b3ae80aSYOSHIFUJI Hideaki * - Candidate Source Address (section 4) 1563072047e4SYOSHIFUJI Hideaki * - In any case, anycast addresses, multicast 1564072047e4SYOSHIFUJI Hideaki * addresses, and the unspecified address MUST 1565072047e4SYOSHIFUJI Hideaki * NOT be included in a candidate set. 1566072047e4SYOSHIFUJI Hideaki */ 1567a9b05723SYOSHIFUJI Hideaki if ((score->ifa->flags & IFA_F_TENTATIVE) && 1568a9b05723SYOSHIFUJI Hideaki (!(score->ifa->flags & IFA_F_OPTIMISTIC))) 15696b3ae80aSYOSHIFUJI Hideaki continue; 1570a9b05723SYOSHIFUJI Hideaki 1571a9b05723SYOSHIFUJI Hideaki score->addr_type = __ipv6_addr_type(&score->ifa->addr); 1572a9b05723SYOSHIFUJI Hideaki 1573a9b05723SYOSHIFUJI Hideaki if (unlikely(score->addr_type == IPV6_ADDR_ANY || 1574a9b05723SYOSHIFUJI Hideaki score->addr_type & IPV6_ADDR_MULTICAST)) { 1575ba7a46f1SJoe Perches net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s", 15769131f3deSYOSHIFUJI Hideaki/吉藤英明 idev->dev->name); 15771da177e4SLinus Torvalds continue; 1578072047e4SYOSHIFUJI Hideaki } 1579072047e4SYOSHIFUJI Hideaki 1580a9b05723SYOSHIFUJI Hideaki score->rule = -1; 1581a9b05723SYOSHIFUJI Hideaki bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX); 1582072047e4SYOSHIFUJI Hideaki 1583a9b05723SYOSHIFUJI Hideaki for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) { 1584a9b05723SYOSHIFUJI Hideaki int minihiscore, miniscore; 1585072047e4SYOSHIFUJI Hideaki 15869131f3deSYOSHIFUJI Hideaki/吉藤英明 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i); 15879131f3deSYOSHIFUJI Hideaki/吉藤英明 miniscore = ipv6_get_saddr_eval(net, score, dst, i); 1588072047e4SYOSHIFUJI Hideaki 1589a9b05723SYOSHIFUJI Hideaki if (minihiscore > miniscore) { 1590a9b05723SYOSHIFUJI Hideaki if (i == IPV6_SADDR_RULE_SCOPE && 1591a9b05723SYOSHIFUJI Hideaki score->scopedist > 0) { 1592a9b05723SYOSHIFUJI Hideaki /* 1593a9b05723SYOSHIFUJI Hideaki * special case: 1594a9b05723SYOSHIFUJI Hideaki * each remaining entry 1595a9b05723SYOSHIFUJI Hideaki * has too small (not enough) 1596a9b05723SYOSHIFUJI Hideaki * scope, because ifa entries 1597a9b05723SYOSHIFUJI Hideaki * are sorted by their scope 1598a9b05723SYOSHIFUJI Hideaki * values. 1599072047e4SYOSHIFUJI Hideaki */ 16009131f3deSYOSHIFUJI Hideaki/吉藤英明 goto out; 1601072047e4SYOSHIFUJI Hideaki } 1602a9b05723SYOSHIFUJI Hideaki break; 1603a9b05723SYOSHIFUJI Hideaki } else if (minihiscore < miniscore) { 1604a9b05723SYOSHIFUJI Hideaki if (hiscore->ifa) 1605a9b05723SYOSHIFUJI Hideaki in6_ifa_put(hiscore->ifa); 160699cd07a5SJuha-Matti Tapio 1607a9b05723SYOSHIFUJI Hideaki in6_ifa_hold(score->ifa); 1608072047e4SYOSHIFUJI Hideaki 1609a0bffffcSIlpo Järvinen swap(hiscore, score); 1610c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 hiscore_idx = 1 - hiscore_idx; 1611a9b05723SYOSHIFUJI Hideaki 1612a9b05723SYOSHIFUJI Hideaki /* restore our iterator */ 1613a9b05723SYOSHIFUJI Hideaki score->ifa = hiscore->ifa; 1614a9b05723SYOSHIFUJI Hideaki 1615a9b05723SYOSHIFUJI Hideaki break; 16161da177e4SLinus Torvalds } 1617a9b05723SYOSHIFUJI Hideaki } 1618a9b05723SYOSHIFUJI Hideaki } 16199131f3deSYOSHIFUJI Hideaki/吉藤英明 out: 16201da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 1621c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 return hiscore_idx; 16221da177e4SLinus Torvalds } 16239131f3deSYOSHIFUJI Hideaki/吉藤英明 1624afbac601SDavid Ahern static int ipv6_get_saddr_master(struct net *net, 1625afbac601SDavid Ahern const struct net_device *dst_dev, 1626afbac601SDavid Ahern const struct net_device *master, 1627afbac601SDavid Ahern struct ipv6_saddr_dst *dst, 1628afbac601SDavid Ahern struct ipv6_saddr_score *scores, 1629afbac601SDavid Ahern int hiscore_idx) 1630afbac601SDavid Ahern { 1631afbac601SDavid Ahern struct inet6_dev *idev; 1632afbac601SDavid Ahern 1633afbac601SDavid Ahern idev = __in6_dev_get(dst_dev); 1634afbac601SDavid Ahern if (idev) 1635afbac601SDavid Ahern hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev, 1636afbac601SDavid Ahern scores, hiscore_idx); 1637afbac601SDavid Ahern 1638afbac601SDavid Ahern idev = __in6_dev_get(master); 1639afbac601SDavid Ahern if (idev) 1640afbac601SDavid Ahern hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev, 1641afbac601SDavid Ahern scores, hiscore_idx); 1642afbac601SDavid Ahern 1643afbac601SDavid Ahern return hiscore_idx; 1644afbac601SDavid Ahern } 1645afbac601SDavid Ahern 16469131f3deSYOSHIFUJI Hideaki/吉藤英明 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev, 16479131f3deSYOSHIFUJI Hideaki/吉藤英明 const struct in6_addr *daddr, unsigned int prefs, 16489131f3deSYOSHIFUJI Hideaki/吉藤英明 struct in6_addr *saddr) 16499131f3deSYOSHIFUJI Hideaki/吉藤英明 { 1650c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_score scores[2], *hiscore; 16519131f3deSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_dst dst; 16529131f3deSYOSHIFUJI Hideaki/吉藤英明 struct inet6_dev *idev; 16539131f3deSYOSHIFUJI Hideaki/吉藤英明 struct net_device *dev; 16549131f3deSYOSHIFUJI Hideaki/吉藤英明 int dst_type; 16559131f3deSYOSHIFUJI Hideaki/吉藤英明 bool use_oif_addr = false; 1656c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 int hiscore_idx = 0; 16579131f3deSYOSHIFUJI Hideaki/吉藤英明 16589131f3deSYOSHIFUJI Hideaki/吉藤英明 dst_type = __ipv6_addr_type(daddr); 16599131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.addr = daddr; 16609131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.ifindex = dst_dev ? dst_dev->ifindex : 0; 16619131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.scope = __ipv6_addr_src_scope(dst_type); 16629131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex); 16639131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.prefs = prefs; 16649131f3deSYOSHIFUJI Hideaki/吉藤英明 1665c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 scores[hiscore_idx].rule = -1; 1666c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 scores[hiscore_idx].ifa = NULL; 16679131f3deSYOSHIFUJI Hideaki/吉藤英明 16689131f3deSYOSHIFUJI Hideaki/吉藤英明 rcu_read_lock(); 16699131f3deSYOSHIFUJI Hideaki/吉藤英明 16709131f3deSYOSHIFUJI Hideaki/吉藤英明 /* Candidate Source Address (section 4) 16719131f3deSYOSHIFUJI Hideaki/吉藤英明 * - multicast and link-local destination address, 16729131f3deSYOSHIFUJI Hideaki/吉藤英明 * the set of candidate source address MUST only 16739131f3deSYOSHIFUJI Hideaki/吉藤英明 * include addresses assigned to interfaces 16749131f3deSYOSHIFUJI Hideaki/吉藤英明 * belonging to the same link as the outgoing 16759131f3deSYOSHIFUJI Hideaki/吉藤英明 * interface. 16769131f3deSYOSHIFUJI Hideaki/吉藤英明 * (- For site-local destination addresses, the 16779131f3deSYOSHIFUJI Hideaki/吉藤英明 * set of candidate source addresses MUST only 16789131f3deSYOSHIFUJI Hideaki/吉藤英明 * include addresses assigned to interfaces 16799131f3deSYOSHIFUJI Hideaki/吉藤英明 * belonging to the same site as the outgoing 16809131f3deSYOSHIFUJI Hideaki/吉藤英明 * interface.) 16813985e8a3SErik Kline * - "It is RECOMMENDED that the candidate source addresses 16823985e8a3SErik Kline * be the set of unicast addresses assigned to the 16833985e8a3SErik Kline * interface that will be used to send to the destination 16843985e8a3SErik Kline * (the 'outgoing' interface)." (RFC 6724) 16859131f3deSYOSHIFUJI Hideaki/吉藤英明 */ 16869131f3deSYOSHIFUJI Hideaki/吉藤英明 if (dst_dev) { 16879131f3deSYOSHIFUJI Hideaki/吉藤英明 idev = __in6_dev_get(dst_dev); 16883985e8a3SErik Kline if ((dst_type & IPV6_ADDR_MULTICAST) || 16893985e8a3SErik Kline dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL || 16903985e8a3SErik Kline (idev && idev->cnf.use_oif_addrs_only)) { 16919131f3deSYOSHIFUJI Hideaki/吉藤英明 use_oif_addr = true; 16929131f3deSYOSHIFUJI Hideaki/吉藤英明 } 16939131f3deSYOSHIFUJI Hideaki/吉藤英明 } 16949131f3deSYOSHIFUJI Hideaki/吉藤英明 16959131f3deSYOSHIFUJI Hideaki/吉藤英明 if (use_oif_addr) { 1696c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 if (idev) 1697c15df306SYOSHIFUJI Hideaki hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx); 16989131f3deSYOSHIFUJI Hideaki/吉藤英明 } else { 1699afbac601SDavid Ahern const struct net_device *master; 1700afbac601SDavid Ahern int master_idx = 0; 1701afbac601SDavid Ahern 1702afbac601SDavid Ahern /* if dst_dev exists and is enslaved to an L3 device, then 1703afbac601SDavid Ahern * prefer addresses from dst_dev and then the master over 1704afbac601SDavid Ahern * any other enslaved devices in the L3 domain. 1705afbac601SDavid Ahern */ 1706afbac601SDavid Ahern master = l3mdev_master_dev_rcu(dst_dev); 1707afbac601SDavid Ahern if (master) { 1708afbac601SDavid Ahern master_idx = master->ifindex; 1709afbac601SDavid Ahern 1710afbac601SDavid Ahern hiscore_idx = ipv6_get_saddr_master(net, dst_dev, 1711afbac601SDavid Ahern master, &dst, 1712afbac601SDavid Ahern scores, hiscore_idx); 1713afbac601SDavid Ahern 1714afbac601SDavid Ahern if (scores[hiscore_idx].ifa) 1715afbac601SDavid Ahern goto out; 1716afbac601SDavid Ahern } 1717afbac601SDavid Ahern 17189131f3deSYOSHIFUJI Hideaki/吉藤英明 for_each_netdev_rcu(net, dev) { 1719afbac601SDavid Ahern /* only consider addresses on devices in the 1720afbac601SDavid Ahern * same L3 domain 1721afbac601SDavid Ahern */ 1722afbac601SDavid Ahern if (l3mdev_master_ifindex_rcu(dev) != master_idx) 1723afbac601SDavid Ahern continue; 17249131f3deSYOSHIFUJI Hideaki/吉藤英明 idev = __in6_dev_get(dev); 17259131f3deSYOSHIFUJI Hideaki/吉藤英明 if (!idev) 17269131f3deSYOSHIFUJI Hideaki/吉藤英明 continue; 1727c15df306SYOSHIFUJI Hideaki hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx); 17289131f3deSYOSHIFUJI Hideaki/吉藤英明 } 17299131f3deSYOSHIFUJI Hideaki/吉藤英明 } 1730afbac601SDavid Ahern 1731afbac601SDavid Ahern out: 17328814c4b5SYOSHIFUJI Hideaki rcu_read_unlock(); 17331da177e4SLinus Torvalds 1734c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 hiscore = &scores[hiscore_idx]; 1735a9b05723SYOSHIFUJI Hideaki if (!hiscore->ifa) 1736072047e4SYOSHIFUJI Hideaki return -EADDRNOTAVAIL; 17371da177e4SLinus Torvalds 17384e3fd7a0SAlexey Dobriyan *saddr = hiscore->ifa->addr; 1739a9b05723SYOSHIFUJI Hideaki in6_ifa_put(hiscore->ifa); 1740072047e4SYOSHIFUJI Hideaki return 0; 17411da177e4SLinus Torvalds } 17425e5f3f0fSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_dev_get_saddr); 17431da177e4SLinus Torvalds 17448965779dSAmerigo Wang int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr, 1745479840ffSJiri Pirko u32 banned_flags) 1746b7b1bfceSHannes Frederic Sowa { 1747b7b1bfceSHannes Frederic Sowa struct inet6_ifaddr *ifp; 1748b7b1bfceSHannes Frederic Sowa int err = -EADDRNOTAVAIL; 1749b7b1bfceSHannes Frederic Sowa 1750602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) { 1751602582caSHannes Frederic Sowa if (ifp->scope > IFA_LINK) 1752602582caSHannes Frederic Sowa break; 1753b7b1bfceSHannes Frederic Sowa if (ifp->scope == IFA_LINK && 1754b7b1bfceSHannes Frederic Sowa !(ifp->flags & banned_flags)) { 1755b7b1bfceSHannes Frederic Sowa *addr = ifp->addr; 1756b7b1bfceSHannes Frederic Sowa err = 0; 1757b7b1bfceSHannes Frederic Sowa break; 1758b7b1bfceSHannes Frederic Sowa } 1759b7b1bfceSHannes Frederic Sowa } 1760b7b1bfceSHannes Frederic Sowa return err; 1761b7b1bfceSHannes Frederic Sowa } 1762b7b1bfceSHannes Frederic Sowa 176395c385b4SNeil Horman int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr, 1764479840ffSJiri Pirko u32 banned_flags) 17651da177e4SLinus Torvalds { 17661da177e4SLinus Torvalds struct inet6_dev *idev; 17671da177e4SLinus Torvalds int err = -EADDRNOTAVAIL; 17681da177e4SLinus Torvalds 17698814c4b5SYOSHIFUJI Hideaki rcu_read_lock(); 1770e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 1771e21e8467SStephen Hemminger if (idev) { 17721da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1773b7b1bfceSHannes Frederic Sowa err = __ipv6_get_lladdr(idev, addr, banned_flags); 17741da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 17751da177e4SLinus Torvalds } 17768814c4b5SYOSHIFUJI Hideaki rcu_read_unlock(); 17771da177e4SLinus Torvalds return err; 17781da177e4SLinus Torvalds } 17791da177e4SLinus Torvalds 17801da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev) 17811da177e4SLinus Torvalds { 17821da177e4SLinus Torvalds int cnt = 0; 17831da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 17841da177e4SLinus Torvalds 17851da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1786502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) 17871da177e4SLinus Torvalds cnt++; 17881da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 17891da177e4SLinus Torvalds return cnt; 17901da177e4SLinus Torvalds } 17911da177e4SLinus Torvalds 1792b71d1d42SEric Dumazet int ipv6_chk_addr(struct net *net, const struct in6_addr *addr, 17932a7851bfSFlorian Westphal const struct net_device *dev, int strict) 17941da177e4SLinus Torvalds { 1795c58da4c6SErik Kline return ipv6_chk_addr_and_flags(net, addr, dev, strict, IFA_F_TENTATIVE); 1796c58da4c6SErik Kline } 1797c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr); 1798c58da4c6SErik Kline 1799c58da4c6SErik Kline int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr, 1800c58da4c6SErik Kline const struct net_device *dev, int strict, 1801c58da4c6SErik Kline u32 banned_flags) 1802c58da4c6SErik Kline { 1803eedf042aSStephen Hemminger struct inet6_ifaddr *ifp; 1804ddbe5032SEric Dumazet unsigned int hash = inet6_addr_hash(addr); 1805c58da4c6SErik Kline u32 ifp_flags; 18061da177e4SLinus Torvalds 18075c578aedSstephen hemminger rcu_read_lock_bh(); 1808b67bfe0dSSasha Levin hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) { 1809878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 1810bfeade08SDaniel Lezcano continue; 1811c58da4c6SErik Kline /* Decouple optimistic from tentative for evaluation here. 1812c58da4c6SErik Kline * Ban optimistic addresses explicitly, when required. 1813c58da4c6SErik Kline */ 1814c58da4c6SErik Kline ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC) 1815c58da4c6SErik Kline ? (ifp->flags&~IFA_F_TENTATIVE) 1816c58da4c6SErik Kline : ifp->flags; 18171da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr) && 1818c58da4c6SErik Kline !(ifp_flags&banned_flags) && 181963159f29SIan Morris (!dev || ifp->idev->dev == dev || 1820eedf042aSStephen Hemminger !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) { 18215c578aedSstephen hemminger rcu_read_unlock_bh(); 1822eedf042aSStephen Hemminger return 1; 1823eedf042aSStephen Hemminger } 1824eedf042aSStephen Hemminger } 18255c578aedSstephen hemminger 1826eedf042aSStephen Hemminger rcu_read_unlock_bh(); 1827eedf042aSStephen Hemminger return 0; 18281da177e4SLinus Torvalds } 1829c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr_and_flags); 18307159039aSYOSHIFUJI Hideaki 18313e81c6daSDavid S. Miller static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr, 183206bfe655SDaniel Lezcano struct net_device *dev) 18331da177e4SLinus Torvalds { 1834ddbe5032SEric Dumazet unsigned int hash = inet6_addr_hash(addr); 18351da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 18361da177e4SLinus Torvalds 1837b67bfe0dSSasha Levin hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) { 1838878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 183906bfe655SDaniel Lezcano continue; 18401da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr)) { 184163159f29SIan Morris if (!dev || ifp->idev->dev == dev) 18423e81c6daSDavid S. Miller return true; 18431da177e4SLinus Torvalds } 18441da177e4SLinus Torvalds } 18453e81c6daSDavid S. Miller return false; 18461da177e4SLinus Torvalds } 18471da177e4SLinus Torvalds 18487df37ff3SCatalin\(ux\) M. BOIE /* Compares an address/prefix_len with addresses on device @dev. 18497df37ff3SCatalin\(ux\) M. BOIE * If one is found it returns true. 18507df37ff3SCatalin\(ux\) M. BOIE */ 18517df37ff3SCatalin\(ux\) M. BOIE bool ipv6_chk_custom_prefix(const struct in6_addr *addr, 18527df37ff3SCatalin\(ux\) M. BOIE const unsigned int prefix_len, struct net_device *dev) 18537df37ff3SCatalin\(ux\) M. BOIE { 18547df37ff3SCatalin\(ux\) M. BOIE struct inet6_dev *idev; 18557df37ff3SCatalin\(ux\) M. BOIE struct inet6_ifaddr *ifa; 18567df37ff3SCatalin\(ux\) M. BOIE bool ret = false; 18577df37ff3SCatalin\(ux\) M. BOIE 18587df37ff3SCatalin\(ux\) M. BOIE rcu_read_lock(); 18597df37ff3SCatalin\(ux\) M. BOIE idev = __in6_dev_get(dev); 18607df37ff3SCatalin\(ux\) M. BOIE if (idev) { 18617df37ff3SCatalin\(ux\) M. BOIE read_lock_bh(&idev->lock); 18627df37ff3SCatalin\(ux\) M. BOIE list_for_each_entry(ifa, &idev->addr_list, if_list) { 18637df37ff3SCatalin\(ux\) M. BOIE ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len); 18647df37ff3SCatalin\(ux\) M. BOIE if (ret) 18657df37ff3SCatalin\(ux\) M. BOIE break; 18667df37ff3SCatalin\(ux\) M. BOIE } 18677df37ff3SCatalin\(ux\) M. BOIE read_unlock_bh(&idev->lock); 18687df37ff3SCatalin\(ux\) M. BOIE } 18697df37ff3SCatalin\(ux\) M. BOIE rcu_read_unlock(); 18707df37ff3SCatalin\(ux\) M. BOIE 18717df37ff3SCatalin\(ux\) M. BOIE return ret; 18727df37ff3SCatalin\(ux\) M. BOIE } 18737df37ff3SCatalin\(ux\) M. BOIE EXPORT_SYMBOL(ipv6_chk_custom_prefix); 18747df37ff3SCatalin\(ux\) M. BOIE 1875b71d1d42SEric Dumazet int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev) 187652eeeb84SYOSHIFUJI Hideaki { 187752eeeb84SYOSHIFUJI Hideaki struct inet6_dev *idev; 187852eeeb84SYOSHIFUJI Hideaki struct inet6_ifaddr *ifa; 187952eeeb84SYOSHIFUJI Hideaki int onlink; 188052eeeb84SYOSHIFUJI Hideaki 188152eeeb84SYOSHIFUJI Hideaki onlink = 0; 188252eeeb84SYOSHIFUJI Hideaki rcu_read_lock(); 188352eeeb84SYOSHIFUJI Hideaki idev = __in6_dev_get(dev); 188452eeeb84SYOSHIFUJI Hideaki if (idev) { 188552eeeb84SYOSHIFUJI Hideaki read_lock_bh(&idev->lock); 1886502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 188752eeeb84SYOSHIFUJI Hideaki onlink = ipv6_prefix_equal(addr, &ifa->addr, 188852eeeb84SYOSHIFUJI Hideaki ifa->prefix_len); 188952eeeb84SYOSHIFUJI Hideaki if (onlink) 189052eeeb84SYOSHIFUJI Hideaki break; 189152eeeb84SYOSHIFUJI Hideaki } 189252eeeb84SYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 189352eeeb84SYOSHIFUJI Hideaki } 189452eeeb84SYOSHIFUJI Hideaki rcu_read_unlock(); 189552eeeb84SYOSHIFUJI Hideaki return onlink; 189652eeeb84SYOSHIFUJI Hideaki } 189752eeeb84SYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_prefix); 189852eeeb84SYOSHIFUJI Hideaki 18999acd9f3aSYOSHIFUJI Hideaki struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr, 19001cab3da6SDaniel Lezcano struct net_device *dev, int strict) 19011da177e4SLinus Torvalds { 1902b79d1d54SDavid S. Miller struct inet6_ifaddr *ifp, *result = NULL; 1903ddbe5032SEric Dumazet unsigned int hash = inet6_addr_hash(addr); 19041da177e4SLinus Torvalds 19055c578aedSstephen hemminger rcu_read_lock_bh(); 1906b67bfe0dSSasha Levin hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) { 1907878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 19081cab3da6SDaniel Lezcano continue; 19091da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr)) { 191063159f29SIan Morris if (!dev || ifp->idev->dev == dev || 19111da177e4SLinus Torvalds !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) { 1912b79d1d54SDavid S. Miller result = ifp; 19131da177e4SLinus Torvalds in6_ifa_hold(ifp); 19141da177e4SLinus Torvalds break; 19151da177e4SLinus Torvalds } 19161da177e4SLinus Torvalds } 19171da177e4SLinus Torvalds } 19185c578aedSstephen hemminger rcu_read_unlock_bh(); 19191da177e4SLinus Torvalds 1920b79d1d54SDavid S. Miller return result; 19211da177e4SLinus Torvalds } 19221da177e4SLinus Torvalds 19231da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */ 19241da177e4SLinus Torvalds 1925cc411d0bSBrian Haley static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed) 19261da177e4SLinus Torvalds { 19273d171f39SLubomir Rintel if (dad_failed) 19283d171f39SLubomir Rintel ifp->flags |= IFA_F_DADFAILED; 19293d171f39SLubomir Rintel 1930ec8add2aSSabrina Dubroca if (ifp->flags&IFA_F_TEMPORARY) { 19311da177e4SLinus Torvalds struct inet6_ifaddr *ifpub; 19321da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 19331da177e4SLinus Torvalds ifpub = ifp->ifpub; 19341da177e4SLinus Torvalds if (ifpub) { 19351da177e4SLinus Torvalds in6_ifa_hold(ifpub); 19361da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 19371da177e4SLinus Torvalds ipv6_create_tempaddr(ifpub, ifp); 19381da177e4SLinus Torvalds in6_ifa_put(ifpub); 19391da177e4SLinus Torvalds } else { 19401da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 19411da177e4SLinus Torvalds } 19421da177e4SLinus Torvalds ipv6_del_addr(ifp); 1943ec8add2aSSabrina Dubroca } else if (ifp->flags&IFA_F_PERMANENT || !dad_failed) { 1944ec8add2aSSabrina Dubroca spin_lock_bh(&ifp->lock); 1945ec8add2aSSabrina Dubroca addrconf_del_dad_work(ifp); 1946ec8add2aSSabrina Dubroca ifp->flags |= IFA_F_TENTATIVE; 1947ec8add2aSSabrina Dubroca spin_unlock_bh(&ifp->lock); 1948ec8add2aSSabrina Dubroca if (dad_failed) 1949ec8add2aSSabrina Dubroca ipv6_ifa_notify(0, ifp); 1950ec8add2aSSabrina Dubroca in6_ifa_put(ifp); 1951c15b1ccaSHannes Frederic Sowa } else { 19521da177e4SLinus Torvalds ipv6_del_addr(ifp); 19531da177e4SLinus Torvalds } 1954c15b1ccaSHannes Frederic Sowa } 19551da177e4SLinus Torvalds 1956f2344a13SHerbert Xu static int addrconf_dad_end(struct inet6_ifaddr *ifp) 1957f2344a13SHerbert Xu { 1958f2344a13SHerbert Xu int err = -ENOENT; 1959f2344a13SHerbert Xu 19608e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 1961f2344a13SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DAD) { 1962f2344a13SHerbert Xu ifp->state = INET6_IFADDR_STATE_POSTDAD; 1963f2344a13SHerbert Xu err = 0; 1964f2344a13SHerbert Xu } 19658e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 1966f2344a13SHerbert Xu 1967f2344a13SHerbert Xu return err; 1968f2344a13SHerbert Xu } 1969f2344a13SHerbert Xu 19703c21edbdSYOSHIFUJI Hideaki void addrconf_dad_failure(struct inet6_ifaddr *ifp) 19713c21edbdSYOSHIFUJI Hideaki { 19721b34be74SYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev; 19731855b7c3SHannes Frederic Sowa struct net *net = dev_net(ifp->idev->dev); 19749bdd8d40SBrian Haley 1975853dc2e0SUrsula Braun if (addrconf_dad_end(ifp)) { 1976853dc2e0SUrsula Braun in6_ifa_put(ifp); 1977f2344a13SHerbert Xu return; 1978853dc2e0SUrsula Braun } 1979f2344a13SHerbert Xu 1980e87cc472SJoe Perches net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n", 19810522fea6SJens Rosenboom ifp->idev->dev->name, &ifp->addr); 19829bdd8d40SBrian Haley 19835f40ef77SHannes Frederic Sowa spin_lock_bh(&ifp->lock); 19841b34be74SYOSHIFUJI Hideaki 19855f40ef77SHannes Frederic Sowa if (ifp->flags & IFA_F_STABLE_PRIVACY) { 19865f40ef77SHannes Frederic Sowa int scope = ifp->scope; 19875f40ef77SHannes Frederic Sowa u32 flags = ifp->flags; 19885f40ef77SHannes Frederic Sowa struct in6_addr new_addr; 19895f40ef77SHannes Frederic Sowa struct inet6_ifaddr *ifp2; 19905f40ef77SHannes Frederic Sowa u32 valid_lft, preferred_lft; 19915f40ef77SHannes Frederic Sowa int pfxlen = ifp->prefix_len; 19925f40ef77SHannes Frederic Sowa int retries = ifp->stable_privacy_retry + 1; 19935f40ef77SHannes Frederic Sowa 19941855b7c3SHannes Frederic Sowa if (retries > net->ipv6.sysctl.idgen_retries) { 19955f40ef77SHannes Frederic Sowa net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n", 19965f40ef77SHannes Frederic Sowa ifp->idev->dev->name); 19975f40ef77SHannes Frederic Sowa goto errdad; 19985f40ef77SHannes Frederic Sowa } 19995f40ef77SHannes Frederic Sowa 20005f40ef77SHannes Frederic Sowa new_addr = ifp->addr; 20015f40ef77SHannes Frederic Sowa if (ipv6_generate_stable_address(&new_addr, retries, 20025f40ef77SHannes Frederic Sowa idev)) 20035f40ef77SHannes Frederic Sowa goto errdad; 20045f40ef77SHannes Frederic Sowa 20055f40ef77SHannes Frederic Sowa valid_lft = ifp->valid_lft; 20065f40ef77SHannes Frederic Sowa preferred_lft = ifp->prefered_lft; 20075f40ef77SHannes Frederic Sowa 20085f40ef77SHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 20095f40ef77SHannes Frederic Sowa 20105f40ef77SHannes Frederic Sowa if (idev->cnf.max_addresses && 20115f40ef77SHannes Frederic Sowa ipv6_count_addresses(idev) >= 20125f40ef77SHannes Frederic Sowa idev->cnf.max_addresses) 20135f40ef77SHannes Frederic Sowa goto lock_errdad; 20145f40ef77SHannes Frederic Sowa 20155f40ef77SHannes Frederic Sowa net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n", 20165f40ef77SHannes Frederic Sowa ifp->idev->dev->name); 20175f40ef77SHannes Frederic Sowa 20185f40ef77SHannes Frederic Sowa ifp2 = ipv6_add_addr(idev, &new_addr, NULL, pfxlen, 20195f40ef77SHannes Frederic Sowa scope, flags, valid_lft, 20205f40ef77SHannes Frederic Sowa preferred_lft); 20215f40ef77SHannes Frederic Sowa if (IS_ERR(ifp2)) 20225f40ef77SHannes Frederic Sowa goto lock_errdad; 20235f40ef77SHannes Frederic Sowa 20245f40ef77SHannes Frederic Sowa spin_lock_bh(&ifp2->lock); 20255f40ef77SHannes Frederic Sowa ifp2->stable_privacy_retry = retries; 20265f40ef77SHannes Frederic Sowa ifp2->state = INET6_IFADDR_STATE_PREDAD; 20275f40ef77SHannes Frederic Sowa spin_unlock_bh(&ifp2->lock); 20285f40ef77SHannes Frederic Sowa 20291855b7c3SHannes Frederic Sowa addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay); 20305f40ef77SHannes Frederic Sowa in6_ifa_put(ifp2); 20315f40ef77SHannes Frederic Sowa lock_errdad: 20325f40ef77SHannes Frederic Sowa spin_lock_bh(&ifp->lock); 20331b34be74SYOSHIFUJI Hideaki } 20341b34be74SYOSHIFUJI Hideaki 20355f40ef77SHannes Frederic Sowa errdad: 2036c15b1ccaSHannes Frederic Sowa /* transition from _POSTDAD to _ERRDAD */ 2037c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_ERRDAD; 20388e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 2039c15b1ccaSHannes Frederic Sowa 2040c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 0); 2041751eb6b6SWei Yongjun in6_ifa_put(ifp); 20423c21edbdSYOSHIFUJI Hideaki } 20431da177e4SLinus Torvalds 2044a9ed4a29SSabrina Dubroca /* Join to solicited addr multicast group. 2045a9ed4a29SSabrina Dubroca * caller must hold RTNL */ 2046b71d1d42SEric Dumazet void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr) 20471da177e4SLinus Torvalds { 20481da177e4SLinus Torvalds struct in6_addr maddr; 20491da177e4SLinus Torvalds 20501da177e4SLinus Torvalds if (dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 20511da177e4SLinus Torvalds return; 20521da177e4SLinus Torvalds 20531da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr); 20541da177e4SLinus Torvalds ipv6_dev_mc_inc(dev, &maddr); 20551da177e4SLinus Torvalds } 20561da177e4SLinus Torvalds 2057a9ed4a29SSabrina Dubroca /* caller must hold RTNL */ 2058b71d1d42SEric Dumazet void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr) 20591da177e4SLinus Torvalds { 20601da177e4SLinus Torvalds struct in6_addr maddr; 20611da177e4SLinus Torvalds 20621da177e4SLinus Torvalds if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 20631da177e4SLinus Torvalds return; 20641da177e4SLinus Torvalds 20651da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr); 20661da177e4SLinus Torvalds __ipv6_dev_mc_dec(idev, &maddr); 20671da177e4SLinus Torvalds } 20681da177e4SLinus Torvalds 2069a9ed4a29SSabrina Dubroca /* caller must hold RTNL */ 207020380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp) 20711da177e4SLinus Torvalds { 20721da177e4SLinus Torvalds struct in6_addr addr; 2073c15b1ccaSHannes Frederic Sowa 207488ad3149SHannes Frederic Sowa if (ifp->prefix_len >= 127) /* RFC 6164 */ 20752bda8a0cSBjørn Mork return; 20761da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 20771da177e4SLinus Torvalds if (ipv6_addr_any(&addr)) 20781da177e4SLinus Torvalds return; 2079013b4d90SWANG Cong __ipv6_dev_ac_inc(ifp->idev, &addr); 20801da177e4SLinus Torvalds } 20811da177e4SLinus Torvalds 2082a9ed4a29SSabrina Dubroca /* caller must hold RTNL */ 208320380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp) 20841da177e4SLinus Torvalds { 20851da177e4SLinus Torvalds struct in6_addr addr; 2086c15b1ccaSHannes Frederic Sowa 208788ad3149SHannes Frederic Sowa if (ifp->prefix_len >= 127) /* RFC 6164 */ 208832019e65SYOSHIFUJI Hideaki return; 20891da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 20901da177e4SLinus Torvalds if (ipv6_addr_any(&addr)) 20911da177e4SLinus Torvalds return; 20921da177e4SLinus Torvalds __ipv6_dev_ac_dec(ifp->idev, &addr); 20931da177e4SLinus Torvalds } 20941da177e4SLinus Torvalds 20958a7a4b47SAlexander Aring static int addrconf_ifid_6lowpan(u8 *eui, struct net_device *dev) 209606a4c1c5Salex.bluesman.smirnov@gmail.com { 20978a7a4b47SAlexander Aring switch (dev->addr_len) { 20988a7a4b47SAlexander Aring case ETH_ALEN: 20999dae2e03SLuiz Augusto von Dentz memcpy(eui, dev->dev_addr, 3); 21009dae2e03SLuiz Augusto von Dentz eui[3] = 0xFF; 21019dae2e03SLuiz Augusto von Dentz eui[4] = 0xFE; 21029dae2e03SLuiz Augusto von Dentz memcpy(eui + 5, dev->dev_addr + 3, 3); 21039dae2e03SLuiz Augusto von Dentz break; 21048a7a4b47SAlexander Aring case EUI64_ADDR_LEN: 21055241c2d7SAlexander Aring memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN); 21065e98a36eSYOSHIFUJI Hideaki / 吉藤英明 eui[0] ^= 2; 21078a7a4b47SAlexander Aring break; 21088a7a4b47SAlexander Aring default: 21098a7a4b47SAlexander Aring return -1; 21108a7a4b47SAlexander Aring } 21118a7a4b47SAlexander Aring 211206a4c1c5Salex.bluesman.smirnov@gmail.com return 0; 211306a4c1c5Salex.bluesman.smirnov@gmail.com } 211406a4c1c5Salex.bluesman.smirnov@gmail.com 2115cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev) 2116cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 { 2117cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 union fwnet_hwaddr *ha; 2118cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 2119cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 if (dev->addr_len != FWNET_ALEN) 2120cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return -1; 2121cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 2122cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 ha = (union fwnet_hwaddr *)dev->dev_addr; 2123cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 2124cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id)); 2125cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 eui[0] ^= 2; 2126cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return 0; 2127cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 } 2128cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 2129073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev) 2130073a8e0eSYOSHIFUJI Hideaki { 21311da177e4SLinus Torvalds /* XXX: inherit EUI-64 from other interface -- yoshfuji */ 21321da177e4SLinus Torvalds if (dev->addr_len != ARCNET_ALEN) 21331da177e4SLinus Torvalds return -1; 21341da177e4SLinus Torvalds memset(eui, 0, 7); 21351da177e4SLinus Torvalds eui[7] = *(u8 *)dev->dev_addr; 21361da177e4SLinus Torvalds return 0; 2137073a8e0eSYOSHIFUJI Hideaki } 2138073a8e0eSYOSHIFUJI Hideaki 2139073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev) 2140073a8e0eSYOSHIFUJI Hideaki { 21411da177e4SLinus Torvalds if (dev->addr_len != INFINIBAND_ALEN) 21421da177e4SLinus Torvalds return -1; 21431da177e4SLinus Torvalds memcpy(eui, dev->dev_addr + 12, 8); 21441da177e4SLinus Torvalds eui[0] |= 2; 21451da177e4SLinus Torvalds return 0; 21461da177e4SLinus Torvalds } 2147073a8e0eSYOSHIFUJI Hideaki 2148c61393eaSstephen hemminger static int __ipv6_isatap_ifid(u8 *eui, __be32 addr) 2149dfd982baSYOSHIFUJI Hideaki { 21509af28511SSascha Hlusiak if (addr == 0) 21519af28511SSascha Hlusiak return -1; 2152dfd982baSYOSHIFUJI Hideaki eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) || 2153dfd982baSYOSHIFUJI Hideaki ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) || 2154dfd982baSYOSHIFUJI Hideaki ipv4_is_private_172(addr) || ipv4_is_test_192(addr) || 2155dfd982baSYOSHIFUJI Hideaki ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) || 2156dfd982baSYOSHIFUJI Hideaki ipv4_is_test_198(addr) || ipv4_is_multicast(addr) || 2157dfd982baSYOSHIFUJI Hideaki ipv4_is_lbcast(addr)) ? 0x00 : 0x02; 2158dfd982baSYOSHIFUJI Hideaki eui[1] = 0; 2159dfd982baSYOSHIFUJI Hideaki eui[2] = 0x5E; 2160dfd982baSYOSHIFUJI Hideaki eui[3] = 0xFE; 2161dfd982baSYOSHIFUJI Hideaki memcpy(eui + 4, &addr, 4); 2162dfd982baSYOSHIFUJI Hideaki return 0; 2163dfd982baSYOSHIFUJI Hideaki } 2164dfd982baSYOSHIFUJI Hideaki 2165dfd982baSYOSHIFUJI Hideaki static int addrconf_ifid_sit(u8 *eui, struct net_device *dev) 2166dfd982baSYOSHIFUJI Hideaki { 2167dfd982baSYOSHIFUJI Hideaki if (dev->priv_flags & IFF_ISATAP) 2168dfd982baSYOSHIFUJI Hideaki return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); 2169dfd982baSYOSHIFUJI Hideaki return -1; 2170dfd982baSYOSHIFUJI Hideaki } 2171dfd982baSYOSHIFUJI Hideaki 2172aee80b54Sstephen hemminger static int addrconf_ifid_gre(u8 *eui, struct net_device *dev) 2173aee80b54Sstephen hemminger { 2174aee80b54Sstephen hemminger return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); 2175aee80b54Sstephen hemminger } 2176aee80b54Sstephen hemminger 2177e837735eSNicolas Dichtel static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev) 2178e837735eSNicolas Dichtel { 2179e837735eSNicolas Dichtel memcpy(eui, dev->perm_addr, 3); 2180e837735eSNicolas Dichtel memcpy(eui + 5, dev->perm_addr + 3, 3); 2181e837735eSNicolas Dichtel eui[3] = 0xFF; 2182e837735eSNicolas Dichtel eui[4] = 0xFE; 2183e837735eSNicolas Dichtel eui[0] ^= 2; 2184e837735eSNicolas Dichtel return 0; 2185e837735eSNicolas Dichtel } 2186e837735eSNicolas Dichtel 2187073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev) 2188073a8e0eSYOSHIFUJI Hideaki { 2189073a8e0eSYOSHIFUJI Hideaki switch (dev->type) { 2190073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ETHER: 2191073a8e0eSYOSHIFUJI Hideaki case ARPHRD_FDDI: 2192073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_eui48(eui, dev); 2193073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ARCNET: 2194073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_arcnet(eui, dev); 2195073a8e0eSYOSHIFUJI Hideaki case ARPHRD_INFINIBAND: 2196073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_infiniband(eui, dev); 2197c7dc89c0SFred L. Templin case ARPHRD_SIT: 2198dfd982baSYOSHIFUJI Hideaki return addrconf_ifid_sit(eui, dev); 2199aee80b54Sstephen hemminger case ARPHRD_IPGRE: 220045ce0fd1SFelix Jia case ARPHRD_TUNNEL: 2201aee80b54Sstephen hemminger return addrconf_ifid_gre(eui, dev); 2202e74bccb8SJukka Rissanen case ARPHRD_6LOWPAN: 22038a7a4b47SAlexander Aring return addrconf_ifid_6lowpan(eui, dev); 2204cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 case ARPHRD_IEEE1394: 2205cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return addrconf_ifid_ieee1394(eui, dev); 2206e837735eSNicolas Dichtel case ARPHRD_TUNNEL6: 220745ce0fd1SFelix Jia case ARPHRD_IP6GRE: 2208e837735eSNicolas Dichtel return addrconf_ifid_ip6tnl(eui, dev); 2209073a8e0eSYOSHIFUJI Hideaki } 22101da177e4SLinus Torvalds return -1; 22111da177e4SLinus Torvalds } 22121da177e4SLinus Torvalds 22131da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev) 22141da177e4SLinus Torvalds { 22151da177e4SLinus Torvalds int err = -1; 22161da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 22171da177e4SLinus Torvalds 22181da177e4SLinus Torvalds read_lock_bh(&idev->lock); 2219602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) { 2220602582caSHannes Frederic Sowa if (ifp->scope > IFA_LINK) 2221602582caSHannes Frederic Sowa break; 22221da177e4SLinus Torvalds if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) { 22231da177e4SLinus Torvalds memcpy(eui, ifp->addr.s6_addr+8, 8); 22241da177e4SLinus Torvalds err = 0; 22251da177e4SLinus Torvalds break; 22261da177e4SLinus Torvalds } 22271da177e4SLinus Torvalds } 22281da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 22291da177e4SLinus Torvalds return err; 22301da177e4SLinus Torvalds } 22311da177e4SLinus Torvalds 22321da177e4SLinus Torvalds /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */ 22339d6280daSJiri Bohac static void ipv6_regen_rndid(struct inet6_dev *idev) 22341da177e4SLinus Torvalds { 22351da177e4SLinus Torvalds regen: 2236955189efSYOSHIFUJI Hideaki get_random_bytes(idev->rndid, sizeof(idev->rndid)); 22371da177e4SLinus Torvalds idev->rndid[0] &= ~0x02; 22381da177e4SLinus Torvalds 22391da177e4SLinus Torvalds /* 22401da177e4SLinus Torvalds * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>: 22411da177e4SLinus Torvalds * check if generated address is not inappropriate 22421da177e4SLinus Torvalds * 22431da177e4SLinus Torvalds * - Reserved subnet anycast (RFC 2526) 22441da177e4SLinus Torvalds * 11111101 11....11 1xxxxxxx 2245c7dc89c0SFred L. Templin * - ISATAP (RFC4214) 6.1 22461da177e4SLinus Torvalds * 00-00-5E-FE-xx-xx-xx-xx 22471da177e4SLinus Torvalds * - value 0 22481da177e4SLinus Torvalds * - XXX: already assigned to an address on the device 22491da177e4SLinus Torvalds */ 22501da177e4SLinus Torvalds if (idev->rndid[0] == 0xfd && 22511da177e4SLinus Torvalds (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff && 22521da177e4SLinus Torvalds (idev->rndid[7]&0x80)) 22531da177e4SLinus Torvalds goto regen; 22541da177e4SLinus Torvalds if ((idev->rndid[0]|idev->rndid[1]) == 0) { 22551da177e4SLinus Torvalds if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe) 22561da177e4SLinus Torvalds goto regen; 22571da177e4SLinus Torvalds if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00) 22581da177e4SLinus Torvalds goto regen; 22591da177e4SLinus Torvalds } 22601da177e4SLinus Torvalds } 22611da177e4SLinus Torvalds 22629d6280daSJiri Bohac static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) 22638e5e8f30SEldad Zack { 22641da177e4SLinus Torvalds if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0) 22659d6280daSJiri Bohac ipv6_regen_rndid(idev); 22661da177e4SLinus Torvalds } 22671da177e4SLinus Torvalds 22681da177e4SLinus Torvalds /* 22691da177e4SLinus Torvalds * Add prefix route. 22701da177e4SLinus Torvalds */ 22711da177e4SLinus Torvalds 22721da177e4SLinus Torvalds static void 22731da177e4SLinus Torvalds addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev, 2274e431b8c0SJamal Hadi Salim unsigned long expires, u32 flags) 22751da177e4SLinus Torvalds { 227686872cb5SThomas Graf struct fib6_config cfg = { 2277ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX, 227886872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 227986872cb5SThomas Graf .fc_ifindex = dev->ifindex, 228086872cb5SThomas Graf .fc_expires = expires, 228186872cb5SThomas Graf .fc_dst_len = plen, 228286872cb5SThomas Graf .fc_flags = RTF_UP | flags, 2283c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 2284f410a1fbSStephen Hemminger .fc_protocol = RTPROT_KERNEL, 228586872cb5SThomas Graf }; 22861da177e4SLinus Torvalds 22874e3fd7a0SAlexey Dobriyan cfg.fc_dst = *pfx; 22881da177e4SLinus Torvalds 22891da177e4SLinus Torvalds /* Prevent useless cloning on PtP SIT. 22901da177e4SLinus Torvalds This thing is done here expecting that the whole 22911da177e4SLinus Torvalds class of non-broadcast devices need not cloning. 22921da177e4SLinus Torvalds */ 229307a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 22941da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT)) 229586872cb5SThomas Graf cfg.fc_flags |= RTF_NONEXTHOP; 22960be669bbSJoerg Roedel #endif 22971da177e4SLinus Torvalds 2298333c4301SDavid Ahern ip6_route_add(&cfg, NULL); 22991da177e4SLinus Torvalds } 23001da177e4SLinus Torvalds 230114ef37b6SAndreas Hofmeister 230214ef37b6SAndreas Hofmeister static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx, 230314ef37b6SAndreas Hofmeister int plen, 230414ef37b6SAndreas Hofmeister const struct net_device *dev, 230514ef37b6SAndreas Hofmeister u32 flags, u32 noflags) 230614ef37b6SAndreas Hofmeister { 230714ef37b6SAndreas Hofmeister struct fib6_node *fn; 230814ef37b6SAndreas Hofmeister struct rt6_info *rt = NULL; 230914ef37b6SAndreas Hofmeister struct fib6_table *table; 2310ca254490SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX; 231114ef37b6SAndreas Hofmeister 2312ca254490SDavid Ahern table = fib6_get_table(dev_net(dev), tb_id); 231363159f29SIan Morris if (!table) 231414ef37b6SAndreas Hofmeister return NULL; 231514ef37b6SAndreas Hofmeister 23165744dd9bSLi RongQing read_lock_bh(&table->tb6_lock); 231714ef37b6SAndreas Hofmeister fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0); 231814ef37b6SAndreas Hofmeister if (!fn) 231914ef37b6SAndreas Hofmeister goto out; 23201f56a01fSMartin KaFai Lau 23211f56a01fSMartin KaFai Lau noflags |= RTF_CACHE; 232214ef37b6SAndreas Hofmeister for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 2323d1918542SDavid S. Miller if (rt->dst.dev->ifindex != dev->ifindex) 232414ef37b6SAndreas Hofmeister continue; 232514ef37b6SAndreas Hofmeister if ((rt->rt6i_flags & flags) != flags) 232614ef37b6SAndreas Hofmeister continue; 232785da53bfSRomain Kuntz if ((rt->rt6i_flags & noflags) != 0) 232814ef37b6SAndreas Hofmeister continue; 232914ef37b6SAndreas Hofmeister dst_hold(&rt->dst); 233014ef37b6SAndreas Hofmeister break; 233114ef37b6SAndreas Hofmeister } 233214ef37b6SAndreas Hofmeister out: 23335744dd9bSLi RongQing read_unlock_bh(&table->tb6_lock); 233414ef37b6SAndreas Hofmeister return rt; 233514ef37b6SAndreas Hofmeister } 233614ef37b6SAndreas Hofmeister 233714ef37b6SAndreas Hofmeister 23381da177e4SLinus Torvalds /* Create "default" multicast route to the interface */ 23391da177e4SLinus Torvalds 23401da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev) 23411da177e4SLinus Torvalds { 234286872cb5SThomas Graf struct fib6_config cfg = { 2343ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL, 234486872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 234586872cb5SThomas Graf .fc_ifindex = dev->ifindex, 234686872cb5SThomas Graf .fc_dst_len = 8, 234786872cb5SThomas Graf .fc_flags = RTF_UP, 2348c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 234986872cb5SThomas Graf }; 23501da177e4SLinus Torvalds 235186872cb5SThomas Graf ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0); 235286872cb5SThomas Graf 2353333c4301SDavid Ahern ip6_route_add(&cfg, NULL); 23541da177e4SLinus Torvalds } 23551da177e4SLinus Torvalds 23561da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev) 23571da177e4SLinus Torvalds { 23581da177e4SLinus Torvalds struct inet6_dev *idev; 23591da177e4SLinus Torvalds 23601da177e4SLinus Torvalds ASSERT_RTNL(); 23611da177e4SLinus Torvalds 2362e21e8467SStephen Hemminger idev = ipv6_find_idev(dev); 2363e21e8467SStephen Hemminger if (!idev) 236464e724f6SBrian Haley return ERR_PTR(-ENOBUFS); 236564e724f6SBrian Haley 236664e724f6SBrian Haley if (idev->cnf.disable_ipv6) 236764e724f6SBrian Haley return ERR_PTR(-EACCES); 23681da177e4SLinus Torvalds 23691da177e4SLinus Torvalds /* Add default multicast route */ 2370ba46ee4cSDavid Ahern if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev)) 23711da177e4SLinus Torvalds addrconf_add_mroute(dev); 23721da177e4SLinus Torvalds 23731da177e4SLinus Torvalds return idev; 23741da177e4SLinus Torvalds } 23751da177e4SLinus Torvalds 237653bd6749SJiri Pirko static void manage_tempaddrs(struct inet6_dev *idev, 237753bd6749SJiri Pirko struct inet6_ifaddr *ifp, 237853bd6749SJiri Pirko __u32 valid_lft, __u32 prefered_lft, 237953bd6749SJiri Pirko bool create, unsigned long now) 238053bd6749SJiri Pirko { 238153bd6749SJiri Pirko u32 flags; 238253bd6749SJiri Pirko struct inet6_ifaddr *ift; 238353bd6749SJiri Pirko 238453bd6749SJiri Pirko read_lock_bh(&idev->lock); 238553bd6749SJiri Pirko /* update all temporary addresses in the list */ 238653bd6749SJiri Pirko list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) { 238753bd6749SJiri Pirko int age, max_valid, max_prefered; 238853bd6749SJiri Pirko 238953bd6749SJiri Pirko if (ifp != ift->ifpub) 239053bd6749SJiri Pirko continue; 239153bd6749SJiri Pirko 239253bd6749SJiri Pirko /* RFC 4941 section 3.3: 239353bd6749SJiri Pirko * If a received option will extend the lifetime of a public 239453bd6749SJiri Pirko * address, the lifetimes of temporary addresses should 239553bd6749SJiri Pirko * be extended, subject to the overall constraint that no 239653bd6749SJiri Pirko * temporary addresses should ever remain "valid" or "preferred" 239753bd6749SJiri Pirko * for a time longer than (TEMP_VALID_LIFETIME) or 239853bd6749SJiri Pirko * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively. 239953bd6749SJiri Pirko */ 240053bd6749SJiri Pirko age = (now - ift->cstamp) / HZ; 240153bd6749SJiri Pirko max_valid = idev->cnf.temp_valid_lft - age; 240253bd6749SJiri Pirko if (max_valid < 0) 240353bd6749SJiri Pirko max_valid = 0; 240453bd6749SJiri Pirko 240553bd6749SJiri Pirko max_prefered = idev->cnf.temp_prefered_lft - 240676506a98SJiri Bohac idev->desync_factor - age; 240753bd6749SJiri Pirko if (max_prefered < 0) 240853bd6749SJiri Pirko max_prefered = 0; 240953bd6749SJiri Pirko 241053bd6749SJiri Pirko if (valid_lft > max_valid) 241153bd6749SJiri Pirko valid_lft = max_valid; 241253bd6749SJiri Pirko 241353bd6749SJiri Pirko if (prefered_lft > max_prefered) 241453bd6749SJiri Pirko prefered_lft = max_prefered; 241553bd6749SJiri Pirko 241653bd6749SJiri Pirko spin_lock(&ift->lock); 241753bd6749SJiri Pirko flags = ift->flags; 241853bd6749SJiri Pirko ift->valid_lft = valid_lft; 241953bd6749SJiri Pirko ift->prefered_lft = prefered_lft; 242053bd6749SJiri Pirko ift->tstamp = now; 242153bd6749SJiri Pirko if (prefered_lft > 0) 242253bd6749SJiri Pirko ift->flags &= ~IFA_F_DEPRECATED; 242353bd6749SJiri Pirko 242453bd6749SJiri Pirko spin_unlock(&ift->lock); 242553bd6749SJiri Pirko if (!(flags&IFA_F_TENTATIVE)) 242653bd6749SJiri Pirko ipv6_ifa_notify(0, ift); 242753bd6749SJiri Pirko } 242853bd6749SJiri Pirko 242953bd6749SJiri Pirko if ((create || list_empty(&idev->tempaddr_list)) && 243053bd6749SJiri Pirko idev->cnf.use_tempaddr > 0) { 243153bd6749SJiri Pirko /* When a new public address is created as described 243253bd6749SJiri Pirko * in [ADDRCONF], also create a new temporary address. 243353bd6749SJiri Pirko * Also create a temporary address if it's enabled but 243453bd6749SJiri Pirko * no temporary address currently exists. 243553bd6749SJiri Pirko */ 243653bd6749SJiri Pirko read_unlock_bh(&idev->lock); 243753bd6749SJiri Pirko ipv6_create_tempaddr(ifp, NULL); 243853bd6749SJiri Pirko } else { 243953bd6749SJiri Pirko read_unlock_bh(&idev->lock); 244053bd6749SJiri Pirko } 244153bd6749SJiri Pirko } 244253bd6749SJiri Pirko 2443cc9da6ccSBjørn Mork static bool is_addr_mode_generate_stable(struct inet6_dev *idev) 2444cc9da6ccSBjørn Mork { 2445d35a00b8SFelix Jia return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY || 2446d35a00b8SFelix Jia idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM; 2447cc9da6ccSBjørn Mork } 2448cc9da6ccSBjørn Mork 2449cc84b3c6SAlexander Aring int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev, 24504f672235SAlexander Aring const struct prefix_info *pinfo, 24514f672235SAlexander Aring struct inet6_dev *in6_dev, 2452cc84b3c6SAlexander Aring const struct in6_addr *addr, int addr_type, 2453cc84b3c6SAlexander Aring u32 addr_flags, bool sllao, bool tokenized, 24544f672235SAlexander Aring __u32 valid_lft, u32 prefered_lft) 24554f672235SAlexander Aring { 24564f672235SAlexander Aring struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1); 24574f672235SAlexander Aring int create = 0, update_lft = 0; 24584f672235SAlexander Aring 24594f672235SAlexander Aring if (!ifp && valid_lft) { 24604f672235SAlexander Aring int max_addresses = in6_dev->cnf.max_addresses; 24614f672235SAlexander Aring 24624f672235SAlexander Aring #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 24634f672235SAlexander Aring if (in6_dev->cnf.optimistic_dad && 24644f672235SAlexander Aring !net->ipv6.devconf_all->forwarding && sllao) 24654f672235SAlexander Aring addr_flags |= IFA_F_OPTIMISTIC; 24664f672235SAlexander Aring #endif 24674f672235SAlexander Aring 24684f672235SAlexander Aring /* Do not allow to create too much of autoconfigured 24694f672235SAlexander Aring * addresses; this would be too easy way to crash kernel. 24704f672235SAlexander Aring */ 24714f672235SAlexander Aring if (!max_addresses || 24724f672235SAlexander Aring ipv6_count_addresses(in6_dev) < max_addresses) 24734f672235SAlexander Aring ifp = ipv6_add_addr(in6_dev, addr, NULL, 24744f672235SAlexander Aring pinfo->prefix_len, 24754f672235SAlexander Aring addr_type&IPV6_ADDR_SCOPE_MASK, 24764f672235SAlexander Aring addr_flags, valid_lft, 24774f672235SAlexander Aring prefered_lft); 24784f672235SAlexander Aring 24794f672235SAlexander Aring if (IS_ERR_OR_NULL(ifp)) 24804f672235SAlexander Aring return -1; 24814f672235SAlexander Aring 24824f672235SAlexander Aring update_lft = 0; 24834f672235SAlexander Aring create = 1; 24844f672235SAlexander Aring spin_lock_bh(&ifp->lock); 24854f672235SAlexander Aring ifp->flags |= IFA_F_MANAGETEMPADDR; 24864f672235SAlexander Aring ifp->cstamp = jiffies; 24874f672235SAlexander Aring ifp->tokenized = tokenized; 24884f672235SAlexander Aring spin_unlock_bh(&ifp->lock); 24894f672235SAlexander Aring addrconf_dad_start(ifp); 24904f672235SAlexander Aring } 24914f672235SAlexander Aring 24924f672235SAlexander Aring if (ifp) { 24934f672235SAlexander Aring u32 flags; 24944f672235SAlexander Aring unsigned long now; 24954f672235SAlexander Aring u32 stored_lft; 24964f672235SAlexander Aring 24974f672235SAlexander Aring /* update lifetime (RFC2462 5.5.3 e) */ 24984f672235SAlexander Aring spin_lock_bh(&ifp->lock); 24994f672235SAlexander Aring now = jiffies; 25004f672235SAlexander Aring if (ifp->valid_lft > (now - ifp->tstamp) / HZ) 25014f672235SAlexander Aring stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ; 25024f672235SAlexander Aring else 25034f672235SAlexander Aring stored_lft = 0; 25044f672235SAlexander Aring if (!update_lft && !create && stored_lft) { 25054f672235SAlexander Aring const u32 minimum_lft = min_t(u32, 25064f672235SAlexander Aring stored_lft, MIN_VALID_LIFETIME); 25074f672235SAlexander Aring valid_lft = max(valid_lft, minimum_lft); 25084f672235SAlexander Aring 25094f672235SAlexander Aring /* RFC4862 Section 5.5.3e: 25104f672235SAlexander Aring * "Note that the preferred lifetime of the 25114f672235SAlexander Aring * corresponding address is always reset to 25124f672235SAlexander Aring * the Preferred Lifetime in the received 25134f672235SAlexander Aring * Prefix Information option, regardless of 25144f672235SAlexander Aring * whether the valid lifetime is also reset or 25154f672235SAlexander Aring * ignored." 25164f672235SAlexander Aring * 25174f672235SAlexander Aring * So we should always update prefered_lft here. 25184f672235SAlexander Aring */ 25194f672235SAlexander Aring update_lft = 1; 25204f672235SAlexander Aring } 25214f672235SAlexander Aring 25224f672235SAlexander Aring if (update_lft) { 25234f672235SAlexander Aring ifp->valid_lft = valid_lft; 25244f672235SAlexander Aring ifp->prefered_lft = prefered_lft; 25254f672235SAlexander Aring ifp->tstamp = now; 25264f672235SAlexander Aring flags = ifp->flags; 25274f672235SAlexander Aring ifp->flags &= ~IFA_F_DEPRECATED; 25284f672235SAlexander Aring spin_unlock_bh(&ifp->lock); 25294f672235SAlexander Aring 25304f672235SAlexander Aring if (!(flags&IFA_F_TENTATIVE)) 25314f672235SAlexander Aring ipv6_ifa_notify(0, ifp); 25324f672235SAlexander Aring } else 25334f672235SAlexander Aring spin_unlock_bh(&ifp->lock); 25344f672235SAlexander Aring 25354f672235SAlexander Aring manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft, 25364f672235SAlexander Aring create, now); 25374f672235SAlexander Aring 25384f672235SAlexander Aring in6_ifa_put(ifp); 25394f672235SAlexander Aring addrconf_verify(); 25404f672235SAlexander Aring } 25414f672235SAlexander Aring 25424f672235SAlexander Aring return 0; 25434f672235SAlexander Aring } 2544cc84b3c6SAlexander Aring EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr); 25454f672235SAlexander Aring 2546e6bff995SNeil Horman void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao) 25471da177e4SLinus Torvalds { 25481da177e4SLinus Torvalds struct prefix_info *pinfo; 25491da177e4SLinus Torvalds __u32 valid_lft; 25501da177e4SLinus Torvalds __u32 prefered_lft; 25514f672235SAlexander Aring int addr_type, err; 255264236f3fSHannes Frederic Sowa u32 addr_flags = 0; 25531da177e4SLinus Torvalds struct inet6_dev *in6_dev; 255456d417b1SBrian Haley struct net *net = dev_net(dev); 25551da177e4SLinus Torvalds 25561da177e4SLinus Torvalds pinfo = (struct prefix_info *) opt; 25571da177e4SLinus Torvalds 25581da177e4SLinus Torvalds if (len < sizeof(struct prefix_info)) { 2559ba3542e1Sdingtianhong ADBG("addrconf: prefix option too short\n"); 25601da177e4SLinus Torvalds return; 25611da177e4SLinus Torvalds } 25621da177e4SLinus Torvalds 25631da177e4SLinus Torvalds /* 25641da177e4SLinus Torvalds * Validation checks ([ADDRCONF], page 19) 25651da177e4SLinus Torvalds */ 25661da177e4SLinus Torvalds 25671da177e4SLinus Torvalds addr_type = ipv6_addr_type(&pinfo->prefix); 25681da177e4SLinus Torvalds 25691da177e4SLinus Torvalds if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)) 25701da177e4SLinus Torvalds return; 25711da177e4SLinus Torvalds 25721da177e4SLinus Torvalds valid_lft = ntohl(pinfo->valid); 25731da177e4SLinus Torvalds prefered_lft = ntohl(pinfo->prefered); 25741da177e4SLinus Torvalds 25751da177e4SLinus Torvalds if (prefered_lft > valid_lft) { 2576e87cc472SJoe Perches net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n"); 25771da177e4SLinus Torvalds return; 25781da177e4SLinus Torvalds } 25791da177e4SLinus Torvalds 25801da177e4SLinus Torvalds in6_dev = in6_dev_get(dev); 25811da177e4SLinus Torvalds 258263159f29SIan Morris if (!in6_dev) { 2583e87cc472SJoe Perches net_dbg_ratelimited("addrconf: device %s not configured\n", 2584e87cc472SJoe Perches dev->name); 25851da177e4SLinus Torvalds return; 25861da177e4SLinus Torvalds } 25871da177e4SLinus Torvalds 25881da177e4SLinus Torvalds /* 25891da177e4SLinus Torvalds * Two things going on here: 25901da177e4SLinus Torvalds * 1) Add routes for on-link prefixes 25911da177e4SLinus Torvalds * 2) Configure prefixes with the auto flag set 25921da177e4SLinus Torvalds */ 25931da177e4SLinus Torvalds 25944bed72e4SYOSHIFUJI Hideaki if (pinfo->onlink) { 25954bed72e4SYOSHIFUJI Hideaki struct rt6_info *rt; 25964bed72e4SYOSHIFUJI Hideaki unsigned long rt_expires; 25974bed72e4SYOSHIFUJI Hideaki 25981da177e4SLinus Torvalds /* Avoid arithmetic overflow. Really, we could 25996f704992SYOSHIFUJI Hideaki * save rt_expires in seconds, likely valid_lft, 26006f704992SYOSHIFUJI Hideaki * but it would require division in fib gc, that it 26016f704992SYOSHIFUJI Hideaki * not good. 26021da177e4SLinus Torvalds */ 26034bed72e4SYOSHIFUJI Hideaki if (HZ > USER_HZ) 26044bed72e4SYOSHIFUJI Hideaki rt_expires = addrconf_timeout_fixup(valid_lft, HZ); 26054bed72e4SYOSHIFUJI Hideaki else 26064bed72e4SYOSHIFUJI Hideaki rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ); 26073dd4bc68SYOSHIFUJI Hideaki 26084bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(rt_expires)) 26094bed72e4SYOSHIFUJI Hideaki rt_expires *= HZ; 26101da177e4SLinus Torvalds 261114ef37b6SAndreas Hofmeister rt = addrconf_get_prefix_route(&pinfo->prefix, 261214ef37b6SAndreas Hofmeister pinfo->prefix_len, 261314ef37b6SAndreas Hofmeister dev, 261414ef37b6SAndreas Hofmeister RTF_ADDRCONF | RTF_PREFIX_RT, 261514ef37b6SAndreas Hofmeister RTF_GATEWAY | RTF_DEFAULT); 26161da177e4SLinus Torvalds 261714ef37b6SAndreas Hofmeister if (rt) { 26186f704992SYOSHIFUJI Hideaki /* Autoconf prefix route */ 26191da177e4SLinus Torvalds if (valid_lft == 0) { 2620e0a1ad73SThomas Graf ip6_del_rt(rt); 26211da177e4SLinus Torvalds rt = NULL; 26224bed72e4SYOSHIFUJI Hideaki } else if (addrconf_finite_timeout(rt_expires)) { 26236f704992SYOSHIFUJI Hideaki /* not infinity */ 26241716a961SGao feng rt6_set_expires(rt, jiffies + rt_expires); 26256f704992SYOSHIFUJI Hideaki } else { 26261716a961SGao feng rt6_clean_expires(rt); 26271da177e4SLinus Torvalds } 26281da177e4SLinus Torvalds } else if (valid_lft) { 26296f704992SYOSHIFUJI Hideaki clock_t expires = 0; 26304bed72e4SYOSHIFUJI Hideaki int flags = RTF_ADDRCONF | RTF_PREFIX_RT; 26314bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(rt_expires)) { 26326f704992SYOSHIFUJI Hideaki /* not infinity */ 26336f704992SYOSHIFUJI Hideaki flags |= RTF_EXPIRES; 26346f704992SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(rt_expires); 26356f704992SYOSHIFUJI Hideaki } 26361da177e4SLinus Torvalds addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len, 26376f704992SYOSHIFUJI Hideaki dev, expires, flags); 26381da177e4SLinus Torvalds } 263994e187c0SAmerigo Wang ip6_rt_put(rt); 26401da177e4SLinus Torvalds } 26411da177e4SLinus Torvalds 26421da177e4SLinus Torvalds /* Try to figure out our local address for this prefix */ 26431da177e4SLinus Torvalds 26441da177e4SLinus Torvalds if (pinfo->autoconf && in6_dev->cnf.autoconf) { 26451da177e4SLinus Torvalds struct in6_addr addr; 2646f997c55cSAlexander Aring bool tokenized = false, dev_addr_generated = false; 26471da177e4SLinus Torvalds 26481da177e4SLinus Torvalds if (pinfo->prefix_len == 64) { 26491da177e4SLinus Torvalds memcpy(&addr, &pinfo->prefix, 8); 2650f53adae4SDaniel Borkmann 2651f53adae4SDaniel Borkmann if (!ipv6_addr_any(&in6_dev->token)) { 2652f53adae4SDaniel Borkmann read_lock_bh(&in6_dev->lock); 2653f53adae4SDaniel Borkmann memcpy(addr.s6_addr + 8, 2654f53adae4SDaniel Borkmann in6_dev->token.s6_addr + 8, 8); 2655f53adae4SDaniel Borkmann read_unlock_bh(&in6_dev->lock); 2656617fe29dSDaniel Borkmann tokenized = true; 2657cc9da6ccSBjørn Mork } else if (is_addr_mode_generate_stable(in6_dev) && 2658622c81d5SHannes Frederic Sowa !ipv6_generate_stable_address(&addr, 0, 2659622c81d5SHannes Frederic Sowa in6_dev)) { 266064236f3fSHannes Frederic Sowa addr_flags |= IFA_F_STABLE_PRIVACY; 2661622c81d5SHannes Frederic Sowa goto ok; 2662f53adae4SDaniel Borkmann } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) && 26631da177e4SLinus Torvalds ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) { 26644f672235SAlexander Aring goto put; 2665f997c55cSAlexander Aring } else { 2666f997c55cSAlexander Aring dev_addr_generated = true; 26671da177e4SLinus Torvalds } 26681da177e4SLinus Torvalds goto ok; 26691da177e4SLinus Torvalds } 2670e87cc472SJoe Perches net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n", 26711da177e4SLinus Torvalds pinfo->prefix_len); 26724f672235SAlexander Aring goto put; 26731da177e4SLinus Torvalds 26741da177e4SLinus Torvalds ok: 26754f672235SAlexander Aring err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, 26764f672235SAlexander Aring &addr, addr_type, 26774f672235SAlexander Aring addr_flags, sllao, 26784f672235SAlexander Aring tokenized, valid_lft, 26798a226b2cSJiri Benc prefered_lft); 26804f672235SAlexander Aring if (err) 26814f672235SAlexander Aring goto put; 2682f997c55cSAlexander Aring 2683f997c55cSAlexander Aring /* Ignore error case here because previous prefix add addr was 2684f997c55cSAlexander Aring * successful which will be notified. 2685f997c55cSAlexander Aring */ 2686f997c55cSAlexander Aring ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr, 2687f997c55cSAlexander Aring addr_type, addr_flags, sllao, 2688f997c55cSAlexander Aring tokenized, valid_lft, 2689f997c55cSAlexander Aring prefered_lft, 2690f997c55cSAlexander Aring dev_addr_generated); 26911da177e4SLinus Torvalds } 26921da177e4SLinus Torvalds inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo); 26934f672235SAlexander Aring put: 26941da177e4SLinus Torvalds in6_dev_put(in6_dev); 26951da177e4SLinus Torvalds } 26961da177e4SLinus Torvalds 26971da177e4SLinus Torvalds /* 26981da177e4SLinus Torvalds * Set destination address. 26991da177e4SLinus Torvalds * Special case for SIT interfaces where we create a new "virtual" 27001da177e4SLinus Torvalds * device. 27011da177e4SLinus Torvalds */ 2702af284937SDaniel Lezcano int addrconf_set_dstaddr(struct net *net, void __user *arg) 27031da177e4SLinus Torvalds { 27041da177e4SLinus Torvalds struct in6_ifreq ireq; 27051da177e4SLinus Torvalds struct net_device *dev; 27061da177e4SLinus Torvalds int err = -EINVAL; 27071da177e4SLinus Torvalds 27081da177e4SLinus Torvalds rtnl_lock(); 27091da177e4SLinus Torvalds 27101da177e4SLinus Torvalds err = -EFAULT; 27111da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 27121da177e4SLinus Torvalds goto err_exit; 27131da177e4SLinus Torvalds 2714af284937SDaniel Lezcano dev = __dev_get_by_index(net, ireq.ifr6_ifindex); 27151da177e4SLinus Torvalds 27161da177e4SLinus Torvalds err = -ENODEV; 271763159f29SIan Morris if (!dev) 27181da177e4SLinus Torvalds goto err_exit; 27191da177e4SLinus Torvalds 272007a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 27211da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT) { 27225bc3eb7eSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 27231da177e4SLinus Torvalds struct ifreq ifr; 27241da177e4SLinus Torvalds struct ip_tunnel_parm p; 27251da177e4SLinus Torvalds 27261da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 27271da177e4SLinus Torvalds if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4)) 27281da177e4SLinus Torvalds goto err_exit; 27291da177e4SLinus Torvalds 27301da177e4SLinus Torvalds memset(&p, 0, sizeof(p)); 27311da177e4SLinus Torvalds p.iph.daddr = ireq.ifr6_addr.s6_addr32[3]; 27321da177e4SLinus Torvalds p.iph.saddr = 0; 27331da177e4SLinus Torvalds p.iph.version = 4; 27341da177e4SLinus Torvalds p.iph.ihl = 5; 27351da177e4SLinus Torvalds p.iph.protocol = IPPROTO_IPV6; 27361da177e4SLinus Torvalds p.iph.ttl = 64; 2737d20b3109SStephen Hemminger ifr.ifr_ifru.ifru_data = (__force void __user *)&p; 27381da177e4SLinus Torvalds 27395bc3eb7eSStephen Hemminger if (ops->ndo_do_ioctl) { 27405bc3eb7eSStephen Hemminger mm_segment_t oldfs = get_fs(); 27415bc3eb7eSStephen Hemminger 27425bc3eb7eSStephen Hemminger set_fs(KERNEL_DS); 27435bc3eb7eSStephen Hemminger err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL); 27441da177e4SLinus Torvalds set_fs(oldfs); 27455bc3eb7eSStephen Hemminger } else 27465bc3eb7eSStephen Hemminger err = -EOPNOTSUPP; 27471da177e4SLinus Torvalds 27481da177e4SLinus Torvalds if (err == 0) { 27491da177e4SLinus Torvalds err = -ENOBUFS; 2750af284937SDaniel Lezcano dev = __dev_get_by_name(net, p.name); 2751af284937SDaniel Lezcano if (!dev) 27521da177e4SLinus Torvalds goto err_exit; 27531da177e4SLinus Torvalds err = dev_open(dev); 27541da177e4SLinus Torvalds } 27551da177e4SLinus Torvalds } 27560be669bbSJoerg Roedel #endif 27571da177e4SLinus Torvalds 27581da177e4SLinus Torvalds err_exit: 27591da177e4SLinus Torvalds rtnl_unlock(); 27601da177e4SLinus Torvalds return err; 27611da177e4SLinus Torvalds } 27621da177e4SLinus Torvalds 276393a714d6SMadhu Challa static int ipv6_mc_config(struct sock *sk, bool join, 276493a714d6SMadhu Challa const struct in6_addr *addr, int ifindex) 276593a714d6SMadhu Challa { 276693a714d6SMadhu Challa int ret; 276793a714d6SMadhu Challa 276893a714d6SMadhu Challa ASSERT_RTNL(); 276993a714d6SMadhu Challa 277093a714d6SMadhu Challa lock_sock(sk); 277193a714d6SMadhu Challa if (join) 277254ff9ef3SMarcelo Ricardo Leitner ret = ipv6_sock_mc_join(sk, ifindex, addr); 277393a714d6SMadhu Challa else 277454ff9ef3SMarcelo Ricardo Leitner ret = ipv6_sock_mc_drop(sk, ifindex, addr); 277593a714d6SMadhu Challa release_sock(sk); 277693a714d6SMadhu Challa 277793a714d6SMadhu Challa return ret; 277893a714d6SMadhu Challa } 277993a714d6SMadhu Challa 27801da177e4SLinus Torvalds /* 27811da177e4SLinus Torvalds * Manual configuration of address on an interface 27821da177e4SLinus Torvalds */ 2783479840ffSJiri Pirko static int inet6_addr_add(struct net *net, int ifindex, 2784479840ffSJiri Pirko const struct in6_addr *pfx, 2785caeaba79SNicolas Dichtel const struct in6_addr *peer_pfx, 2786479840ffSJiri Pirko unsigned int plen, __u32 ifa_flags, 2787479840ffSJiri Pirko __u32 prefered_lft, __u32 valid_lft) 27881da177e4SLinus Torvalds { 27891da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 27901da177e4SLinus Torvalds struct inet6_dev *idev; 27911da177e4SLinus Torvalds struct net_device *dev; 279293a714d6SMadhu Challa unsigned long timeout; 279393a714d6SMadhu Challa clock_t expires; 27941da177e4SLinus Torvalds int scope; 27956f704992SYOSHIFUJI Hideaki u32 flags; 27961da177e4SLinus Torvalds 27971da177e4SLinus Torvalds ASSERT_RTNL(); 27981da177e4SLinus Torvalds 279924ef0da7SThomas Graf if (plen > 128) 280024ef0da7SThomas Graf return -EINVAL; 280124ef0da7SThomas Graf 28020778769dSNoriaki TAKAMIYA /* check the lifetime */ 28030778769dSNoriaki TAKAMIYA if (!valid_lft || prefered_lft > valid_lft) 28040778769dSNoriaki TAKAMIYA return -EINVAL; 28050778769dSNoriaki TAKAMIYA 280653bd6749SJiri Pirko if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64) 280753bd6749SJiri Pirko return -EINVAL; 280853bd6749SJiri Pirko 2809af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifindex); 2810af284937SDaniel Lezcano if (!dev) 28111da177e4SLinus Torvalds return -ENODEV; 28121da177e4SLinus Torvalds 281364e724f6SBrian Haley idev = addrconf_add_dev(dev); 281464e724f6SBrian Haley if (IS_ERR(idev)) 281564e724f6SBrian Haley return PTR_ERR(idev); 28161da177e4SLinus Torvalds 281793a714d6SMadhu Challa if (ifa_flags & IFA_F_MCAUTOJOIN) { 281893a714d6SMadhu Challa int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk, 281993a714d6SMadhu Challa true, pfx, ifindex); 282093a714d6SMadhu Challa 282193a714d6SMadhu Challa if (ret < 0) 282293a714d6SMadhu Challa return ret; 282393a714d6SMadhu Challa } 282493a714d6SMadhu Challa 28251da177e4SLinus Torvalds scope = ipv6_addr_scope(pfx); 28261da177e4SLinus Torvalds 28274bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(valid_lft, HZ); 28284bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 28294bed72e4SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(timeout * HZ); 28304bed72e4SYOSHIFUJI Hideaki valid_lft = timeout; 28316f704992SYOSHIFUJI Hideaki flags = RTF_EXPIRES; 28324bed72e4SYOSHIFUJI Hideaki } else { 28334bed72e4SYOSHIFUJI Hideaki expires = 0; 28344bed72e4SYOSHIFUJI Hideaki flags = 0; 28354bed72e4SYOSHIFUJI Hideaki ifa_flags |= IFA_F_PERMANENT; 28366f704992SYOSHIFUJI Hideaki } 28370778769dSNoriaki TAKAMIYA 28384bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(prefered_lft, HZ); 28394bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 28404bed72e4SYOSHIFUJI Hideaki if (timeout == 0) 28410778769dSNoriaki TAKAMIYA ifa_flags |= IFA_F_DEPRECATED; 28424bed72e4SYOSHIFUJI Hideaki prefered_lft = timeout; 28434bed72e4SYOSHIFUJI Hideaki } 28440778769dSNoriaki TAKAMIYA 28458a226b2cSJiri Benc ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags, 28468a226b2cSJiri Benc valid_lft, prefered_lft); 28470778769dSNoriaki TAKAMIYA 28481da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 2849761aac73SThomas Haller if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) { 285046d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 28516f704992SYOSHIFUJI Hideaki expires, flags); 2852761aac73SThomas Haller } 2853761aac73SThomas Haller 285495c385b4SNeil Horman /* 285595c385b4SNeil Horman * Note that section 3.1 of RFC 4429 indicates 285695c385b4SNeil Horman * that the Optimistic flag should not be set for 285795c385b4SNeil Horman * manually configured addresses 285895c385b4SNeil Horman */ 2859cf22f9a2SDavid S. Miller addrconf_dad_start(ifp); 286053bd6749SJiri Pirko if (ifa_flags & IFA_F_MANAGETEMPADDR) 286153bd6749SJiri Pirko manage_tempaddrs(idev, ifp, valid_lft, prefered_lft, 286253bd6749SJiri Pirko true, jiffies); 28631da177e4SLinus Torvalds in6_ifa_put(ifp); 2864c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 28651da177e4SLinus Torvalds return 0; 286693a714d6SMadhu Challa } else if (ifa_flags & IFA_F_MCAUTOJOIN) { 286793a714d6SMadhu Challa ipv6_mc_config(net->ipv6.mc_autojoin_sk, 286893a714d6SMadhu Challa false, pfx, ifindex); 28691da177e4SLinus Torvalds } 28701da177e4SLinus Torvalds 28711da177e4SLinus Torvalds return PTR_ERR(ifp); 28721da177e4SLinus Torvalds } 28731da177e4SLinus Torvalds 28746046d5b4SHeiner Kallweit static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags, 28756046d5b4SHeiner Kallweit const struct in6_addr *pfx, unsigned int plen) 28761da177e4SLinus Torvalds { 28771da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 28781da177e4SLinus Torvalds struct inet6_dev *idev; 28791da177e4SLinus Torvalds struct net_device *dev; 28801da177e4SLinus Torvalds 288124ef0da7SThomas Graf if (plen > 128) 288224ef0da7SThomas Graf return -EINVAL; 288324ef0da7SThomas Graf 2884af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifindex); 2885af284937SDaniel Lezcano if (!dev) 28861da177e4SLinus Torvalds return -ENODEV; 28871da177e4SLinus Torvalds 2888e5d08d71SIan Morris idev = __in6_dev_get(dev); 288963159f29SIan Morris if (!idev) 28901da177e4SLinus Torvalds return -ENXIO; 28911da177e4SLinus Torvalds 28921da177e4SLinus Torvalds read_lock_bh(&idev->lock); 2893502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 28941da177e4SLinus Torvalds if (ifp->prefix_len == plen && 28951da177e4SLinus Torvalds ipv6_addr_equal(pfx, &ifp->addr)) { 28961da177e4SLinus Torvalds in6_ifa_hold(ifp); 28971da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 28981da177e4SLinus Torvalds 28996046d5b4SHeiner Kallweit if (!(ifp->flags & IFA_F_TEMPORARY) && 29006046d5b4SHeiner Kallweit (ifa_flags & IFA_F_MANAGETEMPADDR)) 29016046d5b4SHeiner Kallweit manage_tempaddrs(idev, ifp, 0, 0, false, 29026046d5b4SHeiner Kallweit jiffies); 29031da177e4SLinus Torvalds ipv6_del_addr(ifp); 29046046d5b4SHeiner Kallweit addrconf_verify_rtnl(); 290593a714d6SMadhu Challa if (ipv6_addr_is_multicast(pfx)) { 290693a714d6SMadhu Challa ipv6_mc_config(net->ipv6.mc_autojoin_sk, 290793a714d6SMadhu Challa false, pfx, dev->ifindex); 290893a714d6SMadhu Challa } 29091da177e4SLinus Torvalds return 0; 29101da177e4SLinus Torvalds } 29111da177e4SLinus Torvalds } 29121da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 29131da177e4SLinus Torvalds return -EADDRNOTAVAIL; 29141da177e4SLinus Torvalds } 29151da177e4SLinus Torvalds 29161da177e4SLinus Torvalds 2917af284937SDaniel Lezcano int addrconf_add_ifaddr(struct net *net, void __user *arg) 29181da177e4SLinus Torvalds { 29191da177e4SLinus Torvalds struct in6_ifreq ireq; 29201da177e4SLinus Torvalds int err; 29211da177e4SLinus Torvalds 2922af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 29231da177e4SLinus Torvalds return -EPERM; 29241da177e4SLinus Torvalds 29251da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 29261da177e4SLinus Torvalds return -EFAULT; 29271da177e4SLinus Torvalds 29281da177e4SLinus Torvalds rtnl_lock(); 2929caeaba79SNicolas Dichtel err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL, 2930af284937SDaniel Lezcano ireq.ifr6_prefixlen, IFA_F_PERMANENT, 2931af284937SDaniel Lezcano INFINITY_LIFE_TIME, INFINITY_LIFE_TIME); 29321da177e4SLinus Torvalds rtnl_unlock(); 29331da177e4SLinus Torvalds return err; 29341da177e4SLinus Torvalds } 29351da177e4SLinus Torvalds 2936af284937SDaniel Lezcano int addrconf_del_ifaddr(struct net *net, void __user *arg) 29371da177e4SLinus Torvalds { 29381da177e4SLinus Torvalds struct in6_ifreq ireq; 29391da177e4SLinus Torvalds int err; 29401da177e4SLinus Torvalds 2941af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 29421da177e4SLinus Torvalds return -EPERM; 29431da177e4SLinus Torvalds 29441da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 29451da177e4SLinus Torvalds return -EFAULT; 29461da177e4SLinus Torvalds 29471da177e4SLinus Torvalds rtnl_lock(); 29486046d5b4SHeiner Kallweit err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr, 2949af284937SDaniel Lezcano ireq.ifr6_prefixlen); 29501da177e4SLinus Torvalds rtnl_unlock(); 29511da177e4SLinus Torvalds return err; 29521da177e4SLinus Torvalds } 29531da177e4SLinus Torvalds 2954b5f348e5SIlpo Järvinen static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr, 2955b5f348e5SIlpo Järvinen int plen, int scope) 2956b5f348e5SIlpo Järvinen { 2957b5f348e5SIlpo Järvinen struct inet6_ifaddr *ifp; 2958b5f348e5SIlpo Järvinen 29598a226b2cSJiri Benc ifp = ipv6_add_addr(idev, addr, NULL, plen, 296007edd741SHannes Frederic Sowa scope, IFA_F_PERMANENT, 296107edd741SHannes Frederic Sowa INFINITY_LIFE_TIME, INFINITY_LIFE_TIME); 2962b5f348e5SIlpo Järvinen if (!IS_ERR(ifp)) { 2963b5f348e5SIlpo Järvinen spin_lock_bh(&ifp->lock); 2964b5f348e5SIlpo Järvinen ifp->flags &= ~IFA_F_TENTATIVE; 2965b5f348e5SIlpo Järvinen spin_unlock_bh(&ifp->lock); 2966764d3be6SPaolo Abeni rt_genid_bump_ipv6(dev_net(idev->dev)); 2967b5f348e5SIlpo Järvinen ipv6_ifa_notify(RTM_NEWADDR, ifp); 2968b5f348e5SIlpo Järvinen in6_ifa_put(ifp); 2969b5f348e5SIlpo Järvinen } 2970b5f348e5SIlpo Järvinen } 2971b5f348e5SIlpo Järvinen 297207a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 29731da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev) 29741da177e4SLinus Torvalds { 29751da177e4SLinus Torvalds struct in6_addr addr; 29761da177e4SLinus Torvalds struct net_device *dev; 2977c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 2978929c9cf3SNicolas Dichtel int scope, plen; 2979f0e2acfaSNicolas Dichtel u32 pflags = 0; 29801da177e4SLinus Torvalds 29811da177e4SLinus Torvalds ASSERT_RTNL(); 29821da177e4SLinus Torvalds 29831da177e4SLinus Torvalds memset(&addr, 0, sizeof(struct in6_addr)); 29841da177e4SLinus Torvalds memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4); 29851da177e4SLinus Torvalds 29861da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) { 29871da177e4SLinus Torvalds addr.s6_addr32[0] = htonl(0xfe800000); 29881da177e4SLinus Torvalds scope = IFA_LINK; 2989929c9cf3SNicolas Dichtel plen = 64; 29901da177e4SLinus Torvalds } else { 29911da177e4SLinus Torvalds scope = IPV6_ADDR_COMPATv4; 2992929c9cf3SNicolas Dichtel plen = 96; 2993f0e2acfaSNicolas Dichtel pflags |= RTF_NONEXTHOP; 29941da177e4SLinus Torvalds } 29951da177e4SLinus Torvalds 29961da177e4SLinus Torvalds if (addr.s6_addr32[3]) { 2997929c9cf3SNicolas Dichtel add_addr(idev, &addr, plen, scope); 2998f0e2acfaSNicolas Dichtel addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags); 29991da177e4SLinus Torvalds return; 30001da177e4SLinus Torvalds } 30011da177e4SLinus Torvalds 30026fda7350SBenjamin Thery for_each_netdev(net, dev) { 3003e5ed6399SHerbert Xu struct in_device *in_dev = __in_dev_get_rtnl(dev); 30041da177e4SLinus Torvalds if (in_dev && (dev->flags & IFF_UP)) { 30051da177e4SLinus Torvalds struct in_ifaddr *ifa; 30061da177e4SLinus Torvalds 30071da177e4SLinus Torvalds int flag = scope; 30081da177e4SLinus Torvalds 30091da177e4SLinus Torvalds for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) { 30101da177e4SLinus Torvalds 30111da177e4SLinus Torvalds addr.s6_addr32[3] = ifa->ifa_local; 30121da177e4SLinus Torvalds 30131da177e4SLinus Torvalds if (ifa->ifa_scope == RT_SCOPE_LINK) 30141da177e4SLinus Torvalds continue; 30151da177e4SLinus Torvalds if (ifa->ifa_scope >= RT_SCOPE_HOST) { 30161da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) 30171da177e4SLinus Torvalds continue; 30181da177e4SLinus Torvalds flag |= IFA_HOST; 30191da177e4SLinus Torvalds } 30201da177e4SLinus Torvalds 3021b5f348e5SIlpo Järvinen add_addr(idev, &addr, plen, flag); 3022f0e2acfaSNicolas Dichtel addrconf_prefix_route(&addr, plen, idev->dev, 0, 3023f0e2acfaSNicolas Dichtel pflags); 30241da177e4SLinus Torvalds } 30251da177e4SLinus Torvalds } 30261da177e4SLinus Torvalds } 30271da177e4SLinus Torvalds } 30280be669bbSJoerg Roedel #endif 30291da177e4SLinus Torvalds 30301da177e4SLinus Torvalds static void init_loopback(struct net_device *dev) 30311da177e4SLinus Torvalds { 30321da177e4SLinus Torvalds struct inet6_dev *idev; 303325fb6ca4SBalakumaran Kannan struct net_device *sp_dev; 303425fb6ca4SBalakumaran Kannan struct inet6_ifaddr *sp_ifa; 303525fb6ca4SBalakumaran Kannan struct rt6_info *sp_rt; 30361da177e4SLinus Torvalds 30371da177e4SLinus Torvalds /* ::1 */ 30381da177e4SLinus Torvalds 30391da177e4SLinus Torvalds ASSERT_RTNL(); 30401da177e4SLinus Torvalds 3041e5d08d71SIan Morris idev = ipv6_find_idev(dev); 304263159f29SIan Morris if (!idev) { 304391df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 30441da177e4SLinus Torvalds return; 30451da177e4SLinus Torvalds } 30461da177e4SLinus Torvalds 3047b5f348e5SIlpo Järvinen add_addr(idev, &in6addr_loopback, 128, IFA_HOST); 304825fb6ca4SBalakumaran Kannan 304925fb6ca4SBalakumaran Kannan /* Add routes to other interface's IPv6 addresses */ 305025fb6ca4SBalakumaran Kannan for_each_netdev(dev_net(dev), sp_dev) { 305125fb6ca4SBalakumaran Kannan if (!strcmp(sp_dev->name, dev->name)) 305225fb6ca4SBalakumaran Kannan continue; 305325fb6ca4SBalakumaran Kannan 305425fb6ca4SBalakumaran Kannan idev = __in6_dev_get(sp_dev); 305525fb6ca4SBalakumaran Kannan if (!idev) 305625fb6ca4SBalakumaran Kannan continue; 305725fb6ca4SBalakumaran Kannan 305825fb6ca4SBalakumaran Kannan read_lock_bh(&idev->lock); 305925fb6ca4SBalakumaran Kannan list_for_each_entry(sp_ifa, &idev->addr_list, if_list) { 306025fb6ca4SBalakumaran Kannan 306125fb6ca4SBalakumaran Kannan if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE)) 306225fb6ca4SBalakumaran Kannan continue; 306325fb6ca4SBalakumaran Kannan 306433d99113SGao feng if (sp_ifa->rt) { 306533d99113SGao feng /* This dst has been added to garbage list when 306633d99113SGao feng * lo device down, release this obsolete dst and 306733d99113SGao feng * reallocate a new router for ifa. 306833d99113SGao feng */ 3069a220445fSNicolas Dichtel if (!atomic_read(&sp_ifa->rt->rt6i_ref)) { 307033d99113SGao feng ip6_rt_put(sp_ifa->rt); 307133d99113SGao feng sp_ifa->rt = NULL; 307233d99113SGao feng } else { 3073a881ae1fSGao feng continue; 307433d99113SGao feng } 307533d99113SGao feng } 3076a881ae1fSGao feng 307757ec0afeSFrançois-Xavier Le Bail sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, false); 307825fb6ca4SBalakumaran Kannan 307925fb6ca4SBalakumaran Kannan /* Failure cases are ignored */ 3080534c8779SGao feng if (!IS_ERR(sp_rt)) { 3081534c8779SGao feng sp_ifa->rt = sp_rt; 308225fb6ca4SBalakumaran Kannan ip6_ins_rt(sp_rt); 308325fb6ca4SBalakumaran Kannan } 3084534c8779SGao feng } 308525fb6ca4SBalakumaran Kannan read_unlock_bh(&idev->lock); 308625fb6ca4SBalakumaran Kannan } 30871da177e4SLinus Torvalds } 30881da177e4SLinus Torvalds 30892ad3ed59SAlexander Aring void addrconf_add_linklocal(struct inet6_dev *idev, 309064236f3fSHannes Frederic Sowa const struct in6_addr *addr, u32 flags) 30911da177e4SLinus Torvalds { 30921da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 309364236f3fSHannes Frederic Sowa u32 addr_flags = flags | IFA_F_PERMANENT; 30941da177e4SLinus Torvalds 309595c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 309695c385b4SNeil Horman if (idev->cnf.optimistic_dad && 3097702beb87SDavid Miller !dev_net(idev->dev)->ipv6.devconf_all->forwarding) 309895c385b4SNeil Horman addr_flags |= IFA_F_OPTIMISTIC; 309995c385b4SNeil Horman #endif 310095c385b4SNeil Horman 310107edd741SHannes Frederic Sowa ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags, 310207edd741SHannes Frederic Sowa INFINITY_LIFE_TIME, INFINITY_LIFE_TIME); 31031da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 310446d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0); 3105cf22f9a2SDavid S. Miller addrconf_dad_start(ifp); 31061da177e4SLinus Torvalds in6_ifa_put(ifp); 31071da177e4SLinus Torvalds } 31081da177e4SLinus Torvalds } 31092ad3ed59SAlexander Aring EXPORT_SYMBOL_GPL(addrconf_add_linklocal); 31101da177e4SLinus Torvalds 3111622c81d5SHannes Frederic Sowa static bool ipv6_reserved_interfaceid(struct in6_addr address) 3112622c81d5SHannes Frederic Sowa { 3113622c81d5SHannes Frederic Sowa if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0) 3114622c81d5SHannes Frederic Sowa return true; 3115622c81d5SHannes Frederic Sowa 3116622c81d5SHannes Frederic Sowa if (address.s6_addr32[2] == htonl(0x02005eff) && 3117622c81d5SHannes Frederic Sowa ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000))) 3118622c81d5SHannes Frederic Sowa return true; 3119622c81d5SHannes Frederic Sowa 3120622c81d5SHannes Frederic Sowa if (address.s6_addr32[2] == htonl(0xfdffffff) && 3121622c81d5SHannes Frederic Sowa ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80))) 3122622c81d5SHannes Frederic Sowa return true; 3123622c81d5SHannes Frederic Sowa 3124622c81d5SHannes Frederic Sowa return false; 3125622c81d5SHannes Frederic Sowa } 3126622c81d5SHannes Frederic Sowa 3127622c81d5SHannes Frederic Sowa static int ipv6_generate_stable_address(struct in6_addr *address, 3128622c81d5SHannes Frederic Sowa u8 dad_count, 3129622c81d5SHannes Frederic Sowa const struct inet6_dev *idev) 3130622c81d5SHannes Frederic Sowa { 3131622c81d5SHannes Frederic Sowa static DEFINE_SPINLOCK(lock); 3132622c81d5SHannes Frederic Sowa static __u32 digest[SHA_DIGEST_WORDS]; 3133622c81d5SHannes Frederic Sowa static __u32 workspace[SHA_WORKSPACE_WORDS]; 3134622c81d5SHannes Frederic Sowa 3135622c81d5SHannes Frederic Sowa static union { 3136622c81d5SHannes Frederic Sowa char __data[SHA_MESSAGE_BYTES]; 3137622c81d5SHannes Frederic Sowa struct { 3138622c81d5SHannes Frederic Sowa struct in6_addr secret; 3139ff40217eSHannes Frederic Sowa __be32 prefix[2]; 3140622c81d5SHannes Frederic Sowa unsigned char hwaddr[MAX_ADDR_LEN]; 3141622c81d5SHannes Frederic Sowa u8 dad_count; 3142622c81d5SHannes Frederic Sowa } __packed; 3143622c81d5SHannes Frederic Sowa } data; 3144622c81d5SHannes Frederic Sowa 3145622c81d5SHannes Frederic Sowa struct in6_addr secret; 3146622c81d5SHannes Frederic Sowa struct in6_addr temp; 3147622c81d5SHannes Frederic Sowa struct net *net = dev_net(idev->dev); 3148622c81d5SHannes Frederic Sowa 3149622c81d5SHannes Frederic Sowa BUILD_BUG_ON(sizeof(data.__data) != sizeof(data)); 3150622c81d5SHannes Frederic Sowa 3151622c81d5SHannes Frederic Sowa if (idev->cnf.stable_secret.initialized) 3152622c81d5SHannes Frederic Sowa secret = idev->cnf.stable_secret.secret; 3153622c81d5SHannes Frederic Sowa else if (net->ipv6.devconf_dflt->stable_secret.initialized) 3154622c81d5SHannes Frederic Sowa secret = net->ipv6.devconf_dflt->stable_secret.secret; 3155622c81d5SHannes Frederic Sowa else 3156622c81d5SHannes Frederic Sowa return -1; 3157622c81d5SHannes Frederic Sowa 3158622c81d5SHannes Frederic Sowa retry: 3159622c81d5SHannes Frederic Sowa spin_lock_bh(&lock); 3160622c81d5SHannes Frederic Sowa 3161622c81d5SHannes Frederic Sowa sha_init(digest); 3162622c81d5SHannes Frederic Sowa memset(&data, 0, sizeof(data)); 3163622c81d5SHannes Frederic Sowa memset(workspace, 0, sizeof(workspace)); 3164622c81d5SHannes Frederic Sowa memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len); 3165ff40217eSHannes Frederic Sowa data.prefix[0] = address->s6_addr32[0]; 3166ff40217eSHannes Frederic Sowa data.prefix[1] = address->s6_addr32[1]; 3167622c81d5SHannes Frederic Sowa data.secret = secret; 3168622c81d5SHannes Frederic Sowa data.dad_count = dad_count; 3169622c81d5SHannes Frederic Sowa 3170622c81d5SHannes Frederic Sowa sha_transform(digest, data.__data, workspace); 3171622c81d5SHannes Frederic Sowa 3172622c81d5SHannes Frederic Sowa temp = *address; 3173ff40217eSHannes Frederic Sowa temp.s6_addr32[2] = (__force __be32)digest[0]; 3174ff40217eSHannes Frederic Sowa temp.s6_addr32[3] = (__force __be32)digest[1]; 3175622c81d5SHannes Frederic Sowa 3176622c81d5SHannes Frederic Sowa spin_unlock_bh(&lock); 3177622c81d5SHannes Frederic Sowa 3178622c81d5SHannes Frederic Sowa if (ipv6_reserved_interfaceid(temp)) { 3179622c81d5SHannes Frederic Sowa dad_count++; 31801855b7c3SHannes Frederic Sowa if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries) 3181622c81d5SHannes Frederic Sowa return -1; 3182622c81d5SHannes Frederic Sowa goto retry; 3183622c81d5SHannes Frederic Sowa } 3184622c81d5SHannes Frederic Sowa 3185622c81d5SHannes Frederic Sowa *address = temp; 3186622c81d5SHannes Frederic Sowa return 0; 3187622c81d5SHannes Frederic Sowa } 3188622c81d5SHannes Frederic Sowa 3189cc9da6ccSBjørn Mork static void ipv6_gen_mode_random_init(struct inet6_dev *idev) 3190cc9da6ccSBjørn Mork { 3191cc9da6ccSBjørn Mork struct ipv6_stable_secret *s = &idev->cnf.stable_secret; 3192cc9da6ccSBjørn Mork 3193cc9da6ccSBjørn Mork if (s->initialized) 3194cc9da6ccSBjørn Mork return; 3195cc9da6ccSBjørn Mork s = &idev->cnf.stable_secret; 3196cc9da6ccSBjørn Mork get_random_bytes(&s->secret, sizeof(s->secret)); 3197cc9da6ccSBjørn Mork s->initialized = true; 3198cc9da6ccSBjørn Mork } 3199cc9da6ccSBjørn Mork 3200bc91b0f0SJiri Pirko static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route) 3201bc91b0f0SJiri Pirko { 3202bc91b0f0SJiri Pirko struct in6_addr addr; 3203bc91b0f0SJiri Pirko 3204ca254490SDavid Ahern /* no link local addresses on L3 master devices */ 3205ca254490SDavid Ahern if (netif_is_l3_master(idev->dev)) 3206ca254490SDavid Ahern return; 3207ca254490SDavid Ahern 3208bc91b0f0SJiri Pirko ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0); 3209622c81d5SHannes Frederic Sowa 3210d35a00b8SFelix Jia switch (idev->cnf.addr_gen_mode) { 3211cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_RANDOM: 3212cc9da6ccSBjørn Mork ipv6_gen_mode_random_init(idev); 3213cc9da6ccSBjørn Mork /* fallthrough */ 3214cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_STABLE_PRIVACY: 3215622c81d5SHannes Frederic Sowa if (!ipv6_generate_stable_address(&addr, 0, idev)) 321664236f3fSHannes Frederic Sowa addrconf_add_linklocal(idev, &addr, 321764236f3fSHannes Frederic Sowa IFA_F_STABLE_PRIVACY); 3218622c81d5SHannes Frederic Sowa else if (prefix_route) 3219622c81d5SHannes Frederic Sowa addrconf_prefix_route(&addr, 64, idev->dev, 0, 0); 3220cc9da6ccSBjørn Mork break; 3221cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_EUI64: 3222bc91b0f0SJiri Pirko /* addrconf_add_linklocal also adds a prefix_route and we 3223bc91b0f0SJiri Pirko * only need to care about prefix routes if ipv6_generate_eui64 3224bc91b0f0SJiri Pirko * couldn't generate one. 3225bc91b0f0SJiri Pirko */ 3226bc91b0f0SJiri Pirko if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0) 322764236f3fSHannes Frederic Sowa addrconf_add_linklocal(idev, &addr, 0); 3228bc91b0f0SJiri Pirko else if (prefix_route) 3229bc91b0f0SJiri Pirko addrconf_prefix_route(&addr, 64, idev->dev, 0, 0); 3230cc9da6ccSBjørn Mork break; 3231cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_NONE: 3232cc9da6ccSBjørn Mork default: 3233cc9da6ccSBjørn Mork /* will not add any link local address */ 3234cc9da6ccSBjørn Mork break; 3235bc91b0f0SJiri Pirko } 3236bc91b0f0SJiri Pirko } 3237bc91b0f0SJiri Pirko 32381da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev) 32391da177e4SLinus Torvalds { 32401da177e4SLinus Torvalds struct inet6_dev *idev; 32411da177e4SLinus Torvalds 32421da177e4SLinus Torvalds ASSERT_RTNL(); 32431da177e4SLinus Torvalds 324474235a25SHerbert Xu if ((dev->type != ARPHRD_ETHER) && 324574235a25SHerbert Xu (dev->type != ARPHRD_FDDI) && 324674235a25SHerbert Xu (dev->type != ARPHRD_ARCNET) && 324706a4c1c5Salex.bluesman.smirnov@gmail.com (dev->type != ARPHRD_INFINIBAND) && 3248e837735eSNicolas Dichtel (dev->type != ARPHRD_IEEE1394) && 3249e74bccb8SJukka Rissanen (dev->type != ARPHRD_TUNNEL6) && 3250cc9da6ccSBjørn Mork (dev->type != ARPHRD_6LOWPAN) && 325145ce0fd1SFelix Jia (dev->type != ARPHRD_IP6GRE) && 325245ce0fd1SFelix Jia (dev->type != ARPHRD_IPGRE) && 325345ce0fd1SFelix Jia (dev->type != ARPHRD_TUNNEL) && 3254cc9da6ccSBjørn Mork (dev->type != ARPHRD_NONE)) { 325574235a25SHerbert Xu /* Alas, we support only Ethernet autoconfiguration. */ 325674235a25SHerbert Xu return; 325774235a25SHerbert Xu } 325874235a25SHerbert Xu 32591da177e4SLinus Torvalds idev = addrconf_add_dev(dev); 326064e724f6SBrian Haley if (IS_ERR(idev)) 32611da177e4SLinus Torvalds return; 32621da177e4SLinus Torvalds 3263cc9da6ccSBjørn Mork /* this device type has no EUI support */ 3264cc9da6ccSBjørn Mork if (dev->type == ARPHRD_NONE && 3265d35a00b8SFelix Jia idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64) 3266d35a00b8SFelix Jia idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM; 3267cc9da6ccSBjørn Mork 3268bc91b0f0SJiri Pirko addrconf_addr_gen(idev, false); 32691da177e4SLinus Torvalds } 32701da177e4SLinus Torvalds 327107a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 32721da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev) 32731da177e4SLinus Torvalds { 32741da177e4SLinus Torvalds struct inet6_dev *idev; 32751da177e4SLinus Torvalds 32761da177e4SLinus Torvalds ASSERT_RTNL(); 32771da177e4SLinus Torvalds 32781da177e4SLinus Torvalds /* 32791da177e4SLinus Torvalds * Configure the tunnel with one of our IPv4 32801da177e4SLinus Torvalds * addresses... we should configure all of 32811da177e4SLinus Torvalds * our v4 addrs in the tunnel 32821da177e4SLinus Torvalds */ 32831da177e4SLinus Torvalds 3284e5d08d71SIan Morris idev = ipv6_find_idev(dev); 328563159f29SIan Morris if (!idev) { 328691df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 32871da177e4SLinus Torvalds return; 32881da177e4SLinus Torvalds } 32891da177e4SLinus Torvalds 3290c7dc89c0SFred L. Templin if (dev->priv_flags & IFF_ISATAP) { 3291bc91b0f0SJiri Pirko addrconf_addr_gen(idev, false); 3292c7dc89c0SFred L. Templin return; 3293c7dc89c0SFred L. Templin } 3294c7dc89c0SFred L. Templin 32951da177e4SLinus Torvalds sit_add_v4_addrs(idev); 32961da177e4SLinus Torvalds 329762b54dd9SNicolas Dichtel if (dev->flags&IFF_POINTOPOINT) 32981da177e4SLinus Torvalds addrconf_add_mroute(dev); 32991da177e4SLinus Torvalds } 33000be669bbSJoerg Roedel #endif 33011da177e4SLinus Torvalds 330207a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE) 3303aee80b54Sstephen hemminger static void addrconf_gre_config(struct net_device *dev) 3304aee80b54Sstephen hemminger { 3305aee80b54Sstephen hemminger struct inet6_dev *idev; 3306aee80b54Sstephen hemminger 3307aee80b54Sstephen hemminger ASSERT_RTNL(); 3308aee80b54Sstephen hemminger 3309e5d08d71SIan Morris idev = ipv6_find_idev(dev); 331063159f29SIan Morris if (!idev) { 331191df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 3312aee80b54Sstephen hemminger return; 3313aee80b54Sstephen hemminger } 3314aee80b54Sstephen hemminger 3315bc91b0f0SJiri Pirko addrconf_addr_gen(idev, true); 3316d9e4ce65SHannes Frederic Sowa if (dev->flags & IFF_POINTOPOINT) 3317d9e4ce65SHannes Frederic Sowa addrconf_add_mroute(dev); 3318aee80b54Sstephen hemminger } 3319aee80b54Sstephen hemminger #endif 3320aee80b54Sstephen hemminger 3321f1705ec1SDavid Ahern static int fixup_permanent_addr(struct inet6_dev *idev, 3322f1705ec1SDavid Ahern struct inet6_ifaddr *ifp) 3323f1705ec1SDavid Ahern { 33248048ced9SDavid Ahern /* rt6i_ref == 0 means the host route was removed from the 33258048ced9SDavid Ahern * FIB, for example, if 'lo' device is taken down. In that 33268048ced9SDavid Ahern * case regenerate the host route. 33278048ced9SDavid Ahern */ 33288048ced9SDavid Ahern if (!ifp->rt || !atomic_read(&ifp->rt->rt6i_ref)) { 33298048ced9SDavid Ahern struct rt6_info *rt, *prev; 3330f1705ec1SDavid Ahern 3331f1705ec1SDavid Ahern rt = addrconf_dst_alloc(idev, &ifp->addr, false); 3332f1705ec1SDavid Ahern if (unlikely(IS_ERR(rt))) 3333f1705ec1SDavid Ahern return PTR_ERR(rt); 3334f1705ec1SDavid Ahern 33358048ced9SDavid Ahern /* ifp->rt can be accessed outside of rtnl */ 33368048ced9SDavid Ahern spin_lock(&ifp->lock); 33378048ced9SDavid Ahern prev = ifp->rt; 3338f1705ec1SDavid Ahern ifp->rt = rt; 33398048ced9SDavid Ahern spin_unlock(&ifp->lock); 33408048ced9SDavid Ahern 33418048ced9SDavid Ahern ip6_rt_put(prev); 3342f1705ec1SDavid Ahern } 3343f1705ec1SDavid Ahern 3344f1705ec1SDavid Ahern if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) { 3345f1705ec1SDavid Ahern addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 3346f1705ec1SDavid Ahern idev->dev, 0, 0); 3347f1705ec1SDavid Ahern } 3348f1705ec1SDavid Ahern 33496d717134SDavid Ahern if (ifp->state == INET6_IFADDR_STATE_PREDAD) 3350f1705ec1SDavid Ahern addrconf_dad_start(ifp); 3351f1705ec1SDavid Ahern 3352f1705ec1SDavid Ahern return 0; 3353f1705ec1SDavid Ahern } 3354f1705ec1SDavid Ahern 3355f1705ec1SDavid Ahern static void addrconf_permanent_addr(struct net_device *dev) 3356f1705ec1SDavid Ahern { 3357f1705ec1SDavid Ahern struct inet6_ifaddr *ifp, *tmp; 3358f1705ec1SDavid Ahern struct inet6_dev *idev; 3359f1705ec1SDavid Ahern 3360f1705ec1SDavid Ahern idev = __in6_dev_get(dev); 3361f1705ec1SDavid Ahern if (!idev) 3362f1705ec1SDavid Ahern return; 3363f1705ec1SDavid Ahern 3364f1705ec1SDavid Ahern write_lock_bh(&idev->lock); 3365f1705ec1SDavid Ahern 3366f1705ec1SDavid Ahern list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) { 3367f1705ec1SDavid Ahern if ((ifp->flags & IFA_F_PERMANENT) && 3368f1705ec1SDavid Ahern fixup_permanent_addr(idev, ifp) < 0) { 3369f1705ec1SDavid Ahern write_unlock_bh(&idev->lock); 3370f1705ec1SDavid Ahern ipv6_del_addr(ifp); 3371f1705ec1SDavid Ahern write_lock_bh(&idev->lock); 3372f1705ec1SDavid Ahern 3373f1705ec1SDavid Ahern net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n", 3374f1705ec1SDavid Ahern idev->dev->name, &ifp->addr); 3375f1705ec1SDavid Ahern } 3376f1705ec1SDavid Ahern } 3377f1705ec1SDavid Ahern 3378f1705ec1SDavid Ahern write_unlock_bh(&idev->lock); 3379f1705ec1SDavid Ahern } 3380f1705ec1SDavid Ahern 33811da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event, 3382351638e7SJiri Pirko void *ptr) 33831da177e4SLinus Torvalds { 3384351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 33854f7f34eaSDavid Ahern struct netdev_notifier_changeupper_info *info; 3386748e2d93SEric Dumazet struct inet6_dev *idev = __in6_dev_get(dev); 338760abc0beSWANG Cong struct net *net = dev_net(dev); 3388c5e33bddSYOSHIFUJI Hideaki int run_pending = 0; 3389b217d616SHerbert Xu int err; 33901da177e4SLinus Torvalds 33911da177e4SLinus Torvalds switch (event) { 339245ba9dd2SYOSHIFUJI Hideaki case NETDEV_REGISTER: 339374235a25SHerbert Xu if (!idev && dev->mtu >= IPV6_MIN_MTU) { 339445ba9dd2SYOSHIFUJI Hideaki idev = ipv6_add_dev(dev); 3395a317a2f1SWANG Cong if (IS_ERR(idev)) 3396a317a2f1SWANG Cong return notifier_from_errno(PTR_ERR(idev)); 339745ba9dd2SYOSHIFUJI Hideaki } 339845ba9dd2SYOSHIFUJI Hideaki break; 3399bcdd553fSStephen Hemminger 3400b7b0b1d2SAlexander Duyck case NETDEV_CHANGEMTU: 3401b7b0b1d2SAlexander Duyck /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */ 3402b7b0b1d2SAlexander Duyck if (dev->mtu < IPV6_MIN_MTU) { 340360abc0beSWANG Cong addrconf_ifdown(dev, dev != net->loopback_dev); 3404b7b0b1d2SAlexander Duyck break; 3405b7b0b1d2SAlexander Duyck } 3406b7b0b1d2SAlexander Duyck 3407b7b0b1d2SAlexander Duyck if (idev) { 3408b7b0b1d2SAlexander Duyck rt6_mtu_change(dev, dev->mtu); 3409b7b0b1d2SAlexander Duyck idev->cnf.mtu6 = dev->mtu; 3410b7b0b1d2SAlexander Duyck break; 3411b7b0b1d2SAlexander Duyck } 3412b7b0b1d2SAlexander Duyck 3413b7b0b1d2SAlexander Duyck /* allocate new idev */ 3414b7b0b1d2SAlexander Duyck idev = ipv6_add_dev(dev); 3415b7b0b1d2SAlexander Duyck if (IS_ERR(idev)) 3416b7b0b1d2SAlexander Duyck break; 3417b7b0b1d2SAlexander Duyck 3418b7b0b1d2SAlexander Duyck /* device is still not ready */ 3419b7b0b1d2SAlexander Duyck if (!(idev->if_flags & IF_READY)) 3420b7b0b1d2SAlexander Duyck break; 3421b7b0b1d2SAlexander Duyck 3422b7b0b1d2SAlexander Duyck run_pending = 1; 3423b7b0b1d2SAlexander Duyck 3424b7b0b1d2SAlexander Duyck /* fall through */ 3425b7b0b1d2SAlexander Duyck 34261da177e4SLinus Torvalds case NETDEV_UP: 34273c21edbdSYOSHIFUJI Hideaki case NETDEV_CHANGE: 3428c2edacf8SJay Vosburgh if (dev->flags & IFF_SLAVE) 3429c2edacf8SJay Vosburgh break; 3430c2edacf8SJay Vosburgh 34313ce62a84SWANG Cong if (idev && idev->cnf.disable_ipv6) 34323ce62a84SWANG Cong break; 34333ce62a84SWANG Cong 34343c21edbdSYOSHIFUJI Hideaki if (event == NETDEV_UP) { 343538bd10c4SDavid Ahern /* restore routes for permanent addresses */ 343638bd10c4SDavid Ahern addrconf_permanent_addr(dev); 343738bd10c4SDavid Ahern 3438f24e3d65SMitsuru Chinen if (!addrconf_qdisc_ok(dev)) { 34393c21edbdSYOSHIFUJI Hideaki /* device is not ready yet. */ 3440f3213831SJoe Perches pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n", 34413c21edbdSYOSHIFUJI Hideaki dev->name); 34423c21edbdSYOSHIFUJI Hideaki break; 34433c21edbdSYOSHIFUJI Hideaki } 344499081049SKristian Slavov 3445d31c7b8fSEvgeniy Polyakov if (!idev && dev->mtu >= IPV6_MIN_MTU) 3446d31c7b8fSEvgeniy Polyakov idev = ipv6_add_dev(dev); 3447d31c7b8fSEvgeniy Polyakov 3448a317a2f1SWANG Cong if (!IS_ERR_OR_NULL(idev)) { 344999081049SKristian Slavov idev->if_flags |= IF_READY; 3450e3ec6cfcSBenjamin Thery run_pending = 1; 3451e3ec6cfcSBenjamin Thery } 3452b7b0b1d2SAlexander Duyck } else if (event == NETDEV_CHANGE) { 3453f24e3d65SMitsuru Chinen if (!addrconf_qdisc_ok(dev)) { 34543c21edbdSYOSHIFUJI Hideaki /* device is still not ready. */ 34553c21edbdSYOSHIFUJI Hideaki break; 34563c21edbdSYOSHIFUJI Hideaki } 34573c21edbdSYOSHIFUJI Hideaki 34583c21edbdSYOSHIFUJI Hideaki if (idev) { 3459a088d1d7SLinus Lüssing if (idev->if_flags & IF_READY) { 3460a088d1d7SLinus Lüssing /* device is already configured - 3461a088d1d7SLinus Lüssing * but resend MLD reports, we might 3462a088d1d7SLinus Lüssing * have roamed and need to update 3463a088d1d7SLinus Lüssing * multicast snooping switches 3464a088d1d7SLinus Lüssing */ 3465a088d1d7SLinus Lüssing ipv6_mc_up(idev); 34663c21edbdSYOSHIFUJI Hideaki break; 3467a088d1d7SLinus Lüssing } 34683c21edbdSYOSHIFUJI Hideaki idev->if_flags |= IF_READY; 34693c21edbdSYOSHIFUJI Hideaki } 34703c21edbdSYOSHIFUJI Hideaki 3471f3213831SJoe Perches pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n", 34723c21edbdSYOSHIFUJI Hideaki dev->name); 34733c21edbdSYOSHIFUJI Hideaki 3474c5e33bddSYOSHIFUJI Hideaki run_pending = 1; 34753c21edbdSYOSHIFUJI Hideaki } 34763c21edbdSYOSHIFUJI Hideaki 34771da177e4SLinus Torvalds switch (dev->type) { 347807a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 34791da177e4SLinus Torvalds case ARPHRD_SIT: 34801da177e4SLinus Torvalds addrconf_sit_config(dev); 34811da177e4SLinus Torvalds break; 34820be669bbSJoerg Roedel #endif 348307a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE) 3484aee80b54Sstephen hemminger case ARPHRD_IPGRE: 3485aee80b54Sstephen hemminger addrconf_gre_config(dev); 3486aee80b54Sstephen hemminger break; 3487aee80b54Sstephen hemminger #endif 34881da177e4SLinus Torvalds case ARPHRD_LOOPBACK: 34891da177e4SLinus Torvalds init_loopback(dev); 34901da177e4SLinus Torvalds break; 34911da177e4SLinus Torvalds 34921da177e4SLinus Torvalds default: 34931da177e4SLinus Torvalds addrconf_dev_config(dev); 34941da177e4SLinus Torvalds break; 34953ff50b79SStephen Hemminger } 3496bcdd553fSStephen Hemminger 3497a317a2f1SWANG Cong if (!IS_ERR_OR_NULL(idev)) { 3498c5e33bddSYOSHIFUJI Hideaki if (run_pending) 3499c5e33bddSYOSHIFUJI Hideaki addrconf_dad_run(idev); 3500c5e33bddSYOSHIFUJI Hideaki 3501bcdd553fSStephen Hemminger /* 3502bcdd553fSStephen Hemminger * If the MTU changed during the interface down, 3503bcdd553fSStephen Hemminger * when the interface up, the changed MTU must be 3504bcdd553fSStephen Hemminger * reflected in the idev as well as routers. 35051da177e4SLinus Torvalds */ 3506bcdd553fSStephen Hemminger if (idev->cnf.mtu6 != dev->mtu && 3507bcdd553fSStephen Hemminger dev->mtu >= IPV6_MIN_MTU) { 35081da177e4SLinus Torvalds rt6_mtu_change(dev, dev->mtu); 35091da177e4SLinus Torvalds idev->cnf.mtu6 = dev->mtu; 35101da177e4SLinus Torvalds } 35111da177e4SLinus Torvalds idev->tstamp = jiffies; 35121da177e4SLinus Torvalds inet6_ifinfo_notify(RTM_NEWLINK, idev); 3513bcdd553fSStephen Hemminger 3514bcdd553fSStephen Hemminger /* 3515bcdd553fSStephen Hemminger * If the changed mtu during down is lower than 3516bcdd553fSStephen Hemminger * IPV6_MIN_MTU stop IPv6 on this interface. 35171da177e4SLinus Torvalds */ 35181da177e4SLinus Torvalds if (dev->mtu < IPV6_MIN_MTU) 351960abc0beSWANG Cong addrconf_ifdown(dev, dev != net->loopback_dev); 35201da177e4SLinus Torvalds } 35211da177e4SLinus Torvalds break; 35221da177e4SLinus Torvalds 35231da177e4SLinus Torvalds case NETDEV_DOWN: 35241da177e4SLinus Torvalds case NETDEV_UNREGISTER: 35251da177e4SLinus Torvalds /* 35261da177e4SLinus Torvalds * Remove all addresses from this interface. 35271da177e4SLinus Torvalds */ 35281da177e4SLinus Torvalds addrconf_ifdown(dev, event != NETDEV_DOWN); 35291da177e4SLinus Torvalds break; 35303c21edbdSYOSHIFUJI Hideaki 35311da177e4SLinus Torvalds case NETDEV_CHANGENAME: 35321da177e4SLinus Torvalds if (idev) { 35335632c515SStephen Hemminger snmp6_unregister_dev(idev); 3534408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 3535a317a2f1SWANG Cong err = addrconf_sysctl_register(idev); 3536b217d616SHerbert Xu if (err) 3537b217d616SHerbert Xu return notifier_from_errno(err); 3538a317a2f1SWANG Cong err = snmp6_register_dev(idev); 3539a317a2f1SWANG Cong if (err) { 3540a317a2f1SWANG Cong addrconf_sysctl_unregister(idev); 3541a317a2f1SWANG Cong return notifier_from_errno(err); 3542a317a2f1SWANG Cong } 35435632c515SStephen Hemminger } 35441da177e4SLinus Torvalds break; 3545bcdd553fSStephen Hemminger 354693d9b7d7SJiri Pirko case NETDEV_PRE_TYPE_CHANGE: 354793d9b7d7SJiri Pirko case NETDEV_POST_TYPE_CHANGE: 35483ef0952cSAndrew Lunn if (idev) 354993d9b7d7SJiri Pirko addrconf_type_change(dev, event); 355075c78500SMoni Shoua break; 35514f7f34eaSDavid Ahern 35524f7f34eaSDavid Ahern case NETDEV_CHANGEUPPER: 35534f7f34eaSDavid Ahern info = ptr; 35544f7f34eaSDavid Ahern 35554f7f34eaSDavid Ahern /* flush all routes if dev is linked to or unlinked from 35564f7f34eaSDavid Ahern * an L3 master device (e.g., VRF) 35574f7f34eaSDavid Ahern */ 35584f7f34eaSDavid Ahern if (info->upper_dev && netif_is_l3_master(info->upper_dev)) 35594f7f34eaSDavid Ahern addrconf_ifdown(dev, 0); 35603ff50b79SStephen Hemminger } 35611da177e4SLinus Torvalds 35621da177e4SLinus Torvalds return NOTIFY_OK; 35631da177e4SLinus Torvalds } 35641da177e4SLinus Torvalds 35651da177e4SLinus Torvalds /* 35661da177e4SLinus Torvalds * addrconf module should be notified of a device going up 35671da177e4SLinus Torvalds */ 35681da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = { 35691da177e4SLinus Torvalds .notifier_call = addrconf_notify, 3570242d3a49SWANG Cong .priority = ADDRCONF_NOTIFY_PRIORITY, 35711da177e4SLinus Torvalds }; 35721da177e4SLinus Torvalds 357393d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, unsigned long event) 357475c78500SMoni Shoua { 357575c78500SMoni Shoua struct inet6_dev *idev; 357675c78500SMoni Shoua ASSERT_RTNL(); 357775c78500SMoni Shoua 357875c78500SMoni Shoua idev = __in6_dev_get(dev); 357975c78500SMoni Shoua 358093d9b7d7SJiri Pirko if (event == NETDEV_POST_TYPE_CHANGE) 358175c78500SMoni Shoua ipv6_mc_remap(idev); 358293d9b7d7SJiri Pirko else if (event == NETDEV_PRE_TYPE_CHANGE) 358375c78500SMoni Shoua ipv6_mc_unmap(idev); 358475c78500SMoni Shoua } 358575c78500SMoni Shoua 358670af921dSDavid Ahern static bool addr_is_local(const struct in6_addr *addr) 358770af921dSDavid Ahern { 358870af921dSDavid Ahern return ipv6_addr_type(addr) & 358970af921dSDavid Ahern (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK); 359070af921dSDavid Ahern } 359170af921dSDavid Ahern 35921da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how) 35931da177e4SLinus Torvalds { 3594c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 3595502a2ffdSstephen hemminger struct inet6_dev *idev; 3596f1705ec1SDavid Ahern struct inet6_ifaddr *ifa, *tmp; 3597f1705ec1SDavid Ahern struct list_head del_list; 3598f1705ec1SDavid Ahern int _keep_addr; 3599f1705ec1SDavid Ahern bool keep_addr; 360073a8bd74SDavid S. Miller int state, i; 36011da177e4SLinus Torvalds 36021da177e4SLinus Torvalds ASSERT_RTNL(); 36031da177e4SLinus Torvalds 3604f3db4851SDaniel Lezcano rt6_ifdown(net, dev); 360573a8bd74SDavid S. Miller neigh_ifdown(&nd_tbl, dev); 36061da177e4SLinus Torvalds 36071da177e4SLinus Torvalds idev = __in6_dev_get(dev); 360863159f29SIan Morris if (!idev) 36091da177e4SLinus Torvalds return -ENODEV; 36101da177e4SLinus Torvalds 3611bcdd553fSStephen Hemminger /* 3612bcdd553fSStephen Hemminger * Step 1: remove reference to ipv6 device from parent device. 3613bcdd553fSStephen Hemminger * Do not dev_put! 36141da177e4SLinus Torvalds */ 3615439e2385SDenis V. Lunev if (how) { 36161da177e4SLinus Torvalds idev->dead = 1; 36178814c4b5SYOSHIFUJI Hideaki 36188814c4b5SYOSHIFUJI Hideaki /* protected by rtnl_lock */ 3619a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(dev->ip6_ptr, NULL); 36201da177e4SLinus Torvalds 36211da177e4SLinus Torvalds /* Step 1.5: remove snmp6 entry */ 36221da177e4SLinus Torvalds snmp6_unregister_dev(idev); 36231da177e4SLinus Torvalds 36241da177e4SLinus Torvalds } 36251da177e4SLinus Torvalds 3626f1705ec1SDavid Ahern /* aggregate the system setting and interface setting */ 3627f1705ec1SDavid Ahern _keep_addr = net->ipv6.devconf_all->keep_addr_on_down; 3628f1705ec1SDavid Ahern if (!_keep_addr) 3629f1705ec1SDavid Ahern _keep_addr = idev->cnf.keep_addr_on_down; 3630f1705ec1SDavid Ahern 3631f1705ec1SDavid Ahern /* combine the user config with event to determine if permanent 3632f1705ec1SDavid Ahern * addresses are to be removed from address hash table 3633f1705ec1SDavid Ahern */ 3634bc561632SMike Manning keep_addr = !(how || _keep_addr <= 0 || idev->cnf.disable_ipv6); 3635f1705ec1SDavid Ahern 363673a8bd74SDavid S. Miller /* Step 2: clear hash table */ 363773a8bd74SDavid S. Miller for (i = 0; i < IN6_ADDR_HSIZE; i++) { 363873a8bd74SDavid S. Miller struct hlist_head *h = &inet6_addr_lst[i]; 363973a8bd74SDavid S. Miller 364073a8bd74SDavid S. Miller spin_lock_bh(&addrconf_hash_lock); 364173a8bd74SDavid S. Miller restart: 3642b67bfe0dSSasha Levin hlist_for_each_entry_rcu(ifa, h, addr_lst) { 364373a8bd74SDavid S. Miller if (ifa->idev == idev) { 3644c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifa); 3645f1705ec1SDavid Ahern /* combined flag + permanent flag decide if 3646f1705ec1SDavid Ahern * address is retained on a down event 3647f1705ec1SDavid Ahern */ 3648f1705ec1SDavid Ahern if (!keep_addr || 364970af921dSDavid Ahern !(ifa->flags & IFA_F_PERMANENT) || 365070af921dSDavid Ahern addr_is_local(&ifa->addr)) { 3651f1705ec1SDavid Ahern hlist_del_init_rcu(&ifa->addr_lst); 365273a8bd74SDavid S. Miller goto restart; 365373a8bd74SDavid S. Miller } 365473a8bd74SDavid S. Miller } 3655f1705ec1SDavid Ahern } 365673a8bd74SDavid S. Miller spin_unlock_bh(&addrconf_hash_lock); 365773a8bd74SDavid S. Miller } 365873a8bd74SDavid S. Miller 36591da177e4SLinus Torvalds write_lock_bh(&idev->lock); 36601da177e4SLinus Torvalds 3661b7b1bfceSHannes Frederic Sowa addrconf_del_rs_timer(idev); 3662b7b1bfceSHannes Frederic Sowa 3663bcdd553fSStephen Hemminger /* Step 2: clear flags for stateless addrconf */ 3664439e2385SDenis V. Lunev if (!how) 36653c21edbdSYOSHIFUJI Hideaki idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY); 36661da177e4SLinus Torvalds 3667bcdd553fSStephen Hemminger /* Step 3: clear tempaddr list */ 3668372e6c8fSstephen hemminger while (!list_empty(&idev->tempaddr_list)) { 3669372e6c8fSstephen hemminger ifa = list_first_entry(&idev->tempaddr_list, 3670372e6c8fSstephen hemminger struct inet6_ifaddr, tmp_list); 3671372e6c8fSstephen hemminger list_del(&ifa->tmp_list); 36721da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 36731da177e4SLinus Torvalds spin_lock_bh(&ifa->lock); 36741da177e4SLinus Torvalds 36751da177e4SLinus Torvalds if (ifa->ifpub) { 36761da177e4SLinus Torvalds in6_ifa_put(ifa->ifpub); 36771da177e4SLinus Torvalds ifa->ifpub = NULL; 36781da177e4SLinus Torvalds } 36791da177e4SLinus Torvalds spin_unlock_bh(&ifa->lock); 36801da177e4SLinus Torvalds in6_ifa_put(ifa); 36811da177e4SLinus Torvalds write_lock_bh(&idev->lock); 36821da177e4SLinus Torvalds } 36838f37ada5Sstephen hemminger 3684f1705ec1SDavid Ahern /* re-combine the user config with event to determine if permanent 3685f1705ec1SDavid Ahern * addresses are to be removed from the interface list 3686f1705ec1SDavid Ahern */ 3687bc561632SMike Manning keep_addr = (!how && _keep_addr > 0 && !idev->cnf.disable_ipv6); 3688f1705ec1SDavid Ahern 3689f1705ec1SDavid Ahern INIT_LIST_HEAD(&del_list); 3690f1705ec1SDavid Ahern list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) { 369138bd10c4SDavid Ahern struct rt6_info *rt = NULL; 3692a2d6cbb0SRabin Vincent bool keep; 369338bd10c4SDavid Ahern 3694c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifa); 369584e8b803Sstephen hemminger 3696a2d6cbb0SRabin Vincent keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) && 3697a2d6cbb0SRabin Vincent !addr_is_local(&ifa->addr); 3698a2d6cbb0SRabin Vincent if (!keep) 3699a2d6cbb0SRabin Vincent list_move(&ifa->if_list, &del_list); 3700a2d6cbb0SRabin Vincent 37014c5ff6a6SHerbert Xu write_unlock_bh(&idev->lock); 37028e8e676dSHannes Frederic Sowa spin_lock_bh(&ifa->lock); 3703f1705ec1SDavid Ahern 3704a2d6cbb0SRabin Vincent if (keep) { 3705f1705ec1SDavid Ahern /* set state to skip the notifier below */ 3706f1705ec1SDavid Ahern state = INET6_IFADDR_STATE_DEAD; 37076d717134SDavid Ahern ifa->state = INET6_IFADDR_STATE_PREDAD; 3708f1705ec1SDavid Ahern if (!(ifa->flags & IFA_F_NODAD)) 3709f1705ec1SDavid Ahern ifa->flags |= IFA_F_TENTATIVE; 371038bd10c4SDavid Ahern 371138bd10c4SDavid Ahern rt = ifa->rt; 371238bd10c4SDavid Ahern ifa->rt = NULL; 3713f1705ec1SDavid Ahern } else { 37144c5ff6a6SHerbert Xu state = ifa->state; 37154c5ff6a6SHerbert Xu ifa->state = INET6_IFADDR_STATE_DEAD; 3716f1705ec1SDavid Ahern } 3717f1705ec1SDavid Ahern 37188e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifa->lock); 37194c5ff6a6SHerbert Xu 372038bd10c4SDavid Ahern if (rt) 372138bd10c4SDavid Ahern ip6_del_rt(rt); 372238bd10c4SDavid Ahern 37239d82ca98SJohn Fastabend if (state != INET6_IFADDR_STATE_DEAD) { 37241da177e4SLinus Torvalds __ipv6_ifa_notify(RTM_DELADDR, ifa); 3725f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_DOWN, ifa); 3726ea06f717SMike Manning } else { 3727ea06f717SMike Manning if (idev->cnf.forwarding) 3728ea06f717SMike Manning addrconf_leave_anycast(ifa); 3729ea06f717SMike Manning addrconf_leave_solict(ifa->idev, &ifa->addr); 37302de79570SLorenzo Colitti } 373173a8bd74SDavid S. Miller 37321da177e4SLinus Torvalds write_lock_bh(&idev->lock); 37331da177e4SLinus Torvalds } 37348f37ada5Sstephen hemminger 37351da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 37361da177e4SLinus Torvalds 3737f1705ec1SDavid Ahern /* now clean up addresses to be removed */ 3738f1705ec1SDavid Ahern while (!list_empty(&del_list)) { 3739f1705ec1SDavid Ahern ifa = list_first_entry(&del_list, 3740f1705ec1SDavid Ahern struct inet6_ifaddr, if_list); 3741f1705ec1SDavid Ahern list_del(&ifa->if_list); 3742f1705ec1SDavid Ahern 3743f1705ec1SDavid Ahern in6_ifa_put(ifa); 3744f1705ec1SDavid Ahern } 3745f1705ec1SDavid Ahern 3746381f4dcaSSabrina Dubroca /* Step 5: Discard anycast and multicast list */ 3747381f4dcaSSabrina Dubroca if (how) { 3748381f4dcaSSabrina Dubroca ipv6_ac_destroy_dev(idev); 37491da177e4SLinus Torvalds ipv6_mc_destroy_dev(idev); 3750381f4dcaSSabrina Dubroca } else { 37511da177e4SLinus Torvalds ipv6_mc_down(idev); 3752381f4dcaSSabrina Dubroca } 37531da177e4SLinus Torvalds 37541da177e4SLinus Torvalds idev->tstamp = jiffies; 37551da177e4SLinus Torvalds 3756bcdd553fSStephen Hemminger /* Last: Shot the device (if unregistered) */ 3757439e2385SDenis V. Lunev if (how) { 3758408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 37591da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, idev->nd_parms); 37601da177e4SLinus Torvalds neigh_ifdown(&nd_tbl, dev); 37611da177e4SLinus Torvalds in6_dev_put(idev); 37621da177e4SLinus Torvalds } 37631da177e4SLinus Torvalds return 0; 37641da177e4SLinus Torvalds } 37651da177e4SLinus Torvalds 37661da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data) 37671da177e4SLinus Torvalds { 3768b7b1bfceSHannes Frederic Sowa struct inet6_dev *idev = (struct inet6_dev *)data; 3769caf92bc4SCong Wang struct net_device *dev = idev->dev; 3770b7b1bfceSHannes Frederic Sowa struct in6_addr lladdr; 37711da177e4SLinus Torvalds 3772b7b1bfceSHannes Frederic Sowa write_lock(&idev->lock); 37735b2a1953Sstephen hemminger if (idev->dead || !(idev->if_flags & IF_READY)) 37741da177e4SLinus Torvalds goto out; 37751da177e4SLinus Torvalds 37769ba2add3SShmulik Ladkani if (!ipv6_accept_ra(idev)) 37771da177e4SLinus Torvalds goto out; 37785b2a1953Sstephen hemminger 37795b2a1953Sstephen hemminger /* Announcement received after solicitation was sent */ 37805b2a1953Sstephen hemminger if (idev->if_flags & IF_RA_RCVD) 37815b2a1953Sstephen hemminger goto out; 37821da177e4SLinus Torvalds 3783bd11f074SMaciej Żenczykowski if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) { 3784caf92bc4SCong Wang write_unlock(&idev->lock); 3785caf92bc4SCong Wang if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE)) 3786caf92bc4SCong Wang ndisc_send_rs(dev, &lladdr, 3787b7b1bfceSHannes Frederic Sowa &in6addr_linklocal_allrouters); 3788b7b1bfceSHannes Frederic Sowa else 3789caf92bc4SCong Wang goto put; 3790b7b1bfceSHannes Frederic Sowa 3791caf92bc4SCong Wang write_lock(&idev->lock); 3792bd11f074SMaciej Żenczykowski idev->rs_interval = rfc3315_s14_backoff_update( 3793bd11f074SMaciej Żenczykowski idev->rs_interval, idev->cnf.rtr_solicit_max_interval); 37941da177e4SLinus Torvalds /* The wait after the last probe can be shorter */ 3795b7b1bfceSHannes Frederic Sowa addrconf_mod_rs_timer(idev, (idev->rs_probes == 3796b7b1bfceSHannes Frederic Sowa idev->cnf.rtr_solicits) ? 37975b2a1953Sstephen hemminger idev->cnf.rtr_solicit_delay : 3798bd11f074SMaciej Żenczykowski idev->rs_interval); 37991da177e4SLinus Torvalds } else { 38001da177e4SLinus Torvalds /* 38011da177e4SLinus Torvalds * Note: we do not support deprecated "all on-link" 38021da177e4SLinus Torvalds * assumption any longer. 38031da177e4SLinus Torvalds */ 380491df42beSJoe Perches pr_debug("%s: no IPv6 routers present\n", idev->dev->name); 38051da177e4SLinus Torvalds } 38061da177e4SLinus Torvalds 38071da177e4SLinus Torvalds out: 3808b7b1bfceSHannes Frederic Sowa write_unlock(&idev->lock); 3809caf92bc4SCong Wang put: 3810b7b1bfceSHannes Frederic Sowa in6_dev_put(idev); 38111da177e4SLinus Torvalds } 38121da177e4SLinus Torvalds 38131da177e4SLinus Torvalds /* 38141da177e4SLinus Torvalds * Duplicate Address Detection 38151da177e4SLinus Torvalds */ 38163c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp) 38173c21edbdSYOSHIFUJI Hideaki { 38183c21edbdSYOSHIFUJI Hideaki unsigned long rand_num; 38193c21edbdSYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev; 3820adc176c5SErik Nordmark u64 nonce; 38213c21edbdSYOSHIFUJI Hideaki 382295c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 382395c385b4SNeil Horman rand_num = 0; 382495c385b4SNeil Horman else 382563862b5bSAruna-Hewapathirane rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1); 382695c385b4SNeil Horman 3827adc176c5SErik Nordmark nonce = 0; 3828adc176c5SErik Nordmark if (idev->cnf.enhanced_dad || 3829adc176c5SErik Nordmark dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad) { 3830adc176c5SErik Nordmark do 3831adc176c5SErik Nordmark get_random_bytes(&nonce, 6); 3832adc176c5SErik Nordmark while (nonce == 0); 3833adc176c5SErik Nordmark } 3834adc176c5SErik Nordmark ifp->dad_nonce = nonce; 3835b7b1bfceSHannes Frederic Sowa ifp->dad_probes = idev->cnf.dad_transmits; 3836c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, rand_num); 38373c21edbdSYOSHIFUJI Hideaki } 38383c21edbdSYOSHIFUJI Hideaki 3839c15b1ccaSHannes Frederic Sowa static void addrconf_dad_begin(struct inet6_ifaddr *ifp) 38401da177e4SLinus Torvalds { 38411da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 38421da177e4SLinus Torvalds struct net_device *dev = idev->dev; 3843764d3be6SPaolo Abeni bool bump_id, notify = false; 38441da177e4SLinus Torvalds 38451da177e4SLinus Torvalds addrconf_join_solict(dev, &ifp->addr); 38461da177e4SLinus Torvalds 384763862b5bSAruna-Hewapathirane prandom_seed((__force u32) ifp->addr.s6_addr32[3]); 38481da177e4SLinus Torvalds 38491da177e4SLinus Torvalds read_lock_bh(&idev->lock); 3850622ccdf1SHerbert Xu spin_lock(&ifp->lock); 3851e9d3e084SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DEAD) 38521da177e4SLinus Torvalds goto out; 38531da177e4SLinus Torvalds 38541da177e4SLinus Torvalds if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) || 38551b34be74SYOSHIFUJI Hideaki idev->cnf.accept_dad < 1 || 385655ebaef1SNoriaki TAKAMIYA !(ifp->flags&IFA_F_TENTATIVE) || 385755ebaef1SNoriaki TAKAMIYA ifp->flags & IFA_F_NODAD) { 3858764d3be6SPaolo Abeni bump_id = ifp->flags & IFA_F_TENTATIVE; 3859cc411d0bSBrian Haley ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 386021809fafSstephen hemminger spin_unlock(&ifp->lock); 38611da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 38621da177e4SLinus Torvalds 3863764d3be6SPaolo Abeni addrconf_dad_completed(ifp, bump_id); 38641da177e4SLinus Torvalds return; 38651da177e4SLinus Torvalds } 38661da177e4SLinus Torvalds 38676732badeSYOSHIFUJI Hideaki if (!(idev->if_flags & IF_READY)) { 386821809fafSstephen hemminger spin_unlock(&ifp->lock); 38696732badeSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 38703c21edbdSYOSHIFUJI Hideaki /* 3871590a9887SMasatake YAMATO * If the device is not ready: 38723c21edbdSYOSHIFUJI Hideaki * - keep it tentative if it is a permanent address. 38733c21edbdSYOSHIFUJI Hideaki * - otherwise, kill it. 38743c21edbdSYOSHIFUJI Hideaki */ 38753c21edbdSYOSHIFUJI Hideaki in6_ifa_hold(ifp); 3876cc411d0bSBrian Haley addrconf_dad_stop(ifp, 0); 38776732badeSYOSHIFUJI Hideaki return; 38783c21edbdSYOSHIFUJI Hideaki } 387995c385b4SNeil Horman 388095c385b4SNeil Horman /* 388195c385b4SNeil Horman * Optimistic nodes can start receiving 388295c385b4SNeil Horman * Frames right away 388395c385b4SNeil Horman */ 38847fd2561eSErik Kline if (ifp->flags & IFA_F_OPTIMISTIC) { 388595c385b4SNeil Horman ip6_ins_rt(ifp->rt); 38867fd2561eSErik Kline if (ipv6_use_optimistic_addr(idev)) { 38877fd2561eSErik Kline /* Because optimistic nodes can use this address, 38887fd2561eSErik Kline * notify listeners. If DAD fails, RTM_DELADDR is sent. 38897fd2561eSErik Kline */ 389016186a82Ssubashab@codeaurora.org notify = true; 38917fd2561eSErik Kline } 38927fd2561eSErik Kline } 389395c385b4SNeil Horman 38946732badeSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 38951da177e4SLinus Torvalds out: 3896622ccdf1SHerbert Xu spin_unlock(&ifp->lock); 38971da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 389816186a82Ssubashab@codeaurora.org if (notify) 389916186a82Ssubashab@codeaurora.org ipv6_ifa_notify(RTM_NEWADDR, ifp); 39001da177e4SLinus Torvalds } 39011da177e4SLinus Torvalds 3902c15b1ccaSHannes Frederic Sowa static void addrconf_dad_start(struct inet6_ifaddr *ifp) 39031da177e4SLinus Torvalds { 3904c15b1ccaSHannes Frederic Sowa bool begin_dad = false; 3905c15b1ccaSHannes Frederic Sowa 39068e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 3907c15b1ccaSHannes Frederic Sowa if (ifp->state != INET6_IFADDR_STATE_DEAD) { 3908c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_PREDAD; 3909c15b1ccaSHannes Frederic Sowa begin_dad = true; 3910c15b1ccaSHannes Frederic Sowa } 39118e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 3912c15b1ccaSHannes Frederic Sowa 3913c15b1ccaSHannes Frederic Sowa if (begin_dad) 3914c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 0); 3915c15b1ccaSHannes Frederic Sowa } 3916c15b1ccaSHannes Frederic Sowa 3917c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w) 3918c15b1ccaSHannes Frederic Sowa { 3919c15b1ccaSHannes Frederic Sowa struct inet6_ifaddr *ifp = container_of(to_delayed_work(w), 3920c15b1ccaSHannes Frederic Sowa struct inet6_ifaddr, 3921c15b1ccaSHannes Frederic Sowa dad_work); 39221da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 3923764d3be6SPaolo Abeni bool bump_id, disable_ipv6 = false; 39241da177e4SLinus Torvalds struct in6_addr mcaddr; 39251da177e4SLinus Torvalds 3926c15b1ccaSHannes Frederic Sowa enum { 3927c15b1ccaSHannes Frederic Sowa DAD_PROCESS, 3928c15b1ccaSHannes Frederic Sowa DAD_BEGIN, 3929c15b1ccaSHannes Frederic Sowa DAD_ABORT, 3930c15b1ccaSHannes Frederic Sowa } action = DAD_PROCESS; 3931c15b1ccaSHannes Frederic Sowa 3932c15b1ccaSHannes Frederic Sowa rtnl_lock(); 3933c15b1ccaSHannes Frederic Sowa 39348e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 3935c15b1ccaSHannes Frederic Sowa if (ifp->state == INET6_IFADDR_STATE_PREDAD) { 3936c15b1ccaSHannes Frederic Sowa action = DAD_BEGIN; 3937c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_DAD; 3938c15b1ccaSHannes Frederic Sowa } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) { 3939c15b1ccaSHannes Frederic Sowa action = DAD_ABORT; 3940c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_POSTDAD; 394185b51b12SMike Manning 394285b51b12SMike Manning if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6 && 394385b51b12SMike Manning !(ifp->flags & IFA_F_STABLE_PRIVACY)) { 394485b51b12SMike Manning struct in6_addr addr; 394585b51b12SMike Manning 394685b51b12SMike Manning addr.s6_addr32[0] = htonl(0xfe800000); 394785b51b12SMike Manning addr.s6_addr32[1] = 0; 394885b51b12SMike Manning 394985b51b12SMike Manning if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) && 395085b51b12SMike Manning ipv6_addr_equal(&ifp->addr, &addr)) { 395185b51b12SMike Manning /* DAD failed for link-local based on MAC */ 395285b51b12SMike Manning idev->cnf.disable_ipv6 = 1; 395385b51b12SMike Manning 395485b51b12SMike Manning pr_info("%s: IPv6 being disabled!\n", 395585b51b12SMike Manning ifp->idev->dev->name); 395685b51b12SMike Manning disable_ipv6 = true; 395785b51b12SMike Manning } 395885b51b12SMike Manning } 3959c15b1ccaSHannes Frederic Sowa } 39608e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 3961c15b1ccaSHannes Frederic Sowa 3962c15b1ccaSHannes Frederic Sowa if (action == DAD_BEGIN) { 3963c15b1ccaSHannes Frederic Sowa addrconf_dad_begin(ifp); 3964c15b1ccaSHannes Frederic Sowa goto out; 3965c15b1ccaSHannes Frederic Sowa } else if (action == DAD_ABORT) { 3966751eb6b6SWei Yongjun in6_ifa_hold(ifp); 3967c15b1ccaSHannes Frederic Sowa addrconf_dad_stop(ifp, 1); 396885b51b12SMike Manning if (disable_ipv6) 396985b51b12SMike Manning addrconf_ifdown(idev->dev, 0); 3970c15b1ccaSHannes Frederic Sowa goto out; 3971c15b1ccaSHannes Frederic Sowa } 3972c15b1ccaSHannes Frederic Sowa 3973b7b1bfceSHannes Frederic Sowa if (!ifp->dad_probes && addrconf_dad_end(ifp)) 3974f2344a13SHerbert Xu goto out; 3975f2344a13SHerbert Xu 3976c15b1ccaSHannes Frederic Sowa write_lock_bh(&idev->lock); 3977122e4519Sstephen hemminger if (idev->dead || !(idev->if_flags & IF_READY)) { 3978c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 39791da177e4SLinus Torvalds goto out; 39801da177e4SLinus Torvalds } 398121809fafSstephen hemminger 398221809fafSstephen hemminger spin_lock(&ifp->lock); 3983622ccdf1SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DEAD) { 3984622ccdf1SHerbert Xu spin_unlock(&ifp->lock); 3985c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 3986622ccdf1SHerbert Xu goto out; 3987622ccdf1SHerbert Xu } 3988622ccdf1SHerbert Xu 3989b7b1bfceSHannes Frederic Sowa if (ifp->dad_probes == 0) { 39901da177e4SLinus Torvalds /* 39911da177e4SLinus Torvalds * DAD was successful 39921da177e4SLinus Torvalds */ 39931da177e4SLinus Torvalds 3994764d3be6SPaolo Abeni bump_id = ifp->flags & IFA_F_TENTATIVE; 3995cc411d0bSBrian Haley ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 399621809fafSstephen hemminger spin_unlock(&ifp->lock); 3997c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 39981da177e4SLinus Torvalds 3999764d3be6SPaolo Abeni addrconf_dad_completed(ifp, bump_id); 40001da177e4SLinus Torvalds 40011da177e4SLinus Torvalds goto out; 40021da177e4SLinus Torvalds } 40031da177e4SLinus Torvalds 4004b7b1bfceSHannes Frederic Sowa ifp->dad_probes--; 4005c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 40061f9248e5SJiri Pirko NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME)); 400721809fafSstephen hemminger spin_unlock(&ifp->lock); 4008c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 40091da177e4SLinus Torvalds 40101da177e4SLinus Torvalds /* send a neighbour solicitation for our addr */ 40111da177e4SLinus Torvalds addrconf_addr_solict_mult(&ifp->addr, &mcaddr); 4012adc176c5SErik Nordmark ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any, 4013adc176c5SErik Nordmark ifp->dad_nonce); 40141da177e4SLinus Torvalds out: 40151da177e4SLinus Torvalds in6_ifa_put(ifp); 4016c15b1ccaSHannes Frederic Sowa rtnl_unlock(); 40171da177e4SLinus Torvalds } 40181da177e4SLinus Torvalds 401911ffff75SHannes Frederic Sowa /* ifp->idev must be at least read locked */ 402011ffff75SHannes Frederic Sowa static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp) 402111ffff75SHannes Frederic Sowa { 402211ffff75SHannes Frederic Sowa struct inet6_ifaddr *ifpiter; 402311ffff75SHannes Frederic Sowa struct inet6_dev *idev = ifp->idev; 402411ffff75SHannes Frederic Sowa 4025602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) { 4026602582caSHannes Frederic Sowa if (ifpiter->scope > IFA_LINK) 4027602582caSHannes Frederic Sowa break; 402811ffff75SHannes Frederic Sowa if (ifp != ifpiter && ifpiter->scope == IFA_LINK && 402911ffff75SHannes Frederic Sowa (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE| 403011ffff75SHannes Frederic Sowa IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) == 403111ffff75SHannes Frederic Sowa IFA_F_PERMANENT) 403211ffff75SHannes Frederic Sowa return false; 403311ffff75SHannes Frederic Sowa } 403411ffff75SHannes Frederic Sowa return true; 403511ffff75SHannes Frederic Sowa } 403611ffff75SHannes Frederic Sowa 4037764d3be6SPaolo Abeni static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id) 40381da177e4SLinus Torvalds { 40391da177e4SLinus Torvalds struct net_device *dev = ifp->idev->dev; 4040b7b1bfceSHannes Frederic Sowa struct in6_addr lladdr; 4041b173ee48SHannes Frederic Sowa bool send_rs, send_mld; 4042b7b1bfceSHannes Frederic Sowa 4043c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifp); 40441da177e4SLinus Torvalds 40451da177e4SLinus Torvalds /* 40461da177e4SLinus Torvalds * Configure the address for reception. Now it is valid. 40471da177e4SLinus Torvalds */ 40481da177e4SLinus Torvalds 40491da177e4SLinus Torvalds ipv6_ifa_notify(RTM_NEWADDR, ifp); 40501da177e4SLinus Torvalds 4051026359bcSTore Anderson /* If added prefix is link local and we are prepared to process 4052026359bcSTore Anderson router advertisements, start sending router solicitations. 40531da177e4SLinus Torvalds */ 40541da177e4SLinus Torvalds 40551ec047ebSHannes Frederic Sowa read_lock_bh(&ifp->idev->lock); 405611ffff75SHannes Frederic Sowa send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp); 4057b173ee48SHannes Frederic Sowa send_rs = send_mld && 4058b173ee48SHannes Frederic Sowa ipv6_accept_ra(ifp->idev) && 4059bd11f074SMaciej Żenczykowski ifp->idev->cnf.rtr_solicits != 0 && 4060b173ee48SHannes Frederic Sowa (dev->flags&IFF_LOOPBACK) == 0; 40611ec047ebSHannes Frederic Sowa read_unlock_bh(&ifp->idev->lock); 40621ec047ebSHannes Frederic Sowa 4063b173ee48SHannes Frederic Sowa /* While dad is in progress mld report's source address is in6_addrany. 4064b173ee48SHannes Frederic Sowa * Resend with proper ll now. 4065b173ee48SHannes Frederic Sowa */ 4066b173ee48SHannes Frederic Sowa if (send_mld) 4067b173ee48SHannes Frederic Sowa ipv6_mc_dad_complete(ifp->idev); 4068b173ee48SHannes Frederic Sowa 40691ec047ebSHannes Frederic Sowa if (send_rs) { 40701da177e4SLinus Torvalds /* 40711da177e4SLinus Torvalds * If a host as already performed a random delay 40721da177e4SLinus Torvalds * [...] as part of DAD [...] there is no need 40731da177e4SLinus Torvalds * to delay again before sending the first RS 40741da177e4SLinus Torvalds */ 40751ec047ebSHannes Frederic Sowa if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE)) 4076b7b1bfceSHannes Frederic Sowa return; 40771ec047ebSHannes Frederic Sowa ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters); 40781da177e4SLinus Torvalds 4079b7b1bfceSHannes Frederic Sowa write_lock_bh(&ifp->idev->lock); 4080b7b1bfceSHannes Frederic Sowa spin_lock(&ifp->lock); 4081bd11f074SMaciej Żenczykowski ifp->idev->rs_interval = rfc3315_s14_backoff_init( 4082bd11f074SMaciej Żenczykowski ifp->idev->cnf.rtr_solicit_interval); 4083b7b1bfceSHannes Frederic Sowa ifp->idev->rs_probes = 1; 40841da177e4SLinus Torvalds ifp->idev->if_flags |= IF_RS_SENT; 4085bd11f074SMaciej Żenczykowski addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval); 4086b7b1bfceSHannes Frederic Sowa spin_unlock(&ifp->lock); 4087b7b1bfceSHannes Frederic Sowa write_unlock_bh(&ifp->idev->lock); 40881da177e4SLinus Torvalds } 4089764d3be6SPaolo Abeni 4090764d3be6SPaolo Abeni if (bump_id) 4091764d3be6SPaolo Abeni rt_genid_bump_ipv6(dev_net(dev)); 4092a11a7f71SMarcus Huewe 4093a11a7f71SMarcus Huewe /* Make sure that a new temporary address will be created 4094a11a7f71SMarcus Huewe * before this temporary address becomes deprecated. 4095a11a7f71SMarcus Huewe */ 4096a11a7f71SMarcus Huewe if (ifp->flags & IFA_F_TEMPORARY) 4097a11a7f71SMarcus Huewe addrconf_verify_rtnl(); 40981da177e4SLinus Torvalds } 40991da177e4SLinus Torvalds 4100e21e8467SStephen Hemminger static void addrconf_dad_run(struct inet6_dev *idev) 4101e21e8467SStephen Hemminger { 4102c5e33bddSYOSHIFUJI Hideaki struct inet6_ifaddr *ifp; 4103c5e33bddSYOSHIFUJI Hideaki 4104c5e33bddSYOSHIFUJI Hideaki read_lock_bh(&idev->lock); 4105502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 410621809fafSstephen hemminger spin_lock(&ifp->lock); 4107f2344a13SHerbert Xu if (ifp->flags & IFA_F_TENTATIVE && 4108f2344a13SHerbert Xu ifp->state == INET6_IFADDR_STATE_DAD) 4109c5e33bddSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 4110f2344a13SHerbert Xu spin_unlock(&ifp->lock); 4111c5e33bddSYOSHIFUJI Hideaki } 4112c5e33bddSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 4113c5e33bddSYOSHIFUJI Hideaki } 4114c5e33bddSYOSHIFUJI Hideaki 41151da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 41161da177e4SLinus Torvalds struct if6_iter_state { 41173c40090aSDaniel Lezcano struct seq_net_private p; 41181da177e4SLinus Torvalds int bucket; 41191d578303SMihai Maruseac int offset; 41201da177e4SLinus Torvalds }; 41211da177e4SLinus Torvalds 41221d578303SMihai Maruseac static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos) 41231da177e4SLinus Torvalds { 41241da177e4SLinus Torvalds struct inet6_ifaddr *ifa = NULL; 41251da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 41261218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 41271d578303SMihai Maruseac int p = 0; 41281da177e4SLinus Torvalds 41291d578303SMihai Maruseac /* initial bucket if pos is 0 */ 41301d578303SMihai Maruseac if (pos == 0) { 41311d578303SMihai Maruseac state->bucket = 0; 41321d578303SMihai Maruseac state->offset = 0; 41331d578303SMihai Maruseac } 41341d578303SMihai Maruseac 41351d578303SMihai Maruseac for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) { 4136b67bfe0dSSasha Levin hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket], 41371d578303SMihai Maruseac addr_lst) { 41389f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 41399f0d3c27SEric Dumazet continue; 41401d578303SMihai Maruseac /* sync with offset */ 41411d578303SMihai Maruseac if (p < state->offset) { 41421d578303SMihai Maruseac p++; 41431d578303SMihai Maruseac continue; 41441d578303SMihai Maruseac } 41451d578303SMihai Maruseac state->offset++; 41461da177e4SLinus Torvalds return ifa; 41471da177e4SLinus Torvalds } 41481d578303SMihai Maruseac 41491d578303SMihai Maruseac /* prepare for next bucket */ 41501d578303SMihai Maruseac state->offset = 0; 41511d578303SMihai Maruseac p = 0; 41521d578303SMihai Maruseac } 4153c2e21293Sstephen hemminger return NULL; 4154c2e21293Sstephen hemminger } 41551da177e4SLinus Torvalds 4156c2e21293Sstephen hemminger static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, 4157c2e21293Sstephen hemminger struct inet6_ifaddr *ifa) 41581da177e4SLinus Torvalds { 41591da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 41601218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 41611da177e4SLinus Torvalds 4162b67bfe0dSSasha Levin hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) { 41639f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 41649f0d3c27SEric Dumazet continue; 41651d578303SMihai Maruseac state->offset++; 41661da177e4SLinus Torvalds return ifa; 41671d578303SMihai Maruseac } 41681da177e4SLinus Torvalds 4169c2e21293Sstephen hemminger while (++state->bucket < IN6_ADDR_HSIZE) { 41701d578303SMihai Maruseac state->offset = 0; 4171b67bfe0dSSasha Levin hlist_for_each_entry_rcu_bh(ifa, 4172c2e21293Sstephen hemminger &inet6_addr_lst[state->bucket], addr_lst) { 41739f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 41749f0d3c27SEric Dumazet continue; 41751d578303SMihai Maruseac state->offset++; 4176c2e21293Sstephen hemminger return ifa; 4177c2e21293Sstephen hemminger } 4178c2e21293Sstephen hemminger } 4179c2e21293Sstephen hemminger 4180c2e21293Sstephen hemminger return NULL; 4181c2e21293Sstephen hemminger } 4182c2e21293Sstephen hemminger 41831da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos) 41844f70eccaSEric Dumazet __acquires(rcu_bh) 41851da177e4SLinus Torvalds { 41865c578aedSstephen hemminger rcu_read_lock_bh(); 41871d578303SMihai Maruseac return if6_get_first(seq, *pos); 41881da177e4SLinus Torvalds } 41891da177e4SLinus Torvalds 41901da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos) 41911da177e4SLinus Torvalds { 41921da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 41931da177e4SLinus Torvalds 41941da177e4SLinus Torvalds ifa = if6_get_next(seq, v); 41951da177e4SLinus Torvalds ++*pos; 41961da177e4SLinus Torvalds return ifa; 41971da177e4SLinus Torvalds } 41981da177e4SLinus Torvalds 41991da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v) 42004f70eccaSEric Dumazet __releases(rcu_bh) 42011da177e4SLinus Torvalds { 42025c578aedSstephen hemminger rcu_read_unlock_bh(); 42031da177e4SLinus Torvalds } 42041da177e4SLinus Torvalds 42051da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v) 42061da177e4SLinus Torvalds { 42071da177e4SLinus Torvalds struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v; 4208971a351cSJiri Pirko seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n", 4209b071195dSHarvey Harrison &ifp->addr, 42101da177e4SLinus Torvalds ifp->idev->dev->ifindex, 42111da177e4SLinus Torvalds ifp->prefix_len, 42121da177e4SLinus Torvalds ifp->scope, 4213971a351cSJiri Pirko (u8) ifp->flags, 42141da177e4SLinus Torvalds ifp->idev->dev->name); 42151da177e4SLinus Torvalds return 0; 42161da177e4SLinus Torvalds } 42171da177e4SLinus Torvalds 421856b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = { 42191da177e4SLinus Torvalds .start = if6_seq_start, 42201da177e4SLinus Torvalds .next = if6_seq_next, 42211da177e4SLinus Torvalds .show = if6_seq_show, 42221da177e4SLinus Torvalds .stop = if6_seq_stop, 42231da177e4SLinus Torvalds }; 42241da177e4SLinus Torvalds 42251da177e4SLinus Torvalds static int if6_seq_open(struct inode *inode, struct file *file) 42261da177e4SLinus Torvalds { 42273c40090aSDaniel Lezcano return seq_open_net(inode, file, &if6_seq_ops, 4228cf7732e4SPavel Emelyanov sizeof(struct if6_iter_state)); 42291da177e4SLinus Torvalds } 42301da177e4SLinus Torvalds 42319a32144eSArjan van de Ven static const struct file_operations if6_fops = { 42321da177e4SLinus Torvalds .owner = THIS_MODULE, 42331da177e4SLinus Torvalds .open = if6_seq_open, 42341da177e4SLinus Torvalds .read = seq_read, 42351da177e4SLinus Torvalds .llseek = seq_lseek, 42363c40090aSDaniel Lezcano .release = seq_release_net, 42371da177e4SLinus Torvalds }; 42381da177e4SLinus Torvalds 42392c8c1e72SAlexey Dobriyan static int __net_init if6_proc_net_init(struct net *net) 42401da177e4SLinus Torvalds { 4241d4beaa66SGao feng if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops)) 42421da177e4SLinus Torvalds return -ENOMEM; 42431da177e4SLinus Torvalds return 0; 42441da177e4SLinus Torvalds } 42451da177e4SLinus Torvalds 42462c8c1e72SAlexey Dobriyan static void __net_exit if6_proc_net_exit(struct net *net) 42473c40090aSDaniel Lezcano { 4248ece31ffdSGao feng remove_proc_entry("if_inet6", net->proc_net); 42493c40090aSDaniel Lezcano } 42503c40090aSDaniel Lezcano 42513c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = { 42523c40090aSDaniel Lezcano .init = if6_proc_net_init, 42533c40090aSDaniel Lezcano .exit = if6_proc_net_exit, 42543c40090aSDaniel Lezcano }; 42553c40090aSDaniel Lezcano 42563c40090aSDaniel Lezcano int __init if6_proc_init(void) 42573c40090aSDaniel Lezcano { 42583c40090aSDaniel Lezcano return register_pernet_subsys(&if6_proc_net_ops); 42593c40090aSDaniel Lezcano } 42603c40090aSDaniel Lezcano 42611da177e4SLinus Torvalds void if6_proc_exit(void) 42621da177e4SLinus Torvalds { 42633c40090aSDaniel Lezcano unregister_pernet_subsys(&if6_proc_net_ops); 42641da177e4SLinus Torvalds } 42651da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 42661da177e4SLinus Torvalds 426707a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6) 42683b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */ 4269b71d1d42SEric Dumazet int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr) 42703b9f9a1cSNoriaki TAKAMIYA { 42713b9f9a1cSNoriaki TAKAMIYA int ret = 0; 4272c2e21293Sstephen hemminger struct inet6_ifaddr *ifp = NULL; 4273ddbe5032SEric Dumazet unsigned int hash = inet6_addr_hash(addr); 4274c2e21293Sstephen hemminger 42755c578aedSstephen hemminger rcu_read_lock_bh(); 4276b67bfe0dSSasha Levin hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) { 4277878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 4278389f6612SDaniel Lezcano continue; 4279caad295fSYOSHIFUJI Hideaki if (ipv6_addr_equal(&ifp->addr, addr) && 42803b9f9a1cSNoriaki TAKAMIYA (ifp->flags & IFA_F_HOMEADDRESS)) { 42813b9f9a1cSNoriaki TAKAMIYA ret = 1; 42823b9f9a1cSNoriaki TAKAMIYA break; 42833b9f9a1cSNoriaki TAKAMIYA } 42843b9f9a1cSNoriaki TAKAMIYA } 42855c578aedSstephen hemminger rcu_read_unlock_bh(); 42863b9f9a1cSNoriaki TAKAMIYA return ret; 42873b9f9a1cSNoriaki TAKAMIYA } 42883b9f9a1cSNoriaki TAKAMIYA #endif 42893b9f9a1cSNoriaki TAKAMIYA 42901da177e4SLinus Torvalds /* 42911da177e4SLinus Torvalds * Periodic address status verification 42921da177e4SLinus Torvalds */ 42931da177e4SLinus Torvalds 4294c15b1ccaSHannes Frederic Sowa static void addrconf_verify_rtnl(void) 42951da177e4SLinus Torvalds { 4296b2db7564SYOSHIFUJI Hideaki unsigned long now, next, next_sec, next_sched; 42971da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 42981da177e4SLinus Torvalds int i; 42991da177e4SLinus Torvalds 4300c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 4301c15b1ccaSHannes Frederic Sowa 43025c578aedSstephen hemminger rcu_read_lock_bh(); 43031da177e4SLinus Torvalds now = jiffies; 4304b2db7564SYOSHIFUJI Hideaki next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY); 43051da177e4SLinus Torvalds 4306c15b1ccaSHannes Frederic Sowa cancel_delayed_work(&addr_chk_work); 43071da177e4SLinus Torvalds 43081da177e4SLinus Torvalds for (i = 0; i < IN6_ADDR_HSIZE; i++) { 43091da177e4SLinus Torvalds restart: 4310c15b1ccaSHannes Frederic Sowa hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) { 43111da177e4SLinus Torvalds unsigned long age; 43121da177e4SLinus Torvalds 4313fad8da3eSYasushi Asano /* When setting preferred_lft to a value not zero or 4314fad8da3eSYasushi Asano * infinity, while valid_lft is infinity 4315fad8da3eSYasushi Asano * IFA_F_PERMANENT has a non-infinity life time. 4316fad8da3eSYasushi Asano */ 4317fad8da3eSYasushi Asano if ((ifp->flags & IFA_F_PERMANENT) && 4318fad8da3eSYasushi Asano (ifp->prefered_lft == INFINITY_LIFE_TIME)) 43191da177e4SLinus Torvalds continue; 43201da177e4SLinus Torvalds 43211da177e4SLinus Torvalds spin_lock(&ifp->lock); 4322b2db7564SYOSHIFUJI Hideaki /* We try to batch several events at once. */ 4323b2db7564SYOSHIFUJI Hideaki age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; 43241da177e4SLinus Torvalds 43258f27ebb9SYOSHIFUJI Hideaki if (ifp->valid_lft != INFINITY_LIFE_TIME && 43268f27ebb9SYOSHIFUJI Hideaki age >= ifp->valid_lft) { 43271da177e4SLinus Torvalds spin_unlock(&ifp->lock); 43281da177e4SLinus Torvalds in6_ifa_hold(ifp); 43291da177e4SLinus Torvalds ipv6_del_addr(ifp); 43301da177e4SLinus Torvalds goto restart; 43318f27ebb9SYOSHIFUJI Hideaki } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) { 43328f27ebb9SYOSHIFUJI Hideaki spin_unlock(&ifp->lock); 43338f27ebb9SYOSHIFUJI Hideaki continue; 43341da177e4SLinus Torvalds } else if (age >= ifp->prefered_lft) { 4335a1ed0526SBrian Haley /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */ 43361da177e4SLinus Torvalds int deprecate = 0; 43371da177e4SLinus Torvalds 43381da177e4SLinus Torvalds if (!(ifp->flags&IFA_F_DEPRECATED)) { 43391da177e4SLinus Torvalds deprecate = 1; 43401da177e4SLinus Torvalds ifp->flags |= IFA_F_DEPRECATED; 43411da177e4SLinus Torvalds } 43421da177e4SLinus Torvalds 4343fad8da3eSYasushi Asano if ((ifp->valid_lft != INFINITY_LIFE_TIME) && 4344fad8da3eSYasushi Asano (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))) 43451da177e4SLinus Torvalds next = ifp->tstamp + ifp->valid_lft * HZ; 43461da177e4SLinus Torvalds 43471da177e4SLinus Torvalds spin_unlock(&ifp->lock); 43481da177e4SLinus Torvalds 43491da177e4SLinus Torvalds if (deprecate) { 43501da177e4SLinus Torvalds in6_ifa_hold(ifp); 43511da177e4SLinus Torvalds 43521da177e4SLinus Torvalds ipv6_ifa_notify(0, ifp); 43531da177e4SLinus Torvalds in6_ifa_put(ifp); 43541da177e4SLinus Torvalds goto restart; 43551da177e4SLinus Torvalds } 43561da177e4SLinus Torvalds } else if ((ifp->flags&IFA_F_TEMPORARY) && 43571da177e4SLinus Torvalds !(ifp->flags&IFA_F_TENTATIVE)) { 435888949cf4Sstephen hemminger unsigned long regen_advance = ifp->idev->cnf.regen_max_retry * 435988949cf4Sstephen hemminger ifp->idev->cnf.dad_transmits * 43601f9248e5SJiri Pirko NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ; 436188949cf4Sstephen hemminger 43621da177e4SLinus Torvalds if (age >= ifp->prefered_lft - regen_advance) { 43631da177e4SLinus Torvalds struct inet6_ifaddr *ifpub = ifp->ifpub; 43641da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 43651da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 43661da177e4SLinus Torvalds if (!ifp->regen_count && ifpub) { 43671da177e4SLinus Torvalds ifp->regen_count++; 43681da177e4SLinus Torvalds in6_ifa_hold(ifp); 43691da177e4SLinus Torvalds in6_ifa_hold(ifpub); 43701da177e4SLinus Torvalds spin_unlock(&ifp->lock); 43715c578aedSstephen hemminger 4372291d809bSHiroyuki YAMAMORI spin_lock(&ifpub->lock); 4373291d809bSHiroyuki YAMAMORI ifpub->regen_count = 0; 4374291d809bSHiroyuki YAMAMORI spin_unlock(&ifpub->lock); 43751da177e4SLinus Torvalds ipv6_create_tempaddr(ifpub, ifp); 43761da177e4SLinus Torvalds in6_ifa_put(ifpub); 43771da177e4SLinus Torvalds in6_ifa_put(ifp); 43781da177e4SLinus Torvalds goto restart; 43791da177e4SLinus Torvalds } 43801da177e4SLinus Torvalds } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next)) 43811da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ; 43821da177e4SLinus Torvalds spin_unlock(&ifp->lock); 43831da177e4SLinus Torvalds } else { 43841da177e4SLinus Torvalds /* ifp->prefered_lft <= ifp->valid_lft */ 43851da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 43861da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 43871da177e4SLinus Torvalds spin_unlock(&ifp->lock); 43881da177e4SLinus Torvalds } 43891da177e4SLinus Torvalds } 43901da177e4SLinus Torvalds } 43911da177e4SLinus Torvalds 4392b2db7564SYOSHIFUJI Hideaki next_sec = round_jiffies_up(next); 4393b2db7564SYOSHIFUJI Hideaki next_sched = next; 4394b2db7564SYOSHIFUJI Hideaki 4395b2db7564SYOSHIFUJI Hideaki /* If rounded timeout is accurate enough, accept it. */ 4396b2db7564SYOSHIFUJI Hideaki if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ)) 4397b2db7564SYOSHIFUJI Hideaki next_sched = next_sec; 4398b2db7564SYOSHIFUJI Hideaki 4399b2db7564SYOSHIFUJI Hideaki /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */ 4400b2db7564SYOSHIFUJI Hideaki if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX)) 4401b2db7564SYOSHIFUJI Hideaki next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX; 4402b2db7564SYOSHIFUJI Hideaki 4403ba3542e1Sdingtianhong ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n", 4404ba3542e1Sdingtianhong now, next, next_sec, next_sched); 4405c15b1ccaSHannes Frederic Sowa mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now); 44065c578aedSstephen hemminger rcu_read_unlock_bh(); 44071da177e4SLinus Torvalds } 44081da177e4SLinus Torvalds 4409c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *w) 4410c15b1ccaSHannes Frederic Sowa { 4411c15b1ccaSHannes Frederic Sowa rtnl_lock(); 4412c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 4413c15b1ccaSHannes Frederic Sowa rtnl_unlock(); 4414c15b1ccaSHannes Frederic Sowa } 4415c15b1ccaSHannes Frederic Sowa 4416c15b1ccaSHannes Frederic Sowa static void addrconf_verify(void) 4417c15b1ccaSHannes Frederic Sowa { 4418c15b1ccaSHannes Frederic Sowa mod_delayed_work(addrconf_wq, &addr_chk_work, 0); 4419c15b1ccaSHannes Frederic Sowa } 4420c15b1ccaSHannes Frederic Sowa 4421caeaba79SNicolas Dichtel static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local, 4422caeaba79SNicolas Dichtel struct in6_addr **peer_pfx) 4423461d8837SThomas Graf { 4424461d8837SThomas Graf struct in6_addr *pfx = NULL; 4425461d8837SThomas Graf 4426caeaba79SNicolas Dichtel *peer_pfx = NULL; 4427caeaba79SNicolas Dichtel 4428461d8837SThomas Graf if (addr) 4429461d8837SThomas Graf pfx = nla_data(addr); 4430461d8837SThomas Graf 4431461d8837SThomas Graf if (local) { 4432461d8837SThomas Graf if (pfx && nla_memcmp(local, pfx, sizeof(*pfx))) 4433caeaba79SNicolas Dichtel *peer_pfx = pfx; 4434461d8837SThomas Graf pfx = nla_data(local); 4435461d8837SThomas Graf } 4436461d8837SThomas Graf 4437461d8837SThomas Graf return pfx; 4438461d8837SThomas Graf } 4439461d8837SThomas Graf 4440ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = { 4441461d8837SThomas Graf [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) }, 4442461d8837SThomas Graf [IFA_LOCAL] = { .len = sizeof(struct in6_addr) }, 4443461d8837SThomas Graf [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) }, 4444479840ffSJiri Pirko [IFA_FLAGS] = { .len = sizeof(u32) }, 4445461d8837SThomas Graf }; 4446461d8837SThomas Graf 44471da177e4SLinus Torvalds static int 4448c21ef3e3SDavid Ahern inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, 4449c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 44501da177e4SLinus Torvalds { 44513b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 4452b933f716SThomas Graf struct ifaddrmsg *ifm; 4453b933f716SThomas Graf struct nlattr *tb[IFA_MAX+1]; 4454caeaba79SNicolas Dichtel struct in6_addr *pfx, *peer_pfx; 44556046d5b4SHeiner Kallweit u32 ifa_flags; 4456b933f716SThomas Graf int err; 44571da177e4SLinus Torvalds 4458fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy, 4459c21ef3e3SDavid Ahern extack); 4460b933f716SThomas Graf if (err < 0) 4461b933f716SThomas Graf return err; 4462b933f716SThomas Graf 4463b933f716SThomas Graf ifm = nlmsg_data(nlh); 4464caeaba79SNicolas Dichtel pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); 446563159f29SIan Morris if (!pfx) 44661da177e4SLinus Torvalds return -EINVAL; 44671da177e4SLinus Torvalds 44686046d5b4SHeiner Kallweit ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags; 44696046d5b4SHeiner Kallweit 44706046d5b4SHeiner Kallweit /* We ignore other flags so far. */ 44716046d5b4SHeiner Kallweit ifa_flags &= IFA_F_MANAGETEMPADDR; 44726046d5b4SHeiner Kallweit 44736046d5b4SHeiner Kallweit return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx, 44746046d5b4SHeiner Kallweit ifm->ifa_prefixlen); 44751da177e4SLinus Torvalds } 44761da177e4SLinus Torvalds 4477479840ffSJiri Pirko static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags, 447855ebaef1SNoriaki TAKAMIYA u32 prefered_lft, u32 valid_lft) 4479081bba5bSNoriaki TAKAMIYA { 44806f704992SYOSHIFUJI Hideaki u32 flags; 44816f704992SYOSHIFUJI Hideaki clock_t expires; 44824bed72e4SYOSHIFUJI Hideaki unsigned long timeout; 448353bd6749SJiri Pirko bool was_managetempaddr; 44845b84efecSThomas Haller bool had_prefixroute; 448546d48046SYOSHIFUJI Hideaki 4486c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 4487c15b1ccaSHannes Frederic Sowa 4488081bba5bSNoriaki TAKAMIYA if (!valid_lft || (prefered_lft > valid_lft)) 4489081bba5bSNoriaki TAKAMIYA return -EINVAL; 4490081bba5bSNoriaki TAKAMIYA 449153bd6749SJiri Pirko if (ifa_flags & IFA_F_MANAGETEMPADDR && 449253bd6749SJiri Pirko (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64)) 449353bd6749SJiri Pirko return -EINVAL; 449453bd6749SJiri Pirko 44954bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(valid_lft, HZ); 44964bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 44974bed72e4SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(timeout * HZ); 44984bed72e4SYOSHIFUJI Hideaki valid_lft = timeout; 44996f704992SYOSHIFUJI Hideaki flags = RTF_EXPIRES; 45004bed72e4SYOSHIFUJI Hideaki } else { 45014bed72e4SYOSHIFUJI Hideaki expires = 0; 45024bed72e4SYOSHIFUJI Hideaki flags = 0; 45034bed72e4SYOSHIFUJI Hideaki ifa_flags |= IFA_F_PERMANENT; 45046f704992SYOSHIFUJI Hideaki } 4505081bba5bSNoriaki TAKAMIYA 45064bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(prefered_lft, HZ); 45074bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 45084bed72e4SYOSHIFUJI Hideaki if (timeout == 0) 450955ebaef1SNoriaki TAKAMIYA ifa_flags |= IFA_F_DEPRECATED; 45104bed72e4SYOSHIFUJI Hideaki prefered_lft = timeout; 45114bed72e4SYOSHIFUJI Hideaki } 4512081bba5bSNoriaki TAKAMIYA 4513081bba5bSNoriaki TAKAMIYA spin_lock_bh(&ifp->lock); 451453bd6749SJiri Pirko was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR; 45155b84efecSThomas Haller had_prefixroute = ifp->flags & IFA_F_PERMANENT && 45165b84efecSThomas Haller !(ifp->flags & IFA_F_NOPREFIXROUTE); 451753bd6749SJiri Pirko ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | 4518761aac73SThomas Haller IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR | 4519761aac73SThomas Haller IFA_F_NOPREFIXROUTE); 452053bd6749SJiri Pirko ifp->flags |= ifa_flags; 4521081bba5bSNoriaki TAKAMIYA ifp->tstamp = jiffies; 4522081bba5bSNoriaki TAKAMIYA ifp->valid_lft = valid_lft; 4523081bba5bSNoriaki TAKAMIYA ifp->prefered_lft = prefered_lft; 4524081bba5bSNoriaki TAKAMIYA 4525081bba5bSNoriaki TAKAMIYA spin_unlock_bh(&ifp->lock); 4526081bba5bSNoriaki TAKAMIYA if (!(ifp->flags&IFA_F_TENTATIVE)) 4527081bba5bSNoriaki TAKAMIYA ipv6_ifa_notify(0, ifp); 4528081bba5bSNoriaki TAKAMIYA 4529761aac73SThomas Haller if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) { 453046d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev, 45316f704992SYOSHIFUJI Hideaki expires, flags); 45325b84efecSThomas Haller } else if (had_prefixroute) { 45335b84efecSThomas Haller enum cleanup_prefix_rt_t action; 45345b84efecSThomas Haller unsigned long rt_expires; 45355b84efecSThomas Haller 45365b84efecSThomas Haller write_lock_bh(&ifp->idev->lock); 45375b84efecSThomas Haller action = check_cleanup_prefix_route(ifp, &rt_expires); 45385b84efecSThomas Haller write_unlock_bh(&ifp->idev->lock); 45395b84efecSThomas Haller 45405b84efecSThomas Haller if (action != CLEANUP_PREFIX_RT_NOP) { 45415b84efecSThomas Haller cleanup_prefix_route(ifp, rt_expires, 45425b84efecSThomas Haller action == CLEANUP_PREFIX_RT_DEL); 45435b84efecSThomas Haller } 4544761aac73SThomas Haller } 454553bd6749SJiri Pirko 454653bd6749SJiri Pirko if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) { 454753bd6749SJiri Pirko if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR)) 454853bd6749SJiri Pirko valid_lft = prefered_lft = 0; 454953bd6749SJiri Pirko manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft, 455053bd6749SJiri Pirko !was_managetempaddr, jiffies); 455153bd6749SJiri Pirko } 455253bd6749SJiri Pirko 4553c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 4554081bba5bSNoriaki TAKAMIYA 4555081bba5bSNoriaki TAKAMIYA return 0; 4556081bba5bSNoriaki TAKAMIYA } 4557081bba5bSNoriaki TAKAMIYA 4558081bba5bSNoriaki TAKAMIYA static int 4559c21ef3e3SDavid Ahern inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, 4560c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 45611da177e4SLinus Torvalds { 45623b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 4563461d8837SThomas Graf struct ifaddrmsg *ifm; 4564461d8837SThomas Graf struct nlattr *tb[IFA_MAX+1]; 4565caeaba79SNicolas Dichtel struct in6_addr *pfx, *peer_pfx; 45667198f8ceSThomas Graf struct inet6_ifaddr *ifa; 45677198f8ceSThomas Graf struct net_device *dev; 456855ebaef1SNoriaki TAKAMIYA u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME; 4569479840ffSJiri Pirko u32 ifa_flags; 4570461d8837SThomas Graf int err; 45711da177e4SLinus Torvalds 4572fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy, 4573c21ef3e3SDavid Ahern extack); 4574461d8837SThomas Graf if (err < 0) 4575461d8837SThomas Graf return err; 4576461d8837SThomas Graf 4577461d8837SThomas Graf ifm = nlmsg_data(nlh); 4578caeaba79SNicolas Dichtel pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); 457963159f29SIan Morris if (!pfx) 45801da177e4SLinus Torvalds return -EINVAL; 45811da177e4SLinus Torvalds 4582461d8837SThomas Graf if (tb[IFA_CACHEINFO]) { 45830778769dSNoriaki TAKAMIYA struct ifa_cacheinfo *ci; 4584461d8837SThomas Graf 4585461d8837SThomas Graf ci = nla_data(tb[IFA_CACHEINFO]); 45860778769dSNoriaki TAKAMIYA valid_lft = ci->ifa_valid; 4587461d8837SThomas Graf preferred_lft = ci->ifa_prefered; 4588461d8837SThomas Graf } else { 4589461d8837SThomas Graf preferred_lft = INFINITY_LIFE_TIME; 4590461d8837SThomas Graf valid_lft = INFINITY_LIFE_TIME; 45910778769dSNoriaki TAKAMIYA } 45920778769dSNoriaki TAKAMIYA 4593af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifm->ifa_index); 459463159f29SIan Morris if (!dev) 45957198f8ceSThomas Graf return -ENODEV; 4596081bba5bSNoriaki TAKAMIYA 4597479840ffSJiri Pirko ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags; 4598479840ffSJiri Pirko 459955ebaef1SNoriaki TAKAMIYA /* We ignore other flags so far. */ 4600761aac73SThomas Haller ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR | 460193a714d6SMadhu Challa IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN; 460255ebaef1SNoriaki TAKAMIYA 46031cab3da6SDaniel Lezcano ifa = ipv6_get_ifaddr(net, pfx, dev, 1); 460463159f29SIan Morris if (!ifa) { 46057198f8ceSThomas Graf /* 46067198f8ceSThomas Graf * It would be best to check for !NLM_F_CREATE here but 4607db9c7c39Sstephen hemminger * userspace already relies on not having to provide this. 46087198f8ceSThomas Graf */ 4609caeaba79SNicolas Dichtel return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx, 4610af284937SDaniel Lezcano ifm->ifa_prefixlen, ifa_flags, 4611af284937SDaniel Lezcano preferred_lft, valid_lft); 46121da177e4SLinus Torvalds } 46131da177e4SLinus Torvalds 46147198f8ceSThomas Graf if (nlh->nlmsg_flags & NLM_F_EXCL || 46157198f8ceSThomas Graf !(nlh->nlmsg_flags & NLM_F_REPLACE)) 46167198f8ceSThomas Graf err = -EEXIST; 46177198f8ceSThomas Graf else 461855ebaef1SNoriaki TAKAMIYA err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft); 46197198f8ceSThomas Graf 46207198f8ceSThomas Graf in6_ifa_put(ifa); 46217198f8ceSThomas Graf 46227198f8ceSThomas Graf return err; 46237198f8ceSThomas Graf } 46247198f8ceSThomas Graf 4625479840ffSJiri Pirko static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags, 4626101bb229SThomas Graf u8 scope, int ifindex) 4627101bb229SThomas Graf { 4628101bb229SThomas Graf struct ifaddrmsg *ifm; 4629101bb229SThomas Graf 4630101bb229SThomas Graf ifm = nlmsg_data(nlh); 4631101bb229SThomas Graf ifm->ifa_family = AF_INET6; 4632101bb229SThomas Graf ifm->ifa_prefixlen = prefixlen; 4633101bb229SThomas Graf ifm->ifa_flags = flags; 4634101bb229SThomas Graf ifm->ifa_scope = scope; 4635101bb229SThomas Graf ifm->ifa_index = ifindex; 4636101bb229SThomas Graf } 4637101bb229SThomas Graf 463885486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp, 463985486af0SThomas Graf unsigned long tstamp, u32 preferred, u32 valid) 464085486af0SThomas Graf { 464185486af0SThomas Graf struct ifa_cacheinfo ci; 464285486af0SThomas Graf 464318a31e1eSThomas Graf ci.cstamp = cstamp_delta(cstamp); 464418a31e1eSThomas Graf ci.tstamp = cstamp_delta(tstamp); 464585486af0SThomas Graf ci.ifa_prefered = preferred; 464685486af0SThomas Graf ci.ifa_valid = valid; 464785486af0SThomas Graf 464885486af0SThomas Graf return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci); 464985486af0SThomas Graf } 465085486af0SThomas Graf 4651101bb229SThomas Graf static inline int rt_scope(int ifa_scope) 4652101bb229SThomas Graf { 4653101bb229SThomas Graf if (ifa_scope & IFA_HOST) 4654101bb229SThomas Graf return RT_SCOPE_HOST; 4655101bb229SThomas Graf else if (ifa_scope & IFA_LINK) 4656101bb229SThomas Graf return RT_SCOPE_LINK; 4657101bb229SThomas Graf else if (ifa_scope & IFA_SITE) 4658101bb229SThomas Graf return RT_SCOPE_SITE; 4659101bb229SThomas Graf else 4660101bb229SThomas Graf return RT_SCOPE_UNIVERSE; 4661101bb229SThomas Graf } 4662101bb229SThomas Graf 46630ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void) 46640ab6803bSThomas Graf { 4665339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifaddrmsg)) 4666caeaba79SNicolas Dichtel + nla_total_size(16) /* IFA_LOCAL */ 4667339bf98fSThomas Graf + nla_total_size(16) /* IFA_ADDRESS */ 4668479840ffSJiri Pirko + nla_total_size(sizeof(struct ifa_cacheinfo)) 4669479840ffSJiri Pirko + nla_total_size(4) /* IFA_FLAGS */; 46700ab6803bSThomas Graf } 4671c5396a31SYOSHIFUJI Hideaki 46721da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa, 467315e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 46741da177e4SLinus Torvalds { 46751da177e4SLinus Torvalds struct nlmsghdr *nlh; 467685486af0SThomas Graf u32 preferred, valid; 46771da177e4SLinus Torvalds 467815e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 467963159f29SIan Morris if (!nlh) 468026932566SPatrick McHardy return -EMSGSIZE; 46810ab6803bSThomas Graf 4682101bb229SThomas Graf put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope), 4683101bb229SThomas Graf ifa->idev->dev->ifindex); 4684101bb229SThomas Graf 4685fad8da3eSYasushi Asano if (!((ifa->flags&IFA_F_PERMANENT) && 4686fad8da3eSYasushi Asano (ifa->prefered_lft == INFINITY_LIFE_TIME))) { 468785486af0SThomas Graf preferred = ifa->prefered_lft; 468885486af0SThomas Graf valid = ifa->valid_lft; 468985486af0SThomas Graf if (preferred != INFINITY_LIFE_TIME) { 46901da177e4SLinus Torvalds long tval = (jiffies - ifa->tstamp)/HZ; 4691a1faa698SJens Rosenboom if (preferred > tval) 469285486af0SThomas Graf preferred -= tval; 4693a1faa698SJens Rosenboom else 4694a1faa698SJens Rosenboom preferred = 0; 4695f56619fcSBen Hutchings if (valid != INFINITY_LIFE_TIME) { 4696f56619fcSBen Hutchings if (valid > tval) 469785486af0SThomas Graf valid -= tval; 4698f56619fcSBen Hutchings else 4699f56619fcSBen Hutchings valid = 0; 4700f56619fcSBen Hutchings } 47011da177e4SLinus Torvalds } 47021da177e4SLinus Torvalds } else { 470385486af0SThomas Graf preferred = INFINITY_LIFE_TIME; 470485486af0SThomas Graf valid = INFINITY_LIFE_TIME; 47051da177e4SLinus Torvalds } 470685486af0SThomas Graf 47077996c799SCong Wang if (!ipv6_addr_any(&ifa->peer_addr)) { 4708930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 || 4709930345eaSJiri Benc nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0) 4710caeaba79SNicolas Dichtel goto error; 4711caeaba79SNicolas Dichtel } else 4712930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0) 4713caeaba79SNicolas Dichtel goto error; 4714caeaba79SNicolas Dichtel 4715caeaba79SNicolas Dichtel if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) 4716caeaba79SNicolas Dichtel goto error; 471785486af0SThomas Graf 4718479840ffSJiri Pirko if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0) 4719479840ffSJiri Pirko goto error; 4720479840ffSJiri Pirko 4721053c095aSJohannes Berg nlmsg_end(skb, nlh); 4722053c095aSJohannes Berg return 0; 4723caeaba79SNicolas Dichtel 4724caeaba79SNicolas Dichtel error: 4725caeaba79SNicolas Dichtel nlmsg_cancel(skb, nlh); 4726caeaba79SNicolas Dichtel return -EMSGSIZE; 47271da177e4SLinus Torvalds } 47281da177e4SLinus Torvalds 47291da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca, 473015e47304SEric W. Biederman u32 portid, u32 seq, int event, u16 flags) 47311da177e4SLinus Torvalds { 47321da177e4SLinus Torvalds struct nlmsghdr *nlh; 4733101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 4734101bb229SThomas Graf int ifindex = ifmca->idev->dev->ifindex; 47351da177e4SLinus Torvalds 47361da177e4SLinus Torvalds if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE) 4737101bb229SThomas Graf scope = RT_SCOPE_SITE; 4738101bb229SThomas Graf 473915e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 474063159f29SIan Morris if (!nlh) 474126932566SPatrick McHardy return -EMSGSIZE; 47420ab6803bSThomas Graf 4743101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 4744930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 || 47450ab6803bSThomas Graf put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp, 474626932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 474726932566SPatrick McHardy nlmsg_cancel(skb, nlh); 474826932566SPatrick McHardy return -EMSGSIZE; 474926932566SPatrick McHardy } 475085486af0SThomas Graf 4751053c095aSJohannes Berg nlmsg_end(skb, nlh); 4752053c095aSJohannes Berg return 0; 47531da177e4SLinus Torvalds } 47541da177e4SLinus Torvalds 47551da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca, 475615e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 47571da177e4SLinus Torvalds { 47581da177e4SLinus Torvalds struct nlmsghdr *nlh; 4759101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 4760101bb229SThomas Graf int ifindex = ifaca->aca_idev->dev->ifindex; 47611da177e4SLinus Torvalds 47621da177e4SLinus Torvalds if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE) 4763101bb229SThomas Graf scope = RT_SCOPE_SITE; 4764101bb229SThomas Graf 476515e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 476663159f29SIan Morris if (!nlh) 476726932566SPatrick McHardy return -EMSGSIZE; 47680ab6803bSThomas Graf 4769101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 4770930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 || 47710ab6803bSThomas Graf put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp, 477226932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 477326932566SPatrick McHardy nlmsg_cancel(skb, nlh); 477426932566SPatrick McHardy return -EMSGSIZE; 477526932566SPatrick McHardy } 477685486af0SThomas Graf 4777053c095aSJohannes Berg nlmsg_end(skb, nlh); 4778053c095aSJohannes Berg return 0; 47791da177e4SLinus Torvalds } 47801da177e4SLinus Torvalds 4781e21e8467SStephen Hemminger enum addr_type_t { 47821da177e4SLinus Torvalds UNICAST_ADDR, 47831da177e4SLinus Torvalds MULTICAST_ADDR, 47841da177e4SLinus Torvalds ANYCAST_ADDR, 47851da177e4SLinus Torvalds }; 47861da177e4SLinus Torvalds 4787234b27c3SEric Dumazet /* called with rcu_read_lock() */ 4788234b27c3SEric Dumazet static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb, 4789234b27c3SEric Dumazet struct netlink_callback *cb, enum addr_type_t type, 4790234b27c3SEric Dumazet int s_ip_idx, int *p_ip_idx) 47911da177e4SLinus Torvalds { 47921da177e4SLinus Torvalds struct ifmcaddr6 *ifmca; 47931da177e4SLinus Torvalds struct ifacaddr6 *ifaca; 4794234b27c3SEric Dumazet int err = 1; 4795234b27c3SEric Dumazet int ip_idx = *p_ip_idx; 47961da177e4SLinus Torvalds 47971da177e4SLinus Torvalds read_lock_bh(&idev->lock); 47981da177e4SLinus Torvalds switch (type) { 4799502a2ffdSstephen hemminger case UNICAST_ADDR: { 4800502a2ffdSstephen hemminger struct inet6_ifaddr *ifa; 4801502a2ffdSstephen hemminger 4802ae9cda5dSYOSHIFUJI Hideaki /* unicast address incl. temp addr */ 4803502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 4804502a2ffdSstephen hemminger if (++ip_idx < s_ip_idx) 48051da177e4SLinus Torvalds continue; 48065d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifaddr(skb, ifa, 480715e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 48085d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 48095d5619b4SYOSHIFUJI Hideaki RTM_NEWADDR, 48105d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 4811053c095aSJohannes Berg if (err < 0) 4812bcd32326SEric Dumazet break; 481363998ac2SNicolas Dichtel nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 48141da177e4SLinus Torvalds } 48151da177e4SLinus Torvalds break; 4816502a2ffdSstephen hemminger } 48171da177e4SLinus Torvalds case MULTICAST_ADDR: 48181da177e4SLinus Torvalds /* multicast address */ 48191da177e4SLinus Torvalds for (ifmca = idev->mc_list; ifmca; 48201da177e4SLinus Torvalds ifmca = ifmca->next, ip_idx++) { 48211da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 48221da177e4SLinus Torvalds continue; 48235d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifmcaddr(skb, ifmca, 482415e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 48255d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 48265d5619b4SYOSHIFUJI Hideaki RTM_GETMULTICAST, 48275d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 4828053c095aSJohannes Berg if (err < 0) 4829bcd32326SEric Dumazet break; 48301da177e4SLinus Torvalds } 48311da177e4SLinus Torvalds break; 48321da177e4SLinus Torvalds case ANYCAST_ADDR: 48331da177e4SLinus Torvalds /* anycast address */ 48341da177e4SLinus Torvalds for (ifaca = idev->ac_list; ifaca; 48351da177e4SLinus Torvalds ifaca = ifaca->aca_next, ip_idx++) { 48361da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 48371da177e4SLinus Torvalds continue; 48385d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifacaddr(skb, ifaca, 483915e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 48405d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 48415d5619b4SYOSHIFUJI Hideaki RTM_GETANYCAST, 48425d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 4843053c095aSJohannes Berg if (err < 0) 4844bcd32326SEric Dumazet break; 48451da177e4SLinus Torvalds } 48461da177e4SLinus Torvalds break; 48471da177e4SLinus Torvalds default: 48481da177e4SLinus Torvalds break; 48491da177e4SLinus Torvalds } 48501da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 4851234b27c3SEric Dumazet *p_ip_idx = ip_idx; 4852234b27c3SEric Dumazet return err; 4853234b27c3SEric Dumazet } 48545d5619b4SYOSHIFUJI Hideaki 4855234b27c3SEric Dumazet static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb, 4856234b27c3SEric Dumazet enum addr_type_t type) 4857234b27c3SEric Dumazet { 4858234b27c3SEric Dumazet struct net *net = sock_net(skb->sk); 4859234b27c3SEric Dumazet int h, s_h; 4860234b27c3SEric Dumazet int idx, ip_idx; 4861234b27c3SEric Dumazet int s_idx, s_ip_idx; 4862234b27c3SEric Dumazet struct net_device *dev; 4863234b27c3SEric Dumazet struct inet6_dev *idev; 4864234b27c3SEric Dumazet struct hlist_head *head; 4865234b27c3SEric Dumazet 4866234b27c3SEric Dumazet s_h = cb->args[0]; 4867234b27c3SEric Dumazet s_idx = idx = cb->args[1]; 4868234b27c3SEric Dumazet s_ip_idx = ip_idx = cb->args[2]; 4869234b27c3SEric Dumazet 4870234b27c3SEric Dumazet rcu_read_lock(); 487163998ac2SNicolas Dichtel cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq; 4872234b27c3SEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 4873234b27c3SEric Dumazet idx = 0; 4874234b27c3SEric Dumazet head = &net->dev_index_head[h]; 4875b67bfe0dSSasha Levin hlist_for_each_entry_rcu(dev, head, index_hlist) { 4876234b27c3SEric Dumazet if (idx < s_idx) 4877234b27c3SEric Dumazet goto cont; 48784b97efdfSPatrick McHardy if (h > s_h || idx > s_idx) 4879234b27c3SEric Dumazet s_ip_idx = 0; 4880234b27c3SEric Dumazet ip_idx = 0; 4881e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 4882e21e8467SStephen Hemminger if (!idev) 4883234b27c3SEric Dumazet goto cont; 4884234b27c3SEric Dumazet 4885234b27c3SEric Dumazet if (in6_dump_addrs(idev, skb, cb, type, 48867b46a644SDavid S. Miller s_ip_idx, &ip_idx) < 0) 4887234b27c3SEric Dumazet goto done; 48887562f876SPavel Emelianov cont: 48897562f876SPavel Emelianov idx++; 48901da177e4SLinus Torvalds } 4891234b27c3SEric Dumazet } 4892234b27c3SEric Dumazet done: 4893234b27c3SEric Dumazet rcu_read_unlock(); 4894234b27c3SEric Dumazet cb->args[0] = h; 4895234b27c3SEric Dumazet cb->args[1] = idx; 4896234b27c3SEric Dumazet cb->args[2] = ip_idx; 4897234b27c3SEric Dumazet 48981da177e4SLinus Torvalds return skb->len; 48991da177e4SLinus Torvalds } 49001da177e4SLinus Torvalds 49011da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) 49021da177e4SLinus Torvalds { 49031da177e4SLinus Torvalds enum addr_type_t type = UNICAST_ADDR; 4904b854272bSDenis V. Lunev 49051da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 49061da177e4SLinus Torvalds } 49071da177e4SLinus Torvalds 49081da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb) 49091da177e4SLinus Torvalds { 49101da177e4SLinus Torvalds enum addr_type_t type = MULTICAST_ADDR; 4911b854272bSDenis V. Lunev 49121da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 49131da177e4SLinus Torvalds } 49141da177e4SLinus Torvalds 49151da177e4SLinus Torvalds 49161da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb) 49171da177e4SLinus Torvalds { 49181da177e4SLinus Torvalds enum addr_type_t type = ANYCAST_ADDR; 4919b854272bSDenis V. Lunev 49201da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 49211da177e4SLinus Torvalds } 49221da177e4SLinus Torvalds 4923c21ef3e3SDavid Ahern static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh, 4924c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 49256c223828SNoriaki TAKAMIYA { 49263b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 49271b29fc2cSThomas Graf struct ifaddrmsg *ifm; 49281b29fc2cSThomas Graf struct nlattr *tb[IFA_MAX+1]; 4929caeaba79SNicolas Dichtel struct in6_addr *addr = NULL, *peer; 49306c223828SNoriaki TAKAMIYA struct net_device *dev = NULL; 49316c223828SNoriaki TAKAMIYA struct inet6_ifaddr *ifa; 49326c223828SNoriaki TAKAMIYA struct sk_buff *skb; 49336c223828SNoriaki TAKAMIYA int err; 49346c223828SNoriaki TAKAMIYA 4935fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy, 4936c21ef3e3SDavid Ahern extack); 49371b29fc2cSThomas Graf if (err < 0) 49381b29fc2cSThomas Graf goto errout; 49396c223828SNoriaki TAKAMIYA 4940caeaba79SNicolas Dichtel addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer); 494163159f29SIan Morris if (!addr) { 49421b29fc2cSThomas Graf err = -EINVAL; 49431b29fc2cSThomas Graf goto errout; 49441b29fc2cSThomas Graf } 49451b29fc2cSThomas Graf 49461b29fc2cSThomas Graf ifm = nlmsg_data(nlh); 49476c223828SNoriaki TAKAMIYA if (ifm->ifa_index) 49486fda7350SBenjamin Thery dev = __dev_get_by_index(net, ifm->ifa_index); 49496c223828SNoriaki TAKAMIYA 4950e21e8467SStephen Hemminger ifa = ipv6_get_ifaddr(net, addr, dev, 1); 4951e21e8467SStephen Hemminger if (!ifa) { 49521b29fc2cSThomas Graf err = -EADDRNOTAVAIL; 49531b29fc2cSThomas Graf goto errout; 49546c223828SNoriaki TAKAMIYA } 49556c223828SNoriaki TAKAMIYA 4956e21e8467SStephen Hemminger skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL); 4957e21e8467SStephen Hemminger if (!skb) { 49581b29fc2cSThomas Graf err = -ENOBUFS; 49591b29fc2cSThomas Graf goto errout_ifa; 49601b29fc2cSThomas Graf } 49611b29fc2cSThomas Graf 496215e47304SEric W. Biederman err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid, 49636c223828SNoriaki TAKAMIYA nlh->nlmsg_seq, RTM_NEWADDR, 0); 496426932566SPatrick McHardy if (err < 0) { 496526932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 496626932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 496726932566SPatrick McHardy kfree_skb(skb); 496826932566SPatrick McHardy goto errout_ifa; 496926932566SPatrick McHardy } 497015e47304SEric W. Biederman err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 49711b29fc2cSThomas Graf errout_ifa: 49726c223828SNoriaki TAKAMIYA in6_ifa_put(ifa); 49731b29fc2cSThomas Graf errout: 49746c223828SNoriaki TAKAMIYA return err; 49756c223828SNoriaki TAKAMIYA } 49766c223828SNoriaki TAKAMIYA 49771da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa) 49781da177e4SLinus Torvalds { 49791da177e4SLinus Torvalds struct sk_buff *skb; 4980c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(ifa->idev->dev); 49815d620266SThomas Graf int err = -ENOBUFS; 49821da177e4SLinus Torvalds 4983f784ad3dSMahesh Bandewar /* Don't send DELADDR notification for TENTATIVE address, 4984f784ad3dSMahesh Bandewar * since NEWADDR notification is sent only after removing 4985f784ad3dSMahesh Bandewar * TENTATIVE flag. 4986f784ad3dSMahesh Bandewar */ 4987f784ad3dSMahesh Bandewar if (ifa->flags & IFA_F_TENTATIVE && event == RTM_DELADDR) 4988f784ad3dSMahesh Bandewar return; 4989f784ad3dSMahesh Bandewar 49900ab6803bSThomas Graf skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC); 499163159f29SIan Morris if (!skb) 49925d620266SThomas Graf goto errout; 49935d620266SThomas Graf 49945d620266SThomas Graf err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0); 499526932566SPatrick McHardy if (err < 0) { 499626932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 499726932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 499826932566SPatrick McHardy kfree_skb(skb); 499926932566SPatrick McHardy goto errout; 500026932566SPatrick McHardy } 50011ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC); 50021ce85fe4SPablo Neira Ayuso return; 50035d620266SThomas Graf errout: 50045d620266SThomas Graf if (err < 0) 50056fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err); 50061da177e4SLinus Torvalds } 50071da177e4SLinus Torvalds 5008b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf, 50091da177e4SLinus Torvalds __s32 *array, int bytes) 50101da177e4SLinus Torvalds { 501104561c1fSThomas Graf BUG_ON(bytes < (DEVCONF_MAX * 4)); 501204561c1fSThomas Graf 50131da177e4SLinus Torvalds memset(array, 0, bytes); 50141da177e4SLinus Torvalds array[DEVCONF_FORWARDING] = cnf->forwarding; 50151da177e4SLinus Torvalds array[DEVCONF_HOPLIMIT] = cnf->hop_limit; 50161da177e4SLinus Torvalds array[DEVCONF_MTU6] = cnf->mtu6; 50171da177e4SLinus Torvalds array[DEVCONF_ACCEPT_RA] = cnf->accept_ra; 50181da177e4SLinus Torvalds array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects; 50191da177e4SLinus Torvalds array[DEVCONF_AUTOCONF] = cnf->autoconf; 50201da177e4SLinus Torvalds array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits; 50211da177e4SLinus Torvalds array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits; 502293908d19SThomas Graf array[DEVCONF_RTR_SOLICIT_INTERVAL] = 502393908d19SThomas Graf jiffies_to_msecs(cnf->rtr_solicit_interval); 5024bd11f074SMaciej Żenczykowski array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] = 5025bd11f074SMaciej Żenczykowski jiffies_to_msecs(cnf->rtr_solicit_max_interval); 502693908d19SThomas Graf array[DEVCONF_RTR_SOLICIT_DELAY] = 502793908d19SThomas Graf jiffies_to_msecs(cnf->rtr_solicit_delay); 50281da177e4SLinus Torvalds array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version; 5029fc4eba58SHannes Frederic Sowa array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] = 5030fc4eba58SHannes Frederic Sowa jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval); 5031fc4eba58SHannes Frederic Sowa array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] = 5032fc4eba58SHannes Frederic Sowa jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval); 50331da177e4SLinus Torvalds array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr; 50341da177e4SLinus Torvalds array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft; 50351da177e4SLinus Torvalds array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft; 50361da177e4SLinus Torvalds array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry; 50371da177e4SLinus Torvalds array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor; 50381da177e4SLinus Torvalds array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses; 503965f5c7c1SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr; 50408013d1d7SHangbin Liu array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit; 5041c4fd30ebSYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo; 5042930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 5043930d6ff2SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref; 504493908d19SThomas Graf array[DEVCONF_RTR_PROBE_INTERVAL] = 504593908d19SThomas Graf jiffies_to_msecs(cnf->rtr_probe_interval); 5046fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 5047bbea124bSJoel Scherpelz array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] = cnf->accept_ra_rt_info_min_plen; 504809c884d4SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen; 504909c884d4SYOSHIFUJI Hideaki #endif 5050930d6ff2SYOSHIFUJI Hideaki #endif 5051fbea49e1SYOSHIFUJI Hideaki array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp; 50520bcbc926SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route; 505395c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 505495c385b4SNeil Horman array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad; 50557fd2561eSErik Kline array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic; 505695c385b4SNeil Horman #endif 50577bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 50587bc570c8SYOSHIFUJI Hideaki array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding; 50597bc570c8SYOSHIFUJI Hideaki #endif 5060778d80beSYOSHIFUJI Hideaki array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6; 50611b34be74SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad; 5062c3faca05SCosmin Ratiu array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao; 50635cb04436SHannes Frederic Sowa array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify; 5064b800c3b9SHannes Frederic Sowa array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc; 5065d9333196SBen Greear array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local; 5066c2943f14SHarout Hedeshian array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu; 506735103d11SAndy Gospodarek array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown; 50683d1bec99SHannes Frederic Sowa /* we omit DEVCONF_STABLE_SECRET for now */ 50693985e8a3SErik Kline array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only; 5070abbc3043SJohannes Berg array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast; 50717a02bf89SJohannes Berg array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na; 5072f1705ec1SDavid Ahern array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down; 50731ababebaSDavid Lebrun array[DEVCONF_SEG6_ENABLED] = cnf->seg6_enabled; 5074bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC 5075bf355b8dSDavid Lebrun array[DEVCONF_SEG6_REQUIRE_HMAC] = cnf->seg6_require_hmac; 5076bf355b8dSDavid Lebrun #endif 5077adc176c5SErik Nordmark array[DEVCONF_ENHANCED_DAD] = cnf->enhanced_dad; 5078d35a00b8SFelix Jia array[DEVCONF_ADDR_GEN_MODE] = cnf->addr_gen_mode; 5079df789fe7SDavid Forster array[DEVCONF_DISABLE_POLICY] = cnf->disable_policy; 50801da177e4SLinus Torvalds } 50811da177e4SLinus Torvalds 5082b382b191SThomas Graf static inline size_t inet6_ifla6_size(void) 5083b382b191SThomas Graf { 5084b382b191SThomas Graf return nla_total_size(4) /* IFLA_INET6_FLAGS */ 5085b382b191SThomas Graf + nla_total_size(sizeof(struct ifla_cacheinfo)) 5086b382b191SThomas Graf + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */ 5087b382b191SThomas Graf + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */ 5088f53adae4SDaniel Borkmann + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */ 5089f53adae4SDaniel Borkmann + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */ 5090b382b191SThomas Graf } 5091b382b191SThomas Graf 5092339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void) 5093339bf98fSThomas Graf { 5094339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 5095339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 5096339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 5097339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 5098339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 50990344338bSAndy Gospodarek + nla_total_size(1) /* IFLA_OPERSTATE */ 5100b382b191SThomas Graf + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */ 5101339bf98fSThomas Graf } 5102c5396a31SYOSHIFUJI Hideaki 5103be281e55SEric Dumazet static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib, 5104aca05671SJia He int bytes) 51057f7d9a6bSHerbert Xu { 51067f7d9a6bSHerbert Xu int i; 5107aca05671SJia He int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX; 51087f7d9a6bSHerbert Xu BUG_ON(pad < 0); 51097f7d9a6bSHerbert Xu 51107f7d9a6bSHerbert Xu /* Use put_unaligned() because stats may not be aligned for u64. */ 5111aca05671SJia He put_unaligned(ICMP6_MIB_MAX, &stats[0]); 5112aca05671SJia He for (i = 1; i < ICMP6_MIB_MAX; i++) 5113be281e55SEric Dumazet put_unaligned(atomic_long_read(&mib[i]), &stats[i]); 51147f7d9a6bSHerbert Xu 5115aca05671SJia He memset(&stats[ICMP6_MIB_MAX], 0, pad); 51167f7d9a6bSHerbert Xu } 51177f7d9a6bSHerbert Xu 5118698365faSWANG Cong static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib, 5119a3a77372SRaghavendra K T int bytes, size_t syncpoff) 51204ce3c183SEric Dumazet { 5121a3a77372SRaghavendra K T int i, c; 5122a3a77372SRaghavendra K T u64 buff[IPSTATS_MIB_MAX]; 5123a3a77372SRaghavendra K T int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX; 5124a3a77372SRaghavendra K T 51254ce3c183SEric Dumazet BUG_ON(pad < 0); 51264ce3c183SEric Dumazet 5127a3a77372SRaghavendra K T memset(buff, 0, sizeof(buff)); 5128a3a77372SRaghavendra K T buff[0] = IPSTATS_MIB_MAX; 51294ce3c183SEric Dumazet 5130a3a77372SRaghavendra K T for_each_possible_cpu(c) { 5131a3a77372SRaghavendra K T for (i = 1; i < IPSTATS_MIB_MAX; i++) 5132a3a77372SRaghavendra K T buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff); 5133a3a77372SRaghavendra K T } 5134a3a77372SRaghavendra K T 5135a3a77372SRaghavendra K T memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64)); 5136a3a77372SRaghavendra K T memset(&stats[IPSTATS_MIB_MAX], 0, pad); 51374ce3c183SEric Dumazet } 51384ce3c183SEric Dumazet 51397f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype, 51407f7d9a6bSHerbert Xu int bytes) 51417f7d9a6bSHerbert Xu { 51427f7d9a6bSHerbert Xu switch (attrtype) { 51437f7d9a6bSHerbert Xu case IFLA_INET6_STATS: 5144a3a77372SRaghavendra K T __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes, 5145a3a77372SRaghavendra K T offsetof(struct ipstats_mib, syncp)); 51467f7d9a6bSHerbert Xu break; 51477f7d9a6bSHerbert Xu case IFLA_INET6_ICMP6STATS: 5148aca05671SJia He __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes); 51497f7d9a6bSHerbert Xu break; 51507f7d9a6bSHerbert Xu } 51517f7d9a6bSHerbert Xu } 51527f7d9a6bSHerbert Xu 5153d5566fd7SSowmini Varadhan static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev, 5154d5566fd7SSowmini Varadhan u32 ext_filter_mask) 51551da177e4SLinus Torvalds { 5156bf99f1bdSYOSHIFUJI Hideaki struct nlattr *nla; 51571da177e4SLinus Torvalds struct ifla_cacheinfo ci; 51581da177e4SLinus Torvalds 5159c78679e8SDavid S. Miller if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags)) 5160c78679e8SDavid S. Miller goto nla_put_failure; 51611da177e4SLinus Torvalds ci.max_reasm_len = IPV6_MAXPLEN; 516224912420SDavid S. Miller ci.tstamp = cstamp_delta(idev->tstamp); 516324912420SDavid S. Miller ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time); 51641f9248e5SJiri Pirko ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME)); 5165c78679e8SDavid S. Miller if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci)) 5166c78679e8SDavid S. Miller goto nla_put_failure; 5167bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32)); 516863159f29SIan Morris if (!nla) 516904561c1fSThomas Graf goto nla_put_failure; 5170bf99f1bdSYOSHIFUJI Hideaki ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla)); 51711da177e4SLinus Torvalds 5172bf99f1bdSYOSHIFUJI Hideaki /* XXX - MC not implemented */ 5173bf99f1bdSYOSHIFUJI Hideaki 5174d5566fd7SSowmini Varadhan if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS) 5175d5566fd7SSowmini Varadhan return 0; 5176d5566fd7SSowmini Varadhan 5177bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64)); 517863159f29SIan Morris if (!nla) 5179bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 5180bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla)); 5181bf99f1bdSYOSHIFUJI Hideaki 5182bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64)); 518363159f29SIan Morris if (!nla) 5184bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 5185bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla)); 51861da177e4SLinus Torvalds 5187f53adae4SDaniel Borkmann nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr)); 518863159f29SIan Morris if (!nla) 5189f53adae4SDaniel Borkmann goto nla_put_failure; 5190bc91b0f0SJiri Pirko 5191d35a00b8SFelix Jia if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->cnf.addr_gen_mode)) 5192bc91b0f0SJiri Pirko goto nla_put_failure; 5193bc91b0f0SJiri Pirko 5194f53adae4SDaniel Borkmann read_lock_bh(&idev->lock); 5195f53adae4SDaniel Borkmann memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla)); 5196f53adae4SDaniel Borkmann read_unlock_bh(&idev->lock); 5197f53adae4SDaniel Borkmann 5198b382b191SThomas Graf return 0; 5199b382b191SThomas Graf 5200b382b191SThomas Graf nla_put_failure: 5201b382b191SThomas Graf return -EMSGSIZE; 5202b382b191SThomas Graf } 5203b382b191SThomas Graf 5204b1974ed0SArad, Ronen static size_t inet6_get_link_af_size(const struct net_device *dev, 5205b1974ed0SArad, Ronen u32 ext_filter_mask) 5206b382b191SThomas Graf { 5207b382b191SThomas Graf if (!__in6_dev_get(dev)) 5208b382b191SThomas Graf return 0; 5209b382b191SThomas Graf 5210b382b191SThomas Graf return inet6_ifla6_size(); 5211b382b191SThomas Graf } 5212b382b191SThomas Graf 5213d5566fd7SSowmini Varadhan static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev, 5214d5566fd7SSowmini Varadhan u32 ext_filter_mask) 5215b382b191SThomas Graf { 5216b382b191SThomas Graf struct inet6_dev *idev = __in6_dev_get(dev); 5217b382b191SThomas Graf 5218b382b191SThomas Graf if (!idev) 5219b382b191SThomas Graf return -ENODATA; 5220b382b191SThomas Graf 5221d5566fd7SSowmini Varadhan if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0) 5222b382b191SThomas Graf return -EMSGSIZE; 5223b382b191SThomas Graf 5224b382b191SThomas Graf return 0; 5225b382b191SThomas Graf } 5226b382b191SThomas Graf 5227f53adae4SDaniel Borkmann static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token) 5228f53adae4SDaniel Borkmann { 5229f53adae4SDaniel Borkmann struct inet6_ifaddr *ifp; 5230f53adae4SDaniel Borkmann struct net_device *dev = idev->dev; 523147e27d5eSDaniel Borkmann bool clear_token, update_rs = false; 5232dc848292SHannes Frederic Sowa struct in6_addr ll_addr; 5233f53adae4SDaniel Borkmann 5234c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 5235c15b1ccaSHannes Frederic Sowa 523663159f29SIan Morris if (!token) 5237f53adae4SDaniel Borkmann return -EINVAL; 5238f53adae4SDaniel Borkmann if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) 5239f53adae4SDaniel Borkmann return -EINVAL; 5240f53adae4SDaniel Borkmann if (!ipv6_accept_ra(idev)) 5241f53adae4SDaniel Borkmann return -EINVAL; 5242bd11f074SMaciej Żenczykowski if (idev->cnf.rtr_solicits == 0) 5243f53adae4SDaniel Borkmann return -EINVAL; 5244f53adae4SDaniel Borkmann 5245f53adae4SDaniel Borkmann write_lock_bh(&idev->lock); 5246f53adae4SDaniel Borkmann 5247f53adae4SDaniel Borkmann BUILD_BUG_ON(sizeof(token->s6_addr) != 16); 5248f53adae4SDaniel Borkmann memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8); 5249f53adae4SDaniel Borkmann 5250f53adae4SDaniel Borkmann write_unlock_bh(&idev->lock); 5251f53adae4SDaniel Borkmann 525247e27d5eSDaniel Borkmann clear_token = ipv6_addr_any(token); 525347e27d5eSDaniel Borkmann if (clear_token) 525447e27d5eSDaniel Borkmann goto update_lft; 525547e27d5eSDaniel Borkmann 5256dc848292SHannes Frederic Sowa if (!idev->dead && (idev->if_flags & IF_READY) && 5257dc848292SHannes Frederic Sowa !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE | 5258dc848292SHannes Frederic Sowa IFA_F_OPTIMISTIC)) { 5259fc403832SDaniel Borkmann /* If we're not ready, then normal ifup will take care 5260fc403832SDaniel Borkmann * of this. Otherwise, we need to request our rs here. 5261fc403832SDaniel Borkmann */ 5262f53adae4SDaniel Borkmann ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters); 5263fc403832SDaniel Borkmann update_rs = true; 5264fc403832SDaniel Borkmann } 5265f53adae4SDaniel Borkmann 526647e27d5eSDaniel Borkmann update_lft: 5267f53adae4SDaniel Borkmann write_lock_bh(&idev->lock); 5268fc403832SDaniel Borkmann 526977ecaaceSHannes Frederic Sowa if (update_rs) { 5270f53adae4SDaniel Borkmann idev->if_flags |= IF_RS_SENT; 5271bd11f074SMaciej Żenczykowski idev->rs_interval = rfc3315_s14_backoff_init( 5272bd11f074SMaciej Żenczykowski idev->cnf.rtr_solicit_interval); 527377ecaaceSHannes Frederic Sowa idev->rs_probes = 1; 5274bd11f074SMaciej Żenczykowski addrconf_mod_rs_timer(idev, idev->rs_interval); 527577ecaaceSHannes Frederic Sowa } 5276f53adae4SDaniel Borkmann 5277f53adae4SDaniel Borkmann /* Well, that's kinda nasty ... */ 5278f53adae4SDaniel Borkmann list_for_each_entry(ifp, &idev->addr_list, if_list) { 5279f53adae4SDaniel Borkmann spin_lock(&ifp->lock); 5280617fe29dSDaniel Borkmann if (ifp->tokenized) { 5281f53adae4SDaniel Borkmann ifp->valid_lft = 0; 5282f53adae4SDaniel Borkmann ifp->prefered_lft = 0; 5283f53adae4SDaniel Borkmann } 5284f53adae4SDaniel Borkmann spin_unlock(&ifp->lock); 5285f53adae4SDaniel Borkmann } 5286f53adae4SDaniel Borkmann 5287f53adae4SDaniel Borkmann write_unlock_bh(&idev->lock); 5288b2ed64a9SLubomir Rintel inet6_ifinfo_notify(RTM_NEWLINK, idev); 5289c15b1ccaSHannes Frederic Sowa addrconf_verify_rtnl(); 5290f53adae4SDaniel Borkmann return 0; 5291f53adae4SDaniel Borkmann } 5292f53adae4SDaniel Borkmann 529311b1f828SDaniel Borkmann static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = { 529411b1f828SDaniel Borkmann [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 }, 529511b1f828SDaniel Borkmann [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) }, 529611b1f828SDaniel Borkmann }; 529711b1f828SDaniel Borkmann 529811b1f828SDaniel Borkmann static int inet6_validate_link_af(const struct net_device *dev, 529911b1f828SDaniel Borkmann const struct nlattr *nla) 530011b1f828SDaniel Borkmann { 530111b1f828SDaniel Borkmann struct nlattr *tb[IFLA_INET6_MAX + 1]; 530211b1f828SDaniel Borkmann 530311b1f828SDaniel Borkmann if (dev && !__in6_dev_get(dev)) 530411b1f828SDaniel Borkmann return -EAFNOSUPPORT; 530511b1f828SDaniel Borkmann 5306fceb6435SJohannes Berg return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy, 5307fceb6435SJohannes Berg NULL); 530811b1f828SDaniel Borkmann } 530911b1f828SDaniel Borkmann 5310d35a00b8SFelix Jia static int check_addr_gen_mode(int mode) 5311d35a00b8SFelix Jia { 5312d35a00b8SFelix Jia if (mode != IN6_ADDR_GEN_MODE_EUI64 && 5313d35a00b8SFelix Jia mode != IN6_ADDR_GEN_MODE_NONE && 5314d35a00b8SFelix Jia mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY && 5315d35a00b8SFelix Jia mode != IN6_ADDR_GEN_MODE_RANDOM) 5316d35a00b8SFelix Jia return -EINVAL; 5317d35a00b8SFelix Jia return 1; 5318d35a00b8SFelix Jia } 5319d35a00b8SFelix Jia 5320d35a00b8SFelix Jia static int check_stable_privacy(struct inet6_dev *idev, struct net *net, 5321d35a00b8SFelix Jia int mode) 5322d35a00b8SFelix Jia { 5323d35a00b8SFelix Jia if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY && 5324d35a00b8SFelix Jia !idev->cnf.stable_secret.initialized && 5325d35a00b8SFelix Jia !net->ipv6.devconf_dflt->stable_secret.initialized) 5326d35a00b8SFelix Jia return -EINVAL; 5327d35a00b8SFelix Jia return 1; 5328d35a00b8SFelix Jia } 5329d35a00b8SFelix Jia 5330f53adae4SDaniel Borkmann static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla) 5331f53adae4SDaniel Borkmann { 5332f53adae4SDaniel Borkmann int err = -EINVAL; 5333f53adae4SDaniel Borkmann struct inet6_dev *idev = __in6_dev_get(dev); 5334f53adae4SDaniel Borkmann struct nlattr *tb[IFLA_INET6_MAX + 1]; 5335f53adae4SDaniel Borkmann 5336f53adae4SDaniel Borkmann if (!idev) 5337f53adae4SDaniel Borkmann return -EAFNOSUPPORT; 5338f53adae4SDaniel Borkmann 5339fceb6435SJohannes Berg if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL, NULL) < 0) 5340f53adae4SDaniel Borkmann BUG(); 5341f53adae4SDaniel Borkmann 5342bc91b0f0SJiri Pirko if (tb[IFLA_INET6_TOKEN]) { 5343f53adae4SDaniel Borkmann err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN])); 5344bc91b0f0SJiri Pirko if (err) 5345bc91b0f0SJiri Pirko return err; 5346bc91b0f0SJiri Pirko } 5347bc91b0f0SJiri Pirko 5348bc91b0f0SJiri Pirko if (tb[IFLA_INET6_ADDR_GEN_MODE]) { 5349bc91b0f0SJiri Pirko u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]); 5350bc91b0f0SJiri Pirko 5351d35a00b8SFelix Jia if (check_addr_gen_mode(mode) < 0 || 5352d35a00b8SFelix Jia check_stable_privacy(idev, dev_net(dev), mode) < 0) 5353bc91b0f0SJiri Pirko return -EINVAL; 5354622c81d5SHannes Frederic Sowa 5355d35a00b8SFelix Jia idev->cnf.addr_gen_mode = mode; 5356bc91b0f0SJiri Pirko err = 0; 5357bc91b0f0SJiri Pirko } 5358f53adae4SDaniel Borkmann 5359f53adae4SDaniel Borkmann return err; 5360f53adae4SDaniel Borkmann } 5361f53adae4SDaniel Borkmann 5362b382b191SThomas Graf static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev, 536315e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 5364b382b191SThomas Graf { 5365b382b191SThomas Graf struct net_device *dev = idev->dev; 5366b382b191SThomas Graf struct ifinfomsg *hdr; 5367b382b191SThomas Graf struct nlmsghdr *nlh; 5368b382b191SThomas Graf void *protoinfo; 5369b382b191SThomas Graf 537015e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags); 537163159f29SIan Morris if (!nlh) 5372b382b191SThomas Graf return -EMSGSIZE; 5373b382b191SThomas Graf 5374b382b191SThomas Graf hdr = nlmsg_data(nlh); 5375b382b191SThomas Graf hdr->ifi_family = AF_INET6; 5376b382b191SThomas Graf hdr->__ifi_pad = 0; 5377b382b191SThomas Graf hdr->ifi_type = dev->type; 5378b382b191SThomas Graf hdr->ifi_index = dev->ifindex; 5379b382b191SThomas Graf hdr->ifi_flags = dev_get_flags(dev); 5380b382b191SThomas Graf hdr->ifi_change = 0; 5381b382b191SThomas Graf 5382c78679e8SDavid S. Miller if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 5383c78679e8SDavid S. Miller (dev->addr_len && 5384c78679e8SDavid S. Miller nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 5385c78679e8SDavid S. Miller nla_put_u32(skb, IFLA_MTU, dev->mtu) || 5386a54acb3aSNicolas Dichtel (dev->ifindex != dev_get_iflink(dev) && 53870344338bSAndy Gospodarek nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) || 53880344338bSAndy Gospodarek nla_put_u8(skb, IFLA_OPERSTATE, 53890344338bSAndy Gospodarek netif_running(dev) ? dev->operstate : IF_OPER_DOWN)) 5390c78679e8SDavid S. Miller goto nla_put_failure; 5391b382b191SThomas Graf protoinfo = nla_nest_start(skb, IFLA_PROTINFO); 539263159f29SIan Morris if (!protoinfo) 5393b382b191SThomas Graf goto nla_put_failure; 5394b382b191SThomas Graf 5395d5566fd7SSowmini Varadhan if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0) 5396b382b191SThomas Graf goto nla_put_failure; 5397b382b191SThomas Graf 539804561c1fSThomas Graf nla_nest_end(skb, protoinfo); 5399053c095aSJohannes Berg nlmsg_end(skb, nlh); 5400053c095aSJohannes Berg return 0; 54011da177e4SLinus Torvalds 540204561c1fSThomas Graf nla_put_failure: 540326932566SPatrick McHardy nlmsg_cancel(skb, nlh); 540426932566SPatrick McHardy return -EMSGSIZE; 54051da177e4SLinus Torvalds } 54061da177e4SLinus Torvalds 54071da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 54081da177e4SLinus Torvalds { 54093b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 541084d2697dSEric Dumazet int h, s_h; 5411434a8a58SDavid S. Miller int idx = 0, s_idx; 54121da177e4SLinus Torvalds struct net_device *dev; 54131da177e4SLinus Torvalds struct inet6_dev *idev; 541484d2697dSEric Dumazet struct hlist_head *head; 54151da177e4SLinus Torvalds 541684d2697dSEric Dumazet s_h = cb->args[0]; 541784d2697dSEric Dumazet s_idx = cb->args[1]; 541884d2697dSEric Dumazet 541984d2697dSEric Dumazet rcu_read_lock(); 542084d2697dSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 54217562f876SPavel Emelianov idx = 0; 542284d2697dSEric Dumazet head = &net->dev_index_head[h]; 5423b67bfe0dSSasha Levin hlist_for_each_entry_rcu(dev, head, index_hlist) { 54241da177e4SLinus Torvalds if (idx < s_idx) 54257562f876SPavel Emelianov goto cont; 542684d2697dSEric Dumazet idev = __in6_dev_get(dev); 542784d2697dSEric Dumazet if (!idev) 54287562f876SPavel Emelianov goto cont; 542984d2697dSEric Dumazet if (inet6_fill_ifinfo(skb, idev, 543015e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 543184d2697dSEric Dumazet cb->nlh->nlmsg_seq, 5432053c095aSJohannes Berg RTM_NEWLINK, NLM_F_MULTI) < 0) 543384d2697dSEric Dumazet goto out; 54347562f876SPavel Emelianov cont: 54357562f876SPavel Emelianov idx++; 54361da177e4SLinus Torvalds } 543784d2697dSEric Dumazet } 543884d2697dSEric Dumazet out: 543984d2697dSEric Dumazet rcu_read_unlock(); 544084d2697dSEric Dumazet cb->args[1] = idx; 544184d2697dSEric Dumazet cb->args[0] = h; 54421da177e4SLinus Torvalds 54431da177e4SLinus Torvalds return skb->len; 54441da177e4SLinus Torvalds } 54451da177e4SLinus Torvalds 54461da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev) 54471da177e4SLinus Torvalds { 54481da177e4SLinus Torvalds struct sk_buff *skb; 5449c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 54508d7a76c9SThomas Graf int err = -ENOBUFS; 54511da177e4SLinus Torvalds 5452339bf98fSThomas Graf skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC); 545363159f29SIan Morris if (!skb) 54548d7a76c9SThomas Graf goto errout; 54558d7a76c9SThomas Graf 54568d7a76c9SThomas Graf err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0); 545726932566SPatrick McHardy if (err < 0) { 545826932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */ 545926932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 546026932566SPatrick McHardy kfree_skb(skb); 546126932566SPatrick McHardy goto errout; 546226932566SPatrick McHardy } 54635f75a104SNicolas Dichtel rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC); 54641ce85fe4SPablo Neira Ayuso return; 54658d7a76c9SThomas Graf errout: 54668d7a76c9SThomas Graf if (err < 0) 54675f75a104SNicolas Dichtel rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err); 54681da177e4SLinus Torvalds } 54691da177e4SLinus Torvalds 5470339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void) 5471339bf98fSThomas Graf { 5472339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct prefixmsg)) 5473339bf98fSThomas Graf + nla_total_size(sizeof(struct in6_addr)) 5474339bf98fSThomas Graf + nla_total_size(sizeof(struct prefix_cacheinfo)); 5475339bf98fSThomas Graf } 5476c5396a31SYOSHIFUJI Hideaki 54771da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev, 547815e47304SEric W. Biederman struct prefix_info *pinfo, u32 portid, u32 seq, 5479b6544c0bSJamal Hadi Salim int event, unsigned int flags) 54801da177e4SLinus Torvalds { 54811da177e4SLinus Torvalds struct prefixmsg *pmsg; 54821da177e4SLinus Torvalds struct nlmsghdr *nlh; 54831da177e4SLinus Torvalds struct prefix_cacheinfo ci; 54841da177e4SLinus Torvalds 548515e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags); 548663159f29SIan Morris if (!nlh) 548726932566SPatrick McHardy return -EMSGSIZE; 54886051e2f4SThomas Graf 54896051e2f4SThomas Graf pmsg = nlmsg_data(nlh); 54901da177e4SLinus Torvalds pmsg->prefix_family = AF_INET6; 54918a47077aSPatrick McHardy pmsg->prefix_pad1 = 0; 54928a47077aSPatrick McHardy pmsg->prefix_pad2 = 0; 54931da177e4SLinus Torvalds pmsg->prefix_ifindex = idev->dev->ifindex; 54941da177e4SLinus Torvalds pmsg->prefix_len = pinfo->prefix_len; 54951da177e4SLinus Torvalds pmsg->prefix_type = pinfo->type; 54968a47077aSPatrick McHardy pmsg->prefix_pad3 = 0; 54971da177e4SLinus Torvalds pmsg->prefix_flags = 0; 54981da177e4SLinus Torvalds if (pinfo->onlink) 54991da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_ONLINK; 55001da177e4SLinus Torvalds if (pinfo->autoconf) 55011da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_AUTOCONF; 55021da177e4SLinus Torvalds 5503c78679e8SDavid S. Miller if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix)) 5504c78679e8SDavid S. Miller goto nla_put_failure; 55051da177e4SLinus Torvalds ci.preferred_time = ntohl(pinfo->prefered); 55061da177e4SLinus Torvalds ci.valid_time = ntohl(pinfo->valid); 5507c78679e8SDavid S. Miller if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci)) 5508c78679e8SDavid S. Miller goto nla_put_failure; 5509053c095aSJohannes Berg nlmsg_end(skb, nlh); 5510053c095aSJohannes Berg return 0; 55111da177e4SLinus Torvalds 55126051e2f4SThomas Graf nla_put_failure: 551326932566SPatrick McHardy nlmsg_cancel(skb, nlh); 551426932566SPatrick McHardy return -EMSGSIZE; 55151da177e4SLinus Torvalds } 55161da177e4SLinus Torvalds 55171da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev, 55181da177e4SLinus Torvalds struct prefix_info *pinfo) 55191da177e4SLinus Torvalds { 55201da177e4SLinus Torvalds struct sk_buff *skb; 5521c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 55228c384bfaSThomas Graf int err = -ENOBUFS; 55231da177e4SLinus Torvalds 5524339bf98fSThomas Graf skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC); 552563159f29SIan Morris if (!skb) 55268c384bfaSThomas Graf goto errout; 55278c384bfaSThomas Graf 55288c384bfaSThomas Graf err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0); 552926932566SPatrick McHardy if (err < 0) { 553026932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */ 553126932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 553226932566SPatrick McHardy kfree_skb(skb); 553326932566SPatrick McHardy goto errout; 553426932566SPatrick McHardy } 55351ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC); 55361ce85fe4SPablo Neira Ayuso return; 55378c384bfaSThomas Graf errout: 55388c384bfaSThomas Graf if (err < 0) 55396fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err); 55401da177e4SLinus Torvalds } 55411da177e4SLinus Torvalds 55421da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 55431da177e4SLinus Torvalds { 554463998ac2SNicolas Dichtel struct net *net = dev_net(ifp->idev->dev); 554563998ac2SNicolas Dichtel 5546c15b1ccaSHannes Frederic Sowa if (event) 5547c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 5548c15b1ccaSHannes Frederic Sowa 55491da177e4SLinus Torvalds inet6_ifa_notify(event ? : RTM_NEWADDR, ifp); 55501da177e4SLinus Torvalds 55511da177e4SLinus Torvalds switch (event) { 55521da177e4SLinus Torvalds case RTM_NEWADDR: 555395c385b4SNeil Horman /* 555495c385b4SNeil Horman * If the address was optimistic 555595c385b4SNeil Horman * we inserted the route at the start of 555695c385b4SNeil Horman * our DAD process, so we don't need 555795c385b4SNeil Horman * to do it again 555895c385b4SNeil Horman */ 5559*4e587ea7SWei Wang if (!rcu_access_pointer(ifp->rt->rt6i_node)) 556040e22e8fSThomas Graf ip6_ins_rt(ifp->rt); 55611da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 55621da177e4SLinus Torvalds addrconf_join_anycast(ifp); 55637996c799SCong Wang if (!ipv6_addr_any(&ifp->peer_addr)) 5564caeaba79SNicolas Dichtel addrconf_prefix_route(&ifp->peer_addr, 128, 5565caeaba79SNicolas Dichtel ifp->idev->dev, 0, 0); 55661da177e4SLinus Torvalds break; 55671da177e4SLinus Torvalds case RTM_DELADDR: 55681da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 55691da177e4SLinus Torvalds addrconf_leave_anycast(ifp); 55701da177e4SLinus Torvalds addrconf_leave_solict(ifp->idev, &ifp->addr); 55717996c799SCong Wang if (!ipv6_addr_any(&ifp->peer_addr)) { 5572caeaba79SNicolas Dichtel struct rt6_info *rt; 5573caeaba79SNicolas Dichtel 5574e7478dfcSNicolas Dichtel rt = addrconf_get_prefix_route(&ifp->peer_addr, 128, 5575e7478dfcSNicolas Dichtel ifp->idev->dev, 0, 0); 55768e3d5be7SMartin KaFai Lau if (rt) 55778e3d5be7SMartin KaFai Lau ip6_del_rt(rt); 5578caeaba79SNicolas Dichtel } 557938bd10c4SDavid Ahern if (ifp->rt) { 5580ad65a2f0SWei Wang if (dst_hold_safe(&ifp->rt->dst)) 55818e3d5be7SMartin KaFai Lau ip6_del_rt(ifp->rt); 558238bd10c4SDavid Ahern } 5583705f1c86SHannes Frederic Sowa rt_genid_bump_ipv6(net); 55841da177e4SLinus Torvalds break; 55851da177e4SLinus Torvalds } 558663998ac2SNicolas Dichtel atomic_inc(&net->ipv6.dev_addr_genid); 55871da177e4SLinus Torvalds } 55881da177e4SLinus Torvalds 55891da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 55901da177e4SLinus Torvalds { 55918814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 55921da177e4SLinus Torvalds if (likely(ifp->idev->dead == 0)) 55931da177e4SLinus Torvalds __ipv6_ifa_notify(event, ifp); 55948814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 55951da177e4SLinus Torvalds } 55961da177e4SLinus Torvalds 55971da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 55981da177e4SLinus Torvalds 55991da177e4SLinus Torvalds static 5600fe2c6338SJoe Perches int addrconf_sysctl_forward(struct ctl_table *ctl, int write, 56011da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 56021da177e4SLinus Torvalds { 56031da177e4SLinus Torvalds int *valp = ctl->data; 56041da177e4SLinus Torvalds int val = *valp; 560588af182eSEric W. Biederman loff_t pos = *ppos; 5606fe2c6338SJoe Perches struct ctl_table lctl; 56071da177e4SLinus Torvalds int ret; 56081da177e4SLinus Torvalds 5609013d97e9SFrancesco Ruggeri /* 5610013d97e9SFrancesco Ruggeri * ctl->data points to idev->cnf.forwarding, we should 5611013d97e9SFrancesco Ruggeri * not modify it until we get the rtnl lock. 5612013d97e9SFrancesco Ruggeri */ 5613013d97e9SFrancesco Ruggeri lctl = *ctl; 5614013d97e9SFrancesco Ruggeri lctl.data = &val; 5615013d97e9SFrancesco Ruggeri 5616013d97e9SFrancesco Ruggeri ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 56171da177e4SLinus Torvalds 5618c8fecf22SPavel Emelyanov if (write) 5619b325fddbSStephen Hemminger ret = addrconf_fixup_forwarding(ctl, valp, val); 562088af182eSEric W. Biederman if (ret) 562188af182eSEric W. Biederman *ppos = pos; 56221da177e4SLinus Torvalds return ret; 56231da177e4SLinus Torvalds } 56241da177e4SLinus Torvalds 562577751427SMarcelo Leitner static 562677751427SMarcelo Leitner int addrconf_sysctl_mtu(struct ctl_table *ctl, int write, 562777751427SMarcelo Leitner void __user *buffer, size_t *lenp, loff_t *ppos) 562877751427SMarcelo Leitner { 562977751427SMarcelo Leitner struct inet6_dev *idev = ctl->extra1; 563077751427SMarcelo Leitner int min_mtu = IPV6_MIN_MTU; 563177751427SMarcelo Leitner struct ctl_table lctl; 563277751427SMarcelo Leitner 563377751427SMarcelo Leitner lctl = *ctl; 563477751427SMarcelo Leitner lctl.extra1 = &min_mtu; 563577751427SMarcelo Leitner lctl.extra2 = idev ? &idev->dev->mtu : NULL; 563677751427SMarcelo Leitner 563777751427SMarcelo Leitner return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos); 563877751427SMarcelo Leitner } 563977751427SMarcelo Leitner 564056d417b1SBrian Haley static void dev_disable_change(struct inet6_dev *idev) 564156d417b1SBrian Haley { 564275538c2bSCong Wang struct netdev_notifier_info info; 564375538c2bSCong Wang 564456d417b1SBrian Haley if (!idev || !idev->dev) 564556d417b1SBrian Haley return; 564656d417b1SBrian Haley 564775538c2bSCong Wang netdev_notifier_info_init(&info, idev->dev); 564856d417b1SBrian Haley if (idev->cnf.disable_ipv6) 564975538c2bSCong Wang addrconf_notify(NULL, NETDEV_DOWN, &info); 565056d417b1SBrian Haley else 565175538c2bSCong Wang addrconf_notify(NULL, NETDEV_UP, &info); 565256d417b1SBrian Haley } 565356d417b1SBrian Haley 565456d417b1SBrian Haley static void addrconf_disable_change(struct net *net, __s32 newf) 565556d417b1SBrian Haley { 565656d417b1SBrian Haley struct net_device *dev; 565756d417b1SBrian Haley struct inet6_dev *idev; 565856d417b1SBrian Haley 565903e4deffSKefeng Wang for_each_netdev(net, dev) { 566056d417b1SBrian Haley idev = __in6_dev_get(dev); 566156d417b1SBrian Haley if (idev) { 566256d417b1SBrian Haley int changed = (!idev->cnf.disable_ipv6) ^ (!newf); 566356d417b1SBrian Haley idev->cnf.disable_ipv6 = newf; 566456d417b1SBrian Haley if (changed) 566556d417b1SBrian Haley dev_disable_change(idev); 566656d417b1SBrian Haley } 566756d417b1SBrian Haley } 566856d417b1SBrian Haley } 566956d417b1SBrian Haley 5670013d97e9SFrancesco Ruggeri static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf) 567156d417b1SBrian Haley { 567256d417b1SBrian Haley struct net *net; 5673013d97e9SFrancesco Ruggeri int old; 5674013d97e9SFrancesco Ruggeri 5675013d97e9SFrancesco Ruggeri if (!rtnl_trylock()) 5676013d97e9SFrancesco Ruggeri return restart_syscall(); 567756d417b1SBrian Haley 567856d417b1SBrian Haley net = (struct net *)table->extra2; 5679013d97e9SFrancesco Ruggeri old = *p; 5680013d97e9SFrancesco Ruggeri *p = newf; 568156d417b1SBrian Haley 5682013d97e9SFrancesco Ruggeri if (p == &net->ipv6.devconf_dflt->disable_ipv6) { 5683013d97e9SFrancesco Ruggeri rtnl_unlock(); 568456d417b1SBrian Haley return 0; 568588af182eSEric W. Biederman } 568656d417b1SBrian Haley 568756d417b1SBrian Haley if (p == &net->ipv6.devconf_all->disable_ipv6) { 568856d417b1SBrian Haley net->ipv6.devconf_dflt->disable_ipv6 = newf; 568956d417b1SBrian Haley addrconf_disable_change(net, newf); 5690013d97e9SFrancesco Ruggeri } else if ((!newf) ^ (!old)) 569156d417b1SBrian Haley dev_disable_change((struct inet6_dev *)table->extra1); 569256d417b1SBrian Haley 569356d417b1SBrian Haley rtnl_unlock(); 569456d417b1SBrian Haley return 0; 569556d417b1SBrian Haley } 569656d417b1SBrian Haley 569756d417b1SBrian Haley static 5698fe2c6338SJoe Perches int addrconf_sysctl_disable(struct ctl_table *ctl, int write, 569956d417b1SBrian Haley void __user *buffer, size_t *lenp, loff_t *ppos) 570056d417b1SBrian Haley { 570156d417b1SBrian Haley int *valp = ctl->data; 570256d417b1SBrian Haley int val = *valp; 570388af182eSEric W. Biederman loff_t pos = *ppos; 5704fe2c6338SJoe Perches struct ctl_table lctl; 570556d417b1SBrian Haley int ret; 570656d417b1SBrian Haley 5707013d97e9SFrancesco Ruggeri /* 5708013d97e9SFrancesco Ruggeri * ctl->data points to idev->cnf.disable_ipv6, we should 5709013d97e9SFrancesco Ruggeri * not modify it until we get the rtnl lock. 5710013d97e9SFrancesco Ruggeri */ 5711013d97e9SFrancesco Ruggeri lctl = *ctl; 5712013d97e9SFrancesco Ruggeri lctl.data = &val; 5713013d97e9SFrancesco Ruggeri 5714013d97e9SFrancesco Ruggeri ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 571556d417b1SBrian Haley 571656d417b1SBrian Haley if (write) 571756d417b1SBrian Haley ret = addrconf_disable_ipv6(ctl, valp, val); 571888af182eSEric W. Biederman if (ret) 571988af182eSEric W. Biederman *ppos = pos; 572056d417b1SBrian Haley return ret; 572156d417b1SBrian Haley } 572256d417b1SBrian Haley 5723c92d5491Sstephen hemminger static 5724c92d5491Sstephen hemminger int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write, 5725c92d5491Sstephen hemminger void __user *buffer, size_t *lenp, loff_t *ppos) 5726c92d5491Sstephen hemminger { 5727c92d5491Sstephen hemminger int *valp = ctl->data; 5728c92d5491Sstephen hemminger int ret; 5729c92d5491Sstephen hemminger int old, new; 5730c92d5491Sstephen hemminger 5731c92d5491Sstephen hemminger old = *valp; 5732c92d5491Sstephen hemminger ret = proc_dointvec(ctl, write, buffer, lenp, ppos); 5733c92d5491Sstephen hemminger new = *valp; 5734c92d5491Sstephen hemminger 5735c92d5491Sstephen hemminger if (write && old != new) { 5736c92d5491Sstephen hemminger struct net *net = ctl->extra2; 5737c92d5491Sstephen hemminger 5738c92d5491Sstephen hemminger if (!rtnl_trylock()) 5739c92d5491Sstephen hemminger return restart_syscall(); 5740c92d5491Sstephen hemminger 5741c92d5491Sstephen hemminger if (valp == &net->ipv6.devconf_dflt->proxy_ndp) 574285b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 574385b3daadSDavid Ahern NETCONFA_PROXY_NEIGH, 5744c92d5491Sstephen hemminger NETCONFA_IFINDEX_DEFAULT, 5745c92d5491Sstephen hemminger net->ipv6.devconf_dflt); 5746c92d5491Sstephen hemminger else if (valp == &net->ipv6.devconf_all->proxy_ndp) 574785b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 574885b3daadSDavid Ahern NETCONFA_PROXY_NEIGH, 5749c92d5491Sstephen hemminger NETCONFA_IFINDEX_ALL, 5750c92d5491Sstephen hemminger net->ipv6.devconf_all); 5751c92d5491Sstephen hemminger else { 5752c92d5491Sstephen hemminger struct inet6_dev *idev = ctl->extra1; 5753c92d5491Sstephen hemminger 575485b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 575585b3daadSDavid Ahern NETCONFA_PROXY_NEIGH, 5756c92d5491Sstephen hemminger idev->dev->ifindex, 5757c92d5491Sstephen hemminger &idev->cnf); 5758c92d5491Sstephen hemminger } 5759c92d5491Sstephen hemminger rtnl_unlock(); 5760c92d5491Sstephen hemminger } 5761c92d5491Sstephen hemminger 5762c92d5491Sstephen hemminger return ret; 5763c92d5491Sstephen hemminger } 5764c92d5491Sstephen hemminger 5765d35a00b8SFelix Jia static int addrconf_sysctl_addr_gen_mode(struct ctl_table *ctl, int write, 5766d35a00b8SFelix Jia void __user *buffer, size_t *lenp, 5767d35a00b8SFelix Jia loff_t *ppos) 5768d35a00b8SFelix Jia { 5769d35a00b8SFelix Jia int ret = 0; 5770d35a00b8SFelix Jia int new_val; 5771d35a00b8SFelix Jia struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1; 5772d35a00b8SFelix Jia struct net *net = (struct net *)ctl->extra2; 5773d35a00b8SFelix Jia 57748c171d6cSFelix Jia if (!rtnl_trylock()) 57758c171d6cSFelix Jia return restart_syscall(); 57768c171d6cSFelix Jia 5777d35a00b8SFelix Jia ret = proc_dointvec(ctl, write, buffer, lenp, ppos); 5778d35a00b8SFelix Jia 5779d35a00b8SFelix Jia if (write) { 5780d35a00b8SFelix Jia new_val = *((int *)ctl->data); 5781d35a00b8SFelix Jia 57828c171d6cSFelix Jia if (check_addr_gen_mode(new_val) < 0) { 57838c171d6cSFelix Jia ret = -EINVAL; 57848c171d6cSFelix Jia goto out; 57858c171d6cSFelix Jia } 5786d35a00b8SFelix Jia 5787d35a00b8SFelix Jia /* request for default */ 5788d35a00b8SFelix Jia if (&net->ipv6.devconf_dflt->addr_gen_mode == ctl->data) { 5789d35a00b8SFelix Jia ipv6_devconf_dflt.addr_gen_mode = new_val; 5790d35a00b8SFelix Jia 5791d35a00b8SFelix Jia /* request for individual net device */ 5792d35a00b8SFelix Jia } else { 5793d35a00b8SFelix Jia if (!idev) 57948c171d6cSFelix Jia goto out; 5795d35a00b8SFelix Jia 57968c171d6cSFelix Jia if (check_stable_privacy(idev, net, new_val) < 0) { 57978c171d6cSFelix Jia ret = -EINVAL; 57988c171d6cSFelix Jia goto out; 57998c171d6cSFelix Jia } 5800d35a00b8SFelix Jia 5801d35a00b8SFelix Jia if (idev->cnf.addr_gen_mode != new_val) { 5802d35a00b8SFelix Jia idev->cnf.addr_gen_mode = new_val; 5803d35a00b8SFelix Jia addrconf_dev_config(idev->dev); 58048c171d6cSFelix Jia } 58058c171d6cSFelix Jia } 58068c171d6cSFelix Jia } 58078c171d6cSFelix Jia 58088c171d6cSFelix Jia out: 5809d35a00b8SFelix Jia rtnl_unlock(); 5810d35a00b8SFelix Jia 5811d35a00b8SFelix Jia return ret; 5812d35a00b8SFelix Jia } 5813d35a00b8SFelix Jia 58143d1bec99SHannes Frederic Sowa static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write, 58153d1bec99SHannes Frederic Sowa void __user *buffer, size_t *lenp, 58163d1bec99SHannes Frederic Sowa loff_t *ppos) 58173d1bec99SHannes Frederic Sowa { 58183d1bec99SHannes Frederic Sowa int err; 58193d1bec99SHannes Frederic Sowa struct in6_addr addr; 58203d1bec99SHannes Frederic Sowa char str[IPV6_MAX_STRLEN]; 58213d1bec99SHannes Frederic Sowa struct ctl_table lctl = *ctl; 5822622c81d5SHannes Frederic Sowa struct net *net = ctl->extra2; 58233d1bec99SHannes Frederic Sowa struct ipv6_stable_secret *secret = ctl->data; 58243d1bec99SHannes Frederic Sowa 5825622c81d5SHannes Frederic Sowa if (&net->ipv6.devconf_all->stable_secret == ctl->data) 5826622c81d5SHannes Frederic Sowa return -EIO; 5827622c81d5SHannes Frederic Sowa 58283d1bec99SHannes Frederic Sowa lctl.maxlen = IPV6_MAX_STRLEN; 58293d1bec99SHannes Frederic Sowa lctl.data = str; 58303d1bec99SHannes Frederic Sowa 58313d1bec99SHannes Frederic Sowa if (!rtnl_trylock()) 58323d1bec99SHannes Frederic Sowa return restart_syscall(); 58333d1bec99SHannes Frederic Sowa 58343d1bec99SHannes Frederic Sowa if (!write && !secret->initialized) { 58353d1bec99SHannes Frederic Sowa err = -EIO; 58363d1bec99SHannes Frederic Sowa goto out; 58373d1bec99SHannes Frederic Sowa } 58383d1bec99SHannes Frederic Sowa 58395449a5caSWANG Cong err = snprintf(str, sizeof(str), "%pI6", &secret->secret); 58403d1bec99SHannes Frederic Sowa if (err >= sizeof(str)) { 58413d1bec99SHannes Frederic Sowa err = -EIO; 58423d1bec99SHannes Frederic Sowa goto out; 58433d1bec99SHannes Frederic Sowa } 58443d1bec99SHannes Frederic Sowa 58453d1bec99SHannes Frederic Sowa err = proc_dostring(&lctl, write, buffer, lenp, ppos); 58463d1bec99SHannes Frederic Sowa if (err || !write) 58473d1bec99SHannes Frederic Sowa goto out; 58483d1bec99SHannes Frederic Sowa 58493d1bec99SHannes Frederic Sowa if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) { 58503d1bec99SHannes Frederic Sowa err = -EIO; 58513d1bec99SHannes Frederic Sowa goto out; 58523d1bec99SHannes Frederic Sowa } 58533d1bec99SHannes Frederic Sowa 58543d1bec99SHannes Frederic Sowa secret->initialized = true; 58553d1bec99SHannes Frederic Sowa secret->secret = addr; 58563d1bec99SHannes Frederic Sowa 5857622c81d5SHannes Frederic Sowa if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) { 5858622c81d5SHannes Frederic Sowa struct net_device *dev; 5859622c81d5SHannes Frederic Sowa 5860622c81d5SHannes Frederic Sowa for_each_netdev(net, dev) { 5861622c81d5SHannes Frederic Sowa struct inet6_dev *idev = __in6_dev_get(dev); 5862622c81d5SHannes Frederic Sowa 5863622c81d5SHannes Frederic Sowa if (idev) { 5864d35a00b8SFelix Jia idev->cnf.addr_gen_mode = 5865622c81d5SHannes Frederic Sowa IN6_ADDR_GEN_MODE_STABLE_PRIVACY; 5866622c81d5SHannes Frederic Sowa } 5867622c81d5SHannes Frederic Sowa } 5868622c81d5SHannes Frederic Sowa } else { 5869622c81d5SHannes Frederic Sowa struct inet6_dev *idev = ctl->extra1; 5870622c81d5SHannes Frederic Sowa 5871d35a00b8SFelix Jia idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY; 5872622c81d5SHannes Frederic Sowa } 5873622c81d5SHannes Frederic Sowa 58743d1bec99SHannes Frederic Sowa out: 58753d1bec99SHannes Frederic Sowa rtnl_unlock(); 58763d1bec99SHannes Frederic Sowa 58773d1bec99SHannes Frederic Sowa return err; 58783d1bec99SHannes Frederic Sowa } 5879c92d5491Sstephen hemminger 588035103d11SAndy Gospodarek static 588135103d11SAndy Gospodarek int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl, 588235103d11SAndy Gospodarek int write, 588335103d11SAndy Gospodarek void __user *buffer, 588435103d11SAndy Gospodarek size_t *lenp, 588535103d11SAndy Gospodarek loff_t *ppos) 588635103d11SAndy Gospodarek { 588735103d11SAndy Gospodarek int *valp = ctl->data; 588835103d11SAndy Gospodarek int val = *valp; 588935103d11SAndy Gospodarek loff_t pos = *ppos; 589035103d11SAndy Gospodarek struct ctl_table lctl; 589135103d11SAndy Gospodarek int ret; 589235103d11SAndy Gospodarek 589335103d11SAndy Gospodarek /* ctl->data points to idev->cnf.ignore_routes_when_linkdown 589435103d11SAndy Gospodarek * we should not modify it until we get the rtnl lock. 589535103d11SAndy Gospodarek */ 589635103d11SAndy Gospodarek lctl = *ctl; 589735103d11SAndy Gospodarek lctl.data = &val; 589835103d11SAndy Gospodarek 589935103d11SAndy Gospodarek ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 590035103d11SAndy Gospodarek 590135103d11SAndy Gospodarek if (write) 590235103d11SAndy Gospodarek ret = addrconf_fixup_linkdown(ctl, valp, val); 590335103d11SAndy Gospodarek if (ret) 590435103d11SAndy Gospodarek *ppos = pos; 590535103d11SAndy Gospodarek return ret; 590635103d11SAndy Gospodarek } 590735103d11SAndy Gospodarek 5908df789fe7SDavid Forster static 5909df789fe7SDavid Forster void addrconf_set_nopolicy(struct rt6_info *rt, int action) 5910df789fe7SDavid Forster { 5911df789fe7SDavid Forster if (rt) { 5912df789fe7SDavid Forster if (action) 5913df789fe7SDavid Forster rt->dst.flags |= DST_NOPOLICY; 5914df789fe7SDavid Forster else 5915df789fe7SDavid Forster rt->dst.flags &= ~DST_NOPOLICY; 5916df789fe7SDavid Forster } 5917df789fe7SDavid Forster } 5918df789fe7SDavid Forster 5919df789fe7SDavid Forster static 5920df789fe7SDavid Forster void addrconf_disable_policy_idev(struct inet6_dev *idev, int val) 5921df789fe7SDavid Forster { 5922df789fe7SDavid Forster struct inet6_ifaddr *ifa; 5923df789fe7SDavid Forster 5924df789fe7SDavid Forster read_lock_bh(&idev->lock); 5925df789fe7SDavid Forster list_for_each_entry(ifa, &idev->addr_list, if_list) { 5926df789fe7SDavid Forster spin_lock(&ifa->lock); 5927df789fe7SDavid Forster if (ifa->rt) { 5928df789fe7SDavid Forster struct rt6_info *rt = ifa->rt; 5929df789fe7SDavid Forster struct fib6_table *table = rt->rt6i_table; 5930df789fe7SDavid Forster int cpu; 5931df789fe7SDavid Forster 5932df789fe7SDavid Forster read_lock(&table->tb6_lock); 5933df789fe7SDavid Forster addrconf_set_nopolicy(ifa->rt, val); 5934df789fe7SDavid Forster if (rt->rt6i_pcpu) { 5935df789fe7SDavid Forster for_each_possible_cpu(cpu) { 5936df789fe7SDavid Forster struct rt6_info **rtp; 5937df789fe7SDavid Forster 5938df789fe7SDavid Forster rtp = per_cpu_ptr(rt->rt6i_pcpu, cpu); 5939df789fe7SDavid Forster addrconf_set_nopolicy(*rtp, val); 5940df789fe7SDavid Forster } 5941df789fe7SDavid Forster } 5942df789fe7SDavid Forster read_unlock(&table->tb6_lock); 5943df789fe7SDavid Forster } 5944df789fe7SDavid Forster spin_unlock(&ifa->lock); 5945df789fe7SDavid Forster } 5946df789fe7SDavid Forster read_unlock_bh(&idev->lock); 5947df789fe7SDavid Forster } 5948df789fe7SDavid Forster 5949df789fe7SDavid Forster static 5950df789fe7SDavid Forster int addrconf_disable_policy(struct ctl_table *ctl, int *valp, int val) 5951df789fe7SDavid Forster { 5952df789fe7SDavid Forster struct inet6_dev *idev; 5953df789fe7SDavid Forster struct net *net; 5954df789fe7SDavid Forster 5955df789fe7SDavid Forster if (!rtnl_trylock()) 5956df789fe7SDavid Forster return restart_syscall(); 5957df789fe7SDavid Forster 5958df789fe7SDavid Forster *valp = val; 5959df789fe7SDavid Forster 5960df789fe7SDavid Forster net = (struct net *)ctl->extra2; 5961df789fe7SDavid Forster if (valp == &net->ipv6.devconf_dflt->disable_policy) { 5962df789fe7SDavid Forster rtnl_unlock(); 5963df789fe7SDavid Forster return 0; 5964df789fe7SDavid Forster } 5965df789fe7SDavid Forster 5966df789fe7SDavid Forster if (valp == &net->ipv6.devconf_all->disable_policy) { 5967df789fe7SDavid Forster struct net_device *dev; 5968df789fe7SDavid Forster 5969df789fe7SDavid Forster for_each_netdev(net, dev) { 5970df789fe7SDavid Forster idev = __in6_dev_get(dev); 5971df789fe7SDavid Forster if (idev) 5972df789fe7SDavid Forster addrconf_disable_policy_idev(idev, val); 5973df789fe7SDavid Forster } 5974df789fe7SDavid Forster } else { 5975df789fe7SDavid Forster idev = (struct inet6_dev *)ctl->extra1; 5976df789fe7SDavid Forster addrconf_disable_policy_idev(idev, val); 5977df789fe7SDavid Forster } 5978df789fe7SDavid Forster 5979df789fe7SDavid Forster rtnl_unlock(); 5980df789fe7SDavid Forster return 0; 5981df789fe7SDavid Forster } 5982df789fe7SDavid Forster 5983df789fe7SDavid Forster static 5984df789fe7SDavid Forster int addrconf_sysctl_disable_policy(struct ctl_table *ctl, int write, 5985df789fe7SDavid Forster void __user *buffer, size_t *lenp, 5986df789fe7SDavid Forster loff_t *ppos) 5987df789fe7SDavid Forster { 5988df789fe7SDavid Forster int *valp = ctl->data; 5989df789fe7SDavid Forster int val = *valp; 5990df789fe7SDavid Forster loff_t pos = *ppos; 5991df789fe7SDavid Forster struct ctl_table lctl; 5992df789fe7SDavid Forster int ret; 5993df789fe7SDavid Forster 5994df789fe7SDavid Forster lctl = *ctl; 5995df789fe7SDavid Forster lctl.data = &val; 5996df789fe7SDavid Forster ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 5997df789fe7SDavid Forster 5998df789fe7SDavid Forster if (write && (*valp != val)) 5999df789fe7SDavid Forster ret = addrconf_disable_policy(ctl, valp, val); 6000df789fe7SDavid Forster 6001df789fe7SDavid Forster if (ret) 6002df789fe7SDavid Forster *ppos = pos; 6003df789fe7SDavid Forster 6004df789fe7SDavid Forster return ret; 6005df789fe7SDavid Forster } 6006df789fe7SDavid Forster 6007cb4a4c69SMaciej Żenczykowski static int minus_one = -1; 6008cb9e684eSMaciej Żenczykowski static const int one = 1; 6009cb9e684eSMaciej Żenczykowski static const int two_five_five = 255; 6010cb9e684eSMaciej Żenczykowski 6011607ea7cdSKonstantin Khlebnikov static const struct ctl_table addrconf_sysctl[] = { 60121da177e4SLinus Torvalds { 60131da177e4SLinus Torvalds .procname = "forwarding", 60141da177e4SLinus Torvalds .data = &ipv6_devconf.forwarding, 60151da177e4SLinus Torvalds .maxlen = sizeof(int), 60161da177e4SLinus Torvalds .mode = 0644, 60176d9f239aSAlexey Dobriyan .proc_handler = addrconf_sysctl_forward, 60181da177e4SLinus Torvalds }, 60191da177e4SLinus Torvalds { 60201da177e4SLinus Torvalds .procname = "hop_limit", 60211da177e4SLinus Torvalds .data = &ipv6_devconf.hop_limit, 60221da177e4SLinus Torvalds .maxlen = sizeof(int), 60231da177e4SLinus Torvalds .mode = 0644, 6024cb9e684eSMaciej Żenczykowski .proc_handler = proc_dointvec_minmax, 6025cb9e684eSMaciej Żenczykowski .extra1 = (void *)&one, 6026cb9e684eSMaciej Żenczykowski .extra2 = (void *)&two_five_five, 60271da177e4SLinus Torvalds }, 60281da177e4SLinus Torvalds { 60291da177e4SLinus Torvalds .procname = "mtu", 60301da177e4SLinus Torvalds .data = &ipv6_devconf.mtu6, 60311da177e4SLinus Torvalds .maxlen = sizeof(int), 60321da177e4SLinus Torvalds .mode = 0644, 603377751427SMarcelo Leitner .proc_handler = addrconf_sysctl_mtu, 60341da177e4SLinus Torvalds }, 60351da177e4SLinus Torvalds { 60361da177e4SLinus Torvalds .procname = "accept_ra", 60371da177e4SLinus Torvalds .data = &ipv6_devconf.accept_ra, 60381da177e4SLinus Torvalds .maxlen = sizeof(int), 60391da177e4SLinus Torvalds .mode = 0644, 60406d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 60411da177e4SLinus Torvalds }, 60421da177e4SLinus Torvalds { 60431da177e4SLinus Torvalds .procname = "accept_redirects", 60441da177e4SLinus Torvalds .data = &ipv6_devconf.accept_redirects, 60451da177e4SLinus Torvalds .maxlen = sizeof(int), 60461da177e4SLinus Torvalds .mode = 0644, 60476d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 60481da177e4SLinus Torvalds }, 60491da177e4SLinus Torvalds { 60501da177e4SLinus Torvalds .procname = "autoconf", 60511da177e4SLinus Torvalds .data = &ipv6_devconf.autoconf, 60521da177e4SLinus Torvalds .maxlen = sizeof(int), 60531da177e4SLinus Torvalds .mode = 0644, 60546d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 60551da177e4SLinus Torvalds }, 60561da177e4SLinus Torvalds { 60571da177e4SLinus Torvalds .procname = "dad_transmits", 60581da177e4SLinus Torvalds .data = &ipv6_devconf.dad_transmits, 60591da177e4SLinus Torvalds .maxlen = sizeof(int), 60601da177e4SLinus Torvalds .mode = 0644, 60616d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 60621da177e4SLinus Torvalds }, 60631da177e4SLinus Torvalds { 60641da177e4SLinus Torvalds .procname = "router_solicitations", 60651da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicits, 60661da177e4SLinus Torvalds .maxlen = sizeof(int), 60671da177e4SLinus Torvalds .mode = 0644, 6068cb4a4c69SMaciej Żenczykowski .proc_handler = proc_dointvec_minmax, 6069cb4a4c69SMaciej Żenczykowski .extra1 = &minus_one, 60701da177e4SLinus Torvalds }, 60711da177e4SLinus Torvalds { 60721da177e4SLinus Torvalds .procname = "router_solicitation_interval", 60731da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_interval, 60741da177e4SLinus Torvalds .maxlen = sizeof(int), 60751da177e4SLinus Torvalds .mode = 0644, 60766d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 60771da177e4SLinus Torvalds }, 60781da177e4SLinus Torvalds { 6079bd11f074SMaciej Żenczykowski .procname = "router_solicitation_max_interval", 6080bd11f074SMaciej Żenczykowski .data = &ipv6_devconf.rtr_solicit_max_interval, 6081bd11f074SMaciej Żenczykowski .maxlen = sizeof(int), 6082bd11f074SMaciej Żenczykowski .mode = 0644, 6083bd11f074SMaciej Żenczykowski .proc_handler = proc_dointvec_jiffies, 6084bd11f074SMaciej Żenczykowski }, 6085bd11f074SMaciej Żenczykowski { 60861da177e4SLinus Torvalds .procname = "router_solicitation_delay", 60871da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_delay, 60881da177e4SLinus Torvalds .maxlen = sizeof(int), 60891da177e4SLinus Torvalds .mode = 0644, 60906d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 60911da177e4SLinus Torvalds }, 60921da177e4SLinus Torvalds { 60931da177e4SLinus Torvalds .procname = "force_mld_version", 60941da177e4SLinus Torvalds .data = &ipv6_devconf.force_mld_version, 60951da177e4SLinus Torvalds .maxlen = sizeof(int), 60961da177e4SLinus Torvalds .mode = 0644, 60976d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 60981da177e4SLinus Torvalds }, 6099fc4eba58SHannes Frederic Sowa { 6100fc4eba58SHannes Frederic Sowa .procname = "mldv1_unsolicited_report_interval", 6101fc4eba58SHannes Frederic Sowa .data = 6102fc4eba58SHannes Frederic Sowa &ipv6_devconf.mldv1_unsolicited_report_interval, 6103fc4eba58SHannes Frederic Sowa .maxlen = sizeof(int), 6104fc4eba58SHannes Frederic Sowa .mode = 0644, 6105fc4eba58SHannes Frederic Sowa .proc_handler = proc_dointvec_ms_jiffies, 6106fc4eba58SHannes Frederic Sowa }, 6107fc4eba58SHannes Frederic Sowa { 6108fc4eba58SHannes Frederic Sowa .procname = "mldv2_unsolicited_report_interval", 6109fc4eba58SHannes Frederic Sowa .data = 6110fc4eba58SHannes Frederic Sowa &ipv6_devconf.mldv2_unsolicited_report_interval, 6111fc4eba58SHannes Frederic Sowa .maxlen = sizeof(int), 6112fc4eba58SHannes Frederic Sowa .mode = 0644, 6113fc4eba58SHannes Frederic Sowa .proc_handler = proc_dointvec_ms_jiffies, 6114fc4eba58SHannes Frederic Sowa }, 61151da177e4SLinus Torvalds { 61161da177e4SLinus Torvalds .procname = "use_tempaddr", 61171da177e4SLinus Torvalds .data = &ipv6_devconf.use_tempaddr, 61181da177e4SLinus Torvalds .maxlen = sizeof(int), 61191da177e4SLinus Torvalds .mode = 0644, 61206d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 61211da177e4SLinus Torvalds }, 61221da177e4SLinus Torvalds { 61231da177e4SLinus Torvalds .procname = "temp_valid_lft", 61241da177e4SLinus Torvalds .data = &ipv6_devconf.temp_valid_lft, 61251da177e4SLinus Torvalds .maxlen = sizeof(int), 61261da177e4SLinus Torvalds .mode = 0644, 61276d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 61281da177e4SLinus Torvalds }, 61291da177e4SLinus Torvalds { 61301da177e4SLinus Torvalds .procname = "temp_prefered_lft", 61311da177e4SLinus Torvalds .data = &ipv6_devconf.temp_prefered_lft, 61321da177e4SLinus Torvalds .maxlen = sizeof(int), 61331da177e4SLinus Torvalds .mode = 0644, 61346d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 61351da177e4SLinus Torvalds }, 61361da177e4SLinus Torvalds { 61371da177e4SLinus Torvalds .procname = "regen_max_retry", 61381da177e4SLinus Torvalds .data = &ipv6_devconf.regen_max_retry, 61391da177e4SLinus Torvalds .maxlen = sizeof(int), 61401da177e4SLinus Torvalds .mode = 0644, 61416d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 61421da177e4SLinus Torvalds }, 61431da177e4SLinus Torvalds { 61441da177e4SLinus Torvalds .procname = "max_desync_factor", 61451da177e4SLinus Torvalds .data = &ipv6_devconf.max_desync_factor, 61461da177e4SLinus Torvalds .maxlen = sizeof(int), 61471da177e4SLinus Torvalds .mode = 0644, 61486d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 61491da177e4SLinus Torvalds }, 61501da177e4SLinus Torvalds { 61511da177e4SLinus Torvalds .procname = "max_addresses", 61521da177e4SLinus Torvalds .data = &ipv6_devconf.max_addresses, 61531da177e4SLinus Torvalds .maxlen = sizeof(int), 61541da177e4SLinus Torvalds .mode = 0644, 61556d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 61561da177e4SLinus Torvalds }, 61571da177e4SLinus Torvalds { 615865f5c7c1SYOSHIFUJI Hideaki .procname = "accept_ra_defrtr", 615965f5c7c1SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_defrtr, 616065f5c7c1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 616165f5c7c1SYOSHIFUJI Hideaki .mode = 0644, 61626d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 616365f5c7c1SYOSHIFUJI Hideaki }, 616465f5c7c1SYOSHIFUJI Hideaki { 61658013d1d7SHangbin Liu .procname = "accept_ra_min_hop_limit", 61668013d1d7SHangbin Liu .data = &ipv6_devconf.accept_ra_min_hop_limit, 61678013d1d7SHangbin Liu .maxlen = sizeof(int), 61688013d1d7SHangbin Liu .mode = 0644, 61698013d1d7SHangbin Liu .proc_handler = proc_dointvec, 61708013d1d7SHangbin Liu }, 61718013d1d7SHangbin Liu { 6172c4fd30ebSYOSHIFUJI Hideaki .procname = "accept_ra_pinfo", 6173c4fd30ebSYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_pinfo, 6174c4fd30ebSYOSHIFUJI Hideaki .maxlen = sizeof(int), 6175c4fd30ebSYOSHIFUJI Hideaki .mode = 0644, 61766d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 6177c4fd30ebSYOSHIFUJI Hideaki }, 6178930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 6179930d6ff2SYOSHIFUJI Hideaki { 6180930d6ff2SYOSHIFUJI Hideaki .procname = "accept_ra_rtr_pref", 6181930d6ff2SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rtr_pref, 6182930d6ff2SYOSHIFUJI Hideaki .maxlen = sizeof(int), 6183930d6ff2SYOSHIFUJI Hideaki .mode = 0644, 61846d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 6185930d6ff2SYOSHIFUJI Hideaki }, 618652e16356SYOSHIFUJI Hideaki { 618752e16356SYOSHIFUJI Hideaki .procname = "router_probe_interval", 618852e16356SYOSHIFUJI Hideaki .data = &ipv6_devconf.rtr_probe_interval, 618952e16356SYOSHIFUJI Hideaki .maxlen = sizeof(int), 619052e16356SYOSHIFUJI Hideaki .mode = 0644, 61916d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 619252e16356SYOSHIFUJI Hideaki }, 6193fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 619409c884d4SYOSHIFUJI Hideaki { 6195bbea124bSJoel Scherpelz .procname = "accept_ra_rt_info_min_plen", 6196bbea124bSJoel Scherpelz .data = &ipv6_devconf.accept_ra_rt_info_min_plen, 6197bbea124bSJoel Scherpelz .maxlen = sizeof(int), 6198bbea124bSJoel Scherpelz .mode = 0644, 6199bbea124bSJoel Scherpelz .proc_handler = proc_dointvec, 6200bbea124bSJoel Scherpelz }, 6201bbea124bSJoel Scherpelz { 620209c884d4SYOSHIFUJI Hideaki .procname = "accept_ra_rt_info_max_plen", 620309c884d4SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rt_info_max_plen, 620409c884d4SYOSHIFUJI Hideaki .maxlen = sizeof(int), 620509c884d4SYOSHIFUJI Hideaki .mode = 0644, 62066d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 620709c884d4SYOSHIFUJI Hideaki }, 620809c884d4SYOSHIFUJI Hideaki #endif 6209930d6ff2SYOSHIFUJI Hideaki #endif 6210c4fd30ebSYOSHIFUJI Hideaki { 6211fbea49e1SYOSHIFUJI Hideaki .procname = "proxy_ndp", 6212fbea49e1SYOSHIFUJI Hideaki .data = &ipv6_devconf.proxy_ndp, 6213fbea49e1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 6214fbea49e1SYOSHIFUJI Hideaki .mode = 0644, 6215c92d5491Sstephen hemminger .proc_handler = addrconf_sysctl_proxy_ndp, 6216fbea49e1SYOSHIFUJI Hideaki }, 6217fbea49e1SYOSHIFUJI Hideaki { 62180bcbc926SYOSHIFUJI Hideaki .procname = "accept_source_route", 62190bcbc926SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_source_route, 62200bcbc926SYOSHIFUJI Hideaki .maxlen = sizeof(int), 62210bcbc926SYOSHIFUJI Hideaki .mode = 0644, 62226d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 62230bcbc926SYOSHIFUJI Hideaki }, 622495c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 622595c385b4SNeil Horman { 622695c385b4SNeil Horman .procname = "optimistic_dad", 622795c385b4SNeil Horman .data = &ipv6_devconf.optimistic_dad, 622895c385b4SNeil Horman .maxlen = sizeof(int), 622995c385b4SNeil Horman .mode = 0644, 62306d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 623195c385b4SNeil Horman }, 62327fd2561eSErik Kline { 62337fd2561eSErik Kline .procname = "use_optimistic", 62347fd2561eSErik Kline .data = &ipv6_devconf.use_optimistic, 62357fd2561eSErik Kline .maxlen = sizeof(int), 62367fd2561eSErik Kline .mode = 0644, 62377fd2561eSErik Kline .proc_handler = proc_dointvec, 62387fd2561eSErik Kline }, 623995c385b4SNeil Horman #endif 62407bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 62417bc570c8SYOSHIFUJI Hideaki { 62427bc570c8SYOSHIFUJI Hideaki .procname = "mc_forwarding", 62437bc570c8SYOSHIFUJI Hideaki .data = &ipv6_devconf.mc_forwarding, 62447bc570c8SYOSHIFUJI Hideaki .maxlen = sizeof(int), 6245a4e6db07SDavid S. Miller .mode = 0444, 62466d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 62477bc570c8SYOSHIFUJI Hideaki }, 62487bc570c8SYOSHIFUJI Hideaki #endif 62490bcbc926SYOSHIFUJI Hideaki { 6250778d80beSYOSHIFUJI Hideaki .procname = "disable_ipv6", 6251778d80beSYOSHIFUJI Hideaki .data = &ipv6_devconf.disable_ipv6, 6252778d80beSYOSHIFUJI Hideaki .maxlen = sizeof(int), 6253778d80beSYOSHIFUJI Hideaki .mode = 0644, 625456d417b1SBrian Haley .proc_handler = addrconf_sysctl_disable, 6255778d80beSYOSHIFUJI Hideaki }, 6256778d80beSYOSHIFUJI Hideaki { 62571b34be74SYOSHIFUJI Hideaki .procname = "accept_dad", 62581b34be74SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_dad, 62591b34be74SYOSHIFUJI Hideaki .maxlen = sizeof(int), 62601b34be74SYOSHIFUJI Hideaki .mode = 0644, 62616d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 62621b34be74SYOSHIFUJI Hideaki }, 62631b34be74SYOSHIFUJI Hideaki { 6264f7734fdfSOctavian Purdila .procname = "force_tllao", 6265f7734fdfSOctavian Purdila .data = &ipv6_devconf.force_tllao, 6266f7734fdfSOctavian Purdila .maxlen = sizeof(int), 6267f7734fdfSOctavian Purdila .mode = 0644, 6268f7734fdfSOctavian Purdila .proc_handler = proc_dointvec 6269f7734fdfSOctavian Purdila }, 6270f7734fdfSOctavian Purdila { 62715cb04436SHannes Frederic Sowa .procname = "ndisc_notify", 62725cb04436SHannes Frederic Sowa .data = &ipv6_devconf.ndisc_notify, 62735cb04436SHannes Frederic Sowa .maxlen = sizeof(int), 62745cb04436SHannes Frederic Sowa .mode = 0644, 62755cb04436SHannes Frederic Sowa .proc_handler = proc_dointvec 62765cb04436SHannes Frederic Sowa }, 62775cb04436SHannes Frederic Sowa { 6278b800c3b9SHannes Frederic Sowa .procname = "suppress_frag_ndisc", 6279b800c3b9SHannes Frederic Sowa .data = &ipv6_devconf.suppress_frag_ndisc, 6280b800c3b9SHannes Frederic Sowa .maxlen = sizeof(int), 6281b800c3b9SHannes Frederic Sowa .mode = 0644, 6282b800c3b9SHannes Frederic Sowa .proc_handler = proc_dointvec 6283b800c3b9SHannes Frederic Sowa }, 6284b800c3b9SHannes Frederic Sowa { 6285d9333196SBen Greear .procname = "accept_ra_from_local", 6286d9333196SBen Greear .data = &ipv6_devconf.accept_ra_from_local, 6287d9333196SBen Greear .maxlen = sizeof(int), 6288d9333196SBen Greear .mode = 0644, 6289d9333196SBen Greear .proc_handler = proc_dointvec, 6290d9333196SBen Greear }, 6291d9333196SBen Greear { 6292c2943f14SHarout Hedeshian .procname = "accept_ra_mtu", 6293c2943f14SHarout Hedeshian .data = &ipv6_devconf.accept_ra_mtu, 6294c2943f14SHarout Hedeshian .maxlen = sizeof(int), 6295c2943f14SHarout Hedeshian .mode = 0644, 6296c2943f14SHarout Hedeshian .proc_handler = proc_dointvec, 6297c2943f14SHarout Hedeshian }, 6298c2943f14SHarout Hedeshian { 62993d1bec99SHannes Frederic Sowa .procname = "stable_secret", 63003d1bec99SHannes Frederic Sowa .data = &ipv6_devconf.stable_secret, 63013d1bec99SHannes Frederic Sowa .maxlen = IPV6_MAX_STRLEN, 63023d1bec99SHannes Frederic Sowa .mode = 0600, 63033d1bec99SHannes Frederic Sowa .proc_handler = addrconf_sysctl_stable_secret, 63043d1bec99SHannes Frederic Sowa }, 63053d1bec99SHannes Frederic Sowa { 63063985e8a3SErik Kline .procname = "use_oif_addrs_only", 63073985e8a3SErik Kline .data = &ipv6_devconf.use_oif_addrs_only, 63083985e8a3SErik Kline .maxlen = sizeof(int), 63093985e8a3SErik Kline .mode = 0644, 63103985e8a3SErik Kline .proc_handler = proc_dointvec, 631135103d11SAndy Gospodarek }, 631235103d11SAndy Gospodarek { 631335103d11SAndy Gospodarek .procname = "ignore_routes_with_linkdown", 631435103d11SAndy Gospodarek .data = &ipv6_devconf.ignore_routes_with_linkdown, 631535103d11SAndy Gospodarek .maxlen = sizeof(int), 631635103d11SAndy Gospodarek .mode = 0644, 631735103d11SAndy Gospodarek .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown, 63183985e8a3SErik Kline }, 63193985e8a3SErik Kline { 6320abbc3043SJohannes Berg .procname = "drop_unicast_in_l2_multicast", 6321abbc3043SJohannes Berg .data = &ipv6_devconf.drop_unicast_in_l2_multicast, 6322abbc3043SJohannes Berg .maxlen = sizeof(int), 6323abbc3043SJohannes Berg .mode = 0644, 6324abbc3043SJohannes Berg .proc_handler = proc_dointvec, 6325abbc3043SJohannes Berg }, 6326abbc3043SJohannes Berg { 63277a02bf89SJohannes Berg .procname = "drop_unsolicited_na", 63287a02bf89SJohannes Berg .data = &ipv6_devconf.drop_unsolicited_na, 63297a02bf89SJohannes Berg .maxlen = sizeof(int), 63307a02bf89SJohannes Berg .mode = 0644, 63317a02bf89SJohannes Berg .proc_handler = proc_dointvec, 63327a02bf89SJohannes Berg }, 63337a02bf89SJohannes Berg { 6334f1705ec1SDavid Ahern .procname = "keep_addr_on_down", 6335f1705ec1SDavid Ahern .data = &ipv6_devconf.keep_addr_on_down, 6336f1705ec1SDavid Ahern .maxlen = sizeof(int), 6337f1705ec1SDavid Ahern .mode = 0644, 6338f1705ec1SDavid Ahern .proc_handler = proc_dointvec, 6339f1705ec1SDavid Ahern 6340f1705ec1SDavid Ahern }, 6341f1705ec1SDavid Ahern { 63421ababebaSDavid Lebrun .procname = "seg6_enabled", 63431ababebaSDavid Lebrun .data = &ipv6_devconf.seg6_enabled, 63441ababebaSDavid Lebrun .maxlen = sizeof(int), 63451ababebaSDavid Lebrun .mode = 0644, 63461ababebaSDavid Lebrun .proc_handler = proc_dointvec, 63471ababebaSDavid Lebrun }, 6348bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC 6349bf355b8dSDavid Lebrun { 6350bf355b8dSDavid Lebrun .procname = "seg6_require_hmac", 6351bf355b8dSDavid Lebrun .data = &ipv6_devconf.seg6_require_hmac, 6352bf355b8dSDavid Lebrun .maxlen = sizeof(int), 6353bf355b8dSDavid Lebrun .mode = 0644, 6354bf355b8dSDavid Lebrun .proc_handler = proc_dointvec, 6355bf355b8dSDavid Lebrun }, 6356bf355b8dSDavid Lebrun #endif 63571ababebaSDavid Lebrun { 6358adc176c5SErik Nordmark .procname = "enhanced_dad", 6359adc176c5SErik Nordmark .data = &ipv6_devconf.enhanced_dad, 6360adc176c5SErik Nordmark .maxlen = sizeof(int), 6361adc176c5SErik Nordmark .mode = 0644, 6362adc176c5SErik Nordmark .proc_handler = proc_dointvec, 6363adc176c5SErik Nordmark }, 6364adc176c5SErik Nordmark { 6365d35a00b8SFelix Jia .procname = "addr_gen_mode", 6366d35a00b8SFelix Jia .data = &ipv6_devconf.addr_gen_mode, 6367d35a00b8SFelix Jia .maxlen = sizeof(int), 6368d35a00b8SFelix Jia .mode = 0644, 6369d35a00b8SFelix Jia .proc_handler = addrconf_sysctl_addr_gen_mode, 6370d35a00b8SFelix Jia }, 6371d35a00b8SFelix Jia { 6372df789fe7SDavid Forster .procname = "disable_policy", 6373df789fe7SDavid Forster .data = &ipv6_devconf.disable_policy, 6374df789fe7SDavid Forster .maxlen = sizeof(int), 6375df789fe7SDavid Forster .mode = 0644, 6376df789fe7SDavid Forster .proc_handler = addrconf_sysctl_disable_policy, 6377df789fe7SDavid Forster }, 6378df789fe7SDavid Forster { 6379f8572d8fSEric W. Biederman /* sentinel */ 63801da177e4SLinus Torvalds } 63811da177e4SLinus Torvalds }; 63821da177e4SLinus Torvalds 6383bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name, 6384f8572d8fSEric W. Biederman struct inet6_dev *idev, struct ipv6_devconf *p) 63851da177e4SLinus Torvalds { 638629c994e3SNicolas Dichtel int i, ifindex; 6387607ea7cdSKonstantin Khlebnikov struct ctl_table *table; 63886105e293SEric W. Biederman char path[sizeof("net/ipv6/conf/") + IFNAMSIZ]; 63891dab6222SPavel Emelyanov 6390607ea7cdSKonstantin Khlebnikov table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL); 6391607ea7cdSKonstantin Khlebnikov if (!table) 6392f68635e6SPavel Emelyanov goto out; 6393f68635e6SPavel Emelyanov 6394607ea7cdSKonstantin Khlebnikov for (i = 0; table[i].data; i++) { 6395607ea7cdSKonstantin Khlebnikov table[i].data += (char *)p - (char *)&ipv6_devconf; 6396cb9e684eSMaciej Żenczykowski /* If one of these is already set, then it is not safe to 6397cb9e684eSMaciej Żenczykowski * overwrite either of them: this makes proc_dointvec_minmax 6398cb9e684eSMaciej Żenczykowski * usable. 6399cb9e684eSMaciej Żenczykowski */ 6400cb9e684eSMaciej Żenczykowski if (!table[i].extra1 && !table[i].extra2) { 6401607ea7cdSKonstantin Khlebnikov table[i].extra1 = idev; /* embedded; no ref */ 6402607ea7cdSKonstantin Khlebnikov table[i].extra2 = net; 64031da177e4SLinus Torvalds } 6404cb9e684eSMaciej Żenczykowski } 64051da177e4SLinus Torvalds 64066105e293SEric W. Biederman snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name); 64071da177e4SLinus Torvalds 6408607ea7cdSKonstantin Khlebnikov p->sysctl_header = register_net_sysctl(net, path, table); 6409607ea7cdSKonstantin Khlebnikov if (!p->sysctl_header) 64106105e293SEric W. Biederman goto free; 6411f68635e6SPavel Emelyanov 641229c994e3SNicolas Dichtel if (!strcmp(dev_name, "all")) 641329c994e3SNicolas Dichtel ifindex = NETCONFA_IFINDEX_ALL; 641429c994e3SNicolas Dichtel else if (!strcmp(dev_name, "default")) 641529c994e3SNicolas Dichtel ifindex = NETCONFA_IFINDEX_DEFAULT; 641629c994e3SNicolas Dichtel else 641729c994e3SNicolas Dichtel ifindex = idev->dev->ifindex; 641885b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL, 641985b3daadSDavid Ahern ifindex, p); 642095897312SPavel Emelyanov return 0; 64211da177e4SLinus Torvalds 64221da177e4SLinus Torvalds free: 6423607ea7cdSKonstantin Khlebnikov kfree(table); 6424f68635e6SPavel Emelyanov out: 642595897312SPavel Emelyanov return -ENOBUFS; 64261da177e4SLinus Torvalds } 64271da177e4SLinus Torvalds 642823452170SDavid Ahern static void __addrconf_sysctl_unregister(struct net *net, 642923452170SDavid Ahern struct ipv6_devconf *p, int ifindex) 6430f52295a9SPavel Emelyanov { 6431607ea7cdSKonstantin Khlebnikov struct ctl_table *table; 6432f52295a9SPavel Emelyanov 6433607ea7cdSKonstantin Khlebnikov if (!p->sysctl_header) 6434408c4768SPavel Emelyanov return; 6435408c4768SPavel Emelyanov 6436607ea7cdSKonstantin Khlebnikov table = p->sysctl_header->ctl_table_arg; 6437607ea7cdSKonstantin Khlebnikov unregister_net_sysctl_table(p->sysctl_header); 6438607ea7cdSKonstantin Khlebnikov p->sysctl_header = NULL; 6439607ea7cdSKonstantin Khlebnikov kfree(table); 644023452170SDavid Ahern 644123452170SDavid Ahern inet6_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL); 64421da177e4SLinus Torvalds } 6443408c4768SPavel Emelyanov 6444a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev) 6445408c4768SPavel Emelyanov { 6446a317a2f1SWANG Cong int err; 6447a317a2f1SWANG Cong 6448a317a2f1SWANG Cong if (!sysctl_dev_name_is_allowed(idev->dev->name)) 6449a317a2f1SWANG Cong return -EINVAL; 6450a317a2f1SWANG Cong 6451a317a2f1SWANG Cong err = neigh_sysctl_register(idev->dev, idev->nd_parms, 6452f8572d8fSEric W. Biederman &ndisc_ifinfo_sysctl_change); 6453a317a2f1SWANG Cong if (err) 6454a317a2f1SWANG Cong return err; 6455a317a2f1SWANG Cong err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name, 6456f8572d8fSEric W. Biederman idev, &idev->cnf); 6457a317a2f1SWANG Cong if (err) 6458a317a2f1SWANG Cong neigh_sysctl_unregister(idev->nd_parms); 6459a317a2f1SWANG Cong 6460a317a2f1SWANG Cong return err; 6461408c4768SPavel Emelyanov } 6462408c4768SPavel Emelyanov 6463408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev) 6464408c4768SPavel Emelyanov { 646523452170SDavid Ahern __addrconf_sysctl_unregister(dev_net(idev->dev), &idev->cnf, 646623452170SDavid Ahern idev->dev->ifindex); 6467408c4768SPavel Emelyanov neigh_sysctl_unregister(idev->nd_parms); 64681da177e4SLinus Torvalds } 64691da177e4SLinus Torvalds 64701da177e4SLinus Torvalds 64711da177e4SLinus Torvalds #endif 64721da177e4SLinus Torvalds 64732c8c1e72SAlexey Dobriyan static int __net_init addrconf_init_net(struct net *net) 6474e0da5a48SPavel Emelyanov { 6475a79ca223SHong Zhiguo int err = -ENOMEM; 6476e0da5a48SPavel Emelyanov struct ipv6_devconf *all, *dflt; 6477e0da5a48SPavel Emelyanov 6478a79ca223SHong Zhiguo all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL); 647963159f29SIan Morris if (!all) 6480e0da5a48SPavel Emelyanov goto err_alloc_all; 6481e0da5a48SPavel Emelyanov 6482a79ca223SHong Zhiguo dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL); 648363159f29SIan Morris if (!dflt) 6484e0da5a48SPavel Emelyanov goto err_alloc_dflt; 6485a79ca223SHong Zhiguo 648656d417b1SBrian Haley /* these will be inherited by all namespaces */ 648756d417b1SBrian Haley dflt->autoconf = ipv6_defaults.autoconf; 648856d417b1SBrian Haley dflt->disable_ipv6 = ipv6_defaults.disable_ipv6; 6489e0da5a48SPavel Emelyanov 64903d1bec99SHannes Frederic Sowa dflt->stable_secret.initialized = false; 64913d1bec99SHannes Frederic Sowa all->stable_secret.initialized = false; 64923d1bec99SHannes Frederic Sowa 6493e0da5a48SPavel Emelyanov net->ipv6.devconf_all = all; 6494e0da5a48SPavel Emelyanov net->ipv6.devconf_dflt = dflt; 6495e0da5a48SPavel Emelyanov 6496e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 6497f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "all", NULL, all); 6498e0da5a48SPavel Emelyanov if (err < 0) 6499e0da5a48SPavel Emelyanov goto err_reg_all; 6500e0da5a48SPavel Emelyanov 6501f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "default", NULL, dflt); 6502e0da5a48SPavel Emelyanov if (err < 0) 6503e0da5a48SPavel Emelyanov goto err_reg_dflt; 6504e0da5a48SPavel Emelyanov #endif 6505e0da5a48SPavel Emelyanov return 0; 6506e0da5a48SPavel Emelyanov 6507e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 6508e0da5a48SPavel Emelyanov err_reg_dflt: 650923452170SDavid Ahern __addrconf_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL); 6510e0da5a48SPavel Emelyanov err_reg_all: 6511e0da5a48SPavel Emelyanov kfree(dflt); 6512e0da5a48SPavel Emelyanov #endif 6513e0da5a48SPavel Emelyanov err_alloc_dflt: 6514e0da5a48SPavel Emelyanov kfree(all); 6515e0da5a48SPavel Emelyanov err_alloc_all: 6516e0da5a48SPavel Emelyanov return err; 6517e0da5a48SPavel Emelyanov } 6518e0da5a48SPavel Emelyanov 65192c8c1e72SAlexey Dobriyan static void __net_exit addrconf_exit_net(struct net *net) 6520e0da5a48SPavel Emelyanov { 6521e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 652223452170SDavid Ahern __addrconf_sysctl_unregister(net, net->ipv6.devconf_dflt, 652323452170SDavid Ahern NETCONFA_IFINDEX_DEFAULT); 652423452170SDavid Ahern __addrconf_sysctl_unregister(net, net->ipv6.devconf_all, 652523452170SDavid Ahern NETCONFA_IFINDEX_ALL); 6526e0da5a48SPavel Emelyanov #endif 6527e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_dflt); 6528e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_all); 6529e0da5a48SPavel Emelyanov } 6530e0da5a48SPavel Emelyanov 6531e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = { 6532e0da5a48SPavel Emelyanov .init = addrconf_init_net, 6533e0da5a48SPavel Emelyanov .exit = addrconf_exit_net, 6534e0da5a48SPavel Emelyanov }; 6535e0da5a48SPavel Emelyanov 6536207895fdSDaniel Borkmann static struct rtnl_af_ops inet6_ops __read_mostly = { 6537b382b191SThomas Graf .family = AF_INET6, 6538b382b191SThomas Graf .fill_link_af = inet6_fill_link_af, 6539b382b191SThomas Graf .get_link_af_size = inet6_get_link_af_size, 654011b1f828SDaniel Borkmann .validate_link_af = inet6_validate_link_af, 6541f53adae4SDaniel Borkmann .set_link_af = inet6_set_link_af, 6542b382b191SThomas Graf }; 6543b382b191SThomas Graf 65441da177e4SLinus Torvalds /* 65451da177e4SLinus Torvalds * Init / cleanup code 65461da177e4SLinus Torvalds */ 65471da177e4SLinus Torvalds 65481da177e4SLinus Torvalds int __init addrconf_init(void) 65491da177e4SLinus Torvalds { 6550a317a2f1SWANG Cong struct inet6_dev *idev; 6551c2e21293Sstephen hemminger int i, err; 65522a8cc6c8SYOSHIFUJI Hideaki 6553e21e8467SStephen Hemminger err = ipv6_addr_label_init(); 6554e21e8467SStephen Hemminger if (err < 0) { 6555f3213831SJoe Perches pr_crit("%s: cannot initialize default policy table: %d\n", 6556f3213831SJoe Perches __func__, err); 65572cc6d2bfSNeil Horman goto out; 65582a8cc6c8SYOSHIFUJI Hideaki } 65591da177e4SLinus Torvalds 65602cc6d2bfSNeil Horman err = register_pernet_subsys(&addrconf_ops); 65612cc6d2bfSNeil Horman if (err < 0) 65622cc6d2bfSNeil Horman goto out_addrlabel; 6563e0da5a48SPavel Emelyanov 6564c15b1ccaSHannes Frederic Sowa addrconf_wq = create_workqueue("ipv6_addrconf"); 6565c15b1ccaSHannes Frederic Sowa if (!addrconf_wq) { 6566c15b1ccaSHannes Frederic Sowa err = -ENOMEM; 6567c15b1ccaSHannes Frederic Sowa goto out_nowq; 6568c15b1ccaSHannes Frederic Sowa } 6569c15b1ccaSHannes Frederic Sowa 65701da177e4SLinus Torvalds /* The addrconf netdev notifier requires that loopback_dev 65711da177e4SLinus Torvalds * has it's ipv6 private information allocated and setup 65721da177e4SLinus Torvalds * before it can bring up and give link-local addresses 65731da177e4SLinus Torvalds * to other devices which are up. 65741da177e4SLinus Torvalds * 65751da177e4SLinus Torvalds * Unfortunately, loopback_dev is not necessarily the first 65761da177e4SLinus Torvalds * entry in the global dev_base list of net devices. In fact, 65771da177e4SLinus Torvalds * it is likely to be the very last entry on that list. 65781da177e4SLinus Torvalds * So this causes the notifier registry below to try and 65791da177e4SLinus Torvalds * give link-local addresses to all devices besides loopback_dev 65801da177e4SLinus Torvalds * first, then loopback_dev, which cases all the non-loopback_dev 65811da177e4SLinus Torvalds * devices to fail to get a link-local address. 65821da177e4SLinus Torvalds * 65831da177e4SLinus Torvalds * So, as a temporary fix, allocate the ipv6 structure for 65841da177e4SLinus Torvalds * loopback_dev first by hand. 65851da177e4SLinus Torvalds * Longer term, all of the dependencies ipv6 has upon the loopback 65861da177e4SLinus Torvalds * device and it being up should be removed. 65871da177e4SLinus Torvalds */ 65881da177e4SLinus Torvalds rtnl_lock(); 6589a317a2f1SWANG Cong idev = ipv6_add_dev(init_net.loopback_dev); 65901da177e4SLinus Torvalds rtnl_unlock(); 6591a317a2f1SWANG Cong if (IS_ERR(idev)) { 6592a317a2f1SWANG Cong err = PTR_ERR(idev); 6593e0da5a48SPavel Emelyanov goto errlo; 6594a317a2f1SWANG Cong } 65951da177e4SLinus Torvalds 65962f460933SWANG Cong ip6_route_init_special_entries(); 65972f460933SWANG Cong 6598c2e21293Sstephen hemminger for (i = 0; i < IN6_ADDR_HSIZE; i++) 6599c2e21293Sstephen hemminger INIT_HLIST_HEAD(&inet6_addr_lst[i]); 6600c2e21293Sstephen hemminger 66011da177e4SLinus Torvalds register_netdevice_notifier(&ipv6_dev_notf); 66021da177e4SLinus Torvalds 6603c15b1ccaSHannes Frederic Sowa addrconf_verify(); 6604c127ea2cSThomas Graf 66053678a9d8Sstephen hemminger rtnl_af_register(&inet6_ops); 6606b382b191SThomas Graf 6607c7ac8679SGreg Rose err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo, 6608c7ac8679SGreg Rose NULL); 6609c127ea2cSThomas Graf if (err < 0) 6610c127ea2cSThomas Graf goto errout; 6611c127ea2cSThomas Graf 6612c127ea2cSThomas Graf /* Only the first call to __rtnl_register can fail */ 6613c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL); 6614c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL); 6615c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr, 6616c7ac8679SGreg Rose inet6_dump_ifaddr, NULL); 6617c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL, 6618c7ac8679SGreg Rose inet6_dump_ifmcaddr, NULL); 6619c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL, 6620c7ac8679SGreg Rose inet6_dump_ifacaddr, NULL); 662176f8f6cbSNicolas Dichtel __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf, 66227a674200SNicolas Dichtel inet6_netconf_dump_devconf, NULL); 6623c127ea2cSThomas Graf 66242a8cc6c8SYOSHIFUJI Hideaki ipv6_addr_label_rtnl_register(); 66252a8cc6c8SYOSHIFUJI Hideaki 66261da177e4SLinus Torvalds return 0; 6627c127ea2cSThomas Graf errout: 6628b382b191SThomas Graf rtnl_af_unregister(&inet6_ops); 6629c127ea2cSThomas Graf unregister_netdevice_notifier(&ipv6_dev_notf); 6630e0da5a48SPavel Emelyanov errlo: 6631c15b1ccaSHannes Frederic Sowa destroy_workqueue(addrconf_wq); 6632c15b1ccaSHannes Frederic Sowa out_nowq: 6633e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 66342cc6d2bfSNeil Horman out_addrlabel: 66352cc6d2bfSNeil Horman ipv6_addr_label_cleanup(); 66362cc6d2bfSNeil Horman out: 6637c127ea2cSThomas Graf return err; 66381da177e4SLinus Torvalds } 66391da177e4SLinus Torvalds 664009f7709fSDaniel Lezcano void addrconf_cleanup(void) 66411da177e4SLinus Torvalds { 6642176c39afSDaniel Lezcano struct net_device *dev; 66431da177e4SLinus Torvalds int i; 66441da177e4SLinus Torvalds 66451da177e4SLinus Torvalds unregister_netdevice_notifier(&ipv6_dev_notf); 6646e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 66472cc6d2bfSNeil Horman ipv6_addr_label_cleanup(); 66481da177e4SLinus Torvalds 66491da177e4SLinus Torvalds rtnl_lock(); 66501da177e4SLinus Torvalds 6651b382b191SThomas Graf __rtnl_af_unregister(&inet6_ops); 6652b382b191SThomas Graf 6653176c39afSDaniel Lezcano /* clean dev list */ 6654176c39afSDaniel Lezcano for_each_netdev(&init_net, dev) { 6655176c39afSDaniel Lezcano if (__in6_dev_get(dev) == NULL) 6656176c39afSDaniel Lezcano continue; 6657176c39afSDaniel Lezcano addrconf_ifdown(dev, 1); 6658176c39afSDaniel Lezcano } 6659176c39afSDaniel Lezcano addrconf_ifdown(init_net.loopback_dev, 2); 6660176c39afSDaniel Lezcano 66611da177e4SLinus Torvalds /* 66621da177e4SLinus Torvalds * Check hash table. 66631da177e4SLinus Torvalds */ 66645c578aedSstephen hemminger spin_lock_bh(&addrconf_hash_lock); 6665c2e21293Sstephen hemminger for (i = 0; i < IN6_ADDR_HSIZE; i++) 6666c2e21293Sstephen hemminger WARN_ON(!hlist_empty(&inet6_addr_lst[i])); 66675c578aedSstephen hemminger spin_unlock_bh(&addrconf_hash_lock); 6668c15b1ccaSHannes Frederic Sowa cancel_delayed_work(&addr_chk_work); 66691da177e4SLinus Torvalds rtnl_unlock(); 6670c15b1ccaSHannes Frederic Sowa 6671c15b1ccaSHannes Frederic Sowa destroy_workqueue(addrconf_wq); 66721da177e4SLinus Torvalds } 6673