12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * IPv6 Address [auto]configuration 41da177e4SLinus Torvalds * Linux INET6 implementation 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Authors: 71da177e4SLinus Torvalds * Pedro Roque <roque@di.fc.ul.pt> 81da177e4SLinus Torvalds * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 111da177e4SLinus Torvalds /* 121da177e4SLinus Torvalds * Changes: 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds * Janos Farkas : delete timer on ifdown 151da177e4SLinus Torvalds * <chexum@bankinf.banki.hu> 161da177e4SLinus Torvalds * Andi Kleen : kill double kfree on module 171da177e4SLinus Torvalds * unload. 181da177e4SLinus Torvalds * Maciej W. Rozycki : FDDI support 191da177e4SLinus Torvalds * sekiya@USAGI : Don't send too many RS 201da177e4SLinus Torvalds * packets. 211da177e4SLinus Torvalds * yoshfuji@USAGI : Fixed interval between DAD 221da177e4SLinus Torvalds * packets. 231da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI : improved accuracy of 241da177e4SLinus Torvalds * address validation timer. 251da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041) 261da177e4SLinus Torvalds * support. 271da177e4SLinus Torvalds * Yuji SEKIYA @USAGI : Don't assign a same IPv6 281da177e4SLinus Torvalds * address on a same interface. 291da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI : ARCnet support 301da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to 311da177e4SLinus Torvalds * seq_file. 32b1cacb68SYOSHIFUJI Hideaki * YOSHIFUJI Hideaki @USAGI : improved source address 33b1cacb68SYOSHIFUJI Hideaki * selection; consider scope, 34b1cacb68SYOSHIFUJI Hideaki * status etc. 351da177e4SLinus Torvalds */ 361da177e4SLinus Torvalds 37f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt 38f3213831SJoe Perches 391da177e4SLinus Torvalds #include <linux/errno.h> 401da177e4SLinus Torvalds #include <linux/types.h> 41a0bffffcSIlpo Järvinen #include <linux/kernel.h> 42174cd4b1SIngo Molnar #include <linux/sched/signal.h> 431da177e4SLinus Torvalds #include <linux/socket.h> 441da177e4SLinus Torvalds #include <linux/sockios.h> 451da177e4SLinus Torvalds #include <linux/net.h> 463d1bec99SHannes Frederic Sowa #include <linux/inet.h> 471da177e4SLinus Torvalds #include <linux/in6.h> 481da177e4SLinus Torvalds #include <linux/netdevice.h> 491823730fSThomas Graf #include <linux/if_addr.h> 501da177e4SLinus Torvalds #include <linux/if_arp.h> 511da177e4SLinus Torvalds #include <linux/if_arcnet.h> 521da177e4SLinus Torvalds #include <linux/if_infiniband.h> 531da177e4SLinus Torvalds #include <linux/route.h> 541da177e4SLinus Torvalds #include <linux/inetdevice.h> 551da177e4SLinus Torvalds #include <linux/init.h> 565a0e3ad6STejun Heo #include <linux/slab.h> 571da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 581da177e4SLinus Torvalds #include <linux/sysctl.h> 591da177e4SLinus Torvalds #endif 604fc268d2SRandy Dunlap #include <linux/capability.h> 611da177e4SLinus Torvalds #include <linux/delay.h> 621da177e4SLinus Torvalds #include <linux/notifier.h> 63543537bdSPaulo Marques #include <linux/string.h> 64ddbe5032SEric Dumazet #include <linux/hash.h> 651da177e4SLinus Torvalds 66457c4cbcSEric W. Biederman #include <net/net_namespace.h> 671da177e4SLinus Torvalds #include <net/sock.h> 681da177e4SLinus Torvalds #include <net/snmp.h> 691da177e4SLinus Torvalds 705241c2d7SAlexander Aring #include <net/6lowpan.h> 71cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 #include <net/firewire.h> 721da177e4SLinus Torvalds #include <net/ipv6.h> 731da177e4SLinus Torvalds #include <net/protocol.h> 741da177e4SLinus Torvalds #include <net/ndisc.h> 751da177e4SLinus Torvalds #include <net/ip6_route.h> 761da177e4SLinus Torvalds #include <net/addrconf.h> 771da177e4SLinus Torvalds #include <net/tcp.h> 781da177e4SLinus Torvalds #include <net/ip.h> 795d620266SThomas Graf #include <net/netlink.h> 80f24e3d65SMitsuru Chinen #include <net/pkt_sched.h> 81ca254490SDavid Ahern #include <net/l3mdev.h> 821da177e4SLinus Torvalds #include <linux/if_tunnel.h> 831da177e4SLinus Torvalds #include <linux/rtnetlink.h> 84f3a1bfb1SNicolas Dichtel #include <linux/netconf.h> 851da177e4SLinus Torvalds #include <linux/random.h> 86e21e8467SStephen Hemminger #include <linux/uaccess.h> 877f7d9a6bSHerbert Xu #include <asm/unaligned.h> 881da177e4SLinus Torvalds 891da177e4SLinus Torvalds #include <linux/proc_fs.h> 901da177e4SLinus Torvalds #include <linux/seq_file.h> 91bc3b2d7fSPaul Gortmaker #include <linux/export.h> 929ee11f0fSJustin Iurman #include <linux/ioam6.h> 931da177e4SLinus Torvalds 941da177e4SLinus Torvalds #define INFINITY_LIFE_TIME 0xFFFFFFFF 9518a31e1eSThomas Graf 963d1bec99SHannes Frederic Sowa #define IPV6_MAX_STRLEN \ 973d1bec99SHannes Frederic Sowa sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255") 983d1bec99SHannes Frederic Sowa 9918a31e1eSThomas Graf static inline u32 cstamp_delta(unsigned long cstamp) 10018a31e1eSThomas Graf { 10118a31e1eSThomas Graf return (cstamp - INITIAL_JIFFIES) * 100UL / HZ; 10218a31e1eSThomas Graf } 1031da177e4SLinus Torvalds 104bd11f074SMaciej Żenczykowski static inline s32 rfc3315_s14_backoff_init(s32 irt) 105bd11f074SMaciej Żenczykowski { 106bd11f074SMaciej Żenczykowski /* multiply 'initial retransmission time' by 0.9 .. 1.1 */ 107e8a533cbSJason A. Donenfeld u64 tmp = get_random_u32_inclusive(900000, 1100000) * (u64)irt; 108bd11f074SMaciej Żenczykowski do_div(tmp, 1000000); 109bd11f074SMaciej Żenczykowski return (s32)tmp; 110bd11f074SMaciej Żenczykowski } 111bd11f074SMaciej Żenczykowski 112bd11f074SMaciej Żenczykowski static inline s32 rfc3315_s14_backoff_update(s32 rt, s32 mrt) 113bd11f074SMaciej Żenczykowski { 114bd11f074SMaciej Żenczykowski /* multiply 'retransmission timeout' by 1.9 .. 2.1 */ 115e8a533cbSJason A. Donenfeld u64 tmp = get_random_u32_inclusive(1900000, 2100000) * (u64)rt; 116bd11f074SMaciej Żenczykowski do_div(tmp, 1000000); 117bd11f074SMaciej Żenczykowski if ((s32)tmp > mrt) { 118bd11f074SMaciej Żenczykowski /* multiply 'maximum retransmission time' by 0.9 .. 1.1 */ 119e8a533cbSJason A. Donenfeld tmp = get_random_u32_inclusive(900000, 1100000) * (u64)mrt; 120bd11f074SMaciej Żenczykowski do_div(tmp, 1000000); 121bd11f074SMaciej Żenczykowski } 122bd11f074SMaciej Żenczykowski return (s32)tmp; 123bd11f074SMaciej Żenczykowski } 124bd11f074SMaciej Żenczykowski 1251da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 126a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev); 127408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev); 128408c4768SPavel Emelyanov #else 129a317a2f1SWANG Cong static inline int addrconf_sysctl_register(struct inet6_dev *idev) 130408c4768SPavel Emelyanov { 131a317a2f1SWANG Cong return 0; 132408c4768SPavel Emelyanov } 133408c4768SPavel Emelyanov 134408c4768SPavel Emelyanov static inline void addrconf_sysctl_unregister(struct inet6_dev *idev) 135408c4768SPavel Emelyanov { 136408c4768SPavel Emelyanov } 1371da177e4SLinus Torvalds #endif 1381da177e4SLinus Torvalds 139969c5464SFernando Gont static void ipv6_gen_rnd_iid(struct in6_addr *addr); 1401da177e4SLinus Torvalds 1411b34be74SYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev); 142d9bf82c2SEric Dumazet static int ipv6_count_addresses(const struct inet6_dev *idev); 143622c81d5SHannes Frederic Sowa static int ipv6_generate_stable_address(struct in6_addr *addr, 144622c81d5SHannes Frederic Sowa u8 dad_count, 145622c81d5SHannes Frederic Sowa const struct inet6_dev *idev); 1461da177e4SLinus Torvalds 14727c565aeSEric Dumazet #define IN6_ADDR_HSIZE_SHIFT 8 14827c565aeSEric Dumazet #define IN6_ADDR_HSIZE (1 << IN6_ADDR_HSIZE_SHIFT) 1491da177e4SLinus Torvalds 1508805d13fSEric Dumazet static void addrconf_verify(struct net *net); 1518805d13fSEric Dumazet static void addrconf_verify_rtnl(struct net *net); 1521da177e4SLinus Torvalds 153c15b1ccaSHannes Frederic Sowa static struct workqueue_struct *addrconf_wq; 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds static void addrconf_join_anycast(struct inet6_ifaddr *ifp); 1561da177e4SLinus Torvalds static void addrconf_leave_anycast(struct inet6_ifaddr *ifp); 1571da177e4SLinus Torvalds 15893d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, 15975c78500SMoni Shoua unsigned long event); 160ae79dbf6SFlorent Fourcot static int addrconf_ifdown(struct net_device *dev, bool unregister); 1611da177e4SLinus Torvalds 1628d1c802bSDavid Ahern static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx, 16321caa662SRomain Kuntz int plen, 16421caa662SRomain Kuntz const struct net_device *dev, 1652b2450caSDavid Ahern u32 flags, u32 noflags, 1662b2450caSDavid Ahern bool no_gw); 16721caa662SRomain Kuntz 168cf22f9a2SDavid S. Miller static void addrconf_dad_start(struct inet6_ifaddr *ifp); 169c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w); 170c76fe2d9SDavid Ahern static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id, 171c76fe2d9SDavid Ahern bool send_na); 172896585d4SHangbin Liu static void addrconf_dad_run(struct inet6_dev *idev, bool restart); 173e99e88a9SKees Cook static void addrconf_rs_timer(struct timer_list *t); 1741da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); 1751da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); 1761da177e4SLinus Torvalds 1771da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev, 1781da177e4SLinus Torvalds struct prefix_info *pinfo); 1791da177e4SLinus Torvalds 180888c848eSAdrian Bunk static struct ipv6_devconf ipv6_devconf __read_mostly = { 1811da177e4SLinus Torvalds .forwarding = 0, 1821da177e4SLinus Torvalds .hop_limit = IPV6_DEFAULT_HOPLIMIT, 1831da177e4SLinus Torvalds .mtu6 = IPV6_MIN_MTU, 1841da177e4SLinus Torvalds .accept_ra = 1, 1851da177e4SLinus Torvalds .accept_redirects = 1, 1861da177e4SLinus Torvalds .autoconf = 1, 1871da177e4SLinus Torvalds .force_mld_version = 0, 188fc4eba58SHannes Frederic Sowa .mldv1_unsolicited_report_interval = 10 * HZ, 189fc4eba58SHannes Frederic Sowa .mldv2_unsolicited_report_interval = HZ, 1901da177e4SLinus Torvalds .dad_transmits = 1, 1911da177e4SLinus Torvalds .rtr_solicits = MAX_RTR_SOLICITATIONS, 1921da177e4SLinus Torvalds .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, 193bd11f074SMaciej Żenczykowski .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL, 1941da177e4SLinus Torvalds .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, 1951da177e4SLinus Torvalds .use_tempaddr = 0, 1961da177e4SLinus Torvalds .temp_valid_lft = TEMP_VALID_LIFETIME, 1971da177e4SLinus Torvalds .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, 1981da177e4SLinus Torvalds .regen_max_retry = REGEN_MAX_RETRY, 1991da177e4SLinus Torvalds .max_desync_factor = MAX_DESYNC_FACTOR, 2001da177e4SLinus Torvalds .max_addresses = IPV6_MAX_ADDRESSES, 20165f5c7c1SYOSHIFUJI Hideaki .accept_ra_defrtr = 1, 2026b2e04bcSPraveen Chaudhary .ra_defrtr_metric = IP6_RT_PRIO_USER, 203d9333196SBen Greear .accept_ra_from_local = 0, 2048013d1d7SHangbin Liu .accept_ra_min_hop_limit= 1, 205c4fd30ebSYOSHIFUJI Hideaki .accept_ra_pinfo = 1, 206930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 207930d6ff2SYOSHIFUJI Hideaki .accept_ra_rtr_pref = 1, 20852e16356SYOSHIFUJI Hideaki .rtr_probe_interval = 60 * HZ, 20909c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 210bbea124bSJoel Scherpelz .accept_ra_rt_info_min_plen = 0, 21109c884d4SYOSHIFUJI Hideaki .accept_ra_rt_info_max_plen = 0, 21209c884d4SYOSHIFUJI Hideaki #endif 213930d6ff2SYOSHIFUJI Hideaki #endif 214fbea49e1SYOSHIFUJI Hideaki .proxy_ndp = 0, 2150bcbc926SYOSHIFUJI Hideaki .accept_source_route = 0, /* we do not accept RH0 by default. */ 216778d80beSYOSHIFUJI Hideaki .disable_ipv6 = 0, 21709400953SNicolas Dichtel .accept_dad = 0, 218b800c3b9SHannes Frederic Sowa .suppress_frag_ndisc = 1, 219c2943f14SHarout Hedeshian .accept_ra_mtu = 1, 2203d1bec99SHannes Frederic Sowa .stable_secret = { 2213d1bec99SHannes Frederic Sowa .initialized = false, 2223985e8a3SErik Kline }, 2233985e8a3SErik Kline .use_oif_addrs_only = 0, 22435103d11SAndy Gospodarek .ignore_routes_with_linkdown = 0, 225f1705ec1SDavid Ahern .keep_addr_on_down = 0, 2261ababebaSDavid Lebrun .seg6_enabled = 0, 227bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC 228bf355b8dSDavid Lebrun .seg6_require_hmac = 0, 229bf355b8dSDavid Lebrun #endif 230adc176c5SErik Nordmark .enhanced_dad = 1, 231d35a00b8SFelix Jia .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64, 232df789fe7SDavid Forster .disable_policy = 0, 2338610c7c6SAlexander Aring .rpl_seg_enabled = 0, 2349ee11f0fSJustin Iurman .ioam6_enabled = 0, 2359ee11f0fSJustin Iurman .ioam6_id = IOAM6_DEFAULT_IF_ID, 2369ee11f0fSJustin Iurman .ioam6_id_wide = IOAM6_DEFAULT_IF_ID_WIDE, 23718ac597aSJames Prestwood .ndisc_evict_nocarrier = 1, 2381da177e4SLinus Torvalds }; 2391da177e4SLinus Torvalds 240ab32ea5dSBrian Haley static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = { 2411da177e4SLinus Torvalds .forwarding = 0, 2421da177e4SLinus Torvalds .hop_limit = IPV6_DEFAULT_HOPLIMIT, 2431da177e4SLinus Torvalds .mtu6 = IPV6_MIN_MTU, 2441da177e4SLinus Torvalds .accept_ra = 1, 2451da177e4SLinus Torvalds .accept_redirects = 1, 2461da177e4SLinus Torvalds .autoconf = 1, 247fc4eba58SHannes Frederic Sowa .force_mld_version = 0, 248fc4eba58SHannes Frederic Sowa .mldv1_unsolicited_report_interval = 10 * HZ, 249fc4eba58SHannes Frederic Sowa .mldv2_unsolicited_report_interval = HZ, 2501da177e4SLinus Torvalds .dad_transmits = 1, 2511da177e4SLinus Torvalds .rtr_solicits = MAX_RTR_SOLICITATIONS, 2521da177e4SLinus Torvalds .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, 253bd11f074SMaciej Żenczykowski .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL, 2541da177e4SLinus Torvalds .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, 2551da177e4SLinus Torvalds .use_tempaddr = 0, 2561da177e4SLinus Torvalds .temp_valid_lft = TEMP_VALID_LIFETIME, 2571da177e4SLinus Torvalds .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, 2581da177e4SLinus Torvalds .regen_max_retry = REGEN_MAX_RETRY, 2591da177e4SLinus Torvalds .max_desync_factor = MAX_DESYNC_FACTOR, 2601da177e4SLinus Torvalds .max_addresses = IPV6_MAX_ADDRESSES, 26165f5c7c1SYOSHIFUJI Hideaki .accept_ra_defrtr = 1, 2626b2e04bcSPraveen Chaudhary .ra_defrtr_metric = IP6_RT_PRIO_USER, 263d9333196SBen Greear .accept_ra_from_local = 0, 2648013d1d7SHangbin Liu .accept_ra_min_hop_limit= 1, 265c4fd30ebSYOSHIFUJI Hideaki .accept_ra_pinfo = 1, 266930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 267930d6ff2SYOSHIFUJI Hideaki .accept_ra_rtr_pref = 1, 26852e16356SYOSHIFUJI Hideaki .rtr_probe_interval = 60 * HZ, 26909c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 270bbea124bSJoel Scherpelz .accept_ra_rt_info_min_plen = 0, 27109c884d4SYOSHIFUJI Hideaki .accept_ra_rt_info_max_plen = 0, 27209c884d4SYOSHIFUJI Hideaki #endif 273930d6ff2SYOSHIFUJI Hideaki #endif 274fbea49e1SYOSHIFUJI Hideaki .proxy_ndp = 0, 2750bcbc926SYOSHIFUJI Hideaki .accept_source_route = 0, /* we do not accept RH0 by default. */ 276778d80beSYOSHIFUJI Hideaki .disable_ipv6 = 0, 2771b34be74SYOSHIFUJI Hideaki .accept_dad = 1, 278b800c3b9SHannes Frederic Sowa .suppress_frag_ndisc = 1, 279c2943f14SHarout Hedeshian .accept_ra_mtu = 1, 2803d1bec99SHannes Frederic Sowa .stable_secret = { 2813d1bec99SHannes Frederic Sowa .initialized = false, 2823d1bec99SHannes Frederic Sowa }, 2833985e8a3SErik Kline .use_oif_addrs_only = 0, 28435103d11SAndy Gospodarek .ignore_routes_with_linkdown = 0, 285f1705ec1SDavid Ahern .keep_addr_on_down = 0, 2861ababebaSDavid Lebrun .seg6_enabled = 0, 287bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC 288bf355b8dSDavid Lebrun .seg6_require_hmac = 0, 289bf355b8dSDavid Lebrun #endif 290adc176c5SErik Nordmark .enhanced_dad = 1, 291d35a00b8SFelix Jia .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64, 292df789fe7SDavid Forster .disable_policy = 0, 2938610c7c6SAlexander Aring .rpl_seg_enabled = 0, 2949ee11f0fSJustin Iurman .ioam6_enabled = 0, 2959ee11f0fSJustin Iurman .ioam6_id = IOAM6_DEFAULT_IF_ID, 2969ee11f0fSJustin Iurman .ioam6_id_wide = IOAM6_DEFAULT_IF_ID_WIDE, 29718ac597aSJames Prestwood .ndisc_evict_nocarrier = 1, 2981da177e4SLinus Torvalds }; 2991da177e4SLinus Torvalds 3001f372c7bSMike Manning /* Check if link is ready: is it up and is a valid qdisc available */ 3011f372c7bSMike Manning static inline bool addrconf_link_ready(const struct net_device *dev) 302f24e3d65SMitsuru Chinen { 3031f372c7bSMike Manning return netif_oper_up(dev) && !qdisc_tx_is_noop(dev); 304f24e3d65SMitsuru Chinen } 305f24e3d65SMitsuru Chinen 306b7b1bfceSHannes Frederic Sowa static void addrconf_del_rs_timer(struct inet6_dev *idev) 3071da177e4SLinus Torvalds { 308b7b1bfceSHannes Frederic Sowa if (del_timer(&idev->rs_timer)) 309b7b1bfceSHannes Frederic Sowa __in6_dev_put(idev); 310b7b1bfceSHannes Frederic Sowa } 311b7b1bfceSHannes Frederic Sowa 312c15b1ccaSHannes Frederic Sowa static void addrconf_del_dad_work(struct inet6_ifaddr *ifp) 313b7b1bfceSHannes Frederic Sowa { 314c15b1ccaSHannes Frederic Sowa if (cancel_delayed_work(&ifp->dad_work)) 3151da177e4SLinus Torvalds __in6_ifa_put(ifp); 3161da177e4SLinus Torvalds } 3171da177e4SLinus Torvalds 318b7b1bfceSHannes Frederic Sowa static void addrconf_mod_rs_timer(struct inet6_dev *idev, 3191da177e4SLinus Torvalds unsigned long when) 3201da177e4SLinus Torvalds { 321b7b1bfceSHannes Frederic Sowa if (!timer_pending(&idev->rs_timer)) 322b7b1bfceSHannes Frederic Sowa in6_dev_hold(idev); 323b7b1bfceSHannes Frederic Sowa mod_timer(&idev->rs_timer, jiffies + when); 3241da177e4SLinus Torvalds } 325b7b1bfceSHannes Frederic Sowa 326c15b1ccaSHannes Frederic Sowa static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp, 327c15b1ccaSHannes Frederic Sowa unsigned long delay) 328b7b1bfceSHannes Frederic Sowa { 329b7b1bfceSHannes Frederic Sowa in6_ifa_hold(ifp); 330f8a894b2SXin Long if (mod_delayed_work(addrconf_wq, &ifp->dad_work, delay)) 331f8a894b2SXin Long in6_ifa_put(ifp); 3321da177e4SLinus Torvalds } 3331da177e4SLinus Torvalds 3347f7d9a6bSHerbert Xu static int snmp6_alloc_dev(struct inet6_dev *idev) 3357f7d9a6bSHerbert Xu { 336827da44cSJohn Stultz int i; 337827da44cSJohn Stultz 338425b9c7fSVasily Averin idev->stats.ipv6 = alloc_percpu_gfp(struct ipstats_mib, GFP_KERNEL_ACCOUNT); 339698365faSWANG Cong if (!idev->stats.ipv6) 3407f7d9a6bSHerbert Xu goto err_ip; 341827da44cSJohn Stultz 342827da44cSJohn Stultz for_each_possible_cpu(i) { 343827da44cSJohn Stultz struct ipstats_mib *addrconf_stats; 344698365faSWANG Cong addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i); 345827da44cSJohn Stultz u64_stats_init(&addrconf_stats->syncp); 346827da44cSJohn Stultz } 347827da44cSJohn Stultz 348827da44cSJohn Stultz 349be281e55SEric Dumazet idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device), 350be281e55SEric Dumazet GFP_KERNEL); 351be281e55SEric Dumazet if (!idev->stats.icmpv6dev) 3527f7d9a6bSHerbert Xu goto err_icmp; 353be281e55SEric Dumazet idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device), 354425b9c7fSVasily Averin GFP_KERNEL_ACCOUNT); 355be281e55SEric Dumazet if (!idev->stats.icmpv6msgdev) 35614878f75SDavid L Stevens goto err_icmpmsg; 3577f7d9a6bSHerbert Xu 3587f7d9a6bSHerbert Xu return 0; 3597f7d9a6bSHerbert Xu 36014878f75SDavid L Stevens err_icmpmsg: 361be281e55SEric Dumazet kfree(idev->stats.icmpv6dev); 3627f7d9a6bSHerbert Xu err_icmp: 363698365faSWANG Cong free_percpu(idev->stats.ipv6); 3647f7d9a6bSHerbert Xu err_ip: 365aaf70ec7SPavel Emelyanov return -ENOMEM; 3667f7d9a6bSHerbert Xu } 3677f7d9a6bSHerbert Xu 3681da177e4SLinus Torvalds static struct inet6_dev *ipv6_add_dev(struct net_device *dev) 3691da177e4SLinus Torvalds { 3701da177e4SLinus Torvalds struct inet6_dev *ndev; 371a317a2f1SWANG Cong int err = -ENOMEM; 3721da177e4SLinus Torvalds 3731da177e4SLinus Torvalds ASSERT_RTNL(); 3741da177e4SLinus Torvalds 375e5f80fcfSEric Dumazet if (dev->mtu < IPV6_MIN_MTU && dev != blackhole_netdev) 376a317a2f1SWANG Cong return ERR_PTR(-EINVAL); 3771da177e4SLinus Torvalds 378425b9c7fSVasily Averin ndev = kzalloc(sizeof(*ndev), GFP_KERNEL_ACCOUNT); 37963159f29SIan Morris if (!ndev) 380a317a2f1SWANG Cong return ERR_PTR(err); 3811da177e4SLinus Torvalds 3821da177e4SLinus Torvalds rwlock_init(&ndev->lock); 3831da177e4SLinus Torvalds ndev->dev = dev; 384502a2ffdSstephen hemminger INIT_LIST_HEAD(&ndev->addr_list); 385e99e88a9SKees Cook timer_setup(&ndev->rs_timer, addrconf_rs_timer, 0); 386c346dca1SYOSHIFUJI Hideaki memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf)); 3879b29c696SHannes Frederic Sowa 3889b29c696SHannes Frederic Sowa if (ndev->cnf.stable_secret.initialized) 389d35a00b8SFelix Jia ndev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY; 3909b29c696SHannes Frederic Sowa 3911da177e4SLinus Torvalds ndev->cnf.mtu6 = dev->mtu; 39249b99da2SRocco Yue ndev->ra_mtu = 0; 3931da177e4SLinus Torvalds ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl); 39463159f29SIan Morris if (!ndev->nd_parms) { 3951da177e4SLinus Torvalds kfree(ndev); 396a317a2f1SWANG Cong return ERR_PTR(err); 3971da177e4SLinus Torvalds } 3980187bdfbSBen Hutchings if (ndev->cnf.forwarding) 3990187bdfbSBen Hutchings dev_disable_lro(dev); 4001da177e4SLinus Torvalds /* We refer to the device */ 401d62607c3SJakub Kicinski netdev_hold(dev, &ndev->dev_tracker, GFP_KERNEL); 4021da177e4SLinus Torvalds 4031da177e4SLinus Torvalds if (snmp6_alloc_dev(ndev) < 0) { 404e32ac250SJoe Perches netdev_dbg(dev, "%s: cannot allocate memory for statistics\n", 405e32ac250SJoe Perches __func__); 4061da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, ndev->nd_parms); 407d62607c3SJakub Kicinski netdev_put(dev, &ndev->dev_tracker); 4088603e33dSRoy Li kfree(ndev); 409a317a2f1SWANG Cong return ERR_PTR(err); 4101da177e4SLinus Torvalds } 4111da177e4SLinus Torvalds 412dd263a8cSIdo Schimmel if (dev != blackhole_netdev) { 4131da177e4SLinus Torvalds if (snmp6_register_dev(ndev) < 0) { 414e32ac250SJoe Perches netdev_dbg(dev, "%s: cannot create /proc/net/dev_snmp6/%s\n", 415ba3542e1Sdingtianhong __func__, dev->name); 416a317a2f1SWANG Cong goto err_release; 4171da177e4SLinus Torvalds } 418e5f80fcfSEric Dumazet } 4199d6280daSJiri Bohac /* One reference from device. */ 4201be92460SReshetova, Elena refcount_set(&ndev->refcnt, 1); 4211da177e4SLinus Torvalds 4221b34be74SYOSHIFUJI Hideaki if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) 4231b34be74SYOSHIFUJI Hideaki ndev->cnf.accept_dad = -1; 4241b34be74SYOSHIFUJI Hideaki 42507a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 426c7dc89c0SFred L. Templin if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) { 427f3213831SJoe Perches pr_info("%s: Disabled Multicast RS\n", dev->name); 428c7dc89c0SFred L. Templin ndev->cnf.rtr_solicits = 0; 429c7dc89c0SFred L. Templin } 430b077d7abSYOSHIFUJI Hideaki #endif 431b077d7abSYOSHIFUJI Hideaki 432372e6c8fSstephen hemminger INIT_LIST_HEAD(&ndev->tempaddr_list); 43376506a98SJiri Bohac ndev->desync_factor = U32_MAX; 4341da177e4SLinus Torvalds if ((dev->flags&IFF_LOOPBACK) || 4351da177e4SLinus Torvalds dev->type == ARPHRD_TUNNEL || 4369625ed72SYOSHIFUJI Hideaki dev->type == ARPHRD_TUNNEL6 || 4371da177e4SLinus Torvalds dev->type == ARPHRD_SIT || 4381da177e4SLinus Torvalds dev->type == ARPHRD_NONE) { 4390be669bbSJoerg Roedel ndev->cnf.use_tempaddr = -1; 440969c5464SFernando Gont } 4415d9efa7eSDavid S. Miller 442914faa14SDaniel Borkmann ndev->token = in6addr_any; 4431da177e4SLinus Torvalds 4441f372c7bSMike Manning if (netif_running(dev) && addrconf_link_ready(dev)) 44553aadcc9SHerbert Xu ndev->if_flags |= IF_READY; 44653aadcc9SHerbert Xu 4471da177e4SLinus Torvalds ipv6_mc_init_dev(ndev); 4481da177e4SLinus Torvalds ndev->tstamp = jiffies; 449e5f80fcfSEric Dumazet if (dev != blackhole_netdev) { 450a317a2f1SWANG Cong err = addrconf_sysctl_register(ndev); 451a317a2f1SWANG Cong if (err) { 452a317a2f1SWANG Cong ipv6_mc_destroy_dev(ndev); 4532a189f9eSSabrina Dubroca snmp6_unregister_dev(ndev); 454a317a2f1SWANG Cong goto err_release; 455a317a2f1SWANG Cong } 456e5f80fcfSEric Dumazet } 45730c4cf57SDavid L Stevens /* protected by rtnl_lock */ 458cf778b00SEric Dumazet rcu_assign_pointer(dev->ip6_ptr, ndev); 459d88ae4ccSYOSHIFUJI Hideaki 460e5f80fcfSEric Dumazet if (dev != blackhole_netdev) { 4612c5e8933SHannes Frederic Sowa /* Join interface-local all-node multicast group */ 4622c5e8933SHannes Frederic Sowa ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes); 4632c5e8933SHannes Frederic Sowa 464d88ae4ccSYOSHIFUJI Hideaki /* Join all-node multicast group */ 465f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes); 466d88ae4ccSYOSHIFUJI Hideaki 467d6ddef9eSLi Wei /* Join all-router multicast group if forwarding is set */ 4688b2aaedeSLi Wei if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST)) 469d6ddef9eSLi Wei ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters); 470e5f80fcfSEric Dumazet } 4711da177e4SLinus Torvalds return ndev; 472a317a2f1SWANG Cong 473a317a2f1SWANG Cong err_release: 474a317a2f1SWANG Cong neigh_parms_release(&nd_tbl, ndev->nd_parms); 475a317a2f1SWANG Cong ndev->dead = 1; 476a317a2f1SWANG Cong in6_dev_finish_destroy(ndev); 477a317a2f1SWANG Cong return ERR_PTR(err); 4781da177e4SLinus Torvalds } 4791da177e4SLinus Torvalds 480cee89473SYOSHIFUJI Hideaki static struct inet6_dev *ipv6_find_idev(struct net_device *dev) 4811da177e4SLinus Torvalds { 4821da177e4SLinus Torvalds struct inet6_dev *idev; 4831da177e4SLinus Torvalds 4841da177e4SLinus Torvalds ASSERT_RTNL(); 4851da177e4SLinus Torvalds 486e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 487e21e8467SStephen Hemminger if (!idev) { 488e21e8467SStephen Hemminger idev = ipv6_add_dev(dev); 489a317a2f1SWANG Cong if (IS_ERR(idev)) 490db0b99f5SSabrina Dubroca return idev; 4911da177e4SLinus Torvalds } 492c5e33bddSYOSHIFUJI Hideaki 4931da177e4SLinus Torvalds if (dev->flags&IFF_UP) 4941da177e4SLinus Torvalds ipv6_mc_up(idev); 4951da177e4SLinus Torvalds return idev; 4961da177e4SLinus Torvalds } 4971da177e4SLinus Torvalds 498f3a1bfb1SNicolas Dichtel static int inet6_netconf_msgsize_devconf(int type) 499f3a1bfb1SNicolas Dichtel { 500f3a1bfb1SNicolas Dichtel int size = NLMSG_ALIGN(sizeof(struct netconfmsg)) 501f3a1bfb1SNicolas Dichtel + nla_total_size(4); /* NETCONFA_IFINDEX */ 502136ba622SZhang Shengju bool all = false; 503f3a1bfb1SNicolas Dichtel 504136ba622SZhang Shengju if (type == NETCONFA_ALL) 505136ba622SZhang Shengju all = true; 506136ba622SZhang Shengju 507136ba622SZhang Shengju if (all || type == NETCONFA_FORWARDING) 508f3a1bfb1SNicolas Dichtel size += nla_total_size(4); 509b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE 510136ba622SZhang Shengju if (all || type == NETCONFA_MC_FORWARDING) 511d67b8c61SNicolas Dichtel size += nla_total_size(4); 512b1afce95SDavid S. Miller #endif 513136ba622SZhang Shengju if (all || type == NETCONFA_PROXY_NEIGH) 514c92d5491Sstephen hemminger size += nla_total_size(4); 515f3a1bfb1SNicolas Dichtel 516136ba622SZhang Shengju if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) 51735103d11SAndy Gospodarek size += nla_total_size(4); 51835103d11SAndy Gospodarek 519f3a1bfb1SNicolas Dichtel return size; 520f3a1bfb1SNicolas Dichtel } 521f3a1bfb1SNicolas Dichtel 522f3a1bfb1SNicolas Dichtel static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex, 523f3a1bfb1SNicolas Dichtel struct ipv6_devconf *devconf, u32 portid, 524f3a1bfb1SNicolas Dichtel u32 seq, int event, unsigned int flags, 525f3a1bfb1SNicolas Dichtel int type) 526f3a1bfb1SNicolas Dichtel { 527f3a1bfb1SNicolas Dichtel struct nlmsghdr *nlh; 528f3a1bfb1SNicolas Dichtel struct netconfmsg *ncm; 529136ba622SZhang Shengju bool all = false; 530f3a1bfb1SNicolas Dichtel 531f3a1bfb1SNicolas Dichtel nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg), 532f3a1bfb1SNicolas Dichtel flags); 53363159f29SIan Morris if (!nlh) 534f3a1bfb1SNicolas Dichtel return -EMSGSIZE; 535f3a1bfb1SNicolas Dichtel 536136ba622SZhang Shengju if (type == NETCONFA_ALL) 537136ba622SZhang Shengju all = true; 538136ba622SZhang Shengju 539f3a1bfb1SNicolas Dichtel ncm = nlmsg_data(nlh); 540f3a1bfb1SNicolas Dichtel ncm->ncm_family = AF_INET6; 541f3a1bfb1SNicolas Dichtel 542f3a1bfb1SNicolas Dichtel if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0) 543f3a1bfb1SNicolas Dichtel goto nla_put_failure; 544f3a1bfb1SNicolas Dichtel 54523452170SDavid Ahern if (!devconf) 54623452170SDavid Ahern goto out; 54723452170SDavid Ahern 548136ba622SZhang Shengju if ((all || type == NETCONFA_FORWARDING) && 549f3a1bfb1SNicolas Dichtel nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0) 550f3a1bfb1SNicolas Dichtel goto nla_put_failure; 551b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE 552136ba622SZhang Shengju if ((all || type == NETCONFA_MC_FORWARDING) && 553d67b8c61SNicolas Dichtel nla_put_s32(skb, NETCONFA_MC_FORWARDING, 554145c7a79SEric Dumazet atomic_read(&devconf->mc_forwarding)) < 0) 555d67b8c61SNicolas Dichtel goto nla_put_failure; 556b1afce95SDavid S. Miller #endif 557136ba622SZhang Shengju if ((all || type == NETCONFA_PROXY_NEIGH) && 558c92d5491Sstephen hemminger nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0) 559c92d5491Sstephen hemminger goto nla_put_failure; 560c92d5491Sstephen hemminger 561136ba622SZhang Shengju if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) && 56235103d11SAndy Gospodarek nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 56335103d11SAndy Gospodarek devconf->ignore_routes_with_linkdown) < 0) 56435103d11SAndy Gospodarek goto nla_put_failure; 56535103d11SAndy Gospodarek 56623452170SDavid Ahern out: 567053c095aSJohannes Berg nlmsg_end(skb, nlh); 568053c095aSJohannes Berg return 0; 569f3a1bfb1SNicolas Dichtel 570f3a1bfb1SNicolas Dichtel nla_put_failure: 571f3a1bfb1SNicolas Dichtel nlmsg_cancel(skb, nlh); 572f3a1bfb1SNicolas Dichtel return -EMSGSIZE; 573f3a1bfb1SNicolas Dichtel } 574f3a1bfb1SNicolas Dichtel 57585b3daadSDavid Ahern void inet6_netconf_notify_devconf(struct net *net, int event, int type, 57685b3daadSDavid Ahern int ifindex, struct ipv6_devconf *devconf) 577f3a1bfb1SNicolas Dichtel { 578f3a1bfb1SNicolas Dichtel struct sk_buff *skb; 579f3a1bfb1SNicolas Dichtel int err = -ENOBUFS; 580f3a1bfb1SNicolas Dichtel 581927265bcSEric Dumazet skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_KERNEL); 58263159f29SIan Morris if (!skb) 583f3a1bfb1SNicolas Dichtel goto errout; 584f3a1bfb1SNicolas Dichtel 585f3a1bfb1SNicolas Dichtel err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0, 58685b3daadSDavid Ahern event, 0, type); 587f3a1bfb1SNicolas Dichtel if (err < 0) { 588f3a1bfb1SNicolas Dichtel /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */ 589f3a1bfb1SNicolas Dichtel WARN_ON(err == -EMSGSIZE); 590f3a1bfb1SNicolas Dichtel kfree_skb(skb); 591f3a1bfb1SNicolas Dichtel goto errout; 592f3a1bfb1SNicolas Dichtel } 593927265bcSEric Dumazet rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_KERNEL); 594f3a1bfb1SNicolas Dichtel return; 595f3a1bfb1SNicolas Dichtel errout: 596f3a1bfb1SNicolas Dichtel rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err); 597f3a1bfb1SNicolas Dichtel } 598f3a1bfb1SNicolas Dichtel 59976f8f6cbSNicolas Dichtel static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = { 60076f8f6cbSNicolas Dichtel [NETCONFA_IFINDEX] = { .len = sizeof(int) }, 60176f8f6cbSNicolas Dichtel [NETCONFA_FORWARDING] = { .len = sizeof(int) }, 602c92d5491Sstephen hemminger [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) }, 60335103d11SAndy Gospodarek [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) }, 60476f8f6cbSNicolas Dichtel }; 60576f8f6cbSNicolas Dichtel 60638d51810SJakub Kicinski static int inet6_netconf_valid_get_req(struct sk_buff *skb, 60738d51810SJakub Kicinski const struct nlmsghdr *nlh, 60838d51810SJakub Kicinski struct nlattr **tb, 60938d51810SJakub Kicinski struct netlink_ext_ack *extack) 61038d51810SJakub Kicinski { 61138d51810SJakub Kicinski int i, err; 61238d51810SJakub Kicinski 61338d51810SJakub Kicinski if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(struct netconfmsg))) { 61438d51810SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf get request"); 61538d51810SJakub Kicinski return -EINVAL; 61638d51810SJakub Kicinski } 61738d51810SJakub Kicinski 61838d51810SJakub Kicinski if (!netlink_strict_get_check(skb)) 6198cb08174SJohannes Berg return nlmsg_parse_deprecated(nlh, sizeof(struct netconfmsg), 6208cb08174SJohannes Berg tb, NETCONFA_MAX, 6218cb08174SJohannes Berg devconf_ipv6_policy, extack); 62238d51810SJakub Kicinski 6238cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct netconfmsg), 6248cb08174SJohannes Berg tb, NETCONFA_MAX, 6258cb08174SJohannes Berg devconf_ipv6_policy, extack); 62638d51810SJakub Kicinski if (err) 62738d51810SJakub Kicinski return err; 62838d51810SJakub Kicinski 62938d51810SJakub Kicinski for (i = 0; i <= NETCONFA_MAX; i++) { 63038d51810SJakub Kicinski if (!tb[i]) 63138d51810SJakub Kicinski continue; 63238d51810SJakub Kicinski 63338d51810SJakub Kicinski switch (i) { 63438d51810SJakub Kicinski case NETCONFA_IFINDEX: 63538d51810SJakub Kicinski break; 63638d51810SJakub Kicinski default: 63738d51810SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in netconf get request"); 63838d51810SJakub Kicinski return -EINVAL; 63938d51810SJakub Kicinski } 64038d51810SJakub Kicinski } 64138d51810SJakub Kicinski 64238d51810SJakub Kicinski return 0; 64338d51810SJakub Kicinski } 64438d51810SJakub Kicinski 64576f8f6cbSNicolas Dichtel static int inet6_netconf_get_devconf(struct sk_buff *in_skb, 646c21ef3e3SDavid Ahern struct nlmsghdr *nlh, 647c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 64876f8f6cbSNicolas Dichtel { 64976f8f6cbSNicolas Dichtel struct net *net = sock_net(in_skb->sk); 65076f8f6cbSNicolas Dichtel struct nlattr *tb[NETCONFA_MAX+1]; 6514ea2607fSFlorian Westphal struct inet6_dev *in6_dev = NULL; 6524ea2607fSFlorian Westphal struct net_device *dev = NULL; 65376f8f6cbSNicolas Dichtel struct sk_buff *skb; 65476f8f6cbSNicolas Dichtel struct ipv6_devconf *devconf; 65576f8f6cbSNicolas Dichtel int ifindex; 65676f8f6cbSNicolas Dichtel int err; 65776f8f6cbSNicolas Dichtel 65838d51810SJakub Kicinski err = inet6_netconf_valid_get_req(in_skb, nlh, tb, extack); 65976f8f6cbSNicolas Dichtel if (err < 0) 6604ea2607fSFlorian Westphal return err; 6614ea2607fSFlorian Westphal 6624ea2607fSFlorian Westphal if (!tb[NETCONFA_IFINDEX]) 6634ea2607fSFlorian Westphal return -EINVAL; 66476f8f6cbSNicolas Dichtel 665a97eb33fSAnton Protopopov err = -EINVAL; 66676f8f6cbSNicolas Dichtel ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]); 66776f8f6cbSNicolas Dichtel switch (ifindex) { 66876f8f6cbSNicolas Dichtel case NETCONFA_IFINDEX_ALL: 66976f8f6cbSNicolas Dichtel devconf = net->ipv6.devconf_all; 67076f8f6cbSNicolas Dichtel break; 67176f8f6cbSNicolas Dichtel case NETCONFA_IFINDEX_DEFAULT: 67276f8f6cbSNicolas Dichtel devconf = net->ipv6.devconf_dflt; 67376f8f6cbSNicolas Dichtel break; 67476f8f6cbSNicolas Dichtel default: 6754ea2607fSFlorian Westphal dev = dev_get_by_index(net, ifindex); 67663159f29SIan Morris if (!dev) 6774ea2607fSFlorian Westphal return -EINVAL; 6784ea2607fSFlorian Westphal in6_dev = in6_dev_get(dev); 67963159f29SIan Morris if (!in6_dev) 68076f8f6cbSNicolas Dichtel goto errout; 68176f8f6cbSNicolas Dichtel devconf = &in6_dev->cnf; 68276f8f6cbSNicolas Dichtel break; 68376f8f6cbSNicolas Dichtel } 68476f8f6cbSNicolas Dichtel 68576f8f6cbSNicolas Dichtel err = -ENOBUFS; 6864ea2607fSFlorian Westphal skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL); 68763159f29SIan Morris if (!skb) 68876f8f6cbSNicolas Dichtel goto errout; 68976f8f6cbSNicolas Dichtel 69076f8f6cbSNicolas Dichtel err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 69176f8f6cbSNicolas Dichtel NETLINK_CB(in_skb).portid, 69276f8f6cbSNicolas Dichtel nlh->nlmsg_seq, RTM_NEWNETCONF, 0, 693136ba622SZhang Shengju NETCONFA_ALL); 69476f8f6cbSNicolas Dichtel if (err < 0) { 69576f8f6cbSNicolas Dichtel /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */ 69676f8f6cbSNicolas Dichtel WARN_ON(err == -EMSGSIZE); 69776f8f6cbSNicolas Dichtel kfree_skb(skb); 69876f8f6cbSNicolas Dichtel goto errout; 69976f8f6cbSNicolas Dichtel } 70076f8f6cbSNicolas Dichtel err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 70176f8f6cbSNicolas Dichtel errout: 7024ea2607fSFlorian Westphal if (in6_dev) 7034ea2607fSFlorian Westphal in6_dev_put(in6_dev); 7044ea2607fSFlorian Westphal dev_put(dev); 70576f8f6cbSNicolas Dichtel return err; 70676f8f6cbSNicolas Dichtel } 70776f8f6cbSNicolas Dichtel 7087a674200SNicolas Dichtel static int inet6_netconf_dump_devconf(struct sk_buff *skb, 7097a674200SNicolas Dichtel struct netlink_callback *cb) 7107a674200SNicolas Dichtel { 711addd383fSDavid Ahern const struct nlmsghdr *nlh = cb->nlh; 7127a674200SNicolas Dichtel struct net *net = sock_net(skb->sk); 7137a674200SNicolas Dichtel int h, s_h; 7147a674200SNicolas Dichtel int idx, s_idx; 7157a674200SNicolas Dichtel struct net_device *dev; 7167a674200SNicolas Dichtel struct inet6_dev *idev; 7177a674200SNicolas Dichtel struct hlist_head *head; 7187a674200SNicolas Dichtel 719addd383fSDavid Ahern if (cb->strict_check) { 720addd383fSDavid Ahern struct netlink_ext_ack *extack = cb->extack; 721addd383fSDavid Ahern struct netconfmsg *ncm; 722addd383fSDavid Ahern 723addd383fSDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ncm))) { 724addd383fSDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf dump request"); 725addd383fSDavid Ahern return -EINVAL; 726addd383fSDavid Ahern } 727addd383fSDavid Ahern 728addd383fSDavid Ahern if (nlmsg_attrlen(nlh, sizeof(*ncm))) { 729addd383fSDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Invalid data after header in netconf dump request"); 730addd383fSDavid Ahern return -EINVAL; 731addd383fSDavid Ahern } 732addd383fSDavid Ahern } 733addd383fSDavid Ahern 7347a674200SNicolas Dichtel s_h = cb->args[0]; 7357a674200SNicolas Dichtel s_idx = idx = cb->args[1]; 7367a674200SNicolas Dichtel 7377a674200SNicolas Dichtel for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 7387a674200SNicolas Dichtel idx = 0; 7397a674200SNicolas Dichtel head = &net->dev_index_head[h]; 7407a674200SNicolas Dichtel rcu_read_lock(); 74163998ac2SNicolas Dichtel cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ 74263998ac2SNicolas Dichtel net->dev_base_seq; 7437a674200SNicolas Dichtel hlist_for_each_entry_rcu(dev, head, index_hlist) { 7447a674200SNicolas Dichtel if (idx < s_idx) 7457a674200SNicolas Dichtel goto cont; 7467a674200SNicolas Dichtel idev = __in6_dev_get(dev); 7477a674200SNicolas Dichtel if (!idev) 7487a674200SNicolas Dichtel goto cont; 7497a674200SNicolas Dichtel 7507a674200SNicolas Dichtel if (inet6_netconf_fill_devconf(skb, dev->ifindex, 7517a674200SNicolas Dichtel &idev->cnf, 7527a674200SNicolas Dichtel NETLINK_CB(cb->skb).portid, 753addd383fSDavid Ahern nlh->nlmsg_seq, 7547a674200SNicolas Dichtel RTM_NEWNETCONF, 7557a674200SNicolas Dichtel NLM_F_MULTI, 756136ba622SZhang Shengju NETCONFA_ALL) < 0) { 7577a674200SNicolas Dichtel rcu_read_unlock(); 7587a674200SNicolas Dichtel goto done; 7597a674200SNicolas Dichtel } 76063998ac2SNicolas Dichtel nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 7617a674200SNicolas Dichtel cont: 7627a674200SNicolas Dichtel idx++; 7637a674200SNicolas Dichtel } 7647a674200SNicolas Dichtel rcu_read_unlock(); 7657a674200SNicolas Dichtel } 7667a674200SNicolas Dichtel if (h == NETDEV_HASHENTRIES) { 7677a674200SNicolas Dichtel if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL, 7687a674200SNicolas Dichtel net->ipv6.devconf_all, 7697a674200SNicolas Dichtel NETLINK_CB(cb->skb).portid, 770addd383fSDavid Ahern nlh->nlmsg_seq, 7717a674200SNicolas Dichtel RTM_NEWNETCONF, NLM_F_MULTI, 772136ba622SZhang Shengju NETCONFA_ALL) < 0) 7737a674200SNicolas Dichtel goto done; 7747a674200SNicolas Dichtel else 7757a674200SNicolas Dichtel h++; 7767a674200SNicolas Dichtel } 7777a674200SNicolas Dichtel if (h == NETDEV_HASHENTRIES + 1) { 7787a674200SNicolas Dichtel if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT, 7797a674200SNicolas Dichtel net->ipv6.devconf_dflt, 7807a674200SNicolas Dichtel NETLINK_CB(cb->skb).portid, 781addd383fSDavid Ahern nlh->nlmsg_seq, 7827a674200SNicolas Dichtel RTM_NEWNETCONF, NLM_F_MULTI, 783136ba622SZhang Shengju NETCONFA_ALL) < 0) 7847a674200SNicolas Dichtel goto done; 7857a674200SNicolas Dichtel else 7867a674200SNicolas Dichtel h++; 7877a674200SNicolas Dichtel } 7887a674200SNicolas Dichtel done: 7897a674200SNicolas Dichtel cb->args[0] = h; 7907a674200SNicolas Dichtel cb->args[1] = idx; 7917a674200SNicolas Dichtel 7927a674200SNicolas Dichtel return skb->len; 7937a674200SNicolas Dichtel } 7947a674200SNicolas Dichtel 7951da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 7961da177e4SLinus Torvalds static void dev_forward_change(struct inet6_dev *idev) 7971da177e4SLinus Torvalds { 7981da177e4SLinus Torvalds struct net_device *dev; 7991da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 80051454ea4SNiels Dossche LIST_HEAD(tmp_addr_list); 8011da177e4SLinus Torvalds 8021da177e4SLinus Torvalds if (!idev) 8031da177e4SLinus Torvalds return; 8041da177e4SLinus Torvalds dev = idev->dev; 8050187bdfbSBen Hutchings if (idev->cnf.forwarding) 8060187bdfbSBen Hutchings dev_disable_lro(dev); 8071a940835SAmerigo Wang if (dev->flags & IFF_MULTICAST) { 8082c5e8933SHannes Frederic Sowa if (idev->cnf.forwarding) { 809f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters); 8102c5e8933SHannes Frederic Sowa ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters); 8112c5e8933SHannes Frederic Sowa ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters); 8122c5e8933SHannes Frederic Sowa } else { 813f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters); 8142c5e8933SHannes Frederic Sowa ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters); 8152c5e8933SHannes Frederic Sowa ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters); 8162c5e8933SHannes Frederic Sowa } 8171da177e4SLinus Torvalds } 818502a2ffdSstephen hemminger 81951454ea4SNiels Dossche read_lock_bh(&idev->lock); 820502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 8212c12a74cSMichal Wrobel if (ifa->flags&IFA_F_TENTATIVE) 8222c12a74cSMichal Wrobel continue; 82351454ea4SNiels Dossche list_add_tail(&ifa->if_list_aux, &tmp_addr_list); 82451454ea4SNiels Dossche } 82551454ea4SNiels Dossche read_unlock_bh(&idev->lock); 82651454ea4SNiels Dossche 82751454ea4SNiels Dossche while (!list_empty(&tmp_addr_list)) { 82851454ea4SNiels Dossche ifa = list_first_entry(&tmp_addr_list, 82951454ea4SNiels Dossche struct inet6_ifaddr, if_list_aux); 83051454ea4SNiels Dossche list_del(&ifa->if_list_aux); 8311da177e4SLinus Torvalds if (idev->cnf.forwarding) 8321da177e4SLinus Torvalds addrconf_join_anycast(ifa); 8331da177e4SLinus Torvalds else 8341da177e4SLinus Torvalds addrconf_leave_anycast(ifa); 8351da177e4SLinus Torvalds } 83651454ea4SNiels Dossche 83785b3daadSDavid Ahern inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF, 83885b3daadSDavid Ahern NETCONFA_FORWARDING, 839f3a1bfb1SNicolas Dichtel dev->ifindex, &idev->cnf); 8401da177e4SLinus Torvalds } 8411da177e4SLinus Torvalds 8421da177e4SLinus Torvalds 843e186932bSPavel Emelyanov static void addrconf_forward_change(struct net *net, __s32 newf) 8441da177e4SLinus Torvalds { 8451da177e4SLinus Torvalds struct net_device *dev; 8461da177e4SLinus Torvalds struct inet6_dev *idev; 8471da177e4SLinus Torvalds 8484acd4945SBen Hutchings for_each_netdev(net, dev) { 8491da177e4SLinus Torvalds idev = __in6_dev_get(dev); 8501da177e4SLinus Torvalds if (idev) { 851e186932bSPavel Emelyanov int changed = (!idev->cnf.forwarding) ^ (!newf); 852e186932bSPavel Emelyanov idev->cnf.forwarding = newf; 8531da177e4SLinus Torvalds if (changed) 8541da177e4SLinus Torvalds dev_forward_change(idev); 8551da177e4SLinus Torvalds } 8561da177e4SLinus Torvalds } 8571da177e4SLinus Torvalds } 858c8fecf22SPavel Emelyanov 859013d97e9SFrancesco Ruggeri static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf) 860c8fecf22SPavel Emelyanov { 861bff16c2fSPavel Emelyanov struct net *net; 862013d97e9SFrancesco Ruggeri int old; 863013d97e9SFrancesco Ruggeri 864013d97e9SFrancesco Ruggeri if (!rtnl_trylock()) 865013d97e9SFrancesco Ruggeri return restart_syscall(); 866bff16c2fSPavel Emelyanov 867bff16c2fSPavel Emelyanov net = (struct net *)table->extra2; 868013d97e9SFrancesco Ruggeri old = *p; 869013d97e9SFrancesco Ruggeri *p = newf; 870c8fecf22SPavel Emelyanov 871013d97e9SFrancesco Ruggeri if (p == &net->ipv6.devconf_dflt->forwarding) { 872f3a1bfb1SNicolas Dichtel if ((!newf) ^ (!old)) 87385b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 87485b3daadSDavid Ahern NETCONFA_FORWARDING, 875f3a1bfb1SNicolas Dichtel NETCONFA_IFINDEX_DEFAULT, 876f3a1bfb1SNicolas Dichtel net->ipv6.devconf_dflt); 877013d97e9SFrancesco Ruggeri rtnl_unlock(); 878013d97e9SFrancesco Ruggeri return 0; 87988af182eSEric W. Biederman } 880b325fddbSStephen Hemminger 881e186932bSPavel Emelyanov if (p == &net->ipv6.devconf_all->forwarding) { 882d26c638cSNicolas Dichtel int old_dflt = net->ipv6.devconf_dflt->forwarding; 883d26c638cSNicolas Dichtel 884e186932bSPavel Emelyanov net->ipv6.devconf_dflt->forwarding = newf; 885d26c638cSNicolas Dichtel if ((!newf) ^ (!old_dflt)) 88685b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 88785b3daadSDavid Ahern NETCONFA_FORWARDING, 888d26c638cSNicolas Dichtel NETCONFA_IFINDEX_DEFAULT, 889d26c638cSNicolas Dichtel net->ipv6.devconf_dflt); 890d26c638cSNicolas Dichtel 891e186932bSPavel Emelyanov addrconf_forward_change(net, newf); 892f3a1bfb1SNicolas Dichtel if ((!newf) ^ (!old)) 89385b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 89485b3daadSDavid Ahern NETCONFA_FORWARDING, 895f3a1bfb1SNicolas Dichtel NETCONFA_IFINDEX_ALL, 896f3a1bfb1SNicolas Dichtel net->ipv6.devconf_all); 897013d97e9SFrancesco Ruggeri } else if ((!newf) ^ (!old)) 898c8fecf22SPavel Emelyanov dev_forward_change((struct inet6_dev *)table->extra1); 8990187bdfbSBen Hutchings rtnl_unlock(); 900c8fecf22SPavel Emelyanov 901013d97e9SFrancesco Ruggeri if (newf) 9027b4da532SDaniel Lezcano rt6_purge_dflt_routers(net); 903b325fddbSStephen Hemminger return 1; 904c8fecf22SPavel Emelyanov } 90535103d11SAndy Gospodarek 90635103d11SAndy Gospodarek static void addrconf_linkdown_change(struct net *net, __s32 newf) 90735103d11SAndy Gospodarek { 90835103d11SAndy Gospodarek struct net_device *dev; 90935103d11SAndy Gospodarek struct inet6_dev *idev; 91035103d11SAndy Gospodarek 91135103d11SAndy Gospodarek for_each_netdev(net, dev) { 91235103d11SAndy Gospodarek idev = __in6_dev_get(dev); 91335103d11SAndy Gospodarek if (idev) { 91435103d11SAndy Gospodarek int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf); 91535103d11SAndy Gospodarek 91635103d11SAndy Gospodarek idev->cnf.ignore_routes_with_linkdown = newf; 91735103d11SAndy Gospodarek if (changed) 91835103d11SAndy Gospodarek inet6_netconf_notify_devconf(dev_net(dev), 91985b3daadSDavid Ahern RTM_NEWNETCONF, 92035103d11SAndy Gospodarek NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 92135103d11SAndy Gospodarek dev->ifindex, 92235103d11SAndy Gospodarek &idev->cnf); 92335103d11SAndy Gospodarek } 92435103d11SAndy Gospodarek } 92535103d11SAndy Gospodarek } 92635103d11SAndy Gospodarek 92735103d11SAndy Gospodarek static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf) 92835103d11SAndy Gospodarek { 92935103d11SAndy Gospodarek struct net *net; 93035103d11SAndy Gospodarek int old; 93135103d11SAndy Gospodarek 93235103d11SAndy Gospodarek if (!rtnl_trylock()) 93335103d11SAndy Gospodarek return restart_syscall(); 93435103d11SAndy Gospodarek 93535103d11SAndy Gospodarek net = (struct net *)table->extra2; 93635103d11SAndy Gospodarek old = *p; 93735103d11SAndy Gospodarek *p = newf; 93835103d11SAndy Gospodarek 93935103d11SAndy Gospodarek if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) { 94035103d11SAndy Gospodarek if ((!newf) ^ (!old)) 94135103d11SAndy Gospodarek inet6_netconf_notify_devconf(net, 94285b3daadSDavid Ahern RTM_NEWNETCONF, 94335103d11SAndy Gospodarek NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 94435103d11SAndy Gospodarek NETCONFA_IFINDEX_DEFAULT, 94535103d11SAndy Gospodarek net->ipv6.devconf_dflt); 94635103d11SAndy Gospodarek rtnl_unlock(); 94735103d11SAndy Gospodarek return 0; 94835103d11SAndy Gospodarek } 94935103d11SAndy Gospodarek 95035103d11SAndy Gospodarek if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) { 95135103d11SAndy Gospodarek net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf; 95235103d11SAndy Gospodarek addrconf_linkdown_change(net, newf); 95335103d11SAndy Gospodarek if ((!newf) ^ (!old)) 95435103d11SAndy Gospodarek inet6_netconf_notify_devconf(net, 95585b3daadSDavid Ahern RTM_NEWNETCONF, 95635103d11SAndy Gospodarek NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 95735103d11SAndy Gospodarek NETCONFA_IFINDEX_ALL, 95835103d11SAndy Gospodarek net->ipv6.devconf_all); 95935103d11SAndy Gospodarek } 96035103d11SAndy Gospodarek rtnl_unlock(); 96135103d11SAndy Gospodarek 96235103d11SAndy Gospodarek return 1; 96335103d11SAndy Gospodarek } 96435103d11SAndy Gospodarek 9651da177e4SLinus Torvalds #endif 9661da177e4SLinus Torvalds 9675c578aedSstephen hemminger /* Nobody refers to this ifaddr, destroy it */ 9681da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp) 9691da177e4SLinus Torvalds { 970c2e21293Sstephen hemminger WARN_ON(!hlist_unhashed(&ifp->addr_lst)); 971547b792cSIlpo Järvinen 9721da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG 97391df42beSJoe Perches pr_debug("%s\n", __func__); 9741da177e4SLinus Torvalds #endif 9751da177e4SLinus Torvalds 9761da177e4SLinus Torvalds in6_dev_put(ifp->idev); 9771da177e4SLinus Torvalds 978c15b1ccaSHannes Frederic Sowa if (cancel_delayed_work(&ifp->dad_work)) 979c15b1ccaSHannes Frederic Sowa pr_notice("delayed DAD work was pending while freeing ifa=%p\n", 980c15b1ccaSHannes Frederic Sowa ifp); 9811da177e4SLinus Torvalds 982e9d3e084SHerbert Xu if (ifp->state != INET6_IFADDR_STATE_DEAD) { 983f3213831SJoe Perches pr_warn("Freeing alive inet6 address %p\n", ifp); 9841da177e4SLinus Torvalds return; 9851da177e4SLinus Torvalds } 9861da177e4SLinus Torvalds 987e5785985SLai Jiangshan kfree_rcu(ifp, rcu); 9881da177e4SLinus Torvalds } 9891da177e4SLinus Torvalds 990e55ffac6SBrian Haley static void 991e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp) 992e55ffac6SBrian Haley { 993502a2ffdSstephen hemminger struct list_head *p; 9948a6ce0c0SYOSHIFUJI Hideaki int ifp_scope = ipv6_addr_src_scope(&ifp->addr); 995e55ffac6SBrian Haley 996e55ffac6SBrian Haley /* 997e55ffac6SBrian Haley * Each device address list is sorted in order of scope - 998e55ffac6SBrian Haley * global before linklocal. 999e55ffac6SBrian Haley */ 1000502a2ffdSstephen hemminger list_for_each(p, &idev->addr_list) { 1001502a2ffdSstephen hemminger struct inet6_ifaddr *ifa 1002502a2ffdSstephen hemminger = list_entry(p, struct inet6_ifaddr, if_list); 10038a6ce0c0SYOSHIFUJI Hideaki if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr)) 1004e55ffac6SBrian Haley break; 1005e55ffac6SBrian Haley } 1006e55ffac6SBrian Haley 10078ef802aaSEric Dumazet list_add_tail_rcu(&ifp->if_list, p); 1008e55ffac6SBrian Haley } 1009e55ffac6SBrian Haley 10103f27fb23SEric Dumazet static u32 inet6_addr_hash(const struct net *net, const struct in6_addr *addr) 10113eb84f49SYOSHIFUJI Hideaki { 10123f27fb23SEric Dumazet u32 val = ipv6_addr_hash(addr) ^ net_hash_mix(net); 10133f27fb23SEric Dumazet 10143f27fb23SEric Dumazet return hash_32(val, IN6_ADDR_HSIZE_SHIFT); 10153eb84f49SYOSHIFUJI Hideaki } 10163eb84f49SYOSHIFUJI Hideaki 101756fc709bSEric Dumazet static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr, 1018752a9292SEric Dumazet struct net_device *dev, unsigned int hash) 101956fc709bSEric Dumazet { 102056fc709bSEric Dumazet struct inet6_ifaddr *ifp; 102156fc709bSEric Dumazet 1022e66d1172SEric Dumazet hlist_for_each_entry(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) { 102356fc709bSEric Dumazet if (ipv6_addr_equal(&ifp->addr, addr)) { 102456fc709bSEric Dumazet if (!dev || ifp->idev->dev == dev) 102556fc709bSEric Dumazet return true; 102656fc709bSEric Dumazet } 102756fc709bSEric Dumazet } 102856fc709bSEric Dumazet return false; 102956fc709bSEric Dumazet } 103056fc709bSEric Dumazet 1031f3d9832eSDavid Ahern static int ipv6_add_addr_hash(struct net_device *dev, struct inet6_ifaddr *ifa) 1032f3d9832eSDavid Ahern { 1033e66d1172SEric Dumazet struct net *net = dev_net(dev); 1034e66d1172SEric Dumazet unsigned int hash = inet6_addr_hash(net, &ifa->addr); 1035f3d9832eSDavid Ahern int err = 0; 1036f3d9832eSDavid Ahern 103709eed119SEric Dumazet spin_lock_bh(&net->ipv6.addrconf_hash_lock); 1038f3d9832eSDavid Ahern 1039f3d9832eSDavid Ahern /* Ignore adding duplicate addresses on an interface */ 1040e66d1172SEric Dumazet if (ipv6_chk_same_addr(net, &ifa->addr, dev, hash)) { 1041e32ac250SJoe Perches netdev_dbg(dev, "ipv6_add_addr: already assigned\n"); 1042f3d9832eSDavid Ahern err = -EEXIST; 1043752a9292SEric Dumazet } else { 1044e66d1172SEric Dumazet hlist_add_head_rcu(&ifa->addr_lst, &net->ipv6.inet6_addr_lst[hash]); 1045f3d9832eSDavid Ahern } 1046f3d9832eSDavid Ahern 104709eed119SEric Dumazet spin_unlock_bh(&net->ipv6.addrconf_hash_lock); 1048f3d9832eSDavid Ahern 1049f3d9832eSDavid Ahern return err; 1050f3d9832eSDavid Ahern } 1051f3d9832eSDavid Ahern 10521da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */ 10531da177e4SLinus Torvalds 10541da177e4SLinus Torvalds static struct inet6_ifaddr * 1055e6464b8cSDavid Ahern ipv6_add_addr(struct inet6_dev *idev, struct ifa6_config *cfg, 1056de95e047SDavid Ahern bool can_block, struct netlink_ext_ack *extack) 10571da177e4SLinus Torvalds { 1058f3d9832eSDavid Ahern gfp_t gfp_flags = can_block ? GFP_KERNEL : GFP_ATOMIC; 1059e6464b8cSDavid Ahern int addr_type = ipv6_addr_type(cfg->pfx); 1060df789fe7SDavid Forster struct net *net = dev_net(idev->dev); 10611da177e4SLinus Torvalds struct inet6_ifaddr *ifa = NULL; 1062360a9887SDavid Ahern struct fib6_info *f6i = NULL; 10631da177e4SLinus Torvalds int err = 0; 1064d68b8270SYOSHIFUJI Hideaki 1065d68b8270SYOSHIFUJI Hideaki if (addr_type == IPV6_ADDR_ANY || 1066f17f7648SHangbin Liu (addr_type & IPV6_ADDR_MULTICAST && 1067f17f7648SHangbin Liu !(cfg->ifa_flags & IFA_F_MCAUTOJOIN)) || 1068d68b8270SYOSHIFUJI Hideaki (!(idev->dev->flags & IFF_LOOPBACK) && 10693ede0bbcSRobert Shearman !netif_is_l3_master(idev->dev) && 1070d68b8270SYOSHIFUJI Hideaki addr_type & IPV6_ADDR_LOOPBACK)) 1071d68b8270SYOSHIFUJI Hideaki return ERR_PTR(-EADDRNOTAVAIL); 10721da177e4SLinus Torvalds 10731da177e4SLinus Torvalds if (idev->dead) { 10741da177e4SLinus Torvalds err = -ENODEV; /*XXX*/ 1075f3d9832eSDavid Ahern goto out; 10761da177e4SLinus Torvalds } 10771da177e4SLinus Torvalds 107856d417b1SBrian Haley if (idev->cnf.disable_ipv6) { 10799bdd8d40SBrian Haley err = -EACCES; 1080f3d9832eSDavid Ahern goto out; 10819bdd8d40SBrian Haley } 10829bdd8d40SBrian Haley 1083ff7883eaSDavid Ahern /* validator notifier needs to be blocking; 1084ff7883eaSDavid Ahern * do not call in atomic context 1085ff7883eaSDavid Ahern */ 1086ff7883eaSDavid Ahern if (can_block) { 1087ff7883eaSDavid Ahern struct in6_validator_info i6vi = { 1088e6464b8cSDavid Ahern .i6vi_addr = *cfg->pfx, 1089ff7883eaSDavid Ahern .i6vi_dev = idev, 1090de95e047SDavid Ahern .extack = extack, 1091ff7883eaSDavid Ahern }; 1092ff7883eaSDavid Ahern 10933ad7d246SKrister Johansen err = inet6addr_validator_notifier_call_chain(NETDEV_UP, &i6vi); 10943ad7d246SKrister Johansen err = notifier_to_errno(err); 1095f3d9832eSDavid Ahern if (err < 0) 10961da177e4SLinus Torvalds goto out; 1097ff7883eaSDavid Ahern } 10981da177e4SLinus Torvalds 10996126891cSVasily Averin ifa = kzalloc(sizeof(*ifa), gfp_flags | __GFP_ACCOUNT); 110063159f29SIan Morris if (!ifa) { 11011da177e4SLinus Torvalds err = -ENOBUFS; 11021da177e4SLinus Torvalds goto out; 11031da177e4SLinus Torvalds } 11041da177e4SLinus Torvalds 1105e6464b8cSDavid Ahern f6i = addrconf_f6i_alloc(net, idev, cfg->pfx, false, gfp_flags); 1106360a9887SDavid Ahern if (IS_ERR(f6i)) { 1107360a9887SDavid Ahern err = PTR_ERR(f6i); 1108360a9887SDavid Ahern f6i = NULL; 11091da177e4SLinus Torvalds goto out; 11101da177e4SLinus Torvalds } 11111da177e4SLinus Torvalds 1112bba24896SJiri Pirko neigh_parms_data_state_setall(idev->nd_parms); 1113bba24896SJiri Pirko 1114e6464b8cSDavid Ahern ifa->addr = *cfg->pfx; 1115e6464b8cSDavid Ahern if (cfg->peer_pfx) 1116e6464b8cSDavid Ahern ifa->peer_addr = *cfg->peer_pfx; 11171da177e4SLinus Torvalds 11181da177e4SLinus Torvalds spin_lock_init(&ifa->lock); 1119c15b1ccaSHannes Frederic Sowa INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work); 1120c2e21293Sstephen hemminger INIT_HLIST_NODE(&ifa->addr_lst); 1121e6464b8cSDavid Ahern ifa->scope = cfg->scope; 1122e6464b8cSDavid Ahern ifa->prefix_len = cfg->plen; 11238308f3ffSDavid Ahern ifa->rt_priority = cfg->rt_priority; 1124e6464b8cSDavid Ahern ifa->flags = cfg->ifa_flags; 112547f0bd50SJacques de Laval ifa->ifa_proto = cfg->ifa_proto; 112666eb9f86SMahesh Bandewar /* No need to add the TENTATIVE flag for addresses with NODAD */ 1127e6464b8cSDavid Ahern if (!(cfg->ifa_flags & IFA_F_NODAD)) 112866eb9f86SMahesh Bandewar ifa->flags |= IFA_F_TENTATIVE; 1129e6464b8cSDavid Ahern ifa->valid_lft = cfg->valid_lft; 1130e6464b8cSDavid Ahern ifa->prefered_lft = cfg->preferred_lft; 11311da177e4SLinus Torvalds ifa->cstamp = ifa->tstamp = jiffies; 1132617fe29dSDaniel Borkmann ifa->tokenized = false; 11331da177e4SLinus Torvalds 1134360a9887SDavid Ahern ifa->rt = f6i; 113557f5f544SKeir Fraser 11361da177e4SLinus Torvalds ifa->idev = idev; 1137f3d9832eSDavid Ahern in6_dev_hold(idev); 1138f3d9832eSDavid Ahern 11391da177e4SLinus Torvalds /* For caller */ 1140271201c0SReshetova, Elena refcount_set(&ifa->refcnt, 1); 11411da177e4SLinus Torvalds 114209eed119SEric Dumazet rcu_read_lock(); 11431da177e4SLinus Torvalds 1144f3d9832eSDavid Ahern err = ipv6_add_addr_hash(idev->dev, ifa); 1145f3d9832eSDavid Ahern if (err < 0) { 114609eed119SEric Dumazet rcu_read_unlock(); 1147f3d9832eSDavid Ahern goto out; 1148f3d9832eSDavid Ahern } 11491da177e4SLinus Torvalds 115009eed119SEric Dumazet write_lock_bh(&idev->lock); 1151f3d9832eSDavid Ahern 11521da177e4SLinus Torvalds /* Add to inet6_dev unicast addr list. */ 1153e55ffac6SBrian Haley ipv6_link_dev_addr(idev, ifa); 11541da177e4SLinus Torvalds 11551da177e4SLinus Torvalds if (ifa->flags&IFA_F_TEMPORARY) { 1156372e6c8fSstephen hemminger list_add(&ifa->tmp_list, &idev->tempaddr_list); 11571da177e4SLinus Torvalds in6_ifa_hold(ifa); 11581da177e4SLinus Torvalds } 11591da177e4SLinus Torvalds 11601da177e4SLinus Torvalds in6_ifa_hold(ifa); 116109eed119SEric Dumazet write_unlock_bh(&idev->lock); 1162f3d9832eSDavid Ahern 116309eed119SEric Dumazet rcu_read_unlock(); 11641da177e4SLinus Torvalds 1165f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_UP, ifa); 1166f3d9832eSDavid Ahern out: 1167f3d9832eSDavid Ahern if (unlikely(err < 0)) { 1168360a9887SDavid Ahern fib6_info_release(f6i); 116993531c67SDavid Ahern 1170f3d9832eSDavid Ahern if (ifa) { 1171f3d9832eSDavid Ahern if (ifa->idev) 1172f3d9832eSDavid Ahern in6_dev_put(ifa->idev); 11731da177e4SLinus Torvalds kfree(ifa); 1174f3d9832eSDavid Ahern } 11751da177e4SLinus Torvalds ifa = ERR_PTR(err); 11761da177e4SLinus Torvalds } 11771da177e4SLinus Torvalds 11781da177e4SLinus Torvalds return ifa; 11791da177e4SLinus Torvalds } 11801da177e4SLinus Torvalds 11815b84efecSThomas Haller enum cleanup_prefix_rt_t { 11825b84efecSThomas Haller CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */ 11835b84efecSThomas Haller CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */ 11845b84efecSThomas Haller CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */ 11855b84efecSThomas Haller }; 11865b84efecSThomas Haller 11875b84efecSThomas Haller /* 11885b84efecSThomas Haller * Check, whether the prefix for ifp would still need a prefix route 11895b84efecSThomas Haller * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_* 11905b84efecSThomas Haller * constants. 11915b84efecSThomas Haller * 11925b84efecSThomas Haller * 1) we don't purge prefix if address was not permanent. 11935b84efecSThomas Haller * prefix is managed by its own lifetime. 11945b84efecSThomas Haller * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE. 11955b84efecSThomas Haller * 3) if there are no addresses, delete prefix. 11965b84efecSThomas Haller * 4) if there are still other permanent address(es), 11975b84efecSThomas Haller * corresponding prefix is still permanent. 11985b84efecSThomas Haller * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE, 11995b84efecSThomas Haller * don't purge the prefix, assume user space is managing it. 12005b84efecSThomas Haller * 6) otherwise, update prefix lifetime to the 12015b84efecSThomas Haller * longest valid lifetime among the corresponding 12025b84efecSThomas Haller * addresses on the device. 12035b84efecSThomas Haller * Note: subsequent RA will update lifetime. 12045b84efecSThomas Haller **/ 12055b84efecSThomas Haller static enum cleanup_prefix_rt_t 12065b84efecSThomas Haller check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires) 12075b84efecSThomas Haller { 12085b84efecSThomas Haller struct inet6_ifaddr *ifa; 12095b84efecSThomas Haller struct inet6_dev *idev = ifp->idev; 12105b84efecSThomas Haller unsigned long lifetime; 12115b84efecSThomas Haller enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL; 12125b84efecSThomas Haller 12135b84efecSThomas Haller *expires = jiffies; 12145b84efecSThomas Haller 12155b84efecSThomas Haller list_for_each_entry(ifa, &idev->addr_list, if_list) { 12165b84efecSThomas Haller if (ifa == ifp) 12175b84efecSThomas Haller continue; 1218e75913c9SZhiqiang Liu if (ifa->prefix_len != ifp->prefix_len || 1219e75913c9SZhiqiang Liu !ipv6_prefix_equal(&ifa->addr, &ifp->addr, 12205b84efecSThomas Haller ifp->prefix_len)) 12215b84efecSThomas Haller continue; 12225b84efecSThomas Haller if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE)) 12235b84efecSThomas Haller return CLEANUP_PREFIX_RT_NOP; 12245b84efecSThomas Haller 12255b84efecSThomas Haller action = CLEANUP_PREFIX_RT_EXPIRE; 12265b84efecSThomas Haller 12275b84efecSThomas Haller spin_lock(&ifa->lock); 12285b84efecSThomas Haller 12295b84efecSThomas Haller lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ); 12305b84efecSThomas Haller /* 12315b84efecSThomas Haller * Note: Because this address is 12325b84efecSThomas Haller * not permanent, lifetime < 12335b84efecSThomas Haller * LONG_MAX / HZ here. 12345b84efecSThomas Haller */ 12355b84efecSThomas Haller if (time_before(*expires, ifa->tstamp + lifetime * HZ)) 12365b84efecSThomas Haller *expires = ifa->tstamp + lifetime * HZ; 12375b84efecSThomas Haller spin_unlock(&ifa->lock); 12385b84efecSThomas Haller } 12395b84efecSThomas Haller 12405b84efecSThomas Haller return action; 12415b84efecSThomas Haller } 12425b84efecSThomas Haller 12435b84efecSThomas Haller static void 1244d0098e4cSHangbin Liu cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, 1245d0098e4cSHangbin Liu bool del_rt, bool del_peer) 12465b84efecSThomas Haller { 124793c2fb25SDavid Ahern struct fib6_info *f6i; 12485b84efecSThomas Haller 1249d0098e4cSHangbin Liu f6i = addrconf_get_prefix_route(del_peer ? &ifp->peer_addr : &ifp->addr, 1250d0098e4cSHangbin Liu ifp->prefix_len, 12512b2450caSDavid Ahern ifp->idev->dev, 0, RTF_DEFAULT, true); 125293c2fb25SDavid Ahern if (f6i) { 12535b84efecSThomas Haller if (del_rt) 125411dd74b3SRoopa Prabhu ip6_del_rt(dev_net(ifp->idev->dev), f6i, false); 12555b84efecSThomas Haller else { 125693c2fb25SDavid Ahern if (!(f6i->fib6_flags & RTF_EXPIRES)) 125793c2fb25SDavid Ahern fib6_set_expires(f6i, expires); 125893c2fb25SDavid Ahern fib6_info_release(f6i); 12595b84efecSThomas Haller } 12605b84efecSThomas Haller } 12615b84efecSThomas Haller } 12625b84efecSThomas Haller 12635b84efecSThomas Haller 12641da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */ 12651da177e4SLinus Torvalds 12661da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp) 12671da177e4SLinus Torvalds { 12685b84efecSThomas Haller enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP; 1269e66d1172SEric Dumazet struct net *net = dev_net(ifp->idev->dev); 12705b84efecSThomas Haller unsigned long expires; 1271e66d1172SEric Dumazet int state; 12721da177e4SLinus Torvalds 1273c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 1274c15b1ccaSHannes Frederic Sowa 12758e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 12764c5ff6a6SHerbert Xu state = ifp->state; 1277e9d3e084SHerbert Xu ifp->state = INET6_IFADDR_STATE_DEAD; 12788e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 12794c5ff6a6SHerbert Xu 12804c5ff6a6SHerbert Xu if (state == INET6_IFADDR_STATE_DEAD) 12814c5ff6a6SHerbert Xu goto out; 12821da177e4SLinus Torvalds 1283e66d1172SEric Dumazet spin_lock_bh(&net->ipv6.addrconf_hash_lock); 12845c578aedSstephen hemminger hlist_del_init_rcu(&ifp->addr_lst); 1285e66d1172SEric Dumazet spin_unlock_bh(&net->ipv6.addrconf_hash_lock); 12861da177e4SLinus Torvalds 12875b84efecSThomas Haller write_lock_bh(&ifp->idev->lock); 12885d9efa7eSDavid S. Miller 12891da177e4SLinus Torvalds if (ifp->flags&IFA_F_TEMPORARY) { 1290372e6c8fSstephen hemminger list_del(&ifp->tmp_list); 12911da177e4SLinus Torvalds if (ifp->ifpub) { 12921da177e4SLinus Torvalds in6_ifa_put(ifp->ifpub); 12931da177e4SLinus Torvalds ifp->ifpub = NULL; 12941da177e4SLinus Torvalds } 12951da177e4SLinus Torvalds __in6_ifa_put(ifp); 12961da177e4SLinus Torvalds } 12971da177e4SLinus Torvalds 12985b84efecSThomas Haller if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE)) 12995b84efecSThomas Haller action = check_cleanup_prefix_route(ifp, &expires); 13005b84efecSThomas Haller 13018ef802aaSEric Dumazet list_del_rcu(&ifp->if_list); 13021da177e4SLinus Torvalds __in6_ifa_put(ifp); 1303502a2ffdSstephen hemminger 13045b84efecSThomas Haller write_unlock_bh(&ifp->idev->lock); 13051da177e4SLinus Torvalds 1306c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifp); 1307b2238566SAndrey Vagin 13081da177e4SLinus Torvalds ipv6_ifa_notify(RTM_DELADDR, ifp); 13091da177e4SLinus Torvalds 1310f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_DOWN, ifp); 13111da177e4SLinus Torvalds 13125b84efecSThomas Haller if (action != CLEANUP_PREFIX_RT_NOP) { 13135b84efecSThomas Haller cleanup_prefix_route(ifp, expires, 1314d0098e4cSHangbin Liu action == CLEANUP_PREFIX_RT_DEL, false); 13151da177e4SLinus Torvalds } 13161da177e4SLinus Torvalds 1317c3968a85SDaniel Walter /* clean up prefsrc entries */ 1318c3968a85SDaniel Walter rt6_remove_prefsrc(ifp); 13194c5ff6a6SHerbert Xu out: 13201da177e4SLinus Torvalds in6_ifa_put(ifp); 13211da177e4SLinus Torvalds } 13221da177e4SLinus Torvalds 1323969c5464SFernando Gont static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, bool block) 13241da177e4SLinus Torvalds { 13251da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 1326e6464b8cSDavid Ahern unsigned long tmp_tstamp, age; 1327eac55bf9SBenoit Boissinot unsigned long regen_advance; 132876f793e3SLorenzo Colitti unsigned long now = jiffies; 13297aa8e63fSJiri Bohac s32 cnf_temp_preferred_lft; 1330969c5464SFernando Gont struct inet6_ifaddr *ift; 1331969c5464SFernando Gont struct ifa6_config cfg; 1332969c5464SFernando Gont long max_desync_factor; 1333969c5464SFernando Gont struct in6_addr addr; 1334969c5464SFernando Gont int ret = 0; 13351da177e4SLinus Torvalds 133653bd6749SJiri Pirko write_lock_bh(&idev->lock); 1337969c5464SFernando Gont 13381da177e4SLinus Torvalds retry: 13391da177e4SLinus Torvalds in6_dev_hold(idev); 13401da177e4SLinus Torvalds if (idev->cnf.use_tempaddr <= 0) { 134153bd6749SJiri Pirko write_unlock_bh(&idev->lock); 1342f3213831SJoe Perches pr_info("%s: use_tempaddr is disabled\n", __func__); 13431da177e4SLinus Torvalds in6_dev_put(idev); 13441da177e4SLinus Torvalds ret = -1; 13451da177e4SLinus Torvalds goto out; 13461da177e4SLinus Torvalds } 13471da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 13481da177e4SLinus Torvalds if (ifp->regen_count++ >= idev->cnf.regen_max_retry) { 13491da177e4SLinus Torvalds idev->cnf.use_tempaddr = -1; /*XXX*/ 13501da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 135153bd6749SJiri Pirko write_unlock_bh(&idev->lock); 1352f3213831SJoe Perches pr_warn("%s: regeneration time exceeded - disabled temporary address support\n", 1353f3213831SJoe Perches __func__); 13541da177e4SLinus Torvalds in6_dev_put(idev); 13551da177e4SLinus Torvalds ret = -1; 13561da177e4SLinus Torvalds goto out; 13571da177e4SLinus Torvalds } 13581da177e4SLinus Torvalds in6_ifa_hold(ifp); 13591da177e4SLinus Torvalds memcpy(addr.s6_addr, ifp->addr.s6_addr, 8); 1360969c5464SFernando Gont ipv6_gen_rnd_iid(&addr); 1361969c5464SFernando Gont 136276f793e3SLorenzo Colitti age = (now - ifp->tstamp) / HZ; 13631da177e4SLinus Torvalds 1364eac55bf9SBenoit Boissinot regen_advance = idev->cnf.regen_max_retry * 1365eac55bf9SBenoit Boissinot idev->cnf.dad_transmits * 136619e16d22SHangbin Liu max(NEIGH_VAR(idev->nd_parms, RETRANS_TIME), HZ/100) / HZ; 136776506a98SJiri Bohac 136876506a98SJiri Bohac /* recalculate max_desync_factor each time and update 136976506a98SJiri Bohac * idev->desync_factor if it's larger 137076506a98SJiri Bohac */ 13717aa8e63fSJiri Bohac cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft); 137276506a98SJiri Bohac max_desync_factor = min_t(__u32, 137376506a98SJiri Bohac idev->cnf.max_desync_factor, 13747aa8e63fSJiri Bohac cnf_temp_preferred_lft - regen_advance); 137576506a98SJiri Bohac 137676506a98SJiri Bohac if (unlikely(idev->desync_factor > max_desync_factor)) { 137776506a98SJiri Bohac if (max_desync_factor > 0) { 137876506a98SJiri Bohac get_random_bytes(&idev->desync_factor, 137976506a98SJiri Bohac sizeof(idev->desync_factor)); 138076506a98SJiri Bohac idev->desync_factor %= max_desync_factor; 138176506a98SJiri Bohac } else { 138276506a98SJiri Bohac idev->desync_factor = 0; 138376506a98SJiri Bohac } 138476506a98SJiri Bohac } 138576506a98SJiri Bohac 13863f2d67b6SDavid Ahern memset(&cfg, 0, sizeof(cfg)); 1387e6464b8cSDavid Ahern cfg.valid_lft = min_t(__u32, ifp->valid_lft, 138876506a98SJiri Bohac idev->cnf.temp_valid_lft + age); 1389e6464b8cSDavid Ahern cfg.preferred_lft = cnf_temp_preferred_lft + age - idev->desync_factor; 1390e6464b8cSDavid Ahern cfg.preferred_lft = min_t(__u32, ifp->prefered_lft, cfg.preferred_lft); 1391e6464b8cSDavid Ahern 1392e6464b8cSDavid Ahern cfg.plen = ifp->prefix_len; 139376506a98SJiri Bohac tmp_tstamp = ifp->tstamp; 139476506a98SJiri Bohac spin_unlock_bh(&ifp->lock); 139576506a98SJiri Bohac 139653bd6749SJiri Pirko write_unlock_bh(&idev->lock); 139795c385b4SNeil Horman 1398eac55bf9SBenoit Boissinot /* A temporary address is created only if this calculated Preferred 1399eac55bf9SBenoit Boissinot * Lifetime is greater than REGEN_ADVANCE time units. In particular, 1400eac55bf9SBenoit Boissinot * an implementation must not create a temporary address with a zero 1401eac55bf9SBenoit Boissinot * Preferred Lifetime. 1402ecab6701SHeiner Kallweit * Use age calculation as in addrconf_verify to avoid unnecessary 1403ecab6701SHeiner Kallweit * temporary addresses being generated. 1404eac55bf9SBenoit Boissinot */ 1405ecab6701SHeiner Kallweit age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; 1406e6464b8cSDavid Ahern if (cfg.preferred_lft <= regen_advance + age) { 1407eac55bf9SBenoit Boissinot in6_ifa_put(ifp); 1408eac55bf9SBenoit Boissinot in6_dev_put(idev); 1409eac55bf9SBenoit Boissinot ret = -1; 1410eac55bf9SBenoit Boissinot goto out; 1411eac55bf9SBenoit Boissinot } 1412eac55bf9SBenoit Boissinot 1413e6464b8cSDavid Ahern cfg.ifa_flags = IFA_F_TEMPORARY; 141495c385b4SNeil Horman /* set in addrconf_prefix_rcv() */ 141595c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 1416e6464b8cSDavid Ahern cfg.ifa_flags |= IFA_F_OPTIMISTIC; 141795c385b4SNeil Horman 1418e6464b8cSDavid Ahern cfg.pfx = &addr; 1419e6464b8cSDavid Ahern cfg.scope = ipv6_addr_scope(cfg.pfx); 1420e6464b8cSDavid Ahern 1421e6464b8cSDavid Ahern ift = ipv6_add_addr(idev, &cfg, block, NULL); 14224b08a8f1SHannes Frederic Sowa if (IS_ERR(ift)) { 14231da177e4SLinus Torvalds in6_ifa_put(ifp); 14241da177e4SLinus Torvalds in6_dev_put(idev); 1425f3213831SJoe Perches pr_info("%s: retry temporary address regeneration\n", __func__); 142653bd6749SJiri Pirko write_lock_bh(&idev->lock); 14271da177e4SLinus Torvalds goto retry; 14281da177e4SLinus Torvalds } 14291da177e4SLinus Torvalds 14301da177e4SLinus Torvalds spin_lock_bh(&ift->lock); 14311da177e4SLinus Torvalds ift->ifpub = ifp; 143276f793e3SLorenzo Colitti ift->cstamp = now; 14331da177e4SLinus Torvalds ift->tstamp = tmp_tstamp; 14341da177e4SLinus Torvalds spin_unlock_bh(&ift->lock); 14351da177e4SLinus Torvalds 1436cf22f9a2SDavid S. Miller addrconf_dad_start(ift); 14371da177e4SLinus Torvalds in6_ifa_put(ift); 14381da177e4SLinus Torvalds in6_dev_put(idev); 14391da177e4SLinus Torvalds out: 14401da177e4SLinus Torvalds return ret; 14411da177e4SLinus Torvalds } 14421da177e4SLinus Torvalds 14431da177e4SLinus Torvalds /* 1444072047e4SYOSHIFUJI Hideaki * Choose an appropriate source address (RFC3484) 14451da177e4SLinus Torvalds */ 1446a9b05723SYOSHIFUJI Hideaki enum { 1447a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_INIT = 0, 1448a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_LOCAL, 1449a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_SCOPE, 1450a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PREFERRED, 1451a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6 1452a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_HOA, 1453a9b05723SYOSHIFUJI Hideaki #endif 1454a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_OIF, 1455a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_LABEL, 1456a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PRIVACY, 1457a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_ORCHID, 1458a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PREFIX, 14597fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 14607fd2561eSErik Kline IPV6_SADDR_RULE_NOT_OPTIMISTIC, 14617fd2561eSErik Kline #endif 1462a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_MAX 1463072047e4SYOSHIFUJI Hideaki }; 1464072047e4SYOSHIFUJI Hideaki 1465a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score { 1466a9b05723SYOSHIFUJI Hideaki int rule; 1467a9b05723SYOSHIFUJI Hideaki int addr_type; 1468a9b05723SYOSHIFUJI Hideaki struct inet6_ifaddr *ifa; 1469a9b05723SYOSHIFUJI Hideaki DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX); 1470a9b05723SYOSHIFUJI Hideaki int scopedist; 1471a9b05723SYOSHIFUJI Hideaki int matchlen; 1472a9b05723SYOSHIFUJI Hideaki }; 1473a9b05723SYOSHIFUJI Hideaki 1474a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst { 14759acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *addr; 1476a9b05723SYOSHIFUJI Hideaki int ifindex; 1477a9b05723SYOSHIFUJI Hideaki int scope; 1478a9b05723SYOSHIFUJI Hideaki int label; 14797cbca67cSYOSHIFUJI Hideaki unsigned int prefs; 1480a9b05723SYOSHIFUJI Hideaki }; 1481072047e4SYOSHIFUJI Hideaki 1482b6f99a21SDave Jones static inline int ipv6_saddr_preferred(int type) 14831da177e4SLinus Torvalds { 148445bb0060SUlrich Weber if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK)) 1485072047e4SYOSHIFUJI Hideaki return 1; 1486072047e4SYOSHIFUJI Hideaki return 0; 14871da177e4SLinus Torvalds } 14881da177e4SLinus Torvalds 148935e015e1SMatteo Croce static bool ipv6_use_optimistic_addr(struct net *net, 149035e015e1SMatteo Croce struct inet6_dev *idev) 14917fd2561eSErik Kline { 14927fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 149335e015e1SMatteo Croce if (!idev) 149435e015e1SMatteo Croce return false; 149535e015e1SMatteo Croce if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad) 149635e015e1SMatteo Croce return false; 149735e015e1SMatteo Croce if (!net->ipv6.devconf_all->use_optimistic && !idev->cnf.use_optimistic) 149835e015e1SMatteo Croce return false; 149935e015e1SMatteo Croce 150035e015e1SMatteo Croce return true; 15017fd2561eSErik Kline #else 15027fd2561eSErik Kline return false; 15037fd2561eSErik Kline #endif 15047fd2561eSErik Kline } 15057fd2561eSErik Kline 1506f1c02cfbSSabrina Dubroca static bool ipv6_allow_optimistic_dad(struct net *net, 1507f1c02cfbSSabrina Dubroca struct inet6_dev *idev) 1508f1c02cfbSSabrina Dubroca { 1509f1c02cfbSSabrina Dubroca #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 1510f1c02cfbSSabrina Dubroca if (!idev) 1511f1c02cfbSSabrina Dubroca return false; 1512f1c02cfbSSabrina Dubroca if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad) 1513f1c02cfbSSabrina Dubroca return false; 1514f1c02cfbSSabrina Dubroca 1515f1c02cfbSSabrina Dubroca return true; 1516f1c02cfbSSabrina Dubroca #else 1517f1c02cfbSSabrina Dubroca return false; 1518f1c02cfbSSabrina Dubroca #endif 1519f1c02cfbSSabrina Dubroca } 1520f1c02cfbSSabrina Dubroca 15213de23255SBenjamin Thery static int ipv6_get_saddr_eval(struct net *net, 15223de23255SBenjamin Thery struct ipv6_saddr_score *score, 1523a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst *dst, 1524a9b05723SYOSHIFUJI Hideaki int i) 1525a9b05723SYOSHIFUJI Hideaki { 1526a9b05723SYOSHIFUJI Hideaki int ret; 1527a9b05723SYOSHIFUJI Hideaki 1528a9b05723SYOSHIFUJI Hideaki if (i <= score->rule) { 1529a9b05723SYOSHIFUJI Hideaki switch (i) { 1530a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_SCOPE: 1531a9b05723SYOSHIFUJI Hideaki ret = score->scopedist; 1532a9b05723SYOSHIFUJI Hideaki break; 1533a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFIX: 1534a9b05723SYOSHIFUJI Hideaki ret = score->matchlen; 1535a9b05723SYOSHIFUJI Hideaki break; 1536a9b05723SYOSHIFUJI Hideaki default: 1537a9b05723SYOSHIFUJI Hideaki ret = !!test_bit(i, score->scorebits); 1538a9b05723SYOSHIFUJI Hideaki } 1539a9b05723SYOSHIFUJI Hideaki goto out; 1540a9b05723SYOSHIFUJI Hideaki } 1541a9b05723SYOSHIFUJI Hideaki 1542a9b05723SYOSHIFUJI Hideaki switch (i) { 1543a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_INIT: 1544a9b05723SYOSHIFUJI Hideaki /* Rule 0: remember if hiscore is not ready yet */ 1545a9b05723SYOSHIFUJI Hideaki ret = !!score->ifa; 1546a9b05723SYOSHIFUJI Hideaki break; 1547a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_LOCAL: 1548a9b05723SYOSHIFUJI Hideaki /* Rule 1: Prefer same address */ 1549a9b05723SYOSHIFUJI Hideaki ret = ipv6_addr_equal(&score->ifa->addr, dst->addr); 1550a9b05723SYOSHIFUJI Hideaki break; 1551a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_SCOPE: 1552a9b05723SYOSHIFUJI Hideaki /* Rule 2: Prefer appropriate scope 1553a9b05723SYOSHIFUJI Hideaki * 1554a9b05723SYOSHIFUJI Hideaki * ret 1555a9b05723SYOSHIFUJI Hideaki * ^ 1556a9b05723SYOSHIFUJI Hideaki * -1 | d 15 1557a9b05723SYOSHIFUJI Hideaki * ---+--+-+---> scope 1558a9b05723SYOSHIFUJI Hideaki * | 1559a9b05723SYOSHIFUJI Hideaki * | d is scope of the destination. 1560a9b05723SYOSHIFUJI Hideaki * B-d | \ 1561a9b05723SYOSHIFUJI Hideaki * | \ <- smaller scope is better if 1562db9c7c39Sstephen hemminger * B-15 | \ if scope is enough for destination. 1563a9b05723SYOSHIFUJI Hideaki * | ret = B - scope (-1 <= scope >= d <= 15). 1564a9b05723SYOSHIFUJI Hideaki * d-C-1 | / 1565a9b05723SYOSHIFUJI Hideaki * |/ <- greater is better 1566a9b05723SYOSHIFUJI Hideaki * -C / if scope is not enough for destination. 1567a9b05723SYOSHIFUJI Hideaki * /| ret = scope - C (-1 <= d < scope <= 15). 1568a9b05723SYOSHIFUJI Hideaki * 1569a9b05723SYOSHIFUJI Hideaki * d - C - 1 < B -15 (for all -1 <= d <= 15). 1570a9b05723SYOSHIFUJI Hideaki * C > d + 14 - B >= 15 + 14 - B = 29 - B. 1571a9b05723SYOSHIFUJI Hideaki * Assume B = 0 and we get C > 29. 1572a9b05723SYOSHIFUJI Hideaki */ 1573a9b05723SYOSHIFUJI Hideaki ret = __ipv6_addr_src_scope(score->addr_type); 1574a9b05723SYOSHIFUJI Hideaki if (ret >= dst->scope) 1575a9b05723SYOSHIFUJI Hideaki ret = -ret; 1576a9b05723SYOSHIFUJI Hideaki else 1577a9b05723SYOSHIFUJI Hideaki ret -= 128; /* 30 is enough */ 1578a9b05723SYOSHIFUJI Hideaki score->scopedist = ret; 1579a9b05723SYOSHIFUJI Hideaki break; 1580a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFERRED: 15817fd2561eSErik Kline { 1582a9b05723SYOSHIFUJI Hideaki /* Rule 3: Avoid deprecated and optimistic addresses */ 15837fd2561eSErik Kline u8 avoid = IFA_F_DEPRECATED; 15847fd2561eSErik Kline 158535e015e1SMatteo Croce if (!ipv6_use_optimistic_addr(net, score->ifa->idev)) 15867fd2561eSErik Kline avoid |= IFA_F_OPTIMISTIC; 1587a9b05723SYOSHIFUJI Hideaki ret = ipv6_saddr_preferred(score->addr_type) || 15887fd2561eSErik Kline !(score->ifa->flags & avoid); 1589a9b05723SYOSHIFUJI Hideaki break; 15907fd2561eSErik Kline } 1591a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6 1592a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_HOA: 15937cbca67cSYOSHIFUJI Hideaki { 1594a9b05723SYOSHIFUJI Hideaki /* Rule 4: Prefer home address */ 15957cbca67cSYOSHIFUJI Hideaki int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA); 15967cbca67cSYOSHIFUJI Hideaki ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome; 1597a9b05723SYOSHIFUJI Hideaki break; 15987cbca67cSYOSHIFUJI Hideaki } 1599a9b05723SYOSHIFUJI Hideaki #endif 1600a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_OIF: 1601a9b05723SYOSHIFUJI Hideaki /* Rule 5: Prefer outgoing interface */ 1602a9b05723SYOSHIFUJI Hideaki ret = (!dst->ifindex || 1603a9b05723SYOSHIFUJI Hideaki dst->ifindex == score->ifa->idev->dev->ifindex); 1604a9b05723SYOSHIFUJI Hideaki break; 1605a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_LABEL: 1606a9b05723SYOSHIFUJI Hideaki /* Rule 6: Prefer matching label */ 16073de23255SBenjamin Thery ret = ipv6_addr_label(net, 16083de23255SBenjamin Thery &score->ifa->addr, score->addr_type, 1609a9b05723SYOSHIFUJI Hideaki score->ifa->idev->dev->ifindex) == dst->label; 1610a9b05723SYOSHIFUJI Hideaki break; 1611a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PRIVACY: 16127cbca67cSYOSHIFUJI Hideaki { 1613a9b05723SYOSHIFUJI Hideaki /* Rule 7: Prefer public address 161425985edcSLucas De Marchi * Note: prefer temporary address if use_tempaddr >= 2 1615a9b05723SYOSHIFUJI Hideaki */ 16167cbca67cSYOSHIFUJI Hideaki int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ? 16177cbca67cSYOSHIFUJI Hideaki !!(dst->prefs & IPV6_PREFER_SRC_TMP) : 16187cbca67cSYOSHIFUJI Hideaki score->ifa->idev->cnf.use_tempaddr >= 2; 16197cbca67cSYOSHIFUJI Hideaki ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp; 1620a9b05723SYOSHIFUJI Hideaki break; 16217cbca67cSYOSHIFUJI Hideaki } 1622a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_ORCHID: 1623a9b05723SYOSHIFUJI Hideaki /* Rule 8-: Prefer ORCHID vs ORCHID or 1624a9b05723SYOSHIFUJI Hideaki * non-ORCHID vs non-ORCHID 1625a9b05723SYOSHIFUJI Hideaki */ 1626a9b05723SYOSHIFUJI Hideaki ret = !(ipv6_addr_orchid(&score->ifa->addr) ^ 1627a9b05723SYOSHIFUJI Hideaki ipv6_addr_orchid(dst->addr)); 1628a9b05723SYOSHIFUJI Hideaki break; 1629a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFIX: 1630a9b05723SYOSHIFUJI Hideaki /* Rule 8: Use longest matching prefix */ 163191b4b04fSYOSHIFUJI Hideaki / 吉藤英明 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr); 163291b4b04fSYOSHIFUJI Hideaki / 吉藤英明 if (ret > score->ifa->prefix_len) 163391b4b04fSYOSHIFUJI Hideaki / 吉藤英明 ret = score->ifa->prefix_len; 163491b4b04fSYOSHIFUJI Hideaki / 吉藤英明 score->matchlen = ret; 1635a9b05723SYOSHIFUJI Hideaki break; 16367fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 16377fd2561eSErik Kline case IPV6_SADDR_RULE_NOT_OPTIMISTIC: 16387fd2561eSErik Kline /* Optimistic addresses still have lower precedence than other 16397fd2561eSErik Kline * preferred addresses. 16407fd2561eSErik Kline */ 16417fd2561eSErik Kline ret = !(score->ifa->flags & IFA_F_OPTIMISTIC); 16427fd2561eSErik Kline break; 16437fd2561eSErik Kline #endif 1644a9b05723SYOSHIFUJI Hideaki default: 1645a9b05723SYOSHIFUJI Hideaki ret = 0; 1646a9b05723SYOSHIFUJI Hideaki } 1647a9b05723SYOSHIFUJI Hideaki 1648a9b05723SYOSHIFUJI Hideaki if (ret) 1649a9b05723SYOSHIFUJI Hideaki __set_bit(i, score->scorebits); 1650a9b05723SYOSHIFUJI Hideaki score->rule = i; 1651a9b05723SYOSHIFUJI Hideaki out: 1652a9b05723SYOSHIFUJI Hideaki return ret; 1653a9b05723SYOSHIFUJI Hideaki } 1654a9b05723SYOSHIFUJI Hideaki 1655c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 static int __ipv6_dev_get_saddr(struct net *net, 16569131f3deSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_dst *dst, 16579131f3deSYOSHIFUJI Hideaki/吉藤英明 struct inet6_dev *idev, 1658c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_score *scores, 1659c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 int hiscore_idx) 16601da177e4SLinus Torvalds { 1661c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx]; 1662072047e4SYOSHIFUJI Hideaki 1663f59c031eSEric Dumazet list_for_each_entry_rcu(score->ifa, &idev->addr_list, if_list) { 1664a9b05723SYOSHIFUJI Hideaki int i; 1665072047e4SYOSHIFUJI Hideaki 1666a9b05723SYOSHIFUJI Hideaki /* 16676b3ae80aSYOSHIFUJI Hideaki * - Tentative Address (RFC2462 section 5.4) 16686b3ae80aSYOSHIFUJI Hideaki * - A tentative address is not considered 16696b3ae80aSYOSHIFUJI Hideaki * "assigned to an interface" in the traditional 167095c385b4SNeil Horman * sense, unless it is also flagged as optimistic. 16716b3ae80aSYOSHIFUJI Hideaki * - Candidate Source Address (section 4) 1672072047e4SYOSHIFUJI Hideaki * - In any case, anycast addresses, multicast 1673072047e4SYOSHIFUJI Hideaki * addresses, and the unspecified address MUST 1674072047e4SYOSHIFUJI Hideaki * NOT be included in a candidate set. 1675072047e4SYOSHIFUJI Hideaki */ 1676a9b05723SYOSHIFUJI Hideaki if ((score->ifa->flags & IFA_F_TENTATIVE) && 1677a9b05723SYOSHIFUJI Hideaki (!(score->ifa->flags & IFA_F_OPTIMISTIC))) 16786b3ae80aSYOSHIFUJI Hideaki continue; 1679a9b05723SYOSHIFUJI Hideaki 1680a9b05723SYOSHIFUJI Hideaki score->addr_type = __ipv6_addr_type(&score->ifa->addr); 1681a9b05723SYOSHIFUJI Hideaki 1682a9b05723SYOSHIFUJI Hideaki if (unlikely(score->addr_type == IPV6_ADDR_ANY || 1683a9b05723SYOSHIFUJI Hideaki score->addr_type & IPV6_ADDR_MULTICAST)) { 1684ba7a46f1SJoe Perches net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s", 16859131f3deSYOSHIFUJI Hideaki/吉藤英明 idev->dev->name); 16861da177e4SLinus Torvalds continue; 1687072047e4SYOSHIFUJI Hideaki } 1688072047e4SYOSHIFUJI Hideaki 1689a9b05723SYOSHIFUJI Hideaki score->rule = -1; 1690a9b05723SYOSHIFUJI Hideaki bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX); 1691072047e4SYOSHIFUJI Hideaki 1692a9b05723SYOSHIFUJI Hideaki for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) { 1693a9b05723SYOSHIFUJI Hideaki int minihiscore, miniscore; 1694072047e4SYOSHIFUJI Hideaki 16959131f3deSYOSHIFUJI Hideaki/吉藤英明 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i); 16969131f3deSYOSHIFUJI Hideaki/吉藤英明 miniscore = ipv6_get_saddr_eval(net, score, dst, i); 1697072047e4SYOSHIFUJI Hideaki 1698a9b05723SYOSHIFUJI Hideaki if (minihiscore > miniscore) { 1699a9b05723SYOSHIFUJI Hideaki if (i == IPV6_SADDR_RULE_SCOPE && 1700a9b05723SYOSHIFUJI Hideaki score->scopedist > 0) { 1701a9b05723SYOSHIFUJI Hideaki /* 1702a9b05723SYOSHIFUJI Hideaki * special case: 1703a9b05723SYOSHIFUJI Hideaki * each remaining entry 1704a9b05723SYOSHIFUJI Hideaki * has too small (not enough) 1705a9b05723SYOSHIFUJI Hideaki * scope, because ifa entries 1706a9b05723SYOSHIFUJI Hideaki * are sorted by their scope 1707a9b05723SYOSHIFUJI Hideaki * values. 1708072047e4SYOSHIFUJI Hideaki */ 17099131f3deSYOSHIFUJI Hideaki/吉藤英明 goto out; 1710072047e4SYOSHIFUJI Hideaki } 1711a9b05723SYOSHIFUJI Hideaki break; 1712a9b05723SYOSHIFUJI Hideaki } else if (minihiscore < miniscore) { 1713a0bffffcSIlpo Järvinen swap(hiscore, score); 1714c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 hiscore_idx = 1 - hiscore_idx; 1715a9b05723SYOSHIFUJI Hideaki 1716a9b05723SYOSHIFUJI Hideaki /* restore our iterator */ 1717a9b05723SYOSHIFUJI Hideaki score->ifa = hiscore->ifa; 1718a9b05723SYOSHIFUJI Hideaki 1719a9b05723SYOSHIFUJI Hideaki break; 17201da177e4SLinus Torvalds } 1721a9b05723SYOSHIFUJI Hideaki } 1722a9b05723SYOSHIFUJI Hideaki } 17239131f3deSYOSHIFUJI Hideaki/吉藤英明 out: 1724c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 return hiscore_idx; 17251da177e4SLinus Torvalds } 17269131f3deSYOSHIFUJI Hideaki/吉藤英明 1727afbac601SDavid Ahern static int ipv6_get_saddr_master(struct net *net, 1728afbac601SDavid Ahern const struct net_device *dst_dev, 1729afbac601SDavid Ahern const struct net_device *master, 1730afbac601SDavid Ahern struct ipv6_saddr_dst *dst, 1731afbac601SDavid Ahern struct ipv6_saddr_score *scores, 1732afbac601SDavid Ahern int hiscore_idx) 1733afbac601SDavid Ahern { 1734afbac601SDavid Ahern struct inet6_dev *idev; 1735afbac601SDavid Ahern 1736afbac601SDavid Ahern idev = __in6_dev_get(dst_dev); 1737afbac601SDavid Ahern if (idev) 1738afbac601SDavid Ahern hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev, 1739afbac601SDavid Ahern scores, hiscore_idx); 1740afbac601SDavid Ahern 1741afbac601SDavid Ahern idev = __in6_dev_get(master); 1742afbac601SDavid Ahern if (idev) 1743afbac601SDavid Ahern hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev, 1744afbac601SDavid Ahern scores, hiscore_idx); 1745afbac601SDavid Ahern 1746afbac601SDavid Ahern return hiscore_idx; 1747afbac601SDavid Ahern } 1748afbac601SDavid Ahern 17499131f3deSYOSHIFUJI Hideaki/吉藤英明 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev, 17509131f3deSYOSHIFUJI Hideaki/吉藤英明 const struct in6_addr *daddr, unsigned int prefs, 17519131f3deSYOSHIFUJI Hideaki/吉藤英明 struct in6_addr *saddr) 17529131f3deSYOSHIFUJI Hideaki/吉藤英明 { 1753c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_score scores[2], *hiscore; 17549131f3deSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_dst dst; 17559131f3deSYOSHIFUJI Hideaki/吉藤英明 struct inet6_dev *idev; 17569131f3deSYOSHIFUJI Hideaki/吉藤英明 struct net_device *dev; 17579131f3deSYOSHIFUJI Hideaki/吉藤英明 int dst_type; 17589131f3deSYOSHIFUJI Hideaki/吉藤英明 bool use_oif_addr = false; 1759c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 int hiscore_idx = 0; 1760cc429c8fSEric Dumazet int ret = 0; 17619131f3deSYOSHIFUJI Hideaki/吉藤英明 17629131f3deSYOSHIFUJI Hideaki/吉藤英明 dst_type = __ipv6_addr_type(daddr); 17639131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.addr = daddr; 17649131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.ifindex = dst_dev ? dst_dev->ifindex : 0; 17659131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.scope = __ipv6_addr_src_scope(dst_type); 17669131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex); 17679131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.prefs = prefs; 17689131f3deSYOSHIFUJI Hideaki/吉藤英明 1769c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 scores[hiscore_idx].rule = -1; 1770c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 scores[hiscore_idx].ifa = NULL; 17719131f3deSYOSHIFUJI Hideaki/吉藤英明 17729131f3deSYOSHIFUJI Hideaki/吉藤英明 rcu_read_lock(); 17739131f3deSYOSHIFUJI Hideaki/吉藤英明 17749131f3deSYOSHIFUJI Hideaki/吉藤英明 /* Candidate Source Address (section 4) 17759131f3deSYOSHIFUJI Hideaki/吉藤英明 * - multicast and link-local destination address, 17769131f3deSYOSHIFUJI Hideaki/吉藤英明 * the set of candidate source address MUST only 17779131f3deSYOSHIFUJI Hideaki/吉藤英明 * include addresses assigned to interfaces 17789131f3deSYOSHIFUJI Hideaki/吉藤英明 * belonging to the same link as the outgoing 17799131f3deSYOSHIFUJI Hideaki/吉藤英明 * interface. 17809131f3deSYOSHIFUJI Hideaki/吉藤英明 * (- For site-local destination addresses, the 17819131f3deSYOSHIFUJI Hideaki/吉藤英明 * set of candidate source addresses MUST only 17829131f3deSYOSHIFUJI Hideaki/吉藤英明 * include addresses assigned to interfaces 17839131f3deSYOSHIFUJI Hideaki/吉藤英明 * belonging to the same site as the outgoing 17849131f3deSYOSHIFUJI Hideaki/吉藤英明 * interface.) 17853985e8a3SErik Kline * - "It is RECOMMENDED that the candidate source addresses 17863985e8a3SErik Kline * be the set of unicast addresses assigned to the 17873985e8a3SErik Kline * interface that will be used to send to the destination 17883985e8a3SErik Kline * (the 'outgoing' interface)." (RFC 6724) 17899131f3deSYOSHIFUJI Hideaki/吉藤英明 */ 17909131f3deSYOSHIFUJI Hideaki/吉藤英明 if (dst_dev) { 17919131f3deSYOSHIFUJI Hideaki/吉藤英明 idev = __in6_dev_get(dst_dev); 17923985e8a3SErik Kline if ((dst_type & IPV6_ADDR_MULTICAST) || 17933985e8a3SErik Kline dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL || 17943985e8a3SErik Kline (idev && idev->cnf.use_oif_addrs_only)) { 17959131f3deSYOSHIFUJI Hideaki/吉藤英明 use_oif_addr = true; 17969131f3deSYOSHIFUJI Hideaki/吉藤英明 } 17979131f3deSYOSHIFUJI Hideaki/吉藤英明 } 17989131f3deSYOSHIFUJI Hideaki/吉藤英明 17999131f3deSYOSHIFUJI Hideaki/吉藤英明 if (use_oif_addr) { 1800c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 if (idev) 1801c15df306SYOSHIFUJI Hideaki hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx); 18029131f3deSYOSHIFUJI Hideaki/吉藤英明 } else { 1803afbac601SDavid Ahern const struct net_device *master; 1804afbac601SDavid Ahern int master_idx = 0; 1805afbac601SDavid Ahern 1806afbac601SDavid Ahern /* if dst_dev exists and is enslaved to an L3 device, then 1807afbac601SDavid Ahern * prefer addresses from dst_dev and then the master over 1808afbac601SDavid Ahern * any other enslaved devices in the L3 domain. 1809afbac601SDavid Ahern */ 1810afbac601SDavid Ahern master = l3mdev_master_dev_rcu(dst_dev); 1811afbac601SDavid Ahern if (master) { 1812afbac601SDavid Ahern master_idx = master->ifindex; 1813afbac601SDavid Ahern 1814afbac601SDavid Ahern hiscore_idx = ipv6_get_saddr_master(net, dst_dev, 1815afbac601SDavid Ahern master, &dst, 1816afbac601SDavid Ahern scores, hiscore_idx); 1817afbac601SDavid Ahern 1818afbac601SDavid Ahern if (scores[hiscore_idx].ifa) 1819afbac601SDavid Ahern goto out; 1820afbac601SDavid Ahern } 1821afbac601SDavid Ahern 18229131f3deSYOSHIFUJI Hideaki/吉藤英明 for_each_netdev_rcu(net, dev) { 1823afbac601SDavid Ahern /* only consider addresses on devices in the 1824afbac601SDavid Ahern * same L3 domain 1825afbac601SDavid Ahern */ 1826afbac601SDavid Ahern if (l3mdev_master_ifindex_rcu(dev) != master_idx) 1827afbac601SDavid Ahern continue; 18289131f3deSYOSHIFUJI Hideaki/吉藤英明 idev = __in6_dev_get(dev); 18299131f3deSYOSHIFUJI Hideaki/吉藤英明 if (!idev) 18309131f3deSYOSHIFUJI Hideaki/吉藤英明 continue; 1831c15df306SYOSHIFUJI Hideaki hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx); 18329131f3deSYOSHIFUJI Hideaki/吉藤英明 } 18339131f3deSYOSHIFUJI Hideaki/吉藤英明 } 1834afbac601SDavid Ahern 1835afbac601SDavid Ahern out: 1836c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 hiscore = &scores[hiscore_idx]; 1837a9b05723SYOSHIFUJI Hideaki if (!hiscore->ifa) 1838cc429c8fSEric Dumazet ret = -EADDRNOTAVAIL; 1839cc429c8fSEric Dumazet else 18404e3fd7a0SAlexey Dobriyan *saddr = hiscore->ifa->addr; 1841cc429c8fSEric Dumazet 1842cc429c8fSEric Dumazet rcu_read_unlock(); 1843cc429c8fSEric Dumazet return ret; 18441da177e4SLinus Torvalds } 18455e5f3f0fSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_dev_get_saddr); 18461da177e4SLinus Torvalds 184726394fc1SIgnat Korchagin static int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr, 1848479840ffSJiri Pirko u32 banned_flags) 1849b7b1bfceSHannes Frederic Sowa { 1850b7b1bfceSHannes Frederic Sowa struct inet6_ifaddr *ifp; 1851b7b1bfceSHannes Frederic Sowa int err = -EADDRNOTAVAIL; 1852b7b1bfceSHannes Frederic Sowa 1853602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) { 1854602582caSHannes Frederic Sowa if (ifp->scope > IFA_LINK) 1855602582caSHannes Frederic Sowa break; 1856b7b1bfceSHannes Frederic Sowa if (ifp->scope == IFA_LINK && 1857b7b1bfceSHannes Frederic Sowa !(ifp->flags & banned_flags)) { 1858b7b1bfceSHannes Frederic Sowa *addr = ifp->addr; 1859b7b1bfceSHannes Frederic Sowa err = 0; 1860b7b1bfceSHannes Frederic Sowa break; 1861b7b1bfceSHannes Frederic Sowa } 1862b7b1bfceSHannes Frederic Sowa } 1863b7b1bfceSHannes Frederic Sowa return err; 1864b7b1bfceSHannes Frederic Sowa } 1865b7b1bfceSHannes Frederic Sowa 186695c385b4SNeil Horman int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr, 1867479840ffSJiri Pirko u32 banned_flags) 18681da177e4SLinus Torvalds { 18691da177e4SLinus Torvalds struct inet6_dev *idev; 18701da177e4SLinus Torvalds int err = -EADDRNOTAVAIL; 18711da177e4SLinus Torvalds 18728814c4b5SYOSHIFUJI Hideaki rcu_read_lock(); 1873e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 1874e21e8467SStephen Hemminger if (idev) { 18751da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1876b7b1bfceSHannes Frederic Sowa err = __ipv6_get_lladdr(idev, addr, banned_flags); 18771da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 18781da177e4SLinus Torvalds } 18798814c4b5SYOSHIFUJI Hideaki rcu_read_unlock(); 18801da177e4SLinus Torvalds return err; 18811da177e4SLinus Torvalds } 18821da177e4SLinus Torvalds 1883d9bf82c2SEric Dumazet static int ipv6_count_addresses(const struct inet6_dev *idev) 18841da177e4SLinus Torvalds { 1885d9bf82c2SEric Dumazet const struct inet6_ifaddr *ifp; 18861da177e4SLinus Torvalds int cnt = 0; 18871da177e4SLinus Torvalds 1888d9bf82c2SEric Dumazet rcu_read_lock(); 1889d9bf82c2SEric Dumazet list_for_each_entry_rcu(ifp, &idev->addr_list, if_list) 18901da177e4SLinus Torvalds cnt++; 1891d9bf82c2SEric Dumazet rcu_read_unlock(); 18921da177e4SLinus Torvalds return cnt; 18931da177e4SLinus Torvalds } 18941da177e4SLinus Torvalds 1895b71d1d42SEric Dumazet int ipv6_chk_addr(struct net *net, const struct in6_addr *addr, 18962a7851bfSFlorian Westphal const struct net_device *dev, int strict) 18971da177e4SLinus Torvalds { 1898232378e8SDavid Ahern return ipv6_chk_addr_and_flags(net, addr, dev, !dev, 1899232378e8SDavid Ahern strict, IFA_F_TENTATIVE); 1900c58da4c6SErik Kline } 1901c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr); 1902c58da4c6SErik Kline 19031893ff20SDavid Ahern /* device argument is used to find the L3 domain of interest. If 19041893ff20SDavid Ahern * skip_dev_check is set, then the ifp device is not checked against 19051893ff20SDavid Ahern * the passed in dev argument. So the 2 cases for addresses checks are: 19061893ff20SDavid Ahern * 1. does the address exist in the L3 domain that dev is part of 19071893ff20SDavid Ahern * (skip_dev_check = true), or 19081893ff20SDavid Ahern * 19091893ff20SDavid Ahern * 2. does the address exist on the specific device 19101893ff20SDavid Ahern * (skip_dev_check = false) 19111893ff20SDavid Ahern */ 19124ef1a7cbSXin Long static struct net_device * 19134ef1a7cbSXin Long __ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr, 1914232378e8SDavid Ahern const struct net_device *dev, bool skip_dev_check, 1915232378e8SDavid Ahern int strict, u32 banned_flags) 1916c58da4c6SErik Kline { 19173f27fb23SEric Dumazet unsigned int hash = inet6_addr_hash(net, addr); 19184ef1a7cbSXin Long struct net_device *l3mdev, *ndev; 1919eedf042aSStephen Hemminger struct inet6_ifaddr *ifp; 1920c58da4c6SErik Kline u32 ifp_flags; 19211da177e4SLinus Torvalds 1922480318a0SEric Dumazet rcu_read_lock(); 1923232378e8SDavid Ahern 19241893ff20SDavid Ahern l3mdev = l3mdev_master_dev_rcu(dev); 1925232378e8SDavid Ahern if (skip_dev_check) 1926232378e8SDavid Ahern dev = NULL; 1927232378e8SDavid Ahern 1928e66d1172SEric Dumazet hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) { 19294ef1a7cbSXin Long ndev = ifp->idev->dev; 19301893ff20SDavid Ahern 19314ef1a7cbSXin Long if (l3mdev_master_dev_rcu(ndev) != l3mdev) 19321893ff20SDavid Ahern continue; 19331893ff20SDavid Ahern 1934c58da4c6SErik Kline /* Decouple optimistic from tentative for evaluation here. 1935c58da4c6SErik Kline * Ban optimistic addresses explicitly, when required. 1936c58da4c6SErik Kline */ 1937c58da4c6SErik Kline ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC) 1938c58da4c6SErik Kline ? (ifp->flags&~IFA_F_TENTATIVE) 1939c58da4c6SErik Kline : ifp->flags; 19401da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr) && 1941c58da4c6SErik Kline !(ifp_flags&banned_flags) && 19424ef1a7cbSXin Long (!dev || ndev == dev || 1943eedf042aSStephen Hemminger !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) { 1944480318a0SEric Dumazet rcu_read_unlock(); 19454ef1a7cbSXin Long return ndev; 1946eedf042aSStephen Hemminger } 1947eedf042aSStephen Hemminger } 19485c578aedSstephen hemminger 1949480318a0SEric Dumazet rcu_read_unlock(); 19504ef1a7cbSXin Long return NULL; 19514ef1a7cbSXin Long } 19524ef1a7cbSXin Long 19534ef1a7cbSXin Long int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr, 19544ef1a7cbSXin Long const struct net_device *dev, bool skip_dev_check, 19554ef1a7cbSXin Long int strict, u32 banned_flags) 19564ef1a7cbSXin Long { 19574ef1a7cbSXin Long return __ipv6_chk_addr_and_flags(net, addr, dev, skip_dev_check, 19584ef1a7cbSXin Long strict, banned_flags) ? 1 : 0; 19591da177e4SLinus Torvalds } 1960c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr_and_flags); 19617159039aSYOSHIFUJI Hideaki 19621da177e4SLinus Torvalds 19637df37ff3SCatalin\(ux\) M. BOIE /* Compares an address/prefix_len with addresses on device @dev. 19647df37ff3SCatalin\(ux\) M. BOIE * If one is found it returns true. 19657df37ff3SCatalin\(ux\) M. BOIE */ 19667df37ff3SCatalin\(ux\) M. BOIE bool ipv6_chk_custom_prefix(const struct in6_addr *addr, 19677df37ff3SCatalin\(ux\) M. BOIE const unsigned int prefix_len, struct net_device *dev) 19687df37ff3SCatalin\(ux\) M. BOIE { 196947e26941SEric Dumazet const struct inet6_ifaddr *ifa; 197047e26941SEric Dumazet const struct inet6_dev *idev; 19717df37ff3SCatalin\(ux\) M. BOIE bool ret = false; 19727df37ff3SCatalin\(ux\) M. BOIE 19737df37ff3SCatalin\(ux\) M. BOIE rcu_read_lock(); 19747df37ff3SCatalin\(ux\) M. BOIE idev = __in6_dev_get(dev); 19757df37ff3SCatalin\(ux\) M. BOIE if (idev) { 197647e26941SEric Dumazet list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) { 19777df37ff3SCatalin\(ux\) M. BOIE ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len); 19787df37ff3SCatalin\(ux\) M. BOIE if (ret) 19797df37ff3SCatalin\(ux\) M. BOIE break; 19807df37ff3SCatalin\(ux\) M. BOIE } 19817df37ff3SCatalin\(ux\) M. BOIE } 19827df37ff3SCatalin\(ux\) M. BOIE rcu_read_unlock(); 19837df37ff3SCatalin\(ux\) M. BOIE 19847df37ff3SCatalin\(ux\) M. BOIE return ret; 19857df37ff3SCatalin\(ux\) M. BOIE } 19867df37ff3SCatalin\(ux\) M. BOIE EXPORT_SYMBOL(ipv6_chk_custom_prefix); 19877df37ff3SCatalin\(ux\) M. BOIE 1988b71d1d42SEric Dumazet int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev) 198952eeeb84SYOSHIFUJI Hideaki { 199024ba333bSEric Dumazet const struct inet6_ifaddr *ifa; 199124ba333bSEric Dumazet const struct inet6_dev *idev; 199252eeeb84SYOSHIFUJI Hideaki int onlink; 199352eeeb84SYOSHIFUJI Hideaki 199452eeeb84SYOSHIFUJI Hideaki onlink = 0; 199552eeeb84SYOSHIFUJI Hideaki rcu_read_lock(); 199652eeeb84SYOSHIFUJI Hideaki idev = __in6_dev_get(dev); 199752eeeb84SYOSHIFUJI Hideaki if (idev) { 199824ba333bSEric Dumazet list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) { 199952eeeb84SYOSHIFUJI Hideaki onlink = ipv6_prefix_equal(addr, &ifa->addr, 200052eeeb84SYOSHIFUJI Hideaki ifa->prefix_len); 200152eeeb84SYOSHIFUJI Hideaki if (onlink) 200252eeeb84SYOSHIFUJI Hideaki break; 200352eeeb84SYOSHIFUJI Hideaki } 200452eeeb84SYOSHIFUJI Hideaki } 200552eeeb84SYOSHIFUJI Hideaki rcu_read_unlock(); 200652eeeb84SYOSHIFUJI Hideaki return onlink; 200752eeeb84SYOSHIFUJI Hideaki } 200852eeeb84SYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_prefix); 200952eeeb84SYOSHIFUJI Hideaki 201081f6cb31SXin Long /** 201181f6cb31SXin Long * ipv6_dev_find - find the first device with a given source address. 201281f6cb31SXin Long * @net: the net namespace 201381f6cb31SXin Long * @addr: the source address 20142c4de211SXin Long * @dev: used to find the L3 domain of interest 201581f6cb31SXin Long * 201681f6cb31SXin Long * The caller should be protected by RCU, or RTNL. 201781f6cb31SXin Long */ 20184ef1a7cbSXin Long struct net_device *ipv6_dev_find(struct net *net, const struct in6_addr *addr, 20194ef1a7cbSXin Long struct net_device *dev) 202081f6cb31SXin Long { 20214ef1a7cbSXin Long return __ipv6_chk_addr_and_flags(net, addr, dev, !dev, 1, 20224ef1a7cbSXin Long IFA_F_TENTATIVE); 202381f6cb31SXin Long } 202481f6cb31SXin Long EXPORT_SYMBOL(ipv6_dev_find); 202581f6cb31SXin Long 20269acd9f3aSYOSHIFUJI Hideaki struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr, 20271cab3da6SDaniel Lezcano struct net_device *dev, int strict) 20281da177e4SLinus Torvalds { 20293f27fb23SEric Dumazet unsigned int hash = inet6_addr_hash(net, addr); 2030b79d1d54SDavid S. Miller struct inet6_ifaddr *ifp, *result = NULL; 20311da177e4SLinus Torvalds 203224f226daSEric Dumazet rcu_read_lock(); 2033e66d1172SEric Dumazet hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) { 20341da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr)) { 203563159f29SIan Morris if (!dev || ifp->idev->dev == dev || 20361da177e4SLinus Torvalds !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) { 2037b79d1d54SDavid S. Miller result = ifp; 20381da177e4SLinus Torvalds in6_ifa_hold(ifp); 20391da177e4SLinus Torvalds break; 20401da177e4SLinus Torvalds } 20411da177e4SLinus Torvalds } 20421da177e4SLinus Torvalds } 204324f226daSEric Dumazet rcu_read_unlock(); 20441da177e4SLinus Torvalds 2045b79d1d54SDavid S. Miller return result; 20461da177e4SLinus Torvalds } 20471da177e4SLinus Torvalds 20481da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */ 20491da177e4SLinus Torvalds 2050cc411d0bSBrian Haley static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed) 20511da177e4SLinus Torvalds { 20523d171f39SLubomir Rintel if (dad_failed) 20533d171f39SLubomir Rintel ifp->flags |= IFA_F_DADFAILED; 20543d171f39SLubomir Rintel 2055ec8add2aSSabrina Dubroca if (ifp->flags&IFA_F_TEMPORARY) { 20561da177e4SLinus Torvalds struct inet6_ifaddr *ifpub; 20571da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 20581da177e4SLinus Torvalds ifpub = ifp->ifpub; 20591da177e4SLinus Torvalds if (ifpub) { 20601da177e4SLinus Torvalds in6_ifa_hold(ifpub); 20611da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 2062969c5464SFernando Gont ipv6_create_tempaddr(ifpub, true); 20631da177e4SLinus Torvalds in6_ifa_put(ifpub); 20641da177e4SLinus Torvalds } else { 20651da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 20661da177e4SLinus Torvalds } 20671da177e4SLinus Torvalds ipv6_del_addr(ifp); 2068ec8add2aSSabrina Dubroca } else if (ifp->flags&IFA_F_PERMANENT || !dad_failed) { 2069ec8add2aSSabrina Dubroca spin_lock_bh(&ifp->lock); 2070ec8add2aSSabrina Dubroca addrconf_del_dad_work(ifp); 2071ec8add2aSSabrina Dubroca ifp->flags |= IFA_F_TENTATIVE; 2072f1c02cfbSSabrina Dubroca if (dad_failed) 2073f1c02cfbSSabrina Dubroca ifp->flags &= ~IFA_F_OPTIMISTIC; 2074ec8add2aSSabrina Dubroca spin_unlock_bh(&ifp->lock); 2075ec8add2aSSabrina Dubroca if (dad_failed) 2076ec8add2aSSabrina Dubroca ipv6_ifa_notify(0, ifp); 2077ec8add2aSSabrina Dubroca in6_ifa_put(ifp); 2078c15b1ccaSHannes Frederic Sowa } else { 20791da177e4SLinus Torvalds ipv6_del_addr(ifp); 20801da177e4SLinus Torvalds } 2081c15b1ccaSHannes Frederic Sowa } 20821da177e4SLinus Torvalds 2083f2344a13SHerbert Xu static int addrconf_dad_end(struct inet6_ifaddr *ifp) 2084f2344a13SHerbert Xu { 2085f2344a13SHerbert Xu int err = -ENOENT; 2086f2344a13SHerbert Xu 20878e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 2088f2344a13SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DAD) { 2089f2344a13SHerbert Xu ifp->state = INET6_IFADDR_STATE_POSTDAD; 2090f2344a13SHerbert Xu err = 0; 2091f2344a13SHerbert Xu } 20928e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 2093f2344a13SHerbert Xu 2094f2344a13SHerbert Xu return err; 2095f2344a13SHerbert Xu } 2096f2344a13SHerbert Xu 2097da13c59bSVishwanath Pai void addrconf_dad_failure(struct sk_buff *skb, struct inet6_ifaddr *ifp) 20983c21edbdSYOSHIFUJI Hideaki { 20991b34be74SYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev; 2100e66d1172SEric Dumazet struct net *net = dev_net(idev->dev); 21019bdd8d40SBrian Haley 2102853dc2e0SUrsula Braun if (addrconf_dad_end(ifp)) { 2103853dc2e0SUrsula Braun in6_ifa_put(ifp); 2104f2344a13SHerbert Xu return; 2105853dc2e0SUrsula Braun } 2106f2344a13SHerbert Xu 2107da13c59bSVishwanath Pai net_info_ratelimited("%s: IPv6 duplicate address %pI6c used by %pM detected!\n", 2108da13c59bSVishwanath Pai ifp->idev->dev->name, &ifp->addr, eth_hdr(skb)->h_source); 21099bdd8d40SBrian Haley 21105f40ef77SHannes Frederic Sowa spin_lock_bh(&ifp->lock); 21111b34be74SYOSHIFUJI Hideaki 21125f40ef77SHannes Frederic Sowa if (ifp->flags & IFA_F_STABLE_PRIVACY) { 21135f40ef77SHannes Frederic Sowa struct in6_addr new_addr; 21145f40ef77SHannes Frederic Sowa struct inet6_ifaddr *ifp2; 21155f40ef77SHannes Frederic Sowa int retries = ifp->stable_privacy_retry + 1; 2116e6464b8cSDavid Ahern struct ifa6_config cfg = { 2117e6464b8cSDavid Ahern .pfx = &new_addr, 2118e6464b8cSDavid Ahern .plen = ifp->prefix_len, 2119e6464b8cSDavid Ahern .ifa_flags = ifp->flags, 2120e6464b8cSDavid Ahern .valid_lft = ifp->valid_lft, 2121e6464b8cSDavid Ahern .preferred_lft = ifp->prefered_lft, 2122e6464b8cSDavid Ahern .scope = ifp->scope, 2123e6464b8cSDavid Ahern }; 21245f40ef77SHannes Frederic Sowa 21251855b7c3SHannes Frederic Sowa if (retries > net->ipv6.sysctl.idgen_retries) { 21265f40ef77SHannes Frederic Sowa net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n", 21275f40ef77SHannes Frederic Sowa ifp->idev->dev->name); 21285f40ef77SHannes Frederic Sowa goto errdad; 21295f40ef77SHannes Frederic Sowa } 21305f40ef77SHannes Frederic Sowa 21315f40ef77SHannes Frederic Sowa new_addr = ifp->addr; 21325f40ef77SHannes Frederic Sowa if (ipv6_generate_stable_address(&new_addr, retries, 21335f40ef77SHannes Frederic Sowa idev)) 21345f40ef77SHannes Frederic Sowa goto errdad; 21355f40ef77SHannes Frederic Sowa 21365f40ef77SHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 21375f40ef77SHannes Frederic Sowa 21385f40ef77SHannes Frederic Sowa if (idev->cnf.max_addresses && 21395f40ef77SHannes Frederic Sowa ipv6_count_addresses(idev) >= 21405f40ef77SHannes Frederic Sowa idev->cnf.max_addresses) 21415f40ef77SHannes Frederic Sowa goto lock_errdad; 21425f40ef77SHannes Frederic Sowa 21435f40ef77SHannes Frederic Sowa net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n", 21445f40ef77SHannes Frederic Sowa ifp->idev->dev->name); 21455f40ef77SHannes Frederic Sowa 2146e6464b8cSDavid Ahern ifp2 = ipv6_add_addr(idev, &cfg, false, NULL); 21475f40ef77SHannes Frederic Sowa if (IS_ERR(ifp2)) 21485f40ef77SHannes Frederic Sowa goto lock_errdad; 21495f40ef77SHannes Frederic Sowa 21505f40ef77SHannes Frederic Sowa spin_lock_bh(&ifp2->lock); 21515f40ef77SHannes Frederic Sowa ifp2->stable_privacy_retry = retries; 21525f40ef77SHannes Frederic Sowa ifp2->state = INET6_IFADDR_STATE_PREDAD; 21535f40ef77SHannes Frederic Sowa spin_unlock_bh(&ifp2->lock); 21545f40ef77SHannes Frederic Sowa 21551855b7c3SHannes Frederic Sowa addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay); 21565f40ef77SHannes Frederic Sowa in6_ifa_put(ifp2); 21575f40ef77SHannes Frederic Sowa lock_errdad: 21585f40ef77SHannes Frederic Sowa spin_lock_bh(&ifp->lock); 21591b34be74SYOSHIFUJI Hideaki } 21601b34be74SYOSHIFUJI Hideaki 21615f40ef77SHannes Frederic Sowa errdad: 2162c15b1ccaSHannes Frederic Sowa /* transition from _POSTDAD to _ERRDAD */ 2163c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_ERRDAD; 21648e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 2165c15b1ccaSHannes Frederic Sowa 2166c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 0); 2167751eb6b6SWei Yongjun in6_ifa_put(ifp); 21683c21edbdSYOSHIFUJI Hideaki } 21691da177e4SLinus Torvalds 2170a9ed4a29SSabrina Dubroca /* Join to solicited addr multicast group. 2171a9ed4a29SSabrina Dubroca * caller must hold RTNL */ 2172b71d1d42SEric Dumazet void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr) 21731da177e4SLinus Torvalds { 21741da177e4SLinus Torvalds struct in6_addr maddr; 21751da177e4SLinus Torvalds 21761da177e4SLinus Torvalds if (dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 21771da177e4SLinus Torvalds return; 21781da177e4SLinus Torvalds 21791da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr); 21801da177e4SLinus Torvalds ipv6_dev_mc_inc(dev, &maddr); 21811da177e4SLinus Torvalds } 21821da177e4SLinus Torvalds 2183a9ed4a29SSabrina Dubroca /* caller must hold RTNL */ 2184b71d1d42SEric Dumazet void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr) 21851da177e4SLinus Torvalds { 21861da177e4SLinus Torvalds struct in6_addr maddr; 21871da177e4SLinus Torvalds 21881da177e4SLinus Torvalds if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 21891da177e4SLinus Torvalds return; 21901da177e4SLinus Torvalds 21911da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr); 21921da177e4SLinus Torvalds __ipv6_dev_mc_dec(idev, &maddr); 21931da177e4SLinus Torvalds } 21941da177e4SLinus Torvalds 2195a9ed4a29SSabrina Dubroca /* caller must hold RTNL */ 219620380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp) 21971da177e4SLinus Torvalds { 21981da177e4SLinus Torvalds struct in6_addr addr; 2199c15b1ccaSHannes Frederic Sowa 220088ad3149SHannes Frederic Sowa if (ifp->prefix_len >= 127) /* RFC 6164 */ 22012bda8a0cSBjørn Mork return; 22021da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 22031da177e4SLinus Torvalds if (ipv6_addr_any(&addr)) 22041da177e4SLinus Torvalds return; 2205013b4d90SWANG Cong __ipv6_dev_ac_inc(ifp->idev, &addr); 22061da177e4SLinus Torvalds } 22071da177e4SLinus Torvalds 2208a9ed4a29SSabrina Dubroca /* caller must hold RTNL */ 220920380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp) 22101da177e4SLinus Torvalds { 22111da177e4SLinus Torvalds struct in6_addr addr; 2212c15b1ccaSHannes Frederic Sowa 221388ad3149SHannes Frederic Sowa if (ifp->prefix_len >= 127) /* RFC 6164 */ 221432019e65SYOSHIFUJI Hideaki return; 22151da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 22161da177e4SLinus Torvalds if (ipv6_addr_any(&addr)) 22171da177e4SLinus Torvalds return; 22181da177e4SLinus Torvalds __ipv6_dev_ac_dec(ifp->idev, &addr); 22191da177e4SLinus Torvalds } 22201da177e4SLinus Torvalds 22218a7a4b47SAlexander Aring static int addrconf_ifid_6lowpan(u8 *eui, struct net_device *dev) 222206a4c1c5Salex.bluesman.smirnov@gmail.com { 22238a7a4b47SAlexander Aring switch (dev->addr_len) { 22248a7a4b47SAlexander Aring case ETH_ALEN: 22259dae2e03SLuiz Augusto von Dentz memcpy(eui, dev->dev_addr, 3); 22269dae2e03SLuiz Augusto von Dentz eui[3] = 0xFF; 22279dae2e03SLuiz Augusto von Dentz eui[4] = 0xFE; 22289dae2e03SLuiz Augusto von Dentz memcpy(eui + 5, dev->dev_addr + 3, 3); 22299dae2e03SLuiz Augusto von Dentz break; 22308a7a4b47SAlexander Aring case EUI64_ADDR_LEN: 22315241c2d7SAlexander Aring memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN); 22325e98a36eSYOSHIFUJI Hideaki / 吉藤英明 eui[0] ^= 2; 22338a7a4b47SAlexander Aring break; 22348a7a4b47SAlexander Aring default: 22358a7a4b47SAlexander Aring return -1; 22368a7a4b47SAlexander Aring } 22378a7a4b47SAlexander Aring 223806a4c1c5Salex.bluesman.smirnov@gmail.com return 0; 223906a4c1c5Salex.bluesman.smirnov@gmail.com } 224006a4c1c5Salex.bluesman.smirnov@gmail.com 2241cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev) 2242cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 { 22431a8a23d2SJakub Kicinski const union fwnet_hwaddr *ha; 2244cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 2245cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 if (dev->addr_len != FWNET_ALEN) 2246cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return -1; 2247cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 22481a8a23d2SJakub Kicinski ha = (const union fwnet_hwaddr *)dev->dev_addr; 2249cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 2250cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id)); 2251cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 eui[0] ^= 2; 2252cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return 0; 2253cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 } 2254cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 2255073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev) 2256073a8e0eSYOSHIFUJI Hideaki { 22571da177e4SLinus Torvalds /* XXX: inherit EUI-64 from other interface -- yoshfuji */ 22581da177e4SLinus Torvalds if (dev->addr_len != ARCNET_ALEN) 22591da177e4SLinus Torvalds return -1; 22601da177e4SLinus Torvalds memset(eui, 0, 7); 22611da177e4SLinus Torvalds eui[7] = *(u8 *)dev->dev_addr; 22621da177e4SLinus Torvalds return 0; 2263073a8e0eSYOSHIFUJI Hideaki } 2264073a8e0eSYOSHIFUJI Hideaki 2265073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev) 2266073a8e0eSYOSHIFUJI Hideaki { 22671da177e4SLinus Torvalds if (dev->addr_len != INFINIBAND_ALEN) 22681da177e4SLinus Torvalds return -1; 22691da177e4SLinus Torvalds memcpy(eui, dev->dev_addr + 12, 8); 22701da177e4SLinus Torvalds eui[0] |= 2; 22711da177e4SLinus Torvalds return 0; 22721da177e4SLinus Torvalds } 2273073a8e0eSYOSHIFUJI Hideaki 2274c61393eaSstephen hemminger static int __ipv6_isatap_ifid(u8 *eui, __be32 addr) 2275dfd982baSYOSHIFUJI Hideaki { 22769af28511SSascha Hlusiak if (addr == 0) 22779af28511SSascha Hlusiak return -1; 2278dfd982baSYOSHIFUJI Hideaki eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) || 2279dfd982baSYOSHIFUJI Hideaki ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) || 2280dfd982baSYOSHIFUJI Hideaki ipv4_is_private_172(addr) || ipv4_is_test_192(addr) || 2281dfd982baSYOSHIFUJI Hideaki ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) || 2282dfd982baSYOSHIFUJI Hideaki ipv4_is_test_198(addr) || ipv4_is_multicast(addr) || 2283dfd982baSYOSHIFUJI Hideaki ipv4_is_lbcast(addr)) ? 0x00 : 0x02; 2284dfd982baSYOSHIFUJI Hideaki eui[1] = 0; 2285dfd982baSYOSHIFUJI Hideaki eui[2] = 0x5E; 2286dfd982baSYOSHIFUJI Hideaki eui[3] = 0xFE; 2287dfd982baSYOSHIFUJI Hideaki memcpy(eui + 4, &addr, 4); 2288dfd982baSYOSHIFUJI Hideaki return 0; 2289dfd982baSYOSHIFUJI Hideaki } 2290dfd982baSYOSHIFUJI Hideaki 2291dfd982baSYOSHIFUJI Hideaki static int addrconf_ifid_sit(u8 *eui, struct net_device *dev) 2292dfd982baSYOSHIFUJI Hideaki { 2293dfd982baSYOSHIFUJI Hideaki if (dev->priv_flags & IFF_ISATAP) 2294dfd982baSYOSHIFUJI Hideaki return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); 2295dfd982baSYOSHIFUJI Hideaki return -1; 2296dfd982baSYOSHIFUJI Hideaki } 2297dfd982baSYOSHIFUJI Hideaki 2298aee80b54Sstephen hemminger static int addrconf_ifid_gre(u8 *eui, struct net_device *dev) 2299aee80b54Sstephen hemminger { 2300aee80b54Sstephen hemminger return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); 2301aee80b54Sstephen hemminger } 2302aee80b54Sstephen hemminger 2303e837735eSNicolas Dichtel static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev) 2304e837735eSNicolas Dichtel { 2305e837735eSNicolas Dichtel memcpy(eui, dev->perm_addr, 3); 2306e837735eSNicolas Dichtel memcpy(eui + 5, dev->perm_addr + 3, 3); 2307e837735eSNicolas Dichtel eui[3] = 0xFF; 2308e837735eSNicolas Dichtel eui[4] = 0xFE; 2309e837735eSNicolas Dichtel eui[0] ^= 2; 2310e837735eSNicolas Dichtel return 0; 2311e837735eSNicolas Dichtel } 2312e837735eSNicolas Dichtel 2313073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev) 2314073a8e0eSYOSHIFUJI Hideaki { 2315073a8e0eSYOSHIFUJI Hideaki switch (dev->type) { 2316073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ETHER: 2317073a8e0eSYOSHIFUJI Hideaki case ARPHRD_FDDI: 2318073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_eui48(eui, dev); 2319073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ARCNET: 2320073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_arcnet(eui, dev); 2321073a8e0eSYOSHIFUJI Hideaki case ARPHRD_INFINIBAND: 2322073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_infiniband(eui, dev); 2323c7dc89c0SFred L. Templin case ARPHRD_SIT: 2324dfd982baSYOSHIFUJI Hideaki return addrconf_ifid_sit(eui, dev); 2325aee80b54Sstephen hemminger case ARPHRD_IPGRE: 232645ce0fd1SFelix Jia case ARPHRD_TUNNEL: 2327aee80b54Sstephen hemminger return addrconf_ifid_gre(eui, dev); 2328e74bccb8SJukka Rissanen case ARPHRD_6LOWPAN: 23298a7a4b47SAlexander Aring return addrconf_ifid_6lowpan(eui, dev); 2330cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 case ARPHRD_IEEE1394: 2331cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return addrconf_ifid_ieee1394(eui, dev); 2332e837735eSNicolas Dichtel case ARPHRD_TUNNEL6: 233345ce0fd1SFelix Jia case ARPHRD_IP6GRE: 23349deb441cSSubash Abhinov Kasiviswanathan case ARPHRD_RAWIP: 2335e837735eSNicolas Dichtel return addrconf_ifid_ip6tnl(eui, dev); 2336073a8e0eSYOSHIFUJI Hideaki } 23371da177e4SLinus Torvalds return -1; 23381da177e4SLinus Torvalds } 23391da177e4SLinus Torvalds 23401da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev) 23411da177e4SLinus Torvalds { 23421da177e4SLinus Torvalds int err = -1; 23431da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 23441da177e4SLinus Torvalds 23451da177e4SLinus Torvalds read_lock_bh(&idev->lock); 2346602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) { 2347602582caSHannes Frederic Sowa if (ifp->scope > IFA_LINK) 2348602582caSHannes Frederic Sowa break; 23491da177e4SLinus Torvalds if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) { 23501da177e4SLinus Torvalds memcpy(eui, ifp->addr.s6_addr+8, 8); 23511da177e4SLinus Torvalds err = 0; 23521da177e4SLinus Torvalds break; 23531da177e4SLinus Torvalds } 23541da177e4SLinus Torvalds } 23551da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 23561da177e4SLinus Torvalds return err; 23571da177e4SLinus Torvalds } 23581da177e4SLinus Torvalds 2359969c5464SFernando Gont /* Generation of a randomized Interface Identifier 2360969c5464SFernando Gont * draft-ietf-6man-rfc4941bis, Section 3.3.1 2361969c5464SFernando Gont */ 2362969c5464SFernando Gont 2363969c5464SFernando Gont static void ipv6_gen_rnd_iid(struct in6_addr *addr) 23641da177e4SLinus Torvalds { 23651da177e4SLinus Torvalds regen: 2366969c5464SFernando Gont get_random_bytes(&addr->s6_addr[8], 8); 23671da177e4SLinus Torvalds 2368969c5464SFernando Gont /* <draft-ietf-6man-rfc4941bis-08.txt>, Section 3.3.1: 2369969c5464SFernando Gont * check if generated address is not inappropriate: 23701da177e4SLinus Torvalds * 2371912b519aSBhaskar Chowdhury * - Reserved IPv6 Interface Identifiers 23721da177e4SLinus Torvalds * - XXX: already assigned to an address on the device 23731da177e4SLinus Torvalds */ 23741da177e4SLinus Torvalds 2375969c5464SFernando Gont /* Subnet-router anycast: 0000:0000:0000:0000 */ 2376969c5464SFernando Gont if (!(addr->s6_addr32[2] | addr->s6_addr32[3])) 2377969c5464SFernando Gont goto regen; 2378969c5464SFernando Gont 2379969c5464SFernando Gont /* IANA Ethernet block: 0200:5EFF:FE00:0000-0200:5EFF:FE00:5212 2380969c5464SFernando Gont * Proxy Mobile IPv6: 0200:5EFF:FE00:5213 2381969c5464SFernando Gont * IANA Ethernet block: 0200:5EFF:FE00:5214-0200:5EFF:FEFF:FFFF 2382969c5464SFernando Gont */ 2383969c5464SFernando Gont if (ntohl(addr->s6_addr32[2]) == 0x02005eff && 2384969c5464SFernando Gont (ntohl(addr->s6_addr32[3]) & 0Xff000000) == 0xfe000000) 2385969c5464SFernando Gont goto regen; 2386969c5464SFernando Gont 2387969c5464SFernando Gont /* Reserved subnet anycast addresses */ 2388969c5464SFernando Gont if (ntohl(addr->s6_addr32[2]) == 0xfdffffff && 2389969c5464SFernando Gont ntohl(addr->s6_addr32[3]) >= 0Xffffff80) 2390969c5464SFernando Gont goto regen; 23911da177e4SLinus Torvalds } 23921da177e4SLinus Torvalds 23931da177e4SLinus Torvalds /* 23941da177e4SLinus Torvalds * Add prefix route. 23951da177e4SLinus Torvalds */ 23961da177e4SLinus Torvalds 23971da177e4SLinus Torvalds static void 23988308f3ffSDavid Ahern addrconf_prefix_route(struct in6_addr *pfx, int plen, u32 metric, 23998308f3ffSDavid Ahern struct net_device *dev, unsigned long expires, 24008308f3ffSDavid Ahern u32 flags, gfp_t gfp_flags) 24011da177e4SLinus Torvalds { 240286872cb5SThomas Graf struct fib6_config cfg = { 2403ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX, 24048308f3ffSDavid Ahern .fc_metric = metric ? : IP6_RT_PRIO_ADDRCONF, 240586872cb5SThomas Graf .fc_ifindex = dev->ifindex, 240686872cb5SThomas Graf .fc_expires = expires, 240786872cb5SThomas Graf .fc_dst_len = plen, 240886872cb5SThomas Graf .fc_flags = RTF_UP | flags, 2409c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 2410f410a1fbSStephen Hemminger .fc_protocol = RTPROT_KERNEL, 2411e8478e80SDavid Ahern .fc_type = RTN_UNICAST, 241286872cb5SThomas Graf }; 24131da177e4SLinus Torvalds 24144e3fd7a0SAlexey Dobriyan cfg.fc_dst = *pfx; 24151da177e4SLinus Torvalds 24161da177e4SLinus Torvalds /* Prevent useless cloning on PtP SIT. 24171da177e4SLinus Torvalds This thing is done here expecting that the whole 24181da177e4SLinus Torvalds class of non-broadcast devices need not cloning. 24191da177e4SLinus Torvalds */ 242007a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 24211da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT)) 242286872cb5SThomas Graf cfg.fc_flags |= RTF_NONEXTHOP; 24230be669bbSJoerg Roedel #endif 24241da177e4SLinus Torvalds 2425acb54e3cSDavid Ahern ip6_route_add(&cfg, gfp_flags, NULL); 24261da177e4SLinus Torvalds } 24271da177e4SLinus Torvalds 242814ef37b6SAndreas Hofmeister 24298d1c802bSDavid Ahern static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx, 243014ef37b6SAndreas Hofmeister int plen, 243114ef37b6SAndreas Hofmeister const struct net_device *dev, 24322b2450caSDavid Ahern u32 flags, u32 noflags, 24332b2450caSDavid Ahern bool no_gw) 243414ef37b6SAndreas Hofmeister { 243514ef37b6SAndreas Hofmeister struct fib6_node *fn; 24368d1c802bSDavid Ahern struct fib6_info *rt = NULL; 243714ef37b6SAndreas Hofmeister struct fib6_table *table; 2438ca254490SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX; 243914ef37b6SAndreas Hofmeister 2440ca254490SDavid Ahern table = fib6_get_table(dev_net(dev), tb_id); 244163159f29SIan Morris if (!table) 244214ef37b6SAndreas Hofmeister return NULL; 244314ef37b6SAndreas Hofmeister 244466f5d6ceSWei Wang rcu_read_lock(); 244538fbeeeeSWei Wang fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0, true); 244614ef37b6SAndreas Hofmeister if (!fn) 244714ef37b6SAndreas Hofmeister goto out; 24481f56a01fSMartin KaFai Lau 244966f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 2450f88d8ea6SDavid Ahern /* prefix routes only use builtin fib6_nh */ 2451f88d8ea6SDavid Ahern if (rt->nh) 2452f88d8ea6SDavid Ahern continue; 2453f88d8ea6SDavid Ahern 24541cf844c7SDavid Ahern if (rt->fib6_nh->fib_nh_dev->ifindex != dev->ifindex) 245514ef37b6SAndreas Hofmeister continue; 24561cf844c7SDavid Ahern if (no_gw && rt->fib6_nh->fib_nh_gw_family) 24572b2450caSDavid Ahern continue; 245893c2fb25SDavid Ahern if ((rt->fib6_flags & flags) != flags) 245914ef37b6SAndreas Hofmeister continue; 246093c2fb25SDavid Ahern if ((rt->fib6_flags & noflags) != 0) 246114ef37b6SAndreas Hofmeister continue; 2462e873e4b9SWei Wang if (!fib6_info_hold_safe(rt)) 2463e873e4b9SWei Wang continue; 246414ef37b6SAndreas Hofmeister break; 246514ef37b6SAndreas Hofmeister } 246614ef37b6SAndreas Hofmeister out: 246766f5d6ceSWei Wang rcu_read_unlock(); 246814ef37b6SAndreas Hofmeister return rt; 246914ef37b6SAndreas Hofmeister } 247014ef37b6SAndreas Hofmeister 247114ef37b6SAndreas Hofmeister 24721da177e4SLinus Torvalds /* Create "default" multicast route to the interface */ 24731da177e4SLinus Torvalds 24741da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev) 24751da177e4SLinus Torvalds { 247686872cb5SThomas Graf struct fib6_config cfg = { 2477ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL, 247886872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 247986872cb5SThomas Graf .fc_ifindex = dev->ifindex, 248086872cb5SThomas Graf .fc_dst_len = 8, 248186872cb5SThomas Graf .fc_flags = RTF_UP, 2482ceed9038SMatteo Croce .fc_type = RTN_MULTICAST, 2483c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 2484a826b043SMatteo Croce .fc_protocol = RTPROT_KERNEL, 248586872cb5SThomas Graf }; 24861da177e4SLinus Torvalds 248786872cb5SThomas Graf ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0); 248886872cb5SThomas Graf 2489e500c6d3SCong Wang ip6_route_add(&cfg, GFP_KERNEL, NULL); 24901da177e4SLinus Torvalds } 24911da177e4SLinus Torvalds 24921da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev) 24931da177e4SLinus Torvalds { 24941da177e4SLinus Torvalds struct inet6_dev *idev; 24951da177e4SLinus Torvalds 24961da177e4SLinus Torvalds ASSERT_RTNL(); 24971da177e4SLinus Torvalds 2498e21e8467SStephen Hemminger idev = ipv6_find_idev(dev); 2499db0b99f5SSabrina Dubroca if (IS_ERR(idev)) 2500db0b99f5SSabrina Dubroca return idev; 250164e724f6SBrian Haley 250264e724f6SBrian Haley if (idev->cnf.disable_ipv6) 250364e724f6SBrian Haley return ERR_PTR(-EACCES); 25041da177e4SLinus Torvalds 25051da177e4SLinus Torvalds /* Add default multicast route */ 2506ba46ee4cSDavid Ahern if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev)) 25071da177e4SLinus Torvalds addrconf_add_mroute(dev); 25081da177e4SLinus Torvalds 25091da177e4SLinus Torvalds return idev; 25101da177e4SLinus Torvalds } 25111da177e4SLinus Torvalds 251253bd6749SJiri Pirko static void manage_tempaddrs(struct inet6_dev *idev, 251353bd6749SJiri Pirko struct inet6_ifaddr *ifp, 251453bd6749SJiri Pirko __u32 valid_lft, __u32 prefered_lft, 251553bd6749SJiri Pirko bool create, unsigned long now) 251653bd6749SJiri Pirko { 251753bd6749SJiri Pirko u32 flags; 251853bd6749SJiri Pirko struct inet6_ifaddr *ift; 251953bd6749SJiri Pirko 252053bd6749SJiri Pirko read_lock_bh(&idev->lock); 252153bd6749SJiri Pirko /* update all temporary addresses in the list */ 252253bd6749SJiri Pirko list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) { 252353bd6749SJiri Pirko int age, max_valid, max_prefered; 252453bd6749SJiri Pirko 252553bd6749SJiri Pirko if (ifp != ift->ifpub) 252653bd6749SJiri Pirko continue; 252753bd6749SJiri Pirko 252853bd6749SJiri Pirko /* RFC 4941 section 3.3: 252953bd6749SJiri Pirko * If a received option will extend the lifetime of a public 253053bd6749SJiri Pirko * address, the lifetimes of temporary addresses should 253153bd6749SJiri Pirko * be extended, subject to the overall constraint that no 253253bd6749SJiri Pirko * temporary addresses should ever remain "valid" or "preferred" 253353bd6749SJiri Pirko * for a time longer than (TEMP_VALID_LIFETIME) or 253453bd6749SJiri Pirko * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively. 253553bd6749SJiri Pirko */ 253653bd6749SJiri Pirko age = (now - ift->cstamp) / HZ; 253753bd6749SJiri Pirko max_valid = idev->cnf.temp_valid_lft - age; 253853bd6749SJiri Pirko if (max_valid < 0) 253953bd6749SJiri Pirko max_valid = 0; 254053bd6749SJiri Pirko 254153bd6749SJiri Pirko max_prefered = idev->cnf.temp_prefered_lft - 254276506a98SJiri Bohac idev->desync_factor - age; 254353bd6749SJiri Pirko if (max_prefered < 0) 254453bd6749SJiri Pirko max_prefered = 0; 254553bd6749SJiri Pirko 254653bd6749SJiri Pirko if (valid_lft > max_valid) 254753bd6749SJiri Pirko valid_lft = max_valid; 254853bd6749SJiri Pirko 254953bd6749SJiri Pirko if (prefered_lft > max_prefered) 255053bd6749SJiri Pirko prefered_lft = max_prefered; 255153bd6749SJiri Pirko 255253bd6749SJiri Pirko spin_lock(&ift->lock); 255353bd6749SJiri Pirko flags = ift->flags; 255453bd6749SJiri Pirko ift->valid_lft = valid_lft; 255553bd6749SJiri Pirko ift->prefered_lft = prefered_lft; 255653bd6749SJiri Pirko ift->tstamp = now; 255753bd6749SJiri Pirko if (prefered_lft > 0) 255853bd6749SJiri Pirko ift->flags &= ~IFA_F_DEPRECATED; 255953bd6749SJiri Pirko 256053bd6749SJiri Pirko spin_unlock(&ift->lock); 256153bd6749SJiri Pirko if (!(flags&IFA_F_TENTATIVE)) 256253bd6749SJiri Pirko ipv6_ifa_notify(0, ift); 256353bd6749SJiri Pirko } 256453bd6749SJiri Pirko 256553bd6749SJiri Pirko if ((create || list_empty(&idev->tempaddr_list)) && 256653bd6749SJiri Pirko idev->cnf.use_tempaddr > 0) { 256753bd6749SJiri Pirko /* When a new public address is created as described 256853bd6749SJiri Pirko * in [ADDRCONF], also create a new temporary address. 256953bd6749SJiri Pirko * Also create a temporary address if it's enabled but 257053bd6749SJiri Pirko * no temporary address currently exists. 257153bd6749SJiri Pirko */ 257253bd6749SJiri Pirko read_unlock_bh(&idev->lock); 2573969c5464SFernando Gont ipv6_create_tempaddr(ifp, false); 257453bd6749SJiri Pirko } else { 257553bd6749SJiri Pirko read_unlock_bh(&idev->lock); 257653bd6749SJiri Pirko } 257753bd6749SJiri Pirko } 257853bd6749SJiri Pirko 2579cc9da6ccSBjørn Mork static bool is_addr_mode_generate_stable(struct inet6_dev *idev) 2580cc9da6ccSBjørn Mork { 2581d35a00b8SFelix Jia return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY || 2582d35a00b8SFelix Jia idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM; 2583cc9da6ccSBjørn Mork } 2584cc9da6ccSBjørn Mork 2585cc84b3c6SAlexander Aring int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev, 25864f672235SAlexander Aring const struct prefix_info *pinfo, 25874f672235SAlexander Aring struct inet6_dev *in6_dev, 2588cc84b3c6SAlexander Aring const struct in6_addr *addr, int addr_type, 2589cc84b3c6SAlexander Aring u32 addr_flags, bool sllao, bool tokenized, 25904f672235SAlexander Aring __u32 valid_lft, u32 prefered_lft) 25914f672235SAlexander Aring { 25924f672235SAlexander Aring struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1); 259336268983SGuillaume Nault int create = 0, update_lft = 0; 25944f672235SAlexander Aring 25954f672235SAlexander Aring if (!ifp && valid_lft) { 25964f672235SAlexander Aring int max_addresses = in6_dev->cnf.max_addresses; 2597e6464b8cSDavid Ahern struct ifa6_config cfg = { 2598e6464b8cSDavid Ahern .pfx = addr, 2599e6464b8cSDavid Ahern .plen = pinfo->prefix_len, 2600e6464b8cSDavid Ahern .ifa_flags = addr_flags, 2601e6464b8cSDavid Ahern .valid_lft = valid_lft, 2602e6464b8cSDavid Ahern .preferred_lft = prefered_lft, 2603e6464b8cSDavid Ahern .scope = addr_type & IPV6_ADDR_SCOPE_MASK, 260447f0bd50SJacques de Laval .ifa_proto = IFAPROT_KERNEL_RA 2605e6464b8cSDavid Ahern }; 26064f672235SAlexander Aring 26074f672235SAlexander Aring #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 260835e015e1SMatteo Croce if ((net->ipv6.devconf_all->optimistic_dad || 260935e015e1SMatteo Croce in6_dev->cnf.optimistic_dad) && 26104f672235SAlexander Aring !net->ipv6.devconf_all->forwarding && sllao) 2611e6464b8cSDavid Ahern cfg.ifa_flags |= IFA_F_OPTIMISTIC; 26124f672235SAlexander Aring #endif 26134f672235SAlexander Aring 26144f672235SAlexander Aring /* Do not allow to create too much of autoconfigured 26154f672235SAlexander Aring * addresses; this would be too easy way to crash kernel. 26164f672235SAlexander Aring */ 26174f672235SAlexander Aring if (!max_addresses || 26184f672235SAlexander Aring ipv6_count_addresses(in6_dev) < max_addresses) 2619e6464b8cSDavid Ahern ifp = ipv6_add_addr(in6_dev, &cfg, false, NULL); 26204f672235SAlexander Aring 26214f672235SAlexander Aring if (IS_ERR_OR_NULL(ifp)) 26224f672235SAlexander Aring return -1; 26234f672235SAlexander Aring 26244f672235SAlexander Aring create = 1; 26254f672235SAlexander Aring spin_lock_bh(&ifp->lock); 26264f672235SAlexander Aring ifp->flags |= IFA_F_MANAGETEMPADDR; 26274f672235SAlexander Aring ifp->cstamp = jiffies; 26284f672235SAlexander Aring ifp->tokenized = tokenized; 26294f672235SAlexander Aring spin_unlock_bh(&ifp->lock); 26304f672235SAlexander Aring addrconf_dad_start(ifp); 26314f672235SAlexander Aring } 26324f672235SAlexander Aring 26334f672235SAlexander Aring if (ifp) { 26344f672235SAlexander Aring u32 flags; 26354f672235SAlexander Aring unsigned long now; 26364f672235SAlexander Aring u32 stored_lft; 26374f672235SAlexander Aring 263836268983SGuillaume Nault /* update lifetime (RFC2462 5.5.3 e) */ 26394f672235SAlexander Aring spin_lock_bh(&ifp->lock); 26404f672235SAlexander Aring now = jiffies; 26414f672235SAlexander Aring if (ifp->valid_lft > (now - ifp->tstamp) / HZ) 26424f672235SAlexander Aring stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ; 26434f672235SAlexander Aring else 26444f672235SAlexander Aring stored_lft = 0; 2645f85f94b8SLorenzo Bianconi if (!create && stored_lft) { 264636268983SGuillaume Nault const u32 minimum_lft = min_t(u32, 264736268983SGuillaume Nault stored_lft, MIN_VALID_LIFETIME); 264836268983SGuillaume Nault valid_lft = max(valid_lft, minimum_lft); 264936268983SGuillaume Nault 265036268983SGuillaume Nault /* RFC4862 Section 5.5.3e: 265136268983SGuillaume Nault * "Note that the preferred lifetime of the 265236268983SGuillaume Nault * corresponding address is always reset to 265336268983SGuillaume Nault * the Preferred Lifetime in the received 265436268983SGuillaume Nault * Prefix Information option, regardless of 265536268983SGuillaume Nault * whether the valid lifetime is also reset or 265636268983SGuillaume Nault * ignored." 265736268983SGuillaume Nault * 265836268983SGuillaume Nault * So we should always update prefered_lft here. 265936268983SGuillaume Nault */ 266036268983SGuillaume Nault update_lft = 1; 266136268983SGuillaume Nault } 266236268983SGuillaume Nault 266336268983SGuillaume Nault if (update_lft) { 26644f672235SAlexander Aring ifp->valid_lft = valid_lft; 26654f672235SAlexander Aring ifp->prefered_lft = prefered_lft; 26664f672235SAlexander Aring ifp->tstamp = now; 26674f672235SAlexander Aring flags = ifp->flags; 26684f672235SAlexander Aring ifp->flags &= ~IFA_F_DEPRECATED; 26694f672235SAlexander Aring spin_unlock_bh(&ifp->lock); 26704f672235SAlexander Aring 26714f672235SAlexander Aring if (!(flags&IFA_F_TENTATIVE)) 26724f672235SAlexander Aring ipv6_ifa_notify(0, ifp); 26734f672235SAlexander Aring } else 26744f672235SAlexander Aring spin_unlock_bh(&ifp->lock); 26754f672235SAlexander Aring 26764f672235SAlexander Aring manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft, 26774f672235SAlexander Aring create, now); 26784f672235SAlexander Aring 26794f672235SAlexander Aring in6_ifa_put(ifp); 26808805d13fSEric Dumazet addrconf_verify(net); 26814f672235SAlexander Aring } 26824f672235SAlexander Aring 26834f672235SAlexander Aring return 0; 26844f672235SAlexander Aring } 2685cc84b3c6SAlexander Aring EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr); 26864f672235SAlexander Aring 2687e6bff995SNeil Horman void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao) 26881da177e4SLinus Torvalds { 26891da177e4SLinus Torvalds struct prefix_info *pinfo; 26901da177e4SLinus Torvalds __u32 valid_lft; 26911da177e4SLinus Torvalds __u32 prefered_lft; 26924f672235SAlexander Aring int addr_type, err; 269364236f3fSHannes Frederic Sowa u32 addr_flags = 0; 26941da177e4SLinus Torvalds struct inet6_dev *in6_dev; 269556d417b1SBrian Haley struct net *net = dev_net(dev); 26961da177e4SLinus Torvalds 26971da177e4SLinus Torvalds pinfo = (struct prefix_info *) opt; 26981da177e4SLinus Torvalds 26991da177e4SLinus Torvalds if (len < sizeof(struct prefix_info)) { 2700e32ac250SJoe Perches netdev_dbg(dev, "addrconf: prefix option too short\n"); 27011da177e4SLinus Torvalds return; 27021da177e4SLinus Torvalds } 27031da177e4SLinus Torvalds 27041da177e4SLinus Torvalds /* 27051da177e4SLinus Torvalds * Validation checks ([ADDRCONF], page 19) 27061da177e4SLinus Torvalds */ 27071da177e4SLinus Torvalds 27081da177e4SLinus Torvalds addr_type = ipv6_addr_type(&pinfo->prefix); 27091da177e4SLinus Torvalds 27101da177e4SLinus Torvalds if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)) 27111da177e4SLinus Torvalds return; 27121da177e4SLinus Torvalds 27131da177e4SLinus Torvalds valid_lft = ntohl(pinfo->valid); 27141da177e4SLinus Torvalds prefered_lft = ntohl(pinfo->prefered); 27151da177e4SLinus Torvalds 27161da177e4SLinus Torvalds if (prefered_lft > valid_lft) { 2717e87cc472SJoe Perches net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n"); 27181da177e4SLinus Torvalds return; 27191da177e4SLinus Torvalds } 27201da177e4SLinus Torvalds 27211da177e4SLinus Torvalds in6_dev = in6_dev_get(dev); 27221da177e4SLinus Torvalds 272363159f29SIan Morris if (!in6_dev) { 2724e87cc472SJoe Perches net_dbg_ratelimited("addrconf: device %s not configured\n", 2725e87cc472SJoe Perches dev->name); 27261da177e4SLinus Torvalds return; 27271da177e4SLinus Torvalds } 27281da177e4SLinus Torvalds 27291da177e4SLinus Torvalds /* 27301da177e4SLinus Torvalds * Two things going on here: 27311da177e4SLinus Torvalds * 1) Add routes for on-link prefixes 27321da177e4SLinus Torvalds * 2) Configure prefixes with the auto flag set 27331da177e4SLinus Torvalds */ 27341da177e4SLinus Torvalds 27354bed72e4SYOSHIFUJI Hideaki if (pinfo->onlink) { 27368d1c802bSDavid Ahern struct fib6_info *rt; 27374bed72e4SYOSHIFUJI Hideaki unsigned long rt_expires; 27384bed72e4SYOSHIFUJI Hideaki 27391da177e4SLinus Torvalds /* Avoid arithmetic overflow. Really, we could 27406f704992SYOSHIFUJI Hideaki * save rt_expires in seconds, likely valid_lft, 27416f704992SYOSHIFUJI Hideaki * but it would require division in fib gc, that it 27426f704992SYOSHIFUJI Hideaki * not good. 27431da177e4SLinus Torvalds */ 27444bed72e4SYOSHIFUJI Hideaki if (HZ > USER_HZ) 27454bed72e4SYOSHIFUJI Hideaki rt_expires = addrconf_timeout_fixup(valid_lft, HZ); 27464bed72e4SYOSHIFUJI Hideaki else 27474bed72e4SYOSHIFUJI Hideaki rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ); 27483dd4bc68SYOSHIFUJI Hideaki 27494bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(rt_expires)) 27504bed72e4SYOSHIFUJI Hideaki rt_expires *= HZ; 27511da177e4SLinus Torvalds 275214ef37b6SAndreas Hofmeister rt = addrconf_get_prefix_route(&pinfo->prefix, 275314ef37b6SAndreas Hofmeister pinfo->prefix_len, 275414ef37b6SAndreas Hofmeister dev, 275514ef37b6SAndreas Hofmeister RTF_ADDRCONF | RTF_PREFIX_RT, 27562b2450caSDavid Ahern RTF_DEFAULT, true); 27571da177e4SLinus Torvalds 275814ef37b6SAndreas Hofmeister if (rt) { 27596f704992SYOSHIFUJI Hideaki /* Autoconf prefix route */ 27601da177e4SLinus Torvalds if (valid_lft == 0) { 276111dd74b3SRoopa Prabhu ip6_del_rt(net, rt, false); 27621da177e4SLinus Torvalds rt = NULL; 27634bed72e4SYOSHIFUJI Hideaki } else if (addrconf_finite_timeout(rt_expires)) { 27646f704992SYOSHIFUJI Hideaki /* not infinity */ 276514895687SDavid Ahern fib6_set_expires(rt, jiffies + rt_expires); 27666f704992SYOSHIFUJI Hideaki } else { 276714895687SDavid Ahern fib6_clean_expires(rt); 27681da177e4SLinus Torvalds } 27691da177e4SLinus Torvalds } else if (valid_lft) { 27706f704992SYOSHIFUJI Hideaki clock_t expires = 0; 27714bed72e4SYOSHIFUJI Hideaki int flags = RTF_ADDRCONF | RTF_PREFIX_RT; 27724bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(rt_expires)) { 27736f704992SYOSHIFUJI Hideaki /* not infinity */ 27746f704992SYOSHIFUJI Hideaki flags |= RTF_EXPIRES; 27756f704992SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(rt_expires); 27766f704992SYOSHIFUJI Hideaki } 27771da177e4SLinus Torvalds addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len, 27788308f3ffSDavid Ahern 0, dev, expires, flags, 27798308f3ffSDavid Ahern GFP_ATOMIC); 27801da177e4SLinus Torvalds } 278193531c67SDavid Ahern fib6_info_release(rt); 27821da177e4SLinus Torvalds } 27831da177e4SLinus Torvalds 27841da177e4SLinus Torvalds /* Try to figure out our local address for this prefix */ 27851da177e4SLinus Torvalds 27861da177e4SLinus Torvalds if (pinfo->autoconf && in6_dev->cnf.autoconf) { 27871da177e4SLinus Torvalds struct in6_addr addr; 2788f997c55cSAlexander Aring bool tokenized = false, dev_addr_generated = false; 27891da177e4SLinus Torvalds 27901da177e4SLinus Torvalds if (pinfo->prefix_len == 64) { 27911da177e4SLinus Torvalds memcpy(&addr, &pinfo->prefix, 8); 2792f53adae4SDaniel Borkmann 2793f53adae4SDaniel Borkmann if (!ipv6_addr_any(&in6_dev->token)) { 2794f53adae4SDaniel Borkmann read_lock_bh(&in6_dev->lock); 2795f53adae4SDaniel Borkmann memcpy(addr.s6_addr + 8, 2796f53adae4SDaniel Borkmann in6_dev->token.s6_addr + 8, 8); 2797f53adae4SDaniel Borkmann read_unlock_bh(&in6_dev->lock); 2798617fe29dSDaniel Borkmann tokenized = true; 2799cc9da6ccSBjørn Mork } else if (is_addr_mode_generate_stable(in6_dev) && 2800622c81d5SHannes Frederic Sowa !ipv6_generate_stable_address(&addr, 0, 2801622c81d5SHannes Frederic Sowa in6_dev)) { 280264236f3fSHannes Frederic Sowa addr_flags |= IFA_F_STABLE_PRIVACY; 2803622c81d5SHannes Frederic Sowa goto ok; 2804f53adae4SDaniel Borkmann } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) && 28051da177e4SLinus Torvalds ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) { 28064f672235SAlexander Aring goto put; 2807f997c55cSAlexander Aring } else { 2808f997c55cSAlexander Aring dev_addr_generated = true; 28091da177e4SLinus Torvalds } 28101da177e4SLinus Torvalds goto ok; 28111da177e4SLinus Torvalds } 2812e87cc472SJoe Perches net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n", 28131da177e4SLinus Torvalds pinfo->prefix_len); 28144f672235SAlexander Aring goto put; 28151da177e4SLinus Torvalds 28161da177e4SLinus Torvalds ok: 28174f672235SAlexander Aring err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, 28184f672235SAlexander Aring &addr, addr_type, 28194f672235SAlexander Aring addr_flags, sllao, 28204f672235SAlexander Aring tokenized, valid_lft, 28218a226b2cSJiri Benc prefered_lft); 28224f672235SAlexander Aring if (err) 28234f672235SAlexander Aring goto put; 2824f997c55cSAlexander Aring 2825f997c55cSAlexander Aring /* Ignore error case here because previous prefix add addr was 2826f997c55cSAlexander Aring * successful which will be notified. 2827f997c55cSAlexander Aring */ 2828f997c55cSAlexander Aring ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr, 2829f997c55cSAlexander Aring addr_type, addr_flags, sllao, 2830f997c55cSAlexander Aring tokenized, valid_lft, 2831f997c55cSAlexander Aring prefered_lft, 2832f997c55cSAlexander Aring dev_addr_generated); 28331da177e4SLinus Torvalds } 28341da177e4SLinus Torvalds inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo); 28354f672235SAlexander Aring put: 28361da177e4SLinus Torvalds in6_dev_put(in6_dev); 28371da177e4SLinus Torvalds } 28381da177e4SLinus Torvalds 283968ad6886SChristoph Hellwig static int addrconf_set_sit_dstaddr(struct net *net, struct net_device *dev, 284068ad6886SChristoph Hellwig struct in6_ifreq *ireq) 284168ad6886SChristoph Hellwig { 284268ad6886SChristoph Hellwig struct ip_tunnel_parm p = { }; 284368ad6886SChristoph Hellwig int err; 284468ad6886SChristoph Hellwig 284568ad6886SChristoph Hellwig if (!(ipv6_addr_type(&ireq->ifr6_addr) & IPV6_ADDR_COMPATv4)) 284668ad6886SChristoph Hellwig return -EADDRNOTAVAIL; 284768ad6886SChristoph Hellwig 284868ad6886SChristoph Hellwig p.iph.daddr = ireq->ifr6_addr.s6_addr32[3]; 284968ad6886SChristoph Hellwig p.iph.version = 4; 285068ad6886SChristoph Hellwig p.iph.ihl = 5; 285168ad6886SChristoph Hellwig p.iph.protocol = IPPROTO_IPV6; 285268ad6886SChristoph Hellwig p.iph.ttl = 64; 285368ad6886SChristoph Hellwig 28548e3db0bbSChristoph Hellwig if (!dev->netdev_ops->ndo_tunnel_ctl) 285568ad6886SChristoph Hellwig return -EOPNOTSUPP; 28568e3db0bbSChristoph Hellwig err = dev->netdev_ops->ndo_tunnel_ctl(dev, &p, SIOCADDTUNNEL); 285768ad6886SChristoph Hellwig if (err) 285868ad6886SChristoph Hellwig return err; 285968ad6886SChristoph Hellwig 286068ad6886SChristoph Hellwig dev = __dev_get_by_name(net, p.name); 286168ad6886SChristoph Hellwig if (!dev) 286268ad6886SChristoph Hellwig return -ENOBUFS; 286368ad6886SChristoph Hellwig return dev_open(dev, NULL); 286468ad6886SChristoph Hellwig } 286568ad6886SChristoph Hellwig 28661da177e4SLinus Torvalds /* 28671da177e4SLinus Torvalds * Set destination address. 28681da177e4SLinus Torvalds * Special case for SIT interfaces where we create a new "virtual" 28691da177e4SLinus Torvalds * device. 28701da177e4SLinus Torvalds */ 2871af284937SDaniel Lezcano int addrconf_set_dstaddr(struct net *net, void __user *arg) 28721da177e4SLinus Torvalds { 28731da177e4SLinus Torvalds struct net_device *dev; 287468ad6886SChristoph Hellwig struct in6_ifreq ireq; 287568ad6886SChristoph Hellwig int err = -ENODEV; 28761da177e4SLinus Torvalds 2877f0988460SChristoph Hellwig if (!IS_ENABLED(CONFIG_IPV6_SIT)) 2878f0988460SChristoph Hellwig return -ENODEV; 287968ad6886SChristoph Hellwig if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 288068ad6886SChristoph Hellwig return -EFAULT; 28811da177e4SLinus Torvalds 28821da177e4SLinus Torvalds rtnl_lock(); 2883af284937SDaniel Lezcano dev = __dev_get_by_index(net, ireq.ifr6_ifindex); 288468ad6886SChristoph Hellwig if (dev && dev->type == ARPHRD_SIT) 288568ad6886SChristoph Hellwig err = addrconf_set_sit_dstaddr(net, dev, &ireq); 28861da177e4SLinus Torvalds rtnl_unlock(); 28871da177e4SLinus Torvalds return err; 28881da177e4SLinus Torvalds } 28891da177e4SLinus Torvalds 289093a714d6SMadhu Challa static int ipv6_mc_config(struct sock *sk, bool join, 289193a714d6SMadhu Challa const struct in6_addr *addr, int ifindex) 289293a714d6SMadhu Challa { 289393a714d6SMadhu Challa int ret; 289493a714d6SMadhu Challa 289593a714d6SMadhu Challa ASSERT_RTNL(); 289693a714d6SMadhu Challa 289793a714d6SMadhu Challa lock_sock(sk); 289893a714d6SMadhu Challa if (join) 289954ff9ef3SMarcelo Ricardo Leitner ret = ipv6_sock_mc_join(sk, ifindex, addr); 290093a714d6SMadhu Challa else 290154ff9ef3SMarcelo Ricardo Leitner ret = ipv6_sock_mc_drop(sk, ifindex, addr); 290293a714d6SMadhu Challa release_sock(sk); 290393a714d6SMadhu Challa 290493a714d6SMadhu Challa return ret; 290593a714d6SMadhu Challa } 290693a714d6SMadhu Challa 29071da177e4SLinus Torvalds /* 29081da177e4SLinus Torvalds * Manual configuration of address on an interface 29091da177e4SLinus Torvalds */ 2910479840ffSJiri Pirko static int inet6_addr_add(struct net *net, int ifindex, 291119b1518cSDavid Ahern struct ifa6_config *cfg, 2912de95e047SDavid Ahern struct netlink_ext_ack *extack) 29131da177e4SLinus Torvalds { 29141da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 29151da177e4SLinus Torvalds struct inet6_dev *idev; 29161da177e4SLinus Torvalds struct net_device *dev; 291793a714d6SMadhu Challa unsigned long timeout; 291893a714d6SMadhu Challa clock_t expires; 29196f704992SYOSHIFUJI Hideaki u32 flags; 29201da177e4SLinus Torvalds 29211da177e4SLinus Torvalds ASSERT_RTNL(); 29221da177e4SLinus Torvalds 292319b1518cSDavid Ahern if (cfg->plen > 128) 292424ef0da7SThomas Graf return -EINVAL; 292524ef0da7SThomas Graf 29260778769dSNoriaki TAKAMIYA /* check the lifetime */ 292719b1518cSDavid Ahern if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft) 29280778769dSNoriaki TAKAMIYA return -EINVAL; 29290778769dSNoriaki TAKAMIYA 293019b1518cSDavid Ahern if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR && cfg->plen != 64) 293153bd6749SJiri Pirko return -EINVAL; 293253bd6749SJiri Pirko 2933af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifindex); 2934af284937SDaniel Lezcano if (!dev) 29351da177e4SLinus Torvalds return -ENODEV; 29361da177e4SLinus Torvalds 293764e724f6SBrian Haley idev = addrconf_add_dev(dev); 293864e724f6SBrian Haley if (IS_ERR(idev)) 293964e724f6SBrian Haley return PTR_ERR(idev); 29401da177e4SLinus Torvalds 294119b1518cSDavid Ahern if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) { 294293a714d6SMadhu Challa int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk, 294319b1518cSDavid Ahern true, cfg->pfx, ifindex); 294493a714d6SMadhu Challa 294593a714d6SMadhu Challa if (ret < 0) 294693a714d6SMadhu Challa return ret; 294793a714d6SMadhu Challa } 294893a714d6SMadhu Challa 294919b1518cSDavid Ahern cfg->scope = ipv6_addr_scope(cfg->pfx); 29501da177e4SLinus Torvalds 295119b1518cSDavid Ahern timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ); 29524bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 29534bed72e4SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(timeout * HZ); 295419b1518cSDavid Ahern cfg->valid_lft = timeout; 29556f704992SYOSHIFUJI Hideaki flags = RTF_EXPIRES; 29564bed72e4SYOSHIFUJI Hideaki } else { 29574bed72e4SYOSHIFUJI Hideaki expires = 0; 29584bed72e4SYOSHIFUJI Hideaki flags = 0; 295919b1518cSDavid Ahern cfg->ifa_flags |= IFA_F_PERMANENT; 29606f704992SYOSHIFUJI Hideaki } 29610778769dSNoriaki TAKAMIYA 296219b1518cSDavid Ahern timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ); 29634bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 29644bed72e4SYOSHIFUJI Hideaki if (timeout == 0) 296519b1518cSDavid Ahern cfg->ifa_flags |= IFA_F_DEPRECATED; 296619b1518cSDavid Ahern cfg->preferred_lft = timeout; 29674bed72e4SYOSHIFUJI Hideaki } 29680778769dSNoriaki TAKAMIYA 296919b1518cSDavid Ahern ifp = ipv6_add_addr(idev, cfg, true, extack); 29701da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 297119b1518cSDavid Ahern if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) { 29728308f3ffSDavid Ahern addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 29738308f3ffSDavid Ahern ifp->rt_priority, dev, expires, 29748308f3ffSDavid Ahern flags, GFP_KERNEL); 2975761aac73SThomas Haller } 2976761aac73SThomas Haller 2977a2d481b3SLorenzo Bianconi /* Send a netlink notification if DAD is enabled and 2978a2d481b3SLorenzo Bianconi * optimistic flag is not set 2979a2d481b3SLorenzo Bianconi */ 2980a2d481b3SLorenzo Bianconi if (!(ifp->flags & (IFA_F_OPTIMISTIC | IFA_F_NODAD))) 2981a2d481b3SLorenzo Bianconi ipv6_ifa_notify(0, ifp); 298295c385b4SNeil Horman /* 298395c385b4SNeil Horman * Note that section 3.1 of RFC 4429 indicates 298495c385b4SNeil Horman * that the Optimistic flag should not be set for 298595c385b4SNeil Horman * manually configured addresses 298695c385b4SNeil Horman */ 2987cf22f9a2SDavid S. Miller addrconf_dad_start(ifp); 298819b1518cSDavid Ahern if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR) 298919b1518cSDavid Ahern manage_tempaddrs(idev, ifp, cfg->valid_lft, 299019b1518cSDavid Ahern cfg->preferred_lft, true, jiffies); 29911da177e4SLinus Torvalds in6_ifa_put(ifp); 29928805d13fSEric Dumazet addrconf_verify_rtnl(net); 29931da177e4SLinus Torvalds return 0; 299419b1518cSDavid Ahern } else if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) { 299519b1518cSDavid Ahern ipv6_mc_config(net->ipv6.mc_autojoin_sk, false, 299619b1518cSDavid Ahern cfg->pfx, ifindex); 29971da177e4SLinus Torvalds } 29981da177e4SLinus Torvalds 29991da177e4SLinus Torvalds return PTR_ERR(ifp); 30001da177e4SLinus Torvalds } 30011da177e4SLinus Torvalds 30026046d5b4SHeiner Kallweit static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags, 30036046d5b4SHeiner Kallweit const struct in6_addr *pfx, unsigned int plen) 30041da177e4SLinus Torvalds { 30051da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 30061da177e4SLinus Torvalds struct inet6_dev *idev; 30071da177e4SLinus Torvalds struct net_device *dev; 30081da177e4SLinus Torvalds 300924ef0da7SThomas Graf if (plen > 128) 301024ef0da7SThomas Graf return -EINVAL; 301124ef0da7SThomas Graf 3012af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifindex); 3013af284937SDaniel Lezcano if (!dev) 30141da177e4SLinus Torvalds return -ENODEV; 30151da177e4SLinus Torvalds 3016e5d08d71SIan Morris idev = __in6_dev_get(dev); 301763159f29SIan Morris if (!idev) 30181da177e4SLinus Torvalds return -ENXIO; 30191da177e4SLinus Torvalds 30201da177e4SLinus Torvalds read_lock_bh(&idev->lock); 3021502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 30221da177e4SLinus Torvalds if (ifp->prefix_len == plen && 30231da177e4SLinus Torvalds ipv6_addr_equal(pfx, &ifp->addr)) { 30241da177e4SLinus Torvalds in6_ifa_hold(ifp); 30251da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 30261da177e4SLinus Torvalds 30276046d5b4SHeiner Kallweit if (!(ifp->flags & IFA_F_TEMPORARY) && 30286046d5b4SHeiner Kallweit (ifa_flags & IFA_F_MANAGETEMPADDR)) 30296046d5b4SHeiner Kallweit manage_tempaddrs(idev, ifp, 0, 0, false, 30306046d5b4SHeiner Kallweit jiffies); 30311da177e4SLinus Torvalds ipv6_del_addr(ifp); 30328805d13fSEric Dumazet addrconf_verify_rtnl(net); 303393a714d6SMadhu Challa if (ipv6_addr_is_multicast(pfx)) { 303493a714d6SMadhu Challa ipv6_mc_config(net->ipv6.mc_autojoin_sk, 303593a714d6SMadhu Challa false, pfx, dev->ifindex); 303693a714d6SMadhu Challa } 30371da177e4SLinus Torvalds return 0; 30381da177e4SLinus Torvalds } 30391da177e4SLinus Torvalds } 30401da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 30411da177e4SLinus Torvalds return -EADDRNOTAVAIL; 30421da177e4SLinus Torvalds } 30431da177e4SLinus Torvalds 30441da177e4SLinus Torvalds 3045af284937SDaniel Lezcano int addrconf_add_ifaddr(struct net *net, void __user *arg) 30461da177e4SLinus Torvalds { 304719b1518cSDavid Ahern struct ifa6_config cfg = { 304819b1518cSDavid Ahern .ifa_flags = IFA_F_PERMANENT, 304919b1518cSDavid Ahern .preferred_lft = INFINITY_LIFE_TIME, 305019b1518cSDavid Ahern .valid_lft = INFINITY_LIFE_TIME, 305119b1518cSDavid Ahern }; 30521da177e4SLinus Torvalds struct in6_ifreq ireq; 30531da177e4SLinus Torvalds int err; 30541da177e4SLinus Torvalds 3055af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 30561da177e4SLinus Torvalds return -EPERM; 30571da177e4SLinus Torvalds 30581da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 30591da177e4SLinus Torvalds return -EFAULT; 30601da177e4SLinus Torvalds 306119b1518cSDavid Ahern cfg.pfx = &ireq.ifr6_addr; 306219b1518cSDavid Ahern cfg.plen = ireq.ifr6_prefixlen; 306319b1518cSDavid Ahern 30641da177e4SLinus Torvalds rtnl_lock(); 306519b1518cSDavid Ahern err = inet6_addr_add(net, ireq.ifr6_ifindex, &cfg, NULL); 30661da177e4SLinus Torvalds rtnl_unlock(); 30671da177e4SLinus Torvalds return err; 30681da177e4SLinus Torvalds } 30691da177e4SLinus Torvalds 3070af284937SDaniel Lezcano int addrconf_del_ifaddr(struct net *net, void __user *arg) 30711da177e4SLinus Torvalds { 30721da177e4SLinus Torvalds struct in6_ifreq ireq; 30731da177e4SLinus Torvalds int err; 30741da177e4SLinus Torvalds 3075af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 30761da177e4SLinus Torvalds return -EPERM; 30771da177e4SLinus Torvalds 30781da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 30791da177e4SLinus Torvalds return -EFAULT; 30801da177e4SLinus Torvalds 30811da177e4SLinus Torvalds rtnl_lock(); 30826046d5b4SHeiner Kallweit err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr, 3083af284937SDaniel Lezcano ireq.ifr6_prefixlen); 30841da177e4SLinus Torvalds rtnl_unlock(); 30851da177e4SLinus Torvalds return err; 30861da177e4SLinus Torvalds } 30871da177e4SLinus Torvalds 3088b5f348e5SIlpo Järvinen static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr, 308947f0bd50SJacques de Laval int plen, int scope, u8 proto) 3090b5f348e5SIlpo Järvinen { 3091b5f348e5SIlpo Järvinen struct inet6_ifaddr *ifp; 3092e6464b8cSDavid Ahern struct ifa6_config cfg = { 3093e6464b8cSDavid Ahern .pfx = addr, 3094e6464b8cSDavid Ahern .plen = plen, 3095e6464b8cSDavid Ahern .ifa_flags = IFA_F_PERMANENT, 3096e6464b8cSDavid Ahern .valid_lft = INFINITY_LIFE_TIME, 3097e6464b8cSDavid Ahern .preferred_lft = INFINITY_LIFE_TIME, 309847f0bd50SJacques de Laval .scope = scope, 309947f0bd50SJacques de Laval .ifa_proto = proto 3100e6464b8cSDavid Ahern }; 3101b5f348e5SIlpo Järvinen 3102e6464b8cSDavid Ahern ifp = ipv6_add_addr(idev, &cfg, true, NULL); 3103b5f348e5SIlpo Järvinen if (!IS_ERR(ifp)) { 3104b5f348e5SIlpo Järvinen spin_lock_bh(&ifp->lock); 3105b5f348e5SIlpo Järvinen ifp->flags &= ~IFA_F_TENTATIVE; 3106b5f348e5SIlpo Järvinen spin_unlock_bh(&ifp->lock); 3107764d3be6SPaolo Abeni rt_genid_bump_ipv6(dev_net(idev->dev)); 3108b5f348e5SIlpo Järvinen ipv6_ifa_notify(RTM_NEWADDR, ifp); 3109b5f348e5SIlpo Järvinen in6_ifa_put(ifp); 3110b5f348e5SIlpo Järvinen } 3111b5f348e5SIlpo Järvinen } 3112b5f348e5SIlpo Järvinen 3113e5dd7294SAntonio Quartulli #if IS_ENABLED(CONFIG_IPV6_SIT) || IS_ENABLED(CONFIG_NET_IPGRE) || IS_ENABLED(CONFIG_IPV6_GRE) 3114e5dd7294SAntonio Quartulli static void add_v4_addrs(struct inet6_dev *idev) 31151da177e4SLinus Torvalds { 31161da177e4SLinus Torvalds struct in6_addr addr; 31171da177e4SLinus Torvalds struct net_device *dev; 3118c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 3119e5dd7294SAntonio Quartulli int scope, plen, offset = 0; 3120f0e2acfaSNicolas Dichtel u32 pflags = 0; 31211da177e4SLinus Torvalds 31221da177e4SLinus Torvalds ASSERT_RTNL(); 31231da177e4SLinus Torvalds 31241da177e4SLinus Torvalds memset(&addr, 0, sizeof(struct in6_addr)); 3125e5dd7294SAntonio Quartulli /* in case of IP6GRE the dev_addr is an IPv6 and therefore we use only the last 4 bytes */ 3126e5dd7294SAntonio Quartulli if (idev->dev->addr_len == sizeof(struct in6_addr)) 3127e5dd7294SAntonio Quartulli offset = sizeof(struct in6_addr) - 4; 3128e5dd7294SAntonio Quartulli memcpy(&addr.s6_addr32[3], idev->dev->dev_addr + offset, 4); 31291da177e4SLinus Torvalds 313030e2291fSThomas Winter if (!(idev->dev->flags & IFF_POINTOPOINT) && idev->dev->type == ARPHRD_SIT) { 313130e2291fSThomas Winter scope = IPV6_ADDR_COMPATv4; 313230e2291fSThomas Winter plen = 96; 313330e2291fSThomas Winter pflags |= RTF_NONEXTHOP; 313430e2291fSThomas Winter } else { 313561e18ce7SStephen Suryaputra if (idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_NONE) 313661e18ce7SStephen Suryaputra return; 313761e18ce7SStephen Suryaputra 31381da177e4SLinus Torvalds addr.s6_addr32[0] = htonl(0xfe800000); 31391da177e4SLinus Torvalds scope = IFA_LINK; 3140929c9cf3SNicolas Dichtel plen = 64; 31411da177e4SLinus Torvalds } 31421da177e4SLinus Torvalds 31431da177e4SLinus Torvalds if (addr.s6_addr32[3]) { 314447f0bd50SJacques de Laval add_addr(idev, &addr, plen, scope, IFAPROT_UNSPEC); 31458308f3ffSDavid Ahern addrconf_prefix_route(&addr, plen, 0, idev->dev, 0, pflags, 3146e500c6d3SCong Wang GFP_KERNEL); 31471da177e4SLinus Torvalds return; 31481da177e4SLinus Torvalds } 31491da177e4SLinus Torvalds 31506fda7350SBenjamin Thery for_each_netdev(net, dev) { 3151e5ed6399SHerbert Xu struct in_device *in_dev = __in_dev_get_rtnl(dev); 31521da177e4SLinus Torvalds if (in_dev && (dev->flags & IFF_UP)) { 31531da177e4SLinus Torvalds struct in_ifaddr *ifa; 31541da177e4SLinus Torvalds int flag = scope; 31551da177e4SLinus Torvalds 3156cd5a411dSFlorian Westphal in_dev_for_each_ifa_rtnl(ifa, in_dev) { 31571da177e4SLinus Torvalds addr.s6_addr32[3] = ifa->ifa_local; 31581da177e4SLinus Torvalds 31591da177e4SLinus Torvalds if (ifa->ifa_scope == RT_SCOPE_LINK) 31601da177e4SLinus Torvalds continue; 31611da177e4SLinus Torvalds if (ifa->ifa_scope >= RT_SCOPE_HOST) { 31621da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) 31631da177e4SLinus Torvalds continue; 31641da177e4SLinus Torvalds flag |= IFA_HOST; 31651da177e4SLinus Torvalds } 31661da177e4SLinus Torvalds 316747f0bd50SJacques de Laval add_addr(idev, &addr, plen, flag, 316847f0bd50SJacques de Laval IFAPROT_UNSPEC); 31698308f3ffSDavid Ahern addrconf_prefix_route(&addr, plen, 0, idev->dev, 3170e500c6d3SCong Wang 0, pflags, GFP_KERNEL); 31711da177e4SLinus Torvalds } 31721da177e4SLinus Torvalds } 31731da177e4SLinus Torvalds } 31741da177e4SLinus Torvalds } 31750be669bbSJoerg Roedel #endif 31761da177e4SLinus Torvalds 31771da177e4SLinus Torvalds static void init_loopback(struct net_device *dev) 31781da177e4SLinus Torvalds { 31791da177e4SLinus Torvalds struct inet6_dev *idev; 31801da177e4SLinus Torvalds 31811da177e4SLinus Torvalds /* ::1 */ 31821da177e4SLinus Torvalds 31831da177e4SLinus Torvalds ASSERT_RTNL(); 31841da177e4SLinus Torvalds 3185e5d08d71SIan Morris idev = ipv6_find_idev(dev); 3186db0b99f5SSabrina Dubroca if (IS_ERR(idev)) { 318791df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 31881da177e4SLinus Torvalds return; 31891da177e4SLinus Torvalds } 31901da177e4SLinus Torvalds 319147f0bd50SJacques de Laval add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFAPROT_KERNEL_LO); 31921da177e4SLinus Torvalds } 31931da177e4SLinus Torvalds 31942ad3ed59SAlexander Aring void addrconf_add_linklocal(struct inet6_dev *idev, 319564236f3fSHannes Frederic Sowa const struct in6_addr *addr, u32 flags) 31961da177e4SLinus Torvalds { 3197e6464b8cSDavid Ahern struct ifa6_config cfg = { 3198e6464b8cSDavid Ahern .pfx = addr, 3199e6464b8cSDavid Ahern .plen = 64, 3200e6464b8cSDavid Ahern .ifa_flags = flags | IFA_F_PERMANENT, 3201e6464b8cSDavid Ahern .valid_lft = INFINITY_LIFE_TIME, 3202e6464b8cSDavid Ahern .preferred_lft = INFINITY_LIFE_TIME, 320347f0bd50SJacques de Laval .scope = IFA_LINK, 320447f0bd50SJacques de Laval .ifa_proto = IFAPROT_KERNEL_LL 3205e6464b8cSDavid Ahern }; 32061da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 32071da177e4SLinus Torvalds 320895c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 320935e015e1SMatteo Croce if ((dev_net(idev->dev)->ipv6.devconf_all->optimistic_dad || 321035e015e1SMatteo Croce idev->cnf.optimistic_dad) && 3211702beb87SDavid Miller !dev_net(idev->dev)->ipv6.devconf_all->forwarding) 3212e6464b8cSDavid Ahern cfg.ifa_flags |= IFA_F_OPTIMISTIC; 321395c385b4SNeil Horman #endif 321495c385b4SNeil Horman 3215e6464b8cSDavid Ahern ifp = ipv6_add_addr(idev, &cfg, true, NULL); 32161da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 32178308f3ffSDavid Ahern addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 0, idev->dev, 3218acb54e3cSDavid Ahern 0, 0, GFP_ATOMIC); 3219cf22f9a2SDavid S. Miller addrconf_dad_start(ifp); 32201da177e4SLinus Torvalds in6_ifa_put(ifp); 32211da177e4SLinus Torvalds } 32221da177e4SLinus Torvalds } 32232ad3ed59SAlexander Aring EXPORT_SYMBOL_GPL(addrconf_add_linklocal); 32241da177e4SLinus Torvalds 3225622c81d5SHannes Frederic Sowa static bool ipv6_reserved_interfaceid(struct in6_addr address) 3226622c81d5SHannes Frederic Sowa { 3227622c81d5SHannes Frederic Sowa if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0) 3228622c81d5SHannes Frederic Sowa return true; 3229622c81d5SHannes Frederic Sowa 3230622c81d5SHannes Frederic Sowa if (address.s6_addr32[2] == htonl(0x02005eff) && 3231622c81d5SHannes Frederic Sowa ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000))) 3232622c81d5SHannes Frederic Sowa return true; 3233622c81d5SHannes Frederic Sowa 3234622c81d5SHannes Frederic Sowa if (address.s6_addr32[2] == htonl(0xfdffffff) && 3235622c81d5SHannes Frederic Sowa ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80))) 3236622c81d5SHannes Frederic Sowa return true; 3237622c81d5SHannes Frederic Sowa 3238622c81d5SHannes Frederic Sowa return false; 3239622c81d5SHannes Frederic Sowa } 3240622c81d5SHannes Frederic Sowa 3241622c81d5SHannes Frederic Sowa static int ipv6_generate_stable_address(struct in6_addr *address, 3242622c81d5SHannes Frederic Sowa u8 dad_count, 3243622c81d5SHannes Frederic Sowa const struct inet6_dev *idev) 3244622c81d5SHannes Frederic Sowa { 3245622c81d5SHannes Frederic Sowa static DEFINE_SPINLOCK(lock); 32466b0b0fa2SEric Biggers static __u32 digest[SHA1_DIGEST_WORDS]; 32476b0b0fa2SEric Biggers static __u32 workspace[SHA1_WORKSPACE_WORDS]; 3248622c81d5SHannes Frederic Sowa 3249622c81d5SHannes Frederic Sowa static union { 32506b0b0fa2SEric Biggers char __data[SHA1_BLOCK_SIZE]; 3251622c81d5SHannes Frederic Sowa struct { 3252622c81d5SHannes Frederic Sowa struct in6_addr secret; 3253ff40217eSHannes Frederic Sowa __be32 prefix[2]; 3254622c81d5SHannes Frederic Sowa unsigned char hwaddr[MAX_ADDR_LEN]; 3255622c81d5SHannes Frederic Sowa u8 dad_count; 3256622c81d5SHannes Frederic Sowa } __packed; 3257622c81d5SHannes Frederic Sowa } data; 3258622c81d5SHannes Frederic Sowa 3259622c81d5SHannes Frederic Sowa struct in6_addr secret; 3260622c81d5SHannes Frederic Sowa struct in6_addr temp; 3261622c81d5SHannes Frederic Sowa struct net *net = dev_net(idev->dev); 3262622c81d5SHannes Frederic Sowa 3263622c81d5SHannes Frederic Sowa BUILD_BUG_ON(sizeof(data.__data) != sizeof(data)); 3264622c81d5SHannes Frederic Sowa 3265622c81d5SHannes Frederic Sowa if (idev->cnf.stable_secret.initialized) 3266622c81d5SHannes Frederic Sowa secret = idev->cnf.stable_secret.secret; 3267622c81d5SHannes Frederic Sowa else if (net->ipv6.devconf_dflt->stable_secret.initialized) 3268622c81d5SHannes Frederic Sowa secret = net->ipv6.devconf_dflt->stable_secret.secret; 3269622c81d5SHannes Frederic Sowa else 3270622c81d5SHannes Frederic Sowa return -1; 3271622c81d5SHannes Frederic Sowa 3272622c81d5SHannes Frederic Sowa retry: 3273622c81d5SHannes Frederic Sowa spin_lock_bh(&lock); 3274622c81d5SHannes Frederic Sowa 32756b0b0fa2SEric Biggers sha1_init(digest); 3276622c81d5SHannes Frederic Sowa memset(&data, 0, sizeof(data)); 3277622c81d5SHannes Frederic Sowa memset(workspace, 0, sizeof(workspace)); 3278622c81d5SHannes Frederic Sowa memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len); 3279ff40217eSHannes Frederic Sowa data.prefix[0] = address->s6_addr32[0]; 3280ff40217eSHannes Frederic Sowa data.prefix[1] = address->s6_addr32[1]; 3281622c81d5SHannes Frederic Sowa data.secret = secret; 3282622c81d5SHannes Frederic Sowa data.dad_count = dad_count; 3283622c81d5SHannes Frederic Sowa 32846b0b0fa2SEric Biggers sha1_transform(digest, data.__data, workspace); 3285622c81d5SHannes Frederic Sowa 3286622c81d5SHannes Frederic Sowa temp = *address; 3287ff40217eSHannes Frederic Sowa temp.s6_addr32[2] = (__force __be32)digest[0]; 3288ff40217eSHannes Frederic Sowa temp.s6_addr32[3] = (__force __be32)digest[1]; 3289622c81d5SHannes Frederic Sowa 3290622c81d5SHannes Frederic Sowa spin_unlock_bh(&lock); 3291622c81d5SHannes Frederic Sowa 3292622c81d5SHannes Frederic Sowa if (ipv6_reserved_interfaceid(temp)) { 3293622c81d5SHannes Frederic Sowa dad_count++; 32941855b7c3SHannes Frederic Sowa if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries) 3295622c81d5SHannes Frederic Sowa return -1; 3296622c81d5SHannes Frederic Sowa goto retry; 3297622c81d5SHannes Frederic Sowa } 3298622c81d5SHannes Frederic Sowa 3299622c81d5SHannes Frederic Sowa *address = temp; 3300622c81d5SHannes Frederic Sowa return 0; 3301622c81d5SHannes Frederic Sowa } 3302622c81d5SHannes Frederic Sowa 3303cc9da6ccSBjørn Mork static void ipv6_gen_mode_random_init(struct inet6_dev *idev) 3304cc9da6ccSBjørn Mork { 3305cc9da6ccSBjørn Mork struct ipv6_stable_secret *s = &idev->cnf.stable_secret; 3306cc9da6ccSBjørn Mork 3307cc9da6ccSBjørn Mork if (s->initialized) 3308cc9da6ccSBjørn Mork return; 3309cc9da6ccSBjørn Mork s = &idev->cnf.stable_secret; 3310cc9da6ccSBjørn Mork get_random_bytes(&s->secret, sizeof(s->secret)); 3311cc9da6ccSBjørn Mork s->initialized = true; 3312cc9da6ccSBjørn Mork } 3313cc9da6ccSBjørn Mork 3314bc91b0f0SJiri Pirko static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route) 3315bc91b0f0SJiri Pirko { 3316bc91b0f0SJiri Pirko struct in6_addr addr; 3317bc91b0f0SJiri Pirko 3318ca254490SDavid Ahern /* no link local addresses on L3 master devices */ 3319ca254490SDavid Ahern if (netif_is_l3_master(idev->dev)) 3320ca254490SDavid Ahern return; 3321ca254490SDavid Ahern 3322744fdc82SJarod Wilson /* no link local addresses on devices flagged as slaves */ 33238a321cf7SXin Long if (idev->dev->priv_flags & IFF_NO_ADDRCONF) 3324744fdc82SJarod Wilson return; 3325744fdc82SJarod Wilson 3326bc91b0f0SJiri Pirko ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0); 3327622c81d5SHannes Frederic Sowa 3328d35a00b8SFelix Jia switch (idev->cnf.addr_gen_mode) { 3329cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_RANDOM: 3330cc9da6ccSBjørn Mork ipv6_gen_mode_random_init(idev); 3331a8eceea8SJoe Perches fallthrough; 3332cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_STABLE_PRIVACY: 3333622c81d5SHannes Frederic Sowa if (!ipv6_generate_stable_address(&addr, 0, idev)) 333464236f3fSHannes Frederic Sowa addrconf_add_linklocal(idev, &addr, 333564236f3fSHannes Frederic Sowa IFA_F_STABLE_PRIVACY); 3336622c81d5SHannes Frederic Sowa else if (prefix_route) 33378308f3ffSDavid Ahern addrconf_prefix_route(&addr, 64, 0, idev->dev, 3338acb54e3cSDavid Ahern 0, 0, GFP_KERNEL); 3339cc9da6ccSBjørn Mork break; 3340cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_EUI64: 3341bc91b0f0SJiri Pirko /* addrconf_add_linklocal also adds a prefix_route and we 3342bc91b0f0SJiri Pirko * only need to care about prefix routes if ipv6_generate_eui64 3343bc91b0f0SJiri Pirko * couldn't generate one. 3344bc91b0f0SJiri Pirko */ 3345bc91b0f0SJiri Pirko if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0) 334664236f3fSHannes Frederic Sowa addrconf_add_linklocal(idev, &addr, 0); 3347bc91b0f0SJiri Pirko else if (prefix_route) 33488308f3ffSDavid Ahern addrconf_prefix_route(&addr, 64, 0, idev->dev, 334927b10608SDavid Ahern 0, 0, GFP_KERNEL); 3350cc9da6ccSBjørn Mork break; 3351cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_NONE: 3352cc9da6ccSBjørn Mork default: 3353cc9da6ccSBjørn Mork /* will not add any link local address */ 3354cc9da6ccSBjørn Mork break; 3355bc91b0f0SJiri Pirko } 3356bc91b0f0SJiri Pirko } 3357bc91b0f0SJiri Pirko 33581da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev) 33591da177e4SLinus Torvalds { 33601da177e4SLinus Torvalds struct inet6_dev *idev; 33611da177e4SLinus Torvalds 33621da177e4SLinus Torvalds ASSERT_RTNL(); 33631da177e4SLinus Torvalds 336474235a25SHerbert Xu if ((dev->type != ARPHRD_ETHER) && 336574235a25SHerbert Xu (dev->type != ARPHRD_FDDI) && 336674235a25SHerbert Xu (dev->type != ARPHRD_ARCNET) && 336706a4c1c5Salex.bluesman.smirnov@gmail.com (dev->type != ARPHRD_INFINIBAND) && 3368e837735eSNicolas Dichtel (dev->type != ARPHRD_IEEE1394) && 3369e74bccb8SJukka Rissanen (dev->type != ARPHRD_TUNNEL6) && 3370cc9da6ccSBjørn Mork (dev->type != ARPHRD_6LOWPAN) && 337145ce0fd1SFelix Jia (dev->type != ARPHRD_TUNNEL) && 33729deb441cSSubash Abhinov Kasiviswanathan (dev->type != ARPHRD_NONE) && 33739deb441cSSubash Abhinov Kasiviswanathan (dev->type != ARPHRD_RAWIP)) { 337474235a25SHerbert Xu /* Alas, we support only Ethernet autoconfiguration. */ 337560380488SHangbin Liu idev = __in6_dev_get(dev); 337660380488SHangbin Liu if (!IS_ERR_OR_NULL(idev) && dev->flags & IFF_UP && 337760380488SHangbin Liu dev->flags & IFF_MULTICAST) 337860380488SHangbin Liu ipv6_mc_up(idev); 337974235a25SHerbert Xu return; 338074235a25SHerbert Xu } 338174235a25SHerbert Xu 33821da177e4SLinus Torvalds idev = addrconf_add_dev(dev); 338364e724f6SBrian Haley if (IS_ERR(idev)) 33841da177e4SLinus Torvalds return; 33851da177e4SLinus Torvalds 3386cc9da6ccSBjørn Mork /* this device type has no EUI support */ 3387cc9da6ccSBjørn Mork if (dev->type == ARPHRD_NONE && 3388d35a00b8SFelix Jia idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64) 3389d35a00b8SFelix Jia idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM; 3390cc9da6ccSBjørn Mork 3391bc91b0f0SJiri Pirko addrconf_addr_gen(idev, false); 33921da177e4SLinus Torvalds } 33931da177e4SLinus Torvalds 339407a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 33951da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev) 33961da177e4SLinus Torvalds { 33971da177e4SLinus Torvalds struct inet6_dev *idev; 33981da177e4SLinus Torvalds 33991da177e4SLinus Torvalds ASSERT_RTNL(); 34001da177e4SLinus Torvalds 34011da177e4SLinus Torvalds /* 34021da177e4SLinus Torvalds * Configure the tunnel with one of our IPv4 34031da177e4SLinus Torvalds * addresses... we should configure all of 34041da177e4SLinus Torvalds * our v4 addrs in the tunnel 34051da177e4SLinus Torvalds */ 34061da177e4SLinus Torvalds 3407e5d08d71SIan Morris idev = ipv6_find_idev(dev); 3408db0b99f5SSabrina Dubroca if (IS_ERR(idev)) { 340991df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 34101da177e4SLinus Torvalds return; 34111da177e4SLinus Torvalds } 34121da177e4SLinus Torvalds 3413c7dc89c0SFred L. Templin if (dev->priv_flags & IFF_ISATAP) { 3414bc91b0f0SJiri Pirko addrconf_addr_gen(idev, false); 3415c7dc89c0SFred L. Templin return; 3416c7dc89c0SFred L. Templin } 3417c7dc89c0SFred L. Templin 3418e5dd7294SAntonio Quartulli add_v4_addrs(idev); 34191da177e4SLinus Torvalds 342062b54dd9SNicolas Dichtel if (dev->flags&IFF_POINTOPOINT) 34211da177e4SLinus Torvalds addrconf_add_mroute(dev); 34221da177e4SLinus Torvalds } 34230be669bbSJoerg Roedel #endif 34241da177e4SLinus Torvalds 3425e5dd7294SAntonio Quartulli #if IS_ENABLED(CONFIG_NET_IPGRE) || IS_ENABLED(CONFIG_IPV6_GRE) 3426aee80b54Sstephen hemminger static void addrconf_gre_config(struct net_device *dev) 3427aee80b54Sstephen hemminger { 3428aee80b54Sstephen hemminger struct inet6_dev *idev; 3429aee80b54Sstephen hemminger 3430aee80b54Sstephen hemminger ASSERT_RTNL(); 3431aee80b54Sstephen hemminger 3432e5d08d71SIan Morris idev = ipv6_find_idev(dev); 3433db0b99f5SSabrina Dubroca if (IS_ERR(idev)) { 343491df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 3435aee80b54Sstephen hemminger return; 3436aee80b54Sstephen hemminger } 3437aee80b54Sstephen hemminger 3438e5dd7294SAntonio Quartulli if (dev->type == ARPHRD_ETHER) { 3439bc91b0f0SJiri Pirko addrconf_addr_gen(idev, true); 3440e5dd7294SAntonio Quartulli return; 3441e5dd7294SAntonio Quartulli } 3442e5dd7294SAntonio Quartulli 3443e5dd7294SAntonio Quartulli add_v4_addrs(idev); 3444e5dd7294SAntonio Quartulli 3445d9e4ce65SHannes Frederic Sowa if (dev->flags & IFF_POINTOPOINT) 3446d9e4ce65SHannes Frederic Sowa addrconf_add_mroute(dev); 3447aee80b54Sstephen hemminger } 3448aee80b54Sstephen hemminger #endif 3449aee80b54Sstephen hemminger 345023ca0c2cSThomas Winter static void addrconf_init_auto_addrs(struct net_device *dev) 345123ca0c2cSThomas Winter { 345223ca0c2cSThomas Winter switch (dev->type) { 345323ca0c2cSThomas Winter #if IS_ENABLED(CONFIG_IPV6_SIT) 345423ca0c2cSThomas Winter case ARPHRD_SIT: 345523ca0c2cSThomas Winter addrconf_sit_config(dev); 345623ca0c2cSThomas Winter break; 345723ca0c2cSThomas Winter #endif 345823ca0c2cSThomas Winter #if IS_ENABLED(CONFIG_NET_IPGRE) || IS_ENABLED(CONFIG_IPV6_GRE) 345923ca0c2cSThomas Winter case ARPHRD_IP6GRE: 346023ca0c2cSThomas Winter case ARPHRD_IPGRE: 346123ca0c2cSThomas Winter addrconf_gre_config(dev); 346223ca0c2cSThomas Winter break; 346323ca0c2cSThomas Winter #endif 346423ca0c2cSThomas Winter case ARPHRD_LOOPBACK: 346523ca0c2cSThomas Winter init_loopback(dev); 346623ca0c2cSThomas Winter break; 346723ca0c2cSThomas Winter 346823ca0c2cSThomas Winter default: 346923ca0c2cSThomas Winter addrconf_dev_config(dev); 347023ca0c2cSThomas Winter break; 347123ca0c2cSThomas Winter } 347223ca0c2cSThomas Winter } 347323ca0c2cSThomas Winter 3474afb1d4b5SDavid Ahern static int fixup_permanent_addr(struct net *net, 3475afb1d4b5SDavid Ahern struct inet6_dev *idev, 3476f1705ec1SDavid Ahern struct inet6_ifaddr *ifp) 3477f1705ec1SDavid Ahern { 347893c2fb25SDavid Ahern /* !fib6_node means the host route was removed from the 34798048ced9SDavid Ahern * FIB, for example, if 'lo' device is taken down. In that 34808048ced9SDavid Ahern * case regenerate the host route. 34818048ced9SDavid Ahern */ 348293c2fb25SDavid Ahern if (!ifp->rt || !ifp->rt->fib6_node) { 3483360a9887SDavid Ahern struct fib6_info *f6i, *prev; 3484f1705ec1SDavid Ahern 3485360a9887SDavid Ahern f6i = addrconf_f6i_alloc(net, idev, &ifp->addr, false, 3486acb54e3cSDavid Ahern GFP_ATOMIC); 3487360a9887SDavid Ahern if (IS_ERR(f6i)) 3488360a9887SDavid Ahern return PTR_ERR(f6i); 3489f1705ec1SDavid Ahern 34908048ced9SDavid Ahern /* ifp->rt can be accessed outside of rtnl */ 34918048ced9SDavid Ahern spin_lock(&ifp->lock); 34928048ced9SDavid Ahern prev = ifp->rt; 3493360a9887SDavid Ahern ifp->rt = f6i; 34948048ced9SDavid Ahern spin_unlock(&ifp->lock); 34958048ced9SDavid Ahern 349693531c67SDavid Ahern fib6_info_release(prev); 3497f1705ec1SDavid Ahern } 3498f1705ec1SDavid Ahern 3499f1705ec1SDavid Ahern if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) { 3500f1705ec1SDavid Ahern addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 35018308f3ffSDavid Ahern ifp->rt_priority, idev->dev, 0, 0, 35028308f3ffSDavid Ahern GFP_ATOMIC); 3503f1705ec1SDavid Ahern } 3504f1705ec1SDavid Ahern 35056d717134SDavid Ahern if (ifp->state == INET6_IFADDR_STATE_PREDAD) 3506f1705ec1SDavid Ahern addrconf_dad_start(ifp); 3507f1705ec1SDavid Ahern 3508f1705ec1SDavid Ahern return 0; 3509f1705ec1SDavid Ahern } 3510f1705ec1SDavid Ahern 3511afb1d4b5SDavid Ahern static void addrconf_permanent_addr(struct net *net, struct net_device *dev) 3512f1705ec1SDavid Ahern { 3513f1705ec1SDavid Ahern struct inet6_ifaddr *ifp, *tmp; 3514f1705ec1SDavid Ahern struct inet6_dev *idev; 3515f1705ec1SDavid Ahern 3516f1705ec1SDavid Ahern idev = __in6_dev_get(dev); 3517f1705ec1SDavid Ahern if (!idev) 3518f1705ec1SDavid Ahern return; 3519f1705ec1SDavid Ahern 3520f1705ec1SDavid Ahern write_lock_bh(&idev->lock); 3521f1705ec1SDavid Ahern 3522f1705ec1SDavid Ahern list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) { 3523f1705ec1SDavid Ahern if ((ifp->flags & IFA_F_PERMANENT) && 3524afb1d4b5SDavid Ahern fixup_permanent_addr(net, idev, ifp) < 0) { 3525f1705ec1SDavid Ahern write_unlock_bh(&idev->lock); 3526e669b869SEric Dumazet in6_ifa_hold(ifp); 3527f1705ec1SDavid Ahern ipv6_del_addr(ifp); 3528f1705ec1SDavid Ahern write_lock_bh(&idev->lock); 3529f1705ec1SDavid Ahern 3530f1705ec1SDavid Ahern net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n", 3531f1705ec1SDavid Ahern idev->dev->name, &ifp->addr); 3532f1705ec1SDavid Ahern } 3533f1705ec1SDavid Ahern } 3534f1705ec1SDavid Ahern 3535f1705ec1SDavid Ahern write_unlock_bh(&idev->lock); 3536f1705ec1SDavid Ahern } 3537f1705ec1SDavid Ahern 35381da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event, 3539351638e7SJiri Pirko void *ptr) 35401da177e4SLinus Torvalds { 3541351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 3542896585d4SHangbin Liu struct netdev_notifier_change_info *change_info; 35434f7f34eaSDavid Ahern struct netdev_notifier_changeupper_info *info; 3544748e2d93SEric Dumazet struct inet6_dev *idev = __in6_dev_get(dev); 354560abc0beSWANG Cong struct net *net = dev_net(dev); 3546c5e33bddSYOSHIFUJI Hideaki int run_pending = 0; 3547b217d616SHerbert Xu int err; 35481da177e4SLinus Torvalds 35491da177e4SLinus Torvalds switch (event) { 355045ba9dd2SYOSHIFUJI Hideaki case NETDEV_REGISTER: 355174235a25SHerbert Xu if (!idev && dev->mtu >= IPV6_MIN_MTU) { 355245ba9dd2SYOSHIFUJI Hideaki idev = ipv6_add_dev(dev); 3553a317a2f1SWANG Cong if (IS_ERR(idev)) 3554a317a2f1SWANG Cong return notifier_from_errno(PTR_ERR(idev)); 355545ba9dd2SYOSHIFUJI Hideaki } 355645ba9dd2SYOSHIFUJI Hideaki break; 3557bcdd553fSStephen Hemminger 3558b7b0b1d2SAlexander Duyck case NETDEV_CHANGEMTU: 3559b7b0b1d2SAlexander Duyck /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */ 3560b7b0b1d2SAlexander Duyck if (dev->mtu < IPV6_MIN_MTU) { 356160abc0beSWANG Cong addrconf_ifdown(dev, dev != net->loopback_dev); 3562b7b0b1d2SAlexander Duyck break; 3563b7b0b1d2SAlexander Duyck } 3564b7b0b1d2SAlexander Duyck 3565b7b0b1d2SAlexander Duyck if (idev) { 3566b7b0b1d2SAlexander Duyck rt6_mtu_change(dev, dev->mtu); 3567b7b0b1d2SAlexander Duyck idev->cnf.mtu6 = dev->mtu; 3568b7b0b1d2SAlexander Duyck break; 3569b7b0b1d2SAlexander Duyck } 3570b7b0b1d2SAlexander Duyck 3571b7b0b1d2SAlexander Duyck /* allocate new idev */ 3572b7b0b1d2SAlexander Duyck idev = ipv6_add_dev(dev); 3573b7b0b1d2SAlexander Duyck if (IS_ERR(idev)) 3574b7b0b1d2SAlexander Duyck break; 3575b7b0b1d2SAlexander Duyck 3576b7b0b1d2SAlexander Duyck /* device is still not ready */ 3577b7b0b1d2SAlexander Duyck if (!(idev->if_flags & IF_READY)) 3578b7b0b1d2SAlexander Duyck break; 3579b7b0b1d2SAlexander Duyck 3580b7b0b1d2SAlexander Duyck run_pending = 1; 3581a8eceea8SJoe Perches fallthrough; 35821da177e4SLinus Torvalds case NETDEV_UP: 35833c21edbdSYOSHIFUJI Hideaki case NETDEV_CHANGE: 35843ce62a84SWANG Cong if (idev && idev->cnf.disable_ipv6) 35853ce62a84SWANG Cong break; 35863ce62a84SWANG Cong 35878a321cf7SXin Long if (dev->priv_flags & IFF_NO_ADDRCONF) { 3588fd16eb94SHangbin Liu if (event == NETDEV_UP && !IS_ERR_OR_NULL(idev) && 3589fd16eb94SHangbin Liu dev->flags & IFF_UP && dev->flags & IFF_MULTICAST) 3590fd16eb94SHangbin Liu ipv6_mc_up(idev); 3591fd16eb94SHangbin Liu break; 3592fd16eb94SHangbin Liu } 3593fd16eb94SHangbin Liu 35943c21edbdSYOSHIFUJI Hideaki if (event == NETDEV_UP) { 359538bd10c4SDavid Ahern /* restore routes for permanent addresses */ 3596afb1d4b5SDavid Ahern addrconf_permanent_addr(net, dev); 359738bd10c4SDavid Ahern 35981f372c7bSMike Manning if (!addrconf_link_ready(dev)) { 35993c21edbdSYOSHIFUJI Hideaki /* device is not ready yet. */ 36007c62b8ddSLubomir Rintel pr_debug("ADDRCONF(NETDEV_UP): %s: link is not ready\n", 36013c21edbdSYOSHIFUJI Hideaki dev->name); 36023c21edbdSYOSHIFUJI Hideaki break; 36033c21edbdSYOSHIFUJI Hideaki } 360499081049SKristian Slavov 3605d31c7b8fSEvgeniy Polyakov if (!idev && dev->mtu >= IPV6_MIN_MTU) 3606d31c7b8fSEvgeniy Polyakov idev = ipv6_add_dev(dev); 3607d31c7b8fSEvgeniy Polyakov 3608a317a2f1SWANG Cong if (!IS_ERR_OR_NULL(idev)) { 360999081049SKristian Slavov idev->if_flags |= IF_READY; 3610e3ec6cfcSBenjamin Thery run_pending = 1; 3611e3ec6cfcSBenjamin Thery } 3612b7b0b1d2SAlexander Duyck } else if (event == NETDEV_CHANGE) { 36131f372c7bSMike Manning if (!addrconf_link_ready(dev)) { 36143c21edbdSYOSHIFUJI Hideaki /* device is still not ready. */ 361527c6fa73SIdo Schimmel rt6_sync_down_dev(dev, event); 36163c21edbdSYOSHIFUJI Hideaki break; 36173c21edbdSYOSHIFUJI Hideaki } 36183c21edbdSYOSHIFUJI Hideaki 3619896585d4SHangbin Liu if (!IS_ERR_OR_NULL(idev)) { 3620a088d1d7SLinus Lüssing if (idev->if_flags & IF_READY) { 3621a088d1d7SLinus Lüssing /* device is already configured - 3622a088d1d7SLinus Lüssing * but resend MLD reports, we might 3623a088d1d7SLinus Lüssing * have roamed and need to update 3624a088d1d7SLinus Lüssing * multicast snooping switches 3625a088d1d7SLinus Lüssing */ 3626a088d1d7SLinus Lüssing ipv6_mc_up(idev); 3627896585d4SHangbin Liu change_info = ptr; 3628896585d4SHangbin Liu if (change_info->flags_changed & IFF_NOARP) 3629896585d4SHangbin Liu addrconf_dad_run(idev, true); 363027c6fa73SIdo Schimmel rt6_sync_up(dev, RTNH_F_LINKDOWN); 36313c21edbdSYOSHIFUJI Hideaki break; 3632a088d1d7SLinus Lüssing } 36333c21edbdSYOSHIFUJI Hideaki idev->if_flags |= IF_READY; 36343c21edbdSYOSHIFUJI Hideaki } 36353c21edbdSYOSHIFUJI Hideaki 3636f69de8aaSMatthieu Baerts pr_debug("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n", 36373c21edbdSYOSHIFUJI Hideaki dev->name); 36383c21edbdSYOSHIFUJI Hideaki 3639c5e33bddSYOSHIFUJI Hideaki run_pending = 1; 36403c21edbdSYOSHIFUJI Hideaki } 36413c21edbdSYOSHIFUJI Hideaki 364223ca0c2cSThomas Winter addrconf_init_auto_addrs(dev); 3643bcdd553fSStephen Hemminger 3644a317a2f1SWANG Cong if (!IS_ERR_OR_NULL(idev)) { 3645c5e33bddSYOSHIFUJI Hideaki if (run_pending) 3646896585d4SHangbin Liu addrconf_dad_run(idev, false); 3647c5e33bddSYOSHIFUJI Hideaki 36482127d95aSIdo Schimmel /* Device has an address by now */ 36492127d95aSIdo Schimmel rt6_sync_up(dev, RTNH_F_DEAD); 36502127d95aSIdo Schimmel 3651bcdd553fSStephen Hemminger /* 3652bcdd553fSStephen Hemminger * If the MTU changed during the interface down, 3653bcdd553fSStephen Hemminger * when the interface up, the changed MTU must be 3654bcdd553fSStephen Hemminger * reflected in the idev as well as routers. 36551da177e4SLinus Torvalds */ 3656bcdd553fSStephen Hemminger if (idev->cnf.mtu6 != dev->mtu && 3657bcdd553fSStephen Hemminger dev->mtu >= IPV6_MIN_MTU) { 36581da177e4SLinus Torvalds rt6_mtu_change(dev, dev->mtu); 36591da177e4SLinus Torvalds idev->cnf.mtu6 = dev->mtu; 36601da177e4SLinus Torvalds } 36611da177e4SLinus Torvalds idev->tstamp = jiffies; 36621da177e4SLinus Torvalds inet6_ifinfo_notify(RTM_NEWLINK, idev); 3663bcdd553fSStephen Hemminger 3664bcdd553fSStephen Hemminger /* 3665bcdd553fSStephen Hemminger * If the changed mtu during down is lower than 3666bcdd553fSStephen Hemminger * IPV6_MIN_MTU stop IPv6 on this interface. 36671da177e4SLinus Torvalds */ 36681da177e4SLinus Torvalds if (dev->mtu < IPV6_MIN_MTU) 366960abc0beSWANG Cong addrconf_ifdown(dev, dev != net->loopback_dev); 36701da177e4SLinus Torvalds } 36711da177e4SLinus Torvalds break; 36721da177e4SLinus Torvalds 36731da177e4SLinus Torvalds case NETDEV_DOWN: 36741da177e4SLinus Torvalds case NETDEV_UNREGISTER: 36751da177e4SLinus Torvalds /* 36761da177e4SLinus Torvalds * Remove all addresses from this interface. 36771da177e4SLinus Torvalds */ 36781da177e4SLinus Torvalds addrconf_ifdown(dev, event != NETDEV_DOWN); 36791da177e4SLinus Torvalds break; 36803c21edbdSYOSHIFUJI Hideaki 36811da177e4SLinus Torvalds case NETDEV_CHANGENAME: 36821da177e4SLinus Torvalds if (idev) { 36835632c515SStephen Hemminger snmp6_unregister_dev(idev); 3684408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 3685a317a2f1SWANG Cong err = addrconf_sysctl_register(idev); 3686b217d616SHerbert Xu if (err) 3687b217d616SHerbert Xu return notifier_from_errno(err); 3688a317a2f1SWANG Cong err = snmp6_register_dev(idev); 3689a317a2f1SWANG Cong if (err) { 3690a317a2f1SWANG Cong addrconf_sysctl_unregister(idev); 3691a317a2f1SWANG Cong return notifier_from_errno(err); 3692a317a2f1SWANG Cong } 36935632c515SStephen Hemminger } 36941da177e4SLinus Torvalds break; 3695bcdd553fSStephen Hemminger 369693d9b7d7SJiri Pirko case NETDEV_PRE_TYPE_CHANGE: 369793d9b7d7SJiri Pirko case NETDEV_POST_TYPE_CHANGE: 36983ef0952cSAndrew Lunn if (idev) 369993d9b7d7SJiri Pirko addrconf_type_change(dev, event); 370075c78500SMoni Shoua break; 37014f7f34eaSDavid Ahern 37024f7f34eaSDavid Ahern case NETDEV_CHANGEUPPER: 37034f7f34eaSDavid Ahern info = ptr; 37044f7f34eaSDavid Ahern 37054f7f34eaSDavid Ahern /* flush all routes if dev is linked to or unlinked from 37064f7f34eaSDavid Ahern * an L3 master device (e.g., VRF) 37074f7f34eaSDavid Ahern */ 37084f7f34eaSDavid Ahern if (info->upper_dev && netif_is_l3_master(info->upper_dev)) 3709ae79dbf6SFlorent Fourcot addrconf_ifdown(dev, false); 37103ff50b79SStephen Hemminger } 37111da177e4SLinus Torvalds 37121da177e4SLinus Torvalds return NOTIFY_OK; 37131da177e4SLinus Torvalds } 37141da177e4SLinus Torvalds 37151da177e4SLinus Torvalds /* 37161da177e4SLinus Torvalds * addrconf module should be notified of a device going up 37171da177e4SLinus Torvalds */ 37181da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = { 37191da177e4SLinus Torvalds .notifier_call = addrconf_notify, 3720242d3a49SWANG Cong .priority = ADDRCONF_NOTIFY_PRIORITY, 37211da177e4SLinus Torvalds }; 37221da177e4SLinus Torvalds 372393d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, unsigned long event) 372475c78500SMoni Shoua { 372575c78500SMoni Shoua struct inet6_dev *idev; 372675c78500SMoni Shoua ASSERT_RTNL(); 372775c78500SMoni Shoua 372875c78500SMoni Shoua idev = __in6_dev_get(dev); 372975c78500SMoni Shoua 373093d9b7d7SJiri Pirko if (event == NETDEV_POST_TYPE_CHANGE) 373175c78500SMoni Shoua ipv6_mc_remap(idev); 373293d9b7d7SJiri Pirko else if (event == NETDEV_PRE_TYPE_CHANGE) 373375c78500SMoni Shoua ipv6_mc_unmap(idev); 373475c78500SMoni Shoua } 373575c78500SMoni Shoua 373670af921dSDavid Ahern static bool addr_is_local(const struct in6_addr *addr) 373770af921dSDavid Ahern { 373870af921dSDavid Ahern return ipv6_addr_type(addr) & 373970af921dSDavid Ahern (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK); 374070af921dSDavid Ahern } 374170af921dSDavid Ahern 3742ae79dbf6SFlorent Fourcot static int addrconf_ifdown(struct net_device *dev, bool unregister) 37431da177e4SLinus Torvalds { 3744ae79dbf6SFlorent Fourcot unsigned long event = unregister ? NETDEV_UNREGISTER : NETDEV_DOWN; 3745c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 3746502a2ffdSstephen hemminger struct inet6_dev *idev; 374751454ea4SNiels Dossche struct inet6_ifaddr *ifa; 374851454ea4SNiels Dossche LIST_HEAD(tmp_addr_list); 37490aef78aaSIvan Vecera bool keep_addr = false; 37509995b408Sj.nixdorf@avm.de bool was_ready; 375173a8bd74SDavid S. Miller int state, i; 37521da177e4SLinus Torvalds 37531da177e4SLinus Torvalds ASSERT_RTNL(); 37541da177e4SLinus Torvalds 37554c981e28SIdo Schimmel rt6_disable_ip(dev, event); 37561da177e4SLinus Torvalds 37571da177e4SLinus Torvalds idev = __in6_dev_get(dev); 375863159f29SIan Morris if (!idev) 37591da177e4SLinus Torvalds return -ENODEV; 37601da177e4SLinus Torvalds 3761bcdd553fSStephen Hemminger /* 3762bcdd553fSStephen Hemminger * Step 1: remove reference to ipv6 device from parent device. 3763bcdd553fSStephen Hemminger * Do not dev_put! 37641da177e4SLinus Torvalds */ 3765ae79dbf6SFlorent Fourcot if (unregister) { 37661da177e4SLinus Torvalds idev->dead = 1; 37678814c4b5SYOSHIFUJI Hideaki 37688814c4b5SYOSHIFUJI Hideaki /* protected by rtnl_lock */ 3769a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(dev->ip6_ptr, NULL); 37701da177e4SLinus Torvalds 37711da177e4SLinus Torvalds /* Step 1.5: remove snmp6 entry */ 37721da177e4SLinus Torvalds snmp6_unregister_dev(idev); 37731da177e4SLinus Torvalds 37741da177e4SLinus Torvalds } 37751da177e4SLinus Torvalds 3776f1705ec1SDavid Ahern /* combine the user config with event to determine if permanent 3777f1705ec1SDavid Ahern * addresses are to be removed from address hash table 3778f1705ec1SDavid Ahern */ 3779ae79dbf6SFlorent Fourcot if (!unregister && !idev->cnf.disable_ipv6) { 37800aef78aaSIvan Vecera /* aggregate the system setting and interface setting */ 37810aef78aaSIvan Vecera int _keep_addr = net->ipv6.devconf_all->keep_addr_on_down; 37820aef78aaSIvan Vecera 37830aef78aaSIvan Vecera if (!_keep_addr) 37840aef78aaSIvan Vecera _keep_addr = idev->cnf.keep_addr_on_down; 37850aef78aaSIvan Vecera 37860aef78aaSIvan Vecera keep_addr = (_keep_addr > 0); 37870aef78aaSIvan Vecera } 3788f1705ec1SDavid Ahern 378973a8bd74SDavid S. Miller /* Step 2: clear hash table */ 379073a8bd74SDavid S. Miller for (i = 0; i < IN6_ADDR_HSIZE; i++) { 3791e66d1172SEric Dumazet struct hlist_head *h = &net->ipv6.inet6_addr_lst[i]; 379273a8bd74SDavid S. Miller 3793e66d1172SEric Dumazet spin_lock_bh(&net->ipv6.addrconf_hash_lock); 379473a8bd74SDavid S. Miller restart: 3795b67bfe0dSSasha Levin hlist_for_each_entry_rcu(ifa, h, addr_lst) { 379673a8bd74SDavid S. Miller if (ifa->idev == idev) { 3797c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifa); 3798f1705ec1SDavid Ahern /* combined flag + permanent flag decide if 3799f1705ec1SDavid Ahern * address is retained on a down event 3800f1705ec1SDavid Ahern */ 3801f1705ec1SDavid Ahern if (!keep_addr || 380270af921dSDavid Ahern !(ifa->flags & IFA_F_PERMANENT) || 380370af921dSDavid Ahern addr_is_local(&ifa->addr)) { 3804f1705ec1SDavid Ahern hlist_del_init_rcu(&ifa->addr_lst); 380573a8bd74SDavid S. Miller goto restart; 380673a8bd74SDavid S. Miller } 380773a8bd74SDavid S. Miller } 3808f1705ec1SDavid Ahern } 3809e66d1172SEric Dumazet spin_unlock_bh(&net->ipv6.addrconf_hash_lock); 381073a8bd74SDavid S. Miller } 381173a8bd74SDavid S. Miller 38121da177e4SLinus Torvalds write_lock_bh(&idev->lock); 38131da177e4SLinus Torvalds 3814b7b1bfceSHannes Frederic Sowa addrconf_del_rs_timer(idev); 3815b7b1bfceSHannes Frederic Sowa 38169995b408Sj.nixdorf@avm.de /* Step 2: clear flags for stateless addrconf, repeated down 38179995b408Sj.nixdorf@avm.de * detection 38189995b408Sj.nixdorf@avm.de */ 38199995b408Sj.nixdorf@avm.de was_ready = idev->if_flags & IF_READY; 3820ae79dbf6SFlorent Fourcot if (!unregister) 38213c21edbdSYOSHIFUJI Hideaki idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY); 38221da177e4SLinus Torvalds 3823bcdd553fSStephen Hemminger /* Step 3: clear tempaddr list */ 3824372e6c8fSstephen hemminger while (!list_empty(&idev->tempaddr_list)) { 3825372e6c8fSstephen hemminger ifa = list_first_entry(&idev->tempaddr_list, 3826372e6c8fSstephen hemminger struct inet6_ifaddr, tmp_list); 3827372e6c8fSstephen hemminger list_del(&ifa->tmp_list); 38281da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 38291da177e4SLinus Torvalds spin_lock_bh(&ifa->lock); 38301da177e4SLinus Torvalds 38311da177e4SLinus Torvalds if (ifa->ifpub) { 38321da177e4SLinus Torvalds in6_ifa_put(ifa->ifpub); 38331da177e4SLinus Torvalds ifa->ifpub = NULL; 38341da177e4SLinus Torvalds } 38351da177e4SLinus Torvalds spin_unlock_bh(&ifa->lock); 38361da177e4SLinus Torvalds in6_ifa_put(ifa); 38371da177e4SLinus Torvalds write_lock_bh(&idev->lock); 38381da177e4SLinus Torvalds } 38398f37ada5Sstephen hemminger 384051454ea4SNiels Dossche list_for_each_entry(ifa, &idev->addr_list, if_list) 384151454ea4SNiels Dossche list_add_tail(&ifa->if_list_aux, &tmp_addr_list); 384251454ea4SNiels Dossche write_unlock_bh(&idev->lock); 384351454ea4SNiels Dossche 384451454ea4SNiels Dossche while (!list_empty(&tmp_addr_list)) { 38458d1c802bSDavid Ahern struct fib6_info *rt = NULL; 3846a2d6cbb0SRabin Vincent bool keep; 384738bd10c4SDavid Ahern 384851454ea4SNiels Dossche ifa = list_first_entry(&tmp_addr_list, 384951454ea4SNiels Dossche struct inet6_ifaddr, if_list_aux); 385051454ea4SNiels Dossche list_del(&ifa->if_list_aux); 385151454ea4SNiels Dossche 3852c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifa); 385384e8b803Sstephen hemminger 3854a2d6cbb0SRabin Vincent keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) && 3855a2d6cbb0SRabin Vincent !addr_is_local(&ifa->addr); 3856a2d6cbb0SRabin Vincent 38578e8e676dSHannes Frederic Sowa spin_lock_bh(&ifa->lock); 3858f1705ec1SDavid Ahern 3859a2d6cbb0SRabin Vincent if (keep) { 3860f1705ec1SDavid Ahern /* set state to skip the notifier below */ 3861f1705ec1SDavid Ahern state = INET6_IFADDR_STATE_DEAD; 38626d717134SDavid Ahern ifa->state = INET6_IFADDR_STATE_PREDAD; 3863f1705ec1SDavid Ahern if (!(ifa->flags & IFA_F_NODAD)) 3864f1705ec1SDavid Ahern ifa->flags |= IFA_F_TENTATIVE; 386538bd10c4SDavid Ahern 386638bd10c4SDavid Ahern rt = ifa->rt; 386738bd10c4SDavid Ahern ifa->rt = NULL; 3868f1705ec1SDavid Ahern } else { 38694c5ff6a6SHerbert Xu state = ifa->state; 38704c5ff6a6SHerbert Xu ifa->state = INET6_IFADDR_STATE_DEAD; 3871f1705ec1SDavid Ahern } 3872f1705ec1SDavid Ahern 38738e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifa->lock); 38744c5ff6a6SHerbert Xu 387538bd10c4SDavid Ahern if (rt) 387611dd74b3SRoopa Prabhu ip6_del_rt(net, rt, false); 387738bd10c4SDavid Ahern 38789d82ca98SJohn Fastabend if (state != INET6_IFADDR_STATE_DEAD) { 38791da177e4SLinus Torvalds __ipv6_ifa_notify(RTM_DELADDR, ifa); 3880f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_DOWN, ifa); 3881ea06f717SMike Manning } else { 3882ea06f717SMike Manning if (idev->cnf.forwarding) 3883ea06f717SMike Manning addrconf_leave_anycast(ifa); 3884ea06f717SMike Manning addrconf_leave_solict(ifa->idev, &ifa->addr); 38852de79570SLorenzo Colitti } 388673a8bd74SDavid S. Miller 38878ef802aaSEric Dumazet if (!keep) { 388851454ea4SNiels Dossche write_lock_bh(&idev->lock); 38898ef802aaSEric Dumazet list_del_rcu(&ifa->if_list); 389051454ea4SNiels Dossche write_unlock_bh(&idev->lock); 38918ef802aaSEric Dumazet in6_ifa_put(ifa); 38928ef802aaSEric Dumazet } 38931da177e4SLinus Torvalds } 38948f37ada5Sstephen hemminger 3895381f4dcaSSabrina Dubroca /* Step 5: Discard anycast and multicast list */ 3896ae79dbf6SFlorent Fourcot if (unregister) { 3897381f4dcaSSabrina Dubroca ipv6_ac_destroy_dev(idev); 38981da177e4SLinus Torvalds ipv6_mc_destroy_dev(idev); 38999995b408Sj.nixdorf@avm.de } else if (was_ready) { 39001da177e4SLinus Torvalds ipv6_mc_down(idev); 3901381f4dcaSSabrina Dubroca } 39021da177e4SLinus Torvalds 39031da177e4SLinus Torvalds idev->tstamp = jiffies; 390449b99da2SRocco Yue idev->ra_mtu = 0; 39051da177e4SLinus Torvalds 3906bcdd553fSStephen Hemminger /* Last: Shot the device (if unregistered) */ 3907ae79dbf6SFlorent Fourcot if (unregister) { 3908408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 39091da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, idev->nd_parms); 39101da177e4SLinus Torvalds neigh_ifdown(&nd_tbl, dev); 39111da177e4SLinus Torvalds in6_dev_put(idev); 39121da177e4SLinus Torvalds } 39131da177e4SLinus Torvalds return 0; 39141da177e4SLinus Torvalds } 39151da177e4SLinus Torvalds 3916e99e88a9SKees Cook static void addrconf_rs_timer(struct timer_list *t) 39171da177e4SLinus Torvalds { 3918e99e88a9SKees Cook struct inet6_dev *idev = from_timer(idev, t, rs_timer); 3919caf92bc4SCong Wang struct net_device *dev = idev->dev; 3920b7b1bfceSHannes Frederic Sowa struct in6_addr lladdr; 39211da177e4SLinus Torvalds 3922b7b1bfceSHannes Frederic Sowa write_lock(&idev->lock); 39235b2a1953Sstephen hemminger if (idev->dead || !(idev->if_flags & IF_READY)) 39241da177e4SLinus Torvalds goto out; 39251da177e4SLinus Torvalds 39269ba2add3SShmulik Ladkani if (!ipv6_accept_ra(idev)) 39271da177e4SLinus Torvalds goto out; 39285b2a1953Sstephen hemminger 39295b2a1953Sstephen hemminger /* Announcement received after solicitation was sent */ 39305b2a1953Sstephen hemminger if (idev->if_flags & IF_RA_RCVD) 39315b2a1953Sstephen hemminger goto out; 39321da177e4SLinus Torvalds 3933bd11f074SMaciej Żenczykowski if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) { 3934caf92bc4SCong Wang write_unlock(&idev->lock); 3935caf92bc4SCong Wang if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE)) 3936caf92bc4SCong Wang ndisc_send_rs(dev, &lladdr, 3937b7b1bfceSHannes Frederic Sowa &in6addr_linklocal_allrouters); 3938b7b1bfceSHannes Frederic Sowa else 3939caf92bc4SCong Wang goto put; 3940b7b1bfceSHannes Frederic Sowa 3941caf92bc4SCong Wang write_lock(&idev->lock); 3942bd11f074SMaciej Żenczykowski idev->rs_interval = rfc3315_s14_backoff_update( 3943bd11f074SMaciej Żenczykowski idev->rs_interval, idev->cnf.rtr_solicit_max_interval); 39441da177e4SLinus Torvalds /* The wait after the last probe can be shorter */ 3945b7b1bfceSHannes Frederic Sowa addrconf_mod_rs_timer(idev, (idev->rs_probes == 3946b7b1bfceSHannes Frederic Sowa idev->cnf.rtr_solicits) ? 39475b2a1953Sstephen hemminger idev->cnf.rtr_solicit_delay : 3948bd11f074SMaciej Żenczykowski idev->rs_interval); 39491da177e4SLinus Torvalds } else { 39501da177e4SLinus Torvalds /* 39511da177e4SLinus Torvalds * Note: we do not support deprecated "all on-link" 39521da177e4SLinus Torvalds * assumption any longer. 39531da177e4SLinus Torvalds */ 395491df42beSJoe Perches pr_debug("%s: no IPv6 routers present\n", idev->dev->name); 39551da177e4SLinus Torvalds } 39561da177e4SLinus Torvalds 39571da177e4SLinus Torvalds out: 3958b7b1bfceSHannes Frederic Sowa write_unlock(&idev->lock); 3959caf92bc4SCong Wang put: 3960b7b1bfceSHannes Frederic Sowa in6_dev_put(idev); 39611da177e4SLinus Torvalds } 39621da177e4SLinus Torvalds 39631da177e4SLinus Torvalds /* 39641da177e4SLinus Torvalds * Duplicate Address Detection 39651da177e4SLinus Torvalds */ 39663c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp) 39673c21edbdSYOSHIFUJI Hideaki { 39683c21edbdSYOSHIFUJI Hideaki unsigned long rand_num; 39693c21edbdSYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev; 3970adc176c5SErik Nordmark u64 nonce; 39713c21edbdSYOSHIFUJI Hideaki 397295c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 397395c385b4SNeil Horman rand_num = 0; 397495c385b4SNeil Horman else 39758032bf12SJason A. Donenfeld rand_num = get_random_u32_below(idev->cnf.rtr_solicit_delay ? : 1); 397695c385b4SNeil Horman 3977adc176c5SErik Nordmark nonce = 0; 3978adc176c5SErik Nordmark if (idev->cnf.enhanced_dad || 3979adc176c5SErik Nordmark dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad) { 3980adc176c5SErik Nordmark do 3981adc176c5SErik Nordmark get_random_bytes(&nonce, 6); 3982adc176c5SErik Nordmark while (nonce == 0); 3983adc176c5SErik Nordmark } 3984adc176c5SErik Nordmark ifp->dad_nonce = nonce; 3985b7b1bfceSHannes Frederic Sowa ifp->dad_probes = idev->cnf.dad_transmits; 3986c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, rand_num); 39873c21edbdSYOSHIFUJI Hideaki } 39883c21edbdSYOSHIFUJI Hideaki 3989c15b1ccaSHannes Frederic Sowa static void addrconf_dad_begin(struct inet6_ifaddr *ifp) 39901da177e4SLinus Torvalds { 39911da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 39921da177e4SLinus Torvalds struct net_device *dev = idev->dev; 3993764d3be6SPaolo Abeni bool bump_id, notify = false; 3994afb1d4b5SDavid Ahern struct net *net; 39951da177e4SLinus Torvalds 39961da177e4SLinus Torvalds addrconf_join_solict(dev, &ifp->addr); 39971da177e4SLinus Torvalds 39981da177e4SLinus Torvalds read_lock_bh(&idev->lock); 3999622ccdf1SHerbert Xu spin_lock(&ifp->lock); 4000e9d3e084SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DEAD) 40011da177e4SLinus Torvalds goto out; 40021da177e4SLinus Torvalds 4003afb1d4b5SDavid Ahern net = dev_net(dev); 40041da177e4SLinus Torvalds if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) || 4005afb1d4b5SDavid Ahern (net->ipv6.devconf_all->accept_dad < 1 && 4006a2d3f3e3SMatteo Croce idev->cnf.accept_dad < 1) || 400755ebaef1SNoriaki TAKAMIYA !(ifp->flags&IFA_F_TENTATIVE) || 400855ebaef1SNoriaki TAKAMIYA ifp->flags & IFA_F_NODAD) { 4009c76fe2d9SDavid Ahern bool send_na = false; 4010c76fe2d9SDavid Ahern 4011c76fe2d9SDavid Ahern if (ifp->flags & IFA_F_TENTATIVE && 4012c76fe2d9SDavid Ahern !(ifp->flags & IFA_F_OPTIMISTIC)) 4013c76fe2d9SDavid Ahern send_na = true; 4014764d3be6SPaolo Abeni bump_id = ifp->flags & IFA_F_TENTATIVE; 4015cc411d0bSBrian Haley ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 401621809fafSstephen hemminger spin_unlock(&ifp->lock); 40171da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 40181da177e4SLinus Torvalds 4019c76fe2d9SDavid Ahern addrconf_dad_completed(ifp, bump_id, send_na); 40201da177e4SLinus Torvalds return; 40211da177e4SLinus Torvalds } 40221da177e4SLinus Torvalds 40236732badeSYOSHIFUJI Hideaki if (!(idev->if_flags & IF_READY)) { 402421809fafSstephen hemminger spin_unlock(&ifp->lock); 40256732badeSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 40263c21edbdSYOSHIFUJI Hideaki /* 4027590a9887SMasatake YAMATO * If the device is not ready: 40283c21edbdSYOSHIFUJI Hideaki * - keep it tentative if it is a permanent address. 40293c21edbdSYOSHIFUJI Hideaki * - otherwise, kill it. 40303c21edbdSYOSHIFUJI Hideaki */ 40313c21edbdSYOSHIFUJI Hideaki in6_ifa_hold(ifp); 4032cc411d0bSBrian Haley addrconf_dad_stop(ifp, 0); 40336732badeSYOSHIFUJI Hideaki return; 40343c21edbdSYOSHIFUJI Hideaki } 403595c385b4SNeil Horman 403695c385b4SNeil Horman /* 403795c385b4SNeil Horman * Optimistic nodes can start receiving 403895c385b4SNeil Horman * Frames right away 403995c385b4SNeil Horman */ 40407fd2561eSErik Kline if (ifp->flags & IFA_F_OPTIMISTIC) { 4041afb1d4b5SDavid Ahern ip6_ins_rt(net, ifp->rt); 4042afb1d4b5SDavid Ahern if (ipv6_use_optimistic_addr(net, idev)) { 40437fd2561eSErik Kline /* Because optimistic nodes can use this address, 40447fd2561eSErik Kline * notify listeners. If DAD fails, RTM_DELADDR is sent. 40457fd2561eSErik Kline */ 404616186a82Ssubashab@codeaurora.org notify = true; 40477fd2561eSErik Kline } 40487fd2561eSErik Kline } 404995c385b4SNeil Horman 40506732badeSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 40511da177e4SLinus Torvalds out: 4052622ccdf1SHerbert Xu spin_unlock(&ifp->lock); 40531da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 405416186a82Ssubashab@codeaurora.org if (notify) 405516186a82Ssubashab@codeaurora.org ipv6_ifa_notify(RTM_NEWADDR, ifp); 40561da177e4SLinus Torvalds } 40571da177e4SLinus Torvalds 4058c15b1ccaSHannes Frederic Sowa static void addrconf_dad_start(struct inet6_ifaddr *ifp) 40591da177e4SLinus Torvalds { 4060c15b1ccaSHannes Frederic Sowa bool begin_dad = false; 4061c15b1ccaSHannes Frederic Sowa 40628e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 4063c15b1ccaSHannes Frederic Sowa if (ifp->state != INET6_IFADDR_STATE_DEAD) { 4064c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_PREDAD; 4065c15b1ccaSHannes Frederic Sowa begin_dad = true; 4066c15b1ccaSHannes Frederic Sowa } 40678e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 4068c15b1ccaSHannes Frederic Sowa 4069c15b1ccaSHannes Frederic Sowa if (begin_dad) 4070c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 0); 4071c15b1ccaSHannes Frederic Sowa } 4072c15b1ccaSHannes Frederic Sowa 4073c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w) 4074c15b1ccaSHannes Frederic Sowa { 4075c15b1ccaSHannes Frederic Sowa struct inet6_ifaddr *ifp = container_of(to_delayed_work(w), 4076c15b1ccaSHannes Frederic Sowa struct inet6_ifaddr, 4077c15b1ccaSHannes Frederic Sowa dad_work); 40781da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 4079764d3be6SPaolo Abeni bool bump_id, disable_ipv6 = false; 40801da177e4SLinus Torvalds struct in6_addr mcaddr; 40811da177e4SLinus Torvalds 4082c15b1ccaSHannes Frederic Sowa enum { 4083c15b1ccaSHannes Frederic Sowa DAD_PROCESS, 4084c15b1ccaSHannes Frederic Sowa DAD_BEGIN, 4085c15b1ccaSHannes Frederic Sowa DAD_ABORT, 4086c15b1ccaSHannes Frederic Sowa } action = DAD_PROCESS; 4087c15b1ccaSHannes Frederic Sowa 4088c15b1ccaSHannes Frederic Sowa rtnl_lock(); 4089c15b1ccaSHannes Frederic Sowa 40908e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock); 4091c15b1ccaSHannes Frederic Sowa if (ifp->state == INET6_IFADDR_STATE_PREDAD) { 4092c15b1ccaSHannes Frederic Sowa action = DAD_BEGIN; 4093c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_DAD; 4094c15b1ccaSHannes Frederic Sowa } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) { 4095c15b1ccaSHannes Frederic Sowa action = DAD_ABORT; 4096c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_POSTDAD; 409785b51b12SMike Manning 409835e015e1SMatteo Croce if ((dev_net(idev->dev)->ipv6.devconf_all->accept_dad > 1 || 409935e015e1SMatteo Croce idev->cnf.accept_dad > 1) && 410035e015e1SMatteo Croce !idev->cnf.disable_ipv6 && 410185b51b12SMike Manning !(ifp->flags & IFA_F_STABLE_PRIVACY)) { 410285b51b12SMike Manning struct in6_addr addr; 410385b51b12SMike Manning 410485b51b12SMike Manning addr.s6_addr32[0] = htonl(0xfe800000); 410585b51b12SMike Manning addr.s6_addr32[1] = 0; 410685b51b12SMike Manning 410785b51b12SMike Manning if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) && 410885b51b12SMike Manning ipv6_addr_equal(&ifp->addr, &addr)) { 410985b51b12SMike Manning /* DAD failed for link-local based on MAC */ 411085b51b12SMike Manning idev->cnf.disable_ipv6 = 1; 411185b51b12SMike Manning 411285b51b12SMike Manning pr_info("%s: IPv6 being disabled!\n", 411385b51b12SMike Manning ifp->idev->dev->name); 411485b51b12SMike Manning disable_ipv6 = true; 411585b51b12SMike Manning } 411685b51b12SMike Manning } 4117c15b1ccaSHannes Frederic Sowa } 41188e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock); 4119c15b1ccaSHannes Frederic Sowa 4120c15b1ccaSHannes Frederic Sowa if (action == DAD_BEGIN) { 4121c15b1ccaSHannes Frederic Sowa addrconf_dad_begin(ifp); 4122c15b1ccaSHannes Frederic Sowa goto out; 4123c15b1ccaSHannes Frederic Sowa } else if (action == DAD_ABORT) { 4124751eb6b6SWei Yongjun in6_ifa_hold(ifp); 4125c15b1ccaSHannes Frederic Sowa addrconf_dad_stop(ifp, 1); 412685b51b12SMike Manning if (disable_ipv6) 4127ae79dbf6SFlorent Fourcot addrconf_ifdown(idev->dev, false); 4128c15b1ccaSHannes Frederic Sowa goto out; 4129c15b1ccaSHannes Frederic Sowa } 4130c15b1ccaSHannes Frederic Sowa 4131b7b1bfceSHannes Frederic Sowa if (!ifp->dad_probes && addrconf_dad_end(ifp)) 4132f2344a13SHerbert Xu goto out; 4133f2344a13SHerbert Xu 4134c15b1ccaSHannes Frederic Sowa write_lock_bh(&idev->lock); 4135122e4519Sstephen hemminger if (idev->dead || !(idev->if_flags & IF_READY)) { 4136c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 41371da177e4SLinus Torvalds goto out; 41381da177e4SLinus Torvalds } 413921809fafSstephen hemminger 414021809fafSstephen hemminger spin_lock(&ifp->lock); 4141622ccdf1SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DEAD) { 4142622ccdf1SHerbert Xu spin_unlock(&ifp->lock); 4143c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 4144622ccdf1SHerbert Xu goto out; 4145622ccdf1SHerbert Xu } 4146622ccdf1SHerbert Xu 4147b7b1bfceSHannes Frederic Sowa if (ifp->dad_probes == 0) { 4148c76fe2d9SDavid Ahern bool send_na = false; 4149c76fe2d9SDavid Ahern 41501da177e4SLinus Torvalds /* 41511da177e4SLinus Torvalds * DAD was successful 41521da177e4SLinus Torvalds */ 41531da177e4SLinus Torvalds 4154c76fe2d9SDavid Ahern if (ifp->flags & IFA_F_TENTATIVE && 4155c76fe2d9SDavid Ahern !(ifp->flags & IFA_F_OPTIMISTIC)) 4156c76fe2d9SDavid Ahern send_na = true; 4157764d3be6SPaolo Abeni bump_id = ifp->flags & IFA_F_TENTATIVE; 4158cc411d0bSBrian Haley ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 415921809fafSstephen hemminger spin_unlock(&ifp->lock); 4160c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 41611da177e4SLinus Torvalds 4162c76fe2d9SDavid Ahern addrconf_dad_completed(ifp, bump_id, send_na); 41631da177e4SLinus Torvalds 41641da177e4SLinus Torvalds goto out; 41651da177e4SLinus Torvalds } 41661da177e4SLinus Torvalds 4167b7b1bfceSHannes Frederic Sowa ifp->dad_probes--; 4168c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 416919e16d22SHangbin Liu max(NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME), 417019e16d22SHangbin Liu HZ/100)); 417121809fafSstephen hemminger spin_unlock(&ifp->lock); 4172c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock); 41731da177e4SLinus Torvalds 41741da177e4SLinus Torvalds /* send a neighbour solicitation for our addr */ 41751da177e4SLinus Torvalds addrconf_addr_solict_mult(&ifp->addr, &mcaddr); 4176adc176c5SErik Nordmark ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any, 4177adc176c5SErik Nordmark ifp->dad_nonce); 41781da177e4SLinus Torvalds out: 41791da177e4SLinus Torvalds in6_ifa_put(ifp); 4180c15b1ccaSHannes Frederic Sowa rtnl_unlock(); 41811da177e4SLinus Torvalds } 41821da177e4SLinus Torvalds 418311ffff75SHannes Frederic Sowa /* ifp->idev must be at least read locked */ 418411ffff75SHannes Frederic Sowa static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp) 418511ffff75SHannes Frederic Sowa { 418611ffff75SHannes Frederic Sowa struct inet6_ifaddr *ifpiter; 418711ffff75SHannes Frederic Sowa struct inet6_dev *idev = ifp->idev; 418811ffff75SHannes Frederic Sowa 4189602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) { 4190602582caSHannes Frederic Sowa if (ifpiter->scope > IFA_LINK) 4191602582caSHannes Frederic Sowa break; 419211ffff75SHannes Frederic Sowa if (ifp != ifpiter && ifpiter->scope == IFA_LINK && 419311ffff75SHannes Frederic Sowa (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE| 419411ffff75SHannes Frederic Sowa IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) == 419511ffff75SHannes Frederic Sowa IFA_F_PERMANENT) 419611ffff75SHannes Frederic Sowa return false; 419711ffff75SHannes Frederic Sowa } 419811ffff75SHannes Frederic Sowa return true; 419911ffff75SHannes Frederic Sowa } 420011ffff75SHannes Frederic Sowa 4201c76fe2d9SDavid Ahern static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id, 4202c76fe2d9SDavid Ahern bool send_na) 42031da177e4SLinus Torvalds { 42041da177e4SLinus Torvalds struct net_device *dev = ifp->idev->dev; 4205b7b1bfceSHannes Frederic Sowa struct in6_addr lladdr; 4206b173ee48SHannes Frederic Sowa bool send_rs, send_mld; 4207b7b1bfceSHannes Frederic Sowa 4208c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifp); 42091da177e4SLinus Torvalds 42101da177e4SLinus Torvalds /* 42111da177e4SLinus Torvalds * Configure the address for reception. Now it is valid. 42121da177e4SLinus Torvalds */ 42131da177e4SLinus Torvalds 42141da177e4SLinus Torvalds ipv6_ifa_notify(RTM_NEWADDR, ifp); 42151da177e4SLinus Torvalds 4216026359bcSTore Anderson /* If added prefix is link local and we are prepared to process 4217026359bcSTore Anderson router advertisements, start sending router solicitations. 42181da177e4SLinus Torvalds */ 42191da177e4SLinus Torvalds 42201ec047ebSHannes Frederic Sowa read_lock_bh(&ifp->idev->lock); 422111ffff75SHannes Frederic Sowa send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp); 4222b173ee48SHannes Frederic Sowa send_rs = send_mld && 4223b173ee48SHannes Frederic Sowa ipv6_accept_ra(ifp->idev) && 4224bd11f074SMaciej Żenczykowski ifp->idev->cnf.rtr_solicits != 0 && 4225b52e1cceSjianghaoran (dev->flags & IFF_LOOPBACK) == 0 && 42262df9bf4dSXin Long (dev->type != ARPHRD_TUNNEL) && 42272df9bf4dSXin Long !netif_is_team_port(dev); 42281ec047ebSHannes Frederic Sowa read_unlock_bh(&ifp->idev->lock); 42291ec047ebSHannes Frederic Sowa 4230b173ee48SHannes Frederic Sowa /* While dad is in progress mld report's source address is in6_addrany. 4231b173ee48SHannes Frederic Sowa * Resend with proper ll now. 4232b173ee48SHannes Frederic Sowa */ 4233b173ee48SHannes Frederic Sowa if (send_mld) 4234b173ee48SHannes Frederic Sowa ipv6_mc_dad_complete(ifp->idev); 4235b173ee48SHannes Frederic Sowa 4236c76fe2d9SDavid Ahern /* send unsolicited NA if enabled */ 4237c76fe2d9SDavid Ahern if (send_na && 4238c76fe2d9SDavid Ahern (ifp->idev->cnf.ndisc_notify || 4239c76fe2d9SDavid Ahern dev_net(dev)->ipv6.devconf_all->ndisc_notify)) { 4240c76fe2d9SDavid Ahern ndisc_send_na(dev, &in6addr_linklocal_allnodes, &ifp->addr, 4241c76fe2d9SDavid Ahern /*router=*/ !!ifp->idev->cnf.forwarding, 4242c76fe2d9SDavid Ahern /*solicited=*/ false, /*override=*/ true, 4243c76fe2d9SDavid Ahern /*inc_opt=*/ true); 4244c76fe2d9SDavid Ahern } 4245c76fe2d9SDavid Ahern 42461ec047ebSHannes Frederic Sowa if (send_rs) { 42471da177e4SLinus Torvalds /* 42481da177e4SLinus Torvalds * If a host as already performed a random delay 42491da177e4SLinus Torvalds * [...] as part of DAD [...] there is no need 42501da177e4SLinus Torvalds * to delay again before sending the first RS 42511da177e4SLinus Torvalds */ 42521ec047ebSHannes Frederic Sowa if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE)) 4253b7b1bfceSHannes Frederic Sowa return; 42541ec047ebSHannes Frederic Sowa ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters); 42551da177e4SLinus Torvalds 4256b7b1bfceSHannes Frederic Sowa write_lock_bh(&ifp->idev->lock); 4257b7b1bfceSHannes Frederic Sowa spin_lock(&ifp->lock); 4258bd11f074SMaciej Żenczykowski ifp->idev->rs_interval = rfc3315_s14_backoff_init( 4259bd11f074SMaciej Żenczykowski ifp->idev->cnf.rtr_solicit_interval); 4260b7b1bfceSHannes Frederic Sowa ifp->idev->rs_probes = 1; 42611da177e4SLinus Torvalds ifp->idev->if_flags |= IF_RS_SENT; 4262bd11f074SMaciej Żenczykowski addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval); 4263b7b1bfceSHannes Frederic Sowa spin_unlock(&ifp->lock); 4264b7b1bfceSHannes Frederic Sowa write_unlock_bh(&ifp->idev->lock); 42651da177e4SLinus Torvalds } 4266764d3be6SPaolo Abeni 4267764d3be6SPaolo Abeni if (bump_id) 4268764d3be6SPaolo Abeni rt_genid_bump_ipv6(dev_net(dev)); 4269a11a7f71SMarcus Huewe 4270a11a7f71SMarcus Huewe /* Make sure that a new temporary address will be created 4271a11a7f71SMarcus Huewe * before this temporary address becomes deprecated. 4272a11a7f71SMarcus Huewe */ 4273a11a7f71SMarcus Huewe if (ifp->flags & IFA_F_TEMPORARY) 42748805d13fSEric Dumazet addrconf_verify_rtnl(dev_net(dev)); 42751da177e4SLinus Torvalds } 42761da177e4SLinus Torvalds 4277896585d4SHangbin Liu static void addrconf_dad_run(struct inet6_dev *idev, bool restart) 4278e21e8467SStephen Hemminger { 4279c5e33bddSYOSHIFUJI Hideaki struct inet6_ifaddr *ifp; 4280c5e33bddSYOSHIFUJI Hideaki 4281c5e33bddSYOSHIFUJI Hideaki read_lock_bh(&idev->lock); 4282502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 428321809fafSstephen hemminger spin_lock(&ifp->lock); 4284896585d4SHangbin Liu if ((ifp->flags & IFA_F_TENTATIVE && 4285896585d4SHangbin Liu ifp->state == INET6_IFADDR_STATE_DAD) || restart) { 4286896585d4SHangbin Liu if (restart) 4287896585d4SHangbin Liu ifp->state = INET6_IFADDR_STATE_PREDAD; 4288c5e33bddSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 4289896585d4SHangbin Liu } 4290f2344a13SHerbert Xu spin_unlock(&ifp->lock); 4291c5e33bddSYOSHIFUJI Hideaki } 4292c5e33bddSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 4293c5e33bddSYOSHIFUJI Hideaki } 4294c5e33bddSYOSHIFUJI Hideaki 42951da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 42961da177e4SLinus Torvalds struct if6_iter_state { 42973c40090aSDaniel Lezcano struct seq_net_private p; 42981da177e4SLinus Torvalds int bucket; 42991d578303SMihai Maruseac int offset; 43001da177e4SLinus Torvalds }; 43011da177e4SLinus Torvalds 43021d578303SMihai Maruseac static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos) 43031da177e4SLinus Torvalds { 43041da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 43051218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 4306a5c1d98fSEric Dumazet struct inet6_ifaddr *ifa = NULL; 43071d578303SMihai Maruseac int p = 0; 43081da177e4SLinus Torvalds 43091d578303SMihai Maruseac /* initial bucket if pos is 0 */ 43101d578303SMihai Maruseac if (pos == 0) { 43111d578303SMihai Maruseac state->bucket = 0; 43121d578303SMihai Maruseac state->offset = 0; 43131d578303SMihai Maruseac } 43141d578303SMihai Maruseac 43151d578303SMihai Maruseac for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) { 4316e66d1172SEric Dumazet hlist_for_each_entry_rcu(ifa, &net->ipv6.inet6_addr_lst[state->bucket], 43171d578303SMihai Maruseac addr_lst) { 43181d578303SMihai Maruseac /* sync with offset */ 43191d578303SMihai Maruseac if (p < state->offset) { 43201d578303SMihai Maruseac p++; 43211d578303SMihai Maruseac continue; 43221d578303SMihai Maruseac } 43231da177e4SLinus Torvalds return ifa; 43241da177e4SLinus Torvalds } 43251d578303SMihai Maruseac 43261d578303SMihai Maruseac /* prepare for next bucket */ 43271d578303SMihai Maruseac state->offset = 0; 43281d578303SMihai Maruseac p = 0; 43291d578303SMihai Maruseac } 4330c2e21293Sstephen hemminger return NULL; 4331c2e21293Sstephen hemminger } 43321da177e4SLinus Torvalds 4333c2e21293Sstephen hemminger static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, 4334c2e21293Sstephen hemminger struct inet6_ifaddr *ifa) 43351da177e4SLinus Torvalds { 43361da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 43371218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 43381da177e4SLinus Torvalds 4339a5c1d98fSEric Dumazet hlist_for_each_entry_continue_rcu(ifa, addr_lst) { 43401d578303SMihai Maruseac state->offset++; 43411da177e4SLinus Torvalds return ifa; 43421d578303SMihai Maruseac } 43431da177e4SLinus Torvalds 43441d578303SMihai Maruseac state->offset = 0; 434586f9bd1fSJeff Barnhill while (++state->bucket < IN6_ADDR_HSIZE) { 4346a5c1d98fSEric Dumazet hlist_for_each_entry_rcu(ifa, 4347e66d1172SEric Dumazet &net->ipv6.inet6_addr_lst[state->bucket], addr_lst) { 4348c2e21293Sstephen hemminger return ifa; 4349c2e21293Sstephen hemminger } 4350c2e21293Sstephen hemminger } 4351c2e21293Sstephen hemminger 4352c2e21293Sstephen hemminger return NULL; 4353c2e21293Sstephen hemminger } 4354c2e21293Sstephen hemminger 43551da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos) 4356a5c1d98fSEric Dumazet __acquires(rcu) 43571da177e4SLinus Torvalds { 4358a5c1d98fSEric Dumazet rcu_read_lock(); 43591d578303SMihai Maruseac return if6_get_first(seq, *pos); 43601da177e4SLinus Torvalds } 43611da177e4SLinus Torvalds 43621da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos) 43631da177e4SLinus Torvalds { 43641da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 43651da177e4SLinus Torvalds 43661da177e4SLinus Torvalds ifa = if6_get_next(seq, v); 43671da177e4SLinus Torvalds ++*pos; 43681da177e4SLinus Torvalds return ifa; 43691da177e4SLinus Torvalds } 43701da177e4SLinus Torvalds 43711da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v) 4372a5c1d98fSEric Dumazet __releases(rcu) 43731da177e4SLinus Torvalds { 4374a5c1d98fSEric Dumazet rcu_read_unlock(); 43751da177e4SLinus Torvalds } 43761da177e4SLinus Torvalds 43771da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v) 43781da177e4SLinus Torvalds { 43791da177e4SLinus Torvalds struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v; 4380971a351cSJiri Pirko seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n", 4381b071195dSHarvey Harrison &ifp->addr, 43821da177e4SLinus Torvalds ifp->idev->dev->ifindex, 43831da177e4SLinus Torvalds ifp->prefix_len, 43841da177e4SLinus Torvalds ifp->scope, 4385971a351cSJiri Pirko (u8) ifp->flags, 43861da177e4SLinus Torvalds ifp->idev->dev->name); 43871da177e4SLinus Torvalds return 0; 43881da177e4SLinus Torvalds } 43891da177e4SLinus Torvalds 439056b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = { 43911da177e4SLinus Torvalds .start = if6_seq_start, 43921da177e4SLinus Torvalds .next = if6_seq_next, 43931da177e4SLinus Torvalds .show = if6_seq_show, 43941da177e4SLinus Torvalds .stop = if6_seq_stop, 43951da177e4SLinus Torvalds }; 43961da177e4SLinus Torvalds 43972c8c1e72SAlexey Dobriyan static int __net_init if6_proc_net_init(struct net *net) 43981da177e4SLinus Torvalds { 4399c3506372SChristoph Hellwig if (!proc_create_net("if_inet6", 0444, net->proc_net, &if6_seq_ops, 4400c3506372SChristoph Hellwig sizeof(struct if6_iter_state))) 44011da177e4SLinus Torvalds return -ENOMEM; 44021da177e4SLinus Torvalds return 0; 44031da177e4SLinus Torvalds } 44041da177e4SLinus Torvalds 44052c8c1e72SAlexey Dobriyan static void __net_exit if6_proc_net_exit(struct net *net) 44063c40090aSDaniel Lezcano { 4407ece31ffdSGao feng remove_proc_entry("if_inet6", net->proc_net); 44083c40090aSDaniel Lezcano } 44093c40090aSDaniel Lezcano 44103c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = { 44113c40090aSDaniel Lezcano .init = if6_proc_net_init, 44123c40090aSDaniel Lezcano .exit = if6_proc_net_exit, 44133c40090aSDaniel Lezcano }; 44143c40090aSDaniel Lezcano 44153c40090aSDaniel Lezcano int __init if6_proc_init(void) 44163c40090aSDaniel Lezcano { 44173c40090aSDaniel Lezcano return register_pernet_subsys(&if6_proc_net_ops); 44183c40090aSDaniel Lezcano } 44193c40090aSDaniel Lezcano 44201da177e4SLinus Torvalds void if6_proc_exit(void) 44211da177e4SLinus Torvalds { 44223c40090aSDaniel Lezcano unregister_pernet_subsys(&if6_proc_net_ops); 44231da177e4SLinus Torvalds } 44241da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 44251da177e4SLinus Torvalds 442607a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6) 44273b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */ 4428b71d1d42SEric Dumazet int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr) 44293b9f9a1cSNoriaki TAKAMIYA { 44303f27fb23SEric Dumazet unsigned int hash = inet6_addr_hash(net, addr); 4431c2e21293Sstephen hemminger struct inet6_ifaddr *ifp = NULL; 44323f27fb23SEric Dumazet int ret = 0; 4433c2e21293Sstephen hemminger 44344e5f47abSEric Dumazet rcu_read_lock(); 4435e66d1172SEric Dumazet hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) { 4436caad295fSYOSHIFUJI Hideaki if (ipv6_addr_equal(&ifp->addr, addr) && 44373b9f9a1cSNoriaki TAKAMIYA (ifp->flags & IFA_F_HOMEADDRESS)) { 44383b9f9a1cSNoriaki TAKAMIYA ret = 1; 44393b9f9a1cSNoriaki TAKAMIYA break; 44403b9f9a1cSNoriaki TAKAMIYA } 44413b9f9a1cSNoriaki TAKAMIYA } 44424e5f47abSEric Dumazet rcu_read_unlock(); 44433b9f9a1cSNoriaki TAKAMIYA return ret; 44443b9f9a1cSNoriaki TAKAMIYA } 44453b9f9a1cSNoriaki TAKAMIYA #endif 44463b9f9a1cSNoriaki TAKAMIYA 4447f37c6059SAlexander Aring /* RFC6554 has some algorithm to avoid loops in segment routing by 4448f37c6059SAlexander Aring * checking if the segments contains any of a local interface address. 4449f37c6059SAlexander Aring * 4450f37c6059SAlexander Aring * Quote: 4451f37c6059SAlexander Aring * 4452f37c6059SAlexander Aring * To detect loops in the SRH, a router MUST determine if the SRH 4453f37c6059SAlexander Aring * includes multiple addresses assigned to any interface on that router. 4454f37c6059SAlexander Aring * If such addresses appear more than once and are separated by at least 4455f37c6059SAlexander Aring * one address not assigned to that router. 4456f37c6059SAlexander Aring */ 4457f37c6059SAlexander Aring int ipv6_chk_rpl_srh_loop(struct net *net, const struct in6_addr *segs, 4458f37c6059SAlexander Aring unsigned char nsegs) 4459f37c6059SAlexander Aring { 4460f37c6059SAlexander Aring const struct in6_addr *addr; 4461f37c6059SAlexander Aring int i, ret = 0, found = 0; 4462f37c6059SAlexander Aring struct inet6_ifaddr *ifp; 4463f37c6059SAlexander Aring bool separated = false; 4464f37c6059SAlexander Aring unsigned int hash; 4465f37c6059SAlexander Aring bool hash_found; 4466f37c6059SAlexander Aring 4467f37c6059SAlexander Aring rcu_read_lock(); 4468f37c6059SAlexander Aring for (i = 0; i < nsegs; i++) { 4469f37c6059SAlexander Aring addr = &segs[i]; 4470f37c6059SAlexander Aring hash = inet6_addr_hash(net, addr); 4471f37c6059SAlexander Aring 4472f37c6059SAlexander Aring hash_found = false; 4473e66d1172SEric Dumazet hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) { 4474f37c6059SAlexander Aring 4475f37c6059SAlexander Aring if (ipv6_addr_equal(&ifp->addr, addr)) { 4476f37c6059SAlexander Aring hash_found = true; 4477f37c6059SAlexander Aring break; 4478f37c6059SAlexander Aring } 4479f37c6059SAlexander Aring } 4480f37c6059SAlexander Aring 4481f37c6059SAlexander Aring if (hash_found) { 4482f37c6059SAlexander Aring if (found > 1 && separated) { 4483f37c6059SAlexander Aring ret = 1; 4484f37c6059SAlexander Aring break; 4485f37c6059SAlexander Aring } 4486f37c6059SAlexander Aring 4487f37c6059SAlexander Aring separated = false; 4488f37c6059SAlexander Aring found++; 4489f37c6059SAlexander Aring } else { 4490f37c6059SAlexander Aring separated = true; 4491f37c6059SAlexander Aring } 4492f37c6059SAlexander Aring } 4493f37c6059SAlexander Aring rcu_read_unlock(); 4494f37c6059SAlexander Aring 4495f37c6059SAlexander Aring return ret; 4496f37c6059SAlexander Aring } 4497f37c6059SAlexander Aring 44981da177e4SLinus Torvalds /* 44991da177e4SLinus Torvalds * Periodic address status verification 45001da177e4SLinus Torvalds */ 45011da177e4SLinus Torvalds 45028805d13fSEric Dumazet static void addrconf_verify_rtnl(struct net *net) 45031da177e4SLinus Torvalds { 4504b2db7564SYOSHIFUJI Hideaki unsigned long now, next, next_sec, next_sched; 45051da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 45061da177e4SLinus Torvalds int i; 45071da177e4SLinus Torvalds 4508c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 4509c15b1ccaSHannes Frederic Sowa 45105c578aedSstephen hemminger rcu_read_lock_bh(); 45111da177e4SLinus Torvalds now = jiffies; 4512b2db7564SYOSHIFUJI Hideaki next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY); 45131da177e4SLinus Torvalds 45148805d13fSEric Dumazet cancel_delayed_work(&net->ipv6.addr_chk_work); 45151da177e4SLinus Torvalds 45161da177e4SLinus Torvalds for (i = 0; i < IN6_ADDR_HSIZE; i++) { 45171da177e4SLinus Torvalds restart: 4518e66d1172SEric Dumazet hlist_for_each_entry_rcu_bh(ifp, &net->ipv6.inet6_addr_lst[i], addr_lst) { 45191da177e4SLinus Torvalds unsigned long age; 45201da177e4SLinus Torvalds 4521fad8da3eSYasushi Asano /* When setting preferred_lft to a value not zero or 4522fad8da3eSYasushi Asano * infinity, while valid_lft is infinity 4523fad8da3eSYasushi Asano * IFA_F_PERMANENT has a non-infinity life time. 4524fad8da3eSYasushi Asano */ 4525fad8da3eSYasushi Asano if ((ifp->flags & IFA_F_PERMANENT) && 4526fad8da3eSYasushi Asano (ifp->prefered_lft == INFINITY_LIFE_TIME)) 45271da177e4SLinus Torvalds continue; 45281da177e4SLinus Torvalds 45291da177e4SLinus Torvalds spin_lock(&ifp->lock); 4530b2db7564SYOSHIFUJI Hideaki /* We try to batch several events at once. */ 4531b2db7564SYOSHIFUJI Hideaki age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; 45321da177e4SLinus Torvalds 4533778964f2SSam Edwards if ((ifp->flags&IFA_F_TEMPORARY) && 4534778964f2SSam Edwards !(ifp->flags&IFA_F_TENTATIVE) && 4535778964f2SSam Edwards ifp->prefered_lft != INFINITY_LIFE_TIME && 4536778964f2SSam Edwards !ifp->regen_count && ifp->ifpub) { 4537778964f2SSam Edwards /* This is a non-regenerated temporary addr. */ 4538778964f2SSam Edwards 4539778964f2SSam Edwards unsigned long regen_advance = ifp->idev->cnf.regen_max_retry * 4540778964f2SSam Edwards ifp->idev->cnf.dad_transmits * 4541778964f2SSam Edwards max(NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME), HZ/100) / HZ; 4542778964f2SSam Edwards 4543778964f2SSam Edwards if (age + regen_advance >= ifp->prefered_lft) { 4544778964f2SSam Edwards struct inet6_ifaddr *ifpub = ifp->ifpub; 4545778964f2SSam Edwards if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 4546778964f2SSam Edwards next = ifp->tstamp + ifp->prefered_lft * HZ; 4547778964f2SSam Edwards 4548778964f2SSam Edwards ifp->regen_count++; 4549778964f2SSam Edwards in6_ifa_hold(ifp); 4550778964f2SSam Edwards in6_ifa_hold(ifpub); 4551778964f2SSam Edwards spin_unlock(&ifp->lock); 4552778964f2SSam Edwards 4553778964f2SSam Edwards spin_lock(&ifpub->lock); 4554778964f2SSam Edwards ifpub->regen_count = 0; 4555778964f2SSam Edwards spin_unlock(&ifpub->lock); 4556778964f2SSam Edwards rcu_read_unlock_bh(); 4557778964f2SSam Edwards ipv6_create_tempaddr(ifpub, true); 4558778964f2SSam Edwards in6_ifa_put(ifpub); 4559778964f2SSam Edwards in6_ifa_put(ifp); 4560778964f2SSam Edwards rcu_read_lock_bh(); 4561778964f2SSam Edwards goto restart; 4562778964f2SSam Edwards } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next)) 4563778964f2SSam Edwards next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ; 4564778964f2SSam Edwards } 4565778964f2SSam Edwards 45668f27ebb9SYOSHIFUJI Hideaki if (ifp->valid_lft != INFINITY_LIFE_TIME && 45678f27ebb9SYOSHIFUJI Hideaki age >= ifp->valid_lft) { 45681da177e4SLinus Torvalds spin_unlock(&ifp->lock); 45691da177e4SLinus Torvalds in6_ifa_hold(ifp); 4570aa8caa76STaehee Yoo rcu_read_unlock_bh(); 45711da177e4SLinus Torvalds ipv6_del_addr(ifp); 4572aa8caa76STaehee Yoo rcu_read_lock_bh(); 45731da177e4SLinus Torvalds goto restart; 45748f27ebb9SYOSHIFUJI Hideaki } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) { 45758f27ebb9SYOSHIFUJI Hideaki spin_unlock(&ifp->lock); 45768f27ebb9SYOSHIFUJI Hideaki continue; 45771da177e4SLinus Torvalds } else if (age >= ifp->prefered_lft) { 4578a1ed0526SBrian Haley /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */ 45791da177e4SLinus Torvalds int deprecate = 0; 45801da177e4SLinus Torvalds 45811da177e4SLinus Torvalds if (!(ifp->flags&IFA_F_DEPRECATED)) { 45821da177e4SLinus Torvalds deprecate = 1; 45831da177e4SLinus Torvalds ifp->flags |= IFA_F_DEPRECATED; 45841da177e4SLinus Torvalds } 45851da177e4SLinus Torvalds 4586fad8da3eSYasushi Asano if ((ifp->valid_lft != INFINITY_LIFE_TIME) && 4587fad8da3eSYasushi Asano (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))) 45881da177e4SLinus Torvalds next = ifp->tstamp + ifp->valid_lft * HZ; 45891da177e4SLinus Torvalds 45901da177e4SLinus Torvalds spin_unlock(&ifp->lock); 45911da177e4SLinus Torvalds 45921da177e4SLinus Torvalds if (deprecate) { 45931da177e4SLinus Torvalds in6_ifa_hold(ifp); 45941da177e4SLinus Torvalds 45951da177e4SLinus Torvalds ipv6_ifa_notify(0, ifp); 45961da177e4SLinus Torvalds in6_ifa_put(ifp); 45971da177e4SLinus Torvalds goto restart; 45981da177e4SLinus Torvalds } 45991da177e4SLinus Torvalds } else { 46001da177e4SLinus Torvalds /* ifp->prefered_lft <= ifp->valid_lft */ 46011da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 46021da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 46031da177e4SLinus Torvalds spin_unlock(&ifp->lock); 46041da177e4SLinus Torvalds } 46051da177e4SLinus Torvalds } 46061da177e4SLinus Torvalds } 46071da177e4SLinus Torvalds 4608b2db7564SYOSHIFUJI Hideaki next_sec = round_jiffies_up(next); 4609b2db7564SYOSHIFUJI Hideaki next_sched = next; 4610b2db7564SYOSHIFUJI Hideaki 4611b2db7564SYOSHIFUJI Hideaki /* If rounded timeout is accurate enough, accept it. */ 4612b2db7564SYOSHIFUJI Hideaki if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ)) 4613b2db7564SYOSHIFUJI Hideaki next_sched = next_sec; 4614b2db7564SYOSHIFUJI Hideaki 4615b2db7564SYOSHIFUJI Hideaki /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */ 4616b2db7564SYOSHIFUJI Hideaki if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX)) 4617b2db7564SYOSHIFUJI Hideaki next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX; 4618b2db7564SYOSHIFUJI Hideaki 4619e32ac250SJoe Perches pr_debug("now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n", 4620ba3542e1Sdingtianhong now, next, next_sec, next_sched); 46218805d13fSEric Dumazet mod_delayed_work(addrconf_wq, &net->ipv6.addr_chk_work, next_sched - now); 46225c578aedSstephen hemminger rcu_read_unlock_bh(); 46231da177e4SLinus Torvalds } 46241da177e4SLinus Torvalds 4625c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *w) 4626c15b1ccaSHannes Frederic Sowa { 46278805d13fSEric Dumazet struct net *net = container_of(to_delayed_work(w), struct net, 46288805d13fSEric Dumazet ipv6.addr_chk_work); 46298805d13fSEric Dumazet 4630c15b1ccaSHannes Frederic Sowa rtnl_lock(); 46318805d13fSEric Dumazet addrconf_verify_rtnl(net); 4632c15b1ccaSHannes Frederic Sowa rtnl_unlock(); 4633c15b1ccaSHannes Frederic Sowa } 4634c15b1ccaSHannes Frederic Sowa 46358805d13fSEric Dumazet static void addrconf_verify(struct net *net) 4636c15b1ccaSHannes Frederic Sowa { 46378805d13fSEric Dumazet mod_delayed_work(addrconf_wq, &net->ipv6.addr_chk_work, 0); 4638c15b1ccaSHannes Frederic Sowa } 4639c15b1ccaSHannes Frederic Sowa 4640caeaba79SNicolas Dichtel static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local, 4641caeaba79SNicolas Dichtel struct in6_addr **peer_pfx) 4642461d8837SThomas Graf { 4643461d8837SThomas Graf struct in6_addr *pfx = NULL; 4644461d8837SThomas Graf 4645caeaba79SNicolas Dichtel *peer_pfx = NULL; 4646caeaba79SNicolas Dichtel 4647461d8837SThomas Graf if (addr) 4648461d8837SThomas Graf pfx = nla_data(addr); 4649461d8837SThomas Graf 4650461d8837SThomas Graf if (local) { 4651461d8837SThomas Graf if (pfx && nla_memcmp(local, pfx, sizeof(*pfx))) 4652caeaba79SNicolas Dichtel *peer_pfx = pfx; 4653461d8837SThomas Graf pfx = nla_data(local); 4654461d8837SThomas Graf } 4655461d8837SThomas Graf 4656461d8837SThomas Graf return pfx; 4657461d8837SThomas Graf } 4658461d8837SThomas Graf 4659ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = { 4660461d8837SThomas Graf [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) }, 4661461d8837SThomas Graf [IFA_LOCAL] = { .len = sizeof(struct in6_addr) }, 4662461d8837SThomas Graf [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) }, 4663479840ffSJiri Pirko [IFA_FLAGS] = { .len = sizeof(u32) }, 46648308f3ffSDavid Ahern [IFA_RT_PRIORITY] = { .len = sizeof(u32) }, 46656ecf4c37SChristian Brauner [IFA_TARGET_NETNSID] = { .type = NLA_S32 }, 466647f0bd50SJacques de Laval [IFA_PROTO] = { .type = NLA_U8 }, 4667461d8837SThomas Graf }; 4668461d8837SThomas Graf 46691da177e4SLinus Torvalds static int 4670c21ef3e3SDavid Ahern inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, 4671c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 46721da177e4SLinus Torvalds { 46733b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 4674b933f716SThomas Graf struct ifaddrmsg *ifm; 4675b933f716SThomas Graf struct nlattr *tb[IFA_MAX+1]; 4676caeaba79SNicolas Dichtel struct in6_addr *pfx, *peer_pfx; 46776046d5b4SHeiner Kallweit u32 ifa_flags; 4678b933f716SThomas Graf int err; 46791da177e4SLinus Torvalds 46808cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX, 46818cb08174SJohannes Berg ifa_ipv6_policy, extack); 4682b933f716SThomas Graf if (err < 0) 4683b933f716SThomas Graf return err; 4684b933f716SThomas Graf 4685b933f716SThomas Graf ifm = nlmsg_data(nlh); 4686caeaba79SNicolas Dichtel pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); 468763159f29SIan Morris if (!pfx) 46881da177e4SLinus Torvalds return -EINVAL; 46891da177e4SLinus Torvalds 46906046d5b4SHeiner Kallweit ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags; 46916046d5b4SHeiner Kallweit 46926046d5b4SHeiner Kallweit /* We ignore other flags so far. */ 46936046d5b4SHeiner Kallweit ifa_flags &= IFA_F_MANAGETEMPADDR; 46946046d5b4SHeiner Kallweit 46956046d5b4SHeiner Kallweit return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx, 46966046d5b4SHeiner Kallweit ifm->ifa_prefixlen); 46971da177e4SLinus Torvalds } 46981da177e4SLinus Torvalds 46998308f3ffSDavid Ahern static int modify_prefix_route(struct inet6_ifaddr *ifp, 470061794012SHangbin Liu unsigned long expires, u32 flags, 470161794012SHangbin Liu bool modify_peer) 47028308f3ffSDavid Ahern { 47038308f3ffSDavid Ahern struct fib6_info *f6i; 4704e7c7faa9SDavid Ahern u32 prio; 47058308f3ffSDavid Ahern 470661794012SHangbin Liu f6i = addrconf_get_prefix_route(modify_peer ? &ifp->peer_addr : &ifp->addr, 470761794012SHangbin Liu ifp->prefix_len, 47082b2450caSDavid Ahern ifp->idev->dev, 0, RTF_DEFAULT, true); 47098308f3ffSDavid Ahern if (!f6i) 47108308f3ffSDavid Ahern return -ENOENT; 47118308f3ffSDavid Ahern 4712e7c7faa9SDavid Ahern prio = ifp->rt_priority ? : IP6_RT_PRIO_ADDRCONF; 4713e7c7faa9SDavid Ahern if (f6i->fib6_metric != prio) { 4714e7c7faa9SDavid Ahern /* delete old one */ 471511dd74b3SRoopa Prabhu ip6_del_rt(dev_net(ifp->idev->dev), f6i, false); 4716e7c7faa9SDavid Ahern 47178308f3ffSDavid Ahern /* add new one */ 471861794012SHangbin Liu addrconf_prefix_route(modify_peer ? &ifp->peer_addr : &ifp->addr, 471961794012SHangbin Liu ifp->prefix_len, 47208308f3ffSDavid Ahern ifp->rt_priority, ifp->idev->dev, 47218308f3ffSDavid Ahern expires, flags, GFP_KERNEL); 47228308f3ffSDavid Ahern } else { 47238308f3ffSDavid Ahern if (!expires) 47248308f3ffSDavid Ahern fib6_clean_expires(f6i); 47258308f3ffSDavid Ahern else 47268308f3ffSDavid Ahern fib6_set_expires(f6i, expires); 47278308f3ffSDavid Ahern 47288308f3ffSDavid Ahern fib6_info_release(f6i); 47298308f3ffSDavid Ahern } 47308308f3ffSDavid Ahern 47318308f3ffSDavid Ahern return 0; 47328308f3ffSDavid Ahern } 47338308f3ffSDavid Ahern 47348805d13fSEric Dumazet static int inet6_addr_modify(struct net *net, struct inet6_ifaddr *ifp, 47358805d13fSEric Dumazet struct ifa6_config *cfg) 4736081bba5bSNoriaki TAKAMIYA { 47376f704992SYOSHIFUJI Hideaki u32 flags; 47386f704992SYOSHIFUJI Hideaki clock_t expires; 47394bed72e4SYOSHIFUJI Hideaki unsigned long timeout; 474053bd6749SJiri Pirko bool was_managetempaddr; 47415b84efecSThomas Haller bool had_prefixroute; 4742d0098e4cSHangbin Liu bool new_peer = false; 474346d48046SYOSHIFUJI Hideaki 4744c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 4745c15b1ccaSHannes Frederic Sowa 4746d169a1f8SDavid Ahern if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft) 4747081bba5bSNoriaki TAKAMIYA return -EINVAL; 4748081bba5bSNoriaki TAKAMIYA 4749d169a1f8SDavid Ahern if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR && 475053bd6749SJiri Pirko (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64)) 475153bd6749SJiri Pirko return -EINVAL; 475253bd6749SJiri Pirko 4753f1c02cfbSSabrina Dubroca if (!(ifp->flags & IFA_F_TENTATIVE) || ifp->flags & IFA_F_DADFAILED) 4754d169a1f8SDavid Ahern cfg->ifa_flags &= ~IFA_F_OPTIMISTIC; 4755f1c02cfbSSabrina Dubroca 4756d169a1f8SDavid Ahern timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ); 47574bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 47584bed72e4SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(timeout * HZ); 4759d169a1f8SDavid Ahern cfg->valid_lft = timeout; 47606f704992SYOSHIFUJI Hideaki flags = RTF_EXPIRES; 47614bed72e4SYOSHIFUJI Hideaki } else { 47624bed72e4SYOSHIFUJI Hideaki expires = 0; 47634bed72e4SYOSHIFUJI Hideaki flags = 0; 4764d169a1f8SDavid Ahern cfg->ifa_flags |= IFA_F_PERMANENT; 47656f704992SYOSHIFUJI Hideaki } 4766081bba5bSNoriaki TAKAMIYA 4767d169a1f8SDavid Ahern timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ); 47684bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 47694bed72e4SYOSHIFUJI Hideaki if (timeout == 0) 4770d169a1f8SDavid Ahern cfg->ifa_flags |= IFA_F_DEPRECATED; 4771d169a1f8SDavid Ahern cfg->preferred_lft = timeout; 47724bed72e4SYOSHIFUJI Hideaki } 4773081bba5bSNoriaki TAKAMIYA 4774d0098e4cSHangbin Liu if (cfg->peer_pfx && 4775d0098e4cSHangbin Liu memcmp(&ifp->peer_addr, cfg->peer_pfx, sizeof(struct in6_addr))) { 4776d0098e4cSHangbin Liu if (!ipv6_addr_any(&ifp->peer_addr)) 4777d0098e4cSHangbin Liu cleanup_prefix_route(ifp, expires, true, true); 4778d0098e4cSHangbin Liu new_peer = true; 4779d0098e4cSHangbin Liu } 4780d0098e4cSHangbin Liu 4781081bba5bSNoriaki TAKAMIYA spin_lock_bh(&ifp->lock); 478253bd6749SJiri Pirko was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR; 47835b84efecSThomas Haller had_prefixroute = ifp->flags & IFA_F_PERMANENT && 47845b84efecSThomas Haller !(ifp->flags & IFA_F_NOPREFIXROUTE); 478553bd6749SJiri Pirko ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | 4786761aac73SThomas Haller IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR | 4787761aac73SThomas Haller IFA_F_NOPREFIXROUTE); 4788d169a1f8SDavid Ahern ifp->flags |= cfg->ifa_flags; 4789081bba5bSNoriaki TAKAMIYA ifp->tstamp = jiffies; 4790d169a1f8SDavid Ahern ifp->valid_lft = cfg->valid_lft; 4791d169a1f8SDavid Ahern ifp->prefered_lft = cfg->preferred_lft; 479247f0bd50SJacques de Laval ifp->ifa_proto = cfg->ifa_proto; 4793081bba5bSNoriaki TAKAMIYA 47948308f3ffSDavid Ahern if (cfg->rt_priority && cfg->rt_priority != ifp->rt_priority) 47958308f3ffSDavid Ahern ifp->rt_priority = cfg->rt_priority; 4796081bba5bSNoriaki TAKAMIYA 4797d0098e4cSHangbin Liu if (new_peer) 4798d0098e4cSHangbin Liu ifp->peer_addr = *cfg->peer_pfx; 4799d0098e4cSHangbin Liu 4800081bba5bSNoriaki TAKAMIYA spin_unlock_bh(&ifp->lock); 4801081bba5bSNoriaki TAKAMIYA if (!(ifp->flags&IFA_F_TENTATIVE)) 4802081bba5bSNoriaki TAKAMIYA ipv6_ifa_notify(0, ifp); 4803081bba5bSNoriaki TAKAMIYA 4804d169a1f8SDavid Ahern if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) { 48058308f3ffSDavid Ahern int rc = -ENOENT; 48068308f3ffSDavid Ahern 48078308f3ffSDavid Ahern if (had_prefixroute) 480861794012SHangbin Liu rc = modify_prefix_route(ifp, expires, flags, false); 48098308f3ffSDavid Ahern 48108308f3ffSDavid Ahern /* prefix route could have been deleted; if so restore it */ 48118308f3ffSDavid Ahern if (rc == -ENOENT) { 4812acb54e3cSDavid Ahern addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 48138308f3ffSDavid Ahern ifp->rt_priority, ifp->idev->dev, 48148308f3ffSDavid Ahern expires, flags, GFP_KERNEL); 48158308f3ffSDavid Ahern } 481661794012SHangbin Liu 481761794012SHangbin Liu if (had_prefixroute && !ipv6_addr_any(&ifp->peer_addr)) 481861794012SHangbin Liu rc = modify_prefix_route(ifp, expires, flags, true); 481961794012SHangbin Liu 482061794012SHangbin Liu if (rc == -ENOENT && !ipv6_addr_any(&ifp->peer_addr)) { 482161794012SHangbin Liu addrconf_prefix_route(&ifp->peer_addr, ifp->prefix_len, 482261794012SHangbin Liu ifp->rt_priority, ifp->idev->dev, 482361794012SHangbin Liu expires, flags, GFP_KERNEL); 482461794012SHangbin Liu } 48255b84efecSThomas Haller } else if (had_prefixroute) { 48265b84efecSThomas Haller enum cleanup_prefix_rt_t action; 48275b84efecSThomas Haller unsigned long rt_expires; 48285b84efecSThomas Haller 48295b84efecSThomas Haller write_lock_bh(&ifp->idev->lock); 48305b84efecSThomas Haller action = check_cleanup_prefix_route(ifp, &rt_expires); 48315b84efecSThomas Haller write_unlock_bh(&ifp->idev->lock); 48325b84efecSThomas Haller 48335b84efecSThomas Haller if (action != CLEANUP_PREFIX_RT_NOP) { 48345b84efecSThomas Haller cleanup_prefix_route(ifp, rt_expires, 4835d0098e4cSHangbin Liu action == CLEANUP_PREFIX_RT_DEL, false); 48365b84efecSThomas Haller } 4837761aac73SThomas Haller } 483853bd6749SJiri Pirko 483953bd6749SJiri Pirko if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) { 4840d169a1f8SDavid Ahern if (was_managetempaddr && 4841d169a1f8SDavid Ahern !(ifp->flags & IFA_F_MANAGETEMPADDR)) { 4842d169a1f8SDavid Ahern cfg->valid_lft = 0; 4843d169a1f8SDavid Ahern cfg->preferred_lft = 0; 4844d169a1f8SDavid Ahern } 4845d169a1f8SDavid Ahern manage_tempaddrs(ifp->idev, ifp, cfg->valid_lft, 4846d169a1f8SDavid Ahern cfg->preferred_lft, !was_managetempaddr, 4847d169a1f8SDavid Ahern jiffies); 484853bd6749SJiri Pirko } 484953bd6749SJiri Pirko 48508805d13fSEric Dumazet addrconf_verify_rtnl(net); 4851081bba5bSNoriaki TAKAMIYA 4852081bba5bSNoriaki TAKAMIYA return 0; 4853081bba5bSNoriaki TAKAMIYA } 4854081bba5bSNoriaki TAKAMIYA 4855081bba5bSNoriaki TAKAMIYA static int 4856c21ef3e3SDavid Ahern inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, 4857c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 48581da177e4SLinus Torvalds { 48593b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 4860461d8837SThomas Graf struct ifaddrmsg *ifm; 4861461d8837SThomas Graf struct nlattr *tb[IFA_MAX+1]; 486219b1518cSDavid Ahern struct in6_addr *peer_pfx; 48637198f8ceSThomas Graf struct inet6_ifaddr *ifa; 48647198f8ceSThomas Graf struct net_device *dev; 4865f1c02cfbSSabrina Dubroca struct inet6_dev *idev; 486619b1518cSDavid Ahern struct ifa6_config cfg; 4867461d8837SThomas Graf int err; 48681da177e4SLinus Torvalds 48698cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX, 48708cb08174SJohannes Berg ifa_ipv6_policy, extack); 4871461d8837SThomas Graf if (err < 0) 4872461d8837SThomas Graf return err; 4873461d8837SThomas Graf 487419b1518cSDavid Ahern memset(&cfg, 0, sizeof(cfg)); 487519b1518cSDavid Ahern 4876461d8837SThomas Graf ifm = nlmsg_data(nlh); 487719b1518cSDavid Ahern cfg.pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); 487819b1518cSDavid Ahern if (!cfg.pfx) 48791da177e4SLinus Torvalds return -EINVAL; 48801da177e4SLinus Torvalds 488119b1518cSDavid Ahern cfg.peer_pfx = peer_pfx; 488219b1518cSDavid Ahern cfg.plen = ifm->ifa_prefixlen; 48838308f3ffSDavid Ahern if (tb[IFA_RT_PRIORITY]) 48848308f3ffSDavid Ahern cfg.rt_priority = nla_get_u32(tb[IFA_RT_PRIORITY]); 48858308f3ffSDavid Ahern 488647f0bd50SJacques de Laval if (tb[IFA_PROTO]) 488747f0bd50SJacques de Laval cfg.ifa_proto = nla_get_u8(tb[IFA_PROTO]); 488847f0bd50SJacques de Laval 488919b1518cSDavid Ahern cfg.valid_lft = INFINITY_LIFE_TIME; 489019b1518cSDavid Ahern cfg.preferred_lft = INFINITY_LIFE_TIME; 489119b1518cSDavid Ahern 4892461d8837SThomas Graf if (tb[IFA_CACHEINFO]) { 48930778769dSNoriaki TAKAMIYA struct ifa_cacheinfo *ci; 4894461d8837SThomas Graf 4895461d8837SThomas Graf ci = nla_data(tb[IFA_CACHEINFO]); 489619b1518cSDavid Ahern cfg.valid_lft = ci->ifa_valid; 489719b1518cSDavid Ahern cfg.preferred_lft = ci->ifa_prefered; 48980778769dSNoriaki TAKAMIYA } 48990778769dSNoriaki TAKAMIYA 4900af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifm->ifa_index); 490163159f29SIan Morris if (!dev) 49027198f8ceSThomas Graf return -ENODEV; 4903081bba5bSNoriaki TAKAMIYA 490419b1518cSDavid Ahern if (tb[IFA_FLAGS]) 490519b1518cSDavid Ahern cfg.ifa_flags = nla_get_u32(tb[IFA_FLAGS]); 490619b1518cSDavid Ahern else 490719b1518cSDavid Ahern cfg.ifa_flags = ifm->ifa_flags; 4908479840ffSJiri Pirko 490955ebaef1SNoriaki TAKAMIYA /* We ignore other flags so far. */ 491019b1518cSDavid Ahern cfg.ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | 491119b1518cSDavid Ahern IFA_F_MANAGETEMPADDR | IFA_F_NOPREFIXROUTE | 491219b1518cSDavid Ahern IFA_F_MCAUTOJOIN | IFA_F_OPTIMISTIC; 4913f1c02cfbSSabrina Dubroca 4914f1c02cfbSSabrina Dubroca idev = ipv6_find_idev(dev); 4915db0b99f5SSabrina Dubroca if (IS_ERR(idev)) 4916db0b99f5SSabrina Dubroca return PTR_ERR(idev); 4917f1c02cfbSSabrina Dubroca 4918f1c02cfbSSabrina Dubroca if (!ipv6_allow_optimistic_dad(net, idev)) 491919b1518cSDavid Ahern cfg.ifa_flags &= ~IFA_F_OPTIMISTIC; 4920f1c02cfbSSabrina Dubroca 492119b1518cSDavid Ahern if (cfg.ifa_flags & IFA_F_NODAD && 492219b1518cSDavid Ahern cfg.ifa_flags & IFA_F_OPTIMISTIC) { 4923f1c02cfbSSabrina Dubroca NL_SET_ERR_MSG(extack, "IFA_F_NODAD and IFA_F_OPTIMISTIC are mutually exclusive"); 4924f1c02cfbSSabrina Dubroca return -EINVAL; 4925f1c02cfbSSabrina Dubroca } 492655ebaef1SNoriaki TAKAMIYA 492719b1518cSDavid Ahern ifa = ipv6_get_ifaddr(net, cfg.pfx, dev, 1); 492863159f29SIan Morris if (!ifa) { 49297198f8ceSThomas Graf /* 49307198f8ceSThomas Graf * It would be best to check for !NLM_F_CREATE here but 4931db9c7c39Sstephen hemminger * userspace already relies on not having to provide this. 49327198f8ceSThomas Graf */ 493319b1518cSDavid Ahern return inet6_addr_add(net, ifm->ifa_index, &cfg, extack); 49341da177e4SLinus Torvalds } 49351da177e4SLinus Torvalds 49367198f8ceSThomas Graf if (nlh->nlmsg_flags & NLM_F_EXCL || 49377198f8ceSThomas Graf !(nlh->nlmsg_flags & NLM_F_REPLACE)) 49387198f8ceSThomas Graf err = -EEXIST; 49397198f8ceSThomas Graf else 49408805d13fSEric Dumazet err = inet6_addr_modify(net, ifa, &cfg); 49417198f8ceSThomas Graf 49427198f8ceSThomas Graf in6_ifa_put(ifa); 49437198f8ceSThomas Graf 49447198f8ceSThomas Graf return err; 49457198f8ceSThomas Graf } 49467198f8ceSThomas Graf 4947479840ffSJiri Pirko static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags, 4948101bb229SThomas Graf u8 scope, int ifindex) 4949101bb229SThomas Graf { 4950101bb229SThomas Graf struct ifaddrmsg *ifm; 4951101bb229SThomas Graf 4952101bb229SThomas Graf ifm = nlmsg_data(nlh); 4953101bb229SThomas Graf ifm->ifa_family = AF_INET6; 4954101bb229SThomas Graf ifm->ifa_prefixlen = prefixlen; 4955101bb229SThomas Graf ifm->ifa_flags = flags; 4956101bb229SThomas Graf ifm->ifa_scope = scope; 4957101bb229SThomas Graf ifm->ifa_index = ifindex; 4958101bb229SThomas Graf } 4959101bb229SThomas Graf 496085486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp, 496185486af0SThomas Graf unsigned long tstamp, u32 preferred, u32 valid) 496285486af0SThomas Graf { 496385486af0SThomas Graf struct ifa_cacheinfo ci; 496485486af0SThomas Graf 496518a31e1eSThomas Graf ci.cstamp = cstamp_delta(cstamp); 496618a31e1eSThomas Graf ci.tstamp = cstamp_delta(tstamp); 496785486af0SThomas Graf ci.ifa_prefered = preferred; 496885486af0SThomas Graf ci.ifa_valid = valid; 496985486af0SThomas Graf 497085486af0SThomas Graf return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci); 497185486af0SThomas Graf } 497285486af0SThomas Graf 4973101bb229SThomas Graf static inline int rt_scope(int ifa_scope) 4974101bb229SThomas Graf { 4975101bb229SThomas Graf if (ifa_scope & IFA_HOST) 4976101bb229SThomas Graf return RT_SCOPE_HOST; 4977101bb229SThomas Graf else if (ifa_scope & IFA_LINK) 4978101bb229SThomas Graf return RT_SCOPE_LINK; 4979101bb229SThomas Graf else if (ifa_scope & IFA_SITE) 4980101bb229SThomas Graf return RT_SCOPE_SITE; 4981101bb229SThomas Graf else 4982101bb229SThomas Graf return RT_SCOPE_UNIVERSE; 4983101bb229SThomas Graf } 4984101bb229SThomas Graf 49850ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void) 49860ab6803bSThomas Graf { 4987339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifaddrmsg)) 4988caeaba79SNicolas Dichtel + nla_total_size(16) /* IFA_LOCAL */ 4989339bf98fSThomas Graf + nla_total_size(16) /* IFA_ADDRESS */ 4990479840ffSJiri Pirko + nla_total_size(sizeof(struct ifa_cacheinfo)) 49918308f3ffSDavid Ahern + nla_total_size(4) /* IFA_FLAGS */ 499247f0bd50SJacques de Laval + nla_total_size(1) /* IFA_PROTO */ 49938308f3ffSDavid Ahern + nla_total_size(4) /* IFA_RT_PRIORITY */; 49940ab6803bSThomas Graf } 4995c5396a31SYOSHIFUJI Hideaki 49966ba1e6e8SDavid Ahern enum addr_type_t { 49976ba1e6e8SDavid Ahern UNICAST_ADDR, 49986ba1e6e8SDavid Ahern MULTICAST_ADDR, 49996ba1e6e8SDavid Ahern ANYCAST_ADDR, 50006ba1e6e8SDavid Ahern }; 50016ba1e6e8SDavid Ahern 5002203651b6SChristian Brauner struct inet6_fill_args { 5003203651b6SChristian Brauner u32 portid; 5004203651b6SChristian Brauner u32 seq; 5005203651b6SChristian Brauner int event; 5006203651b6SChristian Brauner unsigned int flags; 5007203651b6SChristian Brauner int netnsid; 50086371a71fSDavid Ahern int ifindex; 50096ba1e6e8SDavid Ahern enum addr_type_t type; 5010203651b6SChristian Brauner }; 5011203651b6SChristian Brauner 50121da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa, 5013203651b6SChristian Brauner struct inet6_fill_args *args) 50141da177e4SLinus Torvalds { 50151da177e4SLinus Torvalds struct nlmsghdr *nlh; 501685486af0SThomas Graf u32 preferred, valid; 50171da177e4SLinus Torvalds 5018203651b6SChristian Brauner nlh = nlmsg_put(skb, args->portid, args->seq, args->event, 5019203651b6SChristian Brauner sizeof(struct ifaddrmsg), args->flags); 502063159f29SIan Morris if (!nlh) 502126932566SPatrick McHardy return -EMSGSIZE; 50220ab6803bSThomas Graf 5023101bb229SThomas Graf put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope), 5024101bb229SThomas Graf ifa->idev->dev->ifindex); 5025101bb229SThomas Graf 5026203651b6SChristian Brauner if (args->netnsid >= 0 && 5027203651b6SChristian Brauner nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) 50286ecf4c37SChristian Brauner goto error; 50296ecf4c37SChristian Brauner 50306c0d8833SNiels Dossche spin_lock_bh(&ifa->lock); 5031fad8da3eSYasushi Asano if (!((ifa->flags&IFA_F_PERMANENT) && 5032fad8da3eSYasushi Asano (ifa->prefered_lft == INFINITY_LIFE_TIME))) { 503385486af0SThomas Graf preferred = ifa->prefered_lft; 503485486af0SThomas Graf valid = ifa->valid_lft; 503585486af0SThomas Graf if (preferred != INFINITY_LIFE_TIME) { 50361da177e4SLinus Torvalds long tval = (jiffies - ifa->tstamp)/HZ; 5037a1faa698SJens Rosenboom if (preferred > tval) 503885486af0SThomas Graf preferred -= tval; 5039a1faa698SJens Rosenboom else 5040a1faa698SJens Rosenboom preferred = 0; 5041f56619fcSBen Hutchings if (valid != INFINITY_LIFE_TIME) { 5042f56619fcSBen Hutchings if (valid > tval) 504385486af0SThomas Graf valid -= tval; 5044f56619fcSBen Hutchings else 5045f56619fcSBen Hutchings valid = 0; 5046f56619fcSBen Hutchings } 50471da177e4SLinus Torvalds } 50481da177e4SLinus Torvalds } else { 504985486af0SThomas Graf preferred = INFINITY_LIFE_TIME; 505085486af0SThomas Graf valid = INFINITY_LIFE_TIME; 50511da177e4SLinus Torvalds } 50526c0d8833SNiels Dossche spin_unlock_bh(&ifa->lock); 505385486af0SThomas Graf 50547996c799SCong Wang if (!ipv6_addr_any(&ifa->peer_addr)) { 5055930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 || 5056930345eaSJiri Benc nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0) 5057caeaba79SNicolas Dichtel goto error; 5058caeaba79SNicolas Dichtel } else 5059930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0) 5060caeaba79SNicolas Dichtel goto error; 5061caeaba79SNicolas Dichtel 50628308f3ffSDavid Ahern if (ifa->rt_priority && 50638308f3ffSDavid Ahern nla_put_u32(skb, IFA_RT_PRIORITY, ifa->rt_priority)) 50648308f3ffSDavid Ahern goto error; 50658308f3ffSDavid Ahern 5066caeaba79SNicolas Dichtel if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) 5067caeaba79SNicolas Dichtel goto error; 506885486af0SThomas Graf 5069479840ffSJiri Pirko if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0) 5070479840ffSJiri Pirko goto error; 5071479840ffSJiri Pirko 507247f0bd50SJacques de Laval if (ifa->ifa_proto && 507347f0bd50SJacques de Laval nla_put_u8(skb, IFA_PROTO, ifa->ifa_proto)) 507447f0bd50SJacques de Laval goto error; 507547f0bd50SJacques de Laval 5076053c095aSJohannes Berg nlmsg_end(skb, nlh); 5077053c095aSJohannes Berg return 0; 5078caeaba79SNicolas Dichtel 5079caeaba79SNicolas Dichtel error: 5080caeaba79SNicolas Dichtel nlmsg_cancel(skb, nlh); 5081caeaba79SNicolas Dichtel return -EMSGSIZE; 50821da177e4SLinus Torvalds } 50831da177e4SLinus Torvalds 50841da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca, 5085203651b6SChristian Brauner struct inet6_fill_args *args) 50861da177e4SLinus Torvalds { 50871da177e4SLinus Torvalds struct nlmsghdr *nlh; 5088101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 5089101bb229SThomas Graf int ifindex = ifmca->idev->dev->ifindex; 50901da177e4SLinus Torvalds 50911da177e4SLinus Torvalds if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE) 5092101bb229SThomas Graf scope = RT_SCOPE_SITE; 5093101bb229SThomas Graf 5094203651b6SChristian Brauner nlh = nlmsg_put(skb, args->portid, args->seq, args->event, 5095203651b6SChristian Brauner sizeof(struct ifaddrmsg), args->flags); 509663159f29SIan Morris if (!nlh) 509726932566SPatrick McHardy return -EMSGSIZE; 50980ab6803bSThomas Graf 5099203651b6SChristian Brauner if (args->netnsid >= 0 && 5100ceb736e1SZhang Qilong nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) { 5101ceb736e1SZhang Qilong nlmsg_cancel(skb, nlh); 51026ecf4c37SChristian Brauner return -EMSGSIZE; 5103ceb736e1SZhang Qilong } 51046ecf4c37SChristian Brauner 5105101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 5106930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 || 51070ab6803bSThomas Graf put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp, 510826932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 510926932566SPatrick McHardy nlmsg_cancel(skb, nlh); 511026932566SPatrick McHardy return -EMSGSIZE; 511126932566SPatrick McHardy } 511285486af0SThomas Graf 5113053c095aSJohannes Berg nlmsg_end(skb, nlh); 5114053c095aSJohannes Berg return 0; 51151da177e4SLinus Torvalds } 51161da177e4SLinus Torvalds 51171da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca, 5118203651b6SChristian Brauner struct inet6_fill_args *args) 51191da177e4SLinus Torvalds { 51209ee8cbb2SDavid Ahern struct net_device *dev = fib6_info_nh_dev(ifaca->aca_rt); 51219ee8cbb2SDavid Ahern int ifindex = dev ? dev->ifindex : 1; 51221da177e4SLinus Torvalds struct nlmsghdr *nlh; 5123101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 51241da177e4SLinus Torvalds 51251da177e4SLinus Torvalds if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE) 5126101bb229SThomas Graf scope = RT_SCOPE_SITE; 5127101bb229SThomas Graf 5128203651b6SChristian Brauner nlh = nlmsg_put(skb, args->portid, args->seq, args->event, 5129203651b6SChristian Brauner sizeof(struct ifaddrmsg), args->flags); 513063159f29SIan Morris if (!nlh) 513126932566SPatrick McHardy return -EMSGSIZE; 51320ab6803bSThomas Graf 5133203651b6SChristian Brauner if (args->netnsid >= 0 && 5134ceb736e1SZhang Qilong nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) { 5135ceb736e1SZhang Qilong nlmsg_cancel(skb, nlh); 51366ecf4c37SChristian Brauner return -EMSGSIZE; 5137ceb736e1SZhang Qilong } 51386ecf4c37SChristian Brauner 5139101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 5140930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 || 51410ab6803bSThomas Graf put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp, 514226932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 514326932566SPatrick McHardy nlmsg_cancel(skb, nlh); 514426932566SPatrick McHardy return -EMSGSIZE; 514526932566SPatrick McHardy } 514685486af0SThomas Graf 5147053c095aSJohannes Berg nlmsg_end(skb, nlh); 5148053c095aSJohannes Berg return 0; 51491da177e4SLinus Torvalds } 51501da177e4SLinus Torvalds 5151234b27c3SEric Dumazet /* called with rcu_read_lock() */ 5152234b27c3SEric Dumazet static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb, 5153fe884c2bSDavid Ahern struct netlink_callback *cb, int s_ip_idx, 51546ba1e6e8SDavid Ahern struct inet6_fill_args *fillargs) 51551da177e4SLinus Torvalds { 51561da177e4SLinus Torvalds struct ifmcaddr6 *ifmca; 51571da177e4SLinus Torvalds struct ifacaddr6 *ifaca; 5158fe884c2bSDavid Ahern int ip_idx = 0; 5159234b27c3SEric Dumazet int err = 1; 51601da177e4SLinus Torvalds 51611da177e4SLinus Torvalds read_lock_bh(&idev->lock); 51626ba1e6e8SDavid Ahern switch (fillargs->type) { 5163502a2ffdSstephen hemminger case UNICAST_ADDR: { 5164502a2ffdSstephen hemminger struct inet6_ifaddr *ifa; 51656ba1e6e8SDavid Ahern fillargs->event = RTM_NEWADDR; 5166502a2ffdSstephen hemminger 5167ae9cda5dSYOSHIFUJI Hideaki /* unicast address incl. temp addr */ 5168502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 51694ba4c566SDavid Ahern if (ip_idx < s_ip_idx) 51704ba4c566SDavid Ahern goto next; 51716ba1e6e8SDavid Ahern err = inet6_fill_ifaddr(skb, ifa, fillargs); 5172053c095aSJohannes Berg if (err < 0) 5173bcd32326SEric Dumazet break; 517463998ac2SNicolas Dichtel nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 51754ba4c566SDavid Ahern next: 51764ba4c566SDavid Ahern ip_idx++; 51771da177e4SLinus Torvalds } 51781da177e4SLinus Torvalds break; 5179502a2ffdSstephen hemminger } 51801da177e4SLinus Torvalds case MULTICAST_ADDR: 518188e2ca30STaehee Yoo read_unlock_bh(&idev->lock); 51826ba1e6e8SDavid Ahern fillargs->event = RTM_GETMULTICAST; 5183203651b6SChristian Brauner 51841da177e4SLinus Torvalds /* multicast address */ 51854e43e64dSEric Dumazet for (ifmca = rtnl_dereference(idev->mc_list); 518688e2ca30STaehee Yoo ifmca; 51874e43e64dSEric Dumazet ifmca = rtnl_dereference(ifmca->next), ip_idx++) { 51881da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 51891da177e4SLinus Torvalds continue; 51906ba1e6e8SDavid Ahern err = inet6_fill_ifmcaddr(skb, ifmca, fillargs); 5191053c095aSJohannes Berg if (err < 0) 5192bcd32326SEric Dumazet break; 51931da177e4SLinus Torvalds } 519488e2ca30STaehee Yoo read_lock_bh(&idev->lock); 51951da177e4SLinus Torvalds break; 51961da177e4SLinus Torvalds case ANYCAST_ADDR: 51976ba1e6e8SDavid Ahern fillargs->event = RTM_GETANYCAST; 51981da177e4SLinus Torvalds /* anycast address */ 51991da177e4SLinus Torvalds for (ifaca = idev->ac_list; ifaca; 52001da177e4SLinus Torvalds ifaca = ifaca->aca_next, ip_idx++) { 52011da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 52021da177e4SLinus Torvalds continue; 52036ba1e6e8SDavid Ahern err = inet6_fill_ifacaddr(skb, ifaca, fillargs); 5204053c095aSJohannes Berg if (err < 0) 5205bcd32326SEric Dumazet break; 52061da177e4SLinus Torvalds } 52071da177e4SLinus Torvalds break; 52081da177e4SLinus Torvalds default: 52091da177e4SLinus Torvalds break; 52101da177e4SLinus Torvalds } 52111da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 5212fe884c2bSDavid Ahern cb->args[2] = ip_idx; 5213234b27c3SEric Dumazet return err; 5214234b27c3SEric Dumazet } 52155d5619b4SYOSHIFUJI Hideaki 5216ed6eff11SDavid Ahern static int inet6_valid_dump_ifaddr_req(const struct nlmsghdr *nlh, 5217ed6eff11SDavid Ahern struct inet6_fill_args *fillargs, 5218ed6eff11SDavid Ahern struct net **tgt_net, struct sock *sk, 52196371a71fSDavid Ahern struct netlink_callback *cb) 5220ed6eff11SDavid Ahern { 52216371a71fSDavid Ahern struct netlink_ext_ack *extack = cb->extack; 5222ed6eff11SDavid Ahern struct nlattr *tb[IFA_MAX+1]; 5223ed6eff11SDavid Ahern struct ifaddrmsg *ifm; 5224ed6eff11SDavid Ahern int err, i; 5225ed6eff11SDavid Ahern 5226ed6eff11SDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 5227ed6eff11SDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Invalid header for address dump request"); 5228ed6eff11SDavid Ahern return -EINVAL; 5229ed6eff11SDavid Ahern } 5230ed6eff11SDavid Ahern 5231ed6eff11SDavid Ahern ifm = nlmsg_data(nlh); 5232ed6eff11SDavid Ahern if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) { 5233ed6eff11SDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for address dump request"); 5234ed6eff11SDavid Ahern return -EINVAL; 5235ed6eff11SDavid Ahern } 52366371a71fSDavid Ahern 52376371a71fSDavid Ahern fillargs->ifindex = ifm->ifa_index; 52386371a71fSDavid Ahern if (fillargs->ifindex) { 52396371a71fSDavid Ahern cb->answer_flags |= NLM_F_DUMP_FILTERED; 52406371a71fSDavid Ahern fillargs->flags |= NLM_F_DUMP_FILTERED; 5241ed6eff11SDavid Ahern } 5242ed6eff11SDavid Ahern 52438cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX, 5244ed6eff11SDavid Ahern ifa_ipv6_policy, extack); 5245ed6eff11SDavid Ahern if (err < 0) 5246ed6eff11SDavid Ahern return err; 5247ed6eff11SDavid Ahern 5248ed6eff11SDavid Ahern for (i = 0; i <= IFA_MAX; ++i) { 5249ed6eff11SDavid Ahern if (!tb[i]) 5250ed6eff11SDavid Ahern continue; 5251ed6eff11SDavid Ahern 5252ed6eff11SDavid Ahern if (i == IFA_TARGET_NETNSID) { 5253ed6eff11SDavid Ahern struct net *net; 5254ed6eff11SDavid Ahern 5255ed6eff11SDavid Ahern fillargs->netnsid = nla_get_s32(tb[i]); 5256ed6eff11SDavid Ahern net = rtnl_get_net_ns_capable(sk, fillargs->netnsid); 5257ed6eff11SDavid Ahern if (IS_ERR(net)) { 5258bf4cc40eSBjørn Mork fillargs->netnsid = -1; 5259ed6eff11SDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Invalid target network namespace id"); 5260ed6eff11SDavid Ahern return PTR_ERR(net); 5261ed6eff11SDavid Ahern } 5262ed6eff11SDavid Ahern *tgt_net = net; 5263ed6eff11SDavid Ahern } else { 5264ed6eff11SDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in dump request"); 5265ed6eff11SDavid Ahern return -EINVAL; 5266ed6eff11SDavid Ahern } 5267ed6eff11SDavid Ahern } 5268ed6eff11SDavid Ahern 5269ed6eff11SDavid Ahern return 0; 5270ed6eff11SDavid Ahern } 5271ed6eff11SDavid Ahern 5272234b27c3SEric Dumazet static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb, 5273234b27c3SEric Dumazet enum addr_type_t type) 5274234b27c3SEric Dumazet { 5275ed6eff11SDavid Ahern const struct nlmsghdr *nlh = cb->nlh; 52766ba1e6e8SDavid Ahern struct inet6_fill_args fillargs = { 52776ba1e6e8SDavid Ahern .portid = NETLINK_CB(cb->skb).portid, 52786ba1e6e8SDavid Ahern .seq = cb->nlh->nlmsg_seq, 52796ba1e6e8SDavid Ahern .flags = NLM_F_MULTI, 52806ba1e6e8SDavid Ahern .netnsid = -1, 52816ba1e6e8SDavid Ahern .type = type, 52826ba1e6e8SDavid Ahern }; 528387117bafSRocco Yue struct net *tgt_net = sock_net(skb->sk); 5284fe884c2bSDavid Ahern int idx, s_idx, s_ip_idx; 5285234b27c3SEric Dumazet int h, s_h; 5286234b27c3SEric Dumazet struct net_device *dev; 5287234b27c3SEric Dumazet struct inet6_dev *idev; 5288234b27c3SEric Dumazet struct hlist_head *head; 5289242afaa6SDavid Ahern int err = 0; 5290234b27c3SEric Dumazet 5291234b27c3SEric Dumazet s_h = cb->args[0]; 5292234b27c3SEric Dumazet s_idx = idx = cb->args[1]; 5293fe884c2bSDavid Ahern s_ip_idx = cb->args[2]; 5294234b27c3SEric Dumazet 5295ed6eff11SDavid Ahern if (cb->strict_check) { 5296ed6eff11SDavid Ahern err = inet6_valid_dump_ifaddr_req(nlh, &fillargs, &tgt_net, 52976371a71fSDavid Ahern skb->sk, cb); 5298ed6eff11SDavid Ahern if (err < 0) 5299242afaa6SDavid Ahern goto put_tgt_net; 53006371a71fSDavid Ahern 5301242afaa6SDavid Ahern err = 0; 53026371a71fSDavid Ahern if (fillargs.ifindex) { 53036371a71fSDavid Ahern dev = __dev_get_by_index(tgt_net, fillargs.ifindex); 5304242afaa6SDavid Ahern if (!dev) { 5305242afaa6SDavid Ahern err = -ENODEV; 5306242afaa6SDavid Ahern goto put_tgt_net; 5307242afaa6SDavid Ahern } 53086371a71fSDavid Ahern idev = __in6_dev_get(dev); 53096371a71fSDavid Ahern if (idev) { 53106371a71fSDavid Ahern err = in6_dump_addrs(idev, skb, cb, s_ip_idx, 53116371a71fSDavid Ahern &fillargs); 53121518039fSJakub Kicinski if (err > 0) 53131518039fSJakub Kicinski err = 0; 53146371a71fSDavid Ahern } 53156371a71fSDavid Ahern goto put_tgt_net; 53166371a71fSDavid Ahern } 53176ecf4c37SChristian Brauner } 53186ecf4c37SChristian Brauner 5319234b27c3SEric Dumazet rcu_read_lock(); 53206ecf4c37SChristian Brauner cb->seq = atomic_read(&tgt_net->ipv6.dev_addr_genid) ^ tgt_net->dev_base_seq; 5321234b27c3SEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 5322234b27c3SEric Dumazet idx = 0; 53236ecf4c37SChristian Brauner head = &tgt_net->dev_index_head[h]; 5324b67bfe0dSSasha Levin hlist_for_each_entry_rcu(dev, head, index_hlist) { 5325234b27c3SEric Dumazet if (idx < s_idx) 5326234b27c3SEric Dumazet goto cont; 53274b97efdfSPatrick McHardy if (h > s_h || idx > s_idx) 5328234b27c3SEric Dumazet s_ip_idx = 0; 5329e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 5330e21e8467SStephen Hemminger if (!idev) 5331234b27c3SEric Dumazet goto cont; 5332234b27c3SEric Dumazet 5333fe884c2bSDavid Ahern if (in6_dump_addrs(idev, skb, cb, s_ip_idx, 53346ba1e6e8SDavid Ahern &fillargs) < 0) 5335234b27c3SEric Dumazet goto done; 53367562f876SPavel Emelianov cont: 53377562f876SPavel Emelianov idx++; 53381da177e4SLinus Torvalds } 5339234b27c3SEric Dumazet } 5340234b27c3SEric Dumazet done: 5341234b27c3SEric Dumazet rcu_read_unlock(); 5342234b27c3SEric Dumazet cb->args[0] = h; 5343234b27c3SEric Dumazet cb->args[1] = idx; 53446371a71fSDavid Ahern put_tgt_net: 53456ba1e6e8SDavid Ahern if (fillargs.netnsid >= 0) 53466ecf4c37SChristian Brauner put_net(tgt_net); 5347234b27c3SEric Dumazet 53487c1e8a38SArthur Gautier return skb->len ? : err; 53491da177e4SLinus Torvalds } 53501da177e4SLinus Torvalds 53511da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) 53521da177e4SLinus Torvalds { 53531da177e4SLinus Torvalds enum addr_type_t type = UNICAST_ADDR; 5354b854272bSDenis V. Lunev 53551da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 53561da177e4SLinus Torvalds } 53571da177e4SLinus Torvalds 53581da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb) 53591da177e4SLinus Torvalds { 53601da177e4SLinus Torvalds enum addr_type_t type = MULTICAST_ADDR; 5361b854272bSDenis V. Lunev 53621da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 53631da177e4SLinus Torvalds } 53641da177e4SLinus Torvalds 53651da177e4SLinus Torvalds 53661da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb) 53671da177e4SLinus Torvalds { 53681da177e4SLinus Torvalds enum addr_type_t type = ANYCAST_ADDR; 5369b854272bSDenis V. Lunev 53701da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 53711da177e4SLinus Torvalds } 53721da177e4SLinus Torvalds 53734b1373deSJakub Kicinski static int inet6_rtm_valid_getaddr_req(struct sk_buff *skb, 53744b1373deSJakub Kicinski const struct nlmsghdr *nlh, 53754b1373deSJakub Kicinski struct nlattr **tb, 53764b1373deSJakub Kicinski struct netlink_ext_ack *extack) 53774b1373deSJakub Kicinski { 53784b1373deSJakub Kicinski struct ifaddrmsg *ifm; 53794b1373deSJakub Kicinski int i, err; 53804b1373deSJakub Kicinski 53814b1373deSJakub Kicinski if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 53824b1373deSJakub Kicinski NL_SET_ERR_MSG_MOD(extack, "Invalid header for get address request"); 53834b1373deSJakub Kicinski return -EINVAL; 53844b1373deSJakub Kicinski } 53854b1373deSJakub Kicinski 53862beb6d29SHangbin Liu if (!netlink_strict_get_check(skb)) 53872beb6d29SHangbin Liu return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX, 53882beb6d29SHangbin Liu ifa_ipv6_policy, extack); 53892beb6d29SHangbin Liu 53904b1373deSJakub Kicinski ifm = nlmsg_data(nlh); 53914b1373deSJakub Kicinski if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) { 53924b1373deSJakub Kicinski NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get address request"); 53934b1373deSJakub Kicinski return -EINVAL; 53944b1373deSJakub Kicinski } 53954b1373deSJakub Kicinski 53968cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX, 53974b1373deSJakub Kicinski ifa_ipv6_policy, extack); 53984b1373deSJakub Kicinski if (err) 53994b1373deSJakub Kicinski return err; 54004b1373deSJakub Kicinski 54014b1373deSJakub Kicinski for (i = 0; i <= IFA_MAX; i++) { 54024b1373deSJakub Kicinski if (!tb[i]) 54034b1373deSJakub Kicinski continue; 54044b1373deSJakub Kicinski 54054b1373deSJakub Kicinski switch (i) { 54064b1373deSJakub Kicinski case IFA_TARGET_NETNSID: 54074b1373deSJakub Kicinski case IFA_ADDRESS: 54084b1373deSJakub Kicinski case IFA_LOCAL: 54094b1373deSJakub Kicinski break; 54104b1373deSJakub Kicinski default: 54114b1373deSJakub Kicinski NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get address request"); 54124b1373deSJakub Kicinski return -EINVAL; 54134b1373deSJakub Kicinski } 54144b1373deSJakub Kicinski } 54154b1373deSJakub Kicinski 54164b1373deSJakub Kicinski return 0; 54174b1373deSJakub Kicinski } 54184b1373deSJakub Kicinski 5419c21ef3e3SDavid Ahern static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh, 5420c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 54216c223828SNoriaki TAKAMIYA { 542287117bafSRocco Yue struct net *tgt_net = sock_net(in_skb->sk); 5423203651b6SChristian Brauner struct inet6_fill_args fillargs = { 5424203651b6SChristian Brauner .portid = NETLINK_CB(in_skb).portid, 5425203651b6SChristian Brauner .seq = nlh->nlmsg_seq, 5426203651b6SChristian Brauner .event = RTM_NEWADDR, 5427203651b6SChristian Brauner .flags = 0, 5428203651b6SChristian Brauner .netnsid = -1, 5429203651b6SChristian Brauner }; 54301b29fc2cSThomas Graf struct ifaddrmsg *ifm; 54311b29fc2cSThomas Graf struct nlattr *tb[IFA_MAX+1]; 5432caeaba79SNicolas Dichtel struct in6_addr *addr = NULL, *peer; 54336c223828SNoriaki TAKAMIYA struct net_device *dev = NULL; 54346c223828SNoriaki TAKAMIYA struct inet6_ifaddr *ifa; 54356c223828SNoriaki TAKAMIYA struct sk_buff *skb; 54366c223828SNoriaki TAKAMIYA int err; 54376c223828SNoriaki TAKAMIYA 54384b1373deSJakub Kicinski err = inet6_rtm_valid_getaddr_req(in_skb, nlh, tb, extack); 54391b29fc2cSThomas Graf if (err < 0) 5440c24675f8SFlorian Westphal return err; 54416c223828SNoriaki TAKAMIYA 54426ecf4c37SChristian Brauner if (tb[IFA_TARGET_NETNSID]) { 5443203651b6SChristian Brauner fillargs.netnsid = nla_get_s32(tb[IFA_TARGET_NETNSID]); 54446ecf4c37SChristian Brauner 54456ecf4c37SChristian Brauner tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(in_skb).sk, 5446203651b6SChristian Brauner fillargs.netnsid); 54476ecf4c37SChristian Brauner if (IS_ERR(tgt_net)) 54486ecf4c37SChristian Brauner return PTR_ERR(tgt_net); 54496ecf4c37SChristian Brauner } 54506ecf4c37SChristian Brauner 5451caeaba79SNicolas Dichtel addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer); 5452c24675f8SFlorian Westphal if (!addr) 5453c24675f8SFlorian Westphal return -EINVAL; 54541b29fc2cSThomas Graf 54551b29fc2cSThomas Graf ifm = nlmsg_data(nlh); 54566c223828SNoriaki TAKAMIYA if (ifm->ifa_index) 54576ecf4c37SChristian Brauner dev = dev_get_by_index(tgt_net, ifm->ifa_index); 54586c223828SNoriaki TAKAMIYA 54596ecf4c37SChristian Brauner ifa = ipv6_get_ifaddr(tgt_net, addr, dev, 1); 5460e21e8467SStephen Hemminger if (!ifa) { 54611b29fc2cSThomas Graf err = -EADDRNOTAVAIL; 54621b29fc2cSThomas Graf goto errout; 54636c223828SNoriaki TAKAMIYA } 54646c223828SNoriaki TAKAMIYA 5465e21e8467SStephen Hemminger skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL); 5466e21e8467SStephen Hemminger if (!skb) { 54671b29fc2cSThomas Graf err = -ENOBUFS; 54681b29fc2cSThomas Graf goto errout_ifa; 54691b29fc2cSThomas Graf } 54701b29fc2cSThomas Graf 5471203651b6SChristian Brauner err = inet6_fill_ifaddr(skb, ifa, &fillargs); 547226932566SPatrick McHardy if (err < 0) { 547326932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 547426932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 547526932566SPatrick McHardy kfree_skb(skb); 547626932566SPatrick McHardy goto errout_ifa; 547726932566SPatrick McHardy } 54786ecf4c37SChristian Brauner err = rtnl_unicast(skb, tgt_net, NETLINK_CB(in_skb).portid); 54791b29fc2cSThomas Graf errout_ifa: 54806c223828SNoriaki TAKAMIYA in6_ifa_put(ifa); 54811b29fc2cSThomas Graf errout: 5482c24675f8SFlorian Westphal dev_put(dev); 5483203651b6SChristian Brauner if (fillargs.netnsid >= 0) 54846ecf4c37SChristian Brauner put_net(tgt_net); 54856ecf4c37SChristian Brauner 54866c223828SNoriaki TAKAMIYA return err; 54876c223828SNoriaki TAKAMIYA } 54886c223828SNoriaki TAKAMIYA 54891da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa) 54901da177e4SLinus Torvalds { 54911da177e4SLinus Torvalds struct sk_buff *skb; 5492c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(ifa->idev->dev); 5493203651b6SChristian Brauner struct inet6_fill_args fillargs = { 5494203651b6SChristian Brauner .portid = 0, 5495203651b6SChristian Brauner .seq = 0, 5496203651b6SChristian Brauner .event = event, 5497203651b6SChristian Brauner .flags = 0, 5498203651b6SChristian Brauner .netnsid = -1, 5499203651b6SChristian Brauner }; 55005d620266SThomas Graf int err = -ENOBUFS; 55011da177e4SLinus Torvalds 55020ab6803bSThomas Graf skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC); 550363159f29SIan Morris if (!skb) 55045d620266SThomas Graf goto errout; 55055d620266SThomas Graf 5506203651b6SChristian Brauner err = inet6_fill_ifaddr(skb, ifa, &fillargs); 550726932566SPatrick McHardy if (err < 0) { 550826932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 550926932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 551026932566SPatrick McHardy kfree_skb(skb); 551126932566SPatrick McHardy goto errout; 551226932566SPatrick McHardy } 55131ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC); 55141ce85fe4SPablo Neira Ayuso return; 55155d620266SThomas Graf errout: 55165d620266SThomas Graf if (err < 0) 55176fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err); 55181da177e4SLinus Torvalds } 55191da177e4SLinus Torvalds 5520b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf, 55211da177e4SLinus Torvalds __s32 *array, int bytes) 55221da177e4SLinus Torvalds { 552304561c1fSThomas Graf BUG_ON(bytes < (DEVCONF_MAX * 4)); 552404561c1fSThomas Graf 55251da177e4SLinus Torvalds memset(array, 0, bytes); 55261da177e4SLinus Torvalds array[DEVCONF_FORWARDING] = cnf->forwarding; 55271da177e4SLinus Torvalds array[DEVCONF_HOPLIMIT] = cnf->hop_limit; 55281da177e4SLinus Torvalds array[DEVCONF_MTU6] = cnf->mtu6; 55291da177e4SLinus Torvalds array[DEVCONF_ACCEPT_RA] = cnf->accept_ra; 55301da177e4SLinus Torvalds array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects; 55311da177e4SLinus Torvalds array[DEVCONF_AUTOCONF] = cnf->autoconf; 55321da177e4SLinus Torvalds array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits; 55331da177e4SLinus Torvalds array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits; 553493908d19SThomas Graf array[DEVCONF_RTR_SOLICIT_INTERVAL] = 553593908d19SThomas Graf jiffies_to_msecs(cnf->rtr_solicit_interval); 5536bd11f074SMaciej Żenczykowski array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] = 5537bd11f074SMaciej Żenczykowski jiffies_to_msecs(cnf->rtr_solicit_max_interval); 553893908d19SThomas Graf array[DEVCONF_RTR_SOLICIT_DELAY] = 553993908d19SThomas Graf jiffies_to_msecs(cnf->rtr_solicit_delay); 55401da177e4SLinus Torvalds array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version; 5541fc4eba58SHannes Frederic Sowa array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] = 5542fc4eba58SHannes Frederic Sowa jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval); 5543fc4eba58SHannes Frederic Sowa array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] = 5544fc4eba58SHannes Frederic Sowa jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval); 55451da177e4SLinus Torvalds array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr; 55461da177e4SLinus Torvalds array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft; 55471da177e4SLinus Torvalds array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft; 55481da177e4SLinus Torvalds array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry; 55491da177e4SLinus Torvalds array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor; 55501da177e4SLinus Torvalds array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses; 555165f5c7c1SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr; 55526b2e04bcSPraveen Chaudhary array[DEVCONF_RA_DEFRTR_METRIC] = cnf->ra_defrtr_metric; 55538013d1d7SHangbin Liu array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit; 5554c4fd30ebSYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo; 5555930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 5556930d6ff2SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref; 555793908d19SThomas Graf array[DEVCONF_RTR_PROBE_INTERVAL] = 555893908d19SThomas Graf jiffies_to_msecs(cnf->rtr_probe_interval); 5559fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 5560bbea124bSJoel Scherpelz array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] = cnf->accept_ra_rt_info_min_plen; 556109c884d4SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen; 556209c884d4SYOSHIFUJI Hideaki #endif 5563930d6ff2SYOSHIFUJI Hideaki #endif 5564fbea49e1SYOSHIFUJI Hideaki array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp; 55650bcbc926SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route; 556695c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 556795c385b4SNeil Horman array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad; 55687fd2561eSErik Kline array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic; 556995c385b4SNeil Horman #endif 55707bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 5571145c7a79SEric Dumazet array[DEVCONF_MC_FORWARDING] = atomic_read(&cnf->mc_forwarding); 55727bc570c8SYOSHIFUJI Hideaki #endif 5573778d80beSYOSHIFUJI Hideaki array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6; 55741b34be74SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad; 5575c3faca05SCosmin Ratiu array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao; 55765cb04436SHannes Frederic Sowa array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify; 5577b800c3b9SHannes Frederic Sowa array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc; 5578d9333196SBen Greear array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local; 5579c2943f14SHarout Hedeshian array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu; 558035103d11SAndy Gospodarek array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown; 55813d1bec99SHannes Frederic Sowa /* we omit DEVCONF_STABLE_SECRET for now */ 55823985e8a3SErik Kline array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only; 5583abbc3043SJohannes Berg array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast; 55847a02bf89SJohannes Berg array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na; 5585f1705ec1SDavid Ahern array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down; 55861ababebaSDavid Lebrun array[DEVCONF_SEG6_ENABLED] = cnf->seg6_enabled; 5587bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC 5588bf355b8dSDavid Lebrun array[DEVCONF_SEG6_REQUIRE_HMAC] = cnf->seg6_require_hmac; 5589bf355b8dSDavid Lebrun #endif 5590adc176c5SErik Nordmark array[DEVCONF_ENHANCED_DAD] = cnf->enhanced_dad; 5591d35a00b8SFelix Jia array[DEVCONF_ADDR_GEN_MODE] = cnf->addr_gen_mode; 5592df789fe7SDavid Forster array[DEVCONF_DISABLE_POLICY] = cnf->disable_policy; 55932210d6b2SMaciej Żenczykowski array[DEVCONF_NDISC_TCLASS] = cnf->ndisc_tclass; 55948610c7c6SAlexander Aring array[DEVCONF_RPL_SEG_ENABLED] = cnf->rpl_seg_enabled; 55959ee11f0fSJustin Iurman array[DEVCONF_IOAM6_ENABLED] = cnf->ioam6_enabled; 55969ee11f0fSJustin Iurman array[DEVCONF_IOAM6_ID] = cnf->ioam6_id; 55979ee11f0fSJustin Iurman array[DEVCONF_IOAM6_ID_WIDE] = cnf->ioam6_id_wide; 559818ac597aSJames Prestwood array[DEVCONF_NDISC_EVICT_NOCARRIER] = cnf->ndisc_evict_nocarrier; 55993e0b8f52SArun Ajith S array[DEVCONF_ACCEPT_UNTRACKED_NA] = cnf->accept_untracked_na; 56001da177e4SLinus Torvalds } 56011da177e4SLinus Torvalds 5602b382b191SThomas Graf static inline size_t inet6_ifla6_size(void) 5603b382b191SThomas Graf { 5604b382b191SThomas Graf return nla_total_size(4) /* IFLA_INET6_FLAGS */ 5605b382b191SThomas Graf + nla_total_size(sizeof(struct ifla_cacheinfo)) 5606b382b191SThomas Graf + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */ 5607b382b191SThomas Graf + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */ 5608f53adae4SDaniel Borkmann + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */ 5609bdd72f41SSabrina Dubroca + nla_total_size(sizeof(struct in6_addr)) /* IFLA_INET6_TOKEN */ 5610bdd72f41SSabrina Dubroca + nla_total_size(1) /* IFLA_INET6_ADDR_GEN_MODE */ 561149b99da2SRocco Yue + nla_total_size(4) /* IFLA_INET6_RA_MTU */ 5612bdd72f41SSabrina Dubroca + 0; 5613b382b191SThomas Graf } 5614b382b191SThomas Graf 5615339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void) 5616339bf98fSThomas Graf { 5617339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 5618339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 5619339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 5620339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 5621339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 56220344338bSAndy Gospodarek + nla_total_size(1) /* IFLA_OPERSTATE */ 5623b382b191SThomas Graf + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */ 5624339bf98fSThomas Graf } 5625c5396a31SYOSHIFUJI Hideaki 5626be281e55SEric Dumazet static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib, 5627aca05671SJia He int bytes) 56287f7d9a6bSHerbert Xu { 56297f7d9a6bSHerbert Xu int i; 5630aca05671SJia He int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX; 56317f7d9a6bSHerbert Xu BUG_ON(pad < 0); 56327f7d9a6bSHerbert Xu 56337f7d9a6bSHerbert Xu /* Use put_unaligned() because stats may not be aligned for u64. */ 5634aca05671SJia He put_unaligned(ICMP6_MIB_MAX, &stats[0]); 5635aca05671SJia He for (i = 1; i < ICMP6_MIB_MAX; i++) 5636be281e55SEric Dumazet put_unaligned(atomic_long_read(&mib[i]), &stats[i]); 56377f7d9a6bSHerbert Xu 5638aca05671SJia He memset(&stats[ICMP6_MIB_MAX], 0, pad); 56397f7d9a6bSHerbert Xu } 56407f7d9a6bSHerbert Xu 5641698365faSWANG Cong static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib, 5642a3a77372SRaghavendra K T int bytes, size_t syncpoff) 56434ce3c183SEric Dumazet { 5644a3a77372SRaghavendra K T int i, c; 5645a3a77372SRaghavendra K T u64 buff[IPSTATS_MIB_MAX]; 5646a3a77372SRaghavendra K T int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX; 5647a3a77372SRaghavendra K T 56484ce3c183SEric Dumazet BUG_ON(pad < 0); 56494ce3c183SEric Dumazet 5650a3a77372SRaghavendra K T memset(buff, 0, sizeof(buff)); 5651a3a77372SRaghavendra K T buff[0] = IPSTATS_MIB_MAX; 56524ce3c183SEric Dumazet 5653a3a77372SRaghavendra K T for_each_possible_cpu(c) { 5654a3a77372SRaghavendra K T for (i = 1; i < IPSTATS_MIB_MAX; i++) 5655a3a77372SRaghavendra K T buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff); 5656a3a77372SRaghavendra K T } 5657a3a77372SRaghavendra K T 5658a3a77372SRaghavendra K T memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64)); 5659a3a77372SRaghavendra K T memset(&stats[IPSTATS_MIB_MAX], 0, pad); 56604ce3c183SEric Dumazet } 56614ce3c183SEric Dumazet 56627f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype, 56637f7d9a6bSHerbert Xu int bytes) 56647f7d9a6bSHerbert Xu { 56657f7d9a6bSHerbert Xu switch (attrtype) { 56667f7d9a6bSHerbert Xu case IFLA_INET6_STATS: 5667a3a77372SRaghavendra K T __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes, 5668a3a77372SRaghavendra K T offsetof(struct ipstats_mib, syncp)); 56697f7d9a6bSHerbert Xu break; 56707f7d9a6bSHerbert Xu case IFLA_INET6_ICMP6STATS: 5671aca05671SJia He __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes); 56727f7d9a6bSHerbert Xu break; 56737f7d9a6bSHerbert Xu } 56747f7d9a6bSHerbert Xu } 56757f7d9a6bSHerbert Xu 5676d5566fd7SSowmini Varadhan static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev, 5677d5566fd7SSowmini Varadhan u32 ext_filter_mask) 56781da177e4SLinus Torvalds { 5679bf99f1bdSYOSHIFUJI Hideaki struct nlattr *nla; 56801da177e4SLinus Torvalds struct ifla_cacheinfo ci; 56811da177e4SLinus Torvalds 5682c78679e8SDavid S. Miller if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags)) 5683c78679e8SDavid S. Miller goto nla_put_failure; 56841da177e4SLinus Torvalds ci.max_reasm_len = IPV6_MAXPLEN; 568524912420SDavid S. Miller ci.tstamp = cstamp_delta(idev->tstamp); 568624912420SDavid S. Miller ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time); 56871f9248e5SJiri Pirko ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME)); 5688c78679e8SDavid S. Miller if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci)) 5689c78679e8SDavid S. Miller goto nla_put_failure; 5690bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32)); 569163159f29SIan Morris if (!nla) 569204561c1fSThomas Graf goto nla_put_failure; 5693bf99f1bdSYOSHIFUJI Hideaki ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla)); 56941da177e4SLinus Torvalds 5695bf99f1bdSYOSHIFUJI Hideaki /* XXX - MC not implemented */ 5696bf99f1bdSYOSHIFUJI Hideaki 5697d5566fd7SSowmini Varadhan if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS) 5698d5566fd7SSowmini Varadhan return 0; 5699d5566fd7SSowmini Varadhan 5700bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64)); 570163159f29SIan Morris if (!nla) 5702bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 5703bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla)); 5704bf99f1bdSYOSHIFUJI Hideaki 5705bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64)); 570663159f29SIan Morris if (!nla) 5707bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 5708bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla)); 57091da177e4SLinus Torvalds 5710f53adae4SDaniel Borkmann nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr)); 571163159f29SIan Morris if (!nla) 5712f53adae4SDaniel Borkmann goto nla_put_failure; 5713f53adae4SDaniel Borkmann read_lock_bh(&idev->lock); 5714f53adae4SDaniel Borkmann memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla)); 5715f53adae4SDaniel Borkmann read_unlock_bh(&idev->lock); 5716f53adae4SDaniel Borkmann 57170d7982ceSNicolas Dichtel if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->cnf.addr_gen_mode)) 57180d7982ceSNicolas Dichtel goto nla_put_failure; 57190d7982ceSNicolas Dichtel 572049b99da2SRocco Yue if (idev->ra_mtu && 572149b99da2SRocco Yue nla_put_u32(skb, IFLA_INET6_RA_MTU, idev->ra_mtu)) 572249b99da2SRocco Yue goto nla_put_failure; 572349b99da2SRocco Yue 5724b382b191SThomas Graf return 0; 5725b382b191SThomas Graf 5726b382b191SThomas Graf nla_put_failure: 5727b382b191SThomas Graf return -EMSGSIZE; 5728b382b191SThomas Graf } 5729b382b191SThomas Graf 5730b1974ed0SArad, Ronen static size_t inet6_get_link_af_size(const struct net_device *dev, 5731b1974ed0SArad, Ronen u32 ext_filter_mask) 5732b382b191SThomas Graf { 5733b382b191SThomas Graf if (!__in6_dev_get(dev)) 5734b382b191SThomas Graf return 0; 5735b382b191SThomas Graf 5736b382b191SThomas Graf return inet6_ifla6_size(); 5737b382b191SThomas Graf } 5738b382b191SThomas Graf 5739d5566fd7SSowmini Varadhan static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev, 5740d5566fd7SSowmini Varadhan u32 ext_filter_mask) 5741b382b191SThomas Graf { 5742b382b191SThomas Graf struct inet6_dev *idev = __in6_dev_get(dev); 5743b382b191SThomas Graf 5744b382b191SThomas Graf if (!idev) 5745b382b191SThomas Graf return -ENODATA; 5746b382b191SThomas Graf 5747d5566fd7SSowmini Varadhan if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0) 5748b382b191SThomas Graf return -EMSGSIZE; 5749b382b191SThomas Graf 5750b382b191SThomas Graf return 0; 5751b382b191SThomas Graf } 5752b382b191SThomas Graf 57533583a4e8SStephen Hemminger static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token, 57543583a4e8SStephen Hemminger struct netlink_ext_ack *extack) 5755f53adae4SDaniel Borkmann { 5756f53adae4SDaniel Borkmann struct inet6_ifaddr *ifp; 5757f53adae4SDaniel Borkmann struct net_device *dev = idev->dev; 575847e27d5eSDaniel Borkmann bool clear_token, update_rs = false; 5759dc848292SHannes Frederic Sowa struct in6_addr ll_addr; 5760f53adae4SDaniel Borkmann 5761c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 5762c15b1ccaSHannes Frederic Sowa 576363159f29SIan Morris if (!token) 5764f53adae4SDaniel Borkmann return -EINVAL; 57653583a4e8SStephen Hemminger 57663583a4e8SStephen Hemminger if (dev->flags & IFF_LOOPBACK) { 57673583a4e8SStephen Hemminger NL_SET_ERR_MSG_MOD(extack, "Device is loopback"); 5768f53adae4SDaniel Borkmann return -EINVAL; 57693583a4e8SStephen Hemminger } 57703583a4e8SStephen Hemminger 57713583a4e8SStephen Hemminger if (dev->flags & IFF_NOARP) { 57723583a4e8SStephen Hemminger NL_SET_ERR_MSG_MOD(extack, 57733583a4e8SStephen Hemminger "Device does not do neighbour discovery"); 5774f53adae4SDaniel Borkmann return -EINVAL; 57753583a4e8SStephen Hemminger } 57763583a4e8SStephen Hemminger 57773583a4e8SStephen Hemminger if (!ipv6_accept_ra(idev)) { 57783583a4e8SStephen Hemminger NL_SET_ERR_MSG_MOD(extack, 57793583a4e8SStephen Hemminger "Router advertisement is disabled on device"); 5780f53adae4SDaniel Borkmann return -EINVAL; 57813583a4e8SStephen Hemminger } 57823583a4e8SStephen Hemminger 57833583a4e8SStephen Hemminger if (idev->cnf.rtr_solicits == 0) { 57843583a4e8SStephen Hemminger NL_SET_ERR_MSG(extack, 57853583a4e8SStephen Hemminger "Router solicitation is disabled on device"); 57863583a4e8SStephen Hemminger return -EINVAL; 57873583a4e8SStephen Hemminger } 5788f53adae4SDaniel Borkmann 5789f53adae4SDaniel Borkmann write_lock_bh(&idev->lock); 5790f53adae4SDaniel Borkmann 5791f53adae4SDaniel Borkmann BUILD_BUG_ON(sizeof(token->s6_addr) != 16); 5792f53adae4SDaniel Borkmann memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8); 5793f53adae4SDaniel Borkmann 5794f53adae4SDaniel Borkmann write_unlock_bh(&idev->lock); 5795f53adae4SDaniel Borkmann 579647e27d5eSDaniel Borkmann clear_token = ipv6_addr_any(token); 579747e27d5eSDaniel Borkmann if (clear_token) 579847e27d5eSDaniel Borkmann goto update_lft; 579947e27d5eSDaniel Borkmann 5800dc848292SHannes Frederic Sowa if (!idev->dead && (idev->if_flags & IF_READY) && 5801dc848292SHannes Frederic Sowa !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE | 5802dc848292SHannes Frederic Sowa IFA_F_OPTIMISTIC)) { 5803fc403832SDaniel Borkmann /* If we're not ready, then normal ifup will take care 5804fc403832SDaniel Borkmann * of this. Otherwise, we need to request our rs here. 5805fc403832SDaniel Borkmann */ 5806f53adae4SDaniel Borkmann ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters); 5807fc403832SDaniel Borkmann update_rs = true; 5808fc403832SDaniel Borkmann } 5809f53adae4SDaniel Borkmann 581047e27d5eSDaniel Borkmann update_lft: 5811f53adae4SDaniel Borkmann write_lock_bh(&idev->lock); 5812fc403832SDaniel Borkmann 581377ecaaceSHannes Frederic Sowa if (update_rs) { 5814f53adae4SDaniel Borkmann idev->if_flags |= IF_RS_SENT; 5815bd11f074SMaciej Żenczykowski idev->rs_interval = rfc3315_s14_backoff_init( 5816bd11f074SMaciej Żenczykowski idev->cnf.rtr_solicit_interval); 581777ecaaceSHannes Frederic Sowa idev->rs_probes = 1; 5818bd11f074SMaciej Żenczykowski addrconf_mod_rs_timer(idev, idev->rs_interval); 581977ecaaceSHannes Frederic Sowa } 5820f53adae4SDaniel Borkmann 5821f53adae4SDaniel Borkmann /* Well, that's kinda nasty ... */ 5822f53adae4SDaniel Borkmann list_for_each_entry(ifp, &idev->addr_list, if_list) { 5823f53adae4SDaniel Borkmann spin_lock(&ifp->lock); 5824617fe29dSDaniel Borkmann if (ifp->tokenized) { 5825f53adae4SDaniel Borkmann ifp->valid_lft = 0; 5826f53adae4SDaniel Borkmann ifp->prefered_lft = 0; 5827f53adae4SDaniel Borkmann } 5828f53adae4SDaniel Borkmann spin_unlock(&ifp->lock); 5829f53adae4SDaniel Borkmann } 5830f53adae4SDaniel Borkmann 5831f53adae4SDaniel Borkmann write_unlock_bh(&idev->lock); 5832b2ed64a9SLubomir Rintel inet6_ifinfo_notify(RTM_NEWLINK, idev); 58338805d13fSEric Dumazet addrconf_verify_rtnl(dev_net(dev)); 5834f53adae4SDaniel Borkmann return 0; 5835f53adae4SDaniel Borkmann } 5836f53adae4SDaniel Borkmann 583711b1f828SDaniel Borkmann static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = { 583811b1f828SDaniel Borkmann [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 }, 583911b1f828SDaniel Borkmann [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) }, 584049b99da2SRocco Yue [IFLA_INET6_RA_MTU] = { .type = NLA_REJECT, 584149b99da2SRocco Yue .reject_message = 584249b99da2SRocco Yue "IFLA_INET6_RA_MTU can not be set" }, 584311b1f828SDaniel Borkmann }; 584411b1f828SDaniel Borkmann 5845d35a00b8SFelix Jia static int check_addr_gen_mode(int mode) 5846d35a00b8SFelix Jia { 5847d35a00b8SFelix Jia if (mode != IN6_ADDR_GEN_MODE_EUI64 && 5848d35a00b8SFelix Jia mode != IN6_ADDR_GEN_MODE_NONE && 5849d35a00b8SFelix Jia mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY && 5850d35a00b8SFelix Jia mode != IN6_ADDR_GEN_MODE_RANDOM) 5851d35a00b8SFelix Jia return -EINVAL; 5852d35a00b8SFelix Jia return 1; 5853d35a00b8SFelix Jia } 5854d35a00b8SFelix Jia 5855d35a00b8SFelix Jia static int check_stable_privacy(struct inet6_dev *idev, struct net *net, 5856d35a00b8SFelix Jia int mode) 5857d35a00b8SFelix Jia { 5858d35a00b8SFelix Jia if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY && 5859d35a00b8SFelix Jia !idev->cnf.stable_secret.initialized && 5860d35a00b8SFelix Jia !net->ipv6.devconf_dflt->stable_secret.initialized) 5861d35a00b8SFelix Jia return -EINVAL; 5862d35a00b8SFelix Jia return 1; 5863d35a00b8SFelix Jia } 5864d35a00b8SFelix Jia 58657dc2bccaSMaxim Mikityanskiy static int inet6_validate_link_af(const struct net_device *dev, 58668679c31eSRocco Yue const struct nlattr *nla, 58678679c31eSRocco Yue struct netlink_ext_ack *extack) 5868f53adae4SDaniel Borkmann { 5869f53adae4SDaniel Borkmann struct nlattr *tb[IFLA_INET6_MAX + 1]; 58707dc2bccaSMaxim Mikityanskiy struct inet6_dev *idev = NULL; 58717dc2bccaSMaxim Mikityanskiy int err; 5872f53adae4SDaniel Borkmann 58737dc2bccaSMaxim Mikityanskiy if (dev) { 58747dc2bccaSMaxim Mikityanskiy idev = __in6_dev_get(dev); 5875f53adae4SDaniel Borkmann if (!idev) 5876f53adae4SDaniel Borkmann return -EAFNOSUPPORT; 58777dc2bccaSMaxim Mikityanskiy } 58787dc2bccaSMaxim Mikityanskiy 58797dc2bccaSMaxim Mikityanskiy err = nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla, 58808679c31eSRocco Yue inet6_af_policy, extack); 58817dc2bccaSMaxim Mikityanskiy if (err) 58827dc2bccaSMaxim Mikityanskiy return err; 58837dc2bccaSMaxim Mikityanskiy 58847dc2bccaSMaxim Mikityanskiy if (!tb[IFLA_INET6_TOKEN] && !tb[IFLA_INET6_ADDR_GEN_MODE]) 58857dc2bccaSMaxim Mikityanskiy return -EINVAL; 58867dc2bccaSMaxim Mikityanskiy 58877dc2bccaSMaxim Mikityanskiy if (tb[IFLA_INET6_ADDR_GEN_MODE]) { 58887dc2bccaSMaxim Mikityanskiy u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]); 58897dc2bccaSMaxim Mikityanskiy 58907dc2bccaSMaxim Mikityanskiy if (check_addr_gen_mode(mode) < 0) 58917dc2bccaSMaxim Mikityanskiy return -EINVAL; 58927dc2bccaSMaxim Mikityanskiy if (dev && check_stable_privacy(idev, dev_net(dev), mode) < 0) 58937dc2bccaSMaxim Mikityanskiy return -EINVAL; 58947dc2bccaSMaxim Mikityanskiy } 58957dc2bccaSMaxim Mikityanskiy 58967dc2bccaSMaxim Mikityanskiy return 0; 58977dc2bccaSMaxim Mikityanskiy } 58987dc2bccaSMaxim Mikityanskiy 58993583a4e8SStephen Hemminger static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla, 59003583a4e8SStephen Hemminger struct netlink_ext_ack *extack) 59017dc2bccaSMaxim Mikityanskiy { 59027dc2bccaSMaxim Mikityanskiy struct inet6_dev *idev = __in6_dev_get(dev); 59037dc2bccaSMaxim Mikityanskiy struct nlattr *tb[IFLA_INET6_MAX + 1]; 59047dc2bccaSMaxim Mikityanskiy int err; 5905f53adae4SDaniel Borkmann 5906db3fa271SEric Dumazet if (!idev) 5907db3fa271SEric Dumazet return -EAFNOSUPPORT; 5908db3fa271SEric Dumazet 59098cb08174SJohannes Berg if (nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla, NULL, NULL) < 0) 59105ac6b198SZheng Yongjun return -EINVAL; 5911f53adae4SDaniel Borkmann 5912bc91b0f0SJiri Pirko if (tb[IFLA_INET6_TOKEN]) { 59133583a4e8SStephen Hemminger err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]), 59143583a4e8SStephen Hemminger extack); 5915bc91b0f0SJiri Pirko if (err) 5916bc91b0f0SJiri Pirko return err; 5917bc91b0f0SJiri Pirko } 5918bc91b0f0SJiri Pirko 5919bc91b0f0SJiri Pirko if (tb[IFLA_INET6_ADDR_GEN_MODE]) { 5920bc91b0f0SJiri Pirko u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]); 5921bc91b0f0SJiri Pirko 5922d35a00b8SFelix Jia idev->cnf.addr_gen_mode = mode; 5923bc91b0f0SJiri Pirko } 5924f53adae4SDaniel Borkmann 59257dc2bccaSMaxim Mikityanskiy return 0; 5926f53adae4SDaniel Borkmann } 5927f53adae4SDaniel Borkmann 5928b382b191SThomas Graf static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev, 592915e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 5930b382b191SThomas Graf { 5931b382b191SThomas Graf struct net_device *dev = idev->dev; 5932b382b191SThomas Graf struct ifinfomsg *hdr; 5933b382b191SThomas Graf struct nlmsghdr *nlh; 5934b382b191SThomas Graf void *protoinfo; 5935b382b191SThomas Graf 593615e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags); 593763159f29SIan Morris if (!nlh) 5938b382b191SThomas Graf return -EMSGSIZE; 5939b382b191SThomas Graf 5940b382b191SThomas Graf hdr = nlmsg_data(nlh); 5941b382b191SThomas Graf hdr->ifi_family = AF_INET6; 5942b382b191SThomas Graf hdr->__ifi_pad = 0; 5943b382b191SThomas Graf hdr->ifi_type = dev->type; 5944b382b191SThomas Graf hdr->ifi_index = dev->ifindex; 5945b382b191SThomas Graf hdr->ifi_flags = dev_get_flags(dev); 5946b382b191SThomas Graf hdr->ifi_change = 0; 5947b382b191SThomas Graf 5948c78679e8SDavid S. Miller if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 5949c78679e8SDavid S. Miller (dev->addr_len && 5950c78679e8SDavid S. Miller nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 5951c78679e8SDavid S. Miller nla_put_u32(skb, IFLA_MTU, dev->mtu) || 5952a54acb3aSNicolas Dichtel (dev->ifindex != dev_get_iflink(dev) && 59530344338bSAndy Gospodarek nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) || 59540344338bSAndy Gospodarek nla_put_u8(skb, IFLA_OPERSTATE, 59550344338bSAndy Gospodarek netif_running(dev) ? dev->operstate : IF_OPER_DOWN)) 5956c78679e8SDavid S. Miller goto nla_put_failure; 5957ae0be8deSMichal Kubecek protoinfo = nla_nest_start_noflag(skb, IFLA_PROTINFO); 595863159f29SIan Morris if (!protoinfo) 5959b382b191SThomas Graf goto nla_put_failure; 5960b382b191SThomas Graf 5961d5566fd7SSowmini Varadhan if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0) 5962b382b191SThomas Graf goto nla_put_failure; 5963b382b191SThomas Graf 596404561c1fSThomas Graf nla_nest_end(skb, protoinfo); 5965053c095aSJohannes Berg nlmsg_end(skb, nlh); 5966053c095aSJohannes Berg return 0; 59671da177e4SLinus Torvalds 596804561c1fSThomas Graf nla_put_failure: 596926932566SPatrick McHardy nlmsg_cancel(skb, nlh); 597026932566SPatrick McHardy return -EMSGSIZE; 59711da177e4SLinus Torvalds } 59721da177e4SLinus Torvalds 5973786e0007SDavid Ahern static int inet6_valid_dump_ifinfo(const struct nlmsghdr *nlh, 5974786e0007SDavid Ahern struct netlink_ext_ack *extack) 5975786e0007SDavid Ahern { 5976786e0007SDavid Ahern struct ifinfomsg *ifm; 5977786e0007SDavid Ahern 5978786e0007SDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 5979786e0007SDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Invalid header for link dump request"); 5980786e0007SDavid Ahern return -EINVAL; 5981786e0007SDavid Ahern } 5982786e0007SDavid Ahern 5983786e0007SDavid Ahern if (nlmsg_attrlen(nlh, sizeof(*ifm))) { 5984786e0007SDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Invalid data after header"); 5985786e0007SDavid Ahern return -EINVAL; 5986786e0007SDavid Ahern } 5987786e0007SDavid Ahern 5988786e0007SDavid Ahern ifm = nlmsg_data(nlh); 5989786e0007SDavid Ahern if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 5990786e0007SDavid Ahern ifm->ifi_change || ifm->ifi_index) { 5991786e0007SDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for dump request"); 5992786e0007SDavid Ahern return -EINVAL; 5993786e0007SDavid Ahern } 5994786e0007SDavid Ahern 5995786e0007SDavid Ahern return 0; 5996786e0007SDavid Ahern } 5997786e0007SDavid Ahern 59981da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 59991da177e4SLinus Torvalds { 60003b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 600184d2697dSEric Dumazet int h, s_h; 6002434a8a58SDavid S. Miller int idx = 0, s_idx; 60031da177e4SLinus Torvalds struct net_device *dev; 60041da177e4SLinus Torvalds struct inet6_dev *idev; 600584d2697dSEric Dumazet struct hlist_head *head; 60061da177e4SLinus Torvalds 6007786e0007SDavid Ahern /* only requests using strict checking can pass data to 6008786e0007SDavid Ahern * influence the dump 6009786e0007SDavid Ahern */ 6010786e0007SDavid Ahern if (cb->strict_check) { 6011786e0007SDavid Ahern int err = inet6_valid_dump_ifinfo(cb->nlh, cb->extack); 6012786e0007SDavid Ahern 6013786e0007SDavid Ahern if (err < 0) 6014786e0007SDavid Ahern return err; 6015786e0007SDavid Ahern } 6016786e0007SDavid Ahern 601784d2697dSEric Dumazet s_h = cb->args[0]; 601884d2697dSEric Dumazet s_idx = cb->args[1]; 601984d2697dSEric Dumazet 602084d2697dSEric Dumazet rcu_read_lock(); 602184d2697dSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 60227562f876SPavel Emelianov idx = 0; 602384d2697dSEric Dumazet head = &net->dev_index_head[h]; 6024b67bfe0dSSasha Levin hlist_for_each_entry_rcu(dev, head, index_hlist) { 60251da177e4SLinus Torvalds if (idx < s_idx) 60267562f876SPavel Emelianov goto cont; 602784d2697dSEric Dumazet idev = __in6_dev_get(dev); 602884d2697dSEric Dumazet if (!idev) 60297562f876SPavel Emelianov goto cont; 603084d2697dSEric Dumazet if (inet6_fill_ifinfo(skb, idev, 603115e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 603284d2697dSEric Dumazet cb->nlh->nlmsg_seq, 6033053c095aSJohannes Berg RTM_NEWLINK, NLM_F_MULTI) < 0) 603484d2697dSEric Dumazet goto out; 60357562f876SPavel Emelianov cont: 60367562f876SPavel Emelianov idx++; 60371da177e4SLinus Torvalds } 603884d2697dSEric Dumazet } 603984d2697dSEric Dumazet out: 604084d2697dSEric Dumazet rcu_read_unlock(); 604184d2697dSEric Dumazet cb->args[1] = idx; 604284d2697dSEric Dumazet cb->args[0] = h; 60431da177e4SLinus Torvalds 60441da177e4SLinus Torvalds return skb->len; 60451da177e4SLinus Torvalds } 60461da177e4SLinus Torvalds 60471da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev) 60481da177e4SLinus Torvalds { 60491da177e4SLinus Torvalds struct sk_buff *skb; 6050c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 60518d7a76c9SThomas Graf int err = -ENOBUFS; 60521da177e4SLinus Torvalds 6053339bf98fSThomas Graf skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC); 605463159f29SIan Morris if (!skb) 60558d7a76c9SThomas Graf goto errout; 60568d7a76c9SThomas Graf 60578d7a76c9SThomas Graf err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0); 605826932566SPatrick McHardy if (err < 0) { 605926932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */ 606026932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 606126932566SPatrick McHardy kfree_skb(skb); 606226932566SPatrick McHardy goto errout; 606326932566SPatrick McHardy } 60645f75a104SNicolas Dichtel rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC); 60651ce85fe4SPablo Neira Ayuso return; 60668d7a76c9SThomas Graf errout: 60678d7a76c9SThomas Graf if (err < 0) 60685f75a104SNicolas Dichtel rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err); 60691da177e4SLinus Torvalds } 60701da177e4SLinus Torvalds 6071339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void) 6072339bf98fSThomas Graf { 6073339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct prefixmsg)) 6074339bf98fSThomas Graf + nla_total_size(sizeof(struct in6_addr)) 6075339bf98fSThomas Graf + nla_total_size(sizeof(struct prefix_cacheinfo)); 6076339bf98fSThomas Graf } 6077c5396a31SYOSHIFUJI Hideaki 60781da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev, 607915e47304SEric W. Biederman struct prefix_info *pinfo, u32 portid, u32 seq, 6080b6544c0bSJamal Hadi Salim int event, unsigned int flags) 60811da177e4SLinus Torvalds { 60821da177e4SLinus Torvalds struct prefixmsg *pmsg; 60831da177e4SLinus Torvalds struct nlmsghdr *nlh; 60841da177e4SLinus Torvalds struct prefix_cacheinfo ci; 60851da177e4SLinus Torvalds 608615e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags); 608763159f29SIan Morris if (!nlh) 608826932566SPatrick McHardy return -EMSGSIZE; 60896051e2f4SThomas Graf 60906051e2f4SThomas Graf pmsg = nlmsg_data(nlh); 60911da177e4SLinus Torvalds pmsg->prefix_family = AF_INET6; 60928a47077aSPatrick McHardy pmsg->prefix_pad1 = 0; 60938a47077aSPatrick McHardy pmsg->prefix_pad2 = 0; 60941da177e4SLinus Torvalds pmsg->prefix_ifindex = idev->dev->ifindex; 60951da177e4SLinus Torvalds pmsg->prefix_len = pinfo->prefix_len; 60961da177e4SLinus Torvalds pmsg->prefix_type = pinfo->type; 60978a47077aSPatrick McHardy pmsg->prefix_pad3 = 0; 60981da177e4SLinus Torvalds pmsg->prefix_flags = 0; 60991da177e4SLinus Torvalds if (pinfo->onlink) 61001da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_ONLINK; 61011da177e4SLinus Torvalds if (pinfo->autoconf) 61021da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_AUTOCONF; 61031da177e4SLinus Torvalds 6104c78679e8SDavid S. Miller if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix)) 6105c78679e8SDavid S. Miller goto nla_put_failure; 61061da177e4SLinus Torvalds ci.preferred_time = ntohl(pinfo->prefered); 61071da177e4SLinus Torvalds ci.valid_time = ntohl(pinfo->valid); 6108c78679e8SDavid S. Miller if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci)) 6109c78679e8SDavid S. Miller goto nla_put_failure; 6110053c095aSJohannes Berg nlmsg_end(skb, nlh); 6111053c095aSJohannes Berg return 0; 61121da177e4SLinus Torvalds 61136051e2f4SThomas Graf nla_put_failure: 611426932566SPatrick McHardy nlmsg_cancel(skb, nlh); 611526932566SPatrick McHardy return -EMSGSIZE; 61161da177e4SLinus Torvalds } 61171da177e4SLinus Torvalds 61181da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev, 61191da177e4SLinus Torvalds struct prefix_info *pinfo) 61201da177e4SLinus Torvalds { 61211da177e4SLinus Torvalds struct sk_buff *skb; 6122c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 61238c384bfaSThomas Graf int err = -ENOBUFS; 61241da177e4SLinus Torvalds 6125339bf98fSThomas Graf skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC); 612663159f29SIan Morris if (!skb) 61278c384bfaSThomas Graf goto errout; 61288c384bfaSThomas Graf 61298c384bfaSThomas Graf err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0); 613026932566SPatrick McHardy if (err < 0) { 613126932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */ 613226932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 613326932566SPatrick McHardy kfree_skb(skb); 613426932566SPatrick McHardy goto errout; 613526932566SPatrick McHardy } 61361ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC); 61371ce85fe4SPablo Neira Ayuso return; 61388c384bfaSThomas Graf errout: 61398c384bfaSThomas Graf if (err < 0) 61406fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err); 61411da177e4SLinus Torvalds } 61421da177e4SLinus Torvalds 61431da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 61441da177e4SLinus Torvalds { 614563998ac2SNicolas Dichtel struct net *net = dev_net(ifp->idev->dev); 614663998ac2SNicolas Dichtel 6147c15b1ccaSHannes Frederic Sowa if (event) 6148c15b1ccaSHannes Frederic Sowa ASSERT_RTNL(); 6149c15b1ccaSHannes Frederic Sowa 61501da177e4SLinus Torvalds inet6_ifa_notify(event ? : RTM_NEWADDR, ifp); 61511da177e4SLinus Torvalds 61521da177e4SLinus Torvalds switch (event) { 61531da177e4SLinus Torvalds case RTM_NEWADDR: 615495c385b4SNeil Horman /* 61552d819d25SDavid Ahern * If the address was optimistic we inserted the route at the 61562d819d25SDavid Ahern * start of our DAD process, so we don't need to do it again. 61572d819d25SDavid Ahern * If the device was taken down in the middle of the DAD 61582d819d25SDavid Ahern * cycle there is a race where we could get here without a 61592d819d25SDavid Ahern * host route, so nothing to insert. That will be fixed when 61602d819d25SDavid Ahern * the device is brought up. 616195c385b4SNeil Horman */ 61622d819d25SDavid Ahern if (ifp->rt && !rcu_access_pointer(ifp->rt->fib6_node)) { 6163afb1d4b5SDavid Ahern ip6_ins_rt(net, ifp->rt); 61642d819d25SDavid Ahern } else if (!ifp->rt && (ifp->idev->dev->flags & IFF_UP)) { 61652d819d25SDavid Ahern pr_warn("BUG: Address %pI6c on device %s is missing its host route.\n", 61662d819d25SDavid Ahern &ifp->addr, ifp->idev->dev->name); 61672d819d25SDavid Ahern } 61682d819d25SDavid Ahern 61691da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 61701da177e4SLinus Torvalds addrconf_join_anycast(ifp); 61717996c799SCong Wang if (!ipv6_addr_any(&ifp->peer_addr)) 617207758eb9SHangbin Liu addrconf_prefix_route(&ifp->peer_addr, 128, 617307758eb9SHangbin Liu ifp->rt_priority, ifp->idev->dev, 617407758eb9SHangbin Liu 0, 0, GFP_ATOMIC); 61751da177e4SLinus Torvalds break; 61761da177e4SLinus Torvalds case RTM_DELADDR: 61771da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 61781da177e4SLinus Torvalds addrconf_leave_anycast(ifp); 61791da177e4SLinus Torvalds addrconf_leave_solict(ifp->idev, &ifp->addr); 61807996c799SCong Wang if (!ipv6_addr_any(&ifp->peer_addr)) { 61818d1c802bSDavid Ahern struct fib6_info *rt; 6182caeaba79SNicolas Dichtel 6183e7478dfcSNicolas Dichtel rt = addrconf_get_prefix_route(&ifp->peer_addr, 128, 61842b2450caSDavid Ahern ifp->idev->dev, 0, 0, 61852b2450caSDavid Ahern false); 61868e3d5be7SMartin KaFai Lau if (rt) 618711dd74b3SRoopa Prabhu ip6_del_rt(net, rt, false); 6188caeaba79SNicolas Dichtel } 618938bd10c4SDavid Ahern if (ifp->rt) { 619011dd74b3SRoopa Prabhu ip6_del_rt(net, ifp->rt, false); 619193531c67SDavid Ahern ifp->rt = NULL; 619238bd10c4SDavid Ahern } 6193705f1c86SHannes Frederic Sowa rt_genid_bump_ipv6(net); 61941da177e4SLinus Torvalds break; 61951da177e4SLinus Torvalds } 619663998ac2SNicolas Dichtel atomic_inc(&net->ipv6.dev_addr_genid); 61971da177e4SLinus Torvalds } 61981da177e4SLinus Torvalds 61991da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 62001da177e4SLinus Torvalds { 62011da177e4SLinus Torvalds if (likely(ifp->idev->dead == 0)) 62021da177e4SLinus Torvalds __ipv6_ifa_notify(event, ifp); 62031da177e4SLinus Torvalds } 62041da177e4SLinus Torvalds 62051da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 62061da177e4SLinus Torvalds 620732927393SChristoph Hellwig static int addrconf_sysctl_forward(struct ctl_table *ctl, int write, 620832927393SChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos) 62091da177e4SLinus Torvalds { 62101da177e4SLinus Torvalds int *valp = ctl->data; 62111da177e4SLinus Torvalds int val = *valp; 621288af182eSEric W. Biederman loff_t pos = *ppos; 6213fe2c6338SJoe Perches struct ctl_table lctl; 62141da177e4SLinus Torvalds int ret; 62151da177e4SLinus Torvalds 6216013d97e9SFrancesco Ruggeri /* 6217013d97e9SFrancesco Ruggeri * ctl->data points to idev->cnf.forwarding, we should 6218013d97e9SFrancesco Ruggeri * not modify it until we get the rtnl lock. 6219013d97e9SFrancesco Ruggeri */ 6220013d97e9SFrancesco Ruggeri lctl = *ctl; 6221013d97e9SFrancesco Ruggeri lctl.data = &val; 6222013d97e9SFrancesco Ruggeri 6223013d97e9SFrancesco Ruggeri ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 62241da177e4SLinus Torvalds 6225c8fecf22SPavel Emelyanov if (write) 6226b325fddbSStephen Hemminger ret = addrconf_fixup_forwarding(ctl, valp, val); 622788af182eSEric W. Biederman if (ret) 622888af182eSEric W. Biederman *ppos = pos; 62291da177e4SLinus Torvalds return ret; 62301da177e4SLinus Torvalds } 62311da177e4SLinus Torvalds 623232927393SChristoph Hellwig static int addrconf_sysctl_mtu(struct ctl_table *ctl, int write, 623332927393SChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos) 623477751427SMarcelo Leitner { 623577751427SMarcelo Leitner struct inet6_dev *idev = ctl->extra1; 623677751427SMarcelo Leitner int min_mtu = IPV6_MIN_MTU; 623777751427SMarcelo Leitner struct ctl_table lctl; 623877751427SMarcelo Leitner 623977751427SMarcelo Leitner lctl = *ctl; 624077751427SMarcelo Leitner lctl.extra1 = &min_mtu; 624177751427SMarcelo Leitner lctl.extra2 = idev ? &idev->dev->mtu : NULL; 624277751427SMarcelo Leitner 624377751427SMarcelo Leitner return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos); 624477751427SMarcelo Leitner } 624577751427SMarcelo Leitner 624656d417b1SBrian Haley static void dev_disable_change(struct inet6_dev *idev) 624756d417b1SBrian Haley { 624875538c2bSCong Wang struct netdev_notifier_info info; 624975538c2bSCong Wang 625056d417b1SBrian Haley if (!idev || !idev->dev) 625156d417b1SBrian Haley return; 625256d417b1SBrian Haley 625375538c2bSCong Wang netdev_notifier_info_init(&info, idev->dev); 625456d417b1SBrian Haley if (idev->cnf.disable_ipv6) 625575538c2bSCong Wang addrconf_notify(NULL, NETDEV_DOWN, &info); 625656d417b1SBrian Haley else 625775538c2bSCong Wang addrconf_notify(NULL, NETDEV_UP, &info); 625856d417b1SBrian Haley } 625956d417b1SBrian Haley 626056d417b1SBrian Haley static void addrconf_disable_change(struct net *net, __s32 newf) 626156d417b1SBrian Haley { 626256d417b1SBrian Haley struct net_device *dev; 626356d417b1SBrian Haley struct inet6_dev *idev; 626456d417b1SBrian Haley 626503e4deffSKefeng Wang for_each_netdev(net, dev) { 626656d417b1SBrian Haley idev = __in6_dev_get(dev); 626756d417b1SBrian Haley if (idev) { 626856d417b1SBrian Haley int changed = (!idev->cnf.disable_ipv6) ^ (!newf); 626956d417b1SBrian Haley idev->cnf.disable_ipv6 = newf; 627056d417b1SBrian Haley if (changed) 627156d417b1SBrian Haley dev_disable_change(idev); 627256d417b1SBrian Haley } 627356d417b1SBrian Haley } 627456d417b1SBrian Haley } 627556d417b1SBrian Haley 6276013d97e9SFrancesco Ruggeri static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf) 627756d417b1SBrian Haley { 627856d417b1SBrian Haley struct net *net; 6279013d97e9SFrancesco Ruggeri int old; 6280013d97e9SFrancesco Ruggeri 6281013d97e9SFrancesco Ruggeri if (!rtnl_trylock()) 6282013d97e9SFrancesco Ruggeri return restart_syscall(); 628356d417b1SBrian Haley 628456d417b1SBrian Haley net = (struct net *)table->extra2; 6285013d97e9SFrancesco Ruggeri old = *p; 6286013d97e9SFrancesco Ruggeri *p = newf; 628756d417b1SBrian Haley 6288013d97e9SFrancesco Ruggeri if (p == &net->ipv6.devconf_dflt->disable_ipv6) { 6289013d97e9SFrancesco Ruggeri rtnl_unlock(); 629056d417b1SBrian Haley return 0; 629188af182eSEric W. Biederman } 629256d417b1SBrian Haley 629356d417b1SBrian Haley if (p == &net->ipv6.devconf_all->disable_ipv6) { 629456d417b1SBrian Haley net->ipv6.devconf_dflt->disable_ipv6 = newf; 629556d417b1SBrian Haley addrconf_disable_change(net, newf); 6296013d97e9SFrancesco Ruggeri } else if ((!newf) ^ (!old)) 629756d417b1SBrian Haley dev_disable_change((struct inet6_dev *)table->extra1); 629856d417b1SBrian Haley 629956d417b1SBrian Haley rtnl_unlock(); 630056d417b1SBrian Haley return 0; 630156d417b1SBrian Haley } 630256d417b1SBrian Haley 630332927393SChristoph Hellwig static int addrconf_sysctl_disable(struct ctl_table *ctl, int write, 630432927393SChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos) 630556d417b1SBrian Haley { 630656d417b1SBrian Haley int *valp = ctl->data; 630756d417b1SBrian Haley int val = *valp; 630888af182eSEric W. Biederman loff_t pos = *ppos; 6309fe2c6338SJoe Perches struct ctl_table lctl; 631056d417b1SBrian Haley int ret; 631156d417b1SBrian Haley 6312013d97e9SFrancesco Ruggeri /* 6313013d97e9SFrancesco Ruggeri * ctl->data points to idev->cnf.disable_ipv6, we should 6314013d97e9SFrancesco Ruggeri * not modify it until we get the rtnl lock. 6315013d97e9SFrancesco Ruggeri */ 6316013d97e9SFrancesco Ruggeri lctl = *ctl; 6317013d97e9SFrancesco Ruggeri lctl.data = &val; 6318013d97e9SFrancesco Ruggeri 6319013d97e9SFrancesco Ruggeri ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 632056d417b1SBrian Haley 632156d417b1SBrian Haley if (write) 632256d417b1SBrian Haley ret = addrconf_disable_ipv6(ctl, valp, val); 632388af182eSEric W. Biederman if (ret) 632488af182eSEric W. Biederman *ppos = pos; 632556d417b1SBrian Haley return ret; 632656d417b1SBrian Haley } 632756d417b1SBrian Haley 632832927393SChristoph Hellwig static int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write, 632932927393SChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos) 6330c92d5491Sstephen hemminger { 6331c92d5491Sstephen hemminger int *valp = ctl->data; 6332c92d5491Sstephen hemminger int ret; 6333c92d5491Sstephen hemminger int old, new; 6334c92d5491Sstephen hemminger 6335c92d5491Sstephen hemminger old = *valp; 6336c92d5491Sstephen hemminger ret = proc_dointvec(ctl, write, buffer, lenp, ppos); 6337c92d5491Sstephen hemminger new = *valp; 6338c92d5491Sstephen hemminger 6339c92d5491Sstephen hemminger if (write && old != new) { 6340c92d5491Sstephen hemminger struct net *net = ctl->extra2; 6341c92d5491Sstephen hemminger 6342c92d5491Sstephen hemminger if (!rtnl_trylock()) 6343c92d5491Sstephen hemminger return restart_syscall(); 6344c92d5491Sstephen hemminger 6345c92d5491Sstephen hemminger if (valp == &net->ipv6.devconf_dflt->proxy_ndp) 634685b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 634785b3daadSDavid Ahern NETCONFA_PROXY_NEIGH, 6348c92d5491Sstephen hemminger NETCONFA_IFINDEX_DEFAULT, 6349c92d5491Sstephen hemminger net->ipv6.devconf_dflt); 6350c92d5491Sstephen hemminger else if (valp == &net->ipv6.devconf_all->proxy_ndp) 635185b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 635285b3daadSDavid Ahern NETCONFA_PROXY_NEIGH, 6353c92d5491Sstephen hemminger NETCONFA_IFINDEX_ALL, 6354c92d5491Sstephen hemminger net->ipv6.devconf_all); 6355c92d5491Sstephen hemminger else { 6356c92d5491Sstephen hemminger struct inet6_dev *idev = ctl->extra1; 6357c92d5491Sstephen hemminger 635885b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 635985b3daadSDavid Ahern NETCONFA_PROXY_NEIGH, 6360c92d5491Sstephen hemminger idev->dev->ifindex, 6361c92d5491Sstephen hemminger &idev->cnf); 6362c92d5491Sstephen hemminger } 6363c92d5491Sstephen hemminger rtnl_unlock(); 6364c92d5491Sstephen hemminger } 6365c92d5491Sstephen hemminger 6366c92d5491Sstephen hemminger return ret; 6367c92d5491Sstephen hemminger } 6368c92d5491Sstephen hemminger 6369d35a00b8SFelix Jia static int addrconf_sysctl_addr_gen_mode(struct ctl_table *ctl, int write, 637032927393SChristoph Hellwig void *buffer, size_t *lenp, 6371d35a00b8SFelix Jia loff_t *ppos) 6372d35a00b8SFelix Jia { 6373d35a00b8SFelix Jia int ret = 0; 6374c6dbf7aaSSabrina Dubroca u32 new_val; 6375d35a00b8SFelix Jia struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1; 6376d35a00b8SFelix Jia struct net *net = (struct net *)ctl->extra2; 6377c6dbf7aaSSabrina Dubroca struct ctl_table tmp = { 6378c6dbf7aaSSabrina Dubroca .data = &new_val, 6379c6dbf7aaSSabrina Dubroca .maxlen = sizeof(new_val), 6380c6dbf7aaSSabrina Dubroca .mode = ctl->mode, 6381c6dbf7aaSSabrina Dubroca }; 6382d35a00b8SFelix Jia 63838c171d6cSFelix Jia if (!rtnl_trylock()) 63848c171d6cSFelix Jia return restart_syscall(); 63858c171d6cSFelix Jia 6386c6dbf7aaSSabrina Dubroca new_val = *((u32 *)ctl->data); 6387c6dbf7aaSSabrina Dubroca 6388c6dbf7aaSSabrina Dubroca ret = proc_douintvec(&tmp, write, buffer, lenp, ppos); 6389c6dbf7aaSSabrina Dubroca if (ret != 0) 6390c6dbf7aaSSabrina Dubroca goto out; 6391d35a00b8SFelix Jia 6392d35a00b8SFelix Jia if (write) { 63938c171d6cSFelix Jia if (check_addr_gen_mode(new_val) < 0) { 63948c171d6cSFelix Jia ret = -EINVAL; 63958c171d6cSFelix Jia goto out; 63968c171d6cSFelix Jia } 6397d35a00b8SFelix Jia 6398c6dbf7aaSSabrina Dubroca if (idev) { 63998c171d6cSFelix Jia if (check_stable_privacy(idev, net, new_val) < 0) { 64008c171d6cSFelix Jia ret = -EINVAL; 64018c171d6cSFelix Jia goto out; 64028c171d6cSFelix Jia } 6403d35a00b8SFelix Jia 6404d35a00b8SFelix Jia if (idev->cnf.addr_gen_mode != new_val) { 6405d35a00b8SFelix Jia idev->cnf.addr_gen_mode = new_val; 640623ca0c2cSThomas Winter addrconf_init_auto_addrs(idev->dev); 64078c171d6cSFelix Jia } 6408f24c5987SSabrina Dubroca } else if (&net->ipv6.devconf_all->addr_gen_mode == ctl->data) { 6409f24c5987SSabrina Dubroca struct net_device *dev; 6410f24c5987SSabrina Dubroca 6411f24c5987SSabrina Dubroca net->ipv6.devconf_dflt->addr_gen_mode = new_val; 6412f24c5987SSabrina Dubroca for_each_netdev(net, dev) { 6413f24c5987SSabrina Dubroca idev = __in6_dev_get(dev); 6414f24c5987SSabrina Dubroca if (idev && 6415f24c5987SSabrina Dubroca idev->cnf.addr_gen_mode != new_val) { 6416f24c5987SSabrina Dubroca idev->cnf.addr_gen_mode = new_val; 641723ca0c2cSThomas Winter addrconf_init_auto_addrs(idev->dev); 6418f24c5987SSabrina Dubroca } 6419f24c5987SSabrina Dubroca } 64208c171d6cSFelix Jia } 6421c6dbf7aaSSabrina Dubroca 6422c6dbf7aaSSabrina Dubroca *((u32 *)ctl->data) = new_val; 64238c171d6cSFelix Jia } 64248c171d6cSFelix Jia 64258c171d6cSFelix Jia out: 6426d35a00b8SFelix Jia rtnl_unlock(); 6427d35a00b8SFelix Jia 6428d35a00b8SFelix Jia return ret; 6429d35a00b8SFelix Jia } 6430d35a00b8SFelix Jia 64313d1bec99SHannes Frederic Sowa static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write, 643232927393SChristoph Hellwig void *buffer, size_t *lenp, 64333d1bec99SHannes Frederic Sowa loff_t *ppos) 64343d1bec99SHannes Frederic Sowa { 64353d1bec99SHannes Frederic Sowa int err; 64363d1bec99SHannes Frederic Sowa struct in6_addr addr; 64373d1bec99SHannes Frederic Sowa char str[IPV6_MAX_STRLEN]; 64383d1bec99SHannes Frederic Sowa struct ctl_table lctl = *ctl; 6439622c81d5SHannes Frederic Sowa struct net *net = ctl->extra2; 64403d1bec99SHannes Frederic Sowa struct ipv6_stable_secret *secret = ctl->data; 64413d1bec99SHannes Frederic Sowa 6442622c81d5SHannes Frederic Sowa if (&net->ipv6.devconf_all->stable_secret == ctl->data) 6443622c81d5SHannes Frederic Sowa return -EIO; 6444622c81d5SHannes Frederic Sowa 64453d1bec99SHannes Frederic Sowa lctl.maxlen = IPV6_MAX_STRLEN; 64463d1bec99SHannes Frederic Sowa lctl.data = str; 64473d1bec99SHannes Frederic Sowa 64483d1bec99SHannes Frederic Sowa if (!rtnl_trylock()) 64493d1bec99SHannes Frederic Sowa return restart_syscall(); 64503d1bec99SHannes Frederic Sowa 64513d1bec99SHannes Frederic Sowa if (!write && !secret->initialized) { 64523d1bec99SHannes Frederic Sowa err = -EIO; 64533d1bec99SHannes Frederic Sowa goto out; 64543d1bec99SHannes Frederic Sowa } 64553d1bec99SHannes Frederic Sowa 64565449a5caSWANG Cong err = snprintf(str, sizeof(str), "%pI6", &secret->secret); 64573d1bec99SHannes Frederic Sowa if (err >= sizeof(str)) { 64583d1bec99SHannes Frederic Sowa err = -EIO; 64593d1bec99SHannes Frederic Sowa goto out; 64603d1bec99SHannes Frederic Sowa } 64613d1bec99SHannes Frederic Sowa 64623d1bec99SHannes Frederic Sowa err = proc_dostring(&lctl, write, buffer, lenp, ppos); 64633d1bec99SHannes Frederic Sowa if (err || !write) 64643d1bec99SHannes Frederic Sowa goto out; 64653d1bec99SHannes Frederic Sowa 64663d1bec99SHannes Frederic Sowa if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) { 64673d1bec99SHannes Frederic Sowa err = -EIO; 64683d1bec99SHannes Frederic Sowa goto out; 64693d1bec99SHannes Frederic Sowa } 64703d1bec99SHannes Frederic Sowa 64713d1bec99SHannes Frederic Sowa secret->initialized = true; 64723d1bec99SHannes Frederic Sowa secret->secret = addr; 64733d1bec99SHannes Frederic Sowa 6474622c81d5SHannes Frederic Sowa if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) { 6475622c81d5SHannes Frederic Sowa struct net_device *dev; 6476622c81d5SHannes Frederic Sowa 6477622c81d5SHannes Frederic Sowa for_each_netdev(net, dev) { 6478622c81d5SHannes Frederic Sowa struct inet6_dev *idev = __in6_dev_get(dev); 6479622c81d5SHannes Frederic Sowa 6480622c81d5SHannes Frederic Sowa if (idev) { 6481d35a00b8SFelix Jia idev->cnf.addr_gen_mode = 6482622c81d5SHannes Frederic Sowa IN6_ADDR_GEN_MODE_STABLE_PRIVACY; 6483622c81d5SHannes Frederic Sowa } 6484622c81d5SHannes Frederic Sowa } 6485622c81d5SHannes Frederic Sowa } else { 6486622c81d5SHannes Frederic Sowa struct inet6_dev *idev = ctl->extra1; 6487622c81d5SHannes Frederic Sowa 6488d35a00b8SFelix Jia idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY; 6489622c81d5SHannes Frederic Sowa } 6490622c81d5SHannes Frederic Sowa 64913d1bec99SHannes Frederic Sowa out: 64923d1bec99SHannes Frederic Sowa rtnl_unlock(); 64933d1bec99SHannes Frederic Sowa 64943d1bec99SHannes Frederic Sowa return err; 64953d1bec99SHannes Frederic Sowa } 6496c92d5491Sstephen hemminger 649735103d11SAndy Gospodarek static 649835103d11SAndy Gospodarek int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl, 649932927393SChristoph Hellwig int write, void *buffer, 650035103d11SAndy Gospodarek size_t *lenp, 650135103d11SAndy Gospodarek loff_t *ppos) 650235103d11SAndy Gospodarek { 650335103d11SAndy Gospodarek int *valp = ctl->data; 650435103d11SAndy Gospodarek int val = *valp; 650535103d11SAndy Gospodarek loff_t pos = *ppos; 650635103d11SAndy Gospodarek struct ctl_table lctl; 650735103d11SAndy Gospodarek int ret; 650835103d11SAndy Gospodarek 650935103d11SAndy Gospodarek /* ctl->data points to idev->cnf.ignore_routes_when_linkdown 651035103d11SAndy Gospodarek * we should not modify it until we get the rtnl lock. 651135103d11SAndy Gospodarek */ 651235103d11SAndy Gospodarek lctl = *ctl; 651335103d11SAndy Gospodarek lctl.data = &val; 651435103d11SAndy Gospodarek 651535103d11SAndy Gospodarek ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 651635103d11SAndy Gospodarek 651735103d11SAndy Gospodarek if (write) 651835103d11SAndy Gospodarek ret = addrconf_fixup_linkdown(ctl, valp, val); 651935103d11SAndy Gospodarek if (ret) 652035103d11SAndy Gospodarek *ppos = pos; 652135103d11SAndy Gospodarek return ret; 652235103d11SAndy Gospodarek } 652335103d11SAndy Gospodarek 6524df789fe7SDavid Forster static 6525df789fe7SDavid Forster void addrconf_set_nopolicy(struct rt6_info *rt, int action) 6526df789fe7SDavid Forster { 6527df789fe7SDavid Forster if (rt) { 6528df789fe7SDavid Forster if (action) 6529df789fe7SDavid Forster rt->dst.flags |= DST_NOPOLICY; 6530df789fe7SDavid Forster else 6531df789fe7SDavid Forster rt->dst.flags &= ~DST_NOPOLICY; 6532df789fe7SDavid Forster } 6533df789fe7SDavid Forster } 6534df789fe7SDavid Forster 6535df789fe7SDavid Forster static 6536df789fe7SDavid Forster void addrconf_disable_policy_idev(struct inet6_dev *idev, int val) 6537df789fe7SDavid Forster { 6538df789fe7SDavid Forster struct inet6_ifaddr *ifa; 6539df789fe7SDavid Forster 6540df789fe7SDavid Forster read_lock_bh(&idev->lock); 6541df789fe7SDavid Forster list_for_each_entry(ifa, &idev->addr_list, if_list) { 6542df789fe7SDavid Forster spin_lock(&ifa->lock); 6543df789fe7SDavid Forster if (ifa->rt) { 6544f88d8ea6SDavid Ahern /* host routes only use builtin fib6_nh */ 65451cf844c7SDavid Ahern struct fib6_nh *nh = ifa->rt->fib6_nh; 6546df789fe7SDavid Forster int cpu; 6547df789fe7SDavid Forster 654866f5d6ceSWei Wang rcu_read_lock(); 65493b6761d1SDavid Ahern ifa->rt->dst_nopolicy = val ? true : false; 6550f40b6ae2SDavid Ahern if (nh->rt6i_pcpu) { 6551df789fe7SDavid Forster for_each_possible_cpu(cpu) { 6552df789fe7SDavid Forster struct rt6_info **rtp; 6553df789fe7SDavid Forster 6554f40b6ae2SDavid Ahern rtp = per_cpu_ptr(nh->rt6i_pcpu, cpu); 6555df789fe7SDavid Forster addrconf_set_nopolicy(*rtp, val); 6556df789fe7SDavid Forster } 6557df789fe7SDavid Forster } 655866f5d6ceSWei Wang rcu_read_unlock(); 6559df789fe7SDavid Forster } 6560df789fe7SDavid Forster spin_unlock(&ifa->lock); 6561df789fe7SDavid Forster } 6562df789fe7SDavid Forster read_unlock_bh(&idev->lock); 6563df789fe7SDavid Forster } 6564df789fe7SDavid Forster 6565df789fe7SDavid Forster static 6566df789fe7SDavid Forster int addrconf_disable_policy(struct ctl_table *ctl, int *valp, int val) 6567df789fe7SDavid Forster { 6568df789fe7SDavid Forster struct inet6_dev *idev; 6569df789fe7SDavid Forster struct net *net; 6570df789fe7SDavid Forster 6571df789fe7SDavid Forster if (!rtnl_trylock()) 6572df789fe7SDavid Forster return restart_syscall(); 6573df789fe7SDavid Forster 6574df789fe7SDavid Forster *valp = val; 6575df789fe7SDavid Forster 6576df789fe7SDavid Forster net = (struct net *)ctl->extra2; 6577df789fe7SDavid Forster if (valp == &net->ipv6.devconf_dflt->disable_policy) { 6578df789fe7SDavid Forster rtnl_unlock(); 6579df789fe7SDavid Forster return 0; 6580df789fe7SDavid Forster } 6581df789fe7SDavid Forster 6582df789fe7SDavid Forster if (valp == &net->ipv6.devconf_all->disable_policy) { 6583df789fe7SDavid Forster struct net_device *dev; 6584df789fe7SDavid Forster 6585df789fe7SDavid Forster for_each_netdev(net, dev) { 6586df789fe7SDavid Forster idev = __in6_dev_get(dev); 6587df789fe7SDavid Forster if (idev) 6588df789fe7SDavid Forster addrconf_disable_policy_idev(idev, val); 6589df789fe7SDavid Forster } 6590df789fe7SDavid Forster } else { 6591df789fe7SDavid Forster idev = (struct inet6_dev *)ctl->extra1; 6592df789fe7SDavid Forster addrconf_disable_policy_idev(idev, val); 6593df789fe7SDavid Forster } 6594df789fe7SDavid Forster 6595df789fe7SDavid Forster rtnl_unlock(); 6596df789fe7SDavid Forster return 0; 6597df789fe7SDavid Forster } 6598df789fe7SDavid Forster 659932927393SChristoph Hellwig static int addrconf_sysctl_disable_policy(struct ctl_table *ctl, int write, 660032927393SChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos) 6601df789fe7SDavid Forster { 6602df789fe7SDavid Forster int *valp = ctl->data; 6603df789fe7SDavid Forster int val = *valp; 6604df789fe7SDavid Forster loff_t pos = *ppos; 6605df789fe7SDavid Forster struct ctl_table lctl; 6606df789fe7SDavid Forster int ret; 6607df789fe7SDavid Forster 6608df789fe7SDavid Forster lctl = *ctl; 6609df789fe7SDavid Forster lctl.data = &val; 6610df789fe7SDavid Forster ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 6611df789fe7SDavid Forster 6612df789fe7SDavid Forster if (write && (*valp != val)) 6613df789fe7SDavid Forster ret = addrconf_disable_policy(ctl, valp, val); 6614df789fe7SDavid Forster 6615df789fe7SDavid Forster if (ret) 6616df789fe7SDavid Forster *ppos = pos; 6617df789fe7SDavid Forster 6618df789fe7SDavid Forster return ret; 6619df789fe7SDavid Forster } 6620df789fe7SDavid Forster 6621cb4a4c69SMaciej Żenczykowski static int minus_one = -1; 6622cb9e684eSMaciej Żenczykowski static const int two_five_five = 255; 6623176f716cSMatthieu Baerts static u32 ioam6_if_id_max = U16_MAX; 6624cb9e684eSMaciej Żenczykowski 6625607ea7cdSKonstantin Khlebnikov static const struct ctl_table addrconf_sysctl[] = { 66261da177e4SLinus Torvalds { 66271da177e4SLinus Torvalds .procname = "forwarding", 66281da177e4SLinus Torvalds .data = &ipv6_devconf.forwarding, 66291da177e4SLinus Torvalds .maxlen = sizeof(int), 66301da177e4SLinus Torvalds .mode = 0644, 66316d9f239aSAlexey Dobriyan .proc_handler = addrconf_sysctl_forward, 66321da177e4SLinus Torvalds }, 66331da177e4SLinus Torvalds { 66341da177e4SLinus Torvalds .procname = "hop_limit", 66351da177e4SLinus Torvalds .data = &ipv6_devconf.hop_limit, 66361da177e4SLinus Torvalds .maxlen = sizeof(int), 66371da177e4SLinus Torvalds .mode = 0644, 6638cb9e684eSMaciej Żenczykowski .proc_handler = proc_dointvec_minmax, 6639eec4844fSMatteo Croce .extra1 = (void *)SYSCTL_ONE, 6640cb9e684eSMaciej Żenczykowski .extra2 = (void *)&two_five_five, 66411da177e4SLinus Torvalds }, 66421da177e4SLinus Torvalds { 66431da177e4SLinus Torvalds .procname = "mtu", 66441da177e4SLinus Torvalds .data = &ipv6_devconf.mtu6, 66451da177e4SLinus Torvalds .maxlen = sizeof(int), 66461da177e4SLinus Torvalds .mode = 0644, 664777751427SMarcelo Leitner .proc_handler = addrconf_sysctl_mtu, 66481da177e4SLinus Torvalds }, 66491da177e4SLinus Torvalds { 66501da177e4SLinus Torvalds .procname = "accept_ra", 66511da177e4SLinus Torvalds .data = &ipv6_devconf.accept_ra, 66521da177e4SLinus Torvalds .maxlen = sizeof(int), 66531da177e4SLinus Torvalds .mode = 0644, 66546d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 66551da177e4SLinus Torvalds }, 66561da177e4SLinus Torvalds { 66571da177e4SLinus Torvalds .procname = "accept_redirects", 66581da177e4SLinus Torvalds .data = &ipv6_devconf.accept_redirects, 66591da177e4SLinus Torvalds .maxlen = sizeof(int), 66601da177e4SLinus Torvalds .mode = 0644, 66616d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 66621da177e4SLinus Torvalds }, 66631da177e4SLinus Torvalds { 66641da177e4SLinus Torvalds .procname = "autoconf", 66651da177e4SLinus Torvalds .data = &ipv6_devconf.autoconf, 66661da177e4SLinus Torvalds .maxlen = sizeof(int), 66671da177e4SLinus Torvalds .mode = 0644, 66686d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 66691da177e4SLinus Torvalds }, 66701da177e4SLinus Torvalds { 66711da177e4SLinus Torvalds .procname = "dad_transmits", 66721da177e4SLinus Torvalds .data = &ipv6_devconf.dad_transmits, 66731da177e4SLinus Torvalds .maxlen = sizeof(int), 66741da177e4SLinus Torvalds .mode = 0644, 66756d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 66761da177e4SLinus Torvalds }, 66771da177e4SLinus Torvalds { 66781da177e4SLinus Torvalds .procname = "router_solicitations", 66791da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicits, 66801da177e4SLinus Torvalds .maxlen = sizeof(int), 66811da177e4SLinus Torvalds .mode = 0644, 6682cb4a4c69SMaciej Żenczykowski .proc_handler = proc_dointvec_minmax, 6683cb4a4c69SMaciej Żenczykowski .extra1 = &minus_one, 66841da177e4SLinus Torvalds }, 66851da177e4SLinus Torvalds { 66861da177e4SLinus Torvalds .procname = "router_solicitation_interval", 66871da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_interval, 66881da177e4SLinus Torvalds .maxlen = sizeof(int), 66891da177e4SLinus Torvalds .mode = 0644, 66906d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 66911da177e4SLinus Torvalds }, 66921da177e4SLinus Torvalds { 6693bd11f074SMaciej Żenczykowski .procname = "router_solicitation_max_interval", 6694bd11f074SMaciej Żenczykowski .data = &ipv6_devconf.rtr_solicit_max_interval, 6695bd11f074SMaciej Żenczykowski .maxlen = sizeof(int), 6696bd11f074SMaciej Żenczykowski .mode = 0644, 6697bd11f074SMaciej Żenczykowski .proc_handler = proc_dointvec_jiffies, 6698bd11f074SMaciej Żenczykowski }, 6699bd11f074SMaciej Żenczykowski { 67001da177e4SLinus Torvalds .procname = "router_solicitation_delay", 67011da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_delay, 67021da177e4SLinus Torvalds .maxlen = sizeof(int), 67031da177e4SLinus Torvalds .mode = 0644, 67046d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 67051da177e4SLinus Torvalds }, 67061da177e4SLinus Torvalds { 67071da177e4SLinus Torvalds .procname = "force_mld_version", 67081da177e4SLinus Torvalds .data = &ipv6_devconf.force_mld_version, 67091da177e4SLinus Torvalds .maxlen = sizeof(int), 67101da177e4SLinus Torvalds .mode = 0644, 67116d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 67121da177e4SLinus Torvalds }, 6713fc4eba58SHannes Frederic Sowa { 6714fc4eba58SHannes Frederic Sowa .procname = "mldv1_unsolicited_report_interval", 6715fc4eba58SHannes Frederic Sowa .data = 6716fc4eba58SHannes Frederic Sowa &ipv6_devconf.mldv1_unsolicited_report_interval, 6717fc4eba58SHannes Frederic Sowa .maxlen = sizeof(int), 6718fc4eba58SHannes Frederic Sowa .mode = 0644, 6719fc4eba58SHannes Frederic Sowa .proc_handler = proc_dointvec_ms_jiffies, 6720fc4eba58SHannes Frederic Sowa }, 6721fc4eba58SHannes Frederic Sowa { 6722fc4eba58SHannes Frederic Sowa .procname = "mldv2_unsolicited_report_interval", 6723fc4eba58SHannes Frederic Sowa .data = 6724fc4eba58SHannes Frederic Sowa &ipv6_devconf.mldv2_unsolicited_report_interval, 6725fc4eba58SHannes Frederic Sowa .maxlen = sizeof(int), 6726fc4eba58SHannes Frederic Sowa .mode = 0644, 6727fc4eba58SHannes Frederic Sowa .proc_handler = proc_dointvec_ms_jiffies, 6728fc4eba58SHannes Frederic Sowa }, 67291da177e4SLinus Torvalds { 67301da177e4SLinus Torvalds .procname = "use_tempaddr", 67311da177e4SLinus Torvalds .data = &ipv6_devconf.use_tempaddr, 67321da177e4SLinus Torvalds .maxlen = sizeof(int), 67331da177e4SLinus Torvalds .mode = 0644, 67346d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 67351da177e4SLinus Torvalds }, 67361da177e4SLinus Torvalds { 67371da177e4SLinus Torvalds .procname = "temp_valid_lft", 67381da177e4SLinus Torvalds .data = &ipv6_devconf.temp_valid_lft, 67391da177e4SLinus Torvalds .maxlen = sizeof(int), 67401da177e4SLinus Torvalds .mode = 0644, 67416d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 67421da177e4SLinus Torvalds }, 67431da177e4SLinus Torvalds { 67441da177e4SLinus Torvalds .procname = "temp_prefered_lft", 67451da177e4SLinus Torvalds .data = &ipv6_devconf.temp_prefered_lft, 67461da177e4SLinus Torvalds .maxlen = sizeof(int), 67471da177e4SLinus Torvalds .mode = 0644, 67486d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 67491da177e4SLinus Torvalds }, 67501da177e4SLinus Torvalds { 67511da177e4SLinus Torvalds .procname = "regen_max_retry", 67521da177e4SLinus Torvalds .data = &ipv6_devconf.regen_max_retry, 67531da177e4SLinus Torvalds .maxlen = sizeof(int), 67541da177e4SLinus Torvalds .mode = 0644, 67556d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 67561da177e4SLinus Torvalds }, 67571da177e4SLinus Torvalds { 67581da177e4SLinus Torvalds .procname = "max_desync_factor", 67591da177e4SLinus Torvalds .data = &ipv6_devconf.max_desync_factor, 67601da177e4SLinus Torvalds .maxlen = sizeof(int), 67611da177e4SLinus Torvalds .mode = 0644, 67626d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 67631da177e4SLinus Torvalds }, 67641da177e4SLinus Torvalds { 67651da177e4SLinus Torvalds .procname = "max_addresses", 67661da177e4SLinus Torvalds .data = &ipv6_devconf.max_addresses, 67671da177e4SLinus Torvalds .maxlen = sizeof(int), 67681da177e4SLinus Torvalds .mode = 0644, 67696d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 67701da177e4SLinus Torvalds }, 67711da177e4SLinus Torvalds { 677265f5c7c1SYOSHIFUJI Hideaki .procname = "accept_ra_defrtr", 677365f5c7c1SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_defrtr, 677465f5c7c1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 677565f5c7c1SYOSHIFUJI Hideaki .mode = 0644, 67766d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 677765f5c7c1SYOSHIFUJI Hideaki }, 677865f5c7c1SYOSHIFUJI Hideaki { 67796b2e04bcSPraveen Chaudhary .procname = "ra_defrtr_metric", 67806b2e04bcSPraveen Chaudhary .data = &ipv6_devconf.ra_defrtr_metric, 67816b2e04bcSPraveen Chaudhary .maxlen = sizeof(u32), 67826b2e04bcSPraveen Chaudhary .mode = 0644, 67836b2e04bcSPraveen Chaudhary .proc_handler = proc_douintvec_minmax, 67846b2e04bcSPraveen Chaudhary .extra1 = (void *)SYSCTL_ONE, 67856b2e04bcSPraveen Chaudhary }, 67866b2e04bcSPraveen Chaudhary { 67878013d1d7SHangbin Liu .procname = "accept_ra_min_hop_limit", 67888013d1d7SHangbin Liu .data = &ipv6_devconf.accept_ra_min_hop_limit, 67898013d1d7SHangbin Liu .maxlen = sizeof(int), 67908013d1d7SHangbin Liu .mode = 0644, 67918013d1d7SHangbin Liu .proc_handler = proc_dointvec, 67928013d1d7SHangbin Liu }, 67938013d1d7SHangbin Liu { 6794c4fd30ebSYOSHIFUJI Hideaki .procname = "accept_ra_pinfo", 6795c4fd30ebSYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_pinfo, 6796c4fd30ebSYOSHIFUJI Hideaki .maxlen = sizeof(int), 6797c4fd30ebSYOSHIFUJI Hideaki .mode = 0644, 67986d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 6799c4fd30ebSYOSHIFUJI Hideaki }, 6800930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 6801930d6ff2SYOSHIFUJI Hideaki { 6802930d6ff2SYOSHIFUJI Hideaki .procname = "accept_ra_rtr_pref", 6803930d6ff2SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rtr_pref, 6804930d6ff2SYOSHIFUJI Hideaki .maxlen = sizeof(int), 6805930d6ff2SYOSHIFUJI Hideaki .mode = 0644, 68066d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 6807930d6ff2SYOSHIFUJI Hideaki }, 680852e16356SYOSHIFUJI Hideaki { 680952e16356SYOSHIFUJI Hideaki .procname = "router_probe_interval", 681052e16356SYOSHIFUJI Hideaki .data = &ipv6_devconf.rtr_probe_interval, 681152e16356SYOSHIFUJI Hideaki .maxlen = sizeof(int), 681252e16356SYOSHIFUJI Hideaki .mode = 0644, 68136d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 681452e16356SYOSHIFUJI Hideaki }, 6815fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 681609c884d4SYOSHIFUJI Hideaki { 6817bbea124bSJoel Scherpelz .procname = "accept_ra_rt_info_min_plen", 6818bbea124bSJoel Scherpelz .data = &ipv6_devconf.accept_ra_rt_info_min_plen, 6819bbea124bSJoel Scherpelz .maxlen = sizeof(int), 6820bbea124bSJoel Scherpelz .mode = 0644, 6821bbea124bSJoel Scherpelz .proc_handler = proc_dointvec, 6822bbea124bSJoel Scherpelz }, 6823bbea124bSJoel Scherpelz { 682409c884d4SYOSHIFUJI Hideaki .procname = "accept_ra_rt_info_max_plen", 682509c884d4SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rt_info_max_plen, 682609c884d4SYOSHIFUJI Hideaki .maxlen = sizeof(int), 682709c884d4SYOSHIFUJI Hideaki .mode = 0644, 68286d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 682909c884d4SYOSHIFUJI Hideaki }, 683009c884d4SYOSHIFUJI Hideaki #endif 6831930d6ff2SYOSHIFUJI Hideaki #endif 6832c4fd30ebSYOSHIFUJI Hideaki { 6833fbea49e1SYOSHIFUJI Hideaki .procname = "proxy_ndp", 6834fbea49e1SYOSHIFUJI Hideaki .data = &ipv6_devconf.proxy_ndp, 6835fbea49e1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 6836fbea49e1SYOSHIFUJI Hideaki .mode = 0644, 6837c92d5491Sstephen hemminger .proc_handler = addrconf_sysctl_proxy_ndp, 6838fbea49e1SYOSHIFUJI Hideaki }, 6839fbea49e1SYOSHIFUJI Hideaki { 68400bcbc926SYOSHIFUJI Hideaki .procname = "accept_source_route", 68410bcbc926SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_source_route, 68420bcbc926SYOSHIFUJI Hideaki .maxlen = sizeof(int), 68430bcbc926SYOSHIFUJI Hideaki .mode = 0644, 68446d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 68450bcbc926SYOSHIFUJI Hideaki }, 684695c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 684795c385b4SNeil Horman { 684895c385b4SNeil Horman .procname = "optimistic_dad", 684995c385b4SNeil Horman .data = &ipv6_devconf.optimistic_dad, 685095c385b4SNeil Horman .maxlen = sizeof(int), 685195c385b4SNeil Horman .mode = 0644, 68526d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 685395c385b4SNeil Horman }, 68547fd2561eSErik Kline { 68557fd2561eSErik Kline .procname = "use_optimistic", 68567fd2561eSErik Kline .data = &ipv6_devconf.use_optimistic, 68577fd2561eSErik Kline .maxlen = sizeof(int), 68587fd2561eSErik Kline .mode = 0644, 68597fd2561eSErik Kline .proc_handler = proc_dointvec, 68607fd2561eSErik Kline }, 686195c385b4SNeil Horman #endif 68627bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 68637bc570c8SYOSHIFUJI Hideaki { 68647bc570c8SYOSHIFUJI Hideaki .procname = "mc_forwarding", 68657bc570c8SYOSHIFUJI Hideaki .data = &ipv6_devconf.mc_forwarding, 68667bc570c8SYOSHIFUJI Hideaki .maxlen = sizeof(int), 6867a4e6db07SDavid S. Miller .mode = 0444, 68686d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 68697bc570c8SYOSHIFUJI Hideaki }, 68707bc570c8SYOSHIFUJI Hideaki #endif 68710bcbc926SYOSHIFUJI Hideaki { 6872778d80beSYOSHIFUJI Hideaki .procname = "disable_ipv6", 6873778d80beSYOSHIFUJI Hideaki .data = &ipv6_devconf.disable_ipv6, 6874778d80beSYOSHIFUJI Hideaki .maxlen = sizeof(int), 6875778d80beSYOSHIFUJI Hideaki .mode = 0644, 687656d417b1SBrian Haley .proc_handler = addrconf_sysctl_disable, 6877778d80beSYOSHIFUJI Hideaki }, 6878778d80beSYOSHIFUJI Hideaki { 68791b34be74SYOSHIFUJI Hideaki .procname = "accept_dad", 68801b34be74SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_dad, 68811b34be74SYOSHIFUJI Hideaki .maxlen = sizeof(int), 68821b34be74SYOSHIFUJI Hideaki .mode = 0644, 68836d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 68841b34be74SYOSHIFUJI Hideaki }, 68851b34be74SYOSHIFUJI Hideaki { 6886f7734fdfSOctavian Purdila .procname = "force_tllao", 6887f7734fdfSOctavian Purdila .data = &ipv6_devconf.force_tllao, 6888f7734fdfSOctavian Purdila .maxlen = sizeof(int), 6889f7734fdfSOctavian Purdila .mode = 0644, 6890f7734fdfSOctavian Purdila .proc_handler = proc_dointvec 6891f7734fdfSOctavian Purdila }, 6892f7734fdfSOctavian Purdila { 68935cb04436SHannes Frederic Sowa .procname = "ndisc_notify", 68945cb04436SHannes Frederic Sowa .data = &ipv6_devconf.ndisc_notify, 68955cb04436SHannes Frederic Sowa .maxlen = sizeof(int), 68965cb04436SHannes Frederic Sowa .mode = 0644, 68975cb04436SHannes Frederic Sowa .proc_handler = proc_dointvec 68985cb04436SHannes Frederic Sowa }, 68995cb04436SHannes Frederic Sowa { 6900b800c3b9SHannes Frederic Sowa .procname = "suppress_frag_ndisc", 6901b800c3b9SHannes Frederic Sowa .data = &ipv6_devconf.suppress_frag_ndisc, 6902b800c3b9SHannes Frederic Sowa .maxlen = sizeof(int), 6903b800c3b9SHannes Frederic Sowa .mode = 0644, 6904b800c3b9SHannes Frederic Sowa .proc_handler = proc_dointvec 6905b800c3b9SHannes Frederic Sowa }, 6906b800c3b9SHannes Frederic Sowa { 6907d9333196SBen Greear .procname = "accept_ra_from_local", 6908d9333196SBen Greear .data = &ipv6_devconf.accept_ra_from_local, 6909d9333196SBen Greear .maxlen = sizeof(int), 6910d9333196SBen Greear .mode = 0644, 6911d9333196SBen Greear .proc_handler = proc_dointvec, 6912d9333196SBen Greear }, 6913d9333196SBen Greear { 6914c2943f14SHarout Hedeshian .procname = "accept_ra_mtu", 6915c2943f14SHarout Hedeshian .data = &ipv6_devconf.accept_ra_mtu, 6916c2943f14SHarout Hedeshian .maxlen = sizeof(int), 6917c2943f14SHarout Hedeshian .mode = 0644, 6918c2943f14SHarout Hedeshian .proc_handler = proc_dointvec, 6919c2943f14SHarout Hedeshian }, 6920c2943f14SHarout Hedeshian { 69213d1bec99SHannes Frederic Sowa .procname = "stable_secret", 69223d1bec99SHannes Frederic Sowa .data = &ipv6_devconf.stable_secret, 69233d1bec99SHannes Frederic Sowa .maxlen = IPV6_MAX_STRLEN, 69243d1bec99SHannes Frederic Sowa .mode = 0600, 69253d1bec99SHannes Frederic Sowa .proc_handler = addrconf_sysctl_stable_secret, 69263d1bec99SHannes Frederic Sowa }, 69273d1bec99SHannes Frederic Sowa { 69283985e8a3SErik Kline .procname = "use_oif_addrs_only", 69293985e8a3SErik Kline .data = &ipv6_devconf.use_oif_addrs_only, 69303985e8a3SErik Kline .maxlen = sizeof(int), 69313985e8a3SErik Kline .mode = 0644, 69323985e8a3SErik Kline .proc_handler = proc_dointvec, 693335103d11SAndy Gospodarek }, 693435103d11SAndy Gospodarek { 693535103d11SAndy Gospodarek .procname = "ignore_routes_with_linkdown", 693635103d11SAndy Gospodarek .data = &ipv6_devconf.ignore_routes_with_linkdown, 693735103d11SAndy Gospodarek .maxlen = sizeof(int), 693835103d11SAndy Gospodarek .mode = 0644, 693935103d11SAndy Gospodarek .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown, 69403985e8a3SErik Kline }, 69413985e8a3SErik Kline { 6942abbc3043SJohannes Berg .procname = "drop_unicast_in_l2_multicast", 6943abbc3043SJohannes Berg .data = &ipv6_devconf.drop_unicast_in_l2_multicast, 6944abbc3043SJohannes Berg .maxlen = sizeof(int), 6945abbc3043SJohannes Berg .mode = 0644, 6946abbc3043SJohannes Berg .proc_handler = proc_dointvec, 6947abbc3043SJohannes Berg }, 6948abbc3043SJohannes Berg { 69497a02bf89SJohannes Berg .procname = "drop_unsolicited_na", 69507a02bf89SJohannes Berg .data = &ipv6_devconf.drop_unsolicited_na, 69517a02bf89SJohannes Berg .maxlen = sizeof(int), 69527a02bf89SJohannes Berg .mode = 0644, 69537a02bf89SJohannes Berg .proc_handler = proc_dointvec, 69547a02bf89SJohannes Berg }, 69557a02bf89SJohannes Berg { 6956f1705ec1SDavid Ahern .procname = "keep_addr_on_down", 6957f1705ec1SDavid Ahern .data = &ipv6_devconf.keep_addr_on_down, 6958f1705ec1SDavid Ahern .maxlen = sizeof(int), 6959f1705ec1SDavid Ahern .mode = 0644, 6960f1705ec1SDavid Ahern .proc_handler = proc_dointvec, 6961f1705ec1SDavid Ahern 6962f1705ec1SDavid Ahern }, 6963f1705ec1SDavid Ahern { 69641ababebaSDavid Lebrun .procname = "seg6_enabled", 69651ababebaSDavid Lebrun .data = &ipv6_devconf.seg6_enabled, 69661ababebaSDavid Lebrun .maxlen = sizeof(int), 69671ababebaSDavid Lebrun .mode = 0644, 69681ababebaSDavid Lebrun .proc_handler = proc_dointvec, 69691ababebaSDavid Lebrun }, 6970bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC 6971bf355b8dSDavid Lebrun { 6972bf355b8dSDavid Lebrun .procname = "seg6_require_hmac", 6973bf355b8dSDavid Lebrun .data = &ipv6_devconf.seg6_require_hmac, 6974bf355b8dSDavid Lebrun .maxlen = sizeof(int), 6975bf355b8dSDavid Lebrun .mode = 0644, 6976bf355b8dSDavid Lebrun .proc_handler = proc_dointvec, 6977bf355b8dSDavid Lebrun }, 6978bf355b8dSDavid Lebrun #endif 69791ababebaSDavid Lebrun { 6980adc176c5SErik Nordmark .procname = "enhanced_dad", 6981adc176c5SErik Nordmark .data = &ipv6_devconf.enhanced_dad, 6982adc176c5SErik Nordmark .maxlen = sizeof(int), 6983adc176c5SErik Nordmark .mode = 0644, 6984adc176c5SErik Nordmark .proc_handler = proc_dointvec, 6985adc176c5SErik Nordmark }, 6986adc176c5SErik Nordmark { 6987d35a00b8SFelix Jia .procname = "addr_gen_mode", 6988d35a00b8SFelix Jia .data = &ipv6_devconf.addr_gen_mode, 6989d35a00b8SFelix Jia .maxlen = sizeof(int), 6990d35a00b8SFelix Jia .mode = 0644, 6991d35a00b8SFelix Jia .proc_handler = addrconf_sysctl_addr_gen_mode, 6992d35a00b8SFelix Jia }, 6993d35a00b8SFelix Jia { 6994df789fe7SDavid Forster .procname = "disable_policy", 6995df789fe7SDavid Forster .data = &ipv6_devconf.disable_policy, 6996df789fe7SDavid Forster .maxlen = sizeof(int), 6997df789fe7SDavid Forster .mode = 0644, 6998df789fe7SDavid Forster .proc_handler = addrconf_sysctl_disable_policy, 6999df789fe7SDavid Forster }, 7000df789fe7SDavid Forster { 70012210d6b2SMaciej Żenczykowski .procname = "ndisc_tclass", 70022210d6b2SMaciej Żenczykowski .data = &ipv6_devconf.ndisc_tclass, 70032210d6b2SMaciej Żenczykowski .maxlen = sizeof(int), 70042210d6b2SMaciej Żenczykowski .mode = 0644, 70052210d6b2SMaciej Żenczykowski .proc_handler = proc_dointvec_minmax, 7006eec4844fSMatteo Croce .extra1 = (void *)SYSCTL_ZERO, 70072210d6b2SMaciej Żenczykowski .extra2 = (void *)&two_five_five, 70082210d6b2SMaciej Żenczykowski }, 70092210d6b2SMaciej Żenczykowski { 70108610c7c6SAlexander Aring .procname = "rpl_seg_enabled", 70118610c7c6SAlexander Aring .data = &ipv6_devconf.rpl_seg_enabled, 70128610c7c6SAlexander Aring .maxlen = sizeof(int), 70138610c7c6SAlexander Aring .mode = 0644, 70148610c7c6SAlexander Aring .proc_handler = proc_dointvec, 70158610c7c6SAlexander Aring }, 70168610c7c6SAlexander Aring { 70179ee11f0fSJustin Iurman .procname = "ioam6_enabled", 70189ee11f0fSJustin Iurman .data = &ipv6_devconf.ioam6_enabled, 70199ee11f0fSJustin Iurman .maxlen = sizeof(u8), 70209ee11f0fSJustin Iurman .mode = 0644, 70219ee11f0fSJustin Iurman .proc_handler = proc_dou8vec_minmax, 70229ee11f0fSJustin Iurman .extra1 = (void *)SYSCTL_ZERO, 70239ee11f0fSJustin Iurman .extra2 = (void *)SYSCTL_ONE, 70249ee11f0fSJustin Iurman }, 70259ee11f0fSJustin Iurman { 70269ee11f0fSJustin Iurman .procname = "ioam6_id", 70279ee11f0fSJustin Iurman .data = &ipv6_devconf.ioam6_id, 70289ee11f0fSJustin Iurman .maxlen = sizeof(u32), 70299ee11f0fSJustin Iurman .mode = 0644, 70309ee11f0fSJustin Iurman .proc_handler = proc_douintvec_minmax, 70319ee11f0fSJustin Iurman .extra1 = (void *)SYSCTL_ZERO, 70329ee11f0fSJustin Iurman .extra2 = (void *)&ioam6_if_id_max, 70339ee11f0fSJustin Iurman }, 70349ee11f0fSJustin Iurman { 70359ee11f0fSJustin Iurman .procname = "ioam6_id_wide", 70369ee11f0fSJustin Iurman .data = &ipv6_devconf.ioam6_id_wide, 70379ee11f0fSJustin Iurman .maxlen = sizeof(u32), 70389ee11f0fSJustin Iurman .mode = 0644, 70399ee11f0fSJustin Iurman .proc_handler = proc_douintvec, 70409ee11f0fSJustin Iurman }, 70419ee11f0fSJustin Iurman { 704218ac597aSJames Prestwood .procname = "ndisc_evict_nocarrier", 704318ac597aSJames Prestwood .data = &ipv6_devconf.ndisc_evict_nocarrier, 704418ac597aSJames Prestwood .maxlen = sizeof(u8), 704518ac597aSJames Prestwood .mode = 0644, 704618ac597aSJames Prestwood .proc_handler = proc_dou8vec_minmax, 704718ac597aSJames Prestwood .extra1 = (void *)SYSCTL_ZERO, 704818ac597aSJames Prestwood .extra2 = (void *)SYSCTL_ONE, 704918ac597aSJames Prestwood }, 705018ac597aSJames Prestwood { 70513e0b8f52SArun Ajith S .procname = "accept_untracked_na", 70523e0b8f52SArun Ajith S .data = &ipv6_devconf.accept_untracked_na, 7053f9a2fb73SArun Ajith S .maxlen = sizeof(int), 7054f9a2fb73SArun Ajith S .mode = 0644, 7055b66eb3a6SJaehee Park .proc_handler = proc_dointvec_minmax, 7056b66eb3a6SJaehee Park .extra1 = SYSCTL_ZERO, 7057b66eb3a6SJaehee Park .extra2 = SYSCTL_TWO, 7058f9a2fb73SArun Ajith S }, 7059f9a2fb73SArun Ajith S { 7060f8572d8fSEric W. Biederman /* sentinel */ 70611da177e4SLinus Torvalds } 70621da177e4SLinus Torvalds }; 70631da177e4SLinus Torvalds 7064bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name, 7065f8572d8fSEric W. Biederman struct inet6_dev *idev, struct ipv6_devconf *p) 70661da177e4SLinus Torvalds { 706729c994e3SNicolas Dichtel int i, ifindex; 7068607ea7cdSKonstantin Khlebnikov struct ctl_table *table; 70696105e293SEric W. Biederman char path[sizeof("net/ipv6/conf/") + IFNAMSIZ]; 70701dab6222SPavel Emelyanov 7071425b9c7fSVasily Averin table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL_ACCOUNT); 7072607ea7cdSKonstantin Khlebnikov if (!table) 7073f68635e6SPavel Emelyanov goto out; 7074f68635e6SPavel Emelyanov 7075607ea7cdSKonstantin Khlebnikov for (i = 0; table[i].data; i++) { 7076607ea7cdSKonstantin Khlebnikov table[i].data += (char *)p - (char *)&ipv6_devconf; 7077cb9e684eSMaciej Żenczykowski /* If one of these is already set, then it is not safe to 7078cb9e684eSMaciej Żenczykowski * overwrite either of them: this makes proc_dointvec_minmax 7079cb9e684eSMaciej Żenczykowski * usable. 7080cb9e684eSMaciej Żenczykowski */ 7081cb9e684eSMaciej Żenczykowski if (!table[i].extra1 && !table[i].extra2) { 7082607ea7cdSKonstantin Khlebnikov table[i].extra1 = idev; /* embedded; no ref */ 7083607ea7cdSKonstantin Khlebnikov table[i].extra2 = net; 70841da177e4SLinus Torvalds } 7085cb9e684eSMaciej Żenczykowski } 70861da177e4SLinus Torvalds 70876105e293SEric W. Biederman snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name); 70881da177e4SLinus Torvalds 7089*c899710fSJoel Granados p->sysctl_header = register_net_sysctl_sz(net, path, table, 7090*c899710fSJoel Granados ARRAY_SIZE(addrconf_sysctl)); 7091607ea7cdSKonstantin Khlebnikov if (!p->sysctl_header) 70926105e293SEric W. Biederman goto free; 7093f68635e6SPavel Emelyanov 709429c994e3SNicolas Dichtel if (!strcmp(dev_name, "all")) 709529c994e3SNicolas Dichtel ifindex = NETCONFA_IFINDEX_ALL; 709629c994e3SNicolas Dichtel else if (!strcmp(dev_name, "default")) 709729c994e3SNicolas Dichtel ifindex = NETCONFA_IFINDEX_DEFAULT; 709829c994e3SNicolas Dichtel else 709929c994e3SNicolas Dichtel ifindex = idev->dev->ifindex; 710085b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL, 710185b3daadSDavid Ahern ifindex, p); 710295897312SPavel Emelyanov return 0; 71031da177e4SLinus Torvalds 71041da177e4SLinus Torvalds free: 7105607ea7cdSKonstantin Khlebnikov kfree(table); 7106f68635e6SPavel Emelyanov out: 710795897312SPavel Emelyanov return -ENOBUFS; 71081da177e4SLinus Torvalds } 71091da177e4SLinus Torvalds 711023452170SDavid Ahern static void __addrconf_sysctl_unregister(struct net *net, 711123452170SDavid Ahern struct ipv6_devconf *p, int ifindex) 7112f52295a9SPavel Emelyanov { 7113607ea7cdSKonstantin Khlebnikov struct ctl_table *table; 7114f52295a9SPavel Emelyanov 7115607ea7cdSKonstantin Khlebnikov if (!p->sysctl_header) 7116408c4768SPavel Emelyanov return; 7117408c4768SPavel Emelyanov 7118607ea7cdSKonstantin Khlebnikov table = p->sysctl_header->ctl_table_arg; 7119607ea7cdSKonstantin Khlebnikov unregister_net_sysctl_table(p->sysctl_header); 7120607ea7cdSKonstantin Khlebnikov p->sysctl_header = NULL; 7121607ea7cdSKonstantin Khlebnikov kfree(table); 712223452170SDavid Ahern 712323452170SDavid Ahern inet6_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL); 71241da177e4SLinus Torvalds } 7125408c4768SPavel Emelyanov 7126a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev) 7127408c4768SPavel Emelyanov { 7128a317a2f1SWANG Cong int err; 7129a317a2f1SWANG Cong 7130a317a2f1SWANG Cong if (!sysctl_dev_name_is_allowed(idev->dev->name)) 7131a317a2f1SWANG Cong return -EINVAL; 7132a317a2f1SWANG Cong 7133a317a2f1SWANG Cong err = neigh_sysctl_register(idev->dev, idev->nd_parms, 7134f8572d8fSEric W. Biederman &ndisc_ifinfo_sysctl_change); 7135a317a2f1SWANG Cong if (err) 7136a317a2f1SWANG Cong return err; 7137a317a2f1SWANG Cong err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name, 7138f8572d8fSEric W. Biederman idev, &idev->cnf); 7139a317a2f1SWANG Cong if (err) 7140a317a2f1SWANG Cong neigh_sysctl_unregister(idev->nd_parms); 7141a317a2f1SWANG Cong 7142a317a2f1SWANG Cong return err; 7143408c4768SPavel Emelyanov } 7144408c4768SPavel Emelyanov 7145408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev) 7146408c4768SPavel Emelyanov { 714723452170SDavid Ahern __addrconf_sysctl_unregister(dev_net(idev->dev), &idev->cnf, 714823452170SDavid Ahern idev->dev->ifindex); 7149408c4768SPavel Emelyanov neigh_sysctl_unregister(idev->nd_parms); 71501da177e4SLinus Torvalds } 71511da177e4SLinus Torvalds 71521da177e4SLinus Torvalds 71531da177e4SLinus Torvalds #endif 71541da177e4SLinus Torvalds 71552c8c1e72SAlexey Dobriyan static int __net_init addrconf_init_net(struct net *net) 7156e0da5a48SPavel Emelyanov { 7157a79ca223SHong Zhiguo int err = -ENOMEM; 7158e0da5a48SPavel Emelyanov struct ipv6_devconf *all, *dflt; 7159e0da5a48SPavel Emelyanov 716021a216a8SEric Dumazet spin_lock_init(&net->ipv6.addrconf_hash_lock); 71618805d13fSEric Dumazet INIT_DEFERRABLE_WORK(&net->ipv6.addr_chk_work, addrconf_verify_work); 716221a216a8SEric Dumazet net->ipv6.inet6_addr_lst = kcalloc(IN6_ADDR_HSIZE, 716321a216a8SEric Dumazet sizeof(struct hlist_head), 716421a216a8SEric Dumazet GFP_KERNEL); 716521a216a8SEric Dumazet if (!net->ipv6.inet6_addr_lst) 716621a216a8SEric Dumazet goto err_alloc_addr; 716721a216a8SEric Dumazet 7168a79ca223SHong Zhiguo all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL); 716963159f29SIan Morris if (!all) 7170e0da5a48SPavel Emelyanov goto err_alloc_all; 7171e0da5a48SPavel Emelyanov 7172a79ca223SHong Zhiguo dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL); 717363159f29SIan Morris if (!dflt) 7174e0da5a48SPavel Emelyanov goto err_alloc_dflt; 7175a79ca223SHong Zhiguo 7176a5612ca1SKuniyuki Iwashima if (!net_eq(net, &init_net)) { 7177a5612ca1SKuniyuki Iwashima switch (net_inherit_devconf()) { 71789efd6a3cSNicolas Dichtel case 1: /* copy from init_net */ 71799efd6a3cSNicolas Dichtel memcpy(all, init_net.ipv6.devconf_all, 71809efd6a3cSNicolas Dichtel sizeof(ipv6_devconf)); 71819efd6a3cSNicolas Dichtel memcpy(dflt, init_net.ipv6.devconf_dflt, 71829efd6a3cSNicolas Dichtel sizeof(ipv6_devconf_dflt)); 71839efd6a3cSNicolas Dichtel break; 71849efd6a3cSNicolas Dichtel case 3: /* copy from the current netns */ 71859efd6a3cSNicolas Dichtel memcpy(all, current->nsproxy->net_ns->ipv6.devconf_all, 71869efd6a3cSNicolas Dichtel sizeof(ipv6_devconf)); 71879efd6a3cSNicolas Dichtel memcpy(dflt, 71889efd6a3cSNicolas Dichtel current->nsproxy->net_ns->ipv6.devconf_dflt, 71899efd6a3cSNicolas Dichtel sizeof(ipv6_devconf_dflt)); 71909efd6a3cSNicolas Dichtel break; 71919efd6a3cSNicolas Dichtel case 0: 71929efd6a3cSNicolas Dichtel case 2: 71939efd6a3cSNicolas Dichtel /* use compiled values */ 71949efd6a3cSNicolas Dichtel break; 71959efd6a3cSNicolas Dichtel } 7196856c395cSCong Wang } 7197856c395cSCong Wang 719856d417b1SBrian Haley /* these will be inherited by all namespaces */ 719956d417b1SBrian Haley dflt->autoconf = ipv6_defaults.autoconf; 720056d417b1SBrian Haley dflt->disable_ipv6 = ipv6_defaults.disable_ipv6; 7201e0da5a48SPavel Emelyanov 72023d1bec99SHannes Frederic Sowa dflt->stable_secret.initialized = false; 72033d1bec99SHannes Frederic Sowa all->stable_secret.initialized = false; 72043d1bec99SHannes Frederic Sowa 7205e0da5a48SPavel Emelyanov net->ipv6.devconf_all = all; 7206e0da5a48SPavel Emelyanov net->ipv6.devconf_dflt = dflt; 7207e0da5a48SPavel Emelyanov 7208e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 7209f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "all", NULL, all); 7210e0da5a48SPavel Emelyanov if (err < 0) 7211e0da5a48SPavel Emelyanov goto err_reg_all; 7212e0da5a48SPavel Emelyanov 7213f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "default", NULL, dflt); 7214e0da5a48SPavel Emelyanov if (err < 0) 7215e0da5a48SPavel Emelyanov goto err_reg_dflt; 7216e0da5a48SPavel Emelyanov #endif 7217e0da5a48SPavel Emelyanov return 0; 7218e0da5a48SPavel Emelyanov 7219e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 7220e0da5a48SPavel Emelyanov err_reg_dflt: 722123452170SDavid Ahern __addrconf_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL); 7222e0da5a48SPavel Emelyanov err_reg_all: 7223e0da5a48SPavel Emelyanov kfree(dflt); 72241ca69520SZhengchao Shao net->ipv6.devconf_dflt = NULL; 7225e0da5a48SPavel Emelyanov #endif 7226e0da5a48SPavel Emelyanov err_alloc_dflt: 7227e0da5a48SPavel Emelyanov kfree(all); 72281ca69520SZhengchao Shao net->ipv6.devconf_all = NULL; 7229e0da5a48SPavel Emelyanov err_alloc_all: 723021a216a8SEric Dumazet kfree(net->ipv6.inet6_addr_lst); 723121a216a8SEric Dumazet err_alloc_addr: 7232e0da5a48SPavel Emelyanov return err; 7233e0da5a48SPavel Emelyanov } 7234e0da5a48SPavel Emelyanov 72352c8c1e72SAlexey Dobriyan static void __net_exit addrconf_exit_net(struct net *net) 7236e0da5a48SPavel Emelyanov { 723721a216a8SEric Dumazet int i; 723821a216a8SEric Dumazet 7239e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 724023452170SDavid Ahern __addrconf_sysctl_unregister(net, net->ipv6.devconf_dflt, 724123452170SDavid Ahern NETCONFA_IFINDEX_DEFAULT); 724223452170SDavid Ahern __addrconf_sysctl_unregister(net, net->ipv6.devconf_all, 724323452170SDavid Ahern NETCONFA_IFINDEX_ALL); 7244e0da5a48SPavel Emelyanov #endif 7245e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_dflt); 72467d9b1b57SEric Dumazet net->ipv6.devconf_dflt = NULL; 7247e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_all); 72487d9b1b57SEric Dumazet net->ipv6.devconf_all = NULL; 724921a216a8SEric Dumazet 7250be6b41c1SEric Dumazet cancel_delayed_work_sync(&net->ipv6.addr_chk_work); 725121a216a8SEric Dumazet /* 725221a216a8SEric Dumazet * Check hash table, then free it. 725321a216a8SEric Dumazet */ 725421a216a8SEric Dumazet for (i = 0; i < IN6_ADDR_HSIZE; i++) 725521a216a8SEric Dumazet WARN_ON_ONCE(!hlist_empty(&net->ipv6.inet6_addr_lst[i])); 725621a216a8SEric Dumazet 725721a216a8SEric Dumazet kfree(net->ipv6.inet6_addr_lst); 725821a216a8SEric Dumazet net->ipv6.inet6_addr_lst = NULL; 7259e0da5a48SPavel Emelyanov } 7260e0da5a48SPavel Emelyanov 7261e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = { 7262e0da5a48SPavel Emelyanov .init = addrconf_init_net, 7263e0da5a48SPavel Emelyanov .exit = addrconf_exit_net, 7264e0da5a48SPavel Emelyanov }; 7265e0da5a48SPavel Emelyanov 7266207895fdSDaniel Borkmann static struct rtnl_af_ops inet6_ops __read_mostly = { 7267b382b191SThomas Graf .family = AF_INET6, 7268b382b191SThomas Graf .fill_link_af = inet6_fill_link_af, 7269b382b191SThomas Graf .get_link_af_size = inet6_get_link_af_size, 727011b1f828SDaniel Borkmann .validate_link_af = inet6_validate_link_af, 7271f53adae4SDaniel Borkmann .set_link_af = inet6_set_link_af, 7272b382b191SThomas Graf }; 7273b382b191SThomas Graf 72741da177e4SLinus Torvalds /* 72751da177e4SLinus Torvalds * Init / cleanup code 72761da177e4SLinus Torvalds */ 72771da177e4SLinus Torvalds 72781da177e4SLinus Torvalds int __init addrconf_init(void) 72791da177e4SLinus Torvalds { 7280a317a2f1SWANG Cong struct inet6_dev *idev; 7281e66d1172SEric Dumazet int err; 72822a8cc6c8SYOSHIFUJI Hideaki 7283e21e8467SStephen Hemminger err = ipv6_addr_label_init(); 7284e21e8467SStephen Hemminger if (err < 0) { 7285f3213831SJoe Perches pr_crit("%s: cannot initialize default policy table: %d\n", 7286f3213831SJoe Perches __func__, err); 72872cc6d2bfSNeil Horman goto out; 72882a8cc6c8SYOSHIFUJI Hideaki } 72891da177e4SLinus Torvalds 72902cc6d2bfSNeil Horman err = register_pernet_subsys(&addrconf_ops); 72912cc6d2bfSNeil Horman if (err < 0) 72922cc6d2bfSNeil Horman goto out_addrlabel; 7293e0da5a48SPavel Emelyanov 7294c15b1ccaSHannes Frederic Sowa addrconf_wq = create_workqueue("ipv6_addrconf"); 7295c15b1ccaSHannes Frederic Sowa if (!addrconf_wq) { 7296c15b1ccaSHannes Frederic Sowa err = -ENOMEM; 7297c15b1ccaSHannes Frederic Sowa goto out_nowq; 7298c15b1ccaSHannes Frederic Sowa } 7299c15b1ccaSHannes Frederic Sowa 73001da177e4SLinus Torvalds rtnl_lock(); 7301e5f80fcfSEric Dumazet idev = ipv6_add_dev(blackhole_netdev); 73021da177e4SLinus Torvalds rtnl_unlock(); 7303a317a2f1SWANG Cong if (IS_ERR(idev)) { 7304a317a2f1SWANG Cong err = PTR_ERR(idev); 7305e0da5a48SPavel Emelyanov goto errlo; 7306a317a2f1SWANG Cong } 73071da177e4SLinus Torvalds 73082f460933SWANG Cong ip6_route_init_special_entries(); 73092f460933SWANG Cong 73101da177e4SLinus Torvalds register_netdevice_notifier(&ipv6_dev_notf); 73111da177e4SLinus Torvalds 73128805d13fSEric Dumazet addrconf_verify(&init_net); 7313c127ea2cSThomas Graf 73143678a9d8Sstephen hemminger rtnl_af_register(&inet6_ops); 7315b382b191SThomas Graf 731616feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETLINK, 731716feebcfSFlorian Westphal NULL, inet6_dump_ifinfo, 0); 7318c127ea2cSThomas Graf if (err < 0) 7319c127ea2cSThomas Graf goto errout; 7320c127ea2cSThomas Graf 732116feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWADDR, 732216feebcfSFlorian Westphal inet6_rtm_newaddr, NULL, 0); 732316feebcfSFlorian Westphal if (err < 0) 732416feebcfSFlorian Westphal goto errout; 732516feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELADDR, 732616feebcfSFlorian Westphal inet6_rtm_deladdr, NULL, 0); 732716feebcfSFlorian Westphal if (err < 0) 732816feebcfSFlorian Westphal goto errout; 732916feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETADDR, 733016feebcfSFlorian Westphal inet6_rtm_getaddr, inet6_dump_ifaddr, 733116feebcfSFlorian Westphal RTNL_FLAG_DOIT_UNLOCKED); 733216feebcfSFlorian Westphal if (err < 0) 733316feebcfSFlorian Westphal goto errout; 733416feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETMULTICAST, 733516feebcfSFlorian Westphal NULL, inet6_dump_ifmcaddr, 0); 733616feebcfSFlorian Westphal if (err < 0) 733716feebcfSFlorian Westphal goto errout; 733816feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETANYCAST, 733916feebcfSFlorian Westphal NULL, inet6_dump_ifacaddr, 0); 734016feebcfSFlorian Westphal if (err < 0) 734116feebcfSFlorian Westphal goto errout; 734216feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETNETCONF, 734316feebcfSFlorian Westphal inet6_netconf_get_devconf, 734416feebcfSFlorian Westphal inet6_netconf_dump_devconf, 734516feebcfSFlorian Westphal RTNL_FLAG_DOIT_UNLOCKED); 734616feebcfSFlorian Westphal if (err < 0) 734716feebcfSFlorian Westphal goto errout; 7348a3fde2adSFlorian Westphal err = ipv6_addr_label_rtnl_register(); 7349a3fde2adSFlorian Westphal if (err < 0) 7350a3fde2adSFlorian Westphal goto errout; 73512a8cc6c8SYOSHIFUJI Hideaki 73521da177e4SLinus Torvalds return 0; 7353c127ea2cSThomas Graf errout: 735416feebcfSFlorian Westphal rtnl_unregister_all(PF_INET6); 7355b382b191SThomas Graf rtnl_af_unregister(&inet6_ops); 7356c127ea2cSThomas Graf unregister_netdevice_notifier(&ipv6_dev_notf); 7357e0da5a48SPavel Emelyanov errlo: 7358c15b1ccaSHannes Frederic Sowa destroy_workqueue(addrconf_wq); 7359c15b1ccaSHannes Frederic Sowa out_nowq: 7360e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 73612cc6d2bfSNeil Horman out_addrlabel: 73622cc6d2bfSNeil Horman ipv6_addr_label_cleanup(); 73632cc6d2bfSNeil Horman out: 7364c127ea2cSThomas Graf return err; 73651da177e4SLinus Torvalds } 73661da177e4SLinus Torvalds 736709f7709fSDaniel Lezcano void addrconf_cleanup(void) 73681da177e4SLinus Torvalds { 7369176c39afSDaniel Lezcano struct net_device *dev; 73701da177e4SLinus Torvalds 73711da177e4SLinus Torvalds unregister_netdevice_notifier(&ipv6_dev_notf); 7372e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 73732cc6d2bfSNeil Horman ipv6_addr_label_cleanup(); 73741da177e4SLinus Torvalds 73755c45121dSFlorian Westphal rtnl_af_unregister(&inet6_ops); 73761da177e4SLinus Torvalds 73775c45121dSFlorian Westphal rtnl_lock(); 7378b382b191SThomas Graf 7379176c39afSDaniel Lezcano /* clean dev list */ 7380176c39afSDaniel Lezcano for_each_netdev(&init_net, dev) { 7381176c39afSDaniel Lezcano if (__in6_dev_get(dev) == NULL) 7382176c39afSDaniel Lezcano continue; 7383ae79dbf6SFlorent Fourcot addrconf_ifdown(dev, true); 7384176c39afSDaniel Lezcano } 7385ae79dbf6SFlorent Fourcot addrconf_ifdown(init_net.loopback_dev, true); 7386176c39afSDaniel Lezcano 73871da177e4SLinus Torvalds rtnl_unlock(); 7388c15b1ccaSHannes Frederic Sowa 7389c15b1ccaSHannes Frederic Sowa destroy_workqueue(addrconf_wq); 73901da177e4SLinus Torvalds } 7391